diff --git a/tools/monochrome/cmd/main/main.go b/tools/monochrome/cmd/main/main.go new file mode 100644 index 0000000..03ba835 --- /dev/null +++ b/tools/monochrome/cmd/main/main.go @@ -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 +} diff --git a/tools/monochrome/go.mod b/tools/monochrome/go.mod new file mode 100644 index 0000000..0a6c5d4 --- /dev/null +++ b/tools/monochrome/go.mod @@ -0,0 +1,5 @@ +module github.com/polldo/dolp/tools/monochrome + +go 1.17 + +require golang.org/x/image v0.0.0-20220321031419-a8550c1d254a diff --git a/tools/monochrome/go.sum b/tools/monochrome/go.sum new file mode 100644 index 0000000..0537b40 --- /dev/null +++ b/tools/monochrome/go.sum @@ -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= diff --git a/tools/monochrome/monochrome.go b/tools/monochrome/monochrome.go new file mode 100644 index 0000000..be2e300 --- /dev/null +++ b/tools/monochrome/monochrome.go @@ -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} +}