]> git.imager.perl.org - imager.git/blobdiff - image.c
RT #117878: handle invalid image objects for write_multi()
[imager.git] / image.c
diff --git a/image.c b/image.c
index b2561ef87b6a894f8f9a228cbc9372bd591cd5c2..cbd1b38a0f3bfc2cb719cdfc0df611c2f53942da 100644 (file)
--- a/image.c
+++ b/image.c
@@ -14,7 +14,7 @@ image.c - implements most of the basic functions of Imager and much of the rest
   i_color *c;
   c = i_color_new(red, green, blue, alpha);
   ICL_DESTROY(c);
-  i = i_img_new();
+  i = i_img_8_new();
   i_img_destroy(i);
   // and much more
 
@@ -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)
+X<im_img_alloc API>X<i_img_alloc API>
 =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<img>)
+=item im_img_init(aIMCTX, image)
+X<im_img_init API>X<i_img_init API>
 =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</im_img_alloc(aIMCTX)> for more information.
 
 =cut
 */
@@ -117,7 +122,7 @@ ICL_new_internal(unsigned char r,unsigned char g,unsigned char b,unsigned char a
 
   im_log((aIMCTX,1,"ICL_new_internal(r %d,g %d,b %d,a %d)\n", r, g, b, a));
 
-  if ( (cl=mymalloc(sizeof(i_color))) == NULL) i_fatal(2,"malloc() error\n");
+  if ( (cl=mymalloc(sizeof(i_color))) == NULL) im_fatal(aIMCTX, 2,"malloc() error\n");
   cl->rgba.r = r;
   cl->rgba.g = g;
   cl->rgba.b = b;
@@ -147,7 +152,7 @@ ICL_set_internal(i_color *cl,unsigned char r,unsigned char g,unsigned char b,uns
   im_log((aIMCTX,1,"ICL_set_internal(cl* %p,r %d,g %d,b %d,a %d)\n",cl,r,g,b,a));
   if (cl == NULL)
     if ( (cl=mymalloc(sizeof(i_color))) == NULL)
-      i_fatal(2,"malloc() error\n");
+      im_fatal(aIMCTX, 2,"malloc() error\n");
   cl->rgba.r=r;
   cl->rgba.g=g;
   cl->rgba.b=b;
@@ -223,7 +228,7 @@ i_fcolor *i_fcolor_new(double r, double g, double b, double a) {
 
   im_log((aIMCTX, 1,"i_fcolor_new(r %g,g %g,b %g,a %g)\n", r, g, b, a));
 
-  if ( (cl=mymalloc(sizeof(i_fcolor))) == NULL) i_fatal(2,"malloc() error\n");
+  if ( (cl=mymalloc(sizeof(i_fcolor))) == NULL) im_fatal(aIMCTX, 2,"malloc() error\n");
   cl->rgba.r = r;
   cl->rgba.g = g;
   cl->rgba.b = b;
@@ -399,6 +404,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<channel> may be C<NULL>.
+
+=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<im>, C<src>, C<x1>, C<y1>, C<x2>, C<y2>, C<tx>, C<ty>, C<trans>)
 
@@ -1044,7 +1128,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 +1144,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<my;y++) for(x=0;x<fx;x++) {
     i_gpix(im,x*2,y,&val1);
     i_gpix(im,x*2+1,y,&val2);
@@ -1127,7 +1210,8 @@ i_count_colors(i_img *im,int maxc) {
       for(x = 0; x < samp_cnt; ) {
           colorcnt += octt_add(ct, samp[x], samp[x+1], samp[x+2]);
           x += 3;
-          if (colorcnt > maxc) { 
+          if (colorcnt > maxc) {
+             myfree(samp);
               octt_delete(ct); 
               return -1; 
           }
@@ -1218,7 +1302,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; 
       }
     }