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) }