]> git.imager.perl.org - imager.git/blobdiff - img16.c
- start of external Imager API access:
[imager.git] / img16.c
diff --git a/img16.c b/img16.c
index 3d8f2c5d342d1e5b99854d026899b4c056630f1b..af477cef2072b3031b486b7cb886aa2c46c2b32b 100644 (file)
--- a/img16.c
+++ b/img16.c
@@ -20,8 +20,8 @@ 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_gpix_d16(i_img *im, int x, int y, i_color *val);
@@ -32,9 +32,9 @@ 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_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps, 
-                       int *chans, int chan_count);
+                       int const *chans, int chan_count);
 static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps, 
-                        int *chans, int chan_count);
+                        int const *chans, int chan_count);
 
 /*
 =item IIM_base_16bit_direct
@@ -49,7 +49,7 @@ static i_img IIM_base_16bit_direct =
 {
   0, /* channels set */
   0, 0, 0, /* xsize, ysize, bytes */
-  ~0, /* ch_mask */
+  ~0U, /* ch_mask */
   i_16_bits, /* bits */
   i_direct_type, /* type */
   0, /* virtual */
@@ -82,8 +82,12 @@ static i_img IIM_base_16bit_direct =
    so we check for one otherwise we work by bytes directly
 
    We do assume 8-bit char
+
+   "Compaq C V6.4-009 on Compaq Tru64 UNIX V5.1A (Rev. 1885)" says it
+   supports C99, but doesn't supply stdint.h, which is required for
+   both hosted and freestanding implementations.  So guard against it.
 */
-#if __STDC_VERSION__ >= 199901L
+#if __STDC_VERSION__ >= 199901L && !defined(OS_dec_osf)
 /* C99 should define something useful */
 #include <stdint.h>
 #ifdef UINT16_MAX
@@ -132,19 +136,45 @@ 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, line_bytes;
   mm_log((1,"i_img_16_new(x %d, y %d, ch %d)\n", x, y, ch));
+
+  if (x < 1 || y < 1) {
+    i_push_error(0, "Image sizes must be positive");
+    return NULL;
+  }
+  if (ch < 1 || ch > MAXCHANNELS) {
+    i_push_errorf(0, "channels must be between 1 and %d", MAXCHANNELS);
+    return NULL;
+  }
+  bytes =  x * y * ch * 2;
+  if (bytes / y / ch / 2 != x) {
+    i_push_errorf(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_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;
   im->ysize = y;
   im->channels = ch;
-  im->bytes = x * y * ch * 2;
+  im->bytes = bytes;
   im->ext_data = NULL;
   im->idata = mymalloc(im->bytes);
   if (im->idata) {
@@ -158,8 +188,22 @@ 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;
+  
+  i_clear_error();
 
   im = mymalloc(sizeof(i_img));
   if (im) {
@@ -181,8 +225,15 @@ static int i_ppix_d16(i_img *im, int x, int y, i_color *val) {
     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;
 }
@@ -207,8 +258,15 @@ static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
     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;
 }
@@ -255,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;
@@ -297,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;
@@ -311,7 +391,7 @@ static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
 }
 
 static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps, 
-                       int *chans, int chan_count) {
+                       int const *chans, int chan_count) {
   int ch, count, i, w;
   int off;
 
@@ -356,7 +436,7 @@ static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps,
 }
 
 static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps, 
-                        int *chans, int chan_count) {
+                        int const *chans, int chan_count) {
   int ch, count, i, w;
   int off;
 
@@ -400,3 +480,16 @@ static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
   }
 }
 
+/*
+=back
+
+=head1 AUTHOR
+
+Tony Cook <tony@develop-help.com>
+
+=head1 SEE ALSO
+
+Imager(3)
+
+=cut
+*/