1330void CPGFImage::RgbToYuv(
int pitch, UINT8* buff, BYTE bpp,
int channelMap[], CallbackPtr cb,
void *data ) THROW_ {
1332 int yPos = 0, cnt = 0;
1334 const double dP = 1.0/
m_header.height;
1335 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
1337 if (channelMap == NULL) channelMap = defMap;
1347 const UINT32 w2 = (
m_header.width + 7)/8;
1350 for (UINT32 h=0; h <
m_header.height; h++) {
1352 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1356 for (UINT32 j=0; j < w2; j++) {
1359 for (UINT32 j=w2; j < w; j++) {
1385 const int channels = bpp/8; ASSERT(channels >=
m_header.channels);
1387 for (UINT32 h=0; h <
m_header.height; h++) {
1389 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1394 for (UINT32 w=0; w <
m_header.width; w++) {
1395 for (
int c=0; c <
m_header.channels; c++) {
1410 ASSERT(bpp%16 == 0);
1412 UINT16 *buff16 = (UINT16 *)buff;
1413 const int pitch16 = pitch/2;
1414 const int channels = bpp/16; ASSERT(channels >=
m_header.channels);
1418 for (UINT32 h=0; h <
m_header.height; h++) {
1420 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1425 for (UINT32 w=0; w <
m_header.width; w++) {
1426 for (
int c=0; c <
m_header.channels; c++) {
1427 m_channel[c][yPos] = (buff16[cnt + channelMap[c]] >> shift) - yuvOffset16;
1445 const int channels = bpp/8; ASSERT(channels >=
m_header.channels);
1448 for (UINT32 h=0; h <
m_header.height; h++) {
1450 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1455 for (UINT32 w=0; w <
m_header.width; w++) {
1456 b = buff[cnt + channelMap[0]];
1457 g = buff[cnt + channelMap[1]];
1458 r = buff[cnt + channelMap[2]];
1460 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset8;
1474 ASSERT(bpp%16 == 0);
1476 UINT16 *buff16 = (UINT16 *)buff;
1477 const int pitch16 = pitch/2;
1478 const int channels = bpp/16; ASSERT(channels >=
m_header.channels);
1487 for (UINT32 h=0; h <
m_header.height; h++) {
1489 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1494 for (UINT32 w=0; w <
m_header.width; w++) {
1495 b = buff16[cnt + channelMap[0]] >> shift;
1496 g = buff16[cnt + channelMap[1]] >> shift;
1497 r = buff16[cnt + channelMap[2]] >> shift;
1499 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1515 const int channels = bpp/8; ASSERT(channels >=
m_header.channels);
1523 for (UINT32 h=0; h <
m_header.height; h++) {
1525 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1530 for (UINT32 w=0; w <
m_header.width; w++) {
1531 b = buff[cnt + channelMap[0]];
1532 g = buff[cnt + channelMap[1]];
1533 r = buff[cnt + channelMap[2]];
1535 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset8;
1538 a[yPos++] = buff[cnt + channelMap[3]] -
YUVoffset8;
1549 ASSERT(bpp%16 == 0);
1551 UINT16 *buff16 = (UINT16 *)buff;
1552 const int pitch16 = pitch/2;
1553 const int channels = bpp/16; ASSERT(channels >=
m_header.channels);
1563 for (UINT32 h=0; h <
m_header.height; h++) {
1565 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1570 for (UINT32 w=0; w <
m_header.width; w++) {
1571 b = buff16[cnt + channelMap[0]] >> shift;
1572 g = buff16[cnt + channelMap[1]] >> shift;
1573 r = buff16[cnt + channelMap[2]] >> shift;
1575 y[yPos] = ((b + (g << 1) + r) >> 2) - yuvOffset16;
1578 a[yPos++] = (buff16[cnt + channelMap[3]] >> shift) - yuvOffset16;
1585#ifdef __PGF32SUPPORT__
1595 UINT32 *buff32 = (UINT32 *)buff;
1596 const int pitch32 = pitch/4;
1600 for (UINT32 h=0; h <
m_header.height; h++) {
1602 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1606 for (UINT32 w=0; w <
m_header.width; w++) {
1607 y[yPos++] = (buff32[w] >> shift) - yuvOffset31;
1618 ASSERT(bpp ==
m_header.channels*4);
1624 UINT8 rgb = 0, b, g, r;
1626 for (UINT32 h=0; h <
m_header.height; h++) {
1628 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1633 for (UINT32 w=0; w <
m_header.width; w++) {
1638 g = (rgb & 0xF0) >> 4;
1644 b = (rgb & 0xF0) >> 4;
1648 r = (rgb & 0xF0) >> 4;
1653 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset4;
1672 UINT16 *buff16 = (UINT16 *)buff;
1673 UINT16 rgb, b, g, r;
1674 const int pitch16 = pitch/2;
1676 for (UINT32 h=0; h <
m_header.height; h++) {
1678 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1681 for (UINT32 w=0; w <
m_header.width; w++) {
1683 r = (rgb & 0xF800) >> 10;
1684 g = (rgb & 0x07E0) >> 5;
1685 b = (rgb & 0x001F) << 1;
1687 y[yPos] = ((b + (g << 1) + r) >> 2) -
YUVoffset6;
1719void CPGFImage::GetBitmap(
int pitch, UINT8* buff, BYTE bpp,
int channelMap[] , CallbackPtr cb ,
void *data )
const THROW_ {
1723 UINT8* targetBuff = 0;
1724 UINT8* buffStart = 0;
1725 int targetPitch = 0;
1727#ifdef __PGFROISUPPORT__
1737 targetPitch = pitch;
1742 buff = buffStart =
new(std::nothrow) UINT8[pitch*h];
1743 if (!buff) ReturnWithError(InsufficientMemory);
1747 const bool wOdd = (1 == w%2);
1749 const double dP = 1.0/h;
1750 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
1751 if (channelMap == NULL) channelMap = defMap;
1752 int sampledPos = 0, yPos = 0;
1764 const UINT32 w2 = (w + 7)/8;
1767 for (i=0; i < h; i++) {
1769 for (j=0; j < w2; j++) {
1789 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1803 int cnt, channels = bpp/8; ASSERT(channels >=
m_header.channels);
1805 for (i=0; i < h; i++) {
1807 for (j=0; j < w; j++) {
1808 for (
int c=0; c <
m_header.channels; c++) {
1818 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1833 UINT16 *buff16 = (UINT16 *)buff;
1834 int pitch16 = pitch/2;
1835 channels = bpp/16; ASSERT(channels >=
m_header.channels);
1837 for (i=0; i < h; i++) {
1839 for (j=0; j < w; j++) {
1840 for (
int c=0; c <
m_header.channels; c++) {
1841 buff16[cnt + channelMap[c]] =
Clamp16((
m_channel[c][yPos] + yuvOffset16) << shift);
1850 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1856 channels = bpp/8; ASSERT(channels >=
m_header.channels);
1858 for (i=0; i < h; i++) {
1860 for (j=0; j < w; j++) {
1861 for (
int c=0; c <
m_header.channels; c++) {
1862 buff[cnt + channelMap[c]] =
Clamp8((
m_channel[c][yPos] + yuvOffset16) >> shift);
1871 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1887 UINT8 *buffg = &buff[channelMap[1]],
1888 *buffr = &buff[channelMap[2]],
1889 *buffb = &buff[channelMap[0]];
1891 int cnt, channels = bpp/8;
1893 for (i=0; i < h; i++) {
1894 if (i%2) sampledPos -= (w + 1)/2;
1896 for (j=0; j < w; j++) {
1898 uAvg = u[sampledPos];
1899 vAvg = v[sampledPos];
1902 buffr[cnt] =
Clamp8(uAvg + g);
1903 buffb[cnt] =
Clamp8(vAvg + g);
1906 if (j%2) sampledPos++;
1911 if (wOdd) sampledPos++;
1914 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1918 for (i=0; i < h; i++) {
1920 for (j = 0; j < w; j++) {
1925 buffr[cnt] =
Clamp8(uAvg + g);
1926 buffb[cnt] =
Clamp8(vAvg + g);
1936 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1955 if (bpp >= 48 && bpp%16 == 0) {
1957 UINT16 *buff16 = (UINT16 *)buff;
1958 int pitch16 = pitch/2;
1959 channels = bpp/16; ASSERT(channels >=
m_header.channels);
1961 for (i=0; i < h; i++) {
1962 if (i%2) sampledPos -= (w + 1)/2;
1964 for (j=0; j < w; j++) {
1967 uAvg = u[sampledPos];
1968 vAvg = v[sampledPos];
1974 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
1975 buff16[cnt + channelMap[1]] =
Clamp16(g << shift);
1976 buff16[cnt + channelMap[2]] =
Clamp16((uAvg + g) << shift);
1977 buff16[cnt + channelMap[0]] =
Clamp16((vAvg + g) << shift);
1980 if (j%2) sampledPos++;
1983 if (wOdd) sampledPos++;
1987 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
1993 channels = bpp/8; ASSERT(channels >=
m_header.channels);
1995 for (i=0; i < h; i++) {
1996 if (i%2) sampledPos -= (w + 1)/2;
1998 for (j=0; j < w; j++) {
2001 uAvg = u[sampledPos];
2002 vAvg = v[sampledPos];
2008 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2009 buff[cnt + channelMap[1]] =
Clamp8(g >> shift);
2010 buff[cnt + channelMap[2]] =
Clamp8((uAvg + g) >> shift);
2011 buff[cnt + channelMap[0]] =
Clamp8((vAvg + g) >> shift);
2014 if (j%2) sampledPos++;
2017 if (wOdd) sampledPos++;
2021 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2036 int cnt, channels = bpp/8; ASSERT(channels >=
m_header.channels);
2038 for (i=0; i < h; i++) {
2039 if (i%2) sampledPos -= (w + 1)/2;
2041 for (j=0; j < w; j++) {
2044 uAvg = a[sampledPos];
2045 vAvg = b[sampledPos];
2055 if (j%2) sampledPos++;
2058 if (wOdd) sampledPos++;
2062 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2081 UINT16 *buff16 = (UINT16 *)buff;
2082 int pitch16 = pitch/2;
2083 channels = bpp/16; ASSERT(channels >=
m_header.channels);
2085 for (i=0; i < h; i++) {
2086 if (i%2) sampledPos -= (w + 1)/2;
2088 for (j=0; j < w; j++) {
2091 uAvg = a[sampledPos];
2092 vAvg = b[sampledPos];
2097 buff16[cnt + channelMap[0]] =
Clamp16((l[yPos] + yuvOffset16) << shift);
2098 buff16[cnt + channelMap[1]] =
Clamp16((uAvg + yuvOffset16) << shift);
2099 buff16[cnt + channelMap[2]] =
Clamp16((vAvg + yuvOffset16) << shift);
2102 if (j%2) sampledPos++;
2105 if (wOdd) sampledPos++;
2109 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2115 channels = bpp/8; ASSERT(channels >=
m_header.channels);
2117 for (i=0; i < h; i++) {
2118 if (i%2) sampledPos -= (w + 1)/2;
2120 for (j=0; j < w; j++) {
2123 uAvg = a[sampledPos];
2124 vAvg = b[sampledPos];
2129 buff[cnt + channelMap[0]] =
Clamp8((l[yPos] + yuvOffset16) >> shift);
2130 buff[cnt + channelMap[1]] =
Clamp8((uAvg + yuvOffset16) >> shift);
2131 buff[cnt + channelMap[2]] =
Clamp8((vAvg + yuvOffset16) >> shift);
2134 if (j%2) sampledPos++;
2137 if (wOdd) sampledPos++;
2141 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2159 int cnt, channels = bpp/8; ASSERT(channels >=
m_header.channels);
2161 for (i=0; i < h; i++) {
2162 if (i%2) sampledPos -= (w + 1)/2;
2164 for (j=0; j < w; j++) {
2167 uAvg = u[sampledPos];
2168 vAvg = v[sampledPos];
2176 buff[cnt + channelMap[1]] = g =
Clamp8(y[yPos] +
YUVoffset8 - ((uAvg + vAvg ) >> 2));
2177 buff[cnt + channelMap[2]] =
Clamp8(uAvg + g);
2178 buff[cnt + channelMap[0]] =
Clamp8(vAvg + g);
2179 buff[cnt + channelMap[3]] = aAvg;
2182 if (j%2) sampledPos++;
2185 if (wOdd) sampledPos++;
2189 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2210 UINT16 *buff16 = (UINT16 *)buff;
2211 int pitch16 = pitch/2;
2212 channels = bpp/16; ASSERT(channels >=
m_header.channels);
2214 for (i=0; i < h; i++) {
2215 if (i%2) sampledPos -= (w + 1)/2;
2217 for (j=0; j < w; j++) {
2220 uAvg = u[sampledPos];
2221 vAvg = v[sampledPos];
2222 aAvg = a[sampledPos] + yuvOffset16;
2226 aAvg = a[yPos] + yuvOffset16;
2229 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2230 buff16[cnt + channelMap[1]] =
Clamp16(g << shift);
2231 buff16[cnt + channelMap[2]] =
Clamp16((uAvg + g) << shift);
2232 buff16[cnt + channelMap[0]] =
Clamp16((vAvg + g) << shift);
2233 buff16[cnt + channelMap[3]] =
Clamp16(aAvg << shift);
2236 if (j%2) sampledPos++;
2239 if (wOdd) sampledPos++;
2243 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2249 channels = bpp/8; ASSERT(channels >=
m_header.channels);
2251 for (i=0; i < h; i++) {
2252 if (i%2) sampledPos -= (w + 1)/2;
2254 for (j=0; j < w; j++) {
2257 uAvg = u[sampledPos];
2258 vAvg = v[sampledPos];
2259 aAvg = a[sampledPos] + yuvOffset16;
2263 aAvg = a[yPos] + yuvOffset16;
2266 g = y[yPos] + yuvOffset16 - ((uAvg + vAvg ) >> 2);
2267 buff[cnt + channelMap[1]] =
Clamp8(g >> shift);
2268 buff[cnt + channelMap[2]] =
Clamp8((uAvg + g) >> shift);
2269 buff[cnt + channelMap[0]] =
Clamp8((vAvg + g) >> shift);
2270 buff[cnt + channelMap[3]] =
Clamp8(aAvg >> shift);
2273 if (j%2) sampledPos++;
2276 if (wOdd) sampledPos++;
2280 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2286#ifdef __PGF32SUPPORT__
2298 UINT32 *buff32 = (UINT32 *)buff;
2299 int pitch32 = pitch/4;
2301 for (i=0; i < h; i++) {
2302 for (j=0; j < w; j++) {
2303 buff32[j] =
Clamp31((y[yPos++] + yuvOffset31) << shift);
2309 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2312 }
else if (bpp == 16) {
2314 UINT16 *buff16 = (UINT16 *)buff;
2315 int pitch16 = pitch/2;
2317 if (usedBits < 16) {
2318 const int shift = 16 - usedBits;
2319 for (i=0; i < h; i++) {
2320 for (j=0; j < w; j++) {
2321 buff16[j] =
Clamp16((y[yPos++] + yuvOffset31) << shift);
2327 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2331 const int shift =
__max(0, usedBits - 16);
2332 for (i=0; i < h; i++) {
2333 for (j=0; j < w; j++) {
2334 buff16[j] =
Clamp16((y[yPos++] + yuvOffset31) >> shift);
2340 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2348 for (i=0; i < h; i++) {
2349 for (j=0; j < w; j++) {
2350 buff[j] =
Clamp8((y[yPos++] + yuvOffset31) >> shift);
2356 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2367 ASSERT(bpp ==
m_header.channels*4);
2376 for (i=0; i < h; i++) {
2378 for (j=0; j < w; j++) {
2384 buff[cnt] = UINT8(
Clamp4(vAvg + yval) | (yval << 4));
2386 buff[cnt] =
Clamp4(uAvg + yval);
2388 buff[cnt] |=
Clamp4(vAvg + yval) << 4;
2390 buff[cnt] = UINT8(yval | (
Clamp4(uAvg + yval) << 4));
2398 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2414 UINT16 *buff16 = (UINT16 *)buff;
2415 int pitch16 = pitch/2;
2417 for (i=0; i < h; i++) {
2418 for (j=0; j < w; j++) {
2423 buff16[j] = (yval << 5) | ((
Clamp6(uAvg + yval) >> 1) << 11) | (
Clamp6(vAvg + yval) >> 1);
2429 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2438#ifdef __PGFROISUPPORT__
2443 buff = buffStart + (levelRoi.
top - roi.
top)*pitch + (levelRoi.
left - roi.
left)*bypp;
2444 w = levelRoi.
Width()*bypp;
2447 for (i=0; i < h; i++) {
2448 for (j=0; j < w; j++) {
2449 targetBuff[j] = buff[j];
2451 targetBuff += targetPitch;
2458 delete[] buffStart; buffStart = 0;
2481 const bool wOdd = (1 == w%2);
2482 const int dataBits =
DataTSize*8; ASSERT(dataBits == 16 || dataBits == 32);
2485 const double dP = 1.0/h;
2487 int defMap[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; ASSERT(
sizeof(defMap)/
sizeof(defMap[0]) ==
MaxChannels);
2488 if (channelMap == NULL) channelMap = defMap;
2489 int sampledPos = 0, yPos = 0;
2495 ASSERT(bpp%dataBits == 0);
2500 int cnt, channels = bpp/dataBits; ASSERT(channels >=
m_header.channels);
2502 for (i=0; i < h; i++) {
2503 if (i%2) sampledPos -= (w + 1)/2;
2505 for (j=0; j < w; j++) {
2508 uAvg = u[sampledPos];
2509 vAvg = v[sampledPos];
2514 buff[cnt + channelMap[0]] = y[yPos];
2515 buff[cnt + channelMap[1]] = uAvg;
2516 buff[cnt + channelMap[2]] = vAvg;
2519 if (j%2) sampledPos++;
2522 if (wOdd) sampledPos++;
2526 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);
2529 }
else if (
m_header.channels == 4) {
2531 ASSERT(bpp%dataBits == 0);
2538 int cnt, channels = bpp/dataBits; ASSERT(channels >=
m_header.channels);
2540 for (i=0; i < h; i++) {
2541 if (i%2) sampledPos -= (w + 1)/2;
2543 for (j=0; j < w; j++) {
2546 uAvg = u[sampledPos];
2547 vAvg = v[sampledPos];
2548 aAvg =
Clamp8(a[sampledPos] + yuvOffset);
2552 aAvg =
Clamp8(a[yPos] + yuvOffset);
2555 buff[cnt + channelMap[0]] = y[yPos];
2556 buff[cnt + channelMap[1]] = uAvg;
2557 buff[cnt + channelMap[2]] = vAvg;
2558 buff[cnt + channelMap[3]] = aAvg;
2561 if (j%2) sampledPos++;
2564 if (wOdd) sampledPos++;
2568 if ((*cb)(percent,
true, data)) ReturnWithError(EscapePressed);