summaryrefslogtreecommitdiffstats
path: root/src/imageutils/croppedqimage.cpp
blob: 81330748e0ce67f3b4053aa7db2230e5a7c3d402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*

 Copyright (C) 2005 Lubos Lunak <[email protected]>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include "croppedqimage.h"

namespace ImageUtils
{

// This class is used in ImageView::performPaint(). Just using TQImage::copy( TQRect )
// takes a significant time with very large images. So instead of copying the image data
// just create CroppedTQImage which fakes a subimage by manipulating its scanline pointers.
// That will of course break if something doesn't use scanlines but accesses the image
// data directly, TQImage::copy() being the most notable case. There are two ways
// to handle that: 1) It is possible to manually call normalize() which will make
// CroppedTQImage copy the image data and own it, just like proper TQImage. 2) CroppedTQImage
// has as a data member also TQImage holding the original image. This ensures that all
// the original image data are still available for the whole lifetime of CroppedTQImage.

CroppedTQImage::CroppedTQImage( const TQImage& im, const TQRect& rect )
    : TQImage( rect.size(), im.depth(), im.numColors(), im.bitOrder())
    , orig( im )
    {
    if( im.isNull())
        return;
    memcpy( colorTable(), im.colorTable(), im.numColors() * sizeof( TQRgb ));
    setAlphaBuffer( im.hasAlphaBuffer());
    setDotsPerMeterX( im.dotsPerMeterX());
    setDotsPerMeterY( im.dotsPerMeterY());
    //data->offset = im.offset();
    // make scanlines point to right places in the original TQImage
    for( int i = 0;
         i < height();
         ++i )
        const_cast<CroppedTQImage*>(this)->jumpTable()[ i ] = const_cast<TQImage&>(im).scanLine( rect.y() + i ) + rect.x() * ( depth() / 8 );
    }

CroppedTQImage& CroppedTQImage::operator= ( const TQImage& im )
    {
    TQImage::operator=( im );
    return *this;
    }

void CroppedTQImage::normalize()
    {
    // is it a normal TQImage with its own data?
    uchar* firstdata = ( uchar* )( jumpTable() + height());
    if( scanLine( 0 ) == firstdata )
        return;
    // copy the image data to our own data and make scanlines point properly there
    for( int i = 0;
         i < height();
         ++i )
        {
        uchar* oldline = scanLine( i );
        jumpTable()[ i ] = firstdata + i * bytesPerLine();
        memcpy( scanLine( i ), oldline, bytesPerLine());
        }
    }

} // namespace