import java.io.*;
import java.util.*;
public class raw2bin {
	public static void main(String args[]) {
		if (args.length < 0) {
			System.err.println("Usage: java raw2bin <filename>\n");
			System.exit(0);
		}
		Scanner sc = new Scanner(System.in);
		String ifn = ""; //"c:\\data\\programming\\ds\\projects\\nhtest\\subfont.raw"; //args[0];
		if (args.length > 0) {
			ifn = args[0];
		} else {
			System.out.print("Input file: ");
			ifn = sc.nextLine();
		}
		String ofn = ifn.substring(0,ifn.length()-3) + "bin";
		File in = new File(ifn);
		
		System.out.print("Tile width: ");
		int tw = sc.nextInt(); sc.nextLine();
		System.out.print("Tile height: ");
		int th = sc.nextInt(); sc.nextLine();
		
		System.out.print("Image width: ");
		int w = sc.nextInt(); sc.nextLine();
		if (w%tw != 0) {
			System.out.printf("The image does not end on a tile boundary; discarding rightmost %d columns...\n",w%tw);
			w -= w%tw;
		}
		System.out.print("Image height: ");
		int h = sc.nextInt(); sc.nextLine();
		if (h%th != 0) {
			System.out.printf("The image does not end on a tile boundary; discarding lower %d rows...\n",h%th);
			h -= h%th;
		}
		if (h*w > 81408) 
			System.out.println("Warning!  The processed image will be larger than 159KB and\nwill not fit in the amount of VRAM allocated for it!");
		try{
		File out = new File(ofn);
		FileInputStream fis = new FileInputStream(in);
		FileOutputStream fos = new FileOutputStream(out);
		
		byte ibuf[] = new byte[3], obuf[] = new byte[2];
		short img_in[] = new short[w*h], img_out[] = new short[w*h];
		int c=0;
		
		while (fis.available() >= 3) {
			fis.read(ibuf);
			img_in[c++] = RGB24ArrToRGB15(ibuf);
		}
		c=0;
		for (int y=0;y<h/th;y++) {
			for (int x=0;x<w/tw;x++) {
				for (int y2=y*th;y2<y*th+th;y2++) {
					System.arraycopy(img_in,y2*w+x*tw,img_out,(c++)*tw,tw);
					
				}
			}
		}
		for (c=0;c<w*h;c++)
			fos.write(RGB15ToRGB15Arr((short)(img_out[c] | 0x8000)));
				// set bit 15 so it'll work right on a rot/scale BG
		
		} catch (Exception e) {e.printStackTrace();}
		
		System.out.println("Press Enter to continue...");
		sc.nextLine();
	}
	
	static short RGB24ArrToRGB15(byte[] i) {
		// RRRR RRRR GGGG GGGG BBBB BBBB => 0BBB BBGG GGGR RRRR
		byte r = (byte)((i[0] & 0xF8) >> 3), g = (byte)((i[1] & 0xF8) >> 3), b = (byte)((i[2] & 0xF8) >> 3);
		short rgb15 = (short)(r | (g<<5) | (b<<10));
		//byte[] o = { (byte)(rgb15 & 0xFF), (byte)((rgb15 & 0xFF00) >> 8)};
			//System.out.printf("%2X %2X %2X  %4X\n",r,g,b,rgb15);
		
		return rgb15;
	}
	
	static byte[] RGB15ToRGB15Arr(short rgb15) {
		// RRRR RRRR GGGG GGGG BBBB BBBB => 0BBB BBGG GGGR RRRR
		//byte r = (byte)((i[0] & 0xF8) >> 3), g = (byte)((i[1] & 0xF8) >> 3), b = (byte)((i[2] & 0xF8) >> 3);
		//int rgb15 = r | (g<<5) | (b<<10);
		byte[] o = { (byte)(rgb15 & 0xFF), (byte)((rgb15 & 0xFF00) >> 8)};
			//System.out.printf("%2X %2X %2X  %4X\n",r,g,b,rgb15);
		
		return o;
	}
}