home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / lib / c_map.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-15  |  5.1 KB  |  187 lines

  1. /*    C_MAP . C
  2. %
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.     Jin Guojun
  27.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  28.     g_jin@lbl.gov
  29.  
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. % AUTHOR:    Jin Guojun - LBL    10/1/91
  33. */
  34.  
  35. #include "header.def"
  36. #include "imagedef.h"
  37.  
  38. #ifndef    MaxColors
  39. #define    MaxColors    256
  40. #endif
  41.  
  42. sht_cmap_t    *r_cmap;    /* for matching rle_map */
  43. cmap_t*        reg_cmap[3];    /* for global usage */
  44.  
  45.  
  46. min_bits(val)    /* minimum bits=(log2(v) + 1) for integer value  */
  47. {
  48. register int    i=1;
  49. if (val > 0)
  50.     while (val>>=1)    i++;
  51. return    i;
  52. }
  53.  
  54. VType*
  55. rgbmap_to_othermap(cmap, number, reg_or_rle)
  56. color_cell    *cmap;
  57. {
  58. register int    i=number;
  59.     if (reg_or_rle) {    /* to regular cmap */
  60.     if (verify_buffer_size(reg_cmap, i, 3*sizeof(cmap_t), "rgb-reg")) {
  61.         reg_cmap[1] = reg_cmap[0] + i;
  62.         reg_cmap[2] = reg_cmap[1] + i;
  63.     }
  64.     for (i=0; i < number; ++i)
  65.         reg_cmap[0][i] = cmap[i].r,
  66.         reg_cmap[1][i] = cmap[i].g,
  67.         reg_cmap[2][i] = cmap[i].b;
  68.     return    (VType *) reg_cmap[0];
  69.     }
  70.     else {    /* to RLE cmap, always be 2**min_bits */
  71.     register int    j=1<<min_bits(i-1);
  72.     verify_buffer_size(&r_cmap, j, 3*sizeof(*r_cmap), "rgb-rle");
  73.     for (i=0; i < number; ++i) {
  74.         r_cmap[i] = cmap[i].r << 8;
  75.         r_cmap[j+i] = cmap[i].g << 8;
  76.         r_cmap[(j<<1)+i] = cmap[i].b << 8;
  77.     }
  78.     return    (VType *)r_cmap;
  79.     }
  80. }
  81.  
  82. sht_cmap_t*
  83. regmap_to_rlemap(cmap, number, channels, rle_hd)
  84. cmap_t    *cmap[3];
  85. rle_hdr    *rle_hd;
  86. {
  87. register int    i=min_bits(MIN(MaxColors, number)-1), j=1<<i;
  88. /*    rle_hd->ncolors = 3;    is always 3 color channels    */
  89.     rle_hd->cmaplen = i;
  90.     if (!(rle_hd->ncmap = channels))
  91.         return    NULL;
  92.     verify_buffer_size(&r_cmap, j, channels*sizeof(*r_cmap), "reg-rle");
  93.     rle_hd->cmap = (rle_map *)r_cmap;
  94.     for (i=0; i < number; ++i) {
  95.         r_cmap[i] = cmap[0][i] << 8;
  96.         r_cmap[j+i] = cmap[1][i] << 8;
  97.         r_cmap[(j<<1)+i] = cmap[2][i] << 8;
  98.     }
  99. return    r_cmap;
  100. }
  101.  
  102. cmap_t*
  103. rlemap_to_regmap(cmap, rle_hd)
  104. cmap_t    *cmap[3];
  105. rle_hdr    *rle_hd;
  106. {
  107. register rle_map*    rmap = rle_hd->cmap;
  108. register int    i = 1 << rle_hd->cmaplen;
  109.  
  110.     if (verify_buffer_size(cmap, i, 3*sizeof(cmap_t), "rle-reg")) {
  111.     cmap[1] = cmap[0] + i;
  112.     cmap[2] = cmap[1] + i;
  113.     }
  114. #ifndef    EXACT_256_RLE_TO_REG
  115.     for (i *= rle_hd->ncmap; i--;)
  116.     cmap[0][i] = rmap[i] >> 8;
  117. #else
  118.     {    register int    c=i
  119.     while (c--)
  120.         cmap[0][c] = rlemap[c] >> 8,
  121.         cmap[1][c] = rlemap[i+c] >> 8,
  122.         cmap[2][c] = rlemap[(i<<1)+c] >> 8;
  123.     }
  124. #endif
  125. return    cmap[0];
  126. }
  127.  
  128. CloseColor_in_Map(cmap, ncolors, r, g, b, Pseudo /* better is 384 */)
  129. register cmap_t    *cmap[];
  130. register int    r, g, b;
  131. {
  132. register int    dis, i;
  133. int    value=0;
  134.  
  135. for (i=0; i < ncolors; i++) {
  136.     dis = abs(cmap[0][i] - r) + abs(cmap[1][i] - g) + abs(cmap[2][i] - b);
  137.     if (!dis)
  138.         return    i;    /* find exact value    */
  139.     else if (dis < Pseudo) {
  140.         Pseudo = dis;
  141.         value = i;    /* get the closest one    */
  142.     }
  143. }
  144. return    value;
  145. }
  146.  
  147. isColorImage(cfmt)
  148. register int    cfmt;
  149. {
  150. register isc =    cfmt != CFM_SGF && cfmt != CFM_BITMAP;
  151. if (isc && cfmt != CFM_SCF)    isc = 3;
  152. return    isc;
  153. }
  154.  
  155. select_color_form(img, osamei8)
  156. register U_IMAGE*    img;
  157. {
  158. int    cfm;    /* no change, special in a TiFF case. May change later on */
  159. if (img->in_type != TiFF || img->color_form != CFM_BITMAP)
  160.     if (img->color_dpy)
  161.         switch(img->mid_type)    {
  162.         case RLE:    case COLOR_PS:    /* no SEPLANE for RLE    */
  163.         if (img->color_dpy > 0 || img->in_color == CFM_SEPLANE) {
  164.             if (isColorImage(img->in_color))
  165.             if (img->channels > 1 || !osamei8)
  166.                 img->in_form = IFMT_ILL,
  167.                 cfm = CFM_ILL;
  168.             else    cfm = CFM_SCF;
  169.             else    cfm = CFM_SGF;
  170.             break;
  171.         }
  172.         case HIPS:    /* no CFM_ILL | CFM_BITMAP output in HIPS    */
  173.         cfm = img->in_color;
  174.         if (img->mid_type == HIPS && cfm == CFM_BITMAP)    cfm = CFM_SGF;
  175.         if (cfm != CFM_ILL || img->mid_type != HIPS)    break;
  176.         default:    cfm = CFM_ILC;
  177.         }
  178.     else    cfm = CFM_SGF;
  179.  
  180.     if (img->mid_type == HIPS && !img->frames)    /* for color only */
  181.             img->frames = 1,    img->in_form = IFMT_BYTE;
  182.     if (cfm == CFM_ALPHA)    cfm--;    /* get rid of the alpha channel    */
  183.     img->dpy_channels = isColorImage(img->color_form = cfm);
  184.     img->dpy_channels += !img->dpy_channels;    /* at least 1 channel    */
  185.     img->mono_img = img->dpy_channels==1;
  186. }
  187.