mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 17:37:34 +00:00
LibGfx: Add a QOI image format encoder
Previously only a QOI image decoder was available on the system. This commit adds an encoder as well. For now it only handles images with 4 channels (RGBA).
This commit is contained in:
parent
2f8afcccfd
commit
44a5558525
3 changed files with 252 additions and 0 deletions
49
Userland/Libraries/LibGfx/QOIWriter.h
Normal file
49
Userland/Libraries/LibGfx/QOIWriter.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Olivier De Cannière <olivier.decanniere96@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
static constexpr Array<u8, 4> qoi_magic_bytes = { 'q', 'o', 'i', 'f' };
|
||||
static constexpr Array<u8, 8> qoi_end_marker = { 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||
|
||||
enum class Colorspace {
|
||||
sRGB,
|
||||
Linear,
|
||||
};
|
||||
|
||||
enum class Channels {
|
||||
RGB,
|
||||
RGBA,
|
||||
};
|
||||
|
||||
class QOIWriter {
|
||||
public:
|
||||
static ByteBuffer encode(Gfx::Bitmap const&);
|
||||
|
||||
private:
|
||||
QOIWriter() = default;
|
||||
|
||||
Vector<u8> m_data;
|
||||
void add_header(u32 width, u32 height, Channels, Colorspace);
|
||||
void add_rgb_chunk(u8, u8, u8);
|
||||
void add_rgba_chunk(u8, u8, u8, u8);
|
||||
void add_index_chunk(u32 index);
|
||||
void add_diff_chunk(i8 red_difference, i8 green_difference, i8 blue_difference);
|
||||
void add_luma_chunk(i8 relative_red_difference, i8 green_difference, i8 relative_blue_difference);
|
||||
void add_run_chunk(u32 run_length);
|
||||
void add_end_marker();
|
||||
|
||||
Array<Color, 64> running_array;
|
||||
static u32 pixel_hash_function(Color pixel);
|
||||
void insert_into_running_array(Color pixel);
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue