From fb890d77f52293035d0bb8523e28bf95770f7193 Mon Sep 17 00:00:00 2001 From: LAX1DUDE Date: Sun, 15 May 2022 22:27:31 -0700 Subject: [PATCH] replaced baislsl's PNG decoder with the browser's native PNG decoder --- .../adapter/EaglerAdapterImpl2.java | 15 ++ .../java/com/baislsl/png/chunk/Chunk.java | 62 -------- .../java/com/baislsl/png/chunk/ChunkType.java | 44 ------ src/main/java/com/baislsl/png/chunk/IDAT.java | 11 -- src/main/java/com/baislsl/png/chunk/IEND.java | 10 -- src/main/java/com/baislsl/png/chunk/IHDR.java | 119 ---------------- src/main/java/com/baislsl/png/chunk/PLTE.java | 35 ----- src/main/java/com/baislsl/png/chunk/tRNS.java | 18 --- .../baislsl/png/decode/DecodeException.java | 22 --- .../java/com/baislsl/png/decode/Decoder.java | 96 ------------- .../com/baislsl/png/decode/IDATManager.java | 23 --- src/main/java/com/baislsl/png/decode/PNG.java | 132 ------------------ .../com/baislsl/png/util/ByteHandler.java | 32 ----- src/main/java/com/baislsl/png/util/CRC.java | 35 ----- .../com/baislsl/png/util/ReverseFilter.java | 64 --------- .../net/lax1dude/eaglercraft/EaglerImage.java | 18 +-- .../lax1dude/eaglercraft/EarlyLoadScreen.java | 6 +- .../eaglercraft/TextureTerrainMap.java | 3 +- .../java/net/minecraft/src/FontRenderer.java | 3 +- .../java/net/minecraft/src/RenderEngine.java | 28 ++-- .../net/minecraft/src/TextureManager.java | 3 +- .../src/TexturePackImplementation.java | 3 +- .../adapter/EaglerAdapterImpl2.java | 71 ++++++++++ 23 files changed, 117 insertions(+), 736 deletions(-) delete mode 100644 src/main/java/com/baislsl/png/chunk/Chunk.java delete mode 100644 src/main/java/com/baislsl/png/chunk/ChunkType.java delete mode 100644 src/main/java/com/baislsl/png/chunk/IDAT.java delete mode 100644 src/main/java/com/baislsl/png/chunk/IEND.java delete mode 100644 src/main/java/com/baislsl/png/chunk/IHDR.java delete mode 100644 src/main/java/com/baislsl/png/chunk/PLTE.java delete mode 100644 src/main/java/com/baislsl/png/chunk/tRNS.java delete mode 100644 src/main/java/com/baislsl/png/decode/DecodeException.java delete mode 100644 src/main/java/com/baislsl/png/decode/Decoder.java delete mode 100644 src/main/java/com/baislsl/png/decode/IDATManager.java delete mode 100644 src/main/java/com/baislsl/png/decode/PNG.java delete mode 100644 src/main/java/com/baislsl/png/util/ByteHandler.java delete mode 100644 src/main/java/com/baislsl/png/util/CRC.java delete mode 100644 src/main/java/com/baislsl/png/util/ReverseFilter.java diff --git a/src/lwjgl/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java b/src/lwjgl/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java index 8bc8fc9..e879661 100644 --- a/src/lwjgl/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java +++ b/src/lwjgl/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java @@ -13,6 +13,7 @@ import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -34,6 +35,7 @@ import java.util.LinkedList; import java.util.Set; import java.util.concurrent.TimeUnit; +import javax.imageio.ImageIO; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JPanel; @@ -65,6 +67,7 @@ import org.lwjgl.util.glu.GLU; import de.cuina.fireandfuel.CodecJLayerMP3; import net.lax1dude.eaglercraft.AssetRepository; +import net.lax1dude.eaglercraft.EaglerImage; import net.lax1dude.eaglercraft.EarlyLoadScreen; import net.lax1dude.eaglercraft.ServerQuery; import net.lax1dude.eaglercraft.adapter.EaglerAdapterImpl2.ProgramGL; @@ -607,6 +610,18 @@ public class EaglerAdapterImpl2 { return GL20.glGetAttribLocation(p1.obj, p2); } + public static final EaglerImage loadPNG(byte[] data) { + try { + BufferedImage img = ImageIO.read(new ByteArrayInputStream(data)); + int[] pxls = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth()); + return new EaglerImage(pxls, img.getWidth(), img.getHeight(), true); + } catch (IOException e) { + System.err.println("Could not load PNG file:"); + e.printStackTrace(); + return null; + } + } + public static final boolean isVideoSupported() { return false; } diff --git a/src/main/java/com/baislsl/png/chunk/Chunk.java b/src/main/java/com/baislsl/png/chunk/Chunk.java deleted file mode 100644 index 1a50dd0..0000000 --- a/src/main/java/com/baislsl/png/chunk/Chunk.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.baislsl.png.chunk; - -import com.baislsl.png.util.ByteHandler; - -/** - * Created by baislsl on 17-7-9. - */ -public class Chunk { - protected long length; - protected ChunkType type; - protected byte[] data; - protected byte[] crc = new byte[4]; - - public byte[] dump() { - byte[] output = new byte[4 + 4 + data.length + 4]; - Byte[] lengthBytes = new Byte[4]; - for (int i = 0; i < 4; i++) { - output[3 - i] = (byte) (length & 0xff); - } - String typeName = type.name().toUpperCase(); - for (int i = 0; i < 4; i++) { - output[4 + i] = (byte) typeName.charAt(i); - } - System.arraycopy(data, 0, output, 8, data.length); - System.arraycopy(crc, 0, output, output.length - crc.length, crc.length); - return output; - } - - protected Chunk(byte[] length, byte[] type, byte[] data, byte[] crc) { - this.length = ByteHandler.byteToLong(length); - this.data = data; - this.crc = crc; - - for (ChunkType chunkType : ChunkType.values()) { - if (chunkType.name().equals(ByteHandler.byteToString(type))) { - this.type = chunkType; - break; - } - } - - } - - public long dataLength() { - return data.length; - } - - public long getLength() { - return length; - } - - public ChunkType getType() { - return type; - } - - public byte[] getData() { - return data; - } - - public byte[] getCrc() { - return crc; - } -} diff --git a/src/main/java/com/baislsl/png/chunk/ChunkType.java b/src/main/java/com/baislsl/png/chunk/ChunkType.java deleted file mode 100644 index 91a3181..0000000 --- a/src/main/java/com/baislsl/png/chunk/ChunkType.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baislsl.png.chunk; - -import com.baislsl.png.decode.DecodeException; -import com.baislsl.png.decode.PNG; - -/** - * Created by baislsl on 17-7-9. - */ -public enum ChunkType { - - IHDR { - @Override - public void apply(PNG png, byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException { - 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 { - png.setPlte(new PLTE(length, type, data, crc)); - } - }, - IDAT { - @Override - public void apply(PNG png, byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException { - png.add(new IDAT(length, type, data, crc)); - } - }, - IEND { - @Override - public void apply(PNG png, byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException { - png.setIend(new IEND(length, type, data, crc)); - } - }; - - public abstract void apply(PNG png, byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException; - -} diff --git a/src/main/java/com/baislsl/png/chunk/IDAT.java b/src/main/java/com/baislsl/png/chunk/IDAT.java deleted file mode 100644 index f8d93d5..0000000 --- a/src/main/java/com/baislsl/png/chunk/IDAT.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baislsl.png.chunk; - -/** - * Created by baislsl on 17-7-10. - */ -public class IDAT extends Chunk { - - public IDAT(byte[] length, byte[] type, byte[] data, byte[] crc) { - super(length, type, data, crc); - } -} diff --git a/src/main/java/com/baislsl/png/chunk/IEND.java b/src/main/java/com/baislsl/png/chunk/IEND.java deleted file mode 100644 index 6132e60..0000000 --- a/src/main/java/com/baislsl/png/chunk/IEND.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baislsl.png.chunk; - -/** - * Created by baislsl on 17-7-9. - */ -public class IEND extends Chunk { - public IEND(byte[] length, byte[] type, byte[] data, byte[] crc) { - super(length, type, data, crc); - } -} diff --git a/src/main/java/com/baislsl/png/chunk/IHDR.java b/src/main/java/com/baislsl/png/chunk/IHDR.java deleted file mode 100644 index 1c4e7d5..0000000 --- a/src/main/java/com/baislsl/png/chunk/IHDR.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.baislsl.png.chunk; - -import com.baislsl.png.decode.DecodeException; -import com.baislsl.png.util.ByteHandler; - -/** - * Created by baislsl on 17-7-9. - */ -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 - }; - - // the number of bytes per complete pixel, rounding up to one - public int getBpp() { - if (colorType == 2) { // Each pixel is an R,G,B triple. - return 3; - } else if (colorType == 6) { // Each pixel is an R,G,B triple, followed by an alpha sample. - return 4; - } else if (colorType == 3) { // palette index, roll up to 1 - return 1; - } else { - // LOG.error("Error when find bpp"); - return 0; - } - } - - public IHDR(byte[] length, byte[] type, byte[] data, byte[] crc) throws DecodeException { - super(length, type, data, crc); - build(); - checkLegal(); - } - - @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); - 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; - } - - private void checkLegal() throws DecodeException { - boolean legal = false; - for (int c : colorTypeValid) { - if (c == colorType) { - legal = true; - break; - } - } - if (!legal) { - throw new DecodeException("Initialize IHDR : color type not legal to be " + colorType); - } - for (int b : mapColorBitDepth[colorType]) { - if (b == bitDepth) { - return; - } - } - throw new DecodeException( - "Initialzie IHDR : bit depth " + bitDepth + " not valid matching color type " + colorType); - } - - public long getWidth() { - return this.width; - } - - public long getHeight() { - return this.height; - } - - public int getBitDepth() { - return bitDepth; - } - - public int getColorType() { - return colorType; - } - - public int getCompressionMethod() { - return compressionMethod; - } - - public int getFilterMethod() { - return filterMethod; - } - - public int getInterlaceMethod() { - return interlaceMethod; - } - -} diff --git a/src/main/java/com/baislsl/png/chunk/PLTE.java b/src/main/java/com/baislsl/png/chunk/PLTE.java deleted file mode 100644 index cb6a489..0000000 --- a/src/main/java/com/baislsl/png/chunk/PLTE.java +++ /dev/null @@ -1,35 +0,0 @@ -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 { - super(length, type, data, crc); - build(); - } - - 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; - 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) | 0xFF000000; - } - } - - public int getColor(int i) { - return color[i]; - } - - public int getPaletteSize() { - return color.length; - } - -} diff --git a/src/main/java/com/baislsl/png/chunk/tRNS.java b/src/main/java/com/baislsl/png/chunk/tRNS.java deleted file mode 100644 index bfca19d..0000000 --- a/src/main/java/com/baislsl/png/chunk/tRNS.java +++ /dev/null @@ -1,18 +0,0 @@ -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; - } - -} diff --git a/src/main/java/com/baislsl/png/decode/DecodeException.java b/src/main/java/com/baislsl/png/decode/DecodeException.java deleted file mode 100644 index 63f287b..0000000 --- a/src/main/java/com/baislsl/png/decode/DecodeException.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baislsl.png.decode; - -/** - * Created by baislsl on 17-7-9. - */ -public class DecodeException extends Exception { - - public DecodeException() { - } - - public DecodeException(String message) { - super(message); - } - - public DecodeException(String message, Throwable cause) { - super(message, cause); - } - - public DecodeException(Throwable cause) { - super(cause); - } -} diff --git a/src/main/java/com/baislsl/png/decode/Decoder.java b/src/main/java/com/baislsl/png/decode/Decoder.java deleted file mode 100644 index bcb3552..0000000 --- a/src/main/java/com/baislsl/png/decode/Decoder.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.baislsl.png.decode; - -import com.baislsl.png.chunk.ChunkType; -import com.baislsl.png.util.CRC; -import com.baislsl.png.util.ByteHandler; - -import java.io.IOException; -import java.io.InputStream; - -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 InputStream in; - - private final static char[] head = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; - - public Decoder(InputStream in) { - this.in = in; - } - - private void readHeader() throws DecodeException, IOException { - byte[] header = readBytes(8); - for (int i = 0; i < 8; i++) { - if ((header[i] & 0xff) != (int) head[i]) - throw new DecodeException("It seems that this is not a PNG files"); - } - // LOG.info(ByteHandler.byteToString(header)); - } - - 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().equalsIgnoreCase(chunkName)) { - chunkType.apply(png, length, type, data, crc); - return true; - } - } - return false; - } - - private boolean checkCrc(byte[] data, long crcNumber) { - return crcNumber == CRC.crc(data, data.length); - } - - private boolean checkCrc(byte[] type, byte[] data, byte[] crc) { - long crcNumber = byteToLong(crc); - byte[] crcData = new byte[4 + data.length]; - System.arraycopy(type, 0, crcData, 0, 4); - System.arraycopy(data, 0, crcData, 4, data.length); - - return checkCrc(crcData, crcNumber); - } - - public PNG readInPNG() throws IOException, DecodeException { - PNG png = new PNG(); - readHeader(); - - String chunkName; - do { - byte[] length = readBytes(4); - long size = byteToLong(length); - byte[] type = readBytes(4); - chunkName = ByteHandler.byteToString(type).toUpperCase(); - if ("IEND".equals(chunkName)) { - break; - } - byte[] data = readBytes((int) size); - byte[] crc = readBytes(4); - // LOG.info(ByteHandler.byteToString(type)); - - boolean found = readChunk(png, chunkName, length, type, data, crc); - if (!found) { - // LOG.info("Not support chunk name {}", chunkName); - } - - boolean crcMatch = checkCrc(type, data, crc); - if (!crcMatch) { - throw new DecodeException("Error data stream for incorrect crc"); - } - } while (!"IEND".equals(chunkName)); - 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; - return result; - } - -} diff --git a/src/main/java/com/baislsl/png/decode/IDATManager.java b/src/main/java/com/baislsl/png/decode/IDATManager.java deleted file mode 100644 index a62ac41..0000000 --- a/src/main/java/com/baislsl/png/decode/IDATManager.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baislsl.png.decode; - -import com.baislsl.png.chunk.IDAT; - -import java.util.ArrayList; - -public class IDATManager extends ArrayList { - - public byte[] getIDATData() { - int dataSize = 0; - for (IDAT idat : this) { - dataSize += idat.dataLength(); - } - byte[] data = new byte[dataSize]; - int curPos = 0; - for (IDAT idat : this) { - System.arraycopy(idat.getData(), 0, data, curPos, (int) idat.dataLength()); - curPos += idat.dataLength(); - } - return data; - } - -} diff --git a/src/main/java/com/baislsl/png/decode/PNG.java b/src/main/java/com/baislsl/png/decode/PNG.java deleted file mode 100644 index aca2767..0000000 --- a/src/main/java/com/baislsl/png/decode/PNG.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.baislsl.png.decode; - -import com.baislsl.png.chunk.*; -import com.baislsl.png.util.ReverseFilter; - -import net.lax1dude.eaglercraft.EaglerInflater; - -import java.io.IOException; - -/** - * Created by baislsl on 17-7-9. - */ -public class PNG { - // 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 boolean isAlpha() { - return this.trns != null || ihdr.getBpp() == 4; - } - - public int[] getColor() throws DecodeException { - byte[] rawData = idats.getIDATData(); - byte[] uncompressData = applyLZ77(rawData); - byte[][] transferData = applyReverseFilter(uncompressData); - int[] colors = applyColorTransfer(transferData); - return colors; - } - - private int[] applyColorTransfer(byte[][] data) throws DecodeException { - int bpp = ihdr.getBpp(); - int width = (int) ihdr.getWidth(); - int height = (int) ihdr.getHeight(); - int colorType = ihdr.getColorType(); - int bitDepth = ihdr.getBitDepth(); - int[] colors = new int[width * height]; - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { - int idx = i * width + j; - 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 { - 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 { - throw new DecodeException("not supported"); - } - break; - case 3: - int gap = 8 / bitDepth; - int a = (1 << bitDepth) - 1; - 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); - } - } - } - - return colors; - } - - private byte[] applyLZ77(byte[] data) throws DecodeException { - byte[] result; - try { - result = EaglerInflater.uncompress(data); - } catch (IOException e) { - // LOG.error("LZ77 decode error", e); - throw new DecodeException(e); - } - // LOG.info("Size after decode={}", result.length); - return result; - } - - private byte[][] applyReverseFilter(byte[] data) { - int width = (int) ihdr.getWidth(), height = (int) ihdr.getHeight(); - return ReverseFilter.apply(data, width, height, ihdr.getBpp()); - } - - public void setIdats(IDATManager idats) { - this.idats = idats; - } - - public void setIhdr(IHDR ihdr) { - this.ihdr = ihdr; - } - - public void setPlte(PLTE plte) { - this.plte = plte; - } - - public void setTrns(tRNS trns) { - this.trns = trns; - } - - public void setIend(IEND iend) { - this.iend = iend; - } - - public void add(IDAT idat) throws DecodeException { - idats.add(idat); - } - - public long getWidth() { - return ihdr.getWidth(); - } - - public long getHeight() { - return ihdr.getHeight(); - } - -} diff --git a/src/main/java/com/baislsl/png/util/ByteHandler.java b/src/main/java/com/baislsl/png/util/ByteHandler.java deleted file mode 100644 index 8e07d1d..0000000 --- a/src/main/java/com/baislsl/png/util/ByteHandler.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baislsl.png.util; - -/** - * Created by baislsl on 17-7-10. - */ -public class ByteHandler { - - public static long byteToLong(byte[] data, int offset, int size) { - long result = 0; - for (int i = 0; i < size; i++) { - result <<= 8; - result |= ((long) data[offset + i] & 0xff); - } - return result; - } - - public static long byteToLong(byte[] data, int offset) { - return byteToLong(data, offset, 4); - } - - public static long byteToLong(byte[] data) { - return byteToLong(data, 0, 4); - } - - public static String byteToString(byte[] data) { - StringBuilder str = new StringBuilder(); - for (byte b : data) { - str.append((char) (0x0ff & b)); - } - return str.toString(); - } -} diff --git a/src/main/java/com/baislsl/png/util/CRC.java b/src/main/java/com/baislsl/png/util/CRC.java deleted file mode 100644 index 1d95c27..0000000 --- a/src/main/java/com/baislsl/png/util/CRC.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baislsl.png.util; - -/** - * Created by baislsl on 17-7-9. - */ -public class CRC { - private final static long[] crcTable = new long[256]; - - static { - for (int i = 0; i < 256; i++) { - long c = i; - for (int k = 0; k < 8; k++) { - if ((c & 1) != 0) { - c = 0xedb88320L ^ (c >> 1); - } else { - c >>= 1; - } - } - crcTable[i] = c; - } - } - - 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); - } - return ans; - } - - public static long crc(byte[] buf, int size) { - return updateCrc(0xffffffffL, buf, size) ^ 0xffffffffL; - } - -} diff --git a/src/main/java/com/baislsl/png/util/ReverseFilter.java b/src/main/java/com/baislsl/png/util/ReverseFilter.java deleted file mode 100644 index 9016fdc..0000000 --- a/src/main/java/com/baislsl/png/util/ReverseFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.baislsl.png.util; - -public class 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; - return c; - } - - // apply reverse Filter Algorithms to byte data - // bpp = 3 - public static byte[][] apply(byte[] data, int width, int height, int bpp) { - int[] filterType = new int[height]; - int[][] blocks = new int[height][width * bpp]; - int dataIndex = 0; - for (int i = 0; i < height; i++) { - filterType[i] = (int) (data[dataIndex++]) & 0xFF; - for (int j = 0; j < width * bpp; j++) { - blocks[i][j] = (int) (data[dataIndex++]) & 0xFF; - } - } - for (int i = 0; i < height; i++) { - for (int j = 0; j < width * bpp; j++) { - int prior = (i == 0) ? 0 : blocks[i - 1][j]; - int rawBpp = (j < bpp) ? 0 : blocks[i][j - bpp]; - int bppPrior = (i == 0 || j < bpp) ? 0 : blocks[i - 1][j - bpp]; - switch (filterType[i]) { - case 0: // none - break; - case 1: // sub - blocks[i][j] = blocks[i][j] + rawBpp; - break; - case 2: // up - blocks[i][j] = blocks[i][j] + prior; - break; - case 3: // average - blocks[i][j] = blocks[i][j] + (rawBpp + prior) / 2; - break; - case 4: // paeth - blocks[i][j] = blocks[i][j] + paethPredictor(rawBpp, prior, bppPrior); - break; - default: - } - blocks[i][j] &= 0xff; - } - } - - byte[][] result = new byte[height][width * bpp]; - for (int i = 0; i < height; i++) { - for (int j = 0; j < width * bpp; j++) { - result[i][j] = (byte) blocks[i][j]; - } - } - return result; - } - -} diff --git a/src/main/java/net/lax1dude/eaglercraft/EaglerImage.java b/src/main/java/net/lax1dude/eaglercraft/EaglerImage.java index 049fdb5..176c2c0 100644 --- a/src/main/java/net/lax1dude/eaglercraft/EaglerImage.java +++ b/src/main/java/net/lax1dude/eaglercraft/EaglerImage.java @@ -1,12 +1,5 @@ package net.lax1dude.eaglercraft; -import java.io.ByteArrayInputStream; -import java.io.IOException; - -import com.baislsl.png.decode.DecodeException; -import com.baislsl.png.decode.Decoder; -import com.baislsl.png.decode.PNG; - public class EaglerImage { public final int[] data; @@ -37,16 +30,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.isAlpha()); - } catch (IOException e) { - e.printStackTrace(); - return null; - } catch (DecodeException e) { - e.printStackTrace(); - return null; - } + return EaglerAdapter.loadPNG(file); } public EaglerImage getSubImage(int x, int y, int pw, int ph) { diff --git a/src/main/java/net/lax1dude/eaglercraft/EarlyLoadScreen.java b/src/main/java/net/lax1dude/eaglercraft/EarlyLoadScreen.java index 9471bcc..4851e13 100644 --- a/src/main/java/net/lax1dude/eaglercraft/EarlyLoadScreen.java +++ b/src/main/java/net/lax1dude/eaglercraft/EarlyLoadScreen.java @@ -24,7 +24,8 @@ public class EarlyLoadScreen { _wglTexParameteri(_wGL_TEXTURE_2D, _wGL_TEXTURE_MIN_FILTER, _wGL_NEAREST); _wglTexParameteri(_wGL_TEXTURE_2D, _wGL_TEXTURE_WRAP_S, _wGL_CLAMP); _wglTexParameteri(_wGL_TEXTURE_2D, _wGL_TEXTURE_WRAP_T, _wGL_CLAMP); - EaglerImage img = EaglerImage.loadImage(Base64.decodeBase64(loadScreen)); + //EaglerImage img = EaglerImage.loadImage(Base64.decodeBase64(loadScreen)); + EaglerImage img = EaglerAdapter.loadPNG(Base64.decodeBase64(loadScreen)); IntBuffer upload = GLAllocation.createDirectIntBuffer(192*192); upload.put(img.data); upload.flip(); @@ -116,7 +117,8 @@ public class EarlyLoadScreen { _wglTexParameteri(_wGL_TEXTURE_2D, _wGL_TEXTURE_MIN_FILTER, _wGL_NEAREST); _wglTexParameteri(_wGL_TEXTURE_2D, _wGL_TEXTURE_WRAP_S, _wGL_CLAMP); _wglTexParameteri(_wGL_TEXTURE_2D, _wGL_TEXTURE_WRAP_T, _wGL_CLAMP); - EaglerImage img = EaglerImage.loadImage(Base64.decodeBase64(enableScreen)); + //EaglerImage img = EaglerImage.loadImage(Base64.decodeBase64(enableScreen)); + EaglerImage img = EaglerAdapter.loadPNG(Base64.decodeBase64(enableScreen)); IntBuffer upload = GLAllocation.createDirectIntBuffer(128*128); upload.put(img.data); upload.flip(); diff --git a/src/main/java/net/lax1dude/eaglercraft/TextureTerrainMap.java b/src/main/java/net/lax1dude/eaglercraft/TextureTerrainMap.java index 66f54df..cdc8630 100644 --- a/src/main/java/net/lax1dude/eaglercraft/TextureTerrainMap.java +++ b/src/main/java/net/lax1dude/eaglercraft/TextureTerrainMap.java @@ -134,7 +134,8 @@ public class TextureTerrainMap implements IconRegister { if(data == null) { map.replaceTexture(this, map.missingData); }else { - EaglerImage img = EaglerImage.loadImage(data); + //EaglerImage img = EaglerImage.loadImage(data); + EaglerImage img = EaglerAdapter.loadPNG(data); if(img == null) { map.replaceTexture(this, map.missingData); }else { diff --git a/src/main/java/net/minecraft/src/FontRenderer.java b/src/main/java/net/minecraft/src/FontRenderer.java index 53c33f7..c016dea 100644 --- a/src/main/java/net/minecraft/src/FontRenderer.java +++ b/src/main/java/net/minecraft/src/FontRenderer.java @@ -130,7 +130,8 @@ public class FontRenderer { } private void readFontTexture(String par1Str) { - EaglerImage e = EaglerImage.loadImage(EaglerAdapter.loadResourceBytes(par1Str)); + //EaglerImage e = EaglerImage.loadImage(EaglerAdapter.loadResourceBytes(par1Str)); + EaglerImage e = EaglerAdapter.loadPNG(EaglerAdapter.loadResourceBytes(par1Str)); int[] var5 = e.data; int var3 = e.w; int var4 = e.h; diff --git a/src/main/java/net/minecraft/src/RenderEngine.java b/src/main/java/net/minecraft/src/RenderEngine.java index 96b308c..d3142d9 100644 --- a/src/main/java/net/minecraft/src/RenderEngine.java +++ b/src/main/java/net/minecraft/src/RenderEngine.java @@ -67,7 +67,8 @@ public class RenderEngine { if (var7 == null) { var4 = this.missingTextureImage.data; } else { - var4 = EaglerImage.loadImage(var7).data; + //var4 = EaglerImage.loadImage(var7).data; + var4 = EaglerAdapter.loadPNG(var7).data; } this.textureContentsMap.put(par1Str, var4); @@ -124,7 +125,7 @@ public class RenderEngine { if (var6 == null) { this.setupTextureExt(this.missingTextureImage, var3, var9, var5); } else { - this.setupTextureExt(this.readTextureImage(var6), var3, var9, var5); + this.setupTextureExt(EaglerImage.loadImage(var6), var3, var9, var5); } this.textureMap.put(var8, Integer.valueOf(var3)); @@ -299,13 +300,16 @@ public class RenderEngine { var11 = var11.substring(7); } - byte[] b = var1.getResourceAsBytes(var11); - if(b != null) { - EaglerImage var5 = this.readTextureImage(b); + //byte[] b = var1.getResourceAsBytes(var11); //what the fuck + //if(b != null) { + EaglerImage var5 = EaglerAdapter.loadPNG(var1.getResourceAsBytes(var11)); + if(var5 == null) { + throw new IOException("Could not load PNG"); + } this.setupTextureExt(var5, var12, var6, var7); - }else { - System.err.println("could not reload: "+var11); - } + //}else { + // System.err.println("could not reload: "+var11); + //} } catch (IOException var9) { var9.printStackTrace(); } @@ -317,7 +321,10 @@ public class RenderEngine { var11 = (String) var2.next(); try { - var4 = this.readTextureImage(var1.getResourceAsBytes(var11)); + var4 = EaglerAdapter.loadPNG(var1.getResourceAsBytes(var11)); + if(var4 == null) { + throw new IOException("Could not load PNG"); + } System.arraycopy(var4.data, 0, (int[]) this.textureContentsMap.get(var11), 0, var4.data.length); } catch (IOException var8) { var8.printStackTrace(); @@ -332,7 +339,8 @@ public class RenderEngine { * Returns a BufferedImage read off the provided input stream. Args: inputStream */ private EaglerImage readTextureImage(byte[] par1InputStream) throws IOException { - return EaglerImage.loadImage(par1InputStream); + return null;//EaglerImage.loadImage(par1InputStream); + //return EaglerAdapter.loadPNG(par1InputStream); } public void refreshTextureMaps() { diff --git a/src/main/java/net/minecraft/src/TextureManager.java b/src/main/java/net/minecraft/src/TextureManager.java index 3a55f59..a3da463 100644 --- a/src/main/java/net/minecraft/src/TextureManager.java +++ b/src/main/java/net/minecraft/src/TextureManager.java @@ -52,7 +52,8 @@ public class TextureManager { ITexturePack var3 = Minecraft.getMinecraft().texturePackList.getSelectedTexturePack(); byte[] b = var3.getResourceAsBytes("/" + par1Str); if(b != null) { - EaglerImage var9 = EaglerImage.loadImage(b); + //EaglerImage var9 = EaglerImage.loadImage(b); + EaglerImage var9 = EaglerAdapter.loadPNG(b); int var10 = var9.w; int var11 = var9.h; String var12 = this.getBasename(par1Str); diff --git a/src/main/java/net/minecraft/src/TexturePackImplementation.java b/src/main/java/net/minecraft/src/TexturePackImplementation.java index 823f652..dd72083 100644 --- a/src/main/java/net/minecraft/src/TexturePackImplementation.java +++ b/src/main/java/net/minecraft/src/TexturePackImplementation.java @@ -62,7 +62,8 @@ public abstract class TexturePackImplementation implements ITexturePack { * Load and initialize thumbnailImage from the the /pack.png file. */ private void loadThumbnailImage() { - this.thumbnailImage = EaglerImage.loadImage(EaglerAdapter.loadResourceBytes("/pack.png")); + //this.thumbnailImage = EaglerImage.loadImage(EaglerAdapter.loadResourceBytes("/pack.png")); + this.thumbnailImage = EaglerAdapter.loadPNG(EaglerAdapter.loadResourceBytes("/pack.png")); } /** diff --git a/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java b/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java index 745db88..d3c1645 100644 --- a/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java +++ b/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java @@ -25,6 +25,8 @@ import org.teavm.jso.ajax.ReadyStateChangeHandler; import org.teavm.jso.ajax.XMLHttpRequest; import org.teavm.jso.browser.TimerHandler; import org.teavm.jso.browser.Window; +import org.teavm.jso.canvas.CanvasRenderingContext2D; +import org.teavm.jso.canvas.ImageData; import org.teavm.jso.dom.events.Event; import org.teavm.jso.dom.events.EventListener; import org.teavm.jso.dom.events.KeyboardEvent; @@ -41,6 +43,7 @@ import org.teavm.jso.typedarrays.ArrayBuffer; import org.teavm.jso.typedarrays.Float32Array; import org.teavm.jso.typedarrays.Int32Array; import org.teavm.jso.typedarrays.Uint8Array; +import org.teavm.jso.typedarrays.Uint8ClampedArray; import org.teavm.jso.webaudio.AudioBuffer; import org.teavm.jso.webaudio.AudioBufferSourceNode; import org.teavm.jso.webaudio.AudioContext; @@ -63,6 +66,7 @@ import org.teavm.jso.websocket.WebSocket; import net.lax1dude.eaglercraft.AssetRepository; import net.lax1dude.eaglercraft.Base64; +import net.lax1dude.eaglercraft.EaglerImage; import net.lax1dude.eaglercraft.EarlyLoadScreen; import net.lax1dude.eaglercraft.LocalStorageManager; import net.lax1dude.eaglercraft.ServerQuery; @@ -888,6 +892,73 @@ public class EaglerAdapterImpl2 { return getString("window.navigator.platform").toLowerCase().contains("win"); } + private static HTMLCanvasElement imageLoadCanvas = null; + private static CanvasRenderingContext2D imageLoadContext = null; + + @JSBody(params = { "buf", "mime" }, script = "return URL.createObjectURL(new Blob([buf], {type: mime}));") + private static native String getDataURL(ArrayBuffer buf, String mime); + + @JSBody(params = { "url" }, script = "URL.revokeObjectURL(url);") + private static native void freeDataURL(String url); + + public static final EaglerImage loadPNG(byte[] data) { + ArrayBuffer arr = ArrayBuffer.create(data.length); + Uint8Array.create(arr).set(data); + return loadPNG0(arr); + } + + @Async + private static native EaglerImage loadPNG0(ArrayBuffer data); + + private static void loadPNG0(ArrayBuffer data, final AsyncCallback ret) { + final HTMLImageElement toLoad = (HTMLImageElement) doc.createElement("img"); + toLoad.addEventListener("load", new EventListener() { + @Override + public void handleEvent(Event evt) { + if(imageLoadCanvas == null) { + imageLoadCanvas = (HTMLCanvasElement) doc.createElement("canvas"); + } + if(imageLoadCanvas.getWidth() < toLoad.getWidth()) { + imageLoadCanvas.setWidth(toLoad.getWidth()); + } + if(imageLoadCanvas.getHeight() < toLoad.getHeight()) { + imageLoadCanvas.setHeight(toLoad.getHeight()); + } + if(imageLoadContext == null) { + imageLoadContext = (CanvasRenderingContext2D) imageLoadCanvas.getContext("2d"); + } + imageLoadContext.clearRect(0, 0, toLoad.getWidth(), toLoad.getHeight()); + imageLoadContext.drawImage(toLoad, 0, 0, toLoad.getWidth(), toLoad.getHeight()); + ImageData pxlsDat = imageLoadContext.getImageData(0, 0, toLoad.getWidth(), toLoad.getHeight()); + Uint8ClampedArray pxls = pxlsDat.getData(); + int totalPixels = pxlsDat.getWidth() * pxlsDat.getHeight(); + freeDataURL(toLoad.getSrc()); + if(pxls.getByteLength() < totalPixels * 4) { + ret.complete(null); + return; + } + int[] pixels = new int[totalPixels]; + for(int i = 0; i < pixels.length; ++i) { + pixels[i] = (pxls.get(i * 4) << 16) | (pxls.get(i * 4 + 1) << 8) | pxls.get(i * 4 + 2) | (pxls.get(i * 4 + 3) << 24); + } + ret.complete(new EaglerImage(pixels, pxlsDat.getWidth(), pxlsDat.getHeight(), true)); + } + }); + toLoad.addEventListener("error", new EventListener() { + @Override + public void handleEvent(Event evt) { + freeDataURL(toLoad.getSrc()); + ret.complete(null); + } + }); + String src = getDataURL(data, "image/png"); + if(src == null) { + ret.complete(null); + }else { + toLoad.setSrc(src); + } + } + private static HTMLVideoElement currentVideo = null; private static TextureGL videoTexture = null; private static boolean videoIsLoaded = false;