image.cpp 90 KB

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