BMPWriter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Vector.h>
  27. #include <LibGfx/BMPWriter.h>
  28. #include <LibGfx/Bitmap.h>
  29. #include <cstring>
  30. namespace Gfx {
  31. constexpr int bytes_per_pixel = 3;
  32. #define FILE_HEADER_SIZE 14
  33. #define IMAGE_INFORMATION_SIZE 40
  34. #define PIXEL_DATA_OFFSET FILE_HEADER_SIZE + IMAGE_INFORMATION_SIZE
  35. class Streamer {
  36. public:
  37. Streamer(u8* data)
  38. : m_data(data)
  39. {
  40. }
  41. void write_u8(u8 i)
  42. {
  43. *(m_data++) = i;
  44. }
  45. void write_u16(u16 i)
  46. {
  47. *(m_data++) = i & 0xFF;
  48. *(m_data++) = (i >> 8) & 0xFF;
  49. }
  50. void write_u32(u32 i)
  51. {
  52. write_u16(i & 0xFFFF);
  53. write_u16((i >> 16) & 0xFFFF);
  54. }
  55. void write_i32(i32 i)
  56. {
  57. write_u32(static_cast<u32>(i));
  58. }
  59. private:
  60. u8* m_data;
  61. };
  62. static ByteBuffer write_pixel_data(const RefPtr<Bitmap> bitmap, int pixel_row_data_size)
  63. {
  64. int image_size = pixel_row_data_size * bitmap->height();
  65. auto buffer = ByteBuffer::create_uninitialized(image_size);
  66. int current_row = 0;
  67. for (int y = bitmap->physical_height() - 1; y >= 0; --y) {
  68. auto* row = buffer.data() + (pixel_row_data_size * current_row++);
  69. for (int x = 0; x < bitmap->physical_width(); x++) {
  70. auto pixel = bitmap->get_pixel(x, y);
  71. row[x * bytes_per_pixel + 0] = pixel.blue();
  72. row[x * bytes_per_pixel + 1] = pixel.green();
  73. row[x * bytes_per_pixel + 2] = pixel.red();
  74. }
  75. }
  76. return buffer;
  77. }
  78. static ByteBuffer compress_pixel_data(const ByteBuffer& pixel_data, BMPWriter::Compression compression)
  79. {
  80. switch (compression) {
  81. case BMPWriter::Compression::RGB:
  82. return pixel_data;
  83. }
  84. ASSERT_NOT_REACHED();
  85. }
  86. ByteBuffer BMPWriter::dump(const RefPtr<Bitmap> bitmap)
  87. {
  88. int pixel_row_data_size = (bytes_per_pixel * 8 * bitmap->width() + 31) / 32 * 4;
  89. int image_size = pixel_row_data_size * bitmap->height();
  90. auto buffer = ByteBuffer::create_uninitialized(PIXEL_DATA_OFFSET);
  91. auto pixel_data = write_pixel_data(bitmap, pixel_row_data_size);
  92. pixel_data = compress_pixel_data(pixel_data, m_compression);
  93. int file_size = PIXEL_DATA_OFFSET + pixel_data.size();
  94. Streamer streamer(buffer.data());
  95. streamer.write_u8('B');
  96. streamer.write_u8('M');
  97. streamer.write_u32(file_size);
  98. streamer.write_u32(0);
  99. streamer.write_u32(PIXEL_DATA_OFFSET);
  100. streamer.write_u32(IMAGE_INFORMATION_SIZE); // Header size
  101. streamer.write_i32(bitmap->width()); // ImageWidth
  102. streamer.write_i32(bitmap->height()); // ImageHeight
  103. streamer.write_u16(1); // Planes
  104. streamer.write_u16(bytes_per_pixel * 8); // BitsPerPixel
  105. streamer.write_u32((u32)m_compression); // Compression
  106. streamer.write_u32(image_size); // ImageSize
  107. streamer.write_i32(0); // XpixelsPerMeter
  108. streamer.write_i32(0); // YpixelsPerMeter
  109. streamer.write_u32(0); // TotalColors
  110. streamer.write_u32(0); // ImportantColors
  111. buffer.append(pixel_data.data(), pixel_data.size());
  112. return buffer;
  113. }
  114. }