From db294bfb602cbd9b5ba71c08e72319776b55d2b5 Mon Sep 17 00:00:00 2001 From: qbnu <93988953+qbnu@users.noreply.github.com> Date: Sun, 3 Sep 2023 17:49:18 -0400 Subject: [PATCH] optimize RGBA to BGRA conversion --- src/JPEGView/HEIFWrapper.cpp | 15 +++++---------- src/JPEGView/JXLWrapper.cpp | 7 ++++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/JPEGView/HEIFWrapper.cpp b/src/JPEGView/HEIFWrapper.cpp index a3906217..d4a9d01e 100644 --- a/src/JPEGView/HEIFWrapper.cpp +++ b/src/JPEGView/HEIFWrapper.cpp @@ -51,21 +51,16 @@ void * HeifReader::ReadImage(int &width, } std::vector iccp = image.get_raw_color_profile(); void* transform = ICCProfileTransform::CreateTransform(iccp.data(), iccp.size(), ICCProfileTransform::FORMAT_RGBA); - uint8_t* p; size_t i, j; if (!ICCProfileTransform::DoTransform(transform, data, pPixelData, width, height, stride=stride)) { - memcpy(pPixelData, data, size); - unsigned char* o = pPixelData; + unsigned int* o = (unsigned int*)pPixelData; for (i = 0; i < height; i++) { - p = data + i * stride; + unsigned int* p = (unsigned int*)(data + i * stride); for (j = 0; j < width; j++) { // RGBA -> BGRA conversion - o[0] = p[2]; - o[1] = p[1]; - o[2] = p[0]; - o[3] = p[3]; - p += nchannels; - o += nchannels; + *o = _rotr(_byteswap_ulong(*p), 8); + p++; + o++; } } } diff --git a/src/JPEGView/JXLWrapper.cpp b/src/JPEGView/JXLWrapper.cpp index b48c00dc..f2ba6867 100644 --- a/src/JPEGView/JXLWrapper.cpp +++ b/src/JPEGView/JXLWrapper.cpp @@ -207,10 +207,11 @@ void* JxlReader::ReadImage(int& width, if (cache.transform == NULL) cache.transform = ICCProfileTransform::CreateTransform(icc_profile.data(), icc_profile.size(), ICCProfileTransform::FORMAT_RGBA); if (!ICCProfileTransform::DoTransform(cache.transform, pixels.data(), pPixelData, width, height)) { - memcpy(pPixelData, pixels.data(), size); // RGBA -> BGRA conversion (with little-endian integers) - for (uint32_t* i = (uint32_t*)pPixelData; (uint8_t*)i < pPixelData + size; i++) - *i = ((*i & 0x00FF0000) >> 16) | ((*i & 0x0000FF00)) | ((*i & 0x000000FF) << 16) | ((*i & 0xFF000000)); + uint32_t* data = (uint32_t*)pixels.data(); + for (int i = 0; i * sizeof(uint32_t) < size; i++) { + ((uint32_t*)pPixelData)[i] = _rotr(_byteswap_ulong(data[i]), 8); + } } // Copy Exif data into the format understood by CEXIFReader