image.cpp 47 KB

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