image.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044
  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. void Image::resize_to_po2(bool p_square) {
  588. if (!_can_modify(format)) {
  589. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  590. ERR_FAIL();
  591. }
  592. int w = next_power_of_2(width);
  593. int h = next_power_of_2(height);
  594. if (w == width && h == height) {
  595. if (!p_square || w == h)
  596. return; //nothing to do
  597. }
  598. resize(w, h);
  599. }
  600. void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
  601. if (data.size() == 0) {
  602. ERR_EXPLAIN("Cannot resize image before creating it, use create() or create_from_data() first.");
  603. ERR_FAIL();
  604. }
  605. if (!_can_modify(format)) {
  606. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  607. ERR_FAIL();
  608. }
  609. bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */;
  610. ERR_FAIL_COND(p_width <= 0);
  611. ERR_FAIL_COND(p_height <= 0);
  612. ERR_FAIL_COND(p_width > MAX_WIDTH);
  613. ERR_FAIL_COND(p_height > MAX_HEIGHT);
  614. if (p_width == width && p_height == height)
  615. return;
  616. Image dst(p_width, p_height, 0, format);
  617. // Setup mipmap-aware scaling
  618. Image dst2;
  619. int mip1 = 0;
  620. int mip2 = 0;
  621. float mip1_weight = 0;
  622. if (mipmap_aware) {
  623. float avg_scale = ((float)p_width / width + (float)p_height / height) * 0.5f;
  624. if (avg_scale >= 1.0f) {
  625. mipmap_aware = false;
  626. } else {
  627. float level = Math::log(1.0f / avg_scale) / Math::log(2.0f);
  628. mip1 = CLAMP((int)Math::floor(level), 0, get_mipmap_count());
  629. mip2 = CLAMP((int)Math::ceil(level), 0, get_mipmap_count());
  630. mip1_weight = 1.0f - (level - mip1);
  631. }
  632. }
  633. bool interpolate_mipmaps = mipmap_aware && mip1 != mip2;
  634. if (interpolate_mipmaps) {
  635. dst2.create(p_width, p_height, 0, format);
  636. }
  637. bool had_mipmaps = mipmaps;
  638. if (interpolate_mipmaps && !had_mipmaps) {
  639. generate_mipmaps();
  640. }
  641. // --
  642. PoolVector<uint8_t>::Read r = data.read();
  643. const unsigned char *r_ptr = r.ptr();
  644. PoolVector<uint8_t>::Write w = dst.data.write();
  645. unsigned char *w_ptr = w.ptr();
  646. switch (p_interpolation) {
  647. case INTERPOLATE_NEAREST: {
  648. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  649. switch (get_format_pixel_size(format)) {
  650. case 1: _scale_nearest<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  651. case 2: _scale_nearest<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  652. case 3: _scale_nearest<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  653. case 4: _scale_nearest<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  654. }
  655. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  656. switch (get_format_pixel_size(format)) {
  657. case 4: _scale_nearest<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  658. case 8: _scale_nearest<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  659. case 12: _scale_nearest<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  660. case 16: _scale_nearest<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  661. }
  662. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  663. switch (get_format_pixel_size(format)) {
  664. case 2: _scale_nearest<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  665. case 4: _scale_nearest<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  666. case 6: _scale_nearest<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  667. case 8: _scale_nearest<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  668. }
  669. }
  670. } break;
  671. case INTERPOLATE_BILINEAR:
  672. case INTERPOLATE_TRILINEAR: {
  673. for (int i = 0; i < 2; ++i) {
  674. int src_width;
  675. int src_height;
  676. const unsigned char *src_ptr;
  677. if (!mipmap_aware) {
  678. if (i == 0) {
  679. // Standard behavior
  680. src_width = width;
  681. src_height = height;
  682. src_ptr = r_ptr;
  683. } else {
  684. // No need for a second iteration
  685. break;
  686. }
  687. } else {
  688. if (i == 0) {
  689. // Read from the first mipmap that will be interpolated
  690. // (if both levels are the same, we will not interpolate, but at least we'll sample from the right level)
  691. int offs;
  692. _get_mipmap_offset_and_size(mip1, offs, src_width, src_height);
  693. src_ptr = r_ptr + offs;
  694. } else if (!interpolate_mipmaps) {
  695. // No need generate a second image
  696. break;
  697. } else {
  698. // Switch to read from the second mipmap that will be interpolated
  699. int offs;
  700. _get_mipmap_offset_and_size(mip2, offs, src_width, src_height);
  701. src_ptr = r_ptr + offs;
  702. // Switch to write to the second destination image
  703. w = dst2.data.write();
  704. w_ptr = w.ptr();
  705. }
  706. }
  707. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  708. switch (get_format_pixel_size(format)) {
  709. case 1: _scale_bilinear<1, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  710. case 2: _scale_bilinear<2, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  711. case 3: _scale_bilinear<3, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  712. case 4: _scale_bilinear<4, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  713. }
  714. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  715. switch (get_format_pixel_size(format)) {
  716. case 4: _scale_bilinear<1, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  717. case 8: _scale_bilinear<2, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  718. case 12: _scale_bilinear<3, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  719. case 16: _scale_bilinear<4, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  720. }
  721. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  722. switch (get_format_pixel_size(format)) {
  723. case 2: _scale_bilinear<1, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  724. case 4: _scale_bilinear<2, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  725. case 6: _scale_bilinear<3, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  726. case 8: _scale_bilinear<4, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
  727. }
  728. }
  729. }
  730. if (interpolate_mipmaps) {
  731. // Switch to read again from the first scaled mipmap to overlay it over the second
  732. r = dst.data.read();
  733. _overlay(r.ptr(), w.ptr(), mip1_weight, p_width, p_height, get_format_pixel_size(format));
  734. }
  735. } break;
  736. case INTERPOLATE_CUBIC: {
  737. if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
  738. switch (get_format_pixel_size(format)) {
  739. case 1: _scale_cubic<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  740. case 2: _scale_cubic<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  741. case 3: _scale_cubic<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  742. case 4: _scale_cubic<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  743. }
  744. } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
  745. switch (get_format_pixel_size(format)) {
  746. case 4: _scale_cubic<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  747. case 8: _scale_cubic<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  748. case 12: _scale_cubic<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  749. case 16: _scale_cubic<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  750. }
  751. } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
  752. switch (get_format_pixel_size(format)) {
  753. case 2: _scale_cubic<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  754. case 4: _scale_cubic<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  755. case 6: _scale_cubic<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  756. case 8: _scale_cubic<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  757. }
  758. }
  759. } break;
  760. }
  761. r = PoolVector<uint8_t>::Read();
  762. w = PoolVector<uint8_t>::Write();
  763. if (interpolate_mipmaps) {
  764. dst._copy_internals_from(dst2);
  765. }
  766. if (had_mipmaps)
  767. dst.generate_mipmaps();
  768. _copy_internals_from(dst);
  769. }
  770. void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) {
  771. if (!_can_modify(format)) {
  772. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  773. ERR_FAIL();
  774. }
  775. ERR_FAIL_COND(p_x < 0);
  776. ERR_FAIL_COND(p_y < 0);
  777. ERR_FAIL_COND(p_width <= 0);
  778. ERR_FAIL_COND(p_height <= 0);
  779. ERR_FAIL_COND(p_x + p_width > MAX_WIDTH);
  780. ERR_FAIL_COND(p_y + p_height > MAX_HEIGHT);
  781. /* to save memory, cropping should be done in-place, however, since this function
  782. will most likely either not be used much, or in critical areas, for now it won't, because
  783. it's a waste of time. */
  784. if (p_width == width && p_height == height && p_x == 0 && p_y == 0)
  785. return;
  786. uint8_t pdata[16]; //largest is 16
  787. uint32_t pixel_size = get_format_pixel_size(format);
  788. Image dst(p_width, p_height, 0, format);
  789. {
  790. PoolVector<uint8_t>::Read r = data.read();
  791. PoolVector<uint8_t>::Write w = dst.data.write();
  792. int m_h = p_y + p_height;
  793. int m_w = p_x + p_width;
  794. for (int y = p_y; y < m_h; y++) {
  795. for (int x = p_x; x < m_w; x++) {
  796. if ((x >= width || y >= height)) {
  797. for (uint32_t i = 0; i < pixel_size; i++)
  798. pdata[i] = 0;
  799. } else {
  800. _get_pixelb(x, y, pixel_size, r.ptr(), pdata);
  801. }
  802. dst._put_pixelb(x - p_x, y - p_y, pixel_size, w.ptr(), pdata);
  803. }
  804. }
  805. }
  806. if (has_mipmaps())
  807. dst.generate_mipmaps();
  808. _copy_internals_from(dst);
  809. }
  810. void Image::crop(int p_width, int p_height) {
  811. crop_from_point(0, 0, p_width, p_height);
  812. }
  813. void Image::flip_y() {
  814. if (!_can_modify(format)) {
  815. ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats.");
  816. ERR_FAIL();
  817. }
  818. bool used_mipmaps = has_mipmaps();
  819. if (used_mipmaps) {
  820. clear_mipmaps();
  821. }
  822. {
  823. PoolVector<uint8_t>::Write w = data.write();
  824. uint8_t up[16];
  825. uint8_t down[16];
  826. uint32_t pixel_size = get_format_pixel_size(format);
  827. for (int y = 0; y < height / 2; y++) {
  828. for (int x = 0; x < width; x++) {
  829. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  830. _get_pixelb(x, height - y - 1, pixel_size, w.ptr(), down);
  831. _put_pixelb(x, height - y - 1, pixel_size, w.ptr(), up);
  832. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  833. }
  834. }
  835. }
  836. if (used_mipmaps) {
  837. generate_mipmaps();
  838. }
  839. }
  840. void Image::flip_x() {
  841. if (!_can_modify(format)) {
  842. ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats.");
  843. ERR_FAIL();
  844. }
  845. bool used_mipmaps = has_mipmaps();
  846. if (used_mipmaps) {
  847. clear_mipmaps();
  848. }
  849. {
  850. PoolVector<uint8_t>::Write w = data.write();
  851. uint8_t up[16];
  852. uint8_t down[16];
  853. uint32_t pixel_size = get_format_pixel_size(format);
  854. for (int y = 0; y < height; y++) {
  855. for (int x = 0; x < width / 2; x++) {
  856. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  857. _get_pixelb(width - x - 1, y, pixel_size, w.ptr(), down);
  858. _put_pixelb(width - x - 1, y, pixel_size, w.ptr(), up);
  859. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  860. }
  861. }
  862. }
  863. if (used_mipmaps) {
  864. generate_mipmaps();
  865. }
  866. }
  867. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps) {
  868. int size = 0;
  869. int w = p_width;
  870. int h = p_height;
  871. int mm = 0;
  872. int pixsize = get_format_pixel_size(p_format);
  873. int pixshift = get_format_pixel_rshift(p_format);
  874. int block = get_format_block_size(p_format);
  875. //technically, you can still compress up to 1 px no matter the format, so commenting this
  876. //int minw, minh;
  877. //get_format_min_pixel_size(p_format, minw, minh);
  878. int minw = 1, minh = 1;
  879. while (true) {
  880. int bw = w % block != 0 ? w + (block - w % block) : w;
  881. int bh = h % block != 0 ? h + (block - h % block) : h;
  882. int s = bw * bh;
  883. s *= pixsize;
  884. s >>= pixshift;
  885. size += s;
  886. if (p_mipmaps >= 0 && mm == p_mipmaps)
  887. break;
  888. if (p_mipmaps >= 0) {
  889. w = MAX(minw, w >> 1);
  890. h = MAX(minh, h >> 1);
  891. } else {
  892. if (w == minw && h == minh)
  893. break;
  894. w = MAX(minw, w >> 1);
  895. h = MAX(minh, h >> 1);
  896. }
  897. mm++;
  898. };
  899. r_mipmaps = mm;
  900. return size;
  901. }
  902. bool Image::_can_modify(Format p_format) const {
  903. return p_format <= FORMAT_RGBE9995;
  904. }
  905. template <class Component, int CC, bool renormalize,
  906. void (*average_func)(Component &, const Component &, const Component &, const Component &, const Component &),
  907. void (*renormalize_func)(Component *)>
  908. static void _generate_po2_mipmap(const Component *p_src, Component *p_dst, uint32_t p_width, uint32_t p_height) {
  909. //fast power of 2 mipmap generation
  910. uint32_t dst_w = MAX(p_width >> 1, 1);
  911. uint32_t dst_h = MAX(p_height >> 1, 1);
  912. int right_step = (p_width == 1) ? 0 : CC;
  913. int down_step = (p_height == 1) ? 0 : (p_width * CC);
  914. for (uint32_t i = 0; i < dst_h; i++) {
  915. const Component *rup_ptr = &p_src[i * 2 * down_step];
  916. const Component *rdown_ptr = rup_ptr + down_step;
  917. Component *dst_ptr = &p_dst[i * dst_w * CC];
  918. uint32_t count = dst_w;
  919. while (count--) {
  920. for (int j = 0; j < CC; j++) {
  921. average_func(dst_ptr[j], rup_ptr[j], rup_ptr[j + right_step], rdown_ptr[j], rdown_ptr[j + right_step]);
  922. }
  923. if (renormalize) {
  924. renormalize_func(dst_ptr);
  925. }
  926. dst_ptr += CC;
  927. rup_ptr += right_step * 2;
  928. rdown_ptr += right_step * 2;
  929. }
  930. }
  931. }
  932. void Image::expand_x2_hq2x() {
  933. ERR_FAIL_COND(!_can_modify(format));
  934. bool used_mipmaps = has_mipmaps();
  935. if (used_mipmaps) {
  936. clear_mipmaps();
  937. }
  938. Format current = format;
  939. if (current != FORMAT_RGBA8)
  940. convert(FORMAT_RGBA8);
  941. PoolVector<uint8_t> dest;
  942. dest.resize(width * 2 * height * 2 * 4);
  943. {
  944. PoolVector<uint8_t>::Read r = data.read();
  945. PoolVector<uint8_t>::Write w = dest.write();
  946. hq2x_resize((const uint32_t *)r.ptr(), width, height, (uint32_t *)w.ptr());
  947. }
  948. width *= 2;
  949. height *= 2;
  950. data = dest;
  951. if (current != FORMAT_RGBA8)
  952. convert(current);
  953. // FIXME: This is likely meant to use "used_mipmaps" as defined above, but if we do,
  954. // we end up with a regression: GH-22747
  955. if (mipmaps) {
  956. generate_mipmaps();
  957. }
  958. }
  959. void Image::shrink_x2() {
  960. ERR_FAIL_COND(data.size() == 0);
  961. if (mipmaps) {
  962. //just use the lower mipmap as base and copy all
  963. PoolVector<uint8_t> new_img;
  964. int ofs = get_mipmap_offset(1);
  965. int new_size = data.size() - ofs;
  966. new_img.resize(new_size);
  967. {
  968. PoolVector<uint8_t>::Write w = new_img.write();
  969. PoolVector<uint8_t>::Read r = data.read();
  970. copymem(w.ptr(), &r[ofs], new_size);
  971. }
  972. width = MAX(width / 2, 1);
  973. height = MAX(height / 2, 1);
  974. data = new_img;
  975. } else {
  976. PoolVector<uint8_t> new_img;
  977. ERR_FAIL_COND(!_can_modify(format));
  978. int ps = get_format_pixel_size(format);
  979. new_img.resize((width / 2) * (height / 2) * ps);
  980. {
  981. PoolVector<uint8_t>::Write w = new_img.write();
  982. PoolVector<uint8_t>::Read r = data.read();
  983. switch (format) {
  984. case FORMAT_L8:
  985. case FORMAT_R8: _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height); break;
  986. case FORMAT_LA8: _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height); break;
  987. case FORMAT_RG8: _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height); break;
  988. case FORMAT_RGB8: _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height); break;
  989. case FORMAT_RGBA8: _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(r.ptr(), w.ptr(), width, height); break;
  990. 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;
  991. 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;
  992. 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;
  993. 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;
  994. 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;
  995. 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;
  996. 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;
  997. 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;
  998. 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;
  999. default: {}
  1000. }
  1001. }
  1002. width /= 2;
  1003. height /= 2;
  1004. data = new_img;
  1005. }
  1006. }
  1007. void Image::normalize() {
  1008. bool used_mipmaps = has_mipmaps();
  1009. if (used_mipmaps) {
  1010. clear_mipmaps();
  1011. }
  1012. lock();
  1013. for (int y = 0; y < height; y++) {
  1014. for (int x = 0; x < width; x++) {
  1015. Color c = get_pixel(x, y);
  1016. Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0);
  1017. v.normalize();
  1018. c.r = v.x * 0.5 + 0.5;
  1019. c.g = v.y * 0.5 + 0.5;
  1020. c.b = v.z * 0.5 + 0.5;
  1021. set_pixel(x, y, c);
  1022. }
  1023. }
  1024. unlock();
  1025. if (used_mipmaps) {
  1026. generate_mipmaps(true);
  1027. }
  1028. }
  1029. Error Image::generate_mipmaps(bool p_renormalize) {
  1030. if (!_can_modify(format)) {
  1031. ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats.");
  1032. ERR_FAIL_V(ERR_UNAVAILABLE);
  1033. }
  1034. ERR_FAIL_COND_V(width == 0 || height == 0, ERR_UNCONFIGURED);
  1035. int mmcount;
  1036. int size = _get_dst_image_size(width, height, format, mmcount);
  1037. data.resize(size);
  1038. PoolVector<uint8_t>::Write wp = data.write();
  1039. int prev_ofs = 0;
  1040. int prev_h = height;
  1041. int prev_w = width;
  1042. for (int i = 1; i <= mmcount; i++) {
  1043. int ofs, w, h;
  1044. _get_mipmap_offset_and_size(i, ofs, w, h);
  1045. switch (format) {
  1046. case FORMAT_L8:
  1047. 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;
  1048. case FORMAT_LA8:
  1049. 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;
  1050. case FORMAT_RGB8:
  1051. if (p_renormalize)
  1052. _generate_po2_mipmap<uint8_t, 3, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1053. else
  1054. _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1055. break;
  1056. case FORMAT_RGBA8:
  1057. if (p_renormalize)
  1058. _generate_po2_mipmap<uint8_t, 4, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1059. else
  1060. _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  1061. break;
  1062. case FORMAT_RF:
  1063. _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);
  1064. break;
  1065. case FORMAT_RGF:
  1066. _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);
  1067. break;
  1068. case FORMAT_RGBF:
  1069. if (p_renormalize)
  1070. _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);
  1071. else
  1072. _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);
  1073. break;
  1074. case FORMAT_RGBAF:
  1075. if (p_renormalize)
  1076. _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);
  1077. else
  1078. _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);
  1079. break;
  1080. case FORMAT_RH:
  1081. _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);
  1082. break;
  1083. case FORMAT_RGH:
  1084. _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);
  1085. break;
  1086. case FORMAT_RGBH:
  1087. if (p_renormalize)
  1088. _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);
  1089. else
  1090. _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);
  1091. break;
  1092. case FORMAT_RGBAH:
  1093. if (p_renormalize)
  1094. _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);
  1095. else
  1096. _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);
  1097. break;
  1098. case FORMAT_RGBE9995:
  1099. if (p_renormalize)
  1100. _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);
  1101. else
  1102. _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);
  1103. break;
  1104. default: {}
  1105. }
  1106. prev_ofs = ofs;
  1107. prev_w = w;
  1108. prev_h = h;
  1109. }
  1110. mipmaps = true;
  1111. return OK;
  1112. }
  1113. void Image::clear_mipmaps() {
  1114. if (!mipmaps)
  1115. return;
  1116. if (empty())
  1117. return;
  1118. int ofs, w, h;
  1119. _get_mipmap_offset_and_size(1, ofs, w, h);
  1120. data.resize(ofs);
  1121. mipmaps = false;
  1122. }
  1123. bool Image::empty() const {
  1124. return (data.size() == 0);
  1125. }
  1126. PoolVector<uint8_t> Image::get_data() const {
  1127. return data;
  1128. }
  1129. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1130. ERR_FAIL_INDEX(p_width - 1, MAX_WIDTH);
  1131. ERR_FAIL_INDEX(p_height - 1, MAX_HEIGHT);
  1132. int mm = 0;
  1133. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1134. data.resize(size);
  1135. {
  1136. PoolVector<uint8_t>::Write w = data.write();
  1137. zeromem(w.ptr(), size);
  1138. }
  1139. width = p_width;
  1140. height = p_height;
  1141. mipmaps = p_use_mipmaps;
  1142. format = p_format;
  1143. }
  1144. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  1145. ERR_FAIL_INDEX(p_width - 1, MAX_WIDTH);
  1146. ERR_FAIL_INDEX(p_height - 1, MAX_HEIGHT);
  1147. int mm;
  1148. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  1149. if (size != p_data.size()) {
  1150. ERR_EXPLAIN("Expected data size of " + itos(size) + " bytes in Image::create(), got instead " + itos(p_data.size()) + " bytes.");
  1151. ERR_FAIL_COND(p_data.size() != size);
  1152. }
  1153. height = p_height;
  1154. width = p_width;
  1155. format = p_format;
  1156. data = p_data;
  1157. mipmaps = p_use_mipmaps;
  1158. }
  1159. void Image::create(const char **p_xpm) {
  1160. int size_width = 0;
  1161. int size_height = 0;
  1162. int pixelchars = 0;
  1163. mipmaps = false;
  1164. bool has_alpha = false;
  1165. enum Status {
  1166. READING_HEADER,
  1167. READING_COLORS,
  1168. READING_PIXELS,
  1169. DONE
  1170. };
  1171. Status status = READING_HEADER;
  1172. int line = 0;
  1173. HashMap<String, Color> colormap;
  1174. int colormap_size = 0;
  1175. uint32_t pixel_size = 0;
  1176. PoolVector<uint8_t>::Write w;
  1177. while (status != DONE) {
  1178. const char *line_ptr = p_xpm[line];
  1179. switch (status) {
  1180. case READING_HEADER: {
  1181. String line_str = line_ptr;
  1182. line_str.replace("\t", " ");
  1183. size_width = line_str.get_slicec(' ', 0).to_int();
  1184. size_height = line_str.get_slicec(' ', 1).to_int();
  1185. colormap_size = line_str.get_slicec(' ', 2).to_int();
  1186. pixelchars = line_str.get_slicec(' ', 3).to_int();
  1187. ERR_FAIL_COND(colormap_size > 32766);
  1188. ERR_FAIL_COND(pixelchars > 5);
  1189. ERR_FAIL_COND(size_width > 32767);
  1190. ERR_FAIL_COND(size_height > 32767);
  1191. status = READING_COLORS;
  1192. } break;
  1193. case READING_COLORS: {
  1194. String colorstring;
  1195. for (int i = 0; i < pixelchars; i++) {
  1196. colorstring += *line_ptr;
  1197. line_ptr++;
  1198. }
  1199. //skip spaces
  1200. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1201. if (*line_ptr == 0)
  1202. break;
  1203. line_ptr++;
  1204. }
  1205. if (*line_ptr == 'c') {
  1206. line_ptr++;
  1207. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  1208. if (*line_ptr == 0)
  1209. break;
  1210. line_ptr++;
  1211. }
  1212. if (*line_ptr == '#') {
  1213. line_ptr++;
  1214. uint8_t col_r = 0;
  1215. uint8_t col_g = 0;
  1216. uint8_t col_b = 0;
  1217. //uint8_t col_a=255;
  1218. for (int i = 0; i < 6; i++) {
  1219. char v = line_ptr[i];
  1220. if (v >= '0' && v <= '9')
  1221. v -= '0';
  1222. else if (v >= 'A' && v <= 'F')
  1223. v = (v - 'A') + 10;
  1224. else if (v >= 'a' && v <= 'f')
  1225. v = (v - 'a') + 10;
  1226. else
  1227. break;
  1228. switch (i) {
  1229. case 0: col_r = v << 4; break;
  1230. case 1: col_r |= v; break;
  1231. case 2: col_g = v << 4; break;
  1232. case 3: col_g |= v; break;
  1233. case 4: col_b = v << 4; break;
  1234. case 5: col_b |= v; break;
  1235. };
  1236. }
  1237. // magenta mask
  1238. if (col_r == 255 && col_g == 0 && col_b == 255) {
  1239. colormap[colorstring] = Color(0, 0, 0, 0);
  1240. has_alpha = true;
  1241. } else {
  1242. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  1243. }
  1244. }
  1245. }
  1246. if (line == colormap_size) {
  1247. status = READING_PIXELS;
  1248. create(size_width, size_height, 0, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8);
  1249. w = data.write();
  1250. pixel_size = has_alpha ? 4 : 3;
  1251. }
  1252. } break;
  1253. case READING_PIXELS: {
  1254. int y = line - colormap_size - 1;
  1255. for (int x = 0; x < size_width; x++) {
  1256. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  1257. for (int i = 0; i < pixelchars; i++)
  1258. pixelstr[i] = line_ptr[x * pixelchars + i];
  1259. Color *colorptr = colormap.getptr(pixelstr);
  1260. ERR_FAIL_COND(!colorptr);
  1261. uint8_t pixel[4];
  1262. for (uint32_t i = 0; i < pixel_size; i++) {
  1263. pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255);
  1264. }
  1265. _put_pixelb(x, y, pixel_size, w.ptr(), pixel);
  1266. }
  1267. if (y == (size_height - 1))
  1268. status = DONE;
  1269. } break;
  1270. default: {}
  1271. }
  1272. line++;
  1273. }
  1274. }
  1275. #define DETECT_ALPHA_MAX_THRESHOLD 254
  1276. #define DETECT_ALPHA_MIN_THRESHOLD 2
  1277. #define DETECT_ALPHA(m_value) \
  1278. { \
  1279. uint8_t value = m_value; \
  1280. if (value < DETECT_ALPHA_MIN_THRESHOLD) \
  1281. bit = true; \
  1282. else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \
  1283. \
  1284. detected = true; \
  1285. break; \
  1286. } \
  1287. }
  1288. #define DETECT_NON_ALPHA(m_value) \
  1289. { \
  1290. uint8_t value = m_value; \
  1291. if (value > 0) { \
  1292. \
  1293. detected = true; \
  1294. break; \
  1295. } \
  1296. }
  1297. bool Image::is_invisible() const {
  1298. if (format == FORMAT_L8 ||
  1299. format == FORMAT_RGB8 || format == FORMAT_RG8)
  1300. return false;
  1301. int len = data.size();
  1302. if (len == 0)
  1303. return true;
  1304. int w, h;
  1305. _get_mipmap_offset_and_size(1, len, w, h);
  1306. PoolVector<uint8_t>::Read r = data.read();
  1307. const unsigned char *data_ptr = r.ptr();
  1308. bool detected = false;
  1309. switch (format) {
  1310. case FORMAT_LA8: {
  1311. for (int i = 0; i < (len >> 1); i++) {
  1312. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  1313. }
  1314. } break;
  1315. case FORMAT_RGBA8: {
  1316. for (int i = 0; i < (len >> 2); i++) {
  1317. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  1318. }
  1319. } break;
  1320. case FORMAT_PVRTC2A:
  1321. case FORMAT_PVRTC4A:
  1322. case FORMAT_DXT3:
  1323. case FORMAT_DXT5: {
  1324. detected = true;
  1325. } break;
  1326. default: {}
  1327. }
  1328. return !detected;
  1329. }
  1330. Image::AlphaMode Image::detect_alpha() const {
  1331. int len = data.size();
  1332. if (len == 0)
  1333. return ALPHA_NONE;
  1334. int w, h;
  1335. _get_mipmap_offset_and_size(1, len, w, h);
  1336. PoolVector<uint8_t>::Read r = data.read();
  1337. const unsigned char *data_ptr = r.ptr();
  1338. bool bit = false;
  1339. bool detected = false;
  1340. switch (format) {
  1341. case FORMAT_LA8: {
  1342. for (int i = 0; i < (len >> 1); i++) {
  1343. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  1344. }
  1345. } break;
  1346. case FORMAT_RGBA8: {
  1347. for (int i = 0; i < (len >> 2); i++) {
  1348. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  1349. }
  1350. } break;
  1351. case FORMAT_PVRTC2A:
  1352. case FORMAT_PVRTC4A:
  1353. case FORMAT_DXT3:
  1354. case FORMAT_DXT5: {
  1355. detected = true;
  1356. } break;
  1357. default: {}
  1358. }
  1359. if (detected)
  1360. return ALPHA_BLEND;
  1361. else if (bit)
  1362. return ALPHA_BIT;
  1363. else
  1364. return ALPHA_NONE;
  1365. }
  1366. Error Image::load(const String &p_path) {
  1367. #ifdef DEBUG_ENABLED
  1368. if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
  1369. 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.");
  1370. }
  1371. #endif
  1372. return ImageLoader::load_image(p_path, this);
  1373. }
  1374. Error Image::save_png(const String &p_path) const {
  1375. if (save_png_func == NULL)
  1376. return ERR_UNAVAILABLE;
  1377. return save_png_func(p_path, Ref<Image>((Image *)this));
  1378. }
  1379. int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) {
  1380. int mm;
  1381. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0);
  1382. }
  1383. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1384. int mm;
  1385. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  1386. return mm;
  1387. }
  1388. int Image::get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap) {
  1389. if (p_mipmap <= 0) {
  1390. return 0;
  1391. }
  1392. int mm;
  1393. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1);
  1394. }
  1395. bool Image::is_compressed() const {
  1396. return format > FORMAT_RGBE9995;
  1397. }
  1398. Error Image::decompress() {
  1399. if (format >= FORMAT_DXT1 && format <= FORMAT_RGTC_RG && _image_decompress_bc)
  1400. _image_decompress_bc(this);
  1401. else if (format >= FORMAT_BPTC_RGBA && format <= FORMAT_BPTC_RGBFU && _image_decompress_bptc)
  1402. _image_decompress_bptc(this);
  1403. else if (format >= FORMAT_PVRTC2 && format <= FORMAT_PVRTC4A && _image_decompress_pvrtc)
  1404. _image_decompress_pvrtc(this);
  1405. else if (format == FORMAT_ETC && _image_decompress_etc1)
  1406. _image_decompress_etc1(this);
  1407. else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RGB8A1 && _image_decompress_etc1)
  1408. _image_decompress_etc2(this);
  1409. else
  1410. return ERR_UNAVAILABLE;
  1411. return OK;
  1412. }
  1413. Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
  1414. switch (p_mode) {
  1415. case COMPRESS_S3TC: {
  1416. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1417. _image_compress_bc_func(this, p_lossy_quality, p_source);
  1418. } break;
  1419. case COMPRESS_PVRTC2: {
  1420. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1421. _image_compress_pvrtc2_func(this);
  1422. } break;
  1423. case COMPRESS_PVRTC4: {
  1424. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1425. _image_compress_pvrtc4_func(this);
  1426. } break;
  1427. case COMPRESS_ETC: {
  1428. ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE);
  1429. _image_compress_etc1_func(this, p_lossy_quality);
  1430. } break;
  1431. case COMPRESS_ETC2: {
  1432. ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE);
  1433. _image_compress_etc2_func(this, p_lossy_quality, p_source);
  1434. } break;
  1435. case COMPRESS_BPTC: {
  1436. ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE);
  1437. _image_compress_bptc_func(this, p_lossy_quality, p_source);
  1438. } break;
  1439. }
  1440. return OK;
  1441. }
  1442. Image::Image(const char **p_xpm) {
  1443. width = 0;
  1444. height = 0;
  1445. mipmaps = false;
  1446. format = FORMAT_L8;
  1447. create(p_xpm);
  1448. }
  1449. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1450. width = 0;
  1451. height = 0;
  1452. mipmaps = p_use_mipmaps;
  1453. format = FORMAT_L8;
  1454. create(p_width, p_height, p_use_mipmaps, p_format);
  1455. }
  1456. Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  1457. width = 0;
  1458. height = 0;
  1459. mipmaps = p_mipmaps;
  1460. format = FORMAT_L8;
  1461. create(p_width, p_height, p_mipmaps, p_format, p_data);
  1462. }
  1463. Rect2 Image::get_used_rect() const {
  1464. if (format != FORMAT_LA8 && format != FORMAT_RGBA8)
  1465. return Rect2(Point2(), Size2(width, height));
  1466. int len = data.size();
  1467. if (len == 0)
  1468. return Rect2();
  1469. //int data_size = len;
  1470. PoolVector<uint8_t>::Read r = data.read();
  1471. const unsigned char *rptr = r.ptr();
  1472. int ps = format == FORMAT_LA8 ? 2 : 4;
  1473. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  1474. int maxx = -1, maxy = -1;
  1475. for (int j = 0; j < height; j++) {
  1476. for (int i = 0; i < width; i++) {
  1477. bool opaque = rptr[(j * width + i) * ps + (ps - 1)] > 2;
  1478. if (!opaque)
  1479. continue;
  1480. if (i > maxx)
  1481. maxx = i;
  1482. if (j > maxy)
  1483. maxy = j;
  1484. if (i < minx)
  1485. minx = i;
  1486. if (j < miny)
  1487. miny = j;
  1488. }
  1489. }
  1490. if (maxx == -1)
  1491. return Rect2();
  1492. else
  1493. return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1);
  1494. }
  1495. Ref<Image> Image::get_rect(const Rect2 &p_area) const {
  1496. Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format));
  1497. img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0));
  1498. return img;
  1499. }
  1500. void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1501. ERR_FAIL_COND(p_src.is_null());
  1502. int dsize = data.size();
  1503. int srcdsize = p_src->data.size();
  1504. ERR_FAIL_COND(dsize == 0);
  1505. ERR_FAIL_COND(srcdsize == 0);
  1506. ERR_FAIL_COND(format != p_src->format);
  1507. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1508. if (p_dest.x < 0)
  1509. clipped_src_rect.position.x = ABS(p_dest.x);
  1510. if (p_dest.y < 0)
  1511. clipped_src_rect.position.y = ABS(p_dest.y);
  1512. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1513. return;
  1514. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  1515. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  1516. PoolVector<uint8_t>::Write wp = data.write();
  1517. uint8_t *dst_data_ptr = wp.ptr();
  1518. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1519. const uint8_t *src_data_ptr = rp.ptr();
  1520. int pixel_size = get_format_pixel_size(format);
  1521. for (int i = 0; i < dest_rect.size.y; i++) {
  1522. for (int j = 0; j < dest_rect.size.x; j++) {
  1523. int src_x = clipped_src_rect.position.x + j;
  1524. int src_y = clipped_src_rect.position.y + i;
  1525. int dst_x = dest_rect.position.x + j;
  1526. int dst_y = dest_rect.position.y + i;
  1527. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1528. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1529. for (int k = 0; k < pixel_size; k++) {
  1530. dst[k] = src[k];
  1531. }
  1532. }
  1533. }
  1534. }
  1535. void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1536. ERR_FAIL_COND(p_src.is_null());
  1537. ERR_FAIL_COND(p_mask.is_null());
  1538. int dsize = data.size();
  1539. int srcdsize = p_src->data.size();
  1540. int maskdsize = p_mask->data.size();
  1541. ERR_FAIL_COND(dsize == 0);
  1542. ERR_FAIL_COND(srcdsize == 0);
  1543. ERR_FAIL_COND(maskdsize == 0);
  1544. ERR_FAIL_COND(p_src->width != p_mask->width);
  1545. ERR_FAIL_COND(p_src->height != p_mask->height);
  1546. ERR_FAIL_COND(format != p_src->format);
  1547. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1548. if (p_dest.x < 0)
  1549. clipped_src_rect.position.x = ABS(p_dest.x);
  1550. if (p_dest.y < 0)
  1551. clipped_src_rect.position.y = ABS(p_dest.y);
  1552. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1553. return;
  1554. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  1555. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  1556. PoolVector<uint8_t>::Write wp = data.write();
  1557. uint8_t *dst_data_ptr = wp.ptr();
  1558. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1559. const uint8_t *src_data_ptr = rp.ptr();
  1560. int pixel_size = get_format_pixel_size(format);
  1561. Ref<Image> msk = p_mask;
  1562. msk->lock();
  1563. for (int i = 0; i < dest_rect.size.y; i++) {
  1564. for (int j = 0; j < dest_rect.size.x; j++) {
  1565. int src_x = clipped_src_rect.position.x + j;
  1566. int src_y = clipped_src_rect.position.y + i;
  1567. if (msk->get_pixel(src_x, src_y).a != 0) {
  1568. int dst_x = dest_rect.position.x + j;
  1569. int dst_y = dest_rect.position.y + i;
  1570. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1571. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1572. for (int k = 0; k < pixel_size; k++) {
  1573. dst[k] = src[k];
  1574. }
  1575. }
  1576. }
  1577. }
  1578. msk->unlock();
  1579. }
  1580. void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1581. ERR_FAIL_COND(p_src.is_null());
  1582. int dsize = data.size();
  1583. int srcdsize = p_src->data.size();
  1584. ERR_FAIL_COND(dsize == 0);
  1585. ERR_FAIL_COND(srcdsize == 0);
  1586. ERR_FAIL_COND(format != p_src->format);
  1587. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1588. if (p_dest.x < 0)
  1589. clipped_src_rect.position.x = ABS(p_dest.x);
  1590. if (p_dest.y < 0)
  1591. clipped_src_rect.position.y = ABS(p_dest.y);
  1592. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1593. return;
  1594. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  1595. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  1596. lock();
  1597. Ref<Image> img = p_src;
  1598. img->lock();
  1599. for (int i = 0; i < dest_rect.size.y; i++) {
  1600. for (int j = 0; j < dest_rect.size.x; j++) {
  1601. int src_x = clipped_src_rect.position.x + j;
  1602. int src_y = clipped_src_rect.position.y + i;
  1603. int dst_x = dest_rect.position.x + j;
  1604. int dst_y = dest_rect.position.y + i;
  1605. Color sc = img->get_pixel(src_x, src_y);
  1606. Color dc = get_pixel(dst_x, dst_y);
  1607. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  1608. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  1609. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  1610. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  1611. set_pixel(dst_x, dst_y, dc);
  1612. }
  1613. }
  1614. img->unlock();
  1615. unlock();
  1616. }
  1617. void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1618. ERR_FAIL_COND(p_src.is_null());
  1619. ERR_FAIL_COND(p_mask.is_null());
  1620. int dsize = data.size();
  1621. int srcdsize = p_src->data.size();
  1622. int maskdsize = p_mask->data.size();
  1623. ERR_FAIL_COND(dsize == 0);
  1624. ERR_FAIL_COND(srcdsize == 0);
  1625. ERR_FAIL_COND(maskdsize == 0);
  1626. ERR_FAIL_COND(p_src->width != p_mask->width);
  1627. ERR_FAIL_COND(p_src->height != p_mask->height);
  1628. ERR_FAIL_COND(format != p_src->format);
  1629. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1630. if (p_dest.x < 0)
  1631. clipped_src_rect.position.x = ABS(p_dest.x);
  1632. if (p_dest.y < 0)
  1633. clipped_src_rect.position.y = ABS(p_dest.y);
  1634. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1635. return;
  1636. Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y));
  1637. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest - src_underscan, clipped_src_rect.size));
  1638. lock();
  1639. Ref<Image> img = p_src;
  1640. Ref<Image> msk = p_mask;
  1641. img->lock();
  1642. msk->lock();
  1643. for (int i = 0; i < dest_rect.size.y; i++) {
  1644. for (int j = 0; j < dest_rect.size.x; j++) {
  1645. int src_x = clipped_src_rect.position.x + j;
  1646. int src_y = clipped_src_rect.position.y + i;
  1647. // If the mask's pixel is transparent then we skip it
  1648. //Color c = msk->get_pixel(src_x, src_y);
  1649. //if (c.a == 0) continue;
  1650. if (msk->get_pixel(src_x, src_y).a != 0) {
  1651. int dst_x = dest_rect.position.x + j;
  1652. int dst_y = dest_rect.position.y + i;
  1653. Color sc = img->get_pixel(src_x, src_y);
  1654. Color dc = get_pixel(dst_x, dst_y);
  1655. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  1656. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  1657. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  1658. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  1659. set_pixel(dst_x, dst_y, dc);
  1660. }
  1661. }
  1662. }
  1663. msk->unlock();
  1664. img->unlock();
  1665. unlock();
  1666. }
  1667. void Image::fill(const Color &c) {
  1668. lock();
  1669. PoolVector<uint8_t>::Write wp = data.write();
  1670. uint8_t *dst_data_ptr = wp.ptr();
  1671. int pixel_size = get_format_pixel_size(format);
  1672. // put first pixel with the format-aware API
  1673. set_pixel(0, 0, c);
  1674. for (int y = 0; y < height; y++) {
  1675. for (int x = 0; x < width; x++) {
  1676. uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size];
  1677. for (int k = 0; k < pixel_size; k++) {
  1678. dst[k] = dst_data_ptr[k];
  1679. }
  1680. }
  1681. }
  1682. unlock();
  1683. }
  1684. ImageMemLoadFunc Image::_png_mem_loader_func = NULL;
  1685. ImageMemLoadFunc Image::_jpg_mem_loader_func = NULL;
  1686. ImageMemLoadFunc Image::_webp_mem_loader_func = NULL;
  1687. void (*Image::_image_compress_bc_func)(Image *, float, Image::CompressSource) = NULL;
  1688. void (*Image::_image_compress_bptc_func)(Image *, float, Image::CompressSource) = NULL;
  1689. void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL;
  1690. void (*Image::_image_compress_pvrtc4_func)(Image *) = NULL;
  1691. void (*Image::_image_compress_etc1_func)(Image *, float) = NULL;
  1692. void (*Image::_image_compress_etc2_func)(Image *, float, Image::CompressSource) = NULL;
  1693. void (*Image::_image_decompress_pvrtc)(Image *) = NULL;
  1694. void (*Image::_image_decompress_bc)(Image *) = NULL;
  1695. void (*Image::_image_decompress_bptc)(Image *) = NULL;
  1696. void (*Image::_image_decompress_etc1)(Image *) = NULL;
  1697. void (*Image::_image_decompress_etc2)(Image *) = NULL;
  1698. PoolVector<uint8_t> (*Image::lossy_packer)(const Ref<Image> &, float) = NULL;
  1699. Ref<Image> (*Image::lossy_unpacker)(const PoolVector<uint8_t> &) = NULL;
  1700. PoolVector<uint8_t> (*Image::lossless_packer)(const Ref<Image> &) = NULL;
  1701. Ref<Image> (*Image::lossless_unpacker)(const PoolVector<uint8_t> &) = NULL;
  1702. void Image::_set_data(const Dictionary &p_data) {
  1703. ERR_FAIL_COND(!p_data.has("width"));
  1704. ERR_FAIL_COND(!p_data.has("height"));
  1705. ERR_FAIL_COND(!p_data.has("format"));
  1706. ERR_FAIL_COND(!p_data.has("mipmaps"));
  1707. ERR_FAIL_COND(!p_data.has("data"));
  1708. int dwidth = p_data["width"];
  1709. int dheight = p_data["height"];
  1710. String dformat = p_data["format"];
  1711. bool dmipmaps = p_data["mipmaps"];
  1712. PoolVector<uint8_t> ddata = p_data["data"];
  1713. Format ddformat = FORMAT_MAX;
  1714. for (int i = 0; i < FORMAT_MAX; i++) {
  1715. if (dformat == get_format_name(Format(i))) {
  1716. ddformat = Format(i);
  1717. break;
  1718. }
  1719. }
  1720. ERR_FAIL_COND(ddformat == FORMAT_MAX);
  1721. create(dwidth, dheight, dmipmaps, ddformat, ddata);
  1722. }
  1723. Dictionary Image::_get_data() const {
  1724. Dictionary d;
  1725. d["width"] = width;
  1726. d["height"] = height;
  1727. d["format"] = get_format_name(format);
  1728. d["mipmaps"] = mipmaps;
  1729. d["data"] = data;
  1730. return d;
  1731. }
  1732. void Image::lock() {
  1733. ERR_FAIL_COND(data.size() == 0);
  1734. write_lock = data.write();
  1735. }
  1736. void Image::unlock() {
  1737. write_lock = PoolVector<uint8_t>::Write();
  1738. }
  1739. Color Image::get_pixelv(const Point2 &p_src) const {
  1740. return get_pixel(p_src.x, p_src.y);
  1741. }
  1742. Color Image::get_pixel(int p_x, int p_y) const {
  1743. uint8_t *ptr = write_lock.ptr();
  1744. #ifdef DEBUG_ENABLED
  1745. if (!ptr) {
  1746. ERR_EXPLAIN("Image must be locked with 'lock()' before using get_pixel()");
  1747. ERR_FAIL_COND_V(!ptr, Color());
  1748. }
  1749. ERR_FAIL_INDEX_V(p_x, width, Color());
  1750. ERR_FAIL_INDEX_V(p_y, height, Color());
  1751. #endif
  1752. uint32_t ofs = p_y * width + p_x;
  1753. switch (format) {
  1754. case FORMAT_L8: {
  1755. float l = ptr[ofs] / 255.0;
  1756. return Color(l, l, l, 1);
  1757. } break;
  1758. case FORMAT_LA8: {
  1759. float l = ptr[ofs * 2 + 0] / 255.0;
  1760. float a = ptr[ofs * 2 + 1] / 255.0;
  1761. return Color(l, l, l, a);
  1762. } break;
  1763. case FORMAT_R8: {
  1764. float r = ptr[ofs] / 255.0;
  1765. return Color(r, 0, 0, 1);
  1766. } break;
  1767. case FORMAT_RG8: {
  1768. float r = ptr[ofs * 2 + 0] / 255.0;
  1769. float g = ptr[ofs * 2 + 1] / 255.0;
  1770. return Color(r, g, 0, 1);
  1771. } break;
  1772. case FORMAT_RGB8: {
  1773. float r = ptr[ofs * 3 + 0] / 255.0;
  1774. float g = ptr[ofs * 3 + 1] / 255.0;
  1775. float b = ptr[ofs * 3 + 2] / 255.0;
  1776. return Color(r, g, b, 1);
  1777. } break;
  1778. case FORMAT_RGBA8: {
  1779. float r = ptr[ofs * 4 + 0] / 255.0;
  1780. float g = ptr[ofs * 4 + 1] / 255.0;
  1781. float b = ptr[ofs * 4 + 2] / 255.0;
  1782. float a = ptr[ofs * 4 + 3] / 255.0;
  1783. return Color(r, g, b, a);
  1784. } break;
  1785. case FORMAT_RGBA4444: {
  1786. uint16_t u = ((uint16_t *)ptr)[ofs];
  1787. float r = (u & 0xF) / 15.0;
  1788. float g = ((u >> 4) & 0xF) / 15.0;
  1789. float b = ((u >> 8) & 0xF) / 15.0;
  1790. float a = ((u >> 12) & 0xF) / 15.0;
  1791. return Color(r, g, b, a);
  1792. } break;
  1793. case FORMAT_RGBA5551: {
  1794. uint16_t u = ((uint16_t *)ptr)[ofs];
  1795. float r = (u & 0x1F) / 15.0;
  1796. float g = ((u >> 5) & 0x1F) / 15.0;
  1797. float b = ((u >> 10) & 0x1F) / 15.0;
  1798. float a = ((u >> 15) & 0x1) / 1.0;
  1799. return Color(r, g, b, a);
  1800. } break;
  1801. case FORMAT_RF: {
  1802. float r = ((float *)ptr)[ofs];
  1803. return Color(r, 0, 0, 1);
  1804. } break;
  1805. case FORMAT_RGF: {
  1806. float r = ((float *)ptr)[ofs * 2 + 0];
  1807. float g = ((float *)ptr)[ofs * 2 + 1];
  1808. return Color(r, g, 0, 1);
  1809. } break;
  1810. case FORMAT_RGBF: {
  1811. float r = ((float *)ptr)[ofs * 3 + 0];
  1812. float g = ((float *)ptr)[ofs * 3 + 1];
  1813. float b = ((float *)ptr)[ofs * 3 + 2];
  1814. return Color(r, g, b, 1);
  1815. } break;
  1816. case FORMAT_RGBAF: {
  1817. float r = ((float *)ptr)[ofs * 4 + 0];
  1818. float g = ((float *)ptr)[ofs * 4 + 1];
  1819. float b = ((float *)ptr)[ofs * 4 + 2];
  1820. float a = ((float *)ptr)[ofs * 4 + 3];
  1821. return Color(r, g, b, a);
  1822. } break;
  1823. case FORMAT_RH: {
  1824. uint16_t r = ((uint16_t *)ptr)[ofs];
  1825. return Color(Math::half_to_float(r), 0, 0, 1);
  1826. } break;
  1827. case FORMAT_RGH: {
  1828. uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0];
  1829. uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1];
  1830. return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1);
  1831. } break;
  1832. case FORMAT_RGBH: {
  1833. uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0];
  1834. uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1];
  1835. uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2];
  1836. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1);
  1837. } break;
  1838. case FORMAT_RGBAH: {
  1839. uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0];
  1840. uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1];
  1841. uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2];
  1842. uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3];
  1843. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a));
  1844. } break;
  1845. case FORMAT_RGBE9995: {
  1846. return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]);
  1847. } break;
  1848. default: {
  1849. ERR_EXPLAIN("Can't get_pixel() on compressed image, sorry.");
  1850. ERR_FAIL_V(Color());
  1851. }
  1852. }
  1853. return Color();
  1854. }
  1855. void Image::set_pixelv(const Point2 &p_dst, const Color &p_color) {
  1856. return set_pixel(p_dst.x, p_dst.y, p_color);
  1857. }
  1858. void Image::set_pixel(int p_x, int p_y, const Color &p_color) {
  1859. uint8_t *ptr = write_lock.ptr();
  1860. #ifdef DEBUG_ENABLED
  1861. if (!ptr) {
  1862. ERR_EXPLAIN("Image must be locked with 'lock()' before using set_pixel()");
  1863. ERR_FAIL_COND(!ptr);
  1864. }
  1865. ERR_FAIL_INDEX(p_x, width);
  1866. ERR_FAIL_INDEX(p_y, height);
  1867. #endif
  1868. uint32_t ofs = p_y * width + p_x;
  1869. switch (format) {
  1870. case FORMAT_L8: {
  1871. ptr[ofs] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  1872. } break;
  1873. case FORMAT_LA8: {
  1874. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255));
  1875. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  1876. } break;
  1877. case FORMAT_R8: {
  1878. ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1879. } break;
  1880. case FORMAT_RG8: {
  1881. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1882. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1883. } break;
  1884. case FORMAT_RGB8: {
  1885. ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1886. ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1887. ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  1888. } break;
  1889. case FORMAT_RGBA8: {
  1890. ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1891. ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1892. ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  1893. ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  1894. } break;
  1895. case FORMAT_RGBA4444: {
  1896. uint16_t rgba = 0;
  1897. rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15));
  1898. rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 4;
  1899. rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 8;
  1900. rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15)) << 12;
  1901. ((uint16_t *)ptr)[ofs] = rgba;
  1902. } break;
  1903. case FORMAT_RGBA5551: {
  1904. uint16_t rgba = 0;
  1905. rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
  1906. rgba |= uint16_t(CLAMP(p_color.g * 31.0, 0, 31)) << 5;
  1907. rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 10;
  1908. rgba |= uint16_t(p_color.a > 0.5 ? 1 : 0) << 15;
  1909. ((uint16_t *)ptr)[ofs] = rgba;
  1910. } break;
  1911. case FORMAT_RF: {
  1912. ((float *)ptr)[ofs] = p_color.r;
  1913. } break;
  1914. case FORMAT_RGF: {
  1915. ((float *)ptr)[ofs * 2 + 0] = p_color.r;
  1916. ((float *)ptr)[ofs * 2 + 1] = p_color.g;
  1917. } break;
  1918. case FORMAT_RGBF: {
  1919. ((float *)ptr)[ofs * 3 + 0] = p_color.r;
  1920. ((float *)ptr)[ofs * 3 + 1] = p_color.g;
  1921. ((float *)ptr)[ofs * 3 + 2] = p_color.b;
  1922. } break;
  1923. case FORMAT_RGBAF: {
  1924. ((float *)ptr)[ofs * 4 + 0] = p_color.r;
  1925. ((float *)ptr)[ofs * 4 + 1] = p_color.g;
  1926. ((float *)ptr)[ofs * 4 + 2] = p_color.b;
  1927. ((float *)ptr)[ofs * 4 + 3] = p_color.a;
  1928. } break;
  1929. case FORMAT_RH: {
  1930. ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r);
  1931. } break;
  1932. case FORMAT_RGH: {
  1933. ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r);
  1934. ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g);
  1935. } break;
  1936. case FORMAT_RGBH: {
  1937. ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r);
  1938. ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g);
  1939. ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b);
  1940. } break;
  1941. case FORMAT_RGBAH: {
  1942. ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r);
  1943. ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g);
  1944. ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b);
  1945. ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a);
  1946. } break;
  1947. case FORMAT_RGBE9995: {
  1948. ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995();
  1949. } break;
  1950. default: {
  1951. ERR_EXPLAIN("Can't set_pixel() on compressed image, sorry.");
  1952. ERR_FAIL();
  1953. }
  1954. }
  1955. }
  1956. Image::DetectChannels Image::get_detected_channels() {
  1957. ERR_FAIL_COND_V(data.size() == 0, DETECTED_RGBA);
  1958. ERR_FAIL_COND_V(is_compressed(), DETECTED_RGBA);
  1959. bool r = false, g = false, b = false, a = false, c = false;
  1960. lock();
  1961. for (int i = 0; i < width; i++) {
  1962. for (int j = 0; j < height; j++) {
  1963. Color col = get_pixel(i, j);
  1964. if (col.r > 0.001)
  1965. r = true;
  1966. if (col.g > 0.001)
  1967. g = true;
  1968. if (col.b > 0.001)
  1969. b = true;
  1970. if (col.a < 0.999)
  1971. a = true;
  1972. if (col.r != col.b || col.r != col.g || col.b != col.g) {
  1973. c = true;
  1974. }
  1975. }
  1976. }
  1977. unlock();
  1978. if (!c && !a)
  1979. return DETECTED_L;
  1980. if (!c && a)
  1981. return DETECTED_LA;
  1982. if (r && !g && !b && !a)
  1983. return DETECTED_R;
  1984. if (r && g && !b && !a)
  1985. return DETECTED_RG;
  1986. if (r && g && b && !a)
  1987. return DETECTED_RGB;
  1988. return DETECTED_RGBA;
  1989. }
  1990. void Image::optimize_channels() {
  1991. switch (get_detected_channels()) {
  1992. case DETECTED_L: convert(FORMAT_L8); break;
  1993. case DETECTED_LA: convert(FORMAT_LA8); break;
  1994. case DETECTED_R: convert(FORMAT_R8); break;
  1995. case DETECTED_RG: convert(FORMAT_RG8); break;
  1996. case DETECTED_RGB: convert(FORMAT_RGB8); break;
  1997. case DETECTED_RGBA: convert(FORMAT_RGBA8); break;
  1998. }
  1999. }
  2000. void Image::_bind_methods() {
  2001. ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
  2002. ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height);
  2003. ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size);
  2004. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps);
  2005. ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format);
  2006. ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data);
  2007. ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert);
  2008. ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset);
  2009. ClassDB::bind_method(D_METHOD("resize_to_po2", "square"), &Image::resize_to_po2, DEFVAL(false));
  2010. ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR));
  2011. ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2);
  2012. ClassDB::bind_method(D_METHOD("expand_x2_hq2x"), &Image::expand_x2_hq2x);
  2013. ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop);
  2014. ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x);
  2015. ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y);
  2016. ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false));
  2017. ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps);
  2018. ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty);
  2019. ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data);
  2020. ClassDB::bind_method(D_METHOD("is_empty"), &Image::empty);
  2021. ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
  2022. ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
  2023. ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
  2024. ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);
  2025. ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress);
  2026. ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress);
  2027. ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed);
  2028. ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges);
  2029. ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha);
  2030. ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear);
  2031. ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy);
  2032. ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb);
  2033. ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap", "bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0));
  2034. ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect);
  2035. ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask);
  2036. ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect);
  2037. ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask);
  2038. ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill);
  2039. ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
  2040. ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect);
  2041. ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from);
  2042. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data);
  2043. ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data);
  2044. ClassDB::bind_method(D_METHOD("lock"), &Image::lock);
  2045. ClassDB::bind_method(D_METHOD("unlock"), &Image::unlock);
  2046. ClassDB::bind_method(D_METHOD("get_pixelv", "src"), &Image::get_pixelv);
  2047. ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel);
  2048. ClassDB::bind_method(D_METHOD("set_pixelv", "dst", "color"), &Image::set_pixelv);
  2049. ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel);
  2050. ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer);
  2051. ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer);
  2052. ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
  2053. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
  2054. BIND_ENUM_CONSTANT(FORMAT_L8); //luminance
  2055. BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha
  2056. BIND_ENUM_CONSTANT(FORMAT_R8);
  2057. BIND_ENUM_CONSTANT(FORMAT_RG8);
  2058. BIND_ENUM_CONSTANT(FORMAT_RGB8);
  2059. BIND_ENUM_CONSTANT(FORMAT_RGBA8);
  2060. BIND_ENUM_CONSTANT(FORMAT_RGBA4444);
  2061. BIND_ENUM_CONSTANT(FORMAT_RGBA5551);
  2062. BIND_ENUM_CONSTANT(FORMAT_RF); //float
  2063. BIND_ENUM_CONSTANT(FORMAT_RGF);
  2064. BIND_ENUM_CONSTANT(FORMAT_RGBF);
  2065. BIND_ENUM_CONSTANT(FORMAT_RGBAF);
  2066. BIND_ENUM_CONSTANT(FORMAT_RH); //half float
  2067. BIND_ENUM_CONSTANT(FORMAT_RGH);
  2068. BIND_ENUM_CONSTANT(FORMAT_RGBH);
  2069. BIND_ENUM_CONSTANT(FORMAT_RGBAH);
  2070. BIND_ENUM_CONSTANT(FORMAT_RGBE9995);
  2071. BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1
  2072. BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2
  2073. BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3
  2074. BIND_ENUM_CONSTANT(FORMAT_RGTC_R);
  2075. BIND_ENUM_CONSTANT(FORMAT_RGTC_RG);
  2076. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h
  2077. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float /
  2078. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float
  2079. BIND_ENUM_CONSTANT(FORMAT_PVRTC2); //pvrtc
  2080. BIND_ENUM_CONSTANT(FORMAT_PVRTC2A);
  2081. BIND_ENUM_CONSTANT(FORMAT_PVRTC4);
  2082. BIND_ENUM_CONSTANT(FORMAT_PVRTC4A);
  2083. BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1
  2084. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2
  2085. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb.
  2086. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11);
  2087. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S);
  2088. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8);
  2089. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8);
  2090. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1);
  2091. BIND_ENUM_CONSTANT(FORMAT_MAX);
  2092. BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST);
  2093. BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR);
  2094. BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC);
  2095. BIND_ENUM_CONSTANT(INTERPOLATE_TRILINEAR);
  2096. BIND_ENUM_CONSTANT(ALPHA_NONE);
  2097. BIND_ENUM_CONSTANT(ALPHA_BIT);
  2098. BIND_ENUM_CONSTANT(ALPHA_BLEND);
  2099. BIND_ENUM_CONSTANT(COMPRESS_S3TC);
  2100. BIND_ENUM_CONSTANT(COMPRESS_PVRTC2);
  2101. BIND_ENUM_CONSTANT(COMPRESS_PVRTC4);
  2102. BIND_ENUM_CONSTANT(COMPRESS_ETC);
  2103. BIND_ENUM_CONSTANT(COMPRESS_ETC2);
  2104. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC);
  2105. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB);
  2106. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
  2107. }
  2108. void Image::set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource)) {
  2109. _image_compress_bc_func = p_compress_func;
  2110. }
  2111. void Image::set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource)) {
  2112. _image_compress_bptc_func = p_compress_func;
  2113. }
  2114. void Image::normalmap_to_xy() {
  2115. convert(Image::FORMAT_RGBA8);
  2116. {
  2117. int len = data.size() / 4;
  2118. PoolVector<uint8_t>::Write wp = data.write();
  2119. unsigned char *data_ptr = wp.ptr();
  2120. for (int i = 0; i < len; i++) {
  2121. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  2122. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  2123. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  2124. }
  2125. }
  2126. convert(Image::FORMAT_LA8);
  2127. }
  2128. Ref<Image> Image::rgbe_to_srgb() {
  2129. if (data.size() == 0)
  2130. return Ref<Image>();
  2131. ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>());
  2132. Ref<Image> new_image;
  2133. new_image.instance();
  2134. new_image->create(width, height, 0, Image::FORMAT_RGB8);
  2135. lock();
  2136. new_image->lock();
  2137. for (int row = 0; row < height; row++) {
  2138. for (int col = 0; col < width; col++) {
  2139. new_image->set_pixel(col, row, get_pixel(col, row).to_srgb());
  2140. }
  2141. }
  2142. unlock();
  2143. new_image->unlock();
  2144. if (has_mipmaps()) {
  2145. new_image->generate_mipmaps();
  2146. }
  2147. return new_image;
  2148. }
  2149. void Image::bumpmap_to_normalmap(float bump_scale) {
  2150. ERR_FAIL_COND(!_can_modify(format));
  2151. convert(Image::FORMAT_RF);
  2152. PoolVector<uint8_t> result_image; //rgba output
  2153. result_image.resize(width * height * 4);
  2154. {
  2155. PoolVector<uint8_t>::Read rp = data.read();
  2156. PoolVector<uint8_t>::Write wp = result_image.write();
  2157. unsigned char *write_ptr = wp.ptr();
  2158. float *read_ptr = (float *)rp.ptr();
  2159. for (int ty = 0; ty < height; ty++) {
  2160. int py = ty + 1;
  2161. if (py >= height) py -= height;
  2162. for (int tx = 0; tx < width; tx++) {
  2163. int px = tx + 1;
  2164. if (px >= width) px -= width;
  2165. float here = read_ptr[ty * width + tx];
  2166. float to_right = read_ptr[ty * width + px];
  2167. float above = read_ptr[py * width + tx];
  2168. Vector3 up = Vector3(0, 1, (here - above) * bump_scale);
  2169. Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale);
  2170. Vector3 normal = across.cross(up);
  2171. normal.normalize();
  2172. write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5);
  2173. write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5);
  2174. write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5);
  2175. write_ptr[((ty * width + tx) << 2) + 3] = 255;
  2176. }
  2177. }
  2178. }
  2179. format = FORMAT_RGBA8;
  2180. data = result_image;
  2181. }
  2182. void Image::srgb_to_linear() {
  2183. if (data.size() == 0)
  2184. return;
  2185. 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 };
  2186. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  2187. if (format == FORMAT_RGBA8) {
  2188. int len = data.size() / 4;
  2189. PoolVector<uint8_t>::Write wp = data.write();
  2190. unsigned char *data_ptr = wp.ptr();
  2191. for (int i = 0; i < len; i++) {
  2192. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  2193. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  2194. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  2195. }
  2196. } else if (format == FORMAT_RGB8) {
  2197. int len = data.size() / 3;
  2198. PoolVector<uint8_t>::Write wp = data.write();
  2199. unsigned char *data_ptr = wp.ptr();
  2200. for (int i = 0; i < len; i++) {
  2201. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  2202. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  2203. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  2204. }
  2205. }
  2206. }
  2207. void Image::premultiply_alpha() {
  2208. if (data.size() == 0)
  2209. return;
  2210. if (format != FORMAT_RGBA8)
  2211. return; //not needed
  2212. PoolVector<uint8_t>::Write wp = data.write();
  2213. unsigned char *data_ptr = wp.ptr();
  2214. for (int i = 0; i < height; i++) {
  2215. for (int j = 0; j < width; j++) {
  2216. uint8_t *ptr = &data_ptr[(i * width + j) * 4];
  2217. ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8;
  2218. ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8;
  2219. ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8;
  2220. }
  2221. }
  2222. }
  2223. void Image::fix_alpha_edges() {
  2224. if (data.size() == 0)
  2225. return;
  2226. if (format != FORMAT_RGBA8)
  2227. return; //not needed
  2228. PoolVector<uint8_t> dcopy = data;
  2229. PoolVector<uint8_t>::Read rp = dcopy.read();
  2230. const uint8_t *srcptr = rp.ptr();
  2231. PoolVector<uint8_t>::Write wp = data.write();
  2232. unsigned char *data_ptr = wp.ptr();
  2233. const int max_radius = 4;
  2234. const int alpha_threshold = 20;
  2235. const int max_dist = 0x7FFFFFFF;
  2236. for (int i = 0; i < height; i++) {
  2237. for (int j = 0; j < width; j++) {
  2238. const uint8_t *rptr = &srcptr[(i * width + j) * 4];
  2239. uint8_t *wptr = &data_ptr[(i * width + j) * 4];
  2240. if (rptr[3] >= alpha_threshold)
  2241. continue;
  2242. int closest_dist = max_dist;
  2243. uint8_t closest_color[3];
  2244. int from_x = MAX(0, j - max_radius);
  2245. int to_x = MIN(width - 1, j + max_radius);
  2246. int from_y = MAX(0, i - max_radius);
  2247. int to_y = MIN(height - 1, i + max_radius);
  2248. for (int k = from_y; k <= to_y; k++) {
  2249. for (int l = from_x; l <= to_x; l++) {
  2250. int dy = i - k;
  2251. int dx = j - l;
  2252. int dist = dy * dy + dx * dx;
  2253. if (dist >= closest_dist)
  2254. continue;
  2255. const uint8_t *rp = &srcptr[(k * width + l) << 2];
  2256. if (rp[3] < alpha_threshold)
  2257. continue;
  2258. closest_dist = dist;
  2259. closest_color[0] = rp[0];
  2260. closest_color[1] = rp[1];
  2261. closest_color[2] = rp[2];
  2262. }
  2263. }
  2264. if (closest_dist != max_dist) {
  2265. wptr[0] = closest_color[0];
  2266. wptr[1] = closest_color[1];
  2267. wptr[2] = closest_color[2];
  2268. }
  2269. }
  2270. }
  2271. }
  2272. String Image::get_format_name(Format p_format) {
  2273. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  2274. return format_names[p_format];
  2275. }
  2276. Error Image::load_png_from_buffer(const PoolVector<uint8_t> &p_array) {
  2277. return _load_from_buffer(p_array, _png_mem_loader_func);
  2278. }
  2279. Error Image::load_jpg_from_buffer(const PoolVector<uint8_t> &p_array) {
  2280. return _load_from_buffer(p_array, _jpg_mem_loader_func);
  2281. }
  2282. Error Image::load_webp_from_buffer(const PoolVector<uint8_t> &p_array) {
  2283. return _load_from_buffer(p_array, _webp_mem_loader_func);
  2284. }
  2285. Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
  2286. int buffer_size = p_array.size();
  2287. ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
  2288. ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER);
  2289. PoolVector<uint8_t>::Read r = p_array.read();
  2290. Ref<Image> image = p_loader(r.ptr(), buffer_size);
  2291. ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR);
  2292. copy_internals_from(image);
  2293. return OK;
  2294. }
  2295. 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) {
  2296. p_out = static_cast<uint8_t>((p_a + p_b + p_c + p_d + 2) >> 2);
  2297. }
  2298. void Image::average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d) {
  2299. p_out = (p_a + p_b + p_c + p_d) * 0.25f;
  2300. }
  2301. 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) {
  2302. 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);
  2303. }
  2304. 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) {
  2305. 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();
  2306. }
  2307. void Image::renormalize_uint8(uint8_t *p_rgb) {
  2308. Vector3 n(p_rgb[0] / 255.0, p_rgb[1] / 255.0, p_rgb[2] / 255.0);
  2309. n *= 2.0;
  2310. n -= Vector3(1, 1, 1);
  2311. n.normalize();
  2312. n += Vector3(1, 1, 1);
  2313. n *= 0.5;
  2314. n *= 255;
  2315. p_rgb[0] = CLAMP(int(n.x), 0, 255);
  2316. p_rgb[1] = CLAMP(int(n.y), 0, 255);
  2317. p_rgb[2] = CLAMP(int(n.z), 0, 255);
  2318. }
  2319. void Image::renormalize_float(float *p_rgb) {
  2320. Vector3 n(p_rgb[0], p_rgb[1], p_rgb[2]);
  2321. n.normalize();
  2322. p_rgb[0] = n.x;
  2323. p_rgb[1] = n.y;
  2324. p_rgb[2] = n.z;
  2325. }
  2326. void Image::renormalize_half(uint16_t *p_rgb) {
  2327. Vector3 n(Math::half_to_float(p_rgb[0]), Math::half_to_float(p_rgb[1]), Math::half_to_float(p_rgb[2]));
  2328. n.normalize();
  2329. p_rgb[0] = Math::make_half_float(n.x);
  2330. p_rgb[1] = Math::make_half_float(n.y);
  2331. p_rgb[2] = Math::make_half_float(n.z);
  2332. }
  2333. void Image::renormalize_rgbe9995(uint32_t *p_rgb) {
  2334. // Never used
  2335. }
  2336. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  2337. width = 0;
  2338. height = 0;
  2339. mipmaps = false;
  2340. format = FORMAT_L8;
  2341. if (_png_mem_loader_func) {
  2342. copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len));
  2343. }
  2344. if (empty() && _jpg_mem_loader_func) {
  2345. copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len));
  2346. }
  2347. }
  2348. Ref<Resource> Image::duplicate(bool p_subresources) const {
  2349. Ref<Image> copy;
  2350. copy.instance();
  2351. copy->_copy_internals_from(*this);
  2352. return copy;
  2353. }
  2354. Image::Image() {
  2355. width = 0;
  2356. height = 0;
  2357. mipmaps = false;
  2358. format = FORMAT_L8;
  2359. }
  2360. Image::~Image() {
  2361. if (write_lock.ptr()) {
  2362. unlock();
  2363. }
  2364. }