]> git.imager.perl.org - imager.git/blobdiff - map.c
avoid a possible sign-extension for offsets/sizes in SGI
[imager.git] / map.c
diff --git a/map.c b/map.c
index 0f9bd1924ccb9e9a586a907cbfae92e5c5e696f6..9ddc62f855ebce456f7dc1588320b340cd1681f1 100644 (file)
--- a/map.c
+++ b/map.c
@@ -17,7 +17,7 @@ converting from RGBA to greyscale and back.
 =cut
 */
 
-#include "image.h"
+#include "imager.h"
 
 
 /*
@@ -35,47 +35,41 @@ maps im inplace into another image.
 */
 
 void
-i_map(i_img *im, int mapcount, unsigned char (*maps)[256], unsigned int *chmasks) {
+i_map(i_img *im, unsigned char (*maps)[256], unsigned int mask) {
   i_color *vals;
-  int x, y;
-  int mapno, i, ch;
-  unsigned int mask = 0;
-  unsigned char (**cmaps)[256];
-  int minset = -1, maxset;
+  i_img_dim x, y;
+  int i, ch;
+  int minset = -1, maxset = 0;
 
-  mm_log((1,"i_map(im %p, mapcount %d, maps %p, chmasks %p)\n", im, mapcount, maps, chmasks));
+  mm_log((1,"i_map(im %p, maps %p, chmask %u)\n", im, maps, mask));
 
-
-  for(mapno=0; mapno<mapcount; mapno++) mask |=chmasks[mapno];
   if (!mask) return; /* nothing to do here */
 
-
-
-  for(i=0; i<im->channels; i++)
+  for(i=0; i<im->channels; i++) {
     if (mask & (1<<i)) {
       if (minset == -1) minset = i;
       maxset = i;
     }
+  }
+
+  mm_log((1, "minset=%d maxset=%d\n", minset, maxset));
+
+  if (minset == -1)
+    return;
 
-  cmaps = mymalloc(sizeof(unsigned char (*)[256])*im->channels);
-  memset(cmaps, 0, sizeof(unsigned char (*)[256])*im->channels);
-  for(mapno=0; mapno<mapcount; mapno++)
-    for(ch=0; ch<im->channels; ch++) 
-      if (chmasks[i] & (1<<ch)) cmaps[ch] = &maps[mapno];
-  
   vals = mymalloc(sizeof(i_color) * im->xsize);
+
   for (y = 0; y < im->ysize; ++y) {
     i_glin(im, 0, im->xsize, y, vals);
     for (x = 0; x < im->xsize; ++x) {
-      int lidx = x * im->channels;
       for(ch = minset; ch<=maxset; ch++) {
-       if (!cmaps[ch]) continue;
-       vals[lidx].channel[ch] = (*cmaps[ch])[vals[lidx].channel[ch]];
+       if (!(mask & (1 << ch)))
+         continue;
+       vals[x].channel[ch] = maps[ch][vals[x].channel[ch]];
       }
     }
     i_plin(im, 0, im->xsize, y, vals);
   }
-  myfree(cmaps);
   myfree(vals);
 }