PNG  IHDR  8] PLTE S =tRNS   PNG  IHDR  8] PLTE S =tRNS   REDROOM
PHP 7.4.33
Preview: decode_test.go Size: 3.71 KB
/proc/thread-self/root/opt/golang/1.22.0/src/image/decode_test.go

// Copyright 2011 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 image_test

import (
	"bufio"
	"fmt"
	"image"
	"image/color"
	"os"
	"testing"

	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
)

type imageTest struct {
	goldenFilename string
	filename       string
	tolerance      int
}

var imageTests = []imageTest{
	{"testdata/video-001.png", "testdata/video-001.png", 0},
	// GIF images are restricted to a 256-color palette and the conversion
	// to GIF loses significant image quality.
	{"testdata/video-001.png", "testdata/video-001.gif", 64 << 8},
	{"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8},
	{"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},
	// JPEG is a lossy format and hence needs a non-zero tolerance.
	{"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8},
	{"testdata/video-001.png", "testdata/video-001.progressive.jpeg", 8 << 8},
	{"testdata/video-001.221212.png", "testdata/video-001.221212.jpeg", 8 << 8},
	{"testdata/video-001.cmyk.png", "testdata/video-001.cmyk.jpeg", 8 << 8},
	{"testdata/video-001.rgb.png", "testdata/video-001.rgb.jpeg", 8 << 8},
	{"testdata/video-001.progressive.truncated.png", "testdata/video-001.progressive.truncated.jpeg", 8 << 8},
	// Grayscale images.
	{"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
	{"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
}

func decode(filename string) (image.Image, string, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, "", err
	}
	defer f.Close()
	return image.Decode(bufio.NewReader(f))
}

func decodeConfig(filename string) (image.Config, string, error) {
	f, err := os.Open(filename)
	if err != nil {
		return image.Config{}, "", err
	}
	defer f.Close()
	return image.DecodeConfig(bufio.NewReader(f))
}

func delta(u0, u1 uint32) int {
	d := int(u0) - int(u1)
	if d < 0 {
		return -d
	}
	return d
}

func withinTolerance(c0, c1 color.Color, tolerance int) bool {
	r0, g0, b0, a0 := c0.RGBA()
	r1, g1, b1, a1 := c1.RGBA()
	r := delta(r0, r1)
	g := delta(g0, g1)
	b := delta(b0, b1)
	a := delta(a0, a1)
	return r <= tolerance && g <= tolerance && b <= tolerance && a <= tolerance
}

func TestDecode(t *testing.T) {
	rgba := func(c color.Color) string {
		r, g, b, a := c.RGBA()
		return fmt.Sprintf("rgba = 0x%04x, 0x%04x, 0x%04x, 0x%04x for %T%v", r, g, b, a, c, c)
	}

	golden := make(map[string]image.Image)
loop:
	for _, it := range imageTests {
		g := golden[it.goldenFilename]
		if g == nil {
			var err error
			g, _, err = decode(it.goldenFilename)
			if err != nil {
				t.Errorf("%s: %v", it.goldenFilename, err)
				continue loop
			}
			golden[it.goldenFilename] = g
		}
		m, imageFormat, err := decode(it.filename)
		if err != nil {
			t.Errorf("%s: %v", it.filename, err)
			continue loop
		}
		b := g.Bounds()
		if !b.Eq(m.Bounds()) {
			t.Errorf("%s: got bounds %v want %v", it.filename, m.Bounds(), b)
			continue loop
		}
		for y := b.Min.Y; y < b.Max.Y; y++ {
			for x := b.Min.X; x < b.Max.X; x++ {
				if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) {
					t.Errorf("%s: at (%d, %d):\ngot  %v\nwant %v",
						it.filename, x, y, rgba(m.At(x, y)), rgba(g.At(x, y)))
					continue loop
				}
			}
		}
		if imageFormat == "gif" {
			// Each frame of a GIF can have a frame-local palette override the
			// GIF-global palette. Thus, image.Decode can yield a different ColorModel
			// than image.DecodeConfig.
			continue
		}
		c, _, err := decodeConfig(it.filename)
		if err != nil {
			t.Errorf("%s: %v", it.filename, err)
			continue loop
		}
		if m.ColorModel() != c.ColorModel {
			t.Errorf("%s: color models differ", it.filename)
			continue loop
		}
	}
}

Directory Contents

Dirs: 7 × Files: 10

Name Size Perms Modified Actions
color DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
draw DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
gif DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
internal DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
jpeg DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
png DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
testdata DIR
- drwxr-xr-x 2024-02-02 18:09:55
Edit Download
7.50 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.71 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.03 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
7.31 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.03 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
34.87 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
10.84 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
1.46 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
8.67 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download
3.34 KB lrw-r--r-- 2024-02-02 18:09:55
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
 !"#$%&'(()*+,-./00123456789 t\ wIDATx ]ys  47Y ƒ -  "  Rv  < f{Ɛ $k l L > L  ~h^ 1  [  r G t& h  l F z3O Y ! p A(_g̷ E8 )S 8 c  Kb"z ~ 5 J xAL WU <  *  5 m;W a pB h ~P J 2 3 6 ҙ .Ƹ P i  4g F R L P ΪK/D  M v (a3 k J Œ4N5* SH ` SdJ z  O J Xՠ V>u ߱ BE&L b2 ?2` tX+  c CB A$ i b C ĀMB E : /  # Dx &l =q Ty  0 \p I ( L Ǎ { e 4k ;`u^ヲ eP!( d {  )T A 8 O;Ě n >;s6 !  :Nx `[S D HU ~ q›J F} a g*D 49 / pn k h (t 8NxƐF _!r չ7 ZR R׷ q/5") Ӎ NY 0 x sZ!   o  fu  ,  K"$ ? pg  㕣=  1» {h " fh7    y  } € +7  $ y " X —ą - G P u 4 m >J 5 L =V ' ^@I p ?MS xЌ XV P ! h "C NS9B8̢ ]!K  e   zA , ӏkbY  !< XQ ٿyS| *" f { w  4@[S <  # 0 ! js [m  =,~ o "ݎ DHf Wo $ g ! Vԅ t mB /y Wf V4񺍸 c+@x?  B ~u " xUN e 0 BĂ) ~J pz! 7y6]l Ԥ@ P a< O /DHC `≻  N m"$  0ObB }{ x AO FCG D R ^ "B  { WDH  UR l@ T #  +"d T ; 0 i  D}. 7 ` ' ] w rE &S i ƕiTD EL P _ u h $ Ա FG wVD G L R Zf ' .!] J /ZR oGЍs Mr Ĥ ʬ 3 Q [3 cL ` ^ p + ( F;# B 5 '  2Y f [  ϶R0e }  E 7 6M aۮ H <& n % L] E}Up x紉, Uw' Q  Ǯշo k ވۙ 0N94 VX5 xEDE l D #֤ } C o )W :  ^ s 9  bRf iX5u ཱི 4 :[  T 1. | [E 2ؽ Iy\ : o x K G 5 ylP ' uK E ftb/i[3 .g _  [3M n G, #NwQ5~  ؚ) | n =Ц"x qg gB ` 듘 ~ x w ? ? R~  _ u. &VQ K˻   H C ( TN˄+ `C dA nB׭ D 3"Z G ê ^k H_ /- ~ " R_  .8 Z_ 6@ o  xg  uP ? 3լ @7AM!  E7^ - =V L  x  g-D0  CtmW 7  O  G _ WD0 g C  w1 r d w : a | \  *" f nֳ ^ H# f L ` Z ۽hV  }S F r0Ù Bć5r] @! NL iQ]{s^=4 d  WD  "  "   M; t" 8 5 e dL| "-*st" ) SWD ?R[S e ooF  20.D ? bo =) A i o d ģ ZҰaO @E =) i D a &ܟa CϞ y6 ,<%{^x%{f8? `iw^ ?/ M * IEND B`