From c0d2e0569097c383b117a4377df422b6359fffb8 Mon Sep 17 00:00:00 2001 From: potassium Date: Mon, 11 Dec 2023 22:48:03 +0300 Subject: [PATCH] initial --- go.mod | 3 +++ resize.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 go.mod create mode 100644 resize.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cff703d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module imageutils + +go 1.21.4 diff --git a/resize.go b/resize.go new file mode 100644 index 0000000..f51e1f0 --- /dev/null +++ b/resize.go @@ -0,0 +1,46 @@ +package imageutils + +import ( + "image" + "image/color" +) + +type resized struct { + src image.Image + scale int +} + +// ColorModel implements image.Image interface +func (r resized) ColorModel() color.Model { + return r.src.ColorModel() +} + +// Bounds implements image.Image interface +func (r resized) Bounds() image.Rectangle { + b := r.src.Bounds() + return image.Rectangle{ + Min: image.Point{ + X: b.Min.X, + Y: b.Min.Y, + }, + Max: image.Point{ + X: b.Max.X * r.scale, + Y: b.Max.Y * r.scale, + }, + } +} + +// At implements image.Image interface +func (r resized) At(x, y int) color.Color { + return r.src.At(x/r.scale, y/r.scale) +} + +// Resize resizes image to a given scale +// and then returns image. +// For now it will work only with positive integers. +func Resize(img image.Image, scale int) image.Image { + if scale < 1 { + scale = 1 + } + return resized{img, scale} +}