| 1 |
/*---------------------------------------------------------------------------- |
| 2 |
pamtopnm |
| 3 |
------------------------------------------------------------------------------ |
| 4 |
Part of the Netpbm package. |
| 5 |
|
| 6 |
Convert PAM images to PBM, PGM, or PPM (i.e. PNM) |
| 7 |
|
| 8 |
By Bryan Henderson, San Jose CA 2000.08.05 |
| 9 |
|
| 10 |
Contributed to the public domain by its author 2000.08.05. |
| 11 |
-----------------------------------------------------------------------------*/ |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "pm_c_util.h" |
| 16 |
#include "pam.h" |
| 17 |
#include "shhopt.h" |
| 18 |
#include "mallocvar.h" |
| 19 |
|
| 20 |
struct CmdlineInfo { |
| 21 |
/* All the information the user supplied in the command line, |
| 22 |
in a form easy for the program to use. |
| 23 |
*/ |
| 24 |
const char * inputFileName; /* Name of input file */ |
| 25 |
unsigned int assume; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
static void |
| 30 |
parseCommandLine(int argc, const char ** argv, |
| 31 |
struct CmdlineInfo *cmdlineP) { |
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
Note that the file spec array we return is stored in the storage that |
| 34 |
was passed to us as the argv array. |
| 35 |
-----------------------------------------------------------------------------*/ |
| 36 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 37 |
optStruct3 opt; |
| 38 |
|
| 39 |
unsigned int option_def_index; |
| 40 |
|
| 41 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 42 |
|
| 43 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 44 |
OPTENT3(0, "assume", OPT_FLAG, NULL, &cmdlineP->assume, 0); |
| 45 |
|
| 46 |
opt.opt_table = option_def; |
| 47 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 48 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 49 |
|
| 50 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 51 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 52 |
|
| 53 |
if (argc-1 == 0) |
| 54 |
cmdlineP->inputFileName = "-"; |
| 55 |
else if (argc-1 != 1) |
| 56 |
pm_error("Program takes zero or one argument (filename). You " |
| 57 |
"specified %d", argc-1); |
| 58 |
else |
| 59 |
cmdlineP->inputFileName = argv[1]; |
| 60 |
|
| 61 |
free(option_def); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static void |
| 67 |
validateTupleType(struct pam const inpam, |
| 68 |
int const assumeTupleType) { |
| 69 |
/*---------------------------------------------------------------------------- |
| 70 |
Make sure the image has a tuple type we know how to convert to PNM. |
| 71 |
|
| 72 |
We're quite liberal, trying to accommodate all sorts of future |
| 73 |
twists on the formats. If the tuple type _starts with_ |
| 74 |
BLACKANDWHITE, GRAYSCALE, or RGB, and has at least as many planes |
| 75 |
as we'd need to convert to PBM, PGM, or PPM, respectively, we |
| 76 |
accept it. We thus accommodate variations on these formats that add |
| 77 |
planes and add to the right end of the tuple type to explain them. |
| 78 |
|
| 79 |
If Callers specified 'assumeTupleType', we're even more liberal. |
| 80 |
-----------------------------------------------------------------------------*/ |
| 81 |
if (assumeTupleType) { |
| 82 |
/* User says tuple type is appropriate regardless of tuple_type. */ |
| 83 |
} else { |
| 84 |
if (inpam.depth >= 1 && |
| 85 |
strncmp(inpam.tuple_type, "BLACKANDWHITE", |
| 86 |
sizeof("BLACKANDWHITE")-1) == 0) { |
| 87 |
/* It's a PBMable image */ |
| 88 |
} else if (inpam.depth >= 1 && |
| 89 |
strncmp(inpam.tuple_type, "GRAYSCALE", |
| 90 |
sizeof("GRAYSCALE")-1) == 0) { |
| 91 |
/* It's a PGMable image */ |
| 92 |
} else if (inpam.depth >= 3 && |
| 93 |
strncmp(inpam.tuple_type, "RGB", sizeof("RGB")-1) == 0) { |
| 94 |
/* It's a PPMable image */ |
| 95 |
} else |
| 96 |
pm_error("PAM image does not have a depth and tuple_type " |
| 97 |
"consistent with a PNM image. " |
| 98 |
"According to its " |
| 99 |
"header, depth is %d and tuple_type is '%s'. " |
| 100 |
"Use the -assume option to convert anyway.", |
| 101 |
inpam.depth, inpam.tuple_type); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
int |
| 108 |
main(int argc, const char **argv) { |
| 109 |
|
| 110 |
struct CmdlineInfo cmdline; |
| 111 |
FILE * ifP; |
| 112 |
int eof; /* no more images in input stream */ |
| 113 |
struct pam inpam; /* Input PAM image */ |
| 114 |
struct pam outpam; /* Output PNM image */ |
| 115 |
|
| 116 |
pm_proginit(&argc, argv); |
| 117 |
|
| 118 |
parseCommandLine(argc, argv, &cmdline); |
| 119 |
|
| 120 |
ifP = pm_openr(cmdline.inputFileName); |
| 121 |
|
| 122 |
eof = FALSE; |
| 123 |
while (!eof) { |
| 124 |
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 125 |
|
| 126 |
validateTupleType(inpam, cmdline.assume); |
| 127 |
|
| 128 |
outpam = inpam; |
| 129 |
outpam.file = stdout; |
| 130 |
|
| 131 |
if (inpam.depth < 3) { |
| 132 |
outpam.depth = 1; |
| 133 |
if (inpam.maxval == 1) |
| 134 |
outpam.format = PBM_FORMAT; |
| 135 |
else |
| 136 |
outpam.format = PGM_FORMAT; |
| 137 |
} else { |
| 138 |
outpam.depth = 3; |
| 139 |
outpam.format = PPM_FORMAT; |
| 140 |
} |
| 141 |
|
| 142 |
pnm_writepaminit(&outpam); |
| 143 |
|
| 144 |
{ |
| 145 |
tuple *tuplerow; |
| 146 |
|
| 147 |
tuplerow = pnm_allocpamrow(&inpam); |
| 148 |
{ |
| 149 |
int row; |
| 150 |
|
| 151 |
for (row = 0; row < inpam.height; row++) { |
| 152 |
pnm_readpamrow(&inpam, tuplerow); |
| 153 |
pnm_writepamrow(&outpam, tuplerow); |
| 154 |
} |
| 155 |
} |
| 156 |
pnm_freepamrow(tuplerow); |
| 157 |
} |
| 158 |
|
| 159 |
pnm_nextimage(ifP, &eof); |
| 160 |
} |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|