Changeset View
Changeset View
Standalone View
Standalone View
libs/image/KisPrecisePaintDeviceWrapper.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 Dmitry Kazakov <dimula73@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "KisPrecisePaintDeviceWrapper.h" | ||||
20 | | ||||
21 | #include <QRegion> | ||||
22 | #include "kis_paint_device.h" | ||||
23 | #include "kis_painter.h" | ||||
24 | #include "kis_sequential_iterator.h" | ||||
25 | | ||||
26 | #include <KoColorSpaceRegistry.h> | ||||
27 | #include <KoColorModelStandardIds.h> | ||||
28 | | ||||
29 | #include <KoColorSpaceMaths.h> | ||||
30 | | ||||
31 | struct KisPrecisePaintDeviceWrapper::Private | ||||
32 | { | ||||
33 | KisPaintDeviceSP srcDevice; | ||||
34 | KisPaintDeviceSP precDevice; | ||||
35 | | ||||
36 | QRegion preparedRegion; | ||||
37 | const KoColorSpace *precColorSpace = 0; | ||||
38 | | ||||
39 | int keepRectsHistory = 50; | ||||
40 | }; | ||||
41 | | ||||
42 | | ||||
43 | KisPrecisePaintDeviceWrapper::KisPrecisePaintDeviceWrapper(KisPaintDeviceSP device, int keepRectsHistory) | ||||
44 | : m_d(new Private) | ||||
45 | { | ||||
46 | m_d->srcDevice = device; | ||||
47 | m_d->keepRectsHistory = keepRectsHistory; | ||||
48 | | ||||
49 | const KoColorSpace *baseSpace = device->colorSpace(); | ||||
50 | const bool useRoundingCorrection = baseSpace->colorDepthId() == Integer8BitsColorDepthID; | ||||
51 | | ||||
52 | if (useRoundingCorrection) { | ||||
53 | m_d->precColorSpace = | ||||
54 | KoColorSpaceRegistry::instance()->colorSpace( | ||||
55 | baseSpace->colorModelId().id(), | ||||
56 | Integer16BitsColorDepthID.id(), | ||||
57 | baseSpace->profile()); | ||||
58 | m_d->precDevice = new KisPaintDevice(m_d->precColorSpace); | ||||
59 | } else { | ||||
60 | // just use source device as a precise operation device | ||||
61 | m_d->precDevice = device; | ||||
62 | m_d->precColorSpace = device->colorSpace(); | ||||
63 | } | ||||
64 | } | ||||
65 | | ||||
66 | KisPrecisePaintDeviceWrapper::~KisPrecisePaintDeviceWrapper() | ||||
67 | { | ||||
68 | } | ||||
69 | | ||||
70 | const KoColorSpace *KisPrecisePaintDeviceWrapper::preciseColorSpace() const | ||||
71 | { | ||||
72 | return m_d->precColorSpace; | ||||
73 | } | ||||
74 | | ||||
75 | KisPaintDeviceSP KisPrecisePaintDeviceWrapper::sourceDevice() const | ||||
76 | { | ||||
77 | return m_d->srcDevice; | ||||
78 | } | ||||
79 | | ||||
80 | KisPaintDeviceSP KisPrecisePaintDeviceWrapper::preciseDevice() const | ||||
81 | { | ||||
82 | return m_d->precDevice; | ||||
83 | } | ||||
84 | | ||||
85 | QRegion KisPrecisePaintDeviceWrapper::cachedRegion() const | ||||
86 | { | ||||
87 | return m_d->precDevice == m_d->srcDevice ? m_d->srcDevice->extent() : m_d->preparedRegion; | ||||
88 | } | ||||
89 | | ||||
90 | void KisPrecisePaintDeviceWrapper::resetCachedRegion() | ||||
91 | { | ||||
92 | m_d->preparedRegion = QRegion(); | ||||
93 | } | ||||
94 | | ||||
95 | void KisPrecisePaintDeviceWrapper::readRect(const QRect &rect) | ||||
96 | { | ||||
97 | readRects({rect}); | ||||
98 | } | ||||
99 | | ||||
100 | void KisPrecisePaintDeviceWrapper::writeRect(const QRect &rc) | ||||
101 | { | ||||
102 | if (m_d->precDevice == m_d->srcDevice) return; | ||||
103 | const int channelCount = m_d->precColorSpace->channelCount(); | ||||
104 | | ||||
105 | KisSequentialIterator srcIt(m_d->srcDevice, rc); | ||||
106 | KisSequentialConstIterator precIt(m_d->precDevice, rc); | ||||
107 | | ||||
108 | while (srcIt.nextPixel() && precIt.nextPixel()) { | ||||
109 | quint8 *srcPtr = reinterpret_cast<quint8*>(srcIt.rawData()); | ||||
110 | const quint16 *precPtr = reinterpret_cast<const quint16*>(precIt.rawDataConst()); | ||||
111 | | ||||
112 | for (int i = 0; i < channelCount; i++) { | ||||
113 | *(srcPtr + i) = KoColorSpaceMaths<quint16, quint8>::scaleToA(*(precPtr + i)); | ||||
114 | } | ||||
115 | } | ||||
116 | } | ||||
117 | | ||||
118 | void KisPrecisePaintDeviceWrapper::readRects(const QVector<QRect> &rects) | ||||
119 | { | ||||
120 | if (m_d->precDevice == m_d->srcDevice) return; | ||||
121 | | ||||
122 | QRegion requestedRects; | ||||
123 | Q_FOREACH (const QRect &rc, rects) { | ||||
124 | requestedRects += rc; | ||||
125 | } | ||||
126 | | ||||
127 | QRegion diff(requestedRects); | ||||
128 | diff -= m_d->preparedRegion; | ||||
129 | | ||||
130 | const int channelCount = m_d->precColorSpace->channelCount(); | ||||
131 | | ||||
132 | Q_FOREACH (const QRect &rc, diff.rects()) { | ||||
133 | KisSequentialConstIterator srcIt(m_d->srcDevice, rc); | ||||
134 | KisSequentialIterator precIt(m_d->precDevice, rc); | ||||
135 | | ||||
136 | while (srcIt.nextPixel() && precIt.nextPixel()) { | ||||
137 | const quint8 *srcPtr = reinterpret_cast<const quint8*>(srcIt.rawDataConst()); | ||||
138 | quint16 *precPtr = reinterpret_cast<quint16*>(precIt.rawData()); | ||||
139 | | ||||
140 | for (int i = 0; i < channelCount; i++) { | ||||
141 | *(precPtr + i) = KoColorSpaceMaths<quint8, quint16>::scaleToA(*(srcPtr + i)); | ||||
142 | } | ||||
143 | } | ||||
144 | } | ||||
145 | | ||||
146 | /** | ||||
147 | * Don't let the region grow too much. When the region has too many | ||||
148 | * rects, it becomes really slow | ||||
149 | */ | ||||
150 | if (m_d->preparedRegion.rectCount() > m_d->keepRectsHistory) { | ||||
151 | m_d->preparedRegion = requestedRects; | ||||
152 | } else { | ||||
153 | m_d->preparedRegion += requestedRects; | ||||
154 | } | ||||
155 | } | ||||
156 | | ||||
157 | void KisPrecisePaintDeviceWrapper::writeRects(const QVector<QRect> &rects) | ||||
158 | { | ||||
159 | if (m_d->precDevice == m_d->srcDevice) return; | ||||
160 | | ||||
161 | Q_FOREACH (const QRect &rc, rects) { | ||||
162 | writeRect(rc); | ||||
163 | } | ||||
164 | } | ||||
165 | |