From 6f96e8ed31e4f82169e4a7e99fc109bf6b9196bd Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 9 Feb 2023 20:54:04 +0300 Subject: [PATCH 1/2] Fix image flipping, work on image rotation fix --- filters/flip.go | 11 ++++++++--- filters/rotate-fixed.go | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/filters/flip.go b/filters/flip.go index a36ce0f..94aae63 100644 --- a/filters/flip.go +++ b/filters/flip.go @@ -27,15 +27,20 @@ func Flip(file io.Reader, direction string) (io.Reader, string, error) { for i := 0; i < len(img.Pix); i += 4 { x, y := utilities.GetCoordinates(i/4, width) var j int + skip := true if direction == constants.FLIP_DIRECTION_HORIZONTAL && x < width/2+widthCorrection { j = utilities.GetPixel(width-x-1, y, width) + skip = false } if direction == constants.FLIP_DIRECTION_VERTICAL && y < height/2+heightCorrection { j = utilities.GetPixel(x, height-y-1, width) + skip = false + } + if !skip { + r, g, b := img.Pix[i], img.Pix[i+1], img.Pix[i+2] + img.Pix[i], img.Pix[i+1], img.Pix[i+2] = img.Pix[j], img.Pix[j+1], img.Pix[j+2] + img.Pix[j], img.Pix[j+1], img.Pix[j+2] = r, g, b } - r, g, b := img.Pix[i], img.Pix[i+1], img.Pix[i+2] - img.Pix[i], img.Pix[i+1], img.Pix[i+2] = img.Pix[j], img.Pix[j+1], img.Pix[j+2] - img.Pix[j], img.Pix[j+1], img.Pix[j+2] = r, g, b } return utilities.EncodeResult(img, format) } diff --git a/filters/rotate-fixed.go b/filters/rotate-fixed.go index a7fec18..a4b612a 100644 --- a/filters/rotate-fixed.go +++ b/filters/rotate-fixed.go @@ -30,9 +30,9 @@ func RotateFixed(file io.Reader, angle uint) (io.Reader, string, error) { var j int if y < height/2+heightCorrection { j = utilities.GetPixel(width-x-1, height-y-1, width) + img.Pix[i], img.Pix[i+1], img.Pix[i+2] = img.Pix[j], img.Pix[j+1], img.Pix[j+2] + img.Pix[j], img.Pix[j+1], img.Pix[j+2] = r, g, b } - img.Pix[i], img.Pix[i+1], img.Pix[i+2] = img.Pix[j], img.Pix[j+1], img.Pix[j+2] - img.Pix[j], img.Pix[j+1], img.Pix[j+2] = r, g, b } return utilities.EncodeResult(img, format) } From 614574831e37f6c9cac13d39513553ff512401ac Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 10 Feb 2023 20:12:01 +0300 Subject: [PATCH 2/2] Fix image rotation, fix Kuwahara filter --- filters/kuwahara.go | 1 + filters/rotate-fixed.go | 45 +++++++++++++++-------------------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/filters/kuwahara.go b/filters/kuwahara.go index a9ee99e..93a96dc 100644 --- a/filters/kuwahara.go +++ b/filters/kuwahara.go @@ -74,6 +74,7 @@ func Kuwahara(file io.Reader, radius uint) (io.Reader, string, error) { destination[i] = uint8(rValues[j] / pixelsCount[j]) destination[i+1] = uint8(gValues[j] / pixelsCount[j]) destination[i+2] = uint8(bValues[j] / pixelsCount[j]) + destination[i+3] = img.Pix[i+3] } img.Pix = destination return utilities.EncodeResult(img, format) diff --git a/filters/rotate-fixed.go b/filters/rotate-fixed.go index a4b612a..3e33c37 100644 --- a/filters/rotate-fixed.go +++ b/filters/rotate-fixed.go @@ -19,44 +19,31 @@ func RotateFixed(file io.Reader, angle uint) (io.Reader, string, error) { angle = constants.ROTATE_FIXED_90 } width, height := img.Rect.Max.X, img.Rect.Max.Y - heightCorrection := 0 - if height%2 != 0 { - heightCorrection = 1 - } + gridWidth, gridHeight := width, height if angle == constants.ROTATE_FIXED_180 { - for i := 0; i < len(img.Pix); i += 4 { - x, y := utilities.GetCoordinates(i/4, width) - r, g, b := img.Pix[i], img.Pix[i+1], img.Pix[i+2] - var j int - if y < height/2+heightCorrection { - j = utilities.GetPixel(width-x-1, height-y-1, width) - img.Pix[i], img.Pix[i+1], img.Pix[i+2] = img.Pix[j], img.Pix[j+1], img.Pix[j+2] - img.Pix[j], img.Pix[j+1], img.Pix[j+2] = r, g, b - } - } - return utilities.EncodeResult(img, format) + gridWidth, gridHeight = height, width } - destination := make([][]color.Color, width) + destination := make([][]color.Color, gridWidth) for i := range destination { - destination[i] = make([]color.Color, height) + destination[i] = make([]color.Color, gridHeight) } for i := 0; i < len(img.Pix); i += 4 { x, y := utilities.GetCoordinates(i/4, width) + dx, dy := y, x if angle == constants.ROTATE_FIXED_90 { - destination[height-y-1][x] = color.RGBA{ - img.Pix[i], - img.Pix[i+1], - img.Pix[i+2], - img.Pix[i+3], - } + dx = height - y - 1 + } + if angle == constants.ROTATE_FIXED_180 { + dx, dy = width-x-1, height-y-1 } if angle == constants.ROTATE_FIXED_270 { - destination[y][width-x-1] = color.RGBA{ - img.Pix[i], - img.Pix[i+1], - img.Pix[i+2], - img.Pix[i+3], - } + dy = width - x - 1 + } + destination[dx][dy] = color.RGBA{ + img.Pix[i], + img.Pix[i+1], + img.Pix[i+2], + img.Pix[i+3], } } return utilities.EncodeGridResult(destination, format)