imageAPI.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #define DFPSR_INTERNAL_ACCESS
  24. #include <limits>
  25. #include <cassert>
  26. #include "imageAPI.h"
  27. #include "drawAPI.h"
  28. #include "../image/draw.h"
  29. #include "../image/internal/imageInternal.h"
  30. #include "../image/stbImage/stbImageWrapper.h"
  31. #include "../math/scalar.h"
  32. using namespace dsr;
  33. // Constructors
  34. AlignedImageU8 dsr::image_create_U8(int32_t width, int32_t height) {
  35. return AlignedImageU8(std::make_shared<ImageU8Impl>(width, height));
  36. }
  37. AlignedImageU16 dsr::image_create_U16(int32_t width, int32_t height) {
  38. return AlignedImageU16(std::make_shared<ImageU16Impl>(width, height));
  39. }
  40. AlignedImageF32 dsr::image_create_F32(int32_t width, int32_t height) {
  41. return AlignedImageF32(std::make_shared<ImageF32Impl>(width, height));
  42. }
  43. OrderedImageRgbaU8 dsr::image_create_RgbaU8(int32_t width, int32_t height) {
  44. return OrderedImageRgbaU8(std::make_shared<ImageRgbaU8Impl>(width, height));
  45. }
  46. AlignedImageRgbaU8 dsr::image_create_RgbaU8_native(int32_t width, int32_t height, PackOrderIndex packOrderIndex) {
  47. return AlignedImageRgbaU8(std::make_shared<ImageRgbaU8Impl>(width, height, packOrderIndex));
  48. }
  49. // Loading and saving
  50. OrderedImageRgbaU8 dsr::image_load_RgbaU8(const String& filename, bool mustExist) {
  51. return image_stb_load_RgbaU8(filename, mustExist);
  52. }
  53. bool dsr::image_save(const ImageRgbaU8 &image, const String& filename) {
  54. return image_stb_save(image, filename);
  55. }
  56. #define GET_OPTIONAL(SOURCE,DEFAULT) \
  57. if (image) { \
  58. return SOURCE; \
  59. } else { \
  60. return DEFAULT; \
  61. }
  62. // Properties
  63. int32_t dsr::image_getWidth(const ImageU8& image) { GET_OPTIONAL(image->width, 0); }
  64. int32_t dsr::image_getWidth(const ImageU16& image) { GET_OPTIONAL(image->width, 0); }
  65. int32_t dsr::image_getWidth(const ImageF32& image) { GET_OPTIONAL(image->width, 0); }
  66. int32_t dsr::image_getWidth(const ImageRgbaU8& image) { GET_OPTIONAL(image->width, 0); }
  67. int32_t dsr::image_getHeight(const ImageU8& image) { GET_OPTIONAL(image->height, 0); }
  68. int32_t dsr::image_getHeight(const ImageU16& image) { GET_OPTIONAL(image->height, 0); }
  69. int32_t dsr::image_getHeight(const ImageF32& image) { GET_OPTIONAL(image->height, 0); }
  70. int32_t dsr::image_getHeight(const ImageRgbaU8& image) { GET_OPTIONAL(image->height, 0); }
  71. int32_t dsr::image_getStride(const ImageU8& image) { GET_OPTIONAL(image->stride, 0); }
  72. int32_t dsr::image_getStride(const ImageU16& image) { GET_OPTIONAL(image->stride, 0); }
  73. int32_t dsr::image_getStride(const ImageF32& image) { GET_OPTIONAL(image->stride, 0); }
  74. int32_t dsr::image_getStride(const ImageRgbaU8& image) { GET_OPTIONAL(image->stride, 0); }
  75. IRect dsr::image_getBound(const ImageU8& image) { GET_OPTIONAL(IRect(0, 0, image->width, image->height), IRect()); }
  76. IRect dsr::image_getBound(const ImageU16& image) { GET_OPTIONAL(IRect(0, 0, image->width, image->height), IRect()); }
  77. IRect dsr::image_getBound(const ImageF32& image) { GET_OPTIONAL(IRect(0, 0, image->width, image->height), IRect()); }
  78. IRect dsr::image_getBound(const ImageRgbaU8& image) { GET_OPTIONAL(IRect(0, 0, image->width, image->height), IRect()); }
  79. bool dsr::image_exists(const ImageU8& image) { GET_OPTIONAL(true, false); }
  80. bool dsr::image_exists(const ImageU16& image) { GET_OPTIONAL(true, false); }
  81. bool dsr::image_exists(const ImageF32& image) { GET_OPTIONAL(true, false); }
  82. bool dsr::image_exists(const ImageRgbaU8& image) { GET_OPTIONAL(true, false); }
  83. int dsr::image_useCount(const ImageU8& image) { return image.use_count(); }
  84. int dsr::image_useCount(const ImageU16& image) { return image.use_count(); }
  85. int dsr::image_useCount(const ImageF32& image) { return image.use_count(); }
  86. int dsr::image_useCount(const ImageRgbaU8& image) { return image.use_count(); }
  87. PackOrderIndex dsr::image_getPackOrderIndex(const ImageRgbaU8& image) {
  88. GET_OPTIONAL(image->packOrder.packOrderIndex, PackOrderIndex::RGBA);
  89. }
  90. // Texture
  91. void dsr::image_generatePyramid(ImageRgbaU8& image) {
  92. if (image) {
  93. image->generatePyramid();
  94. }
  95. }
  96. void dsr::image_removePyramid(ImageRgbaU8& image) {
  97. if (image) {
  98. image->removePyramid();
  99. }
  100. }
  101. bool dsr::image_hasPyramid(const ImageRgbaU8& image) {
  102. GET_OPTIONAL(image->texture.hasMipBuffer(), false);
  103. }
  104. bool dsr::image_isTexture(const ImageRgbaU8& image) {
  105. GET_OPTIONAL(image->isTexture(), false);
  106. }
  107. // Pixel access
  108. #define INSIDE_XY (x >= 0 && x < image->width && y >= 0 && y < image->height)
  109. #define CLAMP_XY \
  110. if (x < 0) { x = 0; } \
  111. if (y < 0) { y = 0; } \
  112. if (x >= image->width) { x = image->width - 1; } \
  113. if (y >= image->height) { y = image->height - 1; }
  114. #define TILE_XY \
  115. x = signedModulo(x, image->width); \
  116. y = signedModulo(y, image->height);
  117. void dsr::image_writePixel(ImageU8& image, int32_t x, int32_t y, int32_t color) {
  118. if (image) {
  119. if (INSIDE_XY) {
  120. if (color < 0) { color = 0; }
  121. if (color > 255) { color = 255; }
  122. ImageU8Impl::writePixel_unsafe(*image, x, y, color);
  123. }
  124. }
  125. }
  126. void dsr::image_writePixel(ImageU16& image, int32_t x, int32_t y, int32_t color) {
  127. if (image) {
  128. if (INSIDE_XY) {
  129. if (color < 0) { color = 0; }
  130. if (color > 65535) { color = 65535; }
  131. ImageU16Impl::writePixel_unsafe(*image, x, y, color);
  132. }
  133. }
  134. }
  135. void dsr::image_writePixel(ImageF32& image, int32_t x, int32_t y, float color) {
  136. if (image) {
  137. if (INSIDE_XY) {
  138. ImageF32Impl::writePixel_unsafe(*image, x, y, color);
  139. }
  140. }
  141. }
  142. void dsr::image_writePixel(ImageRgbaU8& image, int32_t x, int32_t y, const ColorRgbaI32& color) {
  143. if (image) {
  144. if (INSIDE_XY) {
  145. ImageRgbaU8Impl::writePixel_unsafe(*image, x, y, image->packRgba(color.saturate()));
  146. }
  147. }
  148. }
  149. int32_t dsr::image_readPixel_border(const ImageU8& image, int32_t x, int32_t y, int32_t border) {
  150. if (image) {
  151. if (INSIDE_XY) {
  152. return ImageU8Impl::readPixel_unsafe(*image, x, y);
  153. } else {
  154. return border;
  155. }
  156. } else {
  157. return 0;
  158. }
  159. }
  160. int32_t dsr::image_readPixel_border(const ImageU16& image, int32_t x, int32_t y, int32_t border) {
  161. if (image) {
  162. if (INSIDE_XY) {
  163. return ImageU16Impl::readPixel_unsafe(*image, x, y);
  164. } else {
  165. return border;
  166. }
  167. } else {
  168. return 0;
  169. }
  170. }
  171. float dsr::image_readPixel_border(const ImageF32& image, int32_t x, int32_t y, float border) {
  172. if (image) {
  173. if (INSIDE_XY) {
  174. return ImageF32Impl::readPixel_unsafe(*image, x, y);
  175. } else {
  176. return border;
  177. }
  178. } else {
  179. return 0.0f;
  180. }
  181. }
  182. ColorRgbaI32 dsr::image_readPixel_border(const ImageRgbaU8& image, int32_t x, int32_t y, const ColorRgbaI32& border) {
  183. if (image) {
  184. if (INSIDE_XY) {
  185. return image->unpackRgba(ImageRgbaU8Impl::readPixel_unsafe(*image, x, y));
  186. } else {
  187. return border; // Can return unsaturated colors as error codes
  188. }
  189. } else {
  190. return ColorRgbaI32();
  191. }
  192. }
  193. uint8_t dsr::image_readPixel_clamp(const ImageU8& image, int32_t x, int32_t y) {
  194. if (image) {
  195. CLAMP_XY;
  196. return ImageU8Impl::readPixel_unsafe(*image, x, y);
  197. } else {
  198. return 0;
  199. }
  200. }
  201. uint16_t dsr::image_readPixel_clamp(const ImageU16& image, int32_t x, int32_t y) {
  202. if (image) {
  203. CLAMP_XY;
  204. return ImageU16Impl::readPixel_unsafe(*image, x, y);
  205. } else {
  206. return 0;
  207. }
  208. }
  209. float dsr::image_readPixel_clamp(const ImageF32& image, int32_t x, int32_t y) {
  210. if (image) {
  211. CLAMP_XY;
  212. return ImageF32Impl::readPixel_unsafe(*image, x, y);
  213. } else {
  214. return 0.0f;
  215. }
  216. }
  217. ColorRgbaI32 dsr::image_readPixel_clamp(const ImageRgbaU8& image, int32_t x, int32_t y) {
  218. if (image) {
  219. CLAMP_XY;
  220. return image->unpackRgba(ImageRgbaU8Impl::readPixel_unsafe(*image, x, y));
  221. } else {
  222. return ColorRgbaI32();
  223. }
  224. }
  225. uint8_t dsr::image_readPixel_tile(const ImageU8& image, int32_t x, int32_t y) {
  226. if (image) {
  227. TILE_XY;
  228. return ImageU8Impl::readPixel_unsafe(*image, x, y);
  229. } else {
  230. return 0;
  231. }
  232. }
  233. uint16_t dsr::image_readPixel_tile(const ImageU16& image, int32_t x, int32_t y) {
  234. if (image) {
  235. TILE_XY;
  236. return ImageU16Impl::readPixel_unsafe(*image, x, y);
  237. } else {
  238. return 0;
  239. }
  240. }
  241. float dsr::image_readPixel_tile(const ImageF32& image, int32_t x, int32_t y) {
  242. if (image) {
  243. TILE_XY;
  244. return ImageF32Impl::readPixel_unsafe(*image, x, y);
  245. } else {
  246. return 0.0f;
  247. }
  248. }
  249. ColorRgbaI32 dsr::image_readPixel_tile(const ImageRgbaU8& image, int32_t x, int32_t y) {
  250. if (image) {
  251. TILE_XY;
  252. return image->unpackRgba(ImageRgbaU8Impl::readPixel_unsafe(*image, x, y));
  253. } else {
  254. return ColorRgbaI32();
  255. }
  256. }
  257. void dsr::image_fill(ImageU8& image, int32_t color) {
  258. if (image) {
  259. imageImpl_draw_solidRectangle(*image, imageInternal::getBound(*image), color);
  260. }
  261. }
  262. void dsr::image_fill(ImageU16& image, int32_t color) {
  263. if (image) {
  264. imageImpl_draw_solidRectangle(*image, imageInternal::getBound(*image), color);
  265. }
  266. }
  267. void dsr::image_fill(ImageF32& image, float color) {
  268. if (image) {
  269. imageImpl_draw_solidRectangle(*image, imageInternal::getBound(*image), color);
  270. }
  271. }
  272. void dsr::image_fill(ImageRgbaU8& image, const ColorRgbaI32& color) {
  273. if (image) {
  274. imageImpl_draw_solidRectangle(*image, imageInternal::getBound(*image), color);
  275. }
  276. }
  277. AlignedImageU8 dsr::image_clone(const ImageU8& image) {
  278. if (image) {
  279. AlignedImageU8 result = image_create_U8(image->width, image->height);
  280. draw_copy(result, image);
  281. return result;
  282. } else {
  283. return AlignedImageU8(); // Null gives null
  284. }
  285. }
  286. AlignedImageU16 dsr::image_clone(const ImageU16& image) {
  287. if (image) {
  288. AlignedImageU16 result = image_create_U16(image->width, image->height);
  289. draw_copy(result, image);
  290. return result;
  291. } else {
  292. return AlignedImageU16(); // Null gives null
  293. }
  294. }
  295. AlignedImageF32 dsr::image_clone(const ImageF32& image) {
  296. if (image) {
  297. AlignedImageF32 result = image_create_F32(image->width, image->height);
  298. draw_copy(result, image);
  299. return result;
  300. } else {
  301. return AlignedImageF32(); // Null gives null
  302. }
  303. }
  304. OrderedImageRgbaU8 dsr::image_clone(const ImageRgbaU8& image) {
  305. if (image) {
  306. OrderedImageRgbaU8 result = image_create_RgbaU8(image->width, image->height);
  307. draw_copy(result, image);
  308. return result;
  309. } else {
  310. return OrderedImageRgbaU8(); // Null gives null
  311. }
  312. }
  313. ImageRgbaU8 dsr::image_removePadding(const ImageRgbaU8& image) {
  314. if (image) {
  315. // TODO: Copy the implementation of getWithoutPadding, to create ImageRgbaU8 directly
  316. return ImageRgbaU8(image->getWithoutPadding());
  317. } else {
  318. return ImageRgbaU8(); // Null gives null
  319. }
  320. }
  321. AlignedImageU8 dsr::image_get_red(const ImageRgbaU8& image) {
  322. if (image) {
  323. // TODO: Copy the implementation of getChannel, to create ImageU8 directly
  324. return AlignedImageU8(image->getChannel(image->packOrder.redIndex));
  325. } else {
  326. return AlignedImageU8(); // Null gives null
  327. }
  328. }
  329. AlignedImageU8 dsr::image_get_green(const ImageRgbaU8& image) {
  330. if (image) {
  331. // TODO: Copy the implementation of getChannel, to create ImageU8 directly
  332. return AlignedImageU8(image->getChannel(image->packOrder.greenIndex));
  333. } else {
  334. return AlignedImageU8(); // Null gives null
  335. }
  336. }
  337. AlignedImageU8 dsr::image_get_blue(const ImageRgbaU8& image) {
  338. if (image) {
  339. // TODO: Copy the implementation of getChannel, to create ImageU8 directly
  340. return AlignedImageU8(image->getChannel(image->packOrder.blueIndex));
  341. } else {
  342. return AlignedImageU8(); // Null gives null
  343. }
  344. }
  345. AlignedImageU8 dsr::image_get_alpha(const ImageRgbaU8& image) {
  346. if (image) {
  347. // TODO: Copy the implementation of getChannel, to create ImageU8 directly
  348. return AlignedImageU8(image->getChannel(image->packOrder.alphaIndex));
  349. } else {
  350. return AlignedImageU8(); // Null gives null
  351. }
  352. }
  353. static inline int32_t readColor(const ImageU8& channel, int x, int y) {
  354. return ImageU8Impl::readPixel_unsafe(*channel, x, y);
  355. }
  356. static inline int32_t readColor(int32_t color, int x, int y) {
  357. return color;
  358. }
  359. template <typename R, typename G, typename B, typename A>
  360. static OrderedImageRgbaU8 pack_template(int32_t width, int32_t height, R red, G green, B blue, A alpha) {
  361. OrderedImageRgbaU8 result = image_create_RgbaU8(width, height);
  362. for (int y = 0; y < height; y++) {
  363. for (int x = 0; x < width; x++) {
  364. ColorRgbaI32 color = ColorRgbaI32(readColor(red, x, y), readColor(green, x, y), readColor(blue, x, y), readColor(alpha, x, y));
  365. image_writePixel(result, x, y, color);
  366. }
  367. }
  368. return result;
  369. }
  370. #define PACK1(FIRST) \
  371. if (FIRST) { \
  372. return pack_template(FIRST->width, FIRST->height, red, green, blue, alpha); \
  373. } else { \
  374. return OrderedImageRgbaU8(); \
  375. }
  376. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, int32_t green, int32_t blue, int32_t alpha) { PACK1(red); }
  377. OrderedImageRgbaU8 dsr::image_pack(int32_t red, const ImageU8& green, int32_t blue, int32_t alpha) { PACK1(green); }
  378. OrderedImageRgbaU8 dsr::image_pack(int32_t red, int32_t green, const ImageU8& blue, int32_t alpha) { PACK1(blue); }
  379. OrderedImageRgbaU8 dsr::image_pack(int32_t red, int32_t green, int32_t blue, const ImageU8& alpha) { PACK1(alpha); }
  380. #define PACK2(FIRST,SECOND) \
  381. if (FIRST && SECOND) { \
  382. if (FIRST->width != SECOND->width || FIRST->height != SECOND->height) { \
  383. throwError("Cannot pack two channels of different size!\n"); \
  384. } \
  385. return pack_template(FIRST->width, FIRST->height, red, green, blue, alpha); \
  386. } else { \
  387. return OrderedImageRgbaU8(); \
  388. }
  389. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, const ImageU8& green, int32_t blue, int32_t alpha) { PACK2(red,green) }
  390. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, int32_t green, const ImageU8& blue, int32_t alpha) { PACK2(red,blue) }
  391. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, int32_t green, int32_t blue, const ImageU8& alpha) { PACK2(red,alpha) }
  392. OrderedImageRgbaU8 dsr::image_pack(int32_t red, const ImageU8& green, const ImageU8& blue, int32_t alpha) { PACK2(green,blue) }
  393. OrderedImageRgbaU8 dsr::image_pack(int32_t red, const ImageU8& green, int32_t blue, const ImageU8& alpha) { PACK2(green,alpha) }
  394. OrderedImageRgbaU8 dsr::image_pack(int32_t red, int32_t green, const ImageU8& blue, const ImageU8& alpha) { PACK2(blue,alpha) }
  395. #define PACK3(FIRST,SECOND,THIRD) \
  396. if (FIRST && SECOND && THIRD) { \
  397. if (FIRST->width != SECOND->width || FIRST->height != SECOND->height \
  398. || FIRST->width != THIRD->width || FIRST->height != THIRD->height) { \
  399. throwError("Cannot pack three channels of different size!\n"); \
  400. } \
  401. return pack_template(FIRST->width, FIRST->height, red, green, blue, alpha); \
  402. } else { \
  403. return OrderedImageRgbaU8(); \
  404. }
  405. OrderedImageRgbaU8 dsr::image_pack(int32_t red, const ImageU8& green, const ImageU8& blue, const ImageU8& alpha) { PACK3(green, blue, alpha) }
  406. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, int32_t green, const ImageU8& blue, const ImageU8& alpha) { PACK3(red, blue, alpha) }
  407. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, const ImageU8& green, int32_t blue, const ImageU8& alpha) { PACK3(red, green, alpha) }
  408. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, const ImageU8& green, const ImageU8& blue, int32_t alpha) { PACK3(red, green, blue) }
  409. // TODO: Optimize using zip instructions
  410. #define PACK4(FIRST,SECOND,THIRD,FOURTH) \
  411. if (FIRST && SECOND && THIRD && FOURTH) { \
  412. if (FIRST->width != SECOND->width || FIRST->height != SECOND->height \
  413. || FIRST->width != THIRD->width || FIRST->height != THIRD->height \
  414. || FIRST->width != FOURTH->width || FIRST->height != FOURTH->height) { \
  415. throwError("Cannot pack four channels of different size!\n"); \
  416. } \
  417. return pack_template(FIRST->width, FIRST->height, red, green, blue, alpha); \
  418. } else { \
  419. return OrderedImageRgbaU8(); \
  420. }
  421. OrderedImageRgbaU8 dsr::image_pack(const ImageU8& red, const ImageU8& green, const ImageU8& blue, const ImageU8& alpha) { PACK4(red, green, blue, alpha) }
  422. // Convert a grayscale image into an ascii image using the given alphabet.
  423. // Since all 256 characters cannot be in the alphabet, the encoding is lossy.
  424. // Each line is stored within <> to prevent text editors from removing meaningful white space.
  425. // The first line contains the given alphabet as a gradient from black to white.
  426. // Preconditions:
  427. // alphabet may not have extended ascii, non printable, '\', '"', '>' or linebreak
  428. // width <= stride
  429. // size of monochromeImage = height * stride
  430. // Example alphabet: " .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  431. String dsr::image_toAscii(const ImageU8& image, const String& alphabet) {
  432. if (!image_exists(image)) {
  433. return U"null";
  434. }
  435. String result;
  436. char alphabetMap[256];
  437. int alphabetSize = alphabet.length();
  438. int width = image_getWidth(image);
  439. int height = image_getHeight(image);
  440. result.reserve(((width + 4) * height) + alphabetSize + 5);
  441. double scale = (double)(alphabetSize - 1) / 255.0;
  442. double output = 0.49;
  443. for (int rawValue = 0; rawValue < 256; rawValue++) {
  444. int charIndex = (int)output;
  445. if (charIndex < 0) charIndex = 0;
  446. if (charIndex > alphabetSize - 1) charIndex = alphabetSize - 1;
  447. alphabetMap[rawValue] = alphabet[charIndex];
  448. output += scale;
  449. }
  450. result.appendChar(U'<');
  451. for (int charIndex = 0; charIndex < alphabetSize; charIndex++) {
  452. result.appendChar(alphabet[charIndex]);
  453. }
  454. result.append(U">\n");
  455. for (int y = 0; y < height; y++) {
  456. result.appendChar(U'<');
  457. for (int x = 0; x < width; x++) {
  458. result.appendChar(alphabetMap[image_readPixel_clamp(image, x, y)]);
  459. }
  460. result.append(U">\n");
  461. }
  462. return result;
  463. }
  464. String dsr::image_toAscii(const ImageU8& image) {
  465. return image_toAscii(image, U" .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  466. }
  467. // Create a monochrome image from the ascii image in content.
  468. // String is used instead of ReadableString, so that the content can be decompressed from 8-bit strings in the binary.
  469. AlignedImageU8 dsr::image_fromAscii(const String& content) {
  470. char alphabet[128];
  471. uint8_t alphabetMap[128];
  472. char current;
  473. int x = 0;
  474. int y = -1;
  475. int width = 0;
  476. int height = 0;
  477. int alphabetSize = 0;
  478. int contentSize = content.length();
  479. bool quoted = false;
  480. int i = 0;
  481. while (i < contentSize && ((current = content[i]) != '\0')) {
  482. if (quoted) {
  483. if (y < 0) {
  484. if (current == '>') {
  485. quoted = false;
  486. y = 0;
  487. } else if (alphabetSize < 128) {
  488. alphabet[alphabetSize] = current;
  489. alphabetSize++;
  490. }
  491. } else {
  492. if (current == '>') {
  493. quoted = false;
  494. if (width < x) width = x;
  495. y++;
  496. x = 0;
  497. } else {
  498. x++;
  499. }
  500. }
  501. } else if (current == '<') {
  502. quoted = true;
  503. }
  504. i++;
  505. }
  506. if (alphabetSize < 2) {
  507. throwError(U"The alphabet needs at least two characters!");
  508. }
  509. height = y;
  510. if (x > 0) {
  511. throwError(U"All ascii images must end with a linebreak!");
  512. }
  513. for (i = 0; i < 128; i++) {
  514. alphabetMap[i] = 0;
  515. }
  516. for (i = 0; i < alphabetSize; i++) {
  517. int code = (int)(alphabet[i]);
  518. if (code < 32 || code > 126) {
  519. throwError(U"Ascii image contained non-printable standard ascii! Use codes 32 to 126.");
  520. }
  521. if (alphabetMap[code] > 0) {
  522. throwError(U"A character in the alphabet was used more than once!");
  523. }
  524. int value = (int)(((double)i) * (255.0f / ((double)(alphabetSize - 1))));
  525. if (value < 0) value = 0;
  526. if (value > 255) value = 255;
  527. alphabetMap[code] = value;
  528. }
  529. if (width <= 0 || height <= 0) {
  530. throwError(U"An ascii image had zero dimensions!");
  531. }
  532. AlignedImageU8 result = image_create_U8(width, height);
  533. x = 0; y = -1;
  534. quoted = false;
  535. i = 0;
  536. while (i < contentSize && ((current = content[i]) != '\0')) {
  537. if (quoted) {
  538. if (current == '>') {
  539. quoted = false;
  540. if (y >= 0 && x != width) {
  541. throwError(U"Lines in the ascii image do not have the same lengths.");
  542. }
  543. y++;
  544. x = 0;
  545. } else if (y >= 0) {
  546. int code = (int)current;
  547. if (code < 0) code = 0;
  548. if (code > 127) code = 127;
  549. image_writePixel(result, x, y, alphabetMap[code]);
  550. x++;
  551. }
  552. } else if (current == '<') {
  553. quoted = true;
  554. }
  555. i++;
  556. }
  557. return result;
  558. }
  559. // TODO: Try to recycle the memory to reduce overhead from heap allocating heads pointing to existing buffers
  560. template <typename IMAGE_TYPE, typename VALUE_TYPE>
  561. static inline IMAGE_TYPE subImage_template(const IMAGE_TYPE& image, const IRect& region) {
  562. if (image) {
  563. IRect cut = IRect::cut(imageInternal::getBound(*image), region);
  564. if (cut.hasArea()) {
  565. intptr_t newOffset = image->startOffset + (cut.left() * image->pixelSize) + (cut.top() * image->stride);
  566. return IMAGE_TYPE(std::make_shared<VALUE_TYPE>(cut.width(), cut.height(), image->stride, image->buffer, newOffset));
  567. }
  568. }
  569. return IMAGE_TYPE(); // Null if where are no overlapping pixels
  570. }
  571. template <typename IMAGE_TYPE, typename VALUE_TYPE>
  572. static inline IMAGE_TYPE subImage_template_withPackOrder(const IMAGE_TYPE& image, const IRect& region) {
  573. if (image) {
  574. IRect cut = IRect::cut(imageInternal::getBound(*image), region);
  575. if (cut.hasArea()) {
  576. intptr_t newOffset = image->startOffset + (cut.left() * image->pixelSize) + (cut.top() * image->stride);
  577. return IMAGE_TYPE(std::make_shared<VALUE_TYPE>(cut.width(), cut.height(), image->stride, image->buffer, newOffset, image->packOrder));
  578. }
  579. }
  580. return IMAGE_TYPE(); // Null if where are no overlapping pixels
  581. }
  582. ImageU8 dsr::image_getSubImage(const ImageU8& image, const IRect& region) {
  583. return subImage_template<ImageU8, ImageU8Impl>(image, region);
  584. }
  585. ImageU16 dsr::image_getSubImage(const ImageU16& image, const IRect& region) {
  586. return subImage_template<ImageU16, ImageU16Impl>(image, region);
  587. }
  588. ImageF32 dsr::image_getSubImage(const ImageF32& image, const IRect& region) {
  589. return subImage_template<ImageF32, ImageF32Impl>(image, region);
  590. }
  591. ImageRgbaU8 dsr::image_getSubImage(const ImageRgbaU8& image, const IRect& region) {
  592. return subImage_template_withPackOrder<ImageRgbaU8, ImageRgbaU8Impl>(image, region);
  593. }
  594. template <typename IMAGE_TYPE, int CHANNELS, typename ELEMENT_TYPE>
  595. ELEMENT_TYPE maxDifference_template(const IMAGE_TYPE& imageA, const IMAGE_TYPE& imageB) {
  596. if (imageA.width != imageB.width || imageA.height != imageB.height) {
  597. return std::numeric_limits<ELEMENT_TYPE>::max();
  598. } else {
  599. ELEMENT_TYPE maxDifference = 0;
  600. const SafePointer<ELEMENT_TYPE> rowDataA = imageInternal::getSafeData<ELEMENT_TYPE>(imageA);
  601. const SafePointer<ELEMENT_TYPE> rowDataB = imageInternal::getSafeData<ELEMENT_TYPE>(imageB);
  602. for (int y = 0; y < imageA.height; y++) {
  603. const SafePointer<ELEMENT_TYPE> pixelDataA = rowDataA;
  604. const SafePointer<ELEMENT_TYPE> pixelDataB = rowDataB;
  605. for (int x = 0; x < imageA.width; x++) {
  606. for (int c = 0; c < CHANNELS; c++) {
  607. ELEMENT_TYPE difference = absDiff(*pixelDataA, *pixelDataB);
  608. if (difference > maxDifference) {
  609. maxDifference = difference;
  610. }
  611. pixelDataA += 1;
  612. pixelDataB += 1;
  613. }
  614. }
  615. rowDataA.increaseBytes(imageA.stride);
  616. rowDataB.increaseBytes(imageB.stride);
  617. }
  618. return maxDifference;
  619. }
  620. }
  621. uint8_t dsr::image_maxDifference(const ImageU8& imageA, const ImageU8& imageB) {
  622. if (imageA && imageB) {
  623. return maxDifference_template<ImageU8Impl, 1, uint8_t>(*imageA, *imageB);
  624. } else {
  625. return std::numeric_limits<uint8_t>::infinity();
  626. }
  627. }
  628. uint16_t dsr::image_maxDifference(const ImageU16& imageA, const ImageU16& imageB) {
  629. if (imageA && imageB) {
  630. return maxDifference_template<ImageU16Impl, 1, uint16_t>(*imageA, *imageB);
  631. } else {
  632. return std::numeric_limits<uint16_t>::infinity();
  633. }
  634. }
  635. float dsr::image_maxDifference(const ImageF32& imageA, const ImageF32& imageB) {
  636. if (imageA && imageB) {
  637. return maxDifference_template<ImageF32Impl, 1, float>(*imageA, *imageB);
  638. } else {
  639. return std::numeric_limits<float>::infinity();
  640. }
  641. }
  642. uint8_t dsr::image_maxDifference(const ImageRgbaU8& imageA, const ImageRgbaU8& imageB) {
  643. if (imageA && imageB) {
  644. return maxDifference_template<ImageRgbaU8Impl, 4, uint8_t>(*imageA, *imageB);
  645. } else {
  646. return std::numeric_limits<uint8_t>::infinity();
  647. }
  648. }
  649. SafePointer<uint8_t> dsr::image_getSafePointer(const ImageU8& image, int rowIndex) {
  650. if (image) {
  651. return imageInternal::getSafeData<uint8_t>(image.get(), rowIndex);
  652. } else {
  653. return SafePointer<uint8_t>();
  654. }
  655. }
  656. SafePointer<uint16_t> dsr::image_getSafePointer(const ImageU16& image, int rowIndex) {
  657. if (image) {
  658. return imageInternal::getSafeData<uint16_t>(image.get(), rowIndex);
  659. } else {
  660. return SafePointer<uint16_t>();
  661. }
  662. }
  663. SafePointer<float> dsr::image_getSafePointer(const ImageF32& image, int rowIndex) {
  664. if (image) {
  665. return imageInternal::getSafeData<float>(image.get(), rowIndex);
  666. } else {
  667. return SafePointer<float>();
  668. }
  669. }
  670. SafePointer<uint32_t> dsr::image_getSafePointer(const ImageRgbaU8& image, int rowIndex) {
  671. if (image) {
  672. return imageInternal::getSafeData<uint32_t>(image.get(), rowIndex);
  673. } else {
  674. return SafePointer<uint32_t>();
  675. }
  676. }
  677. SafePointer<uint8_t> dsr::image_getSafePointer_channels(const ImageRgbaU8& image, int rowIndex) {
  678. if (image) {
  679. return imageInternal::getSafeData<uint8_t>(image.get(), rowIndex);
  680. } else {
  681. return SafePointer<uint8_t>();
  682. }
  683. }
  684. void dsr::image_dangerous_replaceDestructor(ImageU8& image, const std::function<void(uint8_t *)>& newDestructor) {
  685. if (image) { return image->buffer->replaceDestructor(newDestructor); }
  686. }
  687. void dsr::image_dangerous_replaceDestructor(ImageU16& image, const std::function<void(uint8_t *)>& newDestructor) {
  688. if (image) { return image->buffer->replaceDestructor(newDestructor); }
  689. }
  690. void dsr::image_dangerous_replaceDestructor(ImageF32& image, const std::function<void(uint8_t *)>& newDestructor) {
  691. if (image) { return image->buffer->replaceDestructor(newDestructor); }
  692. }
  693. void dsr::image_dangerous_replaceDestructor(ImageRgbaU8& image, const std::function<void(uint8_t *)>& newDestructor) {
  694. if (image) { return image->buffer->replaceDestructor(newDestructor); }
  695. }
  696. uint8_t* dsr::image_dangerous_getData(const ImageU8& image) {
  697. if (image) {
  698. return imageInternal::getSafeData<uint8_t>(*image).getUnsafe();
  699. } else {
  700. return nullptr;
  701. }
  702. }
  703. uint8_t* dsr::image_dangerous_getData(const ImageU16& image) {
  704. if (image) {
  705. return imageInternal::getSafeData<uint8_t>(*image).getUnsafe();
  706. } else {
  707. return nullptr;
  708. }
  709. }
  710. uint8_t* dsr::image_dangerous_getData(const ImageF32& image) {
  711. if (image) {
  712. return imageInternal::getSafeData<uint8_t>(*image).getUnsafe();
  713. } else {
  714. return nullptr;
  715. }
  716. }
  717. uint8_t* dsr::image_dangerous_getData(const ImageRgbaU8& image) {
  718. if (image) {
  719. return imageInternal::getSafeData<uint8_t>(*image).getUnsafe();
  720. } else {
  721. return nullptr;
  722. }
  723. }