draw.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 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. #include "../base/simdExtra.h"
  24. #include "draw.h"
  25. #include "internal/imageInternal.h"
  26. #include "../math/scalar.h"
  27. #include <limits>
  28. using namespace dsr;
  29. // -------------------------------- Drawing shapes --------------------------------
  30. template <typename COLOR_TYPE>
  31. static inline void drawSolidRectangleAssign(ImageImpl &target, int left, int top, int right, int bottom, COLOR_TYPE color) {
  32. int leftBound = std::max(0, left);
  33. int topBound = std::max(0, top);
  34. int rightBound = std::min(right, target.width);
  35. int bottomBound = std::min(bottom, target.height);
  36. int stride = target.stride;
  37. SafePointer<COLOR_TYPE> rowData = imageInternal::getSafeData<COLOR_TYPE>(target, topBound);
  38. rowData += leftBound;
  39. for (int y = topBound; y < bottomBound; y++) {
  40. SafePointer<COLOR_TYPE> pixelData = rowData;
  41. for (int x = leftBound; x < rightBound; x++) {
  42. pixelData.get() = color;
  43. pixelData += 1;
  44. }
  45. rowData.increaseBytes(stride);
  46. }
  47. }
  48. template <typename COLOR_TYPE>
  49. static inline void drawSolidRectangleMemset(ImageImpl &target, int left, int top, int right, int bottom, uint8_t uniformByte) {
  50. int leftBound = std::max(0, left);
  51. int topBound = std::max(0, top);
  52. int rightBound = std::min(right, target.width);
  53. int bottomBound = std::min(bottom, target.height);
  54. if (rightBound > leftBound && bottomBound > topBound) {
  55. int stride = target.stride;
  56. SafePointer<COLOR_TYPE> rowData = imageInternal::getSafeData<COLOR_TYPE>(target, topBound);
  57. rowData += leftBound;
  58. int filledWidth = rightBound - leftBound;
  59. int rowSize = filledWidth * sizeof(COLOR_TYPE);
  60. int rowCount = bottomBound - topBound;
  61. if (!target.isSubImage && filledWidth == target.width) {
  62. // Write over any padding for parent images owning the whole buffer.
  63. // Including parent images with sub-images using the same data
  64. // because no child image may display the parent-image's padding bytes.
  65. safeMemorySet(rowData, uniformByte, (stride * (rowCount - 1)) + rowSize);
  66. } else if (rowSize == stride) {
  67. // When the filled row stretches all the way from left to right in the main allocation
  68. // there's no unseen pixels being overwritten in other images sharing the buffer.
  69. // This case handles sub-images that uses the full width of
  70. // the parent image which doesn't have any padding.
  71. safeMemorySet(rowData, uniformByte, rowSize * rowCount);
  72. } else {
  73. // Fall back on using one memset operation per row.
  74. // This case is for sub-images that must preserve interleaved pixel rows belonging
  75. // to other images that aren't visible and therefore not owned by this image.
  76. for (int y = topBound; y < bottomBound; y++) {
  77. safeMemorySet(rowData, uniformByte, rowSize);
  78. rowData.increaseBytes(stride);
  79. }
  80. }
  81. }
  82. }
  83. void dsr::imageImpl_draw_solidRectangle(ImageU8Impl& image, const IRect& bound, int color) {
  84. if (color < 0) { color = 0; }
  85. if (color > 255) { color = 255; }
  86. drawSolidRectangleMemset<uint8_t>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), color);
  87. }
  88. void dsr::imageImpl_draw_solidRectangle(ImageU16Impl& image, const IRect& bound, int color) {
  89. if (color < 0) { color = 0; }
  90. if (color > 65535) { color = 65535; }
  91. uint16_t uColor = color;
  92. if (isUniformByteU16(uColor)) {
  93. drawSolidRectangleMemset<uint16_t>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), 0);
  94. } else {
  95. drawSolidRectangleAssign<uint16_t>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), uColor);
  96. }
  97. }
  98. void dsr::imageImpl_draw_solidRectangle(ImageF32Impl& image, const IRect& bound, float color) {
  99. if (color == 0.0f) {
  100. drawSolidRectangleMemset<float>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), 0);
  101. } else {
  102. drawSolidRectangleAssign<float>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), color);
  103. }
  104. }
  105. void dsr::imageImpl_draw_solidRectangle(ImageRgbaU8Impl& image, const IRect& bound, const ColorRgbaI32& color) {
  106. Color4xU8 packedColor = image.packRgba(color.saturate());
  107. if (packedColor.isUniformByte()) {
  108. drawSolidRectangleMemset<Color4xU8>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), packedColor.channels[0]);
  109. } else {
  110. drawSolidRectangleAssign<Color4xU8>(image, bound.left(), bound.top(), bound.right(), bound.bottom(), packedColor);
  111. }
  112. }
  113. template <typename IMAGE_TYPE, typename COLOR_TYPE>
  114. inline void drawLineSuper(IMAGE_TYPE &target, int x1, int y1, int x2, int y2, COLOR_TYPE color) {
  115. if (y1 == y2) {
  116. // Sideways
  117. int left = std::min(x1, x2);
  118. int right = std::max(x1, x2);
  119. for (int x = left; x <= right; x++) {
  120. IMAGE_TYPE::writePixel(target, x, y1, color);
  121. }
  122. } else if (x1 == x2) {
  123. // Down
  124. int top = std::min(y1, y2);
  125. int bottom = std::max(y1, y2);
  126. for (int y = top; y <= bottom; y++) {
  127. IMAGE_TYPE::writePixel(target, x1, y, color);
  128. }
  129. } else {
  130. if (std::abs(y2 - y1) >= std::abs(x2 - x1)) {
  131. if (y2 < y1) {
  132. swap(x1, x2);
  133. swap(y1, y2);
  134. }
  135. assert(y2 > y1);
  136. if (x2 > x1) {
  137. // Down right
  138. int x = x1;
  139. int y = y1;
  140. int tilt = (x2 - x1) * 2;
  141. int maxError = y2 - y1;
  142. int error = 0;
  143. while (y <= y2) {
  144. IMAGE_TYPE::writePixel(target, x, y, color);
  145. error += tilt;
  146. if (error >= maxError) {
  147. x++;
  148. error -= maxError * 2;
  149. }
  150. y++;
  151. }
  152. } else {
  153. // Down left
  154. int x = x1;
  155. int y = y1;
  156. int tilt = (x1 - x2) * 2;
  157. int maxError = y2 - y1;
  158. int error = 0;
  159. while (y <= y2) {
  160. IMAGE_TYPE::writePixel(target, x, y, color);
  161. error += tilt;
  162. if (error >= maxError) {
  163. x--;
  164. error -= maxError * 2;
  165. }
  166. y++;
  167. }
  168. }
  169. } else {
  170. if (x2 < x1) {
  171. swap(x1, x2);
  172. swap(y1, y2);
  173. }
  174. assert(x2 > x1);
  175. if (y2 > y1) {
  176. // Down right
  177. int x = x1;
  178. int y = y1;
  179. int tilt = (y2 - y1) * 2;
  180. int maxError = x2 - x1;
  181. int error = 0;
  182. while (x <= x2) {
  183. IMAGE_TYPE::writePixel(target, x, y, color);
  184. error += tilt;
  185. if (error >= maxError) {
  186. y++;
  187. error -= maxError * 2;
  188. }
  189. x++;
  190. }
  191. } else {
  192. // Up right
  193. int x = x1;
  194. int y = y1;
  195. int tilt = (y1 - y2) * 2;
  196. int maxError = x2 - x1;
  197. int error = 0;
  198. while (x <= x2) {
  199. IMAGE_TYPE::writePixel(target, x, y, color);
  200. error += tilt;
  201. if (error >= maxError) {
  202. y--;
  203. error -= maxError * 2;
  204. }
  205. x++;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. void dsr::imageImpl_draw_line(ImageU8Impl& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color) {
  212. if (color < 0) { color = 0; }
  213. if (color > 255) { color = 255; }
  214. drawLineSuper<ImageU8Impl, uint8_t>(image, x1, y1, x2, y2, color);
  215. }
  216. void dsr::imageImpl_draw_line(ImageU16Impl& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color) {
  217. if (color < 0) { color = 0; }
  218. if (color > 65535) { color = 65535; }
  219. drawLineSuper<ImageU16Impl, uint16_t>(image, x1, y1, x2, y2, color);
  220. }
  221. void dsr::imageImpl_draw_line(ImageF32Impl& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color) {
  222. drawLineSuper<ImageF32Impl, float>(image, x1, y1, x2, y2, color);
  223. }
  224. void dsr::imageImpl_draw_line(ImageRgbaU8Impl& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color) {
  225. drawLineSuper<ImageRgbaU8Impl, Color4xU8>(image, x1, y1, x2, y2, image.packRgba(color.saturate()));
  226. }
  227. // -------------------------------- Drawing images --------------------------------
  228. // A packet with the dimensions of an image
  229. struct ImageDimensions {
  230. // width is the number of used pixels on each row.
  231. // height is the number of rows.
  232. // stride is the byte offset from one row to another including any padding.
  233. // pixelSize is the byte offset from one pixel to another from left to right.
  234. int32_t width, height, stride, pixelSize;
  235. ImageDimensions() : width(0), height(0), stride(0), pixelSize(0) {}
  236. ImageDimensions(const ImageImpl& image) :
  237. width(image.width), height(image.height), stride(image.stride), pixelSize(image.pixelSize) {}
  238. };
  239. struct ImageWriter : public ImageDimensions {
  240. uint8_t *data;
  241. ImageWriter(const ImageDimensions &dimensions, uint8_t *data) :
  242. ImageDimensions(dimensions), data(data) {}
  243. };
  244. struct ImageReader : public ImageDimensions {
  245. const uint8_t *data;
  246. ImageReader(const ImageDimensions &dimensions, const uint8_t *data) :
  247. ImageDimensions(dimensions), data(data) {}
  248. };
  249. static ImageWriter getWriter(ImageImpl &image) {
  250. return ImageWriter(ImageDimensions(image), buffer_dangerous_getUnsafeData(image.buffer) + image.startOffset);
  251. }
  252. static ImageReader getReader(const ImageImpl &image) {
  253. return ImageReader(ImageDimensions(image), buffer_dangerous_getUnsafeData(image.buffer) + image.startOffset);
  254. }
  255. static ImageImpl getGenericSubImage(const ImageImpl &image, int32_t left, int32_t top, int32_t width, int32_t height) {
  256. assert(left >= 0 && top >= 0 && width >= 1 && height >= 1 && left + width <= image.width && top + height <= image.height);
  257. intptr_t newOffset = image.startOffset + (left * image.pixelSize) + (top * image.stride);
  258. return ImageImpl(width, height, image.stride, image.pixelSize, image.buffer, newOffset);
  259. }
  260. struct ImageIntersection {
  261. ImageWriter subTarget;
  262. ImageReader subSource;
  263. ImageIntersection(const ImageWriter &subTarget, const ImageReader &subSource) :
  264. subTarget(subTarget), subSource(subSource) {}
  265. static bool canCreate(ImageImpl &target, const ImageImpl &source, int32_t left, int32_t top) {
  266. int32_t targetRegionRight = left + source.width;
  267. int32_t targetRegionBottom = top + source.height;
  268. return left < target.width && top < target.height && targetRegionRight > 0 && targetRegionBottom > 0;
  269. }
  270. // Only call if canCreate passed with the same arguments
  271. static ImageIntersection create(ImageImpl &target, const ImageImpl &source, int32_t left, int32_t top) {
  272. int32_t targetRegionRight = left + source.width;
  273. int32_t targetRegionBottom = top + source.height;
  274. assert(ImageIntersection::canCreate(target, source, left, top));
  275. // Check if the source has to be clipped
  276. if (left < 0 || top < 0 || targetRegionRight > target.width || targetRegionBottom > target.height) {
  277. int32_t clipLeft = std::max(0, -left);
  278. int32_t clipTop = std::max(0, -top);
  279. int32_t clipRight = std::max(0, targetRegionRight - target.width);
  280. int32_t clipBottom = std::max(0, targetRegionBottom - target.height);
  281. int32_t newWidth = source.width - (clipLeft + clipRight);
  282. int32_t newHeight = source.height - (clipTop + clipBottom);
  283. assert(newWidth > 0 && newHeight > 0);
  284. // Partial drawing
  285. ImageImpl subTarget = getGenericSubImage(target, left + clipLeft, top + clipTop, newWidth, newHeight);
  286. ImageImpl subSource = getGenericSubImage(source, clipLeft, clipTop, newWidth, newHeight);
  287. return ImageIntersection(getWriter(subTarget), getReader(subSource));
  288. } else {
  289. // Full drawing
  290. ImageImpl subTarget = getGenericSubImage(target, left, top, source.width, source.height);
  291. return ImageIntersection(getWriter(subTarget), getReader(source));
  292. }
  293. }
  294. };
  295. #define ITERATE_ROWS(WRITER, READER, OPERATION) \
  296. { \
  297. uint8_t *targetRow = WRITER.data; \
  298. const uint8_t *sourceRow = READER.data; \
  299. for (int32_t y = 0; y < READER.height; y++) { \
  300. OPERATION; \
  301. targetRow += WRITER.stride; \
  302. sourceRow += READER.stride; \
  303. } \
  304. }
  305. #define ITERATE_PIXELS(WRITER, READER, OPERATION) \
  306. { \
  307. uint8_t *targetRow = WRITER.data; \
  308. const uint8_t *sourceRow = READER.data; \
  309. for (int32_t y = 0; y < READER.height; y++) { \
  310. uint8_t *targetPixel = targetRow; \
  311. const uint8_t *sourcePixel = sourceRow; \
  312. for (int32_t x = 0; x < READER.width; x++) { \
  313. {OPERATION;} \
  314. targetPixel += WRITER.pixelSize; \
  315. sourcePixel += READER.pixelSize; \
  316. } \
  317. targetRow += WRITER.stride; \
  318. sourceRow += READER.stride; \
  319. } \
  320. }
  321. #define ITERATE_PIXELS_2(WRITER1, READER1, WRITER2, READER2, OPERATION) \
  322. { \
  323. uint8_t *targetRow1 = WRITER1.data; \
  324. uint8_t *targetRow2 = WRITER2.data; \
  325. const uint8_t *sourceRow1 = READER1.data; \
  326. const uint8_t *sourceRow2 = READER2.data; \
  327. int minWidth = std::min(READER1.width, READER2.width); \
  328. int minHeight = std::min(READER1.height, READER2.height); \
  329. for (int32_t y = 0; y < minHeight; y++) { \
  330. uint8_t *targetPixel1 = targetRow1; \
  331. uint8_t *targetPixel2 = targetRow2; \
  332. const uint8_t *sourcePixel1 = sourceRow1; \
  333. const uint8_t *sourcePixel2 = sourceRow2; \
  334. for (int32_t x = 0; x < minWidth; x++) { \
  335. {OPERATION;} \
  336. targetPixel1 += WRITER1.pixelSize; \
  337. targetPixel2 += WRITER2.pixelSize; \
  338. sourcePixel1 += READER1.pixelSize; \
  339. sourcePixel2 += READER2.pixelSize; \
  340. } \
  341. targetRow1 += WRITER1.stride; \
  342. targetRow2 += WRITER2.stride; \
  343. sourceRow1 += READER1.stride; \
  344. sourceRow2 += READER2.stride; \
  345. } \
  346. }
  347. #define ITERATE_PIXELS_3(WRITER1, READER1, WRITER2, READER2, WRITER3, READER3, OPERATION) \
  348. { \
  349. uint8_t *targetRow1 = WRITER1.data; \
  350. uint8_t *targetRow2 = WRITER2.data; \
  351. uint8_t *targetRow3 = WRITER3.data; \
  352. const uint8_t *sourceRow1 = READER1.data; \
  353. const uint8_t *sourceRow2 = READER2.data; \
  354. const uint8_t *sourceRow3 = READER3.data; \
  355. int minWidth = std::min(std::min(READER1.width, READER2.width), READER3.width); \
  356. int minHeight = std::min(std::min(READER1.height, READER2.height), READER3.height); \
  357. for (int32_t y = 0; y < minHeight; y++) { \
  358. uint8_t *targetPixel1 = targetRow1; \
  359. uint8_t *targetPixel2 = targetRow2; \
  360. uint8_t *targetPixel3 = targetRow3; \
  361. const uint8_t *sourcePixel1 = sourceRow1; \
  362. const uint8_t *sourcePixel2 = sourceRow2; \
  363. const uint8_t *sourcePixel3 = sourceRow3; \
  364. for (int32_t x = 0; x < minWidth; x++) { \
  365. {OPERATION;} \
  366. targetPixel1 += WRITER1.pixelSize; \
  367. targetPixel2 += WRITER2.pixelSize; \
  368. targetPixel3 += WRITER3.pixelSize; \
  369. sourcePixel1 += READER1.pixelSize; \
  370. sourcePixel2 += READER2.pixelSize; \
  371. sourcePixel3 += READER3.pixelSize; \
  372. } \
  373. targetRow1 += WRITER1.stride; \
  374. targetRow2 += WRITER2.stride; \
  375. targetRow3 += WRITER3.stride; \
  376. sourceRow1 += READER1.stride; \
  377. sourceRow2 += READER2.stride; \
  378. sourceRow3 += READER3.stride; \
  379. } \
  380. }
  381. static inline int saturateFloat(float value) {
  382. if (!(value >= 0.0f)) {
  383. // NaN or negative
  384. return 0;
  385. } else if (value > 255.0f) {
  386. // Too large
  387. return 255;
  388. } else {
  389. // Round to closest
  390. return (int)(value + 0.5f);
  391. }
  392. }
  393. // Copy data from one image region to another of the same size.
  394. // Packing order is reinterpreted without conversion.
  395. static void copyImageData(ImageWriter writer, ImageReader reader) {
  396. assert(writer.width == reader.width && writer.height == reader.height && writer.pixelSize == reader.pixelSize);
  397. ITERATE_ROWS(writer, reader, std::memcpy(targetRow, sourceRow, reader.width * reader.pixelSize));
  398. }
  399. void dsr::imageImpl_drawCopy(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int32_t left, int32_t top) {
  400. if (ImageIntersection::canCreate(target, source, left, top)) {
  401. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  402. if (target.packOrder == source.packOrder) {
  403. // No conversion needed
  404. copyImageData(intersection.subTarget, intersection.subSource);
  405. } else {
  406. // Read and repack to convert between different color formats
  407. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  408. targetPixel[target.packOrder.redIndex] = sourcePixel[source.packOrder.redIndex];
  409. targetPixel[target.packOrder.greenIndex] = sourcePixel[source.packOrder.greenIndex];
  410. targetPixel[target.packOrder.blueIndex] = sourcePixel[source.packOrder.blueIndex];
  411. targetPixel[target.packOrder.alphaIndex] = sourcePixel[source.packOrder.alphaIndex];
  412. );
  413. }
  414. }
  415. }
  416. void dsr::imageImpl_drawCopy(ImageU8Impl& target, const ImageU8Impl& source, int32_t left, int32_t top) {
  417. if (ImageIntersection::canCreate(target, source, left, top)) {
  418. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  419. copyImageData(intersection.subTarget, intersection.subSource);
  420. }
  421. }
  422. void dsr::imageImpl_drawCopy(ImageU16Impl& target, const ImageU16Impl& source, int32_t left, int32_t top) {
  423. if (ImageIntersection::canCreate(target, source, left, top)) {
  424. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  425. copyImageData(intersection.subTarget, intersection.subSource);
  426. }
  427. }
  428. void dsr::imageImpl_drawCopy(ImageF32Impl& target, const ImageF32Impl& source, int32_t left, int32_t top) {
  429. if (ImageIntersection::canCreate(target, source, left, top)) {
  430. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  431. copyImageData(intersection.subTarget, intersection.subSource);
  432. }
  433. }
  434. void dsr::imageImpl_drawCopy(ImageRgbaU8Impl& target, const ImageU8Impl& source, int32_t left, int32_t top) {
  435. if (ImageIntersection::canCreate(target, source, left, top)) {
  436. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  437. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  438. uint8_t luma = *sourcePixel;
  439. targetPixel[target.packOrder.redIndex] = luma;
  440. targetPixel[target.packOrder.greenIndex] = luma;
  441. targetPixel[target.packOrder.blueIndex] = luma;
  442. targetPixel[target.packOrder.alphaIndex] = 255;
  443. );
  444. }
  445. }
  446. void dsr::imageImpl_drawCopy(ImageRgbaU8Impl& target, const ImageU16Impl& source, int32_t left, int32_t top) {
  447. if (ImageIntersection::canCreate(target, source, left, top)) {
  448. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  449. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  450. int luma = *((const uint16_t*)sourcePixel);
  451. if (luma > 255) { luma = 255; }
  452. targetPixel[target.packOrder.redIndex] = luma;
  453. targetPixel[target.packOrder.greenIndex] = luma;
  454. targetPixel[target.packOrder.blueIndex] = luma;
  455. targetPixel[target.packOrder.alphaIndex] = 255;
  456. );
  457. }
  458. }
  459. void dsr::imageImpl_drawCopy(ImageRgbaU8Impl& target, const ImageF32Impl& source, int32_t left, int32_t top) {
  460. if (ImageIntersection::canCreate(target, source, left, top)) {
  461. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  462. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  463. int luma = saturateFloat(*((const float*)sourcePixel));
  464. targetPixel[target.packOrder.redIndex] = luma;
  465. targetPixel[target.packOrder.greenIndex] = luma;
  466. targetPixel[target.packOrder.blueIndex] = luma;
  467. targetPixel[target.packOrder.alphaIndex] = 255;
  468. );
  469. }
  470. }
  471. void dsr::imageImpl_drawCopy(ImageU8Impl& target, const ImageF32Impl& source, int32_t left, int32_t top) {
  472. if (ImageIntersection::canCreate(target, source, left, top)) {
  473. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  474. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  475. *targetPixel = saturateFloat(*((const float*)sourcePixel));
  476. );
  477. }
  478. }
  479. void dsr::imageImpl_drawCopy(ImageU8Impl& target, const ImageU16Impl& source, int32_t left, int32_t top) {
  480. if (ImageIntersection::canCreate(target, source, left, top)) {
  481. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  482. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  483. int luma = *((const uint16_t*)sourcePixel);
  484. if (luma > 255) { luma = 255; }
  485. *targetPixel = luma;
  486. );
  487. }
  488. }
  489. void dsr::imageImpl_drawCopy(ImageU16Impl& target, const ImageU8Impl& source, int32_t left, int32_t top) {
  490. if (ImageIntersection::canCreate(target, source, left, top)) {
  491. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  492. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  493. *((uint16_t*)targetPixel) = *sourcePixel;
  494. );
  495. }
  496. }
  497. void dsr::imageImpl_drawCopy(ImageU16Impl& target, const ImageF32Impl& source, int32_t left, int32_t top) {
  498. if (ImageIntersection::canCreate(target, source, left, top)) {
  499. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  500. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  501. int luma = *((const float*)sourcePixel);
  502. if (luma < 0) { luma = 0; }
  503. if (luma > 65535) { luma = 65535; }
  504. *((uint16_t*)targetPixel) = *sourcePixel;
  505. );
  506. }
  507. }
  508. void dsr::imageImpl_drawCopy(ImageF32Impl& target, const ImageU8Impl& source, int32_t left, int32_t top) {
  509. if (ImageIntersection::canCreate(target, source, left, top)) {
  510. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  511. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  512. *((float*)targetPixel) = (float)(*sourcePixel);
  513. );
  514. }
  515. }
  516. void dsr::imageImpl_drawCopy(ImageF32Impl& target, const ImageU16Impl& source, int32_t left, int32_t top) {
  517. if (ImageIntersection::canCreate(target, source, left, top)) {
  518. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  519. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  520. int luma = *((const uint16_t*)sourcePixel);
  521. if (luma > 255) { luma = 255; }
  522. *((float*)targetPixel) = (float)luma;
  523. );
  524. }
  525. }
  526. void dsr::imageImpl_drawAlphaFilter(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int32_t left, int32_t top) {
  527. if (ImageIntersection::canCreate(target, source, left, top)) {
  528. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  529. // Read and repack to convert between different color formats
  530. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  531. // Optimized for anti-aliasing, where most alpha values are 0 or 255
  532. uint32_t sourceRatio = sourcePixel[source.packOrder.alphaIndex];
  533. if (sourceRatio > 0) {
  534. if (sourceRatio == 255) {
  535. targetPixel[target.packOrder.redIndex] = sourcePixel[source.packOrder.redIndex];
  536. targetPixel[target.packOrder.greenIndex] = sourcePixel[source.packOrder.greenIndex];
  537. targetPixel[target.packOrder.blueIndex] = sourcePixel[source.packOrder.blueIndex];
  538. targetPixel[target.packOrder.alphaIndex] = 255;
  539. } else {
  540. uint32_t targetRatio = 255 - sourceRatio;
  541. targetPixel[target.packOrder.redIndex] = mulByte_8(targetPixel[target.packOrder.redIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.redIndex], sourceRatio);
  542. targetPixel[target.packOrder.greenIndex] = mulByte_8(targetPixel[target.packOrder.greenIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.greenIndex], sourceRatio);
  543. targetPixel[target.packOrder.blueIndex] = mulByte_8(targetPixel[target.packOrder.blueIndex], targetRatio) + mulByte_8(sourcePixel[source.packOrder.blueIndex], sourceRatio);
  544. targetPixel[target.packOrder.alphaIndex] = mulByte_8(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
  545. }
  546. }
  547. );
  548. }
  549. }
  550. void dsr::imageImpl_drawMaxAlpha(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int32_t left, int32_t top, int32_t sourceAlphaOffset) {
  551. if (ImageIntersection::canCreate(target, source, left, top)) {
  552. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  553. // Read and repack to convert between different color formats
  554. if (sourceAlphaOffset == 0) {
  555. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  556. int sourceAlpha = sourcePixel[source.packOrder.alphaIndex];
  557. if (sourceAlpha > targetPixel[target.packOrder.alphaIndex]) {
  558. targetPixel[target.packOrder.redIndex] = sourcePixel[source.packOrder.redIndex];
  559. targetPixel[target.packOrder.greenIndex] = sourcePixel[source.packOrder.greenIndex];
  560. targetPixel[target.packOrder.blueIndex] = sourcePixel[source.packOrder.blueIndex];
  561. targetPixel[target.packOrder.alphaIndex] = sourceAlpha;
  562. }
  563. );
  564. } else {
  565. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  566. int sourceAlpha = sourcePixel[source.packOrder.alphaIndex];
  567. if (sourceAlpha > 0) {
  568. sourceAlpha += sourceAlphaOffset;
  569. if (sourceAlpha > targetPixel[target.packOrder.alphaIndex]) {
  570. targetPixel[target.packOrder.redIndex] = sourcePixel[source.packOrder.redIndex];
  571. targetPixel[target.packOrder.greenIndex] = sourcePixel[source.packOrder.greenIndex];
  572. targetPixel[target.packOrder.blueIndex] = sourcePixel[source.packOrder.blueIndex];
  573. if (sourceAlpha < 0) { sourceAlpha = 0; }
  574. if (sourceAlpha > 255) { sourceAlpha = 255; }
  575. targetPixel[target.packOrder.alphaIndex] = sourceAlpha;
  576. }
  577. }
  578. );
  579. }
  580. }
  581. }
  582. void dsr::imageImpl_drawAlphaClip(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int32_t left, int32_t top, int32_t threshold) {
  583. if (ImageIntersection::canCreate(target, source, left, top)) {
  584. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  585. // Read and repack to convert between different color formats
  586. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  587. if (sourcePixel[source.packOrder.alphaIndex] > threshold) {
  588. targetPixel[target.packOrder.redIndex] = sourcePixel[source.packOrder.redIndex];
  589. targetPixel[target.packOrder.greenIndex] = sourcePixel[source.packOrder.greenIndex];
  590. targetPixel[target.packOrder.blueIndex] = sourcePixel[source.packOrder.blueIndex];
  591. targetPixel[target.packOrder.alphaIndex] = 255;
  592. }
  593. );
  594. }
  595. }
  596. template <bool FULL_ALPHA>
  597. static void drawSilhouette_template(ImageRgbaU8Impl& target, const ImageU8Impl& source, const ColorRgbaI32& color, int32_t left, int32_t top) {
  598. if (ImageIntersection::canCreate(target, source, left, top)) {
  599. ImageIntersection intersection = ImageIntersection::create(target, source, left, top);
  600. // Read and repack to convert between different color formats
  601. ITERATE_PIXELS(intersection.subTarget, intersection.subSource,
  602. uint32_t sourceRatio;
  603. if (FULL_ALPHA) {
  604. sourceRatio = *sourcePixel;
  605. } else {
  606. sourceRatio = mulByte_8(*sourcePixel, color.alpha);
  607. }
  608. if (sourceRatio > 0) {
  609. if (sourceRatio == 255) {
  610. targetPixel[target.packOrder.redIndex] = color.red;
  611. targetPixel[target.packOrder.greenIndex] = color.green;
  612. targetPixel[target.packOrder.blueIndex] = color.blue;
  613. targetPixel[target.packOrder.alphaIndex] = 255;
  614. } else {
  615. uint32_t targetRatio = 255 - sourceRatio;
  616. targetPixel[target.packOrder.redIndex] = mulByte_8(targetPixel[target.packOrder.redIndex], targetRatio) + mulByte_8(color.red, sourceRatio);
  617. targetPixel[target.packOrder.greenIndex] = mulByte_8(targetPixel[target.packOrder.greenIndex], targetRatio) + mulByte_8(color.green, sourceRatio);
  618. targetPixel[target.packOrder.blueIndex] = mulByte_8(targetPixel[target.packOrder.blueIndex], targetRatio) + mulByte_8(color.blue, sourceRatio);
  619. targetPixel[target.packOrder.alphaIndex] = mulByte_8(targetPixel[target.packOrder.alphaIndex], targetRatio) + sourceRatio;
  620. }
  621. }
  622. );
  623. }
  624. }
  625. void dsr::imageImpl_drawSilhouette(ImageRgbaU8Impl& target, const ImageU8Impl& source, const ColorRgbaI32& color, int32_t left, int32_t top) {
  626. if (color.alpha > 0) {
  627. ColorRgbaI32 saturatedColor = color.saturate();
  628. if (color.alpha < 255) {
  629. drawSilhouette_template<false>(target, source, saturatedColor, left, top);
  630. } else {
  631. drawSilhouette_template<true>(target, source, saturatedColor, left, top);
  632. }
  633. }
  634. }
  635. void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& sourceHeight, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  636. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  637. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  638. ITERATE_PIXELS(intersectionH.subTarget, intersectionH.subSource,
  639. int32_t sourceHeight = *((const uint16_t*)sourcePixel);
  640. if (sourceHeight > 0) {
  641. sourceHeight += sourceHeightOffset;
  642. int32_t targetHeight = *((uint16_t*)targetPixel);
  643. if (sourceHeight < 0) { sourceHeight = 0; }
  644. if (sourceHeight > 65535) { sourceHeight = 65535; }
  645. if (sourceHeight > 0 && sourceHeight > targetHeight) {
  646. *((uint16_t*)targetPixel) = sourceHeight;
  647. }
  648. }
  649. );
  650. }
  651. }
  652. void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  653. int32_t left, int32_t top, int32_t sourceHeightOffset) {
  654. assert(sourceA.width == sourceHeight.width);
  655. assert(sourceA.height == sourceHeight.height);
  656. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  657. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  658. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  659. ITERATE_PIXELS_2(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource,
  660. int32_t sourceHeight = *((const uint16_t*)sourcePixel1);
  661. if (sourceHeight > 0) {
  662. sourceHeight += sourceHeightOffset;
  663. int32_t targetHeight = *((uint16_t*)targetPixel1);
  664. if (sourceHeight < 0) { sourceHeight = 0; }
  665. if (sourceHeight > 65535) { sourceHeight = 65535; }
  666. if (sourceHeight > targetHeight) {
  667. *((uint16_t*)targetPixel1) = sourceHeight;
  668. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  669. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  670. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  671. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  672. }
  673. }
  674. );
  675. }
  676. }
  677. void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  678. ImageRgbaU8Impl& targetB, const ImageRgbaU8Impl& sourceB, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  679. assert(sourceA.width == sourceHeight.width);
  680. assert(sourceA.height == sourceHeight.height);
  681. assert(sourceB.width == sourceHeight.width);
  682. assert(sourceB.height == sourceHeight.height);
  683. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  684. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  685. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  686. ImageIntersection intersectionB = ImageIntersection::create(targetB, sourceB, left, top);
  687. ITERATE_PIXELS_3(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource, intersectionB.subTarget, intersectionB.subSource,
  688. int32_t sourceHeight = *((const uint16_t*)sourcePixel1);
  689. if (sourceHeight > 0) {
  690. sourceHeight += sourceHeightOffset;
  691. int32_t targetHeight = *((uint16_t*)targetPixel1);
  692. if (sourceHeight < 0) { sourceHeight = 0; }
  693. if (sourceHeight > 65535) { sourceHeight = 65535; }
  694. if (sourceHeight > targetHeight) {
  695. *((uint16_t*)targetPixel1) = sourceHeight;
  696. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  697. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  698. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  699. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  700. targetPixel3[targetB.packOrder.redIndex] = sourcePixel3[sourceB.packOrder.redIndex];
  701. targetPixel3[targetB.packOrder.greenIndex] = sourcePixel3[sourceB.packOrder.greenIndex];
  702. targetPixel3[targetB.packOrder.blueIndex] = sourcePixel3[sourceB.packOrder.blueIndex];
  703. targetPixel3[targetB.packOrder.alphaIndex] = sourcePixel3[sourceB.packOrder.alphaIndex];
  704. }
  705. }
  706. );
  707. }
  708. }
  709. void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, int32_t left, int32_t top, float sourceHeightOffset) {
  710. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  711. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  712. ITERATE_PIXELS(intersectionH.subTarget, intersectionH.subSource,
  713. float sourceHeight = *((const float*)sourcePixel);
  714. if (sourceHeight > -std::numeric_limits<float>::infinity()) {
  715. sourceHeight += sourceHeightOffset;
  716. float targetHeight = *((float*)targetPixel);
  717. if (sourceHeight > targetHeight) {
  718. *((float*)targetPixel) = sourceHeight;
  719. }
  720. }
  721. );
  722. }
  723. }
  724. void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  725. int32_t left, int32_t top, float sourceHeightOffset) {
  726. assert(sourceA.width == sourceHeight.width);
  727. assert(sourceA.height == sourceHeight.height);
  728. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  729. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  730. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  731. ITERATE_PIXELS_2(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource,
  732. float sourceHeight = *((const float*)sourcePixel1);
  733. if (sourceHeight > -std::numeric_limits<float>::infinity()) {
  734. sourceHeight += sourceHeightOffset;
  735. float targetHeight = *((float*)targetPixel1);
  736. if (sourceHeight > targetHeight) {
  737. *((float*)targetPixel1) = sourceHeight;
  738. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  739. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  740. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  741. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  742. }
  743. }
  744. );
  745. }
  746. }
  747. void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  748. ImageRgbaU8Impl& targetB, const ImageRgbaU8Impl& sourceB, int32_t left, int32_t top, float sourceHeightOffset) {
  749. assert(sourceA.width == sourceHeight.width);
  750. assert(sourceA.height == sourceHeight.height);
  751. assert(sourceB.width == sourceHeight.width);
  752. assert(sourceB.height == sourceHeight.height);
  753. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  754. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  755. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  756. ImageIntersection intersectionB = ImageIntersection::create(targetB, sourceB, left, top);
  757. ITERATE_PIXELS_3(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource, intersectionB.subTarget, intersectionB.subSource,
  758. float sourceHeight = *((const float*)sourcePixel1);
  759. if (sourceHeight > -std::numeric_limits<float>::infinity()) {
  760. sourceHeight += sourceHeightOffset;
  761. float targetHeight = *((float*)targetPixel1);
  762. if (sourceHeight > targetHeight) {
  763. *((float*)targetPixel1) = sourceHeight;
  764. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  765. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  766. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  767. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  768. targetPixel3[targetB.packOrder.redIndex] = sourcePixel3[sourceB.packOrder.redIndex];
  769. targetPixel3[targetB.packOrder.greenIndex] = sourcePixel3[sourceB.packOrder.greenIndex];
  770. targetPixel3[targetB.packOrder.blueIndex] = sourcePixel3[sourceB.packOrder.blueIndex];
  771. targetPixel3[targetB.packOrder.alphaIndex] = sourcePixel3[sourceB.packOrder.alphaIndex];
  772. }
  773. }
  774. );
  775. }
  776. }
  777. /*
  778. void imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0);
  779. void imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  780. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0);
  781. void imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  782. ImageRgbaU8Impl& targetB, const ImageRgbaU8Impl& sourceB, int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0);
  783. */
  784. // -------------------------------- Resize --------------------------------
  785. static inline U32x4 ColorRgbaI32_to_U32x4(const ColorRgbaI32& color) {
  786. return U32x4(color.red, color.green, color.blue, color.alpha);
  787. }
  788. static inline ColorRgbaI32 U32x4_to_ColorRgbaI32(const U32x4& color) {
  789. UVector4D vResult = color.get();
  790. return ColorRgbaI32(vResult.x, vResult.y, vResult.z, vResult.w);
  791. }
  792. // Uniform linear interpolation of colors from a 16-bit sub-pixel weight
  793. // Pre-condition0 <= fineRatio <= 65536
  794. // Post-condition: Returns colorA * (1 - (fineRatio / 65536)) + colorB * (fineRatio / 65536)
  795. static inline U32x4 mixColorsUniform(const U32x4 &colorA, const U32x4 &colorB, uint32_t fineRatio) {
  796. uint16_t ratio = fineRatio >> 8;
  797. uint16_t invRatio = 256 - ratio;
  798. ALIGN16 U16x8 weightA = U16x8(invRatio);
  799. ALIGN16 U16x8 weightB = U16x8(ratio);
  800. ALIGN16 U32x4 lowMask(0x00FF00FFu);
  801. ALIGN16 U16x8 lowColorA = U16x8(colorA & lowMask);
  802. ALIGN16 U16x8 lowColorB = U16x8(colorB & lowMask);
  803. ALIGN16 U32x4 highMask(0xFF00FF00u);
  804. ALIGN16 U16x8 highColorA = U16x8((colorA & highMask) >> 8);
  805. ALIGN16 U16x8 highColorB = U16x8((colorB & highMask) >> 8);
  806. ALIGN16 U32x4 lowColor = (((lowColorA * weightA) + (lowColorB * weightB))).get_U32();
  807. ALIGN16 U32x4 highColor = (((highColorA * weightA) + (highColorB * weightB))).get_U32();
  808. return (((lowColor >> 8) & lowMask) | (highColor & highMask));
  809. }
  810. #define READ_CLAMP(X,Y) ImageRgbaU8Impl::unpackRgba(ImageRgbaU8Impl::readPixel_clamp(source, X, Y), source.packOrder)
  811. #define READ_CLAMP_SIMD(X,Y) ColorRgbaI32_to_U32x4(READ_CLAMP(X,Y))
  812. // Fixed-precision decimal system with 16-bit indices and 16-bit sub-pixel weights
  813. static const uint32_t interpolationFullPixel = 65536;
  814. static const uint32_t interpolationHalfPixel = interpolationFullPixel / 2;
  815. // Modulo mask for values greater than or equal to 0 and lesser than interpolationFullPixel
  816. static const uint32_t interpolationWeightMask = interpolationFullPixel - 1;
  817. // BILINEAR: Enables linear interpolation
  818. // scaleRegion:
  819. // The stretched location of the source image in the target image
  820. // Making it smaller than the target image will fill the outside with stretched pixels
  821. // Allowing the caller to crop away parts of the source image that aren't interesting
  822. // Can be used to round the region to a multiple of the input size for a fixed pixel size
  823. template <bool BILINEAR>
  824. static void resize_reference(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, const IRect& scaleRegion) {
  825. // Reference implementation
  826. // Offset in source pixels per target pixel
  827. int32_t offsetX = interpolationFullPixel * source.width / scaleRegion.width();
  828. int32_t offsetY = interpolationFullPixel * source.height / scaleRegion.height();
  829. int32_t startX = interpolationFullPixel * scaleRegion.left() + offsetX / 2;
  830. int32_t startY = interpolationFullPixel * scaleRegion.top() + offsetY / 2;
  831. if (BILINEAR) {
  832. startX -= interpolationHalfPixel;
  833. startY -= interpolationHalfPixel;
  834. }
  835. SafePointer<uint32_t> targetRow = imageInternal::getSafeData<uint32_t>(target);
  836. int32_t readY = startY;
  837. for (int32_t y = 0; y < target.height; y++) {
  838. int32_t naturalY = readY;
  839. if (naturalY < 0) { naturalY = 0; }
  840. uint32_t sampleY = (uint32_t)naturalY;
  841. uint32_t upperY = sampleY >> 16;
  842. uint32_t lowerY = upperY + 1;
  843. uint32_t lowerRatio = sampleY & interpolationWeightMask;
  844. uint32_t upperRatio = 65536 - lowerRatio;
  845. SafePointer<uint32_t> targetPixel = targetRow;
  846. int32_t readX = startX;
  847. for (int32_t x = 0; x < target.width; x++) {
  848. int32_t naturalX = readX;
  849. if (naturalX < 0) { naturalX = 0; }
  850. uint32_t sampleX = (uint32_t)naturalX;
  851. uint32_t leftX = sampleX >> 16;
  852. uint32_t rightX = leftX + 1;
  853. uint32_t rightRatio = sampleX & interpolationWeightMask;
  854. uint32_t leftRatio = 65536 - rightRatio;
  855. ColorRgbaI32 finalColor;
  856. if (BILINEAR) {
  857. ALIGN16 U32x4 vUpperLeftColor = READ_CLAMP_SIMD(leftX, upperY);
  858. ALIGN16 U32x4 vUpperRightColor = READ_CLAMP_SIMD(rightX, upperY);
  859. ALIGN16 U32x4 vLowerLeftColor = READ_CLAMP_SIMD(leftX, lowerY);
  860. ALIGN16 U32x4 vLowerRightColor = READ_CLAMP_SIMD(rightX, lowerY);
  861. ALIGN16 U32x4 vLeftRatio = U32x4(leftRatio);
  862. ALIGN16 U32x4 vRightRatio = U32x4(rightRatio);
  863. ALIGN16 U32x4 vUpperColor = ((vUpperLeftColor * vLeftRatio) + (vUpperRightColor * vRightRatio)) >> 16;
  864. ALIGN16 U32x4 vLowerColor = ((vLowerLeftColor * vLeftRatio) + (vLowerRightColor * vRightRatio)) >> 16;
  865. ALIGN16 U32x4 vCenterColor = ((vUpperColor * upperRatio) + (vLowerColor * lowerRatio)) >> 16;
  866. finalColor = U32x4_to_ColorRgbaI32(vCenterColor);
  867. } else {
  868. finalColor = READ_CLAMP(leftX, upperY);
  869. }
  870. *targetPixel = target.packRgba(finalColor).packed;
  871. targetPixel += 1;
  872. readX += offsetX;
  873. }
  874. targetRow.increaseBytes(target.stride);
  875. readY += offsetY;
  876. }
  877. }
  878. // BILINEAR: Enables linear interpolation
  879. // SIMD_ALIGNED: Each line starts 16-byte aligned, has a stride divisible with 16-bytes and is allowed to overwrite padding.
  880. template <bool BILINEAR, bool SIMD_ALIGNED>
  881. static void resize_optimized(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, const IRect& scaleRegion) {
  882. // Get source information
  883. // Compare dimensions
  884. const bool sameWidth = source.width == scaleRegion.width() && scaleRegion.left() == 0;
  885. const bool sameHeight = source.height == scaleRegion.height() && scaleRegion.top() == 0;
  886. const bool samePackOrder = target.packOrder.packOrderIndex == source.packOrder.packOrderIndex;
  887. if (sameWidth && sameHeight) {
  888. // No need to resize, just make a copy to save time
  889. imageImpl_drawCopy(target, source);
  890. } else if (sameWidth && (samePackOrder || BILINEAR)) {
  891. // Only vertical interpolation
  892. // Offset in source pixels per target pixel
  893. int32_t offsetY = interpolationFullPixel * source.height / scaleRegion.height();
  894. int32_t startY = interpolationFullPixel * scaleRegion.top() + offsetY / 2;
  895. if (BILINEAR) {
  896. startY -= interpolationHalfPixel;
  897. }
  898. SafePointer<uint32_t> targetRow = imageInternal::getSafeData<uint32_t>(target);
  899. int32_t readY = startY;
  900. for (int32_t y = 0; y < target.height; y++) {
  901. int32_t naturalY = readY;
  902. if (naturalY < 0) { naturalY = 0; }
  903. uint32_t sampleY = (uint32_t)naturalY;
  904. uint32_t upperY = sampleY >> 16;
  905. uint32_t lowerY = upperY + 1;
  906. if (upperY >= (uint32_t)source.height) upperY = source.height - 1;
  907. if (lowerY >= (uint32_t)source.height) lowerY = source.height - 1;
  908. if (BILINEAR) {
  909. uint32_t lowerRatio = sampleY & interpolationWeightMask;
  910. uint32_t upperRatio = 65536 - lowerRatio;
  911. SafePointer<uint32_t> targetPixel = targetRow;
  912. if (SIMD_ALIGNED) {
  913. const SafePointer<uint32_t> sourceRowUpper = imageInternal::getSafeData<uint32_t>(source, upperY);
  914. const SafePointer<uint32_t> sourceRowLower = imageInternal::getSafeData<uint32_t>(source, lowerY);
  915. for (int32_t x = 0; x < target.width; x += 4) {
  916. ALIGN16 U32x4 vUpperPackedColor = U32x4::readAligned(sourceRowUpper, "resize_optimized @ read vUpperPackedColor");
  917. ALIGN16 U32x4 vLowerPackedColor = U32x4::readAligned(sourceRowLower, "resize_optimized @ read vLowerPackedColor");
  918. ALIGN16 U32x4 vCenterColor = mixColorsUniform(vUpperPackedColor, vLowerPackedColor, lowerRatio);
  919. vCenterColor.writeAligned(targetPixel, "resize_optimized @ write vCenterColor");
  920. sourceRowUpper += 4;
  921. sourceRowLower += 4;
  922. targetPixel += 4;
  923. }
  924. } else {
  925. for (int32_t x = 0; x < target.width; x++) {
  926. ALIGN16 U32x4 vUpperColor = READ_CLAMP_SIMD(x, upperY);
  927. ALIGN16 U32x4 vLowerColor = READ_CLAMP_SIMD(x, lowerY);
  928. ALIGN16 U32x4 vCenterColor = ((vUpperColor * upperRatio) + (vLowerColor * lowerRatio)) >> 16;
  929. ColorRgbaI32 finalColor = U32x4_to_ColorRgbaI32(vCenterColor);
  930. *targetPixel = target.packRgba(finalColor).packed;
  931. targetPixel += 1;
  932. }
  933. }
  934. } else {
  935. const SafePointer<uint32_t> sourceRowUpper = imageInternal::getSafeData<uint32_t>(source, upperY);
  936. // Nearest neighbor sampling from a same width can be done using one copy per row
  937. safeMemoryCopy(targetRow, sourceRowUpper, source.width * 4);
  938. }
  939. targetRow.increaseBytes(target.stride);
  940. readY += offsetY;
  941. }
  942. } else if (sameHeight) {
  943. // Only horizontal interpolation
  944. // Offset in source pixels per target pixel
  945. int32_t offsetX = interpolationFullPixel * source.width / scaleRegion.width();
  946. int32_t startX = interpolationFullPixel * scaleRegion.left() + offsetX / 2;
  947. if (BILINEAR) {
  948. startX -= interpolationHalfPixel;
  949. }
  950. SafePointer<uint32_t> targetRow = imageInternal::getSafeData<uint32_t>(target);
  951. for (int32_t y = 0; y < target.height; y++) {
  952. SafePointer<uint32_t> targetPixel = targetRow;
  953. int32_t readX = startX;
  954. for (int32_t x = 0; x < target.width; x++) {
  955. int32_t naturalX = readX;
  956. if (naturalX < 0) { naturalX = 0; }
  957. uint32_t sampleX = (uint32_t)naturalX;
  958. uint32_t leftX = sampleX >> 16;
  959. uint32_t rightX = leftX + 1;
  960. uint32_t rightRatio = sampleX & interpolationWeightMask;
  961. uint32_t leftRatio = 65536 - rightRatio;
  962. ColorRgbaI32 finalColor;
  963. if (BILINEAR) {
  964. ALIGN16 U32x4 vLeftColor = READ_CLAMP_SIMD(leftX, y);
  965. ALIGN16 U32x4 vRightColor = READ_CLAMP_SIMD(rightX, y);
  966. ALIGN16 U32x4 vCenterColor = ((vLeftColor * leftRatio) + (vRightColor * rightRatio)) >> 16;
  967. finalColor = U32x4_to_ColorRgbaI32(vCenterColor);
  968. } else {
  969. finalColor = READ_CLAMP(leftX, y);
  970. }
  971. *targetPixel = target.packRgba(finalColor).packed;
  972. targetPixel += 1;
  973. readX += offsetX;
  974. }
  975. targetRow.increaseBytes(target.stride);
  976. }
  977. } else {
  978. // Call the reference implementation
  979. resize_reference<BILINEAR>(target, source, scaleRegion);
  980. }
  981. }
  982. // Returns true iff each line start in image is aligned with 16 bytes
  983. // Often not the case for sub-images, even if the parent image is aligned
  984. static bool imageIs16ByteAligned(const ImageImpl& image) {
  985. return (uint32_t)((image.stride & 15) == 0 && ((uintptr_t)(imageInternal::getSafeData<uint8_t>(image).getUnsafe()) & 15) == 0);
  986. }
  987. // Converting run-time flags into compile-time constants
  988. static void resize_aux(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, bool interpolate, bool paddWrite, const IRect& scaleRegion) {
  989. // If writing to padding is allowed and both images are 16-byte aligned with the same pack order
  990. if (paddWrite && imageIs16ByteAligned(source) && imageIs16ByteAligned(target)) {
  991. // Optimized resize allowed
  992. if (interpolate) {
  993. resize_optimized<true, true>(target, source, scaleRegion);
  994. } else {
  995. resize_optimized<false, true>(target, source, scaleRegion);
  996. }
  997. } else {
  998. // Non-optimized resize
  999. if (interpolate) {
  1000. resize_optimized<true, false>(target, source, scaleRegion);
  1001. } else {
  1002. resize_optimized<false, false>(target, source, scaleRegion);
  1003. }
  1004. }
  1005. }
  1006. void dsr::imageImpl_resizeInPlace(ImageRgbaU8Impl& target, ImageRgbaU8Impl* wideTempImage, const ImageRgbaU8Impl& source, bool interpolate, const IRect& scaleRegion) {
  1007. if (target.width != source.width && target.height > source.height) {
  1008. // Upscaling is faster in two steps by both reusing the horizontal interpolation and vectorizing the vertical interpolation.
  1009. int tempWidth = target.width;
  1010. int tempHeight = source.height;
  1011. PackOrderIndex tempPackOrder = target.packOrder.packOrderIndex;
  1012. IRect tempScaleRegion = IRect(scaleRegion.left(), 0, scaleRegion.width(), source.height);
  1013. if (wideTempImage == nullptr
  1014. || wideTempImage->width != tempWidth
  1015. || wideTempImage->height != tempHeight
  1016. || wideTempImage->packOrder.packOrderIndex != tempPackOrder) {
  1017. // Performance warnings
  1018. // TODO: Make optional
  1019. if (wideTempImage != nullptr) {
  1020. if (wideTempImage->width != tempWidth) { printText("Ignored temp buffer of wrong width! Found ", wideTempImage->width, " instead of ", tempWidth, "\n"); }
  1021. if (wideTempImage->height != tempHeight) { printText("Ignored temp buffer of wrong height! Found ", wideTempImage->height, " instead of ", tempHeight, "\n"); }
  1022. if (wideTempImage->packOrder.packOrderIndex != tempPackOrder) { printText("Ignored temp buffer of wrong pack order!\n"); }
  1023. }
  1024. // Create a new buffer
  1025. ImageRgbaU8Impl newTempImage = ImageRgbaU8Impl(tempWidth, tempHeight, tempPackOrder);
  1026. resize_aux(newTempImage, source, interpolate, true, tempScaleRegion);
  1027. resize_aux(target, newTempImage, interpolate, true, scaleRegion);
  1028. } else {
  1029. // Use existing buffer
  1030. resize_aux(*wideTempImage, source, interpolate, true, tempScaleRegion);
  1031. resize_aux(target, *wideTempImage, interpolate, true, scaleRegion);
  1032. }
  1033. } else {
  1034. // Downscaling or only changing one dimension is faster in one step
  1035. resize_aux(target, source, interpolate, true, scaleRegion);
  1036. }
  1037. }
  1038. void dsr::imageImpl_resizeToTarget(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, bool interpolate) {
  1039. imageImpl_resizeInPlace(target, nullptr, source, interpolate, imageInternal::getBound(target));
  1040. }
  1041. template <bool CONVERT_COLOR>
  1042. static inline Color4xU8 convertRead(const ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int x, int y) {
  1043. Color4xU8 result = ImageRgbaU8Impl::readPixel_clamp(source, x, y);
  1044. if (CONVERT_COLOR) {
  1045. result = target.packRgba(ImageRgbaU8Impl::unpackRgba(result, source.packOrder));
  1046. }
  1047. return result;
  1048. }
  1049. // Used for drawing large pixels
  1050. static inline void fillRectangle(ImageRgbaU8Impl& target, int pixelLeft, int pixelRight, int pixelTop, int pixelBottom, const Color4xU8& packedColor) {
  1051. // TODO: Get target pointer in advance and add the correct offsets
  1052. SafePointer<Color4xU8> targetRow = imageInternal::getSafeData<Color4xU8>(target, pixelTop) + pixelLeft;
  1053. for (int y = pixelTop; y < pixelBottom; y++) {
  1054. SafePointer<Color4xU8> targetPixel = targetRow;
  1055. for (int x = pixelLeft; x < pixelRight; x++) {
  1056. *targetPixel = packedColor;
  1057. targetPixel += 1;
  1058. }
  1059. targetRow.increaseBytes(target.stride);
  1060. }
  1061. }
  1062. template <bool CONVERT_COLOR>
  1063. static void blockMagnify_reference(
  1064. ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source,
  1065. int pixelWidth, int pixelHeight, int clipWidth, int clipHeight) {
  1066. int sourceY = 0;
  1067. int maxSourceX = source.width - 1;
  1068. int maxSourceY = source.height - 1;
  1069. if (clipWidth > target.width) { clipWidth = target.width; }
  1070. if (clipHeight > target.height) { clipHeight = target.height; }
  1071. for (int32_t pixelTop = 0; pixelTop < clipHeight; pixelTop += pixelHeight) {
  1072. int sourceX = 0;
  1073. for (int32_t pixelLeft = 0; pixelLeft < clipWidth; pixelLeft += pixelWidth) {
  1074. // Read the pixel once
  1075. Color4xU8 sourceColor = convertRead<CONVERT_COLOR>(target, source, sourceX, sourceY);
  1076. // Write to all target pixels in a conditionless loop
  1077. fillRectangle(target, pixelLeft, pixelLeft + pixelWidth, pixelTop, pixelTop + pixelHeight, sourceColor);
  1078. // Iterate and clamp the read coordinate
  1079. sourceX++;
  1080. if (sourceX > maxSourceX) { sourceX = maxSourceX; }
  1081. }
  1082. // Iterate and clamp the read coordinate
  1083. sourceY++;
  1084. if (sourceY > maxSourceY) { sourceY = maxSourceY; }
  1085. }
  1086. }
  1087. // Pre-condition:
  1088. // * The source and target images have the same pack order
  1089. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1090. // * clipWidth % 2 == 0
  1091. // * clipHeight % 2 == 0
  1092. static void blockMagnify_2x2(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1093. #ifdef USE_SIMD_EXTRA
  1094. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1095. SafePointer<uint32_t> upperTargetRow = imageInternal::getSafeData<uint32_t>(target, 0);
  1096. SafePointer<uint32_t> lowerTargetRow = imageInternal::getSafeData<uint32_t>(target, 1);
  1097. int doubleTargetStride = target.stride * 2;
  1098. for (int upperTargetY = 0; upperTargetY + 2 <= clipHeight; upperTargetY+=2) {
  1099. // Carriage return
  1100. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1101. SafePointer<uint32_t> upperTargetPixel = upperTargetRow;
  1102. SafePointer<uint32_t> lowerTargetPixel = lowerTargetRow;
  1103. // Write to whole multiples of 8 pixels
  1104. int writeLeftX = 0;
  1105. while (writeLeftX + 8 <= clipWidth) {
  1106. // Read pixels
  1107. ALIGN16 SIMD_U32x4 sourcePixels = U32x4::readAligned(sourcePixel, "blockMagnify_2x2 @ whole sourcePixels").v;
  1108. sourcePixel += 4;
  1109. // Double the pixels by zipping with itself
  1110. ALIGN16 SIMD_U32x4x2 doubledPixels = ZIP_U32_SIMD(sourcePixels, sourcePixels);
  1111. // Write lower part
  1112. U32x4(doubledPixels.val[0]).writeAligned(upperTargetPixel, "blockMagnify_2x2 @ write upper left #1");
  1113. upperTargetPixel += 4;
  1114. U32x4(doubledPixels.val[0]).writeAligned(lowerTargetPixel, "blockMagnify_2x2 @ write lower left #1");
  1115. lowerTargetPixel += 4;
  1116. // Write upper part
  1117. U32x4(doubledPixels.val[1]).writeAligned(upperTargetPixel, "blockMagnify_2x2 @ write upper right #1");
  1118. upperTargetPixel += 4;
  1119. U32x4(doubledPixels.val[1]).writeAligned(lowerTargetPixel, "blockMagnify_2x2 @ write lower right #1");
  1120. lowerTargetPixel += 4;
  1121. // Count
  1122. writeLeftX += 8;
  1123. }
  1124. // Fill the last pixels using scalar operations to avoid going out of bound
  1125. while (writeLeftX + 2 <= clipWidth) {
  1126. // Read one pixel
  1127. uint32_t sourceColor = *sourcePixel;
  1128. // Write 2x2 pixels
  1129. *upperTargetPixel = sourceColor; upperTargetPixel += 1;
  1130. *upperTargetPixel = sourceColor; upperTargetPixel += 1;
  1131. *lowerTargetPixel = sourceColor; lowerTargetPixel += 1;
  1132. *lowerTargetPixel = sourceColor; lowerTargetPixel += 1;
  1133. // Count
  1134. writeLeftX += 2;
  1135. }
  1136. // Line feed
  1137. sourceRow.increaseBytes(source.stride);
  1138. upperTargetRow.increaseBytes(doubleTargetStride);
  1139. lowerTargetRow.increaseBytes(doubleTargetStride);
  1140. }
  1141. #else
  1142. blockMagnify_reference<false>(target, source, 2, 2, clipWidth, clipHeight);
  1143. #endif
  1144. }
  1145. static void blackEdges(ImageRgbaU8Impl& target, int excludedWidth, int excludedHeight) {
  1146. // Right side
  1147. drawSolidRectangleMemset<Color4xU8>(target, excludedWidth, 0, target.width, excludedHeight, 0);
  1148. // Bottom and corner
  1149. drawSolidRectangleMemset<Color4xU8>(target, 0, excludedHeight, target.width, target.height, 0);
  1150. }
  1151. void dsr::imageImpl_blockMagnify(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int pixelWidth, int pixelHeight) {
  1152. if (pixelWidth < 1) { pixelWidth = 1; }
  1153. if (pixelHeight < 1) { pixelHeight = 1; }
  1154. bool sameOrder = target.packOrder.packOrderIndex == source.packOrder.packOrderIndex;
  1155. // Find the part of source which fits into target with whole pixels
  1156. int clipWidth = roundDown(std::min(target.width, source.width * pixelWidth), pixelWidth);
  1157. int clipHeight = roundDown(std::min(target.height, source.height * pixelHeight), pixelHeight);
  1158. if (sameOrder) {
  1159. if (imageIs16ByteAligned(source) && imageIs16ByteAligned(target) && pixelWidth == 2 && pixelHeight == 2) {
  1160. blockMagnify_2x2(target, source, clipWidth, clipHeight);
  1161. } else {
  1162. blockMagnify_reference<false>(target, source, pixelWidth, pixelHeight, clipWidth, clipHeight);
  1163. }
  1164. } else {
  1165. blockMagnify_reference<true>(target, source, pixelWidth, pixelHeight, clipWidth, clipHeight);
  1166. }
  1167. blackEdges(target, clipWidth, clipHeight);
  1168. }