image.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #pragma once
  2. #include <algorithm>
  3. #include <nall/file-map.hpp>
  4. #include <nall/interpolation.hpp>
  5. #include <nall/stdint.hpp>
  6. #include <nall/decode/bmp.hpp>
  7. #include <nall/decode/png.hpp>
  8. namespace nall {
  9. struct image {
  10. enum class blend : uint {
  11. add,
  12. sourceAlpha, //color = sourceColor * sourceAlpha + targetColor * (1 - sourceAlpha)
  13. sourceColor, //color = sourceColor
  14. targetAlpha, //color = targetColor * targetAlpha + sourceColor * (1 - targetAlpha)
  15. targetColor, //color = targetColor
  16. };
  17. struct channel {
  18. channel(uint64_t mask, uint depth, uint shift) : _mask(mask), _depth(depth), _shift(shift) {
  19. }
  20. auto operator==(const channel& source) const -> bool {
  21. return _mask == source._mask && _depth == source._depth && _shift == source._shift;
  22. }
  23. auto operator!=(const channel& source) const -> bool {
  24. return !operator==(source);
  25. }
  26. auto mask() const { return _mask; }
  27. auto depth() const { return _depth; }
  28. auto shift() const { return _shift; }
  29. private:
  30. uint64_t _mask;
  31. uint _depth;
  32. uint _shift;
  33. };
  34. //core.hpp
  35. image(const image& source);
  36. image(image&& source);
  37. image(bool endian, uint depth, uint64_t alphaMask, uint64_t redMask, uint64_t greenMask, uint64_t blueMask);
  38. image(const string& filename);
  39. image(const void* data, uint size);
  40. image(const vector<uint8_t>& buffer);
  41. template<uint Size> image(const uint8_t (&Name)[Size]);
  42. image();
  43. ~image();
  44. auto operator=(const image& source) -> image&;
  45. auto operator=(image&& source) -> image&;
  46. explicit operator bool() const;
  47. auto operator==(const image& source) const -> bool;
  48. auto operator!=(const image& source) const -> bool;
  49. auto read(const uint8_t* data) const -> uint64_t;
  50. auto write(uint8_t* data, uint64_t value) const -> void;
  51. auto free() -> void;
  52. auto load(const string& filename) -> bool;
  53. auto copy(const void* data, uint pitch, uint width, uint height) -> void;
  54. auto allocate(uint width, uint height) -> void;
  55. //fill.hpp
  56. auto fill(uint64_t color = 0) -> void;
  57. auto gradient(uint64_t a, uint64_t b, uint64_t c, uint64_t d) -> void;
  58. auto gradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY, function<double (double, double)> callback) -> void;
  59. auto crossGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  60. auto diamondGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  61. auto horizontalGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  62. auto radialGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  63. auto sphericalGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  64. auto squareGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  65. auto verticalGradient(uint64_t a, uint64_t b, int radiusX, int radiusY, int centerX, int centerY) -> void;
  66. //scale.hpp
  67. auto scale(uint width, uint height, bool linear = true) -> void;
  68. //blend.hpp
  69. auto impose(blend mode, uint targetX, uint targetY, image source, uint x, uint y, uint width, uint height) -> void;
  70. //utility.hpp
  71. auto shrink(uint64_t transparentColor = 0) -> void;
  72. auto crop(uint x, uint y, uint width, uint height) -> bool;
  73. auto alphaBlend(uint64_t alphaColor) -> void;
  74. auto alphaMultiply() -> void;
  75. auto transform(const image& source = {}) -> void;
  76. auto transform(bool endian, uint depth, uint64_t alphaMask, uint64_t redMask, uint64_t greenMask, uint64_t blueMask) -> void;
  77. //static.hpp
  78. static auto bitDepth(uint64_t color) -> uint;
  79. static auto bitShift(uint64_t color) -> uint;
  80. static auto normalize(uint64_t color, uint sourceDepth, uint targetDepth) -> uint64_t;
  81. //access
  82. auto data() { return _data; }
  83. auto data() const { return _data; }
  84. auto width() const { return _width; }
  85. auto height() const { return _height; }
  86. auto endian() const { return _endian; }
  87. auto depth() const { return _depth; }
  88. auto stride() const { return (_depth + 7) >> 3; }
  89. auto pitch() const { return _width * stride(); }
  90. auto size() const { return _height * pitch(); }
  91. auto alpha() const { return _alpha; }
  92. auto red() const { return _red; }
  93. auto green() const { return _green; }
  94. auto blue() const { return _blue; }
  95. private:
  96. //core.hpp
  97. auto allocate(uint width, uint height, uint stride) -> uint8_t*;
  98. //scale.hpp
  99. auto scaleLinearWidth(uint width) -> void;
  100. auto scaleLinearHeight(uint height) -> void;
  101. auto scaleLinear(uint width, uint height) -> void;
  102. auto scaleNearest(uint width, uint height) -> void;
  103. //load.hpp
  104. auto loadBMP(const string& filename) -> bool;
  105. auto loadBMP(const uint8_t* data, uint size) -> bool;
  106. auto loadPNG(const string& filename) -> bool;
  107. auto loadPNG(const uint8_t* data, uint size) -> bool;
  108. //interpolation.hpp
  109. auto isplit(uint64_t* component, uint64_t color) -> void;
  110. auto imerge(const uint64_t* component) -> uint64_t;
  111. auto interpolate1f(uint64_t a, uint64_t b, double x) -> uint64_t;
  112. auto interpolate1f(uint64_t a, uint64_t b, uint64_t c, uint64_t d, double x, double y) -> uint64_t;
  113. auto interpolate1i(int64_t a, int64_t b, uint32_t x) -> uint64_t;
  114. auto interpolate1i(int64_t a, int64_t b, int64_t c, int64_t d, uint32_t x, uint32_t y) -> uint64_t;
  115. auto interpolate4f(uint64_t a, uint64_t b, double x) -> uint64_t;
  116. auto interpolate4f(uint64_t a, uint64_t b, uint64_t c, uint64_t d, double x, double y) -> uint64_t;
  117. auto interpolate4i(uint64_t a, uint64_t b, uint32_t x) -> uint64_t;
  118. auto interpolate4i(uint64_t a, uint64_t b, uint64_t c, uint64_t d, uint32_t x, uint32_t y) -> uint64_t;
  119. uint8_t* _data = nullptr;
  120. uint _width = 0;
  121. uint _height = 0;
  122. bool _endian = 0; //0 = lsb, 1 = msb
  123. uint _depth = 32;
  124. channel _alpha{255u << 24, 8, 24};
  125. channel _red {255u << 16, 8, 16};
  126. channel _green{255u << 8, 8, 8};
  127. channel _blue {255u << 0, 8, 0};
  128. };
  129. }
  130. #include <nall/image/static.hpp>
  131. #include <nall/image/core.hpp>
  132. #include <nall/image/load.hpp>
  133. #include <nall/image/interpolation.hpp>
  134. #include <nall/image/fill.hpp>
  135. #include <nall/image/scale.hpp>
  136. #include <nall/image/blend.hpp>
  137. #include <nall/image/utility.hpp>