diff --git a/README.md b/README.md index 3020cb5..28c9dba 100644 --- a/README.md +++ b/README.md @@ -549,6 +549,10 @@ example. PNG or JPEG XL source. This holds in every output format, `orig` included. So an animated GIF requested as GIF comes back as a still GIF. +**Transparency.** JPEG has no transparency, so pixa fills the transparent areas +of an image with white in JPEG output. This applies to `auto` too, when it +chooses JPEG. + **SVG sources.** `orig` of an SVG source is the SVG itself, unchanged, and is cached like any other image. It is given only at the `orig` size, since pixa does not resize an SVG as SVG. `orig` of an SVG at any other size is refused diff --git a/internal/imageprocessor/imageprocessor.go b/internal/imageprocessor/imageprocessor.go index 8294d29..96cb2c9 100644 --- a/internal/imageprocessor/imageprocessor.go +++ b/internal/imageprocessor/imageprocessor.go @@ -808,9 +808,20 @@ func (p *ImageProcessor) encode( // every setting whose zero value is not what pixa wants. Stripping metadata // drops EXIF, XMP, IPTC and the ICC profile. -// exportJPEG encodes img as JPEG at quality, without metadata. The settings -// it leaves out are at libvips' defaults. +// exportJPEG encodes img as JPEG at quality, without metadata. JPEG has no +// transparency, so any transparent areas are filled with white; left to +// itself, libvips would fill them with black. govips scales the 8-bit white +// for a 16-bit image. The settings it leaves out are at libvips' defaults. func exportJPEG(img *vips.ImageRef, quality int) ([]byte, error) { + if img.HasAlpha() { + err := img.Flatten(&vips.Color{ + R: math.MaxUint8, G: math.MaxUint8, B: math.MaxUint8, + }) + if err != nil { + return nil, fmt.Errorf("failed to fill transparent areas: %w", err) + } + } + output, _, err := img.ExportJpeg(&vips.JpegExportParams{ StripMetadata: true, Quality: quality,