Advertisement
Guest User

getbmp.cpp

a guest
Feb 28th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. #include "getbmp.h"
  4.  
  5. using namespace std;
  6.  
  7. // Routine to read an uncompressed 24-bit color RGB bmp file into a
  8. // 32-bit color RGBA bitmap file (A value being set to 1).
  9. BitMapFile *getbmp(string filename)
  10. {
  11. int offset, headerSize;
  12.  
  13. // Initialize bitmap files for RGB (input) and RGBA (output).
  14. BitMapFile *bmpRGB = new BitMapFile;
  15. BitMapFile *bmpRGBA = new BitMapFile;
  16.  
  17. // Read input bmp file name.
  18. ifstream infile(filename.c_str(), ios::binary);
  19.  
  20. // Get starting point of image data in bmp file.
  21. infile.seekg(10);
  22. infile.read((char *)&offset, 4);
  23.  
  24. // Get header size of bmp file.
  25. infile.read((char *)&headerSize,4);
  26.  
  27. // Get image width and height values from bmp file header.
  28. infile.seekg(18);
  29. infile.read((char *)&bmpRGB->sizeX, 4);
  30. infile.read((char *)&bmpRGB->sizeY, 4);
  31.  
  32. // Determine the length of zero-byte padding of the scanlines
  33. // (each scanline of a bmp file is 4-byte aligned by padding with zeros).
  34. int padding = (3 * bmpRGB->sizeX) % 4 ? 4 - (3 * bmpRGB->sizeX) % 4 : 0;
  35.  
  36. // Add the padding to determine size of each scanline.
  37. int sizeScanline = 3 * bmpRGB->sizeX + padding;
  38.  
  39. // Allocate storage for image in input bitmap file.
  40. int sizeStorage = sizeScanline * bmpRGB->sizeY;
  41. bmpRGB->data = new unsigned char[sizeStorage];
  42.  
  43. // Read bmp file image data into input bitmap file.
  44. infile.seekg(offset);
  45. infile.read((char *) bmpRGB->data , sizeStorage);
  46.  
  47. // Reverse color values from BGR (bmp storage format) to RGB.
  48. int startScanline, endScanlineImageData, temp;
  49. for (int y = 0; y < bmpRGB->sizeY; y++)
  50. {
  51. startScanline = y * sizeScanline; // Start position of y'th scanline.
  52. endScanlineImageData = startScanline + 3 * bmpRGB->sizeX; // Image data excludes padding.
  53. for (int x = startScanline; x < endScanlineImageData; x += 3)
  54. {
  55. temp = bmpRGB->data[x];
  56. bmpRGB->data[x] = bmpRGB->data[x+2];
  57. bmpRGB->data[x+2] = temp;
  58. }
  59. }
  60.  
  61. // Set image width and height values and allocate storage for image in output bitmap file.
  62. bmpRGBA->sizeX = bmpRGB->sizeX;
  63. bmpRGBA->sizeY = bmpRGB->sizeY;
  64. bmpRGBA->data = new unsigned char[4*bmpRGB->sizeX*bmpRGB->sizeY];
  65.  
  66. // Copy RGB data from input to output bitmap files, set output A to 1.
  67. for(int j = 0; j < 4*bmpRGB->sizeY * bmpRGB->sizeX; j+=4)
  68. {
  69. bmpRGBA->data[j] = bmpRGB->data[(j/4)*3];
  70. bmpRGBA->data[j+1] = bmpRGB->data[(j/4)*3+1];
  71. bmpRGBA->data[j+2] = bmpRGB->data[(j/4)*3+2];
  72. bmpRGBA->data[j+3] = 0xFF;
  73. }
  74.  
  75. return bmpRGBA;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement