1 // The progressive and CMYK decode paths, which no other fixture reaches: the
2 // encoder only writes baseline YCbCr, and Go's encoder refuses CMYK input, so
3 // both were unexercised until a JPEG corpus was decoded against Go 1.25.3.
4 //
5 // The progressive path is the one that exercises saved coefficient blocks:
6 // `progCoeffs` keeps every block between scans, refine() mutates the stored
7 // block in place, and reconstructProgressiveImage() runs the inverse DCT over
8 // all of them at the end. With `block` converted from an array to a slice it
9 // also exercises the allocation shape - a slice of blocks starts out as n
10 // empty headers, not as n blocks of storage, so newProgCoeffs has to allocate
11 // each one. Before that it panicked with "index out of range" on the first
12 // coefficient write.
13 //
14 // The CMYK path exercises the four-plane interleave in applyBlack: the
15 // channel table there was an array of {slice, stride} pairs.
16 //
17 // The expected pixels are Go 1.25.3's output for the same bytes.
18 package main
19 20 import (
21 "bytes"
22 "image"
23 "image/jpeg"
24 "os"
25 )
26 27 // unhex decodes a hex string. The JPEG inputs are embedded rather than read
28 // from disk so the fixture carries its own corpus.
29 func unhex(s string) (out []byte) {
30 out = []byte{:len(s) / 2}
31 var hi, lo byte
32 for i := 0; i < len(out); i++ {
33 hi = s[2*i]
34 lo = s[2*i+1]
35 if hi >= 'a' {
36 hi = hi - 'a' + 10
37 } else {
38 hi = hi - '0'
39 }
40 if lo >= 'a' {
41 lo = lo - 'a' + 10
42 } else {
43 lo = lo - '0'
44 }
45 out[i] = hi<<4 | lo
46 }
47 return out
48 }
49 50 func check(name string, got, want uint32) {
51 if got != want {
52 println("FAIL: " | name | ": got ", int32(got), " want ", int32(want))
53 os.Exit(1)
54 }
55 }
56 57 func px(img image.Image, x, y int32) (r, g, b uint32) {
58 rr, gg, bb, _ := img.At(x, y).RGBA()
59 return rr >> 8, gg >> 8, bb >> 8
60 }
61 62 // progressiveJPEG is a 37x29 4:2:0 progressive JPEG.
63 const progressiveJPEG = "ffd8ffe000104a46494600010100000100010000ffdb0043000503040404030504040405050506070c08070707070f0b0b090c110f1212110f111113161c1713141a1511111821181a1d1d1f1f1f13172224221e241c1e1f1effdb0043010505050706070e08080e1e1411141e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1effc2001108001d002503012200021101031101ffc400160001010100000000000000000000000000000406ffc400160101010100000000000000000000000000000702ffda000c03010002100310000001c8a46a8d5a415a412255327b52515251ffc400161001010100000000000000000000000000001211ffda0008010100010502a5294a5294a5294a529ad6b5afffc400161101010100000000000000000000000000001501ffda0008010301013f01a6a6a6a3aa3aa3afffc40017110003010000000000000000000000000000011561ffda0008010201013f0193849c24e1311311311fffc40014100100000000000000000000000000000040ffda0008010100063f0227ffc400161001010100000000000000000000000000001130ffda0008010100013f21dc0005294a53ffda000c0301000200030000001004107cf3cfffc400161101010100000000000000000000000000710020ffda0008010301013f10739e3fffc40014110100000000000000000000000000000020ffda0008010201013f10557fffc400161001010100000000000000000000000000710020ffda0008010100013f105294a5294a5294a5296aaaffd9"
64 65 // cmykJPG is a 37x29 four-component (CMYK) JPEG with Adobe APP14 metadata.
66 const cmykJPG = "ffd8ffee000e41646f626500640000000002ffdb0043000302020302020303030304030304050805050404050a070706080c0a0c0c0b0a0b0b0d0e12100d0e110e0b0b1016101113141515150c0f171816141812141514ffdb00430103040405040509050509140d0b0d1414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414ffc0001408001d002504011100021101031101041100ffc40017000101010100000000000000000000000000070806ffc400241000010009040300000000000000000000000102030415161751a114526392536191ffc40017010101010100000000000000000000000000080609ffc4001e11000103050101000000000000000000000002031401131651521591ffda000e040100021103110400003f00bdeb7d9bff00713b396768dfd5539b235bec7b89d8b42aa73646b7d8f713b168554e6c8d6fb1ee2762d0aa9cd91adf63dc4ec5a15539b27311269e35faa49033947469603bcd7e190ea33c59af5488934f1afd52339474203bcd7e0a8cf166bd52224d3c6bf548ce51d080ef35f82a33c59af5488934f1afd52339474203bcd7e0a8cf166bd52224d3c6bf548ce51d080ef35f82a33c59af5495492d4d88f84bf3ebb2c3c4dbe4a152ce1c092d4d88f827d763136f914b387024b53623e09f5d8c4dbe452ce1c092d4d88f827d763136f914b387024b53623e09f5d8c4dbe452ce1c1538732b60c85d51454068d6326b8db021ccad81754203424d71b6043995b02ea8406849ae36c08732b605d5080d0935c6d810e656c0baa101a126b8db07ffd9"
67 68 func main() {
69 // Termination: a batch program must still be stoppable, so the entry
70 // handles the channel the runtime closes on SIGINT/SIGTERM.
71 select {
72 case <-shutdown:
73 return
74 default:
75 }
76 77 pimg, perr := jpeg.Decode(bytes.NewReader(unhex(progressiveJPEG)))
78 if perr != nil {
79 println("FAIL: progressive decode error")
80 os.Exit(1)
81 }
82 pb := pimg.Bounds()
83 check("progressive width", uint32(pb.Dx()), 37)
84 check("progressive height", uint32(pb.Dy()), 29)
85 r, g, b := px(pimg, 0, 0)
86 check("progressive 0,0 r", r, 251)
87 check("progressive 0,0 g", g, 2)
88 check("progressive 0,0 b", b, 4)
89 r, g, b = px(pimg, 0, 28)
90 check("progressive 0,28 r", r, 2)
91 check("progressive 0,28 b", b, 250)
92 r, g, b = px(pimg, 18, 14)
93 check("progressive 18,14 r", r, 125)
94 check("progressive 18,14 b", b, 133)
95 r, g, b = px(pimg, 36, 28)
96 check("progressive 36,28 r", r, 2)
97 check("progressive 36,28 b", b, 250)
98 99 cimg, cerr := jpeg.Decode(bytes.NewReader(unhex(cmykJPG)))
100 if cerr != nil {
101 println("FAIL: cmyk decode error")
102 os.Exit(1)
103 }
104 cm, ok := cimg.(*image.CMYK)
105 if !ok {
106 println("FAIL: 4-component JPEG did not decode to CMYK")
107 os.Exit(1)
108 }
109 check("cmyk pix len", uint32(len(cm.Pix)), 37*29*4)
110 cb := cm.Bounds()
111 check("cmyk width", uint32(cb.Dx()), 37)
112 r, g, b = px(cimg, 0, 0)
113 check("cmyk 0,0 r", r, 255)
114 check("cmyk 0,0 g", g, 0)
115 check("cmyk 0,0 b", b, 0)
116 r, g, b = px(cimg, 0, 28)
117 check("cmyk 0,28 r", r, 1)
118 check("cmyk 0,28 b", b, 255)
119 r, g, b = px(cimg, 18, 14)
120 check("cmyk 18,14 r", r, 128)
121 check("cmyk 18,14 b", b, 125)
122 println("ok")
123 }
124