image.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /*************************************************************************/
  2. /* image.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. void Image::_put_pixel(int p_x,int p_y, const BColor& p_color, unsigned char *p_data) {
  36. _put_pixelw(p_x,p_y,width,p_color,p_data);
  37. }
  38. void Image::_put_pixelw(int p_x,int p_y, int p_width, const BColor& p_color, unsigned char *p_data) {
  39. int ofs=p_y*p_width+p_x;
  40. switch(format) {
  41. case FORMAT_GRAYSCALE: {
  42. p_data[ofs]=p_color.gray();
  43. } break;
  44. case FORMAT_INTENSITY: {
  45. p_data[ofs]=p_color.a;
  46. } break;
  47. case FORMAT_GRAYSCALE_ALPHA: {
  48. p_data[ofs*2]=p_color.gray();
  49. p_data[ofs*2+1]=p_color.a;
  50. } break;
  51. case FORMAT_RGB: {
  52. p_data[ofs*3+0]=p_color.r;
  53. p_data[ofs*3+1]=p_color.g;
  54. p_data[ofs*3+2]=p_color.b;
  55. } break;
  56. case FORMAT_RGBA: {
  57. p_data[ofs*4+0]=p_color.r;
  58. p_data[ofs*4+1]=p_color.g;
  59. p_data[ofs*4+2]=p_color.b;
  60. p_data[ofs*4+3]=p_color.a;
  61. } break;
  62. case FORMAT_INDEXED:
  63. case FORMAT_INDEXED_ALPHA: {
  64. ERR_FAIL();
  65. } break;
  66. default: {};
  67. }
  68. }
  69. void Image::_get_mipmap_offset_and_size(int p_mipmap,int &r_offset, int &r_width,int &r_height) const {
  70. int w=width;
  71. int h=height;
  72. int ofs=0;
  73. int pixel_size = get_format_pixel_size(format);
  74. int pixel_rshift = get_format_pixel_rshift(format);
  75. int minw,minh;
  76. _get_format_min_data_size(format,minw,minh);
  77. for(int i=0;i<p_mipmap;i++) {
  78. int s = w*h;
  79. s*=pixel_size;
  80. s>>=pixel_rshift;
  81. ofs+=s;
  82. w=MAX(minw,w>>1);
  83. h=MAX(minh,h>>1);
  84. }
  85. r_offset=ofs;
  86. r_width=w;
  87. r_height=h;
  88. }
  89. int Image::get_mipmap_offset(int p_mipmap) const {
  90. ERR_FAIL_INDEX_V(p_mipmap,(mipmaps+1),-1);
  91. int ofs,w,h;
  92. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  93. return ofs;
  94. }
  95. void Image::get_mipmap_offset_and_size(int p_mipmap,int &r_ofs, int &r_size) const {
  96. int ofs,w,h;
  97. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  98. int ofs2;
  99. _get_mipmap_offset_and_size(p_mipmap+1,ofs2,w,h);
  100. r_ofs=ofs;
  101. r_size=ofs2-ofs;
  102. }
  103. void Image::put_pixel(int p_x,int p_y, const Color& p_color,int p_mipmap){
  104. ERR_FAIL_INDEX(p_mipmap,mipmaps+1);
  105. int ofs,w,h;
  106. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  107. ERR_FAIL_INDEX(p_x,w);
  108. ERR_FAIL_INDEX(p_y,h);
  109. DVector<uint8_t>::Write wp = data.write();
  110. unsigned char *data_ptr=wp.ptr();
  111. _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]);
  112. }
  113. Image::BColor Image::_get_pixel(int p_x,int p_y,const unsigned char *p_data,int p_data_size) const{
  114. return _get_pixelw(p_x,p_y,width,p_data,p_data_size);
  115. }
  116. Image::BColor Image::_get_pixelw(int p_x,int p_y,int p_width,const unsigned char *p_data,int p_data_size) const{
  117. int ofs=p_y*p_width+p_x;
  118. BColor result(0,0,0,0);
  119. switch(format) {
  120. case FORMAT_GRAYSCALE: {
  121. result=BColor(p_data[ofs],p_data[ofs],p_data[ofs],255.0);
  122. } break;
  123. case FORMAT_INTENSITY: {
  124. result=BColor(255,255,255,p_data[ofs]);
  125. } break;
  126. case FORMAT_GRAYSCALE_ALPHA: {
  127. result=BColor(p_data[ofs*2],p_data[ofs*2],p_data[ofs*2],p_data[ofs*2+1]);
  128. } break;
  129. case FORMAT_RGB: {
  130. result=BColor(p_data[ofs*3],p_data[ofs*3+1],p_data[ofs*3+2]);
  131. } break;
  132. case FORMAT_RGBA: {
  133. result=BColor(p_data[ofs*4],p_data[ofs*4+1],p_data[ofs*4+2],p_data[ofs*4+3]);
  134. } break;
  135. case FORMAT_INDEXED_ALPHA: {
  136. int pitch = 4;
  137. const uint8_t* pal = &p_data[ p_data_size - pitch * 256 ];
  138. int idx = p_data[ofs];
  139. result=BColor(pal[idx * pitch + 0] , pal[idx * pitch + 1] , pal[idx * pitch + 2] , pal[idx * pitch + 3] );
  140. } break;
  141. case FORMAT_INDEXED: {
  142. int pitch = 3;
  143. const uint8_t* pal = &p_data[ p_data_size - pitch * 256 ];
  144. int idx = p_data[ofs];
  145. result=BColor(pal[idx * pitch + 0] , pal[idx * pitch + 1] , pal[idx * pitch + 2] ,255);
  146. } break;
  147. case FORMAT_YUV_422: {
  148. int y, u, v;
  149. if (p_x % 2) {
  150. const uint8_t* yp = &p_data[p_width * 2 * p_y + p_x * 2];
  151. u = *(yp-1);
  152. y = yp[0];
  153. v = yp[1];
  154. } else {
  155. const uint8_t* yp = &p_data[p_width * 2 * p_y + p_x * 2];
  156. y = yp[0];
  157. u = yp[1];
  158. v = yp[3];
  159. };
  160. int32_t r = 1.164 * (y - 16) + 1.596 * (v - 128);
  161. int32_t g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  162. int32_t b = 1.164 * (y - 16) + 2.018 * (u - 128);
  163. result = BColor(CLAMP(r, 0, 255), CLAMP(g, 0, 255), CLAMP(b, 0, 255));
  164. } break;
  165. case FORMAT_YUV_444: {
  166. uint8_t y, u, v;
  167. const uint8_t* yp = &p_data[p_width * 3 * p_y + p_x * 3];
  168. y = yp[0];
  169. u = yp[1];
  170. v = yp[2];
  171. int32_t r = 1.164 * (y - 16) + 1.596 * (v - 128);
  172. int32_t g = 1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128);
  173. int32_t b = 1.164 * (y - 16) + 2.018 * (u - 128);
  174. result = BColor(CLAMP(r, 0, 255), CLAMP(g, 0, 255), CLAMP(b, 0, 255));
  175. } break;
  176. default:{}
  177. }
  178. return result;
  179. }
  180. void Image::put_indexed_pixel(int p_x, int p_y, uint8_t p_idx,int p_mipmap) {
  181. ERR_FAIL_COND(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA);
  182. ERR_FAIL_INDEX(p_mipmap,mipmaps+1);
  183. int ofs,w,h;
  184. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  185. ERR_FAIL_INDEX(p_x,w);
  186. ERR_FAIL_INDEX(p_y,h);
  187. data.set(ofs + p_y * w + p_x, p_idx);
  188. };
  189. uint8_t Image::get_indexed_pixel(int p_x, int p_y,int p_mipmap) const {
  190. ERR_FAIL_COND_V(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA, 0);
  191. ERR_FAIL_INDEX_V(p_mipmap,mipmaps+1,0);
  192. int ofs,w,h;
  193. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  194. ERR_FAIL_INDEX_V(p_x,w,0);
  195. ERR_FAIL_INDEX_V(p_y,h,0);
  196. return data[ofs + p_y * w + p_x];
  197. };
  198. void Image::set_pallete(const DVector<uint8_t>& p_data) {
  199. int len = p_data.size();
  200. ERR_FAIL_COND(format != FORMAT_INDEXED && format != FORMAT_INDEXED_ALPHA);
  201. ERR_FAIL_COND(format == FORMAT_INDEXED && len!=(256*3));
  202. ERR_FAIL_COND(format == FORMAT_INDEXED_ALPHA && len!=(256*4));
  203. int ofs,w,h;
  204. _get_mipmap_offset_and_size(mipmaps+1,ofs,w,h);
  205. int pal_ofs = ofs;
  206. data.resize(pal_ofs + p_data.size());
  207. DVector<uint8_t>::Write wp = data.write();
  208. unsigned char *dst=wp.ptr() + pal_ofs;
  209. DVector<uint8_t>::Read r = data.read();
  210. const unsigned char *src=r.ptr();
  211. copymem(dst, src, len);
  212. };
  213. int Image::get_width() const {
  214. return width;
  215. }
  216. int Image::get_height() const{
  217. return height;
  218. }
  219. int Image::get_mipmaps() const {
  220. return mipmaps;
  221. }
  222. Color Image::get_pixel(int p_x,int p_y,int p_mipmap) const {
  223. ERR_FAIL_INDEX_V(p_mipmap,mipmaps+1,Color());
  224. int ofs,w,h;
  225. _get_mipmap_offset_and_size(p_mipmap,ofs,w,h);
  226. ERR_FAIL_INDEX_V(p_x,w,Color());
  227. ERR_FAIL_INDEX_V(p_y,h,Color());
  228. int len = data.size();
  229. DVector<uint8_t>::Read r = data.read();
  230. const unsigned char*data_ptr=r.ptr();
  231. BColor c = _get_pixelw(p_x,p_y,w,&data_ptr[ofs],len);
  232. return Color( c.r/255.0,c.g/255.0,c.b/255.0,c.a/255.0 );
  233. }
  234. void Image::convert( Format p_new_format ){
  235. if (data.size()==0)
  236. return;
  237. if (p_new_format==format)
  238. return;
  239. if (format>=FORMAT_BC1 || p_new_format>=FORMAT_BC1) {
  240. ERR_EXPLAIN("Cannot convert to <-> from compressed/custom image formats (for now).");
  241. ERR_FAIL();
  242. }
  243. if (p_new_format==FORMAT_INDEXED || p_new_format==FORMAT_INDEXED_ALPHA) {
  244. return;
  245. }
  246. Image new_img(width,height,0,p_new_format);
  247. int len=data.size();
  248. DVector<uint8_t>::Read r = data.read();
  249. DVector<uint8_t>::Write w = new_img.data.write();
  250. const uint8_t *rptr = r.ptr();
  251. uint8_t *wptr = w.ptr();
  252. if (p_new_format==FORMAT_RGBA && format==FORMAT_INDEXED_ALPHA) {
  253. //optimized unquantized form
  254. int dataend = len-256*4;
  255. const uint32_t *palpos = (const uint32_t*)&rptr[dataend];
  256. uint32_t *dst32 = (uint32_t *)wptr;
  257. for(int i=0;i<dataend;i++)
  258. dst32[i]=palpos[rptr[i]]; //since this is read/write, endianness is not a problem
  259. } else {
  260. //this is temporary, must find a faster way to do it.
  261. for(int i=0;i<width;i++)
  262. for(int j=0;j<height;j++)
  263. new_img._put_pixel(i,j,_get_pixel(i,j,rptr,len),wptr);
  264. }
  265. r = DVector<uint8_t>::Read();
  266. w = DVector<uint8_t>::Write();
  267. bool gen_mipmaps=mipmaps>0;
  268. *this=new_img;
  269. if (gen_mipmaps)
  270. generate_mipmaps();
  271. }
  272. Image::Format Image::get_format() const{
  273. return format;
  274. }
  275. template<int CC>
  276. 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) {
  277. enum {
  278. FRAC_BITS=8,
  279. FRAC_LEN=(1<<FRAC_BITS),
  280. FRAC_MASK=FRAC_LEN-1
  281. };
  282. for(uint32_t i=0;i<p_dst_height;i++) {
  283. uint32_t src_yofs_up_fp = (i*p_src_height*FRAC_LEN/p_dst_height);
  284. uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK;
  285. uint32_t src_yofs_up = src_yofs_up_fp >> FRAC_BITS;
  286. uint32_t src_yofs_down = (i+1)*p_src_height/p_dst_height;
  287. if (src_yofs_down>=p_src_height)
  288. src_yofs_down=p_src_height-1;
  289. //src_yofs_up*=CC;
  290. //src_yofs_down*=CC;
  291. uint32_t y_ofs_up = src_yofs_up * p_src_width * CC;
  292. uint32_t y_ofs_down = src_yofs_down * p_src_width * CC;
  293. for(uint32_t j=0;j<p_dst_width;j++) {
  294. uint32_t src_xofs_left_fp = (j*p_src_width*FRAC_LEN/p_dst_width);
  295. uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK;
  296. uint32_t src_xofs_left = src_xofs_left_fp >> FRAC_BITS;
  297. uint32_t src_xofs_right = (j+1)*p_src_width/p_dst_width;
  298. if (src_xofs_right>=p_src_width)
  299. src_xofs_right=p_src_width-1;
  300. src_xofs_left*=CC;
  301. src_xofs_right*=CC;
  302. for(uint32_t l=0;l<CC;l++) {
  303. uint32_t p00=p_src[y_ofs_up+src_xofs_left+l]<<FRAC_BITS;
  304. uint32_t p10=p_src[y_ofs_up+src_xofs_right+l]<<FRAC_BITS;
  305. uint32_t p01=p_src[y_ofs_down+src_xofs_left+l]<<FRAC_BITS;
  306. uint32_t p11=p_src[y_ofs_down+src_xofs_right+l]<<FRAC_BITS;
  307. uint32_t interp_up = p00+(((p10-p00)*src_xofs_frac)>>FRAC_BITS);
  308. uint32_t interp_down = p01+(((p11-p01)*src_xofs_frac)>>FRAC_BITS);
  309. uint32_t interp = interp_up+(((interp_down-interp_up)*src_yofs_frac)>>FRAC_BITS);
  310. interp>>=FRAC_BITS;
  311. p_dst[i*p_dst_width*CC+j*CC+l]=interp;
  312. }
  313. }
  314. }
  315. }
  316. template<int CC>
  317. 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) {
  318. for(uint32_t i=0;i<p_dst_height;i++) {
  319. uint32_t src_yofs = i*p_src_height/p_dst_height;
  320. uint32_t y_ofs = src_yofs * p_src_width * CC;
  321. for(uint32_t j=0;j<p_dst_width;j++) {
  322. uint32_t src_xofs = j*p_src_width/p_dst_width;
  323. src_xofs*=CC;
  324. for(uint32_t l=0;l<CC;l++) {
  325. uint32_t p=p_src[y_ofs+src_xofs+l];
  326. p_dst[i*p_dst_width*CC+j*CC+l]=p;
  327. }
  328. }
  329. }
  330. }
  331. void Image::resize_to_po2(bool p_square) {
  332. if (!_can_modify(format)) {
  333. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  334. ERR_FAIL();
  335. }
  336. int w = nearest_power_of_2(width);
  337. int h = nearest_power_of_2(height);
  338. if (w==width && h==height) {
  339. if (!p_square || w==h)
  340. return; //nothing to do
  341. }
  342. resize(w,h);
  343. }
  344. Image Image::resized( int p_width, int p_height, int p_interpolation ) {
  345. Image ret = *this;
  346. ret.resize(p_width, p_height, (Interpolation)p_interpolation);
  347. return ret;
  348. };
  349. void Image::resize( int p_width, int p_height, Interpolation p_interpolation ) {
  350. if (!_can_modify(format)) {
  351. ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats.");
  352. ERR_FAIL();
  353. }
  354. ERR_FAIL_COND(p_width<=0);
  355. ERR_FAIL_COND(p_height<=0);
  356. ERR_FAIL_COND(p_width>MAX_WIDTH);
  357. ERR_FAIL_COND(p_height>MAX_HEIGHT);
  358. if (p_width==width && p_height==height)
  359. return;
  360. Image dst( p_width, p_height, 0, format );
  361. if (format==FORMAT_INDEXED)
  362. p_interpolation=INTERPOLATE_NEAREST;
  363. DVector<uint8_t>::Read r = data.read();
  364. const unsigned char*r_ptr=r.ptr();
  365. DVector<uint8_t>::Write w = dst.data.write();
  366. unsigned char*w_ptr=w.ptr();
  367. switch(p_interpolation) {
  368. case INTERPOLATE_NEAREST: {
  369. switch(get_format_pixel_size(format)) {
  370. case 1: _scale_nearest<1>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  371. case 2: _scale_nearest<2>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  372. case 3: _scale_nearest<3>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  373. case 4: _scale_nearest<4>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  374. }
  375. } break;
  376. case INTERPOLATE_BILINEAR: {
  377. switch(get_format_pixel_size(format)) {
  378. case 1: _scale_bilinear<1>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  379. case 2: _scale_bilinear<2>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  380. case 3: _scale_bilinear<3>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  381. case 4: _scale_bilinear<4>(r_ptr,w_ptr,width,height,p_width,p_height); break;
  382. }
  383. } break;
  384. }
  385. r = DVector<uint8_t>::Read();
  386. w = DVector<uint8_t>::Write();
  387. if (mipmaps>0)
  388. dst.generate_mipmaps();
  389. *this=dst;
  390. }
  391. void Image::crop( int p_width, int p_height ) {
  392. if (!_can_modify(format)) {
  393. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  394. ERR_FAIL();
  395. }
  396. ERR_FAIL_COND(p_width<=0);
  397. ERR_FAIL_COND(p_height<=0);
  398. ERR_FAIL_COND(p_width>MAX_WIDTH);
  399. ERR_FAIL_COND(p_height>MAX_HEIGHT);
  400. /* to save memory, cropping should be done in-place, however, since this function
  401. will most likely either not be used much, or in critical areas, for now it wont, because
  402. it's a waste of time. */
  403. if (p_width==width && p_height==height)
  404. return;
  405. Image dst( p_width, p_height,0, format );
  406. for (int y=0;y<p_height;y++) {
  407. for (int x=0;x<p_width;x++) {
  408. Color col = (x>=width || y>=height)? Color() : get_pixel(x,y);
  409. dst.put_pixel(x,y,col);
  410. }
  411. }
  412. if (mipmaps>0)
  413. dst.generate_mipmaps();
  414. *this=dst;
  415. }
  416. void Image::flip_y() {
  417. if (!_can_modify(format)) {
  418. ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats.");
  419. ERR_FAIL();
  420. }
  421. bool gm=mipmaps;
  422. if (gm)
  423. clear_mipmaps();;
  424. for (int y=0;y<(height/2);y++) {
  425. for (int x=0;x<width;x++) {
  426. Color up = get_pixel(x,y);
  427. Color down = get_pixel(x,height-y-1);
  428. put_pixel(x,y,down);
  429. put_pixel(x,height-y-1,up);
  430. }
  431. }
  432. if (gm)
  433. generate_mipmaps();;
  434. }
  435. void Image::flip_x() {
  436. if (!_can_modify(format)) {
  437. ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats.");
  438. ERR_FAIL();
  439. }
  440. bool gm=mipmaps;
  441. if (gm)
  442. clear_mipmaps();;
  443. for (int y=0;y<(height/2);y++) {
  444. for (int x=0;x<width;x++) {
  445. Color up = get_pixel(x,y);
  446. Color down = get_pixel(width-x-1,y);
  447. put_pixel(x,y,down);
  448. put_pixel(width-x-1,y,up);
  449. }
  450. }
  451. if (gm)
  452. generate_mipmaps();;
  453. }
  454. int Image::_get_dst_image_size(int p_width, int p_height, Format p_format,int &r_mipmaps,int p_mipmaps) {
  455. int size=0;
  456. int w=p_width;
  457. int h=p_height;
  458. int mm=0;
  459. int pixsize=get_format_pixel_size(p_format);
  460. int pixshift=get_format_pixel_rshift(p_format);
  461. int minw,minh;
  462. _get_format_min_data_size(p_format,minw,minh);
  463. switch(p_format) {
  464. case FORMAT_INDEXED: pixsize=1; size=256*3; break;
  465. case FORMAT_INDEXED_ALPHA: pixsize=1; size=256*4;break;
  466. default: {}
  467. } ;
  468. while(true) {
  469. int s = w*h;
  470. s*=pixsize;
  471. s>>=pixshift;
  472. size+=s;
  473. if (p_mipmaps>=0 && mm==p_mipmaps)
  474. break;
  475. if (p_mipmaps>=0) {
  476. w=MAX(minw,w>>1);
  477. h=MAX(minh,h>>1);
  478. } else {
  479. if (w==minw && h==minh)
  480. break;
  481. w=MAX(minw,w>>1);
  482. h=MAX(minh,h>>1);
  483. }
  484. mm++;
  485. };
  486. r_mipmaps=mm;
  487. return size;
  488. }
  489. bool Image::_can_modify(Format p_format) const {
  490. switch(p_format) {
  491. //these are OK
  492. case FORMAT_GRAYSCALE:
  493. case FORMAT_INTENSITY:
  494. case FORMAT_GRAYSCALE_ALPHA:
  495. case FORMAT_RGB:
  496. case FORMAT_RGBA:
  497. return true;
  498. default:
  499. return false;
  500. }
  501. return false;
  502. }
  503. template<int CC>
  504. static void _generate_po2_mipmap(const uint8_t* p_src, uint8_t* p_dst, uint32_t p_width, uint32_t p_height) {
  505. //fast power of 2 mipmap generation
  506. uint32_t dst_w = p_width >> 1;
  507. uint32_t dst_h = p_height >> 1;
  508. for(uint32_t i=0;i<dst_h;i++) {
  509. const uint8_t *rup_ptr = &p_src[i*2*p_width*CC];
  510. const uint8_t *rdown_ptr = rup_ptr + p_width * CC;
  511. uint8_t *dst_ptr = &p_dst[i*dst_w*CC];
  512. uint32_t count=dst_w;
  513. while(count--) {
  514. for(int j=0;j<CC;j++) {
  515. uint16_t val=0;
  516. val+=rup_ptr[j];
  517. val+=rup_ptr[j+CC];
  518. val+=rdown_ptr[j];
  519. val+=rdown_ptr[j+CC];
  520. dst_ptr[j]=val>>2;
  521. }
  522. dst_ptr+=CC;
  523. rup_ptr+=CC*2;
  524. rdown_ptr+=CC*2;
  525. }
  526. }
  527. }
  528. Error Image::generate_mipmaps(int p_mipmaps,bool p_keep_existing) {
  529. if (!_can_modify(format)) {
  530. ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats.");
  531. ERR_FAIL_V(ERR_UNAVAILABLE);
  532. }
  533. int from_mm=1;
  534. if (p_keep_existing) {
  535. from_mm=mipmaps+1;
  536. }
  537. int size = _get_dst_image_size(width,height,format,mipmaps,p_mipmaps);
  538. data.resize(size);
  539. DVector<uint8_t>::Write wp=data.write();
  540. if (nearest_power_of_2(width)==uint32_t(width) && nearest_power_of_2(height)==uint32_t(height)) {
  541. //use fast code for powers of 2
  542. int prev_ofs=0;
  543. int prev_h=height;
  544. int prev_w=width;
  545. for(int i=1;i<mipmaps;i++) {
  546. int ofs,w,h;
  547. _get_mipmap_offset_and_size(i,ofs, w,h);
  548. if (i>=from_mm) {
  549. switch(format) {
  550. case FORMAT_GRAYSCALE:
  551. case FORMAT_INTENSITY: _generate_po2_mipmap<1>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  552. case FORMAT_GRAYSCALE_ALPHA: _generate_po2_mipmap<2>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  553. case FORMAT_RGB: _generate_po2_mipmap<3>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  554. case FORMAT_RGBA: _generate_po2_mipmap<4>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break;
  555. default: {}
  556. }
  557. }
  558. prev_ofs=ofs;
  559. prev_w=w;
  560. prev_h=h;
  561. }
  562. } else {
  563. //use slow code..
  564. //use bilinear filtered code for non powers of 2
  565. int prev_ofs=0;
  566. int prev_h=height;
  567. int prev_w=width;
  568. for(int i=1;i<mipmaps;i++) {
  569. int ofs,w,h;
  570. _get_mipmap_offset_and_size(i,ofs, w,h);
  571. if (i>=from_mm) {
  572. switch(format) {
  573. case FORMAT_GRAYSCALE:
  574. case FORMAT_INTENSITY: _scale_bilinear<1>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  575. case FORMAT_GRAYSCALE_ALPHA: _scale_bilinear<2>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  576. case FORMAT_RGB: _scale_bilinear<3>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  577. case FORMAT_RGBA: _scale_bilinear<4>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break;
  578. default: {}
  579. }
  580. }
  581. prev_ofs=ofs;
  582. prev_w=w;
  583. prev_h=h;
  584. }
  585. }
  586. return OK;
  587. }
  588. void Image::clear_mipmaps() {
  589. if (mipmaps==0)
  590. return;
  591. if (format==FORMAT_CUSTOM) {
  592. ERR_EXPLAIN("Cannot clear mipmaps in indexed, compressed or custom image formats.");
  593. ERR_FAIL();
  594. }
  595. if (empty())
  596. return;
  597. int ofs,w,h;
  598. _get_mipmap_offset_and_size(1,ofs,w,h);
  599. int palsize = get_format_pallete_size(format);
  600. DVector<uint8_t> pallete;
  601. ERR_FAIL_COND(ofs+palsize > data.size()); //bug?
  602. if (palsize) {
  603. pallete.resize(palsize);
  604. DVector<uint8_t>::Read r = data.read();
  605. DVector<uint8_t>::Write w = pallete.write();
  606. copymem(&w[0],&r[data.size()-palsize],palsize);
  607. }
  608. data.resize(ofs+palsize);
  609. if (palsize) {
  610. DVector<uint8_t>::Read r = pallete.read();
  611. DVector<uint8_t>::Write w = data.write();
  612. copymem(&w[ofs],&r[0],palsize);
  613. }
  614. mipmaps=0;
  615. }
  616. void Image::make_normalmap(float p_height_scale) {
  617. if (!_can_modify(format)) {
  618. ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats.");
  619. ERR_FAIL();
  620. }
  621. ERR_FAIL_COND( empty() );
  622. Image normalmap(width,height,0, FORMAT_RGB);
  623. /*
  624. for (int y=0;y<height;y++) {
  625. for (int x=0;x<width;x++) {
  626. float center=get_pixel(x,y).gray()/255.0;
  627. float up=(y>0)?get_pixel(x,y-1).gray()/255.0:center;
  628. float down=(y<(height-1))?get_pixel(x,y+1).gray()/255.0:center;
  629. float left=(x>0)?get_pixel(x-1,y).gray()/255.0:center;
  630. float right=(x<(width-1))?get_pixel(x+1,y).gray()/255.0:center;
  631. // uhm, how do i do this? ....
  632. 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) );
  633. normalmap.put_pixel( x, y, result );
  634. }
  635. }
  636. */
  637. *this=normalmap;
  638. }
  639. bool Image::empty() const {
  640. return (data.size()==0);
  641. }
  642. DVector<uint8_t> Image::get_data() const {
  643. return data;
  644. }
  645. void Image::create(int p_width, int p_height, bool p_use_mipmaps,Format p_format) {
  646. int mm=0;
  647. int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_use_mipmaps?-1:0);
  648. data.resize( size );
  649. {
  650. DVector<uint8_t>::Write w= data.write();
  651. zeromem(w.ptr(),size);
  652. }
  653. width=p_width;
  654. height=p_height;
  655. mipmaps=mm;
  656. format=p_format;
  657. }
  658. void Image::create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data) {
  659. ERR_FAIL_INDEX(p_width-1,MAX_WIDTH);
  660. ERR_FAIL_INDEX(p_height-1,MAX_HEIGHT);
  661. if (p_format < FORMAT_CUSTOM) {
  662. int mm;
  663. int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_mipmaps);
  664. if (size!=p_data.size()) {
  665. ERR_EXPLAIN("Expected data size of "+itos(size)+" in Image::create()");
  666. ERR_FAIL_COND(p_data.size()!=size);
  667. }
  668. };
  669. height=p_height;
  670. width=p_width;
  671. format=p_format;
  672. data=p_data;
  673. mipmaps=p_mipmaps;
  674. }
  675. void Image::create( const char ** p_xpm ) {
  676. int size_width,size_height;
  677. int pixelchars=0;
  678. mipmaps=0;
  679. bool has_alpha=false;
  680. enum Status {
  681. READING_HEADER,
  682. READING_COLORS,
  683. READING_PIXELS,
  684. DONE
  685. };
  686. Status status = READING_HEADER;
  687. int line=0;
  688. HashMap<String,Color> colormap;
  689. int colormap_size;
  690. while (status!=DONE) {
  691. const char * line_ptr = p_xpm[line];
  692. switch (status) {
  693. case READING_HEADER: {
  694. String line_str=line_ptr;
  695. line_str.replace("\t"," ");
  696. size_width=line_str.get_slice(" ",0).to_int();
  697. size_height=line_str.get_slice(" ",1).to_int();
  698. colormap_size=line_str.get_slice(" ",2).to_int();
  699. pixelchars=line_str.get_slice(" ",3).to_int();
  700. ERR_FAIL_COND(colormap_size > 32766);
  701. ERR_FAIL_COND(pixelchars > 5);
  702. ERR_FAIL_COND(size_width > 32767);
  703. ERR_FAIL_COND(size_height > 32767);
  704. status=READING_COLORS;
  705. } break;
  706. case READING_COLORS: {
  707. String colorstring;
  708. for (int i=0;i<pixelchars;i++) {
  709. colorstring+=*line_ptr;
  710. line_ptr++;
  711. }
  712. //skip spaces
  713. while (*line_ptr==' ' || *line_ptr=='\t' || *line_ptr==0) {
  714. if (*line_ptr==0)
  715. break;
  716. line_ptr++;
  717. }
  718. if (*line_ptr=='c') {
  719. line_ptr++;
  720. while (*line_ptr==' ' || *line_ptr=='\t' || *line_ptr==0) {
  721. if (*line_ptr==0)
  722. break;
  723. line_ptr++;
  724. }
  725. if (*line_ptr=='#') {
  726. line_ptr++;
  727. uint8_t col_r;
  728. uint8_t col_g;
  729. uint8_t col_b;
  730. // uint8_t col_a=255;
  731. for (int i=0;i<6;i++) {
  732. char v = line_ptr[i];
  733. if (v>='0' && v<='9')
  734. v-='0';
  735. else if (v>='A' && v<='F')
  736. v=(v-'A')+10;
  737. else if (v>='a' && v<='f')
  738. v=(v-'a')+10;
  739. else
  740. break;
  741. switch(i) {
  742. case 0: col_r=v<<4; break;
  743. case 1: col_r|=v; break;
  744. case 2: col_g=v<<4; break;
  745. case 3: col_g|=v; break;
  746. case 4: col_b=v<<4; break;
  747. case 5: col_b|=v; break;
  748. };
  749. }
  750. // magenta mask
  751. if (col_r==255 && col_g==0 && col_b==255) {
  752. colormap[colorstring]=Color(0,0,0,0);
  753. has_alpha=true;
  754. } else {
  755. colormap[colorstring]=Color(col_r/255.0,col_g/255.0,col_b/255.0,1.0);
  756. }
  757. }
  758. }
  759. if (line==colormap_size) {
  760. status=READING_PIXELS;
  761. create(size_width,size_height,0,has_alpha?FORMAT_RGBA:FORMAT_RGB);
  762. }
  763. } break;
  764. case READING_PIXELS: {
  765. int y=line-colormap_size-1;
  766. for (int x=0;x<size_width;x++) {
  767. char pixelstr[6]={0,0,0,0,0,0};
  768. for (int i=0;i<pixelchars;i++)
  769. pixelstr[i]=line_ptr[x*pixelchars+i];
  770. Color *colorptr = colormap.getptr(pixelstr);
  771. ERR_FAIL_COND(!colorptr);
  772. put_pixel(x,y,*colorptr);
  773. }
  774. if (y==(size_height-1))
  775. status=DONE;
  776. } break;
  777. default:{}
  778. }
  779. line++;
  780. }
  781. }
  782. #define DETECT_ALPHA_MAX_TRESHOLD 254
  783. #define DETECT_ALPHA_MIN_TRESHOLD 2
  784. #define DETECT_ALPHA( m_value )\
  785. { \
  786. uint8_t value=m_value;\
  787. if (value<DETECT_ALPHA_MIN_TRESHOLD)\
  788. bit=true;\
  789. else if (value<DETECT_ALPHA_MAX_TRESHOLD) {\
  790. \
  791. detected=true;\
  792. break;\
  793. }\
  794. }
  795. Image::AlphaMode Image::detect_alpha() const {
  796. if (format==FORMAT_GRAYSCALE ||
  797. format==FORMAT_RGB ||
  798. format==FORMAT_INDEXED)
  799. return ALPHA_NONE;
  800. int len = data.size();
  801. if (len==0)
  802. return ALPHA_NONE;
  803. if (format >= FORMAT_YUV_422 && format <= FORMAT_YUV_444)
  804. return ALPHA_NONE;
  805. int w,h;
  806. _get_mipmap_offset_and_size(1,len,w,h);
  807. DVector<uint8_t>::Read r = data.read();
  808. const unsigned char *data_ptr=r.ptr();
  809. bool bit=false;
  810. bool detected=false;
  811. switch(format) {
  812. case FORMAT_INTENSITY: {
  813. for(int i=0;i<len;i++) {
  814. DETECT_ALPHA(data_ptr[i]);
  815. }
  816. } break;
  817. case FORMAT_GRAYSCALE_ALPHA: {
  818. for(int i=0;i<(len>>1);i++) {
  819. DETECT_ALPHA(data_ptr[(i<<1)+1]);
  820. }
  821. } break;
  822. case FORMAT_RGBA: {
  823. for(int i=0;i<(len>>2);i++) {
  824. DETECT_ALPHA(data_ptr[(i<<2)+3])
  825. }
  826. } break;
  827. case FORMAT_INDEXED: {
  828. return ALPHA_NONE;
  829. } break;
  830. case FORMAT_INDEXED_ALPHA: {
  831. return ALPHA_BLEND;
  832. } break;
  833. case FORMAT_PVRTC2_ALPHA:
  834. case FORMAT_PVRTC4_ALPHA:
  835. case FORMAT_BC2:
  836. case FORMAT_BC3: {
  837. detected=true;
  838. } break;
  839. default: {}
  840. }
  841. if (detected)
  842. return ALPHA_BLEND;
  843. else if (bit)
  844. return ALPHA_BIT;
  845. else
  846. return ALPHA_NONE;
  847. }
  848. Error Image::load(const String& p_path) {
  849. return ImageLoader::load_image(p_path, this);
  850. }
  851. bool Image::operator==(const Image& p_image) const {
  852. if (data.size() == 0 && p_image.data.size() == 0)
  853. return true;
  854. DVector<uint8_t>::Read r = data.read();
  855. DVector<uint8_t>::Read pr = p_image.data.read();
  856. return r.ptr() == pr.ptr();
  857. }
  858. int Image::get_format_pixel_size(Format p_format) {
  859. switch(p_format) {
  860. case FORMAT_GRAYSCALE: {
  861. return 1;
  862. } break;
  863. case FORMAT_INTENSITY: {
  864. return 1;
  865. } break;
  866. case FORMAT_GRAYSCALE_ALPHA: {
  867. return 2;
  868. } break;
  869. case FORMAT_RGB: {
  870. return 3;
  871. } break;
  872. case FORMAT_RGBA: {
  873. return 4;
  874. } break;
  875. case FORMAT_INDEXED: {
  876. return 1;
  877. } break;
  878. case FORMAT_INDEXED_ALPHA: {
  879. return 1;
  880. } break;
  881. case FORMAT_BC1:
  882. case FORMAT_BC2:
  883. case FORMAT_BC3:
  884. case FORMAT_BC4:
  885. case FORMAT_BC5: {
  886. return 1;
  887. } break;
  888. case FORMAT_PVRTC2:
  889. case FORMAT_PVRTC2_ALPHA: {
  890. return 1;
  891. } break;
  892. case FORMAT_PVRTC4:
  893. case FORMAT_PVRTC4_ALPHA: {
  894. return 1;
  895. } break;
  896. case FORMAT_ETC: {
  897. return 1;
  898. } break;
  899. case FORMAT_YUV_422: {
  900. return 2;
  901. };
  902. case FORMAT_YUV_444: {
  903. return 3;
  904. } break;
  905. case FORMAT_CUSTOM: {
  906. ERR_EXPLAIN("pixel size requested for custom image format, and it's unknown obviously");
  907. ERR_FAIL_V(1);
  908. } break;
  909. default:{
  910. ERR_EXPLAIN("Cannot obtain pixel size from this format");
  911. ERR_FAIL_V(1);
  912. }
  913. }
  914. return 0;
  915. }
  916. int Image::get_image_data_size(int p_width, int p_height, Format p_format,int p_mipmaps) {
  917. int mm;
  918. return _get_dst_image_size(p_width,p_height,p_format,mm,p_mipmaps);
  919. }
  920. int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
  921. int mm;
  922. _get_dst_image_size(p_width,p_height,p_format,mm,-1);
  923. return mm;
  924. }
  925. void Image::_get_format_min_data_size(Format p_format,int &r_w, int &r_h) {
  926. switch(p_format) {
  927. case FORMAT_BC1:
  928. case FORMAT_BC2:
  929. case FORMAT_BC3:
  930. case FORMAT_BC4:
  931. case FORMAT_BC5: {
  932. r_w=4;
  933. r_h=4;
  934. } break;
  935. case FORMAT_PVRTC2:
  936. case FORMAT_PVRTC2_ALPHA: {
  937. r_w=16;
  938. r_h=8;
  939. } break;
  940. case FORMAT_PVRTC4_ALPHA:
  941. case FORMAT_PVRTC4: {
  942. r_w=8;
  943. r_h=8;
  944. } break;
  945. case FORMAT_ETC: {
  946. r_w=4;
  947. r_h=4;
  948. } break;
  949. default: {
  950. r_w=1;
  951. r_h=1;
  952. } break;
  953. }
  954. }
  955. int Image::get_format_pixel_rshift(Format p_format) {
  956. if (p_format==FORMAT_BC1 || p_format==FORMAT_BC4 || p_format==FORMAT_PVRTC4 || p_format==FORMAT_PVRTC4_ALPHA || p_format==FORMAT_ETC)
  957. return 1;
  958. else if (p_format==FORMAT_PVRTC2 || p_format==FORMAT_PVRTC2_ALPHA)
  959. return 2;
  960. else
  961. return 0;
  962. }
  963. int Image::get_format_pallete_size(Format p_format) {
  964. switch(p_format) {
  965. case FORMAT_GRAYSCALE: {
  966. return 0;
  967. } break;
  968. case FORMAT_INTENSITY: {
  969. return 0;
  970. } break;
  971. case FORMAT_GRAYSCALE_ALPHA: {
  972. return 0;
  973. } break;
  974. case FORMAT_RGB: {
  975. return 0;
  976. } break;
  977. case FORMAT_RGBA: {
  978. return 0;
  979. } break;
  980. case FORMAT_INDEXED: {
  981. return 3*256;
  982. } break;
  983. case FORMAT_INDEXED_ALPHA: {
  984. return 4*256;
  985. } break;
  986. default:{}
  987. }
  988. return 0;
  989. }
  990. void Image::decompress() {
  991. if (format>=FORMAT_BC1 && format<=FORMAT_BC5 && _image_decompress_bc)
  992. _image_decompress_bc(this);
  993. if (format>=FORMAT_PVRTC2 && format<=FORMAT_PVRTC4_ALPHA && _image_decompress_pvrtc)
  994. _image_decompress_pvrtc(this);
  995. if (format==FORMAT_ETC && _image_decompress_etc)
  996. _image_decompress_etc(this);
  997. }
  998. Error Image::compress(CompressMode p_mode) {
  999. switch(p_mode) {
  1000. case COMPRESS_BC: {
  1001. ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
  1002. _image_compress_bc_func(this);
  1003. } break;
  1004. case COMPRESS_PVRTC2: {
  1005. ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE);
  1006. _image_compress_pvrtc2_func(this);
  1007. } break;
  1008. case COMPRESS_PVRTC4: {
  1009. ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE);
  1010. _image_compress_pvrtc4_func(this);
  1011. } break;
  1012. case COMPRESS_ETC: {
  1013. ERR_FAIL_COND_V(!_image_compress_etc_func, ERR_UNAVAILABLE);
  1014. _image_compress_etc_func(this);
  1015. } break;
  1016. }
  1017. return OK;
  1018. }
  1019. Image Image::compressed(int p_mode) {
  1020. Image ret = *this;
  1021. ret.compress((Image::CompressMode)p_mode);
  1022. return ret;
  1023. };
  1024. Image::Image(const char **p_xpm) {
  1025. width=0;
  1026. height=0;
  1027. mipmaps=0;
  1028. format=FORMAT_GRAYSCALE;
  1029. create(p_xpm);
  1030. }
  1031. Image::Image(int p_width, int p_height,bool p_use_mipmaps, Format p_format) {
  1032. width=0;
  1033. height=0;
  1034. mipmaps=0;
  1035. format=FORMAT_GRAYSCALE;
  1036. create(p_width,p_height,p_use_mipmaps,p_format);
  1037. }
  1038. Image::Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data) {
  1039. width=0;
  1040. height=0;
  1041. mipmaps=0;
  1042. format=FORMAT_GRAYSCALE;
  1043. create(p_width,p_height,p_mipmaps,p_format,p_data);
  1044. }
  1045. Image Image::brushed(const Image& p_src, const Image& p_brush, const Point2& p_dest) const {
  1046. Image img = *this;
  1047. img.brush_transfer(p_src,p_brush,p_dest);
  1048. return img;
  1049. }
  1050. Rect2 Image::get_used_rect() const {
  1051. if (format==FORMAT_GRAYSCALE ||
  1052. format==FORMAT_RGB ||
  1053. format==FORMAT_INDEXED || format>FORMAT_INDEXED_ALPHA)
  1054. return Rect2(Point2(),Size2(width,height));
  1055. int len = data.size();
  1056. if (len==0)
  1057. return Rect2();
  1058. int data_size = len;
  1059. DVector<uint8_t>::Read r = data.read();
  1060. const unsigned char *rptr=r.ptr();
  1061. int minx=0xFFFFFF,miny=0xFFFFFFF;
  1062. int maxx=-1,maxy=-1;
  1063. for(int i=0;i<width;i++) {
  1064. for(int j=0;j<height;j++) {
  1065. bool opaque = _get_pixel(i,j,rptr,data_size).a>2;
  1066. if (!opaque)
  1067. continue;
  1068. if (i>maxx)
  1069. maxx=i;
  1070. if (j>maxy)
  1071. maxy=j;
  1072. if (i<minx)
  1073. minx=i;
  1074. if (j<miny)
  1075. miny=j;
  1076. }
  1077. }
  1078. if (maxx==-1)
  1079. return Rect2();
  1080. else
  1081. return Rect2(minx,miny,maxx-minx+1,maxy-miny+1);
  1082. }
  1083. Image Image::get_rect(const Rect2& p_area) const {
  1084. Image img(p_area.size.x, p_area.size.y, mipmaps, format);
  1085. img.blit_rect(*this, p_area, Point2(0, 0));
  1086. return img;
  1087. };
  1088. void Image::brush_transfer(const Image& p_src, const Image& p_brush, const Point2& p_dest) {
  1089. ERR_FAIL_COND( width != p_src.width || height !=p_src.height);
  1090. int dst_data_size = data.size();
  1091. DVector<uint8_t>::Write wp = data.write();
  1092. unsigned char *dst_data_ptr=wp.ptr();
  1093. int src_data_size = p_src.data.size();
  1094. DVector<uint8_t>::Read rp = p_src.data.read();
  1095. const unsigned char *src_data_ptr=rp.ptr();
  1096. int brush_data_size = p_brush.data.size();
  1097. DVector<uint8_t>::Read bp = p_brush.data.read();
  1098. const unsigned char *src_brush_ptr=bp.ptr();
  1099. int bw = p_brush.get_width();
  1100. int bh = p_brush.get_height();
  1101. int dx=p_dest.x;
  1102. int dy=p_dest.y;
  1103. for(int i=dy;i<dy+bh;i++) {
  1104. if (i<0 || i >= height)
  1105. continue;
  1106. for(int j=dx;j<dx+bw;j++) {
  1107. if (j<0 || j>=width)
  1108. continue;
  1109. BColor src = p_src._get_pixel(j,i,src_data_ptr,src_data_size);
  1110. BColor dst = _get_pixel(j,i,dst_data_ptr,dst_data_size);
  1111. BColor brush = p_brush._get_pixel(j-dx,i-dy,src_brush_ptr,brush_data_size);
  1112. uint32_t mult = brush.r;
  1113. dst.r = dst.r + (((int32_t(src.r)-int32_t(dst.r))*mult)>>8);
  1114. dst.g = dst.g + (((int32_t(src.g)-int32_t(dst.g))*mult)>>8);
  1115. dst.b = dst.b + (((int32_t(src.b)-int32_t(dst.b))*mult)>>8);
  1116. dst.a = dst.a + (((int32_t(src.a)-int32_t(dst.a))*mult)>>8);
  1117. _put_pixel(j,i,dst,dst_data_ptr);
  1118. }
  1119. }
  1120. }
  1121. void Image::blit_rect(const Image& p_src, const Rect2& p_src_rect,const Point2& p_dest) {
  1122. int dsize=data.size();
  1123. int srcdsize=p_src.data.size();
  1124. ERR_FAIL_COND( dsize==0 );
  1125. ERR_FAIL_COND( srcdsize==0 );
  1126. Rect2 rrect = Rect2(0,0,p_src.width,p_src.height).clip(p_src_rect);
  1127. DVector<uint8_t>::Write wp = data.write();
  1128. unsigned char *dst_data_ptr=wp.ptr();
  1129. DVector<uint8_t>::Read rp = p_src.data.read();
  1130. const unsigned char *src_data_ptr=rp.ptr();
  1131. if ((format==FORMAT_INDEXED || format == FORMAT_INDEXED_ALPHA) && (p_src.format==FORMAT_INDEXED || p_src.format == FORMAT_INDEXED_ALPHA)) {
  1132. Point2i desti(p_dest.x, p_dest.y);
  1133. Point2i srci(rrect.pos.x, rrect.pos.y);
  1134. for(int i=0;i<rrect.size.y;i++) {
  1135. if (i<0 || i >= height)
  1136. continue;
  1137. for(int j=0;j<rrect.size.x;j++) {
  1138. if (j<0 || j>=width)
  1139. continue;
  1140. dst_data_ptr[width * (desti.y + i) + desti.x + j] = src_data_ptr[p_src.width * (srci.y+i) + srci.x+j];
  1141. }
  1142. }
  1143. } else {
  1144. for(int i=0;i<rrect.size.y;i++) {
  1145. if (i<0 || i >= height)
  1146. continue;
  1147. for(int j=0;j<rrect.size.x;j++) {
  1148. if (j<0 || j>=width)
  1149. continue;
  1150. _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);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. Image (*Image::_png_mem_loader_func)(const uint8_t*)=NULL;
  1156. void (*Image::_image_compress_bc_func)(Image *)=NULL;
  1157. void (*Image::_image_compress_pvrtc2_func)(Image *)=NULL;
  1158. void (*Image::_image_compress_pvrtc4_func)(Image *)=NULL;
  1159. void (*Image::_image_compress_etc_func)(Image *)=NULL;
  1160. void (*Image::_image_decompress_pvrtc)(Image *)=NULL;
  1161. void (*Image::_image_decompress_bc)(Image *)=NULL;
  1162. void (*Image::_image_decompress_etc)(Image *)=NULL;
  1163. DVector<uint8_t> (*Image::lossy_packer)(const Image& ,float )=NULL;
  1164. Image (*Image::lossy_unpacker)(const DVector<uint8_t>& )=NULL;
  1165. DVector<uint8_t> (*Image::lossless_packer)(const Image& )=NULL;
  1166. Image (*Image::lossless_unpacker)(const DVector<uint8_t>& )=NULL;
  1167. void Image::set_compress_bc_func(void (*p_compress_func)(Image *)) {
  1168. _image_compress_bc_func=p_compress_func;
  1169. }
  1170. void Image::fix_alpha_edges() {
  1171. if (data.size()==0)
  1172. return;
  1173. if (format!=FORMAT_RGBA)
  1174. return; //not needed
  1175. DVector<uint8_t> dcopy = data;
  1176. DVector<uint8_t>::Read rp = data.read();
  1177. const uint8_t *rptr=rp.ptr();
  1178. DVector<uint8_t>::Write wp = data.write();
  1179. unsigned char *data_ptr=wp.ptr();
  1180. const int max_radius=4;
  1181. const int alpha_treshold=20;
  1182. const int max_dist=0x7FFFFFFF;
  1183. for(int i=0;i<height;i++) {
  1184. for(int j=0;j<width;j++) {
  1185. BColor bc = _get_pixel(j,i,rptr,0);
  1186. if (bc.a>=alpha_treshold)
  1187. continue;
  1188. int closest_dist=max_dist;
  1189. BColor closest_color;
  1190. closest_color.a=bc.a;
  1191. int from_x = MAX(0,j-max_radius);
  1192. int to_x = MIN(width-1,j+max_radius);
  1193. int from_y = MAX(0,i-max_radius);
  1194. int to_y = MIN(height-1,i+max_radius);
  1195. for(int k=from_y;k<=to_y;k++) {
  1196. for(int l=from_x;l<=to_x;l++) {
  1197. int dy = i-k;
  1198. int dx = j-l;
  1199. int dist = dy*dy+dx*dx;
  1200. if (dist>=closest_dist)
  1201. continue;
  1202. const uint8_t * rp = &rptr[(k*width+l)<<2];
  1203. if (rp[3]<alpha_treshold)
  1204. continue;
  1205. closest_dist=dist;
  1206. closest_color.r=rp[0];
  1207. closest_color.g=rp[1];
  1208. closest_color.b=rp[2];
  1209. }
  1210. }
  1211. if (closest_dist!=max_dist)
  1212. _put_pixel(j,i,closest_color,data_ptr);
  1213. }
  1214. }
  1215. }
  1216. Image::Image(const uint8_t* p_png) {
  1217. width=0;
  1218. height=0;
  1219. mipmaps=0;
  1220. format=FORMAT_GRAYSCALE;
  1221. if (_png_mem_loader_func) {
  1222. *this = _png_mem_loader_func(p_png);
  1223. }
  1224. }
  1225. Image::Image() {
  1226. width=0;
  1227. height=0;
  1228. mipmaps=0;
  1229. format = FORMAT_GRAYSCALE;
  1230. }
  1231. Image::~Image() {
  1232. }