image.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718
  1. /*************************************************************************/
  2. /* image.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. uint32_t max_bytes = MAX(read_bytes, write_bytes);
  309. for (int y = 0; y < p_height; y++) {
  310. for (int x = 0; x < p_width; x++) {
  311. const uint8_t *rofs = &p_src[((y * p_width) + x) * (read_bytes + (read_alpha ? 1 : 0))];
  312. uint8_t *wofs = &p_dst[((y * p_width) + x) * (write_bytes + (write_alpha ? 1 : 0))];
  313. uint8_t rgba[4];
  314. if (read_gray) {
  315. rgba[0] = rofs[0];
  316. rgba[1] = rofs[0];
  317. rgba[2] = rofs[0];
  318. } else {
  319. for (uint32_t i = 0; i < max_bytes; i++) {
  320. rgba[i] = (i < read_bytes) ? rofs[i] : 0;
  321. }
  322. }
  323. if (read_alpha || write_alpha) {
  324. rgba[3] = read_alpha ? rofs[read_bytes] : 255;
  325. }
  326. if (write_gray) {
  327. //TODO: not correct grayscale, should use fixed point version of actual weights
  328. wofs[0] = uint8_t((uint16_t(rofs[0]) + uint16_t(rofs[1]) + uint16_t(rofs[2])) / 3);
  329. } else {
  330. for (uint32_t i = 0; i < write_bytes; i++) {
  331. wofs[i] = rgba[i];
  332. }
  333. }
  334. if (write_alpha) {
  335. wofs[write_bytes] = rgba[3];
  336. }
  337. }
  338. }
  339. }
  340. void Image::convert(Format p_new_format) {
  341. if (data.size() == 0)
  342. return;
  343. if (p_new_format == format)
  344. return;
  345. if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) {
  346. ERR_EXPLAIN("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead.");
  347. ERR_FAIL();
  348. } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) {
  349. //use put/set pixel which is slower but works with non byte formats
  350. Image new_img(width, height, 0, p_new_format);
  351. lock();
  352. new_img.lock();
  353. for (int i = 0; i < width; i++) {
  354. for (int j = 0; j < height; j++) {
  355. new_img.set_pixel(i, j, get_pixel(i, j));
  356. }
  357. }
  358. unlock();
  359. new_img.unlock();
  360. if (has_mipmaps()) {
  361. new_img.generate_mipmaps();
  362. }
  363. _copy_internals_from(new_img);
  364. return;
  365. }
  366. Image new_img(width, height, 0, p_new_format);
  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 (data.size() == 0) {
  555. ERR_EXPLAIN("Cannot resize image before creating it, use create() or create_from_data() first.");
  556. ERR_FAIL();
  557. }
  558. if (!_can_modify(format)) {
  559. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  560. ERR_FAIL();
  561. }
  562. ERR_FAIL_COND(p_width <= 0);
  563. ERR_FAIL_COND(p_height <= 0);
  564. ERR_FAIL_COND(p_width > MAX_WIDTH);
  565. ERR_FAIL_COND(p_height > MAX_HEIGHT);
  566. if (p_width == width && p_height == height)
  567. return;
  568. Image dst(p_width, p_height, 0, format);
  569. PoolVector<uint8_t>::Read r = data.read();
  570. const unsigned char *r_ptr = r.ptr();
  571. PoolVector<uint8_t>::Write w = dst.data.write();
  572. unsigned char *w_ptr = w.ptr();
  573. switch (p_interpolation) {
  574. case INTERPOLATE_NEAREST: {
  575. switch (get_format_pixel_size(format)) {
  576. case 1: _scale_nearest<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  577. case 2: _scale_nearest<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  578. case 3: _scale_nearest<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  579. case 4: _scale_nearest<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  580. }
  581. } break;
  582. case INTERPOLATE_BILINEAR: {
  583. switch (get_format_pixel_size(format)) {
  584. case 1: _scale_bilinear<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  585. case 2: _scale_bilinear<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  586. case 3: _scale_bilinear<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  587. case 4: _scale_bilinear<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  588. }
  589. } break;
  590. case INTERPOLATE_CUBIC: {
  591. switch (get_format_pixel_size(format)) {
  592. case 1: _scale_cubic<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  593. case 2: _scale_cubic<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  594. case 3: _scale_cubic<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  595. case 4: _scale_cubic<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
  596. }
  597. } break;
  598. }
  599. r = PoolVector<uint8_t>::Read();
  600. w = PoolVector<uint8_t>::Write();
  601. if (mipmaps > 0)
  602. dst.generate_mipmaps();
  603. _copy_internals_from(dst);
  604. }
  605. void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) {
  606. if (!_can_modify(format)) {
  607. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  608. ERR_FAIL();
  609. }
  610. ERR_FAIL_COND(p_x < 0);
  611. ERR_FAIL_COND(p_y < 0);
  612. ERR_FAIL_COND(p_width <= 0);
  613. ERR_FAIL_COND(p_height <= 0);
  614. ERR_FAIL_COND(p_x + p_width > MAX_WIDTH);
  615. ERR_FAIL_COND(p_y + p_height > MAX_HEIGHT);
  616. /* to save memory, cropping should be done in-place, however, since this function
  617. will most likely either not be used much, or in critical areas, for now it won't, because
  618. it's a waste of time. */
  619. if (p_width == width && p_height == height && p_x == 0 && p_y == 0)
  620. return;
  621. uint8_t pdata[16]; //largest is 16
  622. uint32_t pixel_size = get_format_pixel_size(format);
  623. Image dst(p_width, p_height, 0, format);
  624. {
  625. PoolVector<uint8_t>::Read r = data.read();
  626. PoolVector<uint8_t>::Write w = dst.data.write();
  627. int m_h = p_y + p_height;
  628. int m_w = p_x + p_width;
  629. for (int y = p_y; y < m_h; y++) {
  630. for (int x = p_x; x < m_w; x++) {
  631. if ((x >= width || y >= height)) {
  632. for (uint32_t i = 0; i < pixel_size; i++)
  633. pdata[i] = 0;
  634. } else {
  635. _get_pixelb(x, y, pixel_size, r.ptr(), pdata);
  636. }
  637. dst._put_pixelb(x - p_x, y - p_y, pixel_size, w.ptr(), pdata);
  638. }
  639. }
  640. }
  641. if (mipmaps > 0)
  642. dst.generate_mipmaps();
  643. _copy_internals_from(dst);
  644. }
  645. void Image::crop(int p_width, int p_height) {
  646. crop_from_point(0, 0, p_width, p_height);
  647. }
  648. void Image::flip_y() {
  649. if (!_can_modify(format)) {
  650. ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats.");
  651. ERR_FAIL();
  652. }
  653. bool gm = mipmaps;
  654. if (gm)
  655. clear_mipmaps();
  656. {
  657. PoolVector<uint8_t>::Write w = data.write();
  658. uint8_t up[16];
  659. uint8_t down[16];
  660. uint32_t pixel_size = get_format_pixel_size(format);
  661. for (int y = 0; y < height / 2; y++) {
  662. for (int x = 0; x < width; x++) {
  663. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  664. _get_pixelb(x, height - y - 1, pixel_size, w.ptr(), down);
  665. _put_pixelb(x, height - y - 1, pixel_size, w.ptr(), up);
  666. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  667. }
  668. }
  669. }
  670. if (gm)
  671. generate_mipmaps();
  672. }
  673. void Image::flip_x() {
  674. if (!_can_modify(format)) {
  675. ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats.");
  676. ERR_FAIL();
  677. }
  678. bool gm = mipmaps;
  679. if (gm)
  680. clear_mipmaps();
  681. {
  682. PoolVector<uint8_t>::Write w = data.write();
  683. uint8_t up[16];
  684. uint8_t down[16];
  685. uint32_t pixel_size = get_format_pixel_size(format);
  686. for (int y = 0; y < height; y++) {
  687. for (int x = 0; x < width / 2; x++) {
  688. _get_pixelb(x, y, pixel_size, w.ptr(), up);
  689. _get_pixelb(width - x - 1, y, pixel_size, w.ptr(), down);
  690. _put_pixelb(width - x - 1, y, pixel_size, w.ptr(), up);
  691. _put_pixelb(x, y, pixel_size, w.ptr(), down);
  692. }
  693. }
  694. }
  695. if (gm)
  696. generate_mipmaps();
  697. }
  698. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps) {
  699. int size = 0;
  700. int w = p_width;
  701. int h = p_height;
  702. int mm = 0;
  703. int pixsize = get_format_pixel_size(p_format);
  704. int pixshift = get_format_pixel_rshift(p_format);
  705. int block = get_format_block_size(p_format);
  706. int minw, minh;
  707. get_format_min_pixel_size(p_format, minw, minh);
  708. while (true) {
  709. int bw = w % block != 0 ? w + (block - w % block) : w;
  710. int bh = h % block != 0 ? h + (block - h % block) : h;
  711. int s = bw * bh;
  712. s *= pixsize;
  713. s >>= pixshift;
  714. size += s;
  715. if (p_mipmaps >= 0 && mm == p_mipmaps)
  716. break;
  717. if (p_mipmaps >= 0) {
  718. w = MAX(minw, w >> 1);
  719. h = MAX(minh, h >> 1);
  720. } else {
  721. if (w == minw && h == minh)
  722. break;
  723. w = MAX(minw, w >> 1);
  724. h = MAX(minh, h >> 1);
  725. }
  726. mm++;
  727. };
  728. r_mipmaps = mm;
  729. return size;
  730. }
  731. bool Image::_can_modify(Format p_format) const {
  732. return p_format <= FORMAT_RGBE9995;
  733. }
  734. template <int CC, bool renormalize>
  735. static void _generate_po2_mipmap(const uint8_t *p_src, uint8_t *p_dst, uint32_t p_width, uint32_t p_height) {
  736. //fast power of 2 mipmap generation
  737. uint32_t dst_w = p_width >> 1;
  738. uint32_t dst_h = p_height >> 1;
  739. for (uint32_t i = 0; i < dst_h; i++) {
  740. const uint8_t *rup_ptr = &p_src[i * 2 * p_width * CC];
  741. const uint8_t *rdown_ptr = rup_ptr + p_width * CC;
  742. uint8_t *dst_ptr = &p_dst[i * dst_w * CC];
  743. uint32_t count = dst_w;
  744. while (count--) {
  745. for (int j = 0; j < CC; j++) {
  746. uint16_t val = 0;
  747. val += rup_ptr[j];
  748. val += rup_ptr[j + CC];
  749. val += rdown_ptr[j];
  750. val += rdown_ptr[j + CC];
  751. dst_ptr[j] = val >> 2;
  752. }
  753. if (renormalize) {
  754. Vector3 n(dst_ptr[0] / 255.0, dst_ptr[1] / 255.0, dst_ptr[2] / 255.0);
  755. n *= 2.0;
  756. n -= Vector3(1, 1, 1);
  757. n.normalize();
  758. n += Vector3(1, 1, 1);
  759. n *= 0.5;
  760. n *= 255;
  761. dst_ptr[0] = CLAMP(int(n.x), 0, 255);
  762. dst_ptr[1] = CLAMP(int(n.y), 0, 255);
  763. dst_ptr[2] = CLAMP(int(n.z), 0, 255);
  764. }
  765. dst_ptr += CC;
  766. rup_ptr += CC * 2;
  767. rdown_ptr += CC * 2;
  768. }
  769. }
  770. }
  771. void Image::expand_x2_hq2x() {
  772. ERR_FAIL_COND(!_can_modify(format));
  773. Format current = format;
  774. bool mm = has_mipmaps();
  775. if (mm) {
  776. clear_mipmaps();
  777. }
  778. if (current != FORMAT_RGBA8)
  779. convert(FORMAT_RGBA8);
  780. PoolVector<uint8_t> dest;
  781. dest.resize(width * 2 * height * 2 * 4);
  782. {
  783. PoolVector<uint8_t>::Read r = data.read();
  784. PoolVector<uint8_t>::Write w = dest.write();
  785. hq2x_resize((const uint32_t *)r.ptr(), width, height, (uint32_t *)w.ptr());
  786. }
  787. width *= 2;
  788. height *= 2;
  789. data = dest;
  790. if (current != FORMAT_RGBA8)
  791. convert(current);
  792. if (mipmaps) {
  793. generate_mipmaps();
  794. }
  795. }
  796. void Image::shrink_x2() {
  797. ERR_FAIL_COND(data.size() == 0);
  798. if (mipmaps) {
  799. //just use the lower mipmap as base and copy all
  800. PoolVector<uint8_t> new_img;
  801. int ofs = get_mipmap_offset(1);
  802. int new_size = data.size() - ofs;
  803. new_img.resize(new_size);
  804. {
  805. PoolVector<uint8_t>::Write w = new_img.write();
  806. PoolVector<uint8_t>::Read r = data.read();
  807. copymem(w.ptr(), &r[ofs], new_size);
  808. }
  809. width = MAX(width / 2, 1);
  810. height = MAX(height / 2, 1);
  811. data = new_img;
  812. } else {
  813. PoolVector<uint8_t> new_img;
  814. ERR_FAIL_COND(!_can_modify(format));
  815. int ps = get_format_pixel_size(format);
  816. new_img.resize((width / 2) * (height / 2) * ps);
  817. {
  818. PoolVector<uint8_t>::Write w = new_img.write();
  819. PoolVector<uint8_t>::Read r = data.read();
  820. switch (format) {
  821. case FORMAT_L8:
  822. case FORMAT_R8: _generate_po2_mipmap<1, false>(r.ptr(), w.ptr(), width, height); break;
  823. case FORMAT_LA8: _generate_po2_mipmap<2, false>(r.ptr(), w.ptr(), width, height); break;
  824. case FORMAT_RG8: _generate_po2_mipmap<2, false>(r.ptr(), w.ptr(), width, height); break;
  825. case FORMAT_RGB8: _generate_po2_mipmap<3, false>(r.ptr(), w.ptr(), width, height); break;
  826. case FORMAT_RGBA8: _generate_po2_mipmap<4, false>(r.ptr(), w.ptr(), width, height); break;
  827. default: {}
  828. }
  829. }
  830. width /= 2;
  831. height /= 2;
  832. data = new_img;
  833. }
  834. }
  835. void Image::normalize() {
  836. bool used_mipmaps = has_mipmaps();
  837. if (used_mipmaps) {
  838. clear_mipmaps();
  839. }
  840. lock();
  841. for (int y = 0; y < height; y++) {
  842. for (int x = 0; x < width; x++) {
  843. Color c = get_pixel(x, y);
  844. Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0);
  845. v.normalize();
  846. c.r = v.x * 0.5 + 0.5;
  847. c.g = v.y * 0.5 + 0.5;
  848. c.b = v.z * 0.5 + 0.5;
  849. set_pixel(x, y, c);
  850. }
  851. }
  852. unlock();
  853. if (used_mipmaps) {
  854. generate_mipmaps(true);
  855. }
  856. }
  857. Error Image::generate_mipmaps(bool p_renormalize) {
  858. if (!_can_modify(format)) {
  859. ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats.");
  860. ERR_FAIL_V(ERR_UNAVAILABLE);
  861. }
  862. ERR_FAIL_COND_V(width == 0 || height == 0, ERR_UNCONFIGURED);
  863. int mmcount;
  864. int size = _get_dst_image_size(width, height, format, mmcount);
  865. data.resize(size);
  866. PoolVector<uint8_t>::Write wp = data.write();
  867. int prev_ofs = 0;
  868. int prev_h = height;
  869. int prev_w = width;
  870. for (int i = 1; i < mmcount; i++) {
  871. int ofs, w, h;
  872. _get_mipmap_offset_and_size(i, ofs, w, h);
  873. switch (format) {
  874. case FORMAT_L8:
  875. case FORMAT_R8: _generate_po2_mipmap<1, false>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  876. case FORMAT_LA8:
  877. case FORMAT_RG8: _generate_po2_mipmap<2, false>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); break;
  878. case FORMAT_RGB8:
  879. if (p_renormalize)
  880. _generate_po2_mipmap<3, true>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  881. else
  882. _generate_po2_mipmap<3, false>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  883. break;
  884. case FORMAT_RGBA8:
  885. if (p_renormalize)
  886. _generate_po2_mipmap<4, true>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  887. else
  888. _generate_po2_mipmap<4, false>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h);
  889. break;
  890. default: {}
  891. }
  892. prev_ofs = ofs;
  893. prev_w = w;
  894. prev_h = h;
  895. }
  896. mipmaps = true;
  897. return OK;
  898. }
  899. void Image::clear_mipmaps() {
  900. if (!mipmaps)
  901. return;
  902. if (empty())
  903. return;
  904. int ofs, w, h;
  905. _get_mipmap_offset_and_size(1, ofs, w, h);
  906. data.resize(ofs);
  907. mipmaps = false;
  908. }
  909. bool Image::empty() const {
  910. return (data.size() == 0);
  911. }
  912. PoolVector<uint8_t> Image::get_data() const {
  913. return data;
  914. }
  915. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  916. ERR_FAIL_INDEX(p_width - 1, MAX_WIDTH);
  917. ERR_FAIL_INDEX(p_height - 1, MAX_HEIGHT);
  918. int mm = 0;
  919. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  920. data.resize(size);
  921. {
  922. PoolVector<uint8_t>::Write w = data.write();
  923. zeromem(w.ptr(), size);
  924. }
  925. width = p_width;
  926. height = p_height;
  927. mipmaps = p_use_mipmaps;
  928. format = p_format;
  929. }
  930. void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  931. ERR_FAIL_INDEX(p_width - 1, MAX_WIDTH);
  932. ERR_FAIL_INDEX(p_height - 1, MAX_HEIGHT);
  933. int mm;
  934. int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
  935. if (size != p_data.size()) {
  936. ERR_EXPLAIN("Expected data size of " + itos(size) + " in Image::create()");
  937. ERR_FAIL_COND(p_data.size() != size);
  938. }
  939. height = p_height;
  940. width = p_width;
  941. format = p_format;
  942. data = p_data;
  943. mipmaps = p_use_mipmaps;
  944. }
  945. void Image::create(const char **p_xpm) {
  946. int size_width, size_height;
  947. int pixelchars = 0;
  948. mipmaps = false;
  949. bool has_alpha = false;
  950. enum Status {
  951. READING_HEADER,
  952. READING_COLORS,
  953. READING_PIXELS,
  954. DONE
  955. };
  956. Status status = READING_HEADER;
  957. int line = 0;
  958. HashMap<String, Color> colormap;
  959. int colormap_size;
  960. uint32_t pixel_size;
  961. PoolVector<uint8_t>::Write w;
  962. while (status != DONE) {
  963. const char *line_ptr = p_xpm[line];
  964. switch (status) {
  965. case READING_HEADER: {
  966. String line_str = line_ptr;
  967. line_str.replace("\t", " ");
  968. size_width = line_str.get_slicec(' ', 0).to_int();
  969. size_height = line_str.get_slicec(' ', 1).to_int();
  970. colormap_size = line_str.get_slicec(' ', 2).to_int();
  971. pixelchars = line_str.get_slicec(' ', 3).to_int();
  972. ERR_FAIL_COND(colormap_size > 32766);
  973. ERR_FAIL_COND(pixelchars > 5);
  974. ERR_FAIL_COND(size_width > 32767);
  975. ERR_FAIL_COND(size_height > 32767);
  976. status = READING_COLORS;
  977. } break;
  978. case READING_COLORS: {
  979. String colorstring;
  980. for (int i = 0; i < pixelchars; i++) {
  981. colorstring += *line_ptr;
  982. line_ptr++;
  983. }
  984. //skip spaces
  985. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  986. if (*line_ptr == 0)
  987. break;
  988. line_ptr++;
  989. }
  990. if (*line_ptr == 'c') {
  991. line_ptr++;
  992. while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) {
  993. if (*line_ptr == 0)
  994. break;
  995. line_ptr++;
  996. }
  997. if (*line_ptr == '#') {
  998. line_ptr++;
  999. uint8_t col_r = 0;
  1000. uint8_t col_g = 0;
  1001. uint8_t col_b = 0;
  1002. //uint8_t col_a=255;
  1003. for (int i = 0; i < 6; i++) {
  1004. char v = line_ptr[i];
  1005. if (v >= '0' && v <= '9')
  1006. v -= '0';
  1007. else if (v >= 'A' && v <= 'F')
  1008. v = (v - 'A') + 10;
  1009. else if (v >= 'a' && v <= 'f')
  1010. v = (v - 'a') + 10;
  1011. else
  1012. break;
  1013. switch (i) {
  1014. case 0: col_r = v << 4; break;
  1015. case 1: col_r |= v; break;
  1016. case 2: col_g = v << 4; break;
  1017. case 3: col_g |= v; break;
  1018. case 4: col_b = v << 4; break;
  1019. case 5: col_b |= v; break;
  1020. };
  1021. }
  1022. // magenta mask
  1023. if (col_r == 255 && col_g == 0 && col_b == 255) {
  1024. colormap[colorstring] = Color(0, 0, 0, 0);
  1025. has_alpha = true;
  1026. } else {
  1027. colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0);
  1028. }
  1029. }
  1030. }
  1031. if (line == colormap_size) {
  1032. status = READING_PIXELS;
  1033. create(size_width, size_height, 0, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8);
  1034. w = data.write();
  1035. pixel_size = has_alpha ? 4 : 3;
  1036. }
  1037. } break;
  1038. case READING_PIXELS: {
  1039. int y = line - colormap_size - 1;
  1040. for (int x = 0; x < size_width; x++) {
  1041. char pixelstr[6] = { 0, 0, 0, 0, 0, 0 };
  1042. for (int i = 0; i < pixelchars; i++)
  1043. pixelstr[i] = line_ptr[x * pixelchars + i];
  1044. Color *colorptr = colormap.getptr(pixelstr);
  1045. ERR_FAIL_COND(!colorptr);
  1046. uint8_t pixel[4];
  1047. for (uint32_t i = 0; i < pixel_size; i++) {
  1048. pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255);
  1049. }
  1050. _put_pixelb(x, y, pixel_size, w.ptr(), pixel);
  1051. }
  1052. if (y == (size_height - 1))
  1053. status = DONE;
  1054. } break;
  1055. default: {}
  1056. }
  1057. line++;
  1058. }
  1059. }
  1060. #define DETECT_ALPHA_MAX_THRESHOLD 254
  1061. #define DETECT_ALPHA_MIN_THRESHOLD 2
  1062. #define DETECT_ALPHA(m_value) \
  1063. { \
  1064. uint8_t value = m_value; \
  1065. if (value < DETECT_ALPHA_MIN_THRESHOLD) \
  1066. bit = true; \
  1067. else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \
  1068. \
  1069. detected = true; \
  1070. break; \
  1071. } \
  1072. }
  1073. #define DETECT_NON_ALPHA(m_value) \
  1074. { \
  1075. uint8_t value = m_value; \
  1076. if (value > 0) { \
  1077. \
  1078. detected = true; \
  1079. break; \
  1080. } \
  1081. }
  1082. bool Image::is_invisible() const {
  1083. if (format == FORMAT_L8 ||
  1084. format == FORMAT_RGB8 || format == FORMAT_RG8)
  1085. return false;
  1086. int len = data.size();
  1087. if (len == 0)
  1088. return true;
  1089. int w, h;
  1090. _get_mipmap_offset_and_size(1, len, w, h);
  1091. PoolVector<uint8_t>::Read r = data.read();
  1092. const unsigned char *data_ptr = r.ptr();
  1093. bool detected = false;
  1094. switch (format) {
  1095. case FORMAT_LA8: {
  1096. for (int i = 0; i < (len >> 1); i++) {
  1097. DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]);
  1098. }
  1099. } break;
  1100. case FORMAT_RGBA8: {
  1101. for (int i = 0; i < (len >> 2); i++) {
  1102. DETECT_NON_ALPHA(data_ptr[(i << 2) + 3])
  1103. }
  1104. } break;
  1105. case FORMAT_PVRTC2A:
  1106. case FORMAT_PVRTC4A:
  1107. case FORMAT_DXT3:
  1108. case FORMAT_DXT5: {
  1109. detected = true;
  1110. } break;
  1111. default: {}
  1112. }
  1113. return !detected;
  1114. }
  1115. Image::AlphaMode Image::detect_alpha() const {
  1116. int len = data.size();
  1117. if (len == 0)
  1118. return ALPHA_NONE;
  1119. int w, h;
  1120. _get_mipmap_offset_and_size(1, len, w, h);
  1121. PoolVector<uint8_t>::Read r = data.read();
  1122. const unsigned char *data_ptr = r.ptr();
  1123. bool bit = false;
  1124. bool detected = false;
  1125. switch (format) {
  1126. case FORMAT_LA8: {
  1127. for (int i = 0; i < (len >> 1); i++) {
  1128. DETECT_ALPHA(data_ptr[(i << 1) + 1]);
  1129. }
  1130. } break;
  1131. case FORMAT_RGBA8: {
  1132. for (int i = 0; i < (len >> 2); i++) {
  1133. DETECT_ALPHA(data_ptr[(i << 2) + 3])
  1134. }
  1135. } break;
  1136. case FORMAT_PVRTC2A:
  1137. case FORMAT_PVRTC4A:
  1138. case FORMAT_DXT3:
  1139. case FORMAT_DXT5: {
  1140. detected = true;
  1141. } break;
  1142. default: {}
  1143. }
  1144. if (detected)
  1145. return ALPHA_BLEND;
  1146. else if (bit)
  1147. return ALPHA_BIT;
  1148. else
  1149. return ALPHA_NONE;
  1150. }
  1151. Error Image::load(const String &p_path) {
  1152. return ImageLoader::load_image(p_path, this);
  1153. }
  1154. Error Image::save_png(const String &p_path) const {
  1155. if (save_png_func == NULL)
  1156. return ERR_UNAVAILABLE;
  1157. return save_png_func(p_path, Ref<Image>((Image *)this));
  1158. }
  1159. int Image::get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps) {
  1160. int mm;
  1161. return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps);
  1162. }
  1163. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1164. int mm;
  1165. _get_dst_image_size(p_width, p_height, p_format, mm, -1);
  1166. return mm;
  1167. }
  1168. bool Image::is_compressed() const {
  1169. return format > FORMAT_RGBE9995;
  1170. }
  1171. Error Image::decompress() {
  1172. if (format >= FORMAT_DXT1 && format <= FORMAT_BPTC_RGBFU && _image_decompress_bc)
  1173. _image_decompress_bc(this);
  1174. else if (format >= FORMAT_PVRTC2 && format <= FORMAT_PVRTC4A && _image_decompress_pvrtc)
  1175. _image_decompress_pvrtc(this);
  1176. else if (format == FORMAT_ETC && _image_decompress_etc1)
  1177. _image_decompress_etc1(this);
  1178. else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RGB8A1 && _image_decompress_etc1)
  1179. _image_decompress_etc2(this);
  1180. else
  1181. return ERR_UNAVAILABLE;
  1182. return OK;
  1183. }
  1184. Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) {
  1185. switch (p_mode) {
  1186. case COMPRESS_S3TC: {
  1187. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1188. _image_compress_bc_func(this, p_source);
  1189. } break;
  1190. case COMPRESS_PVRTC2: {
  1191. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1192. _image_compress_pvrtc2_func(this);
  1193. } break;
  1194. case COMPRESS_PVRTC4: {
  1195. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1196. _image_compress_pvrtc4_func(this);
  1197. } break;
  1198. case COMPRESS_ETC: {
  1199. ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE);
  1200. _image_compress_etc1_func(this, p_lossy_quality);
  1201. } break;
  1202. case COMPRESS_ETC2: {
  1203. ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE);
  1204. _image_compress_etc2_func(this, p_lossy_quality, p_source);
  1205. } break;
  1206. }
  1207. return OK;
  1208. }
  1209. Image::Image(const char **p_xpm) {
  1210. width = 0;
  1211. height = 0;
  1212. mipmaps = false;
  1213. format = FORMAT_L8;
  1214. create(p_xpm);
  1215. }
  1216. Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  1217. width = 0;
  1218. height = 0;
  1219. mipmaps = p_use_mipmaps;
  1220. format = FORMAT_L8;
  1221. create(p_width, p_height, p_use_mipmaps, p_format);
  1222. }
  1223. Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  1224. width = 0;
  1225. height = 0;
  1226. mipmaps = p_mipmaps;
  1227. format = FORMAT_L8;
  1228. create(p_width, p_height, p_mipmaps, p_format, p_data);
  1229. }
  1230. Rect2 Image::get_used_rect() const {
  1231. if (format != FORMAT_LA8 && format != FORMAT_RGBA8)
  1232. return Rect2(Point2(), Size2(width, height));
  1233. int len = data.size();
  1234. if (len == 0)
  1235. return Rect2();
  1236. //int data_size = len;
  1237. PoolVector<uint8_t>::Read r = data.read();
  1238. const unsigned char *rptr = r.ptr();
  1239. int ps = format == FORMAT_LA8 ? 2 : 4;
  1240. int minx = 0xFFFFFF, miny = 0xFFFFFFF;
  1241. int maxx = -1, maxy = -1;
  1242. for (int j = 0; j < height; j++) {
  1243. for (int i = 0; i < width; i++) {
  1244. bool opaque = rptr[(j * width + i) * ps + (ps - 1)] > 2;
  1245. if (!opaque)
  1246. continue;
  1247. if (i > maxx)
  1248. maxx = i;
  1249. if (j > maxy)
  1250. maxy = j;
  1251. if (i < minx)
  1252. minx = i;
  1253. if (j < miny)
  1254. miny = j;
  1255. }
  1256. }
  1257. if (maxx == -1)
  1258. return Rect2();
  1259. else
  1260. return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1);
  1261. }
  1262. Ref<Image> Image::get_rect(const Rect2 &p_area) const {
  1263. Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format));
  1264. img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0));
  1265. return img;
  1266. }
  1267. void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1268. ERR_FAIL_COND(p_src.is_null());
  1269. int dsize = data.size();
  1270. int srcdsize = p_src->data.size();
  1271. ERR_FAIL_COND(dsize == 0);
  1272. ERR_FAIL_COND(srcdsize == 0);
  1273. ERR_FAIL_COND(format != p_src->format);
  1274. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1275. if (p_dest.x < 0)
  1276. clipped_src_rect.position.x = ABS(p_dest.x);
  1277. if (p_dest.y < 0)
  1278. clipped_src_rect.position.y = ABS(p_dest.y);
  1279. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1280. return;
  1281. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1282. PoolVector<uint8_t>::Write wp = data.write();
  1283. uint8_t *dst_data_ptr = wp.ptr();
  1284. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1285. const uint8_t *src_data_ptr = rp.ptr();
  1286. int pixel_size = get_format_pixel_size(format);
  1287. for (int i = 0; i < dest_rect.size.y; i++) {
  1288. for (int j = 0; j < dest_rect.size.x; j++) {
  1289. int src_x = clipped_src_rect.position.x + j;
  1290. int src_y = clipped_src_rect.position.y + i;
  1291. int dst_x = dest_rect.position.x + j;
  1292. int dst_y = dest_rect.position.y + i;
  1293. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1294. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1295. for (int k = 0; k < pixel_size; k++) {
  1296. dst[k] = src[k];
  1297. }
  1298. }
  1299. }
  1300. }
  1301. void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1302. ERR_FAIL_COND(p_src.is_null());
  1303. ERR_FAIL_COND(p_mask.is_null());
  1304. int dsize = data.size();
  1305. int srcdsize = p_src->data.size();
  1306. int maskdsize = p_mask->data.size();
  1307. ERR_FAIL_COND(dsize == 0);
  1308. ERR_FAIL_COND(srcdsize == 0);
  1309. ERR_FAIL_COND(maskdsize == 0);
  1310. ERR_FAIL_COND(p_src->width != p_mask->width);
  1311. ERR_FAIL_COND(p_src->height != p_mask->height);
  1312. ERR_FAIL_COND(format != p_src->format);
  1313. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1314. if (p_dest.x < 0)
  1315. clipped_src_rect.position.x = ABS(p_dest.x);
  1316. if (p_dest.y < 0)
  1317. clipped_src_rect.position.y = ABS(p_dest.y);
  1318. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1319. return;
  1320. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1321. PoolVector<uint8_t>::Write wp = data.write();
  1322. uint8_t *dst_data_ptr = wp.ptr();
  1323. PoolVector<uint8_t>::Read rp = p_src->data.read();
  1324. const uint8_t *src_data_ptr = rp.ptr();
  1325. int pixel_size = get_format_pixel_size(format);
  1326. Ref<Image> msk = p_mask;
  1327. msk->lock();
  1328. for (int i = 0; i < dest_rect.size.y; i++) {
  1329. for (int j = 0; j < dest_rect.size.x; j++) {
  1330. int src_x = clipped_src_rect.position.x + j;
  1331. int src_y = clipped_src_rect.position.y + i;
  1332. if (msk->get_pixel(src_x, src_y).a != 0) {
  1333. int dst_x = dest_rect.position.x + j;
  1334. int dst_y = dest_rect.position.y + i;
  1335. const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size];
  1336. uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size];
  1337. for (int k = 0; k < pixel_size; k++) {
  1338. dst[k] = src[k];
  1339. }
  1340. }
  1341. }
  1342. }
  1343. msk->unlock();
  1344. }
  1345. void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1346. ERR_FAIL_COND(p_src.is_null());
  1347. int dsize = data.size();
  1348. int srcdsize = p_src->data.size();
  1349. ERR_FAIL_COND(dsize == 0);
  1350. ERR_FAIL_COND(srcdsize == 0);
  1351. ERR_FAIL_COND(format != p_src->format);
  1352. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1353. if (p_dest.x < 0)
  1354. clipped_src_rect.position.x = ABS(p_dest.x);
  1355. if (p_dest.y < 0)
  1356. clipped_src_rect.position.y = ABS(p_dest.y);
  1357. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1358. return;
  1359. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1360. lock();
  1361. Ref<Image> img = p_src;
  1362. img->lock();
  1363. for (int i = 0; i < dest_rect.size.y; i++) {
  1364. for (int j = 0; j < dest_rect.size.x; j++) {
  1365. int src_x = clipped_src_rect.position.x + j;
  1366. int src_y = clipped_src_rect.position.y + i;
  1367. int dst_x = dest_rect.position.x + j;
  1368. int dst_y = dest_rect.position.y + i;
  1369. Color sc = img->get_pixel(src_x, src_y);
  1370. Color dc = get_pixel(dst_x, dst_y);
  1371. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  1372. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  1373. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  1374. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  1375. set_pixel(dst_x, dst_y, dc);
  1376. }
  1377. }
  1378. img->unlock();
  1379. unlock();
  1380. }
  1381. void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) {
  1382. ERR_FAIL_COND(p_src.is_null());
  1383. ERR_FAIL_COND(p_mask.is_null());
  1384. int dsize = data.size();
  1385. int srcdsize = p_src->data.size();
  1386. int maskdsize = p_mask->data.size();
  1387. ERR_FAIL_COND(dsize == 0);
  1388. ERR_FAIL_COND(srcdsize == 0);
  1389. ERR_FAIL_COND(maskdsize == 0);
  1390. ERR_FAIL_COND(p_src->width != p_mask->width);
  1391. ERR_FAIL_COND(p_src->height != p_mask->height);
  1392. ERR_FAIL_COND(format != p_src->format);
  1393. Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect);
  1394. if (p_dest.x < 0)
  1395. clipped_src_rect.position.x = ABS(p_dest.x);
  1396. if (p_dest.y < 0)
  1397. clipped_src_rect.position.y = ABS(p_dest.y);
  1398. if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0)
  1399. return;
  1400. Rect2i dest_rect = Rect2i(0, 0, width, height).clip(Rect2i(p_dest, clipped_src_rect.size));
  1401. lock();
  1402. Ref<Image> img = p_src;
  1403. Ref<Image> msk = p_mask;
  1404. img->lock();
  1405. msk->lock();
  1406. for (int i = 0; i < dest_rect.size.y; i++) {
  1407. for (int j = 0; j < dest_rect.size.x; j++) {
  1408. int src_x = clipped_src_rect.position.x + j;
  1409. int src_y = clipped_src_rect.position.y + i;
  1410. // If the mask's pixel is transparent then we skip it
  1411. //Color c = msk->get_pixel(src_x, src_y);
  1412. //if (c.a == 0) continue;
  1413. if (msk->get_pixel(src_x, src_y).a != 0) {
  1414. int dst_x = dest_rect.position.x + j;
  1415. int dst_y = dest_rect.position.y + i;
  1416. Color sc = img->get_pixel(src_x, src_y);
  1417. Color dc = get_pixel(dst_x, dst_y);
  1418. dc.r = (double)(sc.a * sc.r + dc.a * (1.0 - sc.a) * dc.r);
  1419. dc.g = (double)(sc.a * sc.g + dc.a * (1.0 - sc.a) * dc.g);
  1420. dc.b = (double)(sc.a * sc.b + dc.a * (1.0 - sc.a) * dc.b);
  1421. dc.a = (double)(sc.a + dc.a * (1.0 - sc.a));
  1422. set_pixel(dst_x, dst_y, dc);
  1423. }
  1424. }
  1425. }
  1426. msk->unlock();
  1427. img->unlock();
  1428. unlock();
  1429. }
  1430. void Image::fill(const Color &c) {
  1431. lock();
  1432. PoolVector<uint8_t>::Write wp = data.write();
  1433. uint8_t *dst_data_ptr = wp.ptr();
  1434. int pixel_size = get_format_pixel_size(format);
  1435. // put first pixel with the format-aware API
  1436. set_pixel(0, 0, c);
  1437. for (int y = 0; y < height; y++) {
  1438. for (int x = 0; x < width; x++) {
  1439. uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size];
  1440. for (int k = 0; k < pixel_size; k++) {
  1441. dst[k] = dst_data_ptr[k];
  1442. }
  1443. }
  1444. }
  1445. unlock();
  1446. }
  1447. ImageMemLoadFunc Image::_png_mem_loader_func = NULL;
  1448. ImageMemLoadFunc Image::_jpg_mem_loader_func = NULL;
  1449. ImageMemLoadFunc Image::_webp_mem_loader_func = NULL;
  1450. void (*Image::_image_compress_bc_func)(Image *, Image::CompressSource) = NULL;
  1451. void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL;
  1452. void (*Image::_image_compress_pvrtc4_func)(Image *) = NULL;
  1453. void (*Image::_image_compress_etc1_func)(Image *, float) = NULL;
  1454. void (*Image::_image_compress_etc2_func)(Image *, float, Image::CompressSource) = NULL;
  1455. void (*Image::_image_decompress_pvrtc)(Image *) = NULL;
  1456. void (*Image::_image_decompress_bc)(Image *) = NULL;
  1457. void (*Image::_image_decompress_etc1)(Image *) = NULL;
  1458. void (*Image::_image_decompress_etc2)(Image *) = NULL;
  1459. PoolVector<uint8_t> (*Image::lossy_packer)(const Ref<Image> &, float) = NULL;
  1460. Ref<Image> (*Image::lossy_unpacker)(const PoolVector<uint8_t> &) = NULL;
  1461. PoolVector<uint8_t> (*Image::lossless_packer)(const Ref<Image> &) = NULL;
  1462. Ref<Image> (*Image::lossless_unpacker)(const PoolVector<uint8_t> &) = NULL;
  1463. void Image::_set_data(const Dictionary &p_data) {
  1464. ERR_FAIL_COND(!p_data.has("width"));
  1465. ERR_FAIL_COND(!p_data.has("height"));
  1466. ERR_FAIL_COND(!p_data.has("format"));
  1467. ERR_FAIL_COND(!p_data.has("mipmaps"));
  1468. ERR_FAIL_COND(!p_data.has("data"));
  1469. int dwidth = p_data["width"];
  1470. int dheight = p_data["height"];
  1471. String dformat = p_data["format"];
  1472. bool dmipmaps = p_data["mipmaps"];
  1473. PoolVector<uint8_t> ddata = p_data["data"];
  1474. Format ddformat = FORMAT_MAX;
  1475. for (int i = 0; i < FORMAT_MAX; i++) {
  1476. if (dformat == get_format_name(Format(i))) {
  1477. ddformat = Format(i);
  1478. break;
  1479. }
  1480. }
  1481. ERR_FAIL_COND(ddformat == FORMAT_MAX);
  1482. create(dwidth, dheight, dmipmaps, ddformat, ddata);
  1483. }
  1484. Dictionary Image::_get_data() const {
  1485. Dictionary d;
  1486. d["width"] = width;
  1487. d["height"] = height;
  1488. d["format"] = get_format_name(format);
  1489. d["mipmaps"] = mipmaps;
  1490. d["data"] = data;
  1491. return d;
  1492. }
  1493. void Image::lock() {
  1494. ERR_FAIL_COND(data.size() == 0);
  1495. write_lock = data.write();
  1496. }
  1497. void Image::unlock() {
  1498. write_lock = PoolVector<uint8_t>::Write();
  1499. }
  1500. Color Image::get_pixelv(const Point2 &p_src) const {
  1501. return get_pixel(p_src.x, p_src.y);
  1502. }
  1503. Color Image::get_pixel(int p_x, int p_y) const {
  1504. uint8_t *ptr = write_lock.ptr();
  1505. #ifdef DEBUG_ENABLED
  1506. if (!ptr) {
  1507. ERR_EXPLAIN("Image must be locked with 'lock()' before using get_pixel()");
  1508. ERR_FAIL_COND_V(!ptr, Color());
  1509. }
  1510. ERR_FAIL_INDEX_V(p_x, width, Color());
  1511. ERR_FAIL_INDEX_V(p_y, height, Color());
  1512. #endif
  1513. uint32_t ofs = p_y * width + p_x;
  1514. switch (format) {
  1515. case FORMAT_L8: {
  1516. float l = ptr[ofs] / 255.0;
  1517. return Color(l, l, l, 1);
  1518. } break;
  1519. case FORMAT_LA8: {
  1520. float l = ptr[ofs * 2 + 0] / 255.0;
  1521. float a = ptr[ofs * 2 + 1] / 255.0;
  1522. return Color(l, l, l, a);
  1523. } break;
  1524. case FORMAT_R8: {
  1525. float r = ptr[ofs] / 255.0;
  1526. return Color(r, 0, 0, 1);
  1527. } break;
  1528. case FORMAT_RG8: {
  1529. float r = ptr[ofs * 2 + 0] / 255.0;
  1530. float g = ptr[ofs * 2 + 1] / 255.0;
  1531. return Color(r, g, 0, 1);
  1532. } break;
  1533. case FORMAT_RGB8: {
  1534. float r = ptr[ofs * 3 + 0] / 255.0;
  1535. float g = ptr[ofs * 3 + 1] / 255.0;
  1536. float b = ptr[ofs * 3 + 2] / 255.0;
  1537. return Color(r, g, b, 1);
  1538. } break;
  1539. case FORMAT_RGBA8: {
  1540. float r = ptr[ofs * 4 + 0] / 255.0;
  1541. float g = ptr[ofs * 4 + 1] / 255.0;
  1542. float b = ptr[ofs * 4 + 2] / 255.0;
  1543. float a = ptr[ofs * 4 + 3] / 255.0;
  1544. return Color(r, g, b, a);
  1545. } break;
  1546. case FORMAT_RGBA4444: {
  1547. uint16_t u = ((uint16_t *)ptr)[ofs];
  1548. float r = (u & 0xF) / 15.0;
  1549. float g = ((u >> 4) & 0xF) / 15.0;
  1550. float b = ((u >> 8) & 0xF) / 15.0;
  1551. float a = ((u >> 12) & 0xF) / 15.0;
  1552. return Color(r, g, b, a);
  1553. } break;
  1554. case FORMAT_RGBA5551: {
  1555. uint16_t u = ((uint16_t *)ptr)[ofs];
  1556. float r = (u & 0x1F) / 15.0;
  1557. float g = ((u >> 5) & 0x1F) / 15.0;
  1558. float b = ((u >> 10) & 0x1F) / 15.0;
  1559. float a = ((u >> 15) & 0x1) / 1.0;
  1560. return Color(r, g, b, a);
  1561. } break;
  1562. case FORMAT_RF: {
  1563. float r = ((float *)ptr)[ofs];
  1564. return Color(r, 0, 0, 1);
  1565. } break;
  1566. case FORMAT_RGF: {
  1567. float r = ((float *)ptr)[ofs * 2 + 0];
  1568. float g = ((float *)ptr)[ofs * 2 + 1];
  1569. return Color(r, g, 0, 1);
  1570. } break;
  1571. case FORMAT_RGBF: {
  1572. float r = ((float *)ptr)[ofs * 3 + 0];
  1573. float g = ((float *)ptr)[ofs * 3 + 1];
  1574. float b = ((float *)ptr)[ofs * 3 + 2];
  1575. return Color(r, g, b, 1);
  1576. } break;
  1577. case FORMAT_RGBAF: {
  1578. float r = ((float *)ptr)[ofs * 4 + 0];
  1579. float g = ((float *)ptr)[ofs * 4 + 1];
  1580. float b = ((float *)ptr)[ofs * 4 + 2];
  1581. float a = ((float *)ptr)[ofs * 4 + 3];
  1582. return Color(r, g, b, a);
  1583. } break;
  1584. case FORMAT_RH: {
  1585. uint16_t r = ((uint16_t *)ptr)[ofs];
  1586. return Color(Math::half_to_float(r), 0, 0, 1);
  1587. } break;
  1588. case FORMAT_RGH: {
  1589. uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0];
  1590. uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1];
  1591. return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1);
  1592. } break;
  1593. case FORMAT_RGBH: {
  1594. uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0];
  1595. uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1];
  1596. uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2];
  1597. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1);
  1598. } break;
  1599. case FORMAT_RGBAH: {
  1600. uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0];
  1601. uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1];
  1602. uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2];
  1603. uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3];
  1604. return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a));
  1605. } break;
  1606. case FORMAT_RGBE9995: {
  1607. uint32_t rgbe = ((uint32_t *)ptr)[ofs];
  1608. float r = rgbe & 0x1ff;
  1609. float g = (rgbe >> 9) & 0x1ff;
  1610. float b = (rgbe >> 18) & 0x1ff;
  1611. float e = (rgbe >> 27);
  1612. float m = Math::pow(2, e - 15.0 - 9.0);
  1613. ;
  1614. float rd = r * m;
  1615. float gd = g * m;
  1616. float bd = b * m;
  1617. return Color(rd, gd, bd, 1.0);
  1618. } break;
  1619. default: {
  1620. ERR_EXPLAIN("Can't get_pixel() on compressed image, sorry.");
  1621. ERR_FAIL_V(Color());
  1622. }
  1623. }
  1624. return Color();
  1625. }
  1626. void Image::set_pixelv(const Point2 &p_dst, const Color &p_color) {
  1627. return set_pixel(p_dst.x, p_dst.y, p_color);
  1628. }
  1629. void Image::set_pixel(int p_x, int p_y, const Color &p_color) {
  1630. uint8_t *ptr = write_lock.ptr();
  1631. #ifdef DEBUG_ENABLED
  1632. if (!ptr) {
  1633. ERR_EXPLAIN("Image must be locked with 'lock()' before using set_pixel()");
  1634. ERR_FAIL_COND(!ptr);
  1635. }
  1636. ERR_FAIL_INDEX(p_x, width);
  1637. ERR_FAIL_INDEX(p_y, height);
  1638. #endif
  1639. uint32_t ofs = p_y * width + p_x;
  1640. switch (format) {
  1641. case FORMAT_L8: {
  1642. ptr[ofs] = uint8_t(CLAMP(p_color.gray() * 255.0, 0, 255));
  1643. } break;
  1644. case FORMAT_LA8: {
  1645. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.gray() * 255.0, 0, 255));
  1646. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  1647. } break;
  1648. case FORMAT_R8: {
  1649. ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1650. } break;
  1651. case FORMAT_RG8: {
  1652. ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1653. ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1654. } break;
  1655. case FORMAT_RGB8: {
  1656. ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1657. ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1658. ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  1659. } break;
  1660. case FORMAT_RGBA8: {
  1661. ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255));
  1662. ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255));
  1663. ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255));
  1664. ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255));
  1665. } break;
  1666. case FORMAT_RGBA4444: {
  1667. uint16_t rgba = 0;
  1668. rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15));
  1669. rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 4;
  1670. rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 8;
  1671. rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15)) << 12;
  1672. ((uint16_t *)ptr)[ofs] = rgba;
  1673. } break;
  1674. case FORMAT_RGBA5551: {
  1675. uint16_t rgba = 0;
  1676. rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
  1677. rgba |= uint16_t(CLAMP(p_color.g * 31.0, 0, 31)) << 5;
  1678. rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 10;
  1679. rgba |= uint16_t(p_color.a > 0.5 ? 1 : 0) << 15;
  1680. ((uint16_t *)ptr)[ofs] = rgba;
  1681. } break;
  1682. case FORMAT_RF: {
  1683. ((float *)ptr)[ofs] = p_color.r;
  1684. } break;
  1685. case FORMAT_RGF: {
  1686. ((float *)ptr)[ofs * 2 + 0] = p_color.r;
  1687. ((float *)ptr)[ofs * 2 + 1] = p_color.g;
  1688. } break;
  1689. case FORMAT_RGBF: {
  1690. ((float *)ptr)[ofs * 3 + 0] = p_color.r;
  1691. ((float *)ptr)[ofs * 3 + 1] = p_color.g;
  1692. ((float *)ptr)[ofs * 3 + 2] = p_color.b;
  1693. } break;
  1694. case FORMAT_RGBAF: {
  1695. ((float *)ptr)[ofs * 4 + 0] = p_color.r;
  1696. ((float *)ptr)[ofs * 4 + 1] = p_color.g;
  1697. ((float *)ptr)[ofs * 4 + 2] = p_color.b;
  1698. ((float *)ptr)[ofs * 4 + 3] = p_color.a;
  1699. } break;
  1700. case FORMAT_RH: {
  1701. ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r);
  1702. } break;
  1703. case FORMAT_RGH: {
  1704. ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r);
  1705. ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g);
  1706. } break;
  1707. case FORMAT_RGBH: {
  1708. ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r);
  1709. ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g);
  1710. ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b);
  1711. } break;
  1712. case FORMAT_RGBAH: {
  1713. ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r);
  1714. ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g);
  1715. ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b);
  1716. ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a);
  1717. } break;
  1718. case FORMAT_RGBE9995: {
  1719. ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995();
  1720. } break;
  1721. default: {
  1722. ERR_EXPLAIN("Can't set_pixel() on compressed image, sorry.");
  1723. ERR_FAIL();
  1724. }
  1725. }
  1726. }
  1727. Image::DetectChannels Image::get_detected_channels() {
  1728. ERR_FAIL_COND_V(data.size() == 0, DETECTED_RGBA);
  1729. ERR_FAIL_COND_V(is_compressed(), DETECTED_RGBA);
  1730. bool r = false, g = false, b = false, a = false, c = false;
  1731. lock();
  1732. for (int i = 0; i < width; i++) {
  1733. for (int j = 0; j < height; j++) {
  1734. Color col = get_pixel(i, j);
  1735. if (col.r > 0.001)
  1736. r = true;
  1737. if (col.g > 0.001)
  1738. g = true;
  1739. if (col.b > 0.001)
  1740. b = true;
  1741. if (col.a < 0.999)
  1742. a = true;
  1743. if (col.r != col.b || col.r != col.g || col.b != col.g) {
  1744. c = true;
  1745. }
  1746. }
  1747. }
  1748. unlock();
  1749. if (!c && !a)
  1750. return DETECTED_L;
  1751. if (!c && a)
  1752. return DETECTED_LA;
  1753. if (r && !g && !b && !a)
  1754. return DETECTED_R;
  1755. if (r && g && !b && !a)
  1756. return DETECTED_RG;
  1757. if (r && g && b && !a)
  1758. return DETECTED_RGB;
  1759. return DETECTED_RGBA;
  1760. }
  1761. void Image::_bind_methods() {
  1762. ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
  1763. ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height);
  1764. ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size);
  1765. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps);
  1766. ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format);
  1767. ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data);
  1768. ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert);
  1769. ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset);
  1770. ClassDB::bind_method(D_METHOD("resize_to_po2", "square"), &Image::resize_to_po2, DEFVAL(false));
  1771. ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR));
  1772. ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2);
  1773. ClassDB::bind_method(D_METHOD("expand_x2_hq2x"), &Image::expand_x2_hq2x);
  1774. ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop);
  1775. ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x);
  1776. ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y);
  1777. ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false));
  1778. ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps);
  1779. ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty);
  1780. ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data);
  1781. ClassDB::bind_method(D_METHOD("is_empty"), &Image::empty);
  1782. ClassDB::bind_method(D_METHOD("load", "path"), &Image::load);
  1783. ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png);
  1784. ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
  1785. ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);
  1786. ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress);
  1787. ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress);
  1788. ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed);
  1789. ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges);
  1790. ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha);
  1791. ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear);
  1792. ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy);
  1793. ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb);
  1794. ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap", "bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0));
  1795. ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect);
  1796. ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask);
  1797. ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect);
  1798. ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask);
  1799. ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill);
  1800. ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect);
  1801. ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect);
  1802. ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from);
  1803. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data);
  1804. ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data);
  1805. ClassDB::bind_method(D_METHOD("lock"), &Image::lock);
  1806. ClassDB::bind_method(D_METHOD("unlock"), &Image::unlock);
  1807. ClassDB::bind_method(D_METHOD("get_pixelv", "src"), &Image::get_pixelv);
  1808. ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel);
  1809. ClassDB::bind_method(D_METHOD("set_pixelv", "dst", "color"), &Image::set_pixelv);
  1810. ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel);
  1811. ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer);
  1812. ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer);
  1813. ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
  1814. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
  1815. BIND_ENUM_CONSTANT(FORMAT_L8); //luminance
  1816. BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha
  1817. BIND_ENUM_CONSTANT(FORMAT_R8);
  1818. BIND_ENUM_CONSTANT(FORMAT_RG8);
  1819. BIND_ENUM_CONSTANT(FORMAT_RGB8);
  1820. BIND_ENUM_CONSTANT(FORMAT_RGBA8);
  1821. BIND_ENUM_CONSTANT(FORMAT_RGBA4444);
  1822. BIND_ENUM_CONSTANT(FORMAT_RGBA5551);
  1823. BIND_ENUM_CONSTANT(FORMAT_RF); //float
  1824. BIND_ENUM_CONSTANT(FORMAT_RGF);
  1825. BIND_ENUM_CONSTANT(FORMAT_RGBF);
  1826. BIND_ENUM_CONSTANT(FORMAT_RGBAF);
  1827. BIND_ENUM_CONSTANT(FORMAT_RH); //half float
  1828. BIND_ENUM_CONSTANT(FORMAT_RGH);
  1829. BIND_ENUM_CONSTANT(FORMAT_RGBH);
  1830. BIND_ENUM_CONSTANT(FORMAT_RGBAH);
  1831. BIND_ENUM_CONSTANT(FORMAT_RGBE9995);
  1832. BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1
  1833. BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2
  1834. BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3
  1835. BIND_ENUM_CONSTANT(FORMAT_RGTC_R);
  1836. BIND_ENUM_CONSTANT(FORMAT_RGTC_RG);
  1837. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h
  1838. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float /
  1839. BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float
  1840. BIND_ENUM_CONSTANT(FORMAT_PVRTC2); //pvrtc
  1841. BIND_ENUM_CONSTANT(FORMAT_PVRTC2A);
  1842. BIND_ENUM_CONSTANT(FORMAT_PVRTC4);
  1843. BIND_ENUM_CONSTANT(FORMAT_PVRTC4A);
  1844. BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1
  1845. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2
  1846. BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb.
  1847. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11);
  1848. BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S);
  1849. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8);
  1850. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8);
  1851. BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1);
  1852. BIND_ENUM_CONSTANT(FORMAT_MAX);
  1853. BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST);
  1854. BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR);
  1855. BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC);
  1856. BIND_ENUM_CONSTANT(ALPHA_NONE);
  1857. BIND_ENUM_CONSTANT(ALPHA_BIT);
  1858. BIND_ENUM_CONSTANT(ALPHA_BLEND);
  1859. BIND_ENUM_CONSTANT(COMPRESS_S3TC);
  1860. BIND_ENUM_CONSTANT(COMPRESS_PVRTC2);
  1861. BIND_ENUM_CONSTANT(COMPRESS_PVRTC4);
  1862. BIND_ENUM_CONSTANT(COMPRESS_ETC);
  1863. BIND_ENUM_CONSTANT(COMPRESS_ETC2);
  1864. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC);
  1865. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB);
  1866. BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
  1867. }
  1868. void Image::set_compress_bc_func(void (*p_compress_func)(Image *, CompressSource)) {
  1869. _image_compress_bc_func = p_compress_func;
  1870. }
  1871. void Image::normalmap_to_xy() {
  1872. convert(Image::FORMAT_RGBA8);
  1873. {
  1874. int len = data.size() / 4;
  1875. PoolVector<uint8_t>::Write wp = data.write();
  1876. unsigned char *data_ptr = wp.ptr();
  1877. for (int i = 0; i < len; i++) {
  1878. data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w
  1879. data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz
  1880. data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1];
  1881. }
  1882. }
  1883. convert(Image::FORMAT_LA8);
  1884. }
  1885. Ref<Image> Image::rgbe_to_srgb() {
  1886. if (data.size() == 0)
  1887. return Ref<Image>();
  1888. ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>());
  1889. Ref<Image> new_image;
  1890. new_image.instance();
  1891. new_image->create(width, height, 0, Image::FORMAT_RGB8);
  1892. lock();
  1893. new_image->lock();
  1894. for (int row = 0; row < height; row++) {
  1895. for (int col = 0; col < width; col++) {
  1896. new_image->set_pixel(col, row, get_pixel(col, row).to_srgb());
  1897. }
  1898. }
  1899. unlock();
  1900. new_image->unlock();
  1901. if (has_mipmaps()) {
  1902. new_image->generate_mipmaps();
  1903. }
  1904. return new_image;
  1905. }
  1906. void Image::bumpmap_to_normalmap(float bump_scale) {
  1907. ERR_FAIL_COND(!_can_modify(format));
  1908. convert(Image::FORMAT_RF);
  1909. PoolVector<uint8_t> result_image; //rgba output
  1910. result_image.resize(width * height * 4);
  1911. {
  1912. PoolVector<uint8_t>::Read rp = data.read();
  1913. PoolVector<uint8_t>::Write wp = result_image.write();
  1914. unsigned char *write_ptr = wp.ptr();
  1915. float *read_ptr = (float *)rp.ptr();
  1916. for (int ty = 0; ty < height; ty++) {
  1917. int py = ty + 1;
  1918. if (py >= height) py -= height;
  1919. for (int tx = 0; tx < width; tx++) {
  1920. int px = tx + 1;
  1921. if (px >= width) px -= width;
  1922. float here = read_ptr[ty * width + tx];
  1923. float to_right = read_ptr[ty * width + px];
  1924. float above = read_ptr[py * width + tx];
  1925. Vector3 up = Vector3(0, 1, (here - above) * bump_scale);
  1926. Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale);
  1927. Vector3 normal = across.cross(up);
  1928. normal.normalize();
  1929. write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5);
  1930. write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5);
  1931. write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5);
  1932. write_ptr[((ty * width + tx) << 2) + 3] = 255;
  1933. }
  1934. }
  1935. }
  1936. format = FORMAT_RGBA8;
  1937. data = result_image;
  1938. }
  1939. void Image::srgb_to_linear() {
  1940. if (data.size() == 0)
  1941. return;
  1942. 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 };
  1943. ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8);
  1944. if (format == FORMAT_RGBA8) {
  1945. int len = data.size() / 4;
  1946. PoolVector<uint8_t>::Write wp = data.write();
  1947. unsigned char *data_ptr = wp.ptr();
  1948. for (int i = 0; i < len; i++) {
  1949. data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]];
  1950. data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]];
  1951. data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]];
  1952. }
  1953. } else if (format == FORMAT_RGB8) {
  1954. int len = data.size() / 3;
  1955. PoolVector<uint8_t>::Write wp = data.write();
  1956. unsigned char *data_ptr = wp.ptr();
  1957. for (int i = 0; i < len; i++) {
  1958. data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]];
  1959. data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]];
  1960. data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]];
  1961. }
  1962. }
  1963. }
  1964. void Image::premultiply_alpha() {
  1965. if (data.size() == 0)
  1966. return;
  1967. if (format != FORMAT_RGBA8)
  1968. return; //not needed
  1969. PoolVector<uint8_t>::Write wp = data.write();
  1970. unsigned char *data_ptr = wp.ptr();
  1971. for (int i = 0; i < height; i++) {
  1972. for (int j = 0; j < width; j++) {
  1973. uint8_t *ptr = &data_ptr[(i * width + j) * 4];
  1974. ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8;
  1975. ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8;
  1976. ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8;
  1977. }
  1978. }
  1979. }
  1980. void Image::fix_alpha_edges() {
  1981. if (data.size() == 0)
  1982. return;
  1983. if (format != FORMAT_RGBA8)
  1984. return; //not needed
  1985. PoolVector<uint8_t> dcopy = data;
  1986. PoolVector<uint8_t>::Read rp = dcopy.read();
  1987. const uint8_t *srcptr = rp.ptr();
  1988. PoolVector<uint8_t>::Write wp = data.write();
  1989. unsigned char *data_ptr = wp.ptr();
  1990. const int max_radius = 4;
  1991. const int alpha_threshold = 20;
  1992. const int max_dist = 0x7FFFFFFF;
  1993. for (int i = 0; i < height; i++) {
  1994. for (int j = 0; j < width; j++) {
  1995. const uint8_t *rptr = &srcptr[(i * width + j) * 4];
  1996. uint8_t *wptr = &data_ptr[(i * width + j) * 4];
  1997. if (rptr[3] >= alpha_threshold)
  1998. continue;
  1999. int closest_dist = max_dist;
  2000. uint8_t closest_color[3];
  2001. int from_x = MAX(0, j - max_radius);
  2002. int to_x = MIN(width - 1, j + max_radius);
  2003. int from_y = MAX(0, i - max_radius);
  2004. int to_y = MIN(height - 1, i + max_radius);
  2005. for (int k = from_y; k <= to_y; k++) {
  2006. for (int l = from_x; l <= to_x; l++) {
  2007. int dy = i - k;
  2008. int dx = j - l;
  2009. int dist = dy * dy + dx * dx;
  2010. if (dist >= closest_dist)
  2011. continue;
  2012. const uint8_t *rp = &srcptr[(k * width + l) << 2];
  2013. if (rp[3] < alpha_threshold)
  2014. continue;
  2015. closest_dist = dist;
  2016. closest_color[0] = rp[0];
  2017. closest_color[1] = rp[1];
  2018. closest_color[2] = rp[2];
  2019. }
  2020. }
  2021. if (closest_dist != max_dist) {
  2022. wptr[0] = closest_color[0];
  2023. wptr[1] = closest_color[1];
  2024. wptr[2] = closest_color[2];
  2025. }
  2026. }
  2027. }
  2028. }
  2029. String Image::get_format_name(Format p_format) {
  2030. ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String());
  2031. return format_names[p_format];
  2032. }
  2033. Error Image::load_png_from_buffer(const PoolVector<uint8_t> &p_array) {
  2034. return _load_from_buffer(p_array, _png_mem_loader_func);
  2035. }
  2036. Error Image::load_jpg_from_buffer(const PoolVector<uint8_t> &p_array) {
  2037. return _load_from_buffer(p_array, _jpg_mem_loader_func);
  2038. }
  2039. Error Image::load_webp_from_buffer(const PoolVector<uint8_t> &p_array) {
  2040. return _load_from_buffer(p_array, _webp_mem_loader_func);
  2041. }
  2042. Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
  2043. int buffer_size = p_array.size();
  2044. ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
  2045. ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER);
  2046. PoolVector<uint8_t>::Read r = p_array.read();
  2047. Ref<Image> image = p_loader(r.ptr(), buffer_size);
  2048. ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR);
  2049. copy_internals_from(image);
  2050. return OK;
  2051. }
  2052. Image::Image(const uint8_t *p_mem_png_jpg, int p_len) {
  2053. width = 0;
  2054. height = 0;
  2055. mipmaps = false;
  2056. format = FORMAT_L8;
  2057. if (_png_mem_loader_func) {
  2058. copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len));
  2059. }
  2060. if (empty() && _jpg_mem_loader_func) {
  2061. copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len));
  2062. }
  2063. }
  2064. Ref<Resource> Image::duplicate(bool p_subresources) const {
  2065. Ref<Image> copy;
  2066. copy.instance();
  2067. copy->_copy_internals_from(*this);
  2068. return copy;
  2069. }
  2070. Image::Image() {
  2071. width = 0;
  2072. height = 0;
  2073. mipmaps = false;
  2074. format = FORMAT_L8;
  2075. }
  2076. Image::~Image() {
  2077. if (write_lock.ptr()) {
  2078. unlock();
  2079. }
  2080. }