Skip to content

Commit

Permalink
Moving imageFlip into it's own module.. adding other images
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Robertson committed Jan 14, 2023
1 parent 4d46ff3 commit 3432a5e
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 77 deletions.
58 changes: 0 additions & 58 deletions imageFlip.go

This file was deleted.

1 change: 1 addition & 0 deletions imageFlip/Untitled-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[{"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}] [{"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}] [{"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"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"\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}] [{"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}] [{"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"ÿ" "ÿ" "ÿ" "ÿ"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}, {"\x00" "\x00" "\x00" "\x00"}]]
3 changes: 3 additions & 0 deletions imageFlip/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/metalix2/PiGames/imageFlip

go 1.19
48 changes: 48 additions & 0 deletions imageFlip/imageFlip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package imageFlip

import (
"image"
"image/color/palette"
"image/color"
)

func imageToGrid(img image.Image) [][]color.Color {
// create and return a grid of pixels
var grid [][]color.Color
size := img.Bounds().Size()
for i := 0; i < size.X; i++ {
var y []color.Color
for j := 0; j < size.Y; j++ {
// gray.Set(i, j, img.At(i, j))
r1, g1, b1, _ := img.At(i, j).RGBA()
r2, g2, b2, _ := color.Black.RGBA()
if (r1 == r2 && g1 == g2 && b1 == b2) {
y = append(y, color.Black)
} else {
y = append(y, color.White)
}
}
grid = append(grid, y)
}
return grid
}

func flip(img image.Image) image.Image {
// create and return a grid of pixels
var grid = imageToGrid(img)
xlen, ylen := len(grid), len(grid[0])

// reverse array
for i, j := 0, len(grid)-1; i < j; i, j = i+1, j-1 {
grid[i], grid[j] = grid[j], grid[i]
}

rect := image.Rect(0, 0, xlen+1, ylen+1)
img2 := image.NewPaletted(image.Rect(0, 0, xlen+1, ylen+1), palette.Plan9)
for x := 0; x < xlen; x++ {
for y := 0; y < ylen; y++ {
img2.Set(x+1, y+1, grid[x][y])
}
}
return img2.SubImage(rect)
}
74 changes: 74 additions & 0 deletions imageFlip/imageFlip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package imageFlip

import (
"testing"
"image/gif"
"image/color"
"os"
"reflect"
)

var b = color.Black
var w = color.White
var output = [][]color.Color{
{b, b, b, w, w, w, w, w, b, b, b, b, b},
{b, b, w, w, b, b, b, w, w, b, b, b, b},
{b, w, w, b, w, b, w, b, b, b, b, b, w},
{b, w, w, b, w, w, w, b, w, w, b, w, w},
{b, w, w, b, w, b, w, b, w, w, b, b, b},
{b, w, w, b, w, w, w, b, w, w, b, w, w},
{b, w, w, w, b, b, w, b, b, w, b, b, w},
{b, w, w, w, w, w, b, b, b, b, b, b, b},
{w, b, w, w, w, w, w, w, b, b, b, b, b},
{b, b, w, b, w, w, w, b, w, b, b, b, b},
{b, b, b, b, b, b, w, b, b, b, b, b, b},
}

var flipOuput = [][]color.Color{
{b, b, b, b, b, b, w, b, b, b, b, b, b},
{b, b, w, b, w, w, w, b, w, b, b, b, b},
{w, b, w, w, w, w, w, w, b, b, b, b, b},
{b, w, w, w, w, w, b, b, b, b, b, b, b},
{b, w, w, w, b, b, w, b, b, w, b, b, w},
{b, w, w, b, w, w, w, b, w, w, b, w, w},
{b, w, w, b, w, b, w, b, w, w, b, b, b},
{b, w, w, b, w, w, w, b, w, w, b, w, w},
{b, w, w, b, w, b, w, b, b, b, b, b, w},
{b, b, w, w, b, b, b, w, w, b, b, b, b},
{b, b, b, w, w, w, w, w, b, b, b, b, b},
}

func TestImageToGrid(t *testing.T) {

avatarpath := "../images/avatar.gif"
f, err := os.Open(avatarpath)
avatarGif, err := gif.DecodeAll(f)
f.Close()

// expected output

img := imageToGrid(avatarGif.Image[0])

if !reflect.DeepEqual(img, output) || err != nil {
t.Errorf(`imageToGrid(avatarGif.Image[0]) = %q, %q`, img, output)
}
}

func TestImageFlip(t *testing.T) {


avatarpath := "../images/avatar.gif"
f, err := os.Open(avatarpath)
avatarGif, err := gif.DecodeAll(f)
f.Close()

// expected output

resultImg := flip(avatarGif.Image[0])

result := imageToGrid(resultImg)

if !reflect.DeepEqual(result, flipOuput) || err != nil {
t.Errorf(`imageToGrid(avatarGif.Image[0]) = %q, %q`, result, flipOuput)
}
}
Binary file added images/heart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/mateto.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/star.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3432a5e

Please sign in to comment.