aboutsummaryrefslogtreecommitdiff
path: root/image/jpeg/quality.go
blob: 674f0b86836830454b7a0332f40561f57638e809 (plain)
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
package jpeg

import (
	"fmt"
	"io"
)

var defaultTables = [][]int{
	{ // Luminance
		16, 11, 12, 14, 12, 10, 16, 14,
		13, 14, 18, 17, 16, 19, 24, 40,
		26, 24, 22, 22, 24, 49, 35, 37,
		29, 40, 58, 51, 61, 60, 57, 51,
		56, 55, 64, 72, 92, 78, 64, 68,
		87, 69, 55, 56, 80, 109, 81, 87,
		95, 98, 103, 104, 103, 62, 77, 113,
		121, 112, 100, 120, 92, 101, 103, 99,
	},
	{ // Chrominance
		17, 18, 18, 24, 21, 24, 47, 26,
		26, 47, 99, 66, 56, 66, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
		99, 99, 99, 99, 99, 99, 99, 99,
	},
}

func read2Bytes(r io.Reader) ([]byte, error) {
	b := make([]byte, 2)
	if _, err := r.Read(b); err != nil {
		return nil, err
	}
	return b, nil
}

// ReadJPEGQuality returns the JPEG quality estimate.
//
// Cleaned up version of github.com/liut/jpegquality
func ReadJPEGQuality(r io.ReadSeeker) (int, error) {
	sign, err := read2Bytes(r)
	if err != nil {
		return 0, err
	}

	if sign[0] != 0xff && sign[1] != 0xd8 {
		return 0, fmt.Errorf("invalid jpeg header")
	}

	for {
		mark, err := read2Bytes(r)
		if err != nil {
			return 0, err
		}

		if mark[0] != 0xff || mark[1] == 0xff || mark[1] == 0x00 {
			// Would also be valid to just keep re-reading to find the marker
			return 0, fmt.Errorf("invalid marker")
		}

		marker := int(mark[0])<<8 + int(mark[1])
		if marker == 0 {
			return 0, fmt.Errorf("invalid jpeg header")
		}

		sign, err := read2Bytes(r)
		if err != nil {
			return 0, err
		}

		length := int(sign[0])<<8 + int(sign[1]) - 2
		if length < 0 {
			return 0, fmt.Errorf("short segment read")
		}

		if (marker & 0xff) != 0xdb { // not a quantization table
			if _, err := r.Seek(int64(length), 1); err != nil {
				return 0, err
			}
			continue
		}

		if length%65 != 0 {
			return 0, fmt.Errorf("wrong size for quantization table")
		}

		tabuf := make([]byte, length)
		n, err := r.Read(tabuf)
		if err != nil {
			return 0, err
		}
		tabuf = tabuf[0:n]

		allones := 1

		var reftable []int
		var cumsf, cumsf2 float64

		for a := 0; a < n; {
			tableindex := int(tabuf[a] & 0x0f)
			a++

			if tableindex < 2 {
				reftable = defaultTables[tableindex]
			}

			// Read in the table, compute statistics relative to reference table
			if a+64 > n {
				return 0, fmt.Errorf("DQT segment too short")
			}

			for coefindex := 0; coefindex < 64 && a < n; coefindex++ {
				var val int

				if tableindex>>4 != 0 {
					temp := int(tabuf[a])
					a++
					temp *= 256
					val = int(tabuf[a]) + temp
					a++
				} else {
					val = int(tabuf[a])
					a++
				}

				// scaling factor in percent
				x := 100.0 * float64(val) / float64(reftable[coefindex])
				cumsf += x
				cumsf2 += x * x

				// separate check for all-ones table (Q 100)
				if val != 1 {
					allones = 0
				}
			}

			if 0 != len(reftable) { // terse output includes quality
				var qual float64
				cumsf /= 64.0 // mean scale factor
				cumsf2 /= 64.0

				if allones == 1 { // special case for all-ones table
					qual = 100.0
				} else if cumsf <= 100.0 {
					qual = (200.0 - cumsf) / 2.0
				} else {
					qual = 5000.0 / cumsf
				}

				if tableindex == 0 {
					return (int)(qual + 0.5), nil
				}
			}
		}
	}
}