image.cpp 105 KB

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