fixed PNG loader to support indexed color

This commit is contained in:
LAX1DUDE 2022-03-21 01:26:04 -07:00
parent 3e17d57250
commit 94a8c3f6e9
15 changed files with 512 additions and 485 deletions

View File

@ -26,7 +26,6 @@ public class Chunk {
return output;
}
protected Chunk(byte[] length, byte[] type, byte[] data, byte[] crc) {
this.length = ByteHandler.byteToLong(length);
this.data = data;

View File

@ -14,6 +14,12 @@ public enum ChunkType {
png.setIhdr(new IHDR(length, type, data, crc));
}
},
tRNS {
@Override
public void apply(PNG png, byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException {
png.setTrns(new tRNS(length, type, data, crc));
}
},
PLTE {
@Override
public void apply(PNG png, byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException {

View File

@ -10,15 +10,12 @@ public class IHDR extends Chunk {
private long width, height;
private int bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
final private static int[] colorTypeValid = {0, 2, 3, 4, 6};
final private static int[][] mapColorBitDepth = {
{1, 2, 4, 8, 16}, // color type = 0
{},
{8, 16}, // color type = 2
{1, 2, 4, 8}, // color type = 3
{8, 16}, // color type = 4
{},
{8, 16} // color type = 6
final private static int[] colorTypeValid = { 0, 2, 3, 4, 6 };
final private static int[][] mapColorBitDepth = { { 1, 2, 4, 8, 16 }, // color type = 0
{}, { 8, 16 }, // color type = 2
{ 1, 2, 4, 8 }, // color type = 3
{ 8, 16 }, // color type = 4
{}, { 8, 16 } // color type = 6
};
// the number of bytes per complete pixel, rounding up to one
@ -30,7 +27,7 @@ public class IHDR extends Chunk {
} else if (colorType == 3) { // palette index, roll up to 1
return 1;
} else {
//LOG.error("Error when find bpp");
// LOG.error("Error when find bpp");
return 0;
}
}
@ -44,28 +41,33 @@ public class IHDR extends Chunk {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("width=");sb.append(width);
sb.append("height=");sb.append(height);
sb.append("bitDepth=");sb.append(bitDepth);
sb.append("colorType=");sb.append(colorType);
sb.append("compressionMethod=");sb.append(compressionMethod);
sb.append("filterMethod=");sb.append(filterMethod);
sb.append("interlaceMethod=");sb.append(interlaceMethod);
sb.append("width=");
sb.append(width);
sb.append("height=");
sb.append(height);
sb.append("bitDepth=");
sb.append(bitDepth);
sb.append("colorType=");
sb.append(colorType);
sb.append("compressionMethod=");
sb.append(compressionMethod);
sb.append("filterMethod=");
sb.append(filterMethod);
sb.append("interlaceMethod=");
sb.append(interlaceMethod);
return sb.toString();
}
private void build() {
this.width = ByteHandler.byteToLong(data);
this.height = ByteHandler.byteToLong(data, 4);
this.bitDepth = ((int)data[8]) & 0xFF;
this.colorType = ((int)data[9]) & 0xFF;
this.compressionMethod = ((int)data[10]) & 0xFF;
this.filterMethod = ((int)data[11]) & 0xFF;
this.interlaceMethod = ((int)data[12]) & 0xFF;
this.bitDepth = ((int) data[8]) & 0xFF;
this.colorType = ((int) data[9]) & 0xFF;
this.compressionMethod = ((int) data[10]) & 0xFF;
this.filterMethod = ((int) data[11]) & 0xFF;
this.interlaceMethod = ((int) data[12]) & 0xFF;
}
private void checkLegal() throws DecodeException {
boolean legal = false;
for (int c : colorTypeValid) {
@ -82,10 +84,10 @@ public class IHDR extends Chunk {
return;
}
}
throw new DecodeException("Initialzie IHDR : bit depth " + bitDepth + " not valid matching color type " + colorType);
throw new DecodeException(
"Initialzie IHDR : bit depth " + bitDepth + " not valid matching color type " + colorType);
}
public long getWidth() {
return this.width;
}
@ -114,5 +116,4 @@ public class IHDR extends Chunk {
return interlaceMethod;
}
}

View File

@ -2,37 +2,34 @@ package com.baislsl.png.chunk;
import com.baislsl.png.decode.DecodeException;
/**
* Created by baislsl on 17-7-9.
*/
public class PLTE extends Chunk {
private int[] color;
public PLTE(byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException{
public PLTE(byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException {
super(length, type, data, crc);
build();
}
private void build() throws DecodeException{
if(this.length % 3 != 0 )
private void build() throws DecodeException {
if (this.length % 3 != 0)
throw new DecodeException("PLTE length can not be divide by 3");
int size = (int)length / 3;
int size = (int) length / 3;
color = new int[size];
for(int i=0;i<size;i++){
color[i] = (((int)data[i*3]) & 0xFF) << 16 | (((int)data[i*3 + 1]) & 0xFF) << 8 | (((int)data[i*3 + 2]) & 0xFF);
for (int i = 0; i < size; i++) {
color[i] = (((int) data[i * 3]) & 0xFF) << 16 | (((int) data[i * 3 + 1]) & 0xFF) << 8
| (((int) data[i * 3 + 2]) & 0xFF) | 0xFF000000;
}
}
public int getColor(int i){
public int getColor(int i) {
return color[i];
}
public int getPaletteSize(){
public int getPaletteSize() {
return color.length;
}
}

View File

@ -0,0 +1,18 @@
package com.baislsl.png.chunk;
import com.baislsl.png.decode.DecodeException;
/**
* Created by baislsl on 17-7-9.
*/
public class tRNS extends Chunk {
public tRNS(byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException {
super(length, type, data, crc);
}
public int getAlpha() {
return (int)data[0] & 0xFF;
}
}

View File

@ -5,7 +5,8 @@ package com.baislsl.png.decode;
*/
public class DecodeException extends Exception {
public DecodeException() {}
public DecodeException() {
}
public DecodeException(String message) {
super(message);

View File

@ -13,12 +13,10 @@ import static com.baislsl.png.util.ByteHandler.byteToLong;
* Created by baislsl on 17-7-9.
*/
public class Decoder {
//private final static Logger LOG = LoggerFactory.getLogger(Decoder.class);
// private final static Logger LOG = LoggerFactory.getLogger(Decoder.class);
private final InputStream in;
private final static char[] head = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a
};
private final static char[] head = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
public Decoder(InputStream in) {
this.in = in;
@ -30,14 +28,13 @@ public class Decoder {
if ((header[i] & 0xff) != (int) head[i])
throw new DecodeException("It seems that this is not a PNG files");
}
//LOG.info(ByteHandler.byteToString(header));
// LOG.info(ByteHandler.byteToString(header));
}
private boolean readChunk(PNG png, String chunkName,
byte[] length, byte[] type,
byte[] data, byte[] crc) throws IOException, DecodeException {
private boolean readChunk(PNG png, String chunkName, byte[] length, byte[] type, byte[] data, byte[] crc)
throws IOException, DecodeException {
for (ChunkType chunkType : ChunkType.values()) {
if (chunkType.name().equals(chunkName)) {
if (chunkType.name().equalsIgnoreCase(chunkName)) {
chunkType.apply(png, length, type, data, crc);
return true;
}
@ -68,16 +65,16 @@ public class Decoder {
long size = byteToLong(length);
byte[] type = readBytes(4);
chunkName = ByteHandler.byteToString(type).toUpperCase();
if("IEND".equals(chunkName)) {
if ("IEND".equals(chunkName)) {
break;
}
byte[] data = readBytes((int) size);
byte[] crc = readBytes(4);
//LOG.info(ByteHandler.byteToString(type));
// LOG.info(ByteHandler.byteToString(type));
boolean found = readChunk(png, chunkName, length, type, data, crc);
if (!found) {
//LOG.info("Not support chunk name {}", chunkName);
// LOG.info("Not support chunk name {}", chunkName);
}
boolean crcMatch = checkCrc(type, data, crc);
@ -88,13 +85,12 @@ public class Decoder {
return png;
}
private byte[] readBytes(int size) throws IOException {
byte[] result = new byte[size];
int ret = in.read(result, 0, size);
if (ret == -1) return null;
if (ret == -1)
return null;
return result;
}
}

View File

@ -11,20 +11,18 @@ import java.io.IOException;
* Created by baislsl on 17-7-9.
*/
public class PNG {
//private final static Logger LOG = LoggerFactory.getLogger(PNG.class);
// private final static Logger LOG = LoggerFactory.getLogger(PNG.class);
public IHDR ihdr;
public IDATManager idats = new IDATManager();
public PLTE plte;
public tRNS trns;
public IEND iend;
public PNG() {
}
public PNG(IHDR ihdr, IDATManager idats, PLTE plte, IEND iend) {
this.ihdr = ihdr;
this.idats = idats;
this.plte = plte;
this.iend = iend;
public boolean isAlpha() {
return this.trns != null || ihdr.getBpp() == 4;
}
public int[] getColor() throws DecodeException {
@ -48,22 +46,30 @@ public class PNG {
switch (colorType) {
case 2:
if (bitDepth == 8) { // bpp = 3
colors[idx] = ((int)data[i][bpp * j] & 0xff) << 16 | ((int)data[i][bpp * j + 1] & 0xff) << 8 | ((int)data[i][bpp * j + 2] & 0xff);
}else {
colors[idx] = ((int) data[i][bpp * j] & 0xff) << 16 | ((int) data[i][bpp * j + 1] & 0xff) << 8
| ((int) data[i][bpp * j + 2] & 0xff);
} else {
throw new DecodeException("not supported");
}
break;
case 6:
if (bitDepth == 8) { // bpp = 4
colors[idx] = ((int)data[i][bpp * j] & 0xff) << 16 | ((int)data[i][bpp * j + 1] & 0xff) << 8 | ((int)data[i][bpp * j + 2] & 0xff) | ((int)data[i][bpp * j + 3] & 0xff) << 24;
}else {
colors[idx] = ((int) data[i][bpp * j] & 0xff) << 16 | ((int) data[i][bpp * j + 1] & 0xff) << 8
| ((int) data[i][bpp * j + 2] & 0xff) | ((int) data[i][bpp * j + 3] & 0xff) << 24;
} else {
throw new DecodeException("not supported");
}
break;
case 3:
int gap = 8 / bitDepth;
int a = (1 << bitDepth) - 1;
colors[idx] = plte.getColor(data[i][j / gap] & a);
int b = gap - (j % gap) - 1;
int pi = (data[i][j / gap] >> (b * bitDepth)) & a;
if (trns != null && trns.getAlpha() == pi) {
colors[idx] = 0;
}else {
colors[idx] = plte.getColor(pi);
}
break;
default:
throw new DecodeException("Do not support color type " + colorType);
@ -79,10 +85,10 @@ public class PNG {
try {
result = EaglerInflater.uncompress(data);
} catch (IOException e) {
//LOG.error("LZ77 decode error", e);
// LOG.error("LZ77 decode error", e);
throw new DecodeException(e);
}
//LOG.info("Size after decode={}", result.length);
// LOG.info("Size after decode={}", result.length);
return result;
}
@ -103,6 +109,10 @@ public class PNG {
this.plte = plte;
}
public void setTrns(tRNS trns) {
this.trns = trns;
}
public void setIend(IEND iend) {
this.iend = iend;
}

View File

@ -5,16 +5,16 @@ package com.baislsl.png.util;
*/
public class ByteHandler {
public static long byteToLong(byte[] data, int offset, int size){
public static long byteToLong(byte[] data, int offset, int size) {
long result = 0;
for(int i=0;i<size;i++){
for (int i = 0; i < size; i++) {
result <<= 8;
result |= ((long)data[offset + i] & 0xff);
result |= ((long) data[offset + i] & 0xff);
}
return result;
}
public static long byteToLong(byte[] data, int offset){
public static long byteToLong(byte[] data, int offset) {
return byteToLong(data, offset, 4);
}
@ -22,7 +22,6 @@ public class ByteHandler {
return byteToLong(data, 0, 4);
}
public static String byteToString(byte[] data) {
StringBuilder str = new StringBuilder();
for (byte b : data) {

View File

@ -20,16 +20,15 @@ public class CRC {
}
}
private static long updateCrc(long crc, byte[] buf, int size){
private static long updateCrc(long crc, byte[] buf, int size) {
long ans = crc;
for(int i=0;i<size;i++){
ans = crcTable[(int)((ans^buf[i])&0xff)] ^ (ans >> 8);
for (int i = 0; i < size; i++) {
ans = crcTable[(int) ((ans ^ buf[i]) & 0xff)] ^ (ans >> 8);
}
return ans;
}
public static long crc(byte[] buf, int size){
public static long crc(byte[] buf, int size) {
return updateCrc(0xffffffffL, buf, size) ^ 0xffffffffL;
}

View File

@ -1,15 +1,16 @@
package com.baislsl.png.util;
public class ReverseFilter {
private ReverseFilter(){}
private ReverseFilter() {
}
private static int paethPredictor(int a, int b, int c) {
int p = a + b - c;
int pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c);
if (pa <= pb && pa <= pc) return a;
if (pb <= pc) return b;
if (pa <= pb && pa <= pc)
return a;
if (pb <= pc)
return b;
return c;
}
@ -20,9 +21,9 @@ public class ReverseFilter {
int[][] blocks = new int[height][width * bpp];
int dataIndex = 0;
for (int i = 0; i < height; i++) {
filterType[i] = (int)(data[dataIndex++]) & 0xFF;
filterType[i] = (int) (data[dataIndex++]) & 0xFF;
for (int j = 0; j < width * bpp; j++) {
blocks[i][j] = (int)(data[dataIndex++]) & 0xFF;
blocks[i][j] = (int) (data[dataIndex++]) & 0xFF;
}
}
for (int i = 0; i < height; i++) {
@ -39,7 +40,7 @@ public class ReverseFilter {
case 2: // up
blocks[i][j] = blocks[i][j] + prior;
break;
case 3: //average
case 3: // average
blocks[i][j] = blocks[i][j] + (rawBpp + prior) / 2;
break;
case 4: // paeth

View File

@ -39,7 +39,7 @@ public class EaglerImage {
public static final EaglerImage loadImage(byte[] file) {
try {
PNG p = (new Decoder(new ByteArrayInputStream(file))).readInPNG();
return new EaglerImage(p.getColor(), (int)p.getWidth(), (int)p.getHeight(), p.ihdr.getBpp() == 4);
return new EaglerImage(p.getColor(), (int)p.getWidth(), (int)p.getHeight(), p.isAlpha());
} catch (IOException e) {
e.printStackTrace();
return null;