about summary refs log tree commit diff
path: root/converter/ppm/ppmtoterm.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-04-09 02:47:20 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-04-09 02:47:20 +0000
commit128ba3d3188187008e6bfb0a4c8e6c4b810c1065 (patch)
treeea648788621ad89f396321db6010dd6e9f74ffd6 /converter/ppm/ppmtoterm.c
parent7bee81b77d9d3aa55382e652dbe3502f2fd0d9f6 (diff)
downloadnetpbm-mirror-128ba3d3188187008e6bfb0a4c8e6c4b810c1065.tar.gz
netpbm-mirror-128ba3d3188187008e6bfb0a4c8e6c4b810c1065.tar.xz
netpbm-mirror-128ba3d3188187008e6bfb0a4c8e6c4b810c1065.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1182 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/ppm/ppmtoterm.c')
-rw-r--r--converter/ppm/ppmtoterm.c138
1 files changed, 70 insertions, 68 deletions
diff --git a/converter/ppm/ppmtoterm.c b/converter/ppm/ppmtoterm.c
index 81df614e..a6c6d844 100644
--- a/converter/ppm/ppmtoterm.c
+++ b/converter/ppm/ppmtoterm.c
@@ -22,29 +22,33 @@
 #include <string.h>
 
 #include "pm_c_util.h"
-#include "ppm.h"
+#include "mallocvar.h"
 #include "shhopt.h"
+#include "ppm.h"
 
 
 struct cmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char *inputFilespec;  /* Filespec of input file */
+    const char * inputFileName;  /* Name of input file */
     unsigned int verbose;
 };
 
 
 
 static void
-parseCommandLine(int argc, char **argv,
-                 struct cmdlineInfo *cmdlineP) {
-    optEntry *option_def = malloc(100*sizeof(optEntry));
+parseCommandLine(int argc, const char ** argv,
+                 struct cmdlineInfo * const cmdlineP) {
+
+    optEntry * option_def;
         /* Instructions to OptParseOptions3 on how to parse our options */
     optStruct3 opt;
 
     unsigned int option_def_index;
 
+    MALLOCARRAY_NOFAIL(option_def, 100);
+
     option_def_index = 0;   /* incremented by OPTENTRY */
     OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0);
 
@@ -52,117 +56,115 @@ parseCommandLine(int argc, char **argv,
     opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
     opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */
 
-    optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
+    optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
         /* Uses and sets argc, argv, and some of *cmdlineP and others. */
 
-    switch (argc-1) {
-    case 0:
-        cmdlineP->inputFilespec = "-";
-        break;
-    case 1:
-        cmdlineP->inputFilespec = argv[1];
-        break;
-    case 2:
-        break;
+    if (argc-1 < 1)
+        cmdlineP->inputFileName = "-";
+    else {
+        cmdlineP->inputFileName = argv[1];
+
+        if (argc-1 > 1)
+            pm_error("Too many arguments: %u.  The only possible argument "
+                     "is the input file name", argc-1);
     }
 }
 
 
+
 #define ESC         "\x1B\x5B"
 #define NUM_COLORS      128
 #define MAX_ANSI_STR_LEN    16
 
 
-static int __inline__ sqr(const int x) {
-    return x*x;
-}
 
-/*
-    Generates some sort of color palette mixing the available
-    colors as different values of background, foreground & brightness.
-*/
-static int 
-generate_palette(unsigned char rgb[NUM_COLORS][3], 
-                 char ansi_code[NUM_COLORS][MAX_ANSI_STR_LEN]) {
-    int code, col=0, cd2=0;
+static void
+generatePalette(unsigned char        rgb[NUM_COLORS][3], 
+                char                 ansiCode[NUM_COLORS][MAX_ANSI_STR_LEN],
+                unsigned int * const paletteSizeP) {
+/*----------------------------------------------------------------------------
+  Generate some sort of color palette mixing the available colors as different
+  values of background, foreground & brightness.
+-----------------------------------------------------------------------------*/
+    unsigned int code;
+    unsigned int col;
+    unsigned int cd2;
     
     memset((void *)rgb, 0, NUM_COLORS*3);
-    memset((void *)ansi_code, 0, NUM_COLORS*MAX_ANSI_STR_LEN);
+    memset((void *)ansiCode, 0, NUM_COLORS*MAX_ANSI_STR_LEN);
 
-    for(col=cd2=0; cd2<8; cd2++) {
+    for( col = cd2 =0; cd2 < 8; ++cd2) {
         unsigned int b;
-        for(b=0;b<2;b++) {
-            for(code=0; code<8; code++) {
+        for (b = 0; b < 2; ++b) {
+            for (code = 0; code < 8; ++code) {
                 unsigned int c;
-                for(c=0;c<3;c++) {
-                    if(code&(1<<c)) {
-                        rgb[col][c]=(192|(b?63:0));
-                    }
-                    if(cd2&(1<<c)) {
-                        rgb[col][c]|=(128);
-                    }
+                for (c = 0; c < 3; ++c) {
+                    if ((code & (0x1 << c)) != 0)
+                        rgb[col][c] = (192 | (b ? 63 : 0));
+                    if ((cd2 & (0x1 << c)) != 0)
+                        rgb[col][c] |= 0x80;
                 }
-                sprintf(ansi_code[col],
+                sprintf(ansiCode[col],
                         ESC"%dm"ESC"3%dm"ESC"4%dm",
                         b, code, cd2);
-                col++;
+                ++col;
             }
         }
     }
-    return col;
+    *paletteSizeP = col;
 }
 
 
 
-int main(int argc, char **argv)
-{
-    FILE            *ifp;
-    pixel           **pixels;
-    int             rows, row, cols, col,
-                    pal_len, i;
+int
+main(int argc, const char ** argv) {
+
+    FILE            * ifP;
+    pixel           ** pixels;
+    int             rows, cols;
+    unsigned int    row;
+    unsigned int    palLen;
     pixval          maxval;
-    struct cmdlineInfo
-                    cmdline;
+    struct cmdlineInfo cmdline;
     unsigned char   rgb[NUM_COLORS][3];
-    char            ansi_code[NUM_COLORS][MAX_ANSI_STR_LEN];
+    char            ansiCode[NUM_COLORS][MAX_ANSI_STR_LEN];
 
-    
-    ppm_init(&argc, argv);    
+    pm_proginit(&argc, argv);    
 
     parseCommandLine(argc, argv, &cmdline);
 
-    ifp = pm_openr(cmdline.inputFilespec);
+    ifP = pm_openr(cmdline.inputFileName);
     
-    pixels = ppm_readppm(ifp, &cols, &rows, &maxval);
+    pixels = ppm_readppm(ifP, &cols, &rows, &maxval);
 
-    pm_close(ifp);
+    pm_close(ifP);
         
-    pal_len=generate_palette(rgb, ansi_code);
+    generatePalette(rgb, ansiCode, &palLen);
     
     for (row = 0; row < rows; ++row) {
-        for (col = 0; col < cols; col++) {
+        unsigned int col;
+        for (col = 0; col < cols; ++col) {
             pixval const r=(int)PPM_GETR(pixels[row][col])*255/maxval;
             pixval const g=(int)PPM_GETG(pixels[row][col])*255/maxval;
             pixval const b=(int)PPM_GETB(pixels[row][col])*255/maxval;
             int val, dist;
+            unsigned int i;
             
-            /*
-            The following loop calculates the index that
-            corresponds to the minimum color distance
-            between the given RGB values and the values
-            available in the palette.
+            /* The following loop calculates the index that corresponds to the
+               minimum color distance between the given RGB values and the
+               values available in the palette.
             */
-            for(i=0, dist=sqr(255)*3, val=0; i<pal_len; i++) {
+            for (i = 0, dist = SQR(255)*3, val = 0; i < palLen; ++i) {
                 pixval const pr=rgb[i][0];
                 pixval const pg=rgb[i][1];
                 pixval const pb=rgb[i][2];
-                unsigned int j;
-                if( (j=sqr(r-pr)+sqr(b-pb)+sqr(g-pg))<dist ) {
-                    dist=j;
-                    val=i;
+                unsigned int const j = SQR(r-pr) + SQR(b-pb) + SQR(g-pg);
+                if (j  < dist) {
+                    dist = j;
+                    val = i;
                 }
             }
-            printf("%s%c", ansi_code[val],0xB1);
+            printf("%s%c", ansiCode[val], 0xB1);
         }
         printf(ESC"\x30m\n");
     }
@@ -170,5 +172,5 @@ int main(int argc, char **argv)
 
     ppm_freearray(pixels, rows);
     
-    exit(0);
+    return 0;
 }