summaryrefslogtreecommitdiff
path: root/utils/mxtk/mxbmp.cpp
blob: 1f6616c3ed9900d0a7fa035ce5bbabb4c25842ea (plain) (blame)
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
//
//                 mxToolKit (c) 1999 by Mete Ciragan
//
// file:           mxBmp.cpp
// implementation: all
// last modified:  Apr 15 1999, Mete Ciragan
// copyright:      The programs and associated files contained in this
//                 distribution were developed by Mete Ciragan. The programs
//                 are not in the public domain, but they are freely
//                 distributable without licensing fees. These programs are
//                 provided without guarantee or warrantee expressed or
//                 implied.

// lbmlib.c

#include "mxtk/mxBmp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



mxImage *
mxBmpRead (const char *filename)
{
	int i;
	FILE *pfile = 0;
	mxBitmapFileHeader bmfh;
	mxBitmapInfoHeader bmih;
	mxBitmapRGBQuad rgrgbPalette[256];
	int cbBmpBits;
	byte *pbBmpBits;
	byte *pb, *pbPal = 0;
	int cbPalBytes;
	int biTrueWidth;
	mxImage *image = 0;

	// File exists?
	if ((pfile = fopen (filename, "rb")) == 0)
		return 0;
	
	// Read file header
	if (fread (&bmfh, sizeof bmfh, 1/*count*/, pfile) != 1)
		goto GetOut;

	// Bogus file header check
	if (!(bmfh.bfReserved1 == 0 && bmfh.bfReserved2 == 0))
		goto GetOut;

	// Read info header
	if (fread (&bmih, sizeof bmih, 1/*count*/, pfile) != 1)
		goto GetOut;

	// Bogus info header check
	if (!(bmih.biSize == sizeof bmih && bmih.biPlanes == 1))
		goto GetOut;

	// Bogus bit depth?  Only 8-bit supported.
	if (bmih.biBitCount != 8)
		goto GetOut;

	// Bogus compression?  Only non-compressed supported.
	if (bmih.biCompression != 0) //BI_RGB)
		goto GetOut;

	// Figure out how many entires are actually in the table
	if (bmih.biClrUsed == 0)
	{
		bmih.biClrUsed = 256;
		cbPalBytes = (1 << bmih.biBitCount) * sizeof (mxBitmapRGBQuad);
	}
	else 
	{
		cbPalBytes = bmih.biClrUsed * sizeof (mxBitmapRGBQuad);
	}

	// Read palette (bmih.biClrUsed entries)
	if (fread (rgrgbPalette, cbPalBytes, 1/*count*/, pfile) != 1)
		goto GetOut;

	image = new mxImage ();
	if (!image)
		goto GetOut;

	if (!image->create (bmih.biWidth, bmih.biHeight, 8))
	{
		delete image;
		goto GetOut;
	}

	pb = (byte *) image->palette;

	// Copy over used entries
	for (i = 0; i < (int) bmih.biClrUsed; i++)
	{
		*pb++ = rgrgbPalette[i].rgbRed;
		*pb++ = rgrgbPalette[i].rgbGreen;
		*pb++ = rgrgbPalette[i].rgbBlue;
	}

	// Fill in unused entires will 0,0,0
	for (i = bmih.biClrUsed; i < 256; i++) 
	{
		*pb++ = 0;
		*pb++ = 0;
		*pb++ = 0;
	}

	// Read bitmap bits (remainder of file)
	cbBmpBits = bmfh.bfSize - ftell (pfile);
	pb = (byte *) malloc (cbBmpBits * sizeof (byte));
	if (pb == 0)
	{
		free (pbPal);
		goto GetOut;
	}

	if (fread (pb, cbBmpBits, 1/*count*/, pfile) != 1)
	{
		free (pb);
		free (pbPal);
		goto GetOut;
	}
/*
	pbBmpBits = malloc(cbBmpBits);
	if (pbBmpBits == 0)
	{
		free (pb);
		free (pbPal);
		goto GetOut;
	}
*/
	pbBmpBits = (byte *) image->data;

	// data is actually stored with the width being rounded up to a multiple of 4
	biTrueWidth = (bmih.biWidth + 3) & ~3;
	
	// reverse the order of the data.
	pb += (bmih.biHeight - 1) * biTrueWidth;
	for(i = 0; i < bmih.biHeight; i++)
	{
		memmove (&pbBmpBits[biTrueWidth * i], pb, biTrueWidth);
		pb -= biTrueWidth;
	}

	pb += biTrueWidth;
	free (pb);

GetOut:
	if (pfile) 
		fclose (pfile);

	return image;
}



bool
mxBmpWrite (const char *filename, mxImage *image)
{
	int i;
	FILE *pfile = 0;
	mxBitmapFileHeader bmfh;
	mxBitmapInfoHeader bmih;
	mxBitmapRGBQuad rgrgbPalette[256];
	int cbBmpBits;
	byte *pbBmpBits;
	byte *pb = 0;
	int cbPalBytes;
	int biTrueWidth;

	if (!image || !image->data || !image->palette)
		return false;

	// File exists?
	if ((pfile = fopen(filename, "wb")) == 0)
		return false;

	biTrueWidth = ((image->width + 3) & ~3);
	cbBmpBits = biTrueWidth * image->height;
	cbPalBytes = 256 * sizeof (mxBitmapRGBQuad);

	// Bogus file header check
	//bmfh.bfType = MAKEWORD( 'B', 'M' );
	bmfh.bfType = (word) (('M' << 8) | 'B');
	bmfh.bfSize = sizeof bmfh + sizeof bmih + cbBmpBits + cbPalBytes;
	bmfh.bfReserved1 = 0;
	bmfh.bfReserved2 = 0;
	bmfh.bfOffBits = sizeof bmfh + sizeof bmih + cbPalBytes;

	// Write file header
	if (fwrite (&bmfh, sizeof bmfh, 1/*count*/, pfile) != 1)
	{
		fclose (pfile);
		return false;
	}

	// Size of structure
	bmih.biSize = sizeof bmih;
	// Width
	bmih.biWidth = biTrueWidth;
	// Height
	bmih.biHeight = image->height;
	// Only 1 plane 
	bmih.biPlanes = 1;
	// Only 8-bit supported.
	bmih.biBitCount = 8;
	// Only non-compressed supported.
	bmih.biCompression = 0; //BI_RGB;
	bmih.biSizeImage = 0;

	// huh?
	bmih.biXPelsPerMeter = 0;
	bmih.biYPelsPerMeter = 0;

	// Always full palette
	bmih.biClrUsed = 256;
	bmih.biClrImportant = 0;
	
	// Write info header
	if (fwrite (&bmih, sizeof bmih, 1/*count*/, pfile) != 1)
	{
		fclose (pfile);
		return false;
	}
	

	// convert to expanded palette
	pb = (byte *) image->palette;

	// Copy over used entries
	for (i = 0; i < (int) bmih.biClrUsed; i++)
	{
		rgrgbPalette[i].rgbRed   = *pb++;
		rgrgbPalette[i].rgbGreen = *pb++;
		rgrgbPalette[i].rgbBlue  = *pb++;
        rgrgbPalette[i].rgbReserved = 0;
	}

	// Write palette (bmih.biClrUsed entries)
	cbPalBytes = bmih.biClrUsed * sizeof (mxBitmapRGBQuad);
	if (fwrite (rgrgbPalette, cbPalBytes, 1/*count*/, pfile) != 1)
	{
		fclose (pfile);
		return false;
	}

	pbBmpBits = (byte *) malloc (cbBmpBits * sizeof (byte));
	if (!pbBmpBits)
	{
		fclose (pfile);
		return false;
	}

	pb = (byte *) image->data;
	// reverse the order of the data.
	pb += (image->height - 1) * image->width;
	for(i = 0; i < bmih.biHeight; i++)
	{
		memmove (&pbBmpBits[biTrueWidth * i], pb, image->width);
		pb -= image->width;
	}

	// Write bitmap bits (remainder of file)
	if (fwrite (pbBmpBits, cbBmpBits, 1/*count*/, pfile) != 1)
	{
		free (pbBmpBits);
		fclose (pfile);
		return false;
	}

	free (pbBmpBits);
	fclose (pfile);

	return true;
}