summaryrefslogtreecommitdiffstats
path: root/tools/h4toh5/h4toh5pal.c
blob: 556ecdc7ea3f2bce376c8761162de6b32ef7f83f (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
195
196
197
198
199
200
201
202
/*-------------------------------------------------------------------------
 *
 * Copyright (C) 2000   National Center for Supercomputing Applications.
 *                      All rights reserved.
 *
 *-------------------------------------------------------------------------
 */

/******************************************************************************

  Description: 

1. converter

See HDF4 to HDF5 mapping specification at
(http://hdf.ncsa.uiuc.edu/HDF5/papers/h4toh5) for the default mapping 
from HDF4 object to HDF5 object.
 
The whole converter includes 10 files, h4toh5util.h, h4toh5main.h, h4toh5util.c, h4toh5main.c, h4toh5sds.c, h4toh5image.c,h4toh5vdata.c,h4toh5vgroup.c,h4toh5pal.c and h4toh5anno.c.

2. this file 

Converting an hdf4 palette object into a hdf5 dataset.
Author:  Kent Yang(ymuqun@ncsa.uiuc.edu)
 

*****************************************************************************/

#include "h4toh5main.h"


/*-------------------------------------------------------------------------
 * Function:	Palette_h4_to_h5
 *
 * Purpose:     translate palette  into hdf5 dataset
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        
                file_id: file id
                pal_id: PALETTE identifier
		h5_g: hdf5 group id
		pal_name: path name of the group where all palettes are in

 *-------------------------------------------------------------------------
 */	

int Palette_h4_to_h5(int32 file_id,int32 pal_id,hid_t h5g,char*pal_name) {

  int32   ncomp;
  int32   pal_ref;
  int32   pal_type;
  int32   interlace_mode;
  int32   num_entries;
  void*   pal_data;
  size_t  h4memsize;
  size_t  h4size;
 
  char    palette_label[MAX_PAL_NAME];
  char    palette_class[MAX_PAL_NAME];
  char    palette_type[MAX_PAL_NAME];
  char    palette_colormodel[MAX_PAL_NAME];

  hid_t   h5memtype;
  hid_t   h5type;
  hid_t   h5d_sid;
  hid_t   h5dset;
  hsize_t h5dims[2];
  
  pal_ref = GRluttoref(pal_id);

  if(pal_ref <0) {
    printf("error in obtaining palette.\n");
    return FAIL;
  }

  /* no palette, just return. */
  if(pal_ref == 0) return SUCCEED;

  if(GRgetlutinfo(pal_id,&ncomp,&pal_type,&interlace_mode,&num_entries)==FAIL) {
    printf("error in getting palette information.\n");
    return FAIL;
  }

  if(h4type_to_h5type(pal_type,&h5memtype,&h4memsize,&h4size,&h5type)== FAIL) {
    fprintf(stderr,"failed to translate image datatype. \n");
    return FAIL;
  }
	
  /* according to mapping document, data type for palette will always be
     uint8. */

  if (h5type == H5T_STRING) {
    if(h5string_to_int(pal_type,&h5memtype,h4memsize,&h5type)==FAIL) {
      fprintf(stderr,"failed to translate H5T_STRING to int8.");
      return FAIL;
    }
  }

  h5dims[0] = num_entries;
  h5dims[1] = ncomp;
 
  pal_data = malloc(h4memsize*ncomp*num_entries);

  if (pal_data == NULL) {
    printf("error in allocating memory for palette data.\n");
    return FAIL;
  }

  if (GRreadlut(pal_id,(VOIDP)pal_data)==FAIL) {
    printf("error in reading palette data. \n");
    free(pal_data);
    return FAIL;
  }

  h5d_sid = H5Screate_simple(2,h5dims,NULL);

  if (h5d_sid <0) {
    printf("error in creating space.\n");
    free(pal_data);
    return FAIL;
  }

  h5dset = H5Dcreate(h5g,pal_name,h5type,h5d_sid,H5P_DEFAULT);

  if (h5dset < 0) {
    printf("error in creating dataset. \n");
    free(pal_data);
    H5Sclose(h5d_sid);
    return FAIL;
  }

  if (H5Dwrite(h5dset,h5memtype,h5d_sid,h5d_sid,H5P_DEFAULT,
	       (void *)pal_data)<0) {
     fprintf(stdout,"error writing data for palette data\n");
     free(pal_data);
     H5Sclose(h5d_sid);
     H5Dclose(h5dset);
     return FAIL;
  }
  free(pal_data);

  
  strcpy(palette_label,PALABEL);
  strcpy(palette_class,PALETTE);
  strcpy(palette_type,PAL_TYPE);
  strcpy(palette_colormodel,RGB);
  /* convert palette annotation into attribute of palette dataset.
     Since there are no routines to find the exact tag of palette object,
     we will check three possible object tags of palette objects, that is:
     DFTAG_LUT. If the object tag of palette object is 
     falling out of this scope, we will not convert annotations into
     hdf5 attributes; it is user's responsibility to make sure that object tags
     for palette objects are DFTAG_LUT.*/

  if(Annoobj_h4_to_h5(file_id,pal_ref,DFTAG_LUT,h5dset)== FAIL){
    printf("failed to convert palette annotation into hdf5 attribute.\n");
    H5Sclose(h5d_sid);
    H5Dclose(h5dset);
    return FAIL;
  }

  if(h4_transpredattrs(h5dset,HDF4_OBJECT_TYPE,palette_label)==FAIL) {
    printf("unable to transfer palette label to HDF4 OBJECT TYPE.\n");
    H5Sclose(h5d_sid);
    H5Dclose(h5dset);
    return FAIL;
  }

  if(h4_transpredattrs(h5dset,HDF4_PALETTE_CLASS,palette_class)==FAIL){
    printf("unable to transfer palette class to HDF4 PALETTE CLASS.\n");
    H5Sclose(h5d_sid);
    H5Dclose(h5dset);
    return FAIL;
  }

  if(h4_transpredattrs(h5dset,HDF4_PALETTE_TYPE,palette_type)==FAIL){
    printf("unable to transfer palette type to HDF4 PALETTE TYPE.\n");
    H5Sclose(h5d_sid);
    H5Dclose(h5dset);
    return FAIL;
  }

  if(h4_transpredattrs(h5dset,PAL_COLORMODEL,palette_colormodel)==FAIL){
    printf("unable to transfer palette type to HDF4 PALETTE TYPE.\n");
    H5Sclose(h5d_sid);
    H5Dclose(h5dset);
    return FAIL;
  }
  if(h4_transnumattr(h5dset,HDF4_REF_NUM,pal_ref)==FAIL) {
    printf("unable to transfer palette reference number to HDF4 REF. NUM.\n");
    H5Sclose(h5d_sid);
    H5Dclose(h5dset);
    return FAIL;
  }
  return SUCCEED;
}