Krita Source Code Documentation
Loading...
Searching...
No Matches
KoBasicHistogramProducers.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2005 Bart Coppens <kde@bartcoppens.be>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
8
9#include <QString>
10#include <klocalizedstring.h>
11
12#include <KoConfig.h>
13#ifdef HAVE_OPENEXR
14#include <half.h>
15#endif
16
17// #include "Ko_global.h"
18#include "KoIntegerMaths.h"
19#include "KoChannelInfo.h"
20
21static const KoColorSpace* m_labCs = 0;
22
23
24KoBasicHistogramProducer::KoBasicHistogramProducer(const KoID& id, int channelCount, int nrOfBins)
25 : m_channels(channelCount)
26 , m_nrOfBins(nrOfBins)
27 , m_colorSpace(0)
28 , m_id(id)
29{
30 m_bins.resize(m_channels);
31 for (int i = 0; i < m_channels; i++)
32 m_bins[i].resize(m_nrOfBins);
33 m_outLeft.resize(m_channels);
34 m_outRight.resize(m_channels);
35 m_count = 0;
36 m_from = 0.0;
37 m_width = 1.0;
38}
39
41 : m_nrOfBins(nrOfBins),
42 m_colorSpace(cs),
43 m_id(id)
44{
45 Q_ASSERT(cs);
47
48 m_bins.resize(m_channels);
49 for (int i = 0; i < m_channels; i++)
50 m_bins[i].resize(m_nrOfBins);
51 m_outLeft.resize(m_channels);
52 m_outRight.resize(m_channels);
53 m_count = 0;
54 m_from = 0.0;
55 m_width = 1.0;
56}
57
58
60{
61 m_count = 0;
62 for (int i = 0; i < m_channels; i++) {
63 for (int j = 0; j < m_nrOfBins; j++) {
64 m_bins[i][j] = 0;
65 }
66 m_outRight[i] = 0;
67 m_outLeft[i] = 0;
68 }
69}
70
72{
73 // This function assumes that the pixel is has no 'gaps'. That is to say: if we start
74 // at byte 0, we can get to the end of the pixel by adding consecutive size()s of
75 // the channels
77 uint count = c.count();
78 int currentPos = 0;
79
80 for (uint i = 0; i < count; i++) {
81 for (uint j = 0; j < count; j++) {
82 if (c[j]->pos() == currentPos) {
83 m_external.append(j);
84 break;
85 }
86 }
87 currentPos += c.at(m_external.at(m_external.count() - 1))->size();
88 }
89}
90
91// ------------ U8 ---------------------
92
97
99{
100 return QString("%1").arg(static_cast<quint8>(pos * UINT8_MAX));
101}
102
103void KoBasicU8HistogramProducer::addRegionToBin(const quint8 * pixels, const quint8 * selectionMask, quint32 nPixels, const KoColorSpace *cs)
104{
105 quint32 dstPixelSize = m_colorSpace->pixelSize();
106 quint8 *dstPixels = new quint8[nPixels * dstPixelSize];
108
109 if (selectionMask) {
110 quint8 *dst = dstPixels;
111 while (nPixels > 0) {
112 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
113
114 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
115 m_bins[i][m_colorSpace->scaleToU8(dst,i)]++;
116 }
117 m_count++;
118 }
119 dst += dstPixelSize;
120 selectionMask++;
121 nPixels--;
122 }
123 } else {
124 quint8 *dst = dstPixels;
125 while (nPixels > 0) {
126 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
127
128 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
129 m_bins[i][m_colorSpace->scaleToU8(dst,i)]++;
130 }
131 m_count++;
132 }
133 dst += dstPixelSize;
134 nPixels--;
135 }
136 }
137 delete[] dstPixels;
138}
139
140// ------------ U16 ---------------------
141
146
148{
149 return QString("%1").arg(static_cast<quint8>(pos * UINT8_MAX));
150}
151
153{
154 return 1.0 / 255.0;
155}
156
157void KoBasicU16HistogramProducer::addRegionToBin(const quint8 * pixels, const quint8 * selectionMask, quint32 nPixels, const KoColorSpace *cs)
158{
159 // The view
160 quint16 from = static_cast<quint16>(m_from * UINT16_MAX);
161 quint16 width = static_cast<quint16>(m_width * UINT16_MAX + 0.5); // We include the end
162 quint16 to = from + width;
163 qreal factor = 255.0 / width;
164
165 quint32 dstPixelSize = m_colorSpace->pixelSize();
166 quint8 *dstPixels = new quint8[nPixels * dstPixelSize];
168 quint8 *dst = dstPixels;
170
171 if (selectionMask) {
172 while (nPixels > 0) {
173 if (!((m_skipUnselected && *selectionMask == 0) || (m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8))) {
175 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
176 quint16 value = channels[i]*UINT16_MAX;
177 if (value > to)
178 m_outRight[i]++;
179 else if (value < from)
180 m_outLeft[i]++;
181 else
182 m_bins[i][static_cast<quint8>((value - from) * factor)]++;
183 }
184 m_count++;
185 }
186 dst += dstPixelSize;
187 selectionMask++;
188 nPixels--;
189 }
190 } else {
191 while (nPixels > 0) {
192 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
194 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
195 quint16 value = channels[i]*UINT16_MAX;
196
197 if (value > to)
198 m_outRight[i]++;
199 else if (value < from)
200 m_outLeft[i]++;
201 else
202 m_bins[i][static_cast<quint8>((value - from) * factor)]++;
203 }
204 m_count++;
205 }
206 dst += dstPixelSize;
207 nPixels--;
208 }
209 }
210 delete[] dstPixels;
211}
212
213// ------------ Float32 ---------------------
218
220{
221 return QString("%1").arg(static_cast<float>(pos)); // XXX I doubt this is correct!
222}
223
225{
226 // XXX What _is_ the maximal zoom here? I don't think there is one with floats, so this seems a fine compromise for the moment
227 return 1.0 / 255.0;
228}
229
230void KoBasicF32HistogramProducer::addRegionToBin(const quint8 * pixels, const quint8 * selectionMask, quint32 nPixels, const KoColorSpace *cs)
231{
232 // The view
233 float from = static_cast<float>(m_from);
234 float width = static_cast<float>(m_width);
235 float to = from + width;
236 float factor = 255.0 / width;
237
238 quint32 dstPixelSize = m_colorSpace->pixelSize();
239 quint8 *dstPixels = new quint8[nPixels * dstPixelSize];
241 quint8 *dst = dstPixels;
243
244 if (selectionMask) {
245 while (nPixels > 0) {
246 if (!((m_skipUnselected && *selectionMask == 0) || (m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8))) {
248 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
249 float value = channels[i];
250 if (value > to)
251 m_outRight[i]++;
252 else if (value < from)
253 m_outLeft[i]++;
254 else
255 m_bins[i][static_cast<quint8>((value - from) * factor)]++;
256 }
257 m_count++;
258 }
259 dst += dstPixelSize;
260 selectionMask++;
261 nPixels--;
262
263 }
264 } else {
265 while (nPixels > 0) {
266 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
268 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
269 float value = channels[i];
270 if (value > to)
271 m_outRight[i]++;
272 else if (value < from)
273 m_outLeft[i]++;
274 else
275 m_bins[i][static_cast<quint8>((value - from) * factor)]++;
276 }
277 m_count++;
278 }
279 dst += dstPixelSize;
280 nPixels--;
281
282 }
283 }
284 delete[] dstPixels;
285}
286
287#ifdef HAVE_OPENEXR
288// ------------ Float16 Half ---------------------
289KoBasicF16HalfHistogramProducer::KoBasicF16HalfHistogramProducer(const KoID& id,
290 const KoColorSpace *cs)
291 : KoBasicHistogramProducer(id, 256, cs)
292{
293}
294
295QString KoBasicF16HalfHistogramProducer::positionToString(qreal pos) const
296{
297 return QString("%1").arg(static_cast<float>(pos)); // XXX I doubt this is correct!
298}
299
300qreal KoBasicF16HalfHistogramProducer::maximalZoom() const
301{
302 // XXX What _is_ the maximal zoom here? I don't think there is one with floats, so this seems a fine compromise for the moment
303 return 1.0 / 255.0;
304}
305
306void KoBasicF16HalfHistogramProducer::addRegionToBin(const quint8 * pixels, const quint8 * selectionMask, quint32 nPixels, const KoColorSpace *cs)
307{
308 // The view
309 float from = static_cast<float>(m_from);
310 float width = static_cast<float>(m_width);
311 float to = from + width;
312 float factor = 255.0 / width;
313
314 quint32 dstPixelSize = m_colorSpace->pixelSize();
315 quint8 *dstPixels = new quint8[nPixels * dstPixelSize];
317 quint8 *dst = dstPixels;
318 QVector<float> channels(m_colorSpace->channelCount());
319
320 if (selectionMask) {
321 while (nPixels > 0) {
322 if (!((m_skipUnselected && *selectionMask == 0) || (m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8))) {
323 m_colorSpace->normalisedChannelsValue(dst,channels);
324 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
325 float value = channels[i];
326 if (value > to)
327 m_outRight[i]++;
328 else if (value < from)
329 m_outLeft[i]++;
330 else
331 m_bins[i][static_cast<quint8>((value - from) * factor)]++;
332 }
333 m_count++;
334 }
335 dst += dstPixelSize;
336 selectionMask++;
337 nPixels--;
338 }
339 } else {
340 while (nPixels > 0) {
341 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
342 m_colorSpace->normalisedChannelsValue(dst,channels);
343 for (int i = 0; i < (int)m_colorSpace->channelCount(); i++) {
344 float value = channels[i];
345 if (value > to)
346 m_outRight[i]++;
347 else if (value < from)
348 m_outLeft[i]++;
349 else
350 m_bins[i][static_cast<quint8>((value - from) * factor)]++;
351 }
352 m_count++;
353 }
354 dst += dstPixelSize;
355 nPixels--;
356 }
357 }
358 delete[] dstPixels;
359}
360#endif
361
362// ------------ Generic RGB ---------------------
364 : KoBasicHistogramProducer(KoID("GENRGBHISTO", i18n("Generic RGB Histogram")), 3, 256)
365{
366 /* we set 0 as colorspace, because we are not based on a specific colorspace. This
367 is no problem for the superclass since we override channels() */
368 m_channelsList.append(new KoChannelInfo(i18n("R"), 0, 0, KoChannelInfo::COLOR, KoChannelInfo::UINT8, 1, QColor(255, 0, 0)));
369 m_channelsList.append(new KoChannelInfo(i18n("G"), 1, 1, KoChannelInfo::COLOR, KoChannelInfo::UINT8, 1, QColor(0, 255, 0)));
370 m_channelsList.append(new KoChannelInfo(i18n("B"), 2, 2, KoChannelInfo::COLOR, KoChannelInfo::UINT8, 1, QColor(0, 0, 255)));
371}
378
383
385{
386 return QString("%1").arg(static_cast<quint8>(pos * UINT8_MAX));
387}
388
390{
391 return 1.0;
392}
393
394
395void KoGenericRGBHistogramProducer::addRegionToBin(const quint8 * pixels, const quint8 * selectionMask, quint32 nPixels, const KoColorSpace *cs)
396{
397 for (int i = 0; i < m_channels; i++) {
398 m_outRight[i] = 0;
399 m_outLeft[i] = 0;
400 }
401
402 QColor c;
403 qint32 pSize = cs->pixelSize();
404 if (selectionMask) {
405 while (nPixels > 0) {
406 if (!((m_skipUnselected && *selectionMask == 0) || (m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8))) {
407 cs->toQColor(pixels, &c);
408 m_bins[0][c.red()]++;
409 m_bins[1][c.green()]++;
410 m_bins[2][c.blue()]++;
411
412 m_count++;
413 }
414 pixels += pSize;
415 selectionMask++;
416 nPixels--;
417 }
418
419 } else {
420 while (nPixels > 0) {
421
422 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
423 cs->toQColor(pixels, &c);
424 m_bins[0][c.red()]++;
425 m_bins[1][c.green()]++;
426 m_bins[2][c.blue()]++;
427
428 m_count++;
429 }
430 pixels += pSize;
431 nPixels--;
432 }
433 }
434}
435
440
441// ------------ Generic L*a*b* ---------------------
443 : KoBasicHistogramProducer(KoID("GENLABHISTO", i18n("L*a*b* Histogram")), 3, 256)
444{
445 /* we set 0 as colorspace, because we are not based on a specific colorspace. This
446 is no problem for the superclass since we override channels() */
450
451 if (!m_labCs) {
453 }
455}
462
467
469{
470 return QString("%1").arg(static_cast<quint16>(pos * UINT16_MAX));
471}
472
474{
475 return 1.0;
476}
477
478
479void KoGenericLabHistogramProducer::addRegionToBin(const quint8 *pixels, const quint8 *selectionMask, quint32 nPixels, const KoColorSpace *cs)
480{
481 for (int i = 0; i < m_channels; i++) {
482 m_outRight[i] = 0;
483 m_outLeft[i] = 0;
484 }
485
486 qint32 dstPixelSize = m_colorSpace->pixelSize();
487
488 quint8 *dstPixels = new quint8[nPixels * dstPixelSize];
490
491 qint32 pSize = cs->pixelSize();
492
493 if (selectionMask) {
494 while (nPixels > 0) {
495 if (!((m_skipUnselected && *selectionMask == 0) || (m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8))) {
496 m_count++;
497 }
498 pixels += pSize;
499 selectionMask++;
500 nPixels--;
501 }
502 } else {
503 quint8 *dst = dstPixels;
504 while (nPixels > 0) {
505 if (!(m_skipTransparent && cs->opacityU8(pixels) == OPACITY_TRANSPARENT_U8)) {
506
507 m_bins[0][m_colorSpace->scaleToU8(dst, 0)]++;
508 m_bins[1][m_colorSpace->scaleToU8(dst, 1)]++;
509 m_bins[2][m_colorSpace->scaleToU8(dst, 2)]++;
510
511 m_count++;
512 }
513 dst+= dstPixelSize;
514 nPixels--;
515 }
516 }
517 delete[] dstPixels;
518}
519
float value(const T *src, size_t ch)
static const KoColorSpace * m_labCs
const quint8 OPACITY_TRANSPARENT_U8
#define UINT16_MAX
unsigned int uint
#define UINT8_MAX
void addRegionToBin(const quint8 *pixels, const quint8 *selectionMask, quint32 nPixels, const KoColorSpace *colorSpace) override
KoBasicF32HistogramProducer(const KoID &id, const KoColorSpace *colorSpace)
QString positionToString(qreal pos) const override
QList< KoChannelInfo * > channels() override
KoBasicHistogramProducer(const KoID &id, int channelCount, int nrOfBins)
void addRegionToBin(const quint8 *pixels, const quint8 *selectionMask, quint32 nPixels, const KoColorSpace *colorSpace) override
KoBasicU16HistogramProducer(const KoID &id, const KoColorSpace *colorSpace)
QString positionToString(qreal pos) const override
KoBasicU8HistogramProducer(const KoID &id, const KoColorSpace *colorSpace)
void addRegionToBin(const quint8 *pixels, const quint8 *selectionMask, quint32 nPixels, const KoColorSpace *colorSpace) override
QString positionToString(qreal pos) const override
@ COLOR
The channel represents a color.
@ UINT8
use this for an unsigned integer 8bits channel
virtual quint32 pixelSize() const =0
virtual void toQColor(const quint8 *src, QColor *c) const =0
virtual quint8 scaleToU8(const quint8 *srcPixel, qint32 channelPos) const =0
virtual quint32 channelCount() const =0
virtual void normalisedChannelsValue(const quint8 *pixel, QVector< float > &channels) const =0
virtual quint8 opacityU8(const quint8 *pixel) const =0
virtual bool convertPixelsTo(const quint8 *src, quint8 *dst, const KoColorSpace *dstColorSpace, quint32 numPixels, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) const
QList< KoChannelInfo * > channels() override
void addRegionToBin(const quint8 *pixels, const quint8 *selectionMask, quint32 nPixels, const KoColorSpace *colorSpace) override
QString positionToString(qreal pos) const override
QString positionToString(qreal pos) const override
void addRegionToBin(const quint8 *pixels, const quint8 *selectionMask, quint32 nPixels, const KoColorSpace *colorSpace) override
QList< KoChannelInfo * > channels() override
Definition KoID.h:30
const KoColorSpace * lab16(const QString &profileName=QString())
static KoColorSpaceRegistry * instance()