image.cpp 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  1. /**************************************************************************/
  2. /* image.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/error_macros.h"
  32. #include "core/hash_map.h"
  33. #include "core/io/image_loader.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/math/math_funcs.h"
  36. #include "core/print_string.h"
  37. #include "thirdparty/misc/hq2x.h"
  38. #include <stdio.h>
  39. const char *Image::format_names[Image::FORMAT_MAX] = {
  40. "Lum8", //luminance
  41. "LumAlpha8", //luminance-alpha
  42. "Red8",
  43. "RedGreen",
  44. "RGB8",
  45. "RGBA8",
  46. "RGBA4444",
  47. "RGBA5551",
  48. "RFloat", //float
  49. "RGFloat",
  50. "RGBFloat",
  51. "RGBAFloat",
  52. "RHalf", //half float
  53. "RGHalf",
  54. "RGBHalf",
  55. "RGBAHalf",
  56. "RGBE9995",
  57. "DXT1 RGB8", //s3tc
  58. "DXT3 RGBA8",
  59. "DXT5 RGBA8",
  60. "RGTC Red8",
  61. "RGTC RedGreen8",
  62. "BPTC_RGBA",
  63. "BPTC_RGBF",
  64. "BPTC_RGBFU",
  65. "PVRTC2", //pvrtc
  66. "PVRTC2A",
  67. "PVRTC4",
  68. "PVRTC4A",
  69. "ETC", //etc1
  70. "ETC2_R11", //etc2
  71. "ETC2_R11S", //signed", NOT srgb.
  72. "ETC2_RG11",
  73. "ETC2_RG11S",
  74. "ETC2_RGB8",
  75. "ETC2_RGBA8",
  76. "ETC2_RGB8A1",
  77. };
  78. SavePNGFunc Image::save_png_func = nullptr;
  79. SaveEXRFunc Image::save_exr_func = nullptr;
  80. SavePNGBufferFunc Image::save_png_buffer_func = nullptr;
  81. void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel) {
  82. uint32_t ofs = (p_y * width + p_x) * p_pixel_size;
  83. memcpy(p_data + ofs, p_pixel, p_pixel_size);
  84. }
  85. void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixel_size, const uint8_t *p_data, uint8_t *p_pixel) {
  86. uint32_t ofs = (p_y * width + p_x) * p_pixel_size;
  87. memcpy(p_pixel, p_data + ofs, p_pixel_size);
  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:
  96. return 1;
  97. case FORMAT_RG8:
  98. return 2;
  99. case FORMAT_RGB8:
  100. return 3;
  101. case FORMAT_RGBA8:
  102. return 4;
  103. case FORMAT_RGBA4444:
  104. return 2;
  105. case FORMAT_RGBA5551:
  106. return 2;
  107. case FORMAT_RF:
  108. return 4; //float
  109. case FORMAT_RGF:
  110. return 8;
  111. case FORMAT_RGBF:
  112. return 12;
  113. case FORMAT_RGBAF:
  114. return 16;
  115. case FORMAT_RH:
  116. return 2; //half float
  117. case FORMAT_RGH:
  118. return 4;
  119. case FORMAT_RGBH:
  120. return 6;
  121. case FORMAT_RGBAH:
  122. return 8;
  123. case FORMAT_RGBE9995:
  124. return 4;
  125. case FORMAT_DXT1:
  126. return 1; //s3tc bc1
  127. case FORMAT_DXT3:
  128. return 1; //bc2
  129. case FORMAT_DXT5:
  130. return 1; //bc3
  131. case FORMAT_RGTC_R:
  132. return 1; //bc4
  133. case FORMAT_RGTC_RG:
  134. return 1; //bc5
  135. case FORMAT_BPTC_RGBA:
  136. return 1; //btpc bc6h
  137. case FORMAT_BPTC_RGBF:
  138. return 1; //float /
  139. case FORMAT_BPTC_RGBFU:
  140. return 1; //unsigned float
  141. case FORMAT_PVRTC2:
  142. return 1; //pvrtc
  143. case FORMAT_PVRTC2A:
  144. return 1;
  145. case FORMAT_PVRTC4:
  146. return 1;
  147. case FORMAT_PVRTC4A:
  148. return 1;
  149. case FORMAT_ETC:
  150. return 1; //etc1
  151. case FORMAT_ETC2_R11:
  152. return 1; //etc2
  153. case FORMAT_ETC2_R11S:
  154. return 1; //signed: return 1; NOT srgb.
  155. case FORMAT_ETC2_RG11:
  156. return 1;
  157. case FORMAT_ETC2_RG11S:
  158. return 1;
  159. case FORMAT_ETC2_RGB8:
  160. return 1;
  161. case FORMAT_ETC2_RGBA8:
  162. return 1;
  163. case FORMAT_ETC2_RGB8A1:
  164. return 1;
  165. case FORMAT_MAX: {
  166. }
  167. }
  168. return 0;
  169. }
  170. void Image::get_format_min_pixel_size(Format p_format, int &r_w, int &r_h) {
  171. switch (p_format) {
  172. case FORMAT_DXT1: //s3tc bc1
  173. case FORMAT_DXT3: //bc2
  174. case FORMAT_DXT5: //bc3
  175. case FORMAT_RGTC_R: //bc4
  176. case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1:
  177. r_w = 4;
  178. r_h = 4;
  179. } break;
  180. case FORMAT_PVRTC2:
  181. case FORMAT_PVRTC2A: {
  182. r_w = 16;
  183. r_h = 8;
  184. } break;
  185. case FORMAT_PVRTC4A:
  186. case FORMAT_PVRTC4: {
  187. r_w = 8;
  188. r_h = 8;
  189. } break;
  190. case FORMAT_ETC: {
  191. r_w = 4;
  192. r_h = 4;
  193. } break;
  194. case FORMAT_BPTC_RGBA:
  195. case FORMAT_BPTC_RGBF:
  196. case FORMAT_BPTC_RGBFU: {
  197. r_w = 4;
  198. r_h = 4;
  199. } break;
  200. case FORMAT_ETC2_R11: //etc2
  201. case FORMAT_ETC2_R11S: //signed: NOT srgb.
  202. case FORMAT_ETC2_RG11:
  203. case FORMAT_ETC2_RG11S:
  204. case FORMAT_ETC2_RGB8:
  205. case FORMAT_ETC2_RGBA8:
  206. case FORMAT_ETC2_RGB8A1: {
  207. r_w = 4;
  208. r_h = 4;
  209. } break;
  210. default: {
  211. r_w = 1;
  212. r_h = 1;
  213. } break;
  214. }
  215. }
  216. int Image::get_format_pixel_rshift(Format p_format) {
  217. 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) {
  218. return 1;
  219. } else if (p_format == FORMAT_PVRTC2 || p_format == FORMAT_PVRTC2A) {
  220. return 2;
  221. } else {
  222. return 0;
  223. }
  224. }
  225. int Image::get_format_block_size(Format p_format) {
  226. switch (p_format) {
  227. case FORMAT_DXT1: //s3tc bc1
  228. case FORMAT_DXT3: //bc2
  229. case FORMAT_DXT5: //bc3
  230. case FORMAT_RGTC_R: //bc4
  231. case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1:
  232. return 4;
  233. }
  234. case FORMAT_PVRTC2:
  235. case FORMAT_PVRTC2A: {
  236. return 4;
  237. }
  238. case FORMAT_PVRTC4A:
  239. case FORMAT_PVRTC4: {
  240. return 4;
  241. }
  242. case FORMAT_ETC: {
  243. return 4;
  244. }
  245. case FORMAT_BPTC_RGBA:
  246. case FORMAT_BPTC_RGBF:
  247. case FORMAT_BPTC_RGBFU: {
  248. return 4;
  249. }
  250. case FORMAT_ETC2_R11: //etc2
  251. case FORMAT_ETC2_R11S: //signed: NOT srgb.
  252. case FORMAT_ETC2_RG11:
  253. case FORMAT_ETC2_RG11S:
  254. case FORMAT_ETC2_RGB8:
  255. case FORMAT_ETC2_RGBA8:
  256. case FORMAT_ETC2_RGB8A1: {
  257. return 4;
  258. }
  259. default: {
  260. }
  261. }
  262. return 1;
  263. }
  264. void Image::_get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const {
  265. int w = width;
  266. int h = height;
  267. int ofs = 0;
  268. int pixel_size = get_format_pixel_size(format);
  269. int pixel_rshift = get_format_pixel_rshift(format);
  270. int block = get_format_block_size(format);
  271. int minw, minh;
  272. get_format_min_pixel_size(format, minw, minh);
  273. for (int i = 0; i < p_mipmap; i++) {
  274. int bw = w % block != 0 ? w + (block - w % block) : w;
  275. int bh = h % block != 0 ? h + (block - h % block) : h;
  276. int s = bw * bh;
  277. s *= pixel_size;
  278. s >>= pixel_rshift;
  279. ofs += s;
  280. w = MAX(minw, w >> 1);
  281. h = MAX(minh, h >> 1);
  282. }
  283. r_offset = ofs;
  284. r_width = w;
  285. r_height = h;
  286. }
  287. int Image::get_mipmap_offset(int p_mipmap) const {
  288. ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1);
  289. int ofs, w, h;
  290. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  291. return ofs;
  292. }
  293. void Image::get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const {
  294. int ofs, w, h;
  295. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  296. int ofs2;
  297. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h);
  298. r_ofs = ofs;
  299. r_size = ofs2 - ofs;
  300. }
  301. void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const {
  302. int ofs;
  303. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  304. int ofs2, w2, h2;
  305. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w2, h2);
  306. r_ofs = ofs;
  307. r_size = ofs2 - ofs;
  308. }
  309. int Image::get_width() const {
  310. return width;
  311. }
  312. int Image::get_height() const {
  313. return height;
  314. }
  315. Vector2 Image::get_size() const {
  316. return Vector2(width, height);
  317. }
  318. bool Image::has_mipmaps() const {
  319. return mipmaps;
  320. }
  321. int Image::get_mipmap_count() const {
  322. if (mipmaps) {
  323. return get_image_required_mipmaps(width, height, format);
  324. } else {
  325. return 0;
  326. }
  327. }
  328. //using template generates perfectly optimized code due to constant expression reduction and unused variable removal present in all compilers
  329. template <uint32_t read_bytes, bool read_alpha, uint32_t write_bytes, bool write_alpha, bool read_gray, bool write_gray>
  330. static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p_dst) {
  331. uint32_t max_bytes = MAX(read_bytes, write_bytes);
  332. for (int y = 0; y < p_height; y++) {
  333. for (int x = 0; x < p_width; x++) {
  334. const uint8_t *rofs = &p_src[((y * p_width) + x) * (read_bytes + (read_alpha ? 1 : 0))];
  335. uint8_t *wofs = &p_dst[((y * p_width) + x) * (write_bytes + (write_alpha ? 1 : 0))];
  336. uint8_t rgba[4] = { 0, 0, 0, 255 };
  337. if (read_gray) {
  338. rgba[0] = rofs[0];
  339. rgba[1] = rofs[0];
  340. rgba[2] = rofs[0];
  341. } else {
  342. for (uint32_t i = 0; i < max_bytes; i++) {
  343. rgba[i] = (i < read_bytes) ? rofs[i] : 0;
  344. }
  345. }
  346. if (read_alpha || write_alpha) {
  347. rgba[3] = read_alpha ? rofs[read_bytes] : 255;
  348. }
  349. if (write_gray) {
  350. //TODO: not correct grayscale, should use fixed point version of actual weights
  351. wofs[0] = uint8_t((uint16_t(rgba[0]) + uint16_t(rgba[1]) + uint16_t(rgba[2])) / 3);
  352. } else {
  353. for (uint32_t i = 0; i < write_bytes; i++) {
  354. wofs[i] = rgba[i];
  355. }
  356. }
  357. if (write_alpha) {
  358. wofs[write_bytes] = rgba[3];
  359. }
  360. }
  361. }
  362. }
  363. void Image::convert(Format p_new_format) {
  364. if (data.size() == 0) {
  365. return;
  366. }
  367. if (p_new_format == format) {
  368. return;
  369. }
  370. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot convert image when it is locked.");
  371. if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) {
  372. ERR_FAIL_MSG("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead.");
  373. } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) {
  374. //use put/set pixel which is slower but works with non byte formats
  375. Image new_img(width, height, false, p_new_format);
  376. lock();
  377. new_img.lock();
  378. for (int i = 0; i < width; i++) {
  379. for (int j = 0; j < height; j++) {
  380. new_img.set_pixel(i, j, get_pixel(i, j));
  381. }
  382. }
  383. unlock();
  384. new_img.unlock();
  385. if (has_mipmaps()) {
  386. new_img.generate_mipmaps();
  387. }
  388. _copy_internals_from(new_img);
  389. return;
  390. }
  391. Image new_img(width, height, false, p_new_format);
  392. PoolVector<uint8_t>::Read r = data.read();
  393. PoolVector<uint8_t>::Write w = new_img.data.write();
  394. const uint8_t *rptr = r.ptr();
  395. uint8_t *wptr = w.ptr();
  396. int conversion_type = format | p_new_format << 8;
  397. switch (conversion_type) {
  398. case FORMAT_L8 | (FORMAT_LA8 << 8):
  399. _convert<1, false, 1, true, true, true>(width, height, rptr, wptr);
  400. break;
  401. case FORMAT_L8 | (FORMAT_R8 << 8):
  402. _convert<1, false, 1, false, true, false>(width, height, rptr, wptr);
  403. break;
  404. case FORMAT_L8 | (FORMAT_RG8 << 8):
  405. _convert<1, false, 2, false, true, false>(width, height, rptr, wptr);
  406. break;
  407. case FORMAT_L8 | (FORMAT_RGB8 << 8):
  408. _convert<1, false, 3, false, true, false>(width, height, rptr, wptr);
  409. break;
  410. case FORMAT_L8 | (FORMAT_RGBA8 << 8):
  411. _convert<1, false, 3, true, true, false>(width, height, rptr, wptr);
  412. break;
  413. case FORMAT_LA8 | (FORMAT_L8 << 8):
  414. _convert<1, true, 1, false, true, true>(width, height, rptr, wptr);
  415. break;
  416. case FORMAT_LA8 | (FORMAT_R8 << 8):
  417. _convert<1, true, 1, false, true, false>(width, height, rptr, wptr);
  418. break;
  419. case FORMAT_LA8 | (FORMAT_RG8 << 8):
  420. _convert<1, true, 2, false, true, false>(width, height, rptr, wptr);
  421. break;
  422. case FORMAT_LA8 | (FORMAT_RGB8 << 8):
  423. _convert<1, true, 3, false, true, false>(width, height, rptr, wptr);
  424. break;
  425. case FORMAT_LA8 | (FORMAT_RGBA8 << 8):
  426. _convert<1, true, 3, true, true, false>(width, height, rptr, wptr);
  427. break;
  428. case FORMAT_R8 | (FORMAT_L8 << 8):
  429. _convert<1, false, 1, false, false, true>(width, height, rptr, wptr);
  430. break;
  431. case FORMAT_R8 | (FORMAT_LA8 << 8):
  432. _convert<1, false, 1, true, false, true>(width, height, rptr, wptr);
  433. break;
  434. case FORMAT_R8 | (FORMAT_RG8 << 8):
  435. _convert<1, false, 2, false, false, false>(width, height, rptr, wptr);
  436. break;
  437. case FORMAT_R8 | (FORMAT_RGB8 << 8):
  438. _convert<1, false, 3, false, false, false>(width, height, rptr, wptr);
  439. break;
  440. case FORMAT_R8 | (FORMAT_RGBA8 << 8):
  441. _convert<1, false, 3, true, false, false>(width, height, rptr, wptr);
  442. break;
  443. case FORMAT_RG8 | (FORMAT_L8 << 8):
  444. _convert<2, false, 1, false, false, true>(width, height, rptr, wptr);
  445. break;
  446. case FORMAT_RG8 | (FORMAT_LA8 << 8):
  447. _convert<2, false, 1, true, false, true>(width, height, rptr, wptr);
  448. break;
  449. case FORMAT_RG8 | (FORMAT_R8 << 8):
  450. _convert<2, false, 1, false, false, false>(width, height, rptr, wptr);
  451. break;
  452. case FORMAT_RG8 | (FORMAT_RGB8 << 8):
  453. _convert<2, false, 3, false, false, false>(width, height, rptr, wptr);
  454. break;
  455. case FORMAT_RG8 | (FORMAT_RGBA8 << 8):
  456. _convert<2, false, 3, true, false, false>(width, height, rptr, wptr);
  457. break;
  458. case FORMAT_RGB8 | (FORMAT_L8 << 8):
  459. _convert<3, false, 1, false, false, true>(width, height, rptr, wptr);
  460. break;
  461. case FORMAT_RGB8 | (FORMAT_LA8 << 8):
  462. _convert<3, false, 1, true, false, true>(width, height, rptr, wptr);
  463. break;
  464. case FORMAT_RGB8 | (FORMAT_R8 << 8):
  465. _convert<3, false, 1, false, false, false>(width, height, rptr, wptr);
  466. break;
  467. case FORMAT_RGB8 | (FORMAT_RG8 << 8):
  468. _convert<3, false, 2, false, false, false>(width, height, rptr, wptr);
  469. break;
  470. case FORMAT_RGB8 | (FORMAT_RGBA8 << 8):
  471. _convert<3, false, 3, true, false, false>(width, height, rptr, wptr);
  472. break;
  473. case FORMAT_RGBA8 | (FORMAT_L8 << 8):
  474. _convert<3, true, 1, false, false, true>(width, height, rptr, wptr);
  475. break;
  476. case FORMAT_RGBA8 | (FORMAT_LA8 << 8):
  477. _convert<3, true, 1, true, false, true>(width, height, rptr, wptr);
  478. break;
  479. case FORMAT_RGBA8 | (FORMAT_R8 << 8):
  480. _convert<3, true, 1, false, false, false>(width, height, rptr, wptr);
  481. break;
  482. case FORMAT_RGBA8 | (FORMAT_RG8 << 8):
  483. _convert<3, true, 2, false, false, false>(width, height, rptr, wptr);
  484. break;
  485. case FORMAT_RGBA8 | (FORMAT_RGB8 << 8):
  486. _convert<3, true, 3, false, false, false>(width, height, rptr, wptr);
  487. break;
  488. }
  489. r.release();
  490. w.release();
  491. bool gen_mipmaps = mipmaps;
  492. _copy_internals_from(new_img);
  493. if (gen_mipmaps) {
  494. generate_mipmaps();
  495. }
  496. }
  497. Image::Format Image::get_format() const {
  498. return format;
  499. }
  500. static double _bicubic_interp_kernel(double x) {
  501. x = ABS(x);
  502. double bc = 0;
  503. if (x <= 1) {
  504. bc = (1.5 * x - 2.5) * x * x + 1;
  505. } else if (x < 2) {
  506. bc = ((-0.5 * x + 2.5) * x - 4) * x + 2;
  507. }
  508. return bc;
  509. }
  510. template <int CC, class T>
  511. static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  512. // get source image size
  513. int width = p_src_width;
  514. int height = p_src_height;
  515. double xfac = (double)width / p_dst_width;
  516. double yfac = (double)height / p_dst_height;
  517. // coordinates of source points and coefficients
  518. double ox, oy, dx, dy, k1, k2;
  519. int ox1, oy1, ox2, oy2;
  520. // destination pixel values
  521. // width and height decreased by 1
  522. int ymax = height - 1;
  523. int xmax = width - 1;
  524. // temporary pointer
  525. for (uint32_t y = 0; y < p_dst_height; y++) {
  526. // Y coordinates
  527. oy = (double)y * yfac - 0.5f;
  528. oy1 = (int)oy;
  529. dy = oy - (double)oy1;
  530. for (uint32_t x = 0; x < p_dst_width; x++) {
  531. // X coordinates
  532. ox = (double)x * xfac - 0.5f;
  533. ox1 = (int)ox;
  534. dx = ox - (double)ox1;
  535. // initial pixel value
  536. T *__restrict dst = ((T *)p_dst) + (y * p_dst_width + x) * CC;
  537. double color[CC];
  538. for (int i = 0; i < CC; i++) {
  539. color[i] = 0;
  540. }
  541. for (int n = -1; n < 3; n++) {
  542. // get Y coefficient
  543. k1 = _bicubic_interp_kernel(dy - (double)n);
  544. oy2 = oy1 + n;
  545. if (oy2 < 0) {
  546. oy2 = 0;
  547. }
  548. if (oy2 > ymax) {
  549. oy2 = ymax;
  550. }
  551. for (int m = -1; m < 3; m++) {
  552. // get X coefficient
  553. k2 = k1 * _bicubic_interp_kernel((double)m - dx);
  554. ox2 = ox1 + m;
  555. if (ox2 < 0) {
  556. ox2 = 0;
  557. }
  558. if (ox2 > xmax) {
  559. ox2 = xmax;
  560. }
  561. // get pixel of original image
  562. const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC;
  563. for (int i = 0; i < CC; i++) {
  564. if (sizeof(T) == 2) { //half float
  565. color[i] = Math::half_to_float(p[i]);
  566. } else {
  567. color[i] += p[i] * k2;
  568. }
  569. }
  570. }
  571. }
  572. for (int i = 0; i < CC; i++) {
  573. if (sizeof(T) == 1) { //byte
  574. dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255);
  575. } else if (sizeof(T) == 2) { //half float
  576. dst[i] = Math::make_half_float(color[i]);
  577. } else {
  578. dst[i] = color[i];
  579. }
  580. }
  581. }
  582. }
  583. }
  584. template <int CC, class T>
  585. static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  586. enum {
  587. FRAC_BITS = 8,
  588. FRAC_LEN = (1 << FRAC_BITS),
  589. FRAC_HALF = (FRAC_LEN >> 1),
  590. FRAC_MASK = FRAC_LEN - 1
  591. };
  592. for (uint32_t i = 0; i < p_dst_height; i++) {
  593. // Add 0.5 in order to interpolate based on pixel center
  594. uint32_t src_yofs_up_fp = (i + 0.5) * p_src_height * FRAC_LEN / p_dst_height;
  595. // Calculate nearest src pixel center above current, and truncate to get y index
  596. uint32_t src_yofs_up = src_yofs_up_fp >= FRAC_HALF ? (src_yofs_up_fp - FRAC_HALF) >> FRAC_BITS : 0;
  597. uint32_t src_yofs_down = (src_yofs_up_fp + FRAC_HALF) >> FRAC_BITS;
  598. if (src_yofs_down >= p_src_height) {
  599. src_yofs_down = p_src_height - 1;
  600. }
  601. // Calculate distance to pixel center of src_yofs_up
  602. uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK;
  603. src_yofs_frac = src_yofs_frac >= FRAC_HALF ? src_yofs_frac - FRAC_HALF : src_yofs_frac + FRAC_HALF;
  604. uint32_t y_ofs_up = src_yofs_up * p_src_width * CC;
  605. uint32_t y_ofs_down = src_yofs_down * p_src_width * CC;
  606. for (uint32_t j = 0; j < p_dst_width; j++) {
  607. uint32_t src_xofs_left_fp = (j + 0.5) * p_src_width * FRAC_LEN / p_dst_width;
  608. uint32_t src_xofs_left = src_xofs_left_fp >= FRAC_HALF ? (src_xofs_left_fp - FRAC_HALF) >> FRAC_BITS : 0;
  609. uint32_t src_xofs_right = (src_xofs_left_fp + FRAC_HALF) >> FRAC_BITS;
  610. if (src_xofs_right >= p_src_width) {
  611. src_xofs_right = p_src_width - 1;
  612. }
  613. uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK;
  614. src_xofs_frac = src_xofs_frac >= FRAC_HALF ? src_xofs_frac - FRAC_HALF : src_xofs_frac + FRAC_HALF;
  615. src_xofs_left *= CC;
  616. src_xofs_right *= CC;
  617. for (uint32_t l = 0; l < CC; l++) {
  618. if (sizeof(T) == 1) { //uint8
  619. uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS;
  620. uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS;
  621. uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS;
  622. uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS;
  623. uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS);
  624. uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS);
  625. uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS);
  626. interp >>= FRAC_BITS;
  627. p_dst[i * p_dst_width * CC + j * CC + l] = interp;
  628. } else if (sizeof(T) == 2) { //half float
  629. float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS);
  630. float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS);
  631. const T *src = ((const T *)p_src);
  632. T *dst = ((T *)p_dst);
  633. float p00 = Math::half_to_float(src[y_ofs_up + src_xofs_left + l]);
  634. float p10 = Math::half_to_float(src[y_ofs_up + src_xofs_right + l]);
  635. float p01 = Math::half_to_float(src[y_ofs_down + src_xofs_left + l]);
  636. float p11 = Math::half_to_float(src[y_ofs_down + src_xofs_right + l]);
  637. float interp_up = p00 + (p10 - p00) * xofs_frac;
  638. float interp_down = p01 + (p11 - p01) * xofs_frac;
  639. float interp = interp_up + ((interp_down - interp_up) * yofs_frac);
  640. dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp);
  641. } else if (sizeof(T) == 4) { //float
  642. float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS);
  643. float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS);
  644. const T *src = ((const T *)p_src);
  645. T *dst = ((T *)p_dst);
  646. float p00 = src[y_ofs_up + src_xofs_left + l];
  647. float p10 = src[y_ofs_up + src_xofs_right + l];
  648. float p01 = src[y_ofs_down + src_xofs_left + l];
  649. float p11 = src[y_ofs_down + src_xofs_right + l];
  650. float interp_up = p00 + (p10 - p00) * xofs_frac;
  651. float interp_down = p01 + (p11 - p01) * xofs_frac;
  652. float interp = interp_up + ((interp_down - interp_up) * yofs_frac);
  653. dst[i * p_dst_width * CC + j * CC + l] = interp;
  654. }
  655. }
  656. }
  657. }
  658. }
  659. template <int CC, class T>
  660. static void _scale_nearest(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  661. for (uint32_t i = 0; i < p_dst_height; i++) {
  662. uint32_t src_yofs = i * p_src_height / p_dst_height;
  663. uint32_t y_ofs = src_yofs * p_src_width * CC;
  664. for (uint32_t j = 0; j < p_dst_width; j++) {
  665. uint32_t src_xofs = j * p_src_width / p_dst_width;
  666. src_xofs *= CC;
  667. for (uint32_t l = 0; l < CC; l++) {
  668. const T *src = ((const T *)p_src);
  669. T *dst = ((T *)p_dst);
  670. T p = src[y_ofs + src_xofs + l];
  671. dst[i * p_dst_width * CC + j * CC + l] = p;
  672. }
  673. }
  674. }
  675. }
  676. #define LANCZOS_TYPE 3
  677. static float _lanczos(float p_x) {
  678. return Math::abs(p_x) >= LANCZOS_TYPE ? 0 : Math::sincn(p_x) * Math::sincn(p_x / LANCZOS_TYPE);
  679. }
  680. template <int CC, class T>
  681. static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  682. int32_t src_width = p_src_width;
  683. int32_t src_height = p_src_height;
  684. int32_t dst_height = p_dst_height;
  685. int32_t dst_width = p_dst_width;
  686. uint32_t buffer_size = src_height * dst_width * CC;
  687. float *buffer = memnew_arr(float, buffer_size); // Store the first pass in a buffer
  688. { // FIRST PASS (horizontal)
  689. float x_scale = float(src_width) / float(dst_width);
  690. float scale_factor = MAX(x_scale, 1); // A larger kernel is required only when downscaling
  691. int32_t half_kernel = LANCZOS_TYPE * scale_factor;
  692. float *kernel = memnew_arr(float, half_kernel * 2);
  693. for (int32_t buffer_x = 0; buffer_x < dst_width; buffer_x++) {
  694. // The corresponding point on the source image
  695. float src_x = (buffer_x + 0.5f) * x_scale; // Offset by 0.5 so it uses the pixel's center
  696. int32_t start_x = MAX(0, int32_t(src_x) - half_kernel + 1);
  697. int32_t end_x = MIN(src_width - 1, int32_t(src_x) + half_kernel);
  698. // Create the kernel used by all the pixels of the column
  699. for (int32_t target_x = start_x; target_x <= end_x; target_x++) {
  700. kernel[target_x - start_x] = _lanczos((target_x + 0.5f - src_x) / scale_factor);
  701. }
  702. for (int32_t buffer_y = 0; buffer_y < src_height; buffer_y++) {
  703. float pixel[CC] = { 0 };
  704. float weight = 0;
  705. for (int32_t target_x = start_x; target_x <= end_x; target_x++) {
  706. float lanczos_val = kernel[target_x - start_x];
  707. weight += lanczos_val;
  708. const T *__restrict src_data = ((const T *)p_src) + (buffer_y * src_width + target_x) * CC;
  709. for (uint32_t i = 0; i < CC; i++) {
  710. if (sizeof(T) == 2) { //half float
  711. pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val;
  712. } else {
  713. pixel[i] += src_data[i] * lanczos_val;
  714. }
  715. }
  716. }
  717. float *dst_data = ((float *)buffer) + (buffer_y * dst_width + buffer_x) * CC;
  718. for (uint32_t i = 0; i < CC; i++) {
  719. dst_data[i] = pixel[i] / weight; // Normalize the sum of all the samples
  720. }
  721. }
  722. }
  723. memdelete_arr(kernel);
  724. } // End of first pass
  725. { // SECOND PASS (vertical + result)
  726. float y_scale = float(src_height) / float(dst_height);
  727. float scale_factor = MAX(y_scale, 1);
  728. int32_t half_kernel = LANCZOS_TYPE * scale_factor;
  729. float *kernel = memnew_arr(float, half_kernel * 2);
  730. for (int32_t dst_y = 0; dst_y < dst_height; dst_y++) {
  731. float buffer_y = (dst_y + 0.5f) * y_scale;
  732. int32_t start_y = MAX(0, int32_t(buffer_y) - half_kernel + 1);
  733. int32_t end_y = MIN(src_height - 1, int32_t(buffer_y) + half_kernel);
  734. for (int32_t target_y = start_y; target_y <= end_y; target_y++) {
  735. kernel[target_y - start_y] = _lanczos((target_y + 0.5f - buffer_y) / scale_factor);
  736. }
  737. for (int32_t dst_x = 0; dst_x < dst_width; dst_x++) {
  738. float pixel[CC] = { 0 };
  739. float weight = 0;
  740. for (int32_t target_y = start_y; target_y <= end_y; target_y++) {
  741. float lanczos_val = kernel[target_y - start_y];
  742. weight += lanczos_val;
  743. float *buffer_data = ((float *)buffer) + (target_y * dst_width + dst_x) * CC;
  744. for (uint32_t i = 0; i < CC; i++) {
  745. pixel[i] += buffer_data[i] * lanczos_val;
  746. }
  747. }
  748. T *dst_data = ((T *)p_dst) + (dst_y * dst_width + dst_x) * CC;
  749. for (uint32_t i = 0; i < CC; i++) {
  750. pixel[i] /= weight;
  751. if (sizeof(T) == 1) { //byte
  752. dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255);
  753. } else if (sizeof(T) == 2) { //half float
  754. dst_data[i] = Math::make_half_float(pixel[i]);
  755. } else { // float
  756. dst_data[i] = pixel[i];
  757. }
  758. }
  759. }
  760. }
  761. memdelete_arr(kernel);
  762. } // End of second pass
  763. memdelete_arr(buffer);
  764. }
  765. static void _overlay(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, float p_alpha, uint32_t p_width, uint32_t p_height, uint32_t p_pixel_size) {
  766. uint16_t alpha = MIN((uint16_t)(p_alpha * 256.0f), 256);
  767. for (uint32_t i = 0; i < p_width * p_height * p_pixel_size; i++) {
  768. p_dst[i] = (p_dst[i] * (256 - alpha) + p_src[i] * alpha) >> 8;
  769. }
  770. }
  771. bool Image::is_size_po2() const {
  772. return uint32_t(width) == next_power_of_2(width) && uint32_t(height) == next_power_of_2(height);
  773. }
  774. void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) {
  775. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats.");
  776. int w = next_power_of_2(width);
  777. int h = next_power_of_2(height);
  778. if (p_square) {
  779. w = h = MAX(w, h);
  780. }
  781. if (w == width && h == height) {
  782. if (!p_square || w == h) {
  783. return; //nothing to do
  784. }
  785. }
  786. resize(w, h, p_interpolation);
  787. }
  788. void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
  789. ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use create() or create_from_data() first.");
  790. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats.");
  791. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot resize image when it is locked.");
  792. bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */;
  793. ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0.");
  794. ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0.");
  795. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + ".");
  796. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + ".");
  797. if (p_width == width && p_height == height) {
  798. return;
  799. }
  800. Image dst(p_width, p_height, false, format);
  801. // Setup mipmap-aware scaling
  802. Image dst2;
  803. int mip1 = 0;
  804. int mip2 = 0;
  805. float mip1_weight = 0;
  806. if (mipmap_aware) {
  807. float avg_scale = ((float)p_width / width + (float)p_height / height) * 0.5f;
  808. if (avg_scale >= 1.0f) {
  809. mipmap_aware = false;
  810. } else {
  811. float level = Math::log(1.0f / avg_scale) / Math::log(2.0f);
  812. mip1 = CLAMP((int)Math::floor(level), 0, get_mipmap_count());
  813. mip2 = CLAMP((int)Math::ceil(level), 0, get_mipmap_count());
  814. mip1_weight = 1.0f - (level - mip1);
  815. }
  816. }
  817. bool interpolate_mipmaps = mipmap_aware && mip1 != mip2;
  818. if (interpolate_mipmaps) {
  819. dst2.create(p_width, p_height, false, format);
  820. }
  821. bool had_mipmaps = mipmaps;
  822. if (interpolate_mipmaps && !had_mipmaps) {
  823. generate_mipmaps();
  824. }
  825. // --
  826. PoolVector<uint8_t>::Read r = data.read();
  827. const unsigned char *r_ptr = r.ptr();
  828. PoolVector<uint8_t>::Write w = dst.data.write();
  829. unsigned char *w_ptr = w.ptr();
  830. switch (p_interpolation) {
  831. case INTERPOLATE_NEAREST: {
  832. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  833. switch (get_format_pixel_size(format)) {
  834. case 1:
  835. _scale_nearest<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  836. break;
  837. case 2:
  838. _scale_nearest<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  839. break;
  840. case 3:
  841. _scale_nearest<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  842. break;
  843. case 4:
  844. _scale_nearest<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  845. break;
  846. }
  847. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  848. switch (get_format_pixel_size(format)) {
  849. case 4:
  850. _scale_nearest<1, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  851. break;
  852. case 8:
  853. _scale_nearest<2, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  854. break;
  855. case 12:
  856. _scale_nearest<3, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  857. break;
  858. case 16:
  859. _scale_nearest<4, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  860. break;
  861. }
  862. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  863. switch (get_format_pixel_size(format)) {
  864. case 2:
  865. _scale_nearest<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  866. break;
  867. case 4:
  868. _scale_nearest<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  869. break;
  870. case 6:
  871. _scale_nearest<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  872. break;
  873. case 8:
  874. _scale_nearest<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  875. break;
  876. }
  877. }
  878. } break;
  879. case INTERPOLATE_BILINEAR:
  880. case INTERPOLATE_TRILINEAR: {
  881. for (int i = 0; i < 2; ++i) {
  882. int src_width;
  883. int src_height;
  884. const unsigned char *src_ptr;
  885. if (!mipmap_aware) {
  886. if (i == 0) {
  887. // Standard behavior
  888. src_width = width;
  889. src_height = height;
  890. src_ptr = r_ptr;
  891. } else {
  892. // No need for a second iteration
  893. break;
  894. }
  895. } else {
  896. if (i == 0) {
  897. // Read from the first mipmap that will be interpolated
  898. // (if both levels are the same, we will not interpolate, but at least we'll sample from the right level)
  899. int offs;
  900. _get_mipmap_offset_and_size(mip1, offs, src_width, src_height);
  901. src_ptr = r_ptr + offs;
  902. } else if (!interpolate_mipmaps) {
  903. // No need generate a second image
  904. break;
  905. } else {
  906. // Switch to read from the second mipmap that will be interpolated
  907. int offs;
  908. _get_mipmap_offset_and_size(mip2, offs, src_width, src_height);
  909. src_ptr = r_ptr + offs;
  910. // Switch to write to the second destination image
  911. w = dst2.data.write();
  912. w_ptr = w.ptr();
  913. }
  914. }
  915. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  916. switch (get_format_pixel_size(format)) {
  917. case 1:
  918. _scale_bilinear<1, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  919. break;
  920. case 2:
  921. _scale_bilinear<2, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  922. break;
  923. case 3:
  924. _scale_bilinear<3, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  925. break;
  926. case 4:
  927. _scale_bilinear<4, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  928. break;
  929. }
  930. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  931. switch (get_format_pixel_size(format)) {
  932. case 4:
  933. _scale_bilinear<1, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  934. break;
  935. case 8:
  936. _scale_bilinear<2, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  937. break;
  938. case 12:
  939. _scale_bilinear<3, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  940. break;
  941. case 16:
  942. _scale_bilinear<4, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  943. break;
  944. }
  945. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  946. switch (get_format_pixel_size(format)) {
  947. case 2:
  948. _scale_bilinear<1, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  949. break;
  950. case 4:
  951. _scale_bilinear<2, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  952. break;
  953. case 6:
  954. _scale_bilinear<3, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  955. break;
  956. case 8:
  957. _scale_bilinear<4, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  958. break;
  959. }
  960. }
  961. }
  962. if (interpolate_mipmaps) {
  963. // Switch to read again from the first scaled mipmap to overlay it over the second
  964. r = dst.data.read();
  965. _overlay(r.ptr(), w.ptr(), mip1_weight, p_width, p_height, get_format_pixel_size(format));
  966. }
  967. } break;
  968. case INTERPOLATE_CUBIC: {
  969. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  970. switch (get_format_pixel_size(format)) {
  971. case 1:
  972. _scale_cubic<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  973. break;
  974. case 2:
  975. _scale_cubic<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  976. break;
  977. case 3:
  978. _scale_cubic<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  979. break;
  980. case 4:
  981. _scale_cubic<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  982. break;
  983. }
  984. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  985. switch (get_format_pixel_size(format)) {
  986. case 4:
  987. _scale_cubic<1, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  988. break;
  989. case 8:
  990. _scale_cubic<2, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  991. break;
  992. case 12:
  993. _scale_cubic<3, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  994. break;
  995. case 16:
  996. _scale_cubic<4, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  997. break;
  998. }
  999. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  1000. switch (get_format_pixel_size(format)) {
  1001. case 2:
  1002. _scale_cubic<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1003. break;
  1004. case 4:
  1005. _scale_cubic<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1006. break;
  1007. case 6:
  1008. _scale_cubic<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1009. break;
  1010. case 8:
  1011. _scale_cubic<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1012. break;
  1013. }
  1014. }
  1015. } break;
  1016. case INTERPOLATE_LANCZOS: {
  1017. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  1018. switch (get_format_pixel_size(format)) {
  1019. case 1:
  1020. _scale_lanczos<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1021. break;
  1022. case 2:
  1023. _scale_lanczos<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1024. break;
  1025. case 3:
  1026. _scale_lanczos<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1027. break;
  1028. case 4:
  1029. _scale_lanczos<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1030. break;
  1031. }
  1032. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  1033. switch (get_format_pixel_size(format)) {
  1034. case 4:
  1035. _scale_lanczos<1, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1036. break;
  1037. case 8:
  1038. _scale_lanczos<2, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1039. break;
  1040. case 12:
  1041. _scale_lanczos<3, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1042. break;
  1043. case 16:
  1044. _scale_lanczos<4, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1045. break;
  1046. }
  1047. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  1048. switch (get_format_pixel_size(format)) {
  1049. case 2:
  1050. _scale_lanczos<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1051. break;
  1052. case 4:
  1053. _scale_lanczos<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1054. break;
  1055. case 6:
  1056. _scale_lanczos<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1057. break;
  1058. case 8:
  1059. _scale_lanczos<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1060. break;
  1061. }
  1062. }
  1063. } break;
  1064. }
  1065. r.release();
  1066. w.release();
  1067. if (interpolate_mipmaps) {
  1068. dst._copy_internals_from(dst2);
  1069. }
  1070. if (had_mipmaps) {
  1071. dst.generate_mipmaps();
  1072. }
  1073. _copy_internals_from(dst);
  1074. }
  1075. void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) {
  1076. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot crop in compressed or custom image formats.");
  1077. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot modify image when it is locked.");
  1078. ERR_FAIL_COND_MSG(p_x < 0, "Start x position cannot be smaller than 0.");
  1079. ERR_FAIL_COND_MSG(p_y < 0, "Start y position cannot be smaller than 0.");
  1080. ERR_FAIL_COND_MSG(p_width <= 0, "Width of image must be greater than 0.");
  1081. ERR_FAIL_COND_MSG(p_height <= 0, "Height of image must be greater than 0.");
  1082. ERR_FAIL_COND_MSG(p_x + p_width > MAX_WIDTH, "End x position cannot be greater than " + itos(MAX_WIDTH) + ".");
  1083. ERR_FAIL_COND_MSG(p_y + p_height > MAX_HEIGHT, "End y position cannot be greater than " + itos(MAX_HEIGHT) + ".");
  1084. /* to save memory, cropping should be done in-place, however, since this function
  1085. will most likely either not be used much, or in critical areas, for now it won't, because
  1086. it's a waste of time. */
  1087. if (p_width == width && p_height == height && p_x == 0 && p_y == 0) {
  1088. return;
  1089. }
  1090. uint8_t pdata[16]; //largest is 16
  1091. uint32_t pixel_size = get_format_pixel_size(format);
  1092. Image dst(p_width, p_height, false, format);
  1093. {
  1094. PoolVector<uint8_t>::Read r = data.read();
  1095. PoolVector<uint8_t>::Write w = dst.data.write();
  1096. int m_h = p_y + p_height;
  1097. int m_w = p_x + p_width;
  1098. for (int y = p_y; y < m_h; y++) {
  1099. for (int x = p_x; x < m_w; x++) {
  1100. if ((x >= width || y >= height)) {
  1101. for (uint32_t i = 0; i < pixel_size; i++) {
  1102. pdata[i] = 0;
  1103. }
  1104. } else {
  1105. _get_pixelb(x, y, pixel_size, r.ptr(), pdata);
  1106. }
  1107. dst._put_pixelb(x - p_x, y - p_y, pixel_size, w.ptr(), pdata);
  1108. }
  1109. }
  1110. }
  1111. if (has_mipmaps()) {
  1112. dst.generate_mipmaps();
  1113. }
  1114. _copy_internals_from(dst);
  1115. }
  1116. void Image::crop(int p_width, int p_height) {
  1117. crop_from_point(0, 0, p_width, p_height);
  1118. }
  1119. void Image::flip_y() {
  1120. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_y in compressed or custom image formats.");
  1121. bool used_mipmaps = has_mipmaps();
  1122. if (used_mipmaps) {
  1123. clear_mipmaps();
  1124. }
  1125. {
  1126. PoolVector<uint8_t>::Write w = data.write();
  1127. uint8_t up[16];
  1128. uint8_t down[16];
  1129. uint32_t pixel_size = get_format_pixel_size(format);
  1130. for (int y = 0; y < height / 2; y++) {
  1131. for (int x = 0; x < width; x++) {
  1132. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  1133. _get_pixelb(x, height - y - 1, pixel_size, w.ptr(), down);
  1134. _put_pixelb(x, height - y - 1, pixel_size, w.ptr(), up);
  1135. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  1136. }
  1137. }
  1138. }
  1139. if (used_mipmaps) {
  1140. generate_mipmaps();
  1141. }
  1142. }
  1143. void Image::flip_x() {
  1144. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_x in compressed or custom image formats.");
  1145. bool used_mipmaps = has_mipmaps();
  1146. if (used_mipmaps) {
  1147. clear_mipmaps();
  1148. }
  1149. {
  1150. PoolVector<uint8_t>::Write w = data.write();
  1151. uint8_t up[16];
  1152. uint8_t down[16];
  1153. uint32_t pixel_size = get_format_pixel_size(format);
  1154. for (int y = 0; y < height; y++) {
  1155. for (int x = 0; x < width / 2; x++) {
  1156. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  1157. _get_pixelb(width - x - 1, y, pixel_size, w.ptr(), down);
  1158. _put_pixelb(width - x - 1, y, pixel_size, w.ptr(), up);
  1159. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  1160. }
  1161. }
  1162. }
  1163. if (used_mipmaps) {
  1164. generate_mipmaps();
  1165. }
  1166. }
  1167. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps) {
  1168. int size = 0;
  1169. int w = p_width;
  1170. int h = p_height;
  1171. int mm = 0;
  1172. int pixsize = get_format_pixel_size(p_format);
  1173. int pixshift = get_format_pixel_rshift(p_format);
  1174. int block = get_format_block_size(p_format);
  1175. //technically, you can still compress up to 1 px no matter the format, so commenting this
  1176. //int minw, minh;
  1177. //get_format_min_pixel_size(p_format, minw, minh);
  1178. int minw = 1, minh = 1;
  1179. while (true) {
  1180. int bw = w % block != 0 ? w + (block - w % block) : w;
  1181. int bh = h % block != 0 ? h + (block - h % block) : h;
  1182. int s = bw * bh;
  1183. s *= pixsize;
  1184. s >>= pixshift;
  1185. size += s;
  1186. if (p_mipmaps >= 0 && mm == p_mipmaps) {
  1187. break;
  1188. }
  1189. if (p_mipmaps >= 0) {
  1190. w = MAX(minw, w >> 1);
  1191. h = MAX(minh, h >> 1);
  1192. } else {
  1193. if (w == minw && h == minh) {
  1194. break;
  1195. }
  1196. w = MAX(minw, w >> 1);
  1197. h = MAX(minh, h >> 1);
  1198. }
  1199. mm++;
  1200. };
  1201. r_mipmaps = mm;
  1202. return size;
  1203. }
  1204. bool Image::_can_modify(Format p_format) const {
  1205. return p_format <= FORMAT_RGBE9995;
  1206. }
  1207. template <class Component, int CC, bool renormalize,
  1208. void (*average_func)(Component &, const Component &, const Component &, const Component &, const Component &),
  1209. void (*renormalize_func)(Component *)>
  1210. static void _generate_po2_mipmap(const Component *p_src, Component *p_dst, uint32_t p_width, uint32_t p_height) {
  1211. //fast power of 2 mipmap generation
  1212. uint32_t dst_w = MAX(p_width >> 1, 1);
  1213. uint32_t dst_h = MAX(p_height >> 1, 1);
  1214. int right_step = (p_width == 1) ? 0 : CC;
  1215. int down_step = (p_height == 1) ? 0 : (p_width * CC);
  1216. for (uint32_t i = 0; i < dst_h; i++) {
  1217. const Component *rup_ptr = &p_src[i * 2 * down_step];
  1218. const Component *rdown_ptr = rup_ptr + down_step;
  1219. Component *dst_ptr = &p_dst[i * dst_w * CC];
  1220. uint32_t count = dst_w;
  1221. while (count) {
  1222. count--;
  1223. for (int j = 0; j < CC; j++) {
  1224. average_func(dst_ptr[j], rup_ptr[j], rup_ptr[j + right_step], rdown_ptr[j], rdown_ptr[j + right_step]);
  1225. }
  1226. if (renormalize) {
  1227. renormalize_func(dst_ptr);
  1228. }
  1229. dst_ptr += CC;
  1230. rup_ptr += right_step * 2;
  1231. rdown_ptr += right_step * 2;
  1232. }
  1233. }
  1234. }
  1235. void Image::expand_x2_hq2x() {
  1236. ERR_FAIL_COND(!_can_modify(format));
  1237. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot modify image when it is locked.");
  1238. bool used_mipmaps = has_mipmaps();
  1239. if (used_mipmaps) {
  1240. clear_mipmaps();
  1241. }
  1242. Format current = format;
  1243. if (current != FORMAT_RGBA8) {
  1244. convert(FORMAT_RGBA8);
  1245. }
  1246. PoolVector<uint8_t> dest;
  1247. dest.resize(width * 2 * height * 2 * 4);
  1248. {
  1249. PoolVector<uint8_t>::Read r = data.read();
  1250. PoolVector<uint8_t>::Write w = dest.write();
  1251. ERR_FAIL_COND(!r.ptr());
  1252. hq2x_resize((const uint32_t *)r.ptr(), width, height, (uint32_t *)w.ptr());
  1253. }
  1254. width *= 2;
  1255. height *= 2;
  1256. data = dest;
  1257. if (current != FORMAT_RGBA8) {
  1258. convert(current);
  1259. }
  1260. // FIXME: This is likely meant to use "used_mipmaps" as defined above, but if we do,
  1261. // we end up with a regression: GH-22747
  1262. if (mipmaps) {
  1263. generate_mipmaps();
  1264. }
  1265. }
  1266. void Image::shrink_x2() {
  1267. ERR_FAIL_COND(!_can_modify(format));
  1268. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot modify image when it is locked.");
  1269. ERR_FAIL_COND(data.size() == 0);
  1270. if (mipmaps) {
  1271. //just use the lower mipmap as base and copy all
  1272. PoolVector<uint8_t> new_img;
  1273. int ofs = get_mipmap_offset(1);
  1274. int new_size = data.size() - ofs;
  1275. new_img.resize(new_size);
  1276. ERR_FAIL_COND(new_img.size() == 0);
  1277. {
  1278. PoolVector<uint8_t>::Write w = new_img.write();
  1279. PoolVector<uint8_t>::Read r = data.read();
  1280. memcpy(w.ptr(), &r[ofs], new_size);
  1281. }
  1282. width = MAX(width / 2, 1);
  1283. height = MAX(height / 2, 1);
  1284. data = new_img;
  1285. } else {
  1286. PoolVector<uint8_t> new_img;
  1287. ERR_FAIL_COND(!_can_modify(format));
  1288. int ps = get_format_pixel_size(format);
  1289. new_img.resize((width / 2) * (height / 2) * ps);
  1290. ERR_FAIL_COND(new_img.size() == 0);
  1291. ERR_FAIL_COND(data.size() == 0);
  1292. {
  1293. PoolVector<uint8_t>::Write w = new_img.write();
  1294. PoolVector<uint8_t>::Read r = data.read();
  1295. switch (format) {
  1296. case FORMAT_L8:
  1297. case FORMAT_R8:
  1298. _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height);
  1299. break;
  1300. case FORMAT_LA8:
  1301. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height);
  1302. break;
  1303. case FORMAT_RG8:
  1304. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height);
  1305. break;
  1306. case FORMAT_RGB8:
  1307. _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height);
  1308. break;
  1309. case FORMAT_RGBA8:
  1310. _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height);
  1311. break;
  1312. case FORMAT_RF:
  1313. _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r.ptr()), reinterpret_cast<float *>(w.ptr()), width, height);
  1314. break;
  1315. case FORMAT_RGF:
  1316. _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r.ptr()), reinterpret_cast<float *>(w.ptr()), width, height);
  1317. break;
  1318. case FORMAT_RGBF:
  1319. _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r.ptr()), reinterpret_cast<float *>(w.ptr()), width, height);
  1320. break;
  1321. case FORMAT_RGBAF:
  1322. _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r.ptr()), reinterpret_cast<float *>(w.ptr()), width, height);
  1323. break;
  1324. case FORMAT_RH:
  1325. _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r.ptr()), reinterpret_cast<uint16_t *>(w.ptr()), width, height);
  1326. break;
  1327. case FORMAT_RGH:
  1328. _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r.ptr()), reinterpret_cast<uint16_t *>(w.ptr()), width, height);
  1329. break;
  1330. case FORMAT_RGBH:
  1331. _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r.ptr()), reinterpret_cast<uint16_t *>(w.ptr()), width, height);
  1332. break;
  1333. case FORMAT_RGBAH:
  1334. _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r.ptr()), reinterpret_cast<uint16_t *>(w.ptr()), width, height);
  1335. break;
  1336. case FORMAT_RGBE9995:
  1337. _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(r.ptr()), reinterpret_cast<uint32_t *>(w.ptr()), width, height);
  1338. break;
  1339. default: {
  1340. }
  1341. }
  1342. }
  1343. width /= 2;
  1344. height /= 2;
  1345. data = new_img;
  1346. }
  1347. }
  1348. void Image::normalize() {
  1349. bool used_mipmaps = has_mipmaps();
  1350. if (used_mipmaps) {
  1351. clear_mipmaps();
  1352. }
  1353. lock();
  1354. for (int y = 0; y < height; y++) {
  1355. for (int x = 0; x < width; x++) {
  1356. Color c = get_pixel(x, y);
  1357. Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0);
  1358. v.normalize();
  1359. c.r = v.x * 0.5 + 0.5;
  1360. c.g = v.y * 0.5 + 0.5;
  1361. c.b = v.z * 0.5 + 0.5;
  1362. set_pixel(x, y, c);
  1363. }
  1364. }
  1365. unlock();
  1366. if (used_mipmaps) {
  1367. generate_mipmaps(true);
  1368. }
  1369. }
  1370. Error Image::generate_mipmaps(bool p_renormalize) {
  1371. ERR_FAIL_COND_V_MSG(!_can_modify(format), ERR_UNAVAILABLE, "Cannot generate mipmaps in compressed or custom image formats.");
  1372. ERR_FAIL_COND_V_MSG(write_lock.ptr(), ERR_UNAVAILABLE, "Cannot modify image when it is locked.");
  1373. ERR_FAIL_COND_V_MSG(format == FORMAT_RGBA4444 || format == FORMAT_RGBA5551, ERR_UNAVAILABLE, "Cannot generate mipmaps in custom image formats.");
  1374. ERR_FAIL_COND_V_MSG(width == 0 || height == 0, ERR_UNCONFIGURED, "Cannot generate mipmaps with width or height equal to 0.");
  1375. int mmcount;
  1376. int size = _get_dst_image_size(width, height, format, mmcount);
  1377. data.resize(size);
  1378. PoolVector<uint8_t>::Write wp = data.write();
  1379. int prev_ofs = 0;
  1380. int prev_h = height;
  1381. int prev_w = width;
  1382. for (int i = 1; i <= mmcount; i++) {
  1383. int ofs, w, h;
  1384. _get_mipmap_offset_and_size(i, ofs, w, h);
  1385. switch (format) {
  1386. case FORMAT_L8:
  1387. case FORMAT_R8:
  1388. _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1389. break;
  1390. case FORMAT_LA8:
  1391. case FORMAT_RG8:
  1392. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1393. break;
  1394. case FORMAT_RGB8:
  1395. if (p_renormalize) {
  1396. _generate_po2_mipmap<uint8_t, 3, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1397. } else {
  1398. _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1399. }
  1400. break;
  1401. case FORMAT_RGBA8:
  1402. if (p_renormalize) {
  1403. _generate_po2_mipmap<uint8_t, 4, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1404. } else {
  1405. _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1406. }
  1407. break;
  1408. case FORMAT_RF:
  1409. _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h);
  1410. break;
  1411. case FORMAT_RGF:
  1412. _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h);
  1413. break;
  1414. case FORMAT_RGBF:
  1415. if (p_renormalize) {
  1416. _generate_po2_mipmap<float, 3, true, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h);
  1417. } else {
  1418. _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h);
  1419. }
  1420. break;
  1421. case FORMAT_RGBAF:
  1422. if (p_renormalize) {
  1423. _generate_po2_mipmap<float, 4, true, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h);
  1424. } else {
  1425. _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h);
  1426. }
  1427. break;
  1428. case FORMAT_RH:
  1429. _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h);
  1430. break;
  1431. case FORMAT_RGH:
  1432. _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h);
  1433. break;
  1434. case FORMAT_RGBH:
  1435. if (p_renormalize) {
  1436. _generate_po2_mipmap<uint16_t, 3, true, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h);
  1437. } else {
  1438. _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h);
  1439. }
  1440. break;
  1441. case FORMAT_RGBAH:
  1442. if (p_renormalize) {
  1443. _generate_po2_mipmap<uint16_t, 4, true, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h);
  1444. } else {
  1445. _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h);
  1446. }
  1447. break;
  1448. case FORMAT_RGBE9995:
  1449. if (p_renormalize) {
  1450. _generate_po2_mipmap<uint32_t, 1, true, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(&wp[prev_ofs]), reinterpret_cast<uint32_t *>(&wp[ofs]), prev_w, prev_h);
  1451. } else {
  1452. _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(&wp[prev_ofs]), reinterpret_cast<uint32_t *>(&wp[ofs]), prev_w, prev_h);
  1453. }
  1454. break;
  1455. default: {
  1456. }
  1457. }
  1458. prev_ofs = ofs;
  1459. prev_w = w;
  1460. prev_h = h;
  1461. }
  1462. mipmaps = true;
  1463. return OK;
  1464. }
  1465. void Image::clear_mipmaps() {
  1466. if (!mipmaps) {
  1467. return;
  1468. }
  1469. if (empty()) {
  1470. return;
  1471. }
  1472. int ofs, w, h;
  1473. _get_mipmap_offset_and_size(1, ofs, w, h);
  1474. data.resize(ofs);
  1475. mipmaps = false;
  1476. }
  1477. bool Image::empty() const {
  1478. return (data.size() == 0);
  1479. }
  1480. PoolVector<uint8_t> Image::get_data() const {
  1481. return data;
  1482. }
  1483. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1484. ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels.");
  1485. ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels.");
  1486. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH,
  1487. "The Image width specified (" + itos(p_width) + " pixels) cannot be greater than " + itos(MAX_WIDTH) + "pixels.");
  1488. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT,
  1489. "The Image height specified (" + itos(p_height) + " pixels) cannot be greater than " + itos(MAX_HEIGHT) + "pixels.");
  1490. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot create image when it is locked.");
  1491. ERR_FAIL_INDEX_MSG(p_format, FORMAT_MAX, "The Image format specified (" + itos(p_format) + ") is out of range. See Image's Format enum.");
  1492. int mm = 0;
  1493. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1494. data.resize(size);
  1495. {
  1496. PoolVector<uint8_t>::Write w = data.write();
  1497. memset(w.ptr(), 0, size);
  1498. }
  1499. width = p_width;
  1500. height = p_height;
  1501. mipmaps = p_use_mipmaps;
  1502. format = p_format;
  1503. }
  1504. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  1505. ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels.");
  1506. ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels.");
  1507. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH,
  1508. "The Image width specified (" + itos(p_width) + " pixels) cannot be greater than " + itos(MAX_WIDTH) + " pixels.");
  1509. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT,
  1510. "The Image height specified (" + itos(p_height) + " pixels) cannot be greater than " + itos(MAX_HEIGHT) + " pixels.");
  1511. ERR_FAIL_INDEX_MSG(p_format, FORMAT_MAX, "The Image format specified (" + itos(p_format) + ") is out of range. See Image's Format enum.");
  1512. int mm;
  1513. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1514. if (unlikely(p_data.size() != size)) {
  1515. String description_mipmaps;
  1516. if (p_use_mipmaps) {
  1517. const int num_mipmaps = get_image_required_mipmaps(p_width, p_height, p_format);
  1518. if (num_mipmaps != 1) {
  1519. description_mipmaps = vformat("with %d mipmaps", num_mipmaps);
  1520. } else {
  1521. description_mipmaps = "with 1 mipmap";
  1522. }
  1523. } else {
  1524. description_mipmaps = "without mipmaps";
  1525. }
  1526. const String description = vformat("%dx%dx%d (%s)", p_width, p_height, get_format_pixel_size(p_format), description_mipmaps);
  1527. ERR_FAIL_MSG(vformat("Expected Image data size of %s = %d bytes, got %d bytes instead.", description, size, p_data.size()));
  1528. }
  1529. height = p_height;
  1530. width = p_width;
  1531. format = p_format;
  1532. data = p_data;
  1533. mipmaps = p_use_mipmaps;
  1534. }
  1535. void Image::create(const char **p_xpm) {
  1536. int size_width = 0;
  1537. int size_height = 0;
  1538. int pixelchars = 0;
  1539. mipmaps = false;
  1540. bool has_alpha = false;
  1541. enum Status {
  1542. READING_HEADER,
  1543. READING_COLORS,
  1544. READING_PIXELS,
  1545. DONE
  1546. };
  1547. Status status = READING_HEADER;
  1548. int line = 0;
  1549. HashMap<String, Color> colormap;
  1550. int colormap_size = 0;
  1551. uint32_t pixel_size = 0;
  1552. PoolVector<uint8_t>::Write w;
  1553. while (status != DONE) {
  1554. const char *line_ptr = p_xpm[line];
  1555. switch (status) {
  1556. case READING_HEADER: {
  1557. String line_str = line_ptr;
  1558. line_str.replace("\t", " ");
  1559. size_width = line_str.get_slicec(' ', 0).to_int();
  1560. size_height = line_str.get_slicec(' ', 1).to_int();
  1561. colormap_size = line_str.get_slicec(' ', 2).to_int();
  1562. pixelchars = line_str.get_slicec(' ', 3).to_int();
  1563. ERR_FAIL_COND(colormap_size > 32766);
  1564. ERR_FAIL_COND(pixelchars > 5);
  1565. ERR_FAIL_COND(size_width > 32767);
  1566. ERR_FAIL_COND(size_height > 32767);
  1567. status = READING_COLORS;
  1568. } break;
  1569. case READING_COLORS: {
  1570. String colorstring;
  1571. for (int i = 0; i < pixelchars; i++) {
  1572. colorstring += *line_ptr;
  1573. line_ptr++;
  1574. }
  1575. //skip spaces
  1576. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1577. if (*line_ptr == 0) {
  1578. break;
  1579. }
  1580. line_ptr++;
  1581. }
  1582. if (*line_ptr == 'c') {
  1583. line_ptr++;
  1584. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1585. if (*line_ptr == 0) {
  1586. break;
  1587. }
  1588. line_ptr++;
  1589. }
  1590. if (*line_ptr == '#') {
  1591. line_ptr++;
  1592. uint8_t col_r = 0;
  1593. uint8_t col_g = 0;
  1594. uint8_t col_b = 0;
  1595. //uint8_t col_a=255;
  1596. for (int i = 0; i < 6; i++) {
  1597. char v = line_ptr[i];
  1598. if (v >= '0' && v <= '9') {
  1599. v -= '0';
  1600. } else if (v >= 'A' && v <= 'F') {
  1601. v = (v - 'A') + 10;
  1602. } else if (v >= 'a' && v <= 'f') {
  1603. v = (v - 'a') + 10;
  1604. } else {
  1605. break;
  1606. }
  1607. switch (i) {
  1608. case 0:
  1609. col_r = v << 4;
  1610. break;
  1611. case 1:
  1612. col_r |= v;
  1613. break;
  1614. case 2:
  1615. col_g = v << 4;
  1616. break;
  1617. case 3:
  1618. col_g |= v;
  1619. break;
  1620. case 4:
  1621. col_b = v << 4;
  1622. break;
  1623. case 5:
  1624. col_b |= v;
  1625. break;
  1626. };
  1627. }
  1628. // magenta mask
  1629. if (col_r == 255 && col_g == 0 && col_b == 255) {
  1630. colormap[colorstring] = Color(0, 0, 0, 0);
  1631. has_alpha = true;
  1632. } else {
  1633. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  1634. }
  1635. }
  1636. }
  1637. if (line == colormap_size) {
  1638. status = READING_PIXELS;
  1639. create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8);
  1640. w = data.write();
  1641. pixel_size = has_alpha ? 4 : 3;
  1642. }
  1643. } break;
  1644. case READING_PIXELS: {
  1645. int y = line - colormap_size - 1;
  1646. for (int x = 0; x < size_width; x++) {
  1647. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  1648. for (int i = 0; i < pixelchars; i++) {
  1649. pixelstr[i] = line_ptr[x * pixelchars + i];
  1650. }
  1651. Color *colorptr = colormap.getptr(pixelstr);
  1652. ERR_FAIL_COND(!colorptr);
  1653. uint8_t pixel[4];
  1654. for (uint32_t i = 0; i < pixel_size; i++) {
  1655. pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255);
  1656. }
  1657. _put_pixelb(x, y, pixel_size, w.ptr(), pixel);
  1658. }
  1659. if (y == (size_height - 1)) {
  1660. status = DONE;
  1661. }
  1662. } break;
  1663. default: {
  1664. }
  1665. }
  1666. line++;
  1667. }
  1668. }
  1669. #define DETECT_ALPHA_MAX_THRESHOLD 254
  1670. #define DETECT_ALPHA_MIN_THRESHOLD 2
  1671. #define DETECT_ALPHA(m_value) \
  1672. { \
  1673. uint8_t value = m_value; \
  1674. if (value < DETECT_ALPHA_MIN_THRESHOLD) \
  1675. bit = true; \
  1676. else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \
  1677. detected = true; \
  1678. break; \
  1679. } \
  1680. }
  1681. #define DETECT_NON_ALPHA(m_value) \
  1682. { \
  1683. uint8_t value = m_value; \
  1684. if (value > 0) { \
  1685. detected = true; \
  1686. break; \
  1687. } \
  1688. }
  1689. bool Image::is_invisible() const {
  1690. if (format == FORMAT_L8 ||
  1691. format == FORMAT_RGB8 || format == FORMAT_RG8) {
  1692. return false;
  1693. }
  1694. int len = data.size();
  1695. if (len == 0) {
  1696. return true;
  1697. }
  1698. int w, h;
  1699. _get_mipmap_offset_and_size(1, len, w, h);
  1700. PoolVector<uint8_t>::Read r = data.read();
  1701. const unsigned char *data_ptr = r.ptr();
  1702. bool detected = false;
  1703. switch (format) {
  1704. case FORMAT_LA8: {
  1705. for (int i = 0; i < (len >> 1); i++) {
  1706. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  1707. }
  1708. } break;
  1709. case FORMAT_RGBA8: {
  1710. for (int i = 0; i < (len >> 2); i++) {
  1711. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  1712. }
  1713. } break;
  1714. case FORMAT_PVRTC2A:
  1715. case FORMAT_PVRTC4A:
  1716. case FORMAT_DXT3:
  1717. case FORMAT_DXT5: {
  1718. detected = true;
  1719. } break;
  1720. default: {
  1721. }
  1722. }
  1723. return !detected;
  1724. }
  1725. Image::AlphaMode Image::detect_alpha() const {
  1726. int len = data.size();
  1727. if (len == 0) {
  1728. return ALPHA_NONE;
  1729. }
  1730. int w, h;
  1731. _get_mipmap_offset_and_size(1, len, w, h);
  1732. PoolVector<uint8_t>::Read r = data.read();
  1733. const unsigned char *data_ptr = r.ptr();
  1734. bool bit = false;
  1735. bool detected = false;
  1736. switch (format) {
  1737. case FORMAT_LA8: {
  1738. for (int i = 0; i < (len >> 1); i++) {
  1739. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  1740. }
  1741. } break;
  1742. case FORMAT_RGBA8: {
  1743. for (int i = 0; i < (len >> 2); i++) {
  1744. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  1745. }
  1746. } break;
  1747. case FORMAT_PVRTC2A:
  1748. case FORMAT_PVRTC4A:
  1749. case FORMAT_DXT3:
  1750. case FORMAT_DXT5: {
  1751. detected = true;
  1752. } break;
  1753. default: {
  1754. }
  1755. }
  1756. if (detected) {
  1757. return ALPHA_BLEND;
  1758. } else if (bit) {
  1759. return ALPHA_BIT;
  1760. } else {
  1761. return ALPHA_NONE;
  1762. }
  1763. }
  1764. Error Image::load(const String &p_path) {
  1765. #ifdef DEBUG_ENABLED
  1766. if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
  1767. WARN_PRINT("Loaded resource as image file, this will not work on export: '" + p_path + "'. Instead, import the image file as an Image resource and load it normally as a resource.");
  1768. }
  1769. #endif
  1770. return ImageLoader::load_image(p_path, this);
  1771. }
  1772. Error Image::save_png(const String &p_path) const {
  1773. if (save_png_func == nullptr) {
  1774. return ERR_UNAVAILABLE;
  1775. }
  1776. return save_png_func(p_path, Ref<Image>((Image *)this));
  1777. }
  1778. PoolVector<uint8_t> Image::save_png_to_buffer() const {
  1779. if (save_png_buffer_func == nullptr) {
  1780. return PoolVector<uint8_t>();
  1781. }
  1782. return save_png_buffer_func(Ref<Image>((Image *)this));
  1783. }
  1784. Error Image::save_exr(const String &p_path, bool p_grayscale) const {
  1785. if (save_exr_func == nullptr) {
  1786. return ERR_UNAVAILABLE;
  1787. }
  1788. return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale);
  1789. }
  1790. int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) {
  1791. int mm;
  1792. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0);
  1793. }
  1794. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1795. int mm;
  1796. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  1797. return mm;
  1798. }
  1799. int Image::get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap) {
  1800. if (p_mipmap <= 0) {
  1801. return 0;
  1802. }
  1803. int mm;
  1804. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1);
  1805. }
  1806. bool Image::is_compressed() const {
  1807. return format > FORMAT_RGBE9995;
  1808. }
  1809. Error Image::decompress() {
  1810. if (format >= FORMAT_DXT1 && format <= FORMAT_RGTC_RG && _image_decompress_bc) {
  1811. _image_decompress_bc(this);
  1812. } else if (format >= FORMAT_BPTC_RGBA && format <= FORMAT_BPTC_RGBFU && _image_decompress_bptc) {
  1813. _image_decompress_bptc(this);
  1814. } else if (format >= FORMAT_PVRTC2 && format <= FORMAT_PVRTC4A && _image_decompress_pvrtc) {
  1815. _image_decompress_pvrtc(this);
  1816. } else if (format == FORMAT_ETC && _image_decompress_etc1) {
  1817. _image_decompress_etc1(this);
  1818. } else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RGB8A1 && _image_decompress_etc2) {
  1819. _image_decompress_etc2(this);
  1820. } else {
  1821. return ERR_UNAVAILABLE;
  1822. }
  1823. return OK;
  1824. }
  1825. Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
  1826. ERR_FAIL_INDEX_V_MSG(p_mode, COMPRESS_MAX, ERR_INVALID_PARAMETER, "Invalid compress mode.");
  1827. ERR_FAIL_INDEX_V_MSG(p_source, COMPRESS_SOURCE_MAX, ERR_INVALID_PARAMETER, "Invalid compress source.");
  1828. switch (p_mode) {
  1829. case COMPRESS_S3TC: {
  1830. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1831. _image_compress_bc_func(this, p_lossy_quality, p_source);
  1832. } break;
  1833. case COMPRESS_PVRTC2: {
  1834. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1835. _image_compress_pvrtc2_func(this);
  1836. } break;
  1837. case COMPRESS_PVRTC4: {
  1838. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1839. _image_compress_pvrtc4_func(this);
  1840. } break;
  1841. case COMPRESS_ETC: {
  1842. ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE);
  1843. _image_compress_etc1_func(this, p_lossy_quality);
  1844. } break;
  1845. case COMPRESS_ETC2: {
  1846. ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE);
  1847. _image_compress_etc2_func(this, p_lossy_quality, p_source);
  1848. } break;
  1849. case COMPRESS_BPTC: {
  1850. ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE);
  1851. _image_compress_bptc_func(this, p_lossy_quality, p_source);
  1852. } break;
  1853. case COMPRESS_MAX: {
  1854. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  1855. } break;
  1856. }
  1857. return OK;
  1858. }
  1859. Image::Image(const char **p_xpm) {
  1860. width = 0;
  1861. height = 0;
  1862. mipmaps = false;
  1863. format = FORMAT_L8;
  1864. create(p_xpm);
  1865. }
  1866. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1867. width = 0;
  1868. height = 0;
  1869. mipmaps = p_use_mipmaps;
  1870. format = FORMAT_L8;
  1871. create(p_width, p_height, p_use_mipmaps, p_format);
  1872. }
  1873. Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  1874. width = 0;
  1875. height = 0;
  1876. mipmaps = p_mipmaps;
  1877. format = FORMAT_L8;
  1878. create(p_width, p_height, p_mipmaps, p_format, p_data);
  1879. }
  1880. Rect2 Image::get_used_rect() const {
  1881. if (format != FORMAT_LA8 && format != FORMAT_RGBA8 && format != FORMAT_RGBAF && format != FORMAT_RGBAH && format != FORMAT_RGBA4444 && format != FORMAT_RGBA5551) {
  1882. return Rect2(Point2(), Size2(width, height));
  1883. }
  1884. int len = data.size();
  1885. if (len == 0) {
  1886. return Rect2();
  1887. }
  1888. const_cast<Image *>(this)->lock();
  1889. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  1890. int maxx = -1, maxy = -1;
  1891. for (int j = 0; j < height; j++) {
  1892. for (int i = 0; i < width; i++) {
  1893. if (!(get_pixel(i, j).a > 0)) {
  1894. continue;
  1895. }
  1896. if (i > maxx) {
  1897. maxx = i;
  1898. }
  1899. if (j > maxy) {
  1900. maxy = j;
  1901. }
  1902. if (i < minx) {
  1903. minx = i;
  1904. }
  1905. if (j < miny) {
  1906. miny = j;
  1907. }
  1908. }
  1909. }
  1910. const_cast<Image *>(this)->unlock();
  1911. if (maxx == -1) {
  1912. return Rect2();
  1913. } else {
  1914. return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1);
  1915. }
  1916. }
  1917. Ref<Image> Image::get_rect(const Rect2 &p_area) const {
  1918. Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format));
  1919. img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0));
  1920. return img;
  1921. }
  1922. void Image::_get_clipped_src_and_dest_rects(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest, Rect2i &r_clipped_src_rect, Rect2i &r_clipped_dest_rect) const {
  1923. r_clipped_dest_rect.position = p_dest;
  1924. r_clipped_src_rect = p_src_rect;
  1925. if (r_clipped_src_rect.position.x < 0) {
  1926. r_clipped_dest_rect.position.x -= r_clipped_src_rect.position.x;
  1927. r_clipped_src_rect.size.x += r_clipped_src_rect.position.x;
  1928. r_clipped_src_rect.position.x = 0;
  1929. }
  1930. if (r_clipped_src_rect.position.y < 0) {
  1931. r_clipped_dest_rect.position.y -= r_clipped_src_rect.position.y;
  1932. r_clipped_src_rect.size.y += r_clipped_src_rect.position.y;
  1933. r_clipped_src_rect.position.y = 0;
  1934. }
  1935. if (r_clipped_dest_rect.position.x < 0) {
  1936. r_clipped_src_rect.position.x -= r_clipped_dest_rect.position.x;
  1937. r_clipped_src_rect.size.x += r_clipped_dest_rect.position.x;
  1938. r_clipped_dest_rect.position.x = 0;
  1939. }
  1940. if (r_clipped_dest_rect.position.y < 0) {
  1941. r_clipped_src_rect.position.y -= r_clipped_dest_rect.position.y;
  1942. r_clipped_src_rect.size.y += r_clipped_dest_rect.position.y;
  1943. r_clipped_dest_rect.position.y = 0;
  1944. }
  1945. r_clipped_src_rect.size.x = MAX(0, MIN(r_clipped_src_rect.size.x, MIN(p_src->width - r_clipped_src_rect.position.x, width - r_clipped_dest_rect.position.x)));
  1946. r_clipped_src_rect.size.y = MAX(0, MIN(r_clipped_src_rect.size.y, MIN(p_src->height - r_clipped_src_rect.position.y, height - r_clipped_dest_rect.position.y)));
  1947. r_clipped_dest_rect.size.x = r_clipped_src_rect.size.x;
  1948. r_clipped_dest_rect.size.y = r_clipped_src_rect.size.y;
  1949. }
  1950. void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1951. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  1952. int dsize = data.size();
  1953. int srcdsize = p_src->data.size();
  1954. ERR_FAIL_COND(dsize == 0);
  1955. ERR_FAIL_COND(srcdsize == 0);
  1956. ERR_FAIL_COND(format != p_src->format);
  1957. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot blit_rect in compressed or custom image formats.");
  1958. Rect2i src_rect;
  1959. Rect2i dest_rect;
  1960. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  1961. if (src_rect.has_no_area() || dest_rect.has_no_area()) {
  1962. return;
  1963. }
  1964. PoolVector<uint8_t>::Write wp = data.write();
  1965. uint8_t *dst_data_ptr = wp.ptr();
  1966. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1967. const uint8_t *src_data_ptr = rp.ptr();
  1968. int pixel_size = get_format_pixel_size(format);
  1969. for (int i = 0; i < dest_rect.size.y; i++) {
  1970. for (int j = 0; j < dest_rect.size.x; j++) {
  1971. int src_x = src_rect.position.x + j;
  1972. int src_y = src_rect.position.y + i;
  1973. int dst_x = dest_rect.position.x + j;
  1974. int dst_y = dest_rect.position.y + i;
  1975. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1976. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1977. for (int k = 0; k < pixel_size; k++) {
  1978. dst[k] = src[k];
  1979. }
  1980. }
  1981. }
  1982. }
  1983. void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1984. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  1985. ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object.");
  1986. int dsize = data.size();
  1987. int srcdsize = p_src->data.size();
  1988. int maskdsize = p_mask->data.size();
  1989. ERR_FAIL_COND(dsize == 0);
  1990. ERR_FAIL_COND(srcdsize == 0);
  1991. ERR_FAIL_COND(maskdsize == 0);
  1992. ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width.");
  1993. ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height.");
  1994. ERR_FAIL_COND(format != p_src->format);
  1995. Rect2i src_rect;
  1996. Rect2i dest_rect;
  1997. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  1998. if (src_rect.has_no_area() || dest_rect.has_no_area()) {
  1999. return;
  2000. }
  2001. PoolVector<uint8_t>::Write wp = data.write();
  2002. uint8_t *dst_data_ptr = wp.ptr();
  2003. PoolVector<uint8_t>::Read rp = p_src->data.read();
  2004. const uint8_t *src_data_ptr = rp.ptr();
  2005. int pixel_size = get_format_pixel_size(format);
  2006. Ref<Image> msk = p_mask;
  2007. msk->lock();
  2008. for (int i = 0; i < dest_rect.size.y; i++) {
  2009. for (int j = 0; j < dest_rect.size.x; j++) {
  2010. int src_x = src_rect.position.x + j;
  2011. int src_y = src_rect.position.y + i;
  2012. if (msk->get_pixel(src_x, src_y).a != 0) {
  2013. int dst_x = dest_rect.position.x + j;
  2014. int dst_y = dest_rect.position.y + i;
  2015. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  2016. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  2017. for (int k = 0; k < pixel_size; k++) {
  2018. dst[k] = src[k];
  2019. }
  2020. }
  2021. }
  2022. }
  2023. msk->unlock();
  2024. }
  2025. void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  2026. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  2027. int dsize = data.size();
  2028. int srcdsize = p_src->data.size();
  2029. ERR_FAIL_COND(dsize == 0);
  2030. ERR_FAIL_COND(srcdsize == 0);
  2031. ERR_FAIL_COND(format != p_src->format);
  2032. Rect2i src_rect;
  2033. Rect2i dest_rect;
  2034. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  2035. if (src_rect.has_no_area() || dest_rect.has_no_area()) {
  2036. return;
  2037. }
  2038. lock();
  2039. Ref<Image> img = p_src;
  2040. img->lock();
  2041. for (int i = 0; i < dest_rect.size.y; i++) {
  2042. for (int j = 0; j < dest_rect.size.x; j++) {
  2043. int src_x = src_rect.position.x + j;
  2044. int src_y = src_rect.position.y + i;
  2045. int dst_x = dest_rect.position.x + j;
  2046. int dst_y = dest_rect.position.y + i;
  2047. Color sc = img->get_pixel(src_x, src_y);
  2048. if (sc.a != 0) {
  2049. Color dc = get_pixel(dst_x, dst_y);
  2050. dc = dc.blend(sc);
  2051. set_pixel(dst_x, dst_y, dc);
  2052. }
  2053. }
  2054. }
  2055. img->unlock();
  2056. unlock();
  2057. }
  2058. void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  2059. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  2060. ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object.");
  2061. int dsize = data.size();
  2062. int srcdsize = p_src->data.size();
  2063. int maskdsize = p_mask->data.size();
  2064. ERR_FAIL_COND(dsize == 0);
  2065. ERR_FAIL_COND(srcdsize == 0);
  2066. ERR_FAIL_COND(maskdsize == 0);
  2067. ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width.");
  2068. ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height.");
  2069. ERR_FAIL_COND(format != p_src->format);
  2070. Rect2i src_rect;
  2071. Rect2i dest_rect;
  2072. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  2073. if (src_rect.has_no_area() || dest_rect.has_no_area()) {
  2074. return;
  2075. }
  2076. lock();
  2077. Ref<Image> img = p_src;
  2078. Ref<Image> msk = p_mask;
  2079. img->lock();
  2080. msk->lock();
  2081. for (int i = 0; i < dest_rect.size.y; i++) {
  2082. for (int j = 0; j < dest_rect.size.x; j++) {
  2083. int src_x = src_rect.position.x + j;
  2084. int src_y = src_rect.position.y + i;
  2085. // If the mask's pixel is transparent then we skip it
  2086. //Color c = msk->get_pixel(src_x, src_y);
  2087. //if (c.a == 0) continue;
  2088. if (msk->get_pixel(src_x, src_y).a != 0) {
  2089. int dst_x = dest_rect.position.x + j;
  2090. int dst_y = dest_rect.position.y + i;
  2091. Color sc = img->get_pixel(src_x, src_y);
  2092. if (sc.a != 0) {
  2093. Color dc = get_pixel(dst_x, dst_y);
  2094. dc = dc.blend(sc);
  2095. set_pixel(dst_x, dst_y, dc);
  2096. }
  2097. }
  2098. }
  2099. }
  2100. msk->unlock();
  2101. img->unlock();
  2102. unlock();
  2103. }
  2104. // Repeats `p_pixel` `p_count` times in consecutive memory.
  2105. // Results in the original pixel and `p_count - 1` subsequent copies of it.
  2106. void Image::_repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_size, int p_count) {
  2107. int offset = 1;
  2108. for (int stride = 1; offset + stride <= p_count; stride *= 2) {
  2109. memcpy(p_pixel + offset * p_pixel_size, p_pixel, stride * p_pixel_size);
  2110. offset += stride;
  2111. }
  2112. if (offset < p_count) {
  2113. memcpy(p_pixel + offset * p_pixel_size, p_pixel, (p_count - offset) * p_pixel_size);
  2114. }
  2115. }
  2116. void Image::fill(const Color &p_color) {
  2117. if (data.size() == 0) {
  2118. return;
  2119. }
  2120. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats.");
  2121. lock();
  2122. PoolVector<uint8_t>::Write wp = data.write();
  2123. uint8_t *dst_data_ptr = wp.ptr();
  2124. int pixel_size = get_format_pixel_size(format);
  2125. // Put first pixel with the format-aware API.
  2126. set_pixel(0, 0, p_color);
  2127. _repeat_pixel_over_subsequent_memory(dst_data_ptr, pixel_size, width * height);
  2128. unlock();
  2129. }
  2130. void Image::fill_rect(const Rect2 &p_rect, const Color &p_color) {
  2131. if (data.size() == 0) {
  2132. return;
  2133. }
  2134. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill rect in compressed or custom image formats.");
  2135. Rect2i r = Rect2i(0, 0, width, height).clip(p_rect.abs());
  2136. if (r.has_no_area()) {
  2137. return;
  2138. }
  2139. lock();
  2140. PoolVector<uint8_t>::Write wp = data.write();
  2141. uint8_t *dst_data_ptr = wp.ptr();
  2142. int pixel_size = get_format_pixel_size(format);
  2143. // Put first pixel with the format-aware API.
  2144. uint8_t *rect_first_pixel_ptr = &dst_data_ptr[(r.position.y * width + r.position.x) * pixel_size];
  2145. set_pixelv(r.position, p_color);
  2146. if (r.size.x == width) {
  2147. // No need to fill rows separately.
  2148. _repeat_pixel_over_subsequent_memory(rect_first_pixel_ptr, pixel_size, width * r.size.y);
  2149. } else {
  2150. _repeat_pixel_over_subsequent_memory(rect_first_pixel_ptr, pixel_size, r.size.x);
  2151. for (int y = 1; y < r.size.y; y++) {
  2152. memcpy(rect_first_pixel_ptr + y * width * pixel_size, rect_first_pixel_ptr, r.size.x * pixel_size);
  2153. }
  2154. }
  2155. unlock();
  2156. }
  2157. ImageMemLoadFunc Image::_png_mem_loader_func = nullptr;
  2158. ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr;
  2159. ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr;
  2160. ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
  2161. ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
  2162. void (*Image::_image_compress_bc_func)(Image *, float, Image::CompressSource) = nullptr;
  2163. void (*Image::_image_compress_bptc_func)(Image *, float, Image::CompressSource) = nullptr;
  2164. void (*Image::_image_compress_pvrtc2_func)(Image *) = nullptr;
  2165. void (*Image::_image_compress_pvrtc4_func)(Image *) = nullptr;
  2166. void (*Image::_image_compress_etc1_func)(Image *, float) = nullptr;
  2167. void (*Image::_image_compress_etc2_func)(Image *, float, Image::CompressSource) = nullptr;
  2168. void (*Image::_image_decompress_pvrtc)(Image *) = nullptr;
  2169. void (*Image::_image_decompress_bc)(Image *) = nullptr;
  2170. void (*Image::_image_decompress_bptc)(Image *) = nullptr;
  2171. void (*Image::_image_decompress_etc1)(Image *) = nullptr;
  2172. void (*Image::_image_decompress_etc2)(Image *) = nullptr;
  2173. PoolVector<uint8_t> (*Image::webp_lossy_packer)(const Ref<Image> &, float) = nullptr;
  2174. PoolVector<uint8_t> (*Image::webp_lossless_packer)(const Ref<Image> &) = nullptr;
  2175. Ref<Image> (*Image::webp_unpacker)(const PoolVector<uint8_t> &) = nullptr;
  2176. PoolVector<uint8_t> (*Image::png_packer)(const Ref<Image> &) = nullptr;
  2177. Ref<Image> (*Image::png_unpacker)(const PoolVector<uint8_t> &) = nullptr;
  2178. void Image::_set_data(const Dictionary &p_data) {
  2179. ERR_FAIL_COND(!p_data.has("width"));
  2180. ERR_FAIL_COND(!p_data.has("height"));
  2181. ERR_FAIL_COND(!p_data.has("format"));
  2182. ERR_FAIL_COND(!p_data.has("mipmaps"));
  2183. ERR_FAIL_COND(!p_data.has("data"));
  2184. int dwidth = p_data["width"];
  2185. int dheight = p_data["height"];
  2186. String dformat = p_data["format"];
  2187. bool dmipmaps = p_data["mipmaps"];
  2188. PoolVector<uint8_t> ddata = p_data["data"];
  2189. Format ddformat = FORMAT_MAX;
  2190. for (int i = 0; i < FORMAT_MAX; i++) {
  2191. if (dformat == get_format_name(Format(i))) {
  2192. ddformat = Format(i);
  2193. break;
  2194. }
  2195. }
  2196. ERR_FAIL_COND(ddformat == FORMAT_MAX);
  2197. create(dwidth, dheight, dmipmaps, ddformat, ddata);
  2198. }
  2199. Dictionary Image::_get_data() const {
  2200. Dictionary d;
  2201. d["width"] = width;
  2202. d["height"] = height;
  2203. d["format"] = get_format_name(format);
  2204. d["mipmaps"] = mipmaps;
  2205. d["data"] = data;
  2206. return d;
  2207. }
  2208. void Image::lock() {
  2209. ERR_FAIL_COND(data.size() == 0);
  2210. write_lock = data.write();
  2211. }
  2212. void Image::unlock() {
  2213. write_lock.release();
  2214. }
  2215. Color Image::get_pixelv(const Point2 &p_src) const {
  2216. return get_pixel(p_src.x, p_src.y);
  2217. }
  2218. Color Image::get_pixel(int p_x, int p_y) const {
  2219. uint8_t *ptr = write_lock.ptr();
  2220. #ifdef DEBUG_ENABLED
  2221. ERR_FAIL_COND_V_MSG(!ptr, Color(), "Image must be locked with 'lock()' before using get_pixel().");
  2222. ERR_FAIL_INDEX_V(p_x, width, Color());
  2223. ERR_FAIL_INDEX_V(p_y, height, Color());
  2224. #endif
  2225. uint32_t ofs = p_y * width + p_x;
  2226. switch (format) {
  2227. case FORMAT_L8: {
  2228. float l = ptr[ofs] / 255.0;
  2229. return Color(l, l, l, 1);
  2230. }
  2231. case FORMAT_LA8: {
  2232. float l = ptr[ofs * 2 + 0] / 255.0;
  2233. float a = ptr[ofs * 2 + 1] / 255.0;
  2234. return Color(l, l, l, a);
  2235. }
  2236. case FORMAT_R8: {
  2237. float r = ptr[ofs] / 255.0;
  2238. return Color(r, 0, 0, 1);
  2239. }
  2240. case FORMAT_RG8: {
  2241. float r = ptr[ofs * 2 + 0] / 255.0;
  2242. float g = ptr[ofs * 2 + 1] / 255.0;
  2243. return Color(r, g, 0, 1);
  2244. }
  2245. case FORMAT_RGB8: {
  2246. float r = ptr[ofs * 3 + 0] / 255.0;
  2247. float g = ptr[ofs * 3 + 1] / 255.0;
  2248. float b = ptr[ofs * 3 + 2] / 255.0;
  2249. return Color(r, g, b, 1);
  2250. }
  2251. case FORMAT_RGBA8: {
  2252. float r = ptr[ofs * 4 + 0] / 255.0;
  2253. float g = ptr[ofs * 4 + 1] / 255.0;
  2254. float b = ptr[ofs * 4 + 2] / 255.0;
  2255. float a = ptr[ofs * 4 + 3] / 255.0;
  2256. return Color(r, g, b, a);
  2257. }
  2258. case FORMAT_RGBA4444: {
  2259. uint16_t u = ((uint16_t *)ptr)[ofs];
  2260. float r = ((u >> 12) & 0xF) / 15.0;
  2261. float g = ((u >> 8) & 0xF) / 15.0;
  2262. float b = ((u >> 4) & 0xF) / 15.0;
  2263. float a = (u & 0xF) / 15.0;
  2264. return Color(r, g, b, a);
  2265. }
  2266. case FORMAT_RGBA5551: {
  2267. uint16_t u = ((uint16_t *)ptr)[ofs];
  2268. float r = ((u >> 11) & 0x1F) / 15.0;
  2269. float g = ((u >> 6) & 0x1F) / 15.0;
  2270. float b = ((u >> 1) & 0x1F) / 15.0;
  2271. float a = (u & 0x1) / 1.0;
  2272. return Color(r, g, b, a);
  2273. }
  2274. case FORMAT_RF: {
  2275. float r = ((float *)ptr)[ofs];
  2276. return Color(r, 0, 0, 1);
  2277. }
  2278. case FORMAT_RGF: {
  2279. float r = ((float *)ptr)[ofs * 2 + 0];
  2280. float g = ((float *)ptr)[ofs * 2 + 1];
  2281. return Color(r, g, 0, 1);
  2282. }
  2283. case FORMAT_RGBF: {
  2284. float r = ((float *)ptr)[ofs * 3 + 0];
  2285. float g = ((float *)ptr)[ofs * 3 + 1];
  2286. float b = ((float *)ptr)[ofs * 3 + 2];
  2287. return Color(r, g, b, 1);
  2288. }
  2289. case FORMAT_RGBAF: {
  2290. float r = ((float *)ptr)[ofs * 4 + 0];
  2291. float g = ((float *)ptr)[ofs * 4 + 1];
  2292. float b = ((float *)ptr)[ofs * 4 + 2];
  2293. float a = ((float *)ptr)[ofs * 4 + 3];
  2294. return Color(r, g, b, a);
  2295. }
  2296. case FORMAT_RH: {
  2297. uint16_t r = ((uint16_t *)ptr)[ofs];
  2298. return Color(Math::half_to_float(r), 0, 0, 1);
  2299. }
  2300. case FORMAT_RGH: {
  2301. uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0];
  2302. uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1];
  2303. return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1);
  2304. }
  2305. case FORMAT_RGBH: {
  2306. uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0];
  2307. uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1];
  2308. uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2];
  2309. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1);
  2310. }
  2311. case FORMAT_RGBAH: {
  2312. uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0];
  2313. uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1];
  2314. uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2];
  2315. uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3];
  2316. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a));
  2317. }
  2318. case FORMAT_RGBE9995: {
  2319. return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]);
  2320. }
  2321. default: {
  2322. ERR_FAIL_V_MSG(Color(), "Can't get_pixel() on compressed image, sorry.");
  2323. }
  2324. }
  2325. }
  2326. void Image::set_pixelv(const Point2 &p_dst, const Color &p_color) {
  2327. set_pixel(p_dst.x, p_dst.y, p_color);
  2328. }
  2329. void Image::set_pixel(int p_x, int p_y, const Color &p_color) {
  2330. uint8_t *ptr = write_lock.ptr();
  2331. #ifdef DEBUG_ENABLED
  2332. ERR_FAIL_COND_MSG(!ptr, "Image must be locked with 'lock()' before using set_pixel().");
  2333. ERR_FAIL_INDEX(p_x, width);
  2334. ERR_FAIL_INDEX(p_y, height);
  2335. #endif
  2336. uint32_t ofs = p_y * width + p_x;
  2337. switch (format) {
  2338. case FORMAT_L8: {
  2339. ptr[ofs] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  2340. } break;
  2341. case FORMAT_LA8: {
  2342. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  2343. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  2344. } break;
  2345. case FORMAT_R8: {
  2346. ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2347. } break;
  2348. case FORMAT_RG8: {
  2349. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2350. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2351. } break;
  2352. case FORMAT_RGB8: {
  2353. ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2354. ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2355. ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  2356. } break;
  2357. case FORMAT_RGBA8: {
  2358. ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2359. ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2360. ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  2361. ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  2362. } break;
  2363. case FORMAT_RGBA4444: {
  2364. uint16_t rgba = 0;
  2365. rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15)) << 12;
  2366. rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 8;
  2367. rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 4;
  2368. rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15));
  2369. ((uint16_t *)ptr)[ofs] = rgba;
  2370. } break;
  2371. case FORMAT_RGBA5551: {
  2372. uint16_t rgba = 0;
  2373. rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31)) << 11;
  2374. rgba |= uint16_t(CLAMP(p_color.g * 31.0, 0, 31)) << 6;
  2375. rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 1;
  2376. rgba |= uint16_t(p_color.a > 0.5 ? 1 : 0);
  2377. ((uint16_t *)ptr)[ofs] = rgba;
  2378. } break;
  2379. case FORMAT_RF: {
  2380. ((float *)ptr)[ofs] = p_color.r;
  2381. } break;
  2382. case FORMAT_RGF: {
  2383. ((float *)ptr)[ofs * 2 + 0] = p_color.r;
  2384. ((float *)ptr)[ofs * 2 + 1] = p_color.g;
  2385. } break;
  2386. case FORMAT_RGBF: {
  2387. ((float *)ptr)[ofs * 3 + 0] = p_color.r;
  2388. ((float *)ptr)[ofs * 3 + 1] = p_color.g;
  2389. ((float *)ptr)[ofs * 3 + 2] = p_color.b;
  2390. } break;
  2391. case FORMAT_RGBAF: {
  2392. ((float *)ptr)[ofs * 4 + 0] = p_color.r;
  2393. ((float *)ptr)[ofs * 4 + 1] = p_color.g;
  2394. ((float *)ptr)[ofs * 4 + 2] = p_color.b;
  2395. ((float *)ptr)[ofs * 4 + 3] = p_color.a;
  2396. } break;
  2397. case FORMAT_RH: {
  2398. ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r);
  2399. } break;
  2400. case FORMAT_RGH: {
  2401. ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r);
  2402. ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g);
  2403. } break;
  2404. case FORMAT_RGBH: {
  2405. ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r);
  2406. ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g);
  2407. ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b);
  2408. } break;
  2409. case FORMAT_RGBAH: {
  2410. ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r);
  2411. ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g);
  2412. ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b);
  2413. ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a);
  2414. } break;
  2415. case FORMAT_RGBE9995: {
  2416. ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995();
  2417. } break;
  2418. default: {
  2419. ERR_FAIL_MSG("Can't set_pixel() on compressed image, sorry.");
  2420. }
  2421. }
  2422. }
  2423. Image::DetectChannels Image::get_detected_channels() {
  2424. ERR_FAIL_COND_V(data.size() == 0, DETECTED_RGBA);
  2425. ERR_FAIL_COND_V(is_compressed(), DETECTED_RGBA);
  2426. bool r = false, g = false, b = false, a = false, c = false;
  2427. lock();
  2428. for (int i = 0; i < width; i++) {
  2429. for (int j = 0; j < height; j++) {
  2430. Color col = get_pixel(i, j);
  2431. if (col.r > 0.001) {
  2432. r = true;
  2433. }
  2434. if (col.g > 0.001) {
  2435. g = true;
  2436. }
  2437. if (col.b > 0.001) {
  2438. b = true;
  2439. }
  2440. if (col.a < 0.999) {
  2441. a = true;
  2442. }
  2443. if (col.r != col.b || col.r != col.g || col.b != col.g) {
  2444. c = true;
  2445. }
  2446. }
  2447. }
  2448. unlock();
  2449. if (!c && !a) {
  2450. return DETECTED_L;
  2451. }
  2452. if (!c && a) {
  2453. return DETECTED_LA;
  2454. }
  2455. if (r && !g && !b && !a) {
  2456. return DETECTED_R;
  2457. }
  2458. if (r && g && !b && !a) {
  2459. return DETECTED_RG;
  2460. }
  2461. if (r && g && b && !a) {
  2462. return DETECTED_RGB;
  2463. }
  2464. return DETECTED_RGBA;
  2465. }
  2466. void Image::optimize_channels() {
  2467. switch (get_detected_channels()) {
  2468. case DETECTED_L:
  2469. convert(FORMAT_L8);
  2470. break;
  2471. case DETECTED_LA:
  2472. convert(FORMAT_LA8);
  2473. break;
  2474. case DETECTED_R:
  2475. convert(FORMAT_R8);
  2476. break;
  2477. case DETECTED_RG:
  2478. convert(FORMAT_RG8);
  2479. break;
  2480. case DETECTED_RGB:
  2481. convert(FORMAT_RGB8);
  2482. break;
  2483. case DETECTED_RGBA:
  2484. convert(FORMAT_RGBA8);
  2485. break;
  2486. }
  2487. }
  2488. void Image::_bind_methods() {
  2489. ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
  2490. ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height);
  2491. ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size);
  2492. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps);
  2493. ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format);
  2494. ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data);
  2495. ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert);
  2496. ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset);
  2497. ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR));
  2498. ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR));
  2499. ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2);
  2500. ClassDB::bind_method(D_METHOD("expand_x2_hq2x"), &Image::expand_x2_hq2x);
  2501. ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop);
  2502. ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x);
  2503. ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y);
  2504. ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false));
  2505. ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps);
  2506. ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty);
  2507. ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data);
  2508. ClassDB::bind_method(D_METHOD("is_empty"), &Image::empty);
  2509. ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
  2510. ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
  2511. ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer);
  2512. ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false));
  2513. ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
  2514. ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);
  2515. ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress);
  2516. ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress);
  2517. ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed);
  2518. ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges);
  2519. ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha);
  2520. ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear);
  2521. ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy);
  2522. ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb);
  2523. ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap", "bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0));
  2524. ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect);
  2525. ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask);
  2526. ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect);
  2527. ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask);
  2528. ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill);
  2529. ClassDB::bind_method(D_METHOD("fill_rect", "rect", "color"), &Image::fill_rect);
  2530. ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
  2531. ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect);
  2532. ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from);
  2533. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data);
  2534. ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data);
  2535. ClassDB::bind_method(D_METHOD("lock"), &Image::lock);
  2536. ClassDB::bind_method(D_METHOD("unlock"), &Image::unlock);
  2537. ClassDB::bind_method(D_METHOD("get_pixelv", "src"), &Image::get_pixelv);
  2538. ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel);
  2539. ClassDB::bind_method(D_METHOD("set_pixelv", "dst", "color"), &Image::set_pixelv);
  2540. ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel);
  2541. ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer);
  2542. ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer);
  2543. ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
  2544. ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
  2545. ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer);
  2546. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
  2547. BIND_CONSTANT(MAX_WIDTH);
  2548. BIND_CONSTANT(MAX_HEIGHT);
  2549. BIND_ENUM_CONSTANT(FORMAT_L8); //luminance
  2550. BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha
  2551. BIND_ENUM_CONSTANT(FORMAT_R8);
  2552. BIND_ENUM_CONSTANT(FORMAT_RG8);
  2553. BIND_ENUM_CONSTANT(FORMAT_RGB8);
  2554. BIND_ENUM_CONSTANT(FORMAT_RGBA8);
  2555. BIND_ENUM_CONSTANT(FORMAT_RGBA4444);
  2556. BIND_ENUM_CONSTANT(FORMAT_RGBA5551);
  2557. BIND_ENUM_CONSTANT(FORMAT_RF); //float
  2558. BIND_ENUM_CONSTANT(FORMAT_RGF);
  2559. BIND_ENUM_CONSTANT(FORMAT_RGBF);
  2560. BIND_ENUM_CONSTANT(FORMAT_RGBAF);
  2561. BIND_ENUM_CONSTANT(FORMAT_RH); //half float
  2562. BIND_ENUM_CONSTANT(FORMAT_RGH);
  2563. BIND_ENUM_CONSTANT(FORMAT_RGBH);
  2564. BIND_ENUM_CONSTANT(FORMAT_RGBAH);
  2565. BIND_ENUM_CONSTANT(FORMAT_RGBE9995);
  2566. BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1
  2567. BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2
  2568. BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3
  2569. BIND_ENUM_CONSTANT(FORMAT_RGTC_R);
  2570. BIND_ENUM_CONSTANT(FORMAT_RGTC_RG);
  2571. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h
  2572. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float /
  2573. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float
  2574. BIND_ENUM_CONSTANT(FORMAT_PVRTC2); //pvrtc
  2575. BIND_ENUM_CONSTANT(FORMAT_PVRTC2A);
  2576. BIND_ENUM_CONSTANT(FORMAT_PVRTC4);
  2577. BIND_ENUM_CONSTANT(FORMAT_PVRTC4A);
  2578. BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1
  2579. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2
  2580. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb.
  2581. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11);
  2582. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S);
  2583. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8);
  2584. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8);
  2585. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1);
  2586. BIND_ENUM_CONSTANT(FORMAT_MAX);
  2587. BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST);
  2588. BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR);
  2589. BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC);
  2590. BIND_ENUM_CONSTANT(INTERPOLATE_TRILINEAR);
  2591. BIND_ENUM_CONSTANT(INTERPOLATE_LANCZOS);
  2592. BIND_ENUM_CONSTANT(ALPHA_NONE);
  2593. BIND_ENUM_CONSTANT(ALPHA_BIT);
  2594. BIND_ENUM_CONSTANT(ALPHA_BLEND);
  2595. BIND_ENUM_CONSTANT(COMPRESS_S3TC);
  2596. BIND_ENUM_CONSTANT(COMPRESS_PVRTC2);
  2597. BIND_ENUM_CONSTANT(COMPRESS_PVRTC4);
  2598. BIND_ENUM_CONSTANT(COMPRESS_ETC);
  2599. BIND_ENUM_CONSTANT(COMPRESS_ETC2);
  2600. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC);
  2601. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB);
  2602. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
  2603. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_LAYERED);
  2604. }
  2605. void Image::set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource)) {
  2606. _image_compress_bc_func = p_compress_func;
  2607. }
  2608. void Image::set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource)) {
  2609. _image_compress_bptc_func = p_compress_func;
  2610. }
  2611. void Image::normalmap_to_xy() {
  2612. convert(Image::FORMAT_RGBA8);
  2613. {
  2614. int len = data.size() / 4;
  2615. PoolVector<uint8_t>::Write wp = data.write();
  2616. unsigned char *data_ptr = wp.ptr();
  2617. for (int i = 0; i < len; i++) {
  2618. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  2619. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  2620. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  2621. }
  2622. }
  2623. convert(Image::FORMAT_LA8);
  2624. }
  2625. Ref<Image> Image::rgbe_to_srgb() {
  2626. if (data.size() == 0) {
  2627. return Ref<Image>();
  2628. }
  2629. ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>());
  2630. Ref<Image> new_image;
  2631. new_image.instance();
  2632. new_image->create(width, height, false, Image::FORMAT_RGB8);
  2633. lock();
  2634. new_image->lock();
  2635. for (int row = 0; row < height; row++) {
  2636. for (int col = 0; col < width; col++) {
  2637. new_image->set_pixel(col, row, get_pixel(col, row).to_srgb());
  2638. }
  2639. }
  2640. unlock();
  2641. new_image->unlock();
  2642. if (has_mipmaps()) {
  2643. new_image->generate_mipmaps();
  2644. }
  2645. return new_image;
  2646. }
  2647. void Image::bumpmap_to_normalmap(float bump_scale) {
  2648. ERR_FAIL_COND(!_can_modify(format));
  2649. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot modify image when it is locked.");
  2650. clear_mipmaps();
  2651. convert(Image::FORMAT_RF);
  2652. PoolVector<uint8_t> result_image; //rgba output
  2653. result_image.resize(width * height * 4);
  2654. {
  2655. PoolVector<uint8_t>::Read rp = data.read();
  2656. PoolVector<uint8_t>::Write wp = result_image.write();
  2657. ERR_FAIL_COND(!rp.ptr());
  2658. unsigned char *write_ptr = wp.ptr();
  2659. float *read_ptr = (float *)rp.ptr();
  2660. for (int ty = 0; ty < height; ty++) {
  2661. int py = ty + 1;
  2662. if (py >= height) {
  2663. py -= height;
  2664. }
  2665. for (int tx = 0; tx < width; tx++) {
  2666. int px = tx + 1;
  2667. if (px >= width) {
  2668. px -= width;
  2669. }
  2670. float here = read_ptr[ty * width + tx];
  2671. float to_right = read_ptr[ty * width + px];
  2672. float above = read_ptr[py * width + tx];
  2673. Vector3 up = Vector3(0, 1, (here - above) * bump_scale);
  2674. Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale);
  2675. Vector3 normal = across.cross(up);
  2676. normal.normalize();
  2677. write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5);
  2678. write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5);
  2679. write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5);
  2680. write_ptr[((ty * width + tx) << 2) + 3] = 255;
  2681. }
  2682. }
  2683. }
  2684. format = FORMAT_RGBA8;
  2685. data = result_image;
  2686. }
  2687. void Image::srgb_to_linear() {
  2688. if (data.size() == 0) {
  2689. return;
  2690. }
  2691. 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, 255 };
  2692. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  2693. if (format == FORMAT_RGBA8) {
  2694. int len = data.size() / 4;
  2695. PoolVector<uint8_t>::Write wp = data.write();
  2696. unsigned char *data_ptr = wp.ptr();
  2697. for (int i = 0; i < len; i++) {
  2698. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  2699. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  2700. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  2701. }
  2702. } else if (format == FORMAT_RGB8) {
  2703. int len = data.size() / 3;
  2704. PoolVector<uint8_t>::Write wp = data.write();
  2705. unsigned char *data_ptr = wp.ptr();
  2706. for (int i = 0; i < len; i++) {
  2707. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  2708. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  2709. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  2710. }
  2711. }
  2712. }
  2713. void Image::premultiply_alpha() {
  2714. if (data.size() == 0) {
  2715. return;
  2716. }
  2717. if (format != FORMAT_RGBA8) {
  2718. return; //not needed
  2719. }
  2720. PoolVector<uint8_t>::Write wp = data.write();
  2721. unsigned char *data_ptr = wp.ptr();
  2722. for (int i = 0; i < height; i++) {
  2723. for (int j = 0; j < width; j++) {
  2724. uint8_t *ptr = &data_ptr[(i * width + j) * 4];
  2725. ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8;
  2726. ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8;
  2727. ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8;
  2728. }
  2729. }
  2730. }
  2731. void Image::fix_alpha_edges() {
  2732. ERR_FAIL_COND(!_can_modify(format));
  2733. ERR_FAIL_COND_MSG(write_lock.ptr(), "Cannot modify image when it is locked.");
  2734. if (data.size() == 0) {
  2735. return;
  2736. }
  2737. if (format != FORMAT_RGBA8) {
  2738. return; //not needed
  2739. }
  2740. PoolVector<uint8_t> dcopy = data;
  2741. PoolVector<uint8_t>::Read rp = dcopy.read();
  2742. const uint8_t *srcptr = rp.ptr();
  2743. PoolVector<uint8_t>::Write wp = data.write();
  2744. unsigned char *data_ptr = wp.ptr();
  2745. const int max_radius = 4;
  2746. const int alpha_threshold = 20;
  2747. const int max_dist = 0x7FFFFFFF;
  2748. for (int i = 0; i < height; i++) {
  2749. for (int j = 0; j < width; j++) {
  2750. const uint8_t *rptr = &srcptr[(i * width + j) * 4];
  2751. uint8_t *wptr = &data_ptr[(i * width + j) * 4];
  2752. if (rptr[3] >= alpha_threshold) {
  2753. continue;
  2754. }
  2755. int closest_dist = max_dist;
  2756. uint8_t closest_color[3];
  2757. int from_x = MAX(0, j - max_radius);
  2758. int to_x = MIN(width - 1, j + max_radius);
  2759. int from_y = MAX(0, i - max_radius);
  2760. int to_y = MIN(height - 1, i + max_radius);
  2761. for (int k = from_y; k <= to_y; k++) {
  2762. for (int l = from_x; l <= to_x; l++) {
  2763. int dy = i - k;
  2764. int dx = j - l;
  2765. int dist = dy * dy + dx * dx;
  2766. if (dist >= closest_dist) {
  2767. continue;
  2768. }
  2769. const uint8_t *rp2 = &srcptr[(k * width + l) << 2];
  2770. if (rp2[3] < alpha_threshold) {
  2771. continue;
  2772. }
  2773. closest_dist = dist;
  2774. closest_color[0] = rp2[0];
  2775. closest_color[1] = rp2[1];
  2776. closest_color[2] = rp2[2];
  2777. }
  2778. }
  2779. if (closest_dist != max_dist) {
  2780. wptr[0] = closest_color[0];
  2781. wptr[1] = closest_color[1];
  2782. wptr[2] = closest_color[2];
  2783. }
  2784. }
  2785. }
  2786. }
  2787. String Image::get_format_name(Format p_format) {
  2788. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  2789. return format_names[p_format];
  2790. }
  2791. Error Image::load_png_from_buffer(const PoolVector<uint8_t> &p_array) {
  2792. return _load_from_buffer(p_array, _png_mem_loader_func);
  2793. }
  2794. Error Image::load_jpg_from_buffer(const PoolVector<uint8_t> &p_array) {
  2795. return _load_from_buffer(p_array, _jpg_mem_loader_func);
  2796. }
  2797. Error Image::load_webp_from_buffer(const PoolVector<uint8_t> &p_array) {
  2798. return _load_from_buffer(p_array, _webp_mem_loader_func);
  2799. }
  2800. Error Image::load_tga_from_buffer(const PoolVector<uint8_t> &p_array) {
  2801. ERR_FAIL_NULL_V_MSG(_tga_mem_loader_func, ERR_UNAVAILABLE, "TGA module was not installed.");
  2802. return _load_from_buffer(p_array, _tga_mem_loader_func);
  2803. }
  2804. Error Image::load_bmp_from_buffer(const PoolVector<uint8_t> &p_array) {
  2805. ERR_FAIL_NULL_V_MSG(
  2806. _bmp_mem_loader_func,
  2807. ERR_UNAVAILABLE,
  2808. "The BMP module isn't enabled. Recompile the Godot editor or export template binary with the `module_bmp_enabled=yes` SCons option.");
  2809. return _load_from_buffer(p_array, _bmp_mem_loader_func);
  2810. }
  2811. Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
  2812. int buffer_size = p_array.size();
  2813. ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
  2814. ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER);
  2815. PoolVector<uint8_t>::Read r = p_array.read();
  2816. Ref<Image> image = p_loader(r.ptr(), buffer_size);
  2817. ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR);
  2818. copy_internals_from(image);
  2819. return OK;
  2820. }
  2821. void Image::average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d) {
  2822. p_out = static_cast<uint8_t>((p_a + p_b + p_c + p_d + 2) >> 2);
  2823. }
  2824. void Image::average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d) {
  2825. p_out = (p_a + p_b + p_c + p_d) * 0.25f;
  2826. }
  2827. void Image::average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d) {
  2828. p_out = Math::make_half_float((Math::half_to_float(p_a) + Math::half_to_float(p_b) + Math::half_to_float(p_c) + Math::half_to_float(p_d)) * 0.25f);
  2829. }
  2830. void Image::average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d) {
  2831. p_out = ((Color::from_rgbe9995(p_a) + Color::from_rgbe9995(p_b) + Color::from_rgbe9995(p_c) + Color::from_rgbe9995(p_d)) * 0.25f).to_rgbe9995();
  2832. }
  2833. void Image::renormalize_uint8(uint8_t *p_rgb) {
  2834. Vector3 n(p_rgb[0] / 255.0, p_rgb[1] / 255.0, p_rgb[2] / 255.0);
  2835. n *= 2.0;
  2836. n -= Vector3(1, 1, 1);
  2837. n.normalize();
  2838. n += Vector3(1, 1, 1);
  2839. n *= 0.5;
  2840. n *= 255;
  2841. p_rgb[0] = CLAMP(int(n.x), 0, 255);
  2842. p_rgb[1] = CLAMP(int(n.y), 0, 255);
  2843. p_rgb[2] = CLAMP(int(n.z), 0, 255);
  2844. }
  2845. void Image::renormalize_float(float *p_rgb) {
  2846. Vector3 n(p_rgb[0], p_rgb[1], p_rgb[2]);
  2847. n.normalize();
  2848. p_rgb[0] = n.x;
  2849. p_rgb[1] = n.y;
  2850. p_rgb[2] = n.z;
  2851. }
  2852. void Image::renormalize_half(uint16_t *p_rgb) {
  2853. Vector3 n(Math::half_to_float(p_rgb[0]), Math::half_to_float(p_rgb[1]), Math::half_to_float(p_rgb[2]));
  2854. n.normalize();
  2855. p_rgb[0] = Math::make_half_float(n.x);
  2856. p_rgb[1] = Math::make_half_float(n.y);
  2857. p_rgb[2] = Math::make_half_float(n.z);
  2858. }
  2859. void Image::renormalize_rgbe9995(uint32_t *p_rgb) {
  2860. // Never used
  2861. }
  2862. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  2863. width = 0;
  2864. height = 0;
  2865. mipmaps = false;
  2866. format = FORMAT_L8;
  2867. if (_png_mem_loader_func) {
  2868. copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len));
  2869. }
  2870. if (empty() && _jpg_mem_loader_func) {
  2871. copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len));
  2872. }
  2873. }
  2874. Ref<Resource> Image::duplicate(bool p_subresources) const {
  2875. Ref<Image> copy;
  2876. copy.instance();
  2877. copy->_copy_internals_from(*this);
  2878. return copy;
  2879. }
  2880. Image::Image() {
  2881. width = 0;
  2882. height = 0;
  2883. mipmaps = false;
  2884. format = FORMAT_L8;
  2885. }
  2886. Image::~Image() {
  2887. if (write_lock.ptr()) {
  2888. unlock();
  2889. }
  2890. }