| | |
Help with reading bitmap
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi, I'm reading a bitmap manually to try and make a program that does steganography, but I can't read the file correctly. The headers I read come out completely messed up, and they fail the check to see if I'm reading a bitmap file or not (if (FileHeader.bfType != (int)"BM")). Please help.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stdio.h> using namespace std; class BMP { public: typedef struct { WORD bfType; DWORD bfSize; DWORD bfReserved; DWORD bfOffBits; } BMP_FILE_HEADER; typedef struct { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BMP_INFO_HEADER; BMP_FILE_HEADER FileHeader; BMP_INFO_HEADER InfoHeader; unsigned char *Data; // Methods bool LoadBitmapFile(const char *Filename); }; bool BMP::LoadBitmapFile(const char *Filename) { BMP Bitmap; unsigned char *bitmap_Image; FILE *File = NULL; unsigned int ImageIdx = 0; File = fopen(Filename,"rb"); if (!File) return false; // Read the file header fread(&Bitmap.FileHeader, sizeof(BMP_FILE_HEADER), 1, File); // Check if its a bitmap or not if (FileHeader.bfType != (int)"BM") return false; // Read the info header fread(&Bitmap.InfoHeader, sizeof(BMP_INFO_HEADER), 1, File); // Store the data memcpy((char*)&this->FileHeader, (char*)&Bitmap.FileHeader, sizeof(BMP_FILE_HEADER)); memcpy((char*)&this->InfoHeader, (char*)&Bitmap.InfoHeader, sizeof(BMP_INFO_HEADER)); // Move to the bitmap data fseek(File, Bitmap.FileHeader.bfOffBits, SEEK_SET); // Allocate memory if (Bitmap.InfoHeader.biSizeImage != 0) bitmap_Image = (unsigned char*)calloc(Bitmap.InfoHeader.biSizeImage, sizeof(unsigned char)); if (!bitmap_Image) { free(bitmap_Image); fclose(File); return false; } // Get RGB instead of BGR for (ImageIdx=0; ImageIdx < Bitmap.InfoHeader.biSizeImage; ImageIdx+=3) { bitmap_Image[ImageIdx] = bitmap_Image[+2]; bitmap_Image[ImageIdx+2] = bitmap_Image[ImageIdx]; } // Store the bitmap data memcpy((unsigned char*)&this->Data, (unsigned char*)&bitmap_Image, Bitmap.InfoHeader.biSizeImage); // Clean up and return fclose(File); return true; }; int main(int argc, char **argv) { BMP bmp; if (bmp.LoadBitmapFile("untitled.bmp")) { printf("UNTITLED.BMP\n\n"); cout << bmp.InfoHeader.biWidth << " by " << bmp.InfoHeader.biHeight; } else cout << "ERROR!"; cin.ignore(); }
Last edited by TheBeast32; Oct 14th, 2008 at 8:26 pm.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
--Martin Golding
![]() |
Similar Threads
- fstream Tutorial (C++)
- Help in C program (Image reading) (C)
- Reading binary data from SPROC..help please... (C#)
- How to read .ocx file in C++ (Not MFC) (C++)
- Using OpenGL in Visual C++: Part I (Game Development)
- Read encrypted file header (C++)
- HDC's (C++)
- Inverting 24-bit bitmap using fstream c++ (C++)
- Reasonable or not (C++)
- error C2375: 'my_strdup' : redefinition; different linkage (C++)
Other Threads in the C++ Forum
- Previous Thread: exporting functions from debugged dll
- Next Thread: need some help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





