Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / simpleImage.java
@cory cory on 25 Feb 2023 3 KB Messing with tornado
package uk.org.floop.epq3d;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;

public class simpleImage extends Image {
    private final int width;
    private final int height;
    private final byte[] data;
    private final int type;
    public simpleImage(BufferedImage img){
        type = img.getType();
        //System.out.println(img.getType());
        width = img.getWidth();
        height = img.getHeight();
        data = ((DataBufferByte)img.getRaster().getDataBuffer()).getData() ;
    }
    @Override
    public int getWidth(ImageObserver observer) {
        return width;
    }
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(ImageObserver observer) {
        return height;
    }
    public int getHeight(){
        return height;
    }
    @Override
    public ImageProducer getSource() {
        return null;
    }

    @Override
    public Graphics getGraphics() {
        return null;
    }

    @Override
    public Object getProperty(String name, ImageObserver observer) {
        return null;
    }
    public int[] getPixel(int x, int y){
        switch (type){
            case BufferedImage.TYPE_INT_RGB:
                return new int[]{
                        Byte.toUnsignedInt(data[3*(y*width + x)+1]),
                        Byte.toUnsignedInt(data[3*(y*width + x)+2]),
                        Byte.toUnsignedInt(data[3*(y*width + x)+3])};
            case BufferedImage.TYPE_4BYTE_ABGR:
                return new int[]{
                        Byte.toUnsignedInt(data[4*(y*width + x)+3]),
                        Byte.toUnsignedInt(data[4*(y*width + x)+2]),
                        Byte.toUnsignedInt(data[4*(y*width + x)+1])};
            case BufferedImage.TYPE_CUSTOM: // but why
                return new int[]{
                        Byte.toUnsignedInt(data[3*(y*width + x)+3]),
                        Byte.toUnsignedInt(data[3*(y*width + x)+1]),
                        Byte.toUnsignedInt(data[3*(y*width + x)+2])};
        }
        throw new RuntimeException("I didn't add this image format: " + type);
    }

    public void setRGB(int x, int y, int i) {
        data[(3 * y * width + x)] = (byte) (i & 0xff);
        data[(3*y*width + x)+1] = (byte) ((i >> 8) & 0xff);
        data[(3*y*width + x)+2] = (byte) ((i >> 16) & 0xff);
    }
    public BufferedImage getBufferedImage(){
//        try {
//            BufferedImage result = ImageIO.read(new ByteArrayInputStream(data));
//            return result;
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for(int x = 0; x < width; x += 1){
            for(int y = 0; y < height; y += 1){
                result.setRGB(x, y,
                                data[3*(y*width + x)]+
                                data[3*(y*width + x)+1]<<8+
                                data[3*(y*width + x)+2]<<16);
            }
        }
        return result;
    }
}