Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

kis_histogram.cc 6.44 KB
Newer Older
1 2
/*
 *  Copyright (c) 2004 Boudewijn Rempt
3
 *            (c) 2005 Bart Coppens <kde@bartcoppens.be>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19 20
 */

#include <kdebug.h>
21
#include <qdatetime.h> // ### Debug
22 23 24 25

#include "kis_histogram.h"
#include "kis_layer.h"
#include "kis_types.h"
26
#include "kis_iterators_pixel.h"
27
#include "kis_colorspace.h"
28

Boudewijn Rempt's avatar
Boudewijn Rempt committed
29
KisHistogram::KisHistogram(KisLayerSP layer,
30 31
                           KisHistogramProducerSP producer,
                           const enumHistogramType type)
32
{
33
    m_dev = layer.data();
34
    m_type = type;
35
    m_producer = producer;
36
    m_selection = false;
37
    m_channel = 0;
38

39 40
    updateHistogram();
}
41

42 43 44 45 46 47 48 49 50
KisHistogram::KisHistogram(KisPaintDeviceImplSP paintdev,
                           KisHistogramProducerSP producer,
                           const enumHistogramType type)
{
    m_dev = paintdev;
    m_type = type;
    m_producer = producer;
    m_selection = false;
    m_channel = 0;
51

52
    updateHistogram();
53 54 55 56 57 58
}

KisHistogram::~KisHistogram()
{
}

59
void KisHistogram::updateHistogram()
60
{
Casper Boemann's avatar
Casper Boemann committed
61
    Q_INT32 x,y,w,h;
62 63
    m_dev -> exactBounds(x,y,w,h);
    KisRectIteratorPixel srcIt = m_dev -> createRectIterator(x,y,w,h, false);
64
    KisColorSpace* cs = m_dev -> colorSpace();
65 66 67 68 69 70

    QTime t;
    t.start();

    // Let the producer do it's work
    m_producer -> clear();
71 72 73 74 75 76
    int i;
    while ( !srcIt.isDone() ) {
        i = srcIt.nConseqPixels();
        m_producer -> addRegionToBin(srcIt.rawData(), srcIt.selectionMask(), i, cs);
        srcIt += i;
    }
77

Boudewijn Rempt's avatar
Boudewijn Rempt committed
78
    //kdDebug() << t.elapsed() << "ms for histogram (new version)" << endl;
79 80 81 82 83 84

    computeHistogram();
}

void KisHistogram::computeHistogram()
{
85 86 87 88 89 90 91
    m_completeCalculations = calculateForRange(m_producer -> viewFrom(),
            m_producer -> viewFrom() + m_producer -> viewWidth());

    if (m_selection) {
        m_selectionCalculations = calculateForRange(m_selFrom, m_selTo);
    } else {
        m_selectionCalculations.clear();
Casper Boemann's avatar
Casper Boemann committed
92
    }
93

94
#if 1
95
    dump();
96
#endif
97 98
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
KisHistogram::Calculations KisHistogram::calculations() {
    return m_completeCalculations.at(m_channel);
}

KisHistogram::Calculations KisHistogram::selectionCalculations() {
    return m_selectionCalculations.at(m_channel);
}

QValueVector<KisHistogram::Calculations> KisHistogram::calculateForRange(double from, double to) {
    QValueVector<Calculations> calculations;
    uint count = m_producer -> channels().count();

    for (uint i = 0; i < count; i++) {
        calculations.append(calculateSingleRange(i, from, to));
    }

    return calculations;
}

KisHistogram::Calculations KisHistogram::calculateSingleRange(int channel, double from, double to) {
    Calculations c;

    // XXX If from == to, we only want a specific bin, handle that properly!

123
    double max = from, min = to, total = 0.0, mean = 0.0; //, median = 0.0, stddev = 0.0;
124
    Q_UINT32 high = 0, low = (Q_UINT32) -1, count = 0;
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    if (m_producer -> count() == 0) {
        // We won't get anything, even if a range is specified
        // XXX make sure all initial '0' values are correct here!
        return c;
    }

    Q_INT32 totbins = m_producer -> numberOfBins();
    Q_UINT32 current;

    // convert the double range into actual bins:
    double factor = static_cast<double>(totbins) / m_producer -> viewWidth();

    Q_INT32 fromBin = static_cast<Q_INT32>((from - m_producer -> viewFrom()) * factor);
    Q_INT32 toBin = fromBin + static_cast<Q_INT32>((to - from) * factor);

Boudewijn Rempt's avatar
Boudewijn Rempt committed
142 143
    //kdDebug(DBG_AREA_MATH) << "calculating from " << from << " to " << to
    //                       << ", means from bin " << fromBin << " to bin " << toBin << endl;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    // Min, max, count, low, high
    for (Q_INT32 i = fromBin; i < toBin; i++) {
        current = m_producer -> getBinAt(channel, i);
        double pos = static_cast<double>(i) / factor + from;
        if (current > high)
            high = current;
        if (current < low)
            low = current;
        if (current > 0) {
            if (pos < min)
                min = pos;
            if (pos > max)
                max = pos;
        }
        // We do the count here as well.
        // we can't use m_producer -> count() for this, because of the range
        count += current;
        total += current * pos;
    }

    if (count > 0)
        mean = total / count;

    c.m_high = high;
    c.m_low = low;
    c.m_count = count;
    c.m_min = min;
    c.m_max = max;
    c.m_mean = mean;
    c.m_total = total;

    return c;
}

179 180

void KisHistogram::dump() {
181
    kdDebug(DBG_AREA_MATH) << "Histogram\n";
182

183 184 185 186 187 188 189
    switch (m_type) {
    case LINEAR:
        kdDebug(DBG_AREA_MATH) << "Linear histogram\n";
        break;
    case LOGARITHMIC:
        kdDebug(DBG_AREA_MATH) << "Logarithmic histogram\n";
    }
190

191 192
    kdDebug(DBG_AREA_MATH) << "Dumping channel " << m_channel << endl;
    Calculations c = calculations();
193

194
/*        for( int i = 0; i <256; ++i ) {
195 196 197
        kdDebug(DBG_AREA_MATH) << "Value "
              << QString().setNum(i)
              << ": "
Casper Boemann's avatar
Casper Boemann committed
198
              <<  QString().setNum(m_values[i])
199
              << "\n";
200
        }*/
201 202
    kdDebug(DBG_AREA_MATH) << "\n";

203 204 205 206 207 208 209 210 211
    kdDebug(DBG_AREA_MATH) << "Max: " << QString().setNum(c.getMax()) << "\n";
    kdDebug(DBG_AREA_MATH) << "Min: " << QString().setNum(c.getMin()) << "\n";
    kdDebug(DBG_AREA_MATH) << "High: " << QString().setNum(c.getHighest()) << "\n";
    kdDebug(DBG_AREA_MATH) << "Low: " << QString().setNum(c.getLowest()) << "\n";
    kdDebug(DBG_AREA_MATH) << "Mean: " << m_producer -> positionToString(c.getMean()) << "\n";
    kdDebug(DBG_AREA_MATH) << "Total: " << QString().setNum(c.getTotal()) << "\n";
//    kdDebug(DBG_AREA_MATH) << "Median: " << QString().setNum(m_median) << "\n";
//    kdDebug(DBG_AREA_MATH) << "Stddev: " << QString().setNum(m_stddev) << "\n";
//    kdDebug(DBG_AREA_MATH) << "percentile: " << QString().setNum(m_percentile) << "\n";
212 213

    kdDebug(DBG_AREA_MATH) << "\n";
214
}