]> git.imager.perl.org - imager.git/blobdiff - palimg.c
- start of external Imager API access:
[imager.git] / palimg.c
index ad87342445e0eb4c443a41d5128ec5bc8fed874d..90ec29e7048c7352738b8cff22012ee6b4ca452c 100644 (file)
--- a/palimg.c
+++ b/palimg.c
@@ -18,15 +18,15 @@ Basic 8-bit/sample paletted image
 =cut
 */
 
-#include "image.h"
-#include "imagei.h"
+#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, 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, i_color *vals);
-static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int *chans, int chan_count);
+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, i_palidx *vals);
 static int i_addcolors_p(i_img *im, i_color *color, int count);
@@ -74,7 +74,7 @@ static i_img IIM_base_8bit_pal =
 };
 
 /*
-=item i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal)
+=item i_img_pal_new_low(im, x, y, channels, maxpal)
 
 Creates a new paletted image.
 
@@ -84,9 +84,10 @@ Currently 0 < maxpal <= 256
 */
 i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
   i_img_pal_ext *palext;
+  int bytes, line_bytes;
 
   i_clear_error();
-  if (maxpal < 0 || maxpal > 256) {
+  if (maxpal < 1 || maxpal > 256) {
     i_push_error(0, "Maximum of 256 palette entries");
     return NULL;
   }
@@ -95,7 +96,21 @@ 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 postive and <= %d", MAXCHANNELS);
+    i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS);
+    return NULL;
+  }
+  bytes = sizeof(i_palidx) * x * y;
+  if (bytes / y / sizeof(i_palidx) != x) {
+    i_push_error(0, "integer overflow calculating image allocation");
+    return NULL;
+  }
+
+  /* basic assumption: we can always allocate a buffer representing a
+     line from the image, otherwise we're going to have trouble
+     working with the image */
+  line_bytes = sizeof(i_color) * x;
+  if (line_bytes / x != sizeof(i_color)) {
+    i_push_error(0, "integer overflow calculating scanline allocation");
     return NULL;
   }
 
@@ -107,7 +122,7 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
   palext->last_found = -1;
   im->ext_data = palext;
   i_tags_new(&im->tags);
-  im->bytes = sizeof(i_palidx) * x * y;
+  im->bytes = bytes;
   im->idata = mymalloc(im->bytes);
   im->channels = channels;
   memset(im->idata, 0, im->bytes);
@@ -117,10 +132,28 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
   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 = mymalloc(sizeof(i_img));
+  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;
+  }
 
-  return i_img_pal_new_low(im, x, y, channels, maxpal);
+  return im;
 }
 
 /*
@@ -158,8 +191,6 @@ The conversion cannot be done for virtual images.
 */
 int i_img_to_rgb_inplace(i_img *im) {
   i_img temp;
-  i_color *pal;
-  int palsize;
 
   if (im->virtual)
     return 0;
@@ -188,20 +219,28 @@ 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;
+
+  i_clear_error();
   
-  im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
+  i_quant_makemap(quant, &src, 1);
+  result = i_quant_translate(quant, src);
 
-  quant_makemap(quant, &src, 1);
-  result = quant_translate(quant, src);
+  if (result) {
 
-  /* copy things over */
-  memcpy(im->idata, result, im->bytes);
-  PALEXT(im)->count = quant->mc_count;
-  memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
+    im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
 
-  myfree(result);
+    /* copy things over */
+    memcpy(im->idata, result, im->bytes);
+    PALEXT(im)->count = quant->mc_count;
+    memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
+    
+    myfree(result);
 
-  return im;
+    return im;
+  }
+  else {
+    return NULL;
+  }
 }
 
 /*
@@ -244,7 +283,7 @@ present in the image.
 
 =cut
 */
-int i_ppix_p(i_img *im, int x, int y, i_color *val) {
+static int i_ppix_p(i_img *im, int x, int y, i_color *val) {
   i_palidx which;
   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
     return -1;
@@ -262,13 +301,13 @@ int i_ppix_p(i_img *im, int x, int y, i_color *val) {
 }
 
 /*
-=item i_gpix(i_img *im, int x, int y, i_color *val)
+=item i_gpix_p(i_img *im, int x, int y, i_color *val)
 
 Retrieve a pixel, converting from a palette index to a color.
 
 =cut
 */
-int i_gpix_p(i_img *im, int x, int y, i_color *val) {
+static int i_gpix_p(i_img *im, int x, int y, i_color *val) {
   i_palidx which;
   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
     return -1;
@@ -288,7 +327,7 @@ Retrieve a row of pixels.
 
 =cut
 */
-int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) {
+static int i_glin_p(i_img *im, int l, int r, int 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;
@@ -320,8 +359,8 @@ RGB.
 
 =cut
 */
-int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
-  int ch, count, i;
+static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
+  int count, i;
   i_palidx *data;
   i_palidx which;
   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
@@ -351,8 +390,8 @@ int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
 
 =cut
 */
-int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, 
-              int *chans, int chan_count) {
+static int i_gsamp_p(i_img *im, int l, int r, int 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;
@@ -405,7 +444,7 @@ int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps,
 =cut
 */
 
-int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
+static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
     i_palidx *data;
     int i, w;
@@ -429,7 +468,7 @@ int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
 =cut
 */
 
-int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
+static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
     i_palidx *data;
     int i, w;
@@ -452,7 +491,7 @@ int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
 
 =cut
 */
-int i_addcolors_p(i_img *im, i_color *color, int count) {
+static int i_addcolors_p(i_img *im, i_color *color, int count) {
   if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
     int result = PALEXT(im)->count;
     int index = result;
@@ -474,7 +513,7 @@ int i_addcolors_p(i_img *im, i_color *color, int count) {
 
 =cut
 */
-int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
+static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
   if (i >= 0 && i+count <= PALEXT(im)->count) {
     while (count) {
       *color++ = PALEXT(im)->pal[i++];
@@ -500,7 +539,7 @@ static int color_eq(i_img *im, i_color *c1, i_color *c2) {
 
 =cut
 */
-int i_colorcount_p(i_img *im) {
+static int i_colorcount_p(i_img *im) {
   return PALEXT(im)->count;
 }
 
@@ -509,7 +548,7 @@ int i_colorcount_p(i_img *im) {
 
 =cut
 */
-int i_maxcolors_p(i_img *im) {
+static int i_maxcolors_p(i_img *im) {
   return PALEXT(im)->alloc;
 }
 
@@ -518,8 +557,8 @@ int i_maxcolors_p(i_img *im) {
 
 =cut
 */
-int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
-  if (index >= 0 && count >= 1 && index + count < PALEXT(im)->count) {
+static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
+  if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
     while (count) {
       PALEXT(im)->pal[index++] = *colors++;
       --count;
@@ -535,7 +574,7 @@ int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
 
 =cut
 */
-int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
+static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
   if (PALEXT(im)->count) {
     int i;
     /* often the same color comes up several times in a row */
@@ -554,3 +593,17 @@ int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
   }
   return 0;
 }
+
+/*
+=back
+
+=head1 AUTHOR
+
+Tony Cook <tony@develop-help.com>
+
+=head1 SEE ALSO
+
+Imager(3)
+
+=cut
+*/