summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_random_sub_accessor.cpp
blob: a1ce8aa450169889ff88bb89a2f663ade59a935a (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
78
79
80
81
82
83
84
/*
 *  This file is part of the KDE project
 *
 *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
 *
 *  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; version 2 of the License.
 *
 *  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 "kis_random_sub_accessor.h"

#include "kis_paint_device.h"

KisRandomSubAccessorPixel::KisRandomSubAccessorPixel(KisPaintDeviceSP device) :
    m_device(device), m_currentPoint( 0, 0 ), m_randomAccessor(device->createRandomAccessor(0,0, false))
{
}


KisRandomSubAccessorPixel::~KisRandomSubAccessorPixel()
{
}


void KisRandomSubAccessorPixel::sampledOldRawData(TQ_UINT8* dst)
{
    const TQ_UINT8* pixels[4];
    TQ_UINT8 weights[4];
    int x = (int)floor(m_currentPoint.x());
    int y = (int)floor(m_currentPoint.y());
    double hsub = m_currentPoint.x() - x;
    if(hsub < 0.0 ) hsub = 1.0 + hsub;
    double vsub = m_currentPoint.y() - y;
    if(vsub < 0.0 ) vsub = 1.0 + vsub;
    weights[0] = (int)tqRound( ( 1.0 - hsub) * ( 1.0 - vsub) * 255 );
    m_randomAccessor.moveTo(x, y);
    pixels[0] = m_randomAccessor.oldRawData();
    weights[1] = (int)tqRound( ( 1.0 - vsub) * hsub * 255 );
    m_randomAccessor.moveTo(x+1, y);
    pixels[1] = m_randomAccessor.oldRawData();
    weights[2] = (int)tqRound( vsub * ( 1.0 - hsub) * 255 );
    m_randomAccessor.moveTo(x, y+1);
    pixels[2] = m_randomAccessor.oldRawData();
    weights[3] = (int)tqRound( hsub * vsub * 255 );
    m_randomAccessor.moveTo(x+1, y+1);
    pixels[3] = m_randomAccessor.oldRawData();
    m_device->colorSpace()->mixColors(pixels, weights, 4, dst);
}

void KisRandomSubAccessorPixel::sampledRawData(TQ_UINT8* dst)
{
    const TQ_UINT8* pixels[4];
    TQ_UINT8 weights[4];
    int x = (int)floor(m_currentPoint.x());
    int y = (int)floor(m_currentPoint.y());
    double hsub = m_currentPoint.x() - x;
    if(hsub < 0.0 ) hsub = 1.0 + hsub;
    double vsub = m_currentPoint.y() - y;
    if(vsub < 0.0 ) vsub = 1.0 + vsub;
    weights[0] = (int)tqRound( ( 1.0 - hsub) * ( 1.0 - vsub) * 255 );
    m_randomAccessor.moveTo(x, y);
    pixels[0] = m_randomAccessor.rawData();
    weights[1] = (int)tqRound( ( 1.0 - vsub) * hsub * 255 );
    m_randomAccessor.moveTo(x+1, y);
    pixels[1] = m_randomAccessor.rawData();
    weights[2] = (int)tqRound( vsub * ( 1.0 - hsub) * 255 );
    m_randomAccessor.moveTo(x, y+1);
    pixels[2] = m_randomAccessor.rawData();
    weights[3] = (int)tqRound( hsub * vsub * 255 );
    m_randomAccessor.moveTo(x+1, y+1);
    pixels[3] = m_randomAccessor.rawData();
    m_device->colorSpace()->mixColors(pixels, weights, 4, dst);
}