Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libgo / go / image / jpeg / idct.go
index b387dfd..46fcaec 100644 (file)
@@ -37,6 +37,10 @@ package jpeg
  *
  */
 
+const blockSize = 64 // A DCT block is 8x8.
+
+type block [blockSize]int32
+
 const (
        w1 = 2841 // 2048*sqrt(2)*cos(1*pi/16)
        w2 = 2676 // 2048*sqrt(2)*cos(2*pi/16)
@@ -55,9 +59,7 @@ const (
        r2 = 181 // 256/sqrt(2)
 )
 
-// idct performs a 2-D Inverse Discrete Cosine Transformation, followed by a
-// +128 level shift and a clip to [0, 255], writing the results to dst.
-// stride is the number of elements between successive rows of dst.
+// idct performs a 2-D Inverse Discrete Cosine Transformation.
 //
 // The input coefficients should already have been multiplied by the
 // appropriate quantization table. We use fixed-point computation, with the
@@ -67,33 +69,34 @@ const (
 // For more on the actual algorithm, see Z. Wang, "Fast algorithms for the
 // discrete W transform and for the discrete Fourier transform", IEEE Trans. on
 // ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.
-func idct(dst []byte, stride int, src *block) {
+func idct(src *block) {
        // Horizontal 1-D IDCT.
        for y := 0; y < 8; y++ {
+               y8 := y * 8
                // If all the AC components are zero, then the IDCT is trivial.
-               if src[y*8+1] == 0 && src[y*8+2] == 0 && src[y*8+3] == 0 &&
-                       src[y*8+4] == 0 && src[y*8+5] == 0 && src[y*8+6] == 0 && src[y*8+7] == 0 {
-                       dc := src[y*8+0] << 3
-                       src[y*8+0] = dc
-                       src[y*8+1] = dc
-                       src[y*8+2] = dc
-                       src[y*8+3] = dc
-                       src[y*8+4] = dc
-                       src[y*8+5] = dc
-                       src[y*8+6] = dc
-                       src[y*8+7] = dc
+               if src[y8+1] == 0 && src[y8+2] == 0 && src[y8+3] == 0 &&
+                       src[y8+4] == 0 && src[y8+5] == 0 && src[y8+6] == 0 && src[y8+7] == 0 {
+                       dc := src[y8+0] << 3
+                       src[y8+0] = dc
+                       src[y8+1] = dc
+                       src[y8+2] = dc
+                       src[y8+3] = dc
+                       src[y8+4] = dc
+                       src[y8+5] = dc
+                       src[y8+6] = dc
+                       src[y8+7] = dc
                        continue
                }
 
                // Prescale.
-               x0 := (src[y*8+0] << 11) + 128
-               x1 := src[y*8+4] << 11
-               x2 := src[y*8+6]
-               x3 := src[y*8+2]
-               x4 := src[y*8+1]
-               x5 := src[y*8+7]
-               x6 := src[y*8+5]
-               x7 := src[y*8+3]
+               x0 := (src[y8+0] << 11) + 128
+               x1 := src[y8+4] << 11
+               x2 := src[y8+6]
+               x3 := src[y8+2]
+               x4 := src[y8+1]
+               x5 := src[y8+7]
+               x6 := src[y8+5]
+               x7 := src[y8+3]
 
                // Stage 1.
                x8 := w7 * (x4 + x5)
@@ -123,14 +126,14 @@ func idct(dst []byte, stride int, src *block) {
                x4 = (r2*(x4-x5) + 128) >> 8
 
                // Stage 4.
-               src[8*y+0] = (x7 + x1) >> 8
-               src[8*y+1] = (x3 + x2) >> 8
-               src[8*y+2] = (x0 + x4) >> 8
-               src[8*y+3] = (x8 + x6) >> 8
-               src[8*y+4] = (x8 - x6) >> 8
-               src[8*y+5] = (x0 - x4) >> 8
-               src[8*y+6] = (x3 - x2) >> 8
-               src[8*y+7] = (x7 - x1) >> 8
+               src[y8+0] = (x7 + x1) >> 8
+               src[y8+1] = (x3 + x2) >> 8
+               src[y8+2] = (x0 + x4) >> 8
+               src[y8+3] = (x8 + x6) >> 8
+               src[y8+4] = (x8 - x6) >> 8
+               src[y8+5] = (x0 - x4) >> 8
+               src[y8+6] = (x3 - x2) >> 8
+               src[y8+7] = (x7 - x1) >> 8
        }
 
        // Vertical 1-D IDCT.
@@ -186,19 +189,4 @@ func idct(dst []byte, stride int, src *block) {
                src[8*6+x] = (y3 - y2) >> 14
                src[8*7+x] = (y7 - y1) >> 14
        }
-
-       // Level shift by +128, clip to [0, 255], and write to dst.
-       for y := 0; y < 8; y++ {
-               for x := 0; x < 8; x++ {
-                       c := src[y*8+x]
-                       if c < -128 {
-                               c = 0
-                       } else if c > 127 {
-                               c = 255
-                       } else {
-                               c += 128
-                       }
-                       dst[y*stride+x] = uint8(c)
-               }
-       }
 }