From 9dab306df66627050c9b9c6ebfa4006d8312e5b6 Mon Sep 17 00:00:00 2001 From: Akmal Date: Wed, 3 Jul 2024 11:19:49 +0700 Subject: [PATCH] YCbCr everything --- handlers/grid.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/handlers/grid.go b/handlers/grid.go index d3d535c..a5098bb 100644 --- a/handlers/grid.go +++ b/handlers/grid.go @@ -2,7 +2,6 @@ package handlers import ( "image" - "image/draw" "image/jpeg" "math" "net" @@ -70,9 +69,9 @@ func avg(n []float64) float64 { // GenerateGrid generates a grid of images // based on https://blog.vjeux.com/2014/image/google-plus-layout-find-best-breaks.html -func GenerateGrid(images []*image.RGBA) (image.Image, error) { +func GenerateGrid(images []image.Image) (image.Image, error) { var imagesWH [][]float64 - images = append(images, image.NewRGBA(image.Rect(0, 0, 0, 0))) // Needed as for some reason the last image is not added + images = append(images, image.Rect(0, 0, 0, 0)) // Needed as for some reason the last image is not added for _, image := range images { imagesWH = append(imagesWH, []float64{float64(image.Bounds().Dx()), float64(image.Bounds().Dy())}) } @@ -111,7 +110,7 @@ func GenerateGrid(images []*image.RGBA) (image.Image, error) { canvasHeight += int(rowHeight) } - canvas := image.NewRGBA(image.Rect(0, 0, canvasWidth, canvasHeight)) + canvas := image.NewYCbCr(image.Rect(0, 0, canvasWidth, canvasHeight), image.YCbCrSubsampleRatio420) oldRowHeight := 0 for i := 1; i < len(path); i++ { @@ -158,7 +157,7 @@ func Grid() fiber.Handler { mediaList = append(mediaList, item.Medias[i]) } - images := make([]*image.RGBA, len(mediaList)) + images := make([]image.Image, len(mediaList)) var wg sync.WaitGroup client := http.Client{Transport: transport, Timeout: timeout} @@ -179,14 +178,10 @@ func Grid() fiber.Handler { } defer res.Body.Close() - imJPEG, err := jpeg.Decode(res.Body) + images[i], err = jpeg.Decode(res.Body) if err != nil { return } - - b := imJPEG.Bounds() - images[i] = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(images[i], images[i].Bounds(), imJPEG, b.Min, draw.Src) }(i, media) } wg.Wait()