forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5i_image.go
79 lines (71 loc) · 2.04 KB
/
h5i_image.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
// license that can be found in the LICENSE file.
package hdf5
// #include "hdf5.h"
// #include "hdf5_hl.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"errors"
"image"
"image/color"
"unsafe"
)
// newImage takes a image object and convert it to a hdf5 format and write to the id node
func newImage(id C.hid_t, name string, img image.Image) error {
if img == nil {
return errors.New("nil image!")
}
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
var c_width, c_height C.hsize_t
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
var gbuf []uint8
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, _ := img.At(x, y).RGBA()
gbuf = append(gbuf, uint8(r>>8))
gbuf = append(gbuf, uint8(g>>8))
gbuf = append(gbuf, uint8(b>>8))
}
}
c_width = C.hsize_t(width)
c_height = C.hsize_t(height)
c_image := (*C.uchar)(unsafe.Pointer(&gbuf[0]))
status := C.H5IMmake_image_24bit(id, c_name, c_width, c_height, C.CString("INTERLACE_PIXEL"), c_image)
if status < 0 {
return errors.New("Failed to create HDF5 true color image")
}
return nil
}
//
func getImage(id C.hid_t, name string) (image.Image, error) {
//TODO Should handle interlace and npal better, yet these two are not needed for simple image read and write
var width, height, planes C.hsize_t
var npals C.hssize_t
var interlace C.char
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
rc := C.H5IMget_image_info(id, c_name, &width, &height, &planes, &interlace, &npals)
err := h5err(rc)
if err != nil {
return nil, err
}
gbuf := make([]uint8, width*height*planes)
c_image := (*C.uchar)(unsafe.Pointer(&gbuf[0]))
rc = C.H5IMread_image(id, c_name, c_image)
err = h5err(rc)
if err != nil {
return nil, err
}
g_width := int(width)
g_height := int(height)
img := image.NewRGBA(image.Rect(0, 0, g_width, g_height))
for y := 0; y < g_height; y++ {
for x := 0; x < g_width; x++ {
img.Set(x, y, color.RGBA{gbuf[y*g_width*3+x*3], gbuf[y*g_width*3+x*3+1], gbuf[y*g_width*3+x*3+2], 255})
}
}
return img, err
}