943,809 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2498
  • C++ RSS
Oct 14th, 2008
0

Help with reading bitmap

Expand Post »
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)
  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.
Similar Threads
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Oct 14th, 2008
0

Re: Help with reading bitmap

Weird, I just used Windows' BITMAPFILEHEADER and BITMAPINFOHEADER structures and it worked fine. Thx anyway.
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: exporting functions from debugged dll
Next Thread in C++ Forum Timeline: need some help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC