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.3-7\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\imdevins6937\bin\ 

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

pushd %IMSRC%
cd www\source
dir *.c* 
 Volume in drive C is Windows
 Volume Serial Number is 7CB7-18B6

 Directory of c:\cygwin64\home\Alan\ImageMagick-6.9.3-7\ImageMagick\www\source

06/03/2016  12:27             9,949 analyze.c
06/03/2016  12:27             2,748 contrast.c
06/03/2016  12:27             1,570 core.c
06/03/2016  12:27             1,257 wand.c
               4 File(s)         15,524 bytes
               0 Dir(s)  72,607,105,024 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 Windows
 Volume Serial Number is 7CB7-18B6

 Directory of c:\cygwin64\home\Alan\ImageMagick-6.9.3-7\ImageMagick\www\source

10/10/2016  08:30            69,528 wand.exe
10/10/2016  08:30            70,464 core.exe
10/10/2016  08:30            71,608 contrast.exe
               3 File(s)        211,600 bytes
               0 Dir(s)  72,607,105,024 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 Windows
 Volume Serial Number is 7CB7-18B6

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

03/04/2016  22:21           356,517 swimtest.exe
03/04/2016  22:21           123,203 swimc2c.exe
05/04/2016  20:39            77,456 svgex.exe
15/06/2016  02:01            72,944 wandLine.exe
23/06/2016  10:55            68,848 rdFile.exe
30/07/2016  12:40            71,141 simprdwr.exe
30/09/2016  14:53            79,395 hellowld.exe
30/09/2016  14:53            67,700 snibconv.exe
30/09/2016  14:53           352,023 swim.exe
02/10/2016  14:21            86,417 animate.exe
02/10/2016  14:21            87,445 compare.exe
02/10/2016  14:21            86,425 composite.exe
02/10/2016  14:21            86,417 conjure.exe
02/10/2016  14:21            86,417 convert.exe
02/10/2016  14:21            86,417 display.exe
02/10/2016  14:21            86,581 identify.exe
02/10/2016  14:21            86,413 import.exe
02/10/2016  14:21            86,417 mogrify.exe
02/10/2016  14:21            86,417 montage.exe
02/10/2016  14:21            86,413 stream.exe
10/10/2016  08:18            79,745 button.exe
10/10/2016  08:30            69,528 wand.exe
10/10/2016  08:30            70,464 core.exe
10/10/2016  08:30            71,608 contrast.exe
              24 File(s)      2,512,351 bytes
               0 Dir(s)  72,607,105,024 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 Windows
 Volume Serial Number is 7CB7-18B6

 Directory of c:\cygwin64\home\Alan\ImageMagick-6.9.3-7\ImageMagick\Magick++\demo

06/03/2016  12:27             1,965 button.cpp
               1 File(s)          1,965 bytes
               0 Dir(s)  72,607,105,024 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

%IM%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 is:

%IM%identify -version
Version: ImageMagick 6.9.5-3 Q16 x86 2016-07-22 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

However, my development version of IM is:

%IMDEV%identify -version
Version: ImageMagick 6.9.3-7 Q32 x86_64 2016-09-30 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP 
Delegates (built-in): bzlib cairo fftw fontconfig freetype fpx jbig jng jpeg lcms ltdl lzma pangocairo png rsvg tiff webp wmf x xml zlib

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 10-Oct-2016 07:30:16.

Copyright © 2016 Alan Gibson.