| 1 |
/* |
| 2 |
=head1 NAME |
| 3 |
|
| 4 |
palimg.c - implements paletted images for Imager. |
| 5 |
|
| 6 |
=head1 SYNOPSIS |
| 7 |
|
| 8 |
=head1 DESCRIPTION |
| 9 |
|
| 10 |
Implements paletted images using the new image interface. |
| 11 |
|
| 12 |
=over |
| 13 |
|
| 14 |
=item IIM_base_8bit_pal |
| 15 |
|
| 16 |
Basic 8-bit/sample paletted image |
| 17 |
|
| 18 |
=cut |
| 19 |
*/ |
| 20 |
|
| 21 |
#define IMAGER_NO_CONTEXT |
| 22 |
|
| 23 |
#include "imager.h" |
| 24 |
#include "imageri.h" |
| 25 |
|
| 26 |
#define PALEXT(im) ((i_img_pal_ext*)((im)->ext_data)) |
| 27 |
static int i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val); |
| 28 |
static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val); |
| 29 |
static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals); |
| 30 |
static i_img_dim i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals); |
| 31 |
static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int const *chans, int chan_count); |
| 32 |
static i_img_dim i_gpal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals); |
| 33 |
static i_img_dim i_ppal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals); |
| 34 |
static int i_addcolors_p(i_img *im, const i_color *color, int count); |
| 35 |
static int i_getcolors_p(i_img *im, int i, i_color *color, int count); |
| 36 |
static int i_colorcount_p(i_img *im); |
| 37 |
static int i_maxcolors_p(i_img *im); |
| 38 |
static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry); |
| 39 |
static int i_setcolors_p(i_img *im, int index, const i_color *color, int count); |
| 40 |
|
| 41 |
static void i_destroy_p(i_img *im); |
| 42 |
static i_img_dim |
| 43 |
i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_sample_t *samps, const int *chans, int chan_count); |
| 44 |
static i_img_dim |
| 45 |
i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fsample_t *samps, const int *chans, int chan_count); |
| 46 |
|
| 47 |
static i_img IIM_base_8bit_pal = |
| 48 |
{ |
| 49 |
0, /* channels set */ |
| 50 |
0, 0, 0, /* xsize, ysize, bytes */ |
| 51 |
~0U, /* ch_mask */ |
| 52 |
i_8_bits, /* bits */ |
| 53 |
i_palette_type, /* type */ |
| 54 |
0, /* virtual */ |
| 55 |
NULL, /* idata */ |
| 56 |
{ 0, 0, NULL }, /* tags */ |
| 57 |
NULL, /* ext_data */ |
| 58 |
|
| 59 |
i_ppix_p, /* i_f_ppix */ |
| 60 |
i_ppixf_fp, /* i_f_ppixf */ |
| 61 |
i_plin_p, /* i_f_plin */ |
| 62 |
i_plinf_fp, /* i_f_plinf */ |
| 63 |
i_gpix_p, /* i_f_gpix */ |
| 64 |
i_gpixf_fp, /* i_f_gpixf */ |
| 65 |
i_glin_p, /* i_f_glin */ |
| 66 |
i_glinf_fp, /* i_f_glinf */ |
| 67 |
i_gsamp_p, /* i_f_gsamp */ |
| 68 |
i_gsampf_fp, /* i_f_gsampf */ |
| 69 |
|
| 70 |
i_gpal_p, /* i_f_gpal */ |
| 71 |
i_ppal_p, /* i_f_ppal */ |
| 72 |
i_addcolors_p, /* i_f_addcolors */ |
| 73 |
i_getcolors_p, /* i_f_getcolors */ |
| 74 |
i_colorcount_p, /* i_f_colorcount */ |
| 75 |
i_maxcolors_p, /* i_f_maxcolors */ |
| 76 |
i_findcolor_p, /* i_f_findcolor */ |
| 77 |
i_setcolors_p, /* i_f_setcolors */ |
| 78 |
|
| 79 |
i_destroy_p, /* i_f_destroy */ |
| 80 |
|
| 81 |
i_gsamp_bits_fb, |
| 82 |
NULL, /* i_f_psamp_bits */ |
| 83 |
|
| 84 |
i_psamp_p, |
| 85 |
i_psampf_p, |
| 86 |
|
| 87 |
NULL, |
| 88 |
NULL |
| 89 |
}; |
| 90 |
|
| 91 |
#define PALETTE_COLOR_COUNT 256 |
| 92 |
|
| 93 |
/* |
| 94 |
=item im_img_pal_new(ctx, C<x>, C<y>, C<channels>, C<maxpal>) |
| 95 |
X<im_img_pal_new API>X<i_img_pal_new API> |
| 96 |
=category Image creation/destruction |
| 97 |
=synopsis i_img *img = im_img_pal_new(aIMCTX, width, height, channels, max_palette_size) |
| 98 |
=synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size) |
| 99 |
|
| 100 |
Creates a new paletted image of the supplied dimensions. |
| 101 |
|
| 102 |
C<max_palette_size> is now ignored, all images have a 256 color |
| 103 |
palette allocated. |
| 104 |
|
| 105 |
Returns a new image or NULL on failure. |
| 106 |
|
| 107 |
Also callable as C<i_img_pal_new(width, height, channels, max_palette_size)>. |
| 108 |
|
| 109 |
=cut |
| 110 |
*/ |
| 111 |
i_img * |
| 112 |
im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) { |
| 113 |
i_img *im; |
| 114 |
i_img_pal_ext *palext; |
| 115 |
size_t bytes, line_bytes; |
| 116 |
|
| 117 |
(void)maxpal; /* no longer used */ |
| 118 |
|
| 119 |
i_clear_error(); |
| 120 |
if (x < 1 || y < 1) { |
| 121 |
i_push_error(0, "Image sizes must be positive"); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
if (channels < 1 || channels > MAXCHANNELS) { |
| 125 |
im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS); |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
bytes = sizeof(i_palidx) * x * y; |
| 129 |
if (bytes / y / sizeof(i_palidx) != (size_t)x) { |
| 130 |
i_push_error(0, "integer overflow calculating image allocation"); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
/* basic assumption: we can always allocate a buffer representing a |
| 135 |
line from the image, otherwise we're going to have trouble |
| 136 |
working with the image */ |
| 137 |
line_bytes = sizeof(i_color) * x; |
| 138 |
if (line_bytes / x != sizeof(i_color)) { |
| 139 |
i_push_error(0, "integer overflow calculating scanline allocation"); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
im = i_img_alloc(); |
| 144 |
memcpy(im, &IIM_base_8bit_pal, sizeof(i_img)); |
| 145 |
palext = mymalloc(sizeof(i_img_pal_ext)); |
| 146 |
palext->pal = mymalloc(sizeof(i_color) * PALETTE_COLOR_COUNT); |
| 147 |
palext->count = 0; |
| 148 |
palext->alloc = PALETTE_COLOR_COUNT; |
| 149 |
{ |
| 150 |
i_color *pal = palext->pal; |
| 151 |
int i; |
| 152 |
/* unused colors are read as transparent black */ |
| 153 |
for (i = 0; i < PALETTE_COLOR_COUNT; ++i) |
| 154 |
pal[i] = i_color_black0; |
| 155 |
} |
| 156 |
palext->last_found = -1; |
| 157 |
im->ext_data = palext; |
| 158 |
i_tags_new(&im->tags); |
| 159 |
im->bytes = bytes; |
| 160 |
im->idata = mymalloc(im->bytes); |
| 161 |
im->channels = channels; |
| 162 |
memset(im->idata, 0, im->bytes); |
| 163 |
im->xsize = x; |
| 164 |
im->ysize = y; |
| 165 |
|
| 166 |
i_img_init(im); |
| 167 |
|
| 168 |
return im; |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
=item i_img_rgb_convert(i_img *targ, i_img *src) |
| 173 |
|
| 174 |
Converts paletted data in src to RGB data in targ |
| 175 |
|
| 176 |
Internal function. |
| 177 |
|
| 178 |
src must be a paletted image and targ must be an RGB image with the |
| 179 |
same width, height and channels. |
| 180 |
|
| 181 |
=cut |
| 182 |
*/ |
| 183 |
static void i_img_rgb_convert(i_img *targ, i_img *src) { |
| 184 |
i_color *row = mymalloc(sizeof(i_color) * targ->xsize); |
| 185 |
i_img_dim y; |
| 186 |
for (y = 0; y < targ->ysize; ++y) { |
| 187 |
i_glin(src, 0, src->xsize, y, row); |
| 188 |
i_plin(targ, 0, src->xsize, y, row); |
| 189 |
} |
| 190 |
myfree(row); |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
=item i_img_to_rgb_inplace(im) |
| 195 |
|
| 196 |
Converts im from a paletted image to an RGB image. |
| 197 |
|
| 198 |
The conversion is done in place. |
| 199 |
|
| 200 |
The conversion cannot be done for virtual images. |
| 201 |
|
| 202 |
=cut |
| 203 |
*/ |
| 204 |
int |
| 205 |
i_img_to_rgb_inplace(i_img *im) { |
| 206 |
i_img temp; |
| 207 |
dIMCTXim(im); |
| 208 |
|
| 209 |
if (i_img_virtual(im)) |
| 210 |
return 0; |
| 211 |
|
| 212 |
if (im->type == i_direct_type) |
| 213 |
return 1; /* trivial success */ |
| 214 |
|
| 215 |
i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels); |
| 216 |
i_img_rgb_convert(&temp, im); |
| 217 |
|
| 218 |
/* nasty hack */ |
| 219 |
i_img_exorcise(im); |
| 220 |
*im = temp; |
| 221 |
|
| 222 |
return 1; |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
=item i_img_to_pal(i_img *im, i_quantize *quant) |
| 227 |
|
| 228 |
Converts an RGB image to a paletted image |
| 229 |
|
| 230 |
=cut |
| 231 |
*/ |
| 232 |
i_img *i_img_to_pal(i_img *src, i_quantize *quant) { |
| 233 |
i_palidx *result; |
| 234 |
i_img *im; |
| 235 |
dIMCTXim(src); |
| 236 |
|
| 237 |
i_clear_error(); |
| 238 |
|
| 239 |
i_quant_makemap(quant, &src, 1); |
| 240 |
result = i_quant_translate(quant, src); |
| 241 |
|
| 242 |
if (result) { |
| 243 |
|
| 244 |
im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size); |
| 245 |
|
| 246 |
/* copy things over */ |
| 247 |
memcpy(im->idata, result, im->bytes); |
| 248 |
PALEXT(im)->count = quant->mc_count; |
| 249 |
memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count); |
| 250 |
|
| 251 |
myfree(result); |
| 252 |
|
| 253 |
return im; |
| 254 |
} |
| 255 |
else { |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
=item i_img_to_rgb(i_img *src) |
| 262 |
|
| 263 |
=cut |
| 264 |
*/ |
| 265 |
i_img * |
| 266 |
i_img_to_rgb(i_img *src) { |
| 267 |
dIMCTXim(src); |
| 268 |
i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels); |
| 269 |
i_img_rgb_convert(im, src); |
| 270 |
|
| 271 |
return im; |
| 272 |
} |
| 273 |
|
| 274 |
/* |
| 275 |
=item i_destroy_p(i_img *im) |
| 276 |
|
| 277 |
Destroys data related to a paletted image. |
| 278 |
|
| 279 |
=cut |
| 280 |
*/ |
| 281 |
static void i_destroy_p(i_img *im) { |
| 282 |
if (im) { |
| 283 |
i_img_pal_ext *palext = im->ext_data; |
| 284 |
if (palext) { |
| 285 |
if (palext->pal) |
| 286 |
myfree(palext->pal); |
| 287 |
myfree(palext); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
=item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) |
| 294 |
|
| 295 |
Write to a pixel in the image. |
| 296 |
|
| 297 |
Warning: converts the image to a RGB image if the color isn't already |
| 298 |
present in the image. |
| 299 |
|
| 300 |
=cut |
| 301 |
*/ |
| 302 |
static int |
| 303 |
i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) { |
| 304 |
const i_color *work_val = val; |
| 305 |
i_color workc; |
| 306 |
i_palidx which; |
| 307 |
const unsigned all_mask = ( 1 << im->channels ) - 1; |
| 308 |
|
| 309 |
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) |
| 310 |
return -1; |
| 311 |
|
| 312 |
if ((im->ch_mask & all_mask) != all_mask) { |
| 313 |
unsigned mask = 1; |
| 314 |
int ch; |
| 315 |
i_gpix(im, x, y, &workc); |
| 316 |
for (ch = 0; ch < im->channels; ++ch) { |
| 317 |
if (im->ch_mask & mask) |
| 318 |
workc.channel[ch] = val->channel[ch]; |
| 319 |
mask <<= 1; |
| 320 |
} |
| 321 |
work_val = &workc; |
| 322 |
} |
| 323 |
|
| 324 |
if (i_findcolor(im, work_val, &which)) { |
| 325 |
((i_palidx *)im->idata)[x + y * im->xsize] = which; |
| 326 |
return 0; |
| 327 |
} |
| 328 |
else { |
| 329 |
dIMCTXim(im); |
| 330 |
im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n", |
| 331 |
val->channel[0], val->channel[1], val->channel[2])); |
| 332 |
if (i_img_to_rgb_inplace(im)) { |
| 333 |
return i_ppix(im, x, y, val); |
| 334 |
} |
| 335 |
else |
| 336 |
return -1; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
=item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) |
| 342 |
|
| 343 |
Retrieve a pixel, converting from a palette index to a color. |
| 344 |
|
| 345 |
=cut |
| 346 |
*/ |
| 347 |
static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) { |
| 348 |
i_palidx which; |
| 349 |
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { |
| 350 |
return -1; |
| 351 |
} |
| 352 |
which = ((i_palidx *)im->idata)[x + y * im->xsize]; |
| 353 |
/* unallocated entries are black */ |
| 354 |
*val = PALEXT(im)->pal[which]; |
| 355 |
if (which >= PALEXT(im)->count) |
| 356 |
return -1; |
| 357 |
|
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
/* |
| 362 |
=item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) |
| 363 |
|
| 364 |
Retrieve a row of pixels. |
| 365 |
|
| 366 |
=cut |
| 367 |
*/ |
| 368 |
static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) { |
| 369 |
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 370 |
i_color *pal = PALEXT(im)->pal; |
| 371 |
i_palidx *data; |
| 372 |
i_img_dim count, i; |
| 373 |
if (r > im->xsize) |
| 374 |
r = im->xsize; |
| 375 |
data = ((i_palidx *)im->idata) + l + y * im->xsize; |
| 376 |
count = r - l; |
| 377 |
for (i = 0; i < count; ++i) { |
| 378 |
i_palidx which = *data++; |
| 379 |
vals[i] = pal[which]; |
| 380 |
} |
| 381 |
return count; |
| 382 |
} |
| 383 |
else { |
| 384 |
return 0; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/* |
| 389 |
=item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) |
| 390 |
|
| 391 |
Write a line of color data to the image. |
| 392 |
|
| 393 |
If any color value is not in the image when the image is converted to |
| 394 |
RGB. |
| 395 |
|
| 396 |
=cut |
| 397 |
*/ |
| 398 |
static i_img_dim |
| 399 |
i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) { |
| 400 |
i_img_dim count, i; |
| 401 |
i_palidx *data; |
| 402 |
i_palidx which; |
| 403 |
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 404 |
if (r > im->xsize) |
| 405 |
r = im->xsize; |
| 406 |
data = ((i_palidx *)im->idata) + l + y * im->xsize; |
| 407 |
count = r - l; |
| 408 |
for (i = 0; i < count; ++i) { |
| 409 |
if (i_findcolor(im, vals+i, &which)) { |
| 410 |
((i_palidx *)data)[i] = which; |
| 411 |
} |
| 412 |
else { |
| 413 |
if (i_img_to_rgb_inplace(im)) { |
| 414 |
return i+i_plin(im, l+i, r, y, vals+i); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
return count; |
| 419 |
} |
| 420 |
else { |
| 421 |
return 0; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/* |
| 426 |
=item i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int chans, int chan_count) |
| 427 |
|
| 428 |
=cut |
| 429 |
*/ |
| 430 |
static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, |
| 431 |
int const *chans, int chan_count) { |
| 432 |
int ch; |
| 433 |
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 434 |
i_color *pal = PALEXT(im)->pal; |
| 435 |
i_palidx *data; |
| 436 |
i_img_dim count, i, w; |
| 437 |
if (r > im->xsize) |
| 438 |
r = im->xsize; |
| 439 |
data = ((i_palidx *)im->idata) + l + y * im->xsize; |
| 440 |
count = 0; |
| 441 |
w = r - l; |
| 442 |
if (chans) { |
| 443 |
for (ch = 0; ch < chan_count; ++ch) { |
| 444 |
if (chans[ch] < 0 || chans[ch] >= im->channels) { |
| 445 |
dIMCTXim(im); |
| 446 |
im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
for (i = 0; i < w; ++i) { |
| 451 |
i_palidx which = *data++; |
| 452 |
for (ch = 0; ch < chan_count; ++ch) { |
| 453 |
*samps++ = pal[which].channel[chans[ch]]; |
| 454 |
++count; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
else { |
| 459 |
if (chan_count <= 0 || chan_count > im->channels) { |
| 460 |
dIMCTXim(im); |
| 461 |
im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", |
| 462 |
chan_count); |
| 463 |
return 0; |
| 464 |
} |
| 465 |
for (i = 0; i < w; ++i) { |
| 466 |
i_palidx which = *data++; |
| 467 |
for (ch = 0; ch < chan_count; ++ch) { |
| 468 |
*samps++ = pal[which].channel[ch]; |
| 469 |
++count; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
return count; |
| 474 |
} |
| 475 |
else { |
| 476 |
return 0; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
=item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) |
| 482 |
|
| 483 |
=cut |
| 484 |
*/ |
| 485 |
|
| 486 |
static i_img_dim i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) { |
| 487 |
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 488 |
i_palidx *data; |
| 489 |
i_img_dim i, w; |
| 490 |
if (r > im->xsize) |
| 491 |
r = im->xsize; |
| 492 |
data = ((i_palidx *)im->idata) + l + y * im->xsize; |
| 493 |
w = r - l; |
| 494 |
for (i = 0; i < w; ++i) { |
| 495 |
*vals++ = *data++; |
| 496 |
} |
| 497 |
return i; |
| 498 |
} |
| 499 |
else { |
| 500 |
return 0; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
/* |
| 505 |
=item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) |
| 506 |
|
| 507 |
=cut |
| 508 |
*/ |
| 509 |
|
| 510 |
static i_img_dim i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) { |
| 511 |
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 512 |
i_palidx *data; |
| 513 |
i_img_dim i, w; |
| 514 |
if (r > im->xsize) |
| 515 |
r = im->xsize; |
| 516 |
data = ((i_palidx *)im->idata) + l + y * im->xsize; |
| 517 |
w = r - l; |
| 518 |
for (i = 0; i < w; ++i) { |
| 519 |
*data++ = *vals++; |
| 520 |
} |
| 521 |
return i; |
| 522 |
} |
| 523 |
else { |
| 524 |
return 0; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
/* |
| 529 |
=item i_addcolors_p(i_img *im, const i_color *color, int count) |
| 530 |
|
| 531 |
=cut |
| 532 |
*/ |
| 533 |
static int i_addcolors_p(i_img *im, const i_color *color, int count) { |
| 534 |
if (PALEXT(im)->count + count <= PALEXT(im)->alloc) { |
| 535 |
int result = PALEXT(im)->count; |
| 536 |
int index = result; |
| 537 |
|
| 538 |
PALEXT(im)->count += count; |
| 539 |
while (count) { |
| 540 |
PALEXT(im)->pal[index++] = *color++; |
| 541 |
--count; |
| 542 |
} |
| 543 |
|
| 544 |
return result; |
| 545 |
} |
| 546 |
else |
| 547 |
return -1; |
| 548 |
} |
| 549 |
|
| 550 |
/* |
| 551 |
=item i_getcolors_p(i_img *im, int i, i_color *color, int count) |
| 552 |
|
| 553 |
=cut |
| 554 |
*/ |
| 555 |
static int i_getcolors_p(i_img *im, int i, i_color *color, int count) { |
| 556 |
if (i >= 0 && i+count <= PALEXT(im)->count) { |
| 557 |
while (count) { |
| 558 |
*color++ = PALEXT(im)->pal[i++]; |
| 559 |
--count; |
| 560 |
} |
| 561 |
return 1; |
| 562 |
} |
| 563 |
else |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
static int color_eq(i_img *im, const i_color *c1, const i_color *c2) { |
| 568 |
int ch; |
| 569 |
for (ch = 0; ch < im->channels; ++ch) { |
| 570 |
if (c1->channel[ch] != c2->channel[ch]) |
| 571 |
return 0; |
| 572 |
} |
| 573 |
return 1; |
| 574 |
} |
| 575 |
|
| 576 |
/* |
| 577 |
=item i_colorcount_p(i_img *im) |
| 578 |
|
| 579 |
=cut |
| 580 |
*/ |
| 581 |
static int i_colorcount_p(i_img *im) { |
| 582 |
return PALEXT(im)->count; |
| 583 |
} |
| 584 |
|
| 585 |
/* |
| 586 |
=item i_maxcolors_p(i_img *im) |
| 587 |
|
| 588 |
=cut |
| 589 |
*/ |
| 590 |
static int i_maxcolors_p(i_img *im) { |
| 591 |
return PALEXT(im)->alloc; |
| 592 |
} |
| 593 |
|
| 594 |
/* |
| 595 |
=item i_setcolors_p(i_img *im, int index, const i_color *colors, int count) |
| 596 |
|
| 597 |
=cut |
| 598 |
*/ |
| 599 |
static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) { |
| 600 |
if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) { |
| 601 |
while (count) { |
| 602 |
PALEXT(im)->pal[index++] = *colors++; |
| 603 |
--count; |
| 604 |
} |
| 605 |
return 1; |
| 606 |
} |
| 607 |
|
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
/* |
| 612 |
=item i_findcolor_p(i_img *im) |
| 613 |
|
| 614 |
=cut |
| 615 |
*/ |
| 616 |
static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) { |
| 617 |
if (PALEXT(im)->count) { |
| 618 |
int i; |
| 619 |
/* often the same color comes up several times in a row */ |
| 620 |
if (PALEXT(im)->last_found >= 0) { |
| 621 |
if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) { |
| 622 |
*entry = PALEXT(im)->last_found; |
| 623 |
return 1; |
| 624 |
} |
| 625 |
} |
| 626 |
for (i = 0; i < PALEXT(im)->count; ++i) { |
| 627 |
if (color_eq(im, color, PALEXT(im)->pal + i)) { |
| 628 |
PALEXT(im)->last_found = *entry = i; |
| 629 |
return 1; |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
=item i_psamp_p(im, l, r, y, samps, chans, chan_count) |
| 638 |
|
| 639 |
Implement psamp() for paletted images. |
| 640 |
|
| 641 |
Since writing samples doesn't really work as a concept for paletted |
| 642 |
images, this is slow. |
| 643 |
|
| 644 |
Also, writing samples may convert the image to a direct image in the |
| 645 |
process, so use i_ppix/i_gpix instead of directly calling the paletted |
| 646 |
handlers. |
| 647 |
|
| 648 |
=cut |
| 649 |
*/ |
| 650 |
|
| 651 |
static i_img_dim |
| 652 |
i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, |
| 653 |
const i_sample_t *samps, const int *chans, int chan_count) { |
| 654 |
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 655 |
i_img_dim count = 0; |
| 656 |
int ch; |
| 657 |
|
| 658 |
if (r > im->xsize) |
| 659 |
r = im->xsize; |
| 660 |
|
| 661 |
if (chans) { |
| 662 |
/* make sure we have good channel numbers */ |
| 663 |
for (ch = 0; ch < chan_count; ++ch) { |
| 664 |
if (chans[ch] < 0 || chans[ch] >= im->channels) { |
| 665 |
dIMCTXim(im); |
| 666 |
im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]); |
| 667 |
return -1; |
| 668 |
} |
| 669 |
} |
| 670 |
while (l < r) { |
| 671 |
i_color c; |
| 672 |
|
| 673 |
i_gpix(im, l, y, &c); |
| 674 |
for (ch = 0; ch < chan_count; ++ch) |
| 675 |
c.channel[chans[ch]] = *samps++; |
| 676 |
i_ppix(im, l, y, &c); |
| 677 |
count += chan_count; |
| 678 |
++l; |
| 679 |
} |
| 680 |
} |
| 681 |
else { |
| 682 |
if (chan_count <= 0 || chan_count > im->channels) { |
| 683 |
dIMCTXim(im); |
| 684 |
im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", |
| 685 |
chan_count); |
| 686 |
return -1; |
| 687 |
} |
| 688 |
|
| 689 |
while (l < r) { |
| 690 |
i_color c; |
| 691 |
|
| 692 |
i_gpix(im, l, y, &c); |
| 693 |
for (ch = 0; ch < chan_count; ++ch) |
| 694 |
c.channel[ch] = *samps++; |
| 695 |
i_ppix(im, l, y, &c); |
| 696 |
count += chan_count; |
| 697 |
++l; |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
return count; |
| 702 |
} |
| 703 |
else { |
| 704 |
dIMCTXim(im); |
| 705 |
i_push_error(0, "Image position outside of image"); |
| 706 |
return -1; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
/* |
| 711 |
=item i_psampf_p(im, l, r, y, samps, chans, chan_count) |
| 712 |
|
| 713 |
Implement psampf() for paletted images. |
| 714 |
|
| 715 |
Since writing samples doesn't really work as a concept for paletted |
| 716 |
images, this is slow. |
| 717 |
|
| 718 |
Also, writing samples may convert the image to a direct image in the |
| 719 |
process, so use i_ppixf/i_gpixf instead of directly calling the paletted |
| 720 |
handlers. |
| 721 |
|
| 722 |
=cut |
| 723 |
*/ |
| 724 |
|
| 725 |
static i_img_dim |
| 726 |
i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, |
| 727 |
const i_fsample_t *samps, const int *chans, int chan_count) { |
| 728 |
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { |
| 729 |
i_img_dim count = 0; |
| 730 |
int ch; |
| 731 |
|
| 732 |
if (r > im->xsize) |
| 733 |
r = im->xsize; |
| 734 |
|
| 735 |
if (chans) { |
| 736 |
/* make sure we have good channel numbers */ |
| 737 |
for (ch = 0; ch < chan_count; ++ch) { |
| 738 |
if (chans[ch] < 0 || chans[ch] >= im->channels) { |
| 739 |
dIMCTXim(im); |
| 740 |
im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]); |
| 741 |
return -1; |
| 742 |
} |
| 743 |
} |
| 744 |
while (l < r) { |
| 745 |
i_fcolor c; |
| 746 |
|
| 747 |
i_gpixf(im, l, y, &c); |
| 748 |
for (ch = 0; ch < chan_count; ++ch) |
| 749 |
c.channel[chans[ch]] = *samps++; |
| 750 |
i_ppixf(im, l, y, &c); |
| 751 |
count += chan_count; |
| 752 |
++l; |
| 753 |
} |
| 754 |
} |
| 755 |
else { |
| 756 |
if (chan_count <= 0 || chan_count > im->channels) { |
| 757 |
dIMCTXim(im); |
| 758 |
im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", |
| 759 |
chan_count); |
| 760 |
return -1; |
| 761 |
} |
| 762 |
|
| 763 |
while (l < r) { |
| 764 |
i_fcolor c; |
| 765 |
|
| 766 |
i_gpixf(im, l, y, &c); |
| 767 |
for (ch = 0; ch < chan_count; ++ch) |
| 768 |
c.channel[ch] = *samps++; |
| 769 |
i_ppixf(im, l, y, &c); |
| 770 |
count += chan_count; |
| 771 |
++l; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
return count; |
| 776 |
} |
| 777 |
else { |
| 778 |
dIMCTXim(im); |
| 779 |
i_push_error(0, "Image position outside of image"); |
| 780 |
return -1; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
/* |
| 785 |
=back |
| 786 |
|
| 787 |
=head1 AUTHOR |
| 788 |
|
| 789 |
Tony Cook <[email protected]> |
| 790 |
|
| 791 |
=head1 SEE ALSO |
| 792 |
|
| 793 |
Imager(3) |
| 794 |
|
| 795 |
=cut |
| 796 |
*/ |