Добавьте версионированное преобразование изображений
This commit is contained in:
@@ -15,6 +15,7 @@ require (
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
golang.org/x/sync v0.23.0
|
||||
google.golang.org/grpc v1.84.0
|
||||
google.golang.org/protobuf v1.36.12
|
||||
)
|
||||
@@ -109,7 +110,6 @@ require (
|
||||
golang.org/x/arch v0.22.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
golang.org/x/time v0.11.0 // indirect
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// PrepareLargePNG runs in the worker's isolated codec child. ImageMagick's
|
||||
// working scanline for a 40M-by-1 RGBA64 PNG exceeds the entire worker cgroup.
|
||||
// Go can decode that source in the same memory budget as the upload validator.
|
||||
// Only a reduced preview and the original centre square cross into ImageMagick.
|
||||
func PrepareLargePNG(r io.Reader) error {
|
||||
data, err := io.ReadAll(io.LimitReader(r, MaxUploadBytes+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg, err := png.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil || int64(len(data)) > MaxUploadBytes || max(cfg.Width, cfg.Height) <= maxMagickPNGAxis ||
|
||||
cfg.Height <= 0 || cfg.Width <= 0 || int64(cfg.Width) > MaxImagePixels/int64(cfg.Height) {
|
||||
return ErrInvalidImage
|
||||
}
|
||||
src, err := png.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return ErrInvalidImage
|
||||
}
|
||||
// The Go PNG decoder returns standard-library images, all with RGBA64At.
|
||||
pixels := src.(image.RGBA64Image)
|
||||
size := fitSize(cfg, 1280)
|
||||
preview := image.NewRGBA64(image.Rectangle{Max: size})
|
||||
for y := range size.Y {
|
||||
y0 := int(int64(y) * int64(cfg.Height) / int64(size.Y))
|
||||
y1 := int(int64(y+1) * int64(cfg.Height) / int64(size.Y))
|
||||
for x := range size.X {
|
||||
x0 := int(int64(x) * int64(cfg.Width) / int64(size.X))
|
||||
x1 := int(int64(x+1) * int64(cfg.Width) / int64(size.X))
|
||||
preview.SetRGBA64(x, y, meanColor(pixels, image.Rect(x0, y0, x1, y1)))
|
||||
}
|
||||
}
|
||||
// A source wider or taller than 65535 and limited to 40 MP has a centre
|
||||
// square of at most 610 pixels. Copy from the full-resolution original.
|
||||
side := min(cfg.Width, cfg.Height)
|
||||
center := image.NewRGBA64(image.Rect(0, 0, side, side))
|
||||
draw.Draw(center, center.Bounds(), src, image.Pt((cfg.Width-side)/2, (cfg.Height-side)/2), draw.Src)
|
||||
exif := pngEXIF(data)
|
||||
if err := writePNG("preview.png", preview, exif); err != nil {
|
||||
return err
|
||||
}
|
||||
return writePNG("center.png", center, exif)
|
||||
}
|
||||
|
||||
func meanColor(src image.RGBA64Image, box image.Rectangle) color.RGBA64 {
|
||||
var r, g, b, a uint64
|
||||
for y := box.Min.Y; y < box.Max.Y; y++ {
|
||||
for x := box.Min.X; x < box.Max.X; x++ {
|
||||
c := src.RGBA64At(x, y)
|
||||
r += uint64(c.R)
|
||||
g += uint64(c.G)
|
||||
b += uint64(c.B)
|
||||
a += uint64(c.A)
|
||||
}
|
||||
}
|
||||
n := uint64(box.Dx()) * uint64(box.Dy())
|
||||
return color.RGBA64{R: uint16(r / n), G: uint16(g / n), B: uint16(b / n), A: uint16(a / n)}
|
||||
}
|
||||
|
||||
// Preserve the original eXIf chunk through the private intermediate files.
|
||||
// The final encoder applies orientation and strips the metadata.
|
||||
func pngEXIF(data []byte) []byte {
|
||||
for chunks := data[8:]; len(chunks) >= 12; {
|
||||
size := int64(binary.BigEndian.Uint32(chunks[:4])) + 12
|
||||
if size > int64(len(chunks)) {
|
||||
break
|
||||
}
|
||||
if string(chunks[4:8]) == "eXIf" {
|
||||
return chunks[:size]
|
||||
}
|
||||
chunks = chunks[size:]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writePNG(path string, img image.Image, exif []byte) error {
|
||||
var data bytes.Buffer
|
||||
if err := png.Encode(&data, img); err != nil {
|
||||
return err
|
||||
}
|
||||
// Insert eXIf immediately after the 8-byte signature and 25-byte IHDR.
|
||||
content := data.Bytes()
|
||||
return os.WriteFile(path, bytes.Join([][]byte{content[:33], exif, content[33:]}, nil), 0600)
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/semaphore"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxUploadBytes int64 = 10 << 20
|
||||
MaxImagePixels int64 = 40_000_000
|
||||
// This bounds a 16-bit RGBA scanline on the ordinary codec path to 512 KiB.
|
||||
maxMagickPNGAxis = 65_535
|
||||
)
|
||||
|
||||
type Variant struct {
|
||||
Name, MIMEType string
|
||||
Width, Height int
|
||||
Data []byte
|
||||
}
|
||||
|
||||
var ErrInvalidImage = errors.New("invalid image or pipeline version")
|
||||
|
||||
// Reserve both slots for a PNG whose full-width decoder scanlines can each
|
||||
// occupy hundreds of MiB. A worker with a 2 GiB cgroup can run two ordinary jobs.
|
||||
var decoderSlots = semaphore.NewWeighted(2)
|
||||
|
||||
// Render decodes once in a deadline-bound child process. Its pixel cache can
|
||||
// spill to a private disk directory when a high-resolution source exceeds RAM.
|
||||
func Render(ctx context.Context, source []byte, version int) ([]Variant, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if int64(len(source)) > MaxUploadBytes || (version != 1 && version != 2) {
|
||||
return nil, ErrInvalidImage
|
||||
}
|
||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(source))
|
||||
if err != nil || (format != "jpeg" && format != "png") ||
|
||||
cfg.Height < 1 || cfg.Width < 1 || int64(cfg.Width) > MaxImagePixels/int64(cfg.Height) {
|
||||
return nil, ErrInvalidImage
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
|
||||
defer cancel()
|
||||
widePNG := format == "png" && max(cfg.Width, cfg.Height) > maxMagickPNGAxis
|
||||
weight := int64(1)
|
||||
if widePNG {
|
||||
weight = 2
|
||||
}
|
||||
if err := decoderSlots.Acquire(ctx, weight); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer decoderSlots.Release(weight)
|
||||
dir, err := os.MkdirTemp("", "attachment-")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
input := format + ":-[0]"
|
||||
if widePNG {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, executable, "--prepare-large-png")
|
||||
cmd.Env = append(os.Environ(), "GOMAXPROCS=1", "GOMEMLIMIT=1280MiB")
|
||||
cmd.Dir = dir
|
||||
cmd.Stdin = bytes.NewReader(source)
|
||||
if err := cmd.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, fmt.Errorf("reduce large PNG: %w", err)
|
||||
}
|
||||
input = "png:preview.png"
|
||||
}
|
||||
|
||||
output := []Variant{
|
||||
{Name: "thumbnail", MIMEType: "image/jpeg"},
|
||||
{Name: "crop", MIMEType: "image/jpeg"},
|
||||
{Name: "optimized", MIMEType: "image/webp"},
|
||||
}
|
||||
args := []string{
|
||||
"-limit", "thread", "1", "-limit", "memory", "96MiB",
|
||||
"-limit", "map", "0", "-limit", "disk", "2GiB",
|
||||
input, "-colorspace", "sRGB",
|
||||
}
|
||||
for _, v := range output {
|
||||
args = append(args, "(")
|
||||
if widePNG && v.Name == "crop" {
|
||||
args = append(args, "png:center.png")
|
||||
} else {
|
||||
args = append(args, "+clone")
|
||||
}
|
||||
quality := 85
|
||||
switch v.Name {
|
||||
case "thumbnail":
|
||||
args = append(args, "-resize", fit(cfg, 256), "-auto-orient")
|
||||
case "crop":
|
||||
side := min(cfg.Width, cfg.Height)
|
||||
square := fmt.Sprintf("%dx%d+0+0", side, side)
|
||||
args = append(args, "-auto-orient", "-gravity", "center", "-crop", square, "+repage", "-resize", "128x128!")
|
||||
case "optimized":
|
||||
quality = 80
|
||||
if version == 2 {
|
||||
quality = 64
|
||||
}
|
||||
args = append(args, "-resize", fit(cfg, 1280), "-auto-orient")
|
||||
}
|
||||
encoder := "webp"
|
||||
if v.MIMEType == "image/jpeg" {
|
||||
encoder = "jpeg"
|
||||
args = append(args, "-background", "white", "-alpha", "remove", "-alpha", "off")
|
||||
}
|
||||
args = append(args, "-strip", "-quality", strconv.Itoa(quality), "-write", encoder+":"+v.Name,
|
||||
"-format", "%w %h\n", "-write", "info:", "+delete", ")")
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "magick", append(args, "null:")...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(cmd.Environ(), "MAGICK_TEMPORARY_PATH="+dir)
|
||||
if !widePNG {
|
||||
cmd.Stdin = bytes.NewReader(source)
|
||||
}
|
||||
dimensions, err := cmd.Output()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, fmt.Errorf("render image variants: %w", err)
|
||||
}
|
||||
sizes := bytes.NewReader(dimensions)
|
||||
for i := range output {
|
||||
v := &output[i]
|
||||
if _, err := fmt.Fscan(sizes, &v.Width, &v.Height); err != nil {
|
||||
return nil, fmt.Errorf("read %s dimensions: %w", v.Name, err)
|
||||
}
|
||||
v.Data, err = os.ReadFile(filepath.Join(dir, v.Name))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// Compute geometry before auto-orientation. Rotation exchanges the axes after
|
||||
// resizing; bounding by a square gives the same floor-rounded dimensions.
|
||||
func fit(cfg image.Config, limit int) string {
|
||||
size := fitSize(cfg, limit)
|
||||
return fmt.Sprintf("%dx%d!", size.X, size.Y)
|
||||
}
|
||||
|
||||
func fitSize(cfg image.Config, limit int) image.Point {
|
||||
longest := max(cfg.Width, cfg.Height)
|
||||
scale := int64(min(longest, limit))
|
||||
width := max(1, int64(cfg.Width)*scale/int64(longest))
|
||||
height := max(1, int64(cfg.Height)*scale/int64(longest))
|
||||
return image.Pt(int(width), int(height))
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if len(os.Args) == 2 && os.Args[1] == "--prepare-large-png" {
|
||||
if err := PrepareLargePNG(os.Stdin); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestWidePNGPreservesCenterPixelAndAveragesStripes(t *testing.T) {
|
||||
requireCodec(t)
|
||||
img := image.NewNRGBA(image.Rect(0, 0, 70_001, 1))
|
||||
for x := range img.Bounds().Dx() {
|
||||
shade := uint8((x % 2) * 255)
|
||||
img.SetNRGBA(x, 0, color.NRGBA{shade, shade, shade, 255})
|
||||
}
|
||||
img.SetNRGBA(35_000, 0, color.NRGBA{255, 0, 0, 255})
|
||||
var source bytes.Buffer
|
||||
if err := png.Encode(&source, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
results := variants(t, source.Bytes(), 1)
|
||||
thumb := decodedJPEG(t, results["thumbnail"], 256, 1)
|
||||
r, _, _, _ := thumb.At(2, 0).RGBA()
|
||||
if r < 0x7000 || r > 0x9000 {
|
||||
t.Fatalf("stripes were not averaged: %x", r)
|
||||
}
|
||||
crop := decodedJPEG(t, results["crop"], 128, 128)
|
||||
r, g, b, _ := crop.At(64, 64).RGBA()
|
||||
if r < 0xe000 || max(g, b) > 0x2000 {
|
||||
t.Fatalf("center crop used reduced image: %x/%x/%x", r, g, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkerPNGChildStartsWithoutServiceConfig(t *testing.T) {
|
||||
if os.Getenv("CODEC_REQUIRED") != "1" {
|
||||
t.Skip("run make codec-test in the packaged worker runtime")
|
||||
}
|
||||
cmd := exec.CommandContext(t.Context(), "worker", "--prepare-large-png")
|
||||
cmd.Dir = t.TempDir()
|
||||
cmd.Stdin = bytes.NewReader(testPicture(t, 70_000, 1))
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("worker PNG child: %v, %s", err, output)
|
||||
}
|
||||
for name, wantWidth := range map[string]int{"preview.png": 1280, "center.png": 1} {
|
||||
data, err := os.ReadFile(filepath.Join(cmd.Dir, name))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := png.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil || cfg.Width != wantWidth || cfg.Height != 1 {
|
||||
t.Fatalf("%s %dx%d: %v", name, cfg.Width, cfg.Height, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testPicture(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
for y := range height {
|
||||
for x := range width {
|
||||
// Transparent margins, fine detail in the centre to distinguish WebP profiles.
|
||||
if x >= width/4 && x < width-width/4 {
|
||||
img.SetNRGBA(x, y, color.NRGBA{uint8(x * 29), uint8(y * 7), uint8(x ^ y), 255})
|
||||
}
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := png.Encode(&buf, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestRejectInvalidSources(t *testing.T) {
|
||||
huge := testPicture(t, 2, 2)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 20_000_001)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
data []byte
|
||||
version int
|
||||
}{
|
||||
{"unrecognized image", []byte("hello"), 1},
|
||||
{"oversize body", make([]byte, (10<<20)+1), 1},
|
||||
{"oversize pixel count", huge, 1},
|
||||
{"unsupported version", testPicture(t, 2, 2), 3},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
results, err := Render(context.Background(), tc.data, tc.version)
|
||||
if !errors.Is(err, ErrInvalidImage) || len(results) != 0 {
|
||||
t.Fatalf("got %d variants, err %v", len(results), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func requireCodec(t *testing.T) {
|
||||
t.Helper()
|
||||
if _, err := exec.LookPath("magick"); err != nil {
|
||||
if os.Getenv("CODEC_REQUIRED") == "1" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Skip("ImageMagick 7 with JPEG, PNG and WebP coders required")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFortyMegapixelFixtures(t *testing.T) {
|
||||
dir := os.Getenv("CODEC_FIXTURES")
|
||||
if dir == "" {
|
||||
t.Skip("run make codec-test CODEC_STRESS=1 with generated decoder fixtures")
|
||||
}
|
||||
requireCodec(t)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
thumbnail, optimized image.Point
|
||||
}{
|
||||
{"progressive-rgb-444-40mp.jpg", image.Pt(256, 160), image.Pt(1280, 800)},
|
||||
{"progressive-cmyk-444-40mp.jpg", image.Pt(256, 160), image.Pt(1280, 800)},
|
||||
{"wide-rgba64-40mp.png", image.Pt(256, 1), image.Pt(1280, 1)},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
source, err := os.ReadFile(filepath.Join(dir, tc.name))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := variants(t, source, 1)
|
||||
decodedJPEG(t, out["thumbnail"], tc.thumbnail.X, tc.thumbnail.Y)
|
||||
decodedJPEG(t, out["crop"], 128, 128)
|
||||
decodedWebP(t, out["optimized"], tc.optimized.X, tc.optimized.Y)
|
||||
t.Logf("encoded bytes: thumbnail=%d crop=%d optimized=%d", len(out["thumbnail"].Data), len(out["crop"].Data), len(out["optimized"].Data))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func variants(t *testing.T, source []byte, version int) map[string]Variant {
|
||||
t.Helper()
|
||||
out, err := Render(context.Background(), source, version)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(out) != 3 {
|
||||
t.Fatalf("expected 3 variants, got %d", len(out))
|
||||
}
|
||||
result := make(map[string]Variant)
|
||||
for _, v := range out {
|
||||
result[v.Name] = v
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func decodedJPEG(t *testing.T, v Variant, width, height int) image.Image {
|
||||
t.Helper()
|
||||
im, err := jpeg.Decode(bytes.NewReader(v.Data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v.MIMEType != "image/jpeg" || im.Bounds() != image.Rect(0, 0, width, height) || v.Width != width || v.Height != height {
|
||||
t.Fatalf("JPEG metadata %s %dx%d, actual %v, want %dx%d", v.MIMEType, v.Width, v.Height, im.Bounds(), width, height)
|
||||
}
|
||||
return im
|
||||
}
|
||||
|
||||
func decodedWebP(t *testing.T, v Variant, width, height int) {
|
||||
t.Helper()
|
||||
if v.MIMEType != "image/webp" || v.Width != width || v.Height != height {
|
||||
t.Fatalf("invalid optimized metadata %s %dx%d", v.MIMEType, v.Width, v.Height)
|
||||
}
|
||||
probe := exec.Command("magick", "webp:-", "-format", "%wx%h", "info:")
|
||||
probe.Stdin = bytes.NewReader(v.Data)
|
||||
got, err := probe.CombinedOutput()
|
||||
if err != nil || string(got) != fmt.Sprintf("%dx%d", width, height) {
|
||||
t.Fatalf("decode optimized: %s, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderProfiles(t *testing.T) {
|
||||
requireCodec(t)
|
||||
source := testPicture(t, 1301, 707)
|
||||
v1 := variants(t, source, 1)
|
||||
thumb := decodedJPEG(t, v1["thumbnail"], 256, 139)
|
||||
r, g, b, _ := thumb.At(7, 5).RGBA()
|
||||
if min(r, g, b) < 0xf000 {
|
||||
t.Fatalf("transparent thumbnail margin became %x/%x/%x", r, g, b)
|
||||
}
|
||||
crop := decodedJPEG(t, v1["crop"], 128, 128)
|
||||
r, g, b, _ = crop.At(13, 64).RGBA()
|
||||
if min(r, g, b) > 0xe000 {
|
||||
t.Error("crop retained the outer transparent margin")
|
||||
}
|
||||
webp := v1["optimized"]
|
||||
decodedWebP(t, webp, 1280, 695)
|
||||
if bytes.Equal(webp.Data, variants(t, source, 2)["optimized"].Data) {
|
||||
t.Error("two WebP profiles gave identical bytes on a detailed image")
|
||||
}
|
||||
if !bytes.Equal(webp.Data, variants(t, source, 1)["optimized"].Data) {
|
||||
t.Error("same version produced different bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTinyRotatedJPEG(t *testing.T) {
|
||||
requireCodec(t)
|
||||
img := image.NewRGBA(image.Rect(0, 0, 32, 16))
|
||||
for y := range 16 {
|
||||
for x := range 32 {
|
||||
if x < 16 {
|
||||
img.Set(x, y, color.RGBA{255, 0, 0, 255})
|
||||
} else {
|
||||
img.Set(x, y, color.RGBA{0, 0, 255, 255})
|
||||
}
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := jpeg.Encode(&buf, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// APP1 with a little-endian TIFF orientation of 6 (rotate 90 degrees clockwise).
|
||||
exif := []byte("\xff\xe1\x00\x22Exif\x00\x00II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00")
|
||||
source := bytes.Join([][]byte{buf.Bytes()[:2], exif, buf.Bytes()[2:]}, nil)
|
||||
v := variants(t, source, 1)
|
||||
thumb := decodedJPEG(t, v["thumbnail"], 16, 32)
|
||||
r, _, b, _ := thumb.At(8, 5).RGBA()
|
||||
if r < 0xc000 || b > 0x3000 {
|
||||
t.Fatal("thumbnail was not rotated clockwise")
|
||||
}
|
||||
decodedJPEG(t, v["crop"], 128, 128)
|
||||
if v["optimized"].Width != 16 || v["optimized"].Height != 32 {
|
||||
t.Fatal("optimized tiny image was enlarged or not rotated")
|
||||
}
|
||||
for name, variant := range v {
|
||||
if bytes.Contains(bytes.ToUpper(variant.Data), []byte("EXIF")) {
|
||||
t.Errorf("%s retained EXIF", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderHonorsCancelledContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := Render(ctx, testPicture(t, 2, 2), 1)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnePixelMinorAxis(t *testing.T) {
|
||||
requireCodec(t)
|
||||
v := variants(t, testPicture(t, 1, 4096), 1)
|
||||
decodedJPEG(t, v["thumbnail"], 1, 256)
|
||||
}
|
||||
Reference in New Issue
Block a user