image.cpp 104 KB

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