summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/viewplugins/separate_channels/kis_channel_separator.cc
blob: ba8c6b84a400dd53e918b643aad87cc65e283a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * This file is part of Chalk
 *
 * Copyright (c) 2005 Michael Thaler <[email protected]>
 *
 * ported from Gimp, Copyright (C) 1997 Eiichi Takamori <[email protected]>
 * original pixelize.c for GIMP 0.54 by Tracy Scott
 *
 *  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
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <limits.h>

#include <stdlib.h>
#include <vector>

#include <tdelocale.h>
#include <kiconloader.h>
#include <kinstance.h>
#include <tdemessagebox.h>
#include <kstandarddirs.h>
#include <tdetempfile.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <knuminput.h>
#include <tdefiledialog.h>


#include <KoFilterManager.h>

#include <kis_doc.h>
#include <kis_image.h>
#include "kis_meta_registry.h"
#include <kis_iterators_pixel.h>
#include <kis_layer.h>
#include <kis_paint_layer.h>
#include <kis_group_layer.h>
#include <kis_transaction.h>
#include <kis_undo_adapter.h>
#include <kis_global.h>
#include <kis_types.h>
#include <kis_progress_subject.h>
#include <kis_progress_display_interface.h>
#include <kis_colorspace.h>
#include <kis_colorspace_factory_registry.h>
#include <kis_view.h>
#include <kis_paint_device.h>
#include <kis_channelinfo.h>

#include "kis_channel_separator.h"

KisChannelSeparator::KisChannelSeparator(KisView * view)
    : m_view(view)
{
}

void KisChannelSeparator::separate(KisProgressDisplayInterface * progress, enumSepAlphaOptions alphaOps, enumSepSource sourceOps, enumSepOutput outputOps, bool downscale, bool toColor)
{
    KisImageSP image = m_view->canvasSubject()->currentImg();
    if (!image) return;

    KisLayerSP layer = image->activeLayer();
    if (!layer) return;

    KisPaintDeviceSP src = image->activeDevice();
    if (!src) return;

    m_cancelRequested = false;
    if ( progress )
        progress->setSubject(this, true, true);
    emit notifyProgressStage(i18n("Separating image..."), 0);

    KisColorSpace * dstCs = 0;

    TQ_UINT32 numberOfChannels = src->nChannels();
    KisColorSpace * srcCs  = src->colorSpace();
    TQValueVector<KisChannelInfo *> channels = srcCs->channels();

    // Use the flattened image, if required
    switch(sourceOps) {

        case(ALL_LAYERS):
            src = image->mergedImage();
            break;
        default:
            break;
    }

    vKisPaintDeviceSP layers;

    TQValueVector<KisChannelInfo *>::const_iterator begin = channels.begin();
    TQValueVector<KisChannelInfo *>::const_iterator end = channels.end();


    TQRect rect = src->exactBounds();

    int i = 0;
    TQ_UINT32 channelIndex = 0;
    for (TQValueVector<KisChannelInfo *>::const_iterator it = begin; it != end; ++it, ++channelIndex)
    {

        KisChannelInfo * ch = (*it);

        if (ch->channelType() == KisChannelInfo::ALPHA && alphaOps != CREATE_ALPHA_SEPARATION) {
            continue;
        }

        TQ_INT32 channelSize = ch->size();
        TQ_INT32 channelPos = ch->pos();
        TQ_INT32 destSize = 1;

        KisPaintDeviceSP dev;
        if (toColor) {
            // We don't downscale if we separate to color channels
            dev = new KisPaintDevice(srcCs, "color separations");
        }
        else {
            if (channelSize == 1 || downscale) {
                dev = new KisPaintDevice( KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("GRAYA",""),"" ), "8 bit grayscale sep");
            }
            else {
                dev = new KisPaintDevice( KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("GRAYA16",""),"" ), "16 bit grayscale sep");
                destSize = 2;
            }
        }

        dstCs = dev->colorSpace();

        layers.push_back(dev);

        for (TQ_INT32 row = 0; row < rect.height(); ++row) {

            KisHLineIteratorPixel srcIt = src->createHLineIterator(rect.x(), rect.y() + row, rect.width(), false);
            KisHLineIteratorPixel dstIt = dev->createHLineIterator(rect.x(), rect.y() + row, rect.width(), true);

            while( ! srcIt.isDone() )
            {
                if (srcIt.isSelected())
                {
                    if (toColor) {
                        dstCs->getSingleChannelPixel(dstIt.rawData(), srcIt.rawData(), channelIndex);

                        if (alphaOps == COPY_ALPHA_TO_SEPARATIONS) {
                            //dstCs->setAlpha(dstIt.rawData(), srcIt.rawData()[srcAlphaPos], 1);
                            dstCs->setAlpha(dstIt.rawData(), srcCs->getAlpha(srcIt.rawData()), 1);
                        }
                        else {
                            dstCs->setAlpha(dstIt.rawData(), OPACITY_OPAQUE, 1);
                        }
                    }
                    else {

                        // To grayscale

                        // Decide wether we need downscaling
                        if (channelSize == 1 && destSize == 1) {

                            // Both 8-bit channels
                            dstIt.rawData()[0] = srcIt.rawData()[channelPos];

                            if (alphaOps == COPY_ALPHA_TO_SEPARATIONS) {
                                dstCs->setAlpha(dstIt.rawData(), srcCs->getAlpha(srcIt.rawData()), 1);
                            }
                            else {
                                dstCs->setAlpha(dstIt.rawData(), OPACITY_OPAQUE, 1);
                            }
                        }
                        else if (channelSize == 2 && destSize == 2) {

                            // Both 16-bit
                            dstIt.rawData()[0] = srcIt.rawData()[channelPos];
                            dstIt.rawData()[1] = srcIt.rawData()[channelPos + 1];

                            if (alphaOps == COPY_ALPHA_TO_SEPARATIONS) {
                                dstCs->setAlpha(dstIt.rawData(), srcCs->getAlpha(srcIt.rawData()), 1);
                            }
                            else {
                                dstCs->setAlpha(dstIt.rawData(), OPACITY_OPAQUE, 1);
                            }
                        }
                        else if (channelSize != 1 && destSize == 1) {
                            // Downscale
                            memset(dstIt.rawData(), srcCs->scaleToU8(srcIt.rawData(), channelPos), 1);

                            // XXX: Do alpha
                            dstCs->setAlpha(dstIt.rawData(), OPACITY_OPAQUE, 1);
                        }
                        else if (channelSize != 2 && destSize == 2) {
                            // Upscale
                            dstIt.rawData()[0] = srcCs->scaleToU8(srcIt.rawData(), channelPos);

                            // XXX: Do alpha
                            dstCs->setAlpha(dstIt.rawData(), OPACITY_OPAQUE, 1);

                        }
                    }
                }
                ++dstIt;
                ++srcIt;
            }
        }
        ++i;

        emit notifyProgress((i * 100) / numberOfChannels);
        if (m_cancelRequested) {
            break;
        }
    }

    vKisPaintDeviceSP_it deviceIt = layers.begin();

    emit notifyProgressDone();

    if (!m_cancelRequested) {

        KisUndoAdapter * undo = 0;
        if ((undo = image->undoAdapter()) && undo->undo()) {
            undo->beginMacro(i18n("Separate Image"));
        }

        // Flatten the image if required
        switch(sourceOps) {
            case(ALL_LAYERS):
                image->flatten();
                break;
            default:
                break;
        }

        for (TQValueVector<KisChannelInfo *>::const_iterator it = begin; it != end; ++it)
        {

            KisChannelInfo * ch = (*it);

            if (ch->channelType() == KisChannelInfo::ALPHA && alphaOps != CREATE_ALPHA_SEPARATION) {
            // Don't make an separate separation of the alpha channel if the user didn't ask for it.
                continue;
            }

            if (outputOps == TO_LAYERS) {
                KisPaintLayerSP l = new KisPaintLayer( image, ch->name(), OPACITY_OPAQUE, *deviceIt);
                image->addLayer( dynamic_cast<KisLayer*>(l.data()), image->rootLayer(), 0);
            }
            else {
                TQStringList listMimeFilter = KoFilterManager::mimeFilter("application/x-chalk", KoFilterManager::Export);
                TQString mimelist = listMimeFilter.join(" ");

                KFileDialog fd (TQString(), mimelist, m_view, "Export Layer", true);
                fd.setCaption(i18n("Export Layer") + "(" + ch->name() + ")");
                fd.setMimeFilter(listMimeFilter);
                fd.setOperationMode(KFileDialog::Saving);
                fd.setURL(KURL(ch->name()));
                if (!fd.exec()) return;

                KURL url = fd.selectedURL();
                TQString mimefilter = fd.currentMimeFilter();

                if (url.isEmpty())
                    return;

                KisPaintLayerSP l = new KisPaintLayer( image, ch->name(), OPACITY_OPAQUE, *deviceIt);
                TQRect r = l->exactBounds();

                KisDoc d;
                d.prepareForImport();

                KisImageSP dst = new KisImage(d.undoAdapter(), r.width(), r.height(), (*deviceIt)->colorSpace(), l->name());
                d.setCurrentImage( dst );
                dst->addLayer(l->clone(), dst->rootLayer(), 0);

                d.setOutputMimeType(mimefilter.latin1());
                d.exp0rt(url);

            }

            ++deviceIt;
        }

        if (undo && undo->undo()) {
            undo->endMacro();
        }

        m_view->canvasSubject()->document()->setModified(true);
    }
}




#include "kis_channel_separator.moc"