I was searching the 'net for 24 bit to 16 bit bitmap conversion code.
I found this and modified it so that it should output the bitmap to a new file after conversion, but it doesn't work. Can anyone help me?
here is the code
Code:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
// Macros
struct BITMAPINFOHEADER1
{
DWORD biSize; // number of bytes in this structure
LONG biWidth; // width of the bitmap
LONG biHeight; // height of the bitmap
WORD biPlanes; // number of planes (don't worry about it)
WORD biBitCount; // bits per pixel (1,4,8,16,24 or 32)
WORD biCompression; // type of compression
DWORD biSizeImage; // size of image in Bytes
LONG biXPelsPerMeter; // x res of target display
LONG biYPelsPerMeter; // y res of target display
DWORD biClrUsed; // how many colors are used
DWORD biClrImportant; // number of important colors
};
#define RGB16(red, green, blue) ( ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3))
void LoadBitmap()
{
BITMAPINFOHEADER1 infoheader;
WORD *bitmapData;
WORD *bitmapDone;
FILE *bitmapFile;
BYTE red, green, blue;
bitmapFile = fopen("Grass.bmp", "rb");
fseek(bitmapFile, sizeof(BITMAPFILEHEADER), SEEK_SET);
fread(&infoheader, sizeof(BITMAPINFOHEADER1), 1, bitmapFile);
bitmapData = new WORD[infoheader.biWidth * infoheader.biHeight];
bitmapDone = new WORD[infoheader.biWidth * infoheader.biHeight];
for( int y=0; y<infoheader.biHeight; ++y)
{
for( int x=0; x<infoheader.biWidth; ++x)
{
fread(&blue, sizeof(BYTE), 1, bitmapFile);
fread(&green, sizeof(BYTE), 1, bitmapFile);
fread(&red, sizeof(BYTE), 1, bitmapFile);
bitmapData[y*infoheader.biWidth + x] = RGB16(red, green, blue);
}
}
int heightIndex = 0;
for( y=infoheader.biHeight-1; y>=0; --y)
{
for( int x=0; x<infoheader.biWidth; ++x)
bitmapDone[heightIndex*infoheader.biWidth + x] = bitmapData[y*infoheader.biWidth + x];
++heightIndex;
}
ofstream out;
out.open("Grass2.bmp");
int Index=0;
while (Index < infoheader.biSizeImage)
{
out << bitmapDone[Index];
Index++;
}
out.close();
}
int main()
{
LoadBitmap();
return 0;
}