-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgtc.go
210 lines (192 loc) · 6.24 KB
/
rgtc.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package image
import (
"github.com/google/gapid/core/data/binary"
"github.com/google/gapid/core/math/sint"
"github.com/google/gapid/core/stream"
)
var (
RGTC1_BC4_R_U8_NORM = NewRGTC1_BC4_R_U8_NORM("RGTC1_BC4_R_U8_NORM")
RGTC1_BC4_R_S8_NORM = NewRGTC1_BC4_R_S8_NORM("RGTC1_BC4_R_S8_NORM")
RGTC2_BC5_RG_U8_NORM = NewRGTC2_BC5_RG_U8_NORM("RGTC2_BC5_RG_U8_NORM")
RGTC2_BC5_RG_S8_NORM = NewRGTC2_BC5_RG_S8_NORM("RGTC2_BC5_RG_S8_NORM")
)
func init() {
RegisterConverter(RGTC1_BC4_R_U8_NORM, RGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decode4x4Blocks(src, w, h, d, func(r binary.Reader, dst []pixel) {
decodeRGTC(r, dst, false, false)
})
})
RegisterConverter(RGTC1_BC4_R_S8_NORM, RGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decode4x4Blocks(src, w, h, d, func(r binary.Reader, dst []pixel) {
decodeRGTC(r, dst, true, false)
})
})
RegisterConverter(RGTC2_BC5_RG_U8_NORM, RGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decode4x4Blocks(src, w, h, d, func(r binary.Reader, dst []pixel) {
decodeRGTC(r, dst, false, true)
})
})
RegisterConverter(RGTC2_BC5_RG_S8_NORM, RGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decode4x4Blocks(src, w, h, d, func(r binary.Reader, dst []pixel) {
decodeRGTC(r, dst, true, true)
})
})
}
// NewRGTC1_BC4_R_U8_NORM returns a format representing the RGTC1_BC4_R_U8_NORM
// block texture compression.
func NewRGTC1_BC4_R_U8_NORM(name string) *Format {
return &Format{Name: name, Format: &Format_Rgtc1Bc4RU8Norm{&FmtRGTC1_BC4_R_U8_NORM{}}}
}
func (f *FmtRGTC1_BC4_R_U8_NORM) key() interface{} {
return "RGTC1_BC4_U"
}
func (*FmtRGTC1_BC4_R_U8_NORM) size(w, h, d int) int {
return d * (sint.Max(sint.AlignUp(w, 4), 4) * sint.Max(sint.AlignUp(h, 4), 4)) / 2
}
func (f *FmtRGTC1_BC4_R_U8_NORM) check(data []byte, w, h, d int) error {
return checkSize(data, f, w, h, d)
}
func (*FmtRGTC1_BC4_R_U8_NORM) channels() stream.Channels {
return stream.Channels{stream.Channel_Red}
}
// NewRGTC1_BC4_R_S8_NORM returns a format representing the RGTC1_BC4_R_S8_NORM
// block texture compression.
func NewRGTC1_BC4_R_S8_NORM(name string) *Format {
return &Format{Name: name, Format: &Format_Rgtc1Bc4RS8Norm{&FmtRGTC1_BC4_R_S8_NORM{}}}
}
func (f *FmtRGTC1_BC4_R_S8_NORM) key() interface{} {
return "RGTC1_BC4_S"
}
func (*FmtRGTC1_BC4_R_S8_NORM) size(w, h, d int) int {
return d * (sint.Max(sint.AlignUp(w, 4), 4) * sint.Max(sint.AlignUp(h, 4), 4)) / 2
}
func (f *FmtRGTC1_BC4_R_S8_NORM) check(data []byte, w, h, d int) error {
return checkSize(data, f, w, h, d)
}
func (*FmtRGTC1_BC4_R_S8_NORM) channels() stream.Channels {
return stream.Channels{stream.Channel_Red}
}
// NewRGTC2_BC5_RG_U8_NORM returns a format representing the RGTC2_BC5_RG_U8_NORM
// block texture compression.
func NewRGTC2_BC5_RG_U8_NORM(name string) *Format {
return &Format{Name: name, Format: &Format_Rgtc2Bc5RgU8Norm{&FmtRGTC2_BC5_RG_U8_NORM{}}}
}
func (f *FmtRGTC2_BC5_RG_U8_NORM) key() interface{} {
return "RGTC2_BC5_U"
}
func (*FmtRGTC2_BC5_RG_U8_NORM) size(w, h, d int) int {
return d * (sint.Max(sint.AlignUp(w, 4), 4) * sint.Max(sint.AlignUp(h, 4), 4))
}
func (f *FmtRGTC2_BC5_RG_U8_NORM) check(data []byte, w, h, d int) error {
return checkSize(data, f, w, h, d)
}
func (*FmtRGTC2_BC5_RG_U8_NORM) channels() stream.Channels {
return stream.Channels{stream.Channel_Red, stream.Channel_Green}
}
// NewRGTC2_BC5_RG_S8_NORM returns a format representing the RGTC2_BC5_RG_S8_NORM
// block texture compression.
func NewRGTC2_BC5_RG_S8_NORM(name string) *Format {
return &Format{Name: name, Format: &Format_Rgtc2Bc5RgS8Norm{&FmtRGTC2_BC5_RG_S8_NORM{}}}
}
func (f *FmtRGTC2_BC5_RG_S8_NORM) key() interface{} {
return "RGTC2_BC5_S"
}
func (*FmtRGTC2_BC5_RG_S8_NORM) size(w, h, d int) int {
return d * (sint.Max(sint.AlignUp(w, 4), 4) * sint.Max(sint.AlignUp(h, 4), 4))
}
func (f *FmtRGTC2_BC5_RG_S8_NORM) check(data []byte, w, h, d int) error {
return checkSize(data, f, w, h, d)
}
func (*FmtRGTC2_BC5_RG_S8_NORM) channels() stream.Channels {
return stream.Channels{stream.Channel_Red, stream.Channel_Green}
}
func decodeRGTC(r binary.Reader, dst []pixel, signed bool, hasGreen bool) {
r0, r1, rc := r.Uint8(), r.Uint8(), uint64(r.Uint16())|(uint64(r.Uint32())<<16)
mixer := &rgtcMixer{
signed: signed,
r0: r0,
r1: r1,
rc: rc,
}
if hasGreen {
g0, g1, gc := r.Uint8(), r.Uint8(), uint64(r.Uint16())|(uint64(r.Uint32())<<16)
mixer.hasGreen, mixer.g0, mixer.g1, mixer.gc = hasGreen, g0, g1, gc
}
for i := 0; i < 16; i++ {
dst[i].r = mixer.red(i)
dst[i].g = mixer.green(i)
dst[i].b = 0
dst[i].a = 255
}
}
type rgtcMixer struct {
signed, hasGreen bool
r0, r1 uint8
g0, g1 uint8
rc, gc uint64
}
func (m *rgtcMixer) red(pos int) int {
return m.mix(m.r0, m.r1, m.rc, pos)
}
func (m *rgtcMixer) green(pos int) int {
if !m.hasGreen {
return 0
}
return m.mix(m.g0, m.g1, m.gc, pos)
}
func (m *rgtcMixer) mix(v0, v1 uint8, codes uint64, pos int) int {
codes = codes >> uint(pos*3)
if !m.signed {
return mixAlphaDXT5(int(v0), int(v1), codes)
}
return mixRGTCSigned(int8(v0), int8(v1), codes)
}
func mixRGTCSigned(v0, v1 int8, code uint64) int {
toFloat := func(i int8) float32 {
if int(i) > -128 {
return float32(int(i)) / 127.0
}
return -1.0
}
f0 := toFloat(v0)
f1 := toFloat(v1)
var f float32
c := code & 0x7
if f0 > f1 {
switch c {
case 0:
f = f0
case 1:
f = f1
default:
f = (f0*float32(8-c) + f1*float32(c-1)) / 7.0
}
} else {
switch c {
case 0:
f = f0
case 1:
f = f1
case 6:
f = -1.0
case 7:
f = 1.0
default:
f = (f0*float32(6-c) + f1*float32(c-1)) / 5.0
}
}
return int((f + 1.0) * 255.0 / 2.0)
}