X-Git-Url: http://git.imager.perl.org/imager.git/blobdiff_plain/8ebac85f46199cb0e1ce01ae60a8db6894bf9fbb..e1c0692925:/image.c
diff --git a/image.c b/image.c
index 56057e87..bd634ee1 100644
--- a/image.c
+++ b/image.c
@@ -44,8 +44,11 @@ im_context_t (*im_get_context)(void) = NULL;
void i_linker_bug_fake(void) { ceil(1); }
/*
-=item i_img_alloc()
+=item im_img_alloc(aIMCTX)
+XX
=category Image Implementation
+=synopsis i_img *im = im_img_alloc(aIMCTX);
+=synopsis i_img *im = i_img_alloc();
Allocates a new i_img structure.
@@ -79,13 +82,15 @@ im_img_alloc(pIMCTX) {
}
/*
-=item i_img_init(C
)
+=item im_img_init(aIMCTX, image)
+XX
=category Image Implementation
+=synopsis im_img_init(aIMCTX, im);
+=synopsis i_img_init(im);
Imager internal initialization of images.
-Currently this does very little, in the future it may be used to
-support threads, or color profiles.
+See L for more information.
=cut
*/
@@ -313,21 +318,15 @@ void
i_img_info(i_img *im, i_img_dim *info) {
dIMCTXim(im);
im_log((aIMCTX,1,"i_img_info(im %p)\n",im));
- if (im != NULL) {
- im_log((aIMCTX,1,"i_img_info: xsize=%" i_DF " ysize=%" i_DF " channels=%d "
- "mask=%ud\n",
- i_DFc(im->xsize), i_DFc(im->ysize), im->channels,im->ch_mask));
- im_log((aIMCTX,1,"i_img_info: idata=%p\n",im->idata));
- info[0] = im->xsize;
- info[1] = im->ysize;
- info[2] = im->channels;
- info[3] = im->ch_mask;
- } else {
- info[0] = 0;
- info[1] = 0;
- info[2] = 0;
- info[3] = 0;
- }
+
+ im_log((aIMCTX,1,"i_img_info: xsize=%" i_DF " ysize=%" i_DF " channels=%d "
+ "mask=%ud\n",
+ i_DFc(im->xsize), i_DFc(im->ysize), im->channels,im->ch_mask));
+ im_log((aIMCTX,1,"i_img_info: idata=%p\n",im->idata));
+ info[0] = im->xsize;
+ info[1] = im->ysize;
+ info[2] = im->channels;
+ info[3] = im->ch_mask;
}
/*
@@ -399,6 +398,85 @@ i_img_get_height(i_img *im) {
return im->ysize;
}
+/*
+=item i_img_color_model(im)
+=category Image Information
+=synopsis i_color_model_t cm = i_img_color_model(im);
+
+Returns the color model for the image.
+
+A future version of Imager will allow for images with extra channels
+beyond gray/rgb and alpha.
+
+=cut
+*/
+i_color_model_t
+i_img_color_model(i_img *im) {
+ return (i_color_model_t)im->channels;
+}
+
+/*
+=item i_img_alpha_channel(im, &channel)
+=category Image Information
+=synopsis int alpha_channel;
+=synopsis int has_alpha = i_img_alpha_channel(im, &alpha_channel);
+
+Work out the alpha channel for an image.
+
+If the image has an alpha channel, sets C<*channel> to the alpha
+channel index and returns non-zero.
+
+If the image has no alpha channel, returns zero and C<*channel> is not
+modified.
+
+C may be C.
+
+=cut
+*/
+
+int
+i_img_alpha_channel(i_img *im, int *channel) {
+ i_color_model_t model = i_img_color_model(im);
+ switch (model) {
+ case icm_gray_alpha:
+ case icm_rgb_alpha:
+ if (channel) *channel = (int)model - 1;
+ return 1;
+
+ default:
+ return 0;
+ }
+}
+
+/*
+=item i_img_color_channels(im)
+=category Image Information
+=synopsis int color_channels = i_img_color_channels(im);
+
+Returns the number of color channels in the image. For now this is
+always 1 (for grayscale) or 3 (for RGB) but may be 0 in some special
+cases in a future release of Imager.
+
+=cut
+*/
+
+int
+i_img_color_channels(i_img *im) {
+ i_color_model_t model = i_img_color_model(im);
+ switch (model) {
+ case icm_gray_alpha:
+ case icm_rgb_alpha:
+ return (int)model - 1;
+
+ case icm_gray:
+ case icm_rgb:
+ return (int)model;
+
+ default:
+ return 0;
+ }
+}
+
/*
=item i_copyto_trans(C, C, C, C, C, C, C, C, C)
@@ -1044,7 +1122,7 @@ i_haar(i_img *im) {
i_img_dim mx,my;
i_img_dim fx,fy;
i_img_dim x,y;
- int ch,c;
+ int ch;
i_img *new_img,*new_img2;
i_color val1,val2,dval1,dval2;
dIMCTXim(im);
@@ -1060,7 +1138,6 @@ i_haar(i_img *im) {
new_img=i_img_empty_ch(NULL,fx*2,fy*2,im->channels);
new_img2=i_img_empty_ch(NULL,fx*2,fy*2,im->channels);
- c=0;
for(y=0;y maxc) {
+ if (colorcnt > maxc) {
+ myfree(samp);
octt_delete(ct);
return -1;
}
@@ -1218,7 +1296,8 @@ i_get_anonymous_color_histo(i_img *im, unsigned int **col_usage, int maxc) {
colorcnt += octt_add(ct, samp[x], samp[x+1], samp[x+2]);
x += 3;
if (colorcnt > maxc) {
- octt_delete(ct);
+ octt_delete(ct);
+ myfree(samp);
return -1;
}
}
@@ -1631,6 +1710,9 @@ i_test_format_probe(io_glue *data, int length) {
/* JPEG 2000
This might match a little loosely */
FORMAT_ENTRY("\x00\x00\x00\x0CjP \x0D\x0A\x87\x0A", "jp2"),
+
+ /* FLIF - Free Lossless Image Format - https://flif.info/spec.html */
+ FORMAT_ENTRY("FLIF", "flif")
};
static const struct magic_entry more_formats[] = {
/* these were originally both listed as ico, but cur files can
@@ -1714,7 +1796,8 @@ i_img_is_monochrome(i_img *im, int *zero_is_white) {
if (im->type == i_palette_type
&& i_colorcount(im) == 2) {
i_color colors[2];
- i_getcolors(im, 0, colors, 2);
+ if (!i_getcolors(im, 0, colors, 2))
+ return 0;
if (im->channels == 3) {
if (colors[0].rgb.r == 255 &&
colors[0].rgb.g == 255 &&