i_color.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains device-specific color database access routines.
  16. These routines are called by various GIFPlotter methods, before drawing
  17. objects. They set the appropriate GIFPlotter-specific fields in the
  18. drawing state. */
  19. #include "sys-defines.h"
  20. #include "extern.h"
  21. /* forward references */
  22. static int bit_depth (int colors);
  23. /* we call this routine to evaluate _plotter->drawstate->i_pen_color_index
  24. lazily, i.e. only when needed (just before a drawing operation) */
  25. void
  26. _pl_i_set_pen_color(S___(Plotter *_plotter))
  27. {
  28. int red_long, green_long, blue_long;
  29. int red, green, blue;
  30. /* 48-bit RGB */
  31. red_long = _plotter->drawstate->fgcolor.red;
  32. green_long = _plotter->drawstate->fgcolor.green;
  33. blue_long = _plotter->drawstate->fgcolor.blue;
  34. /* 24-bit RGB (as used in GIFs) */
  35. red = (((unsigned int)red_long) >> 8) & 0xff;
  36. green = (((unsigned int)green_long) >> 8) & 0xff;
  37. blue = (((unsigned int)blue_long) >> 8) & 0xff;
  38. if (!(_plotter->drawstate->i_pen_color_status
  39. && _plotter->drawstate->i_pen_color.red == red
  40. && _plotter->drawstate->i_pen_color.green == green
  41. && _plotter->drawstate->i_pen_color.blue == blue))
  42. /* need another color index: search table, expand if necessary */
  43. {
  44. unsigned char index;
  45. index = _pl_i_new_color_index (R___(_plotter) red, green, blue);
  46. /* set new 24-bit RGB and color index in the drawing state */
  47. _plotter->drawstate->i_pen_color.red = red;
  48. _plotter->drawstate->i_pen_color.green = green;
  49. _plotter->drawstate->i_pen_color.blue = blue;
  50. _plotter->drawstate->i_pen_color_index = index;
  51. /* flag this color index as genuine */
  52. _plotter->drawstate->i_pen_color_status = true;
  53. }
  54. }
  55. /* we call this routine to evaluate _plotter->drawstate->i_fill_color_index
  56. lazily, i.e. only when needed (just before a filling operation) */
  57. void
  58. _pl_i_set_fill_color(S___(Plotter *_plotter))
  59. {
  60. int red_long, green_long, blue_long;
  61. int red, green, blue;
  62. if (_plotter->drawstate->fill_type == 0)
  63. /* don't do anything, fill color will be ignored when writing objects */
  64. return;
  65. /* 48-bit RGB */
  66. red_long = _plotter->drawstate->fillcolor.red;
  67. green_long = _plotter->drawstate->fillcolor.green;
  68. blue_long = _plotter->drawstate->fillcolor.blue;
  69. /* 24-bit RGB (as used in GIFs) */
  70. red = (((unsigned int)red_long) >> 8) & 0xff;
  71. green = (((unsigned int)green_long) >> 8) & 0xff;
  72. blue = (((unsigned int)blue_long) >> 8) & 0xff;
  73. if (!(_plotter->drawstate->i_fill_color_status
  74. && _plotter->drawstate->i_fill_color.red == red
  75. && _plotter->drawstate->i_fill_color.green == green
  76. && _plotter->drawstate->i_fill_color.blue == blue))
  77. /* need another color index: search table, expand if necessary */
  78. {
  79. unsigned char index;
  80. index = _pl_i_new_color_index (R___(_plotter) red, green, blue);
  81. /* set new 24-bit RGB and color index in the drawing state */
  82. _plotter->drawstate->i_fill_color.red = red;
  83. _plotter->drawstate->i_fill_color.green = green;
  84. _plotter->drawstate->i_fill_color.blue = blue;
  85. _plotter->drawstate->i_fill_color_index = index;
  86. }
  87. }
  88. /* we call this routine to compute and set
  89. _plotter->drawstate->i_bg_color_index, e.g. in _pl_i_new_image */
  90. void
  91. _pl_i_set_bg_color(S___(Plotter *_plotter))
  92. {
  93. int red_long, green_long, blue_long;
  94. int red, green, blue;
  95. /* 48-bit RGB */
  96. red_long = _plotter->drawstate->bgcolor.red;
  97. green_long = _plotter->drawstate->bgcolor.green;
  98. blue_long = _plotter->drawstate->bgcolor.blue;
  99. /* 24-bit RGB (as used in GIFs) */
  100. red = (((unsigned int)red_long) >> 8) & 0xff;
  101. green = (((unsigned int)green_long) >> 8) & 0xff;
  102. blue = (((unsigned int)blue_long) >> 8) & 0xff;
  103. if (!(_plotter->drawstate->i_bg_color_status
  104. && _plotter->drawstate->i_bg_color.red == red
  105. && _plotter->drawstate->i_bg_color.green == green
  106. && _plotter->drawstate->i_bg_color.blue == blue))
  107. /* need another color index: search table, expand if necessary */
  108. {
  109. unsigned char index;
  110. index = _pl_i_new_color_index (R___(_plotter) red, green, blue);
  111. /* set new 24-bit RGB and color index in the drawing state */
  112. _plotter->drawstate->i_bg_color.red = red;
  113. _plotter->drawstate->i_bg_color.green = green;
  114. _plotter->drawstate->i_bg_color.blue = blue;
  115. _plotter->drawstate->i_bg_color_index = index;
  116. /* flag this color index as genuine */
  117. _plotter->drawstate->i_bg_color_status = true;
  118. }
  119. }
  120. /* Internal function, called by each of the above. It searches for a
  121. 24-bit RGB in the color table. If not found, it's added to table,
  122. unless table can't be expanded, in which case index with closest RGB is
  123. returned. */
  124. unsigned char
  125. _pl_i_new_color_index (R___(Plotter *_plotter) int red, int green, int blue)
  126. {
  127. int i, j;
  128. int sqdist;
  129. bool found = false;
  130. for (i = 0; i < _plotter->i_num_color_indices; i++)
  131. if (_plotter->i_colormap[i].red == red
  132. && _plotter->i_colormap[i].green == green
  133. && _plotter->i_colormap[i].blue == blue)
  134. {
  135. found = true;
  136. break;
  137. }
  138. if (found)
  139. return (unsigned char)i;
  140. /* not found, try to allocate new index */
  141. i = _plotter->i_num_color_indices;
  142. if (i < 256)
  143. {
  144. _plotter->i_colormap[i].red = red;
  145. _plotter->i_colormap[i].green = green;
  146. _plotter->i_colormap[i].blue = blue;
  147. _plotter->i_num_color_indices = i + 1;
  148. /* New bit depth of colormap, e.g. sizes 129..256 get mapped to 8.
  149. In effect the colormap for any of these sizes will be of size 256. */
  150. _plotter->i_bit_depth = bit_depth (i + 1);
  151. return (unsigned char)i;
  152. }
  153. /* table full, do our best */
  154. sqdist = INT_MAX;
  155. i = 0;
  156. for (j = 0; j < 256; j++)
  157. {
  158. int new_sqdist;
  159. int a_red, a_green, a_blue;
  160. a_red = _plotter->i_colormap[j].red;
  161. a_green = _plotter->i_colormap[j].green;
  162. a_blue = _plotter->i_colormap[j].blue;
  163. new_sqdist = ((a_red - red) * (a_red - red)
  164. + (a_green - green) * (a_green - green)
  165. + (a_blue - blue) * (a_blue - blue));
  166. if (new_sqdist <= sqdist)
  167. {
  168. sqdist = new_sqdist;
  169. i = j; /* best to date */
  170. }
  171. }
  172. return (unsigned char)i;
  173. }
  174. /* compute number of bits needed to represent all color indices
  175. (when this is called, colors >= 1) */
  176. static int
  177. bit_depth (int colors)
  178. {
  179. int size;
  180. unsigned int ucolors;
  181. /* subtract 1, see how many bits needed to represent result */
  182. size = 0;
  183. for (ucolors = colors - 1; ucolors; ucolors = ucolors >> 1)
  184. size++;
  185. return size;
  186. }