Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

hpquasar

looking for algorithm to open bmp files

This topic is 5279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Does anyone has a c++ algorith to open a bmp file and modify it under Windows XP OS. I will appreciate if you have it if you can share it with e thanks

Share this post


Link to post
Share on other sites
Advertisement
(vc6/vc7)
bitmap.h :

#ifndef BITMAP_STUFF
#define BITMAP_STUFF

#include <stdio.h>

#pragma pack(push, structPackSize)
#pragma pack(2)

struct bitmapHeader {
//FileHeader

short type;//19778

unsigned int fileSize;
short reserved1;//0

short reserved2;//0

int offsetBits;//118

//InfoHeader

int infoHeaderSize;//40

int width;
int height;
short planes;//1

short bitCount;
int compression;//0

unsigned int imageSize;
int xPelsPerMeter;//0

int yPelsPerMeter;//0

int colorsUsed;//0

int colorsImportant;//0

};

#pragma pack(pop, structPackSize)

void initBitmapHeader(bitmapHeader &b, int width, int height, int bitcount);
void printBitmapHeader(bitmapHeader b);
void freadBitmapHeader(FILE *f, bitmapHeader &b);
void fwriteBitmapHeader(FILE *f, bitmapHeader b);
bool freadBitmapData(FILE* f, bitmapHeader b, unsigned char *data);
void fwriteBitmapData(FILE* f, bitmapHeader b, unsigned char* data);

#endif



bitmap.cpp :

 
#include "bitmap.h"

#define PIXELSIZE 3

void initBitmapHeader(bitmapHeader &b, int width, int height, int bitcount)
{
int padding = (4 - (width*PIXELSIZE)%4)%4;

b.type = 19778;
b.fileSize = (width)*height*(bitcount/8) + padding*height + sizeof(bitmapHeader);
b.reserved1 = 0;
b.reserved2 = 0;
b.offsetBits = sizeof(bitmapHeader);
b.infoHeaderSize = 40;
b.width = width;
b.height = height;
b.planes = 1;
b.bitCount = bitcount;
b.compression = 0;
b.imageSize = (width)*height*(bitcount/8) + padding*height;
b.xPelsPerMeter = 0;
b.yPelsPerMeter = 0;
b.colorsUsed = 0;
b.colorsImportant = 0;
}

void printBitmapHeader(bitmapHeader b)
{
printf(
"type : %i\n"
"filesize : %i\n"
"reserved1 : %i\n"
"reserved2 : %i\n"
"offsetBits : %i\n"
"infoHeaderSize : %i\n"
"width : %i\n"
"height : %i\n"
"planes : %i\n"
"bitCount : %i\n"
"compression : %i\n"
"imageSize : %i\n"
"xPelPerMeter : %i\n"
"yPelsPerMeter : %i\n"
"colorsUsed : %i\n"
"colorsImportant : %i\n"
,
b.type,
b.fileSize,
b.reserved1,
b.reserved2,
b.offsetBits,
b.infoHeaderSize,
b.width,
b.height,
b.planes,
b.bitCount,
b.compression,
b.imageSize,
b.xPelsPerMeter,
b.yPelsPerMeter,
b.colorsUsed,
b.colorsImportant
);
}

void freadBitmapHeader(FILE *f, bitmapHeader &b)
{
fread(&b.type, 1, sizeof(short), f);
fread(&b.fileSize, 1, sizeof(int), f);
fread(&b.reserved1, 1, sizeof(short), f);
fread(&b.reserved2, 1, sizeof(short), f);
fread(&b.offsetBits, 1, sizeof(int), f);
fread(&b.infoHeaderSize, 1, sizeof(int), f);
fread(&b.width, 1, sizeof(int), f);
fread(&b.height, 1, sizeof(int), f);
fread(&b.planes, 1, sizeof(short), f);
fread(&b.bitCount, 1, sizeof(short), f);
fread(&b.compression, 1, sizeof(int), f);
fread(&b.imageSize, 1, sizeof(int), f);
fread(&b.xPelsPerMeter, 1, sizeof(int), f);
fread(&b.yPelsPerMeter, 1, sizeof(int), f);
fread(&b.colorsUsed, 1, sizeof(int), f);
fread(&b.colorsImportant, 1, sizeof(int), f);
}

void fwriteBitmapHeader(FILE *f, bitmapHeader b)
{
fwrite(&b.type, 1, sizeof(short), f);
fwrite(&b.fileSize, 1, sizeof(int), f);
fwrite(&b.reserved1, 1, sizeof(short), f);
fwrite(&b.reserved2, 1, sizeof(short), f);
fwrite(&b.offsetBits, 1, sizeof(int), f);
fwrite(&b.infoHeaderSize, 1, sizeof(int), f);
fwrite(&b.width, 1, sizeof(int), f);
fwrite(&b.height, 1, sizeof(int), f);
fwrite(&b.planes, 1, sizeof(short), f);
fwrite(&b.bitCount, 1, sizeof(short), f);
fwrite(&b.compression, 1, sizeof(int), f);
fwrite(&b.imageSize, 1, sizeof(int), f);
fwrite(&b.xPelsPerMeter, 1, sizeof(int), f);
fwrite(&b.yPelsPerMeter, 1, sizeof(int), f);
fwrite(&b.colorsUsed, 1, sizeof(int), f);
fwrite(&b.colorsImportant, 1, sizeof(int), f);
}

bool freadBitmapData(FILE* f, bitmapHeader b, unsigned char *data)
{
unsigned char dummyData[4];
int padding = (b.imageSize-(b.width*b.height*PIXELSIZE))/b.height;
for (int row = 0; row < b.height; row++) {
if (fread(data+(row*(b.width*PIXELSIZE)), 1, b.width*PIXELSIZE, f) != b.width*PIXELSIZE) {
return false;
}
fread(dummyData, 1, padding, f);
}
return true;
}

void fwriteBitmapData(FILE* f, bitmapHeader b, unsigned char* data)
{
unsigned char dummyData[4] = {0,0,0,0};
int padding = (4 - (b.width*PIXELSIZE)%4)%4;
for (int row = 0; row < b.height; row++) {
fwrite(data+(row*(b.width*PIXELSIZE)), 1, b.width*PIXELSIZE, f);
fwrite(dummyData, 1, padding, f);
}
}


to read a bitmap:
FILE* f = ...
bitmapHeader bh;
freadBitmapHeader(f, bh);
unsigned char* data = new unsigned char[bh.imageSize];
freadBitmapData(f, bh, data);

to write a bitmap
FILE* f = ...
bitmapHeader bh;
initBitmapHeader(bh, 320, 240, 24);
fwriteBitmapHeader(f, bh);
fwriteBitmapData(f, bh, data);

PIXELSIZE is defined as 3 in .cpp file, change if you want to work with files other than 24bit images. Manipulate pixels like this:
int index = y*width+x;
data[3*index+0] = red;
data[3*index+1] = green;
data[3*index+2] = blue;



[edited by - zppz on June 7, 2004 3:42:19 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!