2009-10-08 00:14:23 -06:00
|
|
|
// Copyright 2009 The Go 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 png
|
|
|
|
|
|
|
|
import (
|
2011-05-29 18:55:37 -06:00
|
|
|
"bytes"
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
"image"
|
image: spin off a new color package out of the image package.
The spin-off renames some types. The new names are simply better:
image.Color -> color.Color
image.ColorModel -> color.Model
image.ColorModelFunc -> color.ModelFunc
image.PalettedColorModel -> color.Palette
image.RGBAColor -> color.RGBA
image.RGBAColorModel -> color.RGBAModel
image.RGBA64Color -> color.RGBA64
image.RGBA64ColorModel -> color.RGBA64Model
(similarly for NRGBAColor, GrayColorModel, etc)
The image.ColorImage type stays in the image package, but is renamed:
image.ColorImage -> image.Uniform
The image.Image implementations (image.RGBA, image.RGBA64, image.NRGBA,
image.Alpha, etc) do not change their name, and gain a nice symmetry:
an image.RGBA is an image of color.RGBA, etc.
The image.Black, image.Opaque uniform images remain unchanged (although
their type is renamed from image.ColorImage to image.Uniform). The
corresponding color types (color.Black, color.Opaque, etc) are new.
Nothing in the image/ycbcr is renamed yet. The ycbcr.YCbCrColor and
ycbcr.YCbCrImage types will eventually migrate to color.YCbCr and
image.YCbCr, but that will be a separate CL.
R=r, bsiegert
CC=golang-dev
https://golang.org/cl/5132048
2011-10-03 18:09:03 -06:00
|
|
|
"image/color"
|
2011-04-29 11:42:44 -06:00
|
|
|
"io/ioutil"
|
2009-12-15 16:35:38 -07:00
|
|
|
"testing"
|
2009-10-08 00:14:23 -06:00
|
|
|
)
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func diff(m0, m1 image.Image) error {
|
2010-08-09 20:08:52 -06:00
|
|
|
b0, b1 := m0.Bounds(), m1.Bounds()
|
2011-05-29 18:55:37 -06:00
|
|
|
if !b0.Size().Eq(b1.Size()) {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("dimensions differ: %v vs %v", b0, b1)
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
2011-05-29 18:55:37 -06:00
|
|
|
dx := b1.Min.X - b0.Min.X
|
|
|
|
dy := b1.Min.Y - b0.Min.Y
|
2010-08-09 20:08:52 -06:00
|
|
|
for y := b0.Min.Y; y < b0.Max.Y; y++ {
|
|
|
|
for x := b0.Min.X; x < b0.Max.X; x++ {
|
2011-05-29 18:55:37 -06:00
|
|
|
c0 := m0.At(x, y)
|
|
|
|
c1 := m1.At(x+dx, y+dy)
|
|
|
|
r0, g0, b0, a0 := c0.RGBA()
|
|
|
|
r1, g1, b1, a1 := c1.RGBA()
|
2009-10-08 00:14:23 -06:00
|
|
|
if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
|
2011-05-29 18:55:37 -06:00
|
|
|
return fmt.Errorf("colors differ at (%d, %d): %v vs %v", x, y, c0, c1)
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func encodeDecode(m image.Image) (image.Image, error) {
|
2012-02-05 20:09:00 -07:00
|
|
|
var b bytes.Buffer
|
|
|
|
err := Encode(&b, m)
|
2011-05-29 18:55:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-02-05 20:09:00 -07:00
|
|
|
m, err = Decode(&b)
|
2011-05-29 18:55:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2009-10-08 00:14:23 -06:00
|
|
|
func TestWriter(t *testing.T) {
|
|
|
|
// The filenames variable is declared in reader_test.go.
|
2011-03-26 12:25:22 -06:00
|
|
|
names := filenames
|
|
|
|
if testing.Short() {
|
|
|
|
names = filenamesShort
|
|
|
|
}
|
|
|
|
for _, fn := range names {
|
2009-12-15 16:35:38 -07:00
|
|
|
qfn := "testdata/pngsuite/" + fn + ".png"
|
2009-10-08 00:14:23 -06:00
|
|
|
// Read the image.
|
2011-09-07 11:23:16 -06:00
|
|
|
m0, err := readPNG(qfn)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
continue
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
2011-05-29 18:55:37 -06:00
|
|
|
// Read the image again, encode it, and decode it.
|
2011-09-07 11:23:16 -06:00
|
|
|
m1, err := readPNG(qfn)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
2011-05-29 18:55:37 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
m2, err := encodeDecode(m1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(fn, err)
|
|
|
|
return
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
// Compare the two.
|
2009-12-15 16:35:38 -07:00
|
|
|
err = diff(m0, m2)
|
2009-10-08 00:14:23 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Error(fn, err)
|
|
|
|
continue
|
2009-10-08 00:14:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 05:48:18 -06:00
|
|
|
|
2011-07-09 22:29:47 -06:00
|
|
|
func TestSubImage(t *testing.T) {
|
2011-09-14 05:39:49 -06:00
|
|
|
m0 := image.NewRGBA(image.Rect(0, 0, 256, 256))
|
2011-05-29 18:55:37 -06:00
|
|
|
for y := 0; y < 256; y++ {
|
|
|
|
for x := 0; x < 256; x++ {
|
image: spin off a new color package out of the image package.
The spin-off renames some types. The new names are simply better:
image.Color -> color.Color
image.ColorModel -> color.Model
image.ColorModelFunc -> color.ModelFunc
image.PalettedColorModel -> color.Palette
image.RGBAColor -> color.RGBA
image.RGBAColorModel -> color.RGBAModel
image.RGBA64Color -> color.RGBA64
image.RGBA64ColorModel -> color.RGBA64Model
(similarly for NRGBAColor, GrayColorModel, etc)
The image.ColorImage type stays in the image package, but is renamed:
image.ColorImage -> image.Uniform
The image.Image implementations (image.RGBA, image.RGBA64, image.NRGBA,
image.Alpha, etc) do not change their name, and gain a nice symmetry:
an image.RGBA is an image of color.RGBA, etc.
The image.Black, image.Opaque uniform images remain unchanged (although
their type is renamed from image.ColorImage to image.Uniform). The
corresponding color types (color.Black, color.Opaque, etc) are new.
Nothing in the image/ycbcr is renamed yet. The ycbcr.YCbCrColor and
ycbcr.YCbCrImage types will eventually migrate to color.YCbCr and
image.YCbCr, but that will be a separate CL.
R=r, bsiegert
CC=golang-dev
https://golang.org/cl/5132048
2011-10-03 18:09:03 -06:00
|
|
|
m0.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
|
2011-05-29 18:55:37 -06:00
|
|
|
}
|
|
|
|
}
|
2011-07-09 22:29:47 -06:00
|
|
|
m0 = m0.SubImage(image.Rect(50, 30, 250, 130)).(*image.RGBA)
|
2011-05-29 18:55:37 -06:00
|
|
|
m1, err := encodeDecode(m0)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = diff(m0, m1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-12 23:47:12 -06:00
|
|
|
func BenchmarkEncodeGray(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
img := image.NewGray(image.Rect(0, 0, 640, 480))
|
|
|
|
b.SetBytes(640 * 480 * 1)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncodeNRGBOpaque(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
|
|
|
|
// Set all pixels to 0xFF alpha to force opaque mode.
|
|
|
|
bo := img.Bounds()
|
|
|
|
for y := bo.Min.Y; y < bo.Max.Y; y++ {
|
|
|
|
for x := bo.Min.X; x < bo.Max.X; x++ {
|
|
|
|
img.Set(x, y, color.NRGBA{0, 0, 0, 255})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !img.Opaque() {
|
|
|
|
b.Fatal("expected image to be opaque")
|
|
|
|
}
|
|
|
|
b.SetBytes(640 * 480 * 4)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncodeNRGBA(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
img := image.NewNRGBA(image.Rect(0, 0, 640, 480))
|
|
|
|
if img.Opaque() {
|
|
|
|
b.Fatal("expected image not to be opaque")
|
|
|
|
}
|
|
|
|
b.SetBytes(640 * 480 * 4)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-27 05:48:18 -06:00
|
|
|
func BenchmarkEncodePaletted(b *testing.B) {
|
|
|
|
b.StopTimer()
|
image: spin off a new color package out of the image package.
The spin-off renames some types. The new names are simply better:
image.Color -> color.Color
image.ColorModel -> color.Model
image.ColorModelFunc -> color.ModelFunc
image.PalettedColorModel -> color.Palette
image.RGBAColor -> color.RGBA
image.RGBAColorModel -> color.RGBAModel
image.RGBA64Color -> color.RGBA64
image.RGBA64ColorModel -> color.RGBA64Model
(similarly for NRGBAColor, GrayColorModel, etc)
The image.ColorImage type stays in the image package, but is renamed:
image.ColorImage -> image.Uniform
The image.Image implementations (image.RGBA, image.RGBA64, image.NRGBA,
image.Alpha, etc) do not change their name, and gain a nice symmetry:
an image.RGBA is an image of color.RGBA, etc.
The image.Black, image.Opaque uniform images remain unchanged (although
their type is renamed from image.ColorImage to image.Uniform). The
corresponding color types (color.Black, color.Opaque, etc) are new.
Nothing in the image/ycbcr is renamed yet. The ycbcr.YCbCrColor and
ycbcr.YCbCrImage types will eventually migrate to color.YCbCr and
image.YCbCr, but that will be a separate CL.
R=r, bsiegert
CC=golang-dev
https://golang.org/cl/5132048
2011-10-03 18:09:03 -06:00
|
|
|
img := image.NewPaletted(image.Rect(0, 0, 640, 480), color.Palette{
|
|
|
|
color.RGBA{0, 0, 0, 255},
|
|
|
|
color.RGBA{255, 255, 255, 255},
|
|
|
|
})
|
2011-04-29 11:42:44 -06:00
|
|
|
b.SetBytes(640 * 480 * 1)
|
2010-10-27 05:48:18 -06:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2011-04-29 11:42:44 -06:00
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncodeRGBOpaque(b *testing.B) {
|
|
|
|
b.StopTimer()
|
2011-09-14 05:39:49 -06:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
2011-04-29 11:42:44 -06:00
|
|
|
// Set all pixels to 0xFF alpha to force opaque mode.
|
|
|
|
bo := img.Bounds()
|
|
|
|
for y := bo.Min.Y; y < bo.Max.Y; y++ {
|
|
|
|
for x := bo.Min.X; x < bo.Max.X; x++ {
|
image: spin off a new color package out of the image package.
The spin-off renames some types. The new names are simply better:
image.Color -> color.Color
image.ColorModel -> color.Model
image.ColorModelFunc -> color.ModelFunc
image.PalettedColorModel -> color.Palette
image.RGBAColor -> color.RGBA
image.RGBAColorModel -> color.RGBAModel
image.RGBA64Color -> color.RGBA64
image.RGBA64ColorModel -> color.RGBA64Model
(similarly for NRGBAColor, GrayColorModel, etc)
The image.ColorImage type stays in the image package, but is renamed:
image.ColorImage -> image.Uniform
The image.Image implementations (image.RGBA, image.RGBA64, image.NRGBA,
image.Alpha, etc) do not change their name, and gain a nice symmetry:
an image.RGBA is an image of color.RGBA, etc.
The image.Black, image.Opaque uniform images remain unchanged (although
their type is renamed from image.ColorImage to image.Uniform). The
corresponding color types (color.Black, color.Opaque, etc) are new.
Nothing in the image/ycbcr is renamed yet. The ycbcr.YCbCrColor and
ycbcr.YCbCrImage types will eventually migrate to color.YCbCr and
image.YCbCr, but that will be a separate CL.
R=r, bsiegert
CC=golang-dev
https://golang.org/cl/5132048
2011-10-03 18:09:03 -06:00
|
|
|
img.Set(x, y, color.RGBA{0, 0, 0, 255})
|
2011-04-29 11:42:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !img.Opaque() {
|
2011-12-20 11:36:25 -07:00
|
|
|
b.Fatal("expected image to be opaque")
|
2011-04-29 11:42:44 -06:00
|
|
|
}
|
|
|
|
b.SetBytes(640 * 480 * 4)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkEncodeRGBA(b *testing.B) {
|
|
|
|
b.StopTimer()
|
2011-09-14 05:39:49 -06:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
2011-04-29 11:42:44 -06:00
|
|
|
if img.Opaque() {
|
2012-09-12 23:47:12 -06:00
|
|
|
b.Fatal("expected image not to be opaque")
|
2011-04-29 11:42:44 -06:00
|
|
|
}
|
|
|
|
b.SetBytes(640 * 480 * 4)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Encode(ioutil.Discard, img)
|
2010-10-27 05:48:18 -06:00
|
|
|
}
|
|
|
|
}
|