Implement 4-bit image format

This commit is contained in:
heyarne 2022-02-09 20:05:35 +01:00
commit 6034a8c17d

View file

@ -5,10 +5,10 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd package cmd
import ( import (
"fmt"
"image" "image"
"image/color"
"image/jpeg" "image/jpeg"
"image/png" _ "image/png"
"log" "log"
"os" "os"
@ -76,22 +76,33 @@ func convertImage(width int, height int) {
defer input.Close() defer input.Close()
// … and writing to a predefined output file // … and writing to a predefined output file
output, err := os.OpenFile("cat_resized.png", os.O_RDWR|os.O_CREATE, 0644) output, err := os.OpenFile("cat_resized.epd", os.O_RDWR|os.O_CREATE, 0644)
if err != nil { if err != nil {
log.Fatalf("Error writing output file: %v", err) log.Fatalf("Error writing output file: %v", err)
} }
defer output.Close() defer output.Close()
src, _ := jpeg.Decode(input) src, _ := jpeg.Decode(input)
dst := image.NewGray16(image.Rect(0, 0, width, height)) dst := image.NewGray(image.Rect(0, 0, width, height))
srcBounds := src.Bounds() srcBounds := src.Bounds()
targetRect := fitRectInto(&srcBounds, &dst.Rect) targetRect := fitRectInto(&srcBounds, &dst.Rect)
fmt.Printf("targetRect: %v", targetRect) 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're taking care of a more efficient output encoding later // the actual conversion works by packing two nibbles together in a byte
png.Encode(output, dst) 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
}
}
output.Write(result)
} }