| 1 |
/* sirtopnm.c - read a Solitaire Image Recorder file and write a portable anymap |
| 2 |
** |
| 3 |
** Copyright (C) 1991 by Marvin Landis. |
| 4 |
** |
| 5 |
** Permission to use, copy, modify, and distribute this software and its |
| 6 |
** documentation for any purpose and without fee is hereby granted, provided |
| 7 |
** that the above copyright notice appear in all copies and that both that |
| 8 |
** copyright notice and this permission notice appear in supporting |
| 9 |
** documentation. This software is provided "as is" without express or |
| 10 |
** implied warranty. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdbool.h> |
| 14 |
#include <assert.h> |
| 15 |
|
| 16 |
#include "mallocvar.h" |
| 17 |
#include "pnm.h" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
static void |
| 22 |
readSirHeader(FILE * const ifP, |
| 23 |
int * const formatP, |
| 24 |
unsigned int * const rowsP, |
| 25 |
unsigned int * const colsP) { |
| 26 |
|
| 27 |
short info; |
| 28 |
|
| 29 |
pm_readlittleshort(ifP, &info); |
| 30 |
if (info != 0x3a4f) |
| 31 |
pm_error( "Input file is not a Solitaire file"); |
| 32 |
|
| 33 |
pm_readlittleshort(ifP, &info); |
| 34 |
|
| 35 |
pm_readlittleshort(ifP, &info); |
| 36 |
if (info == 17) |
| 37 |
*formatP = PGM_TYPE; |
| 38 |
else if (info == 11) |
| 39 |
*formatP = PPM_TYPE; |
| 40 |
else |
| 41 |
pm_error( "Input is not MGI TYPE 11 or MGI TYPE 17" ); |
| 42 |
|
| 43 |
pm_readlittleshort(ifP, &info); |
| 44 |
*colsP = info; |
| 45 |
|
| 46 |
pm_readlittleshort(ifP, &info); |
| 47 |
*rowsP = info; |
| 48 |
|
| 49 |
{ |
| 50 |
unsigned int i; |
| 51 |
for (i = 1; i < 1531; ++i) |
| 52 |
pm_readlittleshort(ifP, &info); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static void |
| 59 |
convertPgm(FILE * const ifP, |
| 60 |
FILE * const ofP, |
| 61 |
unsigned int const rows, |
| 62 |
unsigned int const cols, |
| 63 |
xel * const xelrow) { |
| 64 |
|
| 65 |
unsigned int row; |
| 66 |
|
| 67 |
pm_message("Writing a PGM file"); |
| 68 |
|
| 69 |
for (row = 0; row < rows; ++row) { |
| 70 |
unsigned int col; |
| 71 |
for (col = 0; col < cols; ++col) |
| 72 |
PNM_ASSIGN1(xelrow[col], fgetc(ifP)); |
| 73 |
|
| 74 |
pnm_writepnmrow(ofP, xelrow, cols, 255, PGM_TYPE, 0); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static void |
| 81 |
convertPpm(FILE * const ifP, |
| 82 |
FILE * const ofP, |
| 83 |
unsigned int const rows, |
| 84 |
unsigned int const cols, |
| 85 |
xel * const xelrow) { |
| 86 |
|
| 87 |
unsigned int const picsize = cols * rows * 3; |
| 88 |
unsigned int const planesize = cols * rows; |
| 89 |
|
| 90 |
unsigned char * sirarray; /* malloc'ed array */ |
| 91 |
unsigned int row; |
| 92 |
|
| 93 |
if (UINT_MAX/cols/rows < 3) |
| 94 |
pm_error("Image is too large (%u x %u x %u) for computation", |
| 95 |
cols, rows, 3); |
| 96 |
|
| 97 |
MALLOCARRAY(sirarray, picsize); |
| 98 |
|
| 99 |
if (!sirarray) |
| 100 |
pm_error( "Not enough memory to load %u x %u x %u SIR file", |
| 101 |
cols, rows, 3); |
| 102 |
|
| 103 |
if (fread(sirarray, 1, picsize, ifP) != picsize) |
| 104 |
pm_error("Error reading SIR file"); |
| 105 |
|
| 106 |
pm_message("Writing a PPM file"); |
| 107 |
for (row = 0; row < rows; ++row) { |
| 108 |
unsigned int col; |
| 109 |
|
| 110 |
for (col = 0; col < cols; col++) |
| 111 |
PPM_ASSIGN(xelrow[col], sirarray[row*cols+col], |
| 112 |
sirarray[planesize + (row*cols+col)], |
| 113 |
sirarray[2*planesize + (row*cols+col)]); |
| 114 |
|
| 115 |
pnm_writepnmrow(ofP, xelrow, cols, 255, PPM_TYPE, 0); |
| 116 |
} |
| 117 |
free(sirarray); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int |
| 123 |
main(int argc, const char ** argv) { |
| 124 |
|
| 125 |
FILE * ifP; |
| 126 |
xel * xelrow; |
| 127 |
unsigned int rows, cols; |
| 128 |
int format; |
| 129 |
|
| 130 |
pm_proginit(&argc, argv); |
| 131 |
|
| 132 |
if (argc-1 > 1) |
| 133 |
pm_error ("Too many arguments. The only possible argument is " |
| 134 |
"the input file name"); |
| 135 |
else if (argc-1 >= 1) |
| 136 |
ifP = pm_openr(argv[1]); |
| 137 |
else |
| 138 |
ifP = stdin; |
| 139 |
|
| 140 |
readSirHeader(ifP, &format, &rows, &cols); |
| 141 |
|
| 142 |
pnm_writepnminit(stdout, cols, rows, 255, format, 0); |
| 143 |
|
| 144 |
xelrow = pnm_allocrow(cols); |
| 145 |
|
| 146 |
switch (PNM_FORMAT_TYPE(format)) { |
| 147 |
case PGM_TYPE: |
| 148 |
convertPgm(ifP, stdout, rows, cols, xelrow); |
| 149 |
break; |
| 150 |
case PPM_TYPE: |
| 151 |
convertPpm(ifP, stdout, rows, cols, xelrow); |
| 152 |
break; |
| 153 |
default: |
| 154 |
assert(false); |
| 155 |
} |
| 156 |
pnm_freerow(xelrow); |
| 157 |
|
| 158 |
pm_close(ifP); |
| 159 |
|
| 160 |
exit(0); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|