summaryrefslogtreecommitdiffstats
path: root/hl/tools/gif2h5/gif2mem.c
blob: 47286642f442883bc40817da3fcf5af6b0ac5493 (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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 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://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * This file contains snippets of code from James Murray's original file to
 * display the GIF header information, but most of it has been modified to
 * suit gif2hdf
 */

/****************************************************************************\
**  Title:       GIFHEAD.C                                                  **
**  Purpose:     Display the data in a GIF image file.                      **
**  Version:     1.0                                                        **
**  Date:        March 1992                                                 **
**  Author:      James D. Murray, Anaheim, CA, USA                          **
**  C Compilers: Borland C++ v2.0, Microsoft C v6.00a                       **
**                                                                          **
**  GIFHEAD displays all real information contained within a GIF image      **
**  file, including all color tables and extension block information.       **
**  GIFHEAD reads both GIF 87a abd 89a-format files.                        **
**                                                                          **
**  Copyright (C) 1991-92 by Graphics Software Labs.  All rights reserved.  **
\****************************************************************************/
#include <stdio.h>

#include "gif.h"

int
Gif2Mem(GIFBYTE *MemGif, GIFTOMEM *GifMemoryStruct)
{
    /*
     * The gif structure outline for passing data to memory is given in gif.h.
     * These pointers are redundant, should take them out in ver. 2
     */
    GIFHEAD            *gifHead;           /* GIF Header structure            */
    GIFIMAGEDESC      **gifImageDesc;      /* Logical Image Descriptor struct */
    GIFPLAINTEXT      **gifPlainText;      /* Plain Text Extension structure  */
    GIFAPPLICATION    **gifApplication;    /* Application Extension structure */
    GIFCOMMENT        **gifComment;        /* Comment Extension structure     */
    GIFGRAPHICCONTROL **gifGraphicControl; /* Graphic Control Extension strct */

    GIFWORD  i;          /* Loop counter                                 */
    GIFBYTE  Identifier; /* Extension block identifier holder            */
    GIFBYTE  Label;      /* Extension block label holder                 */
    GIFBYTE  ImageCount; /* Count of the number of images in the file    */
    GIFBYTE  ImageArray; /* Keep the size of the array to store Images   */
    GIFBYTE  CommentCount;
    GIFBYTE  CommentArray;
    GIFBYTE  ApplicationCount;
    GIFBYTE  ApplicationArray;
    GIFBYTE  PlainTextCount;
    GIFBYTE  PlainTextArray;
    GIFBYTE  GCEflag;
    GIFBYTE  aTemp;
    GIFBYTE  j;
    GIFBYTE  w; /* Two more variables needed only while testing */
    GIFBYTE *b; /* Endian Ordering                              */

    /* Allocate memory for the GIF structures           */
    /* Plug the structs into GifMemoryStruct at the end */
    /****************************************************/
    if (!(gifHead = (GIFHEAD *)malloc(sizeof(GIFHEAD)))) {
        printf("Could not allocate memory for gifHead\n");
        exit(EXIT_FAILURE);
    }

    /*
     * The next three have to grow dynamically so we leave them for now and
     * let realloc handle it later on.
     */
    gifImageDesc      = NULL;
    gifPlainText      = NULL;
    gifGraphicControl = NULL;
    gifComment        = NULL;
    gifApplication    = NULL;

    /******************************/
    /* Memory allocation complete */
    /******************************/

    /* Carry out Endian Testing and set Endian Order */
    w           = 0x0001;
    b           = (GIFBYTE *)&w;
    EndianOrder = (b[0] ? 1 : 0);

    /* Read the GIF image file header information */
    ReadGifHeader(gifHead, &MemGif);

    /* Check for FILE stream error */
#if 0
    if (ferror(fpGif))
    {
        fputs("GIFHEAD: Error reading header information!\n", stderr);
        exit(EXIT_FAILURE);
    }
#endif /* 0 */

    /*
     * Identify, read, and display block information.
     */
    ImageCount = ImageArray = 0;
    CommentCount = CommentArray = 0;
    ApplicationCount = ApplicationArray = 0;
    PlainTextCount = PlainTextArray = 0;
    GCEflag                         = 0;

    for (;;) {
        Identifier = *MemGif++;

        switch (Identifier) {
            case 0x3B: /* Trailer */
                /*
                 * The counts are stored to make it easier while putting stuff
                 * into the HDF file and then deallocating space.
                 */
                gifHead->ImageCount       = ImageCount;
                gifHead->CommentCount     = CommentCount;
                gifHead->ApplicationCount = ApplicationCount;
                gifHead->PlainTextCount   = PlainTextCount;

                /* putting stuff into the gif2mem structure */
                GifMemoryStruct->GifHeader                  = gifHead;
                GifMemoryStruct->GifImageDesc               = gifImageDesc;
                GifMemoryStruct->GifPlainTextExtension      = gifPlainText;
                GifMemoryStruct->GifApplicationExtension    = gifApplication;
                GifMemoryStruct->GifCommentExtension        = gifComment;
                GifMemoryStruct->GifGraphicControlExtension = gifGraphicControl;

                /* return the struct */
                return 0;

            case 0x2C: /* Image Descriptor */
                /*
                 * If there was no image descriptor before this increase image
                 * count. If an imagedescriptor was present, reset GCEflag
                 */
                if (GCEflag == 0)
                    ImageCount++;
                else
                    GCEflag = 0;

                if (ImageCount > ImageArray) {
                    aTemp      = ImageArray;
                    ImageArray = (GIFBYTE)((ImageArray << 1) + 1);
                    if (!(gifImageDesc =
                              (GIFIMAGEDESC **)realloc(gifImageDesc, sizeof(GIFIMAGEDESC *) * ImageArray))) {
                        printf("Out of memory!");
                        exit(EXIT_FAILURE);
                    }

                    if (!(gifGraphicControl = (GIFGRAPHICCONTROL **)realloc(
                              gifGraphicControl, sizeof(GIFGRAPHICCONTROL *) * ImageArray))) {
                        printf("Out of memory!");
                        exit(EXIT_FAILURE);
                    }

                    for (j = aTemp; j < ImageArray; j++) {
                        gifGraphicControl[j] = NULL;
                        gifImageDesc[j]      = NULL;
                    }
                }

                if (!(gifImageDesc[ImageCount - 1] = (GIFIMAGEDESC *)malloc(sizeof(GIFIMAGEDESC)))) {
                    printf("Out of memory!");
                    exit(EXIT_FAILURE);
                }

                if (ReadGifImageDesc(gifImageDesc[ImageCount - 1], &MemGif) == -1)
                    fputs("Error reading Image Descriptor information\n", stderr);

                /* Decompress the Image */
                gifImageDesc[ImageCount - 1]->Image = Decompress(gifImageDesc[ImageCount - 1], gifHead);
                free(gifImageDesc[ImageCount - 1]->GIFImage);

                /*
                 * Convert the local palette into an HDF compatible palette In
                 * case the local color table is present, it is written out as
                 * the HDFPalette If it is absent the global table is written
                 * as the HDFPalette.
                 */
                if (!((gifImageDesc[ImageCount - 1]->PackedField) & 0x80)) {
                    /* Check to see if the global color table exists.... */
                    if (gifHead->PackedField & 0x80) {
                        for (i = 0; i < gifHead->TableSize; i++) {
                            gifImageDesc[ImageCount - 1]->HDFPalette[i][0] = gifHead->HDFPalette[i][0];
                            gifImageDesc[ImageCount - 1]->HDFPalette[i][1] = gifHead->HDFPalette[i][1];
                            gifImageDesc[ImageCount - 1]->HDFPalette[i][2] = gifHead->HDFPalette[i][2];
                        }
                    }

                    gifImageDesc[ImageCount - 1]->TableSize = gifHead->TableSize;
                }

                break;

            case 0x21: /* Extension Block */
                Label = *MemGif++;

                switch (Label) {
                    case 0x01: /* Plain Text Extension */
                        puts("Plain Text Extension\n");
                        PlainTextCount++;

                        if (PlainTextCount > PlainTextArray)
                            PlainTextArray = (GIFBYTE)((PlainTextArray << 1) + 1);

                        if (!(gifPlainText = (GIFPLAINTEXT **)realloc(gifPlainText, sizeof(GIFPLAINTEXT *) *
                                                                                        PlainTextArray))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (!(gifPlainText[PlainTextCount - 1] =
                                  (GIFPLAINTEXT *)malloc(sizeof(GIFPLAINTEXT)))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (ReadGifPlainText(gifPlainText[PlainTextCount - 1], &MemGif))
                            fprintf(stderr, "Error reading Plain Text Extension information.\n");

                        break;

                    case 0xFE: /* Comment Extension */
                        CommentCount++;

                        if (CommentCount > CommentArray)
                            CommentArray = (GIFBYTE)((CommentArray << 1) + 1);

                        if (!(gifComment =
                                  (GIFCOMMENT **)realloc(gifComment, sizeof(GIFCOMMENT *) * CommentArray))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (!(gifComment[CommentCount - 1] = (GIFCOMMENT *)malloc(sizeof(GIFCOMMENT)))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (ReadGifComment(gifComment[CommentCount - 1], &MemGif))
                            fprintf(stderr, "Error reading Comment Extension information\n");

                        break;

                    case 0xF9: /* Graphic Control Extension */
                        if (GCEflag == 0)
                            ImageCount++;

                        GCEflag = 1;

                        if (ImageCount > ImageArray) {
                            aTemp      = ImageArray;
                            ImageArray = (GIFBYTE)((ImageArray << 1) + 1);

                            if (!(gifGraphicControl = (GIFGRAPHICCONTROL **)realloc(
                                      gifGraphicControl, sizeof(GIFGRAPHICCONTROL *) * ImageArray))) {
                                printf("Out of memory!");
                                exit(EXIT_FAILURE);
                            }

                            if (!(gifImageDesc = (GIFIMAGEDESC **)realloc(
                                      gifImageDesc, sizeof(GIFIMAGEDESC *) * ImageArray))) {
                                printf("Out of memory!");
                                exit(EXIT_FAILURE);
                            }

                            for (j = aTemp; j < ImageArray; j++) {
                                gifGraphicControl[j] = NULL;
                                gifImageDesc[j]      = NULL;
                            }
                        }

                        if (!(gifGraphicControl[ImageCount - 1] =
                                  (GIFGRAPHICCONTROL *)malloc(sizeof(GIFGRAPHICCONTROL)))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (ReadGifGraphicControl(gifGraphicControl[ImageCount - 1], &MemGif))
                            fprintf(stderr, "Error reading Graphic Control Extension information\n");

                        (*MemGif)++;
                        if ((!*MemGif) == 0)
                            fprintf(stderr, "Error reading Graphic Control Extension\n");

                        break;

                    case 0xFF: /* Application Extension */
                        ApplicationCount++;

                        if (ApplicationCount > ApplicationArray)
                            ApplicationArray = (GIFBYTE)((ApplicationArray << 1) + 1);

                        if (!(gifApplication = (GIFAPPLICATION **)realloc(
                                  gifApplication, sizeof(GIFAPPLICATION *) * ApplicationArray))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (!(gifApplication[ApplicationCount - 1] =
                                  (GIFAPPLICATION *)malloc(sizeof(GIFAPPLICATION)))) {
                            printf("Out of memory!");
                            exit(EXIT_FAILURE);
                        }

                        if (ReadGifApplication(gifApplication[ApplicationCount - 1], &MemGif))
                            fprintf(stderr, "Error reading Application Extension information\n");

                        break;

                    default:
                        printf("Unknown Extension Label: %#02x\n", Label);
                        break;
                }

                break;

            default:
                fprintf(stderr, "Unknown Block Separator Character: %#02x\n", Identifier);
        }
    }
}