Bill Yi | 7fb3c4c | 2015-03-23 09:04:07 -0700 | [diff] [blame] | 1 | // Copyright 2011 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 file implements a Forward Discrete Cosine Transformation. |
| 8 | |
| 9 | /* |
| 10 | It is based on the code in jfdctint.c from the Independent JPEG Group, |
| 11 | found at http://www.ijg.org/files/jpegsrc.v8c.tar.gz. |
| 12 | |
| 13 | The "LEGAL ISSUES" section of the README in that archive says: |
| 14 | |
| 15 | In plain English: |
| 16 | |
| 17 | 1. We don't promise that this software works. (But if you find any bugs, |
| 18 | please let us know!) |
| 19 | 2. You can use this software for whatever you want. You don't have to pay us. |
| 20 | 3. You may not pretend that you wrote this software. If you use it in a |
| 21 | program, you must acknowledge somewhere in your documentation that |
| 22 | you've used the IJG code. |
| 23 | |
| 24 | In legalese: |
| 25 | |
| 26 | The authors make NO WARRANTY or representation, either express or implied, |
| 27 | with respect to this software, its quality, accuracy, merchantability, or |
| 28 | fitness for a particular purpose. This software is provided "AS IS", and you, |
| 29 | its user, assume the entire risk as to its quality and accuracy. |
| 30 | |
| 31 | This software is copyright (C) 1991-2011, Thomas G. Lane, Guido Vollbeding. |
| 32 | All Rights Reserved except as specified below. |
| 33 | |
| 34 | Permission is hereby granted to use, copy, modify, and distribute this |
| 35 | software (or portions thereof) for any purpose, without fee, subject to these |
| 36 | conditions: |
| 37 | (1) If any part of the source code for this software is distributed, then this |
| 38 | README file must be included, with this copyright and no-warranty notice |
| 39 | unaltered; and any additions, deletions, or changes to the original files |
| 40 | must be clearly indicated in accompanying documentation. |
| 41 | (2) If only executable code is distributed, then the accompanying |
| 42 | documentation must state that "this software is based in part on the work of |
| 43 | the Independent JPEG Group". |
| 44 | (3) Permission for use of this software is granted only if the user accepts |
| 45 | full responsibility for any undesirable consequences; the authors accept |
| 46 | NO LIABILITY for damages of any kind. |
| 47 | |
| 48 | These conditions apply to any software derived from or based on the IJG code, |
| 49 | not just to the unmodified library. If you use our work, you ought to |
| 50 | acknowledge us. |
| 51 | |
| 52 | Permission is NOT granted for the use of any IJG author's name or company name |
| 53 | in advertising or publicity relating to this software or products derived from |
| 54 | it. This software may be referred to only as "the Independent JPEG Group's |
| 55 | software". |
| 56 | |
| 57 | We specifically permit and encourage the use of this software as the basis of |
| 58 | commercial products, provided that all warranty or liability claims are |
| 59 | assumed by the product vendor. |
| 60 | */ |
| 61 | |
| 62 | // Trigonometric constants in 13-bit fixed point format. |
| 63 | const ( |
| 64 | fix_0_298631336 = 2446 |
| 65 | fix_0_390180644 = 3196 |
| 66 | fix_0_541196100 = 4433 |
| 67 | fix_0_765366865 = 6270 |
| 68 | fix_0_899976223 = 7373 |
| 69 | fix_1_175875602 = 9633 |
| 70 | fix_1_501321110 = 12299 |
| 71 | fix_1_847759065 = 15137 |
| 72 | fix_1_961570560 = 16069 |
| 73 | fix_2_053119869 = 16819 |
| 74 | fix_2_562915447 = 20995 |
| 75 | fix_3_072711026 = 25172 |
| 76 | ) |
| 77 | |
| 78 | const ( |
| 79 | constBits = 13 |
| 80 | pass1Bits = 2 |
| 81 | centerJSample = 128 |
| 82 | ) |
| 83 | |
| 84 | // fdct performs a forward DCT on an 8x8 block of coefficients, including a |
| 85 | // level shift. |
| 86 | func fdct(b *block) { |
| 87 | // Pass 1: process rows. |
| 88 | for y := 0; y < 8; y++ { |
| 89 | x0 := b[y*8+0] |
| 90 | x1 := b[y*8+1] |
| 91 | x2 := b[y*8+2] |
| 92 | x3 := b[y*8+3] |
| 93 | x4 := b[y*8+4] |
| 94 | x5 := b[y*8+5] |
| 95 | x6 := b[y*8+6] |
| 96 | x7 := b[y*8+7] |
| 97 | |
| 98 | tmp0 := x0 + x7 |
| 99 | tmp1 := x1 + x6 |
| 100 | tmp2 := x2 + x5 |
| 101 | tmp3 := x3 + x4 |
| 102 | |
| 103 | tmp10 := tmp0 + tmp3 |
| 104 | tmp12 := tmp0 - tmp3 |
| 105 | tmp11 := tmp1 + tmp2 |
| 106 | tmp13 := tmp1 - tmp2 |
| 107 | |
| 108 | tmp0 = x0 - x7 |
| 109 | tmp1 = x1 - x6 |
| 110 | tmp2 = x2 - x5 |
| 111 | tmp3 = x3 - x4 |
| 112 | |
| 113 | b[y*8+0] = (tmp10 + tmp11 - 8*centerJSample) << pass1Bits |
| 114 | b[y*8+4] = (tmp10 - tmp11) << pass1Bits |
| 115 | z1 := (tmp12 + tmp13) * fix_0_541196100 |
| 116 | z1 += 1 << (constBits - pass1Bits - 1) |
| 117 | b[y*8+2] = (z1 + tmp12*fix_0_765366865) >> (constBits - pass1Bits) |
| 118 | b[y*8+6] = (z1 - tmp13*fix_1_847759065) >> (constBits - pass1Bits) |
| 119 | |
| 120 | tmp10 = tmp0 + tmp3 |
| 121 | tmp11 = tmp1 + tmp2 |
| 122 | tmp12 = tmp0 + tmp2 |
| 123 | tmp13 = tmp1 + tmp3 |
| 124 | z1 = (tmp12 + tmp13) * fix_1_175875602 |
| 125 | z1 += 1 << (constBits - pass1Bits - 1) |
| 126 | tmp0 = tmp0 * fix_1_501321110 |
| 127 | tmp1 = tmp1 * fix_3_072711026 |
| 128 | tmp2 = tmp2 * fix_2_053119869 |
| 129 | tmp3 = tmp3 * fix_0_298631336 |
| 130 | tmp10 = tmp10 * -fix_0_899976223 |
| 131 | tmp11 = tmp11 * -fix_2_562915447 |
| 132 | tmp12 = tmp12 * -fix_0_390180644 |
| 133 | tmp13 = tmp13 * -fix_1_961570560 |
| 134 | |
| 135 | tmp12 += z1 |
| 136 | tmp13 += z1 |
| 137 | b[y*8+1] = (tmp0 + tmp10 + tmp12) >> (constBits - pass1Bits) |
| 138 | b[y*8+3] = (tmp1 + tmp11 + tmp13) >> (constBits - pass1Bits) |
| 139 | b[y*8+5] = (tmp2 + tmp11 + tmp12) >> (constBits - pass1Bits) |
| 140 | b[y*8+7] = (tmp3 + tmp10 + tmp13) >> (constBits - pass1Bits) |
| 141 | } |
| 142 | // Pass 2: process columns. |
| 143 | // We remove pass1Bits scaling, but leave results scaled up by an overall factor of 8. |
| 144 | for x := 0; x < 8; x++ { |
| 145 | tmp0 := b[0*8+x] + b[7*8+x] |
| 146 | tmp1 := b[1*8+x] + b[6*8+x] |
| 147 | tmp2 := b[2*8+x] + b[5*8+x] |
| 148 | tmp3 := b[3*8+x] + b[4*8+x] |
| 149 | |
| 150 | tmp10 := tmp0 + tmp3 + 1<<(pass1Bits-1) |
| 151 | tmp12 := tmp0 - tmp3 |
| 152 | tmp11 := tmp1 + tmp2 |
| 153 | tmp13 := tmp1 - tmp2 |
| 154 | |
| 155 | tmp0 = b[0*8+x] - b[7*8+x] |
| 156 | tmp1 = b[1*8+x] - b[6*8+x] |
| 157 | tmp2 = b[2*8+x] - b[5*8+x] |
| 158 | tmp3 = b[3*8+x] - b[4*8+x] |
| 159 | |
| 160 | b[0*8+x] = (tmp10 + tmp11) >> pass1Bits |
| 161 | b[4*8+x] = (tmp10 - tmp11) >> pass1Bits |
| 162 | |
| 163 | z1 := (tmp12 + tmp13) * fix_0_541196100 |
| 164 | z1 += 1 << (constBits + pass1Bits - 1) |
| 165 | b[2*8+x] = (z1 + tmp12*fix_0_765366865) >> (constBits + pass1Bits) |
| 166 | b[6*8+x] = (z1 - tmp13*fix_1_847759065) >> (constBits + pass1Bits) |
| 167 | |
| 168 | tmp10 = tmp0 + tmp3 |
| 169 | tmp11 = tmp1 + tmp2 |
| 170 | tmp12 = tmp0 + tmp2 |
| 171 | tmp13 = tmp1 + tmp3 |
| 172 | z1 = (tmp12 + tmp13) * fix_1_175875602 |
| 173 | z1 += 1 << (constBits + pass1Bits - 1) |
| 174 | tmp0 = tmp0 * fix_1_501321110 |
| 175 | tmp1 = tmp1 * fix_3_072711026 |
| 176 | tmp2 = tmp2 * fix_2_053119869 |
| 177 | tmp3 = tmp3 * fix_0_298631336 |
| 178 | tmp10 = tmp10 * -fix_0_899976223 |
| 179 | tmp11 = tmp11 * -fix_2_562915447 |
| 180 | tmp12 = tmp12 * -fix_0_390180644 |
| 181 | tmp13 = tmp13 * -fix_1_961570560 |
| 182 | |
| 183 | tmp12 += z1 |
| 184 | tmp13 += z1 |
| 185 | b[1*8+x] = (tmp0 + tmp10 + tmp12) >> (constBits + pass1Bits) |
| 186 | b[3*8+x] = (tmp1 + tmp11 + tmp13) >> (constBits + pass1Bits) |
| 187 | b[5*8+x] = (tmp2 + tmp11 + tmp12) >> (constBits + pass1Bits) |
| 188 | b[7*8+x] = (tmp3 + tmp10 + tmp13) >> (constBits + pass1Bits) |
| 189 | } |
| 190 | } |