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 "KisFastDeviceProcessingUtils.h" | ||||
24 | | ||||
25 | #include <KoColor.h> | ||||
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 | m_d->precDevice->setDefaultPixel(device->defaultPixel().convertedTo(m_d->precColorSpace)); | ||||
60 | m_d->precDevice->setDefaultBounds(device->defaultBounds()); | ||||
61 | m_d->precDevice->moveTo(device->offset()); | ||||
62 | | ||||
63 | } else { | ||||
64 | // just use source device as a precise operation device | ||||
65 | m_d->precDevice = device; | ||||
66 | m_d->precColorSpace = device->colorSpace(); | ||||
67 | } | ||||
68 | } | ||||
69 | | ||||
70 | KisPrecisePaintDeviceWrapper::~KisPrecisePaintDeviceWrapper() | ||||
71 | { | ||||
72 | } | ||||
73 | | ||||
74 | const KoColorSpace *KisPrecisePaintDeviceWrapper::preciseColorSpace() const | ||||
75 | { | ||||
76 | return m_d->precColorSpace; | ||||
77 | } | ||||
78 | | ||||
79 | KisPaintDeviceSP KisPrecisePaintDeviceWrapper::sourceDevice() const | ||||
80 | { | ||||
81 | return m_d->srcDevice; | ||||
82 | } | ||||
83 | | ||||
84 | KisPaintDeviceSP KisPrecisePaintDeviceWrapper::preciseDevice() const | ||||
85 | { | ||||
86 | return m_d->precDevice; | ||||
87 | } | ||||
88 | | ||||
89 | QRegion KisPrecisePaintDeviceWrapper::cachedRegion() const | ||||
90 | { | ||||
91 | return m_d->precDevice == m_d->srcDevice ? m_d->srcDevice->extent() : m_d->preparedRegion; | ||||
92 | } | ||||
93 | | ||||
94 | void KisPrecisePaintDeviceWrapper::resetCachedRegion() | ||||
95 | { | ||||
96 | m_d->preparedRegion = QRegion(); | ||||
97 | } | ||||
98 | | ||||
99 | void KisPrecisePaintDeviceWrapper::readRect(const QRect &rect) | ||||
100 | { | ||||
101 | readRects({rect}); | ||||
102 | } | ||||
103 | | ||||
104 | void KisPrecisePaintDeviceWrapper::writeRect(const QRect &rc) | ||||
105 | { | ||||
106 | writeRects({rc}); | ||||
107 | } | ||||
108 | | ||||
109 | namespace { | ||||
110 | | ||||
111 | struct WriteProcessor { | ||||
112 | WriteProcessor(int _channelCount) : channelCount(_channelCount) {} | ||||
113 | | ||||
114 | ALWAYS_INLINE | ||||
115 | void operator()(const quint8 *srcPtr, quint8 *dstPtr) { | ||||
116 | const quint16 *srcChannel = reinterpret_cast<const quint16*>(srcPtr); | ||||
117 | quint8 *dstChannel = reinterpret_cast<quint8*>(dstPtr); | ||||
118 | | ||||
119 | for (int k = 0; k < channelCount; k++) { | ||||
120 | *(dstChannel + k) = KoColorSpaceMaths<quint16, quint8>::scaleToA(*(srcChannel + k)); | ||||
121 | } | ||||
122 | } | ||||
123 | | ||||
124 | const int channelCount; | ||||
125 | }; | ||||
126 | | ||||
127 | struct ReadProcessor { | ||||
128 | ReadProcessor(int _channelCount) : channelCount(_channelCount) {} | ||||
129 | | ||||
130 | ALWAYS_INLINE | ||||
131 | void operator()(const quint8 *srcPtr, quint8 *dstPtr) { | ||||
132 | const quint8 *srcChannel = reinterpret_cast<const quint8*>(srcPtr); | ||||
133 | quint16 *dstChannel = reinterpret_cast<quint16*>(dstPtr); | ||||
134 | | ||||
135 | for (int k = 0; k < channelCount; k++) { | ||||
136 | *(dstChannel + k) = KoColorSpaceMaths<quint8, quint16>::scaleToA(*(srcChannel + k)); | ||||
137 | } | ||||
138 | } | ||||
139 | | ||||
140 | const int channelCount; | ||||
141 | }; | ||||
142 | | ||||
143 | } | ||||
144 | | ||||
145 | void KisPrecisePaintDeviceWrapper::readRects(const QVector<QRect> &rects) | ||||
146 | { | ||||
147 | if (m_d->precDevice == m_d->srcDevice) return; | ||||
148 | | ||||
149 | const QRect srcExtent = m_d->srcDevice->extent(); | ||||
150 | | ||||
151 | QRegion requestedRects; | ||||
152 | Q_FOREACH (const QRect &rc, rects) { | ||||
153 | const QRect croppedRect = rc & srcExtent; | ||||
154 | | ||||
155 | requestedRects += croppedRect; | ||||
156 | } | ||||
157 | | ||||
158 | QRegion diff(requestedRects); | ||||
159 | diff -= m_d->preparedRegion; | ||||
160 | | ||||
161 | if (rects.isEmpty()) return; | ||||
162 | const QPoint firstPoint = rects.first().topLeft(); | ||||
163 | const int channelCount = m_d->precColorSpace->channelCount(); | ||||
164 | | ||||
165 | KisRandomConstAccessorSP srcIt = m_d->srcDevice->createRandomConstAccessorNG(firstPoint.x(), firstPoint.y()); | ||||
166 | KisRandomAccessorSP dstIt = m_d->precDevice->createRandomAccessorNG(firstPoint.x(), firstPoint.y()); | ||||
167 | | ||||
168 | Q_FOREACH (const QRect &rc, diff.rects()) { | ||||
169 | KritaUtils::processTwoDevices(rc, | ||||
170 | srcIt, dstIt, | ||||
171 | m_d->srcDevice->pixelSize(), | ||||
172 | m_d->precDevice->pixelSize(), | ||||
173 | ReadProcessor(channelCount)); | ||||
174 | } | ||||
175 | | ||||
176 | /** | ||||
177 | * Don't let the region grow too much. When the region has too many | ||||
178 | * rects, it becomes really slow | ||||
179 | */ | ||||
180 | if (m_d->preparedRegion.rectCount() > m_d->keepRectsHistory) { | ||||
181 | m_d->preparedRegion = requestedRects; | ||||
182 | } else { | ||||
183 | m_d->preparedRegion += requestedRects; | ||||
184 | } | ||||
185 | } | ||||
186 | | ||||
187 | void KisPrecisePaintDeviceWrapper::writeRects(const QVector<QRect> &rects) | ||||
188 | { | ||||
189 | if (m_d->precDevice == m_d->srcDevice) return; | ||||
190 | | ||||
191 | if (rects.isEmpty()) return; | ||||
192 | const QPoint firstPoint = rects.first().topLeft(); | ||||
193 | const int channelCount = m_d->precColorSpace->channelCount(); | ||||
194 | | ||||
195 | KisRandomConstAccessorSP srcIt = m_d->precDevice->createRandomConstAccessorNG(firstPoint.x(), firstPoint.y()); | ||||
196 | KisRandomAccessorSP dstIt = m_d->srcDevice->createRandomAccessorNG(firstPoint.x(), firstPoint.y()); | ||||
197 | | ||||
198 | Q_FOREACH (const QRect &rc, rects) { | ||||
199 | KritaUtils::processTwoDevices(rc, | ||||
200 | srcIt, dstIt, | ||||
201 | m_d->precDevice->pixelSize(), | ||||
202 | m_d->srcDevice->pixelSize(), | ||||
203 | WriteProcessor(channelCount)); | ||||
204 | } | ||||
205 | } | ||||
206 | |