| 1 |
/* lispmtopgm.c - read a file written by the tv:write-bit-array-file function |
| 2 |
** of TI Explorer and Symbolics Lisp Machines, and write a PGM. |
| 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. Since the pgm reader can also read pbms, |
| 16 |
** this doesn't matter if you're using only single plane images. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
#include <limits.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "nstring.h" |
| 25 |
#include "pgm.h" |
| 26 |
|
| 27 |
#define LISPM_MAGIC "This is a BitMap file" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static long item, bitmask; |
| 32 |
static unsigned int bitsperitem, maxbitsperitem, bitshift; |
| 33 |
|
| 34 |
static unsigned int |
| 35 |
wordSizeFmDepth(unsigned int const depth) { |
| 36 |
/*---------------------------------------------------------------------------- |
| 37 |
Lispm architecture specific - if a bitmap is written out with a depth of 5, |
| 38 |
it really has a depth of 8, and is stored that way in the file. |
| 39 |
-----------------------------------------------------------------------------*/ |
| 40 |
if (depth==0 || depth==1) return 1; |
| 41 |
else if (depth == 2) return 2; |
| 42 |
else if (depth <= 4) return 4; |
| 43 |
else if (depth <= 8) return 8; |
| 44 |
else if (depth <= 16) return 16; |
| 45 |
else if (depth <= 32) return 32; |
| 46 |
else { |
| 47 |
pm_error("depth was %u, which is not in the range 1-32.", depth); |
| 48 |
assert(false); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
getinit(FILE * const ifP, |
| 56 |
unsigned int * const colsP, |
| 57 |
unsigned int * const rowsP, |
| 58 |
unsigned int * const depthP, |
| 59 |
unsigned int * const padrightP) { |
| 60 |
|
| 61 |
short cols, rows, cols32; |
| 62 |
char magic[sizeof(LISPM_MAGIC)]; |
| 63 |
unsigned int i; |
| 64 |
|
| 65 |
for (i = 0; i < sizeof(magic)-1; ++i) |
| 66 |
magic[i] = getc(ifP); |
| 67 |
|
| 68 |
magic[i]='\0'; |
| 69 |
|
| 70 |
if (!streq(LISPM_MAGIC, magic)) |
| 71 |
pm_error("bad id string in Lispm file"); |
| 72 |
|
| 73 |
pm_readlittleshort(ifP, &cols); |
| 74 |
pm_readlittleshort(ifP, &rows); |
| 75 |
pm_readlittleshort(ifP, &cols32); |
| 76 |
|
| 77 |
*colsP = cols; |
| 78 |
*rowsP = rows; |
| 79 |
|
| 80 |
*depthP = getc(ifP); |
| 81 |
|
| 82 |
if (*depthP == 0) |
| 83 |
*depthP = 1; /* very old file */ |
| 84 |
|
| 85 |
assert(*colsP < UINT_MAX - 31); |
| 86 |
|
| 87 |
*padrightP = ROUNDUP(*colsP, 32) - *colsP; |
| 88 |
|
| 89 |
# if 0 |
| 90 |
if (*colsP != (cols32 - *padrightP)) { |
| 91 |
pm_message("inconsistent input: " |
| 92 |
"Width and Width(mod32) fields don't agree" ); |
| 93 |
*padrightP = cols32 - *colsP; /* hmmmm.... */ |
| 94 |
|
| 95 |
/* This is a dilemma. Usually the output is rounded up to mod32, but |
| 96 |
not always. For the Lispm code to not round up, the array size |
| 97 |
must be the same size as the portion being written - that is, the |
| 98 |
array itself must be an odd size, not just the selected portion. |
| 99 |
Since arrays that are odd sizes can't be handed to bitblt, such |
| 100 |
arrays are probably not image data - so punt on it for now. |
| 101 |
|
| 102 |
Also, the lispm code for saving bitmaps has a bug, in that if you |
| 103 |
are writing a bitmap which is not mod32 across, the file may be up |
| 104 |
to 7 bits too short! They round down instead of up. |
| 105 |
|
| 106 |
The code in 'pgmtolispm.c' always rounds up to mod32, which is |
| 107 |
totally reasonable. |
| 108 |
*/ |
| 109 |
} |
| 110 |
#endif |
| 111 |
bitsperitem = 0; |
| 112 |
maxbitsperitem = wordSizeFmDepth(*depthP); |
| 113 |
bitmask = (1 << maxbitsperitem) - 1; /* for depth=3, mask=00000111 */ |
| 114 |
|
| 115 |
for (i = 0; i < 9; ++i) |
| 116 |
getc(ifP); /* discard bytes reserved for future use */ |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static unsigned int |
| 122 |
getval(FILE * const ifP) { |
| 123 |
|
| 124 |
unsigned int b; |
| 125 |
|
| 126 |
if (bitsperitem == 0) { |
| 127 |
pm_readlittlelong(ifP, &item); |
| 128 |
bitsperitem = 32; |
| 129 |
bitshift = 0; |
| 130 |
item = ~item; |
| 131 |
} |
| 132 |
b = ((item >> bitshift ) & bitmask); |
| 133 |
bitsperitem = bitsperitem - maxbitsperitem; |
| 134 |
bitshift = bitshift + maxbitsperitem; |
| 135 |
return b; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
int |
| 141 |
main(int argc, const char * argv[]) { |
| 142 |
|
| 143 |
FILE * ifP; |
| 144 |
gray * grayrow; |
| 145 |
unsigned int rows, cols, depth, padright; |
| 146 |
unsigned int row; |
| 147 |
gray maxval; |
| 148 |
|
| 149 |
|
| 150 |
pm_proginit(&argc, argv); |
| 151 |
|
| 152 |
if (argc-1 > 1) |
| 153 |
pm_error("Too many arguments. The only possible argument is the " |
| 154 |
"input file name"); |
| 155 |
|
| 156 |
if (argc-1 == 1) |
| 157 |
ifP = pm_openr(argv[1]); |
| 158 |
else |
| 159 |
ifP = stdin; |
| 160 |
|
| 161 |
getinit(ifP, &cols, &rows, &depth, &padright); |
| 162 |
|
| 163 |
if (depth > 16) |
| 164 |
pm_error("Invalid depth (%u bits). Maximum is 15", depth); |
| 165 |
|
| 166 |
maxval = (1 << depth); |
| 167 |
|
| 168 |
pgm_writepgminit(stdout, cols, rows, maxval, 0); |
| 169 |
|
| 170 |
grayrow = pgm_allocrow(ROUNDUP(cols, 8)); |
| 171 |
|
| 172 |
for (row = 0; row < rows; ++row) { |
| 173 |
unsigned int col; |
| 174 |
|
| 175 |
for (col = 0; col < cols; ++col) |
| 176 |
grayrow[col] = getval(ifP); |
| 177 |
|
| 178 |
pgm_writepgmrow(stdout, grayrow, cols, maxval, 0); |
| 179 |
} |
| 180 |
pm_close(ifP); |
| 181 |
pm_close(stdout); |
| 182 |
exit(0); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|