Debian Codegraph @

qr/resize.go

raw symbols SCIP
1 package qr
2
3 import (
4 "image"
5 "image/color"
6 )
7
8 // average convert the sums to averages and returns the result.
9 func average(sum []uint64, w, h int, n uint64) *image.RGBA {
10 ret := image.NewRGBA(image.Rect(0, 0, w, h))
11 for y := 0; y < h; y++ {
12 for x := 0; x < w; x++ {
13 index := 4 * (y*w + x)
14 pix := ret.Pix[y*ret.Stride+x*4:]
15 pix[0] = uint8(sum[index+0] / n)
16 pix[1] = uint8(sum[index+1] / n)
17 pix[2] = uint8(sum[index+2] / n)
18 pix[3] = uint8(sum[index+3] / n)
19 }
20 }
21 return ret
22 }
23
24
25 // ResizeRGBA returns a scaled copy of the RGBA image slice r of m.
26 // The returned image has width w and height h.
27 func ResizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) *image.RGBA {
28 ww, hh := uint64(w), uint64(h)
29 dx, dy := uint64(r.Dx()), uint64(r.Dy())
30 // See comment in Resize.
31 n, sum := dx*dy, make([]uint64, 4*w*h)
32 for y := r.Min.Y; y < r.Max.Y; y++ {
33 pix := m.Pix[(y-r.Min.Y)*m.Stride:]
34 for x := r.Min.X; x < r.Max.X; x++ {
35 // Get the source pixel.
36 p := pix[(x-r.Min.X)*4:]
37 r64 := uint64(p[0])
38 g64 := uint64(p[1])
39 b64 := uint64(p[2])
40 a64 := uint64(p[3])
41 // Spread the source pixel over 1 or more destination rows.
42 py := uint64(y) * hh
43 for remy := hh; remy > 0; {
44 qy := dy - (py % dy)
45 if qy > remy {
46 qy = remy
47 }
48 // Spread the source pixel over 1 or more destination columns.
49 px := uint64(x) * ww
50 index := 4 * ((py/dy)*ww + (px / dx))
51 for remx := ww; remx > 0; {
52 qx := dx - (px % dx)
53 if qx > remx {
54 qx = remx
55 }
56 qxy := qx * qy
57 sum[index+0] += r64 * qxy
58 sum[index+1] += g64 * qxy
59 sum[index+2] += b64 * qxy
60 sum[index+3] += a64 * qxy
61 index += 4
62 px += qx
63 remx -= qx
64 }
65 py += qy
66 remy -= qy
67 }
68 }
69 }
70 return average(sum, w, h, (uint64)(n))
71 }
72
73
74 // ResizeNRGBA returns a scaled copy of the RGBA image slice r of m.
75 // The returned image has width w and height h.
76 func ResizeNRGBA(m *image.NRGBA, r image.Rectangle, w, h int) *image.RGBA {
77 ww, hh := uint64(w), uint64(h)
78 dx, dy := uint64(r.Dx()), uint64(r.Dy())
79 // See comment in Resize.
80 n, sum := dx*dy, make([]uint64, 4*w*h)
81 for y := r.Min.Y; y < r.Max.Y; y++ {
82 pix := m.Pix[(y-r.Min.Y)*m.Stride:]
83 for x := r.Min.X; x < r.Max.X; x++ {
84 // Get the source pixel.
85 p := pix[(x-r.Min.X)*4:]
86 r64 := uint64(p[0])
87 g64 := uint64(p[1])
88 b64 := uint64(p[2])
89 a64 := uint64(p[3])
90 r64 = (r64 * a64) / 255
91 g64 = (g64 * a64) / 255
92 b64 = (b64 * a64) / 255
93 // Spread the source pixel over 1 or more destination rows.
94 py := uint64(y) * hh
95 for remy := hh; remy > 0; {
96 qy := dy - (py % dy)
97 if qy > remy {
98 qy = remy
99 }
100 // Spread the source pixel over 1 or more destination columns.
101 px := uint64(x) * ww
102 index := 4 * ((py/dy)*ww + (px / dx))
103 for remx := ww; remx > 0; {
104 qx := dx - (px % dx)
105 if qx > remx {
106 qx = remx
107 }
108 qxy := qx * qy
109 sum[index+0] += r64 * qxy
110 sum[index+1] += g64 * qxy
111 sum[index+2] += b64 * qxy
112 sum[index+3] += a64 * qxy
113 index += 4
114 px += qx
115 remx -= qx
116 }
117 py += qy
118 remy -= qy
119 }
120 }
121 }
122 return average(sum, w, h, (uint64)(n))
123 }
124
125 // Resample returns a resampled copy of the image slice r of m.
126 // The returned image has width w and height h.
127 func Resample(m image.Image, r image.Rectangle, w, h int) *image.RGBA {
128 if w < 0 || h < 0 {
129 return nil
130 }
131 if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
132 return image.NewRGBA(image.Rect(0, 0, w, h))
133 }
134 curw, curh := r.Dx(), r.Dy()
135 img := image.NewRGBA(image.Rect(0, 0, w, h))
136 for y := 0; y < h; y++ {
137 for x := 0; x < w; x++ {
138 // Get a source pixel.
139 subx := x * curw / w
140 suby := y * curh / h
141 r32, g32, b32, a32 := m.At(subx, suby).RGBA()
142 r := uint8(r32 >> 8)
143 g := uint8(g32 >> 8)
144 b := uint8(b32 >> 8)
145 a := uint8(a32 >> 8)
146 img.SetRGBA(x, y, color.RGBA{r, g, b, a})
147 }
148 }
149 return img
150 }
151