| 1 |
/* pbmtoybm.c - read a pbm and write a file for Bennet Yee's 'xbm' and 'face' |
| 2 |
** programs. |
| 3 |
** |
| 4 |
** Written by Jamie Zawinski based on code (C) 1988 by Jef Poskanzer. |
| 5 |
** |
| 6 |
** Permission to use, copy, modify, and distribute this software and its |
| 7 |
** documentation for any purpose and without fee is hereby granted, provided |
| 8 |
** that the above copyright notice appear in all copies and that both that |
| 9 |
** copyright notice and this permission notice appear in supporting |
| 10 |
** documentation. This software is provided "as is" without express or |
| 11 |
** implied warranty. |
| 12 |
** |
| 13 |
** Feb 2010 afu |
| 14 |
** Added dimension check to prevent short int from overflowing |
| 15 |
** Changed code style (ANSI-style function definitions, etc.) |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
#include "pm.h" |
| 21 |
#include "pbm.h" |
| 22 |
#include "bitreverse.h" |
| 23 |
|
| 24 |
#define YBM_MAGIC ( ( '!' << 8 ) | '!' ) |
| 25 |
#define INT16MAX 32767 |
| 26 |
|
| 27 |
static void |
| 28 |
putinit(int const cols, |
| 29 |
int const rows) { |
| 30 |
|
| 31 |
pm_writebigshort(stdout, YBM_MAGIC); |
| 32 |
pm_writebigshort(stdout, cols); |
| 33 |
pm_writebigshort(stdout, rows); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
int |
| 39 |
main(int argc, const char *argv[]) { |
| 40 |
|
| 41 |
FILE * ifP; |
| 42 |
bit * bitrow; |
| 43 |
int rows; |
| 44 |
int cols; |
| 45 |
int format; |
| 46 |
unsigned int row; |
| 47 |
const char * inputFileName; |
| 48 |
|
| 49 |
pm_proginit(&argc, argv); |
| 50 |
|
| 51 |
if (argc-1 < 1) |
| 52 |
inputFileName = "-"; |
| 53 |
else { |
| 54 |
inputFileName = argv[1]; |
| 55 |
|
| 56 |
if (argc-1 > 1) |
| 57 |
pm_error("Too many arguments. The only argument is the optional " |
| 58 |
"input file name"); |
| 59 |
} |
| 60 |
|
| 61 |
ifP = pm_openr(inputFileName); |
| 62 |
|
| 63 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 64 |
|
| 65 |
if (rows > INT16MAX || cols > INT16MAX) |
| 66 |
pm_error("Input image is too large."); |
| 67 |
|
| 68 |
bitrow = pbm_allocrow_packed(cols + 8); |
| 69 |
|
| 70 |
putinit(cols, rows); |
| 71 |
|
| 72 |
bitrow[pbm_packed_bytes(cols + 8) - 1] = 0x00; |
| 73 |
for (row = 0; row < rows; ++row) { |
| 74 |
uint16_t * const itemrow = (uint16_t *) bitrow; |
| 75 |
unsigned int const itemCt = (cols + 15) / 16; |
| 76 |
|
| 77 |
unsigned int i; |
| 78 |
|
| 79 |
pbm_readpbmrow_packed(ifP, bitrow, cols, format); |
| 80 |
pbm_cleanrowend_packed(bitrow, cols); |
| 81 |
|
| 82 |
for (i = 0; i < pbm_packed_bytes(cols); ++i) |
| 83 |
bitrow[i] = bitreverse[bitrow[i]]; |
| 84 |
|
| 85 |
for (i = 0; i < itemCt; ++i) |
| 86 |
pm_writebigshort(stdout, itemrow[i]); |
| 87 |
} |
| 88 |
|
| 89 |
pbm_freerow_packed(bitrow); |
| 90 |
|
| 91 |
if (ifP != stdin) |
| 92 |
fclose(ifP); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|