From 89d87294dbc84df8dd7740cd04cc4842b8738e96 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 4 May 2016 12:36:51 -0300 Subject: ability to shrink all images x2 on load this is for extreme cases when running on devices with very low video memory, so you can still retain compatibility. --- core/image.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/image.h | 1 + 2 files changed, 61 insertions(+) (limited to 'core') diff --git a/core/image.cpp b/core/image.cpp index 52946bbb85..8635aa1b29 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -901,6 +901,66 @@ static void _generate_po2_mipmap(const uint8_t* p_src, uint8_t* p_dst, uint32_t } +void Image::shrink_x2() { + + ERR_FAIL_COND(format==FORMAT_INDEXED || format==FORMAT_INDEXED_ALPHA); + ERR_FAIL_COND( data.size()==0 ); + + + + if (mipmaps) { + + //just use the lower mipmap as base and copy all + DVector new_img; + + int ofs = get_mipmap_offset(1); + + int new_size = data.size()-ofs; + new_img.resize(new_size); + + + { + DVector::Write w=new_img.write(); + DVector::Read r=data.read(); + + copymem(w.ptr(),&r[ofs],new_size); + } + + mipmaps--; + width/=2; + height/=2; + data=new_img; + + } else { + + DVector new_img; + + ERR_FAIL_COND( format>=FORMAT_INDEXED ); + int ps = get_format_pixel_size(format); + new_img.resize((width/2)*(height/2)*ps); + + { + DVector::Write w=new_img.write(); + DVector::Read r=data.read(); + + switch(format) { + + case FORMAT_GRAYSCALE: + case FORMAT_INTENSITY: _generate_po2_mipmap<1>(r.ptr(), w.ptr(), width,height); break; + case FORMAT_GRAYSCALE_ALPHA: _generate_po2_mipmap<2>(r.ptr(), w.ptr(), width,height); break; + case FORMAT_RGB: _generate_po2_mipmap<3>(r.ptr(), w.ptr(), width,height); break; + case FORMAT_RGBA: _generate_po2_mipmap<4>(r.ptr(), w.ptr(), width,height); break; + default: {} + } + } + + width/=2; + height/=2; + data=new_img; + + } +} + Error Image::generate_mipmaps(int p_mipmaps,bool p_keep_existing) { if (!_can_modify(format)) { diff --git a/core/image.h b/core/image.h index fe1822f661..35bbd1a684 100644 --- a/core/image.h +++ b/core/image.h @@ -249,6 +249,7 @@ public: void resize_to_po2(bool p_square=false); void resize( int p_width, int p_height, Interpolation p_interpolation=INTERPOLATE_BILINEAR ); Image resized( int p_width, int p_height, int p_interpolation=INTERPOLATE_BILINEAR ); + void shrink_x2(); /** * Crop the image to a specific size, if larger, then the image is filled by black */ -- cgit v1.2.3