/
layer.c
2057 lines (1972 loc) Β· 74.3 KB
/
layer.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
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% L AAA Y Y EEEEE RRRR %
% L A A Y Y E R R %
% L AAAAA Y EEE RRRR %
% L A A Y E R R %
% LLLLL A A Y EEEEE R R %
% %
% MagickCore Image Layering Methods %
% %
% Software Design %
% Cristy %
% Anthony Thyssen %
% January 2006 %
% %
% %
% Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% https://imagemagick.org/script/license.php %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
*/
/*
Include declarations.
*/
#include "MagickCore/studio.h"
#include "MagickCore/artifact.h"
#include "MagickCore/cache.h"
#include "MagickCore/channel.h"
#include "MagickCore/color.h"
#include "MagickCore/color-private.h"
#include "MagickCore/composite.h"
#include "MagickCore/effect.h"
#include "MagickCore/exception.h"
#include "MagickCore/exception-private.h"
#include "MagickCore/geometry.h"
#include "MagickCore/image.h"
#include "MagickCore/layer.h"
#include "MagickCore/list.h"
#include "MagickCore/memory_.h"
#include "MagickCore/monitor.h"
#include "MagickCore/monitor-private.h"
#include "MagickCore/option.h"
#include "MagickCore/pixel-accessor.h"
#include "MagickCore/property.h"
#include "MagickCore/profile.h"
#include "MagickCore/resource_.h"
#include "MagickCore/resize.h"
#include "MagickCore/statistic.h"
#include "MagickCore/string_.h"
#include "MagickCore/transform.h"
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ C l e a r B o u n d s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ClearBounds() Clear the area specified by the bounds in an image to
% transparency. This typically used to handle Background Disposal for the
% previous frame in an animation sequence.
%
% Warning: no bounds checks are performed, except for the null or missed
% image, for images that don't change. in all other cases bound must fall
% within the image.
%
% The format is:
%
% void ClearBounds(Image *image,RectangleInfo *bounds,
% ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: the image to had the area cleared in
%
% o bounds: the area to be clear within the imag image
%
% o exception: return any errors or warnings in this structure.
%
*/
static void ClearBounds(Image *image,RectangleInfo *bounds,
ExceptionInfo *exception)
{
ssize_t
y;
if (bounds->x < 0)
return;
if (image->alpha_trait == UndefinedPixelTrait)
(void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
for (y=0; y < (ssize_t) bounds->height; y++)
{
register ssize_t
x;
register Quantum
*magick_restrict q;
q=GetAuthenticPixels(image,bounds->x,bounds->y+y,bounds->width,1,exception);
if (q == (Quantum *) NULL)
break;
for (x=0; x < (ssize_t) bounds->width; x++)
{
SetPixelAlpha(image,TransparentAlpha,q);
q+=GetPixelChannels(image);
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ I s B o u n d s C l e a r e d %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% IsBoundsCleared() tests whether any pixel in the bounds given, gets cleared
% when going from the first image to the second image. This typically used
% to check if a proposed disposal method will work successfully to generate
% the second frame image from the first disposed form of the previous frame.
%
% Warning: no bounds checks are performed, except for the null or missed
% image, for images that don't change. in all other cases bound must fall
% within the image.
%
% The format is:
%
% MagickBooleanType IsBoundsCleared(const Image *image1,
% const Image *image2,RectangleInfo bounds,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image1, image 2: the images to check for cleared pixels
%
% o bounds: the area to be clear within the imag image
%
% o exception: return any errors or warnings in this structure.
%
*/
static MagickBooleanType IsBoundsCleared(const Image *image1,
const Image *image2,RectangleInfo *bounds,ExceptionInfo *exception)
{
register const Quantum
*p,
*q;
register ssize_t
x;
ssize_t
y;
if (bounds->x < 0)
return(MagickFalse);
for (y=0; y < (ssize_t) bounds->height; y++)
{
p=GetVirtualPixels(image1,bounds->x,bounds->y+y,bounds->width,1,exception);
q=GetVirtualPixels(image2,bounds->x,bounds->y+y,bounds->width,1,exception);
if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
break;
for (x=0; x < (ssize_t) bounds->width; x++)
{
if ((GetPixelAlpha(image1,p) >= (Quantum) (QuantumRange/2)) &&
(GetPixelAlpha(image2,q) < (Quantum) (QuantumRange/2)))
break;
p+=GetPixelChannels(image1);
q+=GetPixelChannels(image2);
}
if (x < (ssize_t) bounds->width)
break;
}
return(y < (ssize_t) bounds->height ? MagickTrue : MagickFalse);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% C o a l e s c e I m a g e s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% CoalesceImages() composites a set of images while respecting any page
% offsets and disposal methods. GIF, MIFF, and MNG animation sequences
% typically start with an image background and each subsequent image
% varies in size and offset. A new image sequence is returned with all
% images the same size as the first images virtual canvas and composited
% with the next image in the sequence.
%
% The format of the CoalesceImages method is:
%
% Image *CoalesceImages(Image *image,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: the image sequence.
%
% o exception: return any errors or warnings in this structure.
%
*/
MagickExport Image *CoalesceImages(const Image *image,ExceptionInfo *exception)
{
Image
*coalesce_image,
*dispose_image,
*previous;
register Image
*next;
RectangleInfo
bounds;
/*
Coalesce the image sequence.
*/
assert(image != (Image *) NULL);
assert(image->signature == MagickCoreSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
next=GetFirstImageInList(image);
bounds=next->page;
if (bounds.width == 0)
{
bounds.width=next->columns;
if (bounds.x > 0)
bounds.width+=bounds.x;
}
if (bounds.height == 0)
{
bounds.height=next->rows;
if (bounds.y > 0)
bounds.height+=bounds.y;
}
bounds.x=0;
bounds.y=0;
coalesce_image=CloneImage(next,bounds.width,bounds.height,MagickTrue,
exception);
if (coalesce_image == (Image *) NULL)
return((Image *) NULL);
coalesce_image->background_color.alpha=(MagickRealType) TransparentAlpha;
(void) SetImageBackgroundColor(coalesce_image,exception);
coalesce_image->alpha_trait=next->alpha_trait;
coalesce_image->page=bounds;
coalesce_image->dispose=NoneDispose;
/*
Coalesce rest of the images.
*/
dispose_image=CloneImage(coalesce_image,0,0,MagickTrue,exception);
(void) CompositeImage(coalesce_image,next,CopyCompositeOp,MagickTrue,
next->page.x,next->page.y,exception);
next=GetNextImageInList(next);
for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
{
/*
Determine the bounds that was overlaid in the previous image.
*/
previous=GetPreviousImageInList(next);
bounds=previous->page;
bounds.width=previous->columns;
bounds.height=previous->rows;
if (bounds.x < 0)
{
bounds.width+=bounds.x;
bounds.x=0;
}
if ((ssize_t) (bounds.x+bounds.width) > (ssize_t) coalesce_image->columns)
bounds.width=coalesce_image->columns-bounds.x;
if (bounds.y < 0)
{
bounds.height+=bounds.y;
bounds.y=0;
}
if ((ssize_t) (bounds.y+bounds.height) > (ssize_t) coalesce_image->rows)
bounds.height=coalesce_image->rows-bounds.y;
/*
Replace the dispose image with the new coalesced image.
*/
if (GetPreviousImageInList(next)->dispose != PreviousDispose)
{
dispose_image=DestroyImage(dispose_image);
dispose_image=CloneImage(coalesce_image,0,0,MagickTrue,exception);
if (dispose_image == (Image *) NULL)
{
coalesce_image=DestroyImageList(coalesce_image);
return((Image *) NULL);
}
}
/*
Clear the overlaid area of the coalesced bounds for background disposal
*/
if (next->previous->dispose == BackgroundDispose)
ClearBounds(dispose_image,&bounds,exception);
/*
Next image is the dispose image, overlaid with next frame in sequence.
*/
coalesce_image->next=CloneImage(dispose_image,0,0,MagickTrue,exception);
coalesce_image->next->previous=coalesce_image;
previous=coalesce_image;
coalesce_image=GetNextImageInList(coalesce_image);
(void) CompositeImage(coalesce_image,next,
next->alpha_trait != UndefinedPixelTrait ? OverCompositeOp : CopyCompositeOp,
MagickTrue,next->page.x,next->page.y,exception);
(void) CloneImageProfiles(coalesce_image,next);
(void) CloneImageProperties(coalesce_image,next);
(void) CloneImageArtifacts(coalesce_image,next);
coalesce_image->page=previous->page;
/*
If a pixel goes opaque to transparent, use background dispose.
*/
if (IsBoundsCleared(previous,coalesce_image,&bounds,exception) != MagickFalse)
coalesce_image->dispose=BackgroundDispose;
else
coalesce_image->dispose=NoneDispose;
previous->dispose=coalesce_image->dispose;
}
dispose_image=DestroyImage(dispose_image);
return(GetFirstImageInList(coalesce_image));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% D i s p o s e I m a g e s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DisposeImages() returns the coalesced frames of a GIF animation as it would
% appear after the GIF dispose method of that frame has been applied. That is
% it returned the appearance of each frame before the next is overlaid.
%
% The format of the DisposeImages method is:
%
% Image *DisposeImages(Image *image,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o images: the image sequence.
%
% o exception: return any errors or warnings in this structure.
%
*/
MagickExport Image *DisposeImages(const Image *images,ExceptionInfo *exception)
{
Image
*dispose_image,
*dispose_images;
RectangleInfo
bounds;
register Image
*image,
*next;
/*
Run the image through the animation sequence
*/
assert(images != (Image *) NULL);
assert(images->signature == MagickCoreSignature);
if (images->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
image=GetFirstImageInList(images);
dispose_image=CloneImage(image,image->page.width,image->page.height,
MagickTrue,exception);
if (dispose_image == (Image *) NULL)
return((Image *) NULL);
dispose_image->page=image->page;
dispose_image->page.x=0;
dispose_image->page.y=0;
dispose_image->dispose=NoneDispose;
dispose_image->background_color.alpha=(MagickRealType) TransparentAlpha;
(void) SetImageBackgroundColor(dispose_image,exception);
dispose_images=NewImageList();
for (next=image; image != (Image *) NULL; image=GetNextImageInList(image))
{
Image
*current_image;
/*
Overlay this frame's image over the previous disposal image.
*/
current_image=CloneImage(dispose_image,0,0,MagickTrue,exception);
if (current_image == (Image *) NULL)
{
dispose_images=DestroyImageList(dispose_images);
dispose_image=DestroyImage(dispose_image);
return((Image *) NULL);
}
(void) CompositeImage(current_image,next,
next->alpha_trait != UndefinedPixelTrait ? OverCompositeOp : CopyCompositeOp,
MagickTrue,next->page.x,next->page.y,exception);
/*
Handle Background dispose: image is displayed for the delay period.
*/
if (next->dispose == BackgroundDispose)
{
bounds=next->page;
bounds.width=next->columns;
bounds.height=next->rows;
if (bounds.x < 0)
{
bounds.width+=bounds.x;
bounds.x=0;
}
if ((ssize_t) (bounds.x+bounds.width) > (ssize_t) current_image->columns)
bounds.width=current_image->columns-bounds.x;
if (bounds.y < 0)
{
bounds.height+=bounds.y;
bounds.y=0;
}
if ((ssize_t) (bounds.y+bounds.height) > (ssize_t) current_image->rows)
bounds.height=current_image->rows-bounds.y;
ClearBounds(current_image,&bounds,exception);
}
/*
Select the appropriate previous/disposed image.
*/
if (next->dispose == PreviousDispose)
current_image=DestroyImage(current_image);
else
{
dispose_image=DestroyImage(dispose_image);
dispose_image=current_image;
current_image=(Image *) NULL;
}
/*
Save the dispose image just calculated for return.
*/
{
Image
*dispose;
dispose=CloneImage(dispose_image,0,0,MagickTrue,exception);
if (dispose == (Image *) NULL)
{
dispose_images=DestroyImageList(dispose_images);
dispose_image=DestroyImage(dispose_image);
return((Image *) NULL);
}
(void) CloneImageProfiles(dispose,next);
(void) CloneImageProperties(dispose,next);
(void) CloneImageArtifacts(dispose,next);
dispose->page.x=0;
dispose->page.y=0;
dispose->dispose=next->dispose;
AppendImageToList(&dispose_images,dispose);
}
}
dispose_image=DestroyImage(dispose_image);
return(GetFirstImageInList(dispose_images));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ C o m p a r e P i x e l s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ComparePixels() Compare the two pixels and return true if the pixels
% differ according to the given LayerType comparision method.
%
% This currently only used internally by CompareImagesBounds(). It is
% doubtful that this sub-routine will be useful outside this module.
%
% The format of the ComparePixels method is:
%
% MagickBooleanType *ComparePixels(const LayerMethod method,
% const PixelInfo *p,const PixelInfo *q)
%
% A description of each parameter follows:
%
% o method: What differences to look for. Must be one of
% CompareAnyLayer, CompareClearLayer, CompareOverlayLayer.
%
% o p, q: the pixels to test for appropriate differences.
%
*/
static MagickBooleanType ComparePixels(const LayerMethod method,
const PixelInfo *p,const PixelInfo *q)
{
double
o1,
o2;
/*
Any change in pixel values
*/
if (method == CompareAnyLayer)
return((MagickBooleanType)(IsFuzzyEquivalencePixelInfo(p,q) == MagickFalse));
o1 = (p->alpha_trait != UndefinedPixelTrait) ? p->alpha : OpaqueAlpha;
o2 = (q->alpha_trait != UndefinedPixelTrait) ? q->alpha : OpaqueAlpha;
/*
Pixel goes from opaque to transprency.
*/
if (method == CompareClearLayer)
return((MagickBooleanType) ( (o1 >= ((double) QuantumRange/2.0)) &&
(o2 < ((double) QuantumRange/2.0)) ) );
/*
Overlay would change first pixel by second.
*/
if (method == CompareOverlayLayer)
{
if (o2 < ((double) QuantumRange/2.0))
return MagickFalse;
return((MagickBooleanType) (IsFuzzyEquivalencePixelInfo(p,q) == MagickFalse));
}
return(MagickFalse);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ C o m p a r e I m a g e B o u n d s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% CompareImagesBounds() Given two images return the smallest rectangular area
% by which the two images differ, accourding to the given 'Compare...'
% layer method.
%
% This currently only used internally in this module, but may eventually
% be used by other modules.
%
% The format of the CompareImagesBounds method is:
%
% RectangleInfo *CompareImagesBounds(const LayerMethod method,
% const Image *image1,const Image *image2,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o method: What differences to look for. Must be one of CompareAnyLayer,
% CompareClearLayer, CompareOverlayLayer.
%
% o image1, image2: the two images to compare.
%
% o exception: return any errors or warnings in this structure.
%
*/
static RectangleInfo CompareImagesBounds(const Image *image1,
const Image *image2,const LayerMethod method,ExceptionInfo *exception)
{
RectangleInfo
bounds;
PixelInfo
pixel1,
pixel2;
register const Quantum
*p,
*q;
register ssize_t
x;
ssize_t
y;
/*
Set bounding box of the differences between images.
*/
GetPixelInfo(image1,&pixel1);
GetPixelInfo(image2,&pixel2);
for (x=0; x < (ssize_t) image1->columns; x++)
{
p=GetVirtualPixels(image1,x,0,1,image1->rows,exception);
q=GetVirtualPixels(image2,x,0,1,image2->rows,exception);
if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
break;
for (y=0; y < (ssize_t) image1->rows; y++)
{
GetPixelInfoPixel(image1,p,&pixel1);
GetPixelInfoPixel(image2,q,&pixel2);
if (ComparePixels(method,&pixel1,&pixel2))
break;
p+=GetPixelChannels(image1);
q+=GetPixelChannels(image2);
}
if (y < (ssize_t) image1->rows)
break;
}
if (x >= (ssize_t) image1->columns)
{
/*
Images are identical, return a null image.
*/
bounds.x=-1;
bounds.y=-1;
bounds.width=1;
bounds.height=1;
return(bounds);
}
bounds.x=x;
for (x=(ssize_t) image1->columns-1; x >= 0; x--)
{
p=GetVirtualPixels(image1,x,0,1,image1->rows,exception);
q=GetVirtualPixels(image2,x,0,1,image2->rows,exception);
if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
break;
for (y=0; y < (ssize_t) image1->rows; y++)
{
GetPixelInfoPixel(image1,p,&pixel1);
GetPixelInfoPixel(image2,q,&pixel2);
if (ComparePixels(method,&pixel1,&pixel2))
break;
p+=GetPixelChannels(image1);
q+=GetPixelChannels(image2);
}
if (y < (ssize_t) image1->rows)
break;
}
bounds.width=(size_t) (x-bounds.x+1);
for (y=0; y < (ssize_t) image1->rows; y++)
{
p=GetVirtualPixels(image1,0,y,image1->columns,1,exception);
q=GetVirtualPixels(image2,0,y,image2->columns,1,exception);
if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
break;
for (x=0; x < (ssize_t) image1->columns; x++)
{
GetPixelInfoPixel(image1,p,&pixel1);
GetPixelInfoPixel(image2,q,&pixel2);
if (ComparePixels(method,&pixel1,&pixel2))
break;
p+=GetPixelChannels(image1);
q+=GetPixelChannels(image2);
}
if (x < (ssize_t) image1->columns)
break;
}
bounds.y=y;
for (y=(ssize_t) image1->rows-1; y >= 0; y--)
{
p=GetVirtualPixels(image1,0,y,image1->columns,1,exception);
q=GetVirtualPixels(image2,0,y,image2->columns,1,exception);
if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
break;
for (x=0; x < (ssize_t) image1->columns; x++)
{
GetPixelInfoPixel(image1,p,&pixel1);
GetPixelInfoPixel(image2,q,&pixel2);
if (ComparePixels(method,&pixel1,&pixel2))
break;
p+=GetPixelChannels(image1);
q+=GetPixelChannels(image2);
}
if (x < (ssize_t) image1->columns)
break;
}
bounds.height=(size_t) (y-bounds.y+1);
return(bounds);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% C o m p a r e I m a g e L a y e r s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% CompareImagesLayers() compares each image with the next in a sequence and
% returns the minimum bounding region of all the pixel differences (of the
% LayerMethod specified) it discovers.
%
% Images do NOT have to be the same size, though it is best that all the
% images are 'coalesced' (images are all the same size, on a flattened
% canvas, so as to represent exactly how an specific frame should look).
%
% No GIF dispose methods are applied, so GIF animations must be coalesced
% before applying this image operator to find differences to them.
%
% The format of the CompareImagesLayers method is:
%
% Image *CompareImagesLayers(const Image *images,
% const LayerMethod method,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: the image.
%
% o method: the layers type to compare images with. Must be one of...
% CompareAnyLayer, CompareClearLayer, CompareOverlayLayer.
%
% o exception: return any errors or warnings in this structure.
%
*/
MagickExport Image *CompareImagesLayers(const Image *image,
const LayerMethod method,ExceptionInfo *exception)
{
Image
*image_a,
*image_b,
*layers;
RectangleInfo
*bounds;
register const Image
*next;
register ssize_t
i;
assert(image != (const Image *) NULL);
assert(image->signature == MagickCoreSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
assert((method == CompareAnyLayer) ||
(method == CompareClearLayer) ||
(method == CompareOverlayLayer));
/*
Allocate bounds memory.
*/
next=GetFirstImageInList(image);
bounds=(RectangleInfo *) AcquireQuantumMemory((size_t)
GetImageListLength(next),sizeof(*bounds));
if (bounds == (RectangleInfo *) NULL)
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
/*
Set up first comparision images.
*/
image_a=CloneImage(next,next->page.width,next->page.height,
MagickTrue,exception);
if (image_a == (Image *) NULL)
{
bounds=(RectangleInfo *) RelinquishMagickMemory(bounds);
return((Image *) NULL);
}
image_a->background_color.alpha=(MagickRealType) TransparentAlpha;
(void) SetImageBackgroundColor(image_a,exception);
image_a->page=next->page;
image_a->page.x=0;
image_a->page.y=0;
(void) CompositeImage(image_a,next,CopyCompositeOp,MagickTrue,next->page.x,
next->page.y,exception);
/*
Compute the bounding box of changes for the later images
*/
i=0;
next=GetNextImageInList(next);
for ( ; next != (const Image *) NULL; next=GetNextImageInList(next))
{
image_b=CloneImage(image_a,0,0,MagickTrue,exception);
if (image_b == (Image *) NULL)
{
image_a=DestroyImage(image_a);
bounds=(RectangleInfo *) RelinquishMagickMemory(bounds);
return((Image *) NULL);
}
(void) CompositeImage(image_a,next,CopyCompositeOp,MagickTrue,next->page.x,
next->page.y,exception);
bounds[i]=CompareImagesBounds(image_b,image_a,method,exception);
image_b=DestroyImage(image_b);
i++;
}
image_a=DestroyImage(image_a);
/*
Clone first image in sequence.
*/
next=GetFirstImageInList(image);
layers=CloneImage(next,0,0,MagickTrue,exception);
if (layers == (Image *) NULL)
{
bounds=(RectangleInfo *) RelinquishMagickMemory(bounds);
return((Image *) NULL);
}
/*
Deconstruct the image sequence.
*/
i=0;
next=GetNextImageInList(next);
for ( ; next != (const Image *) NULL; next=GetNextImageInList(next))
{
if ((bounds[i].x == -1) && (bounds[i].y == -1) &&
(bounds[i].width == 1) && (bounds[i].height == 1))
{
/*
An empty frame is returned from CompareImageBounds(), which means the
current frame is identical to the previous frame.
*/
i++;
continue;
}
image_a=CloneImage(next,0,0,MagickTrue,exception);
if (image_a == (Image *) NULL)
break;
image_b=CropImage(image_a,&bounds[i],exception);
image_a=DestroyImage(image_a);
if (image_b == (Image *) NULL)
break;
AppendImageToList(&layers,image_b);
i++;
}
bounds=(RectangleInfo *) RelinquishMagickMemory(bounds);
if (next != (Image *) NULL)
{
layers=DestroyImageList(layers);
return((Image *) NULL);
}
return(GetFirstImageInList(layers));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ O p t i m i z e L a y e r F r a m e s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% OptimizeLayerFrames() takes a coalesced GIF animation, and compares each
% frame against the three different 'disposal' forms of the previous frame.
% From this it then attempts to select the smallest cropped image and
% disposal method needed to reproduce the resulting image.
%
% Note that this not easy, and may require the expansion of the bounds
% of previous frame, simply clear pixels for the next animation frame to
% transparency according to the selected dispose method.
%
% The format of the OptimizeLayerFrames method is:
%
% Image *OptimizeLayerFrames(const Image *image,
% const LayerMethod method,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: the image.
%
% o method: the layers technique to optimize with. Must be one of...
% OptimizeImageLayer, or OptimizePlusLayer. The Plus form allows
% the addition of extra 'zero delay' frames to clear pixels from
% the previous frame, and the removal of frames that done change,
% merging the delay times together.
%
% o exception: return any errors or warnings in this structure.
%
*/
/*
Define a 'fake' dispose method where the frame is duplicated, (for
OptimizePlusLayer) with a extra zero time delay frame which does a
BackgroundDisposal to clear the pixels that need to be cleared.
*/
#define DupDispose ((DisposeType)9)
/*
Another 'fake' dispose method used to removed frames that don't change.
*/
#define DelDispose ((DisposeType)8)
#define DEBUG_OPT_FRAME 0
static Image *OptimizeLayerFrames(const Image *image,const LayerMethod method,
ExceptionInfo *exception)
{
ExceptionInfo
*sans_exception;
Image
*prev_image,
*dup_image,
*bgnd_image,
*optimized_image;
RectangleInfo
try_bounds,
bgnd_bounds,
dup_bounds,
*bounds;
MagickBooleanType
add_frames,
try_cleared,
cleared;
DisposeType
*disposals;
register const Image
*curr;
register ssize_t
i;
assert(image != (const Image *) NULL);
assert(image->signature == MagickCoreSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
assert(method == OptimizeLayer ||
method == OptimizeImageLayer ||
method == OptimizePlusLayer);
/*
Are we allowed to add/remove frames from animation?
*/
add_frames=method == OptimizePlusLayer ? MagickTrue : MagickFalse;
/*
Ensure all the images are the same size.
*/
curr=GetFirstImageInList(image);
for (; curr != (Image *) NULL; curr=GetNextImageInList(curr))
{
if ((curr->columns != image->columns) || (curr->rows != image->rows))
ThrowImageException(OptionError,"ImagesAreNotTheSameSize");
if ((curr->page.x != 0) || (curr->page.y != 0) ||
(curr->page.width != image->page.width) ||
(curr->page.height != image->page.height))
ThrowImageException(OptionError,"ImagePagesAreNotCoalesced");
}
/*
Allocate memory (times 2 if we allow the use of frame duplications)
*/
curr=GetFirstImageInList(image);
bounds=(RectangleInfo *) AcquireQuantumMemory((size_t)
GetImageListLength(curr),(add_frames != MagickFalse ? 2UL : 1UL)*
sizeof(*bounds));
if (bounds == (RectangleInfo *) NULL)
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
disposals=(DisposeType *) AcquireQuantumMemory((size_t)
GetImageListLength(image),(add_frames != MagickFalse ? 2UL : 1UL)*
sizeof(*disposals));
if (disposals == (DisposeType *) NULL)
{
bounds=(RectangleInfo *) RelinquishMagickMemory(bounds);
ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
}
/*
Initialise Previous Image as fully transparent
*/
prev_image=CloneImage(curr,curr->columns,curr->rows,MagickTrue,exception);
if (prev_image == (Image *) NULL)
{
bounds=(RectangleInfo *) RelinquishMagickMemory(bounds);
disposals=(DisposeType *) RelinquishMagickMemory(disposals);
return((Image *) NULL);
}
prev_image->page=curr->page; /* ERROR: <-- should not be need, but is! */
prev_image->page.x=0;
prev_image->page.y=0;