Jump to content
  • Advertisement
Sign in to follow this  

Reading Bitmap

This topic is 4101 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

Can someone point me in the right direction? I want to read a Bitmap (each pixels hex value, or whatever they use). Then use some sort of algorithm to reduce the amount of colors. Then save bitmap again.

Share this post

Link to post
Share on other sites
This is what i got so far, Im pretty sure i have to read it in binary which doesnt copy over at all.

#include "stdafx.h"

// sections to find and import all else skip Mesh {, MeshNormals {, vertecies, index

// reading a text file
#include <iostream>
#include <fstream>
#include <string>

#define negative -1
using namespace std;
ifstream::pos_type size;

int _tmain(int argc, _TCHAR* argv[]){

string line;
string line2;
string ReturnLine = "\n";

ifstream myfile ("bitmap.bmp", ios::in|ios::binary|ios::ate);
ofstream myNewFile("newsbitmap.bmp", ios::out | ios::binary);

if (myfile.is_open())
while (! myfile.eof() )
getline (myfile,line);

cout<< line<< endl;

string::iterator check = line.begin();
if(check != line.end()){

}// end second if
}// end while loop

}// first if

else cout << "Unable to open file";

myNewFile<<line2 << endl;

return 0;

Share this post

Link to post
Share on other sites
Bitmaps all have a BITMAPFILEHEADER and a BITMAPINFOHEADER which describe the bitmap file.

The fileheader is going to have the "magic number", size of the file and the offset to where the data starts.

The infoheader is going to have the size of it's own structure, width and height of the bmp, and the bitdepth. Also information rergarding the compression algorithm used (if any) and a color index.

The link that NyteGard posted will be very valuable and if no compression is used deciphering the bmp will be easy.

I'll post a quick and dirty C program a wrote to create a simple bitmap with no compression. It may help you.

#include <stdio.h>
#include <windows.h>

#define width 512
#define height 512
#define depth 3

int main(int argc, char *argv[])
pFile = fopen("c:\\mybitmap.bmp", "wb");

fileheader.bfType = 0x4d42;
fileheader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + depth*width*height;
fileheader.bfReserved1 = 0;
fileheader.bfReserved2 = 0;
fileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


infoheader.biSize = sizeof(BITMAPINFOHEADER);
infoheader.biWidth = width;
infoheader.biHeight = height;
infoheader.biPlanes = 1;
infoheader.biBitCount = depth*8;
infoheader.biCompression = BI_RGB;
infoheader.biSizeImage = depth*width*height;
infoheader.biXPelsPerMeter = 0;
infoheader.biYPelsPerMeter = 0;
infoheader.biClrUsed = 0;
infoheader.biClrImportant = 0;

BYTE *pData = new BYTE[depth*width*height];
for (int x = 0; x < width; ++x)
for (int y = 0; y < depth*height; y+=3)
if ((y/3)==x || (x==width-y/depth))
pData[(x*depth*width)+y] = 0xFF;
pData[(x*depth*width)+y+1] = 0xFF;
pData[(x*depth*width)+y+2] = 0xFF;
pData[(x*depth*width)+y] = x/2;
pData[(x*depth*width)+y+1] = 0x00;
pData[(x*depth*width)+y+2] = x/2;
fwrite(&fileheader, sizeof(BITMAPFILEHEADER), 1, pFile);
fwrite(&infoheader, sizeof(BITMAPINFOHEADER), 1, pFile);
fwrite(pData, sizeof(BYTE)*depth*width*height, 1, pFile);

delete [] pData;
return 0;

I know it is mostly C but it works and will give you an idea of a bitmap structure.

Share this post

Link to post
Share on other sites
getline() is not appropriate for binary I/O, use myfile.read() instead.

If you don't have to write the code yourself, there are plenty of libraries (and applications) that will do it for you.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!