00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "toolkits/media/jed_img.h"
00023
00024 #ifdef VEL_ROSITA
00025 #include "toolkits/media/img_rgba.h"
00026 #endif
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030
00031 #ifdef GL_VERSION_1_1 // OJO: Toca revisar si esta vaina funciona bien!
00032 #define GL_COMPLETO // Si se quita, parece que el otro (en jed_defs)
00033 #endif // no se activa bien
00034
00035
00036
00037
00038
00039 IMAGEN_RGBA::IMAGEN_RGBA()
00040 {
00041
00042 x_tam = y_tam = 0; Data = NULL;
00043 #ifdef GL_COMPLETO
00044 con_lista = FALSE;
00045 #endif
00046
00047 #ifdef MESA_ENABLED
00048 _contexto_osmesa = 0;
00049 #endif
00050
00051 }
00052
00053 IMAGEN_RGBA::~IMAGEN_RGBA()
00054 {
00055
00056 if ( Data ) elim();
00057 }
00058
00059 BOOLEAN
00060 IMAGEN_RGBA::init(int width, int height)
00067 {
00068
00069
00070 if ( x_tam == width && y_tam == height ) return TRUE;
00071 elim();
00072 Data = new PIXEL_RGBA[height * width];
00073 if ( !Data ) return FALSE;
00074 x_tam = width;
00075 y_tam = height;
00076 return TRUE;
00077 }
00078
00079 void
00080 IMAGEN_RGBA::elim(void)
00081 {
00082
00083 if ( Data ) {
00084 delete Data;
00085 Data = NULL;
00086 x_tam = y_tam = 0;
00087 }
00088 }
00089
00090 void
00091 IMAGEN_RGBA::exportar_ppm(FILE *fd)
00092 {
00093 char cad[180];
00094 int i, j;
00095
00096 sprintf(cad, "P6\n"
00097 "# PPM CREATED BY: AQUYNZA http://www.aquynza.org\n");
00098 fwrite(cad, sizeof(char), strlen(cad), fd);
00099 sprintf(cad, "%d %d\n", x_tam, y_tam);
00100 fwrite(cad, sizeof(char), strlen(cad), fd);
00101 sprintf(cad, "255\n");
00102 fwrite(cad, sizeof(char), 4, fd);
00103
00104
00105
00106 PIXEL_RGB pixel;
00107 PIXEL_RGBA *f2, p2;
00108
00109 for ( i = y_tam - 1; i >= 0 ; i-- ) {
00110 f2 = &((PIXEL_RGBA *)Data)[i*x_tam];
00111 for ( j = 0; j < x_tam ; j++ ) {
00112 p2 = f2[j];
00113 pixel.r = p2.r; pixel.g = p2.g; pixel.b = p2.b;
00114 fwrite(&pixel, sizeof(PIXEL_RGB), 1, fd);
00115 }
00116 }
00117 }
00118
00119
00120 #ifdef NNN
00121
00122 IMAGEN_PAL *
00123 IMAGEN_RGBA::exportar_a_grises(PALETA * )
00140 {
00141 IMAGEN_PAL *Nueva = NULL;
00142 int x, y;
00143 BYTE r, g, b, a;
00144
00145 Nueva = new IMAGEN_PAL();
00146
00147 if ( !Nueva->init(x_tam, y_tam) ) {
00148 fprintf(stderr, "Warning[JED_IMG]: No hay memoria para crear una "
00149 "imagen en tonos de gris de %d x %d pixels!\n", x_tam, y_tam);
00150 fflush(stderr);
00151 return NULL;
00152 }
00153
00154 for ( x = 0; x < x_tam; x++ ) {
00155 for ( y = 0; y < y_tam; y++ ) {
00156 getpixel(x, y, r, g, b, a);
00157
00158
00159
00160 Nueva->putcolorindex(x, y, nivel_de_gris(r, g, b));
00161 }
00162 }
00163
00164
00165
00166 return Nueva;
00167 }
00168
00169 IMAGEN *
00170 IMAGEN_RGBA::copie(void)
00171 {
00172 IMAGEN_RGBA *Nueva;
00173 int x, y;
00174 BYTE r, g, b, a;
00175
00176 Nueva = new IMAGEN_RGBA();
00177 if ( !Nueva ) {
00178 fprintf(stderr,"ERROR: No hay memoria para crear una nueva imagen!\n");
00179 fflush(stderr);
00180 return NULL;
00181 }
00182
00183 if ( !Nueva->init(x_tam, y_tam) ) {
00184 fprintf(stderr,"ERROR: No hay memoria para crear una nueva imagen!\n");
00185 fflush(stderr);
00186 delete Nueva;
00187 return NULL;
00188 }
00189
00190 for ( x = 0; x < x_tam; x++ ) {
00191 for ( y = 0; y < y_tam; y++ ) {
00192 getpixel(x, y, r, g, b, a);
00193 Nueva->putpixel(x, y, r, g, b, a);
00194 }
00195 }
00196
00197 return Nueva;
00198 }
00199
00200 #endif // NNN
00201
00202 #ifdef GL_ENABLED
00203 void
00204 IMAGEN_RGBA::pintar_gl(void)
00205 {
00206
00207 if ( !Data ) {
00208 fprintf(stderr,
00209 "<IMAGEN_RGBA> ERROR: Intentando pintar una imagen nula.\n");
00210 fflush(stderr);
00211 return;
00212 }
00213 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00214 glRasterPos2f(-1, -1);
00215 glDrawPixels(x_tam, y_tam, GL_RGBA, GL_UNSIGNED_BYTE, (void*)Data);
00216 }
00217
00218 void
00219 IMAGEN_RGBA::activar_gl(void)
00220 {
00221
00222
00223 if ( !Data ) {
00224 fprintf(stderr,
00225 "<IMAGEN_RGB> ERROR: Intentando activar una imagen nula.\n");
00226 fflush(stderr);
00227 return;
00228 }
00229
00230 if ( !(x_tam % 4) )
00231 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
00232 else if ( !(x_tam % 2) )
00233 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
00234 else
00235 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00236
00237 #ifndef GL_COMPLETO
00238
00239
00240 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, x_tam, y_tam, GL_RGB,
00241 GL_UNSIGNED_BYTE, (void*)Data);
00242 #endif
00243 #ifdef GL_COMPLETO
00244 if ( ! con_lista ) {
00245 glGenTextures(1, &lista_gl);
00246 glBindTexture(GL_TEXTURE_2D, lista_gl);
00247 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, x_tam, y_tam, GL_RGBA,
00248 GL_UNSIGNED_BYTE, (void*)Data);
00249 con_lista = TRUE;
00250 }
00251 glBindTexture(GL_TEXTURE_2D, lista_gl);
00252 #endif
00253 }
00254
00255 void
00256 IMAGEN_RGBA::activar_como_contexto_gl(void)
00257 {
00258 #ifdef MESA_ENABLED
00259 _contexto_osmesa = OSMesaCreateContext(GL_RGBA, NULL);
00260 OSMesaMakeCurrent(_contexto_osmesa,
00261 (void *)Data, GL_UNSIGNED_BYTE, x_tam, y_tam);
00262 #endif
00263 #ifndef MESA_ENABLED
00264 fprintf(stderr, "<IMAGEN_RGBA> - ERROR: El contexto GL sobre una imagen\n"
00265 "solo ha sido implementado para MESA!\n");
00266 fflush(stderr);
00267 exit(-4);
00268 #endif
00269 }
00270
00271 void
00272 IMAGEN_RGBA::desactivar_como_contexto_gl(void)
00273 {
00274 #ifdef MESA_ENABLED
00275 if ( _contexto_osmesa ) OSMesaDestroyContext(_contexto_osmesa);
00276 #endif
00277 }
00278
00279 #endif // GL_ENABLED
00280
00281
00282 BOOLEAN
00283 IMAGEN_RGBA::generar_con_stencil(IMAGEN_RGB *Origen, BYTE r, BYTE g, BYTE b)
00292 {
00293
00294 if ( !init(Origen->xtam(), Origen->ytam()) ) return FALSE;
00295
00296
00297 int x, y;
00298 BYTE l = 255, m = 100, n = 10, a = 255;
00299
00300 for ( x = 0; x < xtam(); x++ ) {
00301 for ( y = 0; y < ytam(); y++ ) {
00302 Origen->getpixel(x, y, l, m, n);
00303 if ( l == r && m == g && n == b ) a = 0;
00304 else a = 255;
00305 putpixel(x, y, l, m, n, a);
00306 }
00307 }
00308 return TRUE;
00309 }
00310
00311
00312
00313
00314