summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authoraavit <qt-info@nokia.com>2010-04-09 13:30:04 (GMT)
committeraavit <qt-info@nokia.com>2010-04-23 11:41:05 (GMT)
commitc1431e6daa5f0e229c2e79a945f28d00d70b0978 (patch)
tree42bb23ffae60d45448c95a638d978b1673747f99 /src/plugins
parent66da5958bc3ec0df60e76bb32ead94bfe7b2eac7 (diff)
downloadQt-c1431e6daa5f0e229c2e79a945f28d00d70b0978.zip
Qt-c1431e6daa5f0e229c2e79a945f28d00d70b0978.tar.gz
Qt-c1431e6daa5f0e229c2e79a945f28d00d70b0978.tar.bz2
Optimization: Avoid data copy when reading jpegs from memory
If the iodevice is actually a qbuffer, avoid read() and just give libjpeg a pointer to the contained data directly. Related to QTBUG-9095. Reviewed-by: Kim
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/imageformats/jpeg/qjpeghandler.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/plugins/imageformats/jpeg/qjpeghandler.cpp b/src/plugins/imageformats/jpeg/qjpeghandler.cpp
index 93b7cc6..1c6a289 100644
--- a/src/plugins/imageformats/jpeg/qjpeghandler.cpp
+++ b/src/plugins/imageformats/jpeg/qjpeghandler.cpp
@@ -44,6 +44,7 @@
#include <qimage.h>
#include <qvariant.h>
#include <qvector.h>
+#include <qbuffer.h>
#include <stdio.h> // jpeglib needs this to be pre-included
#include <setjmp.h>
@@ -102,6 +103,7 @@ struct my_jpeg_source_mgr : public jpeg_source_mgr {
// Nothing dynamic - cannot rely on destruction over longjump
QIODevice *device;
JOCTET buffer[max_buf];
+ const QBuffer *memDevice;
public:
my_jpeg_source_mgr(QIODevice *device);
@@ -117,10 +119,14 @@ static void qt_init_source(j_decompress_ptr)
static boolean qt_fill_input_buffer(j_decompress_ptr cinfo)
{
- int num_read;
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src;
+ if (src->memDevice) {
+ src->next_input_byte = (const JOCTET *)src->memDevice->data().constData();
+ src->bytes_in_buffer = (size_t)src->memDevice->data().size();
+ return true;
+ }
src->next_input_byte = src->buffer;
- num_read = src->device->read((char*)src->buffer, max_buf);
+ int num_read = src->device->read((char*)src->buffer, max_buf);
if (num_read <= 0) {
// Insert a fake EOI marker - as per jpeglib recommendation
src->buffer[0] = (JOCTET) 0xFF;
@@ -147,7 +153,7 @@ static void qt_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
* any trouble anyway --- large skips are infrequent.
*/
if (num_bytes > 0) {
- while (num_bytes > (long) src->bytes_in_buffer) {
+ while (num_bytes > (long) src->bytes_in_buffer) { // Should not happen in case of memDevice
num_bytes -= (long) src->bytes_in_buffer;
(void) qt_fill_input_buffer(cinfo);
/* note we assume that qt_fill_input_buffer will never return false,
@@ -178,6 +184,7 @@ inline my_jpeg_source_mgr::my_jpeg_source_mgr(QIODevice *device)
jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart;
jpeg_source_mgr::term_source = qt_term_source;
this->device = device;
+ memDevice = qobject_cast<QBuffer *>(device);
bytes_in_buffer = 0;
next_input_byte = buffer;
}