filterAPI.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #define DFPSR_INTERNAL_ACCESS
  24. #include <cassert>
  25. #include "imageAPI.h"
  26. #include "filterAPI.h"
  27. #include "../image/draw.h"
  28. #include "../image/PackOrder.h"
  29. #include "../image/internal/imageTemplate.h"
  30. #include "../image/internal/imageInternal.h"
  31. using namespace dsr;
  32. // -------------------------------- Image generation and filtering --------------------------------
  33. static void mapRgbaU8(ImageRgbaU8Impl& target, const ImageGenRgbaU8& lambda, int startX, int startY) {
  34. const int targetWidth = target.width;
  35. const int targetHeight = target.height;
  36. const int targetStride = target.stride;
  37. SafePointer<Color4xU8> targetRow = imageInternal::getSafeData<Color4xU8>(target);
  38. for (int y = startY; y < targetHeight + startY; y++) {
  39. SafePointer<Color4xU8> targetPixel = targetRow;
  40. for (int x = startX; x < targetWidth + startX; x++) {
  41. *targetPixel = target.packRgba(lambda(x, y).saturate());
  42. targetPixel += 1;
  43. }
  44. targetRow.increaseBytes(targetStride);
  45. }
  46. }
  47. void dsr::filter_mapRgbaU8(ImageRgbaU8 target, const ImageGenRgbaU8& lambda, int startX, int startY) {
  48. if (target.get() != nullptr) {
  49. mapRgbaU8(*target, lambda, startX, startY);
  50. }
  51. }
  52. OrderedImageRgbaU8 dsr::filter_generateRgbaU8(int width, int height, const ImageGenRgbaU8& lambda, int startX, int startY) {
  53. OrderedImageRgbaU8 result = image_create_RgbaU8(width, height);
  54. filter_mapRgbaU8(result, lambda, startX, startY);
  55. return result;
  56. }
  57. template <typename IMAGE_TYPE, typename PIXEL_TYPE, int MIN_VALUE, int MAX_VALUE>
  58. static void mapMonochrome(IMAGE_TYPE& target, const ImageGenI32& lambda, int startX, int startY) {
  59. const int targetWidth = target.width;
  60. const int targetHeight = target.height;
  61. const int targetStride = target.stride;
  62. SafePointer<PIXEL_TYPE> targetRow = imageInternal::getSafeData<PIXEL_TYPE>(target);
  63. for (int y = startY; y < targetHeight + startY; y++) {
  64. SafePointer<PIXEL_TYPE> targetPixel = targetRow;
  65. for (int x = startX; x < targetWidth + startX; x++) {
  66. int output = lambda(x, y);
  67. if (output < MIN_VALUE) { output = MIN_VALUE; }
  68. if (output > MAX_VALUE) { output = MAX_VALUE; }
  69. *targetPixel = output;
  70. targetPixel += 1;
  71. }
  72. targetRow.increaseBytes(targetStride);
  73. }
  74. }
  75. void dsr::filter_mapU8(ImageU8 target, const ImageGenI32& lambda, int startX, int startY) {
  76. if (target.get() != nullptr) {
  77. mapMonochrome<ImageU8Impl, uint8_t, 0, 255>(*target, lambda, startX, startY);
  78. }
  79. }
  80. AlignedImageU8 dsr::filter_generateU8(int width, int height, const ImageGenI32& lambda, int startX, int startY) {
  81. AlignedImageU8 result = image_create_U8(width, height);
  82. filter_mapU8(result, lambda, startX, startY);
  83. return result;
  84. }
  85. void dsr::filter_mapU16(ImageU16 target, const ImageGenI32& lambda, int startX, int startY) {
  86. if (target.get() != nullptr) {
  87. mapMonochrome<ImageU16Impl, uint16_t, 0, 65535>(*target, lambda, startX, startY);
  88. }
  89. }
  90. AlignedImageU16 dsr::filter_generateU16(int width, int height, const ImageGenI32& lambda, int startX, int startY) {
  91. AlignedImageU16 result = image_create_U16(width, height);
  92. filter_mapU16(result, lambda, startX, startY);
  93. return result;
  94. }
  95. static void mapF32(ImageF32Impl& target, const ImageGenF32& lambda, int startX, int startY) {
  96. const int targetWidth = target.width;
  97. const int targetHeight = target.height;
  98. const int targetStride = target.stride;
  99. SafePointer<float> targetRow = imageInternal::getSafeData<float>(target);
  100. for (int y = startY; y < targetHeight + startY; y++) {
  101. SafePointer<float> targetPixel = targetRow;
  102. for (int x = startX; x < targetWidth + startX; x++) {
  103. *targetPixel = lambda(x, y);
  104. targetPixel += 1;
  105. }
  106. targetRow.increaseBytes(targetStride);
  107. }
  108. }
  109. void dsr::filter_mapF32(ImageF32 target, const ImageGenF32& lambda, int startX, int startY) {
  110. if (target.get() != nullptr) {
  111. mapF32(*target, lambda, startX, startY);
  112. }
  113. }
  114. AlignedImageF32 dsr::filter_generateF32(int width, int height, const ImageGenF32& lambda, int startX, int startY) {
  115. AlignedImageF32 result = image_create_F32(width, height);
  116. filter_mapF32(result, lambda, startX, startY);
  117. return result;
  118. }
  119. // -------------------------------- Resize --------------------------------
  120. static ImageRgbaU8Impl resizeToValue(const ImageRgbaU8Impl& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  121. ImageRgbaU8Impl resultImage = ImageRgbaU8Impl(newWidth, newHeight);
  122. imageImpl_resizeToTarget(resultImage, image, interpolation == Sampler::Linear); // TODO: Pass Sampler to internal API if more modes are created
  123. return resultImage;
  124. }
  125. static OrderedImageRgbaU8 resizeToRef(const ImageRgbaU8Impl& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  126. OrderedImageRgbaU8 resultImage = image_create_RgbaU8(newWidth, newHeight);
  127. imageImpl_resizeToTarget(*resultImage, image, interpolation == Sampler::Linear); // TODO: Pass Sampler to internal API if more modes are created
  128. return resultImage;
  129. }
  130. OrderedImageRgbaU8 dsr::filter_resize(const ImageRgbaU8& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  131. if (image) {
  132. return resizeToRef(*image, interpolation, newWidth, newHeight);
  133. } else {
  134. return OrderedImageRgbaU8(); // Null gives null
  135. }
  136. }
  137. void dsr::filter_blockMagnify(ImageRgbaU8& target, const ImageRgbaU8& source, int pixelWidth, int pixelHeight) {
  138. if (target && source) {
  139. imageImpl_blockMagnify(*target, *source, pixelWidth, pixelHeight);
  140. }
  141. }
  142. // Get RGBA sub-images without allocating heads on the heap
  143. static const ImageRgbaU8Impl getView(const ImageRgbaU8Impl& image, const IRect& region) {
  144. assert(region.left() >= 0); assert(region.top() >= 0); assert(region.width() >= 1); assert(region.height() >= 1);
  145. assert(region.right() <= image.width); assert(region.bottom() <= image.height);
  146. intptr_t newOffset = image.startOffset + (region.left() * image.pixelSize) + (region.top() * image.stride);
  147. return ImageRgbaU8Impl(region.width(), region.height(), image.stride, image.buffer, newOffset, image.packOrder);
  148. }
  149. OrderedImageRgbaU8 dsr::filter_resize3x3(const ImageRgbaU8& image, Sampler interpolation, int newWidth, int newHeight, int leftBorder, int topBorder, int rightBorder, int bottomBorder) {
  150. if (image) {
  151. // Get source dimensions
  152. int sourceWidth = image->width;
  153. int sourceHeight = image->height;
  154. // Limit borders to a place near the center while leaving at least 2x2 pixels at the center for bilinear interpolation
  155. int maxLeftBorder = std::min(sourceWidth, newWidth) / 2 - 1;
  156. int maxTopBorder = std::min(sourceHeight, newHeight) / 2 - 1;
  157. int maxRightBorder = maxLeftBorder;
  158. int maxBottomBorder = maxTopBorder;
  159. if (leftBorder > maxLeftBorder) leftBorder = maxLeftBorder;
  160. if (topBorder > maxTopBorder) topBorder = maxTopBorder;
  161. if (rightBorder > maxRightBorder) rightBorder = maxRightBorder;
  162. if (bottomBorder > maxBottomBorder) bottomBorder = maxBottomBorder;
  163. if (leftBorder < 0) leftBorder = 0;
  164. if (topBorder < 0) topBorder = 0;
  165. if (rightBorder < 0) rightBorder = 0;
  166. if (bottomBorder < 0) bottomBorder = 0;
  167. // Combine dimensions
  168. // L_R T_B
  169. int leftRightBorder = leftBorder + rightBorder;
  170. int topBottomBorder = topBorder + bottomBorder;
  171. // _C_
  172. int targetCenterWidth = newWidth - leftRightBorder;
  173. int targetCenterHeight = newHeight - topBottomBorder;
  174. // LC_ RC_
  175. int targetLeftAndCenter = newWidth - rightBorder;
  176. int targetTopAndCenter = newHeight - bottomBorder;
  177. // _C_
  178. int sourceCenterWidth = sourceWidth - leftRightBorder;
  179. int sourceCenterHeight = sourceHeight - topBottomBorder;
  180. // LC_ RC_
  181. int sourceLeftAndCenter = sourceWidth - rightBorder;
  182. int sourceTopAndCenter = sourceHeight - bottomBorder;
  183. // Allocate target image
  184. OrderedImageRgbaU8 result = image_create_RgbaU8(newWidth, newHeight);
  185. ImageRgbaU8Impl* target = result.get();
  186. // Draw corners
  187. if (leftBorder > 0 && topBorder > 0) {
  188. imageImpl_drawCopy(*target, getView(*image, IRect(0, 0, leftBorder, topBorder)), 0, 0);
  189. }
  190. if (rightBorder > 0 && topBorder > 0) {
  191. imageImpl_drawCopy(*target, getView(*image, IRect(sourceLeftAndCenter, 0, rightBorder, topBorder)), targetLeftAndCenter, 0);
  192. }
  193. if (leftBorder > 0 && bottomBorder > 0) {
  194. imageImpl_drawCopy(*target, getView(*image, IRect(0, sourceTopAndCenter, leftBorder, bottomBorder)), 0, targetTopAndCenter);
  195. }
  196. if (rightBorder > 0 && bottomBorder > 0) {
  197. imageImpl_drawCopy(*target, getView(*image, IRect(sourceLeftAndCenter, sourceTopAndCenter, rightBorder, bottomBorder)), targetLeftAndCenter, targetTopAndCenter);
  198. }
  199. // Resize and draw edges
  200. if (targetCenterHeight > 0) {
  201. if (leftBorder > 0) {
  202. ImageRgbaU8Impl edgeSource = getView(*image, IRect(0, topBorder, leftBorder, sourceCenterHeight));
  203. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, leftBorder, targetCenterHeight);
  204. imageImpl_drawCopy(*target, stretchedEdge, 0, topBorder);
  205. }
  206. if (rightBorder > 0) {
  207. ImageRgbaU8Impl edgeSource = getView(*image, IRect(sourceLeftAndCenter, topBorder, rightBorder, sourceCenterHeight));
  208. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, rightBorder, targetCenterHeight);
  209. imageImpl_drawCopy(*target, stretchedEdge, targetLeftAndCenter, topBorder);
  210. }
  211. }
  212. if (targetCenterWidth > 0) {
  213. if (topBorder > 0) {
  214. ImageRgbaU8Impl edgeSource = getView(*image, IRect(leftBorder, 0, sourceCenterWidth, topBorder));
  215. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, targetCenterWidth, topBorder);
  216. imageImpl_drawCopy(*target, stretchedEdge, leftBorder, 0);
  217. }
  218. if (bottomBorder > 0) {
  219. ImageRgbaU8Impl edgeSource = getView(*image, IRect(leftBorder, sourceTopAndCenter, sourceCenterWidth, bottomBorder));
  220. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, targetCenterWidth, bottomBorder);
  221. imageImpl_drawCopy(*target, stretchedEdge, leftBorder, targetTopAndCenter);
  222. }
  223. }
  224. // Resize and draw center
  225. if (targetCenterWidth > 0 && targetCenterHeight > 0) {
  226. ImageRgbaU8Impl centerSource = getView(*image, IRect(leftBorder, topBorder, sourceCenterWidth, sourceCenterHeight));
  227. ImageRgbaU8Impl stretchedCenter = resizeToValue(centerSource, interpolation, targetCenterWidth, targetCenterHeight);
  228. imageImpl_drawCopy(*target, stretchedCenter, leftBorder, topBorder);
  229. }
  230. return result;
  231. } else {
  232. return OrderedImageRgbaU8(); // Null gives null
  233. }
  234. }