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

View file

@ -126,7 +126,7 @@ bool JP2Handler::read(QImage *image)
}
opj_codec_t* ojcodec = opj_create_decompress(guessOJCodec(data));
if (!ojcodec) {
if (Q_UNLIKELY(!ojcodec)) {
kWarning() << "Could not create codec";
return false;
}
@ -142,7 +142,7 @@ bool JP2Handler::read(QImage *image)
opj_setup_decoder(ojcodec, &ojparameters);
opj_stream_t *ojstream = opj_stream_create(s_ojbuffersize, OPJ_TRUE);
if (!ojstream) {
if (Q_UNLIKELY(!ojstream)) {
kWarning() << "Could not create stream";
opj_destroy_codec(ojcodec);
return false;
@ -155,7 +155,7 @@ bool JP2Handler::read(QImage *image)
opj_stream_set_user_data(ojstream, this, 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";
opj_destroy_codec(ojcodec);
opj_stream_destroy(ojstream);
@ -163,13 +163,13 @@ bool JP2Handler::read(QImage *image)
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";
}
opj_end_decompress(ojcodec, ojstream);
*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";
opj_destroy_codec(ojcodec);
opj_stream_destroy(ojstream);

View file

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

View file

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