image.cpp 47 KB

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