blob: b810f2815f8c2146a724cae713eb88ae86e649c1 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// 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 (
"bytes"
"android.googlesource.com/platform/tools/gpu/framework/binary"
"android.googlesource.com/platform/tools/gpu/framework/binary/endian"
"android.googlesource.com/platform/tools/gpu/framework/device"
)
type fmtRGBF32 struct{ binary.Generate }
func (f *fmtRGBF32) Key() interface{} { return *f }
func (*fmtRGBF32) String() string { return "RGBF32" }
func (*fmtRGBF32) Size(w, h int) int { return w * h * 3 * 4 }
func (*fmtRGBF32) Check(d []byte, w, h int) error { return checkSize(d, w, h, 96) }
// RGBF32 returns a format containing a red, green and blue 32-bit
// floating point channel per pixel.
func RGBF32() *fmtRGBF32 { return &fmtRGBF32{} }
func init() {
RegisterConverter(RGBF32(), RGBA(),
func(src []byte, width, height int) ([]byte, error) {
r := endian.Reader(bytes.NewBuffer(src), device.LittleEndian)
dst, i := make([]byte, width*height*4), 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
dst[i+0] = f32ToByte(r.Float32())
dst[i+1] = f32ToByte(r.Float32())
dst[i+2] = f32ToByte(r.Float32())
dst[i+3] = 255
i += 4
}
}
return dst, nil
})
RegisterConverter(RGBA(), RGBF32(),
func(src []byte, width, height int) ([]byte, error) {
dst := make([]byte, width*height*3*4)
w, i := endian.Writer(bytes.NewBuffer(dst[:0]), device.LittleEndian), 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
w.Float32(float32(src[i+0]) / 0xff)
w.Float32(float32(src[i+1]) / 0xff)
w.Float32(float32(src[i+2]) / 0xff)
i += 4
}
}
return dst, nil
})
}