| 1 |
#include <stdio.h> |
| 2 |
|
| 3 |
#include "pbm.h" |
| 4 |
|
| 5 |
/* By Bryan Henderson, San Jose CA 2003.09.06. |
| 6 |
|
| 7 |
Contributed to the public domain by its author. |
| 8 |
|
| 9 |
This is a replacement of Joseph Sheedy's ([email protected]) Perl |
| 10 |
program of the same name, distributed in his Pbmtomatrixorbital |
| 11 |
package. This version uses Netpbm libraries and is fully |
| 12 |
consistent with other Netpbm programs. |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
static void |
| 17 |
generateMo(FILE * const ofP, |
| 18 |
bit ** const bits, |
| 19 |
unsigned int const cols, |
| 20 |
unsigned int const rows) { |
| 21 |
|
| 22 |
unsigned int col; |
| 23 |
|
| 24 |
fputc(cols, ofP); |
| 25 |
fputc(rows, ofP); |
| 26 |
|
| 27 |
for (col = 0; col < cols; ++col) { |
| 28 |
unsigned int row; |
| 29 |
unsigned int outbitpos; |
| 30 |
unsigned char outchar; |
| 31 |
|
| 32 |
outbitpos = 0; /* Start at 1st bit of 1st output byte */ |
| 33 |
|
| 34 |
for (row = 0; row < rows; ++row) { |
| 35 |
if (outbitpos == 0) |
| 36 |
/* We're starting a new byte; initialize it to zeroes */ |
| 37 |
outchar = 0; |
| 38 |
|
| 39 |
outchar |= bits[row][col] << outbitpos; |
| 40 |
|
| 41 |
if (outbitpos == 7) |
| 42 |
/* We filled up a byte. Output it. */ |
| 43 |
fputc(outchar, ofP); |
| 44 |
|
| 45 |
outbitpos = (outbitpos + 1) % 8; |
| 46 |
} |
| 47 |
if (outbitpos != 0) |
| 48 |
/* Our last byte is partial, so must be output now. */ |
| 49 |
fputc(outchar, ofP); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
int |
| 56 |
main(int argc, const char ** argv) { |
| 57 |
|
| 58 |
FILE * ifP; |
| 59 |
bit ** bits; |
| 60 |
int rows, cols; |
| 61 |
const char * inputFilename; |
| 62 |
|
| 63 |
pm_proginit(&argc, argv); |
| 64 |
|
| 65 |
if (argc-1 > 1) |
| 66 |
pm_error("Too many arguments (%u). The only valid argument is an " |
| 67 |
"input file name.", argc-1); |
| 68 |
else if (argc-1 == 1) |
| 69 |
inputFilename = argv[1]; |
| 70 |
else |
| 71 |
inputFilename = "-"; |
| 72 |
|
| 73 |
ifP = pm_openr(inputFilename); |
| 74 |
|
| 75 |
bits = pbm_readpbm(ifP, &cols, &rows); |
| 76 |
|
| 77 |
if (rows > 255) |
| 78 |
pm_error("Image is too high: %u rows. Max height: 255 rows", rows); |
| 79 |
if (cols > 255) |
| 80 |
pm_error("Image is too wide: %u cols. Max width: 255 cols", cols); |
| 81 |
|
| 82 |
generateMo(stdout, bits, cols, rows); |
| 83 |
|
| 84 |
pm_close(ifP); |
| 85 |
|
| 86 |
pbm_freearray(bits, rows); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|