Skip to content

Commit

Permalink
Add tool for images
Browse files Browse the repository at this point in the history
  • Loading branch information
polldo committed Mar 29, 2022
1 parent a6ec93c commit dc87d73
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tools/monochrome/cmd/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"flag"
"fmt"
"image"

_ "golang.org/x/image/bmp"

_ "image/jpeg"
_ "image/png"

"os"

"github.com/polldo/dolp/tools/monochrome"
)

func main() {
var filepath string
flag.StringVar(&filepath, "i", "", "path of image to convert")
flag.Parse()
if filepath == "" {
fmt.Println("please provide a filepath")
os.Exit(1)
}

img, err := openImage(filepath)
if err != nil {
fmt.Printf("cannot open file %s: %s\n", filepath, err)
os.Exit(1)
}

b := monochrome.Convert(img)
fmt.Printf("Image for c++: \n%s\n", b.ToCpp())
fmt.Println()
fmt.Printf("Image for python: \n%s\n", b.ToPy())
}

func openImage(filepath string) (image.Image, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, fmt.Errorf("opening file %s: %w", filepath, err)
}
defer f.Close()
img, _, err := image.Decode(f)
return img, err
}
5 changes: 5 additions & 0 deletions tools/monochrome/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/polldo/dolp/tools/monochrome

go 1.17

require golang.org/x/image v0.0.0-20220321031419-a8550c1d254a
4 changes: 4 additions & 0 deletions tools/monochrome/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a h1:LnH9RNcpPv5Kzi15lXg42lYMPUf0x8CuPv1YnvBWZAg=
golang.org/x/image v0.0.0-20220321031419-a8550c1d254a/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
56 changes: 56 additions & 0 deletions tools/monochrome/monochrome.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package monochrome

import (
"fmt"
"image"
"image/color"
"strings"
)

type Monochrome struct {
val []byte
width int
height int
}

func (m *Monochrome) ToCpp() string {
s := "const uint8_t image[] = { "
s += fmt.Sprintf("0x%02x, 0x%02x, ", m.width, m.height)
for _, b := range m.val {
s += fmt.Sprintf("0x%02x, ", b)
}
s = strings.TrimRight(s, ", ")
s += " };"
return s
}

func (m *Monochrome) ToPy() string {
s := "image = bytearray(b'"
s += fmt.Sprintf("\\x%02x\\x%02x", m.width, m.height)
for _, b := range m.val {
s += fmt.Sprintf("\\x%02x", b)
}
s += "')"
return s
}

func Convert(img image.Image) *Monochrome {
w, h := img.Bounds().Dx(), img.Bounds().Dy()
conv := make([]byte, 0, w*h/8)

for y := 0; y < h; y += 8 {
for x := 0; x < w; x++ {
var col byte
for j := 7; j >= 0; j-- {
pixel := 1
if color.Gray16Model.Convert(img.At(x, y+j)) == color.Black {
pixel = 0
}
col = (col << 1) | byte(pixel)
}
conv = append(conv, col)
}
}

return &Monochrome{val: conv, width: w, height: h}
}

0 comments on commit dc87d73

Please sign in to comment.