image.cpp 63 KB

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