From 0a0d178f64209c9d0654e16f8b2447f7e9fee6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sun, 22 Sep 2013 01:20:09 -0300 Subject: [PATCH] Added cImage::Crop. Some minor fixes in cImage and cTexture. --- include/eepp/graphics/cimage.hpp | 4 ++++ include/eepp/graphics/ctexture.hpp | 5 ++++- src/eepp/graphics/cimage.cpp | 20 +++++++++++++++++++- src/eepp/graphics/ctexture.cpp | 12 ++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/include/eepp/graphics/cimage.hpp b/include/eepp/graphics/cimage.hpp index facfaedb7..d439fa225 100644 --- a/include/eepp/graphics/cimage.hpp +++ b/include/eepp/graphics/cimage.hpp @@ -4,6 +4,7 @@ #include #include +#include using namespace EE::Math; #include @@ -152,6 +153,9 @@ class EE_API cImage { /** Create a thumnail of the image */ cImage * Thumbnail( const Uint32& maxWidth, const Uint32& maxHeight, EE_RESAMPLER_FILTER filter = RESAMPLER_LANCZOS4 ); + /** Creates a cropped image from the current image */ + cImage * Crop( eeRecti rect ); + /** Set as true if you dont want to free the image data in the cImage destruction ( false as default ). */ void AvoidFreeImage( const bool& AvoidFree ); diff --git a/include/eepp/graphics/ctexture.hpp b/include/eepp/graphics/ctexture.hpp index a19fd446c..1ed38e94d 100755 --- a/include/eepp/graphics/ctexture.hpp +++ b/include/eepp/graphics/ctexture.hpp @@ -77,7 +77,10 @@ class EE_API cTexture : public cImage, private NonCopyable { void FillWithColor( const eeColorA& Color ); /** Resize the texture */ - void Resize( const Uint32& newWidth, const Uint32& newHeight ); + void Resize( const Uint32& newWidth, const Uint32& newHeight, EE_RESAMPLER_FILTER filter = RESAMPLER_LANCZOS4 ); + + /** Scale the texture */ + void Scale( const eeFloat& scale, EE_RESAMPLER_FILTER filter = RESAMPLER_LANCZOS4 ); /** Copy an image inside the texture */ void CopyImage( cImage * image, const Uint32& x, const Uint32& y ); diff --git a/src/eepp/graphics/cimage.cpp b/src/eepp/graphics/cimage.cpp index 71ae0dd30..8c54426d1 100644 --- a/src/eepp/graphics/cimage.cpp +++ b/src/eepp/graphics/cimage.cpp @@ -611,13 +611,31 @@ cImage * cImage::Thumbnail( const Uint32& maxWidth, const Uint32& maxHeight, EE_ return NULL; } +cImage * cImage::Crop( eeRecti rect ) { + if ( rect.Left >= 0 && rect.Right <= (Int32)mWidth && rect.Top >= 0 && rect.Bottom <= (Int32)mHeight ) { + cImage * img = eeNew( cImage, ( rect.Size().Width(), rect.Size().Height(), mChannels ) ); + + // Copy per row + for ( eeUint ty = 0; ty < img->mHeight; ty++ ) { + Uint8 * pDst = &img->mPixels[ ( ty * img->mWidth ) * mChannels ]; + const Uint8 * pSrc = &mPixels[ ( rect.Left + ( ( ty + rect.Top ) * mWidth ) ) * mChannels ]; + + memcpy( pDst, pSrc, mChannels * img->mWidth ); + } + + return img; + } + + return NULL; +} + void cImage::Flip() { if ( NULL != mPixels ) { cImage tImg( mHeight, mWidth, mChannels ); for ( eeUint y = 0; y < mHeight; y++ ) for ( eeUint x = 0; x < mWidth; x++ ) - tImg.SetPixel( y, x, GetPixel( x, y ) ); + tImg.SetPixel( y, x, GetPixel( x, mHeight - 1 - y ) ); ClearCache(); diff --git a/src/eepp/graphics/ctexture.cpp b/src/eepp/graphics/ctexture.cpp index 9e2f6ae54..4840d122f 100755 --- a/src/eepp/graphics/ctexture.cpp +++ b/src/eepp/graphics/ctexture.cpp @@ -262,10 +262,18 @@ void cTexture::FillWithColor( const eeColorA& Color ) { Unlock( false, true ); } -void cTexture::Resize(const Uint32& newWidth, const Uint32& newHeight ) { +void cTexture::Resize( const Uint32& newWidth, const Uint32& newHeight , EE_RESAMPLER_FILTER filter ) { Lock(); - cImage::Resize( newWidth, newHeight ); + cImage::Resize( newWidth, newHeight, filter ); + + Unlock( false, true ); +} + +void cTexture::Scale( const eeFloat& scale, EE_RESAMPLER_FILTER filter ) { + Lock(); + + cImage::Scale( scale, filter ); Unlock( false, true ); }