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