image.cpp 109 KB

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