00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "jed_defs.h"
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <stdlib.h>
00031 #include "color.h"
00032
00033 #ifdef MOTIF_ENABLED // Librerias Motif
00034 #include <Xm/Xm.h>
00035 #endif
00036
00037 #include "jed_gl.h"
00038
00039 #include "toolkits/media/jed_pal.h"
00040 #include "lista.cc"
00041
00042
00043
00044
00045
00046 #define ESPERO(tipo, msg) \
00047 if ( tipo_token != (tipo) ) { \
00048 fprintf(stderr, "<PALETA> ERROR: Esperaba %s y recibi [%s].\n", \
00049 (msg), cad); fflush(stderr); return FALSE; \
00050 }
00051
00052
00053
00054
00055
00056 PIXEL_RGB::PIXEL_RGB() { r = g = b = 0;}
00057 PIXEL_RGBA::PIXEL_RGBA() { r = g = b = 0; a = 255;}
00058 PIXEL_PAL::PIXEL_PAL() { index = 0;}
00059 PIXEL_PAL::PIXEL_PAL(BYTE i) { index = i; }
00060
00061
00062
00063
00064
00065 PALETA::PALETA()
00069 {
00070 int i;
00071
00072 init(256);
00073
00074 for ( i = 0; i < tam_tabla; i++ ) {
00075 tabla[i].r = tabla[i].g = tabla[i].b =
00076 (BYTE)(i * (255/(double)(tam_tabla - 1)));
00077 tabla_r[i] = (float)((double)tabla[i].r / 255);
00078 tabla_g[i] = (float)((double)tabla[i].g / 255);
00079 tabla_b[i] = (float)((double)tabla[i].b / 255);
00080
00081
00082
00083 }
00084
00085 }
00086
00087 void
00088 PALETA::imprima(void)
00089 {
00090 int i;
00091
00092 printf("- <PALETA::imprima> Tabla de colores ----------------------------------\n");
00093 printf("Tabla con %d colores:\n", tam_tabla);
00094 for ( i = 0; i < tam_tabla; i++ ) {
00095 printf(" - Color %d: RGB[%d, %d, %d] (%.2f, %.2f, %.2f)\n",
00096 i, tabla[i].r, tabla[i].g, tabla[i].b,
00097 tabla_r[i], tabla_g[i], tabla_b[i]);
00098 }
00099 printf("-----------------------------------------------------------------------\n");
00100 }
00101
00102 PIXEL_RGB
00103 PALETA::pal2rgb(PIXEL_PAL i)
00104
00105
00106
00107 {
00108 return tabla[i.index];
00109 }
00110
00111 void
00112 PALETA::aplique_gamma(double factor)
00113 {
00114 int i;
00115
00116 for ( i = 0; i < tam_tabla; i++ ) {
00117 tabla[i].r = (BYTE)( (double)tabla[i].r * factor );
00118 tabla[i].g = (BYTE)( (double)tabla[i].g * factor );
00119 tabla[i].b = (BYTE)( (double)tabla[i].b * factor );
00120
00121 tabla_r[i] = (float)tabla[i].r / 255;
00122 tabla_g[i] = (float)tabla[i].g / 255;
00123 tabla_b[i] = (float)tabla[i].b / 255;
00124
00125
00126
00127
00128
00129
00130
00131 }
00132 }
00133
00134 #ifdef GL_ENABLED
00135 void
00136 PALETA::activar_gl(void)
00137 {
00138 glPixelMapfv(GL_PIXEL_MAP_I_TO_R, tam_tabla, tabla_r);
00139 glPixelMapfv(GL_PIXEL_MAP_I_TO_G, tam_tabla, tabla_g);
00140 glPixelMapfv(GL_PIXEL_MAP_I_TO_B, tam_tabla, tabla_b);
00141 }
00142 #endif
00143
00144 BOOLEAN
00145 PALETA::init(int tam)
00146 {
00147 tam_tabla = tam;
00148 tabla_r = new float[tam_tabla];
00149 tabla_g = new float[tam_tabla];
00150 tabla_b = new float[tam_tabla];
00151
00152 if ( !tabla_r || !tabla_g || !tabla_b ) {
00153 elim();
00154 return FALSE;
00155 }
00156 return TRUE;
00157 }
00158
00159 void
00160 PALETA::elim(void)
00161 {
00162 if ( tabla_r ) delete tabla_r;
00163 if ( tabla_g ) delete tabla_g;
00164 if ( tabla_b ) delete tabla_b;
00165
00166 tam_tabla = 0;
00167 }
00168
00169 void
00170 PALETA::importar_raw256(FILE *fd)
00171 {
00172 int i;
00173
00174 elim();
00175 init(256);
00176 fread(tabla, sizeof(BYTE), 256 * 3, fd);
00177 for ( i = 0; i < tam_tabla; i++ ) {
00178 tabla_r[i] = (float)tabla[i].r / 255;
00179 tabla_g[i] = (float)tabla[i].g / 255;
00180 tabla_b[i] = (float)tabla[i].b / 255;
00181
00182
00183
00184 }
00185 }
00186
00187 void
00188 PALETA::importar_dib(FILE *in_fd)
00194 {
00195 #ifdef FILESYSTEM_ENABLED
00196 int i;
00197 BYTE a;
00198
00199 for ( i = 0; i < tam_tabla; i++ ) {
00200 lea_BYTE(&tabla[i].b, in_fd);
00201 lea_BYTE(&tabla[i].g, in_fd);
00202 lea_BYTE(&tabla[i].r, in_fd);
00203 lea_BYTE(&a, in_fd);
00204
00205 tabla_r[i] = (float)tabla[i].r / 255;
00206 tabla_g[i] = (float)tabla[i].g / 255;
00207 tabla_b[i] = (float)tabla[i].b / 255;
00208
00209
00210
00211 }
00212 #endif
00213 }
00214
00215 #ifdef X_ENABLED
00216
00217 void
00218 PALETA::importe_x(Display *display, Colormap cmap)
00219 {
00220 XColor *Paleta_x = new XColor[tam_tabla];
00221 int i;
00222
00223 for ( i = 0; i < tam_tabla; i++ ) Paleta_x[i].pixel = i;
00224 XQueryColors(display, cmap, Paleta_x, tam_tabla);
00225 for ( i = 0; i < tam_tabla; i++ ) {
00226 tabla[i].r = Paleta_x[i].red/256;
00227 tabla[i].g = Paleta_x[i].green/256;
00228 tabla[i].b = Paleta_x[i].blue/256;
00229 tabla_r[i] = (double)tabla[i].r / 255;
00230 tabla_g[i] = (double)tabla[i].g / 255;
00231 tabla_b[i] = (double)tabla[i].b / 255;
00232 }
00233 delete Paleta_x;
00234 }
00235
00236 #endif // X_ENABLED
00237
00238 BOOLEAN
00239 PALETA::leer(TOKENIZADOR *Sabiondo)
00240 {
00241 #ifndef FILESYSTEM_ENABLED
00242 return FALSE;
00243 #endif
00244
00245 #ifdef FILESYSTEM_ENABLED
00246 char cad[1000];
00247 int tipo_token = TK_DESCONOCIDO, pos, i;
00248 COLOR color, *Color;
00249 LISTA <COLOR *>colores;
00250
00251
00252 pos = 1;
00253 while ( tipo_token != TK_CERRAR) {
00254 tipo_token = Sabiondo->siguiente_token(cad);
00255 switch ( pos ) {
00256 case 1:
00257 ESPERO(TK_CADENA, "una cadena");
00258 if ( strlen(cad) > MAX_CAD-1 ) cad[MAX_CAD-1] = '\0';
00259 des_comille(cad);
00260
00261 pos++;
00262 break;
00263 case 2: ESPERO(TK_ABRIR, "\"{\""); pos++; break;
00264 default:
00265 if ( tipo_token == TK_CERRAR ) break;
00266
00267 ESPERO(TK_VECTOR_INICIO, "el inicio de un COLOR");
00268 color.r = (float)atof(&cad[1]);
00269
00270 tipo_token = Sabiondo->siguiente_token(cad);
00271 ESPERO(TK_NUMERO, "un numero (dato 2 de un COLOR)");
00272 color.g = (float)atof(cad);
00273
00274 tipo_token = Sabiondo->siguiente_token(cad);
00275 ESPERO(TK_VECTOR_FIN, "el final de un COLOR");
00276 cad[strlen(cad) - 1] = '\0';
00277 color.b = (float)atof(cad);
00278
00279 Color = new COLOR;
00280 (*Color) = color;
00281 colores.anx(Color);
00282 break;
00283 }
00284 }
00285
00286 if ( !init(colores.tam()) ) {
00287 return FALSE;
00288 }
00289 for ( i = 0; i < tam_tabla; i++ ) {
00290 tabla[i].r = (BYTE)colores[i]->r;
00291 tabla[i].g = (BYTE)colores[i]->g;
00292 tabla[i].b = (BYTE)colores[i]->b;
00293 tabla_r[i] = (float)((double)tabla[i].r / 255);
00294 tabla_g[i] = (float)((double)tabla[i].g / 255);
00295 tabla_b[i] = (float)((double)tabla[i].b / 255);
00296 delete colores[i];
00297 }
00298 colores.elim();
00299
00300 return TRUE;
00301 #endif // FILESYSTEM_ENABLED
00302 }
00303
00304 #ifdef GL_ENABLED
00305
00306 void
00307 PALETA::pintar_gl(void)
00308 {
00309 double x_pos = 0.8;
00310 double y_pos = -0.99;
00311 double x_tam = 0.18;
00312 double y_tam = 1.96;
00313
00314 glDisable(GL_LIGHTING);
00315 glDisable(GL_DEPTH_TEST);
00316 glShadeModel(GL_SMOOTH);
00317 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00318
00319
00320 double y;
00321 int i;
00322
00323 glBegin(GL_QUAD_STRIP);
00324 for ( y = y_pos, i = 0; y < y_pos+y_tam; y += y_tam/(tam_tabla), i++ ) {
00325 glColor3d(tabla_r[i], tabla_g[i], tabla_b[i]);
00326 glVertex2d(x_pos, y); glVertex2d(x_pos+x_tam, y);
00327 }
00328 glVertex2d(x_pos, y); glVertex2d(x_pos+x_tam, y);
00329 glEnd();
00330
00331
00332 glColor3d(1, 1, 1);
00333 glBegin(GL_LINE_LOOP);
00334 glVertex2d(x_pos, y_pos);
00335 glVertex2d(x_pos+x_tam, y_pos);
00336 glVertex2d(x_pos+x_tam, y_pos+y_tam);
00337 glVertex2d(x_pos, y_pos+y_tam);
00338 glEnd();
00339 glEnable(GL_DEPTH_TEST);
00340 }
00341 #endif // GL_ENABLED
00342
00343 COLOR
00344 PALETA::escala(double val)
00355 {
00356 COLOR c;
00357 int i;
00358
00359 val *= tam_tabla;
00360 i = (int)val;
00361 c.r = tabla_r[i];
00362 c.g = tabla_g[i];
00363 c.b = tabla_b[i];
00364
00365 return c;
00366 }
00367
00368
00369
00370
00371
00372