diff --git a/TODO.md b/TODO.md index ae9e6ef8..ece41dab 100644 --- a/TODO.md +++ b/TODO.md @@ -10,8 +10,6 @@ src/lol/algorithm/aabb_tree.h src/lol/algorithm/portal.h src/lol/base/avl_tree.h - src/lol/image/pixel.h (but is it useful?) - src/lol/math/arraynd.h (but do we really want to keep it?) src/lol/math/geometry.h src/lol/math/interp.h (but what is it?) @@ -41,3 +39,19 @@ src/lol/base/enum.h (can’t see the point) +## Interesting libraries: + + - magic enum https://github.com/Neargye/magic_enum + - event dispatcher https://github.com/wqking/eventpp + - random https://github.com/effolkronium/random + - neither (handle exceptions with the type system) https://github.com/LoopPerfect/neither + - lock-free queue https://github.com/rigtorp/SPSCQueue + - fixed point math https://github.com/MikeLankamp/fpm + +## Other libraries + + - coroutines (uses macros) https://github.com/jamboree/co2 + - SIMD https://awesomeopensource.com/project/ospray/tsimd + - deep neural networks https://github.com/yixuan/MiniDNN + - emoji https://github.com/99xt/emojicpp + diff --git a/include/lol/image b/include/lol/image new file mode 100644 index 00000000..f72ad3d3 --- /dev/null +++ b/include/lol/image @@ -0,0 +1,18 @@ +// +// Lol Engine +// +// Copyright © 2010—2020 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +#include "private/push_macros.h" +#include "private/image/image.h" +#include "private/pop_macros.h" + diff --git a/include/lol/private/image/image.h b/include/lol/private/image/image.h new file mode 100644 index 00000000..88b25a6f --- /dev/null +++ b/include/lol/private/image/image.h @@ -0,0 +1,161 @@ +// +// Lol Engine +// +// Copyright © 2010—2020 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The image class +// ——————————————— +// + +#include "pixel.h" + +#include // lol::array2d +#include // lol::ivec2 +#include // std::shared_ptr +#include // std::string +#include // std::unordered_set + +namespace lol +{ + +template class image_t; + +using image = image_t; + +class image_data +{ + virtual ~image_data() {} +}; + +// Codec class for loading and saving images +class image_codec +{ +public: + virtual bool test(std::string const &path) = 0; + virtual bool load(std::string const &path, image &im) = 0; + virtual bool save(std::string const &path, image &im) = 0; + + static void add(std::shared_ptr codec) + { + codecs().insert(codec); + } + + static std::unordered_set> codecs() + { + static std::unordered_set> instance; + return instance; + }; +}; + +// The generic image class +template class image_t +{ +public: + using pixel_type = typename pixel::type::value; + + image_t() = default; + + image_t(int width, int height) + : m_pixels(width, height) + {} + + image_t(ivec2 size) + : m_pixels(size) + {} + + // Build from another image + template + image_t(image_t const &other) + : m_pixels(other.size) + { + auto src = other.pixels(); + auto dst = pixels(); + + for (size_t i = 0; i < src.size(); ++i) + dst[i] = pixel::convert(src[i]); + } + + // Load and save image + bool load(std::string const &path) + { + for (auto &codec : image_codec::codecs()) + if (codec->load(path, *this)) + return true; + return false; + } + + bool save(std::string const &path) + { + for (auto &codec : image_codec::codecs()) + if (codec->save(path, *this)) + return true; + return false; + } + + // Pixel array properties + ivec2 size() const { return m_pixels.sizes(); } + size_t bytes() const { return m_pixels.bytes(); } + + // Direct access to pixels + span2d pixels() + { + return m_pixels.span(); + } + + span2d pixels() const + { + return m_pixels.span(); + } + +private: + array2d m_pixels; + std::shared_ptr m_data; +}; + +using image = image_t; + +enum class WrapMode : uint8_t +{ + Clamp, + Repeat, +}; + +enum class ScanMode : uint8_t +{ + Raster, + Serpentine, +}; + +enum class ResampleAlgorithm : uint8_t +{ + Bicubic, + Bresenham, +}; + +enum class EdiffAlgorithm : uint8_t +{ + FloydSteinberg, + JaJuNi, + Atkinson, + Fan, + ShiauFan, + ShiauFan2, + Stucki, + Burkes, + Sierra, + Sierra2, + Lite, +}; + +} // namespace lol + diff --git a/include/lol/private/image/pixel.h b/include/lol/private/image/pixel.h new file mode 100644 index 00000000..4b758950 --- /dev/null +++ b/include/lol/private/image/pixel.h @@ -0,0 +1,47 @@ +// +// Lol Engine +// +// Copyright © 2004—2020 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The Pixel-related classes +// ————————————————————————— +// + +#include + +namespace lol::pixel +{ + +// The pixel formats we know about +enum class format : uint8_t +{ + y_u8, rgb_u8, rgba_u8, y_f32, rgb_f32, rgba_f32, +}; + +// Associated storage types for each pixel format +template struct type {}; +template<> struct type { typedef uint8_t value; }; +template<> struct type { typedef u8vec3 value; }; +template<> struct type { typedef u8vec4 value; }; +template<> struct type { typedef float value; }; +template<> struct type { typedef vec3 value; }; +template<> struct type { typedef vec4 value; }; + +// Convert between pixel formats +template +typename type::value convert(typename type::value const &p) +{ + return type::value(); +} + +} // namespace lol::pixel