| 1 |
/* pnmtorast.c - read a portable anymap and produce a Sun rasterfile |
| 2 |
** |
| 3 |
** Copyright (C) 1989, 1991 by Jef Poskanzer. |
| 4 |
** |
| 5 |
** Permission to use, copy, modify, and distribute this software and its |
| 6 |
** documentation for any purpose and without fee is hereby granted, provided |
| 7 |
** that the above copyright notice appear in all copies and that both that |
| 8 |
** copyright notice and this permission notice appear in supporting |
| 9 |
** documentation. This software is provided "as is" without express or |
| 10 |
** implied warranty. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "pnm.h" |
| 14 |
#include "rast.h" |
| 15 |
#include "mallocvar.h" |
| 16 |
|
| 17 |
#define MAXCOLORS 256 |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
static colormap_t * |
| 22 |
allocPrColormap(void) { |
| 23 |
|
| 24 |
colormap_t * prColormapP; |
| 25 |
|
| 26 |
MALLOCVAR(prColormapP); |
| 27 |
if (prColormapP == NULL) |
| 28 |
pm_error("out of memory"); |
| 29 |
prColormapP->type = RMT_EQUAL_RGB; |
| 30 |
prColormapP->length = MAXCOLORS; |
| 31 |
MALLOCARRAY(prColormapP->map[0], MAXCOLORS); |
| 32 |
MALLOCARRAY(prColormapP->map[1], MAXCOLORS); |
| 33 |
MALLOCARRAY(prColormapP->map[2], MAXCOLORS); |
| 34 |
if (prColormapP->map[0] == NULL || |
| 35 |
prColormapP->map[1] == NULL || |
| 36 |
prColormapP->map[2] == NULL) |
| 37 |
pm_error("out of memory"); |
| 38 |
|
| 39 |
return prColormapP; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static colormap_t * |
| 45 |
makePrColormap(colorhist_vector const chv, |
| 46 |
unsigned int const colors) { |
| 47 |
|
| 48 |
colormap_t * prColormapP; |
| 49 |
unsigned int i; |
| 50 |
|
| 51 |
prColormapP = allocPrColormap(); |
| 52 |
|
| 53 |
for (i = 0; i < colors; ++i) { |
| 54 |
prColormapP->map[0][i] = PPM_GETR(chv[i].color); |
| 55 |
prColormapP->map[1][i] = PPM_GETG(chv[i].color); |
| 56 |
prColormapP->map[2][i] = PPM_GETB(chv[i].color); |
| 57 |
} |
| 58 |
for ( ; i < MAXCOLORS; ++i) { |
| 59 |
prColormapP->map[0][i] = 0; |
| 60 |
prColormapP->map[1][i] = 0; |
| 61 |
prColormapP->map[2][i] = 0; |
| 62 |
} |
| 63 |
return prColormapP; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static colormap_t * |
| 69 |
makeGrayPrColormap(void) { |
| 70 |
|
| 71 |
colormap_t * prColormapP; |
| 72 |
unsigned int i; |
| 73 |
|
| 74 |
prColormapP = allocPrColormap(); |
| 75 |
|
| 76 |
for (i = 0; i < MAXCOLORS; ++i) { |
| 77 |
prColormapP->map[0][i] = i; |
| 78 |
prColormapP->map[1][i] = i; |
| 79 |
prColormapP->map[2][i] = i; |
| 80 |
} |
| 81 |
|
| 82 |
return prColormapP; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static void |
| 88 |
doRowDepth1(const xel * const xelrow, |
| 89 |
unsigned char * const rastRow, |
| 90 |
unsigned int const cols, |
| 91 |
int const format, |
| 92 |
xelval const maxval, |
| 93 |
colorhash_table const cht, |
| 94 |
unsigned int * const lenP) { |
| 95 |
|
| 96 |
unsigned int col; |
| 97 |
int bitcount; |
| 98 |
unsigned int cursor; |
| 99 |
|
| 100 |
cursor = 0; |
| 101 |
|
| 102 |
rastRow[cursor] = 0; |
| 103 |
bitcount = 7; |
| 104 |
|
| 105 |
for (col = 0; col < cols; ++col) { |
| 106 |
switch (PNM_FORMAT_TYPE(format)) { |
| 107 |
case PPM_TYPE: { |
| 108 |
xel adjustedXel; |
| 109 |
int color; |
| 110 |
if (maxval != 255) |
| 111 |
PPM_DEPTH(adjustedXel, xelrow[col], maxval, 255 ); |
| 112 |
color = ppm_lookupcolor(cht, &adjustedXel); |
| 113 |
if (color == -1) |
| 114 |
pm_error("color not found?!? " |
| 115 |
"col=%u r=%u g=%u b=%u", |
| 116 |
col, |
| 117 |
PPM_GETR(adjustedXel), |
| 118 |
PPM_GETG(adjustedXel), |
| 119 |
PPM_GETB(adjustedXel)); |
| 120 |
if (color) |
| 121 |
rastRow[cursor] |= 1 << bitcount; |
| 122 |
} break; |
| 123 |
|
| 124 |
default: { |
| 125 |
int const color = PNM_GET1(xelrow[col]); |
| 126 |
if (!color) |
| 127 |
rastRow[cursor] |= 1 << bitcount; |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
--bitcount; |
| 132 |
if (bitcount < 0) { |
| 133 |
++cursor; |
| 134 |
rastRow[cursor] = 0; |
| 135 |
bitcount = 7; |
| 136 |
} |
| 137 |
} |
| 138 |
*lenP = cursor; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
static void |
| 144 |
doRowDepth8(const xel * const xelrow, |
| 145 |
unsigned char * const rastRow, |
| 146 |
unsigned int const cols, |
| 147 |
int const format, |
| 148 |
xelval const maxval, |
| 149 |
colorhash_table const cht, |
| 150 |
unsigned int * const lenP) { |
| 151 |
|
| 152 |
unsigned int col; |
| 153 |
unsigned int cursor; |
| 154 |
|
| 155 |
for (col = 0, cursor = 0; col < cols; ++col) { |
| 156 |
int color; /* color index of pixel or -1 if not in 'cht' */ |
| 157 |
|
| 158 |
switch (PNM_FORMAT_TYPE(format)) { |
| 159 |
case PPM_TYPE: { |
| 160 |
xel adjustedXel; |
| 161 |
|
| 162 |
if (maxval == 255) |
| 163 |
adjustedXel = xelrow[col]; |
| 164 |
else |
| 165 |
PPM_DEPTH(adjustedXel, xelrow[col], maxval, 255); |
| 166 |
|
| 167 |
color = ppm_lookupcolor(cht, &adjustedXel); |
| 168 |
if (color == -1) |
| 169 |
pm_error("color not found?!? " |
| 170 |
"col=%u r=%u g=%u b=%u", |
| 171 |
col, |
| 172 |
PPM_GETR(adjustedXel), |
| 173 |
PPM_GETG(adjustedXel), |
| 174 |
PPM_GETB(adjustedXel)); |
| 175 |
} break; |
| 176 |
|
| 177 |
case PGM_TYPE: { |
| 178 |
int const rawColor = PNM_GET1(xelrow[col]); |
| 179 |
|
| 180 |
color = maxval == 255 ? rawColor : rawColor * 255 / maxval; |
| 181 |
|
| 182 |
} break; |
| 183 |
|
| 184 |
default: |
| 185 |
color = PNM_GET1(xelrow[col]); |
| 186 |
} |
| 187 |
rastRow[cursor++] = color; |
| 188 |
} |
| 189 |
*lenP = cursor; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static void |
| 195 |
doRowDepth24(const xel * const xelrow, |
| 196 |
unsigned char * const rastRow, |
| 197 |
unsigned int const cols, |
| 198 |
int const format, |
| 199 |
xelval const maxval, |
| 200 |
unsigned int * const lenP) { |
| 201 |
|
| 202 |
/* Since depth is 24, we do NOT have a valid cht. */ |
| 203 |
|
| 204 |
unsigned int col; |
| 205 |
unsigned int cursor; |
| 206 |
|
| 207 |
for (col = 0, cursor = 0; col < cols; ++col) { |
| 208 |
xel adjustedXel; |
| 209 |
|
| 210 |
if (maxval == 255) |
| 211 |
adjustedXel = xelrow[col]; |
| 212 |
else |
| 213 |
PPM_DEPTH(adjustedXel, xelrow[col], maxval, 255); |
| 214 |
|
| 215 |
rastRow[cursor++] = PPM_GETB(adjustedXel); |
| 216 |
rastRow[cursor++] = PPM_GETG(adjustedXel); |
| 217 |
rastRow[cursor++] = PPM_GETR(adjustedXel); |
| 218 |
} |
| 219 |
*lenP = cursor; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
static void |
| 225 |
computeRaster(unsigned char * const rastRaster, |
| 226 |
unsigned int const lineSize, |
| 227 |
unsigned int const depth, |
| 228 |
unsigned int const cols, |
| 229 |
unsigned int const rows, |
| 230 |
int const format, |
| 231 |
xelval const maxval, |
| 232 |
xel ** const xels, |
| 233 |
colorhash_table const cht) { |
| 234 |
|
| 235 |
unsigned int row; |
| 236 |
unsigned char * rastRow; |
| 237 |
|
| 238 |
for (row = 0, rastRow = &rastRaster[0]; row < rows; ++row) { |
| 239 |
xel * const xelrow = xels[row]; |
| 240 |
|
| 241 |
unsigned int len; /* Number of bytes of rast data added to rastRow[] */ |
| 242 |
|
| 243 |
switch (depth) { |
| 244 |
case 1: |
| 245 |
doRowDepth1(xelrow, rastRow, cols, format, maxval, cht, &len); |
| 246 |
break; |
| 247 |
case 8: |
| 248 |
doRowDepth8(xelrow, rastRow, cols, format, maxval, cht, &len); |
| 249 |
break; |
| 250 |
case 24: |
| 251 |
doRowDepth24(xelrow, rastRow, cols, format, maxval, &len); |
| 252 |
break; |
| 253 |
default: |
| 254 |
pm_error("INTERNAL ERROR: impossible depth %u", depth); |
| 255 |
} |
| 256 |
{ |
| 257 |
/* Pad out the line (which has a rounded size) with zeroes so |
| 258 |
the resulting file is repeatable. |
| 259 |
*/ |
| 260 |
unsigned int i; |
| 261 |
for (i = len; i < lineSize; ++i) |
| 262 |
rastRow[i] = 0; |
| 263 |
} |
| 264 |
rastRow += lineSize; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
int |
| 271 |
main(int argc, const char ** argv) { |
| 272 |
|
| 273 |
FILE * ifP; |
| 274 |
xel ** xels; |
| 275 |
xel p; |
| 276 |
colorhist_vector chv; |
| 277 |
colorhash_table cht; |
| 278 |
colormap_t * prColormapP; |
| 279 |
int argn; |
| 280 |
int prType; |
| 281 |
int rows, cols; |
| 282 |
int format; |
| 283 |
unsigned int depth; |
| 284 |
int colorCt; |
| 285 |
xelval maxval; |
| 286 |
struct pixrect * prP; |
| 287 |
const char * const usage = "[-standard|-rle] [pnmfile]"; |
| 288 |
|
| 289 |
pm_proginit(&argc, argv); |
| 290 |
|
| 291 |
argn = 1; |
| 292 |
prType = RT_BYTE_ENCODED; |
| 293 |
|
| 294 |
while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) |
| 295 |
{ |
| 296 |
if ( pm_keymatch( argv[argn], "-standard", 2 ) ) |
| 297 |
prType = RT_STANDARD; |
| 298 |
else if ( pm_keymatch( argv[argn], "-rle", 2 ) ) |
| 299 |
prType = RT_BYTE_ENCODED; |
| 300 |
else |
| 301 |
pm_usage( usage ); |
| 302 |
++argn; |
| 303 |
} |
| 304 |
|
| 305 |
if ( argn != argc ) |
| 306 |
{ |
| 307 |
ifP = pm_openr( argv[argn] ); |
| 308 |
++argn; |
| 309 |
} |
| 310 |
else |
| 311 |
ifP = stdin; |
| 312 |
|
| 313 |
if ( argn != argc ) |
| 314 |
pm_usage( usage ); |
| 315 |
|
| 316 |
xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format); |
| 317 |
|
| 318 |
pm_close(ifP); |
| 319 |
|
| 320 |
/* Figure out the proper depth and colormap. */ |
| 321 |
switch (PNM_FORMAT_TYPE(format)) { |
| 322 |
case PPM_TYPE: |
| 323 |
pm_message("computing colormap..."); |
| 324 |
chv = ppm_computecolorhist(xels, cols, rows, MAXCOLORS, &colorCt); |
| 325 |
if (!chv) { |
| 326 |
pm_message( |
| 327 |
"Too many colors - proceeding to write a 24-bit non-mapped"); |
| 328 |
pm_message( |
| 329 |
"rasterfile. If you want 8 bits, try doing a 'pnmquant %d'.", |
| 330 |
MAXCOLORS); |
| 331 |
depth = 24; |
| 332 |
prType = RT_STANDARD; |
| 333 |
prColormapP = NULL; |
| 334 |
} else { |
| 335 |
pm_message("%u colors found", colorCt); |
| 336 |
|
| 337 |
if (maxval != 255) { |
| 338 |
unsigned int i; |
| 339 |
for (i = 0; i < colorCt; ++i) |
| 340 |
PPM_DEPTH(chv[i].color, chv[i].color, maxval, 255); |
| 341 |
} |
| 342 |
/* Force white to slot 0 and black to slot 1, if possible. */ |
| 343 |
PPM_ASSIGN(p, 255, 255, 255); |
| 344 |
ppm_addtocolorhist(chv, &colorCt, MAXCOLORS, &p, 0, 0); |
| 345 |
PPM_ASSIGN(p, 0, 0, 0); |
| 346 |
ppm_addtocolorhist(chv, &colorCt, MAXCOLORS, &p, 0, 1); |
| 347 |
|
| 348 |
if (colorCt == 2) { |
| 349 |
/* Monochrome */ |
| 350 |
depth = 1; |
| 351 |
prColormapP = NULL; |
| 352 |
} else { |
| 353 |
/* Turn the ppm colormap into the appropriate Sun colormap. */ |
| 354 |
depth = 8; |
| 355 |
prColormapP = makePrColormap(chv, colorCt); |
| 356 |
} |
| 357 |
cht = ppm_colorhisttocolorhash(chv, colorCt); |
| 358 |
ppm_freecolorhist(chv); |
| 359 |
} |
| 360 |
|
| 361 |
break; |
| 362 |
|
| 363 |
case PGM_TYPE: |
| 364 |
depth = 8; |
| 365 |
prColormapP = makeGrayPrColormap(); |
| 366 |
break; |
| 367 |
|
| 368 |
default: |
| 369 |
depth = 1; |
| 370 |
prColormapP = NULL; |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
if (maxval > 255 && depth != 1) |
| 375 |
pm_message( |
| 376 |
"maxval is not 255 - automatically rescaling colors"); |
| 377 |
|
| 378 |
/* Allocate space for the Sun-format image. */ |
| 379 |
prP = mem_create(cols, rows, depth); |
| 380 |
if (!prP) |
| 381 |
pm_error("unable to create new pixrect"); |
| 382 |
|
| 383 |
computeRaster(prP->pr_data->md_image, |
| 384 |
prP->pr_data->md_linebytes, |
| 385 |
depth, |
| 386 |
cols, rows, format, maxval, xels, cht); |
| 387 |
|
| 388 |
pnm_freearray(xels, rows); |
| 389 |
|
| 390 |
{ |
| 391 |
int rc; |
| 392 |
|
| 393 |
rc = pr_dump(prP, stdout, prColormapP, prType, 0); |
| 394 |
if (rc == PIX_ERR ) |
| 395 |
pm_error("error writing rasterfile"); |
| 396 |
} |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|