image.cpp 53 KB

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