2020-10-18 14:39:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-18 14:39:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGfx/Bitmap.h>
|
2023-03-21 14:58:06 -04:00
|
|
|
#include <LibGfx/ImageFormats/BMPWriter.h>
|
2020-10-18 14:39:22 +00:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2021-05-28 07:01:52 +02:00
|
|
|
class OutputStreamer {
|
2020-10-18 14:39:22 +00:00
|
|
|
public:
|
2021-05-28 07:01:52 +02:00
|
|
|
OutputStreamer(u8* data)
|
2020-10-18 14:39:22 +00:00
|
|
|
: m_data(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u8(u8 i)
|
|
|
|
{
|
|
|
|
*(m_data++) = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u16(u16 i)
|
|
|
|
{
|
|
|
|
*(m_data++) = i & 0xFF;
|
|
|
|
*(m_data++) = (i >> 8) & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u32(u32 i)
|
|
|
|
{
|
|
|
|
write_u16(i & 0xFFFF);
|
|
|
|
write_u16((i >> 16) & 0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_i32(i32 i)
|
|
|
|
{
|
|
|
|
write_u32(static_cast<u32>(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
u8* m_data;
|
|
|
|
};
|
|
|
|
|
2023-03-12 13:22:19 -04:00
|
|
|
static ErrorOr<ByteBuffer> write_pixel_data(Bitmap const& bitmap, int pixel_row_data_size, int bytes_per_pixel, bool include_alpha_channel)
|
2020-10-18 14:39:22 +00:00
|
|
|
{
|
2023-03-12 12:45:30 -04:00
|
|
|
int image_size = pixel_row_data_size * bitmap.height();
|
2023-03-12 13:22:19 -04:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(image_size));
|
2020-10-18 14:39:22 +00:00
|
|
|
|
|
|
|
int current_row = 0;
|
2024-06-05 08:17:28 +02:00
|
|
|
for (int y = bitmap.height() - 1; y >= 0; --y) {
|
2020-10-18 14:39:22 +00:00
|
|
|
auto* row = buffer.data() + (pixel_row_data_size * current_row++);
|
2024-06-05 08:17:28 +02:00
|
|
|
for (int x = 0; x < bitmap.width(); x++) {
|
2023-03-12 12:45:30 -04:00
|
|
|
auto pixel = bitmap.get_pixel(x, y);
|
2020-10-18 14:39:22 +00:00
|
|
|
row[x * bytes_per_pixel + 0] = pixel.blue();
|
|
|
|
row[x * bytes_per_pixel + 1] = pixel.green();
|
|
|
|
row[x * bytes_per_pixel + 2] = pixel.red();
|
2021-07-03 23:50:21 +02:00
|
|
|
if (include_alpha_channel)
|
|
|
|
row[x * bytes_per_pixel + 3] = pixel.alpha();
|
2020-10-18 14:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2023-03-12 12:09:29 -04:00
|
|
|
ErrorOr<ByteBuffer> BMPWriter::encode(Bitmap const& bitmap, Options options)
|
|
|
|
{
|
|
|
|
return BMPWriter().dump(bitmap, options);
|
|
|
|
}
|
|
|
|
|
2023-03-12 12:15:08 -04:00
|
|
|
ByteBuffer BMPWriter::compress_pixel_data(ByteBuffer pixel_data, BMPWriter::Compression compression)
|
2020-10-18 14:39:22 +00:00
|
|
|
{
|
|
|
|
switch (compression) {
|
2021-07-03 23:50:21 +02:00
|
|
|
case BMPWriter::Compression::BI_BITFIELDS:
|
|
|
|
case BMPWriter::Compression::BI_RGB:
|
2020-10-18 14:39:22 +00:00
|
|
|
return pixel_data;
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-10-18 14:39:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-12 13:22:19 -04:00
|
|
|
ErrorOr<ByteBuffer> BMPWriter::dump(Bitmap const& bitmap, Options options)
|
2020-10-18 14:39:22 +00:00
|
|
|
{
|
2023-03-12 10:24:30 -04:00
|
|
|
Options::DibHeader dib_header = options.dib_header;
|
2021-07-03 23:50:21 +02:00
|
|
|
|
2023-03-15 12:05:36 +01:00
|
|
|
auto icc_data = options.icc_data;
|
|
|
|
if (icc_data.has_value() && dib_header < Options::DibHeader::V5)
|
|
|
|
return Error::from_string_literal("can only embed ICC profiles in v5+ bmps");
|
|
|
|
|
2021-07-03 23:50:21 +02:00
|
|
|
switch (dib_header) {
|
2023-03-12 10:24:30 -04:00
|
|
|
case Options::DibHeader::Info:
|
2021-07-03 23:50:21 +02:00
|
|
|
m_compression = Compression::BI_RGB;
|
|
|
|
m_bytes_per_pixel = 3;
|
|
|
|
m_include_alpha_channel = false;
|
|
|
|
break;
|
2023-03-12 10:24:30 -04:00
|
|
|
case Options::DibHeader::V3:
|
|
|
|
case Options::DibHeader::V4:
|
2023-03-15 11:54:24 +01:00
|
|
|
case Options::DibHeader::V5:
|
2021-07-03 23:50:21 +02:00
|
|
|
m_compression = Compression::BI_BITFIELDS;
|
|
|
|
m_bytes_per_pixel = 4;
|
|
|
|
m_include_alpha_channel = true;
|
|
|
|
}
|
|
|
|
|
2024-04-18 15:32:56 -04:00
|
|
|
size_t const file_header_size = 14;
|
2023-03-15 12:05:36 +01:00
|
|
|
size_t header_size = file_header_size + (u32)dib_header;
|
2021-07-03 23:50:21 +02:00
|
|
|
|
2023-03-12 12:45:30 -04:00
|
|
|
int pixel_row_data_size = (m_bytes_per_pixel * 8 * bitmap.width() + 31) / 32 * 4;
|
|
|
|
int image_size = pixel_row_data_size * bitmap.height();
|
2023-03-15 12:05:36 +01:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(header_size));
|
2020-10-18 14:39:22 +00:00
|
|
|
|
2023-03-12 13:22:19 -04:00
|
|
|
auto pixel_data = TRY(write_pixel_data(bitmap, pixel_row_data_size, m_bytes_per_pixel, m_include_alpha_channel));
|
2023-03-12 12:15:08 -04:00
|
|
|
pixel_data = compress_pixel_data(move(pixel_data), m_compression);
|
2020-10-18 14:39:22 +00:00
|
|
|
|
2023-03-15 12:05:36 +01:00
|
|
|
size_t icc_profile_size = 0;
|
|
|
|
if (icc_data.has_value())
|
|
|
|
icc_profile_size = icc_data->size();
|
|
|
|
|
|
|
|
size_t pixel_data_offset = header_size + icc_profile_size;
|
2021-07-03 23:50:21 +02:00
|
|
|
size_t file_size = pixel_data_offset + pixel_data.size();
|
2021-05-28 07:01:52 +02:00
|
|
|
OutputStreamer streamer(buffer.data());
|
2020-10-18 14:39:22 +00:00
|
|
|
streamer.write_u8('B');
|
|
|
|
streamer.write_u8('M');
|
|
|
|
streamer.write_u32(file_size);
|
|
|
|
streamer.write_u32(0);
|
2021-07-03 23:50:21 +02:00
|
|
|
streamer.write_u32(pixel_data_offset);
|
|
|
|
|
|
|
|
streamer.write_u32((u32)dib_header); // Header size
|
2023-03-12 12:45:30 -04:00
|
|
|
streamer.write_i32(bitmap.width()); // ImageWidth
|
|
|
|
streamer.write_i32(bitmap.height()); // ImageHeight
|
2021-07-03 23:50:21 +02:00
|
|
|
streamer.write_u16(1); // Planes
|
|
|
|
streamer.write_u16(m_bytes_per_pixel * 8); // BitsPerPixel
|
|
|
|
streamer.write_u32((u32)m_compression); // Compression
|
|
|
|
streamer.write_u32(image_size); // ImageSize
|
|
|
|
streamer.write_i32(0); // XpixelsPerMeter
|
|
|
|
streamer.write_i32(0); // YpixelsPerMeter
|
|
|
|
streamer.write_u32(0); // TotalColors
|
|
|
|
streamer.write_u32(0); // ImportantColors
|
|
|
|
|
2023-03-15 11:54:24 +01:00
|
|
|
if (dib_header >= Options::DibHeader::V3) {
|
2021-07-03 23:50:21 +02:00
|
|
|
streamer.write_u32(0x00ff0000); // Red bitmask
|
|
|
|
streamer.write_u32(0x0000ff00); // Green bitmask
|
|
|
|
streamer.write_u32(0x000000ff); // Blue bitmask
|
|
|
|
streamer.write_u32(0xff000000); // Alpha bitmask
|
|
|
|
}
|
|
|
|
|
2023-03-15 11:54:24 +01:00
|
|
|
if (dib_header >= Options::DibHeader::V4) {
|
2023-03-15 12:05:36 +01:00
|
|
|
if (icc_data.has_value())
|
|
|
|
streamer.write_u32(0x4D424544); // Colorspace EMBEDDED
|
|
|
|
else
|
|
|
|
streamer.write_u32(0); // Colorspace CALIBRATED_RGB
|
2021-07-03 23:50:21 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 12; i++) {
|
2023-03-15 13:36:58 +01:00
|
|
|
streamer.write_u32(0); // Endpoints and gamma
|
2021-07-03 23:50:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-18 14:39:22 +00:00
|
|
|
|
2023-03-15 11:54:24 +01:00
|
|
|
if (dib_header >= Options::DibHeader::V5) {
|
|
|
|
streamer.write_u32(4); // Rendering intent IMAGES / Perceptual.
|
2023-03-15 12:05:36 +01:00
|
|
|
|
|
|
|
if (icc_data.has_value()) {
|
|
|
|
streamer.write_u32((u32)dib_header); // Profile data (relative to file_header_size)
|
|
|
|
streamer.write_u32(icc_data->size()); // Profile size
|
|
|
|
} else {
|
|
|
|
streamer.write_u32(0); // Profile data
|
|
|
|
streamer.write_u32(0); // Profile size
|
|
|
|
}
|
2023-03-15 11:54:24 +01:00
|
|
|
streamer.write_u32(0); // Reserved
|
|
|
|
}
|
|
|
|
|
2023-03-15 12:05:36 +01:00
|
|
|
if (icc_data.has_value())
|
|
|
|
TRY(buffer.try_append(icc_data.value()));
|
|
|
|
|
2023-03-15 12:28:12 +01:00
|
|
|
TRY(buffer.try_append(pixel_data));
|
2020-10-18 14:39:22 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|