File: jpegcreator.cpp

package info (click to toggle)
kio-extras 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,720 kB
  • ctags: 3,303
  • sloc: cpp: 26,172; ansic: 5,034; perl: 1,056; xml: 460; python: 28; sh: 20; makefile: 6
file content (305 lines) | stat: -rw-r--r-- 9,709 bytes parent folder | download
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
302
303
304
305
/*  This file is part of the KDE libraries
    Copyright (C) 2008 Andre Gemünd <scroogie@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "jpegcreator.h"

#include <cstdio>
#include <csetjmp>
#include "jpegcreatorsettings5.h"
#include <QCheckBox>
#include <QFile>
#include <QImage>
#include <klocalizedstring.h>

#ifdef HAVE_EXIV2
#include <exiv2/image.hpp>
#include <exiv2/exif.hpp>
#endif

extern "C"
{
    #include <jpeglib.h>

    Q_DECL_EXPORT ThumbCreator *new_creator()
    {
        return new JpegCreator;
    }
}

struct jpeg_custom_error_mgr
{
   struct jpeg_error_mgr builtin;
   jmp_buf setjmp_buffer;
};

void jpeg_custom_error_callback(j_common_ptr jpegDecompress)
{
   jpeg_custom_error_mgr *custom_err = (jpeg_custom_error_mgr *)jpegDecompress->err;

   // jump to error recovery (fallback to old method)
   longjmp(custom_err->setjmp_buffer, 1);
}

#ifdef Q_CC_MSVC
typedef struct
{
    struct jpeg_source_mgr pub;
    JOCTET eoi[2];
} jpeg_custom_source_mgr;

void init_source (j_decompress_ptr cinfo)
{
}

boolean fill_input_buffer (j_decompress_ptr cinfo)
{
    jpeg_custom_source_mgr* src = (jpeg_custom_source_mgr*) cinfo->src;

    /* Create a fake EOI marker */
    src->eoi[0] = (JOCTET) 0xFF;
    src->eoi[1] = (JOCTET) JPEG_EOI;
    src->pub.next_input_byte = src->eoi;
    src->pub.bytes_in_buffer = 2;

    return true;
}

void skip_input_data (j_decompress_ptr cinfo, long nbytes)
{
    jpeg_custom_source_mgr* src = (jpeg_custom_source_mgr*) cinfo->src;

    if (nbytes > 0)
    {
        while (nbytes > (long) src->pub.bytes_in_buffer)
        {
            nbytes -= (long) src->pub.bytes_in_buffer;
            (void) fill_input_buffer(cinfo);
        }
        src->pub.next_input_byte += (size_t) nbytes;
        src->pub.bytes_in_buffer -= (size_t) nbytes;
    }
}

void term_source (j_decompress_ptr cinfo)
{
}

void jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
{
    jpeg_custom_source_mgr* src;

    if (cinfo->src == NULL)
    {
        cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
        sizeof(jpeg_custom_source_mgr));
    }

    src = (jpeg_custom_source_mgr*) cinfo->src;
    src->pub.init_source = init_source;
    src->pub.fill_input_buffer = fill_input_buffer;
    src->pub.skip_input_data = skip_input_data;
    src->pub.resync_to_restart = jpeg_resync_to_restart;    // default
    src->pub.term_source = term_source;

    src->pub.next_input_byte = buffer;
    src->pub.bytes_in_buffer = bufsize;
}
#endif

JpegCreator::JpegCreator()
{
}

QTransform JpegCreator::orientationMatrix(int exivOrientation) const
{
    //Check (e.g.) man jpegexiforient for an explanation
    switch (exivOrientation) {
    case 2:
        return QTransform(-1, 0, 0, 1, 0, 0);
    case 3:
        return QTransform(-1, 0, 0, -1, 0, 0);
    case 4:
        return QTransform(1, 0, 0, -1, 0, 0);
    case 5:
        return QTransform(0, 1, 1, 0, 0, 0);
    case 6:
        return QTransform(0, 1, -1, 0, 0, 0);
    case 7:
        return QTransform(0, -1, -1, 0, 0, 0);
    case 8:
        return QTransform(0, -1, 1, 0, 0, 0);
    case 1:
    default:
        return QTransform(1, 0, 0, 1, 0, 0);
    }
}

/**
 * This is a faster thumbnail creation specifically for JPEG images, as it uses the libjpeg feature of
 * calculating the inverse dct for a part of coefficients for lower resolutions.
 * Interesting parameters are the quality settings of libjpeg
 *         jpegDecompress.do_fancy_upsampling (TRUE, FALSE)
 *         jpegDecompress.do_block_smoothing (TRUE, FALSE)
 *         jpegDecompress.dct_method (JDCT_IFAST, JDCT_ISLOW, JDCT_IFLOAT)
 * and the resampling parameter of QImage.
 *
 * Important: We do not need to scaled to exact dimesions, as thumbnail.cpp will check dimensions and
 * rescale anyway.
 */
bool JpegCreator::create(const QString &path, int width, int height, QImage &image)
{
    QImage img;
    const QByteArray name = QFile::encodeName(path);
    FILE *jpegFile = fopen(name.constData(), "rb");
    if (jpegFile  == 0) {
       return false;
    }

    // create jpeglib data structures and calculate scale denominator
    struct jpeg_decompress_struct jpegDecompress;
    struct jpeg_custom_error_mgr jpegError;
    jpegDecompress.err = jpeg_std_error(&jpegError.builtin);
    jpeg_create_decompress(&jpegDecompress);
#ifdef Q_CC_MSVC
    QFile inFile(path);
    QByteArray buf;
    if(inFile.open(QIODevice::ReadOnly)) {
        while(!inFile.atEnd()) {
            buf += inFile.readLine();
        }
        inFile.close();
    }

    jpeg_memory_src(&jpegDecompress, (JOCTET*)buf.data(), buf.size());
#else
    jpeg_stdio_src(&jpegDecompress, jpegFile);
#endif
    jpeg_read_header(&jpegDecompress, TRUE);

    const double ratioWidth = jpegDecompress.image_width / (double)width;
    const double ratioHeight = jpegDecompress.image_height / (double)height;
    int scale = 1;
    if (ratioWidth > 7 || ratioHeight > 7) {
        scale = 8;
    } else if (ratioWidth > 3.5 || ratioHeight > 3.5) {
        scale = 4;
    } else if (ratioWidth > 1.75 || ratioHeight > 1.75) {
        scale = 2;
    }

    // set jpeglib decompression parameters
    jpegDecompress.scale_num           = 1;
    jpegDecompress.scale_denom         = scale;
    jpegDecompress.do_fancy_upsampling = FALSE;
    jpegDecompress.do_block_smoothing  = FALSE;
    jpegDecompress.dct_method          = JDCT_IFAST;
    jpegDecompress.err->error_exit     = jpeg_custom_error_callback;
    jpegDecompress.out_color_space     = JCS_RGB;

    jpeg_calc_output_dimensions(&jpegDecompress);

    if (setjmp(jpegError.setjmp_buffer)) {
        jpeg_abort_decompress(&jpegDecompress);
        fclose(jpegFile);
        // libjpeg version failed, fall back to direct loading of QImage
        if (!img.load(path)) {
            return false;
        }
        if (img.depth() != 32) {
            img = img.convertToFormat(QImage::Format_RGB32);
        }
    } else {
        jpeg_start_decompress(&jpegDecompress);
        img = QImage(jpegDecompress.output_width, jpegDecompress.output_height, QImage::Format_RGB32);
        uchar *buffer = img.bits();
        const int bpl = img.bytesPerLine();
        while (jpegDecompress.output_scanline < jpegDecompress.output_height) {
            // advance line-pointer to next line
            uchar *line = buffer + jpegDecompress.output_scanline * bpl;
            jpeg_read_scanlines(&jpegDecompress, &line, 1);
        }
        jpeg_finish_decompress(&jpegDecompress);

        // align correctly for QImage
        // code copied from Gwenview and digiKam
        for (int i = 0; i < int(jpegDecompress.output_height); ++i) {
            uchar *in = img.scanLine(i) + jpegDecompress.output_width * 3;
            QRgb *out = (QRgb*)img.scanLine(i);
            for (int j = jpegDecompress.output_width - 1; j >= 0; --j) {
                in -= 3;
                out[j] = qRgb(in[0], in[1], in[2]);
            }
        }
        fclose(jpegFile);
        jpeg_destroy_decompress(&jpegDecompress);
    }

#ifdef HAVE_EXIV2
    JpegCreatorSettings* settings = JpegCreatorSettings::self();
    settings->load();
    if (settings->rotate()) {
        //Handle exif rotation
        try {
            Exiv2::Image::AutoPtr exivImg = Exiv2::ImageFactory::open(name.constData());
            if (exivImg.get()) {
                exivImg->readMetadata();
                Exiv2::ExifData exifData = exivImg->exifData();
                if (!exifData.empty()) {
                    Exiv2::ExifKey key("Exif.Image.Orientation");
                    Exiv2::ExifData::iterator it = exifData.findKey(key);
                    if (it != exifData.end()) {
                        int orient = it->toLong();
                        image = img.transformed(orientationMatrix(orient));
                        return true;
                    }
                }
            }
        } catch (...) {
            // Apparently libexiv changed its API at some point, a different exception is thrown
            // depending on the version. an ifdef could make it work, but since we just ignore the exception
            // there is no point in doing that
        }
    }
#endif

    image = img;
    return true;
}

ThumbCreator::Flags JpegCreator::flags() const
{
    return None;
}

QWidget *JpegCreator::createConfigurationWidget()
{
    QCheckBox *rotateCheckBox = new QCheckBox(i18nc("@option:check", "Rotate the image automatically"));
    rotateCheckBox->setChecked(JpegCreatorSettings::rotate());
    return rotateCheckBox;
}

void JpegCreator::writeConfiguration(const QWidget *configurationWidget)
{
    const QCheckBox *rotateCheckBox = qobject_cast<const QCheckBox*>(configurationWidget);
    if (rotateCheckBox) {
        JpegCreatorSettings* settings = JpegCreatorSettings::self();
        settings->setRotate(rotateCheckBox->isChecked());
        settings->save();
    }
}