Skip to content

Commit

Permalink
Add enableConcurrentImageProcessing config
Browse files Browse the repository at this point in the history
  • Loading branch information
albertusdev committed Jul 16, 2019
1 parent 484147f commit d773e5e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions config.yaml.example
Expand Up @@ -28,3 +28,5 @@ port: 3000

cache:
time: 31536000 # One year

enableConcurrentImageProcessing: true
27 changes: 17 additions & 10 deletions pkg/config/config.go
Expand Up @@ -7,12 +7,13 @@ import (
)

type config struct {
logLevel string
app app
debugMode bool
port int
cacheTime int
source source
logLevel string
app app
debugMode bool
port int
cacheTime int
source source
enableConcurrentImageProcessing bool
}

var instance *config
Expand Down Expand Up @@ -54,10 +55,11 @@ func newConfig() *config {
version: v.GetString("app.version"),
description: v.GetString("app.description"),
},
debugMode: v.GetBool("debug"),
port: port,
cacheTime: v.GetInt("cache.time"),
source: s,
debugMode: v.GetBool("debug"),
port: port,
cacheTime: v.GetInt("cache.time"),
source: s,
enableConcurrentImageProcessing: v.GetBool("enableConcurrentImageProcessing"),
}
}

Expand Down Expand Up @@ -105,3 +107,8 @@ func CacheTime() int {
func Source() *source {
return &getConfig().source
}

// ConcurrentImageProcessingEnabled returns true if we want to process image using multiple cores (checking isOpaque)
func ConcurrentImageProcessingEnabled() bool {
return getConfig().enableConcurrentImageProcessing
}
10 changes: 8 additions & 2 deletions pkg/processor/native/utils.go
Expand Up @@ -2,6 +2,7 @@ package native

import (
"github.com/anthonynsimon/bild/parallel"
"github.com/gojek/darkroom/pkg/config"
"github.com/gojek/darkroom/pkg/processor"
"image"
)
Expand All @@ -16,15 +17,20 @@ func isOpaque(im image.Image) bool {
// No Opaque() method, we need to loop through all pixels and check manually:
rect := im.Bounds()
isOpaque := true
parallel.Line(rect.Dy(), func(start, end int) {
f := func(start, end int) {
for y := rect.Min.Y + start; isOpaque && y < rect.Min.Y+end; y++ {
for x := rect.Min.X; isOpaque && x < rect.Max.X; x++ {
if _, _, _, a := im.At(x, y).RGBA(); a != 0xffff {
isOpaque = false // Found a non-opaque pixel: image is non-opaque
}
}
}
})
}
if config.ConcurrentImageProcessingEnabled() {
parallel.Line(rect.Dy(), f)
} else {
f(rect.Min.Y, rect.Max.Y)
}
return isOpaque // All pixels are opaque, so is the image
}

Expand Down

0 comments on commit d773e5e

Please sign in to comment.