Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,104 @@
|
||||
package pixel
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
// ToGrayBits converts a color to a n-bit grayscale color.
|
||||
func ToGrayBits(c color.Color, n uint8) uint16 {
|
||||
r, g, b, _ := c.RGBA()
|
||||
|
||||
if n < 1 {
|
||||
return 0 // no color
|
||||
} else if n > 16 {
|
||||
n = 16 // most that will fit
|
||||
}
|
||||
|
||||
// Shift values to fit n bits.
|
||||
s := 32 - n
|
||||
|
||||
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
|
||||
// as those given by the JFIF specification and used by func RGBToYCbCr in
|
||||
// image/color/ycbcr.go.
|
||||
//
|
||||
// Note that 19595 + 38470 + 7471 equals 65536.
|
||||
//
|
||||
// The 28 is 16 + 8 + n. The 16 is the same as used in RGBToYCbCr. The 8 + n is
|
||||
// because the return value is n-bit color, not 16 bit color.
|
||||
return uint16((19595*r + 38470*g + 7471*b + 1<<15) >> s)
|
||||
}
|
||||
|
||||
func IsOn(c color.Color) bool {
|
||||
return ToGrayBits(c, 1) == 1
|
||||
}
|
||||
|
||||
func IsOff(c color.Color) bool {
|
||||
return ToGrayBits(c, 1) == 0
|
||||
}
|
||||
|
||||
// Monochrome is a 1-bit color.
|
||||
type Monochrome uint8
|
||||
|
||||
// Monochrome constants.
|
||||
const (
|
||||
On Monochrome = iota
|
||||
Off
|
||||
)
|
||||
|
||||
func (m Monochrome) RGBA() (r, g, b, a uint32) {
|
||||
if m == 0 {
|
||||
return 0, 0, 0, 0
|
||||
}
|
||||
return 0xffff, 0xffff, 0xffff, 0xffff
|
||||
}
|
||||
|
||||
var MonochromeModel color.Model = color.ModelFunc(monochromeModel)
|
||||
|
||||
func monochromeModel(c color.Color) color.Color {
|
||||
if _, ok := c.(Monochrome); ok {
|
||||
return c
|
||||
}
|
||||
if IsOn(c) {
|
||||
return Monochrome(1)
|
||||
}
|
||||
return Monochrome(0)
|
||||
}
|
||||
|
||||
type MonochromeImage struct {
|
||||
Pix []byte
|
||||
Rect image.Rectangle
|
||||
Stride int
|
||||
}
|
||||
|
||||
func NewMonochromeImage(r image.Rectangle) *MonochromeImage {
|
||||
var (
|
||||
w, h = r.Dx(), r.Dy()
|
||||
size = w * h / 8
|
||||
pix = make([]byte, size)
|
||||
)
|
||||
return &MonochromeImage{pix, r, w / 8}
|
||||
}
|
||||
|
||||
func (i *MonochromeImage) ColorModel() color.Model { return MonochromeModel }
|
||||
func (i *MonochromeImage) Bounds() image.Rectangle { return i.Rect }
|
||||
|
||||
func (i *MonochromeImage) At(x, y int) color.Color {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return Monochrome(0)
|
||||
}
|
||||
o := y*i.Stride + x/8
|
||||
return Monochrome(i.Pix[o] >> (7 - uint(x%8)) & 1)
|
||||
}
|
||||
|
||||
func (i *MonochromeImage) Set(x, y int, c color.Color) {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return
|
||||
}
|
||||
o := y*i.Stride + x/8
|
||||
if IsOn(c) {
|
||||
i.Pix[o] |= 1 << (7 - uint(x%8))
|
||||
} else {
|
||||
i.Pix[o] &^= 1 << (7 - uint(x%8))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user