-
Notifications
You must be signed in to change notification settings - Fork 13
/
gifsicle.c
2192 lines (1874 loc) · 61.9 KB
/
gifsicle.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c-basic-offset: 2 -*- */
/* gifsicle.c - gifsicle's main loop.
Copyright (C) 1997-2021 Eddie Kohler, ekohler@gmail.com
This file is part of gifsicle.
Gifsicle is free software. It is distributed under the GNU Public License,
version 2; you can copy, distribute, or alter it at will, as long
as this notice is kept intact and this source code is made available. There
is no warranty, express or implied. */
#include <config.h>
#include "gifsicle.h"
#include "kcolor.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef static_assert
#define static_assert(x, msg) switch ((int) (x)) case 0: case !!((int) (x)):
#endif
Gt_Frame def_frame;
Gt_Frameset *frames = 0;
int first_input_frame = 0;
Gt_Frameset *nested_frames = 0;
Gif_Stream *input = 0;
const char *input_name = 0;
static int unoptimizing = 0;
const int GIFSICLE_DEFAULT_THREAD_COUNT = 8;
int thread_count = 0;
static int gif_read_flags = 0;
static int nextfile = 0;
Gif_CompressInfo gif_write_info;
static int frames_done = 0;
static int files_given = 0;
int warn_local_colormaps = 1;
static Gt_ColorTransform *input_transforms;
static Gt_ColorTransform *output_transforms;
int mode = BLANK_MODE;
int nested_mode = 0;
static int infoing = 0;
int verbosing = 0;
static int no_ignore_errors = 0;
#define CHANGED(next, flag) (((next) & (1<<(flag))) != 0)
#define UNCHECKED_MARK_CH(where, what) \
next_##where |= 1<<what;
#define MARK_CH(where, what) do { \
if (CHANGED(next_##where, what)) \
redundant_option_warning(where##_option_types[what]); \
UNCHECKED_MARK_CH(where, what); \
} while (0)
/* frame option types */
static int next_frame = 0;
#define CH_INTERLACE 0
#define CH_DISPOSAL 1
#define CH_DELAY 2
#define CH_TRANSPARENT 3
#define CH_COMMENT 4
#define CH_NAME 5
#define CH_POSITION 6
#define CH_CROP 7
#define CH_EXTENSION 8
#define CH_FLIP 9
#define CH_ROTATE 10
static const char *frame_option_types[] = {
"interlace", "disposal", "delay", "transparency",
"comment", "name", "position", "crop",
"extension", "flip", "rotation"
};
/* input option types */
static int next_input = 0;
#define CH_UNOPTIMIZE 0
#define CH_CHANGE_COLOR 1
static const char *input_option_types[] = {
"unoptimization", "color change"
};
/* output option types */
static Gt_OutputData def_output_data;
Gt_OutputData active_output_data;
static int next_output = 0;
static int active_next_output = 0;
static int any_output_successful = 0;
#define CH_LOOPCOUNT 0
#define CH_LOGICAL_SCREEN 1
#define CH_OPTIMIZE 2
#define CH_OUTPUT 3
#define CH_COLORMAP 4
#define CH_DITHER 5
#define CH_USE_COLORMAP 6
#define CH_COLORMAP_METHOD 7
#define CH_BACKGROUND 8
#define CH_COLOR_TRANSFORM 9
#define CH_RESIZE 10
#define CH_MEMORY 11
#define CH_GAMMA 12
#define CH_RESIZE_METHOD 13
#define CH_SCALE_COLORS 14
static const char *output_option_types[] = {
"loopcount", "logical screen", "optimization", "output file",
"colormap size", "dither", "colormap", "colormap method",
"background", "color transformation", "resize", "memory conservation",
"gamma", "resize method", "resize colors"
};
#define SAME_INTERLACE_OPT 300
#define INFO_OPT 301
#define DISPOSAL_OPT 302
#define SAME_LOOPCOUNT_OPT 303
#define SAME_DISPOSAL_OPT 304
#define SAME_DELAY_OPT 305
#define SAME_TRANSPARENT_OPT 308
#define LOGICAL_SCREEN_OPT 309
#define COMMENT_OPT 310
#define UNOPTIMIZE_OPT 311
#define CAREFUL_OPT 312
#define OPTIMIZE_OPT 313
#define SAME_LOGICAL_SCREEN_OPT 314
#define DELETE_OPT 315
#define REPLACE_OPT 316
#define INSERT_OPT 317
#define ALTER_DONE_OPT 318
#define APPEND_OPT 319
#define COLOR_INFO_OPT 320
#define VERBOSE_OPT 321
#define NO_COMMENTS_OPT 322
#define SAME_COMMENTS_OPT 323
#define NAME_OPT 324
#define SAME_NAME_OPT 325
#define NO_NAME_OPT 326
#define POSITION_OPT 327
#define SAME_POSITION_OPT 328
#define VERSION_OPT 329
#define HELP_OPT 330
#define OUTPUT_OPT 331
#define CROP_OPT 332
#define SAME_CROP_OPT 333
#define CHANGE_COLOR_OPT 334
#define COLORMAP_OPT 335
#define COLORMAP_ALGORITHM_OPT 336
#define DITHER_OPT 337
#define USE_COLORMAP_OPT 338
#define NO_EXTENSIONS_OPT 339
#define SAME_EXTENSIONS_OPT 340
#define EXTENSION_INFO_OPT 341
#define BACKGROUND_OPT 342
#define SAME_BACKGROUND_OPT 343
#define FLIP_HORIZ_OPT 344
#define FLIP_VERT_OPT 345
#define NO_FLIP_OPT 346
#define ROTATE_90_OPT 347
#define ROTATE_180_OPT 348
#define ROTATE_270_OPT 349
#define NO_ROTATE_OPT 350
#define APP_EXTENSION_OPT 351
#define EXTENSION_OPT 352
#define COLOR_TRANSFORM_OPT 353
#define RESIZE_OPT 354
#define SCALE_OPT 355
#define NO_WARNINGS_OPT 356
#define WARNINGS_OPT 357
#define RESIZE_WIDTH_OPT 358
#define RESIZE_HEIGHT_OPT 359
#define CROP_TRANSPARENCY_OPT 360
#define CONSERVE_MEMORY_OPT 361
#define MULTIFILE_OPT 362
#define NEXTFILE_OPT 363
#define RESIZE_FIT_OPT 364
#define RESIZE_FIT_WIDTH_OPT 365
#define RESIZE_FIT_HEIGHT_OPT 366
#define SIZE_INFO_OPT 367
#define GAMMA_OPT 368
#define GRAY_OPT 369
#define RESIZE_METHOD_OPT 370
#define RESIZE_COLORS_OPT 371
#define NO_APP_EXTENSIONS_OPT 372
#define SAME_APP_EXTENSIONS_OPT 373
#define IGNORE_ERRORS_OPT 374
#define THREADS_OPT 375
#define RESIZE_GEOMETRY_OPT 376
#define RESIZE_TOUCH_OPT 377
#define RESIZE_TOUCH_WIDTH_OPT 378
#define RESIZE_TOUCH_HEIGHT_OPT 379
#define LOSSY_OPT 380
#define LOOP_TYPE (Clp_ValFirstUser)
#define DISPOSAL_TYPE (Clp_ValFirstUser + 1)
#define DIMENSIONS_TYPE (Clp_ValFirstUser + 2)
#define FRAME_SPEC_TYPE (Clp_ValFirstUser + 3)
#define COLOR_TYPE (Clp_ValFirstUser + 4)
#define POSITION_TYPE (Clp_ValFirstUser + 5)
#define RECTANGLE_TYPE (Clp_ValFirstUser + 6)
#define TWO_COLORS_TYPE (Clp_ValFirstUser + 7)
#define COLORMAP_ALG_TYPE (Clp_ValFirstUser + 8)
#define SCALE_FACTOR_TYPE (Clp_ValFirstUser + 9)
#define OPTIMIZE_TYPE (Clp_ValFirstUser + 10)
#define RESIZE_METHOD_TYPE (Clp_ValFirstUser + 11)
const Clp_Option options[] = {
{ "append", 0, APPEND_OPT, 0, 0 },
{ "app-extension", 'x', APP_EXTENSION_OPT, Clp_ValString, 0 },
{ "no-app-extensions", 0, NO_APP_EXTENSIONS_OPT, 0, 0 },
{ "background", 'B', BACKGROUND_OPT, COLOR_TYPE, Clp_Negate },
{ "batch", 'b', 'b', 0, 0 },
{ "bg", 0, BACKGROUND_OPT, COLOR_TYPE, Clp_Negate },
{ "careful", 0, CAREFUL_OPT, 0, Clp_Negate },
{ "change-color", 0, CHANGE_COLOR_OPT, TWO_COLORS_TYPE, Clp_Negate },
{ "cinfo", 0, COLOR_INFO_OPT, 0, Clp_Negate },
{ "clip", 0, CROP_OPT, RECTANGLE_TYPE, Clp_Negate },
{ "colors", 'k', COLORMAP_OPT, Clp_ValInt, Clp_Negate },
{ "color-method", 0, COLORMAP_ALGORITHM_OPT, COLORMAP_ALG_TYPE, 0 },
{ "color-info", 0, COLOR_INFO_OPT, 0, Clp_Negate },
{ "comment", 'c', COMMENT_OPT, Clp_ValString, 0 },
{ "no-comments", 'c', NO_COMMENTS_OPT, 0, 0 },
{ "conserve-memory", 0, CONSERVE_MEMORY_OPT, 0, Clp_Negate },
{ "crop", 0, CROP_OPT, RECTANGLE_TYPE, Clp_Negate },
{ "crop-transparency", 0, CROP_TRANSPARENCY_OPT, 0, Clp_Negate },
{ "delay", 'd', 'd', Clp_ValInt, Clp_Negate },
{ "delete", 0, DELETE_OPT, 0, 0 },
{ "disposal", 'D', DISPOSAL_OPT, DISPOSAL_TYPE, Clp_Negate },
{ 0, 'f', DITHER_OPT, 0, Clp_Negate },
{ "dither", 0, DITHER_OPT, Clp_ValString, Clp_Negate | Clp_Optional },
{ "done", 0, ALTER_DONE_OPT, 0, 0 },
{ "explode", 'e', 'e', 0, 0 },
{ "explode-by-name", 'E', 'E', 0, 0 },
{ "extension", 0, EXTENSION_OPT, Clp_ValString, 0 },
{ "no-extension", 0, NO_EXTENSIONS_OPT, 0, 0 },
{ "no-extensions", 'x', NO_EXTENSIONS_OPT, 0, 0 },
{ "extension-info", 0, EXTENSION_INFO_OPT, 0, Clp_Negate },
{ "flip-horizontal", 0, FLIP_HORIZ_OPT, 0, Clp_Negate },
{ "flip-vertical", 0, FLIP_VERT_OPT, 0, Clp_Negate },
{ "no-flip", 0, NO_FLIP_OPT, 0, 0 },
{ "gamma", 0, GAMMA_OPT, Clp_ValString, Clp_Negate },
{ "gray", 0, GRAY_OPT, 0, 0 },
{ "help", 'h', HELP_OPT, 0, 0 },
{ "ignore-errors", 0, IGNORE_ERRORS_OPT, 0, Clp_Negate },
{ "info", 'I', INFO_OPT, 0, Clp_Negate },
{ "insert-before", 0, INSERT_OPT, FRAME_SPEC_TYPE, 0 },
{ "interlace", 'i', 'i', 0, Clp_Negate },
{ "logical-screen", 'S', LOGICAL_SCREEN_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "loopcount", 'l', 'l', LOOP_TYPE, Clp_Optional | Clp_Negate },
{ "lossy", 0, LOSSY_OPT, Clp_ValInt, Clp_Optional },
{ "merge", 'm', 'm', 0, 0 },
{ "method", 0, COLORMAP_ALGORITHM_OPT, COLORMAP_ALG_TYPE, 0 },
{ "multifile", 0, MULTIFILE_OPT, 0, Clp_Negate },
{ "name", 'n', NAME_OPT, Clp_ValString, 0 },
{ "nextfile", 0, NEXTFILE_OPT, 0, Clp_Negate },
{ "no-names", 'n', NO_NAME_OPT, 0, 0 },
{ "optimize", 'O', OPTIMIZE_OPT, OPTIMIZE_TYPE, Clp_Negate | Clp_Optional },
{ "output", 'o', OUTPUT_OPT, Clp_ValStringNotOption, 0 },
{ "position", 'p', POSITION_OPT, POSITION_TYPE, Clp_Negate },
{ "replace", 0, REPLACE_OPT, FRAME_SPEC_TYPE, 0 },
{ "resize", 0, RESIZE_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-width", 0, RESIZE_WIDTH_OPT, Clp_ValUnsigned, Clp_Negate },
{ "resize-height", 0, RESIZE_HEIGHT_OPT, Clp_ValUnsigned, Clp_Negate },
{ "resiz", 0, RESIZE_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resi", 0, RESIZE_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "res", 0, RESIZE_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-fit", 0, RESIZE_FIT_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-fit-width", 0, RESIZE_FIT_WIDTH_OPT, Clp_ValUnsigned, Clp_Negate },
{ "resize-fit-height", 0, RESIZE_FIT_HEIGHT_OPT, Clp_ValUnsigned, Clp_Negate },
{ "resize-fi", 0, RESIZE_FIT_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-f", 0, RESIZE_FIT_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-touch", 0, RESIZE_TOUCH_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-touch-width", 0, RESIZE_TOUCH_WIDTH_OPT, Clp_ValUnsigned, Clp_Negate },
{ "resize-touch-height", 0, RESIZE_TOUCH_HEIGHT_OPT, Clp_ValUnsigned, Clp_Negate },
{ "resize-touc", 0, RESIZE_TOUCH_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-tou", 0, RESIZE_TOUCH_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-to", 0, RESIZE_TOUCH_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-t", 0, RESIZE_TOUCH_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "resize-geometry", 0, RESIZE_GEOMETRY_OPT, Clp_ValString, Clp_Negate },
{ "resize-method", 0, RESIZE_METHOD_OPT, RESIZE_METHOD_TYPE, 0 },
{ "resize-colors", 0, RESIZE_COLORS_OPT, Clp_ValInt, Clp_Negate },
{ "rotate-90", 0, ROTATE_90_OPT, 0, 0 },
{ "rotate-180", 0, ROTATE_180_OPT, 0, 0 },
{ "rotate-270", 0, ROTATE_270_OPT, 0, 0 },
{ "no-rotate", 0, NO_ROTATE_OPT, 0, 0 },
{ "same-app-extensions", 0, SAME_APP_EXTENSIONS_OPT, 0, 0 },
{ "same-background", 0, SAME_BACKGROUND_OPT, 0, 0 },
{ "same-bg", 0, SAME_BACKGROUND_OPT, 0, 0 },
{ "same-clip", 0, SAME_CROP_OPT, 0, 0 },
{ "same-comments", 0, SAME_COMMENTS_OPT, 0, 0 },
{ "same-crop", 0, SAME_CROP_OPT, 0, 0 },
{ "same-extensions", 0, SAME_EXTENSIONS_OPT, 0, 0 },
{ "same-interlace", 0, SAME_INTERLACE_OPT, 0, 0 },
{ "same-logical-screen", 0, SAME_LOGICAL_SCREEN_OPT, 0, 0 },
{ "same-loopcount", 0, SAME_LOOPCOUNT_OPT, 0, 0 },
{ "same-disposal", 0, SAME_DISPOSAL_OPT, 0, 0 },
{ "same-delay", 0, SAME_DELAY_OPT, 0, 0 },
{ "same-names", 0, SAME_NAME_OPT, 0, 0 },
{ "same-position", 0, SAME_POSITION_OPT, 0, 0 },
{ "same-screen", 0, SAME_LOGICAL_SCREEN_OPT, 0, 0 },
{ "same-transparent", 0, SAME_TRANSPARENT_OPT, 0, 0 },
{ "scale", 0, SCALE_OPT, SCALE_FACTOR_TYPE, Clp_Negate },
{ "scale-method", 0, RESIZE_METHOD_OPT, RESIZE_METHOD_TYPE, 0 },
{ "scale-colors", 0, RESIZE_COLORS_OPT, Clp_ValInt, Clp_Negate },
{ "screen", 0, LOGICAL_SCREEN_OPT, DIMENSIONS_TYPE, Clp_Negate },
{ "sinfo", 0, SIZE_INFO_OPT, 0, Clp_Negate },
{ "size-info", 0, SIZE_INFO_OPT, 0, Clp_Negate },
{ "transform-colormap", 0, COLOR_TRANSFORM_OPT, Clp_ValStringNotOption,
Clp_Negate },
{ "transparent", 't', 't', COLOR_TYPE, Clp_Negate },
{ "unoptimize", 'U', UNOPTIMIZE_OPT, 0, Clp_Negate },
{ "use-colormap", 0, USE_COLORMAP_OPT, Clp_ValString, Clp_Negate },
{ "verbose", 'V', VERBOSE_OPT, 0, Clp_Negate },
{ 0, 'v', VERBOSE_OPT, 0, Clp_Negate },
{ "version", 0, VERSION_OPT, 0, 0 },
{ 0, 'w', NO_WARNINGS_OPT, 0, Clp_Negate },
{ "warnings", 0, WARNINGS_OPT, 0, Clp_Negate },
{ "xinfo", 0, EXTENSION_INFO_OPT, 0, Clp_Negate },
{ "threads", 'j', THREADS_OPT, Clp_ValUnsigned, Clp_Optional | Clp_Negate },
};
Clp_Parser* clp;
static void combine_output_options(void);
static void initialize_def_frame(void);
static void redundant_option_warning(const char *);
#if 0
void resize_check(void)
{
int nw, nh;
nw = nh = 256;
resize_dimensions(&nw, &nh, 640, 480, 0);
assert(nw == 640 && nh == 480);
nw = nh = 256;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT);
assert(nw == 480 && nh == 480);
nw = nh = 512;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT);
assert(nw == 480 && nh == 480);
nw = nh = 256;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT | GT_RESIZE_FIT_DOWN);
assert(nw == 256 && nh == 256);
nw = nh = 512;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT | GT_RESIZE_FIT_DOWN);
assert(nw == 480 && nh == 480);
nw = nh = 256;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT | GT_RESIZE_FIT_UP);
assert(nw == 480 && nh == 480);
nw = nh = 512;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT | GT_RESIZE_FIT_UP);
assert(nw == 512 && nh == 512);
nw = nh = 256;
resize_dimensions(&nw, &nh, 640, 480, GT_RESIZE_FIT | GT_RESIZE_FIT_UP | GT_RESIZE_MIN_DIMEN);
assert(nw == 640 && nh == 640);
fprintf(stderr, "got\n");
}
#endif
static void
set_mode(int newmode)
{
if (mode == BLANK_MODE) {
if (newmode != BLANK_MODE)
mode = newmode;
else if (infoing == 1)
mode = INFOING;
else
mode = MERGING;
}
if (mode != INFOING && infoing == 1)
fatal_error("%<--info%> suppresses normal output, can%,t use with an\n output mode like %<--merge%> or %<--batch%>.\n (Try %<-II%>, which doesn%,t suppress normal output.)");
if (newmode != BLANK_MODE && newmode != mode)
fatal_error("too late to change modes");
}
void
set_frame_change(int kind)
{
int i;
Gt_Frameset *fset;
set_mode(BLANK_MODE);
if (mode < DELETING && frames_done) {
fatal_error("frame selection and frame changes don%,t mix");
return;
}
assert(!nested_mode);
nested_mode = mode;
if (frame_spec_1 > frame_spec_2) {
i = frame_spec_1;
frame_spec_1 = frame_spec_2;
frame_spec_2 = i;
}
switch (kind) {
case DELETE_OPT:
mode = DELETING;
break;
case REPLACE_OPT:
for (i = frame_spec_1; i < frame_spec_2; i++)
FRAME(frames, i).use = 0;
/* We want to use the last frame's delay, but nothing else about it. */
FRAME(frames, i).use = -1;
/* FALLTHRU */
case INSERT_OPT:
/* Define a nested frameset (or use an existing one). */
fset = FRAME(frames, frame_spec_2).nest;
if (!fset) fset = new_frameset(8);
FRAME(frames, frame_spec_2).nest = fset;
/* Later: Merge frames at the end of the nested frameset. */
mode = INSERTING;
nested_frames = frames;
frames = fset;
break;
case APPEND_OPT:
/* Just merge frames at the end of this frameset. */
mode = INSERTING;
break;
}
}
static void
frame_change_done(void)
{
if (nested_mode)
mode = nested_mode;
if (nested_frames)
frames = nested_frames;
nested_mode = 0;
nested_frames = 0;
}
static void
show_frame(int imagenumber, int usename)
{
Gif_Image *gfi;
Gt_Frame *frame;
if (!input || !(gfi = Gif_GetImage(input, imagenumber)))
return;
switch (mode) {
case MERGING:
case INSERTING:
case EXPLODING:
case INFOING:
case BATCHING:
if (!frames_done)
clear_frameset(frames, first_input_frame);
frame = add_frame(frames, input, gfi);
if (usename)
frame->explode_by_name = 1;
break;
case DELETING:
frame = &FRAME(frames, first_input_frame + imagenumber);
frame->use = 0;
break;
}
next_frame = 0;
frames_done = 1;
}
/*****
* input a stream
**/
static void
gifread_error(Gif_Stream* gfs, Gif_Image* gfi,
int is_error, const char* message)
{
static int last_is_error = 0;
static char last_landmark[256];
static char last_message[256];
static int different_error_count = 0;
static int same_error_count = 0;
char landmark[256];
int which_image = Gif_ImageNumber(gfs, gfi);
if (gfs && which_image < 0)
which_image = gfs->nimages;
/* ignore warnings if "no_warning" */
if (no_warnings && is_error == 0)
return;
if (message) {
const char *filename = gfs && gfs->landmark ? gfs->landmark : "<unknown>";
if (gfi && (which_image != 0 || gfs->nimages > 1))
snprintf(landmark, sizeof(landmark), "%s:#%d",
filename, which_image < 0 ? gfs->nimages : which_image);
else
snprintf(landmark, sizeof(landmark), "%s", filename);
}
if (last_message[0]
&& different_error_count <= 10
&& (!message
|| strcmp(message, last_message) != 0
|| strcmp(landmark, last_landmark) != 0)) {
const char* etype = last_is_error ? "read error: " : "";
void (*f)(const char*, const char*, ...) = last_is_error ? lerror : lwarning;
if (gfi && gfi->user_flags)
/* error already reported */;
else if (same_error_count == 1)
f(last_landmark, "%s%s", etype, last_message);
else if (same_error_count > 0)
f(last_landmark, "%s%s (%d times)", etype, last_message, same_error_count);
same_error_count = 0;
last_message[0] = 0;
}
if (message) {
if (last_message[0] == 0)
different_error_count++;
same_error_count++;
strncpy(last_message, message, sizeof(last_message));
last_message[sizeof(last_message) - 1] = 0;
strncpy(last_landmark, landmark, sizeof(last_landmark));
last_landmark[sizeof(last_landmark) - 1] = 0;
last_is_error = is_error;
if (different_error_count == 11) {
if (!(gfi && gfi->user_flags))
error(0, "(plus more errors; is this GIF corrupt?)");
different_error_count++;
}
} else
last_message[0] = 0;
{
unsigned long missing;
if (message && sscanf(message, "missing %lu pixel", &missing) == 1
&& missing > 10000 && no_ignore_errors) {
gifread_error(gfs, 0, -1, 0);
lerror(landmark, "fatal error: too many missing pixels, giving up");
exit(1);
}
}
if (gfi && is_error < 0)
gfi->user_flags |= 1;
}
struct StoredFile {
FILE *f;
struct StoredFile *next;
char name[1];
};
static struct StoredFile *stored_files = 0;
static FILE *
open_giffile(const char *name)
{
struct StoredFile *sf;
FILE *f;
if (name == 0 || strcmp(name, "-") == 0) {
#ifndef OUTPUT_GIF_TO_TERMINAL
if (isatty(fileno(stdin))) {
lerror("<stdin>", "Is a terminal");
return NULL;
}
#endif
#if defined(_MSDOS) || defined(_WIN32)
_setmode(_fileno(stdin), _O_BINARY);
#elif defined(__DJGPP__)
setmode(fileno(stdin), O_BINARY);
#elif defined(__EMX__)
_fsetmode(stdin, "b");
#endif
return stdin;
}
if (nextfile)
for (sf = stored_files; sf; sf = sf->next)
if (strcmp(name, sf->name) == 0)
return sf->f;
f = fopen(name, "rb");
if (f && nextfile) {
sf = (struct StoredFile *) malloc(sizeof(struct StoredFile) + strlen(name));
sf->f = f;
sf->next = stored_files;
stored_files = sf;
strcpy(sf->name, name);
} else if (!f)
lerror(name, "%s", strerror(errno));
return f;
}
static void
close_giffile(FILE *f, int final)
{
struct StoredFile **sf_pprev, *sf;
if (!final && nextfile) {
int c = getc(f);
if (c == EOF)
final = 1;
else
ungetc(c, f);
}
for (sf_pprev = &stored_files; (sf = *sf_pprev); sf_pprev = &sf->next)
if (sf->f == f) {
if (final) {
fclose(f);
*sf_pprev = sf->next;
free((void *) sf);
}
return;
}
if (f != stdin)
fclose(f);
}
void
input_stream(const char *name)
{
char* component_namebuf;
FILE *f;
Gif_Stream *gfs;
int i;
int saved_next_frame = next_frame;
int componentno = 0;
const char *main_name = 0;
Gt_Frame old_def_frame;
input = 0;
input_name = name;
frames_done = 0;
next_frame = 0;
next_input = 0;
if (next_output)
combine_output_options();
files_given++;
set_mode(BLANK_MODE);
f = open_giffile(name);
if (!f)
return;
if (f == stdin) {
name = "<stdin>";
input_name = 0;
}
main_name = name;
retry_file:
/* change filename for component files */
componentno++;
if (componentno > 1) {
component_namebuf = (char*) malloc(strlen(main_name) + 10);
sprintf(component_namebuf, "%s~%d", main_name, componentno);
name = component_namebuf;
} else
component_namebuf = 0;
/* check for empty file */
i = getc(f);
if (i == EOF) {
if (!(gif_read_flags & GIF_READ_TRAILING_GARBAGE_OK))
lerror(name, "empty file");
else if (nextfile)
lerror(name, "no more images in file");
goto error;
}
ungetc(i, f);
if (verbosing)
verbose_open('<', name);
/* read file */
{
int old_error_count = error_count;
gfs = Gif_FullReadFile(f, gif_read_flags | GIF_READ_COMPRESSED,
name, gifread_error);
if ((!gfs || (Gif_ImageCount(gfs) == 0 && gfs->errors > 0))
&& componentno != 1)
lerror(name, "trailing garbage ignored");
if (!no_ignore_errors)
error_count = old_error_count;
}
if (!gfs || (Gif_ImageCount(gfs) == 0 && gfs->errors > 0)) {
if (componentno == 1)
lerror(name, "file not in GIF format");
Gif_DeleteStream(gfs);
if (verbosing)
verbose_close('>');
goto error;
}
/* special processing for components after the first */
if (componentno > 1) {
if (mode == BATCHING || mode == INSERTING)
fatal_error("%s: %<--multifile%> is useful only in merge mode", name);
input_done();
}
input = gfs;
/* Processing when we've got a new input frame */
set_mode(BLANK_MODE);
if (active_output_data.output_name == 0) {
/* Don't override explicit output names.
This code works 'cause output_name is reset to 0 after each output. */
if (mode == BATCHING)
active_output_data.output_name = input_name;
else if (mode == EXPLODING) {
/* Explode into current directory. */
const char *explode_name = (input_name ? input_name : "#stdin#");
const char *slash = strrchr(explode_name, PATHNAME_SEPARATOR);
if (slash)
active_output_data.output_name = slash + 1;
else
active_output_data.output_name = explode_name;
}
}
/* This code rather sucks. Here's the problem: Since we consider options
strictly sequentially, one at a time, we can't tell the difference
between these:
--name=X g.gif h.gif // name on g.gif #0
--name=X g.gif #2 h.gif // name on g.gif #2
g.gif --name=X #2 h.gif // name on g.gif #2
g.gif --name=X h.gif // name on h.gif #0 !!!
Here's the solution. Mark when we CHANGE an option. After processing
an input GIF, mark all the options as 'unchanged' -- but leave the
VALUES as is. Then when we read the next frame, CLEAR the unchanged
options. So it's like so: (* means changed, . means not.)
[-.] --name=X [X*] g.gif [X.] #2 [-.] h.gif == name on g.gif #2
[-.] g.gif [-.] --name=X [X*] #2 [-.] h.gif == name on g.gif #2
[-.] --name=X [X*] g.gif [X.|-.] h.gif == name on g.gif #0
[-.] g.gif [-.] --name=X [X*] h.gif == name on h.gif #0 */
/* Clear old options from the last input stream */
if (!CHANGED(saved_next_frame, CH_NAME))
def_frame.name = 0;
if (!CHANGED(saved_next_frame, CH_COMMENT))
def_frame.comment = 0;
if (!CHANGED(saved_next_frame, CH_EXTENSION))
def_frame.extensions = 0;
def_frame.input_filename = input_name;
old_def_frame = def_frame;
first_input_frame = frames->count;
def_frame.position_is_offset = 1;
for (i = 0; i < gfs->nimages; i++)
add_frame(frames, gfs, gfs->images[i]);
def_frame = old_def_frame;
if (unoptimizing)
if (!Gif_FullUnoptimize(gfs, GIF_UNOPTIMIZE_SIMPLEST_DISPOSAL)) {
static int context = 0;
if (!context) {
lwarning(name, "GIF too complex to unoptimize\n"
" (The reason was local color tables or complex transparency.\n"
" Try running the GIF through %<gifsicle --colors=255%> first.)");
context = 1;
} else
lwarning(name, "GIF too complex to unoptimize");
}
apply_color_transforms(input_transforms, gfs);
gfs->refcount++;
/* Read more files. */
free(component_namebuf);
if ((gif_read_flags & GIF_READ_TRAILING_GARBAGE_OK) && !nextfile)
goto retry_file;
close_giffile(f, 0);
return;
error:
free(component_namebuf);
close_giffile(f, 1);
}
void
input_done(void)
{
if (!input) return;
if (verbosing) verbose_close('>');
Gif_DeleteStream(input);
input = 0;
if (mode == DELETING)
frame_change_done();
if (mode == BATCHING || mode == EXPLODING)
output_frames();
}
/*****
* colormap stuff
**/
static void
set_new_fixed_colormap(const char *name)
{
int i;
if (name && strcmp(name, "web") == 0) {
Gif_Colormap *cm = Gif_NewFullColormap(216, 256);
Gif_Color *col = cm->col;
for (i = 0; i < 216; i++) {
col[i].gfc_red = (i / 36) * 0x33;
col[i].gfc_green = ((i / 6) % 6) * 0x33;
col[i].gfc_blue = (i % 6) * 0x33;
}
def_output_data.colormap_fixed = cm;
} else if (name && (strcmp(name, "gray") == 0
|| strcmp(name, "grey") == 0)) {
Gif_Colormap *cm = Gif_NewFullColormap(256, 256);
Gif_Color *col = cm->col;
for (i = 0; i < 256; i++)
col[i].gfc_red = col[i].gfc_green = col[i].gfc_blue = i;
def_output_data.colormap_fixed = cm;
} else if (name && strcmp(name, "bw") == 0) {
Gif_Colormap *cm = Gif_NewFullColormap(2, 256);
cm->col[0].gfc_red = cm->col[0].gfc_green = cm->col[0].gfc_blue = 0;
cm->col[1].gfc_red = cm->col[1].gfc_green = cm->col[1].gfc_blue = 255;
def_output_data.colormap_fixed = cm;
} else
def_output_data.colormap_fixed = read_colormap_file(name, 0);
}
static void
do_colormap_change(Gif_Stream *gfs)
{
if (active_output_data.colormap_fixed || active_output_data.colormap_size > 0)
kc_set_gamma(active_output_data.colormap_gamma_type,
active_output_data.colormap_gamma);
if (active_output_data.colormap_fixed)
colormap_stream(gfs, active_output_data.colormap_fixed,
&active_output_data);
if (active_output_data.colormap_size > 0) {
kchist kch;
Gif_Colormap* (*adapt_func)(kchist*, Gt_OutputData*);
Gif_Colormap *new_cm;
/* set up the histogram */
{
uint32_t ntransp;
int i, any_locals = 0;
for (i = 0; i < gfs->nimages; i++)
if (gfs->images[i]->local)
any_locals = 1;
kchist_make(&kch, gfs, &ntransp);
if (kch.n <= active_output_data.colormap_size
&& !any_locals
&& !active_output_data.colormap_fixed) {
warning(1, "trivial adaptive palette (only %d colors in source)", kch.n);
kchist_cleanup(&kch);
return;
}
active_output_data.colormap_needs_transparency = ntransp > 0;
}
switch (active_output_data.colormap_algorithm) {
case COLORMAP_DIVERSITY:
adapt_func = &colormap_flat_diversity;
break;
case COLORMAP_BLEND_DIVERSITY:
adapt_func = &colormap_blend_diversity;
break;
case COLORMAP_MEDIAN_CUT:
adapt_func = &colormap_median_cut;
break;
default:
fatal_error("can't happen");
}
new_cm = (*adapt_func)(&kch, &active_output_data);
colormap_stream(gfs, new_cm, &active_output_data);
Gif_DeleteColormap(new_cm);
kchist_cleanup(&kch);
}
}
/*****
* output GIF images
**/
static void
write_stream(const char *output_name, Gif_Stream *gfs)
{
FILE *f;
if (output_name)
f = fopen(output_name, "wb");
else {
#ifndef OUTPUT_GIF_TO_TERMINAL
if (isatty(fileno(stdout))) {
lerror("<stdout>", "Is a terminal: try `-o OUTPUTFILE`");
return;
}
#endif
#if defined(_MSDOS) || defined(_WIN32)
_setmode(_fileno(stdout), _O_BINARY);
#elif defined(__DJGPP__)
setmode(fileno(stdout), O_BINARY);
#elif defined(__EMX__)
_fsetmode(stdout, "b");
#endif
f = stdout;
output_name = "<stdout>";
}
if (f) {
Gif_FullWriteFile(gfs, &gif_write_info, f);
fclose(f);
any_output_successful = 1;
} else
lerror(output_name, "%s", strerror(errno));
}
static void
merge_and_write_frames(const char *outfile, int f1, int f2)
{
Gif_Stream *out;
int compress_immediately;
int colormap_change;
int huge_stream;
assert(!nested_mode);
if (verbosing)