Debian Codegraph @

box.go

raw symbols SCIP
1 // Package png2svg can convert images from PNG to TinySVG by drawing rectangles
2 package png2svg
3
4 import (
5 "fmt"
6 "math/rand"
7 "strconv"
8 )
9
10 // Box represents a box with the following properties:
11 // * position (x, y)
12 // * size (w, h)
13 // * color (r, g, b, a)
14 type Box struct {
15 x, y int
16 w, h int
17 r, g, b, a int
18 }
19
20 // CreateRandomBox randomly searches for a place for a 1x1 size box.
21 // Note: If checkIfPossible is true, the function continue running until
22 // it either finds a free spot or no spots are available.
23 func (pi *PixelImage) CreateRandomBox(checkIfPossible bool) *Box {
24 w := 1
25 h := 1
26 var x, y, r, g, b, a int
27 for !checkIfPossible || !pi.Done(0, 0) {
28 // Find a random placement for (x,y), for a box of size (1,1)
29 x = rand.Intn(pi.w)
30 y = rand.Intn(pi.h)
31 if pi.verbose {
32 fmt.Printf("Random box at (%d, %d)\n", x, y)
33 }
34 if pi.Covered(x, y) {
35 continue
36 }
37 r, g, b, a = pi.At2(x, y)
38 break
39 }
40 // Create a box at that placement, with width 1 and height 1
41 // Return the box
42 return &Box{x, y, w, h, r, g, b, a}
43 }
44
45 // CreateBox creates a 1x1 box at the given location, if it's not already covered
46 func (pi *PixelImage) CreateBox(x, y int) *Box {
47 if pi.Covered(x, y) {
48 panic("CreateBox at location that was already covered")
49 }
50 w, h := 1, 1
51 r, g, b, a := pi.At2(x, y)
52 // Create a box at that placement, with width 1 and height 1
53 // Return the box
54 return &Box{x, y, w, h, r, g, b, a}
55 }
56
57 // ExpandLeft will expand a box 1 pixel to the left,
58 // if all new pixels have the same color
59 func (pi *PixelImage) ExpandLeft(bo *Box) bool {
60 // Loop from box top left (-1,0) to box bot left (-1,0)
61 x := bo.x - 1
62 if x <= 0 {
63 return false
64 }
65 for y := bo.y; y < (bo.y + bo.h); y++ {
66 r, g, b, a := pi.At2(x, y)
67 if (r != bo.r) || (g != bo.g) || (b != bo.b) || (a != bo.a) {
68 return false
69 }
70 }
71 // Expand the box 1 pixel to the left
72 bo.w++
73 bo.x--
74 return true
75 }
76
77 // ExpandUp will expand a box 1 pixel upwards,
78 // if all new pixels have the same color
79 func (pi *PixelImage) ExpandUp(bo *Box) bool {
80 // Loop from box top left to box top right
81 y := bo.y - 1
82 if y <= 0 {
83 return false
84 }
85 for x := bo.x; x < (bo.x + bo.w); x++ {
86 r, g, b, a := pi.At2(x, y)
87 if (r != bo.r) || (g != bo.g) || (b != bo.b) || (a != bo.a) {
88 return false
89 }
90 }
91 // Expand the box 1 pixel up
92 bo.h++
93 bo.y--
94 return true
95 }
96
97 // ExpandRight will expand a box 1 pixel to the right,
98 // if all new pixels have the same color
99 func (pi *PixelImage) ExpandRight(bo *Box) bool {
100 // Loop from box top right (+1,0) to box bot right (+1,0)
101 x := bo.x + bo.w //+ 1
102 if x >= pi.w {
103 return false
104 }
105 for y := bo.y; y < (bo.y + bo.h); y++ {
106 r, g, b, a := pi.At2(x, y)
107 if (r != bo.r) || (g != bo.g) || (b != bo.b) || (a != bo.a) {
108 return false
109 }
110 }
111 // Expand the box 1 pixel to the right
112 bo.w++
113 return true
114 }
115
116 // ExpandDown will expand a box 1 pixel downwards,
117 // if all new pixels have the same color
118 func (pi *PixelImage) ExpandDown(bo *Box) bool {
119 // Loop from box bot left to box bot right
120 y := bo.y + bo.h //+ 1
121 if y >= pi.h {
122 return false
123 }
124 for x := bo.x; x < (bo.x + bo.w); x++ {
125 r, g, b, a := pi.At2(x, y)
126 if (r != bo.r) || (g != bo.g) || (b != bo.b) || (a != bo.a) {
127 return false
128 }
129 }
130 // Expand the box 1 pixel down
131 bo.h++
132 return true
133 }
134
135 // ExpandOnce tries to expand the box to the right and downwards, once
136 func (pi *PixelImage) ExpandOnce(bo *Box) bool {
137 if pi.ExpandRight(bo) {
138 return true
139 }
140 return pi.ExpandDown(bo)
141 }
142
143 // Expand tries to expand the box to the right and downwards, until it can't expand any more.
144 // Returns true if the box was expanded at least once.
145 func (pi *PixelImage) Expand(bo *Box) (expanded bool) {
146 for {
147 if !pi.ExpandOnce(bo) {
148 break
149 }
150 expanded = true
151 }
152 return
153 }
154
155 // singleHex returns a single digit hex number, as a string
156 // the numbers are not rounded, just floored
157 func singleHex(x byte) string {
158 hex := strconv.FormatInt(int64(x), 16)
159 if len(hex) < 2 {
160 return "0" + hex // Prepend "0" if the hex representation is a single digit
161 }
162 return hex
163 }
164
165 // shortColorString returns a string representing a color on the short form "#000"
166 func shortColorString(r, g, b byte) string {
167 return "#" + singleHex(r) + singleHex(g) + singleHex(b)
168 }
169
170 // CoverBox creates rectangles in the SVG image, and also marks the pixels as covered
171 // if pink is true, the rectangles will be pink
172 // if optimizeColors is true, the color strings will be shortened (and quantized)
173 func (pi *PixelImage) CoverBox(bo *Box, pink bool, optimizeColors bool) {
174 // Draw the rectangle
175 rect := pi.svgTag.AddRect(bo.x, bo.y, bo.w, bo.h)
176
177 // Generate a fill color string
178 var colorString string
179 if pink {
180 if optimizeColors {
181 colorString = "#b38"
182 } else {
183 colorString = "#bb3388"
184 }
185 } else if optimizeColors {
186 colorString = shortColorString(byte(bo.r), byte(bo.g), byte(bo.b))
187 } else {
188 colorString = fmt.Sprintf("#%02x%02x%02x", bo.r, bo.g, bo.b)
189 }
190
191 // Set the fill color
192 rect.Fill(colorString)
193
194 // Mark all covered pixels in the PixelImage
195 for y := bo.y; y < (bo.y + bo.h); y++ {
196 for x := bo.x; x < (bo.x + bo.w); x++ {
197 pi.pixels[y*pi.w+x].covered = true
198 }
199 }
200 }