implemented image reader
This commit is contained in:
@ -1,9 +1,74 @@
|
||||
package reader
|
||||
|
||||
import "maze-solver/maze"
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"os"
|
||||
|
||||
type ImageReader struct{}
|
||||
"golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
func (r *ImageReader) Read(filename string) (*maze.Maze, error) {
|
||||
return nil, nil
|
||||
type ImageReader struct {
|
||||
Filename string
|
||||
PathColor, WallColor color.Color
|
||||
CellWidth, CellHeight int
|
||||
}
|
||||
|
||||
func (r *ImageReader) Read() (*RawMaze, error) {
|
||||
image, err := r.getShrunkImage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
width, height := image.Bounds().Max.X, image.Bounds().Max.Y
|
||||
ret := &RawMaze{
|
||||
Width: width,
|
||||
Height: height,
|
||||
Data: make([][]byte, height),
|
||||
}
|
||||
|
||||
n_chunks := width/CHUNK_SIZE + 1
|
||||
|
||||
for i := 0; i < height; i++ {
|
||||
ret.Data[i] = make([]byte, n_chunks)
|
||||
}
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
for i := 0; i < n_chunks; i++ {
|
||||
var chunk byte = 0 // all walls
|
||||
|
||||
end_index := min((i+1)*CHUNK_SIZE, width)
|
||||
|
||||
for x := i * CHUNK_SIZE; x < end_index; x++ {
|
||||
c := image.At(x, y)
|
||||
if c == r.PathColor {
|
||||
chunk |= 1 << (CHUNK_SIZE - 1 - (x - i*CHUNK_SIZE))
|
||||
}
|
||||
}
|
||||
|
||||
ret.Data[y][i] = chunk
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *ImageReader) getShrunkImage() (*image.RGBA, error) {
|
||||
input, err := os.Open(r.Filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer input.Close()
|
||||
|
||||
// Decode the image (from PNG to image.Image):
|
||||
src, _ := png.Decode(input)
|
||||
|
||||
// Set the expected size that you want:
|
||||
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/r.CellWidth, src.Bounds().Max.Y/r.CellHeight))
|
||||
|
||||
// Resize:
|
||||
draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
|
||||
|
||||
return dst, nil
|
||||
}
|
||||
|
112
io/reader/image_test.go
Normal file
112
io/reader/image_test.go
Normal file
@ -0,0 +1,112 @@
|
||||
package reader
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"maze-solver/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImageReader(t *testing.T) {
|
||||
white := color.RGBA{255, 255, 255, 255}
|
||||
black := color.RGBA{0, 0, 0, 255}
|
||||
tests := []struct {
|
||||
name string
|
||||
width, height int
|
||||
cellWidth, cellHeight int
|
||||
pathColor, wallColor color.Color
|
||||
filename string
|
||||
expected [][]byte
|
||||
}{
|
||||
{
|
||||
"Trivial",
|
||||
5, 3,
|
||||
40, 40,
|
||||
white, black,
|
||||
"../../assets/trivial.png",
|
||||
[][]byte{
|
||||
{0b_00100_000},
|
||||
{0b_01110_000},
|
||||
{0b_00010_000},
|
||||
},
|
||||
},
|
||||
{
|
||||
"Trivial Bigger",
|
||||
7, 5,
|
||||
40, 40,
|
||||
white, black,
|
||||
"../../assets/bigger.png",
|
||||
[][]byte{
|
||||
{0b_0001000_0},
|
||||
{0b_0001000_0},
|
||||
{0b_0111110_0},
|
||||
{0b_0000010_0},
|
||||
{0b_0000010_0},
|
||||
},
|
||||
},
|
||||
{
|
||||
"Bigger Staggered",
|
||||
7, 5,
|
||||
40, 40,
|
||||
|
||||
white, black,
|
||||
"../../assets/bigger_staggered.png",
|
||||
[][]byte{
|
||||
{0b_0001000_0},
|
||||
{0b_0001000_0},
|
||||
{0b_0111110_0},
|
||||
{0b_0000100_0},
|
||||
{0b_0000100_0},
|
||||
},
|
||||
},
|
||||
{
|
||||
"Normal",
|
||||
11, 11,
|
||||
40, 40,
|
||||
white, black,
|
||||
"../../assets/normal.png",
|
||||
[][]byte{
|
||||
{0b_00000100, 0b000_00000},
|
||||
{0b_01111101, 0b110_00000},
|
||||
{0b_00000100, 0b010_00000},
|
||||
{0b_01110111, 0b110_00000},
|
||||
{0b_01010000, 0b010_00000},
|
||||
{0b_01011111, 0b110_00000},
|
||||
{0b_00010001, 0b010_00000},
|
||||
{0b_01110111, 0b010_00000},
|
||||
{0b_01000000, 0b010_00000},
|
||||
{0b_01111101, 0b110_00000},
|
||||
{0b_00000100, 0b000_00000},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
reader := ImageReader{
|
||||
Filename: test.filename,
|
||||
PathColor: test.pathColor,
|
||||
WallColor: test.wallColor,
|
||||
CellWidth: test.cellWidth,
|
||||
CellHeight: test.cellHeight,
|
||||
}
|
||||
got, err := reader.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("%s: got error while reading, got\n%v", test.filename, err)
|
||||
}
|
||||
|
||||
utils.AssertEqual(t, got.Width, test.width, "%s: width of raw maze don't match", test.name)
|
||||
utils.AssertEqual(t, got.Height, test.height, "%s: height of raw maze don't match", test.name)
|
||||
utils.AssertEqual(t, len(got.Data), len(test.expected), "%s: don't have the same number of rows", test.name)
|
||||
|
||||
for y, line_exp := range test.expected {
|
||||
line_got := got.Data[y]
|
||||
utils.AssertEqual(t, len(line_got), len(line_exp), "%s (line %v): don't have same number of chunks", test.name, y)
|
||||
|
||||
for i, chunk_exp := range line_exp {
|
||||
chunk_got := line_got[i]
|
||||
if chunk_got != chunk_exp {
|
||||
t.Fatalf("%s (line %v): chunk %v don't coincide, %08b, want %08b", test.name, y, i, chunk_got, chunk_exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user