image.cpp 96 KB

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