about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2013-02-24 02:03:42 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2013-02-24 02:03:42 +0000
commit788f6229f23cd83933a90118ed362842c5e0aa6b (patch)
treef67af3f2de30be1302ff31e3335c2da8dc72cd91
parent1430075d20c203d87b6fad10121501d1556d5d33 (diff)
downloadnetpbm-mirror-788f6229f23cd83933a90118ed362842c5e0aa6b.tar.gz
netpbm-mirror-788f6229f23cd83933a90118ed362842c5e0aa6b.tar.xz
netpbm-mirror-788f6229f23cd83933a90118ed362842c5e0aa6b.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1858 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/other/pnmtorast.c449
1 files changed, 253 insertions, 196 deletions
diff --git a/converter/other/pnmtorast.c b/converter/other/pnmtorast.c
index 605e815c..dafd2f9e 100644
--- a/converter/other/pnmtorast.c
+++ b/converter/other/pnmtorast.c
@@ -19,103 +19,240 @@
 
 
 static colormap_t *
-alloc_pr_colormap(void) {
-
-    colormap_t* pr_colormapP;
-
-    MALLOCVAR(pr_colormapP);
-    if ( pr_colormapP == NULL )
-        pm_error( "out of memory" );
-    pr_colormapP->type = RMT_EQUAL_RGB;
-    pr_colormapP->length = MAXCOLORS;
-    MALLOCARRAY(pr_colormapP->map[0], MAXCOLORS);
-    MALLOCARRAY(pr_colormapP->map[1], MAXCOLORS);
-    MALLOCARRAY(pr_colormapP->map[2], MAXCOLORS);
-    if ( pr_colormapP->map[0] == NULL || 
-         pr_colormapP->map[1] == NULL ||
-         pr_colormapP->map[2] == NULL )
-        pm_error( "out of memory" );
-
-    return pr_colormapP;
+allocPrColormap(void) {
+
+    colormap_t * prColormapP;
+
+    MALLOCVAR(prColormapP);
+    if (prColormapP == NULL)
+        pm_error("out of memory");
+    prColormapP->type = RMT_EQUAL_RGB;
+    prColormapP->length = MAXCOLORS;
+    MALLOCARRAY(prColormapP->map[0], MAXCOLORS);
+    MALLOCARRAY(prColormapP->map[1], MAXCOLORS);
+    MALLOCARRAY(prColormapP->map[2], MAXCOLORS);
+    if (prColormapP->map[0] == NULL || 
+        prColormapP->map[1] == NULL ||
+        prColormapP->map[2] == NULL)
+        pm_error("out of memory");
+
+    return prColormapP;
 }
 
 
 
-static colormap_t*
-make_pr_colormap(colorhist_vector const chv,
-                 int              const colors) {
+static colormap_t *
+makePrColormap(colorhist_vector const chv,
+               unsigned int     const colors) {
 
-    colormap_t* pr_colormapP;
-    int i;
+    colormap_t * prColormapP;
+    unsigned int i;
 
-    pr_colormapP = alloc_pr_colormap( );
+    prColormapP = allocPrColormap();
 
-    for ( i = 0; i < colors; ++i )
-    {
-        pr_colormapP->map[0][i] = PPM_GETR( chv[i].color );
-        pr_colormapP->map[1][i] = PPM_GETG( chv[i].color );
-        pr_colormapP->map[2][i] = PPM_GETB( chv[i].color );
+    for (i = 0; i < colors; ++i) {
+        prColormapP->map[0][i] = PPM_GETR(chv[i].color);
+        prColormapP->map[1][i] = PPM_GETG(chv[i].color);
+        prColormapP->map[2][i] = PPM_GETB(chv[i].color);
     }
-    for ( ; i < MAXCOLORS; ++i )
-        pr_colormapP->map[0][i] = pr_colormapP->map[1][i] =
-            pr_colormapP->map[2][i] = 0;
-
-    return pr_colormapP;
+    for ( ; i < MAXCOLORS; ++i) {
+        prColormapP->map[0][i] = 0;
+        prColormapP->map[1][i] = 0;
+        prColormapP->map[2][i] = 0;
+    }
+    return prColormapP;
 }
 
 
 
 static colormap_t *
-make_gray_pr_colormap(void) {
+makeGrayPrColormap(void) {
 
-    colormap_t* pr_colormapP;
-    int i;
+    colormap_t * prColormapP;
+    unsigned int i;
 
-    pr_colormapP = alloc_pr_colormap( );
+    prColormapP = allocPrColormap();
 
-    for ( i = 0; i < MAXCOLORS; ++i )
-    {
-        pr_colormapP->map[0][i] = i;
-        pr_colormapP->map[1][i] = i;
-        pr_colormapP->map[2][i] = i;
+    for (i = 0; i < MAXCOLORS; ++i) {
+        prColormapP->map[0][i] = i;
+        prColormapP->map[1][i] = i;
+        prColormapP->map[2][i] = i;
+    }
+
+    return prColormapP;
+}
+
+
+
+static void
+doRowDepth1(const xel *     const xelrow,
+            unsigned char * const rastRow,
+            unsigned int    const cols,
+            int             const format,
+            xelval          const maxval,
+            colorhash_table const cht) {
+                
+    unsigned int col;
+    int bitcount;
+    unsigned char * byteP;
+
+    byteP = rastRow;
+
+    *byteP = 0;
+    bitcount = 7;
+
+    for (col = 0; col < cols; ++col) {
+        switch (PNM_FORMAT_TYPE(format)) {
+        case PPM_TYPE: {
+            xel adjustedXel;
+            int color;
+            if (maxval != 255)
+                PPM_DEPTH(adjustedXel, xelrow[col], maxval, 255 );
+            color = ppm_lookupcolor(cht, &adjustedXel);
+            if (color == -1)
+                pm_error("color not found?!?  "
+                         "col=%u  r=%u g=%u b=%u",
+                         col,
+                         PPM_GETR(adjustedXel),
+                         PPM_GETG(adjustedXel),
+                         PPM_GETB(adjustedXel));
+            if (color)
+                *byteP |= 1 << bitcount;
+        } break;
+
+        default: {
+            int const color = PNM_GET1(xelrow[col]);
+            if (!color)
+                *byteP |= 1 << bitcount;
+            break;
+        }
+        }
+        --bitcount;
+        if (bitcount < 0) {
+            ++byteP;
+            *byteP = 0;
+            bitcount = 7;
+        }
     }
+}
+
+
+
+static void
+doRowDepth8(const xel *     const xelrow,
+            unsigned char * const rastRow,
+            unsigned int    const cols,
+            int             const format,
+            xelval          const maxval,
+            colorhash_table const cht) {
+
+    unsigned int col;
+    unsigned char * byteP;
 
-    return pr_colormapP;
+    byteP = rastRow;
+
+    for (col = 0; col < cols; ++col) {
+        int color;
+
+        switch (PNM_FORMAT_TYPE(format)) {
+        case PPM_TYPE: {
+            xel adjustedXel;
+
+            if (maxval == 255)
+                adjustedXel = xelrow[col];
+            else
+                PPM_DEPTH(adjustedXel, xelrow[col], maxval, 255);
+
+            color = ppm_lookupcolor(cht, &adjustedXel);
+            if (color == -1)
+                pm_error("color not found?!?  "
+                         "col=%u  r=%u g=%u b=%u",
+                         col,
+                         PPM_GETR(adjustedXel),
+                         PPM_GETG(adjustedXel),
+                         PPM_GETB(adjustedXel));
+        } break;
+
+        case PGM_TYPE: {
+            int const rawColor = PNM_GET1(xelrow[col]);
+
+            color = maxval == 255 ? rawColor : rawColor * 255 / maxval;
+
+        } break;
+
+        default:
+            color = PNM_GET1(xelrow[col]);
+        }
+        *byteP++ = color;
+    }
+}
+
+
+
+
+static void
+doRowDepth24(const xel *     const xelrow,
+             unsigned char * const rastRow,
+             unsigned int    const cols,
+             int             const format,
+             xelval          const maxval) {
+
+    /* Since depth is 24, we do NOT have a valid cht. */
+
+    unsigned char * byteP;
+    unsigned int col;
+
+    byteP = rastRow;
+
+    for (col = 0; col < cols; ++col) {
+        xel adjustedXel;
+
+        if (maxval == 255)
+            adjustedXel = xelrow[col];
+        else
+            PPM_DEPTH(adjustedXel, xelrow[col], maxval, 255);
+
+        *byteP++ = PPM_GETB(adjustedXel);
+        *byteP++ = PPM_GETG(adjustedXel);
+        *byteP++ = PPM_GETR(adjustedXel);
+    }
 }
 
 
 
 int
-main(int argc, char ** argv) {
+main(int argc, const char ** argv) {
 
-    FILE* ifp;
-    xel** xels;
-    xel* xelrow;
+    FILE * ifP;
+    xel ** xels;
+    xel * xelrow;
     xel p;
-    register xel* xP;
     colorhist_vector chv;
     colorhash_table cht;
-    colormap_t* pr_colormapP;
-    int argn, pr_type, rows, cols, format, i;
-    int depth, colors, linesize, row;
-    register int col, bitcount;
+    colormap_t * prColormapP;
+    int argn;
+    int prType;
+    int rows, cols;
+    int format;
+    unsigned int depth;
+    int colorCt;
+    unsigned int row;
     xelval maxval;
-    struct pixrect* pr;
-    unsigned char* data;
-    register unsigned char* byteP;
-    const char* const usage = "[-standard|-rle] [pnmfile]";
+    struct pixrect * prP;
+    unsigned char * rastRow;
+    const char * const usage = "[-standard|-rle] [pnmfile]";
 
-    pnm_init( &argc, argv );
+    pm_proginit(&argc, argv);
 
     argn = 1;
-    pr_type = RT_BYTE_ENCODED;
+    prType = RT_BYTE_ENCODED;
 
     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
     {
         if ( pm_keymatch( argv[argn], "-standard", 2 ) )
-            pr_type = RT_STANDARD;
+            prType = RT_STANDARD;
         else if ( pm_keymatch( argv[argn], "-rle", 2 ) )
-            pr_type = RT_BYTE_ENCODED;
+            prType = RT_BYTE_ENCODED;
         else
             pm_usage( usage );
         ++argn;
@@ -123,191 +260,111 @@ main(int argc, char ** argv) {
 
     if ( argn != argc )
     {
-        ifp = pm_openr( argv[argn] );
+        ifP = pm_openr( argv[argn] );
         ++argn;
     }
     else
-        ifp = stdin;
+        ifP = stdin;
 
     if ( argn != argc )
         pm_usage( usage );
 
-    xels = pnm_readpnm( ifp, &cols, &rows, &maxval, &format );
+    xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format);
 
-    pm_close( ifp );
+    pm_close(ifP);
 
     /* Figure out the proper depth and colormap. */
-    switch ( PNM_FORMAT_TYPE(format) )
-    {
+    switch (PNM_FORMAT_TYPE(format)) {
     case PPM_TYPE:
-        pm_message( "computing colormap..." );
-        chv = ppm_computecolorhist( xels, cols, rows, MAXCOLORS, &colors );
-        if ( chv == (colorhist_vector) 0 )
-        {
+        pm_message("computing colormap...");
+        chv = ppm_computecolorhist(xels, cols, rows, MAXCOLORS, &colorCt);
+        if (!chv) {
             pm_message(
-                "Too many colors - proceeding to write a 24-bit non-mapped" );
+                "Too many colors - proceeding to write a 24-bit non-mapped");
             pm_message(
                 "rasterfile.  If you want 8 bits, try doing a 'pnmquant %d'.",
-                MAXCOLORS );
+                MAXCOLORS);
             depth = 24;
-            pr_type = RT_STANDARD;
-            pr_colormapP = (colormap_t*) 0;
-        }
-        else
-        {
-            pm_message( "%d colors found", colors );
-
-            if ( maxval != 255 )
-                for ( i = 0; i < colors; ++i )
-                    PPM_DEPTH( chv[i].color, chv[i].color, maxval, 255 );
-
+            prType = RT_STANDARD;
+            prColormapP = NULL;
+        } else {
+            pm_message("%u colors found", colorCt);
+
+            if (maxval != 255) {
+                unsigned int i;
+                for (i = 0; i < colorCt; ++i)
+                    PPM_DEPTH(chv[i].color, chv[i].color, maxval, 255);
+            }
             /* Force white to slot 0 and black to slot 1, if possible. */
-            PPM_ASSIGN( p, 255, 255, 255 );
-            ppm_addtocolorhist( chv, &colors, MAXCOLORS, &p, 0, 0 );
-            PPM_ASSIGN( p, 0, 0, 0 );
-            ppm_addtocolorhist( chv, &colors, MAXCOLORS, &p, 0, 1 );
-
-            if ( colors == 2 )
-            {
-                /* Monochrome. */
+            PPM_ASSIGN(p, 255, 255, 255);
+            ppm_addtocolorhist(chv, &colorCt, MAXCOLORS, &p, 0, 0);
+            PPM_ASSIGN(p, 0, 0, 0);
+            ppm_addtocolorhist(chv, &colorCt, MAXCOLORS, &p, 0, 1);
+
+            if (colorCt == 2) {
+                /* Monochrome */
                 depth = 1;
-                pr_colormapP = (colormap_t*) 0;
-            }
-            else
-            {
+                prColormapP = NULL;
+            } else {
                 /* Turn the ppm colormap into the appropriate Sun colormap. */
                 depth = 8;
-                pr_colormapP = make_pr_colormap( chv, colors );
+                prColormapP = makePrColormap(chv, colorCt);
             }
-            cht = ppm_colorhisttocolorhash( chv, colors );
-            ppm_freecolorhist( chv );
+            cht = ppm_colorhisttocolorhash(chv, colorCt);
+            ppm_freecolorhist(chv);
         }
 
         break;
 
     case PGM_TYPE:
         depth = 8;
-        pr_colormapP = make_gray_pr_colormap( );
+        prColormapP = makeGrayPrColormap();
         break;
 
     default:
         depth = 1;
-        pr_colormapP = (colormap_t*) 0;
+        prColormapP = NULL;
         break;
     }
 
-    if ( maxval > 255 && depth != 1 )
+    if (maxval > 255 && depth != 1)
         pm_message(
-            "maxval is not 255 - automatically rescaling colors" );
+            "maxval is not 255 - automatically rescaling colors");
     
     /* Allocate space for the Sun-format image. */
-    if ( (pr = mem_create(cols, rows, depth)) == (struct pixrect*) 0 )
-        pm_error( "unable to create new pixrect" );
-    data = ( (struct mpr_data*) pr->pr_data )->md_image;
-    linesize = ( (struct mpr_data*) pr->pr_data )->md_linebytes;
+    prP = mem_create(cols, rows, depth);
+    if (!prP)
+        pm_error("unable to create new pixrect");
 
     /* And compute the Sun image.  The variables at this point are:
-    **   cht is null or not
-    **   depth is 1, 8, or 24
+       cht is null or not.  depth is 1, 8, or 24
     */
-    for ( row = 0; row < rows; ++row )
-    {
+    for (row = 0, rastRow = prP->pr_data->md_image; row < rows; ++row) {
         xelrow = xels[row];
-        byteP = data;
-        switch ( depth )
-        {
+        switch (depth) {
         case 1:
-            *byteP = 0;
-            bitcount = 7;
-            for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
-            {
-                register int color;
-
-                switch ( PNM_FORMAT_TYPE(format) )
-                {
-                case PPM_TYPE:
-                    if ( maxval != 255 )
-                        PPM_DEPTH( *xP, *xP, maxval, 255 );
-                    color = ppm_lookupcolor( cht, xP );
-                    if ( color == -1 )
-                        pm_error(
-                            "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
-                            row, col, PPM_GETR(*xP), PPM_GETG(*xP),
-                            PPM_GETB(*xP) );
-                    if ( color )
-                        *byteP |= 1 << bitcount;
-                    break;
-
-                default:
-                    color = PNM_GET1( *xP );
-                    if ( ! color )
-                        *byteP |= 1 << bitcount;
-                    break;
-                }
-                --bitcount;
-                if ( bitcount < 0 )
-                {
-                    ++byteP;
-                    *byteP = 0;
-                    bitcount = 7;
-                }
-            }
+            doRowDepth1(xelrow, rastRow, cols, format, maxval, cht);
             break;
-
         case 8:
-            for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
-            {
-                register int color;
-
-                switch ( PNM_FORMAT_TYPE(format) )
-                {
-                case PPM_TYPE:
-                    if ( maxval != 255 )
-                        PPM_DEPTH( *xP, *xP, maxval, 255 );
-                    color = ppm_lookupcolor( cht, xP );
-                    if ( color == -1 )
-                        pm_error(
-                            "color not found?!?  row=%d col=%d  r=%d g=%d b=%d",
-                            row, col, PPM_GETR(*xP), PPM_GETG(*xP),
-                            PPM_GETB(*xP) );
-                    break;
-
-                case PGM_TYPE:
-                    color = PNM_GET1( *xP );
-                    if ( maxval != 255 )
-                        color = color * 255 / maxval;
-                    break;
-
-                default:
-                    color = PNM_GET1( *xP );
-                }
-                *byteP++ = color;
-            }
+            doRowDepth8(xelrow, rastRow, cols, format, maxval, cht);
             break;
-
         case 24:
-            /* If depth is 24, we do NOT have a valid cht. */
-            for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
-            {
-                if ( maxval != 255 )
-                    PPM_DEPTH( *xP, *xP, maxval, 255 );
-                *byteP++ = PPM_GETB( *xP );
-                *byteP++ = PPM_GETG( *xP );
-                *byteP++ = PPM_GETR( *xP );
-            }
+            doRowDepth24(xelrow, rastRow, cols, format, maxval);
             break;
-
         default:
-            pm_error( "can't happen" );
+            pm_error("INTERNAL ERROR: impossible depth %u", depth);
         }
-        data += linesize;
+        rastRow += prP->pr_data->md_linebytes;
     }
-    pnm_freearray( xels, rows );
+    pnm_freearray(xels, rows);
 
-    /* Finally, write the sucker out. */
-    if ( pr_dump( pr, stdout, pr_colormapP, pr_type, 0 ) == PIX_ERR )
-        pm_error( "error writing rasterfile" );
+    {
+        int rc;
 
-    exit( 0 );
+        rc = pr_dump(prP, stdout, prColormapP, prType, 0);
+        if (rc == PIX_ERR )
+            pm_error("error writing rasterfile");
+    }
+    return 0;
 }