image.cpp 101 KB

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