image.cpp 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370
  1. /**************************************************************************/
  2. /* image.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "image.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/error/error_list.h"
  33. #include "core/error/error_macros.h"
  34. #include "core/io/image_loader.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/math/math_funcs.h"
  37. #include "core/templates/hash_map.h"
  38. #include "core/variant/dictionary.h"
  39. #include <cmath>
  40. const char *Image::format_names[Image::FORMAT_MAX] = {
  41. "Lum8",
  42. "LumAlpha8",
  43. "Red8",
  44. "RedGreen",
  45. "RGB8",
  46. "RGBA8",
  47. "RGBA4444",
  48. "RGBA5551", // Actually RGB565, kept as RGBA5551 for compatibility.
  49. "RFloat",
  50. "RGFloat",
  51. "RGBFloat",
  52. "RGBAFloat",
  53. "RHalf",
  54. "RGHalf",
  55. "RGBHalf",
  56. "RGBAHalf",
  57. "RGBE9995",
  58. "DXT1 RGB8",
  59. "DXT3 RGBA8",
  60. "DXT5 RGBA8",
  61. "RGTC Red8",
  62. "RGTC RedGreen8",
  63. "BPTC_RGBA",
  64. "BPTC_RGBF",
  65. "BPTC_RGBFU",
  66. "ETC",
  67. "ETC2_R11",
  68. "ETC2_R11S",
  69. "ETC2_RG11",
  70. "ETC2_RG11S",
  71. "ETC2_RGB8",
  72. "ETC2_RGBA8",
  73. "ETC2_RGB8A1",
  74. "ETC2_RA_AS_RG",
  75. "FORMAT_DXT5_RA_AS_RG",
  76. "ASTC_4x4",
  77. "ASTC_4x4_HDR",
  78. "ASTC_8x8",
  79. "ASTC_8x8_HDR",
  80. };
  81. // External saver function pointers.
  82. SavePNGFunc Image::save_png_func = nullptr;
  83. SaveJPGFunc Image::save_jpg_func = nullptr;
  84. SaveEXRFunc Image::save_exr_func = nullptr;
  85. SaveWebPFunc Image::save_webp_func = nullptr;
  86. SavePNGBufferFunc Image::save_png_buffer_func = nullptr;
  87. SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr;
  88. SaveEXRBufferFunc Image::save_exr_buffer_func = nullptr;
  89. SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr;
  90. // External loader function pointers.
  91. ImageMemLoadFunc Image::_png_mem_loader_func = nullptr;
  92. ImageMemLoadFunc Image::_png_mem_unpacker_func = nullptr;
  93. ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr;
  94. ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr;
  95. ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
  96. ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
  97. ScalableImageMemLoadFunc Image::_svg_scalable_mem_loader_func = nullptr;
  98. ImageMemLoadFunc Image::_ktx_mem_loader_func = nullptr;
  99. // External VRAM compression function pointers.
  100. void (*Image::_image_compress_bc_func)(Image *, Image::UsedChannels) = nullptr;
  101. void (*Image::_image_compress_bptc_func)(Image *, Image::UsedChannels) = nullptr;
  102. void (*Image::_image_compress_etc1_func)(Image *) = nullptr;
  103. void (*Image::_image_compress_etc2_func)(Image *, Image::UsedChannels) = nullptr;
  104. void (*Image::_image_compress_astc_func)(Image *, Image::ASTCFormat) = nullptr;
  105. Error (*Image::_image_compress_bptc_rd_func)(Image *, Image::UsedChannels) = nullptr;
  106. Error (*Image::_image_compress_bc_rd_func)(Image *, Image::UsedChannels) = nullptr;
  107. // External VRAM decompression function pointers.
  108. void (*Image::_image_decompress_bc)(Image *) = nullptr;
  109. void (*Image::_image_decompress_bptc)(Image *) = nullptr;
  110. void (*Image::_image_decompress_etc1)(Image *) = nullptr;
  111. void (*Image::_image_decompress_etc2)(Image *) = nullptr;
  112. void (*Image::_image_decompress_astc)(Image *) = nullptr;
  113. // External packer function pointers.
  114. Vector<uint8_t> (*Image::webp_lossy_packer)(const Ref<Image> &, float) = nullptr;
  115. Vector<uint8_t> (*Image::webp_lossless_packer)(const Ref<Image> &) = nullptr;
  116. Vector<uint8_t> (*Image::png_packer)(const Ref<Image> &) = nullptr;
  117. Vector<uint8_t> (*Image::basis_universal_packer)(const Ref<Image> &, Image::UsedChannels) = nullptr;
  118. Ref<Image> (*Image::webp_unpacker)(const Vector<uint8_t> &) = nullptr;
  119. Ref<Image> (*Image::png_unpacker)(const Vector<uint8_t> &) = nullptr;
  120. Ref<Image> (*Image::basis_universal_unpacker)(const Vector<uint8_t> &) = nullptr;
  121. Ref<Image> (*Image::basis_universal_unpacker_ptr)(const uint8_t *, int) = nullptr;
  122. void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel) {
  123. uint32_t ofs = (p_y * width + p_x) * p_pixel_size;
  124. memcpy(p_data + ofs, p_pixel, p_pixel_size);
  125. }
  126. void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixel_size, const uint8_t *p_data, uint8_t *p_pixel) {
  127. uint32_t ofs = (p_y * width + p_x) * p_pixel_size;
  128. memcpy(p_pixel, p_data + ofs, p_pixel_size);
  129. }
  130. int Image::get_format_pixel_size(Format p_format) {
  131. switch (p_format) {
  132. case FORMAT_L8:
  133. return 1;
  134. case FORMAT_LA8:
  135. return 2;
  136. case FORMAT_R8:
  137. return 1;
  138. case FORMAT_RG8:
  139. return 2;
  140. case FORMAT_RGB8:
  141. return 3;
  142. case FORMAT_RGBA8:
  143. return 4;
  144. case FORMAT_RGBA4444:
  145. return 2;
  146. case FORMAT_RGB565:
  147. return 2;
  148. case FORMAT_RF:
  149. return 4;
  150. case FORMAT_RGF:
  151. return 8;
  152. case FORMAT_RGBF:
  153. return 12;
  154. case FORMAT_RGBAF:
  155. return 16;
  156. case FORMAT_RH:
  157. return 2;
  158. case FORMAT_RGH:
  159. return 4;
  160. case FORMAT_RGBH:
  161. return 6;
  162. case FORMAT_RGBAH:
  163. return 8;
  164. case FORMAT_RGBE9995:
  165. return 4;
  166. case FORMAT_DXT1:
  167. return 1;
  168. case FORMAT_DXT3:
  169. return 1;
  170. case FORMAT_DXT5:
  171. return 1;
  172. case FORMAT_RGTC_R:
  173. return 1;
  174. case FORMAT_RGTC_RG:
  175. return 1;
  176. case FORMAT_BPTC_RGBA:
  177. return 1;
  178. case FORMAT_BPTC_RGBF:
  179. return 1;
  180. case FORMAT_BPTC_RGBFU:
  181. return 1;
  182. case FORMAT_ETC:
  183. return 1;
  184. case FORMAT_ETC2_R11:
  185. return 1;
  186. case FORMAT_ETC2_R11S:
  187. return 1;
  188. case FORMAT_ETC2_RG11:
  189. return 1;
  190. case FORMAT_ETC2_RG11S:
  191. return 1;
  192. case FORMAT_ETC2_RGB8:
  193. return 1;
  194. case FORMAT_ETC2_RGBA8:
  195. return 1;
  196. case FORMAT_ETC2_RGB8A1:
  197. return 1;
  198. case FORMAT_ETC2_RA_AS_RG:
  199. return 1;
  200. case FORMAT_DXT5_RA_AS_RG:
  201. return 1;
  202. case FORMAT_ASTC_4x4:
  203. return 1;
  204. case FORMAT_ASTC_4x4_HDR:
  205. return 1;
  206. case FORMAT_ASTC_8x8:
  207. return 1;
  208. case FORMAT_ASTC_8x8_HDR:
  209. return 1;
  210. case FORMAT_MAX: {
  211. }
  212. }
  213. return 0;
  214. }
  215. void Image::get_format_min_pixel_size(Format p_format, int &r_w, int &r_h) {
  216. switch (p_format) {
  217. case FORMAT_DXT1:
  218. case FORMAT_DXT3:
  219. case FORMAT_DXT5:
  220. case FORMAT_RGTC_R:
  221. case FORMAT_RGTC_RG: {
  222. r_w = 4;
  223. r_h = 4;
  224. } break;
  225. case FORMAT_ETC: {
  226. r_w = 4;
  227. r_h = 4;
  228. } break;
  229. case FORMAT_BPTC_RGBA:
  230. case FORMAT_BPTC_RGBF:
  231. case FORMAT_BPTC_RGBFU: {
  232. r_w = 4;
  233. r_h = 4;
  234. } break;
  235. case FORMAT_ETC2_R11:
  236. case FORMAT_ETC2_R11S:
  237. case FORMAT_ETC2_RG11:
  238. case FORMAT_ETC2_RG11S:
  239. case FORMAT_ETC2_RGB8:
  240. case FORMAT_ETC2_RGBA8:
  241. case FORMAT_ETC2_RGB8A1:
  242. case FORMAT_ETC2_RA_AS_RG:
  243. case FORMAT_DXT5_RA_AS_RG: {
  244. r_w = 4;
  245. r_h = 4;
  246. } break;
  247. case FORMAT_ASTC_4x4:
  248. case FORMAT_ASTC_4x4_HDR: {
  249. r_w = 4;
  250. r_h = 4;
  251. } break;
  252. case FORMAT_ASTC_8x8:
  253. case FORMAT_ASTC_8x8_HDR: {
  254. r_w = 8;
  255. r_h = 8;
  256. } break;
  257. default: {
  258. r_w = 1;
  259. r_h = 1;
  260. } break;
  261. }
  262. }
  263. int Image::get_format_pixel_rshift(Format p_format) {
  264. if (p_format == FORMAT_ASTC_8x8) {
  265. return 2;
  266. } else if (p_format == FORMAT_DXT1 || p_format == FORMAT_RGTC_R || p_format == FORMAT_ETC || p_format == FORMAT_ETC2_R11 || p_format == FORMAT_ETC2_R11S || p_format == FORMAT_ETC2_RGB8 || p_format == FORMAT_ETC2_RGB8A1) {
  267. return 1;
  268. } else {
  269. return 0;
  270. }
  271. }
  272. int Image::get_format_block_size(Format p_format) {
  273. switch (p_format) {
  274. case FORMAT_DXT1:
  275. case FORMAT_DXT3:
  276. case FORMAT_DXT5:
  277. case FORMAT_RGTC_R:
  278. case FORMAT_RGTC_RG: {
  279. return 4;
  280. }
  281. case FORMAT_ETC: {
  282. return 4;
  283. }
  284. case FORMAT_BPTC_RGBA:
  285. case FORMAT_BPTC_RGBF:
  286. case FORMAT_BPTC_RGBFU: {
  287. return 4;
  288. }
  289. case FORMAT_ETC2_R11:
  290. case FORMAT_ETC2_R11S:
  291. case FORMAT_ETC2_RG11:
  292. case FORMAT_ETC2_RG11S:
  293. case FORMAT_ETC2_RGB8:
  294. case FORMAT_ETC2_RGBA8:
  295. case FORMAT_ETC2_RGB8A1:
  296. case FORMAT_ETC2_RA_AS_RG:
  297. case FORMAT_DXT5_RA_AS_RG: {
  298. return 4;
  299. }
  300. case FORMAT_ASTC_4x4:
  301. case FORMAT_ASTC_4x4_HDR: {
  302. return 4;
  303. }
  304. case FORMAT_ASTC_8x8:
  305. case FORMAT_ASTC_8x8_HDR: {
  306. return 8;
  307. }
  308. default: {
  309. }
  310. }
  311. return 1;
  312. }
  313. void Image::_get_mipmap_offset_and_size(int p_mipmap, int64_t &r_offset, int &r_width, int &r_height) const {
  314. int w = width;
  315. int h = height;
  316. int64_t ofs = 0;
  317. int pixel_size = get_format_pixel_size(format);
  318. int pixel_rshift = get_format_pixel_rshift(format);
  319. int block = get_format_block_size(format);
  320. int minw, minh;
  321. get_format_min_pixel_size(format, minw, minh);
  322. for (int i = 0; i < p_mipmap; i++) {
  323. int bw = w % block != 0 ? w + (block - w % block) : w;
  324. int bh = h % block != 0 ? h + (block - h % block) : h;
  325. int64_t s = bw * bh;
  326. s *= pixel_size;
  327. s >>= pixel_rshift;
  328. ofs += s;
  329. w = MAX(minw, w >> 1);
  330. h = MAX(minh, h >> 1);
  331. }
  332. r_offset = ofs;
  333. r_width = w;
  334. r_height = h;
  335. }
  336. int64_t Image::get_mipmap_offset(int p_mipmap) const {
  337. ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1);
  338. int64_t ofs;
  339. int w, h;
  340. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  341. return ofs;
  342. }
  343. void Image::get_mipmap_offset_and_size(int p_mipmap, int64_t &r_ofs, int64_t &r_size) const {
  344. int64_t ofs;
  345. int w, h;
  346. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  347. int64_t ofs2;
  348. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h);
  349. r_ofs = ofs;
  350. r_size = ofs2 - ofs;
  351. }
  352. void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap, int64_t &r_ofs, int64_t &r_size, int &w, int &h) const {
  353. int64_t ofs;
  354. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  355. int64_t ofs2;
  356. int w2, h2;
  357. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w2, h2);
  358. r_ofs = ofs;
  359. r_size = ofs2 - ofs;
  360. }
  361. Image::Image3DValidateError Image::validate_3d_image(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_images) {
  362. int w = p_width;
  363. int h = p_height;
  364. int d = p_depth;
  365. int arr_ofs = 0;
  366. while (true) {
  367. for (int i = 0; i < d; i++) {
  368. int idx = i + arr_ofs;
  369. if (idx >= p_images.size()) {
  370. return VALIDATE_3D_ERR_MISSING_IMAGES;
  371. }
  372. if (p_images[idx].is_null() || p_images[idx]->is_empty()) {
  373. return VALIDATE_3D_ERR_IMAGE_EMPTY;
  374. }
  375. if (p_images[idx]->get_format() != p_format) {
  376. return VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH;
  377. }
  378. if (p_images[idx]->get_width() != w || p_images[idx]->get_height() != h) {
  379. return VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH;
  380. }
  381. if (p_images[idx]->has_mipmaps()) {
  382. return VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS;
  383. }
  384. }
  385. arr_ofs += d;
  386. if (!p_mipmaps) {
  387. break;
  388. }
  389. if (w == 1 && h == 1 && d == 1) {
  390. break;
  391. }
  392. w = MAX(1, w >> 1);
  393. h = MAX(1, h >> 1);
  394. d = MAX(1, d >> 1);
  395. }
  396. if (arr_ofs != p_images.size()) {
  397. return VALIDATE_3D_ERR_EXTRA_IMAGES;
  398. }
  399. return VALIDATE_3D_OK;
  400. }
  401. String Image::get_3d_image_validation_error_text(Image3DValidateError p_error) {
  402. switch (p_error) {
  403. case VALIDATE_3D_OK: {
  404. return "Ok";
  405. } break;
  406. case VALIDATE_3D_ERR_IMAGE_EMPTY: {
  407. return "Empty Image found";
  408. } break;
  409. case VALIDATE_3D_ERR_MISSING_IMAGES: {
  410. return "Missing Images";
  411. } break;
  412. case VALIDATE_3D_ERR_EXTRA_IMAGES: {
  413. return "Too many Images";
  414. } break;
  415. case VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH: {
  416. return "Image size mismatch";
  417. } break;
  418. case VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH: {
  419. return "Image format mismatch";
  420. } break;
  421. case VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS: {
  422. return "Image has included mipmaps";
  423. } break;
  424. }
  425. return String();
  426. }
  427. int Image::get_width() const {
  428. return width;
  429. }
  430. int Image::get_height() const {
  431. return height;
  432. }
  433. Size2i Image::get_size() const {
  434. return Size2i(width, height);
  435. }
  436. bool Image::has_mipmaps() const {
  437. return mipmaps;
  438. }
  439. int Image::get_mipmap_count() const {
  440. if (mipmaps) {
  441. return get_image_required_mipmaps(width, height, format);
  442. } else {
  443. return 0;
  444. }
  445. }
  446. // Using template generates perfectly optimized code due to constant expression reduction and unused variable removal present in all compilers.
  447. template <uint32_t read_bytes, bool read_alpha, uint32_t write_bytes, bool write_alpha, bool read_gray, bool write_gray>
  448. static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p_dst) {
  449. constexpr uint32_t max_bytes = MAX(read_bytes, write_bytes);
  450. for (int y = 0; y < p_height; y++) {
  451. for (int x = 0; x < p_width; x++) {
  452. const uint8_t *rofs = &p_src[((y * p_width) + x) * (read_bytes + (read_alpha ? 1 : 0))];
  453. uint8_t *wofs = &p_dst[((y * p_width) + x) * (write_bytes + (write_alpha ? 1 : 0))];
  454. uint8_t rgba[4] = { 0, 0, 0, 255 };
  455. if constexpr (read_gray) {
  456. rgba[0] = rofs[0];
  457. rgba[1] = rofs[0];
  458. rgba[2] = rofs[0];
  459. } else {
  460. for (uint32_t i = 0; i < max_bytes; i++) {
  461. rgba[i] = (i < read_bytes) ? rofs[i] : 0;
  462. }
  463. }
  464. if constexpr (read_alpha || write_alpha) {
  465. rgba[3] = read_alpha ? rofs[read_bytes] : 255;
  466. }
  467. if constexpr (write_gray) {
  468. // REC.709
  469. const uint8_t luminance = (13938U * rgba[0] + 46869U * rgba[1] + 4729U * rgba[2] + 32768U) >> 16U;
  470. wofs[0] = luminance;
  471. } else {
  472. for (uint32_t i = 0; i < write_bytes; i++) {
  473. wofs[i] = rgba[i];
  474. }
  475. }
  476. if constexpr (write_alpha) {
  477. wofs[write_bytes] = rgba[3];
  478. }
  479. }
  480. }
  481. }
  482. template <typename T, uint32_t read_channels, uint32_t write_channels, T def_zero, T def_one>
  483. static void _convert_fast(int p_width, int p_height, const T *p_src, T *p_dst) {
  484. uint32_t dst_count = 0;
  485. uint32_t src_count = 0;
  486. const int resolution = p_width * p_height;
  487. for (int i = 0; i < resolution; i++) {
  488. memcpy(p_dst + dst_count, p_src + src_count, MIN(read_channels, write_channels) * sizeof(T));
  489. if constexpr (write_channels > read_channels) {
  490. const T def_value[4] = { def_zero, def_zero, def_zero, def_one };
  491. memcpy(p_dst + dst_count + read_channels, &def_value[read_channels], (write_channels - read_channels) * sizeof(T));
  492. }
  493. dst_count += write_channels;
  494. src_count += read_channels;
  495. }
  496. }
  497. static bool _are_formats_compatible(Image::Format p_format0, Image::Format p_format1) {
  498. if (p_format0 <= Image::FORMAT_RGBA8 && p_format1 <= Image::FORMAT_RGBA8) {
  499. return true;
  500. } else if (p_format0 <= Image::FORMAT_RGBAH && p_format0 >= Image::FORMAT_RH && p_format1 <= Image::FORMAT_RGBAH && p_format1 >= Image::FORMAT_RH) {
  501. return true;
  502. } else if (p_format0 <= Image::FORMAT_RGBAF && p_format0 >= Image::FORMAT_RF && p_format1 <= Image::FORMAT_RGBAF && p_format1 >= Image::FORMAT_RF) {
  503. return true;
  504. }
  505. return false;
  506. }
  507. void Image::convert(Format p_new_format) {
  508. ERR_FAIL_INDEX_MSG(p_new_format, FORMAT_MAX, vformat("The Image format specified (%d) is out of range. See Image's Format enum.", p_new_format));
  509. if (data.size() == 0 || p_new_format == format) {
  510. return;
  511. }
  512. ERR_FAIL_COND_MSG(Image::is_format_compressed(format) || Image::is_format_compressed(p_new_format),
  513. "Cannot convert to (or from) compressed formats. Use compress() and decompress() instead.");
  514. // Includes the main image.
  515. const int mipmap_count = get_mipmap_count() + 1;
  516. if (!_are_formats_compatible(format, p_new_format)) {
  517. // Use put/set pixel which is slower but works with non-byte formats.
  518. Image new_img(width, height, mipmaps, p_new_format);
  519. for (int mip = 0; mip < mipmap_count; mip++) {
  520. Ref<Image> src_mip = get_image_from_mipmap(mip);
  521. Ref<Image> new_mip = new_img.get_image_from_mipmap(mip);
  522. for (int y = 0; y < src_mip->height; y++) {
  523. for (int x = 0; x < src_mip->width; x++) {
  524. new_mip->set_pixel(x, y, src_mip->get_pixel(x, y));
  525. }
  526. }
  527. int64_t mip_offset = 0;
  528. int64_t mip_size = 0;
  529. new_img.get_mipmap_offset_and_size(mip, mip_offset, mip_size);
  530. memcpy(new_img.data.ptrw() + mip_offset, new_mip->data.ptr(), mip_size);
  531. }
  532. _copy_internals_from(new_img);
  533. return;
  534. }
  535. // Convert the formats in an optimized way by removing/adding color channels if necessary.
  536. Image new_img(width, height, mipmaps, p_new_format);
  537. const int conversion_type = format | p_new_format << 8;
  538. for (int mip = 0; mip < mipmap_count; mip++) {
  539. int64_t mip_offset = 0;
  540. int64_t mip_size = 0;
  541. int mip_width = 0;
  542. int mip_height = 0;
  543. get_mipmap_offset_size_and_dimensions(mip, mip_offset, mip_size, mip_width, mip_height);
  544. const uint8_t *rptr = data.ptr() + mip_offset;
  545. uint8_t *wptr = new_img.data.ptrw() + new_img.get_mipmap_offset(mip);
  546. switch (conversion_type) {
  547. case FORMAT_L8 | (FORMAT_LA8 << 8):
  548. _convert<1, false, 1, true, true, true>(mip_width, mip_height, rptr, wptr);
  549. break;
  550. case FORMAT_L8 | (FORMAT_R8 << 8):
  551. _convert<1, false, 1, false, true, false>(mip_width, mip_height, rptr, wptr);
  552. break;
  553. case FORMAT_L8 | (FORMAT_RG8 << 8):
  554. _convert<1, false, 2, false, true, false>(mip_width, mip_height, rptr, wptr);
  555. break;
  556. case FORMAT_L8 | (FORMAT_RGB8 << 8):
  557. _convert<1, false, 3, false, true, false>(mip_width, mip_height, rptr, wptr);
  558. break;
  559. case FORMAT_L8 | (FORMAT_RGBA8 << 8):
  560. _convert<1, false, 3, true, true, false>(mip_width, mip_height, rptr, wptr);
  561. break;
  562. case FORMAT_LA8 | (FORMAT_L8 << 8):
  563. _convert<1, true, 1, false, true, true>(mip_width, mip_height, rptr, wptr);
  564. break;
  565. case FORMAT_LA8 | (FORMAT_R8 << 8):
  566. _convert<1, true, 1, false, true, false>(mip_width, mip_height, rptr, wptr);
  567. break;
  568. case FORMAT_LA8 | (FORMAT_RG8 << 8):
  569. _convert<1, true, 2, false, true, false>(mip_width, mip_height, rptr, wptr);
  570. break;
  571. case FORMAT_LA8 | (FORMAT_RGB8 << 8):
  572. _convert<1, true, 3, false, true, false>(mip_width, mip_height, rptr, wptr);
  573. break;
  574. case FORMAT_LA8 | (FORMAT_RGBA8 << 8):
  575. _convert<1, true, 3, true, true, false>(mip_width, mip_height, rptr, wptr);
  576. break;
  577. case FORMAT_R8 | (FORMAT_L8 << 8):
  578. _convert<1, false, 1, false, false, true>(mip_width, mip_height, rptr, wptr);
  579. break;
  580. case FORMAT_R8 | (FORMAT_LA8 << 8):
  581. _convert<1, false, 1, true, false, true>(mip_width, mip_height, rptr, wptr);
  582. break;
  583. case FORMAT_R8 | (FORMAT_RG8 << 8):
  584. _convert<1, false, 2, false, false, false>(mip_width, mip_height, rptr, wptr);
  585. break;
  586. case FORMAT_R8 | (FORMAT_RGB8 << 8):
  587. _convert<1, false, 3, false, false, false>(mip_width, mip_height, rptr, wptr);
  588. break;
  589. case FORMAT_R8 | (FORMAT_RGBA8 << 8):
  590. _convert<1, false, 3, true, false, false>(mip_width, mip_height, rptr, wptr);
  591. break;
  592. case FORMAT_RG8 | (FORMAT_L8 << 8):
  593. _convert<2, false, 1, false, false, true>(mip_width, mip_height, rptr, wptr);
  594. break;
  595. case FORMAT_RG8 | (FORMAT_LA8 << 8):
  596. _convert<2, false, 1, true, false, true>(mip_width, mip_height, rptr, wptr);
  597. break;
  598. case FORMAT_RG8 | (FORMAT_R8 << 8):
  599. _convert<2, false, 1, false, false, false>(mip_width, mip_height, rptr, wptr);
  600. break;
  601. case FORMAT_RG8 | (FORMAT_RGB8 << 8):
  602. _convert<2, false, 3, false, false, false>(mip_width, mip_height, rptr, wptr);
  603. break;
  604. case FORMAT_RG8 | (FORMAT_RGBA8 << 8):
  605. _convert<2, false, 3, true, false, false>(mip_width, mip_height, rptr, wptr);
  606. break;
  607. case FORMAT_RGB8 | (FORMAT_L8 << 8):
  608. _convert<3, false, 1, false, false, true>(mip_width, mip_height, rptr, wptr);
  609. break;
  610. case FORMAT_RGB8 | (FORMAT_LA8 << 8):
  611. _convert<3, false, 1, true, false, true>(mip_width, mip_height, rptr, wptr);
  612. break;
  613. case FORMAT_RGB8 | (FORMAT_R8 << 8):
  614. _convert<3, false, 1, false, false, false>(mip_width, mip_height, rptr, wptr);
  615. break;
  616. case FORMAT_RGB8 | (FORMAT_RG8 << 8):
  617. _convert<3, false, 2, false, false, false>(mip_width, mip_height, rptr, wptr);
  618. break;
  619. case FORMAT_RGB8 | (FORMAT_RGBA8 << 8):
  620. _convert<3, false, 3, true, false, false>(mip_width, mip_height, rptr, wptr);
  621. break;
  622. case FORMAT_RGBA8 | (FORMAT_L8 << 8):
  623. _convert<3, true, 1, false, false, true>(mip_width, mip_height, rptr, wptr);
  624. break;
  625. case FORMAT_RGBA8 | (FORMAT_LA8 << 8):
  626. _convert<3, true, 1, true, false, true>(mip_width, mip_height, rptr, wptr);
  627. break;
  628. case FORMAT_RGBA8 | (FORMAT_R8 << 8):
  629. _convert<3, true, 1, false, false, false>(mip_width, mip_height, rptr, wptr);
  630. break;
  631. case FORMAT_RGBA8 | (FORMAT_RG8 << 8):
  632. _convert<3, true, 2, false, false, false>(mip_width, mip_height, rptr, wptr);
  633. break;
  634. case FORMAT_RGBA8 | (FORMAT_RGB8 << 8):
  635. _convert<3, true, 3, false, false, false>(mip_width, mip_height, rptr, wptr);
  636. break;
  637. case FORMAT_RH | (FORMAT_RGH << 8):
  638. _convert_fast<uint16_t, 1, 2, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  639. break;
  640. case FORMAT_RH | (FORMAT_RGBH << 8):
  641. _convert_fast<uint16_t, 1, 3, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  642. break;
  643. case FORMAT_RH | (FORMAT_RGBAH << 8):
  644. _convert_fast<uint16_t, 1, 4, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  645. break;
  646. case FORMAT_RGH | (FORMAT_RH << 8):
  647. _convert_fast<uint16_t, 2, 1, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  648. break;
  649. case FORMAT_RGH | (FORMAT_RGBH << 8):
  650. _convert_fast<uint16_t, 2, 3, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  651. break;
  652. case FORMAT_RGH | (FORMAT_RGBAH << 8):
  653. _convert_fast<uint16_t, 2, 4, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  654. break;
  655. case FORMAT_RGBH | (FORMAT_RH << 8):
  656. _convert_fast<uint16_t, 3, 1, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  657. break;
  658. case FORMAT_RGBH | (FORMAT_RGH << 8):
  659. _convert_fast<uint16_t, 3, 2, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  660. break;
  661. case FORMAT_RGBH | (FORMAT_RGBAH << 8):
  662. _convert_fast<uint16_t, 3, 4, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  663. break;
  664. case FORMAT_RGBAH | (FORMAT_RH << 8):
  665. _convert_fast<uint16_t, 4, 1, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  666. break;
  667. case FORMAT_RGBAH | (FORMAT_RGH << 8):
  668. _convert_fast<uint16_t, 4, 2, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  669. break;
  670. case FORMAT_RGBAH | (FORMAT_RGBH << 8):
  671. _convert_fast<uint16_t, 4, 3, 0x0000, 0x3C00>(mip_width, mip_height, (const uint16_t *)rptr, (uint16_t *)wptr);
  672. break;
  673. case FORMAT_RF | (FORMAT_RGF << 8):
  674. _convert_fast<uint32_t, 1, 2, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  675. break;
  676. case FORMAT_RF | (FORMAT_RGBF << 8):
  677. _convert_fast<uint32_t, 1, 3, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  678. break;
  679. case FORMAT_RF | (FORMAT_RGBAF << 8):
  680. _convert_fast<uint32_t, 1, 4, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  681. break;
  682. case FORMAT_RGF | (FORMAT_RF << 8):
  683. _convert_fast<uint32_t, 2, 1, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  684. break;
  685. case FORMAT_RGF | (FORMAT_RGBF << 8):
  686. _convert_fast<uint32_t, 2, 3, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  687. break;
  688. case FORMAT_RGF | (FORMAT_RGBAF << 8):
  689. _convert_fast<uint32_t, 2, 4, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  690. break;
  691. case FORMAT_RGBF | (FORMAT_RF << 8):
  692. _convert_fast<uint32_t, 3, 1, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  693. break;
  694. case FORMAT_RGBF | (FORMAT_RGF << 8):
  695. _convert_fast<uint32_t, 3, 2, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  696. break;
  697. case FORMAT_RGBF | (FORMAT_RGBAF << 8):
  698. _convert_fast<uint32_t, 3, 4, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  699. break;
  700. case FORMAT_RGBAF | (FORMAT_RF << 8):
  701. _convert_fast<uint32_t, 4, 1, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  702. break;
  703. case FORMAT_RGBAF | (FORMAT_RGF << 8):
  704. _convert_fast<uint32_t, 4, 2, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  705. break;
  706. case FORMAT_RGBAF | (FORMAT_RGBF << 8):
  707. _convert_fast<uint32_t, 4, 3, 0x00000000, 0x3F800000>(mip_width, mip_height, (const uint32_t *)rptr, (uint32_t *)wptr);
  708. break;
  709. }
  710. }
  711. _copy_internals_from(new_img);
  712. }
  713. Image::Format Image::get_format() const {
  714. return format;
  715. }
  716. static double _bicubic_interp_kernel(double x) {
  717. x = ABS(x);
  718. double bc = 0;
  719. if (x <= 1) {
  720. bc = (1.5 * x - 2.5) * x * x + 1;
  721. } else if (x < 2) {
  722. bc = ((-0.5 * x + 2.5) * x - 4) * x + 2;
  723. }
  724. return bc;
  725. }
  726. template <int CC, typename T>
  727. 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) {
  728. // get source image size
  729. int width = p_src_width;
  730. int height = p_src_height;
  731. double xfac = (double)width / p_dst_width;
  732. double yfac = (double)height / p_dst_height;
  733. // coordinates of source points and coefficients
  734. double ox, oy, dx, dy;
  735. int ox1, oy1, ox2, oy2;
  736. // destination pixel values
  737. // width and height decreased by 1
  738. int ymax = height - 1;
  739. int xmax = width - 1;
  740. // temporary pointer
  741. for (uint32_t y = 0; y < p_dst_height; y++) {
  742. // Y coordinates
  743. oy = (double)y * yfac - 0.5f;
  744. oy1 = (int)oy;
  745. dy = oy - (double)oy1;
  746. for (uint32_t x = 0; x < p_dst_width; x++) {
  747. // X coordinates
  748. ox = (double)x * xfac - 0.5f;
  749. ox1 = (int)ox;
  750. dx = ox - (double)ox1;
  751. // initial pixel value
  752. T *__restrict dst = ((T *)p_dst) + (y * p_dst_width + x) * CC;
  753. double color[CC];
  754. for (int i = 0; i < CC; i++) {
  755. color[i] = 0;
  756. }
  757. for (int n = -1; n < 3; n++) {
  758. // get Y coefficient
  759. [[maybe_unused]] double k1 = _bicubic_interp_kernel(dy - (double)n);
  760. oy2 = oy1 + n;
  761. if (oy2 < 0) {
  762. oy2 = 0;
  763. }
  764. if (oy2 > ymax) {
  765. oy2 = ymax;
  766. }
  767. for (int m = -1; m < 3; m++) {
  768. // get X coefficient
  769. [[maybe_unused]] double k2 = k1 * _bicubic_interp_kernel((double)m - dx);
  770. ox2 = ox1 + m;
  771. if (ox2 < 0) {
  772. ox2 = 0;
  773. }
  774. if (ox2 > xmax) {
  775. ox2 = xmax;
  776. }
  777. // get pixel of original image
  778. const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC;
  779. for (int i = 0; i < CC; i++) {
  780. if constexpr (sizeof(T) == 2) { //half float
  781. color[i] = Math::half_to_float(p[i]);
  782. } else {
  783. color[i] += p[i] * k2;
  784. }
  785. }
  786. }
  787. }
  788. for (int i = 0; i < CC; i++) {
  789. if constexpr (sizeof(T) == 1) { //byte
  790. dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255);
  791. } else if constexpr (sizeof(T) == 2) { //half float
  792. dst[i] = Math::make_half_float(color[i]);
  793. } else {
  794. dst[i] = color[i];
  795. }
  796. }
  797. }
  798. }
  799. }
  800. template <int CC, typename T>
  801. 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) {
  802. constexpr uint32_t FRAC_BITS = 8;
  803. constexpr uint32_t FRAC_LEN = (1 << FRAC_BITS);
  804. constexpr uint32_t FRAC_HALF = (FRAC_LEN >> 1);
  805. constexpr uint32_t FRAC_MASK = FRAC_LEN - 1;
  806. for (uint32_t i = 0; i < p_dst_height; i++) {
  807. // Add 0.5 in order to interpolate based on pixel center
  808. uint32_t src_yofs_up_fp = (i + 0.5) * p_src_height * FRAC_LEN / p_dst_height;
  809. // Calculate nearest src pixel center above current, and truncate to get y index
  810. uint32_t src_yofs_up = src_yofs_up_fp >= FRAC_HALF ? (src_yofs_up_fp - FRAC_HALF) >> FRAC_BITS : 0;
  811. uint32_t src_yofs_down = (src_yofs_up_fp + FRAC_HALF) >> FRAC_BITS;
  812. if (src_yofs_down >= p_src_height) {
  813. src_yofs_down = p_src_height - 1;
  814. }
  815. // Calculate distance to pixel center of src_yofs_up
  816. uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK;
  817. src_yofs_frac = src_yofs_frac >= FRAC_HALF ? src_yofs_frac - FRAC_HALF : src_yofs_frac + FRAC_HALF;
  818. uint32_t y_ofs_up = src_yofs_up * p_src_width * CC;
  819. uint32_t y_ofs_down = src_yofs_down * p_src_width * CC;
  820. for (uint32_t j = 0; j < p_dst_width; j++) {
  821. uint32_t src_xofs_left_fp = (j + 0.5) * p_src_width * FRAC_LEN / p_dst_width;
  822. uint32_t src_xofs_left = src_xofs_left_fp >= FRAC_HALF ? (src_xofs_left_fp - FRAC_HALF) >> FRAC_BITS : 0;
  823. uint32_t src_xofs_right = (src_xofs_left_fp + FRAC_HALF) >> FRAC_BITS;
  824. if (src_xofs_right >= p_src_width) {
  825. src_xofs_right = p_src_width - 1;
  826. }
  827. uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK;
  828. src_xofs_frac = src_xofs_frac >= FRAC_HALF ? src_xofs_frac - FRAC_HALF : src_xofs_frac + FRAC_HALF;
  829. src_xofs_left *= CC;
  830. src_xofs_right *= CC;
  831. for (uint32_t l = 0; l < CC; l++) {
  832. if constexpr (sizeof(T) == 1) { //uint8
  833. uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS;
  834. uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS;
  835. uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS;
  836. uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS;
  837. uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS);
  838. uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS);
  839. uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS);
  840. interp >>= FRAC_BITS;
  841. p_dst[i * p_dst_width * CC + j * CC + l] = uint8_t(interp);
  842. } else if constexpr (sizeof(T) == 2) { //half float
  843. float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS);
  844. float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS);
  845. const T *src = ((const T *)p_src);
  846. T *dst = ((T *)p_dst);
  847. float p00 = Math::half_to_float(src[y_ofs_up + src_xofs_left + l]);
  848. float p10 = Math::half_to_float(src[y_ofs_up + src_xofs_right + l]);
  849. float p01 = Math::half_to_float(src[y_ofs_down + src_xofs_left + l]);
  850. float p11 = Math::half_to_float(src[y_ofs_down + src_xofs_right + l]);
  851. float interp_up = p00 + (p10 - p00) * xofs_frac;
  852. float interp_down = p01 + (p11 - p01) * xofs_frac;
  853. float interp = interp_up + ((interp_down - interp_up) * yofs_frac);
  854. dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp);
  855. } else if constexpr (sizeof(T) == 4) { //float
  856. float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS);
  857. float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS);
  858. const T *src = ((const T *)p_src);
  859. T *dst = ((T *)p_dst);
  860. float p00 = src[y_ofs_up + src_xofs_left + l];
  861. float p10 = src[y_ofs_up + src_xofs_right + l];
  862. float p01 = src[y_ofs_down + src_xofs_left + l];
  863. float p11 = src[y_ofs_down + src_xofs_right + l];
  864. float interp_up = p00 + (p10 - p00) * xofs_frac;
  865. float interp_down = p01 + (p11 - p01) * xofs_frac;
  866. float interp = interp_up + ((interp_down - interp_up) * yofs_frac);
  867. dst[i * p_dst_width * CC + j * CC + l] = interp;
  868. }
  869. }
  870. }
  871. }
  872. }
  873. template <int CC, typename T>
  874. 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) {
  875. for (uint32_t i = 0; i < p_dst_height; i++) {
  876. uint32_t src_yofs = i * p_src_height / p_dst_height;
  877. uint32_t y_ofs = src_yofs * p_src_width * CC;
  878. for (uint32_t j = 0; j < p_dst_width; j++) {
  879. uint32_t src_xofs = j * p_src_width / p_dst_width;
  880. src_xofs *= CC;
  881. for (uint32_t l = 0; l < CC; l++) {
  882. const T *src = ((const T *)p_src);
  883. T *dst = ((T *)p_dst);
  884. T p = src[y_ofs + src_xofs + l];
  885. dst[i * p_dst_width * CC + j * CC + l] = p;
  886. }
  887. }
  888. }
  889. }
  890. #define LANCZOS_TYPE 3
  891. static float _lanczos(float p_x) {
  892. return Math::abs(p_x) >= LANCZOS_TYPE ? 0 : Math::sincn(p_x) * Math::sincn(p_x / LANCZOS_TYPE);
  893. }
  894. template <int CC, typename T>
  895. 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) {
  896. int32_t src_width = p_src_width;
  897. int32_t src_height = p_src_height;
  898. int32_t dst_height = p_dst_height;
  899. int32_t dst_width = p_dst_width;
  900. uint32_t buffer_size = src_height * dst_width * CC;
  901. float *buffer = memnew_arr(float, buffer_size); // Store the first pass in a buffer
  902. { // FIRST PASS (horizontal)
  903. float x_scale = float(src_width) / float(dst_width);
  904. float scale_factor = MAX(x_scale, 1); // A larger kernel is required only when downscaling
  905. int32_t half_kernel = LANCZOS_TYPE * scale_factor;
  906. float *kernel = memnew_arr(float, half_kernel * 2);
  907. for (int32_t buffer_x = 0; buffer_x < dst_width; buffer_x++) {
  908. // The corresponding point on the source image
  909. float src_x = (buffer_x + 0.5f) * x_scale; // Offset by 0.5 so it uses the pixel's center
  910. int32_t start_x = MAX(0, int32_t(src_x) - half_kernel + 1);
  911. int32_t end_x = MIN(src_width - 1, int32_t(src_x) + half_kernel);
  912. // Create the kernel used by all the pixels of the column
  913. for (int32_t target_x = start_x; target_x <= end_x; target_x++) {
  914. kernel[target_x - start_x] = _lanczos((target_x + 0.5f - src_x) / scale_factor);
  915. }
  916. for (int32_t buffer_y = 0; buffer_y < src_height; buffer_y++) {
  917. float pixel[CC] = { 0 };
  918. float weight = 0;
  919. for (int32_t target_x = start_x; target_x <= end_x; target_x++) {
  920. float lanczos_val = kernel[target_x - start_x];
  921. weight += lanczos_val;
  922. const T *__restrict src_data = ((const T *)p_src) + (buffer_y * src_width + target_x) * CC;
  923. for (uint32_t i = 0; i < CC; i++) {
  924. if constexpr (sizeof(T) == 2) { //half float
  925. pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val;
  926. } else {
  927. pixel[i] += src_data[i] * lanczos_val;
  928. }
  929. }
  930. }
  931. float *dst_data = ((float *)buffer) + (buffer_y * dst_width + buffer_x) * CC;
  932. for (uint32_t i = 0; i < CC; i++) {
  933. dst_data[i] = pixel[i] / weight; // Normalize the sum of all the samples
  934. }
  935. }
  936. }
  937. memdelete_arr(kernel);
  938. } // End of first pass
  939. { // SECOND PASS (vertical + result)
  940. float y_scale = float(src_height) / float(dst_height);
  941. float scale_factor = MAX(y_scale, 1);
  942. int32_t half_kernel = LANCZOS_TYPE * scale_factor;
  943. float *kernel = memnew_arr(float, half_kernel * 2);
  944. for (int32_t dst_y = 0; dst_y < dst_height; dst_y++) {
  945. float buffer_y = (dst_y + 0.5f) * y_scale;
  946. int32_t start_y = MAX(0, int32_t(buffer_y) - half_kernel + 1);
  947. int32_t end_y = MIN(src_height - 1, int32_t(buffer_y) + half_kernel);
  948. for (int32_t target_y = start_y; target_y <= end_y; target_y++) {
  949. kernel[target_y - start_y] = _lanczos((target_y + 0.5f - buffer_y) / scale_factor);
  950. }
  951. for (int32_t dst_x = 0; dst_x < dst_width; dst_x++) {
  952. float pixel[CC] = { 0 };
  953. float weight = 0;
  954. for (int32_t target_y = start_y; target_y <= end_y; target_y++) {
  955. float lanczos_val = kernel[target_y - start_y];
  956. weight += lanczos_val;
  957. float *buffer_data = ((float *)buffer) + (target_y * dst_width + dst_x) * CC;
  958. for (uint32_t i = 0; i < CC; i++) {
  959. pixel[i] += buffer_data[i] * lanczos_val;
  960. }
  961. }
  962. T *dst_data = ((T *)p_dst) + (dst_y * dst_width + dst_x) * CC;
  963. for (uint32_t i = 0; i < CC; i++) {
  964. pixel[i] /= weight;
  965. if constexpr (sizeof(T) == 1) { //byte
  966. dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255);
  967. } else if constexpr (sizeof(T) == 2) { //half float
  968. dst_data[i] = Math::make_half_float(pixel[i]);
  969. } else { // float
  970. dst_data[i] = pixel[i];
  971. }
  972. }
  973. }
  974. }
  975. memdelete_arr(kernel);
  976. } // End of second pass
  977. memdelete_arr(buffer);
  978. }
  979. 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) {
  980. uint16_t alpha = MIN((uint16_t)(p_alpha * 256.0f), 256);
  981. for (uint32_t i = 0; i < p_width * p_height * p_pixel_size; i++) {
  982. p_dst[i] = (p_dst[i] * (256 - alpha) + p_src[i] * alpha) >> 8;
  983. }
  984. }
  985. bool Image::is_size_po2() const {
  986. return is_power_of_2(width) && is_power_of_2(height);
  987. }
  988. void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) {
  989. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats.");
  990. int w = next_power_of_2(width);
  991. int h = next_power_of_2(height);
  992. if (p_square) {
  993. w = h = MAX(w, h);
  994. }
  995. if (w == width && h == height) {
  996. if (!p_square || w == h) {
  997. return; //nothing to do
  998. }
  999. }
  1000. resize(w, h, p_interpolation);
  1001. }
  1002. void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
  1003. ERR_FAIL_COND_MSG(data.is_empty(), "Cannot resize image before creating it, use set_data() first.");
  1004. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats.");
  1005. bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */;
  1006. ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0.");
  1007. ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0.");
  1008. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, vformat("Image width cannot be greater than %d pixels.", MAX_WIDTH));
  1009. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, vformat("Image height cannot be greater than %d pixels.", MAX_HEIGHT));
  1010. ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, vformat("Too many pixels for image, maximum is %d pixels.", MAX_PIXELS));
  1011. if (p_width == width && p_height == height) {
  1012. return;
  1013. }
  1014. Image dst(p_width, p_height, false, format);
  1015. // Setup mipmap-aware scaling
  1016. Image dst2;
  1017. int mip1 = 0;
  1018. int mip2 = 0;
  1019. float mip1_weight = 0;
  1020. if (mipmap_aware) {
  1021. float avg_scale = ((float)p_width / width + (float)p_height / height) * 0.5f;
  1022. if (avg_scale >= 1.0f) {
  1023. mipmap_aware = false;
  1024. } else {
  1025. float level = Math::log(1.0f / avg_scale) / Math::log(2.0f);
  1026. mip1 = CLAMP((int)Math::floor(level), 0, get_mipmap_count());
  1027. mip2 = CLAMP((int)Math::ceil(level), 0, get_mipmap_count());
  1028. mip1_weight = 1.0f - (level - mip1);
  1029. }
  1030. }
  1031. bool interpolate_mipmaps = mipmap_aware && mip1 != mip2;
  1032. if (interpolate_mipmaps) {
  1033. dst2.initialize_data(p_width, p_height, false, format);
  1034. }
  1035. bool had_mipmaps = mipmaps;
  1036. if (interpolate_mipmaps && !had_mipmaps) {
  1037. generate_mipmaps();
  1038. }
  1039. // --
  1040. const uint8_t *r = data.ptr();
  1041. const unsigned char *r_ptr = r;
  1042. uint8_t *w = dst.data.ptrw();
  1043. unsigned char *w_ptr = w;
  1044. switch (p_interpolation) {
  1045. case INTERPOLATE_NEAREST: {
  1046. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  1047. switch (get_format_pixel_size(format)) {
  1048. case 1:
  1049. _scale_nearest<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1050. break;
  1051. case 2:
  1052. _scale_nearest<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1053. break;
  1054. case 3:
  1055. _scale_nearest<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1056. break;
  1057. case 4:
  1058. _scale_nearest<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1059. break;
  1060. }
  1061. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  1062. switch (get_format_pixel_size(format)) {
  1063. case 4:
  1064. _scale_nearest<1, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1065. break;
  1066. case 8:
  1067. _scale_nearest<2, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1068. break;
  1069. case 12:
  1070. _scale_nearest<3, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1071. break;
  1072. case 16:
  1073. _scale_nearest<4, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1074. break;
  1075. }
  1076. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  1077. switch (get_format_pixel_size(format)) {
  1078. case 2:
  1079. _scale_nearest<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1080. break;
  1081. case 4:
  1082. _scale_nearest<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1083. break;
  1084. case 6:
  1085. _scale_nearest<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1086. break;
  1087. case 8:
  1088. _scale_nearest<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1089. break;
  1090. }
  1091. }
  1092. } break;
  1093. case INTERPOLATE_BILINEAR:
  1094. case INTERPOLATE_TRILINEAR: {
  1095. for (int i = 0; i < 2; ++i) {
  1096. int src_width;
  1097. int src_height;
  1098. const unsigned char *src_ptr;
  1099. if (!mipmap_aware) {
  1100. if (i == 0) {
  1101. // Standard behavior
  1102. src_width = width;
  1103. src_height = height;
  1104. src_ptr = r_ptr;
  1105. } else {
  1106. // No need for a second iteration
  1107. break;
  1108. }
  1109. } else {
  1110. if (i == 0) {
  1111. // Read from the first mipmap that will be interpolated
  1112. // (if both levels are the same, we will not interpolate, but at least we'll sample from the right level)
  1113. int64_t offs;
  1114. _get_mipmap_offset_and_size(mip1, offs, src_width, src_height);
  1115. src_ptr = r_ptr + offs;
  1116. } else if (!interpolate_mipmaps) {
  1117. // No need generate a second image
  1118. break;
  1119. } else {
  1120. // Switch to read from the second mipmap that will be interpolated
  1121. int64_t offs;
  1122. _get_mipmap_offset_and_size(mip2, offs, src_width, src_height);
  1123. src_ptr = r_ptr + offs;
  1124. // Switch to write to the second destination image
  1125. w = dst2.data.ptrw();
  1126. w_ptr = w;
  1127. }
  1128. }
  1129. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  1130. switch (get_format_pixel_size(format)) {
  1131. case 1:
  1132. _scale_bilinear<1, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1133. break;
  1134. case 2:
  1135. _scale_bilinear<2, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1136. break;
  1137. case 3:
  1138. _scale_bilinear<3, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1139. break;
  1140. case 4:
  1141. _scale_bilinear<4, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1142. break;
  1143. }
  1144. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  1145. switch (get_format_pixel_size(format)) {
  1146. case 4:
  1147. _scale_bilinear<1, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1148. break;
  1149. case 8:
  1150. _scale_bilinear<2, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1151. break;
  1152. case 12:
  1153. _scale_bilinear<3, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1154. break;
  1155. case 16:
  1156. _scale_bilinear<4, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1157. break;
  1158. }
  1159. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  1160. switch (get_format_pixel_size(format)) {
  1161. case 2:
  1162. _scale_bilinear<1, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1163. break;
  1164. case 4:
  1165. _scale_bilinear<2, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1166. break;
  1167. case 6:
  1168. _scale_bilinear<3, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1169. break;
  1170. case 8:
  1171. _scale_bilinear<4, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height);
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. if (interpolate_mipmaps) {
  1177. // Switch to read again from the first scaled mipmap to overlay it over the second
  1178. r = dst.data.ptr();
  1179. _overlay(r, w, mip1_weight, p_width, p_height, get_format_pixel_size(format));
  1180. }
  1181. } break;
  1182. case INTERPOLATE_CUBIC: {
  1183. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  1184. switch (get_format_pixel_size(format)) {
  1185. case 1:
  1186. _scale_cubic<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1187. break;
  1188. case 2:
  1189. _scale_cubic<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1190. break;
  1191. case 3:
  1192. _scale_cubic<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1193. break;
  1194. case 4:
  1195. _scale_cubic<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1196. break;
  1197. }
  1198. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  1199. switch (get_format_pixel_size(format)) {
  1200. case 4:
  1201. _scale_cubic<1, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1202. break;
  1203. case 8:
  1204. _scale_cubic<2, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1205. break;
  1206. case 12:
  1207. _scale_cubic<3, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1208. break;
  1209. case 16:
  1210. _scale_cubic<4, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1211. break;
  1212. }
  1213. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  1214. switch (get_format_pixel_size(format)) {
  1215. case 2:
  1216. _scale_cubic<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1217. break;
  1218. case 4:
  1219. _scale_cubic<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1220. break;
  1221. case 6:
  1222. _scale_cubic<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1223. break;
  1224. case 8:
  1225. _scale_cubic<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1226. break;
  1227. }
  1228. }
  1229. } break;
  1230. case INTERPOLATE_LANCZOS: {
  1231. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  1232. switch (get_format_pixel_size(format)) {
  1233. case 1:
  1234. _scale_lanczos<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1235. break;
  1236. case 2:
  1237. _scale_lanczos<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1238. break;
  1239. case 3:
  1240. _scale_lanczos<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1241. break;
  1242. case 4:
  1243. _scale_lanczos<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1244. break;
  1245. }
  1246. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  1247. switch (get_format_pixel_size(format)) {
  1248. case 4:
  1249. _scale_lanczos<1, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1250. break;
  1251. case 8:
  1252. _scale_lanczos<2, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1253. break;
  1254. case 12:
  1255. _scale_lanczos<3, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1256. break;
  1257. case 16:
  1258. _scale_lanczos<4, float>(r_ptr, w_ptr, width, height, p_width, p_height);
  1259. break;
  1260. }
  1261. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  1262. switch (get_format_pixel_size(format)) {
  1263. case 2:
  1264. _scale_lanczos<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1265. break;
  1266. case 4:
  1267. _scale_lanczos<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1268. break;
  1269. case 6:
  1270. _scale_lanczos<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1271. break;
  1272. case 8:
  1273. _scale_lanczos<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height);
  1274. break;
  1275. }
  1276. }
  1277. } break;
  1278. }
  1279. if (interpolate_mipmaps) {
  1280. dst._copy_internals_from(dst2);
  1281. }
  1282. if (had_mipmaps) {
  1283. dst.generate_mipmaps();
  1284. }
  1285. _copy_internals_from(dst);
  1286. }
  1287. void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) {
  1288. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot crop in compressed or custom image formats.");
  1289. ERR_FAIL_COND_MSG(p_x < 0, "Start x position cannot be smaller than 0.");
  1290. ERR_FAIL_COND_MSG(p_y < 0, "Start y position cannot be smaller than 0.");
  1291. ERR_FAIL_COND_MSG(p_width <= 0, "Width of image must be greater than 0.");
  1292. ERR_FAIL_COND_MSG(p_height <= 0, "Height of image must be greater than 0.");
  1293. ERR_FAIL_COND_MSG(p_x + p_width > MAX_WIDTH, vformat("End x position cannot be greater than %d.", MAX_WIDTH));
  1294. ERR_FAIL_COND_MSG(p_y + p_height > MAX_HEIGHT, vformat("End y position cannot be greater than %d.", MAX_HEIGHT));
  1295. /* to save memory, cropping should be done in-place, however, since this function
  1296. will most likely either not be used much, or in critical areas, for now it won't, because
  1297. it's a waste of time. */
  1298. if (p_width == width && p_height == height && p_x == 0 && p_y == 0) {
  1299. return;
  1300. }
  1301. uint8_t pdata[16]; //largest is 16
  1302. uint32_t pixel_size = get_format_pixel_size(format);
  1303. Image dst(p_width, p_height, false, format);
  1304. {
  1305. const uint8_t *r = data.ptr();
  1306. uint8_t *w = dst.data.ptrw();
  1307. int m_h = p_y + p_height;
  1308. int m_w = p_x + p_width;
  1309. for (int y = p_y; y < m_h; y++) {
  1310. for (int x = p_x; x < m_w; x++) {
  1311. if ((x >= width || y >= height)) {
  1312. for (uint32_t i = 0; i < pixel_size; i++) {
  1313. pdata[i] = 0;
  1314. }
  1315. } else {
  1316. _get_pixelb(x, y, pixel_size, r, pdata);
  1317. }
  1318. dst._put_pixelb(x - p_x, y - p_y, pixel_size, w, pdata);
  1319. }
  1320. }
  1321. }
  1322. if (has_mipmaps()) {
  1323. dst.generate_mipmaps();
  1324. }
  1325. _copy_internals_from(dst);
  1326. }
  1327. void Image::crop(int p_width, int p_height) {
  1328. crop_from_point(0, 0, p_width, p_height);
  1329. }
  1330. void Image::rotate_90(ClockDirection p_direction) {
  1331. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot rotate in compressed or custom image formats.");
  1332. ERR_FAIL_COND_MSG(width <= 0, vformat("The Image width specified (%d pixels) must be greater than 0 pixels.", width));
  1333. ERR_FAIL_COND_MSG(height <= 0, vformat("The Image height specified (%d pixels) must be greater than 0 pixels.", height));
  1334. bool used_mipmaps = has_mipmaps();
  1335. if (used_mipmaps) {
  1336. clear_mipmaps();
  1337. }
  1338. // In-place 90 degrees rotation by following the permutation cycles.
  1339. {
  1340. // Explanation by example (clockwise):
  1341. //
  1342. // abc da
  1343. // def -> eb
  1344. // fc
  1345. //
  1346. // In memory:
  1347. // 012345 012345
  1348. // abcdef -> daebfc
  1349. //
  1350. // Permutation cycles:
  1351. // (0 --a--> 1 --b--> 3 --d--> 0)
  1352. // (2 --c--> 5 --f--> 4 --e--> 2)
  1353. //
  1354. // Applying cycles (backwards):
  1355. // 0->s s=a (store)
  1356. // 3->0 abcdef -> dbcdef
  1357. // 1->3 dbcdef -> dbcbef
  1358. // s->1 dbcbef -> dacbef
  1359. //
  1360. // 2->s s=c
  1361. // 4->2 dacbef -> daebef
  1362. // 5->4 daebef -> daebff
  1363. // s->5 daebff -> daebfc
  1364. const int w = width;
  1365. const int h = height;
  1366. const int size = w * h;
  1367. uint8_t *data_ptr = data.ptrw();
  1368. uint32_t pixel_size = get_format_pixel_size(format);
  1369. uint8_t single_pixel_buffer[16];
  1370. #define PREV_INDEX_IN_CYCLE(index) (p_direction == CLOCKWISE) ? ((h - 1 - (index % h)) * w + (index / h)) : ((index % h) * w + (w - 1 - (index / h)))
  1371. if (w == h) { // Square case, 4-length cycles only (plus irrelevant thus skipped 1-length cycle in the middle for odd-sized squares).
  1372. for (int y = 0; y < h / 2; y++) {
  1373. for (int x = 0; x < (w + 1) / 2; x++) {
  1374. int current = y * w + x;
  1375. memcpy(single_pixel_buffer, data_ptr + current * pixel_size, pixel_size);
  1376. for (int i = 0; i < 3; i++) {
  1377. int prev = PREV_INDEX_IN_CYCLE(current);
  1378. memcpy(data_ptr + current * pixel_size, data_ptr + prev * pixel_size, pixel_size);
  1379. current = prev;
  1380. }
  1381. memcpy(data_ptr + current * pixel_size, single_pixel_buffer, pixel_size);
  1382. }
  1383. }
  1384. } else { // Rectangular case (w != h), kinda unpredictable cycles.
  1385. int permuted_pixels_count = 0;
  1386. for (int i = 0; i < size; i++) {
  1387. int prev = PREV_INDEX_IN_CYCLE(i);
  1388. if (prev == i) {
  1389. // 1-length cycle, pixel remains at the same index.
  1390. permuted_pixels_count++;
  1391. continue;
  1392. }
  1393. // Check whether we already processed this cycle.
  1394. // We iterate over it and if we'll find an index smaller than `i` then we already
  1395. // processed this cycle because we always start at the smallest index in the cycle.
  1396. // TODO: Improve this naive approach, can be done better.
  1397. while (prev > i) {
  1398. prev = PREV_INDEX_IN_CYCLE(prev);
  1399. }
  1400. if (prev < i) {
  1401. continue;
  1402. }
  1403. // Save the in-cycle pixel with the smallest index (`i`).
  1404. memcpy(single_pixel_buffer, data_ptr + i * pixel_size, pixel_size);
  1405. // Overwrite pixels one by one by the preceding pixel in the cycle.
  1406. int current = i;
  1407. prev = PREV_INDEX_IN_CYCLE(current);
  1408. while (prev != i) {
  1409. memcpy(data_ptr + current * pixel_size, data_ptr + prev * pixel_size, pixel_size);
  1410. permuted_pixels_count++;
  1411. current = prev;
  1412. prev = PREV_INDEX_IN_CYCLE(current);
  1413. };
  1414. // Overwrite the remaining pixel in the cycle by the saved pixel with the smallest index.
  1415. memcpy(data_ptr + current * pixel_size, single_pixel_buffer, pixel_size);
  1416. permuted_pixels_count++;
  1417. if (permuted_pixels_count == size) {
  1418. break;
  1419. }
  1420. }
  1421. width = h;
  1422. height = w;
  1423. }
  1424. #undef PREV_INDEX_IN_CYCLE
  1425. }
  1426. if (used_mipmaps) {
  1427. generate_mipmaps();
  1428. }
  1429. }
  1430. void Image::rotate_180() {
  1431. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot rotate in compressed or custom image formats.");
  1432. ERR_FAIL_COND_MSG(width <= 0, vformat("The Image width specified (%d pixels) must be greater than 0 pixels.", width));
  1433. ERR_FAIL_COND_MSG(height <= 0, vformat("The Image height specified (%d pixels) must be greater than 0 pixels.", height));
  1434. bool used_mipmaps = has_mipmaps();
  1435. if (used_mipmaps) {
  1436. clear_mipmaps();
  1437. }
  1438. {
  1439. uint8_t *data_ptr = data.ptrw();
  1440. uint32_t pixel_size = get_format_pixel_size(format);
  1441. uint8_t single_pixel_buffer[16];
  1442. uint8_t *from_begin_ptr = data_ptr;
  1443. uint8_t *from_end_ptr = data_ptr + (width * height - 1) * pixel_size;
  1444. while (from_begin_ptr < from_end_ptr) {
  1445. memcpy(single_pixel_buffer, from_begin_ptr, pixel_size);
  1446. memcpy(from_begin_ptr, from_end_ptr, pixel_size);
  1447. memcpy(from_end_ptr, single_pixel_buffer, pixel_size);
  1448. from_begin_ptr += pixel_size;
  1449. from_end_ptr -= pixel_size;
  1450. }
  1451. }
  1452. if (used_mipmaps) {
  1453. generate_mipmaps();
  1454. }
  1455. }
  1456. void Image::flip_y() {
  1457. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_y in compressed or custom image formats.");
  1458. bool used_mipmaps = has_mipmaps();
  1459. if (used_mipmaps) {
  1460. clear_mipmaps();
  1461. }
  1462. {
  1463. uint8_t *w = data.ptrw();
  1464. uint8_t up[16];
  1465. uint8_t down[16];
  1466. uint32_t pixel_size = get_format_pixel_size(format);
  1467. for (int y = 0; y < height / 2; y++) {
  1468. for (int x = 0; x < width; x++) {
  1469. _get_pixelb(x, y, pixel_size, w, up);
  1470. _get_pixelb(x, height - y - 1, pixel_size, w, down);
  1471. _put_pixelb(x, height - y - 1, pixel_size, w, up);
  1472. _put_pixelb(x, y, pixel_size, w, down);
  1473. }
  1474. }
  1475. }
  1476. if (used_mipmaps) {
  1477. generate_mipmaps();
  1478. }
  1479. }
  1480. void Image::flip_x() {
  1481. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_x in compressed or custom image formats.");
  1482. bool used_mipmaps = has_mipmaps();
  1483. if (used_mipmaps) {
  1484. clear_mipmaps();
  1485. }
  1486. {
  1487. uint8_t *w = data.ptrw();
  1488. uint8_t up[16];
  1489. uint8_t down[16];
  1490. uint32_t pixel_size = get_format_pixel_size(format);
  1491. for (int y = 0; y < height; y++) {
  1492. for (int x = 0; x < width / 2; x++) {
  1493. _get_pixelb(x, y, pixel_size, w, up);
  1494. _get_pixelb(width - x - 1, y, pixel_size, w, down);
  1495. _put_pixelb(width - x - 1, y, pixel_size, w, up);
  1496. _put_pixelb(x, y, pixel_size, w, down);
  1497. }
  1498. }
  1499. }
  1500. if (used_mipmaps) {
  1501. generate_mipmaps();
  1502. }
  1503. }
  1504. // Get mipmap size and offset.
  1505. int64_t Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps, int *r_mm_width, int *r_mm_height) {
  1506. // Data offset in mipmaps (including the original texture).
  1507. int64_t size = 0;
  1508. int w = p_width;
  1509. int h = p_height;
  1510. // Current mipmap index in the loop below. p_mipmaps is the target mipmap index.
  1511. // In this function, mipmap 0 represents the first mipmap instead of the original texture.
  1512. int mm = 0;
  1513. int pixsize = get_format_pixel_size(p_format);
  1514. int pixshift = get_format_pixel_rshift(p_format);
  1515. int block = get_format_block_size(p_format);
  1516. // Technically, you can still compress up to 1 px no matter the format, so commenting this.
  1517. //int minw, minh;
  1518. //get_format_min_pixel_size(p_format, minw, minh);
  1519. int minw = 1, minh = 1;
  1520. while (true) {
  1521. int bw = w % block != 0 ? w + (block - w % block) : w;
  1522. int bh = h % block != 0 ? h + (block - h % block) : h;
  1523. int64_t s = bw * bh;
  1524. s *= pixsize;
  1525. s >>= pixshift;
  1526. size += s;
  1527. if (p_mipmaps >= 0) {
  1528. w = MAX(minw, w >> 1);
  1529. h = MAX(minh, h >> 1);
  1530. } else {
  1531. if (w == minw && h == minh) {
  1532. break;
  1533. }
  1534. w = MAX(minw, w >> 1);
  1535. h = MAX(minh, h >> 1);
  1536. }
  1537. // Set mipmap size.
  1538. if (r_mm_width) {
  1539. *r_mm_width = w;
  1540. }
  1541. if (r_mm_height) {
  1542. *r_mm_height = h;
  1543. }
  1544. // Reach target mipmap.
  1545. if (p_mipmaps >= 0 && mm == p_mipmaps) {
  1546. break;
  1547. }
  1548. mm++;
  1549. }
  1550. r_mipmaps = mm;
  1551. return size;
  1552. }
  1553. bool Image::_can_modify(Format p_format) const {
  1554. return !Image::is_format_compressed(p_format);
  1555. }
  1556. template <typename Component, int CC, bool renormalize,
  1557. void (*average_func)(Component &, const Component &, const Component &, const Component &, const Component &),
  1558. void (*renormalize_func)(Component *)>
  1559. static void _generate_po2_mipmap(const Component *p_src, Component *p_dst, uint32_t p_width, uint32_t p_height) {
  1560. // Fast power of 2 mipmap generation.
  1561. uint32_t dst_w = MAX(p_width >> 1, 1u);
  1562. uint32_t dst_h = MAX(p_height >> 1, 1u);
  1563. int right_step = (p_width == 1) ? 0 : CC;
  1564. int down_step = (p_height == 1) ? 0 : (p_width * CC);
  1565. for (uint32_t i = 0; i < dst_h; i++) {
  1566. const Component *rup_ptr = &p_src[i * 2 * down_step];
  1567. const Component *rdown_ptr = rup_ptr + down_step;
  1568. Component *dst_ptr = &p_dst[i * dst_w * CC];
  1569. uint32_t count = dst_w;
  1570. while (count) {
  1571. count--;
  1572. for (int j = 0; j < CC; j++) {
  1573. average_func(dst_ptr[j], rup_ptr[j], rup_ptr[j + right_step], rdown_ptr[j], rdown_ptr[j + right_step]);
  1574. }
  1575. if (renormalize) {
  1576. renormalize_func(dst_ptr);
  1577. }
  1578. dst_ptr += CC;
  1579. rup_ptr += right_step * 2;
  1580. rdown_ptr += right_step * 2;
  1581. }
  1582. }
  1583. }
  1584. void Image::_generate_mipmap_from_format(Image::Format p_format, const uint8_t *p_src, uint8_t *p_dst, uint32_t p_width, uint32_t p_height, bool p_renormalize) {
  1585. const float *src_float = reinterpret_cast<const float *>(p_src);
  1586. float *dst_float = reinterpret_cast<float *>(p_dst);
  1587. const uint16_t *src_u16 = reinterpret_cast<const uint16_t *>(p_src);
  1588. uint16_t *dst_u16 = reinterpret_cast<uint16_t *>(p_dst);
  1589. const uint32_t *src_u32 = reinterpret_cast<const uint32_t *>(p_src);
  1590. uint32_t *dst_u32 = reinterpret_cast<uint32_t *>(p_dst);
  1591. switch (p_format) {
  1592. case Image::FORMAT_L8:
  1593. case Image::FORMAT_R8:
  1594. _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1595. break;
  1596. case Image::FORMAT_LA8:
  1597. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1598. break;
  1599. case Image::FORMAT_RG8:
  1600. _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1601. break;
  1602. case Image::FORMAT_RGB8: {
  1603. if (p_renormalize) {
  1604. _generate_po2_mipmap<uint8_t, 3, true, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1605. } else {
  1606. _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1607. }
  1608. } break;
  1609. case Image::FORMAT_RGBA8: {
  1610. if (p_renormalize) {
  1611. _generate_po2_mipmap<uint8_t, 4, true, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1612. } else {
  1613. _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(p_src, p_dst, p_width, p_height);
  1614. }
  1615. } break;
  1616. case Image::FORMAT_RF:
  1617. _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(src_float, dst_float, p_width, p_height);
  1618. break;
  1619. case Image::FORMAT_RGF:
  1620. _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(src_float, dst_float, p_width, p_height);
  1621. break;
  1622. case Image::FORMAT_RGBF: {
  1623. if (p_renormalize) {
  1624. _generate_po2_mipmap<float, 3, true, Image::average_4_float, Image::renormalize_float>(src_float, dst_float, p_width, p_height);
  1625. } else {
  1626. _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(src_float, dst_float, p_width, p_height);
  1627. }
  1628. } break;
  1629. case Image::FORMAT_RGBAF: {
  1630. if (p_renormalize) {
  1631. _generate_po2_mipmap<float, 4, true, Image::average_4_float, Image::renormalize_float>(src_float, dst_float, p_width, p_height);
  1632. } else {
  1633. _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(src_float, dst_float, p_width, p_height);
  1634. }
  1635. } break;
  1636. case Image::FORMAT_RH:
  1637. _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(src_u16, dst_u16, p_width, p_height);
  1638. break;
  1639. case Image::FORMAT_RGH:
  1640. _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(src_u16, dst_u16, p_width, p_height);
  1641. break;
  1642. case Image::FORMAT_RGBH: {
  1643. if (p_renormalize) {
  1644. _generate_po2_mipmap<uint16_t, 3, true, Image::average_4_half, Image::renormalize_half>(src_u16, dst_u16, p_width, p_height);
  1645. } else {
  1646. _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(src_u16, dst_u16, p_width, p_height);
  1647. }
  1648. } break;
  1649. case Image::FORMAT_RGBAH: {
  1650. if (p_renormalize) {
  1651. _generate_po2_mipmap<uint16_t, 4, true, Image::average_4_half, Image::renormalize_half>(src_u16, dst_u16, p_width, p_height);
  1652. } else {
  1653. _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(src_u16, dst_u16, p_width, p_height);
  1654. }
  1655. } break;
  1656. case Image::FORMAT_RGBE9995:
  1657. _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(src_u32, dst_u32, p_width, p_height);
  1658. break;
  1659. default:
  1660. return;
  1661. }
  1662. }
  1663. void Image::shrink_x2() {
  1664. ERR_FAIL_COND(data.is_empty());
  1665. Vector<uint8_t> new_data;
  1666. if (mipmaps) {
  1667. // Just use the lower mipmap as base and copy all.
  1668. int64_t ofs = get_mipmap_offset(1);
  1669. int64_t new_size = data.size() - ofs;
  1670. new_data.resize(new_size);
  1671. ERR_FAIL_COND(new_data.is_empty());
  1672. memcpy(new_data.ptrw(), data.ptr() + ofs, new_size);
  1673. } else {
  1674. // Generate a mipmap and replace the original.
  1675. ERR_FAIL_COND(!_can_modify(format));
  1676. new_data.resize((width / 2) * (height / 2) * get_format_pixel_size(format));
  1677. ERR_FAIL_COND(data.is_empty() || new_data.is_empty());
  1678. _generate_mipmap_from_format(format, data.ptr(), new_data.ptrw(), width, height, false);
  1679. }
  1680. width = MAX(width / 2, 1);
  1681. height = MAX(height / 2, 1);
  1682. data = new_data;
  1683. }
  1684. void Image::normalize() {
  1685. bool used_mipmaps = has_mipmaps();
  1686. if (used_mipmaps) {
  1687. clear_mipmaps();
  1688. }
  1689. for (int y = 0; y < height; y++) {
  1690. for (int x = 0; x < width; x++) {
  1691. Color c = get_pixel(x, y);
  1692. Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0);
  1693. v.normalize();
  1694. c.r = v.x * 0.5 + 0.5;
  1695. c.g = v.y * 0.5 + 0.5;
  1696. c.b = v.z * 0.5 + 0.5;
  1697. set_pixel(x, y, c);
  1698. }
  1699. }
  1700. if (used_mipmaps) {
  1701. generate_mipmaps(true);
  1702. }
  1703. }
  1704. Error Image::generate_mipmaps(bool p_renormalize) {
  1705. ERR_FAIL_COND_V_MSG(!_can_modify(format), ERR_UNAVAILABLE, "Cannot generate mipmaps in compressed or custom image formats.");
  1706. ERR_FAIL_COND_V_MSG(format == FORMAT_RGBA4444, ERR_UNAVAILABLE, "Cannot generate mipmaps from RGBA4444 format.");
  1707. ERR_FAIL_COND_V_MSG(width == 0 || height == 0, ERR_UNCONFIGURED, "Cannot generate mipmaps with width or height equal to 0.");
  1708. int gen_mipmap_count;
  1709. int64_t size = _get_dst_image_size(width, height, format, gen_mipmap_count);
  1710. data.resize(size);
  1711. uint8_t *wp = data.ptrw();
  1712. int prev_ofs = 0;
  1713. int prev_h = height;
  1714. int prev_w = width;
  1715. for (int i = 1; i <= gen_mipmap_count; i++) {
  1716. int64_t ofs;
  1717. int w, h;
  1718. _get_mipmap_offset_and_size(i, ofs, w, h);
  1719. _generate_mipmap_from_format(format, wp + prev_ofs, wp + ofs, prev_w, prev_h, p_renormalize);
  1720. prev_ofs = ofs;
  1721. prev_w = w;
  1722. prev_h = h;
  1723. }
  1724. mipmaps = true;
  1725. return OK;
  1726. }
  1727. Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map) {
  1728. LocalVector<double> normal_sat_vec; //summed area table
  1729. int normal_w = 0, normal_h = 0;
  1730. ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->is_empty(), ERR_INVALID_PARAMETER, "Must provide a valid normal map for roughness mipmaps");
  1731. Ref<Image> nm = p_normal_map->duplicate();
  1732. if (nm->is_compressed()) {
  1733. nm->decompress();
  1734. }
  1735. normal_w = nm->get_width();
  1736. normal_h = nm->get_height();
  1737. normal_sat_vec.resize(normal_w * normal_h * 3);
  1738. double *normal_sat = normal_sat_vec.ptr();
  1739. // Create summed area table.
  1740. for (int y = 0; y < normal_h; y++) {
  1741. double line_sum[3] = { 0, 0, 0 };
  1742. for (int x = 0; x < normal_w; x++) {
  1743. double normal[3];
  1744. Color color = nm->get_pixel(x, y);
  1745. normal[0] = color.r * 2.0 - 1.0;
  1746. normal[1] = color.g * 2.0 - 1.0;
  1747. normal[2] = Math::sqrt(MAX(0.0, 1.0 - (normal[0] * normal[0] + normal[1] * normal[1]))); //reconstruct if missing
  1748. line_sum[0] += normal[0];
  1749. line_sum[1] += normal[1];
  1750. line_sum[2] += normal[2];
  1751. uint32_t ofs = (y * normal_w + x) * 3;
  1752. normal_sat[ofs + 0] = line_sum[0];
  1753. normal_sat[ofs + 1] = line_sum[1];
  1754. normal_sat[ofs + 2] = line_sum[2];
  1755. if (y > 0) {
  1756. uint32_t prev_ofs = ((y - 1) * normal_w + x) * 3;
  1757. normal_sat[ofs + 0] += normal_sat[prev_ofs + 0];
  1758. normal_sat[ofs + 1] += normal_sat[prev_ofs + 1];
  1759. normal_sat[ofs + 2] += normal_sat[prev_ofs + 2];
  1760. }
  1761. }
  1762. }
  1763. int mmcount;
  1764. _get_dst_image_size(width, height, format, mmcount);
  1765. uint8_t *base_ptr = data.ptrw();
  1766. for (int i = 1; i <= mmcount; i++) {
  1767. int64_t ofs;
  1768. int w, h;
  1769. _get_mipmap_offset_and_size(i, ofs, w, h);
  1770. uint8_t *ptr = &base_ptr[ofs];
  1771. for (int x = 0; x < w; x++) {
  1772. for (int y = 0; y < h; y++) {
  1773. int from_x = x * normal_w / w;
  1774. int from_y = y * normal_h / h;
  1775. int to_x = (x + 1) * normal_w / w;
  1776. int to_y = (y + 1) * normal_h / h;
  1777. to_x = MIN(to_x - 1, normal_w);
  1778. to_y = MIN(to_y - 1, normal_h);
  1779. int size_x = (to_x - from_x) + 1;
  1780. int size_y = (to_y - from_y) + 1;
  1781. //summed area table version (much faster)
  1782. double avg[3] = { 0, 0, 0 };
  1783. if (from_x > 0 && from_y > 0) {
  1784. uint32_t tofs = ((from_y - 1) * normal_w + (from_x - 1)) * 3;
  1785. avg[0] += normal_sat[tofs + 0];
  1786. avg[1] += normal_sat[tofs + 1];
  1787. avg[2] += normal_sat[tofs + 2];
  1788. }
  1789. if (from_y > 0 && to_x > 0) {
  1790. uint32_t tofs = ((from_y - 1) * normal_w + to_x) * 3;
  1791. avg[0] -= normal_sat[tofs + 0];
  1792. avg[1] -= normal_sat[tofs + 1];
  1793. avg[2] -= normal_sat[tofs + 2];
  1794. }
  1795. if (from_x > 0 && to_y > 0) {
  1796. uint32_t tofs = (to_y * normal_w + (from_x - 1)) * 3;
  1797. avg[0] -= normal_sat[tofs + 0];
  1798. avg[1] -= normal_sat[tofs + 1];
  1799. avg[2] -= normal_sat[tofs + 2];
  1800. }
  1801. if (to_y > 0 && to_x > 0) {
  1802. uint32_t tofs = (to_y * normal_w + to_x) * 3;
  1803. avg[0] += normal_sat[tofs + 0];
  1804. avg[1] += normal_sat[tofs + 1];
  1805. avg[2] += normal_sat[tofs + 2];
  1806. }
  1807. double div = double(size_x * size_y);
  1808. Vector3 vec(avg[0] / div, avg[1] / div, avg[2] / div);
  1809. float r = vec.length();
  1810. int pixel_ofs = y * w + x;
  1811. Color c = _get_color_at_ofs(ptr, pixel_ofs);
  1812. float roughness = 0;
  1813. switch (p_roughness_channel) {
  1814. case ROUGHNESS_CHANNEL_R: {
  1815. roughness = c.r;
  1816. } break;
  1817. case ROUGHNESS_CHANNEL_G: {
  1818. roughness = c.g;
  1819. } break;
  1820. case ROUGHNESS_CHANNEL_B: {
  1821. roughness = c.b;
  1822. } break;
  1823. case ROUGHNESS_CHANNEL_L: {
  1824. roughness = c.get_v();
  1825. } break;
  1826. case ROUGHNESS_CHANNEL_A: {
  1827. roughness = c.a;
  1828. } break;
  1829. }
  1830. float variance = 0;
  1831. if (r < 1.0f) {
  1832. float r2 = r * r;
  1833. float kappa = (3.0f * r - r * r2) / (1.0f - r2);
  1834. variance = 0.25f / kappa;
  1835. }
  1836. float threshold = 0.4;
  1837. roughness = Math::sqrt(roughness * roughness + MIN(3.0f * variance, threshold * threshold));
  1838. switch (p_roughness_channel) {
  1839. case ROUGHNESS_CHANNEL_R: {
  1840. c.r = roughness;
  1841. } break;
  1842. case ROUGHNESS_CHANNEL_G: {
  1843. c.g = roughness;
  1844. } break;
  1845. case ROUGHNESS_CHANNEL_B: {
  1846. c.b = roughness;
  1847. } break;
  1848. case ROUGHNESS_CHANNEL_L: {
  1849. c.r = roughness;
  1850. c.g = roughness;
  1851. c.b = roughness;
  1852. } break;
  1853. case ROUGHNESS_CHANNEL_A: {
  1854. c.a = roughness;
  1855. } break;
  1856. }
  1857. _set_color_at_ofs(ptr, pixel_ofs, c);
  1858. }
  1859. }
  1860. }
  1861. return OK;
  1862. }
  1863. void Image::clear_mipmaps() {
  1864. if (!mipmaps) {
  1865. return;
  1866. }
  1867. if (is_empty()) {
  1868. return;
  1869. }
  1870. int64_t ofs;
  1871. int w, h;
  1872. _get_mipmap_offset_and_size(1, ofs, w, h);
  1873. data.resize(ofs);
  1874. mipmaps = false;
  1875. }
  1876. bool Image::is_empty() const {
  1877. return (data.size() == 0);
  1878. }
  1879. Vector<uint8_t> Image::get_data() const {
  1880. return data;
  1881. }
  1882. Ref<Image> Image::create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1883. Ref<Image> image;
  1884. image.instantiate();
  1885. image->initialize_data(p_width, p_height, p_use_mipmaps, p_format);
  1886. return image;
  1887. }
  1888. Ref<Image> Image::create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  1889. Ref<Image> image;
  1890. image.instantiate();
  1891. image->initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data);
  1892. return image;
  1893. }
  1894. void Image::set_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  1895. initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data);
  1896. }
  1897. void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1898. ERR_FAIL_COND_MSG(p_width <= 0, vformat("The Image width specified (%d pixels) must be greater than 0 pixels.", p_width));
  1899. ERR_FAIL_COND_MSG(p_height <= 0, vformat("The Image height specified (%d pixels) must be greater than 0 pixels.", p_height));
  1900. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH,
  1901. vformat("The Image width specified (%d pixels) cannot be greater than %d pixels.", p_width, MAX_WIDTH));
  1902. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT,
  1903. vformat("The Image height specified (%d pixels) cannot be greater than %d pixels.", p_height, MAX_HEIGHT));
  1904. ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS,
  1905. vformat("Too many pixels for Image. Maximum is %dx%d = %d pixels.", MAX_WIDTH, MAX_HEIGHT, MAX_PIXELS));
  1906. ERR_FAIL_INDEX_MSG(p_format, FORMAT_MAX, vformat("The Image format specified (%d) is out of range. See Image's Format enum.", p_format));
  1907. int mm = 0;
  1908. int64_t size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1909. data.resize(size);
  1910. {
  1911. uint8_t *w = data.ptrw();
  1912. memset(w, 0, size);
  1913. }
  1914. width = p_width;
  1915. height = p_height;
  1916. mipmaps = p_use_mipmaps;
  1917. format = p_format;
  1918. }
  1919. void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  1920. ERR_FAIL_COND_MSG(p_width <= 0, vformat("The Image width specified (%d pixels) must be greater than 0 pixels.", p_width));
  1921. ERR_FAIL_COND_MSG(p_height <= 0, vformat("The Image height specified (%d pixels) must be greater than 0 pixels.", p_height));
  1922. ERR_FAIL_COND_MSG(p_width > MAX_WIDTH,
  1923. vformat("The Image width specified (%d pixels) cannot be greater than %d pixels.", p_width, MAX_WIDTH));
  1924. ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT,
  1925. vformat("The Image height specified (%d pixels) cannot be greater than %d pixels.", p_height, MAX_HEIGHT));
  1926. ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS,
  1927. vformat("Too many pixels for Image. Maximum is %dx%d = %d pixels.", MAX_WIDTH, MAX_HEIGHT, MAX_PIXELS));
  1928. ERR_FAIL_INDEX_MSG(p_format, FORMAT_MAX, vformat("The Image format specified (%d) is out of range. See Image's Format enum.", p_format));
  1929. int mm;
  1930. int64_t size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1931. if (unlikely(p_data.size() != size)) {
  1932. String description_mipmaps = get_format_name(p_format) + " ";
  1933. if (p_use_mipmaps) {
  1934. const int num_mipmaps = get_image_required_mipmaps(p_width, p_height, p_format);
  1935. if (num_mipmaps != 1) {
  1936. description_mipmaps += vformat("with %d mipmaps", num_mipmaps);
  1937. } else {
  1938. description_mipmaps += "with 1 mipmap";
  1939. }
  1940. } else {
  1941. description_mipmaps += "without mipmaps";
  1942. }
  1943. const String description = vformat("%dx%dx%d (%s)", p_width, p_height, get_format_pixel_size(p_format), description_mipmaps);
  1944. ERR_FAIL_MSG(vformat("Expected Image data size of %s = %d bytes, got %d bytes instead.", description, size, p_data.size()));
  1945. }
  1946. height = p_height;
  1947. width = p_width;
  1948. format = p_format;
  1949. data = p_data;
  1950. mipmaps = p_use_mipmaps;
  1951. }
  1952. void Image::initialize_data(const char **p_xpm) {
  1953. int size_width = 0;
  1954. int size_height = 0;
  1955. int pixelchars = 0;
  1956. mipmaps = false;
  1957. bool has_alpha = false;
  1958. enum Status {
  1959. READING_HEADER,
  1960. READING_COLORS,
  1961. READING_PIXELS,
  1962. DONE
  1963. };
  1964. Status status = READING_HEADER;
  1965. int line = 0;
  1966. HashMap<String, Color> colormap;
  1967. int colormap_size = 0;
  1968. uint32_t pixel_size = 0;
  1969. uint8_t *data_write = nullptr;
  1970. while (status != DONE) {
  1971. const char *line_ptr = p_xpm[line];
  1972. switch (status) {
  1973. case READING_HEADER: {
  1974. String line_str = line_ptr;
  1975. line_str.replace("\t", " ");
  1976. size_width = line_str.get_slicec(' ', 0).to_int();
  1977. size_height = line_str.get_slicec(' ', 1).to_int();
  1978. colormap_size = line_str.get_slicec(' ', 2).to_int();
  1979. pixelchars = line_str.get_slicec(' ', 3).to_int();
  1980. ERR_FAIL_COND(colormap_size > 32766);
  1981. ERR_FAIL_COND(pixelchars > 5);
  1982. ERR_FAIL_COND(size_width > 32767);
  1983. ERR_FAIL_COND(size_height > 32767);
  1984. status = READING_COLORS;
  1985. } break;
  1986. case READING_COLORS: {
  1987. String colorstring;
  1988. for (int i = 0; i < pixelchars; i++) {
  1989. colorstring += *line_ptr;
  1990. line_ptr++;
  1991. }
  1992. //skip spaces
  1993. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1994. if (*line_ptr == 0) {
  1995. break;
  1996. }
  1997. line_ptr++;
  1998. }
  1999. if (*line_ptr == 'c') {
  2000. line_ptr++;
  2001. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  2002. if (*line_ptr == 0) {
  2003. break;
  2004. }
  2005. line_ptr++;
  2006. }
  2007. if (*line_ptr == '#') {
  2008. line_ptr++;
  2009. uint8_t col_r = 0;
  2010. uint8_t col_g = 0;
  2011. uint8_t col_b = 0;
  2012. //uint8_t col_a=255;
  2013. for (int i = 0; i < 6; i++) {
  2014. char v = line_ptr[i];
  2015. if (is_digit(v)) {
  2016. v -= '0';
  2017. } else if (v >= 'A' && v <= 'F') {
  2018. v = (v - 'A') + 10;
  2019. } else if (v >= 'a' && v <= 'f') {
  2020. v = (v - 'a') + 10;
  2021. } else {
  2022. break;
  2023. }
  2024. switch (i) {
  2025. case 0:
  2026. col_r = v << 4;
  2027. break;
  2028. case 1:
  2029. col_r |= v;
  2030. break;
  2031. case 2:
  2032. col_g = v << 4;
  2033. break;
  2034. case 3:
  2035. col_g |= v;
  2036. break;
  2037. case 4:
  2038. col_b = v << 4;
  2039. break;
  2040. case 5:
  2041. col_b |= v;
  2042. break;
  2043. }
  2044. }
  2045. // magenta mask
  2046. if (col_r == 255 && col_g == 0 && col_b == 255) {
  2047. colormap[colorstring] = Color(0, 0, 0, 0);
  2048. has_alpha = true;
  2049. } else {
  2050. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  2051. }
  2052. }
  2053. }
  2054. if (line == colormap_size) {
  2055. status = READING_PIXELS;
  2056. initialize_data(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8);
  2057. data_write = data.ptrw();
  2058. pixel_size = has_alpha ? 4 : 3;
  2059. }
  2060. } break;
  2061. case READING_PIXELS: {
  2062. int y = line - colormap_size - 1;
  2063. for (int x = 0; x < size_width; x++) {
  2064. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  2065. for (int i = 0; i < pixelchars; i++) {
  2066. pixelstr[i] = line_ptr[x * pixelchars + i];
  2067. }
  2068. Color *colorptr = colormap.getptr(pixelstr);
  2069. ERR_FAIL_NULL(colorptr);
  2070. uint8_t pixel[4];
  2071. for (uint32_t i = 0; i < pixel_size; i++) {
  2072. pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255);
  2073. }
  2074. _put_pixelb(x, y, pixel_size, data_write, pixel);
  2075. }
  2076. if (y == (size_height - 1)) {
  2077. status = DONE;
  2078. }
  2079. } break;
  2080. default: {
  2081. }
  2082. }
  2083. line++;
  2084. }
  2085. }
  2086. #define DETECT_ALPHA_MAX_THRESHOLD 254
  2087. #define DETECT_ALPHA_MIN_THRESHOLD 2
  2088. #define DETECT_ALPHA(m_value) \
  2089. { \
  2090. uint8_t value = m_value; \
  2091. if (value < DETECT_ALPHA_MIN_THRESHOLD) \
  2092. bit = true; \
  2093. else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \
  2094. detected = true; \
  2095. break; \
  2096. } \
  2097. }
  2098. #define DETECT_NON_ALPHA(m_value) \
  2099. { \
  2100. uint8_t value = m_value; \
  2101. if (value > 0) { \
  2102. detected = true; \
  2103. break; \
  2104. } \
  2105. }
  2106. bool Image::is_invisible() const {
  2107. if (format == FORMAT_L8 || format == FORMAT_RGB8 || format == FORMAT_RG8) {
  2108. return false;
  2109. }
  2110. int64_t len = data.size();
  2111. if (len == 0) {
  2112. return true;
  2113. }
  2114. int w, h;
  2115. _get_mipmap_offset_and_size(1, len, w, h);
  2116. const uint8_t *r = data.ptr();
  2117. const unsigned char *data_ptr = r;
  2118. bool detected = false;
  2119. switch (format) {
  2120. case FORMAT_LA8: {
  2121. for (int i = 0; i < (len >> 1); i++) {
  2122. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  2123. }
  2124. } break;
  2125. case FORMAT_RGBA8: {
  2126. for (int i = 0; i < (len >> 2); i++) {
  2127. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  2128. }
  2129. } break;
  2130. case FORMAT_DXT3:
  2131. case FORMAT_DXT5: {
  2132. detected = true;
  2133. } break;
  2134. default: {
  2135. }
  2136. }
  2137. return !detected;
  2138. }
  2139. Image::AlphaMode Image::detect_alpha() const {
  2140. int64_t len = data.size();
  2141. if (len == 0) {
  2142. return ALPHA_NONE;
  2143. }
  2144. int w, h;
  2145. _get_mipmap_offset_and_size(1, len, w, h);
  2146. const uint8_t *r = data.ptr();
  2147. const unsigned char *data_ptr = r;
  2148. bool bit = false;
  2149. bool detected = false;
  2150. switch (format) {
  2151. case FORMAT_LA8: {
  2152. for (int i = 0; i < (len >> 1); i++) {
  2153. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  2154. }
  2155. } break;
  2156. case FORMAT_RGBA8: {
  2157. for (int i = 0; i < (len >> 2); i++) {
  2158. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  2159. }
  2160. } break;
  2161. case FORMAT_DXT3:
  2162. case FORMAT_DXT5: {
  2163. detected = true;
  2164. } break;
  2165. default: {
  2166. }
  2167. }
  2168. if (detected) {
  2169. return ALPHA_BLEND;
  2170. } else if (bit) {
  2171. return ALPHA_BIT;
  2172. } else {
  2173. return ALPHA_NONE;
  2174. }
  2175. }
  2176. Error Image::load(const String &p_path) {
  2177. String path = ResourceUID::ensure_path(p_path);
  2178. #ifdef DEBUG_ENABLED
  2179. if (path.begins_with("res://") && ResourceLoader::exists(path)) {
  2180. WARN_PRINT(vformat("Loaded resource as image file, this will not work on export: '%s'. Instead, import the image file as an Image resource and load it normally as a resource.", path));
  2181. }
  2182. #endif
  2183. return ImageLoader::load_image(path, this);
  2184. }
  2185. Ref<Image> Image::load_from_file(const String &p_path) {
  2186. String path = ResourceUID::ensure_path(p_path);
  2187. #ifdef DEBUG_ENABLED
  2188. if (path.begins_with("res://") && ResourceLoader::exists(path)) {
  2189. WARN_PRINT(vformat("Loaded resource as image file, this will not work on export: '%s'. Instead, import the image file as an Image resource and load it normally as a resource.", path));
  2190. }
  2191. #endif
  2192. Ref<Image> image;
  2193. image.instantiate();
  2194. Error err = ImageLoader::load_image(path, image);
  2195. if (err != OK) {
  2196. ERR_FAIL_V_MSG(Ref<Image>(), vformat("Failed to load image. Error %d", err));
  2197. }
  2198. return image;
  2199. }
  2200. Error Image::save_png(const String &p_path) const {
  2201. if (save_png_func == nullptr) {
  2202. return ERR_UNAVAILABLE;
  2203. }
  2204. return save_png_func(p_path, Ref<Image>((Image *)this));
  2205. }
  2206. Error Image::save_jpg(const String &p_path, float p_quality) const {
  2207. if (save_jpg_func == nullptr) {
  2208. return ERR_UNAVAILABLE;
  2209. }
  2210. return save_jpg_func(p_path, Ref<Image>((Image *)this), p_quality);
  2211. }
  2212. Vector<uint8_t> Image::save_png_to_buffer() const {
  2213. if (save_png_buffer_func == nullptr) {
  2214. return Vector<uint8_t>();
  2215. }
  2216. return save_png_buffer_func(Ref<Image>((Image *)this));
  2217. }
  2218. Vector<uint8_t> Image::save_jpg_to_buffer(float p_quality) const {
  2219. if (save_jpg_buffer_func == nullptr) {
  2220. return Vector<uint8_t>();
  2221. }
  2222. return save_jpg_buffer_func(Ref<Image>((Image *)this), p_quality);
  2223. }
  2224. Error Image::save_exr(const String &p_path, bool p_grayscale) const {
  2225. if (save_exr_func == nullptr) {
  2226. return ERR_UNAVAILABLE;
  2227. }
  2228. return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale);
  2229. }
  2230. Vector<uint8_t> Image::save_exr_to_buffer(bool p_grayscale) const {
  2231. if (save_exr_buffer_func == nullptr) {
  2232. return Vector<uint8_t>();
  2233. }
  2234. return save_exr_buffer_func(Ref<Image>((Image *)this), p_grayscale);
  2235. }
  2236. Error Image::save_webp(const String &p_path, const bool p_lossy, const float p_quality) const {
  2237. if (save_webp_func == nullptr) {
  2238. return ERR_UNAVAILABLE;
  2239. }
  2240. ERR_FAIL_COND_V_MSG(p_lossy && !(0.0f <= p_quality && p_quality <= 1.0f), ERR_INVALID_PARAMETER, vformat("The WebP lossy quality was set to %f, which is not valid. WebP lossy quality must be between 0.0 and 1.0 (inclusive).", p_quality));
  2241. return save_webp_func(p_path, Ref<Image>((Image *)this), p_lossy, p_quality);
  2242. }
  2243. Vector<uint8_t> Image::save_webp_to_buffer(const bool p_lossy, const float p_quality) const {
  2244. if (save_webp_buffer_func == nullptr) {
  2245. return Vector<uint8_t>();
  2246. }
  2247. ERR_FAIL_COND_V_MSG(p_lossy && !(0.0f <= p_quality && p_quality <= 1.0f), Vector<uint8_t>(), vformat("The WebP lossy quality was set to %f, which is not valid. WebP lossy quality must be between 0.0 and 1.0 (inclusive).", p_quality));
  2248. return save_webp_buffer_func(Ref<Image>((Image *)this), p_lossy, p_quality);
  2249. }
  2250. int64_t Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) {
  2251. int mm;
  2252. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0);
  2253. }
  2254. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  2255. int mm;
  2256. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  2257. return mm;
  2258. }
  2259. Size2i Image::get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap) {
  2260. int mm;
  2261. Size2i ret;
  2262. _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap, &ret.x, &ret.y);
  2263. return ret;
  2264. }
  2265. int64_t Image::get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap) {
  2266. if (p_mipmap <= 0) {
  2267. return 0;
  2268. }
  2269. int mm;
  2270. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1);
  2271. }
  2272. int64_t Image::get_image_mipmap_offset_and_dimensions(int p_width, int p_height, Format p_format, int p_mipmap, int &r_w, int &r_h) {
  2273. if (p_mipmap <= 0) {
  2274. r_w = p_width;
  2275. r_h = p_height;
  2276. return 0;
  2277. }
  2278. int mm;
  2279. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1, &r_w, &r_h);
  2280. }
  2281. bool Image::is_compressed() const {
  2282. return is_format_compressed(format);
  2283. }
  2284. bool Image::is_format_compressed(Format p_format) {
  2285. return p_format > FORMAT_RGBE9995;
  2286. }
  2287. Error Image::decompress() {
  2288. if (((format >= FORMAT_DXT1 && format <= FORMAT_RGTC_RG) || (format == FORMAT_DXT5_RA_AS_RG)) && _image_decompress_bc) {
  2289. _image_decompress_bc(this);
  2290. } else if (format >= FORMAT_BPTC_RGBA && format <= FORMAT_BPTC_RGBFU && _image_decompress_bptc) {
  2291. _image_decompress_bptc(this);
  2292. } else if (format == FORMAT_ETC && _image_decompress_etc1) {
  2293. _image_decompress_etc1(this);
  2294. } else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RA_AS_RG && _image_decompress_etc2) {
  2295. _image_decompress_etc2(this);
  2296. } else if (format >= FORMAT_ASTC_4x4 && format <= FORMAT_ASTC_8x8_HDR && _image_decompress_astc) {
  2297. _image_decompress_astc(this);
  2298. } else {
  2299. return ERR_UNAVAILABLE;
  2300. }
  2301. return OK;
  2302. }
  2303. Error Image::compress(CompressMode p_mode, CompressSource p_source, ASTCFormat p_astc_format) {
  2304. ERR_FAIL_INDEX_V_MSG(p_mode, COMPRESS_MAX, ERR_INVALID_PARAMETER, "Invalid compress mode.");
  2305. ERR_FAIL_INDEX_V_MSG(p_source, COMPRESS_SOURCE_MAX, ERR_INVALID_PARAMETER, "Invalid compress source.");
  2306. return compress_from_channels(p_mode, detect_used_channels(p_source), p_astc_format);
  2307. }
  2308. Error Image::compress_from_channels(CompressMode p_mode, UsedChannels p_channels, ASTCFormat p_astc_format) {
  2309. ERR_FAIL_COND_V(data.is_empty(), ERR_INVALID_DATA);
  2310. // RenderingDevice only.
  2311. if (GLOBAL_GET("rendering/textures/vram_compression/compress_with_gpu")) {
  2312. switch (p_mode) {
  2313. case COMPRESS_BPTC: {
  2314. // BC7 is unsupported currently.
  2315. if ((format >= FORMAT_RF && format <= FORMAT_RGBE9995) && _image_compress_bptc_rd_func) {
  2316. Error result = _image_compress_bptc_rd_func(this, p_channels);
  2317. // If the image was compressed successfully, we return here. If not, we fall back to the default compression scheme.
  2318. if (result == OK) {
  2319. return OK;
  2320. }
  2321. }
  2322. } break;
  2323. case COMPRESS_S3TC: {
  2324. if (_image_compress_bc_rd_func) {
  2325. Error result = _image_compress_bc_rd_func(this, p_channels);
  2326. // If the image was compressed successfully, we return here. If not, we fall back to the default compression scheme.
  2327. if (result == OK) {
  2328. return OK;
  2329. }
  2330. }
  2331. } break;
  2332. default: {
  2333. }
  2334. }
  2335. }
  2336. switch (p_mode) {
  2337. case COMPRESS_S3TC: {
  2338. ERR_FAIL_NULL_V(_image_compress_bc_func, ERR_UNAVAILABLE);
  2339. _image_compress_bc_func(this, p_channels);
  2340. } break;
  2341. case COMPRESS_ETC: {
  2342. ERR_FAIL_NULL_V(_image_compress_etc1_func, ERR_UNAVAILABLE);
  2343. _image_compress_etc1_func(this);
  2344. } break;
  2345. case COMPRESS_ETC2: {
  2346. ERR_FAIL_NULL_V(_image_compress_etc2_func, ERR_UNAVAILABLE);
  2347. _image_compress_etc2_func(this, p_channels);
  2348. } break;
  2349. case COMPRESS_BPTC: {
  2350. ERR_FAIL_NULL_V(_image_compress_bptc_func, ERR_UNAVAILABLE);
  2351. _image_compress_bptc_func(this, p_channels);
  2352. } break;
  2353. case COMPRESS_ASTC: {
  2354. ERR_FAIL_NULL_V(_image_compress_astc_func, ERR_UNAVAILABLE);
  2355. _image_compress_astc_func(this, p_astc_format);
  2356. } break;
  2357. case COMPRESS_MAX: {
  2358. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  2359. } break;
  2360. }
  2361. return OK;
  2362. }
  2363. Image::Image(const char **p_xpm) {
  2364. width = 0;
  2365. height = 0;
  2366. mipmaps = false;
  2367. format = FORMAT_L8;
  2368. initialize_data(p_xpm);
  2369. }
  2370. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  2371. width = 0;
  2372. height = 0;
  2373. mipmaps = p_use_mipmaps;
  2374. format = FORMAT_L8;
  2375. initialize_data(p_width, p_height, p_use_mipmaps, p_format);
  2376. }
  2377. Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  2378. width = 0;
  2379. height = 0;
  2380. mipmaps = p_mipmaps;
  2381. format = FORMAT_L8;
  2382. initialize_data(p_width, p_height, p_mipmaps, p_format, p_data);
  2383. }
  2384. Rect2i Image::get_used_rect() const {
  2385. if (format != FORMAT_LA8 && format != FORMAT_RGBA8 && format != FORMAT_RGBAF && format != FORMAT_RGBAH && format != FORMAT_RGBA4444 && format != FORMAT_RGB565) {
  2386. return Rect2i(0, 0, width, height);
  2387. }
  2388. int len = data.size();
  2389. if (len == 0) {
  2390. return Rect2i();
  2391. }
  2392. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  2393. int maxx = -1, maxy = -1;
  2394. for (int j = 0; j < height; j++) {
  2395. for (int i = 0; i < width; i++) {
  2396. if (!(get_pixel(i, j).a > 0)) {
  2397. continue;
  2398. }
  2399. if (i > maxx) {
  2400. maxx = i;
  2401. }
  2402. if (j > maxy) {
  2403. maxy = j;
  2404. }
  2405. if (i < minx) {
  2406. minx = i;
  2407. }
  2408. if (j < miny) {
  2409. miny = j;
  2410. }
  2411. }
  2412. }
  2413. if (maxx == -1) {
  2414. return Rect2i();
  2415. } else {
  2416. return Rect2i(minx, miny, maxx - minx + 1, maxy - miny + 1);
  2417. }
  2418. }
  2419. Ref<Image> Image::get_region(const Rect2i &p_region) const {
  2420. Ref<Image> img = memnew(Image(p_region.size.x, p_region.size.y, mipmaps, format));
  2421. img->blit_rect(Ref<Image>((Image *)this), p_region, Point2i(0, 0));
  2422. return img;
  2423. }
  2424. void Image::_get_clipped_src_and_dest_rects(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest, Rect2i &r_clipped_src_rect, Rect2i &r_clipped_dest_rect) const {
  2425. r_clipped_dest_rect.position = p_dest;
  2426. r_clipped_src_rect = p_src_rect;
  2427. if (r_clipped_src_rect.position.x < 0) {
  2428. r_clipped_dest_rect.position.x -= r_clipped_src_rect.position.x;
  2429. r_clipped_src_rect.size.x += r_clipped_src_rect.position.x;
  2430. r_clipped_src_rect.position.x = 0;
  2431. }
  2432. if (r_clipped_src_rect.position.y < 0) {
  2433. r_clipped_dest_rect.position.y -= r_clipped_src_rect.position.y;
  2434. r_clipped_src_rect.size.y += r_clipped_src_rect.position.y;
  2435. r_clipped_src_rect.position.y = 0;
  2436. }
  2437. if (r_clipped_dest_rect.position.x < 0) {
  2438. r_clipped_src_rect.position.x -= r_clipped_dest_rect.position.x;
  2439. r_clipped_src_rect.size.x += r_clipped_dest_rect.position.x;
  2440. r_clipped_dest_rect.position.x = 0;
  2441. }
  2442. if (r_clipped_dest_rect.position.y < 0) {
  2443. r_clipped_src_rect.position.y -= r_clipped_dest_rect.position.y;
  2444. r_clipped_src_rect.size.y += r_clipped_dest_rect.position.y;
  2445. r_clipped_dest_rect.position.y = 0;
  2446. }
  2447. r_clipped_src_rect.size.x = MAX(0, MIN(r_clipped_src_rect.size.x, MIN(p_src->width - r_clipped_src_rect.position.x, width - r_clipped_dest_rect.position.x)));
  2448. r_clipped_src_rect.size.y = MAX(0, MIN(r_clipped_src_rect.size.y, MIN(p_src->height - r_clipped_src_rect.position.y, height - r_clipped_dest_rect.position.y)));
  2449. r_clipped_dest_rect.size.x = r_clipped_src_rect.size.x;
  2450. r_clipped_dest_rect.size.y = r_clipped_src_rect.size.y;
  2451. }
  2452. void Image::blit_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest) {
  2453. ERR_FAIL_COND_MSG(p_src.is_null(), "Cannot blit_rect an image: invalid source Image object.");
  2454. int dsize = data.size();
  2455. int srcdsize = p_src->data.size();
  2456. ERR_FAIL_COND(dsize == 0);
  2457. ERR_FAIL_COND(srcdsize == 0);
  2458. ERR_FAIL_COND(format != p_src->format);
  2459. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot blit_rect in compressed or custom image formats.");
  2460. Rect2i src_rect;
  2461. Rect2i dest_rect;
  2462. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  2463. if (!src_rect.has_area() || !dest_rect.has_area()) {
  2464. return;
  2465. }
  2466. uint8_t *wp = data.ptrw();
  2467. uint8_t *dst_data_ptr = wp;
  2468. const uint8_t *rp = p_src->data.ptr();
  2469. const uint8_t *src_data_ptr = rp;
  2470. int pixel_size = get_format_pixel_size(format);
  2471. for (int i = 0; i < dest_rect.size.y; i++) {
  2472. for (int j = 0; j < dest_rect.size.x; j++) {
  2473. int src_x = src_rect.position.x + j;
  2474. int src_y = src_rect.position.y + i;
  2475. int dst_x = dest_rect.position.x + j;
  2476. int dst_y = dest_rect.position.y + i;
  2477. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  2478. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  2479. for (int k = 0; k < pixel_size; k++) {
  2480. dst[k] = src[k];
  2481. }
  2482. }
  2483. }
  2484. }
  2485. void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2i &p_src_rect, const Point2i &p_dest) {
  2486. ERR_FAIL_COND_MSG(p_src.is_null(), "Cannot blit_rect_mask an image: invalid source Image object.");
  2487. ERR_FAIL_COND_MSG(p_mask.is_null(), "Cannot blit_rect_mask an image: invalid mask Image object.");
  2488. int dsize = data.size();
  2489. int srcdsize = p_src->data.size();
  2490. int maskdsize = p_mask->data.size();
  2491. ERR_FAIL_COND(dsize == 0);
  2492. ERR_FAIL_COND(srcdsize == 0);
  2493. ERR_FAIL_COND(maskdsize == 0);
  2494. ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width.");
  2495. ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height.");
  2496. ERR_FAIL_COND(format != p_src->format);
  2497. Rect2i src_rect;
  2498. Rect2i dest_rect;
  2499. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  2500. if (!src_rect.has_area() || !dest_rect.has_area()) {
  2501. return;
  2502. }
  2503. uint8_t *wp = data.ptrw();
  2504. uint8_t *dst_data_ptr = wp;
  2505. const uint8_t *rp = p_src->data.ptr();
  2506. const uint8_t *src_data_ptr = rp;
  2507. int pixel_size = get_format_pixel_size(format);
  2508. Ref<Image> msk = p_mask;
  2509. for (int i = 0; i < dest_rect.size.y; i++) {
  2510. for (int j = 0; j < dest_rect.size.x; j++) {
  2511. int src_x = src_rect.position.x + j;
  2512. int src_y = src_rect.position.y + i;
  2513. if (msk->get_pixel(src_x, src_y).a != 0) {
  2514. int dst_x = dest_rect.position.x + j;
  2515. int dst_y = dest_rect.position.y + i;
  2516. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  2517. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  2518. for (int k = 0; k < pixel_size; k++) {
  2519. dst[k] = src[k];
  2520. }
  2521. }
  2522. }
  2523. }
  2524. }
  2525. void Image::blend_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest) {
  2526. ERR_FAIL_COND_MSG(p_src.is_null(), "Cannot blend_rect an image: invalid source Image object.");
  2527. int dsize = data.size();
  2528. int srcdsize = p_src->data.size();
  2529. ERR_FAIL_COND(dsize == 0);
  2530. ERR_FAIL_COND(srcdsize == 0);
  2531. ERR_FAIL_COND(format != p_src->format);
  2532. Rect2i src_rect;
  2533. Rect2i dest_rect;
  2534. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  2535. if (!src_rect.has_area() || !dest_rect.has_area()) {
  2536. return;
  2537. }
  2538. Ref<Image> img = p_src;
  2539. for (int i = 0; i < dest_rect.size.y; i++) {
  2540. for (int j = 0; j < dest_rect.size.x; j++) {
  2541. int src_x = src_rect.position.x + j;
  2542. int src_y = src_rect.position.y + i;
  2543. int dst_x = dest_rect.position.x + j;
  2544. int dst_y = dest_rect.position.y + i;
  2545. Color sc = img->get_pixel(src_x, src_y);
  2546. if (sc.a != 0) {
  2547. Color dc = get_pixel(dst_x, dst_y);
  2548. dc = dc.blend(sc);
  2549. set_pixel(dst_x, dst_y, dc);
  2550. }
  2551. }
  2552. }
  2553. }
  2554. void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2i &p_src_rect, const Point2i &p_dest) {
  2555. ERR_FAIL_COND_MSG(p_src.is_null(), "Cannot blend_rect_mask an image: invalid source Image object.");
  2556. ERR_FAIL_COND_MSG(p_mask.is_null(), "Cannot blend_rect_mask an image: invalid mask Image object.");
  2557. int dsize = data.size();
  2558. int srcdsize = p_src->data.size();
  2559. int maskdsize = p_mask->data.size();
  2560. ERR_FAIL_COND(dsize == 0);
  2561. ERR_FAIL_COND(srcdsize == 0);
  2562. ERR_FAIL_COND(maskdsize == 0);
  2563. ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width.");
  2564. ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height.");
  2565. ERR_FAIL_COND(format != p_src->format);
  2566. Rect2i src_rect;
  2567. Rect2i dest_rect;
  2568. _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect);
  2569. if (!src_rect.has_area() || !dest_rect.has_area()) {
  2570. return;
  2571. }
  2572. Ref<Image> img = p_src;
  2573. Ref<Image> msk = p_mask;
  2574. for (int i = 0; i < dest_rect.size.y; i++) {
  2575. for (int j = 0; j < dest_rect.size.x; j++) {
  2576. int src_x = src_rect.position.x + j;
  2577. int src_y = src_rect.position.y + i;
  2578. // If the mask's pixel is transparent then we skip it
  2579. //Color c = msk->get_pixel(src_x, src_y);
  2580. //if (c.a == 0) continue;
  2581. if (msk->get_pixel(src_x, src_y).a != 0) {
  2582. int dst_x = dest_rect.position.x + j;
  2583. int dst_y = dest_rect.position.y + i;
  2584. Color sc = img->get_pixel(src_x, src_y);
  2585. if (sc.a != 0) {
  2586. Color dc = get_pixel(dst_x, dst_y);
  2587. dc = dc.blend(sc);
  2588. set_pixel(dst_x, dst_y, dc);
  2589. }
  2590. }
  2591. }
  2592. }
  2593. }
  2594. // Repeats `p_pixel` `p_count` times in consecutive memory.
  2595. // Results in the original pixel and `p_count - 1` subsequent copies of it.
  2596. void Image::_repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_size, int p_count) {
  2597. int offset = 1;
  2598. for (int stride = 1; offset + stride <= p_count; stride *= 2) {
  2599. memcpy(p_pixel + offset * p_pixel_size, p_pixel, stride * p_pixel_size);
  2600. offset += stride;
  2601. }
  2602. if (offset < p_count) {
  2603. memcpy(p_pixel + offset * p_pixel_size, p_pixel, (p_count - offset) * p_pixel_size);
  2604. }
  2605. }
  2606. void Image::fill(const Color &p_color) {
  2607. if (data.size() == 0) {
  2608. return;
  2609. }
  2610. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats.");
  2611. uint8_t *dst_data_ptr = data.ptrw();
  2612. int pixel_size = get_format_pixel_size(format);
  2613. // Put first pixel with the format-aware API.
  2614. _set_color_at_ofs(dst_data_ptr, 0, p_color);
  2615. _repeat_pixel_over_subsequent_memory(dst_data_ptr, pixel_size, width * height);
  2616. }
  2617. void Image::fill_rect(const Rect2i &p_rect, const Color &p_color) {
  2618. if (data.size() == 0) {
  2619. return;
  2620. }
  2621. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill rect in compressed or custom image formats.");
  2622. Rect2i r = Rect2i(0, 0, width, height).intersection(p_rect.abs());
  2623. if (!r.has_area()) {
  2624. return;
  2625. }
  2626. uint8_t *dst_data_ptr = data.ptrw();
  2627. int pixel_size = get_format_pixel_size(format);
  2628. // Put first pixel with the format-aware API.
  2629. uint8_t *rect_first_pixel_ptr = &dst_data_ptr[(r.position.y * width + r.position.x) * pixel_size];
  2630. _set_color_at_ofs(rect_first_pixel_ptr, 0, p_color);
  2631. if (r.size.x == width) {
  2632. // No need to fill rows separately.
  2633. _repeat_pixel_over_subsequent_memory(rect_first_pixel_ptr, pixel_size, width * r.size.y);
  2634. } else {
  2635. _repeat_pixel_over_subsequent_memory(rect_first_pixel_ptr, pixel_size, r.size.x);
  2636. for (int y = 1; y < r.size.y; y++) {
  2637. memcpy(rect_first_pixel_ptr + y * width * pixel_size, rect_first_pixel_ptr, r.size.x * pixel_size);
  2638. }
  2639. }
  2640. }
  2641. void Image::_set_data(const Dictionary &p_data) {
  2642. ERR_FAIL_COND(!p_data.has("width"));
  2643. ERR_FAIL_COND(!p_data.has("height"));
  2644. ERR_FAIL_COND(!p_data.has("format"));
  2645. ERR_FAIL_COND(!p_data.has("mipmaps"));
  2646. ERR_FAIL_COND(!p_data.has("data"));
  2647. int dwidth = p_data["width"];
  2648. int dheight = p_data["height"];
  2649. String dformat = p_data["format"];
  2650. bool dmipmaps = p_data["mipmaps"];
  2651. Vector<uint8_t> ddata = p_data["data"];
  2652. Format ddformat = FORMAT_MAX;
  2653. for (int i = 0; i < FORMAT_MAX; i++) {
  2654. if (dformat == get_format_name(Format(i))) {
  2655. ddformat = Format(i);
  2656. break;
  2657. }
  2658. }
  2659. ERR_FAIL_COND(ddformat == FORMAT_MAX);
  2660. initialize_data(dwidth, dheight, dmipmaps, ddformat, ddata);
  2661. }
  2662. Dictionary Image::_get_data() const {
  2663. Dictionary d;
  2664. d["width"] = width;
  2665. d["height"] = height;
  2666. d["format"] = get_format_name(format);
  2667. d["mipmaps"] = mipmaps;
  2668. d["data"] = data;
  2669. return d;
  2670. }
  2671. Color Image::get_pixelv(const Point2i &p_point) const {
  2672. return get_pixel(p_point.x, p_point.y);
  2673. }
  2674. void Image::_copy_internals_from(const Image &p_image) {
  2675. format = p_image.format;
  2676. width = p_image.width;
  2677. height = p_image.height;
  2678. mipmaps = p_image.mipmaps;
  2679. data = p_image.data;
  2680. }
  2681. Color Image::_get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const {
  2682. switch (format) {
  2683. case FORMAT_L8: {
  2684. float l = ptr[ofs] / 255.0;
  2685. return Color(l, l, l, 1);
  2686. }
  2687. case FORMAT_LA8: {
  2688. float l = ptr[ofs * 2 + 0] / 255.0;
  2689. float a = ptr[ofs * 2 + 1] / 255.0;
  2690. return Color(l, l, l, a);
  2691. }
  2692. case FORMAT_R8: {
  2693. float r = ptr[ofs] / 255.0;
  2694. return Color(r, 0, 0, 1);
  2695. }
  2696. case FORMAT_RG8: {
  2697. float r = ptr[ofs * 2 + 0] / 255.0;
  2698. float g = ptr[ofs * 2 + 1] / 255.0;
  2699. return Color(r, g, 0, 1);
  2700. }
  2701. case FORMAT_RGB8: {
  2702. float r = ptr[ofs * 3 + 0] / 255.0;
  2703. float g = ptr[ofs * 3 + 1] / 255.0;
  2704. float b = ptr[ofs * 3 + 2] / 255.0;
  2705. return Color(r, g, b, 1);
  2706. }
  2707. case FORMAT_RGBA8: {
  2708. float r = ptr[ofs * 4 + 0] / 255.0;
  2709. float g = ptr[ofs * 4 + 1] / 255.0;
  2710. float b = ptr[ofs * 4 + 2] / 255.0;
  2711. float a = ptr[ofs * 4 + 3] / 255.0;
  2712. return Color(r, g, b, a);
  2713. }
  2714. case FORMAT_RGBA4444: {
  2715. uint16_t u = ((uint16_t *)ptr)[ofs];
  2716. float r = ((u >> 12) & 0xF) / 15.0;
  2717. float g = ((u >> 8) & 0xF) / 15.0;
  2718. float b = ((u >> 4) & 0xF) / 15.0;
  2719. float a = (u & 0xF) / 15.0;
  2720. return Color(r, g, b, a);
  2721. }
  2722. case FORMAT_RGB565: {
  2723. uint16_t u = ((uint16_t *)ptr)[ofs];
  2724. float r = (u & 0x1F) / 31.0;
  2725. float g = ((u >> 5) & 0x3F) / 63.0;
  2726. float b = ((u >> 11) & 0x1F) / 31.0;
  2727. return Color(r, g, b, 1.0);
  2728. }
  2729. case FORMAT_RF: {
  2730. float r = ((float *)ptr)[ofs];
  2731. return Color(r, 0, 0, 1);
  2732. }
  2733. case FORMAT_RGF: {
  2734. float r = ((float *)ptr)[ofs * 2 + 0];
  2735. float g = ((float *)ptr)[ofs * 2 + 1];
  2736. return Color(r, g, 0, 1);
  2737. }
  2738. case FORMAT_RGBF: {
  2739. float r = ((float *)ptr)[ofs * 3 + 0];
  2740. float g = ((float *)ptr)[ofs * 3 + 1];
  2741. float b = ((float *)ptr)[ofs * 3 + 2];
  2742. return Color(r, g, b, 1);
  2743. }
  2744. case FORMAT_RGBAF: {
  2745. float r = ((float *)ptr)[ofs * 4 + 0];
  2746. float g = ((float *)ptr)[ofs * 4 + 1];
  2747. float b = ((float *)ptr)[ofs * 4 + 2];
  2748. float a = ((float *)ptr)[ofs * 4 + 3];
  2749. return Color(r, g, b, a);
  2750. }
  2751. case FORMAT_RH: {
  2752. uint16_t r = ((uint16_t *)ptr)[ofs];
  2753. return Color(Math::half_to_float(r), 0, 0, 1);
  2754. }
  2755. case FORMAT_RGH: {
  2756. uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0];
  2757. uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1];
  2758. return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1);
  2759. }
  2760. case FORMAT_RGBH: {
  2761. uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0];
  2762. uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1];
  2763. uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2];
  2764. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1);
  2765. }
  2766. case FORMAT_RGBAH: {
  2767. uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0];
  2768. uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1];
  2769. uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2];
  2770. uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3];
  2771. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a));
  2772. }
  2773. case FORMAT_RGBE9995: {
  2774. return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]);
  2775. }
  2776. default: {
  2777. ERR_FAIL_V_MSG(Color(), "Can't get_pixel() on compressed image, sorry.");
  2778. }
  2779. }
  2780. }
  2781. void Image::_set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color) {
  2782. switch (format) {
  2783. case FORMAT_L8: {
  2784. ptr[ofs] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  2785. } break;
  2786. case FORMAT_LA8: {
  2787. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  2788. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  2789. } break;
  2790. case FORMAT_R8: {
  2791. ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2792. } break;
  2793. case FORMAT_RG8: {
  2794. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2795. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2796. } break;
  2797. case FORMAT_RGB8: {
  2798. ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2799. ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2800. ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  2801. } break;
  2802. case FORMAT_RGBA8: {
  2803. ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  2804. ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  2805. ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  2806. ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  2807. } break;
  2808. case FORMAT_RGBA4444: {
  2809. uint16_t rgba = 0;
  2810. rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15)) << 12;
  2811. rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 8;
  2812. rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 4;
  2813. rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15));
  2814. ((uint16_t *)ptr)[ofs] = rgba;
  2815. } break;
  2816. case FORMAT_RGB565: {
  2817. uint16_t rgba = 0;
  2818. rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
  2819. rgba |= uint16_t(CLAMP(p_color.g * 63.0, 0, 33)) << 5;
  2820. rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 11;
  2821. ((uint16_t *)ptr)[ofs] = rgba;
  2822. } break;
  2823. case FORMAT_RF: {
  2824. ((float *)ptr)[ofs] = p_color.r;
  2825. } break;
  2826. case FORMAT_RGF: {
  2827. ((float *)ptr)[ofs * 2 + 0] = p_color.r;
  2828. ((float *)ptr)[ofs * 2 + 1] = p_color.g;
  2829. } break;
  2830. case FORMAT_RGBF: {
  2831. ((float *)ptr)[ofs * 3 + 0] = p_color.r;
  2832. ((float *)ptr)[ofs * 3 + 1] = p_color.g;
  2833. ((float *)ptr)[ofs * 3 + 2] = p_color.b;
  2834. } break;
  2835. case FORMAT_RGBAF: {
  2836. ((float *)ptr)[ofs * 4 + 0] = p_color.r;
  2837. ((float *)ptr)[ofs * 4 + 1] = p_color.g;
  2838. ((float *)ptr)[ofs * 4 + 2] = p_color.b;
  2839. ((float *)ptr)[ofs * 4 + 3] = p_color.a;
  2840. } break;
  2841. case FORMAT_RH: {
  2842. ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r);
  2843. } break;
  2844. case FORMAT_RGH: {
  2845. ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r);
  2846. ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g);
  2847. } break;
  2848. case FORMAT_RGBH: {
  2849. ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r);
  2850. ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g);
  2851. ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b);
  2852. } break;
  2853. case FORMAT_RGBAH: {
  2854. ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r);
  2855. ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g);
  2856. ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b);
  2857. ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a);
  2858. } break;
  2859. case FORMAT_RGBE9995: {
  2860. ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995();
  2861. } break;
  2862. default: {
  2863. ERR_FAIL_MSG("Can't set_pixel() on compressed image, sorry.");
  2864. }
  2865. }
  2866. }
  2867. Color Image::get_pixel(int p_x, int p_y) const {
  2868. #ifdef DEBUG_ENABLED
  2869. ERR_FAIL_INDEX_V(p_x, width, Color());
  2870. ERR_FAIL_INDEX_V(p_y, height, Color());
  2871. #endif
  2872. uint32_t ofs = p_y * width + p_x;
  2873. return _get_color_at_ofs(data.ptr(), ofs);
  2874. }
  2875. void Image::set_pixelv(const Point2i &p_point, const Color &p_color) {
  2876. set_pixel(p_point.x, p_point.y, p_color);
  2877. }
  2878. void Image::set_pixel(int p_x, int p_y, const Color &p_color) {
  2879. #ifdef DEBUG_ENABLED
  2880. ERR_FAIL_INDEX(p_x, width);
  2881. ERR_FAIL_INDEX(p_y, height);
  2882. #endif
  2883. uint32_t ofs = p_y * width + p_x;
  2884. _set_color_at_ofs(data.ptrw(), ofs, p_color);
  2885. }
  2886. const uint8_t *Image::ptr() const {
  2887. return data.ptr();
  2888. }
  2889. uint8_t *Image::ptrw() {
  2890. return data.ptrw();
  2891. }
  2892. int64_t Image::get_data_size() const {
  2893. return data.size();
  2894. }
  2895. void Image::adjust_bcs(float p_brightness, float p_contrast, float p_saturation) {
  2896. ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot adjust_bcs in compressed or custom image formats.");
  2897. uint8_t *w = data.ptrw();
  2898. uint32_t pixel_size = get_format_pixel_size(format);
  2899. uint32_t pixel_count = data.size() / pixel_size;
  2900. for (uint32_t i = 0; i < pixel_count; i++) {
  2901. Color c = _get_color_at_ofs(w, i);
  2902. Vector3 rgb(c.r, c.g, c.b);
  2903. rgb *= p_brightness;
  2904. rgb = Vector3(0.5, 0.5, 0.5).lerp(rgb, p_contrast);
  2905. float center = (rgb.x + rgb.y + rgb.z) / 3.0;
  2906. rgb = Vector3(center, center, center).lerp(rgb, p_saturation);
  2907. c.r = rgb.x;
  2908. c.g = rgb.y;
  2909. c.b = rgb.z;
  2910. _set_color_at_ofs(w, i, c);
  2911. }
  2912. }
  2913. Image::UsedChannels Image::detect_used_channels(CompressSource p_source) const {
  2914. ERR_FAIL_COND_V(data.is_empty(), USED_CHANNELS_RGBA);
  2915. ERR_FAIL_COND_V(is_compressed(), USED_CHANNELS_RGBA);
  2916. if (p_source == COMPRESS_SOURCE_NORMAL) {
  2917. return USED_CHANNELS_RG; // Normal maps only use RG channels.
  2918. }
  2919. if (format == FORMAT_L8) {
  2920. return USED_CHANNELS_L; // Grayscale only cannot have any channel less.
  2921. } else if (format == FORMAT_R8 || format == FORMAT_RH || format == FORMAT_RF) {
  2922. return USED_CHANNELS_R; // Red only cannot have any channel less.
  2923. }
  2924. const bool supports_alpha = format == FORMAT_RGBA8 || format == FORMAT_RGBA4444 || format == FORMAT_RGBAH || format == FORMAT_RGBAF;
  2925. bool r = false, g = false, b = false, a = false, c = false;
  2926. const uint8_t *data_ptr = data.ptr();
  2927. const uint32_t data_total = width * height;
  2928. for (uint32_t i = 0; i < data_total; i++) {
  2929. Color col = _get_color_at_ofs(data_ptr, i);
  2930. if (!r && col.r > 0.001) {
  2931. r = true;
  2932. }
  2933. if (!g && col.g > 0.001) {
  2934. g = true;
  2935. }
  2936. if (!b && col.b > 0.001) {
  2937. b = true;
  2938. }
  2939. if (!a && col.a < 0.999) {
  2940. a = true;
  2941. }
  2942. if (col.r != col.b || col.r != col.g || col.b != col.g) {
  2943. c = true; // The image is not grayscale.
  2944. }
  2945. if (r && g && b && c) {
  2946. // All channels are used, no need to continue.
  2947. if (!supports_alpha) {
  2948. break;
  2949. } else if (a) {
  2950. break;
  2951. }
  2952. }
  2953. }
  2954. UsedChannels used_channels;
  2955. if (!c && !a) {
  2956. used_channels = USED_CHANNELS_L;
  2957. } else if (!c && a) {
  2958. used_channels = USED_CHANNELS_LA;
  2959. } else if (r && !g && !b && !a) {
  2960. used_channels = USED_CHANNELS_R;
  2961. } else if (r && g && !b && !a) {
  2962. used_channels = USED_CHANNELS_RG;
  2963. } else if (r && g && b && !a) {
  2964. used_channels = USED_CHANNELS_RGB;
  2965. } else {
  2966. used_channels = USED_CHANNELS_RGBA;
  2967. }
  2968. if (p_source == COMPRESS_SOURCE_SRGB && (used_channels == USED_CHANNELS_R || used_channels == USED_CHANNELS_RG)) {
  2969. used_channels = USED_CHANNELS_RGB; // R and RG do not support SRGB.
  2970. }
  2971. return used_channels;
  2972. }
  2973. void Image::optimize_channels() {
  2974. switch (detect_used_channels()) {
  2975. case USED_CHANNELS_L:
  2976. convert(FORMAT_L8);
  2977. break;
  2978. case USED_CHANNELS_LA:
  2979. convert(FORMAT_LA8);
  2980. break;
  2981. case USED_CHANNELS_R:
  2982. convert(FORMAT_R8);
  2983. break;
  2984. case USED_CHANNELS_RG:
  2985. convert(FORMAT_RG8);
  2986. break;
  2987. case USED_CHANNELS_RGB:
  2988. convert(FORMAT_RGB8);
  2989. break;
  2990. case USED_CHANNELS_RGBA:
  2991. convert(FORMAT_RGBA8);
  2992. break;
  2993. }
  2994. }
  2995. void Image::_bind_methods() {
  2996. ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
  2997. ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height);
  2998. ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size);
  2999. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps);
  3000. ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format);
  3001. ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data);
  3002. ClassDB::bind_method(D_METHOD("get_data_size"), &Image::get_data_size);
  3003. ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert);
  3004. ClassDB::bind_method(D_METHOD("get_mipmap_count"), &Image::get_mipmap_count);
  3005. ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset);
  3006. ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR));
  3007. ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR));
  3008. ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2);
  3009. ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop);
  3010. ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x);
  3011. ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y);
  3012. ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false));
  3013. ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps);
  3014. #ifndef DISABLE_DEPRECATED
  3015. ClassDB::bind_static_method("Image", D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::create_empty);
  3016. #endif
  3017. ClassDB::bind_static_method("Image", D_METHOD("create_empty", "width", "height", "use_mipmaps", "format"), &Image::create_empty);
  3018. ClassDB::bind_static_method("Image", D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::create_from_data);
  3019. ClassDB::bind_method(D_METHOD("set_data", "width", "height", "use_mipmaps", "format", "data"), &Image::set_data);
  3020. ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty);
  3021. ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
  3022. ClassDB::bind_static_method("Image", D_METHOD("load_from_file", "path"), &Image::load_from_file);
  3023. ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
  3024. ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer);
  3025. ClassDB::bind_method(D_METHOD("save_jpg", "path", "quality"), &Image::save_jpg, DEFVAL(0.75));
  3026. ClassDB::bind_method(D_METHOD("save_jpg_to_buffer", "quality"), &Image::save_jpg_to_buffer, DEFVAL(0.75));
  3027. ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false));
  3028. ClassDB::bind_method(D_METHOD("save_exr_to_buffer", "grayscale"), &Image::save_exr_to_buffer, DEFVAL(false));
  3029. ClassDB::bind_method(D_METHOD("save_webp", "path", "lossy", "quality"), &Image::save_webp, DEFVAL(false), DEFVAL(0.75f));
  3030. ClassDB::bind_method(D_METHOD("save_webp_to_buffer", "lossy", "quality"), &Image::save_webp_to_buffer, DEFVAL(false), DEFVAL(0.75f));
  3031. ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
  3032. ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);
  3033. ClassDB::bind_method(D_METHOD("detect_used_channels", "source"), &Image::detect_used_channels, DEFVAL(COMPRESS_SOURCE_GENERIC));
  3034. ClassDB::bind_method(D_METHOD("compress", "mode", "source", "astc_format"), &Image::compress, DEFVAL(COMPRESS_SOURCE_GENERIC), DEFVAL(ASTC_FORMAT_4x4));
  3035. ClassDB::bind_method(D_METHOD("compress_from_channels", "mode", "channels", "astc_format"), &Image::compress_from_channels, DEFVAL(ASTC_FORMAT_4x4));
  3036. ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress);
  3037. ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed);
  3038. ClassDB::bind_method(D_METHOD("rotate_90", "direction"), &Image::rotate_90);
  3039. ClassDB::bind_method(D_METHOD("rotate_180"), &Image::rotate_180);
  3040. ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges);
  3041. ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha);
  3042. ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear);
  3043. ClassDB::bind_method(D_METHOD("linear_to_srgb"), &Image::linear_to_srgb);
  3044. ClassDB::bind_method(D_METHOD("normal_map_to_xy"), &Image::normal_map_to_xy);
  3045. ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb);
  3046. ClassDB::bind_method(D_METHOD("bump_map_to_normal_map", "bump_scale"), &Image::bump_map_to_normal_map, DEFVAL(1.0));
  3047. ClassDB::bind_method(D_METHOD("compute_image_metrics", "compared_image", "use_luma"), &Image::compute_image_metrics);
  3048. ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect);
  3049. ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask);
  3050. ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect);
  3051. ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask);
  3052. ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill);
  3053. ClassDB::bind_method(D_METHOD("fill_rect", "rect", "color"), &Image::fill_rect);
  3054. ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
  3055. ClassDB::bind_method(D_METHOD("get_region", "region"), &Image::get_region);
  3056. ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from);
  3057. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data);
  3058. ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data);
  3059. ClassDB::bind_method(D_METHOD("get_pixelv", "point"), &Image::get_pixelv);
  3060. ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel);
  3061. ClassDB::bind_method(D_METHOD("set_pixelv", "point", "color"), &Image::set_pixelv);
  3062. ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel);
  3063. ClassDB::bind_method(D_METHOD("adjust_bcs", "brightness", "contrast", "saturation"), &Image::adjust_bcs);
  3064. ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer);
  3065. ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer);
  3066. ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
  3067. ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
  3068. ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer);
  3069. ClassDB::bind_method(D_METHOD("load_ktx_from_buffer", "buffer"), &Image::load_ktx_from_buffer);
  3070. ClassDB::bind_method(D_METHOD("load_svg_from_buffer", "buffer", "scale"), &Image::load_svg_from_buffer, DEFVAL(1.0));
  3071. ClassDB::bind_method(D_METHOD("load_svg_from_string", "svg_str", "scale"), &Image::load_svg_from_string, DEFVAL(1.0));
  3072. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
  3073. BIND_CONSTANT(MAX_WIDTH);
  3074. BIND_CONSTANT(MAX_HEIGHT);
  3075. BIND_ENUM_CONSTANT(FORMAT_L8);
  3076. BIND_ENUM_CONSTANT(FORMAT_LA8);
  3077. BIND_ENUM_CONSTANT(FORMAT_R8);
  3078. BIND_ENUM_CONSTANT(FORMAT_RG8);
  3079. BIND_ENUM_CONSTANT(FORMAT_RGB8);
  3080. BIND_ENUM_CONSTANT(FORMAT_RGBA8);
  3081. BIND_ENUM_CONSTANT(FORMAT_RGBA4444);
  3082. BIND_ENUM_CONSTANT(FORMAT_RGB565);
  3083. BIND_ENUM_CONSTANT(FORMAT_RF);
  3084. BIND_ENUM_CONSTANT(FORMAT_RGF);
  3085. BIND_ENUM_CONSTANT(FORMAT_RGBF);
  3086. BIND_ENUM_CONSTANT(FORMAT_RGBAF);
  3087. BIND_ENUM_CONSTANT(FORMAT_RH);
  3088. BIND_ENUM_CONSTANT(FORMAT_RGH);
  3089. BIND_ENUM_CONSTANT(FORMAT_RGBH);
  3090. BIND_ENUM_CONSTANT(FORMAT_RGBAH);
  3091. BIND_ENUM_CONSTANT(FORMAT_RGBE9995);
  3092. BIND_ENUM_CONSTANT(FORMAT_DXT1);
  3093. BIND_ENUM_CONSTANT(FORMAT_DXT3);
  3094. BIND_ENUM_CONSTANT(FORMAT_DXT5);
  3095. BIND_ENUM_CONSTANT(FORMAT_RGTC_R);
  3096. BIND_ENUM_CONSTANT(FORMAT_RGTC_RG);
  3097. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA);
  3098. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF);
  3099. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU);
  3100. BIND_ENUM_CONSTANT(FORMAT_ETC);
  3101. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11);
  3102. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S);
  3103. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11);
  3104. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S);
  3105. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8);
  3106. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8);
  3107. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1);
  3108. BIND_ENUM_CONSTANT(FORMAT_ETC2_RA_AS_RG);
  3109. BIND_ENUM_CONSTANT(FORMAT_DXT5_RA_AS_RG);
  3110. BIND_ENUM_CONSTANT(FORMAT_ASTC_4x4);
  3111. BIND_ENUM_CONSTANT(FORMAT_ASTC_4x4_HDR);
  3112. BIND_ENUM_CONSTANT(FORMAT_ASTC_8x8);
  3113. BIND_ENUM_CONSTANT(FORMAT_ASTC_8x8_HDR);
  3114. BIND_ENUM_CONSTANT(FORMAT_MAX);
  3115. BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST);
  3116. BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR);
  3117. BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC);
  3118. BIND_ENUM_CONSTANT(INTERPOLATE_TRILINEAR);
  3119. BIND_ENUM_CONSTANT(INTERPOLATE_LANCZOS);
  3120. BIND_ENUM_CONSTANT(ALPHA_NONE);
  3121. BIND_ENUM_CONSTANT(ALPHA_BIT);
  3122. BIND_ENUM_CONSTANT(ALPHA_BLEND);
  3123. BIND_ENUM_CONSTANT(COMPRESS_S3TC);
  3124. BIND_ENUM_CONSTANT(COMPRESS_ETC);
  3125. BIND_ENUM_CONSTANT(COMPRESS_ETC2);
  3126. BIND_ENUM_CONSTANT(COMPRESS_BPTC);
  3127. BIND_ENUM_CONSTANT(COMPRESS_ASTC);
  3128. BIND_ENUM_CONSTANT(COMPRESS_MAX);
  3129. BIND_ENUM_CONSTANT(USED_CHANNELS_L);
  3130. BIND_ENUM_CONSTANT(USED_CHANNELS_LA);
  3131. BIND_ENUM_CONSTANT(USED_CHANNELS_R);
  3132. BIND_ENUM_CONSTANT(USED_CHANNELS_RG);
  3133. BIND_ENUM_CONSTANT(USED_CHANNELS_RGB);
  3134. BIND_ENUM_CONSTANT(USED_CHANNELS_RGBA);
  3135. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC);
  3136. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB);
  3137. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
  3138. BIND_ENUM_CONSTANT(ASTC_FORMAT_4x4);
  3139. BIND_ENUM_CONSTANT(ASTC_FORMAT_8x8);
  3140. }
  3141. void Image::normal_map_to_xy() {
  3142. convert(Image::FORMAT_RGBA8);
  3143. {
  3144. int len = data.size() / 4;
  3145. uint8_t *data_ptr = data.ptrw();
  3146. for (int i = 0; i < len; i++) {
  3147. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  3148. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  3149. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  3150. }
  3151. }
  3152. convert(Image::FORMAT_LA8);
  3153. }
  3154. Ref<Image> Image::rgbe_to_srgb() {
  3155. if (data.size() == 0) {
  3156. return Ref<Image>();
  3157. }
  3158. ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>());
  3159. Ref<Image> new_image = create_empty(width, height, false, Image::FORMAT_RGB8);
  3160. for (int row = 0; row < height; row++) {
  3161. for (int col = 0; col < width; col++) {
  3162. new_image->set_pixel(col, row, get_pixel(col, row).linear_to_srgb());
  3163. }
  3164. }
  3165. if (has_mipmaps()) {
  3166. new_image->generate_mipmaps();
  3167. }
  3168. return new_image;
  3169. }
  3170. Ref<Image> Image::get_image_from_mipmap(int p_mipmap) const {
  3171. int64_t ofs, size;
  3172. int w, h;
  3173. get_mipmap_offset_size_and_dimensions(p_mipmap, ofs, size, w, h);
  3174. Vector<uint8_t> new_data;
  3175. new_data.resize(size);
  3176. {
  3177. uint8_t *wr = new_data.ptrw();
  3178. const uint8_t *rd = data.ptr();
  3179. memcpy(wr, rd + ofs, size);
  3180. }
  3181. Ref<Image> image;
  3182. image.instantiate();
  3183. image->width = w;
  3184. image->height = h;
  3185. image->format = format;
  3186. image->data = new_data;
  3187. image->mipmaps = false;
  3188. return image;
  3189. }
  3190. void Image::bump_map_to_normal_map(float bump_scale) {
  3191. ERR_FAIL_COND(!_can_modify(format));
  3192. clear_mipmaps();
  3193. convert(Image::FORMAT_RF);
  3194. Vector<uint8_t> result_image; //rgba output
  3195. result_image.resize(width * height * 4);
  3196. {
  3197. const uint8_t *rp = data.ptr();
  3198. uint8_t *wp = result_image.ptrw();
  3199. ERR_FAIL_NULL(rp);
  3200. unsigned char *write_ptr = wp;
  3201. float *read_ptr = (float *)rp;
  3202. for (int ty = 0; ty < height; ty++) {
  3203. int py = ty + 1;
  3204. if (py >= height) {
  3205. py -= height;
  3206. }
  3207. for (int tx = 0; tx < width; tx++) {
  3208. int px = tx + 1;
  3209. if (px >= width) {
  3210. px -= width;
  3211. }
  3212. float here = read_ptr[ty * width + tx];
  3213. float to_right = read_ptr[ty * width + px];
  3214. float above = read_ptr[py * width + tx];
  3215. Vector3 up = Vector3(0, 1, (here - above) * bump_scale);
  3216. Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale);
  3217. Vector3 normal = across.cross(up);
  3218. normal.normalize();
  3219. write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5);
  3220. write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5);
  3221. write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5);
  3222. write_ptr[((ty * width + tx) << 2) + 3] = 255;
  3223. }
  3224. }
  3225. }
  3226. format = FORMAT_RGBA8;
  3227. data = result_image;
  3228. }
  3229. bool Image::detect_signed(bool p_include_mips) const {
  3230. ERR_FAIL_COND_V(is_compressed(), false);
  3231. if (format >= Image::FORMAT_RH && format <= Image::FORMAT_RGBAH) {
  3232. const uint16_t *img_data = reinterpret_cast<const uint16_t *>(data.ptr());
  3233. const uint64_t img_size = p_include_mips ? (data.size() / 2) : (width * height * get_format_pixel_size(format) / 2);
  3234. for (uint64_t i = 0; i < img_size; i++) {
  3235. if ((img_data[i] & 0x8000) != 0 && (img_data[i] & 0x7fff) != 0) {
  3236. return true;
  3237. }
  3238. }
  3239. } else if (format >= Image::FORMAT_RF && format <= Image::FORMAT_RGBAF) {
  3240. const uint32_t *img_data = reinterpret_cast<const uint32_t *>(data.ptr());
  3241. const uint64_t img_size = p_include_mips ? (data.size() / 4) : (width * height * get_format_pixel_size(format) / 4);
  3242. for (uint64_t i = 0; i < img_size; i++) {
  3243. if ((img_data[i] & 0x80000000) != 0 && (img_data[i] & 0x7fffffff) != 0) {
  3244. return true;
  3245. }
  3246. }
  3247. }
  3248. return false;
  3249. }
  3250. void Image::srgb_to_linear() {
  3251. if (data.size() == 0) {
  3252. return;
  3253. }
  3254. 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 };
  3255. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  3256. if (format == FORMAT_RGBA8) {
  3257. int len = data.size() / 4;
  3258. uint8_t *data_ptr = data.ptrw();
  3259. for (int i = 0; i < len; i++) {
  3260. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  3261. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  3262. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  3263. }
  3264. } else if (format == FORMAT_RGB8) {
  3265. int len = data.size() / 3;
  3266. uint8_t *data_ptr = data.ptrw();
  3267. for (int i = 0; i < len; i++) {
  3268. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  3269. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  3270. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  3271. }
  3272. }
  3273. }
  3274. void Image::linear_to_srgb() {
  3275. if (data.size() == 0) {
  3276. return;
  3277. }
  3278. static const uint8_t lin2srgb[256] = { 0, 12, 21, 28, 33, 38, 42, 46, 49, 52, 55, 58, 61, 63, 66, 68, 70, 73, 75, 77, 79, 81, 82, 84, 86, 88, 89, 91, 93, 94, 96, 97, 99, 100, 102, 103, 104, 106, 107, 109, 110, 111, 112, 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 151, 152, 153, 154, 155, 156, 157, 157, 158, 159, 160, 161, 161, 162, 163, 164, 165, 165, 166, 167, 168, 168, 169, 170, 171, 171, 172, 173, 174, 174, 175, 176, 176, 177, 178, 179, 179, 180, 181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 193, 193, 194, 194, 195, 196, 196, 197, 197, 198, 199, 199, 200, 201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206, 207, 208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 222, 222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236, 237, 237, 237, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 251, 252, 252, 253, 253, 254, 254, 255 };
  3279. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  3280. if (format == FORMAT_RGBA8) {
  3281. int len = data.size() / 4;
  3282. uint8_t *data_ptr = data.ptrw();
  3283. for (int i = 0; i < len; i++) {
  3284. data_ptr[(i << 2) + 0] = lin2srgb[data_ptr[(i << 2) + 0]];
  3285. data_ptr[(i << 2) + 1] = lin2srgb[data_ptr[(i << 2) + 1]];
  3286. data_ptr[(i << 2) + 2] = lin2srgb[data_ptr[(i << 2) + 2]];
  3287. }
  3288. } else if (format == FORMAT_RGB8) {
  3289. int len = data.size() / 3;
  3290. uint8_t *data_ptr = data.ptrw();
  3291. for (int i = 0; i < len; i++) {
  3292. data_ptr[(i * 3) + 0] = lin2srgb[data_ptr[(i * 3) + 0]];
  3293. data_ptr[(i * 3) + 1] = lin2srgb[data_ptr[(i * 3) + 1]];
  3294. data_ptr[(i * 3) + 2] = lin2srgb[data_ptr[(i * 3) + 2]];
  3295. }
  3296. }
  3297. }
  3298. void Image::premultiply_alpha() {
  3299. if (data.size() == 0) {
  3300. return;
  3301. }
  3302. if (format != FORMAT_RGBA8) {
  3303. return; //not needed
  3304. }
  3305. uint8_t *data_ptr = data.ptrw();
  3306. for (int i = 0; i < height; i++) {
  3307. for (int j = 0; j < width; j++) {
  3308. uint8_t *ptr = &data_ptr[(i * width + j) * 4];
  3309. ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3]) + 255U) >> 8;
  3310. ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3]) + 255U) >> 8;
  3311. ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3]) + 255U) >> 8;
  3312. }
  3313. }
  3314. }
  3315. void Image::fix_alpha_edges() {
  3316. if (data.size() == 0) {
  3317. return;
  3318. }
  3319. if (format != FORMAT_RGBA8) {
  3320. return; //not needed
  3321. }
  3322. Vector<uint8_t> dcopy = data;
  3323. const uint8_t *srcptr = dcopy.ptr();
  3324. uint8_t *data_ptr = data.ptrw();
  3325. const int max_radius = 4;
  3326. const int alpha_threshold = 20;
  3327. const int max_dist = 0x7FFFFFFF;
  3328. for (int i = 0; i < height; i++) {
  3329. for (int j = 0; j < width; j++) {
  3330. const uint8_t *rptr = &srcptr[(i * width + j) * 4];
  3331. uint8_t *wptr = &data_ptr[(i * width + j) * 4];
  3332. if (rptr[3] >= alpha_threshold) {
  3333. continue;
  3334. }
  3335. int closest_dist = max_dist;
  3336. uint8_t closest_color[3] = { 0 };
  3337. int from_x = MAX(0, j - max_radius);
  3338. int to_x = MIN(width - 1, j + max_radius);
  3339. int from_y = MAX(0, i - max_radius);
  3340. int to_y = MIN(height - 1, i + max_radius);
  3341. for (int k = from_y; k <= to_y; k++) {
  3342. for (int l = from_x; l <= to_x; l++) {
  3343. int dy = i - k;
  3344. int dx = j - l;
  3345. int dist = dy * dy + dx * dx;
  3346. if (dist >= closest_dist) {
  3347. continue;
  3348. }
  3349. const uint8_t *rp2 = &srcptr[(k * width + l) << 2];
  3350. if (rp2[3] < alpha_threshold) {
  3351. continue;
  3352. }
  3353. closest_dist = dist;
  3354. closest_color[0] = rp2[0];
  3355. closest_color[1] = rp2[1];
  3356. closest_color[2] = rp2[2];
  3357. }
  3358. }
  3359. if (closest_dist != max_dist) {
  3360. wptr[0] = closest_color[0];
  3361. wptr[1] = closest_color[1];
  3362. wptr[2] = closest_color[2];
  3363. }
  3364. }
  3365. }
  3366. }
  3367. String Image::get_format_name(Format p_format) {
  3368. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  3369. return format_names[p_format];
  3370. }
  3371. uint32_t Image::get_format_component_mask(Format p_format) {
  3372. const uint32_t r = 1;
  3373. const uint32_t rg = 3;
  3374. const uint32_t rgb = 7;
  3375. const uint32_t rgba = 15;
  3376. switch (p_format) {
  3377. case FORMAT_L8:
  3378. return rgb;
  3379. case FORMAT_LA8:
  3380. return rgba;
  3381. case FORMAT_R8:
  3382. return r;
  3383. case FORMAT_RG8:
  3384. return rg;
  3385. case FORMAT_RGB8:
  3386. return rgb;
  3387. case FORMAT_RGBA8:
  3388. return rgba;
  3389. case FORMAT_RGBA4444:
  3390. return rgba;
  3391. case FORMAT_RGB565:
  3392. return rgb;
  3393. case FORMAT_RF:
  3394. return r;
  3395. case FORMAT_RGF:
  3396. return rg;
  3397. case FORMAT_RGBF:
  3398. return rgb;
  3399. case FORMAT_RGBAF:
  3400. return rgba;
  3401. case FORMAT_RH:
  3402. return r;
  3403. case FORMAT_RGH:
  3404. return rg;
  3405. case FORMAT_RGBH:
  3406. return rgb;
  3407. case FORMAT_RGBAH:
  3408. return rgba;
  3409. case FORMAT_RGBE9995:
  3410. return rgb;
  3411. case FORMAT_DXT1:
  3412. return rgb;
  3413. case FORMAT_DXT3:
  3414. return rgba;
  3415. case FORMAT_DXT5:
  3416. return rgba;
  3417. case FORMAT_RGTC_R:
  3418. return r;
  3419. case FORMAT_RGTC_RG:
  3420. return rg;
  3421. case FORMAT_BPTC_RGBA:
  3422. return rgba;
  3423. case FORMAT_BPTC_RGBF:
  3424. return rgb;
  3425. case FORMAT_BPTC_RGBFU:
  3426. return rgb;
  3427. case FORMAT_ETC:
  3428. return rgb;
  3429. case FORMAT_ETC2_R11:
  3430. return r;
  3431. case FORMAT_ETC2_R11S:
  3432. return r;
  3433. case FORMAT_ETC2_RG11:
  3434. return rg;
  3435. case FORMAT_ETC2_RG11S:
  3436. return rg;
  3437. case FORMAT_ETC2_RGB8:
  3438. return rgb;
  3439. case FORMAT_ETC2_RGBA8:
  3440. return rgba;
  3441. case FORMAT_ETC2_RGB8A1:
  3442. return rgba;
  3443. case FORMAT_ETC2_RA_AS_RG:
  3444. return rg;
  3445. case FORMAT_DXT5_RA_AS_RG:
  3446. return rg;
  3447. case FORMAT_ASTC_4x4:
  3448. return rgba;
  3449. case FORMAT_ASTC_4x4_HDR:
  3450. return rgba;
  3451. case FORMAT_ASTC_8x8:
  3452. return rgba;
  3453. case FORMAT_ASTC_8x8_HDR:
  3454. return rgba;
  3455. default:
  3456. ERR_PRINT("Unhandled format.");
  3457. return rgba;
  3458. }
  3459. }
  3460. Error Image::load_png_from_buffer(const Vector<uint8_t> &p_array) {
  3461. return _load_from_buffer(p_array, _png_mem_loader_func);
  3462. }
  3463. Error Image::load_jpg_from_buffer(const Vector<uint8_t> &p_array) {
  3464. return _load_from_buffer(p_array, _jpg_mem_loader_func);
  3465. }
  3466. Error Image::load_webp_from_buffer(const Vector<uint8_t> &p_array) {
  3467. return _load_from_buffer(p_array, _webp_mem_loader_func);
  3468. }
  3469. Error Image::load_tga_from_buffer(const Vector<uint8_t> &p_array) {
  3470. ERR_FAIL_NULL_V_MSG(
  3471. _tga_mem_loader_func,
  3472. ERR_UNAVAILABLE,
  3473. "The TGA module isn't enabled. Recompile the Godot editor or export template binary with the `module_tga_enabled=yes` SCons option.");
  3474. return _load_from_buffer(p_array, _tga_mem_loader_func);
  3475. }
  3476. Error Image::load_bmp_from_buffer(const Vector<uint8_t> &p_array) {
  3477. ERR_FAIL_NULL_V_MSG(
  3478. _bmp_mem_loader_func,
  3479. ERR_UNAVAILABLE,
  3480. "The BMP module isn't enabled. Recompile the Godot editor or export template binary with the `module_bmp_enabled=yes` SCons option.");
  3481. return _load_from_buffer(p_array, _bmp_mem_loader_func);
  3482. }
  3483. Error Image::load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale) {
  3484. ERR_FAIL_NULL_V_MSG(
  3485. _svg_scalable_mem_loader_func,
  3486. ERR_UNAVAILABLE,
  3487. "The SVG module isn't enabled. Recompile the Godot editor or export template binary with the `module_svg_enabled=yes` SCons option.");
  3488. int buffer_size = p_array.size();
  3489. ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
  3490. Ref<Image> image = _svg_scalable_mem_loader_func(p_array.ptr(), buffer_size, scale);
  3491. ERR_FAIL_COND_V(image.is_null(), ERR_PARSE_ERROR);
  3492. copy_internals_from(image);
  3493. return OK;
  3494. }
  3495. Error Image::load_svg_from_string(const String &p_svg_str, float scale) {
  3496. return load_svg_from_buffer(p_svg_str.to_utf8_buffer(), scale);
  3497. }
  3498. Error Image::load_ktx_from_buffer(const Vector<uint8_t> &p_array) {
  3499. ERR_FAIL_NULL_V_MSG(
  3500. _ktx_mem_loader_func,
  3501. ERR_UNAVAILABLE,
  3502. "The KTX module isn't enabled. Recompile the Godot editor or export template binary with the `module_ktx_enabled=yes` SCons option.");
  3503. return _load_from_buffer(p_array, _ktx_mem_loader_func);
  3504. }
  3505. void Image::convert_rg_to_ra_rgba8() {
  3506. ERR_FAIL_COND(format != FORMAT_RGBA8);
  3507. ERR_FAIL_COND(data.is_empty());
  3508. int s = data.size();
  3509. uint8_t *w = data.ptrw();
  3510. for (int i = 0; i < s; i += 4) {
  3511. w[i + 3] = w[i + 1];
  3512. w[i + 1] = 0;
  3513. w[i + 2] = 0;
  3514. }
  3515. }
  3516. void Image::convert_ra_rgba8_to_rg() {
  3517. ERR_FAIL_COND(format != FORMAT_RGBA8);
  3518. ERR_FAIL_COND(data.is_empty());
  3519. int s = data.size();
  3520. uint8_t *w = data.ptrw();
  3521. for (int i = 0; i < s; i += 4) {
  3522. w[i + 1] = w[i + 3];
  3523. w[i + 2] = 0;
  3524. w[i + 3] = 255;
  3525. }
  3526. }
  3527. void Image::convert_rgba8_to_bgra8() {
  3528. ERR_FAIL_COND(format != FORMAT_RGBA8);
  3529. ERR_FAIL_COND(data.is_empty());
  3530. int s = data.size();
  3531. uint8_t *w = data.ptrw();
  3532. for (int i = 0; i < s; i += 4) {
  3533. uint8_t r = w[i];
  3534. w[i] = w[i + 2]; // Swap R to B
  3535. w[i + 2] = r; // Swap B to R
  3536. }
  3537. }
  3538. Error Image::_load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
  3539. int buffer_size = p_array.size();
  3540. ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
  3541. ERR_FAIL_NULL_V(p_loader, ERR_INVALID_PARAMETER);
  3542. const uint8_t *r = p_array.ptr();
  3543. Ref<Image> image = p_loader(r, buffer_size);
  3544. ERR_FAIL_COND_V(image.is_null(), ERR_PARSE_ERROR);
  3545. copy_internals_from(image);
  3546. return OK;
  3547. }
  3548. 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) {
  3549. p_out = static_cast<uint8_t>((p_a + p_b + p_c + p_d + 2) >> 2);
  3550. }
  3551. void Image::average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d) {
  3552. p_out = (p_a + p_b + p_c + p_d) * 0.25f;
  3553. }
  3554. 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) {
  3555. 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);
  3556. }
  3557. 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) {
  3558. 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();
  3559. }
  3560. void Image::renormalize_uint8(uint8_t *p_rgb) {
  3561. Vector3 n(p_rgb[0] / 255.0, p_rgb[1] / 255.0, p_rgb[2] / 255.0);
  3562. n *= 2.0;
  3563. n -= Vector3(1, 1, 1);
  3564. n.normalize();
  3565. n += Vector3(1, 1, 1);
  3566. n *= 0.5;
  3567. n *= 255;
  3568. p_rgb[0] = CLAMP(int(n.x), 0, 255);
  3569. p_rgb[1] = CLAMP(int(n.y), 0, 255);
  3570. p_rgb[2] = CLAMP(int(n.z), 0, 255);
  3571. }
  3572. void Image::renormalize_float(float *p_rgb) {
  3573. Vector3 n(p_rgb[0], p_rgb[1], p_rgb[2]);
  3574. n.normalize();
  3575. p_rgb[0] = n.x;
  3576. p_rgb[1] = n.y;
  3577. p_rgb[2] = n.z;
  3578. }
  3579. void Image::renormalize_half(uint16_t *p_rgb) {
  3580. Vector3 n(Math::half_to_float(p_rgb[0]), Math::half_to_float(p_rgb[1]), Math::half_to_float(p_rgb[2]));
  3581. n.normalize();
  3582. p_rgb[0] = Math::make_half_float(n.x);
  3583. p_rgb[1] = Math::make_half_float(n.y);
  3584. p_rgb[2] = Math::make_half_float(n.z);
  3585. }
  3586. void Image::renormalize_rgbe9995(uint32_t *p_rgb) {
  3587. // Never used.
  3588. }
  3589. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  3590. width = 0;
  3591. height = 0;
  3592. mipmaps = false;
  3593. format = FORMAT_L8;
  3594. if (_png_mem_loader_func) {
  3595. copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len));
  3596. }
  3597. if (is_empty() && _jpg_mem_loader_func) {
  3598. copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len));
  3599. }
  3600. if (is_empty() && _webp_mem_loader_func) {
  3601. copy_internals_from(_webp_mem_loader_func(p_mem_png_jpg, p_len));
  3602. }
  3603. }
  3604. Ref<Resource> Image::duplicate(bool p_subresources) const {
  3605. Ref<Image> copy;
  3606. copy.instantiate();
  3607. copy->_copy_internals_from(*this);
  3608. return copy;
  3609. }
  3610. void Image::set_as_black() {
  3611. memset(data.ptrw(), 0, data.size());
  3612. }
  3613. void Image::copy_internals_from(const Ref<Image> &p_image) {
  3614. ERR_FAIL_COND_MSG(p_image.is_null(), "Cannot copy image internals: invalid Image object.");
  3615. format = p_image->format;
  3616. width = p_image->width;
  3617. height = p_image->height;
  3618. mipmaps = p_image->mipmaps;
  3619. data = p_image->data;
  3620. }
  3621. Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool p_luma_metric) {
  3622. // https://github.com/richgel999/bc7enc_rdo/blob/master/LICENSE
  3623. //
  3624. // This is free and unencumbered software released into the public domain.
  3625. // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  3626. // software, either in source code form or as a compiled binary, for any purpose,
  3627. // commercial or non - commercial, and by any means.
  3628. // In jurisdictions that recognize copyright laws, the author or authors of this
  3629. // software dedicate any and all copyright interest in the software to the public
  3630. // domain. We make this dedication for the benefit of the public at large and to
  3631. // the detriment of our heirs and successors. We intend this dedication to be an
  3632. // overt act of relinquishment in perpetuity of all present and future rights to
  3633. // this software under copyright law.
  3634. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3635. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3636. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  3637. // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  3638. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  3639. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3640. Dictionary result;
  3641. result["max"] = INFINITY;
  3642. result["mean"] = INFINITY;
  3643. result["mean_squared"] = INFINITY;
  3644. result["root_mean_squared"] = INFINITY;
  3645. result["peak_snr"] = 0.0f;
  3646. ERR_FAIL_COND_V(p_compared_image.is_null(), result);
  3647. Error err = OK;
  3648. Ref<Image> compared_image = duplicate(true);
  3649. if (compared_image->is_compressed()) {
  3650. err = compared_image->decompress();
  3651. }
  3652. ERR_FAIL_COND_V(err != OK, result);
  3653. Ref<Image> source_image = p_compared_image->duplicate(true);
  3654. if (source_image->is_compressed()) {
  3655. err = source_image->decompress();
  3656. }
  3657. ERR_FAIL_COND_V(err != OK, result);
  3658. ERR_FAIL_COND_V_MSG((compared_image->get_format() >= Image::FORMAT_RH) && (compared_image->get_format() <= Image::FORMAT_RGBE9995), result, "Metrics on HDR images are not supported.");
  3659. ERR_FAIL_COND_V_MSG((source_image->get_format() >= Image::FORMAT_RH) && (source_image->get_format() <= Image::FORMAT_RGBE9995), result, "Metrics on HDR images are not supported.");
  3660. double image_metric_max, image_metric_mean, image_metric_mean_squared, image_metric_root_mean_squared, image_metric_peak_snr = 0.0;
  3661. const bool average_component_error = true;
  3662. const uint32_t w = MIN(compared_image->get_width(), source_image->get_width());
  3663. const uint32_t h = MIN(compared_image->get_height(), source_image->get_height());
  3664. // Histogram approach originally due to Charles Bloom.
  3665. double hist[256];
  3666. memset(hist, 0, sizeof(hist));
  3667. for (uint32_t y = 0; y < h; y++) {
  3668. for (uint32_t x = 0; x < w; x++) {
  3669. const Color color_a = compared_image->get_pixel(x, y);
  3670. const Color color_b = source_image->get_pixel(x, y);
  3671. if (!p_luma_metric) {
  3672. ERR_FAIL_COND_V_MSG(color_a.r > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3673. ERR_FAIL_COND_V_MSG(color_b.r > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3674. hist[Math::abs(color_a.get_r8() - color_b.get_r8())]++;
  3675. ERR_FAIL_COND_V_MSG(color_a.g > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3676. ERR_FAIL_COND_V_MSG(color_b.g > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3677. hist[Math::abs(color_a.get_g8() - color_b.get_g8())]++;
  3678. ERR_FAIL_COND_V_MSG(color_a.b > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3679. ERR_FAIL_COND_V_MSG(color_b.b > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3680. hist[Math::abs(color_a.get_b8() - color_b.get_b8())]++;
  3681. ERR_FAIL_COND_V_MSG(color_a.a > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3682. ERR_FAIL_COND_V_MSG(color_b.a > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3683. hist[Math::abs(color_a.get_a8() - color_b.get_a8())]++;
  3684. } else {
  3685. ERR_FAIL_COND_V_MSG(color_a.r > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3686. ERR_FAIL_COND_V_MSG(color_b.r > 1.0f, Dictionary(), "Can't compare HDR colors.");
  3687. // REC709 weightings
  3688. int luma_a = (13938U * color_a.get_r8() + 46869U * color_a.get_g8() + 4729U * color_a.get_b8() + 32768U) >> 16U;
  3689. int luma_b = (13938U * color_b.get_r8() + 46869U * color_b.get_g8() + 4729U * color_b.get_b8() + 32768U) >> 16U;
  3690. hist[Math::abs(luma_a - luma_b)]++;
  3691. }
  3692. }
  3693. }
  3694. image_metric_max = 0;
  3695. double sum = 0.0f, sum2 = 0.0f;
  3696. for (uint32_t i = 0; i < 256; i++) {
  3697. if (!hist[i]) {
  3698. continue;
  3699. }
  3700. image_metric_max = i;
  3701. double x = i * hist[i];
  3702. sum += x;
  3703. sum2 += i * x;
  3704. }
  3705. // See http://richg42.blogspot.com/2016/09/how-to-compute-psnr-from-old-berkeley.html
  3706. double total_values = w * h;
  3707. if (average_component_error) {
  3708. total_values *= 4;
  3709. }
  3710. image_metric_mean = CLAMP(sum / total_values, 0.0f, 255.0f);
  3711. image_metric_mean_squared = CLAMP(sum2 / total_values, 0.0f, 255.0f * 255.0f);
  3712. image_metric_root_mean_squared = sqrt(image_metric_mean_squared);
  3713. if (!image_metric_root_mean_squared) {
  3714. image_metric_peak_snr = 1e+10f;
  3715. } else {
  3716. image_metric_peak_snr = CLAMP(log10(255.0f / image_metric_root_mean_squared) * 20.0f, 0.0f, 500.0f);
  3717. }
  3718. result["max"] = image_metric_max;
  3719. result["mean"] = image_metric_mean;
  3720. result["mean_squared"] = image_metric_mean_squared;
  3721. result["root_mean_squared"] = image_metric_root_mean_squared;
  3722. result["peak_snr"] = image_metric_peak_snr;
  3723. return result;
  3724. }