-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimageinput.go
345 lines (301 loc) · 11.5 KB
/
imageinput.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package oiio
/*
#include "stdlib.h"
#include "oiio.h"
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
// ImageInput abstracts the reading of an image file in a file format-agnostic manner.
type ImageInput struct {
ptr unsafe.Pointer
}
func newImageInput(i unsafe.Pointer) *ImageInput {
in := &ImageInput{i}
runtime.SetFinalizer(in, deleteImageInput)
return in
}
func deleteImageInput(i *ImageInput) {
if i.ptr != nil {
C.ImageInput_close(i.ptr)
C.deleteImageInput(i.ptr)
i.ptr = nil
}
runtime.KeepAlive(i)
}
// Create an ImageInput subclass instance that is able to read the given file and open it,
// returning the opened ImageInput if successful. If it fails, return error.
func OpenImageInput(filename string) (*ImageInput, error) {
c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))
cfg := unsafe.Pointer(nil)
ptr := C.ImageInput_Open(c_str, cfg)
in := newImageInput(ptr)
return in, in.LastError()
}
// Destroy the object immediately instead of waiting for GC.
func (i *ImageInput) Destroy() {
runtime.SetFinalizer(i, nil)
deleteImageInput(i)
}
// Return the last error generated by API calls.
// An nil error will be returned if no error has occured.
func (i *ImageInput) LastError() error {
c_str := C.ImageInput_geterror(i.ptr)
runtime.KeepAlive(i)
if c_str == nil {
return nil
}
err := C.GoString(c_str)
C.free(unsafe.Pointer(c_str))
if err == "" {
return nil
}
return errors.New(err)
}
// Open file with given name. Return true if the file was found and opened okay.
func (i *ImageInput) Open(filename string) error {
i.Close()
deleteImageInput(i)
c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))
cfg := unsafe.Pointer(nil)
ptr := C.ImageInput_Open(c_str, cfg)
i.ptr = ptr
return i.LastError()
}
// Close an image that we are totally done with.
func (i *ImageInput) Close() error {
if !bool(C.ImageInput_close(i.ptr)) {
return i.LastError()
}
return nil
}
// Return the name of the format implemented by this image.
func (i *ImageInput) FormatName() string {
ret := C.GoString(C.ImageInput_format_name(i.ptr))
runtime.KeepAlive(i)
return ret
}
// Return true if the named file is file of the type for this ImageInput.
// The implementation will try to determine this as efficiently as possible,
// in most cases much less expensively than doing a full Open().
// Note that a file can appear to be of the right type (i.e., ValidFIle() returning true)
// but still fail a subsequent call to Open(), such as if the contents of the file are
// truncated, nonsensical, or otherwise corrupted.
func (i *ImageInput) ValidFile(filename string) bool {
c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))
ret := bool(C.ImageInput_valid_file(i.ptr, c_str))
runtime.KeepAlive(i)
return ret
}
// Given the name of a 'feature', return whether this ImageOutput
// supports output of images with the given properties.
// Feature names that ImageIO plugins are expected to recognize
// include:
// "tiles" Is this format able to write tiled images?
// "rectangles" Does this plugin accept arbitrary rectangular
// pixel regions, not necessarily aligned to
// scanlines or tiles?
// "random_access" May tiles or scanlines be written in
// any order (false indicates that they MUST
// be in successive order).
// "multiimage" Does this format support multiple subimages
// within a file?
// "appendsubimage" Does this format support adding subimages one at
// a time through open(name,spec,AppendSubimage)?
// If not, then open(name,subimages,specs) must
// be used instead.
// "mipmap" Does this format support multiple resolutions
// for an image/subimage?
// "volumes" Does this format support "3D" pixel arrays?
// "rewrite" May the same scanline or tile be sent more than
// once? (Generally, this will be true for
// plugins that implement interactive display.)
// "empty" Does this plugin support passing a NULL data
// pointer to write_scanline or write_tile to
// indicate that the entire data block is zero?
// "channelformats" Does the plugin/format support per-channel
// data formats?
// "displaywindow" Does the format support display ("full") windows
// distinct from the pixel data window?
// "origin" Does the format support a nonzero x,y,z
// origin of the pixel data window?
// "negativeorigin" Does the format support negative x,y,z
// and full_{x,y,z} origin values?
// "deepdata" Deep (multi-sample per pixel) data
//
// Note that main advantage of this approach, versus having
// separate individual supports_foo() methods, is that this allows
// future expansion of the set of possible queries without changing
// the API, adding new entry points, or breaking linkage
// compatibility.
func (i *ImageInput) Supports(feature string) bool {
c_str := C.CString(feature)
defer C.free(unsafe.Pointer(c_str))
ret := bool(C.ImageInput_supports(i.ptr, c_str))
runtime.KeepAlive(i)
return ret
}
// Return a reference to the image format specification of the current subimage/MIPlevel.
// Note that the contents of the spec are invalid before Open() or after Close(), and may
// change with a call to SeekSubImage().
func (i *ImageInput) Spec() *ImageSpec {
ptr := C.ImageInput_spec(i.ptr)
runtime.KeepAlive(i)
return &ImageSpec{ptr}
}
// CurrentSubimage returns the index of the subimage that is currently being read.
// The first subimage (or the only subimage, if there is just one) is number 0.
func (i *ImageInput) CurrentSubimage() int {
ret := int(C.ImageInput_current_subimage(i.ptr))
runtime.KeepAlive(i)
return ret
}
// Seek to the given subimage within the open image file.
// The first subimage of the file has index 0. Return true on
// success, false on failure (including that there is not a
// subimage with the specified index). The new
// subimage's vital statistics are put in newspec (and also saved
// in ImageInput.Spec()). The reader is expected to give the appearance
// of random access to subimages -- in other words,
// if it can't randomly seek to the given subimage, it should
// transparently close, reopen, and sequentially read through prior
// subimages.
func (i *ImageInput) SeekSubimage(index int, newSpec *ImageSpec) bool {
if index < 0 {
index = 0
}
if newSpec == nil || newSpec.ptr == nil {
newSpec = NewImageSpec(TypeUnknown)
}
ok := C.ImageInput_seek_subimage(i.ptr, C.int(index), newSpec.ptr)
runtime.KeepAlive(i)
runtime.KeepAlive(newSpec)
return bool(ok)
}
// Returns the index of the MIPmap image that is currently being read.
// The highest-res MIP level (or the only level, if there is just
// one) is number 0.
func (i *ImageInput) CurrentMipLevel() int {
ret := int(C.ImageInput_current_miplevel(i.ptr))
runtime.KeepAlive(i)
return ret
}
// Seek to the given subimage and MIP-map level within the open
// image file. The first subimage of the file has index 0, the
// highest-resolution MIP level has index 0. Return true on
// success, false on failure (including that there is not a
// subimage or MIP level with the specified index). The new
// subimage's vital statistics are put in newspec (and also saved
// in ImageInpit.Spec()). The reader is expected to give the appearance
// of random access to subimages and MIP levels -- in other words,
// if it can't randomly seek to the given subimage/level, it should
// transparently close, reopen, and sequentially read through prior
// subimages and levels.
func (i *ImageInput) SeekMipLevel(subimage, miplevel int, newSpec *ImageSpec) bool {
if subimage < 0 {
subimage = 0
}
if miplevel < 0 {
miplevel = 0
}
if newSpec == nil || newSpec.ptr == nil {
newSpec = NewImageSpec(TypeUnknown)
}
ok := C.ImageInput_seek_subimage_miplevel(i.ptr, C.int(subimage), C.int(miplevel), newSpec.ptr)
runtime.KeepAlive(i)
runtime.KeepAlive(newSpec)
return bool(ok)
}
// Read the entire image of width * height * depth * channels into contiguous float32 pixels.
// Read tiles or scanlines automatically.
func (i *ImageInput) ReadImage() ([]float32, error) {
spec := i.Spec()
size := spec.Width() * spec.Height() * spec.Depth() * spec.NumChannels()
pixels := make([]float32, size)
pixels_ptr := (*C.float)(unsafe.Pointer(&pixels[0]))
C.ImageInput_read_image_floats(i.ptr, pixels_ptr)
return pixels, i.LastError()
}
// Read the entire image of width * height * depth * channels into contiguous pixels.
// Read tiles or scanlines automatically.
//
// This call supports passing a callback pointer to both track the progress,
// and to optionally abort the processing. The callback function will receive
// a float32 value indicating the percentage done of the processing, and should
// return true if the process should abort, and false if it should continue.
//
// The underlying type of data is determined by the given TypeDesc.
// Returned interface{} will be:
// TypeUint8 => []uint8
// TypeInt8 => []int8
// TypeUint16 => []uint16
// TypeInt16 => []int16
// TypeUint => []uint
// TypeInt => []int
// TypeUint64 => []uint64
// TypeInt64 => []int64
// TypeHalf => []float32
// TypeFloat => []float32
// TypeDouble => []float64
//
// Example:
//
// // Without a callback
// val, err := in.ReadImageFormat(TypeFloat, nil)
// if err != nil {
// panic(err.Error())
// }
// floatPixels := val.([]float32)
//
// // With a callback
// var cbk ProgressCallback = func(done float32) bool {
// fmt.Printf("Progress: %0.2f\n", done)
// // Keep processing (return true to abort)
// return false
// }
// val, _ = in.ReadImageFormat(TypeFloat, &cbk)
// floatPixels = val.([]float32)
//
func (i *ImageInput) ReadImageFormat(format TypeDesc, progress *ProgressCallback) (interface{}, error) {
spec := i.Spec()
pixel_iface, ptr, err := allocatePixelBuffer(spec, format)
if err != nil {
return nil, err
}
var cbk unsafe.Pointer = nil
if progress != nil {
cbk = unsafe.Pointer(progress)
}
C.ImageInput_read_image_format(i.ptr, (C.TypeDesc)(format), ptr, cbk)
return pixel_iface, i.LastError()
}
// Read the scanline that includes pixels (*,y,z), converting if necessary
// from the native data format of the file into contiguous float32 pixels (z==0 for non-volume images).
// The size of the slice is: width * depth * channels
func (i *ImageInput) ReadScanline(y, z int) ([]float32, error) {
spec := i.Spec()
size := spec.Width() * spec.Depth() * spec.NumChannels()
pixels := make([]float32, size)
pixels_ptr := (*C.float)(unsafe.Pointer(&pixels[0]))
C.ImageInput_read_scanline_floats(i.ptr, C.int(y), C.int(z), pixels_ptr)
return pixels, i.LastError()
}
// Read the tile whose upper-left origin is (x,y,z),
// converting if necessary from the native data format of the file
// into contiguous float32 pixels.
// The size of the slice is: tilewidth * tileheight * depth * channels
// (z==0 for non-volume images.)
func (i *ImageInput) ReadTile(x, y, z int) ([]float32, error) {
spec := i.Spec()
size := spec.TilePixels()
pixels := make([]float32, size)
pixels_ptr := (*C.float)(unsafe.Pointer(&pixels[0]))
C.ImageInput_read_tile_floats(i.ptr, C.int(x), C.int(y), C.int(z), pixels_ptr)
return pixels, i.LastError()
}