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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"image"
"image/draw"
"image/png"
"log"
"net/http"
"os"
"path"
"sort"
"strings"
"sync"
"go.skia.org/infra/golden/go/search"
)
const (
min_png = "min.png"
max_png = "max.png"
)
type ExportTestRecordArray []search.ExportTestRecord
func (a ExportTestRecordArray) Len() int { return len(a) }
func (a ExportTestRecordArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ExportTestRecordArray) Less(i, j int) bool { return a[i].TestName < a[j].TestName }
func in(v string, a []string) bool {
for _, u := range a {
if u == v {
return true
}
}
return false
}
func processTest(testName string, imgUrls []string, output string) error {
if strings.ContainsRune(testName, '/') {
return nil
}
output_directory := path.Join(output, testName)
var img_max image.NRGBA
var img_min image.NRGBA
for _, url := range imgUrls {
resp, err := http.Get(url)
if err != nil {
return err
}
img, err := png.Decode(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
if img_max.Rect.Max.X == 0 {
// N.B. img_max.Pix may alias img.Pix (if they're already NRGBA).
img_max = toNrgba(img)
img_min = copyNrgba(img_max)
continue
}
w := img.Bounds().Max.X - img.Bounds().Min.X
h := img.Bounds().Max.Y - img.Bounds().Min.Y
if img_max.Rect.Max.X != w || img_max.Rect.Max.Y != h {
return errors.New("size mismatch")
}
img_nrgba := toNrgba(img)
for i, value := range img_nrgba.Pix {
if value > img_max.Pix[i] {
img_max.Pix[i] = value
} else if value < img_min.Pix[i] {
img_min.Pix[i] = value
}
}
}
if img_max.Rect.Max.X == 0 {
return nil
}
if err := os.Mkdir(output_directory, os.ModePerm); err != nil && !os.IsExist(err) {
return err
}
if err := writePngToFile(path.Join(output_directory, min_png), &img_min); err != nil {
return err
}
if err := writePngToFile(path.Join(output_directory, max_png), &img_max); err != nil {
return err
}
return nil
}
func readMetaJsonFile(filename string) ([]search.ExportTestRecord, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
dec := json.NewDecoder(file)
var records []search.ExportTestRecord
err = dec.Decode(&records)
return records, err
}
func writePngToFile(path string, img image.Image) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return png.Encode(file, img)
}
// to_nrgb() may return a shallow copy of img if it's already NRGBA.
func toNrgba(img image.Image) image.NRGBA {
switch v := img.(type) {
case *image.NRGBA:
return *v
}
nimg := *image.NewNRGBA(img.Bounds())
draw.Draw(&nimg, img.Bounds(), img, image.Point{0, 0}, draw.Src)
return nimg
}
func copyNrgba(src image.NRGBA) image.NRGBA {
dst := image.NRGBA{make([]uint8, len(src.Pix)), src.Stride, src.Rect}
copy(dst.Pix, src.Pix)
return dst
}
func main() {
if len(os.Args) != 3 {
log.Printf("Usage:\n %s INPUT.json OUTPUT_DIRECTORY\n\n", os.Args[0])
os.Exit(1)
}
input := os.Args[1]
output := os.Args[2]
// output is removed and replaced with a clean directory.
if err := os.RemoveAll(output); err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
if err := os.MkdirAll(output, os.ModePerm); err != nil && !os.IsExist(err) {
log.Fatal(err)
}
records, err := readMetaJsonFile(input)
if err != nil {
log.Fatal(err)
}
sort.Sort(ExportTestRecordArray(records))
var wg sync.WaitGroup
for _, record := range records {
var goodUrls []string
for _, digest := range record.Digests {
if (in("vk", digest.ParamSet["config"]) ||
in("gles", digest.ParamSet["config"])) &&
digest.Status == "positive" {
goodUrls = append(goodUrls, digest.URL)
}
}
wg.Add(1)
go func(testName string, imgUrls []string, output string) {
defer wg.Done()
if err := processTest(testName, imgUrls, output); err != nil {
log.Fatal(err)
}
fmt.Printf("\r%-60s", testName)
}(record.TestName, goodUrls, output)
}
wg.Wait()
fmt.Printf("\r%60s\n", "")
}
|