From ed334b4ec59331592a81d43228a9f9adb09c1cae Mon Sep 17 00:00:00 2001 From: giraffedata Date: Wed, 10 Mar 2021 02:46:51 +0000 Subject: fix recent cleanup and more cleanup git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4046 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- generator/ppmrough.c | 240 +++++++++++++++++++++++++++++---------------------- 1 file changed, 136 insertions(+), 104 deletions(-) (limited to 'generator') diff --git a/generator/ppmrough.c b/generator/ppmrough.c index 88a27a69..a4a1f14d 100644 --- a/generator/ppmrough.c +++ b/generator/ppmrough.c @@ -20,7 +20,6 @@ #include "shhopt.h" #include "ppm.h" -static pixval BG_RED, BG_GREEN, BG_BLUE; struct CmdlineInfo { @@ -28,9 +27,12 @@ struct CmdlineInfo { in a form easy for the program to use. */ unsigned int left, right, top, bottom; - unsigned int width, height, var; - const char * bg_rgb; - const char * fg_rgb; + unsigned int leftSpec, rightSpec, topSpec, bottomSpec; + unsigned int width; + unsigned int height; + unsigned int var; + const char * bg; /* Null if not specified */ + const char * fg; /* Null if not specified */ unsigned int randomseed; unsigned int randomseedSpec; unsigned int verbose; @@ -42,6 +44,8 @@ static void parseCommandLine(int argc, const char ** argv, struct CmdlineInfo * const cmdlineP) { + unsigned int widthSpec, heightSpec, bgSpec, fgSpec, varSpec; + optEntry * option_def; /* Instructions to OptParseOptions2 on how to parse our options. */ optStruct3 opt; @@ -51,28 +55,30 @@ parseCommandLine(int argc, const char ** argv, MALLOCARRAY_NOFAIL(option_def, 100); option_def_index = 0; /* incremented by OPTENTRY */ - OPTENT3(0, "width", OPT_UINT, &cmdlineP->width, NULL, 0); - OPTENT3(0, "height", OPT_UINT, &cmdlineP->height, NULL, 0); - OPTENT3(0, "left", OPT_UINT, &cmdlineP->left, NULL, 0); - OPTENT3(0, "right", OPT_UINT, &cmdlineP->right, NULL, 0); - OPTENT3(0, "top", OPT_UINT, &cmdlineP->top, NULL, 0); - OPTENT3(0, "bottom", OPT_UINT, &cmdlineP->bottom, NULL, 0); - OPTENT3(0, "bg", OPT_STRING, &cmdlineP->bg_rgb, NULL, 0); - OPTENT3(0, "fg", OPT_STRING, &cmdlineP->fg_rgb, NULL, 0); - OPTENT3(0, "var", OPT_UINT, &cmdlineP->var, NULL, 0); + OPTENT3(0, "width", OPT_UINT, &cmdlineP->width, + &widthSpec, 0); + OPTENT3(0, "height", OPT_UINT, &cmdlineP->height, + &heightSpec, 0); + OPTENT3(0, "left", OPT_UINT, &cmdlineP->left, + &cmdlineP->leftSpec, 0); + OPTENT3(0, "right", OPT_UINT, &cmdlineP->right, + &cmdlineP->rightSpec, 0); + OPTENT3(0, "top", OPT_UINT, &cmdlineP->top, + &cmdlineP->topSpec, 0); + OPTENT3(0, "bottom", OPT_UINT, &cmdlineP->bottom, + &cmdlineP->bottomSpec, 0); + OPTENT3(0, "bg", OPT_STRING, &cmdlineP->bg, + &bgSpec, 0); + OPTENT3(0, "fg", OPT_STRING, &cmdlineP->fg, + &fgSpec, 0); + OPTENT3(0, "var", OPT_UINT, &cmdlineP->var, + &varSpec, 0); OPTENT3(0, "randomseed", OPT_UINT, &cmdlineP->randomseed, &cmdlineP->randomseedSpec, 0); OPTENT3(0, "init", OPT_UINT, &cmdlineP->randomseed, &cmdlineP->randomseedSpec, 0); - OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); - - /* Set the defaults */ - cmdlineP->width = 100; - cmdlineP->height = 100; - cmdlineP->left = cmdlineP->right = cmdlineP->top = cmdlineP->bottom = -1; - cmdlineP->bg_rgb = NULL; - cmdlineP->fg_rgb = NULL; - cmdlineP->var = 10; + OPTENT3(0, "verbose", OPT_FLAG, NULL, + &cmdlineP->verbose, 0); opt.opt_table = option_def; opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ @@ -80,6 +86,17 @@ parseCommandLine(int argc, const char ** argv, pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); + if (!widthSpec) + cmdlineP->width = 100; + if (!heightSpec) + cmdlineP->height = 100; + if (!bgSpec) + cmdlineP->bg = NULL; + if (!fgSpec) + cmdlineP->fg = NULL; + if (!varSpec) + cmdlineP->var = 10; + if (argc-1 != 0) pm_error("There are no arguments. You specified %d.", argc-1); @@ -88,13 +105,41 @@ parseCommandLine(int argc, const char ** argv, +static void +reportParameters(struct CmdlineInfo const cmdline, + pixel const bgcolor, + pixel const fgcolor) { + + pm_message("width is %d, height is %d, variance is %d.", + cmdline.width, cmdline.height, cmdline.var); + if (cmdline.leftSpec) + pm_message("ragged left border is required"); + if (cmdline.rightSpec) + pm_message("ragged right border is required"); + if (cmdline.topSpec) + pm_message("ragged top border is required"); + if (cmdline.bottomSpec) + pm_message("ragged bottom border is required"); + pm_message("background is %s", + ppm_colorname(&bgcolor, PPM_MAXMAXVAL, 1)); + pm_message("foreground is %s", + ppm_colorname(&fgcolor, PPM_MAXMAXVAL, 1)); + if (cmdline.randomseedSpec) + pm_message("pm_rand() initialized with seed %u", + cmdline.randomseed); +} + + + static void makeAllForegroundColor(pixel ** const pixels, unsigned int const rows, unsigned int const cols, - pixval const r, - pixval const g, - pixval const b) { + pixel const fgcolor) { + + pixval const r = PPM_GETR(fgcolor); + pixval const g = PPM_GETG(fgcolor); + pixval const b = PPM_GETB(fgcolor); unsigned int row; @@ -115,6 +160,7 @@ procLeft(pixel ** const pixels, int const c1, int const c2, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { if (r1 + 1 != r2) { @@ -125,10 +171,10 @@ procLeft(pixel ** const pixels, int c; for (c = 0; c < cm; c++) - PPM_ASSIGN(pixels[rm][c], BG_RED, BG_GREEN, BG_BLUE); + pixels[rm][c] = bgcolor; - procLeft(pixels, r1, rm, c1, cm, var, randStP); - procLeft(pixels, rm, r2, cm, c2, var, randStP); + procLeft(pixels, r1, rm, c1, cm, var, bgcolor, randStP); + procLeft(pixels, rm, r2, cm, c2, var, bgcolor, randStP); } } @@ -142,6 +188,7 @@ procRight(pixel ** const pixels, int const c2, unsigned int const width, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { if (r1 + 1 != r2) { @@ -152,10 +199,10 @@ procRight(pixel ** const pixels, int c; for (c = cm; c < width; c++) - PPM_ASSIGN(pixels[rm][c], BG_RED, BG_GREEN, BG_BLUE); + pixels[rm][c] = bgcolor; - procRight(pixels, r1, rm, c1, cm, width, var, randStP); - procRight(pixels, rm, r2, cm, c2, width, var, randStP); + procRight(pixels, r1, rm, c1, cm, width, var, bgcolor, randStP); + procRight(pixels, rm, r2, cm, c2, width, var, bgcolor, randStP); } } @@ -168,6 +215,7 @@ procTop(pixel ** const pixels, int const r1, int const r2, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { if (c1 + 1 != c2) { @@ -178,10 +226,10 @@ procTop(pixel ** const pixels, int r; for (r = 0; r < rm; r++) - PPM_ASSIGN(pixels[r][cm], BG_RED, BG_GREEN, BG_BLUE); + pixels[r][cm] = bgcolor; - procTop(pixels, c1, cm, r1, rm, var, randStP); - procTop(pixels, cm, c2, rm, r2, var, randStP); + procTop(pixels, c1, cm, r1, rm, var, bgcolor, randStP); + procTop(pixels, cm, c2, rm, r2, var, bgcolor, randStP); } } @@ -195,6 +243,7 @@ procBottom(pixel ** const pixels, int const r2, unsigned int const height, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { if (c1 + 1 != c2) { @@ -205,10 +254,10 @@ procBottom(pixel ** const pixels, int r; for (r = rm; r < height; ++r) - PPM_ASSIGN(pixels[r][cm], BG_RED, BG_GREEN, BG_BLUE); + pixels[r][cm] = bgcolor; - procBottom(pixels, c1, cm, r1, rm, height, var, randStP); - procBottom(pixels, cm, c2, rm, r2, height, var, randStP); + procBottom(pixels, c1, cm, r1, rm, height, var, bgcolor, randStP); + procBottom(pixels, cm, c2, rm, r2, height, var, bgcolor, randStP); } } @@ -218,11 +267,13 @@ static void makeRaggedLeftBorder(pixel ** const pixels, unsigned int const rows, unsigned int const cols, + bool const leftSpec, unsigned int const left, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { - if (left >= 0) { + if (leftSpec) { int const leftC1 = left; int const leftC2 = left; int const leftR1 = 0; @@ -231,11 +282,12 @@ makeRaggedLeftBorder(pixel ** const pixels, unsigned int col; for (col = 0; col < leftC1; ++col) - PPM_ASSIGN(pixels[leftR1][col], BG_RED, BG_GREEN, BG_BLUE); + pixels[leftR1][col] = bgcolor; for (col = 0; col < leftC2; ++col) - PPM_ASSIGN(pixels[leftR2][col], BG_RED, BG_GREEN, BG_BLUE); + pixels[leftR2][col] = bgcolor; - procLeft(pixels, leftR1, leftR2, leftC1, leftC2, var, randStP); + procLeft(pixels, leftR1, leftR2, leftC1, leftC2, var, + bgcolor, randStP); } } @@ -245,12 +297,14 @@ static void makeRaggedRightBorder(pixel ** const pixels, unsigned int const rows, unsigned int const cols, + bool const rightSpec, unsigned int const right, unsigned int const width, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { - if (right >= 0) { + if (rightSpec) { int const rightC1 = cols - right - 1; int const rightC2 = cols - right - 1; int const rightR1 = 0; @@ -259,12 +313,12 @@ makeRaggedRightBorder(pixel ** const pixels, unsigned int col; for (col = rightC1; col < cols; ++col) - PPM_ASSIGN(pixels[rightR1][col], BG_RED, BG_GREEN, BG_BLUE); + pixels[rightR1][col] = bgcolor; for (col = rightC2; col < cols; ++col) - PPM_ASSIGN(pixels[rightR2][col], BG_RED, BG_GREEN, BG_BLUE); + pixels[rightR2][col] = bgcolor; procRight(pixels, rightR1, rightR2, rightC1, rightC2, width, var, - randStP); + bgcolor, randStP); } } @@ -274,11 +328,13 @@ static void makeRaggedTopBorder(pixel ** const pixels, unsigned int const rows, unsigned int const cols, + bool const topSpec, unsigned int const top, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { - if (top >= 0) { + if (topSpec) { unsigned int const topR1 = top; unsigned int const topR2 = top; unsigned int const topC1 = 0; @@ -287,11 +343,11 @@ makeRaggedTopBorder(pixel ** const pixels, unsigned int row; for (row = 0; row < topR1; ++row) - PPM_ASSIGN(pixels[row][topC1], BG_RED, BG_GREEN, BG_BLUE); + pixels[row][topC1] = bgcolor; for (row = 0; row < topR2; ++row) - PPM_ASSIGN(pixels[row][topC2], BG_RED, BG_GREEN, BG_BLUE); + pixels[row][topC2] = bgcolor; - procTop(pixels, topC1, topC2, topR1, topR2, var, randStP); + procTop(pixels, topC1, topC2, topR1, topR2, var, bgcolor, randStP); } } @@ -301,12 +357,14 @@ static void makeRaggedBottomBorder(pixel ** const pixels, unsigned int const rows, unsigned int const cols, + bool const bottomSpec, unsigned int const bottom, unsigned int const height, unsigned int const var, + pixel const bgcolor, struct pm_randSt * const randStP) { - if (bottom >= 0) { + if (bottomSpec) { unsigned int const bottomR1 = rows - bottom - 1; unsigned int const bottomR2 = rows - bottom - 1; unsigned int const bottomC1 = 0; @@ -315,25 +373,22 @@ makeRaggedBottomBorder(pixel ** const pixels, unsigned int row; for (row = bottomR1; row < rows; ++row) - PPM_ASSIGN(pixels[row][bottomC1], BG_RED, BG_GREEN, BG_BLUE); + pixels[row][bottomC1] = bgcolor; for (row = bottomR2; row < rows; ++row) - PPM_ASSIGN(pixels[row][bottomC2], BG_RED, BG_GREEN, BG_BLUE); + pixels[row][bottomC2] = bgcolor; procBottom(pixels, bottomC1, bottomC2, bottomR1, bottomR2, - height, var, randStP); + height, var, bgcolor, randStP); } } int -main(int argc, const char * argv[]) { +main(int argc, const char ** const argv) { struct CmdlineInfo cmdline; pixel bgcolor, fgcolor; - pixval fg_red, fg_green, fg_blue; - int rows, cols; - int left, right, top, bottom; struct pm_randSt randSt; static pixel** pixels; @@ -345,69 +400,46 @@ main(int argc, const char * argv[]) { pm_srand(&randSt, cmdline.randomseedSpec ? cmdline.randomseed : pm_randseed()); - cols = cmdline.width; - rows = cmdline.height; - left = cmdline.left; - right = cmdline.right; - top = cmdline.top; - bottom = cmdline.bottom; - - if (cmdline.bg_rgb) - bgcolor = ppm_parsecolor(cmdline.bg_rgb, PPM_MAXMAXVAL); + if (cmdline.bg) + bgcolor = ppm_parsecolor(cmdline.bg, PPM_MAXMAXVAL); else PPM_ASSIGN(bgcolor, 0, 0, 0); - BG_RED = PPM_GETR(bgcolor); - BG_GREEN = PPM_GETG(bgcolor); - BG_BLUE = PPM_GETB(bgcolor); - if (cmdline.fg_rgb) - fgcolor = ppm_parsecolor(cmdline.fg_rgb, PPM_MAXMAXVAL); + if (cmdline.fg) + fgcolor = ppm_parsecolor(cmdline.fg, PPM_MAXMAXVAL); else PPM_ASSIGN(fgcolor, PPM_MAXMAXVAL, PPM_MAXMAXVAL, PPM_MAXMAXVAL); - fg_red = PPM_GETR(fgcolor); - fg_green = PPM_GETG(fgcolor); - fg_blue = PPM_GETB(fgcolor); - - if (cmdline.verbose) { - pm_message("width is %d, height is %d, variance is %d.", - cols, rows, cmdline.var); - if (left >= 0) - pm_message("ragged left border is required"); - if (right >= 0) - pm_message("ragged right border is required"); - if (top >= 0) - pm_message("ragged top border is required"); - if (bottom >= 0) - pm_message("ragged bottom border is required"); - pm_message("background is %s", - ppm_colorname(&bgcolor, PPM_MAXMAXVAL, 1)); - pm_message("foreground is %s", - ppm_colorname(&fgcolor, PPM_MAXMAXVAL, 1)); - if (cmdline.randomseedSpec) - pm_message("pm_rand() initialized with seed %u", - cmdline.randomseed); - } - pixels = ppm_allocarray(cols, rows); + if (cmdline.verbose) + reportParameters(cmdline, bgcolor, fgcolor); + + pixels = ppm_allocarray(cmdline.width, cmdline.height); - makeAllForegroundColor(pixels, rows, cols, fg_red, fg_green, fg_blue); + makeAllForegroundColor(pixels, cmdline.height, cmdline.width, fgcolor); - makeRaggedLeftBorder(pixels, rows, cols, left, cmdline.var, &randSt); + makeRaggedLeftBorder(pixels, cmdline.height, cmdline.width, + cmdline.leftSpec, cmdline.left, + cmdline.var, bgcolor, &randSt); - makeRaggedRightBorder(pixels, rows, cols, right, - cmdline.width, cmdline.var, &randSt); + makeRaggedRightBorder(pixels, cmdline.height, cmdline.width, + cmdline.rightSpec, cmdline.right, + cmdline.width, cmdline.var, bgcolor, &randSt); - makeRaggedTopBorder(pixels, rows, cols, top, cmdline.var, &randSt); + makeRaggedTopBorder(pixels, cmdline.height, cmdline.width, + cmdline.topSpec, cmdline.top, + cmdline.var, bgcolor, &randSt); - makeRaggedBottomBorder(pixels, rows, cols, bottom, - cmdline.height, cmdline.var, &randSt); + makeRaggedBottomBorder(pixels, cmdline.height, cmdline.width, + cmdline.bottomSpec, cmdline.bottom, + cmdline.height, cmdline.var, bgcolor, &randSt); pm_randterm(&randSt); /* Write pixmap */ - ppm_writeppm(stdout, pixels, cols, rows, PPM_MAXMAXVAL, 0); + ppm_writeppm(stdout, pixels, cmdline.width, cmdline.height, + PPM_MAXMAXVAL, 0); - ppm_freearray(pixels, rows); + ppm_freearray(pixels, cmdline.height); pm_close(stdout); -- cgit 1.4.1