| 1 | package imagefile |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | ) |
| 7 | |
| 8 | // The JPEG walker (§5). It keeps every marker segment but two kinds: |
| 9 | // APPn segments other than JFIF's APP0, an ICC profile's APP2 and |
| 10 | // Adobe's APP14; and comments. So every SOFn, DQT, DHT, DRI, DNL, DAC |
| 11 | // and SOS stays, with its scan data and restart markers. The JFIF |
| 12 | // thumbnail goes, as does JFXX. The file ends at EOI, which drops Motion |
| 13 | // Photo video, MPF's second images, gain maps and vendor trailers. |
| 14 | // EXIF goes too; its orientation, when not 1, is written back as a |
| 15 | // minimal APP1 holding that one tag, so a phone's photo stays upright. |
| 16 | |
| 17 | const ( |
| 18 | markerSOI = 0xd8 |
| 19 | markerEOI = 0xd9 |
| 20 | markerSOS = 0xda |
| 21 | markerAPP0 = 0xe0 |
| 22 | markerAPP1 = 0xe1 |
| 23 | markerAPP2 = 0xe2 |
| 24 | markerAPPE = 0xee |
| 25 | markerAPPF = 0xef |
| 26 | markerCOM = 0xfe |
| 27 | |
| 28 | // adobeHeader is APP14's whole meaning: "Adobe", version, flags0, |
| 29 | // flags1, transform. |
| 30 | adobeHeader = 12 |
| 31 | ) |
| 32 | |
| 33 | // jpegWalk is one walk's state. |
| 34 | type jpegWalk struct { |
| 35 | raw []byte |
| 36 | segments [][]byte |
| 37 | orientation int |
| 38 | frame, scan bool |
| 39 | } |
| 40 | |
| 41 | func cleanJPEG(raw []byte) ([]byte, int, error) { |
| 42 | if len(raw) < 4 || raw[0] != 0xff || raw[1] != markerSOI { |
| 43 | return nil, 0, ErrMalformed |
| 44 | } |
| 45 | walk := &jpegWalk{raw: raw, orientation: 1} |
| 46 | for at := 2; ; { |
| 47 | marker, start, err := walk.marker(at) |
| 48 | if err != nil { |
| 49 | return nil, 0, err |
| 50 | } |
| 51 | if marker == markerEOI { |
| 52 | return walk.finish() |
| 53 | } |
| 54 | if at, err = walk.segment(marker, start); err != nil { |
| 55 | return nil, 0, err |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // marker reads the marker at raw[at], after any fill bytes. |
| 61 | func (walk *jpegWalk) marker(at int) (byte, int, error) { |
| 62 | raw := walk.raw |
| 63 | if at >= len(raw) || raw[at] != 0xff { |
| 64 | return 0, 0, ErrMalformed |
| 65 | } |
| 66 | start := at |
| 67 | for at < len(raw) && raw[at] == 0xff { |
| 68 | at++ |
| 69 | } |
| 70 | if at >= len(raw) || raw[at] == 0 { |
| 71 | return 0, 0, ErrMalformed |
| 72 | } |
| 73 | return raw[at], start, nil |
| 74 | } |
| 75 | |
| 76 | // segment reads the segment starting at raw[start] and keeps it or not. |
| 77 | // It returns where the next marker starts. |
| 78 | func (walk *jpegWalk) segment(marker byte, start int) (int, error) { |
| 79 | raw := walk.raw |
| 80 | at := start |
| 81 | for raw[at] == 0xff { |
| 82 | at++ |
| 83 | } |
| 84 | at++ // past the marker byte |
| 85 | if standalone(marker) { |
| 86 | walk.segments = append(walk.segments, []byte{0xff, marker}) |
| 87 | return at, nil |
| 88 | } |
| 89 | if at+2 > len(raw) { |
| 90 | return 0, ErrMalformed |
| 91 | } |
| 92 | length := int(binary.BigEndian.Uint16(raw[at:])) |
| 93 | end := at + length |
| 94 | if length < 2 || end > len(raw) { |
| 95 | return 0, ErrMalformed |
| 96 | } |
| 97 | payload := raw[at+2 : end] |
| 98 | if marker == markerSOS { |
| 99 | walk.scan = true |
| 100 | scanned, err := scanEnd(raw, end) |
| 101 | if err != nil { |
| 102 | return 0, err |
| 103 | } |
| 104 | walk.keep(marker, raw[at-2:scanned], payload) |
| 105 | return scanned, nil |
| 106 | } |
| 107 | walk.keep(marker, raw[at-2:end], payload) |
| 108 | return end, nil |
| 109 | } |
| 110 | |
| 111 | // standalone markers have no length: RSTn and TEM. |
| 112 | func standalone(marker byte) bool { |
| 113 | return marker >= 0xd0 && marker <= 0xd7 || marker == 0x01 |
| 114 | } |
| 115 | |
| 116 | // keep decides one segment: dropped, kept as it is, or (JFIF) kept |
| 117 | // without its thumbnail. whole is the segment as read, from its marker |
| 118 | // to its end (a scan's entropy-coded bytes included), never copied. |
| 119 | func (walk *jpegWalk) keep(marker byte, whole, payload []byte) { |
| 120 | isFrame := marker >= 0xc0 && marker <= 0xcf && marker != 0xc4 && |
| 121 | marker != 0xc8 && marker != 0xcc |
| 122 | walk.frame = walk.frame || isFrame |
| 123 | switch { |
| 124 | case marker == markerCOM: |
| 125 | return |
| 126 | case marker == markerAPP1 && bytes.HasPrefix(payload, |
| 127 | []byte("Exif\x00\x00")): |
| 128 | walk.orientation = orientation(payload[6:]) |
| 129 | return |
| 130 | case marker == markerAPP0 && bytes.HasPrefix(payload, |
| 131 | []byte("JFIF\x00")) && len(payload) >= 14: |
| 132 | whole = segmentBytes(marker, |
| 133 | append(append([]byte{}, payload[:12]...), 0, 0)) |
| 134 | case marker == markerAPPE && keptApp(marker, payload) && |
| 135 | len(payload) > adobeHeader: |
| 136 | // "Adobe", its version, two flag words and the transform: what |
| 137 | // a CMYK picture needs, and nothing after it (Fable L10). |
| 138 | whole = segmentBytes(marker, payload[:adobeHeader]) |
| 139 | case marker >= markerAPP0 && marker <= markerAPPF && |
| 140 | !keptApp(marker, payload): |
| 141 | return |
| 142 | } |
| 143 | walk.segments = append(walk.segments, whole) |
| 144 | } |
| 145 | |
| 146 | // keptApp is the two APPn segments kept as they are: an ICC profile and |
| 147 | // Adobe's colour transform, which a CMYK JPEG needs. |
| 148 | func keptApp(marker byte, payload []byte) bool { |
| 149 | return marker == markerAPP2 && bytes.HasPrefix(payload, |
| 150 | []byte("ICC_PROFILE\x00")) || |
| 151 | marker == markerAPPE && bytes.HasPrefix(payload, []byte("Adobe")) |
| 152 | } |
| 153 | |
| 154 | func segmentBytes(marker byte, payload []byte) []byte { |
| 155 | out := []byte{0xff, marker, 0, 0} |
| 156 | binary.BigEndian.PutUint16(out[2:], uint16(len(payload)+2)) |
| 157 | return append(out, payload...) |
| 158 | } |
| 159 | |
| 160 | // scanEnd is where a scan's entropy-coded data ends: the first marker |
| 161 | // that is not a stuffed zero, a restart marker or a fill byte. |
| 162 | func scanEnd(raw []byte, at int) (int, error) { |
| 163 | for at < len(raw) { |
| 164 | if raw[at] != 0xff { |
| 165 | at++ |
| 166 | continue |
| 167 | } |
| 168 | if at+1 >= len(raw) { |
| 169 | return 0, ErrMalformed |
| 170 | } |
| 171 | next := raw[at+1] |
| 172 | switch { |
| 173 | case next == 0 || next >= 0xd0 && next <= 0xd7: |
| 174 | at += 2 |
| 175 | case next == 0xff: |
| 176 | at++ |
| 177 | default: |
| 178 | return at, nil |
| 179 | } |
| 180 | } |
| 181 | return 0, ErrMalformed |
| 182 | } |
| 183 | |
| 184 | // finish assembles the kept segments, the orientation APP1 after |
| 185 | // JFIF's APP0 when there is one, and EOI. |
| 186 | func (walk *jpegWalk) finish() ([]byte, int, error) { |
| 187 | if !walk.frame || !walk.scan { |
| 188 | return nil, 0, ErrMalformed |
| 189 | } |
| 190 | segments := walk.segments |
| 191 | if walk.orientation != 1 { |
| 192 | at := 0 |
| 193 | if len(segments) > 0 && segments[0][1] == markerAPP0 { |
| 194 | at = 1 |
| 195 | } |
| 196 | app1 := segmentBytes(markerAPP1, orientationExif(walk.orientation)) |
| 197 | segments = append(segments[:at:at], |
| 198 | append([][]byte{app1}, segments[at:]...)...) |
| 199 | } |
| 200 | out := append(make([]byte, 0, len(walk.raw)+64), 0xff, markerSOI) |
| 201 | for _, segment := range segments { |
| 202 | out = append(out, segment...) |
| 203 | } |
| 204 | return append(out, 0xff, markerEOI), walk.orientation, nil |
| 205 | } |
| 206 | |