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