import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.spi.*;
import javax.imageio.*;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.*;
import java.util.Iterator;

public class PairTest {

    private static int nTest = 1;

    public static void doTest(int type1, int type2) throws Exception {

        ImageWriterSpi spi = null;
        ImageWriter writer = null;

        IIORegistry reg = IIORegistry.getDefaultInstance();
        Iterator writerSpiIt = reg.getServiceProviders(ImageWriterSpi.class, true);

        while (writerSpiIt.hasNext()) {
            spi = (ImageWriterSpi) writerSpiIt.next();
            if(spi.getDescription(null).contains("TIFF")) { break; }

        }
        writer = spi.createWriterInstance();

        int w = 100, h = 100;
        BufferedImage bimg;
        Graphics g;
        ImageOutputStream ios;

        String f1 = "out-" + nTest + "-1";
        String f2 = "out-" + nTest + "-2";
        nTest++;

        ///////////////////////////////////////////////////////////////////////

        bimg = new BufferedImage(w, h, type1);
        g = bimg.getGraphics();
        g.setColor(Color.gray);
        g.drawRect(0, 0, w / 2, h / 2);
        g.dispose();

        File file = new File(f1);
        ios = ImageIO.createImageOutputStream(file);

        writer.setOutput(ios);
        writer.write(bimg);
        ios.close();

        ///////////////////////////////////////////////////////////////////////

        bimg = new BufferedImage(w, h, type2);
        g = bimg.getGraphics();
        g.setColor(Color.white);
        g.drawRect(0, 0, w, h);
        g.dispose();

        file = new File(f2);
        ios = ImageIO.createImageOutputStream(file);

        writer.setOutput(ios);
        writer.write(bimg);
        ios.close();

    }

    public static void main(String args[]) {

        int types[] = new int[]{
           BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB, BufferedImage.TYPE_INT_ARGB_PRE,
           BufferedImage.TYPE_INT_BGR, BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR,
           BufferedImage.TYPE_4BYTE_ABGR_PRE, BufferedImage.TYPE_BYTE_GRAY, BufferedImage.TYPE_USHORT_GRAY,
           BufferedImage.TYPE_BYTE_BINARY, BufferedImage.TYPE_BYTE_INDEXED, BufferedImage.TYPE_USHORT_565_RGB, BufferedImage.TYPE_USHORT_555_RGB};

        String names[] = new String[]{
           "TYPE_INT_RGB", "TYPE_INT_ARGB", "TYPE_INT_ARGB_PRE",
           "TYPE_INT_BGR", "TYPE_3BYTE_BGR", "TYPE_4BYTE_ABGR",
           "TYPE_4BYTE_ABGR_PRE", "TYPE_BYTE_GRAY", "TYPE_USHORT_GRAY",
           "TYPE_BYTE_BINARY", "TYPE_BYTE_INDEXED", "TYPE_USHORT_565_RGB", "TYPE_USHORT_555_RGB"};


        int n = types.length;

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                try {
                    doTest(types[i], types[j]);
                } catch (Exception e) {
                    System.out.println(names[i] + " -> " + names[j] + ": \t" +
                        "nok, " + e.getClass().getSimpleName());
                }
            }
        }
    }
}
