package main

import "os"
import "io"
import "log"
import "fmt"
import "image"
import _ "image/png"

func main () {
	output, err := os.OpenFile("video.bin", os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0644)
	if err != nil { log.Fatal(err) }

	for frame := 1; frame <= 2516; frame ++ {
		input, err := os.Open(fmt.Sprintf("png/%d.png", frame))
		if err != nil { log.Fatal(err) }
		err = convert(input, output)
		if err != nil { log.Fatal(err) }
		input.Close()
	}
	
	output.Close()
}

func convert (input io.Reader, output io.Writer) (err error) {
	var im image.Image
	im, _, err = image.Decode(input)
	if err != nil { return err }

	buffer := [72]byte { }

	index := 0
	for y := 0; y < 24; y ++ {
		bit := byte(0)
		for x := 0; x < 24; x ++ {
			red, _, _, _ := im.At(x, y).RGBA()
			bitState := byte(0)
			if red > 0x8000 { bitState = 1 }
			buffer[index] |= bitState << (7 - bit)
			
			bit ++
			if bit >= 8 {
				bit = 0
				index ++
			}
		}
	}

	output.Write(buffer[:])
	
	return
}
