cloud goes social

Icon

computer, travel, movies, music, cuisine and more

ImageMagick on iPhone (with jpeg + png)

UPDATE: iOS4.3 and Xcode4

PLEASE READ THE UPDATE

I’m working on some iPhone applications, which need some image filtering. For now, I won’t say more! Stay tuned for the news when it comes out!

Long story short, I soon found the need (ant the challenge was a good one) to get ImageMagick static libraries so that I can use it on the iPhone. Not only that, but with jpeg and png support. That’s what I need.

After a fast search through the net, I couldn’t find a single _GOOD_ post on how to do that. Just some bits and bytes – Nothing complete.

I soon set on a mission to get ImageMagick to work on the iPhone, as a library. And here’s the result of my quest (around 1 day of search, trials and errors, and at last some light!)

Setting

I’m running MacOSX 10.5.7, with the latest MacPorts installed. If you don’t, get it! I have many ports installed, the ones I “think” are important are:

  • jpeg
  • libpng

Also, I’m testing everything with the latest beta of the iPhoneOS 3.0 – It should be working also for 2.2.1 and the final 3.0 when it comes out.

The Script

I now provide you with a shell script that should do the whole trick of creating the static libraries needed in Xcode for iPhone development.

Just download it, give it run permission (chmod +x ./compile_im.sh), and launch it. It will ask for your password because it needs to copy a file that is not owned by you, the user.

Before complaining that it’s not working, open it, and look at the required directory structure for it to work.
In particular you will need:

  • a Desktop folder named cross_compile
  • ImageMagick source: ImageMagick
  • jpeg source: jpeg
  • – to which I’ve added the MacPorts patches

  • libpng source: libpng
  • Untar ImageMagick source and place it into ~/Desktop/cross_compile/i_m
  • Untar/Unzip jpeg/libpng source and place it into: ~/Desktop/cross_compile/i_m/IMDelegates/jpeg-6b and ~/Desktop/cross_compile/i_m/IMDelegates/libpng-1.2.37

Now you can run the script :)

Result

The result will be stored under: ~/Desktop/tmp_target. In particular you can find all the header files needed while developing your application under: ~/Desktop/tmp_target/im_libs/includes (divided by library). While all the static libraries are inside ~/Desktop/tmp_target/im_libs/:

  • libjpeg.a
  • libpng.a
  • libMagickCore.a
  • libMagickWand.a

Please note that the script joins both the i386 and the arm build inside one single .a static library to ease up developing (for testing both on the Simulator and on the device itself. When finishing up the application you may want to put only the arm library to make the overall application lighter.

XCode configuration

Step 1

To work with your libraries you need to put some easy adjustments to your Project in XCode. I’ve found that the best thing to do, especially in a shared environment with multiple developers, to just put everything inside the XCode project. To do so do:

  • Project->Add To Project->Select all the .a libraries->Click ‘ADD’
  • Project->Add To Project->Select the folders inside the ~/Desktop/tmp_target/im_libs/include->Click ‘ADD’

Step 2

Click on Project->Edit Project Settings and edit (in the Build:

  • Other Linker Flags: -lMagickCore -lMagickWand -lz -lbz2 -ljpeg -lpng
  • Header Search Paths: $(SRCROOT) – make it Recursive
  • Library Search Paths: $(SRCROOT) – make it Recursive
  • On the lower left click on the small-wheel and select: Add User-Defined Setting
    • Key: OTHER_CFLAGS
    • Value: -Dmacinsoth=1

This should be enough to make you start.

For the Lazy

If you’re lazy, don’t manage or for whatever other reason you can just download the whole compiled package

Last note

In case the script doesn’t work, you have questions/suggestions/support please don’t hesitate to contact me, and in case you need help with the script please send me the FULL output in a file.
./compile_im.sh > file_to_send.txt

Category: Code, tips & howtos

Tagged: , , ,

40 Responses

  1. curtis says:

    Hi Cloud,

    Thanks for the great efforts! Besides the ImageMagick library, I am wondering if you would consider to build the ImageMagick command line utility for iOS? Sometimes I would prefer to simply give it a system() call for my simple image processing task, instead of setting up the Magick environment. Thanks!

    curtis.

  2. Rodolfo says:

    Does the iphone library include support for ICC profiles?

  3. greta says:

    Hi cloud. I’m trying to use imageMagick on iPhone, and it works fine but there are some things i cant do. for example I cant use MagickAnnotateImage(…) because i get this error:
    iMagickProject: UnableToOpenConfigureFile `type.xml’ @ warning/configure.c/GetConfigureOptions/589.
    iMagickProject: DelegateLibrarySupportNotBuiltIn `(null)’ (Freetype) @ warning/annotate.c/RenderFreetype/1449.
    iMagickProject: UnableToOpenConfigureFile `delegates.xml’ @ warning/configure.c/GetConfigureOptions/589.
    iMagickProject: `%s’ (%d) “gs” -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 “-sDEVICE=pnmraw” -dTextAlphaBits=4 -dGraphicsAlphaBits=4 “-r72x72″ -g61x63 “-sOutputFile=/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-japgGpaU” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-jc6PvfhY” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-dxXdkq05″ @ error/utility.c/SystemCommand/1982.
    iMagickProject: `%s’ (%d) “gs” -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 “-sDEVICE=pnmraw” -dTextAlphaBits=4 -dGraphicsAlphaBits=4 “-r72x72″ -g61x63 “-sOutputFile=/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-japgGpaU” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-jc6PvfhY” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-dxXdkq05″ -c showpage @ error/utility.c/SystemCommand/1982.
    iMagickProject: PostscriptDelegateFailed `/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-z2G2u74M’: No such file or directory @ error/ps.c/ReadPSImage/781.
    iMagickProject: DelegateLibrarySupportNotBuiltIn `(null)’ (Freetype) @ warning/annotate.c/RenderFreetype/1449.
    iMagickProject: `%s’ (%d) “gs” -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 “-sDEVICE=pnmraw” -dTextAlphaBits=4 -dGraphicsAlphaBits=4 “-r72x72″ -g61x63 “-sOutputFile=/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-M4nAX64O” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-KXgJhLpI” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-sqv9Qv9Y” @ error/utility.c/SystemCommand/1982.
    iMagickProject: `%s’ (%d) “gs” -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 “-sDEVICE=pnmraw” -dTextAlphaBits=4 -dGraphicsAlphaBits=4 “-r72x72″ -g61x63 “-sOutputFile=/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-M4nAX64O” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-KXgJhLpI” “-f/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-sqv9Qv9Y” -c showpage @ error/utility.c/SystemCommand/1982.
    iMagickProject: PostscriptDelegateFailed `/private/var/mobile/Applications/35952822-51A3-4B19-A696-050236E9E47F/tmp/magick-d8GEOecI’: No such file or directory @ error/ps.c/ReadPSImage/781.

    Can you help please?

    Thanks i advance, Greta

  4. Eugene says:

    Hi, Cloud!
    At the first, thanks for your post. It’s very useful.

    But I have a problem with use this. Could you please help me to resolve this issue?
    If I use your compiled library my project works perfectly.
    But when I build a library myself and insert it into my project, the function
    MagickReadImageBlob(i_wand, [dataObject bytes], [dataObject length])
    returns false always (dataObject is a NSDate that contains jpeg image).

    The compilation is finished successfully, without any errors. BUT my libMagickCore has a size 9,8 MB versus your 10,0 MB.

    Do you have any thoughts about this?
    Thank you.

    P.S. I have compiled it on Mac OS 10.5.8

    • Cloud says:

      Hi,
      I do not know what that problem could be, but apparently something didn’t compile properly. Two things to do:

      1) Try to have dataObject to be something with a png rather than a jpeg and see if it works, this would mean that jpeg didn’t compile correctly
      2) when compiling output everything to a file and send me the file, so I can have a look at it, to do this:

      ./compile_im.sh > ~/Desktop/compilation_output

  5. hytgbn says:

    Hi,
    Thanks for your post :)

    I compiled well and Imagemagick worked well.

    After I update my leopard to snow leopard,
    and update my xcode to 3.2

    when I compile my project for simulator

    it gives me library error

    ———————
    “_GOMP_critical_name_start”, referenced from:
    “_GOMP_parallel_loop_dynamic_start”, referenced from:
    “_GOMP_loop_dynamic_start”, referenced from:
    and similar errors…

    ———————

    Do you know why these happens?
    It works on device well! :O

    Is there some library I missed?
    or is there some check point when I migrate from 3.1 to 3.2 xcode?

    please help me, thank you

  6. loosy says:

    hi everyone,
    I want to use imageMagick for taking pdf cover thumbnails?

    Any Suggestion would be greatly appreciated.

    Thanks.

  7. Laurent says:

    Hi Everyone

    Has anyone been able to build it with Snow Leopard?
    I am trying something similar using Tesseract. I can build the library on the ARM architecture, include it in my project but impossible to get it to work on the Simulator. I get the error “ld: warning: in /Developer/photoStrip/lib/iPhone/libtesseract_full.a, file is not of required architecture” when I build the App. I also tried with the setup (x86_64) Christophe Verdier recommended.

    My Setup is Snow Leopard 10.6.2, SDK 3.1, XCode 3.2.

    Any help would be greatly appreciated

    Thanks

  8. update: the modified script for snow…
    worked for me at least :)

    # Author: Claudio Marforio
    # e-mail: marforio@gmail.com
    # date: 9.06.2009

    # Script to make static libraries (jpeg + png) and ImageMagick
    # the libraries will be conbined into i386+arm.a static libraries
    # to be used inside an XCODE project for iPhone development

    # The directory structure has to be:
    # ~/Desktop/cross_compile/i_m/ <- ImageMagick top directory
    # |-IMDelegataes/ <- Some delegates, in particular jpeg + png
    # |-jpeg-6b/ <- Patched jpeg6b
    # |-libpng-1.2.37 <- png lib – no need to patch it
    # |-tiff-3.8.2 <- tiff lib – no need to patch it
    # |-… <- we don't care what's here! :)

    # If you don't have this Directory structure you can either create it or try change around the script

    #!/bin/bash

    # Set this to the top directory of ImageMagick source:
    IM_DIR=$(pwd)/cross_compile/i_m
    JPEG_DIR=$IM_DIR/IMDelegates/jpeg-6b
    PNG_DIR=$IM_DIR/IMDelegates/libpng-1.2.40
    TIFF_DIR=$IM_DIR/IMDelegates/tiff-3.9.1
    ARCH_SIM="i386"
    GCC_VERSION="4.0.1"

    # Set this to where you want the libraries to be placed (if dir is not present it will be created):
    TARGET_LIB_DIR=$(pwd)/tmp_target
    LIB_DIR=$TARGET_LIB_DIR/im_libs
    JPEG_LIB_DIR=$TARGET_LIB_DIR/libjpeg
    PNG_LIB_DIR=$TARGET_LIB_DIR/libpng
    TIFF_LIB_DIR=$TARGET_LIB_DIR/libtiff
    IM_LIB_DIR=$TARGET_LIB_DIR/imagemagick

    # Set the build directories
    mkdir -p $TARGET_LIB_DIR
    mkdir -p $LIB_DIR/include/jpeg
    mkdir -p $LIB_DIR/include/magick
    mkdir -p $LIB_DIR/include/png
    mkdir -p $LIB_DIR/include/tiff
    mkdir -p $LIB_DIR/include/wand
    mkdir -p $LIB_DIR/jpeg_arm_dylib
    mkdir -p $LIB_DIR/png_arm_dylib
    mkdir -p $LIB_DIR/tiff_arm_dylib
    mkdir -p $JPEG_LIB_DIR/lib #we don't need bin/ and share/
    mkdir -p $JPEG_LIB_DIR/include
    mkdir -p $PNG_LIB_DIR #libpng manages to create subdirectories by itself with make install
    mkdir -p $TIFF_LIB_DIR #libtiff manages to create subdirectories by itself with make install

    # General folders where you have the iPhone compiler + tools
    export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
    # Change this to match for which version of the SDK you want to compile – you can change the number for the version
    export SDKROOT=$DEVROOT/SDKs/iPhoneOS3.1.sdk

    ############ HACK #################################
    # ImageMagick requires this header, that doesn't exist for the iPhone
    # Just copying it make things compile/work (more testing needed)
    #sudo cp /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/crt_externs.h \
    # $SDKROOT/usr/include/crt_externs.h
    ############ END – HACK #############################

    function png()
    {
    #######################################################
    ############ PNG ###########################
    #######################################################

    cd $PNG_DIR

    LIBPATH_png=libpng12.a

    #######################################################
    ############ ARM ###########################
    #######################################################

    U_CC=$CC
    U_CFLAGS=$CFLAGS
    U_LD=$LD
    U_LDFLAGS=$LDFLAGS
    U_CPP=$CPP
    U_CPPFLAGS=$CPPFLAGS

    export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
    export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -L$SDKROOT/usr/lib/ -O3"
    export CPP="/usr/bin/cpp $CPPFLAGS"
    export LDFLAGS="-L$SDKROOT/usr/lib/"

    ./configure prefix=$PNG_LIB_DIR –enable-shared –enable-static \
    CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin

    make
    make install

    # cp the static + shared library
    cp $PNG_LIB_DIR/lib/$LIBPATH_png $LIB_DIR/libpng.a.arm
    cp $PNG_LIB_DIR/lib/libpng12.0.dylib $LIB_DIR/png_arm_dylib/libpng.dylib

    make distclean

    #######################################################
    ############ INTEL ###########################
    #######################################################

    # Use default environment
    export CC=$U_CC
    export CFLAGS="-arch $ARCH_SIM -O3"
    export LD=$U_LD
    export LDFLAGS=$U_LDFLAGS
    export CPP=$U_CPP
    export CPPFLAGS=$U_CPPFLAGS

    LIBPATH_png=libpng12.a

    ./configure prefix=$PNG_LIB_DIR –enable-shared –enable-static –host=i686-apple-darwin9

    make
    make install

    # cp the static library
    cp $PNG_LIB_DIR/lib/$LIBPATH_png $LIB_DIR/libpng.a.i386
    # cp the include/* files
    cp $PNG_LIB_DIR/include/libpng12/* $LIB_DIR/include/png/

    make distclean

    # combine the static libraries for i386 and arm
    $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/libpng.a.arm -arch $ARCH_SIM $LIB_DIR/libpng.a.i386 -create -output $LIB_DIR/libpng.a
    }
    function jpeg()
    {
    #######################################################
    ############ JPEG ###########################
    #######################################################

    cd $JPEG_DIR

    LIBPATH_jpeg=libjpeg.a
    LIBNAME_jpeg=`basename $LIBPATH_jpeg`

    #######################################################
    ############ ARM ###########################
    #######################################################

    U_CC=$CC
    U_CFLAGS=$CFLAGS
    U_LD=$LD
    U_LDFLAGS=$LDFLAGS
    U_CPP=$CPP
    U_CPPFLAGS=$CPPFLAGS

    export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
    export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -L$SDKROOT/usr/lib/ -O3"
    export CPP="/usr/bin/cpp $CPPFLAGS"
    export LDFLAGS="-L$SDKROOT/usr/lib/"

    ./configure prefix=$JPEG_LIB_DIR –enable-shared –enable-static \
    CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin

    make
    make install-lib

    # cp the static + shared library
    cp $JPEG_LIB_DIR/lib/$LIBPATH_jpeg $LIB_DIR/$LIBNAME_jpeg.arm
    cp $JPEG_LIB_DIR/lib/libjpeg.62.0.0.dylib $LIB_DIR/jpeg_arm_dylib/libjpeg.dylib

    make distclean

    #######################################################
    ############ INTEL ###########################
    #######################################################

    # Use default environment
    export CC=$U_CC
    export CFLAGS="-arch $ARCH_SIM -O3"
    export LD=$U_LD
    export LDFLAGS=$U_LDFLAGS
    export CPP=$U_CPP
    export CPPFLAGS=$U_CPPFLAGS

    ./configure prefix=$JPEG_LIB_DIR –enable-shared –enable-static –host=i686-apple-darwin9

    make
    make install-lib

    # cp the static library
    cp $JPEG_LIB_DIR/lib/$LIBPATH_jpeg $LIB_DIR/$LIBNAME_jpeg.i386
    # cp the include/* files
    cp $JPEG_LIB_DIR/include/*.h $LIB_DIR/include/jpeg/

    make distclean

    # combine the static libraries for i386 and arm
    $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_jpeg.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_jpeg.i386 -create -output $LIB_DIR/$LIBNAME_jpeg
    }
    function tiff()
    {
    ########################################################
    ############# TIFF ############################
    ########################################################

    cd $TIFF_DIR

    LIBPATH_tiff=libtiff.a
    LIBNAME_tiff=`basename $LIBPATH_tiff`

    if [ "$1" == "arm" ]; then
    #######################################################
    ############ ARM ###########################
    #######################################################

    U_CC=$CC
    U_CFLAGS=$CFLAGS
    U_LD=$LD
    U_LDFLAGS=$LDFLAGS
    U_CPP=$CPP
    U_CPPFLAGS=$CPPFLAGS

    export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
    export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -L$SDKROOT/usr/lib/ -O3"
    export CPP="/usr/bin/cpp $CPPFLAGS"
    export LDFLAGS="-L$SDKROOT/usr/lib/"

    ./configure prefix=$TIFF_LIB_DIR CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION \
    LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin –disable-cxx \
    && make \
    && make install

    # cp the static + shared library
    cp $TIFF_LIB_DIR/lib/$LIBPATH_tiff $LIB_DIR/$LIBNAME_tiff.arm
    cp $TIFF_LIB_DIR/lib/libtiff.3.dylib $LIB_DIR/tiff_arm_dylib/libtiff.dylib

    make distclean
    elif [ "$1" == "$ARCH_SIM" ]; then
    #######################################################
    ############ INTEL ###########################
    #######################################################

    # Use default environment
    export CC=$U_CC
    export CFLAGS="-arch $ARCH_SIM"
    export LD=$U_LD
    export LDFLAGS=$U_LDFLAGS
    export CPP=$U_CPP
    export CPPFLAGS=$U_CPPFLAGS

    ./configure prefix=$TIFF_LIB_DIR –host=i686-apple-darwin9 –disable-cxx \
    && make \
    && make install

    # cp the static library
    cp $TIFF_LIB_DIR/lib/$LIBPATH_tiff $LIB_DIR/$LIBNAME_tiff.i386
    # cp the include/* files
    cp $TIFF_LIB_DIR/include/*.h $LIB_DIR/include/tiff/

    make distclean

    # combine the static libraries for i386 and arm
    $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_tiff.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_tiff.i386 -create -output $LIB_DIR/$LIBNAME_tiff
    fi
    }
    function im()
    {
    #######################################################
    ############ IMAGEMAGICK #######################
    #######################################################
    cd $IM_DIR

    # static library that will be generated
    LIBPATH_static=$IM_LIB_DIR/lib/libMagickCore.a
    LIBNAME_static=`basename $LIBPATH_static`
    LIBPATH_static2=$IM_LIB_DIR/lib/libMagickWand.a
    LIBNAME_static2=`basename $LIBPATH_static2`

    #######################################################
    ############ ARM ###########################
    #######################################################

    # Save relevant environment
    U_CC=$CC
    U_CFLAGS=$CFLAGS
    U_LD=$LD
    U_LDFLAGS=$LDFLAGS
    U_CPP=$CPP
    U_CPPFLAGS=$CPPFLAGS

    export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
    export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -I$LIB_DIR/include -O3"
    export LDFLAGS="-L$LIB_DIR/jpeg_arm_dylib/ -L$LIB_DIR/png_arm_dylib/ -L$LIB_DIR/tiff_arm_dylib/ -L$SDKROOT/usr/lib/"
    export CPP="/usr/bin/cpp $CPPFLAGS"
    export CXXFLAGS="-O3 -Wall -W -D_THREAD_SAFE"

    # configure to have the static libraries and make
    ./configure prefix=$IM_LIB_DIR CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin \
    –disable-largefile –with-quantum-depth=8 –without-magick-plus-plus –without-perl –without-x –without-freetype \
    –disable-shared

    # compile ImageMagick
    make
    make install

    # copy the CORE + WAND libraries – ARM version
    cp $LIBPATH_static $LIB_DIR/$LIBNAME_static.arm
    cp $LIBPATH_static2 $LIB_DIR/$LIBNAME_static2.arm

    # clean the ImageMagick build
    make distclean

    #######################################################
    ############ INTEL ###########################
    #######################################################

    # Use default environment
    export CC=$U_CC
    export CFLAGS="-arch $ARCH_SIM -O3"
    export LD=$U_LD
    export LDFLAGS=$U_LDFLAGS
    export CPP=$U_CPP
    export CPPFLAGS=$U_CPPFLAGS

    # configure with standard parameters
    ./configure prefix=$IM_LIB_DIR –host=i686-apple-darwin9 –without-magick-plus-plus –without-perl –without-x –without-freetype –disable-shared

    # compile ImageMagick
    make
    make install

    # copy the CORE + WAND libraries – INTEL version
    cp $LIBPATH_static $LIB_DIR/$LIBNAME_static.i386
    cp $LIBPATH_static2 $LIB_DIR/$LIBNAME_static2.i386

    # copy the wand/ + core/ headers
    cp $IM_LIB_DIR/include/ImageMagick/magick/* $LIB_DIR/include/magick/
    cp $IM_LIB_DIR/include/ImageMagick/wand/* $LIB_DIR/include/wand/

    # clean the ImageMagick build
    make distclean

    # combine the two generated libraries to be used both in the simulator and in the device
    $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_static.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_static.i386 -create -output $LIB_DIR/$LIBNAME_static
    $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_static2.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_static2.i386 -create -output $LIB_DIR/$LIBNAME_static2
    }

    png
    jpeg
    tiff "arm"
    tiff "$ARCH_SIM"
    im

  9. squap says:

    I’m getting :

    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/lipo: specifed architecture type (arm) for file (/Users/oliver/Desktop/tmp_target/im_libs/libMagickCore.a.arm) does not match it’s cputype (7) and cpusubtype (3) (should be cputype (12) and cpusubtype (0))
    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/lipo: specifed architecture type (arm) for file (/Users/oliver/Desktop/tmp_target/im_libs/libMagickWand.a.arm) does not match it’s cputype (7) and cpusubtype (3) (should be cputype (12) and cpusubtype (0))

    As a result my libMagickWand.a and libMagickCore.a are not created. How do I fix it ?

    • on snow change the last 2 lines of the script by this :
      $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_static.arm -arch x86_64 $LIB_DIR/$LIBNAME_static.i386 -create -output $LIB_DIR/$LIBNAME_static
      $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_static2.arm -arch x86_64 $LIB_DIR/$LIBNAME_static2.i386 -create -output $LIB_DIR/$LIBNAME_static2

      • Cloud says:

        Thank you very much for this fix, I will update the fixed scrip ASAP, I’m very busy with other projects now…

      • my mistake, few additional modification have to be done :
        the script below worked for me on snow

        # Author: Claudio Marforio
        # e-mail: marforio@gmail.com
        # date: 9.06.2009

        # Script to make static libraries (jpeg + png) and ImageMagick
        # the libraries will be conbined into i386+arm.a static libraries
        # to be used inside an XCODE project for iPhone development

        # The directory structure has to be:
        # ~/Desktop/cross_compile/i_m/ <- ImageMagick top directory
        # |-IMDelegataes/ <- Some delegates, in particular jpeg + png
        # |-jpeg-6b/ <- Patched jpeg6b
        # |-libpng-1.2.37 <- png lib – no need to patch it
        # |-tiff-3.8.2 <- tiff lib – no need to patch it
        # |-… <- we don't care what's here! :)

        # If you don't have this Directory structure you can either create it or try change around the script

        #!/bin/bash

        # Set this to the top directory of ImageMagick source:
        IM_DIR=$(pwd)/cross_compile/i_m
        JPEG_DIR=$IM_DIR/IMDelegates/jpeg-6b
        PNG_DIR=$IM_DIR/IMDelegates/libpng-1.2.40
        TIFF_DIR=$IM_DIR/IMDelegates/tiff-3.9.1
        ARCH_SIM="i386"
        GCC_VERSION="4.0.1"

        # Set this to where you want the libraries to be placed (if dir is not present it will be created):
        TARGET_LIB_DIR=$(pwd)/tmp_target
        LIB_DIR=$TARGET_LIB_DIR/im_libs
        JPEG_LIB_DIR=$TARGET_LIB_DIR/libjpeg
        PNG_LIB_DIR=$TARGET_LIB_DIR/libpng
        TIFF_LIB_DIR=$TARGET_LIB_DIR/libtiff
        IM_LIB_DIR=$TARGET_LIB_DIR/imagemagick

        # Set the build directories
        mkdir -p $TARGET_LIB_DIR
        mkdir -p $LIB_DIR/include/jpeg
        mkdir -p $LIB_DIR/include/magick
        mkdir -p $LIB_DIR/include/png
        mkdir -p $LIB_DIR/include/tiff
        mkdir -p $LIB_DIR/include/wand
        mkdir -p $LIB_DIR/jpeg_arm_dylib
        mkdir -p $LIB_DIR/png_arm_dylib
        mkdir -p $LIB_DIR/tiff_arm_dylib
        mkdir -p $JPEG_LIB_DIR/lib #we don't need bin/ and share/
        mkdir -p $JPEG_LIB_DIR/include
        mkdir -p $PNG_LIB_DIR #libpng manages to create subdirectories by itself with make install
        mkdir -p $TIFF_LIB_DIR #libtiff manages to create subdirectories by itself with make install

        # General folders where you have the iPhone compiler + tools
        export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
        # Change this to match for which version of the SDK you want to compile – you can change the number for the version
        export SDKROOT=$DEVROOT/SDKs/iPhoneOS3.1.sdk

        ############ HACK #################################
        # ImageMagick requires this header, that doesn't exist for the iPhone
        # Just copying it make things compile/work (more testing needed)
        #sudo cp /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/crt_externs.h \
        # $SDKROOT/usr/include/crt_externs.h
        ############ END – HACK #############################

        function png()
        {
        #######################################################
        ############ PNG ###########################
        #######################################################

        cd $PNG_DIR

        LIBPATH_png=libpng12.a

        #######################################################
        ############ ARM ###########################
        #######################################################

        U_CC=$CC
        U_CFLAGS=$CFLAGS
        U_LD=$LD
        U_LDFLAGS=$LDFLAGS
        U_CPP=$CPP
        U_CPPFLAGS=$CPPFLAGS

        export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
        export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -L$SDKROOT/usr/lib/ -O3"
        export CPP="/usr/bin/cpp $CPPFLAGS"
        export LDFLAGS="-L$SDKROOT/usr/lib/"

        ./configure prefix=$PNG_LIB_DIR –enable-shared –enable-static \
        CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin

        make
        make install

        # cp the static + shared library
        cp $PNG_LIB_DIR/lib/$LIBPATH_png $LIB_DIR/libpng.a.arm
        cp $PNG_LIB_DIR/lib/libpng12.0.dylib $LIB_DIR/png_arm_dylib/libpng.dylib

        make distclean

        #######################################################
        ############ INTEL ###########################
        #######################################################

        # Use default environment
        export CC=$U_CC
        export CFLAGS="-arch $ARCH_SIM -O3"
        export LD=$U_LD
        export LDFLAGS=$U_LDFLAGS
        export CPP=$U_CPP
        export CPPFLAGS=$U_CPPFLAGS

        LIBPATH_png=libpng12.a

        ./configure prefix=$PNG_LIB_DIR –enable-shared –enable-static –host=i686-apple-darwin9

        make
        make install

        # cp the static library
        cp $PNG_LIB_DIR/lib/$LIBPATH_png $LIB_DIR/libpng.a.i386
        # cp the include/* files
        cp $PNG_LIB_DIR/include/libpng12/* $LIB_DIR/include/png/

        make distclean

        # combine the static libraries for i386 and arm
        $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/libpng.a.arm -arch $ARCH_SIM $LIB_DIR/libpng.a.i386 -create -output $LIB_DIR/libpng.a
        }
        function jpeg()
        {
        #######################################################
        ############ JPEG ###########################
        #######################################################

        cd $JPEG_DIR

        LIBPATH_jpeg=libjpeg.a
        LIBNAME_jpeg=`basename $LIBPATH_jpeg`

        #######################################################
        ############ ARM ###########################
        #######################################################

        U_CC=$CC
        U_CFLAGS=$CFLAGS
        U_LD=$LD
        U_LDFLAGS=$LDFLAGS
        U_CPP=$CPP
        U_CPPFLAGS=$CPPFLAGS

        export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
        export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -L$SDKROOT/usr/lib/ -O3"
        export CPP="/usr/bin/cpp $CPPFLAGS"
        export LDFLAGS="-L$SDKROOT/usr/lib/"

        ./configure prefix=$JPEG_LIB_DIR –enable-shared –enable-static \
        CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin

        make
        make install-lib

        # cp the static + shared library
        cp $JPEG_LIB_DIR/lib/$LIBPATH_jpeg $LIB_DIR/$LIBNAME_jpeg.arm
        cp $JPEG_LIB_DIR/lib/libjpeg.62.0.0.dylib $LIB_DIR/jpeg_arm_dylib/libjpeg.dylib

        make distclean

        #######################################################
        ############ INTEL ###########################
        #######################################################

        # Use default environment
        export CC=$U_CC
        export CFLAGS="-arch $ARCH_SIM -O3"
        export LD=$U_LD
        export LDFLAGS=$U_LDFLAGS
        export CPP=$U_CPP
        export CPPFLAGS=$U_CPPFLAGS

        ./configure prefix=$JPEG_LIB_DIR –enable-shared –enable-static –host=i686-apple-darwin9

        make
        make install-lib

        # cp the static library
        cp $JPEG_LIB_DIR/lib/$LIBPATH_jpeg $LIB_DIR/$LIBNAME_jpeg.i386
        # cp the include/* files
        cp $JPEG_LIB_DIR/include/*.h $LIB_DIR/include/jpeg/

        make distclean

        # combine the static libraries for i386 and arm
        $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_jpeg.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_jpeg.i386 -create -output $LIB_DIR/$LIBNAME_jpeg
        }
        function tiff()
        {
        ########################################################
        ############# TIFF ############################
        ########################################################

        cd $TIFF_DIR

        LIBPATH_tiff=libtiff.a
        LIBNAME_tiff=`basename $LIBPATH_tiff`

        if [ "$1" == "arm" ]; then
        #######################################################
        ############ ARM ###########################
        #######################################################

        U_CC=$CC
        U_CFLAGS=$CFLAGS
        U_LD=$LD
        U_LDFLAGS=$LDFLAGS
        U_CPP=$CPP
        U_CPPFLAGS=$CPPFLAGS

        export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
        export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -L$SDKROOT/usr/lib/ -O3"
        export CPP="/usr/bin/cpp $CPPFLAGS"
        export LDFLAGS="-L$SDKROOT/usr/lib/"

        ./configure prefix=$TIFF_LIB_DIR CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION \
        LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin –disable-cxx \
        && make \
        && make install

        # cp the static + shared library
        cp $TIFF_LIB_DIR/lib/$LIBPATH_tiff $LIB_DIR/$LIBNAME_tiff.arm
        cp $TIFF_LIB_DIR/lib/libtiff.3.dylib $LIB_DIR/tiff_arm_dylib/libtiff.dylib

        make distclean
        elif [ "$1" == "$ARCH_SIM" ]; then
        #######################################################
        ############ INTEL ###########################
        #######################################################

        # Use default environment
        export CC=$U_CC
        export CFLAGS="-arch $ARCH_SIM"
        export LD=$U_LD
        export LDFLAGS=$U_LDFLAGS
        export CPP=$U_CPP
        export CPPFLAGS=$U_CPPFLAGS

        ./configure prefix=$TIFF_LIB_DIR –host=i686-apple-darwin9 –disable-cxx \
        && make \
        && make install

        # cp the static library
        cp $TIFF_LIB_DIR/lib/$LIBPATH_tiff $LIB_DIR/$LIBNAME_tiff.i386
        # cp the include/* files
        cp $TIFF_LIB_DIR/include/*.h $LIB_DIR/include/tiff/

        make distclean

        # combine the static libraries for i386 and arm
        $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_tiff.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_tiff.i386 -create -output $LIB_DIR/$LIBNAME_tiff
        fi
        }
        function im()
        {
        #######################################################
        ############ IMAGEMAGICK #######################
        #######################################################
        cd $IM_DIR

        # static library that will be generated
        LIBPATH_static=$IM_LIB_DIR/lib/libMagickCore.a
        LIBNAME_static=`basename $LIBPATH_static`
        LIBPATH_static2=$IM_LIB_DIR/lib/libMagickWand.a
        LIBNAME_static2=`basename $LIBPATH_static2`

        #######################################################
        ############ ARM ###########################
        #######################################################

        # Save relevant environment
        U_CC=$CC
        U_CFLAGS=$CFLAGS
        U_LD=$LD
        U_LDFLAGS=$LDFLAGS
        U_CPP=$CPP
        U_CPPFLAGS=$CPPFLAGS

        export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/$GCC_VERSION/include/ -I$SDKROOT/usr/include/"
        export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -I$SDKROOT/usr/include -I$LIB_DIR/include -O3"
        export LDFLAGS="-L$LIB_DIR/jpeg_arm_dylib/ -L$LIB_DIR/png_arm_dylib/ -L$LIB_DIR/tiff_arm_dylib/ -L$SDKROOT/usr/lib/"
        export CPP="/usr/bin/cpp $CPPFLAGS"
        export CXXFLAGS="-O3 -Wall -W -D_THREAD_SAFE"

        # configure to have the static libraries and make
        ./configure prefix=$IM_LIB_DIR CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-$GCC_VERSION LD=$DEVROOT/usr/bin/ld –host=arm-apple-darwin \
        –disable-largefile –with-quantum-depth=8 –without-magick-plus-plus –without-perl –without-x –without-freetype \
        –disable-shared

        # compile ImageMagick
        make
        make install

        # copy the CORE + WAND libraries – ARM version
        cp $LIBPATH_static $LIB_DIR/$LIBNAME_static.arm
        cp $LIBPATH_static2 $LIB_DIR/$LIBNAME_static2.arm

        # clean the ImageMagick build
        make distclean

        #######################################################
        ############ INTEL ###########################
        #######################################################

        # Use default environment
        export CC=$U_CC
        export CFLAGS="-arch $ARCH_SIM -O3"
        export LD=$U_LD
        export LDFLAGS=$U_LDFLAGS
        export CPP=$U_CPP
        export CPPFLAGS=$U_CPPFLAGS

        # configure with standard parameters
        ./configure prefix=$IM_LIB_DIR –host=i686-apple-darwin9 –without-magick-plus-plus –without-perl –without-x –without-freetype –disable-shared

        # compile ImageMagick
        make
        make install

        # copy the CORE + WAND libraries – INTEL version
        cp $LIBPATH_static $LIB_DIR/$LIBNAME_static.i386
        cp $LIBPATH_static2 $LIB_DIR/$LIBNAME_static2.i386

        # copy the wand/ + core/ headers
        cp $IM_LIB_DIR/include/ImageMagick/magick/* $LIB_DIR/include/magick/
        cp $IM_LIB_DIR/include/ImageMagick/wand/* $LIB_DIR/include/wand/

        # clean the ImageMagick build
        make distclean

        # combine the two generated libraries to be used both in the simulator and in the device
        $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_static.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_static.i386 -create -output $LIB_DIR/$LIBNAME_static
        $DEVROOT/usr/bin/lipo -arch arm $LIB_DIR/$LIBNAME_static2.arm -arch $ARCH_SIM $LIB_DIR/$LIBNAME_static2.i386 -create -output $LIB_DIR/$LIBNAME_static2
        }

        png
        jpeg
        tiff "arm"
        tiff "$ARCH_SIM"
        im

    • Jade says:

      I’m getting this exact error, and the script below from Verdier Christophe doesn’t work for me. I’m using Leopard 10.5.7, not Snow. Any help would be appreciated.

  10. bille says:

    Hi,

    My link and build seem to go fine – but at runtime I get this exception message:

    [Session started at 2009-08-23 21:26:59 -0400.]
    dyld: Symbol not found: _CFXMLNodeGetInfoPtr

    Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
    Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation

    Any idea why?

    AFAIK, I’m running the latest versions of Xcode, Mac OS X, and your files.

  11. Stoneage says:

    Maybe it’ a stupid question, but will the use of ImageMagick lead to problems with the AppStore?

  12. [...] for the iPhone please refer to my two previous posts where that’s described in detail: first and second. TIFF is compiled with ZIP functionality, so you can compress your TIFF [...]

  13. sachin says:

    hey cloud

    do u have universal binary for libtiff
    need that urgently.
    will be great if you can share script or the library itself.

  14. sachin says:

    he cloud

    Dont we need magic++ if we are working with objective c.
    basically if i am writing a wrapper (mm) class i will need to call c++ functionality.

    • Cloud says:

      @sachin: no, you can work with C ImageMagick, please look at the latest post where you can download a full XCode project to help you getting started with ImageMagick and Objective-C integration.
      For TIFF support I will look at it most likely when I come home from my holidays.

  15. sachin says:

    hi cloud .. can you help me with of how to convert jpg image to tif using imageMagic library.
    A quich help would be appritiated.
    Also what is entry points to imageMagic from code.
    I am working on objective c.

  16. Mario says:

    That would be great! I got it running in the simulator now, but not the ARM version… Enjoy your vacation!

  17. Mario says:

    Hey thanks for your script and of course for the Xcode project! I’m pretty new to Xcode/iPhone programming. Could you please give me a hint on what I’d have to do to get LIBTIFF into my iPhone project?

    • Cloud says:

      @Mario: I’ll have a look at cross-compiling libtiff and if everything works fine I’ll update the script and the XCode project (I’m on vacation so it might take a few days, I’ll email you once that is done!)

  18. Tom Jones says:

    Do you have a sample iPhone project with the libraries? BTW, thanks for the “compiled package”. The script did not libMagick*.a.arm files.

  19. [...] a static library that can be linked against for programming you can follow these instructions here: ImageMagick with JPG and PNG for iPhone and also the update that followed afterwards [...]

  20. TonTon says:

    can you please send me an email for “out of the blog “conversation, keeping it private of course
    thanks

  21. Cloud says:

    @TonTon: thank you! Done, it’s fixed now!

  22. TonTon says:

    hello the link to jpeg patched source is down can you please update?
    thanks 4 all

  23. Matt says:

    Wow, nevermind, figured it all out. :)

  24. Matt says:

    Thanks! I’ve been looking all over for something like this. Any notes on how to implement this once it’s added to a project? i.e. maybe how to load an image and get a histogram?

Leave a Reply

Donate

If you appreciate my work, my applications, this blog in general or you simply feel like rewarding me for something please consider donate.
Any amount will motivate me in keeping up with the blog and the applications.


Thank you!

Donors

F. Olsen, W. Chang, W. Edmondson, C. Sharff, M. Brown, G. Helton, J. de Ruiter, K. Langner, K. Smith, T. OHalloran, B. Zabarauskas

Listening to ...