2
0

LoadBMP.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Image/LoadBMP.h>
  6. #include <Image/BlitSurface.h>
  7. #include <Image/Surface.h>
  8. #pragma pack (1)
  9. struct BitmapFileHeader
  10. {
  11. char mTypeB;
  12. char mTypeM;
  13. uint32 mSize;
  14. uint16 mReserved1;
  15. uint16 mReserved2;
  16. uint32 mOffBits;
  17. };
  18. struct BitmapInfoHeader
  19. {
  20. uint32 mSize;
  21. uint32 mWidth;
  22. uint32 mHeight;
  23. uint16 mPlanes;
  24. uint16 mBitCount;
  25. uint32 mCompression;
  26. uint32 mSizeImage;
  27. uint32 mXPelsPerMeter;
  28. uint32 mYPelsPerMeter;
  29. uint32 mClrUsed;
  30. uint32 mClrImportant;
  31. };
  32. #pragma pack ()
  33. Ref<Surface> LoadBMP(istream &inStream)
  34. {
  35. bool loaded = true;
  36. // Read bitmap info
  37. BitmapFileHeader bfh;
  38. BitmapInfoHeader bih;
  39. inStream.read((char *)&bfh, sizeof(bfh));
  40. if (inStream.fail())
  41. return nullptr;
  42. inStream.read((char *)&bih, sizeof(bih));
  43. if (inStream.fail())
  44. return nullptr;
  45. // Get properties
  46. int bpp = (bih.mBitCount + 7) >> 3;
  47. int scan_width = (bih.mWidth * bpp + 3) & (~3);
  48. // Check if it is a bitmap
  49. if (bfh.mTypeB != 'B' || bfh.mTypeM != 'M')
  50. {
  51. Trace("Not a BMP");
  52. return nullptr;
  53. }
  54. // Check if bitmap is bottom-up
  55. if (bih.mHeight <= 0)
  56. {
  57. Trace("Not bottom-up");
  58. return nullptr;
  59. }
  60. // Check if it is not compressed
  61. if (bih.mCompression != 0)
  62. {
  63. Trace("Is compressed");
  64. return nullptr;
  65. }
  66. Ref<Surface> surface;
  67. if (bih.mBitCount == 8)
  68. {
  69. // Load palette
  70. uint32 *palette = new uint32 [256];
  71. int pal_bytes = 4 * (bih.mClrUsed != 0? bih.mClrUsed : 256);
  72. inStream.read((char *)palette, pal_bytes);
  73. loaded = loaded && !inStream.fail();
  74. // Seek to image data
  75. inStream.seekg(bfh.mOffBits);
  76. // Convert pixel data to a surface
  77. surface = new SoftwareSurface(bih.mWidth, bih.mHeight, ESurfaceFormat::X8R8G8B8);
  78. surface->Lock(ESurfaceLockMode::Write);
  79. uint8 *scan_line = new uint8 [scan_width];
  80. for (int y = bih.mHeight - 1; y >= 0; --y)
  81. {
  82. // Load one scan line
  83. inStream.read((char *)scan_line, scan_width);
  84. loaded = loaded && !inStream.fail();
  85. // Copy one scan line
  86. uint8 *in_pixel = scan_line;
  87. uint32 *out_pixel = (uint32 *)surface->GetScanLine(y);
  88. for (uint x = 0; x < bih.mWidth; ++x, ++in_pixel, ++out_pixel)
  89. *out_pixel = palette[*in_pixel];
  90. }
  91. surface->UnLock();
  92. // Release temporaries
  93. delete [] palette;
  94. delete [] scan_line;
  95. }
  96. else
  97. {
  98. // Determine pixel format
  99. ESurfaceFormat format;
  100. switch (bih.mBitCount)
  101. {
  102. case 16: format = ESurfaceFormat::X1R5G5B5; break;
  103. case 24: format = ESurfaceFormat::R8G8B8; break;
  104. default: Trace("Has invalid format"); return nullptr;
  105. }
  106. // Seek to image data
  107. inStream.seekg(bfh.mOffBits);
  108. // Convert pixel data to a surface
  109. surface = new SoftwareSurface(bih.mWidth, bih.mHeight, format, scan_width);
  110. surface->Lock(ESurfaceLockMode::Write);
  111. for (int y = bih.mHeight - 1; y >= 0; --y)
  112. {
  113. inStream.read((char *)surface->GetScanLine(y), scan_width);
  114. loaded = loaded && !inStream.fail();
  115. }
  116. surface->UnLock();
  117. }
  118. return loaded? surface : Ref<Surface>(nullptr);
  119. }
  120. bool SaveBMP(RefConst<Surface> inSurface, ostream &inStream)
  121. {
  122. bool stored = true;
  123. // Convert surface if required
  124. const Surface *src = inSurface;
  125. Ref<Surface> tmp_src;
  126. if (inSurface->GetFormat() != ESurfaceFormat::R8G8B8)
  127. {
  128. tmp_src = new SoftwareSurface(inSurface->GetWidth(), inSurface->GetHeight(), ESurfaceFormat::R8G8B8);
  129. BlitSurface(inSurface, tmp_src);
  130. src = tmp_src.GetPtr();
  131. }
  132. // Lock the surface
  133. src->Lock(ESurfaceLockMode::Read);
  134. JPH_ASSERT(src->GetStride() % 4 == 0);
  135. BitmapFileHeader bfh;
  136. BitmapInfoHeader bih;
  137. // Fill in headers
  138. bfh.mTypeB = 'B';
  139. bfh.mTypeM = 'M';
  140. bfh.mSize = sizeof(bfh) + sizeof(bih) + src->GetHeight() * src->GetStride();
  141. bfh.mReserved1 = 0;
  142. bfh.mReserved2 = 0;
  143. bfh.mOffBits = sizeof(bfh) + sizeof(bih);
  144. bih.mSize = sizeof(bih);
  145. bih.mWidth = src->GetWidth();
  146. bih.mHeight = src->GetHeight();
  147. bih.mPlanes = 1;
  148. bih.mBitCount = 24;
  149. bih.mCompression = 0;
  150. bih.mSizeImage = src->GetHeight() * src->GetStride();
  151. bih.mXPelsPerMeter = 300;
  152. bih.mYPelsPerMeter = 300;
  153. bih.mClrUsed = 0;
  154. bih.mClrImportant = 0;
  155. // Write headers
  156. inStream.write((char *)&bfh, sizeof(bfh));
  157. stored = stored && !inStream.fail();
  158. inStream.write((char *)&bih, sizeof(bih));
  159. stored = stored && !inStream.fail();
  160. // Write image data
  161. for (int y = src->GetHeight() - 1; y >= 0; --y)
  162. {
  163. inStream.write((const char *)src->GetScanLine(y), src->GetStride());
  164. stored = stored && !inStream.fail();
  165. }
  166. src->UnLock();
  167. return stored;
  168. }