2
0

LoadBMP.cpp 4.6 KB

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