Hey. I was once a C++ ninja... and now since I've been much less active in programming, and only recently restarted with my C++ "journey", I've been re-learning a lot of old things I was once good at.
I've decided to start some experiments, beginning with image algorithms and software rendering type stuff.
For the sake of retention, rather than starting with OpenGL (which is easy anyway) to display the results directly in a window, I've decided to re-implement my rudimentary Microsoft BMP image support. So I'll just render my image results into BMP files.
The Microsoft Bitmap (*.bmp) format
http://msdn.microsoft.com/en-us/library/dd183391%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/dd183374%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/dd183376%28v=vs.85%29.aspx
I have this so far:
msbmp.h
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
|
#ifndef _MSBMP_H
#define _MSBMP_H
#ifndef w8
# define w8 unsigned char
#endif
#ifndef w16
# define w16 unsigned short int
#endif
#ifndef w32
# define w32 unsigned long int
#endif
#ifndef MemoryChunk
struct MemoryChunk
{
w32 size;
w8* data;
};
#endif
#ifndef Mchunk
# define Mchunk MemoryChunk
# define Mchunk MemoryChunk
#endif
#ifndef Color24
struct Color24
{
w8 red, green, blue;
};
#endif
#ifndef PixelMatrix
struct PixelMatrix
{
w32 area;
w16 width, height;
Color24* pixels;
};
#endif
Mchunk* MsBMPFormat (PixelMatrix&);
//PixelMatrix* MsBMPDeformat (Mchunk&);
#endif
|
msbmp.cpp
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
|
#include "msbmp.h"
Mchunk* MsBMPFormat (PixelMatrix& image)
{
// Create a memory chunk.
Mchunk* code;
code->size = 0;
code->data = 0;
// Figure the size.
// File Header Size = 14
// Info Header Size = 40
// Total Header Size = 54
w32 size = image.width * image.height * 3;
code->size = 54 + size;
// Format Data into Microsoft BMP format.
code->data = new w8[code->size];
code->data[0] = 0x4D;
code->data[1] = 0x42;
code->data[2] = (w8)((size >> 24) & 0xFF);
code->data[3] = (w8)((size >> 16) & 0xFF);
code->data[4] = (w8)((size >> 8) & 0xFF);
code->data[5] = (w8)(size & 0xFF);
code->data[6] = 0x0;
code->data[7] = 0x0;
code->data[8] = 0x0;
code->data[9] = 0x0;
// Work-in progress...
return code;
}
|
Please tell me if you see any immediate (minor) problems or have any suggestions to refresh my strife for robustness.
But I'm wonder if doing this
1 2 3 4
|
code->data[/* byte index */] = /*value*/;
code->data[/* byte index */] = /*value*/;
code->data[/* byte index */] = /*value*/;
// etc.
|
For all the determined bytes (like the file/information headers) will cause the generated object code to be bloated, instead of some other method, like a for loop which uses a constant byte array.
Also, I wonder if I should have an extra argument which points to a user defined output/write function, like this:
bool MsBMPFormat (PixelMatrix& image, void(*Out)(w8 byte), w32 *size);
That should be better, right?