[go: up one dir, main page]

Menu

[efba94]: / util / gifflip.c  Maximize  Restore  History

Download this file

333 lines (297 with data), 11.2 kB

  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
/*****************************************************************************
* "Gif-Lib" - Yet another gif library.
*
* Written by: Gershon Elber Ver 0.1, Jul. 1989
******************************************************************************
* Program to rotate image 90 degrees to the right/left or flip the image
* horizontally/vertically (mirror).
* Options:
* -v : verbose mode.
* -r : rotate 90 degrees to the right (default).
* -l : rotate 90 degrees to the left.
* -x : Mirror the image horizontally (first line switch places with last).
* -y : Mirror the image vertically (first column switch places with last).
* -h : on-line help
******************************************************************************
* History:
* 10 Jul 89 - Version 1.0 by Gershon Elber.
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include "gif_lib.h"
#include "getarg.h"
#define PROGRAM_NAME "gifflip"
#define FLIP_NONE 0
#define FLIP_RIGHT 1
#define FLIP_LEFT 2
#define FLIP_HORIZ 3
#define FLIP_VERT 4
static char
*VersionStr =
PROGRAM_NAME
VERSION_COOKIE
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr =
PROGRAM_NAME
" v%- r%- l%- x%- y%- h%- GifFile!*s";
/* Make some variables global, so we could access them faster: */
static int
ImageNum = 0;
static int LoadImage(GifFileType *GifFile, GifRowType **ImageBuffer);
static int DumpImage(GifFileType *GifFile, GifRowType *ImageBuffer,
int Width, int Height, int FlipDirection);
static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
/******************************************************************************
* Interpret the command line and scan the given GIF file.
******************************************************************************/
int main(int argc, char **argv)
{
int i, NumFiles, ExtCode, FlipDirection = FLIP_RIGHT;
bool Error,
RightFlag = false, LeftFlag = false,
HorizFlag = false, VertFlag = false, HelpFlag = false;
GifRecordType RecordType;
GifByteType *Extension;
char **FileName = NULL;
GifRowType *ImageBuffer;
GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint,
&RightFlag, &LeftFlag, &HorizFlag, &VertFlag, &HelpFlag,
&NumFiles, &FileName)) != false ||
(NumFiles > 1 && !HelpFlag)) {
if (Error)
GAPrintErrMsg(Error);
else if (NumFiles > 1)
GIF_MESSAGE("Error in command line parsing - one GIF file please.");
GAPrintHowTo(CtrlStr);
exit(EXIT_FAILURE);
}
if (HelpFlag) {
(void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
GAPrintHowTo(CtrlStr);
exit(EXIT_SUCCESS);
}
if ((i = (RightFlag != 0) + (LeftFlag != 0) +
(HorizFlag != 0) + (VertFlag != 0)) > 1)
GIF_EXIT("Only one of -r, -l, -x, -y please.");
if (i == 0)
FlipDirection = FLIP_RIGHT; /* Make it the default. */
else {
if (RightFlag) FlipDirection = FLIP_RIGHT;
if (LeftFlag) FlipDirection = FLIP_LEFT;
if (HorizFlag) FlipDirection = FLIP_HORIZ;
if (VertFlag) FlipDirection = FLIP_VERT;
}
if (NumFiles == 1) {
if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
QuitGifError(GifFileIn, GifFileOut);
}
else {
/* Use the stdin instead: */
if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
QuitGifError(GifFileIn, GifFileOut);
}
/* Open stdout for the output file: */
if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
QuitGifError(GifFileIn, GifFileOut);
if (RightFlag || LeftFlag) {
/* Dump out same screen information, but flip Screen Width/Height: */
if (EGifPutScreenDesc(GifFileOut,
GifFileIn->SHeight, GifFileIn->SWidth,
GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
GifFileIn->SColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
else {
/* Dump out exactly same screen information: */
if (EGifPutScreenDesc(GifFileOut,
GifFileIn->SWidth, GifFileIn->SHeight,
GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
GifFileIn->SColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
/* Scan the content of the GIF file and load the image(s) in: */
do {
if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
switch (RecordType) {
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (GifFileIn->Image.Interlace)
GIF_EXIT("Cannt flip interlaced images - use GifInter first.");
/* Put the image descriptor to out file: */
if (RightFlag) {
/* Rotate to the right: */
if (EGifPutImageDesc(GifFileOut,
GifFileIn->SHeight - GifFileIn->Image.Height -
GifFileIn->Image.Top,
GifFileIn->Image.Left,
GifFileIn->Image.Height, GifFileIn->Image.Width,
false, GifFileIn->Image.ColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
else if (LeftFlag) {
/* Rotate to the left: */
if (EGifPutImageDesc(GifFileOut,
GifFileIn->Image.Top,
GifFileIn->SWidth - GifFileIn->Image.Width -
GifFileIn->Image.Left,
GifFileIn->Image.Height, GifFileIn->Image.Width,
false, GifFileIn->Image.ColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
else {
/* No rotation - only flipping vert. or horiz.: */
if (EGifPutImageDesc(GifFileOut,
GifFileIn->Image.Left, GifFileIn->Image.Top,
GifFileIn->Image.Width, GifFileIn->Image.Height,
false, GifFileIn->Image.ColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
/* Load the image (either Interlaced or not), and dump it */
/* fliped as requrested by Flags: */
if (LoadImage(GifFileIn, &ImageBuffer) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (DumpImage(GifFileOut, ImageBuffer, GifFileIn->Image.Width,
GifFileIn->Image.Height, FlipDirection) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
break;
case EXTENSION_RECORD_TYPE:
/* Skip any extension blocks in file: */
if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
/* No support to more than one extension blocks, so discard: */
while (Extension != NULL) {
if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
break;
case TERMINATE_RECORD_TYPE:
break;
default: /* Should be traps by DGifGetRecordType. */
break;
}
}
while (RecordType != TERMINATE_RECORD_TYPE);
if (DGifCloseFile(GifFileIn) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (EGifCloseFile(GifFileOut) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
return 0;
}
/******************************************************************************
* Routine to read Image out. The image can be Non interlaced only.
* The memory required to hold the image is allocate by the routine itself.
* Return GIF_OK if succesful, GIF_ERROR otherwise.
******************************************************************************/
static int LoadImage(GifFileType *GifFile, GifRowType **ImageBufferPtr)
{
int Size, i;
GifRowType *ImageBuffer;
/* Allocate the image as vector of column of rows */
if ((ImageBuffer = (GifRowType *)
malloc(GifFile->Image.Height * sizeof(GifRowType))) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
Size = GifFile->Image.Width * sizeof(GifPixelType);/* One row size in bytes.*/
for (i = 0; i < GifFile->Image.Height; i++) {
/* Allocate the rows: */
if ((ImageBuffer[i] = (GifRowType) malloc(Size)) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
}
*ImageBufferPtr = ImageBuffer;
GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ",
PROGRAM_NAME, ++ImageNum, GifFile->Image.Left, GifFile->Image.Top,
GifFile->Image.Width, GifFile->Image.Height);
for (i = 0; i < GifFile->Image.Height; i++) {
GifQprintf("\b\b\b\b%-4d", i);
if (DGifGetLine(GifFile, ImageBuffer[i], GifFile->Image.Width)
== GIF_ERROR) return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* Routine to dump image out. The given Image buffer should always hold the
* image sequencially, and Width & Height hold image dimensions BEFORE flip.
* Image will be dumped according to FlipDirection.
* Once dumped, the memory holding the image is freed.
* Return GIF_OK if succesful, GIF_ERROR otherwise.
******************************************************************************/
static int DumpImage(GifFileType *GifFile, GifRowType *ImageBuffer,
int Width, int Height, int FlipDirection)
{
int i, j, Count;
GifRowType Line; /* New scan line is copied to it. */
/* Allocate scan line that will fit both image width and height: */
if ((Line = (GifRowType) malloc((Width > Height ? Width : Height)
* sizeof(GifPixelType))) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
switch (FlipDirection) {
case FLIP_RIGHT:
for (Count = Width, i = 0; i < Width; i++) {
GifQprintf("\b\b\b\b%-4d", Count--);
for (j = 0; j < Height; j++)
Line[j] = ImageBuffer[Height - j - 1][i];
if (EGifPutLine(GifFile, Line, Height) == GIF_ERROR) {
free(Line);
return GIF_ERROR;
}
}
break;
case FLIP_LEFT:
for (i = Width - 1; i >= 0; i--) {
GifQprintf("\b\b\b\b%-4d", i + 1);
for (j = 0; j < Height; j++)
Line[j] = ImageBuffer[j][i];
if (EGifPutLine(GifFile, Line, Height) == GIF_ERROR) {
free(Line);
return GIF_ERROR;
}
}
break;
case FLIP_HORIZ:
for (i = Height - 1; i >= 0; i--) {
GifQprintf("\b\b\b\b%-4d", i);
if (EGifPutLine(GifFile, ImageBuffer[i], Width) == GIF_ERROR) {
free(Line);
return GIF_ERROR;
}
}
break;
case FLIP_VERT:
for (Count = Height, i = 0; i < Height; i++) {
GifQprintf("\b\b\b\b%-4d", Count--);
for (j = 0; j < Width; j++)
Line[j] = ImageBuffer[i][Width - j - 1];
if (EGifPutLine(GifFile, Line, Width) == GIF_ERROR) {
free(Line);
return GIF_ERROR;
}
}
break;
}
/* Free the memory used for this image, and the temporary scan line: */
for (i = 0; i < Height; i++) free((char *) ImageBuffer[i]);
free((char *) ImageBuffer);
free((char *) Line);
return GIF_OK;
}
/******************************************************************************
* Close both input and output file (if open), and exit.
******************************************************************************/
static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
{
PrintGifError();
if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
exit(EXIT_FAILURE);
}