Add adjustPixels

This commit is contained in:
Hajime Hoshi 2013-07-07 04:39:56 +09:00
parent 239f008045
commit 9b2e8d023f
3 changed files with 51 additions and 8 deletions

View File

@ -20,4 +20,5 @@
package opengl
var AdjustPixels = adjustPixels
var Clp2 = clp2

View File

@ -40,6 +40,22 @@ func clp2(x uint64) uint64 {
return x + 1
}
func adjustPixels(width, height int, pixels []uint8) []uint8 {
textureWidth := int(clp2(uint64(width)))
textureHeight := int(clp2(uint64(height)))
if width == textureWidth && height == textureHeight {
return pixels
}
newPixels := make([]uint8, textureWidth*textureHeight*4)
for j := 0; j < height; j++ {
copy(newPixels[textureWidth*4*j:],
pixels[width*4*j:width*4*j+width*4])
}
return newPixels
}
type Texture struct {
id C.GLuint
width int
@ -49,16 +65,11 @@ type Texture struct {
}
func createTexture(width, height int, pixels []uint8) *Texture {
if pixels != nil {
pixels = adjustPixels(width, height, pixels)
}
textureWidth := int(clp2(uint64(width)))
textureHeight := int(clp2(uint64(height)))
if pixels != nil {
if width != textureWidth {
panic("sorry, but width should be power of 2")
}
if height != textureHeight {
panic("sorry, but height should be power of 2")
}
}
texture := &Texture{
id: 0,
width: width,

View File

@ -25,6 +25,37 @@ import (
"testing"
)
func TestAdjustPixels(t *testing.T) {
pixels := [...]uint8{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
}
result := AdjustPixels(3, 5, pixels[0:len(pixels)])
wanted := [...]uint8{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
if len(wanted) != len(result) {
t.Errorf("len(result) = %d, wanted %d",
len(result), len(wanted))
}
for i := 0; i < len(result); i++ {
if wanted[i] != result[i] {
t.Errorf("result[%d] = %d, wanted %d",
i, result[i], wanted[i])
}
}
}
func TestClp2(t *testing.T) {
testCases := []struct {
expected uint64