about summary refs log tree commit diff
path: root/converter/ppm
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2024-10-23 19:38:01 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2024-10-23 19:38:01 +0000
commit6d139bfd596a6a955bb732203b53b50884895ea1 (patch)
tree7899af62232776acdb7726e98ca6adaa87afa462 /converter/ppm
parent4597173816262c5e0abf3147a4bd08577a12f20a (diff)
downloadnetpbm-mirror-6d139bfd596a6a955bb732203b53b50884895ea1.tar.gz
netpbm-mirror-6d139bfd596a6a955bb732203b53b50884895ea1.tar.xz
netpbm-mirror-6d139bfd596a6a955bb732203b53b50884895ea1.zip
Fix always incorrect output with multiplane images introduced by previous cleanup HEAD trunk
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4965 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/ppm')
-rw-r--r--converter/ppm/pcxtoppm.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/converter/ppm/pcxtoppm.c b/converter/ppm/pcxtoppm.c
index f06dd4e8..8341cf18 100644
--- a/converter/ppm/pcxtoppm.c
+++ b/converter/ppm/pcxtoppm.c
@@ -363,9 +363,25 @@ pcxPlanesToPixels(unsigned char * const pixels,
                   unsigned int    const planes,
                   unsigned int    const bitsPerPixel) {
 /*----------------------------------------------------------------------------
-   Convert multi-plane format into 1 pixel per byte.
+   Unpack sub-byte sample values to yield one byte per pixel.
+
+   As input, 'bitPlanes' is a row of the image.  Each byte contains 8 samples,
+   one per bit.  They are arranged in plane major, column minor order, e.g.
+   in a 100-column image, the Plane 1 samples of the Columns 0-8 are in
+   bitPlanes[100].
+
+   As output, each byte of 'pixels' is one pixel, in major order by column and
+   minor order by plane, e.g. in a 4-plane image, pixels[5] is the Plane 1 of
+   Column 1.
+
+   'bitsPerPixel' is the number of bits per sample, and must be 1.  It's easy
+   to conceive how this subroutine should work with other numbers of bits per
+   sample, but it doesn't.
 -----------------------------------------------------------------------------*/
-    unsigned int const npixels = (bytesPerLine * 8) / bitsPerPixel;
+    unsigned int const pixelCt = bytesPerLine * 8;
+
+    unsigned int bitPlanesIdx;
+        /* Index into 'bitPlanes' of next byte to unpack */
 
     unsigned int  i;
 
@@ -375,23 +391,29 @@ pcxPlanesToPixels(unsigned char * const pixels,
         pm_error("can't handle more than 1 bit per pixel");
 
     /* Clear the pixel buffer - initial value */
-    for (i = 0; i < npixels; ++i)
+    for (i = 0; i < pixelCt; ++i)
         pixels[i] = 0;
 
+    bitPlanesIdx = 0;  /* initial value */
+
     for (i = 0; i < planes; ++i) {
         unsigned int const pixbit = (1 << i);
 
+        unsigned int pixelIdx;
+            /* Index into 'pixels' of next pixel to output */
+
         unsigned int j;
 
-        for (j = 0; j < bytesPerLine; ++j) {
-            unsigned int const bits = bitPlanes[j];
+        for (j = 0, pixelIdx = 0; j < bytesPerLine; ++j) {
+            unsigned int const bits = bitPlanes[bitPlanesIdx++];
 
             unsigned int mask;
-            unsigned int k;
 
-            for (mask = 0x80, k = 0; mask != 0; mask >>= 1, ++k)
+            for (mask = 0x80; mask != 0; mask >>= 1) {
                 if (bits & mask)
-                    pixels[k] |= pixbit;
+                    pixels[pixelIdx] |= pixbit;
+                ++pixelIdx;
+            }
         }
     }
 }