drawAPI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "drawAPI.h"
  27. #include "../image/draw.h"
  28. #include "../image/ImageRgbaU8.h"
  29. #include "../image/PackOrder.h"
  30. #include "../image/internal/imageTemplate.h"
  31. #include "../image/internal/imageInternal.h"
  32. using namespace dsr;
  33. // -------------------------------- Image generation and filtering --------------------------------
  34. static void mapRgbaU8(ImageRgbaU8Impl& target, const ImageGenRgbaU8& lambda, int startX, int startY) {
  35. const int targetWidth = target.width;
  36. const int targetHeight = target.height;
  37. const int targetStride = target.stride;
  38. // Use the output directly
  39. SafePointer<Color4xU8> targetRow = imageInternal::getSafeData<Color4xU8>(target);
  40. for (int y = startY; y < targetHeight + startY; y++) {
  41. SafePointer<Color4xU8> targetPixel = targetRow;
  42. for (int x = startX; x < targetWidth + startX; x++) {
  43. *targetPixel = target.packRgba(lambda(x, y).saturate());
  44. targetPixel += 1;
  45. }
  46. targetRow.increaseBytes(targetStride);
  47. }
  48. }
  49. void dsr::filter_mapRgbaU8(ImageRgbaU8& target, const ImageGenRgbaU8& lambda, int startX, int startY) {
  50. if (target) {
  51. mapRgbaU8(*target, lambda, startX, startY);
  52. }
  53. }
  54. ImageRgbaU8 dsr::filter_generateRgbaU8(int width, int height, const ImageGenRgbaU8& lambda, int startX, int startY) {
  55. ImageRgbaU8 result = image_create_RgbaU8(width, height);
  56. filter_mapRgbaU8(result, lambda, startX, startY);
  57. return result;
  58. }
  59. static void mapF32(ImageF32Impl& target, const ImageGenF32& lambda, int startX, int startY) {
  60. const int targetWidth = target.width;
  61. const int targetHeight = target.height;
  62. const int targetStride = target.stride;
  63. // Use the output directly
  64. SafePointer<float> targetRow = imageInternal::getSafeData<float>(target);
  65. for (int y = startY; y < targetHeight + startY; y++) {
  66. SafePointer<float> targetPixel = targetRow;
  67. for (int x = startX; x < targetWidth + startX; x++) {
  68. *targetPixel = lambda(x, y);
  69. targetPixel += 1;
  70. }
  71. targetRow.increaseBytes(targetStride);
  72. }
  73. }
  74. void dsr::filter_mapF32(ImageF32& target, const ImageGenF32& lambda, int startX, int startY) {
  75. if (target) {
  76. mapF32(*target, lambda, startX, startY);
  77. }
  78. }
  79. ImageF32 dsr::filter_generateF32(int width, int height, const ImageGenF32& lambda, int startX, int startY) {
  80. ImageF32 result = image_create_F32(width, height);
  81. filter_mapF32(result, lambda, startX, startY);
  82. return result;
  83. }
  84. // Basic immutable image operations
  85. // TODO: Create optimized in-place versions for aligned images based on the reference implementations
  86. static const uint32_t imageMultiply_shift = 10;
  87. static const float imageMultiply_scale = (1u << imageMultiply_shift) / 255.0f;
  88. ImageRgbaU8 dsr::filter_mulColorRgb(const ImageRgbaU8& inputImage, const ColorRgbI32& color) {
  89. if (inputImage) {
  90. const int iR = (float)color.red * imageMultiply_scale;
  91. const int iG = (float)color.green * imageMultiply_scale;
  92. const int iB = (float)color.blue * imageMultiply_scale;
  93. return filter_generateRgbaU8(inputImage->width, inputImage->height, [inputImage, iR, iG, iB](int x, int y)->ColorRgbaI32 {
  94. ColorRgbaI32 source = image_readPixel_clamp(inputImage, x, y);
  95. return ColorRgbaI32((source.red * iR) >> imageMultiply_shift, (source.green * iG) >> imageMultiply_shift, (source.blue * iB) >> imageMultiply_shift, source.alpha);
  96. });
  97. } else {
  98. return ImageRgbaU8();
  99. }
  100. }
  101. ImageRgbaU8 dsr::filter_mulColorRgba(const ImageRgbaU8& inputImage, const ColorRgbaI32& color) {
  102. if (inputImage) {
  103. const int iR = (float)color.red * imageMultiply_scale;
  104. const int iG = (float)color.green * imageMultiply_scale;
  105. const int iB = (float)color.blue * imageMultiply_scale;
  106. const int iA = (float)color.alpha * imageMultiply_scale;
  107. return filter_generateRgbaU8(inputImage->width, inputImage->height, [inputImage, iR, iG, iB, iA](int x, int y)->ColorRgbaI32 {
  108. ColorRgbaI32 source = image_readPixel_clamp(inputImage, x, y);
  109. return ColorRgbaI32((source.red * iR) >> imageMultiply_shift, (source.green * iG) >> imageMultiply_shift, (source.blue * iB) >> imageMultiply_shift, (source.alpha * iA) >> imageMultiply_shift);
  110. });
  111. } else {
  112. return ImageRgbaU8();
  113. }
  114. }
  115. // -------------------------------- Drawing shapes --------------------------------
  116. void dsr::draw_rectangle(ImageU8& image, const IRect& bound, int color) {
  117. if (image) {
  118. imageImpl_draw_solidRectangle(*image, bound, color);
  119. }
  120. }
  121. void dsr::draw_rectangle(ImageF32& image, const IRect& bound, float color) {
  122. if (image) {
  123. imageImpl_draw_solidRectangle(*image, bound, color);
  124. }
  125. }
  126. void dsr::draw_rectangle(ImageRgbaU8& image, const IRect& bound, const ColorRgbaI32& color) {
  127. if (image) {
  128. imageImpl_draw_solidRectangle(*image, bound, color);
  129. }
  130. }
  131. void dsr::draw_line(ImageU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color) {
  132. if (image) {
  133. imageImpl_draw_line(*image, x1, y1, x2, y2, color);
  134. }
  135. }
  136. void dsr::draw_line(ImageF32& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color) {
  137. if (image) {
  138. imageImpl_draw_line(*image, x1, y1, x2, y2, color);
  139. }
  140. }
  141. void dsr::draw_line(ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color) {
  142. if (image) {
  143. imageImpl_draw_line(*image, x1, y1, x2, y2, color);
  144. }
  145. }
  146. // -------------------------------- Drawing images --------------------------------
  147. #define DRAW_COPY_WRAPPER(TARGET_TYPE, SOURCE_TYPE) \
  148. void dsr::draw_copy(TARGET_TYPE& target, const SOURCE_TYPE& source, int32_t left, int32_t top) { \
  149. if (target && source) { \
  150. imageImpl_drawCopy(*target, *source, left, top); \
  151. } \
  152. }
  153. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageRgbaU8);
  154. DRAW_COPY_WRAPPER(ImageU8, ImageU8);
  155. DRAW_COPY_WRAPPER(ImageU16, ImageU16);
  156. DRAW_COPY_WRAPPER(ImageF32, ImageF32);
  157. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageU8);
  158. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageU16);
  159. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageF32);
  160. DRAW_COPY_WRAPPER(ImageU8, ImageU16);
  161. DRAW_COPY_WRAPPER(ImageU8, ImageF32);
  162. DRAW_COPY_WRAPPER(ImageU16, ImageU8);
  163. DRAW_COPY_WRAPPER(ImageU16, ImageF32);
  164. DRAW_COPY_WRAPPER(ImageF32, ImageU8);
  165. DRAW_COPY_WRAPPER(ImageF32, ImageU16);
  166. void dsr::draw_alphaFilter(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left, int32_t top) {
  167. if (target && source) {
  168. imageImpl_drawAlphaFilter(*target, *source, left, top);
  169. }
  170. }
  171. void dsr::draw_maxAlpha(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left, int32_t top, int32_t sourceAlphaOffset) {
  172. if (target && source) {
  173. imageImpl_drawMaxAlpha(*target, *source, left, top, sourceAlphaOffset);
  174. }
  175. }
  176. void dsr::draw_alphaClip(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left, int32_t top, int32_t threshold) {
  177. if (target && source) {
  178. imageImpl_drawAlphaClip(*target, *source, left, top, threshold);
  179. }
  180. }
  181. void dsr::draw_silhouette(ImageRgbaU8& target, const ImageU8& source, const ColorRgbaI32& color, int32_t left, int32_t top) {
  182. if (target && source) {
  183. imageImpl_drawSilhouette(*target, *source, color, left, top);
  184. }
  185. }
  186. void dsr::draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  187. if (targetHeight && sourceHeight) {
  188. imageImpl_drawHigher(*targetHeight, *sourceHeight, left, top, sourceHeightOffset);
  189. }
  190. }
  191. void dsr::draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  192. int32_t left, int32_t top, int32_t sourceHeightOffset) {
  193. if (targetHeight && sourceHeight && targetA && sourceA) {
  194. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, left, top, sourceHeightOffset);
  195. }
  196. }
  197. void dsr::draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  198. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  199. if (targetHeight && sourceHeight && targetA && sourceA && targetB && sourceB) {
  200. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, *targetB, *sourceB, left, top, sourceHeightOffset);
  201. }
  202. }
  203. void dsr::draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight, int32_t left, int32_t top, float sourceHeightOffset) {
  204. if (targetHeight && sourceHeight) {
  205. imageImpl_drawHigher(*targetHeight, *sourceHeight, left, top, sourceHeightOffset);
  206. }
  207. }
  208. void dsr::draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  209. int32_t left, int32_t top, float sourceHeightOffset) {
  210. if (targetHeight && sourceHeight && targetA && sourceA) {
  211. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, left, top, sourceHeightOffset);
  212. }
  213. }
  214. void dsr::draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  215. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB, int32_t left, int32_t top, float sourceHeightOffset) {
  216. if (targetHeight && sourceHeight && targetA && sourceA && targetB && sourceB) {
  217. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, *targetB, *sourceB, left, top, sourceHeightOffset);
  218. }
  219. }
  220. // -------------------------------- Resize --------------------------------
  221. static ImageRgbaU8Impl resizeToValue(const ImageRgbaU8Impl& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  222. ImageRgbaU8Impl resultImage = ImageRgbaU8Impl(newWidth, newHeight);
  223. imageImpl_resizeToTarget(resultImage, image, interpolation == Sampler::Linear); // TODO: Pass Sampler to internal API if more modes are created
  224. return resultImage;
  225. }
  226. static OrderedImageRgbaU8 resizeToRef(const ImageRgbaU8Impl& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  227. OrderedImageRgbaU8 resultImage = image_create_RgbaU8(newWidth, newHeight);
  228. imageImpl_resizeToTarget(*resultImage, image, interpolation == Sampler::Linear); // TODO: Pass Sampler to internal API if more modes are created
  229. return resultImage;
  230. }
  231. OrderedImageRgbaU8 dsr::filter_resize(const ImageRgbaU8& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  232. if (image) {
  233. return resizeToRef(*image, interpolation, newWidth, newHeight);
  234. } else {
  235. return OrderedImageRgbaU8(); // Null gives null
  236. }
  237. }
  238. void dsr::filter_blockMagnify(ImageRgbaU8& target, const ImageRgbaU8& source, int pixelWidth, int pixelHeight) {
  239. if (target && source) {
  240. imageImpl_blockMagnify(*target, *source, pixelWidth, pixelHeight);
  241. }
  242. }
  243. // Get RGBA sub-images without allocating heads on the heap
  244. static const ImageRgbaU8Impl getView(const ImageRgbaU8Impl& image, const IRect& region) {
  245. assert(region.left() >= 0); assert(region.top() >= 0); assert(region.width() >= 1); assert(region.height() >= 1);
  246. assert(region.right() <= image.width); assert(region.bottom() <= image.height);
  247. intptr_t newOffset = image.startOffset + (region.left() * image.pixelSize) + (region.top() * image.stride);
  248. return ImageRgbaU8Impl(region.width(), region.height(), image.stride, image.buffer, newOffset, image.packOrder);
  249. }
  250. OrderedImageRgbaU8 dsr::filter_resize3x3(const ImageRgbaU8& image, Sampler interpolation, int newWidth, int newHeight, int leftBorder, int topBorder, int rightBorder, int bottomBorder) {
  251. if (image) {
  252. // Get source dimensions
  253. int sourceWidth = image->width;
  254. int sourceHeight = image->height;
  255. // Limit borders to a place near the center while leaving at least 2x2 pixels at the center for bilinear interpolation
  256. int maxLeftBorder = std::min(sourceWidth, newWidth) / 2 - 1;
  257. int maxTopBorder = std::min(sourceHeight, newHeight) / 2 - 1;
  258. int maxRightBorder = maxLeftBorder;
  259. int maxBottomBorder = maxTopBorder;
  260. if (leftBorder > maxLeftBorder) leftBorder = maxLeftBorder;
  261. if (topBorder > maxTopBorder) topBorder = maxTopBorder;
  262. if (rightBorder > maxRightBorder) rightBorder = maxRightBorder;
  263. if (bottomBorder > maxBottomBorder) bottomBorder = maxBottomBorder;
  264. if (leftBorder < 0) leftBorder = 0;
  265. if (topBorder < 0) topBorder = 0;
  266. if (rightBorder < 0) rightBorder = 0;
  267. if (bottomBorder < 0) bottomBorder = 0;
  268. // Combine dimensions
  269. // L_R T_B
  270. int leftRightBorder = leftBorder + rightBorder;
  271. int topBottomBorder = topBorder + bottomBorder;
  272. // _C_
  273. int targetCenterWidth = newWidth - leftRightBorder;
  274. int targetCenterHeight = newHeight - topBottomBorder;
  275. // LC_ RC_
  276. int targetLeftAndCenter = newWidth - rightBorder;
  277. int targetTopAndCenter = newHeight - bottomBorder;
  278. // _C_
  279. int sourceCenterWidth = sourceWidth - leftRightBorder;
  280. int sourceCenterHeight = sourceHeight - topBottomBorder;
  281. // LC_ RC_
  282. int sourceLeftAndCenter = sourceWidth - rightBorder;
  283. int sourceTopAndCenter = sourceHeight - bottomBorder;
  284. // Allocate target image
  285. OrderedImageRgbaU8 result = image_create_RgbaU8(newWidth, newHeight);
  286. ImageRgbaU8Impl* target = result.get();
  287. // Draw corners
  288. if (leftBorder > 0 && topBorder > 0) {
  289. imageImpl_drawCopy(*target, getView(*image, IRect(0, 0, leftBorder, topBorder)), 0, 0);
  290. }
  291. if (rightBorder > 0 && topBorder > 0) {
  292. imageImpl_drawCopy(*target, getView(*image, IRect(sourceLeftAndCenter, 0, rightBorder, topBorder)), targetLeftAndCenter, 0);
  293. }
  294. if (leftBorder > 0 && bottomBorder > 0) {
  295. imageImpl_drawCopy(*target, getView(*image, IRect(0, sourceTopAndCenter, leftBorder, bottomBorder)), 0, targetTopAndCenter);
  296. }
  297. if (rightBorder > 0 && bottomBorder > 0) {
  298. imageImpl_drawCopy(*target, getView(*image, IRect(sourceLeftAndCenter, sourceTopAndCenter, rightBorder, bottomBorder)), targetLeftAndCenter, targetTopAndCenter);
  299. }
  300. // Resize and draw edges
  301. if (targetCenterHeight > 0) {
  302. if (leftBorder > 0) {
  303. ImageRgbaU8Impl edgeSource = getView(*image, IRect(0, topBorder, leftBorder, sourceCenterHeight));
  304. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, leftBorder, targetCenterHeight);
  305. imageImpl_drawCopy(*target, stretchedEdge, 0, topBorder);
  306. }
  307. if (rightBorder > 0) {
  308. ImageRgbaU8Impl edgeSource = getView(*image, IRect(sourceLeftAndCenter, topBorder, rightBorder, sourceCenterHeight));
  309. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, rightBorder, targetCenterHeight);
  310. imageImpl_drawCopy(*target, stretchedEdge, targetLeftAndCenter, topBorder);
  311. }
  312. }
  313. if (targetCenterWidth > 0) {
  314. if (topBorder > 0) {
  315. ImageRgbaU8Impl edgeSource = getView(*image, IRect(leftBorder, 0, sourceCenterWidth, topBorder));
  316. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, targetCenterWidth, topBorder);
  317. imageImpl_drawCopy(*target, stretchedEdge, leftBorder, 0);
  318. }
  319. if (bottomBorder > 0) {
  320. ImageRgbaU8Impl edgeSource = getView(*image, IRect(leftBorder, sourceTopAndCenter, sourceCenterWidth, bottomBorder));
  321. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, targetCenterWidth, bottomBorder);
  322. imageImpl_drawCopy(*target, stretchedEdge, leftBorder, targetTopAndCenter);
  323. }
  324. }
  325. // Resize and draw center
  326. if (targetCenterWidth > 0 && targetCenterHeight > 0) {
  327. ImageRgbaU8Impl centerSource = getView(*image, IRect(leftBorder, topBorder, sourceCenterWidth, sourceCenterHeight));
  328. ImageRgbaU8Impl stretchedCenter = resizeToValue(centerSource, interpolation, targetCenterWidth, targetCenterHeight);
  329. imageImpl_drawCopy(*target, stretchedCenter, leftBorder, topBorder);
  330. }
  331. return result;
  332. } else {
  333. return OrderedImageRgbaU8(); // Null gives null
  334. }
  335. }