#-----------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------
#
#  Makefile  : Makefile for compiling CImg-based code on Unix
#
#  Copyright : David Tschumperle (http://www.greyc.ensicaen.fr/~dtschump/)
#
#-----------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------------
# Define the CImg files to be compiled (name of the source file without the .cpp extension)
#-------------------------------------------------------------------------------------------
CIMG_FILES = CImg_demo \
	     dtmri_view \
	     edge_explorer \
	     fade_images \
	     greycstoration \
	     greycstoration4integration \
	     hough_transform \
	     image_registration \
	     image2ascii \
	     image_surface \
	     mcf_levelsets \
	     mcf_levelsets3D \
	     nlmeans \
	     odykill \
	     pslider \
	     pde_heatflow2D \
	     pde_TschumperleDeriche2D \
	     tetris \
	     tutorial \
	     wavelet_atrous \
	     inrcast

#---------------------------------
# Set correct variables and paths
#---------------------------------
CIMG_VERSION = 1.20
CC           = g++
X11PATH      = /usr/X11R6
CFLAGS       = -Wall -W -ansi -pedantic -ffast-math
LDFLAGS      = -lm -lpthread

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

# Flags to perform strict CImg compilation
# (Do not try to preserve compatibility with older CImg versions)
CIMG_STRICT_FLAGS = -Dcimg_strict

# Flags to enable the use of the X11 library.
# (X11 is used by CImg to handle display windows)
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 compile on Solaris
CIMG_SOLARIS_FLAGS = -R$(X11PATH)/lib -lrt -lnsl -lsocket

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

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

#-------------------------
# Define Makefile entries
#-------------------------
.cpp:
	@echo
	@echo "** Compiling '$* ($(CIMG_VERSION))' with '`$(CC) -v 2>&1 | grep version`'"
	@echo
	$(CC) -o $* $< $(CFLAGS) $(LDFLAGS) $(ARCHFLAGS)
menu:
	@echo
	@echo "CImg Library $(CIMG_VERSION) : Examples"
	@echo "-----------------------------"
	@echo "  > dlinux   : Compile for Linux/MacOSX, with debug informations."
	@echo "  > linux    : Compile for Linux/MacOSX, with standard options."
	@echo "  > olinux   : Compile for Linux/MacOSX, with optimizations."
	@echo "  > mlinux   : Compile for Linux/MacOSX, with minimal dependancies."
	@echo "  > Mlinux   : Compile for Linux/MacOSX, with maximal dependancies."
	@echo
	@echo "  > dsolaris : Compile for Solaris, with debug informations."
	@echo "  > solaris  : Compile for Solaris, with standard options."
	@echo "  > osolaris : Compile for Solaris, with optimizations."
	@echo "  > mlinux   : Compile for Solaris, with minimal dependancies."
	@echo "  > Mlinux   : Compile for Solaris, with maximal dependancies."
	@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 -f *.exe *~ $(CIMG_FILES)

# Linux/Mac OSX targets
linux:
	make "ARCHFLAGS=$(CIMG_DEFAULT_FLAGS)" all
dlinux:
	make "ARCHFLAGS=-Dcimg_debug=2 -g $(CIMG_DEFAULT_FLAGS)" all
olinux:
	make "ARCHFLAGS=-O3 $(CIMG_DEFAULT_FLAGS)" all
mlinux:
	make "ARCHFLAGS=-Dcimg_display_type=0 -O3" all
Mlinux:
	make "ARCHFLAGS=-O3 $(CIMG_ALL_FLAGS)" all

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

