File: r8tohdf.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (194 lines) | stat: -rw-r--r-- 5,851 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  r8tohdf.c
 *  Encoding of raster images in HDF files
 */

/* The intrepretation of arguments has changed a little.  A -p introduces a
   palette which will be used for subsequent images, till another -p.
   -i and -c introduce a series of images/compressed images */

#include <stdlib.h>

#include "hdf.h"

int32 xdim, ydim;

int main(int argc, char *argv[]);
int palconv(char *palfile);
int imconv(char *outfile, char *imfile, uint16 compress);

int
main(int argc, char *argv[])
{
    int    is_pal   = 0;
    int    image    = 1;
    char  *outfile  = NULL;
    uint16 compress = 0;

    if (argc < 5) {
        printf("%s,  version: 1.1   date: July 1, 1992\n", argv[0]);
        printf("   This utility converts one or more raw raster-8 images to\n");
        printf("   HDF RIS8 format and writes them to an HDF file.\n\n");
        printf("Usage:\n");
        printf("  %s xdim ydim outfile [-p palfile] ", argv[0]);
        printf("{[-r],[-c],[-i]} imagefile\n");
        printf("\t\t\t\t... [-p palfile] {[-r],[-c],[-i]} imagefile ...\n");
        printf("  -r: Store without compression (default)\n");
        printf("  -c: Store using RLE compression\n");
        printf("  -i: Store using IMCOMP compression\n\n");
        printf("* r8tohdf can take any number of images and palettes\n");
        printf("* Compression, palette, apply to all subsequent images.\n");
        printf("* All images are assumed to be the same dimensions.\n\n");
        exit(1);
    }

    xdim = atoi(argv[1]);
    ydim = atoi(argv[2]);

    if (xdim < 1 || ydim < 1) {
        printf("Must specify xdim and ydim\n");
        exit(1);
    }

    outfile = argv[3];

    for (int i = 4; i < argc; i++) {
        if (*argv[i] == '-') {
            switch (argv[i][1]) {
                case 'p': /* palette */
                    is_pal = 1;
                    image  = 0;
                    break;
                case 'r': /* raster */
                    image    = 1;
                    compress = (uint16)0;
                    break;
                case 'c': /* RLE */
                    image    = 1;
                    compress = DFTAG_RLE;
                    break;
                case 'i': /* IMCOMP */
                    image    = 1;
                    compress = DFTAG_IMC;
                    break;
                default:
                    printf("Illegal option: %s, skipping....\n", argv[i]);
                    break;
            }
        }
        else { /* file name */
            if (image) {
                if (compress == DFTAG_IMC && is_pal == 0) {
                    printf("Illegal options.  If imcomp compression (-i) ");
                    printf("chosen, you must supply a palette.\n");
                    printf("Program aborted.\n");
                    exit(1);
                }
                imconv(outfile, argv[i], compress);
            }
            else {
                palconv(argv[i]);
                image = 1;
            }
        }
    }
    return 0;
}

/*
 *  palconv(file) sets the palette
 */

int
palconv(char *palfile)
{
    uint8  palspace[1024], reds[256], greens[256], blues[256];
    uint8 *p    = NULL;
    FILE  *fp   = NULL;
    int    ret  = FAIL;
    size_t fret = 0;

    fp = fopen(palfile, "rb");
    if (fp == NULL) {
        printf(" Error opening palette file %s\n", palfile);
        exit(1);
    }

    fret = fread(reds, 1, 256, fp);
    if (fret != 256) {
        printf(" Error reading reds\n");
        exit(1);
    }
    fret = fread(greens, 1, 256, fp);
    if (fret != 256) {
        printf(" Error reading greens\n");
        exit(1);
    }
    fret = fread(blues, 1, 256, fp);
    if (fret != 256) {
        printf(" Error reading blues\n");
        exit(1);
    }
    fclose(fp);

    p = palspace;
    for (int j = 0; j < 256; j++) {
        *p++ = reds[j];
        *p++ = greens[j];
        *p++ = blues[j];
    }

    ret = DFR8setpalette(palspace);
    if (ret < 0) {
        printf(" Error: %d, in writing palette %s\n", ret, palfile);
        exit(1);
    }
    return 0;
}

int
imconv(char *outfile, char *imfile, uint16 compress)
{
    char  *space = NULL;
    FILE  *fp    = NULL;
    size_t fret  = 0;

    if ((fp = fopen(imfile, "rb")) == NULL) {
        printf("Error opening image file\n");
        exit(1);
    }

    if ((space = (char *)calloc((size_t)(xdim * ydim), sizeof(char))) == NULL) {
        printf("Not enough memory to convert image\n");
        exit(1);
    }

    fret = fread(space, sizeof(uint8), (size_t)(xdim * ydim), fp);
    if (fret != (size_t)(xdim * ydim)) {
        printf("Error reading image file\n");
        fclose(fp);
        exit(1);
    }

    if (DFR8addimage(outfile, space, xdim, ydim, compress) < 0) {
        printf(" Error: %d, in writing image %s\n", HEvalue(1), outfile);
        exit(1);
    }

    free(space);
    fclose(fp);
    return 0;
}