diff --git a/script/truncate-progressive-jpeg.go b/script/truncate-progressive-jpeg.go new file mode 100644 index 00000000..3ae97d40 --- /dev/null +++ b/script/truncate-progressive-jpeg.go @@ -0,0 +1,108 @@ +// Copyright 2023 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build ignore +// +build ignore + +package main + +// truncate-progressive-jpeg.go saves truncated editions of a JPEG, cutting +// just after every SOS (Start Of Scan) marker, appending an EOI (End Of Image) +// marker after the cut. +// +// Usage: go run truncate-progressive-jpeg.go foo.jpeg + +import ( + "fmt" + "os" + "strings" +) + +func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { + if len(os.Args) < 2 { + return fmt.Errorf("usage: progname filename.jpeg") + } + src, err := os.ReadFile(os.Args[1]) + if err != nil { + return err + } + prefix, suffix := os.Args[1], ".jpeg" + if strings.HasSuffix(prefix, ".jpg") { + prefix = prefix[:len(prefix)-4] + suffix = ".jpg" + } else if strings.HasSuffix(prefix, ".jpeg") { + prefix = prefix[:len(prefix)-5] + } + + if len(src) < 2 { + return fmt.Errorf("invalid JPEG") + } + scanNumber := 0 + for pos := 0; pos <= (len(src) - 2); { + if src[pos] != 0xFF { + return fmt.Errorf("invalid JPEG") + } + marker := src[pos+1] + pos += 2 + switch { + case (0xD0 <= marker) && (marker < 0xD9): + // RSTn (Restart) and SOI (Start Of Image) markers have no payload. + continue + case marker == 0xD9: + // EOI (End Of Image) marker. + return nil + } + + if pos >= (len(src) - 2) { + return fmt.Errorf("invalid JPEG") + } + payloadLength := (int(src[pos]) << 8) | int(src[pos+1]) + pos += payloadLength + if (payloadLength < 2) || (pos < 0) || (pos > len(src)) { + return fmt.Errorf("invalid JPEG") + } + + if marker != 0xDA { // SOS (Start Of Scan) marker. + continue + } + for pos < len(src) { + if src[pos] != 0xFF { + pos += 1 + } else if pos >= (len(src) - 1) { + return fmt.Errorf("invalid JPEG") + } else if src[pos+1] != 0x00 { + break + } else { + pos += 2 + } + } + outName := fmt.Sprintf("%s.scan%03d%s", prefix, scanNumber, suffix) + scanNumber++ + data := ([]byte)(nil) + data = append(data, src[:pos]...) + data = append(data, "\xFF\xD9"...) // Append an EOI marker. + if err := os.WriteFile(outName, data, 0666); err != nil { + return err + } + fmt.Printf("Wrote %s (cut at 0x%08X = %6d bytes).\n", outName, pos, pos) + } + return fmt.Errorf("invalid JPEG") +} diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index a7790849..4f8bf3df 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -146,16 +146,19 @@ pub struct decoder? implements base.image_decoder( // is a special case, meaning the scan holds (8 - Al) bits. // // The test/data/peacock.progressive.jpeg file has 10 scans: - // 1. Ss = 0 Se = 0 Ah = 0 Al = 1 3 components (Y, Cb, Cr) - // 2. Ss = 1 Se = 5 Ah = 0 Al = 2 1 component (Y) - // 3. Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cr) - // 4. Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cb) - // 5. Ss = 6 Se = 63 Ah = 0 Al = 2 1 component (Y) - // 6. Ss = 1 Se = 63 Ah = 2 Al = 1 1 component (Y) - // 7. Ss = 0 Se = 0 Ah = 1 Al = 0 3 components (Y, Cb, Cr) - // 8. Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cr) - // 9. Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cb) - // 10. Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Y) + // 0: Ss = 0 Se = 0 Ah = 0 Al = 1 3 components (Y, Cb, Cr) + // 1: Ss = 1 Se = 5 Ah = 0 Al = 2 1 component (Y) + // 2: Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cr) + // 3: Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cb) + // 4: Ss = 6 Se = 63 Ah = 0 Al = 2 1 component (Y) + // 5: Ss = 1 Se = 63 Ah = 2 Al = 1 1 component (Y) + // 6: Ss = 0 Se = 0 Ah = 1 Al = 0 3 components (Y, Cb, Cr) + // 7: Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cr) + // 8: Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cb) + // 9: Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Y) + // + // This matches /usr/bin/cjpeg's default script for progressive YCbCr: + // https://github.com/libjpeg-turbo/libjpeg-turbo/blob/2192560d74e6e6cf99dd05928885573be00a8208/jcparam.c#L517-L535 // // The first scan holds the high 7 bits of all components' DC // coefficients. diff --git a/test/data/README.md b/test/data/README.md index d47a8e82..58dacc9d 100644 --- a/test/data/README.md +++ b/test/data/README.md @@ -126,6 +126,7 @@ image](https://commons.wikimedia.org/wiki/File:Pavo_Real_Venezolano.jpg), Dedication": - `wget --quiet https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Pavo_Real_Venezolano.jpg/1024px-Pavo_Real_Venezolano.jpg` - `convert 1024px-Pavo_Real_Venezolano.jpg -resize 100x75 peacock.ppm` + - `convert peacock.ppm peacock.png` - `rm 1024px-Pavo_Real_Venezolano.jpg` - `cjpeg peacock.ppm > peacock.default.jpeg` - `cjpeg -optimize peacock.ppm > peacock.optimize.jpeg` @@ -137,6 +138,7 @@ Dedication": - `cjpeg -sample 2x1 peacock.ppm > peacock.s2x1-422.jpeg` - `cjpeg -sample 1x1 peacock.ppm > peacock.s1x1-444.jpeg` - `cjpeg -sample 2x2,2x1,1x1 peacock.ppm > peacock.s-weird.jpeg` + - `go run ../../script/truncate-progressive-jpeg.go peacock.progressive.jpeg` `pi.txt` contains the digits of pi. diff --git a/test/data/peacock.png b/test/data/peacock.png new file mode 100644 index 00000000..e3037b58 Binary files /dev/null and b/test/data/peacock.png differ diff --git a/test/data/peacock.progressive.scan000.jpeg b/test/data/peacock.progressive.scan000.jpeg new file mode 100644 index 00000000..5cc37e00 Binary files /dev/null and b/test/data/peacock.progressive.scan000.jpeg differ diff --git a/test/data/peacock.progressive.scan001.jpeg b/test/data/peacock.progressive.scan001.jpeg new file mode 100644 index 00000000..983328bc Binary files /dev/null and b/test/data/peacock.progressive.scan001.jpeg differ diff --git a/test/data/peacock.progressive.scan002.jpeg b/test/data/peacock.progressive.scan002.jpeg new file mode 100644 index 00000000..e065a63e Binary files /dev/null and b/test/data/peacock.progressive.scan002.jpeg differ diff --git a/test/data/peacock.progressive.scan003.jpeg b/test/data/peacock.progressive.scan003.jpeg new file mode 100644 index 00000000..6c63c1b5 Binary files /dev/null and b/test/data/peacock.progressive.scan003.jpeg differ diff --git a/test/data/peacock.progressive.scan004.jpeg b/test/data/peacock.progressive.scan004.jpeg new file mode 100644 index 00000000..4bb46e9b Binary files /dev/null and b/test/data/peacock.progressive.scan004.jpeg differ diff --git a/test/data/peacock.progressive.scan005.jpeg b/test/data/peacock.progressive.scan005.jpeg new file mode 100644 index 00000000..32759193 Binary files /dev/null and b/test/data/peacock.progressive.scan005.jpeg differ diff --git a/test/data/peacock.progressive.scan006.jpeg b/test/data/peacock.progressive.scan006.jpeg new file mode 100644 index 00000000..3ea0a17f Binary files /dev/null and b/test/data/peacock.progressive.scan006.jpeg differ diff --git a/test/data/peacock.progressive.scan007.jpeg b/test/data/peacock.progressive.scan007.jpeg new file mode 100644 index 00000000..3f395778 Binary files /dev/null and b/test/data/peacock.progressive.scan007.jpeg differ diff --git a/test/data/peacock.progressive.scan008.jpeg b/test/data/peacock.progressive.scan008.jpeg new file mode 100644 index 00000000..83ea7ac8 Binary files /dev/null and b/test/data/peacock.progressive.scan008.jpeg differ diff --git a/test/data/peacock.progressive.scan009.jpeg b/test/data/peacock.progressive.scan009.jpeg new file mode 100644 index 00000000..9c3733ad Binary files /dev/null and b/test/data/peacock.progressive.scan009.jpeg differ diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index bd7e711a..1485bb0e 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -84,8 +84,19 @@ db2733f5 test/data/muybridge.gif d42a13cd test/data/peacock.default.jpeg d49bab2d test/data/peacock.grayscale.jpeg d42a13cd test/data/peacock.optimize.jpeg +bdf8cc70 test/data/peacock.png bdf8cc70 test/data/peacock.ppm d42a13cd test/data/peacock.progressive.jpeg +768072d3 test/data/peacock.progressive.scan000.jpeg +6b15534b test/data/peacock.progressive.scan001.jpeg +4073df79 test/data/peacock.progressive.scan002.jpeg +10395b34 test/data/peacock.progressive.scan003.jpeg +962a4d89 test/data/peacock.progressive.scan004.jpeg +be442911 test/data/peacock.progressive.scan005.jpeg +298e35bf test/data/peacock.progressive.scan006.jpeg +a6727c95 test/data/peacock.progressive.scan007.jpeg +206c9cab test/data/peacock.progressive.scan008.jpeg +d42a13cd test/data/peacock.progressive.scan009.jpeg 951676cc test/data/peacock.q30.jpeg 56835223 test/data/peacock.q99.jpeg 1543f677 test/data/peacock.s-weird.jpeg