| 1 |
/****************************************************************************** |
| 2 |
pamtohdiff |
| 3 |
******************************************************************************* |
| 4 |
This program creates a PAM output which a horizontal difference image of |
| 5 |
the input PAM. The samples in each row are a number to be added to to |
| 6 |
the previous output row to create the next output row (and the first |
| 7 |
output row is simply the same as the input row). |
| 8 |
|
| 9 |
Because these samples must be positive and negative and PAM samples are |
| 10 |
unsigned, we bias each PAM sample by the input maxval and make the output |
| 11 |
maxval twice the input maxval. |
| 12 |
|
| 13 |
By Bryan Henderson, San Jose, CA 2002.04.15. |
| 14 |
|
| 15 |
******************************************************************************/ |
| 16 |
#include <string.h> |
| 17 |
#include <stdio.h> |
| 18 |
|
| 19 |
#include "pm_c_util.h" |
| 20 |
#include "pam.h" |
| 21 |
#include "shhopt.h" |
| 22 |
|
| 23 |
struct cmdlineInfo { |
| 24 |
/* All the information the user supplied in the command line, |
| 25 |
in a form easy for the program to use. |
| 26 |
*/ |
| 27 |
const char *inputFilespec; /* Filespecs of input files */ |
| 28 |
unsigned int verbose; |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static void |
| 34 |
parseCommandLine(int argc, char ** argv, |
| 35 |
struct cmdlineInfo * const cmdlineP) { |
| 36 |
/*---------------------------------------------------------------------------- |
| 37 |
Note that the file spec array we return is stored in the storage that |
| 38 |
was passed to us as the argv array. |
| 39 |
-----------------------------------------------------------------------------*/ |
| 40 |
optEntry *option_def = malloc( 100*sizeof( optEntry ) ); |
| 41 |
/* Instructions to pm_optParseOptions3 on how to parse our options. |
| 42 |
*/ |
| 43 |
optStruct3 opt; |
| 44 |
|
| 45 |
unsigned int option_def_index; |
| 46 |
|
| 47 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 48 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); |
| 49 |
|
| 50 |
opt.opt_table = option_def; |
| 51 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 52 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 53 |
|
| 54 |
pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 ); |
| 55 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 56 |
|
| 57 |
if (argc-1 < 1) |
| 58 |
cmdlineP->inputFilespec = "-"; |
| 59 |
else if (argc-1 == 1) |
| 60 |
cmdlineP->inputFilespec = argv[1]; |
| 61 |
else |
| 62 |
pm_error("Too many arguments."); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
int |
| 68 |
main(int argc, char *argv[]) { |
| 69 |
FILE *ifP; |
| 70 |
struct cmdlineInfo cmdline; |
| 71 |
struct pam inpam, outpam; |
| 72 |
unsigned int row; |
| 73 |
tuple * inrow; |
| 74 |
tuple * outrow; |
| 75 |
tuple * prevrow; |
| 76 |
|
| 77 |
pnm_init( &argc, argv ); |
| 78 |
|
| 79 |
parseCommandLine(argc, argv, &cmdline); |
| 80 |
|
| 81 |
ifP = pm_openr(cmdline.inputFilespec); |
| 82 |
|
| 83 |
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 84 |
|
| 85 |
outpam = inpam; |
| 86 |
outpam.file = stdout; |
| 87 |
outpam.format = PAM_FORMAT; |
| 88 |
strcpy(outpam.tuple_type, "hdiff"); |
| 89 |
|
| 90 |
pnm_writepaminit(&outpam); |
| 91 |
|
| 92 |
inrow = pnm_allocpamrow(&inpam); |
| 93 |
outrow = pnm_allocpamrow(&outpam); |
| 94 |
prevrow = pnm_allocpamrow(&inpam); |
| 95 |
|
| 96 |
pnm_setpamrow(&inpam, prevrow, 0); |
| 97 |
|
| 98 |
/* All arithmetic in this operation and in the reverse operation |
| 99 |
(to recover the image) is done modulus the maxval+1 (the hdiff |
| 100 |
PAM and the image have the same maxval) in order to produce |
| 101 |
legal PAM samples (which must be in the range 0..maxval). This |
| 102 |
might seem to throw away information, but it doesn't. Example: |
| 103 |
maxval is 99. Intensity goes from 90 in Row 0 to 10 in Row 1. |
| 104 |
The difference is -80. -80 mod 100 is 20, so 20 goes in the |
| 105 |
hdiff output. On reconstructing the image, the interpreter |
| 106 |
knows the "20" can't be +20, because that would create the |
| 107 |
sample 90 + 20 = 110, and violate maxval. So it must be -80. |
| 108 |
Modulus arithmetic by the interpreter effectively makes that |
| 109 |
decision. |
| 110 |
*/ |
| 111 |
|
| 112 |
|
| 113 |
/* The bias is just to make it easier to look at the output visually. |
| 114 |
If you display the values as intensities, and your differences are |
| 115 |
all +/- half of maxval, you can see positive transitions as bright |
| 116 |
spots and negative transitions as dark spots. |
| 117 |
*/ |
| 118 |
|
| 119 |
{ |
| 120 |
unsigned int const bias = outpam.maxval/2; |
| 121 |
for (row = 0; row < inpam.height; ++row) { |
| 122 |
unsigned int col; |
| 123 |
pnm_readpamrow(&inpam, inrow); |
| 124 |
for (col = 0; col < inpam.width; ++col) { |
| 125 |
unsigned int plane; |
| 126 |
for (plane = 0; plane < inpam.depth; ++plane) { |
| 127 |
|
| 128 |
sample const sampleValue = inrow[col][plane]; |
| 129 |
int const difference = sampleValue - prevrow[col][plane]; |
| 130 |
outrow[col][plane] = (difference + bias) % (outpam.maxval+1); |
| 131 |
prevrow[col][plane] = sampleValue; |
| 132 |
} |
| 133 |
} |
| 134 |
pnm_writepamrow(&outpam, outrow); |
| 135 |
} |
| 136 |
} |
| 137 |
pnm_freepamrow(prevrow); |
| 138 |
pnm_freepamrow(outrow); |
| 139 |
pnm_freepamrow(inrow); |
| 140 |
|
| 141 |
exit(0); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|