~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

Viewing changes to src/pkg/image/jpeg/idct.go

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 *
38
38
 */
39
39
 
 
40
const blockSize = 64 // A DCT block is 8x8.
 
41
 
 
42
type block [blockSize]int32
 
43
 
40
44
const (
41
45
        w1 = 2841 // 2048*sqrt(2)*cos(1*pi/16)
42
46
        w2 = 2676 // 2048*sqrt(2)*cos(2*pi/16)
55
59
        r2 = 181 // 256/sqrt(2)
56
60
)
57
61
 
58
 
// idct performs a 2-D Inverse Discrete Cosine Transformation, followed by a
59
 
// +128 level shift and a clip to [0, 255], writing the results to dst.
60
 
// stride is the number of elements between successive rows of dst.
 
62
// idct performs a 2-D Inverse Discrete Cosine Transformation.
61
63
//
62
64
// The input coefficients should already have been multiplied by the
63
65
// appropriate quantization table. We use fixed-point computation, with the
67
69
// For more on the actual algorithm, see Z. Wang, "Fast algorithms for the
68
70
// discrete W transform and for the discrete Fourier transform", IEEE Trans. on
69
71
// ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.
70
 
func idct(dst []byte, stride int, src *block) {
 
72
func idct(src *block) {
71
73
        // Horizontal 1-D IDCT.
72
74
        for y := 0; y < 8; y++ {
 
75
                y8 := y * 8
73
76
                // If all the AC components are zero, then the IDCT is trivial.
74
 
                if src[y*8+1] == 0 && src[y*8+2] == 0 && src[y*8+3] == 0 &&
75
 
                        src[y*8+4] == 0 && src[y*8+5] == 0 && src[y*8+6] == 0 && src[y*8+7] == 0 {
76
 
                        dc := src[y*8+0] << 3
77
 
                        src[y*8+0] = dc
78
 
                        src[y*8+1] = dc
79
 
                        src[y*8+2] = dc
80
 
                        src[y*8+3] = dc
81
 
                        src[y*8+4] = dc
82
 
                        src[y*8+5] = dc
83
 
                        src[y*8+6] = dc
84
 
                        src[y*8+7] = dc
 
77
                if src[y8+1] == 0 && src[y8+2] == 0 && src[y8+3] == 0 &&
 
78
                        src[y8+4] == 0 && src[y8+5] == 0 && src[y8+6] == 0 && src[y8+7] == 0 {
 
79
                        dc := src[y8+0] << 3
 
80
                        src[y8+0] = dc
 
81
                        src[y8+1] = dc
 
82
                        src[y8+2] = dc
 
83
                        src[y8+3] = dc
 
84
                        src[y8+4] = dc
 
85
                        src[y8+5] = dc
 
86
                        src[y8+6] = dc
 
87
                        src[y8+7] = dc
85
88
                        continue
86
89
                }
87
90
 
88
91
                // Prescale.
89
 
                x0 := (src[y*8+0] << 11) + 128
90
 
                x1 := src[y*8+4] << 11
91
 
                x2 := src[y*8+6]
92
 
                x3 := src[y*8+2]
93
 
                x4 := src[y*8+1]
94
 
                x5 := src[y*8+7]
95
 
                x6 := src[y*8+5]
96
 
                x7 := src[y*8+3]
 
92
                x0 := (src[y8+0] << 11) + 128
 
93
                x1 := src[y8+4] << 11
 
94
                x2 := src[y8+6]
 
95
                x3 := src[y8+2]
 
96
                x4 := src[y8+1]
 
97
                x5 := src[y8+7]
 
98
                x6 := src[y8+5]
 
99
                x7 := src[y8+3]
97
100
 
98
101
                // Stage 1.
99
102
                x8 := w7 * (x4 + x5)
123
126
                x4 = (r2*(x4-x5) + 128) >> 8
124
127
 
125
128
                // Stage 4.
126
 
                src[8*y+0] = (x7 + x1) >> 8
127
 
                src[8*y+1] = (x3 + x2) >> 8
128
 
                src[8*y+2] = (x0 + x4) >> 8
129
 
                src[8*y+3] = (x8 + x6) >> 8
130
 
                src[8*y+4] = (x8 - x6) >> 8
131
 
                src[8*y+5] = (x0 - x4) >> 8
132
 
                src[8*y+6] = (x3 - x2) >> 8
133
 
                src[8*y+7] = (x7 - x1) >> 8
 
129
                src[y8+0] = (x7 + x1) >> 8
 
130
                src[y8+1] = (x3 + x2) >> 8
 
131
                src[y8+2] = (x0 + x4) >> 8
 
132
                src[y8+3] = (x8 + x6) >> 8
 
133
                src[y8+4] = (x8 - x6) >> 8
 
134
                src[y8+5] = (x0 - x4) >> 8
 
135
                src[y8+6] = (x3 - x2) >> 8
 
136
                src[y8+7] = (x7 - x1) >> 8
134
137
        }
135
138
 
136
139
        // Vertical 1-D IDCT.
186
189
                src[8*6+x] = (y3 - y2) >> 14
187
190
                src[8*7+x] = (y7 - y1) >> 14
188
191
        }
189
 
 
190
 
        // Level shift by +128, clip to [0, 255], and write to dst.
191
 
        for y := 0; y < 8; y++ {
192
 
                for x := 0; x < 8; x++ {
193
 
                        c := src[y*8+x]
194
 
                        if c < -128 {
195
 
                                c = 0
196
 
                        } else if c > 127 {
197
 
                                c = 255
198
 
                        } else {
199
 
                                c += 128
200
 
                        }
201
 
                        dst[y*stride+x] = uint8(c)
202
 
                }
203
 
        }
204
192
}