| 1 |
/***************************************************************************** |
| 2 |
pamtodjvurle |
| 3 |
****************************************************************************** |
| 4 |
This program converts a PAM image to DjVu Color RLE format. |
| 5 |
|
| 6 |
By Bryan Henderson, San Jose, CA April 2004. |
| 7 |
|
| 8 |
Contributed to the public domain by its author. |
| 9 |
|
| 10 |
This work is inspired by Ppmtodjvurle, written by Scott Pakin |
| 11 |
<[email protected]> in March 2004. Bryan took the requirements of |
| 12 |
the DjVu Color RLE format and the technique for generating the format |
| 13 |
(but not code) from that program. |
| 14 |
|
| 15 |
*****************************************************************************/ |
| 16 |
#include <stdio.h> |
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
#include "pm_c_util.h" |
| 20 |
#include "pam.h" |
| 21 |
#include "pammap.h" |
| 22 |
#include "shhopt.h" |
| 23 |
|
| 24 |
|
| 25 |
struct cmdlineInfo { |
| 26 |
const char * inputFilespec; |
| 27 |
const char * transparent; |
| 28 |
unsigned int showcolormap; |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static void |
| 34 |
parseCommandLine(int argc, |
| 35 |
char ** argv, |
| 36 |
struct cmdlineInfo * const cmdlineP) { |
| 37 |
/* -------------------------------------------------------------------------- |
| 38 |
Parse program command line described in Unix standard form by argc |
| 39 |
and argv. Return the information in the options as *cmdlineP. |
| 40 |
|
| 41 |
If command line is internally inconsistent (invalid options, etc.), |
| 42 |
issue error message to stderr and abort program. |
| 43 |
|
| 44 |
Note that the strings we return are stored in the storage that |
| 45 |
was passed to us as the argv array. We also trash *argv. |
| 46 |
--------------------------------------------------------------------------*/ |
| 47 |
optEntry *option_def = malloc( 100*sizeof( optEntry ) ); |
| 48 |
/* Instructions to pm_optParseOptions3 on how to parse our options. */ |
| 49 |
optStruct3 opt; |
| 50 |
|
| 51 |
unsigned int option_def_index; |
| 52 |
unsigned int transparentSpec; |
| 53 |
|
| 54 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 55 |
OPTENT3(0, "transparent", OPT_STRING, &cmdlineP->transparent, |
| 56 |
&transparentSpec, 0); |
| 57 |
OPTENT3(0, "showcolormap", OPT_FLAG, NULL, |
| 58 |
&cmdlineP->showcolormap, 0); |
| 59 |
|
| 60 |
opt.opt_table = option_def; |
| 61 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 62 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 63 |
|
| 64 |
pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 ); |
| 65 |
/* Uses and sets argc, argv, and some of *cmdline_p and others. */ |
| 66 |
|
| 67 |
if (!transparentSpec) |
| 68 |
cmdlineP->transparent = "white"; |
| 69 |
|
| 70 |
/* Get the program parameters */ |
| 71 |
|
| 72 |
if (argc-1 >= 1) |
| 73 |
cmdlineP->inputFilespec = argv[1]; |
| 74 |
else |
| 75 |
cmdlineP->inputFilespec = "-"; |
| 76 |
|
| 77 |
if (argc-1 > 1) |
| 78 |
pm_error("Program takes at most one argument: the file name. " |
| 79 |
"You specified %d", argc-1); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static void |
| 85 |
computeColorMap(struct pam * const pamP, |
| 86 |
tuple ** const tupleArray, |
| 87 |
unsigned int * const numColorsP, |
| 88 |
tupletable * const colormapP, |
| 89 |
tuplehash * const colorhashP, |
| 90 |
bool const show) { |
| 91 |
|
| 92 |
unsigned int numColors; |
| 93 |
tupletable colormap; |
| 94 |
|
| 95 |
colormap = pnm_computetuplefreqtable(pamP, tupleArray, 0, &numColors); |
| 96 |
if (numColors > 0xFF0) |
| 97 |
pm_error("too many colors; " |
| 98 |
"use pnmquant to reduce to no more than %u colors", 0xFF0); |
| 99 |
|
| 100 |
if (show) { |
| 101 |
unsigned int colorIndex; |
| 102 |
fprintf(stderr, "Color map:\n"); |
| 103 |
fprintf(stderr, " Index Color\n"); |
| 104 |
for (colorIndex = 0; colorIndex < numColors; ++colorIndex) { |
| 105 |
unsigned int plane; |
| 106 |
fprintf(stderr, " %5u ", colorIndex); |
| 107 |
for (plane = 0; plane < pamP->depth; ++plane) |
| 108 |
fprintf(stderr, "%3lu ", colormap[colorIndex]->tuple[plane]); |
| 109 |
fprintf(stderr, "\n"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
*colorhashP = pnm_computetupletablehash(pamP, colormap, numColors); |
| 114 |
|
| 115 |
*numColorsP = numColors; |
| 116 |
*colormapP = colormap; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static void |
| 122 |
makeDjvurleHeader(FILE * const ofP, |
| 123 |
struct pam * const pamP, |
| 124 |
unsigned int const numColors, |
| 125 |
tupletable const colormap) { |
| 126 |
|
| 127 |
unsigned int colorIndex; |
| 128 |
|
| 129 |
fprintf(ofP, "R6\n"); |
| 130 |
fprintf(ofP, "%d %d %d\n", pamP->width, pamP->height, numColors); |
| 131 |
|
| 132 |
for (colorIndex = 0; colorIndex < numColors; ++colorIndex) { |
| 133 |
sample red, grn, blu; |
| 134 |
|
| 135 |
if (pamP->depth >= 3) { |
| 136 |
red = colormap[colorIndex]->tuple[PAM_RED_PLANE]; |
| 137 |
grn = colormap[colorIndex]->tuple[PAM_GRN_PLANE]; |
| 138 |
blu = colormap[colorIndex]->tuple[PAM_BLU_PLANE]; |
| 139 |
} else |
| 140 |
red = grn = blu = colormap[colorIndex]->tuple[0]; |
| 141 |
|
| 142 |
fputc(pnm_scalesample(red, pamP->maxval, 255), ofP); |
| 143 |
fputc(pnm_scalesample(grn, pamP->maxval, 255), ofP); |
| 144 |
fputc(pnm_scalesample(blu, pamP->maxval, 255), ofP); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static bool |
| 151 |
colorEqual(tuple comparand, |
| 152 |
unsigned int comparandDepth, |
| 153 |
tuple comparator) { |
| 154 |
|
| 155 |
/* comparator has depth 3 */ |
| 156 |
|
| 157 |
if (comparandDepth >= 3) |
| 158 |
return (comparand[0] == comparator[0] && |
| 159 |
comparand[1] == comparator[1] && |
| 160 |
comparand[2] == comparator[2]); |
| 161 |
else |
| 162 |
return (comparand[0] == comparator[0] && |
| 163 |
comparand[0] == comparator[1] && |
| 164 |
comparand[0] == comparator[2]); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static void |
| 170 |
writeRleRun(FILE * const ofP, |
| 171 |
struct pam * const pamP, |
| 172 |
tuple const color, |
| 173 |
int const count, |
| 174 |
tuplehash const colorhash, |
| 175 |
tuple const transcolor) { |
| 176 |
/*---------------------------------------------------------------------------- |
| 177 |
Write one DjVu Color RLE run to the file 'ofP'. The run is |
| 178 |
'count' pixels of color 'color', using the color index given by |
| 179 |
'colorhash' and assuming 'transcolor' is the transparent color. |
| 180 |
|
| 181 |
'transcolor' is a 3-deep tuple with the same maxval as the image. |
| 182 |
-----------------------------------------------------------------------------*/ |
| 183 |
uint32_t rlevalue; /* RLE-encoded color/valuex */ |
| 184 |
int index; |
| 185 |
|
| 186 |
if (count > 0) { |
| 187 |
if (colorEqual(color, pamP->depth, transcolor)) |
| 188 |
index = 0xFFF; |
| 189 |
else { |
| 190 |
int found; |
| 191 |
pnm_lookuptuple(pamP, colorhash, color, &found, &index); |
| 192 |
assert(found); |
| 193 |
} |
| 194 |
rlevalue = (index << 20) | count; |
| 195 |
|
| 196 |
pm_writebiglong(ofP, rlevalue); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static void |
| 203 |
writeDjvurleRow(FILE * const ofP, |
| 204 |
struct pam * const pamP, |
| 205 |
tuple * const tupleRow, |
| 206 |
tuplehash const colorhash, |
| 207 |
tuple const transcolor) { |
| 208 |
|
| 209 |
unsigned int col; |
| 210 |
unsigned int runlength; |
| 211 |
tuple prevpixel; /* Previous pixel seen */ |
| 212 |
|
| 213 |
prevpixel = tupleRow[0]; |
| 214 |
runlength = 0; |
| 215 |
|
| 216 |
for (col = 0; col < pamP->width; ++col) { |
| 217 |
tuple const newpixel = tupleRow[col]; /* Current pixel color */ |
| 218 |
|
| 219 |
if (pnm_tupleequal(pamP, newpixel, prevpixel)) |
| 220 |
/* This is a continuation of the current run */ |
| 221 |
++runlength; |
| 222 |
else { |
| 223 |
/* The run is over. Write it out and start a run of the next |
| 224 |
color. |
| 225 |
*/ |
| 226 |
writeRleRun(ofP, pamP, prevpixel, runlength, |
| 227 |
colorhash, transcolor); |
| 228 |
runlength = 1; |
| 229 |
prevpixel = newpixel; |
| 230 |
} |
| 231 |
if (runlength >= (1<<20)-1) { |
| 232 |
/* Can't make the run any longer. Write it out and start a |
| 233 |
new run. |
| 234 |
*/ |
| 235 |
writeRleRun(ofP, pamP, prevpixel, runlength, |
| 236 |
colorhash, transcolor); |
| 237 |
runlength = 1; |
| 238 |
} |
| 239 |
} |
| 240 |
/* Write the last run we started */ |
| 241 |
writeRleRun(ofP, pamP, prevpixel, runlength, colorhash, transcolor); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
int |
| 247 |
main(int argc, char *argv[]) { |
| 248 |
|
| 249 |
FILE * const rlefile = stdout; |
| 250 |
|
| 251 |
struct cmdlineInfo cmdline; |
| 252 |
FILE *ifP; /* Input (Netpbm) file */ |
| 253 |
struct pam pam; /* Description of the image */ |
| 254 |
tuple ** tupleArray; /* The image raster */ |
| 255 |
tupletable colormap; /* List of all of the colors used */ |
| 256 |
unsigned int numColors; /* Number of unique colors in the color map */ |
| 257 |
tuplehash colorhash; |
| 258 |
/* Mapping from color to index into colormap[] */ |
| 259 |
tuple transcolor; |
| 260 |
/* Color that should be considered transparent */ |
| 261 |
|
| 262 |
pnm_init (&argc, argv); |
| 263 |
|
| 264 |
parseCommandLine(argc, argv, &cmdline); |
| 265 |
|
| 266 |
ifP = pm_openr(cmdline.inputFilespec); |
| 267 |
|
| 268 |
tupleArray = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 269 |
|
| 270 |
transcolor = pnm_parsecolor(cmdline.transparent, pam.maxval); |
| 271 |
|
| 272 |
computeColorMap(&pam, tupleArray, &numColors, &colormap, &colorhash, |
| 273 |
cmdline.showcolormap); |
| 274 |
|
| 275 |
makeDjvurleHeader(rlefile, &pam, numColors, colormap); |
| 276 |
|
| 277 |
/* Write the raster */ |
| 278 |
|
| 279 |
{ |
| 280 |
unsigned int row; |
| 281 |
for (row = 0; row < pam.height; ++row) |
| 282 |
writeDjvurleRow(rlefile, &pam, tupleArray[row], colorhash, |
| 283 |
transcolor); |
| 284 |
} |
| 285 |
/* Clean up */ |
| 286 |
|
| 287 |
pnm_freepamarray(tupleArray, &pam); |
| 288 |
pnm_freetupletable(&pam, colormap); |
| 289 |
pnm_destroytuplehash(colorhash); |
| 290 |
pnm_freepamtuple(transcolor); |
| 291 |
pm_close(ifP); |
| 292 |
|
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|