script: add truncate-progressive-jpeg.go
"script/confirm-matches-djpeg.sh test/data/*.jpeg" shows differences on "test/data/peacock.progressive.scan00[0-8].jpeg".
No files matched your search
@@ -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")
|
||||||
|
}
|
||||||
@@ -146,16 +146,19 @@ pub struct decoder? implements base.image_decoder(
|
|||||||
// is a special case, meaning the scan holds (8 - Al) bits.
|
// is a special case, meaning the scan holds (8 - Al) bits.
|
||||||
//
|
//
|
||||||
// The test/data/peacock.progressive.jpeg file has 10 scans:
|
// The test/data/peacock.progressive.jpeg file has 10 scans:
|
||||||
// 1. Ss = 0 Se = 0 Ah = 0 Al = 1 3 components (Y, Cb, Cr)
|
// 0: 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)
|
// 1: Ss = 1 Se = 5 Ah = 0 Al = 2 1 component (Y)
|
||||||
// 3. Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cr)
|
// 2: Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cr)
|
||||||
// 4. Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cb)
|
// 3: Ss = 1 Se = 63 Ah = 0 Al = 1 1 component (Cb)
|
||||||
// 5. Ss = 6 Se = 63 Ah = 0 Al = 2 1 component (Y)
|
// 4: Ss = 6 Se = 63 Ah = 0 Al = 2 1 component (Y)
|
||||||
// 6. Ss = 1 Se = 63 Ah = 2 Al = 1 1 component (Y)
|
// 5: 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)
|
// 6: 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)
|
// 7: Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cr)
|
||||||
// 9. Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cb)
|
// 8: Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Cb)
|
||||||
// 10. Ss = 1 Se = 63 Ah = 1 Al = 0 1 component (Y)
|
// 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
|
// The first scan holds the high 7 bits of all components' DC
|
||||||
// coefficients.
|
// coefficients.
|
||||||
|
|||||||
@@ -126,6 +126,7 @@ image](https://commons.wikimedia.org/wiki/File:Pavo_Real_Venezolano.jpg),
|
|||||||
Dedication":
|
Dedication":
|
||||||
- `wget --quiet https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Pavo_Real_Venezolano.jpg/1024px-Pavo_Real_Venezolano.jpg`
|
- `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 1024px-Pavo_Real_Venezolano.jpg -resize 100x75 peacock.ppm`
|
||||||
|
- `convert peacock.ppm peacock.png`
|
||||||
- `rm 1024px-Pavo_Real_Venezolano.jpg`
|
- `rm 1024px-Pavo_Real_Venezolano.jpg`
|
||||||
- `cjpeg peacock.ppm > peacock.default.jpeg`
|
- `cjpeg peacock.ppm > peacock.default.jpeg`
|
||||||
- `cjpeg -optimize peacock.ppm > peacock.optimize.jpeg`
|
- `cjpeg -optimize peacock.ppm > peacock.optimize.jpeg`
|
||||||
@@ -137,6 +138,7 @@ Dedication":
|
|||||||
- `cjpeg -sample 2x1 peacock.ppm > peacock.s2x1-422.jpeg`
|
- `cjpeg -sample 2x1 peacock.ppm > peacock.s2x1-422.jpeg`
|
||||||
- `cjpeg -sample 1x1 peacock.ppm > peacock.s1x1-444.jpeg`
|
- `cjpeg -sample 1x1 peacock.ppm > peacock.s1x1-444.jpeg`
|
||||||
- `cjpeg -sample 2x2,2x1,1x1 peacock.ppm > peacock.s-weird.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.
|
`pi.txt` contains the digits of pi.
|
||||||
|
|
||||||
|
|||||||
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 405 B |
|
After Width: | Height: | Size: 718 B |
|
After Width: | Height: | Size: 816 B |
|
After Width: | Height: | Size: 910 B |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
@@ -84,8 +84,19 @@ db2733f5 test/data/muybridge.gif
|
|||||||
d42a13cd test/data/peacock.default.jpeg
|
d42a13cd test/data/peacock.default.jpeg
|
||||||
d49bab2d test/data/peacock.grayscale.jpeg
|
d49bab2d test/data/peacock.grayscale.jpeg
|
||||||
d42a13cd test/data/peacock.optimize.jpeg
|
d42a13cd test/data/peacock.optimize.jpeg
|
||||||
|
bdf8cc70 test/data/peacock.png
|
||||||
bdf8cc70 test/data/peacock.ppm
|
bdf8cc70 test/data/peacock.ppm
|
||||||
d42a13cd test/data/peacock.progressive.jpeg
|
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
|
951676cc test/data/peacock.q30.jpeg
|
||||||
56835223 test/data/peacock.q99.jpeg
|
56835223 test/data/peacock.q99.jpeg
|
||||||
1543f677 test/data/peacock.s-weird.jpeg
|
1543f677 test/data/peacock.s-weird.jpeg
|
||||||
|
|||||||