Implement 4-bit image format
This commit is contained in:
parent
7dadd7b7f7
commit
6034a8c17d
1 changed files with 22 additions and 11 deletions
|
|
@ -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"
|
||||||
|
|
||||||
|
|
@ -64,7 +64,7 @@ func fitRectInto(src *image.Rectangle, dst *image.Rectangle) image.Rectangle {
|
||||||
targetX := (dst.Max.X - targetWidth) / 2
|
targetX := (dst.Max.X - targetWidth) / 2
|
||||||
targetY := (dst.Max.Y - targetHeight) / 2
|
targetY := (dst.Max.Y - targetHeight) / 2
|
||||||
|
|
||||||
return image.Rect(targetX, targetY, targetWidth + targetX, targetHeight + targetY)
|
return image.Rect(targetX, targetY, targetWidth+targetX, targetHeight+targetY)
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertImage(width int, height int) {
|
func convertImage(width int, height int) {
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue