/
flat-fix.go
75 lines (63 loc) · 1.45 KB
/
flat-fix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
"strings"
)
import "os"
import "image"
import "image/color"
import "image/jpeg"
import "image/png"
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func flattenChannel(value1, value2 uint32) uint16 {
if value1 >= value2/2 {
return uint16(value1 - value2/2)
}
return 0
}
type ProxyImage struct {
image.Image
flat image.Image
}
func (p ProxyImage) At(x, y int) color.Color {
r1, g1, b1, _ := p.Image.At(x, y).RGBA()
r2, g2, b2, _ := p.flat.At(x, y).RGBA()
return color.NRGBA64{
flattenChannel(r1, r2),
flattenChannel(g1, g2),
flattenChannel(b1, b2),
0xFFFF}
}
func main() {
srcPath := os.Args[1]
flatPath := os.Args[2]
dstPath := os.Args[3]
reader, err := os.Open(srcPath)
check(err)
src, srcFmt, err := image.Decode(reader)
check(err)
fmt.Println("Decoded source", srcPath, "as", srcFmt)
reader, err = os.Open(flatPath)
check(err)
flat, name, err := image.Decode(reader)
check(err)
fmt.Println("Decoded flat", flatPath, "as", name)
if src.Bounds() != flat.Bounds() {
log.Fatal("Bounds not equal")
}
proxy := ProxyImage{src, flat}
writer, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, 0600)
check(err)
if strings.HasSuffix(dstPath, ".jpg") || strings.HasSuffix(dstPath, ".jpeg") {
jpeg.Encode(writer, proxy, &jpeg.Options{90})
} else if strings.HasSuffix(dstPath, ".png") {
png.Encode(writer, proxy)
} else {
log.Fatal(dstPath + "does not have .jpg or .png extension")
}
}