From e5e1fac136065d7c8afc49ac72cb517f64c87910 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 15 Jun 2017 13:04:36 +0200 Subject: Move endian integers to qendian_p.h The endian integers have more use than just in QJson, and is already used separately by QtDeclarative. This patch moves q_littleendian out of qjson_p.h and matches it with a corresponding q_bigendian, and puts it to use in simplifying endian handling in the ico image handler. Change-Id: I975bb701a089256db8ced3cb53b4bd62cdfb02dd Reviewed-by: Thiago Macieira --- src/plugins/imageformats/ico/ico.pro | 1 + src/plugins/imageformats/ico/qicohandler.cpp | 128 +++++++-------------------- 2 files changed, 33 insertions(+), 96 deletions(-) (limited to 'src/plugins/imageformats') diff --git a/src/plugins/imageformats/ico/ico.pro b/src/plugins/imageformats/ico/ico.pro index 7ca1f18cb1..c8bb37eff2 100644 --- a/src/plugins/imageformats/ico/ico.pro +++ b/src/plugins/imageformats/ico/ico.pro @@ -3,6 +3,7 @@ TARGET = qico HEADERS += main.h qicohandler.h SOURCES += main.cpp qicohandler.cpp OTHER_FILES += ico.json +QT += core-private PLUGIN_TYPE = imageformats PLUGIN_CLASS_NAME = QICOPlugin diff --git a/src/plugins/imageformats/ico/qicohandler.cpp b/src/plugins/imageformats/ico/qicohandler.cpp index 17d1aeeb5d..e61173db30 100644 --- a/src/plugins/imageformats/ico/qicohandler.cpp +++ b/src/plugins/imageformats/ico/qicohandler.cpp @@ -48,6 +48,7 @@ #include "qicohandler.h" #include +#include #include #include #include @@ -63,34 +64,34 @@ typedef struct quint8 bHeight; // Height of the image (actual height, not times 2) quint8 bColorCount; // Number of colors in image (0 if >=8bpp) [ not ture ] quint8 bReserved; // Reserved - quint16 wPlanes; // Color Planes - quint16 wBitCount; // Bits per pixel - quint32 dwBytesInRes; // how many bytes in this resource? - quint32 dwImageOffset; // where in the file is this image + quint16_le wPlanes; // Color Planes + quint16_le wBitCount; // Bits per pixel + quint32_le dwBytesInRes; // how many bytes in this resource? + quint32_le dwImageOffset; // where in the file is this image } ICONDIRENTRY, *LPICONDIRENTRY; #define ICONDIRENTRY_SIZE 16 typedef struct { - quint16 idReserved; // Reserved - quint16 idType; // resource type (1 for icons, 2 for cursors) - quint16 idCount; // how many images? + quint16_le idReserved; // Reserved + quint16_le idType; // resource type (1 for icons, 2 for cursors) + quint16_le idCount; // how many images? ICONDIRENTRY idEntries[1]; // the entries for each image } ICONDIR, *LPICONDIR; #define ICONDIR_SIZE 6 // Exclude the idEntries field typedef struct { // BMP information header - quint32 biSize; // size of this struct - quint32 biWidth; // pixmap width - quint32 biHeight; // pixmap height (specifies the combined height of the XOR and AND masks) - quint16 biPlanes; // should be 1 - quint16 biBitCount; // number of bits per pixel - quint32 biCompression; // compression method - quint32 biSizeImage; // size of image - quint32 biXPelsPerMeter; // horizontal resolution - quint32 biYPelsPerMeter; // vertical resolution - quint32 biClrUsed; // number of colors used - quint32 biClrImportant; // number of important colors + quint32_le biSize; // size of this struct + quint32_le biWidth; // pixmap width + quint32_le biHeight; // pixmap height (specifies the combined height of the XOR and AND masks) + quint16_le biPlanes; // should be 1 + quint16_le biBitCount; // number of bits per pixel + quint32_le biCompression; // compression method + quint32_le biSizeImage; // size of image + quint32_le biXPelsPerMeter; // horizontal resolution + quint32_le biYPelsPerMeter; // vertical resolution + quint32_le biClrUsed; // number of colors used + quint32_le biClrImportant; // number of important colors } BMP_INFOHDR ,*LPBMP_INFOHDR; #define BMP_INFOHDR_SIZE 40 @@ -140,108 +141,43 @@ private: // Data readers and writers that takes care of alignment and endian stuff. static bool readIconDirEntry(QIODevice *iodev, ICONDIRENTRY *iconDirEntry) { - if (iodev) { - uchar tmp[ICONDIRENTRY_SIZE]; - if (iodev->read((char*)tmp, ICONDIRENTRY_SIZE) == ICONDIRENTRY_SIZE) { - iconDirEntry->bWidth = tmp[0]; - iconDirEntry->bHeight = tmp[1]; - iconDirEntry->bColorCount = tmp[2]; - iconDirEntry->bReserved = tmp[3]; - - iconDirEntry->wPlanes = qFromLittleEndian(&tmp[4]); - iconDirEntry->wBitCount = qFromLittleEndian(&tmp[6]); - iconDirEntry->dwBytesInRes = qFromLittleEndian(&tmp[8]); - iconDirEntry->dwImageOffset = qFromLittleEndian(&tmp[12]); - return true; - } - } + if (iodev) + return (iodev->read((char*)iconDirEntry, ICONDIRENTRY_SIZE) == ICONDIRENTRY_SIZE); return false; } static bool writeIconDirEntry(QIODevice *iodev, const ICONDIRENTRY &iconEntry) { - if (iodev) { - uchar tmp[ICONDIRENTRY_SIZE]; - tmp[0] = iconEntry.bWidth; - tmp[1] = iconEntry.bHeight; - tmp[2] = iconEntry.bColorCount; - tmp[3] = iconEntry.bReserved; - qToLittleEndian(iconEntry.wPlanes, &tmp[4]); - qToLittleEndian(iconEntry.wBitCount, &tmp[6]); - qToLittleEndian(iconEntry.dwBytesInRes, &tmp[8]); - qToLittleEndian(iconEntry.dwImageOffset, &tmp[12]); - return iodev->write((char*)tmp, ICONDIRENTRY_SIZE) == ICONDIRENTRY_SIZE; - } - + if (iodev) + return iodev->write((char*)&iconEntry, ICONDIRENTRY_SIZE) == ICONDIRENTRY_SIZE; return false; } static bool readIconDir(QIODevice *iodev, ICONDIR *iconDir) { - if (iodev) { - uchar tmp[ICONDIR_SIZE]; - if (iodev->read((char*)tmp, ICONDIR_SIZE) == ICONDIR_SIZE) { - iconDir->idReserved = qFromLittleEndian(&tmp[0]); - iconDir->idType = qFromLittleEndian(&tmp[2]); - iconDir->idCount = qFromLittleEndian(&tmp[4]); - return true; - } - } + if (iodev) + return (iodev->read((char*)iconDir, ICONDIR_SIZE) == ICONDIR_SIZE); return false; } static bool writeIconDir(QIODevice *iodev, const ICONDIR &iconDir) { - if (iodev) { - uchar tmp[6]; - qToLittleEndian(iconDir.idReserved, tmp); - qToLittleEndian(iconDir.idType, &tmp[2]); - qToLittleEndian(iconDir.idCount, &tmp[4]); - return iodev->write((char*)tmp, 6) == 6; - } + if (iodev) + return iodev->write((char*)&iconDir, 6) == 6; return false; } static bool readBMPInfoHeader(QIODevice *iodev, BMP_INFOHDR *pHeader) { - if (iodev) { - uchar header[BMP_INFOHDR_SIZE]; - if (iodev->read((char*)header, BMP_INFOHDR_SIZE) == BMP_INFOHDR_SIZE) { - pHeader->biSize = qFromLittleEndian(&header[0]); - pHeader->biWidth = qFromLittleEndian(&header[4]); - pHeader->biHeight = qFromLittleEndian(&header[8]); - pHeader->biPlanes = qFromLittleEndian(&header[12]); - pHeader->biBitCount = qFromLittleEndian(&header[14]); - pHeader->biCompression = qFromLittleEndian(&header[16]); - pHeader->biSizeImage = qFromLittleEndian(&header[20]); - pHeader->biXPelsPerMeter = qFromLittleEndian(&header[24]); - pHeader->biYPelsPerMeter = qFromLittleEndian(&header[28]); - pHeader->biClrUsed = qFromLittleEndian(&header[32]); - pHeader->biClrImportant = qFromLittleEndian(&header[36]); - return true; - } - } + if (iodev) + return (iodev->read((char*)pHeader, BMP_INFOHDR_SIZE) == BMP_INFOHDR_SIZE); return false; } static bool writeBMPInfoHeader(QIODevice *iodev, const BMP_INFOHDR &header) { - if (iodev) { - uchar tmp[BMP_INFOHDR_SIZE]; - qToLittleEndian(header.biSize, &tmp[0]); - qToLittleEndian(header.biWidth, &tmp[4]); - qToLittleEndian(header.biHeight, &tmp[8]); - qToLittleEndian(header.biPlanes, &tmp[12]); - qToLittleEndian(header.biBitCount, &tmp[14]); - qToLittleEndian(header.biCompression, &tmp[16]); - qToLittleEndian(header.biSizeImage, &tmp[20]); - qToLittleEndian(header.biXPelsPerMeter, &tmp[24]); - qToLittleEndian(header.biYPelsPerMeter, &tmp[28]); - qToLittleEndian(header.biClrUsed, &tmp[32]); - qToLittleEndian(header.biClrImportant, &tmp[36]); - - return iodev->write((char*)tmp, BMP_INFOHDR_SIZE) == BMP_INFOHDR_SIZE; - } + if (iodev) + return iodev->write((char*)&header, BMP_INFOHDR_SIZE) == BMP_INFOHDR_SIZE; return false; } @@ -561,7 +497,7 @@ QImage ICOReader::iconAt(int index) if (icoAttrib.depth == 32) // there's no colormap icoAttrib.ncolors = 0; else // # colors used - icoAttrib.ncolors = header.biClrUsed ? header.biClrUsed : 1 << icoAttrib.nbits; + icoAttrib.ncolors = header.biClrUsed ? uint(header.biClrUsed) : 1 << icoAttrib.nbits; if (icoAttrib.ncolors > 256) //color table can't be more than 256 return img; icoAttrib.w = iconEntry.bWidth; -- cgit v1.2.3