~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/iodevicejpegsourcemanager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
36
36
 
37
37
// Local
38
38
 
39
 
namespace Gwenview {
40
 
namespace IODeviceJpegSourceManager {
 
39
namespace Gwenview
 
40
{
 
41
namespace IODeviceJpegSourceManager
 
42
{
41
43
 
42
44
#define SOURCE_MANAGER_BUFFER_SIZE 4096
43
45
struct IODeviceJpegSourceManager : public jpeg_source_mgr {
44
 
        QIODevice* mIODevice;
45
 
        JOCTET mBuffer[SOURCE_MANAGER_BUFFER_SIZE];
 
46
    QIODevice* mIODevice;
 
47
    JOCTET mBuffer[SOURCE_MANAGER_BUFFER_SIZE];
46
48
};
47
49
 
48
 
 
49
 
static boolean fill_input_buffer(j_decompress_ptr cinfo) {
50
 
        IODeviceJpegSourceManager* src = static_cast<IODeviceJpegSourceManager*>(cinfo->src);
51
 
        Q_ASSERT(src->mIODevice);
52
 
        int readSize = src->mIODevice->read((char*)src->mBuffer, SOURCE_MANAGER_BUFFER_SIZE);
53
 
        if (readSize > 0) {
54
 
                src->next_input_byte = src->mBuffer;
55
 
                src->bytes_in_buffer = readSize;
56
 
        } else {
57
 
                /**
58
 
                 * JPEG file is broken. We feed the decoder with fake EOI, as specified
59
 
                 * in the libjpeg documentation.
60
 
                 */
61
 
                static JOCTET fakeEOI[2] = { JOCTET(0xFF), JOCTET(JPEG_EOI)};
62
 
                kWarning() << "Image is incomplete";
63
 
                cinfo->src->next_input_byte = fakeEOI;
64
 
                cinfo->src->bytes_in_buffer = 2;
65
 
        }
66
 
        return true;
67
 
}
68
 
 
69
 
 
70
 
static void init_source(j_decompress_ptr cinfo) {
71
 
        fill_input_buffer(cinfo);
72
 
}
73
 
 
74
 
 
75
 
static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
76
 
        if (num_bytes > 0) {
77
 
                while (num_bytes > (long) cinfo->src->bytes_in_buffer) {
78
 
                        num_bytes -= (long) cinfo->src->bytes_in_buffer;
79
 
                        fill_input_buffer(cinfo);
80
 
                        /**
81
 
                         * we assume that fill_input_buffer will never return FALSE, so
82
 
                         * suspension need not be handled.
83
 
                         */
84
 
                }
85
 
                cinfo->src->next_input_byte += (size_t) num_bytes;
86
 
                cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
87
 
        }
88
 
}
89
 
 
90
 
 
91
 
static void term_source(j_decompress_ptr) {
92
 
}
93
 
 
94
 
 
95
 
void setup(j_decompress_ptr cinfo, QIODevice* ioDevice) {
96
 
        Q_ASSERT(!cinfo->src);
97
 
        IODeviceJpegSourceManager* src = (IODeviceJpegSourceManager*)
98
 
                (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
99
 
                                                                        sizeof(IODeviceJpegSourceManager));
100
 
        cinfo->src = src;
101
 
 
102
 
        src->init_source = init_source;
103
 
        src->fill_input_buffer = fill_input_buffer;
104
 
        src->skip_input_data = skip_input_data;
105
 
        src->resync_to_restart = jpeg_resync_to_restart;
106
 
        src->term_source = term_source;
107
 
 
108
 
        src->mIODevice = ioDevice;
109
 
}
110
 
 
 
50
static boolean fill_input_buffer(j_decompress_ptr cinfo)
 
51
{
 
52
    IODeviceJpegSourceManager* src = static_cast<IODeviceJpegSourceManager*>(cinfo->src);
 
53
    Q_ASSERT(src->mIODevice);
 
54
    int readSize = src->mIODevice->read((char*)src->mBuffer, SOURCE_MANAGER_BUFFER_SIZE);
 
55
    if (readSize > 0) {
 
56
        src->next_input_byte = src->mBuffer;
 
57
        src->bytes_in_buffer = readSize;
 
58
    } else {
 
59
        /**
 
60
         * JPEG file is broken. We feed the decoder with fake EOI, as specified
 
61
         * in the libjpeg documentation.
 
62
         */
 
63
        static JOCTET fakeEOI[2] = { JOCTET(0xFF), JOCTET(JPEG_EOI)};
 
64
        kWarning() << "Image is incomplete";
 
65
        cinfo->src->next_input_byte = fakeEOI;
 
66
        cinfo->src->bytes_in_buffer = 2;
 
67
    }
 
68
    return true;
 
69
}
 
70
 
 
71
static void init_source(j_decompress_ptr cinfo)
 
72
{
 
73
    fill_input_buffer(cinfo);
 
74
}
 
75
 
 
76
static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
 
77
{
 
78
    if (num_bytes > 0) {
 
79
        while (num_bytes > (long) cinfo->src->bytes_in_buffer) {
 
80
            num_bytes -= (long) cinfo->src->bytes_in_buffer;
 
81
            fill_input_buffer(cinfo);
 
82
            /**
 
83
             * we assume that fill_input_buffer will never return FALSE, so
 
84
             * suspension need not be handled.
 
85
             */
 
86
        }
 
87
        cinfo->src->next_input_byte += (size_t) num_bytes;
 
88
        cinfo->src->bytes_in_buffer -= (size_t) num_bytes;
 
89
    }
 
90
}
 
91
 
 
92
static void term_source(j_decompress_ptr)
 
93
{
 
94
}
 
95
 
 
96
void setup(j_decompress_ptr cinfo, QIODevice* ioDevice)
 
97
{
 
98
    Q_ASSERT(!cinfo->src);
 
99
    IODeviceJpegSourceManager* src = (IODeviceJpegSourceManager*)
 
100
                                     (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_PERMANENT,
 
101
                                             sizeof(IODeviceJpegSourceManager));
 
102
    cinfo->src = src;
 
103
 
 
104
    src->init_source = init_source;
 
105
    src->fill_input_buffer = fill_input_buffer;
 
106
    src->skip_input_data = skip_input_data;
 
107
    src->resync_to_restart = jpeg_resync_to_restart;
 
108
    src->term_source = term_source;
 
109
 
 
110
    src->mIODevice = ioDevice;
 
111
}
111
112
 
112
113
} // IODeviceJpegSourceManager namespace
113
114
} // Gwenview namespace