1332void CPGFImage::RgbToYuv(
int pitch, UINT8* buff, BYTE bpp,
int channelMap[], CallbackPtr cb,
void *data ) THROW_ {
1334 int yPos = 0, cnt = 0;
1336 const double dP = 1.0/
m_header.height;
1337 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
1339 if (channelMap == NULL) channelMap = defMap;
1349 const UINT32 w2 = (
m_header.width + 7)/8;
1352 for (UINT32 h=0; h <
m_header.height; h++) {
1354 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1358 for (UINT32 j=0; j < w2; j++) {
1361 for (UINT32 j=w2; j < w; j++) {
1387 const int channels = bpp/8; ASSERT(channels >=
m_header.channels);
1389 for (UINT32 h=0; h <
m_header.height; h++) {
1391 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1396 for (UINT32 w=0; w <
m_header.width; w++) {
1397 for (
int c=0; c <
m_header.channels; c++) {
1412 ASSERT(bpp%16 == 0);
1414 UINT16 *buff16 = (UINT16 *)buff;
1415 const int pitch16 = pitch/2;
1416 const int channels = bpp/16; ASSERT(channels >=
m_header.channels);
1420 for (UINT32 h=0; h <
m_header.height; h++) {
1422 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1427 for (UINT32 w=0; w <
m_header.width; w++) {
1428 for (
int c=0; c <
m_header.channels; c++) {
1429 m_channel[c][yPos] = (buff16[cnt + channelMap[c]] >> shift) - yuvOffset16;
1447 const int channels = bpp/8; ASSERT(channels >=
m_header.channels);
1450 for (UINT32 h=0; h <
m_header.height; h++) {
1452 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1457 for (UINT32 w=0; w <
m_header.width; w++) {
1458 b = buff[cnt + channelMap[0]];
1459 g = buff[cnt + channelMap[1]];
1460 r = buff[cnt + channelMap[2]];
1462 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset8;
1476 ASSERT(bpp%16 == 0);
1478 UINT16 *buff16 = (UINT16 *)buff;
1479 const int pitch16 = pitch/2;
1480 const int channels = bpp/16; ASSERT(channels >=
m_header.channels);
1489 for (UINT32 h=0; h <
m_header.height; h++) {
1491 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1496 for (UINT32 w=0; w <
m_header.width; w++) {
1497 b = buff16[cnt + channelMap[0]] >> shift;
1498 g = buff16[cnt + channelMap[1]] >> shift;
1499 r = buff16[cnt + channelMap[2]] >> shift;
1501 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1517 const int channels = bpp/8; ASSERT(channels >=
m_header.channels);
1525 for (UINT32 h=0; h <
m_header.height; h++) {
1527 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1532 for (UINT32 w=0; w <
m_header.width; w++) {
1533 b = buff[cnt + channelMap[0]];
1534 g = buff[cnt + channelMap[1]];
1535 r = buff[cnt + channelMap[2]];
1537 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset8;
1540 a[yPos++] = buff[cnt + channelMap[3]] -
YUVoffset8;
1551 ASSERT(bpp%16 == 0);
1553 UINT16 *buff16 = (UINT16 *)buff;
1554 const int pitch16 = pitch/2;
1555 const int channels = bpp/16; ASSERT(channels >=
m_header.channels);
1565 for (UINT32 h=0; h <
m_header.height; h++) {
1567 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1572 for (UINT32 w=0; w <
m_header.width; w++) {
1573 b = buff16[cnt + channelMap[0]] >> shift;
1574 g = buff16[cnt + channelMap[1]] >> shift;
1575 r = buff16[cnt + channelMap[2]] >> shift;
1577 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1580 a[yPos++] = (buff16[cnt + channelMap[3]] >> shift) - yuvOffset16;
1587#ifdef __PGF32SUPPORT__
1597 UINT32 *buff32 = (UINT32 *)buff;
1598 const int pitch32 = pitch/4;
1602 for (UINT32 h=0; h <
m_header.height; h++) {
1604 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1608 for (UINT32 w=0; w <
m_header.width; w++) {
1609 y[yPos++] = (buff32[w] >> shift) - yuvOffset31;
1620 ASSERT(bpp ==
m_header.channels*4);
1626 UINT8 rgb = 0, b, g, r;
1628 for (UINT32 h=0; h <
m_header.height; h++) {
1630 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1635 for (UINT32 w=0; w <
m_header.width; w++) {
1640 g = (rgb & 0xF0) >> 4;
1646 b = (rgb & 0xF0) >> 4;
1650 r = (rgb & 0xF0) >> 4;
1655 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset4;
1674 UINT16 *buff16 = (UINT16 *)buff;
1675 UINT16 rgb, b, g, r;
1676 const int pitch16 = pitch/2;
1678 for (UINT32 h=0; h <
m_header.height; h++) {
1680 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1683 for (UINT32 w=0; w <
m_header.width; w++) {
1685 r = (rgb & 0xF800) >> 10;
1686 g = (rgb & 0x07E0) >> 5;
1687 b = (rgb & 0x001F) << 1;
1689 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset6;
1721void CPGFImage::GetBitmap(
int pitch, UINT8* buff, BYTE bpp,
int channelMap[] , CallbackPtr cb ,
void *data )
const THROW_ {
1725 UINT8* targetBuff = 0;
1726 UINT8* buffStart = 0;
1727 int targetPitch = 0;
1729#ifdef __PGFROISUPPORT__
1739 targetPitch = pitch;
1744 buff = buffStart =
new(std::nothrow) UINT8[pitch*h];
1745 if (!buff) ReturnWithError(InsufficientMemory);
1749 const bool wOdd = (1 == w%2);
1751 const double dP = 1.0/h;
1752 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
1753 if (channelMap == NULL) channelMap = defMap;
1754 int sampledPos = 0, yPos = 0;
1766 const UINT32 w2 = (w + 7)/8;
1769 for (i=0; i < h; i++) {
1771 for (j=0; j < w2; j++) {
1791 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1805 int cnt, channels = bpp/8; ASSERT(channels >=
m_header.channels);
1807 for (i=0; i < h; i++) {
1809 for (j=0; j < w; j++) {
1810 for (
int c=0; c <
m_header.channels; c++) {
1820 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1835 UINT16 *buff16 = (UINT16 *)buff;
1836 int pitch16 = pitch/2;
1837 channels = bpp/16; ASSERT(channels >=
m_header.channels);
1839 for (i=0; i < h; i++) {
1841 for (j=0; j < w; j++) {
1842 for (
int c=0; c <
m_header.channels; c++) {
1843 buff16[cnt + channelMap[c]] =
Clamp16((
m_channel[c][yPos] + yuvOffset16) << shift);
1852 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1858 channels = bpp/8; ASSERT(channels >=
m_header.channels);
1860 for (i=0; i < h; i++) {
1862 for (j=0; j < w; j++) {
1863 for (
int c=0; c <
m_header.channels; c++) {
1864 buff[cnt + channelMap[c]] =
Clamp8((
m_channel[c][yPos] + yuvOffset16) >> shift);
1873 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1889 UINT8 *buffg = &buff[channelMap[1]],
1890 *buffr = &buff[channelMap[2]],
1891 *buffb = &buff[channelMap[0]];
1893 int cnt, channels = bpp/8;
1895 for (i=0; i < h; i++) {
1896 if (i%2) sampledPos -= (w + 1)/2;
1898 for (j=0; j < w; j++) {
1900 uAvg = u[sampledPos];
1901 vAvg = v[sampledPos];
1904 buffr[cnt] =
Clamp8(uAvg + g);
1905 buffb[cnt] =
Clamp8(vAvg + g);
1908 if (j%2) sampledPos++;
1913 if (wOdd) sampledPos++;
1916 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1920 for (i=0; i < h; i++) {
1922 for (j = 0; j < w; j++) {
1927 buffr[cnt] =
Clamp8(uAvg + g);
1928 buffb[cnt] =
Clamp8(vAvg + g);
1938 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1957 if (bpp >= 48 && bpp%16 == 0) {
1959 UINT16 *buff16 = (UINT16 *)buff;
1960 int pitch16 = pitch/2;
1961 channels = bpp/16; ASSERT(channels >=
m_header.channels);
1963 for (i=0; i < h; i++) {
1964 if (i%2) sampledPos -= (w + 1)/2;
1966 for (j=0; j < w; j++) {
1969 uAvg = u[sampledPos];
1970 vAvg = v[sampledPos];
1976 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
1977 buff16[cnt + channelMap[1]] =
Clamp16(g << shift);
1978 buff16[cnt + channelMap[2]] =
Clamp16((uAvg + g) << shift);
1979 buff16[cnt + channelMap[0]] =
Clamp16((vAvg + g) << shift);
1982 if (j%2) sampledPos++;
1985 if (wOdd) sampledPos++;
1989 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1995 channels = bpp/8; ASSERT(channels >=
m_header.channels);
1997 for (i=0; i < h; i++) {
1998 if (i%2) sampledPos -= (w + 1)/2;
2000 for (j=0; j < w; j++) {
2003 uAvg = u[sampledPos];
2004 vAvg = v[sampledPos];
2010 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2011 buff[cnt + channelMap[1]] =
Clamp8(g >> shift);
2012 buff[cnt + channelMap[2]] =
Clamp8((uAvg + g) >> shift);
2013 buff[cnt + channelMap[0]] =
Clamp8((vAvg + g) >> shift);
2016 if (j%2) sampledPos++;
2019 if (wOdd) sampledPos++;
2023 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2038 int cnt, channels = bpp/8; ASSERT(channels >=
m_header.channels);
2040 for (i=0; i < h; i++) {
2041 if (i%2) sampledPos -= (w + 1)/2;
2043 for (j=0; j < w; j++) {
2046 uAvg = a[sampledPos];
2047 vAvg = b[sampledPos];
2057 if (j%2) sampledPos++;
2060 if (wOdd) sampledPos++;
2064 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2083 UINT16 *buff16 = (UINT16 *)buff;
2084 int pitch16 = pitch/2;
2085 channels = bpp/16; ASSERT(channels >=
m_header.channels);
2087 for (i=0; i < h; i++) {
2088 if (i%2) sampledPos -= (w + 1)/2;
2090 for (j=0; j < w; j++) {
2093 uAvg = a[sampledPos];
2094 vAvg = b[sampledPos];
2099 buff16[cnt + channelMap[0]] =
Clamp16((l[yPos] + yuvOffset16) << shift);
2100 buff16[cnt + channelMap[1]] =
Clamp16((uAvg + yuvOffset16) << shift);
2101 buff16[cnt + channelMap[2]] =
Clamp16((vAvg + yuvOffset16) << shift);
2104 if (j%2) sampledPos++;
2107 if (wOdd) sampledPos++;
2111 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2117 channels = bpp/8; ASSERT(channels >=
m_header.channels);
2119 for (i=0; i < h; i++) {
2120 if (i%2) sampledPos -= (w + 1)/2;
2122 for (j=0; j < w; j++) {
2125 uAvg = a[sampledPos];
2126 vAvg = b[sampledPos];
2131 buff[cnt + channelMap[0]] =
Clamp8((l[yPos] + yuvOffset16) >> shift);
2132 buff[cnt + channelMap[1]] =
Clamp8((uAvg + yuvOffset16) >> shift);
2133 buff[cnt + channelMap[2]] =
Clamp8((vAvg + yuvOffset16) >> shift);
2136 if (j%2) sampledPos++;
2139 if (wOdd) sampledPos++;
2143 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2161 int cnt, channels = bpp/8; ASSERT(channels >=
m_header.channels);
2163 for (i=0; i < h; i++) {
2164 if (i%2) sampledPos -= (w + 1)/2;
2166 for (j=0; j < w; j++) {
2169 uAvg = u[sampledPos];
2170 vAvg = v[sampledPos];
2178 buff[cnt + channelMap[1]] = g =
Clamp8(y[yPos] +
YUVoffset8 - ((uAvg + vAvg ) >> 2));
2179 buff[cnt + channelMap[2]] =
Clamp8(uAvg + g);
2180 buff[cnt + channelMap[0]] =
Clamp8(vAvg + g);
2181 buff[cnt + channelMap[3]] = aAvg;
2184 if (j%2) sampledPos++;
2187 if (wOdd) sampledPos++;
2191 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2212 UINT16 *buff16 = (UINT16 *)buff;
2213 int pitch16 = pitch/2;
2214 channels = bpp/16; ASSERT(channels >=
m_header.channels);
2216 for (i=0; i < h; i++) {
2217 if (i%2) sampledPos -= (w + 1)/2;
2219 for (j=0; j < w; j++) {
2222 uAvg = u[sampledPos];
2223 vAvg = v[sampledPos];
2224 aAvg = a[sampledPos] + yuvOffset16;
2228 aAvg = a[yPos] + yuvOffset16;
2231 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2232 buff16[cnt + channelMap[1]] =
Clamp16(g << shift);
2233 buff16[cnt + channelMap[2]] =
Clamp16((uAvg + g) << shift);
2234 buff16[cnt + channelMap[0]] =
Clamp16((vAvg + g) << shift);
2235 buff16[cnt + channelMap[3]] =
Clamp16(aAvg << shift);
2238 if (j%2) sampledPos++;
2241 if (wOdd) sampledPos++;
2245 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2251 channels = bpp/8; ASSERT(channels >=
m_header.channels);
2253 for (i=0; i < h; i++) {
2254 if (i%2) sampledPos -= (w + 1)/2;
2256 for (j=0; j < w; j++) {
2259 uAvg = u[sampledPos];
2260 vAvg = v[sampledPos];
2261 aAvg = a[sampledPos] + yuvOffset16;
2265 aAvg = a[yPos] + yuvOffset16;
2268 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2269 buff[cnt + channelMap[1]] =
Clamp8(g >> shift);
2270 buff[cnt + channelMap[2]] =
Clamp8((uAvg + g) >> shift);
2271 buff[cnt + channelMap[0]] =
Clamp8((vAvg + g) >> shift);
2272 buff[cnt + channelMap[3]] =
Clamp8(aAvg >> shift);
2275 if (j%2) sampledPos++;
2278 if (wOdd) sampledPos++;
2282 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2288#ifdef __PGF32SUPPORT__
2300 UINT32 *buff32 = (UINT32 *)buff;
2301 int pitch32 = pitch/4;
2303 for (i=0; i < h; i++) {
2304 for (j=0; j < w; j++) {
2305 buff32[j] =
Clamp31((y[yPos++] + yuvOffset31) << shift);
2311 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2314 }
else if (bpp == 16) {
2316 UINT16 *buff16 = (UINT16 *)buff;
2317 int pitch16 = pitch/2;
2319 if (usedBits < 16) {
2320 const int shift = 16 - usedBits;
2321 for (i=0; i < h; i++) {
2322 for (j=0; j < w; j++) {
2323 buff16[j] =
Clamp16((y[yPos++] + yuvOffset31) << shift);
2329 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2333 const int shift =
__max(0, usedBits - 16);
2334 for (i=0; i < h; i++) {
2335 for (j=0; j < w; j++) {
2336 buff16[j] =
Clamp16((y[yPos++] + yuvOffset31) >> shift);
2342 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2350 for (i=0; i < h; i++) {
2351 for (j=0; j < w; j++) {
2352 buff[j] =
Clamp8((y[yPos++] + yuvOffset31) >> shift);
2358 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2369 ASSERT(bpp ==
m_header.channels*4);
2378 for (i=0; i < h; i++) {
2380 for (j=0; j < w; j++) {
2386 buff[cnt] = UINT8(
Clamp4(vAvg + yval) | (yval << 4));
2388 buff[cnt] =
Clamp4(uAvg + yval);
2390 buff[cnt] |=
Clamp4(vAvg + yval) << 4;
2392 buff[cnt] = UINT8(yval | (
Clamp4(uAvg + yval) << 4));
2400 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2416 UINT16 *buff16 = (UINT16 *)buff;
2417 int pitch16 = pitch/2;
2419 for (i=0; i < h; i++) {
2420 for (j=0; j < w; j++) {
2425 buff16[j] = (yval << 5) | ((
Clamp6(uAvg + yval) >> 1) << 11) | (
Clamp6(vAvg + yval) >> 1);
2431 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2440#ifdef __PGFROISUPPORT__
2445 buff = buffStart + (levelRoi.
top - roi.
top)*pitch + (levelRoi.
left - roi.
left)*bypp;
2446 w = levelRoi.
Width()*bypp;
2449 for (i=0; i < h; i++) {
2450 for (j=0; j < w; j++) {
2451 targetBuff[j] = buff[j];
2453 targetBuff += targetPitch;
2460 delete[] buffStart; buffStart = 0;
2483 const bool wOdd = (1 == w%2);
2484 const int dataBits =
DataTSize*8; ASSERT(dataBits == 16 || dataBits == 32);
2487 const double dP = 1.0/h;
2489 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
2490 if (channelMap == NULL) channelMap = defMap;
2491 int sampledPos = 0, yPos = 0;
2497 ASSERT(bpp%dataBits == 0);
2502 int cnt, channels = bpp/dataBits; ASSERT(channels >=
m_header.channels);
2504 for (i=0; i < h; i++) {
2505 if (i%2) sampledPos -= (w + 1)/2;
2507 for (j=0; j < w; j++) {
2510 uAvg = u[sampledPos];
2511 vAvg = v[sampledPos];
2516 buff[cnt + channelMap[0]] = y[yPos];
2517 buff[cnt + channelMap[1]] = uAvg;
2518 buff[cnt + channelMap[2]] = vAvg;
2521 if (j%2) sampledPos++;
2524 if (wOdd) sampledPos++;
2528 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2531 }
else if (
m_header.channels == 4) {
2533 ASSERT(bpp%dataBits == 0);
2540 int cnt, channels = bpp/dataBits; ASSERT(channels >=
m_header.channels);
2542 for (i=0; i < h; i++) {
2543 if (i%2) sampledPos -= (w + 1)/2;
2545 for (j=0; j < w; j++) {
2548 uAvg = u[sampledPos];
2549 vAvg = v[sampledPos];
2550 aAvg =
Clamp8(a[sampledPos] + yuvOffset);
2554 aAvg =
Clamp8(a[yPos] + yuvOffset);
2557 buff[cnt + channelMap[0]] = y[yPos];
2558 buff[cnt + channelMap[1]] = uAvg;
2559 buff[cnt + channelMap[2]] = vAvg;
2560 buff[cnt + channelMap[3]] = aAvg;
2563 if (j%2) sampledPos++;
2566 if (wOdd) sampledPos++;
2570 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);