Add http server

This commit is contained in:
heyarne 2022-02-09 23:19:23 +01:00
commit 9b177cbd6d
6 changed files with 278 additions and 85 deletions

View file

@ -5,27 +5,19 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd
import (
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"os"
"github.com/saunaclub/inkpot-cli/epd"
"github.com/spf13/cobra"
"golang.org/x/image/draw"
)
var width int
var height int
var infile string
var outfile string
func Foobar () (io.Writer, error) {
return os.Stdout, nil
}
// convertCmd represents the convert command
var convertCmd = &cobra.Command{
Use: "convert <file|->",
@ -50,9 +42,9 @@ Pass "-" as the filename to read from stdin.`,
defer input.Close()
}
// write to given outfile, default to stdout
var output io.Writer
if outfile == "" {
// write to given outfile, default to stdout
var output io.Writer
if outfile == "" || outfile == "-" {
output = os.Stdout
} else {
file, err := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0644)
@ -63,7 +55,7 @@ Pass "-" as the filename to read from stdin.`,
output = file
}
result, err := convertImage(input, width, height)
result, err := epd.ConvertImage(input, width, height)
if err != nil {
log.Fatalf("Could not convert image: %v", err)
}
@ -78,58 +70,3 @@ func init() {
convertCmd.Flags().IntVarP(&height, "height", "y", 960, "target height")
convertCmd.Flags().StringVarP(&outfile, "output", "o", "", "file to write the result to (default stdout)")
}
// Returns a new Rectangle that is resized and centered in `dst`
func fitRectInto(src *image.Rectangle, dst *image.Rectangle) image.Rectangle {
var targetWidth int
var targetHeight int
var scale float64
srcRatio := float64(src.Max.X) / float64(src.Max.Y)
dstRatio := float64(dst.Max.X) / float64(dst.Max.Y)
if srcRatio < dstRatio {
// center horizontally, scale vertically
scale = float64(dst.Max.Y) / float64(src.Max.Y)
} else {
// center vertically, scale horizontally
scale = float64(dst.Max.X) / float64(src.Max.X)
}
targetWidth = int(float64(src.Max.X) * scale)
targetHeight = int(float64(src.Max.Y) * scale)
targetX := (dst.Max.X - targetWidth) / 2
targetY := (dst.Max.Y - targetHeight) / 2
return image.Rect(targetX, targetY, targetWidth+targetX, targetHeight+targetY)
}
func convertImage(input io.Reader, width int, height int) ([]byte, error) {
src, _, err := image.Decode(input)
if err != nil {
return nil, err
}
dst := image.NewGray(image.Rect(0, 0, width, height))
srcBounds := src.Bounds()
targetRect := fitRectInto(&srcBounds, &dst.Rect)
draw.Draw(dst, dst.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
draw.CatmullRom.Scale(dst, targetRect, src, src.Bounds(), draw.Over, nil)
// the actual conversion works by packing two nibbles together in a byte
var result = make([]byte, (width*height+1)/2)
for i, p := range dst.Pix {
res := uint8((uint16(p) + 8) / 16)
if i%2 == 0 {
result[i/2] = (res << 4)
} else {
// note that integer division makes sure we're writing at the same
// index for odd and even indices
result[i/2] = result[i/2] | res
}
}
return result, nil
}