Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / simpleImage.java
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 int width;
    private int height;
    private byte[] data;
    public simpleImage(BufferedImage img){
        //System.out.println(img.getType());
        width = img.getWidth();
        height = img.getHeight();
        data = ((DataBufferByte)img.getRaster().getDataBuffer()).getData() ;
    }
    public simpleImage(int x, int y){
        //System.out.println(img.getType());
        width = x;
        height = y;
        data = new byte[x*y*3];
    }
    @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){
        return new int[]{
                Byte.toUnsignedInt(data[3*(y*width + x)]),
                Byte.toUnsignedInt(data[3*(y*width + x)+1]),
                Byte.toUnsignedInt(data[3*(y*width + x)+2])};
    }
    public int getRGB(int x, int y){
        int result =
                Byte.toUnsignedInt(data[(3 * y * width + x)])|
                Byte.toUnsignedInt(data[(3 * y * width + x)+1]) << 8|
                Byte.toUnsignedInt(data[(3 * y * width + x)+2]) << 16;
        if(result != 0){
            int debug = 1;
        }
        return result;
    }

    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;
    }
}