From b6de74e925b6665c8996e7ad63eef3aae3f80125 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 29 Dec 2007 18:56:33 +0000 Subject: Deal with zero width/height git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@506 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- editor/pnmtile.c | 124 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 38 deletions(-) (limited to 'editor/pnmtile.c') diff --git a/editor/pnmtile.c b/editor/pnmtile.c index 96bf658d..4dcdfbfe 100644 --- a/editor/pnmtile.c +++ b/editor/pnmtile.c @@ -10,54 +10,102 @@ ** implied warranty. */ +#include "mallocvar.h" +#include "shhopt.h" #include "pnm.h" -int -main( argc, argv ) - int argc; - char* argv[]; - { - FILE* ifp; - xel** xels; - register xel* xelrow; - xelval maxval; - int rows, cols, format, width, height, row, col; - const char* const usage = "width height [pnmfile]"; - pnm_init( &argc, argv ); - if ( argc < 3 || argc > 4 ) - pm_usage( usage ); +struct cmdlineInfo { + /* All the information the user supplied in the command line, + in a form easy for the program to use. + */ + const char * inputFileName; + unsigned int width; + unsigned int height; +}; + + + +static void +parseCommandLine(int argc, const char ** argv, + struct cmdlineInfo * const cmdlineP) { +/*---------------------------------------------------------------------------- + Note that the file spec array we return is stored in the storage that + was passed to us as the argv array. +-----------------------------------------------------------------------------*/ + 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 OPTENT3 */ + + opt.opt_table = option_def; + opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ + opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ + + optParseOptions3(&argc, (char**)argv, opt, sizeof opt, 0); + /* Uses and sets argc, argv, and some of *cmdlineP and others. */ - if ( sscanf( argv[1], "%d", &width ) != 1 ) - pm_usage( usage ); - if ( sscanf( argv[2], "%d", &height ) != 1 ) - pm_usage( usage ); + if (argc-1 < 2) + pm_error("You must specify at least two parameters: " + "width and height. You specified %u", + argc-1); + else { + cmdlineP->width = pm_parse_width(argv[1]); + cmdlineP->height = pm_parse_height(argv[2]); - if ( width < 1 ) - pm_error( "width is less than 1" ); - if ( height < 1 ) - pm_error( "height is less than 1" ); + if (argc-1 > 2) { + cmdlineP->inputFileName = argv[3]; - if ( argc == 4 ) - ifp = pm_openr( argv[3] ); - else - ifp = stdin; + if (argc-1 > 3) + pm_error("There are at most three arguments: " + "width, height, file name. You specified %u", + argc-1); + } else + cmdlineP->inputFileName = "-"; + } +} + + + +int +main(int argc, const char ** argv) { - xels = pnm_readpnm( ifp, &cols, &rows, &maxval, &format ); - pm_close( ifp ); + struct cmdlineInfo cmdline; + FILE * ifP; + xel ** xels; + xel * xelrow; + xelval maxval; + int rows, cols; + int format; + unsigned int row; + + pm_proginit(&argc, argv); + + parseCommandLine(argc, argv, &cmdline); - xelrow = pnm_allocrow( width ); + ifP = pm_openr(cmdline.inputFileName); - pnm_writepnminit( stdout, width, height, maxval, format, 0 ); - for ( row = 0; row < height; ++row ) - { - for ( col = 0; col < width; ++col ) - xelrow[col] = xels[row % rows][col % cols]; - pnm_writepnmrow( stdout, xelrow, width, maxval, format, 0 ); - } + xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format); + pm_close(ifP); - pm_close( stdout ); + xelrow = pnm_allocrow(cmdline.width); - exit( 0 ); + pnm_writepnminit(stdout, cmdline.width, cmdline.height, maxval, format, 0); + for (row = 0; row < cmdline.height; ++row) { + unsigned int col; + for (col = 0; col < cmdline.width; ++col) + xelrow[col] = xels[row % rows][col % cols]; + pnm_writepnmrow(stdout, xelrow, cmdline.width, maxval, format, 0); } + + pm_close(stdout); + + return 0; +} -- cgit 1.4.1