|
| 1 | +package gif |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + |
| 9 | + "github.com/donislawdev/TestingFilesGenerator/internal/core" |
| 10 | + "github.com/donislawdev/TestingFilesGenerator/internal/format" |
| 11 | +) |
| 12 | + |
| 13 | +// loopExtensionSize is the application extension the encoder writes after the |
| 14 | +// colour table of a picture that moves: introducer, label, block size, the |
| 15 | +// eleven bytes of NETSCAPE2.0, a three byte sub block with the loop count, and |
| 16 | +// the terminator. |
| 17 | +const loopExtensionSize = 3 + 11 + 4 + 1 |
| 18 | + |
| 19 | +// loopExtensionStart is how that extension opens. |
| 20 | +var loopExtensionStart = []byte("\x21\xff\x0bNETSCAPE2.0") |
| 21 | + |
| 22 | +// writeForACut writes a file a damage will cut after its first m.cutAt bytes, |
| 23 | +// with the padding split into a comment in front of the first frame and one in |
| 24 | +// front of the trailer, so that the cut keeps the share of the frames it keeps |
| 25 | +// of the whole file (format.Request.CutAt). |
| 26 | +// |
| 27 | +// With all of the padding behind the frames, as an uncut file has it, a large |
| 28 | +// file cut at any share kept the whole picture and lost only padding. GIF89a |
| 29 | +// lets a comment stand wherever a block can begin, the beginning of the data |
| 30 | +// stream included (section 24), but not in front of the colour table, which |
| 31 | +// belongs to the screen descriptor (appendix B). The comment in front goes after |
| 32 | +// the head the encoder writes - the descriptor, the colour table and, for a |
| 33 | +// picture that moves, the loop extension - where Pillow, FFmpeg, ImageMagick, |
| 34 | +// Go, the Windows Imaging Component, GDI+, exiftool, Tk and Chrome return the |
| 35 | +// same frames, the same loop count and the same pixels as for the file uncut |
| 36 | +// (docs/TRUNCATE-2026-11-11.md section 9.3). |
| 37 | +// |
| 38 | +// The frames stream out as the encoder writes them, so the cut stops the |
| 39 | +// encoder where it falls. The comment in front is sized from their length, so a |
| 40 | +// plan that did not encode the picture encodes it here first, which is what |
| 41 | +// every GIF below the ladder ceiling pays at planning today. |
| 42 | +func writeForACut(ctx context.Context, w io.Writer, m memo, whole int64) error { |
| 43 | + if !m.bodyKnown { |
| 44 | + body, err := encodedBodySize(m) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + m.body, m.bodyKnown = body, true |
| 49 | + } |
| 50 | + if err := settlePadding(&m, whole, m.body+trailerSize); err != nil { |
| 51 | + return core.Defect(fmt.Errorf("gif: %w - ladderCeiling is wrong", err)) |
| 52 | + } |
| 53 | + head := headBytes(m) |
| 54 | + pad := whole - m.body - trailerSize |
| 55 | + front := frontPadding(pad, m.body-head, head, m.cutAt, whole) |
| 56 | + markAroundThePicture(w, m.body, head, front, pad-front) |
| 57 | + |
| 58 | + // Both comments draw from the file's seed. Nobody reads padding, and the |
| 59 | + // comment behind the frames lies past any cut. |
| 60 | + split := &format.AfterTheHead{W: w, Head: head, Insert: func(w io.Writer, got []byte) error { |
| 61 | + if err := checkHead(got, m); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + return writePadding(ctx, w, m.seed, front) |
| 65 | + }} |
| 66 | + holder := &format.TailHolder{W: split, Keep: trailerSize} |
| 67 | + if err := encode(holder, m); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + if holder.Written != m.body { |
| 71 | + return core.Defect(fmt.Errorf("gif: the picture encoded to %d B where planning said %d B", holder.Written, m.body)) |
| 72 | + } |
| 73 | + if holder.Tail[0] != 0x3B { |
| 74 | + return core.Defect(fmt.Errorf("gif: the encoded stream does not end with the trailer")) |
| 75 | + } |
| 76 | + if err := writePadding(ctx, w, m.seed, pad-front); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + _, err := w.Write(holder.Tail) |
| 80 | + return err |
| 81 | +} |
| 82 | + |
| 83 | +// headBytes is how long the head the encoder writes in front of the first |
| 84 | +// frame is: the signature and screen descriptor, the colour table, and the loop |
| 85 | +// extension when the picture moves. The map counts it as structure - the colour |
| 86 | +// table is a table the frames are read through, and the frames are what a |
| 87 | +// reader hands back (docs/TRUNCATE-2026-11-11.md section 9.4). |
| 88 | +func headBytes(m memo) int64 { |
| 89 | + n := int64(screenHeader + 3*paletteSize(m.width, m.height)) |
| 90 | + if m.frames > 1 { |
| 91 | + n += loopExtensionSize |
| 92 | + } |
| 93 | + return n |
| 94 | +} |
| 95 | + |
| 96 | +// checkHead is whether the head that went out before the comment is the head |
| 97 | +// the encoder wrote: the screen descriptor, the colour table its packed field |
| 98 | +// announces, of the size planning counted, and for a picture that moves the |
| 99 | +// loop extension right after it - nothing more and nothing less. A head of |
| 100 | +// another length puts the comment inside the colour table or a frame. |
| 101 | +func checkHead(got []byte, m memo) error { |
| 102 | + if len(got) < screenHeader { |
| 103 | + return core.Defect(fmt.Errorf("gif: %d B went out before the comment, less than the screen descriptor", len(got))) |
| 104 | + } |
| 105 | + table, packed := 0, got[10] |
| 106 | + if packed&0x80 != 0 { |
| 107 | + table = 3 << (packed&0x07 + 1) |
| 108 | + } |
| 109 | + if want := 3 * paletteSize(m.width, m.height); table != want { |
| 110 | + return core.Defect(fmt.Errorf("gif: the encoder's colour table is %d B where planning counted %d B", table, want)) |
| 111 | + } |
| 112 | + head := screenHeader + table |
| 113 | + if m.frames > 1 { |
| 114 | + head += loopExtensionSize |
| 115 | + } |
| 116 | + if len(got) != head { |
| 117 | + return core.Defect(fmt.Errorf("gif: %d B went out before the comment where the encoder's head is %d B", len(got), head)) |
| 118 | + } |
| 119 | + if m.frames > 1 { |
| 120 | + loop := got[screenHeader+table:] |
| 121 | + if !bytes.HasPrefix(loop, loopExtensionStart) || loop[len(loop)-1] != 0 { |
| 122 | + return core.Defect(fmt.Errorf("gif: the encoder did not write the loop extension right after the colour table")) |
| 123 | + } |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// frontPadding is how much of the padding, as a comment, goes in front of the |
| 129 | +// first frame, so that a cut after the first cutAt bytes of a whole file keeps |
| 130 | +// frames*cutAt/whole bytes of the frames. |
| 131 | +// |
| 132 | +// A comment is nothing, three bytes or at least five, so both sides are |
| 133 | +// rounded to that. Where they cannot land exactly, the front grows - at most |
| 134 | +// four bytes - which moves the frames later and cuts more of them, the "at |
| 135 | +// least" missing promises. The one way it can keep more is a padding too short |
| 136 | +// for the frames the cut should take: there all of it goes in front, and the cut |
| 137 | +// keeps at most one byte more than the share (docs/TRUNCATE-2026-11-11.md |
| 138 | +// section 9.5). The manifest's cut reports what was kept either way. |
| 139 | +func frontPadding(pad, frames, head, cutAt, whole int64) int64 { |
| 140 | + if pad == 0 || cutAt <= 0 || cutAt >= whole { |
| 141 | + return 0 |
| 142 | + } |
| 143 | + front := format.PaddingInFront(head, frames, cutAt, whole) |
| 144 | + if front <= 0 { |
| 145 | + return 0 |
| 146 | + } |
| 147 | + for ; front < pad; front++ { |
| 148 | + if isComment(front) && isComment(pad-front) { |
| 149 | + return front |
| 150 | + } |
| 151 | + } |
| 152 | + return pad |
| 153 | +} |
| 154 | + |
| 155 | +// isComment is whether n bytes can be spent as one comment extension, or as |
| 156 | +// none at all. |
| 157 | +func isComment(n int64) bool { |
| 158 | + return n == 0 || n == emptyComment || n >= smallestCarryingComment |
| 159 | +} |
| 160 | + |
| 161 | +// writePadding writes n bytes of padding as one comment, or nothing for n = 0. |
| 162 | +func writePadding(ctx context.Context, w io.Writer, seed uint64, n int64) error { |
| 163 | + if n == 0 { |
| 164 | + return nil |
| 165 | + } |
| 166 | + blocks, payload := commentShape(n) |
| 167 | + return writeComment(ctx, w, seed, blocks, payload) |
| 168 | +} |
| 169 | + |
| 170 | +// markAroundThePicture marks the map of a file whose padding is split around |
| 171 | +// its frames: the head, the comment in front, the frames, the comment behind |
| 172 | +// and the trailer. A side with no padding has no comment and no region. |
| 173 | +func markAroundThePicture(w io.Writer, body, head, front, back int64) { |
| 174 | + format.Mark(w, format.RoleStructure, 0, head) |
| 175 | + at := head |
| 176 | + if front > 0 { |
| 177 | + format.Mark(w, format.RolePadding, at, front) |
| 178 | + at += front |
| 179 | + } |
| 180 | + format.Mark(w, format.RoleContent, at, body-head) |
| 181 | + at += body - head |
| 182 | + if back > 0 { |
| 183 | + format.Mark(w, format.RolePadding, at, back) |
| 184 | + at += back |
| 185 | + } |
| 186 | + format.Mark(w, format.RoleEnd, at, trailerSize) |
| 187 | +} |
0 commit comments