44#include "config_hdf.h"
61inline int min(
int t1,
int t2)
63 return (t1 < t2 ? t1 : t2);
67void hdfistream_gri::_init(
void)
69 _ri_id = _attr_index = _pal_index = 0;
70 _npals = _nri = _nattrs = _nfattrs = _gr_id = _file_id = 0;
71 _index = _interlace_mode = -1;
72 _meta = _slab.set =
false;
77void hdfistream_gri::_get_fileinfo(
void)
79 if (GRfileinfo(_gr_id, &_nri, &_nfattrs) < 0)
85void hdfistream_gri::_get_iminfo(
void)
87 char junk0[hdfclass::MAXSTR];
88 int32 junk1, junk2, junk3, junk4[2];
89 if (GRgetiminfo(_ri_id, junk0, &junk1, &junk2, &junk3, junk4, &_nattrs)
93 GRgetlutinfo(GRgetlutid(_ri_id, 0), &junk1, &junk2, &junk3,
104void hdfistream_gri::_close_ri(
void)
108 _ri_id = _attr_index = _pal_index = _nattrs = 0;
115hdfistream_gri::hdfistream_gri(
const string filename):
hdfistream_obj
119 if (_filename.length() != 0)
120 open(_filename.c_str());
125void hdfistream_gri::open(
const char *filename)
131 if ((_file_id = Hopen((
char *) filename, DFACC_RDONLY, 0)) < 0)
133 _filename = filename;
134 if ((_gr_id = GRstart(_file_id)) < 0)
142void hdfistream_gri::close(
void)
149 _file_id = _gr_id = 0;
155void hdfistream_gri::seek(
int index)
157 if (_filename.length() == 0)
161 _ri_id = GRselect(_gr_id, _index);
162 if (!eos() && !bos())
167void hdfistream_gri::seek(
const char *name)
169 if (_filename.length() == 0)
171 int32 index = GRnametoindex(_gr_id, (
char *) name);
176void hdfistream_gri::seek_ref(
int ref)
178 if (_filename.length() == 0)
180 int32 index = GRreftoindex(_gr_id, ref);
185void hdfistream_gri::rewind(
void)
187 if (_filename.length() == 0)
195bool hdfistream_gri::bos(
void)
const
197 if (_filename.length() == 0)
209bool hdfistream_gri::eos(
void)
const
211 if (_filename.length() == 0)
219 return (_index >= _nri);
225bool hdfistream_gri::eo_attr(
void)
const
227 if (_filename.length() == 0)
233 return (_attr_index >= _nfattrs);
235 return (_attr_index >= _nattrs);
240void hdfistream_gri::setinterlace(int32 interlace_mode)
242 if (interlace_mode == MFGR_INTERLACE_PIXEL ||
243 interlace_mode == MFGR_INTERLACE_COMPONENT ||
244 interlace_mode == MFGR_INTERLACE_LINE)
245 _interlace_mode = interlace_mode;
252bool hdfistream_gri::eo_pal(
void)
const
254 if (_filename.length() == 0)
262 return (_pal_index >= _npals);
267void hdfistream_gri::setslab(vector < int >start, vector < int >edge,
268 vector < int >stride,
bool reduce_rank)
271 if (start.size() != edge.size() || edge.size() != stride.size() ||
275 if (start.size() == 3) {
277 start.erase(start.begin());
278 edge.erase(edge.begin());
279 stride.erase(stride.begin());
282 for (
int i = 0; i < 2; ++i) {
291 _slab.start[1 - i] = start[i];
292 _slab.edge[1 - i] = edge[i];
293 _slab.stride[1 - i] = stride[i];
296 _slab.reduce_rank = reduce_rank;
302 if (_filename.length() == 0)
305 hr.palettes = vector < hdf_palette > ();
306 hr.attrs = vector < hdf_attr > ();
314 char name[hdfclass::MAXSTR];
321 (_ri_id, name, &ncomp, &data_type, &il, dim_sizes, &nattrs) < 0)
323 hr.ref = GRidtoref(_ri_id);
325 hr.dims[0] = dim_sizes[0];
326 hr.dims[1] = dim_sizes[1];
328 if (_interlace_mode == -1) {
333 *
this >> hr.palettes;
336 hr.image.import(data_type);
341 nelts = _slab.edge[0] * _slab.edge[1] * ncomp;
343 int imagesize = nelts * DFKNTsize(data_type);
344 image =
new char[imagesize];
348 GRreqimageil(_ri_id, _interlace_mode);
350 cerr <<
"ncomp: " << ncomp <<
" imagesize: " << imagesize <<
352 cerr <<
"_slab.start = " << _slab.start[0] <<
"," << _slab.
353 start[1] <<
" _slab.edge = " << _slab.
354 edge[0] <<
"," << _slab.
355 edge[1] <<
" _slab.stride = " << _slab.
356 stride[0] <<
"," << _slab.stride[1] << endl;
359 (_ri_id, _slab.start, _slab.stride, _slab.edge,
366 zero[0] = zero[1] = 0;
367 nelts = dim_sizes[0] * dim_sizes[1] * ncomp;
369 int imagesize = nelts * DFKNTsize(data_type);
370 image =
new char[imagesize];
374 GRreqimageil(_ri_id, _interlace_mode);
376 cerr <<
"dim_sizes[0] = " << dim_sizes[0] <<
" dim_sizes[1] = "
377 << dim_sizes[1] << endl;
379 if (GRreadimage(_ri_id, zero, 0, dim_sizes, image) < 0) {
385 hr.image.import(data_type, image, nelts);
398hdfistream_gri & hdfistream_gri::operator>>(vector < hdf_gri > &hrv)
410 if (_filename.length() == 0)
425 char name[hdfclass::MAXSTR];
426 int32 number_type, count;
427 if (GRattrinfo(
id, _attr_index, name, &number_type, &count) < 0)
431 data =
new char[count * DFKNTsize(number_type)];
435 if (GRgetattr(
id, _attr_index, data) < 0) {
441 if (number_type == DFNT_CHAR)
442 count = (int32) min((
int) count, (
int) strlen((
char *) data));
445 ha.values.import(number_type, data, count);
459hdfistream_gri & hdfistream_gri::operator>>(vector < hdf_attr > &hav)
471 if (_filename.length() == 0)
477 if ((pal_id = GRgetlutid(_ri_id, _pal_index)) < 0)
480 int32 ncomp = 0, number_type = 0, num_entries = 0, junk0;
481 if (GRgetlutinfo(pal_id, &ncomp, &number_type, &junk0, &num_entries) <
486 hp.num_entries = num_entries;
491 if (number_type == DFNT_UCHAR8)
492 number_type = DFNT_UINT8;
494 int32 count = ncomp * num_entries;
495 if (number_type != 0) {
497 pal_data =
new char[count * DFKNTsize(number_type)];
501 GRreqlutil(pal_id, MFGR_INTERLACE_PIXEL);
502 if (GRreadlut(pal_id, pal_data) < 0) {
507 hp.table.import(number_type, pal_data, count);
520hdfistream_gri & hdfistream_gri::operator>>(vector < hdf_palette > &hpv)
532bool hdf_gri::_ok()
const
536 bool ok = (dims[0] * dims[1] * num_comp == image.size());
540 for (
int i = 0; i <
int (palettes.size()) && ok; i++)
541 ok = (palettes[i].ncomp * palettes[i].num_entries ==
542 palettes[i].table.size());