From 5e393f205207f17e5b3f067ba4431dd5acabf662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 20 Nov 2019 11:35:59 +0100 Subject: [PATCH 01/20] Fix CR3 decoding on big endian platforms and some warnings from cppcheck --- rtengine/canon_cr3_decoder.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 359243ed5..f10a7c083 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -17,6 +17,7 @@ */ #include + #include "dcraw.h" #ifdef __GNUC__ // silence warning #pragma GCC diagnostic push @@ -587,7 +588,7 @@ fin: #undef _min #undef _constrain #endif -#define _abs(x) (((x) ^ ((int32_t)(x) >> 31)) - ((int32_t)(x) >> 31)) +#define _abs(x) ((x) < 0 ? -(x) : (x)) #define _min(a, b) ((a) < (b) ? (a) : (b)) #define _constrain(x, l, u) ((x) < (l) ? (l) : ((x) > (u) ? (u) : (x))) @@ -622,7 +623,14 @@ libraw_inline void _BitScanReverse(DWORD *Index, unsigned long Mask) { *Index = sizeof(unsigned long) * 8 - 1 - __builtin_clzl(Mask); } -#define _byteswap_ulong(x) __builtin_bswap32(x) +uint32_t _byteswap_ulong(uint32_t x) +{ + #if BYTE_ORDER == BIG_ENDIAN + return x; + #else + return __builtin_bswap32(x); + #endif +} #endif @@ -1041,7 +1049,7 @@ libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam *param, bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); int32_t code = -(bitCode & 1) ^ (bitCode >> 1); - param->lineBuf1[1] = param->roundedBitsMask * 2 * code + (code >> 31) + sym; + param->lineBuf1[1] = param->roundedBitsMask * 2 * code + static_cast(code < 0) + sym; if (doCode) { From f42efdb2e0b6f76149f3a08fd53835b480755773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 20 Nov 2019 14:58:46 +0100 Subject: [PATCH 02/20] Use better endianness check (by @heckflosse) --- rtengine/canon_cr3_decoder.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index f10a7c083..248cdd294 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -625,11 +625,11 @@ libraw_inline void _BitScanReverse(DWORD *Index, unsigned long Mask) } uint32_t _byteswap_ulong(uint32_t x) { - #if BYTE_ORDER == BIG_ENDIAN - return x; - #else - return __builtin_bswap32(x); - #endif +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + return x; +#else + return __builtin_bswap32(x); +#endif } #endif From 6039f1e3001bcd106d141d6fa9e68f3371e74148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 20 Nov 2019 15:52:25 +0100 Subject: [PATCH 03/20] Tackle warnings --- rtengine/canon_cr3_decoder.cc | 102 +++++++++++++++++++--------------- rtengine/dcraw.h | 8 +-- 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 248cdd294..e945a4ab8 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -1,4 +1,24 @@ -/* -*- C++ -*- +/* + * This file is part of RawTherapee. + * + * Copyright (c) 2019 RawTherapee development team + * + * RawTherapee is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * RawTherapee is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with RawTherapee. If not, see . + */ + +// Code adapted from ART +/* * * This file is part of ART. * @@ -16,16 +36,6 @@ * along with ART. If not, see . */ -#include - -#include "dcraw.h" -#ifdef __GNUC__ // silence warning -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wsign-compare" -#pragma GCC diagnostic ignored "-Wunused-variable" -#pragma GCC diagnostic ignored "-Wunused-but-set-variable" -#endif - // Code adapted from libraw /* -*- C++ -*- * Copyright 2019 LibRaw LLC (info@libraw.org) @@ -41,13 +51,17 @@ */ +#include +#include + +#include "dcraw.h" void DCraw::parse_canon_cr3() { int err; unsigned long long szAtomList; short nesting = -1; - short nTrack = -1; + unsigned short nTrack = 0; short TrackType; char AtomNameStack[128]; strcpy(make, "Canon"); @@ -61,14 +75,14 @@ void DCraw::parse_canon_cr3() #define LIBRAW_CRXTRACKS_MAXCOUNT RT_canon_CR3_data.CRXTRACKS_MAXCOUNT -void DCraw::selectCRXTrack(short maxTrack) +void DCraw::selectCRXTrack(unsigned short maxTrack) { if (maxTrack < 0) return; INT64 bitcounts[LIBRAW_CRXTRACKS_MAXCOUNT], maxbitcount = 0; uint32_t maxjpegbytes = 0; memset(bitcounts, 0, sizeof(bitcounts)); - for (int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) + for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) { CanonCR3Data::crx_data_header_t *d = &RT_canon_CR3_data.crx_header[i]; if (d->MediaType == 1) // RAW @@ -89,18 +103,21 @@ void DCraw::selectCRXTrack(short maxTrack) } if (maxbitcount < 8) return; - int framei = -1, framecnt = 0; - for (int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) + bool has_framei = false; + unsigned int framei = 0, framecnt = 0; + for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) { if (bitcounts[i] == maxbitcount) { - if (framecnt <= shot_select) + if (framecnt <= shot_select) { + has_framei = true; framei = i; + } framecnt++; } } is_raw = framecnt; - if (framei >= 0 && framei < LIBRAW_CRXTRACKS_MAXCOUNT) + if (has_framei && framei < LIBRAW_CRXTRACKS_MAXCOUNT) { CanonCR3Data::crx_data_header_t *d = &RT_canon_CR3_data.crx_header[framei]; @@ -129,7 +146,7 @@ void DCraw::selectCRXTrack(short maxTrack) int tiff_idx = -1; INT64 tpixels = 0; - for (int i = 0; i < tiff_nifds; i++) + for (unsigned int i = 0; i < tiff_nifds; i++) if (INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height) > tpixels) { tpixels = INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height); @@ -147,7 +164,7 @@ void DCraw::selectCRXTrack(short maxTrack) (get4() != 0x00000008)) int DCraw::parseCR3(unsigned long long oAtomList, unsigned long long szAtomList, short &nesting, - char *AtomNameStack, short &nTrack, short &TrackType) + char *AtomNameStack, unsigned short &nTrack, short &TrackType) { /* Atom starts with 4 bytes for Atom size and 4 bytes containing Atom name @@ -159,8 +176,8 @@ int DCraw::parseCR3(unsigned long long oAtomList, */ const char UIID_Canon[17] = "\x85\xc0\xb6\x87\x82\x0f\x11\xe0\x81\x11\xf4\xce\x46\x2b\x6a\x48"; - const char UIID_Preview[17] = - "\xea\xf4\x2b\x5e\x1c\x98\x4b\x88\xb9\xfb\xb7\xdc\x40\x6e\x4d\x16"; +// const char UIID_Preview[17] = +// "\xea\xf4\x2b\x5e\x1c\x98\x4b\x88\xb9\xfb\xb7\xdc\x40\x6e\x4d\x16"; /* AtomType = 0 - unknown: "unk." @@ -168,7 +185,7 @@ int DCraw::parseCR3(unsigned long long oAtomList, AtomType = 2 - leaf atom: "leaf" AtomType = 3 - can be container, can be leaf: "both" */ - const char sAtomeType[4][5] = {"unk.", "cont", "leaf", "both"}; +// const char sAtomeType[4][5] = {"unk.", "cont", "leaf", "both"}; short AtomType; static const struct { @@ -256,7 +273,8 @@ int DCraw::parseCR3(unsigned long long oAtomList, const char sHandlerType[5][5] = {"unk.", "soun", "vide", "hint", "meta"}; - int c, err = 0; + unsigned int c; + int err = 0; ushort tL; // Atom length represented in 4 or 8 bytes char nmAtom[5]; // Atom name @@ -268,14 +286,14 @@ int DCraw::parseCR3(unsigned long long oAtomList, char UIID[16]; uchar CMP1[36]; char HandlerType[5], MediaFormatID[5]; - unsigned ImageWidth, ImageHeight; +// unsigned ImageWidth, ImageHeight; long relpos_inDir, relpos_inBox; unsigned szItem, Tag, lTag; ushort tItem; nmAtom[0] = MediaFormatID[0] = nmAtom[4] = MediaFormatID[4] = '\0'; strcpy(HandlerType, sHandlerType[0]); - ImageWidth = ImageHeight = 0U; +// ImageWidth = ImageHeight = 0U; oAtom = oAtomList; nesting++; if (nesting > 31) @@ -450,8 +468,8 @@ int DCraw::parseCR3(unsigned long long oAtomList, } #define current_track RT_canon_CR3_data.crx_header[nTrack] - ImageWidth = get2(); - ImageHeight = get2(); +// ImageWidth = get2(); +// ImageHeight = get2(); } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) { @@ -813,7 +831,7 @@ static inline void crxFillBuffer(CrxBitstream *bitStrm) libraw_inline int crxBitstreamGetZeros(CrxBitstream *bitStrm) { - uint32_t bitData = bitStrm->bitData; +// uint32_t bitData = bitStrm->bitData; uint32_t nonZeroBit = 0; uint64_t nextData = 0; int32_t result = 0; @@ -2049,7 +2067,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp *comp, uint32_t level) lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); lineBufH2[i] = delta; } - if (wavelet->curLine >= wavelet->height - 3 && wavelet->height & 1) + if (wavelet->curLine >= wavelet->height - 3 && (wavelet->height & 1)) { wavelet->curH += 3; wavelet->curLine += 3; @@ -2644,10 +2662,9 @@ int crxReadSubbandHeaders(crx_data_header_t *hdr, CrxImage *img, CrxTile *tile, if (!img->subbandCount) return 0; - int32_t curSubband = 0; int32_t subbandOffset = 0; band = comp->subBands; - for (int curSubband = 0; curSubband < img->subbandCount; curSubband++, band++) + for (unsigned int curSubband = 0; curSubband < img->subbandCount; curSubband++, band++) { if (*mdatSize < 0xC) return -1; @@ -2683,7 +2700,7 @@ int crxReadSubbandHeaders(crx_data_header_t *hdr, CrxImage *img, CrxTile *tile, int crxReadImageHeaders(crx_data_header_t *hdr, CrxImage *img, uint8_t *mdatPtr, uint32_t mdatSize) { - int nTiles = img->tileRows * img->tileCols; + unsigned int nTiles = img->tileRows * img->tileCols; if (!nTiles) return -1; @@ -2702,7 +2719,7 @@ int crxReadImageHeaders(crx_data_header_t *hdr, CrxImage *img, uint8_t *mdatPtr, CrxPlaneComp *comps = (CrxPlaneComp *)(tile + nTiles); CrxSubband *bands = (CrxSubband *)(comps + img->nPlanes * nTiles); - for (int curTile = 0; curTile < nTiles; curTile++, tile++) + for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { tile->tileFlag = 0; // tile neighbouring flags tile->tileNumber = curTile; @@ -2780,7 +2797,7 @@ int crxReadImageHeaders(crx_data_header_t *hdr, CrxImage *img, uint8_t *mdatPtr, uint8_t *dataPtr = mdatPtr; CrxTile *tile = img->tiles; - for (int curTile = 0; curTile < nTiles; curTile++, tile++) + for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { if (dataSize < 0xC) return -1; @@ -2997,8 +3014,7 @@ void DCraw::crxLoadFinalizeLoopE3(void *p, int planeHeight) void DCraw::crxLoadRaw() { CrxImage img; - if (RT_canon_CR3_data.crx_track_selected < 0 || - RT_canon_CR3_data.crx_track_selected >= + if (RT_canon_CR3_data.crx_track_selected >= LIBRAW_CRXTRACKS_MAXCOUNT) derror(); crx_data_header_t hdr = @@ -3052,9 +3068,9 @@ void DCraw::crxLoadRaw() crxFreeImageData(&img); } -int DCraw::crxParseImageHeader(uchar *cmp1TagData, int nTrack) +int DCraw::crxParseImageHeader(uchar *cmp1TagData, unsigned int nTrack) { - if (nTrack < 0 || nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) + if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) return -1; if (!cmp1TagData) return -1; @@ -3099,8 +3115,8 @@ int DCraw::crxParseImageHeader(uchar *cmp1TagData, int nTrack) if (hdr->nBits != 8) return -1; } - else if (hdr->nPlanes != 4 || hdr->f_width & 1 || hdr->f_height & 1 || - hdr->tileWidth & 1 || hdr->tileHeight & 1 || hdr->cfaLayout > 3u || + else if (hdr->nPlanes != 4 || (hdr->f_width & 1) || (hdr->f_height & 1) || + (hdr->tileWidth & 1) || (hdr->tileHeight & 1) || hdr->cfaLayout > 3 || (hdr->encType && hdr->encType != 1 && hdr->encType != 3) || hdr->nBits == 8) return -1; @@ -3118,7 +3134,3 @@ int DCraw::crxParseImageHeader(uchar *cmp1TagData, int nTrack) #undef _constrain #undef libraw_inline #undef LIBRAW_CRXTRACKS_MAXCOUNT - -#ifdef __GNUC__ // silence warning -#pragma GCC diagnostic pop -#endif diff --git a/rtengine/dcraw.h b/rtengine/dcraw.h index 75570b065..5b91d7e8b 100644 --- a/rtengine/dcraw.h +++ b/rtengine/dcraw.h @@ -188,7 +188,7 @@ public: }; static constexpr size_t CRXTRACKS_MAXCOUNT = 16; crx_data_header_t crx_header[CRXTRACKS_MAXCOUNT]; - int crx_track_selected; + unsigned int crx_track_selected; short CR3_CTMDtag; }; protected: @@ -562,16 +562,16 @@ void nikon_14bit_load_raw(); // ported from LibRaw // Canon CR3 support ported from LibRaw //----------------------------------------------------------------------------- void parse_canon_cr3(); -void selectCRXTrack(short maxTrack); +void selectCRXTrack(unsigned short maxTrack); int parseCR3(unsigned long long oAtomList, unsigned long long szAtomList, short &nesting, - char *AtomNameStack, short &nTrack, short &TrackType); + char *AtomNameStack, unsigned short &nTrack, short &TrackType); int crxDecodePlane(void *p, uint32_t planeNumber); void crxLoadDecodeLoop(void *img, int nPlanes); void crxConvertPlaneLineDf(void *p, int imageRow); void crxLoadFinalizeLoopE3(void *p, int planeHeight); void crxLoadRaw(); -int crxParseImageHeader(uchar *cmp1TagData, int nTrack); +int crxParseImageHeader(uchar *cmp1TagData, unsigned int nTrack); //----------------------------------------------------------------------------- }; From fb01435e3f65eff5a52642d5131f9159eff21df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 20 Nov 2019 16:05:24 +0100 Subject: [PATCH 04/20] Consider side effects of `get2()` --- rtengine/canon_cr3_decoder.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index e945a4ab8..b69821280 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -468,8 +468,8 @@ int DCraw::parseCR3(unsigned long long oAtomList, } #define current_track RT_canon_CR3_data.crx_header[nTrack] -// ImageWidth = get2(); -// ImageHeight = get2(); + /*ImageWidth = */get2(); + /*ImageHeight = */get2(); } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) { From d986a56c50d008372e7fac565a94a81c7a07c19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Thu, 21 Nov 2019 08:48:33 +0100 Subject: [PATCH 05/20] Fix LGTM warnings --- rtengine/canon_cr3_decoder.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index b69821280..b616a2b42 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -68,8 +68,7 @@ void DCraw::parse_canon_cr3() szAtomList = ifp->size; err = parseCR3(0ULL, szAtomList, nesting, AtomNameStack, nTrack, TrackType); - if ((err == 0 || err == -14) && - nTrack >= 0) // no error, or too deep nesting + if (err == 0 || err == -14) // no error, or too deep nesting selectCRXTrack(nTrack); } @@ -77,8 +76,6 @@ void DCraw::parse_canon_cr3() void DCraw::selectCRXTrack(unsigned short maxTrack) { - if (maxTrack < 0) - return; INT64 bitcounts[LIBRAW_CRXTRACKS_MAXCOUNT], maxbitcount = 0; uint32_t maxjpegbytes = 0; memset(bitcounts, 0, sizeof(bitcounts)); @@ -117,7 +114,7 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) } } is_raw = framecnt; - if (has_framei && framei < LIBRAW_CRXTRACKS_MAXCOUNT) + if (has_framei) { CanonCR3Data::crx_data_header_t *d = &RT_canon_CR3_data.crx_header[framei]; From 319ed6cdd5ac3dbcf5c5b2c7165652a55a34b4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Thu, 21 Nov 2019 09:57:29 +0100 Subject: [PATCH 06/20] Style with uncrustify, astyle, and clang-tidy --- rtengine/canon_cr3_decoder.cc | 5543 +++++++++++++++++---------------- 1 file changed, 2828 insertions(+), 2715 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index b616a2b42..01bca72f8 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -19,7 +19,7 @@ // Code adapted from ART /* - * + * * This file is part of ART. * * ART is free software: you can redistribute it and/or modify @@ -68,535 +68,530 @@ void DCraw::parse_canon_cr3() szAtomList = ifp->size; err = parseCR3(0ULL, szAtomList, nesting, AtomNameStack, nTrack, TrackType); - if (err == 0 || err == -14) // no error, or too deep nesting + + if (err == 0 || err == -14) { // no error, or too deep nesting selectCRXTrack(nTrack); + } } #define LIBRAW_CRXTRACKS_MAXCOUNT RT_canon_CR3_data.CRXTRACKS_MAXCOUNT void DCraw::selectCRXTrack(unsigned short maxTrack) { - INT64 bitcounts[LIBRAW_CRXTRACKS_MAXCOUNT], maxbitcount = 0; - uint32_t maxjpegbytes = 0; - memset(bitcounts, 0, sizeof(bitcounts)); - for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) - { - CanonCR3Data::crx_data_header_t *d = &RT_canon_CR3_data.crx_header[i]; - if (d->MediaType == 1) // RAW - { - bitcounts[i] = INT64(d->nBits) * INT64(d->f_width) * INT64(d->f_height); - if (bitcounts[i] > maxbitcount) - maxbitcount = bitcounts[i]; - } - else if (d->MediaType == 2) // JPEG - { - if (d->MediaSize > maxjpegbytes) - { - maxjpegbytes = d->MediaSize; - thumb_offset = d->MediaOffset; - thumb_length = d->MediaSize; - } - } - } - if (maxbitcount < 8) - return; - bool has_framei = false; - unsigned int framei = 0, framecnt = 0; - for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) - { - if (bitcounts[i] == maxbitcount) - { - if (framecnt <= shot_select) { - has_framei = true; - framei = i; - } - framecnt++; - } - } - is_raw = framecnt; - if (has_framei) - { - CanonCR3Data::crx_data_header_t *d = - &RT_canon_CR3_data.crx_header[framei]; - data_offset = d->MediaOffset; - //data_size = d->MediaSize; - raw_width = d->f_width; - raw_height = d->f_height; - load_raw = &DCraw::crxLoadRaw; - switch (d->cfaLayout) - { - case 0: - filters = 0x94949494; - break; - case 1: - filters = 0x61616161; - break; - case 2: - filters = 0x49494949; - break; - case 3: - filters = 0x16161616; - break; + INT64 bitcounts[LIBRAW_CRXTRACKS_MAXCOUNT], maxbitcount = 0; + uint32_t maxjpegbytes = 0; + memset(bitcounts, 0, sizeof(bitcounts)); + + for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) { + CanonCR3Data::crx_data_header_t* d = &RT_canon_CR3_data.crx_header[i]; + + if (d->MediaType == 1) {// RAW + bitcounts[i] = INT64(d->nBits) * INT64(d->f_width) * INT64(d->f_height); + + if (bitcounts[i] > maxbitcount) { + maxbitcount = bitcounts[i]; + } + } else if (d->MediaType == 2) {// JPEG + if (d->MediaSize > maxjpegbytes) { + maxjpegbytes = d->MediaSize; + thumb_offset = d->MediaOffset; + thumb_length = d->MediaSize; + } + } } - RT_canon_CR3_data.crx_track_selected = framei; + if (maxbitcount < 8) { + return; + } - int tiff_idx = -1; - INT64 tpixels = 0; - for (unsigned int i = 0; i < tiff_nifds; i++) - if (INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height) > tpixels) - { - tpixels = INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height); - tiff_idx = i; - } - if (tiff_idx >= 0) - flip = tiff_ifd[tiff_idx].flip; - } + bool has_framei = false; + unsigned int framei = 0, framecnt = 0; + + for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) { + if (bitcounts[i] == maxbitcount) { + if (framecnt <= shot_select) { + has_framei = true; + framei = i; + } + + framecnt++; + } + } + + is_raw = framecnt; + + if (has_framei) { + CanonCR3Data::crx_data_header_t* d = + &RT_canon_CR3_data.crx_header[framei]; + data_offset = d->MediaOffset; + // data_size = d->MediaSize; + raw_width = d->f_width; + raw_height = d->f_height; + load_raw = &DCraw::crxLoadRaw; + + switch (d->cfaLayout) { + case 0: { + filters = 0x94949494; + break; + } + + case 1: { + filters = 0x61616161; + break; + } + + case 2: { + filters = 0x49494949; + break; + } + + case 3: { + filters = 0x16161616; + break; + } + } + + RT_canon_CR3_data.crx_track_selected = framei; + + int tiff_idx = -1; + INT64 tpixels = 0; + + for (unsigned int i = 0; i < tiff_nifds; i++) { + if (INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height) > tpixels) { + tpixels = INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height); + tiff_idx = i; + } + } + + if (tiff_idx >= 0) { + flip = tiff_ifd[tiff_idx].flip; + } + } } -#define FORC4 for (c=0; c < 4; c++) +#define FORC4 for (c = 0; c < 4; c++) #define bad_hdr \ - (((order != 0x4d4d) && (order != 0x4949)) || (get2() != 0x002a) || \ - (get4() != 0x00000008)) + (((order != 0x4d4d) && (order != 0x4949)) || (get2() != 0x002a) || \ + (get4() != 0x00000008)) int DCraw::parseCR3(unsigned long long oAtomList, - unsigned long long szAtomList, short &nesting, - char *AtomNameStack, unsigned short &nTrack, short &TrackType) + unsigned long long szAtomList, short& nesting, + char* AtomNameStack, unsigned short& nTrack, short& TrackType) { - /* - Atom starts with 4 bytes for Atom size and 4 bytes containing Atom name - Atom size includes the length of the header and the size of all "contained" - Atoms if Atom size == 1, Atom has the extended size stored in 8 bytes located - after the Atom name if Atom size == 0, it is the last top-level Atom extending - to the end of the file Atom name is often a 4 symbol mnemonic, but can be a - 4-byte integer - */ - const char UIID_Canon[17] = - "\x85\xc0\xb6\x87\x82\x0f\x11\xe0\x81\x11\xf4\xce\x46\x2b\x6a\x48"; + /* + Atom starts with 4 bytes for Atom size and 4 bytes containing Atom name + Atom size includes the length of the header and the size of all "contained" + Atoms if Atom size == 1, Atom has the extended size stored in 8 bytes located + after the Atom name if Atom size == 0, it is the last top-level Atom extending + to the end of the file Atom name is often a 4 symbol mnemonic, but can be a + 4-byte integer + */ + const char UIID_Canon[17] = + "\x85\xc0\xb6\x87\x82\x0f\x11\xe0\x81\x11\xf4\xce\x46\x2b\x6a\x48"; // const char UIID_Preview[17] = // "\xea\xf4\x2b\x5e\x1c\x98\x4b\x88\xb9\xfb\xb7\xdc\x40\x6e\x4d\x16"; - /* - AtomType = 0 - unknown: "unk." - AtomType = 1 - container atom: "cont" - AtomType = 2 - leaf atom: "leaf" - AtomType = 3 - can be container, can be leaf: "both" - */ + /* + AtomType = 0 - unknown: "unk." + AtomType = 1 - container atom: "cont" + AtomType = 2 - leaf atom: "leaf" + AtomType = 3 - can be container, can be leaf: "both" + */ // const char sAtomeType[4][5] = {"unk.", "cont", "leaf", "both"}; - short AtomType; - static const struct - { - char AtomName[5]; short AtomType; - } AtomNamesList[] = { - {"dinf", 1}, - {"edts", 1}, - {"fiin", 1}, - {"ipro", 1}, - {"iprp", 1}, - {"mdia", 1}, - {"meco", 1}, - {"mere", 1}, - {"mfra", 1}, - {"minf", 1}, - {"moof", 1}, - {"moov", 1}, - {"mvex", 1}, - {"paen", 1}, - {"schi", 1}, - {"sinf", 1}, - {"skip", 1}, - {"stbl", 1}, - {"stsd", 1}, - {"strk", 1}, - {"tapt", 1}, - {"traf", 1}, - {"trak", 1}, + static const struct { + char AtomName[5]; + short AtomType; + } AtomNamesList[] = { + {"dinf", 1}, + {"edts", 1}, + {"fiin", 1}, + {"ipro", 1}, + {"iprp", 1}, + {"mdia", 1}, + {"meco", 1}, + {"mere", 1}, + {"mfra", 1}, + {"minf", 1}, + {"moof", 1}, + {"moov", 1}, + {"mvex", 1}, + {"paen", 1}, + {"schi", 1}, + {"sinf", 1}, + {"skip", 1}, + {"stbl", 1}, + {"stsd", 1}, + {"strk", 1}, + {"tapt", 1}, + {"traf", 1}, + {"trak", 1}, - {"cdsc", 2}, - {"colr", 2}, - {"dimg", 2}, - // {"dref", 2}, - {"free", 2}, - {"frma", 2}, - {"ftyp", 2}, - {"hdlr", 2}, - {"hvcC", 2}, - {"iinf", 2}, - {"iloc", 2}, - {"infe", 2}, - {"ipco", 2}, - {"ipma", 2}, - {"iref", 2}, - {"irot", 2}, - {"ispe", 2}, - {"meta", 2}, - {"mvhd", 2}, - {"pitm", 2}, - {"pixi", 2}, - {"schm", 2}, - {"thmb", 2}, - {"tkhd", 2}, - {"url ", 2}, - {"urn ", 2}, + {"cdsc", 2}, + {"colr", 2}, + {"dimg", 2}, + // {"dref", 2}, + {"free", 2}, + {"frma", 2}, + {"ftyp", 2}, + {"hdlr", 2}, + {"hvcC", 2}, + {"iinf", 2}, + {"iloc", 2}, + {"infe", 2}, + {"ipco", 2}, + {"ipma", 2}, + {"iref", 2}, + {"irot", 2}, + {"ispe", 2}, + {"meta", 2}, + {"mvhd", 2}, + {"pitm", 2}, + {"pixi", 2}, + {"schm", 2}, + {"thmb", 2}, + {"tkhd", 2}, + {"url ", 2}, + {"urn ", 2}, - {"CCTP", 1}, - {"CRAW", 1}, + {"CCTP", 1}, + {"CRAW", 1}, - {"JPEG", 2}, - {"CDI1", 2}, - {"CMP1", 2}, + {"JPEG", 2}, + {"CDI1", 2}, + {"CMP1", 2}, - {"CNCV", 2}, - {"CCDT", 2}, - {"CTBO", 2}, - {"CMT1", 2}, - {"CMT2", 2}, - {"CMT3", 2}, - {"CMT4", 2}, - {"THMB", 2}, - {"co64", 2}, - {"mdat", 2}, - {"mdhd", 2}, - {"nmhd", 2}, - {"stsc", 2}, - {"stsz", 2}, - {"stts", 2}, - {"vmhd", 2}, + {"CNCV", 2}, + {"CCDT", 2}, + {"CTBO", 2}, + {"CMT1", 2}, + {"CMT2", 2}, + {"CMT3", 2}, + {"CMT4", 2}, + {"THMB", 2}, + {"co64", 2}, + {"mdat", 2}, + {"mdhd", 2}, + {"nmhd", 2}, + {"stsc", 2}, + {"stsz", 2}, + {"stts", 2}, + {"vmhd", 2}, - {"dref", 3}, - {"uuid", 3}, - }; + {"dref", 3}, + {"uuid", 3}, + }; - const char sHandlerType[5][5] = {"unk.", "soun", "vide", "hint", "meta"}; + const char sHandlerType[5][5] = {"unk.", "soun", "vide", "hint", "meta"}; - unsigned int c; - int err = 0; + unsigned int c; + int err = 0; - ushort tL; // Atom length represented in 4 or 8 bytes - char nmAtom[5]; // Atom name - unsigned long long oAtom, szAtom; // Atom offset and Atom size - unsigned long long oAtomContent, - szAtomContent; // offset and size of Atom content - unsigned long long lHdr; + ushort tL; // Atom length represented in 4 or 8 bytes + char nmAtom[5]; // Atom name + unsigned long long oAtom, szAtom; // Atom offset and Atom size + unsigned long long oAtomContent, + szAtomContent; // offset and size of Atom content + unsigned long long lHdr; - char UIID[16]; - uchar CMP1[36]; - char HandlerType[5], MediaFormatID[5]; + char UIID[16]; + uchar CMP1[36]; + char HandlerType[5], MediaFormatID[5]; // unsigned ImageWidth, ImageHeight; - long relpos_inDir, relpos_inBox; - unsigned szItem, Tag, lTag; - ushort tItem; + long relpos_inDir, relpos_inBox; + unsigned szItem, Tag, lTag; + ushort tItem; - nmAtom[0] = MediaFormatID[0] = nmAtom[4] = MediaFormatID[4] = '\0'; - strcpy(HandlerType, sHandlerType[0]); + nmAtom[0] = MediaFormatID[0] = nmAtom[4] = MediaFormatID[4] = '\0'; + strcpy(HandlerType, sHandlerType[0]); // ImageWidth = ImageHeight = 0U; - oAtom = oAtomList; - nesting++; - if (nesting > 31) - return -14; // too deep nesting - short s_order = order; + oAtom = oAtomList; + nesting++; - while ((oAtom + 8ULL) <= (oAtomList + szAtomList)) - { - lHdr = 0ULL; - err = 0; - order = 0x4d4d; - fseek(ifp, oAtom, SEEK_SET); - szAtom = get4(); - FORC4 nmAtom[c] = AtomNameStack[nesting * 4 + c] = fgetc(ifp); - AtomNameStack[(nesting + 1) * 4] = '\0'; - tL = 4; - AtomType = 0; - - for (c = 0; c < sizeof AtomNamesList / sizeof *AtomNamesList; c++) - if (!strcmp(nmAtom, AtomNamesList[c].AtomName)) - { - AtomType = AtomNamesList[c].AtomType; - break; - } - - if (!AtomType) - { - err = 1; + if (nesting > 31) { + return -14; // too deep nesting } - if (szAtom == 0ULL) - { - if (nesting != 0) - { - err = -2; - goto fin; - } - szAtom = szAtomList - oAtom; - oAtomContent = oAtom + 8ULL; - szAtomContent = szAtom - 8ULL; - } - else if (szAtom == 1ULL) - { - if ((oAtom + 16ULL) > (oAtomList + szAtomList)) - { - err = -3; - goto fin; - } - tL = 8; - szAtom = (((unsigned long long)get4()) << 32) | get4(); - oAtomContent = oAtom + 16ULL; - szAtomContent = szAtom - 16ULL; - } - else - { - oAtomContent = oAtom + 8ULL; - szAtomContent = szAtom - 8ULL; - } + short s_order = order; - if (!strcmp(nmAtom, "trak")) - { - nTrack++; - TrackType = 0; - if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) - break; - } - if (!strcmp(AtomNameStack, "moovuuid")) - { - lHdr = 16ULL; - fread(UIID, 1, lHdr, ifp); - if (!strncmp(UIID, UIID_Canon, lHdr)) - { - AtomType = 1; - } - else - fseek(ifp, -lHdr, SEEK_CUR); - } - else if (!strcmp(AtomNameStack, "moovuuidCCTP")) - { - lHdr = 12ULL; - } - else if (!strcmp(AtomNameStack, "moovuuidCMT1")) - { - short q_order = order; - order = get2(); - if ((tL != 4) || bad_hdr) - { - err = -4; - goto fin; - } - parse_tiff_ifd(oAtomContent); - order = q_order; - } - else if (!strcmp(AtomNameStack, "moovuuidCMT2")) - { - short q_order = order; - order = get2(); - if ((tL != 4) || bad_hdr) - { - err = -5; - goto fin; - } - parse_exif(oAtomContent); - order = q_order; - } - else if (!strcmp(AtomNameStack, "moovuuidCMT3")) - { - short q_order = order; - order = get2(); - if ((tL != 4) || bad_hdr) - { - err = -6; - goto fin; - } - fseek(ifp, -12L, SEEK_CUR); - parse_makernote(oAtomContent, 0); - order = q_order; - } - else if (!strcmp(AtomNameStack, "moovuuidCMT4")) - { - short q_order = order; - order = get2(); - if ((tL != 4) || bad_hdr) - { - err = -6; - goto fin; - } - INT64 off = ftell(ifp); - parse_gps(oAtomContent); - fseek(ifp, off, SEEK_SET); + while ((oAtom + 8ULL) <= (oAtomList + szAtomList)) { + lHdr = 0ULL; + err = 0; + order = 0x4d4d; + fseek(ifp, oAtom, SEEK_SET); + szAtom = get4(); + FORC4 nmAtom[c] = AtomNameStack[nesting * 4 + c] = fgetc(ifp); + AtomNameStack[(nesting + 1) * 4] = '\0'; + tL = 4; + AtomType = 0; + + for (c = 0; c < sizeof AtomNamesList / sizeof * AtomNamesList; c++) { + if (!strcmp(nmAtom, AtomNamesList[c].AtomName)) { + AtomType = AtomNamesList[c].AtomType; + break; + } + } + + if (!AtomType) { + err = 1; + } + + if (szAtom == 0ULL) { + if (nesting != 0) { + err = -2; + goto fin; + } + + szAtom = szAtomList - oAtom; + oAtomContent = oAtom + 8ULL; + szAtomContent = szAtom - 8ULL; + } else if (szAtom == 1ULL) { + if ((oAtom + 16ULL) > (oAtomList + szAtomList)) { + err = -3; + goto fin; + } + + tL = 8; + szAtom = (((unsigned long long)get4()) << 32) | get4(); + oAtomContent = oAtom + 16ULL; + szAtomContent = szAtom - 16ULL; + } else { + oAtomContent = oAtom + 8ULL; + szAtomContent = szAtom - 8ULL; + } + + if (!strcmp(nmAtom, "trak")) { + nTrack++; + TrackType = 0; + + if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) { + break; + } + } + + if (!strcmp(AtomNameStack, "moovuuid")) { + lHdr = 16ULL; + fread(UIID, 1, lHdr, ifp); + + if (!strncmp(UIID, UIID_Canon, lHdr)) { + AtomType = 1; + } else { + fseek(ifp, -lHdr, SEEK_CUR); + } + } else if (!strcmp(AtomNameStack, "moovuuidCCTP")) { + lHdr = 12ULL; + } else if (!strcmp(AtomNameStack, "moovuuidCMT1")) { + short q_order = order; + order = get2(); + + if ((tL != 4) || bad_hdr) { + err = -4; + goto fin; + } + + parse_tiff_ifd(oAtomContent); + order = q_order; + } else if (!strcmp(AtomNameStack, "moovuuidCMT2")) { + short q_order = order; + order = get2(); + + if ((tL != 4) || bad_hdr) { + err = -5; + goto fin; + } + + parse_exif(oAtomContent); + order = q_order; + } else if (!strcmp(AtomNameStack, "moovuuidCMT3")) { + short q_order = order; + order = get2(); + + if ((tL != 4) || bad_hdr) { + err = -6; + goto fin; + } + + fseek(ifp, -12L, SEEK_CUR); + parse_makernote(oAtomContent, 0); + order = q_order; + } else if (!strcmp(AtomNameStack, "moovuuidCMT4")) { + short q_order = order; + order = get2(); + + if ((tL != 4) || bad_hdr) { + err = -6; + goto fin; + } + + INT64 off = ftell(ifp); + parse_gps(oAtomContent); + fseek(ifp, off, SEEK_SET); // parse_gps_libraw(oAtomContent); - order = q_order; - } - else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) - { - fseek(ifp, 8L, SEEK_CUR); - FORC4 HandlerType[c] = fgetc(ifp); - for (c = 1; c < sizeof sHandlerType / sizeof *sHandlerType; c++) - if (!strcmp(HandlerType, sHandlerType[c])) - { - TrackType = c; - break; - } - } - else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsd")) - { - if (szAtomContent >= 16) - { - fseek(ifp, 12L, SEEK_CUR); - lHdr = 8; - } - else - { - err = -7; - goto fin; - } - FORC4 MediaFormatID[c] = fgetc(ifp); - if ((TrackType == 2) && (!strcmp(MediaFormatID, "CRAW"))) - { - if (szAtomContent >= 44) - fseek(ifp, 24L, SEEK_CUR); - else - { - err = -8; - goto fin; - } - } - else - { - AtomType = 2; // only continue for CRAW - lHdr = 0; - } + order = q_order; + } else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) { + fseek(ifp, 8L, SEEK_CUR); + FORC4 HandlerType[c] = fgetc(ifp); + + for (c = 1; c < sizeof sHandlerType / sizeof * sHandlerType; c++) { + if (!strcmp(HandlerType, sHandlerType[c])) { + TrackType = c; + break; + } + } + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsd")) { + if (szAtomContent >= 16) { + fseek(ifp, 12L, SEEK_CUR); + lHdr = 8; + } else { + err = -7; + goto fin; + } + + FORC4 MediaFormatID[c] = fgetc(ifp); + + if ((TrackType == 2) && (!strcmp(MediaFormatID, "CRAW"))) { + if (szAtomContent >= 44) { + fseek(ifp, 24L, SEEK_CUR); + } else { + err = -8; + goto fin; + } + } else { + AtomType = 2; // only continue for CRAW + lHdr = 0; + } + #define current_track RT_canon_CR3_data.crx_header[nTrack] - /*ImageWidth = */get2(); - /*ImageHeight = */get2(); - } - else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) - { - lHdr = 82; - } - else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWCMP1")) - { - if (szAtomContent >= 40) - fread(CMP1, 1, 36, ifp); - else - { - err = -7; - goto fin; - } - if (!crxParseImageHeader(CMP1, nTrack)) - current_track.MediaType = 1; - } - else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWJPEG")) - { - current_track.MediaType = 2; - } - else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsz")) - { - if (szAtomContent == 12) - fseek(ifp, 4L, SEEK_CUR); - else if (szAtomContent == 16) - fseek(ifp, 12L, SEEK_CUR); - else - { - err = -9; - goto fin; - } - current_track.MediaSize = get4(); - } - else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblco64")) - { - if (szAtomContent == 16) - fseek(ifp, 8L, SEEK_CUR); - else - { - err = -10; - goto fin; - } - current_track.MediaOffset = (((unsigned long long)get4()) << 32) | get4(); - } - - if (current_track.MediaSize && current_track.MediaOffset && - ((oAtom + szAtom) >= (oAtomList + szAtomList)) && - !strncmp(AtomNameStack, "moovtrakmdiaminfstbl", 20)) - { - if ((TrackType == 4) && (!strcmp(MediaFormatID, "CTMD"))) - { - order = 0x4949; - relpos_inDir = 0L; - while (relpos_inDir + 6 < current_track.MediaSize) - { - fseek(ifp, current_track.MediaOffset + relpos_inDir, SEEK_SET); - szItem = get4(); - tItem = get2(); - if ((relpos_inDir + szItem) > current_track.MediaSize) - { - err = -11; - goto fin; - } - if ((tItem == 7) || (tItem == 8) || (tItem == 9)) - { - relpos_inBox = relpos_inDir + 12L; - while (relpos_inBox + 8 < relpos_inDir + szItem) - { - fseek(ifp, current_track.MediaOffset + relpos_inBox, SEEK_SET); - lTag = get4(); - Tag = get4(); - if (lTag < 8) - { - err = -12; + /*ImageWidth = */ get2(); + /*ImageHeight = */ get2(); + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) { + lHdr = 82; + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWCMP1")) { + if (szAtomContent >= 40) { + fread(CMP1, 1, 36, ifp); + } else { + err = -7; goto fin; - } - else if ((relpos_inBox + lTag) > (relpos_inDir + szItem)) - { - err = -11; - goto fin; - } - if ((Tag == 0x927c) && ((tItem == 7) || (tItem == 8))) - { - fseek(ifp, current_track.MediaOffset + relpos_inBox + 8L, - SEEK_SET); - short q_order = order; - order = get2(); - if (bad_hdr) - { - err = -13; - goto fin; - } - fseek(ifp, -8L, SEEK_CUR); - RT_canon_CR3_data.CR3_CTMDtag = 1; - parse_makernote(current_track.MediaOffset + relpos_inBox + 8, - 0); - RT_canon_CR3_data.CR3_CTMDtag = 0; - order = q_order; - } - relpos_inBox += lTag; } - } - relpos_inDir += szItem; + + if (!crxParseImageHeader(CMP1, nTrack)) { + current_track.MediaType = 1; + } + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWJPEG")) { + current_track.MediaType = 2; + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsz")) { + if (szAtomContent == 12) { + fseek(ifp, 4L, SEEK_CUR); + } else if (szAtomContent == 16) { + fseek(ifp, 12L, SEEK_CUR); + } else { + err = -9; + goto fin; + } + + current_track.MediaSize = get4(); + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblco64")) { + if (szAtomContent == 16) { + fseek(ifp, 8L, SEEK_CUR); + } else { + err = -10; + goto fin; + } + + current_track.MediaOffset = (((unsigned long long)get4()) << 32) | get4(); } - order = 0x4d4d; - } - } + + if (current_track.MediaSize && current_track.MediaOffset && + ((oAtom + szAtom) >= (oAtomList + szAtomList)) && + !strncmp(AtomNameStack, "moovtrakmdiaminfstbl", 20)) { + if ((TrackType == 4) && (!strcmp(MediaFormatID, "CTMD"))) { + order = 0x4949; + relpos_inDir = 0L; + + while (relpos_inDir + 6 < current_track.MediaSize) { + fseek(ifp, current_track.MediaOffset + relpos_inDir, SEEK_SET); + szItem = get4(); + tItem = get2(); + + if ((relpos_inDir + szItem) > current_track.MediaSize) { + err = -11; + goto fin; + } + + if ((tItem == 7) || (tItem == 8) || (tItem == 9)) { + relpos_inBox = relpos_inDir + 12L; + + while (relpos_inBox + 8 < relpos_inDir + szItem) { + fseek(ifp, current_track.MediaOffset + relpos_inBox, SEEK_SET); + lTag = get4(); + Tag = get4(); + + if (lTag < 8) { + err = -12; + goto fin; + } else if ((relpos_inBox + lTag) > (relpos_inDir + szItem)) { + err = -11; + goto fin; + } + + if ((Tag == 0x927c) && ((tItem == 7) || (tItem == 8))) { + fseek(ifp, current_track.MediaOffset + relpos_inBox + 8L, + SEEK_SET); + short q_order = order; + order = get2(); + + if (bad_hdr) { + err = -13; + goto fin; + } + + fseek(ifp, -8L, SEEK_CUR); + RT_canon_CR3_data.CR3_CTMDtag = 1; + parse_makernote(current_track.MediaOffset + relpos_inBox + 8, + 0); + RT_canon_CR3_data.CR3_CTMDtag = 0; + order = q_order; + } + + relpos_inBox += lTag; + } + } + + relpos_inDir += szItem; + } + + order = 0x4d4d; + } + } + #undef current_track - if (AtomType == 1) - { - err = parseCR3(oAtomContent + lHdr, szAtomContent - lHdr, nesting, - AtomNameStack, nTrack, TrackType); - if (err) - goto fin; + + if (AtomType == 1) { + err = parseCR3(oAtomContent + lHdr, szAtomContent - lHdr, nesting, + AtomNameStack, nTrack, TrackType); + + if (err) { + goto fin; + } + } + + oAtom += szAtom; } - oAtom += szAtom; - } fin: - nesting--; - if (nesting >= 0) - AtomNameStack[nesting * 4] = '\0'; - order = s_order; - return err; + nesting--; + + if (nesting >= 0) { + AtomNameStack[nesting * 4] = '\0'; + } + + order = s_order; + return err; } #undef bad_hdr - -//----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- #ifdef _abs #undef _abs @@ -607,17 +602,17 @@ fin: #define _min(a, b) ((a) < (b) ? (a) : (b)) #define _constrain(x, l, u) ((x) < (l) ? (l) : ((x) > (u) ? (u) : (x))) -#if defined(__clang__) || defined(__GNUG__) +#if defined (__clang__) || defined (__GNUG__) #define libraw_inline inline __attribute__((always_inline)) -#elif defined(_MSC_VER) && _MSC_VER > 1400 +#elif defined (_MSC_VER) && _MSC_VER > 1400 #define libraw_inline __forceinline #else #define libraw_inline inline #endif -namespace { - -static unsigned sgetn (int n, unsigned char *s) +namespace +{ +static unsigned sgetn(int n, unsigned char* s) { unsigned result = 0; @@ -630,13 +625,13 @@ static unsigned sgetn (int n, unsigned char *s) // this should be divisible by 4 #define CRX_BUF_SIZE 0x10000 -#if !defined(_WIN32) || (defined (__GNUC__) && !defined(__INTRINSIC_SPECIAL__BitScanReverse)) +#if !defined (_WIN32) || (defined (__GNUC__) && !defined (__INTRINSIC_SPECIAL__BitScanReverse)) /* __INTRINSIC_SPECIAL__BitScanReverse found in MinGW32-W64 v7.30 headers, may be there is a better solution? */ -typedef uint32_t DWORD; -typedef uint8_t byte; -libraw_inline void _BitScanReverse(DWORD *Index, unsigned long Mask) +using DWORD = uint32_t; +using byte = uint8_t; +libraw_inline void _BitScanReverse(DWORD* Index, unsigned long Mask) { - *Index = sizeof(unsigned long) * 8 - 1 - __builtin_clzl(Mask); + *Index = sizeof(unsigned long) * 8 - 1 - __builtin_clzl(Mask); } uint32_t _byteswap_ulong(uint32_t x) { @@ -648,128 +643,128 @@ uint32_t _byteswap_ulong(uint32_t x) } #endif - #define LIBRAW_EXCEPTION_IO_EOF std::exception() struct LibRaw_abstract_datastream { - IMFILE *ifp; - - void lock() {} - void unlock() {} - void seek(int p, int how) { fseek(ifp, p, how); } + IMFILE* ifp; + + void lock() + { + } + void unlock() + { + } + void seek(int p, int how) + { + fseek(ifp, p, how); + } int read(void* dst, int es, int count) - { return fread(dst, es, count, ifp); } + { + return fread(dst, es, count, ifp); + } }; -struct CrxBitstream -{ - uint8_t mdatBuf[CRX_BUF_SIZE]; - uint64_t mdatSize; - uint64_t curBufOffset; - uint32_t curPos; - uint32_t curBufSize; - uint32_t bitData; - int32_t bitsLeft; - LibRaw_abstract_datastream *input; +struct CrxBitstream { + uint8_t mdatBuf[CRX_BUF_SIZE]; + uint64_t mdatSize; + uint64_t curBufOffset; + uint32_t curPos; + uint32_t curBufSize; + uint32_t bitData; + int32_t bitsLeft; + LibRaw_abstract_datastream* input; }; -struct CrxBandParam -{ - CrxBitstream bitStream; - int16_t subbandWidth; - int16_t subbandHeight; - int32_t roundedBitsMask; - int32_t roundedBits; - int16_t curLine; - int32_t *lineBuf0; - int32_t *lineBuf1; - int32_t *lineBuf2; - int32_t sParam; - int32_t kParam; - int32_t *paramData; - int32_t *nonProgrData; - int8_t supportsPartial; +struct CrxBandParam { + CrxBitstream bitStream; + int16_t subbandWidth; + int16_t subbandHeight; + int32_t roundedBitsMask; + int32_t roundedBits; + int16_t curLine; + int32_t* lineBuf0; + int32_t* lineBuf1; + int32_t* lineBuf2; + int32_t sParam; + int32_t kParam; + int32_t* paramData; + int32_t* nonProgrData; + int8_t supportsPartial; }; -struct CrxWaveletTransform -{ - int32_t *subband0Buf; - int32_t *subband1Buf; - int32_t *subband2Buf; - int32_t *subband3Buf; - int32_t *lineBuf[8]; - int16_t curLine; - int16_t curH; - int8_t fltTapH; - int16_t height; - int16_t width; +struct CrxWaveletTransform { + int32_t* subband0Buf; + int32_t* subband1Buf; + int32_t* subband2Buf; + int32_t* subband3Buf; + int32_t* lineBuf[8]; + int16_t curLine; + int16_t curH; + int8_t fltTapH; + int16_t height; + int16_t width; }; -struct CrxSubband -{ - CrxBandParam *bandParam; - uint64_t mdatOffset; - uint8_t *bandBuf; - int32_t bandSize; - uint64_t dataSize; - int8_t supportsPartial; - int32_t quantValue; - uint16_t width; - uint16_t height; - int32_t paramK; - int64_t dataOffset; +struct CrxSubband { + CrxBandParam* bandParam; + uint64_t mdatOffset; + uint8_t* bandBuf; + int32_t bandSize; + uint64_t dataSize; + int8_t supportsPartial; + int32_t quantValue; + uint16_t width; + uint16_t height; + int32_t paramK; + int64_t dataOffset; }; -struct CrxPlaneComp -{ - byte *compBuf; - CrxSubband *subBands; - CrxWaveletTransform *waveletTransform; - int8_t compNumber; - int64_t dataOffset; - int32_t compSize; - int8_t supportsPartial; - int32_t roundedBitsMask; - int8_t tileFlag; +struct CrxPlaneComp { + byte* compBuf; + CrxSubband* subBands; + CrxWaveletTransform* waveletTransform; + int8_t compNumber; + int64_t dataOffset; + int32_t compSize; + int8_t supportsPartial; + int32_t roundedBitsMask; + int8_t tileFlag; }; -struct CrxTile -{ - CrxPlaneComp *comps; - int8_t tileFlag; - int8_t tileNumber; - int64_t dataOffset; - int32_t tileSize; - uint16_t width; - uint16_t height; +struct CrxTile { + CrxPlaneComp* comps; + int8_t tileFlag; + int8_t tileNumber; + int64_t dataOffset; + int32_t tileSize; + uint16_t width; + uint16_t height; }; -struct CrxImage -{ - uint8_t nPlanes; - uint16_t planeWidth; - uint16_t planeHeight; - uint8_t samplePrecision; - uint8_t subbandCount; - uint8_t levels; - uint8_t nBits; - uint8_t encType; - uint8_t tileCols; - uint8_t tileRows; - CrxTile *tiles; - uint64_t mdatOffset; - uint64_t mdatSize; - int16_t *outBufs[4]; // one per plane - int16_t *planeBuf; - LibRaw_abstract_datastream *input; +struct CrxImage { + uint8_t nPlanes; + uint16_t planeWidth; + uint16_t planeHeight; + uint8_t samplePrecision; + uint8_t subbandCount; + uint8_t levels; + uint8_t nBits; + uint8_t encType; + uint8_t tileCols; + uint8_t tileRows; + CrxTile* tiles; + uint64_t mdatOffset; + uint64_t mdatSize; + int16_t* outBufs[4];// one per plane + int16_t* planeBuf; + LibRaw_abstract_datastream* input; }; -enum TileFlags -{ - E_HAS_TILES_ON_THE_RIGHT = 1, - E_HAS_TILES_ON_THE_LEFT = 2, - E_HAS_TILES_ON_THE_BOTTOM = 4, - E_HAS_TILES_ON_THE_TOP = 8 +enum TileFlags { + E_HAS_TILES_ON_THE_RIGHT = 1, + E_HAS_TILES_ON_THE_LEFT = 2, + E_HAS_TILES_ON_THE_BOTTOM = 4, + E_HAS_TILES_ON_THE_TOP = 8 }; int32_t exCoefNumTbl[0x120] = { @@ -789,2341 +784,2459 @@ int32_t exCoefNumTbl[0x120] = { 1, 1, 7, 7, 1, 1, 3, 3, 1, 1, 1, 1, 1, 0, 7, 6, 1, 0, 3, 2, 1, 0, 1, 0, 1, 2, 10, 10, 2, 2, 5, 4, 2, 1, 2, 1, 1, 1, 10, 9, 1, 2, 4, 4, 2, 1, 2, 1, 1, 1, 9, 9, 1, 2, 4, 4, 2, 1, 2, 1, 1, 0, 9, 8, 1, 1, 4, 3, 1, 1, 1, 1, 1, 2, - 8, 8, 2, 1, 4, 3, 1, 1, 1, 1, 1, 1, 8, 7, 1, 1, 3, 3, 1, 1, 1, 1}; + 8, 8, 2, 1, 4, 3, 1, 1, 1, 1, 1, 1, 8, 7, 1, 1, 3, 3, 1, 1, 1, 1 +}; uint32_t JS[32] = {1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 0x10, 0x10, 0x20, 0x20, 0x40, 0x40, 0x80, 0x80, - 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000}; + 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 + }; uint32_t J[32] = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, - 7, 7, 8, 9, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; + 7, 7, 8, 9, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + }; -static inline void crxFillBuffer(CrxBitstream *bitStrm) +static inline void crxFillBuffer(CrxBitstream* bitStrm) { - if (bitStrm->curPos >= bitStrm->curBufSize && bitStrm->mdatSize) - { - bitStrm->curPos = 0; - bitStrm->curBufOffset += bitStrm->curBufSize; + if (bitStrm->curPos >= bitStrm->curBufSize && bitStrm->mdatSize) { + bitStrm->curPos = 0; + bitStrm->curBufOffset += bitStrm->curBufSize; #ifdef _OPENMP -#pragma omp critical + #pragma omp critical #endif - { + { #ifndef _OPENMP - bitStrm->input->lock(); + bitStrm->input->lock(); #endif - bitStrm->input->seek(bitStrm->curBufOffset, SEEK_SET); - bitStrm->curBufSize = bitStrm->input->read( - bitStrm->mdatBuf, 1, _min(bitStrm->mdatSize, CRX_BUF_SIZE)); + bitStrm->input->seek(bitStrm->curBufOffset, SEEK_SET); + bitStrm->curBufSize = bitStrm->input->read( + bitStrm->mdatBuf, 1, _min(bitStrm->mdatSize, CRX_BUF_SIZE)); #ifndef _OPENMP - bitStrm->input->unlock(); + bitStrm->input->unlock(); #endif - if (bitStrm->curBufSize < 1) // nothing read - throw LIBRAW_EXCEPTION_IO_EOF; - bitStrm->mdatSize -= bitStrm->curBufSize; + + if (bitStrm->curBufSize < 1) { // nothing read + throw LIBRAW_EXCEPTION_IO_EOF; + } + + bitStrm->mdatSize -= bitStrm->curBufSize; + } } - } } -libraw_inline int crxBitstreamGetZeros(CrxBitstream *bitStrm) +libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) { // uint32_t bitData = bitStrm->bitData; - uint32_t nonZeroBit = 0; - uint64_t nextData = 0; - int32_t result = 0; + uint32_t nonZeroBit = 0; + uint64_t nextData = 0; + int32_t result = 0; - if (bitStrm->bitData) - { - _BitScanReverse((DWORD *)&nonZeroBit, (DWORD)bitStrm->bitData); - result = 31 - nonZeroBit; - bitStrm->bitData <<= 32 - nonZeroBit; - bitStrm->bitsLeft -= 32 - nonZeroBit; - } - else - { - uint32_t bitsLeft = bitStrm->bitsLeft; - while (1) - { - while (bitStrm->curPos + 4 <= bitStrm->curBufSize) - { - nextData = - _byteswap_ulong(*(uint32_t *)(bitStrm->mdatBuf + bitStrm->curPos)); - bitStrm->curPos += 4; - crxFillBuffer(bitStrm); - if (nextData) - { - _BitScanReverse((DWORD *)&nonZeroBit, (DWORD)nextData); - result = bitsLeft + 31 - nonZeroBit; - bitStrm->bitData = nextData << (32 - nonZeroBit); - bitStrm->bitsLeft = nonZeroBit; - return result; + if (bitStrm->bitData) { + _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)bitStrm->bitData); + result = 31 - nonZeroBit; + bitStrm->bitData <<= 32 - nonZeroBit; + bitStrm->bitsLeft -= 32 - nonZeroBit; + } else { + uint32_t bitsLeft = bitStrm->bitsLeft; + + while (true) { + while (bitStrm->curPos + 4 <= bitStrm->curBufSize) { + nextData = + _byteswap_ulong(*(uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); + bitStrm->curPos += 4; + crxFillBuffer(bitStrm); + + if (nextData) { + _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)nextData); + result = bitsLeft + 31 - nonZeroBit; + bitStrm->bitData = nextData << (32 - nonZeroBit); + bitStrm->bitsLeft = nonZeroBit; + return result; + } + + bitsLeft += 32; + } + + if (bitStrm->curBufSize < bitStrm->curPos + 1) { + break; // error + } + + nextData = bitStrm->mdatBuf[bitStrm->curPos++]; + crxFillBuffer(bitStrm); + + if (nextData) { + break; + } + + bitsLeft += 8; } - bitsLeft += 32; - } - if (bitStrm->curBufSize < bitStrm->curPos + 1) - break; // error - nextData = bitStrm->mdatBuf[bitStrm->curPos++]; - crxFillBuffer(bitStrm); - if (nextData) - break; - bitsLeft += 8; + + _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)nextData); + result = (uint32_t)(bitsLeft + 7 - nonZeroBit); + bitStrm->bitData = nextData << (32 - nonZeroBit); + bitStrm->bitsLeft = nonZeroBit; } - _BitScanReverse((DWORD *)&nonZeroBit, (DWORD)nextData); - result = (uint32_t)(bitsLeft + 7 - nonZeroBit); - bitStrm->bitData = nextData << (32 - nonZeroBit); - bitStrm->bitsLeft = nonZeroBit; - } - return result; + + return result; } -libraw_inline uint32_t crxBitstreamGetBits(CrxBitstream *bitStrm, int bits) +libraw_inline uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) { - int bitsLeft = bitStrm->bitsLeft; - uint32_t bitData = bitStrm->bitData; - uint32_t nextWord; - uint8_t nextByte; - uint32_t result; + int bitsLeft = bitStrm->bitsLeft; + uint32_t bitData = bitStrm->bitData; + uint32_t nextWord; + uint8_t nextByte; + uint32_t result; - if (bitsLeft < bits) - { - // get them from stream - if (bitStrm->curPos + 4 <= bitStrm->curBufSize) - { - nextWord = - _byteswap_ulong(*(uint32_t *)(bitStrm->mdatBuf + bitStrm->curPos)); - bitStrm->curPos += 4; - crxFillBuffer(bitStrm); - bitStrm->bitsLeft = 32 - (bits - bitsLeft); - result = ((nextWord >> bitsLeft) | bitData) >> (32 - bits); - bitStrm->bitData = nextWord << (bits - bitsLeft); - return result; + if (bitsLeft < bits) { + // get them from stream + if (bitStrm->curPos + 4 <= bitStrm->curBufSize) { + nextWord = + _byteswap_ulong(*(uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); + bitStrm->curPos += 4; + crxFillBuffer(bitStrm); + bitStrm->bitsLeft = 32 - (bits - bitsLeft); + result = ((nextWord >> bitsLeft) | bitData) >> (32 - bits); + bitStrm->bitData = nextWord << (bits - bitsLeft); + return result; + } + + // less than a word left - read byte at a time + do { + if (bitStrm->curPos >= bitStrm->curBufSize) { + break; // error + } + + bitsLeft += 8; + nextByte = bitStrm->mdatBuf[bitStrm->curPos++]; + crxFillBuffer(bitStrm); + bitData |= nextByte << (32 - bitsLeft); + } while (bitsLeft < bits); } - // less than a word left - read byte at a time - do - { - if (bitStrm->curPos >= bitStrm->curBufSize) - break; // error - bitsLeft += 8; - nextByte = bitStrm->mdatBuf[bitStrm->curPos++]; - crxFillBuffer(bitStrm); - bitData |= nextByte << (32 - bitsLeft); - } while (bitsLeft < bits); - } - result = bitData >> (32 - bits); // 32-bits - bitStrm->bitData = bitData << bits; - bitStrm->bitsLeft = bitsLeft - bits; - return result; + + result = bitData >> (32 - bits);// 32-bits + bitStrm->bitData = bitData << bits; + bitStrm->bitsLeft = bitsLeft - bits; + return result; } libraw_inline int crxPredictKParameter(int32_t prevK, int32_t bitCode, int32_t maxVal = 0) { - int32_t newKParam = prevK - (bitCode < (1 << prevK >> 1)) + - ((bitCode >> prevK) > 2) + ((bitCode >> prevK) > 5); + int32_t newKParam = prevK - (bitCode < (1 << prevK >> 1)) + + ((bitCode >> prevK) > 2) + ((bitCode >> prevK) > 5); - return !maxVal || newKParam < maxVal ? newKParam : maxVal; + return !maxVal || newKParam < maxVal ? newKParam : maxVal; } -libraw_inline void crxDecodeSymbolL1(CrxBandParam *param, +libraw_inline void crxDecodeSymbolL1(CrxBandParam* param, int32_t doMedianPrediction, int32_t notEOL = 0) { - if (doMedianPrediction) - { - int32_t symb[4]; + if (doMedianPrediction) { + int32_t symb[4]; - int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; - symb[2] = param->lineBuf1[0]; - symb[0] = symb[1] = delta + symb[2]; - symb[3] = param->lineBuf0[1]; + int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; + symb[2] = param->lineBuf1[0]; + symb[0] = symb[1] = delta + symb[2]; + symb[3] = param->lineBuf0[1]; - param->lineBuf1[1] = - symb[(((param->lineBuf0[0] < param->lineBuf1[0]) ^ (delta < 0)) << 1) + - ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (delta < 0))]; - } - else - param->lineBuf1[1] = param->lineBuf0[1]; - - // get next error symbol - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - - // add converted (+/-) error code to predicted value - param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); - - // for not end of the line - use one symbol ahead to estimate next K - if (notEOL) - { - int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; - bitCode = (bitCode + _abs(nextDelta)) >> 1; - ++param->lineBuf0; - } - - // update K parameter - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - - ++param->lineBuf1; -} - -int crxDecodeLine(CrxBandParam *param) -{ - int length = param->subbandWidth; - - param->lineBuf1[0] = param->lineBuf0[1]; - for (; length > 1; --length) - { - if (param->lineBuf1[0] != param->lineBuf0[1] || - param->lineBuf1[0] != param->lineBuf0[2]) - { - crxDecodeSymbolL1(param, 1, 1); + param->lineBuf1[1] = + symb[(((param->lineBuf0[0] < param->lineBuf1[0]) ^ (delta < 0)) << 1) + + ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (delta < 0))]; + } else { + param->lineBuf1[1] = param->lineBuf0[1]; } - else - { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms = 1; - while (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms += JS[param->sParam]; - if (nSyms > length) - { - nSyms = length; - break; - } - if (param->sParam < 31) - ++param->sParam; - if (nSyms == length) - break; - } - if (nSyms < length) - { - if (J[param->sParam]) - nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); - if (param->sParam > 0) - --param->sParam; - if (nSyms > length) - return -1; - } + // get next error symbol + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - length -= nSyms; - - // copy symbol nSyms times - param->lineBuf0 += nSyms; - - // copy symbol nSyms times - while (nSyms-- > 0) - { - param->lineBuf1[1] = param->lineBuf1[0]; - ++param->lineBuf1; - } - } - - if (length > 0) - crxDecodeSymbolL1(param, 0, (length > 1)); - } - } - - if (length == 1) - crxDecodeSymbolL1(param, 1, 0); - - param->lineBuf1[1] = param->lineBuf1[0] + 1; - - return 0; -} - -libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam *param, - int32_t doSym = 1, - int32_t doCode = 1) -{ - int32_t sym = param->lineBuf0[1]; - - if (doSym) - { - // calculate the next symbol gradient - int32_t symb[4]; - int32_t deltaH = param->lineBuf0[1] - param->lineBuf0[0]; - symb[2] = param->lineBuf1[0]; - symb[0] = symb[1] = deltaH + symb[2]; - symb[3] = param->lineBuf0[1]; - sym = - symb[(((param->lineBuf0[0] < param->lineBuf1[0]) ^ (deltaH < 0)) << 1) + - ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (deltaH < 0))]; - } - - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - int32_t code = -(bitCode & 1) ^ (bitCode >> 1); - param->lineBuf1[1] = param->roundedBitsMask * 2 * code + static_cast(code < 0) + sym; - - if (doCode) - { - if (param->lineBuf0[2] > param->lineBuf0[1]) - code = (param->lineBuf0[2] - param->lineBuf0[1] + param->roundedBitsMask - - 1) >> - param->roundedBits; - else - code = -( - (param->lineBuf0[1] - param->lineBuf0[2] + param->roundedBitsMask) >> - param->roundedBits); - - param->kParam = crxPredictKParameter(param->kParam, - (bitCode + 2 * _abs(code)) >> 1, 15); - } - else - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - - ++param->lineBuf1; -} - -int crxDecodeLineRounded(CrxBandParam *param) -{ - int32_t valueReached = 0; - - param->lineBuf0[0] = param->lineBuf0[1]; - param->lineBuf1[0] = param->lineBuf0[1]; - int32_t length = param->subbandWidth; - - for (; length > 1; --length) - { - if (_abs(param->lineBuf0[2] - param->lineBuf0[1]) > param->roundedBitsMask) - { - crxDecodeSymbolL1Rounded(param); - ++param->lineBuf0; - valueReached = 1; - } - else if (valueReached || _abs(param->lineBuf0[0] - param->lineBuf1[0]) > - param->roundedBitsMask) - { - crxDecodeSymbolL1Rounded(param); - ++param->lineBuf0; - valueReached = 0; - } - else - { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms = 1; - while (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms += JS[param->sParam]; - if (nSyms > length) - { - nSyms = length; - break; - } - if (param->sParam < 31) - ++param->sParam; - if (nSyms == length) - break; - } - if (nSyms < length) - { - if (J[param->sParam]) - nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); - if (param->sParam > 0) - --param->sParam; - } - if (nSyms > length) - return -1; - } - length -= nSyms; - - // copy symbol nSyms times - param->lineBuf0 += nSyms; - - // copy symbol nSyms times - while (nSyms-- > 0) - { - param->lineBuf1[1] = param->lineBuf1[0]; - ++param->lineBuf1; - } - - if (length > 1) - { - crxDecodeSymbolL1Rounded(param, 0); - ++param->lineBuf0; - valueReached = _abs(param->lineBuf0[1] - param->lineBuf0[0]) > - param->roundedBitsMask; - } - else if (length == 1) - crxDecodeSymbolL1Rounded(param, 0, 0); - } - } - if (length == 1) - crxDecodeSymbolL1Rounded(param, 1, 0); - - param->lineBuf1[1] = param->lineBuf1[0] + 1; - - return 0; -} - -int crxDecodeLineNoRefPrevLine(CrxBandParam *param) -{ - int32_t i = 0; - - for (; i < param->subbandWidth - 1; i++) - { - if (param->lineBuf0[i + 2] | param->lineBuf0[i + 1] | param->lineBuf1[i]) - { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) + if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) + } else if (param->kParam) { bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); - param->lineBuf1[i + 1] = -(bitCode & 1) ^ (bitCode >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode); - if (param->lineBuf2[i + 1] - param->kParam <= 1) - { - if (param->kParam >= 15) - param->kParam = 15; - } - else - ++param->kParam; } - else - { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms = 1; - if (i != param->subbandWidth - 1) - { - while (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms += JS[param->sParam]; - if (i + nSyms > param->subbandWidth) - { - nSyms = param->subbandWidth - i; - break; + + // add converted (+/-) error code to predicted value + param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); + + // for not end of the line - use one symbol ahead to estimate next K + if (notEOL) { + int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; + bitCode = (bitCode + _abs(nextDelta)) >> 1; + ++param->lineBuf0; + } + + // update K parameter + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + + ++param->lineBuf1; +} + +int crxDecodeLine(CrxBandParam* param) +{ + int length = param->subbandWidth; + + param->lineBuf1[0] = param->lineBuf0[1]; + + for (; length > 1; --length) { + if (param->lineBuf1[0] != param->lineBuf0[1] || + param->lineBuf1[0] != param->lineBuf0[2]) { + crxDecodeSymbolL1(param, 1, 1); + } else { + int nSyms = 0; + + if (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms = 1; + + while (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms += JS[param->sParam]; + + if (nSyms > length) { + nSyms = length; + break; + } + + if (param->sParam < 31) { + ++param->sParam; + } + + if (nSyms == length) { + break; + } + } + + if (nSyms < length) { + if (J[param->sParam]) { + nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); + } + + if (param->sParam > 0) { + --param->sParam; + } + + if (nSyms > length) { + return -1; + } + } + + length -= nSyms; + + // copy symbol nSyms times + param->lineBuf0 += nSyms; + + // copy symbol nSyms times + while (nSyms-- > 0) { + param->lineBuf1[1] = param->lineBuf1[0]; + ++param->lineBuf1; + } + } + + if (length > 0) { + crxDecodeSymbolL1(param, 0, (length > 1)); } - if (param->sParam < 31) - ++param->sParam; - if (i + nSyms == param->subbandWidth) - break; - } - if (i + nSyms < param->subbandWidth) - { - if (J[param->sParam]) - nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); - if (param->sParam > 0) - --param->sParam; - } - if (i + nSyms > param->subbandWidth) - return -1; } - } - else if (i > param->subbandWidth) - return -1; + } - if (nSyms > 0) - { - memset(param->lineBuf1 + i + 1, 0, nSyms * sizeof(int32_t)); - memset(param->lineBuf2 + i, 0, nSyms * sizeof(int32_t)); - i += nSyms; - } + if (length == 1) { + crxDecodeSymbolL1(param, 1, 0); + } - if (i >= param->subbandWidth - 1) - { - if (i == param->subbandWidth - 1) - { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) + param->lineBuf1[1] = param->lineBuf1[0] + 1; + + return 0; +} + +libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, + int32_t doSym = 1, + int32_t doCode = 1) +{ + int32_t sym = param->lineBuf0[1]; + + if (doSym) { + // calculate the next symbol gradient + int32_t symb[4]; + int32_t deltaH = param->lineBuf0[1] - param->lineBuf0[0]; + symb[2] = param->lineBuf1[0]; + symb[0] = symb[1] = deltaH + symb[2]; + symb[3] = param->lineBuf0[1]; + sym = + symb[(((param->lineBuf0[0] < param->lineBuf1[0]) ^ (deltaH < 0)) << 1) + + ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (deltaH < 0))]; + } + + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + int32_t code = -(bitCode & 1) ^ (bitCode >> 1); + param->lineBuf1[1] = param->roundedBitsMask * 2 * code + static_cast(code < 0) + sym; + + if (doCode) { + if (param->lineBuf0[2] > param->lineBuf0[1]) { + code = (param->lineBuf0[2] - param->lineBuf0[1] + param->roundedBitsMask - + 1) >> + param->roundedBits; + } else { + code = -( + (param->lineBuf0[1] - param->lineBuf0[2] + param->roundedBitsMask) >> + param->roundedBits); + } + + param->kParam = crxPredictKParameter(param->kParam, + (bitCode + 2 * _abs(code)) >> 1, 15); + } else { + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + } + + ++param->lineBuf1; +} + +int crxDecodeLineRounded(CrxBandParam* param) +{ + int32_t valueReached = 0; + + param->lineBuf0[0] = param->lineBuf0[1]; + param->lineBuf1[0] = param->lineBuf0[1]; + int32_t length = param->subbandWidth; + + for (; length > 1; --length) { + if (_abs(param->lineBuf0[2] - param->lineBuf0[1]) > param->roundedBitsMask) { + crxDecodeSymbolL1Rounded(param); + ++param->lineBuf0; + valueReached = 1; + } else if (valueReached || _abs(param->lineBuf0[0] - param->lineBuf1[0]) > + param->roundedBitsMask) { + crxDecodeSymbolL1Rounded(param); + ++param->lineBuf0; + valueReached = 0; + } else { + int nSyms = 0; + + if (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms = 1; + + while (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms += JS[param->sParam]; + + if (nSyms > length) { + nSyms = length; + break; + } + + if (param->sParam < 31) { + ++param->sParam; + } + + if (nSyms == length) { + break; + } + } + + if (nSyms < length) { + if (J[param->sParam]) { + nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); + } + + if (param->sParam > 0) { + --param->sParam; + } + } + + if (nSyms > length) { + return -1; + } + } + + length -= nSyms; + + // copy symbol nSyms times + param->lineBuf0 += nSyms; + + // copy symbol nSyms times + while (nSyms-- > 0) { + param->lineBuf1[1] = param->lineBuf1[0]; + ++param->lineBuf1; + } + + if (length > 1) { + crxDecodeSymbolL1Rounded(param, 0); + ++param->lineBuf0; + valueReached = _abs(param->lineBuf0[1] - param->lineBuf0[0]) > + param->roundedBitsMask; + } else if (length == 1) { + crxDecodeSymbolL1Rounded(param, 0, 0); + } + } + } + + if (length == 1) { + crxDecodeSymbolL1Rounded(param, 1, 0); + } + + param->lineBuf1[1] = param->lineBuf1[0] + 1; + + return 0; +} + +int crxDecodeLineNoRefPrevLine(CrxBandParam* param) +{ + int32_t i = 0; + + for (; i < param->subbandWidth - 1; i++) { + if (param->lineBuf0[i + 2] | param->lineBuf0[i + 1] | param->lineBuf1[i]) { + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[i + 1] = -(bitCode & 1) ^ (bitCode >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode); + + if (param->lineBuf2[i + 1] - param->kParam <= 1) { + if (param->kParam >= 15) { + param->kParam = 15; + } + } else { + ++param->kParam; + } + } else { + int nSyms = 0; + + if (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms = 1; + + if (i != param->subbandWidth - 1) { + while (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms += JS[param->sParam]; + + if (i + nSyms > param->subbandWidth) { + nSyms = param->subbandWidth - i; + break; + } + + if (param->sParam < 31) { + ++param->sParam; + } + + if (i + nSyms == param->subbandWidth) { + break; + } + } + + if (i + nSyms < param->subbandWidth) { + if (J[param->sParam]) { + nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); + } + + if (param->sParam > 0) { + --param->sParam; + } + } + + if (i + nSyms > param->subbandWidth) { + return -1; + } + } + } else if (i > param->subbandWidth) { + return -1; + } + + if (nSyms > 0) { + memset(param->lineBuf1 + i + 1, 0, nSyms * sizeof(int32_t)); + memset(param->lineBuf2 + i, 0, nSyms * sizeof(int32_t)); + i += nSyms; + } + + if (i >= param->subbandWidth - 1) { + if (i == param->subbandWidth - 1) { + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[i + 1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + param->lineBuf2[i] = param->kParam; + } + + continue; + } else { + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[i + 1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode); + + if (param->lineBuf2[i + 1] - param->kParam <= 1) { + if (param->kParam >= 15) { + param->kParam = 15; + } + } else { + ++param->kParam; + } + } + } + + param->lineBuf2[i] = param->kParam; + } + + if (i == param->subbandWidth - 1) { + int32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) + } else if (param->kParam) { bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); - param->lineBuf1[i + 1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - param->lineBuf2[i] = param->kParam; } - continue; - } - else - { + + param->lineBuf1[i + 1] = -(bitCode & 1) ^ (bitCode >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + param->lineBuf2[i] = param->kParam; + } + + return 0; +} + +int crxDecodeTopLine(CrxBandParam* param) +{ + param->lineBuf1[0] = 0; + + int32_t length = param->subbandWidth; + + // read the line from bitstream + for (; length > 1; --length) { + if (param->lineBuf1[0]) { + param->lineBuf1[1] = param->lineBuf1[0]; + } else { + int nSyms = 0; + + if (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms = 1; + + while (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms += JS[param->sParam]; + + if (nSyms > length) { + nSyms = length; + break; + } + + if (param->sParam < 31) { + ++param->sParam; + } + + if (nSyms == length) { + break; + } + } + + if (nSyms < length) { + if (J[param->sParam]) { + nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); + } + + if (param->sParam > 0) { + --param->sParam; + } + + if (nSyms > length) { + return -1; + } + } + + length -= nSyms; + + // copy symbol nSyms times + while (nSyms-- > 0) { + param->lineBuf1[1] = param->lineBuf1[0]; + ++param->lineBuf1; + } + + if (length <= 0) { + break; + } + } + + param->lineBuf1[1] = 0; + } + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[i + 1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode); - if (param->lineBuf2[i + 1] - param->kParam <= 1) - { - if (param->kParam >= 15) - param->kParam = 15; - } - else - ++param->kParam; - } - } - param->lineBuf2[i] = param->kParam; - } - if (i == param->subbandWidth - 1) - { - int32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[i + 1] = -(bitCode & 1) ^ (bitCode >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - param->lineBuf2[i] = param->kParam; - } - return 0; -} - -int crxDecodeTopLine(CrxBandParam *param) -{ - param->lineBuf1[0] = 0; - - int32_t length = param->subbandWidth; - - // read the line from bitstream - for (; length > 1; --length) - { - if (param->lineBuf1[0]) - param->lineBuf1[1] = param->lineBuf1[0]; - else - { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms = 1; - while (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms += JS[param->sParam]; - if (nSyms > length) - { - nSyms = length; - break; - } - if (param->sParam < 31) - ++param->sParam; - if (nSyms == length) - break; - } - if (nSyms < length) - { - if (J[param->sParam]) - nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); - if (param->sParam > 0) - --param->sParam; - if (nSyms > length) - return -1; + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); } - length -= nSyms; - - // copy symbol nSyms times - while (nSyms-- > 0) - { - param->lineBuf1[1] = param->lineBuf1[0]; - ++param->lineBuf1; - } - - if (length <= 0) - break; - } - - param->lineBuf1[1] = 0; + param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + ++param->lineBuf1; } - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - ++param->lineBuf1; - } - - if (length == 1) - { - param->lineBuf1[1] = param->lineBuf1[0]; - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - ++param->lineBuf1; - } - - param->lineBuf1[1] = param->lineBuf1[0] + 1; - - return 0; -} - -int crxDecodeTopLineRounded(CrxBandParam *param) -{ - param->lineBuf1[0] = 0; - - int32_t length = param->subbandWidth; - - // read the line from bitstream - for (; length > 1; --length) - { - if (_abs(param->lineBuf1[0]) > param->roundedBitsMask) - param->lineBuf1[1] = param->lineBuf1[0]; - else - { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms = 1; - while (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms += JS[param->sParam]; - if (nSyms > length) - { - nSyms = length; - break; - } - if (param->sParam < 31) - ++param->sParam; - if (nSyms == length) - break; - } - if (nSyms < length) - { - if (J[param->sParam]) - nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); - if (param->sParam > 0) - --param->sParam; - if (nSyms > length) - return -1; - } - } - - length -= nSyms; - - // copy symbol nSyms times - while (nSyms-- > 0) - { + if (length == 1) { param->lineBuf1[1] = param->lineBuf1[0]; + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); ++param->lineBuf1; - } - - if (length <= 0) - break; - - param->lineBuf1[1] = 0; } - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + param->lineBuf1[1] = param->lineBuf1[0] + 1; - int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); - param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - ++param->lineBuf1; - } - - if (length == 1) - { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); - param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - ++param->lineBuf1; - } - - param->lineBuf1[1] = param->lineBuf1[0] + 1; - - return 0; + return 0; } -int crxDecodeTopLineNoRefPrevLine(CrxBandParam *param) +int crxDecodeTopLineRounded(CrxBandParam* param) { - param->lineBuf0[0] = 0; - param->lineBuf1[0] = 0; - int32_t length = param->subbandWidth; - for (; length > 1; --length) - { - if (param->lineBuf1[0]) - { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[1] = -(bitCode & 1) ^ (bitCode >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - } - else - { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms = 1; - while (crxBitstreamGetBits(¶m->bitStream, 1)) - { - nSyms += JS[param->sParam]; - if (nSyms > length) - { - nSyms = length; - break; - } - if (param->sParam < 31) - ++param->sParam; - if (nSyms == length) - break; + param->lineBuf1[0] = 0; + + int32_t length = param->subbandWidth; + + // read the line from bitstream + for (; length > 1; --length) { + if (_abs(param->lineBuf1[0]) > param->roundedBitsMask) { + param->lineBuf1[1] = param->lineBuf1[0]; + } else { + int nSyms = 0; + + if (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms = 1; + + while (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms += JS[param->sParam]; + + if (nSyms > length) { + nSyms = length; + break; + } + + if (param->sParam < 31) { + ++param->sParam; + } + + if (nSyms == length) { + break; + } + } + + if (nSyms < length) { + if (J[param->sParam]) { + nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); + } + + if (param->sParam > 0) { + --param->sParam; + } + + if (nSyms > length) { + return -1; + } + } + } + + length -= nSyms; + + // copy symbol nSyms times + while (nSyms-- > 0) { + param->lineBuf1[1] = param->lineBuf1[0]; + ++param->lineBuf1; + } + + if (length <= 0) { + break; + } + + param->lineBuf1[1] = 0; } - if (nSyms < length) - { - if (J[param->sParam]) - nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); - if (param->sParam > 0) - --param->sParam; - if (nSyms > length) + + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); + param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + ++param->lineBuf1; + } + + if (length == 1) { + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); + param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + ++param->lineBuf1; + } + + param->lineBuf1[1] = param->lineBuf1[0] + 1; + + return 0; +} + +int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) +{ + param->lineBuf0[0] = 0; + param->lineBuf1[0] = 0; + int32_t length = param->subbandWidth; + + for (; length > 1; --length) { + if (param->lineBuf1[0]) { + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[1] = -(bitCode & 1) ^ (bitCode >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + } else { + int nSyms = 0; + + if (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms = 1; + + while (crxBitstreamGetBits(¶m->bitStream, 1)) { + nSyms += JS[param->sParam]; + + if (nSyms > length) { + nSyms = length; + break; + } + + if (param->sParam < 31) { + ++param->sParam; + } + + if (nSyms == length) { + break; + } + } + + if (nSyms < length) { + if (J[param->sParam]) { + nSyms += crxBitstreamGetBits(¶m->bitStream, J[param->sParam]); + } + + if (param->sParam > 0) { + --param->sParam; + } + + if (nSyms > length) { + return -1; + } + } + } + + length -= nSyms; + + // copy symbol nSyms times + while (nSyms-- > 0) { + param->lineBuf2[0] = 0; + param->lineBuf1[1] = 0; + ++param->lineBuf1; + ++param->lineBuf2; + } + + if (length <= 0) { + break; + } + + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + } + + param->lineBuf2[0] = param->kParam; + ++param->lineBuf2; + ++param->lineBuf1; + } + + if (length == 1) { + uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + + if (bitCode >= 41) { + bitCode = crxBitstreamGetBits(¶m->bitStream, 21); + } else if (param->kParam) { + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | + (bitCode << param->kParam); + } + + param->lineBuf1[1] = -(bitCode & 1) ^ (bitCode >> 1); + param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); + param->lineBuf2[0] = param->kParam; + ++param->lineBuf1; + } + + param->lineBuf1[1] = 0; + + return 0; +} + +int crxDecodeLine(CrxBandParam* param, uint8_t* bandBuf) +{ + if (!param || !bandBuf) { + return -1; + } + + if (param->curLine >= param->subbandHeight) { + return -1; + } + + if (param->curLine == 0) { + int32_t lineLength = param->subbandWidth + 2; + + param->sParam = 0; + param->kParam = 0; + + if (param->supportsPartial) { + if (param->roundedBitsMask <= 0) { + param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf1 = param->lineBuf0 + lineLength; + int32_t* lineBuf = param->lineBuf1 + 1; + + if (crxDecodeTopLine(param)) { + return -1; + } + + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + ++param->curLine; + } else { + param->roundedBits = 1; + + if (param->roundedBitsMask & ~1) { + while (param->roundedBitsMask >> param->roundedBits) { + ++param->roundedBits; + } + } + + param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf1 = param->lineBuf0 + lineLength; + int32_t* lineBuf = param->lineBuf1 + 1; + + if (crxDecodeTopLineRounded(param)) { + return -1; + } + + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + ++param->curLine; + } + } else { + param->lineBuf2 = (int32_t*)param->nonProgrData; + param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf1 = param->lineBuf0 + lineLength; + int32_t* lineBuf = param->lineBuf1 + 1; + + if (crxDecodeTopLineNoRefPrevLine(param)) { + return -1; + } + + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + ++param->curLine; + } + } else if (!param->supportsPartial) { + int32_t lineLength = param->subbandWidth + 2; + param->lineBuf2 = (int32_t*)param->nonProgrData; + + if (param->curLine & 1) { + param->lineBuf1 = (int32_t*)param->paramData; + param->lineBuf0 = param->lineBuf1 + lineLength; + } else { + param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf1 = param->lineBuf0 + lineLength; + } + + int32_t* lineBuf = param->lineBuf1 + 1; + + if (crxDecodeLineNoRefPrevLine(param)) { return -1; } - } - length -= nSyms; - - // copy symbol nSyms times - while (nSyms-- > 0) - { - param->lineBuf2[0] = 0; - param->lineBuf1[1] = 0; - ++param->lineBuf1; - ++param->lineBuf2; - } - - if (length <= 0) - break; - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - } - param->lineBuf2[0] = param->kParam; - ++param->lineBuf2; - ++param->lineBuf1; - } - - if (length == 1) - { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); - if (bitCode >= 41) - bitCode = crxBitstreamGetBits(¶m->bitStream, 21); - else if (param->kParam) - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); - param->lineBuf1[1] = -(bitCode & 1) ^ (bitCode >> 1); - param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - param->lineBuf2[0] = param->kParam; - ++param->lineBuf1; - } - - param->lineBuf1[1] = 0; - - return 0; -} - -int crxDecodeLine(CrxBandParam *param, uint8_t *bandBuf) -{ - if (!param || !bandBuf) - return -1; - if (param->curLine >= param->subbandHeight) - return -1; - - if (param->curLine == 0) - { - int32_t lineLength = param->subbandWidth + 2; - - param->sParam = 0; - param->kParam = 0; - if (param->supportsPartial) - { - if (param->roundedBitsMask <= 0) - { - param->lineBuf0 = (int32_t *)param->paramData; - param->lineBuf1 = param->lineBuf0 + lineLength; - int32_t *lineBuf = param->lineBuf1 + 1; - if (crxDecodeTopLine(param)) - return -1; memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); ++param->curLine; - } - else - { - param->roundedBits = 1; - if (param->roundedBitsMask & ~1) - { - while (param->roundedBitsMask >> param->roundedBits) - ++param->roundedBits; + } else if (param->roundedBitsMask <= 0) { + int32_t lineLength = param->subbandWidth + 2; + + if (param->curLine & 1) { + param->lineBuf1 = (int32_t*)param->paramData; + param->lineBuf0 = param->lineBuf1 + lineLength; + } else { + param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf1 = param->lineBuf0 + lineLength; } - param->lineBuf0 = (int32_t *)param->paramData; - param->lineBuf1 = param->lineBuf0 + lineLength; - int32_t *lineBuf = param->lineBuf1 + 1; - if (crxDecodeTopLineRounded(param)) - return -1; + + int32_t* lineBuf = param->lineBuf1 + 1; + + if (crxDecodeLine(param)) { + return -1; + } + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); ++param->curLine; - } - } - else - { - param->lineBuf2 = (int32_t *)param->nonProgrData; - param->lineBuf0 = (int32_t *)param->paramData; - param->lineBuf1 = param->lineBuf0 + lineLength; - int32_t *lineBuf = param->lineBuf1 + 1; - if (crxDecodeTopLineNoRefPrevLine(param)) - return -1; - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); - ++param->curLine; - } - } - else if (!param->supportsPartial) - { - int32_t lineLength = param->subbandWidth + 2; - param->lineBuf2 = (int32_t *)param->nonProgrData; - if (param->curLine & 1) - { - param->lineBuf1 = (int32_t *)param->paramData; - param->lineBuf0 = param->lineBuf1 + lineLength; - } - else - { - param->lineBuf0 = (int32_t *)param->paramData; - param->lineBuf1 = param->lineBuf0 + lineLength; - } - int32_t *lineBuf = param->lineBuf1 + 1; - if (crxDecodeLineNoRefPrevLine(param)) - return -1; - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); - ++param->curLine; - } - else if (param->roundedBitsMask <= 0) - { - int32_t lineLength = param->subbandWidth + 2; - if (param->curLine & 1) - { - param->lineBuf1 = (int32_t *)param->paramData; - param->lineBuf0 = param->lineBuf1 + lineLength; - } - else - { - param->lineBuf0 = (int32_t *)param->paramData; - param->lineBuf1 = param->lineBuf0 + lineLength; - } - int32_t *lineBuf = param->lineBuf1 + 1; - if (crxDecodeLine(param)) - return -1; - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); - ++param->curLine; - } - else - { - int32_t lineLength = param->subbandWidth + 2; - if (param->curLine & 1) - { - param->lineBuf1 = (int32_t *)param->paramData; - param->lineBuf0 = param->lineBuf1 + lineLength; - } - else - { - param->lineBuf0 = (int32_t *)param->paramData; - param->lineBuf1 = param->lineBuf0 + lineLength; - } - int32_t *lineBuf = param->lineBuf1 + 1; - if (crxDecodeLineRounded(param)) - return -1; - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); - ++param->curLine; - } - return 0; -} + } else { + int32_t lineLength = param->subbandWidth + 2; -int crxDecodeLineWithIQuantization(CrxSubband *subband) -{ - int32_t q_step_tbl[6] = {0x28, 0x2D, 0x33, 0x39, 0x40, 0x48}; - - if (!subband->dataSize) - { - memset(subband->bandBuf, 0, subband->bandSize); - return 0; - } - - if (subband->supportsPartial) - { - uint32_t bitCode = crxBitstreamGetZeros(&subband->bandParam->bitStream); - if (bitCode >= 23) - bitCode = crxBitstreamGetBits(&subband->bandParam->bitStream, 8); - else if (subband->paramK) - bitCode = - crxBitstreamGetBits(&subband->bandParam->bitStream, subband->paramK) | - (bitCode << subband->paramK); - - subband->quantValue += - -(bitCode & 1) ^ (bitCode >> 1); // converting encoded to signed integer - subband->paramK = crxPredictKParameter(subband->paramK, bitCode); - if (subband->paramK > 7) - return -1; - } - if (crxDecodeLine(subband->bandParam, subband->bandBuf)) - return -1; - - if (subband->width <= 0) - return 0LL; - - // update subband buffers - int32_t *bandBuf = (int32_t *)subband->bandBuf; - int32_t qScale = - q_step_tbl[subband->quantValue % 6] >> (6 - subband->quantValue / 6); - if (subband->quantValue / 6 >= 6) - qScale = q_step_tbl[subband->quantValue % 6] * - (1 << (subband->quantValue / 6 + 26)); - - if (qScale != 1) - for (int32_t i = 0; i < subband->width; i++) - bandBuf[i] *= qScale; - - return 0; -} - -void crxHorizontal53(int32_t *lineBufLA, int32_t *lineBufLB, - CrxWaveletTransform *wavelet, uint32_t tileFlag) -{ - int32_t *band0Buf = wavelet->subband0Buf; - int32_t *band1Buf = wavelet->subband1Buf; - int32_t *band2Buf = wavelet->subband2Buf; - int32_t *band3Buf = wavelet->subband3Buf; - - if (wavelet->width <= 1) - { - lineBufLA[0] = band0Buf[0]; - lineBufLB[0] = band2Buf[0]; - } - else - { - if (tileFlag & E_HAS_TILES_ON_THE_LEFT) - { - lineBufLA[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufLB[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - ++band1Buf; - ++band3Buf; - } - else - { - lineBufLA[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - lineBufLB[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - } - ++band0Buf; - ++band2Buf; - - for (int i = 0; i < wavelet->width - 3; i += 2) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufLA[1] = band1Buf[0] + ((delta + lineBufLA[0]) >> 1); - lineBufLA[2] = delta; - - delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - lineBufLB[1] = band3Buf[0] + ((delta + lineBufLB[0]) >> 1); - lineBufLB[2] = delta; - - ++band0Buf; - ++band1Buf; - ++band2Buf; - ++band3Buf; - lineBufLA += 2; - lineBufLB += 2; - } - if (tileFlag & E_HAS_TILES_ON_THE_RIGHT) - { - int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufLA[1] = band1Buf[0] + ((deltaA + lineBufLA[0]) >> 1); - - int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - lineBufLB[1] = band3Buf[0] + ((deltaB + lineBufLB[0]) >> 1); - - if (wavelet->width & 1) - { - lineBufLA[2] = deltaA; - lineBufLB[2] = deltaB; - } - } - else if (wavelet->width & 1) - { - lineBufLA[1] = - band1Buf[0] + - ((lineBufLA[0] + band0Buf[0] - ((band1Buf[0] + 1) >> 1)) >> 1); - lineBufLA[2] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - - lineBufLB[1] = - band3Buf[0] + - ((lineBufLB[0] + band2Buf[0] - ((band3Buf[0] + 1) >> 1)) >> 1); - lineBufLB[2] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - } - else - { - lineBufLA[1] = lineBufLA[0] + band1Buf[0]; - lineBufLB[1] = lineBufLB[0] + band3Buf[0]; - } - } -} - -int32_t *crxIdwt53FilterGetLine(CrxPlaneComp *comp, int32_t level) -{ - int32_t *result = comp->waveletTransform[level] - .lineBuf[(comp->waveletTransform[level].fltTapH - - comp->waveletTransform[level].curH + 5) % - 5 + - 3]; - comp->waveletTransform[level].curH--; - return result; -} - -int crxIdwt53FilterDecode(CrxPlaneComp *comp, int32_t level) -{ - if (comp->waveletTransform[level].curH) - return 0; - - CrxSubband *sband = comp->subBands + 3 * level; - - if (comp->waveletTransform[level].height - 3 <= - comp->waveletTransform[level].curLine && - !(comp->tileFlag & E_HAS_TILES_ON_THE_BOTTOM)) - { - if (comp->waveletTransform[level].height & 1) - { - if (level) - { - if (crxIdwt53FilterDecode(comp, level - 1)) - return -1; - } - else if (crxDecodeLineWithIQuantization(sband)) - return -1; - - if (crxDecodeLineWithIQuantization(sband + 1)) - return -1; - } - } - else - { - if (level) - { - if (crxIdwt53FilterDecode(comp, level - 1)) - return -1; - } - else if (crxDecodeLineWithIQuantization(sband)) // LL band - return -1; - - if (crxDecodeLineWithIQuantization(sband + 1) || // HL band - crxDecodeLineWithIQuantization(sband + 2) || // LH band - crxDecodeLineWithIQuantization(sband + 3)) // HH band - return -1; - } - - return 0; -} - -int crxIdwt53FilterTransform(CrxPlaneComp *comp, uint32_t level) -{ - CrxWaveletTransform *wavelet = comp->waveletTransform + level; - - if (wavelet->curH) - return 0; - - if (wavelet->curLine >= wavelet->height - 3) - { - if (!(comp->tileFlag & E_HAS_TILES_ON_THE_BOTTOM)) - { - if (wavelet->height & 1) - { - if (level) - { - if (!wavelet[-1].curH) - if (crxIdwt53FilterTransform(comp, level - 1)) - return -1; - wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); + if (param->curLine & 1) { + param->lineBuf1 = (int32_t*)param->paramData; + param->lineBuf0 = param->lineBuf1 + lineLength; + } else { + param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf1 = param->lineBuf0 + lineLength; } - int32_t *band0Buf = wavelet->subband0Buf; - int32_t *band1Buf = wavelet->subband1Buf; - int32_t *lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - int32_t *lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - int32_t *lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; - int32_t *lineBufL0 = wavelet->lineBuf[0]; - int32_t *lineBufL1 = wavelet->lineBuf[1]; + int32_t* lineBuf = param->lineBuf1 + 1; + + if (crxDecodeLineRounded(param)) { + return -1; + } + + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + ++param->curLine; + } + + return 0; +} + +int crxDecodeLineWithIQuantization(CrxSubband* subband) +{ + int32_t q_step_tbl[6] = {0x28, 0x2D, 0x33, 0x39, 0x40, 0x48}; + + if (!subband->dataSize) { + memset(subband->bandBuf, 0, subband->bandSize); + return 0; + } + + if (subband->supportsPartial) { + uint32_t bitCode = crxBitstreamGetZeros(&subband->bandParam->bitStream); + + if (bitCode >= 23) { + bitCode = crxBitstreamGetBits(&subband->bandParam->bitStream, 8); + } else if (subband->paramK) { + bitCode = + crxBitstreamGetBits(&subband->bandParam->bitStream, subband->paramK) | + (bitCode << subband->paramK); + } + + subband->quantValue += + -(bitCode & 1) ^ (bitCode >> 1);// converting encoded to signed integer + subband->paramK = crxPredictKParameter(subband->paramK, bitCode); + + if (subband->paramK > 7) { + return -1; + } + } + + if (crxDecodeLine(subband->bandParam, subband->bandBuf)) { + return -1; + } + + if (subband->width <= 0) { + return 0LL; + } + + // update subband buffers + int32_t* bandBuf = (int32_t*)subband->bandBuf; + int32_t qScale = + q_step_tbl[subband->quantValue % 6] >> (6 - subband->quantValue / 6); + + if (subband->quantValue / 6 >= 6) { + qScale = q_step_tbl[subband->quantValue % 6] * + (1 << (subband->quantValue / 6 + 26)); + } + + if (qScale != 1) { + for (int32_t i = 0; i < subband->width; i++) { + bandBuf[i] *= qScale; + } + } + + return 0; +} + +void crxHorizontal53(int32_t* lineBufLA, int32_t* lineBufLB, + CrxWaveletTransform* wavelet, uint32_t tileFlag) +{ + int32_t* band0Buf = wavelet->subband0Buf; + int32_t* band1Buf = wavelet->subband1Buf; + int32_t* band2Buf = wavelet->subband2Buf; + int32_t* band3Buf = wavelet->subband3Buf; + + if (wavelet->width <= 1) { + lineBufLA[0] = band0Buf[0]; + lineBufLB[0] = band2Buf[0]; + } else { + if (tileFlag & E_HAS_TILES_ON_THE_LEFT) { + lineBufLA[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufLB[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + ++band1Buf; + ++band3Buf; + } else { + lineBufLA[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + lineBufLB[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + } + + ++band0Buf; + ++band2Buf; + + for (int i = 0; i < wavelet->width - 3; i += 2) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufLA[1] = band1Buf[0] + ((delta + lineBufLA[0]) >> 1); + lineBufLA[2] = delta; + + delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + lineBufLB[1] = band3Buf[0] + ((delta + lineBufLB[0]) >> 1); + lineBufLB[2] = delta; + + ++band0Buf; + ++band1Buf; + ++band2Buf; + ++band3Buf; + lineBufLA += 2; + lineBufLB += 2; + } + + if (tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufLA[1] = band1Buf[0] + ((deltaA + lineBufLA[0]) >> 1); + + int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + lineBufLB[1] = band3Buf[0] + ((deltaB + lineBufLB[0]) >> 1); + + if (wavelet->width & 1) { + lineBufLA[2] = deltaA; + lineBufLB[2] = deltaB; + } + } else if (wavelet->width & 1) { + lineBufLA[1] = + band1Buf[0] + + ((lineBufLA[0] + band0Buf[0] - ((band1Buf[0] + 1) >> 1)) >> 1); + lineBufLA[2] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + + lineBufLB[1] = + band3Buf[0] + + ((lineBufLB[0] + band2Buf[0] - ((band3Buf[0] + 1) >> 1)) >> 1); + lineBufLB[2] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + } else { + lineBufLA[1] = lineBufLA[0] + band1Buf[0]; + lineBufLB[1] = lineBufLB[0] + band3Buf[0]; + } + } +} + +int32_t* crxIdwt53FilterGetLine(CrxPlaneComp* comp, int32_t level) +{ + int32_t* result = comp->waveletTransform[level] + .lineBuf[(comp->waveletTransform[level].fltTapH - + comp->waveletTransform[level].curH + 5) % + 5 + + 3]; + comp->waveletTransform[level].curH--; + return result; +} + +int crxIdwt53FilterDecode(CrxPlaneComp* comp, int32_t level) +{ + if (comp->waveletTransform[level].curH) { + return 0; + } + + CrxSubband* sband = comp->subBands + 3 * level; + + if (comp->waveletTransform[level].height - 3 <= + comp->waveletTransform[level].curLine && + !(comp->tileFlag & E_HAS_TILES_ON_THE_BOTTOM)) { + if (comp->waveletTransform[level].height & 1) { + if (level) { + if (crxIdwt53FilterDecode(comp, level - 1)) { + return -1; + } + } else if (crxDecodeLineWithIQuantization(sband)) { + return -1; + } + + if (crxDecodeLineWithIQuantization(sband + 1)) { + return -1; + } + } + } else { + if (level) { + if (crxIdwt53FilterDecode(comp, level - 1)) { + return -1; + } + } else if (crxDecodeLineWithIQuantization(sband)) {// LL band + return -1; + } + + if (crxDecodeLineWithIQuantization(sband + 1) ||// HL band + crxDecodeLineWithIQuantization(sband + 2) ||// LH band + crxDecodeLineWithIQuantization(sband + 3)) {// HH band + return -1; + } + } + + return 0; +} + +int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) +{ + CrxWaveletTransform* wavelet = comp->waveletTransform + level; + + if (wavelet->curH) { + return 0; + } + + if (wavelet->curLine >= wavelet->height - 3) { + if (!(comp->tileFlag & E_HAS_TILES_ON_THE_BOTTOM)) { + if (wavelet->height & 1) { + if (level) { + if (!wavelet[-1].curH) { + if (crxIdwt53FilterTransform(comp, level - 1)) { + return -1; + } + } + + wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); + } + + int32_t* band0Buf = wavelet->subband0Buf; + int32_t* band1Buf = wavelet->subband1Buf; + int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; + + int32_t* lineBufL0 = wavelet->lineBuf[0]; + int32_t* lineBufL1 = wavelet->lineBuf[1]; + wavelet->lineBuf[1] = wavelet->lineBuf[2]; + wavelet->lineBuf[2] = lineBufL1; + + // process L bands + if (wavelet->width <= 1) { + lineBufL0[0] = band0Buf[0]; + } else { + if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { + lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + ++band1Buf; + } else { + lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + } + + ++band0Buf; + + for (int i = 0; i < wavelet->width - 3; i += 2) { + int32_t delta = + band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); + lineBufL0[2] = delta; + ++band0Buf; + ++band1Buf; + lineBufL0 += 2; + } + + if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + int32_t delta = + band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); + + if (wavelet->width & 1) { + lineBufL0[2] = delta; + } + } else if (wavelet->width & 1) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); + lineBufL0[2] = delta; + } else { + lineBufL0[1] = band1Buf[0] + lineBufL0[0]; + } + } + + // process H bands + lineBufL0 = wavelet->lineBuf[0]; + lineBufL1 = wavelet->lineBuf[1]; + + for (int32_t i = 0; i < wavelet->width; i++) { + int32_t delta = lineBufL0[i] - ((lineBufL1[i] + 1) >> 1); + lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); + lineBufH2[i] = delta; + } + + wavelet->curH += 3; + wavelet->curLine += 3; + wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; + } else { + int32_t* lineBufL2 = wavelet->lineBuf[2]; + int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + wavelet->lineBuf[1] = lineBufL2; + wavelet->lineBuf[2] = wavelet->lineBuf[1]; + + for (int32_t i = 0; i < wavelet->width; i++) { + lineBufH1[i] = lineBufH0[i] + lineBufL2[i]; + } + + wavelet->curH += 2; + wavelet->curLine += 2; + wavelet->fltTapH = (wavelet->fltTapH + 2) % 5; + } + } + } else { + if (level) { + if (!wavelet[-1].curH && crxIdwt53FilterTransform(comp, level - 1)) { + return -1; + } + + wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); + } + + int32_t* band0Buf = wavelet->subband0Buf; + int32_t* band1Buf = wavelet->subband1Buf; + int32_t* band2Buf = wavelet->subband2Buf; + int32_t* band3Buf = wavelet->subband3Buf; + + int32_t* lineBufL0 = wavelet->lineBuf[0]; + int32_t* lineBufL1 = wavelet->lineBuf[1]; + int32_t* lineBufL2 = wavelet->lineBuf[2]; + int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; + wavelet->lineBuf[1] = wavelet->lineBuf[2]; wavelet->lineBuf[2] = lineBufL1; // process L bands - if (wavelet->width <= 1) - { - lineBufL0[0] = band0Buf[0]; - } - else - { - if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) - { - lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - ++band1Buf; - } - else - { - lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - } - ++band0Buf; - for (int i = 0; i < wavelet->width - 3; i += 2) - { - int32_t delta = - band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); - lineBufL0[2] = delta; + if (wavelet->width <= 1) { + lineBufL0[0] = band0Buf[0]; + lineBufL1[0] = band2Buf[0]; + } else { + if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { + lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufL1[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + ++band1Buf; + ++band3Buf; + } else { + lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + lineBufL1[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + } + ++band0Buf; - ++band1Buf; - lineBufL0 += 2; - } - if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) - { - int32_t delta = - band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); - if (wavelet->width & 1) - lineBufL0[2] = delta; - } - else if (wavelet->width & 1) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); - lineBufL0[2] = delta; - } - else - lineBufL0[1] = band1Buf[0] + lineBufL0[0]; + ++band2Buf; + + for (int i = 0; i < wavelet->width - 3; i += 2) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); + lineBufL0[2] = delta; + + delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + lineBufL1[1] = band3Buf[0] + ((delta + lineBufL1[0]) >> 1); + lineBufL1[2] = delta; + + ++band0Buf; + ++band1Buf; + ++band2Buf; + ++band3Buf; + lineBufL0 += 2; + lineBufL1 += 2; + } + + if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufL0[1] = band1Buf[0] + ((deltaA + lineBufL0[0]) >> 1); + + int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + lineBufL1[1] = band3Buf[0] + ((deltaB + lineBufL1[0]) >> 1); + + if (wavelet->width & 1) { + lineBufL0[2] = deltaA; + lineBufL1[2] = deltaB; + } + } else if (wavelet->width & 1) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); + lineBufL0[2] = delta; + + delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + lineBufL1[1] = band3Buf[0] + ((delta + lineBufL1[0]) >> 1); + lineBufL1[2] = delta; + } else { + lineBufL0[1] = lineBufL0[0] + band1Buf[0]; + lineBufL1[1] = lineBufL1[0] + band3Buf[0]; + } } // process H bands lineBufL0 = wavelet->lineBuf[0]; lineBufL1 = wavelet->lineBuf[1]; - for (int32_t i = 0; i < wavelet->width; i++) - { - int32_t delta = lineBufL0[i] - ((lineBufL1[i] + 1) >> 1); - lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); - lineBufH2[i] = delta; + lineBufL2 = wavelet->lineBuf[2]; + + for (int32_t i = 0; i < wavelet->width; i++) { + int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); + lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); + lineBufH2[i] = delta; } - wavelet->curH += 3; - wavelet->curLine += 3; - wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; - } - else - { - int32_t *lineBufL2 = wavelet->lineBuf[2]; - int32_t *lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - int32_t *lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - wavelet->lineBuf[1] = lineBufL2; - wavelet->lineBuf[2] = wavelet->lineBuf[1]; - for (int32_t i = 0; i < wavelet->width; i++) - lineBufH1[i] = lineBufH0[i] + lineBufL2[i]; - - wavelet->curH += 2; - wavelet->curLine += 2; - wavelet->fltTapH = (wavelet->fltTapH + 2) % 5; - } - } - } - else - { - if (level) - { - if (!wavelet[-1].curH && crxIdwt53FilterTransform(comp, level - 1)) - return -1; - wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); - } - - int32_t *band0Buf = wavelet->subband0Buf; - int32_t *band1Buf = wavelet->subband1Buf; - int32_t *band2Buf = wavelet->subband2Buf; - int32_t *band3Buf = wavelet->subband3Buf; - - int32_t *lineBufL0 = wavelet->lineBuf[0]; - int32_t *lineBufL1 = wavelet->lineBuf[1]; - int32_t *lineBufL2 = wavelet->lineBuf[2]; - int32_t *lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - int32_t *lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - int32_t *lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; - - wavelet->lineBuf[1] = wavelet->lineBuf[2]; - wavelet->lineBuf[2] = lineBufL1; - - // process L bands - if (wavelet->width <= 1) - { - lineBufL0[0] = band0Buf[0]; - lineBufL1[0] = band2Buf[0]; - } - else - { - if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) - { - lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufL1[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - ++band1Buf; - ++band3Buf; - } - else - { - lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - lineBufL1[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - } - ++band0Buf; - ++band2Buf; - for (int i = 0; i < wavelet->width - 3; i += 2) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); - lineBufL0[2] = delta; - - delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - lineBufL1[1] = band3Buf[0] + ((delta + lineBufL1[0]) >> 1); - lineBufL1[2] = delta; - - ++band0Buf; - ++band1Buf; - ++band2Buf; - ++band3Buf; - lineBufL0 += 2; - lineBufL1 += 2; - } - if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) - { - int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufL0[1] = band1Buf[0] + ((deltaA + lineBufL0[0]) >> 1); - - int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - lineBufL1[1] = band3Buf[0] + ((deltaB + lineBufL1[0]) >> 1); - - if (wavelet->width & 1) - { - lineBufL0[2] = deltaA; - lineBufL1[2] = deltaB; + if (wavelet->curLine >= wavelet->height - 3 && (wavelet->height & 1)) { + wavelet->curH += 3; + wavelet->curLine += 3; + wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; + } else { + wavelet->curH += 2; + wavelet->curLine += 2; + wavelet->fltTapH = (wavelet->fltTapH + 2) % 5; } - } - else if (wavelet->width & 1) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); - lineBufL0[2] = delta; - - delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - lineBufL1[1] = band3Buf[0] + ((delta + lineBufL1[0]) >> 1); - lineBufL1[2] = delta; - } - else - { - lineBufL0[1] = lineBufL0[0] + band1Buf[0]; - lineBufL1[1] = lineBufL1[0] + band3Buf[0]; - } } - // process H bands - lineBufL0 = wavelet->lineBuf[0]; - lineBufL1 = wavelet->lineBuf[1]; - lineBufL2 = wavelet->lineBuf[2]; - for (int32_t i = 0; i < wavelet->width; i++) - { - int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); - lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); - lineBufH2[i] = delta; - } - if (wavelet->curLine >= wavelet->height - 3 && (wavelet->height & 1)) - { - wavelet->curH += 3; - wavelet->curLine += 3; - wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; - } - else - { - wavelet->curH += 2; - wavelet->curLine += 2; - wavelet->fltTapH = (wavelet->fltTapH + 2) % 5; - } - } - - return 0; -} - -int crxIdwt53FilterInitialize(CrxPlaneComp *comp, int32_t prevLevel) -{ - if (prevLevel < 0) return 0; - - for (int curLevel = 0, curBand = 0; curLevel < prevLevel + 1; - curLevel++, curBand += 3) - { - CrxWaveletTransform *wavelet = comp->waveletTransform + curLevel; - if (curLevel) - wavelet[0].subband0Buf = crxIdwt53FilterGetLine(comp, curLevel - 1); - else if (crxDecodeLineWithIQuantization(comp->subBands + curBand)) - return -1; - - int32_t *lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - if (wavelet->height > 1) - { - if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1) || - crxDecodeLineWithIQuantization(comp->subBands + curBand + 2) || - crxDecodeLineWithIQuantization(comp->subBands + curBand + 3)) - return -1; - - int32_t *lineBufL0 = wavelet->lineBuf[0]; - int32_t *lineBufL1 = wavelet->lineBuf[1]; - int32_t *lineBufL2 = wavelet->lineBuf[2]; - - if (comp->tileFlag & E_HAS_TILES_ON_THE_TOP) - { - crxHorizontal53(lineBufL0, wavelet->lineBuf[1], wavelet, - comp->tileFlag); - if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 3) || - crxDecodeLineWithIQuantization(comp->subBands + curBand + 2)) - return -1; - - int32_t *band2Buf = wavelet->subband2Buf; - int32_t *band3Buf = wavelet->subband3Buf; - - // process L band - if (wavelet->width <= 1) - lineBufL2[0] = band2Buf[0]; - else - { - if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) - { - lineBufL2[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - ++band3Buf; - } - else - lineBufL2[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - - ++band2Buf; - - for (int i = 0; i < wavelet->width - 3; i += 2) - { - int32_t delta = - band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); - lineBufL2[2] = delta; - - ++band2Buf; - ++band3Buf; - lineBufL2 += 2; - } - if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) - { - int32_t delta = - band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); - lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); - if (wavelet->width & 1) - lineBufL2[2] = delta; - } - else if (wavelet->width & 1) - { - int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - - lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); - lineBufL2[2] = delta; - } - else - { - lineBufL2[1] = band3Buf[0] + lineBufL2[0]; - } - } - - // process H band - for (int32_t i = 0; i < wavelet->width; i++) - lineBufH0[i] = - lineBufL0[i] - ((lineBufL1[i] + lineBufL2[i] + 2) >> 2); - } - else - { - crxHorizontal53(lineBufL0, wavelet->lineBuf[2], wavelet, - comp->tileFlag); - for (int i = 0; i < wavelet->width; i++) - lineBufH0[i] = lineBufL0[i] - ((lineBufL2[i] + 1) >> 1); - } - - if (crxIdwt53FilterDecode(comp, curLevel) || - crxIdwt53FilterTransform(comp, curLevel)) - return -1; - } - else - { - if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1)) - return -1; - - int32_t *band0Buf = wavelet->subband0Buf; - int32_t *band1Buf = wavelet->subband1Buf; - - // process H band - if (wavelet->width <= 1) - lineBufH0[0] = band0Buf[0]; - else - { - if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) - { - lineBufH0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - ++band1Buf; - } - else - lineBufH0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - - ++band0Buf; - - for (int i = 0; i < wavelet->width - 3; i += 2) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); - lineBufH0[2] = delta; - - ++band0Buf; - ++band1Buf; - lineBufH0 += 2; - } - - if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); - lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); - lineBufH0[2] = delta; - } - else if (wavelet->width & 1) - { - int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); - lineBufH0[2] = delta; - } - else - { - lineBufH0[1] = band1Buf[0] + lineBufH0[0]; - } - } - ++wavelet->curLine; - ++wavelet->curH; - wavelet->fltTapH = (wavelet->fltTapH + 1) % 5; - } - } - - return 0; } -void crxFreeSubbandData(CrxImage *image, CrxPlaneComp *comp) +int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) { - if (comp->compBuf) - { - free(comp->compBuf); - comp->compBuf = 0; - } - - if (!comp->subBands) - return; - - for (int32_t i = 0; i < image->subbandCount; i++) - { - if (comp->subBands[i].bandParam) - { - free(comp->subBands[i].bandParam); - comp->subBands[i].bandParam = 0LL; + if (prevLevel < 0) { + return 0; } - comp->subBands[i].bandBuf = 0; - comp->subBands[i].bandSize = 0; - } + + for (int curLevel = 0, curBand = 0; curLevel < prevLevel + 1; + curLevel++, curBand += 3) { + CrxWaveletTransform* wavelet = comp->waveletTransform + curLevel; + + if (curLevel) { + wavelet[0].subband0Buf = crxIdwt53FilterGetLine(comp, curLevel - 1); + } else if (crxDecodeLineWithIQuantization(comp->subBands + curBand)) { + return -1; + } + + int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + + if (wavelet->height > 1) { + if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1) || + crxDecodeLineWithIQuantization(comp->subBands + curBand + 2) || + crxDecodeLineWithIQuantization(comp->subBands + curBand + 3)) { + return -1; + } + + int32_t* lineBufL0 = wavelet->lineBuf[0]; + int32_t* lineBufL1 = wavelet->lineBuf[1]; + int32_t* lineBufL2 = wavelet->lineBuf[2]; + + if (comp->tileFlag & E_HAS_TILES_ON_THE_TOP) { + crxHorizontal53(lineBufL0, wavelet->lineBuf[1], wavelet, + comp->tileFlag); + + if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 3) || + crxDecodeLineWithIQuantization(comp->subBands + curBand + 2)) { + return -1; + } + + int32_t* band2Buf = wavelet->subband2Buf; + int32_t* band3Buf = wavelet->subband3Buf; + + // process L band + if (wavelet->width <= 1) { + lineBufL2[0] = band2Buf[0]; + } else { + if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { + lineBufL2[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + ++band3Buf; + } else { + lineBufL2[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + } + + ++band2Buf; + + for (int i = 0; i < wavelet->width - 3; i += 2) { + int32_t delta = + band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); + lineBufL2[2] = delta; + + ++band2Buf; + ++band3Buf; + lineBufL2 += 2; + } + + if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + int32_t delta = + band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); + + if (wavelet->width & 1) { + lineBufL2[2] = delta; + } + } else if (wavelet->width & 1) { + int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + + lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); + lineBufL2[2] = delta; + } else { + lineBufL2[1] = band3Buf[0] + lineBufL2[0]; + } + } + + // process H band + for (int32_t i = 0; i < wavelet->width; i++) { + lineBufH0[i] = + lineBufL0[i] - ((lineBufL1[i] + lineBufL2[i] + 2) >> 2); + } + } else { + crxHorizontal53(lineBufL0, wavelet->lineBuf[2], wavelet, + comp->tileFlag); + + for (int i = 0; i < wavelet->width; i++) { + lineBufH0[i] = lineBufL0[i] - ((lineBufL2[i] + 1) >> 1); + } + } + + if (crxIdwt53FilterDecode(comp, curLevel) || + crxIdwt53FilterTransform(comp, curLevel)) { + return -1; + } + } else { + if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1)) { + return -1; + } + + int32_t* band0Buf = wavelet->subband0Buf; + int32_t* band1Buf = wavelet->subband1Buf; + + // process H band + if (wavelet->width <= 1) { + lineBufH0[0] = band0Buf[0]; + } else { + if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { + lineBufH0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + ++band1Buf; + } else { + lineBufH0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + } + + ++band0Buf; + + for (int i = 0; i < wavelet->width - 3; i += 2) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); + lineBufH0[2] = delta; + + ++band0Buf; + ++band1Buf; + lineBufH0 += 2; + } + + if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); + lineBufH0[2] = delta; + } else if (wavelet->width & 1) { + int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); + lineBufH0[2] = delta; + } else { + lineBufH0[1] = band1Buf[0] + lineBufH0[0]; + } + } + + ++wavelet->curLine; + ++wavelet->curH; + wavelet->fltTapH = (wavelet->fltTapH + 1) % 5; + } + } + + return 0; } -void crxConvertPlaneLine(CrxImage *img, int imageRow, int imageCol = 0, - int plane = 0, int32_t *lineData = 0, +void crxFreeSubbandData(CrxImage* image, CrxPlaneComp* comp) +{ + if (comp->compBuf) { + free(comp->compBuf); + comp->compBuf = nullptr; + } + + if (!comp->subBands) { + return; + } + + for (int32_t i = 0; i < image->subbandCount; i++) { + if (comp->subBands[i].bandParam) { + free(comp->subBands[i].bandParam); + comp->subBands[i].bandParam = nullptr; + } + + comp->subBands[i].bandBuf = nullptr; + comp->subBands[i].bandSize = 0; + } +} + +void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, + int plane = 0, const int32_t* lineData = nullptr, int lineLength = 0) { - if (lineData) - { - uint64_t rawOffset = 4 * img->planeWidth * imageRow + 2 * imageCol; - if (img->encType == 1) - { - int32_t maxVal = 1 << (img->nBits - 1); - int32_t minVal = -maxVal; - --maxVal; - for (int i = 0; i < lineLength; i++) - img->outBufs[plane][rawOffset + 2 * i] = - _constrain(lineData[i], minVal, maxVal); - } - else if (img->encType == 3) - { - // copy to intermediate planeBuf - rawOffset = plane * img->planeWidth * img->planeHeight + - img->planeWidth * imageRow + imageCol; - for (int i = 0; i < lineLength; i++) - img->planeBuf[rawOffset + i] = lineData[i]; - } - else if (img->nPlanes == 4) - { - int32_t median = 1 << (img->nBits - 1); - int32_t maxVal = (1 << img->nBits) - 1; - for (int i = 0; i < lineLength; i++) - img->outBufs[plane][rawOffset + 2 * i] = - _constrain(median + lineData[i], 0, maxVal); - } - else if (img->nPlanes == 1) - { - int32_t maxVal = (1 << img->nBits) - 1; - int32_t median = 1 << (img->nBits - 1); - rawOffset = img->planeWidth * imageRow + imageCol; - for (int i = 0; i < lineLength; i++) - img->outBufs[0][rawOffset + i] = - _constrain(median + lineData[i], 0, maxVal); - } - } - else if (img->encType == 3 && img->planeBuf) - { - int32_t planeSize = img->planeWidth * img->planeHeight; - int16_t *plane0 = img->planeBuf + imageRow * img->planeWidth; - int16_t *plane1 = plane0 + planeSize; - int16_t *plane2 = plane1 + planeSize; - int16_t *plane3 = plane2 + planeSize; + if (lineData) { + uint64_t rawOffset = 4 * img->planeWidth * imageRow + 2 * imageCol; - int32_t median = 1 << (img->nBits - 1) << 10; - int32_t maxVal = (1 << img->nBits) - 1; - uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; + if (img->encType == 1) { + int32_t maxVal = 1 << (img->nBits - 1); + int32_t minVal = -maxVal; + --maxVal; - // for this stage - all except imageRow is ignored - for (int i = 0; i < img->planeWidth; i++) - { - int32_t gr = - median + (plane0[i] << 10) - 168 * plane1[i] - 585 * plane3[i]; - int32_t val = 0; - if (gr < 0) - gr = -(((_abs(gr) + 512) >> 9) & ~1); - else - gr = ((_abs(gr) + 512) >> 9) & ~1; + for (int i = 0; i < lineLength; i++) { + img->outBufs[plane][rawOffset + 2 * i] = + _constrain(lineData[i], minVal, maxVal); + } + } else if (img->encType == 3) { + // copy to intermediate planeBuf + rawOffset = plane * img->planeWidth * img->planeHeight + + img->planeWidth * imageRow + imageCol; - // Essentially R = round(median + P0 + 1.474*P3) - val = (median + (plane0[i] << 10) + 1510 * plane3[i] + 512) >> 10; - img->outBufs[0][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); - // Essentially G1 = round(median + P0 + P2 - 0.164*P1 - 0.571*P3) - val = (plane2[i] + gr + 1) >> 1; - img->outBufs[1][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); - // Essentially G1 = round(median + P0 - P2 - 0.164*P1 - 0.571*P3) - val = (gr - plane2[i] + 1) >> 1; - img->outBufs[2][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); - // Essentially B = round(median + P0 + 1.881*P1) - val = (median + (plane0[i] << 10) + 1927 * plane1[i] + 512) >> 10; - img->outBufs[3][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + for (int i = 0; i < lineLength; i++) { + img->planeBuf[rawOffset + i] = lineData[i]; + } + } else if (img->nPlanes == 4) { + int32_t median = 1 << (img->nBits - 1); + int32_t maxVal = (1 << img->nBits) - 1; + + for (int i = 0; i < lineLength; i++) { + img->outBufs[plane][rawOffset + 2 * i] = + _constrain(median + lineData[i], 0, maxVal); + } + } else if (img->nPlanes == 1) { + int32_t maxVal = (1 << img->nBits) - 1; + int32_t median = 1 << (img->nBits - 1); + rawOffset = img->planeWidth * imageRow + imageCol; + + for (int i = 0; i < lineLength; i++) { + img->outBufs[0][rawOffset + i] = + _constrain(median + lineData[i], 0, maxVal); + } + } + } else if (img->encType == 3 && img->planeBuf) { + int32_t planeSize = img->planeWidth * img->planeHeight; + int16_t* plane0 = img->planeBuf + imageRow * img->planeWidth; + int16_t* plane1 = plane0 + planeSize; + int16_t* plane2 = plane1 + planeSize; + int16_t* plane3 = plane2 + planeSize; + + int32_t median = 1 << (img->nBits - 1) << 10; + int32_t maxVal = (1 << img->nBits) - 1; + uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; + + // for this stage - all except imageRow is ignored + for (int i = 0; i < img->planeWidth; i++) { + int32_t gr = + median + (plane0[i] << 10) - 168 * plane1[i] - 585 * plane3[i]; + int32_t val = 0; + + if (gr < 0) { + gr = -(((_abs(gr) + 512) >> 9) & ~1); + } else { + gr = ((_abs(gr) + 512) >> 9) & ~1; + } + + // Essentially R = round(median + P0 + 1.474*P3) + val = (median + (plane0[i] << 10) + 1510 * plane3[i] + 512) >> 10; + img->outBufs[0][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + // Essentially G1 = round(median + P0 + P2 - 0.164*P1 - 0.571*P3) + val = (plane2[i] + gr + 1) >> 1; + img->outBufs[1][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + // Essentially G1 = round(median + P0 - P2 - 0.164*P1 - 0.571*P3) + val = (gr - plane2[i] + 1) >> 1; + img->outBufs[2][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + // Essentially B = round(median + P0 + 1.881*P1) + val = (median + (plane0[i] << 10) + 1927 * plane1[i] + 512) >> 10; + img->outBufs[3][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + } } - } } -int crxParamInit(CrxBandParam **param, uint64_t subbandMdatOffset, +int crxParamInit(CrxBandParam** param, uint64_t subbandMdatOffset, uint64_t subbandDataSize, uint32_t subbandWidth, uint32_t subbandHeight, int32_t supportsPartial, - uint32_t roundedBitsMask, LibRaw_abstract_datastream *input) + uint32_t roundedBitsMask, LibRaw_abstract_datastream* input) { - int32_t progrDataSize = supportsPartial ? 0 : sizeof(int32_t) * subbandWidth; - int32_t paramLength = 2 * subbandWidth + 4; - uint8_t *paramBuf = (uint8_t *)calloc( - 1, sizeof(CrxBandParam) + sizeof(int32_t) * paramLength + progrDataSize); + int32_t progrDataSize = supportsPartial ? 0 : sizeof(int32_t) * subbandWidth; + int32_t paramLength = 2 * subbandWidth + 4; + uint8_t* paramBuf = (uint8_t*)calloc( + 1, sizeof(CrxBandParam) + sizeof(int32_t) * paramLength + progrDataSize); - if (!paramBuf) - return -1; - - *param = (CrxBandParam *)paramBuf; - - paramBuf += sizeof(CrxBandParam); - - (*param)->paramData = (int32_t *)paramBuf; - (*param)->nonProgrData = - progrDataSize ? (*param)->paramData + paramLength : 0; - (*param)->subbandWidth = subbandWidth; - (*param)->subbandHeight = subbandHeight; - (*param)->roundedBits = 0; - (*param)->curLine = 0; - (*param)->roundedBitsMask = roundedBitsMask; - (*param)->supportsPartial = supportsPartial; - (*param)->bitStream.bitData = 0; - (*param)->bitStream.bitsLeft = 0; - (*param)->bitStream.mdatSize = subbandDataSize; - (*param)->bitStream.curPos = 0; - (*param)->bitStream.curBufSize = 0; - (*param)->bitStream.curBufOffset = subbandMdatOffset; - (*param)->bitStream.input = input; - - crxFillBuffer(&(*param)->bitStream); - - return 0; -} - -int crxSetupSubbandData(CrxImage *img, CrxPlaneComp *planeComp, - const CrxTile *tile, uint32_t mdatOffset) -{ - long compDataSize = 0; - long waveletDataOffset = 0; - long compCoeffDataOffset = 0; - int32_t toSubbands = 3 * img->levels + 1; - int32_t transformWidth = 0; - - CrxSubband *subbands = planeComp->subBands; - - // calculate sizes - for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) - { - subbands[subbandNum].bandSize = - subbands[subbandNum].width * sizeof(int32_t); // 4bytes - compDataSize += subbands[subbandNum].bandSize; - } - - if (img->levels) - { - int32_t encLevels = img->levels ? img->levels : 1; - waveletDataOffset = (compDataSize + 7) & ~7; - compDataSize = - (sizeof(CrxWaveletTransform) * encLevels + waveletDataOffset + 7) & ~7; - compCoeffDataOffset = compDataSize; - - // calc wavelet line buffer sizes (always at one level up from current) - for (int level = 0; level < img->levels; ++level) - if (level < img->levels - 1) - compDataSize += 8 * sizeof(int32_t) * - planeComp->subBands[3 * (level + 1) + 2].width; - else - compDataSize += 8 * sizeof(int32_t) * tile->width; - } - - // buffer allocation - planeComp->compBuf = (uint8_t *)malloc(compDataSize); - if (!planeComp->compBuf) - return -1; - - // subbands buffer and sizes initialisation - uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; - uint8_t *subbandBuf = planeComp->compBuf; - - for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) - { - subbands[subbandNum].bandBuf = subbandBuf; - subbandBuf += subbands[subbandNum].bandSize; - subbands[subbandNum].mdatOffset = - subbandMdatOffset + subbands[subbandNum].dataOffset; - } - - // wavelet data initialisation - if (img->levels) - { - CrxWaveletTransform *waveletTransforms = - (CrxWaveletTransform *)(planeComp->compBuf + waveletDataOffset); - int32_t *paramData = (int32_t *)(planeComp->compBuf + compCoeffDataOffset); - - planeComp->waveletTransform = waveletTransforms; - waveletTransforms[0].subband0Buf = (int32_t *)subbands->bandBuf; - - for (int level = 0; level < img->levels; ++level) - { - int32_t band = 3 * level + 1; - - if (level >= img->levels - 1) - { - waveletTransforms[level].height = tile->height; - transformWidth = tile->width; - } - else - { - waveletTransforms[level].height = subbands[band + 3].height; - transformWidth = subbands[band + 4].width; - } - waveletTransforms[level].width = transformWidth; - waveletTransforms[level].lineBuf[0] = paramData; - waveletTransforms[level].lineBuf[1] = - waveletTransforms[level].lineBuf[0] + transformWidth; - waveletTransforms[level].lineBuf[2] = - waveletTransforms[level].lineBuf[1] + transformWidth; - waveletTransforms[level].lineBuf[3] = - waveletTransforms[level].lineBuf[2] + transformWidth; - waveletTransforms[level].lineBuf[4] = - waveletTransforms[level].lineBuf[3] + transformWidth; - waveletTransforms[level].lineBuf[5] = - waveletTransforms[level].lineBuf[4] + transformWidth; - waveletTransforms[level].lineBuf[6] = - waveletTransforms[level].lineBuf[5] + transformWidth; - waveletTransforms[level].lineBuf[7] = - waveletTransforms[level].lineBuf[6] + transformWidth; - waveletTransforms[level].curLine = 0; - waveletTransforms[level].curH = 0; - waveletTransforms[level].fltTapH = 0; - waveletTransforms[level].subband1Buf = (int32_t *)subbands[band].bandBuf; - waveletTransforms[level].subband2Buf = - (int32_t *)subbands[band + 1].bandBuf; - waveletTransforms[level].subband3Buf = - (int32_t *)subbands[band + 2].bandBuf; - - paramData = waveletTransforms[level].lineBuf[7] + transformWidth; - } - } - - // decoding params and bitstream initialisation - for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) - { - if (subbands[subbandNum].dataSize) - { - int32_t supportsPartial = 0; - uint32_t roundedBitsMask = 0; - - if (planeComp->supportsPartial && subbandNum == 0) - { - roundedBitsMask = planeComp->roundedBitsMask; - supportsPartial = 1; - } - if (crxParamInit(&subbands[subbandNum].bandParam, - subbands[subbandNum].mdatOffset, - subbands[subbandNum].dataSize, - subbands[subbandNum].width, subbands[subbandNum].height, - supportsPartial, roundedBitsMask, img->input)) + if (!paramBuf) { return -1; } - } - return 0; -} + *param = (CrxBandParam*)paramBuf; -} // namespace + paramBuf += sizeof(CrxBandParam); + (*param)->paramData = (int32_t*)paramBuf; + (*param)->nonProgrData = + progrDataSize ? (*param)->paramData + paramLength : nullptr; + (*param)->subbandWidth = subbandWidth; + (*param)->subbandHeight = subbandHeight; + (*param)->roundedBits = 0; + (*param)->curLine = 0; + (*param)->roundedBitsMask = roundedBitsMask; + (*param)->supportsPartial = supportsPartial; + (*param)->bitStream.bitData = 0; + (*param)->bitStream.bitsLeft = 0; + (*param)->bitStream.mdatSize = subbandDataSize; + (*param)->bitStream.curPos = 0; + (*param)->bitStream.curBufSize = 0; + (*param)->bitStream.curBufOffset = subbandMdatOffset; + (*param)->bitStream.input = input; -int DCraw::crxDecodePlane(void *p, uint32_t planeNumber) -{ - CrxImage *img = (CrxImage *)p; - int imageRow = 0; - for (int tRow = 0; tRow < img->tileRows; tRow++) - { - int imageCol = 0; - for (int tCol = 0; tCol < img->tileCols; tCol++) - { - CrxTile *tile = img->tiles + tRow * img->tileRows + tCol; - CrxPlaneComp *planeComp = tile->comps + planeNumber; - uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; + crxFillBuffer(&(*param)->bitStream); - // decode single tile - if (crxSetupSubbandData(img, planeComp, tile, tileMdatOffset)) - return -1; - - if (img->levels) - { - if (crxIdwt53FilterInitialize(planeComp, img->levels - 1)) - return -1; - for (int i = 0; i < tile->height; ++i) - { - if (crxIdwt53FilterDecode(planeComp, img->levels - 1) || - crxIdwt53FilterTransform(planeComp, img->levels - 1)) - return -1; - int32_t *lineData = - crxIdwt53FilterGetLine(planeComp, img->levels - 1); - crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, - lineData, tile->width); - } - } - else - { - // we have the only subband in this case - if (!planeComp->subBands->dataSize) - { - memset(planeComp->subBands->bandBuf, 0, - planeComp->subBands->bandSize); - return 0; - } - - for (int i = 0; i < tile->height; ++i) - { - if (crxDecodeLine(planeComp->subBands->bandParam, - planeComp->subBands->bandBuf)) - return -1; - int32_t *lineData = (int32_t *)planeComp->subBands->bandBuf; - crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, - lineData, tile->width); - } - } - imageCol += tile->width; - } - imageRow += img->tiles[tRow * img->tileRows].height; - } - - return 0; -} - - -namespace { - -typedef DCraw::CanonCR3Data::crx_data_header_t crx_data_header_t; - - -int crxReadSubbandHeaders(crx_data_header_t *hdr, CrxImage *img, CrxTile *tile, - CrxPlaneComp *comp, uint8_t **subbandMdatPtr, - uint32_t *mdatSize) -{ - CrxSubband *band = comp->subBands + img->subbandCount - 1; // set to last band - uint32_t bandHeight = tile->height; - uint32_t bandWidth = tile->width; - int32_t bandWidthExCoef = 0; - int32_t bandHeightExCoef = 0; - if (img->levels) - { - // Build up subband sequences to crxDecode to a level in a header - - // Coefficient structure is a bit unclear and convoluted: - // 3 levels max - 8 groups (for tile width rounded to 8 bytes) - // of 3 band per level 4 sets of coefficients for each - int32_t *rowExCoef = - exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->width & 7); - int32_t *colExCoef = - exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->height & 7); - for (int level = 0; level < img->levels; ++level) - { - int32_t widthOddPixel = bandWidth & 1; - int32_t heightOddPixel = bandHeight & 1; - bandWidth = (widthOddPixel + bandWidth) >> 1; - bandHeight = (heightOddPixel + bandHeight) >> 1; - - int32_t bandWidthExCoef0 = 0; - int32_t bandWidthExCoef1 = 0; - int32_t bandHeightExCoef0 = 0; - int32_t bandHeightExCoef1 = 0; - if (tile->tileFlag & E_HAS_TILES_ON_THE_RIGHT) - { - bandWidthExCoef0 = rowExCoef[0]; - bandWidthExCoef1 = rowExCoef[1]; - } - if (tile->tileFlag & E_HAS_TILES_ON_THE_LEFT) - ++bandWidthExCoef0; - if (tile->tileFlag & E_HAS_TILES_ON_THE_BOTTOM) - { - bandHeightExCoef0 = colExCoef[0]; - bandHeightExCoef1 = colExCoef[1]; - } - if (tile->tileFlag & E_HAS_TILES_ON_THE_TOP) - ++bandHeightExCoef0; - - band[0].width = bandWidth + bandWidthExCoef0 - widthOddPixel; - band[0].height = bandHeight + bandHeightExCoef0 - heightOddPixel; - - band[-1].width = bandWidth + bandWidthExCoef1; - band[-1].height = bandHeight + bandHeightExCoef0 - heightOddPixel; - - band[-2].width = bandWidth + bandWidthExCoef0 - widthOddPixel; - band[-2].height = bandHeight + bandHeightExCoef1; - - rowExCoef += 4; - colExCoef += 4; - band -= 3; - } - bandWidthExCoef = bandHeightExCoef = 0; - if (tile->tileFlag & E_HAS_TILES_ON_THE_RIGHT) - bandWidthExCoef = - exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->width & 7) + - 4 * (img->levels - 1) + 1]; - if (tile->tileFlag & E_HAS_TILES_ON_THE_BOTTOM) - bandHeightExCoef = - exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->height & 7) + - 4 * (img->levels - 1) + 1]; - } - band->width = bandWidthExCoef + bandWidth; - band->height = bandHeightExCoef + bandHeight; - - if (!img->subbandCount) return 0; - int32_t subbandOffset = 0; - band = comp->subBands; - for (unsigned int curSubband = 0; curSubband < img->subbandCount; curSubband++, band++) - { - if (*mdatSize < 0xC) - return -1; - - if (sgetn(2, *subbandMdatPtr) != 0xFF03) - return -1; - - uint32_t bitData = sgetn(4, *subbandMdatPtr + 8); - uint32_t subbandSize = sgetn(4, *subbandMdatPtr + 4); - - if (curSubband != bitData >> 28) - { - band->dataSize = subbandSize; - return -1; - } - band->dataSize = subbandSize - (bitData & 0x7FF); - band->supportsPartial = bitData & 0x8000 ? 1 : 0; - band->dataOffset = subbandOffset; - band->quantValue = (bitData >> 19) & 0xFF; - band->paramK = 0; - band->bandParam = 0; - band->bandBuf = 0; - band->bandSize = 0; - - subbandOffset += subbandSize; - - *subbandMdatPtr += 0xC; - *mdatSize -= 0xC; - } - return 0; } -int crxReadImageHeaders(crx_data_header_t *hdr, CrxImage *img, uint8_t *mdatPtr, +int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, + const CrxTile* tile, uint32_t mdatOffset) +{ + long compDataSize = 0; + long waveletDataOffset = 0; + long compCoeffDataOffset = 0; + int32_t toSubbands = 3 * img->levels + 1; + int32_t transformWidth = 0; + + CrxSubband* subbands = planeComp->subBands; + + // calculate sizes + for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + subbands[subbandNum].bandSize = + subbands[subbandNum].width * sizeof(int32_t); // 4bytes + compDataSize += subbands[subbandNum].bandSize; + } + + if (img->levels) { + int32_t encLevels = img->levels ? img->levels : 1; + waveletDataOffset = (compDataSize + 7) & ~7; + compDataSize = + (sizeof(CrxWaveletTransform) * encLevels + waveletDataOffset + 7) & ~7; + compCoeffDataOffset = compDataSize; + + // calc wavelet line buffer sizes (always at one level up from current) + for (int level = 0; level < img->levels; ++level) { + if (level < img->levels - 1) { + compDataSize += 8 * sizeof(int32_t) * + planeComp->subBands[3 * (level + 1) + 2].width; + } else { + compDataSize += 8 * sizeof(int32_t) * tile->width; + } + } + } + + // buffer allocation + planeComp->compBuf = (uint8_t*)malloc(compDataSize); + + if (!planeComp->compBuf) { + return -1; + } + + // subbands buffer and sizes initialisation + uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; + uint8_t* subbandBuf = planeComp->compBuf; + + for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + subbands[subbandNum].bandBuf = subbandBuf; + subbandBuf += subbands[subbandNum].bandSize; + subbands[subbandNum].mdatOffset = + subbandMdatOffset + subbands[subbandNum].dataOffset; + } + + // wavelet data initialisation + if (img->levels) { + CrxWaveletTransform* waveletTransforms = + (CrxWaveletTransform*)(planeComp->compBuf + waveletDataOffset); + int32_t* paramData = (int32_t*)(planeComp->compBuf + compCoeffDataOffset); + + planeComp->waveletTransform = waveletTransforms; + waveletTransforms[0].subband0Buf = (int32_t*)subbands->bandBuf; + + for (int level = 0; level < img->levels; ++level) { + int32_t band = 3 * level + 1; + + if (level >= img->levels - 1) { + waveletTransforms[level].height = tile->height; + transformWidth = tile->width; + } else { + waveletTransforms[level].height = subbands[band + 3].height; + transformWidth = subbands[band + 4].width; + } + + waveletTransforms[level].width = transformWidth; + waveletTransforms[level].lineBuf[0] = paramData; + waveletTransforms[level].lineBuf[1] = + waveletTransforms[level].lineBuf[0] + transformWidth; + waveletTransforms[level].lineBuf[2] = + waveletTransforms[level].lineBuf[1] + transformWidth; + waveletTransforms[level].lineBuf[3] = + waveletTransforms[level].lineBuf[2] + transformWidth; + waveletTransforms[level].lineBuf[4] = + waveletTransforms[level].lineBuf[3] + transformWidth; + waveletTransforms[level].lineBuf[5] = + waveletTransforms[level].lineBuf[4] + transformWidth; + waveletTransforms[level].lineBuf[6] = + waveletTransforms[level].lineBuf[5] + transformWidth; + waveletTransforms[level].lineBuf[7] = + waveletTransforms[level].lineBuf[6] + transformWidth; + waveletTransforms[level].curLine = 0; + waveletTransforms[level].curH = 0; + waveletTransforms[level].fltTapH = 0; + waveletTransforms[level].subband1Buf = (int32_t*)subbands[band].bandBuf; + waveletTransforms[level].subband2Buf = + (int32_t*)subbands[band + 1].bandBuf; + waveletTransforms[level].subband3Buf = + (int32_t*)subbands[band + 2].bandBuf; + + paramData = waveletTransforms[level].lineBuf[7] + transformWidth; + } + } + + // decoding params and bitstream initialisation + for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + if (subbands[subbandNum].dataSize) { + int32_t supportsPartial = 0; + uint32_t roundedBitsMask = 0; + + if (planeComp->supportsPartial && subbandNum == 0) { + roundedBitsMask = planeComp->roundedBitsMask; + supportsPartial = 1; + } + + if (crxParamInit(&subbands[subbandNum].bandParam, + subbands[subbandNum].mdatOffset, + subbands[subbandNum].dataSize, + subbands[subbandNum].width, subbands[subbandNum].height, + supportsPartial, roundedBitsMask, img->input)) { + return -1; + } + } + } + + return 0; +} +} // namespace + +int DCraw::crxDecodePlane(void* p, uint32_t planeNumber) +{ + CrxImage* img = (CrxImage*)p; + int imageRow = 0; + + for (int tRow = 0; tRow < img->tileRows; tRow++) { + int imageCol = 0; + + for (int tCol = 0; tCol < img->tileCols; tCol++) { + CrxTile* tile = img->tiles + tRow * img->tileRows + tCol; + CrxPlaneComp* planeComp = tile->comps + planeNumber; + uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; + + // decode single tile + if (crxSetupSubbandData(img, planeComp, tile, tileMdatOffset)) { + return -1; + } + + if (img->levels) { + if (crxIdwt53FilterInitialize(planeComp, img->levels - 1)) { + return -1; + } + + for (int i = 0; i < tile->height; ++i) { + if (crxIdwt53FilterDecode(planeComp, img->levels - 1) || + crxIdwt53FilterTransform(planeComp, img->levels - 1)) { + return -1; + } + + int32_t* lineData = + crxIdwt53FilterGetLine(planeComp, img->levels - 1); + crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, + lineData, tile->width); + } + } else { + // we have the only subband in this case + if (!planeComp->subBands->dataSize) { + memset(planeComp->subBands->bandBuf, 0, + planeComp->subBands->bandSize); + return 0; + } + + for (int i = 0; i < tile->height; ++i) { + if (crxDecodeLine(planeComp->subBands->bandParam, + planeComp->subBands->bandBuf)) { + return -1; + } + + int32_t* lineData = (int32_t*)planeComp->subBands->bandBuf; + crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, + lineData, tile->width); + } + } + + imageCol += tile->width; + } + + imageRow += img->tiles[tRow * img->tileRows].height; + } + + return 0; +} + +namespace +{ +using crx_data_header_t = DCraw::CanonCR3Data::crx_data_header_t; + +int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, + CrxPlaneComp* comp, uint8_t** subbandMdatPtr, + uint32_t* mdatSize) +{ + CrxSubband* band = comp->subBands + img->subbandCount - 1; // set to last band + uint32_t bandHeight = tile->height; + uint32_t bandWidth = tile->width; + int32_t bandWidthExCoef = 0; + int32_t bandHeightExCoef = 0; + + if (img->levels) { + // Build up subband sequences to crxDecode to a level in a header + + // Coefficient structure is a bit unclear and convoluted: + // 3 levels max - 8 groups (for tile width rounded to 8 bytes) + // of 3 band per level 4 sets of coefficients for each + int32_t* rowExCoef = + exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->width & 7); + int32_t* colExCoef = + exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->height & 7); + + for (int level = 0; level < img->levels; ++level) { + int32_t widthOddPixel = bandWidth & 1; + int32_t heightOddPixel = bandHeight & 1; + bandWidth = (widthOddPixel + bandWidth) >> 1; + bandHeight = (heightOddPixel + bandHeight) >> 1; + + int32_t bandWidthExCoef0 = 0; + int32_t bandWidthExCoef1 = 0; + int32_t bandHeightExCoef0 = 0; + int32_t bandHeightExCoef1 = 0; + + if (tile->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + bandWidthExCoef0 = rowExCoef[0]; + bandWidthExCoef1 = rowExCoef[1]; + } + + if (tile->tileFlag & E_HAS_TILES_ON_THE_LEFT) { + ++bandWidthExCoef0; + } + + if (tile->tileFlag & E_HAS_TILES_ON_THE_BOTTOM) { + bandHeightExCoef0 = colExCoef[0]; + bandHeightExCoef1 = colExCoef[1]; + } + + if (tile->tileFlag & E_HAS_TILES_ON_THE_TOP) { + ++bandHeightExCoef0; + } + + band[0].width = bandWidth + bandWidthExCoef0 - widthOddPixel; + band[0].height = bandHeight + bandHeightExCoef0 - heightOddPixel; + + band[-1].width = bandWidth + bandWidthExCoef1; + band[-1].height = bandHeight + bandHeightExCoef0 - heightOddPixel; + + band[-2].width = bandWidth + bandWidthExCoef0 - widthOddPixel; + band[-2].height = bandHeight + bandHeightExCoef1; + + rowExCoef += 4; + colExCoef += 4; + band -= 3; + } + + bandWidthExCoef = bandHeightExCoef = 0; + + if (tile->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { + bandWidthExCoef = + exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->width & 7) + + 4 * (img->levels - 1) + 1]; + } + + if (tile->tileFlag & E_HAS_TILES_ON_THE_BOTTOM) { + bandHeightExCoef = + exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->height & 7) + + 4 * (img->levels - 1) + 1]; + } + } + + band->width = bandWidthExCoef + bandWidth; + band->height = bandHeightExCoef + bandHeight; + + if (!img->subbandCount) { + return 0; + } + + int32_t subbandOffset = 0; + band = comp->subBands; + + for (unsigned int curSubband = 0; curSubband < img->subbandCount; curSubband++, band++) { + if (*mdatSize < 0xC) { + return -1; + } + + if (sgetn(2, *subbandMdatPtr) != 0xFF03) { + return -1; + } + + uint32_t bitData = sgetn(4, *subbandMdatPtr + 8); + uint32_t subbandSize = sgetn(4, *subbandMdatPtr + 4); + + if (curSubband != bitData >> 28) { + band->dataSize = subbandSize; + return -1; + } + + band->dataSize = subbandSize - (bitData & 0x7FF); + band->supportsPartial = bitData & 0x8000 ? 1 : 0; + band->dataOffset = subbandOffset; + band->quantValue = (bitData >> 19) & 0xFF; + band->paramK = 0; + band->bandParam = nullptr; + band->bandBuf = nullptr; + band->bandSize = 0; + + subbandOffset += subbandSize; + + *subbandMdatPtr += 0xC; + *mdatSize -= 0xC; + } + + return 0; +} + +int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, uint8_t* mdatPtr, uint32_t mdatSize) { - unsigned int nTiles = img->tileRows * img->tileCols; + unsigned int nTiles = img->tileRows * img->tileCols; - if (!nTiles) - return -1; + if (!nTiles) { + return -1; + } - if (!img->tiles) - { - img->tiles = (CrxTile *)malloc( - sizeof(CrxTile) * nTiles + - sizeof(CrxPlaneComp) * nTiles * img->nPlanes + - sizeof(CrxSubband) * nTiles * img->nPlanes * img->subbandCount); - if (!img->tiles) - return -1; + if (!img->tiles) { + img->tiles = (CrxTile*)malloc( + sizeof(CrxTile) * nTiles + + sizeof(CrxPlaneComp) * nTiles * img->nPlanes + + sizeof(CrxSubband) * nTiles * img->nPlanes * img->subbandCount); - // memory areas in allocated chunk - CrxTile *tile = img->tiles; - CrxPlaneComp *comps = (CrxPlaneComp *)(tile + nTiles); - CrxSubband *bands = (CrxSubband *)(comps + img->nPlanes * nTiles); - - for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) - { - tile->tileFlag = 0; // tile neighbouring flags - tile->tileNumber = curTile; - tile->tileSize = 0; - tile->comps = comps + curTile * img->nPlanes; - - if ((curTile + 1) % img->tileCols) - { - // not the last tile in a tile row - tile->width = hdr->tileWidth; - if (img->tileCols > 1) - { - tile->tileFlag = E_HAS_TILES_ON_THE_RIGHT; - if (curTile % img->tileCols) - // not the first tile in tile row - tile->tileFlag |= E_HAS_TILES_ON_THE_LEFT; + if (!img->tiles) { + return -1; } - } - else - { - // last tile in a tile row - tile->width = img->planeWidth - hdr->tileWidth * (img->tileCols - 1); - if (img->tileCols > 1) - tile->tileFlag = E_HAS_TILES_ON_THE_LEFT; - } - if (curTile < nTiles - img->tileCols) - { - // in first tile row - tile->height = hdr->tileHeight; - if (img->tileRows > 1) - { - tile->tileFlag |= E_HAS_TILES_ON_THE_BOTTOM; - if (curTile >= img->tileCols) - tile->tileFlag |= E_HAS_TILES_ON_THE_TOP; - } - } - else - { - // non first tile row - tile->height = img->planeHeight - hdr->tileHeight * (img->tileRows - 1); - if (img->tileRows > 1) - tile->tileFlag |= E_HAS_TILES_ON_THE_TOP; - } - if (img->nPlanes) - { - CrxPlaneComp *comp = tile->comps; - CrxSubband *band = bands + curTile * img->nPlanes * img->subbandCount; - for (int curComp = 0; curComp < img->nPlanes; curComp++, comp++) - { - comp->compNumber = curComp; - comp->supportsPartial = 1; - comp->tileFlag = tile->tileFlag; - comp->subBands = band; - comp->compBuf = 0; - comp->waveletTransform = 0; - if (img->subbandCount) - { - for (int curBand = 0; curBand < img->subbandCount; - curBand++, band++) - { - band->supportsPartial = 0; - band->quantValue = 4; - band->bandParam = 0; - band->dataSize = 0; + // memory areas in allocated chunk + CrxTile* tile = img->tiles; + CrxPlaneComp* comps = (CrxPlaneComp*)(tile + nTiles); + CrxSubband* bands = (CrxSubband*)(comps + img->nPlanes * nTiles); + + for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { + tile->tileFlag = 0; // tile neighbouring flags + tile->tileNumber = curTile; + tile->tileSize = 0; + tile->comps = comps + curTile * img->nPlanes; + + if ((curTile + 1) % img->tileCols) { + // not the last tile in a tile row + tile->width = hdr->tileWidth; + + if (img->tileCols > 1) { + tile->tileFlag = E_HAS_TILES_ON_THE_RIGHT; + + if (curTile % img->tileCols) { + // not the first tile in tile row + tile->tileFlag |= E_HAS_TILES_ON_THE_LEFT; + } + } + } else { + // last tile in a tile row + tile->width = img->planeWidth - hdr->tileWidth * (img->tileCols - 1); + + if (img->tileCols > 1) { + tile->tileFlag = E_HAS_TILES_ON_THE_LEFT; + } + } + + if (curTile < nTiles - img->tileCols) { + // in first tile row + tile->height = hdr->tileHeight; + + if (img->tileRows > 1) { + tile->tileFlag |= E_HAS_TILES_ON_THE_BOTTOM; + + if (curTile >= img->tileCols) { + tile->tileFlag |= E_HAS_TILES_ON_THE_TOP; + } + } + } else { + // non first tile row + tile->height = img->planeHeight - hdr->tileHeight * (img->tileRows - 1); + + if (img->tileRows > 1) { + tile->tileFlag |= E_HAS_TILES_ON_THE_TOP; + } + } + + if (img->nPlanes) { + CrxPlaneComp* comp = tile->comps; + CrxSubband* band = bands + curTile * img->nPlanes * img->subbandCount; + + for (int curComp = 0; curComp < img->nPlanes; curComp++, comp++) { + comp->compNumber = curComp; + comp->supportsPartial = 1; + comp->tileFlag = tile->tileFlag; + comp->subBands = band; + comp->compBuf = nullptr; + comp->waveletTransform = nullptr; + + if (img->subbandCount) { + for (int curBand = 0; curBand < img->subbandCount; + curBand++, band++) { + band->supportsPartial = 0; + band->quantValue = 4; + band->bandParam = nullptr; + band->dataSize = 0; + } + } + } } - } } - } } - } - uint32_t tileOffset = 0; - uint32_t dataSize = mdatSize; - uint8_t *dataPtr = mdatPtr; - CrxTile *tile = img->tiles; + uint32_t tileOffset = 0; + uint32_t dataSize = mdatSize; + uint8_t* dataPtr = mdatPtr; + CrxTile* tile = img->tiles; - for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) - { - if (dataSize < 0xC) - return -1; + for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { + if (dataSize < 0xC) { + return -1; + } - if (sgetn(2, dataPtr) != 0xFF01) - return -1; - if (sgetn(2, dataPtr + 8) != curTile) - return -1; + if (sgetn(2, dataPtr) != 0xFF01) { + return -1; + } - dataSize -= 0xC; + if (sgetn(2, dataPtr + 8) != curTile) { + return -1; + } - tile->tileSize = sgetn(4, dataPtr + 4); - tile->dataOffset = tileOffset; + dataSize -= 0xC; - int32_t hdrExtraBytes = sgetn(2, dataPtr + 2) - 8; - tileOffset += tile->tileSize; - dataPtr += hdrExtraBytes + 0xC; - dataSize -= hdrExtraBytes; + tile->tileSize = sgetn(4, dataPtr + 4); + tile->dataOffset = tileOffset; - uint32_t compOffset = 0; - CrxPlaneComp *comp = tile->comps; + int32_t hdrExtraBytes = sgetn(2, dataPtr + 2) - 8; + tileOffset += tile->tileSize; + dataPtr += hdrExtraBytes + 0xC; + dataSize -= hdrExtraBytes; - for (int compNum = 0; compNum < img->nPlanes; compNum++, comp++) - { - if (dataSize < 0xC) - return -1; + uint32_t compOffset = 0; + CrxPlaneComp* comp = tile->comps; - if (sgetn(2, dataPtr) != 0xFF02) - return -1; - if (compNum != dataPtr[8] >> 4) - return -1; + for (int compNum = 0; compNum < img->nPlanes; compNum++, comp++) { + if (dataSize < 0xC) { + return -1; + } - comp->compSize = sgetn(4, dataPtr + 4); + if (sgetn(2, dataPtr) != 0xFF02) { + return -1; + } - int32_t compHdrRoundedBits = (dataPtr[8] >> 1) & 3; - comp->supportsPartial = (dataPtr[8] & 8) != 0; + if (compNum != dataPtr[8] >> 4) { + return -1; + } - comp->dataOffset = compOffset; - comp->tileFlag = tile->tileFlag; + comp->compSize = sgetn(4, dataPtr + 4); - compOffset += comp->compSize; - dataSize -= 0xC; - dataPtr += 0xC; + int32_t compHdrRoundedBits = (dataPtr[8] >> 1) & 3; + comp->supportsPartial = (dataPtr[8] & 8) != 0; - comp->roundedBitsMask = 0; + comp->dataOffset = compOffset; + comp->tileFlag = tile->tileFlag; - if (compHdrRoundedBits) - { - if (img->levels || !comp->supportsPartial) - return -1; + compOffset += comp->compSize; + dataSize -= 0xC; + dataPtr += 0xC; - comp->roundedBitsMask = 1 << (compHdrRoundedBits - 1); - } + comp->roundedBitsMask = 0; - if (crxReadSubbandHeaders(hdr, img, tile, comp, &dataPtr, &dataSize)) - return -1; + if (compHdrRoundedBits) { + if (img->levels || !comp->supportsPartial) { + return -1; + } + + comp->roundedBitsMask = 1 << (compHdrRoundedBits - 1); + } + + if (crxReadSubbandHeaders(hdr, img, tile, comp, &dataPtr, &dataSize)) { + return -1; + } + } } - } - return 0; + + return 0; } -int crxSetupImageData(crx_data_header_t *hdr, CrxImage *img, int16_t *outBuf, +int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, int16_t* outBuf, uint64_t mdatOffset, uint32_t mdatSize, - uint8_t *mdatHdrPtr) + uint8_t* mdatHdrPtr) { - int IncrBitTable[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, - 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0}; + int IncrBitTable[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0 + }; - img->planeWidth = hdr->f_width; - img->planeHeight = hdr->f_height; + img->planeWidth = hdr->f_width; + img->planeHeight = hdr->f_height; - if (hdr->tileWidth < 0x16 || hdr->tileHeight < 0x16 || - img->planeWidth > 0x7FFF || img->planeHeight > 0x7FFF) - return -1; - - img->tileCols = (img->planeWidth + hdr->tileWidth - 1) / hdr->tileWidth; - img->tileRows = (img->planeHeight + hdr->tileHeight - 1) / hdr->tileHeight; - - if (img->tileCols > 0xFF || img->tileRows > 0xFF || - img->planeWidth - hdr->tileWidth * (img->tileCols - 1) < 0x16 || - img->planeHeight - hdr->tileHeight * (img->tileRows - 1) < 0x16) - return -1; - - img->tiles = 0; - img->levels = hdr->imageLevels; - img->subbandCount = 3 * img->levels + 1; // 3 bands per level + one last LL - img->nPlanes = hdr->nPlanes; - img->nBits = hdr->nBits; - img->encType = hdr->encType; - img->samplePrecision = hdr->nBits + IncrBitTable[4 * hdr->encType + 2] + 1; - img->mdatOffset = mdatOffset + hdr->mdatHdrSize; - img->mdatSize = mdatSize; - img->planeBuf = 0; - img->outBufs[0] = img->outBufs[1] = img->outBufs[2] = img->outBufs[3] = 0; - - // The encoding type 3 needs all 4 planes to be decoded to generate row of - // RGGB values. It seems to be using some other colour space for raw encoding - // It is a massive buffer so ideallly it will need a different approach: - // decode planes line by line and convert single line then without - // intermediate plane buffer. At the moment though it's too many changes so - // left as is. - if (img->encType == 3 && img->nPlanes == 4 && img->nBits > 8) - { - img->planeBuf = - (int16_t *)malloc(img->planeHeight * img->planeWidth * img->nPlanes * - ((img->samplePrecision + 7) >> 3)); - if (!img->planeBuf) - return -1; - } - - int32_t rowSize = 2 * img->planeWidth; - - if (img->nPlanes == 1) - img->outBufs[0] = outBuf; - else - switch (hdr->cfaLayout) - { - case 0: - // R G - // G B - img->outBufs[0] = outBuf; - img->outBufs[1] = outBuf + 1; - img->outBufs[2] = outBuf + rowSize; - img->outBufs[3] = img->outBufs[2] + 1; - break; - case 1: - // G R - // B G - img->outBufs[1] = outBuf; - img->outBufs[0] = outBuf + 1; - img->outBufs[3] = outBuf + rowSize; - img->outBufs[2] = img->outBufs[3] + 1; - break; - case 2: - // G B - // R G - img->outBufs[2] = outBuf; - img->outBufs[3] = outBuf + 1; - img->outBufs[0] = outBuf + rowSize; - img->outBufs[1] = img->outBufs[0] + 1; - break; - case 3: - // B G - // G R - img->outBufs[3] = outBuf; - img->outBufs[2] = outBuf + 1; - img->outBufs[1] = outBuf + rowSize; - img->outBufs[0] = img->outBufs[1] + 1; - break; + if (hdr->tileWidth < 0x16 || hdr->tileHeight < 0x16 || + img->planeWidth > 0x7FFF || img->planeHeight > 0x7FFF) { + return -1; } - // read header - return crxReadImageHeaders(hdr, img, mdatHdrPtr, mdatSize); + img->tileCols = (img->planeWidth + hdr->tileWidth - 1) / hdr->tileWidth; + img->tileRows = (img->planeHeight + hdr->tileHeight - 1) / hdr->tileHeight; + + if (img->tileCols > 0xFF || img->tileRows > 0xFF || + img->planeWidth - hdr->tileWidth * (img->tileCols - 1) < 0x16 || + img->planeHeight - hdr->tileHeight * (img->tileRows - 1) < 0x16) { + return -1; + } + + img->tiles = nullptr; + img->levels = hdr->imageLevels; + img->subbandCount = 3 * img->levels + 1;// 3 bands per level + one last LL + img->nPlanes = hdr->nPlanes; + img->nBits = hdr->nBits; + img->encType = hdr->encType; + img->samplePrecision = hdr->nBits + IncrBitTable[4 * hdr->encType + 2] + 1; + img->mdatOffset = mdatOffset + hdr->mdatHdrSize; + img->mdatSize = mdatSize; + img->planeBuf = nullptr; + img->outBufs[0] = img->outBufs[1] = img->outBufs[2] = img->outBufs[3] = nullptr; + + // The encoding type 3 needs all 4 planes to be decoded to generate row of + // RGGB values. It seems to be using some other colour space for raw encoding + // It is a massive buffer so ideallly it will need a different approach: + // decode planes line by line and convert single line then without + // intermediate plane buffer. At the moment though it's too many changes so + // left as is. + if (img->encType == 3 && img->nPlanes == 4 && img->nBits > 8) { + img->planeBuf = + (int16_t*)malloc(img->planeHeight * img->planeWidth * img->nPlanes * + ((img->samplePrecision + 7) >> 3)); + + if (!img->planeBuf) { + return -1; + } + } + + int32_t rowSize = 2 * img->planeWidth; + + if (img->nPlanes == 1) { + img->outBufs[0] = outBuf; + } else { + switch (hdr->cfaLayout) { + case 0: { + // R G + // G B + img->outBufs[0] = outBuf; + img->outBufs[1] = outBuf + 1; + img->outBufs[2] = outBuf + rowSize; + img->outBufs[3] = img->outBufs[2] + 1; + break; + } + + case 1: { + // G R + // B G + img->outBufs[1] = outBuf; + img->outBufs[0] = outBuf + 1; + img->outBufs[3] = outBuf + rowSize; + img->outBufs[2] = img->outBufs[3] + 1; + break; + } + + case 2: { + // G B + // R G + img->outBufs[2] = outBuf; + img->outBufs[3] = outBuf + 1; + img->outBufs[0] = outBuf + rowSize; + img->outBufs[1] = img->outBufs[0] + 1; + break; + } + + case 3: { + // B G + // G R + img->outBufs[3] = outBuf; + img->outBufs[2] = outBuf + 1; + img->outBufs[1] = outBuf + rowSize; + img->outBufs[0] = img->outBufs[1] + 1; + break; + } + } + } + + // read header + return crxReadImageHeaders(hdr, img, mdatHdrPtr, mdatSize); } -int crxFreeImageData(CrxImage *img) +int crxFreeImageData(CrxImage* img) { - CrxTile *tile = img->tiles; - int nTiles = img->tileRows * img->tileCols; + CrxTile* tile = img->tiles; + int nTiles = img->tileRows * img->tileCols; - if (img->tiles) - { - for (int32_t curTile = 0; curTile < nTiles; curTile++, tile++) - if (tile[curTile].comps) - for (int32_t curPlane = 0; curPlane < img->nPlanes; curPlane++) - crxFreeSubbandData(img, tile[curTile].comps + curPlane); - free(img->tiles); - img->tiles = 0; - } + if (img->tiles) { + for (int32_t curTile = 0; curTile < nTiles; curTile++, tile++) { + if (tile[curTile].comps) { + for (int32_t curPlane = 0; curPlane < img->nPlanes; curPlane++) { + crxFreeSubbandData(img, tile[curTile].comps + curPlane); + } + } + } - if (img->planeBuf) - { - free(img->planeBuf); - img->planeBuf = 0; - } + free(img->tiles); + img->tiles = nullptr; + } - return 0; + if (img->planeBuf) { + free(img->planeBuf); + img->planeBuf = nullptr; + } + + return 0; } +} // namespace -} // namespace - -void DCraw::crxLoadDecodeLoop(void *img, int nPlanes) +void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) { #ifdef _OPENMP - int results[4]; // nPlanes is always <= 4 -#pragma omp parallel for - for (int32_t plane = 0; plane < nPlanes; ++plane) - results[plane] = crxDecodePlane(img, plane); + int results[4]; // nPlanes is always <= 4 + #pragma omp parallel for + + for (int32_t plane = 0; plane < nPlanes; ++plane) { + results[plane] = crxDecodePlane(img, plane); + } + + for (int32_t plane = 0; plane < nPlanes; ++plane) { + if (results[plane]) { + derror(); + } + } - for (int32_t plane = 0; plane < nPlanes; ++plane) - if (results[plane]) - derror(); #else - for (int32_t plane = 0; plane < nPlanes; ++plane) - if (crxDecodePlane(img, plane)) - derror(); + + for (int32_t plane = 0; plane < nPlanes; ++plane) { + if (crxDecodePlane(img, plane)) { + derror(); + } + } + #endif } -void DCraw::crxConvertPlaneLineDf(void *p, int imageRow) +void DCraw::crxConvertPlaneLineDf(void* p, int imageRow) { - crxConvertPlaneLine((CrxImage *)p, imageRow); + crxConvertPlaneLine((CrxImage*)p, imageRow); } -void DCraw::crxLoadFinalizeLoopE3(void *p, int planeHeight) +void DCraw::crxLoadFinalizeLoopE3(void* p, int planeHeight) { #ifdef _OPENMP -#pragma omp parallel for + #pragma omp parallel for #endif - for (int i = 0; i < planeHeight; ++i) - crxConvertPlaneLineDf(p, i); + + for (int i = 0; i < planeHeight; ++i) { + crxConvertPlaneLineDf(p, i); + } } void DCraw::crxLoadRaw() { - CrxImage img; - if (RT_canon_CR3_data.crx_track_selected >= - LIBRAW_CRXTRACKS_MAXCOUNT) - derror(); - crx_data_header_t hdr = - RT_canon_CR3_data - .crx_header[RT_canon_CR3_data.crx_track_selected]; + CrxImage img; - LibRaw_abstract_datastream input = { ifp }; - img.input = &input; //libraw_internal_data.internal_data.input; + if (RT_canon_CR3_data.crx_track_selected >= + LIBRAW_CRXTRACKS_MAXCOUNT) { + derror(); + } - // update sizes for the planes - if (hdr.nPlanes == 4) - { - hdr.f_width >>= 1; - hdr.f_height >>= 1; - hdr.tileWidth >>= 1; - hdr.tileHeight >>= 1; - } + crx_data_header_t hdr = + RT_canon_CR3_data + .crx_header[RT_canon_CR3_data.crx_track_selected]; + + LibRaw_abstract_datastream input = {ifp}; + img.input = &input; // libraw_internal_data.internal_data.input; + + // update sizes for the planes + if (hdr.nPlanes == 4) { + hdr.f_width >>= 1; + hdr.f_height >>= 1; + hdr.tileWidth >>= 1; + hdr.tileHeight >>= 1; + } // /*imgdata.color.*/maximum = (1 << hdr.nBits) - 1; - uint8_t *hdrBuf = (uint8_t *)malloc(hdr.mdatHdrSize); + uint8_t* hdrBuf = (uint8_t*)malloc(hdr.mdatHdrSize); - // read image header + // read image header #ifdef _OPENMP -#pragma omp critical + #pragma omp critical #endif - { + { #ifndef _OPENMP - /*libraw_internal_data.internal_data.input->*/input.lock(); + /*libraw_internal_data.internal_data.input->*/ input.lock(); #endif - /*libraw_internal_data.internal_data.input->*/input.seek( - data_offset, SEEK_SET); - /*libraw_internal_data.internal_data.input->*/input.read(hdrBuf, 1, hdr.mdatHdrSize); + /*libraw_internal_data.internal_data.input->*/ input.seek( + data_offset, SEEK_SET); + /*libraw_internal_data.internal_data.input->*/ input.read(hdrBuf, 1, hdr.mdatHdrSize); #ifndef _OPENMP - /*libraw_internal_data.internal_data.input->*/input.unlock(); + /*libraw_internal_data.internal_data.input->*/ input.unlock(); #endif - } + } - // parse and setup the image data - if (crxSetupImageData(&hdr, &img, (int16_t *)raw_image, - hdr.MediaOffset/*data_offset*/, - hdr.MediaSize/*RT_canon_CR3_data.data_size*/, hdrBuf)) - derror(); - free(hdrBuf); + // parse and setup the image data + if (crxSetupImageData(&hdr, &img, (int16_t*)raw_image, + hdr.MediaOffset /*data_offset*/, + hdr.MediaSize /*RT_canon_CR3_data.data_size*/, hdrBuf)) { + derror(); + } - crxLoadDecodeLoop(&img, hdr.nPlanes); + free(hdrBuf); - if (img.encType == 3) - crxLoadFinalizeLoopE3(&img, img.planeHeight); + crxLoadDecodeLoop(&img, hdr.nPlanes); - crxFreeImageData(&img); + if (img.encType == 3) { + crxLoadFinalizeLoopE3(&img, img.planeHeight); + } + + crxFreeImageData(&img); } -int DCraw::crxParseImageHeader(uchar *cmp1TagData, unsigned int nTrack) +int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) { - if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) - return -1; - if (!cmp1TagData) - return -1; + if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) { + return -1; + } - crx_data_header_t *hdr = - &RT_canon_CR3_data.crx_header[nTrack]; + if (!cmp1TagData) { + return -1; + } - hdr->version = sgetn(2, cmp1TagData + 4); - hdr->f_width = sgetn(4, cmp1TagData + 8); - hdr->f_height = sgetn(4, cmp1TagData + 12); - hdr->tileWidth = sgetn(4, cmp1TagData + 16); - hdr->tileHeight = sgetn(4, cmp1TagData + 20); - hdr->nBits = cmp1TagData[24]; - hdr->nPlanes = cmp1TagData[25] >> 4; - hdr->cfaLayout = cmp1TagData[25] & 0xF; - hdr->encType = cmp1TagData[26] >> 4; - hdr->imageLevels = cmp1TagData[26] & 0xF; - hdr->hasTileCols = cmp1TagData[27] >> 7; - hdr->hasTileRows = (cmp1TagData[27] >> 6) & 1; - hdr->mdatHdrSize = sgetn(4, cmp1TagData + 28); + crx_data_header_t* hdr = + &RT_canon_CR3_data.crx_header[nTrack]; - // validation - if (hdr->version != 0x100 || !hdr->mdatHdrSize) - return -1; - if (hdr->encType == 1) - { - if (hdr->nBits > 15) - return -1; - } - else - { - if (hdr->encType && hdr->encType != 3) - return -1; - if (hdr->nBits > 14) - return -1; - } + hdr->version = sgetn(2, cmp1TagData + 4); + hdr->f_width = sgetn(4, cmp1TagData + 8); + hdr->f_height = sgetn(4, cmp1TagData + 12); + hdr->tileWidth = sgetn(4, cmp1TagData + 16); + hdr->tileHeight = sgetn(4, cmp1TagData + 20); + hdr->nBits = cmp1TagData[24]; + hdr->nPlanes = cmp1TagData[25] >> 4; + hdr->cfaLayout = cmp1TagData[25] & 0xF; + hdr->encType = cmp1TagData[26] >> 4; + hdr->imageLevels = cmp1TagData[26] & 0xF; + hdr->hasTileCols = cmp1TagData[27] >> 7; + hdr->hasTileRows = (cmp1TagData[27] >> 6) & 1; + hdr->mdatHdrSize = sgetn(4, cmp1TagData + 28); - if (hdr->nPlanes == 1) - { - if (hdr->cfaLayout || hdr->encType) - return -1; - if (hdr->nBits != 8) - return -1; - } - else if (hdr->nPlanes != 4 || (hdr->f_width & 1) || (hdr->f_height & 1) || - (hdr->tileWidth & 1) || (hdr->tileHeight & 1) || hdr->cfaLayout > 3 || - (hdr->encType && hdr->encType != 1 && hdr->encType != 3) || - hdr->nBits == 8) - return -1; + // validation + if (hdr->version != 0x100 || !hdr->mdatHdrSize) { + return -1; + } - if (hdr->tileWidth > hdr->f_width || hdr->tileHeight > hdr->f_height) - return -1; + if (hdr->encType == 1) { + if (hdr->nBits > 15) { + return -1; + } + } else { + if (hdr->encType && hdr->encType != 3) { + return -1; + } - if (hdr->imageLevels > 3 || hdr->hasTileCols > 1 || hdr->hasTileRows > 1) - return -1; - return 0; + if (hdr->nBits > 14) { + return -1; + } + } + + if (hdr->nPlanes == 1) { + if (hdr->cfaLayout || hdr->encType) { + return -1; + } + + if (hdr->nBits != 8) { + return -1; + } + } else if (hdr->nPlanes != 4 || (hdr->f_width & 1) || (hdr->f_height & 1) || + (hdr->tileWidth & 1) || (hdr->tileHeight & 1) || hdr->cfaLayout > 3 || + (hdr->encType && hdr->encType != 1 && hdr->encType != 3) || + hdr->nBits == 8) { + return -1; + } + + if (hdr->tileWidth > hdr->f_width || hdr->tileHeight > hdr->f_height) { + return -1; + } + + if (hdr->imageLevels > 3 || hdr->hasTileCols > 1 || hdr->hasTileRows > 1) { + return -1; + } + + return 0; } #undef _abs From 2ae5f198d7237cbec8649123ed2b0e0e94de5f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Fri, 22 Nov 2019 10:40:01 +0100 Subject: [PATCH 07/20] Cleanups for the first few lines --- rtengine/canon_cr3_decoder.cc | 730 +++++++++++++++++----------------- 1 file changed, 375 insertions(+), 355 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 01bca72f8..609f0c849 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -51,42 +51,40 @@ */ +#include +#include #include -#include #include "dcraw.h" void DCraw::parse_canon_cr3() { - int err; - unsigned long long szAtomList; + strncpy(make, "Canon", sizeof(make)); + + unsigned long long szAtomList = ifp->size; short nesting = -1; + char AtomNameStack[128]; unsigned short nTrack = 0; short TrackType; - char AtomNameStack[128]; - strcpy(make, "Canon"); - szAtomList = ifp->size; - err = parseCR3(0ULL, szAtomList, nesting, AtomNameStack, nTrack, TrackType); + const int err = parseCR3(0, szAtomList, nesting, AtomNameStack, nTrack, TrackType); if (err == 0 || err == -14) { // no error, or too deep nesting selectCRXTrack(nTrack); } } -#define LIBRAW_CRXTRACKS_MAXCOUNT RT_canon_CR3_data.CRXTRACKS_MAXCOUNT - void DCraw::selectCRXTrack(unsigned short maxTrack) { - INT64 bitcounts[LIBRAW_CRXTRACKS_MAXCOUNT], maxbitcount = 0; - uint32_t maxjpegbytes = 0; - memset(bitcounts, 0, sizeof(bitcounts)); + std::int64_t bitcounts[RT_canon_CR3_data.CRXTRACKS_MAXCOUNT] = {}; + std::int64_t maxbitcount = 0; + std::uint32_t maxjpegbytes = 0; - for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) { - CanonCR3Data::crx_data_header_t* d = &RT_canon_CR3_data.crx_header[i]; + for (unsigned int i = 0; i <= maxTrack && i < RT_canon_CR3_data.CRXTRACKS_MAXCOUNT; ++i) { + CanonCR3Data::crx_data_header_t* const d = &RT_canon_CR3_data.crx_header[i]; if (d->MediaType == 1) {// RAW - bitcounts[i] = INT64(d->nBits) * INT64(d->f_width) * INT64(d->f_height); + bitcounts[i] = std::int64_t(d->nBits) * std::int64_t(d->f_width) * std::int64_t(d->f_height); if (bitcounts[i] > maxbitcount) { maxbitcount = bitcounts[i]; @@ -105,9 +103,10 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) } bool has_framei = false; - unsigned int framei = 0, framecnt = 0; + unsigned int framei = 0; + unsigned int framecnt = 0; - for (unsigned int i = 0; i <= maxTrack && i < LIBRAW_CRXTRACKS_MAXCOUNT; i++) { + for (unsigned int i = 0; i <= maxTrack && i < RT_canon_CR3_data.CRXTRACKS_MAXCOUNT; ++i) { if (bitcounts[i] == maxbitcount) { if (framecnt <= shot_select) { has_framei = true; @@ -121,8 +120,7 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) is_raw = framecnt; if (has_framei) { - CanonCR3Data::crx_data_header_t* d = - &RT_canon_CR3_data.crx_header[framei]; + CanonCR3Data::crx_data_header_t* const d = &RT_canon_CR3_data.crx_header[framei]; data_offset = d->MediaOffset; // data_size = d->MediaSize; raw_width = d->f_width; @@ -154,11 +152,11 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) RT_canon_CR3_data.crx_track_selected = framei; int tiff_idx = -1; - INT64 tpixels = 0; + std::int64_t tpixels = 0; for (unsigned int i = 0; i < tiff_nifds; i++) { - if (INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height) > tpixels) { - tpixels = INT64(tiff_ifd[i].height) * INT64(tiff_ifd[i].height); + if (std::int64_t(tiff_ifd[i].height) * std::int64_t(tiff_ifd[i].height) > tpixels) { + tpixels = std::int64_t(tiff_ifd[i].height) * std::int64_t(tiff_ifd[i].height); tiff_idx = i; } } @@ -169,14 +167,14 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) } } -#define FORC4 for (c = 0; c < 4; c++) - -#define bad_hdr \ - (((order != 0x4d4d) && (order != 0x4949)) || (get2() != 0x002a) || \ - (get4() != 0x00000008)) -int DCraw::parseCR3(unsigned long long oAtomList, - unsigned long long szAtomList, short& nesting, - char* AtomNameStack, unsigned short& nTrack, short& TrackType) +int DCraw::parseCR3( + unsigned long long oAtomList, + unsigned long long szAtomList, + short& nesting, + char* AtomNameStack, + unsigned short& nTrack, + short& TrackType +) { /* Atom starts with 4 bytes for Atom size and 4 bytes containing Atom name @@ -289,17 +287,22 @@ int DCraw::parseCR3(unsigned long long oAtomList, ushort tL; // Atom length represented in 4 or 8 bytes char nmAtom[5]; // Atom name - unsigned long long oAtom, szAtom; // Atom offset and Atom size - unsigned long long oAtomContent, - szAtomContent; // offset and size of Atom content + unsigned long long oAtom; + unsigned long long szAtom; // Atom offset and Atom size + unsigned long long oAtomContent; + unsigned long long szAtomContent; // offset and size of Atom content unsigned long long lHdr; char UIID[16]; uchar CMP1[36]; - char HandlerType[5], MediaFormatID[5]; + char HandlerType[5]; + char MediaFormatID[5]; // unsigned ImageWidth, ImageHeight; - long relpos_inDir, relpos_inBox; - unsigned szItem, Tag, lTag; + long relpos_inDir; + long relpos_inBox; + unsigned szItem; + unsigned Tag; + unsigned lTag; ushort tItem; nmAtom[0] = MediaFormatID[0] = nmAtom[4] = MediaFormatID[4] = '\0'; @@ -314,13 +317,27 @@ int DCraw::parseCR3(unsigned long long oAtomList, short s_order = order; + const auto is_bad_header = + [this]() -> bool + { + return + ( + order != 0x4D4D + && order != 0x4949 + ) + || get2() != 0x002A + || get4() != 0x00000008; + }; + while ((oAtom + 8ULL) <= (oAtomList + szAtomList)) { lHdr = 0ULL; err = 0; order = 0x4d4d; fseek(ifp, oAtom, SEEK_SET); szAtom = get4(); - FORC4 nmAtom[c] = AtomNameStack[nesting * 4 + c] = fgetc(ifp); + for (c = 0; c < 4; c++) { + nmAtom[c] = AtomNameStack[nesting * 4 + c] = fgetc(ifp); + } AtomNameStack[(nesting + 1) * 4] = '\0'; tL = 4; AtomType = 0; @@ -364,7 +381,7 @@ int DCraw::parseCR3(unsigned long long oAtomList, nTrack++; TrackType = 0; - if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) { + if (nTrack >= RT_canon_CR3_data.CRXTRACKS_MAXCOUNT) { break; } } @@ -384,7 +401,7 @@ int DCraw::parseCR3(unsigned long long oAtomList, short q_order = order; order = get2(); - if ((tL != 4) || bad_hdr) { + if ((tL != 4) || is_bad_header()) { err = -4; goto fin; } @@ -395,7 +412,7 @@ int DCraw::parseCR3(unsigned long long oAtomList, short q_order = order; order = get2(); - if ((tL != 4) || bad_hdr) { + if ((tL != 4) || is_bad_header()) { err = -5; goto fin; } @@ -406,7 +423,7 @@ int DCraw::parseCR3(unsigned long long oAtomList, short q_order = order; order = get2(); - if ((tL != 4) || bad_hdr) { + if ((tL != 4) || is_bad_header()) { err = -6; goto fin; } @@ -418,19 +435,21 @@ int DCraw::parseCR3(unsigned long long oAtomList, short q_order = order; order = get2(); - if ((tL != 4) || bad_hdr) { + if ((tL != 4) || is_bad_header()) { err = -6; goto fin; } - INT64 off = ftell(ifp); + std::int64_t off = ftell(ifp); parse_gps(oAtomContent); fseek(ifp, off, SEEK_SET); // parse_gps_libraw(oAtomContent); order = q_order; } else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) { fseek(ifp, 8L, SEEK_CUR); - FORC4 HandlerType[c] = fgetc(ifp); + for (c = 0; c < 4; c++) { + HandlerType[c] = fgetc(ifp); + } for (c = 1; c < sizeof sHandlerType / sizeof * sHandlerType; c++) { if (!strcmp(HandlerType, sHandlerType[c])) { @@ -447,7 +466,9 @@ int DCraw::parseCR3(unsigned long long oAtomList, goto fin; } - FORC4 MediaFormatID[c] = fgetc(ifp); + for (c = 0; c < 4; c++) { + MediaFormatID[c] = fgetc(ifp); + } if ((TrackType == 2) && (!strcmp(MediaFormatID, "CRAW"))) { if (szAtomContent >= 44) { @@ -541,7 +562,7 @@ int DCraw::parseCR3(unsigned long long oAtomList, short q_order = order; order = get2(); - if (bad_hdr) { + if (is_bad_header()) { err = -13; goto fin; } @@ -627,13 +648,13 @@ static unsigned sgetn(int n, unsigned char* s) #define CRX_BUF_SIZE 0x10000 #if !defined (_WIN32) || (defined (__GNUC__) && !defined (__INTRINSIC_SPECIAL__BitScanReverse)) /* __INTRINSIC_SPECIAL__BitScanReverse found in MinGW32-W64 v7.30 headers, may be there is a better solution? */ -using DWORD = uint32_t; -using byte = uint8_t; +using DWORD = std::uint32_t; +using byte = std::uint8_t; libraw_inline void _BitScanReverse(DWORD* Index, unsigned long Mask) { *Index = sizeof(unsigned long) * 8 - 1 - __builtin_clzl(Mask); } -uint32_t _byteswap_ulong(uint32_t x) +std::uint32_t _byteswap_ulong(std::uint32_t x) { #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ return x; @@ -665,98 +686,98 @@ struct LibRaw_abstract_datastream { }; struct CrxBitstream { - uint8_t mdatBuf[CRX_BUF_SIZE]; - uint64_t mdatSize; - uint64_t curBufOffset; - uint32_t curPos; - uint32_t curBufSize; - uint32_t bitData; - int32_t bitsLeft; + std::uint8_t mdatBuf[CRX_BUF_SIZE]; + std::uint64_t mdatSize; + std::uint64_t curBufOffset; + std::uint32_t curPos; + std::uint32_t curBufSize; + std::uint32_t bitData; + std::int32_t bitsLeft; LibRaw_abstract_datastream* input; }; struct CrxBandParam { CrxBitstream bitStream; - int16_t subbandWidth; - int16_t subbandHeight; - int32_t roundedBitsMask; - int32_t roundedBits; - int16_t curLine; - int32_t* lineBuf0; - int32_t* lineBuf1; - int32_t* lineBuf2; - int32_t sParam; - int32_t kParam; - int32_t* paramData; - int32_t* nonProgrData; - int8_t supportsPartial; + std::int16_t subbandWidth; + std::int16_t subbandHeight; + std::int32_t roundedBitsMask; + std::int32_t roundedBits; + std::int16_t curLine; + std::int32_t* lineBuf0; + std::int32_t* lineBuf1; + std::int32_t* lineBuf2; + std::int32_t sParam; + std::int32_t kParam; + std::int32_t* paramData; + std::int32_t* nonProgrData; + std::int8_t supportsPartial; }; struct CrxWaveletTransform { - int32_t* subband0Buf; - int32_t* subband1Buf; - int32_t* subband2Buf; - int32_t* subband3Buf; - int32_t* lineBuf[8]; - int16_t curLine; - int16_t curH; - int8_t fltTapH; - int16_t height; - int16_t width; + std::int32_t* subband0Buf; + std::int32_t* subband1Buf; + std::int32_t* subband2Buf; + std::int32_t* subband3Buf; + std::int32_t* lineBuf[8]; + std::int16_t curLine; + std::int16_t curH; + std::int8_t fltTapH; + std::int16_t height; + std::int16_t width; }; struct CrxSubband { CrxBandParam* bandParam; - uint64_t mdatOffset; - uint8_t* bandBuf; - int32_t bandSize; - uint64_t dataSize; - int8_t supportsPartial; - int32_t quantValue; - uint16_t width; - uint16_t height; - int32_t paramK; - int64_t dataOffset; + std::uint64_t mdatOffset; + std::uint8_t* bandBuf; + std::int32_t bandSize; + std::uint64_t dataSize; + std::int8_t supportsPartial; + std::int32_t quantValue; + std::uint16_t width; + std::uint16_t height; + std::int32_t paramK; + std::int64_t dataOffset; }; struct CrxPlaneComp { byte* compBuf; CrxSubband* subBands; CrxWaveletTransform* waveletTransform; - int8_t compNumber; - int64_t dataOffset; - int32_t compSize; - int8_t supportsPartial; - int32_t roundedBitsMask; - int8_t tileFlag; + std::int8_t compNumber; + std::int64_t dataOffset; + std::int32_t compSize; + std::int8_t supportsPartial; + std::int32_t roundedBitsMask; + std::int8_t tileFlag; }; struct CrxTile { CrxPlaneComp* comps; - int8_t tileFlag; - int8_t tileNumber; - int64_t dataOffset; - int32_t tileSize; - uint16_t width; - uint16_t height; + std::int8_t tileFlag; + std::int8_t tileNumber; + std::int64_t dataOffset; + std::int32_t tileSize; + std::uint16_t width; + std::uint16_t height; }; struct CrxImage { - uint8_t nPlanes; - uint16_t planeWidth; - uint16_t planeHeight; - uint8_t samplePrecision; - uint8_t subbandCount; - uint8_t levels; - uint8_t nBits; - uint8_t encType; - uint8_t tileCols; - uint8_t tileRows; + std::uint8_t nPlanes; + std::uint16_t planeWidth; + std::uint16_t planeHeight; + std::uint8_t samplePrecision; + std::uint8_t subbandCount; + std::uint8_t levels; + std::uint8_t nBits; + std::uint8_t encType; + std::uint8_t tileCols; + std::uint8_t tileRows; CrxTile* tiles; - uint64_t mdatOffset; - uint64_t mdatSize; - int16_t* outBufs[4];// one per plane - int16_t* planeBuf; + std::uint64_t mdatOffset; + std::uint64_t mdatSize; + std::int16_t* outBufs[4];// one per plane + std::int16_t* planeBuf; LibRaw_abstract_datastream* input; }; @@ -767,7 +788,7 @@ enum TileFlags { E_HAS_TILES_ON_THE_TOP = 8 }; -int32_t exCoefNumTbl[0x120] = { +std::int32_t exCoefNumTbl[0x120] = { // level 1 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @@ -787,13 +808,13 @@ int32_t exCoefNumTbl[0x120] = { 8, 8, 2, 1, 4, 3, 1, 1, 1, 1, 1, 1, 8, 7, 1, 1, 3, 3, 1, 1, 1, 1 }; -uint32_t JS[32] = {1, 1, 1, 1, 2, 2, 2, 2, +std::uint32_t JS[32] = {1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 0x10, 0x10, 0x20, 0x20, 0x40, 0x40, 0x80, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 }; -uint32_t J[32] = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, +std::uint32_t J[32] = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; @@ -828,10 +849,10 @@ static inline void crxFillBuffer(CrxBitstream* bitStrm) libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) { -// uint32_t bitData = bitStrm->bitData; - uint32_t nonZeroBit = 0; - uint64_t nextData = 0; - int32_t result = 0; +// std::uint32_t bitData = bitStrm->bitData; + std::uint32_t nonZeroBit = 0; + std::uint64_t nextData = 0; + std::int32_t result = 0; if (bitStrm->bitData) { _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)bitStrm->bitData); @@ -839,12 +860,12 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) bitStrm->bitData <<= 32 - nonZeroBit; bitStrm->bitsLeft -= 32 - nonZeroBit; } else { - uint32_t bitsLeft = bitStrm->bitsLeft; + std::uint32_t bitsLeft = bitStrm->bitsLeft; while (true) { while (bitStrm->curPos + 4 <= bitStrm->curBufSize) { nextData = - _byteswap_ulong(*(uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); + _byteswap_ulong(*(std::uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); bitStrm->curPos += 4; crxFillBuffer(bitStrm); @@ -874,7 +895,7 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) } _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)nextData); - result = (uint32_t)(bitsLeft + 7 - nonZeroBit); + result = (std::uint32_t)(bitsLeft + 7 - nonZeroBit); bitStrm->bitData = nextData << (32 - nonZeroBit); bitStrm->bitsLeft = nonZeroBit; } @@ -882,19 +903,19 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) return result; } -libraw_inline uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) +libraw_inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) { int bitsLeft = bitStrm->bitsLeft; - uint32_t bitData = bitStrm->bitData; - uint32_t nextWord; - uint8_t nextByte; - uint32_t result; + std::uint32_t bitData = bitStrm->bitData; + std::uint32_t nextWord; + std::uint8_t nextByte; + std::uint32_t result; if (bitsLeft < bits) { // get them from stream if (bitStrm->curPos + 4 <= bitStrm->curBufSize) { nextWord = - _byteswap_ulong(*(uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); + _byteswap_ulong(*(std::uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); bitStrm->curPos += 4; crxFillBuffer(bitStrm); bitStrm->bitsLeft = 32 - (bits - bitsLeft); @@ -922,23 +943,23 @@ libraw_inline uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) return result; } -libraw_inline int crxPredictKParameter(int32_t prevK, int32_t bitCode, - int32_t maxVal = 0) +libraw_inline int crxPredictKParameter(std::int32_t prevK, std::int32_t bitCode, + std::int32_t maxVal = 0) { - int32_t newKParam = prevK - (bitCode < (1 << prevK >> 1)) + + std::int32_t newKParam = prevK - (bitCode < (1 << prevK >> 1)) + ((bitCode >> prevK) > 2) + ((bitCode >> prevK) > 5); return !maxVal || newKParam < maxVal ? newKParam : maxVal; } libraw_inline void crxDecodeSymbolL1(CrxBandParam* param, - int32_t doMedianPrediction, - int32_t notEOL = 0) + std::int32_t doMedianPrediction, + std::int32_t notEOL = 0) { if (doMedianPrediction) { - int32_t symb[4]; + std::int32_t symb[4]; - int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; + std::int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; symb[2] = param->lineBuf1[0]; symb[0] = symb[1] = delta + symb[2]; symb[3] = param->lineBuf0[1]; @@ -951,7 +972,7 @@ libraw_inline void crxDecodeSymbolL1(CrxBandParam* param, } // get next error symbol - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -965,7 +986,7 @@ libraw_inline void crxDecodeSymbolL1(CrxBandParam* param, // for not end of the line - use one symbol ahead to estimate next K if (notEOL) { - int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; + std::int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; bitCode = (bitCode + _abs(nextDelta)) >> 1; ++param->lineBuf0; } @@ -1051,15 +1072,15 @@ int crxDecodeLine(CrxBandParam* param) } libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, - int32_t doSym = 1, - int32_t doCode = 1) + std::int32_t doSym = 1, + std::int32_t doCode = 1) { - int32_t sym = param->lineBuf0[1]; + std::int32_t sym = param->lineBuf0[1]; if (doSym) { // calculate the next symbol gradient - int32_t symb[4]; - int32_t deltaH = param->lineBuf0[1] - param->lineBuf0[0]; + std::int32_t symb[4]; + std::int32_t deltaH = param->lineBuf0[1] - param->lineBuf0[0]; symb[2] = param->lineBuf1[0]; symb[0] = symb[1] = deltaH + symb[2]; symb[3] = param->lineBuf0[1]; @@ -1068,7 +1089,7 @@ libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (deltaH < 0))]; } - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1077,7 +1098,7 @@ libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, (bitCode << param->kParam); } - int32_t code = -(bitCode & 1) ^ (bitCode >> 1); + std::int32_t code = -(bitCode & 1) ^ (bitCode >> 1); param->lineBuf1[1] = param->roundedBitsMask * 2 * code + static_cast(code < 0) + sym; if (doCode) { @@ -1102,11 +1123,11 @@ libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, int crxDecodeLineRounded(CrxBandParam* param) { - int32_t valueReached = 0; + std::int32_t valueReached = 0; param->lineBuf0[0] = param->lineBuf0[1]; param->lineBuf1[0] = param->lineBuf0[1]; - int32_t length = param->subbandWidth; + std::int32_t length = param->subbandWidth; for (; length > 1; --length) { if (_abs(param->lineBuf0[2] - param->lineBuf0[1]) > param->roundedBitsMask) { @@ -1189,11 +1210,11 @@ int crxDecodeLineRounded(CrxBandParam* param) int crxDecodeLineNoRefPrevLine(CrxBandParam* param) { - int32_t i = 0; + std::int32_t i = 0; for (; i < param->subbandWidth - 1; i++) { if (param->lineBuf0[i + 2] | param->lineBuf0[i + 1] | param->lineBuf1[i]) { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1255,14 +1276,14 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) } if (nSyms > 0) { - memset(param->lineBuf1 + i + 1, 0, nSyms * sizeof(int32_t)); - memset(param->lineBuf2 + i, 0, nSyms * sizeof(int32_t)); + memset(param->lineBuf1 + i + 1, 0, nSyms * sizeof(std::int32_t)); + memset(param->lineBuf2 + i, 0, nSyms * sizeof(std::int32_t)); i += nSyms; } if (i >= param->subbandWidth - 1) { if (i == param->subbandWidth - 1) { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1278,7 +1299,7 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) continue; } else { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1304,7 +1325,7 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) } if (i == param->subbandWidth - 1) { - int32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::int32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1325,7 +1346,7 @@ int crxDecodeTopLine(CrxBandParam* param) { param->lineBuf1[0] = 0; - int32_t length = param->subbandWidth; + std::int32_t length = param->subbandWidth; // read the line from bitstream for (; length > 1; --length) { @@ -1384,7 +1405,7 @@ int crxDecodeTopLine(CrxBandParam* param) param->lineBuf1[1] = 0; } - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1400,7 +1421,7 @@ int crxDecodeTopLine(CrxBandParam* param) if (length == 1) { param->lineBuf1[1] = param->lineBuf1[0]; - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1423,7 +1444,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) { param->lineBuf1[0] = 0; - int32_t length = param->subbandWidth; + std::int32_t length = param->subbandWidth; // read the line from bitstream for (; length > 1; --length) { @@ -1482,7 +1503,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) param->lineBuf1[1] = 0; } - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1491,14 +1512,14 @@ int crxDecodeTopLineRounded(CrxBandParam* param) (bitCode << param->kParam); } - int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); + std::int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); ++param->lineBuf1; } if (length == 1) { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1507,7 +1528,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) (bitCode << param->kParam); } - int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); + std::int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); ++param->lineBuf1; @@ -1522,11 +1543,11 @@ int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) { param->lineBuf0[0] = 0; param->lineBuf1[0] = 0; - int32_t length = param->subbandWidth; + std::int32_t length = param->subbandWidth; for (; length > 1; --length) { if (param->lineBuf1[0]) { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1589,7 +1610,7 @@ int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) break; } - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1608,7 +1629,7 @@ int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) } if (length == 1) { - uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); @@ -1628,7 +1649,7 @@ int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) return 0; } -int crxDecodeLine(CrxBandParam* param, uint8_t* bandBuf) +int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) { if (!param || !bandBuf) { return -1; @@ -1639,22 +1660,22 @@ int crxDecodeLine(CrxBandParam* param, uint8_t* bandBuf) } if (param->curLine == 0) { - int32_t lineLength = param->subbandWidth + 2; + std::int32_t lineLength = param->subbandWidth + 2; param->sParam = 0; param->kParam = 0; if (param->supportsPartial) { if (param->roundedBitsMask <= 0) { - param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf0 = (std::int32_t*)param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeTopLine(param)) { return -1; } - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } else { param->roundedBits = 1; @@ -1665,87 +1686,87 @@ int crxDecodeLine(CrxBandParam* param, uint8_t* bandBuf) } } - param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf0 = (std::int32_t*)param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeTopLineRounded(param)) { return -1; } - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } } else { - param->lineBuf2 = (int32_t*)param->nonProgrData; - param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf2 = (std::int32_t*)param->nonProgrData; + param->lineBuf0 = (std::int32_t*)param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeTopLineNoRefPrevLine(param)) { return -1; } - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } } else if (!param->supportsPartial) { - int32_t lineLength = param->subbandWidth + 2; - param->lineBuf2 = (int32_t*)param->nonProgrData; + std::int32_t lineLength = param->subbandWidth + 2; + param->lineBuf2 = (std::int32_t*)param->nonProgrData; if (param->curLine & 1) { - param->lineBuf1 = (int32_t*)param->paramData; + param->lineBuf1 = (std::int32_t*)param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; } else { - param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf0 = (std::int32_t*)param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } - int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeLineNoRefPrevLine(param)) { return -1; } - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } else if (param->roundedBitsMask <= 0) { - int32_t lineLength = param->subbandWidth + 2; + std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { - param->lineBuf1 = (int32_t*)param->paramData; + param->lineBuf1 = (std::int32_t*)param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; } else { - param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf0 = (std::int32_t*)param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } - int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeLine(param)) { return -1; } - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } else { - int32_t lineLength = param->subbandWidth + 2; + std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { - param->lineBuf1 = (int32_t*)param->paramData; + param->lineBuf1 = (std::int32_t*)param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; } else { - param->lineBuf0 = (int32_t*)param->paramData; + param->lineBuf0 = (std::int32_t*)param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } - int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeLineRounded(param)) { return -1; } - memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(int32_t)); + memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } @@ -1754,7 +1775,7 @@ int crxDecodeLine(CrxBandParam* param, uint8_t* bandBuf) int crxDecodeLineWithIQuantization(CrxSubband* subband) { - int32_t q_step_tbl[6] = {0x28, 0x2D, 0x33, 0x39, 0x40, 0x48}; + std::int32_t q_step_tbl[6] = {0x28, 0x2D, 0x33, 0x39, 0x40, 0x48}; if (!subband->dataSize) { memset(subband->bandBuf, 0, subband->bandSize); @@ -1762,7 +1783,7 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) } if (subband->supportsPartial) { - uint32_t bitCode = crxBitstreamGetZeros(&subband->bandParam->bitStream); + std::uint32_t bitCode = crxBitstreamGetZeros(&subband->bandParam->bitStream); if (bitCode >= 23) { bitCode = crxBitstreamGetBits(&subband->bandParam->bitStream, 8); @@ -1790,8 +1811,8 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) } // update subband buffers - int32_t* bandBuf = (int32_t*)subband->bandBuf; - int32_t qScale = + std::int32_t* bandBuf = (std::int32_t*)subband->bandBuf; + std::int32_t qScale = q_step_tbl[subband->quantValue % 6] >> (6 - subband->quantValue / 6); if (subband->quantValue / 6 >= 6) { @@ -1800,7 +1821,7 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) } if (qScale != 1) { - for (int32_t i = 0; i < subband->width; i++) { + for (std::int32_t i = 0; i < subband->width; i++) { bandBuf[i] *= qScale; } } @@ -1808,13 +1829,13 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) return 0; } -void crxHorizontal53(int32_t* lineBufLA, int32_t* lineBufLB, - CrxWaveletTransform* wavelet, uint32_t tileFlag) +void crxHorizontal53(std::int32_t* lineBufLA, std::int32_t* lineBufLB, + CrxWaveletTransform* wavelet, std::uint32_t tileFlag) { - int32_t* band0Buf = wavelet->subband0Buf; - int32_t* band1Buf = wavelet->subband1Buf; - int32_t* band2Buf = wavelet->subband2Buf; - int32_t* band3Buf = wavelet->subband3Buf; + std::int32_t* band0Buf = wavelet->subband0Buf; + std::int32_t* band1Buf = wavelet->subband1Buf; + std::int32_t* band2Buf = wavelet->subband2Buf; + std::int32_t* band3Buf = wavelet->subband3Buf; if (wavelet->width <= 1) { lineBufLA[0] = band0Buf[0]; @@ -1834,7 +1855,7 @@ void crxHorizontal53(int32_t* lineBufLA, int32_t* lineBufLB, ++band2Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufLA[1] = band1Buf[0] + ((delta + lineBufLA[0]) >> 1); lineBufLA[2] = delta; @@ -1851,10 +1872,10 @@ void crxHorizontal53(int32_t* lineBufLA, int32_t* lineBufLB, } if (tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + std::int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufLA[1] = band1Buf[0] + ((deltaA + lineBufLA[0]) >> 1); - int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + std::int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufLB[1] = band3Buf[0] + ((deltaB + lineBufLB[0]) >> 1); if (wavelet->width & 1) { @@ -1878,9 +1899,9 @@ void crxHorizontal53(int32_t* lineBufLA, int32_t* lineBufLB, } } -int32_t* crxIdwt53FilterGetLine(CrxPlaneComp* comp, int32_t level) +std::int32_t* crxIdwt53FilterGetLine(CrxPlaneComp* comp, std::int32_t level) { - int32_t* result = comp->waveletTransform[level] + std::int32_t* result = comp->waveletTransform[level] .lineBuf[(comp->waveletTransform[level].fltTapH - comp->waveletTransform[level].curH + 5) % 5 + @@ -1889,7 +1910,7 @@ int32_t* crxIdwt53FilterGetLine(CrxPlaneComp* comp, int32_t level) return result; } -int crxIdwt53FilterDecode(CrxPlaneComp* comp, int32_t level) +int crxIdwt53FilterDecode(CrxPlaneComp* comp, std::int32_t level) { if (comp->waveletTransform[level].curH) { return 0; @@ -1932,7 +1953,7 @@ int crxIdwt53FilterDecode(CrxPlaneComp* comp, int32_t level) return 0; } -int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) +int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) { CrxWaveletTransform* wavelet = comp->waveletTransform + level; @@ -1953,14 +1974,14 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); } - int32_t* band0Buf = wavelet->subband0Buf; - int32_t* band1Buf = wavelet->subband1Buf; - int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; + std::int32_t* band0Buf = wavelet->subband0Buf; + std::int32_t* band1Buf = wavelet->subband1Buf; + std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + std::int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; - int32_t* lineBufL0 = wavelet->lineBuf[0]; - int32_t* lineBufL1 = wavelet->lineBuf[1]; + std::int32_t* lineBufL0 = wavelet->lineBuf[0]; + std::int32_t* lineBufL1 = wavelet->lineBuf[1]; wavelet->lineBuf[1] = wavelet->lineBuf[2]; wavelet->lineBuf[2] = lineBufL1; @@ -1978,7 +1999,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) ++band0Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - int32_t delta = + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); lineBufL0[2] = delta; @@ -1988,7 +2009,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - int32_t delta = + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); @@ -1996,7 +2017,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) lineBufL0[2] = delta; } } else if (wavelet->width & 1) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); lineBufL0[2] = delta; } else { @@ -2008,8 +2029,8 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) lineBufL0 = wavelet->lineBuf[0]; lineBufL1 = wavelet->lineBuf[1]; - for (int32_t i = 0; i < wavelet->width; i++) { - int32_t delta = lineBufL0[i] - ((lineBufL1[i] + 1) >> 1); + for (std::int32_t i = 0; i < wavelet->width; i++) { + std::int32_t delta = lineBufL0[i] - ((lineBufL1[i] + 1) >> 1); lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); lineBufH2[i] = delta; } @@ -2018,13 +2039,13 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) wavelet->curLine += 3; wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; } else { - int32_t* lineBufL2 = wavelet->lineBuf[2]; - int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + std::int32_t* lineBufL2 = wavelet->lineBuf[2]; + std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; wavelet->lineBuf[1] = lineBufL2; wavelet->lineBuf[2] = wavelet->lineBuf[1]; - for (int32_t i = 0; i < wavelet->width; i++) { + for (std::int32_t i = 0; i < wavelet->width; i++) { lineBufH1[i] = lineBufH0[i] + lineBufL2[i]; } @@ -2042,17 +2063,17 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); } - int32_t* band0Buf = wavelet->subband0Buf; - int32_t* band1Buf = wavelet->subband1Buf; - int32_t* band2Buf = wavelet->subband2Buf; - int32_t* band3Buf = wavelet->subband3Buf; + std::int32_t* band0Buf = wavelet->subband0Buf; + std::int32_t* band1Buf = wavelet->subband1Buf; + std::int32_t* band2Buf = wavelet->subband2Buf; + std::int32_t* band3Buf = wavelet->subband3Buf; - int32_t* lineBufL0 = wavelet->lineBuf[0]; - int32_t* lineBufL1 = wavelet->lineBuf[1]; - int32_t* lineBufL2 = wavelet->lineBuf[2]; - int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; + std::int32_t* lineBufL0 = wavelet->lineBuf[0]; + std::int32_t* lineBufL1 = wavelet->lineBuf[1]; + std::int32_t* lineBufL2 = wavelet->lineBuf[2]; + std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + std::int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; wavelet->lineBuf[1] = wavelet->lineBuf[2]; wavelet->lineBuf[2] = lineBufL1; @@ -2076,7 +2097,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) ++band2Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); lineBufL0[2] = delta; @@ -2093,10 +2114,10 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + std::int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((deltaA + lineBufL0[0]) >> 1); - int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + std::int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufL1[1] = band3Buf[0] + ((deltaB + lineBufL1[0]) >> 1); if (wavelet->width & 1) { @@ -2104,7 +2125,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) lineBufL1[2] = deltaB; } } else if (wavelet->width & 1) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); lineBufL0[2] = delta; @@ -2122,8 +2143,8 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) lineBufL1 = wavelet->lineBuf[1]; lineBufL2 = wavelet->lineBuf[2]; - for (int32_t i = 0; i < wavelet->width; i++) { - int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); + for (std::int32_t i = 0; i < wavelet->width; i++) { + std::int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); lineBufH2[i] = delta; } @@ -2142,7 +2163,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, uint32_t level) return 0; } -int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) +int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) { if (prevLevel < 0) { return 0; @@ -2158,7 +2179,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) return -1; } - int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; if (wavelet->height > 1) { if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1) || @@ -2167,9 +2188,9 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) return -1; } - int32_t* lineBufL0 = wavelet->lineBuf[0]; - int32_t* lineBufL1 = wavelet->lineBuf[1]; - int32_t* lineBufL2 = wavelet->lineBuf[2]; + std::int32_t* lineBufL0 = wavelet->lineBuf[0]; + std::int32_t* lineBufL1 = wavelet->lineBuf[1]; + std::int32_t* lineBufL2 = wavelet->lineBuf[2]; if (comp->tileFlag & E_HAS_TILES_ON_THE_TOP) { crxHorizontal53(lineBufL0, wavelet->lineBuf[1], wavelet, @@ -2180,8 +2201,8 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) return -1; } - int32_t* band2Buf = wavelet->subband2Buf; - int32_t* band3Buf = wavelet->subband3Buf; + std::int32_t* band2Buf = wavelet->subband2Buf; + std::int32_t* band3Buf = wavelet->subband3Buf; // process L band if (wavelet->width <= 1) { @@ -2197,7 +2218,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) ++band2Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - int32_t delta = + std::int32_t delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); lineBufL2[2] = delta; @@ -2208,7 +2229,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - int32_t delta = + std::int32_t delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); @@ -2216,7 +2237,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) lineBufL2[2] = delta; } } else if (wavelet->width & 1) { - int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + std::int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); lineBufL2[2] = delta; @@ -2226,7 +2247,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) } // process H band - for (int32_t i = 0; i < wavelet->width; i++) { + for (std::int32_t i = 0; i < wavelet->width; i++) { lineBufH0[i] = lineBufL0[i] - ((lineBufL1[i] + lineBufL2[i] + 2) >> 2); } @@ -2248,8 +2269,8 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) return -1; } - int32_t* band0Buf = wavelet->subband0Buf; - int32_t* band1Buf = wavelet->subband1Buf; + std::int32_t* band0Buf = wavelet->subband0Buf; + std::int32_t* band1Buf = wavelet->subband1Buf; // process H band if (wavelet->width <= 1) { @@ -2265,7 +2286,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) ++band0Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; @@ -2275,11 +2296,11 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, int32_t prevLevel) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; } else if (wavelet->width & 1) { - int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; } else { @@ -2307,7 +2328,7 @@ void crxFreeSubbandData(CrxImage* image, CrxPlaneComp* comp) return; } - for (int32_t i = 0; i < image->subbandCount; i++) { + for (std::int32_t i = 0; i < image->subbandCount; i++) { if (comp->subBands[i].bandParam) { free(comp->subBands[i].bandParam); comp->subBands[i].bandParam = nullptr; @@ -2319,15 +2340,15 @@ void crxFreeSubbandData(CrxImage* image, CrxPlaneComp* comp) } void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, - int plane = 0, const int32_t* lineData = nullptr, + int plane = 0, const std::int32_t* lineData = nullptr, int lineLength = 0) { if (lineData) { - uint64_t rawOffset = 4 * img->planeWidth * imageRow + 2 * imageCol; + std::uint64_t rawOffset = 4 * img->planeWidth * imageRow + 2 * imageCol; if (img->encType == 1) { - int32_t maxVal = 1 << (img->nBits - 1); - int32_t minVal = -maxVal; + std::int32_t maxVal = 1 << (img->nBits - 1); + std::int32_t minVal = -maxVal; --maxVal; for (int i = 0; i < lineLength; i++) { @@ -2343,16 +2364,16 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, img->planeBuf[rawOffset + i] = lineData[i]; } } else if (img->nPlanes == 4) { - int32_t median = 1 << (img->nBits - 1); - int32_t maxVal = (1 << img->nBits) - 1; + std::int32_t median = 1 << (img->nBits - 1); + std::int32_t maxVal = (1 << img->nBits) - 1; for (int i = 0; i < lineLength; i++) { img->outBufs[plane][rawOffset + 2 * i] = _constrain(median + lineData[i], 0, maxVal); } } else if (img->nPlanes == 1) { - int32_t maxVal = (1 << img->nBits) - 1; - int32_t median = 1 << (img->nBits - 1); + std::int32_t maxVal = (1 << img->nBits) - 1; + std::int32_t median = 1 << (img->nBits - 1); rawOffset = img->planeWidth * imageRow + imageCol; for (int i = 0; i < lineLength; i++) { @@ -2361,21 +2382,21 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, } } } else if (img->encType == 3 && img->planeBuf) { - int32_t planeSize = img->planeWidth * img->planeHeight; - int16_t* plane0 = img->planeBuf + imageRow * img->planeWidth; - int16_t* plane1 = plane0 + planeSize; - int16_t* plane2 = plane1 + planeSize; - int16_t* plane3 = plane2 + planeSize; + std::int32_t planeSize = img->planeWidth * img->planeHeight; + std::int16_t* plane0 = img->planeBuf + imageRow * img->planeWidth; + std::int16_t* plane1 = plane0 + planeSize; + std::int16_t* plane2 = plane1 + planeSize; + std::int16_t* plane3 = plane2 + planeSize; - int32_t median = 1 << (img->nBits - 1) << 10; - int32_t maxVal = (1 << img->nBits) - 1; - uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; + std::int32_t median = 1 << (img->nBits - 1) << 10; + std::int32_t maxVal = (1 << img->nBits) - 1; + std::uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; // for this stage - all except imageRow is ignored for (int i = 0; i < img->planeWidth; i++) { - int32_t gr = + std::int32_t gr = median + (plane0[i] << 10) - 168 * plane1[i] - 585 * plane3[i]; - int32_t val = 0; + std::int32_t val = 0; if (gr < 0) { gr = -(((_abs(gr) + 512) >> 9) & ~1); @@ -2399,15 +2420,15 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, } } -int crxParamInit(CrxBandParam** param, uint64_t subbandMdatOffset, - uint64_t subbandDataSize, uint32_t subbandWidth, - uint32_t subbandHeight, int32_t supportsPartial, - uint32_t roundedBitsMask, LibRaw_abstract_datastream* input) +int crxParamInit(CrxBandParam** param, std::uint64_t subbandMdatOffset, + std::uint64_t subbandDataSize, std::uint32_t subbandWidth, + std::uint32_t subbandHeight, std::int32_t supportsPartial, + std::uint32_t roundedBitsMask, LibRaw_abstract_datastream* input) { - int32_t progrDataSize = supportsPartial ? 0 : sizeof(int32_t) * subbandWidth; - int32_t paramLength = 2 * subbandWidth + 4; - uint8_t* paramBuf = (uint8_t*)calloc( - 1, sizeof(CrxBandParam) + sizeof(int32_t) * paramLength + progrDataSize); + std::int32_t progrDataSize = supportsPartial ? 0 : sizeof(std::int32_t) * subbandWidth; + std::int32_t paramLength = 2 * subbandWidth + 4; + std::uint8_t* paramBuf = (std::uint8_t*)calloc( + 1, sizeof(CrxBandParam) + sizeof(std::int32_t) * paramLength + progrDataSize); if (!paramBuf) { return -1; @@ -2417,7 +2438,7 @@ int crxParamInit(CrxBandParam** param, uint64_t subbandMdatOffset, paramBuf += sizeof(CrxBandParam); - (*param)->paramData = (int32_t*)paramBuf; + (*param)->paramData = (std::int32_t*)paramBuf; (*param)->nonProgrData = progrDataSize ? (*param)->paramData + paramLength : nullptr; (*param)->subbandWidth = subbandWidth; @@ -2440,25 +2461,25 @@ int crxParamInit(CrxBandParam** param, uint64_t subbandMdatOffset, } int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, - const CrxTile* tile, uint32_t mdatOffset) + const CrxTile* tile, std::uint32_t mdatOffset) { long compDataSize = 0; long waveletDataOffset = 0; long compCoeffDataOffset = 0; - int32_t toSubbands = 3 * img->levels + 1; - int32_t transformWidth = 0; + std::int32_t toSubbands = 3 * img->levels + 1; + std::int32_t transformWidth = 0; CrxSubband* subbands = planeComp->subBands; // calculate sizes - for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + for (std::int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { subbands[subbandNum].bandSize = - subbands[subbandNum].width * sizeof(int32_t); // 4bytes + subbands[subbandNum].width * sizeof(std::int32_t); // 4bytes compDataSize += subbands[subbandNum].bandSize; } if (img->levels) { - int32_t encLevels = img->levels ? img->levels : 1; + std::int32_t encLevels = img->levels ? img->levels : 1; waveletDataOffset = (compDataSize + 7) & ~7; compDataSize = (sizeof(CrxWaveletTransform) * encLevels + waveletDataOffset + 7) & ~7; @@ -2467,26 +2488,26 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, // calc wavelet line buffer sizes (always at one level up from current) for (int level = 0; level < img->levels; ++level) { if (level < img->levels - 1) { - compDataSize += 8 * sizeof(int32_t) * + compDataSize += 8 * sizeof(std::int32_t) * planeComp->subBands[3 * (level + 1) + 2].width; } else { - compDataSize += 8 * sizeof(int32_t) * tile->width; + compDataSize += 8 * sizeof(std::int32_t) * tile->width; } } } // buffer allocation - planeComp->compBuf = (uint8_t*)malloc(compDataSize); + planeComp->compBuf = (std::uint8_t*)malloc(compDataSize); if (!planeComp->compBuf) { return -1; } // subbands buffer and sizes initialisation - uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; - uint8_t* subbandBuf = planeComp->compBuf; + std::uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; + std::uint8_t* subbandBuf = planeComp->compBuf; - for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + for (std::int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { subbands[subbandNum].bandBuf = subbandBuf; subbandBuf += subbands[subbandNum].bandSize; subbands[subbandNum].mdatOffset = @@ -2497,13 +2518,13 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, if (img->levels) { CrxWaveletTransform* waveletTransforms = (CrxWaveletTransform*)(planeComp->compBuf + waveletDataOffset); - int32_t* paramData = (int32_t*)(planeComp->compBuf + compCoeffDataOffset); + std::int32_t* paramData = (std::int32_t*)(planeComp->compBuf + compCoeffDataOffset); planeComp->waveletTransform = waveletTransforms; - waveletTransforms[0].subband0Buf = (int32_t*)subbands->bandBuf; + waveletTransforms[0].subband0Buf = (std::int32_t*)subbands->bandBuf; for (int level = 0; level < img->levels; ++level) { - int32_t band = 3 * level + 1; + std::int32_t band = 3 * level + 1; if (level >= img->levels - 1) { waveletTransforms[level].height = tile->height; @@ -2532,21 +2553,21 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, waveletTransforms[level].curLine = 0; waveletTransforms[level].curH = 0; waveletTransforms[level].fltTapH = 0; - waveletTransforms[level].subband1Buf = (int32_t*)subbands[band].bandBuf; + waveletTransforms[level].subband1Buf = (std::int32_t*)subbands[band].bandBuf; waveletTransforms[level].subband2Buf = - (int32_t*)subbands[band + 1].bandBuf; + (std::int32_t*)subbands[band + 1].bandBuf; waveletTransforms[level].subband3Buf = - (int32_t*)subbands[band + 2].bandBuf; + (std::int32_t*)subbands[band + 2].bandBuf; paramData = waveletTransforms[level].lineBuf[7] + transformWidth; } } // decoding params and bitstream initialisation - for (int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + for (std::int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { if (subbands[subbandNum].dataSize) { - int32_t supportsPartial = 0; - uint32_t roundedBitsMask = 0; + std::int32_t supportsPartial = 0; + std::uint32_t roundedBitsMask = 0; if (planeComp->supportsPartial && subbandNum == 0) { roundedBitsMask = planeComp->roundedBitsMask; @@ -2567,7 +2588,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, } } // namespace -int DCraw::crxDecodePlane(void* p, uint32_t planeNumber) +int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) { CrxImage* img = (CrxImage*)p; int imageRow = 0; @@ -2578,7 +2599,7 @@ int DCraw::crxDecodePlane(void* p, uint32_t planeNumber) for (int tCol = 0; tCol < img->tileCols; tCol++) { CrxTile* tile = img->tiles + tRow * img->tileRows + tCol; CrxPlaneComp* planeComp = tile->comps + planeNumber; - uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; + std::uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; // decode single tile if (crxSetupSubbandData(img, planeComp, tile, tileMdatOffset)) { @@ -2596,7 +2617,7 @@ int DCraw::crxDecodePlane(void* p, uint32_t planeNumber) return -1; } - int32_t* lineData = + std::int32_t* lineData = crxIdwt53FilterGetLine(planeComp, img->levels - 1); crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, lineData, tile->width); @@ -2615,7 +2636,7 @@ int DCraw::crxDecodePlane(void* p, uint32_t planeNumber) return -1; } - int32_t* lineData = (int32_t*)planeComp->subBands->bandBuf; + std::int32_t* lineData = (std::int32_t*)planeComp->subBands->bandBuf; crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, lineData, tile->width); } @@ -2635,14 +2656,14 @@ namespace using crx_data_header_t = DCraw::CanonCR3Data::crx_data_header_t; int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, - CrxPlaneComp* comp, uint8_t** subbandMdatPtr, - uint32_t* mdatSize) + CrxPlaneComp* comp, std::uint8_t** subbandMdatPtr, + std::uint32_t* mdatSize) { CrxSubband* band = comp->subBands + img->subbandCount - 1; // set to last band - uint32_t bandHeight = tile->height; - uint32_t bandWidth = tile->width; - int32_t bandWidthExCoef = 0; - int32_t bandHeightExCoef = 0; + std::uint32_t bandHeight = tile->height; + std::uint32_t bandWidth = tile->width; + std::int32_t bandWidthExCoef = 0; + std::int32_t bandHeightExCoef = 0; if (img->levels) { // Build up subband sequences to crxDecode to a level in a header @@ -2650,21 +2671,21 @@ int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, // Coefficient structure is a bit unclear and convoluted: // 3 levels max - 8 groups (for tile width rounded to 8 bytes) // of 3 band per level 4 sets of coefficients for each - int32_t* rowExCoef = + std::int32_t* rowExCoef = exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->width & 7); - int32_t* colExCoef = + std::int32_t* colExCoef = exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->height & 7); for (int level = 0; level < img->levels; ++level) { - int32_t widthOddPixel = bandWidth & 1; - int32_t heightOddPixel = bandHeight & 1; + std::int32_t widthOddPixel = bandWidth & 1; + std::int32_t heightOddPixel = bandHeight & 1; bandWidth = (widthOddPixel + bandWidth) >> 1; bandHeight = (heightOddPixel + bandHeight) >> 1; - int32_t bandWidthExCoef0 = 0; - int32_t bandWidthExCoef1 = 0; - int32_t bandHeightExCoef0 = 0; - int32_t bandHeightExCoef1 = 0; + std::int32_t bandWidthExCoef0 = 0; + std::int32_t bandWidthExCoef1 = 0; + std::int32_t bandHeightExCoef0 = 0; + std::int32_t bandHeightExCoef1 = 0; if (tile->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { bandWidthExCoef0 = rowExCoef[0]; @@ -2720,7 +2741,7 @@ int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, return 0; } - int32_t subbandOffset = 0; + std::int32_t subbandOffset = 0; band = comp->subBands; for (unsigned int curSubband = 0; curSubband < img->subbandCount; curSubband++, band++) { @@ -2732,8 +2753,8 @@ int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, return -1; } - uint32_t bitData = sgetn(4, *subbandMdatPtr + 8); - uint32_t subbandSize = sgetn(4, *subbandMdatPtr + 4); + std::uint32_t bitData = sgetn(4, *subbandMdatPtr + 8); + std::uint32_t subbandSize = sgetn(4, *subbandMdatPtr + 4); if (curSubband != bitData >> 28) { band->dataSize = subbandSize; @@ -2758,8 +2779,8 @@ int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, return 0; } -int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, uint8_t* mdatPtr, - uint32_t mdatSize) +int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mdatPtr, + std::uint32_t mdatSize) { unsigned int nTiles = img->tileRows * img->tileCols; @@ -2855,9 +2876,9 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, uint8_t* mdatPtr, } } - uint32_t tileOffset = 0; - uint32_t dataSize = mdatSize; - uint8_t* dataPtr = mdatPtr; + std::uint32_t tileOffset = 0; + std::uint32_t dataSize = mdatSize; + std::uint8_t* dataPtr = mdatPtr; CrxTile* tile = img->tiles; for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { @@ -2878,12 +2899,12 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, uint8_t* mdatPtr, tile->tileSize = sgetn(4, dataPtr + 4); tile->dataOffset = tileOffset; - int32_t hdrExtraBytes = sgetn(2, dataPtr + 2) - 8; + std::int32_t hdrExtraBytes = sgetn(2, dataPtr + 2) - 8; tileOffset += tile->tileSize; dataPtr += hdrExtraBytes + 0xC; dataSize -= hdrExtraBytes; - uint32_t compOffset = 0; + std::uint32_t compOffset = 0; CrxPlaneComp* comp = tile->comps; for (int compNum = 0; compNum < img->nPlanes; compNum++, comp++) { @@ -2901,7 +2922,7 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, uint8_t* mdatPtr, comp->compSize = sgetn(4, dataPtr + 4); - int32_t compHdrRoundedBits = (dataPtr[8] >> 1) & 3; + std::int32_t compHdrRoundedBits = (dataPtr[8] >> 1) & 3; comp->supportsPartial = (dataPtr[8] & 8) != 0; comp->dataOffset = compOffset; @@ -2930,9 +2951,9 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, uint8_t* mdatPtr, return 0; } -int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, int16_t* outBuf, - uint64_t mdatOffset, uint32_t mdatSize, - uint8_t* mdatHdrPtr) +int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, std::int16_t* outBuf, + std::uint64_t mdatOffset, std::uint32_t mdatSize, + std::uint8_t* mdatHdrPtr) { int IncrBitTable[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0 @@ -2975,7 +2996,7 @@ int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, int16_t* outBuf, // left as is. if (img->encType == 3 && img->nPlanes == 4 && img->nBits > 8) { img->planeBuf = - (int16_t*)malloc(img->planeHeight * img->planeWidth * img->nPlanes * + (std::int16_t*)malloc(img->planeHeight * img->planeWidth * img->nPlanes * ((img->samplePrecision + 7) >> 3)); if (!img->planeBuf) { @@ -2983,7 +3004,7 @@ int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, int16_t* outBuf, } } - int32_t rowSize = 2 * img->planeWidth; + std::int32_t rowSize = 2 * img->planeWidth; if (img->nPlanes == 1) { img->outBufs[0] = outBuf; @@ -3041,9 +3062,9 @@ int crxFreeImageData(CrxImage* img) int nTiles = img->tileRows * img->tileCols; if (img->tiles) { - for (int32_t curTile = 0; curTile < nTiles; curTile++, tile++) { + for (std::int32_t curTile = 0; curTile < nTiles; curTile++, tile++) { if (tile[curTile].comps) { - for (int32_t curPlane = 0; curPlane < img->nPlanes; curPlane++) { + for (std::int32_t curPlane = 0; curPlane < img->nPlanes; curPlane++) { crxFreeSubbandData(img, tile[curTile].comps + curPlane); } } @@ -3068,11 +3089,11 @@ void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) int results[4]; // nPlanes is always <= 4 #pragma omp parallel for - for (int32_t plane = 0; plane < nPlanes; ++plane) { + for (std::int32_t plane = 0; plane < nPlanes; ++plane) { results[plane] = crxDecodePlane(img, plane); } - for (int32_t plane = 0; plane < nPlanes; ++plane) { + for (std::int32_t plane = 0; plane < nPlanes; ++plane) { if (results[plane]) { derror(); } @@ -3080,7 +3101,7 @@ void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) #else - for (int32_t plane = 0; plane < nPlanes; ++plane) { + for (std::int32_t plane = 0; plane < nPlanes; ++plane) { if (crxDecodePlane(img, plane)) { derror(); } @@ -3110,7 +3131,7 @@ void DCraw::crxLoadRaw() CrxImage img; if (RT_canon_CR3_data.crx_track_selected >= - LIBRAW_CRXTRACKS_MAXCOUNT) { + RT_canon_CR3_data.CRXTRACKS_MAXCOUNT) { derror(); } @@ -3131,7 +3152,7 @@ void DCraw::crxLoadRaw() // /*imgdata.color.*/maximum = (1 << hdr.nBits) - 1; - uint8_t* hdrBuf = (uint8_t*)malloc(hdr.mdatHdrSize); + std::uint8_t* hdrBuf = (std::uint8_t*)malloc(hdr.mdatHdrSize); // read image header #ifdef _OPENMP @@ -3150,7 +3171,7 @@ void DCraw::crxLoadRaw() } // parse and setup the image data - if (crxSetupImageData(&hdr, &img, (int16_t*)raw_image, + if (crxSetupImageData(&hdr, &img, (std::int16_t*)raw_image, hdr.MediaOffset /*data_offset*/, hdr.MediaSize /*RT_canon_CR3_data.data_size*/, hdrBuf)) { derror(); @@ -3169,7 +3190,7 @@ void DCraw::crxLoadRaw() int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) { - if (nTrack >= LIBRAW_CRXTRACKS_MAXCOUNT) { + if (nTrack >= RT_canon_CR3_data.CRXTRACKS_MAXCOUNT) { return -1; } @@ -3243,4 +3264,3 @@ int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) #undef _min #undef _constrain #undef libraw_inline -#undef LIBRAW_CRXTRACKS_MAXCOUNT From e89b6be76e08f8f3e6aaa85eca7ed9d29d19bf7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Tue, 26 Nov 2019 09:52:51 +0100 Subject: [PATCH 08/20] `DCraw::parseCR3()` cleanup --- rtengine/canon_cr3_decoder.cc | 170 ++++++++++++++++++---------------- 1 file changed, 90 insertions(+), 80 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 609f0c849..3fe221097 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -280,12 +280,17 @@ int DCraw::parseCR3( {"uuid", 3}, }; - const char sHandlerType[5][5] = {"unk.", "soun", "vide", "hint", "meta"}; + const char sHandlerType[5][5] = { + "unk.", + "soun", + "vide", + "hint", + "meta" + }; - unsigned int c; int err = 0; - ushort tL; // Atom length represented in 4 or 8 bytes + unsigned short tL; // Atom length represented in 4 or 8 bytes char nmAtom[5]; // Atom name unsigned long long oAtom; unsigned long long szAtom; // Atom offset and Atom size @@ -297,19 +302,19 @@ int DCraw::parseCR3( uchar CMP1[36]; char HandlerType[5]; char MediaFormatID[5]; -// unsigned ImageWidth, ImageHeight; +// unsigned int ImageWidth, ImageHeight; long relpos_inDir; long relpos_inBox; - unsigned szItem; - unsigned Tag; - unsigned lTag; - ushort tItem; + unsigned int szItem; + unsigned int Tag; + unsigned int lTag; + unsigned short tItem; nmAtom[0] = MediaFormatID[0] = nmAtom[4] = MediaFormatID[4] = '\0'; - strcpy(HandlerType, sHandlerType[0]); + strncpy(HandlerType, sHandlerType[0], sizeof(HandlerType)); // ImageWidth = ImageHeight = 0U; oAtom = oAtomList; - nesting++; + ++nesting; if (nesting > 31) { return -14; // too deep nesting @@ -329,20 +334,20 @@ int DCraw::parseCR3( || get4() != 0x00000008; }; - while ((oAtom + 8ULL) <= (oAtomList + szAtomList)) { - lHdr = 0ULL; + while ((oAtom + 8) <= (oAtomList + szAtomList)) { + lHdr = 0U; err = 0; - order = 0x4d4d; + order = 0x4D4D; fseek(ifp, oAtom, SEEK_SET); szAtom = get4(); - for (c = 0; c < 4; c++) { + for (unsigned int c = 0; c < 4; ++c) { nmAtom[c] = AtomNameStack[nesting * 4 + c] = fgetc(ifp); } AtomNameStack[(nesting + 1) * 4] = '\0'; tL = 4; AtomType = 0; - for (c = 0; c < sizeof AtomNamesList / sizeof * AtomNamesList; c++) { + for (unsigned int c = 0; c < sizeof(AtomNamesList) / sizeof(*AtomNamesList); ++c) { if (!strcmp(nmAtom, AtomNamesList[c].AtomName)) { AtomType = AtomNamesList[c].AtomType; break; @@ -353,28 +358,28 @@ int DCraw::parseCR3( err = 1; } - if (szAtom == 0ULL) { + if (szAtom == 0) { if (nesting != 0) { err = -2; goto fin; } szAtom = szAtomList - oAtom; - oAtomContent = oAtom + 8ULL; - szAtomContent = szAtom - 8ULL; - } else if (szAtom == 1ULL) { - if ((oAtom + 16ULL) > (oAtomList + szAtomList)) { + oAtomContent = oAtom + 8; + szAtomContent = szAtom - 8; + } else if (szAtom == 1) { + if ((oAtom + 16) > (oAtomList + szAtomList)) { err = -3; goto fin; } tL = 8; - szAtom = (((unsigned long long)get4()) << 32) | get4(); - oAtomContent = oAtom + 16ULL; - szAtomContent = szAtom - 16ULL; + szAtom = (static_cast(get4()) << 32) | get4(); + oAtomContent = oAtom + 16; + szAtomContent = szAtom - 16; } else { - oAtomContent = oAtom + 8ULL; - szAtomContent = szAtom - 8ULL; + oAtomContent = oAtom + 8; + szAtomContent = szAtom - 8; } if (!strcmp(nmAtom, "trak")) { @@ -387,7 +392,7 @@ int DCraw::parseCR3( } if (!strcmp(AtomNameStack, "moovuuid")) { - lHdr = 16ULL; + lHdr = 16; fread(UIID, 1, lHdr, ifp); if (!strncmp(UIID, UIID_Canon, lHdr)) { @@ -396,12 +401,12 @@ int DCraw::parseCR3( fseek(ifp, -lHdr, SEEK_CUR); } } else if (!strcmp(AtomNameStack, "moovuuidCCTP")) { - lHdr = 12ULL; + lHdr = 12; } else if (!strcmp(AtomNameStack, "moovuuidCMT1")) { - short q_order = order; + const short q_order = order; order = get2(); - if ((tL != 4) || is_bad_header()) { + if (tL != 4 || is_bad_header()) { err = -4; goto fin; } @@ -409,10 +414,10 @@ int DCraw::parseCR3( parse_tiff_ifd(oAtomContent); order = q_order; } else if (!strcmp(AtomNameStack, "moovuuidCMT2")) { - short q_order = order; + const short q_order = order; order = get2(); - if ((tL != 4) || is_bad_header()) { + if (tL != 4 || is_bad_header()) { err = -5; goto fin; } @@ -420,10 +425,10 @@ int DCraw::parseCR3( parse_exif(oAtomContent); order = q_order; } else if (!strcmp(AtomNameStack, "moovuuidCMT3")) { - short q_order = order; + const short q_order = order; order = get2(); - if ((tL != 4) || is_bad_header()) { + if (tL != 4 || is_bad_header()) { err = -6; goto fin; } @@ -432,26 +437,26 @@ int DCraw::parseCR3( parse_makernote(oAtomContent, 0); order = q_order; } else if (!strcmp(AtomNameStack, "moovuuidCMT4")) { - short q_order = order; + const short q_order = order; order = get2(); - if ((tL != 4) || is_bad_header()) { + if (tL != 4 || is_bad_header()) { err = -6; goto fin; } - std::int64_t off = ftell(ifp); + const std::int64_t off = ftell(ifp); // FIXME: ftell() returns int parse_gps(oAtomContent); - fseek(ifp, off, SEEK_SET); + fseek(ifp, off, SEEK_SET); // FIXME: fseek() takes int offset // parse_gps_libraw(oAtomContent); order = q_order; } else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) { - fseek(ifp, 8L, SEEK_CUR); - for (c = 0; c < 4; c++) { + fseek(ifp, 8, SEEK_CUR); + for (unsigned int c = 0; c < 4; ++c) { HandlerType[c] = fgetc(ifp); } - for (c = 1; c < sizeof sHandlerType / sizeof * sHandlerType; c++) { + for (unsigned int c = 1; c < sizeof(sHandlerType) / sizeof(*sHandlerType); ++c) { if (!strcmp(HandlerType, sHandlerType[c])) { TrackType = c; break; @@ -466,13 +471,13 @@ int DCraw::parseCR3( goto fin; } - for (c = 0; c < 4; c++) { + for (unsigned int c = 0; c < 4; ++c) { MediaFormatID[c] = fgetc(ifp); } - if ((TrackType == 2) && (!strcmp(MediaFormatID, "CRAW"))) { + if (TrackType == 2 && !strcmp(MediaFormatID, "CRAW")) { if (szAtomContent >= 44) { - fseek(ifp, 24L, SEEK_CUR); + fseek(ifp, 24, SEEK_CUR); } else { err = -8; goto fin; @@ -482,8 +487,6 @@ int DCraw::parseCR3( lHdr = 0; } -#define current_track RT_canon_CR3_data.crx_header[nTrack] - /*ImageWidth = */ get2(); /*ImageHeight = */ get2(); } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) { @@ -497,69 +500,81 @@ int DCraw::parseCR3( } if (!crxParseImageHeader(CMP1, nTrack)) { - current_track.MediaType = 1; + RT_canon_CR3_data.crx_header[nTrack].MediaType = 1; } } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWJPEG")) { - current_track.MediaType = 2; + RT_canon_CR3_data.crx_header[nTrack].MediaType = 2; } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsz")) { if (szAtomContent == 12) { - fseek(ifp, 4L, SEEK_CUR); + fseek(ifp, 4, SEEK_CUR); } else if (szAtomContent == 16) { - fseek(ifp, 12L, SEEK_CUR); + fseek(ifp, 12, SEEK_CUR); } else { err = -9; goto fin; } - current_track.MediaSize = get4(); + RT_canon_CR3_data.crx_header[nTrack].MediaSize = get4(); } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblco64")) { if (szAtomContent == 16) { - fseek(ifp, 8L, SEEK_CUR); + fseek(ifp, 8, SEEK_CUR); } else { err = -10; goto fin; } - current_track.MediaOffset = (((unsigned long long)get4()) << 32) | get4(); + RT_canon_CR3_data.crx_header[nTrack].MediaOffset = (static_cast(get4()) << 32) | get4(); } - if (current_track.MediaSize && current_track.MediaOffset && - ((oAtom + szAtom) >= (oAtomList + szAtomList)) && - !strncmp(AtomNameStack, "moovtrakmdiaminfstbl", 20)) { - if ((TrackType == 4) && (!strcmp(MediaFormatID, "CTMD"))) { + if ( + RT_canon_CR3_data.crx_header[nTrack].MediaSize + && RT_canon_CR3_data.crx_header[nTrack].MediaOffset + && oAtom + szAtom >= oAtomList + szAtomList + && !strncmp(AtomNameStack, "moovtrakmdiaminfstbl", 20) + ) { + if (TrackType == 4 && !strcmp(MediaFormatID, "CTMD")) { order = 0x4949; - relpos_inDir = 0L; + relpos_inDir = 0; - while (relpos_inDir + 6 < current_track.MediaSize) { - fseek(ifp, current_track.MediaOffset + relpos_inDir, SEEK_SET); + while (relpos_inDir + 6 < RT_canon_CR3_data.crx_header[nTrack].MediaSize) { + fseek(ifp, RT_canon_CR3_data.crx_header[nTrack].MediaOffset + relpos_inDir, SEEK_SET); szItem = get4(); tItem = get2(); - if ((relpos_inDir + szItem) > current_track.MediaSize) { + if ((relpos_inDir + szItem) > RT_canon_CR3_data.crx_header[nTrack].MediaSize) { err = -11; goto fin; } - if ((tItem == 7) || (tItem == 8) || (tItem == 9)) { - relpos_inBox = relpos_inDir + 12L; + if ( + tItem == 7 + || tItem == 8 + || tItem == 9 + ) { + relpos_inBox = relpos_inDir + 12; while (relpos_inBox + 8 < relpos_inDir + szItem) { - fseek(ifp, current_track.MediaOffset + relpos_inBox, SEEK_SET); + fseek(ifp, RT_canon_CR3_data.crx_header[nTrack].MediaOffset + relpos_inBox, SEEK_SET); lTag = get4(); Tag = get4(); if (lTag < 8) { err = -12; goto fin; - } else if ((relpos_inBox + lTag) > (relpos_inDir + szItem)) { + } else if (relpos_inBox + lTag > relpos_inDir + szItem) { err = -11; goto fin; } - if ((Tag == 0x927c) && ((tItem == 7) || (tItem == 8))) { - fseek(ifp, current_track.MediaOffset + relpos_inBox + 8L, - SEEK_SET); - short q_order = order; + if ( + Tag == 0x927C + && ( + tItem == 7 + || tItem == 8 + ) + ) { + fseek(ifp, RT_canon_CR3_data.crx_header[nTrack].MediaOffset + relpos_inBox + 8, SEEK_SET); + const short q_order = order; order = get2(); if (is_bad_header()) { @@ -567,10 +582,9 @@ int DCraw::parseCR3( goto fin; } - fseek(ifp, -8L, SEEK_CUR); + fseek(ifp, -8, SEEK_CUR); RT_canon_CR3_data.CR3_CTMDtag = 1; - parse_makernote(current_track.MediaOffset + relpos_inBox + 8, - 0); + parse_makernote(RT_canon_CR3_data.crx_header[nTrack].MediaOffset + relpos_inBox + 8, 0); RT_canon_CR3_data.CR3_CTMDtag = 0; order = q_order; } @@ -582,15 +596,12 @@ int DCraw::parseCR3( relpos_inDir += szItem; } - order = 0x4d4d; + order = 0x4D4D; } } -#undef current_track - if (AtomType == 1) { - err = parseCR3(oAtomContent + lHdr, szAtomContent - lHdr, nesting, - AtomNameStack, nTrack, TrackType); + err = parseCR3(oAtomContent + lHdr, szAtomContent - lHdr, nesting, AtomNameStack, nTrack, TrackType); if (err) { goto fin; @@ -601,7 +612,7 @@ int DCraw::parseCR3( } fin: - nesting--; + --nesting; if (nesting >= 0) { AtomNameStack[nesting * 4] = '\0'; @@ -610,7 +621,6 @@ fin: order = s_order; return err; } -#undef bad_hdr // ----------------------------------------------------------------------------- @@ -633,9 +643,9 @@ fin: namespace { -static unsigned sgetn(int n, unsigned char* s) +static unsigned int sgetn(int n, unsigned char* s) { - unsigned result = 0; + unsigned int result = 0; while (n-- > 0) { result = (result << 8) | (*s++); From 4f153a6d72b0a09ed4e58df230a05695ea75805d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 27 Nov 2019 09:49:13 +0100 Subject: [PATCH 09/20] Fix warnings --- rtengine/canon_cr3_decoder.cc | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 3fe221097..4698f72c0 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -303,8 +303,8 @@ int DCraw::parseCR3( char HandlerType[5]; char MediaFormatID[5]; // unsigned int ImageWidth, ImageHeight; - long relpos_inDir; - long relpos_inBox; + unsigned long relpos_inDir; + unsigned long relpos_inBox; unsigned int szItem; unsigned int Tag; unsigned int lTag; @@ -2663,9 +2663,10 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) namespace { + using crx_data_header_t = DCraw::CanonCR3Data::crx_data_header_t; -int crxReadSubbandHeaders(crx_data_header_t* hdr, CrxImage* img, CrxTile* tile, +int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, CrxPlaneComp* comp, std::uint8_t** subbandMdatPtr, std::uint32_t* mdatSize) { @@ -2952,7 +2953,7 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda comp->roundedBitsMask = 1 << (compHdrRoundedBits - 1); } - if (crxReadSubbandHeaders(hdr, img, tile, comp, &dataPtr, &dataSize)) { + if (crxReadSubbandHeaders(img, tile, comp, &dataPtr, &dataSize)) { return -1; } } @@ -2980,9 +2981,7 @@ int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, std::int16_t* outBu img->tileCols = (img->planeWidth + hdr->tileWidth - 1) / hdr->tileWidth; img->tileRows = (img->planeHeight + hdr->tileHeight - 1) / hdr->tileHeight; - if (img->tileCols > 0xFF || img->tileRows > 0xFF || - img->planeWidth - hdr->tileWidth * (img->tileCols - 1) < 0x16 || - img->planeHeight - hdr->tileHeight * (img->tileRows - 1) < 0x16) { + if (img->planeWidth - hdr->tileWidth * (img->tileCols - 1) < 0x16 || img->planeHeight - hdr->tileHeight * (img->tileRows - 1) < 0x16) { return -1; } From 97960f335f362264f5aa79f15a896678752bbfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 27 Nov 2019 10:32:43 +0100 Subject: [PATCH 10/20] Fix silly error and with it the Clang build --- rtengine/canon_cr3_decoder.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 4698f72c0..f32258469 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -76,7 +76,7 @@ void DCraw::parse_canon_cr3() void DCraw::selectCRXTrack(unsigned short maxTrack) { - std::int64_t bitcounts[RT_canon_CR3_data.CRXTRACKS_MAXCOUNT] = {}; + std::int64_t bitcounts[CanonCR3Data::CRXTRACKS_MAXCOUNT] = {}; std::int64_t maxbitcount = 0; std::uint32_t maxjpegbytes = 0; From 85abe54a1638409c563f6bde93cf66fe584049ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Thu, 28 Nov 2019 08:54:38 +0100 Subject: [PATCH 11/20] Replace defines --- rtengine/canon_cr3_decoder.cc | 117 ++++++++++++++-------------------- 1 file changed, 48 insertions(+), 69 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index f32258469..0d284f68b 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -53,6 +53,8 @@ #include #include +#include +#include #include #include "dcraw.h" @@ -624,26 +626,10 @@ fin: // ----------------------------------------------------------------------------- -#ifdef _abs -#undef _abs -#undef _min -#undef _constrain -#endif -#define _abs(x) ((x) < 0 ? -(x) : (x)) -#define _min(a, b) ((a) < (b) ? (a) : (b)) -#define _constrain(x, l, u) ((x) < (l) ? (l) : ((x) > (u) ? (u) : (x))) - -#if defined (__clang__) || defined (__GNUG__) -#define libraw_inline inline __attribute__((always_inline)) -#elif defined (_MSC_VER) && _MSC_VER > 1400 -#define libraw_inline __forceinline -#else -#define libraw_inline inline -#endif - namespace { -static unsigned int sgetn(int n, unsigned char* s) + +unsigned int sgetn(int n, unsigned char* s) { unsigned int result = 0; @@ -655,12 +641,11 @@ static unsigned int sgetn(int n, unsigned char* s) } // this should be divisible by 4 -#define CRX_BUF_SIZE 0x10000 +constexpr std::uint64_t CRX_BUF_SIZE = 0x10000; + #if !defined (_WIN32) || (defined (__GNUC__) && !defined (__INTRINSIC_SPECIAL__BitScanReverse)) /* __INTRINSIC_SPECIAL__BitScanReverse found in MinGW32-W64 v7.30 headers, may be there is a better solution? */ -using DWORD = std::uint32_t; -using byte = std::uint8_t; -libraw_inline void _BitScanReverse(DWORD* Index, unsigned long Mask) +inline void _BitScanReverse(std::uint32_t* Index, unsigned long Mask) { *Index = sizeof(unsigned long) * 8 - 1 - __builtin_clzl(Mask); } @@ -674,8 +659,6 @@ std::uint32_t _byteswap_ulong(std::uint32_t x) } #endif -#define LIBRAW_EXCEPTION_IO_EOF std::exception() - struct LibRaw_abstract_datastream { IMFILE* ifp; @@ -751,7 +734,7 @@ struct CrxSubband { }; struct CrxPlaneComp { - byte* compBuf; + std::uint8_t* compBuf; CrxSubband* subBands; CrxWaveletTransform* waveletTransform; std::int8_t compNumber; @@ -798,7 +781,7 @@ enum TileFlags { E_HAS_TILES_ON_THE_TOP = 8 }; -std::int32_t exCoefNumTbl[0x120] = { +const std::int32_t exCoefNumTbl[0x120] = { // level 1 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @@ -818,18 +801,20 @@ std::int32_t exCoefNumTbl[0x120] = { 8, 8, 2, 1, 4, 3, 1, 1, 1, 1, 1, 1, 8, 7, 1, 1, 3, 3, 1, 1, 1, 1 }; -std::uint32_t JS[32] = {1, 1, 1, 1, 2, 2, 2, 2, - 4, 4, 4, 4, 8, 8, 8, 8, - 0x10, 0x10, 0x20, 0x20, 0x40, 0x40, 0x80, 0x80, - 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 - }; +const std::uint32_t JS[32] = { + 0x0001, 0x0001, 0x0001, 0x0001, 0x0002, 0x0002, 0x0002, 0x0002, + 0x0004, 0x0004, 0x0004, 0x0004, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0010, 0x0010, 0x0020, 0x0020, 0x0040, 0x0040, 0x0080, 0x0080, + 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000 +}; -std::uint32_t J[32] = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, - 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, - 7, 7, 8, 9, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F - }; +const std::uint32_t J[32] = { + 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, + 0x2, 0x3, 0x3, 0x3, 0x3, 0x4, 0x4, 0x5, 0x5, 0x6, 0x6, + 0x7, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF +}; -static inline void crxFillBuffer(CrxBitstream* bitStrm) +inline void crxFillBuffer(CrxBitstream* bitStrm) { if (bitStrm->curPos >= bitStrm->curBufSize && bitStrm->mdatSize) { bitStrm->curPos = 0; @@ -842,14 +827,13 @@ static inline void crxFillBuffer(CrxBitstream* bitStrm) bitStrm->input->lock(); #endif bitStrm->input->seek(bitStrm->curBufOffset, SEEK_SET); - bitStrm->curBufSize = bitStrm->input->read( - bitStrm->mdatBuf, 1, _min(bitStrm->mdatSize, CRX_BUF_SIZE)); + bitStrm->curBufSize = bitStrm->input->read(bitStrm->mdatBuf, 1, std::min(bitStrm->mdatSize, CRX_BUF_SIZE)); #ifndef _OPENMP bitStrm->input->unlock(); #endif if (bitStrm->curBufSize < 1) { // nothing read - throw LIBRAW_EXCEPTION_IO_EOF; + throw std::exception(); } bitStrm->mdatSize -= bitStrm->curBufSize; @@ -857,7 +841,7 @@ static inline void crxFillBuffer(CrxBitstream* bitStrm) } } -libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) +inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) { // std::uint32_t bitData = bitStrm->bitData; std::uint32_t nonZeroBit = 0; @@ -865,7 +849,7 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) std::int32_t result = 0; if (bitStrm->bitData) { - _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)bitStrm->bitData); + _BitScanReverse((std::uint32_t*)&nonZeroBit, (std::uint32_t)bitStrm->bitData); result = 31 - nonZeroBit; bitStrm->bitData <<= 32 - nonZeroBit; bitStrm->bitsLeft -= 32 - nonZeroBit; @@ -880,7 +864,7 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) crxFillBuffer(bitStrm); if (nextData) { - _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)nextData); + _BitScanReverse((std::uint32_t*)&nonZeroBit, (std::uint32_t)nextData); result = bitsLeft + 31 - nonZeroBit; bitStrm->bitData = nextData << (32 - nonZeroBit); bitStrm->bitsLeft = nonZeroBit; @@ -904,7 +888,7 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) bitsLeft += 8; } - _BitScanReverse((DWORD*)&nonZeroBit, (DWORD)nextData); + _BitScanReverse((std::uint32_t*)&nonZeroBit, (std::uint32_t)nextData); result = (std::uint32_t)(bitsLeft + 7 - nonZeroBit); bitStrm->bitData = nextData << (32 - nonZeroBit); bitStrm->bitsLeft = nonZeroBit; @@ -913,7 +897,7 @@ libraw_inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) return result; } -libraw_inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) +inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) { int bitsLeft = bitStrm->bitsLeft; std::uint32_t bitData = bitStrm->bitData; @@ -953,7 +937,7 @@ libraw_inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) return result; } -libraw_inline int crxPredictKParameter(std::int32_t prevK, std::int32_t bitCode, +inline int crxPredictKParameter(std::int32_t prevK, std::int32_t bitCode, std::int32_t maxVal = 0) { std::int32_t newKParam = prevK - (bitCode < (1 << prevK >> 1)) + @@ -962,7 +946,7 @@ libraw_inline int crxPredictKParameter(std::int32_t prevK, std::int32_t bitCode, return !maxVal || newKParam < maxVal ? newKParam : maxVal; } -libraw_inline void crxDecodeSymbolL1(CrxBandParam* param, +inline void crxDecodeSymbolL1(CrxBandParam* param, std::int32_t doMedianPrediction, std::int32_t notEOL = 0) { @@ -997,7 +981,7 @@ libraw_inline void crxDecodeSymbolL1(CrxBandParam* param, // for not end of the line - use one symbol ahead to estimate next K if (notEOL) { std::int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; - bitCode = (bitCode + _abs(nextDelta)) >> 1; + bitCode = (bitCode + std::abs(nextDelta)) >> 1; ++param->lineBuf0; } @@ -1081,7 +1065,7 @@ int crxDecodeLine(CrxBandParam* param) return 0; } -libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, +inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, std::int32_t doSym = 1, std::int32_t doCode = 1) { @@ -1123,7 +1107,7 @@ libraw_inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, } param->kParam = crxPredictKParameter(param->kParam, - (bitCode + 2 * _abs(code)) >> 1, 15); + (bitCode + 2 * std::abs(code)) >> 1, 15); } else { param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); } @@ -1140,11 +1124,11 @@ int crxDecodeLineRounded(CrxBandParam* param) std::int32_t length = param->subbandWidth; for (; length > 1; --length) { - if (_abs(param->lineBuf0[2] - param->lineBuf0[1]) > param->roundedBitsMask) { + if (std::abs(param->lineBuf0[2] - param->lineBuf0[1]) > param->roundedBitsMask) { crxDecodeSymbolL1Rounded(param); ++param->lineBuf0; valueReached = 1; - } else if (valueReached || _abs(param->lineBuf0[0] - param->lineBuf1[0]) > + } else if (valueReached || std::abs(param->lineBuf0[0] - param->lineBuf1[0]) > param->roundedBitsMask) { crxDecodeSymbolL1Rounded(param); ++param->lineBuf0; @@ -1201,7 +1185,7 @@ int crxDecodeLineRounded(CrxBandParam* param) if (length > 1) { crxDecodeSymbolL1Rounded(param, 0); ++param->lineBuf0; - valueReached = _abs(param->lineBuf0[1] - param->lineBuf0[0]) > + valueReached = std::abs(param->lineBuf0[1] - param->lineBuf0[0]) > param->roundedBitsMask; } else if (length == 1) { crxDecodeSymbolL1Rounded(param, 0, 0); @@ -1458,7 +1442,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) // read the line from bitstream for (; length > 1; --length) { - if (_abs(param->lineBuf1[0]) > param->roundedBitsMask) { + if (std::abs(param->lineBuf1[0]) > param->roundedBitsMask) { param->lineBuf1[1] = param->lineBuf1[0]; } else { int nSyms = 0; @@ -2363,7 +2347,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, for (int i = 0; i < lineLength; i++) { img->outBufs[plane][rawOffset + 2 * i] = - _constrain(lineData[i], minVal, maxVal); + rtengine::LIM(lineData[i], minVal, maxVal); } } else if (img->encType == 3) { // copy to intermediate planeBuf @@ -2379,7 +2363,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, for (int i = 0; i < lineLength; i++) { img->outBufs[plane][rawOffset + 2 * i] = - _constrain(median + lineData[i], 0, maxVal); + rtengine::LIM(median + lineData[i], 0, maxVal); } } else if (img->nPlanes == 1) { std::int32_t maxVal = (1 << img->nBits) - 1; @@ -2388,7 +2372,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, for (int i = 0; i < lineLength; i++) { img->outBufs[0][rawOffset + i] = - _constrain(median + lineData[i], 0, maxVal); + rtengine::LIM(median + lineData[i], 0, maxVal); } } } else if (img->encType == 3 && img->planeBuf) { @@ -2409,23 +2393,23 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, std::int32_t val = 0; if (gr < 0) { - gr = -(((_abs(gr) + 512) >> 9) & ~1); + gr = -(((std::abs(gr) + 512) >> 9) & ~1); } else { - gr = ((_abs(gr) + 512) >> 9) & ~1; + gr = ((std::abs(gr) + 512) >> 9) & ~1; } // Essentially R = round(median + P0 + 1.474*P3) val = (median + (plane0[i] << 10) + 1510 * plane3[i] + 512) >> 10; - img->outBufs[0][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + img->outBufs[0][rawLineOffset + 2 * i] = rtengine::LIM(val, 0, maxVal); // Essentially G1 = round(median + P0 + P2 - 0.164*P1 - 0.571*P3) val = (plane2[i] + gr + 1) >> 1; - img->outBufs[1][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + img->outBufs[1][rawLineOffset + 2 * i] = rtengine::LIM(val, 0, maxVal); // Essentially G1 = round(median + P0 - P2 - 0.164*P1 - 0.571*P3) val = (gr - plane2[i] + 1) >> 1; - img->outBufs[2][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + img->outBufs[2][rawLineOffset + 2 * i] = rtengine::LIM(val, 0, maxVal); // Essentially B = round(median + P0 + 1.881*P1) val = (median + (plane0[i] << 10) + 1927 * plane1[i] + 512) >> 10; - img->outBufs[3][rawLineOffset + 2 * i] = _constrain(val, 0, maxVal); + img->outBufs[3][rawLineOffset + 2 * i] = rtengine::LIM(val, 0, maxVal); } } } @@ -2682,9 +2666,9 @@ int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, // Coefficient structure is a bit unclear and convoluted: // 3 levels max - 8 groups (for tile width rounded to 8 bytes) // of 3 band per level 4 sets of coefficients for each - std::int32_t* rowExCoef = + const std::int32_t* rowExCoef = exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->width & 7); - std::int32_t* colExCoef = + const std::int32_t* colExCoef = exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->height & 7); for (int level = 0; level < img->levels; ++level) { @@ -3268,8 +3252,3 @@ int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) return 0; } - -#undef _abs -#undef _min -#undef _constrain -#undef libraw_inline From 4529999f493d41458889f083eaf0a3d3a3cd3e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Thu, 28 Nov 2019 09:11:56 +0100 Subject: [PATCH 12/20] C++-style casts (clang-tidy) --- rtengine/canon_cr3_decoder.cc | 86 +++++++++++++++++------------------ 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 0d284f68b..a64a83010 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -349,9 +349,9 @@ int DCraw::parseCR3( tL = 4; AtomType = 0; - for (unsigned int c = 0; c < sizeof(AtomNamesList) / sizeof(*AtomNamesList); ++c) { - if (!strcmp(nmAtom, AtomNamesList[c].AtomName)) { - AtomType = AtomNamesList[c].AtomType; + for (const auto& atom : AtomNamesList) { + if (!strcmp(nmAtom, atom.AtomName)) { + AtomType = atom.AtomType; break; } } @@ -849,7 +849,7 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) std::int32_t result = 0; if (bitStrm->bitData) { - _BitScanReverse((std::uint32_t*)&nonZeroBit, (std::uint32_t)bitStrm->bitData); + _BitScanReverse(&nonZeroBit, bitStrm->bitData); result = 31 - nonZeroBit; bitStrm->bitData <<= 32 - nonZeroBit; bitStrm->bitsLeft -= 32 - nonZeroBit; @@ -859,12 +859,12 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) while (true) { while (bitStrm->curPos + 4 <= bitStrm->curBufSize) { nextData = - _byteswap_ulong(*(std::uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); + _byteswap_ulong(*reinterpret_cast(bitStrm->mdatBuf + bitStrm->curPos)); bitStrm->curPos += 4; crxFillBuffer(bitStrm); if (nextData) { - _BitScanReverse((std::uint32_t*)&nonZeroBit, (std::uint32_t)nextData); + _BitScanReverse(&nonZeroBit, static_cast(nextData)); result = bitsLeft + 31 - nonZeroBit; bitStrm->bitData = nextData << (32 - nonZeroBit); bitStrm->bitsLeft = nonZeroBit; @@ -888,8 +888,8 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) bitsLeft += 8; } - _BitScanReverse((std::uint32_t*)&nonZeroBit, (std::uint32_t)nextData); - result = (std::uint32_t)(bitsLeft + 7 - nonZeroBit); + _BitScanReverse(&nonZeroBit, static_cast(nextData)); + result = static_cast(bitsLeft + 7 - nonZeroBit); bitStrm->bitData = nextData << (32 - nonZeroBit); bitStrm->bitsLeft = nonZeroBit; } @@ -909,7 +909,7 @@ inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) // get them from stream if (bitStrm->curPos + 4 <= bitStrm->curBufSize) { nextWord = - _byteswap_ulong(*(std::uint32_t*)(bitStrm->mdatBuf + bitStrm->curPos)); + _byteswap_ulong(*reinterpret_cast(bitStrm->mdatBuf + bitStrm->curPos)); bitStrm->curPos += 4; crxFillBuffer(bitStrm); bitStrm->bitsLeft = 32 - (bits - bitsLeft); @@ -1661,7 +1661,7 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) if (param->supportsPartial) { if (param->roundedBitsMask <= 0) { - param->lineBuf0 = (std::int32_t*)param->paramData; + param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; std::int32_t* lineBuf = param->lineBuf1 + 1; @@ -1680,7 +1680,7 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) } } - param->lineBuf0 = (std::int32_t*)param->paramData; + param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; std::int32_t* lineBuf = param->lineBuf1 + 1; @@ -1692,8 +1692,8 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) ++param->curLine; } } else { - param->lineBuf2 = (std::int32_t*)param->nonProgrData; - param->lineBuf0 = (std::int32_t*)param->paramData; + param->lineBuf2 = param->nonProgrData; + param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; std::int32_t* lineBuf = param->lineBuf1 + 1; @@ -1706,13 +1706,13 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) } } else if (!param->supportsPartial) { std::int32_t lineLength = param->subbandWidth + 2; - param->lineBuf2 = (std::int32_t*)param->nonProgrData; + param->lineBuf2 = param->nonProgrData; if (param->curLine & 1) { - param->lineBuf1 = (std::int32_t*)param->paramData; + param->lineBuf1 = param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; } else { - param->lineBuf0 = (std::int32_t*)param->paramData; + param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } @@ -1728,10 +1728,10 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { - param->lineBuf1 = (std::int32_t*)param->paramData; + param->lineBuf1 = param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; } else { - param->lineBuf0 = (std::int32_t*)param->paramData; + param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } @@ -1747,10 +1747,10 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { - param->lineBuf1 = (std::int32_t*)param->paramData; + param->lineBuf1 = param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; } else { - param->lineBuf0 = (std::int32_t*)param->paramData; + param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } @@ -1805,7 +1805,7 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) } // update subband buffers - std::int32_t* bandBuf = (std::int32_t*)subband->bandBuf; + std::int32_t* bandBuf = reinterpret_cast(subband->bandBuf); std::int32_t qScale = q_step_tbl[subband->quantValue % 6] >> (6 - subband->quantValue / 6); @@ -2421,18 +2421,18 @@ int crxParamInit(CrxBandParam** param, std::uint64_t subbandMdatOffset, { std::int32_t progrDataSize = supportsPartial ? 0 : sizeof(std::int32_t) * subbandWidth; std::int32_t paramLength = 2 * subbandWidth + 4; - std::uint8_t* paramBuf = (std::uint8_t*)calloc( - 1, sizeof(CrxBandParam) + sizeof(std::int32_t) * paramLength + progrDataSize); + std::uint8_t* paramBuf = static_cast(calloc( + 1, sizeof(CrxBandParam) + sizeof(std::int32_t) * paramLength + progrDataSize)); if (!paramBuf) { return -1; } - *param = (CrxBandParam*)paramBuf; + *param = reinterpret_cast(paramBuf); paramBuf += sizeof(CrxBandParam); - (*param)->paramData = (std::int32_t*)paramBuf; + (*param)->paramData = reinterpret_cast(paramBuf); (*param)->nonProgrData = progrDataSize ? (*param)->paramData + paramLength : nullptr; (*param)->subbandWidth = subbandWidth; @@ -2491,7 +2491,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, } // buffer allocation - planeComp->compBuf = (std::uint8_t*)malloc(compDataSize); + planeComp->compBuf = static_cast(malloc(compDataSize)); if (!planeComp->compBuf) { return -1; @@ -2511,11 +2511,11 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, // wavelet data initialisation if (img->levels) { CrxWaveletTransform* waveletTransforms = - (CrxWaveletTransform*)(planeComp->compBuf + waveletDataOffset); - std::int32_t* paramData = (std::int32_t*)(planeComp->compBuf + compCoeffDataOffset); + reinterpret_cast(planeComp->compBuf + waveletDataOffset); + std::int32_t* paramData = reinterpret_cast(planeComp->compBuf + compCoeffDataOffset); planeComp->waveletTransform = waveletTransforms; - waveletTransforms[0].subband0Buf = (std::int32_t*)subbands->bandBuf; + waveletTransforms[0].subband0Buf = reinterpret_cast(subbands->bandBuf); for (int level = 0; level < img->levels; ++level) { std::int32_t band = 3 * level + 1; @@ -2547,11 +2547,11 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, waveletTransforms[level].curLine = 0; waveletTransforms[level].curH = 0; waveletTransforms[level].fltTapH = 0; - waveletTransforms[level].subband1Buf = (std::int32_t*)subbands[band].bandBuf; + waveletTransforms[level].subband1Buf = reinterpret_cast(subbands[band].bandBuf); waveletTransforms[level].subband2Buf = - (std::int32_t*)subbands[band + 1].bandBuf; + reinterpret_cast(subbands[band + 1].bandBuf); waveletTransforms[level].subband3Buf = - (std::int32_t*)subbands[band + 2].bandBuf; + reinterpret_cast(subbands[band + 2].bandBuf); paramData = waveletTransforms[level].lineBuf[7] + transformWidth; } @@ -2584,7 +2584,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) { - CrxImage* img = (CrxImage*)p; + CrxImage* img = static_cast(p); int imageRow = 0; for (int tRow = 0; tRow < img->tileRows; tRow++) { @@ -2630,7 +2630,7 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) return -1; } - std::int32_t* lineData = (std::int32_t*)planeComp->subBands->bandBuf; + std::int32_t* lineData = reinterpret_cast(planeComp->subBands->bandBuf); crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, lineData, tile->width); } @@ -2784,10 +2784,10 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda } if (!img->tiles) { - img->tiles = (CrxTile*)malloc( + img->tiles = static_cast(malloc( sizeof(CrxTile) * nTiles + sizeof(CrxPlaneComp) * nTiles * img->nPlanes + - sizeof(CrxSubband) * nTiles * img->nPlanes * img->subbandCount); + sizeof(CrxSubband) * nTiles * img->nPlanes * img->subbandCount)); if (!img->tiles) { return -1; @@ -2795,8 +2795,8 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda // memory areas in allocated chunk CrxTile* tile = img->tiles; - CrxPlaneComp* comps = (CrxPlaneComp*)(tile + nTiles); - CrxSubband* bands = (CrxSubband*)(comps + img->nPlanes * nTiles); + CrxPlaneComp* comps = reinterpret_cast(tile + nTiles); + CrxSubband* bands = reinterpret_cast(comps + img->nPlanes * nTiles); for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { tile->tileFlag = 0; // tile neighbouring flags @@ -2989,8 +2989,8 @@ int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, std::int16_t* outBu // left as is. if (img->encType == 3 && img->nPlanes == 4 && img->nBits > 8) { img->planeBuf = - (std::int16_t*)malloc(img->planeHeight * img->planeWidth * img->nPlanes * - ((img->samplePrecision + 7) >> 3)); + static_cast(malloc(img->planeHeight * img->planeWidth * img->nPlanes * + ((img->samplePrecision + 7) >> 3))); if (!img->planeBuf) { return -1; @@ -3105,7 +3105,7 @@ void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) void DCraw::crxConvertPlaneLineDf(void* p, int imageRow) { - crxConvertPlaneLine((CrxImage*)p, imageRow); + crxConvertPlaneLine(static_cast(p), imageRow); } void DCraw::crxLoadFinalizeLoopE3(void* p, int planeHeight) @@ -3145,7 +3145,7 @@ void DCraw::crxLoadRaw() // /*imgdata.color.*/maximum = (1 << hdr.nBits) - 1; - std::uint8_t* hdrBuf = (std::uint8_t*)malloc(hdr.mdatHdrSize); + std::uint8_t* hdrBuf = static_cast(malloc(hdr.mdatHdrSize)); // read image header #ifdef _OPENMP @@ -3164,7 +3164,7 @@ void DCraw::crxLoadRaw() } // parse and setup the image data - if (crxSetupImageData(&hdr, &img, (std::int16_t*)raw_image, + if (crxSetupImageData(&hdr, &img, reinterpret_cast(raw_image), hdr.MediaOffset /*data_offset*/, hdr.MediaSize /*RT_canon_CR3_data.data_size*/, hdrBuf)) { derror(); From c5168038e8b4491fb5cb92cb05b4b9823c3b2898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Thu, 28 Nov 2019 10:37:18 +0100 Subject: [PATCH 13/20] Add missing include --- rtengine/canon_cr3_decoder.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index a64a83010..8b39c3b02 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -59,6 +59,8 @@ #include "dcraw.h" +#include "rt_math.h" + void DCraw::parse_canon_cr3() { strncpy(make, "Canon", sizeof(make)); From c4fe355fbb3802fa104c07a108a7ee83ee440ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Fri, 29 Nov 2019 14:57:14 +0100 Subject: [PATCH 14/20] Fix formatting and use `bool` (WIP) --- rtengine/canon_cr3_decoder.cc | 173 ++++++++++++++++------------------ 1 file changed, 83 insertions(+), 90 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 8b39c3b02..6056f46bb 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -860,8 +860,7 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) while (true) { while (bitStrm->curPos + 4 <= bitStrm->curBufSize) { - nextData = - _byteswap_ulong(*reinterpret_cast(bitStrm->mdatBuf + bitStrm->curPos)); + nextData = _byteswap_ulong(*reinterpret_cast(bitStrm->mdatBuf + bitStrm->curPos)); bitStrm->curPos += 4; crxFillBuffer(bitStrm); @@ -910,8 +909,7 @@ inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) if (bitsLeft < bits) { // get them from stream if (bitStrm->curPos + 4 <= bitStrm->curBufSize) { - nextWord = - _byteswap_ulong(*reinterpret_cast(bitStrm->mdatBuf + bitStrm->curPos)); + nextWord = _byteswap_ulong(*reinterpret_cast(bitStrm->mdatBuf + bitStrm->curPos)); bitStrm->curPos += 4; crxFillBuffer(bitStrm); bitStrm->bitsLeft = 32 - (bits - bitsLeft); @@ -939,30 +937,34 @@ inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) return result; } -inline int crxPredictKParameter(std::int32_t prevK, std::int32_t bitCode, - std::int32_t maxVal = 0) +inline std::int32_t crxPredictKParameter(std::int32_t prevK, std::int32_t bitCode, std::int32_t maxVal = 0) { - std::int32_t newKParam = prevK - (bitCode < (1 << prevK >> 1)) + - ((bitCode >> prevK) > 2) + ((bitCode >> prevK) > 5); + const std::int32_t newKParam = + prevK + - (bitCode < (1 << prevK >> 1)) + + ((bitCode >> prevK) > 2) + ((bitCode >> prevK) > 5); - return !maxVal || newKParam < maxVal ? newKParam : maxVal; + return + !maxVal || newKParam < maxVal + ? newKParam + : maxVal; } -inline void crxDecodeSymbolL1(CrxBandParam* param, - std::int32_t doMedianPrediction, - std::int32_t notEOL = 0) +inline void crxDecodeSymbolL1(CrxBandParam* param, std::int32_t doMedianPrediction, bool notEOL = false) { if (doMedianPrediction) { - std::int32_t symb[4]; + const std::int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; + const std::int32_t symb[4] = { + delta + param->lineBuf1[0], + delta + param->lineBuf1[0], + param->lineBuf1[0], + param->lineBuf0[1] + }; - std::int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; - symb[2] = param->lineBuf1[0]; - symb[0] = symb[1] = delta + symb[2]; - symb[3] = param->lineBuf0[1]; - - param->lineBuf1[1] = - symb[(((param->lineBuf0[0] < param->lineBuf1[0]) ^ (delta < 0)) << 1) + - ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (delta < 0))]; + param->lineBuf1[1] = symb[ + (((param->lineBuf0[0] < param->lineBuf1[0]) ^ (delta < 0)) << 1) + + ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (delta < 0)) + ]; } else { param->lineBuf1[1] = param->lineBuf0[1]; } @@ -973,8 +975,7 @@ inline void crxDecodeSymbolL1(CrxBandParam* param, if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } // add converted (+/-) error code to predicted value @@ -982,7 +983,7 @@ inline void crxDecodeSymbolL1(CrxBandParam* param, // for not end of the line - use one symbol ahead to estimate next K if (notEOL) { - std::int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; + const std::int32_t nextDelta = (param->lineBuf0[2] - param->lineBuf0[1]) << 1; bitCode = (bitCode + std::abs(nextDelta)) >> 1; ++param->lineBuf0; } @@ -993,17 +994,16 @@ inline void crxDecodeSymbolL1(CrxBandParam* param, ++param->lineBuf1; } -int crxDecodeLine(CrxBandParam* param) +bool crxDecodeLine(CrxBandParam* param) { int length = param->subbandWidth; param->lineBuf1[0] = param->lineBuf0[1]; for (; length > 1; --length) { - if (param->lineBuf1[0] != param->lineBuf0[1] || - param->lineBuf1[0] != param->lineBuf0[2]) { - crxDecodeSymbolL1(param, 1, 1); - } else { + if (param->lineBuf1[0] != param->lineBuf0[1] || param->lineBuf1[0] != param->lineBuf0[2]) { + crxDecodeSymbolL1(param, 1, true); + } else { int nSyms = 0; if (crxBitstreamGetBits(¶m->bitStream, 1)) { @@ -1036,7 +1036,7 @@ int crxDecodeLine(CrxBandParam* param) } if (nSyms > length) { - return -1; + return false; } } @@ -1053,36 +1053,37 @@ int crxDecodeLine(CrxBandParam* param) } if (length > 0) { - crxDecodeSymbolL1(param, 0, (length > 1)); + crxDecodeSymbolL1(param, 0, length > 1); } } } if (length == 1) { - crxDecodeSymbolL1(param, 1, 0); + crxDecodeSymbolL1(param, 1, false); } param->lineBuf1[1] = param->lineBuf1[0] + 1; - return 0; + return true; } -inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, - std::int32_t doSym = 1, - std::int32_t doCode = 1) +inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, bool doSym = true, bool doCode = true) { std::int32_t sym = param->lineBuf0[1]; if (doSym) { // calculate the next symbol gradient - std::int32_t symb[4]; - std::int32_t deltaH = param->lineBuf0[1] - param->lineBuf0[0]; - symb[2] = param->lineBuf1[0]; - symb[0] = symb[1] = deltaH + symb[2]; - symb[3] = param->lineBuf0[1]; - sym = - symb[(((param->lineBuf0[0] < param->lineBuf1[0]) ^ (deltaH < 0)) << 1) + - ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (deltaH < 0))]; + const std::int32_t deltaH = param->lineBuf0[1] - param->lineBuf0[0]; + const std::int32_t symb[4] = { + deltaH + param->lineBuf1[0], + deltaH + param->lineBuf1[0], + param->lineBuf1[0], + param->lineBuf0[1] + }; + sym = symb[ + (((param->lineBuf0[0] < param->lineBuf1[0]) ^ (deltaH < 0)) << 1) + + ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (deltaH < 0)) + ]; } std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); @@ -1090,36 +1091,30 @@ inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } std::int32_t code = -(bitCode & 1) ^ (bitCode >> 1); - param->lineBuf1[1] = param->roundedBitsMask * 2 * code + static_cast(code < 0) + sym; + param->lineBuf1[1] = param->roundedBitsMask * 2 * code + (code < 0) + sym; if (doCode) { if (param->lineBuf0[2] > param->lineBuf0[1]) { - code = (param->lineBuf0[2] - param->lineBuf0[1] + param->roundedBitsMask - - 1) >> - param->roundedBits; + code = (param->lineBuf0[2] - param->lineBuf0[1] + param->roundedBitsMask - 1) >> param->roundedBits; } else { - code = -( - (param->lineBuf0[1] - param->lineBuf0[2] + param->roundedBitsMask) >> - param->roundedBits); + code = -((param->lineBuf0[1] - param->lineBuf0[2] + param->roundedBitsMask) >> param->roundedBits); } - param->kParam = crxPredictKParameter(param->kParam, - (bitCode + 2 * std::abs(code)) >> 1, 15); - } else { + param->kParam = crxPredictKParameter(param->kParam, (bitCode + 2 * std::abs(code)) >> 1, 15); + } else { param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); } ++param->lineBuf1; } -int crxDecodeLineRounded(CrxBandParam* param) +bool crxDecodeLineRounded(CrxBandParam* param) { - std::int32_t valueReached = 0; + bool valueReached = false; param->lineBuf0[0] = param->lineBuf0[1]; param->lineBuf1[0] = param->lineBuf0[1]; @@ -1129,13 +1124,12 @@ int crxDecodeLineRounded(CrxBandParam* param) if (std::abs(param->lineBuf0[2] - param->lineBuf0[1]) > param->roundedBitsMask) { crxDecodeSymbolL1Rounded(param); ++param->lineBuf0; - valueReached = 1; - } else if (valueReached || std::abs(param->lineBuf0[0] - param->lineBuf1[0]) > - param->roundedBitsMask) { + valueReached = true; + } else if (valueReached || std::abs(param->lineBuf0[0] - param->lineBuf1[0]) > param->roundedBitsMask) { crxDecodeSymbolL1Rounded(param); ++param->lineBuf0; - valueReached = 0; - } else { + valueReached = false; + } else { int nSyms = 0; if (crxBitstreamGetBits(¶m->bitStream, 1)) { @@ -1169,7 +1163,7 @@ int crxDecodeLineRounded(CrxBandParam* param) } if (nSyms > length) { - return -1; + return false; } } @@ -1185,23 +1179,22 @@ int crxDecodeLineRounded(CrxBandParam* param) } if (length > 1) { - crxDecodeSymbolL1Rounded(param, 0); + crxDecodeSymbolL1Rounded(param, false); ++param->lineBuf0; - valueReached = std::abs(param->lineBuf0[1] - param->lineBuf0[0]) > - param->roundedBitsMask; + valueReached = std::abs(param->lineBuf0[1] - param->lineBuf0[0]) > param->roundedBitsMask; } else if (length == 1) { - crxDecodeSymbolL1Rounded(param, 0, 0); + crxDecodeSymbolL1Rounded(param, false, false); } } } if (length == 1) { - crxDecodeSymbolL1Rounded(param, 1, 0); + crxDecodeSymbolL1Rounded(param, true, false); } param->lineBuf1[1] = param->lineBuf1[0] + 1; - return 0; + return true; } int crxDecodeLineNoRefPrevLine(CrxBandParam* param) @@ -1535,7 +1528,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) return 0; } -int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) +bool crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) { param->lineBuf0[0] = 0; param->lineBuf1[0] = 0; @@ -1587,7 +1580,7 @@ int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) } if (nSyms > length) { - return -1; + return false; } } } @@ -1642,21 +1635,21 @@ int crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) param->lineBuf1[1] = 0; - return 0; + return true; } -int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) +bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) { if (!param || !bandBuf) { - return -1; + return false; } if (param->curLine >= param->subbandHeight) { - return -1; + return false; } if (param->curLine == 0) { - std::int32_t lineLength = param->subbandWidth + 2; + const std::int32_t lineLength = param->subbandWidth + 2; param->sParam = 0; param->kParam = 0; @@ -1665,10 +1658,10 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) if (param->roundedBitsMask <= 0) { param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - std::int32_t* lineBuf = param->lineBuf1 + 1; + std::int32_t* const lineBuf = param->lineBuf1 + 1; if (crxDecodeTopLine(param)) { - return -1; + return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); @@ -1687,7 +1680,7 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeTopLineRounded(param)) { - return -1; + return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); @@ -1699,8 +1692,8 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) param->lineBuf1 = param->lineBuf0 + lineLength; std::int32_t* lineBuf = param->lineBuf1 + 1; - if (crxDecodeTopLineNoRefPrevLine(param)) { - return -1; + if (!crxDecodeTopLineNoRefPrevLine(param)) { + return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); @@ -1721,7 +1714,7 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) std::int32_t* lineBuf = param->lineBuf1 + 1; if (crxDecodeLineNoRefPrevLine(param)) { - return -1; + return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); @@ -1739,8 +1732,8 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) std::int32_t* lineBuf = param->lineBuf1 + 1; - if (crxDecodeLine(param)) { - return -1; + if (!crxDecodeLine(param)) { + return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); @@ -1758,15 +1751,15 @@ int crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) std::int32_t* lineBuf = param->lineBuf1 + 1; - if (crxDecodeLineRounded(param)) { - return -1; + if (!crxDecodeLineRounded(param)) { + return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } - return 0; + return true; } int crxDecodeLineWithIQuantization(CrxSubband* subband) @@ -1798,7 +1791,7 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) } } - if (crxDecodeLine(subband->bandParam, subband->bandBuf)) { + if (!crxDecodeLine(subband->bandParam, subband->bandBuf)) { return -1; } @@ -2627,7 +2620,7 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) } for (int i = 0; i < tile->height; ++i) { - if (crxDecodeLine(planeComp->subBands->bandParam, + if (!crxDecodeLine(planeComp->subBands->bandParam, planeComp->subBands->bandBuf)) { return -1; } From 49f5833002fbce0457fd0a13a85b20c9678187cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Wed, 4 Dec 2019 10:23:25 +0100 Subject: [PATCH 15/20] More `bool` and `const` (WIP) --- rtengine/canon_cr3_decoder.cc | 456 ++++++++++++++++------------------ 1 file changed, 215 insertions(+), 241 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 6056f46bb..f436c759e 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -158,7 +158,7 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) int tiff_idx = -1; std::int64_t tpixels = 0; - for (unsigned int i = 0; i < tiff_nifds; i++) { + for (unsigned int i = 0; i < tiff_nifds; ++i) { if (std::int64_t(tiff_ifd[i].height) * std::int64_t(tiff_ifd[i].height) > tpixels) { tpixels = std::int64_t(tiff_ifd[i].height) * std::int64_t(tiff_ifd[i].height); tiff_idx = i; @@ -381,7 +381,7 @@ int DCraw::parseCR3( szAtom = (static_cast(get4()) << 32) | get4(); oAtomContent = oAtom + 16; szAtomContent = szAtom - 16; - } else { + } else { oAtomContent = oAtom + 8; szAtomContent = szAtom - 8; } @@ -401,7 +401,7 @@ int DCraw::parseCR3( if (!strncmp(UIID, UIID_Canon, lHdr)) { AtomType = 1; - } else { + } else { fseek(ifp, -lHdr, SEEK_CUR); } } else if (!strcmp(AtomNameStack, "moovuuidCCTP")) { @@ -470,7 +470,7 @@ int DCraw::parseCR3( if (szAtomContent >= 16) { fseek(ifp, 12L, SEEK_CUR); lHdr = 8; - } else { + } else { err = -7; goto fin; } @@ -486,7 +486,7 @@ int DCraw::parseCR3( err = -8; goto fin; } - } else { + } else { AtomType = 2; // only continue for CRAW lHdr = 0; } @@ -855,7 +855,7 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) result = 31 - nonZeroBit; bitStrm->bitData <<= 32 - nonZeroBit; bitStrm->bitsLeft -= 32 - nonZeroBit; - } else { + } else { std::uint32_t bitsLeft = bitStrm->bitsLeft; while (true) { @@ -950,7 +950,7 @@ inline std::int32_t crxPredictKParameter(std::int32_t prevK, std::int32_t bitCod : maxVal; } -inline void crxDecodeSymbolL1(CrxBandParam* param, std::int32_t doMedianPrediction, bool notEOL = false) +inline void crxDecodeSymbolL1(CrxBandParam* param, bool doMedianPrediction, bool notEOL = false) { if (doMedianPrediction) { const std::int32_t delta = param->lineBuf0[1] - param->lineBuf0[0]; @@ -965,7 +965,7 @@ inline void crxDecodeSymbolL1(CrxBandParam* param, std::int32_t doMedianPredicti (((param->lineBuf0[0] < param->lineBuf1[0]) ^ (delta < 0)) << 1) + ((param->lineBuf1[0] < param->lineBuf0[1]) ^ (delta < 0)) ]; - } else { + } else { param->lineBuf1[1] = param->lineBuf0[1]; } @@ -1002,7 +1002,7 @@ bool crxDecodeLine(CrxBandParam* param) for (; length > 1; --length) { if (param->lineBuf1[0] != param->lineBuf0[1] || param->lineBuf1[0] != param->lineBuf0[2]) { - crxDecodeSymbolL1(param, 1, true); + crxDecodeSymbolL1(param, true, true); } else { int nSyms = 0; @@ -1053,13 +1053,13 @@ bool crxDecodeLine(CrxBandParam* param) } if (length > 0) { - crxDecodeSymbolL1(param, 0, length > 1); + crxDecodeSymbolL1(param, false, length > 1); } } } if (length == 1) { - crxDecodeSymbolL1(param, 1, false); + crxDecodeSymbolL1(param, true, false); } param->lineBuf1[1] = param->lineBuf1[0] + 1; @@ -1197,19 +1197,18 @@ bool crxDecodeLineRounded(CrxBandParam* param) return true; } -int crxDecodeLineNoRefPrevLine(CrxBandParam* param) +bool crxDecodeLineNoRefPrevLine(CrxBandParam* param) { std::int32_t i = 0; - for (; i < param->subbandWidth - 1; i++) { - if (param->lineBuf0[i + 2] | param->lineBuf0[i + 1] | param->lineBuf1[i]) { + for (; i < param->subbandWidth - 1; ++i) { + if (param->lineBuf0[i + 2] || param->lineBuf0[i + 1] || param->lineBuf1[i]) { std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[i + 1] = -(bitCode & 1) ^ (bitCode >> 1); @@ -1219,10 +1218,10 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) if (param->kParam >= 15) { param->kParam = 15; } - } else { + } else { ++param->kParam; } - } else { + } else { int nSyms = 0; if (crxBitstreamGetBits(¶m->bitStream, 1)) { @@ -1257,11 +1256,11 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) } if (i + nSyms > param->subbandWidth) { - return -1; + return false; } } } else if (i > param->subbandWidth) { - return -1; + return false; } if (nSyms > 0) { @@ -1277,8 +1276,7 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[i + 1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); @@ -1287,14 +1285,13 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) } continue; - } else { + } else { std::uint32_t bitCode = crxBitstreamGetZeros(¶m->bitStream); if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[i + 1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); @@ -1304,7 +1301,7 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) if (param->kParam >= 15) { param->kParam = 15; } - } else { + } else { ++param->kParam; } } @@ -1319,8 +1316,7 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[i + 1] = -(bitCode & 1) ^ (bitCode >> 1); @@ -1328,10 +1324,10 @@ int crxDecodeLineNoRefPrevLine(CrxBandParam* param) param->lineBuf2[i] = param->kParam; } - return 0; + return true; } -int crxDecodeTopLine(CrxBandParam* param) +bool crxDecodeTopLine(CrxBandParam* param) { param->lineBuf1[0] = 0; @@ -1374,7 +1370,7 @@ int crxDecodeTopLine(CrxBandParam* param) } if (nSyms > length) { - return -1; + return false; } } @@ -1399,8 +1395,7 @@ int crxDecodeTopLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); @@ -1415,8 +1410,7 @@ int crxDecodeTopLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[1] += -(bitCode & 1) ^ (bitCode >> 1); @@ -1426,10 +1420,10 @@ int crxDecodeTopLine(CrxBandParam* param) param->lineBuf1[1] = param->lineBuf1[0] + 1; - return 0; + return true; } -int crxDecodeTopLineRounded(CrxBandParam* param) +bool crxDecodeTopLineRounded(CrxBandParam* param) { param->lineBuf1[0] = 0; @@ -1472,7 +1466,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) } if (nSyms > length) { - return -1; + return false; } } } @@ -1497,11 +1491,10 @@ int crxDecodeTopLineRounded(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } - std::int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); + const std::int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); ++param->lineBuf1; @@ -1513,11 +1506,10 @@ int crxDecodeTopLineRounded(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } - std::int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); + const std::int32_t sVal = -(bitCode & 1) ^ (bitCode >> 1); param->lineBuf1[1] += param->roundedBitsMask * 2 * sVal + (sVal >> 31); param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); ++param->lineBuf1; @@ -1525,7 +1517,7 @@ int crxDecodeTopLineRounded(CrxBandParam* param) param->lineBuf1[1] = param->lineBuf1[0] + 1; - return 0; + return true; } bool crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) @@ -1541,13 +1533,12 @@ bool crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[1] = -(bitCode & 1) ^ (bitCode >> 1); param->kParam = crxPredictKParameter(param->kParam, bitCode, 15); - } else { + } else { int nSyms = 0; if (crxBitstreamGetBits(¶m->bitStream, 1)) { @@ -1604,8 +1595,7 @@ bool crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[1] = -((bitCode + 1) & 1) ^ ((bitCode + 1) >> 1); @@ -1623,8 +1613,7 @@ bool crxDecodeTopLineNoRefPrevLine(CrxBandParam* param) if (bitCode >= 41) { bitCode = crxBitstreamGetBits(¶m->bitStream, 21); } else if (param->kParam) { - bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | - (bitCode << param->kParam); + bitCode = crxBitstreamGetBits(¶m->bitStream, param->kParam) | (bitCode << param->kParam); } param->lineBuf1[1] = -(bitCode & 1) ^ (bitCode >> 1); @@ -1658,15 +1647,15 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) if (param->roundedBitsMask <= 0) { param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - std::int32_t* const lineBuf = param->lineBuf1 + 1; + const std::int32_t* const lineBuf = param->lineBuf1 + 1; - if (crxDecodeTopLine(param)) { + if (!crxDecodeTopLine(param)) { return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; - } else { + } else { param->roundedBits = 1; if (param->roundedBitsMask & ~1) { @@ -1677,20 +1666,20 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - std::int32_t* lineBuf = param->lineBuf1 + 1; + const std::int32_t* const lineBuf = param->lineBuf1 + 1; - if (crxDecodeTopLineRounded(param)) { + if (!crxDecodeTopLineRounded(param)) { return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } - } else { + } else { param->lineBuf2 = param->nonProgrData; param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; - std::int32_t* lineBuf = param->lineBuf1 + 1; + const std::int32_t* const lineBuf = param->lineBuf1 + 1; if (!crxDecodeTopLineNoRefPrevLine(param)) { return false; @@ -1700,37 +1689,37 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) ++param->curLine; } } else if (!param->supportsPartial) { - std::int32_t lineLength = param->subbandWidth + 2; + const std::int32_t lineLength = param->subbandWidth + 2; param->lineBuf2 = param->nonProgrData; if (param->curLine & 1) { param->lineBuf1 = param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; - } else { + } else { param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } - std::int32_t* lineBuf = param->lineBuf1 + 1; + const std::int32_t* const lineBuf = param->lineBuf1 + 1; - if (crxDecodeLineNoRefPrevLine(param)) { + if (!crxDecodeLineNoRefPrevLine(param)) { return false; } memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } else if (param->roundedBitsMask <= 0) { - std::int32_t lineLength = param->subbandWidth + 2; + const std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { param->lineBuf1 = param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; - } else { + } else { param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } - std::int32_t* lineBuf = param->lineBuf1 + 1; + const std::int32_t* const lineBuf = param->lineBuf1 + 1; if (!crxDecodeLine(param)) { return false; @@ -1738,18 +1727,18 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; - } else { - std::int32_t lineLength = param->subbandWidth + 2; + } else { + const std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { param->lineBuf1 = param->paramData; param->lineBuf0 = param->lineBuf1 + lineLength; - } else { + } else { param->lineBuf0 = param->paramData; param->lineBuf1 = param->lineBuf0 + lineLength; } - std::int32_t* lineBuf = param->lineBuf1 + 1; + const std::int32_t* const lineBuf = param->lineBuf1 + 1; if (!crxDecodeLineRounded(param)) { return false; @@ -1762,13 +1751,13 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) return true; } -int crxDecodeLineWithIQuantization(CrxSubband* subband) +bool crxDecodeLineWithIQuantization(CrxSubband* subband) { - std::int32_t q_step_tbl[6] = {0x28, 0x2D, 0x33, 0x39, 0x40, 0x48}; + constexpr std::int32_t q_step_tbl[6] = {0x28, 0x2D, 0x33, 0x39, 0x40, 0x48}; if (!subband->dataSize) { memset(subband->bandBuf, 0, subband->bandSize); - return 0; + return true; } if (subband->supportsPartial) { @@ -1777,49 +1766,48 @@ int crxDecodeLineWithIQuantization(CrxSubband* subband) if (bitCode >= 23) { bitCode = crxBitstreamGetBits(&subband->bandParam->bitStream, 8); } else if (subband->paramK) { - bitCode = - crxBitstreamGetBits(&subband->bandParam->bitStream, subband->paramK) | - (bitCode << subband->paramK); + bitCode = crxBitstreamGetBits(&subband->bandParam->bitStream, subband->paramK) | (bitCode << subband->paramK); } - subband->quantValue += - -(bitCode & 1) ^ (bitCode >> 1);// converting encoded to signed integer + subband->quantValue += -(bitCode & 1) ^ (bitCode >> 1);// converting encoded to signed integer subband->paramK = crxPredictKParameter(subband->paramK, bitCode); if (subband->paramK > 7) { - return -1; + return false; } } if (!crxDecodeLine(subband->bandParam, subband->bandBuf)) { - return -1; + return false; } if (subband->width <= 0) { - return 0LL; + return true; } // update subband buffers - std::int32_t* bandBuf = reinterpret_cast(subband->bandBuf); - std::int32_t qScale = - q_step_tbl[subband->quantValue % 6] >> (6 - subband->quantValue / 6); + std::int32_t* const bandBuf = reinterpret_cast(subband->bandBuf); + std::int32_t qScale = q_step_tbl[subband->quantValue % 6] >> (6 - subband->quantValue / 6); if (subband->quantValue / 6 >= 6) { - qScale = q_step_tbl[subband->quantValue % 6] * - (1 << (subband->quantValue / 6 + 26)); + qScale = q_step_tbl[subband->quantValue % 6] * (1 << (subband->quantValue / 6 + 26)); } if (qScale != 1) { - for (std::int32_t i = 0; i < subband->width; i++) { + for (std::int32_t i = 0; i < subband->width; ++i) { bandBuf[i] *= qScale; } } - return 0; + return true; } -void crxHorizontal53(std::int32_t* lineBufLA, std::int32_t* lineBufLB, - CrxWaveletTransform* wavelet, std::uint32_t tileFlag) +void crxHorizontal53( + std::int32_t* lineBufLA, + std::int32_t* lineBufLB, + CrxWaveletTransform* wavelet, + std::uint32_t tileFlag +) { std::int32_t* band0Buf = wavelet->subband0Buf; std::int32_t* band1Buf = wavelet->subband1Buf; @@ -1829,13 +1817,13 @@ void crxHorizontal53(std::int32_t* lineBufLA, std::int32_t* lineBufLB, if (wavelet->width <= 1) { lineBufLA[0] = band0Buf[0]; lineBufLB[0] = band2Buf[0]; - } else { + } else { if (tileFlag & E_HAS_TILES_ON_THE_LEFT) { lineBufLA[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufLB[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); ++band1Buf; ++band3Buf; - } else { + } else { lineBufLA[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufLB[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); } @@ -1861,10 +1849,10 @@ void crxHorizontal53(std::int32_t* lineBufLA, std::int32_t* lineBufLB, } if (tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - std::int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + const std::int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufLA[1] = band1Buf[0] + ((deltaA + lineBufLA[0]) >> 1); - std::int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + const std::int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufLB[1] = band3Buf[0] + ((deltaB + lineBufLB[0]) >> 1); if (wavelet->width & 1) { @@ -1872,16 +1860,12 @@ void crxHorizontal53(std::int32_t* lineBufLA, std::int32_t* lineBufLB, lineBufLB[2] = deltaB; } } else if (wavelet->width & 1) { - lineBufLA[1] = - band1Buf[0] + - ((lineBufLA[0] + band0Buf[0] - ((band1Buf[0] + 1) >> 1)) >> 1); + lineBufLA[1] = band1Buf[0] + ((lineBufLA[0] + band0Buf[0] - ((band1Buf[0] + 1) >> 1)) >> 1); lineBufLA[2] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); - lineBufLB[1] = - band3Buf[0] + - ((lineBufLB[0] + band2Buf[0] - ((band3Buf[0] + 1) >> 1)) >> 1); + lineBufLB[1] = band3Buf[0] + ((lineBufLB[0] + band2Buf[0] - ((band3Buf[0] + 1) >> 1)) >> 1); lineBufLB[2] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); - } else { + } else { lineBufLA[1] = lineBufLA[0] + band1Buf[0]; lineBufLB[1] = lineBufLB[0] + band3Buf[0]; } @@ -1890,64 +1874,62 @@ void crxHorizontal53(std::int32_t* lineBufLA, std::int32_t* lineBufLB, std::int32_t* crxIdwt53FilterGetLine(CrxPlaneComp* comp, std::int32_t level) { - std::int32_t* result = comp->waveletTransform[level] - .lineBuf[(comp->waveletTransform[level].fltTapH - - comp->waveletTransform[level].curH + 5) % - 5 + - 3]; - comp->waveletTransform[level].curH--; + std::int32_t* const result = comp->waveletTransform[level].lineBuf[ + (comp->waveletTransform[level].fltTapH - comp->waveletTransform[level].curH + 5) % 5 + 3 + ]; + --comp->waveletTransform[level].curH; return result; } -int crxIdwt53FilterDecode(CrxPlaneComp* comp, std::int32_t level) +bool crxIdwt53FilterDecode(CrxPlaneComp* comp, std::int32_t level) { if (comp->waveletTransform[level].curH) { - return 0; + return true; } - CrxSubband* sband = comp->subBands + 3 * level; + CrxSubband* const sband = comp->subBands + 3 * level; - if (comp->waveletTransform[level].height - 3 <= - comp->waveletTransform[level].curLine && - !(comp->tileFlag & E_HAS_TILES_ON_THE_BOTTOM)) { + if (comp->waveletTransform[level].height - 3 <= comp->waveletTransform[level].curLine && !(comp->tileFlag & E_HAS_TILES_ON_THE_BOTTOM)) { if (comp->waveletTransform[level].height & 1) { if (level) { - if (crxIdwt53FilterDecode(comp, level - 1)) { - return -1; + if (!crxIdwt53FilterDecode(comp, level - 1)) { + return false; } - } else if (crxDecodeLineWithIQuantization(sband)) { - return -1; + } else if (!crxDecodeLineWithIQuantization(sband)) { + return false; } - if (crxDecodeLineWithIQuantization(sband + 1)) { - return -1; + if (!crxDecodeLineWithIQuantization(sband + 1)) { + return false; } } - } else { + } else { if (level) { - if (crxIdwt53FilterDecode(comp, level - 1)) { - return -1; + if (!crxIdwt53FilterDecode(comp, level - 1)) { + return false; } - } else if (crxDecodeLineWithIQuantization(sband)) {// LL band - return -1; + } else if (!crxDecodeLineWithIQuantization(sband)) { // LL band + return false; } - if (crxDecodeLineWithIQuantization(sband + 1) ||// HL band - crxDecodeLineWithIQuantization(sband + 2) ||// LH band - crxDecodeLineWithIQuantization(sband + 3)) {// HH band - return -1; + if ( + !crxDecodeLineWithIQuantization(sband + 1) // HL band + || !crxDecodeLineWithIQuantization(sband + 2) // LH band + || !crxDecodeLineWithIQuantization(sband + 3) // HH band + ) { + return false; } } - return 0; + return true; } -int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) +bool crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) { - CrxWaveletTransform* wavelet = comp->waveletTransform + level; + CrxWaveletTransform* const wavelet = comp->waveletTransform + level; if (wavelet->curH) { - return 0; + return true; } if (wavelet->curLine >= wavelet->height - 3) { @@ -1955,19 +1937,19 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) if (wavelet->height & 1) { if (level) { if (!wavelet[-1].curH) { - if (crxIdwt53FilterTransform(comp, level - 1)) { - return -1; + if (!crxIdwt53FilterTransform(comp, level - 1)) { + return false; } } wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); } - std::int32_t* band0Buf = wavelet->subband0Buf; - std::int32_t* band1Buf = wavelet->subband1Buf; - std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - std::int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - std::int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; + const std::int32_t* band0Buf = wavelet->subband0Buf; + const std::int32_t* band1Buf = wavelet->subband1Buf; + const std::int32_t* const lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* const lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + std::int32_t* const lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; std::int32_t* lineBufL0 = wavelet->lineBuf[0]; std::int32_t* lineBufL1 = wavelet->lineBuf[1]; @@ -1977,19 +1959,18 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) // process L bands if (wavelet->width <= 1) { lineBufL0[0] = band0Buf[0]; - } else { + } else { if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); ++band1Buf; - } else { + } else { lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); } ++band0Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - std::int32_t delta = - band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); lineBufL0[2] = delta; ++band0Buf; @@ -1998,18 +1979,17 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - std::int32_t delta = - band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); if (wavelet->width & 1) { lineBufL0[2] = delta; } } else if (wavelet->width & 1) { - std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); lineBufL0[2] = delta; - } else { + } else { lineBufL0[1] = band1Buf[0] + lineBufL0[0]; } } @@ -2018,8 +1998,8 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) lineBufL0 = wavelet->lineBuf[0]; lineBufL1 = wavelet->lineBuf[1]; - for (std::int32_t i = 0; i < wavelet->width; i++) { - std::int32_t delta = lineBufL0[i] - ((lineBufL1[i] + 1) >> 1); + for (std::int32_t i = 0; i < wavelet->width; ++i) { + const std::int32_t delta = lineBufL0[i] - ((lineBufL1[i] + 1) >> 1); lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); lineBufH2[i] = delta; } @@ -2027,14 +2007,14 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) wavelet->curH += 3; wavelet->curLine += 3; wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; - } else { - std::int32_t* lineBufL2 = wavelet->lineBuf[2]; - std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - std::int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + } else { + std::int32_t* const lineBufL2 = wavelet->lineBuf[2]; + const std::int32_t* const lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* const lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; wavelet->lineBuf[1] = lineBufL2; wavelet->lineBuf[2] = wavelet->lineBuf[1]; - for (std::int32_t i = 0; i < wavelet->width; i++) { + for (std::int32_t i = 0; i < wavelet->width; ++i) { lineBufH1[i] = lineBufH0[i] + lineBufL2[i]; } @@ -2043,26 +2023,26 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) wavelet->fltTapH = (wavelet->fltTapH + 2) % 5; } } - } else { + } else { if (level) { - if (!wavelet[-1].curH && crxIdwt53FilterTransform(comp, level - 1)) { - return -1; + if (!wavelet[-1].curH && !crxIdwt53FilterTransform(comp, level - 1)) { + return false; } wavelet->subband0Buf = crxIdwt53FilterGetLine(comp, level - 1); } - std::int32_t* band0Buf = wavelet->subband0Buf; - std::int32_t* band1Buf = wavelet->subband1Buf; - std::int32_t* band2Buf = wavelet->subband2Buf; - std::int32_t* band3Buf = wavelet->subband3Buf; + const std::int32_t* band0Buf = wavelet->subband0Buf; + const std::int32_t* band1Buf = wavelet->subband1Buf; + const std::int32_t* band2Buf = wavelet->subband2Buf; + const std::int32_t* band3Buf = wavelet->subband3Buf; std::int32_t* lineBufL0 = wavelet->lineBuf[0]; std::int32_t* lineBufL1 = wavelet->lineBuf[1]; - std::int32_t* lineBufL2 = wavelet->lineBuf[2]; - std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; - std::int32_t* lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; - std::int32_t* lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; + const std::int32_t* lineBufL2 = wavelet->lineBuf[2]; + const std::int32_t* const lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; + std::int32_t* const lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; + std::int32_t* const lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; wavelet->lineBuf[1] = wavelet->lineBuf[2]; wavelet->lineBuf[2] = lineBufL1; @@ -2071,13 +2051,13 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) if (wavelet->width <= 1) { lineBufL0[0] = band0Buf[0]; lineBufL1[0] = band2Buf[0]; - } else { + } else { if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL1[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); ++band1Buf; ++band3Buf; - } else { + } else { lineBufL0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufL1[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); } @@ -2103,10 +2083,10 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - std::int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + const std::int32_t deltaA = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufL0[1] = band1Buf[0] + ((deltaA + lineBufL0[0]) >> 1); - std::int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + const std::int32_t deltaB = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufL1[1] = band3Buf[0] + ((deltaB + lineBufL1[0]) >> 1); if (wavelet->width & 1) { @@ -2121,7 +2101,7 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); lineBufL1[1] = band3Buf[0] + ((delta + lineBufL1[0]) >> 1); lineBufL1[2] = delta; - } else { + } else { lineBufL0[1] = lineBufL0[0] + band1Buf[0]; lineBufL1[1] = lineBufL1[0] + band3Buf[0]; } @@ -2132,8 +2112,8 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) lineBufL1 = wavelet->lineBuf[1]; lineBufL2 = wavelet->lineBuf[2]; - for (std::int32_t i = 0; i < wavelet->width; i++) { - std::int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); + for (std::int32_t i = 0; i < wavelet->width; ++i) { + const std::int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); lineBufH1[i] = lineBufL1[i] + ((delta + lineBufH0[i]) >> 1); lineBufH2[i] = delta; } @@ -2142,56 +2122,55 @@ int crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) wavelet->curH += 3; wavelet->curLine += 3; wavelet->fltTapH = (wavelet->fltTapH + 3) % 5; - } else { + } else { wavelet->curH += 2; wavelet->curLine += 2; wavelet->fltTapH = (wavelet->fltTapH + 2) % 5; } } - return 0; + return true; } -int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) +bool crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) { if (prevLevel < 0) { - return 0; + return true; } - for (int curLevel = 0, curBand = 0; curLevel < prevLevel + 1; - curLevel++, curBand += 3) { - CrxWaveletTransform* wavelet = comp->waveletTransform + curLevel; + for (int curLevel = 0, curBand = 0; curLevel < prevLevel + 1; ++curLevel, curBand += 3) { + CrxWaveletTransform* const wavelet = comp->waveletTransform + curLevel; if (curLevel) { wavelet[0].subband0Buf = crxIdwt53FilterGetLine(comp, curLevel - 1); - } else if (crxDecodeLineWithIQuantization(comp->subBands + curBand)) { - return -1; + } else if (!crxDecodeLineWithIQuantization(comp->subBands + curBand)) { + return false; } std::int32_t* lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; if (wavelet->height > 1) { - if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1) || - crxDecodeLineWithIQuantization(comp->subBands + curBand + 2) || - crxDecodeLineWithIQuantization(comp->subBands + curBand + 3)) { - return -1; + if ( + !crxDecodeLineWithIQuantization(comp->subBands + curBand + 1) + || !crxDecodeLineWithIQuantization(comp->subBands + curBand + 2) + || !crxDecodeLineWithIQuantization(comp->subBands + curBand + 3) + ) { + return false; } - std::int32_t* lineBufL0 = wavelet->lineBuf[0]; - std::int32_t* lineBufL1 = wavelet->lineBuf[1]; + std::int32_t* const lineBufL0 = wavelet->lineBuf[0]; + const std::int32_t* const lineBufL1 = wavelet->lineBuf[1]; std::int32_t* lineBufL2 = wavelet->lineBuf[2]; if (comp->tileFlag & E_HAS_TILES_ON_THE_TOP) { - crxHorizontal53(lineBufL0, wavelet->lineBuf[1], wavelet, - comp->tileFlag); + crxHorizontal53(lineBufL0, wavelet->lineBuf[1], wavelet, comp->tileFlag); - if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 3) || - crxDecodeLineWithIQuantization(comp->subBands + curBand + 2)) { - return -1; + if (!crxDecodeLineWithIQuantization(comp->subBands + curBand + 3)|| !crxDecodeLineWithIQuantization(comp->subBands + curBand + 2)) { + return false; } - std::int32_t* band2Buf = wavelet->subband2Buf; - std::int32_t* band3Buf = wavelet->subband3Buf; + const std::int32_t* band2Buf = wavelet->subband2Buf; + const std::int32_t* band3Buf = wavelet->subband3Buf; // process L band if (wavelet->width <= 1) { @@ -2200,15 +2179,14 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { lineBufL2[0] = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); ++band3Buf; - } else { + } else { lineBufL2[0] = band2Buf[0] - ((band3Buf[0] + 1) >> 1); } ++band2Buf; for (int i = 0; i < wavelet->width - 3; i += 2) { - std::int32_t delta = - band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + const std::int32_t delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); lineBufL2[2] = delta; @@ -2218,48 +2196,44 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - std::int32_t delta = - band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); + const std::int32_t delta = band2Buf[0] - ((band3Buf[0] + band3Buf[1] + 2) >> 2); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); if (wavelet->width & 1) { lineBufL2[2] = delta; } } else if (wavelet->width & 1) { - std::int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); + const std::int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); lineBufL2[2] = delta; - } else { + } else { lineBufL2[1] = band3Buf[0] + lineBufL2[0]; } } // process H band - for (std::int32_t i = 0; i < wavelet->width; i++) { - lineBufH0[i] = - lineBufL0[i] - ((lineBufL1[i] + lineBufL2[i] + 2) >> 2); + for (std::int32_t i = 0; i < wavelet->width; ++i) { + lineBufH0[i] = lineBufL0[i] - ((lineBufL1[i] + lineBufL2[i] + 2) >> 2); } - } else { - crxHorizontal53(lineBufL0, wavelet->lineBuf[2], wavelet, - comp->tileFlag); + } else { + crxHorizontal53(lineBufL0, wavelet->lineBuf[2], wavelet, comp->tileFlag); - for (int i = 0; i < wavelet->width; i++) { + for (int i = 0; i < wavelet->width; ++i) { lineBufH0[i] = lineBufL0[i] - ((lineBufL2[i] + 1) >> 1); } } - if (crxIdwt53FilterDecode(comp, curLevel) || - crxIdwt53FilterTransform(comp, curLevel)) { - return -1; + if (!crxIdwt53FilterDecode(comp, curLevel) || !crxIdwt53FilterTransform(comp, curLevel)) { + return false; } - } else { - if (crxDecodeLineWithIQuantization(comp->subBands + curBand + 1)) { - return -1; + } else { + if (!crxDecodeLineWithIQuantization(comp->subBands + curBand + 1)) { + return false; } - std::int32_t* band0Buf = wavelet->subband0Buf; - std::int32_t* band1Buf = wavelet->subband1Buf; + const std::int32_t* band0Buf = wavelet->subband0Buf; + const std::int32_t* band1Buf = wavelet->subband1Buf; // process H band if (wavelet->width <= 1) { @@ -2268,7 +2242,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) if (comp->tileFlag & E_HAS_TILES_ON_THE_LEFT) { lineBufH0[0] = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); ++band1Buf; - } else { + } else { lineBufH0[0] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); } @@ -2285,14 +2259,14 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) } if (comp->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); + const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; } else if (wavelet->width & 1) { - std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); + const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; - } else { + } else { lineBufH0[1] = band1Buf[0] + lineBufH0[0]; } } @@ -2303,7 +2277,7 @@ int crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) } } - return 0; + return true; } void crxFreeSubbandData(CrxImage* image, CrxPlaneComp* comp) @@ -2317,7 +2291,7 @@ void crxFreeSubbandData(CrxImage* image, CrxPlaneComp* comp) return; } - for (std::int32_t i = 0; i < image->subbandCount; i++) { + for (std::int32_t i = 0; i < image->subbandCount; ++i) { if (comp->subBands[i].bandParam) { free(comp->subBands[i].bandParam); comp->subBands[i].bandParam = nullptr; @@ -2340,7 +2314,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, std::int32_t minVal = -maxVal; --maxVal; - for (int i = 0; i < lineLength; i++) { + for (int i = 0; i < lineLength; ++i) { img->outBufs[plane][rawOffset + 2 * i] = rtengine::LIM(lineData[i], minVal, maxVal); } @@ -2349,14 +2323,14 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, rawOffset = plane * img->planeWidth * img->planeHeight + img->planeWidth * imageRow + imageCol; - for (int i = 0; i < lineLength; i++) { + for (int i = 0; i < lineLength; ++i) { img->planeBuf[rawOffset + i] = lineData[i]; } } else if (img->nPlanes == 4) { std::int32_t median = 1 << (img->nBits - 1); std::int32_t maxVal = (1 << img->nBits) - 1; - for (int i = 0; i < lineLength; i++) { + for (int i = 0; i < lineLength; ++i) { img->outBufs[plane][rawOffset + 2 * i] = rtengine::LIM(median + lineData[i], 0, maxVal); } @@ -2365,7 +2339,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, std::int32_t median = 1 << (img->nBits - 1); rawOffset = img->planeWidth * imageRow + imageCol; - for (int i = 0; i < lineLength; i++) { + for (int i = 0; i < lineLength; ++i) { img->outBufs[0][rawOffset + i] = rtengine::LIM(median + lineData[i], 0, maxVal); } @@ -2382,7 +2356,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, std::uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; // for this stage - all except imageRow is ignored - for (int i = 0; i < img->planeWidth; i++) { + for (int i = 0; i < img->planeWidth; ++i) { std::int32_t gr = median + (plane0[i] << 10) - 168 * plane1[i] - 585 * plane3[i]; std::int32_t val = 0; @@ -2461,7 +2435,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, CrxSubband* subbands = planeComp->subBands; // calculate sizes - for (std::int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + for (std::int32_t subbandNum = 0; subbandNum < toSubbands; ++subbandNum) { subbands[subbandNum].bandSize = subbands[subbandNum].width * sizeof(std::int32_t); // 4bytes compDataSize += subbands[subbandNum].bandSize; @@ -2496,7 +2470,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, std::uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; std::uint8_t* subbandBuf = planeComp->compBuf; - for (std::int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + for (std::int32_t subbandNum = 0; subbandNum < toSubbands; ++subbandNum) { subbands[subbandNum].bandBuf = subbandBuf; subbandBuf += subbands[subbandNum].bandSize; subbands[subbandNum].mdatOffset = @@ -2518,7 +2492,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, if (level >= img->levels - 1) { waveletTransforms[level].height = tile->height; transformWidth = tile->width; - } else { + } else { waveletTransforms[level].height = subbands[band + 3].height; transformWidth = subbands[band + 4].width; } @@ -2553,7 +2527,7 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, } // decoding params and bitstream initialisation - for (std::int32_t subbandNum = 0; subbandNum < toSubbands; subbandNum++) { + for (std::int32_t subbandNum = 0; subbandNum < toSubbands; ++subbandNum) { if (subbands[subbandNum].dataSize) { std::int32_t supportsPartial = 0; std::uint32_t roundedBitsMask = 0; @@ -2582,10 +2556,10 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) CrxImage* img = static_cast(p); int imageRow = 0; - for (int tRow = 0; tRow < img->tileRows; tRow++) { + for (int tRow = 0; tRow < img->tileRows; ++tRow) { int imageCol = 0; - for (int tCol = 0; tCol < img->tileCols; tCol++) { + for (int tCol = 0; tCol < img->tileCols; ++tCol) { CrxTile* tile = img->tiles + tRow * img->tileRows + tCol; CrxPlaneComp* planeComp = tile->comps + planeNumber; std::uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; @@ -2596,13 +2570,13 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) } if (img->levels) { - if (crxIdwt53FilterInitialize(planeComp, img->levels - 1)) { + if (!crxIdwt53FilterInitialize(planeComp, img->levels - 1)) { return -1; } for (int i = 0; i < tile->height; ++i) { - if (crxIdwt53FilterDecode(planeComp, img->levels - 1) || - crxIdwt53FilterTransform(planeComp, img->levels - 1)) { + if (!crxIdwt53FilterDecode(planeComp, img->levels - 1) || + !crxIdwt53FilterTransform(planeComp, img->levels - 1)) { return -1; } @@ -2611,7 +2585,7 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, lineData, tile->width); } - } else { + } else { // we have the only subband in this case if (!planeComp->subBands->dataSize) { memset(planeComp->subBands->bandBuf, 0, @@ -2811,7 +2785,7 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda tile->tileFlag |= E_HAS_TILES_ON_THE_LEFT; } } - } else { + } else { // last tile in a tile row tile->width = img->planeWidth - hdr->tileWidth * (img->tileCols - 1); @@ -2831,7 +2805,7 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda tile->tileFlag |= E_HAS_TILES_ON_THE_TOP; } } - } else { + } else { // non first tile row tile->height = img->planeHeight - hdr->tileHeight * (img->tileRows - 1); @@ -3052,7 +3026,7 @@ int crxFreeImageData(CrxImage* img) if (img->tiles) { for (std::int32_t curTile = 0; curTile < nTiles; curTile++, tile++) { if (tile[curTile].comps) { - for (std::int32_t curPlane = 0; curPlane < img->nPlanes; curPlane++) { + for (std::int32_t curPlane = 0; curPlane < img->nPlanes; ++curPlane) { crxFreeSubbandData(img, tile[curTile].comps + curPlane); } } @@ -3212,7 +3186,7 @@ int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) if (hdr->nBits > 15) { return -1; } - } else { + } else { if (hdr->encType && hdr->encType != 3) { return -1; } From 8f82a8362afe90151b2d3d95339fdd13e2e2b122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Fri, 6 Dec 2019 08:18:17 +0100 Subject: [PATCH 16/20] Finalize `const`/`bool` cleanup --- rtengine/canon_cr3_decoder.cc | 445 ++++++++++++++++++---------------- rtengine/dcraw.h | 4 +- 2 files changed, 236 insertions(+), 213 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index f436c759e..3912af6b4 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -503,7 +503,7 @@ int DCraw::parseCR3( goto fin; } - if (!crxParseImageHeader(CMP1, nTrack)) { + if (crxParseImageHeader(CMP1, nTrack)) { RT_canon_CR3_data.crx_header[nTrack].MediaType = 1; } } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWJPEG")) { @@ -2302,64 +2302,63 @@ void crxFreeSubbandData(CrxImage* image, CrxPlaneComp* comp) } } -void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, - int plane = 0, const std::int32_t* lineData = nullptr, - int lineLength = 0) +void crxConvertPlaneLine( + CrxImage* img, + int imageRow, + int imageCol = 0, + int plane = 0, + const std::int32_t* lineData = nullptr, + int lineLength = 0 +) { if (lineData) { std::uint64_t rawOffset = 4 * img->planeWidth * imageRow + 2 * imageCol; if (img->encType == 1) { - std::int32_t maxVal = 1 << (img->nBits - 1); - std::int32_t minVal = -maxVal; - --maxVal; + const std::int32_t maxVal = 1 << (img->nBits - 1); + const std::int32_t minVal = -maxVal; for (int i = 0; i < lineLength; ++i) { - img->outBufs[plane][rawOffset + 2 * i] = - rtengine::LIM(lineData[i], minVal, maxVal); + img->outBufs[plane][rawOffset + 2 * i] = rtengine::LIM(lineData[i], minVal, maxVal - 1); } } else if (img->encType == 3) { // copy to intermediate planeBuf - rawOffset = plane * img->planeWidth * img->planeHeight + - img->planeWidth * imageRow + imageCol; + rawOffset = plane * img->planeWidth * img->planeHeight + img->planeWidth * imageRow + imageCol; for (int i = 0; i < lineLength; ++i) { img->planeBuf[rawOffset + i] = lineData[i]; } } else if (img->nPlanes == 4) { - std::int32_t median = 1 << (img->nBits - 1); - std::int32_t maxVal = (1 << img->nBits) - 1; + const std::int32_t median = 1 << (img->nBits - 1); + const std::int32_t maxVal = (1 << img->nBits) - 1; for (int i = 0; i < lineLength; ++i) { - img->outBufs[plane][rawOffset + 2 * i] = - rtengine::LIM(median + lineData[i], 0, maxVal); + img->outBufs[plane][rawOffset + 2 * i] = rtengine::LIM(median + lineData[i], 0, maxVal); } } else if (img->nPlanes == 1) { - std::int32_t maxVal = (1 << img->nBits) - 1; - std::int32_t median = 1 << (img->nBits - 1); + const std::int32_t maxVal = (1 << img->nBits) - 1; + const std::int32_t median = 1 << (img->nBits - 1); + rawOffset = img->planeWidth * imageRow + imageCol; for (int i = 0; i < lineLength; ++i) { - img->outBufs[0][rawOffset + i] = - rtengine::LIM(median + lineData[i], 0, maxVal); + img->outBufs[0][rawOffset + i] = rtengine::LIM(median + lineData[i], 0, maxVal); } } } else if (img->encType == 3 && img->planeBuf) { - std::int32_t planeSize = img->planeWidth * img->planeHeight; - std::int16_t* plane0 = img->planeBuf + imageRow * img->planeWidth; - std::int16_t* plane1 = plane0 + planeSize; - std::int16_t* plane2 = plane1 + planeSize; - std::int16_t* plane3 = plane2 + planeSize; + const std::int32_t planeSize = img->planeWidth * img->planeHeight; + const std::int16_t* const plane0 = img->planeBuf + imageRow * img->planeWidth; + const std::int16_t* const plane1 = plane0 + planeSize; + const std::int16_t* const plane2 = plane1 + planeSize; + const std::int16_t* const plane3 = plane2 + planeSize; - std::int32_t median = 1 << (img->nBits - 1) << 10; - std::int32_t maxVal = (1 << img->nBits) - 1; - std::uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; + const std::int32_t median = 1 << (img->nBits - 1) << 10; + const std::int32_t maxVal = (1 << img->nBits) - 1; + const std::uint32_t rawLineOffset = 4 * img->planeWidth * imageRow; // for this stage - all except imageRow is ignored for (int i = 0; i < img->planeWidth; ++i) { - std::int32_t gr = - median + (plane0[i] << 10) - 168 * plane1[i] - 585 * plane3[i]; - std::int32_t val = 0; + std::int32_t gr = median + (plane0[i] << 10) - 168 * plane1[i] - 585 * plane3[i]; if (gr < 0) { gr = -(((std::abs(gr) + 512) >> 9) & ~1); @@ -2368,7 +2367,7 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, } // Essentially R = round(median + P0 + 1.474*P3) - val = (median + (plane0[i] << 10) + 1510 * plane3[i] + 512) >> 10; + std::int32_t val = (median + (plane0[i] << 10) + 1510 * plane3[i] + 512) >> 10; img->outBufs[0][rawLineOffset + 2 * i] = rtengine::LIM(val, 0, maxVal); // Essentially G1 = round(median + P0 + P2 - 0.164*P1 - 0.571*P3) val = (plane2[i] + gr + 1) >> 1; @@ -2383,18 +2382,27 @@ void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0, } } -int crxParamInit(CrxBandParam** param, std::uint64_t subbandMdatOffset, - std::uint64_t subbandDataSize, std::uint32_t subbandWidth, - std::uint32_t subbandHeight, std::int32_t supportsPartial, - std::uint32_t roundedBitsMask, LibRaw_abstract_datastream* input) +bool crxParamInit( + CrxBandParam** param, + std::uint64_t subbandMdatOffset, + std::uint64_t subbandDataSize, + std::uint32_t subbandWidth, + std::uint32_t subbandHeight, + std::int32_t supportsPartial, + std::uint32_t roundedBitsMask, + LibRaw_abstract_datastream* input +) { - std::int32_t progrDataSize = supportsPartial ? 0 : sizeof(std::int32_t) * subbandWidth; - std::int32_t paramLength = 2 * subbandWidth + 4; - std::uint8_t* paramBuf = static_cast(calloc( - 1, sizeof(CrxBandParam) + sizeof(std::int32_t) * paramLength + progrDataSize)); + const std::int32_t progrDataSize = + supportsPartial + ? 0 + : sizeof(std::int32_t) * subbandWidth; + const std::int32_t paramLength = 2 * subbandWidth + 4; + + std::uint8_t* paramBuf = static_cast(calloc(1, sizeof(CrxBandParam) + sizeof(std::int32_t) * paramLength + progrDataSize)); if (!paramBuf) { - return -1; + return false; } *param = reinterpret_cast(paramBuf); @@ -2403,7 +2411,9 @@ int crxParamInit(CrxBandParam** param, std::uint64_t subbandMdatOffset, (*param)->paramData = reinterpret_cast(paramBuf); (*param)->nonProgrData = - progrDataSize ? (*param)->paramData + paramLength : nullptr; + progrDataSize + ? (*param)->paramData + paramLength + : nullptr; (*param)->subbandWidth = subbandWidth; (*param)->subbandHeight = subbandHeight; (*param)->roundedBits = 0; @@ -2420,39 +2430,42 @@ int crxParamInit(CrxBandParam** param, std::uint64_t subbandMdatOffset, crxFillBuffer(&(*param)->bitStream); - return 0; + return true; } -int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, - const CrxTile* tile, std::uint32_t mdatOffset) +bool crxSetupSubbandData( + CrxImage* img, + CrxPlaneComp* planeComp, + const CrxTile* tile, + std::uint32_t mdatOffset +) { long compDataSize = 0; long waveletDataOffset = 0; long compCoeffDataOffset = 0; - std::int32_t toSubbands = 3 * img->levels + 1; - std::int32_t transformWidth = 0; + const std::int32_t toSubbands = 3 * img->levels + 1; - CrxSubband* subbands = planeComp->subBands; + CrxSubband* const subbands = planeComp->subBands; // calculate sizes for (std::int32_t subbandNum = 0; subbandNum < toSubbands; ++subbandNum) { - subbands[subbandNum].bandSize = - subbands[subbandNum].width * sizeof(std::int32_t); // 4bytes + subbands[subbandNum].bandSize = subbands[subbandNum].width * sizeof(std::int32_t); // 4 bytes compDataSize += subbands[subbandNum].bandSize; } if (img->levels) { - std::int32_t encLevels = img->levels ? img->levels : 1; + const std::int32_t encLevels = + img->levels + ? img->levels + : 1; waveletDataOffset = (compDataSize + 7) & ~7; - compDataSize = - (sizeof(CrxWaveletTransform) * encLevels + waveletDataOffset + 7) & ~7; + compDataSize = (sizeof(CrxWaveletTransform) * encLevels + waveletDataOffset + 7) & ~7; compCoeffDataOffset = compDataSize; // calc wavelet line buffer sizes (always at one level up from current) for (int level = 0; level < img->levels; ++level) { if (level < img->levels - 1) { - compDataSize += 8 * sizeof(std::int32_t) * - planeComp->subBands[3 * (level + 1) + 2].width; + compDataSize += 8 * sizeof(std::int32_t) * planeComp->subBands[3 * (level + 1) + 2].width; } else { compDataSize += 8 * sizeof(std::int32_t) * tile->width; } @@ -2463,31 +2476,31 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, planeComp->compBuf = static_cast(malloc(compDataSize)); if (!planeComp->compBuf) { - return -1; + return false; } // subbands buffer and sizes initialisation - std::uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; + const std::uint64_t subbandMdatOffset = img->mdatOffset + mdatOffset; std::uint8_t* subbandBuf = planeComp->compBuf; for (std::int32_t subbandNum = 0; subbandNum < toSubbands; ++subbandNum) { subbands[subbandNum].bandBuf = subbandBuf; subbandBuf += subbands[subbandNum].bandSize; - subbands[subbandNum].mdatOffset = - subbandMdatOffset + subbands[subbandNum].dataOffset; + subbands[subbandNum].mdatOffset = subbandMdatOffset + subbands[subbandNum].dataOffset; } // wavelet data initialisation if (img->levels) { - CrxWaveletTransform* waveletTransforms = - reinterpret_cast(planeComp->compBuf + waveletDataOffset); + CrxWaveletTransform* const waveletTransforms = reinterpret_cast(planeComp->compBuf + waveletDataOffset); std::int32_t* paramData = reinterpret_cast(planeComp->compBuf + compCoeffDataOffset); planeComp->waveletTransform = waveletTransforms; waveletTransforms[0].subband0Buf = reinterpret_cast(subbands->bandBuf); for (int level = 0; level < img->levels; ++level) { - std::int32_t band = 3 * level + 1; + const std::int32_t band = 3 * level + 1; + + std::int32_t transformWidth = 0; if (level >= img->levels - 1) { waveletTransforms[level].height = tile->height; @@ -2499,28 +2512,19 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, waveletTransforms[level].width = transformWidth; waveletTransforms[level].lineBuf[0] = paramData; - waveletTransforms[level].lineBuf[1] = - waveletTransforms[level].lineBuf[0] + transformWidth; - waveletTransforms[level].lineBuf[2] = - waveletTransforms[level].lineBuf[1] + transformWidth; - waveletTransforms[level].lineBuf[3] = - waveletTransforms[level].lineBuf[2] + transformWidth; - waveletTransforms[level].lineBuf[4] = - waveletTransforms[level].lineBuf[3] + transformWidth; - waveletTransforms[level].lineBuf[5] = - waveletTransforms[level].lineBuf[4] + transformWidth; - waveletTransforms[level].lineBuf[6] = - waveletTransforms[level].lineBuf[5] + transformWidth; - waveletTransforms[level].lineBuf[7] = - waveletTransforms[level].lineBuf[6] + transformWidth; + waveletTransforms[level].lineBuf[1] = waveletTransforms[level].lineBuf[0] + transformWidth; + waveletTransforms[level].lineBuf[2] = waveletTransforms[level].lineBuf[1] + transformWidth; + waveletTransforms[level].lineBuf[3] = waveletTransforms[level].lineBuf[2] + transformWidth; + waveletTransforms[level].lineBuf[4] = waveletTransforms[level].lineBuf[3] + transformWidth; + waveletTransforms[level].lineBuf[5] = waveletTransforms[level].lineBuf[4] + transformWidth; + waveletTransforms[level].lineBuf[6] = waveletTransforms[level].lineBuf[5] + transformWidth; + waveletTransforms[level].lineBuf[7] = waveletTransforms[level].lineBuf[6] + transformWidth; waveletTransforms[level].curLine = 0; waveletTransforms[level].curH = 0; waveletTransforms[level].fltTapH = 0; waveletTransforms[level].subband1Buf = reinterpret_cast(subbands[band].bandBuf); - waveletTransforms[level].subband2Buf = - reinterpret_cast(subbands[band + 1].bandBuf); - waveletTransforms[level].subband3Buf = - reinterpret_cast(subbands[band + 2].bandBuf); + waveletTransforms[level].subband2Buf = reinterpret_cast(subbands[band + 1].bandBuf); + waveletTransforms[level].subband3Buf = reinterpret_cast(subbands[band + 2].bandBuf); paramData = waveletTransforms[level].lineBuf[7] + transformWidth; } @@ -2537,71 +2541,73 @@ int crxSetupSubbandData(CrxImage* img, CrxPlaneComp* planeComp, supportsPartial = 1; } - if (crxParamInit(&subbands[subbandNum].bandParam, - subbands[subbandNum].mdatOffset, - subbands[subbandNum].dataSize, - subbands[subbandNum].width, subbands[subbandNum].height, - supportsPartial, roundedBitsMask, img->input)) { - return -1; + if ( + !crxParamInit( + &subbands[subbandNum].bandParam, + subbands[subbandNum].mdatOffset, + subbands[subbandNum].dataSize, + subbands[subbandNum].width, + subbands[subbandNum].height, + supportsPartial, + roundedBitsMask, + img->input + ) + ) { + return false; } } } - return 0; + return true; } -} // namespace -int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) +} // namespace + +bool DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) { - CrxImage* img = static_cast(p); + CrxImage* const img = static_cast(p); int imageRow = 0; for (int tRow = 0; tRow < img->tileRows; ++tRow) { int imageCol = 0; for (int tCol = 0; tCol < img->tileCols; ++tCol) { - CrxTile* tile = img->tiles + tRow * img->tileRows + tCol; - CrxPlaneComp* planeComp = tile->comps + planeNumber; - std::uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; + const CrxTile* const tile = img->tiles + tRow * img->tileRows + tCol; + CrxPlaneComp* const planeComp = tile->comps + planeNumber; + const std::uint64_t tileMdatOffset = tile->dataOffset + planeComp->dataOffset; // decode single tile - if (crxSetupSubbandData(img, planeComp, tile, tileMdatOffset)) { - return -1; + if (!crxSetupSubbandData(img, planeComp, tile, tileMdatOffset)) { + return false; } if (img->levels) { if (!crxIdwt53FilterInitialize(planeComp, img->levels - 1)) { - return -1; + return false; } for (int i = 0; i < tile->height; ++i) { - if (!crxIdwt53FilterDecode(planeComp, img->levels - 1) || - !crxIdwt53FilterTransform(planeComp, img->levels - 1)) { - return -1; + if (!crxIdwt53FilterDecode(planeComp, img->levels - 1) || !crxIdwt53FilterTransform(planeComp, img->levels - 1)) { + return false; } - std::int32_t* lineData = - crxIdwt53FilterGetLine(planeComp, img->levels - 1); - crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, - lineData, tile->width); + const std::int32_t* const lineData = crxIdwt53FilterGetLine(planeComp, img->levels - 1); + crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, lineData, tile->width); } } else { // we have the only subband in this case if (!planeComp->subBands->dataSize) { - memset(planeComp->subBands->bandBuf, 0, - planeComp->subBands->bandSize); - return 0; + memset(planeComp->subBands->bandBuf, 0, planeComp->subBands->bandSize); + return true; } for (int i = 0; i < tile->height; ++i) { - if (!crxDecodeLine(planeComp->subBands->bandParam, - planeComp->subBands->bandBuf)) { - return -1; + if (!crxDecodeLine(planeComp->subBands->bandParam, planeComp->subBands->bandBuf)) { + return false; } - std::int32_t* lineData = reinterpret_cast(planeComp->subBands->bandBuf); - crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, - lineData, tile->width); + const std::int32_t* const lineData = reinterpret_cast(planeComp->subBands->bandBuf); + crxConvertPlaneLine(img, imageRow + i, imageCol, planeNumber, lineData, tile->width); } } @@ -2611,7 +2617,7 @@ int DCraw::crxDecodePlane(void* p, std::uint32_t planeNumber) imageRow += img->tiles[tRow * img->tileRows].height; } - return 0; + return true; } namespace @@ -2619,9 +2625,13 @@ namespace using crx_data_header_t = DCraw::CanonCR3Data::crx_data_header_t; -int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, - CrxPlaneComp* comp, std::uint8_t** subbandMdatPtr, - std::uint32_t* mdatSize) +bool crxReadSubbandHeaders( + CrxImage* img, + CrxTile* tile, + CrxPlaneComp* comp, + std::uint8_t** subbandMdatPtr, + std::uint32_t* mdatSize +) { CrxSubband* band = comp->subBands + img->subbandCount - 1; // set to last band std::uint32_t bandHeight = tile->height; @@ -2635,14 +2645,12 @@ int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, // Coefficient structure is a bit unclear and convoluted: // 3 levels max - 8 groups (for tile width rounded to 8 bytes) // of 3 band per level 4 sets of coefficients for each - const std::int32_t* rowExCoef = - exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->width & 7); - const std::int32_t* colExCoef = - exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->height & 7); + const std::int32_t* rowExCoef = exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->width & 7); + const std::int32_t* colExCoef = exCoefNumTbl + 0x60 * (img->levels - 1) + 12 * (tile->height & 7); for (int level = 0; level < img->levels; ++level) { - std::int32_t widthOddPixel = bandWidth & 1; - std::int32_t heightOddPixel = bandHeight & 1; + const std::int32_t widthOddPixel = bandWidth & 1; + const std::int32_t heightOddPixel = bandHeight & 1; bandWidth = (widthOddPixel + bandWidth) >> 1; bandHeight = (heightOddPixel + bandHeight) >> 1; @@ -2683,18 +2691,15 @@ int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, band -= 3; } - bandWidthExCoef = bandHeightExCoef = 0; + bandWidthExCoef = 0; + bandHeightExCoef = 0; if (tile->tileFlag & E_HAS_TILES_ON_THE_RIGHT) { - bandWidthExCoef = - exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->width & 7) + - 4 * (img->levels - 1) + 1]; + bandWidthExCoef = exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->width & 7) + 4 * (img->levels - 1) + 1]; } if (tile->tileFlag & E_HAS_TILES_ON_THE_BOTTOM) { - bandHeightExCoef = - exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->height & 7) + - 4 * (img->levels - 1) + 1]; + bandHeightExCoef = exCoefNumTbl[0x60 * (img->levels - 1) + 12 * (tile->height & 7) + 4 * (img->levels - 1) + 1]; } } @@ -2702,7 +2707,7 @@ int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, band->height = bandHeightExCoef + bandHeight; if (!img->subbandCount) { - return 0; + return true; } std::int32_t subbandOffset = 0; @@ -2710,19 +2715,19 @@ int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, for (unsigned int curSubband = 0; curSubband < img->subbandCount; curSubband++, band++) { if (*mdatSize < 0xC) { - return -1; + return false; } if (sgetn(2, *subbandMdatPtr) != 0xFF03) { - return -1; + return false; } - std::uint32_t bitData = sgetn(4, *subbandMdatPtr + 8); - std::uint32_t subbandSize = sgetn(4, *subbandMdatPtr + 4); + const std::uint32_t bitData = sgetn(4, *subbandMdatPtr + 8); + const std::uint32_t subbandSize = sgetn(4, *subbandMdatPtr + 4); if (curSubband != bitData >> 28) { band->dataSize = subbandSize; - return -1; + return false; } band->dataSize = subbandSize - (bitData & 0x7FF); @@ -2740,32 +2745,39 @@ int crxReadSubbandHeaders(CrxImage* img, CrxTile* tile, *mdatSize -= 0xC; } - return 0; + return true; } -int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mdatPtr, - std::uint32_t mdatSize) +bool crxReadImageHeaders( + crx_data_header_t* hdr, + CrxImage* img, + std::uint8_t* mdatPtr, + std::uint32_t mdatSize +) { - unsigned int nTiles = img->tileRows * img->tileCols; + const unsigned int nTiles = img->tileRows * img->tileCols; if (!nTiles) { - return -1; + return false; } if (!img->tiles) { - img->tiles = static_cast(malloc( - sizeof(CrxTile) * nTiles + - sizeof(CrxPlaneComp) * nTiles * img->nPlanes + - sizeof(CrxSubband) * nTiles * img->nPlanes * img->subbandCount)); + img->tiles = static_cast( + malloc( + sizeof(CrxTile) * nTiles + + sizeof(CrxPlaneComp) * nTiles * img->nPlanes + + sizeof(CrxSubband) * nTiles * img->nPlanes * img->subbandCount + ) + ); if (!img->tiles) { - return -1; + return false; } // memory areas in allocated chunk CrxTile* tile = img->tiles; - CrxPlaneComp* comps = reinterpret_cast(tile + nTiles); - CrxSubband* bands = reinterpret_cast(comps + img->nPlanes * nTiles); + CrxPlaneComp* const comps = reinterpret_cast(tile + nTiles); + CrxSubband* const bands = reinterpret_cast(comps + img->nPlanes * nTiles); for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { tile->tileFlag = 0; // tile neighbouring flags @@ -2827,8 +2839,7 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda comp->waveletTransform = nullptr; if (img->subbandCount) { - for (int curBand = 0; curBand < img->subbandCount; - curBand++, band++) { + for (int curBand = 0; curBand < img->subbandCount; curBand++, band++) { band->supportsPartial = 0; band->quantValue = 4; band->bandParam = nullptr; @@ -2847,15 +2858,15 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda for (unsigned int curTile = 0; curTile < nTiles; curTile++, tile++) { if (dataSize < 0xC) { - return -1; + return false; } if (sgetn(2, dataPtr) != 0xFF01) { - return -1; + return false; } if (sgetn(2, dataPtr + 8) != curTile) { - return -1; + return false; } dataSize -= 0xC; @@ -2863,7 +2874,7 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda tile->tileSize = sgetn(4, dataPtr + 4); tile->dataOffset = tileOffset; - std::int32_t hdrExtraBytes = sgetn(2, dataPtr + 2) - 8; + const std::int32_t hdrExtraBytes = sgetn(2, dataPtr + 2) - 8; tileOffset += tile->tileSize; dataPtr += hdrExtraBytes + 0xC; dataSize -= hdrExtraBytes; @@ -2873,15 +2884,15 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda for (int compNum = 0; compNum < img->nPlanes; compNum++, comp++) { if (dataSize < 0xC) { - return -1; + return false; } if (sgetn(2, dataPtr) != 0xFF02) { - return -1; + return false; } if (compNum != dataPtr[8] >> 4) { - return -1; + return false; } comp->compSize = sgetn(4, dataPtr + 4); @@ -2900,42 +2911,52 @@ int crxReadImageHeaders(crx_data_header_t* hdr, CrxImage* img, std::uint8_t* mda if (compHdrRoundedBits) { if (img->levels || !comp->supportsPartial) { - return -1; + return false; } comp->roundedBitsMask = 1 << (compHdrRoundedBits - 1); } - if (crxReadSubbandHeaders(img, tile, comp, &dataPtr, &dataSize)) { - return -1; + if (!crxReadSubbandHeaders(img, tile, comp, &dataPtr, &dataSize)) { + return false; } } } - return 0; + return true; } -int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, std::int16_t* outBuf, - std::uint64_t mdatOffset, std::uint32_t mdatSize, - std::uint8_t* mdatHdrPtr) +bool crxSetupImageData( + crx_data_header_t* hdr, + CrxImage* img, + std::int16_t* outBuf, + std::uint64_t mdatOffset, + std::uint32_t mdatSize, + std::uint8_t* mdatHdrPtr +) { - int IncrBitTable[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, - 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0 - }; + constexpr bool IncrBitTable[32] = { + false, false, false, false, false, false, false, false, false, true, true, false, false, false, true, false, + false, false, true, false, false, true, true, true, false, true, true, true, false, false, false, false + }; img->planeWidth = hdr->f_width; img->planeHeight = hdr->f_height; - if (hdr->tileWidth < 0x16 || hdr->tileHeight < 0x16 || - img->planeWidth > 0x7FFF || img->planeHeight > 0x7FFF) { - return -1; + if ( + hdr->tileWidth < 0x16 + || hdr->tileHeight < 0x16 + || img->planeWidth > 0x7FFF + || img->planeHeight > 0x7FFF + ) { + return false; } img->tileCols = (img->planeWidth + hdr->tileWidth - 1) / hdr->tileWidth; img->tileRows = (img->planeHeight + hdr->tileHeight - 1) / hdr->tileHeight; if (img->planeWidth - hdr->tileWidth * (img->tileCols - 1) < 0x16 || img->planeHeight - hdr->tileHeight * (img->tileRows - 1) < 0x16) { - return -1; + return false; } img->tiles = nullptr; @@ -2957,16 +2978,16 @@ int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, std::int16_t* outBu // intermediate plane buffer. At the moment though it's too many changes so // left as is. if (img->encType == 3 && img->nPlanes == 4 && img->nBits > 8) { - img->planeBuf = - static_cast(malloc(img->planeHeight * img->planeWidth * img->nPlanes * - ((img->samplePrecision + 7) >> 3))); + img->planeBuf = static_cast( + malloc(img->planeHeight * img->planeWidth * img->nPlanes * ((img->samplePrecision + 7) >> 3)) + ); if (!img->planeBuf) { - return -1; + return false; } } - std::int32_t rowSize = 2 * img->planeWidth; + const std::int32_t rowSize = 2 * img->planeWidth; if (img->nPlanes == 1) { img->outBufs[0] = outBuf; @@ -3018,10 +3039,10 @@ int crxSetupImageData(crx_data_header_t* hdr, CrxImage* img, std::int16_t* outBu return crxReadImageHeaders(hdr, img, mdatHdrPtr, mdatSize); } -int crxFreeImageData(CrxImage* img) +void crxFreeImageData(CrxImage* img) { CrxTile* tile = img->tiles; - int nTiles = img->tileRows * img->tileCols; + const int nTiles = img->tileRows * img->tileCols; if (img->tiles) { for (std::int32_t curTile = 0; curTile < nTiles; curTile++, tile++) { @@ -3040,15 +3061,14 @@ int crxFreeImageData(CrxImage* img) free(img->planeBuf); img->planeBuf = nullptr; } - - return 0; } + } // namespace void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) { #ifdef _OPENMP - int results[4]; // nPlanes is always <= 4 + bool results[4]; // nPlanes is always <= 4 #pragma omp parallel for for (std::int32_t plane = 0; plane < nPlanes; ++plane) { @@ -3056,7 +3076,7 @@ void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) } for (std::int32_t plane = 0; plane < nPlanes; ++plane) { - if (results[plane]) { + if (!results[plane]) { derror(); } } @@ -3064,7 +3084,7 @@ void DCraw::crxLoadDecodeLoop(void* img, int nPlanes) #else for (std::int32_t plane = 0; plane < nPlanes; ++plane) { - if (crxDecodePlane(img, plane)) { + if (!crxDecodePlane(img, plane)) { derror(); } } @@ -3092,14 +3112,11 @@ void DCraw::crxLoadRaw() { CrxImage img; - if (RT_canon_CR3_data.crx_track_selected >= - RT_canon_CR3_data.CRXTRACKS_MAXCOUNT) { + if (RT_canon_CR3_data.crx_track_selected >= RT_canon_CR3_data.CRXTRACKS_MAXCOUNT) { derror(); } - crx_data_header_t hdr = - RT_canon_CR3_data - .crx_header[RT_canon_CR3_data.crx_track_selected]; + crx_data_header_t hdr = RT_canon_CR3_data.crx_header[RT_canon_CR3_data.crx_track_selected]; LibRaw_abstract_datastream input = {ifp}; img.input = &input; // libraw_internal_data.internal_data.input; @@ -3114,7 +3131,7 @@ void DCraw::crxLoadRaw() // /*imgdata.color.*/maximum = (1 << hdr.nBits) - 1; - std::uint8_t* hdrBuf = static_cast(malloc(hdr.mdatHdrSize)); + std::uint8_t* const hdrBuf = static_cast(malloc(hdr.mdatHdrSize)); // read image header #ifdef _OPENMP @@ -3124,8 +3141,7 @@ void DCraw::crxLoadRaw() #ifndef _OPENMP /*libraw_internal_data.internal_data.input->*/ input.lock(); #endif - /*libraw_internal_data.internal_data.input->*/ input.seek( - data_offset, SEEK_SET); + /*libraw_internal_data.internal_data.input->*/ input.seek(data_offset, SEEK_SET); /*libraw_internal_data.internal_data.input->*/ input.read(hdrBuf, 1, hdr.mdatHdrSize); #ifndef _OPENMP /*libraw_internal_data.internal_data.input->*/ input.unlock(); @@ -3133,9 +3149,7 @@ void DCraw::crxLoadRaw() } // parse and setup the image data - if (crxSetupImageData(&hdr, &img, reinterpret_cast(raw_image), - hdr.MediaOffset /*data_offset*/, - hdr.MediaSize /*RT_canon_CR3_data.data_size*/, hdrBuf)) { + if (!crxSetupImageData(&hdr, &img, reinterpret_cast(raw_image), hdr.MediaOffset /*data_offset*/, hdr.MediaSize /*RT_canon_CR3_data.data_size*/, hdrBuf)) { derror(); } @@ -3150,18 +3164,17 @@ void DCraw::crxLoadRaw() crxFreeImageData(&img); } -int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) +bool DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) { if (nTrack >= RT_canon_CR3_data.CRXTRACKS_MAXCOUNT) { - return -1; + return false; } if (!cmp1TagData) { - return -1; + return false; } - crx_data_header_t* hdr = - &RT_canon_CR3_data.crx_header[nTrack]; + crx_data_header_t* const hdr = &RT_canon_CR3_data.crx_header[nTrack]; hdr->version = sgetn(2, cmp1TagData + 4); hdr->f_width = sgetn(4, cmp1TagData + 8); @@ -3179,45 +3192,55 @@ int DCraw::crxParseImageHeader(uchar* cmp1TagData, unsigned int nTrack) // validation if (hdr->version != 0x100 || !hdr->mdatHdrSize) { - return -1; + return false; } if (hdr->encType == 1) { if (hdr->nBits > 15) { - return -1; + return false; } } else { if (hdr->encType && hdr->encType != 3) { - return -1; + return false; } if (hdr->nBits > 14) { - return -1; + return false; } } if (hdr->nPlanes == 1) { if (hdr->cfaLayout || hdr->encType) { - return -1; + return false; } if (hdr->nBits != 8) { - return -1; + return false; } - } else if (hdr->nPlanes != 4 || (hdr->f_width & 1) || (hdr->f_height & 1) || - (hdr->tileWidth & 1) || (hdr->tileHeight & 1) || hdr->cfaLayout > 3 || - (hdr->encType && hdr->encType != 1 && hdr->encType != 3) || - hdr->nBits == 8) { - return -1; + } else if ( + hdr->nPlanes != 4 + || (hdr->f_width & 1) + || (hdr->f_height & 1) + || (hdr->tileWidth & 1) + || (hdr->tileHeight & 1) + || hdr->cfaLayout > 3 + || ( + hdr->encType + && hdr->encType != 1 + && hdr->encType != 3 + ) + || hdr->nBits == 8 + ) { + return false; } if (hdr->tileWidth > hdr->f_width || hdr->tileHeight > hdr->f_height) { - return -1; + return false; } if (hdr->imageLevels > 3 || hdr->hasTileCols > 1 || hdr->hasTileRows > 1) { - return -1; + return false; } - return 0; + return true; } diff --git a/rtengine/dcraw.h b/rtengine/dcraw.h index c18e6c2ff..a47d1c88a 100644 --- a/rtengine/dcraw.h +++ b/rtengine/dcraw.h @@ -567,12 +567,12 @@ void selectCRXTrack(unsigned short maxTrack); int parseCR3(unsigned long long oAtomList, unsigned long long szAtomList, short &nesting, char *AtomNameStack, unsigned short &nTrack, short &TrackType); -int crxDecodePlane(void *p, uint32_t planeNumber); +bool crxDecodePlane(void *p, uint32_t planeNumber); void crxLoadDecodeLoop(void *img, int nPlanes); void crxConvertPlaneLineDf(void *p, int imageRow); void crxLoadFinalizeLoopE3(void *p, int planeHeight); void crxLoadRaw(); -int crxParseImageHeader(uchar *cmp1TagData, unsigned int nTrack); +bool crxParseImageHeader(uchar *cmp1TagData, unsigned int nTrack); //----------------------------------------------------------------------------- }; From 03c094a42bcf9d6d7a0198eadc758b9fc32e38fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Fri, 6 Dec 2019 09:18:16 +0100 Subject: [PATCH 17/20] Fix `fseek()`/`ftell()` issue --- rtengine/canon_cr3_decoder.cc | 8 ++++---- rtengine/dcraw.h | 2 +- rtengine/myfile.h | 10 ++++------ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 3912af6b4..14a7f0e14 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -449,9 +449,9 @@ int DCraw::parseCR3( goto fin; } - const std::int64_t off = ftell(ifp); // FIXME: ftell() returns int + const long off = ftell(ifp); parse_gps(oAtomContent); - fseek(ifp, off, SEEK_SET); // FIXME: fseek() takes int offset + fseek(ifp, off, SEEK_SET); // parse_gps_libraw(oAtomContent); order = q_order; } else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) { @@ -468,7 +468,7 @@ int DCraw::parseCR3( } } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsd")) { if (szAtomContent >= 16) { - fseek(ifp, 12L, SEEK_CUR); + fseek(ifp, 12, SEEK_CUR); lHdr = 8; } else { err = -7; @@ -670,7 +670,7 @@ struct LibRaw_abstract_datastream { void unlock() { } - void seek(int p, int how) + void seek(long p, int how) { fseek(ifp, p, how); } diff --git a/rtengine/dcraw.h b/rtengine/dcraw.h index a47d1c88a..89c1fcaff 100644 --- a/rtengine/dcraw.h +++ b/rtengine/dcraw.h @@ -184,7 +184,7 @@ public: int32_t mdatHdrSize; // Not from header, but from datastream uint32_t MediaSize; - INT64 MediaOffset; + int64_t MediaOffset; uint32_t MediaType; /* 1 -> /C/RAW, 2-> JPEG */ }; static constexpr size_t CRXTRACKS_MAXCOUNT = 16; diff --git a/rtengine/myfile.h b/rtengine/myfile.h index 7c498e556..423edea9a 100644 --- a/rtengine/myfile.h +++ b/rtengine/myfile.h @@ -56,28 +56,26 @@ IMFILE* fopen (const char* fname); IMFILE* gfopen (const char* fname); IMFILE* fopen (unsigned* buf, int size); void fclose (IMFILE* f); -inline int ftell (IMFILE* f) +inline long ftell (IMFILE* f) { - return f->pos; } inline int feof (IMFILE* f) { - return f->eof; } -inline void fseek (IMFILE* f, int p, int how) +inline void fseek (IMFILE* f, long p, int how) { - int fpos = f->pos; + ssize_t fpos = f->pos; if (how == SEEK_SET) { f->pos = p; } else if (how == SEEK_CUR) { f->pos += p; } else if (how == SEEK_END) { - if(p <= 0 && -p <= f->size) { + if (p <= 0 && -p <= f->size) { f->pos = f->size + p; } return; From b0e52b32023524e2be22ebb03fd465af0f9e05d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Fri, 6 Dec 2019 09:28:34 +0100 Subject: [PATCH 18/20] Whitespace corrections --- rtengine/canon_cr3_decoder.cc | 68 +++++++++++++++++------------------ 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 14a7f0e14..ba92bf04a 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -87,13 +87,13 @@ void DCraw::selectCRXTrack(unsigned short maxTrack) for (unsigned int i = 0; i <= maxTrack && i < RT_canon_CR3_data.CRXTRACKS_MAXCOUNT; ++i) { CanonCR3Data::crx_data_header_t* const d = &RT_canon_CR3_data.crx_header[i]; - if (d->MediaType == 1) {// RAW + if (d->MediaType == 1) { // RAW bitcounts[i] = std::int64_t(d->nBits) * std::int64_t(d->f_width) * std::int64_t(d->f_height); if (bitcounts[i] > maxbitcount) { maxbitcount = bitcounts[i]; } - } else if (d->MediaType == 2) {// JPEG + } else if (d->MediaType == 2) { // JPEG if (d->MediaSize > maxjpegbytes) { maxjpegbytes = d->MediaSize; thumb_offset = d->MediaOffset; @@ -371,7 +371,7 @@ int DCraw::parseCR3( szAtom = szAtomList - oAtom; oAtomContent = oAtom + 8; szAtomContent = szAtom - 8; - } else if (szAtom == 1) { + } else if (szAtom == 1) { if ((oAtom + 16) > (oAtomList + szAtomList)) { err = -3; goto fin; @@ -404,9 +404,9 @@ int DCraw::parseCR3( } else { fseek(ifp, -lHdr, SEEK_CUR); } - } else if (!strcmp(AtomNameStack, "moovuuidCCTP")) { + } else if (!strcmp(AtomNameStack, "moovuuidCCTP")) { lHdr = 12; - } else if (!strcmp(AtomNameStack, "moovuuidCMT1")) { + } else if (!strcmp(AtomNameStack, "moovuuidCMT1")) { const short q_order = order; order = get2(); @@ -417,7 +417,7 @@ int DCraw::parseCR3( parse_tiff_ifd(oAtomContent); order = q_order; - } else if (!strcmp(AtomNameStack, "moovuuidCMT2")) { + } else if (!strcmp(AtomNameStack, "moovuuidCMT2")) { const short q_order = order; order = get2(); @@ -428,7 +428,7 @@ int DCraw::parseCR3( parse_exif(oAtomContent); order = q_order; - } else if (!strcmp(AtomNameStack, "moovuuidCMT3")) { + } else if (!strcmp(AtomNameStack, "moovuuidCMT3")) { const short q_order = order; order = get2(); @@ -440,7 +440,7 @@ int DCraw::parseCR3( fseek(ifp, -12L, SEEK_CUR); parse_makernote(oAtomContent, 0); order = q_order; - } else if (!strcmp(AtomNameStack, "moovuuidCMT4")) { + } else if (!strcmp(AtomNameStack, "moovuuidCMT4")) { const short q_order = order; order = get2(); @@ -454,7 +454,7 @@ int DCraw::parseCR3( fseek(ifp, off, SEEK_SET); // parse_gps_libraw(oAtomContent); order = q_order; - } else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiahdlr")) { fseek(ifp, 8, SEEK_CUR); for (unsigned int c = 0; c < 4; ++c) { HandlerType[c] = fgetc(ifp); @@ -466,7 +466,7 @@ int DCraw::parseCR3( break; } } - } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsd")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsd")) { if (szAtomContent >= 16) { fseek(ifp, 12, SEEK_CUR); lHdr = 8; @@ -493,9 +493,9 @@ int DCraw::parseCR3( /*ImageWidth = */ get2(); /*ImageHeight = */ get2(); - } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAW")) { lHdr = 82; - } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWCMP1")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWCMP1")) { if (szAtomContent >= 40) { fread(CMP1, 1, 36, ifp); } else { @@ -506,9 +506,9 @@ int DCraw::parseCR3( if (crxParseImageHeader(CMP1, nTrack)) { RT_canon_CR3_data.crx_header[nTrack].MediaType = 1; } - } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWJPEG")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsdCRAWJPEG")) { RT_canon_CR3_data.crx_header[nTrack].MediaType = 2; - } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsz")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblstsz")) { if (szAtomContent == 12) { fseek(ifp, 4, SEEK_CUR); } else if (szAtomContent == 16) { @@ -519,7 +519,7 @@ int DCraw::parseCR3( } RT_canon_CR3_data.crx_header[nTrack].MediaSize = get4(); - } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblco64")) { + } else if (!strcmp(AtomNameStack, "moovtrakmdiaminfstblco64")) { if (szAtomContent == 16) { fseek(ifp, 8, SEEK_CUR); } else { @@ -771,7 +771,7 @@ struct CrxImage { CrxTile* tiles; std::uint64_t mdatOffset; std::uint64_t mdatSize; - std::int16_t* outBufs[4];// one per plane + std::int16_t* outBufs[4]; // one per plane std::int16_t* planeBuf; LibRaw_abstract_datastream* input; }; @@ -931,7 +931,7 @@ inline std::uint32_t crxBitstreamGetBits(CrxBitstream* bitStrm, int bits) } while (bitsLeft < bits); } - result = bitData >> (32 - bits);// 32-bits + result = bitData >> (32 - bits); // 32-bits bitStrm->bitData = bitData << bits; bitStrm->bitsLeft = bitsLeft - bits; return result; @@ -1182,7 +1182,7 @@ bool crxDecodeLineRounded(CrxBandParam* param) crxDecodeSymbolL1Rounded(param, false); ++param->lineBuf0; valueReached = std::abs(param->lineBuf0[1] - param->lineBuf0[0]) > param->roundedBitsMask; - } else if (length == 1) { + } else if (length == 1) { crxDecodeSymbolL1Rounded(param, false, false); } } @@ -1259,7 +1259,7 @@ bool crxDecodeLineNoRefPrevLine(CrxBandParam* param) return false; } } - } else if (i > param->subbandWidth) { + } else if (i > param->subbandWidth) { return false; } @@ -1688,7 +1688,7 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; } - } else if (!param->supportsPartial) { + } else if (!param->supportsPartial) { const std::int32_t lineLength = param->subbandWidth + 2; param->lineBuf2 = param->nonProgrData; @@ -1708,7 +1708,7 @@ bool crxDecodeLine(CrxBandParam* param, std::uint8_t* bandBuf) memcpy(bandBuf, lineBuf, param->subbandWidth * sizeof(std::int32_t)); ++param->curLine; - } else if (param->roundedBitsMask <= 0) { + } else if (param->roundedBitsMask <= 0) { const std::int32_t lineLength = param->subbandWidth + 2; if (param->curLine & 1) { @@ -1769,7 +1769,7 @@ bool crxDecodeLineWithIQuantization(CrxSubband* subband) bitCode = crxBitstreamGetBits(&subband->bandParam->bitStream, subband->paramK) | (bitCode << subband->paramK); } - subband->quantValue += -(bitCode & 1) ^ (bitCode >> 1);// converting encoded to signed integer + subband->quantValue += -(bitCode & 1) ^ (bitCode >> 1); // converting encoded to signed integer subband->paramK = crxPredictKParameter(subband->paramK, bitCode); if (subband->paramK > 7) { @@ -1859,7 +1859,7 @@ void crxHorizontal53( lineBufLA[2] = deltaA; lineBufLB[2] = deltaB; } - } else if (wavelet->width & 1) { + } else if (wavelet->width & 1) { lineBufLA[1] = band1Buf[0] + ((lineBufLA[0] + band0Buf[0] - ((band1Buf[0] + 1) >> 1)) >> 1); lineBufLA[2] = band0Buf[0] - ((band1Buf[0] + 1) >> 1); @@ -1895,7 +1895,7 @@ bool crxIdwt53FilterDecode(CrxPlaneComp* comp, std::int32_t level) if (!crxIdwt53FilterDecode(comp, level - 1)) { return false; } - } else if (!crxDecodeLineWithIQuantization(sband)) { + } else if (!crxDecodeLineWithIQuantization(sband)) { return false; } @@ -1908,7 +1908,7 @@ bool crxIdwt53FilterDecode(CrxPlaneComp* comp, std::int32_t level) if (!crxIdwt53FilterDecode(comp, level - 1)) { return false; } - } else if (!crxDecodeLineWithIQuantization(sband)) { // LL band + } else if (!crxDecodeLineWithIQuantization(sband)) { // LL band return false; } @@ -1985,7 +1985,7 @@ bool crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) if (wavelet->width & 1) { lineBufL0[2] = delta; } - } else if (wavelet->width & 1) { + } else if (wavelet->width & 1) { const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufL0[1] = band1Buf[0] + ((lineBufL0[0] + delta) >> 1); lineBufL0[2] = delta; @@ -2093,7 +2093,7 @@ bool crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) lineBufL0[2] = deltaA; lineBufL1[2] = deltaB; } - } else if (wavelet->width & 1) { + } else if (wavelet->width & 1) { std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufL0[1] = band1Buf[0] + ((delta + lineBufL0[0]) >> 1); lineBufL0[2] = delta; @@ -2202,7 +2202,7 @@ bool crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) if (wavelet->width & 1) { lineBufL2[2] = delta; } - } else if (wavelet->width & 1) { + } else if (wavelet->width & 1) { const std::int32_t delta = band2Buf[0] - ((band3Buf[0] + 1) >> 1); lineBufL2[1] = band3Buf[0] + ((lineBufL2[0] + delta) >> 1); @@ -2262,7 +2262,7 @@ bool crxIdwt53FilterInitialize(CrxPlaneComp* comp, std::int32_t prevLevel) const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + band1Buf[1] + 2) >> 2); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; - } else if (wavelet->width & 1) { + } else if (wavelet->width & 1) { const std::int32_t delta = band0Buf[0] - ((band1Buf[0] + 1) >> 1); lineBufH0[1] = band1Buf[0] + ((lineBufH0[0] + delta) >> 1); lineBufH0[2] = delta; @@ -2321,21 +2321,21 @@ void crxConvertPlaneLine( for (int i = 0; i < lineLength; ++i) { img->outBufs[plane][rawOffset + 2 * i] = rtengine::LIM(lineData[i], minVal, maxVal - 1); } - } else if (img->encType == 3) { + } else if (img->encType == 3) { // copy to intermediate planeBuf rawOffset = plane * img->planeWidth * img->planeHeight + img->planeWidth * imageRow + imageCol; for (int i = 0; i < lineLength; ++i) { img->planeBuf[rawOffset + i] = lineData[i]; } - } else if (img->nPlanes == 4) { + } else if (img->nPlanes == 4) { const std::int32_t median = 1 << (img->nBits - 1); const std::int32_t maxVal = (1 << img->nBits) - 1; for (int i = 0; i < lineLength; ++i) { img->outBufs[plane][rawOffset + 2 * i] = rtengine::LIM(median + lineData[i], 0, maxVal); } - } else if (img->nPlanes == 1) { + } else if (img->nPlanes == 1) { const std::int32_t maxVal = (1 << img->nBits) - 1; const std::int32_t median = 1 << (img->nBits - 1); @@ -2345,7 +2345,7 @@ void crxConvertPlaneLine( img->outBufs[0][rawOffset + i] = rtengine::LIM(median + lineData[i], 0, maxVal); } } - } else if (img->encType == 3 && img->planeBuf) { + } else if (img->encType == 3 && img->planeBuf) { const std::int32_t planeSize = img->planeWidth * img->planeHeight; const std::int16_t* const plane0 = img->planeBuf + imageRow * img->planeWidth; const std::int16_t* const plane1 = plane0 + planeSize; @@ -2961,7 +2961,7 @@ bool crxSetupImageData( img->tiles = nullptr; img->levels = hdr->imageLevels; - img->subbandCount = 3 * img->levels + 1;// 3 bands per level + one last LL + img->subbandCount = 3 * img->levels + 1; // 3 bands per level + one last LL img->nPlanes = hdr->nPlanes; img->nBits = hdr->nBits; img->encType = hdr->encType; From 743827c7c86f8de14b50ca76b05530e3ea30259f Mon Sep 17 00:00:00 2001 From: Ingo Weyrich Date: Fri, 6 Dec 2019 12:21:19 +0100 Subject: [PATCH 19/20] Fix cppcheck warnings --- rtengine/canon_cr3_decoder.cc | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index ba92bf04a..9e3b3ef4b 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -1004,10 +1004,8 @@ bool crxDecodeLine(CrxBandParam* param) if (param->lineBuf1[0] != param->lineBuf0[1] || param->lineBuf1[0] != param->lineBuf0[2]) { crxDecodeSymbolL1(param, true, true); } else { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) { - nSyms = 1; + int nSyms = 1; while (crxBitstreamGetBits(¶m->bitStream, 1)) { nSyms += JS[param->sParam]; @@ -1338,10 +1336,8 @@ bool crxDecodeTopLine(CrxBandParam* param) if (param->lineBuf1[0]) { param->lineBuf1[1] = param->lineBuf1[0]; } else { - int nSyms = 0; - if (crxBitstreamGetBits(¶m->bitStream, 1)) { - nSyms = 1; + int nSyms = 1; while (crxBitstreamGetBits(¶m->bitStream, 1)) { nSyms += JS[param->sParam]; @@ -1781,7 +1777,7 @@ bool crxDecodeLineWithIQuantization(CrxSubband* subband) return false; } - if (subband->width <= 0) { + if (subband->width == 0) { return true; } @@ -2039,7 +2035,6 @@ bool crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) std::int32_t* lineBufL0 = wavelet->lineBuf[0]; std::int32_t* lineBufL1 = wavelet->lineBuf[1]; - const std::int32_t* lineBufL2 = wavelet->lineBuf[2]; const std::int32_t* const lineBufH0 = wavelet->lineBuf[wavelet->fltTapH + 3]; std::int32_t* const lineBufH1 = wavelet->lineBuf[(wavelet->fltTapH + 1) % 5 + 3]; std::int32_t* const lineBufH2 = wavelet->lineBuf[(wavelet->fltTapH + 2) % 5 + 3]; @@ -2110,7 +2105,7 @@ bool crxIdwt53FilterTransform(CrxPlaneComp* comp, std::uint32_t level) // process H bands lineBufL0 = wavelet->lineBuf[0]; lineBufL1 = wavelet->lineBuf[1]; - lineBufL2 = wavelet->lineBuf[2]; + const std::int32_t* lineBufL2 = wavelet->lineBuf[2]; for (std::int32_t i = 0; i < wavelet->width; ++i) { const std::int32_t delta = lineBufL0[i] - ((lineBufL2[i] + lineBufL1[i] + 2) >> 2); @@ -2731,7 +2726,7 @@ bool crxReadSubbandHeaders( } band->dataSize = subbandSize - (bitData & 0x7FF); - band->supportsPartial = bitData & 0x8000 ? 1 : 0; + band->supportsPartial = (bitData & 0x8000) ? 1 : 0; band->dataOffset = subbandOffset; band->quantValue = (bitData >> 19) & 0xFF; band->paramK = 0; From b1bce98c069959564382a9882c89d32176ccfdd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Sat, 7 Dec 2019 09:38:18 +0100 Subject: [PATCH 20/20] `supportsPartial` is `bool` --- rtengine/canon_cr3_decoder.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rtengine/canon_cr3_decoder.cc b/rtengine/canon_cr3_decoder.cc index 9e3b3ef4b..6274154cb 100644 --- a/rtengine/canon_cr3_decoder.cc +++ b/rtengine/canon_cr3_decoder.cc @@ -705,7 +705,7 @@ struct CrxBandParam { std::int32_t kParam; std::int32_t* paramData; std::int32_t* nonProgrData; - std::int8_t supportsPartial; + bool supportsPartial; }; struct CrxWaveletTransform { @@ -727,7 +727,7 @@ struct CrxSubband { std::uint8_t* bandBuf; std::int32_t bandSize; std::uint64_t dataSize; - std::int8_t supportsPartial; + bool supportsPartial; std::int32_t quantValue; std::uint16_t width; std::uint16_t height; @@ -742,7 +742,7 @@ struct CrxPlaneComp { std::int8_t compNumber; std::int64_t dataOffset; std::int32_t compSize; - std::int8_t supportsPartial; + bool supportsPartial; std::int32_t roundedBitsMask; std::int8_t tileFlag; }; @@ -2383,7 +2383,7 @@ bool crxParamInit( std::uint64_t subbandDataSize, std::uint32_t subbandWidth, std::uint32_t subbandHeight, - std::int32_t supportsPartial, + bool supportsPartial, std::uint32_t roundedBitsMask, LibRaw_abstract_datastream* input ) @@ -2528,12 +2528,12 @@ bool crxSetupSubbandData( // decoding params and bitstream initialisation for (std::int32_t subbandNum = 0; subbandNum < toSubbands; ++subbandNum) { if (subbands[subbandNum].dataSize) { - std::int32_t supportsPartial = 0; + bool supportsPartial = false; std::uint32_t roundedBitsMask = 0; if (planeComp->supportsPartial && subbandNum == 0) { roundedBitsMask = planeComp->roundedBitsMask; - supportsPartial = 1; + supportsPartial = true; } if ( @@ -2726,7 +2726,7 @@ bool crxReadSubbandHeaders( } band->dataSize = subbandSize - (bitData & 0x7FF); - band->supportsPartial = (bitData & 0x8000) ? 1 : 0; + band->supportsPartial = bitData & 0x8000; band->dataOffset = subbandOffset; band->quantValue = (bitData >> 19) & 0xFF; band->paramK = 0; @@ -2827,7 +2827,7 @@ bool crxReadImageHeaders( for (int curComp = 0; curComp < img->nPlanes; curComp++, comp++) { comp->compNumber = curComp; - comp->supportsPartial = 1; + comp->supportsPartial = true; comp->tileFlag = tile->tileFlag; comp->subBands = band; comp->compBuf = nullptr; @@ -2835,7 +2835,7 @@ bool crxReadImageHeaders( if (img->subbandCount) { for (int curBand = 0; curBand < img->subbandCount; curBand++, band++) { - band->supportsPartial = 0; + band->supportsPartial = false; band->quantValue = 4; band->bandParam = nullptr; band->dataSize = 0;