Header File:
struct BitmapHeader
{
unsigned char type[2];
unsigned int size;//
unsigned int reserved;
unsigned int offset;//
unsigned int header;
unsigned int width;//
unsigned int height;//
unsigned short planes;
unsigned short bits;
unsigned int compression;
unsigned int dataSize;
unsigned int horizontalResolution;
unsigned int verticalResolution;
unsigned int colors;
unsigned int importantColors;
};
You need a header file to read data into
///////////////////////////////////////////////////////////////////////////////
The following code works only for padded files.
#include <iostream>
#include <fstream>
#include "Utility.h"
using namespace std;
int main()
{
bool Ask = true;
char choice;
char indicator = 'w';
char InFileName[20], OutFileName[20];
unsigned char **pictdouble, **picshrink;;
// Ask File name
cout << "Enter: Input file name: ";
cin >> InFileName;
BitmapHeader pic, picdouble, shrink;
ifstream picture( InFileName, ios::in | ios::binary );
//this reads the header file
picture.read ((char*)&pic, sizeof(pic));
int widthsize = pic.width * 3;//to allocate enough room for the color values
/*int padding;
padding = widthsize%4;
cout<<"padding: "<<padding<<endl;
*/
unsigned char **picstore; //pointer to a pointer
picstore = new unsigned char*[pic.height];
for(int i = 0; i < pic.height; i++)
{
//each array index pointer now points to an array of unsigned chars
picstore[i] = new unsigned char[widthsize];
picture.read((char*) picstore[i], widthsize);
}
while (Ask == true)
{
//User options
cout << endl << endl;
cout << "Press i to inverse colors of the image :" << endl;
cout << "Press h to horizontally flip the image :" << endl;
cout << "Press v to vertically flip the image :" << endl;
cout << "Press g to make a grascale of the image :" << endl;
cout << "Press s to shrink the size of the image :" << endl;
cout << "Press d to double the size of the image :" << endl;
cout << "Press q to quit :" << endl;
cin >> choice;
// Perform action according to choice ladies.bmp
switch (choice)
{
case 's':
case 'S':
{
indicator = 's';
picshrink = new unsigned char*[pic.height/2];
for(int i = 0; i < pic.height/2; i++)
picshrink[i] = new unsigned char[widthsize/2];
ifstream picture5( "ladies.bmp", ios::in | ios::binary );
picture5.read ((char*)&shrink, sizeof(shrink));
shrink.height/=2;
shrink.width/=2;
shrink.dataSize/=4;
shrink.size = (shrink.dataSize + 54);
for(int i = 0, l= 0; i < pic.height; i+=2, l++)
{
for(int j = 0, k= 0; j < widthsize; j+=6,k+=3)
{
picshrink[l][k] = (picstore[i][j] + picstore[i][j+3] + picstore[i+1][j] + picstore[i+1][j+3])/4;
picshrink[l][k+1] = (picstore[i][j+1] + picstore[i][j+4] + picstore[i+1][j+1] + picstore[i+1][j+4])/4;
picshrink[l][k+2] = (picstore[i][j+2] + picstore[i][j+5] + picstore[i+1][j+2] + picstore[i+1][j+5])/4;
}
}
}
break;
case 'd':
case 'D':
{
indicator = 'd';
pictdouble = new unsigned char*[pic.height*2];
for(int i = 0; i < pic.height*2; i++)
pictdouble[i] = new unsigned char[widthsize*2];
ifstream picture6( "ladies.bmp", ios::in | ios::binary );
picture6.read ((char*)&picdouble, sizeof(picdouble));
picdouble.height *=2;
picdouble.width *=2;
picdouble.dataSize *=4;
picdouble.size = (picdouble.dataSize + 54);
for(int i = 0, l= 0; i < picdouble.height; i+=2, l++)
{
for(int j = 0, k= 0; j < picdouble.width*3; j+=6,k+=3)
{
pictdouble[i][j] = pictdouble[i][j+3] = pictdouble[i+1][j] = pictdouble[i+1][j+3] = picstore[l][k];
pictdouble[i][j+1] = pictdouble[i][j+4] = pictdouble[i+1][j+1] = pictdouble[i+1][j+4] = picstore[l][k+1];
pictdouble[i][j+2] = pictdouble[i][j+5] = pictdouble[i+1][j+2] = pictdouble[i+1][j+5] = picstore[l][k+2];
}
}
}
break;
case 'i':
case 'I':
{
if ((indicator != 's') || (indicator != 'S') || (indicator != 'd') || (indicator != 'D'))
indicator = 'i';
for(int i = 0; i < pic.height; i++)
{
for(int j = 0; j< widthsize; j++)
{
picstore[i][j] = 255 - picstore[i][j];
}
}
}
break;
case 'h':
case 'H':
{
if ((indicator != 's') || (indicator != 'S') || (indicator != 'd') || (indicator != 'D'))
indicator = 'h';
unsigned char **picreverse;
picreverse = new unsigned char*[pic.height];
for(int i = 0; i < pic.height; i++)
picreverse[i] = new unsigned char[widthsize];
for(int i = 0; i < pic.height; i++)
{
for(int j = 0; j < widthsize; j++)
{
picreverse[i][widthsize - 1 - j] = picstore[i][j];
}
for(int j = 0; j < widthsize; j+=3)
{
double temp;
temp = picreverse[i][j];
picreverse[i][j] = picreverse[i][j+2];
picreverse[i][j+2] = temp;
}
}
for(int i = 0; i < pic.height; i++)
{
for(int j = 0; j < widthsize; j++)
picstore[i][j] = picreverse[i][j];
}
for (int i = 0; i < pic.height; i++)
{
delete [] picreverse[i];
}
delete [] picreverse;
}
break;
case 'v':
case 'V':
{
if ((indicator != 's') || (indicator != 'S') || (indicator != 'd') || (indicator != 'D'))
indicator = 'v';
unsigned char **picreverse;
picreverse = new unsigned char*[pic.height];
for(int i = 0; i < pic.height; i++)
picreverse[i] = new unsigned char[widthsize];
for(int i = 0; i < pic.height; i++)
{
for(int j = 0; j < widthsize; j++)
{
picreverse[pic.height-i-1][j] = picstore[i][j];
}
}
for(int i = 0; i < pic.height; i++)
{
for(int j = 0; j < widthsize; j++)
{
picstore[i][j] = picreverse[i][j];
}
}
for (int i = 0; i < pic.height; i++)
{
delete [] picreverse[i];
}
delete [] picreverse;
}
break;
case 'g':
case 'G':
{
if ((indicator != 's') || (indicator != 'S') || (indicator != 'd') || (indicator != 'D'))
indicator = 'g';
for(int i = 0; i < pic.height; i++)
{
for(int j = 0; j < widthsize; j+=3)
{
picstore[i][j] = picstore[i][j+1] = picstore[i][j+2] = picstore[i][j]*.11 + picstore[i][j+1]*.59 + picstore[i][j+2]*.3;
}
}
}
break;
case 'q':
case 'Q':
{
//padding
//picture.seekg(1, istream::cur)
cout<<"What is the file you wish to save to: ";
cin>>OutFileName;
ofstream picture2( OutFileName, ios::out | ios::binary );
if (indicator=='s')
{
picture2.write ((char*)&shrink, sizeof(shrink));
for(int i = 0; i < shrink.height; i++)
picture2.write((char*) picshrink[i],widthsize/2);
}
else if (indicator == 'd')
{
picture2.write((char*)&picdouble, sizeof(picdouble));
for(int i= 0; i<picdouble.height ;i++ )
picture2.write((char*) pictdouble[i], picdouble.width*3);
}
else
{
picture2.write ((char*)&pic, sizeof(pic));
for(int i = 0; i < pic.height; i++)
picture2.write((char*) picstore[i],widthsize);
}
picture2.close();
cout << endl << "Your Program has been terminated..\n";
Ask = false;
break;
}
default :
cout << endl << "Please choose from the available options: \n";
}
}
//free up memory
for (int i = 0; i < pic.height; i++)
{
delete [] picstore[i];
}
delete [] picstore;
picture.close();
return 0;
}