From 612e4a29beeeb73ca6a63adfc6be66f62554764b Mon Sep 17 00:00:00 2001 From: lorenzhohermuth Date: Mon, 19 Feb 2024 19:39:35 +0100 Subject: [PATCH] added squash --- pkg/image/convert.go | 12 --------- pkg/image/image.go | 9 ++----- pkg/image/pixel.go | 32 +++++++++++++++++++++++ pkg/image/split.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 19 deletions(-) delete mode 100644 pkg/image/convert.go create mode 100644 pkg/image/pixel.go create mode 100644 pkg/image/split.go diff --git a/pkg/image/convert.go b/pkg/image/convert.go deleted file mode 100644 index b5b1f02..0000000 --- a/pkg/image/convert.go +++ /dev/null @@ -1,12 +0,0 @@ -package image - -//func getBighnessMap(map [][]Pixel) [][]float64{} - -func (pix Pixel) Brightness() float64 { - R := float64(pix.R) - G := float64(pix.G) - B := float64(pix.B) - A := float64(pix.A) - brightness := (0.2126*R + 0.7152*G + 0.0722*B) /255 - return brightness * (A / 255) -} diff --git a/pkg/image/image.go b/pkg/image/image.go index c567e1d..b1fef70 100644 --- a/pkg/image/image.go +++ b/pkg/image/image.go @@ -7,18 +7,13 @@ import ( "os" ) -type Pixel struct { - R int - G int - B int - A int -} func ImageToPixles(path string) [][]Pixel { image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig) file, err := os.Open(path) check(err) defer file.Close() - return getPixles(file) + mat := getPixles(file) + return squashMatrix(mat) } func getPixles(f io.Reader) [][]Pixel { diff --git a/pkg/image/pixel.go b/pkg/image/pixel.go new file mode 100644 index 0000000..308b12e --- /dev/null +++ b/pkg/image/pixel.go @@ -0,0 +1,32 @@ +package image + +//func getBighnessMap(map [][]Pixel) [][]float64{} + +func (pix Pixel) Brightness() float64 { + R := float64(pix.R) + G := float64(pix.G) + B := float64(pix.B) + A := float64(pix.A) + brightness := (0.2126*R + 0.7152*G + 0.0722*B) /255 + return brightness * (A / 255) +} + +type Pixel struct { + R int + G int + B int + A int +} + +func averagePixel(arr []Pixel) Pixel { + l := len(arr) + var sumR, sumG, sumB, sumA int + for _, v := range arr { + sumR += v.R + sumG += v.G + sumB += v.B + sumA += v.A + } + + return Pixel{sumR / l, sumG / l, sumB / l, sumA / l} +} diff --git a/pkg/image/split.go b/pkg/image/split.go new file mode 100644 index 0000000..c1148c3 --- /dev/null +++ b/pkg/image/split.go @@ -0,0 +1,62 @@ +package image + +var heigthCell int = 2 +var widthCell int = 1 + +func reFormatVertical(matrix [][]Pixel ) [][]Pixel { + heightMatrix := len(matrix) + widthMatrix := len(matrix[0]) + newMatrix := makePixleMatrix(widthMatrix, heightMatrix) + for y := range matrix { + for x := range matrix[y] { + newMatrix[x][y] = matrix[y][x] + } + } + return newMatrix +} + +func splitArray(arr []Pixel, size int) [][]Pixel { + var out [][]Pixel + var j int + for i := 0; i < len(arr); i += size { + j += size + if j > len(arr) { + j = len(arr) + } + out = append(out, arr[i:j]) + } + return out +} + +func squashMatrix(pix [][]Pixel) [][]Pixel{ + pix = reFormatVertical(pix) + var tensor [][][]Pixel + for _, arr := range pix { + tensor = append(tensor, splitArray(arr, heigthCell)) + } + + out := makePixleMatrix(len(tensor[0]), len(tensor)) + + for x := range tensor { + for y := range tensor[x] { + p := averagePixel(tensor[x][y]) + out[y][x] = p + } + } + + return out +} + + +type cord struct { + x int + y int +} + +func makePixleMatrix(height int, width int) [][]Pixel { + arr := make([][]Pixel, height) + for i := range arr { + arr[i] = make([]Pixel, width) + } + return arr +}