You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

129 lines
3.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. //
  11. // The Image class
  12. // ---------------
  13. //
  14. #if !defined __LOL_IMAGE_IMAGE_H__
  15. #define __LOL_IMAGE_IMAGE_H__
  16. #include <lol/math/vector.h>
  17. namespace lol
  18. {
  19. template <PixelFormat T> struct PixelType { typedef void type; };
  20. template<> struct PixelType<PixelFormat::Y_8> { typedef uint8_t type; };
  21. template<> struct PixelType<PixelFormat::RGB_8> { typedef u8vec3 type; };
  22. template<> struct PixelType<PixelFormat::RGBA_8> { typedef u8vec4 type; };
  23. template<> struct PixelType<PixelFormat::Y_F32> { typedef float type; };
  24. template<> struct PixelType<PixelFormat::RGB_F32> { typedef vec3 type; };
  25. template<> struct PixelType<PixelFormat::RGBA_F32> { typedef vec4 type; };
  26. enum class WrapMode : uint8_t
  27. {
  28. Clamp,
  29. Repeat,
  30. };
  31. enum class ScanMode : uint8_t
  32. {
  33. Raster,
  34. Serpentine,
  35. };
  36. enum class EdiffAlgorithm : uint8_t
  37. {
  38. FloydSteinberg,
  39. JaJuNi,
  40. Atkinson,
  41. Fan,
  42. ShiauFan,
  43. ShiauFan2,
  44. Stucki,
  45. Burkes,
  46. Sierra,
  47. Sierra2,
  48. Lite,
  49. };
  50. class Image
  51. {
  52. public:
  53. Image();
  54. Image(ivec2 size);
  55. /* XXX: use of this ctor should be discouraged, as it will not
  56. * return information about a possible error. */
  57. Image(char const *path);
  58. /* Rule of three */
  59. Image(Image const &other);
  60. Image & operator =(Image other);
  61. ~Image();
  62. bool Load(char const *path);
  63. bool Save(char const *path);
  64. /* Low level access */
  65. ivec2 GetSize() const;
  66. void SetSize(ivec2);
  67. PixelFormat GetFormat() const;
  68. void SetFormat(PixelFormat fmt);
  69. WrapMode GetWrapX() const;
  70. WrapMode GetWrapY() const;
  71. void SetWrap(WrapMode wrap_x, WrapMode wrap_y);
  72. template<PixelFormat T> typename PixelType<T>::type *Lock();
  73. void *Lock();
  74. void Unlock(void const *pixels);
  75. bool RetrieveTiles(Array<ivec2, ivec2>& tiles) const;
  76. /* Image processing kernels */
  77. static Array2D<float> BayerKernel(ivec2 size);
  78. static Array2D<float> HalftoneKernel(ivec2 size);
  79. static Array2D<float> EdiffKernel(EdiffAlgorithm algorithm);
  80. static Array2D<float> NormalizeKernel(Array2D<float> const &kernel);
  81. static Array2D<float> GaussianKernel(vec2 radius,
  82. float angle = 0.f,
  83. vec2 delta = vec2(0.f, 0.f));
  84. /* Rendering */
  85. bool Stock(char const *desc);
  86. bool RenderRandom(ivec2 size);
  87. /* Image processing */
  88. Image AutoContrast() const;
  89. Image Convolution(Array2D<float> const &kernel);
  90. Image Crop(ibox2 box) const;
  91. Image Median(ivec2 radii) const;
  92. Image YUVToRGB() const;
  93. Image RGBToYUV() const;
  94. Image DitherRandom() const;
  95. Image DitherEdiff(Array2D<float> const &kernel,
  96. ScanMode scan = ScanMode::Raster) const;
  97. Image DitherOstromoukhov(ScanMode scan = ScanMode::Raster) const;
  98. Image DitherOrdered(Array2D<float> const &kernel) const;
  99. Image DitherHalftone(float radius, float angle) const;
  100. private:
  101. class ImageData *m_data;
  102. };
  103. } /* namespace lol */
  104. #endif // __LOL_IMAGE_IMAGE_H__