36 #define YUVoffset4 8 // 2^3
37 #define YUVoffset6 32 // 2^5
38 #define YUVoffset8 128 // 2^7
39 #define YUVoffset16 32768 // 2^15
47 OSError GetLastPGFError() {
48 OSError tmp = _PGF_Error_;
49 _PGF_Error_ = NoError;
56 struct DebugBGRImage {
57 int width, height, pitch;
76 #ifdef __PGFROISUPPORT__
205 if (!
m_channel[c]) ReturnWithError(InsufficientMemory);
208 for (UINT32 i=0; i < size; i++) {
211 if (count !=
DataTSize) ReturnWithError(MissingData);
322 if (bpc > 31) bpc = 31;
360 #ifdef __PGFROISUPPORT__
367 while (currentLevel > level) {
381 if (err != NoError) ReturnWithError(err);
406 #ifdef __PGFROISUPPORT__
410 Read(rect, level, cb, data);
420 if ((*cb)(1.0,
true, data)) ReturnWithError(EscapePressed);
449 volatile OSError error = NoError;
450 #ifdef LIBPGF_USE_OPENMP
451 #pragma omp parallel for default(shared)
455 if (error == NoError) {
457 if (err != NoError) error = err;
461 if (error != NoError) ReturnWithError(error);
473 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
479 #ifdef __PGFROISUPPORT__
496 Read(level, cb, data);
510 if (levelDiff <= 0) {
533 for (UINT32 tileY=0; tileY < nTiles; tileY++) {
534 for (UINT32 tileX=0; tileX < nTiles; tileX++) {
549 volatile OSError error = NoError;
550 #ifdef LIBPGF_USE_OPENMP
551 #pragma omp parallel for default(shared)
555 if (error == NoError) {
557 if (err != NoError) error = err;
561 if (error != NoError) ReturnWithError(error);
573 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
591 return PGFRect(rLeft, rTop, rRight - rLeft, rBottom - rTop);
606 ASSERT(roi.Width() ==
m_width[c]);
607 ASSERT(roi.Height() ==
m_height[c]);
642 #endif // __PGFROISUPPORT__
662 ASSERT(targetLen > 0);
673 ASSERT(len >= 0 && len <= targetLen);
709 ASSERT(targetLen > 0);
728 ASSERT(len >= 0 && len <= targetLen);
741 while(maxValue > 0) {
746 if (pot > bpc) pot = bpc;
747 if (pot > 31) pot = 31;
796 RgbToYuv(pitch, buff, bpp, channelMap, cb, data);
815 const int oddW = w%2;
822 for (
int i=0; i < h2; i++) {
823 for (
int j=0; j < w2; j++) {
825 buff[sampledPos] = (buff[loPos] + buff[loPos + 1] + buff[hiPos] + buff[hiPos + 1]) >> 2;
826 loPos += 2; hiPos += 2;
830 buff[sampledPos] = (buff[loPos] + buff[hiPos]) >> 1;
834 loPos += w; hiPos += w;
837 for (
int j=0; j < w2; j++) {
838 buff[sampledPos] = (buff[loPos] + buff[loPos+1]) >> 1;
839 loPos += 2; hiPos += 2;
843 buff[sampledPos] = buff[loPos];
861 while (s > maxThumbnailWidth) {
899 #ifdef __PGFROISUPPORT__
940 if (userDataLength && userData) {
965 ReturnWithError(InsufficientMemory);
983 volatile OSError error = NoError;
985 #ifdef LIBPGF_USE_OPENMP
986 #pragma omp parallel for default(shared)
989 DataT *temp =
nullptr;
990 if (error == NoError) {
995 temp =
new(std::nothrow)
DataT[size];
1001 error = InsufficientMemory;
1004 if (error == NoError) {
1011 #ifdef __PGFROISUPPORT__
1018 if (err != NoError) error = err;
1022 error = InsufficientMemory;
1027 if (error != NoError) {
1032 ReturnWithError(error);
1041 #ifdef __PGFROISUPPORT__
1056 return (nBytes > 0) ? (UINT32)nBytes : 0;
1072 #ifdef __PGFROISUPPORT__
1079 const UINT32 lastTile = nTiles - 1;
1083 ASSERT(nTiles == 1);
1087 for (UINT32 tileY=0; tileY < nTiles; tileY++) {
1088 for (UINT32 tileX=0; tileX < nTiles; tileX++) {
1093 if (i == lastChannel && tileY == lastTile && tileX == lastTile) {
1154 double percent = pow(0.25, levels);
1165 for (UINT32 i=0; i < size; i++) {
1173 if ((*cb)(1,
true, data)) ReturnWithError(EscapePressed);
1188 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1204 return nWrittenBytes;
1231 if (nWrittenBytes) *nWrittenBytes += nBytes;
1234 #ifdef __PGFROISUPPORT__
1257 UINT32 nWrittenBytes = 0;
1281 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1295 return nWrittenBytes;
1297 #endif // __PGFROISUPPORT__
1350 if (iFirstColor + nColors >
ColorTableLen) ReturnWithError(ColorTableError);
1352 for (UINT32 i=iFirstColor, j=0; j < nColors; i++, j++) {
1364 if (iFirstColor + nColors >
ColorTableLen) ReturnWithError(ColorTableError);
1366 for (UINT32 i=iFirstColor, j=0; j < nColors; i++, j++) {
1390 UINT32 yPos = 0, cnt = 0;
1393 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
1395 if (channelMap ==
nullptr) channelMap = defMap;
1411 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1415 for (UINT32 j = 0; j < w2; j++) {
1416 UINT8
byte = buff[j];
1417 for (
int k = 0; k < 8; k++) {
1418 UINT8 bit = (
byte & 0x80) >> 7;
1419 if (cnt < w) y[yPos++] = bit;
1458 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1479 ASSERT(bpp%16 == 0);
1481 UINT16 *buff16 = (UINT16 *)buff;
1482 const int pitch16 = pitch/2;
1489 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1496 m_channel[c][yPos] = (buff16[cnt + channelMap[c]] >> shift) - yuvOffset16;
1519 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1525 b = buff[cnt + channelMap[0]];
1526 g = buff[cnt + channelMap[1]];
1527 r = buff[cnt + channelMap[2]];
1529 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset8;
1543 ASSERT(bpp%16 == 0);
1545 UINT16 *buff16 = (UINT16 *)buff;
1546 const int pitch16 = pitch/2;
1558 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1564 b = buff16[cnt + channelMap[0]] >> shift;
1565 g = buff16[cnt + channelMap[1]] >> shift;
1566 r = buff16[cnt + channelMap[2]] >> shift;
1568 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1594 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1600 b = buff[cnt + channelMap[0]];
1601 g = buff[cnt + channelMap[1]];
1602 r = buff[cnt + channelMap[2]];
1604 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset8;
1607 a[yPos++] = buff[cnt + channelMap[3]] -
YUVoffset8;
1618 ASSERT(bpp%16 == 0);
1620 UINT16 *buff16 = (UINT16 *)buff;
1621 const int pitch16 = pitch/2;
1634 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1640 b = buff16[cnt + channelMap[0]] >> shift;
1641 g = buff16[cnt + channelMap[1]] >> shift;
1642 r = buff16[cnt + channelMap[2]] >> shift;
1644 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1647 a[yPos++] = (buff16[cnt + channelMap[3]] >> shift) - yuvOffset16;
1654 #ifdef __PGF32SUPPORT__
1664 UINT32 *buff32 = (UINT32 *)buff;
1665 const int pitch32 = pitch/4;
1671 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1676 y[yPos++] = (buff32[w] >> shift) - yuvOffset31;
1693 UINT8 rgb = 0, b, g, r;
1697 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1707 g = (rgb & 0xF0) >> 4;
1713 b = (rgb & 0xF0) >> 4;
1717 r = (rgb & 0xF0) >> 4;
1722 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset4;
1741 UINT16 *buff16 = (UINT16 *)buff;
1742 UINT16 rgb, b, g, r;
1743 const int pitch16 = pitch/2;
1747 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1752 r = (rgb & 0xF800) >> 10;
1753 g = (rgb & 0x07E0) >> 5;
1754 b = (rgb & 0x001F) << 1;
1756 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset6;
1788 void CPGFImage::GetBitmap(
int pitch, UINT8* buff, BYTE bpp,
int channelMap[] , CallbackPtr cb ,
void *data )
const {
1794 UINT32 roiOffsetX = 0;
1795 UINT32 roiOffsetY = 0;
1799 #ifdef __PGFROISUPPORT__
1808 w = levelRoi.
Width();
1810 roiOffsetX = levelRoi.
left - roi.
left;
1811 roiOffsetY = levelRoi.
top - roi.
top;
1812 yOffset = roiOffsetX + roiOffsetY*yw;
1823 const double dP = 1.0/h;
1824 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
1825 if (channelMap ==
nullptr) channelMap = defMap;
1837 const UINT32 w2 = (w + 7)/8;
1843 for (i = 0; i < h; i++) {
1845 for (j = 0; j < w2; j++) {
1847 for (
int k = 0; k < 8; k++) {
1851 bit = y[yOffset + cnt] & 1;
1863 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1870 yOffset = roiOffsetX/8 + roiOffsetY*yw;
1871 for (i = 0; i < h; i++) {
1872 for (j = 0; j < w2; j++) {
1880 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1897 for (i=0; i < h; i++) {
1898 UINT32 yPos = yOffset;
1900 for (j=0; j < w; j++) {
1912 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1923 UINT32 cnt, channels;
1927 UINT16 *buff16 = (UINT16 *)buff;
1928 int pitch16 = pitch/2;
1931 for (i=0; i < h; i++) {
1932 UINT32 yPos = yOffset;
1934 for (j=0; j < w; j++) {
1936 buff16[cnt + channelMap[c]] =
Clamp16((
m_channel[c][yPos] + yuvOffset16) << shift);
1946 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1954 for (i=0; i < h; i++) {
1955 UINT32 yPos = yOffset;
1957 for (j=0; j < w; j++) {
1959 buff[cnt + channelMap[c]] =
Clamp8((
m_channel[c][yPos] + yuvOffset16) >> shift);
1969 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1985 UINT8 *buffg = &buff[channelMap[1]],
1986 *buffr = &buff[channelMap[2]],
1987 *buffb = &buff[channelMap[0]];
1989 UINT32 cnt, channels = bpp/8;
1992 for (i=0; i < h; i++) {
1993 UINT32 uPos = uOffset;
1994 UINT32 yPos = yOffset;
1996 for (j=0; j < w; j++) {
2002 buffr[cnt] =
Clamp8(uAvg + g);
2003 buffb[cnt] =
Clamp8(vAvg + g);
2008 if (i & 1) uOffset += uw;
2016 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2021 for (i=0; i < h; i++) {
2023 UINT32 yPos = yOffset;
2024 for (j = 0; j < w; j++) {
2029 buffr[cnt] =
Clamp8(uAvg + g);
2030 buffb[cnt] =
Clamp8(vAvg + g);
2041 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2057 UINT32 cnt, channels;
2060 if (bpp >= 48 && bpp%16 == 0) {
2062 UINT16 *buff16 = (UINT16 *)buff;
2063 int pitch16 = pitch/2;
2066 for (i=0; i < h; i++) {
2067 UINT32 uPos = uOffset;
2068 UINT32 yPos = yOffset;
2070 for (j=0; j < w; j++) {
2074 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2075 buff16[cnt + channelMap[1]] =
Clamp16(g << shift);
2076 buff16[cnt + channelMap[2]] =
Clamp16((uAvg + g) << shift);
2077 buff16[cnt + channelMap[0]] =
Clamp16((vAvg + g) << shift);
2088 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2096 for (i=0; i < h; i++) {
2097 UINT32 uPos = uOffset;
2098 UINT32 yPos = yOffset;
2100 for (j=0; j < w; j++) {
2104 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2105 buff[cnt + channelMap[1]] =
Clamp8(g >> shift);
2106 buff[cnt + channelMap[2]] =
Clamp8((uAvg + g) >> shift);
2107 buff[cnt + channelMap[0]] =
Clamp8((vAvg + g) >> shift);
2118 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2135 for (i=0; i < h; i++) {
2136 UINT32 uPos = uOffset;
2137 UINT32 yPos = yOffset;
2139 for (j=0; j < w; j++) {
2155 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2170 UINT32 cnt, channels;
2174 UINT16 *buff16 = (UINT16 *)buff;
2175 int pitch16 = pitch/2;
2178 for (i=0; i < h; i++) {
2179 UINT32 uPos = uOffset;
2180 UINT32 yPos = yOffset;
2182 for (j=0; j < w; j++) {
2185 buff16[cnt + channelMap[0]] =
Clamp16((l[yPos] + yuvOffset16) << shift);
2186 buff16[cnt + channelMap[1]] =
Clamp16((uAvg + yuvOffset16) << shift);
2187 buff16[cnt + channelMap[2]] =
Clamp16((vAvg + yuvOffset16) << shift);
2198 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2206 for (i=0; i < h; i++) {
2207 UINT32 uPos = uOffset;
2208 UINT32 yPos = yOffset;
2210 for (j=0; j < w; j++) {
2213 buff[cnt + channelMap[0]] =
Clamp8((l[yPos] + yuvOffset16) >> shift);
2214 buff[cnt + channelMap[1]] =
Clamp8((uAvg + yuvOffset16) >> shift);
2215 buff[cnt + channelMap[2]] =
Clamp8((vAvg + yuvOffset16) >> shift);
2226 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2246 for (i=0; i < h; i++) {
2247 UINT32 uPos = uOffset;
2248 UINT32 yPos = yOffset;
2250 for (j=0; j < w; j++) {
2255 buff[cnt + channelMap[1]] = g =
Clamp8(y[yPos] +
YUVoffset8 - ((uAvg + vAvg ) >> 2));
2256 buff[cnt + channelMap[2]] =
Clamp8(uAvg + g);
2257 buff[cnt + channelMap[0]] =
Clamp8(vAvg + g);
2258 buff[cnt + channelMap[3]] = aAvg;
2269 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2286 UINT32 cnt, channels;
2290 UINT16 *buff16 = (UINT16 *)buff;
2291 int pitch16 = pitch/2;
2294 for (i=0; i < h; i++) {
2295 UINT32 uPos = uOffset;
2296 UINT32 yPos = yOffset;
2298 for (j=0; j < w; j++) {
2301 aAvg = a[uPos] + yuvOffset16;
2303 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2304 buff16[cnt + channelMap[1]] =
Clamp16(g << shift);
2305 buff16[cnt + channelMap[2]] =
Clamp16((uAvg + g) << shift);
2306 buff16[cnt + channelMap[0]] =
Clamp16((vAvg + g) << shift);
2307 buff16[cnt + channelMap[3]] =
Clamp16(aAvg << shift);
2318 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2326 for (i=0; i < h; i++) {
2327 UINT32 uPos = uOffset;
2328 UINT32 yPos = yOffset;
2330 for (j=0; j < w; j++) {
2333 aAvg = a[uPos] + yuvOffset16;
2335 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2336 buff[cnt + channelMap[1]] =
Clamp8(g >> shift);
2337 buff[cnt + channelMap[2]] =
Clamp8((uAvg + g) >> shift);
2338 buff[cnt + channelMap[0]] =
Clamp8((vAvg + g) >> shift);
2339 buff[cnt + channelMap[3]] =
Clamp8(aAvg >> shift);
2350 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2356 #ifdef __PGF32SUPPORT__
2367 UINT32 *buff32 = (UINT32 *)buff;
2368 int pitch32 = pitch/4;
2370 for (i=0; i < h; i++) {
2371 UINT32 yPos = yOffset;
2372 for (j = 0; j < w; j++) {
2373 buff32[j] =
Clamp31((y[yPos++] + yuvOffset31) << shift);
2380 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2383 }
else if (bpp == 16) {
2385 UINT16 *buff16 = (UINT16 *)buff;
2386 int pitch16 = pitch/2;
2388 if (usedBits < 16) {
2389 const int shift = 16 - usedBits;
2390 for (i=0; i < h; i++) {
2391 UINT32 yPos = yOffset;
2392 for (j = 0; j < w; j++) {
2393 buff16[j] =
Clamp16((y[yPos++] + yuvOffset31) << shift);
2400 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2404 const int shift =
__max(0, usedBits - 16);
2405 for (i=0; i < h; i++) {
2406 UINT32 yPos = yOffset;
2407 for (j = 0; j < w; j++) {
2408 buff16[j] =
Clamp16((y[yPos++] + yuvOffset31) >> shift);
2415 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2423 for (i=0; i < h; i++) {
2424 UINT32 yPos = yOffset;
2425 for (j = 0; j < w; j++) {
2426 buff[j] =
Clamp8((y[yPos++] + yuvOffset31) >> shift);
2433 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2453 for (i=0; i < h; i++) {
2454 UINT32 yPos = yOffset;
2456 for (j=0; j < w; j++) {
2462 buff[cnt] = UINT8(
Clamp4(vAvg + yval) | (yval << 4));
2464 buff[cnt] =
Clamp4(uAvg + yval);
2466 buff[cnt] |=
Clamp4(vAvg + yval) << 4;
2468 buff[cnt] = UINT8(yval | (
Clamp4(uAvg + yval) << 4));
2478 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2494 UINT16 *buff16 = (UINT16 *)buff;
2495 int pitch16 = pitch/2;
2497 for (i=0; i < h; i++) {
2498 UINT32 yPos = yOffset;
2499 for (j = 0; j < w; j++) {
2504 buff16[j] = (yval << 5) | ((
Clamp6(uAvg + yval) >> 1) << 11) | (
Clamp6(vAvg + yval) >> 1);
2511 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2523 roiimage.height = h;
2525 roiimage.pitch = pitch;
2526 roiimage.data = buff;
2528 roiimage.pitch = -pitch;
2529 roiimage.data = buff + (h - 1)*pitch;
2553 const bool wOdd = (1 == w%2);
2554 const int dataBits =
DataTSize*8; ASSERT(dataBits == 16 || dataBits == 32);
2557 const double dP = 1.0/h;
2559 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
2560 if (channelMap ==
nullptr) channelMap = defMap;
2561 int sampledPos = 0, yPos = 0;
2567 ASSERT(bpp%dataBits == 0);
2574 for (i=0; i < h; i++) {
2575 if (i%2) sampledPos -= (w + 1)/2;
2577 for (j=0; j < w; j++) {
2580 uAvg = u[sampledPos];
2581 vAvg = v[sampledPos];
2586 buff[cnt + channelMap[0]] = y[yPos];
2587 buff[cnt + channelMap[1]] = uAvg;
2588 buff[cnt + channelMap[2]] = vAvg;
2591 if (j%2) sampledPos++;
2594 if (wOdd) sampledPos++;
2598 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2603 ASSERT(bpp%dataBits == 0);
2612 for (i=0; i < h; i++) {
2613 if (i%2) sampledPos -= (w + 1)/2;
2615 for (j=0; j < w; j++) {
2618 uAvg = u[sampledPos];
2619 vAvg = v[sampledPos];
2620 aAvg =
Clamp8(a[sampledPos] + yuvOffset);
2624 aAvg =
Clamp8(a[yPos] + yuvOffset);
2627 buff[cnt + channelMap[0]] = y[yPos];
2628 buff[cnt + channelMap[1]] = uAvg;
2629 buff[cnt + channelMap[2]] = vAvg;
2630 buff[cnt + channelMap[3]] = aAvg;
2633 if (j%2) sampledPos++;
2636 if (wOdd) sampledPos++;
2640 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2663 const int dataBits =
DataTSize*8; ASSERT(dataBits == 16 || dataBits == 32);
2667 int yPos = 0, cnt = 0;
2669 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
2671 if (channelMap ==
nullptr) channelMap = defMap;
2674 ASSERT(bpp%dataBits == 0);
2683 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2689 y[yPos] = buff[cnt + channelMap[0]];
2690 u[yPos] = buff[cnt + channelMap[1]];
2691 v[yPos] = buff[cnt + channelMap[2]];
2698 ASSERT(bpp%dataBits == 0);
2708 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2714 y[yPos] = buff[cnt + channelMap[0]];
2715 u[yPos] = buff[cnt + channelMap[1]];
2716 v[yPos] = buff[cnt + channelMap[2]];
2717 a[yPos] = buff[cnt + channelMap[3]] - yuvOffset;