Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
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)
|
|
}
|