summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/tiff/kfile_tiff.cpp
blob: 1e844d0d79fb7fcc5280261361da045f791ab251 (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
/* This file is part of the KDE project
 * Copyright (C) 2002 Nadeem Hasan <nhasan@kde.org>
 *
 * 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 version 2.
 *
 * 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "kfile_tiff.h"

#include <kgenericfactory.h>
#include <kdebug.h>

#include <qstringlist.h>
#include <qfile.h>
#include <qdatetime.h>
#include <qregexp.h>

#include <tiff.h>
#include <tiffio.h>

typedef KGenericFactory<KTiffPlugin> TiffFactory;

K_EXPORT_COMPONENT_FACTORY(kfile_tiff, TiffFactory("kfile_tiff"))

KTiffPlugin::KTiffPlugin(QObject *parent, const char *name,
        const QStringList &args) : KFilePlugin(parent, name, args)
{
    kdDebug(7034) << "TIFF file meta info plugin" << endl;
    KFileMimeTypeInfo* info = addMimeTypeInfo( "image/tiff" );

    KFileMimeTypeInfo::GroupInfo* group =
            addGroupInfo(info, "General", i18n("General"));

    KFileMimeTypeInfo::ItemInfo* item;
    item = addItemInfo(group, "Description", i18n("Description"),
            QVariant::String);
    setHint(item, KFileMimeTypeInfo::Description);
    item = addItemInfo(group, "Copyright", i18n("Copyright"),
            QVariant::String);
    item = addItemInfo(group, "ColorMode", i18n("Color Mode"),
            QVariant::String);
    item = addItemInfo(group, "Dimensions", i18n("Dimensions"),
            QVariant::Size);
    setHint(item, KFileMimeTypeInfo::Size);
    setUnit(item, KFileMimeTypeInfo::Pixels);
    item = addItemInfo(group, "Resolution", i18n("Resolution"),
            QVariant::Size);
    setUnit(item, KFileMimeTypeInfo::DotsPerInch);
    item = addItemInfo(group, "BitDepth", i18n("Bit Depth"),
            QVariant::Int);
    setUnit(item, KFileMimeTypeInfo::BitsPerPixel);
    item = addItemInfo(group, "Compression", i18n("Compression"),
            QVariant::String);
    item = addItemInfo(group, "Software", i18n("Software"),
            QVariant::String);
    item = addItemInfo(group, "DateTime", i18n("Date/Time"),
            QVariant::DateTime);
    item = addItemInfo(group, "Artist", i18n("Artist"),
            QVariant::String);
    setHint(item, KFileMimeTypeInfo::Author);
    item = addItemInfo(group, "FaxPages", i18n("Fax Pages"),
            QVariant::Int);

    group = addGroupInfo(info, "Scanner", i18n("Scanner"));

    item = addItemInfo(group, "Make", i18n("Make"), QVariant::String);
    item = addItemInfo(group, "Model", i18n("Model"), QVariant::String);

    m_colorMode.setAutoDelete(true);
    m_imageCompression.setAutoDelete(true);

    m_colorMode.insert(PHOTOMETRIC_MINISWHITE,
                new QString(I18N_NOOP("Monochrome")));
    m_colorMode.insert(PHOTOMETRIC_MINISBLACK,
                new QString(I18N_NOOP("Monochrome")));
    m_colorMode.insert(PHOTOMETRIC_RGB,
                new QString(I18N_NOOP("RGB")));
    m_colorMode.insert(PHOTOMETRIC_PALETTE,
                new QString(I18N_NOOP("Palette color")));
    m_colorMode.insert(PHOTOMETRIC_MASK,
                new QString(I18N_NOOP("Transparency mask")));
    m_colorMode.insert(PHOTOMETRIC_SEPARATED,
                new QString(I18N_NOOP("Color separations")));
    m_colorMode.insert(PHOTOMETRIC_YCBCR,
                new QString(I18N_NOOP("YCbCr")));
    m_colorMode.insert(PHOTOMETRIC_CIELAB,
                new QString(I18N_NOOP("CIE Lab")));
#ifdef PHOTOMETRIC_ITULAB
    m_colorMode.insert(PHOTOMETRIC_ITULAB,
                new QString(I18N_NOOP("ITU Lab")));
#endif
    m_colorMode.insert(PHOTOMETRIC_LOGL,
                new QString(I18N_NOOP("LOGL")));
    m_colorMode.insert(PHOTOMETRIC_LOGLUV,
                new QString(I18N_NOOP("LOGLUV")));

    m_imageCompression.insert(COMPRESSION_NONE,
                new QString(I18N_NOOP("None")));
    m_imageCompression.insert(COMPRESSION_CCITTRLE,
                new QString(I18N_NOOP("RLE")));
    m_imageCompression.insert(COMPRESSION_CCITTFAX3,
                new QString(I18N_NOOP("G3 Fax")));
    m_imageCompression.insert(COMPRESSION_CCITTFAX4,
                new QString(I18N_NOOP("G4 Fax")));
    m_imageCompression.insert(COMPRESSION_LZW,
                new QString(I18N_NOOP("LZW")));
    m_imageCompression.insert(COMPRESSION_OJPEG,
                new QString(I18N_NOOP("JPEG")));
    m_imageCompression.insert(COMPRESSION_JPEG,
                new QString(I18N_NOOP("JPEG DCT")));
#ifdef COMPRESSION_ADOBE_DEFLATE
    m_imageCompression.insert(COMPRESSION_ADOBE_DEFLATE,
                new QString(I18N_NOOP("Adobe Deflate")));
#endif
    m_imageCompression.insert(COMPRESSION_NEXT,
                new QString(I18N_NOOP("NeXT 2-bit RLE")));
    m_imageCompression.insert(COMPRESSION_CCITTRLEW,
                new QString(I18N_NOOP("RLE Word")));
    m_imageCompression.insert(COMPRESSION_PACKBITS,
                new QString(I18N_NOOP("Packbits")));
    m_imageCompression.insert(COMPRESSION_THUNDERSCAN,
                new QString(I18N_NOOP("Thunderscan RLE")));
    m_imageCompression.insert(COMPRESSION_IT8CTPAD,
                new QString(I18N_NOOP("IT8 CT w/padding")));
    m_imageCompression.insert(COMPRESSION_IT8LW,
                new QString(I18N_NOOP("IT8 linework RLE")));
    m_imageCompression.insert(COMPRESSION_IT8MP,
                new QString(I18N_NOOP("IT8 monochrome")));
    m_imageCompression.insert(COMPRESSION_IT8BL,
                new QString(I18N_NOOP("IT8 binary lineart")));
    m_imageCompression.insert(COMPRESSION_PIXARFILM,
                new QString(I18N_NOOP("Pixar 10-bit LZW")));
    m_imageCompression.insert(COMPRESSION_PIXARLOG,
                new QString(I18N_NOOP("Pixar 11-bit ZIP")));
    m_imageCompression.insert(COMPRESSION_DEFLATE,
                new QString(I18N_NOOP("Pixar deflate")));
    m_imageCompression.insert(COMPRESSION_DCS,
                new QString(I18N_NOOP("Kodak DCS")));
    m_imageCompression.insert(COMPRESSION_JBIG,
                new QString(I18N_NOOP("ISO JBIG")));
    m_imageCompression.insert(COMPRESSION_SGILOG,
                new QString(I18N_NOOP("SGI log luminance RLE")));
    m_imageCompression.insert(COMPRESSION_SGILOG24,
                new QString(I18N_NOOP("SGI log 24-bit packed")));
}

QDateTime KTiffPlugin::tiffDate(const QString& s) const
{
    QDateTime dt;
    QRegExp rxDate("^([0-9]{4}):([0-9]{2}):([0-9]{2})\\s"
                   "([0-9]{2}):([0-9]{2}):([0-9]{2})$");

    if (rxDate.search(s) != -1)
    {
        int year = rxDate.cap(1).toInt();
        int month = rxDate.cap(2).toInt();
        int day = rxDate.cap(3).toInt();
        int hour = rxDate.cap(4).toInt();
        int min = rxDate.cap(5).toInt();
        int sec = rxDate.cap(6).toInt();

        QDate d = QDate(year, month, day);
        QTime t = QTime(hour, min, sec);

        if (d.isValid() && t.isValid())
        {
            dt.setDate(d);
            dt.setTime(t);
        }
    }

    return dt;
}

bool KTiffPlugin::readInfo(KFileMetaInfo& info, uint)
{
    TIFF *tiff = TIFFOpen(QFile::encodeName(info.path()), "r");
    if (!tiff)
        return false;

    uint32 imageLength=0, imageWidth=0;
    uint16 bitsPerSample=0, imageCompression=0, colorMode=0, samplesPerPixel=0,
        imageAlpha=0, imageResUnit=0, dummy=0, faxPages=0;
    float imageXResolution=0, imageYResolution=0;
    char *description=0, *copyright=0, *software=0, *datetime=0, *artist=0,
        *scannerMake=0, *scannerModel=0;

    TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &imageLength);
    TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &imageWidth);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);
    TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &colorMode);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_COMPRESSION, &imageCompression);
    TIFFGetField(tiff, TIFFTAG_MATTEING, &imageAlpha);
    TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &imageXResolution);
    TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &imageYResolution);
    TIFFGetFieldDefaulted(tiff, TIFFTAG_RESOLUTIONUNIT, &imageResUnit);
    TIFFGetField(tiff, TIFFTAG_IMAGEDESCRIPTION, &description);
    TIFFGetField(tiff, TIFFTAG_SOFTWARE, &software);
    TIFFGetField(tiff, TIFFTAG_COPYRIGHT, &copyright);
    TIFFGetField(tiff, TIFFTAG_DATETIME, &datetime);
    TIFFGetField(tiff, TIFFTAG_ARTIST, &artist);
    TIFFGetField(tiff, TIFFTAG_PAGENUMBER, &dummy, &faxPages);
    TIFFGetField(tiff, TIFFTAG_MAKE, &scannerMake);
    TIFFGetField(tiff, TIFFTAG_MODEL, &scannerModel);

    kdDebug(7034) << "Description: " << description << endl;
    kdDebug(7034) << "Width: " << imageWidth << endl;
    kdDebug(7034) << "Height: " << imageLength << endl;
    kdDebug(7034) << "BitDepth: " << bitsPerSample << endl;
    kdDebug(7034) << "ColorMode: " << colorMode << endl;
    kdDebug(7034) << "Compression: " << imageCompression << endl;
    kdDebug(7034) << "SamplesPerPixel: " << samplesPerPixel << endl;
    kdDebug(7034) << "ImageAlpha: " << imageAlpha << endl;
    kdDebug(7034) << "XResolution: " << imageXResolution << endl;
    kdDebug(7034) << "YResolution: " << imageYResolution << endl;
    kdDebug(7034) << "ResolutionUnit: " << imageResUnit << endl;
    kdDebug(7034) << "FaxPages: " << faxPages << endl;
    kdDebug(7034) << "DateTime: " << datetime << endl;
    kdDebug(7034) << "Copyright: " << copyright << endl;
    kdDebug(7034) << "Software: " <<  software << endl;
    kdDebug(7034) << "Artist: " << artist << endl;
    kdDebug(7034) << "Make: " << scannerMake << endl;
    kdDebug(7034) << "Model: " << scannerModel << endl;

    if (imageResUnit == RESUNIT_CENTIMETER)
    {
        imageXResolution *= 2.54;
        imageYResolution *= 2.54;
    }
    else if (imageResUnit == RESUNIT_NONE)
    {
        imageXResolution = 0;
        imageYResolution = 0;
    }

    int imageBpp = bitsPerSample*samplesPerPixel;
    if (imageAlpha && colorMode==PHOTOMETRIC_RGB)
        m_colorMode.replace(PHOTOMETRIC_RGB, new QString(I18N_NOOP("RGBA")));

    KFileMetaInfoGroup group = appendGroup(info, "General");
    if (description)
        appendItem(group, "Description", QString(description));
    appendItem(group, "Dimensions", QSize(imageWidth, imageLength));
    appendItem(group, "BitDepth", imageBpp);
    if (imageXResolution>0 && imageYResolution>0)
        appendItem(group, "Resolution", QSize(
                static_cast<int>(imageXResolution),
                static_cast<int>(imageYResolution)));
    if (m_colorMode[colorMode])
        appendItem(group, "ColorMode", *m_colorMode[colorMode]);
    if (m_imageCompression[imageCompression])
        appendItem(group, "Compression", *m_imageCompression[imageCompression]);
    if (datetime)
    {
        QDateTime dt = tiffDate(QString(datetime));
        if (dt.isValid())
            appendItem(group, "DateTime", dt);
    }
    if (copyright)
        appendItem(group, "Copyright", QString(copyright));
    if (software)
        appendItem(group, "Software", QString(software));
    if (artist)
        appendItem(group, "Artist", QString(artist));

    if (faxPages>0 && (imageCompression==COMPRESSION_CCITTFAX3 ||
            imageCompression==COMPRESSION_CCITTFAX4))
    {
        appendItem(group, "FaxPages", faxPages);
    }

    if (scannerMake || scannerModel)
    {
        group = appendGroup(info, "Scanner");
        if (scannerMake)
            appendItem(group, "Make", QString(scannerMake));
        if (scannerModel)
            appendItem(group, "Model", QString(scannerModel));
    }

    TIFFClose(tiff);

    return true;
}

#include "kfile_tiff.moc"