Skip to content

Commit

Permalink
FIX: Crash on duplicate images, but some of the methods in Image need…
Browse files Browse the repository at this point in the history
…s review. #3
  • Loading branch information
spillerrec committed Oct 17, 2015
1 parent 7b1ecb4 commit ca4a13c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ const auto PIXEL_DIFFERENT = 0; //Pixel do not match with other image
const auto PIXEL_MATCH = 1; //Pixel is the same as other image
const auto PIXEL_SHARED = 2; //Pixel is the same, but must not be set as it differ in another image

static QImage make_mask( QSize size ){
QImage mask( size, QImage::Format_Indexed8 );
mask.setColor( PIXEL_MATCH, qRgb(255, 0, 0) );
mask.setColor( PIXEL_DIFFERENT, qRgb(0, 255, 0) );
mask.setColor( PIXEL_SHARED, qRgb(0, 0, 255) );
return mask;
}

Image::Image( QPoint pos, QImage img ) : pos(pos), img(img), mask(make_mask(img.size())) {
mask.fill( PIXEL_DIFFERENT );
}

/** Create a resized version of this image, will keep aspect ratio
* \param [in] size Maximum dimensions of the resized image
* \return The resized image */
Expand Down Expand Up @@ -170,11 +182,7 @@ Image Image::combine( Image on_top ) const{
Image Image::difference( Image input ) const{
//TODO: images must be the same size and at same point

QImage mask( img.size(), QImage::Format_Indexed8 );
mask.setColor( PIXEL_MATCH, qRgb(255, 0, 0) );
mask.setColor( PIXEL_DIFFERENT, qRgb(0, 255, 0) );
mask.setColor( PIXEL_SHARED, qRgb(0, 0, 255) );

auto mask = make_mask( img.size() );
for( int iy=0; iy<img.height(); iy++ ){
auto out = (const QRgb*) img.constScanLine( iy );
auto in = (const QRgb*)input.img.constScanLine( iy );
Expand Down
4 changes: 2 additions & 2 deletions src/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Image {
public:
/** \param [in] pos Offset of the image
* \param [in] img The image data */
Image( QPoint pos, QImage img ) : pos(pos), img(img) { }
Image( QPoint pos, QImage img );

/** \param [in] img QImage which will be converted to ARGB32 and positioned at {0,0} */
Image( QImage img ) : Image( {0,0}, img.convertToFormat(QImage::Format_ARGB32) ) { }
Expand Down Expand Up @@ -93,7 +93,7 @@ class Image {
Image sub_image( int x, int y, int width, int height ) const{
QSize newSize( std::min( x+width, x+mask.width() ) - x
, std::min( y+height, y+mask.height() ) - y );
auto newMask = newSize.isNull() ? mask : mask.copy( x,y, newSize.width(), newSize.height() );
auto newMask = newSize.isNull() ? QImage() : mask.copy( x,y, newSize.width(), newSize.height() );
return Image( pos+QPoint(x,y), img, newMask );
}

Expand Down

0 comments on commit ca4a13c

Please sign in to comment.