Example #1
0
// GetPixelRegion retrieves the rectangle of pixels defined by an ROI,
// at the current subimage and MIP-map level, storing the pixel values in
// a slice that has been casted to an interface, and in the pixel format type
// described by 'format'.
//
// 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:
//
//     val, err := buf.GetPixelRegion(roi, TypeFloat)
//     if err != nil {
//         panic(err.Error())
//     }
//     floatPixels := val.([]float32)
//
func (i *ImageBuf) GetPixelRegion(roi *ROI, format TypeDesc) (interface{}, error) {
	pixel_iface, ptr, err := allocatePixelBufferSize(roi.NumPixels()*roi.NumChannels(), format)
	if err != nil {
		return nil, err
	}

	ok := bool(C.ImageBuf_get_pixel_channels(
		i.ptr,
		C.int(roi.XBegin()), C.int(roi.XEnd()),
		C.int(roi.YBegin()), C.int(roi.YEnd()),
		C.int(roi.ZBegin()), C.int(roi.ZEnd()),
		C.int(roi.ChannelsBegin()), C.int(roi.ChannelsEnd()),
		(C.TypeDesc)(format), ptr),
	)

	if !ok {
		return nil, i.LastError()
	}

	return pixel_iface, nil
}
Example #2
0
// GetFloatPixels retrieves the rectangle of pixels spanning the entire image,
// at the current subimage and MIP-map level, storing the float pixel values in
// a []float32
func (i *ImageBuf) GetFloatPixels() ([]float32, error) {
	spec := i.Spec()
	size := spec.Width() * spec.Height() * spec.Depth() * spec.NumChannels()
	pixels := make([]float32, size)
	ptr := unsafe.Pointer(&pixels[0])

	roi := i.ROI()

	ok := bool(C.ImageBuf_get_pixel_channels(
		i.ptr,
		C.int(roi.XBegin()), C.int(roi.XEnd()),
		C.int(roi.YBegin()), C.int(roi.YEnd()),
		C.int(roi.ZBegin()), C.int(roi.ZEnd()),
		C.int(roi.ChannelsBegin()), C.int(roi.ChannelsEnd()),
		(C.TypeDesc)(TypeFloat), ptr),
	)

	if !ok {
		return nil, i.LastError()
	}

	return pixels, nil
}