Skip to content

Commit

Permalink
Improved DNG workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mdouchement committed Jan 1, 2019
1 parent 994de0d commit 9f12456
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

ifdLen = 12 // Length of an IFD entry in bytes.

// TIFF variants
// TIFF variantes
fTIFF = 0
fDNG = 1
)
Expand Down
96 changes: 49 additions & 47 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/mdouchement/hdr/format"
"github.com/mdouchement/hdr/hdrcolor"
"github.com/mdouchement/tiff/bayer"
"gonum.org/v1/gonum/mat"
)

// decode decodes the raw data of an image.
Expand Down Expand Up @@ -91,7 +90,7 @@ func (d *decoder) decode(dst image.Image, xmin, ymin, xmax, ymax int) error {
}
// Step 1 - Linearizing + Luminance ReScale used in Bayer.
if t, exists := d.features[tLinearizationTable]; exists {
fmt.Println("You need to linearize the CFA:", t.val)
fmt.Println("You may need to linearize the CFA:", t.val)
}
if t, exists := d.features[tBlackLevel]; exists {
opts.BlackLevel = t.asFloat(0)
Expand All @@ -103,56 +102,59 @@ func (d *decoder) decode(dst image.Image, xmin, ymin, xmax, ymax int) error {
}

// Step 2 - White Balancing
// if t, exists := d.features[tAsShotNeutral]; exists {
// // Invert the values and then rescale them all so that the green multiplier is 1.
// opts.WhiteBalance = make([]float64, len(t.val))
// for i := range t.val {
// opts.WhiteBalance[i] = 1 / t.asFloat(i)
// }
// opts.WhiteBalance[0] /= opts.WhiteBalance[1]
// opts.WhiteBalance[1] /= opts.WhiteBalance[1]
// opts.WhiteBalance[2] /= opts.WhiteBalance[1]
// } else {
// opts.WhiteBalance = []float64{1, 1, 1}
// }
opts.WhiteBalance = []float64{1, 1, 1}

// Step 3 - Demosaicing
bayer := bayer.NewBilinear(d.buf, opts)

// Step 4 - Color Space Correction TODO
camToXYZ := []float64{}
if t, exists := d.features[tColorMatrix2]; exists {
data := make([]float64, len(t.val))
if t, exists := d.features[tAsShotNeutral]; exists {
// Invert the values and then rescale them all so that the green multiplier is 1.
opts.WhiteBalance = make([]float64, len(t.val))
for i := range t.val {
data[i] = t.asFloat(i)
opts.WhiteBalance[i] = 1 / t.asFloat(i)
}
xyzToCam := mat.NewDense(3, 3, data) // nbOfRows should be equal to len(d.features[tCFAPlaneColor].val)
var im mat.Dense
im.Inverse(xyzToCam)
camToXYZ = append(camToXYZ, im.RawRowView(0)...)
camToXYZ = append(camToXYZ, im.RawRowView(1)...)
camToXYZ = append(camToXYZ, im.RawRowView(2)...)
} else if t, exists := d.features[tColorMatrix1]; exists {
data := make([]float64, len(t.val))
for i := range t.val {
data[i] = t.asFloat(i)
}
xyzToCam := mat.NewDense(3, 3, data) // nbOfRows should be equal to len(d.features[tCFAPlaneColor].val)
var im mat.Dense
im.Inverse(xyzToCam)
camToXYZ = append(camToXYZ, im.RawRowView(0)...)
camToXYZ = append(camToXYZ, im.RawRowView(1)...)
camToXYZ = append(camToXYZ, im.RawRowView(2)...)
opts.WhiteBalance[0] /= opts.WhiteBalance[1]
opts.WhiteBalance[1] /= opts.WhiteBalance[1]
opts.WhiteBalance[2] /= opts.WhiteBalance[1]
} else {
// sRBG->XYZ (D65)
camToXYZ = []float64{
0.4124564, 0.3575761, 0.1804375,
0.2126729, 0.7151522, 0.0721750,
0.0193339, 0.1191920, 0.9503041,
}
opts.WhiteBalance = []float64{1, 1, 1}
}

// Step 3 - Demosaicing
bayer := bayer.NewBilinear(d.buf, opts)

// Step 4 - Color Space Correction
// camToXYZ := []float64{}
// if t, exists := d.features[tColorMatrix2]; exists {
// data := make([]float64, len(t.val))
// for i := range t.val {
// data[i] = t.asFloat(i)
// }
// xyzToCam := mat.NewDense(3, 3, data) // nbOfRows should be equal to len(d.features[tCFAPlaneColor].val)
// var im mat.Dense
// im.Inverse(xyzToCam)
// camToXYZ = append(camToXYZ, im.RawRowView(0)...)
// camToXYZ = append(camToXYZ, im.RawRowView(1)...)
// camToXYZ = append(camToXYZ, im.RawRowView(2)...)
// } else if t, exists := d.features[tColorMatrix1]; exists {
// data := make([]float64, len(t.val))
// for i := range t.val {
// data[i] = t.asFloat(i)
// }
// xyzToCam := mat.NewDense(3, 3, data) // nbOfRows should be equal to len(d.features[tCFAPlaneColor].val)
// var im mat.Dense
// im.Inverse(xyzToCam)
// camToXYZ = append(camToXYZ, im.RawRowView(0)...)
// camToXYZ = append(camToXYZ, im.RawRowView(1)...)
// camToXYZ = append(camToXYZ, im.RawRowView(2)...)
// } else {
// // sRBG->XYZ (D65)
// camToXYZ = []float64{
// 0.4124564, 0.3575761, 0.1804375,
// 0.2126729, 0.7151522, 0.0721750,
// 0.0193339, 0.1191920, 0.9503041,
// }
// }
camToXYZ := []float64{
0.4124564, 0.3575761, 0.1804375,
0.2126729, 0.7151522, 0.0721750,
0.0193339, 0.1191920, 0.9503041,
}
// Step 5 - Brightness & Gamma correction TODO (or not because TMO handle it well)

//
Expand Down

0 comments on commit 9f12456

Please sign in to comment.