mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* Add benchmarks for image transformation functions Covers GeneratePreview, GenerateThumbnail, GenerateMiniPreviewImage, FillCenter, and MakeImageUpright — all functions that delegate to the external imaging library. Baseline before/after the library swap. * Replace anthonynsimon/bild with boxes-ltd/imaging Reverts the imaging library swap from PR #29657 (disintegration/imaging → anthonynsimon/bild) and replaces it with github.com/boxes-ltd/imaging, the maintained fork of disintegration/imaging with the same API. - emoji.go: restore direct use of imaging.Fit with Lanczos filter - orientation.go: restore FlipH/FlipV/Rotate90/180/270/Transpose/Transverse - preview.go: restore imaging.Resize calls with Lanczos filter - utils.go: remove bild-based Resize/Fit/CropCenter helpers; restore the original FillCenter using imaging.Fill - Remove tests and test images added for the bild-specific helpers - Regenerate orientation test expected images (2,3,6,7,8) and GIF thumbnail to match boxes-ltd/imaging output * gofmt imaging package files * Add tests and fixtures for the boxes-ltd/imaging-backed functions Cover MakeImageUpright across all 8 EXIF orientations, GeneratePreview, GenerateMiniPreviewImage, and FillCenter against fixtures regenerated under the new library. * Contain boxes-ltd/imaging behind the local imaging wrapper Add a Fit wrapper alongside FillCenter and route emoji.go through the local package so boxes-ltd/imaging is only imported from channels/app/imaging. * Add TestFit to cover the local Fit wrapper Dimensional checks for the Fit wrapper used by emoji.go, mirroring TestGenerateThumbnail. Pixel correctness is covered by the upstream boxes-ltd/imaging tests; this guards against wrapper-level mistakes (transposed args, wrong filter). * Address CodeRabbit nits in TestFillCenter and TestFit Decode a fresh source image per TestFillCenter subtest so cases stay isolated even if the wrapper ever mutates input. Rename the TestFit "smaller than bounds (clone)" case to "no resize when smaller than bounds" since the assertion only checks dimensions, not clone semantics. * Revert per-subtest decode in TestFillCenter imaging.Fill never mutates its input — it always returns a fresh *image.NRGBA — so re-decoding the source for every subtest was unnecessary work. Decode once, share across subtests. * Compare decoded pixels instead of raw PNG/JPEG bytes in tests image/png isn't byte-stable across Go versions, so comparing re-encoded byte streams against checked-in fixtures is brittle to toolchain bumps and re-encoding. Decode both sides and compare RGBA pixels via a shared requireSameImage helper. Fixtures stay on disk unchanged. Covers TestFillCenter, TestGeneratePreview, TestGenerateMiniPreviewImage, and TestMakeImageUpright. TestFillImageTransparency is left alone since its byte-comparison pattern predates this branch. * Report total diff rate when requireSameImage fails Walk every pixel before failing instead of stopping at the first mismatch. The failure message now includes how many pixels differ, the percentage of the image that's off, and the first divergence. --------- Co-authored-by: Mattermost Build <[email protected]> Co-authored-by: Hanzo Dev <[email protected]>
154 lines
3.7 KiB
Go
154 lines
3.7 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package imaging
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"github.com/boxes-ltd/imaging"
|
|
)
|
|
|
|
type rawImg interface {
|
|
Set(x, y int, c color.Color)
|
|
Opaque() bool
|
|
}
|
|
|
|
func isFullyTransparent(c color.Color) bool {
|
|
// TODO: This can be optimized by checking the color type and
|
|
// only extract the needed alpha value.
|
|
_, _, _, a := c.RGBA()
|
|
return a == 0
|
|
}
|
|
|
|
// FillImageTransparency fills in-place all the fully transparent pixels of the
|
|
// input image with the given color.
|
|
func FillImageTransparency(img image.Image, c color.Color) {
|
|
var i rawImg
|
|
|
|
bounds := img.Bounds()
|
|
|
|
fillFunc := func() {
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
if isFullyTransparent(img.At(x, y)) {
|
|
i.Set(x, y, c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
switch raw := img.(type) {
|
|
case *image.Alpha:
|
|
i = raw
|
|
case *image.Alpha16:
|
|
i = raw
|
|
case *image.Gray:
|
|
i = raw
|
|
case *image.Gray16:
|
|
i = raw
|
|
case *image.NRGBA:
|
|
i = raw
|
|
col := color.NRGBAModel.Convert(c).(color.NRGBA)
|
|
fillFunc = func() {
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
i := raw.PixOffset(x, y)
|
|
if raw.Pix[i+3] == 0x00 {
|
|
raw.Pix[i] = col.R
|
|
raw.Pix[i+1] = col.G
|
|
raw.Pix[i+2] = col.B
|
|
raw.Pix[i+3] = col.A
|
|
}
|
|
}
|
|
}
|
|
}
|
|
case *image.NRGBA64:
|
|
i = raw
|
|
col := color.NRGBA64Model.Convert(c).(color.NRGBA64)
|
|
fillFunc = func() {
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
i := raw.PixOffset(x, y)
|
|
a := uint16(raw.Pix[i+6])<<8 | uint16(raw.Pix[i+7])
|
|
if a == 0 {
|
|
raw.Pix[i] = uint8(col.R >> 8)
|
|
raw.Pix[i+1] = uint8(col.R)
|
|
raw.Pix[i+2] = uint8(col.G >> 8)
|
|
raw.Pix[i+3] = uint8(col.G)
|
|
raw.Pix[i+4] = uint8(col.B >> 8)
|
|
raw.Pix[i+5] = uint8(col.B)
|
|
raw.Pix[i+6] = uint8(col.A >> 8)
|
|
raw.Pix[i+7] = uint8(col.A)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
case *image.Paletted:
|
|
i = raw
|
|
fillFunc = func() {
|
|
for i := range raw.Palette {
|
|
if isFullyTransparent(raw.Palette[i]) {
|
|
raw.Palette[i] = c
|
|
}
|
|
}
|
|
}
|
|
case *image.RGBA:
|
|
i = raw
|
|
col := color.RGBAModel.Convert(c).(color.RGBA)
|
|
fillFunc = func() {
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
i := raw.PixOffset(x, y)
|
|
if raw.Pix[i+3] == 0x00 {
|
|
raw.Pix[i] = col.R
|
|
raw.Pix[i+1] = col.G
|
|
raw.Pix[i+2] = col.B
|
|
raw.Pix[i+3] = col.A
|
|
}
|
|
}
|
|
}
|
|
}
|
|
case *image.RGBA64:
|
|
i = raw
|
|
col := color.RGBA64Model.Convert(c).(color.RGBA64)
|
|
fillFunc = func() {
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
i := raw.PixOffset(x, y)
|
|
a := uint16(raw.Pix[i+6])<<8 | uint16(raw.Pix[i+7])
|
|
if a == 0 {
|
|
raw.Pix[i] = uint8(col.R >> 8)
|
|
raw.Pix[i+1] = uint8(col.R)
|
|
raw.Pix[i+2] = uint8(col.G >> 8)
|
|
raw.Pix[i+3] = uint8(col.G)
|
|
raw.Pix[i+4] = uint8(col.B >> 8)
|
|
raw.Pix[i+5] = uint8(col.B)
|
|
raw.Pix[i+6] = uint8(col.A >> 8)
|
|
raw.Pix[i+7] = uint8(col.A)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
return
|
|
}
|
|
|
|
if !i.Opaque() {
|
|
fillFunc()
|
|
}
|
|
}
|
|
|
|
// FillCenter creates an image with the specified dimensions and fills it with
|
|
// the centered and scaled source image.
|
|
func FillCenter(img image.Image, w, h int) *image.NRGBA {
|
|
return imaging.Fill(img, w, h, imaging.Center, imaging.Lanczos)
|
|
}
|
|
|
|
// Fit scales down the image to fit within the specified maximum dimensions,
|
|
// preserving the aspect ratio.
|
|
func Fit(img image.Image, maxW, maxH int) *image.NRGBA {
|
|
return imaging.Fit(img, maxW, maxH, imaging.Lanczos)
|
|
}
|