| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#define _DEFAULT_SOURCE 1 |
| 14 |
#define _BSD_SOURCE 1 |
| 15 |
#define _XOPEN_SOURCE 500 |
| 16 |
|
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "pm_c_util.h" |
| 20 |
#include "mallocvar.h" |
| 21 |
#include "ppm.h" |
| 22 |
#include "xim.h" |
| 23 |
#include "shhopt.h" |
| 24 |
#include "nstring.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
struct CmdlineInfo { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
const char * inputFilename; |
| 33 |
const char * alphaFilename; |
| 34 |
bool alphaStdout; |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static void |
| 40 |
parseCommandLine(int argc, const char ** argv, |
| 41 |
struct CmdlineInfo *cmdlineP) { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
optEntry option_def[100]; |
| 49 |
optStruct3 opt; |
| 50 |
|
| 51 |
unsigned int option_def_index; |
| 52 |
|
| 53 |
unsigned int alphaoutSpec; |
| 54 |
|
| 55 |
option_def_index = 0; |
| 56 |
OPTENT3(0, "alphaout", OPT_STRING, |
| 57 |
&cmdlineP->alphaFilename, &alphaoutSpec, 0); |
| 58 |
|
| 59 |
opt.opt_table = option_def; |
| 60 |
opt.short_allowed = false; |
| 61 |
opt.allowNegNum = false; |
| 62 |
|
| 63 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 64 |
|
| 65 |
|
| 66 |
if (!alphaoutSpec) |
| 67 |
cmdlineP->alphaFilename = NULL; |
| 68 |
|
| 69 |
if (argc - 1 == 0) |
| 70 |
cmdlineP->inputFilename = "-"; |
| 71 |
else if (argc - 1 == 1) |
| 72 |
cmdlineP->inputFilename = pm_strdup(argv[1]); |
| 73 |
else |
| 74 |
pm_error("Too many arguments. The only argument accepted " |
| 75 |
"is the input file specification"); |
| 76 |
|
| 77 |
if (cmdlineP->alphaFilename && |
| 78 |
streq(cmdlineP->alphaFilename, "-")) |
| 79 |
cmdlineP->alphaStdout = true; |
| 80 |
else |
| 81 |
cmdlineP->alphaStdout = false; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static int |
| 91 |
readXimHeader(FILE * const ifP, |
| 92 |
XimImage * const headerP) { |
| 93 |
|
| 94 |
unsigned int i; |
| 95 |
XimAsciiHeader aHead; |
| 96 |
|
| 97 |
{ |
| 98 |
unsigned char * cp; |
| 99 |
cp = (unsigned char *)headerP; |
| 100 |
for (i = 0; i < sizeof(XimImage); ++i) |
| 101 |
*cp++ = 0; |
| 102 |
} |
| 103 |
|
| 104 |
if (fread((char *)&aHead, sizeof(ImageHeader), 1, ifP) != 1) { |
| 105 |
pm_message("ReadXimHeader: unable to read file header" ); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
aHead.author [sizeof(aHead.author) - 1] = '\0'; |
| 110 |
aHead.date [sizeof(aHead.date) - 1] = '\0'; |
| 111 |
aHead.program[sizeof(aHead.program) - 1] = '\0'; |
| 112 |
|
| 113 |
if (atoi(aHead.header_size) != sizeof(ImageHeader)) { |
| 114 |
pm_message("ReadXimHeader: header size mismatch" ); |
| 115 |
return(0); |
| 116 |
} |
| 117 |
if (atoi(aHead.file_version) != IMAGE_VERSION) { |
| 118 |
pm_message("ReadXimHeader: incorrect Image_file version" ); |
| 119 |
return(0); |
| 120 |
} |
| 121 |
headerP->width = atoi(aHead.image_width); |
| 122 |
headerP->height = atoi(aHead.image_height); |
| 123 |
headerP->ncolors = atoi(aHead.num_colors); |
| 124 |
headerP->nchannels = atoi(aHead.num_channels); |
| 125 |
headerP->bytes_per_line = atoi(aHead.bytes_per_line); |
| 126 |
#if 0 |
| 127 |
headerP->npics = atoi(aHead.num_pictures); |
| 128 |
#endif |
| 129 |
headerP->bits_channel = atoi(aHead.bits_per_channel); |
| 130 |
headerP->alpha_flag = atoi(aHead.alpha_channel); |
| 131 |
headerP->author = pm_strdup(aHead.author); |
| 132 |
headerP->date = pm_strdup(aHead.date); |
| 133 |
headerP->program = pm_strdup(aHead.program); |
| 134 |
|
| 135 |
|
| 136 |
if (headerP->npics == 0) |
| 137 |
headerP->npics = 1; |
| 138 |
if (headerP->bits_channel == 0) |
| 139 |
headerP->bits_channel = 8; |
| 140 |
else if (headerP->bits_channel == 24) { |
| 141 |
headerP->nchannels = 3; |
| 142 |
headerP->bits_channel = 8; |
| 143 |
} |
| 144 |
if (headerP->bytes_per_line == 0) |
| 145 |
headerP->bytes_per_line = |
| 146 |
(headerP->bits_channel == 1 && headerP->nchannels == 1) ? |
| 147 |
(headerP->width + 7) / 8 : |
| 148 |
headerP->width; |
| 149 |
headerP->datasize = |
| 150 |
(unsigned int)headerP->bytes_per_line * headerP->height; |
| 151 |
if (headerP->nchannels == 3 && headerP->bits_channel == 8) |
| 152 |
headerP->ncolors = 0; |
| 153 |
else if (headerP->nchannels == 1 && headerP->bits_channel == 8) { |
| 154 |
unsigned int i; |
| 155 |
|
| 156 |
MALLOCARRAY_NOFAIL(headerP->colors, headerP->ncolors); |
| 157 |
|
| 158 |
for (i=0; i < headerP->ncolors; ++i) { |
| 159 |
headerP->colors[i].red = aHead.c_map[i][0]; |
| 160 |
headerP->colors[i].grn = aHead.c_map[i][1]; |
| 161 |
headerP->colors[i].blu = aHead.c_map[i][2]; |
| 162 |
} |
| 163 |
} |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static int |
| 170 |
readImageChannel(FILE * const ifP, |
| 171 |
byte * const buf, |
| 172 |
unsigned int * const bufsizeP, |
| 173 |
bool const encoded) { |
| 174 |
|
| 175 |
unsigned int j; |
| 176 |
long marker; |
| 177 |
|
| 178 |
if (!encoded) |
| 179 |
j = fread((char *)buf, 1, (int)*bufsizeP, ifP); |
| 180 |
else { |
| 181 |
byte * line; |
| 182 |
|
| 183 |
MALLOCARRAY(line, BUFSIZ); |
| 184 |
if (!line) { |
| 185 |
pm_message("ReadImageChannel: can't malloc() fread string" ); |
| 186 |
return 0; |
| 187 |
} else { |
| 188 |
|
| 189 |
unsigned int byteCt; |
| 190 |
|
| 191 |
marker = ftell(ifP); |
| 192 |
j = 0; |
| 193 |
while (((byteCt = fread((char *)line, 1, BUFSIZ, ifP)) > 0) && |
| 194 |
(j < *bufsizeP)) { |
| 195 |
unsigned int i; |
| 196 |
for (i=0; (i < byteCt) && (j < *bufsizeP); ++i) { |
| 197 |
unsigned int runlen; |
| 198 |
runlen = (unsigned int)line[i] + 1; |
| 199 |
++i; |
| 200 |
while (runlen--) |
| 201 |
buf[j++] = line[i]; |
| 202 |
} |
| 203 |
marker += i; |
| 204 |
} |
| 205 |
|
| 206 |
if (fseek(ifP, marker, 0) == -1) { |
| 207 |
pm_message("ReadImageChannel: can't fseek to location " |
| 208 |
"in image buffer"); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
free(line); |
| 212 |
} |
| 213 |
} |
| 214 |
if (j != *bufsizeP) { |
| 215 |
pm_message("unable to complete channel: %u / %u (%f%%)", |
| 216 |
j, *bufsizeP, j * 100.0 / *bufsizeP); |
| 217 |
*bufsizeP = j; |
| 218 |
} |
| 219 |
return 1; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
static int |
| 225 |
readXimImage(FILE * const ifP, |
| 226 |
XimImage * const ximP) { |
| 227 |
|
| 228 |
if (ximP->data) { |
| 229 |
free(ximP->data); |
| 230 |
ximP->data = NULL; |
| 231 |
} |
| 232 |
if (ximP->grn_data) { |
| 233 |
free(ximP->grn_data); |
| 234 |
ximP->grn_data = NULL; |
| 235 |
} |
| 236 |
if (ximP->blu_data) { |
| 237 |
free(ximP->blu_data); |
| 238 |
ximP->blu_data = NULL; |
| 239 |
} |
| 240 |
if (ximP->other) { |
| 241 |
free(ximP->other); |
| 242 |
ximP->other = NULL; |
| 243 |
} |
| 244 |
ximP->npics = 0; |
| 245 |
MALLOCARRAY(ximP->data, ximP->datasize); |
| 246 |
if (!ximP->data) { |
| 247 |
pm_message("ReadXimImage: can't malloc pixmap data"); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
if (!readImageChannel(ifP, ximP->data, &ximP->datasize, false)) { |
| 251 |
pm_message("ReadXimImage: end of the images"); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
if (ximP->nchannels == 3) { |
| 255 |
MALLOCARRAY(ximP->grn_data, ximP->datasize); |
| 256 |
MALLOCARRAY(ximP->blu_data, ximP->datasize); |
| 257 |
if (!ximP->grn_data || !ximP->blu_data) { |
| 258 |
pm_message("ReadXimImage: can't malloc rgb channel data"); |
| 259 |
free(ximP->data); |
| 260 |
if (ximP->grn_data) |
| 261 |
free(ximP->grn_data); |
| 262 |
if (ximP->blu_data) |
| 263 |
free(ximP->blu_data); |
| 264 |
ximP->data = ximP->grn_data = ximP->blu_data = NULL; |
| 265 |
return 0; |
| 266 |
} |
| 267 |
if (!readImageChannel(ifP, ximP->grn_data, &ximP->datasize, false)) |
| 268 |
return 0; |
| 269 |
if (!readImageChannel(ifP, ximP->blu_data, &ximP->datasize, false)) |
| 270 |
return 0; |
| 271 |
} else if (ximP->nchannels > 3) { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
MALLOCARRAY(ximP->other, ximP->datasize); |
| 277 |
if (!ximP->other) { |
| 278 |
pm_message("ReadXimImage: can't malloc alpha data"); |
| 279 |
return 0; |
| 280 |
} |
| 281 |
if (!readImageChannel(ifP, ximP->other, &ximP->datasize, false)) |
| 282 |
return(0); |
| 283 |
} |
| 284 |
ximP->npics = 1; |
| 285 |
|
| 286 |
return 1; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
static int |
| 322 |
readXim(FILE * const ifP, |
| 323 |
XimImage * const ximP) { |
| 324 |
|
| 325 |
int retval; |
| 326 |
|
| 327 |
if (!readXimHeader(ifP, ximP)) { |
| 328 |
pm_message("can't read xim header"); |
| 329 |
retval = 0; |
| 330 |
} else if (!readXimImage(ifP, ximP)) { |
| 331 |
pm_message("can't read xim data"); |
| 332 |
retval = 0; |
| 333 |
} else |
| 334 |
retval = 1; |
| 335 |
|
| 336 |
return retval; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
int |
| 342 |
main(int argc, |
| 343 |
const char **argv) { |
| 344 |
|
| 345 |
struct CmdlineInfo cmdline; |
| 346 |
FILE * ifP; |
| 347 |
FILE * imageoutFileP; |
| 348 |
FILE * alphaFileP; |
| 349 |
XimImage xim; |
| 350 |
pixel * pixelrow; |
| 351 |
pixel colormap[256]; |
| 352 |
gray * alpharow; |
| 353 |
|
| 354 |
|
| 355 |
unsigned int rows, cols; |
| 356 |
unsigned int row; |
| 357 |
bool mapped; |
| 358 |
pixval maxval; |
| 359 |
bool succeeded; |
| 360 |
|
| 361 |
pm_proginit(&argc, argv); |
| 362 |
|
| 363 |
parseCommandLine(argc, argv, &cmdline); |
| 364 |
|
| 365 |
ifP = pm_openr(cmdline.inputFilename); |
| 366 |
|
| 367 |
if (cmdline.alphaStdout) |
| 368 |
alphaFileP = stdout; |
| 369 |
else if (cmdline.alphaFilename == NULL) |
| 370 |
alphaFileP = NULL; |
| 371 |
else |
| 372 |
alphaFileP = pm_openw(cmdline.alphaFilename); |
| 373 |
|
| 374 |
if (cmdline.alphaStdout) |
| 375 |
imageoutFileP = NULL; |
| 376 |
else |
| 377 |
imageoutFileP = stdout; |
| 378 |
|
| 379 |
succeeded = readXim(ifP, &xim); |
| 380 |
if (!succeeded) |
| 381 |
pm_error("can't read Xim file"); |
| 382 |
|
| 383 |
rows = xim.height; |
| 384 |
cols = xim.width; |
| 385 |
|
| 386 |
if (xim.nchannels == 1 && xim.bits_channel == 8) { |
| 387 |
unsigned int i; |
| 388 |
|
| 389 |
mapped = true; |
| 390 |
maxval = 255; |
| 391 |
for (i = 0; i < xim.ncolors; ++i) { |
| 392 |
PPM_ASSIGN( |
| 393 |
colormap[i], xim.colors[i].red, |
| 394 |
xim.colors[i].grn, xim.colors[i].blu ); |
| 395 |
|
| 396 |
} |
| 397 |
} else if (xim.nchannels == 3 || xim.nchannels == 4) { |
| 398 |
mapped = false; |
| 399 |
maxval = pm_bitstomaxval(xim.bits_channel); |
| 400 |
} else |
| 401 |
pm_error( |
| 402 |
"unknown Xim file type, nchannels == %d, bits_channel == %d", |
| 403 |
xim.nchannels, xim.bits_channel); |
| 404 |
|
| 405 |
if (imageoutFileP) |
| 406 |
ppm_writeppminit(imageoutFileP, cols, rows, maxval, 0); |
| 407 |
if (alphaFileP) |
| 408 |
pgm_writepgminit(alphaFileP, cols, rows, maxval, 0); |
| 409 |
|
| 410 |
pixelrow = ppm_allocrow(cols); |
| 411 |
alpharow = pgm_allocrow(cols); |
| 412 |
|
| 413 |
for (row = 0; row < rows; ++row) { |
| 414 |
if (mapped) { |
| 415 |
byte * const ximrow = &xim.data[row * xim.bytes_per_line]; |
| 416 |
|
| 417 |
unsigned int col; |
| 418 |
|
| 419 |
for (col = 0; col < cols; ++col) |
| 420 |
pixelrow[col] = colormap[ximrow[col]]; |
| 421 |
|
| 422 |
alpharow[col] = 0; |
| 423 |
} else { |
| 424 |
byte * const redrow = &xim.data [row * xim.bytes_per_line]; |
| 425 |
byte * const grnrow = &xim.grn_data [row * xim.bytes_per_line]; |
| 426 |
byte * const blurow = &xim.blu_data [row * xim.bytes_per_line]; |
| 427 |
byte * const othrow = &xim.other [row * xim.bytes_per_line]; |
| 428 |
|
| 429 |
unsigned int col; |
| 430 |
|
| 431 |
for (col = 0; col < cols; ++col) { |
| 432 |
PPM_ASSIGN(pixelrow[col], |
| 433 |
redrow[col], grnrow[col], blurow[col]); |
| 434 |
if (xim.nchannels > 3) |
| 435 |
alpharow[col] = othrow[col]; |
| 436 |
else |
| 437 |
alpharow[col] = 0; |
| 438 |
} |
| 439 |
} |
| 440 |
if (imageoutFileP) |
| 441 |
ppm_writeppmrow(imageoutFileP, pixelrow, cols, maxval, 0); |
| 442 |
if (alphaFileP) |
| 443 |
pgm_writepgmrow(alphaFileP, alpharow, cols, maxval, 0); |
| 444 |
} |
| 445 |
pm_close(ifP); |
| 446 |
if (imageoutFileP) |
| 447 |
pm_close(imageoutFileP); |
| 448 |
if (alphaFileP) |
| 449 |
pm_close(alphaFileP); |
| 450 |
|
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|