Help with reading bitmap

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Help with reading bitmap

 
0
  #1
Oct 14th, 2008
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.

  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5.  
  6. class BMP
  7. {
  8. public:
  9.  
  10. typedef struct
  11. {
  12. WORD bfType;
  13. DWORD bfSize;
  14. DWORD bfReserved;
  15. DWORD bfOffBits;
  16. } BMP_FILE_HEADER;
  17.  
  18. typedef struct
  19. {
  20. DWORD biSize;
  21. LONG biWidth;
  22. LONG biHeight;
  23. WORD biPlanes;
  24. WORD biBitCount;
  25. DWORD biCompression;
  26. DWORD biSizeImage;
  27. LONG biXPelsPerMeter;
  28. LONG biYPelsPerMeter;
  29. DWORD biClrUsed;
  30. DWORD biClrImportant;
  31. } BMP_INFO_HEADER;
  32.  
  33. BMP_FILE_HEADER FileHeader;
  34. BMP_INFO_HEADER InfoHeader;
  35. unsigned char *Data;
  36.  
  37. // Methods
  38. bool LoadBitmapFile(const char *Filename);
  39. };
  40.  
  41. bool BMP::LoadBitmapFile(const char *Filename)
  42. {
  43. BMP Bitmap;
  44. unsigned char *bitmap_Image;
  45.  
  46. FILE *File = NULL;
  47. unsigned int ImageIdx = 0;
  48.  
  49. File = fopen(Filename,"rb");
  50.  
  51. if (!File)
  52. return false;
  53.  
  54. // Read the file header
  55. fread(&Bitmap.FileHeader, sizeof(BMP_FILE_HEADER), 1, File);
  56.  
  57. // Check if its a bitmap or not
  58. if (FileHeader.bfType != (int)"BM")
  59. return false;
  60.  
  61. // Read the info header
  62. fread(&Bitmap.InfoHeader, sizeof(BMP_INFO_HEADER), 1, File);
  63.  
  64. // Store the data
  65. memcpy((char*)&this->FileHeader, (char*)&Bitmap.FileHeader, sizeof(BMP_FILE_HEADER));
  66. memcpy((char*)&this->InfoHeader, (char*)&Bitmap.InfoHeader, sizeof(BMP_INFO_HEADER));
  67.  
  68. // Move to the bitmap data
  69. fseek(File, Bitmap.FileHeader.bfOffBits, SEEK_SET);
  70.  
  71. // Allocate memory
  72. if (Bitmap.InfoHeader.biSizeImage != 0)
  73. bitmap_Image = (unsigned char*)calloc(Bitmap.InfoHeader.biSizeImage, sizeof(unsigned char));
  74.  
  75. if (!bitmap_Image)
  76. {
  77. free(bitmap_Image);
  78. fclose(File);
  79. return false;
  80. }
  81.  
  82. // Get RGB instead of BGR
  83. for (ImageIdx=0; ImageIdx < Bitmap.InfoHeader.biSizeImage; ImageIdx+=3)
  84. {
  85. bitmap_Image[ImageIdx] = bitmap_Image[+2];
  86. bitmap_Image[ImageIdx+2] = bitmap_Image[ImageIdx];
  87. }
  88.  
  89. // Store the bitmap data
  90. memcpy((unsigned char*)&this->Data, (unsigned char*)&bitmap_Image, Bitmap.InfoHeader.biSizeImage);
  91.  
  92. // Clean up and return
  93. fclose(File);
  94. return true;
  95. };
  96.  
  97. int main(int argc, char **argv)
  98. {
  99. BMP bmp;
  100.  
  101. if (bmp.LoadBitmapFile("untitled.bmp"))
  102. {
  103. printf("UNTITLED.BMP\n\n");
  104. cout << bmp.InfoHeader.biWidth << " by " << bmp.InfoHeader.biHeight;
  105. }
  106. else
  107. cout << "ERROR!";
  108.  
  109. cin.ignore();
  110.  
  111. }
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Help with reading bitmap

 
0
  #2
Oct 14th, 2008
Weird, I just used Windows' BITMAPFILEHEADER and BITMAPINFOHEADER structures and it worked fine. Thx anyway.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC