| 1 |
/*********************************************************************/ |
| 2 |
/* ppmshift - shift lines of a picture left or right by x pixels */ |
| 3 |
/* Frank Neumann, October 1993 */ |
| 4 |
/* V1.1 16.11.1993 */ |
| 5 |
/* */ |
| 6 |
/* version history: */ |
| 7 |
/* V1.0 11.10.1993 first version */ |
| 8 |
/* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */ |
| 9 |
/*********************************************************************/ |
| 10 |
|
| 11 |
#include <stdbool.h> |
| 12 |
|
| 13 |
#include "mallocvar.h" |
| 14 |
#include "rand.h" |
| 15 |
#include "shhopt.h" |
| 16 |
#include "ppm.h" |
| 17 |
|
| 18 |
|
| 19 |
struct CmdlineInfo { |
| 20 |
/* All the information the user supplied in the command line, |
| 21 |
in a form easy for the program to use. |
| 22 |
*/ |
| 23 |
const char * inputFileName; |
| 24 |
|
| 25 |
unsigned int shift; |
| 26 |
unsigned int seedSpec; |
| 27 |
unsigned int seed; |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
static void |
| 33 |
parseCommandLine(int argc, const char ** const argv, |
| 34 |
struct CmdlineInfo * const cmdlineP) { |
| 35 |
/*---------------------------------------------------------------------------- |
| 36 |
Note that the file spec array we return is stored in the storage that |
| 37 |
was passed to us as the argv array. |
| 38 |
-----------------------------------------------------------------------------*/ |
| 39 |
optEntry * option_def; |
| 40 |
/* Instructions to OptParseOptions3 on how to parse our options. */ |
| 41 |
optStruct3 opt; |
| 42 |
|
| 43 |
unsigned int option_def_index; |
| 44 |
|
| 45 |
MALLOCARRAY(option_def, 100); |
| 46 |
|
| 47 |
opt.opt_table = option_def; |
| 48 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 49 |
opt.allowNegNum = true; /* We have no parms that are negative numbers */ |
| 50 |
|
| 51 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 52 |
OPTENT3(0, "seed", OPT_UINT, &cmdlineP->seed, |
| 53 |
&cmdlineP->seedSpec, 0); |
| 54 |
|
| 55 |
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); |
| 56 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 57 |
|
| 58 |
if (argc-1 < 1) |
| 59 |
pm_error("You must specify the shift factor as an argument"); |
| 60 |
else { |
| 61 |
int const arg1 = atoi(argv[1]); |
| 62 |
if (arg1 < 0) |
| 63 |
pm_error("shift factor must be 0 or more"); |
| 64 |
cmdlineP->shift = arg1; |
| 65 |
|
| 66 |
if (argc-1 < 2) |
| 67 |
cmdlineP->inputFileName = "-"; |
| 68 |
else { |
| 69 |
cmdlineP->inputFileName = argv[2]; |
| 70 |
|
| 71 |
if (argc-1 > 2) |
| 72 |
pm_error("Too many arguments (%u). " |
| 73 |
"Shift factor and input file name are the only " |
| 74 |
"possible arguments", argc-1); |
| 75 |
} |
| 76 |
} |
| 77 |
free(option_def); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static void |
| 83 |
shiftRow(pixel * const srcrow, |
| 84 |
unsigned int const cols, |
| 85 |
unsigned int const shift, |
| 86 |
pixel * const destrow, |
| 87 |
struct pm_randSt * const randStP) { |
| 88 |
|
| 89 |
/* the range by which a line is shifted lays in the range from */ |
| 90 |
/* -shift/2 .. +shift/2 pixels; however, within this range it is */ |
| 91 |
/* randomly chosen */ |
| 92 |
|
| 93 |
pixel * pP; |
| 94 |
pixel * pP2; |
| 95 |
int nowshift; |
| 96 |
|
| 97 |
if (shift != 0) |
| 98 |
nowshift = (pm_rand(randStP) % (shift+1)) - ((shift+1) / 2); |
| 99 |
else |
| 100 |
nowshift = 0; |
| 101 |
|
| 102 |
pP = &srcrow[0]; |
| 103 |
pP2 = &destrow[0]; |
| 104 |
|
| 105 |
/* if the shift value is less than zero, we take the original |
| 106 |
pixel line and copy it into the destination line translated |
| 107 |
to the left by x pixels. The empty pixels on the right end |
| 108 |
of the destination line are filled up with the pixel that |
| 109 |
is the right-most in the original pixel line. |
| 110 |
*/ |
| 111 |
if (nowshift < 0) { |
| 112 |
unsigned int col; |
| 113 |
pP += abs(nowshift); |
| 114 |
for (col = 0; col < cols; ++col) { |
| 115 |
PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); |
| 116 |
++pP2; |
| 117 |
if (col < (cols + nowshift) - 1) |
| 118 |
++pP; |
| 119 |
} |
| 120 |
} else { |
| 121 |
unsigned int col; |
| 122 |
/* The shift value is 0 or positive, so fill the first |
| 123 |
<nowshift> pixels of the destination line with the |
| 124 |
first pixel from the source line, and copy the rest of |
| 125 |
the source line to the dest line |
| 126 |
*/ |
| 127 |
for (col = 0; col < cols; ++col) { |
| 128 |
PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); |
| 129 |
++pP2; |
| 130 |
if (col >= nowshift) |
| 131 |
++pP; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
int |
| 139 |
main(int argc, const char ** argv) { |
| 140 |
|
| 141 |
FILE * ifP; |
| 142 |
struct CmdlineInfo cmdline; |
| 143 |
int rows, cols, format; |
| 144 |
pixval maxval; |
| 145 |
pixel * srcrow; |
| 146 |
pixel * destrow; |
| 147 |
unsigned int row; |
| 148 |
unsigned int shift; |
| 149 |
struct pm_randSt randSt; |
| 150 |
|
| 151 |
/* parse in 'default' parameters */ |
| 152 |
pm_proginit(&argc, argv); |
| 153 |
|
| 154 |
parseCommandLine(argc, argv, &cmdline); |
| 155 |
|
| 156 |
pm_randinit(&randSt); |
| 157 |
pm_srand2(&randSt, cmdline.seedSpec, cmdline.seed); |
| 158 |
|
| 159 |
ifP = pm_openr(cmdline.inputFileName); |
| 160 |
|
| 161 |
/* read first data from file */ |
| 162 |
ppm_readppminit(ifP, &cols, &rows, &maxval, &format); |
| 163 |
|
| 164 |
if (cmdline.shift > cols) { |
| 165 |
shift = cols; |
| 166 |
pm_message("shift amount is larger than picture width - reset to %u", |
| 167 |
shift); |
| 168 |
} else |
| 169 |
shift = cmdline.shift; |
| 170 |
|
| 171 |
srcrow = ppm_allocrow(cols); |
| 172 |
destrow = ppm_allocrow(cols); |
| 173 |
|
| 174 |
ppm_writeppminit(stdout, cols, rows, maxval, 0); |
| 175 |
|
| 176 |
for (row = 0; row < rows; ++row) { |
| 177 |
ppm_readppmrow(ifP, srcrow, cols, maxval, format); |
| 178 |
|
| 179 |
shiftRow(srcrow, cols, shift, destrow, &randSt); |
| 180 |
|
| 181 |
ppm_writeppmrow(stdout, destrow, cols, maxval, 0); |
| 182 |
} |
| 183 |
|
| 184 |
ppm_freerow(destrow); |
| 185 |
ppm_freerow(srcrow); |
| 186 |
pm_close(ifP); |
| 187 |
pm_randterm(&randSt); |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |