Skip to content

Commit 792f9c8

Browse files
committed
revised color conversion methods
1 parent 13fbea2 commit 792f9c8

File tree

3 files changed

+1710
-1797
lines changed

3 files changed

+1710
-1797
lines changed

core/src/processing/opengl/PGL.java

Lines changed: 105 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,14 +1779,13 @@ protected static int nextPowerOfTwo(int val) {
17791779
* endian) to Java ARGB.
17801780
*/
17811781
protected static int nativeToJavaARGB(int color) {
1782-
if (BIG_ENDIAN) { // RGBA to ARGB
1783-
return (color & 0xff000000) |
1784-
((color >> 8) & 0x00ffffff);
1782+
if (PGL.BIG_ENDIAN) { // RGBA to ARGB
1783+
return (color >>> 8) | ((color << 24) & 0xFF000000);
1784+
// equivalent to
1785+
// ((color >> 8) & 0x00FFFFFF) | ((color << 24) & 0xFF000000)
17851786
} else { // ABGR to ARGB
1786-
return (color & 0xff000000) |
1787-
((color << 16) & 0xff0000) |
1788-
(color & 0xff00) |
1789-
((color >> 16) & 0xff);
1787+
return ((color & 0xFF) << 16) | ((color & 0xFF0000) >> 16) |
1788+
(color & 0xFF00FF00);
17901789
}
17911790
}
17921791

@@ -1801,51 +1800,35 @@ protected static void nativeToJavaARGB(int[] pixels, int width, int height) {
18011800
int index = 0;
18021801
int yindex = (height - 1) * width;
18031802
for (int y = 0; y < height / 2; y++) {
1804-
if (BIG_ENDIAN) { // RGBA to ARGB
1805-
for (int x = 0; x < width; x++) {
1806-
int temp = pixels[index];
1807-
pixels[index] = (pixels[yindex] & 0xff000000) |
1808-
((pixels[yindex] >> 8) & 0x00ffffff);
1809-
pixels[yindex] = (temp & 0xff000000) |
1810-
((temp >> 8) & 0x00ffffff);
1811-
index++;
1812-
yindex++;
1813-
}
1814-
} else { // ABGR to ARGB
1815-
for (int x = 0; x < width; x++) {
1816-
int temp = pixels[index];
1817-
pixels[index] = (pixels[yindex] & 0xff000000) |
1818-
((pixels[yindex] << 16) & 0xff0000) |
1819-
(pixels[yindex] & 0xff00) |
1820-
((pixels[yindex] >> 16) & 0xff);
1821-
pixels[yindex] = (temp & 0xff000000) |
1822-
((temp << 16) & 0xff0000) |
1823-
(temp & 0xff00) |
1824-
((temp >> 16) & 0xff);
1825-
index++;
1826-
yindex++;
1803+
for (int x = 0; x < width; x++) {
1804+
int pixy = pixels[yindex];
1805+
int pixi = pixels[index];
1806+
if (BIG_ENDIAN) { // RGBA to ARGB
1807+
pixels[index] = (pixy >>> 8) | ((pixy << 24) & 0xFF000000);
1808+
pixels[yindex] = (pixi >>> 8) | ((pixi << 24) & 0xFF000000);
1809+
} else { // ABGR to ARGB
1810+
pixels[index] = ((pixy & 0xFF) << 16) | ((pixy & 0xFF0000) >> 16) |
1811+
(pixy & 0xFF00FF00);
1812+
pixels[yindex] = ((pixi & 0xFF) << 16) | ((pixi & 0xFF0000) >> 16) |
1813+
(pixi & 0xFF00FF00);
18271814
}
1815+
index++;
1816+
yindex++;
18281817
}
18291818
yindex -= width * 2;
18301819
}
18311820

1832-
// Flips image
1833-
if ((height % 2) == 1) {
1821+
if ((height % 2) == 1) { // Converts center row
18341822
index = (height / 2) * width;
1835-
if (BIG_ENDIAN) { // RGBA to ARGB
1836-
for (int x = 0; x < width; x++) {
1837-
pixels[index] = (pixels[index] & 0xff000000) |
1838-
((pixels[index] >> 8) & 0x00ffffff);
1839-
index++;
1840-
}
1841-
} else { // ABGR to ARGB
1842-
for (int x = 0; x < width; x++) {
1843-
pixels[index] = (pixels[index] & 0xff000000) |
1844-
((pixels[index] << 16) & 0xff0000) |
1845-
(pixels[index] & 0xff00) |
1846-
((pixels[index] >> 16) & 0xff);
1847-
index++;
1823+
for (int x = 0; x < width; x++) {
1824+
int pixi = pixels[index];
1825+
if (BIG_ENDIAN) { // RGBA to ARGB
1826+
pixels[index] = (pixi >>> 8) | ((pixi << 24) & 0xFF000000);
1827+
} else { // ABGR to ARGB
1828+
pixels[index] = ((pixi & 0xFF) << 16) | ((pixi & 0xFF0000) >> 16) |
1829+
(pixi & 0xFF00FF00);
18481830
}
1831+
index++;
18491832
}
18501833
}
18511834
}
@@ -1858,11 +1841,10 @@ protected static void nativeToJavaARGB(int[] pixels, int width, int height) {
18581841
*/
18591842
protected static int nativeToJavaRGB(int color) {
18601843
if (BIG_ENDIAN) { // RGBA to ARGB
1861-
return ((color << 8) & 0xffffff00) | 0xff;
1844+
return (color >>> 8) | 0xFF000000;
18621845
} else { // ABGR to ARGB
1863-
return 0xff000000 | ((color << 16) & 0xff0000) |
1864-
(color & 0xff00) |
1865-
((color >> 16) & 0xff);
1846+
return ((color & 0xFF) << 16) | ((color & 0xFF0000) >> 16) |
1847+
(color & 0xFF00FF00) | 0xFF000000;
18661848
}
18671849
}
18681850

@@ -1878,45 +1860,35 @@ protected static void nativeToJavaRGB(int[] pixels, int width, int height) {
18781860
int index = 0;
18791861
int yindex = (height - 1) * width;
18801862
for (int y = 0; y < height / 2; y++) {
1881-
if (BIG_ENDIAN) { // RGBA to ARGB
1882-
for (int x = 0; x < width; x++) {
1883-
int temp = pixels[index];
1884-
pixels[index] = 0xff000000 | ((pixels[yindex] >> 8) & 0x00ffffff);
1885-
pixels[yindex] = 0xff000000 | ((temp >> 8) & 0x00ffffff);
1886-
index++;
1887-
yindex++;
1888-
}
1889-
} else { // ABGR to ARGB
1890-
for (int x = 0; x < width; x++) {
1891-
int temp = pixels[index];
1892-
pixels[index] = 0xff000000 | ((pixels[yindex] << 16) & 0xff0000) |
1893-
(pixels[yindex] & 0xff00) |
1894-
((pixels[yindex] >> 16) & 0xff);
1895-
pixels[yindex] = 0xff000000 | ((temp << 16) & 0xff0000) |
1896-
(temp & 0xff00) |
1897-
((temp >> 16) & 0xff);
1898-
index++;
1899-
yindex++;
1863+
for (int x = 0; x < width; x++) {
1864+
int pixy = pixels[yindex];
1865+
int pixi = pixels[index];
1866+
if (BIG_ENDIAN) { // RGBA to ARGB
1867+
pixels[index] = (pixy >>> 8) | 0xFF000000;
1868+
pixels[yindex] = (pixi >>> 8) | 0xFF000000;
1869+
} else { // ABGR to ARGB
1870+
pixels[index] = ((pixy & 0xFF) << 16) | ((pixy & 0xFF0000) >> 16) |
1871+
(pixy & 0xFF00FF00) | 0xFF000000;
1872+
pixels[yindex] = ((pixi & 0xFF) << 16) | ((pixi & 0xFF0000) >> 16) |
1873+
(pixi & 0xFF00FF00) | 0xFF000000;
19001874
}
1875+
index++;
1876+
yindex++;
19011877
}
19021878
yindex -= width * 2;
19031879
}
19041880

1905-
// Flips image
1906-
if ((height % 2) == 1) {
1881+
if ((height % 2) == 1) { // Converts center row
19071882
index = (height / 2) * width;
1908-
if (BIG_ENDIAN) { // RGBA to ARGB
1909-
for (int x = 0; x < width; x++) {
1910-
pixels[index] = 0xff000000 | ((pixels[index] >> 8) & 0x00ffffff);
1911-
index++;
1912-
}
1913-
} else { // ABGR to ARGB
1914-
for (int x = 0; x < width; x++) {
1915-
pixels[index] = 0xff000000 | ((pixels[index] << 16) & 0xff0000) |
1916-
(pixels[index] & 0xff00) |
1917-
((pixels[index] >> 16) & 0xff);
1918-
index++;
1883+
for (int x = 0; x < width; x++) {
1884+
int pixi = pixels[index];
1885+
if (BIG_ENDIAN) { // RGBA to ARGB
1886+
pixels[index] = (pixi >>> 8) | 0xFF000000;
1887+
} else { // ABGR to ARGB
1888+
pixels[index] = ((pixi & 0xFF) << 16) | ((pixi & 0xFF0000) >> 16) |
1889+
(pixi & 0xFF00FF00) | 0xFF000000;
19191890
}
1891+
index++;
19201892
}
19211893
}
19221894
}
@@ -1928,13 +1900,10 @@ protected static void nativeToJavaRGB(int[] pixels, int width, int height) {
19281900
*/
19291901
protected static int javaToNativeARGB(int color) {
19301902
if (BIG_ENDIAN) { // ARGB to RGBA
1931-
return ((color >> 24) & 0xff) |
1932-
((color << 8) & 0xffffff00);
1903+
return ((color >> 24) & 0xFF) | ((color << 8) & 0xFFFFFF00);
19331904
} else { // ARGB to ABGR
1934-
return (color & 0xff000000) |
1935-
((color << 16) & 0xff0000) |
1936-
(color & 0xff00) |
1937-
((color >> 16) & 0xff);
1905+
return (color & 0xFF000000) | ((color << 16) & 0xFF0000) |
1906+
(color & 0xFF00) | ((color >> 16) & 0xFF);
19381907
}
19391908
}
19401909

@@ -1949,52 +1918,35 @@ protected static void javaToNativeARGB(int[] pixels, int width, int height) {
19491918
int index = 0;
19501919
int yindex = (height - 1) * width;
19511920
for (int y = 0; y < height / 2; y++) {
1952-
if (BIG_ENDIAN) { // ARGB to RGBA
1953-
for (int x = 0; x < width; x++) {
1954-
int temp = pixels[index];
1955-
pixels[index] = ((pixels[yindex] >> 24) & 0xff) |
1956-
((pixels[yindex] << 8) & 0xffffff00);
1957-
pixels[yindex] = ((temp >> 24) & 0xff) |
1958-
((temp << 8) & 0xffffff00);
1959-
index++;
1960-
yindex++;
1961-
}
1962-
1963-
} else { // ARGB to ABGR
1964-
for (int x = 0; x < width; x++) {
1965-
int temp = pixels[index];
1966-
pixels[index] = (pixels[yindex] & 0xff000000) |
1967-
((pixels[yindex] << 16) & 0xff0000) |
1968-
(pixels[yindex] & 0xff00) |
1969-
((pixels[yindex] >> 16) & 0xff);
1970-
pixels[yindex] = (temp & 0xff000000) |
1971-
((temp << 16) & 0xff0000) |
1972-
(temp & 0xff00) |
1973-
((temp >> 16) & 0xff);
1974-
index++;
1975-
yindex++;
1921+
for (int x = 0; x < width; x++) {
1922+
int pixy = pixels[yindex];
1923+
int pixi = pixels[index];
1924+
if (BIG_ENDIAN) { // ARGB to RGBA
1925+
pixels[index] = ((pixy >> 24) & 0xFF) | ((pixy << 8) & 0xFFFFFF00);
1926+
pixels[yindex] = ((pixi >> 24) & 0xFF) | ((pixi << 8) & 0xFFFFFF00);
1927+
} else { // ARGB to ABGR
1928+
pixels[index] = (pixy & 0xFF000000) | ((pixy << 16) & 0xFF0000) |
1929+
(pixy & 0xFF00) | ((pixy >> 16) & 0xFF);
1930+
pixels[yindex] = (pixi & 0xFF000000) | ((pixi << 16) & 0xFF0000) |
1931+
(pixi & 0xFF00) | ((pixi >> 16) & 0xFF);
19761932
}
1933+
index++;
1934+
yindex++;
19771935
}
19781936
yindex -= width * 2;
19791937
}
19801938

1981-
// Flips image
1982-
if ((height % 2) == 1) {
1939+
if ((height % 2) == 1) { // Converts center row
19831940
index = (height / 2) * width;
1984-
if (BIG_ENDIAN) { // ARGB to RGBA
1985-
for (int x = 0; x < width; x++) {
1986-
pixels[index] = ((pixels[index] >> 24) & 0xff) |
1987-
((pixels[index] << 8) & 0xffffff00);
1988-
index++;
1989-
}
1990-
} else { // ARGB to ABGR
1991-
for (int x = 0; x < width; x++) {
1992-
pixels[index] = (pixels[index] & 0xff000000) |
1993-
((pixels[index] << 16) & 0xff0000) |
1994-
(pixels[index] & 0xff00) |
1995-
((pixels[index] >> 16) & 0xff);
1996-
index++;
1941+
for (int x = 0; x < width; x++) {
1942+
int pixi = pixels[index];
1943+
if (BIG_ENDIAN) { // ARGB to RGBA
1944+
pixels[index] = ((pixi >> 24) & 0xFF) | ((pixi << 8) & 0xFFFFFF00);
1945+
} else { // ARGB to ABGR
1946+
pixels[index] = (pixi & 0xFF000000) | ((pixi << 16) & 0xFF0000) |
1947+
(pixi & 0xFF00) | ((pixi >> 16) & 0xFF);
19971948
}
1949+
index++;
19981950
}
19991951
}
20001952
}
@@ -2005,12 +1957,11 @@ protected static void javaToNativeARGB(int[] pixels, int width, int height) {
20051957
* BGRA on little endian), setting alpha component to opaque (255).
20061958
*/
20071959
protected static int javaToNativeRGB(int color) {
2008-
if (BIG_ENDIAN) { // ARGB to RGBA
2009-
return ((color << 8) & 0xffffff00) | 0xff;
2010-
} else { // ARGB to ABGR
2011-
return 0xff000000 | ((color << 16) & 0xff0000) |
2012-
(color & 0xff00) |
2013-
((color >> 16) & 0xff);
1960+
if (BIG_ENDIAN) { // ARGB to RGB
1961+
return 0xFF | ((color << 8) & 0xFFFFFF00);
1962+
} else { // ARGB to BGR
1963+
return 0xFF000000 | ((color << 16) & 0xFF0000) |
1964+
(color & 0xFF00) | ((color >> 16) & 0xFF);
20141965
}
20151966
}
20161967

@@ -2026,46 +1977,35 @@ protected static void javaToNativeRGB(int[] pixels, int width, int height) {
20261977
int index = 0;
20271978
int yindex = (height - 1) * width;
20281979
for (int y = 0; y < height / 2; y++) {
2029-
if (BIG_ENDIAN) { // ARGB to RGBA
2030-
for (int x = 0; x < width; x++) {
2031-
int temp = pixels[index];
2032-
pixels[index] = ((pixels[yindex] << 8) & 0xffffff00) | 0xff;
2033-
pixels[yindex] = ((temp << 8) & 0xffffff00) | 0xff;
2034-
index++;
2035-
yindex++;
2036-
}
2037-
2038-
} else {
2039-
for (int x = 0; x < width; x++) { // ARGB to ABGR
2040-
int temp = pixels[index];
2041-
pixels[index] = 0xff000000 | ((pixels[yindex] << 16) & 0xff0000) |
2042-
(pixels[yindex] & 0xff00) |
2043-
((pixels[yindex] >> 16) & 0xff);
2044-
pixels[yindex] = 0xff000000 | ((temp << 16) & 0xff0000) |
2045-
(temp & 0xff00) |
2046-
((temp >> 16) & 0xff);
2047-
index++;
2048-
yindex++;
1980+
for (int x = 0; x < width; x++) {
1981+
int pixy = pixels[yindex];
1982+
int pixi = pixels[index];
1983+
if (BIG_ENDIAN) { // ARGB to RGB
1984+
pixels[index] = 0xFF | ((pixy << 8) & 0xFFFFFF00);
1985+
pixels[yindex] = 0xFF | ((pixi << 8) & 0xFFFFFF00);
1986+
} else { // ARGB to BGR
1987+
pixels[index] = 0xFF000000 | ((pixy << 16) & 0xFF0000) |
1988+
(pixy & 0xFF00) | ((pixy >> 16) & 0xFF);
1989+
pixels[yindex] = 0xFF000000 | ((pixi << 16) & 0xFF0000) |
1990+
(pixi & 0xFF00) | ((pixi >> 16) & 0xFF);
20491991
}
1992+
index++;
1993+
yindex++;
20501994
}
20511995
yindex -= width * 2;
20521996
}
20531997

2054-
// Flips image
2055-
if ((height % 2) == 1) { // ARGB to RGBA
1998+
if ((height % 2) == 1) { // Converts center row
20561999
index = (height / 2) * width;
2057-
if (BIG_ENDIAN) {
2058-
for (int x = 0; x < width; x++) {
2059-
pixels[index] = ((pixels[index] << 8) & 0xffffff00) | 0xff;
2060-
index++;
2061-
}
2062-
} else { // ARGB to ABGR
2063-
for (int x = 0; x < width; x++) {
2064-
pixels[index] = 0xff000000 | ((pixels[index] << 16) & 0xff0000) |
2065-
(pixels[index] & 0xff00) |
2066-
((pixels[index] >> 16) & 0xff);
2067-
index++;
2000+
for (int x = 0; x < width; x++) {
2001+
int pixi = pixels[index];
2002+
if (BIG_ENDIAN) { // ARGB to RGB
2003+
pixels[index] = 0xFF | ((pixi << 8) & 0xFFFFFF00);
2004+
} else { // ARGB to BGR
2005+
pixels[index] = 0xFF000000 | ((pixi << 16) & 0xFF0000) |
2006+
(pixi & 0xFF00) | ((pixi >> 16) & 0xFF);
20682007
}
2008+
index++;
20692009
}
20702010
}
20712011
}

0 commit comments

Comments
 (0)