summaryrefslogtreecommitdiffstats
path: root/hl/tools/gif2h5/gif2mem.c
blob: 01aa5a23cb6030b26dd924a76a360f6bff0f1fb0 (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
333
334
335
336
337
338
339
340
341
342
343
344
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * 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 files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * 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"

#define VERSION                     "1.00"

GIFTOMEM
Gif2Mem(BYTE *MemGif)
{
    /*
     * The gif structure outline for passing data to memory is given in gif.h.
     * These pointers are redunant, 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 */

    GIFTOMEM GifMemoryStruct;

    register WORD i;        /* Loop counter                                 */
    BYTE Identifier;        /* Extension block identifier holder            */
    BYTE Label;             /* Extension block label holder                 */
    BYTE ImageCount;        /* Count of the number of images in the file    */
    BYTE ImageArray;        /* Keep the size of the array to store Images   */
    BYTE CommentCount;
    BYTE CommentArray;
    BYTE ApplicationCount;
    BYTE ApplicationArray;
    BYTE PlainTextCount;
    BYTE PlainTextArray;
    BYTE GCEflag;
    BYTE aTemp;
    BYTE j;
    BYTE w;                 /* Two more variables needed only while testing */
    BYTE *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(-1);
    }

    /*
     * 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 = (BYTE *) &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(-3);
    }
#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 GifMemoryStruct;

            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 = (ImageArray << 1) + 1;
                    if (!(gifImageDesc = (GIFIMAGEDESC **)realloc(gifImageDesc,
                                            sizeof(GIFIMAGEDESC *) * ImageArray))) {
                        printf("Out of memory!");
                        exit(-1);
                    }

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

                    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(-1);
                }
                

                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 = (PlainTextArray << 1) + 1;

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

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

                        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 = (CommentArray << 1) + 1;

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

                        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 = (ImageArray << 1) + 1;

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

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

                            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(-1);
                        }
                        
                        
                        if (ReadGifGraphicControl(gifGraphicControl[ImageCount-1], &MemGif))
                            fprintf(stderr,
                                    "Error reading Graphic Control Extension information\n");
                        
                        if (!*MemGif++ == 0)
                            fprintf(stderr,
                                    "Error reading Graphic Control Extension\n");

                        break;

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

                        if (ApplicationCount > ApplicationArray)
                            ApplicationArray = (ApplicationArray << 1) + 1;
                        
                        if (!(gifApplication = (GIFAPPLICATION **)realloc(gifApplication , sizeof(GIFAPPLICATION *) * ApplicationArray))) {
                            printf("Out of memory!");
                            exit(-1);
                        }
                        
                        if(!(gifApplication[ApplicationCount - 1] = (GIFAPPLICATION *)malloc(sizeof(GIFAPPLICATION)))) {
                            printf("Out of memory!");
                            exit(-1);
                        }

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

                        break;

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

                    break;

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