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