image.cpp 108 KB

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