-
Notifications
You must be signed in to change notification settings - Fork 23
/
pow2.go
94 lines (73 loc) · 2.12 KB
/
pow2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gltext
import (
"fmt"
"image"
"image/color"
)
// Pow2 returns the first power-of-two value >= to n.
// This can be used to create suitable texture dimensions.
func Pow2(x uint32) uint32 {
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
return x + 1
}
// IsPow2 returns true if the given value is a power-of-two.
func IsPow2(x uint32) bool { return (x & (x - 1)) == 0 }
// Pow2Image returns the given image, scaled to the smallest power-of-two
// dimensions larger or equal to the input dimensions.
// It preserves the image format and contents.
//
// This is useful if an image is to be used as an OpenGL texture.
// These often require image data to have power-of-two dimensions.
func Pow2Image(src image.Image) image.Image {
sb := src.Bounds()
w, h := uint32(sb.Dx()), uint32(sb.Dy())
if IsPow2(w) && IsPow2(h) {
return src // Nothing to do.
}
rect := image.Rect(0, 0, int(Pow2(w)), int(Pow2(h)))
switch src := src.(type) {
case *image.Alpha:
return copyImg(src, image.NewAlpha(rect))
case *image.Alpha16:
return copyImg(src, image.NewAlpha16(rect))
case *image.Gray:
return copyImg(src, image.NewGray(rect))
case *image.Gray16:
return copyImg(src, image.NewGray16(rect))
case *image.NRGBA:
return copyImg(src, image.NewNRGBA(rect))
case *image.NRGBA64:
return copyImg(src, image.NewNRGBA64(rect))
case *image.Paletted:
return copyImg(src, image.NewPaletted(rect, src.Palette))
case *image.RGBA:
return copyImg(src, image.NewRGBA(rect))
case *image.RGBA64:
return copyImg(src, image.NewRGBA64(rect))
}
panic(fmt.Sprintf("Unsupported image format: %T", src))
}
// Why the image.Image interface does not support this,
// I can never understand.
type copyable interface {
image.Image
Set(x, y int, clr color.Color)
}
func copyImg(src, dst copyable) image.Image {
var x, y int
sb := src.Bounds()
for y = 0; y < sb.Dy(); y++ {
for x = 0; x < sb.Dx(); x++ {
dst.Set(x, y, src.At(x, y))
}
}
return dst
}