Alejandro Garcia
Alejandro Garcia

Reputation: 1221

Creating a custom Image format in java

I want to create a custom file image format that holds inside PNG images. I've been investigating how I possibly could it, but I dont have any idea where to start. I want to define the custom file format this way: - The first byte contain the format version - Next 2 bytes contain the number of files that the file holds - The rest of the file contents is the png files copied one after other

I can use a stream in order to read the file, but I dont know how I could convert each png into a BufferedImage since ImageIO reads the full stream. This is all with what I've came with:

public static void main(String[] args) throws IOException {
    DataInputStream input = new DataInputStream(new FileInputStream("C:\\a.spr"));
    byte version = input.readByte();
    short imagenum = input.readShort();
    System.out.printf("version:%s\nimagenum:%s",version,imagenum);
    BufferedImage[] images = new BufferedImage[imagenum];
    for(int i=0;i<imagenum;i++)
    {
         BufferedImage img; //TODO: Read the image
         images[i] = img;
    }
}

Upvotes: 0

Views: 1674

Answers (1)

GavinCattell
GavinCattell

Reputation: 3963

You should investigate the specification for PNG.

In the simplest terms you want to read 4 byte chunks into an array until you find an IEND (0x73 0x69 0x78 0x68). Then feed that into javax.imageio.ImageIO.read();

Upvotes: 1

Related Questions