image.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  1. /*************************************************************************/
  2. /* image.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "image.h"
  30. #include "hash_map.h"
  31. #include "core/io/image_loader.h"
  32. #include "core/os/copymem.h"
  33. #include "print_string.h"
  34. #include <stdio.h>
  35. const char* Image::format_names[Image::FORMAT_MAX]={
  36. "Grayscale",
  37. "Intensity",
  38. "GrayscaleAlpha",
  39. "RGB",
  40. "RGBA",
  41. "Indexed",
  42. "IndexedAlpha",
  43. "YUV422",
  44. "YUV444",
  45. "BC1",
  46. "BC2",
  47. "BC3",
  48. "BC4",
  49. "BC5",
  50. "PVRTC2",
  51. "PVRTC2Alpha",
  52. "PVRTC4",
  53. "PVRTC4Alpha",
  54. "ETC",
  55. "ATC",
  56. "ATCAlphaExp",
  57. "ATCAlphaInterp",
  58. };
  59. SavePNGFunc Image::save_png_func = NULL;
  60. void Image::_put_pixel(int p_x,int p_y, const BColor& p_color, unsigned char *p_data) {
  61. _put_pixelw(p_x,p_y,width,p_color,p_data);
  62. }
  63. void Image::_put_pixelw(int p_x,int p_y, int p_width, const BColor& p_color, unsigned char *p_data) {
  64. int ofs=p_y*p_width+p_x;
  65. switch(format) {
  66. case FORMAT_GRAYSCALE: {
  67. p_data[ofs]=p_color.gray();
  68. } break;
  69. case FORMAT_INTENSITY: {
  70. p_data[ofs]=p_color.a;
  71. } break;
  72. case FORMAT_GRAYSCALE_ALPHA: {
  73. p_data[ofs*2]=p_color.gray();
  74. p_data[ofs*2+1]=p_color.a;
  75. } break;
  76. case FORMAT_RGB: {
  77. p_data[ofs*3+0]=p_color.r;
  78. p_data[ofs*3+1]=p_color.g;
  79. p_data[ofs*3+2]=p_color.b;
  80. } break;
  81. case FORMAT_RGBA: {
  82. p_data[ofs*4+0]=p_color.r;
  83. p_data[ofs*4+1]=p_color.g;
  84. p_data[ofs*4+2]=p_color.b;
  85. p_data[ofs*4+3]=p_color.a;
  86. } break;
  87. case FORMAT_INDEXED:
  88. case FORMAT_INDEXED_ALPHA: {
  89. ERR_FAIL();
  90. } break;
  91. default: {};
  92. }
  93. }
  94. void Image::_get_mipmap_offset_and_size(int p_mipmap,int &r_offset, int &r_width,int &r_height) const {
  95. int w=width;
  96. int h=height;
  97. int ofs=0;
  98. int pixel_size = get_format_pixel_size(format);
  99. int pixel_rshift = get_format_pixel_rshift(format);
  100. int minw,minh;
  101. _get_format_min_data_size(format,minw,minh);
  102. for(int i=0;i<p_mipmap;i++) {
  103. int s = w*h;
  104. s*=pixel_size;
  105. s>>=pixel_rshift;
  106. ofs+=s;
  107. w=MAX(minw,w>>1);
  108. h=MAX(minh,h>>1);
  109. }
  110. r_offset=ofs;
  111. r_width=w;
  112. r_height=h;
  113. }
  114. int Image::get_mipmap_offset(int p_mipmap) const {
  115. ERR_FAIL_INDEX_V(p_mipmap,(mipmaps+1),-1);
  116. int ofs,w,h;
  117. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  118. return ofs;
  119. }
  120. void Image::get_mipmap_offset_and_size(int p_mipmap,int &r_ofs, int &r_size) const {
  121. int ofs,w,h;
  122. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  123. int ofs2;
  124. _get_mipmap_offset_and_size(p_mipmap+1,ofs2,w,h);
  125. r_ofs=ofs;
  126. r_size=ofs2-ofs;
  127. }
  128. void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap,int &r_ofs, int &r_size,int &w, int& h) const {
  129. int ofs;
  130. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  131. int ofs2,w2,h2;
  132. _get_mipmap_offset_and_size(p_mipmap+1,ofs2,w2,h2);
  133. r_ofs=ofs;
  134. r_size=ofs2-ofs;
  135. }
  136. void Image::put_pixel(int p_x,int p_y, const Color& p_color,int p_mipmap){
  137. ERR_FAIL_INDEX(p_mipmap,mipmaps+1);
  138. int ofs,w,h;
  139. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  140. ERR_FAIL_INDEX(p_x,w);
  141. ERR_FAIL_INDEX(p_y,h);
  142. DVector<uint8_t>::Write wp = data.write();
  143. unsigned char *data_ptr=wp.ptr();
  144. _put_pixelw(p_x,p_y,w,BColor(p_color.r*255,p_color.g*255,p_color.b*255,p_color.a*255),&data_ptr[ofs]);
  145. }
  146. Image::BColor Image::_get_pixel(int p_x,int p_y,const unsigned char *p_data,int p_data_size) const{
  147. return _get_pixelw(p_x,p_y,width,p_data,p_data_size);
  148. }
  149. Image::BColor Image::_get_pixelw(int p_x,int p_y,int p_width,const unsigned char *p_data,int p_data_size) const{
  150. int ofs=p_y*p_width+p_x;
  151. BColor result(0,0,0,0);
  152. switch(format) {
  153. case FORMAT_GRAYSCALE: {
  154. result=BColor(p_data[ofs],p_data[ofs],p_data[ofs],255.0);
  155. } break;
  156. case FORMAT_INTENSITY: {
  157. result=BColor(255,255,255,p_data[ofs]);
  158. } break;
  159. case FORMAT_GRAYSCALE_ALPHA: {
  160. result=BColor(p_data[ofs*2],p_data[ofs*2],p_data[ofs*2],p_data[ofs*2+1]);
  161. } break;
  162. case FORMAT_RGB: {
  163. result=BColor(p_data[ofs*3],p_data[ofs*3+1],p_data[ofs*3+2]);
  164. } break;
  165. case FORMAT_RGBA: {
  166. result=BColor(p_data[ofs*4],p_data[ofs*4+1],p_data[ofs*4+2],p_data[ofs*4+3]);
  167. } break;
  168. case FORMAT_INDEXED_ALPHA: {
  169. int pitch = 4;
  170. const uint8_t* pal = &p_data[ p_data_size - pitch * 256 ];
  171. int idx = p_data[ofs];
  172. result=BColor(pal[idx * pitch + 0] , pal[idx * pitch + 1] , pal[idx * pitch + 2] , pal[idx * pitch + 3] );
  173. } break;
  174. case FORMAT_INDEXED: {
  175. int pitch = 3;
  176. const uint8_t* pal = &p_data[ p_data_size - pitch * 256 ];
  177. int idx = p_data[ofs];
  178. result=BColor(pal[idx * pitch + 0] , pal[idx * pitch + 1] , pal[idx * pitch + 2] ,255);
  179. } break;
  180. case FORMAT_YUV_422: {
  181. int y, u, v;
  182. if (p_x % 2) {
  183. const uint8_t* yp = &p_data[p_width * 2 * p_y + p_x * 2];
  184. u = *(yp-1);
  185. y = yp[0];
  186. v = yp[1];
  187. } else {
  188. const uint8_t* yp = &p_data[p_width * 2 * p_y + p_x * 2];
  189. y = yp[0];
  190. u = yp[1];
  191. v = yp[3];
  192. };
  193. int32_t r = 1.164 * (y - 16) + 1.596 * (v - 128);
  194. int32_t g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  195. int32_t b = 1.164 * (y - 16) + 2.018 * (u - 128);
  196. result = BColor(CLAMP(r, 0, 255), CLAMP(g, 0, 255), CLAMP(b, 0, 255));
  197. } break;
  198. case FORMAT_YUV_444: {
  199. uint8_t y, u, v;
  200. const uint8_t* yp = &p_data[p_width * 3 * p_y + p_x * 3];
  201. y = yp[0];
  202. u = yp[1];
  203. v = yp[2];
  204. int32_t r = 1.164 * (y - 16) + 1.596 * (v - 128);
  205. int32_t g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  206. int32_t b = 1.164 * (y - 16) + 2.018 * (u - 128);
  207. result = BColor(CLAMP(r, 0, 255), CLAMP(g, 0, 255), CLAMP(b, 0, 255));
  208. } break;
  209. default:{}
  210. }
  211. return result;
  212. }
  213. void Image::put_indexed_pixel(int p_x, int p_y, uint8_t p_idx,int p_mipmap) {
  214. ERR_FAIL_COND(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA);
  215. ERR_FAIL_INDEX(p_mipmap,mipmaps+1);
  216. int ofs,w,h;
  217. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  218. ERR_FAIL_INDEX(p_x,w);
  219. ERR_FAIL_INDEX(p_y,h);
  220. data.set(ofs + p_y * w + p_x, p_idx);
  221. };
  222. uint8_t Image::get_indexed_pixel(int p_x, int p_y,int p_mipmap) const {
  223. ERR_FAIL_COND_V(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA, 0);
  224. ERR_FAIL_INDEX_V(p_mipmap,mipmaps+1,0);
  225. int ofs,w,h;
  226. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  227. ERR_FAIL_INDEX_V(p_x,w,0);
  228. ERR_FAIL_INDEX_V(p_y,h,0);
  229. return data[ofs + p_y * w + p_x];
  230. };
  231. void Image::set_pallete(const DVector<uint8_t>& p_data) {
  232. int len = p_data.size();
  233. ERR_FAIL_COND(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA);
  234. ERR_FAIL_COND(format == FORMAT_INDEXED && len!=(256*3));
  235. ERR_FAIL_COND(format == FORMAT_INDEXED_ALPHA && len!=(256*4));
  236. int ofs,w,h;
  237. _get_mipmap_offset_and_size(mipmaps+1,ofs,w,h);
  238. int pal_ofs = ofs;
  239. data.resize(pal_ofs + p_data.size());
  240. DVector<uint8_t>::Write wp = data.write();
  241. unsigned char *dst=wp.ptr() + pal_ofs;
  242. DVector<uint8_t>::Read r = data.read();
  243. const unsigned char *src=r.ptr();
  244. copymem(dst, src, len);
  245. };
  246. int Image::get_width() const {
  247. return width;
  248. }
  249. int Image::get_height() const{
  250. return height;
  251. }
  252. int Image::get_mipmaps() const {
  253. return mipmaps;
  254. }
  255. Color Image::get_pixel(int p_x,int p_y,int p_mipmap) const {
  256. ERR_FAIL_INDEX_V(p_mipmap,mipmaps+1,Color());
  257. int ofs,w,h;
  258. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  259. ERR_FAIL_INDEX_V(p_x,w,Color());
  260. ERR_FAIL_INDEX_V(p_y,h,Color());
  261. int len = data.size();
  262. DVector<uint8_t>::Read r = data.read();
  263. const unsigned char*data_ptr=r.ptr();
  264. BColor c = _get_pixelw(p_x,p_y,w,&data_ptr[ofs],len);
  265. return Color( c.r/255.0,c.g/255.0,c.b/255.0,c.a/255.0 );
  266. }
  267. void Image::convert( Format p_new_format ){
  268. if (data.size()==0)
  269. return;
  270. if (p_new_format==format)
  271. return;
  272. if (format>=FORMAT_BC1 || p_new_format>=FORMAT_BC1) {
  273. ERR_EXPLAIN("Cannot convert to <-> from compressed/custom image formats (for now).");
  274. ERR_FAIL();
  275. }
  276. if (p_new_format==FORMAT_INDEXED || p_new_format==FORMAT_INDEXED_ALPHA) {
  277. return;
  278. }
  279. Image new_img(width,height,0,p_new_format);
  280. int len=data.size();
  281. DVector<uint8_t>::Read r = data.read();
  282. DVector<uint8_t>::Write w = new_img.data.write();
  283. const uint8_t *rptr = r.ptr();
  284. uint8_t *wptr = w.ptr();
  285. if (p_new_format==FORMAT_RGBA && format==FORMAT_INDEXED_ALPHA) {
  286. //optimized unquantized form
  287. int dataend = len-256*4;
  288. const uint32_t *palpos = (const uint32_t*)&rptr[dataend];
  289. uint32_t *dst32 = (uint32_t *)wptr;
  290. for(int i=0;i<dataend;i++)
  291. dst32[i]=palpos[rptr[i]]; //since this is read/write, endianness is not a problem
  292. } else {
  293. //this is temporary, must find a faster way to do it.
  294. for(int i=0;i<width;i++)
  295. for(int j=0;j<height;j++)
  296. new_img._put_pixel(i,j,_get_pixel(i,j,rptr,len),wptr);
  297. }
  298. r = DVector<uint8_t>::Read();
  299. w = DVector<uint8_t>::Write();
  300. bool gen_mipmaps=mipmaps>0;
  301. *this=new_img;
  302. if (gen_mipmaps)
  303. generate_mipmaps();
  304. }
  305. Image::Format Image::get_format() const{
  306. return format;
  307. }
  308. static double _bicubic_interp_kernel( double x ) {
  309. x = ABS(x);
  310. double bc = 0;
  311. if ( x <= 1 )
  312. bc = ( 1.5 * x - 2.5 ) * x * x + 1;
  313. else if ( x < 2 )
  314. bc = ( ( -0.5 * x + 2.5 ) * x - 4 ) * x + 2;
  315. return bc;
  316. }
  317. template<int CC>
  318. 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) {
  319. // get source image size
  320. int width = p_src_width;
  321. int height = p_src_height;
  322. double xfac = (double) width / p_dst_width;
  323. double yfac = (double) height / p_dst_height;
  324. // coordinates of source points and cooefficiens
  325. double ox, oy, dx, dy, k1, k2;
  326. int ox1, oy1, ox2, oy2;
  327. // destination pixel values
  328. // width and height decreased by 1
  329. int ymax = height - 1;
  330. int xmax = width - 1;
  331. // temporary pointer
  332. for ( int y = 0; y < p_dst_height; y++ ) {
  333. // Y coordinates
  334. oy = (double) y * yfac - 0.5f;
  335. oy1 = (int) oy;
  336. dy = oy - (double) oy1;
  337. for ( int x = 0; x < p_dst_width; x++ ) {
  338. // X coordinates
  339. ox = (double) x * xfac - 0.5f;
  340. ox1 = (int) ox;
  341. dx = ox - (double) ox1;
  342. // initial pixel value
  343. uint8_t *dst=p_dst + (y*p_dst_width+x)*CC;
  344. double color[CC];
  345. for(int i=0;i<CC;i++) {
  346. color[i]=0;
  347. }
  348. for ( int n = -1; n < 3; n++ ) {
  349. // get Y cooefficient
  350. k1 = _bicubic_interp_kernel( dy - (double) n );
  351. oy2 = oy1 + n;
  352. if ( oy2 < 0 )
  353. oy2 = 0;
  354. if ( oy2 > ymax )
  355. oy2 = ymax;
  356. for ( int m = -1; m < 3; m++ ) {
  357. // get X cooefficient
  358. k2 = k1 * _bicubic_interp_kernel( (double) m - dx );
  359. ox2 = ox1 + m;
  360. if ( ox2 < 0 )
  361. ox2 = 0;
  362. if ( ox2 > xmax )
  363. ox2 = xmax;
  364. // get pixel of original image
  365. const uint8_t *p = p_src + (oy2 * p_src_width + ox2)*CC;
  366. for(int i=0;i<CC;i++) {
  367. color[i]+=p[i]*k2;
  368. }
  369. }
  370. }
  371. for(int i=0;i<CC;i++) {
  372. dst[i]=CLAMP(Math::fast_ftoi(color[i]),0,255);
  373. }
  374. }
  375. }
  376. }
  377. template<int CC>
  378. 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) {
  379. enum {
  380. FRAC_BITS=8,
  381. FRAC_LEN=(1<<FRAC_BITS),
  382. FRAC_MASK=FRAC_LEN-1
  383. };
  384. for(uint32_t i=0;i<p_dst_height;i++) {
  385. uint32_t src_yofs_up_fp = (i*p_src_height*FRAC_LEN/p_dst_height);
  386. uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK;
  387. uint32_t src_yofs_up = src_yofs_up_fp >> FRAC_BITS;
  388. uint32_t src_yofs_down = (i+1)*p_src_height/p_dst_height;
  389. if (src_yofs_down>=p_src_height)
  390. src_yofs_down=p_src_height-1;
  391. //src_yofs_up*=CC;
  392. //src_yofs_down*=CC;
  393. uint32_t y_ofs_up = src_yofs_up * p_src_width * CC;
  394. uint32_t y_ofs_down = src_yofs_down * p_src_width * CC;
  395. for(uint32_t j=0;j<p_dst_width;j++) {
  396. uint32_t src_xofs_left_fp = (j*p_src_width*FRAC_LEN/p_dst_width);
  397. uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK;
  398. uint32_t src_xofs_left = src_xofs_left_fp >> FRAC_BITS;
  399. uint32_t src_xofs_right = (j+1)*p_src_width/p_dst_width;
  400. if (src_xofs_right>=p_src_width)
  401. src_xofs_right=p_src_width-1;
  402. src_xofs_left*=CC;
  403. src_xofs_right*=CC;
  404. for(uint32_t l=0;l<CC;l++) {
  405. uint32_t p00=p_src[y_ofs_up+src_xofs_left+l]<<FRAC_BITS;
  406. uint32_t p10=p_src[y_ofs_up+src_xofs_right+l]<<FRAC_BITS;
  407. uint32_t p01=p_src[y_ofs_down+src_xofs_left+l]<<FRAC_BITS;
  408. uint32_t p11=p_src[y_ofs_down+src_xofs_right+l]<<FRAC_BITS;
  409. uint32_t interp_up = p00+(((p10-p00)*src_xofs_frac)>>FRAC_BITS);
  410. uint32_t interp_down = p01+(((p11-p01)*src_xofs_frac)>>FRAC_BITS);
  411. uint32_t interp = interp_up+(((interp_down-interp_up)*src_yofs_frac)>>FRAC_BITS);
  412. interp>>=FRAC_BITS;
  413. p_dst[i*p_dst_width*CC+j*CC+l]=interp;
  414. }
  415. }
  416. }
  417. }
  418. template<int CC>
  419. 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) {
  420. for(uint32_t i=0;i<p_dst_height;i++) {
  421. uint32_t src_yofs = i*p_src_height/p_dst_height;
  422. uint32_t y_ofs = src_yofs * p_src_width * CC;
  423. for(uint32_t j=0;j<p_dst_width;j++) {
  424. uint32_t src_xofs = j*p_src_width/p_dst_width;
  425. src_xofs*=CC;
  426. for(uint32_t l=0;l<CC;l++) {
  427. uint32_t p=p_src[y_ofs+src_xofs+l];
  428. p_dst[i*p_dst_width*CC+j*CC+l]=p;
  429. }
  430. }
  431. }
  432. }
  433. void Image::resize_to_po2(bool p_square) {
  434. if (!_can_modify(format)) {
  435. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  436. ERR_FAIL();
  437. }
  438. int w = nearest_power_of_2(width);
  439. int h = nearest_power_of_2(height);
  440. if (w==width && h==height) {
  441. if (!p_square || w==h)
  442. return; //nothing to do
  443. }
  444. resize(w,h);
  445. }
  446. Image Image::resized( int p_width, int p_height, int p_interpolation ) {
  447. Image ret = *this;
  448. ret.resize(p_width, p_height, (Interpolation)p_interpolation);
  449. return ret;
  450. };
  451. void Image::resize( int p_width, int p_height, Interpolation p_interpolation ) {
  452. if (!_can_modify(format)) {
  453. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  454. ERR_FAIL();
  455. }
  456. ERR_FAIL_COND(p_width<=0);
  457. ERR_FAIL_COND(p_height<=0);
  458. ERR_FAIL_COND(p_width>MAX_WIDTH);
  459. ERR_FAIL_COND(p_height>MAX_HEIGHT);
  460. if (p_width==width && p_height==height)
  461. return;
  462. Image dst( p_width, p_height, 0, format );
  463. if (format==FORMAT_INDEXED)
  464. p_interpolation=INTERPOLATE_NEAREST;
  465. DVector<uint8_t>::Read r = data.read();
  466. const unsigned char*r_ptr=r.ptr();
  467. DVector<uint8_t>::Write w = dst.data.write();
  468. unsigned char*w_ptr=w.ptr();
  469. switch(p_interpolation) {
  470. case INTERPOLATE_NEAREST: {
  471. switch(get_format_pixel_size(format)) {
  472. case 1: _scale_nearest<1>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  473. case 2: _scale_nearest<2>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  474. case 3: _scale_nearest<3>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  475. case 4: _scale_nearest<4>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  476. }
  477. } break;
  478. case INTERPOLATE_BILINEAR: {
  479. switch(get_format_pixel_size(format)) {
  480. case 1: _scale_bilinear<1>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  481. case 2: _scale_bilinear<2>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  482. case 3: _scale_bilinear<3>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  483. case 4: _scale_bilinear<4>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  484. }
  485. } break;
  486. case INTERPOLATE_CUBIC: {
  487. switch(get_format_pixel_size(format)) {
  488. case 1: _scale_cubic<1>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  489. case 2: _scale_cubic<2>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  490. case 3: _scale_cubic<3>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  491. case 4: _scale_cubic<4>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  492. }
  493. } break;
  494. }
  495. r = DVector<uint8_t>::Read();
  496. w = DVector<uint8_t>::Write();
  497. if (mipmaps>0)
  498. dst.generate_mipmaps();
  499. *this=dst;
  500. }
  501. void Image::crop( int p_width, int p_height ) {
  502. if (!_can_modify(format)) {
  503. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  504. ERR_FAIL();
  505. }
  506. ERR_FAIL_COND(p_width<=0);
  507. ERR_FAIL_COND(p_height<=0);
  508. ERR_FAIL_COND(p_width>MAX_WIDTH);
  509. ERR_FAIL_COND(p_height>MAX_HEIGHT);
  510. /* to save memory, cropping should be done in-place, however, since this function
  511. will most likely either not be used much, or in critical areas, for now it wont, because
  512. it's a waste of time. */
  513. if (p_width==width && p_height==height)
  514. return;
  515. Image dst( p_width, p_height,0, format );
  516. for (int y=0;y<p_height;y++) {
  517. for (int x=0;x<p_width;x++) {
  518. Color col = (x>=width || y>=height)? Color() : get_pixel(x,y);
  519. dst.put_pixel(x,y,col);
  520. }
  521. }
  522. if (mipmaps>0)
  523. dst.generate_mipmaps();
  524. *this=dst;
  525. }
  526. void Image::flip_y() {
  527. if (!_can_modify(format)) {
  528. ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats.");
  529. ERR_FAIL();
  530. }
  531. bool gm=mipmaps;
  532. if (gm)
  533. clear_mipmaps();;
  534. for (int y=0;y<(height/2);y++) {
  535. for (int x=0;x<width;x++) {
  536. Color up = get_pixel(x,y);
  537. Color down = get_pixel(x,height-y-1);
  538. put_pixel(x,y,down);
  539. put_pixel(x,height-y-1,up);
  540. }
  541. }
  542. if (gm)
  543. generate_mipmaps();;
  544. }
  545. void Image::flip_x() {
  546. if (!_can_modify(format)) {
  547. ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats.");
  548. ERR_FAIL();
  549. }
  550. bool gm=mipmaps;
  551. if (gm)
  552. clear_mipmaps();;
  553. for (int y=0;y<(height/2);y++) {
  554. for (int x=0;x<width;x++) {
  555. Color up = get_pixel(x,y);
  556. Color down = get_pixel(width-x-1,y);
  557. put_pixel(x,y,down);
  558. put_pixel(width-x-1,y,up);
  559. }
  560. }
  561. if (gm)
  562. generate_mipmaps();;
  563. }
  564. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format,int &r_mipmaps,int p_mipmaps) {
  565. int size=0;
  566. int w=p_width;
  567. int h=p_height;
  568. int mm=0;
  569. int pixsize=get_format_pixel_size(p_format);
  570. int pixshift=get_format_pixel_rshift(p_format);
  571. int minw,minh;
  572. _get_format_min_data_size(p_format,minw,minh);
  573. switch(p_format) {
  574. case FORMAT_INDEXED: pixsize=1; size=256*3; break;
  575. case FORMAT_INDEXED_ALPHA: pixsize=1; size=256*4;break;
  576. default: {}
  577. } ;
  578. while(true) {
  579. int s = w*h;
  580. s*=pixsize;
  581. s>>=pixshift;
  582. size+=s;
  583. if (p_mipmaps>=0 && mm==p_mipmaps)
  584. break;
  585. if (p_mipmaps>=0) {
  586. w=MAX(minw,w>>1);
  587. h=MAX(minh,h>>1);
  588. } else {
  589. if (w==minw && h==minh)
  590. break;
  591. w=MAX(minw,w>>1);
  592. h=MAX(minh,h>>1);
  593. }
  594. mm++;
  595. };
  596. r_mipmaps=mm;
  597. return size;
  598. }
  599. bool Image::_can_modify(Format p_format) const {
  600. switch(p_format) {
  601. //these are OK
  602. case FORMAT_GRAYSCALE:
  603. case FORMAT_INTENSITY:
  604. case FORMAT_GRAYSCALE_ALPHA:
  605. case FORMAT_RGB:
  606. case FORMAT_RGBA:
  607. return true;
  608. default:
  609. return false;
  610. }
  611. return false;
  612. }
  613. template<int CC>
  614. static void _generate_po2_mipmap(const uint8_t* p_src, uint8_t* p_dst, uint32_t p_width, uint32_t p_height) {
  615. //fast power of 2 mipmap generation
  616. uint32_t dst_w = p_width >> 1;
  617. uint32_t dst_h = p_height >> 1;
  618. for(uint32_t i=0;i<dst_h;i++) {
  619. const uint8_t *rup_ptr = &p_src[i*2*p_width*CC];
  620. const uint8_t *rdown_ptr = rup_ptr + p_width * CC;
  621. uint8_t *dst_ptr = &p_dst[i*dst_w*CC];
  622. uint32_t count=dst_w;
  623. while(count--) {
  624. for(int j=0;j<CC;j++) {
  625. uint16_t val=0;
  626. val+=rup_ptr[j];
  627. val+=rup_ptr[j+CC];
  628. val+=rdown_ptr[j];
  629. val+=rdown_ptr[j+CC];
  630. dst_ptr[j]=val>>2;
  631. }
  632. dst_ptr+=CC;
  633. rup_ptr+=CC*2;
  634. rdown_ptr+=CC*2;
  635. }
  636. }
  637. }
  638. Error Image::generate_mipmaps(int p_mipmaps,bool p_keep_existing) {
  639. if (!_can_modify(format)) {
  640. ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats.");
  641. ERR_FAIL_V(ERR_UNAVAILABLE);
  642. }
  643. int from_mm=1;
  644. if (p_keep_existing) {
  645. from_mm=mipmaps+1;
  646. }
  647. int size = _get_dst_image_size(width,height,format,mipmaps,p_mipmaps);
  648. data.resize(size);
  649. DVector<uint8_t>::Write wp=data.write();
  650. if (nearest_power_of_2(width)==uint32_t(width) && nearest_power_of_2(height)==uint32_t(height)) {
  651. //use fast code for powers of 2
  652. int prev_ofs=0;
  653. int prev_h=height;
  654. int prev_w=width;
  655. for(int i=1;i<mipmaps;i++) {
  656. int ofs,w,h;
  657. _get_mipmap_offset_and_size(i,ofs, w,h);
  658. if (i>=from_mm) {
  659. switch(format) {
  660. case FORMAT_GRAYSCALE:
  661. case FORMAT_INTENSITY: _generate_po2_mipmap<1>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  662. case FORMAT_GRAYSCALE_ALPHA: _generate_po2_mipmap<2>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  663. case FORMAT_RGB: _generate_po2_mipmap<3>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  664. case FORMAT_RGBA: _generate_po2_mipmap<4>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  665. default: {}
  666. }
  667. }
  668. prev_ofs=ofs;
  669. prev_w=w;
  670. prev_h=h;
  671. }
  672. } else {
  673. //use slow code..
  674. //use bilinear filtered code for non powers of 2
  675. int prev_ofs=0;
  676. int prev_h=height;
  677. int prev_w=width;
  678. for(int i=1;i<mipmaps;i++) {
  679. int ofs,w,h;
  680. _get_mipmap_offset_and_size(i,ofs, w,h);
  681. if (i>=from_mm) {
  682. switch(format) {
  683. case FORMAT_GRAYSCALE:
  684. case FORMAT_INTENSITY: _scale_bilinear<1>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  685. case FORMAT_GRAYSCALE_ALPHA: _scale_bilinear<2>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  686. case FORMAT_RGB: _scale_bilinear<3>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  687. case FORMAT_RGBA: _scale_bilinear<4>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  688. default: {}
  689. }
  690. }
  691. prev_ofs=ofs;
  692. prev_w=w;
  693. prev_h=h;
  694. }
  695. }
  696. return OK;
  697. }
  698. void Image::clear_mipmaps() {
  699. if (mipmaps==0)
  700. return;
  701. if (format==FORMAT_CUSTOM) {
  702. ERR_EXPLAIN("Cannot clear mipmaps in indexed, compressed or custom image formats.");
  703. ERR_FAIL();
  704. }
  705. if (empty())
  706. return;
  707. int ofs,w,h;
  708. _get_mipmap_offset_and_size(1,ofs,w,h);
  709. int palsize = get_format_pallete_size(format);
  710. DVector<uint8_t> pallete;
  711. ERR_FAIL_COND(ofs+palsize > data.size()); //bug?
  712. if (palsize) {
  713. pallete.resize(palsize);
  714. DVector<uint8_t>::Read r = data.read();
  715. DVector<uint8_t>::Write w = pallete.write();
  716. copymem(&w[0],&r[data.size()-palsize],palsize);
  717. }
  718. data.resize(ofs+palsize);
  719. if (palsize) {
  720. DVector<uint8_t>::Read r = pallete.read();
  721. DVector<uint8_t>::Write w = data.write();
  722. copymem(&w[ofs],&r[0],palsize);
  723. }
  724. mipmaps=0;
  725. }
  726. void Image::make_normalmap(float p_height_scale) {
  727. if (!_can_modify(format)) {
  728. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  729. ERR_FAIL();
  730. }
  731. ERR_FAIL_COND( empty() );
  732. Image normalmap(width,height,0, FORMAT_RGB);
  733. /*
  734. for (int y=0;y<height;y++) {
  735. for (int x=0;x<width;x++) {
  736. float center=get_pixel(x,y).gray()/255.0;
  737. float up=(y>0)?get_pixel(x,y-1).gray()/255.0:center;
  738. float down=(y<(height-1))?get_pixel(x,y+1).gray()/255.0:center;
  739. float left=(x>0)?get_pixel(x-1,y).gray()/255.0:center;
  740. float right=(x<(width-1))?get_pixel(x+1,y).gray()/255.0:center;
  741. // uhm, how do i do this? ....
  742. Color result( (uint8_t)((normal.x+1.0)*127.0), (uint8_t)((normal.y+1.0)*127.0), (uint8_t)((normal.z+1.0)*127.0) );
  743. normalmap.put_pixel( x, y, result );
  744. }
  745. }
  746. */
  747. *this=normalmap;
  748. }
  749. bool Image::empty() const {
  750. return (data.size()==0);
  751. }
  752. DVector<uint8_t> Image::get_data() const {
  753. return data;
  754. }
  755. void Image::create(int p_width, int p_height, bool p_use_mipmaps,Format p_format) {
  756. int mm=0;
  757. int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_use_mipmaps?-1:0);
  758. data.resize( size );
  759. {
  760. DVector<uint8_t>::Write w= data.write();
  761. zeromem(w.ptr(),size);
  762. }
  763. width=p_width;
  764. height=p_height;
  765. mipmaps=mm;
  766. format=p_format;
  767. }
  768. void Image::create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data) {
  769. ERR_FAIL_INDEX(p_width-1,MAX_WIDTH);
  770. ERR_FAIL_INDEX(p_height-1,MAX_HEIGHT);
  771. if (p_format < FORMAT_CUSTOM) {
  772. int mm;
  773. int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_mipmaps);
  774. if (size!=p_data.size()) {
  775. ERR_EXPLAIN("Expected data size of "+itos(size)+" in Image::create()");
  776. ERR_FAIL_COND(p_data.size()!=size);
  777. }
  778. };
  779. height=p_height;
  780. width=p_width;
  781. format=p_format;
  782. data=p_data;
  783. mipmaps=p_mipmaps;
  784. }
  785. void Image::create( const char ** p_xpm ) {
  786. int size_width,size_height;
  787. int pixelchars=0;
  788. mipmaps=0;
  789. bool has_alpha=false;
  790. enum Status {
  791. READING_HEADER,
  792. READING_COLORS,
  793. READING_PIXELS,
  794. DONE
  795. };
  796. Status status = READING_HEADER;
  797. int line=0;
  798. HashMap<String,Color> colormap;
  799. int colormap_size;
  800. while (status!=DONE) {
  801. const char * line_ptr = p_xpm[line];
  802. switch (status) {
  803. case READING_HEADER: {
  804. String line_str=line_ptr;
  805. line_str.replace("\t"," ");
  806. size_width=line_str.get_slicec(' ',0).to_int();
  807. size_height=line_str.get_slicec(' ',1).to_int();
  808. colormap_size=line_str.get_slicec(' ',2).to_int();
  809. pixelchars=line_str.get_slicec(' ',3).to_int();
  810. ERR_FAIL_COND(colormap_size > 32766);
  811. ERR_FAIL_COND(pixelchars > 5);
  812. ERR_FAIL_COND(size_width > 32767);
  813. ERR_FAIL_COND(size_height > 32767);
  814. status=READING_COLORS;
  815. } break;
  816. case READING_COLORS: {
  817. String colorstring;
  818. for (int i=0;i<pixelchars;i++) {
  819. colorstring+=*line_ptr;
  820. line_ptr++;
  821. }
  822. //skip spaces
  823. while (*line_ptr==' ' || *line_ptr=='\t' || *line_ptr==0) {
  824. if (*line_ptr==0)
  825. break;
  826. line_ptr++;
  827. }
  828. if (*line_ptr=='c') {
  829. line_ptr++;
  830. while (*line_ptr==' ' || *line_ptr=='\t' || *line_ptr==0) {
  831. if (*line_ptr==0)
  832. break;
  833. line_ptr++;
  834. }
  835. if (*line_ptr=='#') {
  836. line_ptr++;
  837. uint8_t col_r;
  838. uint8_t col_g;
  839. uint8_t col_b;
  840. // uint8_t col_a=255;
  841. for (int i=0;i<6;i++) {
  842. char v = line_ptr[i];
  843. if (v>='0' && v<='9')
  844. v-='0';
  845. else if (v>='A' && v<='F')
  846. v=(v-'A')+10;
  847. else if (v>='a' && v<='f')
  848. v=(v-'a')+10;
  849. else
  850. break;
  851. switch(i) {
  852. case 0: col_r=v<<4; break;
  853. case 1: col_r|=v; break;
  854. case 2: col_g=v<<4; break;
  855. case 3: col_g|=v; break;
  856. case 4: col_b=v<<4; break;
  857. case 5: col_b|=v; break;
  858. };
  859. }
  860. // magenta mask
  861. if (col_r==255 && col_g==0 && col_b==255) {
  862. colormap[colorstring]=Color(0,0,0,0);
  863. has_alpha=true;
  864. } else {
  865. colormap[colorstring]=Color(col_r/255.0,col_g/255.0,col_b/255.0,1.0);
  866. }
  867. }
  868. }
  869. if (line==colormap_size) {
  870. status=READING_PIXELS;
  871. create(size_width,size_height,0,has_alpha?FORMAT_RGBA:FORMAT_RGB);
  872. }
  873. } break;
  874. case READING_PIXELS: {
  875. int y=line-colormap_size-1;
  876. for (int x=0;x<size_width;x++) {
  877. char pixelstr[6]={0,0,0,0,0,0};
  878. for (int i=0;i<pixelchars;i++)
  879. pixelstr[i]=line_ptr[x*pixelchars+i];
  880. Color *colorptr = colormap.getptr(pixelstr);
  881. ERR_FAIL_COND(!colorptr);
  882. put_pixel(x,y,*colorptr);
  883. }
  884. if (y==(size_height-1))
  885. status=DONE;
  886. } break;
  887. default:{}
  888. }
  889. line++;
  890. }
  891. }
  892. #define DETECT_ALPHA_MAX_TRESHOLD 254
  893. #define DETECT_ALPHA_MIN_TRESHOLD 2
  894. #define DETECT_ALPHA( m_value )\
  895. { \
  896. uint8_t value=m_value;\
  897. if (value<DETECT_ALPHA_MIN_TRESHOLD)\
  898. bit=true;\
  899. else if (value<DETECT_ALPHA_MAX_TRESHOLD) {\
  900. \
  901. detected=true;\
  902. break;\
  903. }\
  904. }
  905. #define DETECT_NON_ALPHA( m_value )\
  906. { \
  907. uint8_t value=m_value;\
  908. if (value>0) {\
  909. \
  910. detected=true;\
  911. break;\
  912. }\
  913. }
  914. bool Image::is_invisible() const {
  915. if (format==FORMAT_GRAYSCALE ||
  916. format==FORMAT_RGB ||
  917. format==FORMAT_INDEXED)
  918. return false;
  919. int len = data.size();
  920. if (len==0)
  921. return true;
  922. if (format >= FORMAT_YUV_422 && format <= FORMAT_YUV_444)
  923. return false;
  924. int w,h;
  925. _get_mipmap_offset_and_size(1,len,w,h);
  926. DVector<uint8_t>::Read r = data.read();
  927. const unsigned char *data_ptr=r.ptr();
  928. bool detected=false;
  929. switch(format) {
  930. case FORMAT_INTENSITY: {
  931. for(int i=0;i<len;i++) {
  932. DETECT_NON_ALPHA(data_ptr[i]);
  933. }
  934. } break;
  935. case FORMAT_GRAYSCALE_ALPHA: {
  936. for(int i=0;i<(len>>1);i++) {
  937. DETECT_NON_ALPHA(data_ptr[(i<<1)+1]);
  938. }
  939. } break;
  940. case FORMAT_RGBA: {
  941. for(int i=0;i<(len>>2);i++) {
  942. DETECT_NON_ALPHA(data_ptr[(i<<2)+3])
  943. }
  944. } break;
  945. case FORMAT_INDEXED: {
  946. return false;
  947. } break;
  948. case FORMAT_INDEXED_ALPHA: {
  949. return false;
  950. } break;
  951. case FORMAT_PVRTC2_ALPHA:
  952. case FORMAT_PVRTC4_ALPHA:
  953. case FORMAT_BC2:
  954. case FORMAT_BC3: {
  955. detected=true;
  956. } break;
  957. default: {}
  958. }
  959. return !detected;
  960. }
  961. Image::AlphaMode Image::detect_alpha() const {
  962. if (format==FORMAT_GRAYSCALE ||
  963. format==FORMAT_RGB ||
  964. format==FORMAT_INDEXED)
  965. return ALPHA_NONE;
  966. int len = data.size();
  967. if (len==0)
  968. return ALPHA_NONE;
  969. if (format >= FORMAT_YUV_422 && format <= FORMAT_YUV_444)
  970. return ALPHA_NONE;
  971. int w,h;
  972. _get_mipmap_offset_and_size(1,len,w,h);
  973. DVector<uint8_t>::Read r = data.read();
  974. const unsigned char *data_ptr=r.ptr();
  975. bool bit=false;
  976. bool detected=false;
  977. switch(format) {
  978. case FORMAT_INTENSITY: {
  979. for(int i=0;i<len;i++) {
  980. DETECT_ALPHA(data_ptr[i]);
  981. }
  982. } break;
  983. case FORMAT_GRAYSCALE_ALPHA: {
  984. for(int i=0;i<(len>>1);i++) {
  985. DETECT_ALPHA(data_ptr[(i<<1)+1]);
  986. }
  987. } break;
  988. case FORMAT_RGBA: {
  989. for(int i=0;i<(len>>2);i++) {
  990. DETECT_ALPHA(data_ptr[(i<<2)+3])
  991. }
  992. } break;
  993. case FORMAT_INDEXED: {
  994. return ALPHA_NONE;
  995. } break;
  996. case FORMAT_INDEXED_ALPHA: {
  997. return ALPHA_BLEND;
  998. } break;
  999. case FORMAT_PVRTC2_ALPHA:
  1000. case FORMAT_PVRTC4_ALPHA:
  1001. case FORMAT_BC2:
  1002. case FORMAT_BC3: {
  1003. detected=true;
  1004. } break;
  1005. default: {}
  1006. }
  1007. if (detected)
  1008. return ALPHA_BLEND;
  1009. else if (bit)
  1010. return ALPHA_BIT;
  1011. else
  1012. return ALPHA_NONE;
  1013. }
  1014. Error Image::load(const String& p_path) {
  1015. return ImageLoader::load_image(p_path, this);
  1016. }
  1017. Error Image::save_png(const String& p_path) {
  1018. if (save_png_func == NULL)
  1019. return ERR_UNAVAILABLE;
  1020. return save_png_func(p_path, *this);
  1021. };
  1022. bool Image::operator==(const Image& p_image) const {
  1023. if (data.size() == 0 && p_image.data.size() == 0)
  1024. return true;
  1025. DVector<uint8_t>::Read r = data.read();
  1026. DVector<uint8_t>::Read pr = p_image.data.read();
  1027. return r.ptr() == pr.ptr();
  1028. }
  1029. int Image::get_format_pixel_size(Format p_format) {
  1030. switch(p_format) {
  1031. case FORMAT_GRAYSCALE: {
  1032. return 1;
  1033. } break;
  1034. case FORMAT_INTENSITY: {
  1035. return 1;
  1036. } break;
  1037. case FORMAT_GRAYSCALE_ALPHA: {
  1038. return 2;
  1039. } break;
  1040. case FORMAT_RGB: {
  1041. return 3;
  1042. } break;
  1043. case FORMAT_RGBA: {
  1044. return 4;
  1045. } break;
  1046. case FORMAT_INDEXED: {
  1047. return 1;
  1048. } break;
  1049. case FORMAT_INDEXED_ALPHA: {
  1050. return 1;
  1051. } break;
  1052. case FORMAT_BC1:
  1053. case FORMAT_BC2:
  1054. case FORMAT_BC3:
  1055. case FORMAT_BC4:
  1056. case FORMAT_BC5: {
  1057. return 1;
  1058. } break;
  1059. case FORMAT_PVRTC2:
  1060. case FORMAT_PVRTC2_ALPHA: {
  1061. return 1;
  1062. } break;
  1063. case FORMAT_PVRTC4:
  1064. case FORMAT_PVRTC4_ALPHA: {
  1065. return 1;
  1066. } break;
  1067. case FORMAT_ATC:
  1068. case FORMAT_ATC_ALPHA_EXPLICIT:
  1069. case FORMAT_ATC_ALPHA_INTERPOLATED: {
  1070. return 1;
  1071. } break;
  1072. case FORMAT_ETC: {
  1073. return 1;
  1074. } break;
  1075. case FORMAT_YUV_422: {
  1076. return 2;
  1077. };
  1078. case FORMAT_YUV_444: {
  1079. return 3;
  1080. } break;
  1081. case FORMAT_CUSTOM: {
  1082. ERR_EXPLAIN("pixel size requested for custom image format, and it's unknown obviously");
  1083. ERR_FAIL_V(1);
  1084. } break;
  1085. default:{
  1086. ERR_EXPLAIN("Cannot obtain pixel size from this format");
  1087. ERR_FAIL_V(1);
  1088. }
  1089. }
  1090. return 0;
  1091. }
  1092. int Image::get_image_data_size(int p_width, int p_height, Format p_format,int p_mipmaps) {
  1093. int mm;
  1094. return _get_dst_image_size(p_width,p_height,p_format,mm,p_mipmaps);
  1095. }
  1096. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  1097. int mm;
  1098. _get_dst_image_size(p_width,p_height,p_format,mm,-1);
  1099. return mm;
  1100. }
  1101. void Image::_get_format_min_data_size(Format p_format,int &r_w, int &r_h) {
  1102. switch(p_format) {
  1103. case FORMAT_BC1:
  1104. case FORMAT_BC2:
  1105. case FORMAT_BC3:
  1106. case FORMAT_BC4:
  1107. case FORMAT_BC5: {
  1108. r_w=4;
  1109. r_h=4;
  1110. } break;
  1111. case FORMAT_PVRTC2:
  1112. case FORMAT_PVRTC2_ALPHA: {
  1113. r_w=16;
  1114. r_h=8;
  1115. } break;
  1116. case FORMAT_PVRTC4_ALPHA:
  1117. case FORMAT_PVRTC4: {
  1118. r_w=8;
  1119. r_h=8;
  1120. } break;
  1121. case FORMAT_ATC:
  1122. case FORMAT_ATC_ALPHA_EXPLICIT:
  1123. case FORMAT_ATC_ALPHA_INTERPOLATED: {
  1124. r_w=8;
  1125. r_h=8;
  1126. } break;
  1127. case FORMAT_ETC: {
  1128. r_w=4;
  1129. r_h=4;
  1130. } break;
  1131. default: {
  1132. r_w=1;
  1133. r_h=1;
  1134. } break;
  1135. }
  1136. }
  1137. int Image::get_format_pixel_rshift(Format p_format) {
  1138. if (p_format==FORMAT_BC1 || p_format==FORMAT_BC4 || p_format==FORMAT_ATC || p_format==FORMAT_PVRTC4 || p_format==FORMAT_PVRTC4_ALPHA || p_format==FORMAT_ETC)
  1139. return 1;
  1140. else if (p_format==FORMAT_PVRTC2 || p_format==FORMAT_PVRTC2_ALPHA)
  1141. return 2;
  1142. else
  1143. return 0;
  1144. }
  1145. int Image::get_format_pallete_size(Format p_format) {
  1146. switch(p_format) {
  1147. case FORMAT_GRAYSCALE: {
  1148. return 0;
  1149. } break;
  1150. case FORMAT_INTENSITY: {
  1151. return 0;
  1152. } break;
  1153. case FORMAT_GRAYSCALE_ALPHA: {
  1154. return 0;
  1155. } break;
  1156. case FORMAT_RGB: {
  1157. return 0;
  1158. } break;
  1159. case FORMAT_RGBA: {
  1160. return 0;
  1161. } break;
  1162. case FORMAT_INDEXED: {
  1163. return 3*256;
  1164. } break;
  1165. case FORMAT_INDEXED_ALPHA: {
  1166. return 4*256;
  1167. } break;
  1168. default:{}
  1169. }
  1170. return 0;
  1171. }
  1172. Error Image::_decompress_bc() {
  1173. print_line("decompressing bc");
  1174. int mm;
  1175. int size = _get_dst_image_size(width,height,FORMAT_RGBA,mm,mipmaps);
  1176. DVector<uint8_t> newdata;
  1177. newdata.resize(size);
  1178. DVector<uint8_t>::Write w = newdata.write();
  1179. DVector<uint8_t>::Read r = data.read();
  1180. int rofs=0;
  1181. int wofs=0;
  1182. int wd=width,ht=height;
  1183. for(int i=0;i<=mm;i++) {
  1184. switch(format) {
  1185. case FORMAT_BC1: {
  1186. int len = (wd*ht)/16;
  1187. uint8_t* dst=&w[wofs];
  1188. uint32_t ofs_table[16];
  1189. for(int x=0;x<4;x++) {
  1190. for(int y=0;y<4;y++) {
  1191. ofs_table[15-(y*4+(3-x))]=(x+y*wd)*4;
  1192. }
  1193. }
  1194. for(int j=0;j<len;j++) {
  1195. const uint8_t* src=&r[rofs+j*8];
  1196. uint16_t col_a=src[1];
  1197. col_a<<=8;
  1198. col_a|=src[0];
  1199. uint16_t col_b=src[3];
  1200. col_b<<=8;
  1201. col_b|=src[2];
  1202. uint8_t table[4][4]={
  1203. { (col_a>>11)<<3, ((col_a>>5)&0x3f)<<2, ((col_a)&0x1f)<<3, 255 },
  1204. { (col_b>>11)<<3, ((col_b>>5)&0x3f)<<2, ((col_b)&0x1f)<<3, 255 },
  1205. {0,0,0,255},
  1206. {0,0,0,255}
  1207. };
  1208. if (col_a<col_b) {
  1209. //punchrough
  1210. table[2][0]=(int(table[0][0])+int(table[1][0]))>>1;
  1211. table[2][1]=(int(table[0][1])+int(table[1][1]))>>1;
  1212. table[2][2]=(int(table[0][2])+int(table[1][2]))>>1;
  1213. table[3][3]=0; //premul alpha black
  1214. } else {
  1215. //gradient
  1216. table[2][0]=(int(table[0][0])*2+int(table[1][0]))/3;
  1217. table[2][1]=(int(table[0][1])*2+int(table[1][1]))/3;
  1218. table[2][2]=(int(table[0][2])*2+int(table[1][2]))/3;
  1219. table[3][0]=(int(table[0][0])+int(table[1][0])*2)/3;
  1220. table[3][1]=(int(table[0][1])+int(table[1][1])*2)/3;
  1221. table[3][2]=(int(table[0][2])+int(table[1][2])*2)/3;
  1222. }
  1223. uint32_t block=src[4];
  1224. block<<=8;
  1225. block|=src[5];
  1226. block<<=8;
  1227. block|=src[6];
  1228. block<<=8;
  1229. block|=src[7];
  1230. int y = (j/(wd/4))*4;
  1231. int x = (j%(wd/4))*4;
  1232. int pixofs = (y*wd+x)*4;
  1233. for(int k=0;k<16;k++) {
  1234. int idx = pixofs+ofs_table[k];
  1235. dst[idx+0]=table[block&0x3][0];
  1236. dst[idx+1]=table[block&0x3][1];
  1237. dst[idx+2]=table[block&0x3][2];
  1238. dst[idx+3]=table[block&0x3][3];
  1239. block>>=2;
  1240. }
  1241. }
  1242. rofs+=len*8;
  1243. wofs+=wd*ht*4;
  1244. wd/=2;
  1245. ht/=2;
  1246. } break;
  1247. case FORMAT_BC2: {
  1248. int len = (wd*ht)/16;
  1249. uint8_t* dst=&w[wofs];
  1250. uint32_t ofs_table[16];
  1251. for(int x=0;x<4;x++) {
  1252. for(int y=0;y<4;y++) {
  1253. ofs_table[15-(y*4+(3-x))]=(x+y*wd)*4;
  1254. }
  1255. }
  1256. for(int j=0;j<len;j++) {
  1257. const uint8_t* src=&r[rofs+j*16];
  1258. uint64_t ablock=src[1];
  1259. ablock<<=8;
  1260. ablock|=src[0];
  1261. ablock<<=8;
  1262. ablock|=src[3];
  1263. ablock<<=8;
  1264. ablock|=src[2];
  1265. ablock<<=8;
  1266. ablock|=src[5];
  1267. ablock<<=8;
  1268. ablock|=src[4];
  1269. ablock<<=8;
  1270. ablock|=src[7];
  1271. ablock<<=8;
  1272. ablock|=src[6];
  1273. uint16_t col_a=src[8+1];
  1274. col_a<<=8;
  1275. col_a|=src[8+0];
  1276. uint16_t col_b=src[8+3];
  1277. col_b<<=8;
  1278. col_b|=src[8+2];
  1279. uint8_t table[4][4]={
  1280. { (col_a>>11)<<3, ((col_a>>5)&0x3f)<<2, ((col_a)&0x1f)<<3, 255 },
  1281. { (col_b>>11)<<3, ((col_b>>5)&0x3f)<<2, ((col_b)&0x1f)<<3, 255 },
  1282. {0,0,0,255},
  1283. {0,0,0,255}
  1284. };
  1285. //always gradient
  1286. table[2][0]=(int(table[0][0])*2+int(table[1][0]))/3;
  1287. table[2][1]=(int(table[0][1])*2+int(table[1][1]))/3;
  1288. table[2][2]=(int(table[0][2])*2+int(table[1][2]))/3;
  1289. table[3][0]=(int(table[0][0])+int(table[1][0])*2)/3;
  1290. table[3][1]=(int(table[0][1])+int(table[1][1])*2)/3;
  1291. table[3][2]=(int(table[0][2])+int(table[1][2])*2)/3;
  1292. uint32_t block=src[4+8];
  1293. block<<=8;
  1294. block|=src[5+8];
  1295. block<<=8;
  1296. block|=src[6+8];
  1297. block<<=8;
  1298. block|=src[7+8];
  1299. int y = (j/(wd/4))*4;
  1300. int x = (j%(wd/4))*4;
  1301. int pixofs = (y*wd+x)*4;
  1302. for(int k=0;k<16;k++) {
  1303. uint8_t alpha = ablock&0xf;
  1304. alpha=int(alpha)*255/15; //right way for alpha
  1305. int idx = pixofs+ofs_table[k];
  1306. dst[idx+0]=table[block&0x3][0];
  1307. dst[idx+1]=table[block&0x3][1];
  1308. dst[idx+2]=table[block&0x3][2];
  1309. dst[idx+3]=alpha;
  1310. block>>=2;
  1311. ablock>>=4;
  1312. }
  1313. }
  1314. rofs+=len*16;
  1315. wofs+=wd*ht*4;
  1316. wd/=2;
  1317. ht/=2;
  1318. } break;
  1319. case FORMAT_BC3: {
  1320. int len = (wd*ht)/16;
  1321. uint8_t* dst=&w[wofs];
  1322. uint32_t ofs_table[16];
  1323. for(int x=0;x<4;x++) {
  1324. for(int y=0;y<4;y++) {
  1325. ofs_table[15-(y*4+(3-x))]=(x+y*wd)*4;
  1326. }
  1327. }
  1328. for(int j=0;j<len;j++) {
  1329. const uint8_t* src=&r[rofs+j*16];
  1330. uint8_t a_start=src[1];
  1331. uint8_t a_end=src[0];
  1332. uint64_t ablock=src[3];
  1333. ablock<<=8;
  1334. ablock|=src[2];
  1335. ablock<<=8;
  1336. ablock|=src[5];
  1337. ablock<<=8;
  1338. ablock|=src[4];
  1339. ablock<<=8;
  1340. ablock|=src[7];
  1341. ablock<<=8;
  1342. ablock|=src[6];
  1343. uint8_t atable[8];
  1344. if (a_start>a_end) {
  1345. atable[0]=(int(a_start)*7+int(a_end)*0)/7;
  1346. atable[1]=(int(a_start)*6+int(a_end)*1)/7;
  1347. atable[2]=(int(a_start)*5+int(a_end)*2)/7;
  1348. atable[3]=(int(a_start)*4+int(a_end)*3)/7;
  1349. atable[4]=(int(a_start)*3+int(a_end)*4)/7;
  1350. atable[5]=(int(a_start)*2+int(a_end)*5)/7;
  1351. atable[6]=(int(a_start)*1+int(a_end)*6)/7;
  1352. atable[7]=(int(a_start)*0+int(a_end)*7)/7;
  1353. } else {
  1354. atable[0]=(int(a_start)*5+int(a_end)*0)/5;
  1355. atable[1]=(int(a_start)*4+int(a_end)*1)/5;
  1356. atable[2]=(int(a_start)*3+int(a_end)*2)/5;
  1357. atable[3]=(int(a_start)*2+int(a_end)*3)/5;
  1358. atable[4]=(int(a_start)*1+int(a_end)*4)/5;
  1359. atable[5]=(int(a_start)*0+int(a_end)*5)/5;
  1360. atable[6]=0;
  1361. atable[7]=255;
  1362. }
  1363. uint16_t col_a=src[8+1];
  1364. col_a<<=8;
  1365. col_a|=src[8+0];
  1366. uint16_t col_b=src[8+3];
  1367. col_b<<=8;
  1368. col_b|=src[8+2];
  1369. uint8_t table[4][4]={
  1370. { (col_a>>11)<<3, ((col_a>>5)&0x3f)<<2, ((col_a)&0x1f)<<3, 255 },
  1371. { (col_b>>11)<<3, ((col_b>>5)&0x3f)<<2, ((col_b)&0x1f)<<3, 255 },
  1372. {0,0,0,255},
  1373. {0,0,0,255}
  1374. };
  1375. //always gradient
  1376. table[2][0]=(int(table[0][0])*2+int(table[1][0]))/3;
  1377. table[2][1]=(int(table[0][1])*2+int(table[1][1]))/3;
  1378. table[2][2]=(int(table[0][2])*2+int(table[1][2]))/3;
  1379. table[3][0]=(int(table[0][0])+int(table[1][0])*2)/3;
  1380. table[3][1]=(int(table[0][1])+int(table[1][1])*2)/3;
  1381. table[3][2]=(int(table[0][2])+int(table[1][2])*2)/3;
  1382. uint32_t block=src[4+8];
  1383. block<<=8;
  1384. block|=src[5+8];
  1385. block<<=8;
  1386. block|=src[6+8];
  1387. block<<=8;
  1388. block|=src[7+8];
  1389. int y = (j/(wd/4))*4;
  1390. int x = (j%(wd/4))*4;
  1391. int pixofs = (y*wd+x)*4;
  1392. for(int k=0;k<16;k++) {
  1393. uint8_t alpha = ablock&0x7;
  1394. int idx = pixofs+ofs_table[k];
  1395. dst[idx+0]=table[block&0x3][0];
  1396. dst[idx+1]=table[block&0x3][1];
  1397. dst[idx+2]=table[block&0x3][2];
  1398. dst[idx+3]=atable[alpha];
  1399. block>>=2;
  1400. ablock>>=3;
  1401. }
  1402. }
  1403. rofs+=len*16;
  1404. wofs+=wd*ht*4;
  1405. wd/=2;
  1406. ht/=2;
  1407. } break;
  1408. }
  1409. }
  1410. w=DVector<uint8_t>::Write();
  1411. r=DVector<uint8_t>::Read();
  1412. data=newdata;
  1413. format=FORMAT_RGBA;
  1414. return OK;
  1415. }
  1416. bool Image::is_compressed() const {
  1417. return format>=FORMAT_BC1;
  1418. }
  1419. Image Image::decompressed() const {
  1420. Image img=*this;
  1421. img.decompress();
  1422. return img;
  1423. }
  1424. Error Image::decompress() {
  1425. if (format>=FORMAT_BC1 && format<=FORMAT_BC5 )
  1426. _decompress_bc();//_image_decompress_bc(this);
  1427. else if (format>=FORMAT_PVRTC2 && format<=FORMAT_PVRTC4_ALPHA && _image_decompress_pvrtc)
  1428. _image_decompress_pvrtc(this);
  1429. else if (format==FORMAT_ETC && _image_decompress_etc)
  1430. _image_decompress_etc(this);
  1431. else
  1432. return ERR_UNAVAILABLE;
  1433. return OK;
  1434. }
  1435. Error Image::compress(CompressMode p_mode) {
  1436. switch(p_mode) {
  1437. case COMPRESS_BC: {
  1438. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1439. _image_compress_bc_func(this);
  1440. } break;
  1441. case COMPRESS_PVRTC2: {
  1442. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1443. _image_compress_pvrtc2_func(this);
  1444. } break;
  1445. case COMPRESS_PVRTC4: {
  1446. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1447. _image_compress_pvrtc4_func(this);
  1448. } break;
  1449. case COMPRESS_ETC: {
  1450. ERR_FAIL_COND_V(!_image_compress_etc_func, ERR_UNAVAILABLE);
  1451. _image_compress_etc_func(this);
  1452. } break;
  1453. }
  1454. return OK;
  1455. }
  1456. Image Image::compressed(int p_mode) {
  1457. Image ret = *this;
  1458. ret.compress((Image::CompressMode)p_mode);
  1459. return ret;
  1460. };
  1461. Image::Image(const char **p_xpm) {
  1462. width=0;
  1463. height=0;
  1464. mipmaps=0;
  1465. format=FORMAT_GRAYSCALE;
  1466. create(p_xpm);
  1467. }
  1468. Image::Image(int p_width, int p_height,bool p_use_mipmaps, Format p_format) {
  1469. width=0;
  1470. height=0;
  1471. mipmaps=0;
  1472. format=FORMAT_GRAYSCALE;
  1473. create(p_width,p_height,p_use_mipmaps,p_format);
  1474. }
  1475. Image::Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data) {
  1476. width=0;
  1477. height=0;
  1478. mipmaps=0;
  1479. format=FORMAT_GRAYSCALE;
  1480. create(p_width,p_height,p_mipmaps,p_format,p_data);
  1481. }
  1482. Image Image::brushed(const Image& p_src, const Image& p_brush, const Point2& p_dest) const {
  1483. Image img = *this;
  1484. img.brush_transfer(p_src,p_brush,p_dest);
  1485. return img;
  1486. }
  1487. Rect2 Image::get_used_rect() const {
  1488. if (format==FORMAT_GRAYSCALE ||
  1489. format==FORMAT_RGB ||
  1490. format==FORMAT_INDEXED || format>FORMAT_INDEXED_ALPHA)
  1491. return Rect2(Point2(),Size2(width,height));
  1492. int len = data.size();
  1493. if (len==0)
  1494. return Rect2();
  1495. int data_size = len;
  1496. DVector<uint8_t>::Read r = data.read();
  1497. const unsigned char *rptr=r.ptr();
  1498. int minx=0xFFFFFF,miny=0xFFFFFFF;
  1499. int maxx=-1,maxy=-1;
  1500. for(int i=0;i<width;i++) {
  1501. for(int j=0;j<height;j++) {
  1502. bool opaque = _get_pixel(i,j,rptr,data_size).a>2;
  1503. if (!opaque)
  1504. continue;
  1505. if (i>maxx)
  1506. maxx=i;
  1507. if (j>maxy)
  1508. maxy=j;
  1509. if (i<minx)
  1510. minx=i;
  1511. if (j<miny)
  1512. miny=j;
  1513. }
  1514. }
  1515. if (maxx==-1)
  1516. return Rect2();
  1517. else
  1518. return Rect2(minx,miny,maxx-minx+1,maxy-miny+1);
  1519. }
  1520. Image Image::get_rect(const Rect2& p_area) const {
  1521. Image img(p_area.size.x, p_area.size.y, mipmaps, format);
  1522. img.blit_rect(*this, p_area, Point2(0, 0));
  1523. return img;
  1524. };
  1525. void Image::brush_transfer(const Image& p_src, const Image& p_brush, const Point2& p_dest) {
  1526. ERR_FAIL_COND( width != p_src.width || height !=p_src.height);
  1527. int dst_data_size = data.size();
  1528. DVector<uint8_t>::Write wp = data.write();
  1529. unsigned char *dst_data_ptr=wp.ptr();
  1530. int src_data_size = p_src.data.size();
  1531. DVector<uint8_t>::Read rp = p_src.data.read();
  1532. const unsigned char *src_data_ptr=rp.ptr();
  1533. int brush_data_size = p_brush.data.size();
  1534. DVector<uint8_t>::Read bp = p_brush.data.read();
  1535. const unsigned char *src_brush_ptr=bp.ptr();
  1536. int bw = p_brush.get_width();
  1537. int bh = p_brush.get_height();
  1538. int dx=p_dest.x;
  1539. int dy=p_dest.y;
  1540. for(int i=dy;i<dy+bh;i++) {
  1541. if (i<0 || i >= height)
  1542. continue;
  1543. for(int j=dx;j<dx+bw;j++) {
  1544. if (j<0 || j>=width)
  1545. continue;
  1546. BColor src = p_src._get_pixel(j,i,src_data_ptr,src_data_size);
  1547. BColor dst = _get_pixel(j,i,dst_data_ptr,dst_data_size);
  1548. BColor brush = p_brush._get_pixel(j-dx,i-dy,src_brush_ptr,brush_data_size);
  1549. uint32_t mult = brush.r;
  1550. dst.r = dst.r + (((int32_t(src.r)-int32_t(dst.r))*mult)>>8);
  1551. dst.g = dst.g + (((int32_t(src.g)-int32_t(dst.g))*mult)>>8);
  1552. dst.b = dst.b + (((int32_t(src.b)-int32_t(dst.b))*mult)>>8);
  1553. dst.a = dst.a + (((int32_t(src.a)-int32_t(dst.a))*mult)>>8);
  1554. _put_pixel(j,i,dst,dst_data_ptr);
  1555. }
  1556. }
  1557. }
  1558. void Image::blit_rect(const Image& p_src, const Rect2& p_src_rect,const Point2& p_dest) {
  1559. int dsize=data.size();
  1560. int srcdsize=p_src.data.size();
  1561. ERR_FAIL_COND( dsize==0 );
  1562. ERR_FAIL_COND( srcdsize==0 );
  1563. Rect2 rrect = Rect2(0,0,p_src.width,p_src.height).clip(p_src_rect);
  1564. DVector<uint8_t>::Write wp = data.write();
  1565. unsigned char *dst_data_ptr=wp.ptr();
  1566. DVector<uint8_t>::Read rp = p_src.data.read();
  1567. const unsigned char *src_data_ptr=rp.ptr();
  1568. if ((format==FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA) && (p_src.format==FORMAT_INDEXED || p_src.format == FORMAT_INDEXED_ALPHA)) {
  1569. Point2i desti(p_dest.x, p_dest.y);
  1570. Point2i srci(rrect.pos.x, rrect.pos.y);
  1571. for(int i=0;i<rrect.size.y;i++) {
  1572. if (i<0 || i >= height)
  1573. continue;
  1574. for(int j=0;j<rrect.size.x;j++) {
  1575. if (j<0 || j>=width)
  1576. continue;
  1577. dst_data_ptr[width * (desti.y + i) + desti.x + j] = src_data_ptr[p_src.width * (srci.y+i) + srci.x+j];
  1578. }
  1579. }
  1580. } else {
  1581. for(int i=0;i<rrect.size.y;i++) {
  1582. if (i<0 || i >= height)
  1583. continue;
  1584. for(int j=0;j<rrect.size.x;j++) {
  1585. if (j<0 || j>=width)
  1586. continue;
  1587. _put_pixel(p_dest.x+j,p_dest.y+i,p_src._get_pixel(rrect.pos.x+j,rrect.pos.y+i,src_data_ptr,srcdsize),dst_data_ptr);
  1588. }
  1589. }
  1590. }
  1591. }
  1592. Image (*Image::_png_mem_loader_func)(const uint8_t*,int)=NULL;
  1593. void (*Image::_image_compress_bc_func)(Image *)=NULL;
  1594. void (*Image::_image_compress_pvrtc2_func)(Image *)=NULL;
  1595. void (*Image::_image_compress_pvrtc4_func)(Image *)=NULL;
  1596. void (*Image::_image_compress_etc_func)(Image *)=NULL;
  1597. void (*Image::_image_decompress_pvrtc)(Image *)=NULL;
  1598. void (*Image::_image_decompress_bc)(Image *)=NULL;
  1599. void (*Image::_image_decompress_etc)(Image *)=NULL;
  1600. DVector<uint8_t> (*Image::lossy_packer)(const Image& ,float )=NULL;
  1601. Image (*Image::lossy_unpacker)(const DVector<uint8_t>& )=NULL;
  1602. DVector<uint8_t> (*Image::lossless_packer)(const Image& )=NULL;
  1603. Image (*Image::lossless_unpacker)(const DVector<uint8_t>& )=NULL;
  1604. void Image::set_compress_bc_func(void (*p_compress_func)(Image *)) {
  1605. _image_compress_bc_func=p_compress_func;
  1606. }
  1607. void Image::normalmap_to_xy() {
  1608. convert(Image::FORMAT_RGBA);
  1609. {
  1610. int len = data.size()/4;
  1611. DVector<uint8_t>::Write wp = data.write();
  1612. unsigned char *data_ptr=wp.ptr();
  1613. for(int i=0;i<len;i++) {
  1614. data_ptr[(i<<2)+3]=data_ptr[(i<<2)+0]; //x to w
  1615. data_ptr[(i<<2)+0]=data_ptr[(i<<2)+1]; //y to xz
  1616. data_ptr[(i<<2)+2]=data_ptr[(i<<2)+1];
  1617. }
  1618. }
  1619. convert(Image::FORMAT_GRAYSCALE_ALPHA);
  1620. }
  1621. void Image::srgb_to_linear() {
  1622. if (data.size()==0)
  1623. return;
  1624. 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};
  1625. ERR_FAIL_COND( format!=FORMAT_RGB && format!=FORMAT_RGBA );
  1626. if (format==FORMAT_RGBA) {
  1627. int len = data.size()/4;
  1628. DVector<uint8_t>::Write wp = data.write();
  1629. unsigned char *data_ptr=wp.ptr();
  1630. for(int i=0;i<len;i++) {
  1631. data_ptr[(i<<2)+0]=srgb2lin[ data_ptr[(i<<2)+0] ];
  1632. data_ptr[(i<<2)+1]=srgb2lin[ data_ptr[(i<<2)+1] ];
  1633. data_ptr[(i<<2)+2]=srgb2lin[ data_ptr[(i<<2)+2] ];
  1634. }
  1635. } else if (format==FORMAT_RGB) {
  1636. int len = data.size()/3;
  1637. DVector<uint8_t>::Write wp = data.write();
  1638. unsigned char *data_ptr=wp.ptr();
  1639. for(int i=0;i<len;i++) {
  1640. data_ptr[(i*3)+0]=srgb2lin[ data_ptr[(i*3)+0] ];
  1641. data_ptr[(i*3)+1]=srgb2lin[ data_ptr[(i*3)+1] ];
  1642. data_ptr[(i*3)+2]=srgb2lin[ data_ptr[(i*3)+2] ];
  1643. }
  1644. }
  1645. }
  1646. void Image::premultiply_alpha() {
  1647. if (data.size()==0)
  1648. return;
  1649. if (format!=FORMAT_RGBA)
  1650. return; //not needed
  1651. DVector<uint8_t>::Write wp = data.write();
  1652. unsigned char *data_ptr=wp.ptr();
  1653. for(int i=0;i<height;i++) {
  1654. for(int j=0;j<width;j++) {
  1655. BColor bc = _get_pixel(j,i,data_ptr,0);
  1656. bc.r=(int(bc.r)*int(bc.a))>>8;
  1657. bc.g=(int(bc.g)*int(bc.a))>>8;
  1658. bc.b=(int(bc.b)*int(bc.a))>>8;
  1659. _put_pixel(j,i,bc,data_ptr);
  1660. }
  1661. }
  1662. }
  1663. void Image::fix_alpha_edges() {
  1664. if (data.size()==0)
  1665. return;
  1666. if (format!=FORMAT_RGBA)
  1667. return; //not needed
  1668. DVector<uint8_t> dcopy = data;
  1669. DVector<uint8_t>::Read rp = data.read();
  1670. const uint8_t *rptr=rp.ptr();
  1671. DVector<uint8_t>::Write wp = data.write();
  1672. unsigned char *data_ptr=wp.ptr();
  1673. const int max_radius=4;
  1674. const int alpha_treshold=20;
  1675. const int max_dist=0x7FFFFFFF;
  1676. for(int i=0;i<height;i++) {
  1677. for(int j=0;j<width;j++) {
  1678. BColor bc = _get_pixel(j,i,rptr,0);
  1679. if (bc.a>=alpha_treshold)
  1680. continue;
  1681. int closest_dist=max_dist;
  1682. BColor closest_color;
  1683. closest_color.a=bc.a;
  1684. int from_x = MAX(0,j-max_radius);
  1685. int to_x = MIN(width-1,j+max_radius);
  1686. int from_y = MAX(0,i-max_radius);
  1687. int to_y = MIN(height-1,i+max_radius);
  1688. for(int k=from_y;k<=to_y;k++) {
  1689. for(int l=from_x;l<=to_x;l++) {
  1690. int dy = i-k;
  1691. int dx = j-l;
  1692. int dist = dy*dy+dx*dx;
  1693. if (dist>=closest_dist)
  1694. continue;
  1695. const uint8_t * rp = &rptr[(k*width+l)<<2];
  1696. if (rp[3]<alpha_treshold)
  1697. continue;
  1698. closest_dist=dist;
  1699. closest_color.r=rp[0];
  1700. closest_color.g=rp[1];
  1701. closest_color.b=rp[2];
  1702. }
  1703. }
  1704. if (closest_dist!=max_dist)
  1705. _put_pixel(j,i,closest_color,data_ptr);
  1706. }
  1707. }
  1708. }
  1709. String Image::get_format_name(Format p_format) {
  1710. ERR_FAIL_INDEX_V(p_format,FORMAT_MAX,String());
  1711. return format_names[p_format];
  1712. }
  1713. Image::Image(const uint8_t* p_png,int p_len) {
  1714. width=0;
  1715. height=0;
  1716. mipmaps=0;
  1717. format=FORMAT_GRAYSCALE;
  1718. if (_png_mem_loader_func) {
  1719. *this = _png_mem_loader_func(p_png,p_len);
  1720. }
  1721. }
  1722. Image::Image() {
  1723. width=0;
  1724. height=0;
  1725. mipmaps=0;
  1726. format = FORMAT_GRAYSCALE;
  1727. }
  1728. Image::~Image() {
  1729. }