| 1790 | // nothing |
| 1791 | #else |
| 1792 | static unsigned char* stbi__convert_format( |
| 1793 | unsigned char* data, int img_n, int req_comp, unsigned int x, unsigned int y) { |
| 1794 | int i, j; |
| 1795 | unsigned char* good; |
| 1796 | |
| 1797 | if (req_comp == img_n) |
| 1798 | return data; |
| 1799 | STBI_ASSERT(req_comp >= 1 && req_comp <= 4); |
| 1800 | |
| 1801 | good = (unsigned char*)stbi__malloc_mad3(req_comp, x, y, 0); |
| 1802 | if (good == NULL) { |
| 1803 | STBI_FREE(data); |
| 1804 | return stbi__errpuc("outofmem", "Out of memory"); |
| 1805 | } |
| 1806 | |
| 1807 | for (j = 0; j < (int)y; ++j) { |
| 1808 | unsigned char* src = data + j * x * img_n; |
| 1809 | unsigned char* dest = good + j * x * req_comp; |
| 1810 | |
| 1811 | #define STBI__COMBO(a, b) ((a)*8 + (b)) |
| 1812 | #define STBI__CASE(a, b) \ |
| 1813 | case STBI__COMBO(a, b): \ |
| 1814 | for (i = x - 1; i >= 0; --i, src += a, dest += b) |
| 1815 | // convert source image with img_n components to one with req_comp components; |
| 1816 | // avoid switch per pixel, so use switch per scanline and massive macros |
| 1817 | switch (STBI__COMBO(img_n, req_comp)) { |
| 1818 | STBI__CASE(1, 2) { |
| 1819 | dest[0] = src[0]; |
| 1820 | dest[1] = 255; |
| 1821 | } |
| 1822 | break; |
| 1823 | STBI__CASE(1, 3) { dest[0] = dest[1] = dest[2] = src[0]; } |
| 1824 | break; |
| 1825 | STBI__CASE(1, 4) { |
| 1826 | dest[0] = dest[1] = dest[2] = src[0]; |
| 1827 | dest[3] = 255; |
| 1828 | } |
| 1829 | break; |
| 1830 | STBI__CASE(2, 1) { dest[0] = src[0]; } |
| 1831 | break; |
| 1832 | STBI__CASE(2, 3) { dest[0] = dest[1] = dest[2] = src[0]; } |
| 1833 | break; |
| 1834 | STBI__CASE(2, 4) { |
| 1835 | dest[0] = dest[1] = dest[2] = src[0]; |
| 1836 | dest[3] = src[1]; |
| 1837 | } |
| 1838 | break; |
| 1839 | STBI__CASE(3, 4) { |
| 1840 | dest[0] = src[0]; |
| 1841 | dest[1] = src[1]; |
| 1842 | dest[2] = src[2]; |
| 1843 | dest[3] = 255; |
| 1844 | } |
| 1845 | break; |
| 1846 | STBI__CASE(3, 1) { dest[0] = stbi__compute_y(src[0], src[1], src[2]); } |
| 1847 | break; |
| 1848 | STBI__CASE(3, 2) { |
| 1849 | dest[0] = stbi__compute_y(src[0], src[1], src[2]); |
no test coverage detected