aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2023-10-30 08:43:11 -0700
committerMike Crute <mike@crute.us>2023-10-30 08:43:11 -0700
commite8ea8ea3312cf2d55777a4730b4bf398e1818156 (patch)
tree7992b031a544e15e28a2e53def38ee155212f011
parente1a7fd00e8c5e3ddd82f696bc359066912d3417e (diff)
downloadgolib-e8ea8ea3312cf2d55777a4730b4bf398e1818156.tar.bz2
golib-e8ea8ea3312cf2d55777a4730b4bf398e1818156.tar.xz
golib-e8ea8ea3312cf2d55777a4730b4bf398e1818156.zip
bin/web: support JPEG and PNG as input formatsbin/webp/v0.2.0
-rw-r--r--bin/webp/main.go31
1 files 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 @@
1package main 1package main
2 2
3// webp is a small HTTP server that listens on a Unix socket and 3// webp is a small HTTP server that listens on a Unix socket and
4// receives an uncompressed TIFF image as a POST value; it returns a 4// receives an image as a POST value; it returns a webp encoded image.
5// webp encoded image. The `quality` header is an integer ranging from 5// The `quality` header is an integer ranging from 0-100 indicating the
6// 0-100 indicating the webp quality, it is required. The `lossless` and 6// webp quality, it is required. The `lossless` and `exact` headers
7// `exact` headers specify that the image is lossless and to preserve 7// specify that the image is lossless and to preserve RGB values in
8// RGB values in transparent areas, respectively. These headers may 8// transparent areas, respectively. These headers may only be set to
9// only be set to "true" and their absence implies that they are false. 9// "true" and their absence implies that they are false.
10// 10//
11// This program exists because there is no pure-Go implementation of 11// This program exists because there is no pure-Go implementation of
12// a webp encoder and using CGO is not desirable for most binaries. 12// a webp encoder and using CGO is not desirable for most binaries.
@@ -21,14 +21,17 @@ package main
21// shutdown in response to SIGTERM. Errors and requests are logged to 21// shutdown in response to SIGTERM. Errors and requests are logged to
22// stderr. 22// stderr.
23// 23//
24// Uncompressed TIFF was used as the transit format because it supports 24// Uncompressed TIFF was originally used exclusively as the transit
25// all image color spaces as well as transparency. Compression is 25// format because it supports all image color spaces as well as
26// unneeded because it wastes CPU cycles on the encode and decode side 26// transparency. Compression is unneeded because it wastes CPU cycles on
27// for an IPC call, which has no appreciable bandwidth limit. 27// the encode and decode side for an IPC call, which has no appreciable
28// bandwidth limit. This was changed because it spends a lot of memory
29// on the client side.
28 30
29import ( 31import (
30 "context" 32 "context"
31 "fmt" 33 "fmt"
34 "image"
32 "log" 35 "log"
33 "net" 36 "net"
34 "net/http" 37 "net/http"
@@ -37,8 +40,12 @@ import (
37 "strconv" 40 "strconv"
38 "syscall" 41 "syscall"
39 42
43 _ "image/jpeg"
44 _ "image/png"
45
46 _ "golang.org/x/image/tiff"
47
40 "github.com/chai2010/webp" 48 "github.com/chai2010/webp"
41 "golang.org/x/image/tiff"
42) 49)
43 50
44func writeError(w http.ResponseWriter, code int, message string, args ...any) { 51func 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) {
50type webpConverterHandler struct{} 57type webpConverterHandler struct{}
51 58
52func (h *webpConverterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 59func (h *webpConverterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
53 img, err := tiff.Decode(r.Body) 60 img, _, err := image.Decode(r.Body)
54 if err != nil { 61 if err != nil {
55 writeError(w, http.StatusBadRequest, "decode failed: %s", err) 62 writeError(w, http.StatusBadRequest, "decode failed: %s", err)
56 return 63 return