kis_tiff_writer_visitor.cpp
1/*
2 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kis_tiff_writer_visitor.h"
21
22#include <tdemessagebox.h>
23#include <tdelocale.h>
24
25#include <kis_annotation.h>
26#include <kis_colorspace.h>
27#include <kis_group_layer.h>
28#include <kis_image.h>
29#include <kis_paint_layer.h>
30#include <kis_types.h>
31
32#include "kis_tiff_converter.h"
33
34namespace {
35 bool writeColorSpaceInformation( TIFF* image, KisColorSpace * cs, uint16& color_type, uint16& sample_format )
36 {
37 sample_format = SAMPLEFORMAT_UINT;
38 if ( cs->id() == KisID("GRAYA") || cs->id() == KisID("GRAYA16") )
39 {
40 color_type = PHOTOMETRIC_MINISBLACK;
41 return true;
42 }
43 if ( cs->id() == KisID("RGBA") || cs->id() == KisID("RGBA16") )
44 {
45 color_type = PHOTOMETRIC_RGB;
46 return true;
47 }
48 if ( cs->id() == KisID("RGBAF16HALF") || cs->id() == KisID("RGBAF32") )
49 {
50 color_type = PHOTOMETRIC_RGB;
51 sample_format = SAMPLEFORMAT_IEEEFP;
52 return true;
53 }
54 if ( cs->id() == KisID("CMYK") || cs->id() == KisID("CMYKA16") )
55 {
56 color_type = PHOTOMETRIC_SEPARATED;
57 TIFFSetField(image, TIFFTAG_INKSET, INKSET_CMYK);
58 return true;
59 }
60 if ( cs->id() == KisID("LABA") )
61 {
62 color_type = PHOTOMETRIC_CIELAB;
63 return true;
64 }
65
66 KMessageBox::error(0, i18n("Cannot export images in %1.\n").arg(cs->id().name()) ) ;
67 return false;
68
69 }
70}
71
72KisTIFFWriterVisitor::KisTIFFWriterVisitor(TIFF*img, KisTIFFOptions* options) : m_image(img), m_options(options)
73{
74}
75
76KisTIFFWriterVisitor::~KisTIFFWriterVisitor()
77{
78}
79
80bool KisTIFFWriterVisitor::saveAlpha() { return m_options->alpha; }
81
82bool KisTIFFWriterVisitor::copyDataToStrips( KisHLineIterator it, tdata_t buff, uint8 depth, uint8 nbcolorssamples, TQ_UINT8* poses)
83{
84 if(depth == 32)
85 {
86 TQ_UINT32 *dst = reinterpret_cast<TQ_UINT32 *>(buff);
87 while (!it.isDone()) {
88 const TQ_UINT32 *d = reinterpret_cast<const TQ_UINT32 *>(it.rawData());
89 int i;
90 for(i = 0; i < nbcolorssamples; i++)
91 {
92 *(dst++) = d[poses[i]];
93 }
94 if(saveAlpha()) *(dst++) = d[poses[i]];
95 ++it;
96 }
97 return true;
98 } else if(depth == 16)
99 {
100 TQ_UINT16 *dst = reinterpret_cast<TQ_UINT16 *>(buff);
101 while (!it.isDone()) {
102 const TQ_UINT16 *d = reinterpret_cast<const TQ_UINT16 *>(it.rawData());
103 int i;
104 for(i = 0; i < nbcolorssamples; i++)
105 {
106 *(dst++) = d[poses[i]];
107 }
108 if(saveAlpha()) *(dst++) = d[poses[i]];
109 ++it;
110 }
111 return true;
112 } else if(depth == 8) {
113 TQ_UINT8 *dst = reinterpret_cast<TQ_UINT8 *>(buff);
114 while (!it.isDone()) {
115 const TQ_UINT8 *d = it.rawData();
116 int i;
117 for(i = 0; i < nbcolorssamples; i++)
118 {
119 *(dst++) = d[poses[i]];
120 }
121 if(saveAlpha()) *(dst++) = d[poses[i]];
122 ++it;
123 }
124 return true;
125 }
126 return false;
127}
128
129
130bool KisTIFFWriterVisitor::visit(KisPaintLayer *layer)
131{
132 kdDebug(41008) << "visiting on paint layer " << layer->name() << "\n";
133 KisPaintDeviceSP pd = layer->paintDevice();
134 // Save depth
135 int depth = 8 * pd->pixelSize() / pd->nChannels();
136 TIFFSetField(image(), TIFFTAG_BITSPERSAMPLE, depth);
137 // Save number of samples
138 if(saveAlpha())
139 {
140 TIFFSetField(image(), TIFFTAG_SAMPLESPERPIXEL, pd->nChannels());
141 uint16 sampleinfo[1] = { EXTRASAMPLE_UNASSALPHA };
142 TIFFSetField(image(), TIFFTAG_EXTRASAMPLES, 1, sampleinfo);
143 } else {
144 TIFFSetField(image(), TIFFTAG_SAMPLESPERPIXEL, pd->nChannels() - 1);
145 TIFFSetField(image(), TIFFTAG_EXTRASAMPLES, 0);
146 }
147 // Save colorspace information
148 uint16 color_type;
149 uint16 sample_format;
150 if(!writeColorSpaceInformation(image(), pd->colorSpace(), color_type, sample_format))
151 { // unsupported colorspace
152 return false;
153 }
154 TIFFSetField(image(), TIFFTAG_PHOTOMETRIC, color_type);
155 TIFFSetField(image(), TIFFTAG_SAMPLEFORMAT, sample_format);
156 TIFFSetField(image(), TIFFTAG_IMAGEWIDTH, layer->image()->width());
157 TIFFSetField(image(), TIFFTAG_IMAGELENGTH, layer->image()->height());
158
159 // Set the compression options
160 TIFFSetField(image(), TIFFTAG_COMPRESSION, m_options->compressionType);
161 TIFFSetField(image(), TIFFTAG_FAXMODE, m_options->faxMode);
162 TIFFSetField(image(), TIFFTAG_JPEGQUALITY, m_options->jpegQuality);
163 TIFFSetField(image(), TIFFTAG_ZIPQUALITY, m_options->deflateCompress);
164 TIFFSetField(image(), TIFFTAG_PIXARLOGQUALITY, m_options->pixarLogCompress);
165
166 // Set the predictor
167 TIFFSetField(image(), TIFFTAG_PREDICTOR, m_options->predictor);
168
169 // Use contiguous configuration
170 TIFFSetField(image(), TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
171 // Use 8 rows per strip
172 TIFFSetField(image(), TIFFTAG_ROWSPERSTRIP, 8);
173
174 // Save profile
175 KisProfile* profile = pd->colorSpace()->getProfile();
176 if(profile)
177 {
178 TQByteArray ba = profile->annotation()->annotation();
179 TIFFSetField(image(), TIFFTAG_ICCPROFILE, ba.size(),ba.data());
180 }
181 tsize_t stripsize = TIFFStripSize(image());
182 tdata_t buff = _TIFFmalloc(stripsize);
183 TQ_INT32 height = layer->image()->height();
184 TQ_INT32 width = layer->image()->width();
185 bool r = true;
186 for (int y = 0; y < height; y++) {
187 KisHLineIterator it = layer->paintDevice()->createHLineIterator(0, y, width, false);
188 switch(color_type)
189 {
190 case PHOTOMETRIC_MINISBLACK:
191 {
192 TQ_UINT8 poses[]={ 0,1 };
193 r = copyDataToStrips(it, buff, depth, 1, poses);
194 }
195 break;
196 case PHOTOMETRIC_RGB:
197 {
198 TQ_UINT8 poses[]={ 2, 1, 0, 3};
199 r = copyDataToStrips(it, buff, depth, 3, poses);
200 }
201 break;
202 case PHOTOMETRIC_SEPARATED:
203 {
204 TQ_UINT8 poses[]={ 0, 1, 2, 3, 4 };
205 r = copyDataToStrips(it, buff, depth, 4, poses);
206 }
207 break;
208 case PHOTOMETRIC_CIELAB:
209 {
210 TQ_UINT8 poses[]={ 0, 1, 2, 3 };
211 r = copyDataToStrips(it, buff, depth, 3, poses);
212 }
213 break;
214 return false;
215 }
216 if(!r) return false;
217 TIFFWriteScanline(image(), buff, y, (tsample_t) -1);
218 }
219 _TIFFfree(buff);
220 TIFFWriteDirectory(image());
221 return true;
222}
223bool KisTIFFWriterVisitor::visit(KisGroupLayer *layer)
224{
225 kdDebug(41008) << "Visiting on grouplayer " << layer->name() << "\n";
226 KisLayerSP child = layer->firstChild();
227 while (child) {
228 child->accept(*this);
229 child = child->nextSibling();
230 }
231 return true;
232}
233
234bool KisTIFFWriterVisitor::visit(KisPartLayer *)
235{
236 return true;
237}