github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/image/jpeg/reader.go (about) 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 implements a JPEG image decoder and encoder. 6 // 7 // JPEG is defined in ITU-T T.81: http://www.w3.org/Graphics/JPEG/itu-t81.pdf. 8 package jpeg 9 10 import ( 11 "bufio" 12 "image" 13 "image/color" 14 "io" 15 ) 16 17 // TODO(nigeltao): fix up the doc comment style so that sentences start with 18 // the name of the type or function that they annotate. 19 20 // A FormatError reports that the input is not a valid JPEG. 21 type FormatError string 22 23 func (e FormatError) Error() string { return "invalid JPEG format: " + string(e) } 24 25 // An UnsupportedError reports that the input uses a valid but unimplemented JPEG feature. 26 type UnsupportedError string 27 28 func (e UnsupportedError) Error() string { return "unsupported JPEG feature: " + string(e) } 29 30 // Component specification, specified in section B.2.2. 31 type component struct { 32 h int // Horizontal sampling factor. 33 v int // Vertical sampling factor. 34 c uint8 // Component identifier. 35 tq uint8 // Quantization table destination selector. 36 } 37 38 const ( 39 dcTable = 0 40 acTable = 1 41 maxTc = 1 42 maxTh = 3 43 maxTq = 3 44 45 // A grayscale JPEG image has only a Y component. 46 nGrayComponent = 1 47 // A color JPEG image has Y, Cb and Cr components. 48 nColorComponent = 3 49 50 // We only support 4:4:4, 4:4:0, 4:2:2 and 4:2:0 downsampling, and therefore the 51 // number of luma samples per chroma sample is at most 2 in the horizontal 52 // and 2 in the vertical direction. 53 maxH = 2 54 maxV = 2 55 ) 56 57 const ( 58 soiMarker = 0xd8 // Start Of Image. 59 eoiMarker = 0xd9 // End Of Image. 60 sof0Marker = 0xc0 // Start Of Frame (Baseline). 61 sof2Marker = 0xc2 // Start Of Frame (Progressive). 62 dhtMarker = 0xc4 // Define Huffman Table. 63 dqtMarker = 0xdb // Define Quantization Table. 64 sosMarker = 0xda // Start Of Scan. 65 driMarker = 0xdd // Define Restart Interval. 66 rst0Marker = 0xd0 // ReSTart (0). 67 rst7Marker = 0xd7 // ReSTart (7). 68 app0Marker = 0xe0 // APPlication specific (0). 69 app15Marker = 0xef // APPlication specific (15). 70 comMarker = 0xfe // COMment. 71 ) 72 73 // unzig maps from the zig-zag ordering to the natural ordering. For example, 74 // unzig[3] is the column and row of the fourth element in zig-zag order. The 75 // value is 16, which means first column (16%8 == 0) and third row (16/8 == 2). 76 var unzig = [blockSize]int{ 77 0, 1, 8, 16, 9, 2, 3, 10, 78 17, 24, 32, 25, 18, 11, 4, 5, 79 12, 19, 26, 33, 40, 48, 41, 34, 80 27, 20, 13, 6, 7, 14, 21, 28, 81 35, 42, 49, 56, 57, 50, 43, 36, 82 29, 22, 15, 23, 30, 37, 44, 51, 83 58, 59, 52, 45, 38, 31, 39, 46, 84 53, 60, 61, 54, 47, 55, 62, 63, 85 } 86 87 // If the passed in io.Reader does not also have ReadByte, then Decode will introduce its own buffering. 88 type Reader interface { 89 io.Reader 90 ReadByte() (c byte, err error) 91 } 92 93 type decoder struct { 94 r Reader 95 b bits 96 width, height int 97 img1 *image.Gray 98 img3 *image.YCbCr 99 ri int // Restart Interval. 100 nComp int 101 progressive bool 102 eobRun uint16 // End-of-Band run, specified in section G.1.2.2. 103 comp [nColorComponent]component 104 progCoeffs [nColorComponent][]block // Saved state between progressive-mode scans. 105 huff [maxTc + 1][maxTh + 1]huffman 106 quant [maxTq + 1]block // Quantization tables, in zig-zag order. 107 tmp [1024]byte 108 } 109 110 // Reads and ignores the next n bytes. 111 func (d *decoder) ignore(n int) error { 112 for n > 0 { 113 m := len(d.tmp) 114 if m > n { 115 m = n 116 } 117 _, err := io.ReadFull(d.r, d.tmp[0:m]) 118 if err != nil { 119 return err 120 } 121 n -= m 122 } 123 return nil 124 } 125 126 // Specified in section B.2.2. 127 func (d *decoder) processSOF(n int, configOnly bool) error { 128 switch n { 129 case 6 + 3*nGrayComponent: 130 d.nComp = nGrayComponent 131 case 6 + 3*nColorComponent: 132 d.nComp = nColorComponent 133 default: 134 return UnsupportedError("SOF has wrong length") 135 } 136 _, err := io.ReadFull(d.r, d.tmp[:n]) 137 if err != nil { 138 return err 139 } 140 // We only support 8-bit precision. 141 if d.tmp[0] != 8 { 142 return UnsupportedError("precision") 143 } 144 d.height = int(d.tmp[1])<<8 + int(d.tmp[2]) 145 d.width = int(d.tmp[3])<<8 + int(d.tmp[4]) 146 if int(d.tmp[5]) != d.nComp { 147 return UnsupportedError("SOF has wrong number of image components") 148 } 149 if configOnly { 150 return nil 151 } 152 for i := 0; i < d.nComp; i++ { 153 d.comp[i].c = d.tmp[6+3*i] 154 d.comp[i].tq = d.tmp[8+3*i] 155 if d.nComp == nGrayComponent { 156 // If a JPEG image has only one component, section A.2 says "this data 157 // is non-interleaved by definition" and section A.2.2 says "[in this 158 // case...] the order of data units within a scan shall be left-to-right 159 // and top-to-bottom... regardless of the values of H_1 and V_1". Section 160 // 4.8.2 also says "[for non-interleaved data], the MCU is defined to be 161 // one data unit". Similarly, section A.1.1 explains that it is the ratio 162 // of H_i to max_j(H_j) that matters, and similarly for V. For grayscale 163 // images, H_1 is the maximum H_j for all components j, so that ratio is 164 // always 1. The component's (h, v) is effectively always (1, 1): even if 165 // the nominal (h, v) is (2, 1), a 20x5 image is encoded in three 8x8 166 // MCUs, not two 16x8 MCUs. 167 d.comp[i].h = 1 168 d.comp[i].v = 1 169 continue 170 } 171 hv := d.tmp[7+3*i] 172 d.comp[i].h = int(hv >> 4) 173 d.comp[i].v = int(hv & 0x0f) 174 // For color images, we only support 4:4:4, 4:4:0, 4:2:2 or 4:2:0 chroma 175 // downsampling ratios. This implies that the (h, v) values for the Y 176 // component are either (1, 1), (1, 2), (2, 1) or (2, 2), and the (h, v) 177 // values for the Cr and Cb components must be (1, 1). 178 if i == 0 { 179 if hv != 0x11 && hv != 0x21 && hv != 0x22 && hv != 0x12 { 180 return UnsupportedError("luma downsample ratio") 181 } 182 } else if hv != 0x11 { 183 return UnsupportedError("chroma downsample ratio") 184 } 185 } 186 return nil 187 } 188 189 // Specified in section B.2.4.1. 190 func (d *decoder) processDQT(n int) error { 191 const qtLength = 1 + blockSize 192 for ; n >= qtLength; n -= qtLength { 193 _, err := io.ReadFull(d.r, d.tmp[0:qtLength]) 194 if err != nil { 195 return err 196 } 197 pq := d.tmp[0] >> 4 198 if pq != 0 { 199 return UnsupportedError("bad Pq value") 200 } 201 tq := d.tmp[0] & 0x0f 202 if tq > maxTq { 203 return FormatError("bad Tq value") 204 } 205 for i := range d.quant[tq] { 206 d.quant[tq][i] = int32(d.tmp[i+1]) 207 } 208 } 209 if n != 0 { 210 return FormatError("DQT has wrong length") 211 } 212 return nil 213 } 214 215 // Specified in section B.2.4.4. 216 func (d *decoder) processDRI(n int) error { 217 if n != 2 { 218 return FormatError("DRI has wrong length") 219 } 220 _, err := io.ReadFull(d.r, d.tmp[0:2]) 221 if err != nil { 222 return err 223 } 224 d.ri = int(d.tmp[0])<<8 + int(d.tmp[1]) 225 return nil 226 } 227 228 // decode reads a JPEG image from r and returns it as an image.Image. 229 func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error) { 230 if rr, ok := r.(Reader); ok { 231 d.r = rr 232 } else { 233 d.r = bufio.NewReader(r) 234 } 235 236 // Check for the Start Of Image marker. 237 _, err := io.ReadFull(d.r, d.tmp[0:2]) 238 if err != nil { 239 return nil, err 240 } 241 if d.tmp[0] != 0xff || d.tmp[1] != soiMarker { 242 return nil, FormatError("missing SOI marker") 243 } 244 245 // Process the remaining segments until the End Of Image marker. 246 for { 247 _, err := io.ReadFull(d.r, d.tmp[0:2]) 248 if err != nil { 249 return nil, err 250 } 251 for d.tmp[0] != 0xff { 252 // Strictly speaking, this is a format error. However, libjpeg is 253 // liberal in what it accepts. As of version 9, next_marker in 254 // jdmarker.c treats this as a warning (JWRN_EXTRANEOUS_DATA) and 255 // continues to decode the stream. Even before next_marker sees 256 // extraneous data, jpeg_fill_bit_buffer in jdhuff.c reads as many 257 // bytes as it can, possibly past the end of a scan's data. It 258 // effectively puts back any markers that it overscanned (e.g. an 259 // "\xff\xd9" EOI marker), but it does not put back non-marker data, 260 // and thus it can silently ignore a small number of extraneous 261 // non-marker bytes before next_marker has a chance to see them (and 262 // print a warning). 263 // 264 // We are therefore also liberal in what we accept. Extraneous data 265 // is silently ignored. 266 // 267 // This is similar to, but not exactly the same as, the restart 268 // mechanism within a scan (the RST[0-7] markers). 269 // 270 // Note that extraneous 0xff bytes in e.g. SOS data are escaped as 271 // "\xff\x00", and so are detected a little further down below. 272 d.tmp[0] = d.tmp[1] 273 d.tmp[1], err = d.r.ReadByte() 274 if err != nil { 275 return nil, err 276 } 277 } 278 marker := d.tmp[1] 279 if marker == 0 { 280 // Treat "\xff\x00" as extraneous data. 281 continue 282 } 283 for marker == 0xff { 284 // Section B.1.1.2 says, "Any marker may optionally be preceded by any 285 // number of fill bytes, which are bytes assigned code X'FF'". 286 marker, err = d.r.ReadByte() 287 if err != nil { 288 return nil, err 289 } 290 } 291 if marker == eoiMarker { // End Of Image. 292 break 293 } 294 if rst0Marker <= marker && marker <= rst7Marker { 295 // Figures B.2 and B.16 of the specification suggest that restart markers should 296 // only occur between Entropy Coded Segments and not after the final ECS. 297 // However, some encoders may generate incorrect JPEGs with a final restart 298 // marker. That restart marker will be seen here instead of inside the processSOS 299 // method, and is ignored as a harmless error. Restart markers have no extra data, 300 // so we check for this before we read the 16-bit length of the segment. 301 continue 302 } 303 304 // Read the 16-bit length of the segment. The value includes the 2 bytes for the 305 // length itself, so we subtract 2 to get the number of remaining bytes. 306 _, err = io.ReadFull(d.r, d.tmp[0:2]) 307 if err != nil { 308 return nil, err 309 } 310 n := int(d.tmp[0])<<8 + int(d.tmp[1]) - 2 311 if n < 0 { 312 return nil, FormatError("short segment length") 313 } 314 315 switch { 316 case marker == sof0Marker || marker == sof2Marker: // Start Of Frame. 317 d.progressive = marker == sof2Marker 318 err = d.processSOF(n, configOnly) 319 if configOnly { 320 return nil, err 321 } 322 case marker == dhtMarker: // Define Huffman Table. 323 err = d.processDHT(n) 324 case marker == dqtMarker: // Define Quantization Table. 325 err = d.processDQT(n) 326 case marker == sosMarker: // Start Of Scan. 327 err = d.processSOS(n) 328 case marker == driMarker: // Define Restart Interval. 329 err = d.processDRI(n) 330 case app0Marker <= marker && marker <= app15Marker || marker == comMarker: // APPlication specific, or COMment. 331 err = d.ignore(n) 332 default: 333 err = UnsupportedError("unknown marker") 334 } 335 if err != nil { 336 return nil, err 337 } 338 } 339 if d.img1 != nil { 340 return d.img1, nil 341 } 342 if d.img3 != nil { 343 return d.img3, nil 344 } 345 return nil, FormatError("missing SOS marker") 346 } 347 348 // Decode reads a JPEG image from r and returns it as an image.Image. 349 func Decode(r io.Reader) (image.Image, error) { 350 var d decoder 351 return d.decode(r, false) 352 } 353 354 // DecodeConfig returns the color model and dimensions of a JPEG image without 355 // decoding the entire image. 356 func DecodeConfig(r io.Reader) (image.Config, error) { 357 var d decoder 358 if _, err := d.decode(r, true); err != nil { 359 return image.Config{}, err 360 } 361 switch d.nComp { 362 case nGrayComponent: 363 return image.Config{ 364 ColorModel: color.GrayModel, 365 Width: d.width, 366 Height: d.height, 367 }, nil 368 case nColorComponent: 369 return image.Config{ 370 ColorModel: color.YCbCrModel, 371 Width: d.width, 372 Height: d.height, 373 }, nil 374 } 375 return image.Config{}, FormatError("missing SOF marker") 376 } 377 378 func init() { 379 image.RegisterFormat("jpeg", "\xff\xd8", Decode, DecodeConfig) 380 }