diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2017-09-30 18:51:40 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2017-09-30 18:51:40 +0000 |
commit | 8b2ff6a4ae59c1ab37af590a7340656f93dbddee (patch) | |
tree | 36477ce3fc89409c484ee53afa10dac9685e7700 /generator | |
parent | eb9c9ea7fe9a04f1977bdd040c173c84f841d431 (diff) | |
download | netpbm-mirror-8b2ff6a4ae59c1ab37af590a7340656f93dbddee.tar.gz netpbm-mirror-8b2ff6a4ae59c1ab37af590a7340656f93dbddee.tar.xz netpbm-mirror-8b2ff6a4ae59c1ab37af590a7340656f93dbddee.zip |
Copy Development as new Advanced
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@3083 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'generator')
-rw-r--r-- | generator/pamgradient.c | 1 | ||||
-rw-r--r-- | generator/pbmtext.c | 71 | ||||
-rw-r--r-- | generator/pgmmake.c | 70 |
3 files changed, 106 insertions, 36 deletions
diff --git a/generator/pamgradient.c b/generator/pamgradient.c index 57e78288..95e4d8c4 100644 --- a/generator/pamgradient.c +++ b/generator/pamgradient.c @@ -77,6 +77,7 @@ parseCommandLine(int argc, const char **argv, pm_error("height argument must be a positive number. You " "specified '%s'", argv[6]); } + free(option_def); } diff --git a/generator/pbmtext.c b/generator/pbmtext.c index 357f0429..e25c6bbe 100644 --- a/generator/pbmtext.c +++ b/generator/pbmtext.c @@ -18,11 +18,13 @@ #include <math.h> #include <limits.h> #include <assert.h> +#include <setjmp.h> #include "pm_c_util.h" #include "mallocvar.h" #include "nstring.h" #include "shhopt.h" +#include "pm.h" #include "pbm.h" #include "pbmfont.h" @@ -157,6 +159,37 @@ reportFont(struct font * const fontP) { +static struct font * +fontFromFile(const char * const fileName) { + + struct font * retval; + + jmp_buf jmpbuf; + int rc; + + rc = setjmp(jmpbuf); + + if (rc == 0) { + /* This is the normal program flow */ + pm_setjmpbuf(&jmpbuf); + + retval = pbm_loadfont(fileName); + + pm_setjmpbuf(NULL); + } else { + /* This is the second pass, after pbm_loadfont does a longjmp + because it fails. + */ + pm_setjmpbuf(NULL); + + pm_error("Failed to load font from file '%s'", fileName); + } + + return retval; +} + + + static void computeFont(struct CmdlineInfo const cmdline, struct font ** const fontPP) { @@ -164,7 +197,7 @@ computeFont(struct CmdlineInfo const cmdline, struct font * fontP; if (cmdline.font) - fontP = pbm_loadfont(cmdline.font); + fontP = fontFromFile(cmdline.font); else { if (cmdline.builtin) fontP = pbm_defaultfont(cmdline.builtin); @@ -538,22 +571,26 @@ insertCharacter(const struct glyph * const glyphP, Insert one character (whose glyph is 'glyph') into the image bits[]. Its top left corner shall be row 'toprow', column 'leftcol'. -----------------------------------------------------------------------------*/ - unsigned int glyph_y; /* Y position within the glyph */ - - if (leftcol + glyphP->x < 0 || - leftcol + glyphP->x + glyphP->width > cols || - toprow < 0 || - toprow + glyphP->height >rows ) - pm_error("internal error. Rendering out of bounds"); - - for (glyph_y = 0; glyph_y < glyphP->height; ++glyph_y) { - unsigned int glyph_x; /* position within the glyph */ - - for (glyph_x = 0; glyph_x < glyphP->width; ++glyph_x) { - if (glyphP->bmap[glyph_y * glyphP->width + glyph_x]) { - unsigned int const col = leftcol + glyphP->x + glyph_x; - bits[toprow+glyph_y][col/8] |= PBM_BLACK << (7-col%8); - } + if (glyphP->width == 0 && glyphP->height == 0) { + /* No bitmap data. Some BDF files code space this way */ + } else { + unsigned int glyph_y; /* Y position within the glyph */ + + if (leftcol + glyphP->x < 0 || + leftcol + glyphP->x + glyphP->width > cols || + toprow < 0 || + toprow + glyphP->height >rows ) + pm_error("internal error. Rendering out of bounds"); + + for (glyph_y = 0; glyph_y < glyphP->height; ++glyph_y) { + unsigned int glyph_x; /* position within the glyph */ + + for (glyph_x = 0; glyph_x < glyphP->width; ++glyph_x) { + if (glyphP->bmap[glyph_y * glyphP->width + glyph_x]) { + unsigned int const col = leftcol + glyphP->x + glyph_x; + bits[toprow+glyph_y][col/8] |= PBM_BLACK << (7-col%8); + } + } } } } diff --git a/generator/pgmmake.c b/generator/pgmmake.c index f8f8b09c..3843e316 100644 --- a/generator/pgmmake.c +++ b/generator/pgmmake.c @@ -1,13 +1,18 @@ +#include <stdlib.h> +#include <string.h> + #include "pm_c_util.h" #include "mallocvar.h" #include "shhopt.h" #include "pgm.h" -struct cmdlineInfo { + + +struct CmdlineInfo { /* All the information the user supplied in the command line, in a form easy for the program to use. */ - gray grayLevel; + double grayLevel; unsigned int cols; unsigned int rows; gray maxval; @@ -15,9 +20,37 @@ struct cmdlineInfo { +static double +grayLevelFromArg(const char * const arg) { + + double retval; + + if (strlen(arg) < 1) + pm_error("Gray level argument is a null string"); + else { + char * endPtr; + + retval = strtod(arg, &endPtr); + + if (*endPtr != '\0') + pm_error("Gray level argument '%s' is not a floating point number", + arg); + + if (retval < 0.0) + pm_error("You can't have a negative gray level (%f)", retval); + if (retval > 1.0) + pm_error("Gray level must be in the range [0.0, 1.0]. " + "You specified %f", retval); + + } + return retval; +} + + + static void -parseCommandLine(int argc, char ** argv, - struct cmdlineInfo * const cmdlineP) { +parseCommandLine(int argc, const char ** argv, + struct CmdlineInfo * const cmdlineP) { /*---------------------------------------------------------------------------- Convert program invocation arguments (argc,argv) into a format the program can use easily, struct cmdlineInfo. Validate arguments along @@ -34,7 +67,7 @@ parseCommandLine(int argc, char ** argv, unsigned int maxvalSpec; unsigned int option_def_index; - MALLOCARRAY(option_def, 100); + MALLOCARRAY_NOFAIL(option_def, 100); option_def_index = 0; /* incremented by OPTENTRY */ OPTENT3(0, "maxval", OPT_UINT, &cmdlineP->maxval, &maxvalSpec, 0); @@ -43,11 +76,9 @@ 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 */ - pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); /* Uses and sets argc, argv, and some of *cmdlineP and others. */ - free (option_def); - if (!maxvalSpec) cmdlineP->maxval = PGM_MAXMAXVAL; else { @@ -65,37 +96,36 @@ parseCommandLine(int argc, char ** argv, pm_error("Only 3 arguments allowed: gray level, width, height. " "You specified %d", argc-1); else { - double const grayLevel = atof(argv[1]); - if (grayLevel < 0.0) - pm_error("You can't have a negative gray level (%f)", grayLevel); - if (grayLevel > 1.0) - pm_error("Gray level must be in the range [0.0, 1.0]. " - "You specified %f", grayLevel); - cmdlineP->grayLevel = ROUNDU(grayLevel * cmdlineP->maxval); + cmdlineP->grayLevel = grayLevelFromArg(argv[1]); cmdlineP->cols = pm_parse_width(argv[2]); cmdlineP->rows = pm_parse_height(argv[3]); } + free(option_def); } int -main(int argc, char *argv[]) { +main(int argc, const char ** const argv) { - struct cmdlineInfo cmdline; + struct CmdlineInfo cmdline; gray * grayrow; unsigned int col, row; + gray grayLevel; - pgm_init(&argc, argv); + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); + grayLevel = ROUNDU(cmdline.grayLevel * cmdline.maxval); + pgm_writepgminit(stdout, cmdline.cols, cmdline.rows, cmdline.maxval, 0); + grayrow = pgm_allocrow(cmdline.cols); /* All rows are identical. Fill once. */ for (col = 0; col < cmdline.cols; ++col) - grayrow[col] = cmdline.grayLevel; + grayrow[col] = grayLevel; for (row = 0; row < cmdline.rows; ++row) pgm_writepgmrow(stdout, grayrow, cmdline.cols, cmdline.maxval, 0); @@ -105,3 +135,5 @@ main(int argc, char *argv[]) { return 0; } + + |