Last active
February 4, 2025 05:28
-
-
Save quells/aaa9a1e4c9fbc8674c3faab3529e9a2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"image" | |
"image/color" | |
"log" | |
"os" | |
"golang.org/x/image/bmp" | |
) | |
func main() { | |
f, err := os.Create("out.bmp") | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
defer f.Close() | |
w, h := 800, 480 | |
im := NewBW(w, h) | |
for y := 0; y < h; y++ { | |
for x := 0; x < w; x++ { | |
idx := x + y*w | |
im.Buf[idx] = byte((x ^ y) & 1) | |
} | |
} | |
err = bmp.Encode(f, im) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
} | |
type Color byte | |
func (c Color) RGBA() (r, g, b, a uint32) { | |
a = 0xffff | |
if c != 0 { | |
r = 0xffff | |
g = 0xffff | |
b = 0xffff | |
} | |
return | |
} | |
type BW struct { | |
Width, Height int | |
Buf []byte | |
} | |
func NewBW(w, h int) *BW { | |
return &BW{ | |
Width: w, | |
Height: h, | |
Buf: make([]byte, w*h), | |
} | |
} | |
func (im *BW) ColorModel() color.Model { | |
return color.Palette([]color.Color{Color(0), Color(1)}) | |
} | |
func (im *BW) Bounds() image.Rectangle { | |
return image.Rectangle{Max: image.Point{X: im.Width, Y: im.Height}} | |
} | |
func (im *BW) At(x, y int) color.Color { | |
idx := x + y*im.Width | |
if idx >= len(im.Buf) { | |
return nil | |
} | |
return Color(im.Buf[idx]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment