소스 검색

Start working on a new image class.

pull/1/head
Sam Hocevar 4 년 전
부모
커밋
2c61a5e805
4개의 변경된 파일242개의 추가작업 그리고 2개의 파일을 삭제
  1. +16
    -2
      TODO.md
  2. +18
    -0
      include/lol/image
  3. +161
    -0
      include/lol/private/image/image.h
  4. +47
    -0
      include/lol/private/image/pixel.h

+ 16
- 2
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


+ 18
- 0
include/lol/image 파일 보기

@@ -0,0 +1,18 @@
//
// Lol Engine
//
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
//
// 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"


+ 161
- 0
include/lol/private/image/image.h 파일 보기

@@ -0,0 +1,161 @@
//
// Lol Engine
//
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
//
// 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/narray> // lol::array2d
#include <lol/vector> // lol::ivec2
#include <memory> // std::shared_ptr
#include <string> // std::string
#include <unordered_set> // std::unordered_set

namespace lol
{

template<pixel::format T> class image_t;

using image = image_t<pixel::format::rgba_u8>;

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<image_codec> codec)
{
codecs().insert(codec);
}

static std::unordered_set<std::shared_ptr<class image_codec>> codecs()
{
static std::unordered_set<std::shared_ptr<image_codec>> instance;
return instance;
};
};

// The generic image class
template<pixel::format T> class image_t
{
public:
using pixel_type = typename pixel::type<T>::value;

image_t<T>() = default;

image_t<T>(int width, int height)
: m_pixels(width, height)
{}

image_t<T>(ivec2 size)
: m_pixels(size)
{}

// Build from another image
template<pixel::format U>
image_t<T>(image_t<U> 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<U, T>(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<pixel_type> pixels()
{
return m_pixels.span();
}

span2d<pixel_type const> pixels() const
{
return m_pixels.span();
}

private:
array2d<pixel_type> m_pixels;
std::shared_ptr<image_data> m_data;
};

using image = image_t<pixel::format::rgba_u8>;

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


+ 47
- 0
include/lol/private/image/pixel.h 파일 보기

@@ -0,0 +1,47 @@
//
// Lol Engine
//
// Copyright © 2004—2020 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/vector>

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<format T> struct type {};
template<> struct type<format::y_u8> { typedef uint8_t value; };
template<> struct type<format::rgb_u8> { typedef u8vec3 value; };
template<> struct type<format::rgba_u8> { typedef u8vec4 value; };
template<> struct type<format::y_f32> { typedef float value; };
template<> struct type<format::rgb_f32> { typedef vec3 value; };
template<> struct type<format::rgba_f32> { typedef vec4 value; };

// Convert between pixel formats
template<format T, format U>
typename type<U>::value convert(typename type<T>::value const &p)
{
return type<T>::value();
}

} // namespace lol::pixel

불러오는 중...
취소
저장