/
golibraw.go
207 lines (161 loc) · 5.37 KB
/
golibraw.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package golibraw
// #cgo LDFLAGS: -lraw
// #include "libraw/libraw.h"
import "C"
import (
"bytes"
"fmt"
"image"
"log"
"os"
"time"
"unsafe"
"github.com/lmittmann/ppm"
)
// ImgMetadata contiene alcuni dati relativi all'immagine letti da libraw
type ImgMetadata struct {
ScattoTimestamp int64
ScattoDataOra string
}
type rawImg struct {
Height int
Width int
Bits uint
DataSize int
Data []byte
}
func (r rawImg) fullBytes() []byte {
header := fmt.Sprintf("P6\n%d %d\n%d\n", r.Width, r.Height, (1<<r.Bits)-1)
return append([]byte(header), r.Data...)
}
func handleError(msg string, err int) {
if err != 0 {
fmt.Printf("ERROR libraw %v\n", C.libraw_strerror(C.int(err)))
}
}
func lrInit() *C.libraw_data_t {
librawProcessor := C.libraw_init(0)
return librawProcessor
}
// ExportEmbeddedJPEG immagine JPEG salvata in file RAW
func ExportEmbeddedJPEG(inputPath string, inputfile os.FileInfo, exportPath string) (string, error) {
outfile := exportPath + "/" + inputfile.Name() + "_embedded.jpg"
infile := inputPath + "/" + inputfile.Name()
if _, err := os.Stat(outfile); os.IsNotExist(err) {
librawProcessor := lrInit()
C.libraw_open_file(librawProcessor, C.CString(infile))
ret := C.libraw_unpack_thumb(librawProcessor)
handleError("unpack thumb", int(ret))
//ret = C.libraw_dcraw_process(iprc)
//handleError("process", int(ret))
//iprc.params.output_tiff = 1
//outfile := exportPath + "/" + inputfile.Name() + ".tiff"
//fmt.Printf("exporting %s -> %s \n", inputfile.Name(), outfile)
ret = C.libraw_dcraw_thumb_writer(librawProcessor, C.CString(outfile))
handleError("save thumb", int(ret))
C.libraw_recycle(librawProcessor)
// lrClose(librawProcessor)
}
return outfile, nil
}
// Raw2Image creates a Image from raw file
func Raw2Image(infile string) (image.Image, ImgMetadata, error) {
t0 := time.Now()
librawProcessor := lrInit()
C.libraw_open_file(librawProcessor, C.CString(infile))
ret := C.libraw_unpack(librawProcessor)
handleError("unpack", int(ret))
ret = C.libraw_dcraw_process(librawProcessor)
handleError("dcraw processing", int(ret))
var makeImageErr C.int
//typedef struct
//{
// enum LibRaw_image_formats type;
// ushort height, width, colors, bits;
// unsigned int data_size;
// unsigned char data[1];
//} libraw_processed_image_t;
//
myImage := C.libraw_dcraw_make_mem_image(librawProcessor, &makeImageErr)
handleError("dcraw processing", int(makeImageErr))
dataBytes := make([]uint8, int(myImage.data_size))
// in C sta usando un flexible array ... non so come accedervi in golang, così però sembra funzionare
start := unsafe.Pointer(&myImage.data)
size := unsafe.Sizeof(uint8(0))
for i := 0; i < int(myImage.data_size); i++ {
item := *(*uint8)(unsafe.Pointer(uintptr(start) + size*uintptr(i)))
dataBytes[i] = item
// fmt.Printf("%d => %d \n", i, item)
}
rawImage := rawImg{
Height: int(myImage.height),
Width: int(myImage.width),
DataSize: int(myImage.data_size),
Bits: uint(myImage.bits),
Data: dataBytes,
}
/*
outfilename := fmt.Sprintf(".rawtool/%s.ppm", inputfile.Name())
f, err := os.Create(outfilename)
if err != nil {
fmt.Println(err)
return nil, fmt.Errorf("errore in creazione file out")
}
n2, err := f.Write(rawImage.fullBytes())
if err != nil {
fmt.Println(err)
f.Close()
return nil, fmt.Errorf("errore in scrittura file out")
}
fmt.Println(n2, "bytes written successfully")
err = f.Close()
*/
//iparam := C.libraw_get_iparams(librawProcessor)
//log.Printf(" iparam = %v", iparam)
//lensinfo := C.libraw_get_lensinfo(librawProcessor)
//log.Printf(" lensinfo = %v", lensinfo)
other := C.libraw_get_imgother(librawProcessor)
//log.Printf(" OTHER = %v", other.timestamp)
// data di scatto (timestamp)
timestamp := int64(other.timestamp)
dataScatto := time.Unix(timestamp, 0)
C.libraw_dcraw_clear_mem(myImage)
C.libraw_recycle(librawProcessor)
log.Printf(" raw decoding required %v", time.Since(t0))
fullbytes := rawImage.fullBytes()
result, err := ppm.Decode(bytes.NewReader(fullbytes))
return result,
ImgMetadata{ScattoTimestamp: timestamp,
ScattoDataOra: dataScatto.Format("2006-01-02T15:04:05")}, err
//outfile := "./" + inputfile.Name() + ".ppm"
//fmt.Printf("exporting %s -> %s \n", inputfile.Name(), outfile)
//ret = C.libraw_dcraw_ppm_tiff_writer(iprc, C.CString(outfile))
//handleError("save ppm", int(ret))
//}
// return nil, nil
}
func Export(inputPath string, inputfile os.FileInfo, exportPath string) error {
// FIXME controllare che file input esiste
// lanciare errore se file input non esiste
outfile := exportPath + "/" + inputfile.Name() + ".ppm"
infile := inputPath + "/" + inputfile.Name()
if _, err := os.Stat(outfile); os.IsNotExist(err) {
librawProcessor := lrInit()
C.libraw_open_file(librawProcessor, C.CString(infile))
ret := C.libraw_unpack(librawProcessor)
handleError("unpack", int(ret))
ret = C.libraw_dcraw_process(librawProcessor)
handleError("dcraw processing", int(ret))
//iprc.params.output_tiff = 1
//outfile := exportPath + "/" + inputfile.Name() + ".tiff"
fmt.Printf("exporting %s -> %s \n", inputfile.Name(), outfile)
ret = C.libraw_dcraw_ppm_tiff_writer(librawProcessor, C.CString(outfile))
handleError("save ppm", int(ret))
C.libraw_recycle(librawProcessor)
//lrClose(librawProcessor)
}
return nil
}
func lrClose(iprc *C.libraw_data_t) {
C.libraw_close(iprc)
}