Commit ad23fe94d for imagemagick.org
commit ad23fe94d4650b9d2e5277981f65890d94cc902c
Author: Cristy <urban-warrior@imagemagick.org>
Date: Fri Nov 27 17:17:18 2026 -0500
update api examples with all the latest recommended development practices
diff --git a/api_examples/cli_operators.c b/api_examples/cli_operators.c
index 0515c270a..70b7392c6 100644
--- a/api_examples/cli_operators.c
+++ b/api_examples/cli_operators.c
@@ -8,20 +8,26 @@
Compile with ImageMagick-develop installed...
- gcc -lMagickWand -lMagickCore cli_operators.c -o cli_operators
+ gcc cli_operators.c -o cli_operators \
+ `pkg-config --cflags --libs MagickWand`
+
+ (The source file must come BEFORE the libraries on the command line,
+ otherwise linkers using --as-needed, the default on many distros,
+ may drop the libraries and fail with undefined references.)
Compile and run directly from Source Directory...
IM_PROG=api_examples/cli_operators
- gcc -I`pwd` -LMagickWand/.libs -LMagickCore/.libs \
- -lMagickWand -lMagickCore $IM_PROG.c -o $IM_PROG
+ gcc -I`pwd` $IM_PROG.c -o $IM_PROG \
+ -LMagickWand/.libs -LMagickCore/.libs -lMagickWand -lMagickCore
sh ./magick.sh $IM_PROG
-
+ Note: the final "-write show:" needs a working display (X11 etc).
+ Replace it with e.g. "-write", "result.png" to run headless.
*/
#include <stdio.h>
-#include "MagickCore/studio.h"
+#include <stdlib.h>
#include "MagickWand/MagickWand.h"
#include "MagickWand/operation.h"
@@ -30,24 +36,40 @@ int main(int argc, char **argv)
MagickCLI
*cli_wand;
+ int
+ status;
+
+ (void) argc;
+
MagickCoreGenesis(argv[0],MagickFalse);
cli_wand = AcquireMagickCLI((ImageInfo *) NULL,(ExceptionInfo *) NULL);
+ if (cli_wand == (MagickCLI *) NULL)
+ {
+ fprintf(stderr, "Unable to acquire MagickCLI wand\n");
+ MagickCoreTerminus();
+ return(EXIT_FAILURE);
+ }
- CLIOption (cli_wand, "-size", "100x100");
- CLIOption (cli_wand, "-read", "xc:red");
- CLIOption (cli_wand, "(", NULL);
- CLIOption (cli_wand, "-read", "rose:");
- CLIOption (cli_wand, "-rotate", "-90", NULL);
- CLIOption (cli_wand, ")", NULL);
- CLIOption (cli_wand, "+append", NULL, NULL);
- CLIOption (cli_wand, "-write", "show:", NULL);
+ CLIOption (cli_wand, "-size", "100x100", NULL);
+ CLIOption (cli_wand, "-read", "xc:red", NULL);
+ CLIOption (cli_wand, "(", NULL, NULL);
+ CLIOption (cli_wand, "-read", "rose:", NULL);
+ CLIOption (cli_wand, "-rotate", "-90", NULL);
+ CLIOption (cli_wand, ")", NULL, NULL);
+ CLIOption (cli_wand, "+append", NULL, NULL);
+ CLIOption (cli_wand, "-write", "show:", NULL);
- /* Note use of 'True' to report all exceptions - including fatals */
+ /* Note use of MagickTrue to report all exceptions - including fatals */
+ status = EXIT_SUCCESS;
if ( CLICatchException(cli_wand,MagickTrue) != MagickFalse )
- fprintf(stderr, "Major Error Detected\n");
+ {
+ fprintf(stderr, "Major Error Detected\n");
+ status = EXIT_FAILURE;
+ }
cli_wand = DestroyMagickCLI(cli_wand);
MagickCoreTerminus();
+ return(status);
}
diff --git a/api_examples/cli_process.c b/api_examples/cli_process.c
index 54f7bd067..13d499c17 100644
--- a/api_examples/cli_process.c
+++ b/api_examples/cli_process.c
@@ -4,22 +4,26 @@
actually splits up the argument array into separate operation
group calls.
+ Compile with ImageMagick 7 development files installed...
- Compile with ImageMagick-develop installed...
-
- gcc -lMagickWand -lMagickCore cli_process.c -o cli_process
+ gcc cli_process_v2.c -o cli_process_v2 \
+ $(pkg-config --cflags --libs MagickWand)
Compile and run directly from Source Directory...
- IM_PROG=api_examples/cli_process
- gcc -I`pwd` -LMagickWand/.libs -LMagickCore/.libs \
- -lMagickWand -lMagickCore $IM_PROG.c -o $IM_PROG
+ IM_PROG=api_examples/cli_process_v2
+ gcc -I`pwd` $IM_PROG.c -o $IM_PROG \
+ -LMagickWand/.libs -LMagickCore/.libs \
+ -lMagickWand -lMagickCore
sh ./magick.sh $IM_PROG
+ Requires a display (or another delegate for "show:") to run
+ successfully; on a headless machine replace "show:" with an output
+ filename such as "out.png".
*/
#include <stdio.h>
-#include "MagickCore/studio.h"
+#include <stdlib.h>
#include "MagickWand/MagickWand.h"
int main(int argc, char **argv)
@@ -27,24 +31,37 @@ int main(int argc, char **argv)
MagickCLI
*cli_wand;
- int arg_count;
+ int
+ arg_count,
+ status = EXIT_SUCCESS;
+
char *args[] = { "-size", "100x100", "xc:red",
"(", "rose:", "-rotate", "-90", ")",
- "+append", "show:", NULL };
-
- for(arg_count = 0; args[arg_count] != (char *) NULL; arg_count++);
+ "+append", "show:" };
+ (void) argc;
+ arg_count = (int) (sizeof(args)/sizeof(args[0]));
MagickCoreGenesis(argv[0],MagickFalse);
+
cli_wand = AcquireMagickCLI((ImageInfo *) NULL,(ExceptionInfo *) NULL);
+ if (cli_wand == (MagickCLI *) NULL)
+ {
+ fprintf(stderr,"Unable to allocate MagickCLI\n");
+ MagickCoreTerminus();
+ return(EXIT_FAILURE);
+ }
ProcessCommandOptions(cli_wand, arg_count, args, 0);
/* Note use of 'True' to report all exceptions - including non-fatals */
if ( CLICatchException(cli_wand,MagickTrue) != MagickFalse )
- fprintf(stderr, "Major Error Detected\n");
-
+ {
+ fprintf(stderr, "Major Error Detected\n");
+ status = EXIT_FAILURE;
+ }
cli_wand = DestroyMagickCLI(cli_wand);
MagickCoreTerminus();
+ return(status);
}
diff --git a/api_examples/wand.c b/api_examples/wand.c
index c05596d00..46f7e0ec4 100644
--- a/api_examples/wand.c
+++ b/api_examples/wand.c
@@ -8,25 +8,31 @@
Compile with ImageMagick-develop installed...
- gcc -lMagickWand -lMagickCore wand.c -o wand
+ gcc wand_patched.c -o wand `pkg-config --cflags --libs MagickWand`
+
+ (or, without pkg-config; note libraries must come AFTER the source file
+ for linkers that resolve symbols left to right)
+
+ gcc wand_patched.c -o wand -lMagickWand -lMagickCore
Compile and run directly from Source Directory...
- IM_PROG=api_examples/wand
- gcc -I`pwd` -LMagickWand/.libs -LMagickCore/.libs \
- -lMagickWand -lMagickCore $IM_PROG.c -o $IM_PROG
+ IM_PROG=api_examples/wand_patched
+ gcc -I`pwd` $IM_PROG.c -o $IM_PROG \
+ -LMagickWand/.libs -LMagickCore/.libs -lMagickWand -lMagickCore
sh ./magick.sh $IM_PROG
-
*/
#include <stdio.h>
+#include <stdlib.h>
#include "MagickWand/MagickWand.h"
/* Simplify the exception handling
* technically we should abort the program if
* severity >= ErrorException
+ * (main() below does so by jumping to its cleanup label on any failure)
*/
-void ThrowWandException(MagickWand *wand)
+static void ThrowWandException(MagickWand *wand)
{ char
*description;
@@ -34,34 +40,55 @@ void ThrowWandException(MagickWand *wand)
severity;
description=MagickGetException(wand,&severity);
- (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description);
- description=(char *) MagickRelinquishMemory(description);
+ (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),
+ description != (char *) NULL ? description : "unknown error");
+ if (description != (char *) NULL)
+ description=(char *) MagickRelinquishMemory(description);
}
/* useful function especially after appending two wands together */
-#define SwapWands(a,b) { MagickWand *tmp=a; a=b; b=tmp; }
+#define SwapWands(a,b) do { MagickWand *tmp=a; a=b; b=tmp; } while (0)
int main(int argc, char *argv[])
{
MagickWand
- *red, /* red image wand */
- *rose, /* rose image wand */
- *output; /* the appended output image */
+ *red=NULL, /* red image wand */
+ *rose=NULL, /* rose image wand */
+ *output=NULL; /* the appended output image */
PixelWand
- *color;
+ *color=NULL;
MagickBooleanType
status;
+ int
+ exit_code=EXIT_FAILURE;
+
+ (void) argc;
+ (void) argv;
+
MagickWandGenesis();
/* read in the red image */
red = NewMagickWand();
- MagickSetSize(red,100,100);
+ if (red == NULL)
+ {
+ (void) fprintf(stderr,"unable to allocate wand\n");
+ goto cleanup;
+ }
+ status = MagickSetSize(red,100,100);
+ if (status == MagickFalse)
+ {
+ ThrowWandException(red);
+ goto cleanup;
+ }
status = MagickReadImage(red, "xc:red" );
if (status == MagickFalse)
- ThrowWandException(red);
+ {
+ ThrowWandException(red);
+ goto cleanup;
+ }
/* NOTE ABOUT MagickReadImage()
* Unless the wand is empty set the first/last iterator to determine
* if the read image(s) are to be prepend/append into that wand image
@@ -72,21 +99,47 @@ int main(int argc, char *argv[])
/* read in the rose image */
rose = NewMagickWand();
+ if (rose == NULL)
+ {
+ (void) fprintf(stderr,"unable to allocate wand\n");
+ goto cleanup;
+ }
status = MagickReadImage(rose, "rose:" );
if (status == MagickFalse)
- ThrowWandException(rose);
+ {
+ ThrowWandException(rose);
+ goto cleanup;
+ }
/* rotate the rose image - one image only */
color=NewPixelWand();
- PixelSetColor(color, "white");
+ if (color == NULL)
+ {
+ (void) fprintf(stderr,"unable to allocate pixel wand\n");
+ goto cleanup;
+ }
+ status = PixelSetColor(color, "white");
+ if (status == MagickFalse)
+ {
+ (void) fprintf(stderr,"unable to set pixel color\n");
+ goto cleanup;
+ }
status = MagickRotateImage(rose,color,-90.0);
if (status == MagickFalse)
- ThrowWandException(rose);
+ {
+ ThrowWandException(rose);
+ goto cleanup;
+ }
color = DestroyPixelWand(color);
/* append rose image into the red image wand */
MagickSetLastIterator(red);
- MagickAddImage(red,rose);
+ status = MagickAddImage(red,rose);
+ if (status == MagickFalse)
+ {
+ ThrowWandException(red);
+ goto cleanup;
+ }
rose = DestroyMagickWand(rose); /* finished with 'rose' wand */
/* NOTE ABOUT MagickAddImage()
*
@@ -100,6 +153,11 @@ int main(int argc, char *argv[])
/* append all images together to create the output wand */
MagickSetFirstIterator(red);
output = MagickAppendImages(red,MagickFalse);
+ if (output == NULL)
+ {
+ ThrowWandException(red);
+ goto cleanup;
+ }
red = DestroyMagickWand(red); /* finished with 'red' wand */
/* NOTE ABOUT MagickAppendImages()
*
@@ -115,11 +173,26 @@ int main(int argc, char *argv[])
/* Final output */
status = MagickWriteImage(output,"show:");
if (status == MagickFalse)
- ThrowWandException(output);
-
- output = DestroyMagickWand(output);
+ {
+ ThrowWandException(output);
+ goto cleanup;
+ }
+
+ exit_code=EXIT_SUCCESS;
+
+cleanup:
+ /* every pointer is NULL or valid here, so this is safe on all paths */
+ if (output != NULL)
+ output = DestroyMagickWand(output);
+ if (rose != NULL)
+ rose = DestroyMagickWand(rose);
+ if (red != NULL)
+ red = DestroyMagickWand(red);
+ if (color != NULL)
+ color = DestroyPixelWand(color);
MagickWandTerminus();
+ return exit_code;
}
/*
@@ -135,4 +208,3 @@ int main(int argc, char *argv[])
* operator (like MagickRotateImage()) to all the images in a wand you must
* iterate over all the images yourself.
*/
-