| 1685 | } |
| 1686 | |
| 1687 | static bool DecompressRle(unsigned char *dst, |
| 1688 | const unsigned long uncompressed_size, |
| 1689 | const unsigned char *src, unsigned long src_size) { |
| 1690 | if (uncompressed_size == src_size) { |
| 1691 | // Data is not compressed(Issue 40). |
| 1692 | memcpy(dst, src, src_size); |
| 1693 | return true; |
| 1694 | } |
| 1695 | |
| 1696 | // Workaround for issue #112. |
| 1697 | // TODO(syoyo): Add more robust out-of-bounds check in `rleUncompress`. |
| 1698 | if (src_size <= 2) { |
| 1699 | return false; |
| 1700 | } |
| 1701 | |
| 1702 | std::vector<unsigned char> tmpBuf(uncompressed_size); |
| 1703 | |
| 1704 | int ret = rleUncompress(static_cast<int>(src_size), |
| 1705 | static_cast<int>(uncompressed_size), |
| 1706 | reinterpret_cast<const signed char *>(src), |
| 1707 | reinterpret_cast<char *>(&tmpBuf.at(0))); |
| 1708 | if (ret != static_cast<int>(uncompressed_size)) { |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
| 1712 | // |
| 1713 | // Apply EXR-specific? postprocess. Grabbed from OpenEXR's |
| 1714 | // ImfRleCompressor.cpp |
| 1715 | // |
| 1716 | |
| 1717 | // Predictor. |
| 1718 | { |
| 1719 | unsigned char *t = &tmpBuf.at(0) + 1; |
| 1720 | unsigned char *stop = &tmpBuf.at(0) + uncompressed_size; |
| 1721 | |
| 1722 | while (t < stop) { |
| 1723 | int d = int(t[-1]) + int(t[0]) - 128; |
| 1724 | t[0] = static_cast<unsigned char>(d); |
| 1725 | ++t; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | // Reorder the pixel data. |
| 1730 | { |
| 1731 | const char *t1 = reinterpret_cast<const char *>(&tmpBuf.at(0)); |
| 1732 | const char *t2 = reinterpret_cast<const char *>(&tmpBuf.at(0)) + |
| 1733 | (uncompressed_size + 1) / 2; |
| 1734 | char *s = reinterpret_cast<char *>(dst); |
| 1735 | char *stop = s + uncompressed_size; |
| 1736 | |
| 1737 | for (;;) { |
| 1738 | if (s < stop) |
| 1739 | *(s++) = *(t1++); |
| 1740 | else |
| 1741 | break; |
| 1742 | |
| 1743 | if (s < stop) |
| 1744 | *(s++) = *(t2++); |
no test coverage detected