BMPWriter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibGfx/ImageFormats/BMPWriter.h>
  8. namespace Gfx {
  9. class OutputStreamer {
  10. public:
  11. OutputStreamer(u8* data)
  12. : m_data(data)
  13. {
  14. }
  15. void write_u8(u8 i)
  16. {
  17. *(m_data++) = i;
  18. }
  19. void write_u16(u16 i)
  20. {
  21. *(m_data++) = i & 0xFF;
  22. *(m_data++) = (i >> 8) & 0xFF;
  23. }
  24. void write_u32(u32 i)
  25. {
  26. write_u16(i & 0xFFFF);
  27. write_u16((i >> 16) & 0xFFFF);
  28. }
  29. void write_i32(i32 i)
  30. {
  31. write_u32(static_cast<u32>(i));
  32. }
  33. private:
  34. u8* m_data;
  35. };
  36. static ErrorOr<ByteBuffer> write_pixel_data(Bitmap const& bitmap, int pixel_row_data_size, int bytes_per_pixel, bool include_alpha_channel)
  37. {
  38. int image_size = pixel_row_data_size * bitmap.height();
  39. auto buffer = TRY(ByteBuffer::create_uninitialized(image_size));
  40. int current_row = 0;
  41. for (int y = bitmap.physical_height() - 1; y >= 0; --y) {
  42. auto* row = buffer.data() + (pixel_row_data_size * current_row++);
  43. for (int x = 0; x < bitmap.physical_width(); x++) {
  44. auto pixel = bitmap.get_pixel(x, y);
  45. row[x * bytes_per_pixel + 0] = pixel.blue();
  46. row[x * bytes_per_pixel + 1] = pixel.green();
  47. row[x * bytes_per_pixel + 2] = pixel.red();
  48. if (include_alpha_channel)
  49. row[x * bytes_per_pixel + 3] = pixel.alpha();
  50. }
  51. }
  52. return buffer;
  53. }
  54. ErrorOr<ByteBuffer> BMPWriter::encode(Bitmap const& bitmap, Options options)
  55. {
  56. return BMPWriter().dump(bitmap, options);
  57. }
  58. ByteBuffer BMPWriter::compress_pixel_data(ByteBuffer pixel_data, BMPWriter::Compression compression)
  59. {
  60. switch (compression) {
  61. case BMPWriter::Compression::BI_BITFIELDS:
  62. case BMPWriter::Compression::BI_RGB:
  63. return pixel_data;
  64. }
  65. VERIFY_NOT_REACHED();
  66. }
  67. ErrorOr<ByteBuffer> BMPWriter::dump(Bitmap const& bitmap, Options options)
  68. {
  69. Options::DibHeader dib_header = options.dib_header;
  70. auto icc_data = options.icc_data;
  71. if (icc_data.has_value() && dib_header < Options::DibHeader::V5)
  72. return Error::from_string_literal("can only embed ICC profiles in v5+ bmps");
  73. switch (dib_header) {
  74. case Options::DibHeader::Info:
  75. m_compression = Compression::BI_RGB;
  76. m_bytes_per_pixel = 3;
  77. m_include_alpha_channel = false;
  78. break;
  79. case Options::DibHeader::V3:
  80. case Options::DibHeader::V4:
  81. case Options::DibHeader::V5:
  82. m_compression = Compression::BI_BITFIELDS;
  83. m_bytes_per_pixel = 4;
  84. m_include_alpha_channel = true;
  85. }
  86. const size_t file_header_size = 14;
  87. size_t header_size = file_header_size + (u32)dib_header;
  88. int pixel_row_data_size = (m_bytes_per_pixel * 8 * bitmap.width() + 31) / 32 * 4;
  89. int image_size = pixel_row_data_size * bitmap.height();
  90. auto buffer = TRY(ByteBuffer::create_uninitialized(header_size));
  91. auto pixel_data = TRY(write_pixel_data(bitmap, pixel_row_data_size, m_bytes_per_pixel, m_include_alpha_channel));
  92. pixel_data = compress_pixel_data(move(pixel_data), m_compression);
  93. size_t icc_profile_size = 0;
  94. if (icc_data.has_value())
  95. icc_profile_size = icc_data->size();
  96. size_t pixel_data_offset = header_size + icc_profile_size;
  97. size_t file_size = pixel_data_offset + pixel_data.size();
  98. OutputStreamer streamer(buffer.data());
  99. streamer.write_u8('B');
  100. streamer.write_u8('M');
  101. streamer.write_u32(file_size);
  102. streamer.write_u32(0);
  103. streamer.write_u32(pixel_data_offset);
  104. streamer.write_u32((u32)dib_header); // Header size
  105. streamer.write_i32(bitmap.width()); // ImageWidth
  106. streamer.write_i32(bitmap.height()); // ImageHeight
  107. streamer.write_u16(1); // Planes
  108. streamer.write_u16(m_bytes_per_pixel * 8); // BitsPerPixel
  109. streamer.write_u32((u32)m_compression); // Compression
  110. streamer.write_u32(image_size); // ImageSize
  111. streamer.write_i32(0); // XpixelsPerMeter
  112. streamer.write_i32(0); // YpixelsPerMeter
  113. streamer.write_u32(0); // TotalColors
  114. streamer.write_u32(0); // ImportantColors
  115. if (dib_header >= Options::DibHeader::V3) {
  116. streamer.write_u32(0x00ff0000); // Red bitmask
  117. streamer.write_u32(0x0000ff00); // Green bitmask
  118. streamer.write_u32(0x000000ff); // Blue bitmask
  119. streamer.write_u32(0xff000000); // Alpha bitmask
  120. }
  121. if (dib_header >= Options::DibHeader::V4) {
  122. if (icc_data.has_value())
  123. streamer.write_u32(0x4D424544); // Colorspace EMBEDDED
  124. else
  125. streamer.write_u32(0); // Colorspace CALIBRATED_RGB
  126. for (int i = 0; i < 12; i++) {
  127. streamer.write_u32(0); // Endpoints and gamma
  128. }
  129. }
  130. if (dib_header >= Options::DibHeader::V5) {
  131. streamer.write_u32(4); // Rendering intent IMAGES / Perceptual.
  132. if (icc_data.has_value()) {
  133. streamer.write_u32((u32)dib_header); // Profile data (relative to file_header_size)
  134. streamer.write_u32(icc_data->size()); // Profile size
  135. } else {
  136. streamer.write_u32(0); // Profile data
  137. streamer.write_u32(0); // Profile size
  138. }
  139. streamer.write_u32(0); // Reserved
  140. }
  141. if (icc_data.has_value())
  142. TRY(buffer.try_append(icc_data.value()));
  143. TRY(buffer.try_append(pixel_data));
  144. return buffer;
  145. }
  146. }