Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

srgb support #1018

Merged
merged 4 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions core/image/astc/astc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,48 +91,50 @@ func NewSRGB8_ALPHA8_12x12(name string) *image.Format { return image.NewASTC(nam
func init() {
C.init_astc()

fmts := []*image.Format{
RGBA_4x4,
RGBA_5x4,
RGBA_5x5,
RGBA_6x5,
RGBA_6x6,
RGBA_8x5,
RGBA_8x6,
RGBA_8x8,
RGBA_10x5,
RGBA_10x6,
RGBA_10x8,
RGBA_10x10,
RGBA_12x10,
RGBA_12x12,
SRGB8_ALPHA8_4x4,
SRGB8_ALPHA8_5x4,
SRGB8_ALPHA8_5x5,
SRGB8_ALPHA8_6x5,
SRGB8_ALPHA8_6x6,
SRGB8_ALPHA8_8x5,
SRGB8_ALPHA8_8x6,
SRGB8_ALPHA8_8x8,
SRGB8_ALPHA8_10x5,
SRGB8_ALPHA8_10x6,
SRGB8_ALPHA8_10x8,
SRGB8_ALPHA8_10x10,
SRGB8_ALPHA8_12x10,
SRGB8_ALPHA8_12x12,
}
for _, f := range fmts {
for _, f := range []struct {
src *image.Format
dst *image.Format
}{
{RGBA_4x4, image.RGBA_U8_NORM},
{RGBA_5x4, image.RGBA_U8_NORM},
{RGBA_5x5, image.RGBA_U8_NORM},
{RGBA_6x5, image.RGBA_U8_NORM},
{RGBA_6x6, image.RGBA_U8_NORM},
{RGBA_8x5, image.RGBA_U8_NORM},
{RGBA_8x6, image.RGBA_U8_NORM},
{RGBA_8x8, image.RGBA_U8_NORM},
{RGBA_10x5, image.RGBA_U8_NORM},
{RGBA_10x6, image.RGBA_U8_NORM},
{RGBA_10x8, image.RGBA_U8_NORM},
{RGBA_10x10, image.RGBA_U8_NORM},
{RGBA_12x10, image.RGBA_U8_NORM},
{RGBA_12x12, image.RGBA_U8_NORM},
{SRGB8_ALPHA8_4x4, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_5x4, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_5x5, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_6x5, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_6x6, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_8x5, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_8x6, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_8x8, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_10x5, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_10x6, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_10x8, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_10x10, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_12x10, image.SRGBA_U8_NORM},
{SRGB8_ALPHA8_12x12, image.SRGBA_U8_NORM},
} {
f := f
image.RegisterConverter(f, image.RGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
image.RegisterConverter(f.src, f.dst, func(src []byte, w, h, d int) ([]byte, error) {
dst := make([]byte, w*h*d*4)
sliceSize := f.Size(w, h, 1)
sliceSize := f.src.Size(w, h, 1)
for z := 0; z < d; z++ {
dst, src := dst[z*w*h*4:], src[z*sliceSize:]
in := (unsafe.Pointer)(&src[0])
out := (unsafe.Pointer)(&dst[0])
blockW := f.GetAstc().BlockWidth
blockH := f.GetAstc().BlockHeight
C.decompress_astc( // TODO: sRGB.
blockW := f.src.GetAstc().BlockWidth
blockH := f.src.GetAstc().BlockHeight
C.decompress_astc(
(*C.uint8_t)(in),
(*C.uint8_t)(out),
(C.uint32_t)(w),
Expand Down
42 changes: 26 additions & 16 deletions core/image/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ type converter interface {
// If no direct converter has been registered to convert from srcFmt to dstFmt,
// then Convert may try converting via an intermediate format.
func Convert(data []byte, width, height, depth int, srcFmt, dstFmt *Format) ([]byte, error) {
out, err := convertDirect(data, width, height, depth, srcFmt, dstFmt)
if err != nil {
return nil, err
}
if out != nil {
return out, nil
}

// No direct conversion found. Try going via a common intermediate formats.
for _, via := range []*Format{
RGBA_U8_NORM, SRGBA_U8_NORM,
} {
if data, _ := convertDirect(data, width, height, depth, srcFmt, via); data != nil {
if data, _ := convertDirect(data, width, height, depth, via, dstFmt); data != nil {
return data, nil
}
}
}

return nil, fmt.Errorf("No converter registered that can convert from format '%s' to '%s'",
srcFmt, dstFmt)
}

func convertDirect(data []byte, width, height, depth int, srcFmt, dstFmt *Format) ([]byte, error) {
srcKey, dstKey := srcFmt.Key(), dstFmt.Key()
if srcKey == dstKey {
return data, nil // No conversion required.
Expand All @@ -78,24 +102,10 @@ func Convert(data []byte, width, height, depth int, srcFmt, dstFmt *Format) ([]b

// Check if the source format supports the converter interface.
if c, ok := protoutil.OneOf(srcFmt.Format).(converter); ok {
data, err := c.convert(data, width, height, depth, dstFmt)
if data != nil || err != nil {
return data, err
}
}

// No direct conversion found. Try going via RGBA_U8_NORM.
rgbaU8Key := RGBA_U8_NORM.Key()
if convA, found := registeredConverters[srcDstFmt{srcKey, rgbaU8Key}]; found {
if convB, found := registeredConverters[srcDstFmt{rgbaU8Key, dstKey}]; found {
if data, err := convA(data, width, height, depth); err != nil {
return convB(data, width, height, depth)
}
}
return c.convert(data, width, height, depth, dstFmt)
}

return nil, fmt.Errorf("No converter registered that can convert from format '%s' to '%s'",
srcFmt, dstFmt)
return nil, nil
}

// Resolve returns the byte array holding the converted image for the resolve
Expand Down
12 changes: 12 additions & 0 deletions core/image/etc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ func init() {
RegisterConverter(ETC2_RGBA_U8U8U8U1_NORM, RGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decodeETC(src, w, h, d, etcAlpha1Bit)
})
RegisterConverter(ETC2_SRGB_U8_NORM, SRGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decodeETC(src, w, h, d, etcAlphaNone)
})
RegisterConverter(ETC2_SRGBA_U8_NORM, SRGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decodeETC(src, w, h, d, etcAlpha8Bit)
})
RegisterConverter(ETC2_SRGBA_U8U8U8U1_NORM, SRGBA_U8_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decodeETC(src, w, h, d, etcAlpha1Bit)
})
RegisterConverter(ETC2_R_U11_NORM, R_U16_NORM, func(src []byte, w, h, d int) ([]byte, error) {
return decodeETCU11(src, w, h, d, 1)
})
Expand All @@ -217,6 +226,9 @@ func init() {
{ETC2_RGB_U8_NORM, RGB_U8_NORM},
{ETC2_RGBA_U8_NORM, RGBA_U8_NORM},
{ETC2_RGBA_U8U8U8U1_NORM, RGBA_U8_NORM},
{ETC2_SRGB_U8_NORM, SRGBA_U8_NORM},
{ETC2_SRGBA_U8_NORM, SRGBA_U8_NORM},
{ETC2_SRGBA_U8U8U8U1_NORM, SRGBA_U8_NORM},
{ETC2_R_U11_NORM, R_U16_NORM},
{ETC2_RG_U11_NORM, RG_U16_NORM},
{ETC2_R_S11_NORM, R_S16_NORM},
Expand Down
18 changes: 10 additions & 8 deletions core/image/uncompressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import (
)

var (
RGBA_F32 = newUncompressed(fmts.RGBA_F32)
RGB_U8_NORM = newUncompressed(fmts.RGB_U8_NORM)
RGBA_U8_NORM = newUncompressed(fmts.RGBA_U8_NORM)
R_U16_NORM = newUncompressed(fmts.R_U16_NORM)
RG_U16_NORM = newUncompressed(fmts.RG_U16_NORM)
R_S16_NORM = newUncompressed(fmts.R_S16_NORM)
RG_S16_NORM = newUncompressed(fmts.RG_S16_NORM)
D_U16_NORM = newUncompressed(fmts.D_U16_NORM)
RGBA_F32 = newUncompressed(fmts.RGBA_F32)
RGB_U8_NORM = newUncompressed(fmts.RGB_U8_NORM)
RGBA_U8_NORM = newUncompressed(fmts.RGBA_U8_NORM)
SRGB_U8_NORM = newUncompressed(fmts.SRGB_U8_NORM)
SRGBA_U8_NORM = newUncompressed(fmts.SRGBA_U8_NORM)
R_U16_NORM = newUncompressed(fmts.R_U16_NORM)
RG_U16_NORM = newUncompressed(fmts.RG_U16_NORM)
R_S16_NORM = newUncompressed(fmts.R_S16_NORM)
RG_S16_NORM = newUncompressed(fmts.RG_S16_NORM)
D_U16_NORM = newUncompressed(fmts.D_U16_NORM)
)

// newUncompressed returns a new uncompressed format containing with the default
Expand Down
1 change: 1 addition & 0 deletions core/stream/CMakeFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(files
component.go
convert.go
convert_test.go
curve.go
datatype.go
doc.go
format.go
Expand Down
11 changes: 10 additions & 1 deletion core/stream/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package stream

import "fmt"
import (
"fmt"

"github.com/golang/protobuf/proto"
)

// Format prints the Component to f.
func (c Component) Format(f fmt.State, r rune) {
Expand All @@ -35,3 +39,8 @@ func (c *Component) IsNormalized() bool {
}
return false
}

// Clone returns a deep copy of c.
func (c *Component) Clone() *Component {
return proto.Clone(c).(*Component)
}
13 changes: 12 additions & 1 deletion core/stream/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type buf struct {
stride uint32 // in bits
}

func (b buf) clone() buf {
b.component = b.component.Clone()
return b
}

type mapping struct {
dst, src buf
}
Expand All @@ -42,7 +47,6 @@ type mapping struct {
// Certain components found in dst that are not in src are filled with default
// values (Y=0, Z=0, W=1, Alpha=1).
// Component order and datatypes can be changed.
// TODO: Deal with curves.
func Convert(dst, src *Format, data []byte) ([]byte, error) {
if dst == src || reflect.DeepEqual(dst, src) {
return data, nil
Expand All @@ -68,6 +72,10 @@ func Convert(dst, src *Format, data []byte) ([]byte, error) {
m.src = buf{data, s, srcOffsets[s], uint32(srcStride) * 8}
}
dstOffset += d.DataType.Bits()

if err := m.convertCurve(count); err != nil {
return nil, err
}
}

if src.HasComponent(Channel_SharedExponent) && !dst.HasComponent(Channel_SharedExponent) {
Expand Down Expand Up @@ -141,6 +149,9 @@ var (

func (m *mapping) conv(count int) error {
d, s := m.dst.component, m.src.component
if d.GetSampling().GetCurve() != s.GetSampling().GetCurve() {
return fmt.Errorf("Cannot convert curve from %v to %v", s.GetSampling().GetCurve(), d.GetSampling().GetCurve())
}
dstIsInt, srcIsInt := d.DataType.IsInteger(), s.DataType.IsInteger()
dstIsFloat, srcIsFloat := d.DataType.IsFloat(), s.DataType.IsFloat()
switch {
Expand Down
33 changes: 33 additions & 0 deletions core/stream/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,36 @@ func TestSharedExp(t *testing.T) {
},
})
}

func TestSRGB(t *testing.T) {
// These are split into two as rounding makes the numbers asymetrical.
// TODO: Try and reduce the rounding errors.
convertTests{
{SRGB_U8_NORM, RGB_U8_NORM},
}.run(t, map[*stream.Format][]byte{
RGB_U8_NORM: []byte{
0x00, 0x00, 0x00,
0xff, 0xff, 0xff,
0x10, 0x50, 0x90,
},
SRGB_U8_NORM: []byte{
0x00, 0x00, 0x00,
0xfe, 0xfe, 0xfe,
0x46, 0x97, 0xC5,
},
})
convertTests{
{RGB_U8_NORM, SRGB_U8_NORM},
}.run(t, map[*stream.Format][]byte{
RGB_U8_NORM: []byte{
0x00, 0x00, 0x00,
0xff, 0xff, 0xff,
0x0f, 0x4e, 0x8e,
},
SRGB_U8_NORM: []byte{
0x00, 0x00, 0x00,
0xff, 0xff, 0xff,
0x46, 0x97, 0xC5,
},
})
}
90 changes: 90 additions & 0 deletions core/stream/curve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (C) 2017 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 stream

import (
"bytes"
"fmt"
"math"

"github.com/google/gapid/core/data/endian"
"github.com/google/gapid/core/os/device"
)

func (m *mapping) transform(count int, f func(float64) float64) error {
data := make([]byte, 8*count)
tmp := mapping{
src: m.src,
dst: buf{
bytes: data,
component: &Component{
DataType: &F64,
Sampling: m.src.component.GetSampling(),
Channel: m.src.component.GetChannel(),
},
stride: 64,
},
}
if err := tmp.conv(count); err != nil {
return err
}
r := endian.Reader(bytes.NewReader(data), device.LittleEndian)
w := endian.Writer(bytes.NewBuffer(data[:0]), device.LittleEndian)
for i := 0; i < count; i++ {
w.Float64(f(r.Float64()))
}
m.src = tmp.dst
return nil
}

func (m *mapping) convertCurve(count int) error {
src := m.src.component.GetSampling().GetCurve()
dst := m.dst.component.GetSampling().GetCurve()

switch {
case src == dst:
return nil

case src == Curve_sRGB && dst == Curve_Linear:
if err := m.transform(count, func(v float64) float64 {
if v <= 0.04045 {
return v / 12.92
}
return math.Pow((v+0.055)/1.055, 2.4)
}); err != nil {
return err
}

m.src = m.src.clone()
m.src.component.Sampling.Curve = Curve_Linear
return nil

case src == Curve_Linear && dst == Curve_sRGB:
if err := m.transform(count, func(v float64) float64 {
if v <= 0.0031308 {
return v * 12.92
}
return 1.055*math.Pow(v, 1.0/2.4) - 0.055
}); err != nil {
return err
}

m.src = m.src.clone()
m.src.component.Sampling.Curve = Curve_sRGB
return nil
}

return fmt.Errorf("Cannot convert curve from %v to %v", src, dst)
}
Loading