TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

gdisp_colormap.c

Go to the documentation of this file.
00001 
00043 /*
00044  * System includes.
00045  */
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <assert.h>
00049 #include <string.h>
00050 
00051 
00052 /*
00053  * GDISP+ includes.
00054  */
00055 #include "gdisp_kernel.h"
00056 #include "gdisp_prototypes.h"
00057 
00058 
00059 /*
00060  --------------------------------------------------------------------
00061                              STATIC ROUTINES
00062  --------------------------------------------------------------------
00063 */
00064 
00065 #define _GD_MAX_COLOR_IN_DEFAULT_COLORMAP_ 170
00066 
00067 
00068 /*
00069  * HLS Tool.
00070  */
00071 static gdouble
00072 gdisp_hlsUtil (gdouble n1,
00073                gdouble n2,
00074                gdouble hue)
00075 {
00076 
00077   gdouble val = 0.0;
00078    
00079   if (hue > 360.0)
00080     hue -= 360.0;
00081 
00082   if (hue < 000.0)
00083     hue += 360.0;
00084 
00085   if (hue < 060.0)
00086     val = n1 + (n2 - n1) * hue / 060.0;
00087   else if (hue < 180.0)
00088     val = n2;
00089   else if (hue < 240.0)
00090     val = n1 + (n2 - n1) * (240.0 - hue) / 060.0;
00091   else
00092     val = n1;
00093 
00094   return val;
00095 
00096 }
00097 
00098 
00099 /*
00100  * HLS to RGB.
00101  */
00102 static void 
00103 gdisp_hls2rgb (gdouble  hue,
00104                gdouble  light,
00105                gdouble  saturation,
00106                gdouble *red,
00107                gdouble *green,
00108                gdouble *blue)
00109 
00110 {
00111 
00112   gdouble m1 = 0.0,
00113           m2 = 0.0;
00114 
00115   if (light < 0.5)
00116     m2 = light * (1.0 + saturation);
00117   else
00118     m2 = light + saturation - light * saturation;
00119 
00120   m1 = 2.0 * light - m2;
00121 
00122   if (saturation == 0.0) {
00123 
00124     *red   = light;
00125     *green = light;
00126     *blue  = light;
00127 
00128   }
00129   else {
00130 
00131     *red   = gdisp_hlsUtil(m1,m2,(gdouble)(hue + 120.0));
00132     *green = gdisp_hlsUtil(m1,m2,(gdouble)(hue        ));
00133     *blue  = gdisp_hlsUtil(m1,m2,(gdouble)(hue - 120.0));
00134 
00135   }
00136 
00137 }
00138 
00139 
00140 /*
00141  * Compute the definition of each color.
00142  * Set up the right value for RED, GREEN and BLUE components.
00143  */
00144 static void
00145 gdisp_getColorDefinition (GdkColor *colors)
00146 {
00147 
00148 #define _GD_COLOR_COEF_ 255.0
00149 
00150   guint   i     = 0;
00151   gdouble h     = 0.0,
00152           l     = 0.0,
00153           s     = 0.0,
00154           r     = 0.0,
00155           g     = 0.0,
00156           b     = 0.0,
00157           val   = 0.0,
00158           incr  = 0.0,
00159           h_min = 0.0,
00160           h_max = 0.0;
00161 
00162 
00163   /*
00164    * Create Red color variations.
00165    */
00166   r = 0.4;
00167   g = 0.0;
00168   b = 0.0;
00169 
00170   for (i=0; i<3; i++) {
00171 
00172     colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00173     colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00174     colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00175 
00176     r = r + 0.2;
00177 
00178   }
00179   
00180   r = 1.0;
00181   g = 0.3;
00182   b = 0.3;
00183 
00184   for (i=3; i<7; i++) {
00185 
00186     colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00187     colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00188     colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00189 
00190     g = g + 0.1;
00191     b = b + 0.1;
00192 
00193   }
00194 
00195 
00196   /*
00197    * Create green color variations.
00198    */
00199   r = 0.0;
00200   g = 0.4;
00201   b = 0.0;
00202 
00203    for (i=7; i<12; i++) {
00204 
00205      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00206      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00207      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00208 
00209      g = g + 0.135;
00210 
00211    }
00212 
00213    r = 0.4;
00214    g = 1.0;
00215    b = 0.4;
00216 
00217    for (i=12; i<14; i++) {
00218 
00219      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00220      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00221      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00222 
00223      b = b + 0.2;
00224      r = r + 0.2;
00225 
00226    }
00227 
00228 
00229    /*
00230     * Create yellow color variations.
00231     */
00232    r = 1.0;
00233    g = 0.6;
00234    b = 0.0;
00235 
00236    for (i=14; i<19; i++) {
00237 
00238      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00239      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00240      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00241 
00242      g = g + 0.1;
00243 
00244    }
00245 
00246    r = 1.0;
00247    g = 1.0;
00248    b = 0.5;
00249 
00250    for (i=19; i<21; i++) {
00251 
00252      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00253      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00254      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00255 
00256      b = b + 0.25;
00257 
00258    }
00259 
00260 
00261    /*
00262     * Create blue color variations.
00263     */
00264    r = 0.0;
00265    g = 0.0;
00266    b = 0.4;
00267 
00268    for (i=21; i<25; i++) {
00269 
00270      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00271      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00272      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00273 
00274      b = b + 0.2;
00275 
00276    }
00277 
00278    r = 0.30;
00279    g = 0.42;
00280    b = 1.00;
00281 
00282    for (i=25; i<28; i++) {
00283 
00284      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00285      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00286      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00287 
00288      r = r + 0.18;
00289      g = g + 0.22;
00290 
00291    }
00292 
00293 
00294    /*
00295     * Create magenta color variations.
00296     */
00297    r = 0.4;
00298    g = 0.0;
00299 
00300    for (i=28; i<35; i++) {
00301 
00302      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00303      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00304      colors[i].blue  = (gushort)(r * _GD_COLOR_COEF_);
00305 
00306      r = r + 0.1;
00307      g = g + 0.7 / 6.0;
00308 
00309    }
00310 
00311 
00312    /*
00313     * Create cyan color variations.
00314     */
00315    r = 0.0;
00316    g = 0.5;
00317    b = 0.5;
00318 
00319    for (i=35; i<39; i++) {
00320 
00321      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00322      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00323      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00324 
00325      g = g + 0.15;
00326      b = b + 0.15;
00327 
00328    }
00329 
00330    r = 0.2;
00331    g = 1.0;
00332    b = 1.0;
00333 
00334    for (i=39; i<42; i++) {
00335 
00336      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00337      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00338      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00339 
00340      r = r + 0.3;
00341 
00342    }
00343 
00344 
00345    /*
00346     * Create variations from black color to white color.
00347     */
00348    r = 0.0;
00349 
00350    for (i=42; i<49; i++) {
00351 
00352      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00353      colors[i].green = (gushort)(r * _GD_COLOR_COEF_);
00354      colors[i].blue  = (gushort)(r * _GD_COLOR_COEF_);
00355 
00356      r = r + 0.5 / 7.0;
00357 
00358    }
00359 
00360    r = 0.5;
00361 
00362    for (i=49; i<56; i++) {
00363 
00364      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00365      colors[i].green = (gushort)(r * _GD_COLOR_COEF_);
00366      colors[i].blue  = (gushort)(r * _GD_COLOR_COEF_);
00367 
00368      r = r + 0.5 / 8.0;
00369 
00370    }
00371 
00372 
00373    /*
00374     * Create variations from blue to red, passing by green and yellow.
00375     */
00376    r = 0.0;
00377    g = 0.0;
00378    b = 1.0;
00379 
00380    for (i=56; i<61; i++) {
00381 
00382      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00383      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00384      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00385 
00386      g = g + 0.2;
00387 
00388    }
00389 
00390    r = 0.0;
00391    g = 1.0;
00392    b = 1.0;
00393 
00394    for (i=61;i<63;i++) {
00395 
00396      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00397      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00398      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00399 
00400      b = b - 0.5;
00401 
00402    }
00403 
00404    r = 0.0;
00405    g = 1.0;
00406    b = 0.0;
00407 
00408    for (i=63; i<65; i++) {
00409 
00410      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00411      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00412      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00413   
00414      r = r + 0.5;
00415 
00416    }
00417 
00418    r = 1.0;
00419    g = 1.0;
00420    b = 0.0;
00421 
00422    for (i=65; i<70; i++) {
00423 
00424      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00425      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00426      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00427 
00428      g = g - 0.25;
00429 
00430    }
00431 
00432 
00433    /*
00434     * 50 variations from red to blue for post-processing.
00435     */
00436    h_min = -10.0;
00437    h_max = 250.0;
00438 
00439    incr = (h_max - h_min) / 50.0;
00440 
00441    h = h_min;
00442    l = 0.5;
00443    s = 1.0;
00444 
00445    for (i=70; i<120; i++) {
00446 
00447      gdisp_hls2rgb(h,l,s,&r,&g,&b);
00448 
00449      colors[i].red   = (gushort)(r * _GD_COLOR_COEF_);
00450      colors[i].green = (gushort)(g * _GD_COLOR_COEF_);
00451      colors[i].blue  = (gushort)(b * _GD_COLOR_COEF_);
00452 
00453      h += incr;
00454 
00455    }
00456 
00457 
00458    /*
00459     * 50 variations from white to black for post-processing.
00460     */
00461    val = 15000.0;
00462    for (i=120; i<170; i++) {
00463 
00464      val = val + 1000.0;
00465 
00466      colors[i].red   = (gushort)(val / _GD_COLOR_COEF_);
00467      colors[i].green = (gushort)(val / _GD_COLOR_COEF_);
00468      colors[i].blue  = (gushort)(val / _GD_COLOR_COEF_);
00469 
00470    }
00471 
00472 
00473    /*
00474     * Change red, green, blue intervals.
00475     */
00476    for (i=0; i<_GD_MAX_COLOR_IN_DEFAULT_COLORMAP_; i++) {
00477 
00478      colors[i].red   *= 257;
00479      colors[i].green *= 257;
00480      colors[i].blue  *= 257;
00481 
00482    }
00483 
00484 }
00485 
00486 
00487 /*
00488  * Returns position of highest set bit in 'ul'
00489  * as an integer (0-31), or -1 if none.
00490  */
00491 #if defined(GDISP_FIRST_METHOD)
00492 
00493 static gint
00494 gdisp_highBit(gulong ul)
00495 {
00496 
00497    gint    i = 0;
00498    gulong hb = 0x8000;
00499 
00500    hb = hb << 16;
00501    for (i=31; ((ul & hb) == 0) && i>=0; i--, ul<<=1);
00502 
00503    return i;
00504 
00505 }
00506 
00507 #endif
00508 
00509 
00510 /*
00511  * Deduce pixel information from RGB inputs and visual parameters.
00512  * TRUE_COLOR visual specific.
00513  */
00514 static gulong
00515 gdisp_rgb2pixel (GdkVisual *visual,
00516                  gushort    red,
00517                  gushort    green,
00518                  gushort    blue)
00519 {
00520 
00521 #if !defined(GDISP_FIRST_METHOD)
00522 
00523   return (gulong)
00524          (((red   >> (16 - visual->red_prec  )) << visual->red_shift  ) |
00525           ((green >> (16 - visual->green_prec)) << visual->green_shift) |
00526           ((blue  >> (16 - visual->blue_prec )) << visual->blue_shift ));
00527 
00528 #else
00529 
00530   gulong ulRed      = (gulong)red,
00531          ulGreen    = (gulong)green,
00532          ulBlue     = (gulong)blue;
00533   gint   redShift   = 0,
00534          greenShift = 0,
00535          blueShift  = 0;
00536     
00537 
00538   /*
00539    * Shift r,g,b so that high bit of 16-bit color specification is 
00540    * aligned with high bit of r,g,b-mask in visual, 
00541    * AND each component with its mask, and OR the three components together.
00542    */
00543   redShift   = 15 - gdisp_highBit(visual->red_mask  );
00544   greenShift = 15 - gdisp_highBit(visual->green_mask);
00545   blueShift  = 15 - gdisp_highBit(visual->blue_mask );
00546 
00547   if (redShift < 0)
00548     ulRed = ulRed     << (-redShift);
00549   else
00550     ulRed = ulRed     >>   redShift;
00551 
00552   if (greenShift < 0)
00553     ulGreen = ulGreen << (-greenShift);
00554   else
00555     ulGreen = ulGreen >>   greenShift;
00556 
00557   if (blueShift < 0)
00558     ulBlue = ulBlue   << (-blueShift);
00559   else
00560     ulBlue = ulBlue   >>   blueShift;
00561 
00562   ulRed   = ulRed   & visual->red_mask;
00563   ulGreen = ulGreen & visual->green_mask;
00564   ulBlue  = ulBlue  & visual->blue_mask;
00565 
00566   return (red | green | blue);
00567 
00568 #endif
00569 
00570 }
00571 
00572 
00573 
00574 /*
00575  --------------------------------------------------------------------
00576                              PUBLIC ROUTINES
00577  --------------------------------------------------------------------
00578 */
00579 
00580 
00581 /*
00582  * Affect a color to a provider.
00583  */
00584 GdkColor*
00585 gdisp_getProviderColor (Kernel_T *kernel,
00586                         gint      providerId)
00587 {
00588 
00589   /*
00590    * Definition of colors affected to each provider.
00591    */
00592   gint providerColors[]  = { 6, 10, 16, 27, 33, 37 };
00593   gint providerColorSize = GD_MAX_PROVIDER_NUMBER;
00594 
00595   assert(kernel->colors);
00596 
00597   if (providerId < providerColorSize) {
00598 
00599     return &kernel->colors[providerColors[providerId]];
00600 
00601   }
00602   else {
00603 
00604     return (GdkColor*)NULL;
00605 
00606   }
00607 
00608 }
00609 
00610 
00611 /*
00612  * Create GDISP+ colormap.
00613  */
00614 void
00615 gdisp_createColormap (Kernel_T *kernel)
00616 {
00617 
00618   gboolean *successTable    = (gboolean*)NULL;
00619   gint      allocatedColors = 0,
00620             colorCpt        = 0;
00621   GString  *messageString   = (GString*)NULL;
00622 
00623 
00624   assert(kernel);
00625 
00626   /*
00627    * Get back system colormap.
00628    * FIXME : Work with an enhanced colormap system management.
00629    */
00630   kernel->colormap = gdk_colormap_get_system();
00631   kernel->visual   = gdk_colormap_get_visual(kernel->colormap);
00632 
00633 
00634   /*
00635    * Define all requested colors.
00636    */
00637   kernel->colorNumber = _GD_MAX_COLOR_IN_DEFAULT_COLORMAP_;
00638   kernel->colors      = (GdkColor*)g_malloc0(kernel->colorNumber *
00639                                              sizeof(GdkColor));
00640   assert(kernel->colors);
00641 
00642 
00643   /*
00644    * Get back color definition.
00645    */
00646   gdisp_getColorDefinition(kernel->colors);
00647 
00648 
00649   /*
00650    * Try to allocate these colors.
00651    * Watch out visual type !!!
00652    * Colors are not writable, thus they can be shared among applications.
00653    */
00654   switch (kernel->visual->type) {
00655 
00656   case GDK_VISUAL_PSEUDO_COLOR :
00657 
00658     successTable = (gboolean*)g_malloc0(kernel->colorNumber *
00659                                         sizeof(gboolean));
00660     assert(successTable);
00661 
00662     allocatedColors = gdk_colormap_alloc_colors(kernel->colormap,
00663                                                 kernel->colors,
00664                                                 kernel->colorNumber,
00665                                                 FALSE, /* not writable     */
00666                                                 TRUE,  /* perform matching */
00667                                                 successTable);
00668 
00669     g_free(successTable);
00670     successTable = (gboolean*)NULL;
00671     break;
00672 
00673   case GDK_VISUAL_TRUE_COLOR :
00674 
00675     for (colorCpt=0; colorCpt<kernel->colorNumber; colorCpt++) {
00676 
00677       kernel->colors[colorCpt].pixel =
00678         gdisp_rgb2pixel(kernel->visual,
00679                         kernel->colors[colorCpt].red,
00680                         kernel->colors[colorCpt].green,
00681                         kernel->colors[colorCpt].blue);
00682 
00683 #if defined(GD_COLORMAP_DEBUG)
00684       fprintf(stdout,
00685               "Color %d : r=%d, g=%d, b=%d, pixel = %ld\n",
00686               colorCpt + 1,
00687               kernel->colors[colorCpt].red,
00688               kernel->colors[colorCpt].green,
00689               kernel->colors[colorCpt].blue,
00690               kernel->colors[colorCpt].pixel);
00691 #endif
00692 
00693     }
00694 
00695 #if defined(GD_COLORMAP_DEBUG)
00696     fflush(stdout);
00697 #endif
00698 
00699     /* No problem for allocating all colors in TRUECOLOR */
00700     allocatedColors = kernel->colorNumber;
00701     break;
00702 
00703   default :
00704     break;
00705 
00706   }
00707 
00708 
00709   /*
00710    * Output message.
00711    */
00712   messageString = g_string_new((gchar*)NULL);
00713   if (allocatedColors == kernel->colorNumber) {
00714 
00715     g_string_sprintf(messageString,
00716                      "All %d requested colors have been allocated.",
00717                      kernel->colorNumber);
00718     kernel->outputFunc(kernel,messageString,GD_MESSAGE);
00719   
00720 
00721   }
00722   else {
00723 
00724     g_string_sprintf(messageString,
00725                      "Only %d colors out of %d have been allocated.",
00726                      allocatedColors,
00727                      kernel->colorNumber);
00728     kernel->outputFunc(kernel,messageString,GD_WARNING);
00729 
00730   }
00731 
00732 }
00733 
00734 
00735 /*
00736  * Destroy GDISP+ colormap.
00737  */
00738 void
00739 gdisp_destroyColormap (Kernel_T *kernel)
00740 {
00741 
00742 
00743   /*
00744    * Free all colors into the colormap.
00745    */
00746   if (kernel->visual->type == GDK_VISUAL_PSEUDO_COLOR) {
00747 
00748     if (kernel->colorNumber > 0) {
00749 
00750       gdk_colormap_free_colors(kernel->colormap,
00751                                kernel->colors,
00752                                kernel->colorNumber);
00753 
00754     }
00755 
00756   }
00757 
00758 
00759   /*
00760    * Free mempry.
00761    */
00762   if (kernel->colors != (GdkColor*)NULL) {
00763     g_free(kernel->colors);
00764   }
00765 
00766   kernel->colors      = (GdkColor*)NULL;
00767   kernel->colorNumber = 0;
00768 
00769 }
00770 
Framework Home Page.

Beware !! TSP wave is coming...