/*************************************************************************/ /* image.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "image.h" #include "hash_map.h" #include "core/io/image_loader.h" #include "core/os/copymem.h" #include "hq2x.h" #include "print_string.h" #include const char* Image::format_names[Image::FORMAT_MAX]={ "Lum8", //luminance "LumAlpha8", //luminance-alpha "Red8", "RedGreen", "RGB8", "RGBA8", "RGB565", //16 bit "RGBA4444", "RGBA5551", "RFloat", //float "RGFloat", "RGBFloat", "RGBAFloat", "RHalf", //half float "RGHalf", "RGBHalf", "RGBAHalf", "DXT1", //s3tc "DXT3", "DXT5", "ATI1", "ATI2", "BPTC_RGBA", "BPTC_RGBF", "BPTC_RGBFU", "PVRTC2", //pvrtc "PVRTC2A", "PVRTC4", "PVRTC4A", "ETC", //etc1 "ETC2_R11", //etc2 "ETC2_R11S", //signed", NOT srgb. "ETC2_RG11", "ETC2_RG11S", "ETC2_RGB8", "ETC2_RGBA8", "ETC2_RGB8A1", }; SavePNGFunc Image::save_png_func = NULL; void Image::_put_pixelb(int p_x,int p_y, uint32_t p_pixelsize,uint8_t *p_dst,const uint8_t *p_src) { uint32_t ofs=(p_y*width+p_x)*p_pixelsize; for(uint32_t i=0;i>=pixel_rshift; ofs+=s; w=MAX(minw,w>>1); h=MAX(minh,h>>1); } r_offset=ofs; r_width=w; r_height=h; } int Image::get_mipmap_offset(int p_mipmap) const { ERR_FAIL_INDEX_V(p_mipmap,(mipmaps+1),-1); int ofs,w,h; _get_mipmap_offset_and_size(p_mipmap,ofs,w,h); return ofs; } void Image::get_mipmap_offset_and_size(int p_mipmap,int &r_ofs, int &r_size) const { int ofs,w,h; _get_mipmap_offset_and_size(p_mipmap,ofs,w,h); int ofs2; _get_mipmap_offset_and_size(p_mipmap+1,ofs2,w,h); r_ofs=ofs; r_size=ofs2-ofs; } void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap,int &r_ofs, int &r_size,int &w, int& h) const { int ofs; _get_mipmap_offset_and_size(p_mipmap,ofs,w,h); int ofs2,w2,h2; _get_mipmap_offset_and_size(p_mipmap+1,ofs2,w2,h2); r_ofs=ofs; r_size=ofs2-ofs; } int Image::get_width() const { return width; } int Image::get_height() const{ return height; } bool Image::has_mipmaps() const { return mipmaps; } int Image::get_mipmap_count() const { if (mipmaps) return get_image_required_mipmaps(width,height,format); else return 0; } //using template generates perfectly optimized code due to constant expression reduction and unused variable removal present in all compilers template static void _convert( int p_width,int p_height,const uint8_t* p_src,uint8_t* p_dst ){ for(int y=0;y=FORMAT_RGB565 || p_new_format>=FORMAT_RGB565) { ERR_EXPLAIN("Cannot convert to <-> from non byte formats."); ERR_FAIL(); } Image new_img(width,height,0,p_new_format); //int len=data.size(); PoolVector::Read r = data.read(); PoolVector::Write w = new_img.data.write(); const uint8_t *rptr = r.ptr(); uint8_t *wptr = w.ptr(); int conversion_type = format | p_new_format<<8; switch(conversion_type) { case FORMAT_L8|(FORMAT_LA8<<8): _convert<1,false,1,true,true,true>( width, height,rptr, wptr ); break; case FORMAT_L8|(FORMAT_R8<<8): _convert<1,false,1,false,true,false>( width, height,rptr, wptr ); break; case FORMAT_L8|(FORMAT_RG8<<8): _convert<1,false,2,false,true,false>( width, height,rptr, wptr ); break; case FORMAT_L8|(FORMAT_RGB8<<8): _convert<1,false,3,false,true,false>( width, height,rptr, wptr ); break; case FORMAT_L8|(FORMAT_RGBA8<<8): _convert<1,false,3,true,true,false>( width, height,rptr, wptr ); break; case FORMAT_LA8|(FORMAT_L8<<8): _convert<1,true,1,false,true,true>( width, height,rptr, wptr ); break; case FORMAT_LA8|(FORMAT_R8<<8): _convert<1,true,1,false,true,false>( width, height,rptr, wptr ); break; case FORMAT_LA8|(FORMAT_RG8<<8): _convert<1,true,2,false,true,false>( width, height,rptr, wptr ); break; case FORMAT_LA8|(FORMAT_RGB8<<8): _convert<1,true,3,false,true,false>( width, height,rptr, wptr ); break; case FORMAT_LA8|(FORMAT_RGBA8<<8): _convert<1,true,3,true,true,false>( width, height,rptr, wptr ); break; case FORMAT_R8|(FORMAT_L8<<8): _convert<1,false,1,false,false,true>( width, height,rptr, wptr ); break; case FORMAT_R8|(FORMAT_LA8<<8): _convert<1,false,1,true,false,true>( width, height,rptr, wptr ); break; case FORMAT_R8|(FORMAT_RG8<<8): _convert<1,false,2,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_R8|(FORMAT_RGB8<<8): _convert<1,false,3,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_R8|(FORMAT_RGBA8<<8): _convert<1,false,3,true,false,false>( width, height,rptr, wptr ); break; case FORMAT_RG8|(FORMAT_L8<<8): _convert<2,false,1,false,false,true>( width, height,rptr, wptr ); break; case FORMAT_RG8|(FORMAT_LA8<<8): _convert<2,false,1,true,false,true>( width, height,rptr, wptr ); break; case FORMAT_RG8|(FORMAT_R8<<8): _convert<2,false,1,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_RG8|(FORMAT_RGB8<<8): _convert<2,false,3,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_RG8|(FORMAT_RGBA8<<8): _convert<2,false,3,true,false,false>( width, height,rptr, wptr ); break; case FORMAT_RGB8|(FORMAT_L8<<8): _convert<3,false,1,false,false,true>( width, height,rptr, wptr ); break; case FORMAT_RGB8|(FORMAT_LA8<<8): _convert<3,false,1,true,false,true>( width, height,rptr, wptr ); break; case FORMAT_RGB8|(FORMAT_R8<<8): _convert<3,false,1,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_RGB8|(FORMAT_RG8<<8): _convert<3,false,2,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_RGB8|(FORMAT_RGBA8<<8): _convert<3,false,3,true,false,false>( width, height,rptr, wptr ); break; case FORMAT_RGBA8|(FORMAT_L8<<8): _convert<3,true,1,false,false,true>( width, height,rptr, wptr ); break; case FORMAT_RGBA8|(FORMAT_LA8<<8): _convert<3,true,1,true,false,true>( width, height,rptr, wptr ); break; case FORMAT_RGBA8|(FORMAT_R8<<8): _convert<3,true,1,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_RGBA8|(FORMAT_RG8<<8): _convert<3,true,2,false,false,false>( width, height,rptr, wptr ); break; case FORMAT_RGBA8|(FORMAT_RGB8<<8): _convert<3,true,3,false,false,false>( width, height,rptr, wptr ); break; } r = PoolVector::Read(); w = PoolVector::Write(); bool gen_mipmaps=mipmaps; //mipmaps=false; *this=new_img; if (gen_mipmaps) generate_mipmaps(); } Image::Format Image::get_format() const{ return format; } static double _bicubic_interp_kernel( double x ) { x = ABS(x); double bc = 0; if ( x <= 1 ) bc = ( 1.5 * x - 2.5 ) * x * x + 1; else if ( x < 2 ) bc = ( ( -0.5 * x + 2.5 ) * x - 4 ) * x + 2; return bc; } template static void _scale_cubic(const uint8_t* p_src, uint8_t* p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { // get source image size int width = p_src_width; int height = p_src_height; double xfac = (double) width / p_dst_width; double yfac = (double) height / p_dst_height; // coordinates of source points and cooefficiens double ox, oy, dx, dy, k1, k2; int ox1, oy1, ox2, oy2; // destination pixel values // width and height decreased by 1 int ymax = height - 1; int xmax = width - 1; // temporary pointer for ( uint32_t y = 0; y < p_dst_height; y++ ) { // Y coordinates oy = (double) y * yfac - 0.5f; oy1 = (int) oy; dy = oy - (double) oy1; for ( uint32_t x = 0; x < p_dst_width; x++ ) { // X coordinates ox = (double) x * xfac - 0.5f; ox1 = (int) ox; dx = ox - (double) ox1; // initial pixel value uint8_t *dst=p_dst + (y*p_dst_width+x)*CC; double color[CC]; for(int i=0;i ymax ) oy2 = ymax; for ( int m = -1; m < 3; m++ ) { // get X cooefficient k2 = k1 * _bicubic_interp_kernel( (double) m - dx ); ox2 = ox1 + m; if ( ox2 < 0 ) ox2 = 0; if ( ox2 > xmax ) ox2 = xmax; // get pixel of original image const uint8_t *p = p_src + (oy2 * p_src_width + ox2)*CC; for(int i=0;i 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) { enum { FRAC_BITS=8, FRAC_LEN=(1<> FRAC_BITS; uint32_t src_yofs_down = (i+1)*p_src_height/p_dst_height; if (src_yofs_down>=p_src_height) src_yofs_down=p_src_height-1; //src_yofs_up*=CC; //src_yofs_down*=CC; uint32_t y_ofs_up = src_yofs_up * p_src_width * CC; uint32_t y_ofs_down = src_yofs_down * p_src_width * CC; for(uint32_t j=0;j> FRAC_BITS; uint32_t src_xofs_right = (j+1)*p_src_width/p_dst_width; if (src_xofs_right>=p_src_width) src_xofs_right=p_src_width-1; src_xofs_left*=CC; src_xofs_right*=CC; for(uint32_t l=0;l>FRAC_BITS); uint32_t interp_down = p01+(((p11-p01)*src_xofs_frac)>>FRAC_BITS); uint32_t interp = interp_up+(((interp_down-interp_up)*src_yofs_frac)>>FRAC_BITS); interp>>=FRAC_BITS; p_dst[i*p_dst_width*CC+j*CC+l]=interp; } } } } template 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) { for(uint32_t i=0;iMAX_WIDTH); ERR_FAIL_COND(p_height>MAX_HEIGHT); if (p_width==width && p_height==height) return; Image dst( p_width, p_height, 0, format ); PoolVector::Read r = data.read(); const unsigned char*r_ptr=r.ptr(); PoolVector::Write w = dst.data.write(); unsigned char*w_ptr=w.ptr(); switch(p_interpolation) { case INTERPOLATE_NEAREST: { switch(get_format_pixel_size(format)) { case 1: _scale_nearest<1>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 2: _scale_nearest<2>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 3: _scale_nearest<3>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 4: _scale_nearest<4>(r_ptr,w_ptr,width,height,p_width,p_height); break; } } break; case INTERPOLATE_BILINEAR: { switch(get_format_pixel_size(format)) { case 1: _scale_bilinear<1>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 2: _scale_bilinear<2>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 3: _scale_bilinear<3>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 4: _scale_bilinear<4>(r_ptr,w_ptr,width,height,p_width,p_height); break; } } break; case INTERPOLATE_CUBIC: { switch(get_format_pixel_size(format)) { case 1: _scale_cubic<1>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 2: _scale_cubic<2>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 3: _scale_cubic<3>(r_ptr,w_ptr,width,height,p_width,p_height); break; case 4: _scale_cubic<4>(r_ptr,w_ptr,width,height,p_width,p_height); break; } } break; } r = PoolVector::Read(); w = PoolVector::Write(); if (mipmaps>0) dst.generate_mipmaps(); *this=dst; } void Image::crop( int p_width, int p_height ) { if (!_can_modify(format)) { ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats."); ERR_FAIL(); } ERR_FAIL_COND(p_width<=0); ERR_FAIL_COND(p_height<=0); ERR_FAIL_COND(p_width>MAX_WIDTH); ERR_FAIL_COND(p_height>MAX_HEIGHT); /* to save memory, cropping should be done in-place, however, since this function will most likely either not be used much, or in critical areas, for now it wont, because it's a waste of time. */ if (p_width==width && p_height==height) return; uint8_t pdata[16]; //largest is 16 uint32_t pixel_size = get_format_pixel_size(format); Image dst( p_width, p_height,0, format ); { PoolVector::Read r = data.read(); PoolVector::Write w = dst.data.write(); for (int y=0;y=width || y>=height)) { for(uint32_t i=0;i0) dst.generate_mipmaps(); *this=dst; } void Image::flip_y() { if (!_can_modify(format)) { ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats."); ERR_FAIL(); } bool gm=mipmaps; if (gm) clear_mipmaps(); { PoolVector::Write w = data.write(); uint8_t up[16]; uint8_t down[16]; uint32_t pixel_size = get_format_pixel_size(format); for (int y=0;y::Write w = data.write(); uint8_t up[16]; uint8_t down[16]; uint32_t pixel_size = get_format_pixel_size(format); for (int y=0;y>=pixshift; size+=s; if (p_mipmaps>=0 && mm==p_mipmaps) break; if (p_mipmaps>=0) { w=MAX(minw,w>>1); h=MAX(minh,h>>1); } else { if (w==minw && h==minh) break; w=MAX(minw,w>>1); h=MAX(minh,h>>1); } mm++; }; r_mipmaps=mm; return size; } bool Image::_can_modify(Format p_format) const { return p_format static void _generate_po2_mipmap(const uint8_t* p_src, uint8_t* p_dst, uint32_t p_width, uint32_t p_height) { //fast power of 2 mipmap generation uint32_t dst_w = p_width >> 1; uint32_t dst_h = p_height >> 1; for(uint32_t i=0;i>2; } dst_ptr+=CC; rup_ptr+=CC*2; rdown_ptr+=CC*2; } } } void Image::expand_x2_hq2x() { ERR_FAIL_COND(!_can_modify(format)); Format current = format; bool mm=has_mipmaps(); if (mm) { clear_mipmaps(); } if (current!=FORMAT_RGBA8) convert(FORMAT_RGBA8); PoolVector dest; dest.resize(width*2*height*2*4); { PoolVector::Read r = data.read(); PoolVector::Write w = dest.write(); hq2x_resize((const uint32_t*)r.ptr(),width,height,(uint32_t*)w.ptr()); } width*=2; height*=2; data=dest; if (current!=FORMAT_RGBA8) convert(current); if (mipmaps) { generate_mipmaps(); } } void Image::shrink_x2() { ERR_FAIL_COND( data.size()==0 ); if (mipmaps) { //just use the lower mipmap as base and copy all PoolVector new_img; int ofs = get_mipmap_offset(1); int new_size = data.size()-ofs; new_img.resize(new_size); { PoolVector::Write w=new_img.write(); PoolVector::Read r=data.read(); copymem(w.ptr(),&r[ofs],new_size); } width/=2; height/=2; data=new_img; } else { PoolVector new_img; ERR_FAIL_COND( !_can_modify(format) ); int ps = get_format_pixel_size(format); new_img.resize((width/2)*(height/2)*ps); { PoolVector::Write w=new_img.write(); PoolVector::Read r=data.read(); switch(format) { case FORMAT_L8: case FORMAT_R8: _generate_po2_mipmap<1>(r.ptr(), w.ptr(), width,height); break; case FORMAT_LA8: _generate_po2_mipmap<2>(r.ptr(), w.ptr(), width,height); break; case FORMAT_RG8: _generate_po2_mipmap<2>(r.ptr(), w.ptr(), width,height); break; case FORMAT_RGB8: _generate_po2_mipmap<3>(r.ptr(), w.ptr(), width,height); break; case FORMAT_RGBA8: _generate_po2_mipmap<4>(r.ptr(), w.ptr(), width,height); break; default: {} } } width/=2; height/=2; data=new_img; } } Error Image::generate_mipmaps(bool p_keep_existing) { if (!_can_modify(format)) { ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats."); ERR_FAIL_V(ERR_UNAVAILABLE); } int mmcount = get_mipmap_count(); int from_mm=1; if (p_keep_existing) { from_mm=mmcount+1; } int size = _get_dst_image_size(width,height,format,mmcount); data.resize(size); PoolVector::Write wp=data.write(); if (nearest_power_of_2(width)==uint32_t(width) && nearest_power_of_2(height)==uint32_t(height)) { //use fast code for powers of 2 int prev_ofs=0; int prev_h=height; int prev_w=width; for(int i=1;i=from_mm) { switch(format) { case FORMAT_L8: case FORMAT_R8: _generate_po2_mipmap<1>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break; case FORMAT_LA8: case FORMAT_RG8: _generate_po2_mipmap<2>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break; case FORMAT_RGB8: _generate_po2_mipmap<3>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break; case FORMAT_RGBA8: _generate_po2_mipmap<4>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h); break; default: {} } } prev_ofs=ofs; prev_w=w; prev_h=h; } } else { //use slow code.. //use bilinear filtered code for non powers of 2 int prev_ofs=0; int prev_h=height; int prev_w=width; for(int i=1;i=from_mm) { switch(format) { case FORMAT_L8: case FORMAT_R8: _scale_bilinear<1>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break; case FORMAT_LA8: case FORMAT_RG8: _scale_bilinear<2>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break; case FORMAT_RGB8:_scale_bilinear<3>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break; case FORMAT_RGBA8: _scale_bilinear<4>(&wp[prev_ofs], &wp[ofs], prev_w,prev_h,w,h); break; default: {} } } prev_ofs=ofs; prev_w=w; prev_h=h; } } return OK; } void Image::clear_mipmaps() { if (!mipmaps) return; if (empty()) return; int ofs,w,h; _get_mipmap_offset_and_size(1,ofs,w,h); data.resize(ofs); mipmaps=false; } bool Image::empty() const { return (data.size()==0); } PoolVector Image::get_data() const { return data; } void Image::create(int p_width, int p_height, bool p_use_mipmaps,Format p_format) { int mm=0; int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_use_mipmaps?-1:0); data.resize( size ); { PoolVector::Write w= data.write(); zeromem(w.ptr(),size); } width=p_width; height=p_height; mipmaps=p_use_mipmaps; format=p_format; } void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector& p_data) { ERR_FAIL_INDEX(p_width-1,MAX_WIDTH); ERR_FAIL_INDEX(p_height-1,MAX_HEIGHT); int mm; int size = _get_dst_image_size(p_width,p_height,p_format,mm,p_use_mipmaps); if (size!=p_data.size()) { ERR_EXPLAIN("Expected data size of "+itos(size)+" in Image::create()"); ERR_FAIL_COND(p_data.size()!=size); } height=p_height; width=p_width; format=p_format; data=p_data; mipmaps=p_use_mipmaps; } void Image::create( const char ** p_xpm ) { int size_width,size_height; int pixelchars=0; mipmaps=false; bool has_alpha=false; enum Status { READING_HEADER, READING_COLORS, READING_PIXELS, DONE }; Status status = READING_HEADER; int line=0; HashMap colormap; int colormap_size; uint32_t pixel_size; PoolVector::Write w; while (status!=DONE) { const char * line_ptr = p_xpm[line]; switch (status) { case READING_HEADER: { String line_str=line_ptr; line_str.replace("\t"," "); size_width=line_str.get_slicec(' ',0).to_int(); size_height=line_str.get_slicec(' ',1).to_int(); colormap_size=line_str.get_slicec(' ',2).to_int(); pixelchars=line_str.get_slicec(' ',3).to_int(); ERR_FAIL_COND(colormap_size > 32766); ERR_FAIL_COND(pixelchars > 5); ERR_FAIL_COND(size_width > 32767); ERR_FAIL_COND(size_height > 32767); status=READING_COLORS; } break; case READING_COLORS: { String colorstring; for (int i=0;i='0' && v<='9') v-='0'; else if (v>='A' && v<='F') v=(v-'A')+10; else if (v>='a' && v<='f') v=(v-'a')+10; else break; switch(i) { case 0: col_r=v<<4; break; case 1: col_r|=v; break; case 2: col_g=v<<4; break; case 3: col_g|=v; break; case 4: col_b=v<<4; break; case 5: col_b|=v; break; }; } // magenta mask if (col_r==255 && col_g==0 && col_b==255) { colormap[colorstring]=Color(0,0,0,0); has_alpha=true; } else { colormap[colorstring]=Color(col_r/255.0,col_g/255.0,col_b/255.0,1.0); } } } if (line==colormap_size) { status=READING_PIXELS; create(size_width,size_height,0,has_alpha?FORMAT_RGBA8:FORMAT_RGB8); w=data.write(); pixel_size=has_alpha?4:3; } } break; case READING_PIXELS: { int y=line-colormap_size-1; for (int x=0;x0) {\ \ detected=true;\ break;\ }\ } bool Image::is_invisible() const { if (format==FORMAT_L8 || format==FORMAT_RGB8 || format==FORMAT_RG8) return false; int len = data.size(); if (len==0) return true; int w,h; _get_mipmap_offset_and_size(1,len,w,h); PoolVector::Read r = data.read(); const unsigned char *data_ptr=r.ptr(); bool detected=false; switch(format) { case FORMAT_LA8: { for(int i=0;i<(len>>1);i++) { DETECT_NON_ALPHA(data_ptr[(i<<1)+1]); } } break; case FORMAT_RGBA8: { for(int i=0;i<(len>>2);i++) { DETECT_NON_ALPHA(data_ptr[(i<<2)+3]) } } break; case FORMAT_PVRTC2A: case FORMAT_PVRTC4A: case FORMAT_DXT3: case FORMAT_DXT5: { detected=true; } break; default: {} } return !detected; } Image::AlphaMode Image::detect_alpha() const { int len = data.size(); if (len==0) return ALPHA_NONE; int w,h; _get_mipmap_offset_and_size(1,len,w,h); PoolVector::Read r = data.read(); const unsigned char *data_ptr=r.ptr(); bool bit=false; bool detected=false; switch(format) { case FORMAT_LA8: { for(int i=0;i<(len>>1);i++) { DETECT_ALPHA(data_ptr[(i<<1)+1]); } } break; case FORMAT_RGBA8: { for(int i=0;i<(len>>2);i++) { DETECT_ALPHA(data_ptr[(i<<2)+3]) } } break; case FORMAT_PVRTC2A: case FORMAT_PVRTC4A: case FORMAT_DXT3: case FORMAT_DXT5: { detected=true; } break; default: {} } if (detected) return ALPHA_BLEND; else if (bit) return ALPHA_BIT; else return ALPHA_NONE; } Error Image::load(const String& p_path) { return ImageLoader::load_image(p_path, this); } Error Image::save_png(const String& p_path) { if (save_png_func == NULL) return ERR_UNAVAILABLE; return save_png_func(p_path, *this); } bool Image::operator==(const Image& p_image) const { if (data.size() == 0 && p_image.data.size() == 0) return true; PoolVector::Read r = data.read(); PoolVector::Read pr = p_image.data.read(); return r.ptr() == pr.ptr(); } int Image::get_image_data_size(int p_width, int p_height, Format p_format,int p_mipmaps) { int mm; return _get_dst_image_size(p_width,p_height,p_format,mm,p_mipmaps); } int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) { int mm; _get_dst_image_size(p_width,p_height,p_format,mm,-1); return mm; } Error Image::_decompress_bc() { int wd=width,ht=height; if (wd%4!=0) { wd+=4-(wd%4); } if (ht%4!=0) { ht+=4-(ht%4); } int mm; int size = _get_dst_image_size(wd,ht,FORMAT_RGBA8,mm); PoolVector newdata; newdata.resize(size); PoolVector::Write w = newdata.write(); PoolVector::Read r = data.read(); int rofs=0; int wofs=0; //print_line("width: "+itos(wd)+" height: "+itos(ht)); for(int i=0;i<=mm;i++) { switch(format) { case FORMAT_DXT1: { int len = (wd*ht)/16; uint8_t* dst=&w[wofs]; uint32_t ofs_table[16]; for(int x=0;x<4;x++) { for(int y=0;y<4;y++) { ofs_table[15-(y*4+(3-x))]=(x+y*wd)*4; } } for(int j=0;j>11)<<3), uint8_t(((col_a>>5)&0x3f)<<2),uint8_t(((col_a)&0x1f)<<3), 255 }, { uint8_t((col_b>>11)<<3), uint8_t(((col_b>>5)&0x3f)<<2),uint8_t(((col_b)&0x1f)<<3), 255 }, {0,0,0,255}, {0,0,0,255} }; if (col_a>1; table[2][1]=(int(table[0][1])+int(table[1][1]))>>1; table[2][2]=(int(table[0][2])+int(table[1][2]))>>1; table[3][3]=0; //premul alpha black } else { //gradient table[2][0]=(int(table[0][0])*2+int(table[1][0]))/3; table[2][1]=(int(table[0][1])*2+int(table[1][1]))/3; table[2][2]=(int(table[0][2])*2+int(table[1][2]))/3; table[3][0]=(int(table[0][0])+int(table[1][0])*2)/3; table[3][1]=(int(table[0][1])+int(table[1][1])*2)/3; table[3][2]=(int(table[0][2])+int(table[1][2])*2)/3; } uint32_t block=src[4]; block<<=8; block|=src[5]; block<<=8; block|=src[6]; block<<=8; block|=src[7]; int y = (j/(wd/4))*4; int x = (j%(wd/4))*4; int pixofs = (y*wd+x)*4; for(int k=0;k<16;k++) { int idx = pixofs+ofs_table[k]; dst[idx+0]=table[block&0x3][0]; dst[idx+1]=table[block&0x3][1]; dst[idx+2]=table[block&0x3][2]; dst[idx+3]=table[block&0x3][3]; block>>=2; } } rofs+=len*8; wofs+=wd*ht*4; wd/=2; ht/=2; } break; case FORMAT_DXT3: { int len = (wd*ht)/16; uint8_t* dst=&w[wofs]; uint32_t ofs_table[16]; for(int x=0;x<4;x++) { for(int y=0;y<4;y++) { ofs_table[15-(y*4+(3-x))]=(x+y*wd)*4; } } for(int j=0;j>11)<<3), uint8_t(((col_a>>5)&0x3f)<<2),uint8_t(((col_a)&0x1f)<<3), 255 }, { uint8_t((col_b>>11)<<3), uint8_t(((col_b>>5)&0x3f)<<2),uint8_t(((col_b)&0x1f)<<3), 255 }, {0,0,0,255}, {0,0,0,255} }; //always gradient table[2][0]=(int(table[0][0])*2+int(table[1][0]))/3; table[2][1]=(int(table[0][1])*2+int(table[1][1]))/3; table[2][2]=(int(table[0][2])*2+int(table[1][2]))/3; table[3][0]=(int(table[0][0])+int(table[1][0])*2)/3; table[3][1]=(int(table[0][1])+int(table[1][1])*2)/3; table[3][2]=(int(table[0][2])+int(table[1][2])*2)/3; uint32_t block=src[4+8]; block<<=8; block|=src[5+8]; block<<=8; block|=src[6+8]; block<<=8; block|=src[7+8]; int y = (j/(wd/4))*4; int x = (j%(wd/4))*4; int pixofs = (y*wd+x)*4; for(int k=0;k<16;k++) { uint8_t alpha = ablock&0xf; alpha=int(alpha)*255/15; //right way for alpha int idx = pixofs+ofs_table[k]; dst[idx+0]=table[block&0x3][0]; dst[idx+1]=table[block&0x3][1]; dst[idx+2]=table[block&0x3][2]; dst[idx+3]=alpha; block>>=2; ablock>>=4; } } rofs+=len*16; wofs+=wd*ht*4; wd/=2; ht/=2; } break; case FORMAT_DXT5: { int len = (wd*ht)/16; uint8_t* dst=&w[wofs]; uint32_t ofs_table[16]; for(int x=0;x<4;x++) { for(int y=0;y<4;y++) { ofs_table[15-(y*4+(3-x))]=(x+y*wd)*4; } } for(int j=0;ja_end) { atable[0]=(int(a_start)*7+int(a_end)*0)/7; atable[1]=(int(a_start)*6+int(a_end)*1)/7; atable[2]=(int(a_start)*5+int(a_end)*2)/7; atable[3]=(int(a_start)*4+int(a_end)*3)/7; atable[4]=(int(a_start)*3+int(a_end)*4)/7; atable[5]=(int(a_start)*2+int(a_end)*5)/7; atable[6]=(int(a_start)*1+int(a_end)*6)/7; atable[7]=(int(a_start)*0+int(a_end)*7)/7; } else { atable[0]=(int(a_start)*5+int(a_end)*0)/5; atable[1]=(int(a_start)*4+int(a_end)*1)/5; atable[2]=(int(a_start)*3+int(a_end)*2)/5; atable[3]=(int(a_start)*2+int(a_end)*3)/5; atable[4]=(int(a_start)*1+int(a_end)*4)/5; atable[5]=(int(a_start)*0+int(a_end)*5)/5; atable[6]=0; atable[7]=255; } uint16_t col_a=src[8+1]; col_a<<=8; col_a|=src[8+0]; uint16_t col_b=src[8+3]; col_b<<=8; col_b|=src[8+2]; uint8_t table[4][4]={ { uint8_t((col_a>>11)<<3), uint8_t(((col_a>>5)&0x3f)<<2),uint8_t(((col_a)&0x1f)<<3), 255 }, { uint8_t((col_b>>11)<<3), uint8_t(((col_b>>5)&0x3f)<<2),uint8_t(((col_b)&0x1f)<<3), 255 }, {0,0,0,255}, {0,0,0,255} }; //always gradient table[2][0]=(int(table[0][0])*2+int(table[1][0]))/3; table[2][1]=(int(table[0][1])*2+int(table[1][1]))/3; table[2][2]=(int(table[0][2])*2+int(table[1][2]))/3; table[3][0]=(int(table[0][0])+int(table[1][0])*2)/3; table[3][1]=(int(table[0][1])+int(table[1][1])*2)/3; table[3][2]=(int(table[0][2])+int(table[1][2])*2)/3; uint32_t block=src[4+8]; block<<=8; block|=src[5+8]; block<<=8; block|=src[6+8]; block<<=8; block|=src[7+8]; int y = (j/(wd/4))*4; int x = (j%(wd/4))*4; int pixofs = (y*wd+x)*4; for(int k=0;k<16;k++) { uint8_t alpha = ablock&0x7; int idx = pixofs+ofs_table[k]; dst[idx+0]=table[block&0x3][0]; dst[idx+1]=table[block&0x3][1]; dst[idx+2]=table[block&0x3][2]; dst[idx+3]=atable[alpha]; block>>=2; ablock>>=3; } } rofs+=len*16; wofs+=wd*ht*4; wd/=2; ht/=2; } break; default: {} } } w=PoolVector::Write(); r=PoolVector::Read(); data=newdata; format=FORMAT_RGBA8; if (wd!=width || ht!=height) { SWAP(width,wd); SWAP(height,ht); crop(wd,ht); } return OK; } bool Image::is_compressed() const { return format>=FORMAT_RGB565; } Image Image::decompressed() const { Image img=*this; img.decompress(); return img; } Error Image::decompress() { if (format>=FORMAT_DXT1 && format<=FORMAT_ATI2 ) _decompress_bc();//_image_decompress_bc(this); else if (format>=FORMAT_PVRTC2 && format<=FORMAT_PVRTC4A&& _image_decompress_pvrtc) _image_decompress_pvrtc(this); else if (format==FORMAT_ETC && _image_decompress_etc) _image_decompress_etc(this); else if (format>=FORMAT_ETC2_R11 && format<=FORMAT_ETC2_RGB8A1 && _image_decompress_etc) _image_decompress_etc2(this); else return ERR_UNAVAILABLE; return OK; } Error Image::compress(CompressMode p_mode) { switch(p_mode) { case COMPRESS_16BIT: { //ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE); //_image_compress_bc_func(this); } break; case COMPRESS_S3TC: { ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE); _image_compress_bc_func(this); } break; case COMPRESS_PVRTC2: { ERR_FAIL_COND_V(!_image_compress_pvrtc2_func, ERR_UNAVAILABLE); _image_compress_pvrtc2_func(this); } break; case COMPRESS_PVRTC4: { ERR_FAIL_COND_V(!_image_compress_pvrtc4_func, ERR_UNAVAILABLE); _image_compress_pvrtc4_func(this); } break; case COMPRESS_ETC: { ERR_FAIL_COND_V(!_image_compress_etc_func, ERR_UNAVAILABLE); _image_compress_etc_func(this); } break; case COMPRESS_ETC2: { ERR_FAIL_COND_V(!_image_compress_etc_func, ERR_UNAVAILABLE); _image_compress_etc_func(this); } break; } return OK; } Image Image::compressed(int p_mode) { Image ret = *this; ret.compress((Image::CompressMode)p_mode); return ret; } Image::Image(const char **p_xpm) { width=0; height=0; mipmaps=false; format=FORMAT_L8; create(p_xpm); } Image::Image(int p_width, int p_height,bool p_use_mipmaps, Format p_format) { width=0; height=0; mipmaps=p_use_mipmaps; format=FORMAT_L8; create(p_width,p_height,p_use_mipmaps,p_format); } Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector& p_data) { width=0; height=0; mipmaps=p_mipmaps; format=FORMAT_L8; create(p_width,p_height,p_mipmaps,p_format,p_data); } Rect2 Image::get_used_rect() const { if (format!=FORMAT_LA8 && format!=FORMAT_RGBA8) return Rect2(Point2(),Size2(width,height)); int len = data.size(); if (len==0) return Rect2(); //int data_size = len; PoolVector::Read r = data.read(); const unsigned char *rptr=r.ptr(); int ps = format==FORMAT_LA8?2:4; int minx=0xFFFFFF,miny=0xFFFFFFF; int maxx=-1,maxy=-1; for(int j=0;j2; if (!opaque) continue; if (i>maxx) maxx=i; if (j>maxy) maxy=j; if (i::Write wp = data.write(); uint8_t *dst_data_ptr=wp.ptr(); PoolVector::Read rp = p_src.data.read(); const uint8_t *src_data_ptr=rp.ptr(); int pixel_size=get_format_pixel_size(format); for(int i=0;i (*Image::lossy_packer)(const Image& ,float )=NULL; Image (*Image::lossy_unpacker)(const PoolVector& )=NULL; PoolVector (*Image::lossless_packer)(const Image& )=NULL; Image (*Image::lossless_unpacker)(const PoolVector& )=NULL; void Image::set_compress_bc_func(void (*p_compress_func)(Image *)) { _image_compress_bc_func=p_compress_func; } void Image::normalmap_to_xy() { convert(Image::FORMAT_RGBA8); { int len = data.size()/4; PoolVector::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); for(int i=0;i>8; ptr[1]=(uint16_t(ptr[1])*uint16_t(ptr[3]))>>8; ptr[2]=(uint16_t(ptr[2])*uint16_t(ptr[3]))>>8; } } } void Image::fix_alpha_edges() { if (data.size()==0) return; if (format!=FORMAT_RGBA8) return; //not needed PoolVector dcopy = data; PoolVector::Read rp = dcopy.read(); const uint8_t *srcptr=rp.ptr(); PoolVector::Write wp = data.write(); unsigned char *data_ptr=wp.ptr(); const int max_radius=4; const int alpha_treshold=20; const int max_dist=0x7FFFFFFF; for(int i=0;i=alpha_treshold) continue; int closest_dist=max_dist; uint8_t closest_color[3]; int from_x = MAX(0,j-max_radius); int to_x = MIN(width-1,j+max_radius); int from_y = MAX(0,i-max_radius); int to_y = MIN(height-1,i+max_radius); for(int k=from_y;k<=to_y;k++) { for(int l=from_x;l<=to_x;l++) { int dy = i-k; int dx = j-l; int dist = dy*dy+dx*dx; if (dist>=closest_dist) continue; const uint8_t * rp = &srcptr[(k*width+l)<<2]; if (rp[3]