Reading Bitmap

Started by
3 comments, last by JohnBolton 17 years, 1 month ago
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.
Advertisement
For a last resort, try http://www.wotsit.org/

Otherwise, probably use something either from SDL or the .NET library, or whatever you're currently using which can make this task easier.
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 -1using 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()){			line2.append(line);	        //line2.append(ReturnLine);				/////////////////////////////////////////////////////////////////////////////////////////////////					}// end second if	 }// end while loop    myfile.close();  }// first if   else cout << "Unable to open file";   	myNewFile<<line2 << endl;	myNewFile.close();	    return 0;}
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 3int main(int argc, char *argv[]){  FILE *pFile=NULL;  pFile = fopen("c:\\mybitmap.bmp", "wb");  BITMAPFILEHEADER fileheader;  fileheader.bfType = 0x4d42;  fileheader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + depth*width*height;  fileheader.bfReserved1 = 0;  fileheader.bfReserved2 = 0;  fileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  //fprintf(pFile,  BITMAPINFOHEADER infoheader;  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;          }          else          {               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;  fclose(pFile);  return 0;}


I know it is mostly C but it works and will give you an idea of a bitmap structure.
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement