20#include "kis_tiff_writer_visitor.h"
22#include <tdemessagebox.h>
25#include <kis_annotation.h>
26#include <kis_colorspace.h>
27#include <kis_group_layer.h>
29#include <kis_paint_layer.h>
32#include "kis_tiff_converter.h"
35 bool writeColorSpaceInformation( TIFF* image, KisColorSpace * cs, uint16& color_type, uint16& sample_format )
37 sample_format = SAMPLEFORMAT_UINT;
38 if ( cs->id() == KisID(
"GRAYA") || cs->id() == KisID(
"GRAYA16") )
40 color_type = PHOTOMETRIC_MINISBLACK;
43 if ( cs->id() == KisID(
"RGBA") || cs->id() == KisID(
"RGBA16") )
45 color_type = PHOTOMETRIC_RGB;
48 if ( cs->id() == KisID(
"RGBAF16HALF") || cs->id() == KisID(
"RGBAF32") )
50 color_type = PHOTOMETRIC_RGB;
51 sample_format = SAMPLEFORMAT_IEEEFP;
54 if ( cs->id() == KisID(
"CMYK") || cs->id() == KisID(
"CMYKA16") )
56 color_type = PHOTOMETRIC_SEPARATED;
57 TIFFSetField(image, TIFFTAG_INKSET, INKSET_CMYK);
60 if ( cs->id() == KisID(
"LABA") )
62 color_type = PHOTOMETRIC_CIELAB;
66 KMessageBox::error(0, i18n(
"Cannot export images in %1.\n").arg(cs->id().name()) ) ;
72KisTIFFWriterVisitor::KisTIFFWriterVisitor(TIFF*img, KisTIFFOptions* options) : m_image(img), m_options(options)
76KisTIFFWriterVisitor::~KisTIFFWriterVisitor()
80bool KisTIFFWriterVisitor::saveAlpha() {
return m_options->alpha; }
82bool KisTIFFWriterVisitor::copyDataToStrips( KisHLineIterator it, tdata_t buff, uint8 depth, uint8 nbcolorssamples, TQ_UINT8* poses)
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());
90 for(i = 0; i < nbcolorssamples; i++)
92 *(dst++) = d[poses[i]];
94 if(saveAlpha()) *(dst++) = d[poses[i]];
98 }
else if(depth == 16)
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());
104 for(i = 0; i < nbcolorssamples; i++)
106 *(dst++) = d[poses[i]];
108 if(saveAlpha()) *(dst++) = d[poses[i]];
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();
117 for(i = 0; i < nbcolorssamples; i++)
119 *(dst++) = d[poses[i]];
121 if(saveAlpha()) *(dst++) = d[poses[i]];
130bool KisTIFFWriterVisitor::visit(KisPaintLayer *layer)
132 kdDebug(41008) <<
"visiting on paint layer " << layer->name() <<
"\n";
133 KisPaintDeviceSP pd = layer->paintDevice();
135 int depth = 8 * pd->pixelSize() / pd->nChannels();
136 TIFFSetField(image(), TIFFTAG_BITSPERSAMPLE, depth);
140 TIFFSetField(image(), TIFFTAG_SAMPLESPERPIXEL, pd->nChannels());
141 uint16 sampleinfo[1] = { EXTRASAMPLE_UNASSALPHA };
142 TIFFSetField(image(), TIFFTAG_EXTRASAMPLES, 1, sampleinfo);
144 TIFFSetField(image(), TIFFTAG_SAMPLESPERPIXEL, pd->nChannels() - 1);
145 TIFFSetField(image(), TIFFTAG_EXTRASAMPLES, 0);
149 uint16 sample_format;
150 if(!writeColorSpaceInformation(image(), pd->colorSpace(), color_type, sample_format))
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());
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);
167 TIFFSetField(image(), TIFFTAG_PREDICTOR, m_options->predictor);
170 TIFFSetField(image(), TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
172 TIFFSetField(image(), TIFFTAG_ROWSPERSTRIP, 8);
175 KisProfile* profile = pd->colorSpace()->getProfile();
178 TQByteArray ba = profile->annotation()->annotation();
179 TIFFSetField(image(), TIFFTAG_ICCPROFILE, ba.size(),ba.data());
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();
186 for (
int y = 0; y < height; y++) {
187 KisHLineIterator it = layer->paintDevice()->createHLineIterator(0, y, width,
false);
190 case PHOTOMETRIC_MINISBLACK:
192 TQ_UINT8 poses[]={ 0,1 };
193 r = copyDataToStrips(it, buff, depth, 1, poses);
196 case PHOTOMETRIC_RGB:
198 TQ_UINT8 poses[]={ 2, 1, 0, 3};
199 r = copyDataToStrips(it, buff, depth, 3, poses);
202 case PHOTOMETRIC_SEPARATED:
204 TQ_UINT8 poses[]={ 0, 1, 2, 3, 4 };
205 r = copyDataToStrips(it, buff, depth, 4, poses);
208 case PHOTOMETRIC_CIELAB:
210 TQ_UINT8 poses[]={ 0, 1, 2, 3 };
211 r = copyDataToStrips(it, buff, depth, 3, poses);
217 TIFFWriteScanline(image(), buff, y, (tsample_t) -1);
220 TIFFWriteDirectory(image());
223bool KisTIFFWriterVisitor::visit(KisGroupLayer *layer)
225 kdDebug(41008) <<
"Visiting on grouplayer " << layer->name() <<
"\n";
226 KisLayerSP child = layer->firstChild();
228 child->accept(*
this);
229 child = child->nextSibling();
234bool KisTIFFWriterVisitor::visit(KisPartLayer *)