Write metadata into file

This commit is contained in:
heyarne 2022-03-20 23:33:45 +01:00
commit a98f3a0b63
3 changed files with 21 additions and 16 deletions

View file

@ -54,7 +54,7 @@ Pass "-" as the filename to read from stdin.`,
output = file output = file
} }
result, err := epd.ConvertImage(input, width, height) result, err := epd.ConvertImage(&input, width, height)
if err != nil { if err != nil {
log.Fatalf("Could not convert image: %v", err) log.Fatalf("Could not convert image: %v", err)
} }

View file

@ -6,10 +6,11 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/saunaclub/inkpot-cli/epd" "github.com/saunaclub/inkpot-cli/epd"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -47,8 +48,8 @@ A webserver to convert GIFs, PNGs and JPEGs to 4-bit grayscale images.
## Routes ## Routes
- POST /convert can be used to convert a file on your filesystem - PUT /convert can be used to convert a file on your filesystem
- GET /convert?url=[url] can be used to convert a file publically accessible via URL - GET /convert takes a "url" parameter that can be used to convert a file publically accessible via http
Both routes accept a "width" and a "height" parameter to configure the output size. Both routes accept a "width" and a "height" parameter to configure the output size.
If you want the response to be gzip encoded, just set the Accept-Encoding header. If you want the response to be gzip encoded, just set the Accept-Encoding header.
@ -107,7 +108,8 @@ func getConvert(c *gin.Context) {
reader := response.Body reader := response.Body
defer reader.Close() defer reader.Close()
converted, err := epd.ConvertImage(reader, width, height) r := io.Reader(reader)
converted, err := epd.ConvertImage(&r, width, height)
if err != nil { if err != nil {
c.Error(err) c.Error(err)
} }
@ -138,14 +140,13 @@ func postConvert(c *gin.Context) {
if err != nil { if err != nil {
c.Error(err) c.Error(err)
} }
reader, err := file.Open() reader, err := file.Open()
if err != nil { if err != nil {
c.Error(err) c.Error(err)
} }
defer reader.Close()
converted, err := epd.ConvertImage(reader, width, height) r := io.Reader(reader)
converted, err := epd.ConvertImage(&r, width, height)
if err != nil { if err != nil {
c.Error(err) c.Error(err)
} }

View file

@ -1,6 +1,7 @@
package epd package epd
import ( import (
"fmt"
"image" "image"
"image/color" "image/color"
_ "image/gif" _ "image/gif"
@ -40,8 +41,8 @@ func fitRectInto(src *image.Rectangle, dst *image.Rectangle) image.Rectangle {
// Converts a GIF, JPEG or PNG into a 16-color grayscale image. A single pixel // Converts a GIF, JPEG or PNG into a 16-color grayscale image. A single pixel
// is represented by 4 bits and each byte holds two pixels. The final 4-bits of // is represented by 4 bits and each byte holds two pixels. The final 4-bits of
// the last byte may be discarded if width * height is odd. // the last byte may be discarded if width * height is odd.
func ConvertImage(input io.Reader, width int, height int) ([]byte, error) { func ConvertImage(input *io.Reader, width int, height int) ([]byte, error) {
src, _, err := image.Decode(input) src, _, err := image.Decode(*input)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -53,18 +54,21 @@ func ConvertImage(input io.Reader, width int, height int) ([]byte, error) {
draw.Draw(dst, dst.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src) draw.Draw(dst, dst.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
draw.CatmullRom.Scale(dst, targetRect, src, src.Bounds(), draw.Over, nil) draw.CatmullRom.Scale(dst, targetRect, src, src.Bounds(), draw.Over, nil)
// we prepend information that is required to decode the image correctly in a
// header of ascii key-value pairs, separated by a colon and a space, one per
// line. the header ends with two consecutive newlines.
headerString := fmt.Sprintf("width: %d\nheight: %d\n\n", width, height) // remember to strconv.QuoteToASCII(headerString) if necessary
header := []byte(headerString)
// the actual conversion works by packing two nibbles together in a byte // the actual conversion works by packing two nibbles together in a byte
var result = make([]byte, (width*height+1)/2) var result = make([]byte, (width*height+1)/2)
for i, p := range dst.Pix { for i, p := range dst.Pix {
res := uint8((uint16(p) + 8) / 16) res := uint8((uint16(p) + 8) / 16)
if i%2 == 0 { if i%2 == 0 {
result[i/2] = (res << 4) res = res << 4
} else { }
// note that integer division makes sure we're writing at the same
// index for odd and even indices
result[i/2] |= res result[i/2] |= res
} }
}
return result, nil return append(header, result...), nil
} }