2024-09-08 00:32:52 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/gif"
|
|
|
|
"image/jpeg"
|
2025-01-22 16:58:09 +01:00
|
|
|
"io"
|
2024-09-08 00:32:52 +02:00
|
|
|
|
2025-01-22 16:58:09 +01:00
|
|
|
ico "github.com/biessek/golang-ico"
|
2024-09-08 00:32:52 +02:00
|
|
|
"github.com/gen2brain/avif"
|
|
|
|
"github.com/gen2brain/jpegxl"
|
|
|
|
"github.com/gen2brain/webp"
|
|
|
|
"golang.org/x/image/bmp"
|
|
|
|
"golang.org/x/image/tiff"
|
|
|
|
)
|
|
|
|
|
2025-01-22 16:58:09 +01:00
|
|
|
func ReadImage(input io.ReadSeeker) (image.Image, error) {
|
2024-09-08 00:32:52 +02:00
|
|
|
decoder, err := GetDecoderFromContent(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoder(input)
|
|
|
|
}
|
|
|
|
|
2025-01-22 16:58:09 +01:00
|
|
|
func WriteImage(output io.Writer, img image.Image, format string) error {
|
2024-09-08 00:32:52 +02:00
|
|
|
switch format {
|
|
|
|
case "webp":
|
|
|
|
options := GetWebPOptions()
|
|
|
|
|
|
|
|
LogWebPOptions(options)
|
|
|
|
|
|
|
|
return webp.Encode(output, img, options)
|
|
|
|
case "jpeg":
|
|
|
|
options := GetJpegOptions()
|
|
|
|
|
|
|
|
LogJpegOptions(options)
|
|
|
|
|
|
|
|
return jpeg.Encode(output, img, options)
|
|
|
|
case "png":
|
2025-01-22 16:58:09 +01:00
|
|
|
encoder := GetPNGOptions()
|
|
|
|
|
|
|
|
LogPNGOptions(encoder)
|
|
|
|
|
|
|
|
return encoder.Encode(output, img)
|
2024-09-08 00:32:52 +02:00
|
|
|
case "gif":
|
|
|
|
options := GetGifOptions()
|
|
|
|
|
|
|
|
LogGifOptions(options)
|
|
|
|
|
|
|
|
return gif.Encode(output, img, options)
|
|
|
|
case "bmp":
|
|
|
|
return bmp.Encode(output, img)
|
|
|
|
case "tiff":
|
|
|
|
options := GetTiffOptions()
|
|
|
|
|
|
|
|
LogTiffOptions(options)
|
|
|
|
|
|
|
|
return tiff.Encode(output, img, options)
|
|
|
|
case "avif":
|
|
|
|
options := GetAvifOptions()
|
|
|
|
|
|
|
|
LogAvifOptions(options)
|
|
|
|
|
|
|
|
return avif.Encode(output, img, options)
|
|
|
|
case "jxl":
|
|
|
|
options := GetJxlOptions()
|
|
|
|
|
|
|
|
LogJxlOptions(options)
|
|
|
|
|
|
|
|
jpegxl.Encode(output, img, options)
|
|
|
|
case "ico":
|
|
|
|
return ico.Encode(output, img)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("unsupported output format: %s", format)
|
|
|
|
}
|