| 1 |
/* pgmtolispm.c - read a pgm and write a file acceptable to the |
| 2 |
** tv:read-bit-array-file function of TI Explorer and Symbolics Lisp Machines. |
| 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 |
** When one writes a multi-plane bitmap with tv:write-bit-array-file, it is |
| 14 |
** usually a color image; but a color map is not written in the file, so we |
| 15 |
** treat this as a graymap instead. To convert a color image to Lispm |
| 16 |
** format, you must convert it to a pgm, and hand-edit a color map... Ick. |
| 17 |
** |
| 18 |
** Feb 2010 afu |
| 19 |
** Added dimension check to prevent short int from overflowing |
| 20 |
** Changed code style (ANSI-style function definitions, etc.) |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "pm.h" |
| 24 |
#include "pgm.h" |
| 25 |
|
| 26 |
#define LISPM_MAGIC "This is a BitMap file" |
| 27 |
#define INT16MAX 32767 |
| 28 |
|
| 29 |
|
| 30 |
static unsigned int item; |
| 31 |
static unsigned int bitsperitem, maxbitsperitem, bitshift; |
| 32 |
|
| 33 |
static unsigned int |
| 34 |
depth_to_word_size(unsigned int const depth) { |
| 35 |
|
| 36 |
/* Lispm architecture specific - if a bitmap is written */ |
| 37 |
/* out with a depth of 5, it really has a depth of 8, and */ |
| 38 |
/* is stored that way in the file. */ |
| 39 |
|
| 40 |
unsigned int const wordSize = |
| 41 |
depth == 1 ? 1 : |
| 42 |
depth == 2 ? 2 : |
| 43 |
depth <= 4 ? 4 : |
| 44 |
depth <= 8 ? 8 : |
| 45 |
depth <= 16 ? 16 : |
| 46 |
depth <= 32 ? 32 : |
| 47 |
0; |
| 48 |
|
| 49 |
if (wordSize == 0) |
| 50 |
pm_error("depth was %u, which is not in the range 1-32", depth); |
| 51 |
|
| 52 |
return wordSize; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static void |
| 58 |
putinit(unsigned int const cols, |
| 59 |
unsigned int const rows, |
| 60 |
unsigned int const depth) { |
| 61 |
|
| 62 |
unsigned int const cols32 = ((cols + 31 ) / 32) * 32; |
| 63 |
|
| 64 |
unsigned int i; |
| 65 |
|
| 66 |
/* Lispms are able to write bit files that are not mod32 wide, but we */ |
| 67 |
/* don't. This should be ok, since bit arrays which are not mod32 wide */ |
| 68 |
/* are pretty useless on a lispm (can't hand them to bitblt). */ |
| 69 |
|
| 70 |
if (rows > INT16MAX || cols > INT16MAX || cols32 > INT16MAX) |
| 71 |
pm_error("Input image is too large."); |
| 72 |
|
| 73 |
printf(LISPM_MAGIC); |
| 74 |
|
| 75 |
pm_writelittleshort(stdout, cols); |
| 76 |
pm_writelittleshort(stdout, rows); |
| 77 |
pm_writelittleshort(stdout, cols32); |
| 78 |
putchar(depth & 0xFF); |
| 79 |
|
| 80 |
for (i = 0; i < 9; ++i) |
| 81 |
putchar(0); /* pad bytes */ |
| 82 |
|
| 83 |
item = 0; |
| 84 |
bitsperitem = 0; |
| 85 |
maxbitsperitem = depth_to_word_size(depth); |
| 86 |
bitshift = 0; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static void |
| 92 |
putitem(void) { |
| 93 |
|
| 94 |
pm_writelittlelong(stdout, ~item); |
| 95 |
|
| 96 |
item = 0; |
| 97 |
bitsperitem = 0; |
| 98 |
bitshift = 0; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static void |
| 104 |
putval(gray const b) { |
| 105 |
|
| 106 |
if (bitsperitem == 32) |
| 107 |
putitem(); |
| 108 |
|
| 109 |
item = item | (b << bitshift); |
| 110 |
bitsperitem = bitsperitem + maxbitsperitem; |
| 111 |
bitshift = bitshift + maxbitsperitem; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static void |
| 117 |
putrest(void) { |
| 118 |
|
| 119 |
if (bitsperitem > 0) |
| 120 |
putitem(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
int |
| 126 |
main(int argc, const char * argv[]) { |
| 127 |
|
| 128 |
FILE * ifP; |
| 129 |
gray * grayrow; |
| 130 |
int rows; |
| 131 |
int cols; |
| 132 |
unsigned int depth; |
| 133 |
int format; |
| 134 |
unsigned int padright; |
| 135 |
unsigned int row; |
| 136 |
gray maxval; |
| 137 |
const char * inputFile; |
| 138 |
|
| 139 |
pm_proginit(&argc, argv); |
| 140 |
|
| 141 |
if (argc-1 < 1) |
| 142 |
inputFile = "-"; |
| 143 |
else { |
| 144 |
inputFile = argv[1]; |
| 145 |
|
| 146 |
if (argc-1 > 2) |
| 147 |
pm_error("Too many arguments. The only argument is the optional " |
| 148 |
"input file name"); |
| 149 |
} |
| 150 |
|
| 151 |
ifP = pm_openr(inputFile); |
| 152 |
|
| 153 |
pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); |
| 154 |
|
| 155 |
grayrow = pgm_allocrow(cols); |
| 156 |
depth = pm_maxvaltobits(maxval); |
| 157 |
|
| 158 |
/* Compute padding to round cols up to the nearest multiple of 32. */ |
| 159 |
padright = ((cols + 31) / 32) * 32 - cols; |
| 160 |
|
| 161 |
putinit(cols, rows, depth); |
| 162 |
|
| 163 |
for (row = 0; row < rows; ++row) { |
| 164 |
unsigned int col; |
| 165 |
|
| 166 |
pgm_readpgmrow(ifP, grayrow, cols, maxval, format); |
| 167 |
|
| 168 |
for (col = 0; col < cols; ++col) |
| 169 |
putval(grayrow[col]); |
| 170 |
|
| 171 |
for (col = 0; col < padright; ++col) |
| 172 |
putval(0); |
| 173 |
} |
| 174 |
|
| 175 |
pm_close(ifP); |
| 176 |
|
| 177 |
putrest(); |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|