| 1 | // Package png2svg can convert images from PNG to TinySVG by drawing rectangles |
| 2 | package png2svgscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/ |
| 3 | |
| 4 | import ( |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 6 | "math/randscip-go gomod github.com/golang/go/src go1.25.1 `math/rand`/" |
| 7 | "strconvscip-go gomod github.com/golang/go/src go1.25.1 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 Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box# struct { |
| 15 | xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x., yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y. int |
| 16 | wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w., hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h. int |
| 17 | rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r., gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g., bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b., ascip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) CreateRandomBoxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#CreateRandomBox().(checkIfPossible bool) *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box# { |
| 24 | w := 1 |
| 25 | h := 1 |
| 26 | var x, y, r, g, b, a int |
| 27 | for !checkIfPossible || !pi.Donescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#Done().(0, 0) { |
| 28 | // Find a random placement for (x,y), for a box of size (1,1) |
| 29 | x = randscip-go gomod github.com/golang/go/src go1.25.1 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.25.1 `math/rand`/Intn().(pi.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#w.) |
| 30 | y = randscip-go gomod github.com/golang/go/src go1.25.1 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.25.1 `math/rand`/Intn().(pi.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#h.) |
| 31 | if pi.verbosescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#verbose. { |
| 32 | fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Printfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Printf().("Random box at (%d, %d)\n", x, y) |
| 33 | } |
| 34 | if pi.Coveredscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#Covered().(x, y) { |
| 35 | continue |
| 36 | } |
| 37 | r, g, b, a = pi.At2scip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#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 &Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) CreateBoxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#CreateBox().(x, y int) *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box# { |
| 47 | if pi.Coveredscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#Covered().(x, y) { |
| 48 | panic("CreateBox at location that was already covered") |
| 49 | } |
| 50 | w, h := 1, 1 |
| 51 | r, g, b, a := pi.At2scip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#At2().(x, y) |
| 52 | // Create a box at that placement, with width 1 and height 1 |
| 53 | // Return the box |
| 54 | return &Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) ExpandLeftscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#ExpandLeft().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#) bool { |
| 60 | // Loop from box top left (-1,0) to box bot left (-1,0) |
| 61 | x := bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x. - 1 |
| 62 | if x <= 0 { |
| 63 | return false |
| 64 | } |
| 65 | for y := bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y.; y < (bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y. + bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h.); y++ { |
| 66 | r, g, b, a := pi.At2scip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#At2().(x, y) |
| 67 | if (r != bo.rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r.) || (g != bo.gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g.) || (b != bo.bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b.) || (a != bo.ascip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#a.) { |
| 68 | return false |
| 69 | } |
| 70 | } |
| 71 | // Expand the box 1 pixel to the left |
| 72 | bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w.++ |
| 73 | bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) ExpandUpscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#ExpandUp().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#) bool { |
| 80 | // Loop from box top left to box top right |
| 81 | y := bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y. - 1 |
| 82 | if y <= 0 { |
| 83 | return false |
| 84 | } |
| 85 | for x := bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x.; x < (bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x. + bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w.); x++ { |
| 86 | r, g, b, a := pi.At2scip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#At2().(x, y) |
| 87 | if (r != bo.rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r.) || (g != bo.gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g.) || (b != bo.bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b.) || (a != bo.ascip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#a.) { |
| 88 | return false |
| 89 | } |
| 90 | } |
| 91 | // Expand the box 1 pixel up |
| 92 | bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h.++ |
| 93 | bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) ExpandRightscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#ExpandRight().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#) bool { |
| 100 | // Loop from box top right (+1,0) to box bot right (+1,0) |
| 101 | x := bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x. + bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w. //+ 1 |
| 102 | if x >= pi.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#w. { |
| 103 | return false |
| 104 | } |
| 105 | for y := bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y.; y < (bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y. + bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h.); y++ { |
| 106 | r, g, b, a := pi.At2scip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#At2().(x, y) |
| 107 | if (r != bo.rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r.) || (g != bo.gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g.) || (b != bo.bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b.) || (a != bo.ascip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#a.) { |
| 108 | return false |
| 109 | } |
| 110 | } |
| 111 | // Expand the box 1 pixel to the right |
| 112 | bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) ExpandDownscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#ExpandDown().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#) bool { |
| 119 | // Loop from box bot left to box bot right |
| 120 | y := bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y. + bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h. //+ 1 |
| 121 | if y >= pi.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#h. { |
| 122 | return false |
| 123 | } |
| 124 | for x := bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x.; x < (bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x. + bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w.); x++ { |
| 125 | r, g, b, a := pi.At2scip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#At2().(x, y) |
| 126 | if (r != bo.rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r.) || (g != bo.gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g.) || (b != bo.bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b.) || (a != bo.ascip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#a.) { |
| 127 | return false |
| 128 | } |
| 129 | } |
| 130 | // Expand the box 1 pixel down |
| 131 | bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h.++ |
| 132 | return true |
| 133 | } |
| 134 | |
| 135 | // ExpandOnce tries to expand the box to the right and downwards, once |
| 136 | func (pi *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) ExpandOncescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#ExpandOnce().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#) bool { |
| 137 | if pi.ExpandRightscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#ExpandRight().(bo) { |
| 138 | return true |
| 139 | } |
| 140 | return pi.ExpandDownscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) Expandscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#Expand().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#) (expanded bool) { |
| 146 | for { |
| 147 | if !pi.ExpandOncescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#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 singleHexscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/singleHex().(x byte) string { |
| 158 | hex := strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.FormatIntscip-go gomod github.com/golang/go/src go1.25.1 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 shortColorStringscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/shortColorString().(r, g, b byte) string { |
| 167 | return "#" + singleHexscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/singleHex().(r) + singleHexscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/singleHex().(g) + singleHexscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/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 *PixelImagescip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#) CoverBoxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#CoverBox().(bo *Boxscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#, pink bool, optimizeColors bool) { |
| 174 | // Draw the rectangle |
| 175 | rect := pi.svgTagscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#svgTag..AddRectscip-go gomod github.com/xyproto/tinysvg v1.2.1 `github.com/xyproto/tinysvg`/Tag#AddRect().(bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x., bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y., bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w., bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#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 = shortColorStringscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/shortColorString().(byte(bo.rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r.), byte(bo.gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g.), byte(bo.bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b.)) |
| 187 | } else { |
| 188 | colorString = fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("#%02x%02x%02x", bo.rscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#r., bo.gscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#g., bo.bscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#b.) |
| 189 | } |
| 190 | |
| 191 | // Set the fill color |
| 192 | rect.Fillscip-go gomod github.com/xyproto/tinysvg v1.2.1 `github.com/xyproto/tinysvg`/Tag#Fill().(colorString) |
| 193 | |
| 194 | // Mark all covered pixels in the PixelImage |
| 195 | for y := bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y.; y < (bo.yscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#y. + bo.hscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#h.); y++ { |
| 196 | for x := bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x.; x < (bo.xscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#x. + bo.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Box#w.); x++ { |
| 197 | pi.pixelsscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#pixels.[y*pi.wscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/PixelImage#w.+x].coveredscip-go gomod github.com/xyproto/png2svg . `github.com/xyproto/png2svg`/Pixel#covered. = true |
| 198 | } |
| 199 | } |
| 200 | } |