Skip to content

Commit

Permalink
Find and fix edge case in rectangular image
Browse files Browse the repository at this point in the history
  • Loading branch information
sevagh committed Feb 3, 2019
1 parent 775d2dc commit 8d31a4f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
6 changes: 3 additions & 3 deletions compress.go
Expand Up @@ -25,11 +25,11 @@ func (q *quadTreeNode) prune(tolerance float64) {
// stack recursion - essentially a DFS of sorts
func (q *quadTreeNode) canPrune(parent *quadTreeNode, tolerance float64) bool {
if q.children[NE] == nil || q.children[NW] == nil || q.children[SE] == nil || q.children[SW] == nil { // leaf node
colorDist := CIE76(parent.color, q.color)
//colorDist := EuclidianDistance(parent.color, q.color)
//colorDist := CIE76(parent.color, q.color)
colorDist := EuclidianDistance(parent.color, q.color)

// can prune if the LAB CIE76 is under the Just Noticeable Difference threshold
return colorDist <= 2.3*10
return colorDist <= tolerance
}

return q.children[NE].canPrune(parent, tolerance) && q.children[NW].canPrune(parent, tolerance) && q.children[SE].canPrune(parent, tolerance) && q.children[SW].canPrune(parent, tolerance)
Expand Down
4 changes: 2 additions & 2 deletions compress_test.go
Expand Up @@ -62,8 +62,8 @@ func TestCompressModifiesQuadTreeObject(t *testing.T) {
}
}

func TestCompressQuadTree(t *testing.T) {
path := "./samples/jungle.jpg"
func TestCompressImageQuadTree(t *testing.T) {
path := "./samples/batman.png"
regularOut := "./normal_out.png"
compressedPath := "./compressed_out.png"

Expand Down
36 changes: 36 additions & 0 deletions convert_test.go
@@ -1,6 +1,7 @@
package main

import (
"image/color"
"testing"
)

Expand All @@ -18,3 +19,38 @@ func TestCreateImageRoundTripThroughQuadTree(t *testing.T) {
t.Fatalf("Error when converting quadtree to image: %+v", err)
}
}

func TestCreateFakeImage(t *testing.T) {
outPath := "./fake_out.png"

qt := QuadTree{}

qnNE := quadTreeNode{}
qnNE.color = color.RGBA{R: 0, G: 0, B: 0, A: 255}

qnNW := quadTreeNode{}
qnNW.color = color.RGBA{R: 255, G: 0, B: 0, A: 255}

qnSE := quadTreeNode{}
qnSE.color = color.RGBA{R: 0, G: 255, B: 0, A: 255}

qnSW := quadTreeNode{}
qnSW.color = color.RGBA{R: 0, G: 0, B: 255, A: 255}

qn := quadTreeNode{}
qn.children[NE] = &qnNE
qn.children[NW] = &qnNW
qn.children[SE] = &qnSE
qn.children[SW] = &qnSW

qn.color = color.RGBA{R: 63, G: 63, B: 63, A: 255}

qt.root = &qn
qt.height = 10
qt.width = 10

err := qt.ConvertToImage(outPath)
if err != nil {
t.Fatalf("Error creating fake image '%s': %+v", outPath, err)
}
}
12 changes: 12 additions & 0 deletions quadtree.go
@@ -1,6 +1,7 @@
package main

import (
//"fmt"
"image"
colorlib "image/color"
)
Expand Down Expand Up @@ -44,6 +45,17 @@ func BuildQuadTree(imageSource string) (*QuadTree, error) {
}

func buildQuadTree(img *image.Image, x, y, w, h int) *quadTreeNode {
//if w == 0 && h == 0 {
// fmt.Printf("YOU THE MOTHERFUCKER\n")
// return nil
//}
if w == 0 {
w = 1
}
if h == 0 {
h = 1
}

if w == 1 && h == 1 {
qn := newQuadTreeNode((*img).At(x, y))
return &qn
Expand Down
3 changes: 3 additions & 0 deletions samples/batman.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8d31a4f

Please sign in to comment.