<?php

$imgList = array();
for ($i = 0; $i < 6; $i++) {
	$imgList[$i] = imagecreatefrompng(__DIR__ . "/zx" . ($i+1) . ".png");
}


$colorList = array(
	0 => 0,
	0xFF0000 => 1+6+6,
	0x00FF00 => 1,
	0x0000FF =>	1+6,
);
$zxImgList = array();
for ($i = 0; $i < 6; $i++) {
	$prev = $i == 0 ? 5 : $i-1;
	$prev2 = $prev == 0 ? 5 : $prev-1;
	$f = fopen(__DIR__ . "/noisehead" . $i . ".asm", "wb");
	$zxImgList[$i] = array();
	for ($y = 0; $y < 48; $y++) {
		$zxImgList[$i][$y] = array();
		for ($x = 0; $x < 32; $x++) {
			$color1 = imagecolorat($imgList[$i], $x*2, $y);
			$color2 = imagecolorat($imgList[$i], $x*2+1, $y);
			$v1 = $colorList[$color1] - $i >= 0 ? $colorList[$color1] - $i : 0;
			$v2 = $colorList[$color2] - $i >= 0 ? $colorList[$color2] - $i : 0;
			
			$color1 = imagecolorat($imgList[$prev], $x*2, $y);
			$color2 = imagecolorat($imgList[$prev], $x*2+1, $y);
			$v11 = $colorList[$color1] - $i - 2 >= 0 ? $colorList[$color1] - $i - 2 : 0;
			$v22 = $colorList[$color2] - $i - 2 >= 0 ? $colorList[$color2] - $i - 2 : 0;	

			$color1 = imagecolorat($imgList[$prev2], $x*2, $y);
			$color2 = imagecolorat($imgList[$prev2], $x*2+1, $y);
			$v111 = $colorList[$color1] - $i - 5 >= 0 ? $colorList[$color1] - $i - 5 : 0;
			$v222 = $colorList[$color2] - $i - 5 >= 0 ? $colorList[$color2] - $i - 5 : 0;
			
			
			$v1 = $v1 | $v11 | $v111;
			$v2 = $v2 | $v22 | $v222;
			
			
			$zxImgList[$i][$y][$x] = $v1*16 + $v2;
			fwrite($f, "\tDB\t" . $zxImgList[$i][$y][$x] . "\r\n")	;
		}
	}
	fclose($f);
}
echo 'OK';
