This file is indexed.

/usr/share/gocode/src/github.com/pointlander/compress/fractal/fractal.go is in golang-github-pointlander-compress-dev 1.0.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fractal

import (
	"bytes"
	"github.com/nfnt/resize"
	"image"
	"image/color"
	"io"
	"math"
)

const (
	GAMMA = 0.75
	DECODE_ITERATIONS = 16
)

type imagePanel struct {
	x, y, mean int
	pixels     []uint16
}

type image8 struct {
	xPanels, yPanels, panelSize int
	bounds                      image.Rectangle
	pixels                      []uint8
	panels                      []imagePanel
}

func splitImage(input image.Image) (r, g, b image.Image) {
	width, height := input.Bounds().Max.X, input.Bounds().Max.Y
	size := width * height
	rpixels, gpixels, bpixels :=
		make([]uint8, size),
		make([]uint8, size),
		make([]uint8, size)
	for y := 0; y < height; y++ {
		offset := y * width
		for x := 0; x < width; x++ {
			r, g, b, _ := input.At(x, y).RGBA()
			i := x + offset
			rpixels[i], gpixels[i], bpixels[i] =
				uint8(r >> 8),
				uint8(g >> 8),
				uint8(b >> 8)
		}
	}
	r = &image8{
		bounds: image.Rectangle{
			Max: image.Point{
				X: width,
				Y: height}},
		pixels: rpixels}
	g = &image8{
		bounds: image.Rectangle{
			Max: image.Point{
				X: width,
				Y: height}},
		pixels: gpixels}
	b = &image8{
		bounds: image.Rectangle{
			Max: image.Point{
				X: width,
				Y: height}},
		pixels: bpixels}
	return
}

func newImage(input image.Image, scale, panelSize int, gamma float64) *image8 {
	width, height := input.Bounds().Max.X, input.Bounds().Max.Y

	if scale > 1 {
		width, height = width/scale, height/scale
		input = resize.Resize(uint(width), uint(height), input, resize.NearestNeighbor)
	}

	for (width%panelSize) != 0 || (width%2) != 0 {
		width--
	}
	for (height%panelSize) != 0 || (height%2) != 0 {
		height--
	}

	pixels := make([]uint8, width*height)
	for y := 0; y < height; y++ {
		offset := y * width
		for x := 0; x < width; x++ {
			c, _, _, _ := input.At(x, y).RGBA()
			pixels[offset+x] = uint8(uint32(round(float64(c)*gamma/256.0)))
		}
	}

	xPanels, yPanels := width/panelSize, height/panelSize
	panels, size := make([]imagePanel, xPanels*yPanels), panelSize*panelSize
	for i := range panels {
		panels[i].pixels = make([]uint16, size)
	}

	paneled := &image8{
		xPanels:   xPanels,
		yPanels:   yPanels,
		panelSize: panelSize,
		bounds: image.Rectangle{
			Max: image.Point{
				X: width,
				Y: height}},
		pixels: pixels,
		panels: panels}
	paneled.updatePanels()
	return paneled
}

func (i *image8) updatePanels() {
	width, height := i.Bounds().Max.X, i.Bounds().Max.Y
	pixels, panels, panelSize := i.pixels, i.panels, i.panelSize
	p, size := 0, panelSize*panelSize

	for y := 0; y < height; y += panelSize {
		for x := 0; x < width; x += panelSize {
			pix, q, sum := panels[p].pixels, 0, 0
			for j := 0; j < panelSize; j++ {
				for i := 0; i < panelSize; i++ {
					pix[q] = uint16(pixels[(y+j)*width+x+i])
					sum, q = sum+int(pix[q]), q+1
				}
			}

			panels[p] = imagePanel{
				x:      x,
				y:      y,
				mean:   sum / size,
				pixels: pix}
			p++
		}
	}
}

func (i *image8) ColorModel() color.Model {
	return color.GrayModel
}

func (i *image8) Bounds() image.Rectangle {
	return i.bounds
}

func (i *image8) At(x, y int) color.Color {
	return color.Gray{Y: i.pixels[y*i.bounds.Max.X+x]}
}

func (i *image8) Set(x, y int, c color.Color) {
	gray, _, _, _ := c.RGBA()
	i.pixels[y*i.bounds.Max.X+x] = uint8(gray)
}

func FractalCoder(in image.Image, panelSize int, out io.Writer) {
	destination := newImage(in, 1, panelSize, 1)
	reference := newImage(in, 2, panelSize, GAMMA)
	maps := newPixelMap(panelSize)

	buffer, count := &bytes.Buffer{}, 0
	write16 := func(s uint16) {
		b := [...]byte{
			byte(s >> 8),
			byte(s & 0xFF)}
		buffer.Write(b[:])
	}

	for _, dPanel := range destination.panels {
		var best imagePanel
		bestError, bestForm, bestBeta := uint64(math.MaxUint64), 0, 0
		for _, rPanel := range reference.panels {
			beta := dPanel.mean - rPanel.mean
		search:
			for f, pmap := range maps {
				error := uint64(0)
				for i, j := range pmap {
					delta := int(dPanel.pixels[i]) -
						int(rPanel.pixels[j]) -
						beta
					error += uint64(delta * delta)
					if error >= bestError {
						continue search
					}
				}
				if error < bestError {
					best = rPanel
					bestBeta = beta
					bestForm = f
					bestError = error
				}
			}
			if bestError == 0 {
				break
			}
		}
		write16(uint16(bestForm))
		write16(uint16(best.x))
		write16(uint16(best.y))
		write16(uint16(bestBeta))
		count++
	}

	write32 := func(i uint32) {
		b := [...]byte{
			byte(i >> 24),
			byte((i >> 16) & 0xFF),
			byte((i >> 8) & 0xFF),
			byte(i & 0xFF)}
		out.Write(b[:])
	}
	write32(uint32(destination.xPanels))
	write32(uint32(destination.yPanels))
	write32(uint32(destination.panelSize))
	write32(uint32(count))
	out.Write(buffer.Bytes())
}

func FractalDecoder(in io.Reader, _panelSize int) image.Image {
	read32 := func() uint32 {
		var p [4]byte
		in.Read(p[:])
		return (uint32(p[0]) << 24) |
			(uint32(p[1]) << 16) |
			(uint32(p[2]) << 8) |
			uint32(p[3])
	}
	xPanels := read32()
	yPanels := read32()
	panelSize := read32()
	count := read32()

	read16 := func() uint16 {
		var p [2]byte
		in.Read(p[:])
		return (uint16(p[0]) << 8) |
			uint16(p[1])
	}
	codes := make([]struct{ form, x, y, beta uint16 }, count)
	for i := range codes {
		codes[i].form = read16()
		codes[i].x = read16()
		codes[i].y = read16()
		codes[i].beta = read16()
	}

	width, height := xPanels*uint32(_panelSize), yPanels*uint32(_panelSize)
	pixels := make([]uint8, width*height)
	for y := uint32(0); y < height; y++ {
		offset := y * width
		for x := uint32(0); x < width; x++ {
			pixels[offset+x] = uint8(0x80)
		}
	}

	panels, size := make([]imagePanel, xPanels*yPanels), _panelSize*_panelSize
	for i := range panels {
		panels[i].pixels = make([]uint16, size)
	}

	destination := &image8{
		xPanels:   int(xPanels),
		yPanels:   int(yPanels),
		panelSize: _panelSize,
		bounds: image.Rectangle{
			Max: image.Point{
				X: int(width),
				Y: int(height)}},
		pixels: pixels,
		panels: panels}
	destination.updatePanels()

	newReference := func() *image8 {
		width, height := destination.Bounds().Max.X, destination.Bounds().Max.Y
		width, height = width/2, height/2
		reference := resize.Resize(uint(width), uint(height), destination, resize.NearestNeighbor)

		pixels := make([]uint8, width*height)
		for y := 0; y < height; y++ {
			offset := y * width
			for x := 0; x < width; x++ {
				r, _, _, _ := reference.At(x, y).RGBA()
				pixels[offset+x] = uint8(uint32(round(float64(r)*GAMMA/256)))
			}
		}

		xPanels, yPanels := width/_panelSize, height/_panelSize
		panels, size := make([]imagePanel, xPanels*yPanels), _panelSize*_panelSize
		for i := range panels {
			panels[i].pixels = make([]uint16, size)
		}

		paneled := &image8{
			xPanels:   xPanels,
			yPanels:   yPanels,
			panelSize: _panelSize,
			bounds: image.Rectangle{
				Max: image.Point{
					X: width,
					Y: height}},
			pixels: pixels,
			panels: panels}
		paneled.updatePanels()
		return paneled
	}

	maps := newPixelMap(_panelSize)
	for i := 0; i < DECODE_ITERATIONS; i++ {
		reference := newReference()

		for j, d := range panels {
			code := codes[j]
			//x, y := int(uint64(_panelSize)*uint64(code.x)/(2*uint64(panelSize))),
			//	int(uint64(_panelSize)*uint64(code.y)/(2*uint64(panelSize)))
			x, y := int(uint32(code.x)/panelSize),
				int(uint32(code.y)/panelSize)
			if x >= reference.xPanels {
				x = reference.xPanels - 1
			}
			if y >= reference.yPanels {
				y = reference.yPanels - 1
			}
			r := reference.panels[x+y*reference.xPanels]
			pmap, f := maps[code.form], 0

			for y := 0; y < _panelSize; y++ {
				for x := 0; x < _panelSize; x++ {
					z, e := int(r.pixels[pmap[f]])+int(int16(code.beta)),
						d.x+x+int(width)*(d.y+y)
					if z < 0 {
						pixels[e] = uint8(0)
					} else if z > 255 {
						pixels[e] = uint8(0xFF)
					} else {
						pixels[e] = uint8(z)
					}
					f++
				}
			}
		}
	}

	return destination
}