-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |