From e8ea8ea3312cf2d55777a4730b4bf398e1818156 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 30 Oct 2023 08:43:11 -0700 Subject: bin/web: support JPEG and PNG as input formats --- bin/webp/main.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/bin/webp/main.go b/bin/webp/main.go index c00a6d8..a85bb03 100644 --- a/bin/webp/main.go +++ b/bin/webp/main.go @@ -1,12 +1,12 @@ package main // webp is a small HTTP server that listens on a Unix socket and -// receives an uncompressed TIFF image as a POST value; it returns a -// webp encoded image. The `quality` header is an integer ranging from -// 0-100 indicating the webp quality, it is required. The `lossless` and -// `exact` headers specify that the image is lossless and to preserve -// RGB values in transparent areas, respectively. These headers may -// only be set to "true" and their absence implies that they are false. +// receives an image as a POST value; it returns a webp encoded image. +// The `quality` header is an integer ranging from 0-100 indicating the +// webp quality, it is required. The `lossless` and `exact` headers +// specify that the image is lossless and to preserve RGB values in +// transparent areas, respectively. These headers may only be set to +// "true" and their absence implies that they are false. // // This program exists because there is no pure-Go implementation of // a webp encoder and using CGO is not desirable for most binaries. @@ -21,14 +21,17 @@ package main // shutdown in response to SIGTERM. Errors and requests are logged to // stderr. // -// Uncompressed TIFF was used as the transit format because it supports -// all image color spaces as well as transparency. Compression is -// unneeded because it wastes CPU cycles on the encode and decode side -// for an IPC call, which has no appreciable bandwidth limit. +// Uncompressed TIFF was originally used exclusively as the transit +// format because it supports all image color spaces as well as +// transparency. Compression is unneeded because it wastes CPU cycles on +// the encode and decode side for an IPC call, which has no appreciable +// bandwidth limit. This was changed because it spends a lot of memory +// on the client side. import ( "context" "fmt" + "image" "log" "net" "net/http" @@ -37,8 +40,12 @@ import ( "strconv" "syscall" + _ "image/jpeg" + _ "image/png" + + _ "golang.org/x/image/tiff" + "github.com/chai2010/webp" - "golang.org/x/image/tiff" ) func writeError(w http.ResponseWriter, code int, message string, args ...any) { @@ -50,7 +57,7 @@ func writeError(w http.ResponseWriter, code int, message string, args ...any) { type webpConverterHandler struct{} func (h *webpConverterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - img, err := tiff.Decode(r.Body) + img, _, err := image.Decode(r.Body) if err != nil { writeError(w, http.StatusBadRequest, "decode failed: %s", err) return -- cgit v1.2.3