 #
 #  File        : Makefile
 #                ( Makefile for GNU 'make' utility )
 #
 #  Description : Makefile for compiling CImg-based code on Unix.
 #                This file is a part of the CImg Library project.
 #                ( http://cimg.sourceforge.net )
 #
 #  Copyright   : David Tschumperle
 #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
 #
 #  License     : CeCILL v2.0
 #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
 #
 #  This software is governed by the CeCILL  license under French law and
 #  abiding by the rules of distribution of free software.  You can  use,
 #  modify and/ or redistribute the software under the terms of the CeCILL
 #  license as circulated by CEA, CNRS and INRIA at the following URL
 #  "http://www.cecill.info".
 #
 #  As a counterpart to the access to the source code and  rights to copy,
 #  modify and redistribute granted by the license, users are provided only
 #  with a limited warranty  and the software's author,  the holder of the
 #  economic rights,  and the successive licensors  have only  limited
 #  liability.
 #
 #  In this respect, the user's attention is drawn to the risks associated
 #  with loading,  using,  modifying and/or developing or reproducing the
 #  software by the user in light of its specific status of free software,
 #  that may mean  that it is complicated to manipulate,  and  that  also
 #  therefore means  that it is reserved for developers  and  experienced
 #  professionals having in-depth computer knowledge. Users are therefore
 #  encouraged to load and test the software's suitability as regards their
 #  requirements in conditions enabling the security of their systems and/or
 #  data to be ensured and,  more generally, to use and operate it in the
 #  same conditions as regards security.
 #
 #  The fact that you are presently reading this means that you have had
 #  knowledge of the CeCILL license and that you accept its terms.
 #

#-------------------------------------------------------------------------------------------
# Define the CImg files to be compiled (name of the source file without the .cpp extension)
#-------------------------------------------------------------------------------------------
CIMG_FILES = CImg_demo \
             curve_editor \
	     dtmri_view \
	     edge_explorer \
	     fade_images \
             generate_loop_macros \
	     greycstoration \
	     greycstoration4integration \
	     hough_transform \
	     image_registration \
	     image2ascii \
	     image_surface \
	     jawbreaker \
	     mcf_levelsets \
	     mcf_levelsets3d \
	     nlmeans \
	     odykill \
	     pde_heatflow2d \
	     pde_TschumperleDeriche2d \
	     radon_transform \
	     tetris \
	     toolbox3d \
	     tron \
	     tutorial \
	     wavelet_atrous \
	     inrcast \

#---------------------------------
# Set correct variables and paths
#---------------------------------
CIMG_VERSION = 1.28
X11PATH      = /usr/X11R6
CC           = g++

ifeq ($(CC),icc)
CFLAGS       = -ansi
LDFLAGS      = -lpthread
else
CFLAGS       = -Wall -W -ansi -pedantic
LDFLAGS      = -lm -lpthread
endif

#--------------------------------------------------
# Set compilation flags allowing to customize CImg
#--------------------------------------------------

# Flags to enable code optimization by the compiler
ifeq ($(CC),icc)
CIMG_OPT_FLAGS = -O3 -ipo -no-prec-div
else
CIMG_OPT_FLAGS = -O3 -ffast-math
endif

# Flags to enable OpenMP support
ifeq ($(CC),icc)
CIMG_OPENMP_FLAGS = -Dcimg_use_openmp -openmp -i-static
else
CIMG_OPENMP_FLAGS = -Dcimg_use_openmp -fopenmp
endif

# Flags to enable the use of the X11 library.
# (X11 is used by CImg to handle display windows)
# !!! For 64bits systems : replace -L$(X11PATH)/lib by -L$(X11PATH)/lib64 !!!
CIMG_X11_FLAGS = -I$(X11PATH)/include -L$(X11PATH)/lib -lX11

# Flags to enable fast image display, using the XSHM library.
# (CIMG_X11_FLAGS must be defined also)
CIMG_XSHM_FLAGS = -Dcimg_use_xshm -lXext

# Flags to enable screen mode switching, using the XRandr library.
# (CIMG_X11_FLAGS must be defined also)
CIMG_XRANDR_FLAGS = -Dcimg_use_xrandr -lXrandr

# Flags to enable native support for PNG image files, using the PNG library.
CIMG_PNG_FLAGS = -Dcimg_use_png -lpng -lz

# Flags to enable native support for JPEG image files, using the JPEG library.
CIMG_JPEG_FLAGS = -Dcimg_use_jpeg -ljpeg

# Flags to enable native support for TIFF image files, using the TIFF library.
CIMG_TIFF_FLAGS = -Dcimg_use_tiff -ltiff

# Flags to enable native support of most classical image file formats, using the Magick++ library.
CIMG_MAGICK_FLAGS = -Dcimg_use_magick `Magick++-config --cppflags` `Magick++-config --cxxflags` `Magick++-config --ldflags` `Magick++-config --libs`

# Flags to enable faster Discrete Fourier Transform computation, using the FFTW3 library
CIMG_FFTW3_FLAGS = -Dcimg_use_fftw3 -lfftw3

# Flags to enable the use of LAPACK routines for matrix computation
CIMG_LAPACK_FLAGS = -Dcimg_use_lapack -lblas -lg2c -llapack

# Flags to enable the use of the Board library
CIMG_BOARD_FLAGS = -Dcimg_use_board -lboard

# Flags to compile on Solaris
CIMG_SOLARIS_FLAGS = -R$(X11PATH)/lib -lrt -lnsl -lsocket

# Flags to compile on MacOSX with Carbon-based display support
CIMG_CARBON_FLAGS = -Dcimg_display_type=3 -framework Carbon

# Set default compilation flags.
# Uncomment lines here, if you want to customize your default configuration
CIMG_DEFAULT_FLAGS = $(CIMG_X11_FLAGS) \
                     $(CIMG_XSHM_FLAGS) \
                     $(CIMG_XRANDR_FLAGS) \
#                     $(CIMG_OPENMP_FLAGS) \
#                     $(CIMG_PNG_FLAGS) \
#                     $(CIMG_JPEG_FLAGS) \
#                     $(CIMG_TIFF_FLAGS) \
#                     $(CIMG_MAGICK_FLAGS) \
#                     $(CIMG_FFTW3_FLAGS)
#		      $(CIMG_LAPACK_FLAGS)

# Gather all compilation flags.
CIMG_ALL_FLAGS = $(CIMG_OPT_FLAGS) \
	         $(CIMG_X11_FLAGS) \
                 $(CIMG_XSHM_FLAGS) \
                 $(CIMG_XRANDR_FLAGS) \
                 $(CIMG_PNG_FLAGS) \
                 $(CIMG_JPEG_FLAGS) \
                 $(CIMG_TIFF_FLAGS) \
                 $(CIMG_MAGICK_FLAGS) \
                 $(CIMG_FFTW3_FLAGS) \
	         $(CIMG_LAPACK_FLAGS) \
                 $(CIMG_BOARD_FLAGS)

#-------------------------
# Define Makefile entries
#-------------------------
.cpp:
	@echo
	@echo "** Compiling '$* ($(CIMG_VERSION))' with '`$(CC) -v 2>&1 | grep -i version`'"
	@echo
	$(CC) -o $* $< $(CFLAGS) $(LDFLAGS) $(ARCHFLAGS)
ifeq ($(MACOSX_APP),1)
	mkdir -p ${*}.app/Contents/MacOS
	mv ${*} ${*}.app/Contents/MacOS
endif
menu:
	@echo
	@echo "CImg Library $(CIMG_VERSION) : Examples"
	@echo "-----------------------------"
	@echo "  > dlinux   : Compile for Linux/BSD/MacOSX, with X11 display and debug informations."
	@echo "  > linux    : Compile for Linux/BSD/MacOSX, with X11 display and standard options."
	@echo "  > olinux   : Compile for Linux/BSD/MacOSX, with X11 display and optimizations."
	@echo "  > mlinux   : Compile for Linus/BSD/MacOSX, with minimal dependancies."
	@echo "  > Mlinux   : Compile for Linux/BSD/MacOSX, with maximal dependancies."
	@echo
	@echo "  > dsolaris : Compile for Solaris, with X11 display and debug informations."
	@echo "  > solaris  : Compile for Solaris, with X11 display and standard options."
	@echo "  > osolaris : Compile for Solaris, with X11 display and optimizations."
	@echo "  > msolaris : Compile for Solaris, with minimal dependancies."
	@echo "  > Msolaris : Compile for Solaris, with maximal dependancies."
	@echo
	@echo "  > dmacosx : Compile for MacOSX, with Carbon display and debug informations."
	@echo "  > macosx  : Compile for MacOSX, with Carbon display and standard options."
	@echo "  > omacosx : Compile for MacOSX, with Carbon display and optimizations."
	@echo
	@echo "  > clean    : Clean generated files."
	@echo
	@echo "Choose your option :"
	@read CHOICE; echo; make $$CHOICE; echo; echo "> Next time, you can bypass the menu by typing directly 'make $$CHOICE'"; echo;

all: $(CIMG_FILES)

clean:
	rm -rf *.app *.exe *~ jpeg_buffer greycstoration4gimp $(CIMG_FILES)

greycstoration4gimp: greycstoration4gimp.cpp
	@echo
	@echo "** Compiling 'greycstoration4gimp ($(CIMG_VERSION))' with '`$(CC) -v 2>&1 | grep version`'"
	@echo
	$(CC) -I.. -I./plugins -o greycstoration4gimp greycstoration4gimp.cpp `gimptool-2.0 --cflags` `gimptool-2.0 --libs` -lpthread $(CIMG_OPT_FLAGS)

# Linux/BSD/Mac OSX targets
linux:
	make "ARCHFLAGS=$(CIMG_DEFAULT_FLAGS)" all
dlinux:
	make "ARCHFLAGS=-Dcimg_debug=3 -g $(CIMG_DEFAULT_FLAGS)" all
olinux:
	make "ARCHFLAGS=$(CIMG_OPT_FLAGS) $(CIMG_DEFAULT_FLAGS)" all
mlinux:
	make "ARCHFLAGS=-Dcimg_display_type=0 $(CIMG_OPT_FLAGS)" all
Mlinux:
	make "ARCHFLAGS=$(CIMG_ALL_FLAGS)" all jpeg_buffer greycstoration4gimp

# Sun Solaris targets
solaris:
	make "ARCHFLAGS=$(CIMG_SOLARIS_FLAGS) $(CIMG_DEFAULT_FLAGS)" all
dsolaris:
	make "ARCHFLAGS=-Dcimg_debug=3 -g $(CIMG_SOLARIS_FLAGS) $(CIMG_DEFAULT_FLAGS)" all
osolaris:
	make "ARCHFLAGS=$(CIMG_OPT_FLAGS) $(CIMG_SOLARIS_FLAGS) $(CIMG_DEFAULT_FLAGS)" all
msolaris:
	make "ARCHFLAGS=-Dcimg_display_type=0 $(CIMG_OPT_FLAGS)" all
Msolaris:
	make "ARCHFLAGS= $(CIMG_ALL_FLAGS)" all jpeg_buffer greycstoration4gimp

# MacOsX with Carbon display targets
macosx:
	make "MACOSX_APP=1" "ARCHFLAGS=$(CIMG_DEFAULT_FLAGS) $(CIMG_CARBON_FLAGS)" all
dmacosx:
	make "MACOSX_APP=1" "ARCHFLAGS=-Dcimg_debug=3 -g $(CIMG_DEFAULT_FLAGS) $(CIMG_CARBON_FLAGS)" all
omacosx:
	make "MACOSX_APP=1" "ARCHFLAGS=$(CIMG_OPT_FLAGS) $(CIMG_DEFAULT_FLAGS) $(CIMG_CARBON_FLAGS)" all
