/* parse an arg with a long value. name should be the full key name, preceded by '-' */ static Err longArg( long *arg, const char *name, int size, long lo, long hi, int *argn, int argc, char **argv ) { char extra ; int count ; if ( pm_keymatch( argv[*argn], name, size ) ) { if ( ++(*argn) == argc ) { fprintf( stderr, "no value for %s\n", name ) ; return ERR_ARG ; } if ( ! (count = sscanf( argv[*argn], "%ld%1c", arg, &extra )) ) { fprintf( stderr, "cannot parse %s for %s\n", argv[*argn], name ) ; return ERR_ARG ; } if ( count > 1 ) { fprintf( stderr, "warning: ignored %c... in value for %s\n", extra, name ) ; } if ( *arg > hi || *arg < lo ) { fprintf( stderr, "%s (%ld) must be in range %ld to %ld\n", name, *arg, lo, hi ) ; return ERR_ARG ; } ++(*argn) ; } return ERR_OK ; }
void pm_proginit(int * const argcP, char * argv[]) { /*---------------------------------------------------------------------------- Do various initialization things that all programs in the Netpbm package, and programs that emulate such programs, should do. This includes processing global options. This includes calling pm_init() to initialize the Netpbm libraries. -----------------------------------------------------------------------------*/ int argn, i; const char * progname; bool showmessages; bool show_version; /* We're supposed to just show the version information, then exit the program. */ bool show_help; /* We're supposed to just tell user where to get help, then exit the program. */ /* Extract program name. */ #ifdef VMS progname = vmsProgname(argcP, argv); #else progname = strrchr( argv[0], '/'); #endif if (progname == NULL) progname = argv[0]; else ++progname; pm_init(progname, 0); /* Check for any global args. */ showmessages = TRUE; show_version = FALSE; show_help = FALSE; pm_plain_output = FALSE; for (argn = 1; argn < *argcP; ++argn) { if (pm_keymatch(argv[argn], "-quiet", 6) || pm_keymatch(argv[argn], "--quiet", 7)) showmessages = FALSE; else if (pm_keymatch(argv[argn], "-version", 8) || pm_keymatch(argv[argn], "--version", 9)) show_version = TRUE; else if (pm_keymatch(argv[argn], "-help", 5) || pm_keymatch(argv[argn], "--help", 6) || pm_keymatch(argv[argn], "-?", 2)) show_help = TRUE; else if (pm_keymatch(argv[argn], "-plain", 6) || pm_keymatch(argv[argn], "--plain", 7)) pm_plain_output = TRUE; else continue; for (i = argn + 1; i <= *argcP; ++i) argv[i - 1] = argv[i]; --(*argcP); } pm_setMessage((unsigned int) showmessages, NULL); if (show_version) { showVersion(); exit( 0 ); } else if (show_help) { pm_error("Use 'man %s' for help.", progname); /* If we can figure out a way to distinguish Netpbm programs from other programs using the Netpbm libraries, we can do better here. */ if (0) showNetpbmHelp(progname); exit(0); } }
int main (int argc, char **argv) { int argc_copy = argc ; char **argv_copy = argv ; int argn; const char * const usage = "[-left <nn>] [-right <nn>] [-top <nn>] " "[-bottom <nn>] [-formlength <nn>] [pbmfile]"; /* Options */ /* These defaults are for a DEC LN03 with A4 paper (2400x3400 pixels) */ const char *opt_left_margin = "0"; const char *opt_top_margin = opt_left_margin; const char *opt_right_margin = "2400"; const char *opt_bottom_margin = "3400"; const char *opt_form_length = opt_bottom_margin; int width, height, format ; pbm_init (&argc_copy, argv_copy) ; argn = 1; while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { if( pm_keymatch(argv[argn], "-left", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_left_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-right", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_right_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-top", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_top_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-bottom", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_bottom_margin = argv[argn]; } else if( pm_keymatch(argv[argn], "-formlength", 2) ) { if( ++argn >= argc ) pm_usage(usage); opt_form_length = argv[argn]; } else pm_usage(usage); ++argn; } if( argn < argc ) { input = pm_openr( argv[argn] ); argn++; } else input = stdin; if( argn != argc ) pm_usage(usage); /* Initialise pbm file */ pbm_readpbminit (input, &width, &height, &format) ; if (format != PBM_FORMAT && format != RPBM_FORMAT) pm_error ("input not in PBM format") ; /* * In explanation of the sequence below: * <ESC>[!p DECSTR soft terminal reset * <ESC>[11h PUM select unit of measurement * <ESC>[7 I SSU select pixel as size unit * <ESC>[?52l DECOPM origin is corner of printable area * <ESC>[%s;%ss DECSLRM left and right margins * <ESC>[%s;%sr DECSTBM top and bottom margins * <ESC>[%st DECSLPP form length * <ESC>P0;0;1q select sixel graphics mode * "1;1 DECGRA aspect ratio (1:1) */ /* Initialise sixel file */ printf ("\033[!p\033[11h\033[7 I\033[?52l\033[%s;%ss\033" "[%s;%sr\033[%st\033P0;0;1q\"1;1", opt_left_margin, opt_right_margin, opt_top_margin, opt_bottom_margin, opt_form_length); /* Convert data */ convert (width, height, format) ; /* Terminate sixel data */ print ("\033\\\n") ; /* If the program failed, it previously aborted with nonzero completion code, via various function calls. */ return 0; }
int main(int argc, char * argv[]) { int argn; const char * const usage = "[-[no]black] [-[no]wpoint] [-[no]label] [-no[axes]] [-full]\n\ [-xy|-upvp] [-rec709|-ntsc|-ebu|-smpte|-hdtv|-cie]\n\ [-red <x> <y>] [-green <x> <y>] [-blue <x> <y>]\n\ [-white <x> <y>] [-gamma <g>]\n\ [-size <s>] [-xsize|-width <x>] [-ysize|-height <y>]"; const struct colorSystem *cs; int widspec = FALSE, hgtspec = FALSE; int xBias, yBias; int upvp = FALSE; /* xy or u'v' color coordinates? */ int showWhite = TRUE; /* Show white point ? */ int showBlack = TRUE; /* Show black body curve ? */ int fullChart = FALSE; /* Fill entire tongue ? */ int showLabel = TRUE; /* Show labels ? */ int showAxes = TRUE; /* Plot axes ? */ ppm_init(&argc, argv); argn = 1; cs = &Rec709system; /* default */ while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') { if (pm_keymatch(argv[argn], "-xy", 2)) { upvp = FALSE; } else if (pm_keymatch(argv[argn], "-upvp", 1)) { upvp = TRUE; } else if (pm_keymatch(argv[argn], "-xsize", 1) || pm_keymatch(argv[argn], "-width", 2)) { if (widspec) { pm_error("already specified a size/width/xsize"); } argn++; if ((argn == argc) || (sscanf(argv[argn], "%d", &sxsize) != 1)) pm_usage(usage); widspec = TRUE; } else if (pm_keymatch(argv[argn], "-ysize", 1) || pm_keymatch(argv[argn], "-height", 2)) { if (hgtspec) { pm_error("already specified a size/height/ysize"); } argn++; if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1)) pm_usage(usage); hgtspec = TRUE; } else if (pm_keymatch(argv[argn], "-size", 2)) { if (hgtspec || widspec) { pm_error("already specified a size/height/ysize"); } argn++; if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1)) pm_usage(usage); sxsize = sysize; hgtspec = widspec = TRUE; } else if (pm_keymatch(argv[argn], "-rec709", 1)) { cs = &Rec709system; } else if (pm_keymatch(argv[argn], "-ntsc", 1)) { cs = &NTSCsystem; } else if (pm_keymatch(argv[argn], "-ebu", 1)) { cs = &EBUsystem; } else if (pm_keymatch(argv[argn], "-smpte", 2)) { cs = &SMPTEsystem; } else if (pm_keymatch(argv[argn], "-hdtv", 2)) { cs = &HDTVsystem; } else if (pm_keymatch(argv[argn], "-cie", 1)) { cs = &CIEsystem; } else if (pm_keymatch(argv[argn], "-black", 3)) { showBlack = TRUE; /* Show black body curve */ } else if (pm_keymatch(argv[argn], "-wpoint", 2)) { showWhite = TRUE; /* Show white point of color system */ } else if (pm_keymatch(argv[argn], "-noblack", 3)) { showBlack = FALSE; /* Don't show black body curve */ } else if (pm_keymatch(argv[argn], "-nowpoint", 3)) { showWhite = FALSE; /* Don't show white point of system */ } else if (pm_keymatch(argv[argn], "-label", 1)) { showLabel = TRUE; /* Show labels. */ } else if (pm_keymatch(argv[argn], "-nolabel", 3)) { showLabel = FALSE; /* Don't show labels */ } else if (pm_keymatch(argv[argn], "-axes", 1)) { showAxes = TRUE; /* Show axes. */ } else if (pm_keymatch(argv[argn], "-noaxes", 3)) { showAxes = FALSE; /* Don't show axes */ } else if (pm_keymatch(argv[argn], "-full", 1)) { fullChart = TRUE; /* Fill whole tongue full-intensity */ } else if (pm_keymatch(argv[argn], "-gamma", 2)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.gamma) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-red", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xRed) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yRed) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-green", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xGreen) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yGreen) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-blue", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xBlue) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yBlue) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-white", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xWhite) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yWhite) != 1)) pm_usage(usage); } else { pm_usage(usage); } argn++; } if (argn != argc) { /* Extra bogus arguments ? */ pm_usage(usage); } pixcols = sxsize; pixrows = sysize; pixels = ppm_allocarray(pixcols, pixrows); /* Partition into plot area and axes and establish subwindow. */ xBias = Sz(32); yBias = Sz(20); makeAllBlack(pixels, pixcols, pixrows); drawTongueOutline(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias); fillInTongue(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias, fullChart); if (showAxes) drawAxes(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias); if (showWhite) plotWhitePoint(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias); if (showBlack) plotBlackBodyCurve(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias); /* Plot wavelengths around periphery of the tongue. */ if (showAxes) plotMonochromeWavelengths(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias); if (showLabel) writeLabel(pixels, pixcols, pixrows, Maxval, cs); ppm_writeppm(stdout, pixels, pixcols, pixrows, Maxval, FALSE); return 0; }
int main( int argc, char * argv[]) { FILE * ifp; int argn, extraskip; const char * const usage = "[-extraskip N] [macpfile]"; int outOfSync; int pixelCnt; pbm_init( &argc, argv ); argn = 1; /* initial value */ extraskip = 0; /* initial value */ /* Check for flags. */ if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { if ( pm_keymatch( argv[argn], "-extraskip", 2 ) ) { argn++; if ( argn == argc || sscanf( argv[argn], "%d", &extraskip ) != 1 ) pm_usage( usage ); } else pm_usage( usage ); argn++; } if ( argn < argc ) { ifp = pm_openr( argv[argn] ); argn++; } else ifp = stdin; if ( argn != argc ) pm_usage( usage ); if ( extraskip > 256 * 1024 ) pm_error("-extraskip value too large"); else if ( extraskip > 0 ) skipExtraBytes( ifp, extraskip); else skipHeader( ifp ); pbm_writepbminit( stdout, MACP_COLS, MACP_ROWS, 0 ); ReadMacPaintFile( ifp, &outOfSync, &pixelCnt ); /* We may not be at EOF. Macpaint files often have extra bytes after image data. */ pm_close( ifp ); if ( pixelCnt == 0 ) pm_error("No image data."); else if ( pixelCnt < MACP_BYTES ) pm_error("Compressed image data terminated prematurely."); else if ( outOfSync > 0 ) pm_message("Warning: Corrupt image data. %d rows misaligned.", outOfSync); pm_close( stdout ); exit( 0 ); }
int main(int argc, const char ** const argv) { FILE * ifP; int rows, cols; int colorCt; int argn; unsigned int bitsPerPixel; pixval maxval; colorhist_vector chv; char rgb[CLUTCOLORCT]; const char * windowName; int display, expand; int winflag; const char* const usage = "[-windowname windowname] [-expand expand] [-display display] [ppmfile]"; pixel** pixels; colorhash_table cht; pm_proginit(&argc, argv); argn = 1; windowName = "untitled"; winflag = 0; expand = 1; display = 0; while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { if ( pm_keymatch(argv[argn],"-windowname",2) && argn + 1 < argc ) { ++argn; windowName = argv[argn]; winflag = 1; } else if ( pm_keymatch(argv[argn],"-expand",2) && argn + 1 < argc ) { ++argn; if ( sscanf( argv[argn], "%d",&expand ) != 1 ) pm_usage( usage ); } else if ( pm_keymatch(argv[argn],"-display",2) && argn + 1 < argc ) { ++argn; if ( sscanf( argv[argn], "%d",&display ) != 1 ) pm_usage( usage ); } else pm_usage( usage ); } if ( argn < argc ) { ifP = pm_openr( argv[argn] ); if ( ! winflag ) windowName = argv[argn]; ++argn; } else ifP = stdin; if ( argn != argc ) pm_usage( usage ); pixels = ppm_readppm(ifP, &cols, &rows, &maxval); pm_close(ifP); /* Figure out the colormap. */ pm_message("Computing colormap..." ); chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORCT, &colorCt); if (!chv) pm_error("Too many colors - try doing a 'pnmquant %u'", MAXCOLORCT); pm_message("%u colors found", colorCt ); makeIcrColormap(chv, colorCt, maxval, rgb); bitsPerPixel = bppFromColorCt(colorCt); /* And make a hash table for fast lookup. */ cht = ppm_colorhisttocolorhash(chv, colorCt); ppm_freecolorhist(chv); /************** Create a new window using ICR protocol *********/ /* Format is "ESC^W;left;top;width;height;display;windowname" */ pm_message("Creating window %s ...", windowName); printf("\033^W;%d;%d;%d;%d;%d;%s^", 0, 0, cols * expand, rows * expand, display, windowName); fflush(stdout); /****************** Download the colormap. ********************/ downloadColormap(rgb, windowName); sendOutPicture(pixels, rows, cols, cht, expand, windowName); return 0; }