Commit c0f3e9f6dc8caba1d7c8965adeeaf3f343d70325

Authored by Pavel Govyadinov
1 parent 14b500f9

UPDATE TO CIMG: version 1.63... minor bug fixes and print statements in cost.h, gl_spider.h

Showing 3 changed files with 7847 additions and 6188 deletions   Show diff stats
stim/cuda/cost.h
... ... @@ -15,21 +15,6 @@ texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn;
15 15 float *result;
16 16 cudaArray* srcArray;
17 17 bool testing = false;
18   -/*
19   -struct SharedMemory
20   -{
21   - __device__ inline operator float* ()
22   - {
23   - extern __shared__ float __smem[];
24   - return (float *)__smem;
25   - }
26   -
27   - __device__ inline operator const float* () const
28   - {
29   - extern __shared__ float __smem[];
30   - return (float *)__smem;
31   - }
32   -};*/
33 18  
34 19 inline void checkCUDAerrors(const char *msg)
35 20 {
... ... @@ -95,25 +80,6 @@ void get_diff (float *result)
95 80 __syncthreads();
96 81 }
97 82 __syncthreads();
98   -/* for(unsigned int step = 1; step < blockDim.x; step *= 2)
99   - {
100   - __syncthreads();
101   - if (x_t %(2*step) == 0)
102   - {
103   - shared[x_t][y_t] += shared[x_t + step][y_t];
104   - }
105   - }
106   - __syncthreads();
107   -
108   - for(unsigned int step = 1; step < blockDim.y; step *= 2)
109   - {
110   - __syncthreads();
111   - if(y_t%(2*step) == 0)
112   - {
113   - shared[x_t][y_t] += shared[x_t][y_t + step];
114   - }
115   - }
116   - __syncthreads(); */
117 83 if(x_t == 0 && y_t == 0)
118 84 result[g_idx] = shared[0][0];
119 85  
... ... @@ -213,6 +179,7 @@ stim::vec&lt;int&gt; get_cost(cudaGraphicsResource_t src, int DIM_Y)
213 179 //output[idx] = get_sum(result+(16*8*idx));
214 180 cleanUP(src);
215 181 ret[0] = idx; ret[1] = (int) output[idx];
  182 + std::cout << output[idx] << std::endl;
216 183 free(output);
217 184 return ret;
218 185 }
... ...
stim/gl/gl_spider.h
... ... @@ -121,7 +121,7 @@ class gl_spider
121 121 setMatrix();
122 122 glCallList(dList+3);
123 123  
124   -// int best = getCost();
  124 + // int best = getCost();
125 125  
126 126 }
127 127  
... ... @@ -730,7 +730,7 @@ class gl_spider
730 730 findOptimalDirection();
731 731 findOptimalPosition();
732 732 findOptimalScale();
733   - // branchDetection();
  733 + branchDetection();
734 734 Unbind();
735 735 return current_cost;
736 736 }
... ...
stim/image/CImg.h
Changes suppressed. Click to show
... ... @@ -5,7 +5,7 @@
5 5 #
6 6 # Description : The C++ Template Image Processing Toolkit.
7 7 # This file is the main component of the CImg Library project.
8   - # ( http://cimg.sourceforge.net )
  8 + # ( http://cimg.eu )
9 9 #
10 10 # Project manager : David Tschumperle.
11 11 # ( http://tschumperle.users.greyc.fr/ )
... ... @@ -54,7 +54,7 @@
54 54  
55 55 // Set version number of the library.
56 56 #ifndef cimg_version
57   -#define cimg_version 160
  57 +#define cimg_version 165
58 58  
59 59 /*-----------------------------------------------------------
60 60 #
... ... @@ -75,6 +75,7 @@
75 75 #include <cstdarg>
76 76 #include <cstring>
77 77 #include <cmath>
  78 +#include <climits>
78 79 #include <ctime>
79 80 #include <exception>
80 81  
... ... @@ -108,13 +109,28 @@
108 109 // Disable silly warnings on some Microsoft VC++ compilers.
109 110 #ifdef _MSC_VER
110 111 #pragma warning(push)
  112 +#pragma warning(disable:4127)
111 113 #pragma warning(disable:4311)
112 114 #pragma warning(disable:4312)
  115 +#pragma warning(disable:4512)
  116 +#pragma warning(disable:4571)
  117 +#pragma warning(disable:4640)
  118 +#pragma warning(disable:4706)
  119 +#pragma warning(disable:4710)
113 120 #pragma warning(disable:4800)
114 121 #pragma warning(disable:4804)
  122 +#pragma warning(disable:4820)
115 123 #pragma warning(disable:4996)
116 124 #define _CRT_SECURE_NO_DEPRECATE 1
117 125 #define _CRT_NONSTDC_NO_DEPRECATE 1
  126 +#define cimg_snprintf cimg::c99_snprintf
  127 +#define cimg_vsnprintf cimg::c99_vsnprintf
  128 +#endif
  129 +
  130 +#ifndef cimg_snprintf
  131 +#include <stdio.h>
  132 +#define cimg_snprintf snprintf
  133 +#define cimg_vsnprintf vsnprintf
118 134 #endif
119 135  
120 136 // Include OS-specific headers.
... ... @@ -123,6 +139,8 @@
123 139 #include <sys/time.h>
124 140 #include <sys/stat.h>
125 141 #include <unistd.h>
  142 +#include <dirent.h>
  143 +#include <fnmatch.h>
126 144 #elif cimg_OS==2
127 145 #ifndef NOMINMAX
128 146 #define NOMINMAX
... ... @@ -134,13 +152,6 @@
134 152 #include <shlobj.h>
135 153 #include <process.h>
136 154 #include <io.h>
137   -#define cimg_snprintf _snprintf
138   -#define cimg_vsnprintf _vsnprintf
139   -#endif
140   -#ifndef cimg_snprintf
141   -#include <stdio.h>
142   -#define cimg_snprintf snprintf
143   -#define cimg_vsnprintf vsnprintf
144 155 #endif
145 156  
146 157 // Look for C++11 features
... ... @@ -311,28 +322,6 @@ extern &quot;C&quot; {
311 322 #include "minc_1_simple_rw.h"
312 323 #endif
313 324  
314   -// Configure FFMPEG support.
315   -// (http://www.ffmpeg.org)
316   -//
317   -// Define 'cimg_use_ffmpeg' to enable FFMPEG lib support.
318   -//
319   -// Avcodec and Avformat libraries from FFMPEG may be used
320   -// to get a native support of various video file formats.
321   -// (see methods 'CImg[List]<T>::load_ffmpeg()').
322   -#ifdef cimg_use_ffmpeg
323   -#if (defined(_STDINT_H) || defined(_STDINT_H_)) && !defined(UINT64_C)
324   -#warning "__STDC_CONSTANT_MACROS has to be defined before including <stdint.h>, this file will probably not compile."
325   -#endif
326   -#ifndef __STDC_CONSTANT_MACROS
327   -#define __STDC_CONSTANT_MACROS // ...or stdint.h wont' define UINT64_C, needed by libavutil
328   -#endif
329   -extern "C" {
330   -#include <libavformat/avformat.h>
331   -#include <libavcodec/avcodec.h>
332   -#include <libswscale/swscale.h>
333   -}
334   -#endif
335   -
336 325 // Configure Zlib support.
337 326 // (http://www.zlib.net)
338 327 //
... ... @@ -346,6 +335,17 @@ extern &quot;C&quot; {
346 335 }
347 336 #endif
348 337  
  338 +// Configure libcurl support.
  339 +// (http://curl.haxx.se/libcurl/)
  340 +//
  341 +// Define 'cimg_use_curl' to enable libcurl support.
  342 +//
  343 +// Libcurl may be used to get a native support of file downloading from the network.
  344 +// (see method 'cimg::load_network()'.)
  345 +#ifdef cimg_use_curl
  346 +#include "curl/curl.h"
  347 +#endif
  348 +
349 349 // Configure Magick++ support.
350 350 // (http://www.imagemagick.org/Magick++)
351 351 //
... ... @@ -715,7 +715,7 @@ extern &quot;C&quot; {
715 715 #define cimg_forYZC(img,y,z,c) cimg_forC(img,c) cimg_forYZ(img,y,z)
716 716 #define cimg_forXYZC(img,x,y,z,c) cimg_forC(img,c) cimg_forXYZ(img,x,y,z)
717 717  
718   -#define cimg_rof1(bound,i) for (int i = (int)(bound)-1; i>=0; --i)
  718 +#define cimg_rof1(bound,i) for (int i = (int)(bound) - 1; i>=0; --i)
719 719 #define cimg_rofX(img,x) cimg_rof1((img)._width,x)
720 720 #define cimg_rofY(img,y) cimg_rof1((img)._height,y)
721 721 #define cimg_rofZ(img,z) cimg_rof1((img)._depth,z)
... ... @@ -733,7 +733,7 @@ extern &quot;C&quot; {
733 733 #define cimg_rofXYZC(img,x,y,z,c) cimg_rofC(img,c) cimg_rofXYZ(img,x,y,z)
734 734  
735 735 #define cimg_for_in1(bound,i0,i1,i) \
736   - for (int i = (int)(i0)<0?0:(int)(i0), _max##i = (int)(i1)<(int)(bound)?(int)(i1):(int)(bound)-1; i<=_max##i; ++i)
  736 + for (int i = (int)(i0)<0?0:(int)(i0), _max##i = (int)(i1)<(int)(bound)?(int)(i1):(int)(bound) - 1; i<=_max##i; ++i)
737 737 #define cimg_for_inX(img,x0,x1,x) cimg_for_in1((img)._width,x0,x1,x)
738 738 #define cimg_for_inY(img,y0,y1,y) cimg_for_in1((img)._height,y0,y1,y)
739 739 #define cimg_for_inZ(img,z0,z1,z) cimg_for_in1((img)._depth,z0,z1,z)
... ... @@ -750,33 +750,33 @@ extern &quot;C&quot; {
750 750 #define cimg_for_inYZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_inC(img,c0,c1,c) cimg_for_inYZ(img,y0,z0,y1,z1,y,z)
751 751 #define cimg_for_inXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
752 752 cimg_for_inC(img,c0,c1,c) cimg_for_inXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
753   -#define cimg_for_insideX(img,x,n) cimg_for_inX(img,n,(img)._width-1-(n),x)
754   -#define cimg_for_insideY(img,y,n) cimg_for_inY(img,n,(img)._height-1-(n),y)
755   -#define cimg_for_insideZ(img,z,n) cimg_for_inZ(img,n,(img)._depth-1-(n),z)
756   -#define cimg_for_insideC(img,c,n) cimg_for_inC(img,n,(img)._spectrum-1-(n),c)
757   -#define cimg_for_insideXY(img,x,y,n) cimg_for_inXY(img,n,n,(img)._width-1-(n),(img)._height-1-(n),x,y)
  753 +#define cimg_for_insideX(img,x,n) cimg_for_inX(img,n,(img)._width - 1 - (n),x)
  754 +#define cimg_for_insideY(img,y,n) cimg_for_inY(img,n,(img)._height - 1 - (n),y)
  755 +#define cimg_for_insideZ(img,z,n) cimg_for_inZ(img,n,(img)._depth - 1 - (n),z)
  756 +#define cimg_for_insideC(img,c,n) cimg_for_inC(img,n,(img)._spectrum - 1 - (n),c)
  757 +#define cimg_for_insideXY(img,x,y,n) cimg_for_inXY(img,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),x,y)
758 758 #define cimg_for_insideXYZ(img,x,y,z,n) \
759   - cimg_for_inXYZ(img,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),x,y,z)
  759 + cimg_for_inXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
760 760 #define cimg_for_insideXYZC(img,x,y,z,c,n) \
761   - cimg_for_inXYZ(img,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),x,y,z)
  761 + cimg_for_inXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
762 762  
763 763 #define cimg_for_out1(boundi,i0,i1,i) \
764   - for (int i = (int)(i0)>0?0:(int)(i1)+1; i<(int)(boundi); ++i, i = i==(int)(i0)?(int)(i1)+1:i)
  764 + for (int i = (int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); ++i, i = i==(int)(i0)?(int)(i1) + 1:i)
765 765 #define cimg_for_out2(boundi,boundj,i0,j0,i1,j1,i,j) \
766 766 for (int j = 0; j<(int)(boundj); ++j) \
767   - for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j?0:(int)(i0)>0?0:(int)(i1)+1; i<(int)(boundi); \
768   - ++i, i = _n1j?i:(i==(int)(i0)?(int)(i1)+1:i))
  767 + for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j?0:(int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); \
  768 + ++i, i = _n1j?i:(i==(int)(i0)?(int)(i1) + 1:i))
769 769 #define cimg_for_out3(boundi,boundj,boundk,i0,j0,k0,i1,j1,k1,i,j,k) \
770 770 for (int k = 0; k<(int)(boundk); ++k) \
771 771 for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \
772   - for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k?0:(int)(i0)>0?0:(int)(i1)+1; i<(int)(boundi); \
773   - ++i, i = _n1j || _n1k?i:(i==(int)(i0)?(int)(i1)+1:i))
  772 + for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k?0:(int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); \
  773 + ++i, i = _n1j || _n1k?i:(i==(int)(i0)?(int)(i1) + 1:i))
774 774 #define cimg_for_out4(boundi,boundj,boundk,boundl,i0,j0,k0,l0,i1,j1,k1,l1,i,j,k,l) \
775 775 for (int l = 0; l<(int)(boundl); ++l) \
776 776 for (int _n1l = (int)(l<(int)(l0) || l>(int)(l1)), k = 0; k<(int)(boundk); ++k) \
777 777 for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \
778   - for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k || _n1l?0:(int)(i0)>0?0:(int)(i1)+1; \
779   - i<(int)(boundi); ++i, i = _n1j || _n1k || _n1l?i:(i==(int)(i0)?(int)(i1)+1:i))
  778 + for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k || _n1l?0:(int)(i0)>0?0:(int)(i1) + 1; \
  779 + i<(int)(boundi); ++i, i = _n1j || _n1k || _n1l?i:(i==(int)(i0)?(int)(i1) + 1:i))
780 780 #define cimg_for_outX(img,x0,x1,x) cimg_for_out1((img)._width,x0,x1,x)
781 781 #define cimg_for_outY(img,y0,y1,y) cimg_for_out1((img)._height,y0,y1,y)
782 782 #define cimg_for_outZ(img,z0,z1,z) cimg_for_out1((img)._depth,z0,z1,z)
... ... @@ -797,25 +797,26 @@ extern &quot;C&quot; {
797 797 cimg_for_out3((img)._height,(img)._depth,(img)._spectrum,y0,z0,c0,y1,z1,c1,y,z,c)
798 798 #define cimg_for_outXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
799 799 cimg_for_out4((img)._width,(img)._height,(img)._depth,(img)._spectrum,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c)
800   -#define cimg_for_borderX(img,x,n) cimg_for_outX(img,n,(img)._width-1-(n),x)
801   -#define cimg_for_borderY(img,y,n) cimg_for_outY(img,n,(img)._height-1-(n),y)
802   -#define cimg_for_borderZ(img,z,n) cimg_for_outZ(img,n,(img)._depth-1-(n),z)
803   -#define cimg_for_borderC(img,c,n) cimg_for_outC(img,n,(img)._spectrum-1-(n),c)
804   -#define cimg_for_borderXY(img,x,y,n) cimg_for_outXY(img,n,n,(img)._width-1-(n),(img)._height-1-(n),x,y)
  800 +#define cimg_for_borderX(img,x,n) cimg_for_outX(img,n,(img)._width - 1 - (n),x)
  801 +#define cimg_for_borderY(img,y,n) cimg_for_outY(img,n,(img)._height - 1 - (n),y)
  802 +#define cimg_for_borderZ(img,z,n) cimg_for_outZ(img,n,(img)._depth - 1 - (n),z)
  803 +#define cimg_for_borderC(img,c,n) cimg_for_outC(img,n,(img)._spectrum - 1 - (n),c)
  804 +#define cimg_for_borderXY(img,x,y,n) cimg_for_outXY(img,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),x,y)
805 805 #define cimg_for_borderXYZ(img,x,y,z,n) \
806   - cimg_for_outXYZ(img,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),x,y,z)
  806 + cimg_for_outXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
807 807 #define cimg_for_borderXYZC(img,x,y,z,c,n) \
808   - cimg_for_outXYZC(img,n,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),(img)._spectrum-1-(n),x,y,z,c)
  808 + cimg_for_outXYZC(img,n,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n), \
  809 + (img)._depth - 1 - (n),(img)._spectrum - 1 - (n),x,y,z,c)
809 810  
810 811 #define cimg_for_spiralXY(img,x,y) \
811 812 for (int x = 0, y = 0, _n1##x = 1, _n1##y = (img).width()*(img).height(); _n1##y; \
812   - --_n1##y, _n1##x+=(_n1##x>>2)-((!(_n1##x&3)?--y:((_n1##x&3)==1?(img)._width-1-++x:\
813   - ((_n1##x&3)==2?(img)._height-1-++y:--x))))?0:1)
  813 + --_n1##y, _n1##x+=(_n1##x>>2) - ((!(_n1##x&3)?--y:((_n1##x&3)==1?(img)._width - 1 - ++x:\
  814 + ((_n1##x&3)==2?(img)._height - 1 - ++y:--x))))?0:1)
814 815  
815 816 #define cimg_for_lineXY(x,y,x0,y0,x1,y1) \
816 817 for (int x = (int)(x0), y = (int)(y0), _sx = 1, _sy = 1, _steep = 0, \
817   - _dx=(x1)>(x0)?(int)(x1)-(int)(x0):(_sx=-1,(int)(x0)-(int)(x1)), \
818   - _dy=(y1)>(y0)?(int)(y1)-(int)(y0):(_sy=-1,(int)(y0)-(int)(y1)), \
  818 + _dx=(x1)>(x0)?(int)(x1) - (int)(x0):(_sx=-1,(int)(x0) - (int)(x1)), \
  819 + _dy=(y1)>(y0)?(int)(y1) - (int)(y0):(_sy=-1,(int)(y0) - (int)(y1)), \
819 820 _counter = _dx, \
820 821 _err = _dx>_dy?(_dy>>1):((_steep=1),(_counter=_dy),(_dx>>1)); \
821 822 _counter>=0; \
... ... @@ -824,7 +825,7 @@ extern &quot;C&quot; {
824 825 (y+=(_err-=_dy)<0?_err+=_dx,_sy:0,_sx))
825 826  
826 827 #define cimg_for2(bound,i) \
827   - for (int i = 0, _n1##i = 1>=(bound)?(int)(bound)-1:1; \
  828 + for (int i = 0, _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
828 829 _n1##i<(int)(bound) || i==--_n1##i; \
829 830 ++i, ++_n1##i)
830 831 #define cimg_for2X(img,x) cimg_for2((img)._width,x)
... ... @@ -844,7 +845,7 @@ extern &quot;C&quot; {
844 845  
845 846 #define cimg_for_in2(bound,i0,i1,i) \
846 847 for (int i = (int)(i0)<0?0:(int)(i0), \
847   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1; \
  848 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1; \
848 849 i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \
849 850 ++i, ++_n1##i)
850 851 #define cimg_for_in2X(img,x0,x1,x) cimg_for_in2((img)._width,x0,x1,x)
... ... @@ -865,7 +866,7 @@ extern &quot;C&quot; {
865 866  
866 867 #define cimg_for3(bound,i) \
867 868 for (int i = 0, _p1##i = 0, \
868   - _n1##i = 1>=(bound)?(int)(bound)-1:1; \
  869 + _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
869 870 _n1##i<(int)(bound) || i==--_n1##i; \
870 871 _p1##i = i++, ++_n1##i)
871 872 #define cimg_for3X(img,x) cimg_for3((img)._width,x)
... ... @@ -885,8 +886,8 @@ extern &quot;C&quot; {
885 886  
886 887 #define cimg_for_in3(bound,i0,i1,i) \
887 888 for (int i = (int)(i0)<0?0:(int)(i0), \
888   - _p1##i = i-1<0?0:i-1, \
889   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1; \
  889 + _p1##i = i - 1<0?0:i - 1, \
  890 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1; \
890 891 i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \
891 892 _p1##i = i++, ++_n1##i)
892 893 #define cimg_for_in3X(img,x0,x1,x) cimg_for_in3((img)._width,x0,x1,x)
... ... @@ -906,8 +907,8 @@ extern &quot;C&quot; {
906 907 cimg_for_in3C(img,c0,c1,c) cimg_for_in3XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
907 908  
908 909 #define cimg_for4(bound,i) \
909   - for (int i = 0, _p1##i = 0, _n1##i = 1>=(bound)?(int)(bound)-1:1, \
910   - _n2##i = 2>=(bound)?(int)(bound)-1:2; \
  910 + for (int i = 0, _p1##i = 0, _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  911 + _n2##i = 2>=(bound)?(int)(bound) - 1:2; \
911 912 _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \
912 913 _p1##i = i++, ++_n1##i, ++_n2##i)
913 914 #define cimg_for4X(img,x) cimg_for4((img)._width,x)
... ... @@ -927,9 +928,9 @@ extern &quot;C&quot; {
927 928  
928 929 #define cimg_for_in4(bound,i0,i1,i) \
929 930 for (int i = (int)(i0)<0?0:(int)(i0), \
930   - _p1##i = i-1<0?0:i-1, \
931   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \
932   - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2; \
  931 + _p1##i = i - 1<0?0:i - 1, \
  932 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  933 + _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2; \
933 934 i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \
934 935 _p1##i = i++, ++_n1##i, ++_n2##i)
935 936 #define cimg_for_in4X(img,x0,x1,x) cimg_for_in4((img)._width,x0,x1,x)
... ... @@ -950,8 +951,8 @@ extern &quot;C&quot; {
950 951  
951 952 #define cimg_for5(bound,i) \
952 953 for (int i = 0, _p2##i = 0, _p1##i = 0, \
953   - _n1##i = 1>=(bound)?(int)(bound)-1:1, \
954   - _n2##i = 2>=(bound)?(int)(bound)-1:2; \
  954 + _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  955 + _n2##i = 2>=(bound)?(int)(bound) - 1:2; \
955 956 _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \
956 957 _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i)
957 958 #define cimg_for5X(img,x) cimg_for5((img)._width,x)
... ... @@ -971,10 +972,10 @@ extern &quot;C&quot; {
971 972  
972 973 #define cimg_for_in5(bound,i0,i1,i) \
973 974 for (int i = (int)(i0)<0?0:(int)(i0), \
974   - _p2##i = i-2<0?0:i-2, \
975   - _p1##i = i-1<0?0:i-1, \
976   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \
977   - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2; \
  975 + _p2##i = i - 2<0?0:i - 2, \
  976 + _p1##i = i - 1<0?0:i - 1, \
  977 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  978 + _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2; \
978 979 i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \
979 980 _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i)
980 981 #define cimg_for_in5X(img,x0,x1,x) cimg_for_in5((img)._width,x0,x1,x)
... ... @@ -995,9 +996,9 @@ extern &quot;C&quot; {
995 996  
996 997 #define cimg_for6(bound,i) \
997 998 for (int i = 0, _p2##i = 0, _p1##i = 0, \
998   - _n1##i = 1>=(bound)?(int)(bound)-1:1, \
999   - _n2##i = 2>=(bound)?(int)(bound)-1:2, \
1000   - _n3##i = 3>=(bound)?(int)(bound)-1:3; \
  999 + _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1000 + _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
  1001 + _n3##i = 3>=(bound)?(int)(bound) - 1:3; \
1001 1002 _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \
1002 1003 _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
1003 1004 #define cimg_for6X(img,x) cimg_for6((img)._width,x)
... ... @@ -1017,11 +1018,11 @@ extern &quot;C&quot; {
1017 1018  
1018 1019 #define cimg_for_in6(bound,i0,i1,i) \
1019 1020 for (int i = (int)(i0)<0?0:(int)(i0), \
1020   - _p2##i = i-2<0?0:i-2, \
1021   - _p1##i = i-1<0?0:i-1, \
1022   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \
1023   - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \
1024   - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3; \
  1021 + _p2##i = i - 2<0?0:i - 2, \
  1022 + _p1##i = i - 1<0?0:i - 1, \
  1023 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1024 + _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1025 + _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3; \
1025 1026 i<=(int)(i1) && \
1026 1027 (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \
1027 1028 _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
... ... @@ -1043,9 +1044,9 @@ extern &quot;C&quot; {
1043 1044  
1044 1045 #define cimg_for7(bound,i) \
1045 1046 for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
1046   - _n1##i = 1>=(bound)?(int)(bound)-1:1, \
1047   - _n2##i = 2>=(bound)?(int)(bound)-1:2, \
1048   - _n3##i = 3>=(bound)?(int)(bound)-1:3; \
  1047 + _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1048 + _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
  1049 + _n3##i = 3>=(bound)?(int)(bound) - 1:3; \
1049 1050 _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \
1050 1051 _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
1051 1052 #define cimg_for7X(img,x) cimg_for7((img)._width,x)
... ... @@ -1065,12 +1066,12 @@ extern &quot;C&quot; {
1065 1066  
1066 1067 #define cimg_for_in7(bound,i0,i1,i) \
1067 1068 for (int i = (int)(i0)<0?0:(int)(i0), \
1068   - _p3##i = i-3<0?0:i-3, \
1069   - _p2##i = i-2<0?0:i-2, \
1070   - _p1##i = i-1<0?0:i-1, \
1071   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \
1072   - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \
1073   - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3; \
  1069 + _p3##i = i - 3<0?0:i - 3, \
  1070 + _p2##i = i - 2<0?0:i - 2, \
  1071 + _p1##i = i - 1<0?0:i - 1, \
  1072 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1073 + _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1074 + _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3; \
1074 1075 i<=(int)(i1) && \
1075 1076 (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \
1076 1077 _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
... ... @@ -1092,10 +1093,10 @@ extern &quot;C&quot; {
1092 1093  
1093 1094 #define cimg_for8(bound,i) \
1094 1095 for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
1095   - _n1##i = 1>=(bound)?(int)(bound)-1:1, \
1096   - _n2##i = 2>=(bound)?(int)(bound)-1:2, \
1097   - _n3##i = 3>=(bound)?(int)(bound)-1:3, \
1098   - _n4##i = 4>=(bound)?(int)(bound)-1:4; \
  1096 + _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1097 + _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
  1098 + _n3##i = 3>=(bound)?(int)(bound) - 1:3, \
  1099 + _n4##i = 4>=(bound)?(int)(bound) - 1:4; \
1099 1100 _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
1100 1101 i==(_n4##i = _n3##i = _n2##i = --_n1##i); \
1101 1102 _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
... ... @@ -1116,13 +1117,13 @@ extern &quot;C&quot; {
1116 1117  
1117 1118 #define cimg_for_in8(bound,i0,i1,i) \
1118 1119 for (int i = (int)(i0)<0?0:(int)(i0), \
1119   - _p3##i = i-3<0?0:i-3, \
1120   - _p2##i = i-2<0?0:i-2, \
1121   - _p1##i = i-1<0?0:i-1, \
1122   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \
1123   - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \
1124   - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3, \
1125   - _n4##i = i+4>=(int)(bound)?(int)(bound)-1:i+4; \
  1120 + _p3##i = i - 3<0?0:i - 3, \
  1121 + _p2##i = i - 2<0?0:i - 2, \
  1122 + _p1##i = i - 1<0?0:i - 1, \
  1123 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1124 + _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1125 + _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3, \
  1126 + _n4##i = i + 4>=(int)(bound)?(int)(bound) - 1:i + 4; \
1126 1127 i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
1127 1128 i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \
1128 1129 _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
... ... @@ -1144,10 +1145,10 @@ extern &quot;C&quot; {
1144 1145  
1145 1146 #define cimg_for9(bound,i) \
1146 1147 for (int i = 0, _p4##i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
1147   - _n1##i = 1>=(int)(bound)?(int)(bound)-1:1, \
1148   - _n2##i = 2>=(int)(bound)?(int)(bound)-1:2, \
1149   - _n3##i = 3>=(int)(bound)?(int)(bound)-1:3, \
1150   - _n4##i = 4>=(int)(bound)?(int)(bound)-1:4; \
  1148 + _n1##i = 1>=(int)(bound)?(int)(bound) - 1:1, \
  1149 + _n2##i = 2>=(int)(bound)?(int)(bound) - 1:2, \
  1150 + _n3##i = 3>=(int)(bound)?(int)(bound) - 1:3, \
  1151 + _n4##i = 4>=(int)(bound)?(int)(bound) - 1:4; \
1151 1152 _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
1152 1153 i==(_n4##i = _n3##i = _n2##i = --_n1##i); \
1153 1154 _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
... ... @@ -1168,14 +1169,14 @@ extern &quot;C&quot; {
1168 1169  
1169 1170 #define cimg_for_in9(bound,i0,i1,i) \
1170 1171 for (int i = (int)(i0)<0?0:(int)(i0), \
1171   - _p4##i = i-4<0?0:i-4, \
1172   - _p3##i = i-3<0?0:i-3, \
1173   - _p2##i = i-2<0?0:i-2, \
1174   - _p1##i = i-1<0?0:i-1, \
1175   - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \
1176   - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \
1177   - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3, \
1178   - _n4##i = i+4>=(int)(bound)?(int)(bound)-1:i+4; \
  1172 + _p4##i = i - 4<0?0:i - 4, \
  1173 + _p3##i = i - 3<0?0:i - 3, \
  1174 + _p2##i = i - 2<0?0:i - 2, \
  1175 + _p1##i = i - 1<0?0:i - 1, \
  1176 + _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1177 + _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1178 + _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3, \
  1179 + _n4##i = i + 4>=(int)(bound)?(int)(bound) - 1:i + 4; \
1179 1180 i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
1180 1181 i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \
1181 1182 _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
... ... @@ -1200,7 +1201,7 @@ extern &quot;C&quot; {
1200 1201 _n1##x = (int)( \
1201 1202 (I[0] = (T)(img)(0,y,z,c)), \
1202 1203 (I[2] = (T)(img)(0,_n1##y,z,c)), \
1203   - 1>=(img)._width?(img).width()-1:1); \
  1204 + 1>=(img)._width?(img).width() - 1:1); \
1204 1205 (_n1##x<(img).width() && ( \
1205 1206 (I[1] = (T)(img)(_n1##x,y,z,c)), \
1206 1207 (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
... ... @@ -1214,7 +1215,7 @@ extern &quot;C&quot; {
1214 1215 _n1##x = (int)( \
1215 1216 (I[0] = (T)(img)(x,y,z,c)), \
1216 1217 (I[2] = (T)(img)(x,_n1##y,z,c)), \
1217   - x+1>=(int)(img)._width?(img).width()-1:x+1); \
  1218 + x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
1218 1219 x<=(int)(x1) && ((_n1##x<(img).width() && ( \
1219 1220 (I[1] = (T)(img)(_n1##x,y,z,c)), \
1220 1221 (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
... ... @@ -1229,8 +1230,8 @@ extern &quot;C&quot; {
1229 1230 _n1##x = (int)( \
1230 1231 (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
1231 1232 (I[3] = I[4] = (T)(img)(0,y,z,c)), \
1232   - (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \
1233   - 1>=(img)._width?(img).width()-1:1); \
  1233 + (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \
  1234 + 1>=(img)._width?(img).width() - 1:1); \
1234 1235 (_n1##x<(img).width() && ( \
1235 1236 (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
1236 1237 (I[5] = (T)(img)(_n1##x,y,z,c)), \
... ... @@ -1243,7 +1244,7 @@ extern &quot;C&quot; {
1243 1244  
1244 1245 #define cimg_for_in3x3(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1245 1246 cimg_for_in3((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1246   - _p1##x = x-1<0?0:x-1, \
  1247 + _p1##x = x - 1<0?0:x - 1, \
1247 1248 _n1##x = (int)( \
1248 1249 (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \
1249 1250 (I[3] = (T)(img)(_p1##x,y,z,c)), \
... ... @@ -1251,12 +1252,12 @@ extern &quot;C&quot; {
1251 1252 (I[1] = (T)(img)(x,_p1##y,z,c)), \
1252 1253 (I[4] = (T)(img)(x,y,z,c)), \
1253 1254 (I[7] = (T)(img)(x,_n1##y,z,c)), \
1254   - x+1>=(int)(img)._width?(img).width()-1:x+1); \
  1255 + x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
1255 1256 x<=(int)(x1) && ((_n1##x<(img).width() && ( \
1256 1257 (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
1257 1258 (I[5] = (T)(img)(_n1##x,y,z,c)), \
1258 1259 (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
1259   - x==--_n1##x); \
  1260 + x==--_n1##x); \
1260 1261 I[0] = I[1], I[1] = I[2], \
1261 1262 I[3] = I[4], I[4] = I[5], \
1262 1263 I[6] = I[7], I[7] = I[8], \
... ... @@ -1265,7 +1266,7 @@ extern &quot;C&quot; {
1265 1266 #define cimg_for4x4(img,x,y,z,c,I,T) \
1266 1267 cimg_for4((img)._height,y) for (int x = 0, \
1267 1268 _p1##x = 0, \
1268   - _n1##x = 1>=(img)._width?(img).width()-1:1, \
  1269 + _n1##x = 1>=(img)._width?(img).width() - 1:1, \
1269 1270 _n2##x = (int)( \
1270 1271 (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
1271 1272 (I[4] = I[5] = (T)(img)(0,y,z,c)), \
... ... @@ -1275,7 +1276,7 @@ extern &quot;C&quot; {
1275 1276 (I[6] = (T)(img)(_n1##x,y,z,c)), \
1276 1277 (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \
1277 1278 (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \
1278   - 2>=(img)._width?(img).width()-1:2); \
  1279 + 2>=(img)._width?(img).width() - 1:2); \
1279 1280 (_n2##x<(img).width() && ( \
1280 1281 (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \
1281 1282 (I[7] = (T)(img)(_n2##x,y,z,c)), \
... ... @@ -1290,8 +1291,8 @@ extern &quot;C&quot; {
1290 1291  
1291 1292 #define cimg_for_in4x4(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1292 1293 cimg_for_in4((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1293   - _p1##x = x-1<0?0:x-1, \
1294   - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \
  1294 + _p1##x = x - 1<0?0:x - 1, \
  1295 + _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
1295 1296 _n2##x = (int)( \
1296 1297 (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \
1297 1298 (I[4] = (T)(img)(_p1##x,y,z,c)), \
... ... @@ -1305,7 +1306,7 @@ extern &quot;C&quot; {
1305 1306 (I[6] = (T)(img)(_n1##x,y,z,c)), \
1306 1307 (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \
1307 1308 (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \
1308   - x+2>=(int)(img)._width?(img).width()-1:x+2); \
  1309 + x + 2>=(int)(img)._width?(img).width() - 1:x + 2); \
1309 1310 x<=(int)(x1) && ((_n2##x<(img).width() && ( \
1310 1311 (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \
1311 1312 (I[7] = (T)(img)(_n2##x,y,z,c)), \
... ... @@ -1321,7 +1322,7 @@ extern &quot;C&quot; {
1321 1322 #define cimg_for5x5(img,x,y,z,c,I,T) \
1322 1323 cimg_for5((img)._height,y) for (int x = 0, \
1323 1324 _p2##x = 0, _p1##x = 0, \
1324   - _n1##x = 1>=(img)._width?(img).width()-1:1, \
  1325 + _n1##x = 1>=(img)._width?(img).width() - 1:1, \
1325 1326 _n2##x = (int)( \
1326 1327 (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \
1327 1328 (I[5] = I[6] = I[7] = (T)(img)(0,_p1##y,z,c)), \
... ... @@ -1333,7 +1334,7 @@ extern &quot;C&quot; {
1333 1334 (I[13] = (T)(img)(_n1##x,y,z,c)), \
1334 1335 (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \
1335 1336 (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \
1336   - 2>=(img)._width?(img).width()-1:2); \
  1337 + 2>=(img)._width?(img).width() - 1:2); \
1337 1338 (_n2##x<(img).width() && ( \
1338 1339 (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
1339 1340 (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \
... ... @@ -1350,9 +1351,9 @@ extern &quot;C&quot; {
1350 1351  
1351 1352 #define cimg_for_in5x5(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1352 1353 cimg_for_in5((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1353   - _p2##x = x-2<0?0:x-2, \
1354   - _p1##x = x-1<0?0:x-1, \
1355   - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \
  1354 + _p2##x = x - 2<0?0:x - 2, \
  1355 + _p1##x = x - 1<0?0:x - 1, \
  1356 + _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
1356 1357 _n2##x = (int)( \
1357 1358 (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \
1358 1359 (I[5] = (T)(img)(_p2##x,_p1##y,z,c)), \
... ... @@ -1374,7 +1375,7 @@ extern &quot;C&quot; {
1374 1375 (I[13] = (T)(img)(_n1##x,y,z,c)), \
1375 1376 (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \
1376 1377 (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \
1377   - x+2>=(int)(img)._width?(img).width()-1:x+2); \
  1378 + x + 2>=(int)(img)._width?(img).width() - 1:x + 2); \
1378 1379 x<=(int)(x1) && ((_n2##x<(img).width() && ( \
1379 1380 (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
1380 1381 (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \
... ... @@ -1392,8 +1393,8 @@ extern &quot;C&quot; {
1392 1393 #define cimg_for6x6(img,x,y,z,c,I,T) \
1393 1394 cimg_for6((img)._height,y) for (int x = 0, \
1394 1395 _p2##x = 0, _p1##x = 0, \
1395   - _n1##x = 1>=(img)._width?(img).width()-1:1, \
1396   - _n2##x = 2>=(img)._width?(img).width()-1:2, \
  1396 + _n1##x = 1>=(img)._width?(img).width() - 1:1, \
  1397 + _n2##x = 2>=(img)._width?(img).width() - 1:2, \
1397 1398 _n3##x = (int)( \
1398 1399 (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \
1399 1400 (I[6] = I[7] = I[8] = (T)(img)(0,_p1##y,z,c)), \
... ... @@ -1413,7 +1414,7 @@ extern &quot;C&quot; {
1413 1414 (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \
1414 1415 (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \
1415 1416 (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \
1416   - 3>=(img)._width?(img).width()-1:3); \
  1417 + 3>=(img)._width?(img).width() - 1:3); \
1417 1418 (_n3##x<(img).width() && ( \
1418 1419 (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \
1419 1420 (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \
... ... @@ -1432,10 +1433,10 @@ extern &quot;C&quot; {
1432 1433  
1433 1434 #define cimg_for_in6x6(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1434 1435 cimg_for_in6((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)x0, \
1435   - _p2##x = x-2<0?0:x-2, \
1436   - _p1##x = x-1<0?0:x-1, \
1437   - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \
1438   - _n2##x = x+2>=(int)(img)._width?(img).width()-1:x+2, \
  1436 + _p2##x = x - 2<0?0:x - 2, \
  1437 + _p1##x = x - 1<0?0:x - 1, \
  1438 + _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
  1439 + _n2##x = x + 2>=(int)(img)._width?(img).width() - 1:x + 2, \
1439 1440 _n3##x = (int)( \
1440 1441 (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \
1441 1442 (I[6] = (T)(img)(_p2##x,_p1##y,z,c)), \
... ... @@ -1467,7 +1468,7 @@ extern &quot;C&quot; {
1467 1468 (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \
1468 1469 (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \
1469 1470 (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \
1470   - x+3>=(int)(img)._width?(img).width()-1:x+3); \
  1471 + x + 3>=(int)(img)._width?(img).width() - 1:x + 3); \
1471 1472 x<=(int)(x1) && ((_n3##x<(img).width() && ( \
1472 1473 (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \
1473 1474 (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \
... ... @@ -1487,8 +1488,8 @@ extern &quot;C&quot; {
1487 1488 #define cimg_for7x7(img,x,y,z,c,I,T) \
1488 1489 cimg_for7((img)._height,y) for (int x = 0, \
1489 1490 _p3##x = 0, _p2##x = 0, _p1##x = 0, \
1490   - _n1##x = 1>=(img)._width?(img).width()-1:1, \
1491   - _n2##x = 2>=(img)._width?(img).width()-1:2, \
  1491 + _n1##x = 1>=(img)._width?(img).width() - 1:1, \
  1492 + _n2##x = 2>=(img)._width?(img).width() - 1:2, \
1492 1493 _n3##x = (int)( \
1493 1494 (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \
1494 1495 (I[7] = I[8] = I[9] = I[10] = (T)(img)(0,_p2##y,z,c)), \
... ... @@ -1511,7 +1512,7 @@ extern &quot;C&quot; {
1511 1512 (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \
1512 1513 (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \
1513 1514 (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \
1514   - 3>=(img)._width?(img).width()-1:3); \
  1515 + 3>=(img)._width?(img).width() - 1:3); \
1515 1516 (_n3##x<(img).width() && ( \
1516 1517 (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
1517 1518 (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \
... ... @@ -1532,11 +1533,11 @@ extern &quot;C&quot; {
1532 1533  
1533 1534 #define cimg_for_in7x7(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1534 1535 cimg_for_in7((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1535   - _p3##x = x-3<0?0:x-3, \
1536   - _p2##x = x-2<0?0:x-2, \
1537   - _p1##x = x-1<0?0:x-1, \
1538   - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \
1539   - _n2##x = x+2>=(int)(img)._width?(img).width()-1:x+2, \
  1536 + _p3##x = x - 3<0?0:x - 3, \
  1537 + _p2##x = x - 2<0?0:x - 2, \
  1538 + _p1##x = x - 1<0?0:x - 1, \
  1539 + _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
  1540 + _n2##x = x + 2>=(int)(img)._width?(img).width() - 1:x + 2, \
1540 1541 _n3##x = (int)( \
1541 1542 (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \
1542 1543 (I[7] = (T)(img)(_p3##x,_p2##y,z,c)), \
... ... @@ -1580,7 +1581,7 @@ extern &quot;C&quot; {
1580 1581 (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \
1581 1582 (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \
1582 1583 (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \
1583   - x+3>=(int)(img)._width?(img).width()-1:x+3); \
  1584 + x + 3>=(int)(img)._width?(img).width() - 1:x + 3); \
1584 1585 x<=(int)(x1) && ((_n3##x<(img).width() && ( \
1585 1586 (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
1586 1587 (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \
... ... @@ -1602,9 +1603,9 @@ extern &quot;C&quot; {
1602 1603 #define cimg_for8x8(img,x,y,z,c,I,T) \
1603 1604 cimg_for8((img)._height,y) for (int x = 0, \
1604 1605 _p3##x = 0, _p2##x = 0, _p1##x = 0, \
1605   - _n1##x = 1>=((img)._width)?(img).width()-1:1, \
1606   - _n2##x = 2>=((img)._width)?(img).width()-1:2, \
1607   - _n3##x = 3>=((img)._width)?(img).width()-1:3, \
  1606 + _n1##x = 1>=((img)._width)?(img).width() - 1:1, \
  1607 + _n2##x = 2>=((img)._width)?(img).width() - 1:2, \
  1608 + _n3##x = 3>=((img)._width)?(img).width() - 1:3, \
1608 1609 _n4##x = (int)( \
1609 1610 (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \
1610 1611 (I[8] = I[9] = I[10] = I[11] = (T)(img)(0,_p2##y,z,c)), \
... ... @@ -1638,7 +1639,7 @@ extern &quot;C&quot; {
1638 1639 (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \
1639 1640 (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \
1640 1641 (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \
1641   - 4>=((img)._width)?(img).width()-1:4); \
  1642 + 4>=((img)._width)?(img).width() - 1:4); \
1642 1643 (_n4##x<(img).width() && ( \
1643 1644 (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \
1644 1645 (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \
... ... @@ -1661,12 +1662,12 @@ extern &quot;C&quot; {
1661 1662  
1662 1663 #define cimg_for_in8x8(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1663 1664 cimg_for_in8((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1664   - _p3##x = x-3<0?0:x-3, \
1665   - _p2##x = x-2<0?0:x-2, \
1666   - _p1##x = x-1<0?0:x-1, \
1667   - _n1##x = x+1>=(img).width()?(img).width()-1:x+1, \
1668   - _n2##x = x+2>=(img).width()?(img).width()-1:x+2, \
1669   - _n3##x = x+3>=(img).width()?(img).width()-1:x+3, \
  1665 + _p3##x = x - 3<0?0:x - 3, \
  1666 + _p2##x = x - 2<0?0:x - 2, \
  1667 + _p1##x = x - 1<0?0:x - 1, \
  1668 + _n1##x = x + 1>=(img).width()?(img).width() - 1:x + 1, \
  1669 + _n2##x = x + 2>=(img).width()?(img).width() - 1:x + 2, \
  1670 + _n3##x = x + 3>=(img).width()?(img).width() - 1:x + 3, \
1670 1671 _n4##x = (int)( \
1671 1672 (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \
1672 1673 (I[8] = (T)(img)(_p3##x,_p2##y,z,c)), \
... ... @@ -1724,7 +1725,7 @@ extern &quot;C&quot; {
1724 1725 (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \
1725 1726 (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \
1726 1727 (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \
1727   - x+4>=(img).width()?(img).width()-1:x+4); \
  1728 + x + 4>=(img).width()?(img).width() - 1:x + 4); \
1728 1729 x<=(int)(x1) && ((_n4##x<(img).width() && ( \
1729 1730 (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \
1730 1731 (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \
... ... @@ -1748,9 +1749,9 @@ extern &quot;C&quot; {
1748 1749 #define cimg_for9x9(img,x,y,z,c,I,T) \
1749 1750 cimg_for9((img)._height,y) for (int x = 0, \
1750 1751 _p4##x = 0, _p3##x = 0, _p2##x = 0, _p1##x = 0, \
1751   - _n1##x = 1>=((img)._width)?(img).width()-1:1, \
1752   - _n2##x = 2>=((img)._width)?(img).width()-1:2, \
1753   - _n3##x = 3>=((img)._width)?(img).width()-1:3, \
  1752 + _n1##x = 1>=((img)._width)?(img).width() - 1:1, \
  1753 + _n2##x = 2>=((img)._width)?(img).width() - 1:2, \
  1754 + _n3##x = 3>=((img)._width)?(img).width() - 1:3, \
1754 1755 _n4##x = (int)( \
1755 1756 (I[0] = I[1] = I[2] = I[3] = I[4] = (T)(img)(_p4##x,_p4##y,z,c)), \
1756 1757 (I[9] = I[10] = I[11] = I[12] = I[13] = (T)(img)(0,_p3##y,z,c)), \
... ... @@ -1788,7 +1789,7 @@ extern &quot;C&quot; {
1788 1789 (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \
1789 1790 (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \
1790 1791 (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \
1791   - 4>=((img)._width)?(img).width()-1:4); \
  1792 + 4>=((img)._width)?(img).width() - 1:4); \
1792 1793 (_n4##x<(img).width() && ( \
1793 1794 (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \
1794 1795 (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \
... ... @@ -1815,13 +1816,13 @@ extern &quot;C&quot; {
1815 1816  
1816 1817 #define cimg_for_in9x9(img,x0,y0,x1,y1,x,y,z,c,I,T) \
1817 1818 cimg_for_in9((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1818   - _p4##x = x-4<0?0:x-4, \
1819   - _p3##x = x-3<0?0:x-3, \
1820   - _p2##x = x-2<0?0:x-2, \
1821   - _p1##x = x-1<0?0:x-1, \
1822   - _n1##x = x+1>=(img).width()?(img).width()-1:x+1, \
1823   - _n2##x = x+2>=(img).width()?(img).width()-1:x+2, \
1824   - _n3##x = x+3>=(img).width()?(img).width()-1:x+3, \
  1819 + _p4##x = x - 4<0?0:x - 4, \
  1820 + _p3##x = x - 3<0?0:x - 3, \
  1821 + _p2##x = x - 2<0?0:x - 2, \
  1822 + _p1##x = x - 1<0?0:x - 1, \
  1823 + _n1##x = x + 1>=(img).width()?(img).width() - 1:x + 1, \
  1824 + _n2##x = x + 2>=(img).width()?(img).width() - 1:x + 2, \
  1825 + _n3##x = x + 3>=(img).width()?(img).width() - 1:x + 3, \
1825 1826 _n4##x = (int)( \
1826 1827 (I[0] = (T)(img)(_p4##x,_p4##y,z,c)), \
1827 1828 (I[9] = (T)(img)(_p4##x,_p3##y,z,c)), \
... ... @@ -1895,7 +1896,7 @@ extern &quot;C&quot; {
1895 1896 (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \
1896 1897 (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \
1897 1898 (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \
1898   - x+4>=(img).width()?(img).width()-1:x+4); \
  1899 + x + 4>=(img).width()?(img).width() - 1:x + 4); \
1899 1900 x<=(int)(x1) && ((_n4##x<(img).width() && ( \
1900 1901 (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \
1901 1902 (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \
... ... @@ -1927,7 +1928,7 @@ extern &quot;C&quot; {
1927 1928 (I[2] = (T)(img)(0,_n1##y,z,c)), \
1928 1929 (I[4] = (T)(img)(0,y,_n1##z,c)), \
1929 1930 (I[6] = (T)(img)(0,_n1##y,_n1##z,c)), \
1930   - 1>=(img)._width?(img).width()-1:1); \
  1931 + 1>=(img)._width?(img).width() - 1:1); \
1931 1932 (_n1##x<(img).width() && ( \
1932 1933 (I[1] = (T)(img)(_n1##x,y,z,c)), \
1933 1934 (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \
... ... @@ -1944,7 +1945,7 @@ extern &quot;C&quot; {
1944 1945 (I[2] = (T)(img)(x,_n1##y,z,c)), \
1945 1946 (I[4] = (T)(img)(x,y,_n1##z,c)), \
1946 1947 (I[6] = (T)(img)(x,_n1##y,_n1##z,c)), \
1947   - x+1>=(int)(img)._width?(img).width()-1:x+1); \
  1948 + x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
1948 1949 x<=(int)(x1) && ((_n1##x<(img).width() && ( \
1949 1950 (I[1] = (T)(img)(_n1##x,y,z,c)), \
1950 1951 (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \
... ... @@ -1967,7 +1968,7 @@ extern &quot;C&quot; {
1967 1968 (I[18] = I[19] = (T)(img)(0,_p1##y,_n1##z,c)), \
1968 1969 (I[21] = I[22] = (T)(img)(0,y,_n1##z,c)), \
1969 1970 (I[24] = I[25] = (T)(img)(0,_n1##y,_n1##z,c)), \
1970   - 1>=(img)._width?(img).width()-1:1); \
  1971 + 1>=(img)._width?(img).width() - 1:1); \
1971 1972 (_n1##x<(img).width() && ( \
1972 1973 (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \
1973 1974 (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \
... ... @@ -1986,7 +1987,7 @@ extern &quot;C&quot; {
1986 1987  
1987 1988 #define cimg_for_in3x3x3(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \
1988 1989 cimg_for_in3((img)._depth,z0,z1,z) cimg_for_in3((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
1989   - _p1##x = x-1<0?0:x-1, \
  1990 + _p1##x = x - 1<0?0:x - 1, \
1990 1991 _n1##x = (int)( \
1991 1992 (I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \
1992 1993 (I[3] = (T)(img)(_p1##x,y,_p1##z,c)), \
... ... @@ -2006,7 +2007,7 @@ extern &quot;C&quot; {
2006 2007 (I[19] = (T)(img)(x,_p1##y,_n1##z,c)), \
2007 2008 (I[22] = (T)(img)(x,y,_n1##z,c)), \
2008 2009 (I[25] = (T)(img)(x,_n1##y,_n1##z,c)), \
2009   - x+1>=(int)(img)._width?(img).width()-1:x+1); \
  2010 + x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
2010 2011 x<=(int)(x1) && ((_n1##x<(img).width() && ( \
2011 2012 (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \
2012 2013 (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \
... ... @@ -2025,7 +2026,7 @@ extern &quot;C&quot; {
2025 2026  
2026 2027 #define cimglist_for(list,l) for (int l = 0; l<(int)(list)._width; ++l)
2027 2028 #define cimglist_for_in(list,l0,l1,l) \
2028   - for (int l = (int)(l0)<0?0:(int)(l0), _max##l = (unsigned int)l1<(list)._width?(int)(l1):(int)(list)._width-1; \
  2029 + for (int l = (int)(l0)<0?0:(int)(l0), _max##l = (unsigned int)l1<(list)._width?(int)(l1):(int)(list)._width - 1; \
2029 2030 l<=_max##l; ++l)
2030 2031  
2031 2032 #define cimglist_apply(list,fn) cimglist_for(list,__##fn) (list)[__##fn].fn
... ... @@ -2108,7 +2109,7 @@ namespace cimg_library_suffixed {
2108 2109  
2109 2110 // [internal] Lock/unlock a mutex for managing concurrent threads.
2110 2111 // 'lock_mode' can be { 0=unlock | 1=lock | 2=trylock }.
2111   - // 'n' can be in [0,31] but mutex range [0,16] is reserved by CImg.
  2112 + // 'n' can be in [0,31] but mutex range [0,15] is reserved by CImg.
2112 2113 inline int mutex(const unsigned int n, const int lock_mode=1);
2113 2114  
2114 2115 inline unsigned int& _exception_mode(const unsigned int value, const bool is_set) {
... ... @@ -2119,6 +2120,27 @@ namespace cimg_library_suffixed {
2119 2120 return mode;
2120 2121 }
2121 2122  
  2123 + // Mandatory because Microsoft's _snprintf() and _vsnprintf() do not add the '\0' character
  2124 + // at the end of the string.
  2125 +#if cimg_OS==2 && defined(_MSC_VER)
  2126 + inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap) {
  2127 + int count = -1;
  2128 + cimg::mutex(6);
  2129 + if (size) count = _vsnprintf_s(str,size,_TRUNCATE,format,ap);
  2130 + if (count==-1) count = _vscprintf(format,ap);
  2131 + cimg::mutex(6,0);
  2132 + return count;
  2133 + }
  2134 + inline int c99_snprintf(char* str, size_t size, const char* format, ...) {
  2135 + int count;
  2136 + va_list ap;
  2137 + va_start(ap, format);
  2138 + count = c99_vsnprintf(str,size,format,ap);
  2139 + va_end(ap);
  2140 + return count;
  2141 + }
  2142 +#endif
  2143 +
2122 2144 //! Set current \CImg exception mode.
2123 2145 /**
2124 2146 The way error messages are handled by \CImg can be changed dynamically, using this function.
... ... @@ -2141,11 +2163,13 @@ namespace cimg_library_suffixed {
2141 2163 return _exception_mode(0,false);
2142 2164 }
2143 2165  
  2166 + // Display a simple dialog box, and wait for the user's response.
2144 2167 inline int dialog(const char *const title, const char *const msg, const char *const button1_label="OK",
2145 2168 const char *const button2_label=0, const char *const button3_label=0,
2146 2169 const char *const button4_label=0, const char *const button5_label=0,
2147 2170 const char *const button6_label=0, const bool centering=false);
2148 2171  
  2172 + // Evaluate math expression.
2149 2173 inline double eval(const char *const expression,
2150 2174 const double x=0, const double y=0, const double z=0, const double c=0);
2151 2175 }
... ... @@ -2219,7 +2243,7 @@ namespace cimg_library_suffixed {
2219 2243 **/
2220 2244 struct CImgException : public std::exception {
2221 2245 #define _cimg_exception_err(etype,disp_flag) \
2222   - std::va_list ap; va_start(ap,format); cimg_vsnprintf(_message,sizeof(_message),format,ap); va_end(ap); \
  2246 + std::va_list ap; va_start(ap,format); cimg_vsnprintf(_message,16384,format,ap); va_end(ap); \
2223 2247 if (cimg::exception_mode()) { \
2224 2248 std::fprintf(cimg::output(),"\n%s[CImg] *** %s ***%s %s\n",cimg::t_red,etype,cimg::t_normal,_message); \
2225 2249 if (cimg_display && disp_flag && !(cimg::exception_mode()%2)) try { cimg::dialog(etype,_message,"Abort"); } \
... ... @@ -2227,9 +2251,12 @@ namespace cimg_library_suffixed {
2227 2251 if (cimg::exception_mode()>=3) cimg_library_suffixed::cimg::info(); \
2228 2252 }
2229 2253  
2230   - char _message[16384];
2231   - CImgException() { *_message = 0; }
2232   - CImgException(const char *const format, ...) { _cimg_exception_err("CImgException",true); }
  2254 + char *_message;
  2255 + CImgException() { _message = new char[16384]; *_message = 0; }
  2256 + CImgException(const char *const format, ...) {
  2257 + _message = new char[16384]; *_message = 0; _cimg_exception_err("CImgException",true);
  2258 + }
  2259 + ~CImgException() throw() { delete[] _message; }
2233 2260 //! Return a C-string containing the error message associated to the thrown exception.
2234 2261 const char *what() const throw() { return _message; }
2235 2262 };
... ... @@ -2293,12 +2320,12 @@ namespace cimg_library_suffixed {
2293 2320 static bool is_float() { return false; }
2294 2321 static bool is_inf(const T) { return false; }
2295 2322 static bool is_nan(const T) { return false; }
2296   - static T min() { return (T)-1>0?(T)0:(T)-1<<(8*sizeof(T)-1); }
2297   - static T max() { return (T)-1>0?(T)-1:~((T)-1<<(8*sizeof(T)-1)); }
  2323 + static T min() { return ~max(); }
  2324 + static T max() { return (T)(1UL<<(8*sizeof(T) - 1)); }
2298 2325 static T inf() { return max(); }
2299 2326 static T cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(T)val; }
2300 2327 static const char* format() { return "%s"; }
2301   - static const char* format(const T val) { static const char *const s = "unknown"; cimg::unused(val); return s; }
  2328 + static const char* format(const T& val) { static const char *const s = "unknown"; cimg::unused(val); return s; }
2302 2329 };
2303 2330  
2304 2331 template<> struct type<bool> {
... ... @@ -2321,7 +2348,7 @@ namespace cimg_library_suffixed {
2321 2348 static bool is_inf(const unsigned char) { return false; }
2322 2349 static bool is_nan(const unsigned char) { return false; }
2323 2350 static unsigned char min() { return 0; }
2324   - static unsigned char max() { return (unsigned char)~0U; }
  2351 + static unsigned char max() { return (unsigned char)-1; }
2325 2352 static unsigned char inf() { return max(); }
2326 2353 static unsigned char cut(const double val) {
2327 2354 return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; }
... ... @@ -2329,31 +2356,47 @@ namespace cimg_library_suffixed {
2329 2356 static unsigned int format(const unsigned char val) { return (unsigned int)val; }
2330 2357 };
2331 2358  
  2359 +#if defined(CHAR_MAX) && CHAR_MAX==255
  2360 + template<> struct type<char> {
  2361 + static const char* string() { static const char *const s = "char"; return s; }
  2362 + static bool is_float() { return false; }
  2363 + static bool is_inf(const char) { return false; }
  2364 + static bool is_nan(const char) { return false; }
  2365 + static char min() { return 0; }
  2366 + static char max() { return (char)-1; }
  2367 + static char inf() { return max(); }
  2368 + static char cut(const double val) {
  2369 + return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; }
  2370 + static const char* format() { return "%u"; }
  2371 + static unsigned int format(const char val) { return (unsigned int)val; }
  2372 + };
  2373 +#else
2332 2374 template<> struct type<char> {
2333 2375 static const char* string() { static const char *const s = "char"; return s; }
2334 2376 static bool is_float() { return false; }
2335 2377 static bool is_inf(const char) { return false; }
2336 2378 static bool is_nan(const char) { return false; }
2337   - static char min() { return (char)(-1L<<(8*sizeof(char)-1)); }
2338   - static char max() { return (char)~((char)(-1L<<(8*sizeof(char)-1))); }
  2379 + static char min() { return ~max(); }
  2380 + static char max() { return (char)((unsigned char)-1>>1); }
2339 2381 static char inf() { return max(); }
2340 2382 static char cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(char)val; }
2341 2383 static const char* format() { return "%d"; }
2342 2384 static int format(const char val) { return (int)val; }
2343 2385 };
  2386 +#endif
2344 2387  
2345 2388 template<> struct type<signed char> {
2346 2389 static const char* string() { static const char *const s = "signed char"; return s; }
2347 2390 static bool is_float() { return false; }
2348 2391 static bool is_inf(const signed char) { return false; }
2349 2392 static bool is_nan(const signed char) { return false; }
2350   - static signed char min() { return (signed char)(-1L<<(8*sizeof(signed char)-1)); }
2351   - static signed char max() { return ~((signed char)(-1L<<(8*sizeof(signed char)-1))); }
  2393 + static signed char min() { return ~max(); }
  2394 + static signed char max() { return (signed char)((unsigned char)-1>>1); }
2352 2395 static signed char inf() { return max(); }
2353 2396 static signed char cut(const double val) {
2354 2397 return val<(double)min()?min():val>(double)max()?max():(signed char)val; }
2355 2398 static const char* format() { return "%d"; }
2356   - static unsigned int format(const signed char val) { return (int)val; }
  2399 + static int format(const signed char val) { return (int)val; }
2357 2400 };
2358 2401  
2359 2402 template<> struct type<unsigned short> {
... ... @@ -2362,7 +2405,7 @@ namespace cimg_library_suffixed {
2362 2405 static bool is_inf(const unsigned short) { return false; }
2363 2406 static bool is_nan(const unsigned short) { return false; }
2364 2407 static unsigned short min() { return 0; }
2365   - static unsigned short max() { return (unsigned short)~0U; }
  2408 + static unsigned short max() { return (unsigned short)-1; }
2366 2409 static unsigned short inf() { return max(); }
2367 2410 static unsigned short cut(const double val) {
2368 2411 return val<(double)min()?min():val>(double)max()?max():(unsigned short)val; }
... ... @@ -2375,8 +2418,8 @@ namespace cimg_library_suffixed {
2375 2418 static bool is_float() { return false; }
2376 2419 static bool is_inf(const short) { return false; }
2377 2420 static bool is_nan(const short) { return false; }
2378   - static short min() { return (short)(-1L<<(8*sizeof(short)-1)); }
2379   - static short max() { return ~((short)(-1L<<(8*sizeof(short)-1))); }
  2421 + static short min() { return ~max(); }
  2422 + static short max() { return (short)((unsigned short)-1>>1); }
2380 2423 static short inf() { return max(); }
2381 2424 static short cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(short)val; }
2382 2425 static const char* format() { return "%d"; }
... ... @@ -2389,7 +2432,7 @@ namespace cimg_library_suffixed {
2389 2432 static bool is_inf(const unsigned int) { return false; }
2390 2433 static bool is_nan(const unsigned int) { return false; }
2391 2434 static unsigned int min() { return 0; }
2392   - static unsigned int max() { return (unsigned int)~0U; }
  2435 + static unsigned int max() { return (unsigned int)-1; }
2393 2436 static unsigned int inf() { return max(); }
2394 2437 static unsigned int cut(const double val) {
2395 2438 return val<(double)min()?min():val>(double)max()?max():(unsigned int)val; }
... ... @@ -2402,8 +2445,8 @@ namespace cimg_library_suffixed {
2402 2445 static bool is_float() { return false; }
2403 2446 static bool is_inf(const int) { return false; }
2404 2447 static bool is_nan(const int) { return false; }
2405   - static int min() { return (int)(-1L<<(8*sizeof(int)-1)); }
2406   - static int max() { return ~((int)(-1L<<(8*sizeof(int)-1))); }
  2448 + static int min() { return ~max(); }
  2449 + static int max() { return (int)((unsigned int)-1>>1); }
2407 2450 static int inf() { return max(); }
2408 2451 static int cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(int)val; }
2409 2452 static const char* format() { return "%d"; }
... ... @@ -2416,7 +2459,7 @@ namespace cimg_library_suffixed {
2416 2459 static bool is_inf(const unsigned long) { return false; }
2417 2460 static bool is_nan(const unsigned long) { return false; }
2418 2461 static unsigned long min() { return 0; }
2419   - static unsigned long max() { return (unsigned long)~0UL; }
  2462 + static unsigned long max() { return (unsigned long)-1; }
2420 2463 static unsigned long inf() { return max(); }
2421 2464 static unsigned long cut(const double val) {
2422 2465 return val<(double)min()?min():val>(double)max()?max():(unsigned long)val; }
... ... @@ -2429,8 +2472,8 @@ namespace cimg_library_suffixed {
2429 2472 static bool is_float() { return false; }
2430 2473 static bool is_inf(const long) { return false; }
2431 2474 static bool is_nan(const long) { return false; }
2432   - static long min() { return (long)(-1L<<(8*sizeof(long)-1)); }
2433   - static long max() { return ~((long)(-1L<<(8*sizeof(long)-1))); }
  2475 + static long min() { return ~max(); }
  2476 + static long max() { return (long)((unsigned long)-1>>1); }
2434 2477 static long inf() { return max(); }
2435 2478 static long cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(long)val; }
2436 2479 static const char* format() { return "%ld"; }
... ... @@ -2457,7 +2500,7 @@ namespace cimg_library_suffixed {
2457 2500 static double min() { return -1.7E308; }
2458 2501 static double max() { return 1.7E308; }
2459 2502 static double inf() { return max()*max(); }
2460   - static double nan() { static const double val_nan = -std::sqrt(-1.0); return val_nan; }
  2503 + static double nan() { const double val_nan = -std::sqrt(-1.0); return val_nan; }
2461 2504 static double cut(const double val) { return val<min()?min():val>max()?max():val; }
2462 2505 static const char* format() { return "%.16g"; }
2463 2506 static double format(const double val) { return val; }
... ... @@ -2589,16 +2632,16 @@ namespace cimg_library_suffixed {
2589 2632 // Define variables used internally by CImg.
2590 2633 #if cimg_display==1
2591 2634 struct X11_info {
2592   - volatile unsigned int nb_wins;
2593   - pthread_t* events_thread;
2594   - pthread_cond_t wait_event;
2595   - pthread_mutex_t wait_event_mutex;
2596   - CImgDisplay* wins[1024];
2597   - Display* display;
2598   - unsigned int nb_bits;
2599   - bool is_blue_first;
2600   - bool is_shm_enabled;
2601   - bool byte_order;
  2635 + unsigned int nb_wins;
  2636 + pthread_t *events_thread;
  2637 + pthread_cond_t wait_event;
  2638 + pthread_mutex_t wait_event_mutex;
  2639 + CImgDisplay **wins;
  2640 + Display *display;
  2641 + unsigned int nb_bits;
  2642 + bool is_blue_first;
  2643 + bool is_shm_enabled;
  2644 + bool byte_order;
2602 2645 #ifdef cimg_use_xrandr
2603 2646 XRRScreenSize *resolutions;
2604 2647 Rotation curr_rotation;
... ... @@ -2607,7 +2650,10 @@ namespace cimg_library_suffixed {
2607 2650 #endif
2608 2651 X11_info():nb_wins(0),events_thread(0),display(0),
2609 2652 nb_bits(0),is_blue_first(false),is_shm_enabled(false),byte_order(false) {
  2653 +#ifdef __FreeBSD__
2610 2654 XInitThreads();
  2655 +#endif
  2656 + wins = new CImgDisplay*[1024];
2611 2657 pthread_mutex_init(&wait_event_mutex,0);
2612 2658 pthread_cond_init(&wait_event,0);
2613 2659 #ifdef cimg_use_xrandr
... ... @@ -2618,14 +2664,17 @@ namespace cimg_library_suffixed {
2618 2664 }
2619 2665  
2620 2666 ~X11_info() {
2621   - if (events_thread) {
  2667 + delete[] wins;
  2668 + /*
  2669 + if (events_thread) {
2622 2670 pthread_cancel(*events_thread);
2623 2671 delete events_thread;
2624   - }
2625   - if (display) {} // XCloseDisplay(display);
2626   - pthread_cond_destroy(&wait_event);
2627   - pthread_mutex_unlock(&wait_event_mutex);
2628   - pthread_mutex_destroy(&wait_event_mutex);
  2672 + }
  2673 + if (display) { } // XCloseDisplay(display); }
  2674 + pthread_cond_destroy(&wait_event);
  2675 + pthread_mutex_unlock(&wait_event_mutex);
  2676 + pthread_mutex_destroy(&wait_event_mutex);
  2677 + */
2629 2678 }
2630 2679 };
2631 2680 #if defined(cimg_module)
... ... @@ -2635,6 +2684,8 @@ namespace cimg_library_suffixed {
2635 2684 #else
2636 2685 inline X11_info& X11_attr() { static X11_info val; return val; }
2637 2686 #endif
  2687 +#define cimg_lock_display() cimg::mutex(15)
  2688 +#define cimg_unlock_display() cimg::mutex(15,0)
2638 2689  
2639 2690 #elif cimg_display==2
2640 2691 struct Win32_info {
... ... @@ -3820,16 +3871,33 @@ namespace cimg_library_suffixed {
3820 3871 123,123,0,6,255,255,0,1,0,0,0,2,123,123,123,18,200,200,200,1,123,123,0,7,255,255,0,1,189,189,189,3,0,0,0,1,189,
3821 3872 189,189,6,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,17,200,200,200,1,123,123,0,8,255,255,0,3,0,0,0,8,255,255,
3822 3873 0,1,0,0,0,2,123,123,123,16,200,200,200,1,123,123,0,9,255,255,0,1,123,123,0,1,0,0,0,1,123,123,0,8,255,255,0,1,189,
3823   - 189,189,1,0,0,0,2,123,123,123,15,200,200,200,1,123,123,0,9,255,255,0,1,189,189,189,1,0,0,0,1,189,189,189,9,255,255,
3824   - 0,1,0,0,0,2,123,123,123,14,200,200,200,1,123,123,0,11,255,255,0,1,0,0,0,10,255,255,0,1,189,189,189,1,0,0,0,2,123,
3825   - 123,123,13,200,200,200,1,123,123,0,23,255,255,0,1,0,0,0,2,123,123,123,12,200,200,200,1,123,123,0,11,255,255,0,1,189,
3826   - 189,189,2,0,0,0,1,189,189,189,9,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,11,200,200,200,1,123,123,0,11,255,255,
3827   - 0,4,0,0,0,10,255,255,0,1,0,0,0,2,123,123,123,10,200,200,200,1,123,123,0,12,255,255,0,4,0,0,0,10,255,255,0,1,189,189,
3828   - 189,1,0,0,0,2,123,123,123,9,200,200,200,1,123,123,0,12,255,255,0,1,189,189,189,2,0,0,0,1,189,189,189,11,255,255,0,1,
3829   - 0,0,0,2,123,123,123,9,200,200,200,1,123,123,0,27,255,255,0,1,0,0,0,3,123,123,123,8,200,200,200,1,123,123,0,26,255,
3830   - 255,0,1,189,189,189,1,0,0,0,3,123,123,123,9,200,200,200,1,123,123,0,24,255,255,0,1,189,189,189,1,0,0,0,4,123,123,
3831   - 123,10,200,200,200,1,123,123,0,24,0,0,0,5,123,123,123,12,200,200,200,27,123,123,123,14,200,200,200,25,123,123,123,86,
3832   - 200,200,200,91,49,124,118,124,71,32,124,95,49,56,114,52,82,121,0};
  3874 + 189,189,1,0,0,0,2,123,123,123,15,200,200,200,1,123,123,0,9,255,255,0,1,189,189,189,1,0,0,0,1,189,189,189,9,255,
  3875 + 255,0,1,0,0,0,2,123,123,123,14,200,200,200,1,123,123,0,11,255,255,0,1,0,0,0,10,255,255,0,1,189,189,189,1,0,0,0,2,
  3876 + 123,123,123,13,200,200,200,1,123,123,0,23,255,255,0,1,0,0,0,2,123,123,123,12,200,200,200,1,123,123,0,11,255,255,0,
  3877 + 1,189,189,189,2,0,0,0,1,189,189,189,9,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,11,200,200,200,1,123,123,0,11,
  3878 + 255,255,0,4,0,0,0,10,255,255,0,1,0,0,0,2,123,123,123,10,200,200,200,1,123,123,0,12,255,255,0,4,0,0,0,10,255,255,0,
  3879 + 1,189,189,189,1,0,0,0,2,123,123,123,9,200,200,200,1,123,123,0,12,255,255,0,1,189,189,189,2,0,0,0,1,189,189,189,11,
  3880 + 255,255,0,1,0,0,0,2,123,123,123,9,200,200,200,1,123,123,0,27,255,255,0,1,0,0,0,3,123,123,123,8,200,200,200,1,123,
  3881 + 123,0,26,255,255,0,1,189,189,189,1,0,0,0,3,123,123,123,9,200,200,200,1,123,123,0,24,255,255,0,1,189,189,189,1,0,0,
  3882 + 0,4,123,123,123,10,200,200,200,1,123,123,0,24,0,0,0,5,123,123,123,12,200,200,200,27,123,123,123,14,200,200,200,25,
  3883 + 123,123,123,86,200,200,200,91,49,124,118,124,71,32,124,95,49,56,114,52,82,121,0 };
  3884 +
  3885 + // Mutex-protected version of sscanf.
  3886 + // Used only MacOSX, as it seems std::sscanf() is not re-entrant on MacOSX.
  3887 +#if (__MACOSX__) || defined(__APPLE__)
  3888 +#define cimg_sscanf cimg::_sscanf
  3889 + inline int _sscanf(const char *s, const char *format, ...) {
  3890 + cimg::mutex(13);
  3891 + va_list args;
  3892 + va_start(args, format);
  3893 + const int result = std::vsscanf(s,format,args);
  3894 + va_end(args);
  3895 + cimg::mutex(13,0);
  3896 + return result;
  3897 + }
  3898 +#else
  3899 +#define cimg_sscanf std::sscanf
  3900 +#endif
3833 3901  
3834 3902 //! Get/set default output stream for the \CImg library messages.
3835 3903 /**
... ... @@ -3883,16 +3951,17 @@ namespace cimg_library_suffixed {
3883 3951 **/
3884 3952 inline void warn(const char *const format, ...) {
3885 3953 if (cimg::exception_mode()>=1) {
3886   - char message[16384] = { 0 };
  3954 + char *const message = new char[16384];
3887 3955 std::va_list ap;
3888 3956 va_start(ap,format);
3889   - cimg_vsnprintf(message,sizeof(message),format,ap);
  3957 + cimg_vsnprintf(message,16384,format,ap);
3890 3958 va_end(ap);
3891 3959 #ifdef cimg_strict_warnings
3892 3960 throw CImgWarningException(message);
3893 3961 #else
3894 3962 std::fprintf(cimg::output(),"\n%s[CImg] *** Warning ***%s%s",cimg::t_red,cimg::t_normal,message);
3895 3963 #endif
  3964 + delete[] message;
3896 3965 }
3897 3966 }
3898 3967  
... ... @@ -3913,9 +3982,9 @@ namespace cimg_library_suffixed {
3913 3982 #if cimg_OS==1
3914 3983 const unsigned int l = std::strlen(command);
3915 3984 if (l) {
3916   - char *const ncommand = new char[l+16];
  3985 + char *const ncommand = new char[l + 16];
3917 3986 std::strncpy(ncommand,command,l);
3918   - std::strcpy(ncommand+l," 2> /dev/null"); // Make command silent.
  3987 + std::strcpy(ncommand + l," 2> /dev/null"); // Make command silent.
3919 3988 const int out_val = std::system(ncommand);
3920 3989 delete[] ncommand;
3921 3990 return out_val;
... ... @@ -4013,17 +4082,17 @@ namespace cimg_library_suffixed {
4013 4082 inline void invert_endianness(T* const buffer, const unsigned long size) {
4014 4083 if (size) switch (sizeof(T)) {
4015 4084 case 1 : break;
4016   - case 2 : { for (unsigned short *ptr = (unsigned short*)buffer+size; ptr>(unsigned short*)buffer; ) {
  4085 + case 2 : { for (unsigned short *ptr = (unsigned short*)buffer + size; ptr>(unsigned short*)buffer; ) {
4017 4086 const unsigned short val = *(--ptr);
4018 4087 *ptr = (unsigned short)((val>>8)|((val<<8)));
4019 4088 }
4020 4089 } break;
4021   - case 4 : { for (unsigned int *ptr = (unsigned int*)buffer+size; ptr>(unsigned int*)buffer; ) {
  4090 + case 4 : { for (unsigned int *ptr = (unsigned int*)buffer + size; ptr>(unsigned int*)buffer; ) {
4022 4091 const unsigned int val = *(--ptr);
4023 4092 *ptr = (val>>24)|((val>>8)&0xff00)|((val<<8)&0xff0000)|(val<<24);
4024 4093 }
4025 4094 } break;
4026   - default : { for (T* ptr = buffer+size; ptr>buffer; ) {
  4095 + default : { for (T* ptr = buffer + size; ptr>buffer; ) {
4027 4096 unsigned char *pb = (unsigned char*)(--ptr), *pe = pb + sizeof(T);
4028 4097 for (int i = 0; i<(int)sizeof(T)/2; ++i) swap(*(pb++),*(--pe));
4029 4098 }
... ... @@ -4073,7 +4142,7 @@ namespace cimg_library_suffixed {
4073 4142 return (unsigned long)(st_time.tv_usec/1000 + st_time.tv_sec*1000);
4074 4143 #elif cimg_OS==2
4075 4144 SYSTEMTIME st_time;
4076   - GetSystemTime(&st_time);
  4145 + GetLocalTime(&st_time);
4077 4146 return (unsigned long)(st_time.wMilliseconds + 1000*(st_time.wSecond + 60*(st_time.wMinute + 60*st_time.wHour)));
4078 4147 #else
4079 4148 return 0;
... ... @@ -4119,7 +4188,7 @@ namespace cimg_library_suffixed {
4119 4188 inline unsigned int _wait(const unsigned int milliseconds, unsigned long& timer) {
4120 4189 if (!timer) timer = cimg::time();
4121 4190 const unsigned long current_time = cimg::time();
4122   - if (current_time>=timer+milliseconds) { timer = current_time; return 0; }
  4191 + if (current_time>=timer + milliseconds) { timer = current_time; return 0; }
4123 4192 const unsigned long time_diff = timer + milliseconds - current_time;
4124 4193 timer = current_time + time_diff;
4125 4194 cimg::sleep(time_diff);
... ... @@ -4162,9 +4231,9 @@ namespace cimg_library_suffixed {
4162 4231 inline void srand() {
4163 4232 const unsigned int t = (unsigned int)cimg::time();
4164 4233 #if cimg_OS==1
4165   - cimg::_rand(t+(unsigned int)getpid(),true);
  4234 + cimg::_rand(t + (unsigned int)getpid(),true);
4166 4235 #elif cimg_OS==2
4167   - cimg::_rand(t+(unsigned int)_getpid(),true);
  4236 + cimg::_rand(t + (unsigned int)_getpid(),true);
4168 4237 #else
4169 4238 cimg::_rand(t,true);
4170 4239 #endif
... ... @@ -4184,9 +4253,9 @@ namespace cimg_library_suffixed {
4184 4253 inline void srand() {
4185 4254 const unsigned int t = (unsigned int)cimg::time();
4186 4255 #if cimg_OS==1
4187   - std::srand(t+(unsigned int)getpid());
  4256 + std::srand(t + (unsigned int)getpid());
4188 4257 #elif cimg_OS==2
4189   - std::srand(t+(unsigned int)_getpid());
  4258 + std::srand(t + (unsigned int)_getpid());
4190 4259 #else
4191 4260 std::srand(t);
4192 4261 #endif
... ... @@ -4208,7 +4277,7 @@ namespace cimg_library_suffixed {
4208 4277 /**
4209 4278 **/
4210 4279 inline double crand() {
4211   - return 1-2*cimg::rand();
  4280 + return 1 - 2*cimg::rand();
4212 4281 }
4213 4282  
4214 4283 //! Return a random variable following a gaussian distribution and a standard deviation of 1.
... ... @@ -4218,7 +4287,7 @@ namespace cimg_library_suffixed {
4218 4287 double x1, w;
4219 4288 do {
4220 4289 const double x2 = 2*cimg::rand() - 1.0;
4221   - x1 = 2*cimg::rand()-1.0;
  4290 + x1 = 2*cimg::rand() - 1.0;
4222 4291 w = x1*x1 + x2*x2;
4223 4292 } while (w<=0 || w>=1.0);
4224 4293 return x1*std::sqrt((-2*std::log(w))/w);
... ... @@ -4233,13 +4302,13 @@ namespace cimg_library_suffixed {
4233 4302 unsigned int k = 0;
4234 4303 const double y = std::exp(-z);
4235 4304 for (double s = 1.0; s>=y; ++k) s*=cimg::rand();
4236   - return k-1;
  4305 + return k - 1;
4237 4306 }
4238 4307  
4239 4308 //! Bitwise-rotate value on the left.
4240 4309 template<typename T>
4241   - inline T rol(const T a, const unsigned int n=1) {
4242   - return n?(T)((a<<n)|(a>>((sizeof(T)<<3)-n))):a;
  4310 + inline T rol(const T& a, const unsigned int n=1) {
  4311 + return n?(T)((a<<n)|(a>>((sizeof(T)<<3) - n))):a;
4243 4312 }
4244 4313  
4245 4314 inline float rol(const float a, const unsigned int n=1) {
... ... @@ -4252,8 +4321,8 @@ namespace cimg_library_suffixed {
4252 4321  
4253 4322 //! Bitwise-rotate value on the right.
4254 4323 template<typename T>
4255   - inline T ror(const T a, const unsigned int n=1) {
4256   - return n?(T)((a>>n)|(a<<((sizeof(T)<<3)-n))):a;
  4324 + inline T ror(const T& a, const unsigned int n=1) {
  4325 + return n?(T)((a>>n)|(a<<((sizeof(T)<<3) - n))):a;
4257 4326 }
4258 4327  
4259 4328 inline float ror(const float a, const unsigned int n=1) {
... ... @@ -4266,7 +4335,7 @@ namespace cimg_library_suffixed {
4266 4335  
4267 4336 //! Return absolute value of a value.
4268 4337 template<typename T>
4269   - inline T abs(const T a) {
  4338 + inline T abs(const T& a) {
4270 4339 return a>=0?a:-a;
4271 4340 }
4272 4341 inline bool abs(const bool a) {
... ... @@ -4296,13 +4365,13 @@ namespace cimg_library_suffixed {
4296 4365  
4297 4366 //! Return square of a value.
4298 4367 template<typename T>
4299   - inline T sqr(const T val) {
  4368 + inline T sqr(const T& val) {
4300 4369 return val*val;
4301 4370 }
4302 4371  
4303 4372 //! Return <tt>1 + log_10(x)</tt> of a value \c x.
4304 4373 inline int xln(const int x) {
4305   - return x>0?(int)(1+std::log10((double)x)):1;
  4374 + return x>0?(int)(1 + std::log10((double)x)):1;
4306 4375 }
4307 4376  
4308 4377 //! Return the minimum between two values.
... ... @@ -4349,13 +4418,13 @@ namespace cimg_library_suffixed {
4349 4418  
4350 4419 //! Return the sign of a value.
4351 4420 template<typename T>
4352   - inline T sign(const T x) {
  4421 + inline T sign(const T& x) {
4353 4422 return (x<0)?(T)(-1):(x==0?(T)0:(T)1);
4354 4423 }
4355 4424  
4356 4425 //! Return the nearest power of 2 higher than given value.
4357 4426 template<typename T>
4358   - inline unsigned long nearest_pow2(const T x) {
  4427 + inline unsigned long nearest_pow2(const T& x) {
4359 4428 unsigned long i = 1;
4360 4429 while (x>i) i<<=1;
4361 4430 return i;
... ... @@ -4381,16 +4450,20 @@ namespace cimg_library_suffixed {
4381 4450 return m?(x?1:0):0;
4382 4451 }
4383 4452 inline int mod(const char x, const char m) {
4384   - return x>=0?x%m:(x%m?m+x%m:0);
  4453 +#if defined(CHAR_MAX) && CHAR_MAX==255
  4454 + return x%m;
  4455 +#else
  4456 + return x>=0?x%m:(x%m?m + x%m:0);
  4457 +#endif
4385 4458 }
4386 4459 inline int mod(const short x, const short m) {
4387   - return x>=0?x%m:(x%m?m+x%m:0);
  4460 + return x>=0?x%m:(x%m?m + x%m:0);
4388 4461 }
4389 4462 inline int mod(const int x, const int m) {
4390   - return x>=0?x%m:(x%m?m+x%m:0);
  4463 + return x>=0?x%m:(x%m?m + x%m:0);
4391 4464 }
4392 4465 inline int mod(const long x, const long m) {
4393   - return x>=0?x%m:(x%m?m+x%m:0);
  4466 + return x>=0?x%m:(x%m?m + x%m:0);
4394 4467 }
4395 4468 inline int mod(const unsigned char x, const unsigned char m) {
4396 4469 return x%m;
... ... @@ -4399,10 +4472,10 @@ namespace cimg_library_suffixed {
4399 4472 return x%m;
4400 4473 }
4401 4474 inline int mod(const unsigned int x, const unsigned int m) {
4402   - return x%m;
  4475 + return (int)(x%m);
4403 4476 }
4404 4477 inline int mod(const unsigned long x, const unsigned long m) {
4405   - return x%m;
  4478 + return (long)(x%m);
4406 4479 }
4407 4480  
4408 4481 //! Return the min-mod of two values.
... ... @@ -4412,13 +4485,13 @@ namespace cimg_library_suffixed {
4412 4485 - <i>minmod(\p a,\p b) = 0</i>, if \p a and \p b have different signs.
4413 4486 **/
4414 4487 template<typename T>
4415   - inline T minmod(const T a, const T b) {
  4488 + inline T minmod(const T& a, const T& b) {
4416 4489 return a*b<=0?0:(a>0?(a<b?a:b):(a<b?b:a));
4417 4490 }
4418 4491  
4419 4492 //! Return base-2 logarithm of a value.
4420 4493 inline double log2(const double x) {
4421   - static const double base = std::log(2.0);
  4494 + const double base = std::log(2.0);
4422 4495 return std::log(x)/base;
4423 4496 }
4424 4497  
... ... @@ -4430,7 +4503,7 @@ namespace cimg_library_suffixed {
4430 4503 \return Rounded value, having the same type as input value \c x.
4431 4504 **/
4432 4505 template<typename T>
4433   - inline T round(const T x, const double y=1, const int rounding_type=0) {
  4506 + inline T round(const T& x, const double y=1, const int rounding_type=0) {
4434 4507 if (y<=0) return x;
4435 4508 const double sx = (double)x/y, floor = std::floor(sx), delta = sx - floor;
4436 4509 return (T)(y*(rounding_type<0?floor:rounding_type>0?std::ceil(sx):delta<0.5?floor:std::ceil(sx)));
... ... @@ -4438,8 +4511,8 @@ namespace cimg_library_suffixed {
4438 4511  
4439 4512 inline double _pythagore(double a, double b) {
4440 4513 const double absa = cimg::abs(a), absb = cimg::abs(b);
4441   - if (absa>absb) { const double tmp = absb/absa; return absa*std::sqrt(1.0+tmp*tmp); }
4442   - else { const double tmp = absa/absb; return absb==0?0:absb*std::sqrt(1.0+tmp*tmp); }
  4514 + if (absa>absb) { const double tmp = absb/absa; return absa*std::sqrt(1.0 + tmp*tmp); }
  4515 + else { const double tmp = absa/absb; return absb==0?0:absb*std::sqrt(1.0 + tmp*tmp); }
4443 4516 }
4444 4517  
4445 4518 inline bool _is_self_expr(const char *expression) {
... ... @@ -4451,7 +4524,7 @@ namespace cimg_library_suffixed {
4451 4524  
4452 4525 //! Convert ascii character to lower case.
4453 4526 inline char uncase(const char x) {
4454   - return (char)((x<'A'||x>'Z')?x:x-'A'+'a');
  4527 + return (char)((x<'A'||x>'Z')?x:x - 'A' + 'a');
4455 4528 }
4456 4529  
4457 4530 //! Convert C-string to lower case.
... ... @@ -4468,7 +4541,7 @@ namespace cimg_library_suffixed {
4468 4541 **/
4469 4542 inline double atof(const char *const str) {
4470 4543 double x = 0, y = 1;
4471   - if (!str) return 0; else { std::sscanf(str,"%lf/%lf",&x,&y); return x/y; }
  4544 + return str && cimg_sscanf(str,"%lf/%lf",&x,&y)>0?x/y:0;
4472 4545 }
4473 4546  
4474 4547 //! Compare the first \p l characters of two C-strings, ignoring the case.
... ... @@ -4483,7 +4556,7 @@ namespace cimg_library_suffixed {
4483 4556 if (!l) return 0;
4484 4557 if (!str1) return str2?-1:0;
4485 4558 const char *nstr1 = str1, *nstr2 = str2;
4486   - int k, diff = 0; for (k = 0; k<l && !(diff = uncase(*nstr1)-uncase(*nstr2)); ++k) { ++nstr1; ++nstr2; }
  4559 + int k, diff = 0; for (k = 0; k<l && !(diff = uncase(*nstr1) - uncase(*nstr2)); ++k) { ++nstr1; ++nstr2; }
4487 4560 return k!=l?diff:0;
4488 4561 }
4489 4562  
... ... @@ -4496,10 +4569,10 @@ namespace cimg_library_suffixed {
4496 4569 **/
4497 4570 inline int strcasecmp(const char *const str1, const char *const str2) {
4498 4571 if (!str1) return str2?-1:0;
4499   - const unsigned int
4500   - l1 = (unsigned int)std::strlen(str1),
4501   - l2 = (unsigned int)std::strlen(str2);
4502   - return cimg::strncasecmp(str1,str2,1+(l1<l2?l1:l2));
  4572 + const int
  4573 + l1 = (int)std::strlen(str1),
  4574 + l2 = (int)std::strlen(str2);
  4575 + return cimg::strncasecmp(str1,str2,1 + (l1<l2?l1:l2));
4503 4576 }
4504 4577  
4505 4578 //! Remove delimiters on the start and/or end of a C-string.
... ... @@ -4516,60 +4589,63 @@ namespace cimg_library_suffixed {
4516 4589 if (!str) return false;
4517 4590 const int l = (int)std::strlen(str);
4518 4591 int p, q;
4519   - if (is_symmetric) for (p = 0, q = l-1; p<q && str[p]==delimiter && str[q]==delimiter; ) {
  4592 + if (is_symmetric) for (p = 0, q = l - 1; p<q && str[p]==delimiter && str[q]==delimiter; ) {
4520 4593 --q; ++p; if (!is_iterative) break;
4521 4594 } else {
4522 4595 for (p = 0; p<l && str[p]==delimiter; ) { ++p; if (!is_iterative) break; }
4523   - for (q = l-1; q>p && str[q]==delimiter; ) { --q; if (!is_iterative) break; }
  4596 + for (q = l - 1; q>p && str[q]==delimiter; ) { --q; if (!is_iterative) break; }
4524 4597 }
4525 4598 const int n = q - p + 1;
4526   - if (n!=l) { std::memmove(str,str+p,n); str[n] = 0; return true; }
  4599 + if (n!=l) { std::memmove(str,str + p,(unsigned int)n); str[n] = 0; return true; }
4527 4600 return false;
4528 4601 }
4529 4602  
  4603 + //! Replace reserved characters (for Windows filename) by another character.
  4604 + /**
  4605 + \param[in,out] str C-string to work with (modified at output).
  4606 + \param[in] c Replacement character.
  4607 + **/
  4608 + inline void strwindows_reserved(char *const str, const char c='_') {
  4609 + for (char *s = str; *s; ++s) {
  4610 + const char i = *s;
  4611 + if (i=='<' || i=='>' || i==':' || i=='\"' || i=='/' || i=='\\' || i=='|' || i=='?' || i=='*') *s = c;
  4612 + }
  4613 + }
  4614 +
4530 4615 //! Replace escape sequences in C-strings by their binary ascii values.
4531 4616 /**
4532 4617 \param[in,out] str C-string to work with (modified at output).
4533   - **/
  4618 + **/
4534 4619 inline void strunescape(char *const str) {
4535   -#define cimg_strunescape(ci,co) case ci: *nd = co; ++ns; break;
  4620 +#define cimg_strunescape(ci,co) case ci : *nd = co; ++ns; break;
4536 4621 unsigned int val = 0;
4537 4622 for (char *ns = str, *nd = str; *ns || (bool)(*nd=0); ++nd) if (*ns=='\\') switch (*(++ns)) {
  4623 + cimg_strunescape('a','\a');
  4624 + cimg_strunescape('b','\b');
  4625 + cimg_strunescape('e',0x1B);
  4626 + cimg_strunescape('f','\f');
4538 4627 cimg_strunescape('n','\n');
  4628 + cimg_strunescape('r','\r');
4539 4629 cimg_strunescape('t','\t');
4540 4630 cimg_strunescape('v','\v');
4541   - cimg_strunescape('b','\b');
4542   - cimg_strunescape('r','\r');
4543   - cimg_strunescape('f','\f');
4544   - cimg_strunescape('a','\a');
4545 4631 cimg_strunescape('\\','\\');
4546   - cimg_strunescape('\?','\?');
4547 4632 cimg_strunescape('\'','\'');
4548 4633 cimg_strunescape('\"','\"');
  4634 + cimg_strunescape('\?','\?');
4549 4635 case 0 : *nd = 0; break;
4550 4636 case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' :
4551   - std::sscanf(ns,"%o",&val); while (*ns>='0' && *ns<='7') ++ns;
4552   - *nd = val; break;
4553   - case 'x':
4554   - std::sscanf(++ns,"%x",&val);
  4637 + cimg_sscanf(ns,"%o",&val); while (*ns>='0' && *ns<='7') ++ns;
  4638 + *nd = (char)val; break;
  4639 + case 'x' :
  4640 + cimg_sscanf(++ns,"%x",&val);
4555 4641 while ((*ns>='0' && *ns<='7') || (*ns>='a' && *ns<='f') || (*ns>='A' && *ns<='F')) ++ns;
4556   - *nd = val; break;
  4642 + *nd = (char)val; break;
4557 4643 default : *nd = *(ns++);
4558 4644 } else *nd = *(ns++);
4559 4645 }
4560 4646  
4561 4647 // Return a temporary string describing the size of a memory buffer.
4562   - inline const char *strbuffersize(const unsigned long size) {
4563   - static char res[256] = { 0 };
4564   - cimg::mutex(5);
4565   - if (size<1024LU) cimg_snprintf(res,sizeof(res),"%lu byte%s",size,size>1?"s":"");
4566   - else if (size<1024*1024LU) { const float nsize = size/1024.0f; cimg_snprintf(res,sizeof(res),"%.1f Kio",nsize); }
4567   - else if (size<1024*1024*1024LU) {
4568   - const float nsize = size/(1024*1024.0f); cimg_snprintf(res,sizeof(res),"%.1f Mio",nsize);
4569   - } else { const float nsize = size/(1024*1024*1024.0f); cimg_snprintf(res,sizeof(res),"%.1f Gio",nsize); }
4570   - cimg::mutex(5,0);
4571   - return res;
4572   - }
  4648 + inline const char *strbuffersize(const unsigned long size);
4573 4649  
4574 4650 // Return string that identifies the running OS.
4575 4651 inline const char *stros() {
... ... @@ -4598,9 +4674,9 @@ namespace cimg_library_suffixed {
4598 4674 }
4599 4675  
4600 4676 //! Return the basename of a filename.
4601   - inline const char* basename(const char *const str) {
4602   - const char *p = 0;
4603   - for (const char *np = str; np>=str && (p=np); np = std::strchr(np,cimg_file_separator)+1) {}
  4677 + inline const char* basename(const char *const s, const char separator=cimg_file_separator) {
  4678 + const char *p = 0, *np = s;
  4679 + while (np>=s && (p=np)) np = std::strchr(np,separator) + 1;
4604 4680 return p;
4605 4681 }
4606 4682  
... ... @@ -4611,7 +4687,7 @@ namespace cimg_library_suffixed {
4611 4687 cimg::srand();
4612 4688 for (unsigned int k = 0; k<8; ++k) {
4613 4689 const int v = (int)std::rand()%3;
4614   - randomid[k] = (char)(v==0?('0'+(std::rand()%10)):(v==1?('a'+(std::rand()%26)):('A'+(std::rand()%26))));
  4690 + randomid[k] = (char)(v==0?('0' + (std::rand()%10)):(v==1?('a' + (std::rand()%26)):('A' + (std::rand()%26))));
4615 4691 }
4616 4692 cimg::mutex(6,0);
4617 4693 return randomid;
... ... @@ -4623,6 +4699,7 @@ namespace cimg_library_suffixed {
4623 4699 #if cimg_OS==2
4624 4700 char *const nstr = new char[MAX_PATH];
4625 4701 if (GetShortPathNameA(str,nstr,MAX_PATH)) std::strcpy(str,nstr);
  4702 + delete[] nstr;
4626 4703 #endif
4627 4704 }
4628 4705 }
... ... @@ -4671,7 +4748,7 @@ namespace cimg_library_suffixed {
4671 4748 return errn;
4672 4749 }
4673 4750  
4674   - //! Check if a path is a directory
  4751 + //! Check if a path is a directory.
4675 4752 /**
4676 4753 \param path Specified path to test.
4677 4754 **/
... ... @@ -4679,615 +4756,131 @@ namespace cimg_library_suffixed {
4679 4756 if (!path || !*path) return false;
4680 4757 #if cimg_OS==1
4681 4758 struct stat st_buf;
4682   - if (!stat(path,&st_buf) && S_ISDIR(st_buf.st_mode)) return true;
  4759 + return (!stat(path,&st_buf) && S_ISDIR(st_buf.st_mode));
4683 4760 #elif cimg_OS==2
4684   - if (GetFileAttributes(path)&16) return true;
4685   -#endif
4686   - return false;
4687   - }
4688   -
4689   - //! Get/set path to store temporary files.
4690   - /**
4691   - \param user_path Specified path, or \c 0 to get the path currently used.
4692   - \param reinit_path Force path to be recalculated (may take some time).
4693   - \return Path where temporary files can be saved.
4694   - **/
4695   - inline const char* temporary_path(const char *const user_path=0, const bool reinit_path=false) {
4696   -#define _cimg_test_temporary_path(p) \
4697   - if (!path_found) { \
4698   - cimg_snprintf(s_path,1024,"%s",p); \
4699   - cimg_snprintf(tmp,sizeof(tmp),"%s%c%s",s_path,cimg_file_separator,filetmp); \
4700   - if ((file=std::fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } \
4701   - }
4702   - static char *s_path = 0;
4703   - cimg::mutex(7);
4704   - if (reinit_path) { delete[] s_path; s_path = 0; }
4705   - if (user_path) {
4706   - if (!s_path) s_path = new char[1024];
4707   - std::memset(s_path,0,1024);
4708   - std::strncpy(s_path,user_path,1023);
4709   - } else if (!s_path) {
4710