draw.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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 newHeight = *((const uint16_t*)sourcePixel);
  640. if (newHeight > 0) {
  641. newHeight += sourceHeightOffset;
  642. if (newHeight < 0) { newHeight = 0; }
  643. if (newHeight > 65535) { newHeight = 65535; }
  644. if (newHeight > 0 && newHeight > *((uint16_t*)targetPixel)) {
  645. *((uint16_t*)targetPixel) = newHeight;
  646. }
  647. }
  648. );
  649. }
  650. }
  651. void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  652. int32_t left, int32_t top, int32_t sourceHeightOffset) {
  653. assert(sourceA.width == sourceHeight.width);
  654. assert(sourceA.height == sourceHeight.height);
  655. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  656. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  657. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  658. ITERATE_PIXELS_2(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource,
  659. int32_t newHeight = *((const uint16_t*)sourcePixel1);
  660. if (newHeight > 0) {
  661. newHeight += sourceHeightOffset;
  662. if (newHeight < 0) { newHeight = 0; }
  663. if (newHeight > 65535) { newHeight = 65535; }
  664. if (newHeight > *((uint16_t*)targetPixel1)) {
  665. *((uint16_t*)targetPixel1) = newHeight;
  666. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  667. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  668. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  669. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  670. }
  671. }
  672. );
  673. }
  674. }
  675. void dsr::imageImpl_drawHigher(ImageU16Impl& targetHeight, const ImageU16Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  676. ImageRgbaU8Impl& targetB, const ImageRgbaU8Impl& sourceB, int32_t left, int32_t top, int32_t sourceHeightOffset) {
  677. assert(sourceA.width == sourceHeight.width);
  678. assert(sourceA.height == sourceHeight.height);
  679. assert(sourceB.width == sourceHeight.width);
  680. assert(sourceB.height == sourceHeight.height);
  681. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  682. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  683. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  684. ImageIntersection intersectionB = ImageIntersection::create(targetB, sourceB, left, top);
  685. ITERATE_PIXELS_3(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource, intersectionB.subTarget, intersectionB.subSource,
  686. int32_t newHeight = *((const uint16_t*)sourcePixel1);
  687. if (newHeight > 0) {
  688. newHeight += sourceHeightOffset;
  689. if (newHeight < 0) { newHeight = 0; }
  690. if (newHeight > 65535) { newHeight = 65535; }
  691. if (newHeight > *((uint16_t*)targetPixel1)) {
  692. *((uint16_t*)targetPixel1) = newHeight;
  693. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  694. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  695. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  696. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  697. targetPixel3[targetB.packOrder.redIndex] = sourcePixel3[sourceB.packOrder.redIndex];
  698. targetPixel3[targetB.packOrder.greenIndex] = sourcePixel3[sourceB.packOrder.greenIndex];
  699. targetPixel3[targetB.packOrder.blueIndex] = sourcePixel3[sourceB.packOrder.blueIndex];
  700. targetPixel3[targetB.packOrder.alphaIndex] = sourcePixel3[sourceB.packOrder.alphaIndex];
  701. }
  702. }
  703. );
  704. }
  705. }
  706. void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, int32_t left, int32_t top, float sourceHeightOffset) {
  707. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  708. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  709. ITERATE_PIXELS(intersectionH.subTarget, intersectionH.subSource,
  710. float newHeight = *((const float*)sourcePixel);
  711. if (newHeight > -std::numeric_limits<float>::infinity()) {
  712. newHeight += sourceHeightOffset;
  713. if (newHeight > *((float*)targetPixel)) {
  714. *((float*)targetPixel) = newHeight;
  715. }
  716. }
  717. );
  718. }
  719. }
  720. void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  721. int32_t left, int32_t top, float sourceHeightOffset) {
  722. assert(sourceA.width == sourceHeight.width);
  723. assert(sourceA.height == sourceHeight.height);
  724. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  725. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  726. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  727. ITERATE_PIXELS_2(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource,
  728. float newHeight = *((const float*)sourcePixel1);
  729. if (newHeight > -std::numeric_limits<float>::infinity()) {
  730. newHeight += sourceHeightOffset;
  731. if (newHeight > *((float*)targetPixel1)) {
  732. *((float*)targetPixel1) = newHeight;
  733. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  734. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  735. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  736. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  737. }
  738. }
  739. );
  740. }
  741. }
  742. void dsr::imageImpl_drawHigher(ImageF32Impl& targetHeight, const ImageF32Impl& sourceHeight, ImageRgbaU8Impl& targetA, const ImageRgbaU8Impl& sourceA,
  743. ImageRgbaU8Impl& targetB, const ImageRgbaU8Impl& sourceB, int32_t left, int32_t top, float sourceHeightOffset) {
  744. assert(sourceA.width == sourceHeight.width);
  745. assert(sourceA.height == sourceHeight.height);
  746. assert(sourceB.width == sourceHeight.width);
  747. assert(sourceB.height == sourceHeight.height);
  748. if (ImageIntersection::canCreate(targetHeight, sourceHeight, left, top)) {
  749. ImageIntersection intersectionH = ImageIntersection::create(targetHeight, sourceHeight, left, top);
  750. ImageIntersection intersectionA = ImageIntersection::create(targetA, sourceA, left, top);
  751. ImageIntersection intersectionB = ImageIntersection::create(targetB, sourceB, left, top);
  752. ITERATE_PIXELS_3(intersectionH.subTarget, intersectionH.subSource, intersectionA.subTarget, intersectionA.subSource, intersectionB.subTarget, intersectionB.subSource,
  753. float newHeight = *((const float*)sourcePixel1);
  754. if (newHeight > -std::numeric_limits<float>::infinity()) {
  755. newHeight += sourceHeightOffset;
  756. if (newHeight > *((float*)targetPixel1)) {
  757. *((float*)targetPixel1) = newHeight;
  758. targetPixel2[targetA.packOrder.redIndex] = sourcePixel2[sourceA.packOrder.redIndex];
  759. targetPixel2[targetA.packOrder.greenIndex] = sourcePixel2[sourceA.packOrder.greenIndex];
  760. targetPixel2[targetA.packOrder.blueIndex] = sourcePixel2[sourceA.packOrder.blueIndex];
  761. targetPixel2[targetA.packOrder.alphaIndex] = sourcePixel2[sourceA.packOrder.alphaIndex];
  762. targetPixel3[targetB.packOrder.redIndex] = sourcePixel3[sourceB.packOrder.redIndex];
  763. targetPixel3[targetB.packOrder.greenIndex] = sourcePixel3[sourceB.packOrder.greenIndex];
  764. targetPixel3[targetB.packOrder.blueIndex] = sourcePixel3[sourceB.packOrder.blueIndex];
  765. targetPixel3[targetB.packOrder.alphaIndex] = sourcePixel3[sourceB.packOrder.alphaIndex];
  766. }
  767. }
  768. );
  769. }
  770. }
  771. // -------------------------------- Resize --------------------------------
  772. static inline U32x4 ColorRgbaI32_to_U32x4(const ColorRgbaI32& color) {
  773. return U32x4(color.red, color.green, color.blue, color.alpha);
  774. }
  775. static inline ColorRgbaI32 U32x4_to_ColorRgbaI32(const U32x4& color) {
  776. UVector4D vResult = color.get();
  777. return ColorRgbaI32(vResult.x, vResult.y, vResult.z, vResult.w);
  778. }
  779. // Uniform linear interpolation of colors from a 16-bit sub-pixel weight
  780. // Pre-condition0 <= fineRatio <= 65536
  781. // Post-condition: Returns colorA * (1 - (fineRatio / 65536)) + colorB * (fineRatio / 65536)
  782. static inline U32x4 mixColorsUniform(const U32x4 &colorA, const U32x4 &colorB, uint32_t fineRatio) {
  783. uint16_t ratio = fineRatio >> 8;
  784. uint16_t invRatio = 256 - ratio;
  785. ALIGN16 U16x8 weightA = U16x8(invRatio);
  786. ALIGN16 U16x8 weightB = U16x8(ratio);
  787. ALIGN16 U32x4 lowMask(0x00FF00FFu);
  788. ALIGN16 U16x8 lowColorA = U16x8(colorA & lowMask);
  789. ALIGN16 U16x8 lowColorB = U16x8(colorB & lowMask);
  790. ALIGN16 U32x4 highMask(0xFF00FF00u);
  791. ALIGN16 U16x8 highColorA = U16x8((colorA & highMask) >> 8);
  792. ALIGN16 U16x8 highColorB = U16x8((colorB & highMask) >> 8);
  793. ALIGN16 U32x4 lowColor = (((lowColorA * weightA) + (lowColorB * weightB))).get_U32();
  794. ALIGN16 U32x4 highColor = (((highColorA * weightA) + (highColorB * weightB))).get_U32();
  795. return (((lowColor >> 8) & lowMask) | (highColor & highMask));
  796. }
  797. #define READ_CLAMP(X,Y) ImageRgbaU8Impl::unpackRgba(ImageRgbaU8Impl::readPixel_clamp(source, X, Y), source.packOrder)
  798. #define READ_CLAMP_SIMD(X,Y) ColorRgbaI32_to_U32x4(READ_CLAMP(X,Y))
  799. // Fixed-precision decimal system with 16-bit indices and 16-bit sub-pixel weights
  800. static const uint32_t interpolationFullPixel = 65536;
  801. static const uint32_t interpolationHalfPixel = interpolationFullPixel / 2;
  802. // Modulo mask for values greater than or equal to 0 and lesser than interpolationFullPixel
  803. static const uint32_t interpolationWeightMask = interpolationFullPixel - 1;
  804. // BILINEAR: Enables linear interpolation
  805. // scaleRegion:
  806. // The stretched location of the source image in the target image
  807. // Making it smaller than the target image will fill the outside with stretched pixels
  808. // Allowing the caller to crop away parts of the source image that aren't interesting
  809. // Can be used to round the region to a multiple of the input size for a fixed pixel size
  810. template <bool BILINEAR>
  811. static void resize_reference(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, const IRect& scaleRegion) {
  812. // Reference implementation
  813. // Offset in source pixels per target pixel
  814. int32_t offsetX = interpolationFullPixel * source.width / scaleRegion.width();
  815. int32_t offsetY = interpolationFullPixel * source.height / scaleRegion.height();
  816. int32_t startX = interpolationFullPixel * scaleRegion.left() + offsetX / 2;
  817. int32_t startY = interpolationFullPixel * scaleRegion.top() + offsetY / 2;
  818. if (BILINEAR) {
  819. startX -= interpolationHalfPixel;
  820. startY -= interpolationHalfPixel;
  821. }
  822. SafePointer<uint32_t> targetRow = imageInternal::getSafeData<uint32_t>(target);
  823. int32_t readY = startY;
  824. for (int32_t y = 0; y < target.height; y++) {
  825. int32_t naturalY = readY;
  826. if (naturalY < 0) { naturalY = 0; }
  827. uint32_t sampleY = (uint32_t)naturalY;
  828. uint32_t upperY = sampleY >> 16;
  829. uint32_t lowerY = upperY + 1;
  830. uint32_t lowerRatio = sampleY & interpolationWeightMask;
  831. uint32_t upperRatio = 65536 - lowerRatio;
  832. SafePointer<uint32_t> targetPixel = targetRow;
  833. int32_t readX = startX;
  834. for (int32_t x = 0; x < target.width; x++) {
  835. int32_t naturalX = readX;
  836. if (naturalX < 0) { naturalX = 0; }
  837. uint32_t sampleX = (uint32_t)naturalX;
  838. uint32_t leftX = sampleX >> 16;
  839. uint32_t rightX = leftX + 1;
  840. uint32_t rightRatio = sampleX & interpolationWeightMask;
  841. uint32_t leftRatio = 65536 - rightRatio;
  842. ColorRgbaI32 finalColor;
  843. if (BILINEAR) {
  844. ALIGN16 U32x4 vUpperLeftColor = READ_CLAMP_SIMD(leftX, upperY);
  845. ALIGN16 U32x4 vUpperRightColor = READ_CLAMP_SIMD(rightX, upperY);
  846. ALIGN16 U32x4 vLowerLeftColor = READ_CLAMP_SIMD(leftX, lowerY);
  847. ALIGN16 U32x4 vLowerRightColor = READ_CLAMP_SIMD(rightX, lowerY);
  848. ALIGN16 U32x4 vLeftRatio = U32x4(leftRatio);
  849. ALIGN16 U32x4 vRightRatio = U32x4(rightRatio);
  850. ALIGN16 U32x4 vUpperColor = ((vUpperLeftColor * vLeftRatio) + (vUpperRightColor * vRightRatio)) >> 16;
  851. ALIGN16 U32x4 vLowerColor = ((vLowerLeftColor * vLeftRatio) + (vLowerRightColor * vRightRatio)) >> 16;
  852. ALIGN16 U32x4 vCenterColor = ((vUpperColor * upperRatio) + (vLowerColor * lowerRatio)) >> 16;
  853. finalColor = U32x4_to_ColorRgbaI32(vCenterColor);
  854. } else {
  855. finalColor = READ_CLAMP(leftX, upperY);
  856. }
  857. *targetPixel = target.packRgba(finalColor).packed;
  858. targetPixel += 1;
  859. readX += offsetX;
  860. }
  861. targetRow.increaseBytes(target.stride);
  862. readY += offsetY;
  863. }
  864. }
  865. // BILINEAR: Enables linear interpolation
  866. // SIMD_ALIGNED: Each line starts 16-byte aligned, has a stride divisible with 16-bytes and is allowed to overwrite padding.
  867. template <bool BILINEAR, bool SIMD_ALIGNED>
  868. static void resize_optimized(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, const IRect& scaleRegion) {
  869. // Get source information
  870. // Compare dimensions
  871. const bool sameWidth = source.width == scaleRegion.width() && scaleRegion.left() == 0;
  872. const bool sameHeight = source.height == scaleRegion.height() && scaleRegion.top() == 0;
  873. const bool samePackOrder = target.packOrder.packOrderIndex == source.packOrder.packOrderIndex;
  874. if (sameWidth && sameHeight) {
  875. // No need to resize, just make a copy to save time
  876. imageImpl_drawCopy(target, source);
  877. } else if (sameWidth && (samePackOrder || BILINEAR)) {
  878. // Only vertical interpolation
  879. // Offset in source pixels per target pixel
  880. int32_t offsetY = interpolationFullPixel * source.height / scaleRegion.height();
  881. int32_t startY = interpolationFullPixel * scaleRegion.top() + offsetY / 2;
  882. if (BILINEAR) {
  883. startY -= interpolationHalfPixel;
  884. }
  885. SafePointer<uint32_t> targetRow = imageInternal::getSafeData<uint32_t>(target);
  886. int32_t readY = startY;
  887. for (int32_t y = 0; y < target.height; y++) {
  888. int32_t naturalY = readY;
  889. if (naturalY < 0) { naturalY = 0; }
  890. uint32_t sampleY = (uint32_t)naturalY;
  891. uint32_t upperY = sampleY >> 16;
  892. uint32_t lowerY = upperY + 1;
  893. if (upperY >= (uint32_t)source.height) upperY = source.height - 1;
  894. if (lowerY >= (uint32_t)source.height) lowerY = source.height - 1;
  895. if (BILINEAR) {
  896. uint32_t lowerRatio = sampleY & interpolationWeightMask;
  897. uint32_t upperRatio = 65536 - lowerRatio;
  898. SafePointer<uint32_t> targetPixel = targetRow;
  899. if (SIMD_ALIGNED) {
  900. const SafePointer<uint32_t> sourceRowUpper = imageInternal::getSafeData<uint32_t>(source, upperY);
  901. const SafePointer<uint32_t> sourceRowLower = imageInternal::getSafeData<uint32_t>(source, lowerY);
  902. for (int32_t x = 0; x < target.width; x += 4) {
  903. ALIGN16 U32x4 vUpperPackedColor = U32x4::readAligned(sourceRowUpper, "resize_optimized @ read vUpperPackedColor");
  904. ALIGN16 U32x4 vLowerPackedColor = U32x4::readAligned(sourceRowLower, "resize_optimized @ read vLowerPackedColor");
  905. ALIGN16 U32x4 vCenterColor = mixColorsUniform(vUpperPackedColor, vLowerPackedColor, lowerRatio);
  906. vCenterColor.writeAligned(targetPixel, "resize_optimized @ write vCenterColor");
  907. sourceRowUpper += 4;
  908. sourceRowLower += 4;
  909. targetPixel += 4;
  910. }
  911. } else {
  912. for (int32_t x = 0; x < target.width; x++) {
  913. ALIGN16 U32x4 vUpperColor = READ_CLAMP_SIMD(x, upperY);
  914. ALIGN16 U32x4 vLowerColor = READ_CLAMP_SIMD(x, lowerY);
  915. ALIGN16 U32x4 vCenterColor = ((vUpperColor * upperRatio) + (vLowerColor * lowerRatio)) >> 16;
  916. ColorRgbaI32 finalColor = U32x4_to_ColorRgbaI32(vCenterColor);
  917. *targetPixel = target.packRgba(finalColor).packed;
  918. targetPixel += 1;
  919. }
  920. }
  921. } else {
  922. const SafePointer<uint32_t> sourceRowUpper = imageInternal::getSafeData<uint32_t>(source, upperY);
  923. // Nearest neighbor sampling from a same width can be done using one copy per row
  924. safeMemoryCopy(targetRow, sourceRowUpper, source.width * 4);
  925. }
  926. targetRow.increaseBytes(target.stride);
  927. readY += offsetY;
  928. }
  929. } else if (sameHeight) {
  930. // Only horizontal interpolation
  931. // Offset in source pixels per target pixel
  932. int32_t offsetX = interpolationFullPixel * source.width / scaleRegion.width();
  933. int32_t startX = interpolationFullPixel * scaleRegion.left() + offsetX / 2;
  934. if (BILINEAR) {
  935. startX -= interpolationHalfPixel;
  936. }
  937. SafePointer<uint32_t> targetRow = imageInternal::getSafeData<uint32_t>(target);
  938. for (int32_t y = 0; y < target.height; y++) {
  939. SafePointer<uint32_t> targetPixel = targetRow;
  940. int32_t readX = startX;
  941. for (int32_t x = 0; x < target.width; x++) {
  942. int32_t naturalX = readX;
  943. if (naturalX < 0) { naturalX = 0; }
  944. uint32_t sampleX = (uint32_t)naturalX;
  945. uint32_t leftX = sampleX >> 16;
  946. uint32_t rightX = leftX + 1;
  947. uint32_t rightRatio = sampleX & interpolationWeightMask;
  948. uint32_t leftRatio = 65536 - rightRatio;
  949. ColorRgbaI32 finalColor;
  950. if (BILINEAR) {
  951. ALIGN16 U32x4 vLeftColor = READ_CLAMP_SIMD(leftX, y);
  952. ALIGN16 U32x4 vRightColor = READ_CLAMP_SIMD(rightX, y);
  953. ALIGN16 U32x4 vCenterColor = ((vLeftColor * leftRatio) + (vRightColor * rightRatio)) >> 16;
  954. finalColor = U32x4_to_ColorRgbaI32(vCenterColor);
  955. } else {
  956. finalColor = READ_CLAMP(leftX, y);
  957. }
  958. *targetPixel = target.packRgba(finalColor).packed;
  959. targetPixel += 1;
  960. readX += offsetX;
  961. }
  962. targetRow.increaseBytes(target.stride);
  963. }
  964. } else {
  965. // Call the reference implementation
  966. resize_reference<BILINEAR>(target, source, scaleRegion);
  967. }
  968. }
  969. // Returns true iff each line start in image is aligned with 16 bytes
  970. // Often not the case for sub-images, even if the parent image is aligned
  971. static bool imageIs16ByteAligned(const ImageImpl& image) {
  972. return (uint32_t)((image.stride & 15) == 0 && ((uintptr_t)(imageInternal::getSafeData<uint8_t>(image).getUnsafe()) & 15) == 0);
  973. }
  974. // Converting run-time flags into compile-time constants
  975. static void resize_aux(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, bool interpolate, bool paddWrite, const IRect& scaleRegion) {
  976. // If writing to padding is allowed and both images are 16-byte aligned with the same pack order
  977. if (paddWrite && imageIs16ByteAligned(source) && imageIs16ByteAligned(target)) {
  978. // Optimized resize allowed
  979. if (interpolate) {
  980. resize_optimized<true, true>(target, source, scaleRegion);
  981. } else {
  982. resize_optimized<false, true>(target, source, scaleRegion);
  983. }
  984. } else {
  985. // Non-optimized resize
  986. if (interpolate) {
  987. resize_optimized<true, false>(target, source, scaleRegion);
  988. } else {
  989. resize_optimized<false, false>(target, source, scaleRegion);
  990. }
  991. }
  992. }
  993. void dsr::imageImpl_resizeInPlace(ImageRgbaU8Impl& target, ImageRgbaU8Impl* wideTempImage, const ImageRgbaU8Impl& source, bool interpolate, const IRect& scaleRegion) {
  994. if (target.width != source.width && target.height > source.height) {
  995. // Upscaling is faster in two steps by both reusing the horizontal interpolation and vectorizing the vertical interpolation.
  996. int tempWidth = target.width;
  997. int tempHeight = source.height;
  998. PackOrderIndex tempPackOrder = target.packOrder.packOrderIndex;
  999. IRect tempScaleRegion = IRect(scaleRegion.left(), 0, scaleRegion.width(), source.height);
  1000. if (wideTempImage == nullptr
  1001. || wideTempImage->width != tempWidth
  1002. || wideTempImage->height != tempHeight
  1003. || wideTempImage->packOrder.packOrderIndex != tempPackOrder) {
  1004. // Performance warnings
  1005. // TODO: Make optional
  1006. if (wideTempImage != nullptr) {
  1007. if (wideTempImage->width != tempWidth) { printText("Ignored temp buffer of wrong width! Found ", wideTempImage->width, " instead of ", tempWidth, "\n"); }
  1008. if (wideTempImage->height != tempHeight) { printText("Ignored temp buffer of wrong height! Found ", wideTempImage->height, " instead of ", tempHeight, "\n"); }
  1009. if (wideTempImage->packOrder.packOrderIndex != tempPackOrder) { printText("Ignored temp buffer of wrong pack order!\n"); }
  1010. }
  1011. // Create a new buffer
  1012. ImageRgbaU8Impl newTempImage = ImageRgbaU8Impl(tempWidth, tempHeight, tempPackOrder);
  1013. resize_aux(newTempImage, source, interpolate, true, tempScaleRegion);
  1014. resize_aux(target, newTempImage, interpolate, true, scaleRegion);
  1015. } else {
  1016. // Use existing buffer
  1017. resize_aux(*wideTempImage, source, interpolate, true, tempScaleRegion);
  1018. resize_aux(target, *wideTempImage, interpolate, true, scaleRegion);
  1019. }
  1020. } else {
  1021. // Downscaling or only changing one dimension is faster in one step
  1022. resize_aux(target, source, interpolate, true, scaleRegion);
  1023. }
  1024. }
  1025. void dsr::imageImpl_resizeToTarget(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, bool interpolate) {
  1026. imageImpl_resizeInPlace(target, nullptr, source, interpolate, imageInternal::getBound(target));
  1027. }
  1028. template <bool CONVERT_COLOR>
  1029. static inline Color4xU8 convertRead(const ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int x, int y) {
  1030. Color4xU8 result = ImageRgbaU8Impl::readPixel_clamp(source, x, y);
  1031. if (CONVERT_COLOR) {
  1032. result = target.packRgba(ImageRgbaU8Impl::unpackRgba(result, source.packOrder));
  1033. }
  1034. return result;
  1035. }
  1036. // Used for drawing large pixels
  1037. static inline void fillRectangle(ImageRgbaU8Impl& target, int pixelLeft, int pixelRight, int pixelTop, int pixelBottom, const Color4xU8& packedColor) {
  1038. // TODO: Get target pointer in advance and add the correct offsets
  1039. SafePointer<Color4xU8> targetRow = imageInternal::getSafeData<Color4xU8>(target, pixelTop) + pixelLeft;
  1040. for (int y = pixelTop; y < pixelBottom; y++) {
  1041. SafePointer<Color4xU8> targetPixel = targetRow;
  1042. for (int x = pixelLeft; x < pixelRight; x++) {
  1043. *targetPixel = packedColor;
  1044. targetPixel += 1;
  1045. }
  1046. targetRow.increaseBytes(target.stride);
  1047. }
  1048. }
  1049. template <bool CONVERT_COLOR>
  1050. static void blockMagnify_reference(
  1051. ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source,
  1052. int pixelWidth, int pixelHeight, int clipWidth, int clipHeight) {
  1053. int sourceY = 0;
  1054. int maxSourceX = source.width - 1;
  1055. int maxSourceY = source.height - 1;
  1056. if (clipWidth > target.width) { clipWidth = target.width; }
  1057. if (clipHeight > target.height) { clipHeight = target.height; }
  1058. for (int32_t pixelTop = 0; pixelTop < clipHeight; pixelTop += pixelHeight) {
  1059. int sourceX = 0;
  1060. for (int32_t pixelLeft = 0; pixelLeft < clipWidth; pixelLeft += pixelWidth) {
  1061. // Read the pixel once
  1062. Color4xU8 sourceColor = convertRead<CONVERT_COLOR>(target, source, sourceX, sourceY);
  1063. // Write to all target pixels in a conditionless loop
  1064. fillRectangle(target, pixelLeft, pixelLeft + pixelWidth, pixelTop, pixelTop + pixelHeight, sourceColor);
  1065. // Iterate and clamp the read coordinate
  1066. sourceX++;
  1067. if (sourceX > maxSourceX) { sourceX = maxSourceX; }
  1068. }
  1069. // Iterate and clamp the read coordinate
  1070. sourceY++;
  1071. if (sourceY > maxSourceY) { sourceY = maxSourceY; }
  1072. }
  1073. }
  1074. // Pre-condition:
  1075. // * The source and target images have the same pack order
  1076. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1077. // * clipWidth % 2 == 0
  1078. // * clipHeight % 2 == 0
  1079. static void blockMagnify_2x2(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1080. #ifdef USE_SIMD_EXTRA
  1081. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1082. SafePointer<uint32_t> upperTargetRow = imageInternal::getSafeData<uint32_t>(target, 0);
  1083. SafePointer<uint32_t> lowerTargetRow = imageInternal::getSafeData<uint32_t>(target, 1);
  1084. int doubleTargetStride = target.stride * 2;
  1085. for (int upperTargetY = 0; upperTargetY + 2 <= clipHeight; upperTargetY+=2) {
  1086. // Carriage return
  1087. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1088. SafePointer<uint32_t> upperTargetPixel = upperTargetRow;
  1089. SafePointer<uint32_t> lowerTargetPixel = lowerTargetRow;
  1090. // Write to whole multiples of 8 pixels
  1091. int writeLeftX = 0;
  1092. while (writeLeftX + 8 <= clipWidth) {
  1093. // Read pixels
  1094. ALIGN16 SIMD_U32x4 sourcePixels = U32x4::readAligned(sourcePixel, "blockMagnify_2x2 @ whole sourcePixels").v;
  1095. sourcePixel += 4;
  1096. // Double the pixels by zipping with itself
  1097. ALIGN16 SIMD_U32x4x2 doubledPixels = ZIP_U32_SIMD(sourcePixels, sourcePixels);
  1098. // Write lower part
  1099. U32x4(doubledPixels.val[0]).writeAligned(upperTargetPixel, "blockMagnify_2x2 @ write upper left #1");
  1100. upperTargetPixel += 4;
  1101. U32x4(doubledPixels.val[0]).writeAligned(lowerTargetPixel, "blockMagnify_2x2 @ write lower left #1");
  1102. lowerTargetPixel += 4;
  1103. // Write upper part
  1104. U32x4(doubledPixels.val[1]).writeAligned(upperTargetPixel, "blockMagnify_2x2 @ write upper right #1");
  1105. upperTargetPixel += 4;
  1106. U32x4(doubledPixels.val[1]).writeAligned(lowerTargetPixel, "blockMagnify_2x2 @ write lower right #1");
  1107. lowerTargetPixel += 4;
  1108. // Count
  1109. writeLeftX += 8;
  1110. }
  1111. // Fill the last pixels using scalar operations to avoid going out of bound
  1112. while (writeLeftX + 2 <= clipWidth) {
  1113. // Read one pixel
  1114. uint32_t sourceColor = *sourcePixel;
  1115. // Write 2x2 pixels
  1116. *upperTargetPixel = sourceColor; upperTargetPixel += 1;
  1117. *upperTargetPixel = sourceColor; upperTargetPixel += 1;
  1118. *lowerTargetPixel = sourceColor; lowerTargetPixel += 1;
  1119. *lowerTargetPixel = sourceColor; lowerTargetPixel += 1;
  1120. // Count
  1121. writeLeftX += 2;
  1122. }
  1123. // Line feed
  1124. sourceRow.increaseBytes(source.stride);
  1125. upperTargetRow.increaseBytes(doubleTargetStride);
  1126. lowerTargetRow.increaseBytes(doubleTargetStride);
  1127. }
  1128. #else
  1129. blockMagnify_reference<false>(target, source, 2, 2, clipWidth, clipHeight);
  1130. #endif
  1131. }
  1132. // Pre-condition:
  1133. // * The source and target images have the same pack order
  1134. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1135. // * clipWidth % 3 == 0
  1136. // * clipHeight % 3 == 0
  1137. static void blockMagnify_3x3(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1138. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1139. SafePointer<uint32_t> targetRowA = imageInternal::getSafeData<uint32_t>(target, 0);
  1140. SafePointer<uint32_t> targetRowB = imageInternal::getSafeData<uint32_t>(target, 1);
  1141. SafePointer<uint32_t> targetRowC = imageInternal::getSafeData<uint32_t>(target, 2);
  1142. int blockTargetStride = target.stride * 3;
  1143. for (int upperTargetY = 0; upperTargetY + 3 <= clipHeight; upperTargetY+=3) {
  1144. // Carriage return
  1145. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1146. SafePointer<uint32_t> targetPixelA = targetRowA;
  1147. SafePointer<uint32_t> targetPixelB = targetRowB;
  1148. SafePointer<uint32_t> targetPixelC = targetRowC;
  1149. int writeLeftX = 0;
  1150. while (writeLeftX + 3 <= clipWidth) {
  1151. // Read one pixel at a time
  1152. uint32_t scalarValue = *sourcePixel;
  1153. sourcePixel += 1;
  1154. // Write to a whole block of pixels
  1155. targetPixelA[0] = scalarValue; targetPixelA[1] = scalarValue; targetPixelA[2] = scalarValue;
  1156. targetPixelB[0] = scalarValue; targetPixelB[1] = scalarValue; targetPixelB[2] = scalarValue;
  1157. targetPixelC[0] = scalarValue; targetPixelC[1] = scalarValue; targetPixelC[2] = scalarValue;
  1158. targetPixelA += 3;
  1159. targetPixelB += 3;
  1160. targetPixelC += 3;
  1161. // Count
  1162. writeLeftX += 3;
  1163. }
  1164. // Line feed
  1165. sourceRow.increaseBytes(source.stride);
  1166. targetRowA.increaseBytes(blockTargetStride);
  1167. targetRowB.increaseBytes(blockTargetStride);
  1168. targetRowC.increaseBytes(blockTargetStride);
  1169. }
  1170. }
  1171. // Pre-condition:
  1172. // * The source and target images have the same pack order
  1173. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1174. // * clipWidth % 4 == 0
  1175. // * clipHeight % 4 == 0
  1176. static void blockMagnify_4x4(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1177. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1178. SafePointer<uint32_t> targetRowA = imageInternal::getSafeData<uint32_t>(target, 0);
  1179. SafePointer<uint32_t> targetRowB = imageInternal::getSafeData<uint32_t>(target, 1);
  1180. SafePointer<uint32_t> targetRowC = imageInternal::getSafeData<uint32_t>(target, 2);
  1181. SafePointer<uint32_t> targetRowD = imageInternal::getSafeData<uint32_t>(target, 3);
  1182. int quadTargetStride = target.stride * 4;
  1183. for (int upperTargetY = 0; upperTargetY + 4 <= clipHeight; upperTargetY+=4) {
  1184. // Carriage return
  1185. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1186. SafePointer<uint32_t> targetPixelA = targetRowA;
  1187. SafePointer<uint32_t> targetPixelB = targetRowB;
  1188. SafePointer<uint32_t> targetPixelC = targetRowC;
  1189. SafePointer<uint32_t> targetPixelD = targetRowD;
  1190. int writeLeftX = 0;
  1191. while (writeLeftX + 4 <= clipWidth) {
  1192. // Read one pixel at a time
  1193. uint32_t scalarValue = *sourcePixel;
  1194. sourcePixel += 1;
  1195. // Convert scalar to SIMD vector of 4 repeated pixels
  1196. ALIGN16 U32x4 sourcePixels = U32x4(scalarValue);
  1197. // Write to 4x4 pixels using 4 SIMD writes
  1198. sourcePixels.writeAligned(targetPixelA, "blockMagnify_4x4 @ write A");
  1199. sourcePixels.writeAligned(targetPixelB, "blockMagnify_4x4 @ write B");
  1200. sourcePixels.writeAligned(targetPixelC, "blockMagnify_4x4 @ write C");
  1201. sourcePixels.writeAligned(targetPixelD, "blockMagnify_4x4 @ write D");
  1202. targetPixelA += 4;
  1203. targetPixelB += 4;
  1204. targetPixelC += 4;
  1205. targetPixelD += 4;
  1206. // Count
  1207. writeLeftX += 4;
  1208. }
  1209. // Line feed
  1210. sourceRow.increaseBytes(source.stride);
  1211. targetRowA.increaseBytes(quadTargetStride);
  1212. targetRowB.increaseBytes(quadTargetStride);
  1213. targetRowC.increaseBytes(quadTargetStride);
  1214. targetRowD.increaseBytes(quadTargetStride);
  1215. }
  1216. }
  1217. // Pre-condition:
  1218. // * The source and target images have the same pack order
  1219. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1220. // * clipWidth % 5 == 0
  1221. // * clipHeight % 5 == 0
  1222. static void blockMagnify_5x5(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1223. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1224. SafePointer<uint32_t> targetRowA = imageInternal::getSafeData<uint32_t>(target, 0);
  1225. SafePointer<uint32_t> targetRowB = imageInternal::getSafeData<uint32_t>(target, 1);
  1226. SafePointer<uint32_t> targetRowC = imageInternal::getSafeData<uint32_t>(target, 2);
  1227. SafePointer<uint32_t> targetRowD = imageInternal::getSafeData<uint32_t>(target, 3);
  1228. SafePointer<uint32_t> targetRowE = imageInternal::getSafeData<uint32_t>(target, 4);
  1229. int blockTargetStride = target.stride * 5;
  1230. for (int upperTargetY = 0; upperTargetY + 5 <= clipHeight; upperTargetY+=5) {
  1231. // Carriage return
  1232. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1233. SafePointer<uint32_t> targetPixelA = targetRowA;
  1234. SafePointer<uint32_t> targetPixelB = targetRowB;
  1235. SafePointer<uint32_t> targetPixelC = targetRowC;
  1236. SafePointer<uint32_t> targetPixelD = targetRowD;
  1237. SafePointer<uint32_t> targetPixelE = targetRowE;
  1238. int writeLeftX = 0;
  1239. while (writeLeftX + 5 <= clipWidth) {
  1240. // Read one pixel at a time
  1241. uint32_t scalarValue = *sourcePixel;
  1242. sourcePixel += 1;
  1243. // Write to a whole block of pixels
  1244. targetPixelA[0] = scalarValue; targetPixelA[1] = scalarValue; targetPixelA[2] = scalarValue; targetPixelA[3] = scalarValue; targetPixelA[4] = scalarValue;
  1245. targetPixelB[0] = scalarValue; targetPixelB[1] = scalarValue; targetPixelB[2] = scalarValue; targetPixelB[3] = scalarValue; targetPixelB[4] = scalarValue;
  1246. targetPixelC[0] = scalarValue; targetPixelC[1] = scalarValue; targetPixelC[2] = scalarValue; targetPixelC[3] = scalarValue; targetPixelC[4] = scalarValue;
  1247. targetPixelD[0] = scalarValue; targetPixelD[1] = scalarValue; targetPixelD[2] = scalarValue; targetPixelD[3] = scalarValue; targetPixelD[4] = scalarValue;
  1248. targetPixelE[0] = scalarValue; targetPixelE[1] = scalarValue; targetPixelE[2] = scalarValue; targetPixelE[3] = scalarValue; targetPixelE[4] = scalarValue;
  1249. targetPixelA += 5;
  1250. targetPixelB += 5;
  1251. targetPixelC += 5;
  1252. targetPixelD += 5;
  1253. targetPixelE += 5;
  1254. // Count
  1255. writeLeftX += 5;
  1256. }
  1257. // Line feed
  1258. sourceRow.increaseBytes(source.stride);
  1259. targetRowA.increaseBytes(blockTargetStride);
  1260. targetRowB.increaseBytes(blockTargetStride);
  1261. targetRowC.increaseBytes(blockTargetStride);
  1262. targetRowD.increaseBytes(blockTargetStride);
  1263. targetRowE.increaseBytes(blockTargetStride);
  1264. }
  1265. }
  1266. // Pre-condition:
  1267. // * The source and target images have the same pack order
  1268. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1269. // * clipWidth % 6 == 0
  1270. // * clipHeight % 6 == 0
  1271. static void blockMagnify_6x6(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1272. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1273. SafePointer<uint32_t> targetRowA = imageInternal::getSafeData<uint32_t>(target, 0);
  1274. SafePointer<uint32_t> targetRowB = imageInternal::getSafeData<uint32_t>(target, 1);
  1275. SafePointer<uint32_t> targetRowC = imageInternal::getSafeData<uint32_t>(target, 2);
  1276. SafePointer<uint32_t> targetRowD = imageInternal::getSafeData<uint32_t>(target, 3);
  1277. SafePointer<uint32_t> targetRowE = imageInternal::getSafeData<uint32_t>(target, 4);
  1278. SafePointer<uint32_t> targetRowF = imageInternal::getSafeData<uint32_t>(target, 5);
  1279. int blockTargetStride = target.stride * 6;
  1280. for (int upperTargetY = 0; upperTargetY + 6 <= clipHeight; upperTargetY+=6) {
  1281. // Carriage return
  1282. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1283. SafePointer<uint32_t> targetPixelA = targetRowA;
  1284. SafePointer<uint32_t> targetPixelB = targetRowB;
  1285. SafePointer<uint32_t> targetPixelC = targetRowC;
  1286. SafePointer<uint32_t> targetPixelD = targetRowD;
  1287. SafePointer<uint32_t> targetPixelE = targetRowE;
  1288. SafePointer<uint32_t> targetPixelF = targetRowF;
  1289. int writeLeftX = 0;
  1290. while (writeLeftX + 6 <= clipWidth) {
  1291. // Read one pixel at a time
  1292. uint32_t scalarValue = *sourcePixel;
  1293. sourcePixel += 1;
  1294. // Write to a whole block of pixels
  1295. targetPixelA[0] = scalarValue; targetPixelA[1] = scalarValue; targetPixelA[2] = scalarValue; targetPixelA[3] = scalarValue; targetPixelA[4] = scalarValue; targetPixelA[5] = scalarValue;
  1296. targetPixelB[0] = scalarValue; targetPixelB[1] = scalarValue; targetPixelB[2] = scalarValue; targetPixelB[3] = scalarValue; targetPixelB[4] = scalarValue; targetPixelB[5] = scalarValue;
  1297. targetPixelC[0] = scalarValue; targetPixelC[1] = scalarValue; targetPixelC[2] = scalarValue; targetPixelC[3] = scalarValue; targetPixelC[4] = scalarValue; targetPixelC[5] = scalarValue;
  1298. targetPixelD[0] = scalarValue; targetPixelD[1] = scalarValue; targetPixelD[2] = scalarValue; targetPixelD[3] = scalarValue; targetPixelD[4] = scalarValue; targetPixelD[5] = scalarValue;
  1299. targetPixelE[0] = scalarValue; targetPixelE[1] = scalarValue; targetPixelE[2] = scalarValue; targetPixelE[3] = scalarValue; targetPixelE[4] = scalarValue; targetPixelE[5] = scalarValue;
  1300. targetPixelF[0] = scalarValue; targetPixelF[1] = scalarValue; targetPixelF[2] = scalarValue; targetPixelF[3] = scalarValue; targetPixelF[4] = scalarValue; targetPixelF[5] = scalarValue;
  1301. targetPixelA += 6;
  1302. targetPixelB += 6;
  1303. targetPixelC += 6;
  1304. targetPixelD += 6;
  1305. targetPixelE += 6;
  1306. targetPixelF += 6;
  1307. // Count
  1308. writeLeftX += 6;
  1309. }
  1310. // Line feed
  1311. sourceRow.increaseBytes(source.stride);
  1312. targetRowA.increaseBytes(blockTargetStride);
  1313. targetRowB.increaseBytes(blockTargetStride);
  1314. targetRowC.increaseBytes(blockTargetStride);
  1315. targetRowD.increaseBytes(blockTargetStride);
  1316. targetRowE.increaseBytes(blockTargetStride);
  1317. targetRowF.increaseBytes(blockTargetStride);
  1318. }
  1319. }
  1320. // Pre-condition:
  1321. // * The source and target images have the same pack order
  1322. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1323. // * clipWidth % 7 == 0
  1324. // * clipHeight % 7 == 0
  1325. static void blockMagnify_7x7(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1326. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1327. SafePointer<uint32_t> targetRowA = imageInternal::getSafeData<uint32_t>(target, 0);
  1328. SafePointer<uint32_t> targetRowB = imageInternal::getSafeData<uint32_t>(target, 1);
  1329. SafePointer<uint32_t> targetRowC = imageInternal::getSafeData<uint32_t>(target, 2);
  1330. SafePointer<uint32_t> targetRowD = imageInternal::getSafeData<uint32_t>(target, 3);
  1331. SafePointer<uint32_t> targetRowE = imageInternal::getSafeData<uint32_t>(target, 4);
  1332. SafePointer<uint32_t> targetRowF = imageInternal::getSafeData<uint32_t>(target, 5);
  1333. SafePointer<uint32_t> targetRowG = imageInternal::getSafeData<uint32_t>(target, 6);
  1334. int blockTargetStride = target.stride * 7;
  1335. for (int upperTargetY = 0; upperTargetY + 7 <= clipHeight; upperTargetY+=7) {
  1336. // Carriage return
  1337. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1338. SafePointer<uint32_t> targetPixelA = targetRowA;
  1339. SafePointer<uint32_t> targetPixelB = targetRowB;
  1340. SafePointer<uint32_t> targetPixelC = targetRowC;
  1341. SafePointer<uint32_t> targetPixelD = targetRowD;
  1342. SafePointer<uint32_t> targetPixelE = targetRowE;
  1343. SafePointer<uint32_t> targetPixelF = targetRowF;
  1344. SafePointer<uint32_t> targetPixelG = targetRowG;
  1345. int writeLeftX = 0;
  1346. while (writeLeftX + 7 <= clipWidth) {
  1347. // Read one pixel at a time
  1348. uint32_t scalarValue = *sourcePixel;
  1349. sourcePixel += 1;
  1350. // Write to a whole block of pixels
  1351. targetPixelA[0] = scalarValue; targetPixelA[1] = scalarValue; targetPixelA[2] = scalarValue; targetPixelA[3] = scalarValue; targetPixelA[4] = scalarValue; targetPixelA[5] = scalarValue; targetPixelA[6] = scalarValue;
  1352. targetPixelB[0] = scalarValue; targetPixelB[1] = scalarValue; targetPixelB[2] = scalarValue; targetPixelB[3] = scalarValue; targetPixelB[4] = scalarValue; targetPixelB[5] = scalarValue; targetPixelB[6] = scalarValue;
  1353. targetPixelC[0] = scalarValue; targetPixelC[1] = scalarValue; targetPixelC[2] = scalarValue; targetPixelC[3] = scalarValue; targetPixelC[4] = scalarValue; targetPixelC[5] = scalarValue; targetPixelC[6] = scalarValue;
  1354. targetPixelD[0] = scalarValue; targetPixelD[1] = scalarValue; targetPixelD[2] = scalarValue; targetPixelD[3] = scalarValue; targetPixelD[4] = scalarValue; targetPixelD[5] = scalarValue; targetPixelD[6] = scalarValue;
  1355. targetPixelE[0] = scalarValue; targetPixelE[1] = scalarValue; targetPixelE[2] = scalarValue; targetPixelE[3] = scalarValue; targetPixelE[4] = scalarValue; targetPixelE[5] = scalarValue; targetPixelE[6] = scalarValue;
  1356. targetPixelF[0] = scalarValue; targetPixelF[1] = scalarValue; targetPixelF[2] = scalarValue; targetPixelF[3] = scalarValue; targetPixelF[4] = scalarValue; targetPixelF[5] = scalarValue; targetPixelF[6] = scalarValue;
  1357. targetPixelG[0] = scalarValue; targetPixelG[1] = scalarValue; targetPixelG[2] = scalarValue; targetPixelG[3] = scalarValue; targetPixelG[4] = scalarValue; targetPixelG[5] = scalarValue; targetPixelG[6] = scalarValue;
  1358. targetPixelA += 7;
  1359. targetPixelB += 7;
  1360. targetPixelC += 7;
  1361. targetPixelD += 7;
  1362. targetPixelE += 7;
  1363. targetPixelF += 7;
  1364. targetPixelG += 7;
  1365. // Count
  1366. writeLeftX += 7;
  1367. }
  1368. // Line feed
  1369. sourceRow.increaseBytes(source.stride);
  1370. targetRowA.increaseBytes(blockTargetStride);
  1371. targetRowB.increaseBytes(blockTargetStride);
  1372. targetRowC.increaseBytes(blockTargetStride);
  1373. targetRowD.increaseBytes(blockTargetStride);
  1374. targetRowE.increaseBytes(blockTargetStride);
  1375. targetRowF.increaseBytes(blockTargetStride);
  1376. targetRowG.increaseBytes(blockTargetStride);
  1377. }
  1378. }
  1379. // Pre-condition:
  1380. // * The source and target images have the same pack order
  1381. // * Both source and target are 16-byte aligned, but does not have to own their padding
  1382. // * clipWidth % 8 == 0
  1383. // * clipHeight % 8 == 0
  1384. static void blockMagnify_8x8(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int clipWidth, int clipHeight) {
  1385. const SafePointer<uint32_t> sourceRow = imageInternal::getSafeData<uint32_t>(source);
  1386. SafePointer<uint32_t> targetRowA = imageInternal::getSafeData<uint32_t>(target, 0);
  1387. SafePointer<uint32_t> targetRowB = imageInternal::getSafeData<uint32_t>(target, 1);
  1388. SafePointer<uint32_t> targetRowC = imageInternal::getSafeData<uint32_t>(target, 2);
  1389. SafePointer<uint32_t> targetRowD = imageInternal::getSafeData<uint32_t>(target, 3);
  1390. SafePointer<uint32_t> targetRowE = imageInternal::getSafeData<uint32_t>(target, 4);
  1391. SafePointer<uint32_t> targetRowF = imageInternal::getSafeData<uint32_t>(target, 5);
  1392. SafePointer<uint32_t> targetRowG = imageInternal::getSafeData<uint32_t>(target, 6);
  1393. SafePointer<uint32_t> targetRowH = imageInternal::getSafeData<uint32_t>(target, 7);
  1394. int blockTargetStride = target.stride * 8;
  1395. for (int upperTargetY = 0; upperTargetY + 8 <= clipHeight; upperTargetY+=8) {
  1396. // Carriage return
  1397. const SafePointer<uint32_t> sourcePixel = sourceRow;
  1398. SafePointer<uint32_t> targetPixelA = targetRowA;
  1399. SafePointer<uint32_t> targetPixelB = targetRowB;
  1400. SafePointer<uint32_t> targetPixelC = targetRowC;
  1401. SafePointer<uint32_t> targetPixelD = targetRowD;
  1402. SafePointer<uint32_t> targetPixelE = targetRowE;
  1403. SafePointer<uint32_t> targetPixelF = targetRowF;
  1404. SafePointer<uint32_t> targetPixelG = targetRowG;
  1405. SafePointer<uint32_t> targetPixelH = targetRowH;
  1406. int writeLeftX = 0;
  1407. while (writeLeftX + 8 <= clipWidth) {
  1408. // Read one pixel at a time
  1409. uint32_t scalarValue = *sourcePixel;
  1410. sourcePixel += 1;
  1411. // Write to a whole block of pixels
  1412. targetPixelA[0] = scalarValue; targetPixelA[1] = scalarValue; targetPixelA[2] = scalarValue; targetPixelA[3] = scalarValue; targetPixelA[4] = scalarValue; targetPixelA[5] = scalarValue; targetPixelA[6] = scalarValue; targetPixelA[7] = scalarValue;
  1413. targetPixelB[0] = scalarValue; targetPixelB[1] = scalarValue; targetPixelB[2] = scalarValue; targetPixelB[3] = scalarValue; targetPixelB[4] = scalarValue; targetPixelB[5] = scalarValue; targetPixelB[6] = scalarValue; targetPixelB[7] = scalarValue;
  1414. targetPixelC[0] = scalarValue; targetPixelC[1] = scalarValue; targetPixelC[2] = scalarValue; targetPixelC[3] = scalarValue; targetPixelC[4] = scalarValue; targetPixelC[5] = scalarValue; targetPixelC[6] = scalarValue; targetPixelC[7] = scalarValue;
  1415. targetPixelD[0] = scalarValue; targetPixelD[1] = scalarValue; targetPixelD[2] = scalarValue; targetPixelD[3] = scalarValue; targetPixelD[4] = scalarValue; targetPixelD[5] = scalarValue; targetPixelD[6] = scalarValue; targetPixelD[7] = scalarValue;
  1416. targetPixelE[0] = scalarValue; targetPixelE[1] = scalarValue; targetPixelE[2] = scalarValue; targetPixelE[3] = scalarValue; targetPixelE[4] = scalarValue; targetPixelE[5] = scalarValue; targetPixelE[6] = scalarValue; targetPixelE[7] = scalarValue;
  1417. targetPixelF[0] = scalarValue; targetPixelF[1] = scalarValue; targetPixelF[2] = scalarValue; targetPixelF[3] = scalarValue; targetPixelF[4] = scalarValue; targetPixelF[5] = scalarValue; targetPixelF[6] = scalarValue; targetPixelF[7] = scalarValue;
  1418. targetPixelG[0] = scalarValue; targetPixelG[1] = scalarValue; targetPixelG[2] = scalarValue; targetPixelG[3] = scalarValue; targetPixelG[4] = scalarValue; targetPixelG[5] = scalarValue; targetPixelG[6] = scalarValue; targetPixelG[7] = scalarValue;
  1419. targetPixelH[0] = scalarValue; targetPixelH[1] = scalarValue; targetPixelH[2] = scalarValue; targetPixelH[3] = scalarValue; targetPixelH[4] = scalarValue; targetPixelH[5] = scalarValue; targetPixelH[6] = scalarValue; targetPixelH[7] = scalarValue;
  1420. targetPixelA += 8;
  1421. targetPixelB += 8;
  1422. targetPixelC += 8;
  1423. targetPixelD += 8;
  1424. targetPixelE += 8;
  1425. targetPixelF += 8;
  1426. targetPixelG += 8;
  1427. targetPixelH += 8;
  1428. // Count
  1429. writeLeftX += 8;
  1430. }
  1431. // Line feed
  1432. sourceRow.increaseBytes(source.stride);
  1433. targetRowA.increaseBytes(blockTargetStride);
  1434. targetRowB.increaseBytes(blockTargetStride);
  1435. targetRowC.increaseBytes(blockTargetStride);
  1436. targetRowD.increaseBytes(blockTargetStride);
  1437. targetRowE.increaseBytes(blockTargetStride);
  1438. targetRowF.increaseBytes(blockTargetStride);
  1439. targetRowG.increaseBytes(blockTargetStride);
  1440. targetRowH.increaseBytes(blockTargetStride);
  1441. }
  1442. }
  1443. static void blackEdges(ImageRgbaU8Impl& target, int excludedWidth, int excludedHeight) {
  1444. // Right side
  1445. drawSolidRectangleMemset<Color4xU8>(target, excludedWidth, 0, target.width, excludedHeight, 0);
  1446. // Bottom and corner
  1447. drawSolidRectangleMemset<Color4xU8>(target, 0, excludedHeight, target.width, target.height, 0);
  1448. }
  1449. void dsr::imageImpl_blockMagnify(ImageRgbaU8Impl& target, const ImageRgbaU8Impl& source, int pixelWidth, int pixelHeight) {
  1450. if (pixelWidth < 1) { pixelWidth = 1; }
  1451. if (pixelHeight < 1) { pixelHeight = 1; }
  1452. bool sameOrder = target.packOrder.packOrderIndex == source.packOrder.packOrderIndex;
  1453. // Find the part of source which fits into target with whole pixels
  1454. int clipWidth = roundDown(std::min(target.width, source.width * pixelWidth), pixelWidth);
  1455. int clipHeight = roundDown(std::min(target.height, source.height * pixelHeight), pixelHeight);
  1456. if (sameOrder) {
  1457. if (imageIs16ByteAligned(source) && imageIs16ByteAligned(target)) {
  1458. if (pixelWidth == 2 && pixelHeight == 2) {
  1459. blockMagnify_2x2(target, source, clipWidth, clipHeight);
  1460. } else if (pixelWidth == 3 && pixelHeight == 3) {
  1461. blockMagnify_3x3(target, source, clipWidth, clipHeight);
  1462. } else if (pixelWidth == 4 && pixelHeight == 4) {
  1463. blockMagnify_4x4(target, source, clipWidth, clipHeight);
  1464. } else if (pixelWidth == 5 && pixelHeight == 5) {
  1465. blockMagnify_5x5(target, source, clipWidth, clipHeight);
  1466. } else if (pixelWidth == 6 && pixelHeight == 6) {
  1467. blockMagnify_6x6(target, source, clipWidth, clipHeight);
  1468. } else if (pixelWidth == 7 && pixelHeight == 7) {
  1469. blockMagnify_7x7(target, source, clipWidth, clipHeight);
  1470. } else if (pixelWidth == 8 && pixelHeight == 8) {
  1471. blockMagnify_8x8(target, source, clipWidth, clipHeight);
  1472. } else {
  1473. blockMagnify_reference<false>(target, source, pixelWidth, pixelHeight, clipWidth, clipHeight);
  1474. }
  1475. } else {
  1476. blockMagnify_reference<false>(target, source, pixelWidth, pixelHeight, clipWidth, clipHeight);
  1477. }
  1478. } else {
  1479. blockMagnify_reference<true>(target, source, pixelWidth, pixelHeight, clipWidth, clipHeight);
  1480. }
  1481. blackEdges(target, clipWidth, clipHeight);
  1482. }