bitmapBmp.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/stream/stream.h"
  23. #include "gfx/bitmap/gBitmap.h"
  24. static bool sReadBMP(Stream &stream, GBitmap *bitmap);
  25. static bool sWriteBMP(GBitmap *bitmap, Stream &stream, U32 compressionLevel);
  26. static struct _privateRegisterBMP
  27. {
  28. _privateRegisterBMP()
  29. {
  30. GBitmap::Registration reg;
  31. reg.extensions.push_back( "bmp" );
  32. reg.readFunc = sReadBMP;
  33. reg.writeFunc = sWriteBMP;
  34. GBitmap::sRegisterFormat( reg );
  35. }
  36. } sStaticRegisterBMP;
  37. // structures mirror those defined by the win32 API
  38. struct RGBQUAD
  39. {
  40. U8 rgbBlue;
  41. U8 rgbGreen;
  42. U8 rgbRed;
  43. U8 rgbReserved;
  44. };
  45. struct BITMAPFILEHEADER
  46. {
  47. U16 bfType;
  48. U32 bfSize;
  49. U16 bfReserved1;
  50. U16 bfReserved2;
  51. U32 bfOffBits;
  52. };
  53. struct BITMAPINFOHEADER
  54. {
  55. U32 biSize;
  56. S32 biWidth;
  57. S32 biHeight;
  58. U16 biPlanes;
  59. U16 biBitCount;
  60. U32 biCompression;
  61. U32 biSizeImage;
  62. S32 biXPelsPerMeter;
  63. S32 biYPelsPerMeter;
  64. U32 biClrUsed;
  65. U32 biClrImportant;
  66. };
  67. // constants for the biCompression field
  68. #define BI_RGB 0L
  69. #define BI_RLE8 1L
  70. #define BI_RLE4 2L
  71. #define BI_BITFIELDS 3L
  72. //------------------------------------------------------------------------------
  73. //-------------------------------------- Supplementary I/O (Partially located in
  74. // bitmapPng.cc)
  75. //
  76. static bool sReadBMP(Stream &stream, GBitmap *bitmap)
  77. {
  78. PROFILE_SCOPE(sReadBMP);
  79. BITMAPINFOHEADER bi;
  80. BITMAPFILEHEADER bf;
  81. RGBQUAD rgb[256];
  82. stream.read(&bf.bfType);
  83. stream.read(&bf.bfSize);
  84. stream.read(&bf.bfReserved1);
  85. stream.read(&bf.bfReserved2);
  86. stream.read(&bf.bfOffBits);
  87. stream.read(&bi.biSize);
  88. stream.read(&bi.biWidth);
  89. stream.read(&bi.biHeight);
  90. stream.read(&bi.biPlanes);
  91. stream.read(&bi.biBitCount);
  92. stream.read(&bi.biCompression);
  93. stream.read(&bi.biSizeImage);
  94. stream.read(&bi.biXPelsPerMeter);
  95. stream.read(&bi.biYPelsPerMeter);
  96. stream.read(&bi.biClrUsed);
  97. stream.read(&bi.biClrImportant);
  98. GFXFormat fmt = GFXFormatR8G8B8;
  99. if(bi.biBitCount == 8)
  100. {
  101. // read in texture palette
  102. if(!bi.biClrUsed)
  103. bi.biClrUsed = 256;
  104. stream.read(sizeof(RGBQUAD) * bi.biClrUsed, rgb);
  105. }
  106. bitmap->allocateBitmap(bi.biWidth, bi.biHeight, false, fmt);
  107. U32 width = bitmap->getWidth();
  108. U32 height = bitmap->getHeight();
  109. U32 bytesPerPixel = bitmap->getBytesPerPixel();
  110. for(U32 i = 0; i < bi.biHeight; i++)
  111. {
  112. U8 *rowDest = bitmap->getAddress(0, height - i - 1);
  113. if (bi.biBitCount == 8)
  114. {
  115. // use palette...don't worry about being slow
  116. for (S32 j=0; j<width; j++)
  117. {
  118. U8 palIdx;
  119. stream.read(&palIdx);
  120. U8 * pixelLocation = &rowDest[j*bytesPerPixel];
  121. pixelLocation[0] = rgb[palIdx].rgbRed;
  122. pixelLocation[1] = rgb[palIdx].rgbGreen;
  123. pixelLocation[2] = rgb[palIdx].rgbBlue;
  124. if (bytesPerPixel==3)
  125. pixelLocation[3] = 255;
  126. }
  127. }
  128. else
  129. stream.read(bytesPerPixel * width, rowDest);
  130. }
  131. if(bytesPerPixel == 3 && bi.biBitCount != 8) // do BGR swap
  132. {
  133. U8 *ptr = bitmap->getAddress(0,0);
  134. for(S32 i = 0; i < width * height; i++)
  135. {
  136. U8 tmp = ptr[0];
  137. ptr[0] = ptr[2];
  138. ptr[2] = tmp;
  139. ptr += 3;
  140. }
  141. }
  142. // We know BMP's don't have any transparency
  143. bitmap->setHasTransparency(false);
  144. return true;
  145. }
  146. static bool sWriteBMP(GBitmap *bitmap, Stream &stream, U32 compressionLevel)
  147. {
  148. TORQUE_UNUSED( compressionLevel ); // BMP does not use compression
  149. BITMAPINFOHEADER bi;
  150. BITMAPFILEHEADER bf;
  151. bi.biSize = sizeof(BITMAPINFOHEADER);
  152. bi.biWidth = bitmap->getWidth();
  153. bi.biHeight = bitmap->getHeight(); //our data is top-down
  154. bi.biPlanes = 1;
  155. if(bitmap->getFormat() == GFXFormatR8G8B8)
  156. {
  157. bi.biBitCount = 24;
  158. bi.biCompression = BI_RGB;
  159. bi.biClrUsed = 0;
  160. }
  161. else
  162. {
  163. bi.biBitCount = 0;
  164. bi.biCompression = BI_RGB; // Removes warning C4701 on line
  165. AssertISV(false, "GBitmap::writeMSBmp - only support R8G8B8 formats!");
  166. }
  167. U32 width = bitmap->getWidth();
  168. U32 height = bitmap->getHeight();
  169. U32 bytesPP = bi.biBitCount >> 3;
  170. bi.biSizeImage = width * height * bytesPP;
  171. bi.biXPelsPerMeter = 0;
  172. bi.biYPelsPerMeter = 0;
  173. bi.biClrUsed = 0;
  174. bi.biClrImportant = 0;
  175. bf.bfType = makeFourCCTag('B','M',0,0); //Type of file 'BM'
  176. bf.bfOffBits= sizeof(BITMAPINFOHEADER)
  177. + sizeof(BITMAPFILEHEADER)
  178. + (sizeof(RGBQUAD)*bi.biClrUsed);
  179. bf.bfSize = bf.bfOffBits + bi.biSizeImage;
  180. bf.bfReserved1 = 0;
  181. bf.bfReserved2 = 0;
  182. stream.write(bf.bfType);
  183. stream.write(bf.bfSize);
  184. stream.write(bf.bfReserved1);
  185. stream.write(bf.bfReserved2);
  186. stream.write(bf.bfOffBits);
  187. stream.write(bi.biSize);
  188. stream.write(bi.biWidth);
  189. stream.write(bi.biHeight);
  190. stream.write(bi.biPlanes);
  191. stream.write(bi.biBitCount);
  192. stream.write(bi.biCompression);
  193. stream.write(bi.biSizeImage);
  194. stream.write(bi.biXPelsPerMeter);
  195. stream.write(bi.biYPelsPerMeter);
  196. stream.write(bi.biClrUsed);
  197. stream.write(bi.biClrImportant);
  198. //write the bitmap bits
  199. U8* pMSUpsideDownBits = new U8[bi.biSizeImage];
  200. for (U32 i = 0; i < height; i++)
  201. {
  202. const U8* pSrc = bitmap->getAddress(0, i);
  203. U8* pDst = pMSUpsideDownBits + (height - i - 1) * width * bytesPP;
  204. dMemcpy(pDst, pSrc, width * bytesPP);
  205. }
  206. stream.write(bi.biSizeImage, pMSUpsideDownBits);
  207. delete [] pMSUpsideDownBits;
  208. return stream.getStatus() == Stream::Ok;
  209. }