#include "bitmap.h" Bitmap::Bitmap() { data = NULL; return; } Bitmap::~Bitmap() { delete [] data; return; } void Bitmap::readBitmap(string fileName) { // ifstream inFile(fileName.c_str()); FILE* inFile; inFile = fopen(fileName.c_str(), "rb"); if (!inFile) { // cout << "Failed to open " << fileName << endl; exit(1); } // char c = inFile.get(); char c = getc(inFile); assert(c == 'B'); // c = inFile.get(); c = getc(inFile); assert(c == 'M'); fileSize = readLong(inFile); readShort(inFile); readShort(inFile); offset = readLong(inFile); headerSize = readLong(inFile); width = readLong(inFile); height = readLong(inFile); trueW = width; trueH = height; readShort(inFile); bitsPerPixel = readShort(inFile); assert(bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8 || bitsPerPixel == 24); readLong(inFile); imageSize = readLong(inFile); readLong(inFile); readLong(inFile); numberOfColors = readLong(inFile); numberOfImpColors = readLong(inFile); switch (bitsPerPixel) { case 1: // cout << "Not implemented yet" << endl; break; case 4: // cout << "Not implemented yet" << endl; break; case 8: // cout << "Not implemented yet" << endl; break; case 24: { int bytesPerRow = ((3*width + 3) >> 2) << 2; delete[] data; data = new unsigned char[height*bytesPerRow]; unsigned char* p = data; for (int i = 0; i < height; i++) { int j; for (j = 0; j < width; j++) { // *(p+2) = inFile.get(); // Read blue // *(p+1) = inFile.get(); // Read green // *p = inFile.get(); // Read red *(p+2) = getc(inFile); // Read blue *(p+1) = getc(inFile); // Read green *p = getc(inFile); // Read red p += 3; } int k=3*j; for (int k = 3*j; k < bytesPerRow; k++) { // inFile.get(); getc(inFile); *(p++) = 0; } } break; } } // inFile.close(); fclose(inFile); return; } //short Bitmap::readShort(ifstream& in) short Bitmap::readShort(FILE* in) { unsigned char low, high; // low = in.get(); // high = in.get(); low = getc(in); high = getc(in); short value = high; value <<= 8; value |= low; return value; } //long Bitmap::readLong(ifstream& in) long Bitmap::readLong(FILE* in) { unsigned char byte0, byte1, byte2, byte3; // byte0 = in.get(); // byte1 = in.get(); // byte2 = in.get(); // byte3 = in.get(); byte0 = getc(in); byte1 = getc(in); byte2 = getc(in); byte3 = getc(in); long value = byte3; value <<= 8; value |= byte2; value <<= 8; value |= byte1; value <<= 8; value |= byte0; return value; } int Bitmap::powerOf2(int n) { int power = 1; n--; while (n) { n >>= 1; power <<= 1; } return power; } void Bitmap::resizeToPowerOf2() { int bytesPerRow = ((3*trueW + 3) >> 2) << 2; width = powerOf2(trueW); height = powerOf2(trueH); imageSize = 3*width*height; unsigned char* newData = new unsigned char[imageSize]; unsigned char* p = data; unsigned char* q = newData; for (int j = 0; j < trueH; j++) { for (int i = 0; i < bytesPerRow; i++) *(q++) = *(p++); for (int i = bytesPerRow; i < 3*width; i++) *(q++) = 0; } for (int j = trueH; j < height; j++) for (int i = 0; i < 3*width; i++) *(q++) = 0; delete[] data; data = newData; return; }