krita/image

kis_color_transformation_filter.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004, 2009 Cyrille Berger <cberger@cberger.net>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kis_color_transformation_filter.h"
00021 
00022 #include <KoColorTransformation.h>
00023 #include <KoProgressUpdater.h>
00024 #include <KoUpdater.h>
00025 
00026 #include <kis_iterators_pixel.h>
00027 #include <kis_processing_information.h>
00028 #include <kis_paint_device.h>
00029 #include <kis_selection.h>
00030 
00031 #ifndef NDEBUG
00032 #include <QTime>
00033 #endif
00034 
00035 typedef QPointer<KoUpdater> KoUpdaterPtr;
00036 
00037 KisColorTransformationFilter::KisColorTransformationFilter(const KoID& id, const KoID & category, const QString & entry) : KisFilter(id, category, entry)
00038 {
00039 }
00040 
00041 KisColorTransformationFilter::~KisColorTransformationFilter()
00042 {
00043 }
00044 
00045 void KisColorTransformationFilter::process(KisConstProcessingInformation srcInfo,
00046         KisProcessingInformation dstInfo,
00047         const QSize& size,
00048         const KisFilterConfiguration* config,
00049         KoUpdater* progressUpdater
00050                                           ) const
00051 {
00052     const KisPaintDeviceSP src = srcInfo.paintDevice();
00053     KisPaintDeviceSP dst = dstInfo.paintDevice();
00054     QPoint dstTopLeft = dstInfo.topLeft();
00055     QPoint srcTopLeft = srcInfo.topLeft();
00056     Q_UNUSED(config);
00057     Q_ASSERT(!src.isNull());
00058     Q_ASSERT(!dst.isNull());
00059 
00060     if (progressUpdater) {
00061         progressUpdater->setRange(0, size.height());
00062     }
00063 
00064     const KoColorSpace * cs = src->colorSpace();
00065     KoColorTransformation* colorTransformation = createTransformation(cs, config);
00066     if (!colorTransformation) return;
00067 
00068     bool hasSelection = srcInfo.selection();
00069     KisHLineConstIteratorPixel srcIt = src->createHLineConstIterator(srcTopLeft.x(), srcTopLeft.y(), size.width(), srcInfo.selection());
00070     KisHLineIteratorPixel dstIt = dst->createHLineIterator(dstTopLeft.x(), dstTopLeft.y(), size.width(), dstInfo.selection());
00071 
00072     for (int row = 0; row < size.height(); ++row) {
00073         while (! srcIt.isDone()) {
00074             int srcItConseq = srcIt.nConseqHPixels();
00075             int dstItConseq = dstIt.nConseqHPixels();
00076             int conseqPixels = qMin(srcItConseq, dstItConseq);
00077 
00078             int pixels = 0;
00079             int pixelsSrc = 0;
00080 
00081             if (hasSelection) {
00082                 // Get largest horizontal row of selected pixels
00083                 const quint8* oldRawData = srcIt.oldRawData();
00084                 while (srcIt.isSelected() && pixels < conseqPixels) {
00085                     ++pixels;
00086                     ++srcIt;
00087                     ++pixelsSrc;
00088                 }
00089                 colorTransformation->transform(oldRawData, dstIt.rawData(), pixels);
00090 
00091                 // We apparently found a non-selected pixels, or the row
00092                 // was done; get the stretch of non-selected pixels
00093                 while (!srcIt.isSelected() && pixels < conseqPixels) {
00094                     ++ pixels;
00095                     ++srcIt;
00096                     ++pixelsSrc;
00097                 }
00098             } else {
00099                 pixels = conseqPixels;
00100                 colorTransformation->transform(srcIt.oldRawData(), dstIt.rawData(), pixels);
00101             }
00102 
00103             // Update progress
00104             srcIt += (pixels - pixelsSrc);
00105             dstIt += pixels;
00106         }
00107         if (progressUpdater) progressUpdater->setValue(row);
00108 
00109         srcIt.nextRow();
00110         dstIt.nextRow();
00111     }
00112     delete colorTransformation;
00113 
00114 
00115 }