1#include <stdio.h>
2#include <MagickWand/MagickWand.h>
3
4/* set this to true to test loops methods with a empty wand */
5#define TEST_EMPTY_WAND 0
6
7/* Simplify the exception handling
8 * technically we should abort the program if
9 *      severity >= ErrorException
10 */
11void ThrowWandException(MagickWand *wand)
12{ char
13  *description;
14
15  ExceptionType
16  severity;
17
18  description=MagickGetException(wand,&severity);
19  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description);
20  description=(char *) MagickRelinquishMemory(description);
21}
22
23/* useful function especially after appending two wands together */
24#define SwapWands(a,b) { MagickWand *tmp=a; a=b; b=tmp; }
25
26int main(int argc, char *argv[])
27{
28  MagickWand
29    *wand,
30    *output;
31
32  MagickBooleanType
33    status;
34
35  MagickWandGenesis();
36
37  printf("Read in a list of 6 images...\n");
38
39  wand = NewMagickWand();
40#if !TEST_EMPTY_WAND
41  status = MagickReadImage(wand, "font_0.gif" )
42        && MagickReadImage(wand, "font_1.gif" )
43        && MagickReadImage(wand, "font_2.gif" )
44        && MagickReadImage(wand, "font_3.gif" )
45        && MagickReadImage(wand, "font_4.gif" )
46        && MagickReadImage(wand, "font_5.gif" );
47  if (status == MagickFalse)
48    ThrowWandException(wand);
49#endif
50
51  printf("I actually read in %u images\n",
52             (unsigned) MagickGetNumberImages(wand) );
53  printf("\n");
54
55  printf("After reading current image is #%d \"%s\"\n",
56              (unsigned) MagickGetIteratorIndex(wand),
57              MagickGetImageFilename(wand) );
58  printf("\n");
59
60  // Note that using MagickGetIteratorIndex() is slower than just
61  // keeping track of the current image index yourself! But not a great cost.
62
63  printf("Standard 'Reset while Next' loop through images\n");
64  // keeping track of it to start with!
65  MagickResetIterator(wand);
66  while (MagickNextImage(wand) != MagickFalse)
67    printf("image #%u \"%s\"\n",
68              (unsigned) MagickGetIteratorIndex(wand),
69              MagickGetImageFilename(wand) );
70  printf("\n");
71
72  printf("At this point, any image 'added' to wand will be appended!\n");
73  printf("This special condition can be set by using either\n");
74  printf("just         MagickSetLastIterator(w)\n");
75  printf("or           MagickSetIteratorIndex(w,-1)\n");
76  printf("\n");
77
78  printf("Now that we are at the end, lets loop backward using 'Previous'\n");
79  while (MagickPreviousImage(wand) != MagickFalse)
80    printf("image #%u \"%s\"\n",
81              (unsigned) MagickGetIteratorIndex(wand),
82              MagickGetImageFilename(wand) );
83  printf("\n");
84
85
86  printf("Note at this point, any image 'added' to wand will be prepended!\n");
87  printf("This special condition can be set by using either\n");
88  printf("just         MagickSetFirstIterator(w)\n");
89  printf("Or      MagickResetIterator(w); MagickPreviousImage(w);\n");
90  printf("The latter method being the cause of the current condition\n");
91  printf("\n");
92
93
94  printf("Directly loop though images backward using 'Last, while Previous'\n");
95  MagickSetLastIterator(wand);
96  while ( MagickPreviousImage(wand) != MagickFalse )
97    printf("image #%u \"%s\"\n",
98              (unsigned) MagickGetIteratorIndex(wand),
99              MagickGetImageFilename(wand) );
100  printf("\n");
101
102
103  printf("Loop through images using Indexes, in a weird flip-flop way!\n");
104  printf("Note that indexing using a negative number, indexes from end \n");
105  { ssize_t  i;
106    ssize_t  n = (ssize_t) MagickGetNumberImages(wand);
107
108    for ( i=0; i!=n;  i= (i>=0) ? -(i+1):-i ) {
109      (void) MagickSetIteratorIndex(wand,i);
110         /* Note that a return of MagickFalse by the above is not actually an
111          * error (no exception will be generated).  It just means that the
112          * index value used (positive or negative) is too large for the
113          * size of the current image list  (EG: range error: -n <= i < n )
114          * When it does happen, no change is made to the current image
115          */
116      printf("index %2d -> #%u \"%s\"\n", (int) i,
117                (unsigned) MagickGetIteratorIndex(wand),
118                MagickGetImageFilename(wand) );
119    }
120  }
121  printf("\n");
122
123
124  wand=DestroyMagickWand(wand);
125
126  MagickWandTerminus();
127}
128
129