diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2015-04-11 16:23:08 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2015-04-11 16:23:08 +0000 |
commit | 858494ad246a1d1ce0426e24b0e6f4ae739c7564 (patch) | |
tree | 65aee5d9e842fa71d34dfc820e67b40d732364e6 | |
parent | cc21149495ad742ddf7e4dd2dc3b59269429abbe (diff) | |
download | netpbm-mirror-858494ad246a1d1ce0426e24b0e6f4ae739c7564.tar.gz netpbm-mirror-858494ad246a1d1ce0426e24b0e6f4ae739c7564.tar.xz netpbm-mirror-858494ad246a1d1ce0426e24b0e6f4ae739c7564.zip |
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2460 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r-- | converter/pbm/pbmtozinc.c | 234 |
1 files changed, 160 insertions, 74 deletions
diff --git a/converter/pbm/pbmtozinc.c b/converter/pbm/pbmtozinc.c index ba649b1d..0c65553c 100644 --- a/converter/pbm/pbmtozinc.c +++ b/converter/pbm/pbmtozinc.c @@ -1,4 +1,4 @@ -/* pbmtozinc.c - read a portable bitmap and produce an bitmap file +/* pbmtozinc.c - read a PBM image and produce a bitmap file ** in the format used by the Zinc Interface Library (v1.0) ** November 1990. ** @@ -21,113 +21,199 @@ #include <stdio.h> #include <string.h> +#include "mallocvar.h" #include "nstring.h" #include "pbm.h" -static int itemsperline; -static int bitsperitem; -static int item; -static int firstitem; +static void +parseCommandLine(int const argc, + const char ** const argv, + const char ** const inputFileNameP) { + + if (argc-1 > 0) { + *inputFileNameP = argv[1]; + + if (argc-1 > 1) + pm_error("To many arguments: %u. " + "The only possible argument is the " + "name of the input file", argc-1); + } else + *inputFileNameP = "-"; +} + + + +static const char * +imageName(const char * const inputFileName) { +/*---------------------------------------------------------------------------- + The image name to put in the Zinc file, based on the input file name + 'inputFileName' ("-" to indicate Standard Input). + + Result is newly malloc'ed space that Caller must free. +-----------------------------------------------------------------------------*/ + const char * retval; + + if (streq(inputFileName, "-")) + pm_asprintf(&retval, "noname"); + else { + char * nameBuf; + char * cp; + + MALLOCARRAY_NOFAIL(nameBuf, strlen(inputFileName + 1)); + + strcpy(nameBuf, inputFileName); + + cp = strchr(nameBuf, '.' ); + if (cp) + *cp = '\0'; + + retval = nameBuf; + } + return retval; +} + + + static const char * const hexchar = "084c2a6e195d3b7f"; +typedef struct { + unsigned int itemsperline; + unsigned int bitsperitem; + unsigned int item; + unsigned int firstitem; +} Packer; + + + +static void +packer_init(Packer * const packerP) { + + packerP->itemsperline = 0; + packerP->bitsperitem = 0; + packerP->item = 0; + packerP->firstitem = 1; +} + static void -putitem() { - if ( firstitem ) - firstitem = 0; +packer_putitem(Packer * const packerP) { + + if (packerP->firstitem) + packerP->firstitem = 0; else - putchar( ',' ); + putchar(','); + + if (packerP->itemsperline == 11) { + putchar('\n'); + packerP->itemsperline = 0; + } + if (packerP->itemsperline == 0) + putchar(' '); - if ( itemsperline == 11 ) { - putchar( '\n' ); - itemsperline = 0; - } - if ( itemsperline == 0 ) - putchar( ' ' ); + ++packerP->itemsperline; - ++itemsperline; putchar('0'); putchar('x'); - putchar(hexchar[item & 15]); - putchar(hexchar[(item >> 4) & 15]); - putchar(hexchar[(item >> 8) & 15]); - putchar(hexchar[item >> 12]); - bitsperitem = 0; - item = 0; + putchar(hexchar[(packerP->item >> 0) & 0xF]); + putchar(hexchar[(packerP->item >> 4) & 0xF]); + putchar(hexchar[(packerP->item >> 8) & 0xF]); + putchar(hexchar[(packerP->item >> 12)]); + + packerP->bitsperitem = 0; + packerP->item = 0; } static void -putbit(const bit b) { - if ( bitsperitem == 16 ) - putitem(); - if ( (b) == PBM_BLACK ) - item += 1 << bitsperitem; - ++bitsperitem; +packer_putbit(Packer * const packerP, + bit const b) { + + if (packerP->bitsperitem == 16) + packer_putitem(packerP); + + if (b == PBM_BLACK) + packerP->item += 1 << packerP->bitsperitem; + + ++packerP->bitsperitem; } -int -main(int argc, char * argv[]) { - FILE* ifp; - bit* bitrow; - bit* bP; - int rows, cols, format, padright, row; - int col; - char name[100]; - char* cp; +static void +packer_term(Packer * const packerP) { - pbm_init( &argc, argv ); + if (packerP->bitsperitem > 0 ) + packer_putitem(packerP); +} - if ( argc > 2 ) - pm_usage( "[pbmfile]" ); - if ( argc == 2 ) { - ifp = pm_openr( argv[1] ); - strcpy( name, argv[1] ); - if ( streq( name, "-" ) ) - strcpy( name, "noname" ); - if ( ( cp = strchr( name, '.' ) ) != 0 ) - *cp = '\0'; - } - else { - ifp = stdin; - strcpy( name, "noname" ); - } +static void +writeRaster(FILE * const ifP, + unsigned int const rows, + unsigned int const cols, + int const format) { + + unsigned int const padright = ((cols + 15) / 16) * 16 - cols; + /* Padding to round cols up to the nearest multiple of 16. */ + + Packer packer; + bit * bitrow; + unsigned int row; + + bitrow = pbm_allocrow(cols); + + packer_init(&packer); + + for (row = 0; row < rows; ++row) { + unsigned int col; + unsigned int i; + pbm_readpbmrow(ifP, bitrow, cols, format); + for (col = 0; col < cols; ++col) + packer_putbit(&packer, bitrow[col]); + for (i = 0; i < padright; ++i) + packer_putbit(&packer, 0); + } + + packer_term(&packer); + + pbm_freerow(bitrow); +} - pbm_readpbminit( ifp, &cols, &rows, &format ); - bitrow = pbm_allocrow( cols ); - /* Compute padding to round cols up to the nearest multiple of 16. */ - padright = ( ( cols + 15 ) / 16 ) * 16 - cols; - printf( "USHORT %s[] = {\n",name); - printf( " %d\n", cols ); - printf( " %d\n", rows ); +int +main(int argc, const char * argv[]) { - itemsperline = 0; - bitsperitem = 0; - item = 0; - firstitem = 1; + const char * inputFileName; + FILE * ifP; + int rows, cols; + int format; + const char * name; + pm_proginit(&argc, argv); - for ( row = 0; row < rows; ++row ) { - pbm_readpbmrow( ifp, bitrow, cols, format ); - for ( col = 0, bP = bitrow; col < cols; ++col, ++bP ) - putbit( *bP ); - for ( col = 0; col < padright; ++col ) - putbit( 0 ); - } + parseCommandLine(argc, argv, &inputFileName); + + ifP = pm_openr(inputFileName); + + name = imageName(inputFileName); + + pbm_readpbminit(ifP, &cols, &rows, &format); + + printf("USHORT %s[] = {\n", name); + printf(" %d\n", cols); + printf(" %d\n", rows); + + writeRaster(ifP, rows, cols, format); + + printf("};\n"); - pm_close( ifp ); + pm_close(ifP); - if ( bitsperitem > 0 ) - putitem(); - printf( "};\n" ); + pm_strfree(name); return 0; } |