bzip2_gif_roundtrip.mx raw
1 // bzip2 decompression and a GIF encode/decode round trip.
2 //
3 // Neither package had a fixture. compress/bzip2 is not imported by anything in
4 // the tree, and image/gif's encoder had never been exercised: it also carried an
5 // impossible type assertion (a color.Model can never BE a color.Palette in
6 // Moxie, where a slice type cannot have methods) that the legacy compiler
7 // rejected and stage4 accepted, so the package only compiled on one of the two.
8 //
9 // The bzip2 stream is the system bzip2's output for the sentence checked below.
10 // The GIF half compares decoded pixels by RGBA() value rather than interface
11 // identity, because two color.Color values are pointers.
12 //
13 // usage: bzip2_gif_roundtrip exit 0 when both halves hold
14 package main
15 import (
16 "bytes"
17 "compress/bzip2"
18 "image"
19 "image/color"
20 "image/gif"
21 "io"
22 )
23 func main() {
24 // Termination: a batch program must still be stoppable, so the entry
25 // handles the channel the runtime closes on SIGINT/SIGTERM.
26 select {
27 case <-shutdown:
28 return
29 default:
30 }
31
32 bz2Stream := []byte{0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xed, 0xd3, 0x36, 0x51, 0x00, 0x00, 0x09, 0xd9, 0x80, 0x00, 0x10, 0x40, 0x04, 0x10, 0x00, 0x36, 0x64, 0xdc, 0xb0, 0x20, 0x00, 0x31, 0x4c, 0x98, 0x99, 0x06, 0x46, 0x11, 0x31, 0x47, 0xb4, 0x91, 0xe6, 0xa8, 0x7a, 0x90, 0x61, 0x64, 0x0a, 0x8b, 0xdb, 0xb2, 0xb4, 0xdf, 0x75, 0x5e, 0x60, 0xe3, 0xd3, 0x38, 0xa8, 0xf9, 0x81, 0x2c, 0xba, 0x3a, 0x13, 0xf8, 0xbb, 0x92, 0x29, 0xc2, 0x84, 0x87, 0x6e, 0x99, 0xb2, 0x88}
33 // bzip2: a stream produced by the system bzip2 for a known sentence.
34 r := bzip2.NewReader(bytes.NewReader(bz2Stream))
35 got, rerr := io.ReadAll(r)
36 if rerr != nil {
37 println("bzip2 read failed:", rerr.Error())
38 return
39 }
40 want := "hello world, this is a bzip2 probe payload\n"
41 if string(got) != want {
42 println("bzip2 mismatch, got len", int32(len(got)).String())
43 println(string(got))
44 return
45 }
46 println("bzip2 ok")
47 // gif: encode a palette image and decode it back.
48 pal := []color.Color{
49 color.RGBA{R: 0, G: 0, B: 0, A: 255},
50 color.RGBA{R: 255, G: 0, B: 0, A: 255},
51 color.RGBA{R: 0, G: 255, B: 0, A: 255},
52 color.RGBA{R: 0, G: 0, B: 255, A: 255},
53 }
54 rect := image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{16, 9}}
55 img := image.NewPaletted(rect, pal)
56 for y := int32(0); y < 9; y++ {
57 for x := int32(0); x < 16; x++ {
58 img.SetColorIndex(x, y, uint8((x+y)%4))
59 }
60 }
61 var buf bytes.Buffer
62 if err := gif.Encode(&buf, img, nil); err != nil {
63 println("gif encode failed:", err.Error())
64 return
65 }
66 println("gif bytes", int32(buf.Len()).String())
67 back, derr := gif.Decode(&buf)
68 if derr != nil {
69 println("gif decode failed:", derr.Error())
70 return
71 }
72 b := back.Bounds()
73 if b.Dx() != 16 || b.Dy() != 9 {
74 println("gif bounds mismatch")
75 return
76 }
77 for y := int32(0); y < 9; y++ {
78 for x := int32(0); x < 16; x++ {
79 br, bg, bb, ba := back.At(x, y).RGBA()
80 wr, wg, wb, wa := img.At(x, y).RGBA()
81 if br != wr || bg != wg || bb != wb || ba != wa {
82 println("gif pixel mismatch at", x, y)
83 println(" have", int32(br>>8).String(), int32(bg>>8).String(), int32(bb>>8).String(), int32(ba>>8).String())
84 println(" want", int32(wr>>8).String(), int32(wg>>8).String(), int32(wb>>8).String(), int32(wa>>8).String())
85 return
86 }
87 }
88 }
89 println("gif round trip ok")
90 }