-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathprint-thumbhash-tables.go
144 lines (125 loc) · 2.79 KB
/
print-thumbhash-tables.go
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
// Copyright 2024 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build ignore
// +build ignore
package main
// print-thumbhash-tables.go prints the std/thumbhash tables.
//
// Usage: go run print-thumbhash-tables.go
import (
"fmt"
"math"
"os"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
printLxLy()
fmt.Println()
printFrom4Bits(1.00)
fmt.Println()
printFrom4Bits(1.25)
fmt.Println()
printCosines()
return nil
}
func printLxLy() {
for bits := 0; bits < 16; bits++ {
lCount := bits & 7
if lCount < 3 {
continue
} else if lCount == 3 {
fmt.Println()
}
hasAlpha := (bits >> 3) & 1
isLandscape := (bits >> 4) & 1
lx := lCount
ly := 7
if hasAlpha != 0 {
ly = 5
}
if isLandscape != 0 {
lx, ly = ly, lx
}
ratio := float64(lx) / float64(ly)
w, h := 32, 32
if ratio > 1 {
h = int(math.Round(32 / ratio))
} else {
w = int(math.Round(32 * ratio))
}
lenlac := 0
for cy := 0; cy < ly; cy++ {
for cx := ifElse(cy, 0, 1); (cx * ly) < (lx * (ly - cy)); cx++ {
lenlac++
}
}
lenpac := 0
for cy := 0; cy < 3; cy++ {
for cx := ifElse(cy, 0, 1); (cx * 3) < (3 * (3 - cy)); cx++ {
lenpac++
}
}
lenaac := 0
if hasAlpha != 0 {
for cy := 0; cy < 5; cy++ {
for cx := ifElse(cy, 0, 1); (cx * 5) < (5 * (5 - cy)); cx++ {
lenaac++
}
}
}
fileSize := 5 + hasAlpha + (lenlac+lenpac+lenpac+lenaac+1)/2
fmt.Printf("l_count: %d lx / ly = %d / %d w = %2d h = %2d #lac = %2d #pac = #qac = %d #aac = %2d file_size = 3 + %2d\n",
lCount, lx, ly, w, h, lenlac, lenpac, lenaac, fileSize)
}
}
func printFrom4Bits(scale float64) {
for i := 0; i < 16; i++ {
x := (float64(i)/7.5 - 1) * scale
y := (x * (1 << 14))
fmt.Printf("0x%04X, // %s%0.3f\n", 0xFFFF&int(math.Round(y)), plusSign(y), x)
}
}
func printCosines() {
widths := []int{
0, 14, 18, 19, 23, 26, 27, 32,
}
for _, w := range widths {
fmt.Printf("\n// w = %2d\n", w)
for x := 0; x < w; x++ {
fmt.Printf("[")
for cx := 1; cx < 7; cx++ {
u := math.Cos(math.Pi * float64(cx) * (float64(x) + 0.5) / float64(w))
v := u * (1 << 14)
fmt.Printf("0x%04X", 0xFFFF&int(math.Round(v)))
if cx < 6 {
fmt.Printf(",")
}
}
fmt.Printf("], // x = %2d\n", x)
}
}
}
func ifElse(c int, x int, y int) int {
if c != 0 {
return x
}
return y
}
func plusSign(x float64) string {
if x >= 0 {
return "+"
}
return ""
}