Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package pixel
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
// Color555 is a 16-bit RGB555 color.
|
||||
//
|
||||
// The 5 most significant bits are red, the 5 middle bits are green, and the 5 last ones are blue:
|
||||
//
|
||||
// 0RRRRRGG GGGBBBBB
|
||||
type Color555 uint16
|
||||
|
||||
func (c Color555) RGBA() (r, g, b, a uint32) {
|
||||
r = ((uint32(c&0x7c00) >> 10) * 255) / 31
|
||||
r |= r << 8
|
||||
g = ((uint32(c&0x03e0) >> 5) * 255) / 31
|
||||
g |= g << 8
|
||||
b = ((uint32(c&0x001f) >> 0) * 255) / 31
|
||||
b |= b << 8
|
||||
a = 0xFFFF
|
||||
return
|
||||
}
|
||||
|
||||
var Color555Model color.Model = color.ModelFunc(color555Model)
|
||||
|
||||
func color555Model(c color.Color) color.Color {
|
||||
if _, ok := c.(Color565); ok {
|
||||
return c
|
||||
}
|
||||
r, g, b, _ := c.RGBA() // 16 bits per channel
|
||||
r = (r & 0xf800) >> 1 // .RRRRR.. ........
|
||||
g = (g & 0xf800) >> 6 // ......GG GGG.....
|
||||
b = (b & 0xf800) >> 11 // ........ ...BBBBB
|
||||
return Color555(r | g | b)
|
||||
}
|
||||
|
||||
type Image555 struct {
|
||||
Pix []byte
|
||||
Rect image.Rectangle
|
||||
Stride int
|
||||
}
|
||||
|
||||
func NewImage555(r image.Rectangle) *Image555 {
|
||||
var (
|
||||
w, h = r.Dx(), r.Dy()
|
||||
size = w * h * 2
|
||||
pix = make([]byte, size)
|
||||
)
|
||||
return &Image555{pix, r, w * 2}
|
||||
}
|
||||
|
||||
func (i *Image555) ColorModel() color.Model { return Color555Model }
|
||||
func (i *Image555) Bounds() image.Rectangle { return i.Rect }
|
||||
|
||||
func (i *Image555) At(x, y int) color.Color {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return Color555(0)
|
||||
}
|
||||
o := y*i.Stride + x*2
|
||||
return Color555(i.Pix[o]) | Color555(i.Pix[o+1])<<8
|
||||
}
|
||||
|
||||
func (i *Image555) Set(x, y int, c color.Color) {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return
|
||||
}
|
||||
o := y*i.Stride + x*2
|
||||
cc := color555Model(c).(Color555)
|
||||
i.Pix[o] = byte(cc)
|
||||
i.Pix[o+1] = byte(cc >> 8)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package pixel
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testsColor555 = []struct {
|
||||
name string
|
||||
test Color555
|
||||
r, g, b uint32
|
||||
}{
|
||||
{"black",
|
||||
Color555(0b000000000000000), 0, 0, 0},
|
||||
{"white",
|
||||
Color555(0b111111111111111), 255, 255, 255},
|
||||
{"red",
|
||||
Color555(0b111110000000000), 255, 0, 0},
|
||||
{"green",
|
||||
Color555(0b000001111100000), 0, 255, 0},
|
||||
{"blue",
|
||||
Color555(0b000001001111011), 0, 156, 222},
|
||||
{"yellow",
|
||||
Color555(0b111111011000001), 255, 180, 8},
|
||||
{"cyan",
|
||||
Color555(0b000001111111111), 0, 255, 255},
|
||||
{"magenta",
|
||||
Color555(0b111110000011111), 255, 0, 255},
|
||||
}
|
||||
|
||||
func TestColor555(t *testing.T) {
|
||||
for _, test := range testsColor555 {
|
||||
t.Run(test.name, func(it *testing.T) {
|
||||
r, g, b, _ := test.test.RGBA()
|
||||
r >>= 8 // undo the premultiplication
|
||||
g >>= 8
|
||||
b >>= 8
|
||||
if r != test.r || g != test.g || b != test.b {
|
||||
it.Errorf("expected %d %d %d, got %d %d %d", test.r, test.g, test.b, r, g, b)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModel555(t *testing.T) {
|
||||
for _, test := range testsColor555 {
|
||||
t.Run(test.name, func(it *testing.T) {
|
||||
c := Color555Model.Convert(color.RGBA{uint8(test.r), uint8(test.g), uint8(test.b), 255})
|
||||
if c != test.test {
|
||||
it.Errorf("expected %#04x, got %#04x", test.test, c)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package pixel
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
// Color565 is a 16-bit RGB565 color.
|
||||
//
|
||||
// The 5 most significant bits are red, the 6 middle bits are green, and the 5 last ones are blue:
|
||||
//
|
||||
// RRRRRGGG GGGBBBBB
|
||||
type Color565 uint16
|
||||
|
||||
//func RGB565(r, g, b uint8) Color565 {
|
||||
// return Color565(uint16(r&0xF8)<<8 | uint16(g&0xFC)<<3 | uint16(b>>3))
|
||||
//}
|
||||
|
||||
func (c Color565) RGBA() (r, g, b, a uint32) {
|
||||
r = ((uint32(c&0xf800) >> 11) * 255) / 31
|
||||
r |= r << 8
|
||||
g = ((uint32(c&0x07e0) >> 5) * 255) / 63
|
||||
g |= g << 8
|
||||
b = ((uint32(c&0x001f) >> 0) * 255) / 31
|
||||
b |= b << 8
|
||||
a = 0xFFFF
|
||||
return
|
||||
}
|
||||
|
||||
var Color565Model color.Model = color.ModelFunc(color565Model)
|
||||
|
||||
func color565Model(c color.Color) color.Color {
|
||||
if _, ok := c.(Color565); ok {
|
||||
return c
|
||||
}
|
||||
r, g, b, _ := c.RGBA() // 16 bits per channel
|
||||
r = (r & 0xf800) // RRRRR... ........
|
||||
g = (g & 0xfc00) >> 5 // .....GGG GGG.....
|
||||
b = (b & 0xf800) >> 11 // ........ ...BBBBB
|
||||
return Color565(r | g | b)
|
||||
}
|
||||
|
||||
type Image565 struct {
|
||||
Pix []byte
|
||||
Rect image.Rectangle
|
||||
Stride int
|
||||
}
|
||||
|
||||
func NewImage565(r image.Rectangle) *Image565 {
|
||||
var (
|
||||
w, h = r.Dx(), r.Dy()
|
||||
size = w * h * 2
|
||||
pix = make([]byte, size)
|
||||
)
|
||||
return &Image565{pix, r, w * 2}
|
||||
}
|
||||
|
||||
func (i *Image565) ColorModel() color.Model { return Color565Model }
|
||||
func (i *Image565) Bounds() image.Rectangle { return i.Rect }
|
||||
|
||||
func (i *Image565) At(x, y int) color.Color {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return Color565(0)
|
||||
}
|
||||
o := y*i.Stride + x*2
|
||||
return Color565(i.Pix[o]) | Color565(i.Pix[o+1])<<8
|
||||
}
|
||||
|
||||
func (i *Image565) Set(x, y int, c color.Color) {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return
|
||||
}
|
||||
o := y*i.Stride + x*2
|
||||
cc := color565Model(c).(Color565)
|
||||
i.Pix[o] = byte(cc)
|
||||
i.Pix[o+1] = byte(cc >> 8)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package pixel
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testsColor565 = []struct {
|
||||
name string
|
||||
test Color565
|
||||
r, g, b uint32
|
||||
}{
|
||||
{"black",
|
||||
Color565(0b0000000000000000), 0, 0, 0},
|
||||
{"white",
|
||||
Color565(0b1111111111111111), 255, 255, 255},
|
||||
{"red",
|
||||
Color565(0b1111100000000000), 255, 0, 0},
|
||||
{"green",
|
||||
Color565(0b0000011111100000), 0, 255, 0},
|
||||
{"blue",
|
||||
Color565(0b0000010011111100), 0, 157, 230},
|
||||
{"yellow",
|
||||
Color565(0b1111110111000001), 255, 186, 8},
|
||||
{"cyan",
|
||||
Color565(0b0000011111111111), 0, 255, 255},
|
||||
{"magenta",
|
||||
Color565(0b1111100000011111), 255, 0, 255},
|
||||
}
|
||||
|
||||
func TestColor565(t *testing.T) {
|
||||
for _, test := range testsColor565 {
|
||||
t.Run(test.name, func(it *testing.T) {
|
||||
r, g, b, _ := test.test.RGBA()
|
||||
r >>= 8 // undo the premultiplication
|
||||
g >>= 8
|
||||
b >>= 8
|
||||
if r != test.r || g != test.g || b != test.b {
|
||||
it.Errorf("expected %d %d %d, got %d %d %d", test.r, test.g, test.b, r, g, b)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModel565(t *testing.T) {
|
||||
for _, test := range testsColor565 {
|
||||
t.Run(test.name, func(it *testing.T) {
|
||||
c := Color565Model.Convert(color.RGBA{uint8(test.r), uint8(test.g), uint8(test.b), 255})
|
||||
if c != test.test {
|
||||
it.Errorf("expected %#04x, got %#04x", test.test, c)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package pixel
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
// Gray4 represents a 4-bit grayscale color.
|
||||
type Gray4 struct {
|
||||
Y uint8
|
||||
}
|
||||
|
||||
func (c Gray4) RGBA() (r, g, b, a uint32) {
|
||||
y := uint32(c.Y & 0x0f)
|
||||
y |= y << 4
|
||||
y |= y << 8
|
||||
return y, y, y, 0xffff
|
||||
}
|
||||
|
||||
var Gray4Model color.Model = color.ModelFunc(gray4Model)
|
||||
|
||||
func gray4Model(c color.Color) color.Color {
|
||||
if _, ok := c.(Gray4); ok {
|
||||
return c
|
||||
}
|
||||
r, g, b, _ := c.RGBA()
|
||||
|
||||
// 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
|
||||
// ycbcr.go.
|
||||
//
|
||||
// Note that 19595 + 38470 + 7471 equals 65536.
|
||||
//
|
||||
// The 28 is 16 + 8 + 4. The 16 is the same as used in RGBToYCbCr. The 8 + 4 is
|
||||
// because the return value is 4 bit color, not 16 bit color.
|
||||
y := (19595*r + 38470*g + 7471*b + 1<<15) >> 28
|
||||
|
||||
return Gray4{uint8(y)}
|
||||
}
|
||||
|
||||
// ImageGray4 contains 4-bit grayscale pixels.
|
||||
type ImageGray4 struct {
|
||||
Pix []byte
|
||||
Rect image.Rectangle
|
||||
Stride int
|
||||
}
|
||||
|
||||
func NewImageGray4(r image.Rectangle) *ImageGray4 {
|
||||
var (
|
||||
w, h = r.Dx(), r.Dy()
|
||||
size = w * h / 2
|
||||
pix = make([]byte, size)
|
||||
)
|
||||
return &ImageGray4{pix, r, w / 2}
|
||||
}
|
||||
|
||||
func (i *ImageGray4) ColorModel() color.Model { return Gray4Model }
|
||||
func (i *ImageGray4) Bounds() image.Rectangle { return i.Rect }
|
||||
|
||||
func (i *ImageGray4) At(x, y int) color.Color {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return Gray4{0}
|
||||
}
|
||||
o := (y*i.Stride + x) >> 1
|
||||
if x&1 == 0 { // even x is stored in the high bits
|
||||
return Gray4{i.Pix[o] >> 4}
|
||||
}
|
||||
return Gray4{i.Pix[o] & 0x0f}
|
||||
}
|
||||
|
||||
func (i *ImageGray4) Set(x, y int, c color.Color) {
|
||||
if !(image.Point{x, y}.In(i.Rect)) {
|
||||
return
|
||||
}
|
||||
o := (y*i.Stride + x) >> 1
|
||||
if x&1 == 0 { // even x is stored in the high bits
|
||||
i.Pix[o] = i.Pix[o]&0x0f | c.(Gray4).Y<<4
|
||||
} else {
|
||||
i.Pix[o] = i.Pix[o]&0xf0 | c.(Gray4).Y
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user