changed Image32 to use ImageIO rather than STBImage
This commit is contained in:
parent
f93f35dae3
commit
931b3fc114
|
@ -0,0 +1,78 @@
|
|||
package com.danitheskunk.skunkworks;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public final class ByteBufferInputStream extends InputStream {
|
||||
private final ByteBuffer buf;
|
||||
|
||||
public ByteBufferInputStream(ByteBuffer buffer) {
|
||||
if(buffer == null) throw new NullPointerException();
|
||||
buf = buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int available() {
|
||||
return buf.remaining();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void mark(int readAheadLimit) {
|
||||
buf.mark();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read() {
|
||||
if(buf.hasRemaining()) return buf.get() & 0xff;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read(byte b[]) {
|
||||
return read(0, b.length, b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read(byte b[], int off, int len) {
|
||||
if((off | len | off + len | b.length - (off + len)) < 0) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
return read(off, len, b);
|
||||
}
|
||||
|
||||
private int read(int off, int len, byte[] b) {
|
||||
if(len == 0) return 0;
|
||||
|
||||
int rem = buf.remaining();
|
||||
if(rem <= 0) return -1;
|
||||
|
||||
if(rem > len) rem = len;
|
||||
buf.get(b, off, rem);
|
||||
return rem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void reset() {
|
||||
buf.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized long skip(long n) {
|
||||
if(n <= 0) return 0;
|
||||
|
||||
int rem = buf.remaining();
|
||||
if(n > rem) n = rem;
|
||||
|
||||
buf.position((int) (buf.position() + n));
|
||||
return n;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,13 @@
|
|||
package com.danitheskunk.skunkworks.gfx;
|
||||
|
||||
import com.danitheskunk.skunkworks.ByteBufferInputStream;
|
||||
import com.danitheskunk.skunkworks.Recti;
|
||||
import com.danitheskunk.skunkworks.Vec2i;
|
||||
import org.lwjgl.stb.STBImage;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class Image32 {
|
||||
|
@ -13,12 +17,26 @@ public class Image32 {
|
|||
//constructors
|
||||
public Image32(ByteBuffer buffer) { //png or similar
|
||||
//todo: resource system
|
||||
int[] x = {0}, y = {0}, n = {0};
|
||||
var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4);
|
||||
size = new Vec2i(x[0], y[0]);
|
||||
data = new byte[x[0] * y[0] * 4];
|
||||
assert img != null;
|
||||
img.get(data);
|
||||
BufferedImage img = null;
|
||||
try {
|
||||
img = ImageIO.read(new ByteBufferInputStream(buffer));
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
size = new Vec2i(img.getWidth(), img.getHeight());
|
||||
data = new byte[img.getWidth() * img.getHeight() * 4];
|
||||
var raster = img.getData();
|
||||
//todo: find faster way?
|
||||
for(int y = 0; y < img.getHeight(); ++y) {
|
||||
for(int x = 0; x < img.getWidth(); ++x) {
|
||||
var pix = raster.getPixel(x, y, (int[])null);
|
||||
int i = x * 4 + y * 4 * img.getWidth();
|
||||
data[i] = (byte)(pix[0] & 0xFF);
|
||||
data[i+1] = (byte)(pix[1] & 0xFF);
|
||||
data[i+2] = (byte)(pix[2] & 0xFF);
|
||||
data[i+3] = (byte)(pix[3] & 0xFF);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Image32(Vec2i size) {
|
||||
|
|
Loading…
Reference in New Issue