image.cpp 67 KB

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