image.cpp 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646
  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] = uint8_t(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. /// Get mipmap size and offset.
  1244. 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) {
  1245. // Data offset in mipmaps (including the original texture).
  1246. int size = 0;
  1247. int w = p_width;
  1248. int h = p_height;
  1249. // Current mipmap index in the loop below. p_mipmaps is the target mipmap index.
  1250. // In this function, mipmap 0 represents the first mipmap instead of the original texture.
  1251. int mm = 0;
  1252. int pixsize = get_format_pixel_size(p_format);
  1253. int pixshift = get_format_pixel_rshift(p_format);
  1254. int block = get_format_block_size(p_format);
  1255. // Technically, you can still compress up to 1 px no matter the format, so commenting this.
  1256. //int minw, minh;
  1257. //get_format_min_pixel_size(p_format, minw, minh);
  1258. int minw = 1, minh = 1;
  1259. while (true) {
  1260. int bw = w % block != 0 ? w + (block - w % block) : w;
  1261. int bh = h % block != 0 ? h + (block - h % block) : h;
  1262. int s = bw * bh;
  1263. s *= pixsize;
  1264. s >>= pixshift;
  1265. size += s;
  1266. if (p_mipmaps >= 0) {
  1267. w = MAX(minw, w >> 1);
  1268. h = MAX(minh, h >> 1);
  1269. } else {
  1270. if (w == minw && h == minh) {
  1271. break;
  1272. }
  1273. w = MAX(minw, w >> 1);
  1274. h = MAX(minh, h >> 1);
  1275. }
  1276. // Set mipmap size.
  1277. // It might be necessary to put this after the minimum mipmap size check because of the possible occurrence of "1 >> 1".
  1278. if (r_mm_width) {
  1279. *r_mm_width = bw >> 1;
  1280. }
  1281. if (r_mm_height) {
  1282. *r_mm_height = bh >> 1;
  1283. }
  1284. // Reach target mipmap.
  1285. if (p_mipmaps >= 0 && mm == p_mipmaps) {
  1286. break;
  1287. }
  1288. mm++;
  1289. }
  1290. r_mipmaps = mm;
  1291. return size;
  1292. }
  1293. bool Image::_can_modify(Format p_format) const {
  1294. return p_format <= FORMAT_RGBE9995;
  1295. }
  1296. template <class Component, int CC, bool renormalize,
  1297. void (*average_func)(Component &, const Component &, const Component &, const Component &, const Component &),
  1298. void (*renormalize_func)(Component *)>
  1299. static void _generate_po2_mipmap(const Component *p_src, Component *p_dst, uint32_t p_width, uint32_t p_height) {
  1300. //fast power of 2 mipmap generation
  1301. uint32_t dst_w = MAX(p_width >> 1, 1);
  1302. uint32_t dst_h = MAX(p_height >> 1, 1);
  1303. int right_step = (p_width == 1) ? 0 : CC;
  1304. int down_step = (p_height == 1) ? 0 : (p_width * CC);
  1305. for (uint32_t i = 0; i < dst_h; i++) {
  1306. const Component *rup_ptr = &p_src[i * 2 * down_step];
  1307. const Component *rdown_ptr = rup_ptr + down_step;
  1308. Component *dst_ptr = &p_dst[i * dst_w * CC];
  1309. uint32_t count = dst_w;
  1310. while (count) {
  1311. count--;
  1312. for (int j = 0; j < CC; j++) {
  1313. average_func(dst_ptr[j], rup_ptr[j], rup_ptr[j + right_step], rdown_ptr[j], rdown_ptr[j + right_step]);
  1314. }
  1315. if (renormalize) {
  1316. renormalize_func(dst_ptr);
  1317. }
  1318. dst_ptr += CC;
  1319. rup_ptr += right_step * 2;
  1320. rdown_ptr += right_step * 2;
  1321. }
  1322. }
  1323. }
  1324. void Image::shrink_x2() {
  1325. ERR_FAIL_COND(data.size() == 0);
  1326. if (mipmaps) {
  1327. //just use the lower mipmap as base and copy all
  1328. Vector<uint8_t> new_img;
  1329. int ofs = get_mipmap_offset(1);
  1330. int new_size = data.size() - ofs;
  1331. new_img.resize(new_size);
  1332. ERR_FAIL_COND(new_img.size() == 0);
  1333. {
  1334. uint8_t *w = new_img.ptrw();
  1335. const uint8_t *r = data.ptr();
  1336. memcpy(w, &r[ofs], new_size);
  1337. }
  1338. width = MAX(width / 2, 1);
  1339. height = MAX(height / 2, 1);
  1340. data = new_img;
  1341. } else {
  1342. Vector<uint8_t> new_img;
  1343. ERR_FAIL_COND(!_can_modify(format));
  1344. int ps = get_format_pixel_size(format);
  1345. new_img.resize((width / 2) * (height / 2) * ps);
  1346. ERR_FAIL_COND(new_img.size() == 0);
  1347. ERR_FAIL_COND(data.size() == 0);
  1348. {
  1349. uint8_t *w = new_img.ptrw();
  1350. const uint8_t *r = data.ptr();
  1351. switch (format) {
  1352. case FORMAT_L8:
  1353. case FORMAT_R8:
  1354. _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height);
  1355. break;
  1356. case FORMAT_LA8:
  1357. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height);
  1358. break;
  1359. case FORMAT_RG8:
  1360. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height);
  1361. break;
  1362. case FORMAT_RGB8:
  1363. _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height);
  1364. break;
  1365. case FORMAT_RGBA8:
  1366. _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height);
  1367. break;
  1368. case FORMAT_RF:
  1369. _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height);
  1370. break;
  1371. case FORMAT_RGF:
  1372. _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height);
  1373. break;
  1374. case FORMAT_RGBF:
  1375. _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height);
  1376. break;
  1377. case FORMAT_RGBAF:
  1378. _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height);
  1379. break;
  1380. case FORMAT_RH:
  1381. _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);
  1382. break;
  1383. case FORMAT_RGH:
  1384. _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);
  1385. break;
  1386. case FORMAT_RGBH:
  1387. _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);
  1388. break;
  1389. case FORMAT_RGBAH:
  1390. _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);
  1391. break;
  1392. case FORMAT_RGBE9995:
  1393. _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);
  1394. break;
  1395. default: {
  1396. }
  1397. }
  1398. }
  1399. width /= 2;
  1400. height /= 2;
  1401. data = new_img;
  1402. }
  1403. }
  1404. void Image::normalize() {
  1405. bool used_mipmaps = has_mipmaps();
  1406. if (used_mipmaps) {
  1407. clear_mipmaps();
  1408. }
  1409. for (int y = 0; y < height; y++) {
  1410. for (int x = 0; x < width; x++) {
  1411. Color c = get_pixel(x, y);
  1412. Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0);
  1413. v.normalize();
  1414. c.r = v.x * 0.5 + 0.5;
  1415. c.g = v.y * 0.5 + 0.5;
  1416. c.b = v.z * 0.5 + 0.5;
  1417. set_pixel(x, y, c);
  1418. }
  1419. }
  1420. if (used_mipmaps) {
  1421. generate_mipmaps(true);
  1422. }
  1423. }
  1424. Error Image::generate_mipmaps(bool p_renormalize) {
  1425. ERR_FAIL_COND_V_MSG(!_can_modify(format), ERR_UNAVAILABLE, "Cannot generate mipmaps in compressed or custom image formats.");
  1426. ERR_FAIL_COND_V_MSG(format == FORMAT_RGBA4444, ERR_UNAVAILABLE, "Cannot generate mipmaps from RGBA4444 format.");
  1427. ERR_FAIL_COND_V_MSG(width == 0 || height == 0, ERR_UNCONFIGURED, "Cannot generate mipmaps with width or height equal to 0.");
  1428. int mmcount;
  1429. int size = _get_dst_image_size(width, height, format, mmcount);
  1430. data.resize(size);
  1431. uint8_t *wp = data.ptrw();
  1432. int prev_ofs = 0;
  1433. int prev_h = height;
  1434. int prev_w = width;
  1435. for (int i = 1; i <= mmcount; i++) {
  1436. int ofs, w, h;
  1437. _get_mipmap_offset_and_size(i, ofs, w, h);
  1438. switch (format) {
  1439. case FORMAT_L8:
  1440. case FORMAT_R8:
  1441. _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1442. break;
  1443. case FORMAT_LA8:
  1444. case FORMAT_RG8:
  1445. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1446. break;
  1447. case FORMAT_RGB8:
  1448. if (p_renormalize) {
  1449. _generate_po2_mipmap<uint8_t, 3, 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, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1452. }
  1453. break;
  1454. case FORMAT_RGBA8:
  1455. if (p_renormalize) {
  1456. _generate_po2_mipmap<uint8_t, 4, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1457. } else {
  1458. _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1459. }
  1460. break;
  1461. case FORMAT_RF:
  1462. _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);
  1463. break;
  1464. case FORMAT_RGF:
  1465. _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);
  1466. break;
  1467. case FORMAT_RGBF:
  1468. if (p_renormalize) {
  1469. _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);
  1470. } else {
  1471. _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);
  1472. }
  1473. break;
  1474. case FORMAT_RGBAF:
  1475. if (p_renormalize) {
  1476. _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);
  1477. } else {
  1478. _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);
  1479. }
  1480. break;
  1481. case FORMAT_RH:
  1482. _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);
  1483. break;
  1484. case FORMAT_RGH:
  1485. _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);
  1486. break;
  1487. case FORMAT_RGBH:
  1488. if (p_renormalize) {
  1489. _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);
  1490. } else {
  1491. _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);
  1492. }
  1493. break;
  1494. case FORMAT_RGBAH:
  1495. if (p_renormalize) {
  1496. _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);
  1497. } else {
  1498. _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);
  1499. }
  1500. break;
  1501. case FORMAT_RGBE9995:
  1502. if (p_renormalize) {
  1503. _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);
  1504. } else {
  1505. _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);
  1506. }
  1507. break;
  1508. default: {
  1509. }
  1510. }
  1511. prev_ofs = ofs;
  1512. prev_w = w;
  1513. prev_h = h;
  1514. }
  1515. mipmaps = true;
  1516. return OK;
  1517. }
  1518. Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map) {
  1519. Vector<double> normal_sat_vec; //summed area table
  1520. double *normal_sat = nullptr; //summed area table for normal map
  1521. int normal_w = 0, normal_h = 0;
  1522. 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");
  1523. Ref<Image> nm = p_normal_map->duplicate();
  1524. if (nm->is_compressed()) {
  1525. nm->decompress();
  1526. }
  1527. normal_w = nm->get_width();
  1528. normal_h = nm->get_height();
  1529. normal_sat_vec.resize(normal_w * normal_h * 3);
  1530. normal_sat = normal_sat_vec.ptrw();
  1531. //create summed area table
  1532. for (int y = 0; y < normal_h; y++) {
  1533. double line_sum[3] = { 0, 0, 0 };
  1534. for (int x = 0; x < normal_w; x++) {
  1535. double normal[3];
  1536. Color color = nm->get_pixel(x, y);
  1537. normal[0] = color.r * 2.0 - 1.0;
  1538. normal[1] = color.g * 2.0 - 1.0;
  1539. normal[2] = Math::sqrt(MAX(0.0, 1.0 - (normal[0] * normal[0] + normal[1] * normal[1]))); //reconstruct if missing
  1540. line_sum[0] += normal[0];
  1541. line_sum[1] += normal[1];
  1542. line_sum[2] += normal[2];
  1543. uint32_t ofs = (y * normal_w + x) * 3;
  1544. normal_sat[ofs + 0] = line_sum[0];
  1545. normal_sat[ofs + 1] = line_sum[1];
  1546. normal_sat[ofs + 2] = line_sum[2];
  1547. if (y > 0) {
  1548. uint32_t prev_ofs = ((y - 1) * normal_w + x) * 3;
  1549. normal_sat[ofs + 0] += normal_sat[prev_ofs + 0];
  1550. normal_sat[ofs + 1] += normal_sat[prev_ofs + 1];
  1551. normal_sat[ofs + 2] += normal_sat[prev_ofs + 2];
  1552. }
  1553. }
  1554. }
  1555. #if 0
  1556. {
  1557. Vector3 beg(normal_sat_vec[0], normal_sat_vec[1], normal_sat_vec[2]);
  1558. 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]);
  1559. Vector3 avg = (end - beg) / (normal_w * normal_h);
  1560. print_line("average: " + avg);
  1561. }
  1562. #endif
  1563. int mmcount;
  1564. _get_dst_image_size(width, height, format, mmcount);
  1565. uint8_t *base_ptr = data.ptrw();
  1566. for (int i = 1; i <= mmcount; i++) {
  1567. int ofs, w, h;
  1568. _get_mipmap_offset_and_size(i, ofs, w, h);
  1569. uint8_t *ptr = &base_ptr[ofs];
  1570. for (int x = 0; x < w; x++) {
  1571. for (int y = 0; y < h; y++) {
  1572. int from_x = x * normal_w / w;
  1573. int from_y = y * normal_h / h;
  1574. int to_x = (x + 1) * normal_w / w;
  1575. int to_y = (y + 1) * normal_h / h;
  1576. to_x = MIN(to_x - 1, normal_w);
  1577. to_y = MIN(to_y - 1, normal_h);
  1578. int size_x = (to_x - from_x) + 1;
  1579. int size_y = (to_y - from_y) + 1;
  1580. //summed area table version (much faster)
  1581. double avg[3] = { 0, 0, 0 };
  1582. if (from_x > 0 && from_y > 0) {
  1583. uint32_t tofs = ((from_y - 1) * normal_w + (from_x - 1)) * 3;
  1584. avg[0] += normal_sat[tofs + 0];
  1585. avg[1] += normal_sat[tofs + 1];
  1586. avg[2] += normal_sat[tofs + 2];
  1587. }
  1588. if (from_y > 0) {
  1589. uint32_t tofs = ((from_y - 1) * normal_w + to_x) * 3;
  1590. avg[0] -= normal_sat[tofs + 0];
  1591. avg[1] -= normal_sat[tofs + 1];
  1592. avg[2] -= normal_sat[tofs + 2];
  1593. }
  1594. if (from_x > 0) {
  1595. uint32_t tofs = (to_y * normal_w + (from_x - 1)) * 3;
  1596. avg[0] -= normal_sat[tofs + 0];
  1597. avg[1] -= normal_sat[tofs + 1];
  1598. avg[2] -= normal_sat[tofs + 2];
  1599. }
  1600. uint32_t tofs = (to_y * normal_w + to_x) * 3;
  1601. avg[0] += normal_sat[tofs + 0];
  1602. avg[1] += normal_sat[tofs + 1];
  1603. avg[2] += normal_sat[tofs + 2];
  1604. double div = double(size_x * size_y);
  1605. Vector3 vec(avg[0] / div, avg[1] / div, avg[2] / div);
  1606. float r = vec.length();
  1607. int pixel_ofs = y * w + x;
  1608. Color c = _get_color_at_ofs(ptr, pixel_ofs);
  1609. float roughness = 0;
  1610. switch (p_roughness_channel) {
  1611. case ROUGHNESS_CHANNEL_R: {
  1612. roughness = c.r;
  1613. } break;
  1614. case ROUGHNESS_CHANNEL_G: {
  1615. roughness = c.g;
  1616. } break;
  1617. case ROUGHNESS_CHANNEL_B: {
  1618. roughness = c.b;
  1619. } break;
  1620. case ROUGHNESS_CHANNEL_L: {
  1621. roughness = c.get_v();
  1622. } break;
  1623. case ROUGHNESS_CHANNEL_A: {
  1624. roughness = c.a;
  1625. } break;
  1626. }
  1627. float variance = 0;
  1628. if (r < 1.0f) {
  1629. float r2 = r * r;
  1630. float kappa = (3.0f * r - r * r2) / (1.0f - r2);
  1631. variance = 0.25f / kappa;
  1632. }
  1633. float threshold = 0.4;
  1634. roughness = Math::sqrt(roughness * roughness + MIN(3.0f * variance, threshold * threshold));
  1635. switch (p_roughness_channel) {
  1636. case ROUGHNESS_CHANNEL_R: {
  1637. c.r = roughness;
  1638. } break;
  1639. case ROUGHNESS_CHANNEL_G: {
  1640. c.g = roughness;
  1641. } break;
  1642. case ROUGHNESS_CHANNEL_B: {
  1643. c.b = roughness;
  1644. } break;
  1645. case ROUGHNESS_CHANNEL_L: {
  1646. c.r = roughness;
  1647. c.g = roughness;
  1648. c.b = roughness;
  1649. } break;
  1650. case ROUGHNESS_CHANNEL_A: {
  1651. c.a = roughness;
  1652. } break;
  1653. }
  1654. _set_color_at_ofs(ptr, pixel_ofs, c);
  1655. }
  1656. }
  1657. #if 0
  1658. {
  1659. int size = get_mipmap_byte_size(i);
  1660. print_line("size for mimpap " + itos(i) + ": " + itos(size));
  1661. Vector<uint8_t> imgdata;
  1662. imgdata.resize(size);
  1663. uint8_t* wr = imgdata.ptrw();
  1664. memcpy(wr.ptr(), ptr, size);
  1665. wr = uint8_t*();
  1666. Ref<Image> im;
  1667. im.instantiate();
  1668. im->create(w, h, false, format, imgdata);
  1669. im->save_png("res://mipmap_" + itos(i) + ".png");
  1670. }
  1671. #endif
  1672. }
  1673. return OK;
  1674. }
  1675. void Image::clear_mipmaps() {
  1676. if (!mipmaps) {
  1677. return;
  1678. }
  1679. if (is_empty()) {
  1680. return;
  1681. }
  1682. int ofs, w, h;
  1683. _get_mipmap_offset_and_size(1, ofs, w, h);
  1684. data.resize(ofs);
  1685. mipmaps = false;
  1686. }
  1687. bool Image::is_empty() const {
  1688. return (data.size() == 0);
  1689. }
  1690. Vector<uint8_t> Image::get_data() const {
  1691. return data;
  1692. }
  1693. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1694. ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0.");
  1695. ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0.");
  1696. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + ".");
  1697. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + ".");
  1698. ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS));
  1699. ERR_FAIL_INDEX_MSG(p_format, FORMAT_MAX, "Image format out of range, please see Image's Format enum.");
  1700. int mm = 0;
  1701. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1702. data.resize(size);
  1703. {
  1704. uint8_t *w = data.ptrw();
  1705. memset(w, 0, size);
  1706. }
  1707. width = p_width;
  1708. height = p_height;
  1709. mipmaps = p_use_mipmaps;
  1710. format = p_format;
  1711. }
  1712. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  1713. ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0.");
  1714. ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0.");
  1715. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + ".");
  1716. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + ".");
  1717. ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS));
  1718. ERR_FAIL_INDEX_MSG(p_format, FORMAT_MAX, "Image format out of range, please see Image's Format enum.");
  1719. int mm;
  1720. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1721. 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.");
  1722. height = p_height;
  1723. width = p_width;
  1724. format = p_format;
  1725. data = p_data;
  1726. mipmaps = p_use_mipmaps;
  1727. }
  1728. void Image::create(const char **p_xpm) {
  1729. int size_width = 0;
  1730. int size_height = 0;
  1731. int pixelchars = 0;
  1732. mipmaps = false;
  1733. bool has_alpha = false;
  1734. enum Status {
  1735. READING_HEADER,
  1736. READING_COLORS,
  1737. READING_PIXELS,
  1738. DONE
  1739. };
  1740. Status status = READING_HEADER;
  1741. int line = 0;
  1742. HashMap<String, Color> colormap;
  1743. int colormap_size = 0;
  1744. uint32_t pixel_size = 0;
  1745. uint8_t *data_write = nullptr;
  1746. while (status != DONE) {
  1747. const char *line_ptr = p_xpm[line];
  1748. switch (status) {
  1749. case READING_HEADER: {
  1750. String line_str = line_ptr;
  1751. line_str.replace("\t", " ");
  1752. size_width = line_str.get_slicec(' ', 0).to_int();
  1753. size_height = line_str.get_slicec(' ', 1).to_int();
  1754. colormap_size = line_str.get_slicec(' ', 2).to_int();
  1755. pixelchars = line_str.get_slicec(' ', 3).to_int();
  1756. ERR_FAIL_COND(colormap_size > 32766);
  1757. ERR_FAIL_COND(pixelchars > 5);
  1758. ERR_FAIL_COND(size_width > 32767);
  1759. ERR_FAIL_COND(size_height > 32767);
  1760. status = READING_COLORS;
  1761. } break;
  1762. case READING_COLORS: {
  1763. String colorstring;
  1764. for (int i = 0; i < pixelchars; i++) {
  1765. colorstring += *line_ptr;
  1766. line_ptr++;
  1767. }
  1768. //skip spaces
  1769. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1770. if (*line_ptr == 0) {
  1771. break;
  1772. }
  1773. line_ptr++;
  1774. }
  1775. if (*line_ptr == 'c') {
  1776. line_ptr++;
  1777. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1778. if (*line_ptr == 0) {
  1779. break;
  1780. }
  1781. line_ptr++;
  1782. }
  1783. if (*line_ptr == '#') {
  1784. line_ptr++;
  1785. uint8_t col_r = 0;
  1786. uint8_t col_g = 0;
  1787. uint8_t col_b = 0;
  1788. //uint8_t col_a=255;
  1789. for (int i = 0; i < 6; i++) {
  1790. char v = line_ptr[i];
  1791. if (v >= '0' && v <= '9') {
  1792. v -= '0';
  1793. } else if (v >= 'A' && v <= 'F') {
  1794. v = (v - 'A') + 10;
  1795. } else if (v >= 'a' && v <= 'f') {
  1796. v = (v - 'a') + 10;
  1797. } else {
  1798. break;
  1799. }
  1800. switch (i) {
  1801. case 0:
  1802. col_r = v << 4;
  1803. break;
  1804. case 1:
  1805. col_r |= v;
  1806. break;
  1807. case 2:
  1808. col_g = v << 4;
  1809. break;
  1810. case 3:
  1811. col_g |= v;
  1812. break;
  1813. case 4:
  1814. col_b = v << 4;
  1815. break;
  1816. case 5:
  1817. col_b |= v;
  1818. break;
  1819. }
  1820. }
  1821. // magenta mask
  1822. if (col_r == 255 && col_g == 0 && col_b == 255) {
  1823. colormap[colorstring] = Color(0, 0, 0, 0);
  1824. has_alpha = true;
  1825. } else {
  1826. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  1827. }
  1828. }
  1829. }
  1830. if (line == colormap_size) {
  1831. status = READING_PIXELS;
  1832. create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8);
  1833. data_write = data.ptrw();
  1834. pixel_size = has_alpha ? 4 : 3;
  1835. }
  1836. } break;
  1837. case READING_PIXELS: {
  1838. int y = line - colormap_size - 1;
  1839. for (int x = 0; x < size_width; x++) {
  1840. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  1841. for (int i = 0; i < pixelchars; i++) {
  1842. pixelstr[i] = line_ptr[x * pixelchars + i];
  1843. }
  1844. Color *colorptr = colormap.getptr(pixelstr);
  1845. ERR_FAIL_COND(!colorptr);
  1846. uint8_t pixel[4];
  1847. for (uint32_t i = 0; i < pixel_size; i++) {
  1848. pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255);
  1849. }
  1850. _put_pixelb(x, y, pixel_size, data_write, pixel);
  1851. }
  1852. if (y == (size_height - 1)) {
  1853. status = DONE;
  1854. }
  1855. } break;
  1856. default: {
  1857. }
  1858. }
  1859. line++;
  1860. }
  1861. }
  1862. #define DETECT_ALPHA_MAX_THRESHOLD 254
  1863. #define DETECT_ALPHA_MIN_THRESHOLD 2
  1864. #define DETECT_ALPHA(m_value) \
  1865. { \
  1866. uint8_t value = m_value; \
  1867. if (value < DETECT_ALPHA_MIN_THRESHOLD) \
  1868. bit = true; \
  1869. else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \
  1870. detected = true; \
  1871. break; \
  1872. } \
  1873. }
  1874. #define DETECT_NON_ALPHA(m_value) \
  1875. { \
  1876. uint8_t value = m_value; \
  1877. if (value > 0) { \
  1878. detected = true; \
  1879. break; \
  1880. } \
  1881. }
  1882. bool Image::is_invisible() const {
  1883. if (format == FORMAT_L8 ||
  1884. format == FORMAT_RGB8 || format == FORMAT_RG8) {
  1885. return false;
  1886. }
  1887. int len = data.size();
  1888. if (len == 0) {
  1889. return true;
  1890. }
  1891. int w, h;
  1892. _get_mipmap_offset_and_size(1, len, w, h);
  1893. const uint8_t *r = data.ptr();
  1894. const unsigned char *data_ptr = r;
  1895. bool detected = false;
  1896. switch (format) {
  1897. case FORMAT_LA8: {
  1898. for (int i = 0; i < (len >> 1); i++) {
  1899. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  1900. }
  1901. } break;
  1902. case FORMAT_RGBA8: {
  1903. for (int i = 0; i < (len >> 2); i++) {
  1904. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  1905. }
  1906. } break;
  1907. case FORMAT_PVRTC1_2A:
  1908. case FORMAT_PVRTC1_4A:
  1909. case FORMAT_DXT3:
  1910. case FORMAT_DXT5: {
  1911. detected = true;
  1912. } break;
  1913. default: {
  1914. }
  1915. }
  1916. return !detected;
  1917. }
  1918. Image::AlphaMode Image::detect_alpha() const {
  1919. int len = data.size();
  1920. if (len == 0) {
  1921. return ALPHA_NONE;
  1922. }
  1923. int w, h;
  1924. _get_mipmap_offset_and_size(1, len, w, h);
  1925. const uint8_t *r = data.ptr();
  1926. const unsigned char *data_ptr = r;
  1927. bool bit = false;
  1928. bool detected = false;
  1929. switch (format) {
  1930. case FORMAT_LA8: {
  1931. for (int i = 0; i < (len >> 1); i++) {
  1932. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  1933. }
  1934. } break;
  1935. case FORMAT_RGBA8: {
  1936. for (int i = 0; i < (len >> 2); i++) {
  1937. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  1938. }
  1939. } break;
  1940. case FORMAT_PVRTC1_2A:
  1941. case FORMAT_PVRTC1_4A:
  1942. case FORMAT_DXT3:
  1943. case FORMAT_DXT5: {
  1944. detected = true;
  1945. } break;
  1946. default: {
  1947. }
  1948. }
  1949. if (detected) {
  1950. return ALPHA_BLEND;
  1951. } else if (bit) {
  1952. return ALPHA_BIT;
  1953. } else {
  1954. return ALPHA_NONE;
  1955. }
  1956. }
  1957. Error Image::load(const String &p_path) {
  1958. #ifdef DEBUG_ENABLED
  1959. if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
  1960. 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.");
  1961. }
  1962. #endif
  1963. return ImageLoader::load_image(p_path, this);
  1964. }
  1965. Error Image::save_png(const String &p_path) const {
  1966. if (save_png_func == nullptr) {
  1967. return ERR_UNAVAILABLE;
  1968. }
  1969. return save_png_func(p_path, Ref<Image>((Image *)this));
  1970. }
  1971. Vector<uint8_t> Image::save_png_to_buffer() const {
  1972. if (save_png_buffer_func == nullptr) {
  1973. return Vector<uint8_t>();
  1974. }
  1975. return save_png_buffer_func(Ref<Image>((Image *)this));
  1976. }
  1977. Error Image::save_exr(const String &p_path, bool p_grayscale) const {
  1978. if (save_exr_func == nullptr) {
  1979. return ERR_UNAVAILABLE;
  1980. }
  1981. return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale);
  1982. }
  1983. int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) {
  1984. int mm;
  1985. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0);
  1986. }
  1987. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1988. int mm;
  1989. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  1990. return mm;
  1991. }
  1992. Size2i Image::get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap) {
  1993. int mm;
  1994. Size2i ret;
  1995. _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap, &ret.x, &ret.y);
  1996. return ret;
  1997. }
  1998. int Image::get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap) {
  1999. if (p_mipmap <= 0) {
  2000. return 0;
  2001. }
  2002. int mm;
  2003. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1);
  2004. }
  2005. 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) {
  2006. if (p_mipmap <= 0) {
  2007. r_w = p_width;
  2008. r_h = p_height;
  2009. return 0;
  2010. }
  2011. int mm;
  2012. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1, &r_w, &r_h);
  2013. }
  2014. bool Image::is_compressed() const {
  2015. return format > FORMAT_RGBE9995;
  2016. }
  2017. Error Image::decompress() {
  2018. if (((format >= FORMAT_DXT1 && format <= FORMAT_RGTC_RG) || (format == FORMAT_DXT5_RA_AS_RG)) && _image_decompress_bc) {
  2019. _image_decompress_bc(this);
  2020. } else if (format >= FORMAT_BPTC_RGBA && format <= FORMAT_BPTC_RGBFU && _image_decompress_bptc) {
  2021. _image_decompress_bptc(this);
  2022. } else if (format >= FORMAT_PVRTC1_2 && format <= FORMAT_PVRTC1_4A && _image_decompress_pvrtc) {
  2023. _image_decompress_pvrtc(this);
  2024. } else if (format == FORMAT_ETC && _image_decompress_etc1) {
  2025. _image_decompress_etc1(this);
  2026. } else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RA_AS_RG && _image_decompress_etc2) {
  2027. _image_decompress_etc2(this);
  2028. } else {
  2029. return ERR_UNAVAILABLE;
  2030. }
  2031. return OK;
  2032. }
  2033. Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
  2034. ERR_FAIL_INDEX_V_MSG(p_mode, COMPRESS_MAX, ERR_INVALID_PARAMETER, "Invalid compress mode.");
  2035. ERR_FAIL_INDEX_V_MSG(p_source, COMPRESS_SOURCE_MAX, ERR_INVALID_PARAMETER, "Invalid compress source.");
  2036. return compress_from_channels(p_mode, detect_used_channels(p_source), p_lossy_quality);
  2037. }
  2038. Error Image::compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality) {
  2039. switch (p_mode) {
  2040. case COMPRESS_S3TC: {
  2041. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  2042. _image_compress_bc_func(this, p_lossy_quality, p_channels);
  2043. } break;
  2044. case COMPRESS_PVRTC1_4: {
  2045. ERR_FAIL_COND_V(!_image_compress_pvrtc1_4bpp_func, ERR_UNAVAILABLE);
  2046. _image_compress_pvrtc1_4bpp_func(this);
  2047. } break;
  2048. case COMPRESS_ETC: {
  2049. ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE);
  2050. _image_compress_etc1_func(this, p_lossy_quality);
  2051. } break;
  2052. case COMPRESS_ETC2: {
  2053. ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE);
  2054. _image_compress_etc2_func(this, p_lossy_quality, p_channels);
  2055. } break;
  2056. case COMPRESS_BPTC: {
  2057. ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE);
  2058. _image_compress_bptc_func(this, p_lossy_quality, p_channels);
  2059. } break;
  2060. case COMPRESS_MAX: {
  2061. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  2062. } break;
  2063. }
  2064. return OK;
  2065. }
  2066. Image::Image(const char **p_xpm) {
  2067. width = 0;
  2068. height = 0;
  2069. mipmaps = false;
  2070. format = FORMAT_L8;
  2071. create(p_xpm);
  2072. }
  2073. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  2074. width = 0;
  2075. height = 0;
  2076. mipmaps = p_use_mipmaps;
  2077. format = FORMAT_L8;
  2078. create(p_width, p_height, p_use_mipmaps, p_format);
  2079. }
  2080. Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  2081. width = 0;
  2082. height = 0;
  2083. mipmaps = p_mipmaps;
  2084. format = FORMAT_L8;
  2085. create(p_width, p_height, p_mipmaps, p_format, p_data);
  2086. }
  2087. Rect2 Image::get_used_rect() const {
  2088. if (format != FORMAT_LA8 && format != FORMAT_RGBA8 && format != FORMAT_RGBAF && format != FORMAT_RGBAH && format != FORMAT_RGBA4444 && format != FORMAT_RGB565) {
  2089. return Rect2(Point2(), Size2(width, height));
  2090. }
  2091. int len = data.size();
  2092. if (len == 0) {
  2093. return Rect2();
  2094. }
  2095. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  2096. int maxx = -1, maxy = -1;
  2097. for (int j = 0; j < height; j++) {
  2098. for (int i = 0; i < width; i++) {
  2099. if (!(get_pixel(i, j).a > 0)) {
  2100. continue;
  2101. }
  2102. if (i > maxx) {
  2103. maxx = i;
  2104. }
  2105. if (j > maxy) {
  2106. maxy = j;
  2107. }
  2108. if (i < minx) {
  2109. minx = i;
  2110. }
  2111. if (j < miny) {
  2112. miny = j;
  2113. }
  2114. }
  2115. }
  2116. if (maxx == -1) {
  2117. return Rect2();
  2118. } else {
  2119. return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1);
  2120. }
  2121. }
  2122. Ref<Image> Image::get_rect(const Rect2 &p_area) const {
  2123. Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format));
  2124. img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0));
  2125. return img;
  2126. }
  2127. void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  2128. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  2129. int dsize = data.size();
  2130. int srcdsize = p_src->data.size();
  2131. ERR_FAIL_COND(dsize == 0);
  2132. ERR_FAIL_COND(srcdsize == 0);
  2133. ERR_FAIL_COND(format != p_src->format);
  2134. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot blit_rect in compressed or custom image formats.");
  2135. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect);
  2136. if (p_dest.x < 0) {
  2137. clipped_src_rect.position.x = ABS(p_dest.x);
  2138. }
  2139. if (p_dest.y < 0) {
  2140. clipped_src_rect.position.y = ABS(p_dest.y);
  2141. }
  2142. if (clipped_src_rect.has_no_area()) {
  2143. return;
  2144. }
  2145. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  2146. Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  2147. uint8_t *wp = data.ptrw();
  2148. uint8_t *dst_data_ptr = wp;
  2149. const uint8_t *rp = p_src->data.ptr();
  2150. const uint8_t *src_data_ptr = rp;
  2151. int pixel_size = get_format_pixel_size(format);
  2152. for (int i = 0; i < dest_rect.size.y; i++) {
  2153. for (int j = 0; j < dest_rect.size.x; j++) {
  2154. int src_x = clipped_src_rect.position.x + j;
  2155. int src_y = clipped_src_rect.position.y + i;
  2156. int dst_x = dest_rect.position.x + j;
  2157. int dst_y = dest_rect.position.y + i;
  2158. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  2159. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  2160. for (int k = 0; k < pixel_size; k++) {
  2161. dst[k] = src[k];
  2162. }
  2163. }
  2164. }
  2165. }
  2166. void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  2167. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  2168. ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object.");
  2169. int dsize = data.size();
  2170. int srcdsize = p_src->data.size();
  2171. int maskdsize = p_mask->data.size();
  2172. ERR_FAIL_COND(dsize == 0);
  2173. ERR_FAIL_COND(srcdsize == 0);
  2174. ERR_FAIL_COND(maskdsize == 0);
  2175. ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width.");
  2176. ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height.");
  2177. ERR_FAIL_COND(format != p_src->format);
  2178. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect);
  2179. if (p_dest.x < 0) {
  2180. clipped_src_rect.position.x = ABS(p_dest.x);
  2181. }
  2182. if (p_dest.y < 0) {
  2183. clipped_src_rect.position.y = ABS(p_dest.y);
  2184. }
  2185. if (clipped_src_rect.has_no_area()) {
  2186. return;
  2187. }
  2188. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  2189. Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  2190. uint8_t *wp = data.ptrw();
  2191. uint8_t *dst_data_ptr = wp;
  2192. const uint8_t *rp = p_src->data.ptr();
  2193. const uint8_t *src_data_ptr = rp;
  2194. int pixel_size = get_format_pixel_size(format);
  2195. Ref<Image> msk = p_mask;
  2196. for (int i = 0; i < dest_rect.size.y; i++) {
  2197. for (int j = 0; j < dest_rect.size.x; j++) {
  2198. int src_x = clipped_src_rect.position.x + j;
  2199. int src_y = clipped_src_rect.position.y + i;
  2200. if (msk->get_pixel(src_x, src_y).a != 0) {
  2201. int dst_x = dest_rect.position.x + j;
  2202. int dst_y = dest_rect.position.y + i;
  2203. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  2204. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  2205. for (int k = 0; k < pixel_size; k++) {
  2206. dst[k] = src[k];
  2207. }
  2208. }
  2209. }
  2210. }
  2211. }
  2212. void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  2213. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  2214. int dsize = data.size();
  2215. int srcdsize = p_src->data.size();
  2216. ERR_FAIL_COND(dsize == 0);
  2217. ERR_FAIL_COND(srcdsize == 0);
  2218. ERR_FAIL_COND(format != p_src->format);
  2219. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect);
  2220. if (p_dest.x < 0) {
  2221. clipped_src_rect.position.x = ABS(p_dest.x);
  2222. }
  2223. if (p_dest.y < 0) {
  2224. clipped_src_rect.position.y = ABS(p_dest.y);
  2225. }
  2226. if (clipped_src_rect.has_no_area()) {
  2227. return;
  2228. }
  2229. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  2230. Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  2231. Ref<Image> img = p_src;
  2232. for (int i = 0; i < dest_rect.size.y; i++) {
  2233. for (int j = 0; j < dest_rect.size.x; j++) {
  2234. int src_x = clipped_src_rect.position.x + j;
  2235. int src_y = clipped_src_rect.position.y + i;
  2236. int dst_x = dest_rect.position.x + j;
  2237. int dst_y = dest_rect.position.y + i;
  2238. Color sc = img->get_pixel(src_x, src_y);
  2239. if (sc.a != 0) {
  2240. Color dc = get_pixel(dst_x, dst_y);
  2241. dc = dc.blend(sc);
  2242. set_pixel(dst_x, dst_y, dc);
  2243. }
  2244. }
  2245. }
  2246. }
  2247. void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  2248. ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object.");
  2249. ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object.");
  2250. int dsize = data.size();
  2251. int srcdsize = p_src->data.size();
  2252. int maskdsize = p_mask->data.size();
  2253. ERR_FAIL_COND(dsize == 0);
  2254. ERR_FAIL_COND(srcdsize == 0);
  2255. ERR_FAIL_COND(maskdsize == 0);
  2256. ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width.");
  2257. ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height.");
  2258. ERR_FAIL_COND(format != p_src->format);
  2259. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect);
  2260. if (p_dest.x < 0) {
  2261. clipped_src_rect.position.x = ABS(p_dest.x);
  2262. }
  2263. if (p_dest.y < 0) {
  2264. clipped_src_rect.position.y = ABS(p_dest.y);
  2265. }
  2266. if (clipped_src_rect.has_no_area()) {
  2267. return;
  2268. }
  2269. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  2270. Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  2271. Ref<Image> img = p_src;
  2272. Ref<Image> msk = p_mask;
  2273. for (int i = 0; i < dest_rect.size.y; i++) {
  2274. for (int j = 0; j < dest_rect.size.x; j++) {
  2275. int src_x = clipped_src_rect.position.x + j;
  2276. int src_y = clipped_src_rect.position.y + i;
  2277. // If the mask's pixel is transparent then we skip it
  2278. //Color c = msk->get_pixel(src_x, src_y);
  2279. //if (c.a == 0) continue;
  2280. if (msk->get_pixel(src_x, src_y).a != 0) {
  2281. int dst_x = dest_rect.position.x + j;
  2282. int dst_y = dest_rect.position.y + i;
  2283. Color sc = img->get_pixel(src_x, src_y);
  2284. if (sc.a != 0) {
  2285. Color dc = get_pixel(dst_x, dst_y);
  2286. dc = dc.blend(sc);
  2287. set_pixel(dst_x, dst_y, dc);
  2288. }
  2289. }
  2290. }
  2291. }
  2292. }
  2293. void Image::fill(const Color &c) {
  2294. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats.");
  2295. uint8_t *wp = data.ptrw();
  2296. uint8_t *dst_data_ptr = wp;
  2297. int pixel_size = get_format_pixel_size(format);
  2298. // put first pixel with the format-aware API
  2299. set_pixel(0, 0, c);
  2300. for (int y = 0; y < height; y++) {
  2301. for (int x = 0; x < width; x++) {
  2302. uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size];
  2303. for (int k = 0; k < pixel_size; k++) {
  2304. dst[k] = dst_data_ptr[k];
  2305. }
  2306. }
  2307. }
  2308. }
  2309. ImageMemLoadFunc Image::_png_mem_loader_func = nullptr;
  2310. ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr;
  2311. ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr;
  2312. ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
  2313. ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
  2314. void (*Image::_image_compress_bc_func)(Image *, float, Image::UsedChannels) = nullptr;
  2315. void (*Image::_image_compress_bptc_func)(Image *, float, Image::UsedChannels) = nullptr;
  2316. void (*Image::_image_compress_pvrtc1_4bpp_func)(Image *) = nullptr;
  2317. void (*Image::_image_compress_etc1_func)(Image *, float) = nullptr;
  2318. void (*Image::_image_compress_etc2_func)(Image *, float, Image::UsedChannels) = nullptr;
  2319. void (*Image::_image_decompress_pvrtc)(Image *) = nullptr;
  2320. void (*Image::_image_decompress_bc)(Image *) = nullptr;
  2321. void (*Image::_image_decompress_bptc)(Image *) = nullptr;
  2322. void (*Image::_image_decompress_etc1)(Image *) = nullptr;
  2323. void (*Image::_image_decompress_etc2)(Image *) = nullptr;
  2324. Vector<uint8_t> (*Image::webp_lossy_packer)(const Ref<Image> &, float) = nullptr;
  2325. Vector<uint8_t> (*Image::webp_lossless_packer)(const Ref<Image> &) = nullptr;
  2326. Ref<Image> (*Image::webp_unpacker)(const Vector<uint8_t> &) = nullptr;
  2327. Vector<uint8_t> (*Image::png_packer)(const Ref<Image> &) = nullptr;
  2328. Ref<Image> (*Image::png_unpacker)(const Vector<uint8_t> &) = nullptr;
  2329. Vector<uint8_t> (*Image::basis_universal_packer)(const Ref<Image> &, Image::UsedChannels) = nullptr;
  2330. Ref<Image> (*Image::basis_universal_unpacker)(const Vector<uint8_t> &) = nullptr;
  2331. void Image::_set_data(const Dictionary &p_data) {
  2332. ERR_FAIL_COND(!p_data.has("width"));
  2333. ERR_FAIL_COND(!p_data.has("height"));
  2334. ERR_FAIL_COND(!p_data.has("format"));
  2335. ERR_FAIL_COND(!p_data.has("mipmaps"));
  2336. ERR_FAIL_COND(!p_data.has("data"));
  2337. int dwidth = p_data["width"];
  2338. int dheight = p_data["height"];
  2339. String dformat = p_data["format"];
  2340. bool dmipmaps = p_data["mipmaps"];
  2341. Vector<uint8_t> ddata = p_data["data"];
  2342. Format ddformat = FORMAT_MAX;
  2343. for (int i = 0; i < FORMAT_MAX; i++) {
  2344. if (dformat == get_format_name(Format(i))) {
  2345. ddformat = Format(i);
  2346. break;
  2347. }
  2348. }
  2349. ERR_FAIL_COND(ddformat == FORMAT_MAX);
  2350. create(dwidth, dheight, dmipmaps, ddformat, ddata);
  2351. }
  2352. Dictionary Image::_get_data() const {
  2353. Dictionary d;
  2354. d["width"] = width;
  2355. d["height"] = height;
  2356. d["format"] = get_format_name(format);
  2357. d["mipmaps"] = mipmaps;
  2358. d["data"] = data;
  2359. return d;
  2360. }
  2361. Color Image::get_pixelv(const Point2i &p_point) const {
  2362. return get_pixel(p_point.x, p_point.y);
  2363. }
  2364. Color Image::_get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const {
  2365. switch (format) {
  2366. case FORMAT_L8: {
  2367. float l = ptr[ofs] / 255.0;
  2368. return Color(l, l, l, 1);
  2369. }
  2370. case FORMAT_LA8: {
  2371. float l = ptr[ofs * 2 + 0] / 255.0;
  2372. float a = ptr[ofs * 2 + 1] / 255.0;
  2373. return Color(l, l, l, a);
  2374. }
  2375. case FORMAT_R8: {
  2376. float r = ptr[ofs] / 255.0;
  2377. return Color(r, 0, 0, 1);
  2378. }
  2379. case FORMAT_RG8: {
  2380. float r = ptr[ofs * 2 + 0] / 255.0;
  2381. float g = ptr[ofs * 2 + 1] / 255.0;
  2382. return Color(r, g, 0, 1);
  2383. }
  2384. case FORMAT_RGB8: {
  2385. float r = ptr[ofs * 3 + 0] / 255.0;
  2386. float g = ptr[ofs * 3 + 1] / 255.0;
  2387. float b = ptr[ofs * 3 + 2] / 255.0;
  2388. return Color(r, g, b, 1);
  2389. }
  2390. case FORMAT_RGBA8: {
  2391. float r = ptr[ofs * 4 + 0] / 255.0;
  2392. float g = ptr[ofs * 4 + 1] / 255.0;
  2393. float b = ptr[ofs * 4 + 2] / 255.0;
  2394. float a = ptr[ofs * 4 + 3] / 255.0;
  2395. return Color(r, g, b, a);
  2396. }
  2397. case FORMAT_RGBA4444: {
  2398. uint16_t u = ((uint16_t *)ptr)[ofs];
  2399. float r = ((u >> 12) & 0xF) / 15.0;
  2400. float g = ((u >> 8) & 0xF) / 15.0;
  2401. float b = ((u >> 4) & 0xF) / 15.0;
  2402. float a = (u & 0xF) / 15.0;
  2403. return Color(r, g, b, a);
  2404. }
  2405. case FORMAT_RGB565: {
  2406. uint16_t u = ((uint16_t *)ptr)[ofs];
  2407. float r = (u & 0x1F) / 31.0;
  2408. float g = ((u >> 5) & 0x3F) / 63.0;
  2409. float b = ((u >> 11) & 0x1F) / 31.0;
  2410. return Color(r, g, b, 1.0);
  2411. }
  2412. case FORMAT_RF: {
  2413. float r = ((float *)ptr)[ofs];
  2414. return Color(r, 0, 0, 1);
  2415. }
  2416. case FORMAT_RGF: {
  2417. float r = ((float *)ptr)[ofs * 2 + 0];
  2418. float g = ((float *)ptr)[ofs * 2 + 1];
  2419. return Color(r, g, 0, 1);
  2420. }
  2421. case FORMAT_RGBF: {
  2422. float r = ((float *)ptr)[ofs * 3 + 0];
  2423. float g = ((float *)ptr)[ofs * 3 + 1];
  2424. float b = ((float *)ptr)[ofs * 3 + 2];
  2425. return Color(r, g, b, 1);
  2426. }
  2427. case FORMAT_RGBAF: {
  2428. float r = ((float *)ptr)[ofs * 4 + 0];
  2429. float g = ((float *)ptr)[ofs * 4 + 1];
  2430. float b = ((float *)ptr)[ofs * 4 + 2];
  2431. float a = ((float *)ptr)[ofs * 4 + 3];
  2432. return Color(r, g, b, a);
  2433. }
  2434. case FORMAT_RH: {
  2435. uint16_t r = ((uint16_t *)ptr)[ofs];
  2436. return Color(Math::half_to_float(r), 0, 0, 1);
  2437. }
  2438. case FORMAT_RGH: {
  2439. uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0];
  2440. uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1];
  2441. return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1);
  2442. }
  2443. case FORMAT_RGBH: {
  2444. uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0];
  2445. uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1];
  2446. uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2];
  2447. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1);
  2448. }
  2449. case FORMAT_RGBAH: {
  2450. uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0];
  2451. uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1];
  2452. uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2];
  2453. uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3];
  2454. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a));
  2455. }
  2456. case FORMAT_RGBE9995: {
  2457. return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]);
  2458. }
  2459. default: {
  2460. ERR_FAIL_V_MSG(Color(), "Can't get_pixel() on compressed image, sorry.");
  2461. }
  2462. }
  2463. }
  2464. void Image::_set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color) {
  2465. switch (format) {
  2466. case FORMAT_L8: {
  2467. ptr[ofs] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  2468. } break;
  2469. case FORMAT_LA8: {
  2470. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  2471. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  2472. } break;
  2473. case FORMAT_R8: {
  2474. ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2475. } break;
  2476. case FORMAT_RG8: {
  2477. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2478. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2479. } break;
  2480. case FORMAT_RGB8: {
  2481. ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2482. ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2483. ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  2484. } break;
  2485. case FORMAT_RGBA8: {
  2486. ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2487. ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2488. ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  2489. ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  2490. } break;
  2491. case FORMAT_RGBA4444: {
  2492. uint16_t rgba = 0;
  2493. rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15)) << 12;
  2494. rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 8;
  2495. rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 4;
  2496. rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15));
  2497. ((uint16_t *)ptr)[ofs] = rgba;
  2498. } break;
  2499. case FORMAT_RGB565: {
  2500. uint16_t rgba = 0;
  2501. rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
  2502. rgba |= uint16_t(CLAMP(p_color.g * 63.0, 0, 33)) << 5;
  2503. rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 11;
  2504. ((uint16_t *)ptr)[ofs] = rgba;
  2505. } break;
  2506. case FORMAT_RF: {
  2507. ((float *)ptr)[ofs] = p_color.r;
  2508. } break;
  2509. case FORMAT_RGF: {
  2510. ((float *)ptr)[ofs * 2 + 0] = p_color.r;
  2511. ((float *)ptr)[ofs * 2 + 1] = p_color.g;
  2512. } break;
  2513. case FORMAT_RGBF: {
  2514. ((float *)ptr)[ofs * 3 + 0] = p_color.r;
  2515. ((float *)ptr)[ofs * 3 + 1] = p_color.g;
  2516. ((float *)ptr)[ofs * 3 + 2] = p_color.b;
  2517. } break;
  2518. case FORMAT_RGBAF: {
  2519. ((float *)ptr)[ofs * 4 + 0] = p_color.r;
  2520. ((float *)ptr)[ofs * 4 + 1] = p_color.g;
  2521. ((float *)ptr)[ofs * 4 + 2] = p_color.b;
  2522. ((float *)ptr)[ofs * 4 + 3] = p_color.a;
  2523. } break;
  2524. case FORMAT_RH: {
  2525. ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r);
  2526. } break;
  2527. case FORMAT_RGH: {
  2528. ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r);
  2529. ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g);
  2530. } break;
  2531. case FORMAT_RGBH: {
  2532. ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r);
  2533. ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g);
  2534. ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b);
  2535. } break;
  2536. case FORMAT_RGBAH: {
  2537. ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r);
  2538. ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g);
  2539. ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b);
  2540. ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a);
  2541. } break;
  2542. case FORMAT_RGBE9995: {
  2543. ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995();
  2544. } break;
  2545. default: {
  2546. ERR_FAIL_MSG("Can't set_pixel() on compressed image, sorry.");
  2547. }
  2548. }
  2549. }
  2550. Color Image::get_pixel(int p_x, int p_y) const {
  2551. #ifdef DEBUG_ENABLED
  2552. ERR_FAIL_INDEX_V(p_x, width, Color());
  2553. ERR_FAIL_INDEX_V(p_y, height, Color());
  2554. #endif
  2555. uint32_t ofs = p_y * width + p_x;
  2556. return _get_color_at_ofs(data.ptr(), ofs);
  2557. }
  2558. void Image::set_pixelv(const Point2i &p_point, const Color &p_color) {
  2559. set_pixel(p_point.x, p_point.y, p_color);
  2560. }
  2561. void Image::set_pixel(int p_x, int p_y, const Color &p_color) {
  2562. #ifdef DEBUG_ENABLED
  2563. ERR_FAIL_INDEX(p_x, width);
  2564. ERR_FAIL_INDEX(p_y, height);
  2565. #endif
  2566. uint32_t ofs = p_y * width + p_x;
  2567. _set_color_at_ofs(data.ptrw(), ofs, p_color);
  2568. }
  2569. void Image::adjust_bcs(float p_brightness, float p_contrast, float p_saturation) {
  2570. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot adjust_bcs in compressed or custom image formats.");
  2571. uint8_t *w = data.ptrw();
  2572. uint32_t pixel_size = get_format_pixel_size(format);
  2573. uint32_t pixel_count = data.size() / pixel_size;
  2574. for (uint32_t i = 0; i < pixel_count; i++) {
  2575. Color c = _get_color_at_ofs(w, i);
  2576. Vector3 rgb(c.r, c.g, c.b);
  2577. rgb *= p_brightness;
  2578. rgb = Vector3(0.5, 0.5, 0.5).lerp(rgb, p_contrast);
  2579. float center = (rgb.x + rgb.y + rgb.z) / 3.0;
  2580. rgb = Vector3(center, center, center).lerp(rgb, p_saturation);
  2581. c.r = rgb.x;
  2582. c.g = rgb.y;
  2583. c.b = rgb.z;
  2584. _set_color_at_ofs(w, i, c);
  2585. }
  2586. }
  2587. Image::UsedChannels Image::detect_used_channels(CompressSource p_source) {
  2588. ERR_FAIL_COND_V(data.size() == 0, USED_CHANNELS_RGBA);
  2589. ERR_FAIL_COND_V(is_compressed(), USED_CHANNELS_RGBA);
  2590. bool r = false, g = false, b = false, a = false, c = false;
  2591. const uint8_t *data_ptr = data.ptr();
  2592. uint32_t data_total = width * height;
  2593. for (uint32_t i = 0; i < data_total; i++) {
  2594. Color col = _get_color_at_ofs(data_ptr, i);
  2595. if (col.r > 0.001) {
  2596. r = true;
  2597. }
  2598. if (col.g > 0.001) {
  2599. g = true;
  2600. }
  2601. if (col.b > 0.001) {
  2602. b = true;
  2603. }
  2604. if (col.a < 0.999) {
  2605. a = true;
  2606. }
  2607. if (col.r != col.b || col.r != col.g || col.b != col.g) {
  2608. c = true;
  2609. }
  2610. }
  2611. UsedChannels used_channels;
  2612. if (!c && !a) {
  2613. used_channels = USED_CHANNELS_L;
  2614. } else if (!c && a) {
  2615. used_channels = USED_CHANNELS_LA;
  2616. } else if (r && !g && !b && !a) {
  2617. used_channels = USED_CHANNELS_R;
  2618. } else if (r && g && !b && !a) {
  2619. used_channels = USED_CHANNELS_RG;
  2620. } else if (r && g && b && !a) {
  2621. used_channels = USED_CHANNELS_RGB;
  2622. } else {
  2623. used_channels = USED_CHANNELS_RGBA;
  2624. }
  2625. if (p_source == COMPRESS_SOURCE_SRGB && (used_channels == USED_CHANNELS_R || used_channels == USED_CHANNELS_RG)) {
  2626. //R and RG do not support SRGB
  2627. used_channels = USED_CHANNELS_RGB;
  2628. }
  2629. if (p_source == COMPRESS_SOURCE_NORMAL) {
  2630. //use RG channels only for normal
  2631. used_channels = USED_CHANNELS_RG;
  2632. }
  2633. return used_channels;
  2634. }
  2635. void Image::optimize_channels() {
  2636. switch (detect_used_channels()) {
  2637. case USED_CHANNELS_L:
  2638. convert(FORMAT_L8);
  2639. break;
  2640. case USED_CHANNELS_LA:
  2641. convert(FORMAT_LA8);
  2642. break;
  2643. case USED_CHANNELS_R:
  2644. convert(FORMAT_R8);
  2645. break;
  2646. case USED_CHANNELS_RG:
  2647. convert(FORMAT_RG8);
  2648. break;
  2649. case USED_CHANNELS_RGB:
  2650. convert(FORMAT_RGB8);
  2651. break;
  2652. case USED_CHANNELS_RGBA:
  2653. convert(FORMAT_RGBA8);
  2654. break;
  2655. }
  2656. }
  2657. void Image::_bind_methods() {
  2658. ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
  2659. ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height);
  2660. ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size);
  2661. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps);
  2662. ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format);
  2663. ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data);
  2664. ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert);
  2665. ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset);
  2666. ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR));
  2667. ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR));
  2668. ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2);
  2669. ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop);
  2670. ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x);
  2671. ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y);
  2672. ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false));
  2673. ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps);
  2674. ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty);
  2675. ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data);
  2676. ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty);
  2677. ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
  2678. ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
  2679. ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer);
  2680. ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false));
  2681. ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
  2682. ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);
  2683. ClassDB::bind_method(D_METHOD("detect_used_channels", "source"), &Image::detect_used_channels, DEFVAL(COMPRESS_SOURCE_GENERIC));
  2684. ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress, DEFVAL(COMPRESS_SOURCE_GENERIC), DEFVAL(0.7));
  2685. ClassDB::bind_method(D_METHOD("compress_from_channels", "mode", "channels", "lossy_quality"), &Image::compress_from_channels, DEFVAL(0.7));
  2686. ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress);
  2687. ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed);
  2688. ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges);
  2689. ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha);
  2690. ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear);
  2691. ClassDB::bind_method(D_METHOD("normal_map_to_xy"), &Image::normal_map_to_xy);
  2692. ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb);
  2693. ClassDB::bind_method(D_METHOD("bump_map_to_normal_map", "bump_scale"), &Image::bump_map_to_normal_map, DEFVAL(1.0));
  2694. ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect);
  2695. ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask);
  2696. ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect);
  2697. ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask);
  2698. ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill);
  2699. ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
  2700. ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect);
  2701. ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from);
  2702. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data);
  2703. ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data);
  2704. ClassDB::bind_method(D_METHOD("get_pixelv", "point"), &Image::get_pixelv);
  2705. ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel);
  2706. ClassDB::bind_method(D_METHOD("set_pixelv", "point", "color"), &Image::set_pixelv);
  2707. ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel);
  2708. ClassDB::bind_method(D_METHOD("adjust_bcs", "brightness", "contrast", "saturation"), &Image::adjust_bcs);
  2709. ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer);
  2710. ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer);
  2711. ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
  2712. ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
  2713. ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer);
  2714. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
  2715. BIND_CONSTANT(MAX_WIDTH);
  2716. BIND_CONSTANT(MAX_HEIGHT);
  2717. BIND_ENUM_CONSTANT(FORMAT_L8); //luminance
  2718. BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha
  2719. BIND_ENUM_CONSTANT(FORMAT_R8);
  2720. BIND_ENUM_CONSTANT(FORMAT_RG8);
  2721. BIND_ENUM_CONSTANT(FORMAT_RGB8);
  2722. BIND_ENUM_CONSTANT(FORMAT_RGBA8);
  2723. BIND_ENUM_CONSTANT(FORMAT_RGBA4444);
  2724. BIND_ENUM_CONSTANT(FORMAT_RGB565);
  2725. BIND_ENUM_CONSTANT(FORMAT_RF); //float
  2726. BIND_ENUM_CONSTANT(FORMAT_RGF);
  2727. BIND_ENUM_CONSTANT(FORMAT_RGBF);
  2728. BIND_ENUM_CONSTANT(FORMAT_RGBAF);
  2729. BIND_ENUM_CONSTANT(FORMAT_RH); //half float
  2730. BIND_ENUM_CONSTANT(FORMAT_RGH);
  2731. BIND_ENUM_CONSTANT(FORMAT_RGBH);
  2732. BIND_ENUM_CONSTANT(FORMAT_RGBAH);
  2733. BIND_ENUM_CONSTANT(FORMAT_RGBE9995);
  2734. BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1
  2735. BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2
  2736. BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3
  2737. BIND_ENUM_CONSTANT(FORMAT_RGTC_R);
  2738. BIND_ENUM_CONSTANT(FORMAT_RGTC_RG);
  2739. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h
  2740. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float /
  2741. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float
  2742. BIND_ENUM_CONSTANT(FORMAT_PVRTC1_2); //pvrtc
  2743. BIND_ENUM_CONSTANT(FORMAT_PVRTC1_2A);
  2744. BIND_ENUM_CONSTANT(FORMAT_PVRTC1_4);
  2745. BIND_ENUM_CONSTANT(FORMAT_PVRTC1_4A);
  2746. BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1
  2747. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2
  2748. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb.
  2749. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11);
  2750. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S);
  2751. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8);
  2752. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8);
  2753. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1);
  2754. BIND_ENUM_CONSTANT(FORMAT_ETC2_RA_AS_RG);
  2755. BIND_ENUM_CONSTANT(FORMAT_DXT5_RA_AS_RG);
  2756. BIND_ENUM_CONSTANT(FORMAT_MAX);
  2757. BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST);
  2758. BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR);
  2759. BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC);
  2760. BIND_ENUM_CONSTANT(INTERPOLATE_TRILINEAR);
  2761. BIND_ENUM_CONSTANT(INTERPOLATE_LANCZOS);
  2762. BIND_ENUM_CONSTANT(ALPHA_NONE);
  2763. BIND_ENUM_CONSTANT(ALPHA_BIT);
  2764. BIND_ENUM_CONSTANT(ALPHA_BLEND);
  2765. BIND_ENUM_CONSTANT(COMPRESS_S3TC);
  2766. BIND_ENUM_CONSTANT(COMPRESS_PVRTC1_4);
  2767. BIND_ENUM_CONSTANT(COMPRESS_ETC);
  2768. BIND_ENUM_CONSTANT(COMPRESS_ETC2);
  2769. BIND_ENUM_CONSTANT(COMPRESS_BPTC);
  2770. BIND_ENUM_CONSTANT(USED_CHANNELS_L);
  2771. BIND_ENUM_CONSTANT(USED_CHANNELS_LA);
  2772. BIND_ENUM_CONSTANT(USED_CHANNELS_R);
  2773. BIND_ENUM_CONSTANT(USED_CHANNELS_RG);
  2774. BIND_ENUM_CONSTANT(USED_CHANNELS_RGB);
  2775. BIND_ENUM_CONSTANT(USED_CHANNELS_RGBA);
  2776. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC);
  2777. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB);
  2778. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
  2779. }
  2780. void Image::set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels)) {
  2781. _image_compress_bc_func = p_compress_func;
  2782. }
  2783. void Image::set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels)) {
  2784. _image_compress_bptc_func = p_compress_func;
  2785. }
  2786. void Image::normal_map_to_xy() {
  2787. convert(Image::FORMAT_RGBA8);
  2788. {
  2789. int len = data.size() / 4;
  2790. uint8_t *data_ptr = data.ptrw();
  2791. for (int i = 0; i < len; i++) {
  2792. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  2793. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  2794. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  2795. }
  2796. }
  2797. convert(Image::FORMAT_LA8);
  2798. }
  2799. Ref<Image> Image::rgbe_to_srgb() {
  2800. if (data.size() == 0) {
  2801. return Ref<Image>();
  2802. }
  2803. ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>());
  2804. Ref<Image> new_image;
  2805. new_image.instantiate();
  2806. new_image->create(width, height, false, Image::FORMAT_RGB8);
  2807. for (int row = 0; row < height; row++) {
  2808. for (int col = 0; col < width; col++) {
  2809. new_image->set_pixel(col, row, get_pixel(col, row).to_srgb());
  2810. }
  2811. }
  2812. if (has_mipmaps()) {
  2813. new_image->generate_mipmaps();
  2814. }
  2815. return new_image;
  2816. }
  2817. Ref<Image> Image::get_image_from_mipmap(int p_mipamp) const {
  2818. int ofs, size, w, h;
  2819. get_mipmap_offset_size_and_dimensions(p_mipamp, ofs, size, w, h);
  2820. Vector<uint8_t> new_data;
  2821. new_data.resize(size);
  2822. {
  2823. uint8_t *wr = new_data.ptrw();
  2824. const uint8_t *rd = data.ptr();
  2825. memcpy(wr, rd + ofs, size);
  2826. }
  2827. Ref<Image> image;
  2828. image.instantiate();
  2829. image->width = w;
  2830. image->height = h;
  2831. image->format = format;
  2832. image->data = new_data;
  2833. image->mipmaps = false;
  2834. return image;
  2835. }
  2836. void Image::bump_map_to_normal_map(float bump_scale) {
  2837. ERR_FAIL_COND(!_can_modify(format));
  2838. convert(Image::FORMAT_RF);
  2839. Vector<uint8_t> result_image; //rgba output
  2840. result_image.resize(width * height * 4);
  2841. {
  2842. const uint8_t *rp = data.ptr();
  2843. uint8_t *wp = result_image.ptrw();
  2844. ERR_FAIL_COND(!rp);
  2845. unsigned char *write_ptr = wp;
  2846. float *read_ptr = (float *)rp;
  2847. for (int ty = 0; ty < height; ty++) {
  2848. int py = ty + 1;
  2849. if (py >= height) {
  2850. py -= height;
  2851. }
  2852. for (int tx = 0; tx < width; tx++) {
  2853. int px = tx + 1;
  2854. if (px >= width) {
  2855. px -= width;
  2856. }
  2857. float here = read_ptr[ty * width + tx];
  2858. float to_right = read_ptr[ty * width + px];
  2859. float above = read_ptr[py * width + tx];
  2860. Vector3 up = Vector3(0, 1, (here - above) * bump_scale);
  2861. Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale);
  2862. Vector3 normal = across.cross(up);
  2863. normal.normalize();
  2864. write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5);
  2865. write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5);
  2866. write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5);
  2867. write_ptr[((ty * width + tx) << 2) + 3] = 255;
  2868. }
  2869. }
  2870. }
  2871. format = FORMAT_RGBA8;
  2872. data = result_image;
  2873. }
  2874. void Image::srgb_to_linear() {
  2875. if (data.size() == 0) {
  2876. return;
  2877. }
  2878. 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 };
  2879. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  2880. if (format == FORMAT_RGBA8) {
  2881. int len = data.size() / 4;
  2882. uint8_t *data_ptr = data.ptrw();
  2883. for (int i = 0; i < len; i++) {
  2884. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  2885. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  2886. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  2887. }
  2888. } else if (format == FORMAT_RGB8) {
  2889. int len = data.size() / 3;
  2890. uint8_t *data_ptr = data.ptrw();
  2891. for (int i = 0; i < len; i++) {
  2892. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  2893. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  2894. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  2895. }
  2896. }
  2897. }
  2898. void Image::premultiply_alpha() {
  2899. if (data.size() == 0) {
  2900. return;
  2901. }
  2902. if (format != FORMAT_RGBA8) {
  2903. return; //not needed
  2904. }
  2905. uint8_t *data_ptr = data.ptrw();
  2906. for (int i = 0; i < height; i++) {
  2907. for (int j = 0; j < width; j++) {
  2908. uint8_t *ptr = &data_ptr[(i * width + j) * 4];
  2909. ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8;
  2910. ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8;
  2911. ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8;
  2912. }
  2913. }
  2914. }
  2915. void Image::fix_alpha_edges() {
  2916. if (data.size() == 0) {
  2917. return;
  2918. }
  2919. if (format != FORMAT_RGBA8) {
  2920. return; //not needed
  2921. }
  2922. Vector<uint8_t> dcopy = data;
  2923. const uint8_t *srcptr = dcopy.ptr();
  2924. uint8_t *data_ptr = data.ptrw();
  2925. const int max_radius = 4;
  2926. const int alpha_threshold = 20;
  2927. const int max_dist = 0x7FFFFFFF;
  2928. for (int i = 0; i < height; i++) {
  2929. for (int j = 0; j < width; j++) {
  2930. const uint8_t *rptr = &srcptr[(i * width + j) * 4];
  2931. uint8_t *wptr = &data_ptr[(i * width + j) * 4];
  2932. if (rptr[3] >= alpha_threshold) {
  2933. continue;
  2934. }
  2935. int closest_dist = max_dist;
  2936. uint8_t closest_color[3];
  2937. int from_x = MAX(0, j - max_radius);
  2938. int to_x = MIN(width - 1, j + max_radius);
  2939. int from_y = MAX(0, i - max_radius);
  2940. int to_y = MIN(height - 1, i + max_radius);
  2941. for (int k = from_y; k <= to_y; k++) {
  2942. for (int l = from_x; l <= to_x; l++) {
  2943. int dy = i - k;
  2944. int dx = j - l;
  2945. int dist = dy * dy + dx * dx;
  2946. if (dist >= closest_dist) {
  2947. continue;
  2948. }
  2949. const uint8_t *rp2 = &srcptr[(k * width + l) << 2];
  2950. if (rp2[3] < alpha_threshold) {
  2951. continue;
  2952. }
  2953. closest_dist = dist;
  2954. closest_color[0] = rp2[0];
  2955. closest_color[1] = rp2[1];
  2956. closest_color[2] = rp2[2];
  2957. }
  2958. }
  2959. if (closest_dist != max_dist) {
  2960. wptr[0] = closest_color[0];
  2961. wptr[1] = closest_color[1];
  2962. wptr[2] = closest_color[2];
  2963. }
  2964. }
  2965. }
  2966. }
  2967. String Image::get_format_name(Format p_format) {
  2968. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  2969. return format_names[p_format];
  2970. }
  2971. Error Image::load_png_from_buffer(const Vector<uint8_t> &p_array) {
  2972. return _load_from_buffer(p_array, _png_mem_loader_func);
  2973. }
  2974. Error Image::load_jpg_from_buffer(const Vector<uint8_t> &p_array) {
  2975. return _load_from_buffer(p_array, _jpg_mem_loader_func);
  2976. }
  2977. Error Image::load_webp_from_buffer(const Vector<uint8_t> &p_array) {
  2978. return _load_from_buffer(p_array, _webp_mem_loader_func);
  2979. }
  2980. Error Image::load_tga_from_buffer(const Vector<uint8_t> &p_array) {
  2981. ERR_FAIL_NULL_V_MSG(
  2982. _tga_mem_loader_func,
  2983. ERR_UNAVAILABLE,
  2984. "The TGA module isn't enabled. Recompile the Godot editor or export template binary with the `module_tga_enabled=yes` SCons option.");
  2985. return _load_from_buffer(p_array, _tga_mem_loader_func);
  2986. }
  2987. Error Image::load_bmp_from_buffer(const Vector<uint8_t> &p_array) {
  2988. ERR_FAIL_NULL_V_MSG(
  2989. _bmp_mem_loader_func,
  2990. ERR_UNAVAILABLE,
  2991. "The BMP module isn't enabled. Recompile the Godot editor or export template binary with the `module_bmp_enabled=yes` SCons option.");
  2992. return _load_from_buffer(p_array, _bmp_mem_loader_func);
  2993. }
  2994. void Image::convert_rg_to_ra_rgba8() {
  2995. ERR_FAIL_COND(format != FORMAT_RGBA8);
  2996. ERR_FAIL_COND(!data.size());
  2997. int s = data.size();
  2998. uint8_t *w = data.ptrw();
  2999. for (int i = 0; i < s; i += 4) {
  3000. w[i + 3] = w[i + 1];
  3001. w[i + 1] = 0;
  3002. w[i + 2] = 0;
  3003. }
  3004. }
  3005. void Image::convert_ra_rgba8_to_rg() {
  3006. ERR_FAIL_COND(format != FORMAT_RGBA8);
  3007. ERR_FAIL_COND(!data.size());
  3008. int s = data.size();
  3009. uint8_t *w = data.ptrw();
  3010. for (int i = 0; i < s; i += 4) {
  3011. w[i + 1] = w[i + 3];
  3012. w[i + 2] = 0;
  3013. w[i + 3] = 255;
  3014. }
  3015. }
  3016. Error Image::_load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
  3017. int buffer_size = p_array.size();
  3018. ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
  3019. ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER);
  3020. const uint8_t *r = p_array.ptr();
  3021. Ref<Image> image = p_loader(r, buffer_size);
  3022. ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR);
  3023. copy_internals_from(image);
  3024. return OK;
  3025. }
  3026. 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) {
  3027. p_out = static_cast<uint8_t>((p_a + p_b + p_c + p_d + 2) >> 2);
  3028. }
  3029. void Image::average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d) {
  3030. p_out = (p_a + p_b + p_c + p_d) * 0.25f;
  3031. }
  3032. 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) {
  3033. 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);
  3034. }
  3035. 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) {
  3036. 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();
  3037. }
  3038. void Image::renormalize_uint8(uint8_t *p_rgb) {
  3039. Vector3 n(p_rgb[0] / 255.0, p_rgb[1] / 255.0, p_rgb[2] / 255.0);
  3040. n *= 2.0;
  3041. n -= Vector3(1, 1, 1);
  3042. n.normalize();
  3043. n += Vector3(1, 1, 1);
  3044. n *= 0.5;
  3045. n *= 255;
  3046. p_rgb[0] = CLAMP(int(n.x), 0, 255);
  3047. p_rgb[1] = CLAMP(int(n.y), 0, 255);
  3048. p_rgb[2] = CLAMP(int(n.z), 0, 255);
  3049. }
  3050. void Image::renormalize_float(float *p_rgb) {
  3051. Vector3 n(p_rgb[0], p_rgb[1], p_rgb[2]);
  3052. n.normalize();
  3053. p_rgb[0] = n.x;
  3054. p_rgb[1] = n.y;
  3055. p_rgb[2] = n.z;
  3056. }
  3057. void Image::renormalize_half(uint16_t *p_rgb) {
  3058. Vector3 n(Math::half_to_float(p_rgb[0]), Math::half_to_float(p_rgb[1]), Math::half_to_float(p_rgb[2]));
  3059. n.normalize();
  3060. p_rgb[0] = Math::make_half_float(n.x);
  3061. p_rgb[1] = Math::make_half_float(n.y);
  3062. p_rgb[2] = Math::make_half_float(n.z);
  3063. }
  3064. void Image::renormalize_rgbe9995(uint32_t *p_rgb) {
  3065. // Never used
  3066. }
  3067. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  3068. width = 0;
  3069. height = 0;
  3070. mipmaps = false;
  3071. format = FORMAT_L8;
  3072. if (_png_mem_loader_func) {
  3073. copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len));
  3074. }
  3075. if (is_empty() && _jpg_mem_loader_func) {
  3076. copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len));
  3077. }
  3078. }
  3079. Ref<Resource> Image::duplicate(bool p_subresources) const {
  3080. Ref<Image> copy;
  3081. copy.instantiate();
  3082. copy->_copy_internals_from(*this);
  3083. return copy;
  3084. }
  3085. void Image::set_as_black() {
  3086. memset(data.ptrw(), 0, data.size());
  3087. }