kimgio: branches optimizations

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-13 03:14:57 +03:00
parent eec59d05e2
commit 565ad4785b
4 changed files with 25 additions and 35 deletions

View file

@ -102,19 +102,14 @@ bool ICOHandler::read(QImage *image)
Q_ASSERT(sizeof(ushort) == 2); Q_ASSERT(sizeof(ushort) == 2);
Q_ASSERT(sizeof(uint) == 4); Q_ASSERT(sizeof(uint) == 4);
QDataStream datastream(device());
datastream.setByteOrder(QDataStream::LittleEndian);
ushort icoreserved = 0; ushort icoreserved = 0;
ushort icotype = 0; ushort icotype = 0;
ushort iconimages = 0; ushort iconimages = 0;
QDataStream datastream(device());
datastream.setByteOrder(QDataStream::LittleEndian);
datastream >> icoreserved; datastream >> icoreserved;
datastream >> icotype; datastream >> icotype;
datastream >> iconimages; datastream >> iconimages;
if (datastream.atEnd()) {
kWarning() << "Reached end of data before valid ICO";
return false;
}
if (icotype == ICOType::CursorType) { if (icotype == ICOType::CursorType) {
kWarning() << "Cursor icons are not supported"; kWarning() << "Cursor icons are not supported";
@ -145,19 +140,19 @@ bool ICOHandler::read(QImage *image)
datastream >> icoimagesize; datastream >> icoimagesize;
datastream >> icoimageoffset; datastream >> icoimageoffset;
if (icoimageoffset > datastream.device()->size()) { if (Q_UNLIKELY(icoimageoffset > datastream.device()->size())) {
kWarning() << "Invalid image offset" << icoimageoffset; kWarning() << "Invalid image offset" << icoimageoffset;
return false; return false;
} }
if (icoimagesize >= INT_MAX) { if (Q_UNLIKELY(icoimagesize >= INT_MAX)) {
kWarning() << "ICO image size is too big" << icoimagesize; kWarning() << "ICO image size is too big" << icoimagesize;
continue; continue;
} }
datastream.device()->seek(icoimageoffset); datastream.device()->seek(icoimageoffset);
QByteArray imagebytes(icoimagesize, char(0)); QByteArray imagebytes(icoimagesize, char(0));
if (datastream.readRawData(imagebytes.data(), icoimagesize) != icoimagesize) { if (Q_UNLIKELY(datastream.readRawData(imagebytes.data(), icoimagesize) != icoimagesize)) {
kWarning() << "Could not read image data"; kWarning() << "Could not read image data";
return false; return false;
} }
@ -210,13 +205,13 @@ bool ICOHandler::read(QImage *image)
} }
} }
if (bmpimagesize >= INT_MAX) { if (Q_UNLIKELY(bmpimagesize >= INT_MAX)) {
kWarning() << "BMP image size is too big" << bmpimagesize; kWarning() << "BMP image size is too big" << bmpimagesize;
continue; continue;
} }
imagebytes.resize(bmpimagesize); imagebytes.resize(bmpimagesize);
if (datastream.readRawData(imagebytes.data(), bmpimagesize) != bmpimagesize) { if (Q_UNLIKELY(datastream.readRawData(imagebytes.data(), bmpimagesize) != bmpimagesize)) {
kWarning() << "Could not read BMP image data"; kWarning() << "Could not read BMP image data";
continue; continue;
} }
@ -225,13 +220,8 @@ bool ICOHandler::read(QImage *image)
const int imagewidth = (icowidth ? icowidth : bmpwidth); const int imagewidth = (icowidth ? icowidth : bmpwidth);
const int imageheight = (icoheight ? icoheight : bmpheight); const int imageheight = (icoheight ? icoheight : bmpheight);
if (imagewidth > USHRT_MAX || imageheight > USHRT_MAX) {
kWarning() << "Image width or height is too big" << imagewidth << imageheight;
continue;
}
QImage bmpimage(imagewidth, imageheight, imageformat); QImage bmpimage(imagewidth, imageheight, imageformat);
if (bmpimage.isNull()) { if (Q_UNLIKELY(bmpimage.isNull())) {
kWarning() << "Could not create BMP image" << imagewidth << imageheight << imageformat; kWarning() << "Could not create BMP image" << imagewidth << imageheight << imageformat;
continue; continue;
} }
@ -248,7 +238,7 @@ bool ICOHandler::read(QImage *image)
} }
const QImage pngimage = QImage::fromData(imagebytes.constData(), imagebytes.size(), "PNG"); const QImage pngimage = QImage::fromData(imagebytes.constData(), imagebytes.size(), "PNG");
if (!pngimage.isNull()) { if (Q_LIKELY(!pngimage.isNull())) {
kDebug() << "Valid PNG image" << ii; kDebug() << "Valid PNG image" << ii;
*image = pngimage; *image = pngimage;
return true; return true;

View file

@ -126,7 +126,7 @@ bool JP2Handler::read(QImage *image)
} }
opj_codec_t* ojcodec = opj_create_decompress(guessOJCodec(data)); opj_codec_t* ojcodec = opj_create_decompress(guessOJCodec(data));
if (!ojcodec) { if (Q_UNLIKELY(!ojcodec)) {
kWarning() << "Could not create codec"; kWarning() << "Could not create codec";
return false; return false;
} }
@ -142,7 +142,7 @@ bool JP2Handler::read(QImage *image)
opj_setup_decoder(ojcodec, &ojparameters); opj_setup_decoder(ojcodec, &ojparameters);
opj_stream_t *ojstream = opj_stream_create(s_ojbuffersize, OPJ_TRUE); opj_stream_t *ojstream = opj_stream_create(s_ojbuffersize, OPJ_TRUE);
if (!ojstream) { if (Q_UNLIKELY(!ojstream)) {
kWarning() << "Could not create stream"; kWarning() << "Could not create stream";
opj_destroy_codec(ojcodec); opj_destroy_codec(ojcodec);
return false; return false;
@ -155,7 +155,7 @@ bool JP2Handler::read(QImage *image)
opj_stream_set_user_data(ojstream, this, NULL); opj_stream_set_user_data(ojstream, this, NULL);
opj_image_t* ojimage = NULL; opj_image_t* ojimage = NULL;
if (opj_read_header(ojstream, ojcodec, &ojimage) == OPJ_FALSE) { if (Q_UNLIKELY(opj_read_header(ojstream, ojcodec, &ojimage) == OPJ_FALSE)) {
kWarning() << "Could not read header"; kWarning() << "Could not read header";
opj_destroy_codec(ojcodec); opj_destroy_codec(ojcodec);
opj_stream_destroy(ojstream); opj_stream_destroy(ojstream);
@ -163,13 +163,13 @@ bool JP2Handler::read(QImage *image)
return false; return false;
} }
if (opj_decode(ojcodec, ojstream, ojimage) == OPJ_FALSE) { if (Q_UNLIKELY(opj_decode(ojcodec, ojstream, ojimage) == OPJ_FALSE)) {
kWarning() << "Could not decode stream"; kWarning() << "Could not decode stream";
} }
opj_end_decompress(ojcodec, ojstream); opj_end_decompress(ojcodec, ojstream);
*image = QImage(ojimage->comps->w, ojimage->comps->h, QImage::Format_ARGB32); *image = QImage(ojimage->comps->w, ojimage->comps->h, QImage::Format_ARGB32);
if (image->isNull()) { if (Q_UNLIKELY(image->isNull())) {
kWarning() << "Could not create image QImage"; kWarning() << "Could not create image QImage";
opj_destroy_codec(ojcodec); opj_destroy_codec(ojcodec);
opj_stream_destroy(ojstream); opj_stream_destroy(ojstream);

View file

@ -71,7 +71,7 @@ bool JPEGHandler::read(QImage *image)
} }
tjhandle jpegdecomp = tjInitDecompress(); tjhandle jpegdecomp = tjInitDecompress();
if (!jpegdecomp) { if (Q_UNLIKELY(!jpegdecomp)) {
kWarning() << "Could not initialize decompressor" << tjGetErrorStr(); kWarning() << "Could not initialize decompressor" << tjGetErrorStr();
return false; return false;
} }
@ -86,7 +86,7 @@ bool JPEGHandler::read(QImage *image)
&jpegwidth, &jpegheight, &jpegwidth, &jpegheight,
&jpegsubsamp, &jpegcolorspace &jpegsubsamp, &jpegcolorspace
); );
if (jpegstatus != 0) { if (Q_UNLIKELY(jpegstatus != 0)) {
kWarning() << "Could not decompress header" << tjGetErrorStr2(jpegdecomp); kWarning() << "Could not decompress header" << tjGetErrorStr2(jpegdecomp);
(void)tjDestroy(jpegdecomp); (void)tjDestroy(jpegdecomp);
return false; return false;
@ -94,7 +94,7 @@ bool JPEGHandler::read(QImage *image)
int jpegbuffersize = (jpegwidth * jpegheight * tjPixelSize[s_jpegpixelformat]); int jpegbuffersize = (jpegwidth * jpegheight * tjPixelSize[s_jpegpixelformat]);
unsigned char *jpegbuffer = tjAlloc(jpegbuffersize); unsigned char *jpegbuffer = tjAlloc(jpegbuffersize);
if (!jpegbuffer) { if (Q_UNLIKELY(!jpegbuffer)) {
kWarning() << "Could not allocate buffer" << tjGetErrorStr2(jpegdecomp); kWarning() << "Could not allocate buffer" << tjGetErrorStr2(jpegdecomp);
(void)tjDestroy(jpegdecomp); (void)tjDestroy(jpegdecomp);
return false; return false;
@ -108,7 +108,7 @@ bool JPEGHandler::read(QImage *image)
s_jpegpixelformat, s_jpegpixelformat,
TJFLAG_FASTDCT TJFLAG_FASTDCT
); );
if (jpegstatus != 0) { if (Q_UNLIKELY(jpegstatus != 0)) {
kWarning() << "Could not decompress" << tjGetErrorStr2(jpegdecomp); kWarning() << "Could not decompress" << tjGetErrorStr2(jpegdecomp);
tjFree(jpegbuffer); tjFree(jpegbuffer);
(void)tjDestroy(jpegdecomp); (void)tjDestroy(jpegdecomp);
@ -116,7 +116,7 @@ bool JPEGHandler::read(QImage *image)
} }
*image = QImage(jpegwidth, jpegheight, QImage::Format_ARGB32); *image = QImage(jpegwidth, jpegheight, QImage::Format_ARGB32);
if (image->isNull()) { if (Q_UNLIKELY(image->isNull())) {
tjFree(jpegbuffer); tjFree(jpegbuffer);
(void)tjDestroy(jpegdecomp); (void)tjDestroy(jpegdecomp);
return false; return false;

View file

@ -56,28 +56,28 @@ bool RAWHandler::read(QImage *image)
raw.imgdata.params.output_color = LIBRAW_COLORSPACE_sRGB; raw.imgdata.params.output_color = LIBRAW_COLORSPACE_sRGB;
int rawresult = raw.open_buffer(data.data(), data.size()); int rawresult = raw.open_buffer(data.data(), data.size());
if (rawresult != LIBRAW_SUCCESS) { if (Q_UNLIKELY(rawresult != LIBRAW_SUCCESS)) {
kWarning() << "Could not open buffer" << libraw_strerror(rawresult); kWarning() << "Could not open buffer" << libraw_strerror(rawresult);
raw.recycle(); raw.recycle();
return false; return false;
} }
rawresult = raw.unpack(); rawresult = raw.unpack();
if (rawresult != LIBRAW_SUCCESS) { if (Q_UNLIKELY(rawresult != LIBRAW_SUCCESS)) {
kWarning() << "Could not unpack" << libraw_strerror(rawresult); kWarning() << "Could not unpack" << libraw_strerror(rawresult);
raw.recycle(); raw.recycle();
return false; return false;
} }
rawresult = raw.dcraw_process(); rawresult = raw.dcraw_process();
if (rawresult != LIBRAW_SUCCESS) { if (Q_UNLIKELY(rawresult != LIBRAW_SUCCESS)) {
kWarning() << "Could not process" << libraw_strerror(rawresult); kWarning() << "Could not process" << libraw_strerror(rawresult);
raw.recycle(); raw.recycle();
return false; return false;
} }
libraw_processed_image_t* rawimg = raw.dcraw_make_mem_image(&rawresult); libraw_processed_image_t* rawimg = raw.dcraw_make_mem_image(&rawresult);
if (!rawimg || rawresult != LIBRAW_SUCCESS) { if (Q_UNLIKELY(!rawimg || rawresult != LIBRAW_SUCCESS)) {
kWarning() << "Could not make image" << libraw_strerror(rawresult); kWarning() << "Could not make image" << libraw_strerror(rawresult);
raw.recycle(); raw.recycle();
return false; return false;
@ -91,7 +91,7 @@ bool RAWHandler::read(QImage *image)
} }
*image = QImage(rawimg->width, rawimg->height, QImage::Format_ARGB32); *image = QImage(rawimg->width, rawimg->height, QImage::Format_ARGB32);
if (image->isNull()) { if (Q_UNLIKELY(image->isNull())) {
kWarning() << "Could not create QImage"; kWarning() << "Could not create QImage";
raw.dcraw_clear_mem(rawimg); raw.dcraw_clear_mem(rawimg);
raw.recycle(); raw.recycle();
@ -149,7 +149,7 @@ bool RAWHandler::canRead(QIODevice *device)
kDebug() << libraw_strerror(rawresult); kDebug() << libraw_strerror(rawresult);
raw.recycle(); raw.recycle();
return false; return false;
} else if (rawresult != LIBRAW_SUCCESS) { } else if (Q_UNLIKELY(rawresult != LIBRAW_SUCCESS)) {
kWarning() << "Could not open buffer" << libraw_strerror(rawresult); kWarning() << "Could not open buffer" << libraw_strerror(rawresult);
raw.recycle(); raw.recycle();
return false; return false;