bitmapBmp.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. BITMAPINFOHEADER bi;
  79. BITMAPFILEHEADER bf;
  80. RGBQUAD rgb[256];
  81. stream.read(&bf.bfType);
  82. stream.read(&bf.bfSize);
  83. stream.read(&bf.bfReserved1);
  84. stream.read(&bf.bfReserved2);
  85. stream.read(&bf.bfOffBits);
  86. stream.read(&bi.biSize);
  87. stream.read(&bi.biWidth);
  88. stream.read(&bi.biHeight);
  89. stream.read(&bi.biPlanes);
  90. stream.read(&bi.biBitCount);
  91. stream.read(&bi.biCompression);
  92. stream.read(&bi.biSizeImage);
  93. stream.read(&bi.biXPelsPerMeter);
  94. stream.read(&bi.biYPelsPerMeter);
  95. stream.read(&bi.biClrUsed);
  96. stream.read(&bi.biClrImportant);
  97. GFXFormat fmt = GFXFormatR8G8B8;
  98. if(bi.biBitCount == 8)
  99. {
  100. // read in texture palette
  101. if(!bi.biClrUsed)
  102. bi.biClrUsed = 256;
  103. stream.read(sizeof(RGBQUAD) * bi.biClrUsed, rgb);
  104. }
  105. bitmap->allocateBitmap(bi.biWidth, bi.biHeight, false, fmt);
  106. U32 width = bitmap->getWidth();
  107. U32 height = bitmap->getHeight();
  108. U32 bytesPerPixel = bitmap->getBytesPerPixel();
  109. for(U32 i = 0; i < bi.biHeight; i++)
  110. {
  111. U8 *rowDest = bitmap->getAddress(0, height - i - 1);
  112. if (bi.biBitCount == 8)
  113. {
  114. // use palette...don't worry about being slow
  115. for (S32 j=0; j<width; j++)
  116. {
  117. U8 palIdx;
  118. stream.read(&palIdx);
  119. U8 * pixelLocation = &rowDest[j*bytesPerPixel];
  120. pixelLocation[0] = rgb[palIdx].rgbRed;
  121. pixelLocation[1] = rgb[palIdx].rgbGreen;
  122. pixelLocation[2] = rgb[palIdx].rgbBlue;
  123. if (bytesPerPixel==3)
  124. pixelLocation[3] = 255;
  125. }
  126. }
  127. else
  128. stream.read(bytesPerPixel * width, rowDest);
  129. }
  130. if(bytesPerPixel == 3 && bi.biBitCount != 8) // do BGR swap
  131. {
  132. U8 *ptr = bitmap->getAddress(0,0);
  133. for(S32 i = 0; i < width * height; i++)
  134. {
  135. U8 tmp = ptr[0];
  136. ptr[0] = ptr[2];
  137. ptr[2] = tmp;
  138. ptr += 3;
  139. }
  140. }
  141. // We know BMP's don't have any transparency
  142. bitmap->setHasTransparency(false);
  143. return true;
  144. }
  145. static bool sWriteBMP(GBitmap *bitmap, Stream &stream, U32 compressionLevel)
  146. {
  147. TORQUE_UNUSED( compressionLevel ); // BMP does not use compression
  148. BITMAPINFOHEADER bi;
  149. BITMAPFILEHEADER bf;
  150. bi.biSize = sizeof(BITMAPINFOHEADER);
  151. bi.biWidth = bitmap->getWidth();
  152. bi.biHeight = bitmap->getHeight(); //our data is top-down
  153. bi.biPlanes = 1;
  154. if(bitmap->getFormat() == GFXFormatR8G8B8)
  155. {
  156. bi.biBitCount = 24;
  157. bi.biCompression = BI_RGB;
  158. bi.biClrUsed = 0;
  159. }
  160. else
  161. {
  162. bi.biBitCount = 0;
  163. bi.biCompression = BI_RGB; // Removes warning C4701 on line
  164. AssertISV(false, "GBitmap::writeMSBmp - only support R8G8B8 formats!");
  165. }
  166. U32 width = bitmap->getWidth();
  167. U32 height = bitmap->getHeight();
  168. U32 bytesPP = bi.biBitCount >> 3;
  169. bi.biSizeImage = width * height * bytesPP;
  170. bi.biXPelsPerMeter = 0;
  171. bi.biYPelsPerMeter = 0;
  172. bi.biClrUsed = 0;
  173. bi.biClrImportant = 0;
  174. bf.bfType = makeFourCCTag('B','M',0,0); //Type of file 'BM'
  175. bf.bfOffBits= sizeof(BITMAPINFOHEADER)
  176. + sizeof(BITMAPFILEHEADER)
  177. + (sizeof(RGBQUAD)*bi.biClrUsed);
  178. bf.bfSize = bf.bfOffBits + bi.biSizeImage;
  179. bf.bfReserved1 = 0;
  180. bf.bfReserved2 = 0;
  181. stream.write(bf.bfType);
  182. stream.write(bf.bfSize);
  183. stream.write(bf.bfReserved1);
  184. stream.write(bf.bfReserved2);
  185. stream.write(bf.bfOffBits);
  186. stream.write(bi.biSize);
  187. stream.write(bi.biWidth);
  188. stream.write(bi.biHeight);
  189. stream.write(bi.biPlanes);
  190. stream.write(bi.biBitCount);
  191. stream.write(bi.biCompression);
  192. stream.write(bi.biSizeImage);
  193. stream.write(bi.biXPelsPerMeter);
  194. stream.write(bi.biYPelsPerMeter);
  195. stream.write(bi.biClrUsed);
  196. stream.write(bi.biClrImportant);
  197. //write the bitmap bits
  198. U8* pMSUpsideDownBits = new U8[bi.biSizeImage];
  199. for (U32 i = 0; i < height; i++)
  200. {
  201. const U8* pSrc = bitmap->getAddress(0, i);
  202. U8* pDst = pMSUpsideDownBits + (height - i - 1) * width * bytesPP;
  203. dMemcpy(pDst, pSrc, width * bytesPP);
  204. }
  205. stream.write(bi.biSizeImage, pMSUpsideDownBits);
  206. delete [] pMSUpsideDownBits;
  207. return stream.getStatus() == Stream::Ok;
  208. }