~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/image/jpeg/reader.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
        tq uint8 // Quantization table destination selector.
36
36
}
37
37
 
38
 
type block [blockSize]int
39
 
 
40
38
const (
41
 
        blockSize = 64 // A DCT block is 8x8.
42
 
 
43
39
        dcTable = 0
44
40
        acTable = 1
45
41
        maxTc   = 1
51
47
        // A color JPEG image has Y, Cb and Cr components.
52
48
        nColorComponent = 3
53
49
 
54
 
        // We only support 4:4:4, 4:2:2 and 4:2:0 downsampling, and therefore the
 
50
        // We only support 4:4:4, 4:4:0, 4:2:2 and 4:2:0 downsampling, and therefore the
55
51
        // number of luma samples per chroma sample is at most 2 in the horizontal
56
52
        // and 2 in the vertical direction.
57
53
        maxH = 2
74
70
        comMarker   = 0xfe // COMment.
75
71
)
76
72
 
77
 
// Maps from the zig-zag ordering to the natural ordering.
 
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).
78
76
var unzig = [blockSize]int{
79
77
        0, 1, 8, 16, 9, 2, 3, 10,
80
78
        17, 24, 32, 25, 18, 11, 4, 5,
94
92
 
95
93
type decoder struct {
96
94
        r             Reader
 
95
        b             bits
97
96
        width, height int
98
97
        img1          *image.Gray
99
98
        img3          *image.YCbCr
100
99
        ri            int // Restart Interval.
101
100
        nComp         int
 
101
        progressive   bool
 
102
        eobRun        uint16 // End-of-Band run, specified in section G.1.2.2.
102
103
        comp          [nColorComponent]component
 
104
        progCoeffs    [nColorComponent][]block // Saved state between progressive-mode scans.
103
105
        huff          [maxTc + 1][maxTh + 1]huffman
104
 
        quant         [maxTq + 1]block
105
 
        b             bits
 
106
        quant         [maxTq + 1]block // Quantization tables, in zig-zag order.
106
107
        tmp           [1024]byte
107
108
}
108
109
 
146
147
                return UnsupportedError("SOF has wrong number of image components")
147
148
        }
148
149
        for i := 0; i < d.nComp; i++ {
 
150
                d.comp[i].c = d.tmp[6+3*i]
 
151
                d.comp[i].tq = d.tmp[8+3*i]
 
152
                if d.nComp == nGrayComponent {
 
153
                        // If a JPEG image has only one component, section A.2 says "this data
 
154
                        // is non-interleaved by definition" and section A.2.2 says "[in this
 
155
                        // case...] the order of data units within a scan shall be left-to-right
 
156
                        // and top-to-bottom... regardless of the values of H_1 and V_1". Section
 
157
                        // 4.8.2 also says "[for non-interleaved data], the MCU is defined to be
 
158
                        // one data unit". Similarly, section A.1.1 explains that it is the ratio
 
159
                        // of H_i to max_j(H_j) that matters, and similarly for V. For grayscale
 
160
                        // images, H_1 is the maximum H_j for all components j, so that ratio is
 
161
                        // always 1. The component's (h, v) is effectively always (1, 1): even if
 
162
                        // the nominal (h, v) is (2, 1), a 20x5 image is encoded in three 8x8
 
163
                        // MCUs, not two 16x8 MCUs.
 
164
                        d.comp[i].h = 1
 
165
                        d.comp[i].v = 1
 
166
                        continue
 
167
                }
149
168
                hv := d.tmp[7+3*i]
150
169
                d.comp[i].h = int(hv >> 4)
151
170
                d.comp[i].v = int(hv & 0x0f)
152
 
                d.comp[i].c = d.tmp[6+3*i]
153
 
                d.comp[i].tq = d.tmp[8+3*i]
154
 
                if d.nComp == nGrayComponent {
155
 
                        continue
156
 
                }
157
 
                // For color images, we only support 4:4:4, 4:2:2 or 4:2:0 chroma
 
171
                // For color images, we only support 4:4:4, 4:4:0, 4:2:2 or 4:2:0 chroma
158
172
                // downsampling ratios. This implies that the (h, v) values for the Y
159
 
                // component are either (1, 1), (2, 1) or (2, 2), and the (h, v)
 
173
                // component are either (1, 1), (1, 2), (2, 1) or (2, 2), and the (h, v)
160
174
                // values for the Cr and Cb components must be (1, 1).
161
175
                if i == 0 {
162
 
                        if hv != 0x11 && hv != 0x21 && hv != 0x22 {
 
176
                        if hv != 0x11 && hv != 0x21 && hv != 0x22 && hv != 0x12 {
163
177
                                return UnsupportedError("luma downsample ratio")
164
178
                        }
165
179
                } else if hv != 0x11 {
186
200
                        return FormatError("bad Tq value")
187
201
                }
188
202
                for i := range d.quant[tq] {
189
 
                        d.quant[tq][i] = int(d.tmp[i+1])
 
203
                        d.quant[tq][i] = int32(d.tmp[i+1])
190
204
                }
191
205
        }
192
206
        if n != 0 {
195
209
        return nil
196
210
}
197
211
 
198
 
// makeImg allocates and initializes the destination image.
199
 
func (d *decoder) makeImg(h0, v0, mxx, myy int) {
200
 
        if d.nComp == nGrayComponent {
201
 
                m := image.NewGray(image.Rect(0, 0, 8*mxx, 8*myy))
202
 
                d.img1 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.Gray)
203
 
                return
204
 
        }
205
 
        var subsampleRatio image.YCbCrSubsampleRatio
206
 
        switch h0 * v0 {
207
 
        case 1:
208
 
                subsampleRatio = image.YCbCrSubsampleRatio444
209
 
        case 2:
210
 
                subsampleRatio = image.YCbCrSubsampleRatio422
211
 
        case 4:
212
 
                subsampleRatio = image.YCbCrSubsampleRatio420
213
 
        default:
214
 
                panic("unreachable")
215
 
        }
216
 
        m := image.NewYCbCr(image.Rect(0, 0, 8*h0*mxx, 8*v0*myy), subsampleRatio)
217
 
        d.img3 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.YCbCr)
218
 
}
219
 
 
220
 
// Specified in section B.2.3.
221
 
func (d *decoder) processSOS(n int) error {
222
 
        if d.nComp == 0 {
223
 
                return FormatError("missing SOF marker")
224
 
        }
225
 
        if n != 4+2*d.nComp {
226
 
                return UnsupportedError("SOS has wrong length")
227
 
        }
228
 
        _, err := io.ReadFull(d.r, d.tmp[0:4+2*d.nComp])
229
 
        if err != nil {
230
 
                return err
231
 
        }
232
 
        if int(d.tmp[0]) != d.nComp {
233
 
                return UnsupportedError("SOS has wrong number of image components")
234
 
        }
235
 
        var scan [nColorComponent]struct {
236
 
                td uint8 // DC table selector.
237
 
                ta uint8 // AC table selector.
238
 
        }
239
 
        for i := 0; i < d.nComp; i++ {
240
 
                cs := d.tmp[1+2*i] // Component selector.
241
 
                if cs != d.comp[i].c {
242
 
                        return UnsupportedError("scan components out of order")
243
 
                }
244
 
                scan[i].td = d.tmp[2+2*i] >> 4
245
 
                scan[i].ta = d.tmp[2+2*i] & 0x0f
246
 
        }
247
 
        // mxx and myy are the number of MCUs (Minimum Coded Units) in the image.
248
 
        h0, v0 := d.comp[0].h, d.comp[0].v // The h and v values from the Y components.
249
 
        mxx := (d.width + 8*h0 - 1) / (8 * h0)
250
 
        myy := (d.height + 8*v0 - 1) / (8 * v0)
251
 
        if d.img1 == nil && d.img3 == nil {
252
 
                d.makeImg(h0, v0, mxx, myy)
253
 
        }
254
 
 
255
 
        mcu, expectedRST := 0, uint8(rst0Marker)
256
 
        var (
257
 
                b  block
258
 
                dc [nColorComponent]int
259
 
        )
260
 
        for my := 0; my < myy; my++ {
261
 
                for mx := 0; mx < mxx; mx++ {
262
 
                        for i := 0; i < d.nComp; i++ {
263
 
                                qt := &d.quant[d.comp[i].tq]
264
 
                                for j := 0; j < d.comp[i].h*d.comp[i].v; j++ {
265
 
                                        // TODO(nigeltao): make this a "var b block" once the compiler's escape
266
 
                                        // analysis is good enough to allocate it on the stack, not the heap.
267
 
                                        b = block{}
268
 
 
269
 
                                        // Decode the DC coefficient, as specified in section F.2.2.1.
270
 
                                        value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
271
 
                                        if err != nil {
272
 
                                                return err
273
 
                                        }
274
 
                                        if value > 16 {
275
 
                                                return UnsupportedError("excessive DC component")
276
 
                                        }
277
 
                                        dcDelta, err := d.receiveExtend(value)
278
 
                                        if err != nil {
279
 
                                                return err
280
 
                                        }
281
 
                                        dc[i] += dcDelta
282
 
                                        b[0] = dc[i] * qt[0]
283
 
 
284
 
                                        // Decode the AC coefficients, as specified in section F.2.2.2.
285
 
                                        for k := 1; k < blockSize; k++ {
286
 
                                                value, err := d.decodeHuffman(&d.huff[acTable][scan[i].ta])
287
 
                                                if err != nil {
288
 
                                                        return err
289
 
                                                }
290
 
                                                val0 := value >> 4
291
 
                                                val1 := value & 0x0f
292
 
                                                if val1 != 0 {
293
 
                                                        k += int(val0)
294
 
                                                        if k > blockSize {
295
 
                                                                return FormatError("bad DCT index")
296
 
                                                        }
297
 
                                                        ac, err := d.receiveExtend(val1)
298
 
                                                        if err != nil {
299
 
                                                                return err
300
 
                                                        }
301
 
                                                        b[unzig[k]] = ac * qt[k]
302
 
                                                } else {
303
 
                                                        if val0 != 0x0f {
304
 
                                                                break
305
 
                                                        }
306
 
                                                        k += 0x0f
307
 
                                                }
308
 
                                        }
309
 
 
310
 
                                        // Perform the inverse DCT and store the MCU component to the image.
311
 
                                        if d.nComp == nGrayComponent {
312
 
                                                idct(d.img1.Pix[8*(my*d.img1.Stride+mx):], d.img1.Stride, &b)
313
 
                                        } else {
314
 
                                                switch i {
315
 
                                                case 0:
316
 
                                                        mx0 := h0*mx + (j % 2)
317
 
                                                        my0 := v0*my + (j / 2)
318
 
                                                        idct(d.img3.Y[8*(my0*d.img3.YStride+mx0):], d.img3.YStride, &b)
319
 
                                                case 1:
320
 
                                                        idct(d.img3.Cb[8*(my*d.img3.CStride+mx):], d.img3.CStride, &b)
321
 
                                                case 2:
322
 
                                                        idct(d.img3.Cr[8*(my*d.img3.CStride+mx):], d.img3.CStride, &b)
323
 
                                                }
324
 
                                        }
325
 
                                } // for j
326
 
                        } // for i
327
 
                        mcu++
328
 
                        if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
329
 
                                // A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input,
330
 
                                // but this one assumes well-formed input, and hence the restart marker follows immediately.
331
 
                                _, err := io.ReadFull(d.r, d.tmp[0:2])
332
 
                                if err != nil {
333
 
                                        return err
334
 
                                }
335
 
                                if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
336
 
                                        return FormatError("bad RST marker")
337
 
                                }
338
 
                                expectedRST++
339
 
                                if expectedRST == rst7Marker+1 {
340
 
                                        expectedRST = rst0Marker
341
 
                                }
342
 
                                // Reset the Huffman decoder.
343
 
                                d.b = bits{}
344
 
                                // Reset the DC components, as per section F.2.1.3.1.
345
 
                                dc = [nColorComponent]int{}
346
 
                        }
347
 
                } // for mx
348
 
        } // for my
349
 
 
350
 
        return nil
351
 
}
352
 
 
353
212
// Specified in section B.2.4.4.
354
213
func (d *decoder) processDRI(n int) error {
355
214
        if n != 2 {
386
245
                if err != nil {
387
246
                        return nil, err
388
247
                }
389
 
                if d.tmp[0] != 0xff {
390
 
                        return nil, FormatError("missing 0xff marker start")
 
248
                for d.tmp[0] != 0xff {
 
249
                        // Strictly speaking, this is a format error. However, libjpeg is
 
250
                        // liberal in what it accepts. As of version 9, next_marker in
 
251
                        // jdmarker.c treats this as a warning (JWRN_EXTRANEOUS_DATA) and
 
252
                        // continues to decode the stream. Even before next_marker sees
 
253
                        // extraneous data, jpeg_fill_bit_buffer in jdhuff.c reads as many
 
254
                        // bytes as it can, possibly past the end of a scan's data. It
 
255
                        // effectively puts back any markers that it overscanned (e.g. an
 
256
                        // "\xff\xd9" EOI marker), but it does not put back non-marker data,
 
257
                        // and thus it can silently ignore a small number of extraneous
 
258
                        // non-marker bytes before next_marker has a chance to see them (and
 
259
                        // print a warning).
 
260
                        //
 
261
                        // We are therefore also liberal in what we accept. Extraneous data
 
262
                        // is silently ignored.
 
263
                        //
 
264
                        // This is similar to, but not exactly the same as, the restart
 
265
                        // mechanism within a scan (the RST[0-7] markers).
 
266
                        //
 
267
                        // Note that extraneous 0xff bytes in e.g. SOS data are escaped as
 
268
                        // "\xff\x00", and so are detected a little further down below.
 
269
                        d.tmp[0] = d.tmp[1]
 
270
                        d.tmp[1], err = d.r.ReadByte()
 
271
                        if err != nil {
 
272
                                return nil, err
 
273
                        }
391
274
                }
392
275
                marker := d.tmp[1]
 
276
                if marker == 0 {
 
277
                        // Treat "\xff\x00" as extraneous data.
 
278
                        continue
 
279
                }
 
280
                for marker == 0xff {
 
281
                        // Section B.1.1.2 says, "Any marker may optionally be preceded by any
 
282
                        // number of fill bytes, which are bytes assigned code X'FF'".
 
283
                        marker, err = d.r.ReadByte()
 
284
                        if err != nil {
 
285
                                return nil, err
 
286
                        }
 
287
                }
393
288
                if marker == eoiMarker { // End Of Image.
394
289
                        break
395
290
                }
 
291
                if rst0Marker <= marker && marker <= rst7Marker {
 
292
                        // Figures B.2 and B.16 of the specification suggest that restart markers should
 
293
                        // only occur between Entropy Coded Segments and not after the final ECS.
 
294
                        // However, some encoders may generate incorrect JPEGs with a final restart
 
295
                        // marker. That restart marker will be seen here instead of inside the processSOS
 
296
                        // method, and is ignored as a harmless error. Restart markers have no extra data,
 
297
                        // so we check for this before we read the 16-bit length of the segment.
 
298
                        continue
 
299
                }
396
300
 
397
301
                // Read the 16-bit length of the segment. The value includes the 2 bytes for the
398
302
                // length itself, so we subtract 2 to get the number of remaining bytes.
406
310
                }
407
311
 
408
312
                switch {
409
 
                case marker == sof0Marker: // Start Of Frame (Baseline).
 
313
                case marker == sof0Marker || marker == sof2Marker: // Start Of Frame.
 
314
                        d.progressive = marker == sof2Marker
410
315
                        err = d.processSOF(n)
411
316
                        if configOnly {
412
317
                                return nil, err
413
318
                        }
414
 
                case marker == sof2Marker: // Start Of Frame (Progressive).
415
 
                        err = UnsupportedError("progressive mode")
416
319
                case marker == dhtMarker: // Define Huffman Table.
417
320
                        err = d.processDHT(n)
418
321
                case marker == dqtMarker: // Define Quantization Table.
421
324
                        err = d.processSOS(n)
422
325
                case marker == driMarker: // Define Restart Interval.
423
326
                        err = d.processDRI(n)
424
 
                case marker >= app0Marker && marker <= app15Marker || marker == comMarker: // APPlication specific, or COMment.
 
327
                case app0Marker <= marker && marker <= app15Marker || marker == comMarker: // APPlication specific, or COMment.
425
328
                        err = d.ignore(n)
426
329
                default:
427
330
                        err = UnsupportedError("unknown marker")