-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.go
More file actions
140 lines (118 loc) · 3.54 KB
/
16.go
File metadata and controls
140 lines (118 loc) · 3.54 KB
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
package main
import (
"fmt"
"net/http"
"io"
"image"
"image/gif"
"image/color"
"os"
"reflect"
"strconv"
)
func main(){
gf, _ := os.Open("mozart.gif")
defer gf.Close()
mozart, _ := gif.Decode(gf)
bounds := mozart.Bounds()
var R, C, r, c int
R, C = bounds.Dy(), bounds.Dx()
fmt.Println("size/", bounds, reflect.TypeOf(bounds))
fmt.Println("rows/", R, "cols/", C)
// try something like Paletted
plt, _ := mozart.(*image.Paletted)
//res := image.NewPaletted( bounds, nil) // BUG
res := image.NewPaletted( bounds, plt.Palette) // bugdix
fmt.Println("init/type", reflect.TypeOf(res))
r = 0
for r < R {
// row reprented as a rgb(a) slice
row := make([]color.Color, C)
c = 0
for c < C {
cl := mozart.At(c, r)
//fmt.Println("color/", cl, reflect.TypeOf(cl))
row[c] = cl
c++
}
// 1 - finding out about that strip|segment
// observation/
// {255 0 255 255} --> pink-ish, len-5 segment found
// findingLongestSegment(row, C)
// 2 - find index-pairs of all pink segments
// assert/
// there is only one such pair; a pinkdot's L-index is sufficient
s := findingPinkSegment(row, C)
// 3 - move pink segments at the end of row (ie. res[r])
row = append(row[s:], row[:s]...) // bugfix
// append(append(row[s:e], row[:s]...), row[e:]...) // BUG
c = 0
for c < C {
res.Set(c, r, row[c])
c++
}
r++
}
outfile, _ := os.Create("res.gif")
defer outfile.Close()
_ = gif.Encode(outfile, res, nil)
}
func findingPinkSegment(row[]color.Color, C int) int {
s, e := 0, 0
c := 1
for c < C {
cl := row[c]
rr, gg, bb, aa := cl.RGBA()
r8, g8, b8, a8 := uint8(rr>>8), uint8(gg>>8), uint8(bb>>8), uint8(aa>>8)
// observation/ first pinkdot we meet suffices
if r8 == 255 && g8 == 0 && b8 == 255 && a8 == 255 /*&& row[c]==row[c+1] && row[c+1]==row[c+2]*/ {
s = c
e = c + 5
c += 5
continue
}
c++
}
if e < 42 { fmt.Println("row/", row[s], s, e, e - s) }
return s//, e
}
func findingLongestSegment(row[]color.Color, C int) (int, int) {
// here we do longest uni-char substring
var commoncolor color.Color = row[0] // most seen
var currentcolor color.Color = commoncolor // now-inspecting
scurr := 0 // curr s(tart)
s, e, maxlen := 0, 0, 0
c := 1
for c < C {
if currentcolor != row[c] {
dist := c - scurr
if maxlen < dist {
commoncolor = currentcolor // bugfix
// commoncolor = row[c] // BUG
s = scurr
e = c
maxlen = dist
}
currentcolor = row[c]
scurr = c
}
c++
}
SE := strconv.Itoa(s) + "-" + strconv.Itoa(e)
fmt.Println(commoncolor, SE, "len/", maxlen)
return s, e
}
func init(){
// URL := "http://www.pythonchallenge.com/pc/return/mozart.html"
URL := "http://www.pythonchallenge.com/pc/return/mozart.gif"
ups, mid := "hugefile", 4
conn := & http.Client{}
req, err := http.NewRequest("GET", URL, nil)
if err != nil {fmt.Println("err/", err)}
req.SetBasicAuth(ups[:mid], ups[mid:])
resp, _ := conn.Do(req)
defer resp.Body.Close()
f, err := os.Create( "mozart.gif" )
defer f.Close()
_, _ = io.Copy(f, resp.Body)
}