snibgo's ImageMagick pages

Compiling the MagickWand demonstration programs

IM demo programs can be compiled with Cygwin tools.

The ImageMagick developers helpfully provide the source code of demonstration programs that use IM libraries. We can use these as templates for our own software. The first step is to figure out how to build binaries of these demonstration programs.

I assume that:

Those two pages are building blocks for this one.

On this computer, my username is Alan. You will need to adjust directory names for your own system.

On this page, I prefix bash commands with a dollar, "$". This is the usual bash prompt. Don't type it.

See also the official pages:

Compiling C programs

The environment variable %IMSRC% should point to IM's source code directory. See the Compiling IM with Cygwin page.

echo %IMSRC% 
c:\cygwin64\home\Alan\ImageMagick-6.9.9-40\ImageMagick 

The environment variable %IMDEV% should point to the installation directory we have used for our Cygwin build of IM.

echo %IMDEV% 
C:\cygwin64\home\Alan\imdevins69940\bin\ 

Go to the source code directory, and list the source files:

pushd %IMSRC%
cd www\source
dir *.c* 
 Volume in drive C is Acer
 Volume Serial Number is 90ED-7474

 Directory of c:\cygwin64\home\Alan\ImageMagick-6.9.9-40\ImageMagick\www\source

25/03/2018  18:19             9,953 analyze.c
25/03/2018  18:19             2,748 contrast.c
25/03/2018  18:19             1,570 core.c
25/03/2018  18:19             1,257 wand.c
               4 File(s)         15,528 bytes
               0 Dir(s)  169,514,569,728 bytes free

In bash, build the executables:

bash
$ cc -O3 -march=native -o wand wand.c `$IMSRC/wand/Wand-config --cflags --libs`
$ cc -O3 -march=native -o core core.c `$IMSRC/wand/Wand-config --cflags --libs`
$ cc -O3 -march=native -o contrast contrast.c `$IMSRC/wand/Wand-config --cflags --libs`
$ exit

Check the executables have been built:

dir /od *.exe 
 Volume in drive C is Acer
 Volume Serial Number is 90ED-7474

 Directory of c:\cygwin64\home\Alan\ImageMagick-6.9.9-40\ImageMagick\www\source

17/03/2023  01:57            71,457 wand.exe
17/03/2023  01:57            72,397 core.exe
17/03/2023  01:57            73,025 contrast.exe
               3 File(s)        216,879 bytes
               0 Dir(s)  169,514,098,688 bytes free

Move the executables to %IMDEV%:

copy /y *.exe %IMDEV%

Check the executables in %IMDEV%:

dir /od %IMDEV%*.exe 
 Volume in drive C is Acer
 Volume Serial Number is 90ED-7474

 Directory of C:\cygwin64\home\Alan\imdevins69940\bin

25/08/2018  02:02           170,438 hellowld.exe
25/08/2018  02:02           157,892 snibconv.exe
25/08/2018  02:02           467,131 swim.exe
25/08/2018  02:02           172,343 nlightest.exe
16/12/2018  09:47           162,797 wdighole.exe
09/01/2019  11:43           170,738 wrtext.exe
23/09/2019  14:05           166,005 tstmat.exe
03/04/2020  12:56           162,832 wand3ch.exe
08/04/2020  02:13           162,832 wandexcep.exe
18/02/2021  22:33           159,964 stripes.exe
24/02/2021  13:28           181,295 animate.exe
24/02/2021  13:28           181,811 compare.exe
24/02/2021  13:28           181,303 composite.exe
24/02/2021  13:28           181,295 conjure.exe
24/02/2021  13:28           181,295 convert.exe
24/02/2021  13:28           181,295 display.exe
24/02/2021  13:28           181,459 identify.exe
24/02/2021  13:28           181,291 import.exe
24/02/2021  13:28           181,295 mogrify.exe
24/02/2021  13:28           181,295 montage.exe
24/02/2021  13:28           181,291 stream.exe
16/03/2023  04:45            83,850 button.exe
17/03/2023  01:57            71,457 wand.exe
17/03/2023  01:57            72,397 core.exe
17/03/2023  01:57            73,025 contrast.exe
              25 File(s)      4,248,626 bytes
               0 Dir(s)  169,514,033,152 bytes free

Go back to where we came from:

popd

Check we can run the new executables in %IMDEV%:

%IMDEV%wand rose: cp_rosewand.png
cp_rosewand.png
%IMDEV%core rose: cp_rosecore.png
cp_rosecore.png
%IMDEV%contrast rose: cp_rosecont.png
cp_rosecont.png

Compiling C++ programs

We will build just one of the demonstration programs, button.cpp.

Go to the source code directory, and list the source file:

pushd %IMSRC%
cd Magick++\demo
dir button.c* 
 Volume in drive C is Acer
 Volume Serial Number is 90ED-7474

 Directory of c:\cygwin64\home\Alan\ImageMagick-6.9.9-40\ImageMagick\Magick++\demo

25/03/2018  18:19             1,965 button.cpp
               1 File(s)          1,965 bytes
               0 Dir(s)  169,514,033,152 bytes free

In bash, build the executable:

bash
$ c++ `Magick++-config --cxxflags --cppflags` -O3 -march=native -o button.exe button.cpp `Magick++-config --ldflags --libs`
$ exit

Move the executable to %IMDEV%:

copy /y button.exe %IMDEV%

Go back to where we came from:

popd

Check we can run the new executable in %IMDEV%:

del button_out.miff

%IMDEV%button
if ERRORLEVEL 1 goto error

%IM%convert button_out.miff cd_cpp_button_out.png
cd_cpp_button_out.png

Compiling under IM v7

For ImageMagick v7, the process is exactly as above, but using the directories %IM7SRC% and %IM7DEV%. For example, we build the C++ button program under v7:

pushd %IM7SRC%
cd Magick++\demo
bash
$ c++ `Magick++-config --cxxflags --cppflags` -O3 -march=native -o button.exe button.cpp `Magick++-config --ldflags --libs`
$ exit
copy /y button.exe %IM7DEV%
popd
del button_out.miff

%IM7DEV%button
if ERRORLEVEL 1 goto error

%IMG7%convert button_out.miff cd_cpp_button_out7.png
cd_cpp_button_out7.png

Historical methods

I first built the demonstration programs under IM v6.8.9-6, after building the IM system with the documentation option in configure. Among other actions, this placed a copy of the demonstration programs in the installation directory tree.

These days, I no longer build with the documentation option, so I no longer use the following methods.

The Cygwin name for the installation directory was ~/iminst32f. The Windows name for that directory was C:\cygwin64\home\Alan\iminst32f.

Historical: Compiling C programs

The installation directory contains demonstration programs.

C:\cygwin64\home\Alan\iminst32f>dir /s *.c
 Volume in drive C is Windows
 Volume Serial Number is 7CB7-18B6

 Directory of C:\cygwin64\home\Alan\iminst32f\share\doc\ImageMagick-6\www\source


31/07/2014  10:08             9,949 analyze.c
31/07/2014  10:08             2,748 contrast.c
31/07/2014  10:08             1,570 core.c
31/07/2014  10:08             1,257 wand.c
               4 File(s)         15,524 bytes

     Total Files Listed:
               4 File(s)         15,524 bytes
               0 Dir(s)  79,314,272,256 bytes free

analyze.c isn't a complete program.

Within bash, create the environment variable PKG_CONFIG_PATH, then build using the appropriate library: MagickWand or MagickCore.

C:
cd \cygwin64\home\Alan\iminst32f\share\doc\ImageMagick-6\www\source
bash
$ export PKG_CONFIG_PATH=~/iminst32f/lib/pkgconfig
$ cc -o wand wand.c `pkg-config --cflags --libs MagickWand`
$ cc -o core core.c `pkg-config --cflags --libs MagickWand`
$ cc -o contrast contrast.c `pkg-config --cflags --libs MagickWand`

Instead of cc, the compiler can be invoked with cpp or c++.

ASIDE: For some days, I was led astray by the official MagickWand C API page, which says we should compile it like this:

$ cc -o wand `pkg-config --cflags --libs MagickWand` wand.c

This compiles (cc1.exe) and assembles (as.exe), but the linker (collect2.exe) fails.

Running the cc command with options -v -Wl,-t, I discovered that it runs the linker in a command that lists the compiled object file after the libraries, resulting in many "undefined reference" errors.

The cure is to run the cc command with the source file wand.c before the pkg-config. This results in a collect2 command with the object file before the libraries, so references can be resolved.

I will ask for the official page to be modified.

An alternative kludge is to note that adding -Wl,-ABC to the command line puts -ABC immediately after the object file, so we can explicitly add the Magick libraries:

$ cc -o wand `~/iminst32f/bin/Wand-config --cflags --libs` wand.c -v -Wl,-t -Wl,-lMagickWand-6.Q32HDRI -Wl,-lMagickCore-6.Q32HDRI

ANOTHER ASIDE: An alternative to ...

`pkg-config --cflags --libs MagickWand`

... is ...

`~/iminst32f/bin/Wand-config --cflags --libs`

Compiling contrast.c, we get the warning that "QuantumScale" is redefined, in both contrast.c and magick-type.h.

We now have compiled programs, .exe files. But they won't run:

$ ./wand.exe
/home/Alan/iminst32f/share/doc/ImageMagick-6/www/source/wand.exe: error while loading shared libraries: cygMagickWand-6.Q32HDRI-2.dll: cannot open shared object file: No such file or directory

The required .dll is in ~/iminstf32/bin, which also contains convert.exe etc. So copy the .exe files to there.

Now, from any console, we can run the programs, from either Cygwin bash or Windows cmd:

F:\web\im>C:\cygwin64\home\Alan\iminst32f\bin\wand.exe
Usage: /home/Alan/iminst32f/bin/wand image thumbnail

That directory isn't in my path, so the program clearly searches for the dll in the program's directory (perhaps among other places).

set NEW_IM=C:\cygwin64\home\Alan\iminst32f\bin\

%NEW_IM%wand wizard: cp_wizwand.png
cp_wizwand.png
%NEW_IM%core wizard: cp_wizcore.png
cp_wizcore.png
%NEW_IM%contrast wizard: cp_wizcont.png
cp_wizcont.png

Historical: Compiling C++ programs

There are C++ demonstration programs in C:\cygwin64\home\Alan\ImageMagick-6.8.9-6\Magick++\demo, aka ~/ImageMagick-6.8.9-6/Magick++/demo.

The directory ~/ImageMagick-6.8.9-6 was created and populated by unpacking the Unix distribution of IM. See Compiling IM with Cygwin: Building the Unix source.

cd \cygwin64\home\Alan\ImageMagick-6.8.9-6\Magick++\demo
dir *.cpp
05/09/2009  22:47             1,934 analyze.cpp
05/01/2010  15:04             1,965 button.cpp
30/05/2010  02:16            15,366 demo.cpp
05/09/2009  22:47             1,280 detrans.cpp
05/09/2009  22:47             1,392 flip.cpp
27/05/2010  21:51             2,585 gravity.cpp
01/08/2014  23:42               348 hellosn.cpp
16/12/2013  00:58             5,418 piddle.cpp
03/02/2011  03:08             2,985 shapes.cpp
30/05/2010  02:16             5,668 zoom.cpp

We invoke the compiler with cpp or c++ instead of cc:

bash
$ cd ~/ImageMagick-6.8.9-6/Magick++/demo
$ cpp -o analyze.exe analyze.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o button.exe button.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o demo.exe demo.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o detrans.exe detrans.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o flip.exe flip.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o gravity.exe gravity.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o hellosn.exe hellosn.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o piddle.exe piddle.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o shapes.exe shapes.cpp `pkg-config --cflags --libs ImageMagick++`
$ cpp -o zoom.exe zoom.cpp `pkg-config --cflags --libs ImageMagick++`

$ mv *.exe ~/iminst32f/bin/
$ exit

We test one of the compiled programs:

%NEW_IM%button

%IM%convert button_out.miff cp_button_out.png
cp_button_out.png

An alternative compilation mechanism (see official page Magick++ C++ API), for an arbitrary CPP program in an arbitrary directory, is:

F:
cd \cprogs\imwand
bash
$ export PKG_CONFIG_PATH=~/iminst32f/lib/pkgconfig
$ c++ `Magick++-config --cxxflags --cppflags` -O2 -o new_prog new_prog.cpp `Magick++-config --ldflags --libs`
$ mv *.exe ~/iminst32f/bin/

All images on this page were created by the commands shown.

My usual version of IM v6 is:

%IM%identify -version
Version: ImageMagick 6.9.12-72 Q16-HDRI x64 55ae040:20221230 https://legacy.imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Visual C++: 193431937
Features: Cipher DPC HDRI OpenCL 
Delegates (built-in): bzlib cairo freetype gslib heic jng jp2 jpeg lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zlib

However, my development version of IM v6 is:

%IMDEV%identify -version
Version: ImageMagick 7.1.0-20 Q32-HDRI x86_64 2021-12-29 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5) 
Delegates (built-in): bzlib cairo fontconfig fpx freetype jbig jng jpeg lcms ltdl lzma pangocairo png raqm rsvg tiff webp wmf x xml zip zlib
Compiler: gcc (11.2)

And my development version of IM v7 is:

%IM7DEV%magick -version
Version: ImageMagick 7.1.0-20 Q32-HDRI x86_64 2021-12-29 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(4.5) 
Delegates (built-in): bzlib cairo fontconfig fpx freetype jbig jng jpeg lcms ltdl lzma pangocairo png raqm rsvg tiff webp wmf x xml zip zlib
Compiler: gcc (11.2)

Source file for this web page is compdemo.h1. To re-create this web page, run "procH1 compdemo".


This page is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v2.0 3-Oct-2016.

Page created 17-Mar-2023 01:57:23.

Copyright © 2023 Alan Gibson.