]> git.imager.perl.org - imager.git/blobdiff - img16.c
API documentation (mostly)
[imager.git] / img16.c
diff --git a/img16.c b/img16.c
index bb5847561e97056e4552d905a2968d5d2fe17680..321821e089efe0c258199a3eaf337d991773f963 100644 (file)
--- a/img16.c
+++ b/img16.c
@@ -20,21 +20,25 @@ 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, 
                         int const *chans, int chan_count);
+static int i_gsamp_bits_d16(i_img *im, int l, int r, int y, unsigned *samps, 
+                           int const *chans, int chan_count, int bits);
+static int i_psamp_bits_d16(i_img *im, int l, int r, int y, unsigned const *samps, 
+                           int const *chans, int chan_count, int bits);
 
 /*
 =item IIM_base_16bit_direct
@@ -70,12 +74,17 @@ static i_img IIM_base_16bit_direct =
 
   NULL, /* i_f_gpal */
   NULL, /* i_f_ppal */
-  NULL, /* i_f_addcolor */
-  NULL, /* i_f_getcolor */
+  NULL, /* i_f_addcolors */
+  NULL, /* i_f_getcolors */
   NULL, /* i_f_colorcount */
+  NULL, /* i_f_maxcolors */
   NULL, /* i_f_findcolor */
+  NULL, /* i_f_setcolors */
 
   NULL, /* i_f_destroy */
+
+  i_gsamp_bits_d16,
+  i_psamp_bits_d16,
 };
 
 /* it's possible some platforms won't have a 16-bit integer type,
@@ -111,12 +120,9 @@ typedef unsigned short i_sample16_t;
 #define STORE16(bytes, offset, word) \
    (((i_sample16_t *)(bytes))[offset] = (word))
 #define STORE8as16(bytes, offset, byte) \
-   (((i_sample16_t *)(bytes))[offset] = (byte) * 256)
+   (((i_sample16_t *)(bytes))[offset] = (byte) * 256 + (byte))
 #define GET16(bytes, offset) \
      (((i_sample16_t *)(bytes))[offset])
-#define GET16as8(bytes, offset) \
-     (((i_sample16_t *)(bytes))[offset] / 256)
-
 #else
 
 /* we have to do this the hard way */
@@ -125,24 +131,34 @@ typedef unsigned short i_sample16_t;
     (((unsigned char *)(bytes))[(offset)*2+1] = (word) & 0xFF))
 #define STORE8as16(bytes, offset, byte) \
    ((((unsigned char *)(bytes))[(offset)*2] = (byte)), \
-    (((unsigned char *)(bytes))[(offset)*2+1] = 0))
+    (((unsigned char *)(bytes))[(offset)*2+1] = (byte)))
    
 #define GET16(bytes, offset) \
    (((unsigned char *)(bytes))[(offset)*2] * 256 \
     + ((unsigned char *)(bytes))[(offset)*2+1])
-#define GET16as8(bytes, offset) \
-   (((unsigned char *)(bytes))[(offset)*2] << 8)
 
 #endif
 
+#define GET16as8(bytes, offset) \
+     ((((i_sample16_t *)(bytes))[offset]+127) / 257)
+
 /*
-=item i_img_16_new(int x, int y, int ch)
+=item i_img_16_new(x, y, ch)
+
+=category Image creation/destruction
+=synopsis i_img *img = i_img_16_new(width, height, channels);
+
+Create a new 16-bit/sample image.
 
-Creates a new 16-bit per sample image.
+Returns the image on success, or NULL on failure.
 
 =cut
 */
-i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
+
+i_img *i_img_16_new(int x, int y, int ch) {
+  i_img *im;
+  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) {
@@ -153,53 +169,85 @@ i_img *i_img_16_new_low(i_img *im, int x, int y, int ch) {
     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 = i_img_alloc();
   *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) {
-    memset(im->idata, 0, im->bytes);
-  }
-  else {
-    i_tags_destroy(&im->tags);
-    im = NULL;
-  }
-  
+  memset(im->idata, 0, im->bytes);
+
+  i_img_init(im);
+
   return im;
 }
 
-i_img *i_img_16_new(int x, int y, int ch) {
-  i_img *im;
-  
-  i_clear_error();
+/*
+=item i_img_to_rgb16(im)
 
-  im = mymalloc(sizeof(i_img));
-  if (im) {
-    if (!i_img_16_new_low(im, x, y, ch)) {
-      myfree(im);
-      im = NULL;
-    }
+=category Image creation
+
+Returns a 16-bit/sample version of the supplied image.
+
+Returns the image on success, or NULL on failure.
+
+=cut
+*/
+
+i_img *
+i_img_to_rgb16(i_img *im) {
+  i_img *targ;
+  i_fcolor *line;
+  int y;
+
+  targ = i_img_16_new(im->xsize, im->ysize, im->channels);
+  if (!targ)
+    return NULL;
+  line = mymalloc(sizeof(i_fcolor) * im->xsize);
+  for (y = 0; y < im->ysize; ++y) {
+    i_glinf(im, 0, im->xsize, y, line);
+    i_plinf(targ, 0, im->xsize, y, line);
   }
-  
-  mm_log((1, "(%p) <- i_img_16_new\n", im));
-  
-  return im;
+
+  myfree(line);
+
+  return targ;
 }
 
-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) 
+  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;
 }
@@ -207,7 +255,7 @@ 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) {
   int off, ch;
 
-  if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize) 
+  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) 
     return -1;
 
   off = (x + y * im->xsize) * im->channels;
@@ -217,15 +265,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) 
+  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;
 }
@@ -233,7 +288,7 @@ static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
 static int i_gpixf_d16(i_img *im, int x, int y, i_fcolor *val) {
   int off, ch;
 
-  if (x < 0 || x >= im->xsize || y < 0 || y > im->ysize) 
+  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) 
     return -1;
 
   off = (x + y * im->xsize) * im->channels;
@@ -264,7 +319,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) {
@@ -272,10 +327,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;
@@ -306,7 +372,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) {
@@ -314,10 +380,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;
@@ -356,6 +433,11 @@ static int i_gsamp_d16(i_img *im, int l, int r, int y, i_sample_t *samps,
       }
     }
     else {
+      if (chan_count <= 0 || chan_count > im->channels) {
+       i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", 
+                     chan_count);
+       return 0;
+      }
       for (i = 0; i < w; ++i) {
         for (ch = 0; ch < chan_count; ++ch) {
           *samps++ = GET16as8(im->idata, off+ch);
@@ -401,6 +483,11 @@ static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
       }
     }
     else {
+      if (chan_count <= 0 || chan_count > im->channels) {
+       i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", 
+                     chan_count);
+       return 0;
+      }
       for (i = 0; i < w; ++i) {
         for (ch = 0; ch < chan_count; ++ch) {
           *samps++ = Sample16ToF(GET16(im->idata, off+ch));
@@ -417,6 +504,121 @@ static int i_gsampf_d16(i_img *im, int l, int r, int y, i_fsample_t *samps,
   }
 }
 
+static int 
+i_gsamp_bits_d16(i_img *im, int l, int r, int y, unsigned *samps, 
+                           int const *chans, int chan_count, int bits) {
+  int ch, count, i, w;
+  int off;
+
+  if (bits != 16) {
+    return i_gsamp_bits_fb(im, l, r, y, samps, chans, chan_count, bits);
+  }
+
+  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
+    if (r > im->xsize)
+      r = im->xsize;
+    off = (l+y*im->xsize) * im->channels;
+    w = r - l;
+    count = 0;
+
+    if (chans) {
+      /* make sure we have good channel numbers */
+      for (ch = 0; ch < chan_count; ++ch) {
+        if (chans[ch] < 0 || chans[ch] >= im->channels) {
+          i_push_errorf(0, "No channel %d in this image", chans[ch]);
+          return -1;
+        }
+      }
+      for (i = 0; i < w; ++i) {
+        for (ch = 0; ch < chan_count; ++ch) {
+          *samps++ = GET16(im->idata, off+chans[ch]);
+          ++count;
+        }
+        off += im->channels;
+      }
+    }
+    else {
+      if (chan_count <= 0 || chan_count > im->channels) {
+       i_push_error(0, "Invalid channel count");
+       return -1;
+      }
+      for (i = 0; i < w; ++i) {
+        for (ch = 0; ch < chan_count; ++ch) {
+          *samps++ = GET16(im->idata, off+ch);
+          ++count;
+        }
+        off += im->channels;
+      }
+    }
+
+    return count;
+  }
+  else {
+    i_push_error(0, "Image position outside of image");
+    return -1;
+  }
+}
+
+static int 
+i_psamp_bits_d16(i_img *im, int l, int r, int y, unsigned const *samps, 
+                           int const *chans, int chan_count, int bits) {
+  int ch, count, i, w;
+  int off;
+
+  if (bits != 16) {
+    i_push_error(0, "Invalid bits for 16-bit image");
+    return -1;
+  }
+
+  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
+    if (r > im->xsize)
+      r = im->xsize;
+    off = (l+y*im->xsize) * im->channels;
+    w = r - l;
+    count = 0;
+
+    if (chans) {
+      /* make sure we have good channel numbers */
+      for (ch = 0; ch < chan_count; ++ch) {
+        if (chans[ch] < 0 || chans[ch] >= im->channels) {
+          i_push_errorf(0, "No channel %d in this image", chans[ch]);
+          return -1;
+        }
+      }
+      for (i = 0; i < w; ++i) {
+        for (ch = 0; ch < chan_count; ++ch) {
+         if (im->ch_mask & (1 << ch))
+           STORE16(im->idata, off+chans[ch], *samps);
+         ++samps;
+         ++count;
+        }
+        off += im->channels;
+      }
+    }
+    else {
+      if (chan_count <= 0 || chan_count > im->channels) {
+       i_push_error(0, "Invalid channel count");
+       return -1;
+      }
+      for (i = 0; i < w; ++i) {
+        for (ch = 0; ch < chan_count; ++ch) {
+         if (im->ch_mask & (1 << ch)) 
+           STORE16(im->idata, off+ch, *samps);
+         ++samps;
+          ++count;
+        }
+        off += im->channels;
+      }
+    }
+
+    return count;
+  }
+  else {
+    i_push_error(0, "Image position outside of image");
+    return -1;
+  }
+}
+
 /*
 =back