]> git.imager.perl.org - imager.git/blobdiff - img16.c
set eol-style so that generation doesn't make svn burp
[imager.git] / img16.c
diff --git a/img16.c b/img16.c
index 2d795477fbf172662c0c56112cc7917c9e4916c7..78c02dde6f9589667d04137532c5191dc542a5fe 100644 (file)
--- a/img16.c
+++ b/img16.c
@@ -20,17 +20,17 @@ sample image type to work with.
 =cut
 */
 
-#include "image.h"
-#include "imagei.h"
+#include "imager.h"
+#include "imageri.h"
 
-static int i_ppix_d16(i_img *im, int x, int y, i_color *val);
+static int i_ppix_d16(i_img *im, int x, int y, const i_color *val);
 static int i_gpix_d16(i_img *im, int x, int y, i_color *val);
 static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals);
-static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals);
-static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val);
+static int i_plin_d16(i_img *im, int l, int r, int y, const i_color *vals);
+static int i_ppixf_d16(i_img *im, int x, int y, const i_fcolor *val);
 static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val);
 static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals);
-static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals);
+static int i_plinf_d16(i_img *im, int l, int r, int y, const i_fcolor *vals);
 static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps, 
                        int const *chans, int chan_count);
 static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps, 
@@ -136,14 +136,14 @@ typedef unsigned short i_sample16_t;
 #endif
 
 /*
-=item i_img_16_new(int x, int y, int ch)
+=item i_img_16_new_low(int x, int y, int ch)
 
 Creates a new 16-bit per sample image.
 
 =cut
 */
 i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
-  int bytes;
+  int bytes, line_bytes;
   mm_log((1,"i_img_16_new(x %d, y %d, ch %d)\n", x, y, ch));
 
   if (x < 1 || y < 1) {
@@ -160,6 +160,15 @@ i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
     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_fcolor) * x;
+  if (line_bytes / x != sizeof(i_fcolor)) {
+    i_push_error(0, "integer overflow calculating scanline allocation");
+    return NULL;
+  }
+
   *im = IIM_base_16bit_direct;
   i_tags_new(&im->tags);
   im->xsize = x;
@@ -179,6 +188,18 @@ i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
   return im;
 }
 
+/*
+=item i_img_16_new(x, y, ch)
+
+=category Image creation
+
+Create a new 16-bit/sample image.
+
+Returns the image on success, or NULL on failure.
+
+=cut
+*/
+
 i_img *i_img_16_new(int x, int y, int ch) {
   i_img *im;
   
@@ -197,15 +218,22 @@ i_img *i_img_16_new(int x, int y, int ch) {
   return im;
 }
 
-static int i_ppix_d16(i_img *im, int x, int y, i_color *val) {
+static int i_ppix_d16(i_img *im, int x, int y, const i_color *val) {
   int off, ch;
 
   if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize) 
     return -1;
 
   off = (x + y * im->xsize) * im->channels;
-  for (ch = 0; ch < im->channels; ++ch)
-    STORE8as16(im->idata, off+ch, val->channel[ch]);
+  if (I_ALL_CHANNELS_WRITABLE(im)) {
+    for (ch = 0; ch < im->channels; ++ch)
+      STORE8as16(im->idata, off+ch, val->channel[ch]);
+  }
+  else {
+    for (ch = 0; ch < im->channels; ++ch)
+      if (im->ch_mask & (1 << ch))
+       STORE8as16(im->idata, off+ch, val->channel[ch]);
+  }
 
   return 0;
 }
@@ -223,15 +251,22 @@ static int i_gpix_d16(i_img *im, int x, int y, i_color *val) {
   return 0;
 }
 
-static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
+static int i_ppixf_d16(i_img *im, int x, int y, const i_fcolor *val) {
   int off, ch;
 
   if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize) 
     return -1;
 
   off = (x + y * im->xsize) * im->channels;
-  for (ch = 0; ch < im->channels; ++ch)
-    STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
+  if (I_ALL_CHANNELS_WRITABLE(im)) {
+    for (ch = 0; ch < im->channels; ++ch)
+      STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
+  }
+  else {
+    for (ch = 0; ch < im->channels; ++ch)
+      if (im->ch_mask & (1 << ch))
+       STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
+  }
 
   return 0;
 }
@@ -270,7 +305,7 @@ static int i_glin_d16(i_img *im, int l, int r, int y, i_color *vals) {
   }
 }
 
-static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals) {
+static int i_plin_d16(i_img *im, int l, int r, int y, const i_color *vals) {
   int ch, count, i;
   int off;
   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
@@ -278,10 +313,21 @@ static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals) {
       r = im->xsize;
     off = (l+y*im->xsize) * im->channels;
     count = r - l;
-    for (i = 0; i < count; ++i) {
-      for (ch = 0; ch < im->channels; ++ch) {
-        STORE8as16(im->idata, off, vals[i].channel[ch]);
-        ++off;
+    if (I_ALL_CHANNELS_WRITABLE(im)) {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         STORE8as16(im->idata, off, vals[i].channel[ch]);
+         ++off;
+       }
+      }
+    }
+    else {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         if (im->ch_mask & (1 << ch))
+           STORE8as16(im->idata, off, vals[i].channel[ch]);
+         ++off;
+       }
       }
     }
     return count;
@@ -312,7 +358,7 @@ static int i_glinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
   }
 }
 
-static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
+static int i_plinf_d16(i_img *im, int l, int r, int y, const i_fcolor *vals) {
   int ch, count, i;
   int off;
   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
@@ -320,10 +366,21 @@ static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
       r = im->xsize;
     off = (l+y*im->xsize) * im->channels;
     count = r - l;
-    for (i = 0; i < count; ++i) {
-      for (ch = 0; ch < im->channels; ++ch) {
-        STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
-        ++off;
+    if (I_ALL_CHANNELS_WRITABLE(im)) {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
+         ++off;
+       }
+      }
+    }
+    else {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         if (im->ch_mask & (1 << ch))
+           STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
+         ++off;
+       }
       }
     }
     return count;