| 1 |
/* pnmtosgi.c - convert portable anymap to SGI image |
| 2 |
** |
| 3 |
** Copyright (C) 1994 by Ingo Wilken ([email protected]) |
| 4 |
** |
| 5 |
** Based on the SGI image description v0.9 by Paul Haeberli ([email protected]) |
| 6 |
** Available via ftp from sgi.com:graphics/SGIIMAGESPEC |
| 7 |
** |
| 8 |
** Permission to use, copy, modify, and distribute this software and its |
| 9 |
** documentation for any purpose and without fee is hereby granted, provided |
| 10 |
** that the above copyright notice appear in all copies and that both that |
| 11 |
** copyright notice and this permission notice appear in supporting |
| 12 |
** documentation. This software is provided "as is" without express or |
| 13 |
** implied warranty. |
| 14 |
** |
| 15 |
** 29Jan94: first version |
| 16 |
|
| 17 |
** Feb 2010 afu |
| 18 |
** Added dimension check to prevent short int from overflowing |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
|
| 23 |
#include "pnm.h" |
| 24 |
#include "sgi.h" |
| 25 |
#include "mallocvar.h" |
| 26 |
#include "runlength.h" |
| 27 |
|
| 28 |
|
| 29 |
/*#define DEBUG*/ |
| 30 |
|
| 31 |
typedef uint16_t ScanElem; |
| 32 |
typedef struct { |
| 33 |
ScanElem * data; |
| 34 |
long length; |
| 35 |
} ScanLine; |
| 36 |
|
| 37 |
#define MAXVAL_BYTE 255 |
| 38 |
#define MAXVAL_WORD 65535 |
| 39 |
#define INT16MAX 32767 |
| 40 |
|
| 41 |
static char storage = STORAGE_RLE; |
| 42 |
static ScanLine * channel[3]; |
| 43 |
static xel * pnmrow; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#define putByte(b) (void)(putc((unsigned char)(b), stdout)) |
| 48 |
|
| 49 |
|
| 50 |
static void |
| 51 |
putBigShort(short const s) { |
| 52 |
|
| 53 |
if (pm_writebigshort(stdout, s ) == -1) |
| 54 |
pm_error( "write error" ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static void |
| 60 |
putBigLong(long const l) { |
| 61 |
|
| 62 |
if (pm_writebiglong( stdout, l ) == -1) |
| 63 |
pm_error( "write error" ); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static void |
| 69 |
putShortAsByte(short const s) { |
| 70 |
|
| 71 |
putByte((unsigned char)s); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static void |
| 77 |
writeTable(long * const table, |
| 78 |
unsigned int const tabsize) { |
| 79 |
|
| 80 |
unsigned int i; |
| 81 |
unsigned long offset; |
| 82 |
|
| 83 |
offset = HeaderSize + tabsize * 8; |
| 84 |
|
| 85 |
for (i = 0; i < tabsize; ++i) { |
| 86 |
putBigLong(offset); |
| 87 |
offset += table[i]; |
| 88 |
} |
| 89 |
for (i = 0; i < tabsize; ++i) |
| 90 |
putBigLong(table[i]); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static void |
| 96 |
writeChannels(unsigned int const cols, |
| 97 |
unsigned int const rows, |
| 98 |
unsigned int const channels, |
| 99 |
void (*put) (short)) { |
| 100 |
|
| 101 |
unsigned int i; |
| 102 |
|
| 103 |
for (i = 0; i < channels; ++i) { |
| 104 |
unsigned int row; |
| 105 |
for (row = 0; row < rows; ++row) { |
| 106 |
unsigned int col; |
| 107 |
for (col = 0; col < channel[i][row].length; ++col) { |
| 108 |
(*put)(channel[i][row].data[col]); |
| 109 |
} |
| 110 |
pm_rlenc_freebuf((unsigned char *) channel[i][row].data); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static ScanElem * |
| 118 |
compress(ScanElem * const tempArg, |
| 119 |
unsigned int const row, |
| 120 |
unsigned int const rows, |
| 121 |
unsigned int const cols, |
| 122 |
unsigned int const chanNum, |
| 123 |
long * const table, |
| 124 |
unsigned int const bpc) { |
| 125 |
/*---------------------------------------------------------------------------- |
| 126 |
Compress a row, putting results in global 'channel' array, in newly |
| 127 |
allocated storage (which Caller must free). |
| 128 |
|
| 129 |
Except that if the compression is null compression, we make 'channel' |
| 130 |
point to existing storage, which Caller must not free. Yuck. |
| 131 |
-----------------------------------------------------------------------------*/ |
| 132 |
ScanElem * retval; |
| 133 |
|
| 134 |
switch (storage) { |
| 135 |
case STORAGE_VERBATIM: |
| 136 |
channel[chanNum][row].length = cols; |
| 137 |
channel[chanNum][row].data = tempArg; |
| 138 |
MALLOCARRAY_NOFAIL(retval, cols); |
| 139 |
break; |
| 140 |
case STORAGE_RLE: { |
| 141 |
unsigned int const tabrow = chanNum * rows + row; |
| 142 |
|
| 143 |
unsigned int len; |
| 144 |
size_t lenBytes; |
| 145 |
ScanElem * p; |
| 146 |
|
| 147 |
pm_rlenc_allocoutbuf((unsigned char **) &p, cols, PM_RLE_SGI16); |
| 148 |
|
| 149 |
pm_rlenc_compressword(tempArg,(unsigned char *) p, PM_RLE_SGI16, |
| 150 |
cols, &lenBytes); |
| 151 |
|
| 152 |
assert((unsigned)lenBytes == lenBytes); |
| 153 |
/* Guaranteed by pm_rlenc_compressword() */ |
| 154 |
|
| 155 |
len = lenBytes / 2; /* sizeof(ScanElem) */ |
| 156 |
channel[chanNum][row].length = len; |
| 157 |
REALLOCARRAY(p, len); /* reclaim some space */ |
| 158 |
if (p == NULL) |
| 159 |
pm_error("realloc failure while reclaiming memory space " |
| 160 |
"for output"); |
| 161 |
channel[chanNum][row].data = p; |
| 162 |
table[tabrow] = len * bpc; |
| 163 |
retval = tempArg; |
| 164 |
} break; |
| 165 |
default: |
| 166 |
pm_error("unknown storage type - can't happen"); |
| 167 |
} |
| 168 |
return retval; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static long * |
| 174 |
buildChannels(FILE * const ifP, |
| 175 |
unsigned int const cols, |
| 176 |
unsigned int const rows, |
| 177 |
xelval const maxval, |
| 178 |
int const format, |
| 179 |
unsigned int const bpc, |
| 180 |
unsigned int const channels) { |
| 181 |
|
| 182 |
unsigned int row; |
| 183 |
unsigned int sgirow; |
| 184 |
long * table; |
| 185 |
ScanElem * temp; |
| 186 |
|
| 187 |
if (storage != STORAGE_VERBATIM) { |
| 188 |
MALLOCARRAY_NOFAIL(table, channels * rows); |
| 189 |
} else |
| 190 |
table = NULL; |
| 191 |
|
| 192 |
MALLOCARRAY_NOFAIL(temp, cols); |
| 193 |
|
| 194 |
{ |
| 195 |
unsigned int i; |
| 196 |
for (i = 0; i < channels; ++i) |
| 197 |
MALLOCARRAY_NOFAIL(channel[i], rows); |
| 198 |
} |
| 199 |
|
| 200 |
for (row = 0, sgirow = rows-1; row < rows; ++row, --sgirow) { |
| 201 |
pnm_readpnmrow(ifP, pnmrow, cols, maxval, format); |
| 202 |
if (channels == 1) { |
| 203 |
unsigned int col; |
| 204 |
for (col = 0; col < cols; ++col) |
| 205 |
temp[col] = (ScanElem)PNM_GET1(pnmrow[col]); |
| 206 |
temp = compress(temp, sgirow, rows, cols, 0, table, bpc); |
| 207 |
} else { |
| 208 |
unsigned int col; |
| 209 |
for (col = 0; col < cols; ++col) |
| 210 |
temp[col] = (ScanElem)PPM_GETR(pnmrow[col]); |
| 211 |
temp = compress(temp, sgirow, rows, cols, 0, table, bpc); |
| 212 |
for (col = 0; col < cols; ++col) |
| 213 |
temp[col] = (ScanElem)PPM_GETG(pnmrow[col]); |
| 214 |
temp = compress(temp, sgirow, rows, cols, 1, table, bpc); |
| 215 |
for (col = 0; col < cols; ++col) |
| 216 |
temp[col] = (ScanElem)PPM_GETB(pnmrow[col]); |
| 217 |
temp = compress(temp, sgirow, rows, cols, 2, table, bpc); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
free(temp); |
| 222 |
return table; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static void |
| 228 |
writeHeader(unsigned int const cols, |
| 229 |
unsigned int const rows, |
| 230 |
xelval const maxval, |
| 231 |
unsigned int const bpc, |
| 232 |
unsigned int const dimensions, |
| 233 |
unsigned int const channels, |
| 234 |
const char * const imagename) { |
| 235 |
|
| 236 |
unsigned int i; |
| 237 |
|
| 238 |
putBigShort(SGI_MAGIC); |
| 239 |
putByte(storage); |
| 240 |
putByte((char)bpc); |
| 241 |
putBigShort(dimensions); |
| 242 |
putBigShort(cols); |
| 243 |
putBigShort(rows); |
| 244 |
putBigShort(channels); |
| 245 |
putBigLong(0); /* PIXMIN */ |
| 246 |
putBigLong(maxval); /* PIXMAX */ |
| 247 |
|
| 248 |
for(i = 0; i < 4; ++i) |
| 249 |
putByte(0); |
| 250 |
|
| 251 |
for (i = 0; i < 79 && imagename[i] != '\0'; ++i) |
| 252 |
putByte(imagename[i]); |
| 253 |
|
| 254 |
for(; i < 80; ++i) |
| 255 |
putByte(0); |
| 256 |
|
| 257 |
putBigLong(CMAP_NORMAL); |
| 258 |
|
| 259 |
for (i = 0; i < 404; ++i) |
| 260 |
putByte(0); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
int |
| 266 |
main(int argc,char * argv[]) { |
| 267 |
|
| 268 |
FILE * ifP; |
| 269 |
int argn; |
| 270 |
const char * const usage = "[-verbatim|-rle] [-imagename <name>] [pnmfile]"; |
| 271 |
int cols, rows; |
| 272 |
int format; |
| 273 |
xelval maxval, newmaxval; |
| 274 |
const char * imagename; |
| 275 |
unsigned int bpc; |
| 276 |
unsigned int dimensions; |
| 277 |
unsigned int channels; |
| 278 |
long * table; |
| 279 |
|
| 280 |
pnm_init(&argc, argv); |
| 281 |
|
| 282 |
imagename = "no name"; /* default value */ |
| 283 |
argn = 1; |
| 284 |
while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { |
| 285 |
if( pm_keymatch(argv[argn], "-verbatim", 2) ) |
| 286 |
storage = STORAGE_VERBATIM; |
| 287 |
else |
| 288 |
if( pm_keymatch(argv[argn], "-rle", 2) ) |
| 289 |
storage = STORAGE_RLE; |
| 290 |
else |
| 291 |
if( pm_keymatch(argv[argn], "-imagename", 2) ) { |
| 292 |
if( ++argn >= argc ) |
| 293 |
pm_usage(usage); |
| 294 |
imagename = argv[argn]; |
| 295 |
} |
| 296 |
else |
| 297 |
pm_usage(usage); |
| 298 |
++argn; |
| 299 |
} |
| 300 |
|
| 301 |
if( argn < argc ) { |
| 302 |
ifP = pm_openr( argv[argn] ); |
| 303 |
argn++; |
| 304 |
} |
| 305 |
else |
| 306 |
ifP = stdin; |
| 307 |
|
| 308 |
if( argn != argc ) |
| 309 |
pm_usage(usage); |
| 310 |
|
| 311 |
pnm_readpnminit(ifP, &cols, &rows, &maxval, &format); |
| 312 |
|
| 313 |
if (rows > INT16MAX || cols > INT16MAX) |
| 314 |
pm_error ("Input image is too large."); |
| 315 |
|
| 316 |
pnmrow = pnm_allocrow(cols); |
| 317 |
|
| 318 |
switch (PNM_FORMAT_TYPE(format)) { |
| 319 |
case PBM_TYPE: |
| 320 |
pm_message("promoting PBM to PGM"); |
| 321 |
newmaxval = PGM_MAXMAXVAL; |
| 322 |
case PGM_TYPE: |
| 323 |
newmaxval = maxval; |
| 324 |
dimensions = 2; |
| 325 |
channels = 1; |
| 326 |
break; |
| 327 |
case PPM_TYPE: |
| 328 |
newmaxval = maxval; |
| 329 |
dimensions = 3; |
| 330 |
channels = 3; |
| 331 |
break; |
| 332 |
default: |
| 333 |
pm_error("can\'t happen"); |
| 334 |
} |
| 335 |
if (newmaxval <= MAXVAL_BYTE) |
| 336 |
bpc = 1; |
| 337 |
else if (newmaxval <= MAXVAL_WORD) |
| 338 |
bpc = 2; |
| 339 |
else |
| 340 |
pm_error("maxval too large - try using \"pnmdepth %u\"", MAXVAL_WORD); |
| 341 |
|
| 342 |
table = buildChannels(ifP, cols, rows, newmaxval, format, bpc, channels); |
| 343 |
|
| 344 |
pnm_freerow(pnmrow); |
| 345 |
|
| 346 |
pm_close(ifP); |
| 347 |
|
| 348 |
writeHeader(cols, rows, newmaxval, bpc, dimensions, channels, imagename); |
| 349 |
|
| 350 |
if (table) |
| 351 |
writeTable(table, rows * channels); |
| 352 |
|
| 353 |
if (bpc == 1) |
| 354 |
writeChannels(cols, rows, channels, putShortAsByte); |
| 355 |
else |
| 356 |
writeChannels(cols, rows, channels, putBigShort); |
| 357 |
|
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|