39
39
#include < initializer_list> // for initializer_list
40
40
41
41
// this should be divisible by 4
42
- #define CRX_BUF_SIZE 0x10000
42
+ #define CRX_BUF_SIZE ( uint64_t ) 0x10000
43
43
44
- // Definitions copied from libraw
45
- #ifdef _abs
46
- #undef _abs
47
- #undef _min
48
- #undef _constrain
49
- #endif
50
- #define _abs (x ) (((x) ^ ((int32_t )(x) >> 31 )) - ((int32_t )(x) >> 31 ))
51
- #define _min (a, b ) ((a) < (b) ? (a) : (b))
52
- #define _constrain (x, l, u ) ((x) < (l) ? (l) : ((x) > (u) ? (u) : (x)))
44
+ #define crx_constrain (x, l, u ) ((x) < (l) ? (l) : ((x) > (u) ? (u) : (x)))
53
45
54
46
#if !defined(_WIN32) || \
55
47
(defined(__GNUC__) && !defined(__INTRINSIC_SPECIAL__BitScanReverse))
56
48
/* __INTRINSIC_SPECIAL__BitScanReverse found in MinGW32-W64 v7.30 headers, may
57
49
* be there is a better solution? */
58
50
typedef uint32_t DWORD;
59
- inline void _BitScanReverse (DWORD* Index, unsigned long Mask) {
51
+ inline void crx_BitScanReverse (DWORD* Index, unsigned long Mask) {
60
52
*Index = sizeof (unsigned long ) * 8 - 1 - __builtin_clzl (Mask);
61
53
}
54
+ #else
55
+ typedef uint32_t DWORD;
56
+ inline void crx_BitScanReverse (DWORD* Index, unsigned long Mask) {
57
+ _BitScanReverse (Index, Mask);
58
+ }
62
59
#endif
63
60
64
61
namespace rawspeed {
@@ -217,7 +214,7 @@ static inline void crxFillBuffer(CrxBitstream* bitStrm) {
217
214
auto sub = bitStrm->crxRawData .getSubView (bitStrm->curBufOffset );
218
215
219
216
bitStrm->mdatBuf .resize (CRX_BUF_SIZE);
220
- auto bytesToRead = _min (bitStrm->mdatSize , CRX_BUF_SIZE);
217
+ auto bytesToRead = std::min (bitStrm->mdatSize , CRX_BUF_SIZE);
221
218
222
219
if (sub.getSize () >= bytesToRead) {
223
220
auto data = sub.getData (0 , bytesToRead);
@@ -238,7 +235,7 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) {
238
235
int32_t result = 0 ;
239
236
240
237
if (bitStrm->bitData ) {
241
- _BitScanReverse (static_cast <DWORD*>(&nonZeroBit), static_cast <DWORD>(bitStrm->bitData ));
238
+ crx_BitScanReverse (static_cast <DWORD*>(&nonZeroBit), static_cast <DWORD>(bitStrm->bitData ));
242
239
result = 31 - nonZeroBit;
243
240
bitStrm->bitData <<= 32 - nonZeroBit;
244
241
bitStrm->bitsLeft -= 32 - nonZeroBit;
@@ -251,7 +248,7 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) {
251
248
bitStrm->curPos += 4 ;
252
249
crxFillBuffer (bitStrm);
253
250
if (nextData) {
254
- _BitScanReverse (static_cast <DWORD*>(&nonZeroBit), static_cast <DWORD>(nextData));
251
+ crx_BitScanReverse (static_cast <DWORD*>(&nonZeroBit), static_cast <DWORD>(nextData));
255
252
result = bitsLeft + 31 - nonZeroBit;
256
253
bitStrm->bitData = nextData << (32 - nonZeroBit);
257
254
bitStrm->bitsLeft = nonZeroBit;
@@ -267,7 +264,7 @@ inline int crxBitstreamGetZeros(CrxBitstream* bitStrm) {
267
264
break ;
268
265
bitsLeft += 8 ;
269
266
}
270
- _BitScanReverse (static_cast <DWORD*>(&nonZeroBit), static_cast <DWORD>(nextData));
267
+ crx_BitScanReverse (static_cast <DWORD*>(&nonZeroBit), static_cast <DWORD>(nextData));
271
268
result = static_cast <uint32_t >(bitsLeft + 7 - nonZeroBit);
272
269
bitStrm->bitData = nextData << (32 - nonZeroBit);
273
270
bitStrm->bitsLeft = nonZeroBit;
@@ -356,7 +353,7 @@ inline void crxDecodeSymbolL1(CrxBandParam* param, int32_t doMedianPrediction,
356
353
// for not end of the line - use one symbol ahead to estimate next K
357
354
if (notEOL) {
358
355
int32_t nextDelta = (param->lineBuf0 [2 ] - param->lineBuf0 [1 ]) << 1 ;
359
- bitCode = (bitCode + _abs (nextDelta)) >> 1 ;
356
+ bitCode = (bitCode + std::abs (nextDelta)) >> 1 ;
360
357
++param->lineBuf0 ;
361
358
}
362
359
@@ -461,7 +458,7 @@ inline void crxDecodeSymbolL1Rounded(CrxBandParam* param, int32_t doSym = 1,
461
458
}
462
459
463
460
param->kParam = crxPredictKParameter (param->kParam ,
464
- (bitCode + 2 * _abs (code)) >> 1 , 15 );
461
+ (bitCode + 2 * std::abs (code)) >> 1 , 15 );
465
462
} else
466
463
param->kParam = crxPredictKParameter (param->kParam , bitCode, 15 );
467
464
@@ -476,12 +473,12 @@ static int crxDecodeLineRounded(CrxBandParam* param) {
476
473
int32_t length = param->subbandWidth ;
477
474
478
475
for (; length > 1 ; --length) {
479
- if (_abs (param->lineBuf0 [2 ] - param->lineBuf0 [1 ]) >
476
+ if (std::abs (param->lineBuf0 [2 ] - param->lineBuf0 [1 ]) >
480
477
param->roundedBitsMask ) {
481
478
crxDecodeSymbolL1Rounded (param);
482
479
++param->lineBuf0 ;
483
480
valueReached = 1 ;
484
- } else if (valueReached || _abs (param->lineBuf0 [0 ] - param->lineBuf1 [0 ]) >
481
+ } else if (valueReached || std::abs (param->lineBuf0 [0 ] - param->lineBuf1 [0 ]) >
485
482
param->roundedBitsMask ) {
486
483
crxDecodeSymbolL1Rounded (param);
487
484
++param->lineBuf0 ;
@@ -524,7 +521,7 @@ static int crxDecodeLineRounded(CrxBandParam* param) {
524
521
if (length > 1 ) {
525
522
crxDecodeSymbolL1Rounded (param, 0 );
526
523
++param->lineBuf0 ;
527
- valueReached = _abs (param->lineBuf0 [1 ] - param->lineBuf0 [0 ]) >
524
+ valueReached = std::abs (param->lineBuf0 [1 ] - param->lineBuf0 [0 ]) >
528
525
param->roundedBitsMask ;
529
526
} else if (length == 1 )
530
527
crxDecodeSymbolL1Rounded (param, 0 , 0 );
@@ -720,7 +717,7 @@ static int crxDecodeTopLineRounded(CrxBandParam* param) {
720
717
721
718
// read the line from bitstream
722
719
for (; length > 1 ; --length) {
723
- if (_abs (param->lineBuf1 [0 ]) > param->roundedBitsMask )
720
+ if (std::abs (param->lineBuf1 [0 ]) > param->roundedBitsMask )
724
721
param->lineBuf1 [1 ] = param->lineBuf1 [0 ];
725
722
else {
726
723
int nSyms = 0 ;
@@ -1016,7 +1013,7 @@ static int crxDecodeLineWithIQuantization(CrxSubband* band, CrxQStep* qStep) {
1016
1013
for (int i = 0 ; i < band->colStartAddOn ; ++i) {
1017
1014
uint32_t quantVal =
1018
1015
band->qStepBase + ((qStepTblPtr[0 ] * band->qStepMult ) >> 3 );
1019
- bandBuf[i] *= _constrain (quantVal, 1 , 0x168000 );
1016
+ bandBuf[i] *= crx_constrain (quantVal, 1 , 0x168000 );
1020
1017
}
1021
1018
1022
1019
for (int i = band->colStartAddOn ; i < band->width - band->colEndAddOn ;
@@ -1026,14 +1023,14 @@ static int crxDecodeLineWithIQuantization(CrxSubband* band, CrxQStep* qStep) {
1026
1023
((qStepTblPtr[(i - band->colStartAddOn ) >> band->levelShift ] *
1027
1024
band->qStepMult ) >>
1028
1025
3 );
1029
- bandBuf[i] *= _constrain (quantVal, 1 , 0x168000 );
1026
+ bandBuf[i] *= crx_constrain (quantVal, 1 , 0x168000 );
1030
1027
}
1031
1028
int lastIdx = (band->width - band->colEndAddOn - band->colStartAddOn - 1 ) >>
1032
1029
band->levelShift ;
1033
1030
for (int i = band->width - band->colEndAddOn ; i < band->width ; ++i) {
1034
1031
uint32_t quantVal =
1035
1032
band->qStepBase + ((qStepTblPtr[lastIdx] * band->qStepMult ) >> 3 );
1036
- bandBuf[i] *= _constrain (quantVal, 1 , 0x168000 );
1033
+ bandBuf[i] *= crx_constrain (quantVal, 1 , 0x168000 );
1037
1034
}
1038
1035
} else {
1039
1036
// prev. version
@@ -1525,7 +1522,7 @@ static void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0,
1525
1522
--maxVal;
1526
1523
for (int i = 0 ; i < lineLength; i++)
1527
1524
img->outBufs [plane][rawOffset + 2 * i] =
1528
- _constrain (lineData[i], minVal, maxVal);
1525
+ crx_constrain (lineData[i], minVal, maxVal);
1529
1526
} else if (img->encType == 3 ) {
1530
1527
// copy to intermediate planeBuf
1531
1528
rawOffset = plane * img->planeWidth * img->planeHeight +
@@ -1537,14 +1534,14 @@ static void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0,
1537
1534
int32_t maxVal = (1 << img->nBits ) - 1 ;
1538
1535
for (int i = 0 ; i < lineLength; i++)
1539
1536
img->outBufs [plane][rawOffset + 2 * i] =
1540
- _constrain (median + lineData[i], 0 , maxVal);
1537
+ crx_constrain (median + lineData[i], 0 , maxVal);
1541
1538
} else if (img->nPlanes == 1 ) {
1542
1539
int32_t maxVal = (1 << img->nBits ) - 1 ;
1543
1540
int32_t median = 1 << (img->nBits - 1 );
1544
1541
rawOffset = img->planeWidth * imageRow + imageCol;
1545
1542
for (int i = 0 ; i < lineLength; i++)
1546
1543
img->outBufs [0 ][rawOffset + i] =
1547
- _constrain (median + lineData[i], 0 , maxVal);
1544
+ crx_constrain (median + lineData[i], 0 , maxVal);
1548
1545
}
1549
1546
} else if (img->encType == 3 && img->planeBuf ) {
1550
1547
int32_t planeSize = img->planeWidth * img->planeHeight ;
@@ -1563,22 +1560,22 @@ static void crxConvertPlaneLine(CrxImage* img, int imageRow, int imageCol = 0,
1563
1560
median + (plane0[i] << 10 ) - 168 * plane1[i] - 585 * plane3[i];
1564
1561
int32_t val = 0 ;
1565
1562
if (gr < 0 )
1566
- gr = -(((_abs (gr) + 512 ) >> 9 ) & ~1 );
1563
+ gr = -(((std::abs (gr) + 512 ) >> 9 ) & ~1 );
1567
1564
else
1568
- gr = ((_abs (gr) + 512 ) >> 9 ) & ~1 ;
1565
+ gr = ((std::abs (gr) + 512 ) >> 9 ) & ~1 ;
1569
1566
1570
1567
// Essentially R = round(median + P0 + 1.474*P3)
1571
1568
val = (median + (plane0[i] << 10 ) + 1510 * plane3[i] + 512 ) >> 10 ;
1572
- img->outBufs [0 ][rawLineOffset + 2 * i] = _constrain (val, 0 , maxVal);
1569
+ img->outBufs [0 ][rawLineOffset + 2 * i] = crx_constrain (val, 0 , maxVal);
1573
1570
// Essentially G1 = round(median + P0 + P2 - 0.164*P1 - 0.571*P3)
1574
1571
val = (plane2[i] + gr + 1 ) >> 1 ;
1575
- img->outBufs [1 ][rawLineOffset + 2 * i] = _constrain (val, 0 , maxVal);
1572
+ img->outBufs [1 ][rawLineOffset + 2 * i] = crx_constrain (val, 0 , maxVal);
1576
1573
// Essentially G2 = round(median + P0 - P2 - 0.164*P1 - 0.571*P3)
1577
1574
val = (gr - plane2[i] + 1 ) >> 1 ;
1578
- img->outBufs [2 ][rawLineOffset + 2 * i] = _constrain (val, 0 , maxVal);
1575
+ img->outBufs [2 ][rawLineOffset + 2 * i] = crx_constrain (val, 0 , maxVal);
1579
1576
// Essentially B = round(median + P0 + 1.881*P1)
1580
1577
val = (median + (plane0[i] << 10 ) + 1927 * plane1[i] + 512 ) >> 10 ;
1581
- img->outBufs [3 ][rawLineOffset + 2 * i] = _constrain (val, 0 , maxVal);
1578
+ img->outBufs [3 ][rawLineOffset + 2 * i] = crx_constrain (val, 0 , maxVal);
1582
1579
}
1583
1580
}
1584
1581
}
@@ -1832,7 +1829,7 @@ static void crxDecodeGolombNormal(CrxBitstream* bitStrm, int32_t width,
1832
1829
lineBuf1[1 ] += -(qp & 1 ) ^ (qp >> 1 );
1833
1830
if (width) {
1834
1831
deltaH = lineBuf0[2 ] - lineBuf0[1 ];
1835
- *kParam = crxPredictKParameter (*kParam , (qp + 2 * _abs (deltaH)) >> 1 , 7 );
1832
+ *kParam = crxPredictKParameter (*kParam , (qp + 2 * std::abs (deltaH)) >> 1 , 7 );
1836
1833
++lineBuf0;
1837
1834
} else
1838
1835
*kParam = crxPredictKParameter (*kParam , qp, 7 );
@@ -1871,10 +1868,10 @@ static int crxMakeQStep(CrxImage* img, CrxTile* tile, int32_t* qpTable,
1871
1868
qStep->width = qpWidth;
1872
1869
qStep->height = qpHeight8;
1873
1870
for (int qpRow = 0 ; qpRow < qpHeight8; ++qpRow) {
1874
- int row0Idx = qpWidth * _min (4 * qpRow, qpHeight - 1 );
1875
- int row1Idx = qpWidth * _min (4 * qpRow + 1 , qpHeight - 1 );
1876
- int row2Idx = qpWidth * _min (4 * qpRow + 2 , qpHeight - 1 );
1877
- int row3Idx = qpWidth * _min (4 * qpRow + 3 , qpHeight - 1 );
1871
+ int row0Idx = qpWidth * std::min (4 * qpRow, qpHeight - 1 );
1872
+ int row1Idx = qpWidth * std::min (4 * qpRow + 1 , qpHeight - 1 );
1873
+ int row2Idx = qpWidth * std::min (4 * qpRow + 2 , qpHeight - 1 );
1874
+ int row3Idx = qpWidth * std::min (4 * qpRow + 3 , qpHeight - 1 );
1878
1875
1879
1876
for (int qpCol = 0 ; qpCol < qpWidth; ++qpCol, ++qStepTbl) {
1880
1877
int32_t quantVal = qpTable[row0Idx++] + qpTable[row1Idx++] +
@@ -1897,8 +1894,8 @@ static int crxMakeQStep(CrxImage* img, CrxTile* tile, int32_t* qpTable,
1897
1894
qStep->width = qpWidth;
1898
1895
qStep->height = qpHeight4;
1899
1896
for (int qpRow = 0 ; qpRow < qpHeight4; ++qpRow) {
1900
- int row0Idx = qpWidth * _min (2 * qpRow, qpHeight - 1 );
1901
- int row1Idx = qpWidth * _min (2 * qpRow + 1 , qpHeight - 1 );
1897
+ int row0Idx = qpWidth * std::min (2 * qpRow, qpHeight - 1 );
1898
+ int row1Idx = qpWidth * std::min (2 * qpRow + 1 , qpHeight - 1 );
1902
1899
1903
1900
for (int qpCol = 0 ; qpCol < qpWidth; ++qpCol, ++qStepTbl) {
1904
1901
int32_t quantVal = (qpTable[row0Idx++] + qpTable[row1Idx++]) / 2 ;
0 commit comments