image.cpp 104 KB

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