Skip to content

Commit

Permalink
Merge pull request #25 from mdouchement/feat-named_packages
Browse files Browse the repository at this point in the history
Named packages
  • Loading branch information
mdouchement authored Jan 27, 2019
2 parents 0535ac3 + 5f2b985 commit e16dd62
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 162 deletions.
7 changes: 4 additions & 3 deletions filter/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/hdrcolor"
"github.com/mdouchement/hdr/mathx"
)

// An Apply filter let's you apply any function on two colors.
Expand Down Expand Up @@ -91,9 +92,9 @@ func (f *Apply) HDRAt(x, y int) hdrcolor.Color {
func (f *Apply) At(x, y int) color.Color {
r, g, b, _ := f.HDRAt(x, y).HDRRGBA()
return color.RGBA{
R: uint8(clamp(0, 255, int(r*255))),
G: uint8(clamp(0, 255, int(g*255))),
B: uint8(clamp(0, 255, int(b*255))),
R: uint8(mathx.Clamp(0, 255, int(r*255))),
G: uint8(mathx.Clamp(0, 255, int(g*255))),
B: uint8(mathx.Clamp(0, 255, int(b*255))),
A: 255,
}
}
13 changes: 7 additions & 6 deletions filter/fast_bilateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/hdrcolor"
"github.com/mdouchement/hdr/mathx"
"gonum.org/v1/gonum/mat"
)

Expand Down Expand Up @@ -125,9 +126,9 @@ func (f *FastBilateral) HDRAt(x, y int) hdrcolor.Color {
func (f *FastBilateral) At(x, y int) color.Color {
r, g, b, _ := f.HDRAt(x, y).HDRRGBA()
return color.RGBA{
R: uint8(clamp(0, 255, int(r*255))),
G: uint8(clamp(0, 255, int(g*255))),
B: uint8(clamp(0, 255, int(b*255))),
R: uint8(mathx.Clamp(0, 255, int(r*255))),
G: uint8(mathx.Clamp(0, 255, int(g*255))),
B: uint8(mathx.Clamp(0, 255, int(b*255))),
A: 255,
}
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func (f *FastBilateral) minmax() {

// fmt.Println("ssp:", f.SigmaSpace, " - sra:", f.SigmaRange)
// fmt.Println("min:", f.min, "- max:", f.max)
// fmt.Println("size:", mul(f.size...), f.size)
// fmt.Println("size:", mathx.Mul(f.size...), f.size)
}

func (f *FastBilateral) downsampling() {
Expand Down Expand Up @@ -266,8 +267,8 @@ func (f *FastBilateral) nLinearInterpolation(offset ...float64) *cell {
for n, s := range f.size {
off := offset[n]
size := s - 1
index[n] = clamp(0, size, int(off))
indexx[n] = clamp(0, size, index[n]+1)
index[n] = mathx.Clamp(0, size, int(off))
indexx[n] = mathx.Clamp(0, size, index[n]+1)
alpha[n] = off - float64(index[n])
}

Expand Down
22 changes: 0 additions & 22 deletions filter/filter.go

This file was deleted.

7 changes: 4 additions & 3 deletions filter/log10.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/hdrcolor"
"github.com/mdouchement/hdr/mathx"
)

// A Log10 applies a log10 for all pixels of the image.
Expand Down Expand Up @@ -78,9 +79,9 @@ func (f *Log10) HDRAt(x, y int) hdrcolor.Color {
func (f *Log10) At(x, y int) color.Color {
r, g, b, _ := f.HDRAt(x, y).HDRRGBA()
return color.RGBA{
R: uint8(clamp(0, 255, int(r*255))),
G: uint8(clamp(0, 255, int(g*255))),
B: uint8(clamp(0, 255, int(b*255))),
R: uint8(mathx.Clamp(0, 255, int(r*255))),
G: uint8(mathx.Clamp(0, 255, int(g*255))),
B: uint8(mathx.Clamp(0, 255, int(b*255))),
A: 255,
}
}
7 changes: 4 additions & 3 deletions filter/pow10.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/hdrcolor"
"github.com/mdouchement/hdr/mathx"
)

// A Pow10 applies a pow10 for all pixels of the image.
Expand Down Expand Up @@ -78,9 +79,9 @@ func (f *Pow10) HDRAt(x, y int) hdrcolor.Color {
func (f *Pow10) At(x, y int) color.Color {
r, g, b, _ := f.HDRAt(x, y).HDRRGBA()
return color.RGBA{
R: uint8(clamp(0, 255, int(r*255))),
G: uint8(clamp(0, 255, int(g*255))),
B: uint8(clamp(0, 255, int(b*255))),
R: uint8(mathx.Clamp(0, 255, int(r*255))),
G: uint8(mathx.Clamp(0, 255, int(g*255))),
B: uint8(mathx.Clamp(0, 255, int(b*255))),
A: 255,
}
}
27 changes: 14 additions & 13 deletions filter/y_fast_bilateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/hdrcolor"
"github.com/mdouchement/hdr/mathx"
"gonum.org/v1/gonum/mat"
)

Expand Down Expand Up @@ -112,9 +113,9 @@ func (f *YFastBilateral) HDRAt(x, y int) hdrcolor.Color {
func (f *YFastBilateral) At(x, y int) color.Color {
r, g, b, _ := f.HDRAt(x, y).HDRRGBA()
return color.RGBA{
R: uint8(clamp(0, 255, int(r*255))),
G: uint8(clamp(0, 255, int(g*255))),
B: uint8(clamp(0, 255, int(b*255))),
R: uint8(mathx.Clamp(0, 255, int(r*255))),
G: uint8(mathx.Clamp(0, 255, int(g*255))),
B: uint8(mathx.Clamp(0, 255, int(b*255))),
A: 255,
}
}
Expand Down Expand Up @@ -163,14 +164,14 @@ func (f *YFastBilateral) minmax() {

// fmt.Println("ssp:", f.SigmaSpace, " - sra:", f.SigmaRange)
// fmt.Println("min:", f.min, "- max:", f.max)
// fmt.Println("size:", mul(f.size...), f.size)
// fmt.Println("size:", mathx.Mul(f.size...), f.size)
}

func (f *YFastBilateral) downsampling() {
d := f.HDRImage.Bounds()
offset := make([]int, yDimension)

size := mul(f.size...)
size := mathx.Mul(f.size...)
dim := yDimension - 1 // # 1 luminance and 1 threshold (edge weight)
f.grid = mat.NewDense(size, dim, make([]float64, dim*size))

Expand All @@ -193,7 +194,7 @@ func (f *YFastBilateral) downsampling() {
}

func (f *YFastBilateral) convolution() {
size := mul(f.size...)
size := mathx.Mul(f.size...)
dim := yDimension - 1 // # luminance and 1 threshold (edge weight)
buffer := mat.NewDense(size, dim, make([]float64, dim*size))

Expand Down Expand Up @@ -239,12 +240,12 @@ func (f *YFastBilateral) trilinearInterpolation(gx, gy, gz float64) float64 {
depth := f.size[2]

// Index
x := clamp(0, width-1, int(gx))
xx := clamp(0, width-1, x+1)
y := clamp(0, height-1, int(gy))
yy := clamp(0, height-1, y+1)
z := clamp(0, depth-1, int(gz))
zz := clamp(0, depth-1, z+1)
x := mathx.Clamp(0, width-1, int(gx))
xx := mathx.Clamp(0, width-1, x+1)
y := mathx.Clamp(0, height-1, int(gy))
yy := mathx.Clamp(0, height-1, y+1)
z := mathx.Clamp(0, depth-1, int(gz))
zz := mathx.Clamp(0, depth-1, z+1)

// Alpha
xa := gx - float64(x)
Expand All @@ -266,7 +267,7 @@ func (f *YFastBilateral) trilinearInterpolation(gx, gy, gz float64) float64 {
func (f *YFastBilateral) offset(size ...int) (n int) {
n = size[0] // x
for i, v := range size[1:] {
n += v * mul(f.size[0:i+1]...) // y, z
n += v * mathx.Mul(f.size[0:i+1]...) // y, z
}
return
}
12 changes: 12 additions & 0 deletions mathx/float64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mathx

// ClampF64 forces v to be between min and max.
func ClampF64(min, max, v float64) float64 {
if v < min {
v = min
}
if v > max {
v = max
}
return v
}
31 changes: 31 additions & 0 deletions mathx/int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mathx

// Abs returns the absolute value of x.
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}

// Clamp forces v to be between min and max.
func Clamp(min, max, v int) int {
if v < min {
v = min
}
if v > max {
v = max
}
return v
}

// Mul multiplies the given ints together.
func Mul(ints ...int) (n int) {
n = 1
for _, v := range ints {
if v != 0 {
n *= v
}
}
return
}
12 changes: 6 additions & 6 deletions util/parallel.go → parallel/parallel.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package parallel

import (
"image"
Expand All @@ -10,13 +10,13 @@ import (

var ncpu = runtime.NumCPU()

// ParallelR runs Parallel with the given r boundaries.
func ParallelR(r image.Rectangle, f func(x1, y1, x2, y2 int)) chan struct{} {
return Parallel(r.Dx(), r.Dy(), f)
// TilesR runs Tiles with the given r boundaries.
func TilesR(r image.Rectangle, f func(x1, y1, x2, y2 int)) chan struct{} {
return Tiles(r.Dx(), r.Dy(), f)
}

// Parallel runs f in runtime.NumCPU() parallel tiles.
func Parallel(width, height int, f func(x1, y1, x2, y2 int)) chan struct{} {
// Tiles runs f in runtime.NumCPU() parallel tiles.
func Tiles(width, height int, f func(x1, y1, x2, y2 int)) chan struct{} {
// FIXME use context
wg := &sync.WaitGroup{}
completed := make(chan struct{})
Expand Down
31 changes: 16 additions & 15 deletions tmo/custom_reinhard05.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (

"github.com/mdouchement/hdr"
"github.com/mdouchement/hdr/filter"
"github.com/mdouchement/hdr/util"
"github.com/mdouchement/hdr/mathx"
"github.com/mdouchement/hdr/parallel"
)

// A CustomReinhard05 is a custom Reinhard05 TMO implementation.
// It looks like a JPEG photo taken with a smartphone.
// It provides a quick render with less RAM consumption than Reinhard05.
type CustomReinhard05 struct {
HDRImage hdr.Image
HDRImage hdr.Image
// Brightness is included in [-50, 50] with 1 increment step.
Brightness float64
Chromatic float64
Light float64
f float64
// Chromatic is included in [0, 1] with 0.01 increment step.
Chromatic float64
// Light is included in [0, 1] with 0.01 increment step.
Light float64
f float64
}

// NewDefaultCustomReinhard05 instanciates a new CustomReinhard05 TMO with default parameters.
Expand All @@ -29,13 +33,10 @@ func NewDefaultCustomReinhard05(m hdr.Image) *CustomReinhard05 {
// NewCustomReinhard05 instanciates a new CustomReinhard05 TMO.
func NewCustomReinhard05(m hdr.Image, brightness, chromatic, light float64) *CustomReinhard05 {
return &CustomReinhard05{
HDRImage: m,
// Brightness is included in [-50, 50] with 1 increment step.
Brightness: brightness * 10,
// Chromatic is included in [0, 1] with 0.01 increment step.
Chromatic: chromatic,
// Light is included in [0, 1] with 0.01 increment step.
Light: light * 10,
HDRImage: m,
Brightness: mathx.ClampF64(-50, 50, brightness) * 10,
Chromatic: mathx.ClampF64(0, 1, chromatic),
Light: mathx.ClampF64(0, 1, light) * 10,
}
}

Expand All @@ -61,7 +62,7 @@ func (t *CustomReinhard05) tonemap() (minSample, maxSample float64) {
minCh := make(chan float64)
maxCh := make(chan float64)

completed := util.ParallelR(qsImg.Bounds(), func(x1, y1, x2, y2 int) {
completed := parallel.TilesR(qsImg.Bounds(), func(x1, y1, x2, y2 int) {
min := 1.0
max := 0.0

Expand Down Expand Up @@ -120,7 +121,7 @@ func (t *CustomReinhard05) sampling(sample, lum float64) float64 {
}

func (t *CustomReinhard05) normalize(img *image.RGBA64, minSample, maxSample float64) {
completed := util.ParallelR(t.HDRImage.Bounds(), func(x1, y1, x2, y2 int) {
completed := parallel.TilesR(t.HDRImage.Bounds(), func(x1, y1, x2, y2 int) {
for y := y1; y < y2; y++ {
for x := x1; x < x2; x++ {
pixel := t.HDRImage.HDRAt(x, y)
Expand Down Expand Up @@ -153,7 +154,7 @@ func (t *CustomReinhard05) nrmz(channel, minSample, maxSample float64) uint16 {
channel = LinearInversePixelMapping(channel, LumPixFloor, LumSize)

// Clamp to solid black and solid white
channel = Clamp(channel)
channel = LDRClamp(channel)

return uint16(channel)
}
Loading

0 comments on commit e16dd62

Please sign in to comment.