File: hdfcomp.c

package info (click to toggle)
libhdf4 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 30,288 kB
  • sloc: ansic: 128,699; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 790; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (240 lines) | stat: -rw-r--r-- 11,920 bytes parent folder | download | duplicates (4)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  hdfcomp.c
 *  Re-compress Raster-8 HDF file
 */

#include <stdlib.h>

#include "hdf.h"

uint8 *space;
uint8  palette[768];
int32  xdim, ydim;
int    ispal;

int
main(int argc, char *argv[])
{
    int       i;
    char     *outfile;
    intn      jpeg_qual = 75; /* JPEG quality factor */
    uint16    prevref, writeref, compress = (uint16)0;
    comp_info cinfo;        /* compression structure */
    int32     out_fid;      /* file ID for the output file */
    intn      copy_flabel,  /* flag to indicate to copy file labels */
        copy_fdesc,         /* flag to indicate to copy file descriptions */
        copy_ilabel,        /* flag to indicate to copy image labels */
        copy_idesc;         /* flag to indicate to copy image descriptions */
    char *annbuf    = NULL; /* buffer to store annotations in */
    int32 annbuflen = 0;    /* length of the annotation buffer */

    if (argc < 3) {
        printf("%s,  version: 1.3   date: October 15, 1994\n", argv[0]);
        printf("  This utility will read in raster-8 images from an\n");
        printf("  HDF file and create a new HDF containing the\n");
        printf("  images in a compressed format.  Images will be\n");
        printf("  appended to outfile, if it exists.\n\n");
        printf("Usage:\n");
        printf(" hdfcomp outfile {[-c],[-r],[-i],[-j<quality>]} imagefile ...\n");
        printf("                 {[-c],[-r],[-i],[-j<quality>]} imagefile\n");
        printf("         -r: Store without compression (default)\n");
        printf("         -c: Store using RLE compression\n");
        printf("         -i: Store using IMCOMP compression (requires a");
        printf(" palette in the HDF file)\n");
        printf("         -j<quality>: Store using JPEG compression\n");
        printf("            with a quality factor from 1-100, 75 default\n");
        exit(1);
    }

    outfile = argv[1];

    /* open the output file so that we can write Annotations into it easily */
    if ((out_fid = Hopen(outfile, DFACC_ALL, 0)) == FAIL) {
        printf("Error opening output file: %s\n", outfile);
        exit(1);
    } /* end if */

    for (i = 2; i < argc; i++) {
        /* turn all the flags on (default settings) */
        copy_flabel = copy_fdesc = copy_ilabel = copy_idesc = TRUE;

        if (*argv[i] == '-') {
            switch (argv[i][1]) {
                case 'r': /* raster */
                    compress = (uint16)0;
                    break;
                case 'c': /* RLE */
                    compress = COMP_RLE;
                    break;
                case 'i': /* IMCOMP */
                    compress = COMP_IMCOMP;
                    break;
                case 'j': /* JPEG */
                    compress = COMP_JPEG;
                    if ((jpeg_qual = atoi(&argv[i][2])) <= 0 || jpeg_qual > 100) {
                        printf("Bad JPEG quality setting, should be between\n");
                        printf("1 and 100, using default value of 75\n");
                        jpeg_qual = 75;
                    }                                      /* end if */
                    cinfo.jpeg.quality        = jpeg_qual; /* set JPEG parameters */
                    cinfo.jpeg.force_baseline = TRUE;
                    break;
                default:
                    printf("Illegal option: %s, skipping....\n", argv[i]);
                    break;
            }
        }
        else { /* file name */
            /* copy the file annotations and labels over */
            if (copy_flabel == TRUE || copy_fdesc == TRUE) {
                intn  isfirst; /* flip-flop for first image */
                int32 annlen;  /* length of the annotation to copy */
                int32 old_fid; /* file ID for the old and new files */

                if ((old_fid = Hopen(argv[i], DFACC_READ, 0)) == FAIL) {
                    printf("Error opening input file: %s, skipping to next file\n", argv[i]);
                    continue;
                } /* end if */
                if (copy_flabel == TRUE) {
                    isfirst = 1;
                    while ((annlen = DFANgetfidlen(old_fid, isfirst)) != FAIL) {
                        if (annbuflen == 0 || annlen > annbuflen) {
                            if (annbuflen != 0)
                                free(annbuf);
                            if ((annbuf = (char *)malloc(annlen)) == NULL) {
                                printf("Error allocating buffer for annotation, aborting!\n");
                                exit(1);
                            } /* end if */
                            annbuflen = annlen;
                        } /* end if */
                        if (DFANgetfid(old_fid, annbuf, annbuflen, isfirst) == FAIL)
                            printf("Error reading file annotation from file:%s, continuing\n", argv[i]);
                        else {
                            if (DFANaddfid(out_fid, annbuf) == FAIL)
                                printf("Error wriring annotation to file:%s, continuing\n", outfile);
                        }            /* end else */
                        isfirst = 0; /* get the next label from the file */
                    }                /* end while */
                }                    /* end if */
                if (copy_fdesc == TRUE) {
                    isfirst = 1;
                    while ((annlen = DFANgetfdslen(old_fid, isfirst)) != FAIL) {
                        if (annbuflen == 0 || annlen > annbuflen) {
                            if (annbuflen != 0)
                                free(annbuf);
                            if ((annbuf = (char *)malloc(annlen)) == NULL) {
                                printf("Error allocating buffer for annotation, aborting!\n");
                                exit(1);
                            } /* end if */
                            annbuflen = annlen;
                        } /* end if */
                        if (DFANgetfds(old_fid, annbuf, annbuflen, isfirst) == FAIL)
                            printf("Error reading file annotation from file:%s, continuing\n", argv[i]);
                        else {
                            if (DFANaddfds(out_fid, annbuf, annlen) == FAIL)
                                printf("Error wriring annotation to file:%s, continuing\n", outfile);
                        }            /* end else */
                        isfirst = 0; /* get the next label from the file */
                    }                /* end while */
                }                    /* end if */
                Hclose(old_fid);     /* remember to close the file */
            }                        /* end if */

            /* copy the images over */
            while (DFR8getdims(argv[i], &xdim, &ydim, &ispal) >= 0) {
                prevref = DFR8lastref();
                if ((space = (uint8 *)malloc((size_t)(xdim * ydim))) == NULL) {
                    printf("Not enough memory to convert image");
                    exit(1);
                }

                if (DFR8getimage(argv[i], space, xdim, ydim, palette) < 0) {
                    printf("Error reading image from file %s\n", argv[i]);
                    exit(1);
                }
                if (ispal)
                    DFR8setpalette((uint8 *)palette);
                else if (compress == DFTAG_IMC) {
                    printf("Couldn't find palette for IMCOMP compression\n");
                    exit(1);
                }

                writeref = Hnewref(out_fid);
                DFR8writeref(outfile, writeref);

                if (compress)
                    DFR8setcompress((int32)compress, &cinfo);
                if (DFR8addimage(outfile, (void *)space, xdim, ydim, compress) < 0) {
                    printf("Error writing image to file %s\n", outfile);
                    exit(1);
                }

                /* sequence through the annotations for this image */
                if (copy_ilabel == TRUE || copy_idesc == TRUE) {
                    uint16 image_tag = DFTAG_RIG; /* tag to look for image annotations with */
                    int32  annlen;                /* length of the annotation to copy */

                    if (copy_ilabel == TRUE) {
                        if ((annlen = DFANgetlablen(argv[i], image_tag, prevref)) != FAIL) {
                            if (annbuflen == 0 || annlen > annbuflen) {
                                if (annbuflen != 0)
                                    free(annbuf);
                                if ((annbuf = (char *)malloc(annlen)) == NULL) {
                                    printf("Error allocating buffer for annotation, aborting!\n");
                                    exit(1);
                                } /* end if */
                                annbuflen = annlen;
                            } /* end if */
                            if (DFANgetlabel(argv[i], image_tag, prevref, annbuf, annbuflen) == FAIL)
                                printf("Error reading annotation from file:%s, continuing\n", argv[i]);
                            else if (DFANputlabel(outfile, image_tag, writeref, annbuf) == FAIL)
                                printf("Error writing annotation to file:%s, continuing\n", outfile);
                        } /* end if */
                    }     /* end if */
                    if (copy_idesc == TRUE) {
                        if ((annlen = DFANgetdesclen(argv[i], image_tag, prevref)) != FAIL) {
                            if (annbuflen == 0 || annlen > annbuflen) {
                                if (annbuflen != 0)
                                    free(annbuf);
                                if ((annbuf = (char *)malloc(annlen)) == NULL) {
                                    printf("Error allocating buffer for annotation, aborting!\n");
                                    exit(1);
                                } /* end if */
                                annbuflen = annlen;
                            } /* end if */
                            if (DFANgetdesc(argv[i], image_tag, prevref, annbuf, annbuflen) == FAIL)
                                printf("Error reading annotation from file:%s, continuing\n", argv[i]);
                            else if (DFANputdesc(outfile, image_tag, writeref, annbuf, annlen) == FAIL)
                                printf("Error writing annotation to file:%s, continuing\n", outfile);
                        } /* end if */
                    }     /* end if */
                }         /* end if */

                /* sequence past this image */
                DFR8readref(argv[i], prevref);
                DFR8getdims(argv[i], &xdim, &ydim, &ispal);

                free(space);
            }
        }
    }

    Hclose(out_fid);    /* remember to close the file */
    if (annbuflen != 0) /* and free the buffer space */
        free(annbuf);

    return (0);
}