| 1 |
/*********************************************************************/ |
| 2 |
/* ppmspread - randomly displace a PPM's pixels by a certain amount */ |
| 3 |
/* Frank Neumann, October 1993 */ |
| 4 |
/* V1.1 16.11.1993 */ |
| 5 |
/* */ |
| 6 |
/* version history: */ |
| 7 |
/* V1.0 12.10.1993 first version */ |
| 8 |
/* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */ |
| 9 |
/*********************************************************************/ |
| 10 |
|
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "nstring.h" |
| 14 |
#include "rand.h" |
| 15 |
#include "shhopt.h" |
| 16 |
#include "ppm.h" |
| 17 |
|
| 18 |
|
| 19 |
struct CmdlineInfo { |
| 20 |
/* This structure represents all of the information the user |
| 21 |
supplied in the command line but in a form that's easy for the |
| 22 |
program to use. |
| 23 |
*/ |
| 24 |
const char * inputFilename; /* '-' if stdin */ |
| 25 |
unsigned int spread; |
| 26 |
unsigned int randomseedSpec; |
| 27 |
unsigned int randomseed; |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
static void |
| 33 |
parseCommandLine(int argc, const char ** const argv, |
| 34 |
struct CmdlineInfo * const cmdlineP ) { |
| 35 |
|
| 36 |
optEntry * option_def; |
| 37 |
/* Instructions to OptParseOptions3 on how to parse our options */ |
| 38 |
optStruct3 opt; |
| 39 |
unsigned int option_def_index; |
| 40 |
|
| 41 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 42 |
option_def_index = 0; /* Incremented by OPTENTRY */ |
| 43 |
|
| 44 |
OPTENT3(0, "randomseed", OPT_UINT, &cmdlineP->randomseed, |
| 45 |
&cmdlineP->randomseedSpec, 0); |
| 46 |
|
| 47 |
opt.opt_table = option_def; |
| 48 |
opt.short_allowed = 0; |
| 49 |
opt.allowNegNum = 1; |
| 50 |
|
| 51 |
pm_optParseOptions3( &argc, (char **)argv, opt, sizeof(opt), 0 ); |
| 52 |
|
| 53 |
if (argc-1 < 1) |
| 54 |
pm_error("You must specify the spread factor as an argument"); |
| 55 |
else { |
| 56 |
const char * error; |
| 57 |
pm_string_to_uint(argv[1], &cmdlineP->spread, &error); |
| 58 |
|
| 59 |
if (error) |
| 60 |
pm_error("Spread factor '%s' is not an unsigned integer. %s", |
| 61 |
argv[1], error); |
| 62 |
|
| 63 |
if (argc-1 < 2) |
| 64 |
cmdlineP->inputFilename = "-"; |
| 65 |
else { |
| 66 |
cmdlineP->inputFilename = argv[2]; |
| 67 |
if (argc-1 >2) |
| 68 |
pm_error("Too many arguments: %u. " |
| 69 |
"The only possible arguments are " |
| 70 |
"the spread factor and the optional input file name", |
| 71 |
argc-1); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static void |
| 79 |
spreadRow(pixel ** const srcarray, |
| 80 |
unsigned int const cols, |
| 81 |
unsigned int const rows, |
| 82 |
unsigned int const spread, |
| 83 |
unsigned int const row, |
| 84 |
pixel ** const destarray, |
| 85 |
struct pm_randSt * const randStP) { |
| 86 |
|
| 87 |
unsigned int col; |
| 88 |
|
| 89 |
for (col = 0; col < cols; ++col) { |
| 90 |
pixel const p = srcarray[row][col]; |
| 91 |
|
| 92 |
int const xdis = (pm_rand(randStP) % (spread + 1) ) |
| 93 |
- ((spread + 1) / 2); |
| 94 |
int const ydis = (pm_rand(randStP) % (spread + 1)) |
| 95 |
- ((spread + 1) / 2); |
| 96 |
|
| 97 |
int const xnew = col + xdis; |
| 98 |
int const ynew = row + ydis; |
| 99 |
|
| 100 |
/* only set the displaced pixel if it's within the bounds |
| 101 |
of the image |
| 102 |
*/ |
| 103 |
if (xnew >= 0 && xnew < cols && ynew >= 0 && ynew < rows) { |
| 104 |
/* Displacing a pixel is accomplished by swapping it |
| 105 |
with another pixel in its vicinity. |
| 106 |
*/ |
| 107 |
pixel const p2 = srcarray[ynew][xnew]; |
| 108 |
/* Original value of second pixel */ |
| 109 |
|
| 110 |
/* Set second pixel to new value */ |
| 111 |
PPM_ASSIGN(destarray[ynew][xnew], |
| 112 |
PPM_GETR(p), PPM_GETG(p), PPM_GETB(p)); |
| 113 |
|
| 114 |
/* Set first pixel to (old) value of second */ |
| 115 |
PPM_ASSIGN(destarray[row][col], |
| 116 |
PPM_GETR(p2), PPM_GETG(p2), PPM_GETB(p2)); |
| 117 |
} else { |
| 118 |
/* Displaced pixel is out of bounds; leave the old pixel there. |
| 119 |
*/ |
| 120 |
PPM_ASSIGN(destarray[row][col], |
| 121 |
PPM_GETR(p), PPM_GETG(p), PPM_GETB(p)); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
int |
| 129 |
main(int argc, |
| 130 |
const char * argv[]) { |
| 131 |
|
| 132 |
struct CmdlineInfo cmdline; |
| 133 |
FILE * ifP; |
| 134 |
int rows, cols; |
| 135 |
unsigned int row; |
| 136 |
pixel ** destarray; |
| 137 |
pixel ** srcarray; |
| 138 |
pixval maxval; |
| 139 |
struct pm_randSt randSt; |
| 140 |
|
| 141 |
pm_proginit(&argc, argv); |
| 142 |
|
| 143 |
parseCommandLine(argc, argv, &cmdline); |
| 144 |
|
| 145 |
ifP = pm_openr(cmdline.inputFilename); |
| 146 |
|
| 147 |
srcarray = ppm_readppm(ifP, &cols, &rows, &maxval); |
| 148 |
|
| 149 |
destarray = ppm_allocarray(cols, rows); |
| 150 |
|
| 151 |
pm_randinit(&randSt); |
| 152 |
pm_srand2(&randSt, cmdline.randomseedSpec, cmdline.randomseed); |
| 153 |
|
| 154 |
/* clear out the buffer */ |
| 155 |
for (row = 0; row < rows; ++row) |
| 156 |
memset(destarray[row], 0, cols * sizeof(pixel)); |
| 157 |
|
| 158 |
/* Displace pixels */ |
| 159 |
for (row = 0; row < rows; ++row) { |
| 160 |
spreadRow(srcarray, cols, rows, cmdline.spread, row, |
| 161 |
destarray, &randSt); |
| 162 |
|
| 163 |
} |
| 164 |
pm_randterm(&randSt); |
| 165 |
|
| 166 |
ppm_writeppm(stdout, destarray, cols, rows, maxval, 0); |
| 167 |
|
| 168 |
pm_close(ifP); |
| 169 |
ppm_freearray(srcarray, rows); |
| 170 |
ppm_freearray(destarray, rows); |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
|