drawAPI.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. // -------------------------------- Drawing shapes --------------------------------
  85. void dsr::draw_rectangle(ImageU8& image, const IRect& bound, int color) {
  86. if (image) {
  87. imageImpl_draw_solidRectangle(*image, bound, color);
  88. }
  89. }
  90. void dsr::draw_rectangle(ImageF32& image, const IRect& bound, float color) {
  91. if (image) {
  92. imageImpl_draw_solidRectangle(*image, bound, color);
  93. }
  94. }
  95. void dsr::draw_rectangle(ImageRgbaU8& image, const IRect& bound, const ColorRgbaI32& color) {
  96. if (image) {
  97. imageImpl_draw_solidRectangle(*image, bound, color);
  98. }
  99. }
  100. void dsr::draw_line(ImageU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color) {
  101. if (image) {
  102. imageImpl_draw_line(*image, x1, y1, x2, y2, color);
  103. }
  104. }
  105. void dsr::draw_line(ImageF32& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color) {
  106. if (image) {
  107. imageImpl_draw_line(*image, x1, y1, x2, y2, color);
  108. }
  109. }
  110. void dsr::draw_line(ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color) {
  111. if (image) {
  112. imageImpl_draw_line(*image, x1, y1, x2, y2, color);
  113. }
  114. }
  115. // -------------------------------- Drawing images --------------------------------
  116. #define DRAW_COPY_WRAPPER(TARGET_TYPE, SOURCE_TYPE) \
  117. void dsr::draw_copy(TARGET_TYPE& target, const SOURCE_TYPE& source, int32_t left, int32_t top) { \
  118. if (target && source) { \
  119. imageImpl_drawCopy(*target, *source, left, top); \
  120. } \
  121. }
  122. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageRgbaU8);
  123. DRAW_COPY_WRAPPER(ImageU8, ImageU8);
  124. DRAW_COPY_WRAPPER(ImageU16, ImageU16);
  125. DRAW_COPY_WRAPPER(ImageF32, ImageF32);
  126. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageU8);
  127. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageU16);
  128. DRAW_COPY_WRAPPER(ImageRgbaU8, ImageF32);
  129. DRAW_COPY_WRAPPER(ImageU8, ImageU16);
  130. DRAW_COPY_WRAPPER(ImageU8, ImageF32);
  131. DRAW_COPY_WRAPPER(ImageU16, ImageU8);
  132. DRAW_COPY_WRAPPER(ImageU16, ImageF32);
  133. DRAW_COPY_WRAPPER(ImageF32, ImageU8);
  134. DRAW_COPY_WRAPPER(ImageF32, ImageU16);
  135. void dsr::draw_alphaFilter(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left, int32_t top) {
  136. if (target && source) {
  137. imageImpl_drawAlphaFilter(*target, *source, left, top);
  138. }
  139. }
  140. void dsr::draw_maxAlpha(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left, int32_t top, int32_t sourceAlphaOffset) {
  141. if (target && source) {
  142. imageImpl_drawMaxAlpha(*target, *source, left, top, sourceAlphaOffset);
  143. }
  144. }
  145. void dsr::draw_alphaClip(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left, int32_t top, int32_t threshold) {
  146. if (target && source) {
  147. imageImpl_drawAlphaClip(*target, *source, left, top, threshold);
  148. }
  149. }
  150. void dsr::draw_silhouette(ImageRgbaU8& target, const ImageU8& source, const ColorRgbaI32& color, int32_t left, int32_t top) {
  151. if (target && source) {
  152. imageImpl_drawSilhouette(*target, *source, color, left, top);
  153. }
  154. }
  155. void dsr::draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  156. if (targetHeight && sourceHeight) {
  157. imageImpl_drawHigher(*targetHeight, *sourceHeight, left, top, sourceHeightOffset);
  158. }
  159. }
  160. void dsr::draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  161. int32_t left, int32_t top, int32_t sourceHeightOffset) {
  162. if (targetHeight && sourceHeight && targetA && sourceA) {
  163. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, left, top, sourceHeightOffset);
  164. }
  165. }
  166. void dsr::draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  167. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  168. if (targetHeight && sourceHeight && targetA && sourceA && targetB && sourceB) {
  169. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, *targetB, *sourceB, left, top, sourceHeightOffset);
  170. }
  171. }
  172. void dsr::draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight, int32_t left, int32_t top, float sourceHeightOffset) {
  173. if (targetHeight && sourceHeight) {
  174. imageImpl_drawHigher(*targetHeight, *sourceHeight, left, top, sourceHeightOffset);
  175. }
  176. }
  177. void dsr::draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  178. int32_t left, int32_t top, float sourceHeightOffset) {
  179. if (targetHeight && sourceHeight && targetA && sourceA) {
  180. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, left, top, sourceHeightOffset);
  181. }
  182. }
  183. void dsr::draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight, ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  184. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB, int32_t left, int32_t top, float sourceHeightOffset) {
  185. if (targetHeight && sourceHeight && targetA && sourceA && targetB && sourceB) {
  186. imageImpl_drawHigher(*targetHeight, *sourceHeight, *targetA, *sourceA, *targetB, *sourceB, left, top, sourceHeightOffset);
  187. }
  188. }
  189. // -------------------------------- Resize --------------------------------
  190. static ImageRgbaU8Impl resizeToValue(const ImageRgbaU8Impl& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  191. ImageRgbaU8Impl resultImage = ImageRgbaU8Impl(newWidth, newHeight);
  192. imageImpl_resizeToTarget(resultImage, image, interpolation == Sampler::Linear); // TODO: Pass Sampler to internal API if more modes are created
  193. return resultImage;
  194. }
  195. static OrderedImageRgbaU8 resizeToRef(const ImageRgbaU8Impl& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  196. OrderedImageRgbaU8 resultImage = image_create_RgbaU8(newWidth, newHeight);
  197. imageImpl_resizeToTarget(*resultImage, image, interpolation == Sampler::Linear); // TODO: Pass Sampler to internal API if more modes are created
  198. return resultImage;
  199. }
  200. OrderedImageRgbaU8 dsr::filter_resize(const ImageRgbaU8& image, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  201. if (image) {
  202. return resizeToRef(*image, interpolation, newWidth, newHeight);
  203. } else {
  204. return OrderedImageRgbaU8(); // Null gives null
  205. }
  206. }
  207. void dsr::filter_blockMagnify(ImageRgbaU8& target, const ImageRgbaU8& source, int pixelWidth, int pixelHeight) {
  208. if (target && source) {
  209. imageImpl_blockMagnify(*target, *source, pixelWidth, pixelHeight);
  210. }
  211. }
  212. // Get RGBA sub-images without allocating heads on the heap
  213. static const ImageRgbaU8Impl getView(const ImageRgbaU8Impl& image, const IRect& region) {
  214. assert(region.left() >= 0); assert(region.top() >= 0); assert(region.width() >= 1); assert(region.height() >= 1);
  215. assert(region.right() <= image.width); assert(region.bottom() <= image.height);
  216. intptr_t newOffset = image.startOffset + (region.left() * image.pixelSize) + (region.top() * image.stride);
  217. return ImageRgbaU8Impl(region.width(), region.height(), image.stride, image.buffer, newOffset, image.packOrder);
  218. }
  219. OrderedImageRgbaU8 dsr::filter_resize3x3(const ImageRgbaU8& image, Sampler interpolation, int newWidth, int newHeight, int leftBorder, int topBorder, int rightBorder, int bottomBorder) {
  220. if (image) {
  221. // Get source dimensions
  222. int sourceWidth = image->width;
  223. int sourceHeight = image->height;
  224. // Limit borders to a place near the center while leaving at least 2x2 pixels at the center for bilinear interpolation
  225. int maxLeftBorder = std::min(sourceWidth, newWidth) / 2 - 1;
  226. int maxTopBorder = std::min(sourceHeight, newHeight) / 2 - 1;
  227. int maxRightBorder = maxLeftBorder;
  228. int maxBottomBorder = maxTopBorder;
  229. if (leftBorder > maxLeftBorder) leftBorder = maxLeftBorder;
  230. if (topBorder > maxTopBorder) topBorder = maxTopBorder;
  231. if (rightBorder > maxRightBorder) rightBorder = maxRightBorder;
  232. if (bottomBorder > maxBottomBorder) bottomBorder = maxBottomBorder;
  233. if (leftBorder < 0) leftBorder = 0;
  234. if (topBorder < 0) topBorder = 0;
  235. if (rightBorder < 0) rightBorder = 0;
  236. if (bottomBorder < 0) bottomBorder = 0;
  237. // Combine dimensions
  238. // L_R T_B
  239. int leftRightBorder = leftBorder + rightBorder;
  240. int topBottomBorder = topBorder + bottomBorder;
  241. // _C_
  242. int targetCenterWidth = newWidth - leftRightBorder;
  243. int targetCenterHeight = newHeight - topBottomBorder;
  244. // LC_ RC_
  245. int targetLeftAndCenter = newWidth - rightBorder;
  246. int targetTopAndCenter = newHeight - bottomBorder;
  247. // _C_
  248. int sourceCenterWidth = sourceWidth - leftRightBorder;
  249. int sourceCenterHeight = sourceHeight - topBottomBorder;
  250. // LC_ RC_
  251. int sourceLeftAndCenter = sourceWidth - rightBorder;
  252. int sourceTopAndCenter = sourceHeight - bottomBorder;
  253. // Allocate target image
  254. OrderedImageRgbaU8 result = image_create_RgbaU8(newWidth, newHeight);
  255. ImageRgbaU8Impl* target = result.get();
  256. // Draw corners
  257. if (leftBorder > 0 && topBorder > 0) {
  258. imageImpl_drawCopy(*target, getView(*image, IRect(0, 0, leftBorder, topBorder)), 0, 0);
  259. }
  260. if (rightBorder > 0 && topBorder > 0) {
  261. imageImpl_drawCopy(*target, getView(*image, IRect(sourceLeftAndCenter, 0, rightBorder, topBorder)), targetLeftAndCenter, 0);
  262. }
  263. if (leftBorder > 0 && bottomBorder > 0) {
  264. imageImpl_drawCopy(*target, getView(*image, IRect(0, sourceTopAndCenter, leftBorder, bottomBorder)), 0, targetTopAndCenter);
  265. }
  266. if (rightBorder > 0 && bottomBorder > 0) {
  267. imageImpl_drawCopy(*target, getView(*image, IRect(sourceLeftAndCenter, sourceTopAndCenter, rightBorder, bottomBorder)), targetLeftAndCenter, targetTopAndCenter);
  268. }
  269. // Resize and draw edges
  270. if (targetCenterHeight > 0) {
  271. if (leftBorder > 0) {
  272. ImageRgbaU8Impl edgeSource = getView(*image, IRect(0, topBorder, leftBorder, sourceCenterHeight));
  273. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, leftBorder, targetCenterHeight);
  274. imageImpl_drawCopy(*target, stretchedEdge, 0, topBorder);
  275. }
  276. if (rightBorder > 0) {
  277. ImageRgbaU8Impl edgeSource = getView(*image, IRect(sourceLeftAndCenter, topBorder, rightBorder, sourceCenterHeight));
  278. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, rightBorder, targetCenterHeight);
  279. imageImpl_drawCopy(*target, stretchedEdge, targetLeftAndCenter, topBorder);
  280. }
  281. }
  282. if (targetCenterWidth > 0) {
  283. if (topBorder > 0) {
  284. ImageRgbaU8Impl edgeSource = getView(*image, IRect(leftBorder, 0, sourceCenterWidth, topBorder));
  285. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, targetCenterWidth, topBorder);
  286. imageImpl_drawCopy(*target, stretchedEdge, leftBorder, 0);
  287. }
  288. if (bottomBorder > 0) {
  289. ImageRgbaU8Impl edgeSource = getView(*image, IRect(leftBorder, sourceTopAndCenter, sourceCenterWidth, bottomBorder));
  290. ImageRgbaU8Impl stretchedEdge = resizeToValue(edgeSource, interpolation, targetCenterWidth, bottomBorder);
  291. imageImpl_drawCopy(*target, stretchedEdge, leftBorder, targetTopAndCenter);
  292. }
  293. }
  294. // Resize and draw center
  295. if (targetCenterWidth > 0 && targetCenterHeight > 0) {
  296. ImageRgbaU8Impl centerSource = getView(*image, IRect(leftBorder, topBorder, sourceCenterWidth, sourceCenterHeight));
  297. ImageRgbaU8Impl stretchedCenter = resizeToValue(centerSource, interpolation, targetCenterWidth, targetCenterHeight);
  298. imageImpl_drawCopy(*target, stretchedCenter, leftBorder, topBorder);
  299. }
  300. return result;
  301. } else {
  302. return OrderedImageRgbaU8(); // Null gives null
  303. }
  304. }