Skip to content

Commit

Permalink
Update ImageBuf to expose Reset methods; Update Read to support subim…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
justinfx committed May 20, 2022
1 parent c383cc8 commit 0a38003
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
5 changes: 3 additions & 2 deletions imagebuf.cpp
Expand Up @@ -64,10 +64,11 @@ void ImageBuf_clear(ImageBuf* buf) {
}

void ImageBuf_reset_subimage(ImageBuf* buf, const char* name, int subimage, int miplevel,
ImageCache *imagecache) {
ImageCache *imagecache, const ImageSpec* spec) {
std::string s_name(name);
static_cast<OIIO::ImageBuf*>(buf)->reset(s_name, subimage, miplevel,
static_cast<OIIO::ImageCache*>(imagecache));
static_cast<OIIO::ImageCache*>(imagecache),
static_cast<const OIIO::ImageSpec*>(spec));
}

void ImageBuf_reset_name_cache(ImageBuf* buf, const char* name, ImageCache *imagecache) {
Expand Down
133 changes: 133 additions & 0 deletions imagebuf.go
Expand Up @@ -159,9 +159,87 @@ func (i *ImageBuf) InitSpec(filename string, subimage, miplevel int) error {
if !ok {
return i.LastError()
}
runtime.KeepAlive(i)
return nil
}

// Reset forgets all previous info, and resets this ImageBuf to a new image
// that is uninitialized (no pixel values, no size or spec).
// 'cache' may be nil, or can be a specific cache to use instead of the global.
func (i *ImageBuf) Reset(filename string, cache *ImageCache) error {
c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))

var cachePtr unsafe.Pointer = nil
if cache != nil {
cachePtr = cache.ptr
}

C.ImageBuf_reset_name_cache(i.ptr, c_str, cachePtr)
err := i.LastError()
runtime.KeepAlive(i)
return err
}

// ResetSubImage forgets all previous info, and resets this ImageBuf to a new image
// that is uninitialized (no pixel values, no size or spec).
// If 'config' is not nil, it points to an ImageSpec giving requests
// or special instructions to be passed on to the eventual
// ImageInput.Open() call.
// 'cache' may be nil, or can be a specific cache to use instead of the global.
func (i *ImageBuf) ResetSubImage(filename string, subimage, miplevel int, cache *ImageCache, config *ImageSpec) error {
c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))

var cachePtr unsafe.Pointer = nil
if cache != nil {
cachePtr = cache.ptr
}

var specPtr unsafe.Pointer = nil
if config != nil {
specPtr = config.ptr
}

C.ImageBuf_reset_subimage(i.ptr, c_str, C.int(subimage), C.int(miplevel), cachePtr, specPtr)
err := i.LastError()
runtime.KeepAlive(i)
return err
}

// ResetSpec forgets all previous info, and reset this ImageBuf to a blank
// image of the given dimensions.
func (i *ImageBuf) ResetSpec(spec *ImageSpec) error {
if spec == nil || spec.ptr == nil {
return errors.New("nil ImageSpec")
}

var specPtr unsafe.Pointer = spec.ptr

C.ImageBuf_reset_spec(i.ptr, specPtr)
err := i.LastError()
runtime.KeepAlive(i)
return err
}

// ResetNameSpec forgets all previous info, and reset this ImageBuf to a blank
// image of the given name and dimensions.
func (i *ImageBuf) ResetNameSpec(filename string, spec *ImageSpec) error {
if spec == nil || spec.ptr == nil {
return errors.New("nil ImageSpec")
}

c_str := C.CString(filename)
defer C.free(unsafe.Pointer(c_str))

var specPtr unsafe.Pointer = spec.ptr

C.ImageBuf_reset_name_spec(i.ptr, c_str, specPtr)
err := i.LastError()
runtime.KeepAlive(i)
return err
}

// Read the file from disk. Generally will skip the read if we've already got a current
// version of the image in memory, unless force==true.
// This uses ImageInput underneath, so will read any file format for which an appropriate
Expand All @@ -172,6 +250,16 @@ func (i *ImageBuf) Read(force bool) error {
return ret
}

// Read the file from disk. Generally will skip the read if we've already got a current
// version of the image in memory, unless force==true.
// This uses ImageInput underneath, so will read any file format for which an appropriate
// imageio plugin can be found.
func (i *ImageBuf) ReadSubImage(subimage, miplevel int, force bool) error {
ret := i.ReadSubImageFormatCallback(subimage, miplevel, force, TypeUnknown, nil)
runtime.KeepAlive(i)
return ret
}

// Read the file from disk. Generally will skip the read if we've already got a current
// version of the image in memory, unless force==true.
// This uses ImageInput underneath, so will read any file format for which an appropriate
Expand All @@ -189,6 +277,23 @@ func (i *ImageBuf) ReadCallback(force bool, progress *ProgressCallback) error {
return ret
}

// Read the file from disk. Generally will skip the read if we've already got a current
// version of the image in memory, unless force==true.
// This uses ImageInput underneath, so will read any file format for which an appropriate
// imageio plugin can be found.
//
// This call optionally 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.
//
func (i *ImageBuf) ReadSubImageCallback(subimage, miplevel int, force bool, progress *ProgressCallback) error {
ret := i.ReadSubImageFormatCallback(subimage, miplevel, force, TypeUnknown, progress)
runtime.KeepAlive(i)
runtime.KeepAlive(progress)
return ret
}

// Read the file from disk. Generally will skip the read if we've already got a current
// version of the image in memory, unless force==true.
// This uses ImageInput underneath, so will read any file format for which an appropriate
Expand Down Expand Up @@ -217,6 +322,34 @@ func (i *ImageBuf) ReadFormatCallback(force bool, convert TypeDesc, progress *Pr
return nil
}

// Read the file from disk. Generally will skip the read if we've already got a current
// version of the image in memory, unless force==true.
// This uses ImageInput underneath, so will read any file format for which an appropriate
// imageio plugin can be found.
//
// Specify a specific conversion format or TypeUnknown for automatic handling.
//
// This call optionally 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.
//
func (i *ImageBuf) ReadSubImageFormatCallback(subimage, miplevel int, force bool, convert TypeDesc, progress *ProgressCallback) error {
var cbk unsafe.Pointer
if progress != nil {
cbk = unsafe.Pointer(progress)
}

ok := C.ImageBuf_read(i.ptr, C.int(subimage), C.int(miplevel), C.bool(force), C.TypeDesc(convert), cbk)
if !bool(ok) {
return i.LastError()
}
runtime.KeepAlive(i)
runtime.KeepAlive(progress)

return nil
}

// Write the image to the named file and file format
// (fileformat=="" means to infer the type from the filename extension).
func (i *ImageBuf) WriteFile(filepath, fileformat string) error {
Expand Down
3 changes: 2 additions & 1 deletion oiio.h
Expand Up @@ -232,7 +232,8 @@ ImageBuf* ImageBuf_New_Spec(const ImageSpec* spec);
void deleteImageBuf(ImageBuf* buf);

void ImageBuf_clear(ImageBuf* buf);
void ImageBuf_reset_subimage(ImageBuf* buf, const char* name, int subimage, int miplevel, ImageCache *imagecache);
void ImageBuf_reset_subimage(ImageBuf* buf, const char* name, int subimage, int miplevel,
ImageCache *imagecache, const ImageSpec* spec);
void ImageBuf_reset_name_cache(ImageBuf* buf, const char* name, ImageCache *imagecache);
void ImageBuf_reset_spec(ImageBuf* buf, ImageSpec* spec);
void ImageBuf_reset_name_spec(ImageBuf* buf, const char* name, const ImageSpec* spec);
Expand Down

0 comments on commit 0a38003

Please sign in to comment.