summaryrefslogtreecommitdiffstats
path: root/tools/h4toh5pal.c
blob: fc59cce0192f66d67c2408833754e5da44d42a45 (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
#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_NC_NAME];
  char    palette_class[MAX_NC_NAME];
  char    palette_type[MAX_NC_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(DFNT_UCHAR8,&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);

  /* 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_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;
}