-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgif2tga.c
206 lines (193 loc) · 5.42 KB
/
gif2tga.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ngiflib.h"
FILE * fgif;
FILE * ftga;
/* ==================================== MAIN =============================== */
int main(int argc, char * * argv) {
struct ngiflib_gif * gif;
struct ngiflib_img * img;
struct ngiflib_rgb * current_palette = NULL;
int current_palette_size = 0;
int err, i;
char tganame[256];
const char * input_file = NULL;
int indexed = 0;
const char * outbase = NULL;
FILE * log = NULL;
#ifdef NGIFLIB_NO_FILE
long filesize;
u8 * buffer;
#endif /* NGIFLIB_NO_FILE */
if(argc<2) {
printf("Usage: %s [--log logfile] [--indexed|-i] [--outbase path/file] truc.gif\n", argv[0]);
return 1;
}
for(i=1; i < argc; i++) {
if(argv[i][0] == '-') {
if(strcmp(argv[i], "--indexed") == 0
|| strcmp(argv[i], "-i") == 0) {
indexed = 1;
} else if(strcmp(argv[i], "--outbase") == 0) {
if(++i >= argc) {
fprintf(stderr, "option %s need one argument.\n", argv[i - 1]);
return 2;
}
outbase = argv[i];
} else if(strcmp(argv[i], "--log") == 0) {
if(++i >= argc) {
fprintf(stderr, "option %s need one argument.\n", argv[i - 1]);
return 2;
}
log = fopen(argv[i], "w");
if(log == NULL) fprintf(stderr, "failed to open log file %s\n", argv[i]);
/*setvbuf(log, NULL, _IONBF, 0);*/
} else {
fprintf(stderr, "%s: unknown option.\n", argv[i]);
return 2;
}
} else if(input_file == NULL) {
input_file = argv[i];
} else {
fprintf(stderr, "%s: invalid argument.\n", argv[i]);
return 2;
}
}
gif = (struct ngiflib_gif *)ngiflib_malloc(sizeof(struct ngiflib_gif));
#ifdef EXTRA_MALLOC_CHECK
if(gif == NULL) {
printf("Cannot allocate %ld bytes of memory.\n", sizeof(struct ngiflib_gif));
return 4;
}
#endif /* EXTRA_MALLOC_CHECK */
ngiflib_memset(gif, 0, sizeof(struct ngiflib_gif));
#ifndef NGIFLIB_NO_FILE
gif->log = log;
#endif /* NGIFLIB_NO_FILE */
fgif = fopen(input_file, "rb");
if(fgif==NULL) {
printf("Cannot open %s\n", input_file);
return 3;
}
#ifdef NGIFLIB_NO_FILE
fseek(fgif, 0, SEEK_END);
filesize = ftell(fgif);
if (filesize < 0) {
printf("Error reading %s\n", input_file);
return 3;
}
fseek(fgif, 0, SEEK_SET);
buffer = malloc(filesize);
if(buffer == NULL) {
printf("Cannot allocate %ld bytes of memory.\n", filesize);
fclose(fgif);
return 4;
}
fread(buffer, 1, filesize, fgif);
gif->input.buffer.bytes = buffer;
gif->input.buffer.count = (unsigned long)filesize;
gif->mode = NGIFLIB_MODE_FROM_MEM;
#else /* NGIFLIB_NO_FILE */
gif->input.file = fgif;
gif->mode = NGIFLIB_MODE_FROM_FILE;
#endif /* NGIFLIB_NO_FILE */
if(indexed) {
gif->mode |= NGIFLIB_MODE_INDEXED;
printf("INDEXED MODE\n");
} else gif->mode |= NGIFLIB_MODE_TRUE_COLOR;
/* MAIN LOOP */
do {
err = LoadGif(gif); /* en retour 0=fini, 1=une image decodee, -1=ERREUR */
printf("LoadGif() returned %d\n", err);
if(err==1) {
char * p;
img = gif->cur_img;
if (gif->palette != img->palette) {
current_palette = img->palette;
current_palette_size = (1 << img->localpalbits);
} else if(current_palette == NULL) {
current_palette = gif->palette;
current_palette_size = gif->ncolors;
}
if (current_palette == NULL) {
fprintf(stderr, "no palette in GIF\n");
break;
}
if(outbase) {
p = tganame + snprintf(tganame, sizeof(tganame), "%s", outbase);
} else {
snprintf(tganame, sizeof(tganame), "%s", input_file);
p = strrchr(tganame, '.');
if(p == NULL) {
p = tganame + strlen(tganame);
}
}
sprintf(p, "_out%02d.tga", gif->nimg);
ftga = fopen(tganame, "wb");
if(ftga == NULL) {
fprintf(stderr, "cannot open file %s for writing.\n", tganame);
fclose(fgif);
#ifdef NGIFLIB_NO_FILE
free(buffer);
#endif
GifDestroy(gif); /* libere la ram */
return 5;
}
putc(0, ftga); /* 0 */
if(gif->mode & NGIFLIB_MODE_INDEXED) {
putc(1, ftga);/* With palette */
putc(1, ftga);/* With palette */
putc(0, ftga);/* Color Map Origin. */
putc(0, ftga);/* " */
putc(current_palette_size & 255, ftga);/* Color Map Length. */
putc(current_palette_size >> 8, ftga);/* " */
putc(24, ftga);/* Color Map Entry Size. */
for(i=0; i<4; i++) putc(0, ftga);
} else {
putc(0, ftga);
putc(2, ftga);/* Truecolor */
for(i=0; i<9; i++) putc(0, ftga);
}
putc(gif->width & 255, ftga);
putc(gif->width >> 8, ftga);
putc(gif->height & 255, ftga);
putc(gif->height >>8, ftga);
if(gif->mode & NGIFLIB_MODE_INDEXED) {
putc(8, ftga); /* bits per pixel */
putc(32, ftga); /* top down */
for(i = 0; i < current_palette_size; i++) {
putc(current_palette[i].b, ftga);
putc(current_palette[i].g, ftga);
putc(current_palette[i].r, ftga);
}
fwrite(gif->frbuff.p8, 1,
(size_t)gif->width * (size_t)gif->height, ftga);
} else {
long l;
u32 pixel;
putc(32, ftga); /* 16 / bits per pixel */
putc(32+8, ftga); /* top down */
for(l = 0; l < (long)gif->width * (long)gif->height; l++) {
pixel = gif->frbuff.p32[l];
putc(pixel & 0xff, ftga); /* B */
putc((pixel >> 8) & 0xff, ftga); /* G */
putc((pixel >> 16) & 0xff, ftga); /* R */
putc(0xff, ftga); /* A */
}
}
fclose(ftga);
printf("%s written\n",tganame);
}
} while(err==1);
fclose(fgif);
#ifdef NGIFLIB_NO_FILE
free(buffer);
#endif /* NGIFLIB_NO_FILE */
#if DEBUG
fprintf_ngiflib_gif(stdout, gif);
#endif /* DEBUG */
GifDestroy(gif); /* libere la ram */
if(log) fclose(log);
return 0;
}