summaryrefslogtreecommitdiffstats
path: root/src/readers/ufo-hdf5-reader.c
blob: e7c94df47c178aea5e340b50ffb80d6db5302b94 (plain)
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
/*
 * Copyright (C) 2011-2015 Karlsruhe Institute of Technology
 *
 * This file is part of Ufo.
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "common/hdf5.h"
#include "readers/ufo-reader.h"
#include "readers/ufo-hdf5-reader.h"


struct _UfoHdf5ReaderPrivate {
    hid_t file_id;
    hid_t dataset_id;
    hid_t src_dataspace_id;

    gint n_dims;
    hsize_t dims[3];
    guint current;
};

static void ufo_reader_interface_init (UfoReaderIface *iface);

G_DEFINE_TYPE_WITH_CODE (UfoHdf5Reader, ufo_hdf5_reader, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (UFO_TYPE_READER,
                                                ufo_reader_interface_init))

#define UFO_HDF5_READER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_HDF5_READER, UfoHdf5ReaderPrivate))

UfoHdf5Reader *
ufo_hdf5_reader_new (void)
{
    return g_object_new (UFO_TYPE_HDF5_READER, NULL);
}

static gboolean
ufo_hdf5_reader_can_open (UfoReader *reader,
                          const gchar *filename)
{
    return ufo_hdf5_can_open (filename);
}

static gboolean
ufo_hdf5_reader_open (UfoReader *reader,
                      const gchar *filename,
                      guint start,
                      GError **error)
{
    UfoHdf5ReaderPrivate *priv;
    gchar *h5_filename;
    gchar *h5_dataset;
    gchar **components;

    priv = UFO_HDF5_READER_GET_PRIVATE (reader);
    components = g_strsplit (filename, ":", 2);

    if (components[1] == NULL) {
        g_set_error_literal (error, UFO_TASK_ERROR, UFO_TASK_ERROR_SETUP,
                             "hdf5: must specify dataset name after colon");
        return FALSE;
    }

    h5_filename = components[0];
    h5_dataset = components[1];

    priv->file_id = H5Fopen (h5_filename, H5F_ACC_RDWR, H5P_DEFAULT);
    priv->dataset_id = H5Dopen (priv->file_id, h5_dataset, H5P_DEFAULT);
    priv->src_dataspace_id = H5Dget_space (priv->dataset_id);
    priv->n_dims = H5Sget_simple_extent_ndims (priv->src_dataspace_id);

    if (priv->n_dims > 3) {
        g_set_error_literal (error, UFO_TASK_ERROR, UFO_TASK_ERROR_SETUP,
                             "hdf5: no support for four-dimensional data");
        return FALSE;
    }

    H5Sget_simple_extent_dims (priv->src_dataspace_id, priv->dims, NULL);

    priv->current = start;
    g_strfreev (components);
    return TRUE;
}

static void
ufo_hdf5_reader_close (UfoReader *reader)
{
    UfoHdf5ReaderPrivate *priv;

    priv = UFO_HDF5_READER_GET_PRIVATE (reader);

    H5Sclose (priv->src_dataspace_id);
    H5Dclose (priv->dataset_id);
    H5Fclose (priv->file_id);
}

static gboolean
ufo_hdf5_reader_data_available (UfoReader *reader)
{
    UfoHdf5ReaderPrivate *priv;

    priv = UFO_HDF5_READER_GET_PRIVATE (reader);

    return priv->current < priv->dims[0];
}

static gsize
ufo_hdf5_reader_read (UfoReader *reader,
                      UfoBuffer *buffer,
                      UfoRequisition *requisition,
                      guint roi_y,
                      guint roi_height,
                      guint roi_step,
                      guint image_step)
{
    UfoHdf5ReaderPrivate *priv;
    gpointer data;
    hid_t dst_dataspace_id;
    hsize_t dst_dims[2];
    gsize num_read = 0;

    priv = UFO_HDF5_READER_GET_PRIVATE (reader);
    data = ufo_buffer_get_host_array (buffer, NULL);

    hsize_t offset[3] = { priv->current, roi_y, 0 };
    hsize_t count[3] = { 1, roi_height, requisition->dims[0] };

    dst_dims[0] = roi_height;
    dst_dims[1] = requisition->dims[0];
    dst_dataspace_id = H5Screate_simple (2, dst_dims, NULL);

    H5Sselect_hyperslab (priv->src_dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
    H5Dread (priv->dataset_id, H5T_NATIVE_FLOAT, dst_dataspace_id, priv->src_dataspace_id, H5P_DEFAULT, data);
    H5Sclose (dst_dataspace_id);

    num_read = MIN (image_step, priv->dims[0] - priv->current);
    priv->current += num_read;

    return num_read;
}

static gboolean
ufo_hdf5_reader_get_meta (UfoReader *reader,
                          UfoRequisition *requisition,
                          gsize *num_images,
                          UfoBufferDepth *bitdepth,
                          GError **error)
{
    UfoHdf5ReaderPrivate *priv;

    priv = UFO_HDF5_READER_GET_PRIVATE (reader);

    requisition->n_dims = 2;
    requisition->dims[0] = priv->dims[2];
    requisition->dims[1] = priv->dims[1];
    *num_images = priv->dims[0];
    *bitdepth = UFO_BUFFER_DEPTH_32F;
    return TRUE;
}

static void
ufo_reader_interface_init (UfoReaderIface *iface)
{
    iface->can_open = ufo_hdf5_reader_can_open;
    iface->open = ufo_hdf5_reader_open;
    iface->close = ufo_hdf5_reader_close;
    iface->read = ufo_hdf5_reader_read;
    iface->get_meta = ufo_hdf5_reader_get_meta;
    iface->data_available = ufo_hdf5_reader_data_available;
}

static void
ufo_hdf5_reader_class_init(UfoHdf5ReaderClass *klass)
{
    g_type_class_add_private (G_OBJECT_CLASS (klass), sizeof (UfoHdf5ReaderPrivate));
}

static void
ufo_hdf5_reader_init (UfoHdf5Reader *self)
{
    self->priv = UFO_HDF5_READER_GET_PRIVATE (self);
}