File indexing completed on 2024-05-12 15:59:36

0001 /*
0002  *  SPDX-FileCopyrightText: 2005 Bart Coppens <kde@bartcoppens.be>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #ifndef _KO_HISTOGRAM_PRODUCER_
0008 #define _KO_HISTOGRAM_PRODUCER_
0009 
0010 #include <QtGlobal>
0011 
0012 #include "kritapigment_export.h"
0013 
0014 #include <KoGenericRegistry.h>
0015 #include <KoID.h>
0016 
0017 class QString;
0018 class KoChannelInfo;
0019 class KoColorSpace;
0020 
0021 /**
0022  * This class is an interface used in the generation of a histogram. It is a container of
0023  * data, all mathematically interesting things will calculated by an histogram.
0024  *
0025  * The default view will be the entire range each color can be in. And don't let the
0026  * numberOfBins return anything else then 256 unless you have a very good reason for it.
0027  *
0028  * About the views: a view is a zoom combined with a start level: the entire
0029  * range of a channel is 0.0 - 1.0: this is the position. Combined with a zoom, we can
0030  * calculate what part of a channel will fall in a bin. This gives us an interface to
0031  * that the views that is not dependent of the actual colorspace of the histogram.
0032  * The 'size' value is the size, again from 0.0 to 1.0 of the displayed range.
0033  *
0034  * For comfort of the GUI, and because it is logical, channels are accessed in the order
0035  * in which they are found in the channels() method. This is potentially different from
0036  * the order in which they are internally ordered!
0037  **/
0038 class KRITAPIGMENT_EXPORT KoHistogramProducer
0039 {
0040 public:
0041     KoHistogramProducer() : m_skipTransparent(true), m_skipUnselected(true) {}
0042     virtual ~KoHistogramProducer() {}
0043 
0044     // Methods to change the bins
0045 
0046     /** Clears the data in this producer, but keeps its other settings */
0047     virtual void clear() = 0;
0048 
0049     /**
0050      * Adds the values from the specified array of pixels to the bins -- does not
0051      * reset anything.
0052      *
0053      * @param pixels A pointer an array of pixeldata in the given colorspace
0054      * @param selectionMask a pointer to an array of bytes, where 0 is unselected and 1-255 is degree of selectedness. The array
0055      *                      must be just as long as the array of pixels.
0056      * @param nPixels The number of pixels
0057      * @param colorSpace the colorspace that can decode the pixel data.
0058      */
0059     virtual void addRegionToBin(const quint8 * pixels, const quint8 * selectionMask, quint32 nPixels, const KoColorSpace* colorSpace) = 0;
0060 
0061     // Methods to set what exactly is being added to the bins
0062     virtual void setView(qreal from, qreal width) = 0;
0063     virtual void setSkipTransparent(bool set) {
0064         m_skipTransparent = set;
0065     }
0066     virtual void setSkipUnselected(bool set) {
0067         m_skipUnselected = set;
0068     }
0069 
0070     // Methods with general information about this specific producer
0071     virtual const KoID& id() const = 0;
0072     virtual QList<KoChannelInfo *> channels() = 0;
0073     virtual qint32 numberOfBins() = 0;
0074     virtual QString positionToString(qreal pos) const = 0;
0075     virtual qreal viewFrom() const = 0;
0076     virtual qreal viewWidth() const = 0;
0077     virtual qreal maximalZoom() const = 0;
0078 
0079     // Methods to get information on the data we have seen
0080     virtual qint32 count() = 0;
0081     virtual qint32 getBinAt(qint32 channel, qint32 position) = 0;
0082     virtual qint32 outOfViewLeft(qint32 channel) = 0;
0083     virtual qint32 outOfViewRight(qint32 channel) = 0;
0084 protected:
0085     bool m_skipTransparent;
0086     bool m_skipUnselected;
0087 };
0088 
0089 class KRITAPIGMENT_EXPORT KoHistogramProducerFactory
0090 {
0091 public:
0092     explicit KoHistogramProducerFactory(const KoID &id) : m_id(id) {}
0093     virtual ~KoHistogramProducerFactory() {}
0094 
0095     /// Factory method, generates a new KoHistogramProducer
0096     virtual KoHistogramProducer *generate() = 0;
0097 
0098     /// Returns if a colorspace can be used with this producer
0099     virtual bool isCompatibleWith(const KoColorSpace* colorSpace, bool strict = false) const = 0;
0100 
0101     /// Returns a float in the [0.0, 1.0] range, 0.0 means this is a very generic method
0102     virtual float preferrednessLevelWith(const KoColorSpace* colorSpace) const = 0;
0103 
0104     virtual QString id() const {
0105         return m_id.id();
0106     }
0107 
0108     virtual QString name() const {
0109         return m_id.name();
0110     }
0111 protected:
0112     KoID m_id;
0113 };
0114 
0115 
0116 class KRITAPIGMENT_EXPORT KoHistogramProducerFactoryRegistry
0117         : public KoGenericRegistry<KoHistogramProducerFactory*>
0118 {
0119 public:
0120     KoHistogramProducerFactoryRegistry();
0121     ~KoHistogramProducerFactoryRegistry() override;
0122     static KoHistogramProducerFactoryRegistry* instance();
0123     /// returns a list, sorted by preference: higher preference comes first
0124     QList<QString> keysCompatibleWith(const KoColorSpace* colorSpace, bool isStrict=false) const;
0125 
0126 private:
0127     KoHistogramProducerFactoryRegistry(const KoHistogramProducerFactoryRegistry&);
0128     KoHistogramProducerFactoryRegistry operator=(const KoHistogramProducerFactoryRegistry&);
0129 };
0130 
0131 #endif // _KO_HISTOGRAM_PRODUCER