mirror of
https://github.com/IllusionMan1212/ColorX.git
synced 2026-10-24 02:12:32 +00:00
feat: implement downsampling
This commit is contained in:
@@ -20,19 +20,42 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"image/color"
|
"image/color"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
|
||||||
|
"golang.org/x/image/draw"
|
||||||
_ "golang.org/x/image/webp"
|
_ "golang.org/x/image/webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MAX_PIXELS = 20000
|
||||||
|
|
||||||
|
// getResizedDims resizes the image to have 20000px or under while keeping the aspect ratio
|
||||||
|
func getResizedDims(width, height int) (int, int) {
|
||||||
|
imagePixels := width * height
|
||||||
|
|
||||||
|
// reference for equation: https://www.wyzant.com/resources/answers/91621/find_two_positive_numbers_whose_ratio_is_2_3_and_whose_product_is_600
|
||||||
|
scale := math.Sqrt(float64(MAX_PIXELS) / float64(imagePixels))
|
||||||
|
|
||||||
|
newWidth := scale * float64(width)
|
||||||
|
newHeight := scale * float64(height)
|
||||||
|
|
||||||
|
return int(newWidth), int(newHeight)
|
||||||
|
}
|
||||||
|
|
||||||
// downsampleImage downsamples images larger than 20000px for faster image processing.
|
// downsampleImage downsamples images larger than 20000px for faster image processing.
|
||||||
func downsampleImage(img image.Image) image.Image {
|
func downsampleImage(img image.Image, width int, height int) image.Image {
|
||||||
// TODO: implement downsampling
|
dstWidth, dstHeight := getResizedDims(width, height)
|
||||||
return img
|
|
||||||
|
dstRect := image.Rect(0, 0, dstWidth, dstHeight)
|
||||||
|
dst := image.NewRGBA(dstRect)
|
||||||
|
|
||||||
|
draw.NearestNeighbor.Scale(dst, dstRect, img, img.Bounds(), draw.Over, nil)
|
||||||
|
|
||||||
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculateColorMap loops through the image pixels and creates a map-
|
// calculateColorMap loops through the image pixels and creates a map-
|
||||||
@@ -69,8 +92,8 @@ func GetProminentColor(data []byte) (string, color.Color, error) {
|
|||||||
pixels := height * width
|
pixels := height * width
|
||||||
|
|
||||||
// image is too big, downsample to a reasonable size
|
// image is too big, downsample to a reasonable size
|
||||||
if pixels > 20000 {
|
if pixels > MAX_PIXELS {
|
||||||
img = downsampleImage(img)
|
img = downsampleImage(img, width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
colorMap := calculateColorMap(img, width, height)
|
colorMap := calculateColorMap(img, width, height)
|
||||||
|
|||||||
Reference in New Issue
Block a user