Skip to content

Commit

Permalink
简化 image 包
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Dec 1, 2023
1 parent c7624be commit 3e48f6a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 139 deletions.
8 changes: 0 additions & 8 deletions waroot/src/image/color/color.wa
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ type Gray struct {
Y: u8
}

type Gray16 struct {
Y: u16
}

type RGBA struct {
R, G, B, A: u8
}

type RGBA64 struct {
R, G, B, A: u16
}
28 changes: 0 additions & 28 deletions waroot/src/image/draw/draw.wa

This file was deleted.

38 changes: 3 additions & 35 deletions waroot/src/image/geom.wa
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import (
"math/bits"
)

// 二维点
type Point struct {
X, Y: int
Expand All @@ -28,7 +24,7 @@ func Rect(x0, y0, x1, y1: int) => Rectangle {
if y0 > y1 {
y0, y1 = y1, y0
}
return Rectangle{ Min: Point{x0, y0}, Max: Point{x1, y1} }
return Rectangle{Min: Point{x0, y0}, Max: Point{x1, y1}}
}

func Rectangle.Dx => int {
Expand All @@ -45,36 +41,8 @@ func Rectangle.Size => Point {
this.Max.Y - this.Min.Y,
}
}
func Rectangle.Contain(x, y: int) => bool {

func Rectangle.Contains(x, y: int) => bool {
return this.Min.X <= x && x < this.Max.X &&
this.Min.Y <= y && y < this.Max.Y
}

func pixelBufferLength(bytesPerPixel: int, r: Rectangle, imageTypeName: string) => int {
totalLength := mul3NonNeg(bytesPerPixel, r.Dx(), r.Dy())
if totalLength < 0 {
panic("image: New" + imageTypeName + " Rectangle has huge or negative dimensions")
}
return totalLength
}

// mul3NonNeg returns (x * y * z), unless at least one argument is negative or
// if the computation overflows the int type, in which case it returns -1.
func mul3NonNeg(x: int, y: int, z: int) => int {
if (x < 0) || (y < 0) || (z < 0) {
return -1
}
hi, lo := bits.Mul64(uint64(x), uint64(y))
if hi != 0 {
return -1
}
hi, lo = bits.Mul64(lo, uint64(z))
if hi != 0 {
return -1
}
a := int(lo)
if (a < 0) || (uint64(a) != lo) {
return -1
}
return a
}
21 changes: 0 additions & 21 deletions waroot/src/image/image.wa
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,20 @@ type Image interface {
At(x, y: int) => color.RGBA
}

// 图像接口(16bit)
type RGBA64Image interface {
Bounds() => Rectangle
PixOffset(x, y: int) => int
At(x, y: int) => color.RGBA64
}

// 二维图像 Gray
type Gray struct {
Pix: []u8
Stride: int
Rect: Rectangle
}

// 二维图像 Gray16
type Gray16 struct {
Pix: []u16
Stride: int
Rect: Rectangle
}

// 二维图像 RGBA
type RGBA struct {
Pix: []u8
Stride: int
Rect: Rectangle
}

// 二维图像 RGBA64
type RGBA64 struct {
Pix: []u16
Stride: int
Rect: Rectangle
}

// 调色板图像
type Paletted struct {
Pix: []u8
Expand Down
51 changes: 4 additions & 47 deletions waroot/src/image/image_impl.wa
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import "image/color"
global (
_ :Image = (*Gray)(nil)
_ :Image = (*RGBA)(nil)

_ :RGBA64Image = (*Gray16)(nil)
_ :RGBA64Image = (*RGBA64)(nil)
)

func NewGray(r: Rectangle) => *Gray {
return &Gray{
Pix: make([]u8, pixelBufferLength(1, r, "Gray")),
Pix: make([]u8, r.Dx()*r.Dy()),
Stride: 1 * r.Dx(),
Rect: r,
}
Expand All @@ -29,28 +26,9 @@ func Gray.At(x, y: int) => color.RGBA {
return color.RGBA{v, v, v, 0xFF}
}

func NewGray16(r: Rectangle) => *Gray16 {
return &Gray16{
Pix: make([]u16, pixelBufferLength(2, r, "Gray16")),
Stride: 2 * r.Dx(),
Rect: r,
}
}
func Gray16.Bounds() => Rectangle {
return this.Rect
}
func Gray16.PixOffset(x, y: int) => int {
return (y-this.Rect.Min.Y)*this.Stride + (x-this.Rect.Min.X)*2
}
func Gray16.At(x, y: int) => color.RGBA64 {
i := this.PixOffset(x, y)
v := this.Pix[i]
return color.RGBA64{v, v, v, 0xFFFF}
}

func NewRGBA(r: Rectangle) => *RGBA {
return &RGBA{
Pix: make([]u8, pixelBufferLength(4, r, "RGBA")),
Pix: make([]u8, 4*r.Dx()*r.Dy()),
Stride: 4 * r.Dx(),
Rect: r,
}
Expand All @@ -69,7 +47,7 @@ func RGBA.At(x, y: int) => color.RGBA {
}

func RGBA.SetRGBA(x, y: int, c: color.RGBA) {
if !this.Rect.Contain(x, y) {
if !this.Rect.Contains(x, y) {
return
}
i := this.PixOffset(x, y)
Expand All @@ -80,30 +58,9 @@ func RGBA.SetRGBA(x, y: int, c: color.RGBA) {
s[3] = c.A
}

func NewRGBA64(r: Rectangle) => *RGBA64 {
return &RGBA64{
Pix: make([]u16, pixelBufferLength(8, r, "RGBA64")),
Stride: 8 * r.Dx(),
Rect: r,
}
}

func RGBA64.Bounds() => Rectangle {
return this.Rect
}

func RGBA64.PixOffset(x, y: int) => int {
return (y-this.Rect.Min.Y)*this.Stride + (x-this.Rect.Min.X)*8
}
func RGBA64.At(x, y: int) => color.RGBA64 {
i := this.PixOffset(x, y)
s := this.Pix[i : i+4 : i+4]
return color.RGBA64{s[0], s[1], s[2], s[3]}
}

func NewPaletted(r: Rectangle, p: []color.RGBA) => *Paletted {
return &Paletted{
Pix: make([]u8, pixelBufferLength(1, r, "Paletted")),
Pix: make([]u8, r.Dx()*r.Dy()),
Stride: 1 * r.Dx(),
Rect: r,
Palette: p,
Expand Down

0 comments on commit 3e48f6a

Please sign in to comment.