image.cpp 104 KB

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