image.cpp 99 KB

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