Browse Source

first commit

master
Lars Hoogestraat 7 months ago
commit
f02a707e5f
4 changed files with 153 additions and 0 deletions
  1. +14
    -0
      LICENSE
  2. +3
    -0
      go.mod
  3. +92
    -0
      rgb565.go
  4. +44
    -0
      rgb565_test.go

+ 14
- 0
LICENSE View File

@ -0,0 +1,14 @@
The MIT License (MIT)
Copyright (c) 2020 Lars Hoogestraat
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 3
- 0
go.mod View File

@ -0,0 +1,3 @@
module git.hoogi.eu/snafu/rgb565
go 1.15

+ 92
- 0
rgb565.go View File

@ -0,0 +1,92 @@
// Copyright 2020 Lars Hoogestraat
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rgb565
import (
"encoding/binary"
"image"
"image/color"
"math"
)
// RGBA is an in-memory image whose At method returns color.RGBA values.
type RGB565 struct {
// Pix holds the image's pixels, as RGB565 values. The pixel at
// (x, y) starts at Pix[(y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*2].
Pix []uint8
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
//Model is the Model for RGB565 colors.
var Model = color.ModelFunc(rgb565Model)
//Color represents a RGB565 color.
type Color uint16
// NewRGB565 returns a new RGB565 image with the given bounds.
func NewRGB565(r image.Rectangle) *RGB565 {
return &RGB565{
Pix: make([]uint8, 2*r.Dx()*r.Dy()),
Stride: 2 * r.Dx(),
Rect: r,
}
}
func (p *RGB565) Bounds() image.Rectangle {
return p.Rect
}
func (p *RGB565) At(x, y int) color.Color {
if !(image.Point{x, y}.In(p.Rect)) {
return Color(0)
}
i := p.PixOffset(x, y)
return Color(binary.LittleEndian.Uint16(p.Pix[i : i+2]))
}
func (p *RGB565) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*2
}
func (p *RGB565) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
binary.LittleEndian.PutUint16(p.Pix[i:i+2], uint16(Model.Convert(c).(Color)))
}
func (p *RGB565) ColorModel() color.Model {
return Model
}
func (c Color) RGBA() (r, g, b, a uint32) {
r = uint32(c&0b1111100000000000) >> 11
g = uint32(c&0b0000011111100000) >> 5
b = uint32(c&0b0000000000011111) >> 0
r = uint32(math.Round(float64(r)*255/31)) << 8
g = uint32(math.Round(float64(g)*255/63)) << 8
b = uint32(math.Round(float64(b)*255/31)) << 8
a = 0xffff
return
}
func rgb565Model(c color.Color) color.Color {
if _, ok := c.(Color); ok {
return c
}
r, g, b, _ := c.RGBA()
return Color((r<<8)&0b1111100000000000 | (g<<3)&0b0000011111100000 | (b>>3)&0b0000000000011111)
}

+ 44
- 0
rgb565_test.go View File

@ -0,0 +1,44 @@
package rgb565_test
import (
"image/color"
"testing"
"git.hoogi.eu/snafu/rgb565"
)
var rgb565TestCases = []struct {
rgb color.RGBA
rgb565 rgb565.Color
}{
{rgb: color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}, rgb565: 0x0000},
{rgb: color.RGBA{R: 0x84, G: 0x00, B: 0x00, A: 0xff}, rgb565: 0x8000},
{rgb: color.RGBA{R: 0x00, G: 0x82, B: 0x00, A: 0xff}, rgb565: 0x0400},
{rgb: color.RGBA{R: 0x00, G: 0x00, B: 0x84, A: 0xff}, rgb565: 0x0010},
{rgb: color.RGBA{R: 0x00, G: 0x2d, B: 0x84, A: 0xff}, rgb565: 0x170},
{rgb: color.RGBA{R: 0x84, G: 0x2d, B: 0x84, A: 0xff}, rgb565: 0x8170},
{rgb: color.RGBA{R: 0x84, G: 0xff, B: 0xff, A: 0xff}, rgb565: 0x87ff},
{rgb: color.RGBA{R: 0x84, G: 0x82, B: 0xff, A: 0xff}, rgb565: 0x841f},
{rgb: color.RGBA{R: 0x84, G: 0x82, B: 0x84, A: 0xff}, rgb565: 0x8410},
{rgb: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}, rgb565: 0xFFFF},
}
func TestRGBToRGB565(t *testing.T) {
for _, test := range rgb565TestCases {
got := rgb565.Model.Convert(test.rgb)
want := test.rgb565
if got != want {
t.Errorf("unexpected RGB565 value for %+v: got: %016b, want: %016b", test.rgb, got, want)
}
}
}
func TestRGB565ToRGBA(t *testing.T) {
for _, test := range rgb565TestCases {
got := color.RGBAModel.Convert(test.rgb565).(color.RGBA)
want := test.rgb
if got != want {
t.Errorf("unexpected RGBA value for %x: got: %+v, want: %+v", test.rgb565, got, want)
}
}
}

Loading…
Cancel
Save