image.cpp 104 KB

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