image.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. /*************************************************************************/
  2. /* image.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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/io/image_loader.h"
  32. #include "core/os/copymem.h"
  33. #include "hash_map.h"
  34. #include "print_string.h"
  35. #include "thirdparty/misc/hq2x.h"
  36. #include <stdio.h>
  37. const char *Image::format_names[Image::FORMAT_MAX] = {
  38. "Lum8", //luminance
  39. "LumAlpha8", //luminance-alpha
  40. "Red8",
  41. "RedGreen",
  42. "RGB8",
  43. "RGBA8",
  44. "RGBA4444",
  45. "RGBA5551",
  46. "RFloat", //float
  47. "RGFloat",
  48. "RGBFloat",
  49. "RGBAFloat",
  50. "RHalf", //half float
  51. "RGHalf",
  52. "RGBHalf",
  53. "RGBAHalf",
  54. "RGBE9995",
  55. "DXT1 RGB8", //s3tc
  56. "DXT3 RGBA8",
  57. "DXT5 RGBA8",
  58. "RGTC Red8",
  59. "RGTC RedGreen8",
  60. "BPTC_RGBA",
  61. "BPTC_RGBF",
  62. "BPTC_RGBFU",
  63. "PVRTC2", //pvrtc
  64. "PVRTC2A",
  65. "PVRTC4",
  66. "PVRTC4A",
  67. "ETC", //etc1
  68. "ETC2_R11", //etc2
  69. "ETC2_R11S", //signed", NOT srgb.
  70. "ETC2_RG11",
  71. "ETC2_RG11S",
  72. "ETC2_RGB8",
  73. "ETC2_RGBA8",
  74. "ETC2_RGB8A1",
  75. };
  76. SavePNGFunc Image::save_png_func = NULL;
  77. void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel) {
  78. uint32_t ofs = (p_y * width + p_x) * p_pixelsize;
  79. for (uint32_t i = 0; i < p_pixelsize; i++) {
  80. p_data[ofs + i] = p_pixel[i];
  81. }
  82. }
  83. void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel) {
  84. uint32_t ofs = (p_y * width + p_x) * p_pixelsize;
  85. for (uint32_t i = 0; i < p_pixelsize; i++) {
  86. p_pixel[i] = p_data[ofs + i];
  87. }
  88. }
  89. int Image::get_format_pixel_size(Format p_format) {
  90. switch (p_format) {
  91. case FORMAT_L8:
  92. return 1; //luminance
  93. case FORMAT_LA8:
  94. return 2; //luminance-alpha
  95. case FORMAT_R8: return 1;
  96. case FORMAT_RG8: return 2;
  97. case FORMAT_RGB8: return 3;
  98. case FORMAT_RGBA8: return 4;
  99. case FORMAT_RGBA4444: return 2;
  100. case FORMAT_RGBA5551: return 2;
  101. case FORMAT_RF:
  102. return 4; //float
  103. case FORMAT_RGF: return 8;
  104. case FORMAT_RGBF: return 12;
  105. case FORMAT_RGBAF: return 16;
  106. case FORMAT_RH:
  107. return 2; //half float
  108. case FORMAT_RGH: return 4;
  109. case FORMAT_RGBH: return 6;
  110. case FORMAT_RGBAH: return 8;
  111. case FORMAT_RGBE9995: return 4;
  112. case FORMAT_DXT1:
  113. return 1; //s3tc bc1
  114. case FORMAT_DXT3:
  115. return 1; //bc2
  116. case FORMAT_DXT5:
  117. return 1; //bc3
  118. case FORMAT_RGTC_R:
  119. return 1; //bc4
  120. case FORMAT_RGTC_RG:
  121. return 1; //bc5
  122. case FORMAT_BPTC_RGBA:
  123. return 1; //btpc bc6h
  124. case FORMAT_BPTC_RGBF:
  125. return 1; //float /
  126. case FORMAT_BPTC_RGBFU:
  127. return 1; //unsigned float
  128. case FORMAT_PVRTC2:
  129. return 1; //pvrtc
  130. case FORMAT_PVRTC2A: return 1;
  131. case FORMAT_PVRTC4: return 1;
  132. case FORMAT_PVRTC4A: return 1;
  133. case FORMAT_ETC:
  134. return 1; //etc1
  135. case FORMAT_ETC2_R11:
  136. return 1; //etc2
  137. case FORMAT_ETC2_R11S:
  138. return 1; //signed: return 1; NOT srgb.
  139. case FORMAT_ETC2_RG11: return 1;
  140. case FORMAT_ETC2_RG11S: return 1;
  141. case FORMAT_ETC2_RGB8: return 1;
  142. case FORMAT_ETC2_RGBA8: return 1;
  143. case FORMAT_ETC2_RGB8A1: return 1;
  144. case FORMAT_MAX: {
  145. }
  146. }
  147. return 0;
  148. }
  149. void Image::get_format_min_pixel_size(Format p_format, int &r_w, int &r_h) {
  150. switch (p_format) {
  151. case FORMAT_DXT1: //s3tc bc1
  152. case FORMAT_DXT3: //bc2
  153. case FORMAT_DXT5: //bc3
  154. case FORMAT_RGTC_R: //bc4
  155. case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1:
  156. r_w = 4;
  157. r_h = 4;
  158. } break;
  159. case FORMAT_PVRTC2:
  160. case FORMAT_PVRTC2A: {
  161. r_w = 16;
  162. r_h = 8;
  163. } break;
  164. case FORMAT_PVRTC4A:
  165. case FORMAT_PVRTC4: {
  166. r_w = 8;
  167. r_h = 8;
  168. } break;
  169. case FORMAT_ETC: {
  170. r_w = 4;
  171. r_h = 4;
  172. } break;
  173. case FORMAT_BPTC_RGBA:
  174. case FORMAT_BPTC_RGBF:
  175. case FORMAT_BPTC_RGBFU: {
  176. r_w = 4;
  177. r_h = 4;
  178. } break;
  179. case FORMAT_ETC2_R11: //etc2
  180. case FORMAT_ETC2_R11S: //signed: NOT srgb.
  181. case FORMAT_ETC2_RG11:
  182. case FORMAT_ETC2_RG11S:
  183. case FORMAT_ETC2_RGB8:
  184. case FORMAT_ETC2_RGBA8:
  185. case FORMAT_ETC2_RGB8A1: {
  186. r_w = 4;
  187. r_h = 4;
  188. } break;
  189. default: {
  190. r_w = 1;
  191. r_h = 1;
  192. } break;
  193. }
  194. }
  195. int Image::get_format_pixel_rshift(Format p_format) {
  196. 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)
  197. return 1;
  198. else if (p_format == FORMAT_PVRTC2 || p_format == FORMAT_PVRTC2A)
  199. return 2;
  200. else
  201. return 0;
  202. }
  203. int Image::get_format_block_size(Format p_format) {
  204. switch (p_format) {
  205. case FORMAT_DXT1: //s3tc bc1
  206. case FORMAT_DXT3: //bc2
  207. case FORMAT_DXT5: //bc3
  208. case FORMAT_RGTC_R: //bc4
  209. case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1:
  210. return 4;
  211. } break;
  212. case FORMAT_PVRTC2:
  213. case FORMAT_PVRTC2A: {
  214. return 4;
  215. } break;
  216. case FORMAT_PVRTC4A:
  217. case FORMAT_PVRTC4: {
  218. return 4;
  219. } break;
  220. case FORMAT_ETC: {
  221. return 4;
  222. } break;
  223. case FORMAT_BPTC_RGBA:
  224. case FORMAT_BPTC_RGBF:
  225. case FORMAT_BPTC_RGBFU: {
  226. return 4;
  227. } break;
  228. case FORMAT_ETC2_R11: //etc2
  229. case FORMAT_ETC2_R11S: //signed: NOT srgb.
  230. case FORMAT_ETC2_RG11:
  231. case FORMAT_ETC2_RG11S:
  232. case FORMAT_ETC2_RGB8:
  233. case FORMAT_ETC2_RGBA8:
  234. case FORMAT_ETC2_RGB8A1: {
  235. return 4;
  236. } break;
  237. default: {
  238. }
  239. }
  240. return 1;
  241. }
  242. void Image::_get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const {
  243. int w = width;
  244. int h = height;
  245. int ofs = 0;
  246. int pixel_size = get_format_pixel_size(format);
  247. int pixel_rshift = get_format_pixel_rshift(format);
  248. int block = get_format_block_size(format);
  249. int minw, minh;
  250. get_format_min_pixel_size(format, minw, minh);
  251. for (int i = 0; i < p_mipmap; i++) {
  252. int bw = w % block != 0 ? w + (block - w % block) : w;
  253. int bh = h % block != 0 ? h + (block - h % block) : h;
  254. int s = bw * bh;
  255. s *= pixel_size;
  256. s >>= pixel_rshift;
  257. ofs += s;
  258. w = MAX(minw, w >> 1);
  259. h = MAX(minh, h >> 1);
  260. }
  261. r_offset = ofs;
  262. r_width = w;
  263. r_height = h;
  264. }
  265. int Image::get_mipmap_offset(int p_mipmap) const {
  266. ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1);
  267. int ofs, w, h;
  268. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  269. return ofs;
  270. }
  271. void Image::get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const {
  272. int ofs, w, h;
  273. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  274. int ofs2;
  275. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h);
  276. r_ofs = ofs;
  277. r_size = ofs2 - ofs;
  278. }
  279. void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const {
  280. int ofs;
  281. _get_mipmap_offset_and_size(p_mipmap, ofs, w, h);
  282. int ofs2, w2, h2;
  283. _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w2, h2);
  284. r_ofs = ofs;
  285. r_size = ofs2 - ofs;
  286. }
  287. int Image::get_width() const {
  288. return width;
  289. }
  290. int Image::get_height() const {
  291. return height;
  292. }
  293. Vector2 Image::get_size() const {
  294. return Vector2(width, height);
  295. }
  296. bool Image::has_mipmaps() const {
  297. return mipmaps;
  298. }
  299. int Image::get_mipmap_count() const {
  300. if (mipmaps)
  301. return get_image_required_mipmaps(width, height, format);
  302. else
  303. return 0;
  304. }
  305. //using template generates perfectly optimized code due to constant expression reduction and unused variable removal present in all compilers
  306. template <uint32_t read_bytes, bool read_alpha, uint32_t write_bytes, bool write_alpha, bool read_gray, bool write_gray>
  307. static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p_dst) {
  308. for (int y = 0; y < p_height; y++) {
  309. for (int x = 0; x < p_width; x++) {
  310. const uint8_t *rofs = &p_src[((y * p_width) + x) * (read_bytes + (read_alpha ? 1 : 0))];
  311. uint8_t *wofs = &p_dst[((y * p_width) + x) * (write_bytes + (write_alpha ? 1 : 0))];
  312. uint8_t rgba[4];
  313. if (read_gray) {
  314. rgba[0] = rofs[0];
  315. rgba[1] = rofs[0];
  316. rgba[2] = rofs[0];
  317. } else {
  318. for (uint32_t i = 0; i < MAX(read_bytes, write_bytes); i++) {
  319. rgba[i] = (i < read_bytes) ? rofs[i] : 0;
  320. }
  321. }
  322. if (read_alpha || write_alpha) {
  323. rgba[3] = read_alpha ? rofs[read_bytes] : 255;
  324. }
  325. if (write_gray) {
  326. //TODO: not correct grayscale, should use fixed point version of actual weights
  327. wofs[0] = uint8_t((uint16_t(rofs[0]) + uint16_t(rofs[1]) + uint16_t(rofs[2])) / 3);
  328. } else {
  329. for (uint32_t i = 0; i < write_bytes; i++) {
  330. wofs[i] = rgba[i];
  331. }
  332. }
  333. if (write_alpha) {
  334. wofs[write_bytes] = rgba[3];
  335. }
  336. }
  337. }
  338. }
  339. void Image::convert(Format p_new_format) {
  340. if (data.size() == 0)
  341. return;
  342. if (p_new_format == format)
  343. return;
  344. if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) {
  345. ERR_EXPLAIN("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead.");
  346. ERR_FAIL();
  347. } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) {
  348. //use put/set pixel which is slower but works with non byte formats
  349. Image new_img(width, height, 0, p_new_format);
  350. lock();
  351. new_img.lock();
  352. for (int i = 0; i < width; i++) {
  353. for (int j = 0; j < height; j++) {
  354. new_img.set_pixel(i, j, get_pixel(i, j));
  355. }
  356. }
  357. unlock();
  358. new_img.unlock();
  359. if (has_mipmaps()) {
  360. new_img.generate_mipmaps();
  361. }
  362. _copy_internals_from(new_img);
  363. return;
  364. }
  365. Image new_img(width, height, 0, p_new_format);
  366. //int len=data.size();
  367. PoolVector<uint8_t>::Read r = data.read();
  368. PoolVector<uint8_t>::Write w = new_img.data.write();
  369. const uint8_t *rptr = r.ptr();
  370. uint8_t *wptr = w.ptr();
  371. int conversion_type = format | p_new_format << 8;
  372. switch (conversion_type) {
  373. case FORMAT_L8 | (FORMAT_LA8 << 8): _convert<1, false, 1, true, true, true>(width, height, rptr, wptr); break;
  374. case FORMAT_L8 | (FORMAT_R8 << 8): _convert<1, false, 1, false, true, false>(width, height, rptr, wptr); break;
  375. case FORMAT_L8 | (FORMAT_RG8 << 8): _convert<1, false, 2, false, true, false>(width, height, rptr, wptr); break;
  376. case FORMAT_L8 | (FORMAT_RGB8 << 8): _convert<1, false, 3, false, true, false>(width, height, rptr, wptr); break;
  377. case FORMAT_L8 | (FORMAT_RGBA8 << 8): _convert<1, false, 3, true, true, false>(width, height, rptr, wptr); break;
  378. case FORMAT_LA8 | (FORMAT_L8 << 8): _convert<1, true, 1, false, true, true>(width, height, rptr, wptr); break;
  379. case FORMAT_LA8 | (FORMAT_R8 << 8): _convert<1, true, 1, false, true, false>(width, height, rptr, wptr); break;
  380. case FORMAT_LA8 | (FORMAT_RG8 << 8): _convert<1, true, 2, false, true, false>(width, height, rptr, wptr); break;
  381. case FORMAT_LA8 | (FORMAT_RGB8 << 8): _convert<1, true, 3, false, true, false>(width, height, rptr, wptr); break;
  382. case FORMAT_LA8 | (FORMAT_RGBA8 << 8): _convert<1, true, 3, true, true, false>(width, height, rptr, wptr); break;
  383. case FORMAT_R8 | (FORMAT_L8 << 8): _convert<1, false, 1, false, false, true>(width, height, rptr, wptr); break;
  384. case FORMAT_R8 | (FORMAT_LA8 << 8): _convert<1, false, 1, true, false, true>(width, height, rptr, wptr); break;
  385. case FORMAT_R8 | (FORMAT_RG8 << 8): _convert<1, false, 2, false, false, false>(width, height, rptr, wptr); break;
  386. case FORMAT_R8 | (FORMAT_RGB8 << 8): _convert<1, false, 3, false, false, false>(width, height, rptr, wptr); break;
  387. case FORMAT_R8 | (FORMAT_RGBA8 << 8): _convert<1, false, 3, true, false, false>(width, height, rptr, wptr); break;
  388. case FORMAT_RG8 | (FORMAT_L8 << 8): _convert<2, false, 1, false, false, true>(width, height, rptr, wptr); break;
  389. case FORMAT_RG8 | (FORMAT_LA8 << 8): _convert<2, false, 1, true, false, true>(width, height, rptr, wptr); break;
  390. case FORMAT_RG8 | (FORMAT_R8 << 8): _convert<2, false, 1, false, false, false>(width, height, rptr, wptr); break;
  391. case FORMAT_RG8 | (FORMAT_RGB8 << 8): _convert<2, false, 3, false, false, false>(width, height, rptr, wptr); break;
  392. case FORMAT_RG8 | (FORMAT_RGBA8 << 8): _convert<2, false, 3, true, false, false>(width, height, rptr, wptr); break;
  393. case FORMAT_RGB8 | (FORMAT_L8 << 8): _convert<3, false, 1, false, false, true>(width, height, rptr, wptr); break;
  394. case FORMAT_RGB8 | (FORMAT_LA8 << 8): _convert<3, false, 1, true, false, true>(width, height, rptr, wptr); break;
  395. case FORMAT_RGB8 | (FORMAT_R8 << 8): _convert<3, false, 1, false, false, false>(width, height, rptr, wptr); break;
  396. case FORMAT_RGB8 | (FORMAT_RG8 << 8): _convert<3, false, 2, false, false, false>(width, height, rptr, wptr); break;
  397. case FORMAT_RGB8 | (FORMAT_RGBA8 << 8): _convert<3, false, 3, true, false, false>(width, height, rptr, wptr); break;
  398. case FORMAT_RGBA8 | (FORMAT_L8 << 8): _convert<3, true, 1, false, false, true>(width, height, rptr, wptr); break;
  399. case FORMAT_RGBA8 | (FORMAT_LA8 << 8): _convert<3, true, 1, true, false, true>(width, height, rptr, wptr); break;
  400. case FORMAT_RGBA8 | (FORMAT_R8 << 8): _convert<3, true, 1, false, false, false>(width, height, rptr, wptr); break;
  401. case FORMAT_RGBA8 | (FORMAT_RG8 << 8): _convert<3, true, 2, false, false, false>(width, height, rptr, wptr); break;
  402. case FORMAT_RGBA8 | (FORMAT_RGB8 << 8): _convert<3, true, 3, false, false, false>(width, height, rptr, wptr); break;
  403. }
  404. r = PoolVector<uint8_t>::Read();
  405. w = PoolVector<uint8_t>::Write();
  406. bool gen_mipmaps = mipmaps;
  407. //mipmaps=false;
  408. _copy_internals_from(new_img);
  409. if (gen_mipmaps)
  410. generate_mipmaps();
  411. }
  412. Image::Format Image::get_format() const {
  413. return format;
  414. }
  415. static double _bicubic_interp_kernel(double x) {
  416. x = ABS(x);
  417. double bc = 0;
  418. if (x <= 1)
  419. bc = (1.5 * x - 2.5) * x * x + 1;
  420. else if (x < 2)
  421. bc = ((-0.5 * x + 2.5) * x - 4) * x + 2;
  422. return bc;
  423. }
  424. template <int CC>
  425. static void _scale_cubic(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  426. // get source image size
  427. int width = p_src_width;
  428. int height = p_src_height;
  429. double xfac = (double)width / p_dst_width;
  430. double yfac = (double)height / p_dst_height;
  431. // coordinates of source points and coefficients
  432. double ox, oy, dx, dy, k1, k2;
  433. int ox1, oy1, ox2, oy2;
  434. // destination pixel values
  435. // width and height decreased by 1
  436. int ymax = height - 1;
  437. int xmax = width - 1;
  438. // temporary pointer
  439. for (uint32_t y = 0; y < p_dst_height; y++) {
  440. // Y coordinates
  441. oy = (double)y * yfac - 0.5f;
  442. oy1 = (int)oy;
  443. dy = oy - (double)oy1;
  444. for (uint32_t x = 0; x < p_dst_width; x++) {
  445. // X coordinates
  446. ox = (double)x * xfac - 0.5f;
  447. ox1 = (int)ox;
  448. dx = ox - (double)ox1;
  449. // initial pixel value
  450. uint8_t *dst = p_dst + (y * p_dst_width + x) * CC;
  451. double color[CC];
  452. for (int i = 0; i < CC; i++) {
  453. color[i] = 0;
  454. }
  455. for (int n = -1; n < 3; n++) {
  456. // get Y coefficient
  457. k1 = _bicubic_interp_kernel(dy - (double)n);
  458. oy2 = oy1 + n;
  459. if (oy2 < 0)
  460. oy2 = 0;
  461. if (oy2 > ymax)
  462. oy2 = ymax;
  463. for (int m = -1; m < 3; m++) {
  464. // get X coefficient
  465. k2 = k1 * _bicubic_interp_kernel((double)m - dx);
  466. ox2 = ox1 + m;
  467. if (ox2 < 0)
  468. ox2 = 0;
  469. if (ox2 > xmax)
  470. ox2 = xmax;
  471. // get pixel of original image
  472. const uint8_t *p = p_src + (oy2 * p_src_width + ox2) * CC;
  473. for (int i = 0; i < CC; i++) {
  474. color[i] += p[i] * k2;
  475. }
  476. }
  477. }
  478. for (int i = 0; i < CC; i++) {
  479. dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255);
  480. }
  481. }
  482. }
  483. }
  484. template <int CC>
  485. static void _scale_bilinear(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  486. enum {
  487. FRAC_BITS = 8,
  488. FRAC_LEN = (1 << FRAC_BITS),
  489. FRAC_MASK = FRAC_LEN - 1
  490. };
  491. for (uint32_t i = 0; i < p_dst_height; i++) {
  492. uint32_t src_yofs_up_fp = (i * p_src_height * FRAC_LEN / p_dst_height);
  493. uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK;
  494. uint32_t src_yofs_up = src_yofs_up_fp >> FRAC_BITS;
  495. uint32_t src_yofs_down = (i + 1) * p_src_height / p_dst_height;
  496. if (src_yofs_down >= p_src_height)
  497. src_yofs_down = p_src_height - 1;
  498. //src_yofs_up*=CC;
  499. //src_yofs_down*=CC;
  500. uint32_t y_ofs_up = src_yofs_up * p_src_width * CC;
  501. uint32_t y_ofs_down = src_yofs_down * p_src_width * CC;
  502. for (uint32_t j = 0; j < p_dst_width; j++) {
  503. uint32_t src_xofs_left_fp = (j * p_src_width * FRAC_LEN / p_dst_width);
  504. uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK;
  505. uint32_t src_xofs_left = src_xofs_left_fp >> FRAC_BITS;
  506. uint32_t src_xofs_right = (j + 1) * p_src_width / p_dst_width;
  507. if (src_xofs_right >= p_src_width)
  508. src_xofs_right = p_src_width - 1;
  509. src_xofs_left *= CC;
  510. src_xofs_right *= CC;
  511. for (uint32_t l = 0; l < CC; l++) {
  512. uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS;
  513. uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS;
  514. uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS;
  515. uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS;
  516. uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS);
  517. uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS);
  518. uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS);
  519. interp >>= FRAC_BITS;
  520. p_dst[i * p_dst_width * CC + j * CC + l] = interp;
  521. }
  522. }
  523. }
  524. }
  525. template <int CC>
  526. static void _scale_nearest(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
  527. for (uint32_t i = 0; i < p_dst_height; i++) {
  528. uint32_t src_yofs = i * p_src_height / p_dst_height;
  529. uint32_t y_ofs = src_yofs * p_src_width * CC;
  530. for (uint32_t j = 0; j < p_dst_width; j++) {
  531. uint32_t src_xofs = j * p_src_width / p_dst_width;
  532. src_xofs *= CC;
  533. for (uint32_t l = 0; l < CC; l++) {
  534. uint32_t p = p_src[y_ofs + src_xofs + l];
  535. p_dst[i * p_dst_width * CC + j * CC + l] = p;
  536. }
  537. }
  538. }
  539. }
  540. void Image::resize_to_po2(bool p_square) {
  541. if (!_can_modify(format)) {
  542. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  543. ERR_FAIL();
  544. }
  545. int w = next_power_of_2(width);
  546. int h = next_power_of_2(height);
  547. if (w == width && h == height) {
  548. if (!p_square || w == h)
  549. return; //nothing to do
  550. }
  551. resize(w, h);
  552. }
  553. void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
  554. if (!_can_modify(format)) {
  555. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  556. ERR_FAIL();
  557. }
  558. ERR_FAIL_COND(p_width <= 0);
  559. ERR_FAIL_COND(p_height <= 0);
  560. ERR_FAIL_COND(p_width > MAX_WIDTH);
  561. ERR_FAIL_COND(p_height > MAX_HEIGHT);
  562. if (p_width == width && p_height == height)
  563. return;
  564. Image dst(p_width, p_height, 0, format);
  565. PoolVector<uint8_t>::Read r = data.read();
  566. const unsigned char *r_ptr = r.ptr();
  567. PoolVector<uint8_t>::Write w = dst.data.write();
  568. unsigned char *w_ptr = w.ptr();
  569. switch (p_interpolation) {
  570. case INTERPOLATE_NEAREST: {
  571. switch (get_format_pixel_size(format)) {
  572. case 1: _scale_nearest<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  573. case 2: _scale_nearest<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  574. case 3: _scale_nearest<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  575. case 4: _scale_nearest<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  576. }
  577. } break;
  578. case INTERPOLATE_BILINEAR: {
  579. switch (get_format_pixel_size(format)) {
  580. case 1: _scale_bilinear<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  581. case 2: _scale_bilinear<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  582. case 3: _scale_bilinear<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  583. case 4: _scale_bilinear<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  584. }
  585. } break;
  586. case INTERPOLATE_CUBIC: {
  587. switch (get_format_pixel_size(format)) {
  588. case 1: _scale_cubic<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  589. case 2: _scale_cubic<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  590. case 3: _scale_cubic<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  591. case 4: _scale_cubic<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  592. }
  593. } break;
  594. }
  595. r = PoolVector<uint8_t>::Read();
  596. w = PoolVector<uint8_t>::Write();
  597. if (mipmaps > 0)
  598. dst.generate_mipmaps();
  599. _copy_internals_from(dst);
  600. }
  601. void Image::crop(int p_width, int p_height) {
  602. if (!_can_modify(format)) {
  603. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  604. ERR_FAIL();
  605. }
  606. ERR_FAIL_COND(p_width <= 0);
  607. ERR_FAIL_COND(p_height <= 0);
  608. ERR_FAIL_COND(p_width > MAX_WIDTH);
  609. ERR_FAIL_COND(p_height > MAX_HEIGHT);
  610. /* to save memory, cropping should be done in-place, however, since this function
  611. will most likely either not be used much, or in critical areas, for now it wont, because
  612. it's a waste of time. */
  613. if (p_width == width && p_height == height)
  614. return;
  615. uint8_t pdata[16]; //largest is 16
  616. uint32_t pixel_size = get_format_pixel_size(format);
  617. Image dst(p_width, p_height, 0, format);
  618. {
  619. PoolVector<uint8_t>::Read r = data.read();
  620. PoolVector<uint8_t>::Write w = dst.data.write();
  621. for (int y = 0; y < p_height; y++) {
  622. for (int x = 0; x < p_width; x++) {
  623. if ((x >= width || y >= height)) {
  624. for (uint32_t i = 0; i < pixel_size; i++)
  625. pdata[i] = 0;
  626. } else {
  627. _get_pixelb(x, y, pixel_size, r.ptr(), pdata);
  628. }
  629. dst._put_pixelb(x, y, pixel_size, w.ptr(), pdata);
  630. }
  631. }
  632. }
  633. if (mipmaps > 0)
  634. dst.generate_mipmaps();
  635. _copy_internals_from(dst);
  636. }
  637. void Image::flip_y() {
  638. if (!_can_modify(format)) {
  639. ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats.");
  640. ERR_FAIL();
  641. }
  642. bool gm = mipmaps;
  643. if (gm)
  644. clear_mipmaps();
  645. {
  646. PoolVector<uint8_t>::Write w = data.write();
  647. uint8_t up[16];
  648. uint8_t down[16];
  649. uint32_t pixel_size = get_format_pixel_size(format);
  650. for (int y = 0; y < height / 2; y++) {
  651. for (int x = 0; x < width; x++) {
  652. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  653. _get_pixelb(x, height - y - 1, pixel_size, w.ptr(), down);
  654. _put_pixelb(x, height - y - 1, pixel_size, w.ptr(), up);
  655. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  656. }
  657. }
  658. }
  659. if (gm)
  660. generate_mipmaps();
  661. }
  662. void Image::flip_x() {
  663. if (!_can_modify(format)) {
  664. ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats.");
  665. ERR_FAIL();
  666. }
  667. bool gm = mipmaps;
  668. if (gm)
  669. clear_mipmaps();
  670. {
  671. PoolVector<uint8_t>::Write w = data.write();
  672. uint8_t up[16];
  673. uint8_t down[16];
  674. uint32_t pixel_size = get_format_pixel_size(format);
  675. for (int y = 0; y < height; y++) {
  676. for (int x = 0; x < width / 2; x++) {
  677. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  678. _get_pixelb(width - x - 1, y, pixel_size, w.ptr(), down);
  679. _put_pixelb(width - x - 1, y, pixel_size, w.ptr(), up);
  680. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  681. }
  682. }
  683. }
  684. if (gm)
  685. generate_mipmaps();
  686. }
  687. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps) {
  688. int size = 0;
  689. int w = p_width;
  690. int h = p_height;
  691. int mm = 0;
  692. int pixsize = get_format_pixel_size(p_format);
  693. int pixshift = get_format_pixel_rshift(p_format);
  694. int block = get_format_block_size(p_format);
  695. int minw, minh;
  696. get_format_min_pixel_size(p_format, minw, minh);
  697. while (true) {
  698. int bw = w % block != 0 ? w + (block - w % block) : w;
  699. int bh = h % block != 0 ? h + (block - h % block) : h;
  700. int s = bw * bh;
  701. s *= pixsize;
  702. s >>= pixshift;
  703. size += s;
  704. if (p_mipmaps >= 0 && mm == p_mipmaps)
  705. break;
  706. if (p_mipmaps >= 0) {
  707. w = MAX(minw, w >> 1);
  708. h = MAX(minh, h >> 1);
  709. } else {
  710. if (w == minw && h == minh)
  711. break;
  712. w = MAX(minw, w >> 1);
  713. h = MAX(minh, h >> 1);
  714. }
  715. mm++;
  716. };
  717. r_mipmaps = mm;
  718. return size;
  719. }
  720. bool Image::_can_modify(Format p_format) const {
  721. return p_format <= FORMAT_RGBE9995;
  722. }
  723. template <int CC>
  724. static void _generate_po2_mipmap(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_width, uint32_t p_height) {
  725. //fast power of 2 mipmap generation
  726. uint32_t dst_w = p_width >> 1;
  727. uint32_t dst_h = p_height >> 1;
  728. for (uint32_t i = 0; i < dst_h; i++) {
  729. const uint8_t *rup_ptr = &p_src[i * 2 * p_width * CC];
  730. const uint8_t *rdown_ptr = rup_ptr + p_width * CC;
  731. uint8_t *dst_ptr = &p_dst[i * dst_w * CC];
  732. uint32_t count = dst_w;
  733. while (count--) {
  734. for (int j = 0; j < CC; j++) {
  735. uint16_t val = 0;
  736. val += rup_ptr[j];
  737. val += rup_ptr[j + CC];
  738. val += rdown_ptr[j];
  739. val += rdown_ptr[j + CC];
  740. dst_ptr[j] = val >> 2;
  741. }
  742. dst_ptr += CC;
  743. rup_ptr += CC * 2;
  744. rdown_ptr += CC * 2;
  745. }
  746. }
  747. }
  748. void Image::expand_x2_hq2x() {
  749. ERR_FAIL_COND(!_can_modify(format));
  750. Format current = format;
  751. bool mm = has_mipmaps();
  752. if (mm) {
  753. clear_mipmaps();
  754. }
  755. if (current != FORMAT_RGBA8)
  756. convert(FORMAT_RGBA8);
  757. PoolVector<uint8_t> dest;
  758. dest.resize(width * 2 * height * 2 * 4);
  759. {
  760. PoolVector<uint8_t>::Read r = data.read();
  761. PoolVector<uint8_t>::Write w = dest.write();
  762. hq2x_resize((const uint32_t *)r.ptr(), width, height, (uint32_t *)w.ptr());
  763. }
  764. width *= 2;
  765. height *= 2;
  766. data = dest;
  767. if (current != FORMAT_RGBA8)
  768. convert(current);
  769. if (mipmaps) {
  770. generate_mipmaps();
  771. }
  772. }
  773. void Image::shrink_x2() {
  774. ERR_FAIL_COND(data.size() == 0);
  775. if (mipmaps) {
  776. //just use the lower mipmap as base and copy all
  777. PoolVector<uint8_t> new_img;
  778. int ofs = get_mipmap_offset(1);
  779. int new_size = data.size() - ofs;
  780. new_img.resize(new_size);
  781. {
  782. PoolVector<uint8_t>::Write w = new_img.write();
  783. PoolVector<uint8_t>::Read r = data.read();
  784. copymem(w.ptr(), &r[ofs], new_size);
  785. }
  786. width = MAX(width / 2, 1);
  787. height = MAX(height / 2, 1);
  788. data = new_img;
  789. } else {
  790. PoolVector<uint8_t> new_img;
  791. ERR_FAIL_COND(!_can_modify(format));
  792. int ps = get_format_pixel_size(format);
  793. new_img.resize((width / 2) * (height / 2) * ps);
  794. {
  795. PoolVector<uint8_t>::Write w = new_img.write();
  796. PoolVector<uint8_t>::Read r = data.read();
  797. switch (format) {
  798. case FORMAT_L8:
  799. case FORMAT_R8: _generate_po2_mipmap<1>(r.ptr(), w.ptr(), width, height); break;
  800. case FORMAT_LA8: _generate_po2_mipmap<2>(r.ptr(), w.ptr(), width, height); break;
  801. case FORMAT_RG8: _generate_po2_mipmap<2>(r.ptr(), w.ptr(), width, height); break;
  802. case FORMAT_RGB8: _generate_po2_mipmap<3>(r.ptr(), w.ptr(), width, height); break;
  803. case FORMAT_RGBA8: _generate_po2_mipmap<4>(r.ptr(), w.ptr(), width, height); break;
  804. default: {}
  805. }
  806. }
  807. width /= 2;
  808. height /= 2;
  809. data = new_img;
  810. }
  811. }
  812. Error Image::generate_mipmaps() {
  813. if (!_can_modify(format)) {
  814. ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats.");
  815. ERR_FAIL_V(ERR_UNAVAILABLE);
  816. }
  817. ERR_FAIL_COND_V(width == 0 || height == 0, ERR_UNCONFIGURED);
  818. int mmcount;
  819. int size = _get_dst_image_size(width, height, format, mmcount);
  820. data.resize(size);
  821. PoolVector<uint8_t>::Write wp = data.write();
  822. if (next_power_of_2(width) == uint32_t(width) && next_power_of_2(height) == uint32_t(height)) {
  823. //use fast code for powers of 2
  824. int prev_ofs = 0;
  825. int prev_h = height;
  826. int prev_w = width;
  827. for (int i = 1; i < mmcount; i++) {
  828. int ofs, w, h;
  829. _get_mipmap_offset_and_size(i, ofs, w, h);
  830. switch (format) {
  831. case FORMAT_L8:
  832. case FORMAT_R8: _generate_po2_mipmap<1>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  833. case FORMAT_LA8:
  834. case FORMAT_RG8: _generate_po2_mipmap<2>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  835. case FORMAT_RGB8: _generate_po2_mipmap<3>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  836. case FORMAT_RGBA8: _generate_po2_mipmap<4>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  837. default: {}
  838. }
  839. prev_ofs = ofs;
  840. prev_w = w;
  841. prev_h = h;
  842. }
  843. } else {
  844. //use slow code..
  845. //use bilinear filtered code for non powers of 2
  846. int prev_ofs = 0;
  847. int prev_h = height;
  848. int prev_w = width;
  849. for (int i = 1; i < mmcount; i++) {
  850. int ofs, w, h;
  851. _get_mipmap_offset_and_size(i, ofs, w, h);
  852. switch (format) {
  853. case FORMAT_L8:
  854. case FORMAT_R8: _scale_bilinear<1>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  855. case FORMAT_LA8:
  856. case FORMAT_RG8: _scale_bilinear<2>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  857. case FORMAT_RGB8: _scale_bilinear<3>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  858. case FORMAT_RGBA8: _scale_bilinear<4>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h, w, h); break;
  859. default: {}
  860. }
  861. prev_ofs = ofs;
  862. prev_w = w;
  863. prev_h = h;
  864. }
  865. }
  866. mipmaps = true;
  867. return OK;
  868. }
  869. void Image::clear_mipmaps() {
  870. if (!mipmaps)
  871. return;
  872. if (empty())
  873. return;
  874. int ofs, w, h;
  875. _get_mipmap_offset_and_size(1, ofs, w, h);
  876. data.resize(ofs);
  877. mipmaps = false;
  878. }
  879. bool Image::empty() const {
  880. return (data.size() == 0);
  881. }
  882. PoolVector<uint8_t> Image::get_data() const {
  883. return data;
  884. }
  885. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  886. int mm = 0;
  887. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  888. data.resize(size);
  889. {
  890. PoolVector<uint8_t>::Write w = data.write();
  891. zeromem(w.ptr(), size);
  892. }
  893. width = p_width;
  894. height = p_height;
  895. mipmaps = p_use_mipmaps;
  896. format = p_format;
  897. }
  898. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  899. ERR_FAIL_INDEX(p_width - 1, MAX_WIDTH);
  900. ERR_FAIL_INDEX(p_height - 1, MAX_HEIGHT);
  901. int mm;
  902. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  903. if (size != p_data.size()) {
  904. ERR_EXPLAIN("Expected data size of " + itos(size) + " in Image::create()");
  905. ERR_FAIL_COND(p_data.size() != size);
  906. }
  907. height = p_height;
  908. width = p_width;
  909. format = p_format;
  910. data = p_data;
  911. mipmaps = p_use_mipmaps;
  912. }
  913. void Image::create(const char **p_xpm) {
  914. int size_width, size_height;
  915. int pixelchars = 0;
  916. mipmaps = false;
  917. bool has_alpha = false;
  918. enum Status {
  919. READING_HEADER,
  920. READING_COLORS,
  921. READING_PIXELS,
  922. DONE
  923. };
  924. Status status = READING_HEADER;
  925. int line = 0;
  926. HashMap<String, Color> colormap;
  927. int colormap_size;
  928. uint32_t pixel_size;
  929. PoolVector<uint8_t>::Write w;
  930. while (status != DONE) {
  931. const char *line_ptr = p_xpm[line];
  932. switch (status) {
  933. case READING_HEADER: {
  934. String line_str = line_ptr;
  935. line_str.replace("\t", " ");
  936. size_width = line_str.get_slicec(' ', 0).to_int();
  937. size_height = line_str.get_slicec(' ', 1).to_int();
  938. colormap_size = line_str.get_slicec(' ', 2).to_int();
  939. pixelchars = line_str.get_slicec(' ', 3).to_int();
  940. ERR_FAIL_COND(colormap_size > 32766);
  941. ERR_FAIL_COND(pixelchars > 5);
  942. ERR_FAIL_COND(size_width > 32767);
  943. ERR_FAIL_COND(size_height > 32767);
  944. status = READING_COLORS;
  945. } break;
  946. case READING_COLORS: {
  947. String colorstring;
  948. for (int i = 0; i < pixelchars; i++) {
  949. colorstring += *line_ptr;
  950. line_ptr++;
  951. }
  952. //skip spaces
  953. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  954. if (*line_ptr == 0)
  955. break;
  956. line_ptr++;
  957. }
  958. if (*line_ptr == 'c') {
  959. line_ptr++;
  960. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  961. if (*line_ptr == 0)
  962. break;
  963. line_ptr++;
  964. }
  965. if (*line_ptr == '#') {
  966. line_ptr++;
  967. uint8_t col_r = 0;
  968. uint8_t col_g = 0;
  969. uint8_t col_b = 0;
  970. //uint8_t col_a=255;
  971. for (int i = 0; i < 6; i++) {
  972. char v = line_ptr[i];
  973. if (v >= '0' && v <= '9')
  974. v -= '0';
  975. else if (v >= 'A' && v <= 'F')
  976. v = (v - 'A') + 10;
  977. else if (v >= 'a' && v <= 'f')
  978. v = (v - 'a') + 10;
  979. else
  980. break;
  981. switch (i) {
  982. case 0: col_r = v << 4; break;
  983. case 1: col_r |= v; break;
  984. case 2: col_g = v << 4; break;
  985. case 3: col_g |= v; break;
  986. case 4: col_b = v << 4; break;
  987. case 5: col_b |= v; break;
  988. };
  989. }
  990. // magenta mask
  991. if (col_r == 255 && col_g == 0 && col_b == 255) {
  992. colormap[colorstring] = Color(0, 0, 0, 0);
  993. has_alpha = true;
  994. } else {
  995. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  996. }
  997. }
  998. }
  999. if (line == colormap_size) {
  1000. status = READING_PIXELS;
  1001. create(size_width, size_height, 0, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8);
  1002. w = data.write();
  1003. pixel_size = has_alpha ? 4 : 3;
  1004. }
  1005. } break;
  1006. case READING_PIXELS: {
  1007. int y = line - colormap_size - 1;
  1008. for (int x = 0; x < size_width; x++) {
  1009. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  1010. for (int i = 0; i < pixelchars; i++)
  1011. pixelstr[i] = line_ptr[x * pixelchars + i];
  1012. Color *colorptr = colormap.getptr(pixelstr);
  1013. ERR_FAIL_COND(!colorptr);
  1014. uint8_t pixel[4];
  1015. for (uint32_t i = 0; i < pixel_size; i++) {
  1016. pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255);
  1017. }
  1018. _put_pixelb(x, y, pixel_size, w.ptr(), pixel);
  1019. }
  1020. if (y == (size_height - 1))
  1021. status = DONE;
  1022. } break;
  1023. default: {}
  1024. }
  1025. line++;
  1026. }
  1027. }
  1028. #define DETECT_ALPHA_MAX_THRESHOLD 254
  1029. #define DETECT_ALPHA_MIN_THRESHOLD 2
  1030. #define DETECT_ALPHA(m_value) \
  1031. { \
  1032. uint8_t value = m_value; \
  1033. if (value < DETECT_ALPHA_MIN_THRESHOLD) \
  1034. bit = true; \
  1035. else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \
  1036. \
  1037. detected = true; \
  1038. break; \
  1039. } \
  1040. }
  1041. #define DETECT_NON_ALPHA(m_value) \
  1042. { \
  1043. uint8_t value = m_value; \
  1044. if (value > 0) { \
  1045. \
  1046. detected = true; \
  1047. break; \
  1048. } \
  1049. }
  1050. bool Image::is_invisible() const {
  1051. if (format == FORMAT_L8 ||
  1052. format == FORMAT_RGB8 || format == FORMAT_RG8)
  1053. return false;
  1054. int len = data.size();
  1055. if (len == 0)
  1056. return true;
  1057. int w, h;
  1058. _get_mipmap_offset_and_size(1, len, w, h);
  1059. PoolVector<uint8_t>::Read r = data.read();
  1060. const unsigned char *data_ptr = r.ptr();
  1061. bool detected = false;
  1062. switch (format) {
  1063. case FORMAT_LA8: {
  1064. for (int i = 0; i < (len >> 1); i++) {
  1065. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  1066. }
  1067. } break;
  1068. case FORMAT_RGBA8: {
  1069. for (int i = 0; i < (len >> 2); i++) {
  1070. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  1071. }
  1072. } break;
  1073. case FORMAT_PVRTC2A:
  1074. case FORMAT_PVRTC4A:
  1075. case FORMAT_DXT3:
  1076. case FORMAT_DXT5: {
  1077. detected = true;
  1078. } break;
  1079. default: {}
  1080. }
  1081. return !detected;
  1082. }
  1083. Image::AlphaMode Image::detect_alpha() const {
  1084. int len = data.size();
  1085. if (len == 0)
  1086. return ALPHA_NONE;
  1087. int w, h;
  1088. _get_mipmap_offset_and_size(1, len, w, h);
  1089. PoolVector<uint8_t>::Read r = data.read();
  1090. const unsigned char *data_ptr = r.ptr();
  1091. bool bit = false;
  1092. bool detected = false;
  1093. switch (format) {
  1094. case FORMAT_LA8: {
  1095. for (int i = 0; i < (len >> 1); i++) {
  1096. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  1097. }
  1098. } break;
  1099. case FORMAT_RGBA8: {
  1100. for (int i = 0; i < (len >> 2); i++) {
  1101. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  1102. }
  1103. } break;
  1104. case FORMAT_PVRTC2A:
  1105. case FORMAT_PVRTC4A:
  1106. case FORMAT_DXT3:
  1107. case FORMAT_DXT5: {
  1108. detected = true;
  1109. } break;
  1110. default: {}
  1111. }
  1112. if (detected)
  1113. return ALPHA_BLEND;
  1114. else if (bit)
  1115. return ALPHA_BIT;
  1116. else
  1117. return ALPHA_NONE;
  1118. }
  1119. Error Image::load(const String &p_path) {
  1120. return ImageLoader::load_image(p_path, this);
  1121. }
  1122. Error Image::save_png(const String &p_path) const {
  1123. if (save_png_func == NULL)
  1124. return ERR_UNAVAILABLE;
  1125. return save_png_func(p_path, Ref<Image>((Image *)this));
  1126. }
  1127. int Image::get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps) {
  1128. int mm;
  1129. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps);
  1130. }
  1131. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1132. int mm;
  1133. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  1134. return mm;
  1135. }
  1136. bool Image::is_compressed() const {
  1137. return format > FORMAT_RGBE9995;
  1138. }
  1139. Error Image::decompress() {
  1140. if (format >= FORMAT_DXT1 && format <= FORMAT_BPTC_RGBFU && _image_decompress_bc)
  1141. _image_decompress_bc(this);
  1142. else if (format >= FORMAT_PVRTC2 && format <= FORMAT_PVRTC4A && _image_decompress_pvrtc)
  1143. _image_decompress_pvrtc(this);
  1144. else if (format == FORMAT_ETC && _image_decompress_etc1)
  1145. _image_decompress_etc1(this);
  1146. else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RGB8A1 && _image_decompress_etc1)
  1147. _image_decompress_etc2(this);
  1148. else
  1149. return ERR_UNAVAILABLE;
  1150. return OK;
  1151. }
  1152. Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
  1153. switch (p_mode) {
  1154. case COMPRESS_S3TC: {
  1155. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1156. _image_compress_bc_func(this, p_source);
  1157. } break;
  1158. case COMPRESS_PVRTC2: {
  1159. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1160. _image_compress_pvrtc2_func(this);
  1161. } break;
  1162. case COMPRESS_PVRTC4: {
  1163. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1164. _image_compress_pvrtc4_func(this);
  1165. } break;
  1166. case COMPRESS_ETC: {
  1167. ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE);
  1168. _image_compress_etc1_func(this, p_lossy_quality);
  1169. } break;
  1170. case COMPRESS_ETC2: {
  1171. ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE);
  1172. _image_compress_etc2_func(this, p_lossy_quality, p_source);
  1173. } break;
  1174. }
  1175. return OK;
  1176. }
  1177. Image::Image(const char **p_xpm) {
  1178. width = 0;
  1179. height = 0;
  1180. mipmaps = false;
  1181. format = FORMAT_L8;
  1182. create(p_xpm);
  1183. }
  1184. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1185. width = 0;
  1186. height = 0;
  1187. mipmaps = p_use_mipmaps;
  1188. format = FORMAT_L8;
  1189. create(p_width, p_height, p_use_mipmaps, p_format);
  1190. }
  1191. Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  1192. width = 0;
  1193. height = 0;
  1194. mipmaps = p_mipmaps;
  1195. format = FORMAT_L8;
  1196. create(p_width, p_height, p_mipmaps, p_format, p_data);
  1197. }
  1198. Rect2 Image::get_used_rect() const {
  1199. if (format != FORMAT_LA8 && format != FORMAT_RGBA8)
  1200. return Rect2(Point2(), Size2(width, height));
  1201. int len = data.size();
  1202. if (len == 0)
  1203. return Rect2();
  1204. //int data_size = len;
  1205. PoolVector<uint8_t>::Read r = data.read();
  1206. const unsigned char *rptr = r.ptr();
  1207. int ps = format == FORMAT_LA8 ? 2 : 4;
  1208. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  1209. int maxx = -1, maxy = -1;
  1210. for (int j = 0; j < height; j++) {
  1211. for (int i = 0; i < width; i++) {
  1212. bool opaque = rptr[(j * width + i) * ps + (ps - 1)] > 2;
  1213. if (!opaque)
  1214. continue;
  1215. if (i > maxx)
  1216. maxx = i;
  1217. if (j > maxy)
  1218. maxy = j;
  1219. if (i < minx)
  1220. minx = i;
  1221. if (j < miny)
  1222. miny = j;
  1223. }
  1224. }
  1225. if (maxx == -1)
  1226. return Rect2();
  1227. else
  1228. return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1);
  1229. }
  1230. Ref<Image> Image::get_rect(const Rect2 &p_area) const {
  1231. Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format));
  1232. img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0));
  1233. return img;
  1234. }
  1235. void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1236. ERR_FAIL_COND(p_src.is_null());
  1237. int dsize = data.size();
  1238. int srcdsize = p_src->data.size();
  1239. ERR_FAIL_COND(dsize == 0);
  1240. ERR_FAIL_COND(srcdsize == 0);
  1241. ERR_FAIL_COND(format != p_src->format);
  1242. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1243. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1244. return;
  1245. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1246. PoolVector<uint8_t>::Write wp = data.write();
  1247. uint8_t *dst_data_ptr = wp.ptr();
  1248. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1249. const uint8_t *src_data_ptr = rp.ptr();
  1250. int pixel_size = get_format_pixel_size(format);
  1251. for (int i = 0; i < dest_rect.size.y; i++) {
  1252. for (int j = 0; j < dest_rect.size.x; j++) {
  1253. int src_x = clipped_src_rect.position.x + j;
  1254. int src_y = clipped_src_rect.position.y + i;
  1255. int dst_x = dest_rect.position.x + j;
  1256. int dst_y = dest_rect.position.y + i;
  1257. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1258. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1259. for (int k = 0; k < pixel_size; k++) {
  1260. dst[k] = src[k];
  1261. }
  1262. }
  1263. }
  1264. }
  1265. void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1266. ERR_FAIL_COND(p_src.is_null());
  1267. ERR_FAIL_COND(p_mask.is_null());
  1268. int dsize = data.size();
  1269. int srcdsize = p_src->data.size();
  1270. int maskdsize = p_mask->data.size();
  1271. ERR_FAIL_COND(dsize == 0);
  1272. ERR_FAIL_COND(srcdsize == 0);
  1273. ERR_FAIL_COND(maskdsize == 0);
  1274. ERR_FAIL_COND(p_src->width != p_mask->width);
  1275. ERR_FAIL_COND(p_src->height != p_mask->height);
  1276. ERR_FAIL_COND(format != p_src->format);
  1277. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1278. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1279. return;
  1280. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1281. PoolVector<uint8_t>::Write wp = data.write();
  1282. uint8_t *dst_data_ptr = wp.ptr();
  1283. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1284. const uint8_t *src_data_ptr = rp.ptr();
  1285. int pixel_size = get_format_pixel_size(format);
  1286. Ref<Image> msk = p_mask;
  1287. msk->lock();
  1288. for (int i = 0; i < dest_rect.size.y; i++) {
  1289. for (int j = 0; j < dest_rect.size.x; j++) {
  1290. int src_x = clipped_src_rect.position.x + j;
  1291. int src_y = clipped_src_rect.position.y + i;
  1292. if (msk->get_pixel(src_x, src_y).a != 0) {
  1293. int dst_x = dest_rect.position.x + j;
  1294. int dst_y = dest_rect.position.y + i;
  1295. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1296. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1297. for (int k = 0; k < pixel_size; k++) {
  1298. dst[k] = src[k];
  1299. }
  1300. }
  1301. }
  1302. }
  1303. msk->unlock();
  1304. }
  1305. void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1306. ERR_FAIL_COND(p_src.is_null());
  1307. int dsize = data.size();
  1308. int srcdsize = p_src->data.size();
  1309. ERR_FAIL_COND(dsize == 0);
  1310. ERR_FAIL_COND(srcdsize == 0);
  1311. ERR_FAIL_COND(format != p_src->format);
  1312. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1313. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1314. return;
  1315. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1316. lock();
  1317. Ref<Image> img = p_src;
  1318. img->lock();
  1319. for (int i = 0; i < dest_rect.size.y; i++) {
  1320. for (int j = 0; j < dest_rect.size.x; j++) {
  1321. int src_x = clipped_src_rect.position.x + j;
  1322. int src_y = clipped_src_rect.position.y + i;
  1323. int dst_x = dest_rect.position.x + j;
  1324. int dst_y = dest_rect.position.y + i;
  1325. Color sc = img->get_pixel(src_x, src_y);
  1326. Color dc = get_pixel(dst_x, dst_y);
  1327. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  1328. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  1329. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  1330. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  1331. set_pixel(dst_x, dst_y, dc);
  1332. }
  1333. }
  1334. img->unlock();
  1335. unlock();
  1336. }
  1337. void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1338. ERR_FAIL_COND(p_src.is_null());
  1339. ERR_FAIL_COND(p_mask.is_null());
  1340. int dsize = data.size();
  1341. int srcdsize = p_src->data.size();
  1342. int maskdsize = p_mask->data.size();
  1343. ERR_FAIL_COND(dsize == 0);
  1344. ERR_FAIL_COND(srcdsize == 0);
  1345. ERR_FAIL_COND(maskdsize == 0);
  1346. ERR_FAIL_COND(p_src->width != p_mask->width);
  1347. ERR_FAIL_COND(p_src->height != p_mask->height);
  1348. ERR_FAIL_COND(format != p_src->format);
  1349. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1350. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1351. return;
  1352. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1353. lock();
  1354. Ref<Image> img = p_src;
  1355. Ref<Image> msk = p_mask;
  1356. img->lock();
  1357. msk->lock();
  1358. for (int i = 0; i < dest_rect.size.y; i++) {
  1359. for (int j = 0; j < dest_rect.size.x; j++) {
  1360. int src_x = clipped_src_rect.position.x + j;
  1361. int src_y = clipped_src_rect.position.y + i;
  1362. // If the mask's pixel is transparent then we skip it
  1363. //Color c = msk->get_pixel(src_x, src_y);
  1364. //if (c.a == 0) continue;
  1365. if (msk->get_pixel(src_x, src_y).a != 0) {
  1366. int dst_x = dest_rect.position.x + j;
  1367. int dst_y = dest_rect.position.y + i;
  1368. Color sc = img->get_pixel(src_x, src_y);
  1369. Color dc = get_pixel(dst_x, dst_y);
  1370. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  1371. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  1372. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  1373. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  1374. set_pixel(dst_x, dst_y, dc);
  1375. }
  1376. }
  1377. }
  1378. msk->unlock();
  1379. img->unlock();
  1380. unlock();
  1381. }
  1382. void Image::fill(const Color &c) {
  1383. lock();
  1384. PoolVector<uint8_t>::Write wp = data.write();
  1385. uint8_t *dst_data_ptr = wp.ptr();
  1386. int pixel_size = get_format_pixel_size(format);
  1387. // put first pixel with the format-aware API
  1388. set_pixel(0, 0, c);
  1389. for (int y = 0; y < height; y++) {
  1390. for (int x = 0; x < width; x++) {
  1391. uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size];
  1392. for (int k = 0; k < pixel_size; k++) {
  1393. dst[k] = dst_data_ptr[k];
  1394. }
  1395. }
  1396. }
  1397. unlock();
  1398. }
  1399. Ref<Image> (*Image::_png_mem_loader_func)(const uint8_t *, int) = NULL;
  1400. Ref<Image> (*Image::_jpg_mem_loader_func)(const uint8_t *, int) = NULL;
  1401. void (*Image::_image_compress_bc_func)(Image *, Image::CompressSource) = NULL;
  1402. void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL;
  1403. void (*Image::_image_compress_pvrtc4_func)(Image *) = NULL;
  1404. void (*Image::_image_compress_etc1_func)(Image *, float) = NULL;
  1405. void (*Image::_image_compress_etc2_func)(Image *, float, Image::CompressSource) = NULL;
  1406. void (*Image::_image_decompress_pvrtc)(Image *) = NULL;
  1407. void (*Image::_image_decompress_bc)(Image *) = NULL;
  1408. void (*Image::_image_decompress_etc1)(Image *) = NULL;
  1409. void (*Image::_image_decompress_etc2)(Image *) = NULL;
  1410. PoolVector<uint8_t> (*Image::lossy_packer)(const Ref<Image> &, float) = NULL;
  1411. Ref<Image> (*Image::lossy_unpacker)(const PoolVector<uint8_t> &) = NULL;
  1412. PoolVector<uint8_t> (*Image::lossless_packer)(const Ref<Image> &) = NULL;
  1413. Ref<Image> (*Image::lossless_unpacker)(const PoolVector<uint8_t> &) = NULL;
  1414. void Image::_set_data(const Dictionary &p_data) {
  1415. ERR_FAIL_COND(!p_data.has("width"));
  1416. ERR_FAIL_COND(!p_data.has("height"));
  1417. ERR_FAIL_COND(!p_data.has("format"));
  1418. ERR_FAIL_COND(!p_data.has("mipmaps"));
  1419. ERR_FAIL_COND(!p_data.has("data"));
  1420. int dwidth = p_data["width"];
  1421. int dheight = p_data["height"];
  1422. String dformat = p_data["format"];
  1423. bool dmipmaps = p_data["mipmaps"];
  1424. PoolVector<uint8_t> ddata = p_data["data"];
  1425. Format ddformat = FORMAT_MAX;
  1426. for (int i = 0; i < FORMAT_MAX; i++) {
  1427. if (dformat == get_format_name(Format(i))) {
  1428. ddformat = Format(i);
  1429. break;
  1430. }
  1431. }
  1432. ERR_FAIL_COND(ddformat == FORMAT_MAX);
  1433. create(dwidth, dheight, dmipmaps, ddformat, ddata);
  1434. }
  1435. Dictionary Image::_get_data() const {
  1436. Dictionary d;
  1437. d["width"] = width;
  1438. d["height"] = height;
  1439. d["format"] = get_format_name(format);
  1440. d["mipmaps"] = mipmaps;
  1441. d["data"] = data;
  1442. return d;
  1443. }
  1444. void Image::lock() {
  1445. ERR_FAIL_COND(data.size() == 0);
  1446. write_lock = data.write();
  1447. }
  1448. void Image::unlock() {
  1449. write_lock = PoolVector<uint8_t>::Write();
  1450. }
  1451. Color Image::get_pixel(int p_x, int p_y) const {
  1452. uint8_t *ptr = write_lock.ptr();
  1453. #ifdef DEBUG_ENABLED
  1454. if (!ptr) {
  1455. ERR_EXPLAIN("Image must be locked with 'lock()' before using get_pixel()");
  1456. ERR_FAIL_COND_V(!ptr, Color());
  1457. }
  1458. ERR_FAIL_INDEX_V(p_x, width, Color());
  1459. ERR_FAIL_INDEX_V(p_y, height, Color());
  1460. #endif
  1461. uint32_t ofs = p_y * width + p_x;
  1462. switch (format) {
  1463. case FORMAT_L8: {
  1464. float l = ptr[ofs] / 255.0;
  1465. return Color(l, l, l, 1);
  1466. } break;
  1467. case FORMAT_LA8: {
  1468. float l = ptr[ofs * 2 + 0] / 255.0;
  1469. float a = ptr[ofs * 2 + 1] / 255.0;
  1470. return Color(l, l, l, a);
  1471. } break;
  1472. case FORMAT_R8: {
  1473. float r = ptr[ofs] / 255.0;
  1474. return Color(r, 0, 0, 1);
  1475. } break;
  1476. case FORMAT_RG8: {
  1477. float r = ptr[ofs * 2 + 0] / 255.0;
  1478. float g = ptr[ofs * 2 + 1] / 255.0;
  1479. return Color(r, g, 0, 1);
  1480. } break;
  1481. case FORMAT_RGB8: {
  1482. float r = ptr[ofs * 3 + 0] / 255.0;
  1483. float g = ptr[ofs * 3 + 1] / 255.0;
  1484. float b = ptr[ofs * 3 + 2] / 255.0;
  1485. return Color(r, g, b, 1);
  1486. } break;
  1487. case FORMAT_RGBA8: {
  1488. float r = ptr[ofs * 4 + 0] / 255.0;
  1489. float g = ptr[ofs * 4 + 1] / 255.0;
  1490. float b = ptr[ofs * 4 + 2] / 255.0;
  1491. float a = ptr[ofs * 4 + 3] / 255.0;
  1492. return Color(r, g, b, a);
  1493. } break;
  1494. case FORMAT_RGBA4444: {
  1495. uint16_t u = ((uint16_t *)ptr)[ofs];
  1496. float r = (u & 0xF) / 15.0;
  1497. float g = ((u >> 4) & 0xF) / 15.0;
  1498. float b = ((u >> 8) & 0xF) / 15.0;
  1499. float a = ((u >> 12) & 0xF) / 15.0;
  1500. return Color(r, g, b, a);
  1501. } break;
  1502. case FORMAT_RGBA5551: {
  1503. uint16_t u = ((uint16_t *)ptr)[ofs];
  1504. float r = (u & 0x1F) / 15.0;
  1505. float g = ((u >> 5) & 0x1F) / 15.0;
  1506. float b = ((u >> 10) & 0x1F) / 15.0;
  1507. float a = ((u >> 15) & 0x1) / 1.0;
  1508. return Color(r, g, b, a);
  1509. } break;
  1510. case FORMAT_RF: {
  1511. float r = ((float *)ptr)[ofs];
  1512. return Color(r, 0, 0, 1);
  1513. } break;
  1514. case FORMAT_RGF: {
  1515. float r = ((float *)ptr)[ofs * 2 + 0];
  1516. float g = ((float *)ptr)[ofs * 2 + 1];
  1517. return Color(r, g, 0, 1);
  1518. } break;
  1519. case FORMAT_RGBF: {
  1520. float r = ((float *)ptr)[ofs * 3 + 0];
  1521. float g = ((float *)ptr)[ofs * 3 + 1];
  1522. float b = ((float *)ptr)[ofs * 3 + 2];
  1523. return Color(r, g, b, 1);
  1524. } break;
  1525. case FORMAT_RGBAF: {
  1526. float r = ((float *)ptr)[ofs * 4 + 0];
  1527. float g = ((float *)ptr)[ofs * 4 + 1];
  1528. float b = ((float *)ptr)[ofs * 4 + 2];
  1529. float a = ((float *)ptr)[ofs * 4 + 3];
  1530. return Color(r, g, b, a);
  1531. } break;
  1532. case FORMAT_RH: {
  1533. uint16_t r = ((uint16_t *)ptr)[ofs];
  1534. return Color(Math::half_to_float(r), 0, 0, 1);
  1535. } break;
  1536. case FORMAT_RGH: {
  1537. uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0];
  1538. uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1];
  1539. return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1);
  1540. } break;
  1541. case FORMAT_RGBH: {
  1542. uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0];
  1543. uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1];
  1544. uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2];
  1545. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1);
  1546. } break;
  1547. case FORMAT_RGBAH: {
  1548. uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0];
  1549. uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1];
  1550. uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2];
  1551. uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3];
  1552. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a));
  1553. } break;
  1554. case FORMAT_RGBE9995: {
  1555. uint32_t rgbe = ((uint32_t *)ptr)[ofs];
  1556. float r = rgbe & 0x1ff;
  1557. float g = (rgbe >> 9) & 0x1ff;
  1558. float b = (rgbe >> 18) & 0x1ff;
  1559. float e = (rgbe >> 27);
  1560. float m = Math::pow(2, e - 15.0 - 9.0);
  1561. ;
  1562. float rd = r * m;
  1563. float gd = g * m;
  1564. float bd = b * m;
  1565. return Color(rd, gd, bd, 1.0);
  1566. } break;
  1567. default: {
  1568. ERR_EXPLAIN("Can't get_pixel() on compressed image, sorry.");
  1569. ERR_FAIL_V(Color());
  1570. }
  1571. }
  1572. return Color();
  1573. }
  1574. void Image::set_pixel(int p_x, int p_y, const Color &p_color) {
  1575. uint8_t *ptr = write_lock.ptr();
  1576. #ifdef DEBUG_ENABLED
  1577. if (!ptr) {
  1578. ERR_EXPLAIN("Image must be locked with 'lock()' before using set_pixel()");
  1579. ERR_FAIL_COND(!ptr);
  1580. }
  1581. ERR_FAIL_INDEX(p_x, width);
  1582. ERR_FAIL_INDEX(p_y, height);
  1583. #endif
  1584. uint32_t ofs = p_y * width + p_x;
  1585. switch (format) {
  1586. case FORMAT_L8: {
  1587. ptr[ofs] = uint8_t(CLAMP(p_color.gray() * 255.0, 0, 255));
  1588. } break;
  1589. case FORMAT_LA8: {
  1590. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.gray() * 255.0, 0, 255));
  1591. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  1592. } break;
  1593. case FORMAT_R8: {
  1594. ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1595. } break;
  1596. case FORMAT_RG8: {
  1597. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1598. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1599. } break;
  1600. case FORMAT_RGB8: {
  1601. ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1602. ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1603. ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  1604. } break;
  1605. case FORMAT_RGBA8: {
  1606. ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1607. ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1608. ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  1609. ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  1610. } break;
  1611. case FORMAT_RGBA4444: {
  1612. uint16_t rgba = 0;
  1613. rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15));
  1614. rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 4;
  1615. rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 8;
  1616. rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15)) << 12;
  1617. ((uint16_t *)ptr)[ofs] = rgba;
  1618. } break;
  1619. case FORMAT_RGBA5551: {
  1620. uint16_t rgba = 0;
  1621. rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
  1622. rgba |= uint16_t(CLAMP(p_color.g * 31.0, 0, 31)) << 5;
  1623. rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 10;
  1624. rgba |= uint16_t(p_color.a > 0.5 ? 1 : 0) << 15;
  1625. ((uint16_t *)ptr)[ofs] = rgba;
  1626. } break;
  1627. case FORMAT_RF: {
  1628. ((float *)ptr)[ofs] = p_color.r;
  1629. } break;
  1630. case FORMAT_RGF: {
  1631. ((float *)ptr)[ofs * 2 + 0] = p_color.r;
  1632. ((float *)ptr)[ofs * 2 + 1] = p_color.g;
  1633. } break;
  1634. case FORMAT_RGBF: {
  1635. ((float *)ptr)[ofs * 3 + 0] = p_color.r;
  1636. ((float *)ptr)[ofs * 3 + 1] = p_color.g;
  1637. ((float *)ptr)[ofs * 3 + 2] = p_color.b;
  1638. } break;
  1639. case FORMAT_RGBAF: {
  1640. ((float *)ptr)[ofs * 4 + 0] = p_color.r;
  1641. ((float *)ptr)[ofs * 4 + 1] = p_color.g;
  1642. ((float *)ptr)[ofs * 4 + 2] = p_color.b;
  1643. ((float *)ptr)[ofs * 4 + 3] = p_color.a;
  1644. } break;
  1645. case FORMAT_RH: {
  1646. ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r);
  1647. } break;
  1648. case FORMAT_RGH: {
  1649. ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r);
  1650. ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g);
  1651. } break;
  1652. case FORMAT_RGBH: {
  1653. ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r);
  1654. ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g);
  1655. ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b);
  1656. } break;
  1657. case FORMAT_RGBAH: {
  1658. ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r);
  1659. ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g);
  1660. ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b);
  1661. ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a);
  1662. } break;
  1663. case FORMAT_RGBE9995: {
  1664. ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995();
  1665. } break;
  1666. default: {
  1667. ERR_EXPLAIN("Can't set_pixel() on compressed image, sorry.");
  1668. ERR_FAIL();
  1669. }
  1670. }
  1671. }
  1672. Image::DetectChannels Image::get_detected_channels() {
  1673. ERR_FAIL_COND_V(data.size() == 0, DETECTED_RGBA);
  1674. ERR_FAIL_COND_V(is_compressed(), DETECTED_RGBA);
  1675. bool r = false, g = false, b = false, a = false, c = false;
  1676. lock();
  1677. for (int i = 0; i < width; i++) {
  1678. for (int j = 0; j < height; j++) {
  1679. Color col = get_pixel(i, j);
  1680. if (col.r > 0.001)
  1681. r = true;
  1682. if (col.g > 0.001)
  1683. g = true;
  1684. if (col.b > 0.001)
  1685. b = true;
  1686. if (col.a < 0.999)
  1687. a = true;
  1688. if (col.r != col.b || col.r != col.g || col.b != col.g) {
  1689. c = true;
  1690. }
  1691. }
  1692. }
  1693. unlock();
  1694. if (!c && !a)
  1695. return DETECTED_L;
  1696. if (!c && a)
  1697. return DETECTED_LA;
  1698. if (r && !g && !b && !a)
  1699. return DETECTED_R;
  1700. if (r && g && !b && !a)
  1701. return DETECTED_RG;
  1702. if (r && g && b && !a)
  1703. return DETECTED_RGB;
  1704. return DETECTED_RGBA;
  1705. }
  1706. void Image::_bind_methods() {
  1707. ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
  1708. ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height);
  1709. ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size);
  1710. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps);
  1711. ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format);
  1712. ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data);
  1713. ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert);
  1714. ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset);
  1715. ClassDB::bind_method(D_METHOD("resize_to_po2", "square"), &Image::resize_to_po2, DEFVAL(false));
  1716. ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR));
  1717. ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2);
  1718. ClassDB::bind_method(D_METHOD("expand_x2_hq2x"), &Image::expand_x2_hq2x);
  1719. ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop);
  1720. ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x);
  1721. ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y);
  1722. ClassDB::bind_method(D_METHOD("generate_mipmaps"), &Image::generate_mipmaps);
  1723. ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps);
  1724. ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty);
  1725. ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data);
  1726. ClassDB::bind_method(D_METHOD("is_empty"), &Image::empty);
  1727. ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
  1728. ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
  1729. ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
  1730. ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);
  1731. ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress);
  1732. ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress);
  1733. ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed);
  1734. ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges);
  1735. ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha);
  1736. ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear);
  1737. ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy);
  1738. ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect);
  1739. ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask);
  1740. ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect);
  1741. ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask);
  1742. ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill);
  1743. ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
  1744. ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect);
  1745. ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from);
  1746. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data);
  1747. ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data);
  1748. ClassDB::bind_method(D_METHOD("lock"), &Image::lock);
  1749. ClassDB::bind_method(D_METHOD("unlock"), &Image::unlock);
  1750. ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel);
  1751. ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel);
  1752. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
  1753. BIND_ENUM_CONSTANT(FORMAT_L8); //luminance
  1754. BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha
  1755. BIND_ENUM_CONSTANT(FORMAT_R8);
  1756. BIND_ENUM_CONSTANT(FORMAT_RG8);
  1757. BIND_ENUM_CONSTANT(FORMAT_RGB8);
  1758. BIND_ENUM_CONSTANT(FORMAT_RGBA8);
  1759. BIND_ENUM_CONSTANT(FORMAT_RGBA4444);
  1760. BIND_ENUM_CONSTANT(FORMAT_RGBA5551);
  1761. BIND_ENUM_CONSTANT(FORMAT_RF); //float
  1762. BIND_ENUM_CONSTANT(FORMAT_RGF);
  1763. BIND_ENUM_CONSTANT(FORMAT_RGBF);
  1764. BIND_ENUM_CONSTANT(FORMAT_RGBAF);
  1765. BIND_ENUM_CONSTANT(FORMAT_RH); //half float
  1766. BIND_ENUM_CONSTANT(FORMAT_RGH);
  1767. BIND_ENUM_CONSTANT(FORMAT_RGBH);
  1768. BIND_ENUM_CONSTANT(FORMAT_RGBAH);
  1769. BIND_ENUM_CONSTANT(FORMAT_RGBE9995);
  1770. BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1
  1771. BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2
  1772. BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3
  1773. BIND_ENUM_CONSTANT(FORMAT_RGTC_R);
  1774. BIND_ENUM_CONSTANT(FORMAT_RGTC_RG);
  1775. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h
  1776. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float /
  1777. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float
  1778. BIND_ENUM_CONSTANT(FORMAT_PVRTC2); //pvrtc
  1779. BIND_ENUM_CONSTANT(FORMAT_PVRTC2A);
  1780. BIND_ENUM_CONSTANT(FORMAT_PVRTC4);
  1781. BIND_ENUM_CONSTANT(FORMAT_PVRTC4A);
  1782. BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1
  1783. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2
  1784. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb.
  1785. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11);
  1786. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S);
  1787. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8);
  1788. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8);
  1789. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1);
  1790. BIND_ENUM_CONSTANT(FORMAT_MAX);
  1791. BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST);
  1792. BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR);
  1793. BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC);
  1794. BIND_ENUM_CONSTANT(ALPHA_NONE);
  1795. BIND_ENUM_CONSTANT(ALPHA_BIT);
  1796. BIND_ENUM_CONSTANT(ALPHA_BLEND);
  1797. BIND_ENUM_CONSTANT(COMPRESS_S3TC);
  1798. BIND_ENUM_CONSTANT(COMPRESS_PVRTC2);
  1799. BIND_ENUM_CONSTANT(COMPRESS_PVRTC4);
  1800. BIND_ENUM_CONSTANT(COMPRESS_ETC);
  1801. BIND_ENUM_CONSTANT(COMPRESS_ETC2);
  1802. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC);
  1803. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB);
  1804. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
  1805. }
  1806. void Image::set_compress_bc_func(void (*p_compress_func)(Image *, CompressSource)) {
  1807. _image_compress_bc_func = p_compress_func;
  1808. }
  1809. void Image::normalmap_to_xy() {
  1810. convert(Image::FORMAT_RGBA8);
  1811. {
  1812. int len = data.size() / 4;
  1813. PoolVector<uint8_t>::Write wp = data.write();
  1814. unsigned char *data_ptr = wp.ptr();
  1815. for (int i = 0; i < len; i++) {
  1816. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  1817. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  1818. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  1819. }
  1820. }
  1821. convert(Image::FORMAT_LA8);
  1822. }
  1823. void Image::srgb_to_linear() {
  1824. if (data.size() == 0)
  1825. return;
  1826. 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 };
  1827. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  1828. if (format == FORMAT_RGBA8) {
  1829. int len = data.size() / 4;
  1830. PoolVector<uint8_t>::Write wp = data.write();
  1831. unsigned char *data_ptr = wp.ptr();
  1832. for (int i = 0; i < len; i++) {
  1833. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  1834. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  1835. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  1836. }
  1837. } else if (format == FORMAT_RGB8) {
  1838. int len = data.size() / 3;
  1839. PoolVector<uint8_t>::Write wp = data.write();
  1840. unsigned char *data_ptr = wp.ptr();
  1841. for (int i = 0; i < len; i++) {
  1842. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  1843. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  1844. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  1845. }
  1846. }
  1847. }
  1848. void Image::premultiply_alpha() {
  1849. if (data.size() == 0)
  1850. return;
  1851. if (format != FORMAT_RGBA8)
  1852. return; //not needed
  1853. PoolVector<uint8_t>::Write wp = data.write();
  1854. unsigned char *data_ptr = wp.ptr();
  1855. for (int i = 0; i < height; i++) {
  1856. for (int j = 0; j < width; j++) {
  1857. uint8_t *ptr = &data_ptr[(i * width + j) * 4];
  1858. ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8;
  1859. ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8;
  1860. ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8;
  1861. }
  1862. }
  1863. }
  1864. void Image::fix_alpha_edges() {
  1865. if (data.size() == 0)
  1866. return;
  1867. if (format != FORMAT_RGBA8)
  1868. return; //not needed
  1869. PoolVector<uint8_t> dcopy = data;
  1870. PoolVector<uint8_t>::Read rp = dcopy.read();
  1871. const uint8_t *srcptr = rp.ptr();
  1872. PoolVector<uint8_t>::Write wp = data.write();
  1873. unsigned char *data_ptr = wp.ptr();
  1874. const int max_radius = 4;
  1875. const int alpha_threshold = 20;
  1876. const int max_dist = 0x7FFFFFFF;
  1877. for (int i = 0; i < height; i++) {
  1878. for (int j = 0; j < width; j++) {
  1879. const uint8_t *rptr = &srcptr[(i * width + j) * 4];
  1880. uint8_t *wptr = &data_ptr[(i * width + j) * 4];
  1881. if (rptr[3] >= alpha_threshold)
  1882. continue;
  1883. int closest_dist = max_dist;
  1884. uint8_t closest_color[3];
  1885. int from_x = MAX(0, j - max_radius);
  1886. int to_x = MIN(width - 1, j + max_radius);
  1887. int from_y = MAX(0, i - max_radius);
  1888. int to_y = MIN(height - 1, i + max_radius);
  1889. for (int k = from_y; k <= to_y; k++) {
  1890. for (int l = from_x; l <= to_x; l++) {
  1891. int dy = i - k;
  1892. int dx = j - l;
  1893. int dist = dy * dy + dx * dx;
  1894. if (dist >= closest_dist)
  1895. continue;
  1896. const uint8_t *rp = &srcptr[(k * width + l) << 2];
  1897. if (rp[3] < alpha_threshold)
  1898. continue;
  1899. closest_dist = dist;
  1900. closest_color[0] = rp[0];
  1901. closest_color[1] = rp[1];
  1902. closest_color[2] = rp[2];
  1903. }
  1904. }
  1905. if (closest_dist != max_dist) {
  1906. wptr[0] = closest_color[0];
  1907. wptr[1] = closest_color[1];
  1908. wptr[2] = closest_color[2];
  1909. }
  1910. }
  1911. }
  1912. }
  1913. String Image::get_format_name(Format p_format) {
  1914. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  1915. return format_names[p_format];
  1916. }
  1917. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  1918. width = 0;
  1919. height = 0;
  1920. mipmaps = false;
  1921. format = FORMAT_L8;
  1922. if (_png_mem_loader_func) {
  1923. copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len));
  1924. }
  1925. if (empty() && _jpg_mem_loader_func) {
  1926. copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len));
  1927. }
  1928. }
  1929. Ref<Resource> Image::duplicate(bool p_subresources) const {
  1930. Ref<Image> copy;
  1931. copy.instance();
  1932. copy->_copy_internals_from(*this);
  1933. return copy;
  1934. }
  1935. Image::Image() {
  1936. width = 0;
  1937. height = 0;
  1938. mipmaps = false;
  1939. format = FORMAT_L8;
  1940. }
  1941. Image::~Image() {
  1942. if (write_lock.ptr()) {
  1943. unlock();
  1944. }
  1945. }