-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
260 lines (226 loc) · 6.75 KB
/
parse.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "windows.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "paulslib.h"
#include "line.h"
/*
Sample program to read a limited number of BMP file types
Write out lots of diagnostics
Write out a 24 bit RAW image file
*/
typedef struct {
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned int offset; /* Offset to image data, bytes */
} HEADER;
typedef struct {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} INFOHEADER;
typedef struct {
unsigned char r,g,b,junk;
} COLOURINDEX;
#define TABLEBLACK(c) ((colourindex[d].r|colourindex[d].g|colourindex[d].b)<10)
#ifdef A1
#define BLACK(c) TABLEBLACK(c)
#else
#define BLACK(c) (c == 1)
#endif
static char *image = (char *)NULL;
static int dataw;
static int h;
static int w;
COLOURINDEX colourindex[256];
void loadImage(char *filename)
{
int i,j;
int gotindex = FALSE;
unsigned char grey,r,g,b;
HEADER header;
INFOHEADER infoheader;
FILE *fptr;
gMem = 300;
/* Open file */
if ((fptr = fopen(filename,"r")) == NULL) {
fprintf(stderr,"Unable to open BMP file \"%s\"\n",filename);
exit(-1);
}
gMem = 301;
/* Read and check the header */
ReadUShort(fptr,&header.type,FALSE);
#ifdef DEBUG
fprintf(stderr,"ID is: %d, should be %d\n",header.type,'M'*256+'B');
#endif
ReadUInt(fptr,&header.size,FALSE);
#ifdef DEBUG
fprintf(stderr,"File size is %d bytes\n",header.size);
#endif
ReadUShort(fptr,&header.reserved1,FALSE);
ReadUShort(fptr,&header.reserved2,FALSE);
ReadUInt(fptr,&header.offset,FALSE);
#ifdef DEBUG
fprintf(stderr,"Offset to image data is %d bytes\n",header.offset);
#endif
/* Read and check the information header */
if (fread(&infoheader,sizeof(INFOHEADER),1,fptr) != 1) {
fprintf(stderr,"Failed to read BMP info header\n");
exit(-1);
}
#ifdef DEBUG
fprintf(stderr,"Image size = %d x %d\n",infoheader.width,infoheader.height);
fprintf(stderr,"Number of colour planes is %d\n",infoheader.planes);
fprintf(stderr,"Bits per pixel is %d\n",infoheader.bits);
fprintf(stderr,"Compression type is %d\n",infoheader.compression);
fprintf(stderr,"Number of colours is %d\n",infoheader.ncolours);
fprintf(stderr,"Number of required colours is %d\n",
infoheader.importantcolours);
#endif
/* Read the lookup table if there is one */
for (i=0;i<255;i++) {
colourindex[i].r = 0;
colourindex[i].g = 0;
colourindex[i].b = 0;
colourindex[i].junk = 0;
}
if (infoheader.ncolours > 0) {
#ifdef DEBUG
fprintf(stderr, "There is a color index table\n");
#endif
for (i=0;i<infoheader.ncolours;i++) {
if (fread(&colourindex[i].b,sizeof(unsigned char),1,fptr) != 1) {
fprintf(stderr,"Image read failed\n");
exit(-1);
}
if (fread(&colourindex[i].g,sizeof(unsigned char),1,fptr) != 1) {
fprintf(stderr,"Image read failed\n");
exit(-1);
}
if (fread(&colourindex[i].r,sizeof(unsigned char),1,fptr) != 1) {
fprintf(stderr,"Image read failed\n");
exit(-1);
}
if (fread(&colourindex[i].junk,sizeof(unsigned char),1,fptr) != 1) {
fprintf(stderr,"Image read failed\n");
exit(-1);
}
/*
fprintf(stderr,"%3d\t%3d\t%3d\t%3d\n",i,
colourindex[i].r,colourindex[i].g,colourindex[i].b);
*/
}
gotindex = TRUE;
}
else
{
printf("No color index table\n");
exit(0);
}
gMem = 302;
/* Free and/or allocate memory for the bitmap */
if (image != (char *)NULL) {
#ifdef DEBUG
fprintf(stderr, "Freeing previous image array\n");
#endif
free(image);
}
gMem = 303;
/* Set the *global* dimensions for future reference */
/* width (w) may not be a multiple of 4, but dataw is */
h = infoheader.height;
dataw = w = infoheader.width;
dataw += (w%4) ? 4-(w%4) : 0;
// printf("height = %d dataw=%d\n", h,dataw);
image = (unsigned char *)malloc((h+1)*(dataw+1));
// printf("H %d W %d HXW %d\n", h, w, h*dataw);
// printf("IMAGE %x (%d) %x\n", image, (h+1)*(dataw+1), image+((h+1)*(dataw+1)));
gMem = 304;
/* Seek to the start of the image data */
fseek(fptr,header.offset,SEEK_SET);
/* Read each image line, and set each byte to white(0) or black(1) */
for (j=0;j<h;j++) {
gMem = 10000;
unsigned char *line = &image[j*dataw];
if (fread(line,sizeof(unsigned char),dataw,fptr) != dataw)
{
fprintf(stderr,"Image read failed scanning line %d of %d\n",j,h);
exit(-1);
}
#ifdef A1
#else
for (i=0;i<w;i++)
{
int d = line[i];
if (TABLEBLACK(d))
line[i] = 1;
else
line[i] = 0;
}
#endif
}
fclose(fptr);
gMem = 30000;
}
char * newgetaline(int scanline, int xoffset)
{
/*
printf("SCANLINE %d\n", scanline);
printf("NEWGETALINE %x %d %d %d\n", image, scanline, dataw, xoffset);
*/
return &image[scanline*dataw + xoffset];
/* printf("ADDRESS %x\n", tmp); */
}
void checkImage(void)
{
printf(" |");
int i,j;
for (j=0;j<h;j++) {
unsigned char *line = newgetaline(j,0);
for (i=0;i<w;i++) {
int d = line[i];
if (i%2) { if ( BLACK(d) ) printf("@"); else printf(" "); }
}
printf("|\n |");
}
}
extern int *gXStart;
extern int *gYStart;
extern int *gWidth;
extern int *gHeight;
extern int show;
void checkImageN(int n)
{
int minx, maxx, miny, maxy;
int i,j;
if (show != n+1) return;
/* Get the Bounding Box for this Cropping */
minx = *(gXStart+n);
maxx = minx + *(gWidth+n);
miny = *(gYStart+n);
maxy = miny + *(gHeight+n);
printf("\n [%3d ]|",n+1);
for (i=minx;i<maxx;i+=2) { printf("_"); }
printf("\n |");
for (j=maxy-1;j>=miny;j--)
{
if (j%2) continue;
unsigned char *line = newgetaline(j,0);
for (i=minx;i<maxx;i++)
{
int d = line[i];
if (i%2) {
if ( BLACK(d) ) printf("@"); else printf(" ");
}
}
printf("|\n%3d %3d |",j-miny, (maxy-j));
}
for (i=minx;i<maxx;i+=2) { printf("_"); } printf("\n");
}