// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package jpeg implements a JPEG image decoder and encoder. // // JPEG is defined in ITU-T T.81: https://www.w3.org/Graphics/JPEG/itu-t81.pdf. package imgsz import ( "io" ) const blockSize = 64 // A DCT block is 8x8. const ( sof0Marker = 0xc0 // Start Of Frame (Baseline Sequential). sof1Marker = 0xc1 // Start Of Frame (Extended Sequential). sof2Marker = 0xc2 // Start Of Frame (Progressive). dhtMarker = 0xc4 // Define Huffman Table. rst0Marker = 0xd0 // ReSTart (0). rst7Marker = 0xd7 // ReSTart (7). soiMarker = 0xd8 // Start Of Image. eoiMarker = 0xd9 // End Of Image. sosMarker = 0xda // Start Of Scan. dqtMarker = 0xdb // Define Quantization Table. driMarker = 0xdd // Define Restart Interval. comMarker = 0xfe // COMment. // "APPlication specific" markers aren't part of the JPEG spec per se, // but in practice, their use is described at // https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html app0Marker = 0xe0 app14Marker = 0xee app15Marker = 0xef ) // bits holds the unprocessed bits that have been taken from the byte-stream. // The n least significant bits of a form the unread bits, to be read in MSB to // LSB order. type bits struct { a uint32 // accumulator. m uint32 // mask. m==1<<(n-1) when n>0, with m==0 when n==0. n int32 // the number of unread bits in a. } type jpgdecoder struct { r io.Reader bits bits // bytes is a byte buffer, similar to a bufio.Reader, except that it // has to be able to unread more than 1 byte, due to byte stuffing. // Byte stuffing is specified in section F.1.2.3. bytes struct { // buf[i:j] are the buffered bytes read from the underlying // io.Reader that haven't yet been passed further on. buf [4096]byte i, j int // nUnreadable is the number of bytes to back up i after // overshooting. It can be 0, 1 or 2. nUnreadable int } width, height int nComp int // As per section 4.5, there are four modes of operation (selected by the // SOF? markers): sequential DCT, progressive DCT, lossless and // hierarchical, although this implementation does not support the latter // two non-DCT modes. Sequential DCT is further split into baseline and // extended, as per section 4.11. baseline bool progressive bool jfif bool tmp [2 * blockSize]byte } // fill fills up the d.bytes.buf buffer from the underlying io.Reader. It // should only be called when there are no unread bytes in d.bytes. func (d *jpgdecoder) fill() error { if d.bytes.i != d.bytes.j { panic("jpeg: fill called when unread bytes exist") } // Move the last 2 bytes to the start of the buffer, in case we need // to call unreadByteStuffedByte. if d.bytes.j > 2 { d.bytes.buf[0] = d.bytes.buf[d.bytes.j-2] d.bytes.buf[1] = d.bytes.buf[d.bytes.j-1] d.bytes.i, d.bytes.j = 2, 2 } // Fill in the rest of the buffer. n, err := d.r.Read(d.bytes.buf[d.bytes.j:]) d.bytes.j += n if n > 0 { err = nil } return err } // unreadByteStuffedByte undoes the most recent readByteStuffedByte call, // giving a byte of data back from d.bits to d.bytes. The Huffman look-up table // requires at least 8 bits for look-up, which means that Huffman decoding can // sometimes overshoot and read one or two too many bytes. Two-byte overshoot // can happen when expecting to read a 0xff 0x00 byte-stuffed byte. func (d *jpgdecoder) unreadByteStuffedByte() { d.bytes.i -= d.bytes.nUnreadable d.bytes.nUnreadable = 0 if d.bits.n >= 8 { d.bits.a >>= 8 d.bits.n -= 8 d.bits.m >>= 8 } } // readByte returns the next byte, whether buffered or not buffered. It does // not care about byte stuffing. func (d *jpgdecoder) readByte() (x byte, err error) { for d.bytes.i == d.bytes.j { if err = d.fill(); err != nil { return 0, err } } x = d.bytes.buf[d.bytes.i] d.bytes.i++ d.bytes.nUnreadable = 0 return x, nil } // readFull reads exactly len(p) bytes into p. It does not care about byte // stuffing. func (d *jpgdecoder) readFull(p []byte) error { // Unread the overshot bytes, if any. if d.bytes.nUnreadable != 0 { if d.bits.n >= 8 { d.unreadByteStuffedByte() } d.bytes.nUnreadable = 0 } for { n := copy(p, d.bytes.buf[d.bytes.i:d.bytes.j]) p = p[n:] d.bytes.i += n if len(p) == 0 { break } if err := d.fill(); err != nil { if err == io.EOF { err = io.ErrUnexpectedEOF } return err } } return nil } // ignore ignores the next n bytes. func (d *jpgdecoder) ignore(n int) error { // Unread the overshot bytes, if any. if d.bytes.nUnreadable != 0 { if d.bits.n >= 8 { d.unreadByteStuffedByte() } d.bytes.nUnreadable = 0 } for { m := d.bytes.j - d.bytes.i if m > n { m = n } d.bytes.i += m n -= m if n == 0 { break } if err := d.fill(); err != nil { if err == io.EOF { err = io.ErrUnexpectedEOF } return err } } return nil } // Specified in section B.2.2. func (d *jpgdecoder) processSOF(n int) error { if d.nComp != 0 { return FormatError("multiple SOF markers") } switch n { case 6 + 3*1: // Grayscale image. d.nComp = 1 case 6 + 3*3: // YCbCr or RGB image. d.nComp = 3 case 6 + 3*4: // YCbCrK or CMYK image. d.nComp = 4 default: return UnsupportedError("number of components") } if err := d.readFull(d.tmp[:n]); err != nil { return err } // We only support 8-bit precision. if d.tmp[0] != 8 { return UnsupportedError("precision") } d.height = int(d.tmp[1])<<8 + int(d.tmp[2]) d.width = int(d.tmp[3])<<8 + int(d.tmp[4]) if int(d.tmp[5]) != d.nComp { return FormatError("SOF has wrong length") } return nil } // decode reads a JPEG image from r and returns its Size func (d *jpgdecoder) decode(r io.Reader) (Size, error) { d.r = r // Check for the Start Of Image marker. if err := d.readFull(d.tmp[:2]); err != nil { return Size{}, err } if d.tmp[0] != 0xff || d.tmp[1] != soiMarker { return Size{}, FormatError("missing SOI marker") } // Process the remaining segments until the End Of Image marker. for { err := d.readFull(d.tmp[:2]) if err != nil { return Size{}, err } for d.tmp[0] != 0xff { // Strictly speaking, this is a format error. However, libjpeg is // liberal in what it accepts. As of version 9, next_marker in // jdmarker.c treats this as a warning (JWRN_EXTRANEOUS_DATA) and // continues to decode the stream. Even before next_marker sees // extraneous data, jpeg_fill_bit_buffer in jdhuff.c reads as many // bytes as it can, possibly past the end of a scan's data. It // effectively puts back any markers that it overscanned (e.g. an // "\xff\xd9" EOI marker), but it does not put back non-marker data, // and thus it can silently ignore a small number of extraneous // non-marker bytes before next_marker has a chance to see them (and // print a warning). // // We are therefore also liberal in what we accept. Extraneous data // is silently ignored. // // This is similar to, but not exactly the same as, the restart // mechanism within a scan (the RST[0-7] markers). // // Note that extraneous 0xff bytes in e.g. SOS data are escaped as // "\xff\x00", and so are detected a little further down below. d.tmp[0] = d.tmp[1] d.tmp[1], err = d.readByte() if err != nil { return Size{}, err } } marker := d.tmp[1] if marker == 0 { // Treat "\xff\x00" as extraneous data. continue } for marker == 0xff { // Section B.1.1.2 says, "Any marker may optionally be preceded by any // number of fill bytes, which are bytes assigned code X'FF'". marker, err = d.readByte() if err != nil { return Size{}, err } } if marker == eoiMarker { // End Of Image. break } if rst0Marker <= marker && marker <= rst7Marker { // Figures B.2 and B.16 of the specification suggest that restart markers should // only occur between Entropy Coded Segments and not after the final ECS. // However, some encoders may generate incorrect JPEGs with a final restart // marker. That restart marker will be seen here instead of inside the processSOS // method, and is ignored as a harmless error. Restart markers have no extra data, // so we check for this before we read the 16-bit length of the segment. continue } // Read the 16-bit length of the segment. The value includes the 2 bytes for the // length itself, so we subtract 2 to get the number of remaining bytes. if err = d.readFull(d.tmp[:2]); err != nil { return Size{}, err } n := int(d.tmp[0])<<8 + int(d.tmp[1]) - 2 if n < 0 { return Size{}, FormatError("short segment length") } switch marker { case sof0Marker, sof1Marker, sof2Marker: d.baseline = marker == sof0Marker d.progressive = marker == sof2Marker err = d.processSOF(n) if d.jfif { return Size{}, err } return Size{d.width, d.height}, nil case sosMarker: return Size{}, nil case dhtMarker, dqtMarker, driMarker, app0Marker, app14Marker: err = d.ignore(n) default: if app0Marker <= marker && marker <= app15Marker || marker == comMarker { err = d.ignore(n) } else if marker < 0xc0 { // See Table B.1 "Marker code assignments". err = FormatError("unknown marker") } else { err = UnsupportedError("unknown marker") } } if err != nil { return Size{}, err } } return Size{d.width, d.height}, nil }