image.cpp 97 KB

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