]> git.imager.perl.org - imager.git/blobdiff - palimg.c
rename APIRef.pm, API.pm to *.pod since they contain no code
[imager.git] / palimg.c
index 1667238ec57b0d3bf94f47044526a3a48122be1e..261325954f2025d5546ff5518c3562a130caee0e 100644 (file)
--- a/palimg.c
+++ b/palimg.c
@@ -18,23 +18,23 @@ 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_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, 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, i_palidx *vals);
-static int i_addcolors_p(i_img *im, i_color *color, int count);
+static int i_ppal_p(i_img *pm, int l, int r, int 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);
 static int i_maxcolors_p(i_img *im);
-static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry);
-static int i_setcolors_p(i_img *im, int index, i_color *color, int count);
+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);
 
@@ -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,10 +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;
+  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;
   }
@@ -101,7 +101,16 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
   }
   bytes = sizeof(i_palidx) * x * y;
   if (bytes / y / sizeof(i_palidx) != x) {
-    i_push_errorf(0, "integer overflow calculating image allocation");
+    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;
   }
 
@@ -123,6 +132,18 @@ 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;
   mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal));
@@ -201,8 +222,8 @@ i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
 
   i_clear_error();
   
-  quant_makemap(quant, &src, 1);
-  result = quant_translate(quant, src);
+  i_quant_makemap(quant, &src, 1);
+  result = i_quant_translate(quant, src);
 
   if (result) {
 
@@ -253,7 +274,7 @@ static void i_destroy_p(i_img *im) {
 }
 
 /*
-=item i_ppix_p(i_img *im, int x, int y, i_color *val)
+=item i_ppix_p(i_img *im, int x, int y, const i_color *val)
 
 Write to a pixel in the image.
 
@@ -262,7 +283,8 @@ present in the image.
 
 =cut
 */
-static 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, const i_color *val) {
   i_palidx which;
   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
     return -1;
@@ -280,7 +302,7 @@ static 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.
 
@@ -329,7 +351,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, i_color *vals)
+=item i_plin_p(i_img *im, int l, int r, int y, const i_color *vals)
 
 Write a line of color data to the image.
 
@@ -338,7 +360,8 @@ RGB.
 
 =cut
 */
-static int i_plin_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) {
   int count, i;
   i_palidx *data;
   i_palidx which;
@@ -442,12 +465,12 @@ 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, i_palidx *vals)
+=item i_ppal_p(i_img *im, int l, int r, int y, const i_palidx *vals)
 
 =cut
 */
 
-static 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, const i_palidx *vals) {
   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
     i_palidx *data;
     int i, w;
@@ -466,11 +489,11 @@ static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
 }
 
 /*
-=item i_addcolors_p(i_img *im, i_color *color, int count)
+=item i_addcolors_p(i_img *im, const i_color *color, int count)
 
 =cut
 */
-static int i_addcolors_p(i_img *im, i_color *color, int count) {
+static int i_addcolors_p(i_img *im, const i_color *color, int count) {
   if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
     int result = PALEXT(im)->count;
     int index = result;
@@ -504,7 +527,7 @@ static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
     return 0;
 }
 
-static int color_eq(i_img *im, i_color *c1, i_color *c2) {
+static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
   int ch;
   for (ch = 0; ch < im->channels; ++ch) {
     if (c1->channel[ch] != c2->channel[ch])
@@ -532,11 +555,11 @@ static int i_maxcolors_p(i_img *im) {
 }
 
 /*
-=item i_setcolors_p(i_img *im, int index, i_color *colors, int count)
+=item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
 
 =cut
 */
-static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
+static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
   if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
     while (count) {
       PALEXT(im)->pal[index++] = *colors++;
@@ -553,7 +576,7 @@ static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
 
 =cut
 */
-static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
+static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
   if (PALEXT(im)->count) {
     int i;
     /* often the same color comes up several times in a row */