package com.epson.cameracopy.alt;

import java.io.File;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;

public class SimpleBmpMerger {
    public static boolean margeBmpFile(String str, Iterator<File> it) {
        if (!it.hasNext()) {
            System.out.println("no bmp file");
            return false;
        }
        File next = it.next();
        BmpFileInfo bmpFileInfo = new BmpFileInfo();
        if (!bmpFileInfo.readParams(next.toString())) {
            PrintStream printStream = System.out;
            printStream.println("<" + next.toString() + "> bmp format error");
            return false;
        }
        BmpFileInfo createH0Instance = BmpFileInfo.createH0Instance(bmpFileInfo.getWidth(), bmpFileInfo.getBitPerPixel(), str);
        if (!createH0Instance.prepareAppendData() || !bmpFileInfo.transferImageData(createH0Instance)) {
            return false;
        }
        while (it.hasNext()) {
            try {
                File next2 = it.next();
                BmpFileInfo bmpFileInfo2 = new BmpFileInfo();
                if (!bmpFileInfo2.readParams(next2.toString())) {
                    PrintStream printStream2 = System.out;
                    printStream2.println("<" + next2.toString() + "> bmp format error");
                    createH0Instance.closeWriteableChannel();
                    return false;
                } else if (bmpFileInfo2.getWidth() != createH0Instance.getWidth()) {
                    System.out.println("width not correspond");
                    createH0Instance.closeWriteableChannel();
                    return false;
                } else if (!bmpFileInfo2.transferImageData(createH0Instance)) {
                    return false;
                } else {
                    next2.delete();
                }
            } finally {
                createH0Instance.closeWriteableChannel();
            }
        }
        if (!createH0Instance.updateHeader()) {
            createH0Instance.closeWriteableChannel();
            return false;
        }
        createH0Instance.closeWriteableChannel();
        System.out.println("convert done.");
        return true;
    }

    public static void main(String[] strArr) {
        LinkedList linkedList = new LinkedList();
        linkedList.addFirst(new File("c:/tmp/bo0.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo1.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo2.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo3.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo4.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo5.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo6.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo7.bmp"));
        linkedList.addFirst(new File("c:/tmp/bo8.bmp"));
        if (margeBmpFile("c:/tmp/tbo-all.bmp", linkedList.iterator())) {
            System.out.println("marge ok.");
        } else {
            System.out.println("marge failed");
        }
    }
}
