Skip to content

Commit

Permalink
Keep the exported types and functions as they were + minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed May 14, 2024
1 parent 5cd2757 commit 5a99e69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
10 changes: 5 additions & 5 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Box struct {
x, y int
w, h int
r, g, b, a byte
r, g, b, a int
}

// CreateRandomBox randomly searches for a place for a 1x1 size box.
Expand All @@ -24,8 +24,7 @@ type Box struct {
func (pi *PixelImage) CreateRandomBox(checkIfPossible bool) *Box {
w := 1
h := 1
var x, y int
var r, g, b, a byte
var x, y, r, g, b, a int
for !checkIfPossible || !pi.Done(0, 0) {
// Find a random placement for (x,y), for a box of size (1,1)
x = rand.Intn(pi.w)
Expand Down Expand Up @@ -185,9 +184,10 @@ func (pi *PixelImage) CoverBox(bo *Box, pink bool, optimizeColors bool) {
colorString = "#bb3388"
}
} else if optimizeColors {
colorString = shortColorString(bo.r, bo.g, bo.b)
colorString = shortColorString(byte(bo.r), byte(bo.g), byte(bo.b))
} else {
colorString = string(tinysvg.ColorBytes(int(bo.r), int(bo.g), int(bo.b)))
// THIS IS THE BUG
colorString = string(tinysvg.ColorBytes(bo.r, bo.g, bo.b))
}

// Set the fill color
Expand Down
34 changes: 20 additions & 14 deletions pixelimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
type Pixel struct {
x int
y int
r byte
g byte
b byte
a byte
r int
g int
b int
a int
covered bool
}

Expand Down Expand Up @@ -108,7 +108,7 @@ func NewPixelImage(img image.Image, verbose bool) *PixelImage {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
c = color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
// Mark transparent pixels as already being "covered" (alpha == 0)
pixels[i] = &Pixel{x, y, byte(c.R), byte(c.G), byte(c.B), byte(c.A), c.A == 0}
pixels[i] = &Pixel{x, y, int(c.R), int(c.G), int(c.B), int(c.A), c.A == 0}
i++
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (pi *PixelImage) Done(startx, starty int) bool {
}

// At returns the RGB color at the given coordinate
func (pi *PixelImage) At(x, y int) (r, g, b byte) {
func (pi *PixelImage) At(x, y int) (r, g, b int) {
i := y*pi.w + x
//if i >= len(pi.pixels) {
// panic("At out of bounds, too large coordinate")
Expand All @@ -151,7 +151,7 @@ func (pi *PixelImage) At(x, y int) (r, g, b byte) {
}

// At2 returns the RGBA color at the given coordinate
func (pi *PixelImage) At2(x, y int) (r, g, b, a byte) {
func (pi *PixelImage) At2(x, y int) (r, g, b, a int) {
i := y*pi.w + x
//if i >= len(pi.pixels) {
// panic("At out of bounds, too large coordinate")
Expand Down Expand Up @@ -277,13 +277,15 @@ func groupLinesByFillColor(lines [][]byte, colorOptimize bool) [][]byte {
}
// Erase this line. The grouped lines will be inserted at the first empty line.
lines[i] = make([]byte, 0)
// TODO: Use the byte string as the key instead of converting to a string
// // Convert from []byte to string because map keys can't be []byte
cs := string(shortenedFillColor)
if _, ok := groupedLines[cs]; !ok {
// Start an empty line
groupedLines[cs] = make([][]byte, 0)
}
line = bytes.Replace(line, fillColor, shortenedFillColor, 1)
if string(fillColor) != cs {
line = bytes.Replace(line, fillColor, shortenedFillColor, 1)
}
line = append(line, '>')
groupedLines[cs] = append(groupedLines[cs], line)
}
Expand All @@ -294,17 +296,20 @@ func groupLinesByFillColor(lines [][]byte, colorOptimize bool) [][]byte {

// Build a string of all lines with fillcolor, grouped by fillcolor, inside <g> tags
var (
buf bytes.Buffer
from []byte
buf bytes.Buffer
from []byte
gfill = []byte("<g fill=\"")
closing = []byte("\">")
spacefill = []byte(" fill=\"")
)
for key, lines := range groupedLines {
if len(lines) > 1 {
buf.Write([]byte("<g fill=\""))
buf.Write(gfill)
//fmt.Printf("WRITING KEY %s\n", key)
buf.WriteString(key)
buf.Write([]byte("\">"))
buf.Write(closing)
for _, line := range lines {
from = append([]byte(" fill=\""), key...)
from = append(spacefill, key...)
buf.Write(bytes.Replace(line, append(from, '"'), []byte{}, 1))
}
buf.Write([]byte("</g>"))
Expand Down Expand Up @@ -370,6 +375,7 @@ func (pi *PixelImage) Bytes() []byte {
// Remove empty width attributes
// Remove empty height attributes
// Remove single spaces between tags

svgDocument = bytes.Replace(svgDocument, []byte("\n"), []byte{}, -1)
svgDocument = bytes.Replace(svgDocument, []byte(" />"), []byte("/>"), -1)
svgDocument = bytes.Replace(svgDocument, []byte(" "), []byte(" "), -1)
Expand Down
2 changes: 1 addition & 1 deletion pixelimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRainforestPixelColorMatch(t *testing.T) {
pixel := pixelImage.pixels[targetY*pixelImage.w+targetX]

// Check if the color of the pixel matches the original image's pixel color
if pixel.r != byte(originalColor.R) || pixel.g != byte(originalColor.G) || pixel.b != byte(originalColor.B) {
if pixel.r != int(originalColor.R) || pixel.g != int(originalColor.G) || pixel.b != int(originalColor.B) {
t.Errorf("Pixel at (%d,%d) has incorrect color: got (R: %d, G: %d, B: %d), want (R: %d, G: %d, B: %d)",
targetX, targetY, pixel.r, pixel.g, pixel.b, originalColor.R, originalColor.G, originalColor.B)
}
Expand Down

0 comments on commit 5a99e69

Please sign in to comment.