summaryrefslogtreecommitdiffstats
path: root/hl/tools/gif2h5/gif2hdf.c
blob: 9220655c2553bf6f936c63dfd0b15aac30102903 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "gif.h"
#include "h5tools.h"
#include "h5tools_utils.h"

int
main(int argv, char *argc[])
{
    GIFTOMEM     GifMemoryStruct;
    GIFIMAGEDESC gifImageDesc;

    FILE *fpGif;

    /* replacing int32 with long */
    long i, ImageCount;
    long filesize;

    GIFBYTE *MemGif;
    GIFBYTE *StartPos;

    char *GIFFileName;
    char *HDFFileName;

    /*
     * Initialize all GifMemoryStruct pointers to null to prevent hassles
     * later on
     */
    GifMemoryStruct.GifHeader                  = NULL;
    GifMemoryStruct.GifImageDesc               = NULL;
    GifMemoryStruct.GifGraphicControlExtension = NULL;
    GifMemoryStruct.GifPlainTextExtension      = NULL;
    GifMemoryStruct.GifApplicationExtension    = NULL;
    GifMemoryStruct.GifCommentExtension        = NULL;

    /* Initialize h5tools lib */
    h5tools_init();

    if (argc[1] && (strcmp("-V", argc[1]) == 0)) {
        print_version("gif2h5");
        exit(EXIT_SUCCESS);
    }

    if (argv < 3) {
        printf("Usage: gif2h5 <GIFFILE> <HDFFILE>\n");
        fprintf(stdout, "       gif2h5 -V \n");
        fprintf(stdout, "        Print HDF5 library version and exit\n");
        exit(EXIT_FAILURE);
    }

    GIFFileName = argc[1];
    HDFFileName = argc[2];

    if (!(fpGif = fopen(GIFFileName, "rb"))) {
        printf("Unable to open GIF file for reading.\n");
        exit(EXIT_FAILURE);
    }

    /* Get the whole file into memory. Mem's much faster than I/O */
    fseek(fpGif, 0L, 2);
    filesize = ftell(fpGif);
    fseek(fpGif, 0L, 0);

    if (filesize == 0)
        printf("File Size Zero");

    if (!(MemGif = StartPos = (GIFBYTE *)malloc((size_t)filesize))) {
        printf("Out of memory");
        exit(EXIT_FAILURE);
    }

    if (fread(MemGif, (size_t)filesize, 1, fpGif) != 1) {
        printf("Corrupted Input File");
        exit(EXIT_FAILURE);
    }

    fseek(fpGif, 0L, 0);

    /*
     * Call Gif2Mem and break the whole file into parts.  Gif2Mem also calls
     * decompresses the images so we don't have to worry about that
     */
    Gif2Mem(MemGif, &GifMemoryStruct);

    if (ferror(fpGif)) {
        printf("File Stream Error\n\n");
        exit(EXIT_FAILURE);
    }

    fclose(fpGif);

    /*
     * Call WriteHDF from here. Go ahead and change WriteHDF to write whatever
     * format you want
     */
    if (WriteHDF(GifMemoryStruct, HDFFileName))
        printf("HDF Write Error\n\n");

    /* Free all buffers */
    /* replacing int32 with long */
    ImageCount = (long)((GifMemoryStruct.GifHeader)->ImageCount);

    for (i = 0; i < ImageCount; i++) {
        gifImageDesc = *(GifMemoryStruct.GifImageDesc[i]);

        if (gifImageDesc.Image != NULL)
            free(gifImageDesc.Image);

        if (GifMemoryStruct.GifImageDesc[i] != NULL) {
            free(GifMemoryStruct.GifImageDesc[i]);
            GifMemoryStruct.GifImageDesc[i] = NULL;
        }

        if (GifMemoryStruct.GifGraphicControlExtension[i] != NULL) {
            free(GifMemoryStruct.GifGraphicControlExtension[i]);
            GifMemoryStruct.GifGraphicControlExtension[i] = NULL;
        }
    }

    free(StartPos);
    if (GifMemoryStruct.GifHeader != NULL) {
        free(GifMemoryStruct.GifHeader);
        GifMemoryStruct.GifHeader = NULL;
    }
    if (GifMemoryStruct.GifApplicationExtension != NULL) {
        free(GifMemoryStruct.GifApplicationExtension);
        GifMemoryStruct.GifApplicationExtension = NULL;
    }
    if (GifMemoryStruct.GifImageDesc != NULL) {
        free(GifMemoryStruct.GifImageDesc);
        GifMemoryStruct.GifImageDesc = NULL;
    }
    if (GifMemoryStruct.GifGraphicControlExtension != NULL) {
        free(GifMemoryStruct.GifGraphicControlExtension);
        GifMemoryStruct.GifGraphicControlExtension = NULL;
    }

    return EXIT_SUCCESS;
}