1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct BmpSignature
{
unsigned char data[2]; //2 Signature check
BmpSignature() { data[0] = data[1] = 0; }
//2 Bytes
};
struct BmpHeader
{
unsigned int fileSize; //4 The size of the file in bytes
unsigned short reserved1;
unsigned short reserved2; //2 Both for apparent extension
unsigned long int dataOffset; //4 Start of Pixel Data
BmpHeader() : fileSize(0), reserved1(0), reserved2(0), dataOffset(0) { }
//12 Bytes
};
struct BmpInfoHeader
{
unsigned long int bmpInfoSize; // 4 Size of info header
unsigned long int bmpInfoWidth; //4 Image width in pixels
unsigned long int bmpInfoHeight; //4 Image height in pixels
unsigned short bmpInfoPlanes; //2 Must be 1
unsigned short bmpInfoBitCount; //2 Bits per pixel - 1, 4, 8, 16, 24, or 32
unsigned long int bmpInfoCompression; //4 Compression type (0 = uncompressed)
unsigned long int bmpInfoSizeImage; //4 Image Size - may be zero for uncompressed images
unsigned long int bmpInfoXPelsPerMeter; //4 Preferred resolution in pixels per meter
unsigned long int bmpInfoYPelsPerMeter; //4 Preferred resolution in pixels per meter
unsigned long int bmpInfoClrUsed; //4 Number Color Map entries that are actually used
unsigned long int bmpInfoClrImportant; //4 Number of Significant Colours
//40 Bytes
};
#pragma pack(push,1)
struct AddressArray
{
unsigned int ImageByteAddress []; //Memory location where the 24 bits have been stored
//But will not accept int at this point (in a struct) or a loop. A loop at print
//Isn't outing the address but packed nonsense. I don't know, where to loop for
//a list of addresses to be read 8 bits at a time.
};
#pragma pack(pop)
fstream fin("8ry328ry.bmp",ios::binary|ios::in); //open bitmap
void ReadHeader(ifstream &fin, BmpSignature &sig, BmpHeader &header) //name of function and access granting
{
if(!fin) // if file is not open end program
return;
fin.seekg(0, ios::beg); //go to start of Hexidecimal data
fin.read((char*) &sig, sizeof(sig)); //read from bitmap hexidecimal and store the equal amount of bit or bytes to corresponding
fin.read((char*) &header, sizeof(header));
}
void ReadInfoHeader(ifstream &fin, BmpInfoHeader &iheader)
{
fin.seekg(14, ios::beg); //Go to the 14th Byte
fin.read((char* ) &iheader, sizeof(iheader)); //read from bitmap hexidecimal and store equal amount of data to corresponding
}
void ReadPixelData(ifstream &fin, BmpHeader &header, BmpInfoHeader &iheader, AddressArray &BA)
{
fin.seekg(header.dataOffset, ios::beg); //locate pixel data using information from initial header
fin.read((char *) &BA.ImageByteAddress, sizeof(iheader.bmpInfoWidth*iheader.bmpInfoHeight*iheader.bmpInfoBitCount)); //Store all pixel data as an array the size of pixels*bits, so here 3 bytes each.
//I can use if statements for anything below 16 but right now I'm only working with 24bit
}
void PrintHeader(BmpSignature sig, BmpHeader header)
{
cout<<"==== BMP HEADER ===="<<endl;
cout<<endl;
cout<<"+ Signature : " << sig.data[0]<<sig.data[1]<<endl;
cout<<"+ File Size : " << header.fileSize<<" byte(s)"<<endl;
cout<<"+ Reserved1 : " << header.reserved1<<endl;
cout<<"+ Reserved2 : " << header.reserved2<<endl;
cout<<"+ Data Offset: " << header.dataOffset<<" byte(s)"<<endl;
}
void PrintBmpInfo(BmpInfoHeader iheader)
{
cout<<"==== BMP INFO HEADER ===="<<endl;
cout<<endl;
cout<<"+ Info Header Size: " <<iheader.bmpInfoSize<<" bytes"<<endl;
cout<<"+ Width of Bmp: " <<iheader.bmpInfoWidth<<" pixels"<<endl;
cout<<"+ Height of Bmp: "<<iheader.bmpInfoHeight<<" pixels"<<endl;
cout<<"+ Number of Planes: "<<iheader.bmpInfoPlanes<<endl;
cout<<"+ Number of Bits per pixel: "<<iheader.bmpInfoBitCount<<endl;
cout<<"+ Compression Type: "<<iheader.bmpInfoCompression<<endl;
cout<<"+ Pixel Data: "<<iheader.bmpInfoSizeImage<<endl;
cout<<"+ Prefered X Res: "<<iheader.bmpInfoXPelsPerMeter<<endl;
cout<<"+ Prefered Y Res: "<<iheader.bmpInfoYPelsPerMeter<<endl;
cout<<"+ Colour Map Entries in Use: "<<iheader.bmpInfoClrUsed<<endl;
cout<<"+ Number of Significant Colours: "<<iheader.bmpInfoClrImportant<<endl;
}
void PrintbitInfo(BmpInfoHeader iheader)
{
cout<<"+ Number of Pixels in Image: "<<iheader.bmpInfoWidth*iheader.bmpInfoHeight<<endl;
cout<<"+ Number of Bits Storing Pixel Information: "<<iheader.bmpInfoWidth*iheader.bmpInfoHeight*iheader.bmpInfoBitCount<<endl;
cout<<"+ Number of Pixel Bytes: "<<iheader.bmpInfoWidth*iheader.bmpInfoHeight*iheader.bmpInfoBitCount/8<<endl;
//The above prints, are printing all to screen, with a descriptor and a 'pointer' of what to print
}
void DataCheck(BmpInfoHeader iheader, BmpHeader header)
{
if (iheader.bmpInfoWidth*iheader.bmpInfoHeight*iheader.bmpInfoBitCount/8 == iheader.bmpInfoSizeImage)
bool DataCheck1 = true;
else
DataCheck1 = false;
if(iheader.bmpInfoSizeImage + iheader.bmpInfoSize + 14 == header.fileSize)
bool DataCheck2 = true;
else
DataCheck2 = false;
if (DataCheck1&&DataCheck2 = true)
cout<<"Data Compatable"<<endl; else
cout<<"Data Incompatable"<<endl;
//Quick DataCheck if Data Incompatable... Something has gone Wrong.
}
void PrintAddressArray(AddressArray BA, BmpInfoHeader iheader)
{
cout<<"==== Address Array ===="<<endl;
cout<<endl;
cout<<BA.ImageByteAddress<<endl;
// This is the printing problem if I loop this it's not printing addresses, it's printing long integers that seem irrelevant.
}
void PrintSpacingLine()
{
cout<<endl;
}
int main()
{
ifstream fin("8ry328ry.bmp", ios::binary);
BmpSignature sig;
BmpHeader hdr;
BmpInfoHeader ihdr;
AddressArray BA;
DataCheck(ihdr, hdr);
ReadHeader(fin, sig, hdr);
PrintHeader(sig, hdr);
PrintSpacingLine();
ReadInfoHeader(fin, ihdr);
ReadPixelData(fin, hdr, ihdr, BA);
PrintSpacingLine();
PrintBmpInfo(ihdr);
PrintSpacingLine();
PrintbitInfo(ihdr);
PrintSpacingLine();
PrintAddressArray(BA, ihdr);
return 0;
//This is for ordering the program.
}
| |