diff options
author | Stephan Unverwerth <s.unverwerth@serenityos.org> | 2021-12-22 18:07:01 +0100 |
---|---|---|
committer | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-12-24 05:10:28 -0800 |
commit | b41ad28654fe82761a354c0746285c413abaf32a (patch) | |
tree | 3bd1165be021371abfa022397140251adeb84f9d | |
parent | 2a72d14336ad4d167b6554f98b8e4cb25ec46664 (diff) | |
download | serenity-b41ad28654fe82761a354c0746285c413abaf32a.zip |
LibSoftGPU: Add methods to read and write image data
This adds two methods, write_texels and read_texels, to the Image class.
Conversion between image formats happens automatically. The layout of
the client image data is passed in via ImageDataLayout struct.
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Image.cpp | 38 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Image.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibSoftGPU/ImageDataLayout.h | 20 |
3 files changed, 62 insertions, 0 deletions
diff --git a/Userland/Libraries/LibSoftGPU/Image.cpp b/Userland/Libraries/LibSoftGPU/Image.cpp index 147e7d9a53..451da3734b 100644 --- a/Userland/Libraries/LibSoftGPU/Image.cpp +++ b/Userland/Libraries/LibSoftGPU/Image.cpp @@ -41,4 +41,42 @@ Image::Image(ImageFormat format, unsigned width, unsigned height, unsigned depth m_data.resize(m_mipchain_size * m_num_layers); } +void Image::write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, ImageDataLayout const& layout) +{ + VERIFY(layer < num_layers()); + VERIFY(level < num_levels()); + VERIFY(offset.x() + size.x() <= level_width(level)); + VERIFY(offset.y() + size.y() <= level_height(level)); + VERIFY(offset.z() + size.z() <= level_depth(level)); + + for (unsigned z = 0; z < size.z(); ++z) { + for (unsigned y = 0; y < size.y(); ++y) { + for (unsigned x = 0; x < size.x(); ++x) { + auto ptr = reinterpret_cast<u8 const*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x; + auto color = unpack_color(ptr, layout.format); + set_texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z, color); + } + } + } +} + +void Image::read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, ImageDataLayout const& layout) const +{ + VERIFY(layer < num_layers()); + VERIFY(level < num_levels()); + VERIFY(offset.x() + size.x() <= level_width(level)); + VERIFY(offset.y() + size.y() <= level_height(level)); + VERIFY(offset.z() + size.z() <= level_depth(level)); + + for (unsigned z = 0; z < size.z(); ++z) { + for (unsigned y = 0; y < size.y(); ++y) { + for (unsigned x = 0; x < size.x(); ++x) { + auto color = texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z); + auto ptr = reinterpret_cast<u8*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x; + pack_color(color, ptr, layout.format); + } + } + } +} + } diff --git a/Userland/Libraries/LibSoftGPU/Image.h b/Userland/Libraries/LibSoftGPU/Image.h index f46d3f601c..449e650e89 100644 --- a/Userland/Libraries/LibSoftGPU/Image.h +++ b/Userland/Libraries/LibSoftGPU/Image.h @@ -10,6 +10,7 @@ #include <AK/Vector.h> #include <LibGfx/Vector3.h> #include <LibGfx/Vector4.h> +#include <LibSoftGPU/ImageDataLayout.h> #include <LibSoftGPU/ImageFormat.h> namespace SoftGPU { @@ -38,6 +39,9 @@ public: pack_color(color, texel_pointer(layer, level, x, y, z), m_format); } + void write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, ImageDataLayout const& layout); + void read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, ImageDataLayout const& layout) const; + private: void const* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const { diff --git a/Userland/Libraries/LibSoftGPU/ImageDataLayout.h b/Userland/Libraries/LibSoftGPU/ImageDataLayout.h new file mode 100644 index 0000000000..86b069d43b --- /dev/null +++ b/Userland/Libraries/LibSoftGPU/ImageDataLayout.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibSoftGPU/ImageFormat.h> + +namespace SoftGPU { + +struct ImageDataLayout final { + ImageFormat format; + size_t column_stride; + size_t row_stride; + size_t depth_stride; +}; + +} |