about summary refs log tree commit diff
path: root/converter/other/pnmtopalm
diff options
context:
space:
mode:
Diffstat (limited to 'converter/other/pnmtopalm')
-rw-r--r--converter/other/pnmtopalm/gen_palm_colormap.c4
-rw-r--r--converter/other/pnmtopalm/palm.h2
-rw-r--r--converter/other/pnmtopalm/palmcolormap.c35
-rw-r--r--converter/other/pnmtopalm/palmtopnm.c59
-rw-r--r--converter/other/pnmtopalm/pnmtopalm.c4
5 files changed, 74 insertions, 30 deletions
diff --git a/converter/other/pnmtopalm/gen_palm_colormap.c b/converter/other/pnmtopalm/gen_palm_colormap.c
index 0f3f8a5f..04555221 100644
--- a/converter/other/pnmtopalm/gen_palm_colormap.c
+++ b/converter/other/pnmtopalm/gen_palm_colormap.c
@@ -15,7 +15,7 @@ main(int     argc,
     Colormap defaultMap;
     unsigned int i;
     pixel pix;
-    
+
     defaultMap = palmcolor_build_default_8bit_colormap();
     qsort (defaultMap->color_entries, defaultMap->ncolors,
            sizeof(Color_s), palmcolor_compare_indices);
@@ -44,3 +44,5 @@ main(int     argc,
     return 0;
 }
 
+
+
diff --git a/converter/other/pnmtopalm/palm.h b/converter/other/pnmtopalm/palm.h
index 0edf9a28..0a2a7cfc 100644
--- a/converter/other/pnmtopalm/palm.h
+++ b/converter/other/pnmtopalm/palm.h
@@ -7,7 +7,7 @@
 #define PALM_INDIRECT_BITMAP        0x1000  /* Palm says internal use only */
 #define PALM_FOR_SCREEN             0x0800  /* Palm says internal use only */
 #define PALM_DIRECT_COLOR_FLAG      0x0400
-#define PALM_INDIRECT_COLORMAP      0x0200  /* Palm says internal use only */ 
+#define PALM_INDIRECT_COLORMAP      0x0200  /* Palm says internal use only */
 #define PALM_NO_DITHER_FLAG         0x0100  /* rather mysterious */
 
 #define PALM_COMPRESSION_SCANLINE   0x00
diff --git a/converter/other/pnmtopalm/palmcolormap.c b/converter/other/pnmtopalm/palmcolormap.c
index 1341ca2b..de3def22 100644
--- a/converter/other/pnmtopalm/palmcolormap.c
+++ b/converter/other/pnmtopalm/palmcolormap.c
@@ -28,7 +28,7 @@ palmcolor_mapEntryColorFmPixel(pixel  const color,
 
     return
         0
-        | (scaleSample(PPM_GETR(color), maxval, newMaxval) << 16) 
+        | (scaleSample(PPM_GETR(color), maxval, newMaxval) << 16)
         | (scaleSample(PPM_GETG(color), maxval, newMaxval) <<  8)
         | (scaleSample(PPM_GETB(color), maxval, newMaxval) <<  0);
 }
@@ -68,6 +68,8 @@ palmcolor_compare_colors(const void * const p1,
         return 0;
 }
 
+
+
 /***********************************************************************
  ***********************************************************************
  ***********************************************************************
@@ -81,7 +83,7 @@ palmcolor_compare_colors(const void * const p1,
 /*
  * The 1bit-2 color system palette for Palm Computing Devices.
  */
-static int PalmPalette1bpp[2][3] = 
+static int PalmPalette1bpp[2][3] =
 {
   { 255, 255, 255}, {   0,   0,   0 }
 };
@@ -89,7 +91,7 @@ static int PalmPalette1bpp[2][3] =
 /*
  * The 2bit-4 color system palette for Palm Computing Devices.
  */
-static int PalmPalette2bpp[4][3] = 
+static int PalmPalette2bpp[4][3] =
 {
   { 255, 255, 255}, { 192, 192, 192}, { 128, 128, 128 }, {   0,   0,   0 }
 };
@@ -97,7 +99,7 @@ static int PalmPalette2bpp[4][3] =
 /*
  * The 4bit-16 color system palette for Palm Computing Devices.
  */
-static int PalmPalette4bpp[16][3] = 
+static int PalmPalette4bpp[16][3] =
 {
   { 255, 255, 255}, { 238, 238, 238 }, { 221, 221, 221 }, { 204, 204, 204 },
   { 187, 187, 187}, { 170, 170, 170 }, { 153, 153, 153 }, { 136, 136, 136 },
@@ -108,7 +110,7 @@ static int PalmPalette4bpp[16][3] =
 /*
  * The 4bit-16 color system palette for Palm Computing Devices.
  */
-static int PalmPalette4bppColor[16][3] = 
+static int PalmPalette4bppColor[16][3] =
 {
   { 255, 255, 255}, { 128, 128, 128 }, { 128,   0,   0 }, { 128, 128,   0 },
   {   0, 128,   0}, {   0, 128, 128 }, {   0,   0, 128 }, { 128,   0, 128 },
@@ -123,12 +125,12 @@ static int PalmPalette4bppColor[16][3] =
  *
  * NOTE:  only the first 231, plus the last one, are valid.
  */
-static int PalmPalette8bpp[256][3] = 
+static int PalmPalette8bpp[256][3] =
 {
-  { 255, 255, 255 }, { 255, 204, 255 }, { 255, 153, 255 }, { 255, 102, 255 }, 
-  { 255,  51, 255 }, { 255,   0, 255 }, { 255, 255, 204 }, { 255, 204, 204 }, 
-  { 255, 153, 204 }, { 255, 102, 204 }, { 255,  51, 204 }, { 255,   0, 204 }, 
-  { 255, 255, 153 }, { 255, 204, 153 }, { 255, 153, 153 }, { 255, 102, 153 }, 
+  { 255, 255, 255 }, { 255, 204, 255 }, { 255, 153, 255 }, { 255, 102, 255 },
+  { 255,  51, 255 }, { 255,   0, 255 }, { 255, 255, 204 }, { 255, 204, 204 },
+  { 255, 153, 204 }, { 255, 102, 204 }, { 255,  51, 204 }, { 255,   0, 204 },
+  { 255, 255, 153 }, { 255, 204, 153 }, { 255, 153, 153 }, { 255, 102, 153 },
   { 255,  51, 153 }, { 255,   0, 153 }, { 204, 255, 255 }, { 204, 204, 255 },
   { 204, 153, 255 }, { 204, 102, 255 }, { 204,  51, 255 }, { 204,   0, 255 },
   { 204, 255, 204 }, { 204, 204, 204 }, { 204, 153, 204 }, { 204, 102, 204 },
@@ -215,7 +217,7 @@ palmcolor_build_default_8bit_colormap(void) {
     cmP->ncolors = 232;
 
     /* now sort the table */
-    qsort(cmP->color_entries, cmP->ncolors, sizeof(ColormapEntry), 
+    qsort(cmP->color_entries, cmP->ncolors, sizeof(ColormapEntry),
           palmcolor_compare_colors);
     return cmP;
 }
@@ -235,7 +237,7 @@ palmcolor_build_custom_8bit_colormap(pixel **     const pixels,
     colormapP->nentries = 256;
     MALLOCARRAY_NOFAIL(colormapP->color_entries, colormapP->nentries);
     colormapP->ncolors = 0;  /* initial value */
-    
+
     for (row = 0; row < rows; ++row) {
         unsigned int col;
         for (col = 0; col < cols; ++col) {
@@ -259,7 +261,7 @@ palmcolor_build_custom_8bit_colormap(pixel **     const pixels,
                     ColormapEntry const newEntry =
                         searchTarget | (colorIndex << 24);
                     colormapP->color_entries[colorIndex] = newEntry;
-                    qsort(colormapP->color_entries, colormapP->ncolors, 
+                    qsort(colormapP->color_entries, colormapP->ncolors,
                           sizeof(ColormapEntry), palmcolor_compare_colors);
                 }
             }
@@ -276,7 +278,7 @@ palmcolor_read_colormap (FILE * const ifP) {
     unsigned short ncolors;
     Colormap * retval;
     int rc;
-    
+
     rc = pm_readbigshort(ifP, (short *) &ncolors);
     if (rc != 0)
         retval = NULL;
@@ -289,7 +291,7 @@ palmcolor_read_colormap (FILE * const ifP) {
         MALLOCVAR_NOFAIL(colormapP);
         colormapP->nentries = ncolors;
         MALLOCARRAY_NOFAIL(colormapP->color_entries, colormapP->nentries);
-        
+
         for (i = 0, error = FALSE;  i < ncolors && !error;  ++i) {
             int rc;
             rc = pm_readbiglong(ifP, &colorentry);
@@ -309,3 +311,6 @@ palmcolor_read_colormap (FILE * const ifP) {
     }
     return retval;
 }
+
+
+
diff --git a/converter/other/pnmtopalm/palmtopnm.c b/converter/other/pnmtopalm/palmtopnm.c
index 445f7839..0e83f5e0 100644
--- a/converter/other/pnmtopalm/palmtopnm.c
+++ b/converter/other/pnmtopalm/palmtopnm.c
@@ -445,6 +445,7 @@ yesno(bool const arg) {
 }
 
 
+
 static void
 reportPalmHeader(struct PalmHeader      const palmHeader,
                  struct DirectColorInfo const directColorInfo) {
@@ -821,27 +822,46 @@ readPackBitsRow16(FILE *          const ifP,
 
         Although the [...] spec is byte-oriented, the 16-bit algorithm is
         identical [to the 8-bit algorithm]: just substitute "word" for "byte".
+
+        A note about the 0x80 control byte value: There are some sources that
+        suggest that 1) no Palm file ever uses that value; and 2) a Palm
+        decoder should treat it as a no-op and other decoders do.  We don't
+        treat it as a no-op because we believe it is far more likely that if
+        someone _does_ put that value in a file, he means "repeat the next
+        word 129 times" than "do nothing."  Plus, it's just simpler and
+        cleaner.  Because of the ambiguity, though, anyone creating a Palm
+        file should avoid 0x80.
     */
     unsigned int j;
 
     for (j = 0;  j < bytesPerRow; ) {
-        char incount;
-        pm_readchar(ifP, &incount);
-        if (incount < 0) {
-            /* How do we handle incount == -128 ? */
-            unsigned int const runlength = (-incount + 1) * 2;
-            unsigned int k;
+        unsigned char controlByte;
+        unsigned int  controlNum;
+
+        pm_readcharu(ifP, &controlByte);
+        controlNum = (unsigned int)controlByte;
+
+        if (controlNum >= 128) {
+            /* It's a run - output multiple copies of the next input word */
+            unsigned int const runlength = (257 - controlNum) * 2;
+
             unsigned short inval;
+
             pm_readlittleshortu(ifP, &inval);
+
             if (j + runlength <= bytesPerRow) {
+                unsigned int k;
                 for (k = 0; k < runlength; k += 2)
                     memcpy(palmrow + j + k, &inval, 2);
             }
             j += runlength;
         } else {
+            /* It's a nonrun - output the next words literally */
             /* We just read the stream of shorts as a stream of chars */
-            unsigned int const nonrunlength = (incount + 1) * 2;
+            unsigned int const nonrunlength = (controlNum + 1) * 2;
+
             unsigned int k;
+
             for (k = 0; (k < nonrunlength) && (j + k <= bytesPerRow); ++k) {
                 unsigned char inval;
                 pm_readcharu(ifP, &inval);
@@ -865,22 +885,32 @@ readPackBitsRow(FILE *          const ifP,
                 unsigned char * const palmrow,
                 unsigned int    const bytesPerRow) {
 
+    /* See comments in 'readPackbitsRow16.  Everything here is the same
+       except with 1-byte words instead of 2-byte words.
+    */
     unsigned int j;
 
     for (j = 0;  j < bytesPerRow; ) {
-        char incount;
-        pm_readchar(ifP, &incount);
-        if (incount < 0) {
-            /* How do we handle incount == -128 ? */
-            unsigned int const runlength = -incount + 1;
+        unsigned char controlByte;
+        unsigned int  controlNum;
+
+        pm_readcharu(ifP, &controlByte);
+        controlNum = controlByte;
+
+        if (controlNum >= 128) {
+            unsigned int const runlength = 257 - controlNum;
+
             unsigned char inval;
+
             pm_readcharu(ifP, &inval);
             if (j + runlength <= bytesPerRow)
                 memset(palmrow + j, inval, runlength);
             j += runlength;
         } else {
-            unsigned int const nonrunlength = incount + 1;
+            unsigned int const nonrunlength = controlNum + 1;
+
             unsigned int k;
+
             for (k = 0; k < nonrunlength && j + k <= bytesPerRow; ++k) {
                 unsigned char inval;
                 pm_readcharu(ifP, &inval);
@@ -1221,3 +1251,6 @@ main(int argc, const char **argv) {
 
     return 0;
 }
+
+
+
diff --git a/converter/other/pnmtopalm/pnmtopalm.c b/converter/other/pnmtopalm/pnmtopalm.c
index 9fc92790..ab82996e 100644
--- a/converter/other/pnmtopalm/pnmtopalm.c
+++ b/converter/other/pnmtopalm/pnmtopalm.c
@@ -816,6 +816,7 @@ computeRawRowNonDirect(const xel *     const xelrow,
 }
 
 
+
 typedef struct {
 /*----------------------------------------------------------------------------
    A buffer to which one can write bytes sequentially.
@@ -1352,3 +1353,6 @@ main( int argc, const char **argv ) {
 
     return 0;
 }
+
+
+