imageAPI.cpp 27 KB

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