...

Source file src/image/jpeg/idct.go

Documentation: image/jpeg

		 1  // Copyright 2009 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package jpeg
		 6  
		 7  // This is a Go translation of idct.c from
		 8  //
		 9  // http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_IEC_13818-4_2004_Conformance_Testing/Video/verifier/mpeg2decode_960109.tar.gz
		10  //
		11  // which carries the following notice:
		12  
		13  /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
		14  
		15  /*
		16   * Disclaimer of Warranty
		17   *
		18   * These software programs are available to the user without any license fee or
		19   * royalty on an "as is" basis.	The MPEG Software Simulation Group disclaims
		20   * any and all warranties, whether express, implied, or statuary, including any
		21   * implied warranties or merchantability or of fitness for a particular
		22   * purpose.	In no event shall the copyright-holder be liable for any
		23   * incidental, punitive, or consequential damages of any kind whatsoever
		24   * arising from the use of these programs.
		25   *
		26   * This disclaimer of warranty extends to the user of these programs and user's
		27   * customers, employees, agents, transferees, successors, and assigns.
		28   *
		29   * The MPEG Software Simulation Group does not represent or warrant that the
		30   * programs furnished hereunder are free of infringement of any third-party
		31   * patents.
		32   *
		33   * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
		34   * are subject to royalty fees to patent holders.	Many of these patents are
		35   * general enough such that they are unavoidable regardless of implementation
		36   * design.
		37   *
		38   */
		39  
		40  const blockSize = 64 // A DCT block is 8x8.
		41  
		42  type block [blockSize]int32
		43  
		44  const (
		45  	w1 = 2841 // 2048*sqrt(2)*cos(1*pi/16)
		46  	w2 = 2676 // 2048*sqrt(2)*cos(2*pi/16)
		47  	w3 = 2408 // 2048*sqrt(2)*cos(3*pi/16)
		48  	w5 = 1609 // 2048*sqrt(2)*cos(5*pi/16)
		49  	w6 = 1108 // 2048*sqrt(2)*cos(6*pi/16)
		50  	w7 = 565	// 2048*sqrt(2)*cos(7*pi/16)
		51  
		52  	w1pw7 = w1 + w7
		53  	w1mw7 = w1 - w7
		54  	w2pw6 = w2 + w6
		55  	w2mw6 = w2 - w6
		56  	w3pw5 = w3 + w5
		57  	w3mw5 = w3 - w5
		58  
		59  	r2 = 181 // 256/sqrt(2)
		60  )
		61  
		62  // idct performs a 2-D Inverse Discrete Cosine Transformation.
		63  //
		64  // The input coefficients should already have been multiplied by the
		65  // appropriate quantization table. We use fixed-point computation, with the
		66  // number of bits for the fractional component varying over the intermediate
		67  // stages.
		68  //
		69  // For more on the actual algorithm, see Z. Wang, "Fast algorithms for the
		70  // discrete W transform and for the discrete Fourier transform", IEEE Trans. on
		71  // ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.
		72  func idct(src *block) {
		73  	// Horizontal 1-D IDCT.
		74  	for y := 0; y < 8; y++ {
		75  		y8 := y * 8
		76  		s := src[y8 : y8+8 : y8+8] // Small cap improves performance, see https://golang.org/issue/27857
		77  		// If all the AC components are zero, then the IDCT is trivial.
		78  		if s[1] == 0 && s[2] == 0 && s[3] == 0 &&
		79  			s[4] == 0 && s[5] == 0 && s[6] == 0 && s[7] == 0 {
		80  			dc := s[0] << 3
		81  			s[0] = dc
		82  			s[1] = dc
		83  			s[2] = dc
		84  			s[3] = dc
		85  			s[4] = dc
		86  			s[5] = dc
		87  			s[6] = dc
		88  			s[7] = dc
		89  			continue
		90  		}
		91  
		92  		// Prescale.
		93  		x0 := (s[0] << 11) + 128
		94  		x1 := s[4] << 11
		95  		x2 := s[6]
		96  		x3 := s[2]
		97  		x4 := s[1]
		98  		x5 := s[7]
		99  		x6 := s[5]
	 100  		x7 := s[3]
	 101  
	 102  		// Stage 1.
	 103  		x8 := w7 * (x4 + x5)
	 104  		x4 = x8 + w1mw7*x4
	 105  		x5 = x8 - w1pw7*x5
	 106  		x8 = w3 * (x6 + x7)
	 107  		x6 = x8 - w3mw5*x6
	 108  		x7 = x8 - w3pw5*x7
	 109  
	 110  		// Stage 2.
	 111  		x8 = x0 + x1
	 112  		x0 -= x1
	 113  		x1 = w6 * (x3 + x2)
	 114  		x2 = x1 - w2pw6*x2
	 115  		x3 = x1 + w2mw6*x3
	 116  		x1 = x4 + x6
	 117  		x4 -= x6
	 118  		x6 = x5 + x7
	 119  		x5 -= x7
	 120  
	 121  		// Stage 3.
	 122  		x7 = x8 + x3
	 123  		x8 -= x3
	 124  		x3 = x0 + x2
	 125  		x0 -= x2
	 126  		x2 = (r2*(x4+x5) + 128) >> 8
	 127  		x4 = (r2*(x4-x5) + 128) >> 8
	 128  
	 129  		// Stage 4.
	 130  		s[0] = (x7 + x1) >> 8
	 131  		s[1] = (x3 + x2) >> 8
	 132  		s[2] = (x0 + x4) >> 8
	 133  		s[3] = (x8 + x6) >> 8
	 134  		s[4] = (x8 - x6) >> 8
	 135  		s[5] = (x0 - x4) >> 8
	 136  		s[6] = (x3 - x2) >> 8
	 137  		s[7] = (x7 - x1) >> 8
	 138  	}
	 139  
	 140  	// Vertical 1-D IDCT.
	 141  	for x := 0; x < 8; x++ {
	 142  		// Similar to the horizontal 1-D IDCT case, if all the AC components are zero, then the IDCT is trivial.
	 143  		// However, after performing the horizontal 1-D IDCT, there are typically non-zero AC components, so
	 144  		// we do not bother to check for the all-zero case.
	 145  		s := src[x : x+57 : x+57] // Small cap improves performance, see https://golang.org/issue/27857
	 146  
	 147  		// Prescale.
	 148  		y0 := (s[8*0] << 8) + 8192
	 149  		y1 := s[8*4] << 8
	 150  		y2 := s[8*6]
	 151  		y3 := s[8*2]
	 152  		y4 := s[8*1]
	 153  		y5 := s[8*7]
	 154  		y6 := s[8*5]
	 155  		y7 := s[8*3]
	 156  
	 157  		// Stage 1.
	 158  		y8 := w7*(y4+y5) + 4
	 159  		y4 = (y8 + w1mw7*y4) >> 3
	 160  		y5 = (y8 - w1pw7*y5) >> 3
	 161  		y8 = w3*(y6+y7) + 4
	 162  		y6 = (y8 - w3mw5*y6) >> 3
	 163  		y7 = (y8 - w3pw5*y7) >> 3
	 164  
	 165  		// Stage 2.
	 166  		y8 = y0 + y1
	 167  		y0 -= y1
	 168  		y1 = w6*(y3+y2) + 4
	 169  		y2 = (y1 - w2pw6*y2) >> 3
	 170  		y3 = (y1 + w2mw6*y3) >> 3
	 171  		y1 = y4 + y6
	 172  		y4 -= y6
	 173  		y6 = y5 + y7
	 174  		y5 -= y7
	 175  
	 176  		// Stage 3.
	 177  		y7 = y8 + y3
	 178  		y8 -= y3
	 179  		y3 = y0 + y2
	 180  		y0 -= y2
	 181  		y2 = (r2*(y4+y5) + 128) >> 8
	 182  		y4 = (r2*(y4-y5) + 128) >> 8
	 183  
	 184  		// Stage 4.
	 185  		s[8*0] = (y7 + y1) >> 14
	 186  		s[8*1] = (y3 + y2) >> 14
	 187  		s[8*2] = (y0 + y4) >> 14
	 188  		s[8*3] = (y8 + y6) >> 14
	 189  		s[8*4] = (y8 - y6) >> 14
	 190  		s[8*5] = (y0 - y4) >> 14
	 191  		s[8*6] = (y3 - y2) >> 14
	 192  		s[8*7] = (y7 - y1) >> 14
	 193  	}
	 194  }
	 195  

View as plain text