File: writehdf.c

package info (click to toggle)
libhdf4 4.2.15-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,476 kB
  • sloc: ansic: 153,757; sh: 15,814; fortran: 12,369; java: 4,731; xml: 1,150; makefile: 1,031; yacc: 680; pascal: 420; lex: 170; cpp: 77; csh: 41
file content (215 lines) | stat: -rw-r--r-- 7,487 bytes parent folder | download | duplicates (3)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <hdf.h>
#include "gif.h"
#include <string.h>
#include <stdlib.h>

int
WriteHDF(GifMemoryStruct, HDFName , GIFFileName)
GIFTOMEM GifMemoryStruct;
char     *HDFName;
char     *GIFFileName;
{
	GIFHEAD            gifHead;           /* GIF Header structure            */
    GIFIMAGEDESC	   gifImageDesc;      /* Logical Image Descriptor struct */

	
	intn  status;       /* status for functions returning an intn */
	int32 file_id,      /* HDF file identifier */
		gr_id,          /* GR interface identifier */
		ri_id,
		vgroup_id,		/* VGroup interface identifier */
		pal_id,
		start[2],       /* start position to write for each dimension */			
		edges[2],       /* number of elements to be written along each dimension */
		dim_sizes[2],   /* dimension sizes of the image array */
		interlace_mode, /* interlace mode of the image */
		data_type,      /* data type of the image data */
		ImageCount,
		CommentCount,
		ApplicationCount,
		PlainTextCount,
		i;


	/* compression structs */
	uint32 comp_type;	/* compression type */
	comp_info c_info;	/* the compression information for setcompress */

	char ImageName[256];	/* Image name for the GR Image */
	char CommentName[256];
	char ApplicationName[256];
	char PlainTextName[256];
	
	/* reference variables */
	uint32 gr_ref;		/* GR reference for the VGroup */

	/* Setting compression to gzip. If you want to change compression
	** type or params change it here
	*/
	c_info.deflate.level = 6;
	comp_type = COMP_CODE_NONE;
	data_type = DFNT_UINT8;
	interlace_mode = MFGR_INTERLACE_PIXEL;

	/* Get the stuff from the GIFMem data struct */
	gifHead = *(GifMemoryStruct.GifHeader);

	ImageCount = (int32)gifHead.ImageCount;
	CommentCount = (int32)gifHead.CommentCount;
	ApplicationCount = (int32)gifHead.ApplicationCount;
	PlainTextCount = (int32)gifHead.PlainTextCount;

	/*Start HDF file*/
	file_id = Hopen(HDFName, DFACC_CREATE, 0);
	if(file_id == -1) {
		printf("Unable to create HDF file");
		status = HEvalue(1);
		printf("%s\n", HEstring(status));
		exit(0);
	}

	if ((status = Vstart(file_id)) == -1) {
		printf("Could not start VGroup interface\n");
		printf("%s\n", HEstring(HEvalue(1)));
		exit(-1);
	}

	vgroup_id = Vattach(file_id , -1 , "w");
	
	status = Vsetname(vgroup_id , GIFFileName);
	
	status = Vsetclass(vgroup_id , "GIF");
	
	
	/* Put the global palette in as an attribute to the vgroup */
	if (gifHead.PackedField & 0x80) {
		status = Vsetattr (vgroup_id, "Global Palette" , DFNT_UINT8, 3 * gifHead.TableSize , (VOIDP)gifHead.HDFPalette);
		if (status) {
			printf("Could not add global palette.\n");
			printf("%s\n", HEstring(HEvalue(1)));
		}
	}
	for (i = 0 ; i < CommentCount ; i++) {
		sprintf(CommentName , "Comment Extension Data %d" , (int)i);
		status = Vsetattr (vgroup_id , CommentName , DFNT_CHAR8 , (int32)(GifMemoryStruct.GifCommentExtension[i])->DataSize , (VOIDP)(GifMemoryStruct.GifCommentExtension[i])->CommentData);
        HDfree(GifMemoryStruct.GifCommentExtension[i]);
	}
    HDfree(GifMemoryStruct.GifCommentExtension);
	
	for (i = 0 ; i < ApplicationCount ; i++) {
		sprintf(ApplicationName , "Application Extension Data %d", (int)i);
		status = Vsetattr (vgroup_id , ApplicationName , DFNT_CHAR8 , (int32)(GifMemoryStruct.GifApplicationExtension[i])->DataSize , (VOIDP)(GifMemoryStruct.GifApplicationExtension[i])->ApplicationData);
		sprintf(ApplicationName , "Application Extension Dump %d", (int)i);
		status = Vsetattr (vgroup_id , ApplicationName , DFNT_CHAR8 , (int32)11, (VOIDP)(GifMemoryStruct.GifApplicationExtension[i])->AEDump);
	}

	for (i = 0 ; i < PlainTextCount ; i++) {
		sprintf(PlainTextName , "PlainText Extension Data %d", (int)i);
		status = Vsetattr (vgroup_id , PlainTextName , DFNT_CHAR8 , (int32)(GifMemoryStruct.GifPlainTextExtension[i])->DataSize , (VOIDP)(GifMemoryStruct.GifPlainTextExtension[i])->PlainTextData);
		sprintf(PlainTextName , "PlainText Extension Dump %d", (int)i);
		status = Vsetattr (vgroup_id , PlainTextName , DFNT_CHAR8 , (int32)15, (VOIDP)(GifMemoryStruct.GifPlainTextExtension[i])->PTEDump);
	}

	gr_id = GRstart(file_id);
	/* Add GR images into VGroup */
	for(i = 0 ; i < ImageCount ; i++)
	{
		
		gifImageDesc = *(GifMemoryStruct.GifImageDesc[i]);
		
		dim_sizes[0] = gifImageDesc.ImageWidth;
		dim_sizes[1] = gifImageDesc.ImageHeight;
		
		start[0] = start[1] = 0;
		edges[0] = gifImageDesc.ImageWidth;
		edges[1] = gifImageDesc.ImageHeight;
		
		/* Create GR Image */
		sprintf(ImageName,"Image%d",(int)i);
		ri_id = GRcreate (gr_id, ImageName, 1, data_type, interlace_mode, dim_sizes);

		/* GRSetCompress */
		if ((status = GRsetcompress(ri_id, comp_type, &c_info)) == -1) {
			printf("Error occurred while setting compression\n");
			printf("%s\n", HEstring(HEvalue(1)));
			exit(-1);
		}

		/* Write the GR Image */
		if ((status = GRwriteimage(ri_id, start, NULL, edges, (VOIDP)gifImageDesc.Image)) == -1) {
			printf("Error occurred while trying to write GR image\n");
			printf("%s\n", HEstring(HEvalue(1)));
			exit(-1);
		}

		/* Create palette */
		pal_id = GRgetlutid (ri_id , 0);

		if ((status = GRwritelut (pal_id , 3, DFNT_UINT8, interlace_mode, 256, (VOIDP)gifImageDesc.HDFPalette)) == -1) {
			printf("Could not write palette\n");
			printf("%s\n", HEstring(HEvalue(1)));
			exit(-1);
		}
		
		/* Put both image and palette in VGroup */
		gr_ref = GRidtoref(ri_id);
		
		if ((status = GRendaccess(ri_id)) == -1) {
			printf("Could not terminate GR access\n");
			printf("%s\n", HEstring(HEvalue(1)));
			exit(-1);
		}

		
		
		/* Adding GR to vgroup */
		if((status = Vaddtagref(vgroup_id,(int32)1965,gr_ref))==-1) {
			printf("Could not add tag to Vgroup");
			printf("%s\n", HEstring(HEvalue(1)));
		}

		
	}

	/* Terminate GR access */
	if ((status = GRend (gr_id))==-1) {
		printf("Could not end GR access\n");
		printf("%s\n", HEstring(HEvalue(1)));
		printf("Trying to continue (file may be corrupt)...\n");
	}
	
	/* Terminate access to the VGroup */
	if ((status = Vdetach(vgroup_id))==-1) {
		printf("Could not detach Vgroup\n");
		printf("%s\n", HEstring(HEvalue(1)));
		printf("Trying to continure (file may be corrupt)...\n");
	}

	/* Terminate access to the V interface */
	if ((status = Vend(file_id))==-1) {
		printf("Could not end VGroup access\n");
		printf("%s\n", HEstring(HEvalue(1)));
		printf("Trying to continure (file may be corrupt)... \n");
	}

	/* Close the HDF file */
	if ((status = Hclose (file_id))==-1) {
		printf("Could not close HDF file. Fatal Error");
		printf("%s\n", HEstring(HEvalue(1)));
		return(-1);
	}
	return(0);
}