X-Git-Url: http://git.imager.perl.org/imager.git/blobdiff_plain/97ac0a9684911cf11b8ef6fdf925295cd8fc42e7..HEAD:/palimg.c diff --git a/palimg.c b/palimg.c index 26132595..0c37b582 100644 --- a/palimg.c +++ b/palimg.c @@ -18,17 +18,19 @@ Basic 8-bit/sample paletted image =cut */ +#define IMAGER_NO_CONTEXT + #include "imager.h" #include "imageri.h" #define PALEXT(im) ((i_img_pal_ext*)((im)->ext_data)) -static int i_ppix_p(i_img *im, int x, int y, const i_color *val); -static int i_gpix_p(i_img *im, int x, int y, i_color *val); -static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals); -static int i_plin_p(i_img *im, int l, int r, int y, const i_color *vals); -static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int const *chans, int chan_count); -static int i_gpal_p(i_img *pm, int l, int r, int y, i_palidx *vals); -static int i_ppal_p(i_img *pm, int l, int r, int y, const i_palidx *vals); +static int i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val); +static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val); +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); +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); +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); +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); +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); static int i_addcolors_p(i_img *im, const i_color *color, int count); static int i_getcolors_p(i_img *im, int i, i_color *color, int count); static int i_colorcount_p(i_img *im); @@ -37,6 +39,10 @@ static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry); static int i_setcolors_p(i_img *im, int index, const i_color *color, int count); static void i_destroy_p(i_img *im); +static i_img_dim +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); +static i_img_dim +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); static i_img IIM_base_8bit_pal = { @@ -71,20 +77,36 @@ static i_img IIM_base_8bit_pal = i_setcolors_p, /* i_f_setcolors */ i_destroy_p, /* i_f_destroy */ + + i_gsamp_bits_fb, + NULL, /* i_f_psamp_bits */ + + i_psamp_p, + i_psampf_p }; /* -=item i_img_pal_new_low(im, x, y, channels, maxpal) +=item im_img_pal_new(ctx, C, C, C, C) +XX +=category Image creation/destruction +=synopsis i_img *img = im_img_pal_new(aIMCTX, width, height, channels, max_palette_size) +=synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size) -Creates a new paletted image. +Creates a new paletted image of the supplied dimensions. + +C is the maximum palette size and should normally be 256. + +Returns a new image or NULL on failure. -Currently 0 < maxpal <= 256 +Also callable as C. =cut */ -i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) { +i_img * +im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) { + i_img *im; i_img_pal_ext *palext; - int bytes, line_bytes; + size_t bytes, line_bytes; i_clear_error(); if (maxpal < 1 || maxpal > 256) { @@ -96,7 +118,7 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) { return NULL; } if (channels < 1 || channels > MAXCHANNELS) { - i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS); + im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS); return NULL; } bytes = sizeof(i_palidx) * x * y; @@ -114,6 +136,7 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) { return NULL; } + im = i_img_alloc(); memcpy(im, &IIM_base_8bit_pal, sizeof(i_img)); palext = mymalloc(sizeof(i_img_pal_ext)); palext->pal = mymalloc(sizeof(i_color) * maxpal); @@ -128,31 +151,9 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) { memset(im->idata, 0, im->bytes); im->xsize = x; im->ysize = y; - - return im; -} - -/* -=item i_img_pal_new(x, y, channels, maxpal) - -=category Image creation - -Creates a new paletted image of the supplied dimensions. - -Returns a new image or NULL on failure. - -=cut -*/ - -i_img *i_img_pal_new(int x, int y, int channels, int maxpal) { - i_img *im; - mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal)); - im = mymalloc(sizeof(i_img)); - if (!i_img_pal_new_low(im, x, y, channels, maxpal)) { - myfree(im); - im = NULL; - } + i_img_init(im); + return im; } @@ -170,7 +171,7 @@ same width, height and channels. */ static void i_img_rgb_convert(i_img *targ, i_img *src) { i_color *row = mymalloc(sizeof(i_color) * targ->xsize); - int y; + i_img_dim y; for (y = 0; y < targ->ysize; ++y) { i_glin(src, 0, src->xsize, y, row); i_plin(targ, 0, src->xsize, y, row); @@ -189,8 +190,10 @@ The conversion cannot be done for virtual images. =cut */ -int i_img_to_rgb_inplace(i_img *im) { +int +i_img_to_rgb_inplace(i_img *im) { i_img temp; + dIMCTXim(im); if (im->virtual) return 0; @@ -202,10 +205,12 @@ int i_img_to_rgb_inplace(i_img *im) { i_img_rgb_convert(&temp, im); /* nasty hack */ - (im->i_f_destroy)(im); - myfree(im->idata); + i_img_exorcise(im); *im = temp; + /* i_img_empty_ch() calls i_img_init() which takes a ref */ + im_context_refdec(aIMCTX, "img_destroy"); + return 1; } @@ -219,6 +224,7 @@ Converts an RGB image to a paletted image i_img *i_img_to_pal(i_img *src, i_quantize *quant) { i_palidx *result; i_img *im; + dIMCTXim(src); i_clear_error(); @@ -248,7 +254,9 @@ i_img *i_img_to_pal(i_img *src, i_quantize *quant) { =cut */ -i_img *i_img_to_rgb(i_img *src) { +i_img * +i_img_to_rgb(i_img *src) { + dIMCTXim(src); i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels); i_img_rgb_convert(im, src); @@ -274,7 +282,7 @@ static void i_destroy_p(i_img *im) { } /* -=item i_ppix_p(i_img *im, int x, int y, const i_color *val) +=item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) Write to a pixel in the image. @@ -284,15 +292,35 @@ present in the image. =cut */ static int -i_ppix_p(i_img *im, int x, int y, const i_color *val) { +i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) { + const i_color *work_val = val; + i_color workc; i_palidx which; + const unsigned all_mask = ( 1 << im->channels ) - 1; + if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) return -1; - if (i_findcolor(im, val, &which)) { + + if ((im->ch_mask & all_mask) != all_mask) { + unsigned mask = 1; + int ch; + i_gpix(im, x, y, &workc); + for (ch = 0; ch < im->channels; ++ch) { + if (im->ch_mask & mask) + workc.channel[ch] = val->channel[ch]; + mask <<= 1; + } + work_val = &workc; + } + + if (i_findcolor(im, work_val, &which)) { ((i_palidx *)im->idata)[x + y * im->xsize] = which; return 0; } else { + dIMCTXim(im); + im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n", + val->channel[0], val->channel[1], val->channel[2])); if (i_img_to_rgb_inplace(im)) { return i_ppix(im, x, y, val); } @@ -302,13 +330,13 @@ i_ppix_p(i_img *im, int x, int y, const i_color *val) { } /* -=item i_gpix_p(i_img *im, int x, int y, i_color *val) +=item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) Retrieve a pixel, converting from a palette index to a color. =cut */ -static int i_gpix_p(i_img *im, int x, int y, i_color *val) { +static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) { i_palidx which; if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { return -1; @@ -322,18 +350,18 @@ static int i_gpix_p(i_img *im, int x, int y, i_color *val) { } /* -=item i_glinp(i_img *im, int l, int r, int y, i_color *vals) +=item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) Retrieve a row of pixels. =cut */ -static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) { +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) { if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { int palsize = PALEXT(im)->count; i_color *pal = PALEXT(im)->pal; i_palidx *data; - int count, i; + i_img_dim count, i; if (r > im->xsize) r = im->xsize; data = ((i_palidx *)im->idata) + l + y * im->xsize; @@ -351,7 +379,7 @@ static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) { } /* -=item i_plin_p(i_img *im, int l, int r, int y, const i_color *vals) +=item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) Write a line of color data to the image. @@ -360,9 +388,9 @@ RGB. =cut */ -static int -i_plin_p(i_img *im, int l, int r, int y, const i_color *vals) { - int count, i; +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) { + i_img_dim count, i; i_palidx *data; i_palidx which; if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { @@ -388,18 +416,18 @@ i_plin_p(i_img *im, int l, int r, int y, const i_color *vals) { } /* -=item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count) +=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) =cut */ -static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, +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) { int ch; if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { int palsize = PALEXT(im)->count; i_color *pal = PALEXT(im)->pal; i_palidx *data; - int count, i, w; + i_img_dim count, i, w; if (r > im->xsize) r = im->xsize; data = ((i_palidx *)im->idata) + l + y * im->xsize; @@ -408,7 +436,8 @@ static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, if (chans) { for (ch = 0; ch < chan_count; ++ch) { if (chans[ch] < 0 || chans[ch] >= im->channels) { - i_push_errorf(0, "No channel %d in this image", chans[ch]); + dIMCTXim(im); + im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]); } } @@ -423,6 +452,12 @@ static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, } } else { + if (chan_count <= 0 || chan_count > im->channels) { + dIMCTXim(im); + im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", + chan_count); + return 0; + } for (i = 0; i < w; ++i) { i_palidx which = *data++; if (which < palsize) { @@ -441,15 +476,15 @@ static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, } /* -=item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) +=item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) =cut */ -static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) { +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) { if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { i_palidx *data; - int i, w; + i_img_dim i, w; if (r > im->xsize) r = im->xsize; data = ((i_palidx *)im->idata) + l + y * im->xsize; @@ -465,15 +500,15 @@ static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) { } /* -=item i_ppal_p(i_img *im, int l, int r, int y, const i_palidx *vals) +=item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) =cut */ -static int i_ppal_p(i_img *im, int l, int r, int y, const i_palidx *vals) { +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) { if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { i_palidx *data; - int i, w; + i_img_dim i, w; if (r > im->xsize) r = im->xsize; data = ((i_palidx *)im->idata) + l + y * im->xsize; @@ -596,6 +631,154 @@ static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) { return 0; } +/* +=item i_psamp_p(im, l, r, y, samps, chans, chan_count) + +Implement psamp() for paletted images. + +Since writing samples doesn't really work as a concept for paletted +images, this is slow. + +Also, writing samples may convert the image to a direct image in the +process, so use i_ppix/i_gpix instead of directly calling the paletted +handlers. + +=cut +*/ + +static i_img_dim +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) { + if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { + i_img_dim count = 0; + int ch; + + if (r > im->xsize) + r = im->xsize; + + if (chans) { + /* make sure we have good channel numbers */ + for (ch = 0; ch < chan_count; ++ch) { + if (chans[ch] < 0 || chans[ch] >= im->channels) { + dIMCTXim(im); + im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]); + return -1; + } + } + while (l < r) { + i_color c; + + i_gpix(im, l, y, &c); + for (ch = 0; ch < chan_count; ++ch) + c.channel[chans[ch]] = *samps++; + i_ppix(im, l, y, &c); + count += chan_count; + ++l; + } + } + else { + if (chan_count <= 0 || chan_count > im->channels) { + dIMCTXim(im); + im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", + chan_count); + return -1; + } + + while (l < r) { + i_color c; + + i_gpix(im, l, y, &c); + for (ch = 0; ch < chan_count; ++ch) + c.channel[ch] = *samps++; + i_ppix(im, l, y, &c); + count += chan_count; + ++l; + } + } + + return count; + } + else { + dIMCTXim(im); + i_push_error(0, "Image position outside of image"); + return -1; + } +} + +/* +=item i_psampf_p(im, l, r, y, samps, chans, chan_count) + +Implement psampf() for paletted images. + +Since writing samples doesn't really work as a concept for paletted +images, this is slow. + +Also, writing samples may convert the image to a direct image in the +process, so use i_ppixf/i_gpixf instead of directly calling the paletted +handlers. + +=cut +*/ + +static i_img_dim +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) { + if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { + i_img_dim count = 0; + int ch; + + if (r > im->xsize) + r = im->xsize; + + if (chans) { + /* make sure we have good channel numbers */ + for (ch = 0; ch < chan_count; ++ch) { + if (chans[ch] < 0 || chans[ch] >= im->channels) { + dIMCTXim(im); + im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]); + return -1; + } + } + while (l < r) { + i_fcolor c; + + i_gpixf(im, l, y, &c); + for (ch = 0; ch < chan_count; ++ch) + c.channel[chans[ch]] = *samps++; + i_ppixf(im, l, y, &c); + count += chan_count; + ++l; + } + } + else { + if (chan_count <= 0 || chan_count > im->channels) { + dIMCTXim(im); + im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", + chan_count); + return -1; + } + + while (l < r) { + i_fcolor c; + + i_gpixf(im, l, y, &c); + for (ch = 0; ch < chan_count; ++ch) + c.channel[ch] = *samps++; + i_ppixf(im, l, y, &c); + count += chan_count; + ++l; + } + } + + return count; + } + else { + dIMCTXim(im); + i_push_error(0, "Image position outside of image"); + return -1; + } +} + /* =back