From 46a94c2b924630e7e47c39195bdadd49469d3950 Mon Sep 17 00:00:00 2001 From: DaniTheSkunk Date: Wed, 14 Sep 2022 06:16:20 +0200 Subject: [PATCH] added image functions --- Test.java | 4 ++++ com/danitheskunk/skunkworks/Color.java | 16 ++++++++-------- com/danitheskunk/skunkworks/Image.java | 26 ++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Test.java b/Test.java index 60135df..ce07a51 100644 --- a/Test.java +++ b/Test.java @@ -1,10 +1,14 @@ import com.danitheskunk.skunkworks.Engine; import com.danitheskunk.skunkworks.GLWindow; +import com.danitheskunk.skunkworks.Vec2f; +import com.danitheskunk.skunkworks.Vec2i; public class Test { public static void main(String[] args) { var engine = new Engine(); var window = engine.openWindow(1280, 720, "Skunkworks"); + var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); + System.out.println(img.getPixel(new Vec2i(0, 0))); while(!window.shouldClose()) { window.tick(); diff --git a/com/danitheskunk/skunkworks/Color.java b/com/danitheskunk/skunkworks/Color.java index 52db3e8..f75a3bb 100644 --- a/com/danitheskunk/skunkworks/Color.java +++ b/com/danitheskunk/skunkworks/Color.java @@ -1,35 +1,35 @@ package com.danitheskunk.skunkworks; public final class Color { - final int r, g, b, a; + final byte r, g, b, a; - public Color(int r, int g, int b) { + public Color(byte r, byte g, byte b) { this.r = r; this.g = g; this.b = b; - this.a = 0xff; + this.a = (byte)0xff; } - public Color(int r, int g, int b, int a) { + public Color(byte r, byte g, byte b, byte a) { this.r = r; this.g = g; this.b = b; this.a = a; } - public int getR() { + public byte getR() { return r; } - public int getG() { + public byte getG() { return g; } - public int getB() { + public byte getB() { return b; } - public int getA() { + public byte getA() { return a; } } diff --git a/com/danitheskunk/skunkworks/Image.java b/com/danitheskunk/skunkworks/Image.java index 4c2ab7b..e5d7490 100644 --- a/com/danitheskunk/skunkworks/Image.java +++ b/com/danitheskunk/skunkworks/Image.java @@ -5,13 +5,35 @@ import org.lwjgl.stb.STBImage; import java.nio.ByteBuffer; public class Image { - ByteBuffer data; + byte[] data; int width, height; Image(ByteBuffer buffer) { //todo: resorce system int[] x = {0}, y = {0}, n = {0}; - data = STBImage.stbi_load(buffer, x, y, n, 4); + data = STBImage.stbi_load(buffer, x, y, n, 4).array(); width = x[0]; height = y[0]; } + + public byte[] getData() { + return data; + } + + public Color getPixel(Vec2i pos) { + int i = pos.getX() * 4 + pos.getY() * 4 * width; + return new Color( + data[i + 0], + data[i + 1], + data[i + 2], + data[i + 3] + ); + } + + public void setPixel(Vec2i pos, Color col) { + int i = pos.getX() * 4 + pos.getY() * 4 * width; + data[i + 0] = col.r; + data[i + 1] = col.g; + data[i + 2] = col.b; + data[i + 3] = col.a; + } }