-
Notifications
You must be signed in to change notification settings - Fork 7
/
bmp.c
331 lines (271 loc) · 7.71 KB
/
bmp.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "filter.h"
#ifdef __Ansi__
#include "sys_ansi.h"
#elif defined (__Win__)
#include "sys_win.h"
#endif
#include "metadata.h"
#include "file.h"
static int readBMPFileHeader(Image *im, file_spec input);
#pragma pack(push, 1)
// Bitmap file header
typedef struct tagwin3xHead{
short ImageFileType; // Always 4D42h ("BM")
long FileSize; // Physical size of file in bytes
short Reserved1; // Always 0
short Reserved2; // Always 0
long ImageDataOffset; // Start of image data offset in bytes
} win3xHead;
typedef struct tagwin3xBitmapInfoHeader{
long HeaderSize; // Size of this header
long ImageWidth; // Image width in pixels
long ImageHeight; // Image height in pixels
short NumberOfImagePlanes; // Number of planes (Always 1)
short BitsPerPixel; // Bits per pixel (1,4, 8 or 24)
long CompressionMethod; // Compression method (0, 1 or 2)
long SizeOfBitmap; // Size of bitmap in bytes
long HorzResolution; // Horizontal resolution in pixels / meter
long VertResolution; // Vertical resolution in pixels / meter
long NumColorsUsed; // Number of colors in image
long NumSignificantColors; // Number of important colors in palette
} win3xBitmapInfoHeader;
#pragma pack(pop)
int writeBMP( Image *im, fullPath *sfile )
{
file_spec output;
unsigned char *buf, *data;
long count;
int scanLength;
int y;
win3xHead head; // First part of bitmap header
win3xBitmapInfoHeader ihead; // Second part of bitmap header
//In BMP files, each line MUST have n bytes, such that n is divisible by 4.
//If the image doesn't have n divisible by 4, it will contain non-valid
//information. For example if an image has 1 pixel per line in 24-bit color
//depth, each line will have 1 additional byte that must be ignored when
//reading the bitmap.
//scanlength must be divisble by four (adding 3, dividing by 4 and then multiplying does this).
scanLength = ((im->width * 3) + 3)/4;
scanLength *= 4;
// Build header structures
head.ImageFileType=0x4D42; // Always 4D42h ("BM")
head.FileSize = scanLength * im->height+54; // Physical size of file in bytes
head.Reserved1=0; // Always 0
head.Reserved2=0; // Always 0
head.ImageDataOffset= 54; // Start of image data offset in bytes
ihead.HeaderSize=40; // Size of this header
ihead.ImageWidth=im->width; // Image width in pixels
ihead.ImageHeight=im->height; // Image height in pixels
ihead.NumberOfImagePlanes=1; // Number of planes (Always 1)
ihead.BitsPerPixel=24; // Bits per pixel (24 bit color)
ihead.CompressionMethod=0; // Compression method (0=uncompressed)
ihead.SizeOfBitmap = head.FileSize-54; // Size of bitmap in bytes
ihead.HorzResolution=7085; // Horizontal resolution in pixels / meter
ihead.VertResolution=7085; // Vertical resolution in pixels / meter
ihead.NumColorsUsed =0; // Number of colors in image
ihead.NumSignificantColors=0; // Number of important colors in palette
if( myopen( sfile, write_bin, output ) )
{
PrintError("writeBMP, could not open file");
return -1;
}
count = sizeof( head );
mywrite( output, count, &head );
if( count != sizeof( head ) )
{
PrintError("writeBMP, could not write header");
return -1;
}
count = sizeof( ihead );
mywrite( output, count, &ihead );
if( count != sizeof( ihead ) )
{
PrintError("writeBMP, could not write header");
return -1;
}
buf = (unsigned char*)malloc( im->bytesPerLine + 1); // Worst case
if( buf == NULL )
{
PrintError( "Not enough memory" );
return -1;
}
// Write image in reverse order, also change rgb->bgr
data = *(im->data) + ((im->height-1) * im->bytesPerLine);
for(y=0; y<im->height; y++)
{
if( im->bitsPerPixel == 32 ) // Convert 4->3 samples
{
int x;
unsigned char *c1=buf, *c2=data;
for(x=0; x < im->width; x++)
{
*c1++ = c2[3];
*c1++ = c2[2];
*c1++ = c2[1];
c2 += 4;
}
}
else
{
int x;
unsigned char *c1=buf, *c2=data;
for(x=0; x < im->width; x++)
{
*c1++ = c2[2];
*c1++ = c2[1];
*c1++ = c2[0];
c2 += 3;
}
}
count = scanLength;
mywrite( output, count, buf );
if( count != scanLength )
{
PrintError("writeBMP, could not write image data");
free( buf );
return -1;
}
data -= im->bytesPerLine;
}
myclose( output );
free( buf );
return 0;
}
// Read bitmap file
static int readBMP( Image *im, fullPath *sfile )
{
file_spec input;
unsigned char *data, *buf;
int y;
int scanLength;
long count;
int reverse = 0;
// Bitmap file open
if( myopen( sfile, read_bin, input ) )
{
PrintError("readBMP, could not open file");
return -1;
}
// Read bitmap file header
if( readBMPFileHeader(im, input) )
{
PrintError("readBMP, error reading bitmap file header");
return -1;
}
if(0 > im->height) //JMW BMP has rows from bottom to top
{
im->height = -im->height;
reverse = 1;
}
scanLength = (im->width * 3 + 1)/2;
scanLength *= 2;
im->data = (unsigned char**) mymalloc( im->dataSize );
if( im->data == NULL )
{
PrintError("Not enough memory");
return -1;
}
buf = (unsigned char*)malloc( im->bytesPerLine + 1); // Worst case
if( buf == NULL )
{
PrintError( "Not enough memory" );
return -1;
}
if( 0 == reverse)
data = *(im->data) + ((im->height-1) * im->bytesPerLine);
else // 1 == reverse
data = *(im->data);
for(y=0; y<im->height; y++)
{
count = scanLength;
myread( input, count, buf );
if( count != scanLength )
{
PrintError("Error reading image data");
return -1;
}
// Convert 3->4 samples; bgr->rgb
{
int x;
unsigned char *c1=buf, *c2=data;
for(x=0; x < im->width; x++)
{
*c2++ = UCHAR_MAX;
*c2++ = c1[2];
*c2++ = c1[1];
*c2++ = c1[0];
c1+=3;
}
}
if( 0 == reverse)
data -= im->bytesPerLine;
else //1 == reverse
data += im->bytesPerLine;
}
myclose(input);
free( buf );
return 0;
}
// Read bitmap file header
static int readBMPFileHeader(Image *im, file_spec input)
{
win3xHead header; // First part of bitmap header
win3xBitmapInfoHeader iheader; // Second part of bitmap header
long count;
count = sizeof(header);
myread( input, count, &header );
if( count != sizeof(header) )
{
PrintError("Error reading first BMP header segment");
return -1;
}
if(header.ImageFileType != 0x4D42)
{
PrintError("readBMPFileHeader, BMP bad magic No");
return -1;
}
count = sizeof(iheader);
myread( input, count, &iheader );
if( count != sizeof(iheader) )
{
PrintError("Error reading second BMP header segment");
return -1;
}
if(iheader.HeaderSize != 40)
{ // Size of this header
PrintError("readBMPFileHeader, secondary header length wrong -- Not MS version 3 compatible");
return -1;
}
if(iheader.NumberOfImagePlanes != 1){ // Number of planes (Always 1)
PrintError("readBMPFileHeader, should be 1 image plane");
return -1;
}
if(iheader.BitsPerPixel != 24){ // Bits per pixel (1,4, 8 or 24)
PrintError("readBMPFileHeader, only 24 bit color supported");
return -1;
}
if(iheader.CompressionMethod != 0)
{ // Compression method (0, 1 or 2)
PrintError("readBMPFileHeader, only uncompressed BMP supported");
return -1;
}
SetImageDefaults( im );
// Setup height and width
im->height = iheader.ImageHeight;
im->width = iheader.ImageWidth;
im->bitsPerPixel = 32;
im->bytesPerLine = im->width * 4;
// JMW Negative height BMP have rows bottom to top
im->dataSize = im->bytesPerLine * abs(im->height);
// Advance file pointer to start of image data
fseek( input, header.ImageDataOffset, SEEK_SET );
return 0; // Header ok -- no error
}
int panoBMPRead( Image *im, fullPath *sfile )
{
if (readBMP(im, sfile) == 0) {
return panoMetadataUpdateFromImage(im);
}
else
return FALSE;
}