image.cpp 101 KB

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