summaryrefslogtreecommitdiff
path: root/common/im_bmp.cpp
blob: f850fa7d793ff67b0b2f0ead65fcc0f03952bf6b (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "quant.h"
#include "image.h"
#include "file.h"

// ========================================================

bool Image::LoadBMP (File& file)
{
  lcint32 bmWidth, bmHeight;
  lcuint8 bmPlanes, bmBitsPixel, m1, m2;
  typedef struct {
    unsigned char rgbBlue;
    unsigned char rgbGreen;
    unsigned char rgbRed;
    unsigned char rgbReserved;
  } RGBQUAD;
  lcint16 res1,res2;
  lcint32 filesize, pixoff;
  lcint32 bmisize, compression;
  lcint32 xscale, yscale;
  lcint32 colors, impcol, rc;
  lcuint32 sizeimage, m_bytesRead = 0;

  FreeData ();

	if (file.Read (&m1, 1) != 1)
    return false;
	m_bytesRead++;

	if (file.Read (&m2, 1) != 1)
    return false;
	m_bytesRead++;

	if ((m1 != 'B') || (m2 != 'M'))
		return false;

	rc = file.ReadLong ((long*)&(filesize), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadShort ((int*)&(res1), 1); m_bytesRead+=2;
	if (rc != 1) { return false; }

	rc = file.ReadShort ((int*)&(res2), 1); m_bytesRead+=2;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(pixoff), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(bmisize), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(bmWidth), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(bmHeight), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadShort ((int*)&(bmPlanes), 1); m_bytesRead+=2;
	if (rc != 1) { return false; }

	rc = file.ReadShort ((int*)&(bmBitsPixel), 1); m_bytesRead+=2;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(compression), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(sizeimage), 1); m_bytesRead+=4;
	if (rc != 1) {return false; }

	rc = file.ReadLong ((long*)&(xscale), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(yscale), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(colors), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	rc = file.ReadLong ((long*)&(impcol), 1); m_bytesRead+=4;
	if (rc != 1) { return false; }

	if (colors == 0)
		colors = 1 << bmBitsPixel;

	RGBQUAD *colormap = NULL;

	if (bmBitsPixel != 24)
	{
		colormap = new RGBQUAD[colors];
		if (colormap == NULL)
			return false;

		int i;
		for (i = 0; i < colors; i++)
		{
			unsigned char r ,g, b, dummy;

			rc = file.Read (&b, 1);
			m_bytesRead++;
			if (rc!=1)
			{
				delete [] colormap;
				return false;
			}

			rc = file.Read (&g, 1); 
			m_bytesRead++;
			if (rc!=1)
			{
				delete [] colormap;
				return false;
			}

			rc = file.Read (&r, 1); 
			m_bytesRead++;
			if (rc != 1)
			{
				delete [] colormap;
				return false;
			}

			rc = file.Read (&dummy, 1); 
			m_bytesRead++;
			if (rc != 1)
			{
				delete [] colormap;
				return false;
			}

			colormap[i].rgbRed = r;
			colormap[i].rgbGreen = g;
			colormap[i].rgbBlue = b;
		}
	}

	if ((long)m_bytesRead > pixoff)
	{
		delete [] colormap;
		return false;
	}

	while ((long)m_bytesRead < pixoff)
	{
		char dummy;
		file.Read (&dummy, 1);
		m_bytesRead++;
	}

	int w = bmWidth;
	int h = bmHeight;

	// set the output params
	m_pData = (unsigned char*)malloc (w*h*3);
	long row_size = w * 3;

	if (m_pData != NULL) 
	{
		m_nWidth = w;
		m_nHeight = h;
    m_bAlpha = false;
		unsigned char* outbuf = m_pData;
		long row = 0;
		long rowOffset = 0;

		if (compression == 0) // BI_RGB
		{
			// read rows in reverse order
			for (row=bmHeight-1;row>=0;row--)
			{
				// which row are we working on?
				rowOffset = (long unsigned)row*row_size;						      

				if (bmBitsPixel == 24)
				{
					for (int col=0;col<w;col++)
					{
						long offset = col * 3;
						char pixel[3];

						if (file.Read (pixel, 3) ==3)
						{
							// we swap red and blue here
							*(outbuf + rowOffset + offset + 0) = pixel[2];		// r
							*(outbuf + rowOffset + offset + 1) = pixel[1];		// g
							*(outbuf + rowOffset + offset + 2) = pixel[0];		// b
						}
					}
					m_bytesRead += row_size;

					// read DWORD padding
					while ((m_bytesRead-pixoff)&3)
					{
						char dummy;
						if (file.Read (&dummy, 1) != 1)
						{
              FreeData ();
							return false;
						}
						m_bytesRead++;
					}
				}
				else
				{
					// pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
					int bit_count = 0;
					unsigned long mask = (1 << bmBitsPixel) - 1;
					unsigned char inbyte = 0;

					for (int col=0;col<w;col++)
					{
						int pix = 0;

						// if we need another byte
						if (bit_count <= 0)
						{
							bit_count = 8;
							if (file.Read (&inbyte, 1) != 1)
							{
                FreeData ();
								delete [] colormap;
								return false;
							}
							m_bytesRead++;
						}

						// keep track of where we are in the bytes
						bit_count -= bmBitsPixel;
						pix = ( inbyte >> bit_count) & mask;

						// lookup the color from the colormap - stuff it in our buffer
						// swap red and blue
						*(outbuf + rowOffset + col * 3 + 2) = colormap[pix].rgbBlue;
						*(outbuf + rowOffset + col * 3 + 1) = colormap[pix].rgbGreen;
						*(outbuf + rowOffset + col * 3 + 0) = colormap[pix].rgbRed;
					}

					// read DWORD padding
					while ((m_bytesRead-pixoff)&3)
					{
						char dummy;
						if (file.Read (&dummy, 1) != 1)
						{
              FreeData ();
							if (colormap)
								delete [] colormap;
							return false;
						}
						m_bytesRead++;
					}
				}
			}
		}
		else
		{
			int i, x = 0;
			unsigned char c, c1 = 0, *pp;
			row = 0;
			pp = outbuf + (bmHeight-1)*bmWidth*3;

			if (bmBitsPixel == 8)
			{
				while (row < bmHeight)
				{
					c = file.GetChar ();

					if (c)
					{
						// encoded mode
						c1 = file.GetChar ();
						for (i = 0; i < c; x++, i++)
						{
							*pp = colormap[c1].rgbRed; pp++;
							*pp = colormap[c1].rgbGreen; pp++;
							*pp = colormap[c1].rgbBlue; pp++;
						}
					}
					else
					{
						// c==0x00,  escape codes
						c = file.GetChar ();

						if (c == 0x00) // end of line
						{
							row++;
							x = 0;
							pp = outbuf + (bmHeight-row-1)*bmWidth*3;
						}
						else if (c == 0x01)
							break; // end of pic
						else if (c == 0x02) // delta
						{
							c = file.GetChar ();
							x += c;
							c = file.GetChar ();
							row += c;
							pp = outbuf + x*3 + (bmHeight-row-1)*bmWidth*3;
						}
						else // absolute mode
						{
							for (i = 0; i < c; x++, i++)
							{
								c1 = file.GetChar ();
								*pp = colormap[c1].rgbRed; pp++;
								*pp = colormap[c1].rgbGreen; pp++;
								*pp = colormap[c1].rgbBlue; pp++;
							}
					
							if (c & 1)
								file.GetChar (); // odd length run: read an extra pad byte
						}
					}
				}
			}
			else if (bmBitsPixel == 4)
			{
				while (row < bmHeight)
				{
					c = file.GetChar ();

					if (c)
					{
						// encoded mode
						c1 = file.GetChar ();
						for (i = 0; i < c; x++, i++)
						{
							*pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
							*pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
							*pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
						}
					}
					else
					{
						// c==0x00,  escape codes
						c = file.GetChar ();

						if (c == 0x00) // end of line
						{
							row++;
							x = 0;
							pp = outbuf + (bmHeight-row-1)*bmWidth*3;
						}
						else if (c == 0x01)
							break; // end of pic
						else if (c == 0x02) // delta
						{
							c = file.GetChar ();
							x += c;
							c = file.GetChar ();
							row += c;
							pp = outbuf + x*3 + (bmHeight-row-1)*bmWidth*3;
						}
						else // absolute mode
						{
							for (i = 0; i < c; x++, i++)
							{
							    if ((i&1) == 0)
									c1 = file.GetChar ();
								*pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
								*pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
								*pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
							}
					
							if (((c&3) == 1) || ((c&3) == 2))
								file.GetChar (); // odd length run: read an extra pad byte
						}
					}
				}
			}
		}

		if (colormap)
			delete [] colormap;
    }

	return true;
}

// ========================================================

bool Image::SaveBMP (File& file, bool quantize) const
{
	unsigned short bits;
	unsigned long cmap, bfSize;
	unsigned char pal[3][256], *colormappedbuffer = NULL;

	if (quantize)
	{
		colormappedbuffer = (unsigned char*)malloc(m_nWidth*m_nHeight);
		dl1quant (m_pData, colormappedbuffer, m_nWidth, m_nHeight, 256, true, pal);
		bits = 8;
		cmap = 256;
		bfSize = 1078 + m_nWidth*m_nHeight;
	}
	else
	{
		bits = 24;
		cmap = 0;
		bfSize = 54 + m_nWidth*m_nHeight*3;
	} 

	long byteswritten = 0;
	long pixoff = 54 + cmap*4;
	short res = 0;
	char m1 ='B', m2 ='M';
	file.WriteByte (&m1, 1);       byteswritten++; // B
	file.WriteByte (&m2, 1);       byteswritten++; // M
	file.WriteLong (&bfSize, 1);   byteswritten+=4;// bfSize
	file.WriteShort (&res, 1);     byteswritten+=2;// bfReserved1
	file.WriteShort (&res, 1);     byteswritten+=2;// bfReserved2
	file.WriteLong (&pixoff, 1);   byteswritten+=4;// bfOffBits

	lcuint32 biSize = 40, compress = 0, size = 0;
	lcint32 width = m_nWidth, height = m_nHeight, pixels = 0;
	lcuint16 planes = 1;
	file.WriteLong (&biSize, 1);   byteswritten+=4;// biSize
	file.WriteLong (&width, 1);    byteswritten+=4;// biWidth
	file.WriteLong (&height, 1);   byteswritten+=4;// biHeight
	file.WriteShort (&planes, 1);  byteswritten+=2;// biPlanes
	file.WriteShort (&bits, 1);    byteswritten+=2;// biBitCount
	file.WriteLong (&compress, 1); byteswritten+=4;// biCompression
	file.WriteLong (&size, 1);     byteswritten+=4;// biSizeImage
	file.WriteLong (&pixels, 1);   byteswritten+=4;// biXPelsPerMeter
	file.WriteLong (&pixels, 1);   byteswritten+=4;// biYPelsPerMeter
	file.WriteLong (&cmap, 1);     byteswritten+=4;// biClrUsed
	file.WriteLong (&cmap, 1);     byteswritten+=4;// biClrImportant

	if (quantize)
	{
		for (int i = 0; i < 256; i++) 
		{
			file.PutChar (pal[2][i]);
			file.PutChar (pal[1][i]);
			file.PutChar (pal[0][i]);
			file.PutChar (0);	// dummy
		}

		for (int row = 0; row < m_nHeight; row++) 
		{
			int pixbuf = 0;

			for (int col = 0; col < m_nWidth; col++) 
			{
				int offset = (m_nHeight-row-1) * width + col;	// offset into our color-mapped RGB buffer
				unsigned char pval = *(colormappedbuffer + offset);

				pixbuf = (pixbuf << 8) | pval;

				file.PutChar (pixbuf);
				pixbuf = 0;
				byteswritten++;
			}

			// DWORD align
			while ((byteswritten - pixoff) & 3)
			{
				file.PutChar (0);
				byteswritten++;
			}
		}

		free(colormappedbuffer);
	}
	else
	{
		unsigned long widthDW = (((m_nWidth*24) + 31) / 32 * 4);
		long row, row_size = m_nWidth*3;
		for (row = 0; row < m_nHeight; row++) 
		{
			unsigned char* buf = m_pData+(m_nHeight-row-1)*row_size;

			// write a row
			for (int col = 0; col < row_size; col += 3)
			{
				file.PutChar (buf[col+2]);
				file.PutChar (buf[col+1]);
				file.PutChar (buf[col]);
			}
			byteswritten += row_size;	

			for (unsigned long count = row_size; count < widthDW; count++)
			{
				file.PutChar (0);	// dummy
				byteswritten++;
			}
		}
	}

	return true;
}