oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package attachtest makes attachment fixtures for tests: a PNG, and a
2// JPEG as a phone writes it, with an EXIF orientation and GPS position.
3package attachtest
4
5import (
6 "bytes"
7 "encoding/binary"
8 "hash/crc32"
9 "image"
10 "image/color"
11 "image/jpeg"
12 "image/png"
13)
14
15// gpsMark is the latitude's numerators, bytes no small fixture holds by
16// chance.
17var gpsMark = []uint32{424242, 171717, 999999}
18
19// PNG is a w by h PNG.
20func PNG(w, h int) []byte {
21 img := image.NewRGBA(image.Rect(0, 0, w, h))
22 for y := 0; y < h; y++ {
23 for x := 0; x < w; x++ {
24 img.Set(x, y, color.RGBA{uint8(x), uint8(y), 90, 255})
25 }
26 }
27 var b bytes.Buffer
28 _ = png.Encode(&b, img)
29 return b.Bytes()
30}
31
32// Photo is a w by h JPEG with EXIF orientation and a GPS latitude.
33func Photo(w, h int, orientation uint16) []byte {
34 img := image.NewRGBA(image.Rect(0, 0, w, h))
35 for y := 0; y < h; y++ {
36 for x := 0; x < w; x++ {
37 img.Set(x, y, color.RGBA{200, uint8(x), uint8(y), 255})
38 }
39 }
40 var b bytes.Buffer
41 _ = jpeg.Encode(&b, img, &jpeg.Options{Quality: 90})
42 raw := b.Bytes()
43 exif := append([]byte("Exif\x00\x00"), tiff(orientation)...)
44 seg := binary.BigEndian.AppendUint16([]byte{0xff, 0xe1}, uint16(len(exif)+2))
45 out := append([]byte{}, raw[:2]...)
46 out = append(out, append(seg, exif...)...)
47 return append(out, raw[2:]...)
48}
49
50// HasGPS reports whether b still holds the fixture's latitude.
51func HasGPS(b []byte) bool {
52 for _, n := range gpsMark {
53 if bytes.Contains(b, binary.LittleEndian.AppendUint32(nil, n)) {
54 return true
55 }
56 }
57 return false
58}
59
60func tiff(orientation uint16) []byte {
61 le := binary.LittleEndian
62 b := le.AppendUint32([]byte("II*\x00"), 8)
63 b = le.AppendUint16(b, 2)
64 b = le.AppendUint16(le.AppendUint16(b, 0x0112), 3)
65 b = le.AppendUint16(le.AppendUint16(le.AppendUint32(b, 1), orientation), 0)
66 gps := uint32(8 + 2 + 2*12 + 4)
67 b = le.AppendUint16(le.AppendUint16(b, 0x8825), 4)
68 b = le.AppendUint32(le.AppendUint32(le.AppendUint32(b, 1), gps), 0)
69 data := gps + 2 + 12 + 4
70 b = le.AppendUint16(b, 1)
71 b = le.AppendUint16(le.AppendUint16(b, 2), 5)
72 b = le.AppendUint32(le.AppendUint32(le.AppendUint32(b, 3), data), 0)
73 for _, n := range gpsMark {
74 b = le.AppendUint32(le.AppendUint32(b, n), 1)
75 }
76 return b
77}
78
79// PNGHeader is a PNG that claims w by h: a signature, IHDR and IEND only.
80func PNGHeader(w, h uint32) []byte {
81 ihdr := binary.BigEndian.AppendUint32(nil, w)
82 ihdr = append(binary.BigEndian.AppendUint32(ihdr, h), 8, 6, 0, 0, 0)
83 out := []byte("\x89PNG\r\n\x1a\n")
84 out = append(out, pngChunk("IHDR", ihdr)...)
85 return append(out, pngChunk("IEND", nil)...)
86}
87
88func pngChunk(kind string, data []byte) []byte {
89 out := binary.BigEndian.AppendUint32(nil, uint32(len(data)))
90 out = append(append(out, kind...), data...)
91 return binary.BigEndian.AppendUint32(out, crc32.ChecksumIEEE(append([]byte(kind), data...)))
92}
93