about summary refs log tree commit diff
path: root/converter/other/pnmtopalm/gen_palm_colormap.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/other/pnmtopalm/gen_palm_colormap.c')
-rw-r--r--converter/other/pnmtopalm/gen_palm_colormap.c48
1 files changed, 35 insertions, 13 deletions
diff --git a/converter/other/pnmtopalm/gen_palm_colormap.c b/converter/other/pnmtopalm/gen_palm_colormap.c
index 4b65e631..c7172c6b 100644
--- a/converter/other/pnmtopalm/gen_palm_colormap.c
+++ b/converter/other/pnmtopalm/gen_palm_colormap.c
@@ -1,24 +1,46 @@
 /* gen_palm_colormap.c - generate a ppm file containing the default Palm colormap
  *
- * Bill Janssen  <bill@janssen.org>
+ * Based on an earlier version by Bill Janssen  <bill@janssen.org>
  */
 
-#include "pnm.h"
+#include "ppm.h"
+#include "pm_c_util.h"
 
 #include "palm.h"
 
-int main( int argc, char **argv ) {
+int
+main(int     argc,
+     char ** argv) {
 
-  int i;
-  Color_s current;
-  Colormap default_map = palmcolor_build_default_8bit_colormap ();
+    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);
 
-  printf("P3\n%d 1\n255\n", default_map->ncolors);
-  for (i = 0;  i < default_map->ncolors;  i++) {
-    current = default_map->color_entries[i];
-    printf ("%d %d %d\n", (current & 0xFF0000) >> 16, (current & 0xFF00) >> 8, (current & 0xFF));
-    /* printf ("%x:  %d %d %d\n", (current & 0xFF000000) >> 24, (current & 0xFF0000) >> 16, (current & 0xFF00) >> 8, (current & 0xFF)); */
-  };
-  return 0;
+    ppm_writeppminit(stdout, 256, 1, 255, TRUE);
+
+    for (i = 0; i < defaultMap->ncolors; ++i) {
+        Color_s const current = defaultMap->color_entries[i];
+
+        PPM_ASSIGN(pix,
+                   (current >> 16) & 0xff,
+                   (current >>  8) & 0xff,
+                   (current >>  0) & 0xff);
+
+        ppm_writeppmrow(stdout, &pix, 1, 255, TRUE);
+    }
+
+    /* palmcolor_build_default_8bit_colormap() builds a map of the 231 default
+     * palm colors and 1 extra black pixel. Add another 24 extra black pixels
+     * as per spec. */
+    PPM_ASSIGN(pix, 0, 0, 0);
+    for (i = 0; i < 256 - defaultMap->ncolors; ++i) {
+        ppm_writeppmrow(stdout, &pix, 1, 255, TRUE);
+    }
+
+    return 0;
 }