package test;

import java.awt.Graphics2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.freedesktop.cairo.Context;
import org.freedesktop.cairo.Format;
import org.freedesktop.cairo.ImageSurface;

public class CairoImage {

    private int width;
    
    private int height;
    
    private ImageSurface surface;
    
    public CairoImage(int width, int height) {
        this.surface = new ImageSurface(Format.ARGB32, width, height);    
    }
    
    public Graphics2D createGraphics2D() {
        Context context = new Context(surface);
        return new CairoGraphics2D(context);
    }
    
    public void writeToPNG(OutputStream out) {
        try {
            surface.writeToPNG(out);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void saveToPNG(File file) {
        try {
            surface.writeToPNG(new FileOutputStream(file));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
