File: quant_test.go

package info (click to toggle)
golang-github-soniakeys-quant 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: makefile: 2
file content (106 lines) | stat: -rw-r--r-- 3,236 bytes parent folder | download
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
package quant_test

import (
	"fmt"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"os"
	"path/filepath"
	"runtime"
	"testing"

	"github.com/soniakeys/quant"
	"github.com/soniakeys/quant/median"
)

// TestDither tests Sierra24A on png files found in the source directory.
// Output files are prefixed with _dither_256_.  Files beginning with _
// are skipped when scanning for input files.  Thus nothing is tested
// with a fresh source tree--drop a png or two in the source directory
// before testing to give the test something to work on.
func TestDitherMedianDraw(t *testing.T) {
	_, file, _, _ := runtime.Caller(0)
	srcDir, _ := filepath.Split(file)
	// ignore file names starting with _, those are result files.
	imgs, err := filepath.Glob(srcDir + "[^_]*.png")
	if err != nil {
		t.Fatal(err)
	}
	const n = 256
	// exercise draw.Quantizer interface
	var q draw.Quantizer = median.Quantizer(n)
	// exercise draw.Drawer interface
	var d draw.Drawer = quant.Sierra24A{}
	for _, p := range imgs {
		f, err := os.Open(p)
		if err != nil {
			t.Error(err) // skip files that can't be opened
			continue
		}
		img, err := png.Decode(f)
		f.Close()
		if err != nil {
			t.Error(err) // skip files that can't be decoded
			continue
		}
		pDir, pFile := filepath.Split(p)
		// prefix _ on file name marks this as a result
		fq, err := os.Create(fmt.Sprintf("%s_dither_median_draw_%d_%s", pDir, n, pFile))
		if err != nil {
			t.Fatal(err) // probably can't create any others
		}
		b := img.Bounds()
		pi := image.NewPaletted(b, q.Quantize(make(color.Palette, 0, n), img))
		d.Draw(pi, b, img, b.Min)
		if err = png.Encode(fq, pi); err != nil {
			t.Fatal(err) // any problem is probably a problem for all
		}
	}
}

// TestDither tests Sierra24A on png files found in the source directory.
// Output files are prefixed with _dither_256_.  Files beginning with _
// are skipped when scanning for input files.  Thus nothing is tested
// with a fresh source tree--drop a png or two in the source directory
// before testing to give the test something to work on.
func TestDitherMedianPalette(t *testing.T) {
	_, file, _, _ := runtime.Caller(0)
	srcDir, _ := filepath.Split(file)
	// ignore file names starting with _, those are result files.
	imgs, err := filepath.Glob(srcDir + "[^_]*.png")
	if err != nil {
		t.Fatal(err)
	}
	const n = 256
	// exercise draw.Quantizer interface
	var q draw.Quantizer = median.Quantizer(n)
	// exercise draw.Drawer interface
	var d draw.Drawer = quant.Sierra24A{}
	for _, p := range imgs {
		f, err := os.Open(p)
		if err != nil {
			t.Error(err) // skip files that can't be opened
			continue
		}
		img, err := png.Decode(f)
		f.Close()
		if err != nil {
			t.Error(err) // skip files that can't be decoded
			continue
		}
		pDir, pFile := filepath.Split(p)
		// prefix _ on file name marks this as a result
		fq, err := os.Create(fmt.Sprintf("%s_dither_median_palette_%d_%s", pDir, n, pFile))
		if err != nil {
			t.Fatal(err) // probably can't create any others
		}
		b := img.Bounds()
		pi := image.NewPaletted(b, q.Quantize(make(color.Palette, 0, n), img))
		d.Draw(pi, b, img, b.Min)
		if err = png.Encode(fq, pi); err != nil {
			t.Fatal(err) // any problem is probably a problem for all
		}
	}
}