image.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  1. /*************************************************************************/
  2. /* image.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "image.h"
  31. #include "core/io/image_loader.h"
  32. #include "core/os/copymem.h"
  33. #include "hash_map.h"
  34. #include "print_string.h"
  35. #include "thirdparty/misc/hq2x.h"
  36. #include <stdio.h>
  37. const char *Image::format_names[Image::FORMAT_MAX] = {
  38. "Grayscale",
  39. "Intensity",
  40. "GrayscaleAlpha",
  41. "RGB",
  42. "RGBA",
  43. "Indexed",
  44. "IndexedAlpha",
  45. "YUV422",
  46. "YUV444",
  47. "BC1",
  48. "BC2",
  49. "BC3",
  50. "BC4",
  51. "BC5",
  52. "PVRTC2",
  53. "PVRTC2Alpha",
  54. "PVRTC4",
  55. "PVRTC4Alpha",
  56. "ETC",
  57. "ATC",
  58. "ATCAlphaExp",
  59. "ATCAlphaInterp",
  60. };
  61. SavePNGFunc Image::save_png_func = NULL;
  62. void Image::_put_pixel(int p_x, int p_y, const BColor &p_color, unsigned char *p_data) {
  63. _put_pixelw(p_x, p_y, width, p_color, p_data);
  64. }
  65. void Image::_put_pixelw(int p_x, int p_y, int p_width, const BColor &p_color, unsigned char *p_data) {
  66. int ofs = p_y * p_width + p_x;
  67. switch (format) {
  68. case FORMAT_GRAYSCALE: {
  69. p_data[ofs] = p_color.gray();
  70. } break;
  71. case FORMAT_INTENSITY: {
  72. p_data[ofs] = p_color.a;
  73. } break;
  74. case FORMAT_GRAYSCALE_ALPHA: {
  75. p_data[ofs * 2] = p_color.gray();
  76. p_data[ofs * 2 + 1] = p_color.a;
  77. } break;
  78. case FORMAT_RGB: {
  79. p_data[ofs * 3 + 0] = p_color.r;
  80. p_data[ofs * 3 + 1] = p_color.g;
  81. p_data[ofs * 3 + 2] = p_color.b;
  82. } break;
  83. case FORMAT_RGBA: {
  84. p_data[ofs * 4 + 0] = p_color.r;
  85. p_data[ofs * 4 + 1] = p_color.g;
  86. p_data[ofs * 4 + 2] = p_color.b;
  87. p_data[ofs * 4 + 3] = p_color.a;
  88. } break;
  89. case FORMAT_INDEXED:
  90. case FORMAT_INDEXED_ALPHA: {
  91. ERR_FAIL();
  92. } break;
  93. default: {};
  94. }
  95. }
  96. void Image::_get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const {
  97. int w = width;
  98. int h = height;
  99. int ofs = 0;
  100. int pixel_size = get_format_pixel_size(format);
  101. int pixel_rshift = get_format_pixel_rshift(format);
  102. int minw, minh;
  103. _get_format_min_data_size(format, minw, minh);
  104. for (int i = 0; i < p_mipmap; i++) {
  105. int s = w * h;
  106. s *= pixel_size;
  107. s >>= pixel_rshift;
  108. ofs += s;
  109. w = MAX(minw, w >> 1);
  110. h = MAX(minh, h >> 1);
  111. }
  112. r_offset = ofs;
  113. r_width = w;
  114. r_height = h;
  115. }
  116. int Image::get_mipmap_offset(int p_mipmap) const {
  117. ERR_FAIL_INDEX_V(p_mipmap, (mipmaps + 1), -1);
  118. int ofs, w, h;
  119. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  120. return ofs;
  121. }
  122. void Image::get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const {
  123. int ofs, w, h;
  124. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  125. int ofs2;
  126. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h);
  127. r_ofs = ofs;
  128. r_size = ofs2 - ofs;
  129. }
  130. void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const {
  131. int ofs;
  132. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  133. int ofs2, w2, h2;
  134. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w2, h2);
  135. r_ofs = ofs;
  136. r_size = ofs2 - ofs;
  137. }
  138. void Image::put_pixel(int p_x, int p_y, const Color &p_color, int p_mipmap) {
  139. ERR_FAIL_INDEX(p_mipmap, mipmaps + 1);
  140. int ofs, w, h;
  141. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  142. ERR_FAIL_INDEX(p_x, w);
  143. ERR_FAIL_INDEX(p_y, h);
  144. DVector<uint8_t>::Write wp = data.write();
  145. unsigned char *data_ptr = wp.ptr();
  146. _put_pixelw(p_x, p_y, w, BColor(p_color.r * 255, p_color.g * 255, p_color.b * 255, p_color.a * 255), &data_ptr[ofs]);
  147. }
  148. Image::BColor Image::_get_pixel(int p_x, int p_y, const unsigned char *p_data, int p_data_size) const {
  149. return _get_pixelw(p_x, p_y, width, p_data, p_data_size);
  150. }
  151. Image::BColor Image::_get_pixelw(int p_x, int p_y, int p_width, const unsigned char *p_data, int p_data_size) const {
  152. int ofs = p_y * p_width + p_x;
  153. BColor result(0, 0, 0, 0);
  154. switch (format) {
  155. case FORMAT_GRAYSCALE: {
  156. result = BColor(p_data[ofs], p_data[ofs], p_data[ofs], 255.0);
  157. } break;
  158. case FORMAT_INTENSITY: {
  159. result = BColor(255, 255, 255, p_data[ofs]);
  160. } break;
  161. case FORMAT_GRAYSCALE_ALPHA: {
  162. result = BColor(p_data[ofs * 2], p_data[ofs * 2], p_data[ofs * 2], p_data[ofs * 2 + 1]);
  163. } break;
  164. case FORMAT_RGB: {
  165. result = BColor(p_data[ofs * 3], p_data[ofs * 3 + 1], p_data[ofs * 3 + 2]);
  166. } break;
  167. case FORMAT_RGBA: {
  168. result = BColor(p_data[ofs * 4], p_data[ofs * 4 + 1], p_data[ofs * 4 + 2], p_data[ofs * 4 + 3]);
  169. } break;
  170. case FORMAT_INDEXED_ALPHA: {
  171. int pitch = 4;
  172. const uint8_t *pal = &p_data[p_data_size - pitch * 256];
  173. int idx = p_data[ofs];
  174. result = BColor(pal[idx * pitch + 0], pal[idx * pitch + 1], pal[idx * pitch + 2], pal[idx * pitch + 3]);
  175. } break;
  176. case FORMAT_INDEXED: {
  177. int pitch = 3;
  178. const uint8_t *pal = &p_data[p_data_size - pitch * 256];
  179. int idx = p_data[ofs];
  180. result = BColor(pal[idx * pitch + 0], pal[idx * pitch + 1], pal[idx * pitch + 2], 255);
  181. } break;
  182. case FORMAT_YUV_422: {
  183. int y, u, v;
  184. if (p_x % 2) {
  185. const uint8_t *yp = &p_data[p_width * 2 * p_y + p_x * 2];
  186. u = *(yp - 1);
  187. y = yp[0];
  188. v = yp[1];
  189. } else {
  190. const uint8_t *yp = &p_data[p_width * 2 * p_y + p_x * 2];
  191. y = yp[0];
  192. u = yp[1];
  193. v = yp[3];
  194. };
  195. int32_t r = 1.164 * (y - 16) + 1.596 * (v - 128);
  196. int32_t g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  197. int32_t b = 1.164 * (y - 16) + 2.018 * (u - 128);
  198. result = BColor(CLAMP(r, 0, 255), CLAMP(g, 0, 255), CLAMP(b, 0, 255));
  199. } break;
  200. case FORMAT_YUV_444: {
  201. uint8_t y, u, v;
  202. const uint8_t *yp = &p_data[p_width * 3 * p_y + p_x * 3];
  203. y = yp[0];
  204. u = yp[1];
  205. v = yp[2];
  206. int32_t r = 1.164 * (y - 16) + 1.596 * (v - 128);
  207. int32_t g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  208. int32_t b = 1.164 * (y - 16) + 2.018 * (u - 128);
  209. result = BColor(CLAMP(r, 0, 255), CLAMP(g, 0, 255), CLAMP(b, 0, 255));
  210. } break;
  211. default: {}
  212. }
  213. return result;
  214. }
  215. void Image::put_indexed_pixel(int p_x, int p_y, uint8_t p_idx, int p_mipmap) {
  216. ERR_FAIL_COND(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA);
  217. ERR_FAIL_INDEX(p_mipmap, mipmaps + 1);
  218. int ofs, w, h;
  219. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  220. ERR_FAIL_INDEX(p_x, w);
  221. ERR_FAIL_INDEX(p_y, h);
  222. data.set(ofs + p_y * w + p_x, p_idx);
  223. };
  224. uint8_t Image::get_indexed_pixel(int p_x, int p_y, int p_mipmap) const {
  225. ERR_FAIL_COND_V(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA, 0);
  226. ERR_FAIL_INDEX_V(p_mipmap, mipmaps + 1, 0);
  227. int ofs, w, h;
  228. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  229. ERR_FAIL_INDEX_V(p_x, w, 0);
  230. ERR_FAIL_INDEX_V(p_y, h, 0);
  231. return data[ofs + p_y * w + p_x];
  232. };
  233. void Image::set_pallete(const DVector<uint8_t> &p_data) {
  234. int len = p_data.size();
  235. ERR_FAIL_COND(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA);
  236. ERR_FAIL_COND(format == FORMAT_INDEXED && len != (256 * 3));
  237. ERR_FAIL_COND(format == FORMAT_INDEXED_ALPHA && len != (256 * 4));
  238. int ofs, w, h;
  239. _get_mipmap_offset_and_size(mipmaps + 1, ofs, w, h);
  240. int pal_ofs = ofs;
  241. data.resize(pal_ofs + p_data.size());
  242. DVector<uint8_t>::Write wp = data.write();
  243. unsigned char *dst = wp.ptr() + pal_ofs;
  244. DVector<uint8_t>::Read r = p_data.read();
  245. const unsigned char *src = r.ptr();
  246. copymem(dst, src, len);
  247. };
  248. int Image::get_width() const {
  249. return width;
  250. }
  251. int Image::get_height() const {
  252. return height;
  253. }
  254. int Image::get_mipmaps() const {
  255. return mipmaps;
  256. }
  257. Color Image::get_pixel(int p_x, int p_y, int p_mipmap) const {
  258. ERR_FAIL_INDEX_V(p_mipmap, mipmaps + 1, Color());
  259. int ofs, w, h;
  260. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  261. ERR_FAIL_INDEX_V(p_x, w, Color());
  262. ERR_FAIL_INDEX_V(p_y, h, Color());
  263. int len = data.size();
  264. DVector<uint8_t>::Read r = data.read();
  265. const unsigned char *data_ptr = r.ptr();
  266. BColor c = _get_pixelw(p_x, p_y, w, &data_ptr[ofs], len);
  267. return Color(c.r / 255.0, c.g / 255.0, c.b / 255.0, c.a / 255.0);
  268. }
  269. void Image::convert(Format p_new_format) {
  270. if (data.size() == 0)
  271. return;
  272. if (p_new_format == format)
  273. return;
  274. if (format >= FORMAT_BC1 || p_new_format >= FORMAT_BC1) {
  275. ERR_EXPLAIN("Cannot convert to <-> from compressed/custom image formats (for now).");
  276. ERR_FAIL();
  277. }
  278. if (p_new_format == FORMAT_INDEXED || p_new_format == FORMAT_INDEXED_ALPHA) {
  279. return;
  280. }
  281. Image new_img(width, height, 0, p_new_format);
  282. int len = data.size();
  283. DVector<uint8_t>::Read r = data.read();
  284. DVector<uint8_t>::Write w = new_img.data.write();
  285. const uint8_t *rptr = r.ptr();
  286. uint8_t *wptr = w.ptr();
  287. if (p_new_format == FORMAT_RGBA && format == FORMAT_INDEXED_ALPHA) {
  288. //optimized unquantized form
  289. int dataend = len - 256 * 4;
  290. const uint32_t *palpos = (const uint32_t *)&rptr[dataend];
  291. uint32_t *dst32 = (uint32_t *)wptr;
  292. for (int i = 0; i < dataend; i++)
  293. dst32[i] = palpos[rptr[i]]; //since this is read/write, endianness is not a problem
  294. } else {
  295. //this is temporary, must find a faster way to do it.
  296. for (int i = 0; i < width; i++)
  297. for (int j = 0; j < height; j++)
  298. new_img._put_pixel(i, j, _get_pixel(i, j, rptr, len), wptr);
  299. }
  300. r = DVector<uint8_t>::Read();
  301. w = DVector<uint8_t>::Write();
  302. bool gen_mipmaps = mipmaps > 0;
  303. *this = new_img;
  304. if (gen_mipmaps)
  305. generate_mipmaps();
  306. }
  307. Image::Format Image::get_format() const {
  308. return format;
  309. }
  310. static double _bicubic_interp_kernel(double x) {
  311. x = ABS(x);
  312. double bc = 0;
  313. if (x <= 1)
  314. bc = (1.5 * x - 2.5) * x * x + 1;
  315. else if (x < 2)
  316. bc = ((-0.5 * x + 2.5) * x - 4) * x + 2;
  317. return bc;
  318. }
  319. template <int CC>
  320. static void _scale_cubic(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  321. // get source image size
  322. int width = p_src_width;
  323. int height = p_src_height;
  324. double xfac = (double)width / p_dst_width;
  325. double yfac = (double)height / p_dst_height;
  326. // coordinates of source points and cooefficiens
  327. double ox, oy, dx, dy, k1, k2;
  328. int ox1, oy1, ox2, oy2;
  329. // destination pixel values
  330. // width and height decreased by 1
  331. int ymax = height - 1;
  332. int xmax = width - 1;
  333. // temporary pointer
  334. for (int y = 0; y < p_dst_height; y++) {
  335. // Y coordinates
  336. oy = (double)y * yfac - 0.5f;
  337. oy1 = (int)oy;
  338. dy = oy - (double)oy1;
  339. for (int x = 0; x < p_dst_width; x++) {
  340. // X coordinates
  341. ox = (double)x * xfac - 0.5f;
  342. ox1 = (int)ox;
  343. dx = ox - (double)ox1;
  344. // initial pixel value
  345. uint8_t *dst = p_dst + (y * p_dst_width + x) * CC;
  346. double color[CC];
  347. for (int i = 0; i < CC; i++) {
  348. color[i] = 0;
  349. }
  350. for (int n = -1; n < 3; n++) {
  351. // get Y cooefficient
  352. k1 = _bicubic_interp_kernel(dy - (double)n);
  353. oy2 = oy1 + n;
  354. if (oy2 < 0)
  355. oy2 = 0;
  356. if (oy2 > ymax)
  357. oy2 = ymax;
  358. for (int m = -1; m < 3; m++) {
  359. // get X cooefficient
  360. k2 = k1 * _bicubic_interp_kernel((double)m - dx);
  361. ox2 = ox1 + m;
  362. if (ox2 < 0)
  363. ox2 = 0;
  364. if (ox2 > xmax)
  365. ox2 = xmax;
  366. // get pixel of original image
  367. const uint8_t *p = p_src + (oy2 * p_src_width + ox2) * CC;
  368. for (int i = 0; i < CC; i++) {
  369. color[i] += p[i] * k2;
  370. }
  371. }
  372. }
  373. for (int i = 0; i < CC; i++) {
  374. dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255);
  375. }
  376. }
  377. }
  378. }
  379. template <int CC>
  380. static void _scale_bilinear(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  381. enum {
  382. FRAC_BITS = 8,
  383. FRAC_LEN = (1 << FRAC_BITS),
  384. FRAC_MASK = FRAC_LEN - 1
  385. };
  386. for (uint32_t i = 0; i < p_dst_height; i++) {
  387. uint32_t src_yofs_up_fp = (i * p_src_height * FRAC_LEN / p_dst_height);
  388. uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK;
  389. uint32_t src_yofs_up = src_yofs_up_fp >> FRAC_BITS;
  390. uint32_t src_yofs_down = (i + 1) * p_src_height / p_dst_height;
  391. if (src_yofs_down >= p_src_height)
  392. src_yofs_down = p_src_height - 1;
  393. //src_yofs_up*=CC;
  394. //src_yofs_down*=CC;
  395. uint32_t y_ofs_up = src_yofs_up * p_src_width * CC;
  396. uint32_t y_ofs_down = src_yofs_down * p_src_width * CC;
  397. for (uint32_t j = 0; j < p_dst_width; j++) {
  398. uint32_t src_xofs_left_fp = (j * p_src_width * FRAC_LEN / p_dst_width);
  399. uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK;
  400. uint32_t src_xofs_left = src_xofs_left_fp >> FRAC_BITS;
  401. uint32_t src_xofs_right = (j + 1) * p_src_width / p_dst_width;
  402. if (src_xofs_right >= p_src_width)
  403. src_xofs_right = p_src_width - 1;
  404. src_xofs_left *= CC;
  405. src_xofs_right *= CC;
  406. for (uint32_t l = 0; l < CC; l++) {
  407. uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS;
  408. uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS;
  409. uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS;
  410. uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS;
  411. uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS);
  412. uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS);
  413. uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS);
  414. interp >>= FRAC_BITS;
  415. p_dst[i * p_dst_width * CC + j * CC + l] = interp;
  416. }
  417. }
  418. }
  419. }
  420. template <int CC>
  421. static void _scale_nearest(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  422. for (uint32_t i = 0; i < p_dst_height; i++) {
  423. uint32_t src_yofs = i * p_src_height / p_dst_height;
  424. uint32_t y_ofs = src_yofs * p_src_width * CC;
  425. for (uint32_t j = 0; j < p_dst_width; j++) {
  426. uint32_t src_xofs = j * p_src_width / p_dst_width;
  427. src_xofs *= CC;
  428. for (uint32_t l = 0; l < CC; l++) {
  429. uint32_t p = p_src[y_ofs + src_xofs + l];
  430. p_dst[i * p_dst_width * CC + j * CC + l] = p;
  431. }
  432. }
  433. }
  434. }
  435. void Image::resize_to_po2(bool p_square) {
  436. if (!_can_modify(format)) {
  437. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  438. ERR_FAIL();
  439. }
  440. int w = next_power_of_2(width);
  441. int h = next_power_of_2(height);
  442. if (w == width && h == height) {
  443. if (!p_square || w == h)
  444. return; //nothing to do
  445. }
  446. resize(w, h);
  447. }
  448. Image Image::resized(int p_width, int p_height, int p_interpolation) {
  449. Image ret = *this;
  450. ret.resize(p_width, p_height, (Interpolation)p_interpolation);
  451. return ret;
  452. };
  453. void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
  454. if (!_can_modify(format)) {
  455. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  456. ERR_FAIL();
  457. }
  458. ERR_FAIL_COND(p_width <= 0);
  459. ERR_FAIL_COND(p_height <= 0);
  460. ERR_FAIL_COND(p_width > MAX_WIDTH);
  461. ERR_FAIL_COND(p_height > MAX_HEIGHT);
  462. if (p_width == width && p_height == height)
  463. return;
  464. Image dst(p_width, p_height, 0, format);
  465. if (format == FORMAT_INDEXED)
  466. p_interpolation = INTERPOLATE_NEAREST;
  467. DVector<uint8_t>::Read r = data.read();
  468. const unsigned char *r_ptr = r.ptr();
  469. DVector<uint8_t>::Write w = dst.data.write();
  470. unsigned char *w_ptr = w.ptr();
  471. switch (p_interpolation) {
  472. case INTERPOLATE_NEAREST: {
  473. switch (get_format_pixel_size(format)) {
  474. case 1: _scale_nearest<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  475. case 2: _scale_nearest<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  476. case 3: _scale_nearest<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  477. case 4: _scale_nearest<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  478. }
  479. } break;
  480. case INTERPOLATE_BILINEAR: {
  481. switch (get_format_pixel_size(format)) {
  482. case 1: _scale_bilinear<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  483. case 2: _scale_bilinear<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  484. case 3: _scale_bilinear<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  485. case 4: _scale_bilinear<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  486. }
  487. } break;
  488. case INTERPOLATE_CUBIC: {
  489. switch (get_format_pixel_size(format)) {
  490. case 1: _scale_cubic<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  491. case 2: _scale_cubic<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  492. case 3: _scale_cubic<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  493. case 4: _scale_cubic<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  494. }
  495. } break;
  496. }
  497. r = DVector<uint8_t>::Read();
  498. w = DVector<uint8_t>::Write();
  499. if (mipmaps > 0)
  500. dst.generate_mipmaps();
  501. *this = dst;
  502. }
  503. void Image::crop(int p_width, int p_height) {
  504. if (!_can_modify(format)) {
  505. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  506. ERR_FAIL();
  507. }
  508. ERR_FAIL_COND(p_width <= 0);
  509. ERR_FAIL_COND(p_height <= 0);
  510. ERR_FAIL_COND(p_width > MAX_WIDTH);
  511. ERR_FAIL_COND(p_height > MAX_HEIGHT);
  512. /* to save memory, cropping should be done in-place, however, since this function
  513. will most likely either not be used much, or in critical areas, for now it wont, because
  514. it's a waste of time. */
  515. if (p_width == width && p_height == height)
  516. return;
  517. Image dst(p_width, p_height, 0, format);
  518. for (int y = 0; y < p_height; y++) {
  519. for (int x = 0; x < p_width; x++) {
  520. Color col = (x >= width || y >= height) ? Color() : get_pixel(x, y);
  521. dst.put_pixel(x, y, col);
  522. }
  523. }
  524. if (mipmaps > 0)
  525. dst.generate_mipmaps();
  526. *this = dst;
  527. }
  528. void Image::flip_y() {
  529. if (!_can_modify(format)) {
  530. ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats.");
  531. ERR_FAIL();
  532. }
  533. bool gm = mipmaps;
  534. if (gm)
  535. clear_mipmaps();
  536. for (int y = 0; y < (height / 2); y++) {
  537. for (int x = 0; x < width; x++) {
  538. Color up = get_pixel(x, y);
  539. Color down = get_pixel(x, height - y - 1);
  540. put_pixel(x, y, down);
  541. put_pixel(x, height - y - 1, up);
  542. }
  543. }
  544. if (gm)
  545. generate_mipmaps();
  546. }
  547. void Image::flip_x() {
  548. if (!_can_modify(format)) {
  549. ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats.");
  550. ERR_FAIL();
  551. }
  552. bool gm = mipmaps;
  553. if (gm)
  554. clear_mipmaps();
  555. for (int y = 0; y < (height / 2); y++) {
  556. for (int x = 0; x < width; x++) {
  557. Color up = get_pixel(x, y);
  558. Color down = get_pixel(width - x - 1, y);
  559. put_pixel(x, y, down);
  560. put_pixel(width - x - 1, y, up);
  561. }
  562. }
  563. if (gm)
  564. generate_mipmaps();
  565. }
  566. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps) {
  567. int size = 0;
  568. int w = p_width;
  569. int h = p_height;
  570. int mm = 0;
  571. int pixsize = get_format_pixel_size(p_format);
  572. int pixshift = get_format_pixel_rshift(p_format);
  573. int minw, minh;
  574. _get_format_min_data_size(p_format, minw, minh);
  575. switch (p_format) {
  576. case FORMAT_INDEXED:
  577. pixsize = 1;
  578. size = 256 * 3;
  579. break;
  580. case FORMAT_INDEXED_ALPHA:
  581. pixsize = 1;
  582. size = 256 * 4;
  583. break;
  584. default: {}
  585. };
  586. while (true) {
  587. int s = w * h;
  588. s *= pixsize;
  589. s >>= pixshift;
  590. size += s;
  591. if (p_mipmaps >= 0 && mm == p_mipmaps)
  592. break;
  593. if (p_mipmaps >= 0) {
  594. w = MAX(minw, w >> 1);
  595. h = MAX(minh, h >> 1);
  596. } else {
  597. if (w == minw && h == minh)
  598. break;
  599. w = MAX(minw, w >> 1);
  600. h = MAX(minh, h >> 1);
  601. }
  602. mm++;
  603. };
  604. r_mipmaps = mm;
  605. return size;
  606. }
  607. bool Image::_can_modify(Format p_format) const {
  608. switch (p_format) {
  609. //these are OK
  610. case FORMAT_GRAYSCALE:
  611. case FORMAT_INTENSITY:
  612. case FORMAT_GRAYSCALE_ALPHA:
  613. case FORMAT_RGB:
  614. case FORMAT_RGBA:
  615. return true;
  616. default:
  617. return false;
  618. }
  619. return false;
  620. }
  621. template <int CC>
  622. static void _generate_po2_mipmap(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_width, uint32_t p_height) {
  623. //fast power of 2 mipmap generation
  624. uint32_t dst_w = p_width >> 1;
  625. uint32_t dst_h = p_height >> 1;
  626. for (uint32_t i = 0; i < dst_h; i++) {
  627. const uint8_t *rup_ptr = &p_src[i * 2 * p_width * CC];
  628. const uint8_t *rdown_ptr = rup_ptr + p_width * CC;
  629. uint8_t *dst_ptr = &p_dst[i * dst_w * CC];
  630. uint32_t count = dst_w;
  631. while (count--) {
  632. for (int j = 0; j < CC; j++) {
  633. uint16_t val = 0;
  634. val += rup_ptr[j];
  635. val += rup_ptr[j + CC];
  636. val += rdown_ptr[j];
  637. val += rdown_ptr[j + CC];
  638. dst_ptr[j] = val >> 2;
  639. }
  640. dst_ptr += CC;
  641. rup_ptr += CC * 2;
  642. rdown_ptr += CC * 2;
  643. }
  644. }
  645. }
  646. void Image::expand_x2_hq2x() {
  647. ERR_FAIL_COND(format >= FORMAT_INDEXED);
  648. Format current = format;
  649. bool mipmaps = get_mipmaps();
  650. if (mipmaps) {
  651. clear_mipmaps();
  652. }
  653. if (current != FORMAT_RGBA)
  654. convert(FORMAT_RGBA);
  655. DVector<uint8_t> dest;
  656. dest.resize(width * 2 * height * 2 * 4);
  657. {
  658. DVector<uint8_t>::Read r = data.read();
  659. DVector<uint8_t>::Write w = dest.write();
  660. hq2x_resize((const uint32_t *)r.ptr(), width, height, (uint32_t *)w.ptr());
  661. }
  662. width *= 2;
  663. height *= 2;
  664. data = dest;
  665. if (current != FORMAT_RGBA)
  666. convert(current);
  667. if (mipmaps) {
  668. generate_mipmaps();
  669. }
  670. }
  671. void Image::shrink_x2() {
  672. ERR_FAIL_COND(format == FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA);
  673. ERR_FAIL_COND(data.size() == 0);
  674. if (mipmaps) {
  675. //just use the lower mipmap as base and copy all
  676. DVector<uint8_t> new_img;
  677. int ofs = get_mipmap_offset(1);
  678. int new_size = data.size() - ofs;
  679. new_img.resize(new_size);
  680. {
  681. DVector<uint8_t>::Write w = new_img.write();
  682. DVector<uint8_t>::Read r = data.read();
  683. copymem(w.ptr(), &r[ofs], new_size);
  684. }
  685. mipmaps--;
  686. width /= 2;
  687. height /= 2;
  688. data = new_img;
  689. } else {
  690. DVector<uint8_t> new_img;
  691. ERR_FAIL_COND(format >= FORMAT_INDEXED);
  692. int ps = get_format_pixel_size(format);
  693. new_img.resize((width / 2) * (height / 2) * ps);
  694. {
  695. DVector<uint8_t>::Write w = new_img.write();
  696. DVector<uint8_t>::Read r = data.read();
  697. switch (format) {
  698. case FORMAT_GRAYSCALE:
  699. case FORMAT_INTENSITY: _generate_po2_mipmap<1>(r.ptr(), w.ptr(), width, height); break;
  700. case FORMAT_GRAYSCALE_ALPHA: _generate_po2_mipmap<2>(r.ptr(), w.ptr(), width, height); break;
  701. case FORMAT_RGB: _generate_po2_mipmap<3>(r.ptr(), w.ptr(), width, height); break;
  702. case FORMAT_RGBA: _generate_po2_mipmap<4>(r.ptr(), w.ptr(), width, height); break;
  703. default: {}
  704. }
  705. }
  706. width /= 2;
  707. height /= 2;
  708. data = new_img;
  709. }
  710. }
  711. Error Image::generate_mipmaps(int p_mipmaps, bool p_keep_existing) {
  712. if (!_can_modify(format)) {
  713. ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats.");
  714. ERR_FAIL_V(ERR_UNAVAILABLE);
  715. }
  716. int from_mm = 1;
  717. if (p_keep_existing) {
  718. from_mm = mipmaps + 1;
  719. }
  720. int size = _get_dst_image_size(width, height, format, mipmaps, p_mipmaps);
  721. data.resize(size);
  722. DVector<uint8_t>::Write wp = data.write();
  723. if (next_power_of_2(width) == uint32_t(width) && next_power_of_2(height) == uint32_t(height)) {
  724. //use fast code for powers of 2
  725. int prev_ofs = 0;
  726. int prev_h = height;
  727. int prev_w = width;
  728. for (int i = 1; i < mipmaps; i++) {
  729. int ofs, w, h;
  730. _get_mipmap_offset_and_size(i, ofs, w, h);
  731. if (i >= from_mm) {
  732. switch (format) {
  733. case FORMAT_GRAYSCALE:
  734. case FORMAT_INTENSITY: _generate_po2_mipmap<1>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  735. case FORMAT_GRAYSCALE_ALPHA: _generate_po2_mipmap<2>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  736. case FORMAT_RGB: _generate_po2_mipmap<3>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  737. case FORMAT_RGBA: _generate_po2_mipmap<4>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  738. default: {}
  739. }
  740. }
  741. prev_ofs = ofs;
  742. prev_w = w;
  743. prev_h = h;
  744. }
  745. } else {
  746. //use slow code..
  747. //use bilinear filtered code for non powers of 2
  748. int prev_ofs = 0;
  749. int prev_h = height;
  750. int prev_w = width;
  751. for (int i = 1; i < mipmaps; i++) {
  752. int ofs, w, h;
  753. _get_mipmap_offset_and_size(i, ofs, w, h);
  754. if (i >= from_mm) {
  755. switch (format) {
  756. case FORMAT_GRAYSCALE:
  757. case FORMAT_INTENSITY: _scale_bilinear<1>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  758. case FORMAT_GRAYSCALE_ALPHA: _scale_bilinear<2>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  759. case FORMAT_RGB: _scale_bilinear<3>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  760. case FORMAT_RGBA: _scale_bilinear<4>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  761. default: {}
  762. }
  763. }
  764. prev_ofs = ofs;
  765. prev_w = w;
  766. prev_h = h;
  767. }
  768. }
  769. return OK;
  770. }
  771. void Image::clear_mipmaps() {
  772. if (mipmaps == 0)
  773. return;
  774. if (format == FORMAT_CUSTOM) {
  775. ERR_EXPLAIN("Cannot clear mipmaps in indexed, compressed or custom image formats.");
  776. ERR_FAIL();
  777. }
  778. if (empty())
  779. return;
  780. int ofs, w, h;
  781. _get_mipmap_offset_and_size(1, ofs, w, h);
  782. int palsize = get_format_pallete_size(format);
  783. DVector<uint8_t> pallete;
  784. ERR_FAIL_COND(ofs + palsize > data.size()); //bug?
  785. if (palsize) {
  786. pallete.resize(palsize);
  787. DVector<uint8_t>::Read r = data.read();
  788. DVector<uint8_t>::Write w = pallete.write();
  789. copymem(&w[0], &r[data.size() - palsize], palsize);
  790. }
  791. data.resize(ofs + palsize);
  792. if (palsize) {
  793. DVector<uint8_t>::Read r = pallete.read();
  794. DVector<uint8_t>::Write w = data.write();
  795. copymem(&w[ofs], &r[0], palsize);
  796. }
  797. mipmaps = 0;
  798. }
  799. void Image::make_normalmap(float p_height_scale) {
  800. if (!_can_modify(format)) {
  801. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  802. ERR_FAIL();
  803. }
  804. ERR_FAIL_COND(empty());
  805. Image normalmap(width, height, 0, FORMAT_RGB);
  806. /*
  807. for (int y=0;y<height;y++) {
  808. for (int x=0;x<width;x++) {
  809. float center=get_pixel(x,y).gray()/255.0;
  810. float up=(y>0)?get_pixel(x,y-1).gray()/255.0:center;
  811. float down=(y<(height-1))?get_pixel(x,y+1).gray()/255.0:center;
  812. float left=(x>0)?get_pixel(x-1,y).gray()/255.0:center;
  813. float right=(x<(width-1))?get_pixel(x+1,y).gray()/255.0:center;
  814. // uhm, how do i do this? ....
  815. Color result( (uint8_t)((normal.x+1.0)*127.0), (uint8_t)((normal.y+1.0)*127.0), (uint8_t)((normal.z+1.0)*127.0) );
  816. normalmap.put_pixel( x, y, result );
  817. }
  818. }
  819. */
  820. *this = normalmap;
  821. }
  822. bool Image::empty() const {
  823. return (data.size() == 0);
  824. }
  825. DVector<uint8_t> Image::get_data() const {
  826. return data;
  827. }
  828. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  829. int mm = 0;
  830. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  831. data.resize(size);
  832. {
  833. DVector<uint8_t>::Write w = data.write();
  834. zeromem(w.ptr(), size);
  835. }
  836. width = p_width;
  837. height = p_height;
  838. mipmaps = mm;
  839. format = p_format;
  840. }
  841. void Image::create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t> &p_data) {
  842. ERR_FAIL_INDEX(p_width - 1, MAX_WIDTH);
  843. ERR_FAIL_INDEX(p_height - 1, MAX_HEIGHT);
  844. if (p_format < FORMAT_CUSTOM) {
  845. int mm;
  846. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps);
  847. if (size != p_data.size()) {
  848. ERR_EXPLAIN("Expected data size of " + itos(size) + " in Image::create()");
  849. ERR_FAIL_COND(p_data.size() != size);
  850. }
  851. };
  852. height = p_height;
  853. width = p_width;
  854. format = p_format;
  855. data = p_data;
  856. mipmaps = p_mipmaps;
  857. }
  858. void Image::create(const char **p_xpm) {
  859. int size_width, size_height;
  860. int pixelchars = 0;
  861. mipmaps = 0;
  862. bool has_alpha = false;
  863. enum Status {
  864. READING_HEADER,
  865. READING_COLORS,
  866. READING_PIXELS,
  867. DONE
  868. };
  869. Status status = READING_HEADER;
  870. int line = 0;
  871. HashMap<String, Color> colormap;
  872. int colormap_size;
  873. while (status != DONE) {
  874. const char *line_ptr = p_xpm[line];
  875. switch (status) {
  876. case READING_HEADER: {
  877. String line_str = line_ptr;
  878. line_str.replace("\t", " ");
  879. size_width = line_str.get_slicec(' ', 0).to_int();
  880. size_height = line_str.get_slicec(' ', 1).to_int();
  881. colormap_size = line_str.get_slicec(' ', 2).to_int();
  882. pixelchars = line_str.get_slicec(' ', 3).to_int();
  883. ERR_FAIL_COND(colormap_size > 32766);
  884. ERR_FAIL_COND(pixelchars > 5);
  885. ERR_FAIL_COND(size_width > 32767);
  886. ERR_FAIL_COND(size_height > 32767);
  887. status = READING_COLORS;
  888. } break;
  889. case READING_COLORS: {
  890. String colorstring;
  891. for (int i = 0; i < pixelchars; i++) {
  892. colorstring += *line_ptr;
  893. line_ptr++;
  894. }
  895. //skip spaces
  896. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  897. if (*line_ptr == 0)
  898. break;
  899. line_ptr++;
  900. }
  901. if (*line_ptr == 'c') {
  902. line_ptr++;
  903. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  904. if (*line_ptr == 0)
  905. break;
  906. line_ptr++;
  907. }
  908. if (*line_ptr == '#') {
  909. line_ptr++;
  910. uint8_t col_r;
  911. uint8_t col_g;
  912. uint8_t col_b;
  913. // uint8_t col_a=255;
  914. for (int i = 0; i < 6; i++) {
  915. char v = line_ptr[i];
  916. if (v >= '0' && v <= '9')
  917. v -= '0';
  918. else if (v >= 'A' && v <= 'F')
  919. v = (v - 'A') + 10;
  920. else if (v >= 'a' && v <= 'f')
  921. v = (v - 'a') + 10;
  922. else
  923. break;
  924. switch (i) {
  925. case 0: col_r = v << 4; break;
  926. case 1: col_r |= v; break;
  927. case 2: col_g = v << 4; break;
  928. case 3: col_g |= v; break;
  929. case 4: col_b = v << 4; break;
  930. case 5: col_b |= v; break;
  931. };
  932. }
  933. // magenta mask
  934. if (col_r == 255 && col_g == 0 && col_b == 255) {
  935. colormap[colorstring] = Color(0, 0, 0, 0);
  936. has_alpha = true;
  937. } else {
  938. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  939. }
  940. }
  941. }
  942. if (line == colormap_size) {
  943. status = READING_PIXELS;
  944. create(size_width, size_height, 0, has_alpha ? FORMAT_RGBA : FORMAT_RGB);
  945. }
  946. } break;
  947. case READING_PIXELS: {
  948. int y = line - colormap_size - 1;
  949. for (int x = 0; x < size_width; x++) {
  950. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  951. for (int i = 0; i < pixelchars; i++)
  952. pixelstr[i] = line_ptr[x * pixelchars + i];
  953. Color *colorptr = colormap.getptr(pixelstr);
  954. ERR_FAIL_COND(!colorptr);
  955. put_pixel(x, y, *colorptr);
  956. }
  957. if (y == (size_height - 1))
  958. status = DONE;
  959. } break;
  960. default: {}
  961. }
  962. line++;
  963. }
  964. }
  965. #define DETECT_ALPHA_MAX_TRESHOLD 254
  966. #define DETECT_ALPHA_MIN_TRESHOLD 2
  967. #define DETECT_ALPHA(m_value) \
  968. { \
  969. uint8_t value = m_value; \
  970. if (value < DETECT_ALPHA_MIN_TRESHOLD) \
  971. bit = true; \
  972. else if (value < DETECT_ALPHA_MAX_TRESHOLD) { \
  973. \
  974. detected = true; \
  975. break; \
  976. } \
  977. }
  978. #define DETECT_NON_ALPHA(m_value) \
  979. { \
  980. uint8_t value = m_value; \
  981. if (value > 0) { \
  982. \
  983. detected = true; \
  984. break; \
  985. } \
  986. }
  987. bool Image::is_invisible() const {
  988. if (format == FORMAT_GRAYSCALE ||
  989. format == FORMAT_RGB ||
  990. format == FORMAT_INDEXED)
  991. return false;
  992. int len = data.size();
  993. if (len == 0)
  994. return true;
  995. if (format >= FORMAT_YUV_422 && format <= FORMAT_YUV_444)
  996. return false;
  997. int w, h;
  998. _get_mipmap_offset_and_size(1, len, w, h);
  999. DVector<uint8_t>::Read r = data.read();
  1000. const unsigned char *data_ptr = r.ptr();
  1001. bool detected = false;
  1002. switch (format) {
  1003. case FORMAT_INTENSITY: {
  1004. for (int i = 0; i < len; i++) {
  1005. DETECT_NON_ALPHA(data_ptr[i]);
  1006. }
  1007. } break;
  1008. case FORMAT_GRAYSCALE_ALPHA: {
  1009. for (int i = 0; i < (len >> 1); i++) {
  1010. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  1011. }
  1012. } break;
  1013. case FORMAT_RGBA: {
  1014. for (int i = 0; i < (len >> 2); i++) {
  1015. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  1016. }
  1017. } break;
  1018. case FORMAT_INDEXED: {
  1019. return false;
  1020. } break;
  1021. case FORMAT_INDEXED_ALPHA: {
  1022. return false;
  1023. } break;
  1024. case FORMAT_PVRTC2_ALPHA:
  1025. case FORMAT_PVRTC4_ALPHA:
  1026. case FORMAT_BC2:
  1027. case FORMAT_BC3: {
  1028. detected = true;
  1029. } break;
  1030. default: {}
  1031. }
  1032. return !detected;
  1033. }
  1034. Image::AlphaMode Image::detect_alpha() const {
  1035. if (format == FORMAT_GRAYSCALE ||
  1036. format == FORMAT_RGB ||
  1037. format == FORMAT_INDEXED)
  1038. return ALPHA_NONE;
  1039. int len = data.size();
  1040. if (len == 0)
  1041. return ALPHA_NONE;
  1042. if (format >= FORMAT_YUV_422 && format <= FORMAT_YUV_444)
  1043. return ALPHA_NONE;
  1044. int w, h;
  1045. _get_mipmap_offset_and_size(1, len, w, h);
  1046. DVector<uint8_t>::Read r = data.read();
  1047. const unsigned char *data_ptr = r.ptr();
  1048. bool bit = false;
  1049. bool detected = false;
  1050. switch (format) {
  1051. case FORMAT_INTENSITY: {
  1052. for (int i = 0; i < len; i++) {
  1053. DETECT_ALPHA(data_ptr[i]);
  1054. }
  1055. } break;
  1056. case FORMAT_GRAYSCALE_ALPHA: {
  1057. for (int i = 0; i < (len >> 1); i++) {
  1058. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  1059. }
  1060. } break;
  1061. case FORMAT_RGBA: {
  1062. for (int i = 0; i < (len >> 2); i++) {
  1063. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  1064. }
  1065. } break;
  1066. case FORMAT_INDEXED: {
  1067. return ALPHA_NONE;
  1068. } break;
  1069. case FORMAT_INDEXED_ALPHA: {
  1070. return ALPHA_BLEND;
  1071. } break;
  1072. case FORMAT_PVRTC2_ALPHA:
  1073. case FORMAT_PVRTC4_ALPHA:
  1074. case FORMAT_BC2:
  1075. case FORMAT_BC3: {
  1076. detected = true;
  1077. } break;
  1078. default: {}
  1079. }
  1080. if (detected)
  1081. return ALPHA_BLEND;
  1082. else if (bit)
  1083. return ALPHA_BIT;
  1084. else
  1085. return ALPHA_NONE;
  1086. }
  1087. Error Image::load(const String &p_path) {
  1088. return ImageLoader::load_image(p_path, this);
  1089. }
  1090. Error Image::save_png(const String &p_path) const {
  1091. if (save_png_func == NULL)
  1092. return ERR_UNAVAILABLE;
  1093. Image image = *this;
  1094. return save_png_func(p_path, image);
  1095. };
  1096. Error Image::_decompress_bc() {
  1097. print_line("decompressing bc");
  1098. int wd = width, ht = height;
  1099. if (wd % 4 != 0) {
  1100. wd += 4 - (wd % 4);
  1101. }
  1102. if (ht % 4 != 0) {
  1103. ht += 4 - (ht % 4);
  1104. }
  1105. int mm;
  1106. int size = _get_dst_image_size(wd, ht, FORMAT_RGBA, mm, mipmaps);
  1107. DVector<uint8_t> newdata;
  1108. newdata.resize(size);
  1109. DVector<uint8_t>::Write w = newdata.write();
  1110. DVector<uint8_t>::Read r = data.read();
  1111. int rofs = 0;
  1112. int wofs = 0;
  1113. //print_line("width: "+itos(wd)+" height: "+itos(ht));
  1114. int mm_w = wd;
  1115. int mm_h = ht;
  1116. for (int i = 0; i <= mm; i++) {
  1117. switch (format) {
  1118. case FORMAT_BC1: {
  1119. int len = (mm_w * mm_h) / 16;
  1120. uint8_t *dst = &w[wofs];
  1121. uint32_t ofs_table[16];
  1122. for (int x = 0; x < 4; x++) {
  1123. for (int y = 0; y < 4; y++) {
  1124. ofs_table[15 - (y * 4 + (3 - x))] = (x + y * mm_w) * 4;
  1125. }
  1126. }
  1127. for (int j = 0; j < len; j++) {
  1128. const uint8_t *src = &r[rofs + j * 8];
  1129. uint16_t col_a = src[1];
  1130. col_a <<= 8;
  1131. col_a |= src[0];
  1132. uint16_t col_b = src[3];
  1133. col_b <<= 8;
  1134. col_b |= src[2];
  1135. uint8_t table[4][4] = {
  1136. { (uint8_t)((col_a >> 11) << 3), (uint8_t)(((col_a >> 5) & 0x3f) << 2), (uint8_t)(((col_a)&0x1f) << 3), 255 },
  1137. { (uint8_t)((col_b >> 11) << 3), (uint8_t)(((col_b >> 5) & 0x3f) << 2), (uint8_t)(((col_b)&0x1f) << 3), 255 },
  1138. { 0, 0, 0, 255 },
  1139. { 0, 0, 0, 255 }
  1140. };
  1141. if (col_a < col_b) {
  1142. //punchrough
  1143. table[2][0] = (int(table[0][0]) + int(table[1][0])) >> 1;
  1144. table[2][1] = (int(table[0][1]) + int(table[1][1])) >> 1;
  1145. table[2][2] = (int(table[0][2]) + int(table[1][2])) >> 1;
  1146. table[3][3] = 0; //premul alpha black
  1147. } else {
  1148. //gradient
  1149. table[2][0] = (int(table[0][0]) * 2 + int(table[1][0])) / 3;
  1150. table[2][1] = (int(table[0][1]) * 2 + int(table[1][1])) / 3;
  1151. table[2][2] = (int(table[0][2]) * 2 + int(table[1][2])) / 3;
  1152. table[3][0] = (int(table[0][0]) + int(table[1][0]) * 2) / 3;
  1153. table[3][1] = (int(table[0][1]) + int(table[1][1]) * 2) / 3;
  1154. table[3][2] = (int(table[0][2]) + int(table[1][2]) * 2) / 3;
  1155. }
  1156. uint32_t block = src[4];
  1157. block <<= 8;
  1158. block |= src[5];
  1159. block <<= 8;
  1160. block |= src[6];
  1161. block <<= 8;
  1162. block |= src[7];
  1163. int y = (j / (mm_w / 4)) * 4;
  1164. int x = (j % (mm_w / 4)) * 4;
  1165. int pixofs = (y * mm_w + x) * 4;
  1166. for (int k = 0; k < 16; k++) {
  1167. int idx = pixofs + ofs_table[k];
  1168. dst[idx + 0] = table[block & 0x3][0];
  1169. dst[idx + 1] = table[block & 0x3][1];
  1170. dst[idx + 2] = table[block & 0x3][2];
  1171. dst[idx + 3] = table[block & 0x3][3];
  1172. block >>= 2;
  1173. }
  1174. }
  1175. rofs += len * 8;
  1176. wofs += mm_w * mm_h * 4;
  1177. mm_w /= 2;
  1178. mm_h /= 2;
  1179. } break;
  1180. case FORMAT_BC2: {
  1181. int len = (mm_w * mm_h) / 16;
  1182. uint8_t *dst = &w[wofs];
  1183. uint32_t ofs_table[16];
  1184. for (int x = 0; x < 4; x++) {
  1185. for (int y = 0; y < 4; y++) {
  1186. ofs_table[15 - (y * 4 + (3 - x))] = (x + y * mm_w) * 4;
  1187. }
  1188. }
  1189. for (int j = 0; j < len; j++) {
  1190. const uint8_t *src = &r[rofs + j * 16];
  1191. uint64_t ablock = src[1];
  1192. ablock <<= 8;
  1193. ablock |= src[0];
  1194. ablock <<= 8;
  1195. ablock |= src[3];
  1196. ablock <<= 8;
  1197. ablock |= src[2];
  1198. ablock <<= 8;
  1199. ablock |= src[5];
  1200. ablock <<= 8;
  1201. ablock |= src[4];
  1202. ablock <<= 8;
  1203. ablock |= src[7];
  1204. ablock <<= 8;
  1205. ablock |= src[6];
  1206. uint16_t col_a = src[8 + 1];
  1207. col_a <<= 8;
  1208. col_a |= src[8 + 0];
  1209. uint16_t col_b = src[8 + 3];
  1210. col_b <<= 8;
  1211. col_b |= src[8 + 2];
  1212. uint8_t table[4][4] = {
  1213. { (uint8_t)((col_a >> 11) << 3), (uint8_t)(((col_a >> 5) & 0x3f) << 2), (uint8_t)(((col_a)&0x1f) << 3), 255 },
  1214. { (uint8_t)((col_b >> 11) << 3), (uint8_t)(((col_b >> 5) & 0x3f) << 2), (uint8_t)(((col_b)&0x1f) << 3), 255 },
  1215. { 0, 0, 0, 255 },
  1216. { 0, 0, 0, 255 }
  1217. };
  1218. //always gradient
  1219. table[2][0] = (int(table[0][0]) * 2 + int(table[1][0])) / 3;
  1220. table[2][1] = (int(table[0][1]) * 2 + int(table[1][1])) / 3;
  1221. table[2][2] = (int(table[0][2]) * 2 + int(table[1][2])) / 3;
  1222. table[3][0] = (int(table[0][0]) + int(table[1][0]) * 2) / 3;
  1223. table[3][1] = (int(table[0][1]) + int(table[1][1]) * 2) / 3;
  1224. table[3][2] = (int(table[0][2]) + int(table[1][2]) * 2) / 3;
  1225. uint32_t block = src[4 + 8];
  1226. block <<= 8;
  1227. block |= src[5 + 8];
  1228. block <<= 8;
  1229. block |= src[6 + 8];
  1230. block <<= 8;
  1231. block |= src[7 + 8];
  1232. int y = (j / (mm_w / 4)) * 4;
  1233. int x = (j % (mm_w / 4)) * 4;
  1234. int pixofs = (y * mm_w + x) * 4;
  1235. for (int k = 0; k < 16; k++) {
  1236. uint8_t alpha = ablock & 0xf;
  1237. alpha = int(alpha) * 255 / 15; //right way for alpha
  1238. int idx = pixofs + ofs_table[k];
  1239. dst[idx + 0] = table[block & 0x3][0];
  1240. dst[idx + 1] = table[block & 0x3][1];
  1241. dst[idx + 2] = table[block & 0x3][2];
  1242. dst[idx + 3] = alpha;
  1243. block >>= 2;
  1244. ablock >>= 4;
  1245. }
  1246. }
  1247. rofs += len * 16;
  1248. wofs += mm_w * mm_h * 4;
  1249. mm_w /= 2;
  1250. mm_h /= 2;
  1251. } break;
  1252. case FORMAT_BC3: {
  1253. int len = (mm_w * mm_h) / 16;
  1254. uint8_t *dst = &w[wofs];
  1255. uint32_t ofs_table[16];
  1256. for (int x = 0; x < 4; x++) {
  1257. for (int y = 0; y < 4; y++) {
  1258. ofs_table[15 - (y * 4 + (3 - x))] = (x + y * mm_w) * 4;
  1259. }
  1260. }
  1261. for (int j = 0; j < len; j++) {
  1262. const uint8_t *src = &r[rofs + j * 16];
  1263. uint8_t a_start = src[1];
  1264. uint8_t a_end = src[0];
  1265. uint64_t ablock = src[3];
  1266. ablock <<= 8;
  1267. ablock |= src[2];
  1268. ablock <<= 8;
  1269. ablock |= src[5];
  1270. ablock <<= 8;
  1271. ablock |= src[4];
  1272. ablock <<= 8;
  1273. ablock |= src[7];
  1274. ablock <<= 8;
  1275. ablock |= src[6];
  1276. uint8_t atable[8];
  1277. if (a_start > a_end) {
  1278. atable[0] = (int(a_start) * 7 + int(a_end) * 0) / 7;
  1279. atable[1] = (int(a_start) * 6 + int(a_end) * 1) / 7;
  1280. atable[2] = (int(a_start) * 5 + int(a_end) * 2) / 7;
  1281. atable[3] = (int(a_start) * 4 + int(a_end) * 3) / 7;
  1282. atable[4] = (int(a_start) * 3 + int(a_end) * 4) / 7;
  1283. atable[5] = (int(a_start) * 2 + int(a_end) * 5) / 7;
  1284. atable[6] = (int(a_start) * 1 + int(a_end) * 6) / 7;
  1285. atable[7] = (int(a_start) * 0 + int(a_end) * 7) / 7;
  1286. } else {
  1287. atable[0] = (int(a_start) * 5 + int(a_end) * 0) / 5;
  1288. atable[1] = (int(a_start) * 4 + int(a_end) * 1) / 5;
  1289. atable[2] = (int(a_start) * 3 + int(a_end) * 2) / 5;
  1290. atable[3] = (int(a_start) * 2 + int(a_end) * 3) / 5;
  1291. atable[4] = (int(a_start) * 1 + int(a_end) * 4) / 5;
  1292. atable[5] = (int(a_start) * 0 + int(a_end) * 5) / 5;
  1293. atable[6] = 0;
  1294. atable[7] = 255;
  1295. }
  1296. uint16_t col_a = src[8 + 1];
  1297. col_a <<= 8;
  1298. col_a |= src[8 + 0];
  1299. uint16_t col_b = src[8 + 3];
  1300. col_b <<= 8;
  1301. col_b |= src[8 + 2];
  1302. uint8_t table[4][4] = {
  1303. { (uint8_t)((col_a >> 11) << 3), (uint8_t)(((col_a >> 5) & 0x3f) << 2), (uint8_t)(((col_a)&0x1f) << 3), 255 },
  1304. { (uint8_t)((col_b >> 11) << 3), (uint8_t)(((col_b >> 5) & 0x3f) << 2), (uint8_t)(((col_b)&0x1f) << 3), 255 },
  1305. { 0, 0, 0, 255 },
  1306. { 0, 0, 0, 255 }
  1307. };
  1308. //always gradient
  1309. table[2][0] = (int(table[0][0]) * 2 + int(table[1][0])) / 3;
  1310. table[2][1] = (int(table[0][1]) * 2 + int(table[1][1])) / 3;
  1311. table[2][2] = (int(table[0][2]) * 2 + int(table[1][2])) / 3;
  1312. table[3][0] = (int(table[0][0]) + int(table[1][0]) * 2) / 3;
  1313. table[3][1] = (int(table[0][1]) + int(table[1][1]) * 2) / 3;
  1314. table[3][2] = (int(table[0][2]) + int(table[1][2]) * 2) / 3;
  1315. uint32_t block = src[4 + 8];
  1316. block <<= 8;
  1317. block |= src[5 + 8];
  1318. block <<= 8;
  1319. block |= src[6 + 8];
  1320. block <<= 8;
  1321. block |= src[7 + 8];
  1322. int y = (j / (mm_w / 4)) * 4;
  1323. int x = (j % (mm_w / 4)) * 4;
  1324. int pixofs = (y * mm_w + x) * 4;
  1325. for (int k = 0; k < 16; k++) {
  1326. uint8_t alpha = ablock & 0x7;
  1327. int idx = pixofs + ofs_table[k];
  1328. dst[idx + 0] = table[block & 0x3][0];
  1329. dst[idx + 1] = table[block & 0x3][1];
  1330. dst[idx + 2] = table[block & 0x3][2];
  1331. dst[idx + 3] = atable[alpha];
  1332. block >>= 2;
  1333. ablock >>= 3;
  1334. }
  1335. }
  1336. rofs += len * 16;
  1337. wofs += mm_w * mm_h * 4;
  1338. mm_w /= 2;
  1339. mm_h /= 2;
  1340. } break;
  1341. }
  1342. }
  1343. w = DVector<uint8_t>::Write();
  1344. r = DVector<uint8_t>::Read();
  1345. data = newdata;
  1346. format = FORMAT_RGBA;
  1347. if (wd != width || ht != height) {
  1348. //todo, crop
  1349. width = wd;
  1350. height = ht;
  1351. }
  1352. return OK;
  1353. }
  1354. bool Image::operator==(const Image &p_image) const {
  1355. if (data.size() == 0 && p_image.data.size() == 0)
  1356. return true;
  1357. DVector<uint8_t>::Read r = data.read();
  1358. DVector<uint8_t>::Read pr = p_image.data.read();
  1359. return r.ptr() == pr.ptr();
  1360. }
  1361. int Image::get_format_pixel_size(Format p_format) {
  1362. switch (p_format) {
  1363. case FORMAT_GRAYSCALE: {
  1364. return 1;
  1365. } break;
  1366. case FORMAT_INTENSITY: {
  1367. return 1;
  1368. } break;
  1369. case FORMAT_GRAYSCALE_ALPHA: {
  1370. return 2;
  1371. } break;
  1372. case FORMAT_RGB: {
  1373. return 3;
  1374. } break;
  1375. case FORMAT_RGBA: {
  1376. return 4;
  1377. } break;
  1378. case FORMAT_INDEXED: {
  1379. return 1;
  1380. } break;
  1381. case FORMAT_INDEXED_ALPHA: {
  1382. return 1;
  1383. } break;
  1384. case FORMAT_BC1:
  1385. case FORMAT_BC2:
  1386. case FORMAT_BC3:
  1387. case FORMAT_BC4:
  1388. case FORMAT_BC5: {
  1389. return 1;
  1390. } break;
  1391. case FORMAT_PVRTC2:
  1392. case FORMAT_PVRTC2_ALPHA: {
  1393. return 1;
  1394. } break;
  1395. case FORMAT_PVRTC4:
  1396. case FORMAT_PVRTC4_ALPHA: {
  1397. return 1;
  1398. } break;
  1399. case FORMAT_ATC:
  1400. case FORMAT_ATC_ALPHA_EXPLICIT:
  1401. case FORMAT_ATC_ALPHA_INTERPOLATED: {
  1402. return 1;
  1403. } break;
  1404. case FORMAT_ETC: {
  1405. return 1;
  1406. } break;
  1407. case FORMAT_YUV_422: {
  1408. return 2;
  1409. };
  1410. case FORMAT_YUV_444: {
  1411. return 3;
  1412. } break;
  1413. case FORMAT_CUSTOM: {
  1414. ERR_EXPLAIN("pixel size requested for custom image format, and it's unknown obviously");
  1415. ERR_FAIL_V(1);
  1416. } break;
  1417. default: {
  1418. ERR_EXPLAIN("Cannot obtain pixel size from this format");
  1419. ERR_FAIL_V(1);
  1420. }
  1421. }
  1422. return 0;
  1423. }
  1424. int Image::get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps) {
  1425. int mm;
  1426. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps);
  1427. }
  1428. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1429. int mm;
  1430. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  1431. return mm;
  1432. }
  1433. void Image::_get_format_min_data_size(Format p_format, int &r_w, int &r_h) {
  1434. switch (p_format) {
  1435. case FORMAT_BC1:
  1436. case FORMAT_BC2:
  1437. case FORMAT_BC3:
  1438. case FORMAT_BC4:
  1439. case FORMAT_BC5: {
  1440. r_w = 4;
  1441. r_h = 4;
  1442. } break;
  1443. case FORMAT_PVRTC2:
  1444. case FORMAT_PVRTC2_ALPHA: {
  1445. r_w = 16;
  1446. r_h = 8;
  1447. } break;
  1448. case FORMAT_PVRTC4_ALPHA:
  1449. case FORMAT_PVRTC4: {
  1450. r_w = 8;
  1451. r_h = 8;
  1452. } break;
  1453. case FORMAT_ATC:
  1454. case FORMAT_ATC_ALPHA_EXPLICIT:
  1455. case FORMAT_ATC_ALPHA_INTERPOLATED: {
  1456. r_w = 8;
  1457. r_h = 8;
  1458. } break;
  1459. case FORMAT_ETC: {
  1460. r_w = 4;
  1461. r_h = 4;
  1462. } break;
  1463. default: {
  1464. r_w = 1;
  1465. r_h = 1;
  1466. } break;
  1467. }
  1468. }
  1469. int Image::get_format_pixel_rshift(Format p_format) {
  1470. if (p_format == FORMAT_BC1 || p_format == FORMAT_BC4 || p_format == FORMAT_ATC || p_format == FORMAT_PVRTC4 || p_format == FORMAT_PVRTC4_ALPHA || p_format == FORMAT_ETC)
  1471. return 1;
  1472. else if (p_format == FORMAT_PVRTC2 || p_format == FORMAT_PVRTC2_ALPHA)
  1473. return 2;
  1474. else
  1475. return 0;
  1476. }
  1477. int Image::get_format_pallete_size(Format p_format) {
  1478. switch (p_format) {
  1479. case FORMAT_GRAYSCALE: {
  1480. return 0;
  1481. } break;
  1482. case FORMAT_INTENSITY: {
  1483. return 0;
  1484. } break;
  1485. case FORMAT_GRAYSCALE_ALPHA: {
  1486. return 0;
  1487. } break;
  1488. case FORMAT_RGB: {
  1489. return 0;
  1490. } break;
  1491. case FORMAT_RGBA: {
  1492. return 0;
  1493. } break;
  1494. case FORMAT_INDEXED: {
  1495. return 3 * 256;
  1496. } break;
  1497. case FORMAT_INDEXED_ALPHA: {
  1498. return 4 * 256;
  1499. } break;
  1500. default: {}
  1501. }
  1502. return 0;
  1503. }
  1504. bool Image::is_compressed() const {
  1505. return format >= FORMAT_BC1;
  1506. }
  1507. Image Image::decompressed() const {
  1508. Image img = *this;
  1509. img.decompress();
  1510. return img;
  1511. }
  1512. Error Image::decompress() {
  1513. if (format >= FORMAT_BC1 && format <= FORMAT_BC5 && _image_decompress_bc)
  1514. _image_decompress_bc(this); // libsquish
  1515. else if (format >= FORMAT_BC1 && format <= FORMAT_BC3)
  1516. _decompress_bc(); // builtin
  1517. else if (format >= FORMAT_PVRTC2 && format <= FORMAT_PVRTC4_ALPHA && _image_decompress_pvrtc)
  1518. _image_decompress_pvrtc(this);
  1519. else if (format == FORMAT_ETC && _image_decompress_etc)
  1520. _image_decompress_etc(this);
  1521. else
  1522. return ERR_UNAVAILABLE;
  1523. return OK;
  1524. }
  1525. Error Image::compress(CompressMode p_mode) {
  1526. switch (p_mode) {
  1527. case COMPRESS_BC: {
  1528. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1529. _image_compress_bc_func(this);
  1530. } break;
  1531. case COMPRESS_PVRTC2: {
  1532. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1533. _image_compress_pvrtc2_func(this);
  1534. } break;
  1535. case COMPRESS_PVRTC4: {
  1536. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1537. _image_compress_pvrtc4_func(this);
  1538. } break;
  1539. case COMPRESS_ETC: {
  1540. ERR_FAIL_COND_V(!_image_compress_etc_func, ERR_UNAVAILABLE);
  1541. _image_compress_etc_func(this);
  1542. } break;
  1543. }
  1544. return OK;
  1545. }
  1546. Image Image::compressed(int p_mode) {
  1547. Image ret = *this;
  1548. ret.compress((Image::CompressMode)p_mode);
  1549. return ret;
  1550. };
  1551. Image::Image(const char **p_xpm) {
  1552. width = 0;
  1553. height = 0;
  1554. mipmaps = 0;
  1555. format = FORMAT_GRAYSCALE;
  1556. create(p_xpm);
  1557. }
  1558. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1559. width = 0;
  1560. height = 0;
  1561. mipmaps = 0;
  1562. format = FORMAT_GRAYSCALE;
  1563. create(p_width, p_height, p_use_mipmaps, p_format);
  1564. }
  1565. Image::Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t> &p_data) {
  1566. width = 0;
  1567. height = 0;
  1568. mipmaps = 0;
  1569. format = FORMAT_GRAYSCALE;
  1570. create(p_width, p_height, p_mipmaps, p_format, p_data);
  1571. }
  1572. Image Image::brushed(const Image &p_src, const Image &p_brush, const Point2 &p_dest) const {
  1573. Image img = *this;
  1574. img.brush_transfer(p_src, p_brush, p_dest);
  1575. return img;
  1576. }
  1577. Rect2 Image::get_used_rect() const {
  1578. if (format == FORMAT_GRAYSCALE ||
  1579. format == FORMAT_RGB ||
  1580. format == FORMAT_INDEXED || format > FORMAT_INDEXED_ALPHA)
  1581. return Rect2(Point2(), Size2(width, height));
  1582. int len = data.size();
  1583. if (len == 0)
  1584. return Rect2();
  1585. int data_size = len;
  1586. DVector<uint8_t>::Read r = data.read();
  1587. const unsigned char *rptr = r.ptr();
  1588. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  1589. int maxx = -1, maxy = -1;
  1590. for (int i = 0; i < width; i++) {
  1591. for (int j = 0; j < height; j++) {
  1592. bool opaque = _get_pixel(i, j, rptr, data_size).a > 2;
  1593. if (!opaque)
  1594. continue;
  1595. if (i > maxx)
  1596. maxx = i;
  1597. if (j > maxy)
  1598. maxy = j;
  1599. if (i < minx)
  1600. minx = i;
  1601. if (j < miny)
  1602. miny = j;
  1603. }
  1604. }
  1605. if (maxx == -1)
  1606. return Rect2();
  1607. else
  1608. return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1);
  1609. }
  1610. Image Image::get_rect(const Rect2 &p_area) const {
  1611. Image img(p_area.size.x, p_area.size.y, mipmaps, format);
  1612. img.blit_rect(*this, p_area, Point2(0, 0));
  1613. return img;
  1614. };
  1615. void Image::brush_transfer(const Image &p_src, const Image &p_brush, const Point2 &p_dest) {
  1616. ERR_FAIL_COND(width != p_src.width || height != p_src.height);
  1617. int dst_data_size = data.size();
  1618. DVector<uint8_t>::Write wp = data.write();
  1619. unsigned char *dst_data_ptr = wp.ptr();
  1620. int src_data_size = p_src.data.size();
  1621. DVector<uint8_t>::Read rp = p_src.data.read();
  1622. const unsigned char *src_data_ptr = rp.ptr();
  1623. int brush_data_size = p_brush.data.size();
  1624. DVector<uint8_t>::Read bp = p_brush.data.read();
  1625. const unsigned char *src_brush_ptr = bp.ptr();
  1626. int bw = p_brush.get_width();
  1627. int bh = p_brush.get_height();
  1628. int dx = p_dest.x;
  1629. int dy = p_dest.y;
  1630. for (int i = dy; i < dy + bh; i++) {
  1631. if (i < 0 || i >= height)
  1632. continue;
  1633. for (int j = dx; j < dx + bw; j++) {
  1634. if (j < 0 || j >= width)
  1635. continue;
  1636. BColor src = p_src._get_pixel(j, i, src_data_ptr, src_data_size);
  1637. BColor dst = _get_pixel(j, i, dst_data_ptr, dst_data_size);
  1638. BColor brush = p_brush._get_pixel(j - dx, i - dy, src_brush_ptr, brush_data_size);
  1639. uint32_t mult = brush.r;
  1640. dst.r = dst.r + (((int32_t(src.r) - int32_t(dst.r)) * mult) >> 8);
  1641. dst.g = dst.g + (((int32_t(src.g) - int32_t(dst.g)) * mult) >> 8);
  1642. dst.b = dst.b + (((int32_t(src.b) - int32_t(dst.b)) * mult) >> 8);
  1643. dst.a = dst.a + (((int32_t(src.a) - int32_t(dst.a)) * mult) >> 8);
  1644. _put_pixel(j, i, dst, dst_data_ptr);
  1645. }
  1646. }
  1647. }
  1648. void Image::blit_rect(const Image &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1649. int dsize = data.size();
  1650. int srcdsize = p_src.data.size();
  1651. ERR_FAIL_COND(dsize == 0);
  1652. ERR_FAIL_COND(srcdsize == 0);
  1653. Rect2 rrect = Rect2(0, 0, p_src.width, p_src.height).clip(p_src_rect);
  1654. DVector<uint8_t>::Write wp = data.write();
  1655. unsigned char *dst_data_ptr = wp.ptr();
  1656. DVector<uint8_t>::Read rp = p_src.data.read();
  1657. const unsigned char *src_data_ptr = rp.ptr();
  1658. if ((format == FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA) && (p_src.format == FORMAT_INDEXED || p_src.format == FORMAT_INDEXED_ALPHA)) {
  1659. Point2i desti(p_dest.x, p_dest.y);
  1660. Point2i srci(rrect.pos.x, rrect.pos.y);
  1661. for (int i = 0; i < rrect.size.y; i++) {
  1662. if (i + desti.y < 0 || i + desti.y >= height)
  1663. continue;
  1664. for (int j = 0; j < rrect.size.x; j++) {
  1665. if (j + desti.x < 0 || j + desti.x >= width)
  1666. continue;
  1667. dst_data_ptr[width * (desti.y + i) + desti.x + j] = src_data_ptr[p_src.width * (srci.y + i) + srci.x + j];
  1668. }
  1669. }
  1670. } else {
  1671. for (int i = 0; i < rrect.size.y; i++) {
  1672. if (i + p_dest.y < 0 || i + p_dest.y >= height)
  1673. continue;
  1674. for (int j = 0; j < rrect.size.x; j++) {
  1675. if (j + p_dest.x < 0 || j + p_dest.x >= width)
  1676. continue;
  1677. _put_pixel(p_dest.x + j, p_dest.y + i, p_src._get_pixel(rrect.pos.x + j, rrect.pos.y + i, src_data_ptr, srcdsize), dst_data_ptr);
  1678. }
  1679. }
  1680. }
  1681. }
  1682. void Image::blit_rect_mask(const Image &p_src, const Image &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1683. int dsize = data.size();
  1684. int srcdsize = p_src.data.size();
  1685. int maskdsize = p_mask.data.size();
  1686. ERR_FAIL_COND(dsize == 0);
  1687. ERR_FAIL_COND(srcdsize == 0);
  1688. ERR_FAIL_COND(maskdsize == 0);
  1689. ERR_FAIL_COND(p_src.width != p_mask.width);
  1690. ERR_FAIL_COND(p_src.height != p_mask.height);
  1691. Rect2 rrect = Rect2(0, 0, p_src.width, p_src.height).clip(p_src_rect);
  1692. DVector<uint8_t>::Write wp = data.write();
  1693. unsigned char *dst_data_ptr = wp.ptr();
  1694. DVector<uint8_t>::Read rp = p_src.data.read();
  1695. const unsigned char *src_data_ptr = rp.ptr();
  1696. DVector<uint8_t>::Read mp = p_mask.data.read();
  1697. const unsigned char *mask_data_ptr = mp.ptr();
  1698. if ((format == FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA) && (p_src.format == FORMAT_INDEXED || p_src.format == FORMAT_INDEXED_ALPHA)) {
  1699. Point2i desti(p_dest.x, p_dest.y);
  1700. Point2i srci(rrect.pos.x, rrect.pos.y);
  1701. for (int i = 0; i < rrect.size.y; i++) {
  1702. if (i + desti.y < 0 || i + desti.y >= height)
  1703. continue;
  1704. for (int j = 0; j < rrect.size.x; j++) {
  1705. if (j + desti.x < 0 || j + desti.x >= width)
  1706. continue;
  1707. BColor msk = p_mask._get_pixel(rrect.pos.x + j, rrect.pos.y + i, mask_data_ptr, maskdsize);
  1708. if (msk.a != 0) {
  1709. dst_data_ptr[width * (desti.y + i) + desti.x + j] = src_data_ptr[p_src.width * (srci.y + i) + srci.x + j];
  1710. }
  1711. }
  1712. }
  1713. } else {
  1714. for (int i = 0; i < rrect.size.y; i++) {
  1715. if (i + p_dest.y < 0 || i + p_dest.y >= height)
  1716. continue;
  1717. for (int j = 0; j < rrect.size.x; j++) {
  1718. if (j + p_dest.x < 0 || j + p_dest.x >= width)
  1719. continue;
  1720. BColor msk = p_mask._get_pixel(rrect.pos.x + j, rrect.pos.y + i, mask_data_ptr, maskdsize);
  1721. if (msk.a != 0) {
  1722. _put_pixel(p_dest.x + j, p_dest.y + i, p_src._get_pixel(rrect.pos.x + j, rrect.pos.y + i, src_data_ptr, srcdsize), dst_data_ptr);
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. void Image::blend_rect(const Image &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1729. int dsize = data.size();
  1730. int srcdsize = p_src.data.size();
  1731. int dst_data_size = data.size();
  1732. ERR_FAIL_COND(dsize == 0);
  1733. ERR_FAIL_COND(srcdsize == 0);
  1734. ERR_FAIL_COND(dst_data_size == 0);
  1735. Rect2 rrect = Rect2(0, 0, p_src.width, p_src.height).clip(p_src_rect);
  1736. DVector<uint8_t>::Write wp = data.write();
  1737. unsigned char *dst_data_ptr = wp.ptr();
  1738. DVector<uint8_t>::Read rp = p_src.data.read();
  1739. const unsigned char *src_data_ptr = rp.ptr();
  1740. if (format == FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA || p_src.format == FORMAT_INDEXED || p_src.format == FORMAT_INDEXED_ALPHA) {
  1741. return;
  1742. } else {
  1743. for (int i = 0; i < rrect.size.y; i++) {
  1744. if (i + p_dest.y < 0 || i + p_dest.y >= height)
  1745. continue;
  1746. for (int j = 0; j < rrect.size.x; j++) {
  1747. if (j + p_dest.x < 0 || j + p_dest.x >= width)
  1748. continue;
  1749. BColor src = p_src._get_pixel(rrect.pos.x + j, rrect.pos.y + i, src_data_ptr, srcdsize);
  1750. BColor dst = _get_pixel(p_dest.x + j, p_dest.y + i, dst_data_ptr, dst_data_size);
  1751. float ba = (float)dst.a / 255.0;
  1752. float fa = (float)src.a / 255.0;
  1753. dst.r = (uint8_t)(fa * src.r + ba * (1.0 - fa) * dst.r);
  1754. dst.g = (uint8_t)(fa * src.g + ba * (1.0 - fa) * dst.g);
  1755. dst.b = (uint8_t)(fa * src.b + ba * (1.0 - fa) * dst.b);
  1756. dst.a = (uint8_t)(255.0 * (fa + ba * (1.0 - fa)));
  1757. _put_pixel(p_dest.x + j, p_dest.y + i, dst, dst_data_ptr);
  1758. }
  1759. }
  1760. }
  1761. }
  1762. void Image::blend_rect_mask(const Image &p_src, const Image &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1763. int dsize = data.size();
  1764. int srcdsize = p_src.data.size();
  1765. int maskdsize = p_mask.data.size();
  1766. int dst_data_size = data.size();
  1767. ERR_FAIL_COND(dsize == 0);
  1768. ERR_FAIL_COND(srcdsize == 0);
  1769. ERR_FAIL_COND(maskdsize == 0);
  1770. ERR_FAIL_COND(dst_data_size == 0);
  1771. ERR_FAIL_COND(p_src.width != p_mask.width);
  1772. ERR_FAIL_COND(p_src.height != p_mask.height);
  1773. Rect2 rrect = Rect2(0, 0, p_src.width, p_src.height).clip(p_src_rect);
  1774. DVector<uint8_t>::Write wp = data.write();
  1775. unsigned char *dst_data_ptr = wp.ptr();
  1776. DVector<uint8_t>::Read rp = p_src.data.read();
  1777. const unsigned char *src_data_ptr = rp.ptr();
  1778. DVector<uint8_t>::Read mrp = p_mask.data.read();
  1779. const unsigned char *mask_data_ptr = mrp.ptr();
  1780. if (format == FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA || p_src.format == FORMAT_INDEXED || p_src.format == FORMAT_INDEXED_ALPHA) {
  1781. return;
  1782. } else {
  1783. for (int i = 0; i < rrect.size.y; i++) {
  1784. if (i + p_dest.y < 0 || i + p_dest.y >= height)
  1785. continue;
  1786. for (int j = 0; j < rrect.size.x; j++) {
  1787. if (j + p_dest.x < 0 || j + p_dest.x >= width)
  1788. continue;
  1789. BColor msk = p_mask._get_pixel(rrect.pos.x + j, rrect.pos.y + i, mask_data_ptr, maskdsize);
  1790. if (msk.a != 0) {
  1791. BColor src = p_src._get_pixel(rrect.pos.x + j, rrect.pos.y + i, src_data_ptr, srcdsize);
  1792. BColor dst = _get_pixel(p_dest.x + j, p_dest.y + i, dst_data_ptr, dst_data_size);
  1793. float ba = (float)dst.a / 255.0;
  1794. float fa = (float)src.a / 255.0;
  1795. dst.r = (uint8_t)(fa * src.r + ba * (1.0 - fa) * dst.r);
  1796. dst.g = (uint8_t)(fa * src.g + ba * (1.0 - fa) * dst.g);
  1797. dst.b = (uint8_t)(fa * src.b + ba * (1.0 - fa) * dst.b);
  1798. dst.a = (uint8_t)(255.0 * (fa + ba * (1.0 - fa)));
  1799. _put_pixel(p_dest.x + j, p_dest.y + i, dst, dst_data_ptr);
  1800. }
  1801. }
  1802. }
  1803. }
  1804. }
  1805. void Image::fill(const Color &p_color) {
  1806. int dsize = data.size();
  1807. ERR_FAIL_COND(dsize == 0);
  1808. DVector<uint8_t>::Write wp = data.write();
  1809. unsigned char *dst_data_ptr = wp.ptr();
  1810. BColor c = BColor(p_color.r * 255, p_color.g * 255, p_color.b * 255, p_color.a * 255);
  1811. for (int i = 0; i < height; i++) {
  1812. for (int j = 0; j < width; j++) {
  1813. _put_pixel(j, i, c, dst_data_ptr);
  1814. }
  1815. }
  1816. }
  1817. Image (*Image::_png_mem_loader_func)(const uint8_t *, int) = NULL;
  1818. Image (*Image::_jpg_mem_loader_func)(const uint8_t *, int) = NULL;
  1819. void (*Image::_image_compress_bc_func)(Image *) = NULL;
  1820. void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL;
  1821. void (*Image::_image_compress_pvrtc4_func)(Image *) = NULL;
  1822. void (*Image::_image_compress_etc_func)(Image *) = NULL;
  1823. void (*Image::_image_decompress_pvrtc)(Image *) = NULL;
  1824. void (*Image::_image_decompress_bc)(Image *) = NULL;
  1825. void (*Image::_image_decompress_etc)(Image *) = NULL;
  1826. DVector<uint8_t> (*Image::lossy_packer)(const Image &, float) = NULL;
  1827. Image (*Image::lossy_unpacker)(const DVector<uint8_t> &) = NULL;
  1828. DVector<uint8_t> (*Image::lossless_packer)(const Image &) = NULL;
  1829. Image (*Image::lossless_unpacker)(const DVector<uint8_t> &) = NULL;
  1830. void Image::set_compress_bc_func(void (*p_compress_func)(Image *)) {
  1831. _image_compress_bc_func = p_compress_func;
  1832. }
  1833. void Image::normalmap_to_xy() {
  1834. convert(Image::FORMAT_RGBA);
  1835. {
  1836. int len = data.size() / 4;
  1837. DVector<uint8_t>::Write wp = data.write();
  1838. unsigned char *data_ptr = wp.ptr();
  1839. for (int i = 0; i < len; i++) {
  1840. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  1841. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  1842. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  1843. }
  1844. }
  1845. convert(Image::FORMAT_GRAYSCALE_ALPHA);
  1846. }
  1847. void Image::srgb_to_linear() {
  1848. if (data.size() == 0)
  1849. return;
  1850. static const uint8_t srgb2lin[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 47, 48, 49, 50, 51, 52, 53, 54, 55, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 101, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 125, 126, 128, 129, 131, 132, 134, 135, 137, 139, 140, 142, 144, 145, 147, 148, 150, 152, 153, 155, 157, 159, 160, 162, 164, 166, 167, 169, 171, 173, 175, 176, 178, 180, 182, 184, 186, 188, 190, 192, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 235, 237, 239, 241, 243, 245, 248, 250, 252 };
  1851. ERR_FAIL_COND(format != FORMAT_RGB && format != FORMAT_RGBA);
  1852. if (format == FORMAT_RGBA) {
  1853. int len = data.size() / 4;
  1854. DVector<uint8_t>::Write wp = data.write();
  1855. unsigned char *data_ptr = wp.ptr();
  1856. for (int i = 0; i < len; i++) {
  1857. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  1858. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  1859. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  1860. }
  1861. } else if (format == FORMAT_RGB) {
  1862. int len = data.size() / 3;
  1863. DVector<uint8_t>::Write wp = data.write();
  1864. unsigned char *data_ptr = wp.ptr();
  1865. for (int i = 0; i < len; i++) {
  1866. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  1867. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  1868. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  1869. }
  1870. }
  1871. }
  1872. void Image::premultiply_alpha() {
  1873. if (data.size() == 0)
  1874. return;
  1875. if (format != FORMAT_RGBA)
  1876. return; //not needed
  1877. DVector<uint8_t>::Write wp = data.write();
  1878. unsigned char *data_ptr = wp.ptr();
  1879. for (int i = 0; i < height; i++) {
  1880. for (int j = 0; j < width; j++) {
  1881. BColor bc = _get_pixel(j, i, data_ptr, 0);
  1882. bc.r = (int(bc.r) * int(bc.a)) >> 8;
  1883. bc.g = (int(bc.g) * int(bc.a)) >> 8;
  1884. bc.b = (int(bc.b) * int(bc.a)) >> 8;
  1885. _put_pixel(j, i, bc, data_ptr);
  1886. }
  1887. }
  1888. }
  1889. void Image::fix_alpha_edges() {
  1890. if (data.size() == 0)
  1891. return;
  1892. if (format != FORMAT_RGBA)
  1893. return; //not needed
  1894. DVector<uint8_t> dcopy = data;
  1895. DVector<uint8_t>::Read rp = data.read();
  1896. const uint8_t *rptr = rp.ptr();
  1897. DVector<uint8_t>::Write wp = data.write();
  1898. unsigned char *data_ptr = wp.ptr();
  1899. const int max_radius = 4;
  1900. const int alpha_treshold = 20;
  1901. const int max_dist = 0x7FFFFFFF;
  1902. for (int i = 0; i < height; i++) {
  1903. for (int j = 0; j < width; j++) {
  1904. BColor bc = _get_pixel(j, i, rptr, 0);
  1905. if (bc.a >= alpha_treshold)
  1906. continue;
  1907. int closest_dist = max_dist;
  1908. BColor closest_color;
  1909. closest_color.a = bc.a;
  1910. int from_x = MAX(0, j - max_radius);
  1911. int to_x = MIN(width - 1, j + max_radius);
  1912. int from_y = MAX(0, i - max_radius);
  1913. int to_y = MIN(height - 1, i + max_radius);
  1914. for (int k = from_y; k <= to_y; k++) {
  1915. for (int l = from_x; l <= to_x; l++) {
  1916. int dy = i - k;
  1917. int dx = j - l;
  1918. int dist = dy * dy + dx * dx;
  1919. if (dist >= closest_dist)
  1920. continue;
  1921. const uint8_t *rp = &rptr[(k * width + l) << 2];
  1922. if (rp[3] < alpha_treshold)
  1923. continue;
  1924. closest_dist = dist;
  1925. closest_color.r = rp[0];
  1926. closest_color.g = rp[1];
  1927. closest_color.b = rp[2];
  1928. }
  1929. }
  1930. if (closest_dist != max_dist)
  1931. _put_pixel(j, i, closest_color, data_ptr);
  1932. }
  1933. }
  1934. }
  1935. String Image::get_format_name(Format p_format) {
  1936. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  1937. return format_names[p_format];
  1938. }
  1939. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  1940. width = 0;
  1941. height = 0;
  1942. mipmaps = 0;
  1943. format = FORMAT_GRAYSCALE;
  1944. if (_png_mem_loader_func) {
  1945. *this = _png_mem_loader_func(p_mem_png_jpg, p_len);
  1946. }
  1947. if (empty() && _jpg_mem_loader_func) {
  1948. *this = _jpg_mem_loader_func(p_mem_png_jpg, p_len);
  1949. }
  1950. }
  1951. Image::Image() {
  1952. width = 0;
  1953. height = 0;
  1954. mipmaps = 0;
  1955. format = FORMAT_GRAYSCALE;
  1956. }
  1957. Image::~Image() {
  1958. }