diff --git a/LICENSE.txt b/LICENSE.txt deleted file mode 100644 index de206bd..0000000 --- a/LICENSE.txt +++ /dev/null @@ -1,26 +0,0 @@ -Copyright (c) 2009, David Mayerich -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. \ No newline at end of file diff --git a/biology/fibernet.h b/biology/fibernet.h deleted file mode 100644 index e69de29..0000000 --- a/biology/fibernet.h +++ /dev/null diff --git a/cuda/callable.h b/cuda/callable.h deleted file mode 100644 index eefc437..0000000 --- a/cuda/callable.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CUDA_CALLABLE - -//define the CUDA_CALLABLE macro (will prefix all members) -#ifdef __CUDACC__ -#define CUDA_CALLABLE __host__ __device__ -#else -#define CUDA_CALLABLE -#endif - -#endif diff --git a/cuda/cost.h b/cuda/cost.h deleted file mode 100644 index db5b7c2..0000000 --- a/cuda/cost.h +++ /dev/null @@ -1,150 +0,0 @@ -#include -#include -#include -#include -#include "cuPrintf.cu" -#include "cuPrintf.cuh" -#include -#include "../visualization/colormap.h" - -#define DIM_Y 10890 -#define DIM_X 20 -typedef unsigned char uchar; -//surface texOut; ///// maybe just do a normal array instead of a surface. - //we may not need a surface at all. -//texture texTemplate -texture texIn; -float *result; -float* v_dif; -cudaArray* srcArray; -bool testing = false; - -inline void checkCUDAerrors(const char *msg) -{ - cudaError_t err = cudaGetLastError(); - if (cudaSuccess != err){ - fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString(err) ); - exit(1); - } -} - - -float get_sum(float *diff) -{ - - cublasStatus_t ret; - cublasHandle_t handle; - ret = cublasCreate(&handle); - - ret = cublasSetVector(20*10, sizeof(*diff), diff, 1, v_dif, 1); - if(!testing){ - stim::gpu2image(v_dif, "sample0.bmp", 20,10,0,1); - testing = true; - } //float* out = (float*) malloc(sizeof(float)); - float out; - ret = cublasSasum(handle, 20*10, v_dif, 1, &out); - cublasDestroy(handle); - return out; -} - -__device__ float Template(int x) -{ - if(x < 20/6 || x > 20*5/6 || (x > 20*2/6 && x < 20*4/6)){ - return 1.0; - }else{ - return 0.0; - } - -} - -__global__ -void get_diff (float *result) -{ - //cuPrintf("Hello"); - int x = threadIdx.x + blockIdx.x * blockDim.x; - int y = threadIdx.y + blockIdx.y * blockDim.y; - int idx = y*DIM_X+x; - - //float valIn = tex2D(texIn, x, y); - float valIn = tex2D(texIn, x, y)/255.0; - float valTemp = Template(x); - result[idx] = abs(valIn-valTemp); - //result[idx] = abs(valTemp); -// #if __CUDA_ARCH__>=200 -// printf("Value is : %f\n and the result is : %f\n", valIn, result[idx]); -// #endif - //cuPrintf("Value is : %f\n and the result is : %f\n", valIn, result[idx]); -} - - - - -void initArray(cudaGraphicsResource_t src) -{ - //cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc (); - //cudaMallocArray(&result, &channelDesc, DIM_X, DIM_Y, 0); - //HANDLE_ERROR( - // cudaGraphicsGLRegisterImage(&src, - // fboID, - // GL_TEXTURE_2D, - HANDLE_ERROR( - cudaGraphicsMapResources(1, &src) - ); - HANDLE_ERROR( - cudaGraphicsSubResourceGetMappedArray(&srcArray, src,0,0) - ); - HANDLE_ERROR( - cudaBindTextureToArray(texIn, srcArray) - ); - cudaMalloc( (void**) &result, DIM_X*DIM_Y*sizeof(float)); - checkCUDAerrors("Memory Allocation Issue 1"); - cudaMalloc((void **) &v_dif, 20*10*sizeof(float)); - checkCUDAerrors("Memory Allocation Issue 2"); - //HANDLE_ERROR( - // cudaBindTextureToArray(texIn, ptr, &channelDesc) - // ); -} - -void cleanUP(cudaGraphicsResource_t src) -{ - HANDLE_ERROR( - cudaUnbindTexture(texIn) - ); - HANDLE_ERROR( - cudaFree(result) - ); - HANDLE_ERROR( - cudaGraphicsUnmapResources(1,&src) - ); - HANDLE_ERROR( - cudaFree(v_dif) - ); -} - -extern "C" -int get_cost(cudaGraphicsResource_t src) -{ - float output[1089]; - float mini = 10000000000000000.0; - int idx; - initArray(src); - dim3 grid(20, 10890); - dim3 block(1, 1); - //texIn.normalized = 1; - get_diff <<< grid, block >>> (result); - stim::gpu2image(result, "test.bmp", 20,10890,0,1); - for (int i = 0; i < 1089; i++){ - output[i] = get_sum(result+(20*10*i)); - if(output[i] < mini){ - mini = output[i]; - idx = i; - } - } - std::cout << output[0] << std::endl; - std::cout << output[100] << std::endl; - std::cout << output[500] << std::endl; - std::cout << output[1000] << std::endl; - std::cout << idx << std::endl; - cleanUP(src); - return idx; -} diff --git a/cuda/devices.h b/cuda/devices.h deleted file mode 100644 index 4dce378..0000000 --- a/cuda/devices.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef RTS_CUDA_DEVICES -#define RTS_CUDA_DEVICES - -#include - -namespace stim{ - -int maxThreadsPerBlock() -{ - int device; - cudaGetDevice(&device); //get the id of the current device - cudaDeviceProp props; //device property structure - cudaGetDeviceProperties(&props, device); - return props.maxThreadsPerBlock; -} -} //end namespace rts - -#endif diff --git a/cuda/error.h b/cuda/error.h deleted file mode 100644 index b49cf4b..0000000 --- a/cuda/error.h +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -using namespace std; -#include "cuda_runtime.h" -#include "device_launch_parameters.h" -#include "cufft.h" - -#ifndef CUDA_HANDLE_ERROR_H -#define CUDA_HANDLE_ERROR_H - -//handle error macro -static void HandleError( cudaError_t err, const char *file, int line ) { - if (err != cudaSuccess) { - //FILE* outfile = fopen("cudaErrorLog.txt", "w"); - //fprintf(outfile, "%s in %s at line %d\n", cudaGetErrorString( err ), file, line ); - //fclose(outfile); - printf("%s in %s at line %d\n", cudaGetErrorString( err ), file, line ); - //exit( EXIT_FAILURE ); - - } -} -#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ )) - -static void CufftError( cufftResult err ) -{ - if (err != CUFFT_SUCCESS) - { - if(err == CUFFT_INVALID_PLAN) - cout<<"The plan parameter is not a valid handle."< -#include - -#include -#include - -//#include -#include "cuda_gl_interop.h" -#include "../gl/error.h" - -namespace stim -{ - -static void InitGLEW() -{ - //Initialize the GLEW toolkit - - GLenum err = glewInit(); - if(GLEW_OK != err) - { - printf("Error starting GLEW."); - } - fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); -} - -static void cudaSetDevice(int major = 1, int minor = 3) -{ - cudaDeviceProp prop; - int dev; - - //find a CUDA device that can handle an offscreen buffer - int num_gpu; - HANDLE_ERROR(cudaGetDeviceCount(&num_gpu)); - printf("Number of CUDA devices detected: %d\n", num_gpu); - memset(&prop, 0, sizeof(cudaDeviceProp)); - prop.major=major; - prop.minor=minor; - HANDLE_ERROR(cudaChooseDevice(&dev, &prop)); - HANDLE_ERROR(cudaGetDeviceProperties(&prop, dev)); - HANDLE_ERROR(cudaGLSetGLDevice(dev)); -} - -static void* cudaMapResource(cudaGraphicsResource* cudaBufferResource) -{ - //this function takes a predefined CUDA resource and maps it to a pointer - void* buffer; - HANDLE_ERROR(cudaGraphicsMapResources(1, &cudaBufferResource, NULL)); - size_t size; - HANDLE_ERROR(cudaGraphicsResourceGetMappedPointer( (void**)&buffer, &size, cudaBufferResource)); - return buffer; -} -static void cudaUnmapResource(cudaGraphicsResource* resource) -{ - //this function unmaps the CUDA resource so it can be used by OpenGL - HANDLE_ERROR(cudaGraphicsUnmapResources(1, &resource, NULL)); -} - -static void cudaCreateRenderBuffer(GLuint &glBufferName, cudaGraphicsResource* &cudaBufferResource, int resX, int resY) -{ - //delete the previous buffer name and resource - if(cudaBufferResource != 0) - HANDLE_ERROR(cudaGraphicsUnregisterResource(cudaBufferResource)); - if(glBufferName != 0) - glDeleteBuffers(1, &glBufferName); - - //generate an OpenGL offscreen buffer - glGenBuffers(1, &glBufferName); - - //bind the buffer - directs all calls to this buffer - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glBufferName); - glBufferData(GL_PIXEL_UNPACK_BUFFER, resX * resY * sizeof(uchar3), NULL, GL_DYNAMIC_DRAW_ARB); - CHECK_OPENGL_ERROR - HANDLE_ERROR(cudaGraphicsGLRegisterBuffer(&cudaBufferResource, glBufferName, cudaGraphicsMapFlagsNone)); -} -} -#endif diff --git a/cuda/threads.h b/cuda/threads.h deleted file mode 100644 index 87c443e..0000000 --- a/cuda/threads.h +++ /dev/null @@ -1,34 +0,0 @@ -#include "cuda_runtime.h" -#include "device_launch_parameters.h" -#include "../cuda/callable.h" - -#ifndef CUDA_THREADS_H -#define CUDA_THREADS_H - -#define MAX_GRID 65535 - -__device__ unsigned int ThreadIndex1D() -{ - return blockIdx.y * gridDim.x * blockDim.x + blockIdx.x * blockDim.x + threadIdx.x; -} - -dim3 GenGrid1D(unsigned int N, unsigned int blocksize = 128) -{ - dim3 dimgrid; - - dimgrid.x = (N + blocksize - 1)/blocksize; - dimgrid.y = 1; - dimgrid.z = 1; - - if(dimgrid.x > MAX_GRID) - { - dimgrid.y = (dimgrid.x + MAX_GRID - 1) / MAX_GRID; - dimgrid.x = MAX_GRID; - } - - return dimgrid; - -} - - -#endif diff --git a/cuda/timer.h b/cuda/timer.h deleted file mode 100644 index fd2786d..0000000 --- a/cuda/timer.h +++ /dev/null @@ -1,21 +0,0 @@ -static cudaEvent_t tStartEvent; -static cudaEvent_t tStopEvent; - -static void gpuStartTimer() -{ - //set up timing events - cudaEventCreate(&tStartEvent); - cudaEventCreate(&tStopEvent); - cudaEventRecord(tStartEvent, 0); -} - -static float gpuStopTimer() -{ - cudaEventRecord(tStopEvent, 0); - cudaEventSynchronize(tStopEvent); - float elapsedTime; - cudaEventElapsedTime(&elapsedTime, tStartEvent, tStopEvent); - cudaEventDestroy(tStartEvent); - cudaEventDestroy(tStopEvent); - return elapsedTime; -} \ No newline at end of file diff --git a/envi/bil.h b/envi/bil.h deleted file mode 100644 index a770292..0000000 --- a/envi/bil.h +++ /dev/null @@ -1,992 +0,0 @@ -#ifndef STIM_BIL_H -#define STIM_BIL_H - -#include "../envi/envi_header.h" -#include "../envi/binary.h" -#include -#include - -namespace stim{ - -/** - The BIL class represents a 3-dimensional binary file stored using band interleaved by line (BIL) image encoding. The binary file is stored - such that X-Z "frames" are stored sequentially to form an image stack along the y-axis. When accessing the data sequentially on disk, - the dimensions read, from fastest to slowest, are X, Z, Y. - - This class is optimized for data streaming, and therefore supports extremely large (terabyte-scale) files. Data is loaded from disk - on request. Functions used to access data are written to support efficient reading. -*/ - -template - -class bil: public binary { - -protected: - - std::vector w; //band wavelength - -public: - - using binary::open; - using binary::file; - using binary::R; - - /// Open a data file for reading using the class interface. - - /// @param filename is the name of the binary file on disk - /// @param X is the number of samples along dimension 1 - /// @param Y is the number of samples (lines) along dimension 2 - /// @param B is the number of samples (bands) along dimension 3 - /// @param header_offset is the number of bytes (if any) in the binary header - /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int B, unsigned int header_offset, std::vector wavelengths){ - - w = wavelengths; - - return open(filename, vec(X, Y, B), header_offset); - - } - - /// Retrieve a single band (based on index) and stores it in pre-allocated memory. - - /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. - /// @param page <= B is the integer number of the band to be copied. - bool band_index( T * p, unsigned int page){ - - unsigned int L = R[0] * sizeof(T); //caculate the number of bytes in a sample line - unsigned int jump = R[0] * (R[2] - 1) * sizeof(T); - - if (page >= R[2]){ //make sure the bank number is right - std::cout<<"ERROR: page out of range"< wavelength ){ - band_index(p, page); - return true; - } - - while( w[page] < wavelength ) - { - page++; - //if wavelength is larger than the last wavelength in header file - if (page == R[2]) { - band_index(p, R[2]-1); - return true; - } - } - if ( wavelength < w[page] ) - { - T * p1; - T * p2; - p1=(T*)malloc(S); //memory allocation - p2=(T*)malloc(S); - band_index(p1, page - 1); - band_index(p2, page ); - for(unsigned i=0; i < XY; i++){ - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); - p[i] = (p2[i] - p1[i]) * r + p1[i]; - } - free(p1); - free(p2); - } - else //if the wavelength is equal to a wavelength in header file - { - band_index(p, page); - } - - return true; - } - - //get YZ line from the a Y slice, Y slice data should be already IN the MEMORY - bool getYZ(T* p, T* c, double wavelength) - { - unsigned int X = R[0]; //calculate the number of pixels in a sample - unsigned int B = R[2]; - unsigned int L = X * sizeof(T); - - unsigned page=0; //samples around the wavelength - T * p1; - T * p2; - - //get the bands numbers around the wavelength - - //if wavelength is smaller than the first one in header file - if ( w[page] > wavelength ){ - memcpy(p, c, L); - return true; - } - - while( w[page] < wavelength ) - { - page++; - //if wavelength is larger than the last wavelength in header file - if (page == B) { - memcpy(p, c + (B - 1) * X, L); - return true; - } - } - if ( wavelength < w[page] ) - { - p1=(T*)malloc( L ); //memory allocation - p2=(T*)malloc( L ); - - memcpy(p1, c + (page - 1) * X, L); - memcpy(p2, c + page * X, L); - - for(unsigned i=0; i < X; i++){ - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); - p[i] = (p2[i] - p1[i]) * r + p1[i]; - } - } - else //if the wavelength is equal to a wavelength in header file - memcpy(p, c + page * X, L); - - return true; - } - - /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory. - - /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size. - /// @param x is the x-coordinate (dimension 1) of the spectrum. - /// @param y is the y-coordinate (dimension 2) of the spectrum. - bool spectrum(T * p, unsigned x, unsigned y){ - - if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right - std::cout<<"ERROR: sample or line out of range"<= R[1]){ //make sure the line number is right - std::cout<<"ERROR: line out of range"< wls){ - - unsigned N = wls.size(); //get the number of baseline points - - std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file - std::string headername = outname + ".hdr"; //the header file name - - //simplify image resolution - unsigned int ZX = R[2] * R[0]; //calculate the number of points in a Y slice - unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice - unsigned int B = R[2]; - unsigned int X = R[0]; - - T* c; //pointer to the current Y slice - c = (T*)malloc(L); //memory allocation - - T* a; //pointer to the two YZ lines surrounding the current YZ line - T* b; - - a = (T*)malloc(X * sizeof(T)); - b = (T*)malloc(X * sizeof(T)); - - - double ai, bi; //stores the two baseline points wavelength surrounding the current band - double ci; //stores the current band's wavelength - unsigned control; - - if (a == NULL || b == NULL || c == NULL){ - std::cout<<"ERROR: error allocating memory"; - exit(1); - } - // loop start correct every y slice - - for (unsigned k =0; k < R[1]; k++) - { - //get the current y slice - getY(c, k); - - //initialize lownum, highnum, low, high - ai = w[0]; - control=0; - //if no baseline point is specified at band 0, - //set the baseline point at band 0 to 0 - if(wls[0] != w[0]){ - bi = wls[control]; - memset(a, (char)0, X * sizeof(T) ); - } - //else get the low band - else{ - control++; - getYZ(a, c, ai); - bi = wls[control]; - } - //get the high band - getYZ(b, c, bi); - - //correct every YZ line - - for(unsigned cii = 0; cii < B; cii++){ - - //update baseline points, if necessary - if( w[cii] >= bi && cii != B - 1) { - //if the high band is now on the last BL point - if (control != N-1) { - - control++; //increment the index - - std::swap(a, b); //swap the baseline band pointers - - ai = bi; - bi = wls[control]; - getYZ(b, c, bi); - - } - //if the last BL point on the last band of the file? - else if ( wls[control] < w[B - 1]) { - - std::swap(a, b); //swap the baseline band pointers - - memset(b, (char)0, X * sizeof(T) ); //clear the high band - - ai = bi; - bi = w[B - 1]; - } - } - - ci = w[cii]; - - unsigned jump = cii * X; - //perform the baseline correction - for(unsigned i=0; i < X; i++) - { - double r = (double) (ci - ai) / (double) (bi - ai); - c[i + jump] =(T) ( c[i + jump] - (b[i] - a[i]) * r - a[i] ); - } - - }//loop for YZ line end - target.write(reinterpret_cast(c), L); //write the corrected data into destination - }//loop for Y slice end - - free(a); - free(b); - free(c); - target.close(); - return true; - - } - - /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file. - - /// @param outname is the name of the output file used to store the resulting baseline-corrected data. - /// @param w is the label specifying the band that the hyperspectral image will be normalized to. - /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers. - bool normalize(std::string outname, double w, double t = 0.0) - { - unsigned int B = R[2]; //calculate the number of bands - unsigned int Y = R[1]; - unsigned int X = R[0]; - unsigned int ZX = R[2] * R[0]; - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band - unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band - unsigned int L = ZX * sizeof(T); - - std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file - std::string headername = outname + ".hdr"; //the header file name - - T * c; //pointer to the current ZX slice - T * b; //pointer to the standard band - - b = (T*)malloc( S ); //memory allocation - c = (T*)malloc( L ); - - band(b, w); //get the certain band into memory - - for(unsigned j = 0; j < Y; j++) - { - getY(c, j); - for(unsigned i = 0; i < B; i++) - { - for(unsigned m = 0; m < X; m++) - { - if( b[m + j * X] < t ) - c[m + i * X] = (T)0.0; - else - c[m + i * X] = c[m + i * X] / b[m + j * X]; - } - } - target.write(reinterpret_cast(c), L); //write normalized data into destination - } - - free(b); - free(c); - target.close(); - return true; - } - - /// Convert the current BIL file to a BSQ file with the specified file name. - - /// @param outname is the name of the output BSQ file to be saved to disk. - bool bsq(std::string outname) - { - unsigned int S = R[0] * R[1] * sizeof(T); //calculate the number of bytes in a band - - std::ofstream target(outname.c_str(), std::ios::binary); - std::string headername = outname + ".hdr"; - - T * p; //pointer to the current band - p = (T*)malloc(S); - - for ( unsigned i = 0; i < R[2]; i++) - { - band_index(p, i); - target.write(reinterpret_cast(p), S); //write a band data into target file - } - - free(p); - target.close(); - return true; - } - - /// Convert the current BIL file to a BIP file with the specified file name. - - /// @param outname is the name of the output BIP file to be saved to disk. - bool bip(std::string outname) - { - unsigned int S = R[0] * R[2] * sizeof(T); //calculate the number of bytes in a ZX slice - - std::ofstream target(outname.c_str(), std::ios::binary); - std::string headername = outname + ".hdr"; - - T * p; //pointer to the current XZ slice for bil file - p = (T*)malloc(S); - T * q; //pointer to the current ZX slice for bip file - q = (T*)malloc(S); - - for ( unsigned i = 0; i < R[1]; i++) - { - getY(p, i); - for ( unsigned k = 0; k < R[2]; k++) - { - unsigned ks = k * R[0]; - for ( unsigned j = 0; j < R[0]; j++) - q[k + j * R[2]] = p[ks + j]; - } - - target.write(reinterpret_cast(q), S); //write a band data into target file - } - - - free(p); - free(q); - target.close(); - return true; - } - - - /// Return a baseline corrected band given two adjacent baseline points and their bands. The result is stored in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lp is a pointer to an array holding the band image for the left baseline point - /// @param rp is a pointer to an array holding the band image for the right baseline point - /// @param wavelength is the label value for the requested baseline-corrected band - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. - bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ - - unsigned XY = R[0] * R[1]; - band(result, wavelength); //get band - - //perform the baseline correction - double r = (double) (wavelength - lb) / (double) (rb - lb); - for(unsigned i=0; i < XY; i++){ - result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] ); - } - return true; - } - - /// Return a baseline corrected band given two adjacent baseline points. The result is stored in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param bandwavelength is the label value for the desired baseline-corrected band - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. - bool height(double lb, double rb, double bandwavelength, T* result){ - - T* lp; - T* rp; - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - - band(lp, lb); - band(rp, rb); - - baseline_band(lb, rb, lp, rp, bandwavelength, result); - - free(lp); - free(rp); - return true; - } - - - - /// Calculate the area under the spectrum between two specified points and stores the result in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label value for the left bound (start of the integration) - /// @param rab is the label value for the right bound (end of the integration) - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool area(double lb, double rb, double lab, double rab, T* result){ - - T* lp; //left band pointer - T* rp; //right band pointer - T* cur; //current band 1 - T* cur2; //current band 2 - - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - cur = (T*) malloc(S); - cur2 = (T*) malloc(S); - - memset(result, (char)0, S); - - //find the wavelenght position in the whole band - unsigned int n = w.size(); - unsigned int ai = 0; //left bound position - unsigned int bi = n - 1; //right bound position - - - - //to make sure the left and the right bound are in the bandwidth - if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ - std::cout<<"ERROR: left bound or right bound out of bandwidth"< rb){ - std::cout<<"ERROR: right bound should be bigger than left bound"<= w[ai]){ - ai++; - } - while (rab <= w[bi]){ - bi--; - } - - band(lp, lb); - band(rp, rb); - - //calculate the beginning and the ending part - baseline_band(lb, rb, lp, rp, rab, cur2); //ending part - baseline_band(lb, rb, lp, rp, w[bi], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (rab - w[bi]) * (cur[j] + cur2[j]) / 2.0; - } - baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part - baseline_band(lb, rb, lp, rp, w[ai], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (w[ai] - lab) * (cur[j] + cur2[j]) / 2.0; - } - - //calculate the area - ai++; - for(unsigned i = ai; i <= bi ;i++) - { - baseline_band(lb, rb, lp, rp, w[ai], cur2); - for(unsigned j = 0; j < XY; j++) - { - result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0; - } - std::swap(cur,cur2); //swap the band pointers - } - - free(lp); - free(rp); - free(cur); - free(cur2); - return true; - } - - /// Compute the ratio of two baseline-corrected peaks. The result is stored in a pre-allocated array. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param pos1 is the label value for the first peak (numerator) position - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param pos2 is the label value for the second peak (denominator) position - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the two peak band - height(lb1, rb1, pos1, p1); - height(lb2, rb2, pos2, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the ratio between a peak area and peak height. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param pos1 is the label value for the first peak (numerator) position - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param pos2 is the label value for the second peak (denominator) position - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, - double lb2, double rb2, double pos, T* result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - area(lb1, rb1, lab1, rab1, p1); - height(lb2, rb2, pos, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the ratio between two peak areas. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param lab1 is the label value for the left bound (start of the integration) of the first peak (numerator) - /// @param rab1 is the label value for the right bound (end of the integration) of the first peak (numerator) - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) - /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator) - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, - double lb2, double rb2, double lab2, double rab2, T* result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - area(lb1, rb1, lab1, rab1, p1); - area(lb2, rb2, lab2, rab2, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the definite integral of a baseline corrected peak. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label for the start of the definite integral - /// @param rab is the label for the end of the definite integral - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool x_area(double lb, double rb, double lab, double rab, T* result){ - T* lp; //left band pointer - T* rp; //right band pointer - T* cur; //current band 1 - T* cur2; //current band 2 - - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - cur = (T*) malloc(S); - cur2 = (T*) malloc(S); - - memset(result, (char)0, S); - - //find the wavelenght position in the whole band - unsigned int n = w.size(); - unsigned int ai = 0; //left bound position - unsigned int bi = n - 1; //right bound position - - //to make sure the left and the right bound are in the bandwidth - if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ - std::cout<<"ERROR: left bound or right bound out of bandwidth"< rb){ - std::cout<<"ERROR: right bound should be bigger than left bound"<= w[ai]){ - ai++; - } - while (rab <= w[bi]){ - bi--; - } - - band(lp, lb); - band(rp, rb); - - //calculate the beginning and the ending part - baseline_band(lb, rb, lp, rp, rab, cur2); //ending part - baseline_band(lb, rb, lp, rp, w[bi], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (rab - w[bi]) * (rab + w[bi]) * (cur[j] + cur2[j]) / 4.0; - } - baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part - baseline_band(lb, rb, lp, rp, w[ai], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (w[ai] - lab) * (w[ai] + lab) * (cur[j] + cur2[j]) / 4.0; - } - - //calculate f(x) times x - ai++; - for(unsigned i = ai; i <= bi ;i++) - { - baseline_band(lb, rb, lp, rp, w[ai], cur2); - for(unsigned j = 0; j < XY; j++) - { - result[j] += (w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * (cur[j] + cur2[j]) / 4.0; - } - std::swap(cur,cur2); //swap the band pointers - } - - free(lp); - free(rp); - free(cur); - free(cur2); - return true; - } - - /// Compute the centroid of a baseline corrected peak. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label for the start of the peak - /// @param rab is the label for the end of the peak - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool cpoint(double lb, double rb, double lab, double rab, T* result){ - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - x_area(lb, rb, lab, rab, p1); - area(lb, rb, lab, rab, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Create a mask based on a given band and threshold value. - - /// All pixels in the - /// specified band greater than the threshold are true and all pixels less than the threshold are false. - /// @param mask_band is the band used to specify the mask - /// @param threshold is the threshold used to determine if the mask value is true or false - /// @param p is a pointer to a pre-allocated array at least X * Y in size - bool build_mask(double mask_band, double threshold, unsigned char* p){ - - T* temp = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate memory for the certain band - band(temp, mask_band); - - for (unsigned int i = 0; i < R[0] * R[1]; i++) { - if (temp[i] < threshold) - p[i] = 0; - else - p[i] = 255; - } - - free(temp); - return true; - } - - /// Apply a mask file to the BSQ image, setting all values outside the mask to zero. - - /// @param outfile is the name of the masked output file - /// @param p is a pointer to memory of size X * Y, where p(i) = 0 for pixels that will be set to zero. - bool apply_mask(std::string outfile, unsigned char* p){ - - std::ofstream target(outfile.c_str(), std::ios::binary); - - unsigned XZ = R[0] * R[2]; //calculate number of a band - unsigned L = XZ * sizeof(T); - - T * temp = (T*)malloc(L); - - for (unsigned i = 0; i < R[1]; i++) - { - getY(temp, i); - for ( unsigned j = 0; j < R[2]; j++) - { - for (unsigned k = 0; k < R[0]; k++) - { - if(p[i * R[0] + k] == 0) - temp[j * R[0] + k] = 0; - else - continue; - } - } - target.write(reinterpret_cast(temp), L); //write a band data into target file - } - target.close(); - free(temp); - return true; - } - - /// Calculate the mean band value (average along B) at each pixel location. - - /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. - bool band_avg(T* p){ - unsigned long long XZ = R[0] * R[2]; - T* temp = (T*)malloc(sizeof(T) * XZ); - T* line = (T*)malloc(sizeof(T) * R[0]); - - for (unsigned i = 0; i < R[1]; i++){ - getY(temp, i); - //initialize x-line - for (unsigned j = 0; j < R[0]; j++){ - line[j] = 0; - } - unsigned c = 0; - for (unsigned j = 0; j < R[2]; j++){ - for (unsigned k = 0; k < R[0]; k++){ - line[k] += temp[c] / (T)R[2]; - c++; - } - } - for (unsigned j = 0; j < R[0]; j++){ - p[j + i * R[0]] = line[j]; - } - } - free(temp); - return true; - } - - /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum - - /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(T*p, unsigned char* mask){ - unsigned long long XZ = R[0] * R[2]; - unsigned long long XY = R[0] * R[1]; - T* temp = (T*)malloc(sizeof(T) * XZ); - for (unsigned j = 0; j < R[2]; j++){ - p[j] = 0; - } - //calculate vaild number in a band - unsigned count = 0; - for (unsigned j = 0; j < XY; j++){ - if (mask[j] != 0){ - count++; - } - } - for (unsigned k = 0; k < R[1]; k++){ - getY(temp, k); - unsigned kx = k * R[0]; - for (unsigned i = 0; i < R[0]; i++){ - if (mask[kx + i] != 0){ - for (unsigned j = 0; j < R[2]; j++){ - p[j] += temp[j * R[0] + i] / (T)count; - } - } - } - } - free(temp); - return true; - } - - /// Calculate the covariance matrix for all masked pixels in the image. - - /// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix - /// @param avg is a pointer to memory of size B that stores the average spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool co_matrix(T* co, T* avg, unsigned char *mask){ - //memory allocation - unsigned long long xy = R[0] * R[1]; - unsigned int B = R[2]; - T* temp = (T*)malloc(sizeof(T) * B); - //count vaild pixels in a band - unsigned count = 0; - for (unsigned j = 0; j < xy; j++){ - if (mask[j] != 0){ - count++; - } - } - //initialize correlation matrix - for (unsigned i = 0; i < B; i++){ - for (unsigned k = 0; k < B; k++){ - co[i * B + k] = 0; - } - } - //calculate correlation coefficient matrix - for (unsigned j = 0; j < xy; j++){ - if (mask[j] != 0){ - pixel(temp, j); - for (unsigned i = 0; i < B; i++){ - for (unsigned k = i; k < B; k++){ - co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count; - } - } - } - } - //because correlation matrix is symmetric - for (unsigned i = 0; i < B; i++){ - for (unsigned k = i + 1; k < B; k++){ - co[k * B + i] = co[i * B + k]; - } - } - - free(temp); - return true; - } - - - /// Crop a region of the image and save it to a new file. - - /// @param outfile is the file name for the new cropped image - /// @param x0 is the lower-left x pixel coordinate to be included in the cropped image - /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image - /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image - /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image - bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ - - //calculate the new number of samples and lines - unsigned long long sam = x1 - x0; //samples - unsigned long long lin = y1 - y0; //lines - unsigned long long L = sam * R[2] * sizeof(T); - //get specified band and save - T* temp = (T*)malloc(L); - std::ofstream out(outfile.c_str(), std::ios::binary); - unsigned long long jumpb = (R[0] - sam) * sizeof(T); //jump pointer to the next band - //get start - file.seekg((y0 * R[0] * R[2] + x0) * sizeof(T), std::ios::beg); - for (unsigned i = 0; i < lin; i++) - { - for (unsigned j = 0; j < R[2]; j++) - { - file.read((char *)(temp + j * sam), sizeof(T) * sam); - file.seekg(jumpb, std::ios::cur); //go to the next band - } - out.write(reinterpret_cast(temp), L); //write slice data into target file - } - free(temp); - return true; - } - - - /// Close the file. - bool close(){ - file.close(); - return true; - } - - }; -} - -#endif diff --git a/envi/binary.h b/envi/binary.h deleted file mode 100644 index 308e71d..0000000 --- a/envi/binary.h +++ /dev/null @@ -1,214 +0,0 @@ - -//make sure that this header file is only loaded once -#ifndef RTS_BINARY_H -#define RTS_BINARY_H - -#include "../envi/envi_header.h" -#include "../math/vector.h" -#include -#include - -namespace stim{ - -/** This class manages the streaming of large multidimensional binary files. - * Generally these are hyperspectral files with 2 spatial and 1 spectral dimension. However, this class supports - * other dimensions via the template parameter D. - * - * @param T is the data type used to store data to disk (generally float or double) - * @param D is the dimension of the data (default 3) - */ -template< typename T, unsigned int D = 3 > -class binary{ - -protected: - std::fstream file; //file stream used for reading and writing - std::string name; //file name - - unsigned long long int R[D]; //resolution - unsigned int header; //header size (in bytes) - unsigned char* mask; //pointer to a character array: 0 = background, 1 = foreground (or valid data) - - - - - /// Private initialization function used to set default parameters in the data structure. - void init(){ - memset(R, 0, sizeof(unsigned int) * D); //initialize the resolution to zero - header = 0; //initialize the header size to zero - mask = NULL; - } - - /// Private helper function that returns the size of the file on disk using system functions. - long long int get_file_size(){ -#ifdef _WIN32 - struct _stat64 results; - if(_stat64(name.c_str(), &results) == 0) - return results.st_size; -#else - struct stat results; - if(stat(name.c_str(), &results) == 0) - return results.st_size; -#endif - else return 0; - } - - /// Private helper function that tests to make sure that the calculated data size specified by the structure is the same as the data size on disk. - bool test_file_size(){ - long long int npts = 1; //initialize the number of data points to 1 - for(unsigned int i = 0; i r, unsigned int h = 0){ - - for(unsigned int i = 0; i < D; i++) //set the dimensions of the binary file object - R[i] = r[i]; - - header = h; //save the header size - - if(!open_file(filename)) return false; //open the binary file - - return test_file_size(); - } - - /// Creates a new binary file for streaming - - /// @param filename is the name of the binary file to be created - /// @param r is a STIM vector specifying the size of the file along each dimension - /// @offset specifies how many bytes to offset the file (used to leave room for a header) - bool create(std::string filename, vec r, unsigned int offset = 0){ - - std::ofstream target(filename.c_str(), std::ios::binary); - - //initialize binary file - T p = 0; - for(unsigned int i =0; i < r[0] * r[1] * r[2]; i++){ - target.write((char*)(&p), sizeof(T)); - } - - for(unsigned int i = 0; i < D; i++) //set the dimensions of the binary file object - R[i] = r[i]; - - header = offset; //save the header size - - if(!open_file(filename)) return false; //open the binary file - - return test_file_size(); - } - - /// Writes a single page of data to disk. A page consists of a sequence of data of size R[0] * R[1] * ... * R[D-1]. - - /// @param p is a pointer to the data to be written - /// @param page is the page number (index of the highest-numbered dimension) - bool write_page( T * p, unsigned int page){ - - if(p == NULL){ - std::cout<<"ERROR: unable to write into file, empty pointer"<= R[2]){ //make sure the bank number is right - std::cout<<"ERROR: page out of range"<= R[0] || y >= R[1]){ //make sure the sample and line number is right - std::cout<<"ERROR: sample or line out of range"< -#include - -namespace stim{ - -/** - The BIP class represents a 3-dimensional binary file stored using band interleaved by pixel (BIP) image encoding. The binary file is stored - such that Z-X "frames" are stored sequentially to form an image stack along the y-axis. When accessing the data sequentially on disk, - the dimensions read, from fastest to slowest, are Z, X, Y. - - This class is optimized for data streaming, and therefore supports extremely large (terabyte-scale) files. Data is loaded from disk - on request. Functions used to access data are written to support efficient reading. -*/ -template - -class bip: public binary { - -protected: - - - std::vector w; //band wavelength - unsigned int offset; //header offset - -public: - - using binary::open; - using binary::file; - using binary::R; - - /// Open a data file for reading using the class interface. - - /// @param filename is the name of the binary file on disk - /// @param X is the number of samples along dimension 1 - /// @param Y is the number of samples (lines) along dimension 2 - /// @param B is the number of samples (bands) along dimension 3 - /// @param header_offset is the number of bytes (if any) in the binary header - /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int B, unsigned int header_offset, std::vector wavelengths){ - - //copy the wavelengths to the BSQ file structure - w = wavelengths; - //copy the offset to the structure - offset = header_offset; - - return open(filename, vec(X, Y, B), header_offset); - - } - - /// Retrieve a single band (based on index) and stores it in pre-allocated memory. - - /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. - /// @param page <= B is the integer number of the band to be copied. - bool band_index( T * p, unsigned int page){ - - if (page >= R[2]){ //make sure the bank number is right - std::cout<<"ERROR: page out of range"< wavelength ){ - band_index(p, page); - return true; - } - - while( w[page] < wavelength ) - { - page++; - //if wavelength is larger than the last wavelength in header file - if (page == R[2]) { - band_index(p, R[2]-1); - return true; - } - } - if ( wavelength < w[page] ) - { - T * p1; - T * p2; - p1=(T*)malloc( XY * sizeof(T)); //memory allocation - p2=(T*)malloc( XY * sizeof(T)); - band_index(p1, page - 1); - band_index(p2, page ); - for(unsigned i=0; i < XY; i++){ - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); - p[i] = (p2[i] - p1[i]) * r + p1[i]; - } - free(p1); - free(p2); - } - else //if the wavelength is equal to a wavelength in header file - { - band_index(p, page); - } - - return true; - } - //get YZ line from the a Y slice, Y slice data should be already IN the MEMORY - bool getYZ(T* p, T* c, double wavelength) - { - unsigned int X = R[0]; //calculate the number of pixels in a sample - unsigned int B = R[2]; - - unsigned page=0; //samples around the wavelength - - - //get the bands numbers around the wavelength - - //if wavelength is smaller than the first one in header file - if ( w[page] > wavelength ){ - for(unsigned j = 0; j < R[0]; j++) - { - p[j] = c[j * B]; - } - return true; - } - - while( w[page] < wavelength ) - { - page++; - //if wavelength is larger than the last wavelength in header file - if (page == B) { - for(unsigned j = 0; j < R[0]; j++) - { - p[j] = c[(j + 1) * B - 1]; - } - return true; - } - } - if ( wavelength < w[page] ) - { - T * p1; - T * p2; - p1=(T*)malloc( X * sizeof(T)); //memory allocation - p2=(T*)malloc( X * sizeof(T)); - //band_index(p1, page - 1); - for(unsigned j = 0; j < X; j++) - { - p1[j] = c[j * B + page - 1]; - } - //band_index(p2, page ); - for(unsigned j = 0; j < X; j++) - { - p2[j] = c[j * B + page]; - } - - for(unsigned i=0; i < X; i++){ - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); - p[i] = (p2[i] - p1[i]) * r + p1[i]; - } - free(p1); - free(p2); - } - else //if the wavelength is equal to a wavelength in header file - { - //band_index(p, page); - for(unsigned j = 0; j < X; j++) - { - p[j] = c[j * B + page]; - } - } - - return true; - } - //given a y and a wavelength, return the y-band data - //I do not use it right now, to accelerate the processing speed, I try to read a YZ whole slice into memory first, and then we can call "getYZ" - bool get_x_wavelength(T * p, unsigned y, double wavelength) - { - unsigned int X = R[0]; //calculate the number of pixels in a sample - - unsigned page=0; //samples around the wavelength - T * p1; - T * p2; - - //get the bands numbers around the wavelength - - //if wavelength is smaller than the first one in header file - if ( w[page] > wavelength ){ - file.seekg( y * R[2] * R[0] * sizeof(T), std::ios::beg); - for(unsigned j = 0; j < R[0]; j++) - { - file.read((char *)(p + j), sizeof(T)); - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur); - } - return true; - } - - while( w[page] < wavelength ) - { - page++; - //if wavelength is larger than the last wavelength in header file - if (page == R[2]) { - file.seekg( (y * R[2] * R[0] + R[2] - 1)* sizeof(T), std::ios::beg); - for(unsigned j = 0; j < R[0]; j++) - { - file.read((char *)(p + j), sizeof(T)); - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur); - } - return true; - } - } - if ( wavelength < w[page] ) - { - p1=(T*)malloc( X * sizeof(T)); //memory allocation - p2=(T*)malloc( X * sizeof(T)); - //band_index(p1, page - 1); - file.seekg( (y * R[2] * R[0] + page - 1)* sizeof(T), std::ios::beg); - for(unsigned j = 0; j < R[0]; j++) - { - file.read((char *)(p1 + j), sizeof(T)); - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur); - } - //band_index(p2, page ); - file.seekg( (y * R[2] * R[0] + page)* sizeof(T), std::ios::beg); - for(unsigned j = 0; j < R[0]; j++) - { - file.read((char *)(p2 + j), sizeof(T)); - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur); - } - - for(unsigned i=0; i < R[0]; i++){ - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); - p[i] = (p2[i] - p1[i]) * r + p1[i]; - } - } - else //if the wavelength is equal to a wavelength in header file - { - //band_index(p, page); - file.seekg( (y * R[2] * R[0] + page) * sizeof(T), std::ios::beg); - for(unsigned j = 0; j < R[0]; j++) - { - file.read((char *)(p + j), sizeof(T)); - file.seekg((R[2] - 1) * sizeof(T), std::ios::cur); - } - } - return true; - } - - /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory. - - /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size. - /// @param x is the x-coordinate (dimension 1) of the spectrum. - /// @param y is the y-coordinate (dimension 2) of the spectrum. - bool spectrum(T * p, unsigned x, unsigned y){ - - if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right - std::cout<<"ERROR: sample or line out of range"<= bandnum){ //make sure the pixel number is right - std::cout<<"ERROR: sample or line out of range"<= R[1]){ //make sure the line number is right - std::cout<<"ERROR: line out of range"< wls){ - - unsigned N = wls.size(); //get the number of baseline points - - std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file - std::string headername = outname + ".hdr"; //the header file name - - //simplify image resolution - unsigned int ZX = R[2] * R[0]; //calculate the number of points in a Y slice - unsigned int L = ZX * sizeof(T); //calculate the number of bytes of a Y slice - unsigned int B = R[2]; - unsigned int X = R[0]; - - T* c; //pointer to the current Y slice - c = (T*)malloc(L); //memory allocation - - T* a; //pointer to the two YZ lines surrounding the current YZ line - T* b; - - a = (T*)malloc(X * sizeof(T)); - b = (T*)malloc(X * sizeof(T)); - - - double ai, bi; //stores the two baseline points wavelength surrounding the current band - double ci; //stores the current band's wavelength - unsigned control; - - if (a == NULL || b == NULL || c == NULL){ - std::cout<<"ERROR: error allocating memory"; - exit(1); - } - // loop start correct every y slice - - for (unsigned k =0; k < R[1]; k++) - { - //get the current y slice - getY(c, k); - - //initialize lownum, highnum, low, high - control=0; - ai = w[0]; - //if no baseline point is specified at band 0, - //set the baseline point at band 0 to 0 - if(wls[0] != w[0]){ - bi = wls[control]; - memset(a, (char)0, X * sizeof(T) ); - } - //else get the low band - else{ - control++; - getYZ(a, c, ai); - bi = wls[control]; - } - //get the high band - getYZ(b, c, bi); - - //correct every YZ line - - for(unsigned cii = 0; cii < B; cii++){ - //update baseline points, if necessary - if( w[cii] >= bi && cii != B - 1) { - //if the high band is now on the last BL point? - if (control != N-1) { - - control++; //increment the index - - std::swap(a, b); //swap the baseline band pointers - - ai = bi; - bi = wls[control]; - getYZ(b, c, bi); - - } - //if the last BL point on the last band of the file? - else if ( wls[control] < w[B - 1]) { - - std::swap(a, b); //swap the baseline band pointers - - memset(b, (char)0, X * sizeof(T) ); //clear the high band - - ai = bi; - bi = w[B - 1]; - } - } - - ci = w[cii]; - - //perform the baseline correction - for(unsigned i=0; i < X; i++) - { - double r = (double) (ci - ai) / (double) (bi - ai); - c[i * B + cii] =(T) ( c[i * B + cii] - (b[i] - a[i]) * r - a[i] ); - } - - }//loop for YZ line end - target.write(reinterpret_cast(c), L); //write the corrected data into destination - }//loop for Y slice end - - - - free(a); - free(b); - free(c); - target.close(); - return true; - - } - - /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file. - - /// @param outname is the name of the output file used to store the resulting baseline-corrected data. - /// @param w is the label specifying the band that the hyperspectral image will be normalized to. - /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers. - bool normalize(std::string outname, double w, double t = 0.0) - { - unsigned int B = R[2]; //calculate the number of bands - unsigned int Y = R[1]; - unsigned int X = R[0]; - unsigned int ZX = R[2] * R[0]; - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band - unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band - unsigned int L = ZX * sizeof(T); - - std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file - std::string headername = outname + ".hdr"; //the header file name - - T * c; //pointer to the current ZX slice - T * b; //pointer to the standard band - - b = (T*)malloc( S ); //memory allocation - c = (T*)malloc( L ); - - band(b, w); //get the certain band into memory - - for(unsigned j = 0; j < Y; j++) - { - getY(c, j); - unsigned jX = j * X; //to avoid calculating it many times - for(unsigned i = 0; i < X; i++) - { - unsigned iB = i * B; - for(unsigned m = 0; m < B; m++) - { - if( b[i+jX] < t ) - c[m + iB] = (T)0.0; - else - c[m + iB] = c[m + iB] / b[i + jX]; //perform normalization - } - } - target.write(reinterpret_cast(c), L); //write normalized data into destination - } - - - free(b); - free(c); - target.close(); - return true; - } - - /// Convert the current BIP file to a BSQ file with the specified file name. - - /// @param outname is the name of the output BSQ file to be saved to disk. - bool bsq(std::string outname) - { - std::string temp = outname + "_temp"; - std::string headtemp = temp + ".hdr"; - //first creat a temporary bil file and convert bip file to bil file - bil(temp); - - stim::bil n; - if(n.open(temp, R[0], R[1], R[2], offset, w)==false){ //open infile - std::cout<<"ERROR: unable to open input file"<(q), S); //write a band data into target file - } - - - free(p); - free(q); - target.close(); - return true; - } - - /// Return a baseline corrected band given two adjacent baseline points and their bands. The result is stored in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lp is a pointer to an array holding the band image for the left baseline point - /// @param rp is a pointer to an array holding the band image for the right baseline point - /// @param wavelength is the label value for the requested baseline-corrected band - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. - bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ - - unsigned XY = R[0] * R[1]; - band(result, wavelength); //get band - - //perform the baseline correction - double r = (double) (wavelength - lb) / (double) (rb - lb); - for(unsigned i=0; i < XY; i++){ - result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] ); - } - return true; - } - - /// Return a baseline corrected band given two adjacent baseline points. The result is stored in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param bandwavelength is the label value for the desired baseline-corrected band - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. - bool height(double lb, double rb, double bandwavelength, T* result){ - - T* lp; - T* rp; - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - - band(lp, lb); - band(rp, rb); - - baseline_band(lb, rb, lp, rp, bandwavelength, result); - - free(lp); - free(rp); - return true; - } - - - /// Calculate the area under the spectrum between two specified points and stores the result in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label value for the left bound (start of the integration) - /// @param rab is the label value for the right bound (end of the integration) - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool area(double lb, double rb, double lab, double rab, T* result){ - - T* lp; //left band pointer - T* rp; //right band pointer - T* cur; //current band 1 - T* cur2; //current band 2 - - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - cur = (T*) malloc(S); - cur2 = (T*) malloc(S); - - memset(result, (char)0, S); - - //find the wavelenght position in the whole band - unsigned int n = w.size(); - unsigned int ai = 0; //left bound position - unsigned int bi = n - 1; //right bound position - - - - //to make sure the left and the right bound are in the bandwidth - if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ - std::cout<<"ERROR: left bound or right bound out of bandwidth"< rb){ - std::cout<<"ERROR: right bound should be bigger than left bound"<= w[ai]){ - ai++; - } - while (rab <= w[bi]){ - bi--; - } - - band(lp, lb); - band(rp, rb); - - //calculate the beginning and the ending part - baseline_band(lb, rb, lp, rp, rab, cur2); //ending part - baseline_band(lb, rb, lp, rp, w[bi], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (rab - w[bi]) * (cur[j] + cur2[j]) / 2.0; - } - baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part - baseline_band(lb, rb, lp, rp, w[ai], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (w[ai] - lab) * (cur[j] + cur2[j]) / 2.0; - } - - //calculate the area - ai++; - for(unsigned i = ai; i <= bi ;i++) - { - baseline_band(lb, rb, lp, rp, w[ai], cur2); - for(unsigned j = 0; j < XY; j++) - { - result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0; - } - std::swap(cur,cur2); //swap the band pointers - } - - free(lp); - free(rp); - free(cur); - free(cur2); - return true; - } - - /// Compute the ratio of two baseline-corrected peaks. The result is stored in a pre-allocated array. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param pos1 is the label value for the first peak (numerator) position - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param pos2 is the label value for the second peak (denominator) position - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the two peak band - height(lb1, rb1, pos1, p1); - height(lb2, rb2, pos2, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the ratio between a peak area and peak height. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param pos1 is the label value for the first peak (numerator) position - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param pos2 is the label value for the second peak (denominator) position - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, - double lb2, double rb2, double pos, T* result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - area(lb1, rb1, lab1, rab1, p1); - height(lb2, rb2, pos, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the ratio between two peak areas. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param lab1 is the label value for the left bound (start of the integration) of the first peak (numerator) - /// @param rab1 is the label value for the right bound (end of the integration) of the first peak (numerator) - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) - /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator) - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, - double lb2, double rb2, double lab2, double rab2, T* result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - area(lb1, rb1, lab1, rab1, p1); - area(lb2, rb2, lab2, rab2, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the definite integral of a baseline corrected peak. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label for the start of the definite integral - /// @param rab is the label for the end of the definite integral - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool x_area(double lb, double rb, double lab, double rab, T* result){ - T* lp; //left band pointer - T* rp; //right band pointer - T* cur; //current band 1 - T* cur2; //current band 2 - - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - cur = (T*) malloc(S); - cur2 = (T*) malloc(S); - - memset(result, (char)0, S); - - //find the wavelenght position in the whole band - unsigned int n = w.size(); - unsigned int ai = 0; //left bound position - unsigned int bi = n - 1; //right bound position - - //to make sure the left and the right bound are in the bandwidth - if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ - std::cout<<"ERROR: left bound or right bound out of bandwidth"< rb){ - std::cout<<"ERROR: right bound should be bigger than left bound"<= w[ai]){ - ai++; - } - while (rab <= w[bi]){ - bi--; - } - - band(lp, lb); - band(rp, rb); - - //calculate the beginning and the ending part - baseline_band(lb, rb, lp, rp, rab, cur2); //ending part - baseline_band(lb, rb, lp, rp, w[bi], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (rab - w[bi]) * (rab + w[bi]) * (cur[j] + cur2[j]) / 4.0; - } - baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part - baseline_band(lb, rb, lp, rp, w[ai], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (w[ai] - lab) * (w[ai] + lab) * (cur[j] + cur2[j]) / 4.0; - } - - //calculate f(x) times x - ai++; - for(unsigned i = ai; i <= bi ;i++) - { - baseline_band(lb, rb, lp, rp, w[ai], cur2); - for(unsigned j = 0; j < XY; j++) - { - result[j] += (w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * (cur[j] + cur2[j]) / 4.0; - } - std::swap(cur,cur2); //swap the band pointers - } - - free(lp); - free(rp); - free(cur); - free(cur2); - return true; - } - - /// Compute the centroid of a baseline corrected peak. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label for the start of the peak - /// @param rab is the label for the end of the peak - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool cpoint(double lb, double rb, double lab, double rab, T* result){ - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - x_area(lb, rb, lab, rab, p1); - area(lb, rb, lab, rab, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Create a mask based on a given band and threshold value. - - /// All pixels in the - /// specified band greater than the threshold are true and all pixels less than the threshold are false. - /// @param mask_band is the band used to specify the mask - /// @param threshold is the threshold used to determine if the mask value is true or false - /// @param p is a pointer to a pre-allocated array at least X * Y in size - bool build_mask(double mask_band, double threshold, unsigned char* p){ - - T* temp = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate memory for the certain band - band(temp, mask_band); - - for (unsigned int i = 0; i < R[0] * R[1];i++) { - if (temp[i] < threshold) - p[i] = 0; - else - p[i] = 255; - } - - free(temp); - return true; - - } - - /// Apply a mask file to the BSQ image, setting all values outside the mask to zero. - - /// @param outfile is the name of the masked output file - /// @param p is a pointer to memory of size X * Y, where p(i) = 0 for pixels that will be set to zero. - bool apply_mask(std::string outfile, unsigned char* p){ - - std::ofstream target(outfile.c_str(), std::ios::binary); - - unsigned ZX = R[2] * R[0]; //calculate number of a band - unsigned L = ZX * sizeof(T); - - T * temp = (T*)malloc(L); - - for (unsigned i = 0; i < R[1]; i++) - { - getY(temp, i); - for ( unsigned j = 0; j < R[0]; j++) - { - for (unsigned k = 0; k < R[2]; k++) - { - if(p[i * R[0] + k] == 0) - temp[j * R[2] + k] = 0; - else - continue; - } - } - target.write(reinterpret_cast(temp), L); //write a band data into target file - } - target.close(); - free(temp); - return true; - } - - /// Calculate the mean band value (average along B) at each pixel location. - - /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. - bool band_avg(T* p){ - unsigned long long XY = R[0] * R[1]; - //get every pixel and calculate average value - T* temp = (T*)malloc(sizeof(T) * R[2]); - T sum; - for (unsigned i = 0; i < XY; i++){ - pixel(temp, i); - //calculate the sum value of every value - sum = 0; //initialize sum value - for (unsigned j = 0; j < R[2]; j++){ - sum += temp[j]/(T)R[2]; - } - p[i] = sum; - } - free(temp); - return true; - } - - /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum - - /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(T*p, unsigned char* mask){ - unsigned long long XY = R[0] * R[1]; - T* temp = (T*)malloc(sizeof(T) * R[2]); - //Iinitialize - for (unsigned j = 0; j < R[2]; j++){ - p[j] = 0; - } - //calculate vaild number in a band - unsigned count = 0; - for (unsigned j = 0; j < XY; j++){ - if (mask[j] != 0){ - count++; - } - } - //calculate average number of a band - for (unsigned i = 0; i < XY; i++){ - if (mask[i] != 0){ - pixel(temp, i); - for (unsigned j = 0; j < R[2]; j++){ - p[j] += temp[j] / (T)count; - } - } - } - free(temp); - return true; - } - - /// Calculate the covariance matrix for all masked pixels in the image. - - /// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix - /// @param avg is a pointer to memory of size B that stores the average spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool co_matrix(T* co, T* avg, unsigned char *mask){ - //memory allocation - unsigned long long xy = R[0] * R[1]; - unsigned int B = R[2]; - T* temp = (T*)malloc(sizeof(T) * B); - //count vaild pixels in a band - unsigned count = 0; - for (unsigned j = 0; j < xy; j++){ - if (mask[j] != 0){ - count++; - } - } - //initialize correlation matrix - for (unsigned i = 0; i < B; i++){ - for (unsigned k = 0; k < B; k++){ - co[i * B + k] = 0; - } - } - //calculate correlation coefficient matrix - for (unsigned j = 0; j < xy; j++){ - if (mask[j] != 0){ - pixel(temp, j); - for (unsigned i = 0; i < B; i++){ - for (unsigned k = i; k < B; k++){ - co[i * B + k] += (temp[i] - avg[i]) * (temp[k] - avg[k]) / count; - } - } - } - } - //because correlation matrix is symmetric - for (unsigned i = 0; i < B; i++){ - for (unsigned k = i + 1; k < B; k++){ - co[k * B + i] = co[i * B + k]; - } - } - - free(temp); - return true; - } - - - /// Crop a region of the image and save it to a new file. - - /// @param outfile is the file name for the new cropped image - /// @param x0 is the lower-left x pixel coordinate to be included in the cropped image - /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image - /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image - /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image - bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ - - //calculate the new number of samples and lines - unsigned long long sam = x1 - x0; //samples - unsigned long long lin = y1 - y0; //lines - unsigned long long L = R[2] * sizeof(T); - //get specified band and save - T* temp = (T*)malloc(L); - std::ofstream out(outfile.c_str(), std::ios::binary); - //get start - unsigned long long sp = y0 * R[0] + x0; //start pixel - for (unsigned i = 0; i < lin; i++) - { - for (unsigned j = 0; j < sam; j++) - { - pixel(temp, sp + j + i * R[0]); - out.write(reinterpret_cast(temp), L); //write slice data into target file - } - } - free(temp); - return true; - } - - - /// Close the file. - bool close(){ - file.close(); - return true; - } - - }; -} - -#endif diff --git a/envi/bsq.h b/envi/bsq.h deleted file mode 100644 index 0e696c5..0000000 --- a/envi/bsq.h +++ /dev/null @@ -1,912 +0,0 @@ -#ifndef STIM_BSQ_H -#define STIM_BSQ_H - -#include "../envi/envi_header.h" -#include "../envi/binary.h" -#include "../envi/bil.h" -#include -#include -#include - - - -namespace stim{ - -/** - The BIP class represents a 3-dimensional binary file stored using band sequential (BSQ) image encoding. The binary file is stored - such that X-Y "frames" are stored sequentially to form an image stack along the z-axis. When accessing the data sequentially on disk, - the dimensions read, from fastest to slowest, are X, Y, Z. - - This class is optimized for data streaming, and therefore supports extremely large (terabyte-scale) files. Data is loaded from disk - on request. Functions used to access data are written to support efficient reading. -*/ -template - -class bsq: public binary { - - -protected: - - std::vector w; //band wavelengths - unsigned int offset; - -public: - - using binary::open; - using binary::file; - //using binary::getSlice; - using binary::R; - - /// Open a data file for reading using the class interface. - - /// @param filename is the name of the binary file on disk - /// @param X is the number of samples along dimension 1 - /// @param Y is the number of samples (lines) along dimension 2 - /// @param B is the number of samples (bands) along dimension 3 - /// @param header_offset is the number of bytes (if any) in the binary header - /// @param wavelengths is an optional STL vector of size B specifying a numerical label for each band - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int B, unsigned int header_offset, std::vector wavelengths){ - - //copy the wavelengths to the BSQ file structure - w = wavelengths; - //copy the wavelengths to the structure - offset = header_offset; - - return open(filename, vec(X, Y, B), header_offset); - - } - - /// Retrieve a single band (based on index) and stores it in pre-allocated memory. - - /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. - /// @param page <= B is the integer number of the band to be copied. - bool band_index( T * p, unsigned int page){ - - if (page >= R[2]){ //make sure the bank number is right - std::cout<<"ERROR: page out of range"< wavelength ){ - band_index(p, page); - return true; - } - - while( w[page] < wavelength ) - { - page++; - //if wavelength is larger than the last wavelength in the header file - // (the wavelength is out of bounds) - if (page == R[2]) { - band_index(p, R[2]-1); //return the last band - return true; - } - } - //when the page counter points to the first band above 'wavelength' - if ( wavelength < w[page] ){ - - //do the interpolation - T * p1; - T * p2; - p1=(T*)malloc( XY * sizeof(T)); //memory allocation - p2=(T*)malloc( XY * sizeof(T)); - band_index(p1, page - 1); - band_index(p2, page ); - for(unsigned i=0; i < XY; i++){ - double r = (double) (wavelength - w[page-1]) / (double) (w[page] - w[page-1]); - p[i] = (p2[i] - p1[i]) * r + p1[i]; - } - free(p1); - free(p2); - } - //if the wavelength is equal to a wavelength in header file - else{ - band_index(p, page); //return the band - } - - return true; - } - - /// Retrieve a single spectrum (B-axis line) at a given (x, y) location and stores it in pre-allocated memory. - - /// @param p is a pointer to pre-allocated memory at least B * sizeof(T) in size. - /// @param x is the x-coordinate (dimension 1) of the spectrum. - /// @param y is the y-coordinate (dimension 2) of the spectrum. - bool spectrum(T * p, unsigned x, unsigned y){ - - unsigned int i; - - if ( x >= R[0] || y >= R[1]){ //make sure the sample and line number is right - std::cout<<"ERROR: sample or line out of range"<= bandnum){ //make sure the pixel number is right - std::cout<<"ERROR: sample or line out of range"< wls ) - { - unsigned N = wls.size(); //get the number of baseline points - - std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file - std::string headername = outname + ".hdr"; //the header file name - - //simplify image resolution - unsigned int B = R[2]; //calculate the number of bands - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band - unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band - - double ai, bi; //stores the two baseline points wavelength surrounding the current band - double ci; //stores the current band's wavelength - - unsigned control=0; - - T * a; //pointers to the high and low band images - T * b; - T * c; //pointer to the current image - - a = (T*)malloc( S ); //memory allocation - b = (T*)malloc( S ); - c = (T*)malloc( S ); - - if (a == NULL || b == NULL || c == NULL){ - std::cout<<"ERROR: error allocating memory"; - exit(1); - } - - - //initialize lownum, highnum, low, high - ai=w[0]; - - //if no baseline point is specified at band 0, - //set the baseline point at band 0 to 0 - if(wls[0] != w[0]){ - bi = wls[control]; - memset(a, (char)0, S); - } - //else get the low band - else{ - control += 1; - band(a, ai); - bi = wls[control]; - } - //get the high band - band(b, bi); - - //correct every band - for(unsigned cii = 0; cii < B; cii++){ - - //update baseline points, if necessary - if( w[cii] >= bi && cii != B - 1) { - //if the high band is now on the last BL point? - if (control != N-1) { - - control++; //increment the index - - std::swap(a, b); //swap the baseline band pointers - - ai = bi; - bi = wls[control]; - band(b, bi); - - } - //if the last BL point on the last band of the file? - else if ( wls[control] < w[B - 1]) { - - std::swap(a, b); //swap the baseline band pointers - - memset(b, (char)0, S); //clear the high band - - ai = bi; - bi = w[B - 1]; - } - } - - //get the current band - band_index(c, cii); - ci = w[cii]; - - //perform the baseline correction - for(unsigned i=0; i < XY; i++){ - double r = (double) (ci - ai) / (double) (bi - ai); - c[i] =(T) ( c[i] - (b[i] - a[i]) * r - a[i] ); - } - - target.write(reinterpret_cast(c), S); //write the corrected data into destination - - } - - //header.save(headername); //save the new header file - - free(a); - free(b); - free(c); - target.close(); - return true; - } - - /// Normalize all spectra based on the value of a single band, storing the result in a new BSQ file. - - /// @param outname is the name of the output file used to store the resulting baseline-corrected data. - /// @param w is the label specifying the band that the hyperspectral image will be normalized to. - /// @param t is a threshold specified such that a spectrum with a value at w less than t is set to zero. Setting this threshold allows the user to limit division by extremely small numbers. - bool normalize(std::string outname, double w, double t = 0.0) - { - unsigned int B = R[2]; //calculate the number of bands - unsigned int XY = R[0] * R[1]; //calculate the number of pixels in a band - unsigned int S = XY * sizeof(T); //calculate the number of bytes in a band - - std::ofstream target(outname.c_str(), std::ios::binary); //open the target binary file - std::string headername = outname + ".hdr"; //the header file name - - T * b; //pointers to the certain wavelength band - T * c; //pointer to the current image - - b = (T*)malloc( S ); //memory allocation - c = (T*)malloc( S ); - - band(b, w); //get the certain band into memory - - for(unsigned j = 0; j < B; j++) - { - band_index(c, j); //get the current band into memory - for(unsigned i = 0; i < XY; i++) - { - if(b[i] < t) - c[i] = (T)0.0; - else - c[i] = c[i] / b[i]; - } - target.write(reinterpret_cast(c), S); //write normalized data into destination - } - - //header.save(headername); //save the new header file - - free(b); - free(c); - target.close(); - return true; - } - - /// Convert the current BSQ file to a BIP file with the specified file name. - - /// @param outname is the name of the output BIP file to be saved to disk. - bool bip(std::string outname) - { - std::string temp = outname + "_temp"; - std::string headtemp = temp + ".hdr"; - //first creat a temporary bil file and convert bsq file to bil file - bil(temp); - - stim::bil n; - if(n.open(temp, R[0], R[1], R[2], offset, w)==false){ //open infile - std::cout<<"ERROR: unable to open input file"<(p), L); //write XZ slice data into target file - } - //header.interleave = rts::envi_header::BIL; //change the type of file in header file - //header.save(headername); - - free(p); - target.close(); - return true; - } - - /// Return a baseline corrected band given two adjacent baseline points and their bands. The result is stored in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lp is a pointer to an array holding the band image for the left baseline point - /// @param rp is a pointer to an array holding the band image for the right baseline point - /// @param wavelength is the label value for the requested baseline-corrected band - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. - bool baseline_band(double lb, double rb, T* lp, T* rp, double wavelength, T* result){ - - unsigned XY = R[0] * R[1]; - band(result, wavelength); //get band - - //perform the baseline correction - double r = (double) (wavelength - lb) / (double) (rb - lb); - for(unsigned i=0; i < XY; i++){ - result[i] =(T) (result[i] - (rp[i] - lp[i]) * r - lp[i] ); - } - return true; - } - - /// Return a baseline corrected band given two adjacent baseline points. The result is stored in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param bandwavelength is the label value for the desired baseline-corrected band - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size. - bool height(double lb, double rb, double bandwavelength, T* result){ - - T* lp; - T* rp; - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - - band(lp, lb); - band(rp, rb); - - baseline_band(lb, rb, lp, rp, bandwavelength, result); - - free(lp); - free(rp); - return true; - } - - - /// Calculate the area under the spectrum between two specified points and stores the result in a pre-allocated array. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label value for the left bound (start of the integration) - /// @param rab is the label value for the right bound (end of the integration) - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool area(double lb, double rb, double lab, double rab, T* result){ - - T* lp; //left band pointer - T* rp; //right band pointer - T* cur; //current band 1 - T* cur2; //current band 2 - - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - cur = (T*) malloc(S); - cur2 = (T*) malloc(S); - - memset(result, (char)0, S); - - //find the wavelenght position in the whole band - unsigned int n = w.size(); - unsigned int ai = 0; //left bound position - unsigned int bi = n - 1; //right bound position - - - - //to make sure the left and the right bound are in the bandwidth - if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ - std::cout<<"ERROR: left bound or right bound out of bandwidth"< rb){ - std::cout<<"ERROR: right bound should be bigger than left bound"<= w[ai]){ - ai++; - } - while (rab <= w[bi]){ - bi--; - } - - band(lp, lb); - band(rp, rb); - - //calculate the beginning and the ending part - baseline_band(lb, rb, lp, rp, rab, cur2); //ending part - baseline_band(lb, rb, lp, rp, w[bi], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (rab - w[bi]) * (cur[j] + cur2[j]) / 2.0; - } - baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part - baseline_band(lb, rb, lp, rp, w[ai], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (w[ai] - lab) * (cur[j] + cur2[j]) / 2.0; - } - - //calculate the area - ai++; - for(unsigned i = ai; i <= bi ;i++) - { - baseline_band(lb, rb, lp, rp, w[ai], cur2); - for(unsigned j = 0; j < XY; j++) - { - result[j] += (w[ai] - w[ai-1]) * (cur[j] + cur2[j]) / 2.0; - } - std::swap(cur,cur2); //swap the band pointers - } - - free(lp); - free(rp); - free(cur); - free(cur2); - return true; - } - - /// Compute the ratio of two baseline-corrected peaks. The result is stored in a pre-allocated array. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param pos1 is the label value for the first peak (numerator) position - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param pos2 is the label value for the second peak (denominator) position - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool ph_to_ph(double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, T * result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the two peak band - height(lb1, rb1, pos1, p1); - height(lb2, rb2, pos2, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the ratio between a peak area and peak height. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param pos1 is the label value for the first peak (numerator) position - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param pos2 is the label value for the second peak (denominator) position - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool pa_to_ph(double lb1, double rb1, double lab1, double rab1, - double lb2, double rb2, double pos, T* result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - area(lb1, rb1, lab1, rab1, p1); - height(lb2, rb2, pos, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the ratio between two peak areas. - - /// @param lb1 is the label value for the left baseline point for the first peak (numerator) - /// @param rb1 is the label value for the right baseline point for the first peak (numerator) - /// @param lab1 is the label value for the left bound (start of the integration) of the first peak (numerator) - /// @param rab1 is the label value for the right bound (end of the integration) of the first peak (numerator) - /// @param lb2 is the label value for the left baseline point for the second peak (denominator) - /// @param rb2 is the label value for the right baseline point for the second peak (denominator) - /// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator) - /// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator) - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool pa_to_pa(double lb1, double rb1, double lab1, double rab1, - double lb2, double rb2, double lab2, double rab2, T* result){ - - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - area(lb1, rb1, lab1, rab1, p1); - area(lb2, rb2, lab2, rab2, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Compute the definite integral of a baseline corrected peak. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label for the start of the definite integral - /// @param rab is the label for the end of the definite integral - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool x_area(double lb, double rb, double lab, double rab, T* result){ - T* lp; //left band pointer - T* rp; //right band pointer - T* cur; //current band 1 - T* cur2; //current band 2 - - unsigned XY = R[0] * R[1]; - unsigned S = XY * sizeof(T); - - lp = (T*) malloc(S); //memory allocation - rp = (T*) malloc(S); - cur = (T*) malloc(S); - cur2 = (T*) malloc(S); - - memset(result, (char)0, S); - - //find the wavelenght position in the whole band - unsigned int n = w.size(); - unsigned int ai = 0; //left bound position - unsigned int bi = n - 1; //right bound position - - //to make sure the left and the right bound are in the bandwidth - if (lb < w[0] || rb < w[0] || lb > w[n-1] || rb >w[n-1]){ - std::cout<<"ERROR: left bound or right bound out of bandwidth"< rb){ - std::cout<<"ERROR: right bound should be bigger than left bound"<= w[ai]){ - ai++; - } - while (rab <= w[bi]){ - bi--; - } - - band(lp, lb); - band(rp, rb); - - //calculate the beginning and the ending part - baseline_band(lb, rb, lp, rp, rab, cur2); //ending part - baseline_band(lb, rb, lp, rp, w[bi], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (rab - w[bi]) * (rab + w[bi]) * (cur[j] + cur2[j]) / 4.0; - } - baseline_band(lb, rb, lp, rp, lab, cur2); //beginnning part - baseline_band(lb, rb, lp, rp, w[ai], cur); - for(unsigned j = 0; j < XY; j++){ - result[j] += (w[ai] - lab) * (w[ai] + lab) * (cur[j] + cur2[j]) / 4.0; - } - - //calculate f(x) times x - ai++; - for(unsigned i = ai; i <= bi ;i++) - { - baseline_band(lb, rb, lp, rp, w[ai], cur2); - for(unsigned j = 0; j < XY; j++) - { - result[j] += (w[ai] - w[ai-1]) * (w[ai] + w[ai-1]) * (cur[j] + cur2[j]) / 4.0; - } - std::swap(cur,cur2); //swap the band pointers - } - - free(lp); - free(rp); - free(cur); - free(cur2); - return true; - } - - /// Compute the centroid of a baseline corrected peak. - - /// @param lb is the label value for the left baseline point - /// @param rb is the label value for the right baseline point - /// @param lab is the label for the start of the peak - /// @param rab is the label for the end of the peak - /// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size - bool cpoint(double lb, double rb, double lab, double rab, T* result){ - T* p1 = (T*)malloc(R[0] * R[1] * sizeof(T)); - T* p2 = (T*)malloc(R[0] * R[1] * sizeof(T)); - - //get the area and the peak band - x_area(lb, rb, lab, rab, p1); - area(lb, rb, lab, rab, p2); - //calculate the ratio in result - for(unsigned i = 0; i < R[0] * R[1]; i++){ - if(p1[i] == 0 && p2[i] ==0) - result[i] = 1; - else - result[i] = p1[i] / p2[i]; - } - - free(p1); - free(p2); - return true; - } - - /// Create a mask based on a given band and threshold value. - - /// All pixels in the - /// specified band greater than the threshold are true and all pixels less than the threshold are false. - /// @param mask_band is the band used to specify the mask - /// @param threshold is the threshold used to determine if the mask value is true or false - /// @param p is a pointer to a pre-allocated array at least X * Y in size - bool build_mask(double mask_band, double threshold, unsigned char* p = NULL){ - - T* temp = (T*)malloc(R[0] * R[1] * sizeof(T)); //allocate memory for the certain band - band(temp, mask_band); - - for (unsigned int i = 0; i < R[0] * R[1]; i++) { - if (temp[i] < threshold) - p[i] = 0; - else - p[i] = 255; - } - - free(temp); - return true; - - } - - /// Apply a mask file to the BSQ image, setting all values outside the mask to zero. - - /// @param outfile is the name of the masked output file - /// @param p is a pointer to memory of size X * Y, where p(i) = 0 for pixels that will be set to zero. - bool apply_mask(std::string outfile, unsigned char* p){ - - std::ofstream target(outfile.c_str(), std::ios::binary); - - unsigned XY = R[0] * R[1]; //calculate number of a band - unsigned L = XY * sizeof(T); - - T * temp = (T*)malloc(L); - - for (unsigned i = 0; i < R[2]; i++) - { - band_index(temp, i); - for ( unsigned j = 0; j < XY; j++) - { - if(p[j] == 0){ - temp[j] = 0; - } - else{ - continue; - } - } - target.write(reinterpret_cast(temp), L); //write a band data into target file - } - target.close(); - free(temp); - return true; - } - - /// Calculate the mean band value (average along B) at each pixel location. - - /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages. - bool band_avg(T* p){ - unsigned long long XY = R[0] * R[1]; - T* temp = (T*)malloc(sizeof(T) * XY); - //initialize p - band_index(p, 0); - for (unsigned j = 0; j < XY; j++){ - p[j] /= (T)R[2]; - } - //get every band and add them all - for (unsigned i = 1; i < R[2]; i++){ - band_index(temp, i); - for (unsigned j = 0; j < XY; j++){ - p[j] += temp[j]/(T)R[2]; - } - } - free(temp); - return true; - } - - /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum - - /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(T*p, unsigned char* mask){ - unsigned long long XY = R[0] * R[1]; - unsigned count = 0; //count will store the number of masked pixels - T* temp = (T*)malloc(sizeof(T) * XY); - //calculate this loop counts the number of true pixels in the mask - for (unsigned j = 0; j < XY; j++){ - if (mask[j] != 0){ - count++; - } - } - //this loops goes through each band in B (R[2]) - // masked (or valid) pixels from that band are averaged and the average is stored in p - for (unsigned i = 0; i < R[2]; i++){ - p[i] = 0; - band_index(temp, i); //get the band image and store it in temp - for (unsigned j = 0; j < XY; j++){ //loop through temp, averaging valid pixels - if (mask[j] != 0){ - p[i] += temp[j] / (T)count; - } - } - } - free(temp); - return true; - } - - /// Calculate the covariance matrix for all masked pixels in the image. - - /// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix - /// @param avg is a pointer to memory of size B that stores the average spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool co_matrix(T* co, T* avg, unsigned char *mask){ - //memory allocation - unsigned long long xy = R[0] * R[1]; - unsigned int B = R[2]; - T* bandi = (T*)malloc(sizeof(T) * xy); - T* bandj = (T*)malloc(sizeof(T) * xy); - - //count vaild pixels in a band - unsigned count = 0; - for (unsigned j = 0; j < xy; j++){ - if (mask[j] != 0){ - count++; - } - } - //calculate correlation coefficient matrix - for (unsigned i = 0; i < B; i++) - { - band_index(bandi, i); - for (unsigned j = i; j < B; j++){ - band_index(bandj, j); - T numerator = 0; //to calculate element in correlation coefficient matrix, numerator part - //calculate the R(i,j) in correlation coeffient matrix - for (unsigned k = 0; k < xy; k++){ - if (mask[k] != 0){ - numerator += (bandi[k] - avg[i]) * (bandj[k] - avg[j]) / count; - } - } - co[i*B + j] = numerator; - co[j*B + i] = numerator; //because correlated matrix is a symmetric matrix - } - } - free(bandi); - free(bandj); - return true; - } - - - /// Crop a region of the image and save it to a new file. - - /// @param outfile is the file name for the new cropped image - /// @param x0 is the lower-left x pixel coordinate to be included in the cropped image - /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image - /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image - /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image - bool crop(std::string outfile, unsigned x0, unsigned y0, unsigned x1, unsigned y1){ - - //calculate the new number of samples and lines - unsigned long long sam = x1 - x0; //samples - unsigned long long lin = y1 - y0; //lines - unsigned long long L = sam * lin * sizeof(T); - //get specified band and save - T* temp = (T*)malloc(L); - std::ofstream out(outfile.c_str(), std::ios::binary); - unsigned long long jumpb = R[0] * (R[1] - lin) * sizeof(T); //jump pointer to the next band - unsigned long long jumpl = (R[0] - sam) * sizeof(T); //jump pointer to the next line - //get start - file.seekg((y0 * R[0] + x0) * sizeof(T), std::ios::beg); - for (unsigned i = 0; i < R[2]; i++) - { - for (unsigned j = 0; j < lin; j++) - { - file.read((char *)(temp + j * sam), sizeof(T) * sam); - file.seekg(jumpl, std::ios::cur); //go to the next band - } - out.write(reinterpret_cast(temp), L); //write slice data into target file - file.seekg(jumpb, std::ios::cur); - } - free(temp); - return true; - } - - - /// Close the file. - bool close(){ - file.close(); - return true; - } - - }; -} - -#endif - - diff --git a/envi/envi.h b/envi/envi.h deleted file mode 100644 index 7022625..0000000 --- a/envi/envi.h +++ /dev/null @@ -1,895 +0,0 @@ -#ifndef STIM_ENVI_H -#define STIM_ENVI_H - -#include "../envi/envi_header.h" -#include "../envi/bsq.h" -#include "../envi/bip.h" -#include "../envi/bil.h" -#include -#include "../image/image.h" - -namespace stim{ - -/** This class implements reading of ENVI hyperspectral files. These files can be stored in multiple orientations - (including BSQ, BIP, and BIL) in order to optimize streaming speed depending on applications. Basic ENVI - files are stored on disk as a large binary file with a corresponding header. Code for reading and processing - ENVI header files is in the envi_header class. -*/ -class envi{ - - void* file; //void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type) - -public: - - envi_header header; - - /// Allocate memory for a new ENVI file based on the current interleave format (BIP, BIL, BSQ) and data type. - bool allocate(){ - - file = NULL; //set file to a NULL pointer - - if(header.interleave == envi_header::BSQ){ - if(header.data_type ==envi_header::float32) - return(file = new bsq()); - else if(header.data_type == envi_header::float64) - return(file = new bsq()); - } - else if(header.interleave == envi_header::BIP){ - if(header.data_type ==envi_header::float32) - return(file = new bip()); - else if(header.data_type == envi_header::float64) - return(file = new bip()); - } - else if(header.interleave == envi_header::BIL){ - if(header.data_type ==envi_header::float32) - return(file = new bil()); - else if(header.data_type == envi_header::float64) - return(file = new bil()); - } - - exit(1); //if the function hasn't already returned, we don't handle this state - - } - - /// Open an existing ENVI file given the file and header names. - - /// @param filename is the name of the ENVI binary file - /// @param headername is the name of the ENVI header file - bool open(std::string filename, std::string headername){ - - //allocate memory - allocate(); - - //load the header - header.load(headername); - - //load the file - if(header.interleave == envi_header::BSQ) { //if the infile is bsq file - if(header.data_type == envi_header::float32) { - return ((bsq*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); - } - else if(header.data_type == envi_header::float64) { - return ((bsq*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); - } - else - return false; - } - - else if(header.interleave == envi_header::BIL) { //if the infile is bil file - if(header.data_type == envi_header::float32) { - return ((bil*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); - } - else if(header.data_type == envi_header::float64) { - return ((bil*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); - } - else - return false; - } - - else if(header.interleave == envi_header::BIP) { //if the infile is bip file - if(header.data_type == envi_header::float32) { - return ((bip*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); - } - else if(header.data_type == envi_header::float64) { - return ((bip*)file)->open(filename, header.samples, header.lines, header.bands, header.header_offset, header.wavelength); - } - else - return false; - } - - else{ - std::cout<<"ERROR: unidentified type file "< threshold (preventing division by small numbers) - bool normalize(std::string outfile, double band, double threshold = 0.0){ - - if(header.interleave == envi_header::BSQ){ //if the infile is bsq file - if(header.data_type ==envi_header::float32) - return ((bsq*)file)->normalize(outfile, band, threshold); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->normalize(outfile,band, threshold); - else - std::cout<<"ERROR: unidentified data type"<*)file)->normalize(outfile, band); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->normalize(outfile,band); - else - std::cout<<"ERROR: unidentified data type"<*)file)->normalize(outfile, band); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->normalize(outfile,band); - else - std::cout<<"ERROR: unidentified data type"< w){ - - if(header.interleave == envi_header::BSQ){ //if the infile is bsq file - if(header.data_type ==envi_header::float32) - return ((bsq*)file)->baseline(outfile, w); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->baseline(outfile,w); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->baseline(outfile, w); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->baseline(outfile, w); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->baseline(outfile, w); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->baseline(outfile, w); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->bil(outfile); - else if(interleave == envi_header::BIP) //if the target file is bip file - return ((bsq*)file)->bip(outfile); - } - - else if(header.data_type == envi_header::float64){ //if the data type is float - if(interleave == envi_header::BSQ){ - std::cout<<"ERROR: is already BSQ file"<*)file)->bil(outfile); - else if(interleave == envi_header::BIP) - return ((bsq*)file)->bip(outfile); - } - - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->bsq(outfile); - else if(interleave == envi_header::BIP) //if the target file is bip file - return ((bil*)file)->bip(outfile); - } - - else if(header.data_type == envi_header::float64){ //if the data type is float - if(interleave == envi_header::BIL){ - std::cout<<"ERROR: is already BIL file"<*)file)->bsq(outfile); - else if(interleave == envi_header::BIP) - return ((bil*)file)->bip(outfile); - } - - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->bil(outfile); - else if(interleave == envi_header::BSQ) //if the target file is bsq file - return ((bip*)file)->bsq(outfile); - } - - else if(header.data_type == envi_header::float64){ //if the data type is float - if(interleave == envi_header::BIP){ - std::cout<<"ERROR: is already BIP file"<*)file)->bil(outfile); - else if(interleave == envi_header::BSQ) //if the target file is bsq file - return ((bip*)file)->bsq(outfile); - } - - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->build_mask(mask_band, threshold, p); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->build_mask(mask_band, threshold, p); - else - std::cout<<"ERROR: unidentified data type"<*)file)->build_mask(mask_band, threshold, p); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->build_mask(mask_band, threshold, p); - else - std::cout<<"ERROR: unidentified data type"<*)file)->build_mask(mask_band, threshold, p); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->build_mask(mask_band, threshold, p); - else - std::cout<<"ERROR: unidentified data type"<*)file)->apply_mask(outfile, p); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->apply_mask(outfile, p); - else - std::cout<<"ERROR: unidentified data type"<*)file)->apply_mask(outfile, p); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->apply_mask(outfile, p); - else - std::cout<<"ERROR: unidentified data type"<*)file)->apply_mask(outfile, p); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->apply_mask(outfile, p); - else - std::cout<<"ERROR: unidentified data type"<*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->ph_to_ph(lb1, rb1, pos1, lb2, rb2, pos2, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->pa_to_ph(lb1, rb1, lab1, rab1, lb2, rb2, pos, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->pa_to_pa(lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->cpoint(lb1, rb1, lab1, rab1, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->cpoint(lb1, rb1, lab1, rab1, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->cpoint(lb1, rb1, lab1, rab1, (float*)result); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->cpoint(lb1, rb1, lab1, rab1, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->cpoint(lb1, rb1, lab1, rab1,(float*)result); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->cpoint(lb1, rb1, lab1, rab1, (double*)result); - else - std::cout<<"ERROR: unidentified data type"<*)file)->close(); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->close(); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->close(); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->close(); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->close(); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->close(); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); - else if(header.data_type == envi_header::float64) - return ((bsq*)file)->pixel((double*)p, n); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); - else if(header.data_type == envi_header::float64) - return ((bil*)file)->pixel((double*)p, n); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->pixel((float*)p, n); - else if(header.data_type == envi_header::float64) - return ((bip*)file)->pixel((double*)p, n); - else{ - std::cout<<"ERROR: unidentified data type"<*)file)->band((float*)ptr, wavelength); - else if (header.data_type == envi_header::float64) - return ((bsq*)file)->band((double*)ptr, wavelength); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIL){ - if (header.data_type == envi_header::float32) - return ((bil*)file)->band((float*)ptr, wavelength); - else if (header.data_type == envi_header::float64) - return ((bil*)file)->band((double*)ptr, wavelength); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIP){ - if (header.data_type == envi_header::float32) - return ((bip*)file)->band((float*)ptr, wavelength); - else if (header.data_type == envi_header::float64) - return ((bip*)file)->band((double*)ptr, wavelength); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - return false; - } - - /// Retrieve a single band (based on index) and stores it in pre-allocated memory. - - /// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size. - /// @param page <= B is the integer number of the band to be copied. - bool band_index(void* ptr, unsigned int b){ - if (header.interleave == envi_header::BSQ){ //if the infile is bsq file - if (header.data_type == envi_header::float32) - return ((bsq*)file)->band_index((float*)ptr, b); - else if (header.data_type == envi_header::float64) - return ((bsq*)file)->band_index((double*)ptr, b); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIL){ - if (header.data_type == envi_header::float32) - return ((bil*)file)->band_index((float*)ptr, b); - else if (header.data_type == envi_header::float64) - return ((bil*)file)->band_index((double*)ptr, b); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIP){ - if (header.data_type == envi_header::float32) - return ((bip*)file)->band_index((float*)ptr, b); - else if (header.data_type == envi_header::float64) - return ((bip*)file)->band_index((double*)ptr, b); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - return false; - } - - /// Helper function that loads a mask into memory given a filename. - - /// @param mask is a pointer to pre-allocated memory of size X*Y - /// @param maskname is the file name for the image that will serve as the mask - bool load_mask(unsigned char * mask, std::string maskname){ - //open the mask file - stim::image mask_image(maskname); - mask_image.data_noninterleaved(mask); - //save mask file into memory - //memcpy(mask, mask_image.data_noninterleaved(), mask_image.size()); - //mask_image.clear(); - return true; - } - - //p:start positon; N: number of pixels saved in X; - bool feature_matrix(void * X, unsigned char * mask, unsigned start, unsigned N) - { - //save pixels in X as floating numbers: float * p - float * p = (float*)malloc(header.bands * sizeof(float)); //save pixel information - unsigned pixels = header.samples * header.lines; //calculate pixel numbers in a band - unsigned count = 0; //for counting use - unsigned j = 0; //memory the pointer location in X - - //create two indices into the mask (mask index and pixel index) - unsigned mi = 0; //valid pixel index - unsigned pi = 0; //actual pixel in the mask - - //find the actual pixel index for the mask index "start" - while(mi < start){ - if(mask[pi]) - mi++; - - pi++; - } - - for(unsigned i = pi; i < pixels; i++){ - if(mask[i] != 0){ - pixel(p, i); - //copy p to X - for(unsigned k = 0; k < header.bands; k++){ - ((float *)X)[j] = p[k]; - j++; - } - count++; - if(count == N) - break; - } - else - continue; - } - if(count < N){ - std::cout << "number of valid pixels in the mask : " << count <<"is less than N: "<< N; - exit(1); - } - free(p); - return true; - } - - /// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum - - /// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool avg_band(void * p, unsigned char* mask){ - if (header.interleave == envi_header::BSQ){ - if (header.data_type == envi_header::float32) - return ((bsq*)file)->avg_band((float*)p, mask); - else if (header.data_type == envi_header::float64) - return ((bsq*)file)->avg_band((double*)p, mask); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIL){ - if (header.data_type == envi_header::float32) - return ((bil*)file)->avg_band((float*)p, mask); - else if (header.data_type == envi_header::float64) - return ((bil*)file)->avg_band((double*)p, mask); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIP){ - if (header.data_type == envi_header::float32) - return ((bip*)file)->avg_band((float*)p, mask); - else if (header.data_type == envi_header::float64) - return ((bip*)file)->avg_band((double*)p, mask); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - return false; - } - - /// Calculate the covariance matrix for all masked pixels in the image. - - /// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix - /// @param avg is a pointer to memory of size B that stores the average spectrum - /// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location - bool co_matrix(void* co, void* avg, unsigned char* mask){ - if (header.interleave == envi_header::BSQ){ - if (header.data_type == envi_header::float32) - return ((bsq*)file)->co_matrix((float*)co, (float*)avg, mask); - else if (header.data_type == envi_header::float64) - return ((bsq*)file)->co_matrix((double*)co, (double*)avg, mask); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIL){ - if (header.data_type == envi_header::float32) - return ((bil*)file)->co_matrix((float*)co, (float*)avg, mask); - else if (header.data_type == envi_header::float64) - return ((bil*)file)->co_matrix((double*)co, (double*)avg, mask); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIP){ - if (header.data_type == envi_header::float32) - return ((bip*)file)->co_matrix((float*)co, (float*)avg, mask); - else if (header.data_type == envi_header::float64) - return ((bip*)file)->co_matrix((double*)co, (double*)avg, mask); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - return false; - } - - - /// Crop a region of the image and save it to a new file. - - /// @param outfile is the file name for the new cropped image - /// @param x0 is the lower-left x pixel coordinate to be included in the cropped image - /// @param y0 is the lower-left y pixel coordinate to be included in the cropped image - /// @param x1 is the upper-right x pixel coordinate to be included in the cropped image - /// @param y1 is the upper-right y pixel coordinate to be included in the cropped image - bool crop(std::string outfile,unsigned x0, unsigned y0, unsigned x1, unsigned y1){ - - if (header.interleave == envi_header::BSQ){ - if (header.data_type == envi_header::float32) - return ((bsq*)file)->crop(outfile, x0, y0, x1, y1); - else if (header.data_type == envi_header::float64) - return ((bsq*)file)->crop(outfile, x0, y0, x1, y1); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIL){ - if (header.data_type == envi_header::float32) - return ((bil*)file)->crop(outfile, x0, y0, x1, y1); - else if (header.data_type == envi_header::float64) - return ((bil*)file)->crop(outfile, x0, y0, x1, y1); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - else if (header.interleave == envi_header::BIP){ - if (header.data_type == envi_header::float32) - return ((bip*)file)->crop(outfile, x0, y0, x1, y1); - else if (header.data_type == envi_header::float64) - return ((bip*)file)->crop(outfile, x0, y0, x1, y1); - else{ - std::cout << "ERROR: unidentified data type" << std::endl; - exit(1); - } - } - return false; - } - -}; - -} //end namespace rts - -#endif diff --git a/envi/envi_header.h b/envi/envi_header.h deleted file mode 100644 index 6650557..0000000 --- a/envi/envi_header.h +++ /dev/null @@ -1,413 +0,0 @@ -#ifndef ENVI_HEADER_H -#define ENVI_HEADER_H - -#include -#include -#include -#include -#include -#include - -//information from an ENVI header file -//A good resource can be found here: http://www.exelisvis.com/docs/enviheaderfiles.html - -namespace stim{ - -struct envi_header -{ - enum dataType {dummy0, int8, int16, int32, float32, float64, complex32, dummy7, dummy8, complex64, dummy10, dummy11, uint16, uint32, int64, uint64}; - enum interleaveType {BIP, BIL, BSQ}; //bip = Z,X,Y; bil = X,Z,Y; bsq = X,Y,Z - enum endianType {endianLittle, endianBig}; - - std::string name; - - std::string description; - - unsigned int samples; //x-axis - unsigned int lines; //y-axis - unsigned int bands; //spectral axis - unsigned int header_offset; //header offset for binary file (in bytes) - std::string file_type; //should be "ENVI Standard" - - envi_header::dataType data_type; //data representation; common value is 4 (32-bit float) - - - envi_header::interleaveType interleave; - - std::string sensor_type; //not really used - - envi_header::endianType byte_order; //little = least significant bit first (most systems) - - double x_start, y_start; //coordinates of the upper-left corner of the image - std::string wavelength_units; //stored wavelength units - std::string z_plot_titles[2]; - - double pixel_size[2]; //pixel size along X and Y - - std::vector band_names; //name for each band in the image - std::vector wavelength; //wavelength for each band - - void init(){ - name = ""; - - //specify default values for a new or empty ENVI file - samples = 0; - lines = 0; - bands = 0; - header_offset = 0; - data_type = float32; - interleave = BSQ; - byte_order = endianLittle; - x_start = y_start = 0; - pixel_size[0] = pixel_size[1] = 1; - - //strings - file_type = "ENVI Standard"; - sensor_type = "Unknown"; - wavelength_units = "Unknown"; - z_plot_titles[0] = z_plot_titles[1] = "Unknown"; - } - - envi_header(){ - init(); - } - envi_header(std::string name){ - init(); - load(name); - } - - std::string trim(std::string line){ - //trims whitespace from the beginning and end of line - unsigned int start_i, end_i; - for(start_i=0; start_i < line.length(); start_i++) - if(line[start_i] != 32) - { - break; - } - - for(end_i = line.length()-1; end_i >= start_i; end_i--) - if(line[end_i] != ' ' && line[end_i] != '\r') - { - break; - } - - return line.substr(start_i, end_i - start_i+1); - } - - - std::string get_token(std::string line){ - //returns a variable name; in this case we look for the '=' sign - size_t i = line.find_first_of('='); - - std::string result; - if(i != std::string::npos) - result = trim(line.substr(0, i-1)); - - return result; - } - - std::string get_data_str(std::string line){ - size_t i = line.find_first_of('='); - - std::string result; - if(i != std::string::npos) - result = trim(line.substr(i+1)); - else - { - std::cout<<"ENVI Header error - data not found for token: "< get_string_seq(std::string token, std::string sequence) - { - //this function returns a sequence of comma-delimited strings - std::vector result; - - std::string entry; - size_t i; - do - { - i = sequence.find_first_of(','); - entry = sequence.substr(0, i); - sequence = sequence.substr(i+1); - result.push_back(trim(entry)); - }while(i != std::string::npos); - - return result; - } - - std::vector get_double_seq(std::string token, std::string sequence) - { - //this function returns a sequence of comma-delimited strings - std::vector result; - std::string entry; - size_t i; - do - { - i = sequence.find_first_of(','); - entry = sequence.substr(0, i); - sequence = sequence.substr(i+1); - result.push_back(atof(entry.c_str())); - //std::cout<>line; - if(line != "ENVI") - { - std::cout<<"ENVI Header Error: The header doesn't appear to be an ENVI file. The first line should be 'ENVI'."< pxsize = get_double_seq(token, string_sequence); - pixel_size[0] = pxsize[0]; - pixel_size[1] = pxsize[1]; - } - else if(token == "z plot titles") - { - std::string string_sequence = get_brace_str(token, line, file); - std::vector titles = get_string_seq(token, string_sequence); - z_plot_titles[0] = titles[0]; - z_plot_titles[1] = titles[1]; - } - - else if(token == "samples") - samples = atoi(get_data_str(line).c_str()); - else if(token == "lines") - lines = atoi(get_data_str(line).c_str()); - else if(token == "bands") - bands = atoi(get_data_str(line).c_str()); - else if(token == "header offset") - header_offset = atoi(get_data_str(line).c_str()); - else if(token == "file type") - file_type = get_data_str(line); - else if(token == "data type") - data_type = (dataType)atoi(get_data_str(line).c_str()); - else if(token == "interleave") - { - std::string interleave_str = get_data_str(line); - if(interleave_str == "bip") - interleave = BIP; - else if(interleave_str == "bil") - interleave = BIL; - else if(interleave_str == "bsq") - interleave = BSQ; - } - else if(token == "sensor type") - sensor_type = get_data_str(line); - else if(token == "byte order") - byte_order = (endianType)atoi(get_data_str(line).c_str()); - else if(token == "x start") - x_start = atof(get_data_str(line).c_str()); - else if(token == "y start") - y_start = atof(get_data_str(line).c_str()); - else if(token == "wavelength units") - wavelength_units = get_data_str(line); - - //get the next line - getline(file, line); - } - - //make sure the number of bands matches the number of wavelengths - unsigned int wavelengths = wavelength.size(); - if(wavelengths && bands != wavelengths) - { - std::cout<<"ENVI Header Error -- Number of wavelengths doesn't match the number of bands. Bands = "< 0) - { - outfile<<"band names = {"< -#include -#include - -#define CHECK_OPENGL_ERROR \ -{ GLenum error; \ - while ( (error = glGetError()) != GL_NO_ERROR) { \ - printf( "OpenGL ERROR: %s\nCHECK POINT: %s (line %d)\n", gluErrorString(error), __FILE__, __LINE__ ); \ - } \ -} - -#endif \ No newline at end of file diff --git a/gl/gl_spider.h b/gl/gl_spider.h deleted file mode 100644 index c28c257..0000000 --- a/gl/gl_spider.h +++ /dev/null @@ -1,580 +0,0 @@ -#ifndef STIM_GL_SPIDER_H -#define STIM_GL_SPIDER_H - -#include -#include -#include -#include -#include -#include -#include "gl_texture.h" -#include "../visualization/camera.h" -#include "./error.h" -#include "../math/vector.h" -#include "../math/rect.h" -#include "../cuda/cost.h" -#include "../cuda/glbind.h" - -namespace stim -{ - -template -class gl_spider : public virtual gl_texture -{ - //doen't use gl_texture really, just needs the GLuint id. - //doesn't even need the texture iD really. - private: - stim::camera rotator; - stim::vec position; //vector designating the position of the spider. - stim::vec direction; //vector designating the orientation of the spider - //always a unit vector. - stim::vec magnitude; //magnitude of the direction vector. - //mag[0] = length. - //mag[1] = width. - using gl_texture::texID; - //float (*S)[][]; - //using image_stack::S; - cudaArray* c_Array; - //void** devPtr; - //size_t size; - cudaGraphicsResource_t resource; - GLuint fboID; - GLuint texbufferID; - - void - findOptimalDirection() - { - /* Method for finding the best direction for the spider. - Uses the camera to rotate. Then Calls Evaluate to find new cost. - */ - } - - void - findOptimalPosition() - { - /* Method for finding the best direction for the spider. - Not sure if necessary since the next position for the spider - will be at direction * magnitude. */ - } - - void - findOptimalScale() - { - /* Method for finding the best scale for the spider. - changes the x, y, z size of the spider to minimize the cost - function. */ - } - - void - Evaluate() - { - /* Uses uniform sampler2D in order to take a difference between - the colors of two textures. 1st texture is the spider template, - the 2nd is the location of the spider's overlap with the - gl_template - - does the spider need to track it's location? Prob not since - position can be set with gl_texture coordinates */ - - } - - void - Optimize() - { - /*find the optimum direction and scale */ - } - /* - void - Step() - { - // move to the new position - } - */ - - void - GenerateFBO(unsigned int width, unsigned int height) - { - glGenFramebuffers(1, &fboID); - glBindFramebuffer(GL_FRAMEBUFFER, fboID); - int numChannels = 1; - unsigned char* texels = new unsigned char[width * height * numChannels]; - glGenTextures(1, &texbufferID); - glBindTexture(GL_TEXTURE_2D, texbufferID); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels); - delete[] texels; - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - } - - void - UpdateBuffer(float v_x, float v_y) - { - //std::cout << v_y << std::endl; - float len = 10.0; - stim::vecp1; - stim::vecp2; - stim::vecp3; - stim::vecp4; - p1 = hor.p(1,1); - p2 = hor.p(1,0); - p3 = hor.p(0,0); - p4 = hor.p(0,1); - glBegin(GL_QUADS); - glTexCoord3f( - p1[0], - p1[1], - p1[2] - ); - //glVertex2f(0.0,0.0); - glVertex2f(v_x,v_y); - glTexCoord3f( - p2[0], - p2[1], - p2[2] - ); - //glVertex2f(1.0, 0.0); - glVertex2f(v_x+len, v_y); - glTexCoord3f( - p3[0], - p3[1], - p3[2] - ); - //glVertex2f(1.0, 2.0); - glVertex2f(v_x+len, v_y+len); - glTexCoord3f( - p4[0], - p4[1], - p4[2] - ); - //glVertex2f(0.0, 2.0); - glVertex2f(v_x, v_y+len); - glEnd(); - p1 = ver.p(1,1); - p2 = ver.p(1,0); - p3 = ver.p(0,0); - p4 = ver.p(0,1); - glBegin(GL_QUADS); - glTexCoord3f( - p1[0], - p1[1], - p1[2] - ); - //glVertex2f(1.0, 0.0); - glVertex2f(v_x+len, v_y); - glTexCoord3f( - p2[0], - p2[1], - p2[2] - ); - //glVertex2f(2.0, 0.0); - glVertex2f(v_x+2*len, v_y); - glTexCoord3f( - p3[0], - p3[1], - p3[2] - ); - //glVertex2f(2.0, 2.0); - glVertex2f(v_x+2*len, v_y+len); - glTexCoord3f( - p4[0], - p4[1], - p4[2] - ); - //glVertex2f(1.0, 2.0); - glVertex2f(v_x+len, v_y+len); - glEnd(); - } - - void - Update(float v_x, float v_y, vec dir) - { - vec Y(1.0,0.0,0.0); - if(cos(Y.dot(dir))< 0.087){ - Y[0] = 0.0; Y[1] = 1.0;} - hor = stim::rect(magnitude, position, dir.norm(), - ((Y.cross(dir)).cross(dir)).norm()); - ver = stim::rect(magnitude, position, dir.norm(), - hor.n()); - UpdateBuffer(v_x, v_y); - } - - - void - Sample(vec in = (0,0,1), int numSamples = 1089, int solidAngle = M_PI/2) - { - GenerateFBO(20, numSamples*10); - - float samples[numSamples][3]; //Set up the variables - //necessary for sample generation - vec d_s = in.cart2sph(); - vec temp; - int dim = (sqrt(numSamples)-1)/2; - std::cout << dim << std::endl; - float y_0 = 0.0; - float len = 10.0; - float p0 = M_PI/3; - float dt = solidAngle/(1.0 * dim); - float dp = p0/(1.0*dim); - - - glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer - glFramebufferTexture2D( - GL_FRAMEBUFFER, - GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, - texbufferID, - 0); - glBindFramebuffer(GL_FRAMEBUFFER, fboID); - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; - glDrawBuffers(1, DrawBuffers); - glBindTexture(GL_TEXTURE_2D, texbufferID); - glClearColor(0,0,0,0); - glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glViewport(0,0,2.0*len, numSamples*len); - gluOrtho2D(0.0,2.0*len,0.0,numSamples*len); - glEnable(GL_TEXTURE_3D); - glBindTexture(GL_TEXTURE_3D, texID); - - //Loop over the samples - int idx; - for(int i = -dim; i <= dim; i++){ - for(int j = -dim; j <= dim; j++){ - //Create linear index - idx = (i+dim)*(dim*2+1) + (j+dim); - // std::cerr << i+dim << "," << j+dim << ":" << idx << std::endl; - - temp[0] = 1; //rotate vector - temp[1] = d_s[1]+dt*i; - temp[2] = d_s[2]+dp*j; - - temp = temp.sph2cart(); //back to cart - samples[idx][0] = temp[0]; //save sample vector - samples[idx][1] = temp[1]; - samples[idx][2] = temp[2]; - - Update(0.0, y_0+(idx)*10, temp); - } - } - //Finalize GL_buffer - glBindTexture(GL_TEXTURE_3D, 0); - glDisable(GL_TEXTURE_3D); - glBindFramebuffer(GL_FRAMEBUFFER,0); - glBindTexture(GL_TEXTURE_2D, 0); - } - - public: - - stim::rect hor; - stim::rect ver; - - - gl_spider - () - { - setPosition(0.0,0.0,0.0); - setDirection(1.0,1.0,1.0); - setMagnitude(0.1,0.1); - //GenerateFBO(400,200); - //Update(); - } - - gl_spider - (vec pos, vec dir, vec mag) - { - position = pos; - direction = dir; - magnitude = mag; - //GenerateFBO(400,200); - //Update(); - } - //temporary cost for convenience. - gl_spider - (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, - float mag_x, float mag_y) - { - setPosition(pos_x, pos_y, pos_z); - setDirection(dir_x, dir_y, dir_z); - setMagnitude(mag_x, mag_y); - //GenerateFBO(400,200); - //Update(); - } - - void - attachSpider(GLuint id) - { - texID = id; - Sample(direction); - //GenerateFBO(20,10000); - // Update(); - // generateVectorField(direction, 4.0); - } - - void - Update() - { - vec Y(1.0,0.0,0.0); - if(cos(Y.dot(direction))< 0.087){ - Y[0] = 0.0; Y[1] = 1.0;} - hor = stim::rect(magnitude, position, direction.norm(), - ((Y.cross(direction)).cross(direction)).norm()); - ver = stim::rect(magnitude, position, direction.norm(), - hor.n()); - //UpdateBuffer(); - generateVectorField(direction, 4.0); - } - - - vec - getPosition() - { - return position; - } - - vec - getDirection() - { - return direction; - } - - vec - getMagnitude() - { - return magnitude; - } - - void - setPosition(vec pos) - { - position = pos; - } - - void - setPosition(float x, float y, float z) - { - position[0] = x; - position[1] = y; - position[2] = z; - } - - void - setDirection(vec dir) - { - direction = dir; - } - - void - setDirection(float x, float y, float z) - { - direction[0] = x; - direction[1] = y; - direction[2] = z; - } - - void - setMagnitude(vec mag) - { - magnitude = mag; - } - - void - setMagnitude(float x, float y) - { - magnitude[0] = x; - magnitude[1] = y; - } - - GLuint - getFB() - { - return fboID; - } - - void - Step() - { - std::cout << position[0] << "," << position[1] << "," << position[1] - << std::endl; - setPosition(direction*magnitude[1]/2+position); - Update(); - std::cout << position[0] << "," << position[1] << "," << position[1] - << std::endl; - - } - - void - UpdateBuffer() - { - stim::vecp1; - stim::vecp2; - stim::vecp3; - stim::vecp4; - glBindFramebuffer(GL_FRAMEBUFFER, fboID); - glFramebufferTexture2D( - GL_FRAMEBUFFER, - GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, - texbufferID, - 0); - glBindFramebuffer(GL_FRAMEBUFFER, fboID); - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; - glDrawBuffers(1, DrawBuffers); - glBindTexture(GL_TEXTURE_2D, texbufferID); - glClearColor(0,0,0,0); - glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glViewport(0,0,400,200); - gluOrtho2D(0.0,2.0,0.0,2.0); - glEnable(GL_TEXTURE_3D); - glBindTexture(GL_TEXTURE_3D, texID); - p1 = hor.p(1,1); - p2 = hor.p(1,0); - p3 = hor.p(0,0); - p4 = hor.p(0,1); - glBegin(GL_QUADS); - glTexCoord3f( - p1[0], - p1[1], - p1[2] - ); - glVertex2f(0.0,0.0); - glTexCoord3f( - p2[0], - p2[1], - p2[2] - ); - glVertex2f(1.0, 0.0); - glTexCoord3f( - p3[0], - p3[1], - p3[2] - ); - glVertex2f(1.0, 2.0); - glTexCoord3f( - p4[0], - p4[1], - p4[2] - ); - glVertex2f(0.0, 2.0); - glEnd(); - p1 = ver.p(1,1); - p2 = ver.p(1,0); - p3 = ver.p(0,0); - p4 = ver.p(0,1); - glBegin(GL_QUADS); - glTexCoord3f( - p1[0], - p1[1], - p1[2] - ); - glVertex2f(1.0, 0.0); - glTexCoord3f( - p2[0], - p2[1], - p2[2] - ); - glVertex2f(2.0, 0.0); - glTexCoord3f( - p3[0], - p3[1], - p3[2] - ); - glVertex2f(2.0, 2.0); - glTexCoord3f( - p4[0], - p4[1], - p4[2] - ); - glVertex2f(1.0, 2.0); - glEnd(); - glBindTexture(GL_TEXTURE_3D, 0); - glDisable(GL_TEXTURE_3D); - glBindFramebuffer(GL_FRAMEBUFFER,0); - glBindTexture(GL_TEXTURE_2D, 0); - } - - - void - generateVectorField(stim::vec d, float dim) - { - vec d_s = d.cart2sph(); - vec temp; - float Dim = (float) dim; - float y_0 = 0.0; - float x_0 = 0.0; - float len = 4.0/(2.0*Dim+1.0); - float t0 = M_PI/2; - float p0 = M_PI/3; - float dt = t0/Dim; - float dp = p0/Dim; - for(int i = -dim; i <= dim; i++){ - for(int j = -dim; j <= dim; j++){ - //field[i+dim][j+dim][0] = d[0]; - //field[i+dim][j+dim][1] = d[1]+dt*i; - //field[i+dim][j+dim][2] = d[2]+dp*j; - temp[0] = 1; - temp[1] = d_s[1]+dt*i; - temp[2] = d_s[2]+dp*j; - temp = temp.sph2cart(); - Update(x_0+2.0*(i+dim)*len, y_0+(j+dim)*len, temp); - } - } - - } - - - - - void - initCuda() - { - stim::cudaSetDevice(); - GLint max; - glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); - std::cout << max << std::endl; - } - - void - createResource() - { - HANDLE_ERROR( - cudaGraphicsGLRegisterImage( - &resource, - texbufferID, - GL_TEXTURE_2D, - //CU_GRAPHICS_REGISTER_FLAGS_NONE) - cudaGraphicsMapFlagsReadOnly) - ); - } - - void - destroyResource() - { - HANDLE_ERROR( - cudaGraphicsUnregisterResource(resource) - ); - } - - - int - getCost() - { - createResource(); - int cost = get_cost(resource); - destroyResource(); - return cost; - } -}; -} -#endif diff --git a/gl/gl_texture.h b/gl/gl_texture.h deleted file mode 100644 index d99164c..0000000 --- a/gl/gl_texture.h +++ /dev/null @@ -1,219 +0,0 @@ -#ifndef STIM_GL_TEXTURE_H -#define STIM_GL_TEXTURE_H - - - - -/* -includes not necessary (yet) - -#include -#include - - -*/ - -#include -#include -#include -#include "../grids/image_stack.h" -#include -//#include -#include "./error.h" -namespace stim{ - -/* -class gl_texture - Uses image_stack class in order to create a texture object. -*/ - -template -class gl_texture : public virtual image_stack -{ - private: - /* - Method: setTextureType - Inputs: - Outputs: - Sets the internal texture_type, based on the data - size. Either 3D, 2D, 1D textures. - */ - - void - setTextureType() - { - if (R[3] > 1) - texture_type = GL_TEXTURE_3D; - else if (R[3] == 1 && R[2] == 0) - texture_type = GL_TEXTURE_1D; - else if (R[3] == 1) - texture_type = GL_TEXTURE_2D; - } - protected: - std::string path; - GLuint texID; //OpenGL object - GLenum texture_type; //1D, 2D, 3D - using image_stack::R; - using image_stack::ptr; - using image_stack::samples; - - public: - - /* - Method: Basic Constructor - Inputs: - Outputs: - Creates an instance of the gl_texture object. - */ - gl_texture() - { - - } - - /* - Method: Path Constructor - Inputs: string file_path - Outputs: - Creates an instance of the gl_texture object with a path to the data. - */ - - gl_texture(std::string file_path) - { - path = file_path; - image_stack::load_images(path.append("/*.jpg")); - setTextureType(); - } - - /* - Method: setPath - Inputs:string file_Path - Outputs: - sets the protected path variable of an instance of the gl_texture class - to the method input. - */ - - void - setPath(std::string file_path) - { - path = file_path; - image_stack::load_images(path.append("/*.jpg")); - setTextureType(); - } - - /* - Method: getPath - Inputs: - Outputs: string path - Returns the path associated with an instance of the gl_texture class. - */ - - std::string - getPath() - { - return path; - } - - /* - Method: getTexture - Inputs: - Outputs: GLuint texID - Returns the id of the texture create by/associated with the - instance of the gl_texture class. - */ - - GLuint - getTexture() - { - return texID; - } - - /* - Method: createTexture - Inputs: - Outputs: - Creates a texture and from the data located at and - assigns that texture to texID - */ - //TO DO :::: 1D textures - //TO DO:::add methods for handling the cases of T - // and convert them to GL equivalent. - // i.e. an overloaded function that handles paramenter conversion. - void - createTexture() - { - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - glGenTextures(1, &texID); - glBindTexture(texture_type, texID); - glTexParameteri(texture_type, - GL_TEXTURE_MIN_FILTER, - GL_LINEAR); - glTexParameteri(texture_type, - GL_TEXTURE_MAG_FILTER, - GL_LINEAR); - switch(texture_type) - { - case GL_TEXTURE_3D: - glTexParameteri(texture_type, - GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(texture_type, - GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(texture_type, - GL_TEXTURE_WRAP_R, GL_REPEAT); - glTexImage3D(texture_type, - 0, - // GL_RGB16, - 1, - R[1], - R[2], - R[3], - 0, - GL_LUMINANCE, - GL_UNSIGNED_BYTE, - ptr); - //GL_UNSIGNED_BYTE can be TYPES, convert to GL equivalents - glPixelStorei(GL_PACK_ALIGNMENT,1); - break; - case GL_TEXTURE_2D: - glTexParameteri(texture_type, - GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(texture_type, - GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexImage2D(texture_type, - 0, - 1, - R[1], - R[2], - 0, - GL_LUMINANCE, - GL_UNSIGNED_BYTE, - ptr); - break; - } - } - /* - Temporary methods for debugging and testing are below. - Self-explanatory. - */ - stim::vec - getDims() - { - return R; - } - - T* - getData() - { - return ptr; - } - - - - }; -} - - - - - - -#endif diff --git a/gl/rtsSourceCode.h b/gl/rtsSourceCode.h deleted file mode 100644 index 0ff6c25..0000000 --- a/gl/rtsSourceCode.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef RTSSOURCECODE_H -#define RTSSOURCECODE_H - -#include -#include -#include -#include - -using namespace std; - -///This class defines generic source code that can be loaded from text files. It is primarily used by the rts_glShaderProgram class for GLSL programming. - -class rtsSourceCode -{ -public: - vector source; //the actual source code - void clear() /// -//#include "windows.h" -#include -#include "rtsSourceCode.h" - -class rts_glShaderObject -{ -private: - void init() - { - id = 0; - compiled = false; - type = GL_FRAGMENT_SHADER; - } -public: - bool compiled; - GLenum type; - rtsSourceCode source; - GLuint id; - string log; - - rts_glShaderObject(GLenum type, const char* filename) - { - init(); //initialize the shader - SetType(type); //set the shader type - LoadSource(filename); //load the source code - } - rts_glShaderObject(GLenum type, rtsSourceCode sourceCode) - { - init(); //initialize the shader - SetType(type); //set the shader type - source = sourceCode; - } - rts_glShaderObject() - { - init(); - } - rts_glShaderObject(GLenum type) - { - init(); - SetType(type); - } - void LoadSource(const char* filename) - { - source = rtsSourceCode(filename); //get the shader source code - - } - void SetType(GLenum type) - { - if(id != 0) //if a shader currently exists, delete it - { - glDeleteShader(id); - id = 0; - } - type = type; - id = glCreateShader(type); //create a shader object - if(id == 0) //if a shader was not created, log an error - { - log = "Error getting shader ID from OpenGL"; - return; - } - } - void UploadSource() - { - //create the structure for the shader source code - GLsizei count = source.source.size(); - GLchar** code_string = new GLchar*[count]; - GLint* length = new GLint[count]; - for(int l = 0; l - -using namespace std; - -class rts_glShaderProgram -{ -private: - void get_uniforms() - { - GLint num_uniforms; - glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &num_uniforms); //get the number of uniform variables - GLint max_name_length; - glGetProgramiv(id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_name_length); //get the maximum uniform name length - GLchar* name_buffer = new GLchar[max_name_length]; //create a buffer to store the name - GLsizei length; //I'm not using these yet - GLint size; - GLenum type; //variable's data type - GLint location; //GPU location of the variable - for(int i=0; i shader_list; //list of opengl shaders - vector uniform_list; //list of active uniform variables - vector texture_list; //list of texture maps - - rts_glShaderProgram() - { - linked = false; - id = 0; - } - void AttachShader(rts_glShaderObject shader) - { - if(id == 0) - { - Init(); - } - if(shader.id == 0) //if the shader is invalid - { - log = "Shader is invalid"; - return; - } - - //attach the shader to the program - glAttachShader(id, shader.id); //attach the shader to the program in OpenGL - CHECK_OPENGL_ERROR - shader_list.push_back(shader); //push the shader onto our list for later access - } - //type = GL_FRAGMENT_SHADER or GL_VERTEX_SHADER - void AttachShader(GLenum type, const char* filename) - { - rts_glShaderObject shader(type, filename); - AttachShader(shader); - } - void AttachShader(GLenum type, rtsSourceCode source) - { - rts_glShaderObject shader(type, source); - AttachShader(shader); - } - void PrintLog() - { - cout<::iterator iter; - for(iter = shader_list.begin(); iter != shader_list.end(); iter++) - { - (*iter).Compile(); - //(*iter).PrintLog(); - } - } - void Link() - { - glLinkProgram(id); //link the current shader program - GLint link_status; //test to see if the link went alright - glGetProgramiv(id, GL_LINK_STATUS, &link_status); - if(link_status != GL_TRUE) - { - linked = false; - } - else - linked = true; - - GLsizei length; - GLchar buffer[1000]; - glGetProgramInfoLog(id, 1000, &length, buffer); - log = buffer; - - get_uniforms(); //create the list of active uniform variables - } - void BeginProgram() - { - CHECK_OPENGL_ERROR - if(id == 0) //if the program is invalid, return - { - log = "Invalid program, cannot use."; - return; - } - if(!linked) - { - cout<<"Shader Program used without being linked."< 0) - glActiveTexture(GL_TEXTURE0); - CHECK_OPENGL_ERROR - - //return to OpenGL default shading - glUseProgram(0); - CHECK_OPENGL_ERROR - } - void PrintUniforms() - { - cout<<"Shader Uniforms: "< -#include - -using namespace std; - -enum rtsUniformEnum {RTS_FLOAT, RTS_INT, RTS_BOOL, RTS_FLOAT_MATRIX}; - -///This class stores a single uniform variable for GLSL and is designed to be used by the rts_glShaderProgram class. -struct rts_glShaderUniform -{ -public: - string name; //the name of the variable - GLint location; //the location in the program - void* p_value; //pointer to the global data representing the value in main memory - GLenum type; //variable type (float, int, vec2, etc.) - //rtsUniformEnum rts_type; //type of variable in rts format - //unsigned int num; //the number of values required by the variable (1 for float, 2 for vec2, etc.) - string log; - - //void convert_type(GLenum gl_type); //converts the OpenGL data type to something useful for rts - void submit_to_gpu() - { - if(location < 0) - return; - if(p_value == NULL) - { - cout<<"Error in uniform address: "< -#include "rts/math/vector.h" -#include "rts/gl/error.h" -#include - -namespace stim{ - -///This class stores an OpenGL texture map and is used by rts_glShaderProgram. -class glTexture -{ -private: - void get_type() //guesses the texture type based on the size - { - if(size[1] == 0) - texture_type = GL_TEXTURE_1D; - else if(size[2] == 0) - texture_type = GL_TEXTURE_2D; - else - texture_type = GL_TEXTURE_3D; - } - void set_wrapping() //set the texture wrapping based on the dimensions - { - CHECK_OPENGL_ERROR - switch(texture_type) - { - case GL_TEXTURE_3D: - glTexParameteri(texture_type, GL_TEXTURE_WRAP_R_EXT, GL_REPEAT); - case GL_TEXTURE_2D: - glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); - case GL_TEXTURE_1D: - glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT); - break; - case GL_TEXTURE_RECTANGLE_ARB: - glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - break; - - default: - break; - } - CHECK_OPENGL_ERROR - } - //void set_bits(GLvoid* bits); -public: - vector size; //vector representing the size of the texture - GLuint name; //texture name assigned by OpenGL - GLenum texture_type; //1D, 2D, 3D - GLint internal_format; //number of components (ex. 4 for RGBA) - GLenum pixel_format; //type of data (RGBA, LUMINANCE) - GLenum data_type; //data type of the bits (float, int, etc.) - - //constructor - glTexture() - { - name = 0; - } - glTexture(GLvoid *bits, - GLenum type = GL_TEXTURE_2D, - GLsizei width = 256, - GLsizei height = 256, - GLsizei depth = 0, - GLint internalformat = 1, - GLenum format = GL_LUMINANCE, - GLenum datatype = GL_UNSIGNED_BYTE, - GLint interpolation = GL_LINEAR) - { - init(bits, type, width, height, depth, internalformat, format, datatype, interpolation); - } - - void begin() - { - glEnable(texture_type); - CHECK_OPENGL_ERROR - glBindTexture(texture_type, name); - CHECK_OPENGL_ERROR - } - void end() - { - glDisable(texture_type); - CHECK_OPENGL_ERROR - } - - ///Creates an OpenGL texture map. This function requires basic information about the texture map as well as a pointer to the bit data describing the texture. - void init(GLvoid *bits, - GLenum type = GL_TEXTURE_2D, - GLsizei width = 256, - GLsizei height = 256, - GLsizei depth = 0, - GLint internalformat = 1, - GLenum format = GL_LUMINANCE, - GLenum datatype = GL_UNSIGNED_BYTE, - GLint interpolation = GL_LINEAR) - { - CHECK_OPENGL_ERROR - if(name != 0) - glDeleteTextures(1, &name); - - - CHECK_OPENGL_ERROR - if(datatype == GL_FLOAT) - { - glPixelStorei(GL_PACK_ALIGNMENT, 4); - glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //I honestly don't know what this does but it fixes problems - } - else if(datatype == GL_UNSIGNED_BYTE) - { - //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - //glPixelStorei(GL_PACK_ALIGNMENT, 1); - } - else if(datatype == GL_UNSIGNED_SHORT) - { - //glPixelStorei(GL_UNPACK_ALIGNMENT, 2); - //glPixelStorei(GL_PACK_ALIGNMENT, 2); - } - CHECK_OPENGL_ERROR - glGenTextures(1, &name); //get the texture name from OpenGL - //cout<<"OpenGL Name: "<(width, height, depth); //assign the texture size - //get_type(); //guess the type based on the size - texture_type = type; //set the type of texture - glEnable(texture_type); //enable the texture map - CHECK_OPENGL_ERROR - glBindTexture(texture_type, name); //bind the texture for editing - CHECK_OPENGL_ERROR - set_wrapping(); //set the texture wrapping parameters - CHECK_OPENGL_ERROR - glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, interpolation); //set filtering - CHECK_OPENGL_ERROR - glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, interpolation); - CHECK_OPENGL_ERROR - internal_format = internalformat; //set the number of components per pixel - pixel_format = format; //set the pixel format - data_type = datatype; //set the data type - SetBits(bits); //send the bits to the OpenGL driver - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //replace the specified vertex color - CHECK_OPENGL_ERROR - glDisable(texture_type); - - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - } - void Clean() - { - if(name != 0) - { - glDeleteTextures(1, &name); - CHECK_OPENGL_ERROR - name = 0; - } - } - void SetBits(GLvoid *bits) - { - glEnable(texture_type); //enable the texture map - CHECK_OPENGL_ERROR - glBindTexture(texture_type, name); - CHECK_OPENGL_ERROR - - switch(texture_type) - { - case GL_TEXTURE_3D: - glTexImage3D(texture_type, 0, internal_format, size[0], size[1], size[2], 0, pixel_format, data_type, bits); - CHECK_OPENGL_ERROR - break; - case GL_TEXTURE_2D: - case GL_TEXTURE_RECTANGLE_ARB: - glTexImage2D(texture_type, 0, internal_format, size[0], size[1], 0, pixel_format, data_type, bits); - CHECK_OPENGL_ERROR - break; - case GL_TEXTURE_1D: - glTexImage1D(texture_type, 0, internal_format, size[0], 0, pixel_format, data_type, bits); - CHECK_OPENGL_ERROR - break; - default: - //glTexImage2D(texture_type, 0, internal_format, size.x, size.y, 0, pixel_format, data_type, bits); - break; - } - CHECK_OPENGL_ERROR - } - void ResetBits(GLvoid *bits) - { - glEnable(texture_type); //enable the texture map - CHECK_OPENGL_ERROR - glBindTexture(texture_type, name); - CHECK_OPENGL_ERROR - - switch(texture_type) - { - case GL_TEXTURE_3D: - //glTexImage3D(texture_type, 0, internal_format, size.x, size.y, size.z, 0, pixel_format, data_type, bits); - break; - case GL_TEXTURE_2D: - case GL_TEXTURE_RECTANGLE_ARB: - glTexSubImage2D(texture_type, 0, 0, 0, size[0], size[1], pixel_format, data_type, bits); - CHECK_OPENGL_ERROR - break; - case GL_TEXTURE_1D: - //glTexImage1D(texture_type, 0, internal_format, size.x, 0, pixel_format, data_type, bits); - break; - default: - //glTexImage2D(texture_type, 0, internal_format, size.x, size.y, 0, pixel_format, data_type, bits); - break; - } - glDisable(texture_type); - CHECK_OPENGL_ERROR - } - void* GetBits(GLenum format, GLenum type) - { - //returns the texture data - - int components; - switch(format) - { - case GL_RED: - case GL_GREEN: - case GL_BLUE: - case GL_ALPHA: - case GL_LUMINANCE: - components = 1; - break; - case GL_LUMINANCE_ALPHA: - components = 2; - break; - case GL_RGB: - case GL_BGR: - components = 3; - break; - case GL_RGBA: - case GL_BGRA: - components = 4; - break; - } - - int type_size; - switch(type) - { - case GL_UNSIGNED_BYTE: - case GL_BYTE: - type_size = sizeof(char); - break; - case GL_UNSIGNED_SHORT: - case GL_SHORT: - type_size = sizeof(short); - break; - case GL_UNSIGNED_INT: - case GL_INT: - type_size = sizeof(int); - break; - case GL_FLOAT: - type_size = sizeof(float); - break; - } - - //allocate memory for the texture - void* result = malloc(components*type_size * size[0] * size[1]); - - begin(); - glGetTexImage(texture_type, 0, format, type, result); - - CHECK_OPENGL_ERROR - end(); - - - return result; - - } -}; - -} - -#define RTS_UNKNOWN 0 - -#endif diff --git a/grids/grid_data.h b/grids/grid_data.h deleted file mode 100644 index 82eef7c..0000000 --- a/grids/grid_data.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef STIM_GRID_DATA_CUH -#define STIM_GRID_DATA_CUH - -#include -#include -#include -#include - -#include "../cuda/threads.h" -#include "../cuda/error.h" -#include "../cuda/devices.h" -#include "../math/vector.h" - - -namespace stim{ - -//This object describes a generic D-dimensional grid containing data of type T - // data can be loaded in the form of images - // data can be saved in the form of binary files -template -class grid_data{ - -protected: - - stim::vec R; //elements in each dimension - T* ptr; //pointer to the data (on the GPU or CPU) - - //return the total number of values in the binary file - unsigned long samples(){ - - unsigned long s = 1; - for(unsigned int d = 0; d < D; d++) - s *= R[d]; - - return s; - - } - -public: - - //write data to disk - void write(std::string filename){ - - std::fstream file; - - //open the file as binary for reading - file.open(filename.c_str(), std::ios::out | std::ios::binary); - - //write file to disk - file.write((char *)ptr, samples() * sizeof(T)); - } - - //load a binary file from disk - // header size is in bytes - void read(std::string filename, stim::vec S, unsigned long header = 0){ - - R = S; //set the sample resolution - - std::fstream file; - - //open the file as binary for writing - file.open(filename.c_str(), std::ios::in | std::ios::binary); - - //seek past the header - file.seekg(header, std::ios::beg); - - //read the data - file.read((char *)ptr, samples() * sizeof(T)); - } - - - -}; - -} - - -#endif diff --git a/grids/image_stack.h b/grids/image_stack.h deleted file mode 100644 index de02005..0000000 --- a/grids/image_stack.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef STIM_IMAGE_STACK_H -#define STIM_IMAGE_STACK_H - -#include "../parser/wildcards.h" -#include "../parser/filename.h" -#include "../grids/grid_data.h" -#include "../image/image.h" - -namespace stim{ - -//this creates a class that can be used to load 3D grid data from stacks of images -// The class uses a 4D grid_data object, where the first dimension is color -template -class image_stack : public virtual grid_data{ - - enum image_type {stimAuto, stimMono, stimRGB, stimRGBA}; - -protected: - using grid_data::R; - using grid_data::ptr; - using grid_data::samples; - -public: - - void load_images(std::string file_mask){ - - stim::filename file_path(file_mask); - - //if the file path is relative, update it with the current working directory - if(file_path.is_relative()){ - stim::filename wd = stim::filename::cwd(); - file_path = wd.get_relative(file_mask); - } - - //get the list of files - std::vector file_list = file_path.get_list(); - - //if there are no matching files, exit - if(file_list.size() == 0){ - std::cout<<"STIM ERROR (image_stack): No matching files for loading a stack."< I(file_list[0].str()); - - //set the image resolution and number of channels - R[0] = I.channels(); - R[1] = I.width(); - R[2] = I.height(); - R[3] = file_list.size(); - - //allocate storage space - ptr = (T*)malloc(sizeof(T) * samples()); - - //load and copy each image into the grid - for(unsigned int i = 0; i I(file_list[i].str()); - - //retrieve the interlaced data from the image - store it in the grid - I.data_interleaved(&ptr[ i * R[0] * R[1] * R[2] ]); - } - } - - void save_image(std::string file_name, unsigned int i){ - - //create an image - stim::image I; - - //retrieve the interlaced data from the image - store it in the grid - I.set_interleaved(&ptr[ i * R[0] * R[1] * R[2] ], R[1], R[2], R[0]); - - I.save(file_name); - } - - void save_images(std::string file_mask){ - - stim::filename file_path(file_mask); - - //if the file path is relative, update it with the current working directory - if(file_path.is_relative()){ - stim::filename wd = stim::filename::cwd(); - file_path = wd.get_relative(file_mask); - } - - //create a list of file names - std::vector file_list = stim::wildcards::increment(file_path.str(), 0, R[3]-1, 1); - - for(int i=0; i -#include -#include -#include -#include -#include -#include - -// Detect/configure OS variables. -// -// Define 'cimg_OS' to: '0' for an unknown OS (will try to minize library dependencies). -// '1' for a Unix-like OS (Linux, Solaris, BSD, MacOSX, Irix, ...). -// '2' for Microsoft Windows. -// (auto-detection is performed if 'cimg_OS' is not set by the user). -#ifndef cimg_OS -#if defined(unix) || defined(__unix) || defined(__unix__) \ - || defined(linux) || defined(__linux) || defined(__linux__) \ - || defined(sun) || defined(__sun) \ - || defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) \ - || defined(__FreeBSD__) || defined (__DragonFly__) \ - || defined(sgi) || defined(__sgi) \ - || defined(__MACOSX__) || defined(__APPLE__) \ - || defined(__CYGWIN__) -#define cimg_OS 1 -#elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \ - || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) -#define cimg_OS 2 -#else -#define cimg_OS 0 -#endif -#elif !(cimg_OS==0 || cimg_OS==1 || cimg_OS==2) -#error CImg Library: Invalid configuration variable 'cimg_OS'. -#error (correct values are '0 = unknown OS', '1 = Unix-like OS', '2 = Microsoft Windows'). -#endif - -// Disable silly warnings on some Microsoft VC++ compilers. -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable:4311) -#pragma warning(disable:4312) -#pragma warning(disable:4800) -#pragma warning(disable:4804) -#pragma warning(disable:4996) -#define _CRT_SECURE_NO_DEPRECATE 1 -#define _CRT_NONSTDC_NO_DEPRECATE 1 -#endif - -// Include OS-specific headers. -#if cimg_OS==1 -#include -#include -#include -#include -#elif cimg_OS==2 -#ifndef NOMINMAX -#define NOMINMAX -#endif -#include -#ifndef _WIN32_IE -#define _WIN32_IE 0x0400 -#endif -#include -#include -#include -#define cimg_snprintf _snprintf -#define cimg_vsnprintf _vsnprintf -#endif -#ifndef cimg_snprintf -#include -#define cimg_snprintf snprintf -#define cimg_vsnprintf vsnprintf -#endif - -// Look for C++11 features -#if !defined(cimg_use_cpp11) && __cplusplus>201100 -#define cimg_use_cpp11 -#endif -#ifdef cimg_use_cpp11 -#include -#include -#endif - -// Configure filename separator. -// -// Filename separator is set by default to '/', except for Windows where it is '\'. -#ifndef cimg_file_separator -#if cimg_OS==2 -#define cimg_file_separator '\\' -#else -#define cimg_file_separator '/' -#endif -#endif - -// Configure verbosity of output messages. -// -// Define 'cimg_verbosity' to: '0' to hide library messages (quiet mode). -// '1' to output library messages on the console. -// '2' to output library messages on a basic dialog window (default behavior). -// '3' to do as '1' + add extra warnings (may slow down the code!). -// '4' to do as '2' + add extra warnings (may slow down the code!). -// -// Define 'cimg_strict_warnings' to replace warning messages by exception throwns. -// -// Define 'cimg_use_vt100' to allow output of color messages on VT100-compatible terminals. -#ifndef cimg_verbosity -#define cimg_verbosity 2 -#elif !(cimg_verbosity==0 || cimg_verbosity==1 || cimg_verbosity==2 || cimg_verbosity==3 || cimg_verbosity==4) -#error CImg Library: Configuration variable 'cimg_verbosity' is badly defined. -#error (should be { 0=quiet | 1=console | 2=dialog | 3=console+warnings | 4=dialog+warnings }). -#endif - -// Configure display framework. -// -// Define 'cimg_display' to: '0' to disable display capabilities. -// '1' to use the X-Window framework (X11). -// '2' to use the Microsoft GDI32 framework. -#ifndef cimg_display -#if cimg_OS==0 -#define cimg_display 0 -#elif cimg_OS==1 -#if defined(__MACOSX__) || defined(__APPLE__) -#define cimg_display 1 -#else -#define cimg_display 1 -#endif -#elif cimg_OS==2 -#define cimg_display 2 -#endif -#elif !(cimg_display==0 || cimg_display==1 || cimg_display==2) -#error CImg Library: Configuration variable 'cimg_display' is badly defined. -#error (should be { 0=none | 1=X-Window (X11) | 2=Microsoft GDI32 }). -#endif - -// Include display-specific headers. -#if cimg_display==1 -#include -#include -#include -#include -#ifdef cimg_use_xshm -#include -#include -#include -#endif -#ifdef cimg_use_xrandr -#include -#endif -#endif -#ifndef cimg_appname -#define cimg_appname "CImg" -#endif - -// Configure OpenMP support. -// (http://www.openmp.org) -// -// Define 'cimg_use_openmp' to enable OpenMP support. -// -// OpenMP directives may be used in a (very) few CImg functions to get -// advantages of multi-core CPUs. -#ifdef cimg_use_openmp -#include -#endif - -// Configure OpenCV support. -// (http://opencv.willowgarage.com/wiki/) -// -// Define 'cimg_use_opencv' to enable OpenCV support. -// -// OpenCV library may be used to access images from cameras -// (see method 'CImg::load_camera()'). -#ifdef cimg_use_opencv -#ifdef True -#undef True -#define _cimg_redefine_True -#endif -#ifdef False -#undef False -#define _cimg_redefine_False -#endif -#include -#include "cv.h" -#include "highgui.h" -#endif - -// Configure LibPNG support. -// (http://www.libpng.org) -// -// Define 'cimg_use_png' to enable LibPNG support. -// -// PNG library may be used to get a native support of '.png' files. -// (see methods 'CImg::{load,save}_png()'. -#ifdef cimg_use_png -extern "C" { -#include "png.h" -} -#endif - -// Configure LibJPEG support. -// (http://en.wikipedia.org/wiki/Libjpeg) -// -// Define 'cimg_use_jpeg' to enable LibJPEG support. -// -// JPEG library may be used to get a native support of '.jpg' files. -// (see methods 'CImg::{load,save}_jpeg()'). -#ifdef cimg_use_jpeg -extern "C" { -#include "jpeglib.h" -#include "setjmp.h" -} -#endif - -// Configure LibTIFF support. -// (http://www.libtiff.org) -// -// Define 'cimg_use_tiff' to enable LibTIFF support. -// -// TIFF library may be used to get a native support of '.tif' files. -// (see methods 'CImg[List]::{load,save}_tiff()'). -#ifdef cimg_use_tiff -extern "C" { -#define uint64 uint64_hack_ -#define int64 int64_hack_ -#include "tiffio.h" -#undef uint64 -#undef int64 -} -#endif - -// Configure LibMINC2 support. -// (http://en.wikibooks.org/wiki/MINC/Reference/MINC2.0_File_Format_Reference) -// -// Define 'cimg_use_minc2' to enable LibMINC2 support. -// -// MINC2 library may be used to get a native support of '.mnc' files. -// (see methods 'CImg::{load,save}_minc2()'). -#ifdef cimg_use_minc2 -#include "minc_io_simple_volume.h" -#include "minc_1_simple.h" -#include "minc_1_simple_rw.h" -#endif - -// Configure FFMPEG support. -// (http://www.ffmpeg.org) -// -// Define 'cimg_use_ffmpeg' to enable FFMPEG lib support. -// -// Avcodec and Avformat libraries from FFMPEG may be used -// to get a native support of various video file formats. -// (see methods 'CImg[List]::load_ffmpeg()'). -#ifdef cimg_use_ffmpeg -#if (defined(_STDINT_H) || defined(_STDINT_H_)) && !defined(UINT64_C) -#warning "__STDC_CONSTANT_MACROS has to be defined before including , this file will probably not compile." -#endif -#ifndef __STDC_CONSTANT_MACROS -#define __STDC_CONSTANT_MACROS // ...or stdint.h wont' define UINT64_C, needed by libavutil -#endif -extern "C" { -#include -#include -#include -} -#endif - -// Configure Zlib support. -// (http://www.zlib.net) -// -// Define 'cimg_use_zlib' to enable Zlib support. -// -// Zlib library may be used to allow compressed data in '.cimgz' files -// (see methods 'CImg[List]::{load,save}_cimg()'). -#ifdef cimg_use_zlib -extern "C" { -#include "zlib.h" -} -#endif - -// Configure Magick++ support. -// (http://www.imagemagick.org/Magick++) -// -// Define 'cimg_use_magick' to enable Magick++ support. -// -// Magick++ library may be used to get a native support of various image file formats. -// (see methods 'CImg::{load,save}()'). -#ifdef cimg_use_magick -#include "Magick++.h" -#endif - -// Configure FFTW3 support. -// (http://www.fftw.org) -// -// Define 'cimg_use_fftw3' to enable libFFTW3 support. -// -// FFTW3 library may be used to efficiently compute the Fast Fourier Transform -// of image data, without restriction on the image size. -// (see method 'CImg[List]::FFT()'). -#ifdef cimg_use_fftw3 -extern "C" { -#include "fftw3.h" -} -#endif - -// Configure LibBoard support. -// (http://libboard.sourceforge.net/) -// -// Define 'cimg_use_board' to enable Board support. -// -// Board library may be used to draw 3d objects in vector-graphics canvas -// that can be saved as '.ps' or '.svg' files afterwards. -// (see method 'CImg::draw_object3d()'). -#ifdef cimg_use_board -#ifdef None -#undef None -#define _cimg_redefine_None -#endif -#include "Board.h" -#endif - -// Configure OpenEXR support. -// (http://www.openexr.com/) -// -// Define 'cimg_use_openexr' to enable OpenEXR support. -// -// OpenEXR library may be used to get a native support of '.exr' files. -// (see methods 'CImg::{load,save}_exr()'). -#ifdef cimg_use_openexr -#include "ImfRgbaFile.h" -#include "ImfInputFile.h" -#include "ImfChannelList.h" -#include "ImfMatrixAttribute.h" -#include "ImfArray.h" -#endif - -// Lapack configuration. -// (http://www.netlib.org/lapack) -// -// Define 'cimg_use_lapack' to enable LAPACK support. -// -// Lapack library may be used in several CImg methods to speed up -// matrix computations (eigenvalues, inverse, ...). -#ifdef cimg_use_lapack -extern "C" { - extern void sgetrf_(int*, int*, float*, int*, int*, int*); - extern void sgetri_(int*, float*, int*, int*, float*, int*, int*); - extern void sgetrs_(char*, int*, int*, float*, int*, int*, float*, int*, int*); - extern void sgesvd_(char*, char*, int*, int*, float*, int*, float*, float*, int*, float*, int*, float*, int*, int*); - extern void ssyev_(char*, char*, int*, float*, int*, float*, float*, int*, int*); - extern void dgetrf_(int*, int*, double*, int*, int*, int*); - extern void dgetri_(int*, double*, int*, int*, double*, int*, int*); - extern void dgetrs_(char*, int*, int*, double*, int*, int*, double*, int*, int*); - extern void dgesvd_(char*, char*, int*, int*, double*, int*, double*, double*, - int*, double*, int*, double*, int*, int*); - extern void dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*); - extern void dgels_(char*, int*,int*,int*,double*,int*,double*,int*,double*,int*,int*); - extern void sgels_(char*, int*,int*,int*,float*,int*,float*,int*,float*,int*,int*); -} -#endif - -// Check if min/max/PI macros are defined. -// -// CImg does not compile if macros 'min', 'max' or 'PI' are defined, -// because it redefines functions min(), max() and const variable PI in the cimg:: namespace. -// so it '#undef' these macros if necessary, and restore them to reasonable -// values at the end of this file. -#ifdef min -#undef min -#define _cimg_redefine_min -#endif -#ifdef max -#undef max -#define _cimg_redefine_max -#endif -#ifdef PI -#undef PI -#define _cimg_redefine_PI -#endif - -// Define 'cimg_library' namespace suffix. -// -// You may want to add a suffix to the 'cimg_library' namespace, for instance if you need to work -// with several versions of the library at the same time. -#ifdef cimg_namespace_suffix -#define __cimg_library_suffixed(s) cimg_library_##s -#define _cimg_library_suffixed(s) __cimg_library_suffixed(s) -#define cimg_library_suffixed _cimg_library_suffixed(cimg_namespace_suffix) -#else -#define cimg_library_suffixed cimg_library -#endif - -/*------------------------------------------------------------------------------ - # - # Define user-friendly macros. - # - # These CImg macros are prefixed by 'cimg_' and can be used safely in your own - # code. They are useful to parse command line options, or to write image loops. - # - ------------------------------------------------------------------------------*/ - -// Macros to define program usage, and retrieve command line arguments. -#define cimg_usage(usage) cimg_library_suffixed::cimg::option((char*)0,argc,argv,(char*)0,usage,false) -#define cimg_help(str) cimg_library_suffixed::cimg::option((char*)0,argc,argv,str,(char*)0) -#define cimg_option(name,defaut,usage) cimg_library_suffixed::cimg::option(name,argc,argv,defaut,usage) -#define cimg_argument(pos) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv) -#define cimg_argument1(pos,s0) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,1,s0) -#define cimg_argument2(pos,s0,s1) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,2,s0,s1) -#define cimg_argument3(pos,s0,s1,s2) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,3,s0,s1,s2) -#define cimg_argument4(pos,s0,s1,s2,s3) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,4,s0,s1,s2,s3) -#define cimg_argument5(pos,s0,s1,s2,s3,s4) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,5,s0,s1,s2,s3,s4) -#define cimg_argument6(pos,s0,s1,s2,s3,s4,s5) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,6,s0,s1,s2,s3,s4,s5) -#define cimg_argument7(pos,s0,s1,s2,s3,s4,s5,s6) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,7,s0,s1,s2,s3,s4,s5,s6) -#define cimg_argument8(pos,s0,s1,s2,s3,s4,s5,s6,s7) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,8,s0,s1,s2,s3,s4,s5,s6,s7) -#define cimg_argument9(pos,s0,s1,s2,s3,s4,s5,s6,s7,s8) \ - cimg_library_suffixed::cimg::argument(pos,argc,argv,9,s0,s1,s2,s3,s4,s5,s6,s7,s8) - -// Macros to define and manipulate local neighborhoods. -#define CImg_2x2(I,T) T I[4]; \ - T& I##cc = I[0]; T& I##nc = I[1]; \ - T& I##cn = I[2]; T& I##nn = I[3]; \ - I##cc = I##nc = \ - I##cn = I##nn = 0 - -#define CImg_3x3(I,T) T I[9]; \ - T& I##pp = I[0]; T& I##cp = I[1]; T& I##np = I[2]; \ - T& I##pc = I[3]; T& I##cc = I[4]; T& I##nc = I[5]; \ - T& I##pn = I[6]; T& I##cn = I[7]; T& I##nn = I[8]; \ - I##pp = I##cp = I##np = \ - I##pc = I##cc = I##nc = \ - I##pn = I##cn = I##nn = 0 - -#define CImg_4x4(I,T) T I[16]; \ - T& I##pp = I[0]; T& I##cp = I[1]; T& I##np = I[2]; T& I##ap = I[3]; \ - T& I##pc = I[4]; T& I##cc = I[5]; T& I##nc = I[6]; T& I##ac = I[7]; \ - T& I##pn = I[8]; T& I##cn = I[9]; T& I##nn = I[10]; T& I##an = I[11]; \ - T& I##pa = I[12]; T& I##ca = I[13]; T& I##na = I[14]; T& I##aa = I[15]; \ - I##pp = I##cp = I##np = I##ap = \ - I##pc = I##cc = I##nc = I##ac = \ - I##pn = I##cn = I##nn = I##an = \ - I##pa = I##ca = I##na = I##aa = 0 - -#define CImg_5x5(I,T) T I[25]; \ - T& I##bb = I[0]; T& I##pb = I[1]; T& I##cb = I[2]; T& I##nb = I[3]; T& I##ab = I[4]; \ - T& I##bp = I[5]; T& I##pp = I[6]; T& I##cp = I[7]; T& I##np = I[8]; T& I##ap = I[9]; \ - T& I##bc = I[10]; T& I##pc = I[11]; T& I##cc = I[12]; T& I##nc = I[13]; T& I##ac = I[14]; \ - T& I##bn = I[15]; T& I##pn = I[16]; T& I##cn = I[17]; T& I##nn = I[18]; T& I##an = I[19]; \ - T& I##ba = I[20]; T& I##pa = I[21]; T& I##ca = I[22]; T& I##na = I[23]; T& I##aa = I[24]; \ - I##bb = I##pb = I##cb = I##nb = I##ab = \ - I##bp = I##pp = I##cp = I##np = I##ap = \ - I##bc = I##pc = I##cc = I##nc = I##ac = \ - I##bn = I##pn = I##cn = I##nn = I##an = \ - I##ba = I##pa = I##ca = I##na = I##aa = 0 - -#define CImg_2x2x2(I,T) T I[8]; \ - T& I##ccc = I[0]; T& I##ncc = I[1]; \ - T& I##cnc = I[2]; T& I##nnc = I[3]; \ - T& I##ccn = I[4]; T& I##ncn = I[5]; \ - T& I##cnn = I[6]; T& I##nnn = I[7]; \ - I##ccc = I##ncc = \ - I##cnc = I##nnc = \ - I##ccn = I##ncn = \ - I##cnn = I##nnn = 0 - -#define CImg_3x3x3(I,T) T I[27]; \ - T& I##ppp = I[0]; T& I##cpp = I[1]; T& I##npp = I[2]; \ - T& I##pcp = I[3]; T& I##ccp = I[4]; T& I##ncp = I[5]; \ - T& I##pnp = I[6]; T& I##cnp = I[7]; T& I##nnp = I[8]; \ - T& I##ppc = I[9]; T& I##cpc = I[10]; T& I##npc = I[11]; \ - T& I##pcc = I[12]; T& I##ccc = I[13]; T& I##ncc = I[14]; \ - T& I##pnc = I[15]; T& I##cnc = I[16]; T& I##nnc = I[17]; \ - T& I##ppn = I[18]; T& I##cpn = I[19]; T& I##npn = I[20]; \ - T& I##pcn = I[21]; T& I##ccn = I[22]; T& I##ncn = I[23]; \ - T& I##pnn = I[24]; T& I##cnn = I[25]; T& I##nnn = I[26]; \ - I##ppp = I##cpp = I##npp = \ - I##pcp = I##ccp = I##ncp = \ - I##pnp = I##cnp = I##nnp = \ - I##ppc = I##cpc = I##npc = \ - I##pcc = I##ccc = I##ncc = \ - I##pnc = I##cnc = I##nnc = \ - I##ppn = I##cpn = I##npn = \ - I##pcn = I##ccn = I##ncn = \ - I##pnn = I##cnn = I##nnn = 0 - -#define cimg_get2x2(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(x,y,z,c), I[1] = (T)(img)(_n1##x,y,z,c), I[2] = (T)(img)(x,_n1##y,z,c), \ - I[3] = (T)(img)(_n1##x,_n1##y,z,c) - -#define cimg_get3x3(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p1##x,_p1##y,z,c), I[1] = (T)(img)(x,_p1##y,z,c), I[2] = (T)(img)(_n1##x,_p1##y,z,c), \ - I[3] = (T)(img)(_p1##x,y,z,c), I[4] = (T)(img)(x,y,z,c), I[5] = (T)(img)(_n1##x,y,z,c), \ - I[6] = (T)(img)(_p1##x,_n1##y,z,c), I[7] = (T)(img)(x,_n1##y,z,c), I[8] = (T)(img)(_n1##x,_n1##y,z,c) - -#define cimg_get4x4(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p1##x,_p1##y,z,c), I[1] = (T)(img)(x,_p1##y,z,c), I[2] = (T)(img)(_n1##x,_p1##y,z,c), \ - I[3] = (T)(img)(_n2##x,_p1##y,z,c), I[4] = (T)(img)(_p1##x,y,z,c), I[5] = (T)(img)(x,y,z,c), \ - I[6] = (T)(img)(_n1##x,y,z,c), I[7] = (T)(img)(_n2##x,y,z,c), I[8] = (T)(img)(_p1##x,_n1##y,z,c), \ - I[9] = (T)(img)(x,_n1##y,z,c), I[10] = (T)(img)(_n1##x,_n1##y,z,c), I[11] = (T)(img)(_n2##x,_n1##y,z,c), \ - I[12] = (T)(img)(_p1##x,_n2##y,z,c), I[13] = (T)(img)(x,_n2##y,z,c), I[14] = (T)(img)(_n1##x,_n2##y,z,c), \ - I[15] = (T)(img)(_n2##x,_n2##y,z,c) - -#define cimg_get5x5(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p2##x,_p2##y,z,c), I[1] = (T)(img)(_p1##x,_p2##y,z,c), I[2] = (T)(img)(x,_p2##y,z,c), \ - I[3] = (T)(img)(_n1##x,_p2##y,z,c), I[4] = (T)(img)(_n2##x,_p2##y,z,c), I[5] = (T)(img)(_p2##x,_p1##y,z,c), \ - I[6] = (T)(img)(_p1##x,_p1##y,z,c), I[7] = (T)(img)(x,_p1##y,z,c), I[8] = (T)(img)(_n1##x,_p1##y,z,c), \ - I[9] = (T)(img)(_n2##x,_p1##y,z,c), I[10] = (T)(img)(_p2##x,y,z,c), I[11] = (T)(img)(_p1##x,y,z,c), \ - I[12] = (T)(img)(x,y,z,c), I[13] = (T)(img)(_n1##x,y,z,c), I[14] = (T)(img)(_n2##x,y,z,c), \ - I[15] = (T)(img)(_p2##x,_n1##y,z,c), I[16] = (T)(img)(_p1##x,_n1##y,z,c), I[17] = (T)(img)(x,_n1##y,z,c), \ - I[18] = (T)(img)(_n1##x,_n1##y,z,c), I[19] = (T)(img)(_n2##x,_n1##y,z,c), I[20] = (T)(img)(_p2##x,_n2##y,z,c), \ - I[21] = (T)(img)(_p1##x,_n2##y,z,c), I[22] = (T)(img)(x,_n2##y,z,c), I[23] = (T)(img)(_n1##x,_n2##y,z,c), \ - I[24] = (T)(img)(_n2##x,_n2##y,z,c) - -#define cimg_get6x6(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p2##x,_p2##y,z,c), I[1] = (T)(img)(_p1##x,_p2##y,z,c), I[2] = (T)(img)(x,_p2##y,z,c), \ - I[3] = (T)(img)(_n1##x,_p2##y,z,c), I[4] = (T)(img)(_n2##x,_p2##y,z,c), I[5] = (T)(img)(_n3##x,_p2##y,z,c), \ - I[6] = (T)(img)(_p2##x,_p1##y,z,c), I[7] = (T)(img)(_p1##x,_p1##y,z,c), I[8] = (T)(img)(x,_p1##y,z,c), \ - I[9] = (T)(img)(_n1##x,_p1##y,z,c), I[10] = (T)(img)(_n2##x,_p1##y,z,c), I[11] = (T)(img)(_n3##x,_p1##y,z,c), \ - I[12] = (T)(img)(_p2##x,y,z,c), I[13] = (T)(img)(_p1##x,y,z,c), I[14] = (T)(img)(x,y,z,c), \ - I[15] = (T)(img)(_n1##x,y,z,c), I[16] = (T)(img)(_n2##x,y,z,c), I[17] = (T)(img)(_n3##x,y,z,c), \ - I[18] = (T)(img)(_p2##x,_n1##y,z,c), I[19] = (T)(img)(_p1##x,_n1##y,z,c), I[20] = (T)(img)(x,_n1##y,z,c), \ - I[21] = (T)(img)(_n1##x,_n1##y,z,c), I[22] = (T)(img)(_n2##x,_n1##y,z,c), I[23] = (T)(img)(_n3##x,_n1##y,z,c), \ - I[24] = (T)(img)(_p2##x,_n2##y,z,c), I[25] = (T)(img)(_p1##x,_n2##y,z,c), I[26] = (T)(img)(x,_n2##y,z,c), \ - I[27] = (T)(img)(_n1##x,_n2##y,z,c), I[28] = (T)(img)(_n2##x,_n2##y,z,c), I[29] = (T)(img)(_n3##x,_n2##y,z,c), \ - I[30] = (T)(img)(_p2##x,_n3##y,z,c), I[31] = (T)(img)(_p1##x,_n3##y,z,c), I[32] = (T)(img)(x,_n3##y,z,c), \ - I[33] = (T)(img)(_n1##x,_n3##y,z,c), I[34] = (T)(img)(_n2##x,_n3##y,z,c), I[35] = (T)(img)(_n3##x,_n3##y,z,c) - -#define cimg_get7x7(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p3##x,_p3##y,z,c), I[1] = (T)(img)(_p2##x,_p3##y,z,c), I[2] = (T)(img)(_p1##x,_p3##y,z,c), \ - I[3] = (T)(img)(x,_p3##y,z,c), I[4] = (T)(img)(_n1##x,_p3##y,z,c), I[5] = (T)(img)(_n2##x,_p3##y,z,c), \ - I[6] = (T)(img)(_n3##x,_p3##y,z,c), I[7] = (T)(img)(_p3##x,_p2##y,z,c), I[8] = (T)(img)(_p2##x,_p2##y,z,c), \ - I[9] = (T)(img)(_p1##x,_p2##y,z,c), I[10] = (T)(img)(x,_p2##y,z,c), I[11] = (T)(img)(_n1##x,_p2##y,z,c), \ - I[12] = (T)(img)(_n2##x,_p2##y,z,c), I[13] = (T)(img)(_n3##x,_p2##y,z,c), I[14] = (T)(img)(_p3##x,_p1##y,z,c), \ - I[15] = (T)(img)(_p2##x,_p1##y,z,c), I[16] = (T)(img)(_p1##x,_p1##y,z,c), I[17] = (T)(img)(x,_p1##y,z,c), \ - I[18] = (T)(img)(_n1##x,_p1##y,z,c), I[19] = (T)(img)(_n2##x,_p1##y,z,c), I[20] = (T)(img)(_n3##x,_p1##y,z,c), \ - I[21] = (T)(img)(_p3##x,y,z,c), I[22] = (T)(img)(_p2##x,y,z,c), I[23] = (T)(img)(_p1##x,y,z,c), \ - I[24] = (T)(img)(x,y,z,c), I[25] = (T)(img)(_n1##x,y,z,c), I[26] = (T)(img)(_n2##x,y,z,c), \ - I[27] = (T)(img)(_n3##x,y,z,c), I[28] = (T)(img)(_p3##x,_n1##y,z,c), I[29] = (T)(img)(_p2##x,_n1##y,z,c), \ - I[30] = (T)(img)(_p1##x,_n1##y,z,c), I[31] = (T)(img)(x,_n1##y,z,c), I[32] = (T)(img)(_n1##x,_n1##y,z,c), \ - I[33] = (T)(img)(_n2##x,_n1##y,z,c), I[34] = (T)(img)(_n3##x,_n1##y,z,c), I[35] = (T)(img)(_p3##x,_n2##y,z,c), \ - I[36] = (T)(img)(_p2##x,_n2##y,z,c), I[37] = (T)(img)(_p1##x,_n2##y,z,c), I[38] = (T)(img)(x,_n2##y,z,c), \ - I[39] = (T)(img)(_n1##x,_n2##y,z,c), I[40] = (T)(img)(_n2##x,_n2##y,z,c), I[41] = (T)(img)(_n3##x,_n2##y,z,c), \ - I[42] = (T)(img)(_p3##x,_n3##y,z,c), I[43] = (T)(img)(_p2##x,_n3##y,z,c), I[44] = (T)(img)(_p1##x,_n3##y,z,c), \ - I[45] = (T)(img)(x,_n3##y,z,c), I[46] = (T)(img)(_n1##x,_n3##y,z,c), I[47] = (T)(img)(_n2##x,_n3##y,z,c), \ - I[48] = (T)(img)(_n3##x,_n3##y,z,c) - -#define cimg_get8x8(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p3##x,_p3##y,z,c), I[1] = (T)(img)(_p2##x,_p3##y,z,c), I[2] = (T)(img)(_p1##x,_p3##y,z,c), \ - I[3] = (T)(img)(x,_p3##y,z,c), I[4] = (T)(img)(_n1##x,_p3##y,z,c), I[5] = (T)(img)(_n2##x,_p3##y,z,c), \ - I[6] = (T)(img)(_n3##x,_p3##y,z,c), I[7] = (T)(img)(_n4##x,_p3##y,z,c), I[8] = (T)(img)(_p3##x,_p2##y,z,c), \ - I[9] = (T)(img)(_p2##x,_p2##y,z,c), I[10] = (T)(img)(_p1##x,_p2##y,z,c), I[11] = (T)(img)(x,_p2##y,z,c), \ - I[12] = (T)(img)(_n1##x,_p2##y,z,c), I[13] = (T)(img)(_n2##x,_p2##y,z,c), I[14] = (T)(img)(_n3##x,_p2##y,z,c), \ - I[15] = (T)(img)(_n4##x,_p2##y,z,c), I[16] = (T)(img)(_p3##x,_p1##y,z,c), I[17] = (T)(img)(_p2##x,_p1##y,z,c), \ - I[18] = (T)(img)(_p1##x,_p1##y,z,c), I[19] = (T)(img)(x,_p1##y,z,c), I[20] = (T)(img)(_n1##x,_p1##y,z,c), \ - I[21] = (T)(img)(_n2##x,_p1##y,z,c), I[22] = (T)(img)(_n3##x,_p1##y,z,c), I[23] = (T)(img)(_n4##x,_p1##y,z,c), \ - I[24] = (T)(img)(_p3##x,y,z,c), I[25] = (T)(img)(_p2##x,y,z,c), I[26] = (T)(img)(_p1##x,y,z,c), \ - I[27] = (T)(img)(x,y,z,c), I[28] = (T)(img)(_n1##x,y,z,c), I[29] = (T)(img)(_n2##x,y,z,c), \ - I[30] = (T)(img)(_n3##x,y,z,c), I[31] = (T)(img)(_n4##x,y,z,c), I[32] = (T)(img)(_p3##x,_n1##y,z,c), \ - I[33] = (T)(img)(_p2##x,_n1##y,z,c), I[34] = (T)(img)(_p1##x,_n1##y,z,c), I[35] = (T)(img)(x,_n1##y,z,c), \ - I[36] = (T)(img)(_n1##x,_n1##y,z,c), I[37] = (T)(img)(_n2##x,_n1##y,z,c), I[38] = (T)(img)(_n3##x,_n1##y,z,c), \ - I[39] = (T)(img)(_n4##x,_n1##y,z,c), I[40] = (T)(img)(_p3##x,_n2##y,z,c), I[41] = (T)(img)(_p2##x,_n2##y,z,c), \ - I[42] = (T)(img)(_p1##x,_n2##y,z,c), I[43] = (T)(img)(x,_n2##y,z,c), I[44] = (T)(img)(_n1##x,_n2##y,z,c), \ - I[45] = (T)(img)(_n2##x,_n2##y,z,c), I[46] = (T)(img)(_n3##x,_n2##y,z,c), I[47] = (T)(img)(_n4##x,_n2##y,z,c), \ - I[48] = (T)(img)(_p3##x,_n3##y,z,c), I[49] = (T)(img)(_p2##x,_n3##y,z,c), I[50] = (T)(img)(_p1##x,_n3##y,z,c), \ - I[51] = (T)(img)(x,_n3##y,z,c), I[52] = (T)(img)(_n1##x,_n3##y,z,c), I[53] = (T)(img)(_n2##x,_n3##y,z,c), \ - I[54] = (T)(img)(_n3##x,_n3##y,z,c), I[55] = (T)(img)(_n4##x,_n3##y,z,c), I[56] = (T)(img)(_p3##x,_n4##y,z,c), \ - I[57] = (T)(img)(_p2##x,_n4##y,z,c), I[58] = (T)(img)(_p1##x,_n4##y,z,c), I[59] = (T)(img)(x,_n4##y,z,c), \ - I[60] = (T)(img)(_n1##x,_n4##y,z,c), I[61] = (T)(img)(_n2##x,_n4##y,z,c), I[62] = (T)(img)(_n3##x,_n4##y,z,c), \ - I[63] = (T)(img)(_n4##x,_n4##y,z,c); - -#define cimg_get9x9(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p4##x,_p4##y,z,c), I[1] = (T)(img)(_p3##x,_p4##y,z,c), I[2] = (T)(img)(_p2##x,_p4##y,z,c), \ - I[3] = (T)(img)(_p1##x,_p4##y,z,c), I[4] = (T)(img)(x,_p4##y,z,c), I[5] = (T)(img)(_n1##x,_p4##y,z,c), \ - I[6] = (T)(img)(_n2##x,_p4##y,z,c), I[7] = (T)(img)(_n3##x,_p4##y,z,c), I[8] = (T)(img)(_n4##x,_p4##y,z,c), \ - I[9] = (T)(img)(_p4##x,_p3##y,z,c), I[10] = (T)(img)(_p3##x,_p3##y,z,c), I[11] = (T)(img)(_p2##x,_p3##y,z,c), \ - I[12] = (T)(img)(_p1##x,_p3##y,z,c), I[13] = (T)(img)(x,_p3##y,z,c), I[14] = (T)(img)(_n1##x,_p3##y,z,c), \ - I[15] = (T)(img)(_n2##x,_p3##y,z,c), I[16] = (T)(img)(_n3##x,_p3##y,z,c), I[17] = (T)(img)(_n4##x,_p3##y,z,c), \ - I[18] = (T)(img)(_p4##x,_p2##y,z,c), I[19] = (T)(img)(_p3##x,_p2##y,z,c), I[20] = (T)(img)(_p2##x,_p2##y,z,c), \ - I[21] = (T)(img)(_p1##x,_p2##y,z,c), I[22] = (T)(img)(x,_p2##y,z,c), I[23] = (T)(img)(_n1##x,_p2##y,z,c), \ - I[24] = (T)(img)(_n2##x,_p2##y,z,c), I[25] = (T)(img)(_n3##x,_p2##y,z,c), I[26] = (T)(img)(_n4##x,_p2##y,z,c), \ - I[27] = (T)(img)(_p4##x,_p1##y,z,c), I[28] = (T)(img)(_p3##x,_p1##y,z,c), I[29] = (T)(img)(_p2##x,_p1##y,z,c), \ - I[30] = (T)(img)(_p1##x,_p1##y,z,c), I[31] = (T)(img)(x,_p1##y,z,c), I[32] = (T)(img)(_n1##x,_p1##y,z,c), \ - I[33] = (T)(img)(_n2##x,_p1##y,z,c), I[34] = (T)(img)(_n3##x,_p1##y,z,c), I[35] = (T)(img)(_n4##x,_p1##y,z,c), \ - I[36] = (T)(img)(_p4##x,y,z,c), I[37] = (T)(img)(_p3##x,y,z,c), I[38] = (T)(img)(_p2##x,y,z,c), \ - I[39] = (T)(img)(_p1##x,y,z,c), I[40] = (T)(img)(x,y,z,c), I[41] = (T)(img)(_n1##x,y,z,c), \ - I[42] = (T)(img)(_n2##x,y,z,c), I[43] = (T)(img)(_n3##x,y,z,c), I[44] = (T)(img)(_n4##x,y,z,c), \ - I[45] = (T)(img)(_p4##x,_n1##y,z,c), I[46] = (T)(img)(_p3##x,_n1##y,z,c), I[47] = (T)(img)(_p2##x,_n1##y,z,c), \ - I[48] = (T)(img)(_p1##x,_n1##y,z,c), I[49] = (T)(img)(x,_n1##y,z,c), I[50] = (T)(img)(_n1##x,_n1##y,z,c), \ - I[51] = (T)(img)(_n2##x,_n1##y,z,c), I[52] = (T)(img)(_n3##x,_n1##y,z,c), I[53] = (T)(img)(_n4##x,_n1##y,z,c), \ - I[54] = (T)(img)(_p4##x,_n2##y,z,c), I[55] = (T)(img)(_p3##x,_n2##y,z,c), I[56] = (T)(img)(_p2##x,_n2##y,z,c), \ - I[57] = (T)(img)(_p1##x,_n2##y,z,c), I[58] = (T)(img)(x,_n2##y,z,c), I[59] = (T)(img)(_n1##x,_n2##y,z,c), \ - I[60] = (T)(img)(_n2##x,_n2##y,z,c), I[61] = (T)(img)(_n3##x,_n2##y,z,c), I[62] = (T)(img)(_n4##x,_n2##y,z,c), \ - I[63] = (T)(img)(_p4##x,_n3##y,z,c), I[64] = (T)(img)(_p3##x,_n3##y,z,c), I[65] = (T)(img)(_p2##x,_n3##y,z,c), \ - I[66] = (T)(img)(_p1##x,_n3##y,z,c), I[67] = (T)(img)(x,_n3##y,z,c), I[68] = (T)(img)(_n1##x,_n3##y,z,c), \ - I[69] = (T)(img)(_n2##x,_n3##y,z,c), I[70] = (T)(img)(_n3##x,_n3##y,z,c), I[71] = (T)(img)(_n4##x,_n3##y,z,c), \ - I[72] = (T)(img)(_p4##x,_n4##y,z,c), I[73] = (T)(img)(_p3##x,_n4##y,z,c), I[74] = (T)(img)(_p2##x,_n4##y,z,c), \ - I[75] = (T)(img)(_p1##x,_n4##y,z,c), I[76] = (T)(img)(x,_n4##y,z,c), I[77] = (T)(img)(_n1##x,_n4##y,z,c), \ - I[78] = (T)(img)(_n2##x,_n4##y,z,c), I[79] = (T)(img)(_n3##x,_n4##y,z,c), I[80] = (T)(img)(_n4##x,_n4##y,z,c) - -#define cimg_get2x2x2(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(x,y,z,c), I[1] = (T)(img)(_n1##x,y,z,c), I[2] = (T)(img)(x,_n1##y,z,c), \ - I[3] = (T)(img)(_n1##x,_n1##y,z,c), I[4] = (T)(img)(x,y,_n1##z,c), I[5] = (T)(img)(_n1##x,y,_n1##z,c), \ - I[6] = (T)(img)(x,_n1##y,_n1##z,c), I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c) - -#define cimg_get3x3x3(img,x,y,z,c,I,T) \ - I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c), I[1] = (T)(img)(x,_p1##y,_p1##z,c), \ - I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c), I[3] = (T)(img)(_p1##x,y,_p1##z,c), I[4] = (T)(img)(x,y,_p1##z,c), \ - I[5] = (T)(img)(_n1##x,y,_p1##z,c), I[6] = (T)(img)(_p1##x,_n1##y,_p1##z,c), I[7] = (T)(img)(x,_n1##y,_p1##z,c), \ - I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c), I[9] = (T)(img)(_p1##x,_p1##y,z,c), I[10] = (T)(img)(x,_p1##y,z,c), \ - I[11] = (T)(img)(_n1##x,_p1##y,z,c), I[12] = (T)(img)(_p1##x,y,z,c), I[13] = (T)(img)(x,y,z,c), \ - I[14] = (T)(img)(_n1##x,y,z,c), I[15] = (T)(img)(_p1##x,_n1##y,z,c), I[16] = (T)(img)(x,_n1##y,z,c), \ - I[17] = (T)(img)(_n1##x,_n1##y,z,c), I[18] = (T)(img)(_p1##x,_p1##y,_n1##z,c), I[19] = (T)(img)(x,_p1##y,_n1##z,c), \ - I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c), I[21] = (T)(img)(_p1##x,y,_n1##z,c), I[22] = (T)(img)(x,y,_n1##z,c), \ - I[23] = (T)(img)(_n1##x,y,_n1##z,c), I[24] = (T)(img)(_p1##x,_n1##y,_n1##z,c), I[25] = (T)(img)(x,_n1##y,_n1##z,c), \ - I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c) - -// Macros to perform various image loops. -// -// These macros are simpler to use than loops with C++ iterators. -#define cimg_for(img,ptrs,T_ptrs) \ - for (T_ptrs *ptrs = (img)._data, *_max##ptrs = (img)._data + (img).size(); ptrs<_max##ptrs; ++ptrs) -#define cimg_rof(img,ptrs,T_ptrs) for (T_ptrs *ptrs = (img)._data + (img).size() - 1; ptrs>=(img)._data; --ptrs) -#define cimg_foroff(img,off) for (unsigned long off = 0, _max##off = (img).size(); off<_max##off; ++off) - -#define cimg_for1(bound,i) for (int i = 0; i<(int)(bound); ++i) -#define cimg_forX(img,x) cimg_for1((img)._width,x) -#define cimg_forY(img,y) cimg_for1((img)._height,y) -#define cimg_forZ(img,z) cimg_for1((img)._depth,z) -#define cimg_forC(img,c) cimg_for1((img)._spectrum,c) -#define cimg_forXY(img,x,y) cimg_forY(img,y) cimg_forX(img,x) -#define cimg_forXZ(img,x,z) cimg_forZ(img,z) cimg_forX(img,x) -#define cimg_forYZ(img,y,z) cimg_forZ(img,z) cimg_forY(img,y) -#define cimg_forXC(img,x,c) cimg_forC(img,c) cimg_forX(img,x) -#define cimg_forYC(img,y,c) cimg_forC(img,c) cimg_forY(img,y) -#define cimg_forZC(img,z,c) cimg_forC(img,c) cimg_forZ(img,z) -#define cimg_forXYZ(img,x,y,z) cimg_forZ(img,z) cimg_forXY(img,x,y) -#define cimg_forXYC(img,x,y,c) cimg_forC(img,c) cimg_forXY(img,x,y) -#define cimg_forXZC(img,x,z,c) cimg_forC(img,c) cimg_forXZ(img,x,z) -#define cimg_forYZC(img,y,z,c) cimg_forC(img,c) cimg_forYZ(img,y,z) -#define cimg_forXYZC(img,x,y,z,c) cimg_forC(img,c) cimg_forXYZ(img,x,y,z) - -#define cimg_rof1(bound,i) for (int i = (int)(bound)-1; i>=0; --i) -#define cimg_rofX(img,x) cimg_rof1((img)._width,x) -#define cimg_rofY(img,y) cimg_rof1((img)._height,y) -#define cimg_rofZ(img,z) cimg_rof1((img)._depth,z) -#define cimg_rofC(img,c) cimg_rof1((img)._spectrum,c) -#define cimg_rofXY(img,x,y) cimg_rofY(img,y) cimg_rofX(img,x) -#define cimg_rofXZ(img,x,z) cimg_rofZ(img,z) cimg_rofX(img,x) -#define cimg_rofYZ(img,y,z) cimg_rofZ(img,z) cimg_rofY(img,y) -#define cimg_rofXC(img,x,c) cimg_rofC(img,c) cimg_rofX(img,x) -#define cimg_rofYC(img,y,c) cimg_rofC(img,c) cimg_rofY(img,y) -#define cimg_rofZC(img,z,c) cimg_rofC(img,c) cimg_rofZ(img,z) -#define cimg_rofXYZ(img,x,y,z) cimg_rofZ(img,z) cimg_rofXY(img,x,y) -#define cimg_rofXYC(img,x,y,c) cimg_rofC(img,c) cimg_rofXY(img,x,y) -#define cimg_rofXZC(img,x,z,c) cimg_rofC(img,c) cimg_rofXZ(img,x,z) -#define cimg_rofYZC(img,y,z,c) cimg_rofC(img,c) cimg_rofYZ(img,y,z) -#define cimg_rofXYZC(img,x,y,z,c) cimg_rofC(img,c) cimg_rofXYZ(img,x,y,z) - -#define cimg_for_in1(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), _max##i = (int)(i1)<(int)(bound)?(int)(i1):(int)(bound)-1; i<=_max##i; ++i) -#define cimg_for_inX(img,x0,x1,x) cimg_for_in1((img)._width,x0,x1,x) -#define cimg_for_inY(img,y0,y1,y) cimg_for_in1((img)._height,y0,y1,y) -#define cimg_for_inZ(img,z0,z1,z) cimg_for_in1((img)._depth,z0,z1,z) -#define cimg_for_inC(img,c0,c1,c) cimg_for_in1((img)._spectrum,c0,c1,c) -#define cimg_for_inXY(img,x0,y0,x1,y1,x,y) cimg_for_inY(img,y0,y1,y) cimg_for_inX(img,x0,x1,x) -#define cimg_for_inXZ(img,x0,z0,x1,z1,x,z) cimg_for_inZ(img,z0,z1,z) cimg_for_inX(img,x0,x1,x) -#define cimg_for_inXC(img,x0,c0,x1,c1,x,c) cimg_for_inC(img,c0,c1,c) cimg_for_inX(img,x0,x1,x) -#define cimg_for_inYZ(img,y0,z0,y1,z1,y,z) cimg_for_inZ(img,x0,z1,z) cimg_for_inY(img,y0,y1,y) -#define cimg_for_inYC(img,y0,c0,y1,c1,y,c) cimg_for_inC(img,c0,c1,c) cimg_for_inY(img,y0,y1,y) -#define cimg_for_inZC(img,z0,c0,z1,c1,z,c) cimg_for_inC(img,c0,c1,c) cimg_for_inZ(img,z0,z1,z) -#define cimg_for_inXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_inZ(img,z0,z1,z) cimg_for_inXY(img,x0,y0,x1,y1,x,y) -#define cimg_for_inXYC(img,x0,y0,c0,x1,y1,c1,x,y,c) cimg_for_inC(img,c0,c1,c) cimg_for_inXY(img,x0,y0,x1,y1,x,y) -#define cimg_for_inXZC(img,x0,z0,c0,x1,z1,c1,x,z,c) cimg_for_inC(img,c0,c1,c) cimg_for_inXZ(img,x0,z0,x1,z1,x,z) -#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) -#define cimg_for_inXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_inC(img,c0,c1,c) cimg_for_inXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) -#define cimg_for_insideX(img,x,n) cimg_for_inX(img,n,(img)._width-1-(n),x) -#define cimg_for_insideY(img,y,n) cimg_for_inY(img,n,(img)._height-1-(n),y) -#define cimg_for_insideZ(img,z,n) cimg_for_inZ(img,n,(img)._depth-1-(n),z) -#define cimg_for_insideC(img,c,n) cimg_for_inC(img,n,(img)._spectrum-1-(n),c) -#define cimg_for_insideXY(img,x,y,n) cimg_for_inXY(img,n,n,(img)._width-1-(n),(img)._height-1-(n),x,y) -#define cimg_for_insideXYZ(img,x,y,z,n) \ - cimg_for_inXYZ(img,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),x,y,z) -#define cimg_for_insideXYZC(img,x,y,z,c,n) \ - cimg_for_inXYZ(img,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),x,y,z) - -#define cimg_for_out1(boundi,i0,i1,i) \ - for (int i = (int)(i0)>0?0:(int)(i1)+1; i<(int)(boundi); ++i, i = i==(int)(i0)?(int)(i1)+1:i) -#define cimg_for_out2(boundi,boundj,i0,j0,i1,j1,i,j) \ - for (int j = 0; j<(int)(boundj); ++j) \ - for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j?0:(int)(i0)>0?0:(int)(i1)+1; i<(int)(boundi); \ - ++i, i = _n1j?i:(i==(int)(i0)?(int)(i1)+1:i)) -#define cimg_for_out3(boundi,boundj,boundk,i0,j0,k0,i1,j1,k1,i,j,k) \ - for (int k = 0; k<(int)(boundk); ++k) \ - for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \ - for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k?0:(int)(i0)>0?0:(int)(i1)+1; i<(int)(boundi); \ - ++i, i = _n1j || _n1k?i:(i==(int)(i0)?(int)(i1)+1:i)) -#define cimg_for_out4(boundi,boundj,boundk,boundl,i0,j0,k0,l0,i1,j1,k1,l1,i,j,k,l) \ - for (int l = 0; l<(int)(boundl); ++l) \ - for (int _n1l = (int)(l<(int)(l0) || l>(int)(l1)), k = 0; k<(int)(boundk); ++k) \ - for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \ - for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k || _n1l?0:(int)(i0)>0?0:(int)(i1)+1; \ - i<(int)(boundi); ++i, i = _n1j || _n1k || _n1l?i:(i==(int)(i0)?(int)(i1)+1:i)) -#define cimg_for_outX(img,x0,x1,x) cimg_for_out1((img)._width,x0,x1,x) -#define cimg_for_outY(img,y0,y1,y) cimg_for_out1((img)._height,y0,y1,y) -#define cimg_for_outZ(img,z0,z1,z) cimg_for_out1((img)._depth,z0,z1,z) -#define cimg_for_outC(img,c0,c1,c) cimg_for_out1((img)._spectrum,c0,c1,c) -#define cimg_for_outXY(img,x0,y0,x1,y1,x,y) cimg_for_out2((img)._width,(img)._height,x0,y0,x1,y1,x,y) -#define cimg_for_outXZ(img,x0,z0,x1,z1,x,z) cimg_for_out2((img)._width,(img)._depth,x0,z0,x1,z1,x,z) -#define cimg_for_outXC(img,x0,c0,x1,c1,x,c) cimg_for_out2((img)._width,(img)._spectrum,x0,c0,x1,c1,x,c) -#define cimg_for_outYZ(img,y0,z0,y1,z1,y,z) cimg_for_out2((img)._height,(img)._depth,y0,z0,y1,z1,y,z) -#define cimg_for_outYC(img,y0,c0,y1,c1,y,c) cimg_for_out2((img)._height,(img)._spectrum,y0,c0,y1,c1,y,c) -#define cimg_for_outZC(img,z0,c0,z1,c1,z,c) cimg_for_out2((img)._depth,(img)._spectrum,z0,c0,z1,c1,z,c) -#define cimg_for_outXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) \ - cimg_for_out3((img)._width,(img)._height,(img)._depth,x0,y0,z0,x1,y1,z1,x,y,z) -#define cimg_for_outXYC(img,x0,y0,c0,x1,y1,c1,x,y,c) \ - cimg_for_out3((img)._width,(img)._height,(img)._spectrum,x0,y0,c0,x1,y1,c1,x,y,c) -#define cimg_for_outXZC(img,x0,z0,c0,x1,z1,c1,x,z,c) \ - cimg_for_out3((img)._width,(img)._depth,(img)._spectrum,x0,z0,c0,x1,z1,c1,x,z,c) -#define cimg_for_outYZC(img,y0,z0,c0,y1,z1,c1,y,z,c) \ - cimg_for_out3((img)._height,(img)._depth,(img)._spectrum,y0,z0,c0,y1,z1,c1,y,z,c) -#define cimg_for_outXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_out4((img)._width,(img)._height,(img)._depth,(img)._spectrum,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) -#define cimg_for_borderX(img,x,n) cimg_for_outX(img,n,(img)._width-1-(n),x) -#define cimg_for_borderY(img,y,n) cimg_for_outY(img,n,(img)._height-1-(n),y) -#define cimg_for_borderZ(img,z,n) cimg_for_outZ(img,n,(img)._depth-1-(n),z) -#define cimg_for_borderC(img,c,n) cimg_for_outC(img,n,(img)._spectrum-1-(n),c) -#define cimg_for_borderXY(img,x,y,n) cimg_for_outXY(img,n,n,(img)._width-1-(n),(img)._height-1-(n),x,y) -#define cimg_for_borderXYZ(img,x,y,z,n) \ - cimg_for_outXYZ(img,n,n,n,(img)._width-1-(n),(img)._height-1-(n),(img)._depth-1-(n),x,y,z) -#define cimg_for_borderXYZC(img,x,y,z,c,n) \ - 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) - -#define cimg_for_spiralXY(img,x,y) \ - for (int x = 0, y = 0, _n1##x = 1, _n1##y = (img).width()*(img).height(); _n1##y; \ - --_n1##y, _n1##x+=(_n1##x>>2)-((!(_n1##x&3)?--y:((_n1##x&3)==1?(img)._width-1-++x:\ - ((_n1##x&3)==2?(img)._height-1-++y:--x))))?0:1) - -#define cimg_for_lineXY(x,y,x0,y0,x1,y1) \ - for (int x = (int)(x0), y = (int)(y0), _sx = 1, _sy = 1, _steep = 0, \ - _dx=(x1)>(x0)?(int)(x1)-(int)(x0):(_sx=-1,(int)(x0)-(int)(x1)), \ - _dy=(y1)>(y0)?(int)(y1)-(int)(y0):(_sy=-1,(int)(y0)-(int)(y1)), \ - _counter = _dx, \ - _err = _dx>_dy?(_dy>>1):((_steep=1),(_counter=_dy),(_dx>>1)); \ - _counter>=0; \ - --_counter, x+=_steep? \ - (y+=_sy,(_err-=_dx)<0?_err+=_dy,_sx:0): \ - (y+=(_err-=_dy)<0?_err+=_dx,_sy:0,_sx)) - -#define cimg_for2(bound,i) \ - for (int i = 0, _n1##i = 1>=(bound)?(int)(bound)-1:1; \ - _n1##i<(int)(bound) || i==--_n1##i; \ - ++i, ++_n1##i) -#define cimg_for2X(img,x) cimg_for2((img)._width,x) -#define cimg_for2Y(img,y) cimg_for2((img)._height,y) -#define cimg_for2Z(img,z) cimg_for2((img)._depth,z) -#define cimg_for2C(img,c) cimg_for2((img)._spectrum,c) -#define cimg_for2XY(img,x,y) cimg_for2Y(img,y) cimg_for2X(img,x) -#define cimg_for2XZ(img,x,z) cimg_for2Z(img,z) cimg_for2X(img,x) -#define cimg_for2XC(img,x,c) cimg_for2C(img,c) cimg_for2X(img,x) -#define cimg_for2YZ(img,y,z) cimg_for2Z(img,z) cimg_for2Y(img,y) -#define cimg_for2YC(img,y,c) cimg_for2C(img,c) cimg_for2Y(img,y) -#define cimg_for2ZC(img,z,c) cimg_for2C(img,c) cimg_for2Z(img,z) -#define cimg_for2XYZ(img,x,y,z) cimg_for2Z(img,z) cimg_for2XY(img,x,y) -#define cimg_for2XZC(img,x,z,c) cimg_for2C(img,c) cimg_for2XZ(img,x,z) -#define cimg_for2YZC(img,y,z,c) cimg_for2C(img,c) cimg_for2YZ(img,y,z) -#define cimg_for2XYZC(img,x,y,z,c) cimg_for2C(img,c) cimg_for2XYZ(img,x,y,z) - -#define cimg_for_in2(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1; \ - i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \ - ++i, ++_n1##i) -#define cimg_for_in2X(img,x0,x1,x) cimg_for_in2((img)._width,x0,x1,x) -#define cimg_for_in2Y(img,y0,y1,y) cimg_for_in2((img)._height,y0,y1,y) -#define cimg_for_in2Z(img,z0,z1,z) cimg_for_in2((img)._depth,z0,z1,z) -#define cimg_for_in2C(img,c0,c1,c) cimg_for_in2((img)._spectrum,c0,c1,c) -#define cimg_for_in2XY(img,x0,y0,x1,y1,x,y) cimg_for_in2Y(img,y0,y1,y) cimg_for_in2X(img,x0,x1,x) -#define cimg_for_in2XZ(img,x0,z0,x1,z1,x,z) cimg_for_in2Z(img,z0,z1,z) cimg_for_in2X(img,x0,x1,x) -#define cimg_for_in2XC(img,x0,c0,x1,c1,x,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2X(img,x0,x1,x) -#define cimg_for_in2YZ(img,y0,z0,y1,z1,y,z) cimg_for_in2Z(img,z0,z1,z) cimg_for_in2Y(img,y0,y1,y) -#define cimg_for_in2YC(img,y0,c0,y1,c1,y,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2Y(img,y0,y1,y) -#define cimg_for_in2ZC(img,z0,c0,z1,c1,z,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2Z(img,z0,z1,z) -#define cimg_for_in2XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in2Z(img,z0,z1,z) cimg_for_in2XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in2XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in2YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in2XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in2C(img,c0,c1,c) cimg_for_in2XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for3(bound,i) \ - for (int i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1; \ - _n1##i<(int)(bound) || i==--_n1##i; \ - _p1##i = i++, ++_n1##i) -#define cimg_for3X(img,x) cimg_for3((img)._width,x) -#define cimg_for3Y(img,y) cimg_for3((img)._height,y) -#define cimg_for3Z(img,z) cimg_for3((img)._depth,z) -#define cimg_for3C(img,c) cimg_for3((img)._spectrum,c) -#define cimg_for3XY(img,x,y) cimg_for3Y(img,y) cimg_for3X(img,x) -#define cimg_for3XZ(img,x,z) cimg_for3Z(img,z) cimg_for3X(img,x) -#define cimg_for3XC(img,x,c) cimg_for3C(img,c) cimg_for3X(img,x) -#define cimg_for3YZ(img,y,z) cimg_for3Z(img,z) cimg_for3Y(img,y) -#define cimg_for3YC(img,y,c) cimg_for3C(img,c) cimg_for3Y(img,y) -#define cimg_for3ZC(img,z,c) cimg_for3C(img,c) cimg_for3Z(img,z) -#define cimg_for3XYZ(img,x,y,z) cimg_for3Z(img,z) cimg_for3XY(img,x,y) -#define cimg_for3XZC(img,x,z,c) cimg_for3C(img,c) cimg_for3XZ(img,x,z) -#define cimg_for3YZC(img,y,z,c) cimg_for3C(img,c) cimg_for3YZ(img,y,z) -#define cimg_for3XYZC(img,x,y,z,c) cimg_for3C(img,c) cimg_for3XYZ(img,x,y,z) - -#define cimg_for_in3(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1; \ - i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \ - _p1##i = i++, ++_n1##i) -#define cimg_for_in3X(img,x0,x1,x) cimg_for_in3((img)._width,x0,x1,x) -#define cimg_for_in3Y(img,y0,y1,y) cimg_for_in3((img)._height,y0,y1,y) -#define cimg_for_in3Z(img,z0,z1,z) cimg_for_in3((img)._depth,z0,z1,z) -#define cimg_for_in3C(img,c0,c1,c) cimg_for_in3((img)._spectrum,c0,c1,c) -#define cimg_for_in3XY(img,x0,y0,x1,y1,x,y) cimg_for_in3Y(img,y0,y1,y) cimg_for_in3X(img,x0,x1,x) -#define cimg_for_in3XZ(img,x0,z0,x1,z1,x,z) cimg_for_in3Z(img,z0,z1,z) cimg_for_in3X(img,x0,x1,x) -#define cimg_for_in3XC(img,x0,c0,x1,c1,x,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3X(img,x0,x1,x) -#define cimg_for_in3YZ(img,y0,z0,y1,z1,y,z) cimg_for_in3Z(img,z0,z1,z) cimg_for_in3Y(img,y0,y1,y) -#define cimg_for_in3YC(img,y0,c0,y1,c1,y,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3Y(img,y0,y1,y) -#define cimg_for_in3ZC(img,z0,c0,z1,c1,z,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3Z(img,z0,z1,z) -#define cimg_for_in3XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in3Z(img,z0,z1,z) cimg_for_in3XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in3XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in3YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in3XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in3C(img,c0,c1,c) cimg_for_in3XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for4(bound,i) \ - for (int i = 0, _p1##i = 0, _n1##i = 1>=(bound)?(int)(bound)-1:1, \ - _n2##i = 2>=(bound)?(int)(bound)-1:2; \ - _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \ - _p1##i = i++, ++_n1##i, ++_n2##i) -#define cimg_for4X(img,x) cimg_for4((img)._width,x) -#define cimg_for4Y(img,y) cimg_for4((img)._height,y) -#define cimg_for4Z(img,z) cimg_for4((img)._depth,z) -#define cimg_for4C(img,c) cimg_for4((img)._spectrum,c) -#define cimg_for4XY(img,x,y) cimg_for4Y(img,y) cimg_for4X(img,x) -#define cimg_for4XZ(img,x,z) cimg_for4Z(img,z) cimg_for4X(img,x) -#define cimg_for4XC(img,x,c) cimg_for4C(img,c) cimg_for4X(img,x) -#define cimg_for4YZ(img,y,z) cimg_for4Z(img,z) cimg_for4Y(img,y) -#define cimg_for4YC(img,y,c) cimg_for4C(img,c) cimg_for4Y(img,y) -#define cimg_for4ZC(img,z,c) cimg_for4C(img,c) cimg_for4Z(img,z) -#define cimg_for4XYZ(img,x,y,z) cimg_for4Z(img,z) cimg_for4XY(img,x,y) -#define cimg_for4XZC(img,x,z,c) cimg_for4C(img,c) cimg_for4XZ(img,x,z) -#define cimg_for4YZC(img,y,z,c) cimg_for4C(img,c) cimg_for4YZ(img,y,z) -#define cimg_for4XYZC(img,x,y,z,c) cimg_for4C(img,c) cimg_for4XYZ(img,x,y,z) - -#define cimg_for_in4(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \ - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2; \ - i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \ - _p1##i = i++, ++_n1##i, ++_n2##i) -#define cimg_for_in4X(img,x0,x1,x) cimg_for_in4((img)._width,x0,x1,x) -#define cimg_for_in4Y(img,y0,y1,y) cimg_for_in4((img)._height,y0,y1,y) -#define cimg_for_in4Z(img,z0,z1,z) cimg_for_in4((img)._depth,z0,z1,z) -#define cimg_for_in4C(img,c0,c1,c) cimg_for_in4((img)._spectrum,c0,c1,c) -#define cimg_for_in4XY(img,x0,y0,x1,y1,x,y) cimg_for_in4Y(img,y0,y1,y) cimg_for_in4X(img,x0,x1,x) -#define cimg_for_in4XZ(img,x0,z0,x1,z1,x,z) cimg_for_in4Z(img,z0,z1,z) cimg_for_in4X(img,x0,x1,x) -#define cimg_for_in4XC(img,x0,c0,x1,c1,x,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4X(img,x0,x1,x) -#define cimg_for_in4YZ(img,y0,z0,y1,z1,y,z) cimg_for_in4Z(img,z0,z1,z) cimg_for_in4Y(img,y0,y1,y) -#define cimg_for_in4YC(img,y0,c0,y1,c1,y,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4Y(img,y0,y1,y) -#define cimg_for_in4ZC(img,z0,c0,z1,c1,z,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4Z(img,z0,z1,z) -#define cimg_for_in4XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in4Z(img,z0,z1,z) cimg_for_in4XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in4XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in4YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in4XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in4C(img,c0,c1,c) cimg_for_in4XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for5(bound,i) \ - for (int i = 0, _p2##i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1, \ - _n2##i = 2>=(bound)?(int)(bound)-1:2; \ - _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \ - _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i) -#define cimg_for5X(img,x) cimg_for5((img)._width,x) -#define cimg_for5Y(img,y) cimg_for5((img)._height,y) -#define cimg_for5Z(img,z) cimg_for5((img)._depth,z) -#define cimg_for5C(img,c) cimg_for5((img)._spectrum,c) -#define cimg_for5XY(img,x,y) cimg_for5Y(img,y) cimg_for5X(img,x) -#define cimg_for5XZ(img,x,z) cimg_for5Z(img,z) cimg_for5X(img,x) -#define cimg_for5XC(img,x,c) cimg_for5C(img,c) cimg_for5X(img,x) -#define cimg_for5YZ(img,y,z) cimg_for5Z(img,z) cimg_for5Y(img,y) -#define cimg_for5YC(img,y,c) cimg_for5C(img,c) cimg_for5Y(img,y) -#define cimg_for5ZC(img,z,c) cimg_for5C(img,c) cimg_for5Z(img,z) -#define cimg_for5XYZ(img,x,y,z) cimg_for5Z(img,z) cimg_for5XY(img,x,y) -#define cimg_for5XZC(img,x,z,c) cimg_for5C(img,c) cimg_for5XZ(img,x,z) -#define cimg_for5YZC(img,y,z,c) cimg_for5C(img,c) cimg_for5YZ(img,y,z) -#define cimg_for5XYZC(img,x,y,z,c) cimg_for5C(img,c) cimg_for5XYZ(img,x,y,z) - -#define cimg_for_in5(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p2##i = i-2<0?0:i-2, \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \ - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2; \ - i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \ - _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i) -#define cimg_for_in5X(img,x0,x1,x) cimg_for_in5((img)._width,x0,x1,x) -#define cimg_for_in5Y(img,y0,y1,y) cimg_for_in5((img)._height,y0,y1,y) -#define cimg_for_in5Z(img,z0,z1,z) cimg_for_in5((img)._depth,z0,z1,z) -#define cimg_for_in5C(img,c0,c1,c) cimg_for_in5((img)._spectrum,c0,c1,c) -#define cimg_for_in5XY(img,x0,y0,x1,y1,x,y) cimg_for_in5Y(img,y0,y1,y) cimg_for_in5X(img,x0,x1,x) -#define cimg_for_in5XZ(img,x0,z0,x1,z1,x,z) cimg_for_in5Z(img,z0,z1,z) cimg_for_in5X(img,x0,x1,x) -#define cimg_for_in5XC(img,x0,c0,x1,c1,x,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5X(img,x0,x1,x) -#define cimg_for_in5YZ(img,y0,z0,y1,z1,y,z) cimg_for_in5Z(img,z0,z1,z) cimg_for_in5Y(img,y0,y1,y) -#define cimg_for_in5YC(img,y0,c0,y1,c1,y,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5Y(img,y0,y1,y) -#define cimg_for_in5ZC(img,z0,c0,z1,c1,z,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5Z(img,z0,z1,z) -#define cimg_for_in5XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in5Z(img,z0,z1,z) cimg_for_in5XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in5XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in5YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in5XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in5C(img,c0,c1,c) cimg_for_in5XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for6(bound,i) \ - for (int i = 0, _p2##i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1, \ - _n2##i = 2>=(bound)?(int)(bound)-1:2, \ - _n3##i = 3>=(bound)?(int)(bound)-1:3; \ - _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \ - _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i) -#define cimg_for6X(img,x) cimg_for6((img)._width,x) -#define cimg_for6Y(img,y) cimg_for6((img)._height,y) -#define cimg_for6Z(img,z) cimg_for6((img)._depth,z) -#define cimg_for6C(img,c) cimg_for6((img)._spectrum,c) -#define cimg_for6XY(img,x,y) cimg_for6Y(img,y) cimg_for6X(img,x) -#define cimg_for6XZ(img,x,z) cimg_for6Z(img,z) cimg_for6X(img,x) -#define cimg_for6XC(img,x,c) cimg_for6C(img,c) cimg_for6X(img,x) -#define cimg_for6YZ(img,y,z) cimg_for6Z(img,z) cimg_for6Y(img,y) -#define cimg_for6YC(img,y,c) cimg_for6C(img,c) cimg_for6Y(img,y) -#define cimg_for6ZC(img,z,c) cimg_for6C(img,c) cimg_for6Z(img,z) -#define cimg_for6XYZ(img,x,y,z) cimg_for6Z(img,z) cimg_for6XY(img,x,y) -#define cimg_for6XZC(img,x,z,c) cimg_for6C(img,c) cimg_for6XZ(img,x,z) -#define cimg_for6YZC(img,y,z,c) cimg_for6C(img,c) cimg_for6YZ(img,y,z) -#define cimg_for6XYZC(img,x,y,z,c) cimg_for6C(img,c) cimg_for6XYZ(img,x,y,z) - -#define cimg_for_in6(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p2##i = i-2<0?0:i-2, \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \ - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \ - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3; \ - i<=(int)(i1) && \ - (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \ - _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i) -#define cimg_for_in6X(img,x0,x1,x) cimg_for_in6((img)._width,x0,x1,x) -#define cimg_for_in6Y(img,y0,y1,y) cimg_for_in6((img)._height,y0,y1,y) -#define cimg_for_in6Z(img,z0,z1,z) cimg_for_in6((img)._depth,z0,z1,z) -#define cimg_for_in6C(img,c0,c1,c) cimg_for_in6((img)._spectrum,c0,c1,c) -#define cimg_for_in6XY(img,x0,y0,x1,y1,x,y) cimg_for_in6Y(img,y0,y1,y) cimg_for_in6X(img,x0,x1,x) -#define cimg_for_in6XZ(img,x0,z0,x1,z1,x,z) cimg_for_in6Z(img,z0,z1,z) cimg_for_in6X(img,x0,x1,x) -#define cimg_for_in6XC(img,x0,c0,x1,c1,x,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6X(img,x0,x1,x) -#define cimg_for_in6YZ(img,y0,z0,y1,z1,y,z) cimg_for_in6Z(img,z0,z1,z) cimg_for_in6Y(img,y0,y1,y) -#define cimg_for_in6YC(img,y0,c0,y1,c1,y,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6Y(img,y0,y1,y) -#define cimg_for_in6ZC(img,z0,c0,z1,c1,z,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6Z(img,z0,z1,z) -#define cimg_for_in6XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in6Z(img,z0,z1,z) cimg_for_in6XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in6XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in6YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in6XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in6C(img,c0,c1,c) cimg_for_in6XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for7(bound,i) \ - for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1, \ - _n2##i = 2>=(bound)?(int)(bound)-1:2, \ - _n3##i = 3>=(bound)?(int)(bound)-1:3; \ - _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \ - _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i) -#define cimg_for7X(img,x) cimg_for7((img)._width,x) -#define cimg_for7Y(img,y) cimg_for7((img)._height,y) -#define cimg_for7Z(img,z) cimg_for7((img)._depth,z) -#define cimg_for7C(img,c) cimg_for7((img)._spectrum,c) -#define cimg_for7XY(img,x,y) cimg_for7Y(img,y) cimg_for7X(img,x) -#define cimg_for7XZ(img,x,z) cimg_for7Z(img,z) cimg_for7X(img,x) -#define cimg_for7XC(img,x,c) cimg_for7C(img,c) cimg_for7X(img,x) -#define cimg_for7YZ(img,y,z) cimg_for7Z(img,z) cimg_for7Y(img,y) -#define cimg_for7YC(img,y,c) cimg_for7C(img,c) cimg_for7Y(img,y) -#define cimg_for7ZC(img,z,c) cimg_for7C(img,c) cimg_for7Z(img,z) -#define cimg_for7XYZ(img,x,y,z) cimg_for7Z(img,z) cimg_for7XY(img,x,y) -#define cimg_for7XZC(img,x,z,c) cimg_for7C(img,c) cimg_for7XZ(img,x,z) -#define cimg_for7YZC(img,y,z,c) cimg_for7C(img,c) cimg_for7YZ(img,y,z) -#define cimg_for7XYZC(img,x,y,z,c) cimg_for7C(img,c) cimg_for7XYZ(img,x,y,z) - -#define cimg_for_in7(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p3##i = i-3<0?0:i-3, \ - _p2##i = i-2<0?0:i-2, \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \ - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \ - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3; \ - i<=(int)(i1) && \ - (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \ - _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i) -#define cimg_for_in7X(img,x0,x1,x) cimg_for_in7((img)._width,x0,x1,x) -#define cimg_for_in7Y(img,y0,y1,y) cimg_for_in7((img)._height,y0,y1,y) -#define cimg_for_in7Z(img,z0,z1,z) cimg_for_in7((img)._depth,z0,z1,z) -#define cimg_for_in7C(img,c0,c1,c) cimg_for_in7((img)._spectrum,c0,c1,c) -#define cimg_for_in7XY(img,x0,y0,x1,y1,x,y) cimg_for_in7Y(img,y0,y1,y) cimg_for_in7X(img,x0,x1,x) -#define cimg_for_in7XZ(img,x0,z0,x1,z1,x,z) cimg_for_in7Z(img,z0,z1,z) cimg_for_in7X(img,x0,x1,x) -#define cimg_for_in7XC(img,x0,c0,x1,c1,x,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7X(img,x0,x1,x) -#define cimg_for_in7YZ(img,y0,z0,y1,z1,y,z) cimg_for_in7Z(img,z0,z1,z) cimg_for_in7Y(img,y0,y1,y) -#define cimg_for_in7YC(img,y0,c0,y1,c1,y,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7Y(img,y0,y1,y) -#define cimg_for_in7ZC(img,z0,c0,z1,c1,z,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7Z(img,z0,z1,z) -#define cimg_for_in7XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in7Z(img,z0,z1,z) cimg_for_in7XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in7XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in7YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in7XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in7C(img,c0,c1,c) cimg_for_in7XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for8(bound,i) \ - for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1, \ - _n2##i = 2>=(bound)?(int)(bound)-1:2, \ - _n3##i = 3>=(bound)?(int)(bound)-1:3, \ - _n4##i = 4>=(bound)?(int)(bound)-1:4; \ - _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \ - i==(_n4##i = _n3##i = _n2##i = --_n1##i); \ - _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i) -#define cimg_for8X(img,x) cimg_for8((img)._width,x) -#define cimg_for8Y(img,y) cimg_for8((img)._height,y) -#define cimg_for8Z(img,z) cimg_for8((img)._depth,z) -#define cimg_for8C(img,c) cimg_for8((img)._spectrum,c) -#define cimg_for8XY(img,x,y) cimg_for8Y(img,y) cimg_for8X(img,x) -#define cimg_for8XZ(img,x,z) cimg_for8Z(img,z) cimg_for8X(img,x) -#define cimg_for8XC(img,x,c) cimg_for8C(img,c) cimg_for8X(img,x) -#define cimg_for8YZ(img,y,z) cimg_for8Z(img,z) cimg_for8Y(img,y) -#define cimg_for8YC(img,y,c) cimg_for8C(img,c) cimg_for8Y(img,y) -#define cimg_for8ZC(img,z,c) cimg_for8C(img,c) cimg_for8Z(img,z) -#define cimg_for8XYZ(img,x,y,z) cimg_for8Z(img,z) cimg_for8XY(img,x,y) -#define cimg_for8XZC(img,x,z,c) cimg_for8C(img,c) cimg_for8XZ(img,x,z) -#define cimg_for8YZC(img,y,z,c) cimg_for8C(img,c) cimg_for8YZ(img,y,z) -#define cimg_for8XYZC(img,x,y,z,c) cimg_for8C(img,c) cimg_for8XYZ(img,x,y,z) - -#define cimg_for_in8(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p3##i = i-3<0?0:i-3, \ - _p2##i = i-2<0?0:i-2, \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \ - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \ - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3, \ - _n4##i = i+4>=(int)(bound)?(int)(bound)-1:i+4; \ - i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \ - i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \ - _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i) -#define cimg_for_in8X(img,x0,x1,x) cimg_for_in8((img)._width,x0,x1,x) -#define cimg_for_in8Y(img,y0,y1,y) cimg_for_in8((img)._height,y0,y1,y) -#define cimg_for_in8Z(img,z0,z1,z) cimg_for_in8((img)._depth,z0,z1,z) -#define cimg_for_in8C(img,c0,c1,c) cimg_for_in8((img)._spectrum,c0,c1,c) -#define cimg_for_in8XY(img,x0,y0,x1,y1,x,y) cimg_for_in8Y(img,y0,y1,y) cimg_for_in8X(img,x0,x1,x) -#define cimg_for_in8XZ(img,x0,z0,x1,z1,x,z) cimg_for_in8Z(img,z0,z1,z) cimg_for_in8X(img,x0,x1,x) -#define cimg_for_in8XC(img,x0,c0,x1,c1,x,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8X(img,x0,x1,x) -#define cimg_for_in8YZ(img,y0,z0,y1,z1,y,z) cimg_for_in8Z(img,z0,z1,z) cimg_for_in8Y(img,y0,y1,y) -#define cimg_for_in8YC(img,y0,c0,y1,c1,y,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8Y(img,y0,y1,y) -#define cimg_for_in8ZC(img,z0,c0,z1,c1,z,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8Z(img,z0,z1,z) -#define cimg_for_in8XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in8Z(img,z0,z1,z) cimg_for_in8XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in8XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in8YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in8XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in8C(img,c0,c1,c) cimg_for_in8XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for9(bound,i) \ - for (int i = 0, _p4##i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \ - _n1##i = 1>=(int)(bound)?(int)(bound)-1:1, \ - _n2##i = 2>=(int)(bound)?(int)(bound)-1:2, \ - _n3##i = 3>=(int)(bound)?(int)(bound)-1:3, \ - _n4##i = 4>=(int)(bound)?(int)(bound)-1:4; \ - _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \ - i==(_n4##i = _n3##i = _n2##i = --_n1##i); \ - _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i) -#define cimg_for9X(img,x) cimg_for9((img)._width,x) -#define cimg_for9Y(img,y) cimg_for9((img)._height,y) -#define cimg_for9Z(img,z) cimg_for9((img)._depth,z) -#define cimg_for9C(img,c) cimg_for9((img)._spectrum,c) -#define cimg_for9XY(img,x,y) cimg_for9Y(img,y) cimg_for9X(img,x) -#define cimg_for9XZ(img,x,z) cimg_for9Z(img,z) cimg_for9X(img,x) -#define cimg_for9XC(img,x,c) cimg_for9C(img,c) cimg_for9X(img,x) -#define cimg_for9YZ(img,y,z) cimg_for9Z(img,z) cimg_for9Y(img,y) -#define cimg_for9YC(img,y,c) cimg_for9C(img,c) cimg_for9Y(img,y) -#define cimg_for9ZC(img,z,c) cimg_for9C(img,c) cimg_for9Z(img,z) -#define cimg_for9XYZ(img,x,y,z) cimg_for9Z(img,z) cimg_for9XY(img,x,y) -#define cimg_for9XZC(img,x,z,c) cimg_for9C(img,c) cimg_for9XZ(img,x,z) -#define cimg_for9YZC(img,y,z,c) cimg_for9C(img,c) cimg_for9YZ(img,y,z) -#define cimg_for9XYZC(img,x,y,z,c) cimg_for9C(img,c) cimg_for9XYZ(img,x,y,z) - -#define cimg_for_in9(bound,i0,i1,i) \ - for (int i = (int)(i0)<0?0:(int)(i0), \ - _p4##i = i-4<0?0:i-4, \ - _p3##i = i-3<0?0:i-3, \ - _p2##i = i-2<0?0:i-2, \ - _p1##i = i-1<0?0:i-1, \ - _n1##i = i+1>=(int)(bound)?(int)(bound)-1:i+1, \ - _n2##i = i+2>=(int)(bound)?(int)(bound)-1:i+2, \ - _n3##i = i+3>=(int)(bound)?(int)(bound)-1:i+3, \ - _n4##i = i+4>=(int)(bound)?(int)(bound)-1:i+4; \ - i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \ - i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \ - _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i) -#define cimg_for_in9X(img,x0,x1,x) cimg_for_in9((img)._width,x0,x1,x) -#define cimg_for_in9Y(img,y0,y1,y) cimg_for_in9((img)._height,y0,y1,y) -#define cimg_for_in9Z(img,z0,z1,z) cimg_for_in9((img)._depth,z0,z1,z) -#define cimg_for_in9C(img,c0,c1,c) cimg_for_in9((img)._spectrum,c0,c1,c) -#define cimg_for_in9XY(img,x0,y0,x1,y1,x,y) cimg_for_in9Y(img,y0,y1,y) cimg_for_in9X(img,x0,x1,x) -#define cimg_for_in9XZ(img,x0,z0,x1,z1,x,z) cimg_for_in9Z(img,z0,z1,z) cimg_for_in9X(img,x0,x1,x) -#define cimg_for_in9XC(img,x0,c0,x1,c1,x,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9X(img,x0,x1,x) -#define cimg_for_in9YZ(img,y0,z0,y1,z1,y,z) cimg_for_in9Z(img,z0,z1,z) cimg_for_in9Y(img,y0,y1,y) -#define cimg_for_in9YC(img,y0,c0,y1,c1,y,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9Y(img,y0,y1,y) -#define cimg_for_in9ZC(img,z0,c0,z1,c1,z,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9Z(img,z0,z1,z) -#define cimg_for_in9XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in9Z(img,z0,z1,z) cimg_for_in9XY(img,x0,y0,x1,y1,x,y) -#define cimg_for_in9XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9XZ(img,x0,y0,x1,y1,x,z) -#define cimg_for_in9YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9YZ(img,y0,z0,y1,z1,y,z) -#define cimg_for_in9XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \ - cimg_for_in9C(img,c0,c1,c) cimg_for_in9XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) - -#define cimg_for2x2(img,x,y,z,c,I,T) \ - cimg_for2((img)._height,y) for (int x = 0, \ - _n1##x = (int)( \ - (I[0] = (T)(img)(0,y,z,c)), \ - (I[2] = (T)(img)(0,_n1##y,z,c)), \ - 1>=(img)._width?(img).width()-1:1); \ - (_n1##x<(img).width() && ( \ - (I[1] = (T)(img)(_n1##x,y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ - x==--_n1##x; \ - I[0] = I[1], \ - I[2] = I[3], \ - ++x, ++_n1##x) - -#define cimg_for_in2x2(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in2((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _n1##x = (int)( \ - (I[0] = (T)(img)(x,y,z,c)), \ - (I[2] = (T)(img)(x,_n1##y,z,c)), \ - x+1>=(int)(img)._width?(img).width()-1:x+1); \ - x<=(int)(x1) && ((_n1##x<(img).width() && ( \ - (I[1] = (T)(img)(_n1##x,y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ - x==--_n1##x); \ - I[0] = I[1], \ - I[2] = I[3], \ - ++x, ++_n1##x) - -#define cimg_for3x3(img,x,y,z,c,I,T) \ - cimg_for3((img)._height,y) for (int x = 0, \ - _p1##x = 0, \ - _n1##x = (int)( \ - (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[3] = I[4] = (T)(img)(0,y,z,c)), \ - (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \ - 1>=(img)._width?(img).width()-1:1); \ - (_n1##x<(img).width() && ( \ - (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,y,z,c)), \ - (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ - x==--_n1##x; \ - I[0] = I[1], I[1] = I[2], \ - I[3] = I[4], I[4] = I[5], \ - I[6] = I[7], I[7] = I[8], \ - _p1##x = x++, ++_n1##x) - -#define cimg_for_in3x3(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in3((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = (int)( \ - (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[3] = (T)(img)(_p1##x,y,z,c)), \ - (I[6] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[1] = (T)(img)(x,_p1##y,z,c)), \ - (I[4] = (T)(img)(x,y,z,c)), \ - (I[7] = (T)(img)(x,_n1##y,z,c)), \ - x+1>=(int)(img)._width?(img).width()-1:x+1); \ - x<=(int)(x1) && ((_n1##x<(img).width() && ( \ - (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,y,z,c)), \ - (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ - x==--_n1##x); \ - I[0] = I[1], I[1] = I[2], \ - I[3] = I[4], I[4] = I[5], \ - I[6] = I[7], I[7] = I[8], \ - _p1##x = x++, ++_n1##x) - -#define cimg_for4x4(img,x,y,z,c,I,T) \ - cimg_for4((img)._height,y) for (int x = 0, \ - _p1##x = 0, \ - _n1##x = 1>=(img)._width?(img).width()-1:1, \ - _n2##x = (int)( \ - (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[4] = I[5] = (T)(img)(0,y,z,c)), \ - (I[8] = I[9] = (T)(img)(0,_n1##y,z,c)), \ - (I[12] = I[13] = (T)(img)(0,_n2##y,z,c)), \ - (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[6] = (T)(img)(_n1##x,y,z,c)), \ - (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \ - 2>=(img)._width?(img).width()-1:2); \ - (_n2##x<(img).width() && ( \ - (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[7] = (T)(img)(_n2##x,y,z,c)), \ - (I[11] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[15] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \ - _n1##x==--_n2##x || x==(_n2##x = --_n1##x); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], \ - I[4] = I[5], I[5] = I[6], I[6] = I[7], \ - I[8] = I[9], I[9] = I[10], I[10] = I[11], \ - I[12] = I[13], I[13] = I[14], I[14] = I[15], \ - _p1##x = x++, ++_n1##x, ++_n2##x) - -#define cimg_for_in4x4(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in4((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \ - _n2##x = (int)( \ - (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[4] = (T)(img)(_p1##x,y,z,c)), \ - (I[8] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[12] = (T)(img)(_p1##x,_n2##y,z,c)), \ - (I[1] = (T)(img)(x,_p1##y,z,c)), \ - (I[5] = (T)(img)(x,y,z,c)), \ - (I[9] = (T)(img)(x,_n1##y,z,c)), \ - (I[13] = (T)(img)(x,_n2##y,z,c)), \ - (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[6] = (T)(img)(_n1##x,y,z,c)), \ - (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \ - x+2>=(int)(img)._width?(img).width()-1:x+2); \ - x<=(int)(x1) && ((_n2##x<(img).width() && ( \ - (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[7] = (T)(img)(_n2##x,y,z,c)), \ - (I[11] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[15] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \ - _n1##x==--_n2##x || x==(_n2##x = --_n1##x)); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], \ - I[4] = I[5], I[5] = I[6], I[6] = I[7], \ - I[8] = I[9], I[9] = I[10], I[10] = I[11], \ - I[12] = I[13], I[13] = I[14], I[14] = I[15], \ - _p1##x = x++, ++_n1##x, ++_n2##x) - -#define cimg_for5x5(img,x,y,z,c,I,T) \ - cimg_for5((img)._height,y) for (int x = 0, \ - _p2##x = 0, _p1##x = 0, \ - _n1##x = 1>=(img)._width?(img).width()-1:1, \ - _n2##x = (int)( \ - (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[5] = I[6] = I[7] = (T)(img)(0,_p1##y,z,c)), \ - (I[10] = I[11] = I[12] = (T)(img)(0,y,z,c)), \ - (I[15] = I[16] = I[17] = (T)(img)(0,_n1##y,z,c)), \ - (I[20] = I[21] = I[22] = (T)(img)(0,_n2##y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[8] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[13] = (T)(img)(_n1##x,y,z,c)), \ - (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \ - 2>=(img)._width?(img).width()-1:2); \ - (_n2##x<(img).width() && ( \ - (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[14] = (T)(img)(_n2##x,y,z,c)), \ - (I[19] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[24] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \ - _n1##x==--_n2##x || x==(_n2##x = --_n1##x); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], \ - I[5] = I[6], I[6] = I[7], I[7] = I[8], I[8] = I[9], \ - I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], \ - I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], \ - I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \ - _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x) - -#define cimg_for_in5x5(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in5((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _p2##x = x-2<0?0:x-2, \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \ - _n2##x = (int)( \ - (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[5] = (T)(img)(_p2##x,_p1##y,z,c)), \ - (I[10] = (T)(img)(_p2##x,y,z,c)), \ - (I[15] = (T)(img)(_p2##x,_n1##y,z,c)), \ - (I[20] = (T)(img)(_p2##x,_n2##y,z,c)), \ - (I[1] = (T)(img)(_p1##x,_p2##y,z,c)), \ - (I[6] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[11] = (T)(img)(_p1##x,y,z,c)), \ - (I[16] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[21] = (T)(img)(_p1##x,_n2##y,z,c)), \ - (I[2] = (T)(img)(x,_p2##y,z,c)), \ - (I[7] = (T)(img)(x,_p1##y,z,c)), \ - (I[12] = (T)(img)(x,y,z,c)), \ - (I[17] = (T)(img)(x,_n1##y,z,c)), \ - (I[22] = (T)(img)(x,_n2##y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[8] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[13] = (T)(img)(_n1##x,y,z,c)), \ - (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \ - x+2>=(int)(img)._width?(img).width()-1:x+2); \ - x<=(int)(x1) && ((_n2##x<(img).width() && ( \ - (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[14] = (T)(img)(_n2##x,y,z,c)), \ - (I[19] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[24] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \ - _n1##x==--_n2##x || x==(_n2##x = --_n1##x)); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], \ - I[5] = I[6], I[6] = I[7], I[7] = I[8], I[8] = I[9], \ - I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], \ - I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], \ - I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \ - _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x) - -#define cimg_for6x6(img,x,y,z,c,I,T) \ - cimg_for6((img)._height,y) for (int x = 0, \ - _p2##x = 0, _p1##x = 0, \ - _n1##x = 1>=(img)._width?(img).width()-1:1, \ - _n2##x = 2>=(img)._width?(img).width()-1:2, \ - _n3##x = (int)( \ - (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[6] = I[7] = I[8] = (T)(img)(0,_p1##y,z,c)), \ - (I[12] = I[13] = I[14] = (T)(img)(0,y,z,c)), \ - (I[18] = I[19] = I[20] = (T)(img)(0,_n1##y,z,c)), \ - (I[24] = I[25] = I[26] = (T)(img)(0,_n2##y,z,c)), \ - (I[30] = I[31] = I[32] = (T)(img)(0,_n3##y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[9] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[15] = (T)(img)(_n1##x,y,z,c)), \ - (I[21] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[27] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[33] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[10] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[16] = (T)(img)(_n2##x,y,z,c)), \ - (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \ - 3>=(img)._width?(img).width()-1:3); \ - (_n3##x<(img).width() && ( \ - (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[17] = (T)(img)(_n3##x,y,z,c)), \ - (I[23] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[29] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[35] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \ - _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3## x = _n2##x = --_n1##x); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], \ - I[6] = I[7], I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], \ - I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], I[16] = I[17], \ - I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \ - I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], \ - I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], I[34] = I[35], \ - _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x) - -#define cimg_for_in6x6(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in6((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)x0, \ - _p2##x = x-2<0?0:x-2, \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \ - _n2##x = x+2>=(int)(img)._width?(img).width()-1:x+2, \ - _n3##x = (int)( \ - (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[6] = (T)(img)(_p2##x,_p1##y,z,c)), \ - (I[12] = (T)(img)(_p2##x,y,z,c)), \ - (I[18] = (T)(img)(_p2##x,_n1##y,z,c)), \ - (I[24] = (T)(img)(_p2##x,_n2##y,z,c)), \ - (I[30] = (T)(img)(_p2##x,_n3##y,z,c)), \ - (I[1] = (T)(img)(_p1##x,_p2##y,z,c)), \ - (I[7] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[13] = (T)(img)(_p1##x,y,z,c)), \ - (I[19] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[25] = (T)(img)(_p1##x,_n2##y,z,c)), \ - (I[31] = (T)(img)(_p1##x,_n3##y,z,c)), \ - (I[2] = (T)(img)(x,_p2##y,z,c)), \ - (I[8] = (T)(img)(x,_p1##y,z,c)), \ - (I[14] = (T)(img)(x,y,z,c)), \ - (I[20] = (T)(img)(x,_n1##y,z,c)), \ - (I[26] = (T)(img)(x,_n2##y,z,c)), \ - (I[32] = (T)(img)(x,_n3##y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[9] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[15] = (T)(img)(_n1##x,y,z,c)), \ - (I[21] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[27] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[33] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[10] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[16] = (T)(img)(_n2##x,y,z,c)), \ - (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \ - x+3>=(int)(img)._width?(img).width()-1:x+3); \ - x<=(int)(x1) && ((_n3##x<(img).width() && ( \ - (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[17] = (T)(img)(_n3##x,y,z,c)), \ - (I[23] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[29] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[35] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \ - _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3## x = _n2##x = --_n1##x)); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], \ - I[6] = I[7], I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], \ - I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], I[16] = I[17], \ - I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \ - I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], \ - I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], I[34] = I[35], \ - _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x) - -#define cimg_for7x7(img,x,y,z,c,I,T) \ - cimg_for7((img)._height,y) for (int x = 0, \ - _p3##x = 0, _p2##x = 0, _p1##x = 0, \ - _n1##x = 1>=(img)._width?(img).width()-1:1, \ - _n2##x = 2>=(img)._width?(img).width()-1:2, \ - _n3##x = (int)( \ - (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \ - (I[7] = I[8] = I[9] = I[10] = (T)(img)(0,_p2##y,z,c)), \ - (I[14] = I[15] = I[16] = I[17] = (T)(img)(0,_p1##y,z,c)), \ - (I[21] = I[22] = I[23] = I[24] = (T)(img)(0,y,z,c)), \ - (I[28] = I[29] = I[30] = I[31] = (T)(img)(0,_n1##y,z,c)), \ - (I[35] = I[36] = I[37] = I[38] = (T)(img)(0,_n2##y,z,c)), \ - (I[42] = I[43] = I[44] = I[45] = (T)(img)(0,_n3##y,z,c)), \ - (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \ - (I[11] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[18] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[25] = (T)(img)(_n1##x,y,z,c)), \ - (I[32] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[39] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[46] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \ - (I[12] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[19] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[26] = (T)(img)(_n2##x,y,z,c)), \ - (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \ - 3>=(img)._width?(img).width()-1:3); \ - (_n3##x<(img).width() && ( \ - (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \ - (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[20] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[27] = (T)(img)(_n3##x,y,z,c)), \ - (I[34] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[41] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[48] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \ - _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3##x = _n2##x = --_n1##x); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], \ - I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], \ - I[14] = I[15], I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], \ - I[21] = I[22], I[22] = I[23], I[23] = I[24], I[24] = I[25], I[25] = I[26], I[26] = I[27], \ - I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], \ - I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], I[40] = I[41], \ - I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], I[47] = I[48], \ - _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x) - -#define cimg_for_in7x7(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in7((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _p3##x = x-3<0?0:x-3, \ - _p2##x = x-2<0?0:x-2, \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = x+1>=(int)(img)._width?(img).width()-1:x+1, \ - _n2##x = x+2>=(int)(img)._width?(img).width()-1:x+2, \ - _n3##x = (int)( \ - (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \ - (I[7] = (T)(img)(_p3##x,_p2##y,z,c)), \ - (I[14] = (T)(img)(_p3##x,_p1##y,z,c)), \ - (I[21] = (T)(img)(_p3##x,y,z,c)), \ - (I[28] = (T)(img)(_p3##x,_n1##y,z,c)), \ - (I[35] = (T)(img)(_p3##x,_n2##y,z,c)), \ - (I[42] = (T)(img)(_p3##x,_n3##y,z,c)), \ - (I[1] = (T)(img)(_p2##x,_p3##y,z,c)), \ - (I[8] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[15] = (T)(img)(_p2##x,_p1##y,z,c)), \ - (I[22] = (T)(img)(_p2##x,y,z,c)), \ - (I[29] = (T)(img)(_p2##x,_n1##y,z,c)), \ - (I[36] = (T)(img)(_p2##x,_n2##y,z,c)), \ - (I[43] = (T)(img)(_p2##x,_n3##y,z,c)), \ - (I[2] = (T)(img)(_p1##x,_p3##y,z,c)), \ - (I[9] = (T)(img)(_p1##x,_p2##y,z,c)), \ - (I[16] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[23] = (T)(img)(_p1##x,y,z,c)), \ - (I[30] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[37] = (T)(img)(_p1##x,_n2##y,z,c)), \ - (I[44] = (T)(img)(_p1##x,_n3##y,z,c)), \ - (I[3] = (T)(img)(x,_p3##y,z,c)), \ - (I[10] = (T)(img)(x,_p2##y,z,c)), \ - (I[17] = (T)(img)(x,_p1##y,z,c)), \ - (I[24] = (T)(img)(x,y,z,c)), \ - (I[31] = (T)(img)(x,_n1##y,z,c)), \ - (I[38] = (T)(img)(x,_n2##y,z,c)), \ - (I[45] = (T)(img)(x,_n3##y,z,c)), \ - (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \ - (I[11] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[18] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[25] = (T)(img)(_n1##x,y,z,c)), \ - (I[32] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[39] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[46] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \ - (I[12] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[19] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[26] = (T)(img)(_n2##x,y,z,c)), \ - (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \ - x+3>=(int)(img)._width?(img).width()-1:x+3); \ - x<=(int)(x1) && ((_n3##x<(img).width() && ( \ - (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \ - (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[20] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[27] = (T)(img)(_n3##x,y,z,c)), \ - (I[34] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[41] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[48] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \ - _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3##x = _n2##x = --_n1##x)); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], \ - I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], \ - I[14] = I[15], I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], \ - I[21] = I[22], I[22] = I[23], I[23] = I[24], I[24] = I[25], I[25] = I[26], I[26] = I[27], \ - I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], \ - I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], I[40] = I[41], \ - I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], I[47] = I[48], \ - _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x) - -#define cimg_for8x8(img,x,y,z,c,I,T) \ - cimg_for8((img)._height,y) for (int x = 0, \ - _p3##x = 0, _p2##x = 0, _p1##x = 0, \ - _n1##x = 1>=((img)._width)?(img).width()-1:1, \ - _n2##x = 2>=((img)._width)?(img).width()-1:2, \ - _n3##x = 3>=((img)._width)?(img).width()-1:3, \ - _n4##x = (int)( \ - (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \ - (I[8] = I[9] = I[10] = I[11] = (T)(img)(0,_p2##y,z,c)), \ - (I[16] = I[17] = I[18] = I[19] = (T)(img)(0,_p1##y,z,c)), \ - (I[24] = I[25] = I[26] = I[27] = (T)(img)(0,y,z,c)), \ - (I[32] = I[33] = I[34] = I[35] = (T)(img)(0,_n1##y,z,c)), \ - (I[40] = I[41] = I[42] = I[43] = (T)(img)(0,_n2##y,z,c)), \ - (I[48] = I[49] = I[50] = I[51] = (T)(img)(0,_n3##y,z,c)), \ - (I[56] = I[57] = I[58] = I[59] = (T)(img)(0,_n4##y,z,c)), \ - (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \ - (I[12] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[20] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[28] = (T)(img)(_n1##x,y,z,c)), \ - (I[36] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[44] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[52] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[60] = (T)(img)(_n1##x,_n4##y,z,c)), \ - (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \ - (I[13] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[21] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[29] = (T)(img)(_n2##x,y,z,c)), \ - (I[37] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[45] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[53] = (T)(img)(_n2##x,_n3##y,z,c)), \ - (I[61] = (T)(img)(_n2##x,_n4##y,z,c)), \ - (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \ - (I[14] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[22] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[30] = (T)(img)(_n3##x,y,z,c)), \ - (I[38] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \ - (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \ - 4>=((img)._width)?(img).width()-1:4); \ - (_n4##x<(img).width() && ( \ - (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \ - (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \ - (I[23] = (T)(img)(_n4##x,_p1##y,z,c)), \ - (I[31] = (T)(img)(_n4##x,y,z,c)), \ - (I[39] = (T)(img)(_n4##x,_n1##y,z,c)), \ - (I[47] = (T)(img)(_n4##x,_n2##y,z,c)), \ - (I[55] = (T)(img)(_n4##x,_n3##y,z,c)), \ - (I[63] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \ - _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], \ - I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], \ - I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \ - I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], \ - I[32] = I[33], I[33] = I[34], I[34] = I[35], I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], \ - I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], \ - I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[53] = I[54], I[54] = I[55], \ - I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[62] = I[63], \ - _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x) - -#define cimg_for_in8x8(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in8((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _p3##x = x-3<0?0:x-3, \ - _p2##x = x-2<0?0:x-2, \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = x+1>=(img).width()?(img).width()-1:x+1, \ - _n2##x = x+2>=(img).width()?(img).width()-1:x+2, \ - _n3##x = x+3>=(img).width()?(img).width()-1:x+3, \ - _n4##x = (int)( \ - (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \ - (I[8] = (T)(img)(_p3##x,_p2##y,z,c)), \ - (I[16] = (T)(img)(_p3##x,_p1##y,z,c)), \ - (I[24] = (T)(img)(_p3##x,y,z,c)), \ - (I[32] = (T)(img)(_p3##x,_n1##y,z,c)), \ - (I[40] = (T)(img)(_p3##x,_n2##y,z,c)), \ - (I[48] = (T)(img)(_p3##x,_n3##y,z,c)), \ - (I[56] = (T)(img)(_p3##x,_n4##y,z,c)), \ - (I[1] = (T)(img)(_p2##x,_p3##y,z,c)), \ - (I[9] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[17] = (T)(img)(_p2##x,_p1##y,z,c)), \ - (I[25] = (T)(img)(_p2##x,y,z,c)), \ - (I[33] = (T)(img)(_p2##x,_n1##y,z,c)), \ - (I[41] = (T)(img)(_p2##x,_n2##y,z,c)), \ - (I[49] = (T)(img)(_p2##x,_n3##y,z,c)), \ - (I[57] = (T)(img)(_p2##x,_n4##y,z,c)), \ - (I[2] = (T)(img)(_p1##x,_p3##y,z,c)), \ - (I[10] = (T)(img)(_p1##x,_p2##y,z,c)), \ - (I[18] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[26] = (T)(img)(_p1##x,y,z,c)), \ - (I[34] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[42] = (T)(img)(_p1##x,_n2##y,z,c)), \ - (I[50] = (T)(img)(_p1##x,_n3##y,z,c)), \ - (I[58] = (T)(img)(_p1##x,_n4##y,z,c)), \ - (I[3] = (T)(img)(x,_p3##y,z,c)), \ - (I[11] = (T)(img)(x,_p2##y,z,c)), \ - (I[19] = (T)(img)(x,_p1##y,z,c)), \ - (I[27] = (T)(img)(x,y,z,c)), \ - (I[35] = (T)(img)(x,_n1##y,z,c)), \ - (I[43] = (T)(img)(x,_n2##y,z,c)), \ - (I[51] = (T)(img)(x,_n3##y,z,c)), \ - (I[59] = (T)(img)(x,_n4##y,z,c)), \ - (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \ - (I[12] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[20] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[28] = (T)(img)(_n1##x,y,z,c)), \ - (I[36] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[44] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[52] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[60] = (T)(img)(_n1##x,_n4##y,z,c)), \ - (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \ - (I[13] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[21] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[29] = (T)(img)(_n2##x,y,z,c)), \ - (I[37] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[45] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[53] = (T)(img)(_n2##x,_n3##y,z,c)), \ - (I[61] = (T)(img)(_n2##x,_n4##y,z,c)), \ - (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \ - (I[14] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[22] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[30] = (T)(img)(_n3##x,y,z,c)), \ - (I[38] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \ - (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \ - x+4>=(img).width()?(img).width()-1:x+4); \ - x<=(int)(x1) && ((_n4##x<(img).width() && ( \ - (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \ - (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \ - (I[23] = (T)(img)(_n4##x,_p1##y,z,c)), \ - (I[31] = (T)(img)(_n4##x,y,z,c)), \ - (I[39] = (T)(img)(_n4##x,_n1##y,z,c)), \ - (I[47] = (T)(img)(_n4##x,_n2##y,z,c)), \ - (I[55] = (T)(img)(_n4##x,_n3##y,z,c)), \ - (I[63] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \ - _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x)); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], \ - I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], \ - I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \ - I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], \ - I[32] = I[33], I[33] = I[34], I[34] = I[35], I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], \ - I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], \ - I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[53] = I[54], I[54] = I[55], \ - I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[62] = I[63], \ - _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x) - -#define cimg_for9x9(img,x,y,z,c,I,T) \ - cimg_for9((img)._height,y) for (int x = 0, \ - _p4##x = 0, _p3##x = 0, _p2##x = 0, _p1##x = 0, \ - _n1##x = 1>=((img)._width)?(img).width()-1:1, \ - _n2##x = 2>=((img)._width)?(img).width()-1:2, \ - _n3##x = 3>=((img)._width)?(img).width()-1:3, \ - _n4##x = (int)( \ - (I[0] = I[1] = I[2] = I[3] = I[4] = (T)(img)(_p4##x,_p4##y,z,c)), \ - (I[9] = I[10] = I[11] = I[12] = I[13] = (T)(img)(0,_p3##y,z,c)), \ - (I[18] = I[19] = I[20] = I[21] = I[22] = (T)(img)(0,_p2##y,z,c)), \ - (I[27] = I[28] = I[29] = I[30] = I[31] = (T)(img)(0,_p1##y,z,c)), \ - (I[36] = I[37] = I[38] = I[39] = I[40] = (T)(img)(0,y,z,c)), \ - (I[45] = I[46] = I[47] = I[48] = I[49] = (T)(img)(0,_n1##y,z,c)), \ - (I[54] = I[55] = I[56] = I[57] = I[58] = (T)(img)(0,_n2##y,z,c)), \ - (I[63] = I[64] = I[65] = I[66] = I[67] = (T)(img)(0,_n3##y,z,c)), \ - (I[72] = I[73] = I[74] = I[75] = I[76] = (T)(img)(0,_n4##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,_p4##y,z,c)), \ - (I[14] = (T)(img)(_n1##x,_p3##y,z,c)), \ - (I[23] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[32] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[41] = (T)(img)(_n1##x,y,z,c)), \ - (I[50] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[59] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[68] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[77] = (T)(img)(_n1##x,_n4##y,z,c)), \ - (I[6] = (T)(img)(_n2##x,_p4##y,z,c)), \ - (I[15] = (T)(img)(_n2##x,_p3##y,z,c)), \ - (I[24] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[33] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[42] = (T)(img)(_n2##x,y,z,c)), \ - (I[51] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[60] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[69] = (T)(img)(_n2##x,_n3##y,z,c)), \ - (I[78] = (T)(img)(_n2##x,_n4##y,z,c)), \ - (I[7] = (T)(img)(_n3##x,_p4##y,z,c)), \ - (I[16] = (T)(img)(_n3##x,_p3##y,z,c)), \ - (I[25] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[34] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[43] = (T)(img)(_n3##x,y,z,c)), \ - (I[52] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \ - (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \ - 4>=((img)._width)?(img).width()-1:4); \ - (_n4##x<(img).width() && ( \ - (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \ - (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \ - (I[26] = (T)(img)(_n4##x,_p2##y,z,c)), \ - (I[35] = (T)(img)(_n4##x,_p1##y,z,c)), \ - (I[44] = (T)(img)(_n4##x,y,z,c)), \ - (I[53] = (T)(img)(_n4##x,_n1##y,z,c)), \ - (I[62] = (T)(img)(_n4##x,_n2##y,z,c)), \ - (I[71] = (T)(img)(_n4##x,_n3##y,z,c)), \ - (I[80] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \ - _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], I[7] = I[8], \ - I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], \ - I[16] = I[17], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \ - I[24] = I[25], I[25] = I[26], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], \ - I[32] = I[33], I[33] = I[34], I[34] = I[35], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], \ - I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[45] = I[46], I[46] = I[47], I[47] = I[48], \ - I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[54] = I[55], I[55] = I[56], \ - I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[63] = I[64], \ - I[64] = I[65], I[65] = I[66], I[66] = I[67], I[67] = I[68], I[68] = I[69], I[69] = I[70], I[70] = I[71], \ - I[72] = I[73], I[73] = I[74], I[74] = I[75], I[75] = I[76], I[76] = I[77], I[77] = I[78], I[78] = I[79], \ - I[79] = I[80], \ - _p4##x = _p3##x, _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x) - -#define cimg_for_in9x9(img,x0,y0,x1,y1,x,y,z,c,I,T) \ - cimg_for_in9((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _p4##x = x-4<0?0:x-4, \ - _p3##x = x-3<0?0:x-3, \ - _p2##x = x-2<0?0:x-2, \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = x+1>=(img).width()?(img).width()-1:x+1, \ - _n2##x = x+2>=(img).width()?(img).width()-1:x+2, \ - _n3##x = x+3>=(img).width()?(img).width()-1:x+3, \ - _n4##x = (int)( \ - (I[0] = (T)(img)(_p4##x,_p4##y,z,c)), \ - (I[9] = (T)(img)(_p4##x,_p3##y,z,c)), \ - (I[18] = (T)(img)(_p4##x,_p2##y,z,c)), \ - (I[27] = (T)(img)(_p4##x,_p1##y,z,c)), \ - (I[36] = (T)(img)(_p4##x,y,z,c)), \ - (I[45] = (T)(img)(_p4##x,_n1##y,z,c)), \ - (I[54] = (T)(img)(_p4##x,_n2##y,z,c)), \ - (I[63] = (T)(img)(_p4##x,_n3##y,z,c)), \ - (I[72] = (T)(img)(_p4##x,_n4##y,z,c)), \ - (I[1] = (T)(img)(_p3##x,_p4##y,z,c)), \ - (I[10] = (T)(img)(_p3##x,_p3##y,z,c)), \ - (I[19] = (T)(img)(_p3##x,_p2##y,z,c)), \ - (I[28] = (T)(img)(_p3##x,_p1##y,z,c)), \ - (I[37] = (T)(img)(_p3##x,y,z,c)), \ - (I[46] = (T)(img)(_p3##x,_n1##y,z,c)), \ - (I[55] = (T)(img)(_p3##x,_n2##y,z,c)), \ - (I[64] = (T)(img)(_p3##x,_n3##y,z,c)), \ - (I[73] = (T)(img)(_p3##x,_n4##y,z,c)), \ - (I[2] = (T)(img)(_p2##x,_p4##y,z,c)), \ - (I[11] = (T)(img)(_p2##x,_p3##y,z,c)), \ - (I[20] = (T)(img)(_p2##x,_p2##y,z,c)), \ - (I[29] = (T)(img)(_p2##x,_p1##y,z,c)), \ - (I[38] = (T)(img)(_p2##x,y,z,c)), \ - (I[47] = (T)(img)(_p2##x,_n1##y,z,c)), \ - (I[56] = (T)(img)(_p2##x,_n2##y,z,c)), \ - (I[65] = (T)(img)(_p2##x,_n3##y,z,c)), \ - (I[74] = (T)(img)(_p2##x,_n4##y,z,c)), \ - (I[3] = (T)(img)(_p1##x,_p4##y,z,c)), \ - (I[12] = (T)(img)(_p1##x,_p3##y,z,c)), \ - (I[21] = (T)(img)(_p1##x,_p2##y,z,c)), \ - (I[30] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[39] = (T)(img)(_p1##x,y,z,c)), \ - (I[48] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[57] = (T)(img)(_p1##x,_n2##y,z,c)), \ - (I[66] = (T)(img)(_p1##x,_n3##y,z,c)), \ - (I[75] = (T)(img)(_p1##x,_n4##y,z,c)), \ - (I[4] = (T)(img)(x,_p4##y,z,c)), \ - (I[13] = (T)(img)(x,_p3##y,z,c)), \ - (I[22] = (T)(img)(x,_p2##y,z,c)), \ - (I[31] = (T)(img)(x,_p1##y,z,c)), \ - (I[40] = (T)(img)(x,y,z,c)), \ - (I[49] = (T)(img)(x,_n1##y,z,c)), \ - (I[58] = (T)(img)(x,_n2##y,z,c)), \ - (I[67] = (T)(img)(x,_n3##y,z,c)), \ - (I[76] = (T)(img)(x,_n4##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,_p4##y,z,c)), \ - (I[14] = (T)(img)(_n1##x,_p3##y,z,c)), \ - (I[23] = (T)(img)(_n1##x,_p2##y,z,c)), \ - (I[32] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[41] = (T)(img)(_n1##x,y,z,c)), \ - (I[50] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[59] = (T)(img)(_n1##x,_n2##y,z,c)), \ - (I[68] = (T)(img)(_n1##x,_n3##y,z,c)), \ - (I[77] = (T)(img)(_n1##x,_n4##y,z,c)), \ - (I[6] = (T)(img)(_n2##x,_p4##y,z,c)), \ - (I[15] = (T)(img)(_n2##x,_p3##y,z,c)), \ - (I[24] = (T)(img)(_n2##x,_p2##y,z,c)), \ - (I[33] = (T)(img)(_n2##x,_p1##y,z,c)), \ - (I[42] = (T)(img)(_n2##x,y,z,c)), \ - (I[51] = (T)(img)(_n2##x,_n1##y,z,c)), \ - (I[60] = (T)(img)(_n2##x,_n2##y,z,c)), \ - (I[69] = (T)(img)(_n2##x,_n3##y,z,c)), \ - (I[78] = (T)(img)(_n2##x,_n4##y,z,c)), \ - (I[7] = (T)(img)(_n3##x,_p4##y,z,c)), \ - (I[16] = (T)(img)(_n3##x,_p3##y,z,c)), \ - (I[25] = (T)(img)(_n3##x,_p2##y,z,c)), \ - (I[34] = (T)(img)(_n3##x,_p1##y,z,c)), \ - (I[43] = (T)(img)(_n3##x,y,z,c)), \ - (I[52] = (T)(img)(_n3##x,_n1##y,z,c)), \ - (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \ - (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \ - (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \ - x+4>=(img).width()?(img).width()-1:x+4); \ - x<=(int)(x1) && ((_n4##x<(img).width() && ( \ - (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \ - (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \ - (I[26] = (T)(img)(_n4##x,_p2##y,z,c)), \ - (I[35] = (T)(img)(_n4##x,_p1##y,z,c)), \ - (I[44] = (T)(img)(_n4##x,y,z,c)), \ - (I[53] = (T)(img)(_n4##x,_n1##y,z,c)), \ - (I[62] = (T)(img)(_n4##x,_n2##y,z,c)), \ - (I[71] = (T)(img)(_n4##x,_n3##y,z,c)), \ - (I[80] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \ - _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x)); \ - I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], I[7] = I[8], \ - I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], \ - I[16] = I[17], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \ - I[24] = I[25], I[25] = I[26], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], \ - I[32] = I[33], I[33] = I[34], I[34] = I[35], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], \ - I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[45] = I[46], I[46] = I[47], I[47] = I[48], \ - I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[54] = I[55], I[55] = I[56], \ - I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[63] = I[64], \ - I[64] = I[65], I[65] = I[66], I[66] = I[67], I[67] = I[68], I[68] = I[69], I[69] = I[70], I[70] = I[71], \ - I[72] = I[73], I[73] = I[74], I[74] = I[75], I[75] = I[76], I[76] = I[77], I[77] = I[78], I[78] = I[79], \ - I[79] = I[80], \ - _p4##x = _p3##x, _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x) - -#define cimg_for2x2x2(img,x,y,z,c,I,T) \ - cimg_for2((img)._depth,z) cimg_for2((img)._height,y) for (int x = 0, \ - _n1##x = (int)( \ - (I[0] = (T)(img)(0,y,z,c)), \ - (I[2] = (T)(img)(0,_n1##y,z,c)), \ - (I[4] = (T)(img)(0,y,_n1##z,c)), \ - (I[6] = (T)(img)(0,_n1##y,_n1##z,c)), \ - 1>=(img)._width?(img).width()-1:1); \ - (_n1##x<(img).width() && ( \ - (I[1] = (T)(img)(_n1##x,y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,y,_n1##z,c)), \ - (I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \ - x==--_n1##x; \ - I[0] = I[1], I[2] = I[3], I[4] = I[5], I[6] = I[7], \ - ++x, ++_n1##x) - -#define cimg_for_in2x2x2(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \ - cimg_for_in2((img)._depth,z0,z1,z) cimg_for_in2((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \ - _n1##x = (int)( \ - (I[0] = (T)(img)(x,y,z,c)), \ - (I[2] = (T)(img)(x,_n1##y,z,c)), \ - (I[4] = (T)(img)(x,y,_n1##z,c)), \ - (I[6] = (T)(img)(x,_n1##y,_n1##z,c)), \ - x+1>=(int)(img)._width?(img).width()-1:x+1); \ - x<=(int)(x1) && ((_n1##x<(img).width() && ( \ - (I[1] = (T)(img)(_n1##x,y,z,c)), \ - (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,y,_n1##z,c)), \ - (I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \ - x==--_n1##x); \ - I[0] = I[1], I[2] = I[3], I[4] = I[5], I[6] = I[7], \ - ++x, ++_n1##x) - -#define cimg_for3x3x3(img,x,y,z,c,I,T) \ - cimg_for3((img)._depth,z) cimg_for3((img)._height,y) for (int x = 0, \ - _p1##x = 0, \ - _n1##x = (int)( \ - (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \ - (I[3] = I[4] = (T)(img)(0,y,_p1##z,c)), \ - (I[6] = I[7] = (T)(img)(0,_n1##y,_p1##z,c)), \ - (I[9] = I[10] = (T)(img)(0,_p1##y,z,c)), \ - (I[12] = I[13] = (T)(img)(0,y,z,c)), \ - (I[15] = I[16] = (T)(img)(0,_n1##y,z,c)), \ - (I[18] = I[19] = (T)(img)(0,_p1##y,_n1##z,c)), \ - (I[21] = I[22] = (T)(img)(0,y,_n1##z,c)), \ - (I[24] = I[25] = (T)(img)(0,_n1##y,_n1##z,c)), \ - 1>=(img)._width?(img).width()-1:1); \ - (_n1##x<(img).width() && ( \ - (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \ - (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \ - (I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c)), \ - (I[11] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[14] = (T)(img)(_n1##x,y,z,c)), \ - (I[17] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c)), \ - (I[23] = (T)(img)(_n1##x,y,_n1##z,c)), \ - (I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \ - x==--_n1##x; \ - I[0] = I[1], I[1] = I[2], I[3] = I[4], I[4] = I[5], I[6] = I[7], I[7] = I[8], \ - I[9] = I[10], I[10] = I[11], I[12] = I[13], I[13] = I[14], I[15] = I[16], I[16] = I[17], \ - I[18] = I[19], I[19] = I[20], I[21] = I[22], I[22] = I[23], I[24] = I[25], I[25] = I[26], \ - _p1##x = x++, ++_n1##x) - -#define cimg_for_in3x3x3(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \ - 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), \ - _p1##x = x-1<0?0:x-1, \ - _n1##x = (int)( \ - (I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \ - (I[3] = (T)(img)(_p1##x,y,_p1##z,c)), \ - (I[6] = (T)(img)(_p1##x,_n1##y,_p1##z,c)), \ - (I[9] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[12] = (T)(img)(_p1##x,y,z,c)), \ - (I[15] = (T)(img)(_p1##x,_n1##y,z,c)), \ - (I[18] = (T)(img)(_p1##x,_p1##y,_n1##z,c)), \ - (I[21] = (T)(img)(_p1##x,y,_n1##z,c)), \ - (I[24] = (T)(img)(_p1##x,_n1##y,_n1##z,c)), \ - (I[1] = (T)(img)(x,_p1##y,_p1##z,c)), \ - (I[4] = (T)(img)(x,y,_p1##z,c)), \ - (I[7] = (T)(img)(x,_n1##y,_p1##z,c)), \ - (I[10] = (T)(img)(x,_p1##y,z,c)), \ - (I[13] = (T)(img)(x,y,z,c)), \ - (I[16] = (T)(img)(x,_n1##y,z,c)), \ - (I[19] = (T)(img)(x,_p1##y,_n1##z,c)), \ - (I[22] = (T)(img)(x,y,_n1##z,c)), \ - (I[25] = (T)(img)(x,_n1##y,_n1##z,c)), \ - x+1>=(int)(img)._width?(img).width()-1:x+1); \ - x<=(int)(x1) && ((_n1##x<(img).width() && ( \ - (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \ - (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \ - (I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c)), \ - (I[11] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[14] = (T)(img)(_n1##x,y,z,c)), \ - (I[17] = (T)(img)(_n1##x,_n1##y,z,c)), \ - (I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c)), \ - (I[23] = (T)(img)(_n1##x,y,_n1##z,c)), \ - (I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \ - x==--_n1##x); \ - I[0] = I[1], I[1] = I[2], I[3] = I[4], I[4] = I[5], I[6] = I[7], I[7] = I[8], \ - I[9] = I[10], I[10] = I[11], I[12] = I[13], I[13] = I[14], I[15] = I[16], I[16] = I[17], \ - I[18] = I[19], I[19] = I[20], I[21] = I[22], I[22] = I[23], I[24] = I[25], I[25] = I[26], \ - _p1##x = x++, ++_n1##x) - -#define cimglist_for(list,l) for (int l = 0; l<(int)(list)._width; ++l) -#define cimglist_for_in(list,l0,l1,l) \ - for (int l = (int)(l0)<0?0:(int)(l0), _max##l = (unsigned int)l1<(list)._width?(int)(l1):(int)(list)._width-1; \ - l<=_max##l; ++l) - -#define cimglist_apply(list,fn) cimglist_for(list,__##fn) (list)[__##fn].fn - -// Macros used to display error messages when exceptions are thrown. -// You should not use these macros is your own code. -#define _cimgdisplay_instance "[instance(%u,%u,%u,%c%s%c)] CImgDisplay::" -#define cimgdisplay_instance _width,_height,_normalization,_title?'\"':'[',_title?_title:"untitled",_title?'\"':']' -#define _cimg_instance "[instance(%u,%u,%u,%u,%p,%sshared)] CImg<%s>::" -#define cimg_instance _width,_height,_depth,_spectrum,_data,_is_shared?"":"non-",pixel_type() -#define _cimglist_instance "[instance(%u,%u,%p)] CImgList<%s>::" -#define cimglist_instance _width,_allocated_width,_data,pixel_type() - -/*------------------------------------------------ - # - # - # Define cimg_library:: namespace - # - # - -------------------------------------------------*/ -//! Contains all classes and functions of the \CImg library. -/** - This namespace is defined to avoid functions and class names collisions - that could happen with the inclusion of other C++ header files. - Anyway, it should not happen often and you should reasonnably start most of your - \CImg-based programs with - \code - #include "CImg.h" - using namespace cimg_library; - \endcode - to simplify the declaration of \CImg Library objects afterwards. -**/ -namespace cimg_library_suffixed { - - // Declare the four classes of the CImg Library. - template struct CImg; - template struct CImgList; - struct CImgDisplay; - struct CImgException; - - // Declare cimg:: namespace. - // This is an uncomplete namespace definition here. It only contains some - // necessary stuffs to ensure a correct declaration order of the classes and functions - // defined afterwards. - namespace cimg { - - // Define ascii sequences for colored terminal output. -#ifdef cimg_use_vt100 - const char t_normal[] = { 0x1b, '[', '0', ';', '0', ';', '0', 'm', 0 }; - const char t_black[] = { 0x1b, '[', '0', ';', '3', '0', ';', '5', '9', 'm', 0 }; - const char t_red[] = { 0x1b, '[', '0', ';', '3', '1', ';', '5', '9', 'm', 0 }; - const char t_green[] = { 0x1b, '[', '0', ';', '3', '2', ';', '5', '9', 'm', 0 }; - const char t_yellow[] = { 0x1b, '[', '0', ';', '3', '3', ';', '5', '9', 'm', 0 }; - const char t_blue[] = { 0x1b, '[', '0', ';', '3', '4', ';', '5', '9', 'm', 0 }; - const char t_magenta[] = { 0x1b, '[', '0', ';', '3', '5', ';', '5', '9', 'm', 0 }; - const char t_cyan[] = { 0x1b, '[', '0', ';', '3', '6', ';', '5', '9', 'm', 0 }; - const char t_white[] = { 0x1b, '[', '0', ';', '3', '7', ';', '5', '9', 'm', 0 }; - const char t_bold[] = { 0x1b, '[', '1', 'm', 0 }; - const char t_underscore[] = { 0x1b, '[', '4', 'm', 0 }; -#else - const char t_normal[] = { 0 }; - const char *const t_black = cimg::t_normal, - *const t_red = cimg::t_normal, - *const t_green = cimg::t_normal, - *const t_yellow = cimg::t_normal, - *const t_blue = cimg::t_normal, - *const t_magenta = cimg::t_normal, - *const t_cyan = cimg::t_normal, - *const t_white = cimg::t_normal, - *const t_bold = cimg::t_normal, - *const t_underscore = cimg::t_normal; -#endif - - inline std::FILE* output(std::FILE *file=0); - inline void info(); - - //! Avoid warning messages due to unused parameters. Do nothing actually. - template - inline void unused(const T&, ...) {} - - // [internal] Lock/unlock a mutex for managing concurrent threads. - // 'lock_mode' can be { 0=unlock | 1=lock | 2=trylock }. - // 'n' can be in [0,31] but mutex range [0,16] is reserved by CImg. - inline int mutex(const unsigned int n, const int lock_mode=1); - - inline unsigned int& _exception_mode(const unsigned int value, const bool is_set) { - static unsigned int mode = cimg_verbosity; - cimg::mutex(0); - if (is_set) mode = value; - cimg::mutex(0,0); - return mode; - } - - //! Set current \CImg exception mode. - /** - The way error messages are handled by \CImg can be changed dynamically, using this function. - \param mode Desired exception mode. Possible values are: - - \c 0: Hide library messages (quiet mode). - - \c 1: Print library messages on the console. - - \c 2: Display library messages on a dialog window (default behavior). - - \c 3: Do as \c 1 + add extra debug warnings (slow down the code!). - - \c 4: Do as \c 2 + add extra debug warnings (slow down the code!). - **/ - inline unsigned int& exception_mode(const unsigned int mode) { - return _exception_mode(mode,true); - } - - //! Return current \CImg exception mode. - /** - \note By default, return the value of configuration macro \c cimg_verbosity - **/ - inline unsigned int& exception_mode() { - return _exception_mode(0,false); - } - - inline int dialog(const char *const title, const char *const msg, const char *const button1_label="OK", - const char *const button2_label=0, const char *const button3_label=0, - const char *const button4_label=0, const char *const button5_label=0, - const char *const button6_label=0, const bool centering=false); - - inline double eval(const char *const expression, - const double x=0, const double y=0, const double z=0, const double c=0); - } - - /*--------------------------------------- - # - # Define the CImgException structures - # - --------------------------------------*/ - //! Instances of \c CImgException are thrown when errors are encountered in a \CImg function call. - /** - \par Overview - - CImgException is the base class of all exceptions thrown by \CImg. - CImgException is never thrown itself. Derived classes that specify the type of errord are thrown instead. - These derived classes can be: - - - \b CImgArgumentException: Thrown when one argument of a called \CImg function is invalid. - This is probably one of the most thrown exception by \CImg. - For instance, the following example throws a \c CImgArgumentException: - \code - CImg img(100,100,1,3); // Define a 100x100 color image with float-valued pixels. - img.mirror('e'); // Try to mirror image along the (non-existing) 'e'-axis. - \endcode - - - \b CImgDisplayException: Thrown when something went wrong during the display of images in CImgDisplay instances. - - - \b CImgInstanceException: Thrown when an instance associated to a called \CImg method does not fit - the function requirements. For instance, the following example throws a \c CImgInstanceException: - \code - const CImg img; // Define an empty image. - const float value = img.at(0); // Try to read first pixel value (does not exist). - \endcode - - - \b CImgIOException: Thrown when an error occured when trying to load or save image files. - This happens when trying to read files that do not exist or with invalid formats. - For instance, the following example throws a \c CImgIOException: - \code - const CImg img("missing_file.jpg"); // Try to load a file that does not exist. - \endcode - - - \b CImgWarningException: Thrown only if configuration macro \c cimg_strict_warnings is set, and - when a \CImg function has to display a warning message (see cimg::warn()). - - It is not recommended to throw CImgException instances by yourself, - since they are expected to be thrown only by \CImg. - When an error occurs in a library function call, \CImg may display error messages on the screen or on the - standard output, depending on the current \CImg exception mode. - The \CImg exception mode can be get and set by functions cimg::exception_mode() and - cimg::exception_mode(unsigned int). - - \par Exceptions handling - - In all cases, when an error occurs in \CImg, an instance of the corresponding exception class is thrown. - This may lead the program to break (this is the default behavior), but you can bypass this behavior by - handling the exceptions by yourself, - using a usual try { ... } catch () { ... } bloc, as in the following example: - \code - #define "CImg.h" - using namespace cimg_library; - int main() { - cimg::exception_mode(0); // Enable quiet exception mode. - try { - ... // Here, do what you want to stress CImg. - } catch (CImgException &e) { // You succeeded: something went wrong! - std::fprintf(stderr,"CImg Library Error: %s",e.what()); // Display your custom error message. - ... // Do what you want now to save the ship! - } - } - \endcode - **/ - struct CImgException : public std::exception { -#define _cimg_exception_err(etype,disp_flag) \ - std::va_list ap; va_start(ap,format); cimg_vsnprintf(_message,sizeof(_message),format,ap); va_end(ap); \ - if (cimg::exception_mode()) { \ - std::fprintf(cimg::output(),"\n%s[CImg] *** %s ***%s %s\n",cimg::t_red,etype,cimg::t_normal,_message); \ - if (cimg_display && disp_flag && !(cimg::exception_mode()%2)) try { cimg::dialog(etype,_message,"Abort"); } \ - catch (CImgException&) {} \ - if (cimg::exception_mode()>=3) cimg_library_suffixed::cimg::info(); \ - } - - char _message[16384]; - CImgException() { *_message = 0; } - CImgException(const char *const format, ...) { _cimg_exception_err("CImgException",true); } - //! Return a C-string containing the error message associated to the thrown exception. - const char *what() const throw() { return _message; } - }; - - // The CImgInstanceException class is used to throw an exception related - // to an invalid instance encountered in a library function call. - struct CImgInstanceException : public CImgException { - CImgInstanceException(const char *const format, ...) { _cimg_exception_err("CImgInstanceException",true); } - }; - - // The CImgArgumentException class is used to throw an exception related - // to invalid arguments encountered in a library function call. - struct CImgArgumentException : public CImgException { - CImgArgumentException(const char *const format, ...) { _cimg_exception_err("CImgArgumentException",true); } - }; - - // The CImgIOException class is used to throw an exception related - // to input/output file problems encountered in a library function call. - struct CImgIOException : public CImgException { - CImgIOException(const char *const format, ...) { _cimg_exception_err("CImgIOException",true); } - }; - - // The CImgDisplayException class is used to throw an exception related - // to display problems encountered in a library function call. - struct CImgDisplayException : public CImgException { - CImgDisplayException(const char *const format, ...) { _cimg_exception_err("CImgDisplayException",false); } - }; - - // The CImgWarningException class is used to throw an exception for warnings - // encountered in a library function call. - struct CImgWarningException : public CImgException { - CImgWarningException(const char *const format, ...) { _cimg_exception_err("CImgWarningException",false); } - }; - - /*------------------------------------- - # - # Define cimg:: namespace - # - -----------------------------------*/ - //! Contains \a low-level functions and variables of the \CImg Library. - /** - Most of the functions and variables within this namespace are used by the \CImg library for low-level operations. - You may use them to access specific const values or environment variables internally used by \CImg. - \warning Never write using namespace cimg_library::cimg; in your source code. Lot of functions in the - cimg:: namespace have the same names as standard C functions that may be defined in the global - namespace ::. - **/ - namespace cimg { - - // Define traits that will be used to determine the best data type to work in CImg functions. - // - template struct type { - static const char* string() { - static const char* s[] = { "unknown", "unknown8", "unknown16", "unknown24", - "unknown32", "unknown40", "unknown48", "unknown56", - "unknown64", "unknown72", "unknown80", "unknown88", - "unknown96", "unknown104", "unknown112", "unknown120", - "unknown128" }; - return s[(sizeof(T)<17)?sizeof(T):0]; - } - static bool is_float() { return false; } - static bool is_inf(const T) { return false; } - static bool is_nan(const T) { return false; } - static T min() { return (T)-1>0?(T)0:(T)-1<<(8*sizeof(T)-1); } - static T max() { return (T)-1>0?(T)-1:~((T)-1<<(8*sizeof(T)-1)); } - static T inf() { return max(); } - static T cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(T)val; } - static const char* format() { return "%s"; } - static const char* format(const T val) { static const char *const s = "unknown"; cimg::unused(val); return s; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "bool"; return s; } - static bool is_float() { return false; } - static bool is_inf(const bool) { return false; } - static bool is_nan(const bool) { return false; } - static bool min() { return false; } - static bool max() { return true; } - static bool inf() { return max(); } - static bool is_inf() { return false; } - static bool cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(bool)val; } - static const char* format() { return "%s"; } - static const char* format(const bool val) { static const char* s[] = { "false", "true" }; return s[val?1:0]; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "unsigned char"; return s; } - static bool is_float() { return false; } - static bool is_inf(const unsigned char) { return false; } - static bool is_nan(const unsigned char) { return false; } - static unsigned char min() { return 0; } - static unsigned char max() { return (unsigned char)~0U; } - static unsigned char inf() { return max(); } - static unsigned char cut(const double val) { - return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; } - static const char* format() { return "%u"; } - static unsigned int format(const unsigned char val) { return (unsigned int)val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "char"; return s; } - static bool is_float() { return false; } - static bool is_inf(const char) { return false; } - static bool is_nan(const char) { return false; } - static char min() { return (char)(-1L<<(8*sizeof(char)-1)); } - static char max() { return (char)~((char)(-1L<<(8*sizeof(char)-1))); } - static char inf() { return max(); } - static char cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(char)val; } - static const char* format() { return "%d"; } - static int format(const char val) { return (int)val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "signed char"; return s; } - static bool is_float() { return false; } - static bool is_inf(const signed char) { return false; } - static bool is_nan(const signed char) { return false; } - static signed char min() { return (signed char)(-1L<<(8*sizeof(signed char)-1)); } - static signed char max() { return ~((signed char)(-1L<<(8*sizeof(signed char)-1))); } - static signed char inf() { return max(); } - static signed char cut(const double val) { - return val<(double)min()?min():val>(double)max()?max():(signed char)val; } - static const char* format() { return "%d"; } - static unsigned int format(const signed char val) { return (int)val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "unsigned short"; return s; } - static bool is_float() { return false; } - static bool is_inf(const unsigned short) { return false; } - static bool is_nan(const unsigned short) { return false; } - static unsigned short min() { return 0; } - static unsigned short max() { return (unsigned short)~0U; } - static unsigned short inf() { return max(); } - static unsigned short cut(const double val) { - return val<(double)min()?min():val>(double)max()?max():(unsigned short)val; } - static const char* format() { return "%u"; } - static unsigned int format(const unsigned short val) { return (unsigned int)val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "short"; return s; } - static bool is_float() { return false; } - static bool is_inf(const short) { return false; } - static bool is_nan(const short) { return false; } - static short min() { return (short)(-1L<<(8*sizeof(short)-1)); } - static short max() { return ~((short)(-1L<<(8*sizeof(short)-1))); } - static short inf() { return max(); } - static short cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(short)val; } - static const char* format() { return "%d"; } - static int format(const short val) { return (int)val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "unsigned int"; return s; } - static bool is_float() { return false; } - static bool is_inf(const unsigned int) { return false; } - static bool is_nan(const unsigned int) { return false; } - static unsigned int min() { return 0; } - static unsigned int max() { return (unsigned int)~0U; } - static unsigned int inf() { return max(); } - static unsigned int cut(const double val) { - return val<(double)min()?min():val>(double)max()?max():(unsigned int)val; } - static const char* format() { return "%u"; } - static unsigned int format(const unsigned int val) { return val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "int"; return s; } - static bool is_float() { return false; } - static bool is_inf(const int) { return false; } - static bool is_nan(const int) { return false; } - static int min() { return (int)(-1L<<(8*sizeof(int)-1)); } - static int max() { return ~((int)(-1L<<(8*sizeof(int)-1))); } - static int inf() { return max(); } - static int cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(int)val; } - static const char* format() { return "%d"; } - static int format(const int val) { return val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "unsigned long"; return s; } - static bool is_float() { return false; } - static bool is_inf(const unsigned long) { return false; } - static bool is_nan(const unsigned long) { return false; } - static unsigned long min() { return 0; } - static unsigned long max() { return (unsigned long)~0UL; } - static unsigned long inf() { return max(); } - static unsigned long cut(const double val) { - return val<(double)min()?min():val>(double)max()?max():(unsigned long)val; } - static const char* format() { return "%lu"; } - static unsigned long format(const unsigned long val) { return val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "long"; return s; } - static bool is_float() { return false; } - static bool is_inf(const long) { return false; } - static bool is_nan(const long) { return false; } - static long min() { return (long)(-1L<<(8*sizeof(long)-1)); } - static long max() { return ~((long)(-1L<<(8*sizeof(long)-1))); } - static long inf() { return max(); } - static long cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(long)val; } - static const char* format() { return "%ld"; } - static long format(const long val) { return val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "double"; return s; } - static bool is_float() { return true; } - static bool is_inf(const double val) { -#ifdef isinf - return (bool)isinf(val); -#else - return !is_nan(val) && (val::min() || val>cimg::type::max()); -#endif - } - static bool is_nan(const double val) { -#ifdef isnan - return (bool)isnan(val); -#else - return !(val==val); -#endif - } - static double min() { return -1.7E308; } - static double max() { return 1.7E308; } - static double inf() { return max()*max(); } - static double nan() { static const double val_nan = -std::sqrt(-1.0); return val_nan; } - static double cut(const double val) { return valmax()?max():val; } - static const char* format() { return "%.16g"; } - static double format(const double val) { return val; } - }; - - template<> struct type { - static const char* string() { static const char *const s = "float"; return s; } - static bool is_float() { return true; } - static bool is_inf(const float val) { -#ifdef isinf - return (bool)isinf(val); -#else - return !is_nan(val) && (val::min() || val>cimg::type::max()); -#endif - } - static bool is_nan(const float val) { -#ifdef isnan - return (bool)isnan(val); -#else - return !(val==val); -#endif - } - static float min() { return -3.4E38f; } - static float max() { return 3.4E38f; } - static float inf() { return (float)cimg::type::inf(); } - static float nan() { return (float)cimg::type::nan(); } - static float cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(float)val; } - static const char* format() { return "%.16g"; } - static double format(const float val) { return (double)val; } - }; - - template struct superset { typedef T type; }; - template<> struct superset { typedef unsigned char type; }; - template<> struct superset { typedef char type; }; - template<> struct superset { typedef signed char type; }; - template<> struct superset { typedef unsigned short type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef unsigned int type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef unsigned long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef unsigned short type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef unsigned int type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef unsigned long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef short type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef unsigned int type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef unsigned long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef int type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef unsigned long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef float type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef long type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef double type; }; - template<> struct superset { typedef double type; }; - - template struct superset2 { - typedef typename superset::type>::type type; - }; - - template struct superset3 { - typedef typename superset::type>::type type; - }; - - template struct last { typedef t2 type; }; - -#define _cimg_Tt typename cimg::superset::type -#define _cimg_Tfloat typename cimg::superset::type -#define _cimg_Ttfloat typename cimg::superset2::type -#define _cimg_Ttdouble typename cimg::superset2::type - - // Define variables used internally by CImg. -#if cimg_display==1 - struct X11_info { - volatile unsigned int nb_wins; - pthread_t* events_thread; - pthread_cond_t wait_event; - pthread_mutex_t wait_event_mutex; - CImgDisplay* wins[1024]; - Display* display; - unsigned int nb_bits; - bool is_blue_first; - bool is_shm_enabled; - bool byte_order; -#ifdef cimg_use_xrandr - XRRScreenSize *resolutions; - Rotation curr_rotation; - unsigned int curr_resolution; - unsigned int nb_resolutions; -#endif - X11_info():nb_wins(0),events_thread(0),display(0), - nb_bits(0),is_blue_first(false),is_shm_enabled(false),byte_order(false) { - XInitThreads(); - pthread_mutex_init(&wait_event_mutex,0); - pthread_cond_init(&wait_event,0); -#ifdef cimg_use_xrandr - resolutions = 0; - curr_rotation = 0; - curr_resolution = nb_resolutions = 0; -#endif - } - - ~X11_info() { - if (events_thread) { - pthread_cancel(*events_thread); - delete events_thread; - } - if (display) {} // XCloseDisplay(display); - pthread_cond_destroy(&wait_event); - pthread_mutex_unlock(&wait_event_mutex); - pthread_mutex_destroy(&wait_event_mutex); - } - }; -#if defined(cimg_module) - X11_info& X11_attr(); -#elif defined(cimg_main) - X11_info& X11_attr() { static X11_info val; return val; } -#else - inline X11_info& X11_attr() { static X11_info val; return val; } -#endif - -#elif cimg_display==2 - struct Win32_info { - HANDLE wait_event; - Win32_info() { wait_event = CreateEvent(0,FALSE,FALSE,0); } - }; -#if defined(cimg_module) - Win32_info& Win32_attr(); -#elif defined(cimg_main) - Win32_info& Win32_attr() { static Win32_info val; return val; } -#else - inline Win32_info& Win32_attr() { static Win32_info val; return val; } -#endif -#endif - - struct Mutex_info { -#if cimg_OS==2 - HANDLE mutex[32]; - Mutex_info() { for (unsigned int i = 0; i<32; ++i) mutex[i] = CreateMutex(0,FALSE,0); } - void lock(const unsigned int n) { WaitForSingleObject(mutex[n],INFINITE); } - void unlock(const unsigned int n) { ReleaseMutex(mutex[n]); } - int trylock(const unsigned int) { return 0; } -#elif defined(_PTHREAD_H) - pthread_mutex_t mutex[32]; - Mutex_info() { for (unsigned int i = 0; i<32; ++i) pthread_mutex_init(&mutex[i],0); } - void lock(const unsigned int n) { pthread_mutex_lock(&mutex[n]); } - void unlock(const unsigned int n) { pthread_mutex_unlock(&mutex[n]); } - int trylock(const unsigned int n) { return pthread_mutex_trylock(&mutex[n]); } -#else - Mutex_info() {} - void lock(const unsigned int) {} - void unlock(const unsigned int) {} - int trylock(const unsigned int) { return 0; } -#endif - }; -#if defined(cimg_module) - Mutex_info& Mutex_attr(); -#elif defined(cimg_main) - Mutex_info& Mutex_attr() { static Mutex_info val; return val; } -#else - inline Mutex_info& Mutex_attr() { static Mutex_info val; return val; } -#endif - -#if defined(cimg_use_magick) - static struct Magick_info { - Magick_info() { - Magick::InitializeMagick(""); - } - } _Magick_info; -#endif - -#if cimg_display==1 - // Define keycodes for X11-based graphical systems. - const unsigned int keyESC = XK_Escape; - const unsigned int keyF1 = XK_F1; - const unsigned int keyF2 = XK_F2; - const unsigned int keyF3 = XK_F3; - const unsigned int keyF4 = XK_F4; - const unsigned int keyF5 = XK_F5; - const unsigned int keyF6 = XK_F6; - const unsigned int keyF7 = XK_F7; - const unsigned int keyF8 = XK_F8; - const unsigned int keyF9 = XK_F9; - const unsigned int keyF10 = XK_F10; - const unsigned int keyF11 = XK_F11; - const unsigned int keyF12 = XK_F12; - const unsigned int keyPAUSE = XK_Pause; - const unsigned int key1 = XK_1; - const unsigned int key2 = XK_2; - const unsigned int key3 = XK_3; - const unsigned int key4 = XK_4; - const unsigned int key5 = XK_5; - const unsigned int key6 = XK_6; - const unsigned int key7 = XK_7; - const unsigned int key8 = XK_8; - const unsigned int key9 = XK_9; - const unsigned int key0 = XK_0; - const unsigned int keyBACKSPACE = XK_BackSpace; - const unsigned int keyINSERT = XK_Insert; - const unsigned int keyHOME = XK_Home; - const unsigned int keyPAGEUP = XK_Page_Up; - const unsigned int keyTAB = XK_Tab; - const unsigned int keyQ = XK_q; - const unsigned int keyW = XK_w; - const unsigned int keyE = XK_e; - const unsigned int keyR = XK_r; - const unsigned int keyT = XK_t; - const unsigned int keyY = XK_y; - const unsigned int keyU = XK_u; - const unsigned int keyI = XK_i; - const unsigned int keyO = XK_o; - const unsigned int keyP = XK_p; - const unsigned int keyDELETE = XK_Delete; - const unsigned int keyEND = XK_End; - const unsigned int keyPAGEDOWN = XK_Page_Down; - const unsigned int keyCAPSLOCK = XK_Caps_Lock; - const unsigned int keyA = XK_a; - const unsigned int keyS = XK_s; - const unsigned int keyD = XK_d; - const unsigned int keyF = XK_f; - const unsigned int keyG = XK_g; - const unsigned int keyH = XK_h; - const unsigned int keyJ = XK_j; - const unsigned int keyK = XK_k; - const unsigned int keyL = XK_l; - const unsigned int keyENTER = XK_Return; - const unsigned int keySHIFTLEFT = XK_Shift_L; - const unsigned int keyZ = XK_z; - const unsigned int keyX = XK_x; - const unsigned int keyC = XK_c; - const unsigned int keyV = XK_v; - const unsigned int keyB = XK_b; - const unsigned int keyN = XK_n; - const unsigned int keyM = XK_m; - const unsigned int keySHIFTRIGHT = XK_Shift_R; - const unsigned int keyARROWUP = XK_Up; - const unsigned int keyCTRLLEFT = XK_Control_L; - const unsigned int keyAPPLEFT = XK_Super_L; - const unsigned int keyALT = XK_Alt_L; - const unsigned int keySPACE = XK_space; - const unsigned int keyALTGR = XK_Alt_R; - const unsigned int keyAPPRIGHT = XK_Super_R; - const unsigned int keyMENU = XK_Menu; - const unsigned int keyCTRLRIGHT = XK_Control_R; - const unsigned int keyARROWLEFT = XK_Left; - const unsigned int keyARROWDOWN = XK_Down; - const unsigned int keyARROWRIGHT = XK_Right; - const unsigned int keyPAD0 = XK_KP_0; - const unsigned int keyPAD1 = XK_KP_1; - const unsigned int keyPAD2 = XK_KP_2; - const unsigned int keyPAD3 = XK_KP_3; - const unsigned int keyPAD4 = XK_KP_4; - const unsigned int keyPAD5 = XK_KP_5; - const unsigned int keyPAD6 = XK_KP_6; - const unsigned int keyPAD7 = XK_KP_7; - const unsigned int keyPAD8 = XK_KP_8; - const unsigned int keyPAD9 = XK_KP_9; - const unsigned int keyPADADD = XK_KP_Add; - const unsigned int keyPADSUB = XK_KP_Subtract; - const unsigned int keyPADMUL = XK_KP_Multiply; - const unsigned int keyPADDIV = XK_KP_Divide; - -#elif cimg_display==2 - // Define keycodes for Windows. - const unsigned int keyESC = VK_ESCAPE; - const unsigned int keyF1 = VK_F1; - const unsigned int keyF2 = VK_F2; - const unsigned int keyF3 = VK_F3; - const unsigned int keyF4 = VK_F4; - const unsigned int keyF5 = VK_F5; - const unsigned int keyF6 = VK_F6; - const unsigned int keyF7 = VK_F7; - const unsigned int keyF8 = VK_F8; - const unsigned int keyF9 = VK_F9; - const unsigned int keyF10 = VK_F10; - const unsigned int keyF11 = VK_F11; - const unsigned int keyF12 = VK_F12; - const unsigned int keyPAUSE = VK_PAUSE; - const unsigned int key1 = '1'; - const unsigned int key2 = '2'; - const unsigned int key3 = '3'; - const unsigned int key4 = '4'; - const unsigned int key5 = '5'; - const unsigned int key6 = '6'; - const unsigned int key7 = '7'; - const unsigned int key8 = '8'; - const unsigned int key9 = '9'; - const unsigned int key0 = '0'; - const unsigned int keyBACKSPACE = VK_BACK; - const unsigned int keyINSERT = VK_INSERT; - const unsigned int keyHOME = VK_HOME; - const unsigned int keyPAGEUP = VK_PRIOR; - const unsigned int keyTAB = VK_TAB; - const unsigned int keyQ = 'Q'; - const unsigned int keyW = 'W'; - const unsigned int keyE = 'E'; - const unsigned int keyR = 'R'; - const unsigned int keyT = 'T'; - const unsigned int keyY = 'Y'; - const unsigned int keyU = 'U'; - const unsigned int keyI = 'I'; - const unsigned int keyO = 'O'; - const unsigned int keyP = 'P'; - const unsigned int keyDELETE = VK_DELETE; - const unsigned int keyEND = VK_END; - const unsigned int keyPAGEDOWN = VK_NEXT; - const unsigned int keyCAPSLOCK = VK_CAPITAL; - const unsigned int keyA = 'A'; - const unsigned int keyS = 'S'; - const unsigned int keyD = 'D'; - const unsigned int keyF = 'F'; - const unsigned int keyG = 'G'; - const unsigned int keyH = 'H'; - const unsigned int keyJ = 'J'; - const unsigned int keyK = 'K'; - const unsigned int keyL = 'L'; - const unsigned int keyENTER = VK_RETURN; - const unsigned int keySHIFTLEFT = VK_SHIFT; - const unsigned int keyZ = 'Z'; - const unsigned int keyX = 'X'; - const unsigned int keyC = 'C'; - const unsigned int keyV = 'V'; - const unsigned int keyB = 'B'; - const unsigned int keyN = 'N'; - const unsigned int keyM = 'M'; - const unsigned int keySHIFTRIGHT = VK_SHIFT; - const unsigned int keyARROWUP = VK_UP; - const unsigned int keyCTRLLEFT = VK_CONTROL; - const unsigned int keyAPPLEFT = VK_LWIN; - const unsigned int keyALT = VK_LMENU; - const unsigned int keySPACE = VK_SPACE; - const unsigned int keyALTGR = VK_CONTROL; - const unsigned int keyAPPRIGHT = VK_RWIN; - const unsigned int keyMENU = VK_APPS; - const unsigned int keyCTRLRIGHT = VK_CONTROL; - const unsigned int keyARROWLEFT = VK_LEFT; - const unsigned int keyARROWDOWN = VK_DOWN; - const unsigned int keyARROWRIGHT = VK_RIGHT; - const unsigned int keyPAD0 = 0x60; - const unsigned int keyPAD1 = 0x61; - const unsigned int keyPAD2 = 0x62; - const unsigned int keyPAD3 = 0x63; - const unsigned int keyPAD4 = 0x64; - const unsigned int keyPAD5 = 0x65; - const unsigned int keyPAD6 = 0x66; - const unsigned int keyPAD7 = 0x67; - const unsigned int keyPAD8 = 0x68; - const unsigned int keyPAD9 = 0x69; - const unsigned int keyPADADD = VK_ADD; - const unsigned int keyPADSUB = VK_SUBTRACT; - const unsigned int keyPADMUL = VK_MULTIPLY; - const unsigned int keyPADDIV = VK_DIVIDE; - -#else - // Define random keycodes when no display is available. - // (should rarely be used then!). - const unsigned int keyESC = 1U; //!< Keycode for the \c ESC key (architecture-dependent). - const unsigned int keyF1 = 2U; //!< Keycode for the \c F1 key (architecture-dependent). - const unsigned int keyF2 = 3U; //!< Keycode for the \c F2 key (architecture-dependent). - const unsigned int keyF3 = 4U; //!< Keycode for the \c F3 key (architecture-dependent). - const unsigned int keyF4 = 5U; //!< Keycode for the \c F4 key (architecture-dependent). - const unsigned int keyF5 = 6U; //!< Keycode for the \c F5 key (architecture-dependent). - const unsigned int keyF6 = 7U; //!< Keycode for the \c F6 key (architecture-dependent). - const unsigned int keyF7 = 8U; //!< Keycode for the \c F7 key (architecture-dependent). - const unsigned int keyF8 = 9U; //!< Keycode for the \c F8 key (architecture-dependent). - const unsigned int keyF9 = 10U; //!< Keycode for the \c F9 key (architecture-dependent). - const unsigned int keyF10 = 11U; //!< Keycode for the \c F10 key (architecture-dependent). - const unsigned int keyF11 = 12U; //!< Keycode for the \c F11 key (architecture-dependent). - const unsigned int keyF12 = 13U; //!< Keycode for the \c F12 key (architecture-dependent). - const unsigned int keyPAUSE = 14U; //!< Keycode for the \c PAUSE key (architecture-dependent). - const unsigned int key1 = 15U; //!< Keycode for the \c 1 key (architecture-dependent). - const unsigned int key2 = 16U; //!< Keycode for the \c 2 key (architecture-dependent). - const unsigned int key3 = 17U; //!< Keycode for the \c 3 key (architecture-dependent). - const unsigned int key4 = 18U; //!< Keycode for the \c 4 key (architecture-dependent). - const unsigned int key5 = 19U; //!< Keycode for the \c 5 key (architecture-dependent). - const unsigned int key6 = 20U; //!< Keycode for the \c 6 key (architecture-dependent). - const unsigned int key7 = 21U; //!< Keycode for the \c 7 key (architecture-dependent). - const unsigned int key8 = 22U; //!< Keycode for the \c 8 key (architecture-dependent). - const unsigned int key9 = 23U; //!< Keycode for the \c 9 key (architecture-dependent). - const unsigned int key0 = 24U; //!< Keycode for the \c 0 key (architecture-dependent). - const unsigned int keyBACKSPACE = 25U; //!< Keycode for the \c BACKSPACE key (architecture-dependent). - const unsigned int keyINSERT = 26U; //!< Keycode for the \c INSERT key (architecture-dependent). - const unsigned int keyHOME = 27U; //!< Keycode for the \c HOME key (architecture-dependent). - const unsigned int keyPAGEUP = 28U; //!< Keycode for the \c PAGEUP key (architecture-dependent). - const unsigned int keyTAB = 29U; //!< Keycode for the \c TAB key (architecture-dependent). - const unsigned int keyQ = 30U; //!< Keycode for the \c Q key (architecture-dependent). - const unsigned int keyW = 31U; //!< Keycode for the \c W key (architecture-dependent). - const unsigned int keyE = 32U; //!< Keycode for the \c E key (architecture-dependent). - const unsigned int keyR = 33U; //!< Keycode for the \c R key (architecture-dependent). - const unsigned int keyT = 34U; //!< Keycode for the \c T key (architecture-dependent). - const unsigned int keyY = 35U; //!< Keycode for the \c Y key (architecture-dependent). - const unsigned int keyU = 36U; //!< Keycode for the \c U key (architecture-dependent). - const unsigned int keyI = 37U; //!< Keycode for the \c I key (architecture-dependent). - const unsigned int keyO = 38U; //!< Keycode for the \c O key (architecture-dependent). - const unsigned int keyP = 39U; //!< Keycode for the \c P key (architecture-dependent). - const unsigned int keyDELETE = 40U; //!< Keycode for the \c DELETE key (architecture-dependent). - const unsigned int keyEND = 41U; //!< Keycode for the \c END key (architecture-dependent). - const unsigned int keyPAGEDOWN = 42U; //!< Keycode for the \c PAGEDOWN key (architecture-dependent). - const unsigned int keyCAPSLOCK = 43U; //!< Keycode for the \c CAPSLOCK key (architecture-dependent). - const unsigned int keyA = 44U; //!< Keycode for the \c A key (architecture-dependent). - const unsigned int keyS = 45U; //!< Keycode for the \c S key (architecture-dependent). - const unsigned int keyD = 46U; //!< Keycode for the \c D key (architecture-dependent). - const unsigned int keyF = 47U; //!< Keycode for the \c F key (architecture-dependent). - const unsigned int keyG = 48U; //!< Keycode for the \c G key (architecture-dependent). - const unsigned int keyH = 49U; //!< Keycode for the \c H key (architecture-dependent). - const unsigned int keyJ = 50U; //!< Keycode for the \c J key (architecture-dependent). - const unsigned int keyK = 51U; //!< Keycode for the \c K key (architecture-dependent). - const unsigned int keyL = 52U; //!< Keycode for the \c L key (architecture-dependent). - const unsigned int keyENTER = 53U; //!< Keycode for the \c ENTER key (architecture-dependent). - const unsigned int keySHIFTLEFT = 54U; //!< Keycode for the \c SHIFTLEFT key (architecture-dependent). - const unsigned int keyZ = 55U; //!< Keycode for the \c Z key (architecture-dependent). - const unsigned int keyX = 56U; //!< Keycode for the \c X key (architecture-dependent). - const unsigned int keyC = 57U; //!< Keycode for the \c C key (architecture-dependent). - const unsigned int keyV = 58U; //!< Keycode for the \c V key (architecture-dependent). - const unsigned int keyB = 59U; //!< Keycode for the \c B key (architecture-dependent). - const unsigned int keyN = 60U; //!< Keycode for the \c N key (architecture-dependent). - const unsigned int keyM = 61U; //!< Keycode for the \c M key (architecture-dependent). - const unsigned int keySHIFTRIGHT = 62U; //!< Keycode for the \c SHIFTRIGHT key (architecture-dependent). - const unsigned int keyARROWUP = 63U; //!< Keycode for the \c ARROWUP key (architecture-dependent). - const unsigned int keyCTRLLEFT = 64U; //!< Keycode for the \c CTRLLEFT key (architecture-dependent). - const unsigned int keyAPPLEFT = 65U; //!< Keycode for the \c APPLEFT key (architecture-dependent). - const unsigned int keyALT = 66U; //!< Keycode for the \c ALT key (architecture-dependent). - const unsigned int keySPACE = 67U; //!< Keycode for the \c SPACE key (architecture-dependent). - const unsigned int keyALTGR = 68U; //!< Keycode for the \c ALTGR key (architecture-dependent). - const unsigned int keyAPPRIGHT = 69U; //!< Keycode for the \c APPRIGHT key (architecture-dependent). - const unsigned int keyMENU = 70U; //!< Keycode for the \c MENU key (architecture-dependent). - const unsigned int keyCTRLRIGHT = 71U; //!< Keycode for the \c CTRLRIGHT key (architecture-dependent). - const unsigned int keyARROWLEFT = 72U; //!< Keycode for the \c ARROWLEFT key (architecture-dependent). - const unsigned int keyARROWDOWN = 73U; //!< Keycode for the \c ARROWDOWN key (architecture-dependent). - const unsigned int keyARROWRIGHT = 74U; //!< Keycode for the \c ARROWRIGHT key (architecture-dependent). - const unsigned int keyPAD0 = 75U; //!< Keycode for the \c PAD0 key (architecture-dependent). - const unsigned int keyPAD1 = 76U; //!< Keycode for the \c PAD1 key (architecture-dependent). - const unsigned int keyPAD2 = 77U; //!< Keycode for the \c PAD2 key (architecture-dependent). - const unsigned int keyPAD3 = 78U; //!< Keycode for the \c PAD3 key (architecture-dependent). - const unsigned int keyPAD4 = 79U; //!< Keycode for the \c PAD4 key (architecture-dependent). - const unsigned int keyPAD5 = 80U; //!< Keycode for the \c PAD5 key (architecture-dependent). - const unsigned int keyPAD6 = 81U; //!< Keycode for the \c PAD6 key (architecture-dependent). - const unsigned int keyPAD7 = 82U; //!< Keycode for the \c PAD7 key (architecture-dependent). - const unsigned int keyPAD8 = 83U; //!< Keycode for the \c PAD8 key (architecture-dependent). - const unsigned int keyPAD9 = 84U; //!< Keycode for the \c PAD9 key (architecture-dependent). - const unsigned int keyPADADD = 85U; //!< Keycode for the \c PADADD key (architecture-dependent). - const unsigned int keyPADSUB = 86U; //!< Keycode for the \c PADSUB key (architecture-dependent). - const unsigned int keyPADMUL = 87U; //!< Keycode for the \c PADMUL key (architecture-dependent). - const unsigned int keyPADDIV = 88U; //!< Keycode for the \c PADDDIV key (architecture-dependent). -#endif - - const double PI = 3.14159265358979323846; //!< Value of the mathematical constant PI - - // Define a 12x13 font (small size). - const char *const data_font12x13 = -" .wjwlwmyuw>wjwkwbwjwkwRxuwmwjwkwmyuwJwjwlx`w Fw mwlwlwuwnwuynwuwmyTwlwkwuwmwuwnwlwkwuwmwuw_wuxl" -"wlwkwuwnwuynwuwTwlwlwtwnwtwnw my Qw +wlw b{ \\w Wx`xTw_w[wbxawSwkw nynwkyw bwswcwkwuwjwuwozpwtwuwnwtwowkwjwmwuwuwkwIxmxuxowuwmwswowswmxnwjwhwowswowsw0wmwowswuwnwrwowswpwswowkwjwrwqw" -"rwpwkwkwtwnwkxsxqxswowswpwswnwswpwswowrwnwmwrwqwqwqwswswrwswowswjwpwlxjwkxuxLw[wcw_wSwkw mw\"wlwiw=wtwmxlwFw cwswnwuwnwkwjwswo{pwrwpwtwtwpwswby`w`yUwlw" -"twpwqwpwswowlw\\wrwrxuwHwrwfwuwjwlwlwTyuwVwlwtwawswowswowswcwuwmwuwmwuwmwuwmwuwlwkwuwnwswpwkwkwkwkwkwkwkwkwswoxswowswowswowswowswowswowrwpwswpwrwpwrwpw" -"rwpwrwpwswoznwtw Ww (wGwtwtwqwqwqwuwuwuwqwswuwqwqw=wqxtw`{nzp~q{ozowrwnxmwtwow bzawkwuwl}rwuwnwtwuwnwtwowkwjwlyjwIwlwswmwiwkwnwuwnwkwhwnwswowswowkwew" -"ewixnwsytwswuwnwrwpwkwrwpwkwkwkwrwpwkwkwuwmwkxsxqwuwtwpwqwqwswowqwqwswowiwmwrwpwswowtwtwpwuwmwuwjwowkwjwlxsxXynzmymznyozlzoznwkwkwtwnwkzuyrzmynzmzowux" -"myozmwswpwrwowtwtwrwrwpwrwp{mwlwiwHyuwpwtwkwmxlynzoxswmwmwswnwswowtxq|owtwtwpym{p{owswnwuwmwlwkwqwqxuwuxqwrwpwtwtwqwqwowlwuwuwkwmwlwtwowuwuwdwjznwl{nw" -"uwnwkx_wtxtwswtwlwtwWwuytwgyjwmwjwawswoyuwVwlwtwnwtwmwtwnwtwmwuwmwlwuwmwuwmwuwmwuwmwuwmwuwmxuwowkwkwkwkwkwkwkwkwkwrwpwuwtwpwqwqwqwqwqwqwqwqwqwowtwpwsw" -"uwqwrwpwrwpwrwpwrwowuwnwswowuwlymymymymymymyuyqymymymymynwkwkwkwjynzmymymymymykwmzowswowswowswowswpwrwozowrwW}q}qwtwtwqwtwtwqwtwtwA}rwuw_{p~r~r}pwtwow" -"rwnxmwtwow aw_w]wtwpwuwmxuwmybwjwlyjwIwlwswmwiwnynwtwnznzkwmynwswTyp}pylwmwtwtwtwswuwn{owkwrwp{o{owk|pwkwkxlwkwuwuwuwqwuwtwpwqwqwswowqwqwswoykwmwrwpws" -"wowuwuwuwowkwjwnwkwjwDwowswowkwswowswowkwswowswowkwkwuwmwkwswswswswowswowswowswoxlwswowkwswpwrwowtwtwqwtwowrwlwoxkwhxVxuxpwtypwuwjwnwtwnwkwswowtxnxmws" -"wowqwqwtwuxqwtwnwtwtwqwswowswmwm{nwuwlxnwkwqwqwtwtwqwrwpwtwtwqwuyuwpwiwhwnwmwrwnwbwkwuwlwlwswoxuxowlwtw`wuwrwszmwtwo}dwuwtwuw[}qymx`wswoyuwow_ylxlwtwo" -"yuwoyuwoyuwmwlwuwmwuwmwuwmwuwmwuwmwuwmwt{swk{o{o{o{owkwkwkwlztwpwuwtwpwqwqwqwqwqwqwqwqwqwnxowtwtwqwrwpwrwpwrwpwrwnwmwswowuwiwkwkwkwkwkwkwswswkwswowswo" -"wswowswowkwkwkwkwswowswowswowswowswowswowswcwtxowswowswowswowswpwrwowswpwrwWwtwtwqwqwqwuwuwuwqwuwswqwqw>wowuw`}q~q|q}qwrwpwrwowtwnwtwo~ izaw]wtwoykwux" -"qwtwswfwjwmwuwuwn}eyaxlwswmwjwjwpwswjwowswmwmwswnzWy]ypwlwtwtwuwswswowrwpwkwrwpwkwkwsyqwrwpwkwkwuwmwkwuwuwuwqwtwuwpwqwqznwqwqzkynwmwrwowuwnwuwuwuwowkw" -"jwnwkxkwGzowswowkwswo{owkwswowswowkwkxlwkwswswswswowswowswowswowjxmwkwswowtwnwuwuwuwpxmwtwlwlwlwiwlytwewtwtwqwswowtxoznwswnxmwswnwuwmwuwnwswowtwtwqwtw" -"twqwtwnwtwtwqwswowswmwmwswowswmwmwkwqwqwtwtwqwrwowuwuwpwuyuwq~own~own~owbwkwuwmznwswmwbwswawuwrwgwtwhwdwuytwXwJwswnxuw=wtwmwswowtxowswqxmwswowswowswow" -"swowswowswnwtwowkwkwkwkwkwkwkwkwkwrwpwtwuwpwqwqwqwqwqwqwqwqwqwnxowtwtwqwrwpwrwpwrwpwrwnwmwswowtwmznznznznznzn~swk{o{o{o{owkwkwkwkwswowswowswowswowswow" -"swowswo}qwuwuwowswowswowswowswowtwnwswowtwUwuwuwowswowswowswowsw@}qx`}q~pzo{pwrwpwrwowtwnwtwow aw_w_}owuwmwuwtwrwswuwewjwkwiwJwkwswmwkwiwp|kwowswmwmws" -"wkwWym}mypwlwszr{owrwpwkwrwpwkwkwqwqwrwpwkwkwtwnwkwtwtwqwtwuwpwqwqwkwqwqwtwiwnwmwrwowuwnwuwuwuwpwuwlwkwmwjwkwHwswowswowkwswowkwkwswowswowkwkwuwmwkwsws" -"wswswowswowswowswowhwnwkwswowtwnwuwuwuwpxmwtwmwkwlwiwmwtydwtwtwqwswowswowtwnwswowkwswnwuwnwtwnwswowtwtwqwtwtwqwtwnwtwtwqwswowswmwmwswowswnwlwkwqwqxuwu" -"xqwrwnyowqwpwiwhwpwuwuwowrwpwuwuwdwkwuwlwlwswo{owkxuwawtxtwszmwtwiwdwuwtwuwXwJwswmwuwKzmwtwlwtxowrwpwtxrxl{o{o{o{o{o{o{owkwkwkwkwkwkwkwkwkwrwpwtwuwpwq" -"wqwqwqwqwqwqwqwqwowtwpwuwswqwrwpwrwpwrwpwrwnwmznwswowswowswowswowswowswowswowswowkwkwkwkwkwkwkwkwkwswowswowswowswowswowswowswcwuwuwowswowswowswowswowt" -"wnwswowtwTymymymymy=wmw^wuwuwmxlxmyowrwowtwnwtwmxmw bwswIwuwmwuwmwuwtwrxswdwjw]wJwkxuxmwlwlwswlwjwowswmwmwswlwSycyawlwswowrwowswpwswowkwjwrwqwrwpwkwkw" -"swowkwqwqwsxowswpwjwswpwswowrwnwmxtxnwlwswpwswmwlwlwjwkwHwswowswowkwswowswowkwswowswowkwkwtwnwkwswswswswowswowswowswowkwswowkwswnxlwswpwtwmxmwjwlwiwTx" -"uxpwtxowswowtwnwswowkwswnynwtwnwswowtwtwqxuwuxqwtwnwtwtwqwswowswmwlwuwnwswowkwjwswo{pwrwmwmwswnwjwiwnymwtwnycwkwuwlwl{mwmwiw_wrwdwtwVwrw*wswmwuw?wtwlw" -"tzqwrwpwtzswkwswowswowswowswowswowswowswnwswpwkwkwkwkwkwkwkwkwswowsxowswowswowswowswowswowrwpwswpxtxpxtxpxtxpxtxnwmwkwswowswowswowswowswowswowswowtxow" -"kwswowswowswowswowkwkwkwkwswowswowswowswowswowswowswlwnxtwowswowswowswowswnxmwswnx >wlw\\wkx`wnwrwoznwtwmxl| gybw^wtwozmwsxpzuxfxlx]wnw_wlxjyn{o{nykwnz" -"mymwkynymwkwewewjwjwrwswqwp{myozn{owizpwrwpwkwkwrwp{owqwqwsxnyowiyowrwozmwlzmwlwswqxsxnwm}qwjxlwGzozmymznynwjzowswowkwkwswowkwswswswswnynzmzowjymxlznx" -"lwswqwrwnwm{mwlwiwHxuxpzmxlymynwswmwnwrwozmxuxo{pwtxn{pzmykwmyo}p{owkyuynwnwrwmwly`w_w_wbwjzo{pwqwnwmwhw_z>zY}M|nwuw2wqwqwryrwqwqyowqwqwqwqwqwqwqwqwqw" -"qwqwqwr{qyo{o{o{o{owkwkwkwkznwsxnymymymymycwuynznznznzmwmwkwuynznznznznznznyuzrymymymymynwkwkwkwjynwswnymymymymybzmznznznznwlzmw hwHwlwSwTw {+qnrmqapmp Kpepgpiuhpephscqfqhqfqhqfqhqfqhqfqhqfqhqixgudxdxdxdxdq]q]q]q]wcqjr" -"bt`t`t`t`taphpgplt`s_s_s_s_q`q]qmsctnqctnqctnqctnqctnqctnqbsktgs_uauauaucq]q]q]q[saqjqbs_s_s_s_sNpms_snqbsnqbsnqbsnqaq`qns_q !p Zp jp#q\\q6q7q l" -"q [sjq Qq -q OqZq]q Cq;q HqWq $rIq`qZq _q iqbqKqFqIq`q hp$q]u JqYpmpLp .p jp ]p Xr`q[r !p Tp\"p\\p6q6q mq Yx Qr -r Ps\\q_s" -" Ipkq:q HqWq $qHq`qZq _q iqbqKqFqIq`q hp$q]t IqYpmpLq /q kq Fq_q[q #s Tp\"q^q6p 1p Vu Rs YsJsMy &v])]2_4^U^ 6^T\\5])]1_2]T\\8^U^ K])]2`4^V^3] " -" U]*\\2a4`V\\8^U^5a F]*\\1\\X\\4^U^=]*\\" -"2a5^U^ 7aV\\4]*\\1a4`V\\8^U^ J]*\\1\\X\\4^V^3\\ " -" S],\\1\\W\\5g8^U^6c F],\\1\\V\\5^U^<],\\2]W]6^U^ 8h3],\\0\\W\\5g8^U^ I],\\1\\V\\5^V" -"^4\\ ;] " -" :\\-]2\\U\\6\\V`7^U^7]U] F\\-]2\\T\\6^U^;\\-]3]U]7^U^ 8\\Va1\\-]1\\U\\6\\V`7^U^ H\\-]2\\T\\6^V^5] =a " -" J] " -" N\\/]2\\S\\7\\T]6^U^7\\S\\ E\\/]2\\R\\7^U^:\\/]3]S]8^U^ 8\\T^/\\/]1\\S\\7\\T]6^U^ G\\/]2\\R\\7^V^6] =c L^ " -" *^ U` O^ )\\S\\ " -" !^$^3\\ E]U\\ K^$^4^ G^$^4] J^$^3\\ #^$^3\\ 4^ B[ " -"&^ Xe S^ (\\S\\ )Z Q^&^3^2]S\\ A\\S\\ K^&^3^ F^&^4_ >]S" -"\\9^&^3^2]S\\ W^&^3^ 6^ Q] M[ ?` ![1^H]?` =]4](\\ %` >b4c Bb ?`2a .a Ib Pb Aa `0`*^ $^.` <^F]F^F]G`G] F\\S\\ ;b %a2a2a2a2a a:]" -".a !^T_ Bg ` Dd2_8n?m7g3]:rD]P]P]@g <] 8] 8] B] 3e J^K^ If7^U^+b@d Fb@f5a Ad4e-] :f Ra0d AaF\\HaF\\HeJ\\?]._0_0_0_0_2\\U\\0tHh@n?n?n?n?].].].]" -"-h:_J]w " -"P[ 9[/a:aQa7[ Wl \"h E]1]T]+\\R\\;[4dL]Ag=])]2])\\ U^1f8c8k;j1`;k7h?n;h9g 5i*b:_8k6kBl=n?l7mD]H]C].].]L_A].`I`H`K]>kAj6kAj9kBuB]H]F]E]E^L_L^" -"R^L^D^I^BrBb7^+b(a D] ;] '] Gd A].].].].] ;] (b:].b #^Q] Dj !a Ff3_8n?m8i4]:rD]P]P]Bk ?_ 9] 9_ C]&[0f I]K]=]0g7^U^-fC\\S] IfBf6c B[" -"S]5[S].] `K]>k]*]3]W]6^U^._V_;]Wa5]*]2\\V\\6]Wa7^V^ I]*]2\\V\\5^V^2]7]+^V^ @]W\\=v P[ 9\\1c_8m:`R`Cn?n?l9`QaE]H]C].].]M_@].aKaH`K]?`S`Bk8`S`Bk;_R_BuB]H]F]E]D]MaM]P]L]B^K^ArB]1]&])c D] <] '] G] :].].].].] " -";] (^6]*^ #]P^ E^P\\ V^ H^T^4_8n?m:`S`6]:rD]P]P]C`S` Aa :] :a D]&[1^S\\ I^M^=]0^R[7^U^/^R^EZO\\ L^R^ N]U] :],\\0] \\H]B\\H]=\\M]>" -"]._0_0_0_0_0_/uK`R`Cn?n?n?n?].].].]-n@`K]?`S`>`S`>`S`>`S`>`S` H`ScE]H]C]H]C]H]C]H]E^K^@],^T^5],]1\\V\\6\\U`7^V^6]U\\ F],]2\\T\\6^U^=],]2\\U\\6^U^-e9\\U`4],]1\\" -"V\\6\\U`7^V^ H],]1\\V\\5^V^3]6]+^V^ B`1`1`1`1`6]W]>u P[ 9]2e>eUf;^ %q $^O\\ F]1]T],]S];[5]T]N\\@]P[=]*]0]2ZR\\RZ $]2]P]<_W]8]N]\\H\\A\\H\\<\\M\\=]/a2a2a" -"2a2a1_/]V];_M]C].].].].].].].]-]ObBaL]@^M^@^M^@^M^@^M^@^M^ J^N`D]H]C]H]C]H]C]H]E^K^@]-^Q]5].]1\\T\\7\\S]6^V^5c E].]2]S\\7^U^<].]2\\S\\7^U^,a6\\S]2].]1\\T\\7\\S" -"]6^V^ G].]1\\T\\6^V^4]5]+^V^ De6e6e6e6e9\\U\\>u P[ :_3f@gVf<_ &r $]M[ F]1]T],\\R]>d<^T^P]A^OZ=]+].]4]T\\T] &^3^P^=[S]8[K].]4\\X];],]!]<]N]>^O^ " -" 8ZM^3`P`Ba9]M^=^J\\C]K_B].],^H\\E]H]C].].]O_>].aKaHaL]A^K^D]N^<^K^D]N^>]JZ6]6]H]E]G]C]MaM]O^P^@^M^-^A]1]&]+_W_ D] >] '] H] 9] B].] ;] )]4](]" -" %]N]:c6] G] J^P^7a8_1],^K^;c=]H]D]P]P]E^K^ Ee <] \\I]A\\I]<\\N]=]/a2a2a2a2a2a1]U]<" -"^J\\C].].].].].].].]-]K_CaL]A^K^B^K^B^K^B^K^B^K^ K]K^D]H]C]H]C]H]C]H]D^M^?]-]P]4]0]1\\R\\ Ha C]0]2]R] E]0]2\\Q\\ 9c 9]0]1\\R\\ !]0]1\\R\\ ?]4] Di:i:i:i:i" -";\\6]G] P\\ :`5g@gWh>a (_ J]KZ F]1]T],\\R\\?h>]R]P\\@]1]+].]3^V\\V^.] T]2]N]5]8ZJ]-]6]X];]-]!^=]L]?]M] *]5_J_Ec:]L^>]H[C]I^C].],]F[E]H]C].].]" -"P_=].]X]M]X]HbM]A]I]D]M]<]I]D]M]?]%]6]H]E]G]C^NaN^N]Q^>^O^-^@]0]'],_U_ &] '] H] 9] B].] ;] )]4](] %]N]:d7] F] K]N]8c8^1],]I]>i@]H" -"]D]P]P]E]I] Fg =] =g G]&[2] <]O];]1] 1\\F\\=\\ Q\\F\\ S\\Q\\+]3\\.] IeU\\ M\\3\\N\\ ?\\I\\@\\I\\=]M\\<]0c4c4c4c4c3a1]U]<]H[C].].].].].].].]-]J_DbM]A]I]B]I]B]I]B]I]" -"B]I] L]J_E]H]C]H]C]H]C]H]C^O^>].]N] .] '`X_ I] FbWa=bWa=bWa=bWa=bWa<\\6^I^ ?Z2[ :a5gAiXh?c *^ H] 7]1]T]-]S]Aj>]R]Q]@]1]," -"],\\1^X\\X^,] T]3]L]6]'].]7]W];]-]!]<]L]?]M^ +]6^F^F]W]:]K]?]FZC]H^D].]-]DZE]H]C].].]Q_<].]X]M]X]H]X]M]B]G]E]M^>]G]E]M^@]%]6]H]E^I^B]O^X]O]M^R^=]O^" -"-^@]0]']-_S_ '] '] H] 9] B].] ;] )]4](] %]N]:e8_ H] L]M]8]W]7^2]-]G]AmB]H]D]P]P]F]G] Hi >] >i J[3] ;^Q^;]1] 2\\RbT\\Ge R\\VdR\\ T\\" -"Q\\+]4\\2a IfU\\ M\\3\\N\\ ?\\J\\?\\J\\AaM\\ G]W]4]W]4]W]4]W]4]W]4c3^U]=]FZC].].].].].].].]-]H]D]X]M]B]G]D]G]D]G]D]G]D]G]A[H[B]J`E]H]C]H]C]H]C]H]B]O^>g8]N] " -" 1]T_ 3[ 9] G_O^?_O^?_O^?_O^?_O^=\\5]I^ @\\3[ ;c6gAy?d7`8]L]7^7]L]>^ H] 6]1]T]-]S]B_W[U]>]R]R]?]1],],]0d*] T]3]L]6]'].]7\\V];]" -".] ]<]L]@]K] 7Z PZ X]7^D^G]W]:]K]?]/]G]D].]-]/]H]C].].]R_;].]X^O^X]H]X^N]B]G]E]L]>]G]E]L]@]%]6]H]D]I]A]O]W]O]L^T^<^Q^-^?]0]'].^O^ Sb7]U`2b4`U]8a8])`" -"7]T_ M].]%_O_@_2`0`3`/_3c9] )]4](] N_6]N]3^7a/c0_ <^ D[U^ Ga N]L]9]W]6^3]-]G]B`W]W`C]H]D]P]P]F]G] I_X]X_ ?] ?_X]X_ Nb7]2ZFZ=]Q]:]0] 3[SfU[I" -"g R[UfS[ T\\Q\\+]5]2a IfU\\ M\\3\\N\\ ?\\K]?\\K]AaN] G]W]4]W]4]W]4]W]4]W]4]W]3]T]=]/].].].].].].].]-]G]E]X^N]B]G]D]G]D]G]D]G]D]G]B]J]C]KbF]H]C]H]C]H]C]H]B" -"^Q^=j;]P_9b3b3b3b3b3b3bN`Bb3a2a2a2a V_2_2`1`1`1`1` ;aU] :]U` S^T]U^A^L^A^L^A^L^A^L^?]5]I] @^5\\ ]R]R\\>]1],],].`(] U^3]L]6]'].]8]V];].]!^<]L]@]K] :] P]#^8^A]I^W^;]K]@].]G^E].].].]H]C].].]S_:].]W]O]W]H]W]N]C]E]F]L]?]E]F]L]@]%]6]H]D]J^A]O]W]O]" -"L^U^:^S^-^>]0^(]/^M^ Wh:]Wd6f8dW]:e>h2dW]?]Vd<].].]O_>].]WdScK]Vd8f;]Wd7dW]?]Wa6h>h6]L]B]I]A]P`P]K^L^B^K^@l4]4](] PdU]A]N]2^8e5g;]Vd?^J^8]6]L] E]V`" -">pA]S]S]:e6kDo>]L]:^W^6^4].]E]D_U]U_D]H]D]P]P]G]E] K_W]W_ @] @_W]W_ Qf9]3\\H\\>^S^:]0_ 6[ThT[K]Q\\ S[T\\R]S[ U]S]+]6],] ?]L]@fU\\ M\\3\\N\\ ?\\K\\>\\K\\;]O\\ G" -"^W^6^W^6^W^6^W^6^W^5]W]4^T]>].].].].].].].].]-]G^F]W]N]C]E]F]E]F]E]F]E]F]E]D_L_E]K]W]F]H]C]H]C]H]C]H]A^S^^K^ O]S]S]B]I]B]I]B]I]B]I]@]5^K^ @]4[ ;f8gAyAg] F] 6]1]T]-\\R\\B]T[6]R]S]>^2]-]*\\.`(] U" -"]2]L]6]'].]9]U];].]!];]L]@]K] =` P`'^7]?\\I]U];]K]@].]F]E].].].]H]C].].]T_9].]W]O]W]H]W^O]C]E]F]L]?]E]F]L]@]%]6]H]C]K]@^P]W]P^K^V^9]S]-^=]/](]0^K^ Xi" -";]Xf9h9fX]h6]L]A]K]@^Q`Q^J^N^@]K]?l4]4](] QfW^A]O^1]6f9h;]Xg@_K]7]6]L]=]G]C^Wc@pA]S]S]]L]:]U" -"]5^5].]E]E^S]S^E]H]D]P]P]G]E]@Z+]V]V^-Z4]5ZKZ:]V]V^ Sh9]4^J^>]S]9]._ 8[U_Q[T[L]P\\ S[T\\Q]T[ T]U]*]7]*] @]L]@fU\\ M\\3\\N\\ ?\\L]>\\L]:]Q]:]1]U]6]U]6]U]6]" -"U]6]U]6^W^5]S]>].].].].].].].].]-]F]F]W^O]C]E]F]E]F]E]F]E]F]E]C_N_D]L^W]F]H]C]H]C]H]C]H]@]S];]P_=]S^8i:i:i:i:i:i:iVgIh9h9h9h9h<].].].]'d<]Xg:h9h9h9h9h" -"0^8k?]L]?]L]?]L]?]L]A]K]>]Xf>]K] O]R]R]D]G]D]VZOZV]D]KZV]D]G]A]4]K] @]3[ j=]L]8`7]N]?] F^ 6]1]T]5uI]T[6]R]S\\<^3]-]*]1d*] U]3]J]7]']" -".]9\\T];].\\Ua-^;]L]@]K^?].] Uc Pc+_8]>]J]U];]K]@].]F]E].].].]H]C].].]U_8].]W^Q^W]H]V]O]C]E]F]L]?]E]F]L]@^&]6]H]C]K]?]Q^V]Q]I^X^8^U^.^<]/](]1^I^ ]R_h6]L]A]K]?]Q`Q]H^P^?]K]?l4]4](] R^U^W]@]O]0^7g;_S];bT^@`L]8_7]L]>]E]E^W]V]@pA]S]S]" -"=_T_].].].].].].].].]-]F]F]V]O]C]E]F]E]F]E]F]E]F]E]B_P_C]L]V^G]H]C]H]C]H]C]H]@^U^;]N^>]T]6]R_;]R_;]R_;]R_;]R_;]R_;]R" -"_X_T^K_R\\:_S^;_S^;_S^;_S^=].].].]*h=bT^;_T_;_T_;_T_;_T_;_T_1^9_T`>]L]?]L]?]L]?]L]A]K]>aT_?]K] P]Q]R]E]F]E]V\\Q\\W]E]K\\W]E]F]A]4^L] A^@ZN\\ =i8e@yCk?^R^" -"=]L]9b8]O^?] Im B]1]T]5uI]T[6]S^T]<^3]-]*]3^X\\X^,] V^3]J]7](^/]9]T];e7]We/]9]N]?]K^?].] Wd Nd._8]O`U\\T\\K]S]<]L^A]-]F^F].]/]-]H]C].].]V_7].]V]Q" -"]V]H]V^P]D]C]G]L]@]C]G]L]?^']6]H]C^M^?]Q]U]Q]Ic6^W^._<]/^)]2^G^ !ZM^=`Q^=^NZ;^Q`>^P^=].^Q`?`Q^>].].]R_;].`R^X\\R^M`Q^=^P^>`Q^=^Q`?`1]MZ;].]L]A^M^?]Q`Q]" -"G^R^>^M^1^4]4](] D]P^A]R^X]@]P^/]9^Vb=^NZ;`Q^AaN^8_7]L]>]E]F^V]U]>]P]>]S]S]>^P^>`T`7]6]J]<]S]5^6]/]C]G]Q]Q]F]H]D]P]P]H]C]C^&]TZ,^7]7^N^6]TZ H]/^U[TZ9" -"]2n;]U]8]0d <[U]F[M\\P]2[R[ M[S\\P\\S[ Tb(]9]'\\ @]L]@fU\\ M\\3]P]9[R[1\\M\\<\\M\\7\\R\\8]2]S]8]S]8]S]8]S]8]S]7]U]6]R]?]-].].].].].].].]-]F]F]V^P]D]C]H]C]H]C]H]" -"C]H]C]B_R_C]L]T]G]H]C]H]C]H]C]H]?^W^:]M]>]U^6ZM^].].].]+i=`Q^=^P^=^P^=^P^=^P^=^P^2^:^P^>]L]?]L]?]L]?]L]" -"A^M^>`Q^@^M^ P]Q]Q]F]E]F]W^S^W]F]L^W]F]E]B]3]M^ B^B^O[ =k8d?xClA^P^>]L]9]X]8^P]>\\ Hl A] 9uI]T[5]T]T]:^ =]*]5^V\\V^.] V]2]J]7](]/^:]S];h:]Xg0]" -"9^P^?]K^?].]!e Je2_7\\PdW\\S\\L]S]<]M^@]-]E]F].]/]-]H]C].].]X_5].]V]Q]V]H]U^Q]D]C]G]L]@]C]G]M^?`)]6]H]B]M]>]Q]U]Q]Hb5c-^;].])] B]=_O]=].]O_>]N^>].]O_?_" -"O]>].].]S_:]._P`P]M_O]=]N]>_O]=]O_?_1]-].]L]@]M]>]RbR]G^R^=]M]1^3]4](] FaSaD^Qa?]R_.]9]R`>]._O]>^N]8`7]L]>]E]G^U]U^?]P]>]S]S]>]N]>^P^7]6]J]<]S]4^7]/]" -"C]G]Q]Q]F]H]D]P]P]H]C]D_&]&_8]8_N_7] B]/]T[3]1l:^W^8]1]W` >\\U\\E\\N\\P]3\\S\\ N\\S\\P\\S\\ S_']:]&\\ @]L]@fU\\ M\\2\\P\\8\\S\\2\\N]<\\N]7\\S]8]2]S]8]S]8]S]8]S]8]S]8]S]" -"7]R]?]-].].].].].].].]-]E]G]U^Q]D]C]H]C]H]C]H]C]H]C]A_T_B]M]S]G]H]C]H]C]H]C]H]>c9]M^?]U]'].].].].].].`O^N].]N^>]N^>]N^>]N^?].].].],_R^>_O]=]N]=]N]=]N]" -"=]N]=]N]2^:]O_?]L]?]L]?]L]?]L]@]M]=_O]?]M] O\\P]Q]F\\D]F\\U^U^V]F\\L^V]F\\D]B]3]M] RuJ`O[ >m9c>wCmA]N]>]L]9]X]7]P]?] Im A] 2\\R\\A]T[5^V^T\\:` ?](\\6]T" -"\\T]/] V]2]J]7])^1_9]S];i;bS^2^8^S_>]K^?].]$e@u@e6_7]QfX\\S\\M^S^=]N^?]-]E]F].]/]-]H]C].].c4].]U]S]U]H]T]Q]D]C]G]M^@]C]G]M]=c-]6]H]B]M]>^R]U]R^G`4c.^:]" -".])] B]=^M]?^/]M^?]L]>]/]M^?^N^?].].]T_9].^O_O^N^N^?]M^?^M]?]M^?^0]-].]L]@]M]>^S]X]S^F^T^<^O^2_3]4](] GcUcE]Pa?]Vb-]:]O_?].^N^>]O^8a8]L]?]C]H]T]T]?" -"]P]>]S]S]?]L]@^N^8]6]J]=^S^4^8]/]C]H^Q]Q^G]H]D]P]P]H]C]E_%]%_9]9_L_8] B]0^T[3]0_T_>cWc=]1]U_ ?[U\\C[N]R^4]T] N[R\\Q]R[ 'uG]&] @]L]?eU\\ M\\2]R]8]T]3\\N\\;" -"\\N\\7]S\\7]3^S^:^S^:^S^:^S^:^S^9]S]8^R]?]-].].].].].].].]-]E]G]T]Q]D]C]H]C]H]C]H]C]H]C]@_V_A]N]R]G]H]C]H]C]H]C]H]>c9]L]?]U]'].].].].].]._M]O^/]L]?]L]?]L" -"]?]L]?].].].]-^O]>^N^?]M^?]M^?]M^?]M^?]M^ I]O`?]L]?]L]?]L]?]L]@^O^=^M]@^O^ P]P]P\\G]C\\G]T^W^T\\G]M^T\\G]C\\B]3^O^ RuJ[X]P[ >o=\\XaX]BwDoC]L\\>]L]:^X^8]P]?" -"] E] 5] 3]S]A^U[4dT];b @](]6ZR\\RZ.] V]2]J]7]*^7d8]R];]R_]-]E]Fm>k=]-rC].].b3].]U]S]U]H]T^R]D]C]G]M]?]C]" -"G]N^^M]?].]M^?]L]>]/]M^?^M]?].].]U_8].^N^N]N^M]?]L]?^M]?]M^?^0]-].]L]@^O^=]S]X]S]D^V^:]O]2_2]4](] H\\U^W]U\\E]Pa?" -"]Vb-];]M^?].^M]>^P]7a8]L]?]C]H]T]T]?]P]>]S]S]?]L]@]L]8]6p=]Q]3^9]/]C]H]P]P]G]H]C]Q]Q]G]ViV]F_$]$_:]:_J_9] B]0]S[3]0]P]>o=]2]S_ @[U\\C[M]T_5^U^;u O[R\\R]" -"Q[ 'uH]/ZQ] ?]L]?eU\\ M\\1]T]7^U^4\\O]O]I\\O]T`MZQ]S]O]E]3]Q]:]Q]:]Q]:]Q]:]Q]:^S^9]QmO]-m>m>m>m>].].].]1hL]G]T^R]D]C]H]C]H]C]H]C]H]C]?_X_@]O]Q]G]H]C]H]C]" -"H]C]H]=a8]L]?]U]&].].].].].].^M]O].]L]?]L]?]L]?]L]?].].].].^M]?^M]?]L]?]L]?]L]?]L]?]L] I]Pa?]L]?]L]?]L]?]L]?]O]<^M]?]O] O]P]P\\G]C\\G]ScS\\G]N^S\\G]P]P\\B" -"]2]O] QuF]Q[ >oAqDuDqD]L]?]L]:^X^8^R^?\\ D] 5] 3]S]@`X[3bS\\R^G]W^N] P](].\\&] W]1]J]7]*^7c8]Q];ZM^=`O^4]4d:]M_?].])d:u:d=_5\\R]O^R\\N]Q]=j<]-]E]F" -"m>k=]-rC].].a2].]U^U^U]H]S]R]D]C]G]N^?]C]G]P_:g3]6]H]A]O]<]S]S]S]E^1_.^8]-]*] A]>^M]?]/^M^?]K]?]0^M^?]L]?].].]V_7].]M]M]N]L]@^L]?^M]@^M^?]/]-].]L]?]" -"O]<]S]X]S]C^X^9]O]2^1]4](]0_IZ O[R\\X]S\\G^O_>]Vd9_U];]L]?].]L]=]P]8]X^9]L]?]C]I^T]S]@]P]>]S]S]?]L]@]L^9]6p=]Q]3^9]/]C]H]P]P]G]H]C]Q]Q]G]ViV]G_#]#_;];_H" -"_:] B]0]S[3]0\\N\\>o=]2]Q^ A[U\\C[LcX\\6]T]9u O[RfP[ 'uIf7e >]L]>dU\\<] :f5d4]T]:fT\\O^NfT\\UdOeR\\O^F^3]Q]:]Q]:]Q]:]Q]:]Q]:]Q]:^QmO]-m>m>m>m>].].].]1hL]G]S]R" -"]D]C]H]C]H]C]H]C]H]C]>d?]P^Q]G]H]C]H]C]H]C]H]<_7]L]?]U^'].].].].].].^L]P].]K]@]K]@]K]@]K]@].].].].]L]?]L]@^L]@^L]@^L]@^L]@^L] I]Q]X^@]L]?]L]?]L]?]L]?]" -"O]<^M]?]O] O\\WmX]H\\WmX]H\\QaR]H\\N^R]H\\O]P]C]2]O] QuF]R\\ ?qCsDtDrE]L]?]L]:]V]7]R]>x '] 5] 3\\R\\?e3^R\\SbJ^V^O] P](].\\&] W]1]J]7]+^6e:]Q]-^>_M]5^6" -"h<^O` Qe8u8e@^5]R\\M]R\\O^Q^>m?]-]E]Fm>k=]KdFrC].].b3].]T]U]T]H]S^S]D]C]G]P_>]C]Gk6f5]6]H]A^Q^<]S]S]S]F_1_/_8]-]*] A]>]K]A].]K]@]J]?]0]K]?]L]?].].]W_" -"6].]M]M]N]L]@]J]@]K]A]K]?]/^.].]L]?]O]<]T^W]T]C^X^9^Q^3^1]3]']3dN\\ P\\R`Q[G]N_>]Q`;bW];\\K^?]/]L]=]Q^8]W]9]L]?]C]I]S]S]@]P]>]S]S]@]J]B^L^9]6p>^Q^4^9]/]C" -"]H]P]P]G]H]C]Q]Q]G]ViV]H_\"]\"_<]<_F_;] B]1]R[3]1]N]8a6]2]P^ B[U\\C[K`V\\7]T]8u O[RdN[ 'uIf5a <]L]=cU\\<] :f3`1]T];fU\\N^NfU\\T[S]NaQ\\N^G^3^Q^<^Q^<^Q^<^Q^<^Q" -"^;]Q]:]PmO]-m>m>m>m>].].].]1hL]G]S^S]D]C]H]C]H]C]H]C]H]C]=b>]P]P]G]H]C]H]C]H]C]H]<_7]L]?]U_(].].].].].].]K]Q].]J]A]J]A]J]A]J]@].].].].]L]?]L]@]J]A]J]A" -"]J]A]J]A]J] K]P\\V]@]L]?]L]?]L]?]L]?^Q^<]K]@^Q^ O\\WmX]H\\WmX]H\\P_Q]H\\O^Q]H\\O]P]C]2^Q^ D^<]R[ >qDuEsCqD]L]?]L]:]V]7]R]>x '] 5] 3\\R\\=f+]TdL^T^P] P]" -"(].\\2u *]1]J]7],^-_=]P],]>_M]5]7_R^<^Qa Sd .dC^4\\R]M]R\\O]O]>]N_@]-]E]F].]/]KdF]H]C].].]X^4].]T]U]T]H]R]S]D]C]Gk=]C]Gj1c6]6]H]@]Q];^T]S]T^Ga1].^7]-]*" -"] Lh>]K]A].]K]@]J]?]0]K]?]L]?].].]X_5].]M]M]N]L]@]J]@]K]A]K]?]._0].]L]>]Q];^U]V]U^Bb7]Q]3^1^3]'^6iS^ P[P^P[G]N_>]N^=dX]<]J]>^1]L]=^R]8^W]9]L]@]A]J]S" -"]S]@]P]>]S]S]@]J]B]J]9]6]J]>]O]5^8]/]C]H]P]P]G]H]B]R]R]F]C]Iz<]]K]@]" -"O[X\\I`3]O]<]O]<]O]<]O]<]O]<]O];]P]?]-].].].].].].].]-]E]G]R]S]D]C]H]C]H]C]H]C]H]C]<`=]Q]O]G]H]C]H]C]H]C]H];]6]L]?]T_4h9h9h9h9h9h9hK]Q].]J]A]J]A]J]A]J]" -"@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]Q\\U]@]L]?]L]?]L]?]L]>]Q];]K]?]Q] N\\WmX]H\\WmX]H\\P_Q]H\\P^P]H\\O]P]C]1]Q] C]:]S[ ?sEvEqAoC]L]?]L];^V^8^T^>x " -" '] 5] 4]S]]K]A].]K]@p?]0]K]?]L]?].].b3].]M]M]N]L]@]J]@]K]A]K]?].c4].]L]>]Q]:]U]V]U]@`6^S^4^5b2]&b^Ua<]J]=" -"c7]L]<]S^8]V^:]L]@]A]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]?^O^7^7]/]C]H]P]P]G]H]B]R]R]F]C]Iz<]\\I\\@\\O\\X\\J`3^O^>^O^>^O^>^O^>^O^=]O]<^P]?]-].].].].].].].]-]E]G]R^T]D]C]H]C]H]C]H]C]H]C];^<]R]N]G]H]C]H]C]H]C]H];]6]L]?]S`8j;j;j;j;j" -";j;|Q].pApApAp@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]R]U]@]L]?]L]?]L]?]L]>^S^;]K]?^S^ N\\WmX]H\\WmX]H\\QaR]H\\Q^O]H\\O]P]C]1^S^ D]9]T\\ ?sFwDo?nC]L]?]L];" -"]T]7]T]=] Hj ?] 4]S]8d/]T]T]N^R_R\\ O](] =u Se =]0]J]7].^(]?]O]+]?^K]7]7]L]]K]A].]K]@p?]0]K]?]L]?].].a2].]M]M]N]L]@]J]@]K]A]K]?]-f8].]L]>^S^:]U]V]U]?^4]S]4^4`0]$`<^Si O[O" -"\\O\\H]N^=]M^@^S`<]J]=c7]L]<]S]8^U]:]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]?]M]7]6]/^E^H]P]P]G]H]A]S]S]E]C]Iz<]]M]>]M]>]M]>]M]>^O^=]O]?]-].].].].].].].]-]E]G]Q]T]D]C]H]C]H]C]H]C]H]C]<`=]S]M]G]H]C]H]C]H]" -"C]H];]6]M^?]R`;l=l=l=l=l=l=~Q].pApApAp@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]S]T]@]L]?]L]?]L]?]L]=]S]:]K]>]S] M]P]P\\G]C\\G]ScS\\G]S^N\\G]P]P\\B]0]S] D]" -"7\\T[ >sFwCn?mB]L]?]L];]T]7]T]=] Hi >] 4]S]7[Xa1]T^T^O]P_T] O](] =u Se =]0]J]7]/^'^A]N]+]?^K]7]8^L^]K]A].]K]@p?]0]K]?]L]?].].b3].]M]M]N]L]@]J]@]K]A]K]?]+e9].]L]=]S]9]V]T]" -"V]@_4]S]5_4b2]&b<\\Nd M[O]P\\H]N^=]L]@]Q_<]J]?e7]L];]T]8]T]:]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]?]M]8^6].]E]G]P]Q^G]H]A^T]T^E]C]Iz<]]M]>]M]>]M]>]M]>]M]>^O]?]-].].].].].].].]-]E]G]Q^U]D]C]H]C]H]C]H]C]" -"H]C]=b>]T]L]G]H]C]H]C]H]C]H];]6]M]>]Qa>`P]>`P]>`P]>`P]>`P]>`P]>`PoQ].pApApAp@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]T]S]@]L]?]L]?]L]?]L]=]S]:]K]>]S] " -"L\\P]P\\F\\C\\F\\T^W^T\\F\\T^M\\F\\C\\B]0]S] E^7]U[ >sFwBl=kA]L]?]L]<^T^8^V^=] Ij >] ]K]A].]K]@],]0]K]?]L]?].].c4].]M]M]N]" -"L]@]J]@]K]A]K]?](d;].]L]=]S]9^W]T]W^@`5^U^5^/_3]'_8ZJ` K[O]P\\H]N^=]L]@]P];]J]@_0]L];]U^9^T^;]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]@^M^:^5].]E]F]Q]Q]F" -"]H]@^U]U^C]E]G_\"]\"_BZT]TZB_F_;] B]1]R[3]1\\L\\?o I_S] A[U]F[ V]T] W] N[S\\R]R[ S] ]L]6\\U\\ ']T]/\\O\\V\\@\\H\\A\\O\\V\\M_0o@o@o@o@o?m>l>].].].].].].].].]-]F^" -"G]P]U]C]E]F]E]F]E]F]E]F]E]=d?^V]L]F]H]C]H]C]H]C]H];]6]N^>]O`?]M]>]M]>]M]>]M]>]M]>]M]>]M]?].].].].]-].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J] K]U]R]@]L]?]L]?" -"]L]?]L]=^U^:]K]>^U^ L\\P]Q]F\\D]F\\U^U^V]F\\U^M]F\\D]B\\/^U^ OuD]V[ =sFwBk;i@]L]?]L]<]R]7]V];] F^ Nu=[T^3]S]R]O]N_V\\ N](] 1] ].]L]6]1_%]Aq0]>]K]" -"8]7]J]/] Md:u:d>]3\\R\\K\\S\\Po@]J]A].]F]E].].]E]F]H]C].].]S^9].]RaR]H]P^V]C]E]F].]E]F]M],]8]6]H]>]U^8]W^Q^W]H^U^4]2^3]+],] R^M]>]K]A].]K]@],]0]K]?]L]?" -"].].]X_5].]M]M]N]L]@]J]@]K]A]K]?]$`;].]L]=^U^8]W]T]W]@b5]U]5^,]3]'] J\\Q_Q[G]N^=]L]A]O];]J]@].]L];]U]8]R];]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]5]L]?]K];" -"^4].^G^F]Q]Q]F]H]?_W]W_B]E]F_#]#_B\\U]U\\B_H_A\\U]U[ H]1]R[3]1]N]?o H`V] @[T]G[ U]T] X] N[S\\Q]S[ S] ]L]6\\U\\ (]T]/]P\\U\\A]I]B]P\\U\\M^/o@o@o@o@o@o@m>].]" -".].].].].].].]-]F]F]P^V]C]E]F]E]F]E]F]E]F]E]>_X_?]W^L]F]H]C]H]C]H]C]H];]6]P_=]M^@^M]?^M]?^M]?^M]?^M]?^M]?^M]?].].].].]-].].].]/]J]@]L]@]J]A]J]A]J]A]J]" -"A]J] K]U\\Q]@]L]?]L]?]L]?]L]<]U]9]K]=]U] K]Q]Q]F]E]F]W^S^W]F]W^L]F]E]B\\.]U] NuC\\V[ =eXZXdFgXhAi9h@]L]?]L]<]R]7]V];] E] Nu=[S]3\\R]R]O]M_X\\ M](" -"] 1] ].]L]6]2_$]Aq0]>]K]8]7]J]/] Ke=u=e<]3\\R\\K\\S\\Po@]J]A].]F]E].].]E]F]H]C].].]R^:].]RaR]H]O^W]C]E]F].]E]F]M^-]8]6]H]>]U]7]W]O]W]I^S^5]3^2]+],] R" -"]L]>]K]A].]K]@],]0]K]?]L]?].].]W_6].]M]M]N]L]@]J]@]K]A]K]?]\"_<].]L]<]U]7]W]T]W]Ac5^W^6^+^4](] H[R\\X]S\\G]N^=]L]A]O];]J]A^.]L]:]W^9^R];]L]@]O]O]J]S]S]@" -"]P]>]S]S]@]J]B]J]9]5]L]?]K];^4]-]G]D]R]R]E]H]>kA]E]E_$]$_B^V]V^B_J_A^V]V] I]1]R[3]0\\N\\>o G`X] ?\\U_Q[T\\ T]T] ] N\\T\\Q]T\\ S] ]L]6\\U\\ )]T].\\P\\T\\A\\I]A" -"\\P\\T\\N^.o@o@o@o@o@o@m>].].].].].].].].]-]F]F]O^W]C]E]F]E]F]E]F]E]F]E]?_V_@]W]K]F]H]C]H]C]H]C]H];]6k<]L^A]L]?]L]?]L]?]L]?]L]?]L]?]L]?].].].].]-].].].]/" -"]J]@]L]@]J]A]J]A]J]A]J]A]J] K]V\\P]@]L]?]L]?]L]?]L]<^W^9]K]=^W^ J]R]R]D]G]D]W\\Q\\W]D]W\\L]D]G]A\\.^V] NuC]W[ ]K]9]6]J]/] He@u@e H\\R]M]T]Q^J]A]J]@]/]G^E].]-]F]F]H]C].].]Q^;].]Q_Q]H]N]W]B]G]E]-]G^F]L]-]8]6]I^>^W^7]" -"W]O]W]I^R^6]4^1]+],] R]M^>^M^@]/^M^?]-]0^M^?]L]?].].]V_7].]M]M]N]L]@^L]?^M^A^M^?] ]<].]L]<]U]7]X]R]X]B^W^5]W]6^)]4](] H\\T]W]U\\F]O_=]L]A]P^;^L^A]-]L" -"]:]W]8]P]<]L]@]O]O]J^T]T]?]P]>]S]S]@^L]A^L]8]5]L]@^J]=^3]-^I^D^S]S^E]H]]G]C_%]%_A_W]W_A_L_@_W]W_ J]0]S[3]0]P]5]4],b =[ThT[ R]T]!] M[T\\P]U[ R] ]L" -"]6\\U\\ *]T].]P[S\\B]J]A]P[S\\N].^J]B^J]B^J]B^J]B^J]B^K^A]M]=]/].].].].].].].]-]G^F]N]W]B]G]D]G]D]G]D]G]D]G]?_T_AbK]E]I^C]I^C]I^C]I^;]6j;]K]A]M^?]M^?]M^" -"?]M^?]M^?]M^?]M_?].].].].].].].].]/]J]@]L]@^L]@^L]@^L]@^L]@^L] J^X]Q]?]L]?]L]?]L]?]L];]W]8^M^<]W] I]R]S]C]H]C]VZOZW]C]VZL]C]H]@\\-]W] MuC]X[ ;cWZWbDe" -"WZXe>e6e>]L]?]L]=]P]8^X^:] F^ H\\R\\5[S]5]Q]R]O^L` K]*] 0] !^.]L]6]4_\"]2],^>^M]8]6]J]0] DeCuCe E]R\\M]T\\P]I]A]J]@]/]G]D].]-]F]F]H]C].].]P^<].]Q" -"_Q]H]N^X]B]G]E]-]G]E]L^.]8]5]J]<]W]6^X]O]X^J^Q^6]5^0]+^-] R]M^>^M]?].]M^?]-]/]M^?]L]?].].]U_8].]M]M]N]L]?]L]?^M]?]M^?] ]<].]M^<^W^6aRbB^V^6]W]7^(]4]" -"(] GcUcE]P_=]L]A]P]9]L]@]-]L]:^X]9^P]<]M^@]P^O]I]T]T]?]P]>]S]S]@^L]@]L]8]5]M]?]I]>^2],]I]B_U]U_D]H]:c<]G]B_&]&_?_X]X_?_N_>_X]X_ I]0]S[3]0_T_5]4]+` ;[" -"SfU[ P^U^#] L[U\\P]V[ Q] ]M^6\\U\\ ,^U^-\\P\\S\\B\\J]@\\P\\S\\N].]I]B]I]B]I]B]I]B]I]B]I]B^M]=]/].].].].].].].]-]G]E]N^X]B]G]D]G]D]G]D]G]D]G]@_R_A`J]D]J]A]J" -"]A]J]A]J]:]6g8]K]A]M^?]M^?]M^?]M^?]M^?]M^?]M_?].].].].].].].].].]L]?]L]?]L]?]L]?]L]?]L]?]L]3^;aP]?]M^?]M^?]M^?]M^;]W]8^M];]W] H]S]T^B]J^B]J^B]J^B]J^@" -"\\-]W] G^1_ :aW[V`BcW[Wc]N]<]P]7]X]8] F]KZ X]S]5[S]5\\P]R]N]K_ K]*] 0] !],]N]5]5_\"]1],]<]M]9^6^L^0] Ad Nd A\\R]O^U\\P^I^B]K^?]H[C]H^D]" -".],]G]F]H]C].].]O^=].]P^Q]H]M]X]A]I]D],]I^E]K]AZH^8]5]J]<]W]5bObJ^O^7]6_0]*]-] R]M^>^M]?^/]M^?^.]/]M^?]L]?].].]T_9].]M]M]N]L]?]L]?^M]?]M^?] ]<].]M^;" -"]W]5aRaB^U^6c8_(]4](] FaSaD]P_=]M]@]P]9]L]@]-]L]9b9]O^=^N^?\\P_Q]H]T]T]?]P]=]T]T]?^L]@]L]8]4]N]@^I^?]1],^K^A`W]W`C]H]7]8]I]@^&]&^=i=^N^^P^=^P]7]X]8_ H^M[ F] 6]S]>ZQ[T^6]P]S^N^K^ K]*] 0]:] 8]0],]O^5]6_2ZI]1]-^<^O^9]4]L]0]<].] Uc Pc1]2\\Q^S`W^P]G]B]K]" -">^J\\C]I^C].],^H]F]H]C].].]N^>].]C]H]MbA^K^D],^K^D]K^B[I]7]5^L^_O]=].]O_>].].]O_?]L]?].].]S_:].]M]M]N]L]>]N]>_O]=]O_?] ]<]-" -"]O_;]X^5aRaC^S^6a8_']4](] D]P^B^Ra>^N]@]Q]7]N]?^.]L]9a8]N]=^N^?]Q_Q]G]U]U]>]P]=]T]T]?_N]>]N]7]4^P^@]G]@^1]+^M^?mB]H]7]8^K^?\\%]%\\;g;\\L\\:g G]/]T[3]2n7]" -"4]'^ <\\F\\ M\\S\\ J\\F\\ L^N^6\\U\\ ,\\S\\-]OhG]K]@]OhQ]LZ=]G]D]G]D]G]D]G]D]G]D]G]D^L]<^J\\C].].].].].].].]-]J_D]MbA^K^B^K^B^K^B^K^B^K^A_N_B^K]B^L^A^L^A^" -"L^A^L^:]6].]K]A^O_?^O_?^O_?^O_?^O_?^O_?^Oa?].].].].]/].].].]-]N]>]L]>]N]=]N]=]N]=]N]=]N]2^;_O]=]O_>]O_>]O_>]O_:a7_O]9a E^P_>^P_>^P_>^P_>^P_>\\,a H^.]" -" /[5]T[S\\8a1`<]L]=^R^<]O^8b7_ H^O\\ F] 6\\R\\=[R[U^5\\N]T]L^M` L]*] 0]:] 8]1^+]P]4]7_1[L_1]ZM];].] R` P`.]2]QfXaN]G]B]L^=^L]C]K_B].]+" -"_J]F]H]C].].]M^?].]C]H]La@^M^C]+^M^C]J]B]L^7]4^N^:a4aMaK^M^8]7^.]*^.] Q]P`>`Q^=^NZ;^Q`>_LZ>].^Q`?]L]?].].]Q^;].]M]M]N]L]>^P^>`Q^=^Q`?]/ZL];]-^Q`:a4`" -"P`D^Q^7a8^&]4](] S]Sb>_P^@]R^7^P^>^MZ<]L]9a9]M]=_P`XZB]Q_Q]G^V]V^>]P]=^U]U^?`P^>^P^6]4]Q^?]G]A^0]*^O^]P`>]P`>]P`>]P`>]P`>]P]X^LZN^NZ;_LZ>_LZ>_LZ>_LZ?].].].]-^P^>]L]>^P^=^P^=^P^=^P^=^P^2^:^P^=^Q`>^Q`>^Q`>^Q`:a7`Q^9a Dk],a " -"H]-] /[,[._0_;]L]=j<]N]7`5a J_S^ F] 6\\R\\=^U[W_5]N^V^K_Rd L],] /]:] 8]1])^T^3]8_0^Q`0]<]Q_8^S^8^3_R_=]R^:].] O] P]+]1\\PdW`N^G^C]N_;`R`C]NaA].]*`O" -"`F]H]C].].]L^@].]C]H]La?`S`B]*`S`B]J]B`Q_6]3_R_9a4aMaL^K^9]8^-])].] Q_Tb>aS^;_R\\:^Sa=`Q]>]-^Sa?]L]?].].]P^<].]M]M]N]L]=_T_=aS^;^Sa?]/^R_:]-^Sa:a3_P_" -"C^P^7_8^%]4](] S_V^X^?aS^>]T^5_T_=`R]<]L]8_8]M^>`SdA]SaS]E^W]W^=]P^=_W]W_>]X]T_<_T_5^4^T^?^G^C^/])^Q^8c=]H]7]6`S` ?] ;c >c E]._W[V\\9]4^J^9]4]%] ;]L]" -" IZQZ H]L] !u ,`Sd9\\U\\ ,ZQZ,]E\\E]L]?]E\\M_S^>^G^F^G^F^G^F^G^F^G^F^G^F^K]:`R`C].].].].].].].]-]ObB]La?`S`>`S`>`S`>`S`>`S`?]J]CcS`?_R_=_R_=_R_=_R_8]6" -"].]V[R^?_Tb>_Tb>_Tb>_Tb>_Tb>_Tb>_T^V_Q]M_R\\:`Q]=`Q]=`Q]=`Q]?].].].],_T_=]L]=_T_;_T_;_T_;_T_;_T_1^:`T_;^Sa=^Sa=^Sa=^Sa9_6aS^7_ Bi:i:i:i:i=]+` I],] /[" -",[-].]:]L]]C]H]K`>kA])kA]J^Cm5" -"]2j7_2`M`K^J]9]8tC])].] PgX]>]Xf9h9fX]],fX]?]L]?].].]O^=].]M]M]N]L]qA^U]W]U^D" -"i<]O`?k=]Xg:h3a7f>uCn?]/eSe;]:]H]7]5k >] :a n?\\H\\8]4]%] 9^R^ *^R^ Xu ,q9\\U\\ /]D\\F]LfH]D\\Li>]E]F]E]F]E]F]E]F]E]F]E]F]JnIkBn?n?n?n?].].]." -"]-n@]K`>ki-]]C]H]K`]Wd6f8dW]:i>]+dW]?]L]?].].]N^>].]M]M]N]L];f;]Wd7dW]?]/i7c3dV]9_2_P_E^M^8_8m4]4](] QdV`B]Xe;d1f8h<]L]8_9]K]>]XdW_@eWeBg;]O" -"`=g;]Vd8f1`6d=uCn?]/eSe;]:]H]7]3g <] 9_ :_ C]+f>n>ZFZ7]4]%] 7f &f Vu ,]XdW_9\\U\\ /\\C\\F\\KfH\\C\\Kg=]E]F]E]F]E]F]E]F]E]F]E]F]JnHh@n?n?n?n?].].].]-l>" -"]K`]C]H]J_9a<]$d?]I^?c0].b3_2" -"_K_M^G^;]8tC](]/] M`T]>]U`2b4`U]7c;])`U]?]L]?].].]M^?].]M]M]N]L]8`8]U`3`U]?],c2a0_T]9_2^N^F^K^8]7m4]4](] O`R^B]Va8b-`3d:]L]7]9^J]?]V`T]>cUc?c9]N_:" -"a8]T`3`-_4`X IX *W FW " -" " -" " -" HX W 4Z 3VCT X W 4Z " -" HX W 4Z 'VCT ;X W 3Y 2UCT KX W 3Y 0W " -" " -" " -" @W !W 4\\ 5YET ?XHX 8] >W !W 4\\ 7XGX KW !W 4\\ 7XHX +YET :W !W 3[ 5ZFT ?XGX EW !W 3[ 7XGX 5W " -" " -" " -" >W \"V 3\\ 7]HU ?XHX 9` ?W \"" -"V 3\\ 7XGX JW \"V 3\\ 7XHX -]HU 9W \"V 3] 7]HT ?XGX DW \"V 3] 8XGX 5V " -" " -" " -" W $V 3VNV 8XGX IW $V 3VNV 8XHX -_KV 8W $V 2] 7_KU ?XGX CW $V " -"2] 8XGX 6V " -" " -" :W &W " -"4VLV :j >XHX :VJV >W &W 4VLV 9XGX HW &W 4VLV 9XHX .j 6W &W 3VMV 9i >XGX BW &W 3VMV 9XGX 7W MW " -" " -" " -" CV 'W 4VJV ;j >XHX ;UGV >V 'W 4VJV :XGX GV 'W 4VJV :XHX .j" -" 5V 'W 3VKV :i >XGX AV 'W 3VKV :XGX 8W N[ " -" " -" " -" DV )W 4VHU TEY ;XHX V ,V 2UEU TCU :XGX =U -V 2UCU =XGX ;V NV" -"IV \"W " -" " -" JU /V 3VBV ETBT :U /" -"V 3VBV FU /V 3VBV (U /V 2UAU DU /V 2UAU @V NVGV " -" $X " -" *X " -" JX GTBT MX GX 7V :UEU DX GX 7V " -" JX GX 7W 4X GX 6V GX GX 5V (X &X " -" )X 8V " -" ;X FTBT " -" LX IX 7X W E\\ AW ,W ,W ,W ,W " -" HY GV +Y 4Z NX @X %W " -" DUDU =Y 7W KW 6Z 4XDT BTAT BW KW 6Z IW KW 6[ ,Y )XDT AW KW 5Z 4XDT " -" KW KW 4Z ,W BW 8V (S W H_ AW ,W ,W ,W ,W L] GV +] ;a " -" #[ F^ 8XGX +W BTEU " -" *R 9a :W MW 6\\ 6ZET ?XHX W Ja AW ,W ,W ,W ,W N_ GV +_ " -"?e 8] J] Jb 8[ <[ $Y FY 7XGX =Z Di 5W 8Z .Y !W FW *Y 4W)V*W)V-Y(V " -" W $a MY " -" EW 5W >W Kb AW ,W ,W ,W ,W !a GV +a Ch =f ^ Mf 2Z @x Mx a 5a &W 0g #\\ -_ <\\*V.\\*V0a-V\"X )Z /Z /Z /Z /Z 4WJV 1~U+d Kx Mx Mx Mx MX -X -X -X ,j" -" @[3X Dc 8c 8c 8c 8c W \"W 4VNV 8]HU ?XHX " -"BW \"W 3VNV 8XHX 2W ?W &XHX ^ K~\\ >S 3Q +[ @[;[ ;Q ;e HX 2VFV #VBV FS 6`1V#g GV !V 3V !T 7W 0d" -" :` ;j ?k -[ Dq :g Ky Df ;d $f 1Z @o 5j Np Ex Mt :m\"X/X'X -X -X3Z%X -]0]0\\4X Gi Lm 4i Ln ;m#~W$X/X-X(X-X4Y4XCY1Y-Y.Y&~S%a >W $a N[ EV " -"5W >W Lc AW ,W ,W ,W ,W \"b GV +a Dk Aj \"_ h 3Z @x Mx ?i 6X C~Q)X?X?X Ni 6V /V /" -"V DX &f #W0W e >XGX %c#e +b\"i 9_ Be 9d 'V 3k %^ /c @^*V0^*V2d.V\"X )Z /Z /Z /Z /Z 3b 1~U.j Nx Mx Mx Mx MX -X -X -X ,p F\\4X Gi >i " -">i >i >i BiEV.X/X'X/X'X/X'X/X.Y.Y#X 'j ;V \"V 5VLV :_IT >XHX V \"V 5VLV 9XGX IV \"V 4VMV 9XGX ,ZHY A_IT XHX AV \"V 3VLV 9" -"XHX 2V >W &XHX !_ K~[ >T 4R -_ D_?_ >S =t Fh IX 2VFV #VBV FS 7c4V#i HV \"W 3V !T 7V 0f @e >o Co 0" -"\\ Dq W M" -"d AW ,W ,W ,W ,W HW 1b GV +b Fm Dm #` \"j 4Z @x Mx Am 8X C~Q)X?X?X!m 9X 0V 0X EX 'h" -" $W0W \"h ?XGX 'g%g 0h%i :a Cf :f *V 4m %^ 0e A^+V/^+V1f1V!X )Z /Z /Z /Z /Z 2` 1~V0o\"x Mx Mx Mx MX -X -X -X ,t J\\4X Im Bm Bm Bm Bm F" -"mHV-X/X'X/X'X/X'X/X-X.X\"X (l ;V $V 4UJU :ULXLU >XHX XHX @V $V 2UJU 9XHX 3V" -" =W &XHX !` K~Z >T 4S /a FaAa @T @w Hl KX 2VFV $WCV ES 8e5V$j HV \"V 1V \"T 7V 2j Eh ?q Dp 1\\ Dq >" -"l Ly Hn Bj +l %e E\\ At >s$v Kx Mt >u&X/X'X -X -X5Z#X -^2^0]5X Jo q ;o r Br%~W$X/X-X(X,X6[6XAY3Y+Y0Y%~S%W 3V IW !_ FW 7W >W Md AW " -",W ,W ,W ,W HW 2[ ?V #[ Hn En #` #l 6\\ Ax Mx Cp 9X C~Q)X?X?X\"o ;Z 1V 1Z FX KS 0i #W2" -"W LV ,i ?XGX *l'h 3l'i ;c Dg ;g ,W 6o %^ 1g B^,V.^,V0g3V X *\\ 1\\ 1\\ 1\\ 1\\ 2^ 0~V2s$x Mx Mx Mx MX -X -X -X ,v L]5X Jo Do Do Do Do HpKW" -"-X/X'X/X'X/X'X/X-Y0Y\"X )n XHX ;UEU XHX @W &W 3VJV :XHX 4W =W &XHX " -" 1\\ 1\\ 1\\ 1\\ 1\\ =XMV K~Y =S 4U 1c IdCc AU Dz In LX 2VFV $VBV ES 9g7V$k HV #W 1W #T 8W 3l Fh ?r Eq 3] Dq ?m L" -"y Ip Em -n )k H\\ Au Av%x Mx Mt ?x(X/X'X -X -X6Z\"X -^2^0]5X Ls\"s ?s\"s Et%~W$X/X,X*X+X6[6X@Y5Y)Y2Y$~S%W 3W JW \"a FW 8W >W NZ 6W ,W " -",W ,W ,W HW 2X \\ 2V 2\\ GX KS 1j #" -"W2W LV -j ?XGX +ZEZ)VGY 5ZDZ)i T 5V 2e KfEe CW G| Jp MX 2VFV $VBV ES 9XIX8V$l HV #V /V #T " -" 8V 3n Gh ?s Fr 5^ Dq @n Lx Ir Go .o -q L^ Bv Cx&z x Mt A{)X/X'X -X -X7Z!X -^2^0^6X Mu#t Au#t Gu%~W$X/X,X*X+X6[6X?X5X'X2X#~S%W 2V JW #c FW" -" 9W >W NX 4W ,W ,W ,W ,W HW 2W ;V NW IZCY Hp JY &ZDZ 9^ Bx Mx Eu W *W 2UFU ;XHX 6W ;W &XHX 7h =h =h =h =h DWJV K~X >T 5W 4g MgFg EY J~ K]FZ MX 2VFV $VBV " -"ES :XGX9V%\\GX HV $W /W 3PATAP GV 3[H[ Gh ?]F] GZE^ 6^ Dq A]FX Lx I\\F\\ G\\G[ /[H] 0u N^ Bw E_D^&{!x Mt B`C_)X/X'X -X -X8Z X -_4_0_7X N^" -"E^$u C^E^$u H^E\\%~W$X/X,Y,Y*W7]8X>Y7Y'Y4Y#~S%W 2V JW $e FV 9W >W NW 3W ,W ,W ,W ,W HW 2W ;V NW IY@X >X " -"4[AV IX &X@X 9^ Bx Mx F^E^ =X C~Q)X?X?X&^E^ B` 4V 4` IX KS 3\\GW \"W4W KV .YBT ?XGX .V7V,P=W :W8W /VEV 3V +V /V " -" 7eGU KU 3WCW ;U-V$U-V LV5V NX +^ 3^ 3^ 3^ 3^ 3^ 1~W6_D^&x Mx Mx Mx MX -X -X -X ,{\"_7X N^E^ L^E^ L^E^ L^E^ L^E^ !^Ed*X/X'X/X'X/X'X/X+Y4Y X +Y?" -"X ;V *V 4UDU >TEZ TEZ T 5Y 5g MhHi G[ M~Q L\\AW MX 2VFV $VCV DS :WEW:V%ZAU HV $V -V 3RCTCR HW 4ZDZ H\\LX ?Y?[ HV>\\ 8_ DX )[?T -Y J[B" -"[ I[CZ 0WAZ 2x ^ BX>^ G]=Z&X=b#X -X '];[)X/X'X -X -X:[ NX -_4_0_7X \\?\\%X@^ E\\?\\%X?] J[=X =X W X 3W 4W ,W HW 3X ;V NX KY?X Ca 9Y:R HX (X>X :VNV BZ /X '\\?\\ A^ FX0X)X?X?X'\\?\\ " -" Db 5V 5b JX KS 3ZBT !W6W JV .X?R 4V4U HV ;V4V 1VCV 4V *U 0V 7fGU KU 4WAW TDX ;a 6V ,V 4UBU GV ,V 3UCU 0` 6TDX 4V ,V" -" 2UDU >TDX >V ,V 1UDU :V 9W (o Do Do Do Do GWIU J~V >T 6Z 6i jIj I\\ N~R M[=U MX 2VFV %VBV H] AWCW;V%Y=R" -" HV %W -V 4UETEU IV 4ZBZ IWGX ?V;[ IS9Z 9VNX DX *Z;R -X JZ>Y JZ?Y 1U>Z 5`C_#` CX;[ H[7W&X9_$X -X (\\6X)X/X'X -X -X;[ MX -_4_0`8X![;[&X" -"=[ F[;[&X<[ LZ8U =X W W 2W 4W ,W HW 3W :V MW KX=W Cc " -";X7P HX (WR !X8X JV /X

z,complex

&cj0,complex

&cj1, - complex

&cy0,complex

&cy1,complex

&cj0p, - complex

&cj1p,complex

&cy0p,complex

&cy1p) -{ - complex

z1,z2,cr,cp,cs,cp0,cq0,cp1,cq1,ct1,ct2,cu; - P a0,w0,w1; - int k,kz; - - static P a[] = { - -7.03125e-2, - 0.112152099609375, - -0.5725014209747314, - 6.074042001273483, - -1.100171402692467e2, - 3.038090510922384e3, - -1.188384262567832e5, - 6.252951493434797e6, - -4.259392165047669e8, - 3.646840080706556e10, - -3.833534661393944e12, - 4.854014686852901e14, - -7.286857349377656e16, - 1.279721941975975e19}; - static P b[] = { - 7.32421875e-2, - -0.2271080017089844, - 1.727727502584457, - -2.438052969955606e1, - 5.513358961220206e2, - -1.825775547429318e4, - 8.328593040162893e5, - -5.006958953198893e7, - 3.836255180230433e9, - -3.649010818849833e11, - 4.218971570284096e13, - -5.827244631566907e15, - 9.476288099260110e17, - -1.792162323051699e20}; - static P a1[] = { - 0.1171875, - -0.1441955566406250, - 0.6765925884246826, - -6.883914268109947, - 1.215978918765359e2, - -3.302272294480852e3, - 1.276412726461746e5, - -6.656367718817688e6, - 4.502786003050393e8, - -3.833857520742790e10, - 4.011838599133198e12, - -5.060568503314727e14, - 7.572616461117958e16, - -1.326257285320556e19}; - static P b1[] = { - -0.1025390625, - 0.2775764465332031, - -1.993531733751297, - 2.724882731126854e1, - -6.038440767050702e2, - 1.971837591223663e4, - -8.902978767070678e5, - 5.310411010968522e7, - -4.043620325107754e9, - 3.827011346598605e11, - -4.406481417852278e13, - 6.065091351222699e15, - -9.833883876590679e17, - 1.855045211579828e20}; - - a0 = abs(z); - z2 = z*z; - z1 = z; - if (a0 == 0.0) { - cj0 = cone; - cj1 = czero; - cy0 = complex

(-1e308,0); - cy1 = complex

(-1e308,0); - cj0p = czero; - cj1p = complex

(0.5,0.0); - cy0p = complex

(1e308,0); - cy1p = complex

(1e308,0); - return 0; - } - if (real(z) < 0.0) z1 = -z; - if (a0 <= 12.0) { - cj0 = cone; - cr = cone; - for (k=1;k<=40;k++) { - cr *= -0.25*z2/(P)(k*k); - cj0 += cr; - if (abs(cr) < abs(cj0)*eps) break; - } - cj1 = cone; - cr = cone; - for (k=1;k<=40;k++) { - cr *= -0.25*z2/(k*(k+1.0)); - cj1 += cr; - if (abs(cr) < abs(cj1)*eps) break; - } - cj1 *= 0.5*z1; - w0 = 0.0; - cr = cone; - cs = czero; - for (k=1;k<=40;k++) { - w0 += 1.0/k; - cr *= -0.25*z2/(P)(k*k); - cp = cr*w0; - cs += cp; - if (abs(cp) < abs(cs)*eps) break; - } - cy0 = M_2_PI*((log(0.5*z1)+el)*cj0-cs); - w1 = 0.0; - cr = cone; - cs = cone; - for (k=1;k<=40;k++) { - w1 += 1.0/k; - cr *= -0.25*z2/(k*(k+1.0)); - cp = cr*(2.0*w1+1.0/(k+1.0)); - cs += cp; - if (abs(cp) < abs(cs)*eps) break; - } - cy1 = M_2_PI*((log(0.5*z1)+el)*cj1-1.0/z1-0.25*z1*cs); - } - else { - if (a0 >= 50.0) kz = 8; // can be changed to 10 - else if (a0 >= 35.0) kz = 10; // " " " 12 - else kz = 12; // " " " 14 - ct1 = z1 - M_PI_4; - cp0 = cone; - for (k=0;k 0.0) { - cy0 += 2.0*cii*cj0; - cy1 = -(cy1+2.0*cii*cj1); - } - cj1 = -cj1; - } - cj0p = -cj1; - cj1p = cj0-cj1/z; - cy0p = -cy1; - cy1p = cy0-cy1/z; - return 0; -} - -template -int cbessjyna(int n,complex

z,int &nm,complex

*cj, - complex

*cy,complex

*cjp,complex

*cyp) -{ - complex

cbj0,cbj1,cby0,cby1,cj0,cjk,cj1,cf,cf1,cf2; - complex

cs,cg0,cg1,cyk,cyl1,cyl2,cylk,cp11,cp12,cp21,cp22; - complex

ch0,ch1,ch2; - P a0,yak,ya1,ya0,wa; - int m,k,lb,lb0; - - if (n < 0) return 1; - a0 = abs(z); - nm = n; - if (a0 < 1.0e-100) { - for (k=0;k<=n;k++) { - cj[k] = czero; - cy[k] = complex

(-1e308,0); - cjp[k] = czero; - cyp[k] = complex

(1e308,0); - } - cj[0] = cone; - cjp[1] = complex

(0.5,0.0); - return 0; - } - cbessjy01(z,cj[0],cj[1],cy[0],cy[1],cjp[0],cjp[1],cyp[0],cyp[1]); - cbj0 = cj[0]; - cbj1 = cj[1]; - cby0 = cy[0]; - cby1 = cy[1]; - if (n <= 1) return 0; - if (n < (int)0.25*a0) { - cj0 = cbj0; - cj1 = cbj1; - for (k=2;k<=n;k++) { - cjk = 2.0*(k-1.0)*cj1/z-cj0; - cj[k] = cjk; - cj0 = cj1; - cj1 = cjk; - } - } - else { - m = msta1(a0,200); - if (m < n) nm = m; - else m = msta2(a0,n,15); - cf2 = czero; - cf1 = complex

(1.0e-100,0.0); - for (k=m;k>=0;k--) { - cf = 2.0*(k+1.0)*cf1/z-cf2; - if (k <=nm) cj[k] = cf; - cf2 = cf1; - cf1 = cf; - } - if (abs(cbj0) > abs(cbj1)) cs = cbj0/cf; - else cs = cbj1/cf2; - for (k=0;k<=nm;k++) { - cj[k] *= cs; - } - } - for (k=2;k<=nm;k++) { - cjp[k] = cj[k-1]-(P)k*cj[k]/z; - } - ya0 = abs(cby0); - lb = 0; - cg0 = cby0; - cg1 = cby1; - for (k=2;k<=nm;k++) { - cyk = 2.0*(k-1.0)*cg1/z-cg0; - yak = abs(cyk); - ya1 = abs(cg0); - if ((yak < ya0) && (yak < ya1)) lb = k; - cy[k] = cyk; - cg0 = cg1; - cg1 = cyk; - } - lb0 = 0; - if ((lb > 4) && (imag(z) != 0.0)) { - while (lb != lb0) { - ch2 = cone; - ch1 = czero; - lb0 = lb; - for (k=lb;k>=1;k--) { - ch0 = 2.0*k*ch1/z-ch2; - ch2 = ch1; - ch1 = ch0; - } - cp12 = ch0; - cp22 = ch2; - ch2 = czero; - ch1 = cone; - for (k=lb;k>=1;k--) { - ch0 = 2.0*k*ch1/z-ch2; - ch2 = ch1; - ch1 = ch0; - } - cp11 = ch0; - cp21 = ch2; - if (lb == nm) - cj[lb+1] = 2.0*lb*cj[lb]/z-cj[lb-1]; - if (abs(cj[0]) > abs(cj[1])) { - cy[lb+1] = (cj[lb+1]*cby0-2.0*cp11/(M_PI*z))/cj[0]; - cy[lb] = (cj[lb]*cby0+2.0*cp12/(M_PI*z))/cj[0]; - } - else { - cy[lb+1] = (cj[lb+1]*cby1-2.0*cp21/(M_PI*z))/cj[1]; - cy[lb] = (cj[lb]*cby1+2.0*cp22/(M_PI*z))/cj[1]; - } - cyl2 = cy[lb+1]; - cyl1 = cy[lb]; - for (k=lb-1;k>=0;k--) { - cylk = 2.0*(k+1.0)*cyl1/z-cyl2; - cy[k] = cylk; - cyl2 = cyl1; - cyl1 = cylk; - } - cyl1 = cy[lb]; - cyl2 = cy[lb+1]; - for (k=lb+1;k -int cbessjynb(int n,complex

z,int &nm,complex

*cj, - complex

*cy,complex

*cjp,complex

*cyp) -{ - complex

cf,cf0,cf1,cf2,cbs,csu,csv,cs0,ce; - complex

ct1,cp0,cq0,cp1,cq1,cu,cbj0,cby0,cbj1,cby1; - complex

cyy,cbjk,ct2; - P a0,y0; - int k,m; - static P a[] = { - -0.7031250000000000e-1, - 0.1121520996093750, - -0.5725014209747314, - 6.074042001273483}; - static P b[] = { - 0.7324218750000000e-1, - -0.2271080017089844, - 1.727727502584457, - -2.438052969955606e1}; - static P a1[] = { - 0.1171875, - -0.1441955566406250, - 0.6765925884246826, - -6.883914268109947}; - static P b1[] = { - -0.1025390625, - 0.2775764465332031, - -1.993531733751297, - 2.724882731126854e1}; - - y0 = abs(imag(z)); - a0 = abs(z); - nm = n; - if (a0 < 1.0e-100) { - for (k=0;k<=n;k++) { - cj[k] = czero; - cy[k] = complex

(-1e308,0); - cjp[k] = czero; - cyp[k] = complex

(1e308,0); - } - cj[0] = cone; - cjp[1] = complex

(0.5,0.0); - return 0; - } - if ((a0 <= 300.0) || (n > (int)(0.25*a0))) { - if (n == 0) nm = 1; - m = msta1(a0,200); - if (m < nm) nm = m; - else m = msta2(a0,nm,15); - cbs = czero; - csu = czero; - csv = czero; - cf2 = czero; - cf1 = complex

(1.0e-100,0.0); - for (k=m;k>=0;k--) { - cf = 2.0*(k+1.0)*cf1/z-cf2; - if (k <= nm) cj[k] = cf; - if (((k & 1) == 0) && (k != 0)) { - if (y0 <= 1.0) { - cbs += 2.0*cf; - } - else { - cbs += (-1)*((k & 2)-1)*2.0*cf; - } - csu += (P)((-1)*((k & 2)-1))*cf/(P)k; - } - else if (k > 1) { - csv += (P)((-1)*((k & 2)-1)*k)*cf/(P)(k*k-1.0); - } - cf2 = cf1; - cf1 = cf; - } - if (y0 <= 1.0) cs0 = cbs+cf; - else cs0 = (cbs+cf)/cos(z); - for (k=0;k<=nm;k++) { - cj[k] /= cs0; - } - ce = log(0.5*z)+el; - cy[0] = M_2_PI*(ce*cj[0]-4.0*csu/cs0); - cy[1] = M_2_PI*(-cj[0]/z+(ce-1.0)*cj[1]-4.0*csv/cs0); - } - else { - ct1 = z-M_PI_4; - cp0 = cone; - for (k=0;k<4;k++) { - cp0 += a[k]*pow(z,-2.0*k-2.0); - } - cq0 = -0.125/z; - for (k=0;k<4;k++) { - cq0 += b[k] *pow(z,-2.0*k-3.0); - } - cu = sqrt(M_2_PI/z); - cbj0 = cu*(cp0*cos(ct1)-cq0*sin(ct1)); - cby0 = cu*(cp0*sin(ct1)+cq0*cos(ct1)); - cj[0] = cbj0; - cy[0] = cby0; - ct2 = z-0.75*M_PI; - cp1 = cone; - for (k=0;k<4;k++) { - cp1 += a1[k]*pow(z,-2.0*k-2.0); - } - cq1 = 0.375/z; - for (k=0;k<4;k++) { - cq1 += b1[k]*pow(z,-2.0*k-3.0); - } - cbj1 = cu*(cp1*cos(ct2)-cq1*sin(ct2)); - cby1 = cu*(cp1*sin(ct2)+cq1*cos(ct2)); - cj[1] = cbj1; - cy[1] = cby1; - for (k=2;k<=n;k++) { - cbjk = 2.0*(k-1.0)*cbj1/z-cbj0; - cj[k] = cbjk; - cbj0 = cbj1; - cbj1 = cbjk; - } - } - cjp[0] = -cj[1]; - for (k=1;k<=nm;k++) { - cjp[k] = cj[k-1]-(P)k*cj[k]/z; - } - if (abs(cj[0]) > 1.0) - cy[1] = (cj[1]*cy[0]-2.0/(M_PI*z))/cj[0]; - for (k=2;k<=nm;k++) { - if (abs(cj[k-1]) >= abs(cj[k-2])) - cyy = (cj[k]*cy[k-1]-2.0/(M_PI*z))/cj[k-1]; - else - cyy = (cj[k]*cy[k-2]-4.0*(k-1.0)/(M_PI*z*z))/cj[k-2]; - cy[k] = cyy; - } - cyp[0] = -cy[1]; - for (k=1;k<=nm;k++) { - cyp[k] = cy[k-1]-(P)k*cy[k]/z; - } - - return 0; -} - -template -int cbessjyva(P v,complex

z,P &vm,complex

*cjv, - complex

*cyv,complex

*cjvp,complex

*cyvp) -{ - complex

z1,z2,zk,cjvl,cr,ca,cjv0,cjv1,cpz,crp; - complex

cqz,crq,ca0,cck,csk,cyv0,cyv1,cju0,cju1,cb; - complex

cs,cs0,cr0,cs1,cr1,cec,cf,cf0,cf1,cf2; - complex

cfac0,cfac1,cg0,cg1,cyk,cp11,cp12,cp21,cp22; - complex

ch0,ch1,ch2,cyl1,cyl2,cylk; - - P a0,v0,pv0,pv1,vl,ga,gb,vg,vv,w0,w1,ya0,yak,ya1,wa; - int j,n,k,kz,l,lb,lb0,m; - - a0 = abs(z); - z1 = z; - z2 = z*z; - n = (int)v; - - - v0 = v-n; - - pv0 = M_PI*v0; - pv1 = M_PI*(1.0+v0); - if (a0 < 1.0e-100) { - for (k=0;k<=n;k++) { - cjv[k] = czero; - cyv[k] = complex

(-1e308,0); - cjvp[k] = czero; - cyvp[k] = complex

(1e308,0); - - } - if (v0 == 0.0) { - cjv[0] = cone; - cjvp[1] = complex

(0.5,0.0); - } - else { - cjvp[0] = complex

(1e308,0); - } - vm = v; - return 0; - } - if (real(z1) < 0.0) z1 = -z; - if (a0 <= 12.0) { - for (l=0;l<2;l++) { - vl = v0+l; - cjvl = cone; - cr = cone; - for (k=1;k<=40;k++) { - cr *= -0.25*z2/(k*(k+vl)); - cjvl += cr; - if (abs(cr) < abs(cjvl)*eps) break; - } - vg = 1.0 + vl; - ga = gamma(vg); - ca = pow(0.5*z1,vl)/ga; - if (l == 0) cjv0 = cjvl*ca; - else cjv1 = cjvl*ca; - } - } - else { - if (a0 >= 50.0) kz = 8; - else if (a0 >= 35.0) kz = 10; - else kz = 11; - for (j=0;j<2;j++) { - vv = 4.0*(j+v0)*(j+v0); - cpz = cone; - crp = cone; - for (k=1;k<=kz;k++) { - crp = -0.78125e-2*crp*(vv-pow(4.0*k-3.0,2.0))* - (vv-pow(4.0*k-1.0,2.0))/(k*(2.0*k-1.0)*z2); - cpz += crp; - } - cqz = cone; - crq = cone; - for (k=1;k<=kz;k++) { - crq = -0.78125e-2*crq*(vv-pow(4.0*k-1.0,2.0))* - (vv-pow(4.0*k+1.0,2.0))/(k*(2.0*k+1.0)*z2); - cqz += crq; - } - cqz *= 0.125*(vv-1.0)/z1; - zk = z1-(0.5*(j+v0)+0.25)*M_PI; - ca0 = sqrt(M_2_PI/z1); - cck = cos(zk); - csk = sin(zk); - if (j == 0) { - cjv0 = ca0*(cpz*cck-cqz*csk); - cyv0 = ca0*(cpz*csk+cqz+cck); - } - else { - cjv1 = ca0*(cpz*cck-cqz*csk); - cyv1 = ca0*(cpz*csk+cqz*cck); - } - } - } - if (a0 <= 12.0) { - if (v0 != 0.0) { - for (l=0;l<2;l++) { - vl = v0+l; - cjvl = cone; - cr = cone; - for (k=1;k<=40;k++) { - cr *= -0.25*z2/(k*(k-vl)); - cjvl += cr; - if (abs(cr) < abs(cjvl)*eps) break; - } - vg = 1.0-vl; - gb = gamma(vg); - cb = pow(2.0/z1,vl)/gb; - if (l == 0) cju0 = cjvl*cb; - else cju1 = cjvl*cb; - } - cyv0 = (cjv0*cos(pv0)-cju0)/sin(pv0); - cyv1 = (cjv1*cos(pv1)-cju1)/sin(pv1); - } - else { - cec = log(0.5*z1)+el; - cs0 = czero; - w0 = 0.0; - cr0 = cone; - for (k=1;k<=30;k++) { - w0 += 1.0/k; - cr0 *= -0.25*z2/(P)(k*k); - cs0 += cr0*w0; - } - cyv0 = M_2_PI*(cec*cjv0-cs0); - cs1 = cone; - w1 = 0.0; - cr1 = cone; - for (k=1;k<=30;k++) { - w1 += 1.0/k; - cr1 *= -0.25*z2/(k*(k+1.0)); - cs1 += cr1*(2.0*w1+1.0/(k+1.0)); - } - cyv1 = M_2_PI*(cec*cjv1-1.0/z1-0.25*z1*cs1); - } - } - if (real(z) < 0.0) { - cfac0 = exp(pv0*cii); - cfac1 = exp(pv1*cii); - if (imag(z) < 0.0) { - cyv0 = cfac0*cyv0-(P)2.0*(complex

)cii*cos(pv0)*cjv0; - cyv1 = cfac1*cyv1-(P)2.0*(complex

)cii*cos(pv1)*cjv1; - cjv0 /= cfac0; - cjv1 /= cfac1; - } - else if (imag(z) > 0.0) { - cyv0 = cyv0/cfac0+(P)2.0*(complex

)cii*cos(pv0)*cjv0; - cyv1 = cyv1/cfac1+(P)2.0*(complex

)cii*cos(pv1)*cjv1; - cjv0 *= cfac0; - cjv1 *= cfac1; - } - } - cjv[0] = cjv0; - cjv[1] = cjv1; - if ((n >= 2) && (n <= (int)(0.25*a0))) { - cf0 = cjv0; - cf1 = cjv1; - for (k=2;k<= n;k++) { - cf = 2.0*(k+v0-1.0)*cf1/z-cf0; - cjv[k] = cf; - cf0 = cf1; - cf1 = cf; - } - } - else if (n >= 2) { - m = msta1(a0,200); - if (m < n) n = m; - else m = msta2(a0,n,15); - cf2 = czero; - cf1 = complex

(1.0e-100,0.0); - for (k=m;k>=0;k--) { - cf = 2.0*(v0+k+1.0)*cf1/z-cf2; - if (k <= n) cjv[k] = cf; - cf2 = cf1; - cf1 = cf; - } - if (abs(cjv0) > abs(cjv1)) cs = cjv0/cf; - else cs = cjv1/cf2; - for (k=0;k<=n;k++) { - cjv[k] *= cs; - } - } - cjvp[0] = v0*cjv[0]/z-cjv[1]; - for (k=1;k<=n;k++) { - cjvp[k] = -(k+v0)*cjv[k]/z+cjv[k-1]; - } - cyv[0] = cyv0; - cyv[1] = cyv1; - ya0 = abs(cyv0); - lb = 0; - cg0 = cyv0; - cg1 = cyv1; - for (k=2;k<=n;k++) { - cyk = 2.0*(v0+k-1.0)*cg1/z-cg0; - yak = abs(cyk); - ya1 = abs(cg0); - if ((yak < ya0) && (yak< ya1)) lb = k; - cyv[k] = cyk; - cg0 = cg1; - cg1 = cyk; - } - lb0 = 0; - if ((lb > 4) && (imag(z) != 0.0)) { - while(lb != lb0) { - ch2 = cone; - ch1 = czero; - lb0 = lb; - for (k=lb;k>=1;k--) { - ch0 = 2.0*(k+v0)*ch1/z-ch2; - ch2 = ch1; - ch1 = ch0; - } - cp12 = ch0; - cp22 = ch2; - ch2 = czero; - ch1 = cone; - for (k=lb;k>=1;k--) { - ch0 = 2.0*(k+v0)*ch1/z-ch2; - ch2 = ch1; - ch1 = ch0; - } - cp11 = ch0; - cp21 = ch2; - if (lb == n) - cjv[lb+1] = 2.0*(lb+v0)*cjv[lb]/z-cjv[lb-1]; - if (abs(cjv[0]) > abs(cjv[1])) { - cyv[lb+1] = (cjv[lb+1]*cyv0-2.0*cp11/(M_PI*z))/cjv[0]; - cyv[lb] = (cjv[lb]*cyv0+2.0*cp12/(M_PI*z))/cjv[0]; - } - else { - cyv[lb+1] = (cjv[lb+1]*cyv1-2.0*cp21/(M_PI*z))/cjv[1]; - cyv[lb] = (cjv[lb]*cyv1+2.0*cp22/(M_PI*z))/cjv[1]; - } - cyl2 = cyv[lb+1]; - cyl1 = cyv[lb]; - for (k=lb-1;k>=0;k--) { - cylk = 2.0*(k+v0+1.0)*cyl1/z-cyl2; - cyv[k] = cylk; - cyl2 = cyl1; - cyl1 = cylk; - } - cyl1 = cyv[lb]; - cyl2 = cyv[lb+1]; - for (k=lb+1;k -int cbessjyva_sph(int v,complex

z,P &vm,complex

*cjv, - complex

*cyv,complex

*cjvp,complex

*cyvp) -{ - //first, compute the bessel functions of fractional order - cbessjyva

(v + 0.5, z, vm, cjv, cyv, cjvp, cyvp); - - //iterate through each and scale - for(int n = 0; n<=v; n++) - { - - cjv[n] = cjv[n] * sqrt(rtsPI/(z * 2.0)); - cyv[n] = cyv[n] * sqrt(rtsPI/(z * 2.0)); - - cjvp[n] = -1.0 / (z * 2.0) * cjv[n] + cjvp[n] * sqrt(rtsPI / (z * 2.0)); - cyvp[n] = -1.0 / (z * 2.0) * cyv[n] + cyvp[n] * sqrt(rtsPI / (z * 2.0)); - } - - return 0; - -} - -} //end namespace rts - - -#endif diff --git a/math/complex.h b/math/complex.h deleted file mode 100644 index 84ba67e..0000000 --- a/math/complex.h +++ /dev/null @@ -1,505 +0,0 @@ -/*RTS Complex number class. This class is CUDA compatible, -and can therefore be used in CUDA code and on CUDA devices. -*/ - -#ifndef RTS_COMPLEX -#define RTS_COMPLEX - -#include "../cuda/callable.h" -#include -#include -#include -#include - -namespace stim -{ - -template -struct complex -{ - T r, i; - - //default constructor - CUDA_CALLABLE complex() - { - r = 0; - i = 0; - } - - //constructor when given real and imaginary values - CUDA_CALLABLE complex(T r, T i = 0) - { - this->r = r; - this->i = i; - } - - //access methods - CUDA_CALLABLE T real() - { - return r; - } - - CUDA_CALLABLE T real(T r_val) - { - r = r_val; - return r_val; - } - - CUDA_CALLABLE T imag() - { - return i; - } - CUDA_CALLABLE T imag(T i_val) - { - i = i_val; - return i_val; - } - - - - //return the current value multiplied by i - CUDA_CALLABLE complex imul() - { - complex result; - result.r = -i; - result.i = r; - - return result; - } - - //returns the complex signum (-1, 0, 1) - CUDA_CALLABLE int sgn(){ - if(r > 0) return 1; - else if(r < 0) return -1; - else return (0 < i - i < 0); - } - - //ARITHMETIC OPERATORS-------------------- - - //binary + operator (returns the result of adding two complex values) - CUDA_CALLABLE complex operator+ (const complex rhs) const - { - complex result; - result.r = r + rhs.r; - result.i = i + rhs.i; - return result; - } - - CUDA_CALLABLE complex operator+ (const T rhs) const - { - complex result; - result.r = r + rhs; - result.i = i; - return result; - } - - //binary - operator (returns the result of adding two complex values) - CUDA_CALLABLE complex operator- (const complex rhs) const - { - complex result; - result.r = r - rhs.r; - result.i = i - rhs.i; - return result; - } - - //binary - operator (returns the result of adding two complex values) - CUDA_CALLABLE complex operator- (const T rhs) - { - complex result; - result.r = r - rhs; - result.i = i; - return result; - } - - //binary MULTIPLICATION operators (returns the result of multiplying complex values) - CUDA_CALLABLE complex operator* (const complex rhs) const - { - complex result; - result.r = r * rhs.r - i * rhs.i; - result.i = r * rhs.i + i * rhs.r; - return result; - } - CUDA_CALLABLE complex operator* (const T rhs) - { - return complex(r * rhs, i * rhs); - } - - //binary DIVISION operators (returns the result of dividing complex values) - CUDA_CALLABLE complex operator/ (const complex rhs) const - { - complex result; - T denom = rhs.r * rhs.r + rhs.i * rhs.i; - result.r = (r * rhs.r + i * rhs.i) / denom; - result.i = (- r * rhs.i + i * rhs.r) / denom; - - return result; - } - CUDA_CALLABLE complex operator/ (const T rhs) - { - return complex(r / rhs, i / rhs); - } - - //ASSIGNMENT operators----------------------------------- - CUDA_CALLABLE complex & operator=(const complex &rhs) - { - //check for self-assignment - if(this != &rhs) - { - this->r = rhs.r; - this->i = rhs.i; - } - return *this; - } - CUDA_CALLABLE complex & operator=(const T &rhs) - { - this->r = rhs; - this->i = 0; - - return *this; - } - - //arithmetic assignment operators - CUDA_CALLABLE complex operator+=(const complex &rhs) - { - *this = *this + rhs; - return *this; - } - CUDA_CALLABLE complex operator+=(const T &rhs) - { - *this = *this + rhs; - return *this; - } - - CUDA_CALLABLE complex operator-=(const complex &rhs) - { - *this = *this - rhs; - return *this; - } - CUDA_CALLABLE complex operator-=(const T &rhs) - { - *this = *this - rhs; - return *this; - } - - CUDA_CALLABLE complex operator*=(const complex &rhs) - { - *this = *this * rhs; - return *this; - } - CUDA_CALLABLE complex operator*=(const T &rhs) - { - *this = *this * rhs; - return *this; - } - //divide and assign - CUDA_CALLABLE complex operator/=(const complex &rhs) - { - *this = *this / rhs; - return *this; - } - CUDA_CALLABLE complex operator/=(const T &rhs) - { - *this = *this / rhs; - return *this; - } - - //absolute value operator (returns the absolute value of the complex number) - CUDA_CALLABLE T abs() - { - return std::sqrt(r * r + i * i); - } - - CUDA_CALLABLE complex log() - { - complex result; - result.r = (T)std::log(std::sqrt(r * r + i * i)); - result.i = (T)std::atan2(i, r); - - - return result; - } - - CUDA_CALLABLE complex exp() - { - complex result; - - T e_r = std::exp(r); - result.r = e_r * (T)std::cos(i); - result.i = e_r * (T)std::sin(i); - - return result; - } - - /*CUDA_CALLABLE complex pow(int y) - { - - return pow((double)y); - }*/ - - CUDA_CALLABLE complex pow(T y) - { - complex result; - - result = log() * y; - - return result.exp(); - } - - CUDA_CALLABLE complex sqrt() - { - complex result; - - //convert to polar coordinates - T a = std::sqrt(r*r + i*i); - T theta = std::atan2(i, r); - - //find the square root - T a_p = std::sqrt(a); - T theta_p = theta/2.0f; - - //convert back to cartesian coordinates - result.r = a_p * std::cos(theta_p); - result.i = a_p * std::sin(theta_p); - - return result; - } - - std::string str() - { - std::stringstream ss; - ss<<"("< rhs) - { - if(r == rhs.r && i == rhs.i) - return true; - return false; - } - - CUDA_CALLABLE bool operator==(T rhs) - { - if(r == rhs && i == 0) - return true; - return false; - } - - CUDA_CALLABLE bool operator!=(T rhs) - { - if(r != rhs || i != 0) - return true; - return false; - } - - CUDA_CALLABLE bool operator<(complex rhs){ - return abs() < rhs.abs(); - } - CUDA_CALLABLE bool operator<=(complex rhs){ - return abs() <= rhs.abs(); - } - CUDA_CALLABLE bool operator>(complex rhs){ - return abs() > rhs.abs(); - } - CUDA_CALLABLE bool operator >=(complex rhs){ - return abs() >= rhs.abs(); - } - - //CASTING operators - template < typename otherT > - operator complex() - { - complex result((otherT)r, (otherT)i); - return result; - } - template< typename otherT > - complex( const complex &rhs) - { - r = (T)rhs.r; - i = (T)rhs.i; - } - template< typename otherT > - complex& operator=(const complex &rhs) - { - r = (T)rhs.r; - i = (T)rhs.i; - return *this; - } - -}; - -} //end RTS namespace - -//addition -template -CUDA_CALLABLE static stim::complex operator+(const double a, const stim::complex b) -{ - return stim::complex((T)a + b.r, b.i); -} - -//subtraction with a real value -template -CUDA_CALLABLE static stim::complex operator-(const double a, const stim::complex b) -{ - return stim::complex((T)a - b.r, -b.i); -} - -//minus sign -template -CUDA_CALLABLE static stim::complex operator-(const stim::complex &rhs) -{ - return stim::complex(-rhs.r, -rhs.i); -} - -//multiply a T value by a complex value -template -CUDA_CALLABLE static stim::complex operator*(const double a, const stim::complex b) -{ - return stim::complex((T)a * b.r, (T)a * b.i); -} - -//divide a T value by a complex value -template -CUDA_CALLABLE static stim::complex operator/(const double a, const stim::complex b) -{ - stim::complex result; - - T denom = b.r * b.r + b.i * b.i; - - result.r = ((T)a * b.r) / denom; - result.i = -((T)a * b.i) / denom; - - return result; -} - - -template -CUDA_CALLABLE static stim::complex pow(stim::complex x, T y) -{ - return x.pow(y); -} -template -CUDA_CALLABLE static stim::complex pow(stim::complex x, int y) -{ - return x.pow(y); -} - -//log function -template -CUDA_CALLABLE static stim::complex log(stim::complex x) -{ - return x.log(); -} - -//exp function -template -CUDA_CALLABLE static stim::complex exp(stim::complex x) -{ - return x.exp(); -} - -//sqrt function -template -CUDA_CALLABLE static stim::complex sqrt(stim::complex x) -{ - return x.sqrt(); -} - - -template -CUDA_CALLABLE static T abs(stim::complex a) -{ - return a.abs(); -} - -template -CUDA_CALLABLE static T real(stim::complex a) -{ - return a.r; -} - -//template -CUDA_CALLABLE static float real(float a) -{ - return a; -} - -template -CUDA_CALLABLE static T imag(stim::complex a) -{ - return a.i; -} - -//trigonometric functions -//template -/*CUDA_CALLABLE static stim::complex sinf(const stim::complex x) -{ - stim::complex result; - result.r = sinf(x.r) * coshf(x.i); - result.i = cosf(x.r) * sinhf(x.i); - - return result; -}*/ - -template -CUDA_CALLABLE stim::complex sin(const stim::complex x) -{ - stim::complex result; - result.r = (A)std::sin(x.r) * (A)std::cosh(x.i); - result.i = (A)std::cos(x.r) * (A)std::sinh(x.i); - - return result; -} - -//floating point template -//template -/*CUDA_CALLABLE static stim::complex cosf(const stim::complex x) -{ - stim::complex result; - result.r = cosf(x.r) * coshf(x.i); - result.i = -(sinf(x.r) * sinhf(x.i)); - - return result; -}*/ - -template -CUDA_CALLABLE stim::complex cos(const stim::complex x) -{ - stim::complex result; - result.r = (A)std::cos(x.r) * (A)std::cosh(x.i); - result.i = -((A)std::sin(x.r) * (A)std::sinh(x.i)); - - return result; -} - - -template -std::ostream& operator<<(std::ostream& os, stim::complex x) -{ - os< -std::istream& operator>>(std::istream& is, stim::complex& x) -{ - A r, i; - r = i = 0; //initialize the real and imaginary parts to zero - is>>r; //parse - is>>i; - - x.real(r); //assign the parsed values to x - x.imag(i); - - return is; //return the stream -} - -//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7 -//template using rtsComplex = stim::complex; -//#endif - - - -#endif diff --git a/math/complexfield.cuh b/math/complexfield.cuh deleted file mode 100644 index 81488cf..0000000 --- a/math/complexfield.cuh +++ /dev/null @@ -1,137 +0,0 @@ -#ifndef RTS_COMPLEXFIELD_H -#define RTS_COMPLEXFIELD_H - -#include "cublas_v2.h" -#include - -#include "../math/field.cuh" -#include "../math/complex.h" -#include "../math/realfield.cuh" - -namespace stim{ - -template -__global__ void gpu_complexfield_mag(T* dest, complex* source, unsigned int r0, unsigned int r1){ - - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //calculate and store the result - dest[i] = source[i].abs(); -} - -/*This class stores functions for saving images of complex fields -*/ -template -class complexfield : public field< stim::complex, D >{ - using field< stim::complex, D >::R; - using field< stim::complex, D >::X; - using field< stim::complex, D >::shape; - using field< stim::complex, D >::cuda_params; - - - -public: - - //find the maximum value of component n - stim::complex find_max(unsigned int n){ - cublasStatus_t stat; - cublasHandle_t handle; - - //create a CUBLAS handle - stat = cublasCreate(&handle); - if(stat != CUBLAS_STATUS_SUCCESS){ - std::cout<<"CUBLAS Error: initialization failed"< result; - - if(sizeof(T) == 8) - stat = cublasIcamax(handle, L, (const cuComplex*)X[n], 1, &index); - else - stat = cublasIzamax(handle, L, (const cuDoubleComplex*)X[n], 1, &index); - - index -= 1; //adjust for 1-based indexing - - //if there was a GPU error, terminate - if(stat != CUBLAS_STATUS_SUCCESS){ - std::cout<<"CUBLAS Error: failure finding maximum value."<), cudaMemcpyDeviceToHost)); - return result; - } - -public: - - enum attribute {magnitude, real, imaginary}; - - //constructor (no parameters) - complexfield() : field, D>(){}; - - //constructor (resolution specified) - complexfield(unsigned int r0, unsigned int r1) : field, D>(r0, r1){}; - - //assignment from a field of complex values - complexfield & operator=(const field< stim::complex, D > rhs){ - field< complex, D >::operator=(rhs); - return *this; - } - - //assignment operator (scalar value) - complexfield & operator= (const complex rhs){ - - field< complex, D >::operator=(rhs); - return *this; - } - - //assignment operator (vector value) - complexfield & operator= (const vec< complex, D > rhs){ - - field< complex, D >::operator=(rhs); - return *this; - } - - //cropping - complexfield crop(unsigned int width, unsigned int height){ - - complexfield result; - result = field< complex, D>::crop(width, height); - return result; - } - - void toImage(std::string filename, attribute type = magnitude, unsigned int n=0){ - - field rf(R[0], R[1]); - - //get cuda parameters - dim3 blocks, grids; - cuda_params(grids, blocks); - - if(type == magnitude){ - gpu_complexfield_mag <<>> (rf.ptr(), X[n], R[0], R[1]); - rf.toImage(filename, n, true); - } - - } - - -}; - - -} //end namespace rts - - -#endif diff --git a/math/constants.h b/math/constants.h deleted file mode 100644 index d333887..0000000 --- a/math/constants.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef RTS_CONSTANTS_H -#define RTS_CONSTANTS_H - -#define stimPI 3.14159 -#define stimTAU 2 * rtsPI - -#endif diff --git a/math/field.cuh b/math/field.cuh deleted file mode 100644 index 1a3a2ab..0000000 --- a/math/field.cuh +++ /dev/null @@ -1,343 +0,0 @@ -#ifndef RTS_FIELD_CUH -#define RTS_FIELD_CUH - -#include -#include -#include - -#include "cublas_v2.h" -#include - -#include "../math/rect.h" -#include "../cuda/threads.h" -#include "../cuda/error.h" -#include "../cuda/devices.h" -#include "../visualization/colormap.h" - - -namespace stim{ - -//multiply R = X * Y -template -__global__ void gpu_field_multiply(T* R, T* X, T* Y, unsigned int r0, unsigned int r1){ - - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //calculate and store the result - R[i] = X[i] * Y[i]; -} - -//assign a constant value to all points -template -__global__ void gpu_field_assign(T* ptr, T val, unsigned int r0, unsigned int r1){ - - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //calculate and store the result - ptr[i] = val; -} - -//crop the field to the new dimensions (width x height) -template -__global__ void gpu_field_crop(T* dest, T* source, - unsigned int r0, unsigned int r1, - unsigned int width, unsigned int height){ - - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= width || iv >= height) return; - - //compute the index into the field - int is = iv*r0 + iu; - int id = iv*width + iu; - - //calculate and store the result - dest[id] = source[is]; -} - -template -class field{ - -protected: - - T* X[D]; //pointer to the field data - unsigned int R[2]; //field resolution - stim::rect shape; //position and shape of the field slice - - //calculates the optimal block and grid sizes using information from the GPU - void cuda_params(dim3& grids, dim3& blocks){ - int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - blocks = dim3(SQRT_BLOCK, SQRT_BLOCK); - grids = dim3((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - } - - //find the maximum value of component n - T find_max(unsigned int n){ - cublasStatus_t stat; - cublasHandle_t handle; - - //create a CUBLAS handle - stat = cublasCreate(&handle); - if(stat != CUBLAS_STATUS_SUCCESS){ - std::cout<<"CUBLAS Error: initialization failed"< process_filename(std::string name){ - std::stringstream ss(name); - std::string item; - std::vector elems; - while(std::getline(ss, item, '.')) //split the string at the '.' character (filename and extension) - { - elems.push_back(item); - } - - std::string prefix = elems[0]; //prefix contains the filename (with wildcard '?' characters) - std::string ext = elems[1]; //file extension (ex. .bmp, .png) - ext = std::string(".") + ext; //add a period back into the extension - - size_t i0 = prefix.find_first_of("?"); //find the positions of the first and last wildcard ('?'') - size_t i1 = prefix.find_last_of("?"); - - std::string postfix = prefix.substr(i1+1); - prefix = prefix.substr(0, i0); - - unsigned int digits = i1 - i0 + 1; //compute the number of wildcards - - std::vector flist; //create a vector of file names - //fill the list - for(unsigned int d=0; d>> (X[n], rhs, R[0], R[1]); - - return *this; - } - - //assignment of vector component - field & operator= (const vec rhs){ - - int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //assign the constant value to all positions and dimensions - for(unsigned int n=0; n>> (X[n], rhs.v[n], R[0], R[1]); - - return *this; - - } - - //multiply two fields (element-wise multiplication) - field operator* (const field & rhs){ - - int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //create a scalar field to store the result - field result(R[0], R[1]); - - for(int n=0; n>> (result.X[n], X[n], rhs.X[n], R[0], R[1]); - - return result; - } - - T* ptr(unsigned int n = 0){ - if(n < D) - return X[n]; - else return NULL; - } - - //return the vector component at position (u, v) - vec get(unsigned int u, unsigned int v){ - - vec result; - for(unsigned int d=0; d crop(unsigned int width, unsigned int height){ - int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((width + SQRT_BLOCK -1)/SQRT_BLOCK, (height + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //create a scalar field to store the result - field result(width, height); - - for(int n=0; n>> (result.X[n], X[n], R[0], R[1], width, height); - - return result; - } - - //save an image representing component n - void toImage(std::string filename, unsigned int n = 0, - bool positive = false, stim::colormapType cmap = stim::cmBrewer){ - T max_val = find_max(n); //find the maximum value - - if(positive) //if the field is positive, use the range [0 max_val] - stim::gpu2image(X[n], filename, R[0], R[1], 0, max_val, cmap); - else - stim::gpu2image(X[n], filename, R[0], R[1], -max_val, max_val, cmap); - } - -}; - -} //end namespace rts -#endif diff --git a/math/function.h b/math/function.h deleted file mode 100644 index 033337d..0000000 --- a/math/function.h +++ /dev/null @@ -1,236 +0,0 @@ -#ifndef RTS_FUNCTION_H -#define RTS_FUNCTION_H - -#include - -namespace stim{ - -//template class for a one-dimensional function -template -class function -{ -protected: - - std::vector X; - std::vector Y; - Ty range[2]; - Ty bounding[2]; - - //comparison function for searching lambda - static bool findCeiling(Tx ax, Tx bx){ - return (ax > bx); - } - - //process a string to extract a function (generally delimited by tabs, commas, spaces, etc.) - void process_string(std::string s){ - std::stringstream ss(s); - - Tx x; - Ty y; - std::string line; - - while(!ss.eof()){ - - std::getline(ss, line); - if(line[0] == '#') continue; - - std::stringstream lstream(line); - - lstream>>x; //read the x value - lstream>>y; //read the y value - - if(ss.eof()) break; - insert(x, y); //insert the read value into the function - } - } - - void update_range(Ty y){ - //if the function is empty, just set the range to the given value - if(X.size() == 0){ - range[0] = range[1] = y; - } - - //otherwise compare the values and set them appropriately - if(y < range[0]) range[0] = y; - if(y > range[1]) range[1] = y; - } - - -public: - function(){ - range[0] = range[1] = 0; - bounding[0] = bounding[1] = 0; - } - - //linear interpolation - Ty linear(Tx x) const - { - if(X.size() == 0) return bounding[0]; //return zero if the function is empty - //return bounding values if x is outside the sample domain - if(x < X[0]) return bounding[0]; - if(x > X.back()) return bounding[1]; - - unsigned int N = X.size(); //number of sample points - - //declare an iterator - typedef typename std::vector< Tx >::iterator f_iter; //declare an iterator - f_iter it; - - //find the first X-coordinate that is greater than x - unsigned int i; - for(i = 0; i x) - break; - } - //i currently holds the ceiling - - //if the wavelength is past the end of the list, return the last sample point - if(i == N) return Y.back(); - //if the wavelength is before the beginning of the list, return the front - else if(i == 0) return Y[0]; - //otherwise interpolate - else{ - Tx xMax = X[i]; - Tx xMin = X[i-1]; - - Tx a = (x - xMin) / (xMax - xMin); - Ty riMin = Y[i - 1]; - Ty riMax = Y[i]; - Ty interp = riMax * a + riMin * (1 - a); - return interp; - } - } - - ///add a data point to a function - void insert(Tx x, Ty y) - { - unsigned int N = X.size(); //number of sample points - - update_range(y); //update the range of the function - - if(N == 0 || X[N-1] < x){ - X.push_back(x); - Y.push_back(y); - return; - } - - //declare an iterator and search for the x value - typename std::vector< Tx >::iterator it; - it = search(X.begin(), X.end(), &x, &x + 1, &function::findCeiling); - - //if the function value is past the end of the vector, add it to the back - if(*it == N){ - X.push_back(x); - Y.push_back(y); - } - //otherwise add the value at the iterator position - else{ - X.insert(it, x); - Y.insert(Y.begin() + *it, y); - } - - } - - Tx getX(unsigned int i) const{ - return X[i]; - } - - Ty getY(unsigned int i) const{ - return Y[i]; - } - - ///get the number of data points in the function - unsigned int getN() const{ - return X.size(); - } - - //look up an indexed component - Ty operator[](int i) const{ - if(i <= X.size()){ - std::cout<<"ERROR: accessing non-existing sample point in 'function'"< operator+(Ty r) const{ - - function result; - - //if there are points in the function - if(X.size() > 0){ - //add r to every point in f - for(unsigned int i=0; i & operator= (const Ty & rhs){ - X.clear(); - Y.clear(); - bounding[0] = bounding[1] = rhs; //set the boundary values to rhs - if(rhs != 0) //if the RHS is zero, just clear, otherwise add one value of RHS - insert(0, rhs); - - return *this; - } - - - //output a string description of the function (used for console output and debugging) - std::string str(){ - unsigned int N = X.size(); - - std::stringstream ss; - ss<<"# of samples: "<(t)), - std::istreambuf_iterator()); - - process_string(str); - } - - -}; - -} //end namespace rts - - -#endif diff --git a/math/legendre.h b/math/legendre.h deleted file mode 100644 index 4859740..0000000 --- a/math/legendre.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef RTS_LEGENDRE_H -#define RTS_LEGENDRE_H - -#include "rts/cuda/callable.h" - -namespace stim{ - -template -CUDA_CALLABLE void init_legendre(T x, T& P0, T& P1) -{ - //compute the first two Legendre polynomials - P0 = 1; - P1 = x; -} - -template -CUDA_CALLABLE void shift_legendre(int n, T x, T& P0, T& P1) -{ - //compute the next (order n) Legendre polynomial - T Pnew = ( (2 * n - 1) * x * P1 - (n-1) * P0 ) / n; - - //shift and add the new value to the array - P0 = P1; - P1 = Pnew; -} - -template -CUDA_CALLABLE void legendre(int n, T x, T* P) -{ - P[0] = 1; - - if(n >= 1) - P[1] = x; - - for(int i=2; i<=n; i++) - { - P[i] = ( (2 * i - 1) * x * P[i-1] - (i-1) * P[i-2] ) / i; - } - -} - -} - - -#endif diff --git a/math/matrix.h b/math/matrix.h deleted file mode 100644 index 9c039e1..0000000 --- a/math/matrix.h +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef RTS_MATRIX_H -#define RTS_MATRIX_H - -//#include "rts/vector.h" -#include -#include -#include "vector.h" -#include "../cuda/callable.h" - -namespace stim{ - -template -struct matrix -{ - //the matrix will be stored in column-major order (compatible with OpenGL) - T M[N*N]; - - CUDA_CALLABLE matrix() - { - for(int r=0; r operator=(T rhs) - { - int Nsq = N*N; - for(int i=0; i - CUDA_CALLABLE vec operator*(vec rhs) - { - vec result; - - for(int r=0; r -std::ostream& operator<<(std::ostream& os, stim::matrix M) -{ - os< 3 && __GNUC_MINOR__ > 7 -//template using rtsMatrix = rts::matrix; -//#endif - -#endif diff --git a/math/plane.h b/math/plane.h deleted file mode 100644 index e384ef0..0000000 --- a/math/plane.h +++ /dev/null @@ -1,179 +0,0 @@ -#ifndef RTS_PLANE_H -#define RTS_PLANE_H - -#include -#include "../math/vector.h" -#include "rts/cuda/callable.h" - - -namespace stim{ -template class plane; -} - -template -CUDA_CALLABLE stim::plane operator-(stim::plane v); - -namespace stim{ - -template -class plane{ - - //a plane is defined by a point and a normal - -private: - - vec P; //point on the plane - vec N; //plane normal - - CUDA_CALLABLE void init(){ - P = vec(0, 0, 0); - N = vec(0, 0, 1); - } - - -public: - - //default constructor - CUDA_CALLABLE plane(){ - init(); - } - - CUDA_CALLABLE plane(vec n, vec p = vec(0, 0, 0)){ - P = p; - N = n.norm(); - } - - CUDA_CALLABLE plane(T z_pos){ - init(); - P[2] = z_pos; - } - - //create a plane from three points (a triangle) - CUDA_CALLABLE plane(vec a, vec b, vec c){ - P = c; - N = (c - a).cross(b - a); - if(N.len() == 0) //handle the degenerate case when two vectors are the same, N = 0 - N = 0; - else - N = N.norm(); - } - - template< typename U > - CUDA_CALLABLE operator plane(){ - - plane result(N, P); - return result; - } - - CUDA_CALLABLE vec norm(){ - return N; - } - - CUDA_CALLABLE vec p(){ - return P; - } - - //flip the plane front-to-back - CUDA_CALLABLE plane flip(){ - plane result = *this; - result.N = -result.N; - return result; - } - - //determines how a vector v intersects the plane (1 = intersects front, 0 = within plane, -1 = intersects back) - CUDA_CALLABLE int face(vec v){ - - T dprod = v.dot(N); //get the dot product between v and N - - //conditional returns the appropriate value - if(dprod < 0) - return 1; - else if(dprod > 0) - return -1; - else - return 0; - } - - //determine on which side of the plane a point lies (1 = front, 0 = on the plane, -1 = back) - CUDA_CALLABLE int side(vec p){ - - vec v = p - P; //get the vector from P to the query point p - - return face(v); - } - - //compute the component of v that is perpendicular to the plane - CUDA_CALLABLE vec perpendicular(vec v){ - return N * v.dot(N); - } - - //compute the projection of v in the plane - CUDA_CALLABLE vec parallel(vec v){ - return v - perpendicular(v); - } - - CUDA_CALLABLE void decompose(vec v, vec& para, vec& perp){ - perp = N * v.dot(N); - para = v - perp; - } - - //get both the parallel and perpendicular components of a vector v w.r.t. the plane - CUDA_CALLABLE void project(vec v, vec &v_par, vec &v_perp){ - - v_perp = v.dot(N); - v_par = v - v_perp; - } - - //compute the reflection of v off of the plane - CUDA_CALLABLE vec reflect(vec v){ - - //compute the reflection using N_prime as the plane normal - vec par = parallel(v); - vec r = (-v) + par * 2; - - /*std::cout<<"----------------REFLECT-----------------------------"< operator-() - { - rts::plane p = *this; - - //negate the normal vector - p.N = -p.N; - - return p; - } - - //output a string - std::string str(){ - std::stringstream ss; - ss<<"P: "< operator- <> (rts::plane v); - - - -}; - -} - -//arithmetic operators - -//negative operator flips the plane (front to back) -//template - - - - -#endif diff --git a/math/quad.h b/math/quad.h deleted file mode 100644 index 628b982..0000000 --- a/math/quad.h +++ /dev/null @@ -1,186 +0,0 @@ -#ifndef RTS_QUAD_H -#define RTS_QUAD_H - -//enable CUDA_CALLABLE macro -#include "../cuda/callable.h" -#include "../math/vector.h" -#include "../math/triangle.h" -#include "../math/quaternion.h" -#include -#include -#include - -namespace stim{ - -//template for a quadangle class in ND space -template -struct quad -{ - /* - B------------------>C - ^ ^ - | | - Y | - | | - | | - A---------X-------->O - */ - - /*T A[N]; - T B[N]; - T C[N];*/ - - rts::vec A; - rts::vec X; - rts::vec Y; - - - CUDA_CALLABLE quad() - { - - } - - CUDA_CALLABLE quad(vec a, vec b, vec c) - { - - A = a; - Y = b - a; - X = c - a - Y; - - } - - /******************************************************************* - Constructor - create a quad from a position, normal, and rotation - *******************************************************************/ - CUDA_CALLABLE quad(rts::vec c, rts::vec normal, T width, T height, T theta) - { - - //compute the X direction - start along world-space X - Y = rts::vec(0, 1, 0); - if(Y == normal) - Y = rts::vec(0, 0, 1); - - X = Y.cross(normal).norm(); - - std::cout< q; - q.CreateRotation(theta, normal); - X = q.toMatrix3() * X; - Y = normal.cross(X); - - //normalize everything - X = X.norm(); - Y = Y.norm(); - - //scale to match the quad width and height - X = X * width; - Y = Y * height; - - //set the corner of the plane - A = c - X * 0.5f - Y * 0.5f; - - std::cout< & rhs) - { - if(A == rhs.A && X == rhs.X && Y == rhs.Y) - return true; - else - return false; - } - - /******************************************* - Return the normal for the quad - *******************************************/ - CUDA_CALLABLE rts::vec n() - { - return (X.cross(Y)).norm(); - } - - CUDA_CALLABLE rts::vec p(T a, T b) - { - rts::vec result; - //given the two parameters a, b = [0 1], returns the position in world space - result = A + X * a + Y * b; - - return result; - } - - CUDA_CALLABLE rts::vec operator()(T a, T b) - { - return p(a, b); - } - - std::string str() - { - std::stringstream ss; - - ss<"<<"C="<"<<"D="< operator*(T rhs) - { - //scales the plane by a scalar value - - //compute the center point - rts::vec c = A + X*0.5f + Y*0.5f; - - //create the new quadangle - quad result; - result.X = X * rhs; - result.Y = Y * rhs; - result.A = c - result.X*0.5f - result.Y*0.5f; - - return result; - - } - - CUDA_CALLABLE T dist(vec p) - { - //compute the distance between a point and this quad - - //first break the quad up into two triangles - triangle T0(A, A+X, A+Y); - triangle T1(A+X+Y, A+X, A+Y); - - - T d0 = T0.dist(p); - T d1 = T1.dist(p); - - if(d0 < d1) - return d0; - else - return d1; - } - - CUDA_CALLABLE T dist_max(vec p) - { - T da = (A - p).len(); - T db = (A+X - p).len(); - T dc = (A+Y - p).len(); - T dd = (A+X+Y - p).len(); - - return std::max( da, std::max(db, std::max(dc, dd) ) ); - } -}; - -} //end namespace rts - -template -std::ostream& operator<<(std::ostream& os, rts::quad R) -{ - os< -class quaternion -{ -public: - T w; - T x; - T y; - T z; - - CUDA_CALLABLE void normalize(){ - - double length=sqrt(w*w + x*x + y*y + z*z); - w=w/length; - x=x/length; - y=y/length; - z=z/length; - } - - CUDA_CALLABLE void CreateRotation(T theta, T ux, T uy, T uz){ - - vec u(ux, uy, uz); - CreateRotation(theta, u); - } - - CUDA_CALLABLE void CreateRotation(T theta, vec u){ - - vec u_hat = u.norm(); - - //assign the given Euler rotation to this quaternion - w = (T)cos(theta/2); - x = u_hat[0]*(T)sin(theta/2); - y = u_hat[1]*(T)sin(theta/2); - z = u_hat[2]*(T)sin(theta/2); - } - - CUDA_CALLABLE void CreateRotation(vec from, vec to){ - - vec r = from.cross(to); //compute the rotation vector - T theta = asin(r.len()); //compute the angle of the rotation about r - //deal with a zero vector (both k and kn point in the same direction) - if(theta == (T)0) - return; - - //create a quaternion to capture the rotation - CreateRotation(theta, r.norm()); - } - - - - CUDA_CALLABLE quaternion operator *(quaternion ¶m){ - - float A, B, C, D, E, F, G, H; - - - A = (w + x)*(param.w + param.x); - B = (z - y)*(param.y - param.z); - C = (w - x)*(param.y + param.z); - D = (y + z)*(param.w - param.x); - E = (x + z)*(param.x + param.y); - F = (x - z)*(param.x - param.y); - G = (w + y)*(param.w - param.z); - H = (w - y)*(param.w + param.z); - - quaternion result; - result.w = B + (-E - F + G + H) /2; - result.x = A - (E + F + G + H)/2; - result.y = C + (E - F + G - H)/2; - result.z = D + (E - F - G + H)/2; - - return result; - } - - CUDA_CALLABLE matrix toMatrix3(){ - - matrix result; - - - T wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; - - - // calculate coefficients - x2 = x + x; y2 = y + y; - z2 = z + z; - xx = x * x2; xy = x * y2; xz = x * z2; - yy = y * y2; yz = y * z2; zz = z * z2; - wx = w * x2; wy = w * y2; wz = w * z2; - - result(0, 0) = 1 - (yy + zz); - result(0, 1) = xy - wz; - - result(0, 2) = xz + wy; - - result(1, 0) = xy + wz; - result(1, 1) = 1 - (xx + zz); - - result(1, 2) = yz - wx; - - result(2, 0) = xz - wy; - result(2, 1) = yz + wx; - - result(2, 2) = 1 - (xx + yy); - - return result; - } - - CUDA_CALLABLE matrix toMatrix4(){ - - matrix result; - T wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; - - // calculate coefficients - x2 = x + x; y2 = y + y; - z2 = z + z; - xx = x * x2; xy = x * y2; xz = x * z2; - yy = y * y2; yz = y * z2; zz = z * z2; - wx = w * x2; wy = w * y2; wz = w * z2; - - result(0, 0) = 1 - (yy + zz); - result(0, 1) = xy - wz; - - result(0, 2) = xz + wy; - - result(1, 0) = xy + wz; - result(1, 1) = 1 - (xx + zz); - - result(1, 2) = yz - wx; - - result(2, 0) = xz - wy; - result(2, 1) = yz + wx; - - result(2, 2) = 1 - (xx + yy); - - result(3, 3) = 1; - - return result; - } - - - CUDA_CALLABLE quaternion(){ - w=0; x=0; y=0; z=0; - } - - CUDA_CALLABLE quaternion(T c, T i, T j, T k){ - w=c; x=i; y=j; z=k; - } - -}; - -} //end rts namespace - - -#endif diff --git a/math/realfield.cuh b/math/realfield.cuh deleted file mode 100644 index 7d6ce26..0000000 --- a/math/realfield.cuh +++ /dev/null @@ -1,262 +0,0 @@ -#ifndef RTS_REALFIELD_H -#define RTS_REALFIELD_H - -#include "../visualization/colormap.h" -#include "../envi/envi.h" -#include "../math/rect.h" -#include "../cuda/devices.h" -#include "cublas_v2.h" -#include - - -namespace stim{ - -//multiply R = X * Y -template -__global__ void gpu_realfield_multiply(T* R, T* X, T* Y, unsigned int r0, unsigned int r1){ - - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //calculate and store the result - R[i] = X[i] * Y[i]; - -} - -template -class realfield{ - - P* X[N]; //an array of N gpu pointers for each field component - int R[2]; //resolution of the slice - rect

shape; - - void process_filename(std::string name, std::string &prefix, std::string &postfix, - std::string &ext, unsigned int &digits) - { - std::stringstream ss(name); - std::string item; - std::vector elems; - while(std::getline(ss, item, '.')) //split the string at the '.' character (filename and extension) - { - elems.push_back(item); - } - - prefix = elems[0]; //prefix contains the filename (with wildcard '?' characters) - ext = elems[1]; //file extension (ex. .bmp, .png) - ext = std::string(".") + ext; //add a period back into the extension - - size_t i0 = prefix.find_first_of("?"); //find the positions of the first and last wildcard ('?'') - size_t i1 = prefix.find_last_of("?"); - - postfix = prefix.substr(i1+1); - prefix = prefix.substr(0, i0); - - digits = i1 - i0 + 1; //compute the number of wildcards - } - - void init() - { - for(unsigned int n=0; n(vec

(-1, -1, 0), vec

(-1, 1, 0), vec

(1, 1, 0)); //default geometry - clear(); //zero the field - //std::cout<<"realfield CONSTRUCTOR"<(X[n], filename, R[0], R[1], vmin, vmax, cmap); - } - - void toImages(std::string filename, bool global_max = true, stim::colormapType cmap = stim::cmBrewer) - { - std::string prefix, postfix, extension; - unsigned int digits; - process_filename(filename, prefix, postfix, extension, digits); //process the filename for wild cards - - cublasStatus_t stat; - cublasHandle_t handle; - - //create a CUBLAS handle - stat = cublasCreate(&handle); - if(stat != CUBLAS_STATUS_SUCCESS) - { - std::cout<<"CUBLAS Error: initialization failed"< maxAll) //if maxVal is larger, update the maxAll variable - maxAll = maxVal[n]; - - } - - cublasDestroy(handle); //destroy the CUBLAS handle - - P outputMax = abs(maxAll); //maximum value used for each output image - for(int n=0; n operator* (const realfield & rhs){ - - int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //create a scalar field to store the result - realfield result(R[0], R[1]); - - for(int n=0; n>> (result.X[n], X[n], rhs.X[n], R[0], R[1]); - - return result; - } - - ///copy constructor - realfield(const realfield &rhs) - { - //first make a shallow copy - R[0] = rhs.R[0]; - R[1] = rhs.R[1]; - - for(unsigned int n=0; n -#include -#include - -namespace stim{ - -//template for a rectangle class in ND space -template -struct rect -{ - /* - ^ O - | - | - Y C - | - | - O---------X---------> - */ - -private: - - stim::vec C; - stim::vec X; - stim::vec Y; - - CUDA_CALLABLE void scale(T factor){ - X *= factor; - Y *= factor; - } - - - CUDA_CALLABLE void normal(vec n){ //orient the rectangle along the specified normal - - n = n.norm(); //normalize, just in case - vec n_current = X.cross(Y).norm(); //compute the current normal - quaternion q; //create a quaternion - q.CreateRotation(n_current, n); //initialize a rotation from n_current to n - - //apply the quaternion to the vectors and position - X = q.toMatrix3() * X; - Y = q.toMatrix3() * Y; - } - - CUDA_CALLABLE void init(){ - C = vec(0, 0, 0); - X = vec(1, 0, 0); - Y = vec(0, 1, 0); - } - -public: - - CUDA_CALLABLE rect(){ - init(); - } - - //create a rectangle given a size and position - CUDA_CALLABLE rect(T size, T z_pos = (T)0){ - init(); //use the default setup - scale(size); //scale the rectangle - C[2] = z_pos; - } - - //create a rectangle from a center point, normal, and size - CUDA_CALLABLE rect(T size, vec c, vec n = vec(0, 0, 1)){ - init(); //start with the default setting - C = c; - scale(size); //scale the rectangle - normal(n); //orient - } - - //create a rectangle from a center point, normal, and size - CUDA_CALLABLE rect(vec size, vec c, vec n = vec(0, 0, 1)){ - init(); //start with the default setting - C = c; - scale(size); //scale the rectangle - normal(n); //orient - } - - CUDA_CALLABLE rect(vec center, vec directionX, vec directionY ) - { - C = center; - X = directionX; - Y = directionY; - } - - CUDA_CALLABLE rect(vec mag, vec center, vec directionX, vec directionY ) - { - C = center; - X = directionX; - Y = directionY; - scale(mag[0], mag[1]); - } - - CUDA_CALLABLE void scale(T factor1, T factor2){ - X *= factor1; - Y *= factor2; - } - - //boolean comparison - bool operator==(const rect & rhs) - { - if(C == rhs.C && X == rhs.X && Y == rhs.Y) - return true; - else - return false; - } - - /******************************************* - Return the normal for the rect - *******************************************/ - CUDA_CALLABLE stim::vec n() - { - return (X.cross(Y)).norm(); - } - - //get the world space value given the planar coordinates a, b in [0, 1] - CUDA_CALLABLE stim::vec p(T a, T b) - { - stim::vec result; - //given the two parameters a, b = [0 1], returns the position in world space - vec A = C - X * (T)0.5 - Y * (T)0.5; - result = A + X * a + Y * b; - - return result; - } - - //parenthesis operator returns the world space given rectangular coordinates a and b in [0 1] - CUDA_CALLABLE stim::vec operator()(T a, T b) - { - return p(a, b); - } - - std::string str() - { - std::stringstream ss; - vec A = C - X * (T)0.5 - Y * (T)0.5; - ss<"<<"C="<"<<"D="< operator*(T rhs) - { - //scales the plane by a scalar value - - //create the new rectangle - rect result = *this; - result.scale(rhs); - - return result; - - } - - //computes the distance between the specified point and this rectangle - CUDA_CALLABLE T dist(vec p) - { - //compute the distance between a point and this rect - - vec A = C - X * (T)0.5 - Y * (T)0.5; - - //first break the rect up into two triangles - triangle T0(A, A+X, A+Y); - triangle T1(A+X+Y, A+X, A+Y); - - - T d0 = T0.dist(p); - T d1 = T1.dist(p); - - if(d0 < d1) - return d0; - else - return d1; - } - - CUDA_CALLABLE T dist_max(vec p) - { - vec A = C - X * (T)0.5 - Y * (T)0.5; - T da = (A - p).len(); - T db = (A+X - p).len(); - T dc = (A+Y - p).len(); - T dd = (A+X+Y - p).len(); - - return std::max( da, std::max(db, std::max(dc, dd) ) ); - } -}; - -} //end namespace rts - -template -std::ostream& operator<<(std::ostream& os, stim::rect R) -{ - os< - -namespace stim{ - -template -struct triangle -{ - /* - A------>B - | / - | / - | / - | / - | / - | / - C - */ - private: - - vec A; - vec B; - vec C; - - CUDA_CALLABLE vec _p(T s, T t) - { - //This function returns the point specified by p = A + s(B-A) + t(C-A) - vec E0 = B-A; - vec E1 = C-A; - - return A + s*E0 + t*E1; - } - - - public: - - - - CUDA_CALLABLE triangle() - { - - } - - CUDA_CALLABLE triangle(vec a, vec b, vec c) - { - A = a; - B = b; - C = c; - } - - CUDA_CALLABLE stim::vec operator()(T s, T t) - { - return _p(s, t); - } - - CUDA_CALLABLE vec nearest(vec p) - { - //comptue the distance between a point and this triangle - // This code is adapted from: http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf - - vec E0 = B-A; - vec E1 = C-A; - vec D = A - p; - - T a = E0.dot(E0); - T b = E0.dot(E1); - T c = E1.dot(E1); - T d = E0.dot(D); - T e = E1.dot(D); - //T f = D.dot(D); - - T det = a*c - b*b; - T s = b*e - c*d; - T t = b*d - a*e; - - /*std::cout<<"E0: "<= 0 ? 0 : ( -e >= c ? 1 : -e/c ) ); - //done - } - } - else if(t < 0) - { - //region 5 - //std::cout<<"Region 5"<= 0 ? 0 : ( -d >= a ? 1 : -d/a ) ); - t = 0; - //done - } - else - { - //region 0 - //std::cout<<"Region 0"<= denom ? 1 : numer/denom ); - } - t = 1 - s; - //done - } - } - - //std::cout<<"s: "< p) - { - vec n = nearest(p); - - return (p - n).len(); - } -}; - -} - -#endif diff --git a/math/vector.h b/math/vector.h deleted file mode 100644 index 86fbbfd..0000000 --- a/math/vector.h +++ /dev/null @@ -1,280 +0,0 @@ -#ifndef RTS_VECTOR_H -#define RTS_VECTOR_H - -#include -#include -#include -//#include "rts/math/point.h" -#include "../cuda/callable.h" - - -namespace stim -{ - - - -template -struct vec -{ - T v[N]; - - CUDA_CALLABLE vec() - { - //memset(v, 0, sizeof(T) * N); - for(int i=0; i& other){ - for(int i=0; i - CUDA_CALLABLE operator vec(){ - vec result; - for(int i=0; i - //friend vec::operator vec(); - - CUDA_CALLABLE T len() const - { - //compute and return the vector length - T sum_sq = (T)0; - for(int i=0; i cart2sph() const - { - //convert the vector from cartesian to spherical coordinates - //x, y, z -> r, theta, phi (where theta = 0 to 2*pi) - - vec sph; - sph[0] = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); - sph[1] = std::atan2(v[1], v[0]); - sph[2] = std::acos(v[2] / sph[0]); - - return sph; - } - - CUDA_CALLABLE vec sph2cart() const - { - //convert the vector from cartesian to spherical coordinates - //r, theta, phi -> x, y, z (where theta = 0 to 2*pi) - - vec cart; - cart[0] = v[0] * std::cos(v[1]) * std::sin(v[2]); - cart[1] = v[0] * std::sin(v[1]) * std::sin(v[2]); - cart[2] = v[0] * std::cos(v[2]); - - return cart; - } - - CUDA_CALLABLE vec norm() const - { - //compute and return the vector norm - vec result; - - //compute the vector length - T l = len(); - - //normalize - for(int i=0; i cross(const vec rhs) const - { - vec result; - - //compute the cross product (only valid for 3D vectors) - result[0] = v[1] * rhs.v[2] - v[2] * rhs.v[1]; - result[1] = v[2] * rhs.v[0] - v[0] * rhs.v[2]; - result[2] = v[0] * rhs.v[1] - v[1] * rhs.v[0]; - - return result; - } - - CUDA_CALLABLE T dot(vec rhs) const - { - T result = (T)0; - - for(int i=0; i operator+(vec rhs) const - { - vec result; - - for(int i=0; i operator+(T rhs) const - { - vec result; - - for(int i=0; i operator-(vec rhs) const - { - vec result; - - for(int i=0; i operator*(T rhs) const - { - vec result; - - for(int i=0; i operator/(T rhs) const - { - vec result; - - for(int i=0; i operator*=(T rhs){ - for(int i=0; i & operator=(T rhs){ - for(int i=0; i - CUDA_CALLABLE vec & operator=(vec rhs){ - for(int i=0; i operator-() const{ - vec r; - - //negate the vector - for(int i=0; i rhs) const - { - if ( (rhs.v[0] == v[0]) && (rhs.v[1] == v[1]) && (rhs.v[2] == v[2]) ) - return true; - - return false; - } - - std::string str() const - { - std::stringstream ss; - - ss<<"["; - for(int i=0; i -std::ostream& operator<<(std::ostream& os, stim::vec v) -{ - os< -CUDA_CALLABLE stim::vec operator*(T lhs, stim::vec rhs) -{ - stim::vec r; - - return rhs * lhs; -} - -//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7 -//template using rtsVector = rts::vector; -//#endif - -#endif diff --git a/matlab/rtsApplyBrewer.m b/matlab/rtsApplyBrewer.m deleted file mode 100644 index bdc0693..0000000 --- a/matlab/rtsApplyBrewer.m +++ /dev/null @@ -1,41 +0,0 @@ -function result = rtsApplyBrewer(field, minVal, maxVal) - -if nargin < 3 - maxVal = max(field(:)); -end -if nargin < 2 - minVal = min(field(:)); -end - -fieldMaskMin = field >= minVal; -fieldMaskMax = field <= maxVal; - -fieldMask = min(fieldMaskMin, fieldMaskMax); - -ctrlPts = zeros(11, 3); - -ctrlPts(1, :) = [0.192157, 0.211765, 0.584314]; -ctrlPts(2, :) = [0.270588, 0.458824, 0.705882]; -ctrlPts(3, :) = [0.454902, 0.678431, 0.819608]; -ctrlPts(4, :) = [0.670588, 0.85098, 0.913725]; -ctrlPts(5, :) = [0.878431, 0.952941, 0.972549]; -ctrlPts(6, :) = [1, 1, 0.74902]; -ctrlPts(7, :) = [0.996078, 0.878431, 0.564706]; -ctrlPts(8, :) = [0.992157, 0.682353, 0.380392]; -ctrlPts(9, :) = [0.956863, 0.427451, 0.262745]; -ctrlPts(10, :) = [0.843137, 0.188235, 0.152941]; -ctrlPts(11, :) = [0.647059, 0, 0.14902]; - -%X = 0:1/10:1; -X = minVal:(maxVal - minVal)/10:maxVal; - -R = interp1(X, ctrlPts(:, 1), field); -G = interp1(X, ctrlPts(:, 2), field); -B = interp1(X, ctrlPts(:, 3), field); - -R = R.* fieldMask; -G = G.* fieldMask; -B = B.* fieldMask; - -result = cat(ndims(field)+1, R, G, B); - diff --git a/matlab/rtsBaselineCorrect.m b/matlab/rtsBaselineCorrect.m deleted file mode 100644 index 673296d..0000000 --- a/matlab/rtsBaselineCorrect.m +++ /dev/null @@ -1,61 +0,0 @@ -function R = rtsBaselineCorrect(A, b, dim) - -%This function baselines an n-dimensional array given a series of points b -%B = baseline-corrected data -%A = raw data -%b = baseline points in the format [b0 b1 ... bn] -%dim = dimension along which the baseline correction will occur - - -%allocate space for the sub-component indices -sz = size(A); -inds = repmat({1},1,ndims(A)); - -%allocate space for the result -R = zeros(size(A)); - -%for each baseline point -for bp = 1:length(b) - 1 - - %extract the data at the first baseline point - n = b(bp); - inds = getIndices(A, n, dim); - BP1 = A(inds{:}); - - %extract the data at the second baseline point - n = b(bp+1); - inds = getIndices(A, n, dim); - BP2 = A(inds{:}); - - %for each point in between the baseline points - for p = b(bp):b(bp+1) - - %compute the weighting of the linear function between BP1 and BP2 - a2 = (p - b(bp))/(b(bp+1) - b(bp)); - a1 = 1.0 - a2; - - n = p; - inds = getIndices(A, n, dim); - - baseFunc = a1 * BP1 + a2 * BP2; - R(inds{:}) = A(inds{:}) - baseFunc; - end -end - - -function I = getIndices(A, n, dim) - -%this function returns indices for all elements in S at -%point n along dimension dim - -sz = size(A); -I = repmat({1},1,ndims(A)); - -for d = 1:ndims(A) - if d ~= dim - I{d} = 1:sz(d); - else - I{d} = n; - end -end - diff --git a/matlab/rtsClass2Color.m b/matlab/rtsClass2Color.m deleted file mode 100644 index 5b8df59..0000000 --- a/matlab/rtsClass2Color.m +++ /dev/null @@ -1,15 +0,0 @@ -function C = rtsClass2Color(classes, classList, colorList) - -C = zeros(length(classes), 3); - -for i = 1:length(classes) - - for c = 1:length(classList) - - if classes(i) == classList(c) - C(i, :) = colorList(c, :); - end - - end - -end \ No newline at end of file diff --git a/matlab/rtsColorMapField.m b/matlab/rtsColorMapField.m deleted file mode 100644 index 3ada8c7..0000000 --- a/matlab/rtsColorMapField.m +++ /dev/null @@ -1,30 +0,0 @@ -function Result = rtsColorMapField(F, range) -%creates a color map for the field F and returns an array with -%an additional size-3 color dimension - -R = zeros(size(F)); -G = zeros(size(F)); -B = zeros(size(F)); - -%if no range is specified, estimate it using min and max values -if nargin == 1 - range(1) = min(F(:)); - range(2) = max(F(:)); -end -range_size = range(2) - range(1); - -num_elements = size(F(:)); -for i =1:num_elements - if(F(i) > range(1) && F(i) < range(2)) - hue_degrees = (1 - (F(i) - range(1))/range_size)*240; - HSVval = [hue_degrees 1.0 1.0]; - RGBval = rtsHSVtoRGB(HSVval); - else - RGBval = [0 0 F(i)/range(1)]; - end - R(i) = RGBval(1); - G(i) = RGBval(2); - B(i) = RGBval(3); -end -Result = cat(ndims(F)+1, R, G, B); -imshow(Result); diff --git a/matlab/rtsColorMapRainbow.m b/matlab/rtsColorMapRainbow.m deleted file mode 100644 index 0c79394..0000000 --- a/matlab/rtsColorMapRainbow.m +++ /dev/null @@ -1,37 +0,0 @@ -function result = rtsColorMapRainbow(field, minVal, maxVal) - -if nargin < 3 - maxVal = max(field(:)); -end -if nargin < 2 - minVal = min(field(:)); -end - -fieldMaskMin = field >= minVal; -fieldMaskMax = field <= maxVal; - -fieldMask = fieldMaskMin;%min(fieldMaskMin, fieldMaskMax); - -ctrlPts = zeros(3, 3); - -ctrlPts(1, :) = [0.0, 0.0, 0.5]; -ctrlPts(2, :) = [0.0, 0.0, 1.0]; -ctrlPts(3, :) = [0.0, 1.0, 1.0]; -ctrlPts(4, :) = [0.0, 1.0, 0.0]; -ctrlPts(5, :) = [1.0, 1.0, 0.0]; -ctrlPts(6, :) = [1.0, 0.0, 0.0]; -ctrlPts(7, :) = [0.5, 0.0, 0.0]; - -%X = 0:1/10:1; -X = minVal:(maxVal - minVal)/6:maxVal; - -R = interp1(X, ctrlPts(:, 1), field); -G = interp1(X, ctrlPts(:, 2), field); -B = interp1(X, ctrlPts(:, 3), field); - -%R = R.* fieldMask; -%G = G.* fieldMask; -%B = B.* fieldMask; - -result = cat(ndims(field)+1, R, G, B); - diff --git a/matlab/rtsCropMask.m b/matlab/rtsCropMask.m deleted file mode 100644 index 5ac7fb3..0000000 --- a/matlab/rtsCropMask.m +++ /dev/null @@ -1,21 +0,0 @@ -function new_mask = rtsCropMask(mask, max_pixels) - -%convert to logical -%mask = mask > 0; - -if nnz(mask) < max_pixels - new_mask = mask; -else - j = 0; - for i = 1:length(mask(:)) - if mask(i) > 0 - j = j + 1; - if j == max_pixels - last_pixel = i; - end - end - end - new_mask = zeros(size(mask, 1), size(mask, 2)); - new_mask(1:last_pixel) = mask(1:last_pixel); -end - \ No newline at end of file diff --git a/matlab/rtsEnviClassify.m b/matlab/rtsEnviClassify.m deleted file mode 100644 index 8326995..0000000 --- a/matlab/rtsEnviClassify.m +++ /dev/null @@ -1,39 +0,0 @@ -function C = rtsEnviClassify(B, envi_filename, mask, nFeatures) - -% B = TreeBagger class -% nFeatures = number of features cropped out (0 means use all features) - -if nargin < 4 - nFeatures = 0; -end - - -batchSize = 100000; - - - -Ns = nnz(mask) %get the number of samples to be classified -Nb = ceil(Ns / batchSize); %get the number of batches - -C(Ns, 1) = char(0); - -%open the ENVI file -envi = rtsEnviOpen(envi_filename, [envi_filename '.hdr'], mask); - -for b = 1:Nb - - %load a batch - batch = rtsEnviRead(envi, batchSize); - - %crop out the number of features specified (if it is specified) - if nFeatures ~= 0 - batch = batch(1:nFeatures, :); - end - - %classify the batch - C( (b-1)*batchSize + 1 : (b-1)*batchSize + size(batch, 2) ) = cell2mat(predict(B, batch')); - - disp([num2str( round( b / Nb * 100 ) ) '%']); -end - -rtsEnviClose(envi); \ No newline at end of file diff --git a/matlab/rtsEnviClose.m b/matlab/rtsEnviClose.m deleted file mode 100644 index 3ee3283..0000000 --- a/matlab/rtsEnviClose.m +++ /dev/null @@ -1,3 +0,0 @@ -function rtsEnviClose(enviFID) - -fclose(enviFID.fid); \ No newline at end of file diff --git a/matlab/rtsEnviID.m b/matlab/rtsEnviID.m deleted file mode 100644 index 10f559e..0000000 --- a/matlab/rtsEnviID.m +++ /dev/null @@ -1,8 +0,0 @@ -classdef rtsEnviID < handle - properties - fid - header - mask - fpos - end -end \ No newline at end of file diff --git a/matlab/rtsEnviLoadClasses.m~ b/matlab/rtsEnviLoadClasses.m~ deleted file mode 100644 index ee30933..0000000 --- a/matlab/rtsEnviLoadClasses.m~ +++ /dev/null @@ -1,11 +0,0 @@ -function [F, C] = rtsEnviLoadClasses(filenames, classnames, ppc) - -Nc = size(filenames, 1); - -for c = 1:Nc - %load the masks - image = imread(filenames{c}); - mask = image(:, :, 1)' > 0; - - -end \ No newline at end of file diff --git a/matlab/rtsEnviLoadHeader.m b/matlab/rtsEnviLoadHeader.m deleted file mode 100644 index 2746cc2..0000000 --- a/matlab/rtsEnviLoadHeader.m +++ /dev/null @@ -1,78 +0,0 @@ -function s = rtsEnviLoadHeader(filename) -%create a cell array of fields -s = struct; - -fid = fopen(filename, 'r', 'n', 'US-ASCII'); - -%read the first field and make sure it is "ENVI" -fname = GetFieldName(fid); -if strcmp(fname, 'ENVI') == 0 - disp('Not an ENVI header file'); - return; -end - -while feof(fid) == 0 - fname = GetFieldName(fid); - if feof(fid) == 1 - return; - end - [value, valid] = ReadField(fid, fname); - if valid == 1 - s = setfield(s, fname, value); - end -end -fclose(fid); - -function t = GetFieldName(fid) -string_struct = textscan(fid, '%s', 1, 'Delimiter', '='); -if feof(fid) == 1 - t = []; - return; -end -t = string_struct{1}{1}; -t = strtrim(t); -t(t==' ') = '_'; - -function [v, valid] = ReadField(fid, field_name) -valid = 1; -stringFields = {'file_type', 'interleave', 'sensor_type', 'wavelength_units'}; -intFields = {'samples', 'lines', 'bands', 'header_offset', 'data_type', 'byte_order'}; - -%if the field is "description", read between the brackets -if strcmp(field_name, 'description') == 1 - textscan(fid, '%[{]', 1); - string_struct = textscan(fid, '%[^}]', 1, 'Whitespace', ''); - textscan(fid, '%[}]', 1); - v = string_struct{1}{1}; - v = strtrim(v); - return; -end -if max(strcmp(field_name, intFields)) ~= 0 - v = fscanf(fid, '%d'); - return; -end -if max(strcmp(field_name, stringFields)) ~= 0 - string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n'); - v = string_struct{1}{1}; - v = strtrim(v); - return; -end - -%read and return the wavelength values -if strcmp(field_name, 'wavelength') == 1 - v = []; - textscan(fid, '%[{]', 1); - c = ' '; - while c ~= '}' - new = fscanf(fid, '%f'); - v = [v new]; - c = fscanf(fid, '%c', 1); - end - return; -end - -%if it doesn't match anything, just read until the end of the line -%string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n'); -string_struct = textscan(fid, '%s', 1, 'Delimiter', '\n'); -v = ''; -valid = 0; diff --git a/matlab/rtsEnviLoadImage.m b/matlab/rtsEnviLoadImage.m deleted file mode 100644 index 6a7ab0f..0000000 --- a/matlab/rtsEnviLoadImage.m +++ /dev/null @@ -1,44 +0,0 @@ -function [image, header] = rtsEnviLoadImage(filename, headername) -%enter the file name (no .hdr extension) - -header_file = headername; -header = rtsEnviLoadHeader(header_file); - -if strcmp(header.interleave, 'bsq') == 1 - image = zeros(header.samples, header.lines, header.bands); -end -if strcmp(header.interleave, 'bip') == 1 - image = zeros(header.bands, header.samples, header.lines); -end -if strcmp(header.interleave, 'bil') == 1 - image = zeros(header.samples, header.bands, header.lines); -end - - -file_bytes = header.samples*header.lines*header.bands; -%check to make sure there is enough memory available -if ispc - [~, sys] = memory; - if sys.PhysicalMemory.Available < file_bytes*header.data_type && strcmp(header.interleave, 'bsq') == 0 - strResponse = input('The data set will require virtual memory for permutation. Continue? (y/n) ', 's'); - if strcmp(strResponse, 'n') == 1 - return; - end - end -else - disp('Sorry, I can''t check available memory for this OS. Keep your fingers crossed!'); -end - -fid = fopen(filename); -%skip the header - start reading header_offset bytes from the beginning -fseek(fid, header.header_offset, 'bof'); - -%read the data -image(:) = fread(fid, file_bytes, 'float32'); - -if strcmp(header.interleave, 'bip') == 1 - image = permute(image, [2 3 1]); -end -if strcmp(header.interleave, 'bil') == 1 - image = permute(image, [1 3 2]); -end \ No newline at end of file diff --git a/matlab/rtsEnviLoadTraining.m b/matlab/rtsEnviLoadTraining.m deleted file mode 100644 index 1788a08..0000000 --- a/matlab/rtsEnviLoadTraining.m +++ /dev/null @@ -1,57 +0,0 @@ -function [F, C] = rtsEnviLoadTraining(filenames, classnames, binfile, ppc) - -Nc = length(filenames); %calculate the number of classes - -if nargin < 3 - ppc = 0; -end - -%---------Load the Masks----------- -%first, get the total number of pixels -Ns = 0; -for c = 1:Nc - %load the mask - image = imread(filenames{c}); - - if c == 1 - mask = boolean(zeros(size(image, 2), size(image, 1), Nc)); - end - - if ppc ~= 0 - mask(:, :, c) = rtsRandomizeMask(image(:, :, 1)' > 0, ppc); - else - mask(:, :, c) = image(:, :, 1)' > 0; - end - -end - -%calculate the number of samples -Ns = Ns + nnz(mask); - -%-----------Load the Training Data------------- -disp(['loading ' num2str(Ns) ' samples']); -ci = 1; -for c = 1:Nc - - disp(['loading class ' classnames(c) '...']); - batch = nnz(mask(:, :, c)); - %create an ENVI file object - envi = rtsEnviOpen(binfile, [binfile '.hdr'], mask(:, :, c)); - - if c == 1 - F = zeros(Ns, envi.header.bands); - C(Ns, 1) = char(0); - end - - %read a bunch of spectra - F(ci:ci+batch - 1, :) = rtsEnviRead(envi, batch)'; - C(ci:ci+batch - 1, 1) = repmat([classnames(c)], [batch 1]); - - %update the current index - ci = ci+batch; - - %close the ENVI file - rtsEnviClose(envi); - - disp('done'); -end \ No newline at end of file diff --git a/matlab/rtsEnviOpen.m b/matlab/rtsEnviOpen.m deleted file mode 100644 index badc640..0000000 --- a/matlab/rtsEnviOpen.m +++ /dev/null @@ -1,35 +0,0 @@ -function EID = rtsEnviOpen(filename, headername, mask) - -%opens an ENVI file and returns an ID structure -% filename = name of ENVI file -% headername = name of ENVI header -% FID = structure containing file information -% mask = binary image specifying valid spectra - -%assign the mask variable -if(nargin < 3) - mask = 1; -end - -%open the file and save the file ID -fid = fopen(filename, 'r'); - -%open the ENVI header file -header = rtsEnviLoadHeader(headername); - -%this is currently only valid for BIP files -if(~strcmp(header.interleave, 'bip')) - disp('Error: This function only works for BIP interleave files. Load in ENVI and convert.'); -end -if(header.data_type ~= 4) - disp('Error: This function only works for floating-point files.'); -end - -EID = rtsEnviID; -EID.fid = fid; -EID.header = header; -EID.mask = mask; -EID.fpos = 0; - -%EID = struct('fid', fid, 'header', header, 'mask', mask, 'fpos', 0); - diff --git a/matlab/rtsEnviRandomForest2C_apply.m b/matlab/rtsEnviRandomForest2C_apply.m deleted file mode 100644 index e1d2a7c..0000000 --- a/matlab/rtsEnviRandomForest2C_apply.m +++ /dev/null @@ -1,41 +0,0 @@ -function P = rtsEnviRandomForest2C_apply(RF, EnviFileName, EnviHeaderName, TissueMask, reference) - -%Applies a 2-class random forest classifier to the given image. Only -%pixels specified in TissueMask are classified. The estimated rule data is -%returned as an image sequence. - -%load the tissue mask -maskImage = imread(TissueMask); -maskBinary = (maskImage(:, :, 1) > 0)'; - -nPixels = nnz(maskBinary); -disp(['Number of pixels: ' num2str(nPixels)]); - - -%load the data -disp(['Loading Features: ' EnviFileName]); -tLoadTime = tic; -fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary); -F = rtsEnviRead(fid, nPixels); -rtsEnviClose(fid); -disp(['Time: ' num2str(toc(tLoadTime)) 's']); - -%transpose -F = F'; - -%apply the reference -if nargin == 5 - Fnorm = repmat(F(:, reference), 1, size(F, 2)); - F = F./Fnorm; - F(:, reference) = []; -end - -%classify the data -disp('Classifying...'); -tClassTime = tic; -T = predict(RF, F); -disp(['Time: ' num2str(toc(tClassTime)) 's']); -%Cl = Cpost > threshold; - -%reconstruct the image from the mask -P = rtsMaskReconstruct(T', maskBinary, -1); \ No newline at end of file diff --git a/matlab/rtsEnviRandomForest2C_train.m b/matlab/rtsEnviRandomForest2C_train.m deleted file mode 100644 index 8bf13d7..0000000 --- a/matlab/rtsEnviRandomForest2C_train.m +++ /dev/null @@ -1,76 +0,0 @@ -function RF = rtsEnviRandomForest2C_train(EnviFileName, EnviHeaderName, RoiImages, reference) - -%Creates a 2-class random forest classifier for an Envi BIP file using the -%provided ROI images. The resulting classifier uses regression to map -%class A to 1 and class B to 0. This allows the creation of ROC curves. -% -% EnviFilename - Name of an ENVI BIP file -% RoiImages - Cell array containing names of ROI images (masks) - -%default parameters -maxPixels = 200000; -threshold = 0.5; -nTrees = 100; -nThreads = 8; -%trainPixels = 200000; - -%determine the number of classes -nClasses = length(RoiImages); -%make sure that there are only two classes -if nClasses > 2 - disp('This classifier only supports 2 classes.'); - return; -end - -%for each class, load the training data -T = []; -F = []; -for c = 1:nClasses - - %load the class mask - maskImage = imread(RoiImages{c}); - maskBinary = (maskImage(:, :, 1) > 0)'; - - disp('------------------------------------------------------'); - %load epithelium spectra - disp(['Loading Training Class ' num2str(c) ' pixels: ' EnviFileName]); - tLoadTime = tic; - fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary); - Fc = rtsEnviRead(fid, maxPixels); - rtsEnviClose(fid); - - if c == 1 - Tc = ones(size(Fc, 2), 1); - else - Tc = zeros(size(Fc, 2), 1); - end - - disp(['Time: ' num2str(toc(tLoadTime)) 's']); - - %add features and targets to the final vectors - T = [T; Tc]; - F = [F; Fc']; -end - -%apply the reference -if nargin == 4 - Fnorm = repmat(F(:, reference), 1, size(F, 2)); - F = F./Fnorm; - F(:, reference) = []; -end - -%parallelize if specified -if nThreads > 1 - matlabpool('open', nThreads); - paraoptions = statset('UseParallel', 'always'); -end - -%train the classifier -disp('Creating a Random Forest classifier...'); -tTrainTime = tic; -RF = TreeBagger(nTrees, F, T, 'method', 'regression', 'Options',paraoptions); -disp(['Time: ' num2str(toc(tTrainTime)) 's']); - -if nThreads > 1 - matlabpool close -end \ No newline at end of file diff --git a/matlab/rtsEnviRandomForest2C_validate.m b/matlab/rtsEnviRandomForest2C_validate.m deleted file mode 100644 index 626a03c..0000000 --- a/matlab/rtsEnviRandomForest2C_validate.m +++ /dev/null @@ -1,65 +0,0 @@ -function [Croc, Cauc] = rtsEnviRandomForest2C_validate(RF, EnviFileName, EnviHeaderName, RoiImages, reference) - -%Validates a 2-class random forest classifier, creating ROC curves. - -%parameters -maxPixels = 200000; - -%determine the number of classes -nClasses = length(RoiImages); -%make sure that there are only two classes -if nClasses > 2 - disp('This classifier only supports 2 classes.'); - return; -end - -%for each class, load the validation data -T = []; -F = []; -for c = 1:nClasses - - %load the class mask - maskImage = imread(RoiImages{c}); - maskBinary = (maskImage(:, :, 1) > 0)'; - - disp('------------------------------------------------------'); - %load epithelium spectra - disp(['Loading Validation Class ' num2str(c) ' pixels: ' EnviFileName]); - tLoadTime = tic; - fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary); - Fc = rtsEnviRead(fid, maxPixels); - rtsEnviClose(fid); - - if c == 1 - Tc = ones(size(Fc, 2), 1); - else - Tc = zeros(size(Fc, 2), 1); - end - - disp(['Time: ' num2str(toc(tLoadTime)) 's']); - - %add features and targets to the final vectors - T = [T; Tc]; - F = [F; Fc']; -end - -%apply the reference -if nargin == 5 - Fnorm = repmat(F(:, reference), 1, size(F, 2)); - F = F./Fnorm; - F(:, reference) = []; -end - -%classify the data (get the estimated posterior probability) -disp('Validating...'); -tClassTime = tic; -Tpost = predict(RF, F); -disp(['Time: ' num2str(toc(tClassTime)) 's']); - -%calculate and display the ROC curve -disp('Calculating ROC...'); -tRocTime = tic; -[Croc, Cauc, threshList] = rtsROC(Tpost, T); -disp(['Time: ' num2str(toc(tRocTime)) 's']); -disp(['AUC = ' num2str(Cauc)]); -plot(Croc(:, 1), Croc(:, 2)); \ No newline at end of file diff --git a/matlab/rtsEnviRead.m b/matlab/rtsEnviRead.m deleted file mode 100644 index d46db6c..0000000 --- a/matlab/rtsEnviRead.m +++ /dev/null @@ -1,68 +0,0 @@ -function S = rtsEnviRead(fid, batchSize) - -%This function reads a batch of spectra from the given ENVI file ID -% fid = ENVI file ID created using rtsEnviOpen -% batchSize = number of spectra to read - -%if there is no mask, just load each spectrum in order -if(fid.mask == 1) - - %compute the new batch size in case we are near the eof - nSpectra = fid.header.samples * fid.header.lines; - remainingSpectra = nSpectra - fid.fpos; - - if(batchSize > remainingSpectra) - batchSize = remainingSpectra; - end - - S = fread(fid.fid, [fid.header.bands batchSize], '*float32'); - - %increment the fid counter - fid.fpos = fid.fpos + batchSize; - -%otherwise only load valid spectra -else - - %compute the new batch size in case we are near the eof - if(fid.fpos == 0) - remainingSpectra = nnz(fid.mask); - else - nSpectra = nnz(fid.mask); - maskRead = fid.mask(1:fid.fpos+1); - remainingSpectra = nSpectra - nnz(maskRead); - end - - if(batchSize > remainingSpectra) - batchSize = remainingSpectra; - end - - %allocate space for the spectra - S = zeros(fid.header.bands, batchSize); - - %for each spectrum in the batch - for s = 1:batchSize - - %while the current spectrum is invalid - skip = 0; - while (~fid.mask(fid.fpos + 1)) - %read the invalid spectrum - %invalid = fread(fid.fid, [fid.header.bands 1], '*float32'); - skip = skip + 1; - - %increment the file position - fid.fpos = fid.fpos + 1; - end - fseek(fid.fid, skip * fid.header.bands * 4, 'cof'); - - test = fread(fid.fid, [fid.header.bands 1], '*float32'); - if size(test) ~= size(S(:, s)) - size(test) - size(S(:, s)) - end - S(:, s) = test; - fid.fpos = fid.fpos + 1; - - end - -end - diff --git a/matlab/rtsEnviSaveImage.m b/matlab/rtsEnviSaveImage.m deleted file mode 100644 index a22402b..0000000 --- a/matlab/rtsEnviSaveImage.m +++ /dev/null @@ -1,41 +0,0 @@ -function rtsEnviSaveImage(S, fileName, headerName, W) - -% S = 3D hyperspectral data -% W = list of wavelength values - -%save the image -rtsSaveRAW(S, fileName, 'float32', 'l'); - -%create the header file -headerFile = [fileName '.hdr']; -if nargin == 3 - headerFile = headerName; -end - -fid = fopen(headerFile, 'w'); - -%write the header and description -fprintf(fid, 'ENVI\n'); -fprintf(fid, 'description = {\n'); -fprintf(fid, ' File Saved using Matlab.}\n'); -fprintf(fid, 'samples = %d\n', size(S, 1)); -fprintf(fid, 'lines = %d\n', size(S, 2)); -fprintf(fid, 'bands = %d\n', size(S, 3)); -fprintf(fid, 'header offset = 0\n'); -fprintf(fid, 'file type = ENVI Standard\n'); -fprintf(fid, 'data type = 4\n'); -fprintf(fid, 'interleave = bsq\n'); -fprintf(fid, 'sensor type = Unknown\n'); -fprintf(fid, 'byte order = 0\n'); - -if nargin == 4 - %output wavelength units - fprintf(fid, 'wavelength = {\n'); - for i = 1:length(W)-1 - fprintf(fid, ' %f,\n', W(i)); - end - fprintf(fid, ' %f\n', W(length(W))); - fprintf(fid, '}\n'); -end - -fclose(fid); diff --git a/matlab/rtsGaussianBlurND.m b/matlab/rtsGaussianBlurND.m deleted file mode 100644 index 3699850..0000000 --- a/matlab/rtsGaussianBlurND.m +++ /dev/null @@ -1,30 +0,0 @@ -function R = rtsGaussianBlurND(input, sigma) -%This function convolves an ND array with a gaussian kernel specified by -%sigma. This takes advantage of the separability of the kernel and is -%therefore very fast. - -%disp('begin***********************************************'); - -%get the number of dimensions for the convolution -dim = ndims(input); - -%initialize the result to the source -R = input; - - -for d=1:dim - %if the dimension is greater than 1 and the sigma not a dirac - if size(input, d) > 1 && sigma(d) > 0 - %create a 1D kernel - kernelshape = ones(1, dim); - kernelshape(d) = sigma(d)*6; - kernel = zeros(kernelshape); - kernel(:) = ndgauss(sigma(d)*6, sigma(d)); - - %perform the convolution - R = convn(R, kernel, 'same'); - end -end - - - \ No newline at end of file diff --git a/matlab/rtsHSVtoRGB.m b/matlab/rtsHSVtoRGB.m deleted file mode 100644 index 9770aea..0000000 --- a/matlab/rtsHSVtoRGB.m +++ /dev/null @@ -1,29 +0,0 @@ -function RGBval = rtsHSVtoRGB(HSVval) - -H = HSVval(1); -S = HSVval(2); -V = HSVval(3); - -C = V*S; -Hprime = H/60; -X = C*(1 - abs(mod(Hprime, 2) - 1)); - -RGBval = [0 0 0]; -if Hprime >= 0 && Hprime < 1 - RGBval = [C X 0]; -end -if Hprime >= 1 && Hprime < 2 - RGBval = [X C 0]; -end -if Hprime >= 2 && Hprime < 3 - RGBval = [0 C X]; -end -if Hprime >= 3 && Hprime < 4 - RGBval = [0 X C]; -end -if Hprime >= 4 && Hprime < 5 - RGBval = [X 0 C]; -end -if Hprime >= 5 && Hprime < 6 - RGBval = [C 0 X]; -end \ No newline at end of file diff --git a/matlab/rtsInt2Str.m b/matlab/rtsInt2Str.m deleted file mode 100644 index 06b9b8d..0000000 --- a/matlab/rtsInt2Str.m +++ /dev/null @@ -1,20 +0,0 @@ -function result = rtsInt2Str(num, digits) - - -%get the number of digits in the current number -temp = num; -current_digits = 0; -while temp >= 1 - temp = temp/10; - current_digits = current_digits+1; -end - -if current_digits > digits - result = int2str(num); -else - result = []; - for i = 1:digits - current_digits - result = [result '0']; - end - result = [result int2str(num)]; -end \ No newline at end of file diff --git a/matlab/rtsLoadImageStack.m b/matlab/rtsLoadImageStack.m deleted file mode 100644 index 15c921a..0000000 --- a/matlab/rtsLoadImageStack.m +++ /dev/null @@ -1,33 +0,0 @@ -function R = rtsLoadImageStack(directoryName) - -fileList = dir(directoryName); -fileList = fileList(3:length(fileList)); - -nFiles = length(fileList); - -%enable the progress bar -gui_active(1); -h = progressbar([], 0, ['Loading ' num2str(nFiles) ' Images...'], 'Load Stack'); - - -%load each file into a volume -for i=1:nFiles - fileName = [directoryName '\' fileList(i).name]; - image = imread(fileName); - - %allocate space - if i == 1 - R = zeros(size(image, 1), size(image, 2), nFiles, 'uint8'); - end - R(:, :, i) = image(:, :, 1); - - h = progressbar(h, 1/(nFiles)); - if ~gui_active - progressbar(h, -1); - break; - end -end - -progressbar(h, -1); - - diff --git a/matlab/rtsMaskReconstruct.m b/matlab/rtsMaskReconstruct.m deleted file mode 100644 index eed102e..0000000 --- a/matlab/rtsMaskReconstruct.m +++ /dev/null @@ -1,35 +0,0 @@ -function I = rtsMaskReconstruct(data, mask, fillVal) - -%This function reconstructs an array of data created from a mask -% data = a VxD matrix, where D = positions and V = values -% mask = a binary image with D nonzero values - -%compute the number of values at each pixel -nV = size(data, 1); - -%allocate space -nX = size(mask, 1); -nY = size(mask, 2); -I = ones(nV, nX*nY) * fillVal; - -d=1; -%for each position -for p = 1:nX*nY - - %if the mask value at the position is true, copy values - if(mask(p) && d < size(data, 2)) - I(:, p) = data(:, d); - - %increment the data pointer - d = d + 1; - - %if the mask value at the position is zero, set all values to zero - else - I(:, p) = ones(nV, 1) * fillVal; - end - -end - -%reshape the array into an image -I = reshape(I', [nX nY nV]); - diff --git a/matlab/rtsMonteCarloSphere.m b/matlab/rtsMonteCarloSphere.m deleted file mode 100644 index 0d80b0b..0000000 --- a/matlab/rtsMonteCarloSphere.m +++ /dev/null @@ -1,29 +0,0 @@ -function S = rtsMonteCarloSphere(sqrtN, startAngle, endAngle) - -%allocate space for the samples -S = zeros(sqrtN^2, 2); - -if sqrtN == 1 - S = [0 0]; - return; -end -cosStart = cos(startAngle); -cosEnd = cos(endAngle); - -i=1; % array index -oneoverN = 1.0/sqrtN; -for a = 0:sqrtN-1 - for b = 0:sqrtN-1 - %generate unbiased distribution of spherical coords - U1 = rand(1, 1); - U2 = rand(1, 1); - %x = 1.0 - (a + U1) * oneoverN * (1.0 - cosEnd) - x = cosStart - (a + U1) * oneoverN * (1.0 - cosEnd); - y = (b + U2) * oneoverN; - theta = acos(x); - phi = 2.0 * pi * y; - S(i, 1) = theta; - S(i, 2) = phi; - i = i+1; - end -end diff --git a/matlab/rtsOffsetPlots.m b/matlab/rtsOffsetPlots.m deleted file mode 100644 index f3e7e84..0000000 --- a/matlab/rtsOffsetPlots.m +++ /dev/null @@ -1,11 +0,0 @@ -function R = rtsOffsetPlots(D, s) -%This function offsets a series of plots in D by intervals of s -%R = the resulting offset data -%D = the data as a 2D matrix -%s = the interval used as an offset - - nPlots = size(D, 2); - - offsetMat = repmat(1:nPlots, size(D, 1), 1) * s; - - R = D + offsetMat; \ No newline at end of file diff --git a/matlab/rtsRAW2Images.m b/matlab/rtsRAW2Images.m deleted file mode 100644 index eea8065..0000000 --- a/matlab/rtsRAW2Images.m +++ /dev/null @@ -1,8 +0,0 @@ -function rtsRAW2Images(source, destination_directory) - -for i=1:size(source, 3) - filename = [destination_directory '\' rtsInt2Str(i, 4) '.bmp']; - imwrite(source(:, :, i)', filename); - disp(filename); -end - \ No newline at end of file diff --git a/matlab/rtsROC.m b/matlab/rtsROC.m deleted file mode 100644 index 82721b1..0000000 --- a/matlab/rtsROC.m +++ /dev/null @@ -1,35 +0,0 @@ -function [ROC, AUC, T] = rtsROC(thresholds, class) - -%Computes the ROC curves given a set of thresholds and class assignments -% class = boolean array describing actual class membership -% thresholds = threshold values for class assignment -% ROC = ROC curve as column vectors (1 = 1-specificity, 2 = sensitivity) -% AUC = area under the ROC curve computed using the trapezoid method -% T = threshold values associated with each 1-specificity value - -nValues = length(thresholds); -ROC = zeros(nValues, 2); - -%sort both arrays -[T, ix] = sort(thresholds, 'descend'); -C = class(ix); - -%compute the necessary global values -P = nnz(class); -N = nValues - P; - -for i = 1:nValues - TP = nnz(C(1:i)); - sensitivity = TP/P; - - FP = i - TP; - FPR = FP/N; - specificity = 1 - FPR; - - ROC(i, 1) = 1 - specificity; - ROC(i, 2) = sensitivity; -end - -AUC = trapz(ROC(:, 1), ROC(:, 2)); - - \ No newline at end of file diff --git a/matlab/rtsRandomizeMask.m b/matlab/rtsRandomizeMask.m deleted file mode 100644 index 62866d3..0000000 --- a/matlab/rtsRandomizeMask.m +++ /dev/null @@ -1,22 +0,0 @@ -function R = rtsRandomizeMask(mask, N) - -%error checking -if N > nnz(mask) - N = nnz(mask); -end - -if N < 0 - N = 0; -end - -%get the indices of all nonzero values in the mask -ind = find(mask); - -%randomize the indices -rind = ind(randperm(size(ind, 1))); - -%create the new randomized mask (random subset of the old mask) -R = zeros(size(mask)); -R(rind(1:N)) = 1; - -R = R > 0; \ No newline at end of file diff --git a/matlab/rtsSaveRAW.m b/matlab/rtsSaveRAW.m deleted file mode 100644 index 042b5e5..0000000 --- a/matlab/rtsSaveRAW.m +++ /dev/null @@ -1,11 +0,0 @@ -function rtsSaveRAW(data, filename, type, endian) -%Loads a RAW image file. -%LoadRAW(filename, x, y, z, c, type) returns a [CxXxYxZ] array representing -%RAW data loaded from disk. 'type' defines the data type to be loaded (ex. -%'uint8'. 'endian' defines the byte order: 'b' = big endian, 'l' = little -%endian. - -file_id = fopen(filename, 'w', endian); -vol = zeros(size(data, 1), size(data, 2), size(data, 3), size(data, 4)); -vol(:) = fwrite(file_id, data, type); -fclose(file_id); \ No newline at end of file diff --git a/matlab/stimBrewerColormap.m b/matlab/stimBrewerColormap.m deleted file mode 100644 index 34568a0..0000000 --- a/matlab/stimBrewerColormap.m +++ /dev/null @@ -1,28 +0,0 @@ -function result = stimBrewerColormap(R) - -%returns a Brewer colormap with the specified resolution R - -ctrlPts = zeros(11, 3); - -ctrlPts(1, :) = [0.192157, 0.211765, 0.584314]; -ctrlPts(2, :) = [0.270588, 0.458824, 0.705882]; -ctrlPts(3, :) = [0.454902, 0.678431, 0.819608]; -ctrlPts(4, :) = [0.670588, 0.85098, 0.913725]; -ctrlPts(5, :) = [0.878431, 0.952941, 0.972549]; -ctrlPts(6, :) = [1, 1, 0.74902]; -ctrlPts(7, :) = [0.996078, 0.878431, 0.564706]; -ctrlPts(8, :) = [0.992157, 0.682353, 0.380392]; -ctrlPts(9, :) = [0.956863, 0.427451, 0.262745]; -ctrlPts(10, :) = [0.843137, 0.188235, 0.152941]; -ctrlPts(11, :) = [0.647059, 0, 0.14902]; - -X = 1:11; - -r = 1:11/R:11; - -R = interp1(X, ctrlPts(:, 1), r); -G = interp1(X, ctrlPts(:, 2), r); -B = interp1(X, ctrlPts(:, 3), r); - -result = [R' G' B']; - diff --git a/optics/beam.h b/optics/beam.h deleted file mode 100644 index 8587bb4..0000000 --- a/optics/beam.h +++ /dev/null @@ -1,203 +0,0 @@ -#ifndef RTS_BEAM -#define RTS_BEAM - -#include "../math/vector.h" -#include "../math/function.h" -#include "../optics/planewave.h" -#include - -namespace stim{ - -template -class beam : public planewave

-{ -public: - enum beam_type {Uniform, Bartlett, Hamming, Hanning}; - -private: - - P _na[2]; //numerical aperature of the focusing optics - vec

f; //focal point - function apod; //apodization function - unsigned int apod_res; //resolution of apodization filter functions - - void apod_uniform() - { - apod = (P)1; - } - void apod_bartlett() - { - apod = (P)1; - apod.insert((P)1, (P)0); - } - void apod_hanning() - { - apod = (P)0; - P x, y; - for(unsigned int n=0; n k = rts::vec

(0, 0, rtsTAU), - vec

_E0 = rts::vec

(1, 0, 0), - beam_type _apod = Uniform) - : planewave

(k, _E0) - { - _na[0] = (P)0.0; - _na[1] = (P)1.0; - f = vec

( (P)0, (P)0, (P)0 ); - apod_res = 256; //set the default resolution for apodization filters - set_apod(_apod); //set the apodization function type - } - - beam

refract(rts::vec

kn) const{ - - beam

new_beam; - new_beam._na[0] = _na[0]; - new_beam._na[1] = _na[1]; - - - rts::planewave

pw = planewave

::bend(kn); - //std::cout< > mc(unsigned int N = 100000, unsigned int seed = 0) const - { - /*Create Monte-Carlo samples of a cassegrain objective by performing uniform sampling - of a sphere and projecting these samples onto an inscribed sphere. - - seed = seed for the random number generator - */ - srand(seed); //seed the random number generator - - vec

k_hat = beam::k.norm(); - - ///compute the rotation operator to transform (0, 0, 1) to k - P cos_angle = k_hat.dot(rts::vec

(0, 0, 1)); - rts::matrix rotation; - - //if the cosine of the angle is -1, the rotation is just a flip across the z axis - if(cos_angle == -1){ - rotation(2, 2) = -1; - } - else if(cos_angle != 1.0) - { - rts::vec

r_axis = rts::vec

(0, 0, 1).cross(k_hat).norm(); //compute the axis of rotation - P angle = acos(cos_angle); //compute the angle of rotation - rts::quaternion

quat; //create a quaternion describing the rotation - quat.CreateRotation(angle, r_axis); - rotation = quat.toMatrix3(); //compute the rotation matrix - } - - //find the phi values associated with the cassegrain ring - P PHI[2]; - PHI[0] = (P)asin(_na[0]); - PHI[1] = (P)asin(_na[1]); - - //calculate the z-axis cylinder coordinates associated with these angles - P Z[2]; - Z[0] = cos(PHI[0]); - Z[1] = cos(PHI[1]); - P range = Z[0] - Z[1]; - - std::vector< planewave

> samples; //create a vector of plane waves - - //draw a distribution of random phi, z values - P z, phi, theta; - for(int i=0; i spherical(1, theta, phi); //convert from spherical to cartesian coordinates - rts::vec

cart = spherical.sph2cart(); - vec

k_prime = rotation * cart; //create a sample vector - - //store a wave refracted along the given direction - //std::cout<<"k prime: "<::refract(k_prime) * apod(phi/PHI[1])); - } - - return samples; - } - - std::string str() - { - std::stringstream ss; - ss<<"Beam:"< -__global__ void gpu_planewave2efield(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, - planewave w, rect q) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //get the current position - vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); - vec r(p[0], p[1], p[2]); - - complex x( 0.0f, w.k.dot(r) ); - - if(Y == NULL) //if this is a scalar simulation - X[i] += w.E0.len() * exp(x); //use the vector magnitude as the plane wave amplitude - else - { - X[i] += w.E0[0] * exp(x); - Y[i] += w.E0[1] * exp(x); - Z[i] += w.E0[2] * exp(x); - } -} - -template -__global__ void gpu_efield_magnitude(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, T* M) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - if(Y == NULL) //if this is a scalar simulation - M[i] = X[i].abs(); //compute the magnitude of the X component - else - { - M[i] = rts::vec(X[i].abs(), Y[i].abs(), Z[i].abs()).len(); - //M[i] = Z[i].abs(); - } -} - -template -__global__ void gpu_efield_real_magnitude(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, T* M) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - if(Y == NULL) //if this is a scalar simulation - M[i] = X[i].abs(); //compute the magnitude of the X component - else - { - M[i] = rts::vec(X[i].real(), Y[i].real(), Z[i].real()).len(); - //M[i] = Z[i].abs(); - } -} - -template -__global__ void gpu_efield_polarization(complex* X, complex* Y, complex* Z, - unsigned int r0, unsigned int r1, - T* Px, T* Py, T* Pz) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //compute the field polarization - Px[i] = X[i].abs(); - Py[i] = Y[i].abs(); - Pz[i] = Z[i].abs(); - -} - -/* This function computes the sum of two complex fields and stores the result in *dest -*/ -template -__global__ void gpu_efield_sum(complex* dest, complex* src, unsigned int r0, unsigned int r1) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //sum the fields - dest[i] += src[i]; -} - -/* This class implements a discrete representation of an electromagnetic field - in 2D. The majority of this representation is done on the GPU. -*/ -template -class efield -{ -protected: - - bool vector; - - //gpu pointer to the field data - rts::complex* X; - rts::complex* Y; - rts::complex* Z; - - //resolution of the discrete field - unsigned int R[2]; - - //shape of the 2D field - rect pos; - - void from_planewave(planewave p) - { - unsigned int SQRT_BLOCK = 16; - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - - gpu_planewave2efield <<>> (X, Y, Z, R[0], R[1], p, pos); - } - - void init(unsigned int res0, unsigned int res1, bool _vector) - { - vector = _vector; //initialize field type - - X = Y = Z = NULL; //initialize all pointers to NULL - R[0] = res0; - R[1] = res1; - - //allocate memory on the gpu - cudaMalloc(&X, sizeof(rts::complex) * R[0] * R[1]); - cudaMemset(X, 0, sizeof(rts::complex) * R[0] * R[1]); - - if(vector) - { - cudaMalloc(&Y, sizeof(rts::complex) * R[0] * R[1]); - cudaMemset(Y, 0, sizeof(rts::complex) * R[0] * R[1]); - - cudaMalloc(&Z, sizeof(rts::complex) * R[0] * R[1]); - cudaMemset(Z, 0, sizeof(rts::complex) * R[0] * R[1]); - } - } - - void destroy() - { - if(X != NULL) cudaFree(X); - if(Y != NULL) cudaFree(Y); - if(Z != NULL) cudaFree(Z); - } - - void shallowcpy(const rts::efield & src) - { - vector = src.vector; - R[0] = src.R[0]; - R[1] = src.R[1]; - } - - void deepcpy(const rts::efield & src) - { - //perform a shallow copy - shallowcpy(src); - - //allocate memory on the gpu - if(src.X != NULL) - { - cudaMalloc(&X, sizeof(rts::complex) * R[0] * R[1]); - cudaMemcpy(X, src.X, sizeof(rts::complex) * R[0] * R[1], cudaMemcpyDeviceToDevice); - } - if(src.Y != NULL) - { - cudaMalloc(&Y, sizeof(rts::complex) * R[0] * R[1]); - cudaMemcpy(Y, src.Y, sizeof(rts::complex) * R[0] * R[1], cudaMemcpyDeviceToDevice); - } - if(src.Z != NULL) - { - cudaMalloc(&Z, sizeof(rts::complex) * R[0] * R[1]); - cudaMemcpy(Z, src.Z, sizeof(rts::complex) * R[0] * R[1], cudaMemcpyDeviceToDevice); - } - } - -public: - efield(unsigned int res0, unsigned int res1, bool _vector = true) - { - init(res0, res1, _vector); - //pos = rts::rect(rts::vec(-10, 0, -10), rts::vec(-10, 0, 10), rts::vec(10, 0, 10)); - } - - //destructor - ~efield() - { - destroy(); - } - - ///Clear the field - set all points to zero - void clear() - { - cudaMemset(X, 0, sizeof(rts::complex) * R[0] * R[1]); - - if(vector) - { - cudaMemset(Y, 0, sizeof(rts::complex) * R[0] * R[1]); - cudaMemset(Z, 0, sizeof(rts::complex) * R[0] * R[1]); - } - } - - void position(rect _p) - { - pos = _p; - } - - //access functions - complex* x(){ - return X; - } - complex* y(){ - return Y; - } - complex* z(){ - return Z; - } - unsigned int Ru(){ - return R[0]; - } - unsigned int Rv(){ - return R[1]; - } - rect p(){ - return pos; - } - - std::string str() - { - stringstream ss; - ss< & operator= (const efield & rhs) - { - destroy(); //destroy any previous information about this field - deepcpy(rhs); //create a deep copy - return *this; //return the current object - } - - //assignment operator: build an electric field from a plane wave - efield & operator= (const planewave & rhs) - { - - clear(); //clear any previous field data - from_planewave(rhs); //create a field from the planewave - return *this; //return the current object - } - - //assignment operator: add an existing electric field - efield & operator+= (const efield & rhs) - { - //if this field and the source field represent the same regions in space - if(R[0] == rhs.R[0] && R[1] == rhs.R[1] && pos == rhs.pos) - { - int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //sum the fields - gpu_efield_sum <<>> (X, rhs.X, R[0], R[1]); - if(Y != NULL) - { - gpu_efield_sum <<>> (Y, rhs.Y, R[0], R[1]); - gpu_efield_sum <<>> (Z, rhs.Z, R[0], R[1]); - } - } - else - { - std::cout<<"ERROR in efield: The two summed fields do not represent the same geometry."< & operator= (const rts::beam & rhs) - { - //get a vector of monte-carlo samples - std::vector< rts::planewave > p_list = rhs.mc(); - - clear(); //clear any previous field data - for(unsigned int i = 0; i < p_list.size(); i++) - from_planewave(p_list[i]); - return *this; - } - - - //return a scalar field representing field magnitude - realfield mag() - { - int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create a scalar field to store the result - realfield M(R[0], R[1]); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //compute the magnitude and store it in a scalar field - gpu_efield_magnitude <<>> (X, Y, Z, R[0], R[1], M.ptr(0)); - - return M; - } - - //return a sacalar field representing the field magnitude at an infinitely small point in time - realfield real_mag() - { - int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create a scalar field to store the result - realfield M(R[0], R[1]); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //compute the magnitude and store it in a scalar field - gpu_efield_real_magnitude <<>> (X, Y, Z, R[0], R[1], M.ptr(0)); - - return M; - } - - //return a vector field representing field polarization - realfield polarization() - { - if(!vector) - { - std::cout<<"ERROR: Cannot compute polarization of a scalar field."< Pol(R[0], R[1]); //create a vector field to store the result - - //compute the polarization and store it in the vector field - gpu_efield_polarization <<>> (X, Y, Z, R[0], R[1], Pol.ptr(0), Pol.ptr(1), Pol.ptr(2)); - - return Pol; //return the vector field - } - - ////////FRIENDSHIP - //friend void operator<< (rts::efield &ef, rts::halfspace hs); - - -}; - - - -} //end namespace rts - -#endif diff --git a/optics/esphere.cuh b/optics/esphere.cuh deleted file mode 100644 index 1b5a559..0000000 --- a/optics/esphere.cuh +++ /dev/null @@ -1,162 +0,0 @@ -#ifndef RTS_ESPHERE -#define RTS_ESPHERE - -#include "../math/complex.h" -#include "../math/bessel.h" -#include "../visualization/colormap.h" -#include "../optics/planewave.h" -#include "../cuda/devices.h" -#include "../optics/efield.cuh" - -namespace stim{ - -/* This class implements a discrete representation of an electromagnetic field - in 2D scattered by a sphere. This class implements Mie scattering. -*/ -template -class esphere : public efield

-{ -private: - stim::complex

n; //sphere refractive index - P a; //sphere radius - - //parameters dependent on wavelength - unsigned int Nl; //number of orders for the calculation - rts::complex

* A; //internal scattering coefficients - rts::complex

* B; //external scattering coefficients - - void calcNl(P kmag) - { - //return ceil( ((P)6.282 * a) / lambda + 4 * pow( ((P)6.282 * a) / lambda, ((P)1/(P)3)) + 2); - Nl = ceil( kmag*a + 4 * pow(kmag * a, (P)1/(P)3) + 2); - } - - void calcAB(P k, unsigned int Nl, rts::complex

* A, rts::complex

* B) - { - /* These calculations require double precision, so they are computed - using doubles and converted to P at the end. - - Input: - - k = magnitude of the k vector (tau/lambda) - Nl = highest order coefficient ([0 Nl] are computed) - */ - - //clear the previous coefficients - rts::complex

* cpuA = (rts::complex

*)malloc(sizeof(rts::complex

) * (Nl+1)); - rts::complex

* cpuB = (rts::complex

*)malloc(sizeof(rts::complex

) * (Nl+1)); - - //convert to an std complex value - complex nc = (rts::complex)n; - - //compute the magnitude of the k vector - complex kna = nc * k * (double)a; - - //compute the arguments k*a and k*n*a - complex ka(k * a, 0.0); - - //allocate space for the Bessel functions of the first and second kind (and derivatives) - unsigned int bytes = sizeof(complex) * (Nl + 1); - complex* cjv_ka = (complex*)malloc(bytes); - complex* cyv_ka = (complex*)malloc(bytes); - complex* cjvp_ka = (complex*)malloc(bytes); - complex* cyvp_ka = (complex*)malloc(bytes); - complex* cjv_kna = (complex*)malloc(bytes); - complex* cyv_kna = (complex*)malloc(bytes); - complex* cjvp_kna = (complex*)malloc(bytes); - complex* cyvp_kna = (complex*)malloc(bytes); - - //allocate space for the spherical Hankel functions and derivative - complex* chv_ka = (complex*)malloc(bytes); - complex* chvp_ka = (complex*)malloc(bytes); - - //compute the bessel functions using the CPU-based algorithm - double vm; - cbessjyva_sph(Nl, ka, vm, cjv_ka, cyv_ka, cjvp_ka, cyvp_ka); - cbessjyva_sph(Nl, kna, vm, cjv_kna, cyv_kna, cjvp_kna, cyvp_kna); - - //compute A for each order - complex i(0, 1); - complex a, b, c, d; - complex An, Bn; - for(int l=0; l<=Nl; l++) - { - //compute the Hankel functions from j and y - chv_ka[l] = cjv_ka[l] + i * cyv_ka[l]; - chvp_ka[l] = cjvp_ka[l] + i * cyvp_ka[l]; - - //Compute A (internal scattering coefficient) - //compute the numerator and denominator for A - a = cjv_ka[l] * chvp_ka[l] - cjvp_ka[l] * chv_ka[l]; - b = cjv_kna[l] * chvp_ka[l] - chv_ka[l] * cjvp_kna[l] * nc; - - //calculate A and add it to the list - rts::complex An = (2.0 * l + 1.0) * pow(i, l) * (a / b); - cpuA[l] = (rts::complex

)An; - - //Compute B (external scattering coefficient) - c = cjv_ka[l] * cjvp_kna[l] * nc - cjv_kna[l] * cjvp_ka[l]; - d = cjv_kna[l] * chvp_ka[l] - chv_ka[l] * cjvp_kna[l] * nc; - - //calculate B and add it to the list - rts::complex Bn = (2.0 * l + 1.0) * pow(i, l) * (c / d); - cpuB[l] = (rts::complex

)Bn; - - std::cout<<"A: "<) * (Nl+1)); //allocate memory for new coefficients - cudaMalloc(&B, sizeof(rts::complex

) * (Nl+1)); - - //copy the calculations from the CPU to the GPU - cudaMemcpy(A, cpuA, sizeof(rts::complex

) * (Nl+1), cudaMemcpyDeviceToHost); - cudaMemcpy(B, cpuB, sizeof(rts::complex

) * (Nl+1), cudaMemcpyDeviceToHost); - } - -public: - - esphere(unsigned int res0, unsigned int res1, P _a, rts::complex

_n, bool _scalar = false) : - efield

(res0, res1, _scalar) - { - std::cout<<"Sphere scattered field created."< & operator= (const planewave

& rhs) - { - calcNl(rhs.kmag()); //compute the number of scattering coefficients - std::cout<<"Nl: "<::str()< -__global__ void gpu_halfspace_pw2ef(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, - plane P, planewave w, rect q, bool at_surface = false) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //get the current position - vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); - - if(at_surface){ - if(P.side(p) > 0) - return; - } - else{ - if(P.side(p) >= 0) - return; - } - - //if the current position is on the wrong side of the plane - - //vec r(p[0], p[1], p[2]); - - complex x( 0.0f, w.kvec().dot(p) ); - //complex phase( 0.0f, w.phase()); - - if(Y == NULL) //if this is a scalar simulation - X[i] += w.E().len() * exp(x); //use the vector magnitude as the plane wave amplitude - else - { - //X[i] = Y[i] = Z[i] = 1; - X[i] += w.E()[0] * exp(x);// * exp(phase); - Y[i] += w.E()[1] * exp(x);// * exp(phase); - Z[i] += w.E()[2] * exp(x);// * exp(phase); - } -} - -//GPU kernel to compute the electric displacement field -template -__global__ void gpu_halfspace_pw2df(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, - plane P, planewave w, rect q, T n, bool at_surface = false) -{ - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //get the current position - vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); - - if(at_surface){ - if(P.side(p) > 0) - return; - } - else{ - if(P.side(p) >= 0) - return; - } - - //if the current position is on the wrong side of the plane - - //vec r(p[0], p[1], p[2]); - - complex x( 0.0f, w.kvec().dot(p) ); - //complex phase( 0.0f, w.phase()); - - //vec< complex > testE(1, 0, 0); - - - - if(Y == NULL) //if this is a scalar simulation - X[i] += w.E().len() * exp(x); //use the vector magnitude as the plane wave amplitude - else - { - plane< complex > cplane = plane< complex, 3>(P); - vec< complex > E_para;// = cplane.parallel(w.E()); - vec< complex > E_perp;// = cplane.perpendicular(w.E()) * (n*n); - cplane.decompose(w.E(), E_para, E_perp); - T epsilon = n*n; - - X[i] += (E_para[0] + E_perp[0] * epsilon) * exp(x); - Y[i] += (E_para[1] + E_perp[1] * epsilon) * exp(x); - Z[i] += (E_para[2] + E_perp[2] * epsilon) * exp(x); - } -} - -//computes a scalar field containing the refractive index of the half-space at each point -template -__global__ void gpu_halfspace_n(T* n, unsigned int r0, unsigned int r1, rect q, plane P, T n0, T n1){ - - int iu = blockIdx.x * blockDim.x + threadIdx.x; - int iv = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(iu >= r0 || iv >= r1) return; - - //compute the index into the field - int i = iv*r0 + iu; - - //get the current position - vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); - - //set the appropriate refractive index - if(P.side(p) < 0) n[i] = n0; - else n[i] = n1; -} - -template -class halfspace -{ -private: - rts::plane S; //surface plane splitting the half space - rts::complex n0; //refractive index at the front of the plane - rts::complex n1; //refractive index at the back of the plane - - //lists of waves in front (pw0) and behind (pw1) the plane - std::vector< rts::planewave > w0; - std::vector< rts::planewave > w1; - - //rts::planewave pi; //incident plane wave - //rts::planewave pr; //plane wave reflected from the surface - //rts::planewave pt; //plane wave transmitted through the surface - - void init(){ - n0 = 1.0; - n1 = 1.5; - } - - //compute which side of the interface is hit by the incoming plane wave (0 = front, 1 = back) - bool facing(planewave p){ - if(p.kvec().dot(S.norm()) > 0) - return 1; - else - return 0; - } - - T calc_theta_i(vec v){ - - vec v_hat = v.norm(); - - //compute the cosine of the angle between k_hat and the plane normal - T cos_theta_i = v_hat.dot(S.norm()); - - return acos(abs(cos_theta_i)); - } - - T calc_theta_t(T ni_nt, T theta_i){ - - T sin_theta_t = ni_nt * sin(theta_i); - return asin(sin_theta_t); - } - - -public: - - //constructors - halfspace(){ - init(); - } - - halfspace(T na, T nb){ - init(); - n0 = na; - n1 = nb; - } - - halfspace(T na, T nb, plane p){ - n0 = na; - n1 = nb; - S = p; - } - - //compute the transmitted and reflective waves given the incident (vacuum) plane wave p - void incident(rts::planewave p){ - - planewave r, t; - p.scatter(S, n1.real()/n0.real(), r, t); - - //std::cout<<"i+r: "< b, unsigned int N = 10000){ - - //generate a plane wave decomposition for the beam - std::vector< planewave > pw_list = b.mc(N); - - //calculate the reflected and refracted waves for each incident wave - for(unsigned int w = 0; w < pw_list.size(); w++){ - incident(pw_list[w]); - } - } - - //return the electric field at the specified resolution and position - rts::efield E(unsigned int r0, unsigned int r1, rect R){ - - int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((r0 + SQRT_BLOCK -1)/SQRT_BLOCK, (r1 + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //create an electric field - rts::efield ef(r0, r1); - ef.position(R); - - //render each plane wave - - //plane waves at the surface front - for(unsigned int w = 0; w < w0.size(); w++){ - //std::cout<<"w0 "< <<>> (ef.x(), ef.y(), ef.z(), r0, r1, S, w0[w], ef.p()); - } - - //plane waves at the surface back - for(unsigned int w = 0; w < w1.size(); w++){ - //std::cout<<"w1 "< <<>> (ef.x(), ef.y(), ef.z(), r0, r1, -S, w1[w], ef.p(), true); - } - - return ef; - } - - //return the electric displacement at the specified resolution and position - rts::efield D(unsigned int r0, unsigned int r1, rect R){ - - int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((r0 + SQRT_BLOCK -1)/SQRT_BLOCK, (r1 + SQRT_BLOCK - 1)/SQRT_BLOCK); - - //create a complex vector field - rts::efield df(r0, r1); - df.position(R); - - //render each plane wave - - //plane waves at the surface front - for(unsigned int w = 0; w < w0.size(); w++){ - //std::cout<<"w0 "< <<>> (df.x(), df.y(), df.z(), r0, r1, S, w0[w], df.p(), n0.real()); - } - - //plane waves at the surface back - for(unsigned int w = 0; w < w1.size(); w++){ - //std::cout<<"w1 "< <<>> (df.x(), df.y(), df.z(), r0, r1, -S, w1[w], df.p(), n1.real(), true); - } - - return df; - } - - realfield nfield(unsigned int Ru, unsigned int Rv, rect p){ - - int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size - int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); - - //create one thread for each detector pixel - dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); - dim3 dimGrid((Ru + SQRT_BLOCK -1)/SQRT_BLOCK, (Rv + SQRT_BLOCK - 1)/SQRT_BLOCK); - - realfield n(Ru, Rv); //create a scalar field to store the result of n - - rts::gpu_halfspace_n <<>> (n.ptr(), Ru, Rv, p, S, n0.real(), n1.real()); - - return n; - } - - std::string str(){ - std::stringstream ss; - ss<<"Half Space-------------"< (rts::efield &ef, rts::halfspace hs); - -}; - - - - -} - - -#endif diff --git a/optics/material.h b/optics/material.h deleted file mode 100644 index fb97cd6..0000000 --- a/optics/material.h +++ /dev/null @@ -1,153 +0,0 @@ -#ifndef RTS_MATERIAL_H -#define RTS_MATERIAL_H - -#include -#include -#include -#include -#include -#include -#include -#include "../math/complex.h" -#include "../math/constants.h" -#include "../math/function.h" - -namespace stim{ - -//Material class - default representation for the material property is the refractive index (RI) -template -class material : public function< T, complex >{ - -public: - enum wave_property{microns, inverse_cm}; - enum material_property{ri, absorbance}; - -private: - - using function< T, complex >::X; - using function< T, complex >::Y; - using function< T, complex >::insert; - using function< T, complex >::bounding; - - std::string name; //name for the material (defaults to file name) - - void process_header(std::string str, wave_property& wp, material_property& mp){ - - std::stringstream ss(str); //create a stream from the data string - std::string line; - std::getline(ss, line); //get the first line as a string - while(line[0] == '#'){ //continue looping while the line is a comment - - std::stringstream lstream(line); //create a stream from the line - lstream.ignore(); //ignore the first character ('#') - - std::string prop; //get the property name - lstream>>prop; - - if(prop == "X"){ - std::string wp_name; - lstream>>wp_name; - if(wp_name == "microns") wp = microns; - else if(wp_name == "inverse_cm") wp = inverse_cm; - } - else if(prop == "Y"){ - std::string mp_name; - lstream>>mp_name; - if(mp_name == "ri") mp = ri; - else if(mp_name == "absorbance") mp = absorbance; - } - - std::getline(ss, line); //get the next line - } - - function< T, stim::complex >::process_string(str); - } - - void from_inverse_cm(){ - //convert inverse centimeters to wavelength (in microns) - for(unsigned int i=0; i(1, 0); - } - - -public: - - material(std::string filename, wave_property wp, material_property mp){ - name = filename; - load(filename, wp, mp); - } - - material(std::string filename){ - name = filename; - load(filename); - } - - material(){ - init(); - } - - complex getN(T lambda){ - return function< T, complex >::linear(lambda); - } - - void load(std::string filename, wave_property wp, material_property mp){ - - //load the file as a function - function< T, complex >::load(filename); - } - - void load(std::string filename){ - - wave_property wp = inverse_cm; - material_property mp = ri; - //turn the file into a string - std::ifstream t(filename.c_str()); //open the file as a stream - - if(!t){ - std::cout<<"ERROR: Couldn't open the material file '"<(t)), - std::istreambuf_iterator()); - - //process the header information - process_header(str, wp, mp); - - //convert units - if(wp == inverse_cm) - from_inverse_cm(); - //set the bounding values - bounding[0] = Y[0]; - bounding[1] = Y.back(); - } - std::string str(){ - std::stringstream ss; - ss< >::str(); - return ss.str(); - } - std::string get_name(){ - return name; - } - - void set_name(std::string str){ - name = str; - } - -}; - -} - - - - -#endif diff --git a/optics/mirst-1d.cuh b/optics/mirst-1d.cuh deleted file mode 100644 index 9279087..0000000 --- a/optics/mirst-1d.cuh +++ /dev/null @@ -1,447 +0,0 @@ -#include "../optics/material.h" -#include "../math/complexfield.cuh" -#include "../math/constants.h" -//#include "../envi/bil.h" - -#include "cufft.h" - -#include -#include - -namespace stim{ - -//this function writes a sinc function to "dest" such that an iFFT produces a slab -template -__global__ void gpu_mirst1d_layer_fft(complex* dest, complex* ri, - T* src, T* zf, - T w, unsigned int zR, unsigned int nuR){ - //dest = complex field representing the sample - //ri = refractive indices for each wavelength - //src = intensity of the light source for each wavelength - //zf = z position of the slab interface for each wavelength (accounting for optical path length) - //w = width of the slab (in pixels) - //zR = number of z-axis samples - //nuR = number of wavelengths - - //get the current coordinate in the plane slice - int ifz = blockIdx.x * blockDim.x + threadIdx.x; - int inu = blockIdx.y * blockDim.y + threadIdx.y; - - //make sure that the thread indices are in-bounds - if(inu >= nuR || ifz >= zR) return; - - int i = inu * zR + ifz; - - T fz; - if(ifz < zR/2) - fz = ifz / (T)zR; - else - fz = -(zR - ifz) / (T)zR; - - //if the slab starts outside of the simulation domain, just return - if(zf[inu] >= zR) return; - - //fill the array along z with a sinc function representing the Fourier transform of the layer - - T opl = w * ri[inu].real(); //optical path length - - //handle the case where the slab goes outside the simulation domain - if(zf[inu] + opl >= zR) - opl = zR - zf[inu]; - - if(opl == 0) return; - - //T l = w * ri[inu].real(); - //complex e(0.0, -2 * PI * fz * (zf[inu] + zR/2 - l/2.0)); - complex e(0, -2 * stimPI * fz * (zf[inu] + opl/2)); - - complex eta = ri[inu] * ri[inu] - 1; - - //dest[i] = fz;//exp(e) * m[inu] * src[inu] * sin(PI * fz * l) / (PI * fz); - if(ifz == 0) - dest[i] += opl * exp(e) * eta * src[inu]; - else - dest[i] += opl * exp(e) * eta * src[inu] * sin(stimPI * fz * opl) / (stimPI * fz * opl); -} - -template -__global__ void gpu_mirst1d_increment_z(T* zf, complex* ri, T w, unsigned int S){ - //zf = current z depth (optical path length) in pixels - //ri = refractive index of the material - //w = actual width of the layer (in pixels) - - - //compute the index for this thread - int i = blockIdx.x * blockDim.x + threadIdx.x; - if(i >= S) return; - - if(ri == NULL) - zf[i] += w; - else - zf[i] += ri[i].real() * w; -} - -//apply the 1D MIRST filter to an existing sample (overwriting the sample) -template -__global__ void gpu_mirst1d_apply_filter(complex* sampleFFT, T* lambda, - T dFz, - T inNA, T outNA, - unsigned int lambdaR, unsigned int zR, - T sigma = 0){ - //sampleFFT = the sample in the Fourier domain (will be overwritten) - //lambda = list of wavelengths - //dFz = delta along the Fz axis in the frequency domain - //inNA = NA of the internal obscuration - //outNA = NA of the objective - //zR = number of pixels along the Fz axis (same as the z-axis) - //lambdaR = number of wavelengths - //sigma = width of the Gaussian source - int ifz = blockIdx.x * blockDim.x + threadIdx.x; - int inu = blockIdx.y * blockDim.y + threadIdx.y; - - if(inu >= lambdaR || ifz >= zR) return; - - //calculate the index into the sample FT - int i = inu * zR + ifz; - - //compute the frequency (and set all negative spatial frequencies to zero) - T fz; - if(ifz < zR / 2) - fz = ifz * dFz; - //if the spatial frequency is negative, set it to zero and exit - else{ - sampleFFT[i] = 0; - return; - } - - //compute the frequency in inverse microns - T nu = 1/lambda[inu]; - - //determine the radius of the integration circle - T nu_sq = nu * nu; - T fz_sq = (fz * fz) / 4; - - //cut off frequencies above the diffraction limit - T r; - if(fz_sq < nu_sq) - r = sqrt(nu_sq - fz_sq); - else - r = 0; - - //account for the optics - T Q = 0; - if(r > nu * inNA && r < nu * outNA) - Q = 1; - - //account for the source - //T sigma = 30.0; - T s = exp( - (r*r * sigma*sigma) / 2 ); - //T s=1; - - //compute the final filter - T mirst = 0; - if(fz != 0) - mirst = 2 * stimPI * r * s * Q * (1/fz); - - sampleFFT[i] *= mirst; - -} - -/*This object performs a 1-dimensional (layered) MIRST simulation -*/ -template -class mirst1d{ - -private: - unsigned int Z; //z-axis resolution - unsigned int pad; //pixel padding on either side of the sample - - std::vector< material > matlist; //list of materials - std::vector< T > layers; //list of layer thicknesses - - std::vector< T > lambdas; //list of wavelengths that are being simulated - unsigned int S; //number of wavelengths (size of "lambdas") - - T NA[2]; //numerical aperature (central obscuration and outer diameter) - - function source_profile; //profile (spectrum) of the source (expressed in inverse centimeters) - - complexfield scratch; //scratch GPU memory used to build samples, transforms, etc. - - void fft(int direction = CUFFT_FORWARD){ - - unsigned padZ = Z + pad; - - //create cuFFT handles - cufftHandle plan; - cufftResult result; - - if(sizeof(T) == 4) - result = cufftPlan1d(&plan, padZ, CUFFT_C2C, lambdas.size()); //single precision - else - result = cufftPlan1d(&plan, padZ, CUFFT_Z2Z, lambdas.size()); //double precision - - //check for Plan 1D errors - if(result != CUFFT_SUCCESS){ - std::cout<<"Error creating CUFFT plan for computing the FFT:"<(Z + pad , lambdas.size()); - scratch = 0; - } - - //get the list of scattering efficiency (eta) values for a specified layer - std::vector< complex > layer_etas(unsigned int l){ - - std::vector< complex > etas; - - //fill the list of etas - for(unsigned int i=0; i* gpuRi; - HANDLE_ERROR(cudaMalloc( (void**)&gpuRi, sizeof(complex) * S)); - - //allocate memory for the source profile - T* gpuSrc; - HANDLE_ERROR(cudaMalloc( (void**)&gpuSrc, sizeof(T) * S)); - - complex ri; - T source; - //store the refractive index and source profile in a CPU array - for(int inu=0; inu), cudaMemcpyHostToDevice )); - - //save the source profile to the GPU - source = source_profile(10000 / lambdas[inu]); - HANDLE_ERROR(cudaMemcpy( gpuSrc + inu, &source, sizeof(T), cudaMemcpyHostToDevice )); - - } - - //create one thread for each pixel of the field slice - dim3 gridDim, blockDim; - cuda_params(gridDim, blockDim); - stim::gpu_mirst1d_layer_fft<<>>(scratch.ptr(), gpuRi, gpuSrc, zf, wpx, paddedZ, S); - - int linBlock = stim::maxThreadsPerBlock(); //compute the optimal block size - int linGrid = S / linBlock + 1; - stim::gpu_mirst1d_increment_z <<>>(zf, gpuRi, wpx, S); - - //free memory - HANDLE_ERROR(cudaFree(gpuRi)); - HANDLE_ERROR(cudaFree(gpuSrc)); - } - - void build_sample(){ - init_scratch(); //initialize the GPU scratch space - //build_layer(1); - - T* zf; - HANDLE_ERROR(cudaMalloc(&zf, sizeof(T) * S)); - HANDLE_ERROR(cudaMemset(zf, 0, sizeof(T) * S)); - - //render each layer of the sample - for(unsigned int l=0; l>>(scratch.ptr(), gpuLambdas, - dFz, - NA[0], NA[1], - S, Zpad); - } - - //crop the image to the sample thickness - keep in mind that sample thickness != optical path length - void crop(){ - - scratch = scratch.crop(Z, S); - } - - //save the scratch field as a binary file - void to_binary(std::string filename){ - - } - - -public: - - //constructor - mirst1d(unsigned int rZ = 100, - unsigned int padding = 0){ - Z = rZ; - pad = padding; - NA[0] = 0; - NA[1] = 0.8; - S = 0; - source_profile = 1; - } - - //add a layer, thickness = microns - void add_layer(material mat, T thickness){ - matlist.push_back(mat); - layers.push_back(thickness); - } - - void add_layer(std::string filename, T thickness){ - add_layer(material(filename), thickness); - } - - //adds a profile spectrum for the light source - void set_source(std::string filename){ - source_profile.load(filename); - } - - //adds a block of wavenumbers (cm^-1) to the simulation parameters - void add_wavenumbers(unsigned int start, unsigned int stop, unsigned int step){ - unsigned int nu = start; - while(nu <= stop){ - lambdas.push_back((T)10000 / nu); - nu += step; - } - S = lambdas.size(); //increment the number of wavelengths (shorthand for later) - } - - T thickness(){ - T t = 0; - for(unsigned int l=0; l get_source(){ - return source_profile; - } - - void save_sample(std::string filename){ - //create a sample and save the magnitude as an image - build_sample(); - fft(CUFFT_INVERSE); - scratch.toImage(filename, stim::complexfield::magnitude); - } - - void save_mirst(std::string filename, bool binary = true){ - //apply the MIRST filter to a sample and save the image - - //build the sample in the Fourier domain - build_sample(); - - //apply the MIRST filter - apply_filter(); - - //apply an inverse FFT to bring the results back into the spatial domain - fft(CUFFT_INVERSE); - - crop(); - - //save the image - if(binary) - to_binary(filename); - else - scratch.toImage(filename, stim::complexfield::magnitude); - } - - - - - std::string str(){ - - stringstream ss; - ss<<"1D MIRST Simulation========================="< -#include - -#include "../math/vector.h" -#include "../math/quaternion.h" -#include "../math/constants.h" -#include "../math/plane.h" -#include "../cuda/callable.h" - -/*Basic conversions used here (assuming a vacuum) - lambda = -*/ - -namespace stim{ - -template -class planewave{ - -protected: - - vec k; //k = tau / lambda - vec< complex > E0; //amplitude - //T phi; - - CUDA_CALLABLE planewave bend(rts::vec kn) const{ - - vec kn_hat = kn.norm(); //normalize the new k - vec k_hat = k.norm(); //normalize the current k - - //std::cout<<"PLANE WAVE BENDING------------------"< new_p; //create a new plane wave - - //if kn is equal to k or -k, handle the degenerate case - T k_dot_kn = k_hat.dot(kn_hat); - - //if k . n < 0, then the bend is a reflection - //flip k_hat - if(k_dot_kn < 0) k_hat = -k_hat; - - //std::cout<<"k dot kn: "< r = k_hat.cross(kn_hat); //compute the rotation vector - - //std::cout<<"r: "< q; - q.CreateRotation(theta, r.norm()); - - //apply the rotation to E0 - vec< complex > E0n = q.toMatrix3() * E0; - - new_p.k = kn_hat * kmag(); - new_p.E0 = E0n; - - return new_p; - } - -public: - - - ///constructor: create a plane wave propagating along z, polarized along x - /*planewave(T lambda = (T)1) - { - k = rts::vec(0, 0, 1) * (TAU/lambda); - E0 = rts::vec(1, 0, 0); - }*/ - ///constructor: create a plane wave propagating along k, polarized along _E0, at frequency _omega - CUDA_CALLABLE planewave(vec kvec = rts::vec(0, 0, rtsTAU), - vec< complex > E = rts::vec(1, 0, 0), T phase = 0) - { - //phi = phase; - - k = kvec; - vec< complex > k_hat = k.norm(); - - if(E.len() == 0) //if the plane wave has an amplitude of 0 - E0 = vec(0); //just return it - else{ - vec< complex > s = (k_hat.cross(E)).norm(); //compute an orthogonal side vector - vec< complex > E_hat = (s.cross(k)).norm(); //compute a normalized E0 direction vector - E0 = E_hat * E_hat.dot(E); //compute the projection of _E0 onto E0_hat - } - - E0 = E0 * exp( complex(0, phase) ); - } - - ///multiplication operator: scale E0 - CUDA_CALLABLE planewave & operator* (const T & rhs) - { - - E0 = E0 * rhs; - return *this; - } - - CUDA_CALLABLE T lambda() const - { - return rtsTAU / k.len(); - } - - CUDA_CALLABLE T kmag() const - { - return k.len(); - } - - CUDA_CALLABLE vec< complex > E(){ - return E0; - } - - CUDA_CALLABLE vec kvec(){ - return k; - } - - /*CUDA_CALLABLE T phase(){ - return phi; - } - - CUDA_CALLABLE void phase(T p){ - phi = p; - }*/ - - CUDA_CALLABLE vec< complex > pos(vec p = vec(0, 0, 0)){ - vec< complex > result; - - T kdp = k.dot(p); - complex x = complex(0, kdp); - complex expx = exp(x); - - result[0] = E0[0] * expx; - result[1] = E0[1] * expx; - result[2] = E0[2] * expx; - - return result; - } - - //scales k based on a transition from material ni to material nt - CUDA_CALLABLE planewave n(T ni, T nt){ - return planewave(k * (nt / ni), E0); - } - - CUDA_CALLABLE planewave refract(rts::vec kn) const - { - return bend(kn); - } - - void scatter(rts::plane P, T nr, planewave &r, planewave &t){ - - int facing = P.face(k); //determine which direction the plane wave is coming in - - //if(facing == 0) //if the wave is tangent to the plane, return an identical wave - // return *this; - //else - if(facing == -1){ //if the wave hits the back of the plane, invert the plane and nr - P = P.flip(); //flip the plane - nr = 1/nr; //invert the refractive index (now nr = n0/n1) - } - - //use Snell's Law to calculate the transmitted angle - T cos_theta_i = k.norm().dot(-P.norm()); //compute the cosine of theta_i - T theta_i = acos(cos_theta_i); //compute theta_i - T sin_theta_t = (1/nr) * sin(theta_i); //compute the sine of theta_t using Snell's law - T theta_t = asin(sin_theta_t); //compute the cosine of theta_t - - bool tir = false; //flag for total internal reflection - if(theta_t != theta_t){ - tir = true; - theta_t = rtsPI / (T)2; - } - - //handle the degenerate case where theta_i is 0 (the plane wave hits head-on) - if(theta_i == 0){ - T rp = (1 - nr) / (1 + nr); //compute the Fresnel coefficients - T tp = 2 / (1 + nr); - vec kr = -k; - vec kt = k * nr; //set the k vectors for theta_i = 0 - vec< complex > Er = E0 * rp; //compute the E vectors - vec< complex > Et = E0 * tp; - T phase_t = P.p().dot(k - kt); //compute the phase offset - T phase_r = P.p().dot(k - kr); - //std::cout<<"Degeneracy: Head-On"<(kr, Er, phase_r); - t = planewave(kt, Et, phase_t); - - //std::cout<<"i + r: "< z_hat = -P.norm(); - vec y_hat = P.parallel(k).norm(); - vec x_hat = y_hat.cross(z_hat).norm(); - - //compute the k vectors for r and t - vec kr, kt; - kr = ( y_hat * sin(theta_i) - z_hat * cos(theta_i) ) * kmag(); - kt = ( y_hat * sin(theta_t) + z_hat * cos(theta_t) ) * kmag() * nr; - - //compute the magnitude of the p- and s-polarized components of the incident E vector - complex Ei_s = E0.dot(x_hat); - //int sgn = (0 < E0.dot(y_hat)) - (E0.dot(y_hat) < 0); - int sgn = E0.dot(y_hat).sgn(); - vec< complex > cx_hat = x_hat; - complex Ei_p = ( E0 - cx_hat * Ei_s ).len() * sgn; - //T Ei_p = ( E0 - x_hat * Ei_s ).len(); - //compute the magnitude of the p- and s-polarized components of the reflected E vector - complex Er_s = Ei_s * rs; - complex Er_p = Ei_p * rp; - //compute the magnitude of the p- and s-polarized components of the transmitted E vector - complex Et_s = Ei_s * ts; - complex Et_p = Ei_p * tp; - - //std::cout<<"E0: "< > Er = vec< complex >(y_hat * cos(theta_i) + z_hat * sin(theta_i)) * Er_p + cx_hat * Er_s; - //compute the transmitted E vector - vec< complex > Et = vec< complex >(y_hat * cos(theta_t) - z_hat * sin(theta_t)) * Et_p + cx_hat * Et_s; - - T phase_t = P.p().dot(k - kt); - T phase_r = P.p().dot(k - kr); - - //std::cout<<"phase r: "<(0, phase_r) ); - //r.phi = phase_r; - - //t = bend(kt); - //t.k = t.k * nr; - - t.k = kt; - t.E0 = Et * exp( complex(0, phase_t) ); - //t.phi = phase_t; - //std::cout<<"i: "< -std::ostream& operator<<(std::ostream& os, rts::planewave p) -{ - os< -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#include -#endif - -namespace stim{ - - class cmd_option - { - private: - bool ansi; - - //argument name - std::string name; - - //description of the argument - std::vector desc; - - //argument values - std::vector vals; - - //range or example - std::string range; - - //flag is true when the argument is user-specified - bool flag; - - void parse_val(const std::string &s){ - - vals.clear(); - - std::stringstream ss(s); - std::string item; - while (std::getline(ss, item, ' ')) { - vals.push_back(item); - } - } - - void parse_desc(const std::string &s){ - - desc.clear(); - - std::stringstream ss(s); - std::string item; - while (std::getline(ss, item, '\n')) { - desc.push_back(item); - } - } - - public: - void set_ansi(bool b){ ansi = b; } - //create an option with a given name, description, and default value - cmd_option(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") - { - name = _name; - parse_desc(_desc); - parse_val(_default); - - //if a default value is provided, set the flag - if(_default != "") - flag = true; - else flag = false; - - range = _range; - - - } - - int nargs() - { - return vals.size(); - } - - //return the value of a text option - std::string as_string(unsigned int n = 0) - { - if(!flag) - { - std::cout<<"ERROR - Option requested without being set: "< n) - return vals[n]; - - else return ""; - } - - //return the value of a floating point option - float as_float(unsigned int n = 0) - { - if(!flag) - { - std::cout<<"ERROR - option requested without being set: "< n) - { - float r; - if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; - return r; - } - - else return 0; - } - - //return the value of an integer option - int as_int(unsigned int n = 0) - { - if(!flag) - { - std::cout<<"ERROR - option requested without being set: "< n) - { - int r; - if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; - return r; - } - - else return 0; - } - - //get the width of the left column - int col_width() - { - int n = 3; - //add the length of the option name - n += name.size(); - - //if there are any default parameters - if(vals.size() > 0) - { - //padding (parenthesis, =, etc.) - n += 6; - - //for each default option value - for(unsigned int v=0; v opts; - std::vector args; - - //column width of the longest option - int col_width; - - //list of sections - std::vector sections; - - public: - - arglist(){ - col_width = 0; - ansi = true; - } - - void set_ansi(bool b) - { - ansi = b; - for(unsigned int i=0; i(col_width, opt.col_width()); - } - - void section(std::string _name) - { - argsection s; - s.name = _name; - s.index = opts.size(); - sections.push_back(s); - } - - //output the options (generally in response to --help) - std::string str() - { - std::stringstream ss; - - int si = -1; - - if(sections.size() > 0) - si = 0; - - //for each option - for(unsigned int a=0; a= opts.size()) - return -1; - - return (int)i; - } - - void set(std::string _name, std::string _value) - { - int i = index(_name); - - if(i != -1) - { - opts[i].set(_value); - //adjust the column width if necessary - col_width = (std::max)(col_width, opts[i].col_width()); - } - else - std::cout<<"ERROR - option not recognized: "<<_name<= opts.size()) - { - std::cout<<"ERROR - Unspecified parameter name: "<<_name< /* defines FILENAME_MAX */ -#ifdef _WIN32 - #include - #define GetCurrentDir _getcwd - #define STIM_FILENAME_DIV '\\' -#else - #include - #define GetCurrentDir getcwd - #define STIM_FILENAME_DIV '/' - #endif - -#include -#include -#include -#include -#include - -#include "../parser/parser.h" - -#include - -namespace stim{ - -//filename class designed to work with both Windows and Unix - -class filename{ - -private: - void init(){ - - absolute = false; - - } - -protected: - - std::string drive; //drive letter (for Windows) - std::vector path; //directory hierarchy - std::string prefix; //file prefix (without extension) - std::string ext; //file extension - - bool absolute; //filename is an absolute path - - //replace any incorrect dividers with the appropriate version for the OS - std::string parse_div(std::string s) { - #ifdef _WIN32 - std::replace( s.begin(), s.end(), '/', '\\'); - #else - std::replace( s.begin(), s.end(), '\\', '/'); - #endif - - return s; - } - - //parse the file name (prefix and extension) - void parse_name(std::string fname){ - - //find the extension - size_t xpos = fname.find_last_of('.'); //find the position of the extension period (if there is one) - if(xpos != std::string::npos){ //split the prefix and extension - prefix = fname.substr(0, xpos); - ext = fname.substr(xpos + 1); - } - else - prefix = fname; - } - - //parse a file locator string - void parse(std::string loc){ - - //determine the drive (if Windows) - drive = ""; - #ifdef _WIN32 - //if the second character is a colon, the character right before it is the drive - if(loc[1] == ':'){ - absolute = true; //this is an absolute path - drive = loc[0]; //copy the drive letter - loc = loc.substr(3); //remove the drive information from the locator string - } - #else - if(loc[0] == STIM_FILENAME_DIV){ - absolute = true; - loc = loc.substr(1); - } - #endif - - //determine the file name - std::string fname = loc.substr( loc.find_last_of(STIM_FILENAME_DIV) + 1 ); //find the file name (including extension) - - //parse the file name - parse_name(fname); - - //find the directory hierarchy - std::string dir = loc.substr(0, loc.find_last_of(STIM_FILENAME_DIV) + 1 ); - path = stim::parser::split(dir, STIM_FILENAME_DIV); - } - -public: - - filename(){ - init(); - } - - filename(std::string loc){ - init(); - parse(loc); - } - - - - std::string get_name(){ - if(prefix == "" && ext == "") - return ""; - else - return prefix + "." + ext; - } - - std::string get_extension(){ - return ext; - } - - std::string get_prefix(){ - return prefix; - } - - std::string dir(){ - std::stringstream ss; - - //if the path is absolute - if(absolute){ - //output the drive letter if in Windows - #ifdef _WIN32 - ss< get_list(){ - - boost::filesystem::path p(dir()); //create a path from the current filename - - std::vector file_list; - - if(boost::filesystem::exists(p)){ - if(boost::filesystem::is_directory(p)){ - - typedef std::vector vec; // store paths, - vec v; // so we can sort them later - - std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(v)); - - std::sort(v.begin(), v.end()); // sort, since directory iteration - // is not ordered on some file systems - - //compare file names to the current template (look for wild cards) - for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) - { - - //if the filename is a wild card *or* it matches the read file name - if( prefix == "*" || prefix == (*it).filename().stem().string()){ - - //if the extension is a wild card *or* it matches the read file extension - if( ext == "*" || "." + ext == (*it).filename().extension().string()){ - - file_list.push_back((*it).string()); //include it in the final list - } - } - - } - } - } - - return file_list; - } - - //gets the current working directory - static stim::filename cwd(){ - - - char cCurrentPath[FILENAME_MAX]; - - if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))){ - std::cout<<"ERROR getting current working directory."< rel_path = stim::parser::split(rel, STIM_FILENAME_DIV); - - //if the relative path doesn't contain a file, add a blank string to be used as the filename - if(rel[rel.size()-1] == STIM_FILENAME_DIV) - rel_path.push_back(""); - - //create a stack representing the current absolute path - std::stack > s(path); - - //for each token in the relative path - for(int d=0; d result_path(begin, end); - - //create a new path to be returned - stim::filename result = *this; - result.path = result_path; - result.set_name(rel_path.back()); - - return result; - } - - bool is_relative(){ - return !absolute; - } - - bool is_absolute(){ - return absolute; - } - - - - - - - - - -}; - - -} //end namespace stim - -#endif diff --git a/parser/parser.h b/parser/parser.h deleted file mode 100644 index 2583fde..0000000 --- a/parser/parser.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef STIM_PARSER_H -#define STIM_PARSER_H - -namespace stim{ - -class parser{ - -public: - - static std::vector &split(const std::string &s, char delim, std::vector &elems) { - std::stringstream ss(s); - std::string item; - while (std::getline(ss, item, delim)) { - elems.push_back(item); - } - return elems; - } - - static std::vector split(const std::string &s, char delim) { - std::vector elems; - split(s, delim, elems); - return elems; - } - -}; - -} //end namespace stim - -#endif \ No newline at end of file diff --git a/parser/wildcards.h b/parser/wildcards.h deleted file mode 100644 index ac8c42d..0000000 --- a/parser/wildcards.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef STIM_WILDCARDS_H -#define STIM_WILDCARDS_H - -#include -#include - - -//#include - -namespace stim{ - -class wildcards{ -public: - //generate a sequence of strings where '?' is replaced by integer increments - static std::vector increment(std::string input, - unsigned int start, - unsigned int end, - unsigned int step){ - - size_t a = input.find_first_of('?'); //find the first ? - size_t b = input.find_last_of('?'); //find the last ? - size_t n = b - a + 1; //calculate the number of ? - - std::string str_a = input.substr(0, a); //split the strings along the wildcard - std::string str_b = input.substr(b + 1, std::string::npos); - - //create a vector to hold the output strings - std::vector out; - - //create a stringstream to handle the padding - for (unsigned int i = start; i <= end; i += step){ - - std::stringstream ss; - ss << str_a - << std::setfill('0') - << std::setw(n) - << i - << str_b; - - out.push_back(ss.str()); - } - - return out; - - - } -/* - //returns a list of files that match the specified wildcards in 'd' - static std::vector get_file_list(std::string s){ - - stim::filename f(s); - std::string target_path(f.dir()); - boost::regex filter(f.name()); - - std::vector< std::string > all_matching_files; - - boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end - for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i ) - { - // Skip if not a file - if( !boost::filesystem::is_regular_file( i->status() ) ) continue; - - boost::smatch what; - - // Skip if no match - if( !boost::regex_match( i->path().filename().string(), what, filter ) ) continue; - - // File matches, store it - all_matching_files.push_back( i->path().filename().string() ); - } - - return all_matching_files; - - } -*/ - -}; - - -} //end namespace stim - -#endif diff --git a/stim/Doxyfile b/stim/Doxyfile new file mode 100644 index 0000000..faa49d9 --- /dev/null +++ b/stim/Doxyfile @@ -0,0 +1,2382 @@ +# Doxyfile 1.8.9.1 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project. +# +# All text after a double hash (##) is considered a comment and is placed in +# front of the TAG it is preceding. +# +# All text after a single hash (#) is considered a comment and will be ignored. +# The format is: +# TAG = value [value, ...] +# For lists, items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (\" \"). + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all text +# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv +# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv +# for the list of possible encodings. +# The default value is: UTF-8. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by +# double-quotes, unless you are using Doxywizard) that should identify the +# project for which the documentation is generated. This name is used in the +# title of most generated pages and in a few other places. +# The default value is: My Project. + +PROJECT_NAME = "stimlab" + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. This +# could be handy for archiving the generated documentation or if some version +# control system is used. + +PROJECT_NUMBER = + +# Using the PROJECT_BRIEF tag one can provide an optional one line description +# for a project that appears at the top of each page and should give viewer a +# quick idea about the purpose of the project. Keep the description short. + +PROJECT_BRIEF = + +# With the PROJECT_LOGO tag one can specify a logo or an icon that is included +# in the documentation. The maximum height of the logo should not exceed 55 +# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy +# the logo to the output directory. + +PROJECT_LOGO = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path +# into which the generated documentation will be written. If a relative path is +# entered, it will be relative to the location where doxygen was started. If +# left blank the current directory will be used. + +OUTPUT_DIRECTORY = + +# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub- +# directories (in 2 levels) under the output directory of each output format and +# will distribute the generated files over these directories. Enabling this +# option can be useful when feeding doxygen a huge amount of source files, where +# putting all generated files in the same directory would otherwise causes +# performance problems for the file system. +# The default value is: NO. + +CREATE_SUBDIRS = NO + +# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII +# characters to appear in the names of generated files. If set to NO, non-ASCII +# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode +# U+3044. +# The default value is: NO. + +ALLOW_UNICODE_NAMES = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese, +# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States), +# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian, +# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages), +# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian, +# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian, +# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish, +# Ukrainian and Vietnamese. +# The default value is: English. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member +# descriptions after the members that are listed in the file and class +# documentation (similar to Javadoc). Set to NO to disable this. +# The default value is: YES. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES, doxygen will prepend the brief +# description of a member or function before the detailed description +# +# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. +# The default value is: YES. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator that is +# used to form the text in various listings. Each string in this list, if found +# as the leading text of the brief description, will be stripped from the text +# and the result, after processing the whole list, is used as the annotated +# text. Otherwise, the brief description is used as-is. If left blank, the +# following values are used ($name is automatically replaced with the name of +# the entity):The $name class, The $name widget, The $name file, is, provides, +# specifies, contains, represents, a, an and the. + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# doxygen will generate a detailed section even if there is only a brief +# description. +# The default value is: NO. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. +# The default value is: NO. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES, doxygen will prepend the full path +# before files name in the file list and in the header files. If set to NO the +# shortest path that makes the file name unique will be used +# The default value is: YES. + +FULL_PATH_NAMES = YES + +# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path. +# Stripping is only done if one of the specified strings matches the left-hand +# part of the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the path to +# strip. +# +# Note that you can specify absolute paths here, but also relative paths, which +# will be relative from the directory where doxygen is started. +# This tag requires that the tag FULL_PATH_NAMES is set to YES. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the +# path mentioned in the documentation of a class, which tells the reader which +# header file to include in order to use a class. If left blank only the name of +# the header file containing the class definition is used. Otherwise one should +# specify the list of include paths that are normally passed to the compiler +# using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but +# less readable) file names. This can be useful is your file systems doesn't +# support long names like on DOS, Mac, or CD-ROM. +# The default value is: NO. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the +# first line (until the first dot) of a Javadoc-style comment as the brief +# description. If set to NO, the Javadoc-style will behave just like regular Qt- +# style comments (thus requiring an explicit @brief command for a brief +# description.) +# The default value is: NO. + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first +# line (until the first dot) of a Qt-style comment as the brief description. If +# set to NO, the Qt-style will behave just like regular Qt-style comments (thus +# requiring an explicit \brief command for a brief description.) +# The default value is: NO. + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a +# multi-line C++ special comment block (i.e. a block of //! or /// comments) as +# a brief description. This used to be the default behavior. The new default is +# to treat a multi-line C++ comment block as a detailed description. Set this +# tag to YES if you prefer the old behavior instead. +# +# Note that setting this tag to YES also means that rational rose comments are +# not recognized any more. +# The default value is: NO. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the +# documentation from any documented member that it re-implements. +# The default value is: YES. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES then doxygen will produce a new +# page for each member. If set to NO, the documentation of a member will be part +# of the file/class/namespace that contains it. +# The default value is: NO. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen +# uses this value to replace tabs by spaces in code fragments. +# Minimum value: 1, maximum value: 16, default value: 4. + +TAB_SIZE = 4 + +# This tag can be used to specify a number of aliases that act as commands in +# the documentation. An alias has the form: +# name=value +# For example adding +# "sideeffect=@par Side Effects:\n" +# will allow you to put the command \sideeffect (or @sideeffect) in the +# documentation, which will result in a user-defined paragraph with heading +# "Side Effects:". You can put \n's in the value part of an alias to insert +# newlines. + +ALIASES = + +# This tag can be used to specify a number of word-keyword mappings (TCL only). +# A mapping has the form "name=value". For example adding "class=itcl::class" +# will allow you to use the command class in the itcl::class meaning. + +TCL_SUBST = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources +# only. Doxygen will then generate output that is more tailored for C. For +# instance, some of the names that are used will be different. The list of all +# members will be omitted, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or +# Python sources only. Doxygen will then generate output that is more tailored +# for that language. For instance, namespaces will be presented as packages, +# qualified scopes will look different, etc. +# The default value is: NO. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources. Doxygen will then generate output that is tailored for Fortran. +# The default value is: NO. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for VHDL. +# The default value is: NO. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it +# parses. With this tag you can assign which parser to use for a given +# extension. Doxygen has a built-in mapping, but you can override or extend it +# using this tag. The format is ext=language, where ext is a file extension, and +# language is one of the parsers supported by doxygen: IDL, Java, Javascript, +# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran: +# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran: +# Fortran. In the later case the parser tries to guess whether the code is fixed +# or free formatted code, this is the default for Fortran type files), VHDL. For +# instance to make doxygen treat .inc files as Fortran files (default is PHP), +# and .f files as C (default is Fortran), use: inc=Fortran f=C. +# +# Note: For files without extension you can use no_extension as a placeholder. +# +# Note that for custom extensions you also need to set FILE_PATTERNS otherwise +# the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments +# according to the Markdown format, which allows for more readable +# documentation. See http://daringfireball.net/projects/markdown/ for details. +# The output of markdown processing is further processed by doxygen, so you can +# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in +# case of backward compatibilities issues. +# The default value is: YES. + +MARKDOWN_SUPPORT = YES + +# When enabled doxygen tries to link words that correspond to documented +# classes, or namespaces to their corresponding documentation. Such a link can +# be prevented in individual cases by putting a % sign in front of the word or +# globally by setting AUTOLINK_SUPPORT to NO. +# The default value is: YES. + +AUTOLINK_SUPPORT = YES + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should set this +# tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); +# versus func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. +# The default value is: NO. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. +# The default value is: NO. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip (see: +# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen +# will parse them like normal C++ but will assume all classes use public instead +# of private inheritance when no explicit protection keyword is present. +# The default value is: NO. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate +# getter and setter methods for a property. Setting this option to YES will make +# doxygen to replace the get and set methods by a property in the documentation. +# This will only work if the methods are indeed getting or setting a simple +# type. If this is not the case, or you want to show the methods anyway, you +# should set this option to NO. +# The default value is: YES. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. +# The default value is: NO. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES to allow class member groups of the same type +# (for instance a group of public functions) to be put as a subgroup of that +# type (e.g. under the Public Functions section). Set it to NO to prevent +# subgrouping. Alternatively, this can be done per class using the +# \nosubgrouping command. +# The default value is: YES. + +SUBGROUPING = YES + +# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions +# are shown inside the group in which they are included (e.g. using \ingroup) +# instead of on a separate page (for HTML and Man pages) or section (for LaTeX +# and RTF). +# +# Note that this feature does not work in combination with +# SEPARATE_MEMBER_PAGES. +# The default value is: NO. + +INLINE_GROUPED_CLASSES = NO + +# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions +# with only public data fields or simple typedef fields will be shown inline in +# the documentation of the scope in which they are defined (i.e. file, +# namespace, or group documentation), provided this scope is documented. If set +# to NO, structs, classes, and unions are shown on a separate page (for HTML and +# Man pages) or section (for LaTeX and RTF). +# The default value is: NO. + +INLINE_SIMPLE_STRUCTS = NO + +# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or +# enum is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically be +# useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. +# The default value is: NO. + +TYPEDEF_HIDES_STRUCT = NO + +# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This +# cache is used to resolve symbols given their name and scope. Since this can be +# an expensive process and often the same symbol appears multiple times in the +# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small +# doxygen will become slower. If the cache is too large, memory is wasted. The +# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range +# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536 +# symbols. At the end of a run doxygen will report the cache usage and suggest +# the optimal cache size from a speed point of view. +# Minimum value: 0, maximum value: 9, default value: 0. + +LOOKUP_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in +# documentation are documented, even if no documentation was available. Private +# class members and static file members will be hidden unless the +# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES. +# Note: This will also disable the warnings about undocumented members that are +# normally produced when WARNINGS is set to YES. +# The default value is: NO. + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will +# be included in the documentation. +# The default value is: NO. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_PACKAGE tag is set to YES, all members with package or internal +# scope will be included in the documentation. +# The default value is: NO. + +EXTRACT_PACKAGE = NO + +# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be +# included in the documentation. +# The default value is: NO. + +EXTRACT_STATIC = NO + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES, classes (and structs) defined +# locally in source files will be included in the documentation. If set to NO, +# only classes defined in header files are included. Does not have any effect +# for Java sources. +# The default value is: YES. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. If set to YES, local methods, +# which are defined in the implementation section but not in the interface are +# included in the documentation. If set to NO, only methods in the interface are +# included. +# The default value is: NO. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base name of +# the file that contains the anonymous namespace. By default anonymous namespace +# are hidden. +# The default value is: NO. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all +# undocumented members inside documented classes or files. If set to NO these +# members will be included in the various overviews, but no documentation +# section is generated. This option has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. If set +# to NO, these classes will be included in the various overviews. This option +# has no effect if EXTRACT_ALL is enabled. +# The default value is: NO. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend +# (class|struct|union) declarations. If set to NO, these declarations will be +# included in the documentation. +# The default value is: NO. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any +# documentation blocks found inside the body of a function. If set to NO, these +# blocks will be appended to the function's detailed documentation block. +# The default value is: NO. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation that is typed after a +# \internal command is included. If the tag is set to NO then the documentation +# will be excluded. Set it to YES to include the internal documentation. +# The default value is: NO. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file +# names in lower-case letters. If set to YES, upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. +# The default value is: system dependent. + +CASE_SENSE_NAMES = NO + +# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with +# their full class and namespace scopes in the documentation. If set to YES, the +# scope will be hidden. +# The default value is: NO. + +HIDE_SCOPE_NAMES = NO + +# If the HIDE_COMPOUND_REFERENCE tag is set to NO (default) then doxygen will +# append additional text to a page's title, such as Class Reference. If set to +# YES the compound reference will be hidden. +# The default value is: NO. + +HIDE_COMPOUND_REFERENCE= NO + +# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of +# the files that are included by a file in the documentation of that file. +# The default value is: YES. + +SHOW_INCLUDE_FILES = YES + +# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each +# grouped member an include statement to the documentation, telling the reader +# which file to include in order to use the member. +# The default value is: NO. + +SHOW_GROUPED_MEMB_INC = NO + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include +# files with double quotes in the documentation rather than with sharp brackets. +# The default value is: NO. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the +# documentation for inline members. +# The default value is: YES. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the +# (detailed) documentation of file and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. +# The default value is: YES. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief +# descriptions of file, namespace and class members alphabetically by member +# name. If set to NO, the members will appear in declaration order. Note that +# this will also influence the order of the classes in the class list. +# The default value is: NO. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the +# (brief and detailed) documentation of class members so that constructors and +# destructors are listed first. If set to NO the constructors will appear in the +# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS. +# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief +# member documentation. +# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting +# detailed member documentation. +# The default value is: NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy +# of group names into alphabetical order. If set to NO the group names will +# appear in their defined order. +# The default value is: NO. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by +# fully-qualified names, including namespaces. If set to NO, the class list will +# be sorted only by class name, not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the alphabetical +# list. +# The default value is: NO. + +SORT_BY_SCOPE_NAME = NO + +# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper +# type resolution of all parameters of a function it will reject a match between +# the prototype and the implementation of a member function even if there is +# only one candidate or it is obvious which candidate to choose by doing a +# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still +# accept a match between prototype and implementation in such cases. +# The default value is: NO. + +STRICT_PROTO_MATCHING = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or disable (NO) the todo +# list. This list is created by putting \todo commands in the documentation. +# The default value is: YES. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or disable (NO) the test +# list. This list is created by putting \test commands in the documentation. +# The default value is: YES. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or disable (NO) the bug +# list. This list is created by putting \bug commands in the documentation. +# The default value is: YES. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO) +# the deprecated list. This list is created by putting \deprecated commands in +# the documentation. +# The default value is: YES. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional documentation +# sections, marked by \if ... \endif and \cond +# ... \endcond blocks. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the +# initial value of a variable or macro / define can have for it to appear in the +# documentation. If the initializer consists of more lines than specified here +# it will be hidden. Use a value of 0 to hide initializers completely. The +# appearance of the value of individual variables and macros / defines can be +# controlled using \showinitializer or \hideinitializer command in the +# documentation regardless of this setting. +# Minimum value: 0, maximum value: 10000, default value: 30. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at +# the bottom of the documentation of classes and structs. If set to YES, the +# list will mention the files that were used to generate the documentation. +# The default value is: YES. + +SHOW_USED_FILES = YES + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This +# will remove the Files entry from the Quick Index and from the Folder Tree View +# (if specified). +# The default value is: YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces +# page. This will remove the Namespaces entry from the Quick Index and from the +# Folder Tree View (if specified). +# The default value is: YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command command input-file, where command is the value of the +# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided +# by doxygen. Whatever the program writes to standard output is used as the file +# version. For an example see the documentation. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed +# by doxygen. The layout file controls the global structure of the generated +# output files in an output format independent way. To create the layout file +# that represents doxygen's defaults, run doxygen with the -l option. You can +# optionally specify a file name after the option, if omitted DoxygenLayout.xml +# will be used as the name of the layout file. +# +# Note that if you run doxygen from a directory containing a file called +# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE +# tag is left empty. + +LAYOUT_FILE = + +# The CITE_BIB_FILES tag can be used to specify one or more bib files containing +# the reference definitions. This must be a list of .bib files. The .bib +# extension is automatically appended if omitted. This requires the bibtex tool +# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info. +# For LaTeX the style of the bibliography can be controlled using +# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the +# search path. See also \cite for info how to create references. + +CITE_BIB_FILES = + +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated to +# standard output by doxygen. If QUIET is set to YES this implies that the +# messages are off. +# The default value is: NO. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES +# this implies that the warnings are on. +# +# Tip: Turn warnings on while writing the documentation. +# The default value is: YES. + +WARNINGS = YES + +# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate +# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag +# will automatically be disabled. +# The default value is: YES. + +WARN_IF_UNDOCUMENTED = YES + +# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some parameters +# in a documented function, or documenting parameters that don't exist or using +# markup commands wrongly. +# The default value is: YES. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that +# are documented, but have no documentation for their parameters or return +# value. If set to NO, doxygen will only warn about wrong or incomplete +# parameter documentation, but not about the absence of documentation. +# The default value is: NO. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that doxygen +# can produce. The string should contain the $file, $line, and $text tags, which +# will be replaced by the file and line number from which the warning originated +# and the warning text. Optionally the format may contain $version, which will +# be replaced by the version of the file (if it could be obtained via +# FILE_VERSION_FILTER) +# The default value is: $file:$line: $text. + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning and error +# messages should be written. If left blank the output is written to standard +# error (stderr). + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag is used to specify the files and/or directories that contain +# documented source files. You may enter file names like myfile.cpp or +# directories like /usr/src/myproject. Separate the files or directories with +# spaces. +# Note: If this tag is empty the current directory is searched. + +INPUT = + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses +# libiconv (or the iconv built into libc) for the transcoding. See the libiconv +# documentation (see: http://www.gnu.org/software/libiconv) for the list of +# possible encodings. +# The default value is: UTF-8. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and +# *.h) to filter out the source-files in the directories. If left blank the +# following patterns are tested:*.c, *.cc, *.cxx, *.cpp, *.c++, *.java, *.ii, +# *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h, *.hh, *.hxx, *.hpp, +# *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc, *.m, *.markdown, +# *.md, *.mm, *.dox, *.py, *.f90, *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf, +# *.qsf, *.as and *.js. + +FILE_PATTERNS = + +# The RECURSIVE tag can be used to specify whether or not subdirectories should +# be searched for input files as well. +# The default value is: NO. + +RECURSIVE = YES + +# The EXCLUDE tag can be used to specify files and/or directories that should be +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. +# +# Note that relative paths are relative to the directory from which doxygen is +# run. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or +# directories that are symbolic links (a Unix file system feature) are excluded +# from the input. +# The default value is: NO. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories for example use the pattern */test/* + +EXCLUDE_PATTERNS = CImg.h + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test +# +# Note that the wildcards are matched against the file with absolute path, so to +# exclude all test directories use the pattern */test/* + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or directories +# that contain example code fragments that are included (see the \include +# command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and +# *.h) to filter out the source-files in the directories. If left blank all +# files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude commands +# irrespective of the value of the RECURSIVE tag. +# The default value is: NO. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or directories +# that contain images that are to be included in the documentation (see the +# \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command: +# +# +# +# where is the value of the INPUT_FILTER tag, and is the +# name of an input file. Doxygen will then use the output that the filter +# program writes to standard output. If FILTER_PATTERNS is specified, this tag +# will be ignored. +# +# Note that the filter must not add or remove lines; it is applied before the +# code is scanned, but not when the output code is generated. If lines are added +# or removed, the anchors will not be placed correctly. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: pattern=filter +# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how +# filters are used. If the FILTER_PATTERNS tag is empty or if none of the +# patterns match the file name, INPUT_FILTER is applied. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will also be used to filter the input files that are used for +# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES). +# The default value is: NO. + +FILTER_SOURCE_FILES = NO + +# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file +# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and +# it is also possible to disable source filtering for a specific pattern using +# *.ext= (so without naming a filter). +# This tag requires that the tag FILTER_SOURCE_FILES is set to YES. + +FILTER_SOURCE_PATTERNS = + +# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that +# is part of the input, its contents will be placed on the main page +# (index.html). This can be useful if you have a project on for instance GitHub +# and want to reuse the introduction page also for the doxygen output. + +USE_MDFILE_AS_MAINPAGE = + +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will be +# generated. Documented entities will be cross-referenced with these sources. +# +# Note: To get rid of all source code in the generated output, make sure that +# also VERBATIM_HEADERS is set to NO. +# The default value is: NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body of functions, +# classes and enums directly into the documentation. +# The default value is: NO. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any +# special comment blocks from generated source code fragments. Normal C, C++ and +# Fortran comments will always remain visible. +# The default value is: YES. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES then for each documented +# function all documented functions referencing it will be listed. +# The default value is: NO. + +REFERENCED_BY_RELATION = NO + +# If the REFERENCES_RELATION tag is set to YES then for each documented function +# all documented entities called/used by that function will be listed. +# The default value is: NO. + +REFERENCES_RELATION = NO + +# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set +# to YES then the hyperlinks from functions in REFERENCES_RELATION and +# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will +# link to the documentation. +# The default value is: YES. + +REFERENCES_LINK_SOURCE = YES + +# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the +# source code will show a tooltip with additional information such as prototype, +# brief description and links to the definition and documentation. Since this +# will make the HTML file larger and loading of large files a bit slower, you +# can opt to disable this feature. +# The default value is: YES. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +SOURCE_TOOLTIPS = YES + +# If the USE_HTAGS tag is set to YES then the references to source code will +# point to the HTML generated by the htags(1) tool instead of doxygen built-in +# source browser. The htags tool is part of GNU's global source tagging system +# (see http://www.gnu.org/software/global/global.html). You will need version +# 4.8.6 or higher. +# +# To use it do the following: +# - Install the latest version of global +# - Enable SOURCE_BROWSER and USE_HTAGS in the config file +# - Make sure the INPUT points to the root of the source tree +# - Run doxygen as normal +# +# Doxygen will invoke htags (and that will in turn invoke gtags), so these +# tools must be available from the command line (i.e. in the search path). +# +# The result: instead of the source browser generated by doxygen, the links to +# source code will now point to the output of htags. +# The default value is: NO. +# This tag requires that the tag SOURCE_BROWSER is set to YES. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a +# verbatim copy of the header file for each class for which an include is +# specified. Set to NO to disable this. +# See also: Section \class. +# The default value is: YES. + +VERBATIM_HEADERS = YES + +# If the CLANG_ASSISTED_PARSING tag is set to YES then doxygen will use the +# clang parser (see: http://clang.llvm.org/) for more accurate parsing at the +# cost of reduced performance. This can be particularly helpful with template +# rich C++ code for which doxygen's built-in parser lacks the necessary type +# information. +# Note: The availability of this option depends on whether or not doxygen was +# compiled with the --with-libclang option. +# The default value is: NO. + +CLANG_ASSISTED_PARSING = NO + +# If clang assisted parsing is enabled you can provide the compiler with command +# line options that you would normally use when invoking the compiler. Note that +# the include paths will already be set by doxygen for the files and directories +# specified with INPUT and INCLUDE_PATH. +# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES. + +CLANG_OPTIONS = + +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all +# compounds will be generated. Enable this if the project contains a lot of +# classes, structs, unions or interfaces. +# The default value is: YES. + +ALPHABETICAL_INDEX = YES + +# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in +# which the alphabetical index list will be split. +# Minimum value: 1, maximum value: 20, default value: 5. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all classes will +# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag +# can be used to specify a prefix (or a list of prefixes) that should be ignored +# while generating the index headers. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output +# The default value is: YES. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a +# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of +# it. +# The default directory is: html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each +# generated HTML page (for example: .htm, .php, .asp). +# The default value is: .html. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a user-defined HTML header file for +# each generated HTML page. If the tag is left blank doxygen will generate a +# standard header. +# +# To get valid HTML the header file that includes any scripts and style sheets +# that doxygen needs, which is dependent on the configuration options used (e.g. +# the setting GENERATE_TREEVIEW). It is highly recommended to start with a +# default header using +# doxygen -w html new_header.html new_footer.html new_stylesheet.css +# YourConfigFile +# and then modify the file new_header.html. See also section "Doxygen usage" +# for information on how to generate the default header that doxygen normally +# uses. +# Note: The header is subject to change so you typically have to regenerate the +# default header when upgrading to a newer version of doxygen. For a description +# of the possible markers and block names see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each +# generated HTML page. If the tag is left blank doxygen will generate a standard +# footer. See HTML_HEADER for more information on how to generate a default +# footer and what special commands can be used inside the footer. See also +# section "Doxygen usage" for information on how to generate the default footer +# that doxygen normally uses. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style +# sheet that is used by each HTML page. It can be used to fine-tune the look of +# the HTML output. If left blank doxygen will generate a default style sheet. +# See also section "Doxygen usage" for information on how to generate the style +# sheet that doxygen normally uses. +# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as +# it is more robust and this tag (HTML_STYLESHEET) will in the future become +# obsolete. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_STYLESHEET = + +# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined +# cascading style sheets that are included after the standard style sheets +# created by doxygen. Using this option one can overrule certain style aspects. +# This is preferred over using HTML_STYLESHEET since it does not replace the +# standard style sheet and is therefore more robust against future updates. +# Doxygen will copy the style sheet files to the output directory. +# Note: The order of the extra style sheet files is of importance (e.g. the last +# style sheet in the list overrules the setting of the previous ones in the +# list). For an example see the documentation. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_STYLESHEET = + +# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or +# other source files which should be copied to the HTML output directory. Note +# that these files will be copied to the base HTML output directory. Use the +# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these +# files. In the HTML_STYLESHEET file, use the file name only. Also note that the +# files will be copied as-is; there are no commands or markers available. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_EXTRA_FILES = + +# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen +# will adjust the colors in the style sheet and background images according to +# this color. Hue is specified as an angle on a colorwheel, see +# http://en.wikipedia.org/wiki/Hue for more information. For instance the value +# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300 +# purple, and 360 is red again. +# Minimum value: 0, maximum value: 359, default value: 220. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_HUE = 220 + +# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors +# in the HTML output. For a value of 0 the output will use grayscales only. A +# value of 255 will produce the most vivid colors. +# Minimum value: 0, maximum value: 255, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_SAT = 100 + +# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the +# luminance component of the colors in the HTML output. Values below 100 +# gradually make the output lighter, whereas values above 100 make the output +# darker. The value divided by 100 is the actual gamma applied, so 80 represents +# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not +# change the gamma. +# Minimum value: 40, maximum value: 240, default value: 80. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_COLORSTYLE_GAMMA = 80 + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting this +# to NO can help when comparing the output of multiple runs. +# The default value is: YES. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_TIMESTAMP = YES + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_DYNAMIC_SECTIONS = NO + +# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries +# shown in the various tree structured indices initially; the user can expand +# and collapse entries dynamically later on. Doxygen will expand the tree to +# such a level that at most the specified number of entries are visible (unless +# a fully collapsed tree already exceeds this amount). So setting the number of +# entries 1 will produce a full collapsed tree by default. 0 is a special value +# representing an infinite number of entries and will result in a full expanded +# tree by default. +# Minimum value: 0, maximum value: 9999, default value: 100. +# This tag requires that the tag GENERATE_HTML is set to YES. + +HTML_INDEX_NUM_ENTRIES = 100 + +# If the GENERATE_DOCSET tag is set to YES, additional index files will be +# generated that can be used as input for Apple's Xcode 3 integrated development +# environment (see: http://developer.apple.com/tools/xcode/), introduced with +# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a +# Makefile in the HTML output directory. Running make will produce the docset in +# that directory and running make install will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at +# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html +# for more information. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_DOCSET = NO + +# This tag determines the name of the docset feed. A documentation feed provides +# an umbrella under which multiple documentation sets from a single provider +# (such as a company or product suite) can be grouped. +# The default value is: Doxygen generated docs. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# This tag specifies a string that should uniquely identify the documentation +# set bundle. This should be a reverse domain-name style string, e.g. +# com.mycompany.MyDocSet. Doxygen will append .docset to the name. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify +# the documentation publisher. This should be a reverse domain-name style +# string, e.g. com.mycompany.MyDocSet.documentation. +# The default value is: org.doxygen.Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_ID = org.doxygen.Publisher + +# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher. +# The default value is: Publisher. +# This tag requires that the tag GENERATE_DOCSET is set to YES. + +DOCSET_PUBLISHER_NAME = Publisher + +# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three +# additional HTML index files: index.hhp, index.hhc, and index.hhk. The +# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop +# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on +# Windows. +# +# The HTML Help Workshop contains a compiler that can convert all HTML output +# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML +# files are now used as the Windows 98 help format, and will replace the old +# Windows help format (.hlp) on all Windows platforms in the future. Compressed +# HTML files also contain an index, a table of contents, and you can search for +# words in the documentation. The HTML workshop also contains a viewer for +# compressed HTML files. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_HTMLHELP = NO + +# The CHM_FILE tag can be used to specify the file name of the resulting .chm +# file. You can add a path in front of the file if the result should not be +# written to the html output directory. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_FILE = + +# The HHC_LOCATION tag can be used to specify the location (absolute path +# including file name) of the HTML help compiler (hhc.exe). If non-empty, +# doxygen will try to run the HTML help compiler on the generated index.hhp. +# The file has to be specified with full path. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +HHC_LOCATION = + +# The GENERATE_CHI flag controls if a separate .chi index file is generated +# (YES) or that it should be included in the master .chm file (NO). +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +GENERATE_CHI = NO + +# The CHM_INDEX_ENCODING is used to encode HtmlHelp index (hhk), content (hhc) +# and project file content. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +CHM_INDEX_ENCODING = + +# The BINARY_TOC flag controls whether a binary table of contents is generated +# (YES) or a normal table of contents (NO) in the .chm file. Furthermore it +# enables the Previous and Next buttons. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members to +# the table of contents of the HTML help documentation and to the tree view. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTMLHELP is set to YES. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and +# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that +# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help +# (.qch) of the generated HTML documentation. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify +# the file name of the resulting .qch file. The path specified is relative to +# the HTML output folder. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help +# Project output. For more information please see Qt Help Project / Namespace +# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace). +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt +# Help Project output. For more information please see Qt Help Project / Virtual +# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual- +# folders). +# The default value is: doc. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_VIRTUAL_FOLDER = doc + +# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom +# filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the +# custom filter to add. For more information please see Qt Help Project / Custom +# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom- +# filters). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this +# project's filter section matches. Qt Help Project / Filter Attributes (see: +# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes). +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHP_SECT_FILTER_ATTRS = + +# The QHG_LOCATION tag can be used to specify the location of Qt's +# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the +# generated .qhp file. +# This tag requires that the tag GENERATE_QHP is set to YES. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be +# generated, together with the HTML files, they form an Eclipse help plugin. To +# install this plugin and make it available under the help contents menu in +# Eclipse, the contents of the directory containing the HTML and XML files needs +# to be copied into the plugins directory of eclipse. The name of the directory +# within the plugins directory should be the same as the ECLIPSE_DOC_ID value. +# After copying Eclipse needs to be restarted before the help appears. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the Eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have this +# name. Each documentation set should have its own identifier. +# The default value is: org.doxygen.Project. +# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# If you want full control over the layout of the generated HTML pages it might +# be necessary to disable the index and replace it with your own. The +# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top +# of each HTML page. A value of NO enables the index and the value YES disables +# it. Since the tabs in the index contain the same information as the navigation +# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +DISABLE_INDEX = NO + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. If the tag +# value is set to YES, a side panel will be generated containing a tree-like +# index structure (just like the one that is generated for HTML Help). For this +# to work a browser that supports JavaScript, DHTML, CSS and frames is required +# (i.e. any modern browser). Windows users are probably better off using the +# HTML help feature. Via custom style sheets (see HTML_EXTRA_STYLESHEET) one can +# further fine-tune the look of the index. As an example, the default style +# sheet generated by doxygen has an example that shows how to put an image at +# the root of the tree instead of the PROJECT_NAME. Since the tree basically has +# the same information as the tab index, you could consider setting +# DISABLE_INDEX to YES when enabling this option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +GENERATE_TREEVIEW = NO + +# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that +# doxygen will group on one line in the generated HTML documentation. +# +# Note that a value of 0 will completely suppress the enum values from appearing +# in the overview section. +# Minimum value: 0, maximum value: 20, default value: 4. +# This tag requires that the tag GENERATE_HTML is set to YES. + +ENUM_VALUES_PER_LINE = 4 + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used +# to set the initial width (in pixels) of the frame in which the tree is shown. +# Minimum value: 0, maximum value: 1500, default value: 250. +# This tag requires that the tag GENERATE_HTML is set to YES. + +TREEVIEW_WIDTH = 250 + +# If the EXT_LINKS_IN_WINDOW option is set to YES, doxygen will open links to +# external symbols imported via tag files in a separate window. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +EXT_LINKS_IN_WINDOW = NO + +# Use this tag to change the font size of LaTeX formulas included as images in +# the HTML documentation. When you change the font size after a successful +# doxygen run you need to manually remove any form_*.png images from the HTML +# output directory to force them to be regenerated. +# Minimum value: 8, maximum value: 50, default value: 10. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_FONTSIZE = 10 + +# Use the FORMULA_TRANPARENT tag to determine whether or not the images +# generated for formulas are transparent PNGs. Transparent PNGs are not +# supported properly for IE 6.0, but are supported on all modern browsers. +# +# Note that when changing this option you need to delete any form_*.png files in +# the HTML output directory before the changes have effect. +# The default value is: YES. +# This tag requires that the tag GENERATE_HTML is set to YES. + +FORMULA_TRANSPARENT = YES + +# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see +# http://www.mathjax.org) which uses client side Javascript for the rendering +# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX +# installed or if you want to formulas look prettier in the HTML output. When +# enabled you may also need to install MathJax separately and configure the path +# to it using the MATHJAX_RELPATH option. +# The default value is: NO. +# This tag requires that the tag GENERATE_HTML is set to YES. + +USE_MATHJAX = NO + +# When MathJax is enabled you can set the default output format to be used for +# the MathJax output. See the MathJax site (see: +# http://docs.mathjax.org/en/latest/output.html) for more details. +# Possible values are: HTML-CSS (which is slower, but has the best +# compatibility), NativeMML (i.e. MathML) and SVG. +# The default value is: HTML-CSS. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_FORMAT = HTML-CSS + +# When MathJax is enabled you need to specify the location relative to the HTML +# output directory using the MATHJAX_RELPATH option. The destination directory +# should contain the MathJax.js script. For instance, if the mathjax directory +# is located at the same level as the HTML output directory, then +# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax +# Content Delivery Network so you can quickly see the result without installing +# MathJax. However, it is strongly recommended to install a local copy of +# MathJax from http://www.mathjax.org before deployment. +# The default value is: http://cdn.mathjax.org/mathjax/latest. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest + +# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax +# extension names that should be enabled during MathJax rendering. For example +# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_EXTENSIONS = + +# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces +# of code that will be used on startup of the MathJax code. See the MathJax site +# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an +# example see the documentation. +# This tag requires that the tag USE_MATHJAX is set to YES. + +MATHJAX_CODEFILE = + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box for +# the HTML output. The underlying search engine uses javascript and DHTML and +# should work on any modern browser. Note that when using HTML help +# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) +# there is already a search function so this one should typically be disabled. +# For large projects the javascript based search engine can be slow, then +# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to +# search using the keyboard; to jump to the search box use + S +# (what the is depends on the OS and browser, but it is typically +# , /`0`*^ $^.` <^F]F^F]G`G] F\\S\\ ;b %a2a2a2a2a a:]" +".a !^T_ Bg ` Dd2_8n?m7g3]:rD]P]P]@g <] 8] 8] B] 3e J^K^ If7^U^+b@d Fb@f5a Ad4e-] :f Ra0d AaF\\HaF\\HeJ\\?]._0_0_0_0_2\\U\\0tHh@n?n?n?n?].].].]" +"-h:_J]w " +"P[ 9[/a:aQa7[ Wl \"h E]1]T]+\\R\\;[4dL]Ag=])]2])\\ U^1f8c8k;j1`;k7h?n;h9g 5i*b:_8k6kBl=n?l7mD]H]C].].]L_A].`I`H`K]>kAj6kAj9kBuB]H]F]E]E^L_L^" +"R^L^D^I^BrBb7^+b(a D] ;] '] Gd A].].].].] ;] (b:].b #^Q] Dj !a Ff3_8n?m8i4]:rD]P]P]Bk ?_ 9] 9_ C]&[0f I]K]=]0g7^U^-fC\\S] IfBf6c B[" +"S]5[S].] `K]>k]*]3]W]6^U^._V_;]Wa5]*]2\\V\\6]Wa7^V^ I]*]2\\V\\5^V^2]7]+^V^ @]W\\=v P[ 9\\1c_8m:`R`Cn?n?l9`QaE]H]C].].]M_@].aKaH`K]?`S`Bk8`S`Bk;_R_BuB]H]F]E]D]MaM]P]L]B^K^ArB]1]&])c D] <] '] G] :].].].].] " +";] (^6]*^ #]P^ E^P\\ V^ H^T^4_8n?m:`S`6]:rD]P]P]C`S` Aa :] :a D]&[1^S\\ I^M^=]0^R[7^U^/^R^EZO\\ L^R^ N]U] :],\\0] \\H]B\\H]=\\M]>" +"]._0_0_0_0_0_/uK`R`Cn?n?n?n?].].].]-n@`K]?`S`>`S`>`S`>`S`>`S` H`ScE]H]C]H]C]H]C]H]E^K^@],^T^5],]1\\V\\6\\U`7^V^6]U\\ F],]2\\T\\6^U^=],]2\\U\\6^U^-e9\\U`4],]1\\" +"V\\6\\U`7^V^ H],]1\\V\\5^V^3]6]+^V^ B`1`1`1`1`6]W]>u P[ 9]2e>eUf;^ %q $^O\\ F]1]T],]S];[5]T]N\\@]P[=]*]0]2ZR\\RZ $]2]P]<_W]8]N]\\H\\A\\H\\<\\M\\=]/a2a2a" +"2a2a1_/]V];_M]C].].].].].].].]-]ObBaL]@^M^@^M^@^M^@^M^@^M^ J^N`D]H]C]H]C]H]C]H]E^K^@]-^Q]5].]1\\T\\7\\S]6^V^5c E].]2]S\\7^U^<].]2\\S\\7^U^,a6\\S]2].]1\\T\\7\\S" +"]6^V^ G].]1\\T\\6^V^4]5]+^V^ De6e6e6e6e9\\U\\>u P[ :_3f@gVf<_ &r $]M[ F]1]T],\\R]>d<^T^P]A^OZ=]+].]4]T\\T] &^3^P^=[S]8[K].]4\\X];],]!]<]N]>^O^ " +" 8ZM^3`P`Ba9]M^=^J\\C]K_B].],^H\\E]H]C].].]O_>].aKaHaL]A^K^D]N^<^K^D]N^>]JZ6]6]H]E]G]C]MaM]O^P^@^M^-^A]1]&]+_W_ D] >] '] H] 9] B].] ;] )]4](]" +" %]N]:c6] G] J^P^7a8_1],^K^;c=]H]D]P]P]E^K^ Ee <] \\I]A\\I]<\\N]=]/a2a2a2a2a2a1]U]<" +"^J\\C].].].].].].].]-]K_CaL]A^K^B^K^B^K^B^K^B^K^ K]K^D]H]C]H]C]H]C]H]D^M^?]-]P]4]0]1\\R\\ Ha C]0]2]R] E]0]2\\Q\\ 9c 9]0]1\\R\\ !]0]1\\R\\ ?]4] Di:i:i:i:i" +";\\6]G] P\\ :`5g@gWh>a (_ J]KZ F]1]T],\\R\\?h>]R]P\\@]1]+].]3^V\\V^.] T]2]N]5]8ZJ]-]6]X];]-]!^=]L]?]M] *]5_J_Ec:]L^>]H[C]I^C].],]F[E]H]C].].]" +"P_=].]X]M]X]HbM]A]I]D]M]<]I]D]M]?]%]6]H]E]G]C^NaN^N]Q^>^O^-^@]0]'],_U_ &] '] H] 9] B].] ;] )]4](] %]N]:d7] F] K]N]8c8^1],]I]>i@]H" +"]D]P]P]E]I] Fg =] =g G]&[2] <]O];]1] 1\\F\\=\\ Q\\F\\ S\\Q\\+]3\\.] IeU\\ M\\3\\N\\ ?\\I\\@\\I\\=]M\\<]0c4c4c4c4c3a1]U]<]H[C].].].].].].].]-]J_DbM]A]I]B]I]B]I]B]I]" +"B]I] L]J_E]H]C]H]C]H]C]H]C^O^>].]N] .] '`X_ I] FbWa=bWa=bWa=bWa=bWa<\\6^I^ ?Z2[ :a5gAiXh?c *^ H] 7]1]T]-]S]Aj>]R]Q]@]1]," +"],\\1^X\\X^,] T]3]L]6]'].]7]W];]-]!]<]L]?]M^ +]6^F^F]W]:]K]?]FZC]H^D].]-]DZE]H]C].].]Q_<].]X]M]X]H]X]M]B]G]E]M^>]G]E]M^@]%]6]H]E^I^B]O^X]O]M^R^=]O^" +"-^@]0]']-_S_ '] '] H] 9] B].] ;] )]4](] %]N]:e8_ H] L]M]8]W]7^2]-]G]AmB]H]D]P]P]F]G] Hi >] >i J[3] ;^Q^;]1] 2\\RbT\\Ge R\\VdR\\ T\\" +"Q\\+]4\\2a IfU\\ M\\3\\N\\ ?\\J\\?\\J\\AaM\\ G]W]4]W]4]W]4]W]4]W]4c3^U]=]FZC].].].].].].].]-]H]D]X]M]B]G]D]G]D]G]D]G]D]G]A[H[B]J`E]H]C]H]C]H]C]H]B]O^>g8]N] " +" 1]T_ 3[ 9] G_O^?_O^?_O^?_O^?_O^=\\5]I^ @\\3[ ;c6gAy?d7`8]L]7^7]L]>^ H] 6]1]T]-]S]B_W[U]>]R]R]?]1],],]0d*] T]3]L]6]'].]7\\V];]" +".] ]<]L]@]K] 7Z PZ X]7^D^G]W]:]K]?]/]G]D].]-]/]H]C].].]R_;].]X^O^X]H]X^N]B]G]E]L]>]G]E]L]@]%]6]H]D]I]A]O]W]O]L^T^<^Q^-^?]0]'].^O^ Sb7]U`2b4`U]8a8])`" +"7]T_ M].]%_O_@_2`0`3`/_3c9] )]4](] N_6]N]3^7a/c0_ <^ D[U^ Ga N]L]9]W]6^3]-]G]B`W]W`C]H]D]P]P]F]G] I_X]X_ ?] ?_X]X_ Nb7]2ZFZ=]Q]:]0] 3[SfU[I" +"g R[UfS[ T\\Q\\+]5]2a IfU\\ M\\3\\N\\ ?\\K]?\\K]AaN] G]W]4]W]4]W]4]W]4]W]4]W]3]T]=]/].].].].].].].]-]G]E]X^N]B]G]D]G]D]G]D]G]D]G]B]J]C]KbF]H]C]H]C]H]C]H]B" +"^Q^=j;]P_9b3b3b3b3b3b3bN`Bb3a2a2a2a V_2_2`1`1`1`1` ;aU] :]U` S^T]U^A^L^A^L^A^L^A^L^?]5]I] @^5\\ ]R]R\\>]1],],].`(] U^3]L]6]'].]8]V];].]!^<]L]@]K] :] P]#^8^A]I^W^;]K]@].]G^E].].].]H]C].].]S_:].]W]O]W]H]W]N]C]E]F]L]?]E]F]L]@]%]6]H]D]J^A]O]W]O]" +"L^U^:^S^-^>]0^(]/^M^ Wh:]Wd6f8dW]:e>h2dW]?]Vd<].].]O_>].]WdScK]Vd8f;]Wd7dW]?]Wa6h>h6]L]B]I]A]P`P]K^L^B^K^@l4]4](] PdU]A]N]2^8e5g;]Vd?^J^8]6]L] E]V`" +">pA]S]S]:e6kDo>]L]:^W^6^4].]E]D_U]U_D]H]D]P]P]G]E] K_W]W_ @] @_W]W_ Qf9]3\\H\\>^S^:]0_ 6[ThT[K]Q\\ S[T\\R]S[ U]S]+]6],] ?]L]@fU\\ M\\3\\N\\ ?\\K\\>\\K\\;]O\\ G" +"^W^6^W^6^W^6^W^6^W^5]W]4^T]>].].].].].].].].]-]G^F]W]N]C]E]F]E]F]E]F]E]F]E]D_L_E]K]W]F]H]C]H]C]H]C]H]A^S^^K^ O]S]S]B]I]B]I]B]I]B]I]@]5^K^ @]4[ ;f8gAyAg] F] 6]1]T]-\\R\\B]T[6]R]S]>^2]-]*\\.`(] U" +"]2]L]6]'].]9]U];].]!];]L]@]K] =` P`'^7]?\\I]U];]K]@].]F]E].].].]H]C].].]T_9].]W]O]W]H]W^O]C]E]F]L]?]E]F]L]@]%]6]H]C]K]@^P]W]P^K^V^9]S]-^=]/](]0^K^ Xi" +";]Xf9h9fX]h6]L]A]K]@^Q`Q^J^N^@]K]?l4]4](] QfW^A]O^1]6f9h;]Xg@_K]7]6]L]=]G]C^Wc@pA]S]S]]L]:]U" +"]5^5].]E]E^S]S^E]H]D]P]P]G]E]@Z+]V]V^-Z4]5ZKZ:]V]V^ Sh9]4^J^>]S]9]._ 8[U_Q[T[L]P\\ S[T\\Q]T[ T]U]*]7]*] @]L]@fU\\ M\\3\\N\\ ?\\L]>\\L]:]Q]:]1]U]6]U]6]U]6]" +"U]6]U]6^W^5]S]>].].].].].].].].]-]F]F]W^O]C]E]F]E]F]E]F]E]F]E]C_N_D]L^W]F]H]C]H]C]H]C]H]@]S];]P_=]S^8i:i:i:i:i:i:iVgIh9h9h9h9h<].].].]'d<]Xg:h9h9h9h9h" +"0^8k?]L]?]L]?]L]?]L]A]K]>]Xf>]K] O]R]R]D]G]D]VZOZV]D]KZV]D]G]A]4]K] @]3[ j=]L]8`7]N]?] F^ 6]1]T]5uI]T[6]R]S\\<^3]-]*]1d*] U]3]J]7]']" +".]9\\T];].\\Ua-^;]L]@]K^?].] Uc Pc+_8]>]J]U];]K]@].]F]E].].].]H]C].].]U_8].]W^Q^W]H]V]O]C]E]F]L]?]E]F]L]@^&]6]H]C]K]?]Q^V]Q]I^X^8^U^.^<]/](]1^I^ ]R_h6]L]A]K]?]Q`Q]H^P^?]K]?l4]4](] R^U^W]@]O]0^7g;_S];bT^@`L]8_7]L]>]E]E^W]V]@pA]S]S]" +"=_T_].].].].].].].].]-]F]F]V]O]C]E]F]E]F]E]F]E]F]E]B_P_C]L]V^G]H]C]H]C]H]C]H]@^U^;]N^>]T]6]R_;]R_;]R_;]R_;]R_;]R_;]R" +"_X_T^K_R\\:_S^;_S^;_S^;_S^=].].].]*h=bT^;_T_;_T_;_T_;_T_;_T_1^9_T`>]L]?]L]?]L]?]L]A]K]>aT_?]K] P]Q]R]E]F]E]V\\Q\\W]E]K\\W]E]F]A]4^L] A^@ZN\\ =i8e@yCk?^R^" +"=]L]9b8]O^?] Im B]1]T]5uI]T[6]S^T]<^3]-]*]3^X\\X^,] V^3]J]7](^/]9]T];e7]We/]9]N]?]K^?].] Wd Nd._8]O`U\\T\\K]S]<]L^A]-]F^F].]/]-]H]C].].]V_7].]V]Q" +"]V]H]V^P]D]C]G]L]@]C]G]L]?^']6]H]C^M^?]Q]U]Q]Ic6^W^._<]/^)]2^G^ !ZM^=`Q^=^NZ;^Q`>^P^=].^Q`?`Q^>].].]R_;].`R^X\\R^M`Q^=^P^>`Q^=^Q`?`1]MZ;].]L]A^M^?]Q`Q]" +"G^R^>^M^1^4]4](] D]P^A]R^X]@]P^/]9^Vb=^NZ;`Q^AaN^8_7]L]>]E]F^V]U]>]P]>]S]S]>^P^>`T`7]6]J]<]S]5^6]/]C]G]Q]Q]F]H]D]P]P]H]C]C^&]TZ,^7]7^N^6]TZ H]/^U[TZ9" +"]2n;]U]8]0d <[U]F[M\\P]2[R[ M[S\\P\\S[ Tb(]9]'\\ @]L]@fU\\ M\\3]P]9[R[1\\M\\<\\M\\7\\R\\8]2]S]8]S]8]S]8]S]8]S]7]U]6]R]?]-].].].].].].].]-]F]F]V^P]D]C]H]C]H]C]H]" +"C]H]C]B_R_C]L]T]G]H]C]H]C]H]C]H]?^W^:]M]>]U^6ZM^].].].]+i=`Q^=^P^=^P^=^P^=^P^=^P^2^:^P^>]L]?]L]?]L]?]L]" +"A^M^>`Q^@^M^ P]Q]Q]F]E]F]W^S^W]F]L^W]F]E]B]3]M^ B^B^O[ =k8d?xClA^P^>]L]9]X]8^P]>\\ Hl A] 9uI]T[5]T]T]:^ =]*]5^V\\V^.] V]2]J]7](]/^:]S];h:]Xg0]" +"9^P^?]K^?].]!e Je2_7\\PdW\\S\\L]S]<]M^@]-]E]F].]/]-]H]C].].]X_5].]V]Q]V]H]U^Q]D]C]G]L]@]C]G]M^?`)]6]H]B]M]>]Q]U]Q]Hb5c-^;].])] B]=_O]=].]O_>]N^>].]O_?_" +"O]>].].]S_:]._P`P]M_O]=]N]>_O]=]O_?_1]-].]L]@]M]>]RbR]G^R^=]M]1^3]4](] FaSaD^Qa?]R_.]9]R`>]._O]>^N]8`7]L]>]E]G^U]U^?]P]>]S]S]>]N]>^P^7]6]J]<]S]4^7]/]" +"C]G]Q]Q]F]H]D]P]P]H]C]D_&]&_8]8_N_7] B]/]T[3]1l:^W^8]1]W` >\\U\\E\\N\\P]3\\S\\ N\\S\\P\\S\\ S_']:]&\\ @]L]@fU\\ M\\2\\P\\8\\S\\2\\N]<\\N]7\\S]8]2]S]8]S]8]S]8]S]8]S]8]S]" +"7]R]?]-].].].].].].].]-]E]G]U^Q]D]C]H]C]H]C]H]C]H]C]A_T_B]M]S]G]H]C]H]C]H]C]H]>c9]M^?]U]'].].].].].].`O^N].]N^>]N^>]N^>]N^?].].].],_R^>_O]=]N]=]N]=]N]" +"=]N]=]N]2^:]O_?]L]?]L]?]L]?]L]@]M]=_O]?]M] O\\P]Q]F\\D]F\\U^U^V]F\\L^V]F\\D]B]3]M] RuJ`O[ >m9c>wCmA]N]>]L]9]X]7]P]?] Im A] 2\\R\\A]T[5^V^T\\:` ?](\\6]T" +"\\T]/] V]2]J]7])^1_9]S];i;bS^2^8^S_>]K^?].]$e@u@e6_7]QfX\\S\\M^S^=]N^?]-]E]F].]/]-]H]C].].c4].]U]S]U]H]T]Q]D]C]G]M^@]C]G]M]=c-]6]H]B]M]>^R]U]R^G`4c.^:]" +".])] B]=^M]?^/]M^?]L]>]/]M^?^N^?].].]T_9].^O_O^N^N^?]M^?^M]?]M^?^0]-].]L]@]M]>^S]X]S^F^T^<^O^2_3]4](] GcUcE]Pa?]Vb-]:]O_?].^N^>]O^8a8]L]?]C]H]T]T]?" +"]P]>]S]S]?]L]@^N^8]6]J]=^S^4^8]/]C]H^Q]Q^G]H]D]P]P]H]C]E_%]%_9]9_L_8] B]0^T[3]0_T_>cWc=]1]U_ ?[U\\C[N]R^4]T] N[R\\Q]R[ 'uG]&] @]L]?eU\\ M\\2]R]8]T]3\\N\\;" +"\\N\\7]S\\7]3^S^:^S^:^S^:^S^:^S^9]S]8^R]?]-].].].].].].].]-]E]G]T]Q]D]C]H]C]H]C]H]C]H]C]@_V_A]N]R]G]H]C]H]C]H]C]H]>c9]L]?]U]'].].].].].]._M]O^/]L]?]L]?]L" +"]?]L]?].].].]-^O]>^N^?]M^?]M^?]M^?]M^?]M^ I]O`?]L]?]L]?]L]?]L]@^O^=^M]@^O^ P]P]P\\G]C\\G]T^W^T\\G]M^T\\G]C\\B]3^O^ RuJ[X]P[ >o=\\XaX]BwDoC]L\\>]L]:^X^8]P]?" +"] E] 5] 3]S]A^U[4dT];b @](]6ZR\\RZ.] V]2]J]7]*^7d8]R];]R_]-]E]Fm>k=]-rC].].b3].]U]S]U]H]T^R]D]C]G]M]?]C]" +"G]N^^M]?].]M^?]L]>]/]M^?^M]?].].]U_8].^N^N]N^M]?]L]?^M]?]M^?^0]-].]L]@^O^=]S]X]S]D^V^:]O]2_2]4](] H\\U^W]U\\E]Pa?" +"]Vb-];]M^?].^M]>^P]7a8]L]?]C]H]T]T]?]P]>]S]S]?]L]@]L]8]6p=]Q]3^9]/]C]H]P]P]G]H]C]Q]Q]G]ViV]F_$]$_:]:_J_9] B]0]S[3]0]P]>o=]2]S_ @[U\\C[M]T_5^U^;u O[R\\R]" +"Q[ 'uH]/ZQ] ?]L]?eU\\ M\\1]T]7^U^4\\O]O]I\\O]T`MZQ]S]O]E]3]Q]:]Q]:]Q]:]Q]:]Q]:^S^9]QmO]-m>m>m>m>].].].]1hL]G]T^R]D]C]H]C]H]C]H]C]H]C]?_X_@]O]Q]G]H]C]H]C]" +"H]C]H]=a8]L]?]U]&].].].].].].^M]O].]L]?]L]?]L]?]L]?].].].].^M]?^M]?]L]?]L]?]L]?]L]?]L] I]Pa?]L]?]L]?]L]?]L]?]O]<^M]?]O] O]P]P\\G]C\\G]ScS\\G]N^S\\G]P]P\\B" +"]2]O] QuF]Q[ >oAqDuDqD]L]?]L]:^X^8^R^?\\ D] 5] 3]S]@`X[3bS\\R^G]W^N] P](].\\&] W]1]J]7]*^7c8]Q];ZM^=`O^4]4d:]M_?].])d:u:d=_5\\R]O^R\\N]Q]=j<]-]E]F" +"m>k=]-rC].].a2].]U^U^U]H]S]R]D]C]G]N^?]C]G]P_:g3]6]H]A]O]<]S]S]S]E^1_.^8]-]*] A]>^M]?]/^M^?]K]?]0^M^?]L]?].].]V_7].]M]M]N]L]@^L]?^M]@^M^?]/]-].]L]?]" +"O]<]S]X]S]C^X^9]O]2^1]4](]0_IZ O[R\\X]S\\G^O_>]Vd9_U];]L]?].]L]=]P]8]X^9]L]?]C]I^T]S]@]P]>]S]S]?]L]@]L^9]6p=]Q]3^9]/]C]H]P]P]G]H]C]Q]Q]G]ViV]G_#]#_;];_H" +"_:] B]0]S[3]0\\N\\>o=]2]Q^ A[U\\C[LcX\\6]T]9u O[RfP[ 'uIf7e >]L]>dU\\<] :f5d4]T]:fT\\O^NfT\\UdOeR\\O^F^3]Q]:]Q]:]Q]:]Q]:]Q]:]Q]:^QmO]-m>m>m>m>].].].]1hL]G]S]R" +"]D]C]H]C]H]C]H]C]H]C]>d?]P^Q]G]H]C]H]C]H]C]H]<_7]L]?]U^'].].].].].].^L]P].]K]@]K]@]K]@]K]@].].].].]L]?]L]@^L]@^L]@^L]@^L]@^L] I]Q]X^@]L]?]L]?]L]?]L]?]" +"O]<^M]?]O] O\\WmX]H\\WmX]H\\QaR]H\\N^R]H\\O]P]C]2]O] QuF]R\\ ?qCsDtDrE]L]?]L]:]V]7]R]>x '] 5] 3\\R\\?e3^R\\SbJ^V^O] P](].\\&] W]1]J]7]+^6e:]Q]-^>_M]5^6" +"h<^O` Qe8u8e@^5]R\\M]R\\O^Q^>m?]-]E]Fm>k=]KdFrC].].b3].]T]U]T]H]S^S]D]C]G]P_>]C]Gk6f5]6]H]A^Q^<]S]S]S]F_1_/_8]-]*] A]>]K]A].]K]@]J]?]0]K]?]L]?].].]W_" +"6].]M]M]N]L]@]J]@]K]A]K]?]/^.].]L]?]O]<]T^W]T]C^X^9^Q^3^1]3]']3dN\\ P\\R`Q[G]N_>]Q`;bW];\\K^?]/]L]=]Q^8]W]9]L]?]C]I]S]S]@]P]>]S]S]@]J]B^L^9]6p>^Q^4^9]/]C" +"]H]P]P]G]H]C]Q]Q]G]ViV]H_\"]\"_<]<_F_;] B]1]R[3]1]N]8a6]2]P^ B[U\\C[K`V\\7]T]8u O[RdN[ 'uIf5a <]L]=cU\\<] :f3`1]T];fU\\N^NfU\\T[S]NaQ\\N^G^3^Q^<^Q^<^Q^<^Q^<^Q" +"^;]Q]:]PmO]-m>m>m>m>].].].]1hL]G]S^S]D]C]H]C]H]C]H]C]H]C]=b>]P]P]G]H]C]H]C]H]C]H]<_7]L]?]U_(].].].].].].]K]Q].]J]A]J]A]J]A]J]@].].].].]L]?]L]@]J]A]J]A" +"]J]A]J]A]J] K]P\\V]@]L]?]L]?]L]?]L]?^Q^<]K]@^Q^ O\\WmX]H\\WmX]H\\P_Q]H\\O^Q]H\\O]P]C]2^Q^ D^<]R[ >qDuEsCqD]L]?]L]:]V]7]R]>x '] 5] 3\\R\\=f+]TdL^T^P] P]" +"(].\\2u *]1]J]7],^-_=]P],]>_M]5]7_R^<^Qa Sd .dC^4\\R]M]R\\O]O]>]N_@]-]E]F].]/]KdF]H]C].].]X^4].]T]U]T]H]R]S]D]C]Gk=]C]Gj1c6]6]H]@]Q];^T]S]T^Ga1].^7]-]*" +"] Lh>]K]A].]K]@]J]?]0]K]?]L]?].].]X_5].]M]M]N]L]@]J]@]K]A]K]?]._0].]L]>]Q];^U]V]U^Bb7]Q]3^1^3]'^6iS^ P[P^P[G]N_>]N^=dX]<]J]>^1]L]=^R]8^W]9]L]@]A]J]S" +"]S]@]P]>]S]S]@]J]B]J]9]6]J]>]O]5^8]/]C]H]P]P]G]H]B]R]R]F]C]Iz<]]K]@]" +"O[X\\I`3]O]<]O]<]O]<]O]<]O]<]O];]P]?]-].].].].].].].]-]E]G]R]S]D]C]H]C]H]C]H]C]H]C]<`=]Q]O]G]H]C]H]C]H]C]H];]6]L]?]T_4h9h9h9h9h9h9hK]Q].]J]A]J]A]J]A]J]" +"@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]Q\\U]@]L]?]L]?]L]?]L]>]Q];]K]?]Q] N\\WmX]H\\WmX]H\\P_Q]H\\P^P]H\\O]P]C]1]Q] C]:]S[ ?sEvEqAoC]L]?]L];^V^8^T^>x " +" '] 5] 4]S]]K]A].]K]@p?]0]K]?]L]?].].b3].]M]M]N]L]@]J]@]K]A]K]?].c4].]L]>]Q]:]U]V]U]@`6^S^4^5b2]&b^Ua<]J]=" +"c7]L]<]S^8]V^:]L]@]A]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]?^O^7^7]/]C]H]P]P]G]H]B]R]R]F]C]Iz<]\\I\\@\\O\\X\\J`3^O^>^O^>^O^>^O^>^O^=]O]<^P]?]-].].].].].].].]-]E]G]R^T]D]C]H]C]H]C]H]C]H]C];^<]R]N]G]H]C]H]C]H]C]H];]6]L]?]S`8j;j;j;j;j" +";j;|Q].pApApAp@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]R]U]@]L]?]L]?]L]?]L]>^S^;]K]?^S^ N\\WmX]H\\WmX]H\\QaR]H\\Q^O]H\\O]P]C]1^S^ D]9]T\\ ?sFwDo?nC]L]?]L];" +"]T]7]T]=] Hj ?] 4]S]8d/]T]T]N^R_R\\ O](] =u Se =]0]J]7].^(]?]O]+]?^K]7]7]L]]K]A].]K]@p?]0]K]?]L]?].].a2].]M]M]N]L]@]J]@]K]A]K]?]-f8].]L]>^S^:]U]V]U]?^4]S]4^4`0]$`<^Si O[O" +"\\O\\H]N^=]M^@^S`<]J]=c7]L]<]S]8^U]:]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]?]M]7]6]/^E^H]P]P]G]H]A]S]S]E]C]Iz<]]M]>]M]>]M]>]M]>^O^=]O]?]-].].].].].].].]-]E]G]Q]T]D]C]H]C]H]C]H]C]H]C]<`=]S]M]G]H]C]H]C]H]" +"C]H];]6]M^?]R`;l=l=l=l=l=l=~Q].pApApAp@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]S]T]@]L]?]L]?]L]?]L]=]S]:]K]>]S] M]P]P\\G]C\\G]ScS\\G]S^N\\G]P]P\\B]0]S] D]" +"7\\T[ >sFwCn?mB]L]?]L];]T]7]T]=] Hi >] 4]S]7[Xa1]T^T^O]P_T] O](] =u Se =]0]J]7]/^'^A]N]+]?^K]7]8^L^]K]A].]K]@p?]0]K]?]L]?].].b3].]M]M]N]L]@]J]@]K]A]K]?]+e9].]L]=]S]9]V]T]" +"V]@_4]S]5_4b2]&b<\\Nd M[O]P\\H]N^=]L]@]Q_<]J]?e7]L];]T]8]T]:]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]?]M]8^6].]E]G]P]Q^G]H]A^T]T^E]C]Iz<]]M]>]M]>]M]>]M]>]M]>^O]?]-].].].].].].].]-]E]G]Q^U]D]C]H]C]H]C]H]C]" +"H]C]=b>]T]L]G]H]C]H]C]H]C]H];]6]M]>]Qa>`P]>`P]>`P]>`P]>`P]>`P]>`PoQ].pApApAp@].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J]?tG]T]S]@]L]?]L]?]L]?]L]=]S]:]K]>]S] " +"L\\P]P\\F\\C\\F\\T^W^T\\F\\T^M\\F\\C\\B]0]S] E^7]U[ >sFwBl=kA]L]?]L]<^T^8^V^=] Ij >] ]K]A].]K]@],]0]K]?]L]?].].c4].]M]M]N]" +"L]@]J]@]K]A]K]?](d;].]L]=]S]9^W]T]W^@`5^U^5^/_3]'_8ZJ` K[O]P\\H]N^=]L]@]P];]J]@_0]L];]U^9^T^;]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]6]J]@^M^:^5].]E]F]Q]Q]F" +"]H]@^U]U^C]E]G_\"]\"_BZT]TZB_F_;] B]1]R[3]1\\L\\?o I_S] A[U]F[ V]T] W] N[S\\R]R[ S] ]L]6\\U\\ ']T]/\\O\\V\\@\\H\\A\\O\\V\\M_0o@o@o@o@o?m>l>].].].].].].].].]-]F^" +"G]P]U]C]E]F]E]F]E]F]E]F]E]=d?^V]L]F]H]C]H]C]H]C]H];]6]N^>]O`?]M]>]M]>]M]>]M]>]M]>]M]>]M]?].].].].]-].].].]/]J]@]L]@]J]A]J]A]J]A]J]A]J] K]U]R]@]L]?]L]?" +"]L]?]L]=^U^:]K]>^U^ L\\P]Q]F\\D]F\\U^U^V]F\\U^M]F\\D]B\\/^U^ OuD]V[ =sFwBk;i@]L]?]L]<]R]7]V];] F^ Nu=[T^3]S]R]O]N_V\\ N](] 1] ].]L]6]1_%]Aq0]>]K]" +"8]7]J]/] Md:u:d>]3\\R\\K\\S\\Po@]J]A].]F]E].].]E]F]H]C].].]S^9].]RaR]H]P^V]C]E]F].]E]F]M],]8]6]H]>]U^8]W^Q^W]H^U^4]2^3]+],] R^M]>]K]A].]K]@],]0]K]?]L]?" +"].].]X_5].]M]M]N]L]@]J]@]K]A]K]?]$`;].]L]=^U^8]W]T]W]@b5]U]5^,]3]'] J\\Q_Q[G]N^=]L]A]O];]J]@].]L];]U]8]R];]L]@]O]O]J]S]S]@]P]>]S]S]@]J]B]J]9]5]L]?]K];" +"^4].^G^F]Q]Q]F]H]?_W]W_B]E]F_#]#_B\\U]U\\B_H_A\\U]U[ H]1]R[3]1]N]?o H`V] @[T]G[ U]T] X] N[S\\Q]S[ S] ]L]6\\U\\ (]T]/]P\\U\\A]I]B]P\\U\\M^/o@o@o@o@o@o@m>].]" +".].].].].].].]-]F]F]P^V]C]E]F]E]F]E]F]E]F]E]>_X_?]W^L]F]H]C]H]C]H]C]H];]6]P_=]M^@^M]?^M]?^M]?^M]?^M]?^M]?^M]?].].].].]-].].].]/]J]@]L]@]J]A]J]A]J]A]J]" +"A]J] K]U\\Q]@]L]?]L]?]L]?]L]<]U]9]K]=]U] K]Q]Q]F]E]F]W^S^W]F]W^L]F]E]B\\.]U] NuC\\V[ =eXZXdFgXhAi9h@]L]?]L]<]R]7]V];] E] Nu=[S]3\\R]R]O]M_X\\ M](" +"] 1] ].]L]6]2_$]Aq0]>]K]8]7]J]/] Ke=u=e<]3\\R\\K\\S\\Po@]J]A].]F]E].].]E]F]H]C].].]R^:].]RaR]H]O^W]C]E]F].]E]F]M^-]8]6]H]>]U]7]W]O]W]I^S^5]3^2]+],] R" +"]L]>]K]A].]K]@],]0]K]?]L]?].].]W_6].]M]M]N]L]@]J]@]K]A]K]?]\"_<].]L]<]U]7]W]T]W]Ac5^W^6^+^4](] H[R\\X]S\\G]N^=]L]A]O];]J]A^.]L]:]W^9^R];]L]@]O]O]J]S]S]@" +"]P]>]S]S]@]J]B]J]9]5]L]?]K];^4]-]G]D]R]R]E]H]>kA]E]E_$]$_B^V]V^B_J_A^V]V] I]1]R[3]0\\N\\>o G`X] ?\\U_Q[T\\ T]T] ] N\\T\\Q]T\\ S] ]L]6\\U\\ )]T].\\P\\T\\A\\I]A" +"\\P\\T\\N^.o@o@o@o@o@o@m>].].].].].].].].]-]F]F]O^W]C]E]F]E]F]E]F]E]F]E]?_V_@]W]K]F]H]C]H]C]H]C]H];]6k<]L^A]L]?]L]?]L]?]L]?]L]?]L]?]L]?].].].].]-].].].]/" +"]J]@]L]@]J]A]J]A]J]A]J]A]J] K]V\\P]@]L]?]L]?]L]?]L]<^W^9]K]=^W^ J]R]R]D]G]D]W\\Q\\W]D]W\\L]D]G]A\\.^V] NuC]W[ ]K]9]6]J]/] He@u@e H\\R]M]T]Q^J]A]J]@]/]G^E].]-]F]F]H]C].].]Q^;].]Q_Q]H]N]W]B]G]E]-]G^F]L]-]8]6]I^>^W^7]" +"W]O]W]I^R^6]4^1]+],] R]M^>^M^@]/^M^?]-]0^M^?]L]?].].]V_7].]M]M]N]L]@^L]?^M^A^M^?] ]<].]L]<]U]7]X]R]X]B^W^5]W]6^)]4](] H\\T]W]U\\F]O_=]L]A]P^;^L^A]-]L" +"]:]W]8]P]<]L]@]O]O]J^T]T]?]P]>]S]S]@^L]A^L]8]5]L]@^J]=^3]-^I^D^S]S^E]H]]G]C_%]%_A_W]W_A_L_@_W]W_ J]0]S[3]0]P]5]4],b =[ThT[ R]T]!] M[T\\P]U[ R] ]L" +"]6\\U\\ *]T].]P[S\\B]J]A]P[S\\N].^J]B^J]B^J]B^J]B^J]B^K^A]M]=]/].].].].].].].]-]G^F]N]W]B]G]D]G]D]G]D]G]D]G]?_T_AbK]E]I^C]I^C]I^C]I^;]6j;]K]A]M^?]M^?]M^" +"?]M^?]M^?]M^?]M_?].].].].].].].].]/]J]@]L]@^L]@^L]@^L]@^L]@^L] J^X]Q]?]L]?]L]?]L]?]L];]W]8^M^<]W] I]R]S]C]H]C]VZOZW]C]VZL]C]H]@\\-]W] MuC]X[ ;cWZWbDe" +"WZXe>e6e>]L]?]L]=]P]8^X^:] F^ H\\R\\5[S]5]Q]R]O^L` K]*] 0] !^.]L]6]4_\"]2],^>^M]8]6]J]0] DeCuCe E]R\\M]T\\P]I]A]J]@]/]G]D].]-]F]F]H]C].].]P^<].]Q" +"_Q]H]N^X]B]G]E]-]G]E]L^.]8]5]J]<]W]6^X]O]X^J^Q^6]5^0]+^-] R]M^>^M]?].]M^?]-]/]M^?]L]?].].]U_8].]M]M]N]L]?]L]?^M]?]M^?] ]<].]M^<^W^6aRbB^V^6]W]7^(]4]" +"(] GcUcE]P_=]L]A]P]9]L]@]-]L]:^X]9^P]<]M^@]P^O]I]T]T]?]P]>]S]S]@^L]@]L]8]5]M]?]I]>^2],]I]B_U]U_D]H]:c<]G]B_&]&_?_X]X_?_N_>_X]X_ I]0]S[3]0_T_5]4]+` ;[" +"SfU[ P^U^#] L[U\\P]V[ Q] ]M^6\\U\\ ,^U^-\\P\\S\\B\\J]@\\P\\S\\N].]I]B]I]B]I]B]I]B]I]B]I]B^M]=]/].].].].].].].]-]G]E]N^X]B]G]D]G]D]G]D]G]D]G]@_R_A`J]D]J]A]J" +"]A]J]A]J]:]6g8]K]A]M^?]M^?]M^?]M^?]M^?]M^?]M_?].].].].].].].].].]L]?]L]?]L]?]L]?]L]?]L]?]L]3^;aP]?]M^?]M^?]M^?]M^;]W]8^M];]W] H]S]T^B]J^B]J^B]J^B]J^@" +"\\-]W] G^1_ :aW[V`BcW[Wc]N]<]P]7]X]8] F]KZ X]S]5[S]5\\P]R]N]K_ K]*] 0] !],]N]5]5_\"]1],]<]M]9^6^L^0] Ad Nd A\\R]O^U\\P^I^B]K^?]H[C]H^D]" +".],]G]F]H]C].].]O^=].]P^Q]H]M]X]A]I]D],]I^E]K]AZH^8]5]J]<]W]5bObJ^O^7]6_0]*]-] R]M^>^M]?^/]M^?^.]/]M^?]L]?].].]T_9].]M]M]N]L]?]L]?^M]?]M^?] ]<].]M^;" +"]W]5aRaB^U^6c8_(]4](] FaSaD]P_=]M]@]P]9]L]@]-]L]9b9]O^=^N^?\\P_Q]H]T]T]?]P]=]T]T]?^L]@]L]8]4]N]@^I^?]1],^K^A`W]W`C]H]7]8]I]@^&]&^=i=^N^^P^=^P]7]X]8_ H^M[ F] 6]S]>ZQ[T^6]P]S^N^K^ K]*] 0]:] 8]0],]O^5]6_2ZI]1]-^<^O^9]4]L]0]<].] Uc Pc1]2\\Q^S`W^P]G]B]K]" +">^J\\C]I^C].],^H]F]H]C].].]N^>].]C]H]MbA^K^D],^K^D]K^B[I]7]5^L^_O]=].]O_>].].]O_?]L]?].].]S_:].]M]M]N]L]>]N]>_O]=]O_?] ]<]-" +"]O_;]X^5aRaC^S^6a8_']4](] D]P^B^Ra>^N]@]Q]7]N]?^.]L]9a8]N]=^N^?]Q_Q]G]U]U]>]P]=]T]T]?_N]>]N]7]4^P^@]G]@^1]+^M^?mB]H]7]8^K^?\\%]%\\;g;\\L\\:g G]/]T[3]2n7]" +"4]'^ <\\F\\ M\\S\\ J\\F\\ L^N^6\\U\\ ,\\S\\-]OhG]K]@]OhQ]LZ=]G]D]G]D]G]D]G]D]G]D]G]D^L]<^J\\C].].].].].].].]-]J_D]MbA^K^B^K^B^K^B^K^B^K^A_N_B^K]B^L^A^L^A^" +"L^A^L^:]6].]K]A^O_?^O_?^O_?^O_?^O_?^O_?^Oa?].].].].]/].].].]-]N]>]L]>]N]=]N]=]N]=]N]=]N]2^;_O]=]O_>]O_>]O_>]O_:a7_O]9a E^P_>^P_>^P_>^P_>^P_>\\,a H^.]" +" /[5]T[S\\8a1`<]L]=^R^<]O^8b7_ H^O\\ F] 6\\R\\=[R[U^5\\N]T]L^M` L]*] 0]:] 8]1^+]P]4]7_1[L_1]ZM];].] R` P`.]2]QfXaN]G]B]L^=^L]C]K_B].]+" +"_J]F]H]C].].]M^?].]C]H]La@^M^C]+^M^C]J]B]L^7]4^N^:a4aMaK^M^8]7^.]*^.] Q]P`>`Q^=^NZ;^Q`>_LZ>].^Q`?]L]?].].]Q^;].]M]M]N]L]>^P^>`Q^=^Q`?]/ZL];]-^Q`:a4`" +"P`D^Q^7a8^&]4](] S]Sb>_P^@]R^7^P^>^MZ<]L]9a9]M]=_P`XZB]Q_Q]G^V]V^>]P]=^U]U^?`P^>^P^6]4]Q^?]G]A^0]*^O^]P`>]P`>]P`>]P`>]P`>]P]X^LZN^NZ;_LZ>_LZ>_LZ>_LZ?].].].]-^P^>]L]>^P^=^P^=^P^=^P^=^P^2^:^P^=^Q`>^Q`>^Q`>^Q`:a7`Q^9a Dk],a " +"H]-] /[,[._0_;]L]=j<]N]7`5a J_S^ F] 6\\R\\=^U[W_5]N^V^K_Rd L],] /]:] 8]1])^T^3]8_0^Q`0]<]Q_8^S^8^3_R_=]R^:].] O] P]+]1\\PdW`N^G^C]N_;`R`C]NaA].]*`O" +"`F]H]C].].]L^@].]C]H]La?`S`B]*`S`B]J]B`Q_6]3_R_9a4aMaL^K^9]8^-])].] Q_Tb>aS^;_R\\:^Sa=`Q]>]-^Sa?]L]?].].]P^<].]M]M]N]L]=_T_=aS^;^Sa?]/^R_:]-^Sa:a3_P_" +"C^P^7_8^%]4](] S_V^X^?aS^>]T^5_T_=`R]<]L]8_8]M^>`SdA]SaS]E^W]W^=]P^=_W]W_>]X]T_<_T_5^4^T^?^G^C^/])^Q^8c=]H]7]6`S` ?] ;c >c E]._W[V\\9]4^J^9]4]%] ;]L]" +" IZQZ H]L] !u ,`Sd9\\U\\ ,ZQZ,]E\\E]L]?]E\\M_S^>^G^F^G^F^G^F^G^F^G^F^G^F^K]:`R`C].].].].].].].]-]ObB]La?`S`>`S`>`S`>`S`>`S`?]J]CcS`?_R_=_R_=_R_=_R_8]6" +"].]V[R^?_Tb>_Tb>_Tb>_Tb>_Tb>_Tb>_T^V_Q]M_R\\:`Q]=`Q]=`Q]=`Q]?].].].],_T_=]L]=_T_;_T_;_T_;_T_;_T_1^:`T_;^Sa=^Sa=^Sa=^Sa9_6aS^7_ Bi:i:i:i:i=]+` I],] /[" +",[-].]:]L]]C]H]K`>kA])kA]J^Cm5" +"]2j7_2`M`K^J]9]8tC])].] PgX]>]Xf9h9fX]],fX]?]L]?].].]O^=].]M]M]N]L]qA^U]W]U^D" +"i<]O`?k=]Xg:h3a7f>uCn?]/eSe;]:]H]7]5k >] :a n?\\H\\8]4]%] 9^R^ *^R^ Xu ,q9\\U\\ /]D\\F]LfH]D\\Li>]E]F]E]F]E]F]E]F]E]F]E]F]JnIkBn?n?n?n?].].]." +"]-n@]K`>ki-]]C]H]K`]Wd6f8dW]:i>]+dW]?]L]?].].]N^>].]M]M]N]L];f;]Wd7dW]?]/i7c3dV]9_2_P_E^M^8_8m4]4](] QdV`B]Xe;d1f8h<]L]8_9]K]>]XdW_@eWeBg;]O" +"`=g;]Vd8f1`6d=uCn?]/eSe;]:]H]7]3g <] 9_ :_ C]+f>n>ZFZ7]4]%] 7f &f Vu ,]XdW_9\\U\\ /\\C\\F\\KfH\\C\\Kg=]E]F]E]F]E]F]E]F]E]F]E]F]JnHh@n?n?n?n?].].].]-l>" +"]K`]C]H]J_9a<]$d?]I^?c0].b3_2" +"_K_M^G^;]8tC](]/] M`T]>]U`2b4`U]7c;])`U]?]L]?].].]M^?].]M]M]N]L]8`8]U`3`U]?],c2a0_T]9_2^N^F^K^8]7m4]4](] O`R^B]Va8b-`3d:]L]7]9^J]?]V`T]>cUc?c9]N_:" +"a8]T`3`-_4`X IX *W FW " +" " +" " +" HX W 4Z 3VCT X W 4Z " +" HX W 4Z 'VCT ;X W 3Y 2UCT KX W 3Y 0W " +" " +" " +" @W !W 4\\ 5YET ?XHX 8] >W !W 4\\ 7XGX KW !W 4\\ 7XHX +YET :W !W 3[ 5ZFT ?XGX EW !W 3[ 7XGX 5W " +" " +" " +" >W \"V 3\\ 7]HU ?XHX 9` ?W \"" +"V 3\\ 7XGX JW \"V 3\\ 7XHX -]HU 9W \"V 3] 7]HT ?XGX DW \"V 3] 8XGX 5V " +" " +" " +" W $V 3VNV 8XGX IW $V 3VNV 8XHX -_KV 8W $V 2] 7_KU ?XGX CW $V " +"2] 8XGX 6V " +" " +" :W &W " +"4VLV :j >XHX :VJV >W &W 4VLV 9XGX HW &W 4VLV 9XHX .j 6W &W 3VMV 9i >XGX BW &W 3VMV 9XGX 7W MW " +" " +" " +" CV 'W 4VJV ;j >XHX ;UGV >V 'W 4VJV :XGX GV 'W 4VJV :XHX .j" +" 5V 'W 3VKV :i >XGX AV 'W 3VKV :XGX 8W N[ " +" " +" " +" DV )W 4VHU TEY ;XHX V ,V 2UEU TCU :XGX =U -V 2UCU =XGX ;V NV" +"IV \"W " +" " +" JU /V 3VBV ETBT :U /" +"V 3VBV FU /V 3VBV (U /V 2UAU DU /V 2UAU @V NVGV " +" $X " +" *X " +" JX GTBT MX GX 7V :UEU DX GX 7V " +" JX GX 7W 4X GX 6V GX GX 5V (X &X " +" )X 8V " +" ;X FTBT " +" LX IX 7X W E\\ AW ,W ,W ,W ,W " +" HY GV +Y 4Z NX @X %W " +" DUDU =Y 7W KW 6Z 4XDT BTAT BW KW 6Z IW KW 6[ ,Y )XDT AW KW 5Z 4XDT " +" KW KW 4Z ,W BW 8V (S W H_ AW ,W ,W ,W ,W L] GV +] ;a " +" #[ F^ 8XGX +W BTEU " +" *R 9a :W MW 6\\ 6ZET ?XHX W Ja AW ,W ,W ,W ,W N_ GV +_ " +"?e 8] J] Jb 8[ <[ $Y FY 7XGX =Z Di 5W 8Z .Y !W FW *Y 4W)V*W)V-Y(V " +" W $a MY " +" EW 5W >W Kb AW ,W ,W ,W ,W !a GV +a Ch =f ^ Mf 2Z @x Mx a 5a &W 0g #\\ -_ <\\*V.\\*V0a-V\"X )Z /Z /Z /Z /Z 4WJV 1~U+d Kx Mx Mx Mx MX -X -X -X ,j" +" @[3X Dc 8c 8c 8c 8c W \"W 4VNV 8]HU ?XHX " +"BW \"W 3VNV 8XHX 2W ?W &XHX ^ K~\\ >S 3Q +[ @[;[ ;Q ;e HX 2VFV #VBV FS 6`1V#g GV !V 3V !T 7W 0d" +" :` ;j ?k -[ Dq :g Ky Df ;d $f 1Z @o 5j Np Ex Mt :m\"X/X'X -X -X3Z%X -]0]0\\4X Gi Lm 4i Ln ;m#~W$X/X-X(X-X4Y4XCY1Y-Y.Y&~S%a >W $a N[ EV " +"5W >W Lc AW ,W ,W ,W ,W \"b GV +a Dk Aj \"_ h 3Z @x Mx ?i 6X C~Q)X?X?X Ni 6V /V /" +"V DX &f #W0W e >XGX %c#e +b\"i 9_ Be 9d 'V 3k %^ /c @^*V0^*V2d.V\"X )Z /Z /Z /Z /Z 3b 1~U.j Nx Mx Mx Mx MX -X -X -X ,p F\\4X Gi >i " +">i >i >i BiEV.X/X'X/X'X/X'X/X.Y.Y#X 'j ;V \"V 5VLV :_IT >XHX V \"V 5VLV 9XGX IV \"V 4VMV 9XGX ,ZHY A_IT XHX AV \"V 3VLV 9" +"XHX 2V >W &XHX !_ K~[ >T 4R -_ D_?_ >S =t Fh IX 2VFV #VBV FS 7c4V#i HV \"W 3V !T 7V 0f @e >o Co 0" +"\\ Dq W M" +"d AW ,W ,W ,W ,W HW 1b GV +b Fm Dm #` \"j 4Z @x Mx Am 8X C~Q)X?X?X!m 9X 0V 0X EX 'h" +" $W0W \"h ?XGX 'g%g 0h%i :a Cf :f *V 4m %^ 0e A^+V/^+V1f1V!X )Z /Z /Z /Z /Z 2` 1~V0o\"x Mx Mx Mx MX -X -X -X ,t J\\4X Im Bm Bm Bm Bm F" +"mHV-X/X'X/X'X/X'X/X-X.X\"X (l ;V $V 4UJU :ULXLU >XHX XHX @V $V 2UJU 9XHX 3V" +" =W &XHX !` K~Z >T 4S /a FaAa @T @w Hl KX 2VFV $WCV ES 8e5V$j HV \"V 1V \"T 7V 2j Eh ?q Dp 1\\ Dq >" +"l Ly Hn Bj +l %e E\\ At >s$v Kx Mt >u&X/X'X -X -X5Z#X -^2^0]5X Jo q ;o r Br%~W$X/X-X(X,X6[6XAY3Y+Y0Y%~S%W 3V IW !_ FW 7W >W Md AW " +",W ,W ,W ,W HW 2[ ?V #[ Hn En #` #l 6\\ Ax Mx Cp 9X C~Q)X?X?X\"o ;Z 1V 1Z FX KS 0i #W2" +"W LV ,i ?XGX *l'h 3l'i ;c Dg ;g ,W 6o %^ 1g B^,V.^,V0g3V X *\\ 1\\ 1\\ 1\\ 1\\ 2^ 0~V2s$x Mx Mx Mx MX -X -X -X ,v L]5X Jo Do Do Do Do HpKW" +"-X/X'X/X'X/X'X/X-Y0Y\"X )n XHX ;UEU XHX @W &W 3VJV :XHX 4W =W &XHX " +" 1\\ 1\\ 1\\ 1\\ 1\\ =XMV K~Y =S 4U 1c IdCc AU Dz In LX 2VFV $VBV ES 9g7V$k HV #W 1W #T 8W 3l Fh ?r Eq 3] Dq ?m L" +"y Ip Em -n )k H\\ Au Av%x Mx Mt ?x(X/X'X -X -X6Z\"X -^2^0]5X Ls\"s ?s\"s Et%~W$X/X,X*X+X6[6X@Y5Y)Y2Y$~S%W 3W JW \"a FW 8W >W NZ 6W ,W " +",W ,W ,W HW 2X \\ 2V 2\\ GX KS 1j #" +"W2W LV -j ?XGX +ZEZ)VGY 5ZDZ)i T 5V 2e KfEe CW G| Jp MX 2VFV $VBV ES 9XIX8V$l HV #V /V #T " +" 8V 3n Gh ?s Fr 5^ Dq @n Lx Ir Go .o -q L^ Bv Cx&z x Mt A{)X/X'X -X -X7Z!X -^2^0^6X Mu#t Au#t Gu%~W$X/X,X*X+X6[6X?X5X'X2X#~S%W 2V JW #c FW" +" 9W >W NX 4W ,W ,W ,W ,W HW 2W ;V NW IZCY Hp JY &ZDZ 9^ Bx Mx Eu W *W 2UFU ;XHX 6W ;W &XHX 7h =h =h =h =h DWJV K~X >T 5W 4g MgFg EY J~ K]FZ MX 2VFV $VBV " +"ES :XGX9V%\\GX HV $W /W 3PATAP GV 3[H[ Gh ?]F] GZE^ 6^ Dq A]FX Lx I\\F\\ G\\G[ /[H] 0u N^ Bw E_D^&{!x Mt B`C_)X/X'X -X -X8Z X -_4_0_7X N^" +"E^$u C^E^$u H^E\\%~W$X/X,Y,Y*W7]8X>Y7Y'Y4Y#~S%W 2V JW $e FV 9W >W NW 3W ,W ,W ,W ,W HW 2W ;V NW IY@X >X " +"4[AV IX &X@X 9^ Bx Mx F^E^ =X C~Q)X?X?X&^E^ B` 4V 4` IX KS 3\\GW \"W4W KV .YBT ?XGX .V7V,P=W :W8W /VEV 3V +V /V " +" 7eGU KU 3WCW ;U-V$U-V LV5V NX +^ 3^ 3^ 3^ 3^ 3^ 1~W6_D^&x Mx Mx Mx MX -X -X -X ,{\"_7X N^E^ L^E^ L^E^ L^E^ L^E^ !^Ed*X/X'X/X'X/X'X/X+Y4Y X +Y?" +"X ;V *V 4UDU >TEZ TEZ T 5Y 5g MhHi G[ M~Q L\\AW MX 2VFV $VCV DS :WEW:V%ZAU HV $V -V 3RCTCR HW 4ZDZ H\\LX ?Y?[ HV>\\ 8_ DX )[?T -Y J[B" +"[ I[CZ 0WAZ 2x ^ BX>^ G]=Z&X=b#X -X '];[)X/X'X -X -X:[ NX -_4_0_7X \\?\\%X@^ E\\?\\%X?] J[=X =X W X 3W 4W ,W HW 3X ;V NX KY?X Ca 9Y:R HX (X>X :VNV BZ /X '\\?\\ A^ FX0X)X?X?X'\\?\\ " +" Db 5V 5b JX KS 3ZBT !W6W JV .X?R 4V4U HV ;V4V 1VCV 4V *U 0V 7fGU KU 4WAW TDX ;a 6V ,V 4UBU GV ,V 3UCU 0` 6TDX 4V ,V" +" 2UDU >TDX >V ,V 1UDU :V 9W (o Do Do Do Do GWIU J~V >T 6Z 6i jIj I\\ N~R M[=U MX 2VFV %VBV H] AWCW;V%Y=R" +" HV %W -V 4UETEU IV 4ZBZ IWGX ?V;[ IS9Z 9VNX DX *Z;R -X JZ>Y JZ?Y 1U>Z 5`C_#` CX;[ H[7W&X9_$X -X (\\6X)X/X'X -X -X;[ MX -_4_0`8X![;[&X" +"=[ F[;[&X<[ LZ8U =X W W 2W 4W ,W HW 3W :V MW KX=W Cc " +";X7P HX (WR !X8X JV /X

W W " +" 2W 4W ,W HW 3W :V MW KWU.U 4VAV &V 5U *U 2V 6gGU KU 5W?W =U/V\"U/V IU7V LX ,WNW 5WNW 5WNW 5WNW 5WNW 5WNW 4XHX H[4U&X -X -" +"X -X -X -X -X -X ,X6]&`8X\"Z7Z#Z7Z#Z7Z#Z7Z#Z7Z 'Z8['X/X'X/X'X/X'X/X)Y8Y MX ,W:W 9V 0V 3U@U ?[ 1V 0V 3U@V GV 0V 3U?U 8h 1V 0V 2U@U " +" CV 0V 1U@U >V 7W *`L` I`L` I`L` I`L` I`L` JV =X,X >T 6] 9k\"lKl K_ #\\ 'Y8S MX 2VFV %VBV Nk IVAV=V$X 1V %V +V " +"6YHTHY -V EW 5Y>Y :X ?R5Z .Y ;VMX DX +Y DX IYW W 2W 4W ,W HW 3W :V MW KW;W De =W " +" -X *W:W V$X 1V &W +W 5XITIX +V EV 4X[ JX -XNW8WNX0a9X#Y3Y(X9Y JY3Y(X9Y NX LX W W 2W 4W ,W HW " +" 3W :V MW LX;W Df >W ,W +W8W >WLW @Y 2X +Z3Z!t\"X0X)X?X?X*Y3Y Kj 9V 9j AS 5X 8W:W HV /W #T)T KV " +" @T(T 6U?U &V 5T +V AhGU KU 5V=V =U0V!U0V JV7V WLW 7WLW 7WLW 7WLW 7WLW 7XNX 6XGX IY.R&X -X -X -X -X -X -X -X ,X2Z'a9X#Y3Y%Y3Y%Y3Y%Y3Y%Y3" +"Y )Y3Z)X/X'X/X'X/X'X/X'X:X Ki >W8V *XHZ FW ,ZW W " +" 2W 4W ,W HW 3W :V MW LW:W Dg ?W ,X ,W8W >WLW ?Y 3X +Y1Y\"v#X0X)X?X?X+Y1Y MYNVNY :V :" +"YNVNY BS 5X 8XU1V U1V KW7V NWLW 7WLW 7WLW 7WLW 7WLW 7WLW 6XGX JY,Q&X -X " +"-X -X -X -X -X -X ,X1Z(XNX:X$Y1Y'Y1Y'Y1Y'Y1Y'Y1Y P)P$Y3[)X/X'X/X'X/X'X/X'YVKX DX -X BX IX8X NX7W KP 1P =X Y *Z W 0W MW +ZAZ 0W >W W 2W 4W ,W HW 3W :V MW LW:W DSF[ @X -X " +" -X8W ?WJW ?Y 4X ,Y/Y%z%X0X)X?X?X,Y/Y YMVMY ;V ;YMVMY CS 5X 5P*Q JWU2V NU2V$_7V NXLX 9XLX 9XLX 9XLX 9XLX 8WLW 6XGX KY*P&X -X -X -X -X -X -X -X ,X0Z)XNX:X%Y/Y)Y/Y)Y/Y)Y/Y)Y/Y\"R+R&Y3]*X/X'X/X'X/X'X/X&Y>Y Jp EW:Y " +" +R@Y 7Q 2W .XEVFY\"X5Y\"X5Y\"X5Y\"X5Y NV ;X/X 0V 5T 8c ^ AW4W ?Z >W6W KY " +" \"Y 0X 2VFV &VCW#[LSKZ KV?V@V\"W 0V 'W )W 1XNTNX &V FW 6Y:Y X *Z NW 0W MW ,Z?Z 1W >W W 2W 4W ,W H" +"W 3W :V MW LW:W DPAY ?Y .W -W6W @WJW >Y 5X ,X-X&_MXM_&X0X)X?X?X,Y/Y !YLVLY " +"W FV /X 'TCfFT2i CUGfBT 9U?U &V 7U 5] >iGU KU 6V;V >U2V NU2V$]5V NWJW 9WJW 9WJW 9WJW 9WJW 9WJW 8XFX KY /X -X -X -X -X -X -X -X ,X" +"/Y)XMX;X%Y/Y)Y/Y)Y/Y)Y/Y)Y/Y#T-T'Y3]*X/X'X/X'X/X'X/X%X>X Ir GW=\\ GY 9S 3W /XDVDX$X2X$X2X$X" +"2X$X2X V ;X0X 0X 7T 8d X$X-WJW EX6X Y .X.Y)X -X -Y .X/X'X -X -XBZ EX -XLV:VLX0XMX;X&Y-Y+X7X NY-Y+X7X!X KX Z W FV .X (TDgFT3j CTFhDT 9U?U &V 8U 4\\ =iGU KU 6V" +";V >U3V MU3V#\\5V MWJW 9WJW 9WJW 9WJW 9WJW 9WJW 8XFX LY .X -X -X -X -X -X -X -X ,X.Y*XMX;X&Y-Y+Y-Y+Y-Y+Y-Y+Y-Y%V/V)Y3_+X/X'X/X'X/X'X/X%Y@Y Is HW?^ " +"?Z /Z /Z /Z /Z /Z /Z6Y NZ 0Z /Z /Z /Z 8Y 1Y 3Z /Z /Z /Z /Z 3ZCV 5WDX DXCVCW%X0W%X0W%X0W%X0W V :X1X 0X 7T 9f =k#~`\"h Cf " +"EW4W @\\ ?X8X LX !Y /X 2VFV 'VBV#XHSET KV?VAV!W 0V (W 'W .` \"V GW 5X8X W\"W.XJX" +" FX6X X -X.Y)X -X -X -X/X'X -X -XCZ DX -XLV:VLX0XLX^4WG_ 9` @WG^ 9^GW MWG\\ ;f Gm ^BV\"W:W 3X ?^ 0e AWG_ KV.X ?X Z 7X -X+X)\\HXH\\(X0X)X?X?X-X+X $YJVJY >V >YJVJY Ma =X 7V0V JW@W EV .Y *TEiET5k DTEiDT :VAV &V 9U 3_ ;W6W NiGU " +"KU 6V;V >U3V MU3V#_8V NXJX ;XJX ;XJX ;XJX ;XJX ;XJX :XEX LX -X -X -X -X -X -X -X -X ,X.Y*XLXa'b 7` 5` 5` 5` AW ,W ,W ,W DY EWG_ 9` 5` 5` 5` 5` (Z <`GV W6W MW6W MW6W MW6W#W1X NWG^ HW1X NWBVBW&W.W&WJP:PJW&W4PJW&W." +"W!V :X2X 0X 6S 8g >k#~`#j Fj GW4W @\\ >W8W LX X .X 2VFV 'VBV$XGSCR KV?VBV X 1V (W 'W ,\\ V GW 5X8X f CWIb =bIW MWI^ =j Im U4V LU4V\"`:V GX /WHW ;WHW ;WHW ;WHW ;WHW ;WHW :XEX MY -X -X -X -X -X -X -X -X ,X-Y+XKWf ;f ;f ;f ;f +Z >eJU NW6W MW6W MW6W MW6W\"W" +"2W MWIb IW2W NWAVAW(W,W(WJRU5V KU5V GXTKW)W4TKW)W+W\"V 9X3X 2X 5T :k ?i\"~`$m Jn IW4W A^ ?X:X MW " +" NY .X 2VFV 7~X2XFS VIV>X2YIY DYFY +Z JW .V NW 1Y3Y 1n DWLh Bm ChLW Gk Ll 6hLW MWKg HW ,W ,W;Y JW " +",WKfGg8WKg Cl FWLh ChLW MWK` @m Im Y =W6W JW-W&YJb }!WCWCW Hk Dx&{ W4W CWFW P JSCVAVDS :WEV $V W6W NiGU KU 6V" +";V BP>P /U5V KU5V EW=V FX 0XHX =XHX =XHX =XHX =XHX =XHX W:X MW NX -X 2VFV 7~X2WES WKX0XJX>X(Y)X,X7X!Y)X,X7X!Y LX VIV>X1YKY BXFX +Z IW .W " +" W 2Y1Y 2o EWMj Dn DjMW Hn Nl 7jMW MWLi IW ,W ,WW6W NiGU KU 6V;V BQ?Q 0U6V JU6V BU>V EX 0WFW =WFW =WFW =WFW =WFW =WFW X(Y)X.Y)X.Y)X.Y)X.Y)X%Z9Z*Y6WJX,X/X'X/X'X/X'X/X!XFX EX;Z LWDX ?o Do Do Do Do Do DoKn4n Cn Cn Cn Cn HW ,W ,W ,W %l HWLi En Cn Cn Cn Cn /Z Cs LW6W MW6" +"W MW6W MW6W!W4W LWMj LW4W W?V?V+W(V+WKXBXKV+W5XKV+W(V$W 8W4X 2X 5T ;n ?g!~_%p LZDZ JW4W A^ >W:W MW MX -X 2VFV 7~X2WES VJX0XIW>X(X" +"'X-X7X!X'X-X7X!Y LX VIV>X1YKY AXHX +Z HW -V W 3Y/Y 3p FWMk Fo EkMW Io Nl 8kMW MWMk JW ,W ,W=Y HW ,WMjJj:WMk Gp HWMk GkMW MWMb Bo Im \\>W0X=X LW5X u 6W :V MW EkJV Wj Fn CWMk\"\\6X =Z >W6W KW+W)[Ke\"}!WCWCW Jo Hz&{ W4W DWDW ;Y ;X /X'X.YBXBY+X0X)X?X?X/X'X#T HV IT " +":V ;T3T :V CV +o BX 6ZM`MZ GXFX CV *\\ 3SFW,S:V>V 0R@R KSBV@VDS 9e #V ?W \"V ?W6W NiGU KU 6V;V BR@R 1U6V JU6V BV?V EX 1XFX ?XFX ?XFX ?XFX" +" ?XFX ?XFW =XCX NX +X -X -X -X -X -X -X -X ,X+X,XIW>X(X'X/X'X/X'X/X'X/X'X%Z;Z)X5VHX-X/X'X/X'X/X'X/X XHX DX:Y LWEX >p Ep Ep Ep Ep Ep EpMp6o Do Do Do Do" +" HW ,W ,W ,W 'o IWMk Gp Ep Ep Ep Ep 0Z Ds KW6W MW6W MW6W MW6W!W5X LWMk MW5X V>V?W,V'W,VKZDYKW,V5YKW,V'W%W 8X5W 2X 4T ;o @g ~^%q NY@Y KW4W B`" +" ?XX -XJW@WJX0XIX?X(X'X-X7X!X'X-X8Y Y MX W/YMY @YJY +Y GW -V W 4X+X 4YE\\ FWNXG\\ H]EX F\\GXNW J\\F[ " +"GW ,\\GXNW MWNXG[ JW ,W ,W?Z GW ,WNXH[KXH[:WNXG[ H]H] IWNXG\\ I\\GXNW MWNXFQ C\\CW CW ,W6W!X6X NW?\\?W.X?X JW6W 1X 6W :V MW 9X=X\"[IZKW W=Y /W @m H]DV " +"CWNXG[\"\\6W =[ >W6W LW)W*ZJWKY\"}!WCWCW K\\H] J{&{ V3W DWDW :Y XCX NX +X -X -X -X -X -X -X -X ,X+X,XIX?" +"X(X'X/X'X/X'X/X'X/X'X$Z=Z(X6WHX-X/X'X/X'X/X'X/X YJY DX9Y MWEW =YE\\ EYE\\ EYE\\ EYE\\ EYE\\ EYE\\ EYE]N\\G[7]EX E\\F[ F\\F[ F\\F[ F\\F[ IW ,W ,W ,W (p IWNXG[ H]H" +"] G]H] G]H] G]H] G]H] 1Z E]H^ JW6W MW6W MW6W MW6W W6W KWNXG\\ MW6W NV>V>V,V&V,VJZFYIV,V6YIV,V&V%W 7W6X 3X LR:T ;q @e N~^&s!Y>Y LW4W B` >WXJX +Z GW -W !W 5X)X 5U>Z G_CZ I[>T FZC_ KZAZ HW -ZB_ " +"M^BZ KW ,W ,W@Z FW ,^CZMVCZ;^BZ IZBZ I_CZ IZC_ M^ 5YY .W AXJa IZW2W EWDW 9Y =X /X'X/YAXAY,X0X)X?X?X/X'X%X JV KX Z FU>Z FU>Z FU>Z FU>Z FU>Z FU>eBZ9[>T FZAZ HZAZ HZAZ HZAZ JW ,W ,W ,W )r J^BZ IZBZ GZBZ GZBZ GZBZ" +" GZBZ 1Z EZB[ JW6W MW6W MW6W MW6W W6W K_CZ MW6W V=V>V-V%V-VHZHYHV-V6YHV-V%V%W 7X7X 4X NU:T WX !Y 0Y BVDX Dk CXJc -X BX>X LX5Y MX -X Ee Le 3Z ?U=bKUCU6XDX IX9Y X +X+X+X -X /X +X/X" +"'X -X -XL[ Y J]?Y KY?] M] 4X8P CW ,W6W X8X MW?\\?W-XAX IW7X 3Y 5W :V MW =_C_(YBXLV NW?Z -W CXC\\ KY ,]@Y LW8X >] ?W6W LW)W,YHWHY MW=W JWCWCW MY>Y " +"L[B[ ;W >W2W FWBW 9Y >X 0X%X0X@X@X,X0X)X?X?X/X'X&Y JV KY =V >Y7Y =V CV .[HSFR BX 3t BWHW AV .WN\\ 9SFV)S;V?W 3UCU LSAV@VCS 7_ V BV LU ?W" +"6W MhGU KU 5W?W AUCU 4U8V HU8V ?UAV CX 2XDX AXDX AXDX AXDX AXDX AXDX @XBX NX +X -X -X -X -X -X -X -X ,X+X,XHX@X(X'X/X'X/X'X/X'X/X'X\"ZAZ&X8WFX-X/X'" +"X/X'X/X'X/X MXLX BX8X MWFW Y;Z:R GY=Y JY=Y JY=Y JY=Y KW ,W ,W ,W *]E[ J]@Y JY>Y IY>Y IY>Y IY>Y IY>Y 2Z FY>Y JW6W MW" +"6W MW6W MW6W W7X K]?Y NW7X V=V=U-V$U-VGZJYFU-V7YFU-V$U%W 7X8X &~X/X:T =t @c L~\\'v\"W:W LW4W CXNX ?X>X MV $x EX 2~X2WES :VDW" +"EV FZ :W #W 7XKTKX )V IV 4X4X >X !X 0Y BWDX Dm FXKf /Y AYBY KX5Y MX -X Gd ~X d 5Y ?V>dLUCU6WBW IX;Z Y +X+Y,X -X 0Y +X/X'X -X -XM[ ;X -XIWBWIX" +"0XGW@X)Y'Y.X8X!Y'Y.X9Y M] #X aEa)X@XNW NWA[ ,W DW?[ LX +[=X KW:X =] ?W6W MW'W-XGWGX MW=W JWCWCW MXZ W2W FWBW 9Z ?X" +" 0X%X0X@X@X,X0X(X@X@X/Y'Y(Y IV JY >V ?Y5Y >V CV .YFSDP BX 2q @XJX AV /WK[ :SFV)S;V@X 4VDV LSAV@VCS 6\\ MV CV KU ?W6W MhGU KU 4V?V @V" +"DV 5U9V GU9V >UBV BX 2WBW AWBW AWBW AWBW AWBW AXDX @XBX Y +X -X -X -X -X -X -X -X ,X+Y-XGW@X)Y'Y1Y'Y1Y'Y1Y'Y1Y'Y\"ZCZ&Y9WEY.X/X'X/X'X/X'X/X MYNY BX8Y N" +"WFW X NW $w DX $VBV#XFS :WFXEV H] ;W #W 9XITIX" +" +V JW 4X4X >X \"Y 3[ BWCX Dn GXLi 1X ?ZFZ JY7Z MX -X Je M~X Me 9Y >U?gMUCV7WBW IX>\\ NX *X*X,X -X 0X *X/X'X -X -XNZ 9X -XHVBVHX0XGXAX)X%X.X9Y!X%" +"X.X:Y La 'X _ @W6W MW'W.YGWFX NW=W JWCWCW NX:X NYW2W FWBW 8Z @X 0X%X0X@X@X,X0X(X@X@X" +"/X%X)Y HV IY ?V @Y3Y ?V CV /YES 6X 1\\H[ JcJc LV 0WI\\ =TFV)S;WAX 5WEW MTAVAWCS 3W 4~W.W KV ?W6W LgGU KU 4WAW @WEW 6U9V GU9V ?VBV BX 2" +"WBW AWBW AWBW AWBW AWBW AWBW AXAX X *X -X -X -X -X -X -X -X ,X*X-XGXAX)X%X1X%X1X%X1X%X1X%X!ZEZ%X9WCX.X/X'X/X'X/X'X/X LXNX AX7X NWFW !W ,W ,W ,W ,W ,W " +",]:X=Y .X9X LX9X LX9X LX9X LW ,W ,W ,W +Z=X K[x A` J~\\(y%W8W MW4W CXMW >W>W MV $x DX $VCV\"XFS 9XIXEV H_ X #Y ?g AVBX Do HXM" +"k 3Y >l HX7Z MX -X Me J~X Je =Y >V?hNUBU8XBX Ju MX *X*X,w Lq IX *~R'X -X -c 8X -XHVBVHX0XFWAX)X%X.X9Y!X%X.X;Z Ke ,X WNV MW" +"Ib +W EW;Y MW *Z;X KV:W =_ @W6W NW%W/XFWFX NW=W JWCWCW NW8X!Y:Y =W >| GW@W 8Y @X 0X%X1Y@X@Y-X0X(X@X@X/XImIX*Y GV HY @V AY1Y @V CV /XDS 6X 0YDY JdL" +"d LV 1WF[ >SFV'SW6W LgGU KU 3WCW ?XFX 7U:V FU:V >UBV AX 3XBX CXBX CXBX CXBX CXBX CXBX BXAw?X *w Lw Lw Lw " +"LX -X -X -X ,X*X-XFWAX)X%X1X%X1X%X1X%X1X%X ZGZ$X:WBX.X/X'X/X'X/X'X/X K` @X7X NWFW W ,W ,W ,W ,W ,W ,[8W=X -W7W LW7W LW7W LW7W LW ,W ,W ,W ,Y:X LZ;X M" +"Y:Y MY:Y MY:Y MY:Y MY:Y \"Y=\\ LW6W MW6W MW6W MW6W MW:W IZ9X NW:W NVV&V 4W:X %~X2TNVW \"W ;WFTFW -V JV 3X4X >X #Y ?f AWBX Dp IXNm 4X ` @W6W NW%W/WEWEW NW=W JW" +"CWCW X8X!X8X =W >| GW@W 7Y AX 0X%X1X?X?X-X0X(X@X@X/XImIX+Y FV GY AV BY/Y AV DX 1XCS 6X 0W@X KdLd LV 1VCZ ?SFV'S;WE[ 7XFX G~X .S@VBWAS @~W0W " +".P>W >W6W KfGU KU 3XEX >XFX 8U;V:W3U;VCZ9P>WCV:W/Y 3W@W CW@W CW@W CW@W CW@W CXBX CX@w?X *w Lw Lw Lw LX -X -X -X 5p9X-XFXBX)X%X1X%X1X%X1X%X1X%X N" +"ZIZ#X:VAX.X/X'X/X'X/X'X/X K` @X7X NWFW W ,W ,W ,W ,W ,W ,[8X?X -X7X NX7X NX7X NX7X MW ,W ,W ,W ,X9X LY9W MX8X MX8X MX8X MX8X MX8X \"X=] LW6W MW6W MW6" +"W MW6W MW:W IZ9X NW:W NVLuKU/VLuKU/VBaAU/V:YAU/V=X=U&V 4X;X %~X2RLW>T >{!z'~Z)}(W6W NW4W DXLX ?X@X MV KX ,X %VBV!YHS 8eEV" +" Ic ?W !W ;UETEU ,V KW 3X4X >X $Y >c ?WAX DWD^ JbG] 5X 9d DY9[ MX -X #d D~X Dd DY a AW6W NW%W0XEWEX W=W JWCWCW W6W!X8X =W >| HX@X 7Y BX 0X%X1X?X?X-X0" +"X(X@X@X/XImIX,Y EV FY BV CY-Y BV DX 1XCS 6X 1W>W KeNe LV 1VB[ ASFV'S;YI] 9YGY F~X .S@VDX@S @~W1V ,TEZ >W6W JeGU IX +U 2YIY T ?|\"}(~X)~(W6W NW4W DXKW >W@X MV KX ,X %VBV!ZIS 7cEV IYNZ8W 0W !W :RCTCR +V KW 3X4X >X %Y" +" =b >V@X DS=\\ K`C[ 6Y 8b BX9[ Nd A~X Ad HY W ,X8X8W=X8X X6X MY7X\"X7Y MX 0W )W ,W6W MXXMW AW6W NW%W0XEWDW W=W JWCWCW!X6X#X6X >W >| HW>W 6Y CX 0X%X1X?X?X-X0X'XAXAX.XImIX-Y DV EY CV DY+Y CV DX 2X" +"BS 6X 1Vh =W6W JeGU IX 4g :g :YFX DgEV:XhCV:X/X 3X?W EX?W EX?W EX?W EX?W EX@X EX?w?" +"X *w Lw Lw Lw LX -X -X -X 5p9X-XEXCX)X%X1X%X1X%X1X%X1X%X LZMZ!XX7X NWFY !V +V +V +V +V +V +Y6W@X ,W5W NW5W NW5W NW5W MW ,W ,W" +" ,W -X7X MX8X X6X X6X X6X X6X X6X $X=_ MW6W MW6W MW6W MW6W LWS >}%~R)~V(~P)W6W NW4W" +" DWJX ?XAW L~^ $X ,X %VCV N\\LS 6aDVAW0XLZ9W 0W !W :PATAP +V KV 2X4X >X &Z =e BW@X DP8[ L^?Z 7X :h EY;\\ \"d >~X ?e LY ;U@W>Y" +"AU:W>W Ks KX *X*X,w Lq IX6f+~R'X -X -b 7X -XGWFWGX0XDWCX)X%X.X@^ NX%X.s Bl 8X X IXDVCVDX)[ 4\\ -Z @W *V #W $W JX5W\"X -W5X W4W KW 0W5X MX7W" +" MW ,W ,WIZ =W ,X8X8W=X7W W4W MX5W\"W5X MX 0X *W ,W6W LWX >XMX BW6W W#W1WD" +"WDW W=W JWCWCW!W4W#X6X >W >| HW>W 7Y BX 0X%X1X?X?X-X0X'XAXAX.XImIX.Y CV DY DV EY)Y DV DX 2XBS 6X 2WY BSFV'S9bMV ;XFY D~X .S@h>S " +"@~W2i >g W EW>W EW>W EW>W EW>W EW>W EX?w?X *w Lw Lw Lw LX -X -X -X 5p9X-XDWCX)X%X1X%X1X%X1X%X1X%X " +"Ke X=W?X.X/X'X/X'X/X'X/X I\\ >X7X NWEY \"W ,W ,W ,W ,W ,W ,X5W@X -W4W W4W W4W W4W MW ,W ,W ,W -W6X MX7W W4W W4W W4W W4W W4W $W=VMW MW6W MW6W MW6W MW6W " +"LW=X HX5W NW=X MVLuKU/VLuKU/V?[>U/V=Y>U/V=X=U&V 3X=W 7X FW@T ?~&~T*~V)~R*W5V NW4W EXJX ?XBX L~^ $X ,X &VBV Mb 4]CVC]4XJZ:W" +" 0W !W +T KV KV 2X4X >X 'Z X Lu MX *X*X,w Lq IX6f+~R'X -X -c 8X -XFVFVFX0XDXDX)X%X.u MX%X.r" +" ?l :X X IXDVCVDX)\\ 4Z ,Y ?W *V #W $W JX5W\"W ,W5X W3W LW 0W5X MX7W MW ,W ,WJY ;W ,X8X8W=X7W W4W MX5W\"W5X MX 0X *W ,W6W LWW 6Y 0X 9V LX 5`3R 0T?[?T/W:[ KWId DbKW HW5X NW +X7W JV>W =WLX BW6W W#W1WDWDW W=W JWCWCW!W4W#W4W >W >| IX>X 9Y AX 0X%X1X?X?X-X0X'XAXAX.XImIX/Y B" +"V CY EV FY'Y EV DX 2WAS ?r CV:V =^ =V 2V=Y CSFV'S8`LV e :W6W GbGU IX 4g 8c 5XFX FgFV:YX GX>X GX>" +"X GX>X GX>X GX>X FX?w?X *w Lw Lw Lw LX -X -X -X 5p9X-XDXDX)X%X1X%X1X%X1X%X1X%X Jc NX>W>X.X/X'X/X'X/X'X/X HZ =X7X NWEZ #W ,W ,W ,W ,W ,W ,X4WAW ,W3W!W3" +"W!W3W!W3W NW ,W ,W ,W .X5W MX7W W4W W4W W4W W4W W4W $W>VLW MW6W MW6W MW6W MW6W KW>W GX5W MW>W LVLuKU/VLuKU/V>Z>U/V>Y=U/V=X=U&V 2W>X 8Y FW@T " +" ?~P(~V*~T(~Q)V4V NW4W EXJX >WBX L~^ $X ,X &VBV Ld 4WAVD`6XHZ;W 0W !W +T KV LW 2X4X >X 'Y ;i GV>X *Z M\\;Y 9X =p HZ?^ 'd " +" Id$Y 9UAWX GWEVJVEW#a >W>W 7Y 1Y 8V KY 9e8T 0T?Z>T0X:[ KWIf GdLW HW4W MW ,W6W JV?X >XKW BW6" +"W W#W2XDWDX!W=W JWCWCW!W4W#W4W >W >| IWX GX>w?X *w Lw Lw Lw LX -X -X -X 5p9X-XCWDX)X%X1X%X1X%", +// Start of second string. +"X1X%X1X%X Ia MX?W=X.X/X'X/X'X/X'X/X GX W GX5W MW>W LVLuKU/VLuKU/V?\\?U/V?YX 8X DWBT ?~Q)~W)~R&~(V4V NW4W EWHW >WBW K~^ $X ,X &VBV Kg \"" +"VEc8WFZ=W /W !W +T 4~W 5V 1X4X >X (Y -] IW>X )Y M[9X 9X >\\F\\ H[C` 'a Ca$Y 9UAV:WAU;WW )V $W 6i JX5X$X -X5X V2W LW 1W3W MW6W MW ,W ,WLY 9W ,W7W7W=W6W!X4X NX5X$X5X MW .[ .W ,W6W KW>" +"W FWEVJVEW#a >W?X 8Z 4\\ 8V K[ =iW2W IWX X *X -X -X -X -X -X -X -X ,X*X-XCXEX)X%X1X%X1X%X1X%X1X%X H_ LX@Wi >i >i >i" +" >i >i3WBX ,V2W!V2W!V2W!V2W NW ,W ,W ,W .W4W MW6W!X4X\"X4X\"X4X\"X4X\"X4X M~Y2X@VIW NW6W MW6W MW6W MW6W KW?X GX5X NW?X LVLuKU/VLuKU/V@^@U/V@Y;U/V=X=U&" +"V 2X?W 8X CWBT ?~R*~X)~Q%}(V4W W4W FXHX ?XDX K~^ $X ,X 'WCV Ii &VEe:XEZ>W /W !W +T 4~W 5V 1X4X >X )Y )[ KW=X (Y N[9Y ;Y " +"?Z@Z I]Gb '^ =^$X 9U@V:WAUXIW CW6W!W!W3WCWCW!W=W JWCWCW\"W2W%W3X ?W >W2W JW;X ~R+~Z*~P#{'V4W W4W FXHX ?XDX K~^ $X " +" ,X 'VBV Gi (VFg;WCZ?W /W !W +T 4~W 6W 1X4X >X *Y &Z LW=X (Y NZ7X ;X ?Z>Z ImNX '[ 8\\%Y 9UAW:WAUX XIW CW6W!W!W3WCWCW!W=W JWCWCW\"W2W%W2W ?W >W2W JW:W =Y >X 0Y'" +"X0X?X?X-X0X%XCXCX,X%X2~a GV H~a HV I~b HV DX 3W@S ?r DV8V V&V 1XAW 9" +"X @WDT ?~S+~Z)}!y'W4W W4W FWFW >WDW J~^ *r ?V &VBV Eh *VEXIXX +Y $Z NWXHX DW6W!WW2W KX:X ?Y =X /X'X0Y@X@Y-X0X%YDXDY,X%X2~a GV H~a HV I~b HV DX 3W@S ?r DV8V ;X DW;V DSFV'S >XFX " +" ;V .S@VFW=S (V \"W6W :UGU IX 0XFX -V;TLU MV0U!V;TLU6Y 0X:X KX:X KX:X KX:X KX:X KX:X JWV&V 1XBX :X ?WDT ?~S,~[({ x&W4W W4W FWFX ?XFX JV \"q >V &VBV Af -VEXGX=W@ZB" +"W .W !W +T 4~W 5f 8V 0X4X >X ,Y \"Y W;X 'X NZ7X X -XDVJVDX0XAXGX)X%X.i" +" AX%X.X>Z ,\\ ?X XGW DW6W!WW2W KW9X ?Y =X /X'X/X@X@X,X0X$YEXEY" +"+X%X2~a GV H~a HV I~b HV DX 3W@S 6X 3V8V ;X DXWEW :V .TAVEW?T (V \"W6W :UGU IX /WEW .V;TKU NV/U\"V;TKU7Y /X:X KX:X KX:" +"X KX:X KX:X KX:X KXWDS >~T-~\\(y" +" Mw&W4W W4W GXFX ?XFX JV #r >V 'WCV X -Y Y!W;X 'Y Y5X =X @Y8Y HgKX 'a Ca%X 8UAV8" +"VAU=W8W NX4X%X *X+Y,X -X 0X(X+X/X'X -X -XI[ ?X -XDWLWDX0X@WGX)X&Y.X 0X&Y.X=Y *[ @X XFX EW6W!WW2W KW8W @Y ] Jt It It It It It I~iBW ,|\"|\"|\"| NW ,W ,W ,W /W2W NW6W!W2W\"W2W\"W2W\"W2W\"W2W M~Y2WCVEW NW6W MW6W MW6W MW6W IWCX EW3W L" +"WCX IV=V=V.V$V.VFYKZFV.VFY7V.V$V&V 0XCW ;Y =WFT >~T-~\\'w Ku%W4W W4W GXEW >WFW IV #q =V 6~X JSN^ /VEWCW?W=ZDW .W !W :~W" +" 5f 9V /X4X >X .Y MX\"W:X &X Y5X >Y @X6X FcJX &d Id%X 8UAV8VAU>X8X X4X$X +X+X+X -X /X)X+X/X'X -X -XH[ @X -XCVLVCX0X@XHX(X'X-X /X'X-XXFX EW6W!WV-V%V-VGYIZHV-VGY7V-V%V%V /WDX ;X ~T-~\\'v Is" +"$W4W W4W GWDX ?XGW HV %r =V 6~X JSJ[ 0VEVAV?WX ?X6X D`IX $d Ne#X 8UAV8" +"VBU=x X4X$X +X+X+X -X /X)X+X/X'X -X -XG[ AX -XCVLVCX0X?WHX(X'X-X /X'X-X;Y *Y @X WDW EW6W!WV>V,V&V,VIYGZIV,VIY6V,V&V&W /XEW N~X'VGT =~T-~\\&u Ir#W4W NV4W HXDX ?XHX HV KX ,V 6~X JSHZ 2VDVAV?W;ZGW -W !W \"V " +"Lf :W .X6X =X 0Z LY#~ /X NX5X >X @X5Y AYFX !d >~X >d X 8UAV8VBU>z!X3X%X +X+X+X -X /X)X+X/X'X -X -XF[ BX -XCWNWCX0X?XIX(X'X-X /X'X-X:X )Y AX XDX FW6W!WV?W,V'W,VJYEZKW,VJY6W,V'W&W /XFX N~X'WHT =~T-~\\%s Gp\"W4W NV4V GXCW >WH" +"X HW LX ,V 6~X JSGY 3VDWAW@W:ZIW ,W !W \"V Lf :W .X6X =X 1Z JX#~ /X NX5X ?Y @X4X .X Md A~X Ad LX 8UAV8VBU>z!X3X%X +X+X+" +"X -X /X)X+X/X'X -X -XE[ CX -XBVNVBX0X>WIX(X'X-X /X'X-X9X *Y AX Q.X $T>Z?T0W8W HW5W\"WWCX FW6W!WXFX >V ,SBVBWCS &V \"W6W :U" +"GU *m 8XFX .VWIX(X'X/X'X/X'X/X'X/X'X KZMZ XHW6X-X/X'X/X'X/X'X/X GX XIW GW LX ;~X JSFX 3VDV?V@W9ZJW +V \"W !V V -X6X =X 2Z IX#" +"~ /X NX5X ?X ?X4X .X Jd D~X Dd IX 8UAV8VCV>z!X3X%Y ,X,Y+X -X /Y*X+X/X'X -X -XD[ DX -XBVNVBX0X>XJX(Y)X,X /Y)X,X9Y *X AX XBW FW6W!WXJX" +"(Y)X.Y)X.Y)X.Y)X.Y)X KZKZ!YJW6X,X/X'X/X'X/X'X/X GX |\"X3X$X ,X,X*X -X .X*X+X/X'X -X -XC[ EX" +" -XA\\AX0X=WJX'X)X,X .X)X,X8X *X AX XBX GW6W!WW 9X =\\KW >SEWWJX FW LX <~X JSEX 6WCV?V@W7ZMW *W #W !V !W -X6X =X 4Z GX#~ /X NX5X @X >X4X " +"/X De J~X Je DX 8U@V:WDV>|\"X3X$X ,X-Y*X -X .X*X+X/X'X -X -XB[ FX -XA\\AX0X=XKX'X*Y,X .X*Y,X8Y +X AX W WJW DW MX .VCV" +" :SDW 6VBV?V@W6b )W #W !V !V +X8X X4X /X Ad L~X Ld AX 8VAV:WDU=|\"X3X$Y -X-Y*X -X .Y+X+X/X'X -X -XA[ GX -XA\\AX0XWKVDVKW\"XLX 9WJW =Z #X :V MX AUEVKVDU/X:Y IW5W#WX@W GW6W!W=Y=W2WDWDW W=W JWCWCW\"X4W#W4W >W X4X 0X =d ~X" +" d LUAWX2X#X3X#X -X.Y)X -X -X+X+X/X'X -X -X@[ HX -X@Z@X0XW ,W7W7W=W6W W4W MX5W\"W5X MW BX FW ,W7X FWHW >WLVBVLW#YKX :WJW =Y !W :V MW @VHXJWHV-W:Y IW5W#WY>W1WDWDW W=W JWCWCW\"X4W#W4W >W W MW7X MW7X " +"MW7X MW7X EWJW AX5W GWJW AXCVCW%X0W%X0W%X0W%X0W\"V +WJX ?X 2WLT 9bKQKb)gLQMh Mi =g MW4W MV6W IX@X ?XLX CW MX 0VBV :SDW " +"7VAV?V@X5_ (W #W !V \"W +X8X XLV;VLX1Y?Y >X 9Z 2W %W )W EW7X JX5W\"X -W5X X )W 0X7Y MW6W MW ,W ,WFY ?W ,W7W7W=W6W W4W MX5W\"W5X MW AW FW ,W7X FXJX" +" =WMVBVMW#YJY ;WKX >Y W :V MW ?dId,W;Z IW5W#W=W DW4W!W )W6W DVKW >X>W HW6W W>Y>W1WDWDW W=W JWCWDX\"X4W#W4W >W ;V7W LX2X LY 4X *X1X%]JXJ]'X0X Hj L" +"Y-Y%Y IV JY LYKVKY MY5Y MYJVJY $X 2XBS 6X 2q 9X :V #\\ 7TDgFT /XFX EV )TFV>VJT #V \"W6W :UGU +XFX *V=TCU%V1V!V=TCU=X ,X1W$X1W$X1W" +"$X1W$X1W$X2X%X7X LY .X -X -X -X -X -X -X -X ,X.Y*X;XMX&Y-Y+Y-Y+Y-Y+Y-Y+Y-Y ZAZ$_3Y*X1X%X1X%X1X%X1X FX W3W$W7X MW7X MW7X MW7X MW7X MW7X MW7Z NX -X " +"-X -X -X +W ,W ,W ,W .W4W MW6W W4W W4W W4W W4W W4W 5Z IWMV=W MW7X MW7X MW7X MW7X EWKX AX5W GWKX @XDVDX$X2X$X2X$X2X$X2X\"V +XKW ?X 1WMT 7`JQKa" +"'fLQLf Kg W >WLW BX NY 1VBV :SDW 8V@V?V?W4] &V $W V \"V *Y:Y YGW>X0X$X4Y\"Y /X/Y(X -X ,Y-X+X/X'X -X -X>[ JX -X@Z@X0X;XMX%Y/Y*X ,Y/Y*X6Y -X AX ;Y3Y IXLX =WLV;VLW0X=Y ?X :Z 1W $V )W EW8Y JY7X\"X -X7Y X " +")W 0X7Y MW6W MW ,W ,WEY @W ,W7W7W=W6W X6X MY7X\"X7Y MW AW FW ,X8X EWJW Y NW :V MW >bGc,W;[ JW6X#W=W DX6X!W )W6W DVLX >W=X IW7" +"X W>Y>W1XEWEX W=W IWDWDW!Y6X#X6X >W ;W8W MX0X MY 4X *Y3Y$^LXL^&X0X Ff IY/Y#Y JV KY JYLVLY KY7Y KYKVKY #X 2XBS 6X 3t ;X :V ![ 8TCfFT .XFX FV )U" +"GV>WKT MW7X :UGU ,XFX *V=TBU&V2W!V=TBU=X -X0X&X0X&X0X&X0X&X0X&X0W%X7X KY /X -X -X -X -X -X -X -X ,X/Y)X;XMX%Y/Y)Y/Y)Y/Y)Y/Y)Y/Y Z?Z$" +"^4Y)Y3Y%Y3Y%Y3Y%Y3Y FX XEVFY\"X5Y\"X5Y\"X5Y\"X5Y!V *WLX @X /WNT 7`JQJ_&eKQKe Je :d KW4W MW8W HW>X ?XNX AX Y 1VCV 9SDW 9V?V?V?X4\\ " +" &W %W V \"V )X:X ;X 9Z CX 4X (Y KW7X AX W BW6W W )W6W DWMX ?X=X IX8X W?[?W0WEWEW NW=W IWDWDW!Y6W!W6W =W ;W8W MX0X NY 3X )Y5Y\"z%X0X C` FY/Y\"X JV " +"KX HYMVMY IX7X IYLVLY \"X 1XCS 6X 4v X ?XNX AY Y4P VBV 9SDW 9V?V?V?Y4Z %W %W V #W )X:X ;X :Z CY 4X (Y KX9Y AX ;X6X 1Y 1e /e @U@XB[JXW BX8X W )W6W CVNX >W;W IX8X X@[@X0XFWEW " +"NW=W IWDWEX!Z8X!X8X =W :W:W LX0X Y 2X (Y7Y Nv#X0X ?X AY1Y V IV JV FYNVNY GV5V GYMVMY !X 1XCS 6X 5x =X :V MZ 8T?ZBT *VDV FV 'T&T KX" +"8X :UGU ,VDV )VWNX @Y !Z6Q VBV KP>SEW 9V>WAW>X3Z &W %W V " +" #V 'XU?ZH^MZ\\ JX8X\"W?W AX9Y X *W6W CVNX ?X;X JX9Y NW@[@W/XFWFX NW=W IXEWEX!Z8X!X8W ;W ;W;X MX.X\"Y 1X 'Y9Y Lt\"X0X ?X @Y3Y MT HV IT Dj ET3T EYNVN" +"Y X 0XDS 6X 6ZM`LY >X :V LY 7T)T (UCU ET(T JX9Y :UGU ,UCU )V;m.V3V NV;mCY7P HX.X(X.X(X.X(X.X(X.X(X.X(X6X IY.R&X -X -X -X -" +"X -X -X -X ,X2Z'X9a$Z3Y&Z3Y&Z3Y&Z3Y&Z3Y!Z9Z&Z3Y&Y5Y#Y5Y#Y5Y#Y5Y EX `" +" >Y !Y8S MX +VBV KQ?SFX 9V=VAV=Y6] &V &W NV BX 1X 1V 'Y>Y :X X:W JY;Z NXB]BX.XGWGX MW=W H" +"XFWFX [:X NX:X ;W :WX HXX 9X =Z 1P2Z 3X GQ5Z GX=Y @X 9Y:Y KP8Z GX -X 4^ 1^ +X 5U?gM_9W,W%X7Z L[4U&X6]%X -X )[2X+X/X'X -X -X9[ X -X&X0X8`\"Z7Z'X )Z7Z'X3X%T2Y ?X 9Z9Z E` :" +"_9_3Y7Y BX >Z -W #W +W DX=\\ J\\=Y LY7P HY=\\ LY5R JW -Y?] MW6W MW ,W ,W@Y EW ,W7W7W=W6W MYX LX.X#Y 0X %Y=Z Gl MX0X ?X ?Z7Z JP FV GP @f AP/P Ah MX " +"/YFSDP BX 8ZFVEY @X :V JX 7V.U %SAS CU.U HZ\\=Y B^ 7r Gr Gr Gr Gr KV (_ BX )Y S 8RBSCR <] 2\\ GW4W KZBZ HX;W >_ <[ " +" $[=U MX ,VBV JUCSHY :V;WCW<[Z 0R5Z 2X GT9[ GY?Z AY 9[>[ KR;Z FX -X 1[ 1[ (X 5V>dL^9X,X&X9[ J[7W&X9_$X " +"-X (\\6Z+X/X'X -X -X8[!X -X&X0X8`![;[&X ([;[&X3Y&W7[ ?X 8Z;Z D` :^7^3X5Y CX ?Z ,W #W +W DY?] J]?Y KZ:R GY?] LZ8T JW -ZA^ MW6W MW ,W ,W?Y FW ,W7W7" +"W=W6W LY>Y J]?Y KY?] MW /T9X DX ,Y@] CWNW 9]>]'Y@Y =^ AY IW :V MW HYCXNW L\\>Y VAX >Y>Y LY ,W6W B] >X9X K[>[ MXDVMVDX,YIWIY LW=W GYHWHY N]>Y LY" +">Y :X :X@X LX,X%Y /X $ZAZ Ch KX0X ?X >[;[ ?V 6d >f LX /[HSFR BX 9Z3Y AX :V IX 7V1V #R@R BU0U G[>[ :UGU ,R@R 'V(U)V6W" +" LV(UU IX,X*X,X*X,X*X,X*X,X*X,X*W4X G[7W&X -X -X -X -X -X -X -X ,X9_%X8`![;[![;[![;[![;[![;[\"Z3Z(];[\"Z;Z NZ;Z NZ;Z NZ;Z CX Y JW6W LY>Y IY>Y IY>Y IY>Y IY>Y 2Z FY>Y HY@] KY@] KY@] KY@] B^ >]?Y A^ 6o Do Do Do " +"Do IV (_ CX (Y S (S ,[ 0[ GW4W J\\H\\ GW:W >^ :\\ %[@W MX ,VBV JXFSIZ :V:WEW:\\@e (V 'V MV BX 1X 2V $ZDZ 8X ?Z /U;] 2X GV=" +"\\ EZC[ @X 7[@[ JT?[ EX -X /Y 1Y &X 5V=bK\\7X,X&X<^ I]=Z&X=b#X -X ']:\\+X/X'X -X -X7[\"X -X&X0X7_ \\?\\%X '\\?\\%X2X&Z<\\ >X 7[?[ B^ 9^7^4Y5Y CX ?Y +W \"V +W " +" DZB_ J_CZ I[>T G[C_ K[=W JW ,\\GXNW MW6W MW ,W ,W>Y GW ,W7W7W=W6W KZBZ I_CZ J[C_ MW /W>Z DZ .ZB^ C` 8\\>\\&X>Y =\\ AY HW :V MW GZFYNY N]AZ N" +"WCX _ FX0X ?X =\\?\\ >V 5b W;[>T F[=W J[=W J[=W J[=W LW ,W ,W ,W *ZBZ IW6W KZBZ GZBZ " +"GZBZ GZBZ GZBZ 1Z F[BZ GZB^ KZB^ KZB^ KZB^ A\\ =_CZ ?\\ 3l Al Al Al Al HV (^ BX (X NS (S ,Z .Y FW4W In GX:X ?^ 9_ (]FZ MX " +",VBV J[ISL\\ :V9XGX9^Fi )W )W MV BX 1X 3W #[H[ Et Mx MZC_ 1X GZD^ C[G\\ @Y 7^F] IXF] DX -X ,V 1V #X 4V<^IY5X*X'y G_D^&{!y NX &`B`+X/X'X -X -X6[#" +"w LX&X0X7_ N^E^$X &^E^$X2Y'^C^ =X 7^E^ B^ 8]7]4Y3Y DX @~U&W \"W ,W C\\HYNW JWNXG\\ H]EX F\\GXNW J]D[ JW +kMW MW6W MW ,W ,W=Y HW ,W7W7W=W6W K]H] IWNX" +"G\\ I\\GXNW MW /[E\\ Be 9[GXNW B^ 7\\>\\'XP @W8W 3~W :_GaKP @UGU ,P>P 'V&U+V6V KV&" +"U;]GZ JX*X,X*X,X*X,X*X,X*X,Y,Y,X4y7_D^&y Ny Ny Ny NX -X -X -X ,{\"X7_ N^E^ L^E^ L^E^ L^E^ L^E^ MV/V(dE^ N^E^ L^E^ L^E^ L^E^ BX \\ Av 6W :V MW FkL]$u LXGX 9p Hp EW6W A[ ?X6X LpN\\#" +"hKh)s JW<] Lu LWNm Hp 6` Bl K~W'x MX 1iEi HX CX0X ?X ;u X :V HW 3X=X )X\\ /c 8c 8c 8c 8c CV '\\ ?T %W U *T *W ,V DW4W Gj EW8W " +">\\ 5~P In LX -VBV Is 9V7g6qJZ *V )V LV BX 1X 3V !l Dt Mx Mt /X Gr ?m ?X 4r Hm BX -X &P 1P LX 3V 3X*X'w Cv%x My NX #x(X/X'X" +" -X -X4[%w LX&X0X5] Ls\"X $s\"X1Y(w ;X 5s ?\\ 7\\5\\5Y1Y EX @~U&W !V ,W BjLW JWMj Dn DjMW Hr JW )hLW MW6W MW ,W ,W;Y JW ,W7W7W=W6W In GWMj EjMW MW /p" +" ?d 8iLW B^ 6Z<[)Y:Y >Z @v 6W :V MW EiK]$t JYLZ 7n Fo EW6W A[ ?X5W LWNfM\\\"gKg'q IW<] Ks KWMk Fn 5` Aj J~W'x MX 1iEi HX CX0X ?X :s ;V 2\\ 6" +"^ HX +n Lz MR,R =X :V HW 1ZEZ %ZDZ 0~W :WNfM\\ @UGU !V%U,V6i/V%U9n JX*X,X*X,X*X,X*X,X*X,X*X-X3y5v%y Ny Ny Ny NX -X -X -X ," +"x NX5] Ls Hs Hs Hs Hs IR+R(WMs Js Hs Hs Hs @X R $V NU *U *U *U DW4W Fh DW8X ?\\ 4~ Hl KX -VBV Hp 8V5e4nGZ +W +W LV BX" +" 1X 3V j Ct Mx Mr -X Gq =j >Y 3p Gl AX -X 2X 3W 5X(X(u ?s$v Ky NX \"v'X/X'X -X -X3[&w LX&X0X5] Kq!X #p X0X(v :X 4p =\\ 7\\5\\6Y/Y FX @~U&W !V ,W " +" AhKW JWLh Bm ChLW Gq JW (eJW MW6W MW ,W ,W:Y KW ,W7W7W=W6W Hl FWLh ChLW MW /o >d 7gKW A\\ 5ZZ @v 6W :V MW DgI\\$s He 5l Dn EW6W @Y " +">W4X MWMeM\\!eIe%o HW<] Jq JWLi Dk 2_ @h J~Y(x MX 1iEi HX CX0X ?X 9q :V 1Z 4\\ GX *m Lz LP*P X X ?v 6W :V MW CeG[$r Fc 2h Am EW6W @Y ?X3W MWMdL\\ cGc#m GW;\\ Hm HWKg Ah /] ?f I~Y(x MX 1iEi HX CX0X ?X 7m 8V 0" +"X 2Z FX (j Kz AX :V HW -g Lh ,~W :WMdL\\ @UGU \"V$U-V5i0V$U7i HX(X.X(X.X(X.X(X.X(X.X(X/X2y1o\"y Ny Ny Ny NX -X -X -X ,t" +" JX4\\ Im Bm Bm Bm Bm %VHm Dm Bm Bm Bm =X eJW GeJW" +" GeJW GeJW ?X ;WJe 9X MW &Z =U W ,W *R &Q BW4W B` AW6W >[ /y Dd GX -VCV Af 5V2a.gBZ ,W -W KV CX 0X 4V " +" Kd @t Mx Km *X Ek 6d ;X .h Bh >X .X 1X 1W 7X(X(q 7j Np Ey NX Mm\"X/X'X -X -X1[(w LX&X0X4\\ Gi LX Ni LX/X$n 7X 0i 9Z 5[5[6Y-Y GX @~U&W V -W " +" >cIW JWIb k EW6W @Y ?" +"W2W MWK`I[ NaEa i EW;\\ Fi FWIc >e ,\\ =b G~Y(x MX 1iEi HX CX0X ?X 5i 6V /V 0X EX &f Iz AX :V /P;W *c Gb )~W :WK`I[ @UGU " +" #V#U.V4i1V#U6f FX(X.X(X.X(X.X(X.X(X.X(X/X2y/j Ny Ny Ny Ny NX -X -X -X ,p FX4\\ Gi >i >i >i >i $VEi @i >i >i >i ;X i0g ;i >i >i >i HW ,W ,W ,W #d BW6W Ef ;f ;f ;f ;f JUJe ;cIW FcIW FcIW FcIW ?X ;WIb 7X MW %Y =T X -X )P %P AW4W ?Z" +" >W6X ?Z ,w B` EX .VBV <] 1V0]*b?[ -W -W KV CW /X 4V I` >t Mx Hg 'X Bf 2` :X +d =b ;X .W 0X 1X 9X&X)m 0d Kj ?y NX Jg " +"NX/X'X -X -X0[)w LX&X0X3[ Dc IX Kf LX/Y!g 4X .e 7Z 5Z3Z7Y+Y HX @~U&W V -W =`GW JWG^ 7b 9^GW Ad CW \"YDW MW6W MW ,W ,W7Y NW ,W7W7W=W6W B` @WG^ 9" +"^GW MW (c 2] 3_GW @Z 3X:X*Y4Y @X ?v 6W :V MW ?_AW$WKb @^ +` 9g CW6W ?W ?X2X NWJ^GY K]B^ Ke CW:[ Dd CWG_ 9` 'Y ;^ F~[)x MX 1iEi HX CX0X ?X 2c " +"3V .T .V DX $b Gz AX :V /R>X &[ ?Z %~W :WJ^GY ?UGU #V +V +V 1b EX&X0X&X0X&X0X&X0X&X0Y'X1X1y,d Ky Ny Ny Ny NX -X -X " +"-X ,j @X3[ Dc 8c 8c 8c 8c !VBc ;e :e :e :e 9X Y BS .V,W#Z ;V -V 7W ;W EX ;\\ 6] " +"+Z 5\\ 5Z WGXBU FX=X E` \"W >] @WDY 3Z 2X C[ >T :[ KV /TAY " +" EWGXBU =UGU BT 6V +V +V ,Y ?\\ +[ 0[ 0[ 0[ 0[ KT=[ 2[ 0[ 0[ 0[ 7Z ;Y .Y .Y .Y .Y .Y -" +"Y2\\\"Z /\\ 1\\ 1\\ 1\\ CZ 3Z /Z /Z /Z /Z FVCZ 1Y .Y .Y .Y ,W :WDX 2W LW 7R #S" +" >W /W 8W :V \"W 5X )X &Z CW NV .W :W %W" +" @W :W -X -W :V MW LW FW ?W >W NW 0W =W 3S GV /XGZ " +" DW HUGU AT %T 'R JT " +" #T (X :W NX LW 7S =V /V 7W :V \"W" +" 4X'Q &Y %Z DW NV .W :W %W @W :W -W ,W :V MW " +" LW FW ?W >W NW 0W =W 3S GV /j CW HUGU @T " +" %T 'P HT \"Q 'W 9W NW KW " +" 7S =W 1W 7V :W \"V 2X)R &X #Z " +" EW NW /W :W %W @W :W -W ,X ;V NX LW FW ?W >W NW 0W =W " +" 3S GV /j CW HUGU @U &U " +" U \"P 'W 9W NW KV 6S " +" W NW 0W =W 3S GV /h " +" AW HUGU ?T %T NT " +" )X 9W X KV 6S W NW 0W =W 3S GV .f @W HUGU ?U &" +"U U *W 8W W JV " +" 6S ;V 3V 6V :W \"V .[5[ *Y Z " +" Ha (W :a W NW 0W =W " +" 3S GV +a >W HUGU >T %T " +" NT +X 8W !X (VIV 6S :V 5V 5U" +" 9W \"U +\\;] )X MZ Ia (W :a =Y %W ?W :W " +" /W )[ ?V #[ KW FW ?W >W NW 0W =W 3S GV 'Z ;W " +" HUGU >U &U U ,W 7W !" +"W 'VIV 6S :V 6W 6V 4V *_C` " +" )Y LZ Ja :a (P7Y $W ?W :W 0X (b GV +b JW FW ?W >W " +" NW 0W =W 3S GV 7W HUGU >U &U " +" U -X 7W \"X 'VJW " +" 6S 9V 7V 5U 3U 'x (Z KZ Ka :a " +" (R:Z $W ?W :W 0X (b GV +b JW FW ?W >W NW 0W =W 3S " +" GV 7W #U &U U " +" -X 7W \"X &UJW 6S 9W 9W " +" Bu ([ IZ La :a (T>[ $X ?W :W 1X &a GV +a " +" IW FW ?W >W NW 0W =W 3S GV 7W $V " +" 'V !V .X 6W #X %VLW " +" 5S 2p -a " +" 8XE] %Y >W :W 3Z $_ GV +_ GW FW ?W >W NW 0W =W " +" 3S GV 7W /QGW 2QGW ,QG" +"W 0Z 6W %Z %a 5S " +" 0l +a 8p +_ >W :W ;a !] G" +"V +] EW FW ?W >W NW 0W =W 3S GV 7W /` " +" 1` +` 7a 5W -a #` " +" >e '` " +" 7o *^ =W :W ;` KY GV +Y AW FW ?W >W NW 0W =W " +" 3S GV 7W /` 1` +` " +" 7` 4W -` \"_ " +" 8\\ #_ \"} 3n )^ =W :W ;` 9V " +" BW FW ?W >W NW 0W =W 'V 7W /_ " +" 0_ *_ 6` 4W -` !] " +" -] " +" } 3l '] W NW 0W =W " +" 'V 7W /^ /^ )^ " +" 5_ 3W -_ N[ " +" ,[ M} 2j &\\ ;W :W ;^ 7V BW " +" FW ?W >W NW 0W =W 7W -Y *Y " +" $Y 2^ 2W -^ LX " +" *X J} " +" /d #Z 9W :W ;\\ 5V BW FW ?W >W NW 0W =W " +" 7W " +" /\\ 0W HT " +" I} *[ NW 6W :W ;Z 3V BW FW ?W >W" +" NW 0W =W 7W " +" /Z .W " +" =} " +" " +" D" }; + + // Define a 40x38 'danger' color logo (used by cimg::dialog()). + const unsigned char logo40x38[4576] = { + 177,200,200,200,3,123,123,0,36,200,200,200,1,123,123,0,2,255,255,0,1,189,189,189,1,0,0,0,34,200,200,200, + 1,123,123,0,4,255,255,0,1,189,189,189,1,0,0,0,1,123,123,123,32,200,200,200,1,123,123,0,5,255,255,0,1,0,0, + 0,2,123,123,123,30,200,200,200,1,123,123,0,6,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,29,200,200,200, + 1,123,123,0,7,255,255,0,1,0,0,0,2,123,123,123,28,200,200,200,1,123,123,0,8,255,255,0,1,189,189,189,1,0,0,0, + 2,123,123,123,27,200,200,200,1,123,123,0,9,255,255,0,1,0,0,0,2,123,123,123,26,200,200,200,1,123,123,0,10,255, + 255,0,1,189,189,189,1,0,0,0,2,123,123,123,25,200,200,200,1,123,123,0,3,255,255,0,1,189,189,189,3,0,0,0,1,189, + 189,189,3,255,255,0,1,0,0,0,2,123,123,123,24,200,200,200,1,123,123,0,4,255,255,0,5,0,0,0,3,255,255,0,1,189, + 189,189,1,0,0,0,2,123,123,123,23,200,200,200,1,123,123,0,4,255,255,0,5,0,0,0,4,255,255,0,1,0,0,0,2,123,123,123, + 22,200,200,200,1,123,123,0,5,255,255,0,5,0,0,0,4,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,21,200,200,200, + 1,123,123,0,5,255,255,0,5,0,0,0,5,255,255,0,1,0,0,0,2,123,123,123,20,200,200,200,1,123,123,0,6,255,255,0,5,0,0, + 0,5,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,19,200,200,200,1,123,123,0,6,255,255,0,1,123,123,0,3,0,0,0,1, + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 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, + 200,200,200,91,49,124,118,124,71,32,124,95,49,56,114,52,82,121,0}; + + //! Get/set default output stream for the \CImg library messages. + /** + \param file Desired output stream. Set to \c 0 to get the currently used output stream only. + \return Currently used output stream. + **/ + inline std::FILE* output(std::FILE *file) { + cimg::mutex(1); + static std::FILE *res = stderr; + if (file) res = file; + cimg::mutex(1,0); + return res; + } + + // Return number of available CPU cores. + inline unsigned int nb_cpus() { + unsigned int res = 1; +#if cimg_OS==2 + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + res = (unsigned int)sysinfo.dwNumberOfProcessors; +#else + res = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN); +#endif + return res?res:1U; + } + + // Lock/unlock mutex for CImg multi-thread programming. + inline int mutex(const unsigned int n, const int lock_mode) { + switch (lock_mode) { + case 0 : cimg::Mutex_attr().unlock(n); return 0; + case 1 : cimg::Mutex_attr().lock(n); return 0; + default : return cimg::Mutex_attr().trylock(n); + } + } + + //! Display a warning message on the default output stream. + /** + \param format C-string containing the format of the message, as with std::printf(). + \note If configuration macro \c cimg_strict_warnings is set, this function throws a + \c CImgWarningException instead. + \warning As the first argument is a format string, it is highly recommended to write + \code + cimg::warn("%s",warning_message); + \endcode + instead of + \code + cimg::warn(warning_message); + \endcode + if \c warning_message can be arbitrary, to prevent nasty memory access. + **/ + inline void warn(const char *const format, ...) { + if (cimg::exception_mode()>=1) { + char message[16384] = { 0 }; + std::va_list ap; + va_start(ap,format); + cimg_vsnprintf(message,sizeof(message),format,ap); + va_end(ap); +#ifdef cimg_strict_warnings + throw CImgWarningException(message); +#else + std::fprintf(cimg::output(),"\n%s[CImg] *** Warning ***%s%s",cimg::t_red,cimg::t_normal,message); +#endif + } + } + + // Execute an external system command. + /** + \param command C-string containing the command line to execute. + \param module_name Module name. + \return Status value of the executed command, whose meaning is OS-dependent. + \note This function is similar to std::system() + but it does not open an extra console windows + on Windows-based systems. + **/ + inline int system(const char *const command, const char *const module_name=0) { + cimg::unused(module_name); +#ifdef cimg_no_system_calls + return -1; +#else +#if cimg_OS==1 + const unsigned int l = std::strlen(command); + if (l) { + char *const ncommand = new char[l+16]; + std::strncpy(ncommand,command,l); + std::strcpy(ncommand+l," 2> /dev/null"); // Make command silent. + const int out_val = std::system(ncommand); + delete[] ncommand; + return out_val; + } else return -1; +#elif cimg_OS==2 + PROCESS_INFORMATION pi; + STARTUPINFO si; + std::memset(&pi,0,sizeof(PROCESS_INFORMATION)); + std::memset(&si,0,sizeof(STARTUPINFO)); + GetStartupInfo(&si); + si.cb = sizeof(si); + si.wShowWindow = SW_HIDE; + si.dwFlags |= SW_HIDE | STARTF_USESHOWWINDOW; + const BOOL res = CreateProcess((LPCTSTR)module_name,(LPTSTR)command,0,0,FALSE,0,0,0,&si,&pi); + if (res) { + WaitForSingleObject(pi.hProcess, INFINITE); + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + return 0; + } else return std::system(command); +#endif +#endif + } + + //! Return a reference to a temporary variable of type T. + template + inline T& temporary(const T&) { + static T temp; + return temp; + } + + //! Exchange values of variables \c a and \c b. + template + inline void swap(T& a, T& b) { T t = a; a = b; b = t; } + + //! Exchange values of variables (\c a1,\c a2) and (\c b1,\c b2). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2) { + cimg::swap(a1,b1); cimg::swap(a2,b2); + } + + //! Exchange values of variables (\c a1,\c a2,\c a3) and (\c b1,\c b2,\c b3). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3) { + cimg::swap(a1,b1,a2,b2); cimg::swap(a3,b3); + } + + //! Exchange values of variables (\c a1,\c a2,...,\c a4) and (\c b1,\c b2,...,\c b4). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4) { + cimg::swap(a1,b1,a2,b2,a3,b3); cimg::swap(a4,b4); + } + + //! Exchange values of variables (\c a1,\c a2,...,\c a5) and (\c b1,\c b2,...,\c b5). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5) { + cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4); cimg::swap(a5,b5); + } + + //! Exchange values of variables (\c a1,\c a2,...,\c a6) and (\c b1,\c b2,...,\c b6). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6) { + cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5); cimg::swap(a6,b6); + } + + //! Exchange values of variables (\c a1,\c a2,...,\c a7) and (\c b1,\c b2,...,\c b7). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6, + T7& a7, T7& b7) { + cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6); cimg::swap(a7,b7); + } + + //! Exchange values of variables (\c a1,\c a2,...,\c a8) and (\c b1,\c b2,...,\c b8). + template + inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6, + T7& a7, T7& b7, T8& a8, T8& b8) { + cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6,a7,b7); cimg::swap(a8,b8); + } + + //! Return the endianness of the current architecture. + /** + \return \c false for Little Endian or \c true for Big Endian. + **/ + inline bool endianness() { + const int x = 1; + return ((unsigned char*)&x)[0]?false:true; + } + + //! Reverse endianness of all elements in a memory buffer. + /** + \param[in,out] buffer Memory buffer whose endianness must be reversed. + \param size Number of buffer elements to reverse. + **/ + template + inline void invert_endianness(T* const buffer, const unsigned long size) { + if (size) switch (sizeof(T)) { + case 1 : break; + case 2 : { for (unsigned short *ptr = (unsigned short*)buffer+size; ptr>(unsigned short*)buffer; ) { + const unsigned short val = *(--ptr); + *ptr = (unsigned short)((val>>8)|((val<<8))); + } + } break; + case 4 : { for (unsigned int *ptr = (unsigned int*)buffer+size; ptr>(unsigned int*)buffer; ) { + const unsigned int val = *(--ptr); + *ptr = (val>>24)|((val>>8)&0xff00)|((val<<8)&0xff0000)|(val<<24); + } + } break; + default : { for (T* ptr = buffer+size; ptr>buffer; ) { + unsigned char *pb = (unsigned char*)(--ptr), *pe = pb + sizeof(T); + for (int i = 0; i<(int)sizeof(T)/2; ++i) swap(*(pb++),*(--pe)); + } + } + } + } + + //! Reverse endianness of a single variable. + /** + \param[in,out] a Variable to reverse. + \return Reference to reversed variable. + **/ + template + inline T& invert_endianness(T& a) { + invert_endianness(&a,1); + return a; + } + + // Conversion functions to get more precision when trying to store unsigned ints values as floats. + inline unsigned int float2uint(const float f) { + int tmp = 0; + std::memcpy(&tmp,&f,sizeof(float)); + if (tmp>=0) return (unsigned int)f; + unsigned int u; + // use memcpy instead of assignment to avoid undesired optimizations by C++-compiler. + std::memcpy(&u,&f,sizeof(float)); + return ((u)<<1)>>1; // set sign bit to 0. + } + + inline float uint2float(const unsigned int u) { + if (u<(1U<<19)) return (float)u; // Consider safe storage of unsigned int as floats until 19bits (i.e 524287). + float f; + const unsigned int v = u|(1U<<(8*sizeof(unsigned int)-1)); // set sign bit to 1. + // use memcpy instead of simple assignment to avoid undesired optimizations by C++-compiler. + std::memcpy(&f,&v,sizeof(float)); + return f; + } + + //! Return the value of a system timer, with a millisecond precision. + /** + \note The timer does not necessarily starts from \c 0. + **/ + inline unsigned long time() { +#if cimg_OS==1 + struct timeval st_time; + gettimeofday(&st_time,0); + return (unsigned long)(st_time.tv_usec/1000 + st_time.tv_sec*1000); +#elif cimg_OS==2 + SYSTEMTIME st_time; + GetSystemTime(&st_time); + return (unsigned long)(st_time.wMilliseconds + 1000*(st_time.wSecond + 60*(st_time.wMinute + 60*st_time.wHour))); +#else + return 0; +#endif + } + + // Implement a tic/toc mechanism to display elapsed time of algorithms. + inline unsigned long tictoc(const bool is_tic); + + //! Start tic/toc timer for time measurement between code instructions. + /** + \return Current value of the timer (same value as time()). + **/ + inline unsigned long tic() { + return cimg::tictoc(true); + } + + //! End tic/toc timer and displays elapsed time from last call to tic(). + /** + \return Time elapsed (in ms) since last call to tic(). + **/ + inline unsigned long toc() { + return cimg::tictoc(false); + } + + //! Sleep for a given numbers of milliseconds. + /** + \param milliseconds Number of milliseconds to wait for. + \note This function frees the CPU ressources during the sleeping time. + It can be used to temporize your program properly, without wasting CPU time. + **/ + inline void sleep(const unsigned int milliseconds) { +#if cimg_OS==1 + struct timespec tv; + tv.tv_sec = milliseconds/1000; + tv.tv_nsec = (milliseconds%1000)*1000000; + nanosleep(&tv,0); +#elif cimg_OS==2 + Sleep(milliseconds); +#endif + } + + inline unsigned int _wait(const unsigned int milliseconds, unsigned long& timer) { + if (!timer) timer = cimg::time(); + const unsigned long current_time = cimg::time(); + if (current_time>=timer+milliseconds) { timer = current_time; return 0; } + const unsigned long time_diff = timer + milliseconds - current_time; + timer = current_time + time_diff; + cimg::sleep(time_diff); + return (unsigned int)time_diff; + } + + //! Wait for a given number of milliseconds since the last call to wait(). + /** + \param milliseconds Number of milliseconds to wait for. + \return Number of milliseconds elapsed since the last call to wait(). + \note Same as sleep() with a waiting time computed with regard to the last call + of wait(). It may be used to temporize your program properly, without wasting CPU time. + **/ + inline unsigned int wait(const unsigned int milliseconds) { + cimg::mutex(3); + static unsigned long timer = 0; + if (!timer) timer = cimg::time(); + cimg::mutex(3,0); + return _wait(milliseconds,timer); + } + + // Random number generators. + // CImg may use its own Random Number Generator (RNG) if configuration macro 'cimg_use_rng' is set. + // Use it for instance when you have to deal with concurrent threads trying to call std::srand() + // at the same time! +#ifdef cimg_use_rng + +#include + + // Use a custom RNG. + inline unsigned int _rand(const unsigned int seed=0, const bool set_seed=false) { + static unsigned long next = 0xB16B00B5; + cimg::mutex(4); + if (set_seed) next = (unsigned long)seed; + next = next*1103515245 + 12345U; + cimg::mutex(4,0); + return (unsigned int)(next&0xFFFFFFU); + } + + inline void srand() { + const unsigned int t = (unsigned int)cimg::time(); +#if cimg_OS==1 + cimg::_rand(t+(unsigned int)getpid(),true); +#elif cimg_OS==2 + cimg::_rand(t+(unsigned int)_getpid(),true); +#else + cimg::_rand(t,true); +#endif + } + + inline void srand(const unsigned int seed) { + _rand(seed,true); + } + + inline double rand() { + return cimg::_rand()/16777215.; + } + +#else + + // Use the system RNG. + inline void srand() { + const unsigned int t = (unsigned int)cimg::time(); +#if cimg_OS==1 + std::srand(t+(unsigned int)getpid()); +#elif cimg_OS==2 + std::srand(t+(unsigned int)_getpid()); +#else + std::srand(t); +#endif + } + + inline void srand(const unsigned int seed) { + std::srand(seed); + } + + //! Return a random variable between [0,1] with respect to an uniform distribution. + /** + **/ + inline double rand() { + return (double)std::rand()/RAND_MAX; + } +#endif + + //! Return a random variable between [-1,1] with respect to an uniform distribution. + /** + **/ + inline double crand() { + return 1-2*cimg::rand(); + } + + //! Return a random variable following a gaussian distribution and a standard deviation of 1. + /** + **/ + inline double grand() { + double x1, w; + do { + const double x2 = 2*cimg::rand() - 1.0; + x1 = 2*cimg::rand()-1.0; + w = x1*x1 + x2*x2; + } while (w<=0 || w>=1.0); + return x1*std::sqrt((-2*std::log(w))/w); + } + + //! Return a random variable following a Poisson distribution of parameter z. + /** + **/ + inline unsigned int prand(const double z) { + if (z<=1.0e-10) return 0; + if (z>100) return (unsigned int)((std::sqrt(z) * cimg::grand()) + z); + unsigned int k = 0; + const double y = std::exp(-z); + for (double s = 1.0; s>=y; ++k) s*=cimg::rand(); + return k-1; + } + + //! Bitwise-rotate value on the left. + template + inline T rol(const T a, const unsigned int n=1) { + return n?(T)((a<>((sizeof(T)<<3)-n))):a; + } + + inline float rol(const float a, const unsigned int n=1) { + return (float)rol((int)a,n); + } + + inline double rol(const double a, const unsigned int n=1) { + return (double)rol((long)a,n); + } + + //! Bitwise-rotate value on the right. + template + inline T ror(const T a, const unsigned int n=1) { + return n?(T)((a>>n)|(a<<((sizeof(T)<<3)-n))):a; + } + + inline float ror(const float a, const unsigned int n=1) { + return (float)ror((int)a,n); + } + + inline double ror(const double a, const unsigned int n=1) { + return (double)ror((long)a,n); + } + + //! Return absolute value of a value. + template + inline T abs(const T a) { + return a>=0?a:-a; + } + inline bool abs(const bool a) { + return a; + } + inline unsigned char abs(const unsigned char a) { + return a; + } + inline unsigned short abs(const unsigned short a) { + return a; + } + inline unsigned int abs(const unsigned int a) { + return a; + } + inline unsigned long abs(const unsigned long a) { + return a; + } + inline double abs(const double a) { + return std::fabs(a); + } + inline float abs(const float a) { + return (float)std::fabs((double)a); + } + inline int abs(const int a) { + return std::abs(a); + } + + //! Return square of a value. + template + inline T sqr(const T val) { + return val*val; + } + + //! Return 1 + log_10(x) of a value \c x. + inline int xln(const int x) { + return x>0?(int)(1+std::log10((double)x)):1; + } + + //! Return the minimum between two values. + template + inline typename cimg::superset::type min(const t1& a, const t2& b) { + typedef typename cimg::superset::type t1t2; + return (t1t2)(a<=b?a:b); + } + + //! Return the minimum between three values. + template + inline typename cimg::superset2::type min(const t1& a, const t2& b, const t3& c) { + typedef typename cimg::superset2::type t1t2t3; + return (t1t2t3)cimg::min(cimg::min(a,b),c); + } + + //! Return the minimum between four values. + template + inline typename cimg::superset3::type min(const t1& a, const t2& b, const t3& c, const t4& d) { + typedef typename cimg::superset3::type t1t2t3t4; + return (t1t2t3t4)cimg::min(cimg::min(a,b,c),d); + } + + //! Return the maximum between two values. + template + inline typename cimg::superset::type max(const t1& a, const t2& b) { + typedef typename cimg::superset::type t1t2; + return (t1t2)(a>=b?a:b); + } + + //! Return the maximum between three values. + template + inline typename cimg::superset2::type max(const t1& a, const t2& b, const t3& c) { + typedef typename cimg::superset2::type t1t2t3; + return (t1t2t3)cimg::max(cimg::max(a,b),c); + } + + //! Return the maximum between four values. + template + inline typename cimg::superset3::type max(const t1& a, const t2& b, const t3& c, const t4& d) { + typedef typename cimg::superset3::type t1t2t3t4; + return (t1t2t3t4)cimg::max(cimg::max(a,b,c),d); + } + + //! Return the sign of a value. + template + inline T sign(const T x) { + return (x<0)?(T)(-1):(x==0?(T)0:(T)1); + } + + //! Return the nearest power of 2 higher than given value. + template + inline unsigned long nearest_pow2(const T x) { + unsigned long i = 1; + while (x>i) i<<=1; + return i; + } + + //! Return the sinc of a given value. + inline double sinc(const double x) { + return x?std::sin(x)/x:1; + } + + //! Return the modulo of a value. + /** + \param x Input value. + \param m Modulo value. + \note This modulo function accepts negative and floating-points modulo numbers, as well as variables of any type. + **/ + template + inline T mod(const T& x, const T& m) { + const double dx = (double)x, dm = (double)m; + return (T)(dx - dm * std::floor(dx / dm)); + } + inline int mod(const bool x, const bool m) { + return m?(x?1:0):0; + } + inline int mod(const char x, const char m) { + return x>=0?x%m:(x%m?m+x%m:0); + } + inline int mod(const short x, const short m) { + return x>=0?x%m:(x%m?m+x%m:0); + } + inline int mod(const int x, const int m) { + return x>=0?x%m:(x%m?m+x%m:0); + } + inline int mod(const long x, const long m) { + return x>=0?x%m:(x%m?m+x%m:0); + } + inline int mod(const unsigned char x, const unsigned char m) { + return x%m; + } + inline int mod(const unsigned short x, const unsigned short m) { + return x%m; + } + inline int mod(const unsigned int x, const unsigned int m) { + return x%m; + } + inline int mod(const unsigned long x, const unsigned long m) { + return x%m; + } + + //! Return the min-mod of two values. + /** + \note minmod(\p a,\p b) is defined to be: + - minmod(\p a,\p b) = min(\p a,\p b), if \p a and \p b have the same sign. + - minmod(\p a,\p b) = 0, if \p a and \p b have different signs. + **/ + template + inline T minmod(const T a, const T b) { + return a*b<=0?0:(a>0?(a + inline T round(const T x, const double y=1, const int rounding_type=0) { + if (y<=0) return x; + const double sx = (double)x/y, floor = std::floor(sx), delta = sx - floor; + return (T)(y*(rounding_type<0?floor:rounding_type>0?std::ceil(sx):delta<0.5?floor:std::ceil(sx))); + } + + inline double _pythagore(double a, double b) { + const double absa = cimg::abs(a), absb = cimg::abs(b); + if (absa>absb) { const double tmp = absb/absa; return absa*std::sqrt(1.0+tmp*tmp); } + else { const double tmp = absa/absb; return absb==0?0:absb*std::sqrt(1.0+tmp*tmp); } + } + + inline bool _is_self_expr(const char *expression) { + if (!expression || *expression=='>' || *expression=='<') return false; + for (const char *s = expression; *s; ++s) + if ((*s=='i' || *s=='j') && (s[1]=='(' || s[1]=='[')) return true; + return false; + } + + //! Convert ascii character to lower case. + inline char uncase(const char x) { + return (char)((x<'A'||x>'Z')?x:x-'A'+'a'); + } + + //! Convert C-string to lower case. + inline void uncase(char *const str) { + if (str) for (char *ptr = str; *ptr; ++ptr) *ptr = uncase(*ptr); + } + + //! Read value in a C-string. + /** + \param str C-string containing the float value to read. + \return Read value. + \note Same as std::atof() extended to manage the retrieval of fractions from C-strings, + as in "1/2". + **/ + inline double atof(const char *const str) { + double x = 0, y = 1; + if (!str) return 0; else { std::sscanf(str,"%lf/%lf",&x,&y); return x/y; } + } + + //! Compare the first \p l characters of two C-strings, ignoring the case. + /** + \param str1 C-string. + \param str2 C-string. + \param l Number of characters to compare. + \return \c 0 if the two strings are equal, something else otherwise. + \note This function has to be defined since it is not provided by all C++-compilers (not ANSI). + **/ + inline int strncasecmp(const char *const str1, const char *const str2, const int l) { + if (!l) return 0; + if (!str1) return str2?-1:0; + const char *nstr1 = str1, *nstr2 = str2; + int k, diff = 0; for (k = 0; kp && str[q]==delimiter; ) { --q; if (!is_iterative) break; } + } + const int n = q - p + 1; + if (n!=l) { std::memmove(str,str+p,n); str[n] = 0; return true; } + return false; + } + + //! Replace escape sequences in C-strings by their binary ascii values. + /** + \param[in,out] str C-string to work with (modified at output). + **/ + inline void strunescape(char *const str) { +#define cimg_strunescape(ci,co) case ci: *nd = co; ++ns; break; + unsigned int val = 0; + for (char *ns = str, *nd = str; *ns || (bool)(*nd=0); ++nd) if (*ns=='\\') switch (*(++ns)) { + cimg_strunescape('n','\n'); + cimg_strunescape('t','\t'); + cimg_strunescape('v','\v'); + cimg_strunescape('b','\b'); + cimg_strunescape('r','\r'); + cimg_strunescape('f','\f'); + cimg_strunescape('a','\a'); + cimg_strunescape('\\','\\'); + cimg_strunescape('\?','\?'); + cimg_strunescape('\'','\''); + cimg_strunescape('\"','\"'); + case 0 : *nd = 0; break; + case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : + std::sscanf(ns,"%o",&val); while (*ns>='0' && *ns<='7') ++ns; + *nd = val; break; + case 'x': + std::sscanf(++ns,"%x",&val); + while ((*ns>='0' && *ns<='7') || (*ns>='a' && *ns<='f') || (*ns>='A' && *ns<='F')) ++ns; + *nd = val; break; + default : *nd = *(ns++); + } else *nd = *(ns++); + } + + // Return a temporary string describing the size of a memory buffer. + inline const char *strbuffersize(const unsigned long size) { + static char res[256] = { 0 }; + cimg::mutex(5); + if (size<1024LU) cimg_snprintf(res,sizeof(res),"%lu byte%s",size,size>1?"s":""); + else if (size<1024*1024LU) { const float nsize = size/1024.0f; cimg_snprintf(res,sizeof(res),"%.1f Kio",nsize); } + else if (size<1024*1024*1024LU) { + const float nsize = size/(1024*1024.0f); cimg_snprintf(res,sizeof(res),"%.1f Mio",nsize); + } else { const float nsize = size/(1024*1024*1024.0f); cimg_snprintf(res,sizeof(res),"%.1f Gio",nsize); } + cimg::mutex(5,0); + return res; + } + + // Return string that identifies the running OS. + inline const char *stros() { +#if defined(linux) || defined(__linux) || defined(__linux__) + const char *const str = "Linux"; +#elif defined(sun) || defined(__sun) + const char *const str = "Sun OS"; +#elif defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined (__DragonFly__) + const char *const str = "BSD"; +#elif defined(sgi) || defined(__sgi) + const char *const str = "Irix"; +#elif defined(__MACOSX__) || defined(__APPLE__) + const char *const str = "Mac OS"; +#elif defined(unix) || defined(__unix) || defined(__unix__) + const char *const str = "Generic Unix"; +#elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \ + defined(WIN64) || defined(_WIN64) || defined(__WIN64__) + const char *const str = "Windows"; +#else + const char + *const _str1 = std::getenv("OSTYPE"), + *const _str2 = _str1?_str1:std::getenv("OS"), + *const str = _str2?_str2:"Unknown OS"; +#endif + return str; + } + + //! Return the basename of a filename. + inline const char* basename(const char *const str) { + const char *p = 0; + for (const char *np = str; np>=str && (p=np); np = std::strchr(np,cimg_file_separator)+1) {} + return p; + } + + // Return a random filename. + inline const char* filenamerand() { + cimg::mutex(6); + static char randomid[9] = { 0 }; + cimg::srand(); + for (unsigned int k = 0; k<8; ++k) { + const int v = (int)std::rand()%3; + randomid[k] = (char)(v==0?('0'+(std::rand()%10)):(v==1?('a'+(std::rand()%26)):('A'+(std::rand()%26)))); + } + cimg::mutex(6,0); + return randomid; + } + + // Convert filename as a Windows-style filename (short path name). + inline void winformat_string(char *const str) { + if (str && *str) { +#if cimg_OS==2 + char *const nstr = new char[MAX_PATH]; + if (GetShortPathNameA(str,nstr,MAX_PATH)) std::strcpy(str,nstr); +#endif + } + } + + //! Open a file. + /** + \param path Path of the filename to open. + \param mode C-string describing the opening mode. + \return Opened file. + \note Same as std::fopen() but throw a \c CImgIOException when + the specified file cannot be opened, instead of returning \c 0. + **/ + inline std::FILE *fopen(const char *const path, const char *const mode) { + if (!path) + throw CImgArgumentException("cimg::fopen(): Specified file path is (null)."); + if (!mode) + throw CImgArgumentException("cimg::fopen(): File '%s', specified mode is (null).", + path); + std::FILE *res = 0; + if (*path=='-' && (!path[1] || path[1]=='.')) { + res = (*mode=='r')?stdin:stdout; +#if cimg_OS==2 + if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode. + if (_setmode(_fileno(res),0x8000)==-1) res = 0; + } +#endif + } else res = std::fopen(path,mode); + if (!res) throw CImgIOException("cimg::fopen(): Failed to open file '%s' with mode '%s'.", + path,mode); + return res; + } + + //! Close a file. + /** + \param file File to close. + \return \c 0 if file has been closed properly, something else otherwise. + \note Same as std::fclose() but display a warning message if + the file has not been closed properly. + **/ + inline int fclose(std::FILE *file) { + if (!file) warn("cimg::fclose(): Specified file is (null)."); + if (!file || file==stdin || file==stdout) return 0; + const int errn = std::fclose(file); + if (errn!=0) warn("cimg::fclose(): Error code %d returned during file closing.", + errn); + return errn; + } + + //! Check if a path is a directory + /** + \param path Specified path to test. + **/ + inline bool is_directory(const char *const path) { + if (!path || !*path) return false; +#if cimg_OS==1 + struct stat st_buf; + if (!stat(path,&st_buf) && S_ISDIR(st_buf.st_mode)) return true; +#elif cimg_OS==2 + if (GetFileAttributes(path)&16) return true; +#endif + return false; + } + + //! Get/set path to store temporary files. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path where temporary files can be saved. + **/ + inline const char* temporary_path(const char *const user_path=0, const bool reinit_path=false) { +#define _cimg_test_temporary_path(p) \ + if (!path_found) { \ + cimg_snprintf(s_path,1024,"%s",p); \ + cimg_snprintf(tmp,sizeof(tmp),"%s%c%s",s_path,cimg_file_separator,filetmp); \ + if ((file=std::fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } \ + } + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + char tmp[1024] = { 0 }, filetmp[512] = { 0 }; + std::FILE *file = 0; + cimg_snprintf(filetmp,sizeof(filetmp),"%s.tmp",cimg::filenamerand()); + char *tmpPath = std::getenv("TMP"); + if (!tmpPath) { tmpPath = std::getenv("TEMP"); winformat_string(tmpPath); } + if (tmpPath) _cimg_test_temporary_path(tmpPath); +#if cimg_OS==2 + _cimg_test_temporary_path("C:\\WINNT\\Temp"); + _cimg_test_temporary_path("C:\\WINDOWS\\Temp"); + _cimg_test_temporary_path("C:\\Temp"); + _cimg_test_temporary_path("C:"); + _cimg_test_temporary_path("D:\\WINNT\\Temp"); + _cimg_test_temporary_path("D:\\WINDOWS\\Temp"); + _cimg_test_temporary_path("D:\\Temp"); + _cimg_test_temporary_path("D:"); +#else + _cimg_test_temporary_path("/tmp"); + _cimg_test_temporary_path("/var/tmp"); +#endif + if (!path_found) { + *s_path = 0; + std::strncpy(tmp,filetmp,sizeof(tmp)-1); + if ((file=std::fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } + } + if (!path_found) { + cimg::mutex(7,0); + throw CImgIOException("cimg::temporary_path(): Failed to locate path for writing temporary files.\n"); + } + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the Program Files/ directory (Windows only). + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the program files. + **/ +#if cimg_OS==2 + inline const char* programfiles_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[MAX_PATH]; + std::memset(s_path,0,MAX_PATH); + // Note: in the following line, 0x26 = CSIDL_PROGRAM_FILES (not defined on every compiler). +#if !defined(__INTEL_COMPILER) + if (!SHGetSpecialFolderPathA(0,s_path,0x0026,false)) { + const char *const pfPath = std::getenv("PROGRAMFILES"); + if (pfPath) std::strncpy(s_path,pfPath,MAX_PATH-1); + else std::strcpy(s_path,"C:\\PROGRA~1"); + } +#else + std::strcpy(s_path,"C:\\PROGRA~1"); +#endif + } + cimg::mutex(7,0); + return s_path; + } +#endif + + //! Get/set path to the ImageMagick's \c convert binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c convert binary. + **/ + inline const char* imagemagick_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + const char *const pf_path = programfiles_path(); + if (!path_found) { + std::strcpy(s_path,".\\convert.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%.2d-\\convert.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d-Q\\convert.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d\\convert.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\convert.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\convert.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d\\VISUA~1\\BIN\\convert.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%.2d-\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d-Q\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d\\VISUA~1\\BIN\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%.2d-\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d-Q\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d\\VISUA~1\\BIN\\convert.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"convert.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./convert"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"convert"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the GraphicsMagick's \c gm binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c gm binary. + **/ + inline const char* graphicsmagick_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + const char *const pf_path = programfiles_path(); + if (!path_found) { + std::strcpy(s_path,".\\gm.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%.2d-\\gm.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d-Q\\gm.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d\\gm.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",pf_path,k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%.2d-\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d-Q\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%.2d-\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d-Q\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=10 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 9; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + for (int k = 32; k>=0 && !path_found; --k) { + cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"gm.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./gm"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"gm"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the XMedcon's \c medcon binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c medcon binary. + **/ + inline const char* medcon_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + const char *const pf_path = programfiles_path(); + if (!path_found) { + std::strcpy(s_path,".\\medcon.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\XMedCon\\bin\\medcon.bat",pf_path); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) { + cimg_snprintf(s_path,sizeof(s_path),"%s\\XMedCon\\bin\\medcon.exe",pf_path); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) { + std::strcpy(s_path,"C:\\XMedCon\\bin\\medcon.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"medcon.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./medcon"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"medcon"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the FFMPEG's \c ffmpeg binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c ffmpeg binary. + **/ + inline const char *ffmpeg_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + if (!path_found) { + std::strcpy(s_path,".\\ffmpeg.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"ffmpeg.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./ffmpeg"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"ffmpeg"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the \c gzip binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c gzip binary. + **/ + inline const char *gzip_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + if (!path_found) { + std::strcpy(s_path,".\\gzip.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"gzip.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./gzip"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"gzip"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the \c gzip binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c gunzip binary. + **/ + inline const char *gunzip_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + if (!path_found) { + std::strcpy(s_path,".\\gunzip.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"gunzip.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./gunzip"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"gunzip"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the \c dcraw binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c dcraw binary. + **/ + inline const char *dcraw_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + if (!path_found) { + std::strcpy(s_path,".\\dcraw.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"dcraw.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./dcraw"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"dcraw"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the \c wget binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c wget binary. + **/ + inline const char *wget_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + if (!path_found) { + std::strcpy(s_path,".\\wget.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"wget.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./wget"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"wget"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Get/set path to the \c curl binary. + /** + \param user_path Specified path, or \c 0 to get the path currently used. + \param reinit_path Force path to be recalculated (may take some time). + \return Path containing the \c curl binary. + **/ + inline const char *curl_path(const char *const user_path=0, const bool reinit_path=false) { + static char *s_path = 0; + cimg::mutex(7); + if (reinit_path) { delete[] s_path; s_path = 0; } + if (user_path) { + if (!s_path) s_path = new char[1024]; + std::memset(s_path,0,1024); + std::strncpy(s_path,user_path,1023); + } else if (!s_path) { + s_path = new char[1024]; + std::memset(s_path,0,1024); + bool path_found = false; + std::FILE *file = 0; +#if cimg_OS==2 + if (!path_found) { + std::strcpy(s_path,".\\curl.exe"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"curl.exe"); +#else + if (!path_found) { + std::strcpy(s_path,"./curl"); + if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } + } + if (!path_found) std::strcpy(s_path,"curl"); +#endif + winformat_string(s_path); + } + cimg::mutex(7,0); + return s_path; + } + + //! Split filename into two C-strings \c body and \c extension. + inline const char *split_filename(const char *const filename, char *const body=0) { + if (!filename) { if (body) *body = 0; return 0; } + const char *p = 0; for (const char *np = filename; np>=filename && (p=np); np = std::strchr(np,'.')+1) {} + if (p==filename) { + if (body) std::strcpy(body,filename); + return filename + std::strlen(filename); + } + const unsigned int l = (unsigned int)(p - filename - 1); + if (body) { std::memcpy(body,filename,l); body[l] = 0; } + return p; + } + + //! Generate a numbered version of a filename. + inline char* number_filename(const char *const filename, const int number, + const unsigned int digits, char *const str) { + if (!filename) { if (str) *str = 0; return 0; } + char format[1024] = { 0 }, body[1024] = { 0 }; + const char *const ext = cimg::split_filename(filename,body); + if (*ext) cimg_snprintf(format,sizeof(format),"%%s_%%.%ud.%%s",digits); + else cimg_snprintf(format,sizeof(format),"%%s_%%.%ud",digits); + std::sprintf(str,format,body,number,ext); + return str; + } + + //! Try to guess format from an image file. + /** + \param file Input file (can be \c 0 if \c filename is set). + \param filename Filename, as a C-string (can be \c 0 if \c file is set). + \return C-string containing the guessed file format, or \c 0 if nothing has been guessed. + **/ + inline const char *file_type(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException("cimg::file_type(): Specified filename is (null)."); + static const char + *const _pnm = "pnm", + *const _pfm = "pfm", + *const _bmp = "bmp", + *const _gif = "gif", + *const _jpg = "jpg", + *const _off = "off", + *const _pan = "pan", + *const _png = "png", + *const _tif = "tif", + *const _inr = "inr", + *const _dcm = "dcm"; + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + const char *f_type = 0, *head; + char header[2048] = { 0 }, item[1024] = { 0 }; + const unsigned char *const uheader = (unsigned char*)header; + int err; char cerr; + const unsigned int siz = (unsigned int)std::fread(header,2048,1,nfile); // Read first 2048 bytes. + if (!file) cimg::fclose(nfile); + + if (!std::strncmp(header,"OFF\n",4)) f_type = _off; // OFF. + else if (!std::strncmp(header,"#INRIMAGE",9)) f_type = _inr; // INRIMAGE. + else if (!std::strncmp(header,"PANDORE",7)) f_type = _pan; // PANDORE. + else if (!std::strncmp(header+128,"DICM",4)) f_type = _dcm; // DICOM. + else if (uheader[0]==0xFF && uheader[1]==0xD8 && uheader[2]==0xFF) f_type = _jpg; // JPEG. + else if (header[0]=='B' && header[1]=='M') f_type = _bmp; // BMP. + else if (header[0]=='G' && header[1]=='I' && header[2]=='F' && header[3]=='8' && header[5]=='a' && // GIF. + (header[4]=='7' || header[4]=='9')) f_type = _gif; + else if (uheader[0]==0x89 && uheader[1]==0x50 && uheader[2]==0x4E && uheader[3]==0x47 && // PNG. + uheader[4]==0x0D && uheader[5]==0x0A && uheader[6]==0x1A && uheader[7]==0x0A) f_type = _png; + else if ((uheader[0]==0x49 && uheader[1]==0x49) || (uheader[0]==0x4D && uheader[1]==0x4D)) f_type = _tif; // TIFF. + else { // PNM or PFM. + head = header; + while (headstd::fread() but may display warning message if all elements could not be read. + **/ + template + inline int fread(T *const ptr, const unsigned long nmemb, std::FILE *stream) { + if (!ptr || nmemb<=0 || !stream) + throw CImgArgumentException("cimg::fread(): Invalid reading request of %u %s%s from file %p to buffer %p.", + nmemb,cimg::type::string(),nmemb>1?"s":"",stream,ptr); + + const unsigned long wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T); + unsigned long to_read = nmemb, al_read = 0, l_to_read = 0, l_al_read = 0; + do { + l_to_read = (to_read*sizeof(T))0); + if (to_read>0) + warn("cimg::fread(): Only %u/%u elements could be read from file.", + al_read,nmemb); + return al_read; + } + + //! Write data to file. + /** + \param ptr Pointer to memory buffer containing the binary data to write on file. + \param nmemb Number of elements to write. + \param[out] stream File to write data on. + \return Number of written elements. + \note Similar to std::fwrite but may display warning messages if all elements could not be written. + **/ + template + inline int fwrite(const T *ptr, const unsigned long nmemb, std::FILE *stream) { + if (!ptr || !stream) + throw CImgArgumentException("cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.", + nmemb,cimg::type::string(),nmemb>1?"s":"",ptr,stream); + if (nmemb<=0) return 0; + const unsigned long wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T); + unsigned long to_write = nmemb, al_write = 0, l_to_write = 0, l_al_write = 0; + do { + l_to_write = (to_write*sizeof(T))0); + if (to_write>0) + warn("cimg::fwrite(): Only %u/%u elements could be written in file.", + al_write,nmemb); + return al_write; + } + + //! Create an empty file. + /** + \param file Input file (can be \c 0 if \c filename is set). + \param filename Filename, as a C-string (can be \c 0 if \c file is set). + **/ + inline void fempty(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException("cimg::file_type(): Specified filename is (null)."); + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + if (!file) cimg::fclose(nfile); + } + + //! Load file from network as a local temporary file. + /** + \param filename Filename, as a C-string. + \param[out] filename_local C-string containing the path to a local copy of \c filename. + \return Value of \c filename_local. + \note Use external binaries \c wget or \c curl to perform. You must have one of these tools installed + to be able to use this function. + **/ + inline char *load_network_external(const char *const filename, char *const filename_local) { + if (!filename) + throw CImgArgumentException("cimg::load_network_external(): Specified filename is (null)."); + if (!filename_local) + throw CImgArgumentException("cimg::load_network_external(): Specified destination string is (null)."); + const char *const _ext = cimg::split_filename(filename), *const ext = (*_ext && _ext>filename)?_ext-1:_ext; + char command[1024] = { 0 }; + std::FILE *file = 0; + *filename_local = 0; + do { + cimg_snprintf(filename_local,512,"%s%c%s%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); + if ((file=std::fopen(filename_local,"rb"))!=0) cimg::fclose(file); + } while (file); + + // Try with 'curl' first. + cimg_snprintf(command,sizeof(command),"%s -f --silent --compressed -o \"%s\" \"%s\"", + cimg::curl_path(),filename_local,filename); + cimg::system(command); + if (!(file = std::fopen(filename_local,"rb"))) { + + // Try with 'wget' else. + cimg_snprintf(command,sizeof(command),"%s -q -r -l 0 --no-cache -O \"%s\" \"%s\"", + cimg::wget_path(),filename_local,filename); + cimg::system(command); + if (!(file = std::fopen(filename_local,"rb"))) + throw CImgIOException("cimg::load_network_external(): Failed to load file '%s' with external commands " + "'wget' or 'curl'.",filename); + cimg::fclose(file); + + // Try gunzip it. + cimg_snprintf(command,sizeof(command),"%s.gz",filename_local); + std::rename(filename_local,command); + cimg_snprintf(command,sizeof(command),"%s --quiet \"%s.gz\"", + gunzip_path(),filename_local); + cimg::system(command); + file = std::fopen(filename_local,"rb"); + if (!file) { + cimg_snprintf(command,sizeof(command),"%s.gz",filename_local); + std::rename(command,filename_local); + file = std::fopen(filename_local,"rb"); + } + } + std::fseek(file,0,SEEK_END); // Check if file size is 0. + if (std::ftell(file)<=0) + throw CImgIOException("cimg::load_network_external(): Failed to load file '%s' with external commands " + "'wget' or 'curl'.",filename); + cimg::fclose(file); + return filename_local; + } + + //! Return options specified on the command line. + inline const char* option(const char *const name, const int argc, const char *const *const argv, + const char *const defaut, const char *const usage, const bool reset_static) { + static bool first = true, visu = false; + if (reset_static) { first = true; return 0; } + const char *res = 0; + if (first) { + first = false; + visu = cimg::option("-h",argc,argv,(char*)0,(char*)0,false)!=0; + visu |= cimg::option("-help",argc,argv,(char*)0,(char*)0,false)!=0; + visu |= cimg::option("--help",argc,argv,(char*)0,(char*)0,false)!=0; + } + if (!name && visu) { + if (usage) { + std::fprintf(cimg::output(),"\n %s%s%s",cimg::t_red,cimg::basename(argv[0]),cimg::t_normal); + std::fprintf(cimg::output(),": %s",usage); + std::fprintf(cimg::output()," (%s, %s)\n\n",__DATE__,__TIME__); + } + if (defaut) std::fprintf(cimg::output(),"%s\n",defaut); + } + if (name) { + if (argc>0) { + int k = 0; + while (k Operating System: %s%-13s%s %s('cimg_OS'=%d)%s\n", + cimg::t_bold, + cimg_OS==1?"Unix":(cimg_OS==2?"Windows":"Unknow"), + cimg::t_normal,cimg::t_green, + cimg_OS, + cimg::t_normal); + + std::fprintf(cimg::output()," > CPU endianness: %s%s Endian%s\n", + cimg::t_bold, + cimg::endianness()?"Big":"Little", + cimg::t_normal); + + std::fprintf(cimg::output()," > Verbosity mode: %s%-13s%s %s('cimg_verbosity'=%d)%s\n", + cimg::t_bold, + cimg_verbosity==0?"Quiet": + cimg_verbosity==1?"Console": + cimg_verbosity==2?"Dialog": + cimg_verbosity==3?"Console+Warnings":"Dialog+Warnings", + cimg::t_normal,cimg::t_green, + cimg_verbosity, + cimg::t_normal); + + std::fprintf(cimg::output()," > Stricts warnings: %s%-13s%s %s('cimg_strict_warnings' %s)%s\n", + cimg::t_bold, +#ifdef cimg_strict_warnings + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Using VT100 messages: %s%-13s%s %s('cimg_use_vt100' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_vt100 + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Display type: %s%-13s%s %s('cimg_display'=%d)%s\n", + cimg::t_bold, + cimg_display==0?"No display":cimg_display==1?"X11":cimg_display==2?"Windows GDI":"Unknown", + cimg::t_normal,cimg::t_green, + cimg_display, + cimg::t_normal); + +#if cimg_display==1 + std::fprintf(cimg::output()," > Using XShm for X11: %s%-13s%s %s('cimg_use_xshm' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_xshm + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Using XRand for X11: %s%-13s%s %s('cimg_use_xrandr' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_xrandr + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); +#endif + std::fprintf(cimg::output()," > Using OpenMP: %s%-13s%s %s('cimg_use_openmp' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_openmp + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + std::fprintf(cimg::output()," > Using PNG library: %s%-13s%s %s('cimg_use_png' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_png + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + std::fprintf(cimg::output()," > Using JPEG library: %s%-13s%s %s('cimg_use_jpeg' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_jpeg + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Using TIFF library: %s%-13s%s %s('cimg_use_tiff' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_tiff + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Using Magick++ library: %s%-13s%s %s('cimg_use_magick' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_magick + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Using FFTW3 library: %s%-13s%s %s('cimg_use_fftw3' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_fftw3 + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + std::fprintf(cimg::output()," > Using LAPACK library: %s%-13s%s %s('cimg_use_lapack' %s)%s\n", + cimg::t_bold, +#ifdef cimg_use_lapack + "Yes",cimg::t_normal,cimg::t_green,"defined", +#else + "No",cimg::t_normal,cimg::t_green,"undefined", +#endif + cimg::t_normal); + + cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::imagemagick_path()); + std::fprintf(cimg::output()," > Path of ImageMagick: %s%-13s%s\n", + cimg::t_bold, + tmp, + cimg::t_normal); + + cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::graphicsmagick_path()); + std::fprintf(cimg::output()," > Path of GraphicsMagick: %s%-13s%s\n", + cimg::t_bold, + tmp, + cimg::t_normal); + + cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::medcon_path()); + std::fprintf(cimg::output()," > Path of 'medcon': %s%-13s%s\n", + cimg::t_bold, + tmp, + cimg::t_normal); + + cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::temporary_path()); + std::fprintf(cimg::output()," > Temporary path: %s%-13s%s\n", + cimg::t_bold, + tmp, + cimg::t_normal); + + std::fprintf(cimg::output(),"\n"); + } + + // Declare LAPACK function signatures if LAPACK support is enabled. +#ifdef cimg_use_lapack + template + inline void getrf(int &N, T *lapA, int *IPIV, int &INFO) { + dgetrf_(&N,&N,lapA,&N,IPIV,&INFO); + } + + inline void getrf(int &N, float *lapA, int *IPIV, int &INFO) { + sgetrf_(&N,&N,lapA,&N,IPIV,&INFO); + } + + template + inline void getri(int &N, T *lapA, int *IPIV, T* WORK, int &LWORK, int &INFO) { + dgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO); + } + + inline void getri(int &N, float *lapA, int *IPIV, float* WORK, int &LWORK, int &INFO) { + sgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO); + } + + template + inline void gesvd(char &JOB, int &M, int &N, T *lapA, int &MN, + T *lapS, T *lapU, T *lapV, T *WORK, int &LWORK, int &INFO) { + dgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO); + } + + inline void gesvd(char &JOB, int &M, int &N, float *lapA, int &MN, + float *lapS, float *lapU, float *lapV, float *WORK, int &LWORK, int &INFO) { + sgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO); + } + + template + inline void getrs(char &TRANS, int &N, T *lapA, int *IPIV, T *lapB, int &INFO) { + int one = 1; + dgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO); + } + + inline void getrs(char &TRANS, int &N, float *lapA, int *IPIV, float *lapB, int &INFO) { + int one = 1; + sgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO); + } + + template + inline void syev(char &JOB, char &UPLO, int &N, T *lapA, T *lapW, T *WORK, int &LWORK, int &INFO) { + dsyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO); + } + + inline void syev(char &JOB, char &UPLO, int &N, float *lapA, float *lapW, float *WORK, int &LWORK, int &INFO) { + ssyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO); + } + + template + inline void sgels(char & TRANS, int &M, int &N, int &NRHS, T* lapA, int &LDA, + T* lapB, int &LDB, T* WORK, int &LWORK, int &INFO){ + dgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO); + } + + inline void sgels(char & TRANS, int &M, int &N, int &NRHS, float* lapA, int &LDA, + float* lapB, int &LDB, float* WORK, int &LWORK, int &INFO){ + sgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO); + } + +#endif + + // End of the 'cimg' namespace + } + + /*------------------------------------------------ + # + # + # Definition of mathematical operators and + # external functions. + # + # + -------------------------------------------------*/ + +#define _cimg_create_ext_operators(typ) \ + template \ + inline CImg::type> operator+(const typ val, const CImg& img) { \ + return img + val; \ + } \ + template \ + inline CImg::type> operator-(const typ val, const CImg& img) { \ + typedef typename cimg::superset::type Tt; \ + return CImg(img._width,img._height,img._depth,img._spectrum,val)-=img; \ + } \ + template \ + inline CImg::type> operator*(const typ val, const CImg& img) { \ + return img*val; \ + } \ + template \ + inline CImg::type> operator/(const typ val, const CImg& img) { \ + return val*img.get_invert(); \ + } \ + template \ + inline CImg::type> operator&(const typ val, const CImg& img) { \ + return img & val; \ + } \ + template \ + inline CImg::type> operator|(const typ val, const CImg& img) { \ + return img | val; \ + } \ + template \ + inline CImg::type> operator^(const typ val, const CImg& img) { \ + return img ^ val; \ + } \ + template \ + inline bool operator==(const typ val, const CImg& img) { \ + return img == val; \ + } \ + template \ + inline bool operator!=(const typ val, const CImg& img) { \ + return img != val; \ + } + + _cimg_create_ext_operators(bool) + _cimg_create_ext_operators(unsigned char) + _cimg_create_ext_operators(char) + _cimg_create_ext_operators(signed char) + _cimg_create_ext_operators(unsigned short) + _cimg_create_ext_operators(short) + _cimg_create_ext_operators(unsigned int) + _cimg_create_ext_operators(int) + _cimg_create_ext_operators(unsigned long) + _cimg_create_ext_operators(long) + _cimg_create_ext_operators(float) + _cimg_create_ext_operators(double) + + template + inline CImg<_cimg_Tfloat> operator+(const char *const expression, const CImg& img) { + return img + expression; + } + + template + inline CImg<_cimg_Tfloat> operator-(const char *const expression, const CImg& img) { + return CImg<_cimg_Tfloat>(img._width,img._height,img._depth,img._spectrum,expression,true)-=img; + } + + template + inline CImg<_cimg_Tfloat> operator*(const char *const expression, const CImg& img) { + return img*expression; + } + + template + inline CImg<_cimg_Tfloat> operator/(const char *const expression, const CImg& img) { + return expression*img.get_invert(); + } + + template + inline CImg operator&(const char *const expression, const CImg& img) { + return img & expression; + } + + template + inline CImg operator|(const char *const expression, const CImg& img) { + return img | expression; + } + + template + inline CImg operator^(const char *const expression, const CImg& img) { + return img ^ expression; + } + + template + inline bool operator==(const char *const expression, const CImg& img) { + return img == expression; + } + + template + inline bool operator!=(const char *const expression, const CImg& img) { + return img != expression; + } + + template + inline CImg<_cimg_Tfloat> sqr(const CImg& instance) { + return instance.get_sqr(); + } + + template + inline CImg<_cimg_Tfloat> sqrt(const CImg& instance) { + return instance.get_sqrt(); + } + + template + inline CImg<_cimg_Tfloat> exp(const CImg& instance) { + return instance.get_exp(); + } + + template + inline CImg<_cimg_Tfloat> log(const CImg& instance) { + return instance.get_log(); + } + + template + inline CImg<_cimg_Tfloat> log2(const CImg& instance) { + return instance.get_log2(); + } + + template + inline CImg<_cimg_Tfloat> log10(const CImg& instance) { + return instance.get_log10(); + } + + template + inline CImg<_cimg_Tfloat> abs(const CImg& instance) { + return instance.get_abs(); + } + + template + inline CImg<_cimg_Tfloat> sign(const CImg& instance) { + return instance.get_sign(); + } + + template + inline CImg<_cimg_Tfloat> cos(const CImg& instance) { + return instance.get_cos(); + } + + template + inline CImg<_cimg_Tfloat> sin(const CImg& instance) { + return instance.get_sin(); + } + + template + inline CImg<_cimg_Tfloat> sinc(const CImg& instance) { + return instance.get_sinc(); + } + + template + inline CImg<_cimg_Tfloat> tan(const CImg& instance) { + return instance.get_tan(); + } + + template + inline CImg<_cimg_Tfloat> acos(const CImg& instance) { + return instance.get_acos(); + } + + template + inline CImg<_cimg_Tfloat> asin(const CImg& instance) { + return instance.get_asin(); + } + + template + inline CImg<_cimg_Tfloat> atan(const CImg& instance) { + return instance.get_atan(); + } + + template + inline CImg<_cimg_Tfloat> cosh(const CImg& instance) { + return instance.get_cosh(); + } + + template + inline CImg<_cimg_Tfloat> sinh(const CImg& instance) { + return instance.get_sinh(); + } + + template + inline CImg<_cimg_Tfloat> tanh(const CImg& instance) { + return instance.get_tanh(); + } + + template + inline CImg transpose(const CImg& instance) { + return instance.get_transpose(); + } + + template + inline CImg<_cimg_Tfloat> invert(const CImg& instance) { + return instance.get_invert(); + } + + template + inline CImg<_cimg_Tfloat> pseudoinvert(const CImg& instance) { + return instance.get_pseudoinvert(); + } + + /*----------------------------------- + # + # Define the CImgDisplay structure + # + ----------------------------------*/ + //! Allow to create windows, display images on them and manage user events (keyboard, mouse and windows events). + /** + CImgDisplay methods rely on a low-level graphic library to perform: it can be either \b X-Window + (X11, for Unix-based systems) or \b GDI32 (for Windows-based systems). + If both libraries are missing, CImgDisplay will not be able to display images on screen, and will enter + a minimal mode where warning messages will be outputed each time the program is trying to call one of the + CImgDisplay method. + + The configuration variable \c cimg_display tells about the graphic library used. + It is set automatically by \CImg when one of these graphic libraries has been detected. + But, you can override its value if necessary. Valid choices are: + - 0: Disable display capabilities. + - 1: Use \b X-Window (X11) library. + - 2: Use \b GDI32 library. + + Remember to link your program against \b X11 or \b GDI32 libraries if you use CImgDisplay. + **/ + struct CImgDisplay { + unsigned long _timer, _fps_frames, _fps_timer; + unsigned int _width, _height, _normalization; + float _fps_fps, _min, _max; + bool _is_fullscreen; + char *_title; + volatile unsigned int _window_width, _window_height, _button, _keys[128], _released_keys[128]; + volatile int _window_x, _window_y, _mouse_x, _mouse_y, _wheel; + volatile bool _is_closed, _is_resized, _is_moved, _is_event, + _is_keyESC, _is_keyF1, _is_keyF2, _is_keyF3, _is_keyF4, _is_keyF5, _is_keyF6, _is_keyF7, + _is_keyF8, _is_keyF9, _is_keyF10, _is_keyF11, _is_keyF12, _is_keyPAUSE, _is_key1, _is_key2, + _is_key3, _is_key4, _is_key5, _is_key6, _is_key7, _is_key8, _is_key9, _is_key0, + _is_keyBACKSPACE, _is_keyINSERT, _is_keyHOME, _is_keyPAGEUP, _is_keyTAB, _is_keyQ, _is_keyW, _is_keyE, + _is_keyR, _is_keyT, _is_keyY, _is_keyU, _is_keyI, _is_keyO, _is_keyP, _is_keyDELETE, + _is_keyEND, _is_keyPAGEDOWN, _is_keyCAPSLOCK, _is_keyA, _is_keyS, _is_keyD, _is_keyF, _is_keyG, + _is_keyH, _is_keyJ, _is_keyK, _is_keyL, _is_keyENTER, _is_keySHIFTLEFT, _is_keyZ, _is_keyX, + _is_keyC, _is_keyV, _is_keyB, _is_keyN, _is_keyM, _is_keySHIFTRIGHT, _is_keyARROWUP, _is_keyCTRLLEFT, + _is_keyAPPLEFT, _is_keyALT, _is_keySPACE, _is_keyALTGR, _is_keyAPPRIGHT, _is_keyMENU, _is_keyCTRLRIGHT, + _is_keyARROWLEFT, _is_keyARROWDOWN, _is_keyARROWRIGHT, _is_keyPAD0, _is_keyPAD1, _is_keyPAD2, _is_keyPAD3, + _is_keyPAD4, _is_keyPAD5, _is_keyPAD6, _is_keyPAD7, _is_keyPAD8, _is_keyPAD9, _is_keyPADADD, _is_keyPADSUB, + _is_keyPADMUL, _is_keyPADDIV; + + //@} + //--------------------------- + // + //! \name Plugins + //@{ + //--------------------------- + +#ifdef cimgdisplay_plugin +#include cimgdisplay_plugin +#endif +#ifdef cimgdisplay_plugin1 +#include cimgdisplay_plugin1 +#endif +#ifdef cimgdisplay_plugin2 +#include cimgdisplay_plugin2 +#endif +#ifdef cimgdisplay_plugin3 +#include cimgdisplay_plugin3 +#endif +#ifdef cimgdisplay_plugin4 +#include cimgdisplay_plugin4 +#endif +#ifdef cimgdisplay_plugin5 +#include cimgdisplay_plugin5 +#endif +#ifdef cimgdisplay_plugin6 +#include cimgdisplay_plugin6 +#endif +#ifdef cimgdisplay_plugin7 +#include cimgdisplay_plugin7 +#endif +#ifdef cimgdisplay_plugin8 +#include cimgdisplay_plugin8 +#endif + + //@} + //-------------------------------------------------------- + // + //! \name Constructors / Destructor / Instance Management + //@{ + //-------------------------------------------------------- + + //! Destructor. + /** + \note If the associated window is visible on the screen, it is closed by the call to the destructor. + **/ + ~CImgDisplay() { + assign(); + } + + //! Construct an empty display. + /** + \note Constructing an empty CImgDisplay instance does not make a window appearing on the screen, until + display of valid data is performed. + \par Example + \code + CImgDisplay disp; // Does actually nothing. + ... + disp.display(img); // Construct new window and display image in it. + \endcode + **/ + CImgDisplay(): + _width(0),_height(0),_normalization(0), + _min(0),_max(0), + _is_fullscreen(false), + _title(0), + _window_width(0),_window_height(0),_button(0), + _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), + _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { + assign(); + } + + //! Construct a display with specified dimensions. + /** \param width Window width. + \param height Window height. + \param title Window title. + \param normalization Normalization type + (0=none, 1=always, 2=once, 3=pixel type-dependent, see normalization()). + \param is_fullscreen Tells if fullscreen mode is enabled. + \param is_closed Tells if associated window is initially visible or not. + \note A black background is initially displayed on the associated window. + **/ + CImgDisplay(const unsigned int width, const unsigned int height, + const char *const title=0, const unsigned int normalization=3, + const bool is_fullscreen=false, const bool is_closed=false): + _width(0),_height(0),_normalization(0), + _min(0),_max(0), + _is_fullscreen(false), + _title(0), + _window_width(0),_window_height(0),_button(0), + _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), + _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { + assign(width,height,title,normalization,is_fullscreen,is_closed); + } + + //! Construct a display from an image. + /** \param img Image used as a model to create the window. + \param title Window title. + \param normalization Normalization type + (0=none, 1=always, 2=once, 3=pixel type-dependent, see normalization()). + \param is_fullscreen Tells if fullscreen mode is enabled. + \param is_closed Tells if associated window is initially visible or not. + \note The pixels of the input image are initially displayed on the associated window. + **/ + template + explicit CImgDisplay(const CImg& img, + const char *const title=0, const unsigned int normalization=3, + const bool is_fullscreen=false, const bool is_closed=false): + _width(0),_height(0),_normalization(0), + _min(0),_max(0), + _is_fullscreen(false), + _title(0), + _window_width(0),_window_height(0),_button(0), + _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), + _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { + assign(img,title,normalization,is_fullscreen,is_closed); + } + + //! Construct a display from an image list. + /** \param list The images list to display. + \param title Window title. + \param normalization Normalization type + (0=none, 1=always, 2=once, 3=pixel type-dependent, see normalization()). + \param is_fullscreen Tells if fullscreen mode is enabled. + \param is_closed Tells if associated window is initially visible or not. + \note All images of the list, appended along the X-axis, are initially displayed on the associated window. + **/ + template + explicit CImgDisplay(const CImgList& list, + const char *const title=0, const unsigned int normalization=3, + const bool is_fullscreen=false, const bool is_closed=false): + _width(0),_height(0),_normalization(0), + _min(0),_max(0), + _is_fullscreen(false), + _title(0), + _window_width(0),_window_height(0),_button(0), + _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), + _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { + assign(list,title,normalization,is_fullscreen,is_closed); + } + + //! Construct a display as a copy of an existing one. + /** + \param disp Display instance to copy. + \note The pixel buffer of the input window is initially displayed on the associated window. + **/ + CImgDisplay(const CImgDisplay& disp): + _width(0),_height(0),_normalization(0), + _min(0),_max(0), + _is_fullscreen(false), + _title(0), + _window_width(0),_window_height(0),_button(0), + _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), + _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { + assign(disp); + } + +#if cimg_display==0 + + static void _no_display_exception() { + throw CImgDisplayException("CImgDisplay(): No display available."); + } + + //! Destructor - Empty constructor \inplace. + /** + \note Replace the current instance by an empty display. + **/ + CImgDisplay& assign() { + return flush(); + } + + //! Construct a display with specified dimensions \inplace. + /** + **/ + CImgDisplay& assign(const unsigned int width, const unsigned int height, + const char *const title=0, const unsigned int normalization=3, + const bool is_fullscreen=false, const bool is_closed=false) { + cimg::unused(width,height,title,normalization,is_fullscreen,is_closed); + _no_display_exception(); + return assign(); + } + + //! Construct a display from an image \inplace. + /** + **/ + template + CImgDisplay& assign(const CImg& img, + const char *const title=0, const unsigned int normalization=3, + const bool is_fullscreen=false, const bool is_closed=false) { + _no_display_exception(); + return assign(img._width,img._height,title,normalization,is_fullscreen,is_closed); + } + + //! Construct a display from an image list \inplace. + /** + **/ + template + CImgDisplay& assign(const CImgList& list, + const char *const title=0, const unsigned int normalization=3, + const bool is_fullscreen=false, const bool is_closed=false) { + _no_display_exception(); + return assign(list._width,list._width,title,normalization,is_fullscreen,is_closed); + } + + //! Construct a display as a copy of another one \inplace. + /** + **/ + CImgDisplay& assign(const CImgDisplay &disp) { + _no_display_exception(); + return assign(disp._width,disp._height); + } + +#endif + + //! Return a reference to an empty display. + /** + \note Can be useful for writing function prototypes where one of the argument (of type CImgDisplay&) + must have a default value. + \par Example + \code + void foo(CImgDisplay& disp=CImgDisplay::empty()); + \endcode + **/ + static CImgDisplay& empty() { + static CImgDisplay _empty; + return _empty.assign(); + } + +#define cimg_fitscreen(dx,dy,dz) CImgDisplay::_fitscreen(dx,dy,dz,128,-85,false), \ + CImgDisplay::_fitscreen(dx,dy,dz,128,-85,true) + static unsigned int _fitscreen(const unsigned int dx, const unsigned int dy, const unsigned int dz, + const int dmin, const int dmax,const bool return_y) { + const unsigned int _nw = dx + (dz>1?dz:0), _nh = dy + (dz>1?dz:0); + unsigned int nw = _nw?_nw:1, nh = _nh?_nh:1; + const unsigned int + sw = CImgDisplay::screen_width(), sh = CImgDisplay::screen_height(), + mw = dmin<0?(unsigned int)(sw*-dmin/100):(unsigned int)dmin, + mh = dmin<0?(unsigned int)(sh*-dmin/100):(unsigned int)dmin, + Mw = dmax<0?(unsigned int)(sw*-dmax/100):(unsigned int)dmax, + Mh = dmax<0?(unsigned int)(sh*-dmax/100):(unsigned int)dmax; + if (nwMw) { nh = nh*Mw/nw; nh+=(nh==0?1:0); nw = Mw; } + if (nh>Mh) { nw = nw*Mh/nh; nw+=(nw==0?1:0); nh = Mh; } + if (nwdisp = img is equivalent to disp.display(img). + **/ + template + CImgDisplay& operator=(const CImg& img) { + return display(img); + } + + //! Display list of images on associated window. + /** + \note disp = list is equivalent to disp.display(list). + **/ + template + CImgDisplay& operator=(const CImgList& list) { + return display(list); + } + + //! Construct a display as a copy of another one \inplace. + /** + \note Equivalent to assign(const CImgDisplay&). + **/ + CImgDisplay& operator=(const CImgDisplay& disp) { + return assign(disp); + } + + //! Return \c false if display is empty, \c true otherwise. + /** + \note if (disp) { ... } is equivalent to if (!disp.is_empty()) { ... }. + **/ + operator bool() const { + return !is_empty(); + } + + //@} + //------------------------------------------ + // + //! \name Instance Checking + //@{ + //------------------------------------------ + + //! Return \c true if display is empty, \c false otherwise. + /** + **/ + bool is_empty() const { + return !(_width && _height); + } + + //! Return \c true if display is closed (i.e. not visible on the screen), \c false otherwise. + /** + \note + - When a user physically closes the associated window, the display is set to closed. + - A closed display is not destroyed. Its associated window can be show again on the screen using show(). + **/ + bool is_closed() const { + return _is_closed; + } + + //! Return \c true if associated window has been resized on the screen, \c false otherwise. + /** + **/ + bool is_resized() const { + return _is_resized; + } + + //! Return \c true if associated window has been moved on the screen, \c false otherwise. + /** + **/ + bool is_moved() const { + return _is_moved; + } + + //! Return \c true if any event has occured on the associated window, \c false otherwise. + /** + **/ + bool is_event() const { + return _is_event; + } + + //! Return \c true if current display is in fullscreen mode, \c false otherwise. + /** + **/ + bool is_fullscreen() const { + return _is_fullscreen; + } + + //! Return \c true if any key is being pressed on the associated window, \c false otherwise. + /** + \note The methods below do the same only for specific keys. + **/ + bool is_key() const { + return _is_keyESC || _is_keyF1 || _is_keyF2 || _is_keyF3 || + _is_keyF4 || _is_keyF5 || _is_keyF6 || _is_keyF7 || + _is_keyF8 || _is_keyF9 || _is_keyF10 || _is_keyF11 || + _is_keyF12 || _is_keyPAUSE || _is_key1 || _is_key2 || + _is_key3 || _is_key4 || _is_key5 || _is_key6 || + _is_key7 || _is_key8 || _is_key9 || _is_key0 || + _is_keyBACKSPACE || _is_keyINSERT || _is_keyHOME || + _is_keyPAGEUP || _is_keyTAB || _is_keyQ || _is_keyW || + _is_keyE || _is_keyR || _is_keyT || _is_keyY || + _is_keyU || _is_keyI || _is_keyO || _is_keyP || + _is_keyDELETE || _is_keyEND || _is_keyPAGEDOWN || + _is_keyCAPSLOCK || _is_keyA || _is_keyS || _is_keyD || + _is_keyF || _is_keyG || _is_keyH || _is_keyJ || + _is_keyK || _is_keyL || _is_keyENTER || + _is_keySHIFTLEFT || _is_keyZ || _is_keyX || _is_keyC || + _is_keyV || _is_keyB || _is_keyN || _is_keyM || + _is_keySHIFTRIGHT || _is_keyARROWUP || _is_keyCTRLLEFT || + _is_keyAPPLEFT || _is_keyALT || _is_keySPACE || _is_keyALTGR || + _is_keyAPPRIGHT || _is_keyMENU || _is_keyCTRLRIGHT || + _is_keyARROWLEFT || _is_keyARROWDOWN || _is_keyARROWRIGHT || + _is_keyPAD0 || _is_keyPAD1 || _is_keyPAD2 || + _is_keyPAD3 || _is_keyPAD4 || _is_keyPAD5 || + _is_keyPAD6 || _is_keyPAD7 || _is_keyPAD8 || + _is_keyPAD9 || _is_keyPADADD || _is_keyPADSUB || + _is_keyPADMUL || _is_keyPADDIV; + } + + //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise. + /** + \param keycode Keycode to test. + \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure + your code stay portable (see cimg::keyESC). + \par Example + \code + CImgDisplay disp(400,400); + while (!disp.is_closed()) { + if (disp.key(cimg::keyTAB)) { ... } // Equivalent to 'if (disp.is_keyTAB())'. + disp.wait(); + } + \endcode + **/ + bool is_key(const unsigned int keycode) const { +#define _cimg_iskey_test(k) if (keycode==cimg::key##k) return _is_key##k; + _cimg_iskey_test(ESC); _cimg_iskey_test(F1); _cimg_iskey_test(F2); _cimg_iskey_test(F3); + _cimg_iskey_test(F4); _cimg_iskey_test(F5); _cimg_iskey_test(F6); _cimg_iskey_test(F7); + _cimg_iskey_test(F8); _cimg_iskey_test(F9); _cimg_iskey_test(F10); _cimg_iskey_test(F11); + _cimg_iskey_test(F12); _cimg_iskey_test(PAUSE); _cimg_iskey_test(1); _cimg_iskey_test(2); + _cimg_iskey_test(3); _cimg_iskey_test(4); _cimg_iskey_test(5); _cimg_iskey_test(6); + _cimg_iskey_test(7); _cimg_iskey_test(8); _cimg_iskey_test(9); _cimg_iskey_test(0); + _cimg_iskey_test(BACKSPACE); _cimg_iskey_test(INSERT); _cimg_iskey_test(HOME); + _cimg_iskey_test(PAGEUP); _cimg_iskey_test(TAB); _cimg_iskey_test(Q); _cimg_iskey_test(W); + _cimg_iskey_test(E); _cimg_iskey_test(R); _cimg_iskey_test(T); _cimg_iskey_test(Y); + _cimg_iskey_test(U); _cimg_iskey_test(I); _cimg_iskey_test(O); _cimg_iskey_test(P); + _cimg_iskey_test(DELETE); _cimg_iskey_test(END); _cimg_iskey_test(PAGEDOWN); + _cimg_iskey_test(CAPSLOCK); _cimg_iskey_test(A); _cimg_iskey_test(S); _cimg_iskey_test(D); + _cimg_iskey_test(F); _cimg_iskey_test(G); _cimg_iskey_test(H); _cimg_iskey_test(J); + _cimg_iskey_test(K); _cimg_iskey_test(L); _cimg_iskey_test(ENTER); + _cimg_iskey_test(SHIFTLEFT); _cimg_iskey_test(Z); _cimg_iskey_test(X); _cimg_iskey_test(C); + _cimg_iskey_test(V); _cimg_iskey_test(B); _cimg_iskey_test(N); _cimg_iskey_test(M); + _cimg_iskey_test(SHIFTRIGHT); _cimg_iskey_test(ARROWUP); _cimg_iskey_test(CTRLLEFT); + _cimg_iskey_test(APPLEFT); _cimg_iskey_test(ALT); _cimg_iskey_test(SPACE); _cimg_iskey_test(ALTGR); + _cimg_iskey_test(APPRIGHT); _cimg_iskey_test(MENU); _cimg_iskey_test(CTRLRIGHT); + _cimg_iskey_test(ARROWLEFT); _cimg_iskey_test(ARROWDOWN); _cimg_iskey_test(ARROWRIGHT); + _cimg_iskey_test(PAD0); _cimg_iskey_test(PAD1); _cimg_iskey_test(PAD2); + _cimg_iskey_test(PAD3); _cimg_iskey_test(PAD4); _cimg_iskey_test(PAD5); + _cimg_iskey_test(PAD6); _cimg_iskey_test(PAD7); _cimg_iskey_test(PAD8); + _cimg_iskey_test(PAD9); _cimg_iskey_test(PADADD); _cimg_iskey_test(PADSUB); + _cimg_iskey_test(PADMUL); _cimg_iskey_test(PADDIV); + return false; + } + + //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise. + /** + \param keycode C-string containing the keycode label of the key to test. + \note Use it when the key you want to test can be dynamically set by the user. + \par Example + \code + CImgDisplay disp(400,400); + const char *const keycode = "TAB"; + while (!disp.is_closed()) { + if (disp.is_key(keycode)) { ... } // Equivalent to 'if (disp.is_keyTAB())'. + disp.wait(); + } + \endcode + **/ + volatile bool& is_key(const char *const keycode) { + static bool f = false; + f = false; +#define _cimg_iskey_test2(k) if (!cimg::strcasecmp(keycode,#k)) return _is_key##k; + _cimg_iskey_test2(ESC); _cimg_iskey_test2(F1); _cimg_iskey_test2(F2); _cimg_iskey_test2(F3); + _cimg_iskey_test2(F4); _cimg_iskey_test2(F5); _cimg_iskey_test2(F6); _cimg_iskey_test2(F7); + _cimg_iskey_test2(F8); _cimg_iskey_test2(F9); _cimg_iskey_test2(F10); _cimg_iskey_test2(F11); + _cimg_iskey_test2(F12); _cimg_iskey_test2(PAUSE); _cimg_iskey_test2(1); _cimg_iskey_test2(2); + _cimg_iskey_test2(3); _cimg_iskey_test2(4); _cimg_iskey_test2(5); _cimg_iskey_test2(6); + _cimg_iskey_test2(7); _cimg_iskey_test2(8); _cimg_iskey_test2(9); _cimg_iskey_test2(0); + _cimg_iskey_test2(BACKSPACE); _cimg_iskey_test2(INSERT); _cimg_iskey_test2(HOME); + _cimg_iskey_test2(PAGEUP); _cimg_iskey_test2(TAB); _cimg_iskey_test2(Q); _cimg_iskey_test2(W); + _cimg_iskey_test2(E); _cimg_iskey_test2(R); _cimg_iskey_test2(T); _cimg_iskey_test2(Y); + _cimg_iskey_test2(U); _cimg_iskey_test2(I); _cimg_iskey_test2(O); _cimg_iskey_test2(P); + _cimg_iskey_test2(DELETE); _cimg_iskey_test2(END); _cimg_iskey_test2(PAGEDOWN); + _cimg_iskey_test2(CAPSLOCK); _cimg_iskey_test2(A); _cimg_iskey_test2(S); _cimg_iskey_test2(D); + _cimg_iskey_test2(F); _cimg_iskey_test2(G); _cimg_iskey_test2(H); _cimg_iskey_test2(J); + _cimg_iskey_test2(K); _cimg_iskey_test2(L); _cimg_iskey_test2(ENTER); + _cimg_iskey_test2(SHIFTLEFT); _cimg_iskey_test2(Z); _cimg_iskey_test2(X); _cimg_iskey_test2(C); + _cimg_iskey_test2(V); _cimg_iskey_test2(B); _cimg_iskey_test2(N); _cimg_iskey_test2(M); + _cimg_iskey_test2(SHIFTRIGHT); _cimg_iskey_test2(ARROWUP); _cimg_iskey_test2(CTRLLEFT); + _cimg_iskey_test2(APPLEFT); _cimg_iskey_test2(ALT); _cimg_iskey_test2(SPACE); _cimg_iskey_test2(ALTGR); + _cimg_iskey_test2(APPRIGHT); _cimg_iskey_test2(MENU); _cimg_iskey_test2(CTRLRIGHT); + _cimg_iskey_test2(ARROWLEFT); _cimg_iskey_test2(ARROWDOWN); _cimg_iskey_test2(ARROWRIGHT); + _cimg_iskey_test2(PAD0); _cimg_iskey_test2(PAD1); _cimg_iskey_test2(PAD2); + _cimg_iskey_test2(PAD3); _cimg_iskey_test2(PAD4); _cimg_iskey_test2(PAD5); + _cimg_iskey_test2(PAD6); _cimg_iskey_test2(PAD7); _cimg_iskey_test2(PAD8); + _cimg_iskey_test2(PAD9); _cimg_iskey_test2(PADADD); _cimg_iskey_test2(PADSUB); + _cimg_iskey_test2(PADMUL); _cimg_iskey_test2(PADDIV); + return f; + } + + //! Return \c true if specified key sequence has been typed on the associated window, \c false otherwise. + /** + \param keycodes_sequence Buffer of keycodes to test. + \param length Number of keys in the \c keycodes_sequence buffer. + \param remove_sequence Tells if the key sequence must be removed from the key history, if found. + \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure + your code stay portable (see cimg::keyESC). + \par Example + \code + CImgDisplay disp(400,400); + const unsigned int key_seq[] = { cimg::keyCTRLLEFT, cimg::keyD }; + while (!disp.is_closed()) { + if (disp.is_key_sequence(key_seq,2)) { ... } // Test for the 'CTRL+D' keyboard event. + disp.wait(); + } + \endcode + **/ + bool is_key_sequence(const unsigned int *const keycodes_sequence, const unsigned int length, + const bool remove_sequence=false) { + if (keycodes_sequence && length) { + const unsigned int + *const ps_end = keycodes_sequence + length - 1, + *const pk_end = (unsigned int*)_keys + 1 + sizeof(_keys)/sizeof(unsigned int) - length, + k = *ps_end; + for (unsigned int *pk = (unsigned int*)_keys; pk[0,255]. + If the range of values of the data to display is different, a normalization may be required for displaying + the data in a correct way. The normalization type can be one of: + - \c 0: Value normalization is disabled. It is then assumed that all input data to be displayed by the + CImgDisplay instance have values in range [0,255]. + - \c 1: Value normalization is always performed (this is the default behavior). + Before displaying an input image, its values will be (virtually) stretched + in range [0,255], so that the contrast of the displayed pixels will be maximum. + Use this mode for images whose minimum and maximum values are not prescribed to known values + (e.g. float-valued images). + Note that when normalized versions of images are computed for display purposes, the actual values of these + images are not modified. + - \c 2: Value normalization is performed once (on the first image display), then the same normalization + coefficients are kept for next displayed frames. + - \c 3: Value normalization depends on the pixel type of the data to display. For integer pixel types, + the normalization is done regarding the minimum/maximum values of the type (no normalization occurs then + for unsigned char). + For float-valued pixel types, the normalization is done regarding the minimum/maximum value of the image + data instead. + **/ + unsigned int normalization() const { + return _normalization; + } + + //! Return title of the associated window as a C-string. + /** + \note Window title may be not visible, depending on the used window manager or if the current display is + in fullscreen mode. + **/ + const char *title() const { + return _title; + } + + //! Return width of the associated window. + /** + \note The width of the display (i.e. the width of the pixel data buffer associated to the CImgDisplay instance) + may be different from the actual width of the associated window. + **/ + int window_width() const { + return (int)_window_width; + } + + //! Return height of the associated window. + /** + \note The height of the display (i.e. the height of the pixel data buffer associated to the CImgDisplay instance) + may be different from the actual height of the associated window. + **/ + int window_height() const { + return (int)_window_height; + } + + //! Return X-coordinate of the associated window. + /** + \note The returned coordinate corresponds to the location of the upper-left corner of the associated window. + **/ + int window_x() const { + return _window_x; + } + + //! Return Y-coordinate of the associated window. + /** + \note The returned coordinate corresponds to the location of the upper-left corner of the associated window. + **/ + int window_y() const { + return _window_y; + } + + //! Return X-coordinate of the mouse pointer. + /** + \note + - If the mouse pointer is outside window area, \c -1 is returned. + - Otherwise, the returned value is in the range [0,width()-1]. + **/ + int mouse_x() const { + return _mouse_x; + } + + //! Return Y-coordinate of the mouse pointer. + /** + \note + - If the mouse pointer is outside window area, \c -1 is returned. + - Otherwise, the returned value is in the range [0,height()-1]. + **/ + int mouse_y() const { + return _mouse_y; + } + + //! Return current state of the mouse buttons. + /** + \note Three mouse buttons can be managed. If one button is pressed, its corresponding bit in the returned + value is set: + - bit \c 0 (value \c 0x1): State of the left mouse button. + - bit \c 1 (value \c 0x2): State of the right mouse button. + - bit \c 2 (value \c 0x4): State of the middle mouse button. + + Several bits can be activated if more than one button are pressed at the same time. + \par Example + \code + CImgDisplay disp(400,400); + while (!disp.is_closed()) { + if (disp.button()&1) { // Left button clicked. + ... + } + if (disp.button()&2) { // Right button clicked. + ... + } + if (disp.button()&4) { // Middle button clicked. + ... + } + disp.wait(); + } + \endcode + **/ + unsigned int button() const { + return _button; + } + + //! Return current state of the mouse wheel. + /** + \note + - The returned value can be positive or negative depending on whether the mouse wheel has been scrolled + forward or backward. + - Scrolling the wheel forward add \c 1 to the wheel value. + - Scrolling the wheel backward substract \c 1 to the wheel value. + - The returned value cumulates the number of forward of backward scrolls since the creation of the display, + or since the last reset of the wheel value (using set_wheel()). It is strongly recommended to quickly reset + the wheel counter when an action has been performed regarding the current wheel value. + Otherwise, the returned wheel value may be for instance \c 0 despite the fact that many scrolls have been done + (as many in forward as in backward directions). + \par Example + \code + CImgDisplay disp(400,400); + while (!disp.is_closed()) { + if (disp.wheel()) { + int counter = disp.wheel(); // Read the state of the mouse wheel. + ... // Do what you want with 'counter'. + disp.set_wheel(); // Reset the wheel value to 0. + } + disp.wait(); + } + \endcode + **/ + int wheel() const { + return _wheel; + } + + //! Return one entry from the pressed keys history. + /** + \param pos Indice to read from the pressed keys history (indice \c 0 corresponds to latest entry). + \return Keycode of a pressed key or \c 0 for a released key. + \note + - Each CImgDisplay stores a history of the pressed keys in a buffer of size \c 128. When a new key is pressed, + its keycode is stored in the pressed keys history. When a key is released, \c 0 is put instead. + This means that up to the 64 last pressed keys may be read from the pressed keys history. + When a new value is stored, the pressed keys history is shifted so that the latest entry is always + stored at position \c 0. + - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure + your code stay portable (see cimg::keyESC). + **/ + unsigned int key(const unsigned int pos=0) const { + return pos<(sizeof(_keys)/sizeof(unsigned int))?_keys[pos]:0; + } + + //! Return one entry from the released keys history. + /** + \param pos Indice to read from the released keys history (indice \c 0 corresponds to latest entry). + \return Keycode of a released key or \c 0 for a pressed key. + \note + - Each CImgDisplay stores a history of the released keys in a buffer of size \c 128. When a new key is released, + its keycode is stored in the pressed keys history. When a key is pressed, \c 0 is put instead. + This means that up to the 64 last released keys may be read from the released keys history. + When a new value is stored, the released keys history is shifted so that the latest entry is always + stored at position \c 0. + - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure + your code stay portable (see cimg::keyESC). + **/ + unsigned int released_key(const unsigned int pos=0) const { + return pos<(sizeof(_released_keys)/sizeof(unsigned int))?_released_keys[pos]:0; + } + + //! Return keycode corresponding to the specified string. + /** + \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure + your code stay portable (see cimg::keyESC). + \par Example + \code + const unsigned int keyTAB = CImgDisplay::keycode("TAB"); // Return cimg::keyTAB. + \endcode + **/ + static unsigned int keycode(const char *const keycode) { +#define _cimg_keycode(k) if (!cimg::strcasecmp(keycode,#k)) return cimg::key##k; + _cimg_keycode(ESC); _cimg_keycode(F1); _cimg_keycode(F2); _cimg_keycode(F3); + _cimg_keycode(F4); _cimg_keycode(F5); _cimg_keycode(F6); _cimg_keycode(F7); + _cimg_keycode(F8); _cimg_keycode(F9); _cimg_keycode(F10); _cimg_keycode(F11); + _cimg_keycode(F12); _cimg_keycode(PAUSE); _cimg_keycode(1); _cimg_keycode(2); + _cimg_keycode(3); _cimg_keycode(4); _cimg_keycode(5); _cimg_keycode(6); + _cimg_keycode(7); _cimg_keycode(8); _cimg_keycode(9); _cimg_keycode(0); + _cimg_keycode(BACKSPACE); _cimg_keycode(INSERT); _cimg_keycode(HOME); + _cimg_keycode(PAGEUP); _cimg_keycode(TAB); _cimg_keycode(Q); _cimg_keycode(W); + _cimg_keycode(E); _cimg_keycode(R); _cimg_keycode(T); _cimg_keycode(Y); + _cimg_keycode(U); _cimg_keycode(I); _cimg_keycode(O); _cimg_keycode(P); + _cimg_keycode(DELETE); _cimg_keycode(END); _cimg_keycode(PAGEDOWN); + _cimg_keycode(CAPSLOCK); _cimg_keycode(A); _cimg_keycode(S); _cimg_keycode(D); + _cimg_keycode(F); _cimg_keycode(G); _cimg_keycode(H); _cimg_keycode(J); + _cimg_keycode(K); _cimg_keycode(L); _cimg_keycode(ENTER); + _cimg_keycode(SHIFTLEFT); _cimg_keycode(Z); _cimg_keycode(X); _cimg_keycode(C); + _cimg_keycode(V); _cimg_keycode(B); _cimg_keycode(N); _cimg_keycode(M); + _cimg_keycode(SHIFTRIGHT); _cimg_keycode(ARROWUP); _cimg_keycode(CTRLLEFT); + _cimg_keycode(APPLEFT); _cimg_keycode(ALT); _cimg_keycode(SPACE); _cimg_keycode(ALTGR); + _cimg_keycode(APPRIGHT); _cimg_keycode(MENU); _cimg_keycode(CTRLRIGHT); + _cimg_keycode(ARROWLEFT); _cimg_keycode(ARROWDOWN); _cimg_keycode(ARROWRIGHT); + _cimg_keycode(PAD0); _cimg_keycode(PAD1); _cimg_keycode(PAD2); + _cimg_keycode(PAD3); _cimg_keycode(PAD4); _cimg_keycode(PAD5); + _cimg_keycode(PAD6); _cimg_keycode(PAD7); _cimg_keycode(PAD8); + _cimg_keycode(PAD9); _cimg_keycode(PADADD); _cimg_keycode(PADSUB); + _cimg_keycode(PADMUL); _cimg_keycode(PADDIV); + return 0; + } + + //! Return the current refresh rate, in frames per second. + /** + \note Returns a significant value when the current instance is used to display successive frames. + It measures the delay between successive calls to frames_per_second(). + **/ + float frames_per_second() { + if (!_fps_timer) _fps_timer = cimg::time(); + const float delta = (cimg::time()-_fps_timer)/1000.0f; + ++_fps_frames; + if (delta>=1) { + _fps_fps = _fps_frames/delta; + _fps_frames = 0; + _fps_timer = cimg::time(); + } + return _fps_fps; + } + + //@} + //--------------------------------------- + // + //! \name Window Manipulation + //@{ + //--------------------------------------- + +#if cimg_display==0 + + //! Display image on associated window. + /** + \param img Input image to display. + \note This method returns immediately. + **/ + template + CImgDisplay& display(const CImg& img) { + return assign(img); + } + +#endif + + //! Display list of images on associated window. + /** + \param list List of images to display. + \param axis Axis used to append the images along, for the visualization (can be \c x, \c y, \c z or \c c). + \param align Relative position of aligned images when displaying lists with images of different sizes + (\c 0 for upper-left, \c 0.5 for centering and \c 1 for lower-right). + \note This method returns immediately. + **/ + template + CImgDisplay& display(const CImgList& list, const char axis='x', const float align=0) { + CImgList::ucharT> visu(list._width); + cimglist_for(list,l) { + const CImg& img = list._data[l]; + img.__get_select(*this,_normalization,(img._width-1)/2,(img._height-1)/2,(img._depth-1)/2).move_to(visu[l]); + } + visu.get_append(axis,align).display(*this); + return *this; + } + +#if cimg_display==0 + + //! Show (closed) associated window on the screen. + /** + \note + - Force the associated window of a display to be visible on the screen, even if it has been closed before. + - Using show() on a visible display does nothing. + **/ + CImgDisplay& show() { + return assign(); + } + + //! Close (visible) associated window and make it disappear from the screen. + /** + \note + - A closed display only means the associated window is not visible anymore. This does not mean the display has + been destroyed. + Use show() to make the associated window reappear. + - Using close() on a closed display does nothing. + **/ + CImgDisplay& close() { + return assign(); + } + + //! Move associated window to a new location. + /** + \param pos_x X-coordinate of the new window location. + \param pos_y Y-coordinate of the new window location. + \note Depending on the window manager behavior, this method may not succeed (no exceptions are thrown + nevertheless). + **/ + CImgDisplay& move(const int pos_x, const int pos_y) { + return assign(pos_x,pos_y); + } + +#endif + + //! Resize display to the size of the associated window. + /** + \param force_redraw Tells if the previous window content must be updated and refreshed as well. + \note + - Calling this method ensures that width() and window_width() become equal, as well as height() and + window_height(). + - The associated window is also resized to specified dimensions. + **/ + CImgDisplay& resize(const bool force_redraw=true) { + resize(_window_width,_window_height,force_redraw); + return *this; + } + +#if cimg_display==0 + + //! Resize display to the specified size. + /** + \param width Requested display width. + \param height Requested display height. + \param force_redraw Tells if the previous window content must be updated and refreshed as well. + \note The associated window is also resized to specified dimensions. + **/ + CImgDisplay& resize(const int width, const int height, const bool force_redraw=true) { + return assign(width,height,0,3,force_redraw); + } + +#endif + + //! Resize display to the size of an input image. + /** + \param img Input image to take size from. + \param force_redraw Tells if the previous window content must be resized and updated as well. + \note + - Calling this method ensures that width() and img.width() become equal, as well as height() and + img.height(). + - The associated window is also resized to specified dimensions. + **/ + template + CImgDisplay& resize(const CImg& img, const bool force_redraw=true) { + return resize(img._width,img._height,force_redraw); + } + + //! Resize display to the size of another CImgDisplay instance. + /** + \param disp Input display to take size from. + \param force_redraw Tells if the previous window content must be resized and updated as well. + \note + - Calling this method ensures that width() and disp.width() become equal, as well as height() and + disp.height(). + - The associated window is also resized to specified dimensions. + **/ + CImgDisplay& resize(const CImgDisplay& disp, const bool force_redraw=true) { + return resize(disp._width,disp._height,force_redraw); + } + + // [internal] Render pixel buffer with size (wd,hd) from source buffer of size (ws,hs). + template + static void _render_resize(const T *ptrs, const unsigned int ws, const unsigned int hs, + t *ptrd, const unsigned int wd, const unsigned int hd) { + unsigned int *const offx = new unsigned int[wd], *const offy = new unsigned int[hd+1], *poffx, *poffy; + float s, curr, old; + s = (float)ws/wd; + poffx = offx; curr = 0; for (unsigned int x = 0; xstd::printf(). + \warning As the first argument is a format string, it is highly recommended to write + \code + disp.set_title("%s",window_title); + \endcode + instead of + \code + disp.set_title(window_title); + \endcode + if \c window_title can be arbitrary, to prevent nasty memory access. + **/ + CImgDisplay& set_title(const char *const format, ...) { + return assign(0,0,format); + } + +#endif + + //! Enable or disable fullscreen mode. + /** + \param is_fullscreen Tells is the fullscreen mode must be activated or not. + \param force_redraw Tells if the previous window content must be displayed as well. + \note + - When the fullscreen mode is enabled, the associated window fills the entire screen but the size of the + current display is not modified. + - The screen resolution may be switched to fit the associated window size and ensure it appears the largest + as possible. + For X-Window (X11) users, the configuration flag \c cimg_use_xrandr has to be set to allow the screen + resolution change (requires the X11 extensions to be enabled). + **/ + CImgDisplay& set_fullscreen(const bool is_fullscreen, const bool force_redraw=true) { + if (is_empty() || _is_fullscreen==is_fullscreen) return *this; + return toggle_fullscreen(force_redraw); + } + +#if cimg_display==0 + + //! Toggle fullscreen mode. + /** + \param force_redraw Tells if the previous window content must be displayed as well. + \note Enable fullscreen mode if it was not enabled, and disable it otherwise. + **/ + CImgDisplay& toggle_fullscreen(const bool force_redraw=true) { + return assign(_width,_height,0,3,force_redraw); + } + + //! Show mouse pointer. + /** + \note Depending on the window manager behavior, this method may not succeed + (no exceptions are thrown nevertheless). + **/ + CImgDisplay& show_mouse() { + return assign(); + } + + //! Hide mouse pointer. + /** + \note Depending on the window manager behavior, this method may not succeed + (no exceptions are thrown nevertheless). + **/ + CImgDisplay& hide_mouse() { + return assign(); + } + + //! Move mouse pointer to a specified location. + /** + \note Depending on the window manager behavior, this method may not succeed + (no exceptions are thrown nevertheless). + **/ + CImgDisplay& set_mouse(const int pos_x, const int pos_y) { + return assign(pos_x,pos_y); + } + +#endif + + //! Simulate a mouse button release event. + /** + \note All mouse buttons are considered released at the same time. + **/ + CImgDisplay& set_button() { + _button = 0; + _is_event = true; +#if cimg_display==1 + pthread_cond_broadcast(&cimg::X11_attr().wait_event); +#elif cimg_display==2 + SetEvent(cimg::Win32_attr().wait_event); +#endif + return *this; + } + + //! Simulate a mouse button press or release event. + /** + \param button Buttons event code, where each button is associated to a single bit. + \param is_pressed Tells if the mouse button is considered as pressed or released. + **/ + CImgDisplay& set_button(const unsigned int button, const bool is_pressed=true) { + const unsigned int buttoncode = button==1?1:button==2?2:button==3?4:0; + if (is_pressed) _button |= buttoncode; else _button &= ~buttoncode; + _is_event = buttoncode?true:false; + if (buttoncode) { +#if cimg_display==1 + pthread_cond_broadcast(&cimg::X11_attr().wait_event); +#elif cimg_display==2 + SetEvent(cimg::Win32_attr().wait_event); +#endif + } + return *this; + } + + //! Flush all mouse wheel events. + /** + \note Make wheel() to return \c 0, if called afterwards. + **/ + CImgDisplay& set_wheel() { + _wheel = 0; + _is_event = true; +#if cimg_display==1 + pthread_cond_broadcast(&cimg::X11_attr().wait_event); +#elif cimg_display==2 + SetEvent(cimg::Win32_attr().wait_event); +#endif + return *this; + } + + //! Simulate a wheel event. + /** + \param amplitude Amplitude of the wheel scrolling to simulate. + \note Make wheel() to return \c amplitude, if called afterwards. + **/ + CImgDisplay& set_wheel(const int amplitude) { + _wheel+=amplitude; + _is_event = amplitude?true:false; + if (amplitude) { +#if cimg_display==1 + pthread_cond_broadcast(&cimg::X11_attr().wait_event); +#elif cimg_display==2 + SetEvent(cimg::Win32_attr().wait_event); +#endif + } + return *this; + } + + //! Flush all key events. + /** + \note Make key() to return \c 0, if called afterwards. + **/ + CImgDisplay& set_key() { + std::memset((void*)_keys,0,sizeof(_keys)); + std::memset((void*)_released_keys,0,sizeof(_released_keys)); + _is_keyESC = _is_keyF1 = _is_keyF2 = _is_keyF3 = _is_keyF4 = _is_keyF5 = _is_keyF6 = _is_keyF7 = _is_keyF8 = + _is_keyF9 = _is_keyF10 = _is_keyF11 = _is_keyF12 = _is_keyPAUSE = _is_key1 = _is_key2 = _is_key3 = _is_key4 = + _is_key5 = _is_key6 = _is_key7 = _is_key8 = _is_key9 = _is_key0 = _is_keyBACKSPACE = _is_keyINSERT = + _is_keyHOME = _is_keyPAGEUP = _is_keyTAB = _is_keyQ = _is_keyW = _is_keyE = _is_keyR = _is_keyT = _is_keyY = + _is_keyU = _is_keyI = _is_keyO = _is_keyP = _is_keyDELETE = _is_keyEND = _is_keyPAGEDOWN = _is_keyCAPSLOCK = + _is_keyA = _is_keyS = _is_keyD = _is_keyF = _is_keyG = _is_keyH = _is_keyJ = _is_keyK = _is_keyL = + _is_keyENTER = _is_keySHIFTLEFT = _is_keyZ = _is_keyX = _is_keyC = _is_keyV = _is_keyB = _is_keyN = + _is_keyM = _is_keySHIFTRIGHT = _is_keyARROWUP = _is_keyCTRLLEFT = _is_keyAPPLEFT = _is_keyALT = _is_keySPACE = + _is_keyALTGR = _is_keyAPPRIGHT = _is_keyMENU = _is_keyCTRLRIGHT = _is_keyARROWLEFT = _is_keyARROWDOWN = + _is_keyARROWRIGHT = _is_keyPAD0 = _is_keyPAD1 = _is_keyPAD2 = _is_keyPAD3 = _is_keyPAD4 = _is_keyPAD5 = + _is_keyPAD6 = _is_keyPAD7 = _is_keyPAD8 = _is_keyPAD9 = _is_keyPADADD = _is_keyPADSUB = _is_keyPADMUL = + _is_keyPADDIV = false; + _is_event = true; +#if cimg_display==1 + pthread_cond_broadcast(&cimg::X11_attr().wait_event); +#elif cimg_display==2 + SetEvent(cimg::Win32_attr().wait_event); +#endif + return *this; + } + + //! Simulate a keyboard press/release event. + /** + \param keycode Keycode of the associated key. + \param is_pressed Tells if the key is considered as pressed or released. + \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure + your code stay portable (see cimg::keyESC). + **/ + CImgDisplay& set_key(const unsigned int keycode, const bool is_pressed=true) { +#define _cimg_set_key(k) if (keycode==cimg::key##k) _is_key##k = is_pressed; + _cimg_set_key(ESC); _cimg_set_key(F1); _cimg_set_key(F2); _cimg_set_key(F3); + _cimg_set_key(F4); _cimg_set_key(F5); _cimg_set_key(F6); _cimg_set_key(F7); + _cimg_set_key(F8); _cimg_set_key(F9); _cimg_set_key(F10); _cimg_set_key(F11); + _cimg_set_key(F12); _cimg_set_key(PAUSE); _cimg_set_key(1); _cimg_set_key(2); + _cimg_set_key(3); _cimg_set_key(4); _cimg_set_key(5); _cimg_set_key(6); + _cimg_set_key(7); _cimg_set_key(8); _cimg_set_key(9); _cimg_set_key(0); + _cimg_set_key(BACKSPACE); _cimg_set_key(INSERT); _cimg_set_key(HOME); + _cimg_set_key(PAGEUP); _cimg_set_key(TAB); _cimg_set_key(Q); _cimg_set_key(W); + _cimg_set_key(E); _cimg_set_key(R); _cimg_set_key(T); _cimg_set_key(Y); + _cimg_set_key(U); _cimg_set_key(I); _cimg_set_key(O); _cimg_set_key(P); + _cimg_set_key(DELETE); _cimg_set_key(END); _cimg_set_key(PAGEDOWN); + _cimg_set_key(CAPSLOCK); _cimg_set_key(A); _cimg_set_key(S); _cimg_set_key(D); + _cimg_set_key(F); _cimg_set_key(G); _cimg_set_key(H); _cimg_set_key(J); + _cimg_set_key(K); _cimg_set_key(L); _cimg_set_key(ENTER); + _cimg_set_key(SHIFTLEFT); _cimg_set_key(Z); _cimg_set_key(X); _cimg_set_key(C); + _cimg_set_key(V); _cimg_set_key(B); _cimg_set_key(N); _cimg_set_key(M); + _cimg_set_key(SHIFTRIGHT); _cimg_set_key(ARROWUP); _cimg_set_key(CTRLLEFT); + _cimg_set_key(APPLEFT); _cimg_set_key(ALT); _cimg_set_key(SPACE); _cimg_set_key(ALTGR); + _cimg_set_key(APPRIGHT); _cimg_set_key(MENU); _cimg_set_key(CTRLRIGHT); + _cimg_set_key(ARROWLEFT); _cimg_set_key(ARROWDOWN); _cimg_set_key(ARROWRIGHT); + _cimg_set_key(PAD0); _cimg_set_key(PAD1); _cimg_set_key(PAD2); + _cimg_set_key(PAD3); _cimg_set_key(PAD4); _cimg_set_key(PAD5); + _cimg_set_key(PAD6); _cimg_set_key(PAD7); _cimg_set_key(PAD8); + _cimg_set_key(PAD9); _cimg_set_key(PADADD); _cimg_set_key(PADSUB); + _cimg_set_key(PADMUL); _cimg_set_key(PADDIV); + if (is_pressed) { + if (*_keys) + std::memmove((void*)(_keys+1),(void*)_keys,sizeof(_keys) - sizeof(unsigned int)); + *_keys = keycode; + if (*_released_keys) { + std::memmove((void*)(_released_keys+1),(void*)_released_keys,sizeof(_released_keys) - sizeof(unsigned int)); + *_released_keys = 0; + } + } else { + if (*_keys) { + std::memmove((void*)(_keys+1),(void*)_keys,sizeof(_keys) - sizeof(unsigned int)); + *_keys = 0; + } + if (*_released_keys) + std::memmove((void*)(_released_keys+1),(void*)_released_keys,sizeof(_released_keys) - sizeof(unsigned int)); + *_released_keys = keycode; + } + _is_event = keycode?true:false; + if (keycode) { +#if cimg_display==1 + pthread_cond_broadcast(&cimg::X11_attr().wait_event); +#elif cimg_display==2 + SetEvent(cimg::Win32_attr().wait_event); +#endif + } + return *this; + } + + //! Flush all display events. + /** + \note Remove all passed events from the current display. + **/ + CImgDisplay& flush() { + set_key().set_button().set_wheel(); + _is_resized = _is_moved = _is_event = false; + _fps_timer = _fps_frames = _timer = 0; + _fps_fps = 0; + return *this; + } + + //! Wait for any user event occuring on the current display. + CImgDisplay& wait() { + wait(*this); + return *this; + } + + //! Wait for a given number of milliseconds since the last call to wait(). + /** + \param milliseconds Number of milliseconds to wait for. + \note Similar to cimg::wait(). + **/ + CImgDisplay& wait(const unsigned int milliseconds) { + cimg::_wait(milliseconds,_timer); + return *this; + } + + //! Wait for any event occuring on the display \c disp1. + static void wait(CImgDisplay& disp1) { + disp1._is_event = false; + while (!disp1._is_closed && !disp1._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1 or \c disp2. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2) { + disp1._is_event = disp2._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed) && + !disp1._is_event && !disp2._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2 or \c disp3. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3) { + disp1._is_event = disp2._is_event = disp3._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3 or \c disp4. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4 or \c disp5. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, + CImgDisplay& disp5) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event) + wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp6. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, + CImgDisplay& disp6) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = + disp6._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || + !disp6._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && + !disp6._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp7. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, + CImgDisplay& disp6, CImgDisplay& disp7) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = + disp6._is_event = disp7._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || + !disp6._is_closed || !disp7._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && + !disp6._is_event && !disp7._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp8. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, + CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = + disp6._is_event = disp7._is_event = disp8._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || + !disp6._is_closed || !disp7._is_closed || !disp8._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && + !disp6._is_event && !disp7._is_event && !disp8._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp9. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, + CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = + disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || + !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && + !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event) wait_all(); + } + + //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp10. + static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, + CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9, + CImgDisplay& disp10) { + disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = + disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = disp10._is_event = false; + while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || + !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed || !disp10._is_closed) && + !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && + !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event && !disp10._is_event) + wait_all(); + } + +#if cimg_display==0 + + //! Wait for any window event occuring in any opened CImgDisplay. + static void wait_all() { + return _no_display_exception(); + } + + //! Render image into internal display buffer. + /** + \param img Input image data to render. + \note + - Convert image data representation into the internal display buffer (architecture-dependent structure). + - The content of the associated window is not modified, until paint() is called. + - Should not be used for common CImgDisplay uses, since display() is more useful. + **/ + template + CImgDisplay& render(const CImg& img) { + return assign(img); + } + + //! Paint internal display buffer on associated window. + /** + \note + - Update the content of the associated window with the internal display buffer, e.g. after a render() call. + - Should not be used for common CImgDisplay uses, since display() is more useful. + **/ + CImgDisplay& paint() { + return assign(); + } + + //! Take a snapshot of the associated window content. + /** + \param[out] img Output snapshot. Can be empty on input. + **/ + template + const CImgDisplay& snapshot(CImg& img) const { + cimg::unused(img); + _no_display_exception(); + return *this; + } +#endif + + // X11-based implementation + //-------------------------- +#if cimg_display==1 + + Atom _wm_window_atom, _wm_protocol_atom; + Window _window, _background_window; + Colormap _colormap; + XImage *_image; + void *_data; +#ifdef cimg_use_xshm + XShmSegmentInfo *_shminfo; +#endif + + static int screen_width() { + Display *const dpy = cimg::X11_attr().display; + int res = 0; + if (!dpy) { + Display *const _dpy = XOpenDisplay(0); + if (!_dpy) + throw CImgDisplayException("CImgDisplay::screen_width(): Failed to open X11 display."); + res = DisplayWidth(_dpy,DefaultScreen(_dpy)); + XCloseDisplay(_dpy); + } else { +#ifdef cimg_use_xrandr + if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) + res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width; + else res = DisplayWidth(dpy,DefaultScreen(dpy)); +#else + res = DisplayWidth(dpy,DefaultScreen(dpy)); +#endif + } + return res; + } + + static int screen_height() { + Display *const dpy = cimg::X11_attr().display; + int res = 0; + if (!dpy) { + Display *const _dpy = XOpenDisplay(0); + if (!_dpy) + throw CImgDisplayException("CImgDisplay::screen_height(): Failed to open X11 display."); + res = DisplayHeight(_dpy,DefaultScreen(_dpy)); + XCloseDisplay(_dpy); + } else { +#ifdef cimg_use_xrandr + if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) + res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height; + else res = DisplayHeight(dpy,DefaultScreen(dpy)); +#else + res = DisplayHeight(dpy,DefaultScreen(dpy)); +#endif + } + return res; + } + + static void wait_all() { + if (!cimg::X11_attr().display) return; + if (cimg::mutex(13,2)) { cimg::sleep(10); return; } + pthread_cond_wait(&cimg::X11_attr().wait_event,&cimg::X11_attr().wait_event_mutex); + cimg::mutex(13,0); + } + + void _handle_events(const XEvent *const pevent) { + Display *const dpy = cimg::X11_attr().display; + XEvent event = *pevent; + switch (event.type) { + case ClientMessage : { + if ((int)event.xclient.message_type==(int)_wm_protocol_atom && + (int)event.xclient.data.l[0]==(int)_wm_window_atom) { + XUnmapWindow(cimg::X11_attr().display,_window); + _is_closed = _is_event = true; + pthread_cond_broadcast(&cimg::X11_attr().wait_event); + } + } break; + case ConfigureNotify : { + while (XCheckWindowEvent(dpy,_window,StructureNotifyMask,&event)) {} + const unsigned int nw = event.xconfigure.width, nh = event.xconfigure.height; + const int nx = event.xconfigure.x, ny = event.xconfigure.y; + if (nw && nh && (nw!=_window_width || nh!=_window_height)) { + _window_width = nw; _window_height = nh; _mouse_x = _mouse_y = -1; + XResizeWindow(dpy,_window,_window_width,_window_height); + _is_resized = _is_event = true; + pthread_cond_broadcast(&cimg::X11_attr().wait_event); + } + if (nx!=_window_x || ny!=_window_y) { + _window_x = nx; _window_y = ny; _is_moved = _is_event = true; + pthread_cond_broadcast(&cimg::X11_attr().wait_event); + } + } break; + case Expose : { + while (XCheckWindowEvent(dpy,_window,ExposureMask,&event)) {} + _paint(false); + if (_is_fullscreen) { + XWindowAttributes attr; + XGetWindowAttributes(dpy,_window,&attr); + while (attr.map_state!=IsViewable) XSync(dpy,0); + XSetInputFocus(dpy,_window,RevertToParent,CurrentTime); + } + } break; + case ButtonPress : { + do { + _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y; + if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; + switch (event.xbutton.button) { + case 1 : set_button(1); break; + case 3 : set_button(2); break; + case 2 : set_button(3); break; + } + } while (XCheckWindowEvent(dpy,_window,ButtonPressMask,&event)); + } break; + case ButtonRelease : { + do { + _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y; + if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; + switch (event.xbutton.button) { + case 1 : set_button(1,false); break; + case 3 : set_button(2,false); break; + case 2 : set_button(3,false); break; + case 4 : set_wheel(1); break; + case 5 : set_wheel(-1); break; + } + } while (XCheckWindowEvent(dpy,_window,ButtonReleaseMask,&event)); + } break; + case KeyPress : { + char tmp = 0; KeySym ksym; + XLookupString(&event.xkey,&tmp,1,&ksym,0); + set_key((unsigned int)ksym,true); + } break; + case KeyRelease : { + char keys_return[32]; // Check that the key has been physically unpressed. + XQueryKeymap(dpy,keys_return); + const unsigned int kc = event.xkey.keycode, kc1 = kc/8, kc2 = kc%8; + const bool is_key_pressed = kc1>=32?false:(keys_return[kc1]>>kc2)&1; + if (!is_key_pressed) { + char tmp = 0; KeySym ksym; + XLookupString(&event.xkey,&tmp,1,&ksym,0); + set_key((unsigned int)ksym,false); + } + } break; + case EnterNotify: { + while (XCheckWindowEvent(dpy,_window,EnterWindowMask,&event)) {} + _mouse_x = event.xmotion.x; + _mouse_y = event.xmotion.y; + if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; + } break; + case LeaveNotify : { + while (XCheckWindowEvent(dpy,_window,LeaveWindowMask,&event)) {} + _mouse_x = _mouse_y =-1; _is_event = true; + pthread_cond_broadcast(&cimg::X11_attr().wait_event); + } break; + case MotionNotify : { + while (XCheckWindowEvent(dpy,_window,PointerMotionMask,&event)) {} + _mouse_x = event.xmotion.x; + _mouse_y = event.xmotion.y; + if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; + _is_event = true; + pthread_cond_broadcast(&cimg::X11_attr().wait_event); + } break; + } + } + + static void* _events_thread(void *arg) { // Thread to manage events for all opened display windows. + Display *const dpy = cimg::X11_attr().display; + XEvent event; + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,0); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0); + if (!arg) for (;;) { + XLockDisplay(dpy); + bool event_flag = XCheckTypedEvent(dpy,ClientMessage,&event); + if (!event_flag) event_flag = XCheckMaskEvent(dpy, + ExposureMask | StructureNotifyMask | ButtonPressMask | + KeyPressMask | PointerMotionMask | EnterWindowMask | + LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask,&event); + if (event_flag) + for (unsigned int i = 0; i_is_closed && event.xany.window==cimg::X11_attr().wins[i]->_window) + cimg::X11_attr().wins[i]->_handle_events(&event); + XUnlockDisplay(dpy); + pthread_testcancel(); + cimg::sleep(8); + } + return 0; + } + + void _set_colormap(Colormap& _colormap, const unsigned int dim) { + XColor colormap[256]; + switch (dim) { + case 1 : { // colormap for greyscale images + for (unsigned int index = 0; index<256; ++index) { + colormap[index].pixel = index; + colormap[index].red = colormap[index].green = colormap[index].blue = (unsigned short)(index<<8); + colormap[index].flags = DoRed | DoGreen | DoBlue; + } + } break; + case 2 : { // colormap for RG images + for (unsigned int index = 0, r = 8; r<256; r+=16) + for (unsigned int g = 8; g<256; g+=16) { + colormap[index].pixel = index; + colormap[index].red = colormap[index].blue = (unsigned short)(r<<8); + colormap[index].green = (unsigned short)(g<<8); + colormap[index++].flags = DoRed | DoGreen | DoBlue; + } + } break; + default : { // colormap for RGB images + for (unsigned int index = 0, r = 16; r<256; r+=32) + for (unsigned int g = 16; g<256; g+=32) + for (unsigned int b = 32; b<256; b+=64) { + colormap[index].pixel = index; + colormap[index].red = (unsigned short)(r<<8); + colormap[index].green = (unsigned short)(g<<8); + colormap[index].blue = (unsigned short)(b<<8); + colormap[index++].flags = DoRed | DoGreen | DoBlue; + } + } + } + XStoreColors(cimg::X11_attr().display,_colormap,colormap,256); + } + + void _map_window() { + Display *const dpy = cimg::X11_attr().display; + bool is_exposed = false, is_mapped = false; + XWindowAttributes attr; + XEvent event; + XMapRaised(dpy,_window); + do { // Wait for the window to be mapped. + XWindowEvent(dpy,_window,StructureNotifyMask | ExposureMask,&event); + switch (event.type) { + case MapNotify : is_mapped = true; break; + case Expose : is_exposed = true; break; + } + } while (!is_exposed || !is_mapped); + do { // Wait for the window to be visible. + XGetWindowAttributes(dpy,_window,&attr); + if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); } + } while (attr.map_state!=IsViewable); + _window_x = attr.x; + _window_y = attr.y; + } + + void _paint(const bool wait_expose=true) { + if (_is_closed || !_image) return; + Display *const dpy = cimg::X11_attr().display; + if (wait_expose) { // Send an expose event sticked to display window to force repaint. + XEvent event; + event.xexpose.type = Expose; + event.xexpose.serial = 0; + event.xexpose.send_event = 1; + event.xexpose.display = dpy; + event.xexpose.window = _window; + event.xexpose.x = 0; + event.xexpose.y = 0; + event.xexpose.width = width(); + event.xexpose.height = height(); + event.xexpose.count = 0; + XSendEvent(dpy,_window,0,0,&event); + } else { // Repaint directly (may be called from the expose event). + GC gc = DefaultGC(dpy,DefaultScreen(dpy)); +#ifdef cimg_use_xshm + if (_shminfo) XShmPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height,1); + else XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height); +#else + XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height); +#endif + } + } + + template + void _resize(T pixel_type, const unsigned int ndimx, const unsigned int ndimy, const bool force_redraw) { + Display *const dpy = cimg::X11_attr().display; + cimg::unused(pixel_type); + +#ifdef cimg_use_xshm + if (_shminfo) { + XShmSegmentInfo *const nshminfo = new XShmSegmentInfo; + XImage *const nimage = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)), + cimg::X11_attr().nb_bits,ZPixmap,0,nshminfo,ndimx,ndimy); + if (!nimage) { delete nshminfo; return; } + else { + nshminfo->shmid = shmget(IPC_PRIVATE,ndimx*ndimy*sizeof(T),IPC_CREAT | 0777); + if (nshminfo->shmid==-1) { XDestroyImage(nimage); delete nshminfo; return; } + else { + nshminfo->shmaddr = nimage->data = (char*)shmat(nshminfo->shmid,0,0); + if (nshminfo->shmaddr==(char*)-1) { + shmctl(nshminfo->shmid,IPC_RMID,0); XDestroyImage(nimage); delete nshminfo; return; + } else { + nshminfo->readOnly = 0; + cimg::X11_attr().is_shm_enabled = true; + XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm); + XShmAttach(dpy,nshminfo); + XFlush(dpy); + XSetErrorHandler(oldXErrorHandler); + if (!cimg::X11_attr().is_shm_enabled) { + shmdt(nshminfo->shmaddr); + shmctl(nshminfo->shmid,IPC_RMID,0); + XDestroyImage(nimage); + delete nshminfo; + return; + } else { + T *const ndata = (T*)nimage->data; + if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy); + else std::memset(ndata,0,sizeof(T)*ndimx*ndimy); + XShmDetach(dpy,_shminfo); + XDestroyImage(_image); + shmdt(_shminfo->shmaddr); + shmctl(_shminfo->shmid,IPC_RMID,0); + delete _shminfo; + _shminfo = nshminfo; + _image = nimage; + _data = (void*)ndata; + } + } + } + } + } else +#endif + { + T *ndata = (T*)std::malloc(ndimx*ndimy*sizeof(T)); + if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy); + else std::memset(ndata,0,sizeof(T)*ndimx*ndimy); + _data = (void*)ndata; + XDestroyImage(_image); + _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)), + cimg::X11_attr().nb_bits,ZPixmap,0,(char*)_data,ndimx,ndimy,8,0); + } + } + + void _init_fullscreen() { + if (!_is_fullscreen || _is_closed) return; + Display *const dpy = cimg::X11_attr().display; + _background_window = 0; + +#ifdef cimg_use_xrandr + int foo; + if (XRRQueryExtension(dpy,&foo,&foo)) { + XRRRotations(dpy,DefaultScreen(dpy),&cimg::X11_attr().curr_rotation); + if (!cimg::X11_attr().resolutions) { + cimg::X11_attr().resolutions = XRRSizes(dpy,DefaultScreen(dpy),&foo); + cimg::X11_attr().nb_resolutions = (unsigned int)foo; + } + if (cimg::X11_attr().resolutions) { + cimg::X11_attr().curr_resolution = 0; + for (unsigned int i = 0; i=_width && nh>=_height && + nw<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width) && + nh<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height)) + cimg::X11_attr().curr_resolution = i; + } + if (cimg::X11_attr().curr_resolution>0) { + XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy)); + XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy), + cimg::X11_attr().curr_resolution,cimg::X11_attr().curr_rotation,CurrentTime); + XRRFreeScreenConfigInfo(config); + XSync(dpy,0); + } + } + } + if (!cimg::X11_attr().resolutions) + cimg::warn(_cimgdisplay_instance + "init_fullscreen(): Xrandr extension not supported by the X server.", + cimgdisplay_instance); +#endif + + const unsigned int sx = screen_width(), sy = screen_height(); + if (sx==_width && sy==_height) return; + XSetWindowAttributes winattr; + winattr.override_redirect = 1; + _background_window = XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,sx,sy,0,0, + InputOutput,CopyFromParent,CWOverrideRedirect,&winattr); + const unsigned long buf_size = (unsigned long)sx*sy*(cimg::X11_attr().nb_bits==8?1: + (cimg::X11_attr().nb_bits==16?2:4)); + void *background_data = std::malloc(buf_size); + std::memset(background_data,0,buf_size); + XImage *background_image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits, + ZPixmap,0,(char*)background_data,sx,sy,8,0); + XEvent event; + XSelectInput(dpy,_background_window,StructureNotifyMask); + XMapRaised(dpy,_background_window); + do XWindowEvent(dpy,_background_window,StructureNotifyMask,&event); + while (event.type!=MapNotify); + GC gc = DefaultGC(dpy,DefaultScreen(dpy)); +#ifdef cimg_use_xshm + if (_shminfo) XShmPutImage(dpy,_background_window,gc,background_image,0,0,0,0,sx,sy,0); + else XPutImage(dpy,_background_window,gc,background_image,0,0,0,0,sx,sy); +#else + XPutImage(dpy,_background_window,gc,background_image,0,0,0,0,sx,sy); +#endif + XWindowAttributes attr; + XGetWindowAttributes(dpy,_background_window,&attr); + while (attr.map_state!=IsViewable) XSync(dpy,0); + XDestroyImage(background_image); + } + + void _desinit_fullscreen() { + if (!_is_fullscreen) return; + Display *const dpy = cimg::X11_attr().display; + XUngrabKeyboard(dpy,CurrentTime); +#ifdef cimg_use_xrandr + if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) { + XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy)); + XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy),0,cimg::X11_attr().curr_rotation,CurrentTime); + XRRFreeScreenConfigInfo(config); + XSync(dpy,0); + cimg::X11_attr().curr_resolution = 0; + } +#endif + if (_background_window) XDestroyWindow(dpy,_background_window); + _background_window = 0; + _is_fullscreen = false; + } + + static int _assign_xshm(Display *dpy, XErrorEvent *error) { + cimg::unused(dpy,error); + cimg::X11_attr().is_shm_enabled = false; + return 0; + } + + void _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + cimg::mutex(14); + + // Allocate space for window title + const char *const nptitle = ptitle?ptitle:""; + const unsigned int s = std::strlen(nptitle) + 1; + char *const tmp_title = s?new char[s]:0; + if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char)); + + // Destroy previous display window if existing + if (!is_empty()) assign(); + + // Open X11 display and retrieve graphical properties. + Display* &dpy = cimg::X11_attr().display; + if (!dpy) { + dpy = XOpenDisplay(0); + if (!dpy) + throw CImgDisplayException(_cimgdisplay_instance + "assign(): Failed to open X11 display.", + cimgdisplay_instance); + + cimg::X11_attr().nb_bits = DefaultDepth(dpy,DefaultScreen(dpy)); + if (cimg::X11_attr().nb_bits!=8 && cimg::X11_attr().nb_bits!=16 && + cimg::X11_attr().nb_bits!=24 && cimg::X11_attr().nb_bits!=32) + throw CImgDisplayException(_cimgdisplay_instance + "assign(): Invalid %u bits screen mode detected " + "(only 8, 16, 24 and 32 bits modes are managed).", + cimgdisplay_instance, + cimg::X11_attr().nb_bits); + XVisualInfo vtemplate; + vtemplate.visualid = XVisualIDFromVisual(DefaultVisual(dpy,DefaultScreen(dpy))); + int nb_visuals; + XVisualInfo *vinfo = XGetVisualInfo(dpy,VisualIDMask,&vtemplate,&nb_visuals); + if (vinfo && vinfo->red_maskblue_mask) cimg::X11_attr().is_blue_first = true; + cimg::X11_attr().byte_order = ImageByteOrder(dpy); + XFree(vinfo); + + XLockDisplay(dpy); + cimg::X11_attr().events_thread = new pthread_t; + pthread_create(cimg::X11_attr().events_thread,0,_events_thread,0); + } else XLockDisplay(dpy); + + // Set display variables. + _width = cimg::min(dimw,(unsigned int)screen_width()); + _height = cimg::min(dimh,(unsigned int)screen_height()); + _normalization = normalization_type<4?normalization_type:3; + _is_fullscreen = fullscreen_flag; + _window_x = _window_y = 0; + _is_closed = closed_flag; + _title = tmp_title; + flush(); + + // Create X11 window (and LUT, if 8bits display) + if (_is_fullscreen) { + if (!_is_closed) _init_fullscreen(); + const unsigned int sx = screen_width(), sy = screen_height(); + XSetWindowAttributes winattr; + winattr.override_redirect = 1; + _window = XCreateWindow(dpy,DefaultRootWindow(dpy),(sx-_width)/2,(sy-_height)/2,_width,_height,0,0, + InputOutput,CopyFromParent,CWOverrideRedirect,&winattr); + } else + _window = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),0,0,_width,_height,0,0L,0L); + + XSelectInput(dpy,_window, + ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask | PointerMotionMask | + EnterWindowMask | LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask); + + XStoreName(dpy,_window,_title?_title:" "); + if (cimg::X11_attr().nb_bits==8) { + _colormap = XCreateColormap(dpy,_window,DefaultVisual(dpy,DefaultScreen(dpy)),AllocAll); + _set_colormap(_colormap,3); + XSetWindowColormap(dpy,_window,_colormap); + } + + static const char *const _window_class = cimg_appname; + XClassHint *const window_class = XAllocClassHint(); + window_class->res_name = (char*)_window_class; + window_class->res_class = (char*)_window_class; + XSetClassHint(dpy,_window,window_class); + XFree(window_class); + + _window_width = _width; + _window_height = _height; + + // Create XImage +#ifdef cimg_use_xshm + _shminfo = 0; + if (XShmQueryExtension(dpy)) { + _shminfo = new XShmSegmentInfo; + _image = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits, + ZPixmap,0,_shminfo,_width,_height); + if (!_image) { delete _shminfo; _shminfo = 0; } + else { + _shminfo->shmid = shmget(IPC_PRIVATE,_image->bytes_per_line*_image->height,IPC_CREAT|0777); + if (_shminfo->shmid==-1) { XDestroyImage(_image); delete _shminfo; _shminfo = 0; } + else { + _shminfo->shmaddr = _image->data = (char*)(_data = shmat(_shminfo->shmid,0,0)); + if (_shminfo->shmaddr==(char*)-1) { + shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image); delete _shminfo; _shminfo = 0; + } else { + _shminfo->readOnly = 0; + cimg::X11_attr().is_shm_enabled = true; + XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm); + XShmAttach(dpy,_shminfo); + XSync(dpy,0); + XSetErrorHandler(oldXErrorHandler); + if (!cimg::X11_attr().is_shm_enabled) { + shmdt(_shminfo->shmaddr); shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image); + delete _shminfo; _shminfo = 0; + } + } + } + } + } + if (!_shminfo) +#endif + { + const unsigned long buf_size = (unsigned long)_width*_height*(cimg::X11_attr().nb_bits==8?1: + (cimg::X11_attr().nb_bits==16?2:4)); + _data = std::malloc(buf_size); + _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits, + ZPixmap,0,(char*)_data,_width,_height,8,0); + } + + _wm_window_atom = XInternAtom(dpy,"WM_DELETE_WINDOW",0); + _wm_protocol_atom = XInternAtom(dpy,"WM_PROTOCOLS",0); + XSetWMProtocols(dpy,_window,&_wm_window_atom,1); + + if (_is_fullscreen) XGrabKeyboard(dpy,_window,1,GrabModeAsync,GrabModeAsync,CurrentTime); + cimg::X11_attr().wins[cimg::X11_attr().nb_wins++]=this; + if (!_is_closed) _map_window(); else { _window_x = _window_y = cimg::type::min(); } + XUnlockDisplay(dpy); + cimg::mutex(14,0); + } + + CImgDisplay& assign() { + if (is_empty()) return flush(); + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + + // Remove display window from event thread list. + unsigned int i; + for (i = 0; ishmaddr); + shmctl(_shminfo->shmid,IPC_RMID,0); + delete _shminfo; + _shminfo = 0; + } else +#endif + XDestroyImage(_image); + _data = 0; _image = 0; + if (cimg::X11_attr().nb_bits==8) XFreeColormap(dpy,_colormap); + _colormap = 0; + XSync(dpy,0); + + // Reset display variables. + delete[] _title; + _width = _height = _normalization = _window_width = _window_height = 0; + _window_x = _window_y = 0; + _is_fullscreen = false; + _is_closed = true; + _min = _max = 0; + _title = 0; + flush(); + + XUnlockDisplay(dpy); + return *this; + } + + CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + if (!dimw || !dimh) return assign(); + _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag); + _min = _max = 0; + std::memset(_data,0,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char): + (cimg::X11_attr().nb_bits==16?sizeof(unsigned short):sizeof(unsigned int)))* + (unsigned long)_width*_height); + return paint(); + } + + template + CImgDisplay& assign(const CImg& img, const char *const title=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + if (!img) return assign(); + CImg tmp; + const CImg& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, + (img._height-1)/2, + (img._depth-1)/2)); + _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); + if (_normalization==2) _min = (float)nimg.min_max(_max); + return render(nimg).paint(); + } + + template + CImgDisplay& assign(const CImgList& list, const char *const title=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + if (!list) return assign(); + CImg tmp; + const CImg img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, + (img._height-1)/2, + (img._depth-1)/2)); + _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); + if (_normalization==2) _min = (float)nimg.min_max(_max); + return render(nimg).paint(); + } + + CImgDisplay& assign(const CImgDisplay& disp) { + if (!disp) return assign(); + _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed); + std::memcpy(_data,disp._data,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char): + cimg::X11_attr().nb_bits==16?sizeof(unsigned short): + sizeof(unsigned int))*(unsigned long)_width*_height); + return paint(); + } + + CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) { + if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign(); + if (is_empty()) return assign(nwidth,nheight); + Display *const dpy = cimg::X11_attr().display; + const unsigned int + tmpdimx = (nwidth>0)?nwidth:(-nwidth*width()/100), + tmpdimy = (nheight>0)?nheight:(-nheight*height()/100), + dimx = tmpdimx?tmpdimx:1, + dimy = tmpdimy?tmpdimy:1; + XLockDisplay(dpy); + if (_window_width!=dimx || _window_height!=dimy) { + XWindowAttributes attr; + for (unsigned int i = 0; i<10; ++i) { + XResizeWindow(dpy,_window,dimx,dimy); + XGetWindowAttributes(dpy,_window,&attr); + if (attr.width==(int)dimx && attr.height==(int)dimy) break; + cimg::wait(5); + } + } + if (_width!=dimx || _height!=dimy) switch (cimg::X11_attr().nb_bits) { + case 8 : { unsigned char pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break; + case 16 : { unsigned short pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break; + default : { unsigned int pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } + } + _window_width = _width = dimx; _window_height = _height = dimy; + _is_resized = false; + XUnlockDisplay(dpy); + if (_is_fullscreen) move((screen_width()-_width)/2,(screen_height()-_height)/2); + if (force_redraw) return paint(); + return *this; + } + + CImgDisplay& toggle_fullscreen(const bool force_redraw=true) { + if (is_empty()) return *this; + if (force_redraw) { + const unsigned long buf_size = (unsigned long)_width*_height* + (cimg::X11_attr().nb_bits==8?1:(cimg::X11_attr().nb_bits==16?2:4)); + void *image_data = std::malloc(buf_size); + std::memcpy(image_data,_data,buf_size); + assign(_width,_height,_title,_normalization,!_is_fullscreen,false); + std::memcpy(_data,image_data,buf_size); + std::free(image_data); + return paint(); + } + return assign(_width,_height,_title,_normalization,!_is_fullscreen,false); + } + + CImgDisplay& show() { + if (is_empty() || !_is_closed) return *this; + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + if (_is_fullscreen) _init_fullscreen(); + _map_window(); + _is_closed = false; + XUnlockDisplay(dpy); + return paint(); + } + + CImgDisplay& close() { + if (is_empty() || _is_closed) return *this; + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + if (_is_fullscreen) _desinit_fullscreen(); + XUnmapWindow(dpy,_window); + _window_x = _window_y = -1; + _is_closed = true; + XUnlockDisplay(dpy); + return *this; + } + + CImgDisplay& move(const int posx, const int posy) { + if (is_empty()) return *this; + show(); + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + XMoveWindow(dpy,_window,posx,posy); + _window_x = posx; _window_y = posy; + _is_moved = false; + XUnlockDisplay(dpy); + return paint(); + } + + CImgDisplay& show_mouse() { + if (is_empty()) return *this; + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + XUndefineCursor(dpy,_window); + XUnlockDisplay(dpy); + return *this; + } + + CImgDisplay& hide_mouse() { + if (is_empty()) return *this; + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + const char pix_data[8] = { 0 }; + XColor col; + col.red = col.green = col.blue = 0; + Pixmap pix = XCreateBitmapFromData(dpy,_window,pix_data,8,8); + Cursor cur = XCreatePixmapCursor(dpy,pix,pix,&col,&col,0,0); + XFreePixmap(dpy,pix); + XDefineCursor(dpy,_window,cur); + XUnlockDisplay(dpy); + return *this; + } + + CImgDisplay& set_mouse(const int posx, const int posy) { + if (is_empty() || _is_closed) return *this; + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + XWarpPointer(dpy,0L,_window,0,0,0,0,posx,posy); + _mouse_x = posx; _mouse_y = posy; + _is_moved = false; + XSync(dpy,0); + XUnlockDisplay(dpy); + return *this; + } + + CImgDisplay& set_title(const char *const format, ...) { + if (is_empty()) return *this; + char tmp[1024] = { 0 }; + va_list ap; + va_start(ap, format); + cimg_vsnprintf(tmp,sizeof(tmp),format,ap); + va_end(ap); + if (!std::strcmp(_title,tmp)) return *this; + delete[] _title; + const unsigned int s = std::strlen(tmp) + 1; + _title = new char[s]; + std::memcpy(_title,tmp,s*sizeof(char)); + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + XStoreName(dpy,_window,tmp); + XUnlockDisplay(dpy); + return *this; + } + + template + CImgDisplay& display(const CImg& img) { + if (!img) + throw CImgArgumentException(_cimgdisplay_instance + "display(): Empty specified image.", + cimgdisplay_instance); + if (is_empty()) return assign(img); + return render(img).paint(false); + } + + CImgDisplay& paint(const bool wait_expose=true) { + if (is_empty()) return *this; + Display *const dpy = cimg::X11_attr().display; + XLockDisplay(dpy); + _paint(wait_expose); + XUnlockDisplay(dpy); + return *this; + } + + template + CImgDisplay& render(const CImg& img, const bool flag8=false) { + if (!img) + throw CImgArgumentException(_cimgdisplay_instance + "render(): Empty specified image.", + cimgdisplay_instance); + if (is_empty()) return *this; + if (img._depth!=1) return render(img.get_projections2d((img._width-1)/2,(img._height-1)/2,(img._depth-1)/2)); + if (cimg::X11_attr().nb_bits==8 && (img._width!=_width || img._height!=_height)) + return render(img.get_resize(_width,_height,1,-100,1)); + if (cimg::X11_attr().nb_bits==8 && !flag8 && img._spectrum==3) { + static const CImg::ucharT> default_colormap = CImg::ucharT>::default_LUT256(); + return render(img.get_index(default_colormap,1,false)); + } + + Display *const dpy = cimg::X11_attr().display; + const T + *data1 = img._data, + *data2 = (img._spectrum>1)?img.data(0,0,0,1):data1, + *data3 = (img._spectrum>2)?img.data(0,0,0,2):data1; + + if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3); + XLockDisplay(dpy); + + if (!_normalization || (_normalization==3 && cimg::type::string()==cimg::type::string())) { + _min = _max = 0; + switch (cimg::X11_attr().nb_bits) { + case 8 : { // 256 colormap, no normalization + _set_colormap(_colormap,img._spectrum); + unsigned char *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:new unsigned char[(unsigned long)img._width*img._height]; + unsigned char *ptrd = (unsigned char*)ndata; + switch (img._spectrum) { + case 1 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) (*ptrd++) = (unsigned char)*(data1++); + break; + case 2 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char R = (unsigned char)*(data1++), G = (unsigned char)*(data2++); + (*ptrd++) = (R&0xf0) | (G>>4); + } break; + default : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char R = (unsigned char)*(data1++), G = (unsigned char)*(data2++), B = (unsigned char)*(data3++); + (*ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6); + } + } + if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height); delete[] ndata; } + } break; + case 16 : { // 16 bits colors, no normalization + unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:new unsigned short[(unsigned long)img._width*img._height]; + unsigned char *ptrd = (unsigned char*)ndata; + const unsigned int M = 248; + switch (img._spectrum) { + case 1 : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)*(data1++), G = val>>2; + *(ptrd++) = (val&M) | (G>>3); + *(ptrd++) = (G<<5) | (G>>1); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)*(data1++), G = val>>2; + *(ptrd++) = (G<<5) | (G>>1); + *(ptrd++) = (val&M) | (G>>3); + } + break; + case 2 : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)*(data2++)>>2; + *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); + *(ptrd++) = (G<<5); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)*(data2++)>>2; + *(ptrd++) = (G<<5); + *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); + } + break; + default : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)*(data2++)>>2; + *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); + *(ptrd++) = (G<<5) | ((unsigned char)*(data3++)>>3); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)*(data2++)>>2; + *(ptrd++) = (G<<5) | ((unsigned char)*(data3++)>>3); + *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); + } + } + if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height); delete[] ndata; } + } break; + default : { // 24 bits colors, no normalization + unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:new unsigned int[(unsigned long)img._width*img._height]; + if (sizeof(int)==4) { // 32 bits int uses optimized version + unsigned int *ptrd = ndata; + switch (img._spectrum) { + case 1 : + if (cimg::X11_attr().byte_order==cimg::endianness()) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)*(data1++); + *(ptrd++) = (val<<16) | (val<<8) | val; + } + else + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)*(data1++); + *(ptrd++) = (val<<16) | (val<<8) | val; + } + break; + case 2 : + if (cimg::X11_attr().byte_order==cimg::endianness()) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8); + else + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = ((unsigned char)*(data2++)<<16) | ((unsigned char)*(data1++)<<8); + break; + default : + if (cimg::X11_attr().byte_order==cimg::endianness()) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8) | (unsigned char)*(data3++); + else + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = ((unsigned char)*(data3++)<<24) | ((unsigned char)*(data2++)<<16) | ((unsigned char)*(data1++)<<8); + } + } else { + unsigned char *ptrd = (unsigned char*)ndata; + switch (img._spectrum) { + case 1 : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + *(ptrd++) = 0; + *(ptrd++) = (unsigned char)*(data1++); + *(ptrd++) = 0; + *(ptrd++) = 0; + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + *(ptrd++) = 0; + *(ptrd++) = 0; + *(ptrd++) = (unsigned char)*(data1++); + *(ptrd++) = 0; + } + break; + case 2 : + if (cimg::X11_attr().byte_order) cimg::swap(data1,data2); + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + *(ptrd++) = 0; + *(ptrd++) = (unsigned char)*(data2++); + *(ptrd++) = (unsigned char)*(data1++); + *(ptrd++) = 0; + } + break; + default : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + *(ptrd++) = 0; + *(ptrd++) = (unsigned char)*(data1++); + *(ptrd++) = (unsigned char)*(data2++); + *(ptrd++) = (unsigned char)*(data3++); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + *(ptrd++) = (unsigned char)*(data3++); + *(ptrd++) = (unsigned char)*(data2++); + *(ptrd++) = (unsigned char)*(data1++); + *(ptrd++) = 0; + } + } + } + if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height); delete[] ndata; } + } + } + } else { + if (_normalization==3) { + if (cimg::type::is_float()) _min = (float)img.min_max(_max); + else { _min = (float)cimg::type::min(); _max = (float)cimg::type::max(); } + } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max); + const float delta = _max - _min, mm = 255/(delta?delta:1.0f); + switch (cimg::X11_attr().nb_bits) { + case 8 : { // 256 colormap, with normalization + _set_colormap(_colormap,img._spectrum); + unsigned char *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:new unsigned char[(unsigned long)img._width*img._height]; + unsigned char *ptrd = (unsigned char*)ndata; + switch (img._spectrum) { + case 1 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char R = (unsigned char)((*(data1++)-_min)*mm); + *(ptrd++) = R; + } break; + case 2 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char + R = (unsigned char)((*(data1++)-_min)*mm), + G = (unsigned char)((*(data2++)-_min)*mm); + (*ptrd++) = (R&0xf0) | (G>>4); + } break; + default : + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char + R = (unsigned char)((*(data1++)-_min)*mm), + G = (unsigned char)((*(data2++)-_min)*mm), + B = (unsigned char)((*(data3++)-_min)*mm); + *(ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6); + } + } + if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height); delete[] ndata; } + } break; + case 16 : { // 16 bits colors, with normalization + unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:new unsigned short[(unsigned long)img._width*img._height]; + unsigned char *ptrd = (unsigned char*)ndata; + const unsigned int M = 248; + switch (img._spectrum) { + case 1 : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm), G = val>>2; + *(ptrd++) = (val&M) | (G>>3); + *(ptrd++) = (G<<5) | (val>>3); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm), G = val>>2; + *(ptrd++) = (G<<5) | (val>>3); + *(ptrd++) = (val&M) | (G>>3); + } + break; + case 2 : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; + *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); + *(ptrd++) = (G<<5); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; + *(ptrd++) = (G<<5); + *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); + } + break; + default : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; + *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); + *(ptrd++) = (G<<5) | ((unsigned char)((*(data3++)-_min)*mm)>>3); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; + *(ptrd++) = (G<<5) | ((unsigned char)((*(data3++)-_min)*mm)>>3); + *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); + } + } + if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height); delete[] ndata; } + } break; + default : { // 24 bits colors, with normalization + unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:new unsigned int[(unsigned long)img._width*img._height]; + if (sizeof(int)==4) { // 32 bits int uses optimized version + unsigned int *ptrd = ndata; + switch (img._spectrum) { + case 1 : + if (cimg::X11_attr().byte_order==cimg::endianness()) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); + *(ptrd++) = (val<<16) | (val<<8) | val; + } + else + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); + *(ptrd++) = (val<<24) | (val<<16) | (val<<8); + } + break; + case 2 : + if (cimg::X11_attr().byte_order==cimg::endianness()) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = + ((unsigned char)((*(data1++)-_min)*mm)<<16) | + ((unsigned char)((*(data2++)-_min)*mm)<<8); + else + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = + ((unsigned char)((*(data2++)-_min)*mm)<<16) | + ((unsigned char)((*(data1++)-_min)*mm)<<8); + break; + default : + if (cimg::X11_attr().byte_order==cimg::endianness()) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = + ((unsigned char)((*(data1++)-_min)*mm)<<16) | + ((unsigned char)((*(data2++)-_min)*mm)<<8) | + (unsigned char)((*(data3++)-_min)*mm); + else + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = + ((unsigned char)((*(data3++)-_min)*mm)<<24) | + ((unsigned char)((*(data2++)-_min)*mm)<<16) | + ((unsigned char)((*(data1++)-_min)*mm)<<8); + } + } else { + unsigned char *ptrd = (unsigned char*)ndata; + switch (img._spectrum) { + case 1 : + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); + (*ptrd++) = 0; + (*ptrd++) = val; + (*ptrd++) = val; + (*ptrd++) = val; + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); + (*ptrd++) = val; + (*ptrd++) = val; + (*ptrd++) = val; + (*ptrd++) = 0; + } + break; + case 2 : + if (cimg::X11_attr().byte_order) cimg::swap(data1,data2); + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + (*ptrd++) = 0; + (*ptrd++) = (unsigned char)((*(data2++)-_min)*mm); + (*ptrd++) = (unsigned char)((*(data1++)-_min)*mm); + (*ptrd++) = 0; + } + break; + default : + if (cimg::X11_attr().byte_order) + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + (*ptrd++) = 0; + (*ptrd++) = (unsigned char)((*(data1++)-_min)*mm); + (*ptrd++) = (unsigned char)((*(data2++)-_min)*mm); + (*ptrd++) = (unsigned char)((*(data3++)-_min)*mm); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + (*ptrd++) = (unsigned char)((*(data3++)-_min)*mm); + (*ptrd++) = (unsigned char)((*(data2++)-_min)*mm); + (*ptrd++) = (unsigned char)((*(data1++)-_min)*mm); + (*ptrd++) = 0; + } + } + } + if (ndata!=_data) { + _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height); delete[] ndata; + } + } + } + } + XUnlockDisplay(dpy); + return *this; + } + + template + const CImgDisplay& snapshot(CImg& img) const { + if (is_empty()) { img.assign(); return *this; } + const unsigned char *ptrs = (unsigned char*)_data; + img.assign(_width,_height,1,3); + T + *data1 = img.data(0,0,0,0), + *data2 = img.data(0,0,0,1), + *data3 = img.data(0,0,0,2); + if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3); + switch (cimg::X11_attr().nb_bits) { + case 8 : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = *(ptrs++); + *(data1++) = (T)(val&0xe0); + *(data2++) = (T)((val&0x1c)<<3); + *(data3++) = (T)(val<<6); + } + } break; + case 16 : { + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val0 = *(ptrs++), val1 = *(ptrs++); + *(data1++) = (T)(val0&0xf8); + *(data2++) = (T)((val0<<5) | ((val1&0xe0)>>5)); + *(data3++) = (T)(val1<<3); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned short val0 = *(ptrs++), val1 = *(ptrs++); + *(data1++) = (T)(val1&0xf8); + *(data2++) = (T)((val1<<5) | ((val0&0xe0)>>5)); + *(data3++) = (T)(val0<<3); + } + } break; + default : { + if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + ++ptrs; + *(data1++) = (T)*(ptrs++); + *(data2++) = (T)*(ptrs++); + *(data3++) = (T)*(ptrs++); + } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + *(data3++) = (T)*(ptrs++); + *(data2++) = (T)*(ptrs++); + *(data1++) = (T)*(ptrs++); + ++ptrs; + } + } + } + return *this; + } + + // Windows-based implementation. + //------------------------------- +#elif cimg_display==2 + + bool _is_mouse_tracked, _is_cursor_visible; + HANDLE _thread, _is_created, _mutex; + HWND _window, _background_window; + CLIENTCREATESTRUCT _ccs; + unsigned int *_data; + DEVMODE _curr_mode; + BITMAPINFO _bmi; + HDC _hdc; + + static int screen_width() { + DEVMODE mode; + mode.dmSize = sizeof(DEVMODE); + mode.dmDriverExtra = 0; + EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode); + return mode.dmPelsWidth; + } + + static int screen_height() { + DEVMODE mode; + mode.dmSize = sizeof(DEVMODE); + mode.dmDriverExtra = 0; + EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode); + return mode.dmPelsHeight; + } + + static void wait_all() { + WaitForSingleObject(cimg::Win32_attr().wait_event,INFINITE); + } + + static LRESULT APIENTRY _handle_events(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) { +#ifdef _WIN64 + CImgDisplay *const disp = (CImgDisplay*)GetWindowLongPtr(window,GWLP_USERDATA); +#else + CImgDisplay *const disp = (CImgDisplay*)GetWindowLong(window,GWL_USERDATA); +#endif + MSG st_msg; + switch (msg) { + case WM_CLOSE : + disp->_mouse_x = disp->_mouse_y = -1; + disp->_window_x = disp->_window_y = 0; + disp->set_button().set_key(0).set_key(0,false)._is_closed = true; + ReleaseMutex(disp->_mutex); + ShowWindow(disp->_window,SW_HIDE); + disp->_is_event = true; + SetEvent(cimg::Win32_attr().wait_event); + return 0; + case WM_SIZE : { + while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {} + WaitForSingleObject(disp->_mutex,INFINITE); + const unsigned int nw = LOWORD(lParam),nh = HIWORD(lParam); + if (nw && nh && (nw!=disp->_width || nh!=disp->_height)) { + disp->_window_width = nw; + disp->_window_height = nh; + disp->_mouse_x = disp->_mouse_y = -1; + disp->_is_resized = disp->_is_event = true; + SetEvent(cimg::Win32_attr().wait_event); + } + ReleaseMutex(disp->_mutex); + } break; + case WM_MOVE : { + while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {} + WaitForSingleObject(disp->_mutex,INFINITE); + const int nx = (int)(short)(LOWORD(lParam)), ny = (int)(short)(HIWORD(lParam)); + if (nx!=disp->_window_x || ny!=disp->_window_y) { + disp->_window_x = nx; + disp->_window_y = ny; + disp->_is_moved = disp->_is_event = true; + SetEvent(cimg::Win32_attr().wait_event); + } + ReleaseMutex(disp->_mutex); + } break; + case WM_PAINT : + disp->paint(); + if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE)>=0); + break; + case WM_KEYDOWN : + disp->set_key((unsigned int)wParam); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_KEYUP : + disp->set_key((unsigned int)wParam,false); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_MOUSEMOVE : { + while (PeekMessage(&st_msg,window,WM_MOUSEMOVE,WM_MOUSEMOVE,PM_REMOVE)) {} + disp->_mouse_x = LOWORD(lParam); + disp->_mouse_y = HIWORD(lParam); +#if (_WIN32_WINNT>=0x0400) && !defined(NOTRACKMOUSEEVENT) + if (!disp->_is_mouse_tracked) { + TRACKMOUSEEVENT tme; + tme.cbSize = sizeof(TRACKMOUSEEVENT); + tme.dwFlags = TME_LEAVE; + tme.hwndTrack = disp->_window; + if (TrackMouseEvent(&tme)) disp->_is_mouse_tracked = true; + } +#endif + if (disp->_mouse_x<0 || disp->_mouse_y<0 || disp->_mouse_x>=disp->width() || disp->_mouse_y>=disp->height()) + disp->_mouse_x = disp->_mouse_y = -1; + disp->_is_event = true; + SetEvent(cimg::Win32_attr().wait_event); + if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE)>=0); + } break; + case WM_MOUSELEAVE : { + disp->_mouse_x = disp->_mouse_y = -1; + disp->_is_mouse_tracked = false; + while (ShowCursor(TRUE)<0); + } break; + case WM_LBUTTONDOWN : + disp->set_button(1); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_RBUTTONDOWN : + disp->set_button(2); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_MBUTTONDOWN : + disp->set_button(3); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_LBUTTONUP : + disp->set_button(1,false); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_RBUTTONUP : + disp->set_button(2,false); + SetEvent(cimg::Win32_attr().wait_event); + break; + case WM_MBUTTONUP : + disp->set_button(3,false); + SetEvent(cimg::Win32_attr().wait_event); + break; + case 0x020A : // WM_MOUSEWHEEL: + disp->set_wheel((int)((short)HIWORD(wParam))/120); + SetEvent(cimg::Win32_attr().wait_event); + } + return DefWindowProc(window,msg,wParam,lParam); + } + + static DWORD WINAPI _events_thread(void* arg) { + CImgDisplay *const disp = (CImgDisplay*)(((void**)arg)[0]); + const char *const title = (const char*)(((void**)arg)[1]); + MSG msg; + delete[] (void**)arg; + disp->_bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + disp->_bmi.bmiHeader.biWidth = disp->width(); + disp->_bmi.bmiHeader.biHeight = -disp->height(); + disp->_bmi.bmiHeader.biPlanes = 1; + disp->_bmi.bmiHeader.biBitCount = 32; + disp->_bmi.bmiHeader.biCompression = BI_RGB; + disp->_bmi.bmiHeader.biSizeImage = 0; + disp->_bmi.bmiHeader.biXPelsPerMeter = 1; + disp->_bmi.bmiHeader.biYPelsPerMeter = 1; + disp->_bmi.bmiHeader.biClrUsed = 0; + disp->_bmi.bmiHeader.biClrImportant = 0; + disp->_data = new unsigned int[(unsigned long)disp->_width*disp->_height]; + if (!disp->_is_fullscreen) { // Normal window + RECT rect; + rect.left = rect.top = 0; rect.right = disp->_width-1; rect.bottom = disp->_height-1; + AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); + const int + border1 = (rect.right - rect.left + 1 - disp->_width)/2, + border2 = rect.bottom - rect.top + 1 - disp->_height - border1; + disp->_window = CreateWindowA("MDICLIENT",title?title:" ", + WS_OVERLAPPEDWINDOW | (disp->_is_closed?0:WS_VISIBLE), CW_USEDEFAULT,CW_USEDEFAULT, + disp->_width + 2*border1, disp->_height + border1 + border2, + 0,0,0,&(disp->_ccs)); + if (!disp->_is_closed) { + GetWindowRect(disp->_window,&rect); + disp->_window_x = rect.left + border1; + disp->_window_y = rect.top + border2; + } else disp->_window_x = disp->_window_y = 0; + } else { // Fullscreen window + const unsigned int sx = screen_width(), sy = screen_height(); + disp->_window = CreateWindowA("MDICLIENT",title?title:" ", + WS_POPUP | (disp->_is_closed?0:WS_VISIBLE), (sx-disp->_width)/2, + (sy-disp->_height)/2, + disp->_width,disp->_height,0,0,0,&(disp->_ccs)); + disp->_window_x = disp->_window_y = 0; + } + SetForegroundWindow(disp->_window); + disp->_hdc = GetDC(disp->_window); + disp->_window_width = disp->_width; + disp->_window_height = disp->_height; + disp->flush(); +#ifdef _WIN64 + SetWindowLongPtr(disp->_window,GWLP_USERDATA,(LONG_PTR)disp); + SetWindowLongPtr(disp->_window,GWLP_WNDPROC,(LONG_PTR)_handle_events); +#else + SetWindowLong(disp->_window,GWL_USERDATA,(LONG)disp); + SetWindowLong(disp->_window,GWL_WNDPROC,(LONG)_handle_events); +#endif + SetEvent(disp->_is_created); + while (GetMessage(&msg,0,0,0)) DispatchMessage(&msg); + return 0; + } + + CImgDisplay& _update_window_pos() { + if (_is_closed) _window_x = _window_y = -1; + else { + RECT rect; + rect.left = rect.top = 0; rect.right = _width-1; rect.bottom = _height-1; + AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); + const int + border1 = (rect.right - rect.left + 1 - _width)/2, + border2 = rect.bottom - rect.top + 1 - _height - border1; + GetWindowRect(_window,&rect); + _window_x = rect.left + border1; + _window_y = rect.top + border2; + } + return *this; + } + + void _init_fullscreen() { + _background_window = 0; + if (!_is_fullscreen || _is_closed) _curr_mode.dmSize = 0; + else { + DEVMODE mode; + unsigned int imode = 0, ibest = 0, bestbpp = 0, bw = ~0U, bh = ~0U; + for (mode.dmSize = sizeof(DEVMODE), mode.dmDriverExtra = 0; EnumDisplaySettings(0,imode,&mode); ++imode) { + const unsigned int nw = mode.dmPelsWidth, nh = mode.dmPelsHeight; + if (nw>=_width && nh>=_height && mode.dmBitsPerPel>=bestbpp && nw<=bw && nh<=bh) { + bestbpp = mode.dmBitsPerPel; + ibest = imode; + bw = nw; bh = nh; + } + } + if (bestbpp) { + _curr_mode.dmSize = sizeof(DEVMODE); _curr_mode.dmDriverExtra = 0; + EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&_curr_mode); + EnumDisplaySettings(0,ibest,&mode); + ChangeDisplaySettings(&mode,0); + } else _curr_mode.dmSize = 0; + + const unsigned int sx = screen_width(), sy = screen_height(); + if (sx!=_width || sy!=_height) { + CLIENTCREATESTRUCT background_ccs; + _background_window = CreateWindowA("MDICLIENT","",WS_POPUP | WS_VISIBLE, 0,0,sx,sy,0,0,0,&background_ccs); + SetForegroundWindow(_background_window); + } + } + } + + void _desinit_fullscreen() { + if (!_is_fullscreen) return; + if (_background_window) DestroyWindow(_background_window); + _background_window = 0; + if (_curr_mode.dmSize) ChangeDisplaySettings(&_curr_mode,0); + _is_fullscreen = false; + } + + CImgDisplay& _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + + // Allocate space for window title + const char *const nptitle = ptitle?ptitle:""; + const unsigned int s = (unsigned int)std::strlen(nptitle) + 1; + char *const tmp_title = s?new char[s]:0; + if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char)); + + // Destroy previous window if existing + if (!is_empty()) assign(); + + // Set display variables + _width = cimg::min(dimw,(unsigned int)screen_width()); + _height = cimg::min(dimh,(unsigned int)screen_height()); + _normalization = normalization_type<4?normalization_type:3; + _is_fullscreen = fullscreen_flag; + _window_x = _window_y = 0; + _is_closed = closed_flag; + _is_cursor_visible = true; + _is_mouse_tracked = false; + _title = tmp_title; + flush(); + if (_is_fullscreen) _init_fullscreen(); + + // Create event thread + void *const arg = (void*)(new void*[2]); + ((void**)arg)[0] = (void*)this; + ((void**)arg)[1] = (void*)_title; + _mutex = CreateMutex(0,FALSE,0); + _is_created = CreateEvent(0,FALSE,FALSE,0); + _thread = CreateThread(0,0,_events_thread,arg,0,0); + WaitForSingleObject(_is_created,INFINITE); + return *this; + } + + CImgDisplay& assign() { + if (is_empty()) return flush(); + DestroyWindow(_window); + TerminateThread(_thread,0); + delete[] _data; + delete[] _title; + _data = 0; + _title = 0; + if (_is_fullscreen) _desinit_fullscreen(); + _width = _height = _normalization = _window_width = _window_height = 0; + _window_x = _window_y = 0; + _is_fullscreen = false; + _is_closed = true; + _min = _max = 0; + _title = 0; + flush(); + return *this; + } + + CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + if (!dimw || !dimh) return assign(); + _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag); + _min = _max = 0; + std::memset(_data,0,sizeof(unsigned int)*_width*_height); + return paint(); + } + + template + CImgDisplay& assign(const CImg& img, const char *const title=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + if (!img) return assign(); + CImg tmp; + const CImg& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, + (img._height-1)/2, + (img._depth-1)/2)); + _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); + if (_normalization==2) _min = (float)nimg.min_max(_max); + return display(nimg); + } + + template + CImgDisplay& assign(const CImgList& list, const char *const title=0, + const unsigned int normalization_type=3, + const bool fullscreen_flag=false, const bool closed_flag=false) { + if (!list) return assign(); + CImg tmp; + const CImg img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, + (img._height-1)/2, + (img._depth-1)/2)); + _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); + if (_normalization==2) _min = (float)nimg.min_max(_max); + return display(nimg); + } + + CImgDisplay& assign(const CImgDisplay& disp) { + if (!disp) return assign(); + _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed); + std::memcpy(_data,disp._data,sizeof(unsigned int)*_width*_height); + return paint(); + } + + CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) { + if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign(); + if (is_empty()) return assign(nwidth,nheight); + const unsigned int + tmpdimx = (nwidth>0)?nwidth:(-nwidth*_width/100), + tmpdimy = (nheight>0)?nheight:(-nheight*_height/100), + dimx = tmpdimx?tmpdimx:1, + dimy = tmpdimy?tmpdimy:1; + if (_window_width!=dimx || _window_height!=dimy) { + RECT rect; rect.left = rect.top = 0; rect.right = dimx - 1; rect.bottom = dimy - 1; + AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); + const int cwidth = rect.right - rect.left + 1, cheight = rect.bottom - rect.top + 1; + SetWindowPos(_window,0,0,0,cwidth,cheight,SWP_NOMOVE | SWP_NOZORDER | SWP_NOCOPYBITS); + } + if (_width!=dimx || _height!=dimy) { + unsigned int *const ndata = new unsigned int[dimx*dimy]; + if (force_redraw) _render_resize(_data,_width,_height,ndata,dimx,dimy); + else std::memset(ndata,0x80,sizeof(unsigned int)*dimx*dimy); + delete[] _data; + _data = ndata; + _bmi.bmiHeader.biWidth = dimx; + _bmi.bmiHeader.biHeight = -(int)dimy; + _width = dimx; + _height = dimy; + } + _window_width = dimx; _window_height = dimy; + _is_resized = false; + if (_is_fullscreen) move((screen_width()-_width)/2,(screen_height()-_height)/2); + if (force_redraw) return paint(); + return *this; + } + + CImgDisplay& toggle_fullscreen(const bool force_redraw=true) { + if (is_empty()) return *this; + if (force_redraw) { + const unsigned long buf_size = _width*_height*4UL; + void *odata = std::malloc(buf_size); + std::memcpy(odata,_data,buf_size); + assign(_width,_height,_title,_normalization,!_is_fullscreen,false); + std::memcpy(_data,odata,buf_size); + std::free(odata); + return paint(); + } + return assign(_width,_height,_title,_normalization,!_is_fullscreen,false); + } + + CImgDisplay& show() { + if (is_empty() || !_is_closed) return *this; + _is_closed = false; + if (_is_fullscreen) _init_fullscreen(); + ShowWindow(_window,SW_SHOW); + _update_window_pos(); + return paint(); + } + + CImgDisplay& close() { + if (is_empty() || _is_closed) return *this; + _is_closed = true; + if (_is_fullscreen) _desinit_fullscreen(); + ShowWindow(_window,SW_HIDE); + _window_x = _window_y = 0; + return *this; + } + + CImgDisplay& move(const int posx, const int posy) { + if (is_empty()) return *this; + if (!_is_fullscreen) { + RECT rect; rect.left = rect.top = 0; rect.right = _window_width-1; rect.bottom = _window_height-1; + AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); + const int border1 = (rect.right-rect.left+1-_width)/2, border2 = rect.bottom-rect.top+1-_height-border1; + SetWindowPos(_window,0,posx-border1,posy-border2,0,0,SWP_NOSIZE | SWP_NOZORDER); + } else SetWindowPos(_window,0,posx,posy,0,0,SWP_NOSIZE | SWP_NOZORDER); + _window_x = posx; + _window_y = posy; + _is_moved = false; + return show(); + } + + CImgDisplay& show_mouse() { + if (is_empty()) return *this; + _is_cursor_visible = true; + return *this; + } + + CImgDisplay& hide_mouse() { + if (is_empty()) return *this; + _is_cursor_visible = false; + return *this; + } + + CImgDisplay& set_mouse(const int posx, const int posy) { + if (_is_closed || posx<0 || posy<0) return *this; + _update_window_pos(); + const int res = (int)SetCursorPos(_window_x + posx,_window_y + posy); + if (res) { _mouse_x = posx; _mouse_y = posy; } + return *this; + } + + CImgDisplay& set_title(const char *const format, ...) { + if (is_empty()) return *this; + char tmp[1024] = { 0 }; + va_list ap; + va_start(ap, format); + cimg_vsnprintf(tmp,sizeof(tmp),format,ap); + va_end(ap); + if (!std::strcmp(_title,tmp)) return *this; + delete[] _title; + const unsigned int s = (unsigned int)std::strlen(tmp) + 1; + _title = new char[s]; + std::memcpy(_title,tmp,s*sizeof(char)); + SetWindowTextA(_window, tmp); + return *this; + } + + template + CImgDisplay& display(const CImg& img) { + if (!img) + throw CImgArgumentException(_cimgdisplay_instance + "display(): Empty specified image.", + cimgdisplay_instance); + if (is_empty()) return assign(img); + return render(img).paint(); + } + + CImgDisplay& paint() { + if (_is_closed) return *this; + WaitForSingleObject(_mutex,INFINITE); + SetDIBitsToDevice(_hdc,0,0,_width,_height,0,0,0,_height,_data,&_bmi,DIB_RGB_COLORS); + ReleaseMutex(_mutex); + return *this; + } + + template + CImgDisplay& render(const CImg& img) { + if (!img) + throw CImgArgumentException(_cimgdisplay_instance + "render(): Empty specified image.", + cimgdisplay_instance); + + if (is_empty()) return *this; + if (img._depth!=1) return render(img.get_projections2d((img._width-1)/2,(img._height-1)/2,(img._depth-1)/2)); + + const T + *data1 = img._data, + *data2 = (img._spectrum>=2)?img.data(0,0,0,1):data1, + *data3 = (img._spectrum>=3)?img.data(0,0,0,2):data1; + + WaitForSingleObject(_mutex,INFINITE); + unsigned int + *const ndata = (img._width==_width && img._height==_height)?_data: + new unsigned int[(unsigned long)img._width*img._height], + *ptrd = ndata; + + if (!_normalization || (_normalization==3 && cimg::type::string()==cimg::type::string())) { + _min = _max = 0; + switch (img._spectrum) { + case 1 : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)*(data1++); + *(ptrd++) = (val<<16) | (val<<8) | val; + } + } break; + case 2 : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8); + } break; + default : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) + *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8) | (unsigned char)*(data3++); + } + } + } else { + if (_normalization==3) { + if (cimg::type::is_float()) _min = (float)img.min_max(_max); + else { _min = (float)cimg::type::min(); _max = (float)cimg::type::max(); } + } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max); + const float delta = _max - _min, mm = 255/(delta?delta:1.0f); + switch (img._spectrum) { + case 1 : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); + *(ptrd++) = (val<<16) | (val<<8) | val; + } + } break; + case 2 : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char + R = (unsigned char)((*(data1++)-_min)*mm), + G = (unsigned char)((*(data2++)-_min)*mm); + *(ptrd++) = (R<<16) | (G<<8); + } + } break; + default : { + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned char + R = (unsigned char)((*(data1++)-_min)*mm), + G = (unsigned char)((*(data2++)-_min)*mm), + B = (unsigned char)((*(data3++)-_min)*mm); + *(ptrd++) = (R<<16) | (G<<8) | B; + } + } + } + } + if (ndata!=_data) { _render_resize(ndata,img._width,img._height,_data,_width,_height); delete[] ndata; } + ReleaseMutex(_mutex); + return *this; + } + + template + const CImgDisplay& snapshot(CImg& img) const { + if (is_empty()) { img.assign(); return *this; } + const unsigned int *ptrs = _data; + img.assign(_width,_height,1,3); + T + *data1 = img.data(0,0,0,0), + *data2 = img.data(0,0,0,1), + *data3 = img.data(0,0,0,2); + for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { + const unsigned int val = *(ptrs++); + *(data1++) = (T)(unsigned char)(val>>16); + *(data2++) = (T)(unsigned char)((val>>8)&0xFF); + *(data3++) = (T)(unsigned char)(val&0xFF); + } + return *this; + } +#endif + + //@} + }; + + /* + #-------------------------------------- + # + # + # + # Definition of the CImg structure + # + # + # + #-------------------------------------- + */ + + //! Class representing an image (up to 4 dimensions wide), each pixel being of type \c T. + /** + This is the main class of the %CImg Library. It declares and constructs + an image, allows access to its pixel values, and is able to perform various image operations. + + \par Image representation + + A %CImg image is defined as an instance of the container \c CImg, which contains a regular grid of pixels, + each pixel value being of type \c T. The image grid can have up to 4 dimensions: width, height, depth + and number of channels. + Usually, the three first dimensions are used to describe spatial coordinates (x,y,z), + while the number of channels is rather used as a vector-valued dimension + (it may describe the R,G,B color channels for instance). + If you need a fifth dimension, you can use image lists \c CImgList rather than simple images \c CImg. + + Thus, the \c CImg class is able to represent volumetric images of vector-valued pixels, + as well as images with less dimensions (1d scalar signal, 2d color images, ...). + Most member functions of the class CImg<\c T> are designed to handle this maximum case of (3+1) dimensions. + + Concerning the pixel value type \c T: + fully supported template types are the basic C++ types: unsigned char, char, short, unsigned int, int, + unsigned long, long, float, double, ... . + Typically, fast image display can be done using CImg images, + while complex image processing algorithms may be rather coded using CImg or CImg + images that have floating-point pixel values. The default value for the template T is \c float. + Using your own template types may be possible. However, you will certainly have to define the complete set + of arithmetic and logical operators for your class. + + \par Image structure + + The \c CImg structure contains \e six fields: + - \c _width defines the number of \a columns of the image (size along the X-axis). + - \c _height defines the number of \a rows of the image (size along the Y-axis). + - \c _depth defines the number of \a slices of the image (size along the Z-axis). + - \c _spectrum defines the number of \a channels of the image (size along the C-axis). + - \c _data defines a \a pointer to the \a pixel \a data (of type \c T). + - \c _is_shared is a boolean that tells if the memory buffer \c data is shared with + another image. + + You can access these fields publicly although it is recommended to use the dedicated functions + width(), height(), depth(), spectrum() and ptr() to do so. + Image dimensions are not limited to a specific range (as long as you got enough available memory). + A value of \e 1 usually means that the corresponding dimension is \a flat. + If one of the dimensions is \e 0, or if the data pointer is null, the image is considered as \e empty. + Empty images should not contain any pixel data and thus, will not be processed by CImg member functions + (a CImgInstanceException will be thrown instead). + Pixel data are stored in memory, in a non interlaced mode (See \ref cimg_storage). + + \par Image declaration and construction + + Declaring an image can be done by using one of the several available constructors. + Here is a list of the most used: + + - Construct images from arbitrary dimensions: + - CImg img; declares an empty image. + - CImg img(128,128); declares a 128x128 greyscale image with + \c unsigned \c char pixel values. + - CImg img(3,3); declares a 3x3 matrix with \c double coefficients. + - CImg img(256,256,1,3); declares a 256x256x1x3 (color) image + (colors are stored as an image with three channels). + - CImg img(128,128,128); declares a 128x128x128 volumetric and greyscale image + (with \c double pixel values). + - CImg<> img(128,128,128,3); declares a 128x128x128 volumetric color image + (with \c float pixels, which is the default value of the template parameter \c T). + - \b Note: images pixels are not automatically initialized to 0. You may use the function \c fill() to + do it, or use the specific constructor taking 5 parameters like this: + CImg<> img(128,128,128,3,0); declares a 128x128x128 volumetric color image with all pixel values to 0. + + - Construct images from filenames: + - CImg img("image.jpg"); reads a JPEG color image from the file "image.jpg". + - CImg img("analyze.hdr"); reads a volumetric image (ANALYZE7.5 format) from the + file "analyze.hdr". + - \b Note: You need to install ImageMagick + to be able to read common compressed image formats (JPG,PNG, ...) (See \ref cimg_files_io). + + - Construct images from C-style arrays: + - CImg img(data_buffer,256,256); constructs a 256x256 greyscale image from a \c int* buffer + \c data_buffer (of size 256x256=65536). + - CImg img(data_buffer,256,256,1,3,false); constructs a 256x256 color image + from a \c unsigned \c char* buffer \c data_buffer (where R,G,B channels follow each others). + - CImg img(data_buffer,256,256,1,3,true); constructs a 256x256 color image + from a \c unsigned \c char* buffer \c data_buffer (where R,G,B channels are multiplexed). + + The complete list of constructors can be found here. + + \par Most useful functions + + The \c CImg class contains a lot of functions that operates on images. + Some of the most useful are: + + - operator()(): allows to access or write pixel values. + - display(): displays the image in a new window. + **/ + template + struct CImg { + + unsigned int _width, _height, _depth, _spectrum; + bool _is_shared; + T *_data; + + //! Simple iterator type, to loop through each pixel value of an image instance. + /** + \note + - The \c CImg::iterator type is defined to be a T*. + - You will seldom have to use iterators in %CImg, most classical operations + being achieved (often in a faster way) using methods of \c CImg. + \par Example + \code + CImg img("reference.jpg"); // Load image from file. + for (CImg::iterator it = img.begin(), it::const_iterator type is defined to be a \c const \c T*. + - You will seldom have to use iterators in %CImg, most classical operations + being achieved (often in a faster way) using methods of \c CImg. + \par Example + \code + const CImg img("reference.jpg"); // Load image from file. + float sum = 0; + for (CImg::iterator it = img.begin(), it::value_type type of a \c CImg is defined to be a \c T. + - \c CImg::value_type is actually not used in %CImg methods. It has been mainly defined for + compatibility with STL naming conventions. + **/ + typedef T value_type; + + // Define common types related to template type T. + typedef typename cimg::superset::type Tbool; + typedef typename cimg::superset::type Tuchar; + typedef typename cimg::superset::type Tchar; + typedef typename cimg::superset::type Tushort; + typedef typename cimg::superset::type Tshort; + typedef typename cimg::superset::type Tuint; + typedef typename cimg::superset::type Tint; + typedef typename cimg::superset::type Tulong; + typedef typename cimg::superset::type Tlong; + typedef typename cimg::superset::type Tfloat; + typedef typename cimg::superset::type Tdouble; + typedef typename cimg::last::type boolT; + typedef typename cimg::last::type ucharT; + typedef typename cimg::last::type charT; + typedef typename cimg::last::type ushortT; + typedef typename cimg::last::type shortT; + typedef typename cimg::last::type uintT; + typedef typename cimg::last::type intT; + typedef typename cimg::last::type ulongT; + typedef typename cimg::last::type longT; + typedef typename cimg::last::type floatT; + typedef typename cimg::last::type doubleT; + + //@} + //--------------------------- + // + //! \name Plugins + //@{ + //--------------------------- +#ifdef cimg_plugin +#include cimg_plugin +#endif +#ifdef cimg_plugin1 +#include cimg_plugin1 +#endif +#ifdef cimg_plugin2 +#include cimg_plugin2 +#endif +#ifdef cimg_plugin3 +#include cimg_plugin3 +#endif +#ifdef cimg_plugin4 +#include cimg_plugin4 +#endif +#ifdef cimg_plugin5 +#include cimg_plugin5 +#endif +#ifdef cimg_plugin6 +#include cimg_plugin6 +#endif +#ifdef cimg_plugin7 +#include cimg_plugin7 +#endif +#ifdef cimg_plugin8 +#include cimg_plugin8 +#endif + + //@} + //--------------------------------------------------------- + // + //! \name Constructors / Destructor / Instance Management + //@{ + //--------------------------------------------------------- + + //! Destroy image. + /** + \note + - The pixel buffer data() is deallocated if necessary, e.g. for non-empty and non-shared image instances. + - Destroying an empty or shared image does nothing actually. + \warning + - When destroying a non-shared image, make sure that you will \e not operate on a remaining shared image + that shares its buffer with the destroyed instance, in order to avoid further invalid memory access + (to a deallocated buffer). + **/ + ~CImg() { + if (!_is_shared) delete[] _data; + } + + //! Construct empty image. + /** + \note + - An empty image has no pixel data and all of its dimensions width(), height(), depth(), spectrum() + are set to \c 0, as well as its pixel buffer pointer data(). + - An empty image may be re-assigned afterwards, e.g. with the family of + assign(unsigned int,unsigned int,unsigned int,unsigned int) methods, + or by operator=(const CImg&). In all cases, the type of pixels stays \c T. + - An empty image is never shared. + \par Example + \code + CImg img1, img2; // Construct two empty images. + img1.assign(256,256,1,3); // Re-assign 'img1' to be a 256x256x1x3 (color) image. + img2 = img1.get_rand(0,255); // Re-assign 'img2' to be a random-valued version of 'img1'. + img2.assign(); // Re-assign 'img2' to be an empty image again. + \endcode + **/ + CImg():_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {} + + //! Construct image with specified size. + /** + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \note + - It is able to create only \e non-shared images, and allocates thus a pixel buffer data() + for each constructed image instance. + - Setting one dimension \c size_x,\c size_y,\c size_z or \c size_c to \c 0 leads to the construction of + an \e empty image. + - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated + (e.g. when requested size is too big for available memory). + \warning + - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values. + In order to initialize pixel values during construction (e.g. with \c 0), use constructor + CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) instead. + \par Example + \code + CImg img1(256,256,1,3); // Construct a 256x256x1x3 (color) image, filled with garbage values. + CImg img2(256,256,1,3,0); // Construct a 256x256x1x3 (color) image, filled with value '0'. + \endcode + **/ + explicit CImg(const unsigned int size_x, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1): + _is_shared(false) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (siz) { + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + } + } else { _width = _height = _depth = _spectrum = 0; _data = 0; } + } + + //! Construct image with specified size and initialize pixel values. + /** + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \param value Initialization value. + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), + but it also fills the pixel buffer with the specified \c value. + \warning + - It cannot be used to construct a vector-valued image and initialize it with \e vector-valued pixels + (e.g. RGB vector, for color images). + For this task, you may use fillC() after construction. + **/ + CImg(const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, const T value): + _is_shared(false) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (siz) { + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + } + fill(value); + } else { _width = _height = _depth = _spectrum = 0; _data = 0; } + } + + //! Construct image with specified size and initialize pixel values from a sequence of integers. + /** + Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, + with pixels of type \c T, and initialize pixel + values from the specified sequence of integers \c value0,\c value1,\c ... + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \param value0 First value of the initialization sequence (must be an \e integer). + \param value1 Second value of the initialization sequence (must be an \e integer). + \param ... + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills + the pixel buffer with a sequence of specified integer values. + \warning + - You must specify \e exactly \c size_x*\c size_y*\c size_z*\c size_c integers in the initialization sequence. + Otherwise, the constructor may crash or fill your image pixels with garbage. + \par Example + \code + const CImg img(2,2,1,3, // Construct a 2x2 color (RGB) image. + 0,255,0,255, // Set the 4 values for the red component. + 0,0,255,255, // Set the 4 values for the green component. + 64,64,64,64); // Set the 4 values for the blue component. + img.resize(150,150).display(); + \endcode + \image html ref_constructor1.jpg + **/ + CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, + const int value0, const int value1, ...): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { +#define _CImg_stdarg(img,a0,a1,N,t) { \ + unsigned long _siz = (unsigned long)N; \ + if (_siz--) { \ + va_list ap; \ + va_start(ap,a1); \ + T *ptrd = (img)._data; \ + *(ptrd++) = (T)a0; \ + if (_siz--) { \ + *(ptrd++) = (T)a1; \ + for (; _siz; --_siz) *(ptrd++) = (T)va_arg(ap,t); \ + } \ + va_end(ap); \ + } \ + } + assign(size_x,size_y,size_z,size_c); + _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,int); + } + +#ifdef cimg_use_cpp11 + //! Construct image with specified size and initialize pixel values from an initializer list of integers. + /** + Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, + with pixels of type \c T, and initialize pixel + values from the specified initializer list of integers { \c value0,\c value1,\c ... } + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \param { value0, value1, ... } Initialization list + \param repeat_values Tells if the value filling process is repeated over the image. + + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills + the pixel buffer with a sequence of specified integer values. + \par Example + \code + const CImg img(2,2,1,3, // Construct a 2x2 color (RGB) image. + { 0,255,0,255, // Set the 4 values for the red component. + 0,0,255,255, // Set the 4 values for the green component. + 64,64,64,64 }); // Set the 4 values for the blue component. + img.resize(150,150).display(); + \endcode + \image html ref_constructor1.jpg + **/ + template + CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, + const std::initializer_list values, + const bool repeat_values=true): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { +#define _cimg_constructor_cpp11(repeat_values) \ + auto it = values.begin(); \ + unsigned long siz = size(); \ + if (repeat_values) for (T *ptrd = _data; siz--; ) { \ + *(ptrd++) = (T)(*(it++)); if (it==values.end()) it = values.begin(); } \ + else { siz = cimg::min(siz,values.size()); for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++)); } + assign(size_x,size_y,size_z,size_c); + _cimg_constructor_cpp11(repeat_values); + } + + template + CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, + std::initializer_list values, + const bool repeat_values=true): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(size_x,size_y,size_z); + _cimg_constructor_cpp11(repeat_values); + } + + template + CImg(const unsigned int size_x, const unsigned int size_y, + std::initializer_list values, + const bool repeat_values=true): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(size_x,size_y); + _cimg_constructor_cpp11(repeat_values); + } + + template + CImg(const unsigned int size_x, + std::initializer_list values, + const bool repeat_values=true):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(size_x); + _cimg_constructor_cpp11(repeat_values); + } + + //! Construct single channel 1D image with pixel values and width obtained from an initializer list of integers. + /** + Construct a new image instance of size \c width x \c 1 x \c 1 x \c 1, + with pixels of type \c T, and initialize pixel + values from the specified initializer list of integers { \c value0,\c value1,\c ... }. Image width is + given by the size of the initializer list. + \param { value0, value1, ... } Initialization list + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int) with height=1, depth=1, and spectrum=1, + but it also fills the pixel buffer with a sequence of specified integer values. + \par Example + \code + const CImg img = {10,20,30,20,10 }; // Construct a 5x1 image with one channel, and set its pixel values. + img.resize(150,150).display(); + \endcode + \image html ref_constructor1.jpg + **/ + template + CImg(const std::initializer_list values): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(values.size(),1,1,1); + auto it = values.begin(); + unsigned long siz = _width; + for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++)); + } + + template + CImg & operator=(std::initializer_list values) { + _cimg_constructor_cpp11(siz>values.size()); + return *this; + } +#endif + + //! Construct image with specified size and initialize pixel values from a sequence of doubles. + /** + Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T, + and initialize pixel values from the specified sequence of doubles \c value0,\c value1,\c ... + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \param value0 First value of the initialization sequence (must be a \e double). + \param value1 Second value of the initialization sequence (must be a \e double). + \param ... + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...), but + takes a sequence of double values instead of integers. + \warning + - You must specify \e exactly \c dx*\c dy*\c dz*\c dc doubles in the initialization sequence. + Otherwise, the constructor may crash or fill your image with garbage. + For instance, the code below will probably crash on most platforms: + \code + const CImg img(2,2,1,1, 0.5,0.5,255,255); // FAIL: The two last arguments are 'int', not 'double'! + \endcode + **/ + CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, + const double value0, const double value1, ...): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(size_x,size_y,size_z,size_c); + _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,double); + } + + //! Construct image with specified size and initialize pixel values from a value string. + /** + Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T, + and initializes pixel values from the specified string \c values. + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \param values Value string describing the way pixel values are set. + \param repeat_values Tells if the value filling process is repeated over the image. + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills + the pixel buffer with values described in the value string \c values. + - Value string \c values may describe two different filling processes: + - Either \c values is a sequences of values assigned to the image pixels, as in "1,2,3,7,8,2". + In this case, set \c repeat_values to \c true to periodically fill the image with the value sequence. + - Either, \c values is a formula, as in "cos(x/10)*sin(y/20)". + In this case, parameter \c repeat_values is pointless. + - For both cases, specifying \c repeat_values is mandatory. + It disambiguates the possible overloading of constructor + CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) with \c T being a const char*. + - A \c CImgArgumentException is thrown when an invalid value string \c values is specified. + \par Example + \code + const CImg img1(129,129,1,3,"0,64,128,192,255",true), // Construct image filled from a value sequence. + img2(129,129,1,3,"if(c==0,255*abs(cos(x/10)),1.8*y)",false); // Construct image filled from a formula. + (img1,img2).display(); + \endcode + \image html ref_constructor2.jpg + **/ + CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, + const char *const values, const bool repeat_values):_is_shared(false) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (siz) { + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + } + fill(values,repeat_values); + } else { _width = _height = _depth = _spectrum = 0; _data = 0; } + } + + //! Construct image with specified size and initialize pixel values from a memory buffer. + /** + Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T, + and initializes pixel values from the specified \c t* memory buffer. + \param values Pointer to the input memory buffer. + \param size_x Image width(). + \param size_y Image height(). + \param size_z Image depth(). + \param size_c Image spectrum() (number of channels). + \param is_shared Tells if input memory buffer must be shared by the current instance. + \note + - If \c is_shared is \c false, the image instance allocates its own pixel buffer, + and values from the specified input buffer are copied to the instance buffer. + If buffer types \c T and \c t are different, a regular static cast is performed during buffer copy. + - Otherwise, the image instance does \e not allocate a new buffer, and uses the input memory buffer as its + own pixel buffer. This case requires that types \c T and \c t are the same. Later, destroying such a shared + image will not deallocate the pixel buffer, this task being obviously charged to the initial buffer allocator. + - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated + (e.g. when requested size is too big for available memory). + \warning + - You must take care when operating on a shared image, since it may have an invalid pixel buffer pointer data() + (e.g. already deallocated). + \par Example + \code + unsigned char tab[256*256] = { 0 }; + CImg img1(tab,256,256,1,1,false), // Construct new non-shared image from buffer 'tab'. + img2(tab,256,256,1,1,true); // Construct new shared-image from buffer 'tab'. + tab[1024] = 255; // Here, 'img2' is indirectly modified, but not 'img1'. + \endcode + **/ + template + CImg(const t *const values, const unsigned int size_x, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false):_is_shared(false) { + if (is_shared) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgArgumentException(_cimg_instance + "CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance " + "from a (%s*) buffer (pixel types are different).", + cimg_instance, + size_x,size_y,size_z,size_c,CImg::pixel_type()); + } + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (values && siz) { + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + + } + const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); + } else { _width = _height = _depth = _spectrum = 0; _data = 0; } + } + + //! Construct image with specified size and initialize pixel values from a memory buffer \specialization. + CImg(const T *const values, const unsigned int size_x, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (values && siz) { + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = is_shared; + if (_is_shared) _data = const_cast(values); + else { + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + } + std::memcpy(_data,values,siz*sizeof(T)); } + } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; } + } + + //! Construct image from reading an image file. + /** + Construct a new image instance with pixels of type \c T, and initialize pixel values with the data read from + an image file. + \param filename Filename, as a C-string. + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it reads the image + dimensions and pixel values from the specified image file. + - The recognition of the image file format by %CImg higly depends on the tools installed on your system + and on the external libraries you used to link your code against. + - Considered pixel type \c T should better fit the file format specification, or data loss may occur during + file load (e.g. constructing a \c CImg from a float-valued image file). + - A \c CImgIOException is thrown when the specified \c filename cannot be read, or if the file format is not + recognized. + \par Example + \code + const CImg img("reference.jpg"); + img.display(); + \endcode + \image html ref_image.jpg + **/ + explicit CImg(const char *const filename):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(filename); + } + + //! Construct image copy. + /** + Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg instance. + \param img Input image to copy. + \note + - Constructed copy has the same size width() x height() x depth() x spectrum() and pixel values as the + input image \c img. + - If input image \c img is \e shared and if types \c T and \c t are the same, the constructed copy is also + \e shared, and shares its pixel buffer with \c img. + Modifying a pixel value in the constructed copy will thus also modifies it in the input image \c img. + This behavior is needful to allow functions to return shared images. + - Otherwise, the constructed copy allocates its own pixel buffer, and copies pixel values from the input + image \c img into its buffer. The copied pixel values may be eventually statically casted if types \c T and + \c t are different. + - Constructing a copy from an image \c img when types \c t and \c T are the same is significantly faster than + with different types. + - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated + (e.g. not enough available memory). + **/ + template + CImg(const CImg& img):_is_shared(false) { + const unsigned long siz = img.size(); + if (img._data && siz) { + _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), + img._width,img._height,img._depth,img._spectrum); + } + const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); + } else { _width = _height = _depth = _spectrum = 0; _data = 0; } + } + + //! Construct image copy \specialization. + CImg(const CImg& img) { + const unsigned long siz = img.size(); + if (img._data && siz) { + _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; + _is_shared = img._is_shared; + if (_is_shared) _data = const_cast(img._data); + else { + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), + img._width,img._height,img._depth,img._spectrum); + + } + std::memcpy(_data,img._data,siz*sizeof(T)); + } + } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; } + } + + //! Advanced copy constructor. + /** + Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg instance, + while forcing the shared state of the constructed copy. + \param img Input image to copy. + \param is_shared Tells about the shared state of the constructed copy. + \note + - Similar to CImg(const CImg&), except that it allows to decide the shared state of + the constructed image, which does not depend anymore on the shared state of the input image \c img: + - If \c is_shared is \c true, the constructed copy will share its pixel buffer with the input image \c img. + For that case, the pixel types \c T and \c t \e must be the same. + - If \c is_shared is \c false, the constructed copy will allocate its own pixel buffer, whether the input + image \c img is shared or not. + - A \c CImgArgumentException is thrown when a shared copy is requested with different pixel types \c T and \c t. + **/ + template + CImg(const CImg& img, const bool is_shared):_is_shared(false) { + if (is_shared) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgArgumentException(_cimg_instance + "CImg(): Invalid construction request of a shared instance from a " + "CImg<%s> image (%u,%u,%u,%u,%p) (pixel types are different).", + cimg_instance, + CImg::pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data); + } + const unsigned long siz = img.size(); + if (img._data && siz) { + _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), + img._width,img._height,img._depth,img._spectrum); + } + const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); + } else { _width = _height = _depth = _spectrum = 0; _data = 0; } + } + + //! Advanced copy constructor \specialization. + CImg(const CImg& img, const bool is_shared) { + const unsigned long siz = img.size(); + if (img._data && siz) { + _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; + _is_shared = is_shared; + if (_is_shared) _data = const_cast(img._data); + else { + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), + img._width,img._height,img._depth,img._spectrum); + } + std::memcpy(_data,img._data,siz*sizeof(T)); + } + } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; } + } + + //! Construct image with dimensions borrowed from another image. + /** + Construct a new image instance with pixels of type \c T, and size get from some dimensions of an existing + \c CImg instance. + \param img Input image from which dimensions are borrowed. + \param dimensions C-string describing the image size along the X,Y,Z and C-dimensions. + \note + - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it takes the image dimensions + (\e not its pixel values) from an existing \c CImg instance. + - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values. + In order to initialize pixel values (e.g. with \c 0), use constructor CImg(const CImg&,const char*,T) + instead. + \par Example + \code + const CImg img1(256,128,1,3), // 'img1' is a 256x128x1x3 image. + img2(img1,"xyzc"), // 'img2' is a 256x128x1x3 image. + img3(img1,"y,x,z,c"), // 'img3' is a 128x256x1x3 image. + img4(img1,"c,x,y,3",0), // 'img4' is a 3x128x256x3 image (with pixels initialized to '0'). + \endcode + **/ + template + CImg(const CImg& img, const char *const dimensions): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(img,dimensions); + } + + //! Construct image with dimensions borrowed from another image and initialize pixel values. + /** + Construct a new image instance with pixels of type \c T, and size get from the dimensions of an existing + \c CImg instance, and set all pixel values to specified \c value. + \param img Input image from which dimensions are borrowed. + \param dimensions String describing the image size along the X,Y,Z and V-dimensions. + \param value Value used for initialization. + \note + - Similar to CImg(const CImg&,const char*), but it also fills the pixel buffer with the specified \c value. + **/ + template + CImg(const CImg& img, const char *const dimensions, const T value): + _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + assign(img,dimensions).fill(value); + } + + //! Construct image from a display window. + /** + Construct a new image instance with pixels of type \c T, as a snapshot of an existing \c CImgDisplay instance. + \param disp Input display window. + \note + - The width() and height() of the constructed image instance are the same as the specified \c CImgDisplay. + - The depth() and spectrum() of the constructed image instance are respectively set to \c 1 and \c 3 + (i.e. a 2d color image). + - The image pixels are read as 8-bits RGB values. + **/ + explicit CImg(const CImgDisplay &disp):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + disp.snapshot(*this); + } + + // Constructor and assignment operator for rvalue references (c++11). + // This avoids an additional image copy for methods returning new images. Can save RAM for big images ! +#ifdef cimg_use_cpp11 + CImg(CImg&& img):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { + swap(img); + } + CImg& operator=(CImg&& img) { + if (_is_shared) return assign(img); + return img.swap(*this); + } +#endif + + //! Construct empty image \inplace. + /** + In-place version of the default constructor CImg(). It simply resets the instance to an empty image. + **/ + CImg& assign() { + if (!_is_shared) delete[] _data; + _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; + return *this; + } + + //! Construct image with specified size \inplace. + /** + In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int). + **/ + CImg& assign(const unsigned int size_x, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (!siz) return assign(); + const unsigned long curr_siz = size(); + if (siz!=curr_siz) { + if (_is_shared) + throw CImgArgumentException(_cimg_instance + "assign(): Invalid assignement request of shared instance from specified " + "image (%u,%u,%u,%u).", + cimg_instance, + size_x,size_y,size_z,size_c); + else { + delete[] _data; + try { _data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + } + } + } + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; + return *this; + } + + //! Construct image with specified size and initialize pixel values \inplace. + /** + In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,T). + **/ + CImg& assign(const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, const T value) { + return assign(size_x,size_y,size_z,size_c).fill(value); + } + + //! Construct image with specified size and initialize pixel values from a sequence of integers \inplace. + /** + In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...). + **/ + CImg& assign(const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, + const int value0, const int value1, ...) { + assign(size_x,size_y,size_z,size_c); + _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,int); + return *this; + } + + //! Construct image with specified size and initialize pixel values from a sequence of doubles \inplace. + /** + In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,double,double,...). + **/ + CImg& assign(const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, + const double value0, const double value1, ...) { + assign(size_x,size_y,size_z,size_c); + _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,double); + return *this; + } + + //! Construct image with specified size and initialize pixel values from a value string \inplace. + /** + In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,const char*,bool). + **/ + CImg& assign(const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, + const char *const values, const bool repeat_values) { + return assign(size_x,size_y,size_z,size_c).fill(values,repeat_values); + } + + //! Construct image with specified size and initialize pixel values from a memory buffer \inplace. + /** + In-place version of the constructor CImg(const t*,unsigned int,unsigned int,unsigned int,unsigned int). + **/ + template + CImg& assign(const t *const values, const unsigned int size_x, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (!values || !siz) return assign(); + assign(size_x,size_y,size_z,size_c); + const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); + return *this; + } + + //! Construct image with specified size and initialize pixel values from a memory buffer \specialization. + CImg& assign(const T *const values, const unsigned int size_x, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (!values || !siz) return assign(); + const unsigned long curr_siz = size(); + if (values==_data && siz==curr_siz) return assign(size_x,size_y,size_z,size_c); + if (_is_shared || values+siz<_data || values>=_data+size()) { + assign(size_x,size_y,size_z,size_c); + if (_is_shared) std::memmove(_data,values,siz*sizeof(T)); + else std::memcpy(_data,values,siz*sizeof(T)); + } else { + T *new_data = 0; + try { new_data = new T[siz]; } catch (...) { + _width = _height = _depth = _spectrum = 0; _data = 0; + throw CImgInstanceException(_cimg_instance + "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", + cimg_instance, + cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), + size_x,size_y,size_z,size_c); + } + std::memcpy(new_data,values,siz*sizeof(T)); + delete[] _data; _data = new_data; _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; + } + return *this; + } + + //! Construct image with specified size and initialize pixel values from a memory buffer \overloading. + template + CImg& assign(const t *const values, const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, const bool is_shared) { + if (is_shared) + throw CImgArgumentException(_cimg_instance + "assign(): Invalid assignment request of shared instance from (%s*) buffer" + "(pixel types are different).", + cimg_instance, + CImg::pixel_type()); + return assign(values,size_x,size_y,size_z,size_c); + } + + //! Construct image with specified size and initialize pixel values from a memory buffer \overloading. + CImg& assign(const T *const values, const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, const bool is_shared) { + const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; + if (!values || !siz) { + if (is_shared) + throw CImgArgumentException(_cimg_instance + "assign(): Invalid assignment request of shared instance from (null) or " + "empty buffer.", + cimg_instance); + else return assign(); + } + if (!is_shared) { if (_is_shared) assign(); assign(values,size_x,size_y,size_z,size_c); } + else { + if (!_is_shared) { + if (values+siz<_data || values>=_data+size()) assign(); + else cimg::warn(_cimg_instance + "assign(): Shared image instance has overlapping memory.", + cimg_instance); + } + _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = true; + _data = const_cast(values); + } + return *this; + } + + //! Construct image from reading an image file \inplace. + /** + In-place version of the constructor CImg(const char*). + **/ + CImg& assign(const char *const filename) { + return load(filename); + } + + //! Construct image copy \inplace. + /** + In-place version of the constructor CImg(const CImg&). + **/ + template + CImg& assign(const CImg& img) { + return assign(img._data,img._width,img._height,img._depth,img._spectrum); + } + + //! In-place version of the advanced copy constructor. + /** + In-place version of the constructor CImg(const CImg&,bool). + **/ + template + CImg& assign(const CImg& img, const bool is_shared) { + return assign(img._data,img._width,img._height,img._depth,img._spectrum,is_shared); + } + + //! Construct image with dimensions borrowed from another image \inplace. + /** + In-place version of the constructor CImg(const CImg&,const char*). + **/ + template + CImg& assign(const CImg& img, const char *const dimensions) { + if (!dimensions || !*dimensions) return assign(img._width,img._height,img._depth,img._spectrum); + unsigned int siz[4] = { 0,1,1,1 }, k = 0; + for (const char *s = dimensions; *s && k<4; ++k) { + char item[256] = { 0 }; + if (std::sscanf(s,"%255[^0-9%xyzvwhdcXYZVWHDC]",item)>0) s+=std::strlen(item); + if (*s) { + unsigned int val = 0; char sep = 0; + if (std::sscanf(s,"%u%c",&val,&sep)>0) { + if (sep=='%') siz[k] = val*(k==0?_width:k==1?_height:k==2?_depth:_spectrum)/100; + else siz[k] = val; + while (*s>='0' && *s<='9') ++s; if (sep=='%') ++s; + } else switch (cimg::uncase(*s)) { + case 'x' : case 'w' : siz[k] = img._width; ++s; break; + case 'y' : case 'h' : siz[k] = img._height; ++s; break; + case 'z' : case 'd' : siz[k] = img._depth; ++s; break; + case 'c' : case 's' : siz[k] = img._spectrum; ++s; break; + default : + throw CImgArgumentException(_cimg_instance + "assign(): Invalid character '%c' detected in specified dimension string '%s'.", + cimg_instance, + *s,dimensions); + } + } + } + return assign(siz[0],siz[1],siz[2],siz[3]); + } + + //! Construct image with dimensions borrowed from another image and initialize pixel values \inplace. + /** + In-place version of the constructor CImg(const CImg&,const char*,T). + **/ + template + CImg& assign(const CImg& img, const char *const dimensions, const T value) { + return assign(img,dimensions).fill(value); + } + + //! Construct image from a display window \inplace. + /** + In-place version of the constructor CImg(const CImgDisplay&). + **/ + CImg& assign(const CImgDisplay &disp) { + disp.snapshot(*this); + return *this; + } + + //! Construct empty image \inplace. + /** + Equivalent to assign(). + \note + - It has been defined for compatibility with STL naming conventions. + **/ + CImg& clear() { + return assign(); + } + + //! Transfer content of an image instance into another one. + /** + Transfer the dimensions and the pixel buffer content of an image instance into another one, + and replace instance by an empty image. It avoids the copy of the pixel buffer + when possible. + \param img Destination image. + \note + - Pixel types \c T and \c t of source and destination images can be different, though the process is + designed to be instantaneous when \c T and \c t are the same. + \par Example + \code + CImg src(256,256,1,3,0), // Construct a 256x256x1x3 (color) image filled with value '0'. + dest(16,16); // Construct a 16x16x1x1 (scalar) image. + src.move_to(dest); // Now, 'src' is empty and 'dest' is the 256x256x1x3 image. + \endcode + **/ + template + CImg& move_to(CImg& img) { + img.assign(*this); + assign(); + return img; + } + + //! Transfer content of an image instance into another one \specialization. + CImg& move_to(CImg& img) { + if (_is_shared || img._is_shared) img.assign(*this); + else swap(img); + assign(); + return img; + } + + //! Transfer content of an image instance into a new image in an image list. + /** + Transfer the dimensions and the pixel buffer content of an image instance + into a newly inserted image at position \c pos in specified \c CImgList instance. + \param list Destination list. + \param pos Position of the newly inserted image in the list. + \note + - When optionnal parameter \c pos is ommited, the image instance is transfered as a new + image at the end of the specified \c list. + - It is convenient to sequentially insert new images into image lists, with no + additional copies of memory buffer. + \par Example + \code + CImgList list; // Construct an empty image list. + CImg img("reference.jpg"); // Read image from filename. + img.move_to(list); // Transfer image content as a new item in the list (no buffer copy). + \endcode + **/ + template + CImgList& move_to(CImgList& list, const unsigned int pos=~0U) { + const unsigned int npos = pos>list._width?list._width:pos; + move_to(list.insert(1,npos)[npos]); + return list; + } + + //! Swap fields of two image instances. + /** + \param img Image to swap fields with. + \note + - It can be used to interchange the content of two images in a very fast way. Can be convenient when dealing + with algorithms requiring two swapping buffers. + \par Example + \code + CImg img1("lena.jpg"), + img2("milla.jpg"); + img1.swap(img2); // Now, 'img1' is 'milla' and 'img2' is 'lena'. + \endcode + **/ + CImg& swap(CImg& img) { + cimg::swap(_width,img._width); + cimg::swap(_height,img._height); + cimg::swap(_depth,img._depth); + cimg::swap(_spectrum,img._spectrum); + cimg::swap(_data,img._data); + cimg::swap(_is_shared,img._is_shared); + return img; + } + + //! Return a reference to an empty image. + /** + \note + This function is useful mainly to declare optional parameters having type \c CImg in functions prototypes, + e.g. + \code + void f(const int x=0, const int y=0, const CImg& img=CImg::empty()); + \endcode + **/ + static CImg& empty() { + static CImg _empty; + return _empty.assign(); + } + + //@} + //------------------------------------------ + // + //! \name Overloaded Operators + //@{ + //------------------------------------------ + + //! Access to a pixel value. + /** + Return a reference to a located pixel value of the image instance, + being possibly \e const, whether the image instance is \e const or not. + This is the standard method to get/set pixel values in \c CImg images. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Range of pixel coordinates start from (0,0,0,0) to + (width()-1,height()-1,depth()-1,spectrum()-1). + - Due to the particular arrangement of the pixel buffers defined in %CImg, you can omit one coordinate if the + corresponding dimension is equal to \c 1. + For instance, pixels of a 2d image (depth() equal to \c 1) can be accessed by img(x,y,c) instead of + img(x,y,0,c). + \warning + - There is \e no boundary checking done in this operator, to make it as fast as possible. + You \e must take care of out-of-bounds access by yourself, if necessary. + For debuging purposes, you may want to define macro \c 'cimg_verbosity'>=3 to enable additional boundary + checking operations in this operator. In that case, warning messages will be printed on the error output + when accessing out-of-bounds pixels. + \par Example + \code + CImg img(100,100,1,3,0); // Construct a 100x100x1x3 (color) image with pixels set to '0'. + const float + valR = img(10,10,0,0), // Read red value at coordinates (10,10). + valG = img(10,10,0,1), // Read green value at coordinates (10,10) + valB = img(10,10,2), // Read blue value at coordinates (10,10) (Z-coordinate can be omitted). + avg = (valR + valG + valB)/3; // Compute average pixel value. + img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the color pixel (10,10) by the average grey value. + \endcode + **/ +#if cimg_verbosity>=3 + T& operator()(const unsigned int x, const unsigned int y=0, + const unsigned int z=0, const unsigned int c=0) { + const unsigned long off = (unsigned long)offset(x,y,z,c); + if (!_data || off>=size()) { + cimg::warn(_cimg_instance + "operator(): Invalid pixel request, at coordinates (%u,%u,%u,%u) [offset=%u].", + cimg_instance, + x,y,z,c,off); + return *_data; + } + else return _data[off]; + } + + //! Access to a pixel value \const. + const T& operator()(const unsigned int x, const unsigned int y=0, + const unsigned int z=0, const unsigned int c=0) const { + return const_cast*>(this)->operator()(x,y,z,c); + } + + //! Access to a pixel value. + /** + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param wh Precomputed offset, must be equal to width()*\ref height(). + \param whd Precomputed offset, must be equal to width()*\ref height()*\ref depth(). + \note + - Similar to (but faster than) operator()(). + It uses precomputed offsets to optimize memory access. You may use it to optimize + the reading/writing of several pixel values in the same image (e.g. in a loop). + **/ + T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, + const unsigned long wh, const unsigned long whd=0) { + cimg::unused(wh,whd); + return (*this)(x,y,z,c); + } + + //! Access to a pixel value \const. + const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, + const unsigned long wh, const unsigned long whd=0) const { + cimg::unused(wh,whd); + return (*this)(x,y,z,c); + } +#else + T& operator()(const unsigned int x) { + return _data[x]; + } + + const T& operator()(const unsigned int x) const { + return _data[x]; + } + + T& operator()(const unsigned int x, const unsigned int y) { + return _data[x + y*_width]; + } + + const T& operator()(const unsigned int x, const unsigned int y) const { + return _data[x + y*_width]; + } + + T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) { + return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height]; + } + + const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) const { + return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height]; + } + + T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) { + return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height + + c*(unsigned long)_width*_height*_depth]; + } + + const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) const { + return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height + + c*(unsigned long)_width*_height*_depth]; + } + + T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int, + const unsigned long wh) { + return _data[x + y*_width + z*wh]; + } + + const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int, + const unsigned long wh) const { + return _data[x + y*_width + z*wh]; + } + + T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, + const unsigned long wh, const unsigned long whd) { + return _data[x + y*_width + z*wh + c*whd]; + } + + const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, + const unsigned long wh, const unsigned long whd) const { + return _data[x + y*_width + z*wh + c*whd]; + } +#endif + + //! Implicitely cast an image into a \c T*. + /** + Implicitely cast a \c CImg instance into a \c T* or \c const \c T* pointer, whether the image instance + is \e const or not. The returned pointer points on the first value of the image pixel buffer. + \note + - It simply returns the pointer data() to the pixel buffer. + - This implicit conversion is convenient to test the empty state of images (data() being \c 0 in this case), e.g. + \code + CImg img1(100,100), img2; // 'img1' is a 100x100 image, 'img2' is an empty image. + if (img1) { // Test succeeds, 'img1' is not an empty image. + if (!img2) { // Test succeeds, 'img2' is an empty image. + std::printf("'img1' is not empty, 'img2' is empty."); + } + } + \endcode + - It also allows to use brackets to access pixel values, without need for a \c CImg::operator[](), e.g. + \code + CImg img(100,100); + const float value = img[99]; // Access to value of the last pixel on the first row. + img[510] = 255; // Set pixel value at (10,5). + \endcode + **/ + operator T*() { + return _data; + } + + //! Implicitely cast an image into a \c T* \const. + operator const T*() const { + return _data; + } + + //! Assign a value to all image pixels. + /** + Assign specified \c value to each pixel value of the image instance. + \param value Value that will be assigned to image pixels. + \note + - The image size is never modified. + - The \c value may be casted to pixel type \c T if necessary. + \par Example + \code + CImg img(100,100); // Declare image (with garbage values). + img = 0; // Set all pixel values to '0'. + img = 1.2; // Set all pixel values to '1' (cast of '1.2' as a 'char'). + \endcode + **/ + CImg& operator=(const T value) { + return fill(value); + } + + //! Assign pixels values from a specified expression. + /** + Initialize all pixel values from the specified string \c expression. + \param expression Value string describing the way pixel values are set. + \note + - String parameter \c expression may describe different things: + - If \c expression is a list of values (as in \c "1,2,3,8,3,2"), or a formula (as in \c "(x*y)%255"), + the pixel values are set from specified \c expression and the image size is not modified. + - If \c expression is a filename (as in \c "reference.jpg"), the corresponding image file is loaded and + replace the image instance. The image size is modified if necessary. + \par Example + \code + CImg img1(100,100), img2(img1), img3(img1); // Declare three 100x100 scalar images with unitialized pixel values. + img1 = "0,50,100,150,200,250,200,150,100,50"; // Set pixel values of 'img1' from a value sequence. + img2 = "10*((x*y)%25)"; // Set pixel values of 'img2' from a formula. + img3 = "reference.jpg"; // Set pixel values of 'img3' from a file (image size is modified). + (img1,img2,img3).display(); + \endcode + \image html ref_operator_eq.jpg + **/ + CImg& operator=(const char *const expression) { + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + fill(expression,true); + } catch (CImgException&) { + cimg::exception_mode() = omode; + load(expression); + } + cimg::exception_mode() = omode; + return *this; + } + + //! Copy an image into the current image instance. + /** + Similar to the in-place copy constructor assign(const CImg&). + **/ + template + CImg& operator=(const CImg& img) { + return assign(img); + } + + //! Copy an image into the current image instance \specialization. + CImg& operator=(const CImg& img) { + return assign(img); + } + + //! Copy the content of a display window to the current image instance. + /** + Similar to assign(const CImgDisplay&). + **/ + CImg& operator=(const CImgDisplay& disp) { + disp.snapshot(*this); + return *this; + } + + //! In-place addition operator. + /** + Add specified \c value to all pixels of an image instance. + \param value Value to add. + \note + - Resulting pixel values are casted to fit the pixel type \c T. + For instance, adding \c 0.2 to a \c CImg is possible but does nothing indeed. + - Overflow values are treated as with standard C++ numeric types. For instance, + \code + CImg img(100,100,1,1,255); // Construct a 100x100 image with pixel values '255'. + img+=1; // Add '1' to each pixels -> Overflow. + // here all pixels of image 'img' are equal to '0'. + \endcode + - To prevent value overflow, you may want to consider pixel type \c T as \c float or \c double, + and use cut() after addition. + \par Example + \code + CImg img1("reference.jpg"); // Load a 8-bits RGB image (values in [0,255]). + CImg img2(img1); // Construct a float-valued copy of 'img1'. + img2+=100; // Add '100' to pixel values -> goes out of [0,255] but no problems with floats. + img2.cut(0,255); // Cut values in [0,255] to fit the 'unsigned char' constraint. + img1 = img2; // Rewrite safe result in 'unsigned char' version 'img1'. + const CImg img3 = (img1 + 100).cut(0,255); // Do the same in a more simple and elegant way. + (img1,img2,img3).display(); + \endcode + \image html ref_operator_plus.jpg + **/ + template + CImg& operator+=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=524288) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd + value); + return *this; + } + + //! In-place addition operator. + /** + Add values to image pixels, according to the specified string \c expression. + \param expression Value string describing the way pixel values are added. + \note + - Similar to operator=(const char*), except that it adds values to the pixels of the current image instance, + instead of assigning them. + **/ + CImg& operator+=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator+="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd + mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd + mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)(*ptrd + lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd + mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this+=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place addition operator. + /** + Add values to image pixels, according to the values of the input image \c img. + \param img Input image to add. + \note + - The size of the image instance is never modified. + - It is not mandatory that input image \c img has the same size as the image instance. + If less values are available in \c img, then the values are added periodically. For instance, adding one + WxH scalar image (spectrum() equal to \c 1) to one WxH color image (spectrum() equal to \c 3) + means each color channel will be incremented with the same values at the same locations. + \par Example + \code + CImg img1("reference.jpg"); // Load a RGB color image (img1.spectrum()==3) + const CImg img2(img1.width(),img.height(),1,1,"255*(x/w)^2"); // Construct a scalar shading (img2.spectrum()==1). + img1+=img2; // Add shading to each channel of 'img1'. + img1.cut(0,255); // Prevent [0,255] overflow. + (img2,img1).display(); + \endcode + \image html ref_operator_plus1.jpg + **/ + template + CImg& operator+=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this+=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs& operator++() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=524288) +#endif + cimg_rof(*this,ptrd,T) ++*ptrd; + return *this; + } + + //! In-place increment operator (postfix). + /** + Add \c 1 to all image pixels, and return a new copy of the initial (pre-incremented) image instance. + \note + - Use the prefixed version operator++() if you don't need a copy of the initial + (pre-incremented) image instance, since a useless image copy may be expensive in terms of memory usage. + **/ + CImg operator++(int) { + const CImg copy(*this,false); + ++*this; + return copy; + } + + //! Return a non-shared copy of the image instance. + /** + \note + - Use this operator to ensure you get a non-shared copy of an image instance with same pixel type \c T. + Indeed, the usual copy constructor CImg(const CImg&) returns a shared copy of a shared input image, + and it may be not desirable to work on a regular copy (e.g. for a resize operation) if you have no + informations about the shared state of the input image. + - Writing \c (+img) is equivalent to \c CImg(img,false). + **/ + CImg operator+() const { + return CImg(*this,false); + } + + //! Addition operator. + /** + Similar to operator+=(const t), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator+(const t value) const { + return CImg<_cimg_Tt>(*this,false)+=value; + } + + //! Addition operator. + /** + Similar to operator+=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + CImg operator+(const char *const expression) const { + return CImg(*this,false)+=expression; + } + + //! Addition operator. + /** + Similar to operator+=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator+(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false)+=img; + } + + //! In-place substraction operator. + /** + Similar to operator+=(const t), except that it performs a substraction instead of an addition. + **/ + template + CImg& operator-=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=524288) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd - value); + return *this; + } + + //! In-place substraction operator. + /** + Similar to operator+=(const char*), except that it performs a substraction instead of an addition. + **/ + CImg& operator-=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator-="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd - mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd - mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)(*ptrd - lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd - mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this-=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place substraction operator. + /** + Similar to operator+=(const CImg&), except that it performs a substraction instead of an addition. + **/ + template + CImg& operator-=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this-=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs& operator--() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=524288) +#endif + cimg_rof(*this,ptrd,T) *ptrd = *ptrd-(T)1; + return *this; + } + + //! In-place decrement operator (postfix). + /** + Similar to operator++(int), except that it performs a decrement instead of an increment. + **/ + CImg operator--(int) { + const CImg copy(*this,false); + --*this; + return copy; + } + + //! Replace each pixel by its opposite value. + /** + \note + - If the computed opposite values are out-of-range, they are treated as with standard C++ numeric types. + For instance, the \c unsigned \c char opposite of \c 1 is \c 255. + \par Example + \code + const CImg + img1("reference.jpg"), // Load a RGB color image. + img2 = -img1; // Compute its opposite (in 'unsigned char'). + (img1,img2).display(); + \endcode + \image html ref_operator_minus.jpg + **/ + CImg operator-() const { + return CImg(_width,_height,_depth,_spectrum,(T)0)-=*this; + } + + //! Substraction operator. + /** + Similar to operator-=(const t), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator-(const t value) const { + return CImg<_cimg_Tt>(*this,false)-=value; + } + + //! Substraction operator. + /** + Similar to operator-=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + CImg operator-(const char *const expression) const { + return CImg(*this,false)-=expression; + } + + //! Substraction operator. + /** + Similar to operator-=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator-(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false)-=img; + } + + //! In-place multiplication operator. + /** + Similar to operator+=(const t), except that it performs a multiplication instead of an addition. + **/ + template + CImg& operator*=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=262144) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd * value); + return *this; + } + + //! In-place multiplication operator. + /** + Similar to operator+=(const char*), except that it performs a multiplication instead of an addition. + **/ + CImg& operator*=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator*="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd * mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd * mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)(*ptrd * lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd * mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + mul(CImg(_width,_height,_depth,_spectrum,expression,true)); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place multiplication operator. + /** + Replace the image instance by the matrix multiplication between the image instance and the specified matrix + \c img. + \param img Second operand of the matrix multiplication. + \note + - It does \e not compute a pointwise multiplication between two images. For this purpose, use + mul(const CImg&) instead. + - The size of the image instance can be modified by this operator. + \par Example + \code + CImg A(2,2,1,1, 1,2,3,4); // Construct 2x2 matrix A = [1,2;3,4]. + const CImg X(1,2,1,1, 1,2); // Construct 1x2 vector X = [1;2]. + A*=X; // Assign matrix multiplication A*X to 'A'. + // 'A' is now a 1x2 vector whose values are [5;11]. + \endcode + **/ + template + CImg& operator*=(const CImg& img) { + return ((*this)*img).move_to(*this); + } + + //! Multiplication operator. + /** + Similar to operator*=(const t), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator*(const t value) const { + return CImg<_cimg_Tt>(*this,false)*=value; + } + + //! Multiplication operator. + /** + Similar to operator*=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + CImg operator*(const char *const expression) const { + return CImg(*this,false)*=expression; + } + + //! Multiplication operator. + /** + Similar to operator*=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator*(const CImg& img) const { + if (_width!=img._height || _depth!=1 || _spectrum!=1) + throw CImgArgumentException(_cimg_instance + "operator*(): Invalid multiplication of instance by specified " + "matrix (%u,%u,%u,%u,%p)", + cimg_instance, + img._width,img._height,img._depth,img._spectrum,img._data); + CImg<_cimg_Tt> res(img._width,_height); +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>1024 && img.size()>1024) collapse(2) + cimg_forXY(res,i,j) { + _cimg_Ttdouble value = 0; cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k); res(i,j) = (_cimg_Tt)value; + } +#else + _cimg_Tt *ptrd = res._data; + cimg_forXY(res,i,j) { + _cimg_Ttdouble value = 0; cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k); *(ptrd++) = (_cimg_Tt)value; + } +#endif + return res; + } + + //! In-place division operator. + /** + Similar to operator+=(const t), except that it performs a division instead of an addition. + **/ + template + CImg& operator/=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd / value); + return *this; + } + + //! In-place division operator. + /** + Similar to operator+=(const char*), except that it performs a division instead of an addition. + **/ + CImg& operator/=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator/="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd / mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd / mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)(*ptrd / lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd / mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + div(CImg(_width,_height,_depth,_spectrum,expression,true)); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place division operator. + /** + Replace the image instance by the (right) matrix division between the image instance and the specified + matrix \c img. + \param img Second operand of the matrix division. + \note + - It does \e not compute a pointwise division between two images. For this purpose, use + div(const CImg&) instead. + - It returns the matrix operation \c A*inverse(img). + - The size of the image instance can be modified by this operator. + **/ + template + CImg& operator/=(const CImg& img) { + return (*this*img.get_invert()).move_to(*this); + } + + //! Division operator. + /** + Similar to operator/=(const t), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator/(const t value) const { + return CImg<_cimg_Tt>(*this,false)/=value; + } + + //! Division operator. + /** + Similar to operator/=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + CImg operator/(const char *const expression) const { + return CImg(*this,false)/=expression; + } + + //! Division operator. + /** + Similar to operator/=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator/(const CImg& img) const { + return (*this)*img.get_invert(); + } + + //! In-place modulo operator. + /** + Similar to operator+=(const t), except that it performs a modulo operation instead of an addition. + **/ + template + CImg& operator%=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=16384) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::mod(*ptrd,(T)value); + return *this; + } + + //! In-place modulo operator. + /** + Similar to operator+=(const char*), except that it performs a modulo operation instead of an addition. + **/ + CImg& operator%=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator%="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::mod(*ptrd,(T)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::mod(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)cimg::mod(*ptrd,(T)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::mod(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this%=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place modulo operator. + /** + Similar to operator+=(const CImg&), except that it performs a modulo operation instead of an addition. + **/ + template + CImg& operator%=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this%=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg<_cimg_Tt> operator%(const t value) const { + return CImg<_cimg_Tt>(*this,false)%=value; + } + + //! Modulo operator. + /** + Similar to operator%=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + CImg operator%(const char *const expression) const { + return CImg(*this,false)%=expression; + } + + //! Modulo operator. + /** + Similar to operator%=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. + **/ + template + CImg<_cimg_Tt> operator%(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false)%=img; + } + + //! In-place bitwise AND operator. + /** + Similar to operator+=(const t), except that it performs a bitwise AND operation instead of an addition. + **/ + template + CImg& operator&=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)((unsigned long)*ptrd & (unsigned long)value); + return *this; + } + + //! In-place bitwise AND operator. + /** + Similar to operator+=(const char*), except that it performs a bitwise AND operation instead of an addition. + **/ + CImg& operator&=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator&="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') + cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this&=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place bitwise AND operator. + /** + Similar to operator+=(const CImg&), except that it performs a bitwise AND operation instead of an addition. + **/ + template + CImg& operator&=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this&=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg operator&(const t value) const { + return (+*this)&=value; + } + + //! Bitwise AND operator. + /** + Similar to operator&=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + CImg operator&(const char *const expression) const { + return (+*this)&=expression; + } + + //! Bitwise AND operator. + /** + Similar to operator&=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + template + CImg operator&(const CImg& img) const { + return (+*this)&=img; + } + + //! In-place bitwise OR operator. + /** + Similar to operator+=(const t), except that it performs a bitwise OR operation instead of an addition. + **/ + template + CImg& operator|=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)((unsigned long)*ptrd | (unsigned long)value); + return *this; + } + + //! In-place bitwise OR operator. + /** + Similar to operator+=(const char*), except that it performs a bitwise OR operation instead of an addition. + **/ + CImg& operator|=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator|="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') + cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this|=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place bitwise OR operator. + /** + Similar to operator+=(const CImg&), except that it performs a bitwise OR operation instead of an addition. + **/ + template + CImg& operator|=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this|=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg operator|(const t value) const { + return (+*this)|=value; + } + + //! Bitwise OR operator. + /** + Similar to operator|=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + CImg operator|(const char *const expression) const { + return (+*this)|=expression; + } + + //! Bitwise OR operator. + /** + Similar to operator|=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + template + CImg operator|(const CImg& img) const { + return (+*this)|=img; + } + + //! In-place bitwise XOR operator. + /** + Similar to operator+=(const t), except that it performs a bitwise XOR operation instead of an addition. + \warning + - It does \e not compute the \e power of pixel values. For this purpose, use pow(const t) instead. + **/ + template + CImg& operator^=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)value); + return *this; + } + + //! In-place bitwise XOR operator. + /** + Similar to operator+=(const char*), except that it performs a bitwise XOR operation instead of an addition. + \warning + - It does \e not compute the \e power of pixel values. For this purpose, use pow(const char*) instead. + **/ + CImg& operator^=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator^="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') + cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this^=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place bitwise XOR operator. + /** + Similar to operator+=(const CImg&), except that it performs a bitwise XOR operation instead of an addition. + \warning + - It does \e not compute the \e power of pixel values. For this purpose, use pow(const CImg&) instead. + **/ + template + CImg& operator^=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this^=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg operator^(const t value) const { + return (+*this)^=value; + } + + //! Bitwise XOR operator. + /** + Similar to operator^=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + CImg operator^(const char *const expression) const { + return (+*this)^=expression; + } + + //! Bitwise XOR operator. + /** + Similar to operator^=(const CImg&), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + template + CImg operator^(const CImg& img) const { + return (+*this)^=img; + } + + //! In-place bitwise left shift operator. + /** + Similar to operator+=(const t), except that it performs a bitwise left shift instead of an addition. + **/ + template + CImg& operator<<=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)(((long)*ptrd) << (int)value); + return *this; + } + + //! In-place bitwise left shift operator. + /** + Similar to operator+=(const char*), except that it performs a bitwise left shift instead of an addition. + **/ + CImg& operator<<=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator<<="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd << (int)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd << (int)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)((long)*ptrd << (int)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd << (int)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this<<=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place bitwise left shift operator. + /** + Similar to operator+=(const CImg&), except that it performs a bitwise left shift instead of an addition. + **/ + template + CImg& operator<<=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this^=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg operator<<(const t value) const { + return (+*this)<<=value; + } + + //! Bitwise left shift operator. + /** + Similar to operator<<=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + CImg operator<<(const char *const expression) const { + return (+*this)<<=expression; + } + + //! Bitwise left shift operator. + /** + Similar to operator<<=(const CImg&), except that it returns a new image instance instead of + operating in-place. + The pixel type of the returned image is \c T. + **/ + template + CImg operator<<(const CImg& img) const { + return (+*this)<<=img; + } + + //! In-place bitwise right shift operator. + /** + Similar to operator+=(const t), except that it performs a bitwise right shift instead of an addition. + **/ + template + CImg& operator>>=(const t value) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)(((long)*ptrd) >> (int)value); + return *this; + } + + //! In-place bitwise right shift operator. + /** + Similar to operator+=(const char*), except that it performs a bitwise right shift instead of an addition. + **/ + CImg& operator>>=(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator<<="); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd >> (int)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd >> (int)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)((long)*ptrd >> (int)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd >> (int)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + cimg::exception_mode() = omode; + *this>>=CImg(_width,_height,_depth,_spectrum,expression,true); + } + cimg::exception_mode() = omode; + return *this; + } + + //! In-place bitwise right shift operator. + /** + Similar to operator+=(const CImg&), except that it performs a bitwise right shift instead of an addition. + **/ + template + CImg& operator>>=(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return *this^=+img; + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs> (int)*(ptrs++)); + for (const t *ptrs = img._data; ptrd> (int)*(ptrs++)); + } + return *this; + } + + //! Bitwise right shift operator. + /** + Similar to operator>>=(const t), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + template + CImg operator>>(const t value) const { + return (+*this)>>=value; + } + + //! Bitwise right shift operator. + /** + Similar to operator>>=(const char*), except that it returns a new image instance instead of operating in-place. + The pixel type of the returned image is \c T. + **/ + CImg operator>>(const char *const expression) const { + return (+*this)>>=expression; + } + + //! Bitwise right shift operator. + /** + Similar to operator>>=(const CImg&), except that it returns a new image instance instead of + operating in-place. + The pixel type of the returned image is \c T. + **/ + template + CImg operator>>(const CImg& img) const { + return (+*this)>>=img; + } + + //! Bitwise inversion operator. + /** + Similar to operator-(), except that it compute the bitwise inverse instead of the opposite value. + **/ + CImg operator~() const { + CImg res(_width,_height,_depth,_spectrum); + const T *ptrs = _data; + cimg_for(res,ptrd,T) { const unsigned long value = (unsigned long)*(ptrs++); *ptrd = (T)~value; } + return res; + } + + //! Test if all pixels of an image have the same value. + /** + Return \c true is all pixels of the image instance are equal to the specified \c value. + \param value Reference value to compare with. + **/ + template + bool operator==(const t value) const { + if (is_empty()) return false; + typedef _cimg_Tt Tt; + bool is_equal = true; + for (T *ptrd = _data + size(); is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)value)) {} + return is_equal; + } + + //! Test if all pixel values of an image follow a specified expression. + /** + Return \c true is all pixels of the image instance are equal to the specified \c expression. + \param expression Value string describing the way pixel values are compared. + **/ + bool operator==(const char *const expression) const { + if (is_empty()) return !*expression; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + bool is_equal = true; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator<<="); + const T *ptrs = *expression=='<'?end()-1:_data; + if (*expression=='<') + cimg_rofXYZC(*this,x,y,z,c) { if (!is_equal) break; is_equal = ((double)*(ptrs--)==mp(x,y,z,c)); } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { if (!is_equal) break; is_equal = ((double)*(ptrs++)==mp(x,y,z,c)); } + else cimg_forXYZC(*this,x,y,z,c) { if (!is_equal) break; is_equal = ((double)*(ptrs++)==mp(x,y,z,c)); } + } catch (CImgException&) { + cimg::exception_mode() = omode; + is_equal = (*this==CImg(_width,_height,_depth,_spectrum,expression,true)); + } + cimg::exception_mode() = omode; + return is_equal; + } + + //! Test if two images have the same size and values. + /** + Return \c true if the image instance and the input image \c img have the same dimensions and pixel values, + and \c false otherwise. + \param img Input image to compare with. + \note + - The pixel buffer pointers data() of the two compared images do not have to be the same for operator==() + to return \c true. + Only the dimensions and the pixel values matter. Thus, the comparison can be \c true even for different + pixel types \c T and \c t. + \par Example + \code + const CImg img1(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'float' pixel values). + const CImg img2(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'char' pixel values). + if (img1==img2) { // Test succeeds, image dimensions and values are the same. + std::printf("'img1' and 'img2' have same dimensions and values."); + } + \endcode + **/ + template + bool operator==(const CImg& img) const { + typedef _cimg_Tt Tt; + const unsigned long siz = size(); + bool is_equal = true; + if (siz!=img.size()) return false; + t *ptrs = img._data + siz; + for (T *ptrd = _data + siz; is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)*(--ptrs))) {} + return is_equal; + } + + //! Test if pixels of an image are all different from a value. + /** + Return \c true is all pixels of the image instance are different than the specified \c value. + \param value Reference value to compare with. + **/ + template + bool operator!=(const t value) const { + return !((*this)==value); + } + + //! Test if all pixel values of an image are different from a specified expression. + /** + Return \c true is all pixels of the image instance are different to the specified \c expression. + \param expression Value string describing the way pixel values are compared. + **/ + bool operator!=(const char *const expression) const { + return !((*this)==expression); + } + + //! Test if two images have different sizes or values. + /** + Return \c true if the image instance and the input image \c img have different dimensions or pixel values, + and \c false otherwise. + \param img Input image to compare with. + \note + - Writing \c img1!=img2 is equivalent to \c !(img1==img2). + **/ + template + bool operator!=(const CImg& img) const { + return !((*this)==img); + } + + //! Construct an image list from two images. + /** + Return a new list of image (\c CImgList instance) containing exactly two elements: + - A copy of the image instance, at position [\c 0]. + - A copy of the specified image \c img, at position [\c 1]. + + \param img Input image that will be the second image of the resulting list. + \note + - The family of operator,() is convenient to easily create list of images, but it is also \e quite \e slow + in practice (see warning below). + - Constructed lists contain no shared images. If image instance or input image \c img are shared, they are + inserted as new non-shared copies in the resulting list. + - The pixel type of the returned list may be a superset of the initial pixel type \c T, if necessary. + \warning + - Pipelining operator,() \c N times will perform \c N copies of the entire content of a (growing) image list. + This may become very expensive in terms of speed and used memory. You should avoid using this technique to + build a new CImgList instance from several images, if you are seeking for performance. + Fast insertions of images in an image list are possible with + CImgList::insert(const CImg&,unsigned int,bool) or move_to(CImgList&,unsigned int). + \par Example + \code + const CImg + img1("reference.jpg"), + img2 = img1.get_mirror('x'), + img3 = img2.get_blur(5); + const CImgList list = (img1,img2); // Create list of two elements from 'img1' and 'img2'. + (list,img3).display(); // Display image list containing copies of 'img1','img2' and 'img3'. + \endcode + \image html ref_operator_comma.jpg + **/ + template + CImgList<_cimg_Tt> operator,(const CImg& img) const { + return CImgList<_cimg_Tt>(*this,img); + } + + //! Construct an image list from image instance and an input image list. + /** + Return a new list of images (\c CImgList instance) containing exactly \c list.size() \c + \c 1 elements: + - A copy of the image instance, at position [\c 0]. + - A copy of the specified image list \c list, from positions [\c 1] to [\c list.size()]. + + \param list Input image list that will be appended to the image instance. + \note + - Similar to operator,(const CImg&) const, except that it takes an image list as an argument. + **/ + template + CImgList<_cimg_Tt> operator,(const CImgList& list) const { + return CImgList<_cimg_Tt>(list,false).insert(*this,0); + } + + //! Split image along specified axis. + /** + Return a new list of images (\c CImgList instance) containing the splitted components + of the instance image along the specified axis. + \param axis Splitting axis (can be '\c x','\c y','\c z' or '\c c') + \note + - Similar to get_split(char,int) const, with default second argument. + \par Example + \code + const CImg img("reference.jpg"); // Load a RGB color image. + const CImgList list = (img<'c'); // Get a list of its three R,G,B channels. + (img,list).display(); + \endcode + \image html ref_operator_less.jpg + **/ + CImgList operator<(const char axis) const { + return get_split(axis); + } + + //@} + //------------------------------------- + // + //! \name Instance Characteristics + //@{ + //------------------------------------- + + //! Return the type of image pixel values as a C string. + /** + Return a \c char* string containing the usual type name of the image pixel values + (i.e. a stringified version of the template parameter \c T). + \note + - The returned string may contain spaces (as in \c "unsigned char"). + - If the pixel type \c T does not correspond to a registered type, the string "unknown" is returned. + **/ + static const char* pixel_type() { + return cimg::type::string(); + } + + //! Return the number of image columns. + /** + Return the image width, i.e. the image dimension along the X-axis. + \note + - The width() of an empty image is equal to \c 0. + - width() is typically equal to \c 1 when considering images as \e vectors for matrix calculations. + - width() returns an \c int, although the image width is internally stored as an \c unsigned \c int. + Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving + \c unsigned \c int variables. + Access to the initial \c unsigned \c int variable is possible (though not recommended) by + (*this)._width. + **/ + int width() const { + return (int)_width; + } + + //! Return the number of image rows. + /** + Return the image height, i.e. the image dimension along the Y-axis. + \note + - The height() of an empty image is equal to \c 0. + - height() returns an \c int, although the image height is internally stored as an \c unsigned \c int. + Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving + \c unsigned \c int variables. + Access to the initial \c unsigned \c int variable is possible (though not recommended) by + (*this)._height. + **/ + int height() const { + return (int)_height; + } + + //! Return the number of image slices. + /** + Return the image depth, i.e. the image dimension along the Z-axis. + \note + - The depth() of an empty image is equal to \c 0. + - depth() is typically equal to \c 1 when considering usual 2d images. When depth()\c > \c 1, the image + is said to be \e volumetric. + - depth() returns an \c int, although the image depth is internally stored as an \c unsigned \c int. + Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving + \c unsigned \c int variables. + Access to the initial \c unsigned \c int variable is possible (though not recommended) by + (*this)._depth. + **/ + int depth() const { + return (int)_depth; + } + + //! Return the number of image channels. + /** + Return the number of image channels, i.e. the image dimension along the C-axis. + \note + - The spectrum() of an empty image is equal to \c 0. + - spectrum() is typically equal to \c 1 when considering scalar-valued images, to \c 3 + for RGB-coded color images, and to \c 4 for RGBA-coded color images (with alpha-channel). + The number of channels of an image instance is not limited. The meaning of the pixel values is not linked + up to the number of channels (e.g. a 4-channel image may indifferently stands for a RGBA or CMYK color image). + - spectrum() returns an \c int, although the image spectrum is internally stored as an \c unsigned \c int. + Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving + \c unsigned \c int variables. + Access to the initial \c unsigned \c int variable is possible (though not recommended) by + (*this)._spectrum. + **/ + int spectrum() const { + return (int)_spectrum; + } + + //! Return the total number of pixel values. + /** + Return width()*\ref height()*\ref depth()*\ref spectrum(), + i.e. the total number of values of type \c T in the pixel buffer of the image instance. + \note + - The size() of an empty image is equal to \c 0. + - The allocated memory size for a pixel buffer of a non-shared \c CImg instance is equal to + size()*sizeof(T). + \par Example + \code + const CImg img(100,100,1,3); // Construct new 100x100 color image. + if (img.size()==30000) // Test succeeds. + std::printf("Pixel buffer uses %lu bytes", + img.size()*sizeof(float)); + \endcode + **/ + unsigned long size() const { + return (unsigned long)_width*_height*_depth*_spectrum; + } + + //! Return a pointer to the first pixel value. + /** + Return a \c T*, or a \c const \c T* pointer to the first value in the pixel buffer of the image instance, + whether the instance is \c const or not. + \note + - The data() of an empty image is equal to \c 0 (null pointer). + - The allocated pixel buffer for the image instance starts from \c data() + and goes to data()+\ref size()-1 (included). + - To get the pointer to one particular location of the pixel buffer, use + data(unsigned int,unsigned int,unsigned int,unsigned int) instead. + **/ + T* data() { + return _data; + } + + //! Return a pointer to the first pixel value \const. + const T* data() const { + return _data; + } + + //! Return a pointer to a located pixel value. + /** + Return a \c T*, or a \c const \c T* pointer to the value located at (\c x,\c y,\c z,\c c) in the pixel buffer + of the image instance, + whether the instance is \c const or not. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Writing \c img.data(x,y,z,c) is equivalent to &(img(x,y,z,c)). Thus, this method has the same + properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int). + **/ +#if cimg_verbosity>=3 + T *data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) { + const unsigned long off = (unsigned long)offset(x,y,z,c); + if (off>=size()) + cimg::warn(_cimg_instance + "data(): Invalid pointer request, at coordinates (%u,%u,%u,%u) [offset=%u].", + cimg_instance, + x,y,z,c,off); + return _data + off; + } + + //! Return a pointer to a located pixel value \const. + const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const { + return const_cast*>(this)->data(x,y,z,c); + } +#else + T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) { + return _data + x + y*(unsigned long)_width + z*(unsigned long)_width*_height + + c*(unsigned long)_width*_height*_depth; + } + + const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const { + return _data + x + y*_width + z*(unsigned long)_width*_height + c*(unsigned long)_width*_height*_depth; + } +#endif + + //! Return the offset to a located pixel value, with respect to the beginning of the pixel buffer. + /** + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Writing \c img.data(x,y,z,c) is equivalent to &(img(x,y,z,c)) - img.data(). + Thus, this method has the same properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int). + \par Example + \code + const CImg img(100,100,1,3); // Define a 100x100 RGB-color image. + const long off = img.offset(10,10,0,2); // Get the offset of the blue value of the pixel located at (10,10). + const float val = img[off]; // Get the blue value of this pixel. + \endcode + **/ + long offset(const int x, const int y=0, const int z=0, const int c=0) const { + return x + y*(long)_width + z*(long)_width*_height + c*(long)_width*_height*_depth; + } + + //! Return a CImg::iterator pointing to the first pixel value. + /** + \note + - Equivalent to data(). + - It has been mainly defined for compatibility with STL naming conventions. + **/ + iterator begin() { + return _data; + } + + //! Return a CImg::iterator pointing to the first value of the pixel buffer \const. + const_iterator begin() const { + return _data; + } + + //! Return a CImg::iterator pointing next to the last pixel value. + /** + \note + - Writing \c img.end() is equivalent to img.data() + img.size(). + - It has been mainly defined for compatibility with STL naming conventions. + \warning + - The returned iterator actually points to a value located \e outside the acceptable bounds of the pixel buffer. + Trying to read or write the content of the returned iterator will probably result in a crash. + Use it mainly as a strict upper bound for a CImg::iterator. + \par Example + \code + CImg img(100,100,1,3); // Define a 100x100 RGB color image. + for (CImg::iterator it = img.begin(); it::iterator pointing next to the last pixel value \const. + const_iterator end() const { + return _data + size(); + } + + //! Return a reference to the first pixel value. + /** + \note + - Writing \c img.front() is equivalent to img[0], or img(0,0,0,0). + - It has been mainly defined for compatibility with STL naming conventions. + **/ + T& front() { + return *_data; + } + + //! Return a reference to the first pixel value \const. + const T& front() const { + return *_data; + } + + //! Return a reference to the last pixel value. + /** + \note + - Writing \c img.end() is equivalent to img[img.size()-1], or + img(img.width()-1,img.height()-1,img.depth()-1,img.spectrum()-1). + - It has been mainly defined for compatibility with STL naming conventions. + **/ + T& back() { + return *(_data + size() - 1); + } + + //! Return a reference to the last pixel value \const. + const T& back() const { + return *(_data + size() - 1); + } + + //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions. + /** + Return a reference to the pixel value of the image instance located at a specified \c offset, + or to a specified default value in case of out-of-bounds access. + \param offset Offset to the desired pixel value. + \param out_value Default value returned if \c offset is outside image bounds. + \note + - Writing \c img.at(offset,out_value) is similar to img[offset], except that if \c offset + is outside bounds (e.g. \c offset<0 or \c offset>=img.size()), a reference to a value \c out_value + is safely returned instead. + - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when + you are \e not sure about the validity of the specified pixel offset. + **/ + T& at(const int offset, const T out_value) { + return (offset<0 || offset>=(int)size())?(cimg::temporary(out_value)=out_value):(*this)[offset]; + } + + //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions \const. + T at(const int offset, const T out_value) const { + return (offset<0 || offset>=(int)size())?out_value:(*this)[offset]; + } + + //! Access to a pixel value at a specified offset, using Neumann boundary conditions. + /** + Return a reference to the pixel value of the image instance located at a specified \c offset, + or to the nearest pixel location in the image instance in case of out-of-bounds access. + \param offset Offset to the desired pixel value. + \note + - Similar to at(int,const T), except that an out-of-bounds access returns the value of the + nearest pixel in the image instance, regarding the specified offset, i.e. + - If \c offset<0, then \c img[0] is returned. + - If \c offset>=img.size(), then \c img[img.size()-1] is returned. + - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when + you are \e not sure about the validity of the specified pixel offset. + - If you know your image instance is \e not empty, you may rather use the slightly faster method \c _at(int). + **/ + T& at(const int offset) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "at(): Empty instance.", + cimg_instance); + return _at(offset); + } + + T& _at(const int offset) { + const unsigned int siz = (unsigned int)size(); + return (*this)[offset<0?0:(unsigned int)offset>=siz?siz-1:offset]; + } + + //! Access to a pixel value at a specified offset, using Neumann boundary conditions \const. + T at(const int offset) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "at(): Empty instance.", + cimg_instance); + return _at(offset); + } + + T _at(const int offset) const { + const unsigned int siz = (unsigned int)size(); + return (*this)[offset<0?0:(unsigned int)offset>=siz?siz-1:offset]; + } + + //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate. + /** + Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c), + or to a specified default value in case of out-of-bounds access along the X-axis. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c (\c x,\c y,\c z,\c c) is outside image bounds. + \note + - Similar to operator()(), except that an out-of-bounds access along the X-axis returns the specified value + \c out_value. + - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when + you are \e not sure about the validity of the specified pixel coordinates. + \warning + - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. + **/ + T& atX(const int x, const int y, const int z, const int c, const T out_value) { + return (x<0 || x>=width())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate \const. + T atX(const int x, const int y, const int z, const int c, const T out_value) const { + return (x<0 || x>=width())?out_value:(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate. + /** + Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c), + or to the nearest pixel location in the image instance in case of out-of-bounds access along the X-axis. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Similar to at(int,int,int,int,const T), except that an out-of-bounds access returns the value of the + nearest pixel in the image instance, regarding the specified X-coordinate. + - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when + you are \e not sure about the validity of the specified pixel coordinates. + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _at(int,int,int,int). + \warning + - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. + **/ + T& atX(const int x, const int y=0, const int z=0, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atX(): Empty instance.", + cimg_instance); + return _atX(x,y,z,c); + } + + T& _atX(const int x, const int y=0, const int z=0, const int c=0) { + return (*this)(x<0?0:(x>=width()?width()-1:x),y,z,c); + } + + //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate \const. + T atX(const int x, const int y=0, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atX(): Empty instance.", + cimg_instance); + return _atX(x,y,z,c); + } + + T _atX(const int x, const int y=0, const int z=0, const int c=0) const { + return (*this)(x<0?0:(x>=width()?width()-1:x),y,z,c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y-coordinates. + /** + Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on X and Y-coordinates. + **/ + T& atXY(const int x, const int y, const int z, const int c, const T out_value) { + return (x<0 || y<0 || x>=width() || y>=height())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y coordinates \const. + T atXY(const int x, const int y, const int z, const int c, const T out_value) const { + return (x<0 || y<0 || x>=width() || y>=height())?out_value:(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates. + /** + Similar to atX(int,int,int,int), except that boundary checking is performed both on X and Y-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _atXY(int,int,int,int). + **/ + T& atXY(const int x, const int y, const int z=0, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atXY(): Empty instance.", + cimg_instance); + return _atXY(x,y,z,c); + } + + T& _atXY(const int x, const int y, const int z=0, const int c=0) { + return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y),z,c); + } + + //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates \const. + T atXY(const int x, const int y, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atXY(): Empty instance.", + cimg_instance); + return _atXY(x,y,z,c); + } + + T _atXY(const int x, const int y, const int z=0, const int c=0) const { + return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y),z,c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates. + /** + Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on + X,Y and Z-coordinates. + **/ + T& atXYZ(const int x, const int y, const int z, const int c, const T out_value) { + return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())? + (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates \const. + T atXYZ(const int x, const int y, const int z, const int c, const T out_value) const { + return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())?out_value:(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates. + /** + Similar to atX(int,int,int,int), except that boundary checking is performed both on X,Y and Z-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _atXYZ(int,int,int,int). + **/ + T& atXYZ(const int x, const int y, const int z, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atXYZ(): Empty instance.", + cimg_instance); + return _atXYZ(x,y,z,c); + } + + T& _atXYZ(const int x, const int y, const int z, const int c=0) { + return (*this)(x<0?0:(x>=width()?width()-1:x),y<0?0:(y>=height()?height()-1:y), + z<0?0:(z>=depth()?depth()-1:z),c); + } + + //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates \const. + T atXYZ(const int x, const int y, const int z, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atXYZ(): Empty instance.", + cimg_instance); + return _atXYZ(x,y,z,c); + } + + T _atXYZ(const int x, const int y, const int z, const int c=0) const { + return (*this)(x<0?0:(x>=width()?width()-1:x),y<0?0:(y>=height()?height()-1:y), + z<0?0:(z>=depth()?depth()-1:z),c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions. + /** + Similar to atX(int,int,int,int,const T), except that boundary checking is performed on all + X,Y,Z and C-coordinates. + **/ + T& atXYZC(const int x, const int y, const int z, const int c, const T out_value) { + return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())? + (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); + } + + //! Access to a pixel value, using Dirichlet boundary conditions \const. + T atXYZC(const int x, const int y, const int z, const int c, const T out_value) const { + return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())?out_value: + (*this)(x,y,z,c); + } + + //! Access to a pixel value, using Neumann boundary conditions. + /** + Similar to atX(int,int,int,int), except that boundary checking is performed on all X,Y,Z and C-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _atXYZC(int,int,int,int). + **/ + T& atXYZC(const int x, const int y, const int z, const int c) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atXYZC(): Empty instance.", + cimg_instance); + return _atXYZC(x,y,z,c); + } + + T& _atXYZC(const int x, const int y, const int z, const int c) { + return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y), + z<0?0:(z>=depth()?depth()-1:z), c<0?0:(c>=spectrum()?spectrum()-1:c)); + } + + //! Access to a pixel value, using Neumann boundary conditions \const. + T atXYZC(const int x, const int y, const int z, const int c) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "atXYZC(): Empty instance.", + cimg_instance); + return _atXYZC(x,y,z,c); + } + + T _atXYZC(const int x, const int y, const int z, const int c) const { + return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y), + z<0?0:(z>=depth()?depth()-1:z), c<0?0:(c>=spectrum()?spectrum()-1:c)); + } + + //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X-coordinate. + /** + Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), + or a specified default value in case of out-of-bounds access along the X-axis. + \param fx X-coordinate of the pixel value (float-valued). + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds. + \note + - Similar to atX(int,int,int,int,const T), except that the returned pixel value is approximated by + a linear interpolation along the X-axis, if corresponding coordinates are not integers. + - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued. + \warning + - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. + **/ + Tfloat linear_atX(const float fx, const int y, const int z, const int c, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), nx = x + 1; + const float + dx = fx - x; + const Tfloat + Ic = (Tfloat)atX(x,y,z,c,out_value), In = (Tfloat)atXY(nx,y,z,c,out_value); + return Ic + dx*(In-Ic); + } + + //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X-coordinate. + /** + Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), + or the value of the nearest pixel location in the image instance in case of out-of-bounds access along + the X-axis. + \param fx X-coordinate of the pixel value (float-valued). + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Similar to linear_atX(float,int,int,int,const T) const, except that an out-of-bounds access returns + the value of the nearest pixel in the image instance, regarding the specified X-coordinate. + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _linear_atX(float,int,int,int). + \warning + - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. + **/ + Tfloat linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "linear_atX(): Empty instance.", + cimg_instance); + + return _linear_atX(fx,y,z,c); + } + + Tfloat _linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx); + const unsigned int + x = (unsigned int)nfx; + const float + dx = nfx - x; + const unsigned int + nx = dx>0?x+1:x; + const Tfloat + Ic = (Tfloat)(*this)(x,y,z,c), In = (Tfloat)(*this)(nx,y,z,c); + return Ic + dx*(In-Ic); + } + + //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X and Y-coordinates. + /** + Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the + boundary checking are achieved both for X and Y-coordinates. + **/ + Tfloat linear_atXY(const float fx, const float fy, const int z, const int c, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), nx = x + 1, + y = (int)fy - (fy>=0?0:1), ny = y + 1; + const float + dx = fx - x, + dy = fy - y; + const Tfloat + Icc = (Tfloat)atXY(x,y,z,c,out_value), Inc = (Tfloat)atXY(nx,y,z,c,out_value), + Icn = (Tfloat)atXY(x,ny,z,c,out_value), Inn = (Tfloat)atXY(nx,ny,z,c,out_value); + return Icc + dx*(Inc-Icc + dy*(Icc+Inn-Icn-Inc)) + dy*(Icn-Icc); + } + + //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X and Y-coordinates. + /** + Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking + are achieved both for X and Y-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _linear_atXY(float,float,int,int). + **/ + Tfloat linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "linear_atXY(): Empty instance.", + cimg_instance); + + return _linear_atXY(fx,fy,z,c); + } + + Tfloat _linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx), + nfy = fy<0?0:(fy>_height-1?_height-1:fy); + const unsigned int + x = (unsigned int)nfx, + y = (unsigned int)nfy; + const float + dx = nfx - x, + dy = nfy - y; + const unsigned int + nx = dx>0?x+1:x, + ny = dy>0?y+1:y; + const Tfloat + Icc = (Tfloat)(*this)(x,y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c), + Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c); + return Icc + dx*(Inc-Icc + dy*(Icc+Inn-Icn-Inc)) + dy*(Icn-Icc); + } + + //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates. + /** + Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the + boundary checking are achieved both for X,Y and Z-coordinates. + **/ + Tfloat linear_atXYZ(const float fx, const float fy, const float fz, const int c, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), nx = x + 1, + y = (int)fy - (fy>=0?0:1), ny = y + 1, + z = (int)fz - (fz>=0?0:1), nz = z + 1; + const float + dx = fx - x, + dy = fy - y, + dz = fz - z; + const Tfloat + Iccc = (Tfloat)atXYZ(x,y,z,c,out_value), Incc = (Tfloat)atXYZ(nx,y,z,c,out_value), + Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value), Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value), + Iccn = (Tfloat)atXYZ(x,y,nz,c,out_value), Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value), + Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value), Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value); + return Iccc + + dx*(Incc-Iccc + + dy*(Iccc+Innc-Icnc-Incc + + dz*(Iccn+Innn+Icnc+Incc-Icnn-Incn-Iccc-Innc)) + + dz*(Iccc+Incn-Iccn-Incc)) + + dy*(Icnc-Iccc + + dz*(Iccc+Icnn-Iccn-Icnc)) + + dz*(Iccn-Iccc); + } + + //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X,Y and Z-coordinates. + /** + Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking + are achieved both for X,Y and Z-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _linear_atXYZ(float,float,float,int). + **/ + Tfloat linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "linear_atXYZ(): Empty instance.", + cimg_instance); + + return _linear_atXYZ(fx,fy,fz,c); + } + + Tfloat _linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx), + nfy = fy<0?0:(fy>_height-1?_height-1:fy), + nfz = fz<0?0:(fz>_depth-1?_depth-1:fz); + const unsigned int + x = (unsigned int)nfx, + y = (unsigned int)nfy, + z = (unsigned int)nfz; + const float + dx = nfx - x, + dy = nfy - y, + dz = nfz - z; + const unsigned int + nx = dx>0?x+1:x, + ny = dy>0?y+1:y, + nz = dz>0?z+1:z; + const Tfloat + Iccc = (Tfloat)(*this)(x,y,z,c), Incc = (Tfloat)(*this)(nx,y,z,c), + Icnc = (Tfloat)(*this)(x,ny,z,c), Innc = (Tfloat)(*this)(nx,ny,z,c), + Iccn = (Tfloat)(*this)(x,y,nz,c), Incn = (Tfloat)(*this)(nx,y,nz,c), + Icnn = (Tfloat)(*this)(x,ny,nz,c), Innn = (Tfloat)(*this)(nx,ny,nz,c); + return Iccc + + dx*(Incc-Iccc + + dy*(Iccc+Innc-Icnc-Incc + + dz*(Iccn+Innn+Icnc+Incc-Icnn-Incn-Iccc-Innc)) + + dz*(Iccc+Incn-Iccn-Incc)) + + dy*(Icnc-Iccc + + dz*(Iccc+Icnn-Iccn-Icnc)) + + dz*(Iccn-Iccc); + } + + //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for all X,Y,Z,C-coordinates. + /** + Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the + boundary checking are achieved for all X,Y,Z and C-coordinates. + **/ + Tfloat linear_atXYZC(const float fx, const float fy, const float fz, const float fc, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), nx = x + 1, + y = (int)fy - (fy>=0?0:1), ny = y + 1, + z = (int)fz - (fz>=0?0:1), nz = z + 1, + c = (int)fc - (fc>=0?0:1), nc = c + 1; + const float + dx = fx - x, + dy = fy - y, + dz = fz - z, + dc = fc - c; + const Tfloat + Icccc = (Tfloat)atXYZC(x,y,z,c,out_value), Inccc = (Tfloat)atXYZC(nx,y,z,c,out_value), + Icncc = (Tfloat)atXYZC(x,ny,z,c,out_value), Inncc = (Tfloat)atXYZC(nx,ny,z,c,out_value), + Iccnc = (Tfloat)atXYZC(x,y,nz,c,out_value), Incnc = (Tfloat)atXYZC(nx,y,nz,c,out_value), + Icnnc = (Tfloat)atXYZC(x,ny,nz,c,out_value), Innnc = (Tfloat)atXYZC(nx,ny,nz,c,out_value), + Icccn = (Tfloat)atXYZC(x,y,z,nc,out_value), Inccn = (Tfloat)atXYZC(nx,y,z,nc,out_value), + Icncn = (Tfloat)atXYZC(x,ny,z,nc,out_value), Inncn = (Tfloat)atXYZC(nx,ny,z,nc,out_value), + Iccnn = (Tfloat)atXYZC(x,y,nz,nc,out_value), Incnn = (Tfloat)atXYZC(nx,y,nz,nc,out_value), + Icnnn = (Tfloat)atXYZC(x,ny,nz,nc,out_value), Innnn = (Tfloat)atXYZC(nx,ny,nz,nc,out_value); + return Icccc + + dx*(Inccc-Icccc + + dy*(Icccc+Inncc-Icncc-Inccc + + dz*(Iccnc+Innnc+Icncc+Inccc-Icnnc-Incnc-Icccc-Inncc + + dc*(Iccnn+Innnn+Icncn+Inccn+Icnnc+Incnc+Icccc+Inncc- + Icnnn-Incnn-Icccn-Inncn-Iccnc-Innnc-Icncc-Inccc)) + + dc*(Icccn+Inncn+Icncc+Inccc-Icncn-Inccn-Icccc-Inncc)) + + dz*(Icccc+Incnc-Iccnc-Inccc + + dc*(Icccn+Incnn+Iccnc+Inccc-Iccnn-Inccn-Icccc-Incnc)) + + dc*(Icccc+Inccn-Inccc-Icccn)) + + dy*(Icncc-Icccc + + dz*(Icccc+Icnnc-Iccnc-Icncc + + dc*(Icccn+Icnnn+Iccnc+Icncc-Iccnn-Icncn-Icccc-Icnnc)) + + dc*(Icccc+Icncn-Icncc-Icccn)) + + dz*(Iccnc-Icccc + + dc*(Icccc+Iccnn-Iccnc-Icccn)) + + dc*(Icccn-Icccc); + } + + //! Return pixel value, using linear interpolation and Neumann boundary conditions for all X,Y,Z and C-coordinates. + /** + Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking + are achieved for all X,Y,Z and C-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _linear_atXYZC(float,float,float,float). + **/ + Tfloat linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "linear_atXYZC(): Empty instance.", + cimg_instance); + + return _linear_atXYZC(fx,fy,fz,fc); + } + + Tfloat _linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx), + nfy = fy<0?0:(fy>_height-1?_height-1:fy), + nfz = fz<0?0:(fz>_depth-1?_depth-1:fz), + nfc = fc<0?0:(fc>_spectrum-1?_spectrum-1:fc); + const unsigned int + x = (unsigned int)nfx, + y = (unsigned int)nfy, + z = (unsigned int)nfz, + c = (unsigned int)nfc; + const float + dx = nfx - x, + dy = nfy - y, + dz = nfz - z, + dc = nfc - c; + const unsigned int + nx = dx>0?x+1:x, + ny = dy>0?y+1:y, + nz = dz>0?z+1:z, + nc = dc>0?c+1:c; + const Tfloat + Icccc = (Tfloat)(*this)(x,y,z,c), Inccc = (Tfloat)(*this)(nx,y,z,c), + Icncc = (Tfloat)(*this)(x,ny,z,c), Inncc = (Tfloat)(*this)(nx,ny,z,c), + Iccnc = (Tfloat)(*this)(x,y,nz,c), Incnc = (Tfloat)(*this)(nx,y,nz,c), + Icnnc = (Tfloat)(*this)(x,ny,nz,c), Innnc = (Tfloat)(*this)(nx,ny,nz,c), + Icccn = (Tfloat)(*this)(x,y,z,nc), Inccn = (Tfloat)(*this)(nx,y,z,nc), + Icncn = (Tfloat)(*this)(x,ny,z,nc), Inncn = (Tfloat)(*this)(nx,ny,z,nc), + Iccnn = (Tfloat)(*this)(x,y,nz,nc), Incnn = (Tfloat)(*this)(nx,y,nz,nc), + Icnnn = (Tfloat)(*this)(x,ny,nz,nc), Innnn = (Tfloat)(*this)(nx,ny,nz,nc); + return Icccc + + dx*(Inccc-Icccc + + dy*(Icccc+Inncc-Icncc-Inccc + + dz*(Iccnc+Innnc+Icncc+Inccc-Icnnc-Incnc-Icccc-Inncc + + dc*(Iccnn+Innnn+Icncn+Inccn+Icnnc+Incnc+Icccc+Inncc- + Icnnn-Incnn-Icccn-Inncn-Iccnc-Innnc-Icncc-Inccc)) + + dc*(Icccn+Inncn+Icncc+Inccc-Icncn-Inccn-Icccc-Inncc)) + + dz*(Icccc+Incnc-Iccnc-Inccc + + dc*(Icccn+Incnn+Iccnc+Inccc-Iccnn-Inccn-Icccc-Incnc)) + + dc*(Icccc+Inccn-Inccc-Icccn)) + + dy*(Icncc-Icccc + + dz*(Icccc+Icnnc-Iccnc-Icncc + + dc*(Icccn+Icnnn+Iccnc+Icncc-Iccnn-Icncn-Icccc-Icnnc)) + + dc*(Icccc+Icncn-Icncc-Icccn)) + + dz*(Iccnc-Icccc + + dc*(Icccc+Iccnn-Iccnc-Icccn)) + + dc*(Icccn-Icccc); + } + + //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate. + /** + Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), + or a specified default value in case of out-of-bounds access along the X-axis. + The cubic interpolation uses Hermite splines. + \param fx d X-coordinate of the pixel value (float-valued). + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds. + \note + - Similar to linear_atX(float,int,int,int,const T) const, except that the returned pixel value is + approximated by a \e cubic interpolation along the X-axis. + - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued. + \warning + - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. + **/ + Tfloat cubic_atX(const float fx, const int y, const int z, const int c, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2; + const float + dx = fx - x; + const Tfloat + Ip = (Tfloat)atX(px,y,z,c,out_value), Ic = (Tfloat)atX(x,y,z,c,out_value), + In = (Tfloat)atX(nx,y,z,c,out_value), Ia = (Tfloat)atX(ax,y,z,c,out_value); + return Ic + 0.5f*(dx*(-Ip+In) + dx*dx*(2*Ip-5*Ic+4*In-Ia) + dx*dx*dx*(-Ip+3*Ic-3*In+Ia)); + } + + //! Return damped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate. + /** + Similar to cubic_atX(float,int,int,int,const T) const, except that you can specify the authorized minimum + and maximum of the returned value. + **/ + Tfloat cubic_atX(const float fx, const int y, const int z, const int c, const T out_value, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = cubic_atX(fx,y,z,c,out_value); + return valmax_value?max_value:val; + } + + //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate. + /** + Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), + or the value of the nearest pixel location in the image instance in case of out-of-bounds access + along the X-axis. The cubic interpolation uses Hermite splines. + \param fx X-coordinate of the pixel value (float-valued). + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Similar to cubic_atX(float,int,int,int,const T) const, except that the returned pixel value is + approximated by a cubic interpolation along the X-axis. + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _cubic_atX(float,int,int,int). + \warning + - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. + **/ + Tfloat cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "cubic_atX(): Empty instance.", + cimg_instance); + return _cubic_atX(fx,y,z,c); + } + + Tfloat _cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx); + const int + x = (int)nfx; + const float + dx = nfx - x; + const int + px = x-1<0?0:x-1, nx = dx>0?x+1:x, ax = x+2>=width()?width()-1:x+2; + const Tfloat + Ip = (Tfloat)(*this)(px,y,z,c), Ic = (Tfloat)(*this)(x,y,z,c), + In = (Tfloat)(*this)(nx,y,z,c), Ia = (Tfloat)(*this)(ax,y,z,c); + return Ic + 0.5f*(dx*(-Ip+In) + dx*dx*(2*Ip-5*Ic+4*In-Ia) + dx*dx*dx*(-Ip+3*Ic-3*In+Ia)); + } + + //! Return damped pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate. + /** + Similar to cubic_atX(float,int,int,int) const, except that you can specify the authorized minimum and maximum + of the returned value. + **/ + Tfloat cubic_atX(const float fx, const int y, const int z, const int c, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = cubic_atX(fx,y,z,c); + return valmax_value?max_value:val; + } + + Tfloat _cubic_atX(const float fx, const int y, const int z, const int c, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = _cubic_atX(fx,y,z,c); + return valmax_value?max_value:val; + } + + //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X and Y-coordinates. + /** + Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking + are achieved both for X and Y-coordinates. + **/ + Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2, + y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2; + const float dx = fx - x, dy = fy - y; + const Tfloat + Ipp = (Tfloat)atXY(px,py,z,c,out_value), Icp = (Tfloat)atXY(x,py,z,c,out_value), + Inp = (Tfloat)atXY(nx,py,z,c,out_value), Iap = (Tfloat)atXY(ax,py,z,c,out_value), + Ip = Icp + 0.5f*(dx*(-Ipp+Inp) + dx*dx*(2*Ipp-5*Icp+4*Inp-Iap) + dx*dx*dx*(-Ipp+3*Icp-3*Inp+Iap)), + Ipc = (Tfloat)atXY(px,y,z,c,out_value), Icc = (Tfloat)atXY(x, y,z,c,out_value), + Inc = (Tfloat)atXY(nx,y,z,c,out_value), Iac = (Tfloat)atXY(ax,y,z,c,out_value), + Ic = Icc + 0.5f*(dx*(-Ipc+Inc) + dx*dx*(2*Ipc-5*Icc+4*Inc-Iac) + dx*dx*dx*(-Ipc+3*Icc-3*Inc+Iac)), + Ipn = (Tfloat)atXY(px,ny,z,c,out_value), Icn = (Tfloat)atXY(x,ny,z,c,out_value), + Inn = (Tfloat)atXY(nx,ny,z,c,out_value), Ian = (Tfloat)atXY(ax,ny,z,c,out_value), + In = Icn + 0.5f*(dx*(-Ipn+Inn) + dx*dx*(2*Ipn-5*Icn+4*Inn-Ian) + dx*dx*dx*(-Ipn+3*Icn-3*Inn+Ian)), + Ipa = (Tfloat)atXY(px,ay,z,c,out_value), Ica = (Tfloat)atXY(x,ay,z,c,out_value), + Ina = (Tfloat)atXY(nx,ay,z,c,out_value), Iaa = (Tfloat)atXY(ax,ay,z,c,out_value), + Ia = Ica + 0.5f*(dx*(-Ipa+Ina) + dx*dx*(2*Ipa-5*Ica+4*Ina-Iaa) + dx*dx*dx*(-Ipa+3*Ica-3*Ina+Iaa)); + return Ic + 0.5f*(dy*(-Ip+In) + dy*dy*(2*Ip-5*Ic+4*In-Ia) + dy*dy*dy*(-Ip+3*Ic-3*In+Ia)); + } + + //! Return damped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y-coordinates. + /** + Similar to cubic_atXY(float,float,int,int,const T) const, except that you can specify the authorized + minimum and maximum of the returned value. + **/ + Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, const T out_value, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = cubic_atXY(fx,fy,z,c,out_value); + return valmax_value?max_value:val; + } + + //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X and Y-coordinates. + /** + Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking + are achieved for both X and Y-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _cubic_atXY(float,float,int,int). + **/ + Tfloat cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "cubic_atXY(): Empty instance.", + cimg_instance); + return _cubic_atXY(fx,fy,z,c); + } + + Tfloat _cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx), + nfy = fy<0?0:(fy>_height-1?_height-1:fy); + const int x = (int)nfx, y = (int)nfy; + const float dx = nfx - x, dy = nfy - y; + const int + px = x-1<0?0:x-1, nx = dx>0?x+1:x, ax = x+2>=width()?width()-1:x+2, + py = y-1<0?0:y-1, ny = dy>0?y+1:y, ay = y+2>=height()?height()-1:y+2; + const Tfloat + Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c), + Iap = (Tfloat)(*this)(ax,py,z,c), + Ip = Icp + 0.5f*(dx*(-Ipp+Inp) + dx*dx*(2*Ipp-5*Icp+4*Inp-Iap) + dx*dx*dx*(-Ipp+3*Icp-3*Inp+Iap)), + Ipc = (Tfloat)(*this)(px,y,z,c), Icc = (Tfloat)(*this)(x, y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c), + Iac = (Tfloat)(*this)(ax,y,z,c), + Ic = Icc + 0.5f*(dx*(-Ipc+Inc) + dx*dx*(2*Ipc-5*Icc+4*Inc-Iac) + dx*dx*dx*(-Ipc+3*Icc-3*Inc+Iac)), + Ipn = (Tfloat)(*this)(px,ny,z,c), Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c), + Ian = (Tfloat)(*this)(ax,ny,z,c), + In = Icn + 0.5f*(dx*(-Ipn+Inn) + dx*dx*(2*Ipn-5*Icn+4*Inn-Ian) + dx*dx*dx*(-Ipn+3*Icn-3*Inn+Ian)), + Ipa = (Tfloat)(*this)(px,ay,z,c), Ica = (Tfloat)(*this)(x,ay,z,c), Ina = (Tfloat)(*this)(nx,ay,z,c), + Iaa = (Tfloat)(*this)(ax,ay,z,c), + Ia = Ica + 0.5f*(dx*(-Ipa+Ina) + dx*dx*(2*Ipa-5*Ica+4*Ina-Iaa) + dx*dx*dx*(-Ipa+3*Ica-3*Ina+Iaa)); + return Ic + 0.5f*(dy*(-Ip+In) + dy*dy*(2*Ip-5*Ic+4*In-Ia) + dy*dy*dy*(-Ip+3*Ic-3*In+Ia)); + } + + //! Return damped pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y-coordinates. + /** + Similar to cubic_atXY(float,float,int,int) const, except that you can specify the authorized minimum and + maximum of the returned value. + **/ + Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = cubic_atXY(fx,fy,z,c); + return valmax_value?max_value:val; + } + + Tfloat _cubic_atXY(const float fx, const float fy, const int z, const int c, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = _cubic_atXY(fx,fy,z,c); + return valmax_value?max_value:val; + } + + //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates. + /** + Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking + are achieved both for X,Y and Z-coordinates. + **/ + Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, const T out_value) const { + const int + x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2, + y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2, + z = (int)fz - (fz>=0?0:1), pz = z - 1, nz = z + 1, az = z + 2; + const float dx = fx - x, dy = fy - y, dz = fz - z; + const Tfloat + Ippp = (Tfloat)atXYZ(px,py,pz,c,out_value), Icpp = (Tfloat)atXYZ(x,py,pz,c,out_value), + Inpp = (Tfloat)atXYZ(nx,py,pz,c,out_value), Iapp = (Tfloat)atXYZ(ax,py,pz,c,out_value), + Ipp = Icpp + 0.5f*(dx*(-Ippp+Inpp) + dx*dx*(2*Ippp-5*Icpp+4*Inpp-Iapp) + dx*dx*dx*(-Ippp+3*Icpp-3*Inpp+Iapp)), + Ipcp = (Tfloat)atXYZ(px,y,pz,c,out_value), Iccp = (Tfloat)atXYZ(x, y,pz,c,out_value), + Incp = (Tfloat)atXYZ(nx,y,pz,c,out_value), Iacp = (Tfloat)atXYZ(ax,y,pz,c,out_value), + Icp = Iccp + 0.5f*(dx*(-Ipcp+Incp) + dx*dx*(2*Ipcp-5*Iccp+4*Incp-Iacp) + dx*dx*dx*(-Ipcp+3*Iccp-3*Incp+Iacp)), + Ipnp = (Tfloat)atXYZ(px,ny,pz,c,out_value), Icnp = (Tfloat)atXYZ(x,ny,pz,c,out_value), + Innp = (Tfloat)atXYZ(nx,ny,pz,c,out_value), Ianp = (Tfloat)atXYZ(ax,ny,pz,c,out_value), + Inp = Icnp + 0.5f*(dx*(-Ipnp+Innp) + dx*dx*(2*Ipnp-5*Icnp+4*Innp-Ianp) + dx*dx*dx*(-Ipnp+3*Icnp-3*Innp+Ianp)), + Ipap = (Tfloat)atXYZ(px,ay,pz,c,out_value), Icap = (Tfloat)atXYZ(x,ay,pz,c,out_value), + Inap = (Tfloat)atXYZ(nx,ay,pz,c,out_value), Iaap = (Tfloat)atXYZ(ax,ay,pz,c,out_value), + Iap = Icap + 0.5f*(dx*(-Ipap+Inap) + dx*dx*(2*Ipap-5*Icap+4*Inap-Iaap) + dx*dx*dx*(-Ipap+3*Icap-3*Inap+Iaap)), + Ip = Icp + 0.5f*(dy*(-Ipp+Inp) + dy*dy*(2*Ipp-5*Icp+4*Inp-Iap) + dy*dy*dy*(-Ipp+3*Icp-3*Inp+Iap)), + Ippc = (Tfloat)atXYZ(px,py,z,c,out_value), Icpc = (Tfloat)atXYZ(x,py,z,c,out_value), + Inpc = (Tfloat)atXYZ(nx,py,z,c,out_value), Iapc = (Tfloat)atXYZ(ax,py,z,c,out_value), + Ipc = Icpc + 0.5f*(dx*(-Ippc+Inpc) + dx*dx*(2*Ippc-5*Icpc+4*Inpc-Iapc) + dx*dx*dx*(-Ippc+3*Icpc-3*Inpc+Iapc)), + Ipcc = (Tfloat)atXYZ(px,y,z,c,out_value), Iccc = (Tfloat)atXYZ(x, y,z,c,out_value), + Incc = (Tfloat)atXYZ(nx,y,z,c,out_value), Iacc = (Tfloat)atXYZ(ax,y,z,c,out_value), + Icc = Iccc + 0.5f*(dx*(-Ipcc+Incc) + dx*dx*(2*Ipcc-5*Iccc+4*Incc-Iacc) + dx*dx*dx*(-Ipcc+3*Iccc-3*Incc+Iacc)), + Ipnc = (Tfloat)atXYZ(px,ny,z,c,out_value), Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value), + Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value), Ianc = (Tfloat)atXYZ(ax,ny,z,c,out_value), + Inc = Icnc + 0.5f*(dx*(-Ipnc+Innc) + dx*dx*(2*Ipnc-5*Icnc+4*Innc-Ianc) + dx*dx*dx*(-Ipnc+3*Icnc-3*Innc+Ianc)), + Ipac = (Tfloat)atXYZ(px,ay,z,c,out_value), Icac = (Tfloat)atXYZ(x,ay,z,c,out_value), + Inac = (Tfloat)atXYZ(nx,ay,z,c,out_value), Iaac = (Tfloat)atXYZ(ax,ay,z,c,out_value), + Iac = Icac + 0.5f*(dx*(-Ipac+Inac) + dx*dx*(2*Ipac-5*Icac+4*Inac-Iaac) + dx*dx*dx*(-Ipac+3*Icac-3*Inac+Iaac)), + Ic = Icc + 0.5f*(dy*(-Ipc+Inc) + dy*dy*(2*Ipc-5*Icc+4*Inc-Iac) + dy*dy*dy*(-Ipc+3*Icc-3*Inc+Iac)), + Ippn = (Tfloat)atXYZ(px,py,nz,c,out_value), Icpn = (Tfloat)atXYZ(x,py,nz,c,out_value), + Inpn = (Tfloat)atXYZ(nx,py,nz,c,out_value), Iapn = (Tfloat)atXYZ(ax,py,nz,c,out_value), + Ipn = Icpn + 0.5f*(dx*(-Ippn+Inpn) + dx*dx*(2*Ippn-5*Icpn+4*Inpn-Iapn) + dx*dx*dx*(-Ippn+3*Icpn-3*Inpn+Iapn)), + Ipcn = (Tfloat)atXYZ(px,y,nz,c,out_value), Iccn = (Tfloat)atXYZ(x, y,nz,c,out_value), + Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value), Iacn = (Tfloat)atXYZ(ax,y,nz,c,out_value), + Icn = Iccn + 0.5f*(dx*(-Ipcn+Incn) + dx*dx*(2*Ipcn-5*Iccn+4*Incn-Iacn) + dx*dx*dx*(-Ipcn+3*Iccn-3*Incn+Iacn)), + Ipnn = (Tfloat)atXYZ(px,ny,nz,c,out_value), Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value), + Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value), Iann = (Tfloat)atXYZ(ax,ny,nz,c,out_value), + Inn = Icnn + 0.5f*(dx*(-Ipnn+Innn) + dx*dx*(2*Ipnn-5*Icnn+4*Innn-Iann) + dx*dx*dx*(-Ipnn+3*Icnn-3*Innn+Iann)), + Ipan = (Tfloat)atXYZ(px,ay,nz,c,out_value), Ican = (Tfloat)atXYZ(x,ay,nz,c,out_value), + Inan = (Tfloat)atXYZ(nx,ay,nz,c,out_value), Iaan = (Tfloat)atXYZ(ax,ay,nz,c,out_value), + Ian = Ican + 0.5f*(dx*(-Ipan+Inan) + dx*dx*(2*Ipan-5*Ican+4*Inan-Iaan) + dx*dx*dx*(-Ipan+3*Ican-3*Inan+Iaan)), + In = Icn + 0.5f*(dy*(-Ipn+Inn) + dy*dy*(2*Ipn-5*Icn+4*Inn-Ian) + dy*dy*dy*(-Ipn+3*Icn-3*Inn+Ian)), + Ippa = (Tfloat)atXYZ(px,py,az,c,out_value), Icpa = (Tfloat)atXYZ(x,py,az,c,out_value), + Inpa = (Tfloat)atXYZ(nx,py,az,c,out_value), Iapa = (Tfloat)atXYZ(ax,py,az,c,out_value), + Ipa = Icpa + 0.5f*(dx*(-Ippa+Inpa) + dx*dx*(2*Ippa-5*Icpa+4*Inpa-Iapa) + dx*dx*dx*(-Ippa+3*Icpa-3*Inpa+Iapa)), + Ipca = (Tfloat)atXYZ(px,y,az,c,out_value), Icca = (Tfloat)atXYZ(x, y,az,c,out_value), + Inca = (Tfloat)atXYZ(nx,y,az,c,out_value), Iaca = (Tfloat)atXYZ(ax,y,az,c,out_value), + Ica = Icca + 0.5f*(dx*(-Ipca+Inca) + dx*dx*(2*Ipca-5*Icca+4*Inca-Iaca) + dx*dx*dx*(-Ipca+3*Icca-3*Inca+Iaca)), + Ipna = (Tfloat)atXYZ(px,ny,az,c,out_value), Icna = (Tfloat)atXYZ(x,ny,az,c,out_value), + Inna = (Tfloat)atXYZ(nx,ny,az,c,out_value), Iana = (Tfloat)atXYZ(ax,ny,az,c,out_value), + Ina = Icna + 0.5f*(dx*(-Ipna+Inna) + dx*dx*(2*Ipna-5*Icna+4*Inna-Iana) + dx*dx*dx*(-Ipna+3*Icna-3*Inna+Iana)), + Ipaa = (Tfloat)atXYZ(px,ay,az,c,out_value), Icaa = (Tfloat)atXYZ(x,ay,az,c,out_value), + Inaa = (Tfloat)atXYZ(nx,ay,az,c,out_value), Iaaa = (Tfloat)atXYZ(ax,ay,az,c,out_value), + Iaa = Icaa + 0.5f*(dx*(-Ipaa+Inaa) + dx*dx*(2*Ipaa-5*Icaa+4*Inaa-Iaaa) + dx*dx*dx*(-Ipaa+3*Icaa-3*Inaa+Iaaa)), + Ia = Ica + 0.5f*(dy*(-Ipa+Ina) + dy*dy*(2*Ipa-5*Ica+4*Ina-Iaa) + dy*dy*dy*(-Ipa+3*Ica-3*Ina+Iaa)); + return Ic + 0.5f*(dz*(-Ip+In) + dz*dz*(2*Ip-5*Ic+4*In-Ia) + dz*dz*dz*(-Ip+3*Ic-3*In+Ia)); + } + + //! Return damped pixel value, using cubic interpolation and Dirichlet boundary conditions for the XYZ-coordinates. + /** + Similar to cubic_atXYZ(float,float,float,int,const T) const, except that you can specify the authorized + minimum and maximum of the returned value. + **/ + Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, const T out_value, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = cubic_atXYZ(fx,fy,fz,c,out_value); + return valmax_value?max_value:val; + } + + //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y and Z-coordinates. + /** + Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking + are achieved both for X,Y and Z-coordinates. + \note + - If you know your image instance is \e not empty, you may rather use the slightly faster method + \c _cubic_atXYZ(float,float,float,int). + **/ + Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "cubic_atXYZ(): Empty instance.", + cimg_instance); + return _cubic_atXYZ(fx,fy,fz,c); + } + + Tfloat _cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const { + const float + nfx = fx<0?0:(fx>_width-1?_width-1:fx), + nfy = fy<0?0:(fy>_height-1?_height-1:fy), + nfz = fz<0?0:(fz>_depth-1?_depth-1:fz); + const int x = (int)nfx, y = (int)nfy, z = (int)nfz; + const float dx = nfx - x, dy = nfy - y, dz = nfz - z; + const int + px = x-1<0?0:x-1, nx = dx>0?x+1:x, ax = x+2>=width()?width()-1:x+2, + py = y-1<0?0:y-1, ny = dy>0?y+1:y, ay = y+2>=height()?height()-1:y+2, + pz = z-1<0?0:z-1, nz = dz>0?z+1:z, az = z+2>=depth()?depth()-1:z+2; + const Tfloat + Ippp = (Tfloat)(*this)(px,py,pz,c), Icpp = (Tfloat)(*this)(x,py,pz,c), + Inpp = (Tfloat)(*this)(nx,py,pz,c), Iapp = (Tfloat)(*this)(ax,py,pz,c), + Ipp = Icpp + 0.5f*(dx*(-Ippp+Inpp) + dx*dx*(2*Ippp-5*Icpp+4*Inpp-Iapp) + dx*dx*dx*(-Ippp+3*Icpp-3*Inpp+Iapp)), + Ipcp = (Tfloat)(*this)(px,y,pz,c), Iccp = (Tfloat)(*this)(x, y,pz,c), + Incp = (Tfloat)(*this)(nx,y,pz,c), Iacp = (Tfloat)(*this)(ax,y,pz,c), + Icp = Iccp + 0.5f*(dx*(-Ipcp+Incp) + dx*dx*(2*Ipcp-5*Iccp+4*Incp-Iacp) + dx*dx*dx*(-Ipcp+3*Iccp-3*Incp+Iacp)), + Ipnp = (Tfloat)(*this)(px,ny,pz,c), Icnp = (Tfloat)(*this)(x,ny,pz,c), + Innp = (Tfloat)(*this)(nx,ny,pz,c), Ianp = (Tfloat)(*this)(ax,ny,pz,c), + Inp = Icnp + 0.5f*(dx*(-Ipnp+Innp) + dx*dx*(2*Ipnp-5*Icnp+4*Innp-Ianp) + dx*dx*dx*(-Ipnp+3*Icnp-3*Innp+Ianp)), + Ipap = (Tfloat)(*this)(px,ay,pz,c), Icap = (Tfloat)(*this)(x,ay,pz,c), + Inap = (Tfloat)(*this)(nx,ay,pz,c), Iaap = (Tfloat)(*this)(ax,ay,pz,c), + Iap = Icap + 0.5f*(dx*(-Ipap+Inap) + dx*dx*(2*Ipap-5*Icap+4*Inap-Iaap) + dx*dx*dx*(-Ipap+3*Icap-3*Inap+Iaap)), + Ip = Icp + 0.5f*(dy*(-Ipp+Inp) + dy*dy*(2*Ipp-5*Icp+4*Inp-Iap) + dy*dy*dy*(-Ipp+3*Icp-3*Inp+Iap)), + Ippc = (Tfloat)(*this)(px,py,z,c), Icpc = (Tfloat)(*this)(x,py,z,c), + Inpc = (Tfloat)(*this)(nx,py,z,c), Iapc = (Tfloat)(*this)(ax,py,z,c), + Ipc = Icpc + 0.5f*(dx*(-Ippc+Inpc) + dx*dx*(2*Ippc-5*Icpc+4*Inpc-Iapc) + dx*dx*dx*(-Ippc+3*Icpc-3*Inpc+Iapc)), + Ipcc = (Tfloat)(*this)(px,y,z,c), Iccc = (Tfloat)(*this)(x, y,z,c), + Incc = (Tfloat)(*this)(nx,y,z,c), Iacc = (Tfloat)(*this)(ax,y,z,c), + Icc = Iccc + 0.5f*(dx*(-Ipcc+Incc) + dx*dx*(2*Ipcc-5*Iccc+4*Incc-Iacc) + dx*dx*dx*(-Ipcc+3*Iccc-3*Incc+Iacc)), + Ipnc = (Tfloat)(*this)(px,ny,z,c), Icnc = (Tfloat)(*this)(x,ny,z,c), + Innc = (Tfloat)(*this)(nx,ny,z,c), Ianc = (Tfloat)(*this)(ax,ny,z,c), + Inc = Icnc + 0.5f*(dx*(-Ipnc+Innc) + dx*dx*(2*Ipnc-5*Icnc+4*Innc-Ianc) + dx*dx*dx*(-Ipnc+3*Icnc-3*Innc+Ianc)), + Ipac = (Tfloat)(*this)(px,ay,z,c), Icac = (Tfloat)(*this)(x,ay,z,c), + Inac = (Tfloat)(*this)(nx,ay,z,c), Iaac = (Tfloat)(*this)(ax,ay,z,c), + Iac = Icac + 0.5f*(dx*(-Ipac+Inac) + dx*dx*(2*Ipac-5*Icac+4*Inac-Iaac) + dx*dx*dx*(-Ipac+3*Icac-3*Inac+Iaac)), + Ic = Icc + 0.5f*(dy*(-Ipc+Inc) + dy*dy*(2*Ipc-5*Icc+4*Inc-Iac) + dy*dy*dy*(-Ipc+3*Icc-3*Inc+Iac)), + Ippn = (Tfloat)(*this)(px,py,nz,c), Icpn = (Tfloat)(*this)(x,py,nz,c), + Inpn = (Tfloat)(*this)(nx,py,nz,c), Iapn = (Tfloat)(*this)(ax,py,nz,c), + Ipn = Icpn + 0.5f*(dx*(-Ippn+Inpn) + dx*dx*(2*Ippn-5*Icpn+4*Inpn-Iapn) + dx*dx*dx*(-Ippn+3*Icpn-3*Inpn+Iapn)), + Ipcn = (Tfloat)(*this)(px,y,nz,c), Iccn = (Tfloat)(*this)(x, y,nz,c), + Incn = (Tfloat)(*this)(nx,y,nz,c), Iacn = (Tfloat)(*this)(ax,y,nz,c), + Icn = Iccn + 0.5f*(dx*(-Ipcn+Incn) + dx*dx*(2*Ipcn-5*Iccn+4*Incn-Iacn) + dx*dx*dx*(-Ipcn+3*Iccn-3*Incn+Iacn)), + Ipnn = (Tfloat)(*this)(px,ny,nz,c), Icnn = (Tfloat)(*this)(x,ny,nz,c), + Innn = (Tfloat)(*this)(nx,ny,nz,c), Iann = (Tfloat)(*this)(ax,ny,nz,c), + Inn = Icnn + 0.5f*(dx*(-Ipnn+Innn) + dx*dx*(2*Ipnn-5*Icnn+4*Innn-Iann) + dx*dx*dx*(-Ipnn+3*Icnn-3*Innn+Iann)), + Ipan = (Tfloat)(*this)(px,ay,nz,c), Ican = (Tfloat)(*this)(x,ay,nz,c), + Inan = (Tfloat)(*this)(nx,ay,nz,c), Iaan = (Tfloat)(*this)(ax,ay,nz,c), + Ian = Ican + 0.5f*(dx*(-Ipan+Inan) + dx*dx*(2*Ipan-5*Ican+4*Inan-Iaan) + dx*dx*dx*(-Ipan+3*Ican-3*Inan+Iaan)), + In = Icn + 0.5f*(dy*(-Ipn+Inn) + dy*dy*(2*Ipn-5*Icn+4*Inn-Ian) + dy*dy*dy*(-Ipn+3*Icn-3*Inn+Ian)), + Ippa = (Tfloat)(*this)(px,py,az,c), Icpa = (Tfloat)(*this)(x,py,az,c), + Inpa = (Tfloat)(*this)(nx,py,az,c), Iapa = (Tfloat)(*this)(ax,py,az,c), + Ipa = Icpa + 0.5f*(dx*(-Ippa+Inpa) + dx*dx*(2*Ippa-5*Icpa+4*Inpa-Iapa) + dx*dx*dx*(-Ippa+3*Icpa-3*Inpa+Iapa)), + Ipca = (Tfloat)(*this)(px,y,az,c), Icca = (Tfloat)(*this)(x, y,az,c), + Inca = (Tfloat)(*this)(nx,y,az,c), Iaca = (Tfloat)(*this)(ax,y,az,c), + Ica = Icca + 0.5f*(dx*(-Ipca+Inca) + dx*dx*(2*Ipca-5*Icca+4*Inca-Iaca) + dx*dx*dx*(-Ipca+3*Icca-3*Inca+Iaca)), + Ipna = (Tfloat)(*this)(px,ny,az,c), Icna = (Tfloat)(*this)(x,ny,az,c), + Inna = (Tfloat)(*this)(nx,ny,az,c), Iana = (Tfloat)(*this)(ax,ny,az,c), + Ina = Icna + 0.5f*(dx*(-Ipna+Inna) + dx*dx*(2*Ipna-5*Icna+4*Inna-Iana) + dx*dx*dx*(-Ipna+3*Icna-3*Inna+Iana)), + Ipaa = (Tfloat)(*this)(px,ay,az,c), Icaa = (Tfloat)(*this)(x,ay,az,c), + Inaa = (Tfloat)(*this)(nx,ay,az,c), Iaaa = (Tfloat)(*this)(ax,ay,az,c), + Iaa = Icaa + 0.5f*(dx*(-Ipaa+Inaa) + dx*dx*(2*Ipaa-5*Icaa+4*Inaa-Iaaa) + dx*dx*dx*(-Ipaa+3*Icaa-3*Inaa+Iaaa)), + Ia = Ica + 0.5f*(dy*(-Ipa+Ina) + dy*dy*(2*Ipa-5*Ica+4*Ina-Iaa) + dy*dy*dy*(-Ipa+3*Ica-3*Ina+Iaa)); + return Ic + 0.5f*(dz*(-Ip+In) + dz*dz*(2*Ip-5*Ic+4*In-Ia) + dz*dz*dz*(-Ip+3*Ic-3*In+Ia)); + } + + //! Return damped pixel value, using cubic interpolation and Neumann boundary conditions for the XYZ-coordinates. + /** + Similar to cubic_atXYZ(float,float,float,int) const, except that you can specify the authorized minimum and + maximum of the returned value. + **/ + Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = cubic_atXYZ(fx,fy,fz,c); + return valmax_value?max_value:val; + } + + Tfloat _cubic_atXYZ(const float fx, const float fy, const float fz, const int c, + const Tfloat min_value, const Tfloat max_value) const { + const Tfloat val = _cubic_atXYZ(fx,fy,fz,c); + return valmax_value?max_value:val; + } + + //! Set pixel value, using linear interpolation for the X and Y-coordinates. + /** + Set pixel value at specified coordinates (\c fx,\c fy,\c z,\c c) in the image instance, in a way that + the value is spread amongst several neighbors if the pixel coordinates are indeed float-valued. + \param value Pixel value to set. + \param fx X-coordinate of the pixel value (float-valued). + \param fy Y-coordinate of the pixel value (float-valued). + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param is_added Tells if the pixel value is added to (\c true), or simply replace (\c false) the current image + pixel(s). + \return A reference to the current image instance. + \note + - If specified coordinates are outside image bounds, no operations are performed. + **/ + CImg& set_linear_atXY(const T& value, const float fx, const float fy=0, const int z=0, const int c=0, + const bool is_added=false) { + const int + x = (int)fx - (fx>=0?0:1), nx = x + 1, + y = (int)fy - (fy>=0?0:1), ny = y + 1; + const float + dx = fx - x, + dy = fy - y; + if (z>=0 && z=0 && c=0 && y=0 && x=0 && nx=0 && ny=0 && x=0 && nx& set_linear_atXYZ(const T& value, const float fx, const float fy=0, const float fz=0, const int c=0, + const bool is_added=false) { + const int + x = (int)fx - (fx>=0?0:1), nx = x + 1, + y = (int)fy - (fy>=0?0:1), ny = y + 1, + z = (int)fz - (fz>=0?0:1), nz = z + 1; + const float + dx = fx - x, + dy = fy - y, + dz = fz - z; + if (c>=0 && c=0 && z=0 && y=0 && x=0 && nx=0 && ny=0 && x=0 && nx=0 && nz=0 && y=0 && x=0 && nx=0 && ny=0 && x=0 && nx image whose buffer data() is a \c char* string describing the list of all pixel values + of the image instance (written in base 10), separated by specified \c separator character. + \param separator A \c char character which specifies the separator between values in the returned C-string. + \param max_size Maximum size of the returned image. + \note + - The returned image is never empty. + - For an empty image instance, the returned string is "". + - If \c max_size is equal to \c 0, there are no limits on the size of the returned string. + - Otherwise, if the maximum number of string characters is exceeded, the value string is cut off + and terminated by character \c '\0'. In that case, the returned image size is max_size + 1. + **/ + CImg value_string(const char separator=',', const unsigned int max_size=0) const { + if (is_empty()) return CImg::string(""); + CImgList items; + char s_item[256] = { 0 }; + const T *ptrs = _data; + unsigned int string_size = 0; + for (unsigned long off = 0, siz = (unsigned int)size(); off::format(),cimg::type::format(*(ptrs++))); + CImg item(s_item,printed_size); + item[printed_size-1] = separator; + item.move_to(items); + if (max_size) string_size+=printed_size; + } + CImg res; + (items>'x').move_to(res); + if (max_size && res._width>max_size) res.crop(0,max_size); + res.back() = 0; + return res; + } + + //@} + //------------------------------------- + // + //! \name Instance Checking + //@{ + //------------------------------------- + + //! Test shared state of the pixel buffer. + /** + Return \c true if image instance has a shared memory buffer, and \c false otherwise. + \note + - A shared image do not own his pixel buffer data() and will not deallocate it on destruction. + - Most of the time, a \c CImg image instance will \e not be shared. + - A shared image can only be obtained by a limited set of constructors and methods (see list below). + **/ + bool is_shared() const { + return _is_shared; + } + + //! Test if image instance is empty. + /** + Return \c true, if image instance is empty, i.e. does \e not contain any pixel values, has dimensions + \c 0 x \c 0 x \c 0 x \c 0 and a pixel buffer pointer set to \c 0 (null pointer), and \c false otherwise. + **/ + bool is_empty() const { + return !(_data && _width && _height && _depth && _spectrum); + } + + //! Test if image instance contains a 'inf' value. + /** + Return \c true, if image instance contains a 'inf' value, and \c false otherwise. + **/ + bool is_inf() const { + if (cimg::type::is_float()) cimg_for(*this,p,T) if (cimg::type::is_inf((float)*p)) return true; + return false; + } + + //! Test if image instance contains a 'nan' value. + /** + Return \c true, if image instance contains a 'nan' value, and \c false otherwise. + **/ + bool is_nan() const { + if (cimg::type::is_float()) cimg_for(*this,p,T) if (cimg::type::is_nan((float)*p)) return true; + return false; + } + + //! Test if image width is equal to specified value. + bool is_sameX(const unsigned int size_x) const { + return _width==size_x; + } + + //! Test if image width is equal to specified value. + template + bool is_sameX(const CImg& img) const { + return is_sameX(img._width); + } + + //! Test if image width is equal to specified value. + bool is_sameX(const CImgDisplay& disp) const { + return is_sameX(disp._width); + } + + //! Test if image height is equal to specified value. + bool is_sameY(const unsigned int size_y) const { + return _height==size_y; + } + + //! Test if image height is equal to specified value. + template + bool is_sameY(const CImg& img) const { + return is_sameY(img._height); + } + + //! Test if image height is equal to specified value. + bool is_sameY(const CImgDisplay& disp) const { + return is_sameY(disp._height); + } + + //! Test if image depth is equal to specified value. + bool is_sameZ(const unsigned int size_z) const { + return _depth==size_z; + } + + //! Test if image depth is equal to specified value. + template + bool is_sameZ(const CImg& img) const { + return is_sameZ(img._depth); + } + + //! Test if image spectrum is equal to specified value. + bool is_sameC(const unsigned int size_c) const { + return _spectrum==size_c; + } + + //! Test if image spectrum is equal to specified value. + template + bool is_sameC(const CImg& img) const { + return is_sameC(img._spectrum); + } + + //! Test if image width and height are equal to specified values. + /** + Test if is_sameX(unsigned int) const and is_sameY(unsigned int) const are both verified. + **/ + bool is_sameXY(const unsigned int size_x, const unsigned int size_y) const { + return _width==size_x && _height==size_y; + } + + //! Test if image width and height are the same as that of another image. + /** + Test if is_sameX(const CImg&) const and is_sameY(const CImg&) const are both verified. + **/ + template + bool is_sameXY(const CImg& img) const { + return is_sameXY(img._width,img._height); + } + + //! Test if image width and height are the same as that of an existing display window. + /** + Test if is_sameX(const CImgDisplay&) const and is_sameY(const CImgDisplay&) const are both verified. + **/ + bool is_sameXY(const CImgDisplay& disp) const { + return is_sameXY(disp._width,disp._height); + } + + //! Test if image width and depth are equal to specified values. + /** + Test if is_sameX(unsigned int) const and is_sameZ(unsigned int) const are both verified. + **/ + bool is_sameXZ(const unsigned int size_x, const unsigned int size_z) const { + return _width==size_x && _depth==size_z; + } + + //! Test if image width and depth are the same as that of another image. + /** + Test if is_sameX(const CImg&) const and is_sameZ(const CImg&) const are both verified. + **/ + template + bool is_sameXZ(const CImg& img) const { + return is_sameXZ(img._width,img._depth); + } + + //! Test if image width and spectrum are equal to specified values. + /** + Test if is_sameX(unsigned int) const and is_sameC(unsigned int) const are both verified. + **/ + bool is_sameXC(const unsigned int size_x, const unsigned int size_c) const { + return _width==size_x && _spectrum==size_c; + } + + //! Test if image width and spectrum are the same as that of another image. + /** + Test if is_sameX(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameXC(const CImg& img) const { + return is_sameXC(img._width,img._spectrum); + } + + //! Test if image height and depth are equal to specified values. + /** + Test if is_sameY(unsigned int) const and is_sameZ(unsigned int) const are both verified. + **/ + bool is_sameYZ(const unsigned int size_y, const unsigned int size_z) const { + return _height==size_y && _depth==size_z; + } + + //! Test if image height and depth are the same as that of another image. + /** + Test if is_sameY(const CImg&) const and is_sameZ(const CImg&) const are both verified. + **/ + template + bool is_sameYZ(const CImg& img) const { + return is_sameYZ(img._height,img._depth); + } + + //! Test if image height and spectrum are equal to specified values. + /** + Test if is_sameY(unsigned int) const and is_sameC(unsigned int) const are both verified. + **/ + bool is_sameYC(const unsigned int size_y, const unsigned int size_c) const { + return _height==size_y && _spectrum==size_c; + } + + //! Test if image height and spectrum are the same as that of another image. + /** + Test if is_sameY(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameYC(const CImg& img) const { + return is_sameYC(img._height,img._spectrum); + } + + //! Test if image depth and spectrum are equal to specified values. + /** + Test if is_sameZ(unsigned int) const and is_sameC(unsigned int) const are both verified. + **/ + bool is_sameZC(const unsigned int size_z, const unsigned int size_c) const { + return _depth==size_z && _spectrum==size_c; + } + + //! Test if image depth and spectrum are the same as that of another image. + /** + Test if is_sameZ(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameZC(const CImg& img) const { + return is_sameZC(img._depth,img._spectrum); + } + + //! Test if image width, height and depth are equal to specified values. + /** + Test if is_sameXY(unsigned int,unsigned int) const and is_sameZ(unsigned int) const are both verified. + **/ + bool is_sameXYZ(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z) const { + return is_sameXY(size_x,size_y) && _depth==size_z; + } + + //! Test if image width, height and depth are the same as that of another image. + /** + Test if is_sameXY(const CImg&) const and is_sameZ(const CImg&) const are both verified. + **/ + template + bool is_sameXYZ(const CImg& img) const { + return is_sameXYZ(img._width,img._height,img._depth); + } + + //! Test if image width, height and spectrum are equal to specified values. + /** + Test if is_sameXY(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified. + **/ + bool is_sameXYC(const unsigned int size_x, const unsigned int size_y, const unsigned int size_c) const { + return is_sameXY(size_x,size_y) && _spectrum==size_c; + } + + //! Test if image width, height and spectrum are the same as that of another image. + /** + Test if is_sameXY(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameXYC(const CImg& img) const { + return is_sameXYC(img._width,img._height,img._spectrum); + } + + //! Test if image width, depth and spectrum are equal to specified values. + /** + Test if is_sameXZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified. + **/ + bool is_sameXZC(const unsigned int size_x, const unsigned int size_z, const unsigned int size_c) const { + return is_sameXZ(size_x,size_z) && _spectrum==size_c; + } + + //! Test if image width, depth and spectrum are the same as that of another image. + /** + Test if is_sameXZ(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameXZC(const CImg& img) const { + return is_sameXZC(img._width,img._depth,img._spectrum); + } + + //! Test if image height, depth and spectrum are equal to specified values. + /** + Test if is_sameYZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified. + **/ + bool is_sameYZC(const unsigned int size_y, const unsigned int size_z, const unsigned int size_c) const { + return is_sameYZ(size_y,size_z) && _spectrum==size_c; + } + + //! Test if image height, depth and spectrum are the same as that of another image. + /** + Test if is_sameYZ(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameYZC(const CImg& img) const { + return is_sameYZC(img._height,img._depth,img._spectrum); + } + + //! Test if image width, height, depth and spectrum are equal to specified values. + /** + Test if is_sameXYZ(unsigned int,unsigned int,unsigned int) const and is_sameC(unsigned int) const are both + verified. + **/ + bool is_sameXYZC(const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c) const { + return is_sameXYZ(size_x,size_y,size_z) && _spectrum==size_c; + } + + //! Test if image width, height, depth and spectrum are the same as that of another image. + /** + Test if is_sameXYZ(const CImg&) const and is_sameC(const CImg&) const are both verified. + **/ + template + bool is_sameXYZC(const CImg& img) const { + return is_sameXYZC(img._width,img._height,img._depth,img._spectrum); + } + + //! Test if specified coordinates are inside image bounds. + /** + Return \c true if pixel located at (\c x,\c y,\c z,\c c) is inside bounds of the image instance, + and \c false otherwise. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note + - Return \c true only if all these conditions are verified: + - The image instance is \e not empty. + - 0<=x<=\ref width()-1. + - 0<=y<=\ref height()-1. + - 0<=z<=\ref depth()-1. + - 0<=c<=\ref spectrum()-1. + **/ + bool containsXYZC(const int x, const int y=0, const int z=0, const int c=0) const { + return !is_empty() && x>=0 && x=0 && y=0 && z=0 && c img(100,100,1,3); // Construct a 100x100 RGB color image. + const unsigned long offset = 1249; // Offset to the pixel (49,12,0,0). + unsigned int x,y,z,c; + if (img.contains(img[offset],x,y,z,c)) { // Convert offset to (x,y,z,c) coordinates. + std::printf("Offset %u refers to pixel located at (%u,%u,%u,%u).\n", + offset,x,y,z,c); + } + \endcode + **/ + template + bool contains(const T& pixel, t& x, t& y, t& z, t& c) const { + const unsigned long wh = (unsigned long)_width*_height, whd = wh*_depth, siz = whd*_spectrum; + const T *const ppixel = &pixel; + if (is_empty() || ppixel<_data || ppixel>=_data+siz) return false; + unsigned long off = (unsigned long)(ppixel - _data); + const unsigned long nc = off/whd; + off%=whd; + const unsigned long nz = off/wh; + off%=wh; + const unsigned long ny = off/_width, nx = off%_width; + x = (t)nx; y = (t)ny; z = (t)nz; c = (t)nc; + return true; + } + + //! Test if pixel value is inside image bounds and get its X,Y and Z-coordinates. + /** + Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X,Y and Z-coordinates are set. + **/ + template + bool contains(const T& pixel, t& x, t& y, t& z) const { + const unsigned long wh = (unsigned long)_width*_height, whd = wh*_depth, siz = whd*_spectrum; + const T *const ppixel = &pixel; + if (is_empty() || ppixel<_data || ppixel>=_data+siz) return false; + unsigned long off = ((unsigned long)(ppixel - _data))%whd; + const unsigned long nz = off/wh; + off%=wh; + const unsigned long ny = off/_width, nx = off%_width; + x = (t)nx; y = (t)ny; z = (t)nz; + return true; + } + + //! Test if pixel value is inside image bounds and get its X and Y-coordinates. + /** + Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X and Y-coordinates are set. + **/ + template + bool contains(const T& pixel, t& x, t& y) const { + const unsigned long wh = (unsigned long)_width*_height, siz = wh*_depth*_spectrum; + const T *const ppixel = &pixel; + if (is_empty() || ppixel<_data || ppixel>=_data+siz) return false; + unsigned long off = ((unsigned int)(ppixel - _data))%wh; + const unsigned long ny = off/_width, nx = off%_width; + x = (t)nx; y = (t)ny; + return true; + } + + //! Test if pixel value is inside image bounds and get its X-coordinate. + /** + Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X-coordinate is set. + **/ + template + bool contains(const T& pixel, t& x) const { + const T *const ppixel = &pixel; + if (is_empty() || ppixel<_data || ppixel>=_data+size()) return false; + x = (t)(((unsigned long)(ppixel - _data))%_width); + return true; + } + + //! Test if pixel value is inside image bounds. + /** + Similar to contains(const T&,t&,t&,t&,t&) const, except that no pixel coordinates are set. + **/ + bool contains(const T& pixel) const { + const T *const ppixel = &pixel; + return !is_empty() && ppixel>=_data && ppixel<_data + size(); + } + + //! Test if pixel buffers of instance and input images overlap. + /** + Return \c true, if pixel buffers attached to image instance and input image \c img overlap, + and \c false otherwise. + \param img Input image to compare with. + \note + - Buffer overlapping may happen when manipulating \e shared images. + - If two image buffers overlap, operating on one of the image will probably modify the other one. + - Most of the time, \c CImg instances are \e non-shared and do not overlap between each others. + \par Example + \code + const CImg + img1("reference.jpg"), // Load RGB-color image. + img2 = img1.get_shared_channel(1); // Get shared version of the green channel. + if (img1.is_overlapped(img2)) { // Test succeeds, 'img1' and 'img2' overlaps. + std::printf("Buffers overlap!\n"); + } + \endcode + **/ + template + bool is_overlapped(const CImg& img) const { + const unsigned long csiz = size(), isiz = img.size(); + return !((void*)(_data + csiz)<=(void*)img._data || (void*)_data>=(void*)(img._data + isiz)); + } + + //! Test if the set {\c *this,\c primitives,\c colors,\c opacities} defines a valid 3d object. + /** + Return \c true is the 3d object represented by the set {\c *this,\c primitives,\c colors,\c opacities} defines a + valid 3d object, and \c false otherwise. The vertex coordinates are defined by the instance image. + \param primitives List of primitives of the 3d object. + \param colors List of colors of the 3d object. + \param opacities List (or image) of opacities of the 3d object. + \param full_check Tells if full checking of the 3d object must be performed. + \param[out] error_message C-string to contain the error message, if the test does not succeed. + \note + - Set \c full_checking to \c false to speed-up the 3d object checking. In this case, only the size of + each 3d object component is checked. + - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message. + **/ + template + bool is_object3d(const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const bool full_check=true, + char *const error_message=0) const { + if (error_message) *error_message = 0; + + // Check consistency for the particular case of an empty 3d object. + if (is_empty()) { + if (primitives || colors || opacities) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) defines no vertices but %u primitives, " + "%u colors and %lu opacities", + _width,primitives._width,primitives._width, + colors._width,(unsigned long)opacities.size()); + return false; + } + return true; + } + + // Check consistency of vertices. + if (_height!=3 || _depth>1 || _spectrum>1) { // Check vertices dimensions. + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) has invalid vertex dimensions (%u,%u,%u,%u)", + _width,primitives._width,_width,_height,_depth,_spectrum); + return false; + } + if (colors._width>primitives._width+1) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) defines %u colors", + _width,primitives._width,colors._width); + return false; + } + if (opacities.size()>primitives._width) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) defines %lu opacities", + _width,primitives._width,(unsigned long)opacities.size()); + return false; + } + if (!full_check) return true; + + // Check consistency of primitives. + cimglist_for(primitives,l) { + const CImg& primitive = primitives[l]; + const unsigned long psiz = primitive.size(); + switch (psiz) { + case 1 : { // Point. + const unsigned int i0 = (unsigned int)primitive(0); + if (i0>=_width) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) refers to invalid vertex indice %u in " + "point primitive [%u]", + _width,primitives._width,i0,l); + return false; + } + } break; + case 5 : { // Sphere. + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1); + if (i0>=_width || i1>=_width) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) refers to invalid vertex indices (%u,%u) in " + "sphere primitive [%u]", + _width,primitives._width,i0,i1,l); + return false; + } + } break; + case 2 : // Segment. + case 6 : { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1); + if (i0>=_width || i1>=_width) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) refers to invalid vertex indices (%u,%u) in " + "segment primitive [%u]", + _width,primitives._width,i0,i1,l); + return false; + } + } break; + case 3 : // Triangle. + case 9 : { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1), + i2 = (unsigned int)primitive(2); + if (i0>=_width || i1>=_width || i2>=_width) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) refers to invalid vertex indices (%u,%u,%u) in " + "triangle primitive [%u]", + _width,primitives._width,i0,i1,i2,l); + return false; + } + } break; + case 4 : // Quadrangle. + case 12 : { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1), + i2 = (unsigned int)primitive(2), + i3 = (unsigned int)primitive(3); + if (i0>=_width || i1>=_width || i2>=_width || i3>=_width) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in " + "quadrangle primitive [%u]", + _width,primitives._width,i0,i1,i2,i3,l); + return false; + } + } break; + default : + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) defines an invalid primitive [%u] of size %u", + _width,primitives._width,l,(unsigned int)psiz); + return false; + } + } + + // Check consistency of colors. + cimglist_for(colors,c) { + const CImg& color = colors[c]; + if (!color) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) defines no color for primitive [%u]", + _width,primitives._width,c); + return false; + } + } + + // Check consistency of light texture. + if (colors._width>primitives._width) { + const CImg &light = colors.back(); + if (!light || light._depth>1) { + if (error_message) std::sprintf(error_message, + "3d object (%u,%u) defines an invalid light texture (%u,%u,%u,%u)", + _width,primitives._width,light._width, + light._height,light._depth,light._spectrum); + return false; + } + } + + return true; + } + + //! Test if image instance represents a valid serialization of a 3d object. + /** + Return \c true if the image instance represents a valid serialization of a 3d object, and \c false otherwise. + \param full_check Tells if full checking of the instance must be performed. + \param[out] error_message C-string to contain the error message, if the test does not succeed. + \note + - Set \c full_check to \c false to speed-up the 3d object checking. In this case, only the size of + each 3d object component is checked. + - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message. + **/ + bool is_CImg3d(const bool full_check=true, char *const error_message=0) const { + if (error_message) *error_message = 0; + + // Check instance dimension and header. + if (_width!=1 || _height<8 || _depth!=1 || _spectrum!=1) { + if (error_message) std::sprintf(error_message, + "CImg3d has invalid dimensions (%u,%u,%u,%u)", + _width,_height,_depth,_spectrum); + return false; + } + const T *ptrs = _data, *const ptre = end(); + if (!_is_CImg3d(*(ptrs++),'C') || !_is_CImg3d(*(ptrs++),'I') || !_is_CImg3d(*(ptrs++),'m') || + !_is_CImg3d(*(ptrs++),'g') || !_is_CImg3d(*(ptrs++),'3') || !_is_CImg3d(*(ptrs++),'d')) { + if (error_message) std::sprintf(error_message, + "CImg3d header not found"); + return false; + } + const unsigned int + nb_points = cimg::float2uint((float)*(ptrs++)), + nb_primitives = cimg::float2uint((float)*(ptrs++)); + + // Check consistency of number of vertices / primitives. + if (!full_check) { + const unsigned long minimal_size = 8UL + 3*nb_points + 6*nb_primitives; + if (_data + minimal_size>ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) has only %lu values, while at least %lu values were expected", + nb_points,nb_primitives,size(),minimal_size); + return false; + } + } + + // Check consistency of vertex data. + if (!nb_points) { + if (nb_primitives) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) defines no vertices but %u primitives", + nb_points,nb_primitives,nb_primitives); + return false; + } + if (ptrs!=ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) is an empty object but contains %u value%s " + "more than expected", + nb_points,nb_primitives,(unsigned int)(ptre-ptrs),(ptre-ptrs)>1?"s":""); + return false; + } + return true; + } + if (ptrs+3*nb_points>ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) defines only %u vertices data", + nb_points,nb_primitives,(unsigned int)(ptre-ptrs)/3); + return false; + } + ptrs+=3*nb_points; + + // Check consistency of primitive data. + if (ptrs==ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) defines %u vertices but no primitive", + nb_points,nb_primitives,nb_points); + return false; + } + + if (!full_check) return true; + + for (unsigned int p = 0; p=nb_points) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid vertex indice %u in point primitive [%u]", + nb_points,nb_primitives,i0,p); + return false; + } + } break; + case 5 : { // Sphere. + const unsigned int + i0 = cimg::float2uint((float)*(ptrs++)), + i1 = cimg::float2uint((float)*(ptrs++)); + ptrs+=3; + if (i0>=nb_points || i1>=nb_points) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in " + "sphere primitive [%u]", + nb_points,nb_primitives,i0,i1,p); + return false; + } + } break; + case 2 : case 6 : { // Segment. + const unsigned int + i0 = cimg::float2uint((float)*(ptrs++)), + i1 = cimg::float2uint((float)*(ptrs++)); + if (nb_inds==6) ptrs+=4; + if (i0>=nb_points || i1>=nb_points) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in " + "segment primitive [%u]", + nb_points,nb_primitives,i0,i1,p); + return false; + } + } break; + case 3 : case 9 : { // Triangle. + const unsigned int + i0 = cimg::float2uint((float)*(ptrs++)), + i1 = cimg::float2uint((float)*(ptrs++)), + i2 = cimg::float2uint((float)*(ptrs++)); + if (nb_inds==9) ptrs+=6; + if (i0>=nb_points || i1>=nb_points || i2>=nb_points) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u) in " + "triangle primitive [%u]", + nb_points,nb_primitives,i0,i1,i2,p); + return false; + } + } break; + case 4 : case 12 : { // Quadrangle. + const unsigned int + i0 = cimg::float2uint((float)*(ptrs++)), + i1 = cimg::float2uint((float)*(ptrs++)), + i2 = cimg::float2uint((float)*(ptrs++)), + i3 = cimg::float2uint((float)*(ptrs++)); + if (nb_inds==12) ptrs+=8; + if (i0>=nb_points || i1>=nb_points || i2>=nb_points || i3>=nb_points) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in " + "quadrangle primitive [%u]", + nb_points,nb_primitives,i0,i1,i2,i3,p); + return false; + } + } break; + default : + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) defines an invalid primitive [%u] of size %u", + nb_points,nb_primitives,p,nb_inds); + return false; + } + if (ptrs>ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) has incomplete primitive data for primitive [%u], " + "%u values missing", + nb_points,nb_primitives,p,(unsigned int)(ptrs-ptre)); + return false; + } + } + + // Check consistency of color data. + if (ptrs==ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) defines no color/texture data", + nb_points,nb_primitives); + return false; + } + for (unsigned int c = 0; c=c) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid shared sprite/texture indice %u " + "for primitive [%u]", + nb_points,nb_primitives,w,c); + return false; + } + } else ptrs+=w*h*s; + } + if (ptrs>ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) has incomplete color/texture data for primitive [%u], " + "%u values missing", + nb_points,nb_primitives,c,(unsigned int)(ptrs-ptre)); + return false; + } + } + + // Check consistency of opacity data. + if (ptrs==ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) defines no opacity data", + nb_points,nb_primitives); + return false; + } + for (unsigned int o = 0; o=o) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) refers to invalid shared opacity indice %u " + "for primitive [%u]", + nb_points,nb_primitives,w,o); + return false; + } + } else ptrs+=w*h*s; + } + if (ptrs>ptre) { + if (error_message) std::sprintf(error_message, + "CImg3d (%u,%u) has incomplete opacity data for primitive [%u]", + nb_points,nb_primitives,o); + return false; + } + } + + // Check end of data. + if (ptrs1?"s":""); + return false; + } + return true; + } + + static bool _is_CImg3d(const T val, const char c) { + return val>=(T)c && val<(T)(c+1); + } + + //@} + //------------------------------------- + // + //! \name Mathematical Functions + //@{ + //------------------------------------- + + // Define the math formula parser/compiler and evaluator. + struct _cimg_math_parser { + CImgList code; + CImg opcode; + const CImg* p_code; + CImgList labelM; + CImg level, labelMpos, label1pos; + CImg mem; + CImg expr; + const CImg& reference; + CImg reference_stats; + unsigned int mempos, result; + const char *const calling_function; + typedef double (*mp_func)(_cimg_math_parser&); + +#define _cimg_mp_return(x) { *se = saved_char; return x; } +#define _cimg_mp_opcode0(op) _cimg_mp_return(opcode0(op)); +#define _cimg_mp_opcode1(op,i1) _cimg_mp_return(opcode1(op,i1)); +#define _cimg_mp_opcode2(op,i1,i2) { const unsigned int _i1 = i1, _i2 = i2; _cimg_mp_return(opcode2(op,_i1,_i2)); } +#define _cimg_mp_opcode3(op,i1,i2,i3) \ + { const unsigned int _i1 = i1, _i2 = i2, _i3 = i3; _cimg_mp_return(opcode3(op,_i1,_i2,_i3)); } +#define _cimg_mp_opcode6(op,i1,i2,i3,i4,i5,i6) \ + { const unsigned int _i1 = i1, _i2 = i2, _i3 = i3, _i4 = i4, _i5 = i5, _i6 = i6; \ + _cimg_mp_return(opcode6(op,_i1,_i2,_i3,_i4,_i5,_i6)); } + +#if defined(_WIN64) + // On Win64 and gcc 4.7, sizeof(long)!=sizeof(pointer), so a workaround is needed.. +#define _cimg_mp_enfunc(op) (long)((char*)(op)-(char*)mp_u) +#define _cimg_mp_defunc(mp) (*(mp_func)((char*)mp_u+(mp).opcode[0]))(mp) +#else +#define _cimg_mp_enfunc(op) (long)(op) +#define _cimg_mp_defunc(mp) (*(mp_func)((mp).opcode[0]))(mp) +#endif + + // Constructors. + _cimg_math_parser():reference(CImg::empty()),calling_function(0) {} + + _cimg_math_parser(const CImg& img, const char *const expression, const char *const funcname=0): + reference(img),calling_function(funcname?funcname:"cimg_math_parser") { + unsigned int l = 0; + if (expression) { + l = (unsigned int)std::strlen(expression); + expr.assign(expression,l+1); + if (*expr._data) { + char *d = expr._data; + for (const char *s = expr._data; *s || (bool)(*d=0); ++s) if (*s!=' ') *(d++) = *s; + l = (unsigned int)(d - expr._data); + } + } + if (!l) throw CImgArgumentException("[_cimg_math_parser] " + "CImg<%s>::%s(): Empty specified expression.", + pixel_type(),calling_function); + + int lv = 0; // Count parentheses/brackets level of expression. + level.assign(l); + unsigned int *pd = level._data; + for (const char *ps = expr._data; *ps && lv>=0; ++ps) + *(pd++) = (unsigned int)(*ps=='('||*ps=='['?lv++:*ps==')'||*ps==']'?--lv:lv); + if (lv!=0) { + throw CImgArgumentException("[_cimg_math_parser] " + "CImg<%s>::%s(): Unbalanced parentheses/brackets in specified expression '%s'.", + pixel_type(),calling_function, + expr._data); + } + // Init constant values. + mem.assign(512); + mem[0] = 0; + mem[1] = 1; + mem[2] = 2; + mem[3] = (double)reference._width; + mem[4] = (double)reference._height; + mem[5] = (double)reference._depth; + mem[6] = (double)reference._spectrum; + mem[7] = cimg::PI; + mem[8] = std::exp(1.0); // Then [9] = x, [10] = y, [11] = z, [12] = c + mempos = 13; + labelMpos.assign(8); + label1pos.assign(128,1,1,1,~0U); + label1pos['w'] = 3; + label1pos['h'] = 4; + label1pos['d'] = 5; + label1pos['s'] = 6; + label1pos[0] = 7; // pi + label1pos['e'] = 8; + label1pos['x'] = 9; + label1pos['y'] = 10; + label1pos['z'] = 11; + label1pos['c'] = 12; + result = compile(expr._data,expr._data+l); // Compile formula into a serie of opcodes. + } + + // Insert code instructions. + unsigned int opcode0(const mp_func op) { + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(op),pos).move_to(code); + return pos; + } + + unsigned int opcode1(const mp_func op, const unsigned int arg1) { + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(op),pos,arg1).move_to(code); + return pos; + } + + unsigned int opcode2(const mp_func op, const unsigned int arg1, const unsigned int arg2) { + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(op),pos,arg1,arg2).move_to(code); + return pos; + } + + unsigned int opcode3(const mp_func op, + const unsigned int arg1, const unsigned int arg2, const unsigned int arg3) { + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(op),pos,arg1,arg2,arg3).move_to(code); + return pos; + } + + unsigned int opcode6(const mp_func op, + const unsigned int arg1, const unsigned int arg2, const unsigned int arg3, + const unsigned int arg4, const unsigned int arg5, const unsigned int arg6) { + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(op),pos,arg1,arg2,arg3,arg4,arg5,arg6).move_to(code); + return pos; + } + + // Compilation procedure. + unsigned int compile(char *const ss, char *const se) { + if (!ss || se<=ss || !*ss) { + throw CImgArgumentException("[_cimg_math_parser] " + "CImg<%s>::%s(): Missing item in specified expression '%s'.", + pixel_type(),calling_function, + expr._data); + } + char + *const se1 = se-1, *const se2 = se-2, *const se3 = se-3, *const se4 = se-4, + *const ss1 = ss+1, *const ss2 = ss+2, *const ss3 = ss+3, *const ss4 = ss+4, + *const ss5 = ss+5, *const ss6 = ss+6, *const ss7 = ss+7; + const char saved_char = *se; *se = 0; + const unsigned int clevel = level[ss-expr._data], clevel1 = clevel+1; + if (*se1==';') return compile(ss,se1); + + // Look for a single value, variable or variable assignment. + char end = 0, sep = 0; double val = 0; + const int nb = std::sscanf(ss,"%lf%c%c",&val,&sep,&end); + if (nb==1) { + if (val==0 || val==1 || val==2) _cimg_mp_return((int)val); + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + mem[pos] = val; + _cimg_mp_return(pos); + } + if (nb==2 && sep=='%') { + if (val==0 || val==100 || val==200) _cimg_mp_return((int)(val/100)); + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + mem[pos] = val/100; + _cimg_mp_return(pos); + } + if (ss1==se) switch (*ss) { + case 'w' : case 'h' : case 'd' : case 's' : + case 'x' : case 'y' : case 'z' : case 'c' : case 'e' : _cimg_mp_return(label1pos[*ss]); + case 'u' : if (label1pos['u']!=~0U) _cimg_mp_return(label1pos['u']); _cimg_mp_opcode2(mp_u,0,1); + case 'g' : if (label1pos['g']!=~0U) _cimg_mp_return(label1pos['g']); _cimg_mp_opcode0(mp_g); + case 'i' : if (label1pos['i']!=~0U) _cimg_mp_return(label1pos['i']); _cimg_mp_opcode0(mp_i); + case '?' : _cimg_mp_opcode2(mp_u,0,1); + } + if (ss1==se1) { + if (*ss=='p' && *ss1=='i') _cimg_mp_return(label1pos[0]); // pi + if (*ss=='i') { + if (*ss1=='m') { if (label1pos[1]!=~0U) _cimg_mp_return(label1pos[1]); _cimg_mp_opcode0(mp_im); } // im + if (*ss1=='M') { if (label1pos[2]!=~0U) _cimg_mp_return(label1pos[2]); _cimg_mp_opcode0(mp_iM); } // iM + if (*ss1=='a') { if (label1pos[3]!=~0U) _cimg_mp_return(label1pos[3]); _cimg_mp_opcode0(mp_ia); } // ia + if (*ss1=='v') { if (label1pos[4]!=~0U) _cimg_mp_return(label1pos[4]); _cimg_mp_opcode0(mp_iv); } // iv + } + if (*ss1=='m') { + if (*ss=='x') { if (label1pos[5]!=~0U) _cimg_mp_return(label1pos[5]); _cimg_mp_opcode0(mp_xm); } // xm + if (*ss=='y') { if (label1pos[6]!=~0U) _cimg_mp_return(label1pos[6]); _cimg_mp_opcode0(mp_ym); } // ym + if (*ss=='z') { if (label1pos[7]!=~0U) _cimg_mp_return(label1pos[7]); _cimg_mp_opcode0(mp_zm); } // zm + if (*ss=='c') { if (label1pos[8]!=~0U) _cimg_mp_return(label1pos[8]); _cimg_mp_opcode0(mp_cm); } // cm + } + if (*ss1=='M') { + if (*ss=='x') { if (label1pos[9]!=~0U) _cimg_mp_return(label1pos[9]); _cimg_mp_opcode0(mp_xM); } // xM + if (*ss=='y') { if (label1pos[10]!=~0U) _cimg_mp_return(label1pos[10]); _cimg_mp_opcode0(mp_yM); } // yM + if (*ss=='z') { if (label1pos[11]!=~0U) _cimg_mp_return(label1pos[11]); _cimg_mp_opcode0(mp_zM); } // zM + if (*ss=='c') { if (label1pos[12]!=~0U) _cimg_mp_return(label1pos[12]); _cimg_mp_opcode0(mp_cM); } // cM + } + } + + // Look for variable declarations. + for (char *s = se2; s>ss; --s) + if (*s==';' && level[s-expr._data]==clevel) { compile(ss,s); _cimg_mp_return(compile(s+1,se)); } + for (char *s = ss1, *ps = ss, *ns = ss2; s variable_name(ss,(unsigned int)(s-ss+1)); + variable_name.back() = 0; + bool is_valid_name = true; + if (*ss>='0' && *ss<='9') is_valid_name = false; + else for (const char *ns = ss+1; ns'z') && (*ns<'A' || *ns>'Z') && (*ns<'0' || *ns>'9') && *ns!='_') { + is_valid_name = false; break; + } + if (!is_valid_name) { + *se = saved_char; + throw CImgArgumentException("[_cimg_math_parser] " + "CImg<%s>::%s(): Invalid variable name '%s' in specified expression " + "'%s%s%s'.", + pixel_type(),calling_function, + variable_name._data, + (ss-8)>expr._data?"...":"", + (ss-8)>expr._data?ss-8:expr._data, + se<&expr.back()?"...":""); + } + const unsigned int pos = compile(s+1,se); + + // Check for particular case of a reserved variable. + if (variable_name[0] && variable_name[1] && !variable_name[2]) { + const char c1 = variable_name[0], c2 = variable_name[1]; + if (c1=='p' && c2=='i') variable_name.fill((char)0,(char)0); // pi + else if (c1=='i') { + if (c2=='m') variable_name.fill(1,0); // im + else if (c2=='M') variable_name.fill(2,0); // iM + else if (c2=='a') variable_name.fill(3,0); // ia + else if (c2=='v') variable_name.fill(4,0); // iv + } else if (c2=='m') { + if (c1=='x') variable_name.fill(5,0); // xm + else if (c1=='y') variable_name.fill(6,0); // ym + else if (c1=='z') variable_name.fill(7,0); // zm + else if (c1=='c') variable_name.fill(8,0); // cm + } else if (c2=='M') { + if (c1=='x') variable_name.fill(9,0); // xM + else if (c1=='y') variable_name.fill(10,0); // yM + else if (c1=='z') variable_name.fill(11,0); // zM + else if (c1=='c') variable_name.fill(12,0); // cM + } + } + if (variable_name[1]) { // Multi-char variable. + int label_pos = -1; + cimglist_for(labelM,i) // Check for existing variable with same name. + if (!std::strcmp(variable_name,labelM[i])) { label_pos = i; break; } + if (label_pos<0) { // If new variable. + if (labelM._width>=labelMpos._width) labelMpos.resize(-200,1,1,1,0); + label_pos = labelM._width; + variable_name.move_to(labelM); + } + labelMpos[label_pos] = pos; + } else label1pos[*variable_name] = pos; // Single-char variable. + _cimg_mp_return(pos); + } + + // Look for unary/binary operators. The operator precedences is defined as in C++. + for (char *s = se3, *ns = se2; s>ss; --s, --ns) if (*s=='|' && *ns=='|' && level[s-expr._data]==clevel) { + const unsigned int mem_A = compile(ss,s), bp1 = code._width, mem_B = compile(s+2,se); + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(mp_logical_or),pos,mem_A,mem_B,code._width-bp1).move_to(code,bp1); + _cimg_mp_return(pos); + } + for (char *s = se3, *ns = se2; s>ss; --s, --ns) if (*s=='&' && *ns=='&' && level[s-expr._data]==clevel) { + const unsigned int mem_A = compile(ss,s), bp1 = code._width, mem_B = compile(s+2,se); + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(mp_logical_and),pos,mem_A,mem_B,code._width-bp1).move_to(code,bp1); + _cimg_mp_return(pos); + } + for (char *s = se2; s>ss; --s) + if (*s=='|' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_bitwise_or,compile(ss,s),compile(s+1,se)); + for (char *s = se2; s>ss; --s) + if (*s=='&' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_bitwise_and,compile(ss,s),compile(s+1,se)); + for (char *s = se3, *ns = se2; s>ss; --s, --ns) + if (*s=='!' && *ns=='=' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_noteq,compile(ss,s),compile(s+2,se)); + for (char *s = se3, *ns = se2; s>ss; --s, --ns) + if (*s=='=' && *ns=='=' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_eqeq,compile(ss,s),compile(s+2,se)); + for (char *s = se3, *ns = se2; s>ss; --s, --ns) + if (*s=='<' && *ns=='=' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_infeq,compile(ss,s),compile(s+2,se)); + for (char *s = se3, *ns = se2; s>ss; --s, --ns) + if (*s=='>' && *ns=='=' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_supeq,compile(ss,s),compile(s+2,se)); + for (char *s = se2, *ns = se1, *ps = se3; s>ss; --s, --ns, --ps) + if (*s=='<' && *ns!='<' && *ps!='<' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_inf,compile(ss,s),compile(s+1,se)); + for (char *s = se2, *ns = se1, *ps = se3; s>ss; --s, --ns, --ps) + if (*s=='>' && *ns!='>' && *ps!='>' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_sup,compile(ss,s),compile(s+1,se)); + for (char *s = se3, *ns = se2; s>ss; --s, --ns) + if (*s=='<' && *ns=='<' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_lsl,compile(ss,s),compile(s+2,se)); + for (char *s = se3, *ns = se2; s>ss; --s, --ns) + if (*s=='>' && *ns=='>' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_lsr,compile(ss,s),compile(s+2,se)); + for (char *s = se2, *ps = se3; s>ss; --s, --ps) + if (*s=='+' && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' && + *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && + (*ps!='e' || !(ps>ss && (*(ps-1)=='.' || (*(ps-1)>='0' && *(ps-1)<='9')))) && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_add,compile(ss,s),compile(s+1,se)); + for (char *s = se2, *ps = se3; s>ss; --s, --ps) + if (*s=='-' && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' && + *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && + (*ps!='e' || !(ps>ss && (*(ps-1)=='.' || (*(ps-1)>='0' && *(ps-1)<='9')))) && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_sub,compile(ss,s),compile(s+1,se)); + for (char *s = se2; s>ss; --s) if (*s=='*' && level[s-expr._data]==clevel) { + const unsigned int mem_A = compile(ss,s), bp1 = code._width, mem_B = compile(s+1,se); + if (mempos>=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(mp_mul),pos,mem_A,mem_B,code._width-bp1).move_to(code,bp1); + _cimg_mp_return(pos); + } + for (char *s = se2; s>ss; --s) + if (*s=='/' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_div,compile(ss,s),compile(s+1,se)); + for (char *s = se2, *ns = se1; s>ss; --s, --ns) + if (*s=='%' && *ns!='^' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_modulo,compile(ss,s),compile(s+1,se)); + if (ssss; --s) + if (*s=='^' && level[s-expr._data]==clevel) + _cimg_mp_opcode2(mp_pow,compile(ss,s),compile(s+1,se)); + + // Look for a function call or a parenthesis. + if (*se1==']') { + const bool is_relative = *ss=='j'; + if ((*ss=='i' || is_relative) && *ss1=='[') { + if (*ss2==']') _cimg_mp_opcode0(mp_i); + _cimg_mp_opcode1(is_relative?mp_joff:mp_ioff,compile(ss2,se1)); + } + } + if (*se1==')') { + if (*ss=='(') _cimg_mp_return(compile(ss1,se1)); + if (!std::strncmp(ss,"sin(",4)) _cimg_mp_opcode1(mp_sin,compile(ss4,se1)); + if (!std::strncmp(ss,"cos(",4)) _cimg_mp_opcode1(mp_cos,compile(ss4,se1)); + if (!std::strncmp(ss,"tan(",4)) _cimg_mp_opcode1(mp_tan,compile(ss4,se1)); + if (!std::strncmp(ss,"asin(",5)) _cimg_mp_opcode1(mp_asin,compile(ss5,se1)); + if (!std::strncmp(ss,"acos(",5)) _cimg_mp_opcode1(mp_acos,compile(ss5,se1)); + if (!std::strncmp(ss,"atan(",5)) _cimg_mp_opcode1(mp_atan,compile(ss5,se1)); + if (!std::strncmp(ss,"sinh(",5)) _cimg_mp_opcode1(mp_sinh,compile(ss5,se1)); + if (!std::strncmp(ss,"cosh(",5)) _cimg_mp_opcode1(mp_cosh,compile(ss5,se1)); + if (!std::strncmp(ss,"tanh(",5)) _cimg_mp_opcode1(mp_tanh,compile(ss5,se1)); + if (!std::strncmp(ss,"log10(",6)) _cimg_mp_opcode1(mp_log10,compile(ss6,se1)); + if (!std::strncmp(ss,"log2(",5)) _cimg_mp_opcode1(mp_log2,compile(ss5,se1)); + if (!std::strncmp(ss,"log(",4)) _cimg_mp_opcode1(mp_log,compile(ss4,se1)); + if (!std::strncmp(ss,"exp(",4)) _cimg_mp_opcode1(mp_exp,compile(ss4,se1)); + if (!std::strncmp(ss,"sqrt(",5)) _cimg_mp_opcode1(mp_sqrt,compile(ss5,se1)); + if (!std::strncmp(ss,"sign(",5)) _cimg_mp_opcode1(mp_sign,compile(ss5,se1)); + if (!std::strncmp(ss,"abs(",4)) _cimg_mp_opcode1(mp_abs,compile(ss4,se1)); + if (!std::strncmp(ss,"atan2(",6)) { + char *s1 = ss6; while (s1=mem._width) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(mp_if),pos,mem_cond,mem_A,mem_B,bp2-bp1,code._width-bp2). + move_to(code,bp1); + _cimg_mp_return(pos); + } + if (!std::strncmp(ss,"round(",6)) { + unsigned int value = 0, round = 1, direction = 0; + char *s1 = ss6; while (s1 opcode; + if (mempos>=mem.size()) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(*ss=='k'?mp_kth:ss[1]=='i'?mp_min:ss[1]=='a'?mp_max:mp_med),pos). + move_to(opcode); + for (char *s = ss4; s::vector(compile(s,ns)).move_to(opcode); + s = ns; + } + (opcode>'y').move_to(code); + _cimg_mp_return(pos); + } + if (!std::strncmp(ss,"arg(",4)) { + CImgList opcode; + if (mempos>=mem.size()) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + CImg::vector(_cimg_mp_enfunc(mp_arg),pos).move_to(opcode); + for (char *s = ss4; s::vector(compile(s,ns)).move_to(opcode); + s = ns; + } + (opcode>'y').move_to(code); + _cimg_mp_return(pos); + } + if (!std::strncmp(ss,"narg(",5)) { + if (*ss5==')') _cimg_mp_return(0); + unsigned int nb_args = 0; + for (char *s = ss5; s=mem.size()) mem.resize(-200,1,1,1,0); + const unsigned int pos = mempos++; + mem[pos] = nb_args; + _cimg_mp_return(pos); + } + if (!std::strncmp(ss,"isval(",6)) { + char sep = 0, end = 0; double val = 0; + if (std::sscanf(ss6,"%lf%c%c",&val,&sep,&end)==2 && sep==')') _cimg_mp_return(1); + _cimg_mp_return(0); + } + if (!std::strncmp(ss,"isnan(",6)) _cimg_mp_opcode1(mp_isnan,compile(ss6,se1)); + if (!std::strncmp(ss,"isinf(",6)) _cimg_mp_opcode1(mp_isinf,compile(ss6,se1)); + if (!std::strncmp(ss,"isint(",6)) _cimg_mp_opcode1(mp_isint,compile(ss6,se1)); + if (!std::strncmp(ss,"isbool(",7)) _cimg_mp_opcode1(mp_isbool,compile(ss7,se1)); + if (!std::strncmp(ss,"rol(",4) || !std::strncmp(ss,"ror(",4)) { + unsigned int value = 0, nb = 1; + char *s1 = ss4; while (s1 variable_name(ss,(unsigned int)(se-ss+1)); + variable_name.back() = 0; + if (variable_name[1]) { // Multi-char variable. + cimglist_for(labelM,i) if (!std::strcmp(variable_name,labelM[i])) _cimg_mp_return(labelMpos[i]); + } else if (label1pos[*variable_name]!=~0U) _cimg_mp_return(label1pos[*variable_name]); // Single-char variable. + *se = saved_char; + throw CImgArgumentException("[_cimg_math_parser] " + "CImg<%s>::%s(): Invalid item '%s' in specified expression '%s%s%s'.\n", + pixel_type(),calling_function, + variable_name._data, + (ss-8)>expr._data?"...":"", + (ss-8)>expr._data?ss-8:expr._data, + se<&expr.back()?"...":""); + } + + // Evaluation functions, known by the parser. + // Defining these functions 'static' ensures that sizeof(mp_func)==sizeof(ulong), so we can store pointers to them + // directly in the opcode vectors. + static double mp_u(_cimg_math_parser& mp) { + return mp.mem[mp.opcode(2)] + cimg::rand()*(mp.mem[mp.opcode(3)]-mp.mem[mp.opcode(2)]); + } + static double mp_g(_cimg_math_parser& mp) { + cimg::unused(mp); + return cimg::grand(); + } + static double mp_i(_cimg_math_parser& mp) { + return (double)mp.reference.atXYZC((int)mp.mem[9],(int)mp.mem[10],(int)mp.mem[11],(int)mp.mem[12],0); + } + static double mp_logical_and(_cimg_math_parser& mp) { + const bool is_A = (bool)mp.mem[mp.opcode(2)]; + const CImg *const pE = ++mp.p_code + mp.opcode(4); + if (!is_A) { mp.p_code = pE - 1; return 0; } + const unsigned int mem_B = (unsigned int)mp.opcode(3); + for ( ; mp.p_code &op = *mp.p_code; + mp.opcode._data = op._data; mp.opcode._height = op._height; + const unsigned int target = (unsigned int)mp.opcode[1]; + mp.mem[target] = _cimg_mp_defunc(mp); + } + --mp.p_code; + return (double)(bool)mp.mem[mem_B]; + } + static double mp_logical_or(_cimg_math_parser& mp) { + const bool is_A = (bool)mp.mem[mp.opcode(2)]; + const CImg *const pE = ++mp.p_code + mp.opcode(4); + if (is_A) { mp.p_code = pE - 1; return 1; } + const unsigned int mem_B = (unsigned int)mp.opcode(3); + for ( ; mp.p_code &op = *mp.p_code; + mp.opcode._data = op._data; mp.opcode._height = op._height; + const unsigned int target = (unsigned int)mp.opcode[1]; + mp.mem[target] = _cimg_mp_defunc(mp); + } + --mp.p_code; + return (double)(bool)mp.mem[mem_B]; + } + static double mp_infeq(_cimg_math_parser& mp) { + return (double)(mp.mem[mp.opcode(2)]<=mp.mem[mp.opcode(3)]); + } + static double mp_supeq(_cimg_math_parser& mp) { + return (double)(mp.mem[mp.opcode(2)]>=mp.mem[mp.opcode(3)]); + } + static double mp_noteq(_cimg_math_parser& mp) { + return (double)(mp.mem[mp.opcode(2)]!=mp.mem[mp.opcode(3)]); + } + static double mp_eqeq(_cimg_math_parser& mp) { + return (double)(mp.mem[mp.opcode(2)]==mp.mem[mp.opcode(3)]); + } + static double mp_inf(_cimg_math_parser& mp) { + return (double)(mp.mem[mp.opcode(2)]mp.mem[mp.opcode(3)]); + } + static double mp_add(_cimg_math_parser& mp) { + return mp.mem[mp.opcode(2)] + mp.mem[mp.opcode(3)]; + } + static double mp_sub(_cimg_math_parser& mp) { + return mp.mem[mp.opcode(2)] - mp.mem[mp.opcode(3)]; + } + static double mp_mul(_cimg_math_parser& mp) { + const double A = mp.mem[mp.opcode(2)]; + const CImg *const pE = ++mp.p_code + mp.opcode(4); + if (!A) { mp.p_code = pE - 1; return 0; } + const unsigned int mem_B = (unsigned int)mp.opcode(3); + for ( ; mp.p_code &op = *mp.p_code; + mp.opcode._data = op._data; mp.opcode._height = op._height; + const unsigned int target = (unsigned int)mp.opcode[1]; + mp.mem[target] = _cimg_mp_defunc(mp); + } + --mp.p_code; + return A*(double)mp.mem[mem_B]; + } + static double mp_div(_cimg_math_parser& mp) { + return mp.mem[mp.opcode(2)] / mp.mem[mp.opcode(3)]; + } + static double mp_minus(_cimg_math_parser& mp) { + return -mp.mem[mp.opcode(2)]; + } + static double mp_not(_cimg_math_parser& mp) { + return !mp.mem[mp.opcode(2)]; + } + static double mp_logical_not(_cimg_math_parser& mp) { + return !mp.mem[mp.opcode(2)]; + } + static double mp_bitwise_not(_cimg_math_parser& mp) { + return ~(unsigned long)mp.mem[mp.opcode(2)]; + } + static double mp_modulo(_cimg_math_parser& mp) { + return cimg::mod(mp.mem[mp.opcode(2)],mp.mem[mp.opcode(3)]); + } + static double mp_bitwise_and(_cimg_math_parser& mp) { + return ((unsigned long)mp.mem[mp.opcode(2)] & (unsigned long)mp.mem[mp.opcode(3)]); + } + static double mp_bitwise_or(_cimg_math_parser& mp) { + return ((unsigned long)mp.mem[mp.opcode(2)] | (unsigned long)mp.mem[mp.opcode(3)]); + } + static double mp_pow(_cimg_math_parser& mp) { + const double v = mp.mem[mp.opcode(2)], p = mp.mem[mp.opcode(3)]; + if (p==0) return 1; + if (p==0.5) return std::sqrt(v); + if (p==1) return v; + if (p==2) return v*v; + if (p==3) return v*v*v; + if (p==4) return v*v*v*v; + return std::pow(v,p); + } + static double mp_sin(_cimg_math_parser& mp) { + return std::sin(mp.mem[mp.opcode(2)]); + } + static double mp_cos(_cimg_math_parser& mp) { + return std::cos(mp.mem[mp.opcode(2)]); + } + static double mp_tan(_cimg_math_parser& mp) { + return std::tan(mp.mem[mp.opcode(2)]); + } + static double mp_asin(_cimg_math_parser& mp) { + return std::asin(mp.mem[mp.opcode(2)]); + } + static double mp_acos(_cimg_math_parser& mp) { + return std::acos(mp.mem[mp.opcode(2)]); + } + static double mp_atan(_cimg_math_parser& mp) { + return std::atan(mp.mem[mp.opcode(2)]); + } + static double mp_sinh(_cimg_math_parser& mp) { + return std::sinh(mp.mem[mp.opcode(2)]); + } + static double mp_cosh(_cimg_math_parser& mp) { + return std::cosh(mp.mem[mp.opcode(2)]); + } + static double mp_tanh(_cimg_math_parser& mp) { + return std::tanh(mp.mem[mp.opcode(2)]); + } + static double mp_log10(_cimg_math_parser& mp) { + return std::log10(mp.mem[mp.opcode(2)]); + } + static double mp_log2(_cimg_math_parser& mp) { + return cimg::log2(mp.mem[mp.opcode(2)]); + } + static double mp_log(_cimg_math_parser& mp) { + return std::log(mp.mem[mp.opcode(2)]); + } + static double mp_exp(_cimg_math_parser& mp) { + return std::exp(mp.mem[mp.opcode(2)]); + } + static double mp_sqrt(_cimg_math_parser& mp) { + return std::sqrt(mp.mem[mp.opcode(2)]); + } + static double mp_sign(_cimg_math_parser& mp) { + return cimg::sign(mp.mem[mp.opcode(2)]); + } + static double mp_abs(_cimg_math_parser& mp) { + return cimg::abs(mp.mem[mp.opcode(2)]); + } + static double mp_atan2(_cimg_math_parser& mp) { + return std::atan2(mp.mem[mp.opcode(2)],mp.mem[mp.opcode(3)]); + } + static double mp_if(_cimg_math_parser& mp) { + const bool is_cond = (bool)mp.mem[mp.opcode(2)]; + const unsigned int mem_A = (unsigned int)mp.opcode(3), mem_B = (unsigned int)mp.opcode(4); + const CImg + *const pB = ++mp.p_code + mp.opcode(5), + *const pE = pB + mp.opcode(6); + if (is_cond) { // Evaluate on-the-fly only the correct argument. + for ( ; mp.p_code &op = *mp.p_code; + mp.opcode._data = op._data; mp.opcode._height = op._height; + const unsigned int target = (unsigned int)mp.opcode[1]; + mp.mem[target] = _cimg_mp_defunc(mp); + } + mp.p_code = pE - 1; + return mp.mem[mem_A]; + } + for (mp.p_code = pB; mp.p_code &op = *mp.p_code; + mp.opcode._data = op._data; mp.opcode._height = op._height; + const unsigned int target = (unsigned int)mp.opcode[1]; + mp.mem[target] = _cimg_mp_defunc(mp); + } + --mp.p_code; + return mp.mem[mem_B]; + } + static double mp_round(_cimg_math_parser& mp) { + return cimg::round(mp.mem[mp.opcode(2)],mp.mem[mp.opcode(3)],(int)mp.mem[mp.opcode(4)]); + } + static double mp_ixyzc(_cimg_math_parser& mp) { + const int i = (int)mp.mem[mp.opcode(6)], b = (int)mp.mem[mp.opcode(7)]; + if (i==0) { // Nearest neighbor interpolation. + if (b==2) return (double)mp.reference.atXYZC(cimg::mod((int)mp.mem[mp.opcode(2)],mp.reference.width()), + cimg::mod((int)mp.mem[mp.opcode(3)],mp.reference.height()), + cimg::mod((int)mp.mem[mp.opcode(4)],mp.reference.depth()), + cimg::mod((int)mp.mem[mp.opcode(5)],mp.reference.spectrum())); + if (b==1) return (double)mp.reference.atXYZC((int)mp.mem[mp.opcode(2)], + (int)mp.mem[mp.opcode(3)], + (int)mp.mem[mp.opcode(4)], + (int)mp.mem[mp.opcode(5)]); + return (double)mp.reference.atXYZC((int)mp.mem[mp.opcode(2)], + (int)mp.mem[mp.opcode(3)], + (int)mp.mem[mp.opcode(4)], + (int)mp.mem[mp.opcode(5)],0); + } else { // Linear interpolation. + if (b==2) return (double)mp.reference.linear_atXYZC(cimg::mod((float)mp.mem[mp.opcode(2)],(float)mp.reference.width()), + cimg::mod((float)mp.mem[mp.opcode(3)],(float)mp.reference.height()), + cimg::mod((float)mp.mem[mp.opcode(4)],(float)mp.reference.depth()), + cimg::mod((float)mp.mem[mp.opcode(5)],(float)mp.reference.spectrum())); + if (b==1) return (double)mp.reference.linear_atXYZC((float)mp.mem[mp.opcode(2)], + (float)mp.mem[mp.opcode(3)], + (float)mp.mem[mp.opcode(4)], + (float)mp.mem[mp.opcode(5)]); + return (double)mp.reference.linear_atXYZC((float)mp.mem[mp.opcode(2)], + (float)mp.mem[mp.opcode(3)], + (float)mp.mem[mp.opcode(4)], + (float)mp.mem[mp.opcode(5)],0); + } + } + static double mp_jxyzc(_cimg_math_parser& mp) { + const double x = mp.mem[9], y = mp.mem[10], z = mp.mem[11], c = mp.mem[12]; + const int i = (int)mp.mem[mp.opcode(6)], b = (int)mp.mem[mp.opcode(7)]; + if (i==0) { // Nearest neighbor interpolation. + if (b==2) return (double)mp.reference.atXYZC(cimg::mod((int)(x+mp.mem[mp.opcode(2)]),mp.reference.width()), + cimg::mod((int)(y+mp.mem[mp.opcode(3)]),mp.reference.height()), + cimg::mod((int)(z+mp.mem[mp.opcode(4)]),mp.reference.depth()), + cimg::mod((int)(c+mp.mem[mp.opcode(5)]),mp.reference.spectrum())); + if (b==1) return (double)mp.reference.atXYZC((int)(x+mp.mem[mp.opcode(2)]), + (int)(y+mp.mem[mp.opcode(3)]), + (int)(z+mp.mem[mp.opcode(4)]), + (int)(c+mp.mem[mp.opcode(5)])); + return (double)mp.reference.atXYZC((int)(x+mp.mem[mp.opcode(2)]), + (int)(y+mp.mem[mp.opcode(3)]), + (int)(z+mp.mem[mp.opcode(4)]), + (int)(c+mp.mem[mp.opcode(5)]),0); + } else { // Linear interpolation. + if (b==2) return (double)mp.reference.linear_atXYZC(cimg::mod((float)(x+mp.mem[mp.opcode(2)]),(float)mp.reference.width()), + cimg::mod((float)(y+mp.mem[mp.opcode(3)]),(float)mp.reference.height()), + cimg::mod((float)(z+mp.mem[mp.opcode(4)]),(float)mp.reference.depth()), + cimg::mod((float)(c+mp.mem[mp.opcode(5)]),(float)mp.reference.spectrum())); + if (b==1) return (double)mp.reference.linear_atXYZC((float)(x+mp.mem[mp.opcode(2)]), + (float)(y+mp.mem[mp.opcode(3)]), + (float)(z+mp.mem[mp.opcode(4)]), + (float)(c+mp.mem[mp.opcode(5)])); + return (double)mp.reference.linear_atXYZC((float)(x+mp.mem[mp.opcode(2)]), + (float)(y+mp.mem[mp.opcode(3)]), + (float)(z+mp.mem[mp.opcode(4)]), + (float)(c+mp.mem[mp.opcode(5)]),0); + } + } + static double mp_min(_cimg_math_parser& mp) { + double val = mp.mem[mp.opcode(2)]; + for (unsigned int i = 3; i values(mp.opcode._height-2); + double *p = values.data(); + for (unsigned int i = 2; i values(mp.opcode._height-3); + double *p = values.data(); + for (unsigned int i = 3; i::is_nan(val); + } + static double mp_isinf(_cimg_math_parser& mp) { + const double val = mp.mem[mp.opcode(2)]; + return cimg::type::is_inf(val); + } + static double mp_isint(_cimg_math_parser& mp) { + const double val = mp.mem[mp.opcode(2)]; + return (double)(cimg::mod(val,1.0)==0); + } + static double mp_isbool(_cimg_math_parser& mp) { + const double val = mp.mem[mp.opcode(2)]; + return (val==0.0 || val==1.0); + } + static double mp_rol(_cimg_math_parser& mp) { + return cimg::rol(mp.mem[mp.opcode(2)],(unsigned int)mp.mem[mp.opcode(3)]); + } + static double mp_ror(_cimg_math_parser& mp) { + return cimg::ror(mp.mem[mp.opcode(2)],(unsigned int)mp.mem[mp.opcode(3)]); + } + static double mp_lsl(_cimg_math_parser& mp) { + return (long)mp.mem[mp.opcode(2)]<<(unsigned int)mp.mem[mp.opcode(3)]; + } + static double mp_lsr(_cimg_math_parser& mp) { + return (long)mp.mem[mp.opcode(2)]>>(unsigned int)mp.mem[mp.opcode(3)]; + } + static double mp_sinc(_cimg_math_parser& mp) { + return cimg::sinc(mp.mem[mp.opcode(2)]); + } + static double mp_im(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[0]:0; + } + static double mp_iM(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[1]:0; + } + static double mp_ia(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[2]:0; + } + static double mp_iv(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[3]:0; + } + static double mp_xm(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[4]:0; + } + static double mp_ym(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[5]:0; + } + static double mp_zm(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[6]:0; + } + static double mp_cm(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[7]:0; + } + static double mp_xM(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[8]:0; + } + static double mp_yM(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[9]:0; + } + static double mp_zM(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[10]:0; + } + static double mp_cM(_cimg_math_parser& mp) { + if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); + return mp.reference_stats?mp.reference_stats[11]:0; + } + static double mp_arg(_cimg_math_parser& mp) { + const int _ind = (int)mp.mem[mp.opcode(2)]; + const unsigned int nb_args = mp.opcode._height-2, ind = _ind<0?_ind+nb_args:(unsigned int)_ind; + if (ind>=nb_args) return 0; + return mp.mem[mp.opcode(ind+2)]; + } + static double mp_int(_cimg_math_parser& mp) { + return (double)(long)mp.mem[mp.opcode(2)]; + } + static double mp_ioff(_cimg_math_parser& mp) { + const unsigned long off = (unsigned long)mp.mem[mp.opcode(2)]; + if (off>=mp.reference.size()) return 0; + return (double)mp.reference[off]; + } + static double mp_joff(_cimg_math_parser& mp) { + const int x = (int)mp.mem[9], y = (int)mp.mem[10], z = (int)mp.mem[11], c = (int)mp.mem[12]; + const unsigned long off = mp.reference.offset(x,y,z,c) + (unsigned long)(mp.mem[mp.opcode(2)]); + if (off>=mp.reference.size()) return 0; + return (double)mp.reference[off]; + } + + // Evaluation procedure, with image data. + double operator()(const double x, const double y, const double z, const double c) { + if (!mem) return 0; + mem[9] = x; mem[10] = y; mem[11] = z; mem[12] = c; + opcode._is_shared = true; opcode._width = opcode._depth = opcode._spectrum = 1; + + for (p_code = code._data; p_code &op = *p_code; + // Allows to avoid parameter passing to evaluation functions. + opcode._data = op._data; opcode._height = op._height; + const unsigned int target = (unsigned int)opcode[1]; + mem[target] = _cimg_mp_defunc(*this); + } + return mem[result]; + } + }; + + //! Compute the square value of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its square value \f$I_{(x,y,z,c)}^2\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + \par Example + \code + const CImg img("reference.jpg"); + (img,img.get_sqr().normalize(0,255)).display(); + \endcode + \image html ref_sqr.jpg + **/ + CImg& sqr() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=524288) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(val*val); }; + return *this; + } + + //! Compute the square value of each pixel value \newinstance. + CImg get_sqr() const { + return CImg(*this,false).sqr(); + } + + //! Compute the square root of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its square root \f$\sqrt{I_{(x,y,z,c)}}\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + \par Example + \code + const CImg img("reference.jpg"); + (img,img.get_sqrt().normalize(0,255)).display(); + \endcode + \image html ref_sqrt.jpg + **/ + CImg& sqrt() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::sqrt((double)*ptrd); + return *this; + } + + //! Compute the square root of each pixel value \newinstance. + CImg get_sqrt() const { + return CImg(*this,false).sqrt(); + } + + //! Compute the exponential of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its exponential \f$e^{I_{(x,y,z,c)}}\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& exp() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=4096) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::exp((double)*ptrd); + return *this; + } + + //! Compute the exponential of each pixel value \newinstance. + CImg get_exp() const { + return CImg(*this,false).exp(); + } + + //! Compute the logarithm of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its logarithm + \f$\mathrm{log}_{e}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& log() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=262144) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::log((double)*ptrd); + return *this; + } + + //! Compute the logarithm of each pixel value \newinstance. + CImg get_log() const { + return CImg(*this,false).log(); + } + + //! Compute the base-2 logarithm of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-2 logarithm + \f$\mathrm{log}_{2}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& log2() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=4096) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::log2((double)*ptrd); + return *this; + } + + //! Compute the base-10 logarithm of each pixel value \newinstance. + CImg get_log2() const { + return CImg(*this,false).log2(); + } + + //! Compute the base-10 logarithm of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-10 logarithm + \f$\mathrm{log}_{10}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& log10() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=4096) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::log10((double)*ptrd); + return *this; + } + + //! Compute the base-10 logarithm of each pixel value \newinstance. + CImg get_log10() const { + return CImg(*this,false).log10(); + } + + //! Compute the absolute value of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its absolute value \f$|I_{(x,y,z,c)}|\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& abs() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=524288) +#endif + cimg_rof(*this,ptrd,T) *ptrd = cimg::abs(*ptrd); + return *this; + } + + //! Compute the absolute value of each pixel value \newinstance. + CImg get_abs() const { + return CImg(*this,false).abs(); + } + + //! Compute the sign of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sign + \f$\mathrm{sign}(I_{(x,y,z,c)})\f$. + \note + - The sign is set to: + - \c 1 if pixel value is strictly positive. + - \c -1 if pixel value is strictly negative. + - \c 0 if pixel value is equal to \c 0. + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& sign() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = cimg::sign(*ptrd); + return *this; + } + + //! Compute the sign of each pixel value \newinstance. + CImg get_sign() const { + return CImg(*this,false).sign(); + } + + //! Compute the cosine of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its cosine \f$\cos(I_{(x,y,z,c)})\f$. + \note + - Pixel values are regarded as being in \e radian. + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& cos() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::cos((double)*ptrd); + return *this; + } + + //! Compute the cosine of each pixel value \newinstance. + CImg get_cos() const { + return CImg(*this,false).cos(); + } + + //! Compute the sine of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sine \f$\sin(I_{(x,y,z,c)})\f$. + \note + - Pixel values are regarded as being in \e radian. + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& sin() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::sin((double)*ptrd); + return *this; + } + + //! Compute the sine of each pixel value \newinstance. + CImg get_sin() const { + return CImg(*this,false).sin(); + } + + //! Compute the sinc of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sinc + \f$\mathrm{sinc}(I_{(x,y,z,c)})\f$. + \note + - Pixel values are regarded as being exin \e radian. + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& sinc() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=2048) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::sinc((double)*ptrd); + return *this; + } + + //! Compute the sinc of each pixel value \newinstance. + CImg get_sinc() const { + return CImg(*this,false).sinc(); + } + + //! Compute the tangent of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its tangent \f$\tan(I_{(x,y,z,c)})\f$. + \note + - Pixel values are regarded as being exin \e radian. + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& tan() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=2048) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::tan((double)*ptrd); + return *this; + } + + //! Compute the tangent of each pixel value \newinstance. + CImg get_tan() const { + return CImg(*this,false).tan(); + } + + //! Compute the hyperbolic cosine of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic cosine + \f$\mathrm{cosh}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& cosh() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=2048) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::cosh((double)*ptrd); + return *this; + } + + //! Compute the hyperbolic cosine of each pixel value \newinstance. + CImg get_cosh() const { + return CImg(*this,false).cosh(); + } + + //! Compute the hyperbolic sine of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic sine + \f$\mathrm{sinh}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& sinh() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=2048) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::sinh((double)*ptrd); + return *this; + } + + //! Compute the hyperbolic sine of each pixel value \newinstance. + CImg get_sinh() const { + return CImg(*this,false).sinh(); + } + + //! Compute the hyperbolic tangent of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic tangent + \f$\mathrm{tanh}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& tanh() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=2048) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::tanh((double)*ptrd); + return *this; + } + + //! Compute the hyperbolic tangent of each pixel value \newinstance. + CImg get_tanh() const { + return CImg(*this,false).tanh(); + } + + //! Compute the arccosine of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arccosine + \f$\mathrm{acos}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& acos() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::acos((double)*ptrd); + return *this; + } + + //! Compute the arccosine of each pixel value \newinstance. + CImg get_acos() const { + return CImg(*this,false).acos(); + } + + //! Compute the arcsine of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arcsine + \f$\mathrm{asin}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& asin() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::asin((double)*ptrd); + return *this; + } + + //! Compute the arcsine of each pixel value \newinstance. + CImg get_asin() const { + return CImg(*this,false).asin(); + } + + //! Compute the arctangent of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent + \f$\mathrm{atan}(I_{(x,y,z,c)})\f$. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + **/ + CImg& atan() { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::atan((double)*ptrd); + return *this; + } + + //! Compute the arctangent of each pixel value \newinstance. + CImg get_atan() const { + return CImg(*this,false).atan(); + } + + //! Compute the arctangent2 of each pixel value. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent2 + \f$\mathrm{atan2}(I_{(x,y,z,c)})\f$. + \param img Image whose pixel values specify the second argument of the \c atan2() function. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + \par Example + \code + const CImg + img_x(100,100,1,1,"x-w/2",false), // Define an horizontal centered gradient, from '-width/2' to 'width/2'. + img_y(100,100,1,1,"y-h/2",false), // Define a vertical centered gradient, from '-height/2' to 'height/2'. + img_atan2 = img_y.get_atan2(img_x); // Compute atan2(y,x) for each pixel value. + (img_x,img_y,img_atan2).display(); + \endcode + **/ + template + CImg& atan2(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return atan2(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg get_atan2(const CImg& img) const { + return CImg(*this,false).atan2(img); + } + + //! In-place pointwise multiplication. + /** + Compute the pointwise multiplication between the image instance and the specified input image \c img. + \param img Input image, as the second operand of the multiplication. + \note + - Similar to operator+=(const CImg&), except that it performs a pointwise multiplication + instead of an addition. + - It does \e not perform a \e matrix multiplication. For this purpose, use operator*=(const CImg&) instead. + \par Example + \code + CImg + img("reference.jpg"), + shade(img.width,img.height(),1,1,"-(x-w/2)^2-(y-h/2)^2",false); + shade.normalize(0,1); + (img,shade,img.get_mul(shade)).display(); + \endcode + **/ + template + CImg& mul(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return mul(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg<_cimg_Tt> get_mul(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false).mul(img); + } + + //! In-place pointwise division. + /** + Similar to mul(const CImg&), except that it performs a pointwise division instead of a multiplication. + **/ + template + CImg& div(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return div(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg<_cimg_Tt> get_div(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false).div(img); + } + + //! Raise each pixel value to a specified power. + /** + Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its power \f$I_{(x,y,z,c)}^p\f$. + \param p Exponent value. + \note + - The \inplace of this method statically casts the computed values to the pixel type \c T. + - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. + \par Example + \code + const CImg + img0("reference.jpg"), // Load reference color image. + img1 = (img0/255).pow(1.8)*=255, // Compute gamma correction, with gamma = 1.8. + img2 = (img0/255).pow(0.5)*=255; // Compute gamma correction, with gamma = 0.5. + (img0,img1,img2).display(); + \endcode + **/ + CImg& pow(const double p) { + if (is_empty()) return *this; + if (p==-4) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/(val*val*val*val)); } + return *this; + } + if (p==-3) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/(val*val*val)); } + return *this; + } + if (p==-2) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/(val*val)); } + return *this; + } + if (p==-1) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/val); } + return *this; + } + if (p==-0.5) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1/std::sqrt((double)val)); } + return *this; + } + if (p==0) return fill(1); + if (p==0.5) return sqrt(); + if (p==1) return *this; + if (p==2) return sqr(); + if (p==3) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=262144) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = val*val*val; } + return *this; + } + if (p==4) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=131072) +#endif + cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = val*val*val*val; } + return *this; + } +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=1024) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)std::pow((double)*ptrd,p); + return *this; + } + + //! Raise each pixel value to a specified power \newinstance. + CImg get_pow(const double p) const { + return CImg(*this,false).pow(p); + } + + //! Raise each pixel value to a power, specified from an expression. + /** + Similar to operator+=(const char*), except it performs a pointwise exponentiation instead of an addition. + **/ + CImg& pow(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"pow"); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)std::pow((double)*ptrd,mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)std::pow((double)*ptrd,mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)std::pow((double)*ptrd,lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)std::pow((double)*ptrd,mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + CImg values(_width,_height,_depth,_spectrum); + try { + values.fill(expression,true); + } catch (CImgException&) { + cimg::exception_mode() = omode; + values.load(expression); + } + pow(values); + } + cimg::exception_mode() = omode; + return *this; + } + + //! Raise each pixel value to a power, specified from an expression \newinstance. + CImg get_pow(const char *const expression) const { + return CImg(*this,false).pow(expression); + } + + //! Raise each pixel value to a power, pointwisely specified from another image. + /** + Similar to operator+=(const CImg& img), except that it performs an exponentiation instead of an addition. + **/ + template + CImg& pow(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return pow(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg get_pow(const CImg& img) const { + return CImg(*this,false).pow(img); + } + + //! Compute the bitwise left rotation of each pixel value. + /** + Similar to operator<<=(unsigned int), except that it performs a left rotation instead of a left shift. + **/ + CImg& rol(const unsigned int n=1) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::rol(*ptrd,n); + return *this; + } + + //! Compute the bitwise left rotation of each pixel value \newinstance. + CImg get_rol(const unsigned int n=1) const { + return (+*this).rol(n); + } + + //! Compute the bitwise left rotation of each pixel value. + /** + Similar to operator<<=(const char*), except that it performs a left rotation instead of a left shift. + **/ + CImg& rol(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"rol"); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') + cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + CImg values(_width,_height,_depth,_spectrum); + try { + values.fill(expression,true); + } catch (CImgException&) { + cimg::exception_mode() = omode; + values.load(expression); + } + rol(values); + } + cimg::exception_mode() = omode; + return *this; + } + + //! Compute the bitwise left rotation of each pixel value \newinstance. + CImg get_rol(const char *const expression) const { + return (+*this).rol(expression); + } + + //! Compute the bitwise left rotation of each pixel value. + /** + Similar to operator<<=(const CImg&), except that it performs a left rotation instead of a left shift. + **/ + template + CImg& rol(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return rol(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg get_rol(const CImg& img) const { + return (+*this).rol(img); + } + + //! Compute the bitwise right rotation of each pixel value. + /** + Similar to operator>>=(unsigned int), except that it performs a right rotation instead of a right shift. + **/ + CImg& ror(const unsigned int n=1) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::ror(*ptrd,n); + return *this; + } + + //! Compute the bitwise right rotation of each pixel value \newinstance. + CImg get_ror(const unsigned int n=1) const { + return (+*this).ror(n); + } + + //! Compute the bitwise right rotation of each pixel value. + /** + Similar to operator>>=(const char*), except that it performs a right rotation instead of a right shift. + **/ + CImg& ror(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"ror"); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') + cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + CImg values(_width,_height,_depth,_spectrum); + try { + values.fill(expression,true); + } catch (CImgException&) { + cimg::exception_mode() = omode; + values.load(expression); + } + ror(values); + } + cimg::exception_mode() = omode; + return *this; + } + + //! Compute the bitwise right rotation of each pixel value \newinstance. + CImg get_ror(const char *const expression) const { + return (+*this).ror(expression); + } + + //! Compute the bitwise right rotation of each pixel value. + /** + Similar to operator>>=(const CImg&), except that it performs a right rotation instead of a right shift. + **/ + template + CImg& ror(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return ror(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg get_ror(const CImg& img) const { + return (+*this).ror(img); + } + + //! Pointwise min operator between instance image and a value. + /** + \param val Value used as the reference argument of the min operator. + \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by + \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{val})\f$. + **/ + CImg& min(const T val) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = cimg::min(*ptrd,val); + return *this; + } + + //! Pointwise min operator between instance image and a value \newinstance. + CImg get_min(const T val) const { + return (+*this).min(val); + } + + //! Pointwise min operator between two images. + /** + \param img Image used as the reference argument of the min operator. + \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by + \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$. + **/ + template + CImg& min(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return min(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg<_cimg_Tt> get_min(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false).min(img); + } + + //! Pointwise min operator between an image and an expression. + /** + \param expression Math formula as a C-string. + \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by + \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$. + **/ + CImg& min(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"min"); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::min(*ptrd,(T)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::min(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)cimg::min(*ptrd,(T)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::min(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + CImg values(_width,_height,_depth,_spectrum); + try { + values.fill(expression,true); + } catch (CImgException&) { + cimg::exception_mode() = omode; + values.load(expression); + } + min(values); + } + cimg::exception_mode() = omode; + return *this; + } + + //! Pointwise min operator between an image and an expression \newinstance. + CImg get_min(const char *const expression) const { + return CImg(*this,false).min(expression); + } + + //! Pointwise max operator between instance image and a value. + /** + \param val Value used as the reference argument of the max operator. + \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by + \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{val})\f$. + **/ + CImg& max(const T val) { + if (is_empty()) return *this; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = cimg::max(*ptrd,val); + return *this; + } + + //! Pointwise max operator between instance image and a value \newinstance. + CImg get_max(const T val) const { + return (+*this).max(val); + } + + //! Pointwise max operator between two images. + /** + \param img Image used as the reference argument of the max operator. + \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by + \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$. + **/ + template + CImg& max(const CImg& img) { + const unsigned long siz = size(), isiz = img.size(); + if (siz && isiz) { + if (is_overlapped(img)) return max(+img); + T *ptrd = _data, *const ptre = _data + siz; + if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) + for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs + CImg<_cimg_Tt> get_max(const CImg& img) const { + return CImg<_cimg_Tt>(*this,false).max(img); + } + + //! Pointwise max operator between an image and an expression. + /** + \param expression Math formula as a C-string. + \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by + \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$. + **/ + CImg& max(const char *const expression) { + if (is_empty()) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"max"); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::max(*ptrd,(T)mp(x,y,z,c)); --ptrd; } + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::max(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) { *ptrd = (T)cimg::max(*ptrd,(T)lmp(x,y,z,c)); ++ptrd; } + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::max(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } + } + } catch (CImgException&) { + CImg values(_width,_height,_depth,_spectrum); + try { + values.fill(expression,true); + } catch (CImgException&) { + cimg::exception_mode() = omode; + values.load(expression); + } + max(values); + } + cimg::exception_mode() = omode; + return *this; + } + + //! Pointwise max operator between an image and an expression \newinstance. + CImg get_max(const char *const expression) const { + return CImg(*this,false).max(expression); + } + + //! Return a reference to the minimum pixel value. + /** + **/ + T& min() { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "min(): Empty instance.", + cimg_instance); + T *ptr_min = _data; + T min_value = *ptr_min; + cimg_for(*this,ptrs,T) if (*ptrsmax_value) max_value = *(ptr_max=ptrs); + return *ptr_max; + } + + //! Return a reference to the maximum pixel value \const. + const T& max() const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "max(): Empty instance.", + cimg_instance); + const T *ptr_max = _data; + T max_value = *ptr_max; + cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs); + return *ptr_max; + } + + //! Return a reference to the minimum pixel value as well as the maximum pixel value. + /** + \param[out] max_val Maximum pixel value. + **/ + template + T& min_max(t& max_val) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "min_max(): Empty instance.", + cimg_instance); + T *ptr_min = _data; + T min_value = *ptr_min, max_value = min_value; + cimg_for(*this,ptrs,T) { + const T val = *ptrs; + if (valmax_value) max_value = val; + } + max_val = (t)max_value; + return *ptr_min; + } + + //! Return a reference to the minimum pixel value as well as the maximum pixel value \const. + template + const T& min_max(t& max_val) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "min_max(): Empty instance.", + cimg_instance); + const T *ptr_min = _data; + T min_value = *ptr_min, max_value = min_value; + cimg_for(*this,ptrs,T) { + const T val = *ptrs; + if (valmax_value) max_value = val; + } + max_val = (t)max_value; + return *ptr_min; + } + + //! Return a reference to the maximum pixel value as well as the minimum pixel value. + /** + \param[out] min_val Minimum pixel value. + **/ + template + T& max_min(t& min_val) { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "max_min(): Empty instance.", + cimg_instance); + T *ptr_max = _data; + T max_value = *ptr_max, min_value = max_value; + cimg_for(*this,ptrs,T) { + const T val = *ptrs; + if (val>max_value) { max_value = val; ptr_max = ptrs; } + if (val + const T& max_min(t& min_val) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "max_min(): Empty instance.", + cimg_instance); + const T *ptr_max = _data; + T max_value = *ptr_max, min_value = max_value; + cimg_for(*this,ptrs,T) { + const T val = *ptrs; + if (val>max_value) { max_value = val; ptr_max = ptrs; } + if (val arr(*this); + unsigned int l = 0, ir = size() - 1; + for (;;) { + if (ir<=l+1) { + if (ir==l+1 && arr[ir]>1; + cimg::swap(arr[mid],arr[l+1]); + if (arr[l]>arr[ir]) cimg::swap(arr[l],arr[ir]); + if (arr[l+1]>arr[ir]) cimg::swap(arr[l+1],arr[ir]); + if (arr[l]>arr[l+1]) cimg::swap(arr[l],arr[l+1]); + unsigned int i = l + 1, j = ir; + const T pivot = arr[l+1]; + for (;;) { + do ++i; while (arr[i]pivot); + if (j=k) ir = j - 1; + if (j<=k) l = i; + } + } + } + + //! Return the median pixel value. + /** + **/ + T median() const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "median(): Empty instance.", + cimg_instance); + const unsigned int s = size(); + const T res = kth_smallest(s>>1); + return (s%2)?res:((res+kth_smallest((s>>1)-1))/2); + } + + //! Return the sum of all the pixel values. + /** + **/ + Tdouble sum() const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "sum(): Empty instance.", + cimg_instance); + Tdouble res = 0; + cimg_for(*this,ptrs,T) res+=(Tdouble)*ptrs; + return res; + } + + //! Return the average pixel value. + /** + **/ + Tdouble mean() const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "mean(): Empty instance.", + cimg_instance); + Tdouble res = 0; + cimg_for(*this,ptrs,T) res+=(Tdouble)*ptrs; + return res/size(); + } + + //! Return the variance of the pixel values. + /** + \param variance_method Method used to estimate the variance. Can be: + - \c 0: Second moment, computed as + \f$1/N \sum\limits_{k=1}^{N} (x_k - \bar x)^2 = + 1/N \left( \sum\limits_{k=1}^N x_k^2 - \left( \sum\limits_{k=1}^N x_k \right)^2 / N \right)\f$ + with \f$ \bar x = 1/N \sum\limits_{k=1}^N x_k \f$. + - \c 1: Best unbiased estimator, computed as \f$\frac{1}{N-1} \sum\limits_{k=1}^{N} (x_k - \bar x)^2 \f$. + - \c 2: Least median of squares. + - \c 3: Least trimmed of squares. + **/ + Tdouble variance(const unsigned int variance_method=1) const { + Tdouble foo; + return variance_mean(variance_method,foo); + } + + //! Return the variance as well as the average of the pixel values. + /** + \param variance_method Method used to estimate the variance (see variance(const unsigned int) const). + \param[out] mean Average pixel value. + **/ + template + Tdouble variance_mean(const unsigned int variance_method, t& mean) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "variance_mean(): Empty instance.", + cimg_instance); + + Tdouble variance = 0, average = 0; + const unsigned long siz = size(); + switch (variance_method) { + case 0 :{ // Least mean square (standard definition) + Tdouble S = 0, S2 = 0; + cimg_for(*this,ptrs,T) { const Tdouble val = (Tdouble)*ptrs; S+=val; S2+=val*val; } + variance = (S2 - S*S/siz)/siz; + average = S; + } break; + case 1 : { // Least mean square (robust definition) + Tdouble S = 0, S2 = 0; + cimg_for(*this,ptrs,T) { const Tdouble val = (Tdouble)*ptrs; S+=val; S2+=val*val; } + variance = siz>1?(S2 - S*S/siz)/(siz - 1):0; + average = S; + } break; + case 2 : { // Least Median of Squares (MAD) + CImg buf(*this,false); + buf.sort(); + const unsigned long siz2 = siz>>1; + const Tdouble med_i = (double)buf[siz2]; + cimg_for(buf,ptrs,Tfloat) { + const Tdouble val = (Tdouble)*ptrs; *ptrs = (Tfloat)cimg::abs(val - med_i); average+=val; + } + buf.sort(); + const Tdouble sig = (Tdouble)(1.4828*buf[siz2]); + variance = sig*sig; + } break; + default : { // Least trimmed of Squares + CImg buf(*this,false); + const unsigned long siz2 = siz>>1; + cimg_for(buf,ptrs,Tfloat) { + const Tdouble val = (Tdouble)*ptrs; (*ptrs)=(Tfloat)((*ptrs)*val); average+=val; + } + buf.sort(); + Tdouble a = 0; + const Tfloat *ptrs = buf._data; + for (unsigned long j = 0; j0?variance:0; + } + + //! Return estimated variance of the noise. + /** + \param variance_method Method used to compute the variance (see variance(const unsigned int) const). + \note Because of structures such as edges in images it is + recommanded to use a robust variance estimation. The variance of the + noise is estimated by computing the variance of the Laplacian \f$(\Delta + I)^2 \f$ scaled by a factor \f$c\f$ insuring \f$ c E[(\Delta I)^2]= + \sigma^2\f$ where \f$\sigma\f$ is the noise variance. + **/ + Tdouble variance_noise(const unsigned int variance_method=2) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "variance_noise(): Empty instance.", + cimg_instance); + + const unsigned long siz = size(); + if (!siz || !_data) return 0; + if (variance_method>1) { // Compute a scaled version of the Laplacian. + CImg tmp(*this); + if (_depth==1) { + const Tdouble cste = 1.0/std::sqrt(20.0); // Depends on how the Laplacian is computed. +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height>=262144 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + CImg_3x3(I,T); + cimg_for3x3(*this,x,y,0,c,I,T) { + tmp(x,y,c) = cste*((Tdouble)Inc + (Tdouble)Ipc + (Tdouble)Icn + + (Tdouble)Icp - 4*(Tdouble)Icc); + } + } + } else { + const Tdouble cste = 1.0/std::sqrt(42.0); // Depends on how the Laplacian is computed. +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=262144 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + CImg_3x3x3(I,T); + cimg_for3x3x3(*this,x,y,z,c,I,T) { + tmp(x,y,z,c) = cste*( + (Tdouble)Incc + (Tdouble)Ipcc + (Tdouble)Icnc + (Tdouble)Icpc + + (Tdouble)Iccn + (Tdouble)Iccp - 6*(Tdouble)Iccc); + } + } + } + return tmp.variance(variance_method); + } + + // Version that doesn't need intermediate images. + Tdouble variance = 0, S = 0, S2 = 0; + if (_depth==1) { + const Tdouble cste = 1.0/std::sqrt(20.0); + CImg_3x3(I,T); + cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,T) { + const Tdouble val = cste*((Tdouble)Inc + (Tdouble)Ipc + + (Tdouble)Icn + (Tdouble)Icp - 4*(Tdouble)Icc); + S+=val; S2+=val*val; + } + } else { + const Tdouble cste = 1.0/std::sqrt(42.0); + CImg_3x3x3(I,T); + cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,T) { + const Tdouble val = cste * + ((Tdouble)Incc + (Tdouble)Ipcc + (Tdouble)Icnc + + (Tdouble)Icpc + + (Tdouble)Iccn + (Tdouble)Iccp - 6*(Tdouble)Iccc); + S+=val; S2+=val*val; + } + } + if (variance_method) variance = siz>1?(S2 - S*S/siz)/(siz - 1):0; + else variance = (S2 - S*S/siz)/siz; + return variance>0?variance:0; + } + + //! Compute the MSE (Mean-Squared Error) between two images. + /** + \param img Image used as the second argument of the MSE operator. + **/ + template + Tdouble MSE(const CImg& img) const { + if (img.size()!=size()) + throw CImgArgumentException(_cimg_instance + "MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.", + cimg_instance, + img._width,img._height,img._depth,img._spectrum,img._data); + Tdouble vMSE = 0; + const t* ptr2 = img._data; + cimg_for(*this,ptr1,T) { + const Tdouble diff = (Tdouble)*ptr1 - (Tdouble)*(ptr2++); + vMSE+=diff*diff; + } + const unsigned long siz = img.size(); + if (siz) vMSE/=siz; + return vMSE; + } + + //! Compute the PSNR (Peak Signal-to-Noise Ratio) between two images. + /** + \param img Image used as the second argument of the PSNR operator. + \param max_value Maximum theoretical value of the signal. + **/ + template + Tdouble PSNR(const CImg& img, const Tdouble max_value=255) const { + const Tdouble vMSE = (Tdouble)std::sqrt(MSE(img)); + return (vMSE!=0)?(Tdouble)(20*std::log10(max_value/vMSE)):(Tdouble)(cimg::type::max()); + } + + //! Evaluate math formula. + /** + \param expression Math formula, as a C-string. + \param x Value of the pre-defined variable \c x. + \param y Value of the pre-defined variable \c y. + \param z Value of the pre-defined variable \c z. + \param c Value of the pre-defined variable \c c. + **/ + double eval(const char *const expression, + const double x=0, const double y=0, const double z=0, const double c=0) const { + if (!expression) return 0; + return _cimg_math_parser(*this,expression,"eval")(x,y,z,c); + } + + //! Evaluate math formula on a set of variables. + /** + \param expression Math formula, as a C-string. + \param xyzc Set of values (x,y,z,c) used for the evaluation. + **/ + template + CImg eval(const char *const expression, const CImg& xyzc) const { + CImg res(1,xyzc.size()/4); + if (!expression) return res.fill(0); + _cimg_math_parser mp(*this,expression,"eval"); +#ifdef cimg_use_openmp +#pragma omp parallel if (res._height>=512 && std::strlen(expression)>=6) + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for + for (unsigned int i = 0; i[min; max; mean; variance; xmin; ymin; zmin; cmin; xmax; ymax; zmax; cmax]. + **/ + CImg get_stats(const unsigned int variance_method=1) const { + if (is_empty()) return CImg(); + const unsigned long siz = size(); + const T *const odata = _data; + const T *pm = odata, *pM = odata; + Tdouble S = 0, S2 = 0; + T m = *pm, M = m; + cimg_for(*this,ptrs,T) { + const T val = *ptrs; + const Tdouble _val = (Tdouble)val; + if (valM) { M = val; pM = ptrs; } + S+=_val; + S2+=_val*_val; + } + const Tdouble + mean_value = S/siz, + _variance_value = variance_method==0?(S2 - S*S/siz)/siz: + (variance_method==1?(siz>1?(S2 - S*S/siz)/(siz - 1):0): + variance(variance_method)), + variance_value = _variance_value>0?_variance_value:0; + int + xm = 0, ym = 0, zm = 0, cm = 0, + xM = 0, yM = 0, zM = 0, cM = 0; + contains(*pm,xm,ym,zm,cm); + contains(*pM,xM,yM,zM,cM); + return CImg(1,12).fill((Tdouble)m,(Tdouble)M,mean_value,variance_value, + (Tdouble)xm,(Tdouble)ym,(Tdouble)zm,(Tdouble)cm, + (Tdouble)xM,(Tdouble)yM,(Tdouble)zM,(Tdouble)cM); + } + + //! Compute statistics vector from the pixel values \inplace. + CImg& stats(const unsigned int variance_method=1) { + return get_stats(variance_method).move_to(*this); + } + + //@} + //------------------------------------- + // + //! \name Vector / Matrix Operations + //@{ + //------------------------------------- + + //! Compute norm of the image, viewed as a matrix. + /** + \param magnitude_type Norm type. Can be: + - \c -1: Linf-norm + - \c 0: L2-norm + - \c 1: L1-norm + **/ + Tdouble magnitude(const int magnitude_type=2) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "magnitude(): Empty instance.", + cimg_instance); + Tdouble res = 0; + switch (magnitude_type) { + case -1 : { + cimg_for(*this,ptrs,T) { const Tdouble val = (Tdouble)cimg::abs(*ptrs); if (val>res) res = val; } + } break; + case 1 : { + cimg_for(*this,ptrs,T) res+=(Tdouble)cimg::abs(*ptrs); + } break; + default : { + cimg_for(*this,ptrs,T) res+=(Tdouble)cimg::sqr(*ptrs); + res = (Tdouble)std::sqrt(res); + } + } + return res; + } + + //! Compute the trace of the image, viewed as a matrix. + /** + **/ + Tdouble trace() const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "trace(): Empty instance.", + cimg_instance); + Tdouble res = 0; + cimg_forX(*this,k) res+=(Tdouble)(*this)(k,k); + return res; + } + + //! Compute the determinant of the image, viewed as a matrix. + /** + **/ + Tdouble det() const { + if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1) + throw CImgInstanceException(_cimg_instance + "det(): Instance is not a square matrix.", + cimg_instance); + + switch (_width) { + case 1 : return (Tdouble)((*this)(0,0)); + case 2 : return (Tdouble)((*this)(0,0))*(Tdouble)((*this)(1,1)) - (Tdouble)((*this)(0,1))*(Tdouble)((*this)(1,0)); + case 3 : { + const Tdouble + a = (Tdouble)_data[0], d = (Tdouble)_data[1], g = (Tdouble)_data[2], + b = (Tdouble)_data[3], e = (Tdouble)_data[4], h = (Tdouble)_data[5], + c = (Tdouble)_data[6], f = (Tdouble)_data[7], i = (Tdouble)_data[8]; + return i*a*e - a*h*f - i*b*d + b*g*f + c*d*h - c*g*e; + } + default : { + CImg lu(*this); + CImg indx; + bool d; + lu._LU(indx,d); + Tdouble res = d?(Tdouble)1:(Tdouble)-1; + cimg_forX(lu,i) res*=lu(i,i); + return res; + } + } + } + + //! Compute the dot product between instance and argument, viewed as matrices. + /** + \param img Image used as a second argument of the dot product. + **/ + template + Tdouble dot(const CImg& img) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "dot(): Empty instance.", + cimg_instance); + if (!img) + throw CImgArgumentException(_cimg_instance + "dot(): Empty specified image.", + cimg_instance); + + const unsigned int nb = cimg::min(size(),img.size()); + Tdouble res = 0; + for (unsigned int off = 0; off get_vector_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const { + CImg res; + if (res._height!=_spectrum) res.assign(1,_spectrum); + const unsigned long whd = (unsigned long)_width*_height*_depth; + const T *ptrs = data(x,y,z); + T *ptrd = res._data; + cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; } + return res; + } + + //! Get (square) matrix-valued pixel located at specified position. + /** + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \note - The spectrum() of the image must be a square. + **/ + CImg get_matrix_at(const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) const { + const int n = (int)std::sqrt((double)_spectrum); + const T *ptrs = data(x,y,z,0); + const unsigned long whd = (unsigned long)_width*_height*_depth; + CImg res(n,n); + T *ptrd = res._data; + cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; } + return res; + } + + //! Get tensor-valued pixel located at specified position. + /** + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + **/ + CImg get_tensor_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const { + const T *ptrs = data(x,y,z,0); + const unsigned long whd = (unsigned long)_width*_height*_depth; + if (_spectrum==6) return tensor(*ptrs,*(ptrs+whd),*(ptrs+2*whd),*(ptrs+3*whd),*(ptrs+4*whd),*(ptrs+5*whd)); + if (_spectrum==3) return tensor(*ptrs,*(ptrs+whd),*(ptrs+2*whd)); + return tensor(*ptrs); + } + + //! Set vector-valued pixel at specified position. + /** + \param vec Vector to put on the instance image. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + **/ + template + CImg& set_vector_at(const CImg& vec, const unsigned int x, const unsigned int y=0, const unsigned int z=0) { + if (x<_width && y<_height && z<_depth) { + const t *ptrs = vec._data; + const unsigned long whd = (unsigned long)_width*_height*_depth; + T *ptrd = data(x,y,z); + for (unsigned int k = cimg::min((unsigned int)vec.size(),_spectrum); k; --k) { + *ptrd = (T)*(ptrs++); ptrd+=whd; + } + } + return *this; + } + + //! Set (square) matrix-valued pixel at specified position. + /** + \param mat Matrix to put on the instance image. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + **/ + template + CImg& set_matrix_at(const CImg& mat, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) { + return set_vector_at(mat,x,y,z); + } + + //! Set tensor-valued pixel at specified position. + /** + \param ten Tensor to put on the instance image. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + **/ + template + CImg& set_tensor_at(const CImg& ten, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) { + T *ptrd = data(x,y,z,0); + const unsigned long siz = (unsigned long)_width*_height*_depth; + if (ten._height==2) { + *ptrd = (T)ten[0]; ptrd+=siz; + *ptrd = (T)ten[1]; ptrd+=siz; + *ptrd = (T)ten[3]; + } + else { + *ptrd = (T)ten[0]; ptrd+=siz; + *ptrd = (T)ten[1]; ptrd+=siz; + *ptrd = (T)ten[2]; ptrd+=siz; + *ptrd = (T)ten[4]; ptrd+=siz; + *ptrd = (T)ten[5]; ptrd+=siz; + *ptrd = (T)ten[8]; + } + return *this; + } + + //! Unroll pixel values along axis \c y. + /** + \note Equivalent to \code unroll('y'); \endcode. + **/ + CImg& vector() { + return unroll('y'); + } + + //! Unroll pixel values along axis \c y \newinstance. + CImg get_vector() const { + return get_unroll('y'); + } + + //! Resize image to become a scalar square matrix. + /** + **/ + CImg& matrix() { + const unsigned long siz = size(); + switch (siz) { + case 1 : break; + case 4 : _width = _height = 2; break; + case 9 : _width = _height = 3; break; + case 16 : _width = _height = 4; break; + case 25 : _width = _height = 5; break; + case 36 : _width = _height = 6; break; + case 49 : _width = _height = 7; break; + case 64 : _width = _height = 8; break; + case 81 : _width = _height = 9; break; + case 100 : _width = _height = 10; break; + default : { + unsigned long i = 11, i2 = i*i; + while (i2 get_matrix() const { + return (+*this).matrix(); + } + + //! Resize image to become a symmetric tensor. + /** + **/ + CImg& tensor() { + return get_tensor().move_to(*this); + } + + //! Resize image to become a symmetric tensor \newinstance. + CImg get_tensor() const { + CImg res; + const unsigned long siz = size(); + switch (siz) { + case 1 : break; + case 3 : + res.assign(2,2); + res(0,0) = (*this)(0); + res(1,0) = res(0,1) = (*this)(1); + res(1,1) = (*this)(2); + break; + case 6 : + res.assign(3,3); + res(0,0) = (*this)(0); + res(1,0) = res(0,1) = (*this)(1); + res(2,0) = res(0,2) = (*this)(2); + res(1,1) = (*this)(3); + res(2,1) = res(1,2) = (*this)(4); + res(2,2) = (*this)(5); + break; + default : + throw CImgInstanceException(_cimg_instance + "tensor(): Invalid instance size (does not define a 1x1, 2x2 or 3x3 tensor).", + cimg_instance); + } + return res; + } + + //! Resize image to become a diagonal matrix. + /** + \note Transform the image as a diagonal matrix so that each of its initial value becomes a diagonal coefficient. + **/ + CImg& diagonal() { + return get_diagonal().move_to(*this); + } + + //! Resize image to become a diagonal matrix \newinstance. + CImg get_diagonal() const { + if (is_empty()) return *this; + CImg res(size(),size(),1,1,0); + cimg_foroff(*this,off) res(off,off) = (*this)(off); + return res; + } + + //! Replace the image by an identity matrix. + /** + \note If the instance image is not square, it is resized to a square matrix using its maximum + dimension as a reference. + **/ + CImg& identity_matrix() { + return identity_matrix(cimg::max(_width,_height)).move_to(*this); + } + + //! Replace the image by an identity matrix \newinstance. + CImg get_identity_matrix() const { + return identity_matrix(cimg::max(_width,_height)); + } + + //! Fill image with a linear sequence of values. + /** + \param a0 Starting value of the sequence. + \param a1 Ending value of the sequence. + **/ + CImg& sequence(const T a0, const T a1) { + if (is_empty()) return *this; + const unsigned int siz = size() - 1; + T* ptr = _data; + if (siz) { + const Tdouble delta = (Tdouble)a1 - (Tdouble)a0; + cimg_foroff(*this,l) *(ptr++) = (T)(a0 + delta*l/siz); + } else *ptr = a0; + return *this; + } + + //! Fill image with a linear sequence of values \newinstance. + CImg get_sequence(const T a0, const T a1) const { + return (+*this).sequence(a0,a1); + } + + //! Transpose the image, viewed as a matrix. + /** + \note Equivalent to \code permute_axes("yxzc"); \endcode + **/ + CImg& transpose() { + if (_width==1) { _width = _height; _height = 1; return *this; } + if (_height==1) { _height = _width; _width = 1; return *this; } + if (_width==_height) { + cimg_forYZC(*this,y,z,c) for (int x = y; x get_transpose() const { + return get_permute_axes("yxzc"); + } + + //! Compute the cross product between two \c 1x3 images, viewed as 3d vectors. + /** + \param img Image used as the second argument of the cross product. + \note The first argument of the cross product is \c *this. + **/ + template + CImg& cross(const CImg& img) { + if (_width!=1 || _height<3 || img._width!=1 || img._height<3) + throw CImgInstanceException(_cimg_instance + "cross(): Instance and/or specified image (%u,%u,%u,%u,%p) are not 3d vectors.", + cimg_instance, + img._width,img._height,img._depth,img._spectrum,img._data); + + const T x = (*this)[0], y = (*this)[1], z = (*this)[2]; + (*this)[0] = (T)(y*img[2] - z*img[1]); + (*this)[1] = (T)(z*img[0] - x*img[2]); + (*this)[2] = (T)(x*img[1] - y*img[0]); + return *this; + } + + //! Compute the cross product between two \c 1x3 images, viewed as 3d vectors \newinstance. + template + CImg<_cimg_Tt> get_cross(const CImg& img) const { + return CImg<_cimg_Tt>(*this).cross(img); + } + + //! Invert the instance image, viewed as a matrix. + /** + \param use_LU Choose the inverting algorithm. Can be: + - \c true: LU-based matrix inversion. + - \c false: SVD-based matrix inversion. + **/ + CImg& invert(const bool use_LU=true) { + if (_width!=_height || _depth!=1 || _spectrum!=1) + throw CImgInstanceException(_cimg_instance + "invert(): Instance is not a square matrix.", + cimg_instance); +#ifdef cimg_use_lapack + int INFO = (int)use_LU, N = _width, LWORK = 4*N, *const IPIV = new int[N]; + Tfloat + *const lapA = new Tfloat[N*N], + *const WORK = new Tfloat[LWORK]; + cimg_forXY(*this,k,l) lapA[k*N+l] = (Tfloat)((*this)(k,l)); + cimg::getrf(N,lapA,IPIV,INFO); + if (INFO) + cimg::warn(_cimg_instance + "invert(): LAPACK function dgetrf_() returned error code %d.", + cimg_instance, + INFO); + else { + cimg::getri(N,lapA,IPIV,WORK,LWORK,INFO); + if (INFO) + cimg::warn(_cimg_instance + "invert(): LAPACK function dgetri_() returned error code %d.", + cimg_instance, + INFO); + } + if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)(lapA[k*N+l]); else fill(0); + delete[] IPIV; delete[] lapA; delete[] WORK; +#else + const double dete = _width>3?-1.0:det(); + if (dete!=0.0 && _width==2) { + const double + a = _data[0], c = _data[1], + b = _data[2], d = _data[3]; + _data[0] = (T)(d/dete); _data[1] = (T)(-c/dete); + _data[2] = (T)(-b/dete); _data[3] = (T)(a/dete); + } else if (dete!=0.0 && _width==3) { + const double + a = _data[0], d = _data[1], g = _data[2], + b = _data[3], e = _data[4], h = _data[5], + c = _data[6], f = _data[7], i = _data[8]; + _data[0] = (T)((i*e-f*h)/dete), _data[1] = (T)((g*f-i*d)/dete), _data[2] = (T)((d*h-g*e)/dete); + _data[3] = (T)((h*c-i*b)/dete), _data[4] = (T)((i*a-c*g)/dete), _data[5] = (T)((g*b-a*h)/dete); + _data[6] = (T)((b*f-e*c)/dete), _data[7] = (T)((d*c-a*f)/dete), _data[8] = (T)((a*e-d*b)/dete); + } else { + if (use_LU) { // LU-based inverse computation + CImg A(*this), indx, col(1,_width); + bool d; + A._LU(indx,d); + cimg_forX(*this,j) { + col.fill(0); + col(j) = 1; + col._solve(A,indx); + cimg_forX(*this,i) (*this)(j,i) = (T)col(i); + } + } else { // SVD-based inverse computation + CImg U(_width,_width), S(1,_width), V(_width,_width); + SVD(U,S,V,false); + U.transpose(); + cimg_forY(S,k) if (S[k]!=0) S[k]=1/S[k]; + S.diagonal(); + *this = V*S*U; + } + } +#endif + return *this; + } + + //! Invert the instance image, viewed as a matrix \newinstance. + CImg get_invert(const bool use_LU=true) const { + return CImg(*this,false).invert(use_LU); + } + + //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix. + /** + **/ + CImg& pseudoinvert() { + return get_pseudoinvert().move_to(*this); + } + + //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix \newinstance. + CImg get_pseudoinvert() const { + CImg U, S, V; + SVD(U,S,V); + const Tfloat tolerance = (sizeof(Tfloat)<=4?5.96e-8f:1.11e-16f)*cimg::max(_width,_height)*S.max(); + cimg_forX(V,x) { + const Tfloat s = S(x), invs = s>tolerance?1/s:(Tfloat)0; + cimg_forY(V,y) V(x,y)*=invs; + } + return V*U.transpose(); + } + + //! Solve a system of linear equations. + /** + \param A Matrix of the linear system. + \note Solve \c AX=B where \c B=*this. + **/ + template + CImg& solve(const CImg& A) { + if (_width!=1 || _depth!=1 || _spectrum!=1 || _height!=A._height || A._depth!=1 || A._spectrum!=1) + throw CImgArgumentException(_cimg_instance + "solve(): Instance and specified matrix (%u,%u,%u,%u,%p) have " + "incompatible dimensions.", + cimg_instance, + A._width,A._height,A._depth,A._spectrum,A._data); + typedef _cimg_Ttfloat Ttfloat; + if (A._width==A._height) { +#ifdef cimg_use_lapack + char TRANS = 'N'; + int INFO, N = _height, LWORK = 4*N, *const IPIV = new int[N]; + Ttfloat + *const lapA = new Ttfloat[N*N], + *const lapB = new Ttfloat[N], + *const WORK = new Ttfloat[LWORK]; + cimg_forXY(A,k,l) lapA[k*N+l] = (Ttfloat)(A(k,l)); + cimg_forY(*this,i) lapB[i] = (Ttfloat)((*this)(i)); + cimg::getrf(N,lapA,IPIV,INFO); + if (INFO) + cimg::warn(_cimg_instance + "solve(): LAPACK library function dgetrf_() returned error code %d.", + cimg_instance, + INFO); + + if (!INFO) { + cimg::getrs(TRANS,N,lapA,IPIV,lapB,INFO); + if (INFO) + cimg::warn(_cimg_instance + "solve(): LAPACK library function dgetrs_() returned error code %d.", + cimg_instance, + INFO); + } + if (!INFO) cimg_forY(*this,i) (*this)(i) = (T)(lapB[i]); else fill(0); + delete[] IPIV; delete[] lapA; delete[] lapB; delete[] WORK; +#else + CImg lu(A,false); + CImg indx; + bool d; + lu._LU(indx,d); + _solve(lu,indx); +#endif + } else { // Least-square solution for non-square systems. +#ifdef cimg_use_lapack + char TRANS = 'N'; + int INFO, N = A._width, M = A._height, LWORK = -1, LDA = M, LDB = M, NRHS = _width; + Ttfloat WORK_QUERY; + Ttfloat + * const lapA = new Ttfloat[M*N], + * const lapB = new Ttfloat[M*NRHS]; + cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, &WORK_QUERY, LWORK, INFO); + LWORK = (int) WORK_QUERY; + Ttfloat *const WORK = new Ttfloat[LWORK]; + cimg_forXY(A,k,l) lapA[k*M+l] = (Ttfloat)(A(k,l)); + cimg_forXY(*this,k,l) lapB[k*M+l] = (Ttfloat)((*this)(k,l)); + cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, WORK, LWORK, INFO); + if (INFO != 0) + cimg::warn(_cimg_instance + "solve(): LAPACK library function sgels() returned error code %d.", + cimg_instance, + INFO); + assign(NRHS, N); + if (!INFO != 0) + cimg_forXY(*this,k,l) (*this)(k,l) = (T)lapB[k*M+l]; + else + assign(A.get_pseudoinvert()*(*this)); + delete[] lapA; delete[] lapB; delete[] WORK; +#else + assign(A.get_pseudoinvert()*(*this)); +#endif + } + return *this; + } + + //! Solve a system of linear equations \newinstance. + template + CImg<_cimg_Ttfloat> get_solve(const CImg& A) const { + return CImg<_cimg_Ttfloat>(*this,false).solve(A); + } + + template + CImg& _solve(const CImg& A, const CImg& indx) { + typedef _cimg_Ttfloat Ttfloat; + const int N = size(); + int ii = -1; + Ttfloat sum; + for (int i = 0; i=0) for (int j = ii; j<=i-1; ++j) sum-=A(j,i)*(*this)(j); + else if (sum!=0) ii = i; + (*this)(i) = (T)sum; + } + for (int i = N - 1; i>=0; --i) { + sum = (*this)(i); + for (int j = i + 1; j + CImg& solve_tridiagonal(const CImg& A) { + const unsigned int siz = (int)size(); + if (A._width!=3 || A._height!=siz) + throw CImgArgumentException(_cimg_instance + "solve_tridiagonal(): Instance and tridiagonal matrix " + "(%u,%u,%u,%u,%p) have incompatible dimensions.", + cimg_instance, + A._width,A._height,A._depth,A._spectrum,A._data); + typedef _cimg_Ttfloat Ttfloat; + const Ttfloat epsilon = 1e-4f; + CImg B = A.get_column(1), V(*this,false); + for (int i = 1; i<(int)siz; ++i) { + const Ttfloat m = A(0,i)/(B[i-1]?B[i-1]:epsilon); + B[i] -= m*A(2,i-1); + V[i] -= m*V[i-1]; + } + (*this)[siz-1] = (T)(V[siz-1]/(B[siz-1]?B[siz-1]:epsilon)); + for (int i = (int)siz - 2; i>=0; --i) (*this)[i] = (T)((V[i] - A(2,i)*(*this)[i+1])/(B[i]?B[i]:epsilon)); + return *this; + } + + //! Solve a tridiagonal system of linear equations \newinstance. + template + CImg<_cimg_Ttfloat> get_solve_tridiagonal(const CImg& A) const { + return CImg<_cimg_Ttfloat>(*this,false).solve_tridiagonal(A); + } + + //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix. + /** + \param[out] val Vector of the estimated eigenvalues, in decreasing order. + \param[out] vec Matrix of the estimated eigenvalues, sorted by columns. + **/ + template + const CImg& eigen(CImg& val, CImg &vec) const { + if (is_empty()) { val.assign(); vec.assign(); } + else { + if (_width!=_height || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "eigen(): Instance is not a square matrix.", + cimg_instance); + + if (val.size()<(unsigned long)_width) val.assign(1,_width); + if (vec.size()<(unsigned long)_width*_width) vec.assign(_width,_width); + switch (_width) { + case 1 : { val[0] = (t)(*this)[0]; vec[0] = (t)1; } break; + case 2 : { + const double a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3], e = a + d; + double f = e*e - 4*(a*d - b*c); + if (f<0) + cimg::warn(_cimg_instance + "eigen(): Complex eigenvalues found.", + cimg_instance); + + f = std::sqrt(f); + const double l1 = 0.5*(e-f), l2 = 0.5*(e+f); + const double theta1 = std::atan2(l2-a,b), theta2 = std::atan2(l1-a,b); + val[0] = (t)l2; + val[1] = (t)l1; + vec(0,0) = (t)std::cos(theta1); + vec(0,1) = (t)std::sin(theta1); + vec(1,0) = (t)std::cos(theta2); + vec(1,1) = (t)std::sin(theta2); + } break; + default : + throw CImgInstanceException(_cimg_instance + "eigen(): Eigenvalues computation of general matrices is limited " + "to 2x2 matrices.", + cimg_instance); + } + } + return *this; + } + + //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix. + /** + \return A list of two images [val; vec], whose meaning is similar as in eigen(CImg&,CImg&) const. + **/ + CImgList get_eigen() const { + CImgList res(2); + eigen(res[0],res[1]); + return res; + } + + //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix. + /** + \param[out] val Vector of the estimated eigenvalues, in decreasing order. + \param[out] vec Matrix of the estimated eigenvalues, sorted by columns. + **/ + template + const CImg& symmetric_eigen(CImg& val, CImg& vec) const { + if (is_empty()) { val.assign(); vec.assign(); } + else { +#ifdef cimg_use_lapack + char JOB = 'V', UPLO = 'U'; + int N = _width, LWORK = 4*N, INFO; + Tfloat + *const lapA = new Tfloat[N*N], + *const lapW = new Tfloat[N], + *const WORK = new Tfloat[LWORK]; + cimg_forXY(*this,k,l) lapA[k*N+l] = (Tfloat)((*this)(k,l)); + cimg::syev(JOB,UPLO,N,lapA,lapW,WORK,LWORK,INFO); + if (INFO) + cimg::warn(_cimg_instance + "symmetric_eigen(): LAPACK library function dsyev_() returned error code %d.", + cimg_instance, + INFO); + + val.assign(1,N); + vec.assign(N,N); + if (!INFO) { + cimg_forY(val,i) val(i) = (T)lapW[N-1-i]; + cimg_forXY(vec,k,l) vec(k,l) = (T)(lapA[(N-1-k)*N+l]); + } else { val.fill(0); vec.fill(0); } + delete[] lapA; delete[] lapW; delete[] WORK; +#else + if (_width!=_height || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "eigen(): Instance is not a square matrix.", + cimg_instance); + + val.assign(1,_width); + if (vec._data) vec.assign(_width,_width); + if (_width<3) { + eigen(val,vec); + if (_width==2) { vec[1] = -vec[2]; vec[3] = vec[0]; } // Force orthogonality for 2x2 matrices. + return *this; + } + CImg V(_width,_width); + SVD(vec,val,V,false); + + bool is_ambiguous = false; + float eig = 0; + cimg_forY(val,p) { // check for ambiguous cases. + if (val[p]>eig) eig = (float)val[p]; + t scal = 0; + cimg_forY(vec,y) scal+=vec(p,y)*V(p,y); + if (cimg::abs(scal)<0.9f) is_ambiguous = true; + if (scal<0) val[p] = -val[p]; + } + if (is_ambiguous) { + ++(eig*=2); + SVD(vec,val,V,false,40,eig); + val-=eig; + } + CImg permutations; // sort eigenvalues in decreasing order + CImg tmp(_width); + val.sort(permutations,false); + cimg_forY(vec,k) { + cimg_forY(permutations,y) tmp(y) = vec(permutations(y),k); + std::memcpy(vec.data(0,k),tmp._data,sizeof(t)*_width); + } +#endif + } + return *this; + } + + //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix. + /** + \return A list of two images [val; vec], whose meaning are similar as in + symmetric_eigen(CImg&,CImg&) const. + **/ + CImgList get_symmetric_eigen() const { + CImgList res(2); + symmetric_eigen(res[0],res[1]); + return res; + } + + //! Sort pixel values and get sorting permutations. + /** + \param[out] permutations Permutation map used for the sorting. + \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way. + **/ + template + CImg& sort(CImg& permutations, const bool is_increasing=true) { + permutations.assign(_width,_height,_depth,_spectrum); + if (is_empty()) return *this; + cimg_foroff(permutations,off) permutations[off] = (t)off; + return _quicksort(0,size()-1,permutations,is_increasing,true); + } + + //! Sort pixel values and get sorting permutations \newinstance. + template + CImg get_sort(CImg& permutations, const bool is_increasing=true) const { + return (+*this).sort(permutations,is_increasing); + } + + //! Sort pixel values. + /** + \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way. + \param axis Tells if the value sorting must be done along a specific axis. Can be: + - \c 0: All pixel values are sorted, independently on their initial position. + - \c 'x': Image columns are sorted, according to the first value in each column. + - \c 'y': Image rows are sorted, according to the first value in each row. + - \c 'z': Image slices are sorted, according to the first value in each slice. + - \c 'c': Image channels are sorted, according to the first value in each channel. + **/ + CImg& sort(const bool is_increasing=true, const char axis=0) { + if (is_empty()) return *this; + CImg perm; + switch (cimg::uncase(axis)) { + case 0 : + _quicksort(0,size()-1,perm,is_increasing,false); + break; + case 'x' : { + perm.assign(_width); + get_crop(0,0,0,0,_width-1,0,0,0).sort(perm,is_increasing); + CImg img(*this,false); + cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(perm[x],y,z,c); + } break; + case 'y' : { + perm.assign(_height); + get_crop(0,0,0,0,0,_height-1,0,0).sort(perm,is_increasing); + CImg img(*this,false); + cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,perm[y],z,c); + } break; + case 'z' : { + perm.assign(_depth); + get_crop(0,0,0,0,0,0,_depth-1,0).sort(perm,is_increasing); + CImg img(*this,false); + cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,perm[z],c); + } break; + case 'c' : { + perm.assign(_spectrum); + get_crop(0,0,0,0,0,0,0,_spectrum-1).sort(perm,is_increasing); + CImg img(*this,false); + cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,z,perm[c]); + } break; + default : + throw CImgArgumentException(_cimg_instance + "sort(): Invalid specified axis '%c' " + "(should be { x | y | z | c }).", + cimg_instance,axis); + } + return *this; + } + + //! Sort pixel values \newinstance. + CImg get_sort(const bool is_increasing=true, const char axis=0) const { + return (+*this).sort(is_increasing,axis); + } + + template + CImg& _quicksort(const int indm, const int indM, CImg& permutations, + const bool is_increasing, const bool is_permutations) { + if (indm(*this)[mid]) { + cimg::swap((*this)[indm],(*this)[mid]); + if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); + } + if ((*this)[mid]>(*this)[indM]) { + cimg::swap((*this)[indM],(*this)[mid]); + if (is_permutations) cimg::swap(permutations[indM],permutations[mid]); + } + if ((*this)[indm]>(*this)[mid]) { + cimg::swap((*this)[indm],(*this)[mid]); + if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); + } + } else { + if ((*this)[indm]<(*this)[mid]) { + cimg::swap((*this)[indm],(*this)[mid]); + if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); + } + if ((*this)[mid]<(*this)[indM]) { + cimg::swap((*this)[indM],(*this)[mid]); + if (is_permutations) cimg::swap(permutations[indM],permutations[mid]); + } + if ((*this)[indm]<(*this)[mid]) { + cimg::swap((*this)[indm],(*this)[mid]); + if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); + } + } + if (indM - indm>=3) { + const T pivot = (*this)[mid]; + int i = indm, j = indM; + if (is_increasing) { + do { + while ((*this)[i]pivot) --j; + if (i<=j) { + if (is_permutations) cimg::swap(permutations[i],permutations[j]); + cimg::swap((*this)[i++],(*this)[j--]); + } + } while (i<=j); + } else { + do { + while ((*this)[i]>pivot) ++i; + while ((*this)[j] A; // Input matrix (assumed to contain some values). + CImg<> U,S,V; + A.SVD(U,S,V) + \endcode + **/ + template + const CImg& SVD(CImg& U, CImg& S, CImg& V, const bool sorting=true, + const unsigned int max_iteration=40, const float lambda=0) const { + if (is_empty()) { U.assign(); S.assign(); V.assign(); } + else { + U = *this; + if (lambda!=0) { + const unsigned int delta = cimg::min(U._width,U._height); + for (unsigned int i = 0; i rv1(_width); + t anorm = 0, c, f, g = 0, h, s, scale = 0; + int l = 0, nm = 0; + + cimg_forX(U,i) { + l = i+1; rv1[i] = scale*g; g = s = scale = 0; + if (i=0?-1:1)*std::sqrt(s)); h=f*g-s; U(i,i) = f-g; + for (int j = l; j=0?-1:1)*std::sqrt(s)); h = f*g-s; U(l,i) = f-g; + for (int k = l; k=0; --i) { + if (i=0; --i) { + l = i+1; g = S[i]; + for (int j = l; j=0; --k) { + for (unsigned int its = 0; its=1; --l) { + nm = l-1; + if ((cimg::abs(rv1[l])+anorm)==anorm) { flag = false; break; } + if ((cimg::abs(S[nm])+anorm)==anorm) break; + } + if (flag) { + c = 0; s = 1; + for (int i = l; i<=k; ++i) { + f = s*rv1[i]; rv1[i] = c*rv1[i]; + if ((cimg::abs(f)+anorm)==anorm) break; + g = S[i]; h = (t)cimg::_pythagore(f,g); S[i] = h; h = 1/h; c = g*h; s = -f*h; + cimg_forY(U,j) { const t y = U(nm,j), z = U(i,j); U(nm,j) = y*c + z*s; U(i,j) = z*c - y*s; } + } + } + + const t z = S[k]; + if (l==k) { if (z<0) { S[k] = -z; cimg_forX(U,j) V(k,j) = -V(k,j); } break; } + nm = k-1; + t x = S[l], y = S[nm]; + g = rv1[nm]; h = rv1[k]; + f = ((y-z)*(y+z)+(g-h)*(g+h))/cimg::max((t)1e-25,2*h*y); + g = (t)cimg::_pythagore(f,1.0); + f = ((x-z)*(x+z)+h*((y/(f + (f>=0?g:-g)))-h))/cimg::max((t)1e-25,x); + c = s = 1; + for (int j = l; j<=nm; ++j) { + const int i = j+1; + g = rv1[i]; h = s*g; g = c*g; + t y = S[i]; + t z = (t)cimg::_pythagore(f,h); + rv1[j] = z; c = f/cimg::max((t)1e-25,z); s = h/cimg::max((t)1e-25,z); + f = x*c+g*s; g = g*c-x*s; h = y*s; y*=c; + cimg_forX(U,jj) { const t x = V(j,jj), z = V(i,jj); V(j,jj) = x*c + z*s; V(i,jj) = z*c - x*s; } + z = (t)cimg::_pythagore(f,h); S[j] = z; + if (z) { z = 1/cimg::max((t)1e-25,z); c = f*z; s = h*z; } + f = c*g+s*y; x = c*y-s*g; + cimg_forY(U,jj) { const t y = U(j,jj); z = U(i,jj); U(j,jj) = y*c + z*s; U(i,jj) = z*c - y*s; } + } + rv1[l] = 0; rv1[k]=f; S[k]=x; + } + } + + if (sorting) { + CImg permutations; + CImg tmp(_width); + S.sort(permutations,false); + cimg_forY(U,k) { + cimg_forY(permutations,y) tmp(y) = U(permutations(y),k); + std::memcpy(U.data(0,k),tmp._data,sizeof(t)*_width); + } + cimg_forY(V,k) { + cimg_forY(permutations,y) tmp(y) = V(permutations(y),k); + std::memcpy(V.data(0,k),tmp._data,sizeof(t)*_width); + } + } + } + return *this; + } + + //! Compute the SVD of the instance image, viewed as a general matrix. + /** + \return A list of three images [U; S; V], whose meaning is similar as in + SVD(CImg&,CImg&,CImg&,bool,unsigned int,float) const. + **/ + CImgList get_SVD(const bool sorting=true, + const unsigned int max_iteration=40, const float lambda=0) const { + CImgList res(3); + SVD(res[0],res[1],res[2],sorting,max_iteration,lambda); + return res; + } + + // [internal] Compute the LU decomposition of a permuted matrix. + template + CImg& _LU(CImg& indx, bool& d) { + const int N = width(); + int imax = 0; + CImg vv(N); + indx.assign(N); + d = true; + cimg_forX(*this,i) { + Tfloat vmax = 0; + cimg_forX(*this,j) { + const Tfloat tmp = cimg::abs((*this)(j,i)); + if (tmp>vmax) vmax = tmp; + } + if (vmax==0) { indx.fill(0); return fill(0); } + vv[i] = 1/vmax; + } + cimg_forX(*this,j) { + for (int i = 0; i=vmax) { vmax=tmp; imax=i; } + } + if (j!=imax) { + cimg_forX(*this,k) cimg::swap((*this)(k,imax),(*this)(k,j)); + d =!d; + vv[imax] = vv[j]; + } + indx[j] = (t)imax; + if ((*this)(j,j)==0) (*this)(j,j) = (T)1e-20; + if (j + static CImg dijkstra(const tf& distance, const unsigned int nb_nodes, + const unsigned int starting_node, const unsigned int ending_node, + CImg& previous_node) { + if (starting_node>=nb_nodes) + throw CImgArgumentException("CImg<%s>::dijkstra(): Specified indice of starting node %u is higher " + "than number of nodes %u.", + pixel_type(),starting_node,nb_nodes); + CImg dist(1,nb_nodes,1,1,cimg::type::max()); + dist(starting_node) = 0; + previous_node.assign(1,nb_nodes,1,1,(t)-1); + previous_node(starting_node) = (t)starting_node; + CImg Q(nb_nodes); + cimg_forX(Q,u) Q(u) = u; + cimg::swap(Q(starting_node),Q(0)); + unsigned int sizeQ = nb_nodes; + while (sizeQ) { + // Update neighbors from minimal vertex + const unsigned int umin = Q(0); + if (umin==ending_node) sizeQ = 0; + else { + const T dmin = dist(umin); + const T infty = cimg::type::max(); + for (unsigned int q = 1; qdist(Q(left))) || + (rightdist(Q(right)));) { + if (right + static CImg dijkstra(const tf& distance, const unsigned int nb_nodes, + const unsigned int starting_node, const unsigned int ending_node=~0U) { + CImg foo; + return dijkstra(distance,nb_nodes,starting_node,ending_node,foo); + } + + //! Return minimal path in a graph, using the Dijkstra algorithm. + /** + \param starting_node Indice of the starting node. + \param ending_node Indice of the ending node. + \param previous_node Array that gives the previous node indice in the path to the starting node + (optional parameter). + \return Array of distances of each node to the starting node. + \note image instance corresponds to the adjacency matrix of the graph. + **/ + template + CImg& dijkstra(const unsigned int starting_node, const unsigned int ending_node, + CImg& previous_node) { + return get_dijkstra(starting_node,ending_node,previous_node).move_to(*this); + } + + //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance. + template + CImg get_dijkstra(const unsigned int starting_node, const unsigned int ending_node, + CImg& previous_node) const { + if (_width!=_height || _depth!=1 || _spectrum!=1) + throw CImgInstanceException(_cimg_instance + "dijkstra(): Instance is not a graph adjacency matrix.", + cimg_instance); + + return dijkstra(*this,_width,starting_node,ending_node,previous_node); + } + + //! Return minimal path in a graph, using the Dijkstra algorithm. + CImg& dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) { + return get_dijkstra(starting_node,ending_node).move_to(*this); + } + + //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance. + CImg get_dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) const { + CImg foo; + return get_dijkstra(starting_node,ending_node,foo); + } + + //! Return an image containing the ascii codes of the specified string. + /** + \param str input C-string to encode as an image. + \param is_last_zero Tells if the ending \c '0' character appear in the resulting image. + **/ + static CImg string(const char *const str, const bool is_last_zero=true, const bool is_shared=false) { + if (!str) return CImg(); + return CImg(str,(unsigned int)std::strlen(str)+(is_last_zero?1:0),1,1,1,is_shared); + } + + //! Return a \c 1x1 image containing specified value. + /** + \param a0 First vector value. + **/ + static CImg vector(const T& a0) { + CImg r(1,1); + r[0] = a0; + return r; + } + + //! Return a \c 1x2 image containing specified values. + /** + \param a0 First vector value. + \param a1 Second vector value. + **/ + static CImg vector(const T& a0, const T& a1) { + CImg r(1,2); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; + return r; + } + + //! Return a \c 1x3 image containing specified values. + /** + \param a0 First vector value. + \param a1 Second vector value. + \param a2 Third vector value. + **/ + static CImg vector(const T& a0, const T& a1, const T& a2) { + CImg r(1,3); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; + return r; + } + + //! Return a \c 1x4 image containing specified values. + /** + \param a0 First vector value. + \param a1 Second vector value. + \param a2 Third vector value. + \param a3 Fourth vector value. + **/ + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3) { + CImg r(1,4); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + return r; + } + + //! Return a \c 1x5 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) { + CImg r(1,5); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4; + return r; + } + + //! Return a \c 1x6 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) { + CImg r(1,6); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4; *(ptr++) = a5; + return r; + } + + //! Return a \c 1x7 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6) { + CImg r(1,7); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; + return r; + } + + //! Return a \c 1x8 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7) { + CImg r(1,8); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + return r; + } + + //! Return a \c 1x9 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8) { + CImg r(1,9); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; + return r; + } + + //! Return a \c 1x10 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9) { + CImg r(1,10); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; + return r; + } + + //! Return a \c 1x11 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10) { + CImg r(1,11); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; + return r; + } + + //! Return a \c 1x12 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10, const T& a11) { + CImg r(1,12); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; + return r; + } + + //! Return a \c 1x13 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10, const T& a11, + const T& a12) { + CImg r(1,13); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; + *(ptr++) = a12; + return r; + } + + //! Return a \c 1x14 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10, const T& a11, + const T& a12, const T& a13) { + CImg r(1,14); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; + *(ptr++) = a12; *(ptr++) = a13; + return r; + } + + //! Return a \c 1x15 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10, const T& a11, + const T& a12, const T& a13, const T& a14) { + CImg r(1,15); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; + *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; + return r; + } + + //! Return a \c 1x16 image containing specified values. + static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10, const T& a11, + const T& a12, const T& a13, const T& a14, const T& a15) { + CImg r(1,16); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; + *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; *(ptr++) = a15; + return r; + } + + //! Return a 1x1 matrix containing specified coefficients. + /** + \param a0 First matrix value. + \note Equivalent to vector(const T&). + **/ + static CImg matrix(const T& a0) { + return vector(a0); + } + + //! Return a 2x2 matrix containing specified coefficients. + /** + \param a0 First matrix value. + \param a1 Second matrix value. + \param a2 Third matrix value. + \param a3 Fourth matrix value. + **/ + static CImg matrix(const T& a0, const T& a1, + const T& a2, const T& a3) { + CImg r(2,2); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; + *(ptr++) = a2; *(ptr++) = a3; + return r; + } + + //! Return a 3x3 matrix containing specified coefficients. + /** + \param a0 First matrix value. + \param a1 Second matrix value. + \param a2 Third matrix value. + \param a3 Fourth matrix value. + \param a4 Fifth matrix value. + \param a5 Sixth matrix value. + \param a6 Seventh matrix value. + \param a7 Eighth matrix value. + \param a8 Nineth matrix value. + **/ + static CImg matrix(const T& a0, const T& a1, const T& a2, + const T& a3, const T& a4, const T& a5, + const T& a6, const T& a7, const T& a8) { + CImg r(3,3); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; + *(ptr++) = a3; *(ptr++) = a4; *(ptr++) = a5; + *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8; + return r; + } + + //! Return a 4x4 matrix containing specified coefficients. + static CImg matrix(const T& a0, const T& a1, const T& a2, const T& a3, + const T& a4, const T& a5, const T& a6, const T& a7, + const T& a8, const T& a9, const T& a10, const T& a11, + const T& a12, const T& a13, const T& a14, const T& a15) { + CImg r(4,4); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; + *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; + *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; + *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; *(ptr++) = a15; + return r; + } + + //! Return a 5x5 matrix containing specified coefficients. + static CImg matrix(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, + const T& a5, const T& a6, const T& a7, const T& a8, const T& a9, + const T& a10, const T& a11, const T& a12, const T& a13, const T& a14, + const T& a15, const T& a16, const T& a17, const T& a18, const T& a19, + const T& a20, const T& a21, const T& a22, const T& a23, const T& a24) { + CImg r(5,5); T *ptr = r._data; + *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4; + *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8; *(ptr++) = a9; + *(ptr++) = a10; *(ptr++) = a11; *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; + *(ptr++) = a15; *(ptr++) = a16; *(ptr++) = a17; *(ptr++) = a18; *(ptr++) = a19; + *(ptr++) = a20; *(ptr++) = a21; *(ptr++) = a22; *(ptr++) = a23; *(ptr++) = a24; + return r; + } + + //! Return a 1x1 symmetric matrix containing specified coefficients. + /** + \param a0 First matrix value. + \note Equivalent to vector(const T&). + **/ + static CImg tensor(const T& a0) { + return matrix(a0); + } + + //! Return a 2x2 symmetric matrix tensor containing specified coefficients. + static CImg tensor(const T& a0, const T& a1, const T& a2) { + return matrix(a0,a1,a1,a2); + } + + //! Return a 3x3 symmetric matrix containing specified coefficients. + static CImg tensor(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) { + return matrix(a0,a1,a2,a1,a3,a4,a2,a4,a5); + } + + //! Return a 1x1 diagonal matrix containing specified coefficients. + static CImg diagonal(const T& a0) { + return matrix(a0); + } + + //! Return a 2x2 diagonal matrix containing specified coefficients. + static CImg diagonal(const T& a0, const T& a1) { + return matrix(a0,0,0,a1); + } + + //! Return a 3x3 diagonal matrix containing specified coefficients. + static CImg diagonal(const T& a0, const T& a1, const T& a2) { + return matrix(a0,0,0,0,a1,0,0,0,a2); + } + + //! Return a 4x4 diagonal matrix containing specified coefficients. + static CImg diagonal(const T& a0, const T& a1, const T& a2, const T& a3) { + return matrix(a0,0,0,0,0,a1,0,0,0,0,a2,0,0,0,0,a3); + } + + //! Return a 5x5 diagonal matrix containing specified coefficients. + static CImg diagonal(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) { + return matrix(a0,0,0,0,0,0,a1,0,0,0,0,0,a2,0,0,0,0,0,a3,0,0,0,0,0,a4); + } + + //! Return a NxN identity matrix. + /** + \param N Dimension of the matrix. + **/ + static CImg identity_matrix(const unsigned int N) { + CImg res(N,N,1,1,0); + cimg_forX(res,x) res(x,x) = 1; + return res; + } + + //! Return a N-numbered sequence vector from \p a0 to \p a1. + /** + \param N Size of the resulting vector. + \param a0 Starting value of the sequence. + \param a1 Ending value of the sequence. + **/ + static CImg sequence(const unsigned int N, const T a0, const T a1) { + if (N) return CImg(1,N).sequence(a0,a1); + return CImg(); + } + + //! Return a 3x3 rotation matrix along the (x,y,z)-axis with an angle w. + /** + \param x X-coordinate of the rotation axis, or first quaternion coordinate. + \param y Y-coordinate of the rotation axis, or second quaternion coordinate. + \param z Z-coordinate of the rotation axis, or third quaternion coordinate. + \param w Angle of the rotation axis, or fourth quaternion coordinate. + \param is_quaternion Tell is the four arguments denotes a set { axis + angle } or a quaternion. + **/ + static CImg rotation_matrix(const float x, const float y, const float z, const float w, + const bool is_quaternion=false) { + float X,Y,Z,W; + if (!is_quaternion) { + const float norm = (float)std::sqrt(x*x + y*y + z*z), + nx = norm>0?x/norm:0, + ny = norm>0?y/norm:0, + nz = norm>0?z/norm:1, + nw = norm>0?w:0, + sina = (float)std::sin(nw/2), + cosa = (float)std::cos(nw/2); + X = nx*sina; + Y = ny*sina; + Z = nz*sina; + W = cosa; + } else { + const float norm = (float)std::sqrt(x*x + y*y + z*z + w*w); + if (norm>0) { X = x/norm; Y = y/norm; Z = z/norm; W = w/norm; } + else { X = Y = Z = 0; W = 1; } + } + const float xx = X*X, xy = X*Y, xz = X*Z, xw = X*W, yy = Y*Y, yz = Y*Z, yw = Y*W, zz = Z*Z, zw = Z*W; + return CImg::matrix((T)(1-2*(yy+zz)), (T)(2*(xy+zw)), (T)(2*(xz-yw)), + (T)(2*(xy-zw)), (T)(1-2*(xx+zz)), (T)(2*(yz+xw)), + (T)(2*(xz+yw)), (T)(2*(yz-xw)), (T)(1-2*(xx+yy))); + } + + //@} + //----------------------------------- + // + //! \name Value Manipulation + //@{ + //----------------------------------- + + //! Fill all pixel values with specified value. + /** + \param val Fill value. + **/ + CImg& fill(const T val) { + if (is_empty()) return *this; + if (val && sizeof(T)!=1) cimg_for(*this,ptrd,T) *ptrd = val; + else std::memset(_data,(int)val,sizeof(T)*size()); + return *this; + } + + //! Fill all pixel values with specified value \newinstance. + CImg get_fill(const T val) const { + return CImg(_width,_height,_depth,_spectrum).fill(val); + } + + //! Fill sequentially all pixel values with specified values. + /** + \param val0 First fill value. + \param val1 Second fill value. + **/ + CImg& fill(const T val0, const T val1) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-1; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-2; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-3; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-4; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-5; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-6; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, + const T val6) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, + const T val6, const T val7) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-7; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-8; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-9; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-10; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-11; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, + val11); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-12; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, + val11,val12); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, + const T val13) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-13; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, + const T val13) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, + val11,val12,val13); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, + const T val13, const T val14) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-14; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, + const T val13, const T val14) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, + val11,val12,val13,val14); + } + + //! Fill sequentially all pixel values with specified values \overloading. + CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, + const T val13, const T val14, const T val15) { + if (is_empty()) return *this; + T *ptrd, *ptre = end()-15; + for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, + const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, + const T val13, const T val14, const T val15) const { + return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, + val11,val12,val13,val14,val15); + } + + //! Fill sequentially pixel values according to a given expression. + /** + \param expression C-string describing a math formula, or a list of values. + \param repeat_flag In case a list of values is provided, tells if this list must be repeated for the filling. + **/ + CImg& fill(const char *const expression, const bool repeat_flag) { + if (is_empty() || !expression || !*expression) return *this; + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { // Try to fill values according to a formula. + const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; + _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"fill"); + T *ptrd = *expression=='<'?end()-1:_data; + if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) *(ptrd--) = (T)mp(x,y,z,c); + else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) *(ptrd++) = (T)mp(x,y,z,c); + else { +#ifdef cimg_use_openmp + if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) +#pragma omp parallel + { + _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; +#pragma omp for collapse(3) + cimg_forYZC(*this,y,z,c) { + T *ptrd = data(0,y,z,c); + cimg_forX(*this,x) *ptrd++ = (T)lmp(x,y,z,c); + } + } + else +#endif + cimg_forXYZC(*this,x,y,z,c) *(ptrd++) = (T)mp(x,y,z,c); + } + } catch (CImgException& e) { // If failed, try to recognize a list of values. + char item[16384] = { 0 }, sep = 0; + const char *nexpression = expression; + unsigned long nb = 0; + const unsigned long siz = size(); + T *ptrd = _data; + for (double val = 0; *nexpression && nb0 && std::sscanf(item,"%lf",&val)==1) { + nexpression+=std::strlen(item) + (err>1?1:0); + *(ptrd++) = (T)val; + } else break; + } + cimg::exception_mode() = omode; + if (nb get_fill(const char *const values, const bool repeat_values) const { + return (+*this).fill(values,repeat_values); + } + + //! Fill sequentially pixel values according to the values found in another image. + /** + \param values Image containing the values used for the filling. + \param repeat_values In case there are less values than necessary in \c values, tells if these values must be + repeated for the filling. + **/ + template + CImg& fill(const CImg& values, const bool repeat_values=true) { + if (is_empty() || !values) return *this; + T *ptrd = _data, *ptre = ptrd + size(); + for (t *ptrs = values._data, *ptrs_end = ptrs + values.size(); ptrs + CImg get_fill(const CImg& values, const bool repeat_values=true) const { + return repeat_values?CImg(_width,_height,_depth,_spectrum).fill(values,repeat_values): + (+*this).fill(values,repeat_values); + } + + //! Fill pixel values along the X-axis at a specified pixel position. + /** + \param y Y-coordinate of the filled column. + \param z Z-coordinate of the filled column. + \param c C-coordinate of the filled column. + \param a0 First fill value. + **/ + CImg& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const int a0, ...) { +#define _cimg_fill1(x,y,z,c,off,siz,t) { \ + va_list ap; va_start(ap,a0); T *ptrd = data(x,y,z,c); *ptrd = (T)a0; \ + for (unsigned long k = 1; k& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const double a0, ...) { + if (y<_height && z<_depth && c<_spectrum) _cimg_fill1(0,y,z,c,1,_width,double); + return *this; + } + + //! Fill pixel values along the Y-axis at a specified pixel position. + /** + \param x X-coordinate of the filled row. + \param z Z-coordinate of the filled row. + \param c C-coordinate of the filled row. + \param a0 First fill value. + **/ + CImg& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const int a0, ...) { + if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,int); + return *this; + } + + //! Fill pixel values along the Y-axis at a specified pixel position \overloading. + CImg& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const double a0, ...) { + if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,double); + return *this; + } + + //! Fill pixel values along the Z-axis at a specified pixel position. + /** + \param x X-coordinate of the filled slice. + \param y Y-coordinate of the filled slice. + \param c C-coordinate of the filled slice. + \param a0 First fill value. + **/ + CImg& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const int a0, ...) { + const unsigned long wh = (unsigned long)_width*_height; + if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,int); + return *this; + } + + //! Fill pixel values along the Z-axis at a specified pixel position \overloading. + CImg& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const double a0, ...) { + const unsigned long wh = (unsigned long)_width*_height; + if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,double); + return *this; + } + + //! Fill pixel values along the C-axis at a specified pixel position. + /** + \param x X-coordinate of the filled channel. + \param y Y-coordinate of the filled channel. + \param z Z-coordinate of the filled channel. + \param a0 First filling value. + **/ + CImg& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const int a0, ...) { + const unsigned long whd = (unsigned long)_width*_height*_depth; + if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,int); + return *this; + } + + //! Fill pixel values along the C-axis at a specified pixel position \overloading. + CImg& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const double a0, ...) { + const unsigned long whd = (unsigned long)_width*_height*_depth; + if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,double); + return *this; + } + + //! Discard specified value in the image buffer. + /** + \param value Value to discard. + \note Discarded values will change the image geometry, so the resulting image + is returned as a one-column vector. + **/ + CImg& discard(const T value) { + return get_discard(value).move_to(*this); + } + + //! Discard specified value in the image buffer \newinstance. + CImg get_discard(const T value) const { + CImg res(1,size()); + T *pd = res._data; + for (const T *ps = _data, *const pse = end(); ps(); + return res.resize(1,pd-res._data,1,1,-1); + } + + //! Discard specified sequence of values in the image buffer. + /** + \param values Sequence of values to discard. + \note Discarded values will change the image geometry, so the resulting image + is returned as a one-column vector. + **/ + template + CImg& discard(const CImg& values) { + return get_discard(values).move_to(*this); + } + + //! Discard specified sequence of values in the image buffer \newinstance. + template + CImg get_discard(const CImg& values) const { + if (!values) return *this; + if (values.size()==1) return get_discard(*values); + CImg res(1,size()); + T *pd = res._data; + const t *const pve = values.end(); + for (const T *ps = _data, *const pse = end(); ps(); + return res.resize(1,pd-res._data,1,1,-1); + } + + //! Invert endianness of all pixel values. + /** + **/ + CImg& invert_endianness() { + cimg::invert_endianness(_data,size()); + return *this; + } + + //! Invert endianness of all pixel values \newinstance. + CImg get_invert_endianness() const { + return (+*this).invert_endianness(); + } + + //! Fill image with random values in specified range. + /** + \param val_min Minimal random value. + \param val_max Maximal random value. + \note Random samples are following a uniform distribution. + **/ + CImg& rand(const T val_min, const T val_max) { + const float delta = (float)val_max - (float)val_min; + cimg_for(*this,ptrd,T) *ptrd = (T)(val_min + cimg::rand()*delta); + return *this; + } + + //! Fill image with random values in specified range \newinstance. + CImg get_rand(const T val_min, const T val_max) const { + return (+*this).rand(val_min,val_max); + } + + //! Round pixel values. + /** + \param y Rounding precision. + \param rounding_type Rounding type. Can be: + - \c -1: Backward. + - \c 0: Nearest. + - \c 1: Forward. + **/ + CImg& round(const double y=1, const int rounding_type=0) { + if (y>0) +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=8192) +#endif + cimg_rof(*this,ptrd,T) *ptrd = cimg::round(*ptrd,y,rounding_type); + return *this; + } + + //! Round pixel values \newinstance. + CImg get_round(const double y=1, const unsigned int rounding_type=0) const { + return (+*this).round(y,rounding_type); + } + + //! Add random noise to pixel values. + /** + \param sigma Amplitude of the random additive noise. If \p sigma<0, it stands for a percentage of the + global value range. + \param noise_type Type of additive noise (can be \p 0=gaussian, \p 1=uniform, \p 2=Salt and Pepper, + \p 3=Poisson or \p 4=Rician). + \return A reference to the modified image instance. + \note + - For Poisson noise (\p noise_type=3), parameter \p sigma is ignored, as Poisson noise only depends on + the image value itself. + - Function \p CImg::get_noise() is also defined. It returns a non-shared modified copy of the image instance. + \par Example + \code + const CImg img("reference.jpg"), res = img.get_noise(40); + (img,res.normalize(0,255)).display(); + \endcode + \image html ref_noise.jpg + **/ + CImg& noise(const double sigma, const unsigned int noise_type=0) { + if (!is_empty()) { + const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); + Tfloat nsigma = (Tfloat)sigma, m = 0, M = 0; + if (nsigma==0 && noise_type!=3) return *this; + if (nsigma<0 || noise_type==2) m = (Tfloat)min_max(M); + if (nsigma<0) nsigma = (Tfloat)(-nsigma*(M-m)/100.0); + switch (noise_type) { + case 0 : { // Gaussian noise + cimg_rof(*this,ptrd,T) { + Tfloat val = (Tfloat)(*ptrd + nsigma*cimg::grand()); + if (val>vmax) val = vmax; + if (valvmax) val = vmax; + if (val::is_float()?1:cimg::type::max()); } + cimg_rof(*this,ptrd,T) if (cimg::rand()*100vmax) val = vmax; + if (val get_noise(const double sigma, const unsigned int noise_type=0) const { + return (+*this).noise(sigma,noise_type); + } + + //! Linearly normalize pixel values. + /** + \param min_value Minimum desired value of the resulting image. + \param max_value Maximum desired value of the resulting image. + \par Example + \code + const CImg img("reference.jpg"), res = img.get_normalize(160,220); + (img,res).display(); + \endcode + \image html ref_normalize2.jpg + **/ + CImg& normalize(const T min_value, const T max_value) { + if (is_empty()) return *this; + const T a = min_value=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (T)((*ptrd-fm)/(fM-fm)*(b-a)+a); + return *this; + } + + //! Linearly normalize pixel values \newinstance. + CImg get_normalize(const T min_value, const T max_value) const { + return CImg(*this,false).normalize((Tfloat)min_value,(Tfloat)max_value); + } + + //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm. + /** + \par Example + \code + const CImg img("reference.jpg"), res = img.get_normalize(); + (img,res.normalize(0,255)).display(); + \endcode + \image html ref_normalize.jpg + **/ + CImg& normalize() { + const unsigned long whd = (unsigned long)_width*_height*_depth; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) +#endif + cimg_forYZ(*this,y,z) { + T *ptrd = data(0,y,z,0); + cimg_forX(*this,x) { + const T *ptrs = ptrd; + float n = 0; + cimg_forC(*this,c) { n+=cimg::sqr((float)*ptrs); ptrs+=whd; } + n = (float)std::sqrt(n); + T *_ptrd = ptrd++; + if (n>0) cimg_forC(*this,c) { *_ptrd = (T)(*_ptrd/n); _ptrd+=whd; } + else cimg_forC(*this,c) { *_ptrd = (T)0; _ptrd+=whd; } + } + } + return *this; + } + + //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm \newinstance. + CImg get_normalize() const { + return CImg(*this,false).normalize(); + } + + //! Compute L2-norm of each multi-valued pixel of the image instance. + /** + \param norm_type Type of computed vector norm (can be \p 0=Linf, \p 1=L1 or \p 2=L2). + \par Example + \code + const CImg img("reference.jpg"), res = img.get_norm(); + (img,res.normalize(0,255)).display(); + \endcode + \image html ref_norm.jpg + **/ + CImg& norm(const int norm_type=2) { + if (_spectrum==1) return abs(); + return get_norm(norm_type).move_to(*this); + } + + //! Compute L2-norm of each multi-valued pixel of the image instance \newinstance. + CImg get_norm(const int norm_type=2) const { + if (is_empty()) return *this; + if (_spectrum==1) return get_abs(); + const unsigned long whd = (unsigned long)_width*_height*_depth; + CImg res(_width,_height,_depth); + switch (norm_type) { + case -1 : { // Linf norm +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) +#endif + cimg_forYZ(*this,y,z) { + const unsigned long off = offset(0,y,z); + const T *ptrs = _data + off; + Tfloat *ptrd = res._data + off; + cimg_forX(*this,x) { + Tfloat n = 0; + const T *_ptrs = ptrs++; + cimg_forC(*this,c) { const Tfloat val = (Tfloat)cimg::abs(*_ptrs); if (val>n) n = val; _ptrs+=whd; } + *(ptrd++) = n; + } + } + } break; + case 1 : { // L1 norm +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) +#endif + cimg_forYZ(*this,y,z) { + const unsigned long off = offset(0,y,z); + const T *ptrs = _data + off; + Tfloat *ptrd = res._data + off; + cimg_forX(*this,x) { + Tfloat n = 0; + const T *_ptrs = ptrs++; + cimg_forC(*this,c) { n+=cimg::abs(*_ptrs); _ptrs+=whd; } + *(ptrd++) = n; + } + } + } break; + default : { // L2 norm +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) +#endif + cimg_forYZ(*this,y,z) { + const unsigned long off = offset(0,y,z); + const T *ptrs = _data + off; + Tfloat *ptrd = res._data + off; + cimg_forX(*this,x) { + Tfloat n = 0; + const T *_ptrs = ptrs++; + cimg_forC(*this,c) { n+=cimg::sqr((Tfloat)*_ptrs); _ptrs+=whd; } + *(ptrd++) = (Tfloat)std::sqrt((Tfloat)n); + } + } + } + } + return res; + } + + //! Cut pixel values in specified range. + /** + \param min_value Minimum desired value of the resulting image. + \param max_value Maximum desired value of the resulting image. + \par Example + \code + const CImg img("reference.jpg"), res = img.get_cut(160,220); + (img,res).display(); + \endcode + \image html ref_cut.jpg + **/ + CImg& cut(const T min_value, const T max_value) { + if (is_empty()) return *this; + const T a = min_value=32768) +#endif + cimg_rof(*this,ptrd,T) *ptrd = (*ptrdb)?b:*ptrd); + return *this; + } + + //! Cut pixel values in specified range \newinstance. + CImg get_cut(const T min_value, const T max_value) const { + return (+*this).cut(min_value,max_value); + } + + //! Uniformly quantize pixel values. + /** + \param nb_levels Number of quantization levels. + \param keep_range Tells if resulting values keep the same range as the original ones. + \par Example + \code + const CImg img("reference.jpg"), res = img.get_quantize(4); + (img,res).display(); + \endcode + \image html ref_quantize.jpg + **/ + CImg& quantize(const unsigned int nb_levels, const bool keep_range=true) { + if (!nb_levels) + throw CImgArgumentException(_cimg_instance + "quantize(): Invalid quantization request with 0 values.", + cimg_instance); + + if (is_empty()) return *this; + Tfloat m, M = (Tfloat)max_min(m), range = M - m; + if (range>0) { + if (keep_range) +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { + const unsigned int val = (unsigned int)((*ptrd-m)*nb_levels/range); + *ptrd = (T)(m + cimg::min(val,nb_levels-1)*range/nb_levels); + } else +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { + const unsigned int val = (unsigned int)((*ptrd-m)*nb_levels/range); + *ptrd = (T)cimg::min(val,nb_levels-1); + } + } + return *this; + } + + //! Uniformly quantize pixel values \newinstance. + CImg get_quantize(const unsigned int n, const bool keep_range=true) const { + return (+*this).quantize(n,keep_range); + } + + //! Threshold pixel values. + /** + \param value Threshold value + \param soft_threshold Tells if soft thresholding must be applied (instead of hard one). + \param strict_threshold Tells if threshold value is strict. + \par Example + \code + const CImg img("reference.jpg"), res = img.get_threshold(128); + (img,res.normalize(0,255)).display(); + \endcode + \image html ref_threshold.jpg + **/ + CImg& threshold(const T value, const bool soft_threshold=false, const bool strict_threshold=false) { + if (is_empty()) return *this; + if (strict_threshold) { + if (soft_threshold) +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { const T v = *ptrd; *ptrd = v>value?(T)(v-value):v<-(float)value?(T)(v+value):(T)0; } + else +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = *ptrd>value?(T)1:(T)0; + } else { + if (soft_threshold) +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=32768) +#endif + cimg_rof(*this,ptrd,T) { + const T v = *ptrd; *ptrd = v>=value?(T)(v-value):v<=-(float)value?(T)(v+value):(T)0; + } + else +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=65536) +#endif + cimg_rof(*this,ptrd,T) *ptrd = *ptrd>=value?(T)1:(T)0; + } + return *this; + } + + //! Threshold pixel values \newinstance. + CImg get_threshold(const T value, const bool soft_threshold=false, const bool strict_threshold=false) const { + return (+*this).threshold(value,soft_threshold,strict_threshold); + } + + //! Compute the histogram of pixel values. + /** + \param nb_levels Number of desired histogram levels. + \param min_value Minimum pixel value considered for the histogram computation. + All pixel values lower than \p min_value will not be counted. + \param max_value Maximum pixel value considered for the histogram computation. + All pixel values higher than \p max_value will not be counted. + \note + - The histogram H of an image I is the 1d function where H(x) counts the number of occurences of the value x + in the image I. + - The resulting histogram is always defined in 1d. Histograms of multi-valued images are not multi-dimensional. + \par Example + \code + const CImg img = CImg("reference.jpg").histogram(256); + img.display_graph(0,3); + \endcode + \image html ref_histogram.jpg + **/ + CImg& histogram(const unsigned int nb_levels, const T min_value, const T max_value) { + return get_histogram(nb_levels,min_value,max_value).move_to(*this); + } + + //! Compute the histogram of pixel values \overloading. + CImg& histogram(const unsigned int nb_levels) { + return get_histogram(nb_levels).move_to(*this); + } + + //! Compute the histogram of pixel values \newinstance. + CImg get_histogram(const unsigned int nb_levels, const T min_value, const T max_value) const { + if (!nb_levels || is_empty()) return CImg(); + T vmin = min_value res(nb_levels,1,1,1,0); + cimg_rof(*this,ptrs,T) { + const T val = *ptrs; + if (val>=vmin && val<=vmax) ++res[val==vmax?nb_levels-1:(unsigned int)((val-vmin)*nb_levels/(vmax-vmin))]; + } + return res; + } + + //! Compute the histogram of pixel values \newinstance. + CImg get_histogram(const unsigned int nb_levels) const { + if (!nb_levels || is_empty()) return CImg(); + T vmax = 0, vmin = min_max(vmax); + return get_histogram(nb_levels,vmin,vmax); + } + + //! Equalize histogram of pixel values. + /** + \param nb_levels Number of histogram levels used for the equalization. + \param min_value Minimum pixel value considered for the histogram computation. + All pixel values lower than \p min_value will not be counted. + \param max_value Maximum pixel value considered for the histogram computation. + All pixel values higher than \p max_value will not be counted. + \par Example + \code + const CImg img("reference.jpg"), res = img.get_equalize(256); + (img,res).display(); + \endcode + \image html ref_equalize.jpg + **/ + CImg& equalize(const unsigned int nb_levels, const T min_value, const T max_value) { + if (!nb_levels || is_empty()) return *this; + T vmin = min_value hist = get_histogram(nb_levels,vmin,vmax); + unsigned long cumul = 0; + cimg_forX(hist,pos) { cumul+=hist[pos]; hist[pos] = cumul; } + if (!cumul) cumul = 1; +#ifdef cimg_use_openmp +#pragma omp parallel for if (size()>=1048576) +#endif + cimg_rof(*this,ptrd,T) { + const int pos = (int)((*ptrd-vmin)*(nb_levels-1)/(vmax-vmin)); + if (pos>=0 && pos<(int)nb_levels) *ptrd = (T)(vmin + (vmax-vmin)*hist[pos]/cumul); + } + return *this; + } + + //! Equalize histogram of pixel values \overloading. + CImg& equalize(const unsigned int nb_levels) { + if (!nb_levels || is_empty()) return *this; + T vmax = 0, vmin = min_max(vmax); + return equalize(nb_levels,vmin,vmax); + } + + //! Equalize histogram of pixel values \newinstance. + CImg get_equalize(const unsigned int nblevels, const T val_min, const T val_max) const { + return (+*this).equalize(nblevels,val_min,val_max); + } + + //! Equalize histogram of pixel values \newinstance. + CImg get_equalize(const unsigned int nblevels) const { + return (+*this).equalize(nblevels); + } + + //! Index multi-valued pixels regarding to a specified colormap. + /** + \param colormap Multi-valued colormap used as the basis for multi-valued pixel indexing. + \param dithering Level of dithering (0=disable, 1=standard level). + \param map_indexes Tell if the values of the resulting image are the colormap indices or the colormap vectors. + \note + - \p img.index(colormap,dithering,1) is equivalent to img.index(colormap,dithering,0).map(colormap). + \par Example + \code + const CImg img("reference.jpg"), colormap(3,1,1,3, 0,128,255, 0,128,255, 0,128,255); + const CImg res = img.get_index(colormap,1,true); + (img,res).display(); + \endcode + \image html ref_index.jpg + **/ + template + CImg& index(const CImg& colormap, const float dithering=1, const bool map_indexes=false) { + return get_index(colormap,dithering,map_indexes).move_to(*this); + } + + //! Index multi-valued pixels regarding to a specified colormap \newinstance. + template + CImg::Tuint> + get_index(const CImg& colormap, const float dithering=1, const bool map_indexes=true) const { + if (colormap._spectrum!=_spectrum) + throw CImgArgumentException(_cimg_instance + "index(): Instance and specified colormap (%u,%u,%u,%u,%p) " + "have incompatible dimensions.", + cimg_instance, + colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data); + + typedef typename CImg::Tuint tuint; + if (is_empty()) return CImg(); + const unsigned long + whd = (unsigned long)_width*_height*_depth, + pwhd = (unsigned long)colormap._width*colormap._height*colormap._depth; + CImg res(_width,_height,_depth,map_indexes?_spectrum:1); + tuint *ptrd = res._data; + if (dithering>0) { // Dithered versions. + const float ndithering = (dithering<0?0:dithering>1?1:dithering)/16; + Tfloat valm = 0, valM = (Tfloat)max_min(valm); + if (valm==valM && valm>=0 && valM<=255) { valm = 0; valM = 255; } + CImg cache = get_crop(-1,0,0,0,_width,1,0,_spectrum-1); + Tfloat *cache_current = cache.data(1,0,0,0), *cache_next = cache.data(1,1,0,0); + const unsigned long cwhd = (unsigned long)cache._width*cache._height*cache._depth; + switch (_spectrum) { + case 1 : { // Optimized for scalars. + cimg_forYZ(*this,y,z) { + if (yvalM?valM:_val0; + Tfloat distmin = cimg::type::max(); const t *ptrmin0 = colormap._data; + for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0valM?valM:_val0, + _val1 = (Tfloat)*ptrs1, val1 = _val1valM?valM:_val1; + Tfloat distmin = cimg::type::max(); const t *ptrmin0 = colormap._data; + for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0valM?valM:_val0, + _val1 = (Tfloat)*ptrs1, val1 = _val1valM?valM:_val1, + _val2 = (Tfloat)*ptrs2, val2 = _val2valM?valM:_val2; + Tfloat distmin = cimg::type::max(); const t *ptrmin0 = colormap._data; + for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd, + *ptrp_end = ptrp1; ptrp0::max(); const t *ptrmin = colormap._data; + for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrpvalM?valM:_val; + dist+=cimg::sqr((*_ptrs=val) - (Tfloat)*_ptrp); _ptrs+=cwhd; _ptrp+=pwhd; + } + if (dist=64 && _height*_depth>=16 && pwhd>=16) +#endif + cimg_forYZ(*this,y,z) { + tuint *ptrd = res.data(0,y,z); + for (const T *ptrs0 = data(0,y,z), *ptrs_end = ptrs0 + _width; ptrs0::max(); const t *ptrmin0 = colormap._data; + for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0=64 && _height*_depth>=16 && pwhd>=16) +#endif + cimg_forYZ(*this,y,z) { + tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd; + for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs_end = ptrs0 + _width; ptrs0::max(); const t *ptrmin0 = colormap._data; + for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0=64 && _height*_depth>=16 && pwhd>=16) +#endif + cimg_forYZ(*this,y,z) { + tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd, *ptrd2 = ptrd1 + whd; + for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs2 = ptrs1 + whd, + *ptrs_end = ptrs0 + _width; ptrs0::max(); const t *ptrmin0 = colormap._data; + for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd, + *ptrp_end = ptrp1; ptrp0=64 && _height*_depth>=16 && pwhd>=16) +#endif + cimg_forYZ(*this,y,z) { + tuint *ptrd = res.data(0,y,z); + for (const T *ptrs = data(0,y,z), *ptrs_end = ptrs + _width; ptrs::max(); const t *ptrmin = colormap._data; + for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrp img("reference.jpg"), + colormap1(3,1,1,3, 0,128,255, 0,128,255, 0,128,255), + colormap2(3,1,1,3, 255,0,0, 0,255,0, 0,0,255), + res = img.get_index(colormap1,0).map(colormap2); + (img,res).display(); + \endcode + \image html ref_map.jpg + **/ + template + CImg& map(const CImg& colormap, const unsigned int boundary_conditions=0) { + return get_map(colormap,boundary_conditions).move_to(*this); + } + + //! Map predefined colormap on the scalar (indexed) image instance \newinstance. + template + CImg get_map(const CImg& colormap, const unsigned int boundary_conditions=0) const { + if (_spectrum!=1 && colormap._spectrum!=1) + throw CImgArgumentException(_cimg_instance + "map(): Instance and specified colormap (%u,%u,%u,%u,%p) " + "have incompatible dimensions.", + cimg_instance, + colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data); + + const unsigned long + whd = (unsigned long)_width*_height*_depth, + pwhd = (unsigned long)colormap._width*colormap._height*colormap._depth; + CImg res(_width,_height,_depth,colormap._spectrum==1?_spectrum:colormap._spectrum); + switch (colormap._spectrum) { + + case 1 : { // Optimized for scalars. + const T *ptrs = _data; + switch (boundary_conditions) { + case 2 : // Periodic boundaries. + cimg_for(res,ptrd,t) { + const unsigned long ind = (unsigned long)*(ptrs++); + *ptrd = colormap[ind%pwhd]; + } break; + case 1 : // Neumann boundaries. + cimg_for(res,ptrd,t) { + const long ind = (long)*(ptrs++); + *ptrd = colormap[ind<0?0:ind>=(long)pwhd?pwhd-1:ind]; + } break; + default : // Dirichlet boundaries. + cimg_for(res,ptrd,t) { + const unsigned long ind = (unsigned long)*(ptrs++); + *ptrd = ind=(long)pwhd?pwhd-1:_ind; + *(ptrd0++) = ptrp0[ind]; *(ptrd1++) = ptrp1[ind]; + } + } break; + default : { // Dirichlet boundaries. + const t *const ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd; + t *ptrd0 = res._data, *ptrd1 = ptrd0 + whd; + for (const T *ptrs = _data, *ptrs_end = ptrs + whd; ptrs=(long)pwhd?pwhd-1:_ind; + *(ptrd0++) = ptrp0[ind]; *(ptrd1++) = ptrp1[ind]; *(ptrd2++) = ptrp2[ind]; + } + } break; + default : { // Dirichlet boundaries. + const t *const ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd; + t *ptrd0 = res._data, *ptrd1 = ptrd0 + whd, *ptrd2 = ptrd1 + whd; + for (const T *ptrs = _data, *ptrs_end = ptrs + whd; ptrs=(long)pwhd?pwhd-1:_ind; + const t *ptrp = colormap._data + ind; + t *_ptrd = ptrd++; cimg_forC(res,c) { *_ptrd = *ptrp; _ptrd+=whd; ptrp+=pwhd; } + } + } break; + default : { // Dirichlet boundaries. + t *ptrd = res._data; + for (const T *ptrs = _data, *ptrs_end = ptrs + whd; ptrs& label(const bool is_high_connectivity=false, const Tfloat tolerance=0) { + return get_label(is_high_connectivity,tolerance).move_to(*this); + } + + //! Label connected components \newinstance. + CImg get_label(const bool is_high_connectivity=false, + const Tfloat tolerance=0) const { + if (is_empty()) return CImg(); + + // Create neighborhood tables. + int dx[13], dy[13], dz[13], nb = 0; + dx[nb]=1; dy[nb] = 0; dz[nb++]=0; + dx[nb]=0; dy[nb] = 1; dz[nb++]=0; + if (is_high_connectivity) { + dx[nb]=1; dy[nb] = 1; dz[nb++]=0; + dx[nb]=1; dy[nb] = -1; dz[nb++]=0; + } + if (_depth>1) { // 3d version. + dx[nb]=0; dy[nb] = 0; dz[nb++]=1; + if (is_high_connectivity) { + dx[nb]=1; dy[nb] = 1; dz[nb++]=-1; + dx[nb]=1; dy[nb] = 0; dz[nb++]=-1; + dx[nb]=1; dy[nb] = -1; dz[nb++]=-1; + dx[nb]=0; dy[nb] = 1; dz[nb++]=-1; + + dx[nb]=0; dy[nb] = 1; dz[nb++]=1; + dx[nb]=1; dy[nb] = -1; dz[nb++]=1; + dx[nb]=1; dy[nb] = 0; dz[nb++]=1; + dx[nb]=1; dy[nb] = 1; dz[nb++]=1; + } + } + return _get_label(nb,dx,dy,dz,tolerance); + } + + //! Label connected components \overloading. + /** + \param connectivity_mask Mask of the neighboring pixels. + \param tolerance Tolerance used to determine if two neighboring pixels belong to the same region. + **/ + template + CImg& label(const CImg& connectivity_mask, const Tfloat tolerance=0) { + return get_label(connectivity_mask,tolerance).move_to(*this); + } + + //! Label connected components \newinstance. + template + CImg get_label(const CImg& connectivity_mask, + const Tfloat tolerance=0) const { + int nb = 0; + cimg_for(connectivity_mask,ptr,t) if (*ptr) ++nb; + CImg dx(nb,1,1,1,0), dy(nb,1,1,1,0), dz(nb,1,1,1,0); + nb = 0; + cimg_forXYZ(connectivity_mask,x,y,z) if ((x || y || z) && + connectivity_mask(x,y,z)) { + dx[nb] = x; dy[nb] = y; dz[nb++] = z; + } + return _get_label(nb,dx,dy,dz,tolerance); + } + + CImg _get_label(const unsigned int nb, const int + *const dx, const int *const dy, const int *const dz, + const Tfloat tolerance) const { + CImg res(_width,_height,_depth,_spectrum); + cimg_forC(*this,c) { + CImg _res = res.get_shared_channel(c); + + // Init label numbers. + unsigned long *ptr = _res.data(); + cimg_foroff(_res,p) *(ptr++) = p; + + // For each neighbour-direction, label. + for (unsigned int n = 0; n& _system_strescape() { +#define cimg_system_strescape(c,s) case c : if (p!=ptrs) CImg(ptrs,(unsigned int)(p-ptrs),1,1,1,false).\ + move_to(list); \ + CImg(s,(unsigned int)std::strlen(s),1,1,1,false).move_to(list); ptrs = p+1; break + CImgList list; + const T *ptrs = _data; + cimg_for(*this,p,T) switch ((int)*p) { + cimg_system_strescape('\\',"\\\\"); + cimg_system_strescape('\"',"\\\""); + cimg_system_strescape('!',"\"\\!\""); + cimg_system_strescape('`',"\\`"); + cimg_system_strescape('$',"\\$"); + } + if (ptrs(ptrs,(unsigned int)(end()-ptrs),1,1,1,false).move_to(list); + return (list>'x').move_to(*this); + } + + //@} + //--------------------------------- + // + //! \name Color Base Management + //@{ + //--------------------------------- + + //! Return colormap \e "default", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_default.jpg + **/ + static const CImg& default_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) { + colormap.assign(1,256,1,3); + for (unsigned int index = 0, r = 16; r<256; r+=32) + for (unsigned int g = 16; g<256; g+=32) + for (unsigned int b = 32; b<256; b+=64) { + colormap(0,index,0) = (Tuchar)r; + colormap(0,index,1) = (Tuchar)g; + colormap(0,index++,2) = (Tuchar)b; + } + } + cimg::mutex(8,0); + return colormap; + } + + //! Return colormap \e "HSV", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_hsv.jpg + **/ + static const CImg& HSV_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) { + CImg tmp(1,256,1,3,1); + tmp.get_shared_channel(0).sequence(0,359); + colormap = tmp.HSVtoRGB(); + } + cimg::mutex(8,0); + return colormap; + } + + //! Return colormap \e "lines", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_lines.jpg + **/ + static const CImg& lines_LUT256() { + static const unsigned char pal[] = { + 217,62,88,75,1,237,240,12,56,160,165,116,1,1,204,2,15,248,148,185,133,141,46,246,222,116,16,5,207,226, + 17,114,247,1,214,53,238,0,95,55,233,235,109,0,17,54,33,0,90,30,3,0,94,27,19,0,68,212,166,130,0,15,7,119, + 238,2,246,198,0,3,16,10,13,2,25,28,12,6,2,99,18,141,30,4,3,140,12,4,30,233,7,10,0,136,35,160,168,184,20, + 233,0,1,242,83,90,56,180,44,41,0,6,19,207,5,31,214,4,35,153,180,75,21,76,16,202,218,22,17,2,136,71,74, + 81,251,244,148,222,17,0,234,24,0,200,16,239,15,225,102,230,186,58,230,110,12,0,7,129,249,22,241,37,219, + 1,3,254,210,3,212,113,131,197,162,123,252,90,96,209,60,0,17,0,180,249,12,112,165,43,27,229,77,40,195,12, + 87,1,210,148,47,80,5,9,1,137,2,40,57,205,244,40,8,252,98,0,40,43,206,31,187,0,180,1,69,70,227,131,108,0, + 223,94,228,35,248,243,4,16,0,34,24,2,9,35,73,91,12,199,51,1,249,12,103,131,20,224,2,70,32, + 233,1,165,3,8,154,246,233,196,5,0,6,183,227,247,195,208,36,0,0,226,160,210,198,69,153,210,1,23,8,192,2,4, + 137,1,0,52,2,249,241,129,0,0,234,7,238,71,7,32,15,157,157,252,158,2,250,6,13,30,11,162,0,199,21,11,27,224, + 4,157,20,181,111,187,218,3,0,11,158,230,196,34,223,22,248,135,254,210,157,219,0,117,239,3,255,4,227,5,247, + 11,4,3,188,111,11,105,195,2,0,14,1,21,219,192,0,183,191,113,241,1,12,17,248,0,48,7,19,1,254,212,0,239,246, + 0,23,0,250,165,194,194,17,3,253,0,24,6,0,141,167,221,24,212,2,235,243,0,0,205,1,251,133,204,28,4,6,1,10, + 141,21,74,12,236,254,228,19,1,0,214,1,186,13,13,6,13,16,27,209,6,216,11,207,251,59,32,9,155,23,19,235,143, + 116,6,213,6,75,159,23,6,0,228,4,10,245,249,1,7,44,234,4,102,174,0,19,239,103,16,15,18,8,214,22,4,47,244, + 255,8,0,251,173,1,212,252,250,251,252,6,0,29,29,222,233,246,5,149,0,182,180,13,151,0,203,183,0,35,149,0, + 235,246,254,78,9,17,203,73,11,195,0,3,5,44,0,0,237,5,106,6,130,16,214,20,168,247,168,4,207,11,5,1,232,251, + 129,210,116,231,217,223,214,27,45,38,4,177,186,249,7,215,172,16,214,27,249,230,236,2,34,216,217,0,175,30, + 243,225,244,182,20,212,2,226,21,255,20,0,2,13,62,13,191,14,76,64,20,121,4,118,0,216,1,147,0,2,210,1,215, + 95,210,236,225,184,46,0,248,24,11,1,9,141,250,243,9,221,233,160,11,147,2,55,8,23,12,253,9,0,54,0,231,6,3, + 141,8,2,246,9,180,5,11,8,227,8,43,110,242,1,130,5,97,36,10,6,219,86,133,11,108,6,1,5,244,67,19,28,0,174, + 154,16,127,149,252,188,196,196,228,244,9,249,0,0,0,37,170,32,250,0,73,255,23,3,224,234,38,195,198,0,255,87, + 33,221,174,31,3,0,189,228,6,153,14,144,14,108,197,0,9,206,245,254,3,16,253,178,248,0,95,125,8,0,3,168,21, + 23,168,19,50,240,244,185,0,1,144,10,168,31,82,1,13 }; + static const CImg colormap(pal,1,256,1,3,false); + return colormap; + } + + //! Return colormap \e "hot", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_hot.jpg + **/ + static const CImg& hot_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) { + colormap.assign(1,4,1,3,0); + colormap[1] = colormap[2] = colormap[3] = colormap[6] = colormap[7] = colormap[11] = 255; + colormap.resize(1,256,1,3,3); + } + cimg::mutex(8,0); + return colormap; + } + + //! Return colormap \e "cool", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_cool.jpg + **/ + static const CImg& cool_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) colormap.assign(1,2,1,3).fill(0,255,255,0,255,255).resize(1,256,1,3,3); + cimg::mutex(8,0); + return colormap; + } + + //! Return colormap \e "jet", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_jet.jpg + **/ + static const CImg& jet_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) { + colormap.assign(1,4,1,3,0); + colormap[2] = colormap[3] = colormap[5] = colormap[6] = colormap[8] = colormap[9] = 255; + colormap.resize(1,256,1,3,3); + } + cimg::mutex(8,0); + return colormap; + } + + //! Return colormap \e "flag", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_flag.jpg + **/ + static const CImg& flag_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) { + colormap.assign(1,4,1,3,0); + colormap[0] = colormap[1] = colormap[5] = colormap[9] = colormap[10] = 255; + colormap.resize(1,256,1,3,0,2); + } + cimg::mutex(8,0); + return colormap; + } + + //! Return colormap \e "cube", containing 256 colors entries in RGB. + /** + \return The following \c 256x1x1x3 colormap is returned: + \image html ref_colormap_cube.jpg + **/ + static const CImg& cube_LUT256() { + static CImg colormap; + cimg::mutex(8); + if (!colormap) { + colormap.assign(1,8,1,3,0); + colormap[1] = colormap[3] = colormap[5] = colormap[7] = + colormap[10] = colormap[11] = colormap[12] = colormap[13] = + colormap[20] = colormap[21] = colormap[22] = colormap[23] = 255; + colormap.resize(1,256,1,3,3); + } + cimg::mutex(8,0); + return colormap; + } + + //! Convert pixel values from sRGB to RGB color spaces. + CImg& sRGBtoRGB() { + cimg_for(*this,ptr,T) { + const Tfloat + sval = (Tfloat)*ptr, + nsval = (sval<0?0:sval>255?255:sval)/255, + val = (Tfloat)(nsval<=0.04045f?nsval/12.92f:std::pow((nsval+0.055f)/(1.055f),2.4f)); + *ptr = (T)(val*255); + } + return *this; + } + + //! Convert pixel values from sRGB to RGB color spaces \newinstance. + CImg get_sRGBtoRGB() const { + return CImg(*this,false).sRGBtoRGB(); + } + + //! Convert pixel values from RGB to sRGB color spaces. + CImg& RGBtosRGB() { + cimg_for(*this,ptr,T) { + const Tfloat + val = (Tfloat)*ptr, + nval = (val<0?0:val>255?255:val)/255, + sval = (Tfloat)(nval<=0.0031308f?nval*12.92f:1.055f*std::pow(nval,0.416667f)-0.055f); + *ptr = (T)(sval*255); + } + return *this; + } + + //! Convert pixel values from RGB to sRGB color spaces \newinstance. + CImg get_RGBtosRGB() const { + return CImg(*this,false).RGBtosRGB(); + } + + //! Convert pixel values from RGB to HSV color spaces. + CImg& RGBtoHSV() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoHSV(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1, + G = (Tfloat)*p2, + B = (Tfloat)*p3, + nR = (R<0?0:(R>255?255:R))/255, + nG = (G<0?0:(G>255?255:G))/255, + nB = (B<0?0:(B>255?255:B))/255, + m = cimg::min(nR,nG,nB), + M = cimg::max(nR,nG,nB); + Tfloat H = 0, S = 0; + if (M!=m) { + const Tfloat + f = (nR==m)?(nG-nB):((nG==m)?(nB-nR):(nR-nG)), + i = (Tfloat)((nR==m)?3:((nG==m)?5:1)); + H = (i-f/(M-m)); + if (H>=6) H-=6; + H*=60; + S = (M-m)/M; + } + *(p1++) = (T)H; + *(p2++) = (T)S; + *(p3++) = (T)M; + } + return *this; + } + + //! Convert pixel values from RGB to HSV color spaces \newinstance. + CImg get_RGBtoHSV() const { + return CImg(*this,false).RGBtoHSV(); + } + + //! Convert pixel values from HSV to RGB color spaces. + CImg& HSVtoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "HSVtoRGB(): Instance is not a HSV image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + Tfloat + H = (Tfloat)*p1, + S = (Tfloat)*p2, + V = (Tfloat)*p3, + R = 0, G = 0, B = 0; + if (H==0 && S==0) R = G = B = V; + else { + H/=60; + const int i = (int)std::floor(H); + const Tfloat + f = (i&1)?(H - i):(1 - H + i), + m = V*(1 - S), + n = V*(1 - S*f); + switch (i) { + case 6 : + case 0 : R = V; G = n; B = m; break; + case 1 : R = n; G = V; B = m; break; + case 2 : R = m; G = V; B = n; break; + case 3 : R = m; G = n; B = V; break; + case 4 : R = n; G = m; B = V; break; + case 5 : R = V; G = m; B = n; break; + } + } + R*=255; G*=255; B*=255; + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from HSV to RGB color spaces \newinstance. + CImg get_HSVtoRGB() const { + return CImg(*this,false).HSVtoRGB(); + } + + //! Convert pixel values from RGB to HSL color spaces. + CImg& RGBtoHSL() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoHSL(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1, + G = (Tfloat)*p2, + B = (Tfloat)*p3, + nR = (R<0?0:(R>255?255:R))/255, + nG = (G<0?0:(G>255?255:G))/255, + nB = (B<0?0:(B>255?255:B))/255, + m = cimg::min(nR,nG,nB), + M = cimg::max(nR,nG,nB), + L = (m + M)/2; + Tfloat H = 0, S = 0; + if (M==m) H = S = 0; + else { + const Tfloat + f = (nR==m)?(nG-nB):((nG==m)?(nB-nR):(nR-nG)), + i = (nR==m)?3.0f:((nG==m)?5.0f:1.0f); + H = (i-f/(M-m)); + if (H>=6) H-=6; + H*=60; + S = (2*L<=1)?((M-m)/(M+m)):((M-m)/(2-M-m)); + } + *(p1++) = (T)H; + *(p2++) = (T)S; + *(p3++) = (T)L; + } + return *this; + } + + //! Convert pixel values from RGB to HSL color spaces \newinstance. + CImg get_RGBtoHSL() const { + return CImg< Tfloat>(*this,false).RGBtoHSL(); + } + + //! Convert pixel values from HSL to RGB color spaces. + CImg& HSLtoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "HSLtoRGB(): Instance is not a HSL image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + H = (Tfloat)*p1, + S = (Tfloat)*p2, + L = (Tfloat)*p3, + q = 2*L<1?L*(1+S):(L+S-L*S), + p = 2*L-q, + h = H/360, + tr = h + 1.0f/3, + tg = h, + tb = h - 1.0f/3, + ntr = tr<0?tr+1:(tr>1?tr-1:tr), + ntg = tg<0?tg+1:(tg>1?tg-1:tg), + ntb = tb<0?tb+1:(tb>1?tb-1:tb), + R = 255*(6*ntr<1?p+(q-p)*6*ntr:(2*ntr<1?q:(3*ntr<2?p+(q-p)*6*(2.0f/3-ntr):p))), + G = 255*(6*ntg<1?p+(q-p)*6*ntg:(2*ntg<1?q:(3*ntg<2?p+(q-p)*6*(2.0f/3-ntg):p))), + B = 255*(6*ntb<1?p+(q-p)*6*ntb:(2*ntb<1?q:(3*ntb<2?p+(q-p)*6*(2.0f/3-ntb):p))); + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from HSL to RGB color spaces \newinstance. + CImg get_HSLtoRGB() const { + return CImg(*this,false).HSLtoRGB(); + } + + //! Convert pixel values from RGB to HSI color spaces. + CImg& RGBtoHSI() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoHSI(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1, + G = (Tfloat)*p2, + B = (Tfloat)*p3, + nR = (R<0?0:(R>255?255:R))/255, + nG = (G<0?0:(G>255?255:G))/255, + nB = (B<0?0:(B>255?255:B))/255, + m = cimg::min(nR,nG,nB), + theta = (Tfloat)(std::acos(0.5f*((nR-nG)+(nR-nB))/std::sqrt(std::pow(nR-nG,2)+(nR-nB)*(nG-nB)))*180/cimg::PI), + sum = nR + nG + nB; + Tfloat H = 0, S = 0, I = 0; + if (theta>0) H = (nB<=nG)?theta:360-theta; + if (sum>0) S = 1 - 3/sum*m; + I = sum/3; + *(p1++) = (T)H; + *(p2++) = (T)S; + *(p3++) = (T)I; + } + return *this; + } + + //! Convert pixel values from RGB to HSI color spaces \newinstance. + CImg get_RGBtoHSI() const { + return CImg(*this,false).RGBtoHSI(); + } + + //! Convert pixel values from HSI to RGB color spaces. + CImg& HSItoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "HSItoRGB(): Instance is not a HSI image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + Tfloat + H = (Tfloat)*p1, + S = (Tfloat)*p2, + I = (Tfloat)*p3, + a = I*(1-S), + R = 0, G = 0, B = 0; + if (H<120) { + B = a; + R = (Tfloat)(I*(1+S*std::cos(H*cimg::PI/180)/std::cos((60-H)*cimg::PI/180))); + G = 3*I-(R+B); + } else if (H<240) { + H-=120; + R = a; + G = (Tfloat)(I*(1+S*std::cos(H*cimg::PI/180)/std::cos((60-H)*cimg::PI/180))); + B = 3*I-(R+G); + } else { + H-=240; + G = a; + B = (Tfloat)(I*(1+S*std::cos(H*cimg::PI/180)/std::cos((60-H)*cimg::PI/180))); + R = 3*I-(G+B); + } + R*=255; G*=255; B*=255; + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from HSI to RGB color spaces \newinstance. + CImg get_HSItoRGB() const { + return CImg< Tuchar>(*this,false).HSItoRGB(); + } + + //! Convert pixel values from RGB to YCbCr color spaces. + CImg& RGBtoYCbCr() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoYCbCr(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1, + G = (Tfloat)*p2, + B = (Tfloat)*p3, + Y = (66*R + 129*G + 25*B + 128)/256 + 16, + Cb = (-38*R - 74*G + 112*B + 128)/256 + 128, + Cr = (112*R - 94*G - 18*B + 128)/256 + 128; + *(p1++) = (T)(Y<0?0:(Y>255?255:Y)); + *(p2++) = (T)(Cb<0?0:(Cb>255?255:Cb)); + *(p3++) = (T)(Cr<0?0:(Cr>255?255:Cr)); + } + return *this; + } + + //! Convert pixel values from RGB to YCbCr color spaces \newinstance. + CImg get_RGBtoYCbCr() const { + return CImg(*this,false).RGBtoYCbCr(); + } + + //! Convert pixel values from RGB to YCbCr color spaces. + CImg& YCbCrtoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "YCbCrtoRGB(): Instance is not a YCbCr image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + Y = (Tfloat)*p1 - 16, + Cb = (Tfloat)*p2 - 128, + Cr = (Tfloat)*p3 - 128, + R = (298*Y + 409*Cr + 128)/256, + G = (298*Y - 100*Cb - 208*Cr + 128)/256, + B = (298*Y + 516*Cb + 128)/256; + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from RGB to YCbCr color spaces \newinstance. + CImg get_YCbCrtoRGB() const { + return CImg(*this,false).YCbCrtoRGB(); + } + + //! Convert pixel values from RGB to YUV color spaces. + CImg& RGBtoYUV() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoYUV(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1/255, + G = (Tfloat)*p2/255, + B = (Tfloat)*p3/255, + Y = 0.299f*R + 0.587f*G + 0.114f*B; + *(p1++) = (T)Y; + *(p2++) = (T)(0.492f*(B-Y)); + *(p3++) = (T)(0.877*(R-Y)); + } + return *this; + } + + //! Convert pixel values from RGB to YUV color spaces \newinstance. + CImg get_RGBtoYUV() const { + return CImg(*this,false).RGBtoYUV(); + } + + //! Convert pixel values from YUV to RGB color spaces. + CImg& YUVtoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "YUVtoRGB(): Instance is not a YUV image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + Y = (Tfloat)*p1, + U = (Tfloat)*p2, + V = (Tfloat)*p3, + R = (Y + 1.140f*V)*255, + G = (Y - 0.395f*U - 0.581f*V)*255, + B = (Y + 2.032f*U)*255; + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from YUV to RGB color spaces \newinstance. + CImg get_YUVtoRGB() const { + return CImg< Tuchar>(*this,false).YUVtoRGB(); + } + + //! Convert pixel values from RGB to CMY color spaces. + CImg& RGBtoCMY() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoCMY(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1, + G = (Tfloat)*p2, + B = (Tfloat)*p3, + C = 255 - R, + M = 255 - G, + Y = 255 - B; + *(p1++) = (T)(C<0?0:(C>255?255:C)); + *(p2++) = (T)(M<0?0:(M>255?255:M)); + *(p3++) = (T)(Y<0?0:(Y>255?255:Y)); + } + return *this; + } + + //! Convert pixel values from RGB to CMY color spaces \newinstance. + CImg get_RGBtoCMY() const { + return CImg(*this,false).RGBtoCMY(); + } + + //! Convert pixel values from CMY to RGB color spaces. + CImg& CMYtoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "CMYtoRGB(): Instance is not a CMY image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + C = (Tfloat)*p1, + M = (Tfloat)*p2, + Y = (Tfloat)*p3, + R = 255 - C, + G = 255 - M, + B = 255 - Y; + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from CMY to RGB color spaces \newinstance. + CImg get_CMYtoRGB() const { + return CImg(*this,false).CMYtoRGB(); + } + + //! Convert pixel values from CMY to CMYK color spaces. + CImg& CMYtoCMYK() { + return get_CMYtoCMYK().move_to(*this); + } + + //! Convert pixel values from CMY to CMYK color spaces \newinstance. + CImg get_CMYtoCMYK() const { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "CMYtoCMYK(): Instance is not a CMY image.", + cimg_instance); + + CImg res(_width,_height,_depth,4); + const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2); + Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2), *pd4 = res.data(0,0,0,3); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + Tfloat + C = (Tfloat)*(ps1++), + M = (Tfloat)*(ps2++), + Y = (Tfloat)*(ps3++), + K = cimg::min(C,M,Y); + if (K>=255) C = M = Y = 0; + else { const Tfloat K1 = 255 - K; C = 255*(C - K)/K1; M = 255*(M - K)/K1; Y = 255*(Y - K)/K1; } + *(pd1++) = (Tfloat)(C<0?0:(C>255?255:C)); + *(pd2++) = (Tfloat)(M<0?0:(M>255?255:M)); + *(pd3++) = (Tfloat)(Y<0?0:(Y>255?255:Y)); + *(pd4++) = (Tfloat)(K<0?0:(K>255?255:K)); + } + return res; + } + + //! Convert pixel values from CMYK to CMY color spaces. + CImg& CMYKtoCMY() { + return get_CMYKtoCMY().move_to(*this); + } + + //! Convert pixel values from CMYK to CMY color spaces \newinstance. + CImg get_CMYKtoCMY() const { + if (_spectrum!=4) + throw CImgInstanceException(_cimg_instance + "CMYKtoCMY(): Instance is not a CMYK image.", + cimg_instance); + + CImg res(_width,_height,_depth,3); + const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2), *ps4 = data(0,0,0,3); + Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + C = (Tfloat)*(ps1++), + M = (Tfloat)*(ps2++), + Y = (Tfloat)*(ps3++), + K = (Tfloat)*(ps4++), + K1 = 1 - K/255, + nC = C*K1 + K, + nM = M*K1 + K, + nY = Y*K1 + K; + *(pd1++) = (Tfloat)(nC<0?0:(nC>255?255:nC)); + *(pd2++) = (Tfloat)(nM<0?0:(nM>255?255:nM)); + *(pd3++) = (Tfloat)(nY<0?0:(nY>255?255:nY)); + } + return res; + } + + //! Convert pixel values from RGB to XYZ_709 color spaces. + /** + \note Uses the standard D65 white point. + **/ + CImg& RGBtoXYZ() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoXYZ(): Instance is not a RGB image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + R = (Tfloat)*p1/255, + G = (Tfloat)*p2/255, + B = (Tfloat)*p3/255; + *(p1++) = (T)(0.412453f*R + 0.357580f*G + 0.180423f*B); + *(p2++) = (T)(0.212671f*R + 0.715160f*G + 0.072169f*B); + *(p3++) = (T)(0.019334f*R + 0.119193f*G + 0.950227f*B); + } + return *this; + } + + //! Convert pixel values from RGB to XYZ_709 color spaces \newinstance. + CImg get_RGBtoXYZ() const { + return CImg(*this,false).RGBtoXYZ(); + } + + //! Convert pixel values from XYZ_709 to RGB color spaces. + CImg& XYZtoRGB() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "XYZtoRGB(): Instance is not a XYZ image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + X = (Tfloat)*p1*255, + Y = (Tfloat)*p2*255, + Z = (Tfloat)*p3*255, + R = 3.240479f*X - 1.537150f*Y - 0.498535f*Z, + G = -0.969256f*X + 1.875992f*Y + 0.041556f*Z, + B = 0.055648f*X - 0.204043f*Y + 1.057311f*Z; + *(p1++) = (T)(R<0?0:(R>255?255:R)); + *(p2++) = (T)(G<0?0:(G>255?255:G)); + *(p3++) = (T)(B<0?0:(B>255?255:B)); + } + return *this; + } + + //! Convert pixel values from XYZ_709 to RGB color spaces \newinstance. + CImg get_XYZtoRGB() const { + return CImg(*this,false).XYZtoRGB(); + } + + //! Convert pixel values from XYZ_709 to Lab color spaces. + CImg& XYZtoLab() { +#define _cimg_Labf(x) ((x)>=0.008856f?(std::pow(x,(Tfloat)1/3)):(7.787f*(x)+16.0f/116)) + + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "XYZtoLab(): Instance is not a XYZ image.", + cimg_instance); + + const Tfloat + Xn = (Tfloat)(0.412453f + 0.357580f + 0.180423f), + Yn = (Tfloat)(0.212671f + 0.715160f + 0.072169f), + Zn = (Tfloat)(0.019334f + 0.119193f + 0.950227f); + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + X = (Tfloat)*p1, + Y = (Tfloat)*p2, + Z = (Tfloat)*p3, + XXn = X/Xn, YYn = Y/Yn, ZZn = Z/Zn, + fX = (Tfloat)_cimg_Labf(XXn), + fY = (Tfloat)_cimg_Labf(YYn), + fZ = (Tfloat)_cimg_Labf(ZZn); + *(p1++) = (T)cimg::max(0.0f,116*fY - 16); + *(p2++) = (T)(500*(fX - fY)); + *(p3++) = (T)(200*(fY - fZ)); + } + return *this; + } + + //! Convert pixel values from XYZ_709 to Lab color spaces \newinstance. + CImg get_XYZtoLab() const { + return CImg(*this,false).XYZtoLab(); + } + + //! Convert pixel values from Lab to XYZ_709 color spaces. + CImg& LabtoXYZ() { +#define _cimg_Labfi(x) ((x)>=0.206893f?((x)*(x)*(x)):(((x)-16.0f/116)/7.787f)) + + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "LabtoXYZ(): Instance is not a Lab image.", + cimg_instance); + + const Tfloat + Xn = (Tfloat)(0.412453f + 0.357580f + 0.180423f), + Yn = (Tfloat)(0.212671f + 0.715160f + 0.072169f), + Zn = (Tfloat)(0.019334f + 0.119193f + 0.950227f); + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + L = (Tfloat)*p1, + a = (Tfloat)*p2, + b = (Tfloat)*p3, + cY = (L + 16)/116, + Y = (Tfloat)(Yn*_cimg_Labfi(cY)), + pY = (Tfloat)std::pow(Y/Yn,(Tfloat)1/3), + cX = a/500 + pY, + X = Xn*cX*cX*cX, + cZ = pY - b/200, + Z = Zn*cZ*cZ*cZ; + *(p1++) = (T)(X); + *(p2++) = (T)(Y); + *(p3++) = (T)(Z); + } + return *this; + } + + //! Convert pixel values from Lab to XYZ_709 color spaces \newinstance. + CImg get_LabtoXYZ() const { + return CImg(*this,false).LabtoXYZ(); + } + + //! Convert pixel values from XYZ_709 to xyY color spaces. + CImg& XYZtoxyY() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "XYZtoxyY(): Instance is not a XYZ image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + X = (Tfloat)*p1, + Y = (Tfloat)*p2, + Z = (Tfloat)*p3, + sum = (X+Y+Z), + nsum = sum>0?sum:1; + *(p1++) = (T)(X/nsum); + *(p2++) = (T)(Y/nsum); + *(p3++) = (T)Y; + } + return *this; + } + + //! Convert pixel values from XYZ_709 to xyY color spaces \newinstance. + CImg get_XYZtoxyY() const { + return CImg(*this,false).XYZtoxyY(); + } + + //! Convert pixel values from xyY pixels to XYZ_709 color spaces. + CImg& xyYtoXYZ() { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "xyYtoXYZ(): Instance is not a xyY image.", + cimg_instance); + + T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); + for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { + const Tfloat + px = (Tfloat)*p1, + py = (Tfloat)*p2, + Y = (Tfloat)*p3, + ny = py>0?py:1; + *(p1++) = (T)(px*Y/ny); + *(p2++) = (T)Y; + *(p3++) = (T)((1-px-py)*Y/ny); + } + return *this; + } + + //! Convert pixel values from xyY pixels to XYZ_709 color spaces \newinstance. + CImg get_xyYtoXYZ() const { + return CImg(*this,false).xyYtoXYZ(); + } + + //! Convert pixel values from RGB to Lab color spaces. + CImg& RGBtoLab() { + return RGBtoXYZ().XYZtoLab(); + } + + //! Convert pixel values from RGB to Lab color spaces \newinstance. + CImg get_RGBtoLab() const { + return CImg(*this,false).RGBtoLab(); + } + + //! Convert pixel values from Lab to RGB color spaces. + CImg& LabtoRGB() { + return LabtoXYZ().XYZtoRGB(); + } + + //! Convert pixel values from Lab to RGB color spaces \newinstance. + CImg get_LabtoRGB() const { + return CImg(*this,false).LabtoRGB(); + } + + //! Convert pixel values from RGB to xyY color spaces. + CImg& RGBtoxyY() { + return RGBtoXYZ().XYZtoxyY(); + } + + //! Convert pixel values from RGB to xyY color spaces \newinstance. + CImg get_RGBtoxyY() const { + return CImg(*this,false).RGBtoxyY(); + } + + //! Convert pixel values from xyY to RGB color spaces. + CImg& xyYtoRGB() { + return xyYtoXYZ().XYZtoRGB(); + } + + //! Convert pixel values from xyY to RGB color spaces \newinstance. + CImg get_xyYtoRGB() const { + return CImg(*this,false).xyYtoRGB(); + } + + //! Convert pixel values from RGB to CMYK color spaces. + CImg& RGBtoCMYK() { + return RGBtoCMY().CMYtoCMYK(); + } + + //! Convert pixel values from RGB to CMYK color spaces \newinstance. + CImg get_RGBtoCMYK() const { + return CImg(*this,false).RGBtoCMYK(); + } + + //! Convert pixel values from CMYK to RGB color spaces. + CImg& CMYKtoRGB() { + return CMYKtoCMY().CMYtoRGB(); + } + + //! Convert pixel values from CMYK to RGB color spaces \newinstance. + CImg get_CMYKtoRGB() const { + return CImg(*this,false).CMYKtoRGB(); + } + + //! Convert RGB color image to a Bayer-coded scalar image. + /** + \note First (upper-left) pixel if the red component of the pixel color. + **/ + CImg& RGBtoBayer() { + return get_RGBtoBayer().move_to(*this); + } + + //! Convert RGB color image to a Bayer-coded scalar image \newinstance. + CImg get_RGBtoBayer() const { + if (_spectrum!=3) + throw CImgInstanceException(_cimg_instance + "RGBtoBayer(): Instance is not a RGB image.", + cimg_instance); + + CImg res(_width,_height,_depth,1); + const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); + T *ptrd = res._data; + cimg_forXYZ(*this,x,y,z) { + if (y%2) { + if (x%2) *(ptrd++) = *ptr_b; + else *(ptrd++) = *ptr_g; + } else { + if (x%2) *(ptrd++) = *ptr_g; + else *(ptrd++) = *ptr_r; + } + ++ptr_r; ++ptr_g; ++ptr_b; + } + return res; + } + + //! Convert Bayer-coded scalar image to a RGB color image. + CImg& BayertoRGB(const unsigned int interpolation_type=3) { + return get_BayertoRGB(interpolation_type).move_to(*this); + } + + //! Convert Bayer-coded scalar image to a RGB color image \newinstance. + CImg get_BayertoRGB(const unsigned int interpolation_type=3) const { + if (_spectrum!=1) + throw CImgInstanceException(_cimg_instance + "BayertoRGB(): Instance is not a Bayer image.", + cimg_instance); + + CImg res(_width,_height,_depth,3); + CImg_3x3(I,T); + Tuchar *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2); + switch (interpolation_type) { + case 3 : { // Edge-directed + CImg_3x3(R,T); + CImg_3x3(G,T); + CImg_3x3(B,T); + cimg_forXYZ(*this,x,y,z) { + const int _p1x = x?x-1:1, _p1y = y?y-1:1, _n1x = x=2) return 0; + const float a = (float)cimg::PI*x, b = 0.5f*a; + return (float)(x?std::sin(a)*std::sin(b)/(a*b):1); + } + + //! Resize image to new dimensions. + /** + \param size_x Number of columns (new size along the X-axis). + \param size_y Number of rows (new size along the Y-axis). + \param size_z Number of slices (new size along the Z-axis). + \param size_c Number of vector-channels (new size along the C-axis). + \param interpolation_type Method of interpolation: + - -1 = no interpolation: raw memory resizing. + - 0 = no interpolation: additional space is filled according to \p boundary_conditions. + - 1 = nearest-neighbor interpolation. + - 2 = moving average interpolation. + - 3 = linear interpolation. + - 4 = grid interpolation. + - 5 = cubic interpolation. + - 6 = lanczos interpolation. + \param boundary_conditions Border condition type. + \param centering_x Set centering type (only if \p interpolation_type=0). + \param centering_y Set centering type (only if \p interpolation_type=0). + \param centering_z Set centering type (only if \p interpolation_type=0). + \param centering_c Set centering type (only if \p interpolation_type=0). + \note If pd[x,y,z,v]<0, it corresponds to a percentage of the original size (the default value is -100). + **/ + CImg& resize(const int size_x, const int size_y=-100, + const int size_z=-100, const int size_c=-100, + const int interpolation_type=1, const unsigned int boundary_conditions=0, + const float centering_x = 0, const float centering_y = 0, + const float centering_z = 0, const float centering_c = 0) { + if (!size_x || !size_y || !size_z || !size_c) return assign(); + const unsigned int + _sx = (unsigned int)(size_x<0?-size_x*width()/100:size_x), + _sy = (unsigned int)(size_y<0?-size_y*height()/100:size_y), + _sz = (unsigned int)(size_z<0?-size_z*depth()/100:size_z), + _sc = (unsigned int)(size_c<0?-size_c*spectrum()/100:size_c), + sx = _sx?_sx:1, sy = _sy?_sy:1, sz = _sz?_sz:1, sc = _sc?_sc:1; + if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return *this; + if (is_empty()) return assign(sx,sy,sz,sc,(T)0); + if (interpolation_type==-1 && sx*sy*sz*sc==size()) { + _width = sx; _height = sy; _depth = sz; _spectrum = sc; + return *this; + } + return get_resize(sx,sy,sz,sc,interpolation_type,boundary_conditions, + centering_x,centering_y,centering_z,centering_c).move_to(*this); + } + + //! Resize image to new dimensions \newinstance. + CImg get_resize(const int size_x, const int size_y = -100, + const int size_z = -100, const int size_c = -100, + const int interpolation_type=1, const unsigned int boundary_conditions=0, + const float centering_x = 0, const float centering_y = 0, + const float centering_z = 0, const float centering_c = 0) const { + if (centering_x<0 || centering_x>1 || centering_y<0 || centering_y>1 || + centering_z<0 || centering_z>1 || centering_c<0 || centering_c>1) + throw CImgArgumentException(_cimg_instance + "resize(): Specified centering arguments (%g,%g,%g,%g) are outside range [0,1].", + cimg_instance, + centering_x,centering_y,centering_z,centering_c); + + if (!size_x || !size_y || !size_z || !size_c) return CImg(); + const unsigned int + _sx = (unsigned int)(size_x<0?-size_x*width()/100:size_x), + _sy = (unsigned int)(size_y<0?-size_y*height()/100:size_y), + _sz = (unsigned int)(size_z<0?-size_z*depth()/100:size_z), + _sc = (unsigned int)(size_c<0?-size_c*spectrum()/100:size_c), + sx = _sx?_sx:1, sy = _sy?_sy:1, sz = _sz?_sz:1, sc = _sc?_sc:1; + if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return +*this; + if (is_empty()) return CImg(sx,sy,sz,sc,0); + + CImg res; + switch (interpolation_type) { + + // Raw resizing. + // + case -1 : + std::memcpy(res.assign(sx,sy,sz,sc,0)._data,_data,sizeof(T)*cimg::min(size(),sx*sy*sz*sc)); + break; + + // No interpolation. + // + case 0 : { + const int + xc = (int)(centering_x*((int)sx - width())), + yc = (int)(centering_y*((int)sy - height())), + zc = (int)(centering_z*((int)sz - depth())), + cc = (int)(centering_c*((int)sc - spectrum())); + + switch (boundary_conditions) { + case 2 : { // Periodic borders. + res.assign(sx,sy,sz,sc); + const int + x0 = ((int)xc%width()) - width(), + y0 = ((int)yc%height()) - height(), + z0 = ((int)zc%depth()) - depth(), + c0 = ((int)cc%spectrum()) - spectrum(); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=65536) +#endif + for (int c = c0; c<(int)sc; c+=spectrum()) + for (int z = z0; z<(int)sz; z+=depth()) + for (int y = y0; y<(int)sy; y+=height()) + for (int x = x0; x<(int)sx; x+=width()) + res.draw_image(x,y,z,c,*this); + } break; + case 1 : { // Neumann borders. + res.assign(sx,sy,sz,sc).draw_image(xc,yc,zc,cc,*this); + CImg sprite; + if (xc>0) { // X-backward + res.get_crop(xc,yc,zc,cc,xc,yc+height()-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); + for (int x = xc-1; x>=0; --x) res.draw_image(x,yc,zc,cc,sprite); + } + if (xc+width()<(int)sx) { // X-forward + res.get_crop(xc+width()-1,yc,zc,cc,xc+width()-1,yc+height()-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); + for (int x = xc+width(); x<(int)sx; ++x) res.draw_image(x,yc,zc,cc,sprite); + } + if (yc>0) { // Y-backward + res.get_crop(0,yc,zc,cc,sx-1,yc,zc+depth()-1,cc+spectrum()-1).move_to(sprite); + for (int y = yc-1; y>=0; --y) res.draw_image(0,y,zc,cc,sprite); + } + if (yc+height()<(int)sy) { // Y-forward + res.get_crop(0,yc+height()-1,zc,cc,sx-1,yc+height()-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); + for (int y = yc+height(); y<(int)sy; ++y) res.draw_image(0,y,zc,cc,sprite); + } + if (zc>0) { // Z-backward + res.get_crop(0,0,zc,cc,sx-1,sy-1,zc,cc+spectrum()-1).move_to(sprite); + for (int z = zc-1; z>=0; --z) res.draw_image(0,0,z,cc,sprite); + } + if (zc+depth()<(int)sz) { // Z-forward + res.get_crop(0,0,zc+depth()-1,cc,sx-1,sy-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); + for (int z = zc+depth(); z<(int)sz; ++z) res.draw_image(0,0,z,cc,sprite); + } + if (cc>0) { // C-backward + res.get_crop(0,0,0,cc,sx-1,sy-1,sz-1,cc).move_to(sprite); + for (int c = cc-1; c>=0; --c) res.draw_image(0,0,0,c,sprite); + } + if (cc+spectrum()<(int)sc) { // C-forward + res.get_crop(0,0,0,cc+spectrum()-1,sx-1,sy-1,sz-1,cc+spectrum()-1).move_to(sprite); + for (int c = cc+spectrum(); c<(int)sc; ++c) res.draw_image(0,0,0,c,sprite); + } + } break; + default : // Dirichlet borders. + res.assign(sx,sy,sz,sc,0).draw_image(xc,yc,zc,cc,*this); + } + break; + } break; + + // Nearest neighbor interpolation. + // + case 1 : { + res.assign(sx,sy,sz,sc); + CImg off_x(sx), off_y(sy+1), off_z(sz+1), off_c(sc+1); + const unsigned long + wh = (unsigned long)_width*_height, + whd = (unsigned long)_width*_height*_depth, + sxy = (unsigned long)sx*sy, + sxyz = (unsigned long)sx*sy*sz; + if (sx==_width) off_x.fill(1); + else { + unsigned long *poff_x = off_x._data, curr = 0; + cimg_forX(res,x) { const unsigned long old = curr; curr = ((x+1LU)*_width/sx); *(poff_x++) = curr - old; } + } + if (sy==_height) off_y.fill(_width); + else { + unsigned long *poff_y = off_y._data, curr = 0; + cimg_forY(res,y) { + const unsigned long old = curr; + curr = ((y+1LU)*_height/sy); + *(poff_y++) = _width*(curr - old); + } + *poff_y = 0; + } + if (sz==_depth) off_z.fill(wh); + else { + unsigned long *poff_z = off_z._data, curr = 0; + cimg_forZ(res,z) { + const unsigned long old = curr; + curr = ((z+1LU)*_depth/sz); + *(poff_z++) = wh*(curr - old); + } + *poff_z = 0; + } + if (sc==_spectrum) off_c.fill(whd); + else { + unsigned long *poff_c = off_c._data, curr = 0; + cimg_forC(res,c) { + const unsigned long old = curr; + curr = ((c+1LU)*_spectrum/sc); + *(poff_c++) = whd*(curr - old); + } + *poff_c = 0; + } + + T *ptrd = res._data; + const T* ptrc = _data; + const unsigned long *poff_c = off_c._data; + for (unsigned int c = 0; c tmp(sx,_height,_depth,_spectrum,0); + for (unsigned int a = _width*sx, b = _width, c = sx, s = 0, t = 0; a; ) { + const unsigned int d = cimg::min(b,c); + a-=d; b-=d; c-=d; + cimg_forYZC(tmp,y,z,v) tmp(t,y,z,v)+=(Tfloat)(*this)(s,y,z,v)*d; + if (!b) { + cimg_forYZC(tmp,y,z,v) tmp(t,y,z,v)/=_width; + ++t; + b = _width; + } + if (!c) { ++s; c = sx; } + } + tmp.move_to(res); + instance_first = false; + } + if (sy!=_height) { + CImg tmp(sx,sy,_depth,_spectrum,0); + for (unsigned int a = _height*sy, b = _height, c = sy, s = 0, t = 0; a; ) { + const unsigned int d = cimg::min(b,c); + a-=d; b-=d; c-=d; + if (instance_first) + cimg_forXZC(tmp,x,z,v) tmp(x,t,z,v)+=(Tfloat)(*this)(x,s,z,v)*d; + else + cimg_forXZC(tmp,x,z,v) tmp(x,t,z,v)+=(Tfloat)res(x,s,z,v)*d; + if (!b) { + cimg_forXZC(tmp,x,z,v) tmp(x,t,z,v)/=_height; + ++t; + b = _height; + } + if (!c) { ++s; c = sy; } + } + tmp.move_to(res); + instance_first = false; + } + if (sz!=_depth) { + CImg tmp(sx,sy,sz,_spectrum,0); + for (unsigned int a = _depth*sz, b = _depth, c = sz, s = 0, t = 0; a; ) { + const unsigned int d = cimg::min(b,c); + a-=d; b-=d; c-=d; + if (instance_first) + cimg_forXYC(tmp,x,y,v) tmp(x,y,t,v)+=(Tfloat)(*this)(x,y,s,v)*d; + else + cimg_forXYC(tmp,x,y,v) tmp(x,y,t,v)+=(Tfloat)res(x,y,s,v)*d; + if (!b) { + cimg_forXYC(tmp,x,y,v) tmp(x,y,t,v)/=_depth; + ++t; + b = _depth; + } + if (!c) { ++s; c = sz; } + } + tmp.move_to(res); + instance_first = false; + } + if (sc!=_spectrum) { + CImg tmp(sx,sy,sz,sc,0); + for (unsigned int a = _spectrum*sc, b = _spectrum, c = sc, s = 0, t = 0; a; ) { + const unsigned int d = cimg::min(b,c); + a-=d; b-=d; c-=d; + if (instance_first) + cimg_forXYZ(tmp,x,y,z) tmp(x,y,z,t)+=(Tfloat)(*this)(x,y,z,s)*d; + else + cimg_forXYZ(tmp,x,y,z) tmp(x,y,z,t)+=(Tfloat)res(x,y,z,s)*d; + if (!b) { + cimg_forXYZ(tmp,x,y,z) tmp(x,y,z,t)/=_spectrum; + ++t; + b = _spectrum; + } + if (!c) { ++s; c = sc; } + } + tmp.move_to(res); + instance_first = false; + } + } break; + + // Linear interpolation. + // + case 3 : { + CImg off(cimg::max(sx,sy,sz,sc)); + CImg foff(off._width); + CImg resx, resy, resz, resc; + + if (sx!=_width) { + if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); + else { + if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx); + else { + const float fx = (!boundary_conditions && sx>_width)?(sx>1?(_width-1.0f)/(sx-1):0):(float)_width/sx; + resx.assign(sx,_height,_depth,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forX(resx,x) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fx; + *(poff++) = (unsigned int)curr - (unsigned int)old; + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resx.size()>=65536) +#endif + cimg_forYZC(resx,y,z,c) { + const T *ptrs = data(0,y,z,c), *const ptrsmax = ptrs + (_width-1); + T *ptrd = resx.data(0,y,z,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forX(resx,x) { + const float alpha = *(pfoff++); + const T val1 = *ptrs, val2 = ptrssy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy); + else { + const float fy = (!boundary_conditions && sy>_height)?(sy>1?(_height-1.0f)/(sy-1):0):(float)_height/sy; + resy.assign(sx,sy,_depth,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forY(resy,y) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fy; + *(poff++) = sx*((unsigned int)curr-(unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resy.size()>=65536) +#endif + cimg_forXZC(resy,x,z,c) { + const T *ptrs = resx.data(x,0,z,c), *const ptrsmax = ptrs + (_height-1)*sx; + T *ptrd = resy.data(x,0,z,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forY(resy,y) { + const float alpha = *(pfoff++); + const T val1 = *ptrs, val2 = ptrssz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz); + else { + const float fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth-1.0f)/(sz-1):0):(float)_depth/sz; + const unsigned int sxy = sx*sy; + resz.assign(sx,sy,sz,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forZ(resz,z) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fz; + *(poff++) = sxy*((unsigned int)curr - (unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resz.size()>=65536) +#endif + cimg_forXYC(resz,x,y,c) { + const T *ptrs = resy.data(x,y,0,c), *const ptrsmax = ptrs + (_depth-1)*sxy; + T *ptrd = resz.data(x,y,0,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forZ(resz,z) { + const float alpha = *(pfoff++); + const T val1 = *ptrs, val2 = ptrssc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc); + else { + const float fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum-1.0f)/(sc-1):0): + (float)_spectrum/sc; + const unsigned int sxyz = sx*sy*sz; + resc.assign(sx,sy,sz,sc); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forC(resc,c) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fc; + *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resc.size()>=65536) +#endif + cimg_forXYZ(resc,x,y,z) { + const T *ptrs = resz.data(x,y,z,0), *const ptrsmax = ptrs + (_spectrum-1)*sxyz; + T *ptrd = resc.data(x,y,z,0); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forC(resc,c) { + const float alpha = *(pfoff++); + const T val1 = *ptrs, val2 = ptrs resx, resy, resz, resc; + if (sx!=_width) { + if (sx<_width) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); + else { + resx.assign(sx,_height,_depth,_spectrum,0); + const int dx = sx*2, dy = width()*2; + int err = (int)(dy + centering_x*(sx*dy/width() - dy)), xs = 0; + cimg_forX(resx,x) if ((err-=dy)<=0) { + cimg_forYZC(resx,y,z,c) resx(x,y,z,c) = (*this)(xs,y,z,c); + ++xs; + err+=dx; + } + } + } else resx.assign(*this,true); + + if (sy!=_height) { + if (sy<_height) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy); + else { + resy.assign(sx,sy,_depth,_spectrum,0); + const int dx = sy*2, dy = height()*2; + int err = (int)(dy + centering_y*(sy*dy/height() - dy)), ys = 0; + cimg_forY(resy,y) if ((err-=dy)<=0) { + cimg_forXZC(resy,x,z,c) resy(x,y,z,c) = resx(x,ys,z,c); + ++ys; + err+=dx; + } + } + resx.assign(); + } else resy.assign(resx,true); + + if (sz!=_depth) { + if (sz<_depth) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz); + else { + resz.assign(sx,sy,sz,_spectrum,0); + const int dx = sz*2, dy = depth()*2; + int err = (int)(dy + centering_z*(sz*dy/depth() - dy)), zs = 0; + cimg_forZ(resz,z) if ((err-=dy)<=0) { + cimg_forXYC(resz,x,y,c) resz(x,y,z,c) = resy(x,y,zs,c); + ++zs; + err+=dx; + } + } + resy.assign(); + } else resz.assign(resy,true); + + if (sc!=_spectrum) { + if (sc<_spectrum) resz.get_resize(sx,sy,sz,sc,1).move_to(resc); + else { + resc.assign(sx,sy,sz,sc,0); + const int dx = sc*2, dy = spectrum()*2; + int err = (int)(dy + centering_c*(sc*dy/spectrum() - dy)), cs = 0; + cimg_forC(resc,c) if ((err-=dy)<=0) { + cimg_forXYZ(resc,x,y,z) resc(x,y,z,c) = resz(x,y,z,cs); + ++cs; + err+=dx; + } + } + resz.assign(); + } else resc.assign(resz,true); + + return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc; + } break; + + // Cubic interpolation. + // + case 5 : { + const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); + CImg off(cimg::max(sx,sy,sz,sc)); + CImg foff(off._width); + CImg resx, resy, resz, resc; + + if (sx!=_width) { + if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); + else { + if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx); + else { + const float fx = (!boundary_conditions && sx>_width)?(sx>1?(_width-1.0f)/(sx-1):0):(float)_width/sx; + resx.assign(sx,_height,_depth,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forX(resx,x) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fx; + *(poff++) = (unsigned int)curr - (unsigned int)old; + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resx.size()>=65536) +#endif + cimg_forYZC(resx,y,z,c) { + const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_width-2); + T *ptrd = resx.data(0,y,z,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forX(resx,x) { + const float t = *(pfoff++); + const Tfloat + val1 = (Tfloat)*ptrs, + val0 = ptrs>ptrs0?(Tfloat)*(ptrs-1):val1, + val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+1):val1, + val3 = ptrsvmax?vmax:val); + ptrs+=*(poff++); + } + } + } + } + } else resx.assign(*this,true); + + if (sy!=_height) { + if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy); + else { + if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy); + else { + const float fy = (!boundary_conditions && sy>_height)?(sy>1?(_height-1.0f)/(sy-1):0):(float)_height/sy; + resy.assign(sx,sy,_depth,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forY(resy,y) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fy; + *(poff++) = sx*((unsigned int)curr-(unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resy.size()>=65536) +#endif + cimg_forXZC(resy,x,z,c) { + const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_height-2)*sx; + T *ptrd = resy.data(x,0,z,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forY(resy,y) { + const float t = *(pfoff++); + const Tfloat + val1 = (Tfloat)*ptrs, + val0 = ptrs>ptrs0?(Tfloat)*(ptrs-sx):val1, + val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sx):val1, + val3 = ptrsvmax?vmax:val); + ptrd+=sx; + ptrs+=*(poff++); + } + } + } + } + resx.assign(); + } else resy.assign(resx,true); + + if (sz!=_depth) { + if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz); + else { + if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz); + else { + const float fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth-1.0f)/(sz-1):0):(float)_depth/sz; + const unsigned int sxy = sx*sy; + resz.assign(sx,sy,sz,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forZ(resz,z) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fz; + *(poff++) = sxy*((unsigned int)curr - (unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resz.size()>=65536) +#endif + cimg_forXYC(resz,x,y,c) { + const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_depth-2)*sxy; + T *ptrd = resz.data(x,y,0,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forZ(resz,z) { + const float t = *(pfoff++); + const Tfloat + val1 = (Tfloat)*ptrs, + val0 = ptrs>ptrs0?(Tfloat)*(ptrs-sxy):val1, + val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxy):val1, + val3 = ptrsvmax?vmax:val); + ptrd+=sxy; + ptrs+=*(poff++); + } + } + } + } + resy.assign(); + } else resz.assign(resy,true); + + if (sc!=_spectrum) { + if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc); + else { + if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc); + else { + const float fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum-1.0f)/(sc-1):0): + (float)_spectrum/sc; + const unsigned int sxyz = sx*sy*sz; + resc.assign(sx,sy,sz,sc); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forC(resc,c) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fc; + *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resc.size()>=65536) +#endif + cimg_forXYZ(resc,x,y,z) { + const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmax = ptrs + (_spectrum-2)*sxyz; + T *ptrd = resc.data(x,y,z,0); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forC(resc,c) { + const float t = *(pfoff++); + const Tfloat + val1 = (Tfloat)*ptrs, + val0 = ptrs>ptrs0?(Tfloat)*(ptrs-sxyz):val1, + val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxyz):val1, + val3 = ptrsvmax?vmax:val); + ptrd+=sxyz; + ptrs+=*(poff++); + } + } + } + } + resz.assign(); + } else resc.assign(resz,true); + + return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc; + } break; + + // Lanczos interpolation. + // + case 6 : { + const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); + CImg off(cimg::max(sx,sy,sz,sc)); + CImg foff(off._width); + CImg resx, resy, resz, resc; + + if (sx!=_width) { + if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); + else { + if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx); + else { + const float fx = (!boundary_conditions && sx>_width)?(sx>1?(_width-1.0f)/(sx-1):0):(float)_width/sx; + resx.assign(sx,_height,_depth,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forX(resx,x) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fx; + *(poff++) = (unsigned int)curr - (unsigned int)old; + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resx.size()>=65536) +#endif + cimg_forYZC(resx,y,z,c) { + const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + 1, + *const ptrsmax = ptrs0 + (_width-2); + T *ptrd = resx.data(0,y,z,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forX(resx,x) { + const float + t = *(pfoff++), + w0 = _cimg_lanczos(t+2), + w1 = _cimg_lanczos(t+1), + w2 = _cimg_lanczos(t), + w3 = _cimg_lanczos(t-1), + w4 = _cimg_lanczos(t-2); + const Tfloat + val2 = (Tfloat)*ptrs, + val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-1):val2, + val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2):val1, + val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+1):val2, + val4 = ptrsvmax?vmax:val); + ptrs+=*(poff++); + } + } + } + } + } else resx.assign(*this,true); + + if (sy!=_height) { + if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy); + else { + if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy); + else { + const float fy = (!boundary_conditions && sy>_height)?(sy>1?(_height-1.0f)/(sy-1):0):(float)_height/sy; + resy.assign(sx,sy,_depth,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forY(resy,y) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fy; + *(poff++) = sx*((unsigned int)curr-(unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resy.size()>=65536) +#endif + cimg_forXZC(resy,x,z,c) { + const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sx, + *const ptrsmax = ptrs0 + (_height-2)*sx; + T *ptrd = resy.data(x,0,z,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forY(resy,y) { + const float + t = *(pfoff++), + w0 = _cimg_lanczos(t+2), + w1 = _cimg_lanczos(t+1), + w2 = _cimg_lanczos(t), + w3 = _cimg_lanczos(t-1), + w4 = _cimg_lanczos(t-2); + const Tfloat + val2 = (Tfloat)*ptrs, + val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-sx):val2, + val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2*sx):val1, + val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sx):val2, + val4 = ptrsvmax?vmax:val); + ptrd+=sx; + ptrs+=*(poff++); + } + } + } + } + resx.assign(); + } else resy.assign(resx,true); + + if (sz!=_depth) { + if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz); + else { + if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz); + else { + const float fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth-1.0f)/(sz-1):0):(float)_depth/sz; + const unsigned int sxy = sx*sy; + resz.assign(sx,sy,sz,_spectrum); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forZ(resz,z) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fz; + *(poff++) = sxy*((unsigned int)curr - (unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resz.size()>=65536) +#endif + cimg_forXYC(resz,x,y,c) { + const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxy, + *const ptrsmax = ptrs0 + (_depth-2)*sxy; + T *ptrd = resz.data(x,y,0,c); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forZ(resz,z) { + const float + t = *(pfoff++), + w0 = _cimg_lanczos(t+2), + w1 = _cimg_lanczos(t+1), + w2 = _cimg_lanczos(t), + w3 = _cimg_lanczos(t-1), + w4 = _cimg_lanczos(t-2); + const Tfloat + val2 = (Tfloat)*ptrs, + val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-sxy):val2, + val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2*sxy):val1, + val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxy):val2, + val4 = ptrsvmax?vmax:val); + ptrd+=sxy; + ptrs+=*(poff++); + } + } + } + } + resy.assign(); + } else resz.assign(resy,true); + + if (sc!=_spectrum) { + if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc); + else { + if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc); + else { + const float fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum-1.0f)/(sc-1):0): + (float)_spectrum/sc; + const unsigned int sxyz = sx*sy*sz; + resc.assign(sx,sy,sz,sc); + float curr = 0, old = 0; + unsigned int *poff = off._data; + float *pfoff = foff._data; + cimg_forC(resc,c) { + *(pfoff++) = curr - (unsigned int)curr; + old = curr; + curr+=fc; + *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old); + } +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (resc.size()>=65536) +#endif + cimg_forXYZ(resc,x,y,z) { + const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxyz, + *const ptrsmax = ptrs + (_spectrum-2)*sxyz; + T *ptrd = resc.data(x,y,z,0); + const unsigned int *poff = off._data; + const float *pfoff = foff._data; + cimg_forC(resc,c) { + const float + t = *(pfoff++), + w0 = _cimg_lanczos(t+2), + w1 = _cimg_lanczos(t+1), + w2 = _cimg_lanczos(t), + w3 = _cimg_lanczos(t-1), + w4 = _cimg_lanczos(t-2); + const Tfloat + val2 = (Tfloat)*ptrs, + val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-sxyz):val2, + val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2*sxyz):val1, + val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxyz):val2, + val4 = ptrsvmax?vmax:val); + ptrd+=sxyz; + ptrs+=*(poff++); + } + } + } + } + resz.assign(); + } else resc.assign(resz,true); + + return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc; + } break; + + // Unknow interpolation. + // + default : + throw CImgArgumentException(_cimg_instance + "resize(): Invalid specified interpolation %d " + "(should be { -1=raw | 0=none | 1=nearest | 2=average | 3=linear | 4=grid | " + "5=cubic | 6=lanczos }).", + cimg_instance, + interpolation_type); + } + return res; + } + + //! Resize image to dimensions of another image. + /** + \param src Reference image used for dimensions. + \param interpolation_type Interpolation method. + \param boundary_conditions Boundary conditions. + \param centering_x Set centering type (only if \p interpolation_type=0). + \param centering_y Set centering type (only if \p interpolation_type=0). + \param centering_z Set centering type (only if \p interpolation_type=0). + \param centering_c Set centering type (only if \p interpolation_type=0). + **/ + template + CImg& resize(const CImg& src, + const int interpolation_type=1, const unsigned int boundary_conditions=0, + const float centering_x = 0, const float centering_y = 0, + const float centering_z = 0, const float centering_c = 0) { + return resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions, + centering_x,centering_y,centering_z,centering_c); + } + + //! Resize image to dimensions of another image \newinstance. + template + CImg get_resize(const CImg& src, + const int interpolation_type=1, const unsigned int boundary_conditions=0, + const float centering_x = 0, const float centering_y = 0, + const float centering_z = 0, const float centering_c = 0) const { + return get_resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions, + centering_x,centering_y,centering_z,centering_c); + } + + //! Resize image to dimensions of a display window. + /** + \param disp Reference display window used for dimensions. + \param interpolation_type Interpolation method. + \param boundary_conditions Boundary conditions. + \param centering_x Set centering type (only if \p interpolation_type=0). + \param centering_y Set centering type (only if \p interpolation_type=0). + \param centering_z Set centering type (only if \p interpolation_type=0). + \param centering_c Set centering type (only if \p interpolation_type=0). + **/ + CImg& resize(const CImgDisplay& disp, + const int interpolation_type=1, const unsigned int boundary_conditions=0, + const float centering_x = 0, const float centering_y = 0, + const float centering_z = 0, const float centering_c = 0) { + return resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions, + centering_x,centering_y,centering_z,centering_c); + } + + //! Resize image to dimensions of a display window \newinstance. + CImg get_resize(const CImgDisplay& disp, + const int interpolation_type=1, const unsigned int boundary_conditions=0, + const float centering_x = 0, const float centering_y = 0, + const float centering_z = 0, const float centering_c = 0) const { + return get_resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions, + centering_x,centering_y,centering_z,centering_c); + } + + //! Resize image to half-size along XY axes, using an optimized filter. + CImg& resize_halfXY() { + return get_resize_halfXY().move_to(*this); + } + + //! Resize image to half-size along XY axes, using an optimized filter \newinstance. + CImg get_resize_halfXY() const { + if (is_empty()) return *this; + const Tfloat mask[9] = { 0.07842776544f, 0.1231940459f, 0.07842776544f, + 0.1231940459f, 0.1935127547f, 0.1231940459f, + 0.07842776544f, 0.1231940459f, 0.07842776544f }; + T I[9] = { 0 }; + CImg res(_width/2,_height/2,_depth,_spectrum); + T *ptrd = res._data; + cimg_forZC(*this,z,c) cimg_for3x3(*this,x,y,z,c,I,T) + if (x%2 && y%2) *(ptrd++) = (T) + (I[0]*mask[0] + I[1]*mask[1] + I[2]*mask[2] + + I[3]*mask[3] + I[4]*mask[4] + I[5]*mask[5] + + I[6]*mask[6] + I[7]*mask[7] + I[8]*mask[8]); + return res; + } + + //! Resize image to double-size, using the Scale2X algorithm. + /** + \note Use anisotropic upscaling algorithm + described here. + **/ + CImg& resize_doubleXY() { + return get_resize_doubleXY().move_to(*this); + } + + //! Resize image to double-size, using the Scale2X algorithm \newinstance. + CImg get_resize_doubleXY() const { +#define _cimg_gs2x_for3(bound,i) \ + for (int i = 0, _p1##i = 0, \ + _n1##i = 1>=(bound)?(int)(bound)-1:1; \ + _n1##i<(int)(bound) || i==--_n1##i; \ + _p1##i = i++, ++_n1##i, ptrd1+=(res)._width, ptrd2+=(res)._width) + +#define _cimg_gs2x_for3x3(img,x,y,z,c,I,T) \ + _cimg_gs2x_for3((img)._height,y) for (int x = 0, \ + _p1##x = 0, \ + _n1##x = (int)( \ + (I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \ + (I[3] = I[4] = (T)(img)(0,y,z,c)), \ + (I[7] = (T)(img)(0,_n1##y,z,c)), \ + 1>=(img)._width?(img).width()-1:1); \ + (_n1##x<(img).width() && ( \ + (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ + (I[5] = (T)(img)(_n1##x,y,z,c)), \ + (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ + x==--_n1##x; \ + I[1] = I[2], \ + I[3] = I[4], I[4] = I[5], \ + I[7] = I[8], \ + _p1##x = x++, ++_n1##x) + + if (is_empty()) return *this; + CImg res(_width<<1,_height<<1,_depth,_spectrum); + CImg_3x3(I,T); + cimg_forZC(*this,z,c) { + T + *ptrd1 = res.data(0,0,z,c), + *ptrd2 = ptrd1 + res._width; + _cimg_gs2x_for3x3(*this,x,y,z,c,I,T) { + if (Icp!=Icn && Ipc!=Inc) { + *(ptrd1++) = Ipc==Icp?Ipc:Icc; + *(ptrd1++) = Icp==Inc?Inc:Icc; + *(ptrd2++) = Ipc==Icn?Ipc:Icc; + *(ptrd2++) = Icn==Inc?Inc:Icc; + } else { *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc; } + } + } + return res; + } + + //! Resize image to triple-size, using the Scale3X algorithm. + /** + \note Use anisotropic upscaling algorithm + described here. + **/ + CImg& resize_tripleXY() { + return get_resize_tripleXY().move_to(*this); + } + + //! Resize image to triple-size, using the Scale3X algorithm \newinstance. + CImg get_resize_tripleXY() const { +#define _cimg_gs3x_for3(bound,i) \ + for (int i = 0, _p1##i = 0, \ + _n1##i = 1>=(bound)?(int)(bound)-1:1; \ + _n1##i<(int)(bound) || i==--_n1##i; \ + _p1##i = i++, ++_n1##i, ptrd1+=2*(res)._width, ptrd2+=2*(res)._width, ptrd3+=2*(res)._width) + +#define _cimg_gs3x_for3x3(img,x,y,z,c,I,T) \ + _cimg_gs3x_for3((img)._height,y) for (int x = 0, \ + _p1##x = 0, \ + _n1##x = (int)( \ + (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \ + (I[3] = I[4] = (T)(img)(0,y,z,c)), \ + (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \ + 1>=(img)._width?(img).width()-1:1); \ + (_n1##x<(img).width() && ( \ + (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ + (I[5] = (T)(img)(_n1##x,y,z,c)), \ + (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ + x==--_n1##x; \ + I[0] = I[1], I[1] = I[2], \ + I[3] = I[4], I[4] = I[5], \ + I[6] = I[7], I[7] = I[8], \ + _p1##x = x++, ++_n1##x) + + if (is_empty()) return *this; + CImg res(3*_width,3*_height,_depth,_spectrum); + CImg_3x3(I,T); + cimg_forZC(*this,z,c) { + T + *ptrd1 = res.data(0,0,z,c), + *ptrd2 = ptrd1 + res._width, + *ptrd3 = ptrd2 + res._width; + _cimg_gs3x_for3x3(*this,x,y,z,c,I,T) { + if (Icp != Icn && Ipc != Inc) { + *(ptrd1++) = Ipc==Icp?Ipc:Icc; + *(ptrd1++) = (Ipc==Icp && Icc!=Inp) || (Icp==Inc && Icc!=Ipp)?Icp:Icc; + *(ptrd1++) = Icp==Inc?Inc:Icc; + *(ptrd2++) = (Ipc==Icp && Icc!=Ipn) || (Ipc==Icn && Icc!=Ipp)?Ipc:Icc; + *(ptrd2++) = Icc; + *(ptrd2++) = (Icp==Inc && Icc!=Inn) || (Icn==Inc && Icc!=Inp)?Inc:Icc; + *(ptrd3++) = Ipc==Icn?Ipc:Icc; + *(ptrd3++) = (Ipc==Icn && Icc!=Inn) || (Icn==Inc && Icc!=Ipn)?Icn:Icc; + *(ptrd3++) = Icn==Inc?Inc:Icc; + } else { + *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd1++) = Icc; + *(ptrd2++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc; + *(ptrd3++) = Icc; *(ptrd3++) = Icc; *(ptrd3++) = Icc; + } + } + } + return res; + } + + //! Mirror image content along specified axis. + /** + \param axis Mirror axis + **/ + CImg& mirror(const char axis) { + if (is_empty()) return *this; + T *pf, *pb, *buf = 0; + switch (cimg::uncase(axis)) { + case 'x' : { + pf = _data; pb = data(_width-1); + const unsigned int width2 = _width/2; + for (unsigned int yzv = 0; yzv<_height*_depth*_spectrum; ++yzv) { + for (unsigned int x = 0; x get_mirror(const char axis) const { + return (+*this).mirror(axis); + } + + //! Mirror image content along specified axes. + /** + \param axes Mirror axes, as a C-string. + \note \c axes may contains multiple character, e.g. \c "xyz" + **/ + CImg& mirror(const char *const axes) { + for (const char *s = axes; *s; s++) mirror(*s); + return *this; + } + + //! Mirror image content along specified axes \newinstance. + CImg get_mirror(const char *const axes) const { + return (+*this).mirror(axes); + } + + //! Shift image content. + /** + \param delta_x Amount of displacement along the X-axis. + \param delta_y Amount of displacement along the Y-axis. + \param delta_z Amount of displacement along the Z-axis. + \param delta_c Amount of displacement along the C-axis. + \param boundary_conditions Border condition. + + - \c boundary_conditions can be: + - 0: Zero border condition (Dirichlet). + - 1: Nearest neighbors (Neumann). + - 2: Repeat Pattern (Fourier style). + **/ + CImg& shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0, + const int boundary_conditions=0) { + if (is_empty()) return *this; + if (delta_x) // Shift along X-axis + switch (boundary_conditions) { + case 0 : + if (cimg::abs(delta_x)>=width()) return fill(0); + if (delta_x<0) cimg_forYZC(*this,y,z,c) { + std::memmove(data(0,y,z,c),data(-delta_x,y,z,c),(_width+delta_x)*sizeof(T)); + std::memset(data(_width+delta_x,y,z,c),0,-delta_x*sizeof(T)); + } else cimg_forYZC(*this,y,z,c) { + std::memmove(data(delta_x,y,z,c),data(0,y,z,c),(_width-delta_x)*sizeof(T)); + std::memset(data(0,y,z,c),0,delta_x*sizeof(T)); + } + break; + case 1 : + if (delta_x<0) { + const int ndelta_x = (-delta_x>=width())?width()-1:-delta_x; + if (!ndelta_x) return *this; + cimg_forYZC(*this,y,z,c) { + std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T)); + T *ptrd = data(_width-1,y,z,c); + const T val = *ptrd; + for (int l = 0; l=width())?width()-1:delta_x; + if (!ndelta_x) return *this; + cimg_forYZC(*this,y,z,c) { + std::memmove(data(ndelta_x,y,z,c),data(0,y,z,c),(_width-ndelta_x)*sizeof(T)); + T *ptrd = data(0,y,z,c); + const T val = *ptrd; + for (int l = 0; l0) cimg_forYZC(*this,y,z,c) { + std::memcpy(buf,data(0,y,z,c),ndelta_x*sizeof(T)); + std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T)); + std::memcpy(data(_width-ndelta_x,y,z,c),buf,ndelta_x*sizeof(T)); + } else cimg_forYZC(*this,y,z,c) { + std::memcpy(buf,data(_width+ndelta_x,y,z,c),-ndelta_x*sizeof(T)); + std::memmove(data(-ndelta_x,y,z,c),data(0,y,z,c),(_width+ndelta_x)*sizeof(T)); + std::memcpy(data(0,y,z,c),buf,-ndelta_x*sizeof(T)); + } + delete[] buf; + } + } + + if (delta_y) // Shift along Y-axis + switch (boundary_conditions) { + case 0 : + if (cimg::abs(delta_y)>=height()) return fill(0); + if (delta_y<0) cimg_forZC(*this,z,c) { + std::memmove(data(0,0,z,c),data(0,-delta_y,z,c),_width*(_height+delta_y)*sizeof(T)); + std::memset(data(0,_height+delta_y,z,c),0,-delta_y*_width*sizeof(T)); + } else cimg_forZC(*this,z,c) { + std::memmove(data(0,delta_y,z,c),data(0,0,z,c),_width*(_height-delta_y)*sizeof(T)); + std::memset(data(0,0,z,c),0,delta_y*_width*sizeof(T)); + } + break; + case 1 : + if (delta_y<0) { + const int ndelta_y = (-delta_y>=height())?height()-1:-delta_y; + if (!ndelta_y) return *this; + cimg_forZC(*this,z,c) { + std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T)); + T *ptrd = data(0,_height-ndelta_y,z,c), *ptrs = data(0,_height-1,z,c); + for (int l = 0; l=height())?height()-1:delta_y; + if (!ndelta_y) return *this; + cimg_forZC(*this,z,c) { + std::memmove(data(0,ndelta_y,z,c),data(0,0,z,c),_width*(_height-ndelta_y)*sizeof(T)); + T *ptrd = data(0,1,z,c), *ptrs = data(0,0,z,c); + for (int l = 0; l0) cimg_forZC(*this,z,c) { + std::memcpy(buf,data(0,0,z,c),_width*ndelta_y*sizeof(T)); + std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T)); + std::memcpy(data(0,_height-ndelta_y,z,c),buf,_width*ndelta_y*sizeof(T)); + } else cimg_forZC(*this,z,c) { + std::memcpy(buf,data(0,_height+ndelta_y,z,c),-ndelta_y*_width*sizeof(T)); + std::memmove(data(0,-ndelta_y,z,c),data(0,0,z,c),_width*(_height+ndelta_y)*sizeof(T)); + std::memcpy(data(0,0,z,c),buf,-ndelta_y*_width*sizeof(T)); + } + delete[] buf; + } + } + + if (delta_z) // Shift along Z-axis + switch (boundary_conditions) { + case 0 : + if (cimg::abs(delta_z)>=depth()) return fill(0); + if (delta_z<0) cimg_forC(*this,c) { + std::memmove(data(0,0,0,c),data(0,0,-delta_z,c),_width*_height*(_depth+delta_z)*sizeof(T)); + std::memset(data(0,0,_depth+delta_z,c),0,_width*_height*(-delta_z)*sizeof(T)); + } else cimg_forC(*this,c) { + std::memmove(data(0,0,delta_z,c),data(0,0,0,c),_width*_height*(_depth-delta_z)*sizeof(T)); + std::memset(data(0,0,0,c),0,delta_z*_width*_height*sizeof(T)); + } + break; + case 1 : + if (delta_z<0) { + const int ndelta_z = (-delta_z>=depth())?depth()-1:-delta_z; + if (!ndelta_z) return *this; + cimg_forC(*this,c) { + std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T)); + T *ptrd = data(0,0,_depth-ndelta_z,c), *ptrs = data(0,0,_depth-1,c); + for (int l = 0; l=depth())?depth()-1:delta_z; + if (!ndelta_z) return *this; + cimg_forC(*this,c) { + std::memmove(data(0,0,ndelta_z,c),data(0,0,0,c),_width*_height*(_depth-ndelta_z)*sizeof(T)); + T *ptrd = data(0,0,1,c), *ptrs = data(0,0,0,c); + for (int l = 0; l0) cimg_forC(*this,c) { + std::memcpy(buf,data(0,0,0,c),_width*_height*ndelta_z*sizeof(T)); + std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T)); + std::memcpy(data(0,0,_depth-ndelta_z,c),buf,_width*_height*ndelta_z*sizeof(T)); + } else cimg_forC(*this,c) { + std::memcpy(buf,data(0,0,_depth+ndelta_z,c),-ndelta_z*_width*_height*sizeof(T)); + std::memmove(data(0,0,-ndelta_z,c),data(0,0,0,c),_width*_height*(_depth+ndelta_z)*sizeof(T)); + std::memcpy(data(0,0,0,c),buf,-ndelta_z*_width*_height*sizeof(T)); + } + delete[] buf; + } + } + + if (delta_c) // Shift along C-axis + switch (boundary_conditions) { + case 0 : + if (cimg::abs(delta_c)>=spectrum()) return fill(0); + if (delta_c<0) { + std::memmove(_data,data(0,0,0,-delta_c),_width*_height*_depth*(_spectrum+delta_c)*sizeof(T)); + std::memset(data(0,0,0,_spectrum+delta_c),0,_width*_height*_depth*(-delta_c)*sizeof(T)); + } else { + std::memmove(data(0,0,0,delta_c),_data,_width*_height*_depth*(_spectrum-delta_c)*sizeof(T)); + std::memset(_data,0,delta_c*_width*_height*_depth*sizeof(T)); + } + break; + case 1 : + if (delta_c<0) { + const int ndelta_c = (-delta_c>=spectrum())?spectrum()-1:-delta_c; + if (!ndelta_c) return *this; + std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T)); + T *ptrd = data(0,0,0,_spectrum-ndelta_c), *ptrs = data(0,0,0,_spectrum-1); + for (int l = 0; l=spectrum())?spectrum()-1:delta_c; + if (!ndelta_c) return *this; + std::memmove(data(0,0,0,ndelta_c),_data,_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T)); + T *ptrd = data(0,0,0,1); + for (int l = 0; l0) { + std::memcpy(buf,_data,_width*_height*_depth*ndelta_c*sizeof(T)); + std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T)); + std::memcpy(data(0,0,0,_spectrum-ndelta_c),buf,_width*_height*_depth*ndelta_c*sizeof(T)); + } else { + std::memcpy(buf,data(0,0,0,_spectrum+ndelta_c),-ndelta_c*_width*_height*_depth*sizeof(T)); + std::memmove(data(0,0,0,-ndelta_c),_data,_width*_height*_depth*(_spectrum+ndelta_c)*sizeof(T)); + std::memcpy(_data,buf,-ndelta_c*_width*_height*_depth*sizeof(T)); + } + delete[] buf; + } + } + return *this; + } + + //! Shift image content \newinstance. + CImg get_shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0, + const int boundary_conditions=0) const { + return (+*this).shift(delta_x,delta_y,delta_z,delta_c,boundary_conditions); + } + + //! Permute axes order. + /** + \param order Axes permutations, as a C-string of 4 characters. + This function permutes image content regarding the specified axes permutation. + **/ + CImg& permute_axes(const char *const order) { + return get_permute_axes(order).move_to(*this); + } + + //! Permute axes order \newinstance. + CImg get_permute_axes(const char *const order) const { + const T foo = (T)0; + return _get_permute_axes(order,foo); + } + + template + CImg _get_permute_axes(const char *const permut, const t&) const { + if (is_empty() || !permut) return CImg(*this,false); + CImg res; + const T* ptrs = _data; + if (!cimg::strncasecmp(permut,"xyzc",4)) return +*this; + if (!cimg::strncasecmp(permut,"xycz",4)) { + res.assign(_width,_height,_spectrum,_depth); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(x,y,c,z,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"xzyc",4)) { + res.assign(_width,_depth,_height,_spectrum); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(x,z,y,c,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"xzcy",4)) { + res.assign(_width,_depth,_spectrum,_height); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(x,z,c,y,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"xcyz",4)) { + res.assign(_width,_spectrum,_height,_depth); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(x,c,y,z,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"xczy",4)) { + res.assign(_width,_spectrum,_depth,_height); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(x,c,z,y,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"yxzc",4)) { + res.assign(_height,_width,_depth,_spectrum); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(y,x,z,c,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"yxcz",4)) { + res.assign(_height,_width,_spectrum,_depth); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(y,x,c,z,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"yzxc",4)) { + res.assign(_height,_depth,_width,_spectrum); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(y,z,x,c,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"yzcx",4)) { + res.assign(_height,_depth,_spectrum,_width); + switch (_width) { + case 1 : { + t *ptr_r = res.data(0,0,0,0); + for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { + *(ptr_r++) = (t)*(ptrs++); + } + } break; + case 2 : { + t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1); + for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { + *(ptr_r++) = (t)*(ptrs++); *(ptr_g++) = (t)*(ptrs++); + } + } break; + case 3 : { // Optimization for the classical conversion from interleaved RGB to planar RGB + t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2); + for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { + *(ptr_r++) = (t)*(ptrs++); *(ptr_g++) = (t)*(ptrs++); *(ptr_b++) = (t)*(ptrs++); + } + } break; + case 4 : { // Optimization for the classical conversion from interleaved RGBA to planar RGBA + t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2), *ptr_a = res.data(0,0,0,3); + for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { + *(ptr_r++) = (t)*(ptrs++); *(ptr_g++) = (t)*(ptrs++); *(ptr_b++) = (t)*(ptrs++); *(ptr_a++) = (t)*(ptrs++); + } + } break; + default : { + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(y,z,c,x,wh,whd) = *(ptrs++); + return res; + } + } + } + if (!cimg::strncasecmp(permut,"ycxz",4)) { + res.assign(_height,_spectrum,_width,_depth); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(y,c,x,z,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"yczx",4)) { + res.assign(_height,_spectrum,_depth,_width); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(y,c,z,x,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"zxyc",4)) { + res.assign(_depth,_width,_height,_spectrum); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(z,x,y,c,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"zxcy",4)) { + res.assign(_depth,_width,_spectrum,_height); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(z,x,c,y,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"zyxc",4)) { + res.assign(_depth,_height,_width,_spectrum); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(z,y,x,c,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"zycx",4)) { + res.assign(_depth,_height,_spectrum,_width); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(z,y,c,x,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"zcxy",4)) { + res.assign(_depth,_spectrum,_width,_height); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(z,c,x,y,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"zcyx",4)) { + res.assign(_depth,_spectrum,_height,_width); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(z,c,y,x,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"cxyz",4)) { + res.assign(_spectrum,_width,_height,_depth); + switch (_spectrum) { + case 1 : { + const T *ptr_r = data(0,0,0,0); + t *ptrd = res._data; + for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) *(ptrd++) = (t)*(ptr_r++); + } break; + case 2 : { + const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1); + t *ptrd = res._data; + for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) { + *(ptrd++) = (t)*(ptr_r++); *(ptrd++) = (t)*(ptr_g++); + } + } break; + case 3 : { // Optimization for the classical conversion from planar RGB to interleaved RGB + const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); + t *ptrd = res._data; + for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) { + *(ptrd++) = (t)*(ptr_r++); *(ptrd++) = (t)*(ptr_g++); *(ptrd++) = (t)*(ptr_b++); + } + } break; + case 4 : { // Optimization for the classical conversion from planar RGBA to interleaved RGBA + const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3); + t *ptrd = res._data; + for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) { + *(ptrd++) = (t)*(ptr_r++); *(ptrd++) = (t)*(ptr_g++); *(ptrd++) = (t)*(ptr_b++); *(ptrd++) = (t)*(ptr_a++); + } + } break; + default : { + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(c,x,y,z,wh,whd) = (t)*(ptrs++); + } + } + } + if (!cimg::strncasecmp(permut,"cxzy",4)) { + res.assign(_spectrum,_width,_depth,_height); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(c,x,z,y,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"cyxz",4)) { + res.assign(_spectrum,_height,_width,_depth); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(c,y,x,z,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"cyzx",4)) { + res.assign(_spectrum,_height,_depth,_width); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(c,y,z,x,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"czxy",4)) { + res.assign(_spectrum,_depth,_width,_height); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(c,z,x,y,wh,whd) = (t)*(ptrs++); + } + if (!cimg::strncasecmp(permut,"czyx",4)) { + res.assign(_spectrum,_depth,_height,_width); + const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; + cimg_forXYZC(*this,x,y,z,c) res(c,z,y,x,wh,whd) = (t)*(ptrs++); + } + if (!res) + throw CImgArgumentException(_cimg_instance + "permute_axes(): Invalid specified permutation '%s'.", + cimg_instance, + permut); + return res; + } + + //! Unroll pixel values along specified axis. + /** + \param axis Unroll axis (can be \c 'x', \c 'y', \c 'z' or c 'c'). + **/ + CImg& unroll(const char axis) { + const unsigned int siz = size(); + if (siz) switch (axis) { + case 'x' : _width = siz; _height = _depth = _spectrum = 1; break; + case 'y' : _height = siz; _width = _depth = _spectrum = 1; break; + case 'z' : _depth = siz; _width = _height = _spectrum = 1; break; + default : _spectrum = siz; _width = _height = _depth = 1; + } + return *this; + } + + //! Unroll pixel values along specified axis \newinstance. + CImg get_unroll(const char axis) const { + return (+*this).unroll(axis); + } + + //! Rotate image with arbitrary angle. + /** + \param angle Rotation angle, in degrees. + \param interpolation Type of interpolation. Can be { 0=nearest | 1=linear | 2=cubic }. + \param boundary Boundary conditions. Can be { 0=dirichlet | 1=neumann | 2=periodic }. + \note Most of the time, size of the image is modified. + **/ + CImg& rotate(const float angle, const unsigned int interpolation=1, const unsigned int boundary=0) { + const float nangle = cimg::mod(angle,360.0f); + if (nangle==0.0f) return *this; + return get_rotate(angle,interpolation,boundary).move_to(*this); + } + + //! Rotate image with arbitrary angle \newinstance. + CImg get_rotate(const float angle, const unsigned int interpolation=1, const unsigned int boundary=0) const { + if (is_empty()) return *this; + CImg res; + const float nangle = cimg::mod(angle,360.0f); + if (boundary!=1 && cimg::mod(nangle,90.0f)==0) { // Optimized version for orthogonal angles. + const int wm1 = width() - 1, hm1 = height() - 1; + const int iangle = (int)nangle/90; + switch (iangle) { + case 1 : { // 90 deg. + res.assign(_height,_width,_depth,_spectrum); + T *ptrd = res._data; + cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(y,hm1-x,z,c); + } break; + case 2 : { // 180 deg. + res.assign(_width,_height,_depth,_spectrum); + T *ptrd = res._data; + cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1-x,hm1-y,z,c); + } break; + case 3 : { // 270 deg. + res.assign(_height,_width,_depth,_spectrum); + T *ptrd = res._data; + cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1-y,x,z,c); + } break; + default : // 0 deg. + return *this; + } + } else { // Generic angle. + const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); + const float + rad = (float)(nangle*cimg::PI/180.0), + ca = (float)std::cos(rad), + sa = (float)std::sin(rad), + ux = cimg::abs(_width*ca), uy = cimg::abs(_width*sa), + vx = cimg::abs(_height*sa), vy = cimg::abs(_height*ca), + w2 = 0.5f*_width, h2 = 0.5f*_height, + dw2 = 0.5f*(ux+vx), dh2 = 0.5f*(uy+vy); + res.assign((int)(ux+vx),(int)(uy+vy),_depth,_spectrum); + switch (boundary) { + case 0 : { // Dirichlet boundaries. + switch (interpolation) { + case 2 : { // Cubic interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) { + const Tfloat val = cubic_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c,0); + res(x,y,z,c) = (T)(valvmax?vmax:val); + } + } break; + case 1 : { // Linear interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (T)linear_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c,0); + } break; + default : { // Nearest-neighbor interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = atXY((int)(w2 + (x-dw2)*ca + (y-dh2)*sa),(int)(h2 - (x-dw2)*sa + (y-dh2)*ca),z,c,0); + } + } + } break; + case 1 : { // Neumann boundaries. + switch (interpolation) { + case 2 : { // Cubic interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) { + const Tfloat val = _cubic_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c); + res(x,y,z,c) = (T)(valvmax?vmax:val); + } + } break; + case 1 : { // Linear interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (T)_linear_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c); + } break; + default : { // Nearest-neighbor interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = _atXY((int)(w2 + (x-dw2)*ca + (y-dh2)*sa),(int)(h2 - (x-dw2)*sa + (y-dh2)*ca),z,c); + } + } + } break; + case 2 : { // Periodic boundaries. + switch (interpolation) { + case 2 : { // Cubic interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) { + const Tfloat val = _cubic_atXY(cimg::mod(w2 + (x-dw2)*ca + (y-dh2)*sa,(float)width()), + cimg::mod(h2 - (x-dw2)*sa + (y-dh2)*ca,(float)height()),z,c); + res(x,y,z,c) = (T)(valvmax?vmax:val); + } + } break; + case 1 : { // Linear interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (T)_linear_atXY(cimg::mod(w2 + (x-dw2)*ca + (y-dh2)*sa,(float)width()), + cimg::mod(h2 - (x-dw2)*sa + (y-dh2)*ca,(float)height()),z,c); + } break; + default : { // Nearest-neighbor interpolation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (*this)(cimg::mod((int)(w2 + (x-dw2)*ca + (y-dh2)*sa),width()), + cimg::mod((int)(h2 - (x-dw2)*sa + (y-dh2)*ca),height()),z,c); + } + } + } break; + default : + throw CImgArgumentException(_cimg_instance + "rotate(): Invalid specified border conditions %d " + "(should be { 0=dirichlet | 1=neumann | 2=periodic }).", + cimg_instance, + boundary); + } + } + return res; + } + + //! Rotate image with arbitrary angle, around a center point. + /** + \param angle Rotation angle, in degrees. + \param cx X-coordinate of the rotation center. + \param cy Y-coordinate of the rotation center. + \param zoom Zoom factor. + \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann | 2=periodic }. + \param interpolation_type Type of interpolation. Can be { 0=nearest | 1=linear | 2=cubic }. + **/ + CImg& rotate(const float angle, const float cx, const float cy, const float zoom, + const unsigned int interpolation=1, const unsigned int boundary=3) { + return get_rotate(angle,cx,cy,zoom,interpolation,boundary).move_to(*this); + } + + //! Rotate image with arbitrary angle, around a center point \newinstance. + CImg get_rotate(const float angle, const float cx, const float cy, const float zoom, + const unsigned int interpolation=1, const unsigned int boundary=3) const { + if (interpolation>2) + throw CImgArgumentException(_cimg_instance + "rotate(): Invalid specified interpolation type %d " + "(should be { 0=none | 1=linear | 2=cubic }).", + cimg_instance, + interpolation); + if (is_empty()) return *this; + CImg res(_width,_height,_depth,_spectrum); + const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); + const float + rad = (float)((angle*cimg::PI)/180.0), + ca = (float)std::cos(rad)/zoom, + sa = (float)std::sin(rad)/zoom; + switch (boundary) { + case 0 : { + switch (interpolation) { + case 2 : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) { + const Tfloat val = cubic_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c,0); + res(x,y,z,c) = (T)(valvmax?vmax:val); + } + } break; + case 1 : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (T)linear_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c,0); + } break; + default : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = atXY((int)(cx + (x-cx)*ca + (y-cy)*sa),(int)(cy - (x-cx)*sa + (y-cy)*ca),z,c,0); + } + } + } break; + case 1 : { + switch (interpolation) { + case 2 : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) { + const Tfloat val = _cubic_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c); + res(x,y,z,c) = (T)(valvmax?vmax:val); + } + } break; + case 1 : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (T)_linear_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c); + } break; + default : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = _atXY((int)(cx + (x-cx)*ca + (y-cy)*sa),(int)(cy - (x-cx)*sa + (y-cy)*ca),z,c); + } + } + } break; + case 2 : { + switch (interpolation) { + case 2 : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) { + const Tfloat val = _cubic_atXY(cimg::mod(cx + (x-cx)*ca + (y-cy)*sa,(float)width()), + cimg::mod(cy - (x-cx)*sa + (y-cy)*ca,(float)height()),z,c); + res(x,y,z,c) = (T)(valvmax?vmax:val); + } + } break; + case 1 : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (T)_linear_atXY(cimg::mod(cx + (x-cx)*ca + (y-cy)*sa,(float)width()), + cimg::mod(cy - (x-cx)*sa + (y-cy)*ca,(float)height()),z,c); + } break; + default : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=2048) +#endif + cimg_forXYZC(res,x,y,z,c) + res(x,y,z,c) = (*this)(cimg::mod((int)(cx + (x-cx)*ca + (y-cy)*sa),width()), + cimg::mod((int)(cy - (x-cx)*sa + (y-cy)*ca),height()),z,c); + } + } + } break; + default : + throw CImgArgumentException(_cimg_instance + "rotate(): Invalid specified border conditions %d " + "(should be { 0=dirichlet | 1=neumann | 2=periodic }).", + cimg_instance, + boundary); + } + return res; + } + + //! Warp image content by a warping field. + /** + \param warp Warping field. + \param is_relative Tells if warping field gives absolute or relative warping coordinates. + \param interpolation Can be { 0=nearest | 1=linear | 2=cubic }. + \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann | 2=periodic }. + **/ + template + CImg& warp(const CImg& warp, const bool is_relative=false, + const unsigned int interpolation=1, const unsigned int boundary_conditions=0) { + return get_warp(warp,is_relative,interpolation,boundary_conditions).move_to(*this); + } + + //! Warp image content by a warping field \newinstance + template + CImg get_warp(const CImg& warp, const bool is_relative=false, + const unsigned int interpolation=1, const unsigned int boundary_conditions=0) const { + if (is_empty() || !warp) return *this; + if (is_relative && !is_sameXYZ(warp)) + throw CImgArgumentException(_cimg_instance + "warp(): Instance and specified relative warping field (%u,%u,%u,%u,%p) " + "have different XYZ dimensions.", + cimg_instance, + warp._width,warp._height,warp._depth,warp._spectrum,warp._data); + + CImg res(warp._width,warp._height,warp._depth,_spectrum); + + if (warp._spectrum==1) { // 1d warping. + if (is_relative) { // Relative warp. + if (interpolation==2) { // Cubic interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX(cimg::mod(x - (float)*(ptrs0++),(float)_width),y,z,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX(x - (float)*(ptrs0++),y,z,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)cubic_atX(x - (float)*(ptrs0++),y,z,c,0); + } + } else if (interpolation==1) { // Linear interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(cimg::mod(x - (float)*(ptrs0++),(float)_width),y,z,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(x - (float)*(ptrs0++),y,z,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)linear_atX(x - (float)*(ptrs0++),y,z,c,0); + } + } else { // Nearest-neighbor interpolation. + if (boundary_conditions==2) // Periodic boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)*(ptrs0++),(int)_width),y,z,c); + } + else if (boundary_conditions==1) // Neumann boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = _atX(x - (int)*(ptrs0++),y,z,c); + } + else // Dirichlet boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = atX(x - (int)*(ptrs0++),y,z,c,0); + } + } + } else { // Absolute warp. + if (interpolation==2) { // Cubic interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX(cimg::mod((float)*(ptrs0++),(float)_width),0,0,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX((float)*(ptrs0++),0,0,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)cubic_atX((float)*(ptrs0++),0,0,c,0); + } + } else if (interpolation==1) { // Linear interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(cimg::mod((float)*(ptrs0++),(float)_width),0,0,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atX((float)*(ptrs0++),0,0,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)linear_atX((float)*(ptrs0++),0,0,c,0); + } + } else { // Nearest-neighbor interpolation. + if (boundary_conditions==2) // Periodic boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)*(ptrs0++),(int)_width),0,0,c); + } + else if (boundary_conditions==1) // Neumann boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = _atX((int)*(ptrs0++),0,0,c); + } + else // Dirichlet boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = atX((int)*(ptrs0++),0,0,c,0); + } + } + } + + } else if (warp._spectrum==2) { // 2d warping. + if (is_relative) { // Relative warp. + if (interpolation==2) { // Cubic interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY(cimg::mod(x - (float)*(ptrs0++),(float)_width), + cimg::mod(y - (float)*(ptrs1++),(float)_height),z,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)cubic_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,0); + } + } else if (interpolation==1) { // Linear interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(cimg::mod(x - (float)*(ptrs0++),(float)_width), + cimg::mod(y - (float)*(ptrs1++),(float)_height),z,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,0); + } + } else { // Nearest-neighbor interpolation. + if (boundary_conditions==2) // Periodic boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)*(ptrs0++),(int)_width), + cimg::mod(y - (int)*(ptrs1++),(int)_height),z,c); + } + else if (boundary_conditions==1) // Neumann boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = _atXY(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z,c); + } + else // Dirichlet boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = atXY(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z,c,0); + } + } + } else { // Absolute warp. + if (interpolation==2) { // Cubic interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY(cimg::mod((float)*(ptrs0++),(float)_width), + cimg::mod((float)*(ptrs1++),(float)_height),0,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)cubic_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c,0); + } + } else if (interpolation==1) { // Linear interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(cimg::mod((float)*(ptrs0++),(float)_width), + cimg::mod((float)*(ptrs1++),(float)_height),0,c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c,0); + } + } else { // Nearest-neighbor interpolation. + if (boundary_conditions==2) // Periodic boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)*(ptrs0++),(int)_width), + cimg::mod((int)*(ptrs1++),(int)_height),0,c); + } + else if (boundary_conditions==1) // Neumann boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = _atXY((int)*(ptrs0++),(int)*(ptrs1++),0,c); + } + else // Dirichlet boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = atXY((int)*(ptrs0++),(int)*(ptrs1++),0,c,0); + } + } + } + + } else if (warp._spectrum==3) { // 3d warping. + if (is_relative) { // Relative warp. + if (interpolation==2) { // Cubic interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXYZ(cimg::mod(x - (float)*(ptrs0++),(float)_width), + cimg::mod(y - (float)*(ptrs1++),(float)_height), + cimg::mod(z - (float)*(ptrs2++),(float)_depth),c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) + *(ptrd++) = (T)_cubic_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) + *(ptrd++) = (T)cubic_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,0); + } + } else if (interpolation==1) { // Linear interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ(cimg::mod(x - (float)*(ptrs0++),(float)_width), + cimg::mod(y - (float)*(ptrs1++),(float)_height), + cimg::mod(z - (float)*(ptrs2++),(float)_depth),c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) + *(ptrd++) = (T)_linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) + *(ptrd++) = (T)linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,0); + } + } else { // Nearest neighbor interpolation. + if (boundary_conditions==2) // Periodic boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)*(ptrs0++),(int)_width), + cimg::mod(y - (int)*(ptrs1++),(int)_height), + cimg::mod(z - (int)*(ptrs2++),(int)_depth),c); + } + else if (boundary_conditions==1) // Neumann boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = _atXYZ(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z - (int)*(ptrs2++),c); + } + else // Dirichlet boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = atXYZ(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z - (int)*(ptrs2++),c,0); + } + } + } else { // Absolute warp. + if (interpolation==2) { // Cubic interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXYZ(cimg::mod((float)*(ptrs0++),(float)_width), + cimg::mod((float)*(ptrs1++),(float)_height), + cimg::mod((float)*(ptrs2++),(float)_depth),c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=4096) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)cubic_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c,0); + } + } else if (interpolation==1) { // Linear interpolation. + if (boundary_conditions==2) // Periodic boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ(cimg::mod((float)*(ptrs0++),(float)_width), + cimg::mod((float)*(ptrs1++),(float)_height), + cimg::mod((float)*(ptrs2++),(float)_depth),c); + } + else if (boundary_conditions==1) // Neumann boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c); + } + else // Dirichlet boundaries. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (res.size()>=1048576) +#endif + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (T)linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c,0); + } + } else { // Nearest-neighbor interpolation. + if (boundary_conditions==2) // Periodic boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)*(ptrs0++),(int)_width), + cimg::mod((int)*(ptrs1++),(int)_height), + cimg::mod((int)*(ptrs2++),(int)_depth),c); + } + else if (boundary_conditions==1) // Neumann boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = _atXYZ((int)*(ptrs0++),(int)*(ptrs1++),(int)*(ptrs2++),c); + } + else // Dirichlet boundaries. + cimg_forYZC(res,y,z,c) { + const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); + T *ptrd = res.data(0,y,z,c); + cimg_forX(res,x) *(ptrd++) = atXYZ((int)*(ptrs0++),(int)*(ptrs1++),(int)*(ptrs2++),c,0); + } + } + } + } + return res; + } + + //! Generate a 2d representation of a 3d image, with XY,XZ and YZ views. + /** + \param x0 X-coordinate of the projection point. + \param y0 Y-coordinate of the projection point. + \param z0 Z-coordinate of the projection point. + **/ + CImg get_projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) const { + if (is_empty() || _depth<2) return +*this; + const unsigned int + _x0 = (x0>=_width)?_width - 1:x0, + _y0 = (y0>=_height)?_height - 1:y0, + _z0 = (z0>=_depth)?_depth - 1:z0; + const CImg + img_xy = get_crop(0,0,_z0,0,_width-1,_height-1,_z0,_spectrum-1), + img_zy = get_crop(_x0,0,0,0,_x0,_height-1,_depth-1,_spectrum-1).permute_axes("xzyc"). + resize(_depth,_height,1,-100,-1), + img_xz = get_crop(0,_y0,0,0,_width-1,_y0,_depth-1,_spectrum-1).resize(_width,_depth,1,-100,-1); + return CImg(_width + _depth,_height + _depth,1,_spectrum,cimg::min(img_xy.min(),img_zy.min(),img_xz.min())). + draw_image(0,0,img_xy).draw_image(img_xy._width,0,img_zy). + draw_image(0,img_xy._height,img_xz); + } + + //! Construct a 2d representation of a 3d image, with XY,XZ and YZ views \inplace. + CImg& projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) { + if (_depth<2) return *this; + return get_projections2d(x0,y0,z0).move_to(*this); + } + + //! Crop image region. + /** + \param x0 = X-coordinate of the upper-left crop rectangle corner. + \param y0 = Y-coordinate of the upper-left crop rectangle corner. + \param z0 = Z-coordinate of the upper-left crop rectangle corner. + \param c0 = C-coordinate of the upper-left crop rectangle corner. + \param x1 = X-coordinate of the lower-right crop rectangle corner. + \param y1 = Y-coordinate of the lower-right crop rectangle corner. + \param z1 = Z-coordinate of the lower-right crop rectangle corner. + \param c1 = C-coordinate of the lower-right crop rectangle corner. + \param boundary_conditions = Dirichlet (false) or Neumann border conditions. + **/ + CImg& crop(const int x0, const int y0, const int z0, const int c0, + const int x1, const int y1, const int z1, const int c1, + const bool boundary_conditions=false) { + return get_crop(x0,y0,z0,c0,x1,y1,z1,c1,boundary_conditions).move_to(*this); + } + + //! Crop image region \newinstance. + CImg get_crop(const int x0, const int y0, const int z0, const int c0, + const int x1, const int y1, const int z1, const int c1, + const bool boundary_conditions=false) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "crop(): Empty instance.", + cimg_instance); + const int + nx0 = x0 res(1U + nx1 - nx0,1U + ny1 - ny0,1U + nz1 - nz0,1U + nc1 - nc0); + if (nx0<0 || nx1>=width() || ny0<0 || ny1>=height() || nz0<0 || nz1>=depth() || nc0<0 || nc1>=spectrum()) { + if (boundary_conditions) cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _atXYZC(nx0+x,ny0+y,nz0+z,nc0+c); + else res.fill(0).draw_image(-nx0,-ny0,-nz0,-nc0,*this); + } else res.draw_image(-nx0,-ny0,-nz0,-nc0,*this); + return res; + } + + //! Crop image region \overloading. + CImg& crop(const int x0, const int y0, const int z0, + const int x1, const int y1, const int z1, + const bool boundary_conditions=false) { + return crop(x0,y0,z0,0,x1,y1,z1,_spectrum-1,boundary_conditions); + } + + //! Crop image region \newinstance. + CImg get_crop(const int x0, const int y0, const int z0, + const int x1, const int y1, const int z1, + const bool boundary_conditions=false) const { + return get_crop(x0,y0,z0,0,x1,y1,z1,_spectrum-1,boundary_conditions); + } + + //! Crop image region \overloading. + CImg& crop(const int x0, const int y0, + const int x1, const int y1, + const bool boundary_conditions=false) { + return crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions); + } + + //! Crop image region \newinstance. + CImg get_crop(const int x0, const int y0, + const int x1, const int y1, + const bool boundary_conditions=false) const { + return get_crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions); + } + + //! Crop image region \overloading. + CImg& crop(const int x0, const int x1, const bool boundary_conditions=false) { + return crop(x0,0,0,0,x1,_height-1,_depth-1,_spectrum-1,boundary_conditions); + } + + //! Crop image region \newinstance. + CImg get_crop(const int x0, const int x1, const bool boundary_conditions=false) const { + return get_crop(x0,0,0,0,x1,_height-1,_depth-1,_spectrum-1,boundary_conditions); + } + + //! Autocrop image region, regarding the specified background value. + CImg& autocrop(const T value, const char *const axes="czyx") { + if (is_empty()) return *this; + for (const char *s = axes; *s; ++s) { + const char axis = cimg::uncase(*s); + const CImg coords = _autocrop(value,axis); + if (coords[0]==-1 && coords[1]==-1) return assign(); // Image has only 'value' pixels. + else switch (axis) { + case 'x' : { + const int x0 = coords[0], x1 = coords[1]; + if (x0>=0 && x1>=0) crop(x0,x1); + } break; + case 'y' : { + const int y0 = coords[0], y1 = coords[1]; + if (y0>=0 && y1>=0) crop(0,y0,_width-1,y1); + } break; + case 'z' : { + const int z0 = coords[0], z1 = coords[1]; + if (z0>=0 && z1>=0) crop(0,0,z0,_width-1,_height-1,z1); + } break; + default : { + const int c0 = coords[0], c1 = coords[1]; + if (c0>=0 && c1>=0) crop(0,0,0,c0,_width-1,_height-1,_depth-1,c1); + } + } + } + return *this; + } + + //! Autocrop image region, regarding the specified background value \newinstance. + CImg get_autocrop(const T value, const char *const axes="czyx") const { + return (+*this).autocrop(value,axes); + } + + //! Autocrop image region, regarding the specified background color. + /** + \param color Color used for the crop. If \c 0, color is guessed. + \param axes Axes used for the crop. + **/ + CImg& autocrop(const T *const color=0, const char *const axes="zyx") { + if (is_empty()) return *this; + if (!color) { // Guess color. + const CImg col1 = get_vector_at(0,0,0); + const unsigned int w = _width, h = _height, d = _depth, s = _spectrum; + autocrop(col1,axes); + if (_width==w && _height==h && _depth==d && _spectrum==s) { + const CImg col2 = get_vector_at(w-1,h-1,d-1); + autocrop(col2,axes); + } + return *this; + } + for (const char *s = axes; *s; ++s) { + const char axis = cimg::uncase(*s); + switch (axis) { + case 'x' : { + int x0 = width(), x1 = -1; + cimg_forC(*this,c) { + const CImg coords = get_shared_channel(c)._autocrop(color[c],'x'); + const int nx0 = coords[0], nx1 = coords[1]; + if (nx0>=0 && nx1>=0) { x0 = cimg::min(x0,nx0); x1 = cimg::max(x1,nx1); } + } + if (x0==width() && x1==-1) return assign(); else crop(x0,x1); + } break; + case 'y' : { + int y0 = height(), y1 = -1; + cimg_forC(*this,c) { + const CImg coords = get_shared_channel(c)._autocrop(color[c],'y'); + const int ny0 = coords[0], ny1 = coords[1]; + if (ny0>=0 && ny1>=0) { y0 = cimg::min(y0,ny0); y1 = cimg::max(y1,ny1); } + } + if (y0==height() && y1==-1) return assign(); else crop(0,y0,_width-1,y1); + } break; + default : { + int z0 = depth(), z1 = -1; + cimg_forC(*this,c) { + const CImg coords = get_shared_channel(c)._autocrop(color[c],'z'); + const int nz0 = coords[0], nz1 = coords[1]; + if (nz0>=0 && nz1>=0) { z0 = cimg::min(z0,nz0); z1 = cimg::max(z1,nz1); } + } + if (z0==depth() && z1==-1) return assign(); else crop(0,0,z0,_width-1,_height-1,z1); + } + } + } + return *this; + } + + //! Autocrop image region, regarding the specified background color \newinstance. + CImg get_autocrop(const T *const color=0, const char *const axes="zyx") const { + return (+*this).autocrop(color,axes); + } + + //! Autocrop image region, regarding the specified background color \overloading. + template CImg& autocrop(const CImg& color, const char *const axes="zyx") { + return get_autocrop(color,axes).move_to(*this); + } + + //! Autocrop image region, regarding the specified background color \newinstance. + template CImg get_autocrop(const CImg& color, const char *const axes="zyx") const { + return get_autocrop(color._data,axes); + } + + CImg _autocrop(const T value, const char axis) const { + CImg res; + switch (cimg::uncase(axis)) { + case 'x' : { + int x0 = -1, x1 = -1; + cimg_forX(*this,x) cimg_forYZC(*this,y,z,c) + if ((*this)(x,y,z,c)!=value) { x0 = x; x = width(); y = height(); z = depth(); c = spectrum(); } + if (x0>=0) { + for (int x = width()-1; x>=0; --x) cimg_forYZC(*this,y,z,c) + if ((*this)(x,y,z,c)!=value) { x1 = x; x = 0; y = height(); z = depth(); c = spectrum(); } + } + res = CImg::vector(x0,x1); + } break; + case 'y' : { + int y0 = -1, y1 = -1; + cimg_forY(*this,y) cimg_forXZC(*this,x,z,c) + if ((*this)(x,y,z,c)!=value) { y0 = y; x = width(); y = height(); z = depth(); c = spectrum(); } + if (y0>=0) { + for (int y = height()-1; y>=0; --y) cimg_forXZC(*this,x,z,c) + if ((*this)(x,y,z,c)!=value) { y1 = y; x = width(); y = 0; z = depth(); c = spectrum(); } + } + res = CImg::vector(y0,y1); + } break; + case 'z' : { + int z0 = -1, z1 = -1; + cimg_forZ(*this,z) cimg_forXYC(*this,x,y,c) + if ((*this)(x,y,z,c)!=value) { z0 = z; x = width(); y = height(); z = depth(); c = spectrum(); } + if (z0>=0) { + for (int z = depth()-1; z>=0; --z) cimg_forXYC(*this,x,y,c) + if ((*this)(x,y,z,c)!=value) { z1 = z; x = width(); y = height(); z = 0; c = spectrum(); } + } + res = CImg::vector(z0,z1); + } break; + default : { + int c0 = -1, c1 = -1; + cimg_forC(*this,c) cimg_forXYZ(*this,x,y,z) + if ((*this)(x,y,z,c)!=value) { c0 = c; x = width(); y = height(); z = depth(); c = spectrum(); } + if (c0>=0) { + for (int c = spectrum()-1; c>=0; --c) cimg_forXYZ(*this,x,y,z) + if ((*this)(x,y,z,c)!=value) { c1 = c; x = width(); y = height(); z = depth(); c = 0; } + } + res = CImg::vector(c0,c1); + } + } + return res; + } + + //! Return specified image column. + /** + \param x0 Image column. + **/ + CImg get_column(const int x0) const { + return get_columns(x0,x0); + } + + //! Return specified image column \inplace. + CImg& column(const int x0) { + return columns(x0,x0); + } + + //! Return specified range of image columns. + /** + \param x0 Starting image column. + \param x1 Ending image column. + **/ + CImg& columns(const int x0, const int x1) { + return get_columns(x0,x1).move_to(*this); + } + + //! Return specified range of image columns \inplace. + CImg get_columns(const int x0, const int x1) const { + return get_crop(x0,0,0,0,x1,height()-1,depth()-1,spectrum()-1); + } + + //! Return specified image row. + CImg get_row(const int y0) const { + return get_rows(y0,y0); + } + + //! Return specified image row \inplace. + /** + \param y0 Image row. + **/ + CImg& row(const int y0) { + return rows(y0,y0); + } + + //! Return specified range of image rows. + /** + \param y0 Starting image row. + \param y1 Ending image row. + **/ + CImg get_rows(const int y0, const int y1) const { + return get_crop(0,y0,0,0,width()-1,y1,depth()-1,spectrum()-1); + } + + //! Return specified range of image rows \inplace. + CImg& rows(const int y0, const int y1) { + return get_rows(y0,y1).move_to(*this); + } + + //! Return specified image slice. + /** + \param z0 Image slice. + **/ + CImg get_slice(const int z0) const { + return get_slices(z0,z0); + } + + //! Return specified image slice \inplace. + CImg& slice(const int z0) { + return slices(z0,z0); + } + + //! Return specified range of image slices. + /** + \param z0 Starting image slice. + \param z1 Ending image slice. + **/ + CImg get_slices(const int z0, const int z1) const { + return get_crop(0,0,z0,0,width()-1,height()-1,z1,spectrum()-1); + } + + //! Return specified range of image slices \inplace. + CImg& slices(const int z0, const int z1) { + return get_slices(z0,z1).move_to(*this); + } + + //! Return specified image channel. + /** + \param c0 Image channel. + **/ + CImg get_channel(const int c0) const { + return get_channels(c0,c0); + } + + //! Return specified image channel \inplace. + CImg& channel(const int c0) { + return channels(c0,c0); + } + + //! Return specified range of image channels. + /** + \param c0 Starting image channel. + \param c1 Ending image channel. + **/ + CImg get_channels(const int c0, const int c1) const { + return get_crop(0,0,0,c0,width()-1,height()-1,depth()-1,c1); + } + + //! Return specified range of image channels \inplace. + CImg& channels(const int c0, const int c1) { + return get_channels(c0,c1).move_to(*this); + } + + //! Return stream line of a 2d or 3d vector field. + CImg get_streamline(const float x, const float y, const float z, + const float L=256, const float dl=0.1f, + const unsigned int interpolation_type=2, const bool is_backward_tracking=false, + const bool is_oriented_only=false) const { + if (_spectrum!=2 && _spectrum!=3) + throw CImgInstanceException(_cimg_instance + "streamline(): Instance is not a 2d or 3d vector field.", + cimg_instance); + if (_spectrum==2) { + if (is_oriented_only) { + typename CImg::_functor4d_streamline2d_oriented func(*this); + return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true, + 0,0,0,_width-1.0f,_height-1.0f,0.0f); + } else { + typename CImg::_functor4d_streamline2d_directed func(*this); + return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false, + 0,0,0,_width-1.0f,_height-1.0f,0.0f); + } + } + if (is_oriented_only) { + typename CImg::_functor4d_streamline3d_oriented func(*this); + return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true, + 0,0,0,_width-1.0f,_height-1.0f,_depth-1.0f); + } + typename CImg::_functor4d_streamline3d_directed func(*this); + return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false, + 0,0,0,_width-1.0f,_height-1.0f,_depth-1.0f); + } + + //! Return stream line of a 3d vector field. + /** + \param func Vector field function. + \param x X-coordinate of the starting point of the streamline. + \param y Y-coordinate of the starting point of the streamline. + \param z Z-coordinate of the starting point of the streamline. + \param L Streamline length. + \param dl Streamline length increment. + \param interpolation_type Type of interpolation. + Can be { 0=nearest int | 1=linear | 2=2nd-order RK | 3=4th-order RK. }. + \param is_backward_tracking Tells if the streamline is estimated forward or backward. + \param is_oriented_only Tells if the direction of the vectors must be ignored. + \param x0 X-coordinate of the first bounding-box vertex. + \param y0 Y-coordinate of the first bounding-box vertex. + \param z0 Z-coordinate of the first bounding-box vertex. + \param x1 X-coordinate of the second bounding-box vertex. + \param y1 Y-coordinate of the second bounding-box vertex. + \param z1 Z-coordinate of the second bounding-box vertex. + **/ + template + static CImg streamline(const tfunc& func, + const float x, const float y, const float z, + const float L=256, const float dl=0.1f, + const unsigned int interpolation_type=2, const bool is_backward_tracking=false, + const bool is_oriented_only=false, + const float x0=0, const float y0=0, const float z0=0, + const float x1=0, const float y1=0, const float z1=0) { + if (dl<=0) + throw CImgArgumentException("CImg<%s>::streamline(): Invalid specified integration length %g " + "(should be >0).", + pixel_type(), + dl); + + const bool is_bounded = (x0!=x1 || y0!=y1 || z0!=z1); + if (L<=0 || (is_bounded && (xx1 || yy1 || zz1))) return CImg(); + const unsigned int size_L = (unsigned int)cimg::round(L/dl+1); + CImg coordinates(size_L,3); + const float dl2 = dl/2; + float + *ptr_x = coordinates.data(0,0), + *ptr_y = coordinates.data(0,1), + *ptr_z = coordinates.data(0,2), + pu = (float)(dl*func(x,y,z,0)), + pv = (float)(dl*func(x,y,z,1)), + pw = (float)(dl*func(x,y,z,2)), + X = x, Y = y, Z = z; + + switch (interpolation_type) { + case 0 : { // Nearest integer interpolation. + cimg_forX(coordinates,l) { + *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; + const int + xi = (int)(X>0?X+0.5f:X-0.5f), + yi = (int)(Y>0?Y+0.5f:Y-0.5f), + zi = (int)(Z>0?Z+0.5f:Z-0.5f); + float + u = (float)(dl*func((float)xi,(float)yi,(float)zi,0)), + v = (float)(dl*func((float)xi,(float)yi,(float)zi,1)), + w = (float)(dl*func((float)xi,(float)yi,(float)zi,2)); + if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; } + if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } + if (is_bounded && (Xx1 || Yy1 || Zz1)) break; + } + } break; + case 1 : { // First-order interpolation. + cimg_forX(coordinates,l) { + *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; + float + u = (float)(dl*func(X,Y,Z,0)), + v = (float)(dl*func(X,Y,Z,1)), + w = (float)(dl*func(X,Y,Z,2)); + if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; } + if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } + if (is_bounded && (Xx1 || Yy1 || Zz1)) break; + } + } break; + case 2 : { // Second order interpolation. + cimg_forX(coordinates,l) { + *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; + float + u0 = (float)(dl2*func(X,Y,Z,0)), + v0 = (float)(dl2*func(X,Y,Z,1)), + w0 = (float)(dl2*func(X,Y,Z,2)); + if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; } + float + u = (float)(dl*func(X+u0,Y+v0,Z+w0,0)), + v = (float)(dl*func(X+u0,Y+v0,Z+w0,1)), + w = (float)(dl*func(X+u0,Y+v0,Z+w0,2)); + if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; } + if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } + if (is_bounded && (Xx1 || Yy1 || Zz1)) break; + } + } break; + default : { // Fourth order interpolation. + cimg_forX(coordinates,x) { + *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; + float + u0 = (float)(dl2*func(X,Y,Z,0)), + v0 = (float)(dl2*func(X,Y,Z,1)), + w0 = (float)(dl2*func(X,Y,Z,2)); + if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; } + float + u1 = (float)(dl2*func(X+u0,Y+v0,Z+w0,0)), + v1 = (float)(dl2*func(X+u0,Y+v0,Z+w0,1)), + w1 = (float)(dl2*func(X+u0,Y+v0,Z+w0,2)); + if (is_oriented_only && u1*pu + v1*pv + w1*pw<0) { u1 = -u1; v1 = -v1; w1 = -w1; } + float + u2 = (float)(dl2*func(X+u1,Y+v1,Z+w1,0)), + v2 = (float)(dl2*func(X+u1,Y+v1,Z+w1,1)), + w2 = (float)(dl2*func(X+u1,Y+v1,Z+w1,2)); + if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u2 = -u2; v2 = -v2; w2 = -w2; } + float + u3 = (float)(dl2*func(X+u2,Y+v2,Z+w2,0)), + v3 = (float)(dl2*func(X+u2,Y+v2,Z+w2,1)), + w3 = (float)(dl2*func(X+u2,Y+v2,Z+w2,2)); + if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u3 = -u3; v3 = -v3; w3 = -w3; } + const float + u = (u0 + u3)/3 + (u1 + u2)/1.5f, + v = (v0 + v3)/3 + (v1 + v2)/1.5f, + w = (w0 + w3)/3 + (w1 + w2)/1.5f; + if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } + if (is_bounded && (Xx1 || Yy1 || Zz1)) break; + } + } + } + if (ptr_x!=coordinates.data(0,1)) coordinates.resize((int)(ptr_x-coordinates.data()),3,1,1,0); + return coordinates; + } + + //! Return stream line of a 3d vector field \overloading. + static CImg streamline(const char *const expression, + const float x, const float y, const float z, + const float L=256, const float dl=0.1f, + const unsigned int interpolation_type=2, const bool is_backward_tracking=true, + const bool is_oriented_only=false, + const float x0=0, const float y0=0, const float z0=0, + const float x1=0, const float y1=0, const float z1=0) { + _functor4d_streamline_expr func(expression); + return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,is_oriented_only,x0,y0,z0,x1,y1,z1); + } + + struct _functor4d_streamline2d_directed { + const CImg& ref; + _functor4d_streamline2d_directed(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y, const float z, const unsigned int c) const { + return c<2?(float)ref._linear_atXY(x,y,(int)z,c):0; + } + }; + + struct _functor4d_streamline3d_directed { + const CImg& ref; + _functor4d_streamline3d_directed(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y, const float z, const unsigned int c) const { + return (float)ref._linear_atXYZ(x,y,z,c); + } + }; + + struct _functor4d_streamline2d_oriented { + const CImg& ref; + CImg *pI; + _functor4d_streamline2d_oriented(const CImg& pref):ref(pref),pI(0) { pI = new CImg(2,2,1,2); } + ~_functor4d_streamline2d_oriented() { delete pI; } + float operator()(const float x, const float y, const float z, const unsigned int c) const { +#define _cimg_vecalign2d(i,j) if (I(i,j,0)*I(0,0,0)+I(i,j,1)*I(0,0,1)<0) { I(i,j,0) = -I(i,j,0); I(i,j,1) = -I(i,j,1); } + int + xi = (int)x - (x>=0?0:1), nxi = xi + 1, + yi = (int)y - (y>=0?0:1), nyi = yi + 1, + zi = (int)z; + const float + dx = x - xi, + dy = y - yi; + if (c==0) { + CImg& I = *pI; + if (xi<0) xi = 0; if (nxi<0) nxi = 0; + if (xi>=ref.width()) xi = ref.width()-1; if (nxi>=ref.width()) nxi = ref.width()-1; + if (yi<0) yi = 0; if (nyi<0) nyi = 0; + if (yi>=ref.height()) yi = ref.height()-1; if (nyi>=ref.height()) nyi = ref.height()-1; + I(0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,1) = (float)ref(xi,yi,zi,1); + I(1,0,0) = (float)ref(nxi,yi,zi,0); I(1,0,1) = (float)ref(nxi,yi,zi,1); + I(1,1,0) = (float)ref(nxi,nyi,zi,0); I(1,1,1) = (float)ref(nxi,nyi,zi,1); + I(0,1,0) = (float)ref(xi,nyi,zi,0); I(0,1,1) = (float)ref(xi,nyi,zi,1); + _cimg_vecalign2d(1,0); _cimg_vecalign2d(1,1); _cimg_vecalign2d(0,1); + } + return c<2?(float)pI->_linear_atXY(dx,dy,0,c):0; + } + }; + + struct _functor4d_streamline3d_oriented { + const CImg& ref; + CImg *pI; + _functor4d_streamline3d_oriented(const CImg& pref):ref(pref),pI(0) { pI = new CImg(2,2,2,3); } + ~_functor4d_streamline3d_oriented() { delete pI; } + float operator()(const float x, const float y, const float z, const unsigned int c) const { +#define _cimg_vecalign3d(i,j,k) if (I(i,j,k,0)*I(0,0,0,0)+I(i,j,k,1)*I(0,0,0,1)+I(i,j,k,2)*I(0,0,0,2)<0) { \ + I(i,j,k,0) = -I(i,j,k,0); I(i,j,k,1) = -I(i,j,k,1); I(i,j,k,2) = -I(i,j,k,2); } + int + xi = (int)x - (x>=0?0:1), nxi = xi + 1, + yi = (int)y - (y>=0?0:1), nyi = yi + 1, + zi = (int)z - (z>=0?0:1), nzi = zi + 1; + const float + dx = x - xi, + dy = y - yi, + dz = z - zi; + if (c==0) { + CImg& I = *pI; + if (xi<0) xi = 0; if (nxi<0) nxi = 0; + if (xi>=ref.width()) xi = ref.width()-1; if (nxi>=ref.width()) nxi = ref.width()-1; + if (yi<0) yi = 0; if (nyi<0) nyi = 0; + if (yi>=ref.height()) yi = ref.height()-1; if (nyi>=ref.height()) nyi = ref.height()-1; + if (zi<0) zi = 0; if (nzi<0) nzi = 0; + if (zi>=ref.depth()) zi = ref.depth()-1; if (nzi>=ref.depth()) nzi = ref.depth()-1; + I(0,0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,0,1) = (float)ref(xi,yi,zi,1); + I(0,0,0,2) = (float)ref(xi,yi,zi,2); I(1,0,0,0) = (float)ref(nxi,yi,zi,0); + I(1,0,0,1) = (float)ref(nxi,yi,zi,1); I(1,0,0,2) = (float)ref(nxi,yi,zi,2); + I(1,1,0,0) = (float)ref(nxi,nyi,zi,0); I(1,1,0,1) = (float)ref(nxi,nyi,zi,1); + I(1,1,0,2) = (float)ref(nxi,nyi,zi,2); I(0,1,0,0) = (float)ref(xi,nyi,zi,0); + I(0,1,0,1) = (float)ref(xi,nyi,zi,1); I(0,1,0,2) = (float)ref(xi,nyi,zi,2); + I(0,0,1,0) = (float)ref(xi,yi,nzi,0); I(0,0,1,1) = (float)ref(xi,yi,nzi,1); + I(0,0,1,2) = (float)ref(xi,yi,nzi,2); I(1,0,1,0) = (float)ref(nxi,yi,nzi,0); + I(1,0,1,1) = (float)ref(nxi,yi,nzi,1); I(1,0,1,2) = (float)ref(nxi,yi,nzi,2); + I(1,1,1,0) = (float)ref(nxi,nyi,nzi,0); I(1,1,1,1) = (float)ref(nxi,nyi,nzi,1); + I(1,1,1,2) = (float)ref(nxi,nyi,nzi,2); I(0,1,1,0) = (float)ref(xi,nyi,nzi,0); + I(0,1,1,1) = (float)ref(xi,nyi,nzi,1); I(0,1,1,2) = (float)ref(xi,nyi,nzi,2); + _cimg_vecalign3d(1,0,0); _cimg_vecalign3d(1,1,0); _cimg_vecalign3d(0,1,0); + _cimg_vecalign3d(0,0,1); _cimg_vecalign3d(1,0,1); _cimg_vecalign3d(1,1,1); _cimg_vecalign3d(0,1,1); + } + return (float)pI->_linear_atXYZ(dx,dy,dz,c); + } + }; + + struct _functor4d_streamline_expr { + _cimg_math_parser *mp; + ~_functor4d_streamline_expr() { delete mp; } + _functor4d_streamline_expr(const char *const expr):mp(0) { + mp = new _cimg_math_parser(CImg::empty(),expr,"streamline"); + } + float operator()(const float x, const float y, const float z, const unsigned int c) const { + return (float)(*mp)(x,y,z,c); + } + }; + + //! Return a shared-memory image referencing a range of pixels of the image instance. + /** + \param x0 X-coordinate of the starting pixel. + \param x1 X-coordinate of the ending pixel. + \param y0 Y-coordinate. + \param z0 Z-coordinate. + \param c0 C-coordinate. + **/ + CImg get_shared_points(const unsigned int x0, const unsigned int x1, + const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) { + const unsigned int beg = (unsigned int)offset(x0,y0,z0,c0), end = offset(x1,y0,z0,c0); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).", + cimg_instance, + x0,x1,y0,z0,c0); + + return CImg(_data+beg,x1-x0+1,1,1,1,true); + } + + //! Return a shared-memory image referencing a range of pixels of the image instance \const. + const CImg get_shared_points(const unsigned int x0, const unsigned int x1, + const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) const { + const unsigned int beg = (unsigned int)offset(x0,y0,z0,c0), end = offset(x1,y0,z0,c0); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).", + cimg_instance, + x0,x1,y0,z0,c0); + + return CImg(_data+beg,x1-x0+1,1,1,1,true); + } + + //! Return a shared-memory image referencing a range of rows of the image instance. + /** + \param y0 Y-coordinate of the starting row. + \param y1 Y-coordinate of the ending row. + \param z0 Z-coordinate. + \param c0 C-coordinate. + **/ + CImg get_shared_rows(const unsigned int y0, const unsigned int y1, + const unsigned int z0=0, const unsigned int c0=0) { + const unsigned int beg = offset(0,y0,z0,c0), end = offset(0,y1,z0,c0); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_rows(): Invalid request of a shared-memory subset " + "(0->%u,%u->%u,%u,%u).", + cimg_instance, + _width-1,y0,y1,z0,c0); + + return CImg(_data+beg,_width,y1-y0+1,1,1,true); + } + + //! Return a shared-memory image referencing a range of rows of the image instance \const. + const CImg get_shared_rows(const unsigned int y0, const unsigned int y1, + const unsigned int z0=0, const unsigned int c0=0) const { + const unsigned int beg = offset(0,y0,z0,c0), end = offset(0,y1,z0,c0); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_rows(): Invalid request of a shared-memory subset " + "(0->%u,%u->%u,%u,%u).", + cimg_instance, + _width-1,y0,y1,z0,c0); + + return CImg(_data+beg,_width,y1-y0+1,1,1,true); + } + + //! Return a shared-memory image referencing one row of the image instance. + /** + \param y0 Y-coordinate. + \param z0 Z-coordinate. + \param c0 C-coordinate. + **/ + CImg get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) { + return get_shared_rows(y0,y0,z0,c0); + } + + //! Return a shared-memory image referencing one row of the image instance \const. + const CImg get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) const { + return get_shared_rows(y0,y0,z0,c0); + } + + //! Return a shared memory image referencing a range of slices of the image instance. + /** + \param z0 Z-coordinate of the starting slice. + \param z1 Z-coordinate of the ending slice. + \param c0 C-coordinate. + **/ + CImg get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) { + const unsigned int beg = offset(0,0,z0,c0), end = offset(0,0,z1,c0); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_slices(): Invalid request of a shared-memory subset " + "(0->%u,0->%u,%u->%u,%u).", + cimg_instance, + _width-1,_height-1,z0,z1,c0); + + return CImg(_data+beg,_width,_height,z1-z0+1,1,true); + } + + //! Return a shared memory image referencing a range of slices of the image instance \const. + const CImg get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) const { + const unsigned int beg = offset(0,0,z0,c0), end = offset(0,0,z1,c0); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_slices(): Invalid request of a shared-memory subset " + "(0->%u,0->%u,%u->%u,%u).", + cimg_instance, + _width-1,_height-1,z0,z1,c0); + + return CImg(_data+beg,_width,_height,z1-z0+1,1,true); + } + + //! Return a shared-memory image referencing one slice of the image instance. + /** + \param z0 Z-coordinate. + \param c0 C-coordinate. + **/ + CImg get_shared_slice(const unsigned int z0, const unsigned int c0=0) { + return get_shared_slices(z0,z0,c0); + } + + //! Return a shared-memory image referencing one slice of the image instance \const. + const CImg get_shared_slice(const unsigned int z0, const unsigned int c0=0) const { + return get_shared_slices(z0,z0,c0); + } + + //! Return a shared-memory image referencing a range of channels of the image instance. + /** + \param c0 C-coordinate of the starting channel. + \param c1 C-coordinate of the ending channel. + **/ + CImg get_shared_channels(const unsigned int c0, const unsigned int c1) { + const unsigned int beg = offset(0,0,0,c0), end = offset(0,0,0,c1); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_channels(): Invalid request of a shared-memory subset " + "(0->%u,0->%u,0->%u,%u->%u).", + cimg_instance, + _width-1,_height-1,_depth-1,c0,c1); + + return CImg(_data+beg,_width,_height,_depth,c1-c0+1,true); + } + + //! Return a shared-memory image referencing a range of channels of the image instance \const. + const CImg get_shared_channels(const unsigned int c0, const unsigned int c1) const { + const unsigned int beg = offset(0,0,0,c0), end = offset(0,0,0,c1); + if (beg>end || beg>=size() || end>=size()) + throw CImgArgumentException(_cimg_instance + "get_shared_channels(): Invalid request of a shared-memory subset " + "(0->%u,0->%u,0->%u,%u->%u).", + cimg_instance, + _width-1,_height-1,_depth-1,c0,c1); + + return CImg(_data+beg,_width,_height,_depth,c1-c0+1,true); + } + + //! Return a shared-memory image referencing one channel of the image instance. + /** + \param c0 C-coordinate. + **/ + CImg get_shared_channel(const unsigned int c0) { + return get_shared_channels(c0,c0); + } + + //! Return a shared-memory image referencing one channel of the image instance \const. + const CImg get_shared_channel(const unsigned int c0) const { + return get_shared_channels(c0,c0); + } + + //! Return a shared-memory version of the image instance. + CImg get_shared() { + return CImg(_data,_width,_height,_depth,_spectrum,true); + } + + //! Return a shared-memory version of the image instance \const. + const CImg get_shared() const { + return CImg(_data,_width,_height,_depth,_spectrum,true); + } + + //! Split image into a list along specified axis. + /** + \param axis Splitting axis. Can be { 'x' | 'y' | 'z' | 'c' }. + \param nb Number of splitted parts. + \note + - If \c nb==0, there are as much splitted parts as the image size along the specified axis. + - If \c nb<=0, instance image is splitted into blocs of -\c nb pixel wide. + - If \c nb>0, instance image is splitted into \c nb blocs. + **/ + CImgList get_split(const char axis, const int nb=0) const { + CImgList res; + if (is_empty()) return res; + const char _axis = cimg::uncase(axis); + + if (nb<=0) { // Split by bloc size. + const unsigned int dp = (unsigned int)(nb?-nb:1); + switch (_axis) { + case 'x': { + if (_width>dp) { + res.assign(_width/dp+(_width%dp?1:0),1,1); + const unsigned int pe = _width - dp; +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._width>=128 && _height*_depth*_spectrum>=128) +#endif + for (unsigned int p = 0; pdp) { + res.assign(_height/dp+(_height%dp?1:0),1,1); + const unsigned int pe = _height - dp; +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._width>=128 && _width*_depth*_spectrum>=128) +#endif + for (unsigned int p = 0; pdp) { + res.assign(_depth/dp+(_depth%dp?1:0),1,1); + const unsigned int pe = _depth - dp; +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._width>=128 && _width*_height*_spectrum>=128) +#endif + for (unsigned int p = 0; pdp) { + res.assign(_spectrum/dp+(_spectrum%dp?1:0),1,1); + const unsigned int pe = _spectrum - dp; +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._width>=128 && _width*_height*_depth>=128) +#endif + for (unsigned int p = 0; psiz) + throw CImgArgumentException(_cimg_instance + "get_split(): Instance cannot be split along %c-axis into %u blocs.", + cimg_instance, + axis,nb); + if (nb==1) res.assign(*this); + else { + int err = (int)siz; + unsigned int _p = 0; + switch (_axis) { + case 'x' : { + cimg_forX(*this,p) if ((err-=nb)<=0) { + get_crop(_p,0,0,0,p,_height-1,_depth-1,_spectrum-1).move_to(res); + err+=(int)siz; + _p=p+1; + } + } break; + case 'y' : { + cimg_forY(*this,p) if ((err-=nb)<=0) { + get_crop(0,_p,0,0,_width-1,p,_depth-1,_spectrum-1).move_to(res); + err+=(int)siz; + _p=p+1; + } + } break; + case 'z' : { + cimg_forZ(*this,p) if ((err-=nb)<=0) { + get_crop(0,0,_p,0,_width-1,_height-1,p,_spectrum-1).move_to(res); + err+=(int)siz; + _p=p+1; + } + } break; + default : { + cimg_forC(*this,p) if ((err-=nb)<=0) { + get_crop(0,0,0,_p,_width-1,_height-1,_depth-1,p).move_to(res); + err+=(int)siz; + _p=p+1; + } + } + } + } + } + return res; + } + + //! Split image into a list of one-column vectors, according to a specified splitting value. + /** + \param value Splitting value. + \param keep_values Tells if the splitting value must be kept in the splitted blocs. + \param is_shared Tells if the splitted blocs have shared memory buffers. + **/ + CImgList get_split(const T value, const bool keep_values, const bool is_shared) const { + CImgList res; + if (is_empty()) return res; + for (const T *ps = _data, *_ps = ps, *const pe = end(); ps(ps,1,siz,1,1,is_shared),~0U,is_shared); + ps = _ps; + while (_ps(ps,1,siz,1,1,is_shared),~0U,is_shared); + ps = _ps; + } + return res; + } + + //! Split image into a list of one-column vectors, according to a specified splitting value sequence. + /** + \param values Splitting value sequence. + \param keep_values Tells if the splitting sequence must be kept in the splitted blocs. + \param is_shared Tells if the splitted blocs have shared memory buffers. + **/ + template + CImgList get_split(const CImg& values, const bool keep_values, const bool is_shared) const { + CImgList res; + if (is_empty()) return res; + if (!values) return CImgList(*this); + if (values.size()==1) return get_split(*values,keep_values,is_shared); + const t *pve = values.end(); + for (const T *ps = _data, *_ps = ps, *const pe = end(); ps(ps,1,siz,1,1,is_shared),~0U,is_shared); // If match found. + ps = _ps; + + // Try to find non-match from current position. + do { + pv = values._data; + while (_ps(ps,1,siz,1,1,is_shared),~0U,is_shared); + ps = _ps; + } + return res; + } + + //! Split the image into a list of one-column vectors each having same values. + CImgList get_split(const bool is_shared) const { + CImgList res; + if (is_empty()) return res; + T *p0 = _data, current = *p0; + cimg_for(*this,p,T) if (*p!=current) { + res.insert(CImg(p0,1,p-p0,1,1,is_shared),~0U,is_shared); p0 = p; current = *p; + } + res.insert(CImg(p0,1,end()-p0,1,1,is_shared),~0U,is_shared); + return res; + } + + //! Append two images along specified axis. + /** + \param img Image to append with instance image. + \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Append alignment in \c [0,1]. + **/ + template + CImg& append(const CImg& img, const char axis='x', const float align=0) { + if (is_empty()) return assign(img,false); + if (!img) return *this; + return CImgList(*this,true).insert(img).get_append(axis,align).move_to(*this); + } + + //! Append two images along specified axis \specialization. + CImg& append(const CImg& img, const char axis='x', const float align=0) { + if (is_empty()) return assign(img,false); + if (!img) return *this; + return CImgList(*this,img,true).get_append(axis,align).move_to(*this); + } + + //! Append two images along specified axis \const. + template + CImg<_cimg_Tt> get_append(const CImg& img, const char axis='x', const float align=0) const { + if (is_empty()) return +img; + if (!img) return +*this; + return CImgList<_cimg_Tt>(*this,true).insert(img).get_append(axis,align); + } + + //! Append two images along specified axis \specialization. + CImg get_append(const CImg& img, const char axis='x', const float align=0) const { + if (is_empty()) return +img; + if (!img) return +*this; + return CImgList(*this,img,true).get_append(axis,align); + } + + //@} + //--------------------------------------- + // + //! \name Filtering / Transforms + //@{ + //--------------------------------------- + + //! Correlate image by a mask. + /** + \param mask = the correlation kernel. + \param boundary_conditions = the border condition type (0=zero, 1=dirichlet) + \param is_normalized = enable local normalization. + \note + - The correlation of the image instance \p *this by the mask \p mask is defined to be: + res(x,y,z) = sum_{i,j,k} (*this)(x+i,y+j,z+k)*mask(i,j,k). + **/ + template + CImg& correlate(const CImg& mask, const unsigned int boundary_conditions=1, const bool is_normalized=false) { + if (is_empty() || !mask) return *this; + return get_correlate(mask,boundary_conditions,is_normalized).move_to(*this); + } + + //! Correlate image by a mask \newinstance. + template + CImg<_cimg_Ttfloat> get_correlate(const CImg& mask, const unsigned int boundary_conditions=1, + const bool is_normalized=false) const { + if (is_empty() || !mask) return *this; + typedef _cimg_Ttfloat Ttfloat; + CImg res(_width,_height,_depth,cimg::max(_spectrum,mask._spectrum)); + if (boundary_conditions && mask._width==mask._height && + ((mask._depth==1 && mask._width<=5) || (mask._depth==mask._width && mask._width<=3))) { + // A special optimization is done for 2x2, 3x3, 4x4, 5x5, 2x2x2 and 3x3x3 mask (with boundary_conditions=1) + Ttfloat *ptrd = res._data; + switch (mask._depth) { + case 3 : { + T I[27] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_for3x3x3(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + + I[ 3]*I[ 3] + I[ 4]*I[ 4] + I[ 5]*I[ 5] + + I[ 6]*I[ 6] + I[ 7]*I[ 7] + I[ 8]*I[ 8] + + I[ 9]*I[ 9] + I[10]*I[10] + I[11]*I[11] + + I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + + I[15]*I[15] + I[16]*I[16] + I[17]*I[17] + + I[18]*I[18] + I[19]*I[19] + I[20]*I[20] + + I[21]*I[21] + I[22]*I[22] + I[23]*I[23] + + I[24]*I[24] + I[25]*I[25] + I[26]*I[26]); + *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + + I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + + I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26])/std::sqrt(N):0); + } + } else cimg_for3x3x3(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + + I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + + I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26]); + } + } break; + case 2 : { + T I[8] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_for2x2x2(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[0]*I[0] + I[1]*I[1] + + I[2]*I[2] + I[3]*I[3] + + I[4]*I[4] + I[5]*I[5] + + I[6]*I[6] + I[7]*I[7]); + *(ptrd++) = (Ttfloat)(N?(I[0]*_mask[0] + I[1]*_mask[1] + + I[2]*_mask[2] + I[3]*_mask[3] + + I[4]*_mask[4] + I[5]*_mask[5] + + I[6]*_mask[6] + I[7]*_mask[7])/std::sqrt(N):0); + } + } else cimg_for2x2x2(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[0]*_mask[0] + I[1]*_mask[1] + + I[2]*_mask[2] + I[3]*_mask[3] + + I[4]*_mask[4] + I[5]*_mask[5] + + I[6]*_mask[6] + I[7]*_mask[7]); + } + } break; + default : + case 1 : + switch (mask._width) { + case 6 : { + T I[36] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_forZ(_img,z) cimg_for6x6(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + I[ 3]*I[ 3] + I[ 4]*I[ 4] + I[ 5]*I[ 5] + + I[ 6]*I[ 6] + I[ 7]*I[ 7] + I[ 8]*I[ 8] + I[ 9]*I[ 9] + I[10]*I[10] + I[11]*I[11] + + I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + I[15]*I[15] + I[16]*I[16] + I[17]*I[17] + + I[18]*I[18] + I[19]*I[19] + I[20]*I[20] + I[21]*I[21] + I[22]*I[22] + I[23]*I[23] + + I[24]*I[24] + I[25]*I[25] + I[26]*I[26] + I[27]*I[27] + I[28]*I[28] + I[29]*I[29] + + I[30]*I[30] + I[31]*I[31] + I[32]*I[32] + I[33]*I[33] + I[34]*I[34] + I[35]*I[35]); + *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + + I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + + I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26] + I[27]*_mask[27] + I[28]*_mask[28] + I[29]*_mask[29] + + I[30]*_mask[30] + I[31]*_mask[31] + I[32]*_mask[32] + I[33]*_mask[33] + I[34]*_mask[34] + I[35]*_mask[35])/std::sqrt(N):0); + } + } else cimg_forZ(_img,z) cimg_for6x6(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + + I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + + I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26] + I[27]*_mask[27] + I[28]*_mask[28] + I[29]*_mask[29] + + I[30]*_mask[30] + I[31]*_mask[31] + I[32]*_mask[32] + I[33]*_mask[33] + I[34]*_mask[34] + I[35]*_mask[35]); + } + } break; + case 5 : { + T I[25] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_forZ(_img,z) cimg_for5x5(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + I[ 3]*I[ 3] + I[ 4]*I[ 4] + + I[ 5]*I[ 5] + I[ 6]*I[ 6] + I[ 7]*I[ 7] + I[ 8]*I[ 8] + I[ 9]*I[ 9] + + I[10]*I[10] + I[11]*I[11] + I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + + I[15]*I[15] + I[16]*I[16] + I[17]*I[17] + I[18]*I[18] + I[19]*I[19] + + I[20]*I[20] + I[21]*I[21] + I[22]*I[22] + I[23]*I[23] + I[24]*I[24]); + *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + + I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + + I[10]*_mask[10] + I[11]*_mask[11] + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + I[18]*_mask[18] + I[19]*_mask[19] + + I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + I[24]*_mask[24])/std::sqrt(N):0); + } + } else cimg_forZ(_img,z) cimg_for5x5(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + + I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + + I[10]*_mask[10] + I[11]*_mask[11] + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + I[18]*_mask[18] + I[19]*_mask[19] + + I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + I[24]*_mask[24]); + } + } break; + case 4 : { + T I[16] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_forZ(_img,z) cimg_for4x4(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + I[ 3]*I[ 3] + + I[ 4]*I[ 4] + I[ 5]*I[ 5] + I[ 6]*I[ 6] + I[ 7]*I[ 7] + + I[ 8]*I[ 8] + I[ 9]*I[ 9] + I[10]*I[10] + I[11]*I[11] + + I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + I[15]*I[15]); + *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15])/std::sqrt(N):0); + } + } else cimg_forZ(_img,z) cimg_for4x4(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15]); + } + } break; + case 3 : { + T I[9] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_forZ(_img,z) cimg_for3x3(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[0]*I[0] + I[1]*I[1] + I[2]*I[2] + + I[3]*I[3] + I[4]*I[4] + I[5]*I[5] + + I[6]*I[6] + I[7]*I[7] + I[8]*I[8]); + *(ptrd++) = (Ttfloat)(N?(I[0]*_mask[0] + I[1]*_mask[1] + I[2]*_mask[2] + + I[3]*_mask[3] + I[4]*_mask[4] + I[5]*_mask[5] + + I[6]*_mask[6] + I[7]*_mask[7] + I[8]*_mask[8])/std::sqrt(N):0); + } + } else cimg_forZ(_img,z) cimg_for3x3(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[0]*_mask[0] + I[1]*_mask[1] + I[2]*_mask[2] + + I[3]*_mask[3] + I[4]*_mask[4] + I[5]*_mask[5] + + I[6]*_mask[6] + I[7]*_mask[7] + I[8]*_mask[8]); + } + } break; + case 2 : { + T I[4] = { 0 }; + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; + cimg_forZ(_img,z) cimg_for2x2(_img,x,y,z,0,I,T) { + const Ttfloat N = M*(I[0]*I[0] + I[1]*I[1] + + I[2]*I[2] + I[3]*I[3]); + *(ptrd++) = (Ttfloat)(N?(I[0]*_mask[0] + I[1]*_mask[1] + + I[2]*_mask[2] + I[3]*_mask[3])/std::sqrt(N):0); + } + } else cimg_forZ(_img,z) cimg_for2x2(_img,x,y,z,0,I,T) + *(ptrd++) = (Ttfloat)(I[0]*_mask[0] + I[1]*_mask[1] + + I[2]*_mask[2] + I[3]*_mask[3]); + } + } break; + case 1 : + if (is_normalized) res.fill(1); + else cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + res.get_shared_channel(c).assign(_img)*=_mask[0]; + } + break; + } + } + } else { // Generic version for other masks and borders conditions. + const int + mx2 = mask.width()/2, my2 = mask.height()/2, mz2 = mask.depth()/2, + mx1 = mx2 - 1 + (mask.width()%2), my1 = my2 - 1 + (mask.height()%2), mz1 = mz2 - 1 + (mask.depth()%2), + mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2; +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._spectrum>=2) +#endif + cimg_forC(res,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { // Normalized correlation. + const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) +#endif + for (int z = mz1; z=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Ttfloat val = 0, N = 0; + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const Ttfloat _val = (Ttfloat)_img._atXYZ(x+xm,y+ym,z+zm); + val+=_val*_mask(mx1+xm,my1+ym,mz1+zm); + N+=_val*_val; + } + N*=M; + res(x,y,z,c) = (Ttfloat)(N?val/std::sqrt(N):0); + } + else +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Ttfloat val = 0, N = 0; + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const Ttfloat _val = (Ttfloat)_img.atXYZ(x+xm,y+ym,z+zm,0,0); + val+=_val*_mask(mx1+xm,my1+ym,mz1+zm); + N+=_val*_val; + } + N*=M; + res(x,y,z,c) = (Ttfloat)(N?val/std::sqrt(N):0); + } + } else { // Classical correlation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) +#endif + for (int z = mz1; z=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Ttfloat val = 0; + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) + val+=_img._atXYZ(x+xm,y+ym,z+zm)*_mask(mx1+xm,my1+ym,mz1+zm); + res(x,y,z,c) = (Ttfloat)val; + } + else +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Ttfloat val = 0; + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) + val+=_img.atXYZ(x+xm,y+ym,z+zm,0,0)*_mask(mx1+xm,my1+ym,mz1+zm); + res(x,y,z,c) = (Ttfloat)val; + } + } + } + } + return res; + } + + //! Convolve image by a mask. + /** + \param mask = the correlation kernel. + \param boundary_conditions = the border condition type (0=zero, 1=dirichlet) + \param is_normalized = enable local normalization. + \note + - The result \p res of the convolution of an image \p img by a mask \p mask is defined to be: + res(x,y,z) = sum_{i,j,k} img(x-i,y-j,z-k)*mask(i,j,k) + **/ + template + CImg& convolve(const CImg& mask, const unsigned int boundary_conditions=1, const bool is_normalized=false) { + if (is_empty() || !mask) return *this; + return get_convolve(mask,boundary_conditions,is_normalized).move_to(*this); + } + + //! Convolve image by a mask \newinstance. + template + CImg<_cimg_Ttfloat> get_convolve(const CImg& mask, const unsigned int boundary_conditions=1, + const bool is_normalized=false) const { + if (is_empty() || !mask) return *this; + return get_correlate(CImg(mask._data,mask.size(),1,1,1,true).get_mirror('x'). + resize(mask,-1),boundary_conditions,is_normalized); + } + + //! Erode image by a structuring element. + /** + \param mask Structuring element. + \param boundary_conditions Boundary conditions. + \param is_normalized Tells if the erosion is locally normalized. + **/ + template + CImg& erode(const CImg& mask, const unsigned int boundary_conditions=1, + const bool is_normalized=false) { + if (is_empty() || !mask) return *this; + return get_erode(mask,boundary_conditions,is_normalized).move_to(*this); + } + + //! Erode image by a structuring element \newinstance. + template + CImg<_cimg_Tt> get_erode(const CImg& mask, const unsigned int boundary_conditions=1, + const bool is_normalized=false) const { + if (is_empty() || !mask) return *this; + typedef _cimg_Tt Tt; + CImg res(_width,_height,_depth,cimg::max(_spectrum,mask._spectrum)); + const int + mx2 = mask.width()/2, my2 = mask.height()/2, mz2 = mask.depth()/2, + mx1 = mx2 - 1 + (mask.width()%2), my1 = my2 - 1 + (mask.height()%2), mz1 = mz2 - 1 + (mask.depth()%2), + mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { // Normalized erosion. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) +#endif + for (int z = mz1; z::max(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const t mval = _mask(mx1+xm,my1+ym,mz1+zm); + const Tt cval = (Tt)(_img(x+xm,y+ym,z+zm) + mval); + if (mval && cval=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt min_val = cimg::type::max(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const t mval = _mask(mx1+xm,my1+ym,mz1+zm); + const Tt cval = (Tt)(_img._atXYZ(x+xm,y+ym,z+zm) + mval); + if (mval && cval=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt min_val = cimg::type::max(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const t mval = _mask(mx1+xm,my1+ym,mz1+zm); + const Tt cval = (Tt)(_img.atXYZ(x+xm,y+ym,z+zm,0,0) + mval); + if (mval && cval=32768) +#endif + for (int z = mz1; z::max(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const Tt cval = (Tt)_img(x+xm,y+ym,z+zm); + if (_mask(mx1+xm,my1+ym,mz1+zm) && cval=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt min_val = cimg::type::max(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const T cval = (Tt)_img._atXYZ(x+xm,y+ym,z+zm); + if (_mask(mx1+xm,my1+ym,mz1+zm) && cval=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt min_val = cimg::type::max(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const T cval = (Tt)_img.atXYZ(x+xm,y+ym,z+zm,0,0); + if (_mask(mx1+xm,my1+ym,mz1+zm) && cval& erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) { + if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this; + if (sx>1 && _width>1) { // Along X-axis. + const int L = width(), off = 1, s = (int)sx, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, s2 = _s2>L?L:_s2; + CImg buf(L); +#ifdef cimg_use_opemp +#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) +#endif + cimg_forYZC(*this,y,z,c) { + T *const ptrdb = buf._data, *ptrd = buf._data, *const ptrde = buf._data + L - 1; + const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; + T cur = *ptrs; ptrs+=off; bool is_first = true; + for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { + const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }} + *(ptrd++) = cur; + if (ptrs>=ptrse) { + T *pd = data(0,y,z,c); cur = cimg::min(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } + } else { + for (int p = s1; p>0 && ptrd<=ptrde; --p) { + const T val = *ptrs; if (ptrs0; --p) { + const T val = *ptrs; ptrs+=off; + if (is_first) { + const T *nptrs = ptrs - off; cur = val; + for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval0 && ptrs>=ptrsb; --p) { + const T val = *ptrs; ptrs-=off; if (val0 && ptrd>=ptrdb; --p) { + const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val1 && _height>1) { // Along Y-axis. + const int L = height(), off = width(), s = (int)sy, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, + s2 = _s2>L?L:_s2; + CImg buf(L); +#ifdef cimg_use_opemp +#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) +#endif + cimg_forXZC(*this,x,z,c) { + T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; + const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; + T cur = *ptrs; ptrs+=off; bool is_first = true; + for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { + const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; } + } + *(ptrd++) = cur; + if (ptrs>=ptrse) { + T *pd = data(x,0,z,c); cur = cimg::min(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } + } else { + for (int p = s1; p>0 && ptrd<=ptrde; --p) { + const T val = *ptrs; if (ptrs0; --p) { + const T val = *ptrs; ptrs+=off; + if (is_first) { + const T *nptrs = ptrs - off; cur = val; + for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval0 && ptrs>=ptrsb; --p) { + const T val = *ptrs; ptrs-=off; if (val0 && ptrd>=ptrdb; --p) { + const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val1 && _depth>1) { // Along Z-axis. + const int L = depth(), off = width()*height(), s = (int)sz, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, + s2 = _s2>L?L:_s2; + CImg buf(L); +#ifdef cimg_use_opemp +#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) +#endif + cimg_forXYC(*this,x,y,c) { + T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; + const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; + T cur = *ptrs; ptrs+=off; bool is_first = true; + for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { + const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; } + } + *(ptrd++) = cur; + if (ptrs>=ptrse) { + T *pd = data(x,y,0,c); cur = cimg::min(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } + } else { + for (int p = s1; p>0 && ptrd<=ptrde; --p) { + const T val = *ptrs; if (ptrs0; --p) { + const T val = *ptrs; ptrs+=off; + if (is_first) { + const T *nptrs = ptrs - off; cur = val; + for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval0 && ptrs>=ptrsb; --p) { + const T val = *ptrs; ptrs-=off; if (val0 && ptrd>=ptrdb; --p) { + const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val get_erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const { + return (+*this).erode(sx,sy,sz); + } + + //! Erode the image by a square structuring element of specified size. + /** + \param s Size of the structuring element. + **/ + CImg& erode(const unsigned int s) { + return erode(s,s,s); + } + + //! Erode the image by a square structuring element of specified size \newinstance. + CImg get_erode(const unsigned int s) const { + return (+*this).erode(s); + } + + //! Dilate image by a structuring element. + /** + \param mask Structuring element. + \param boundary_conditions Boundary conditions. + \param is_normalized Tells if the erosion is locally normalized. + **/ + template + CImg& dilate(const CImg& mask, const unsigned int boundary_conditions=1, + const bool is_normalized=false) { + if (is_empty() || !mask) return *this; + return get_dilate(mask,boundary_conditions,is_normalized).move_to(*this); + } + + //! Dilate image by a structuring element \newinstance. + template + CImg<_cimg_Tt> get_dilate(const CImg& mask, const unsigned int boundary_conditions=1, + const bool is_normalized=false) const { + if (is_empty() || !mask) return *this; + typedef _cimg_Tt Tt; + CImg res(_width,_height,_depth,_spectrum); + const int + mx2 = mask.width()/2, my2 = mask.height()/2, mz2 = mask.depth()/2, + mx1 = mx2 - 1 + (mask.width()%2), my1 = my2 - 1 + (mask.height()%2), mz1 = mz2 - 1 + (mask.depth()%2), + mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + const CImg _img = get_shared_channel(c%_spectrum); + const CImg _mask = mask.get_shared_channel(c%mask._spectrum); + if (is_normalized) { // Normalized dilation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) +#endif + for (int z = mz1; z::min(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const t mval = _mask(mx1+xm,my1+ym,mz1+zm); + const Tt cval = (Tt)(_img(x+xm,y+ym,z+zm) - mval); + if (mval && cval>max_val) max_val = cval; + } + res(x,y,z,c) = max_val; + } + if (boundary_conditions) +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt max_val = cimg::type::min(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const t mval = _mask(mx1+xm,my1+ym,mz1+zm); + const Tt cval = (Tt)(_img._atXYZ(x+xm,y+ym,z+zm) - mval); + if (mval && cval>max_val) max_val = cval; + } + res(x,y,z,c) = max_val; + } + else +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) +#endif + cimg_forYZ(*this,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt max_val = cimg::type::min(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const t mval = _mask(mx1+xm,my1+ym,mz1+zm); + const Tt cval = (Tt)(_img.atXYZ(x+xm,y+ym,z+zm,0,0) - mval); + if (mval && cval>max_val) max_val = cval; + } + res(x,y,z,c) = max_val; + } + } else { // Classical dilation. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth>=128) +#endif + for (int z = mz1; z::min(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const Tt cval = (Tt)_img(x+xm,y+ym,z+zm); + if (_mask(mx1+xm,my1+ym,mz1+zm) && cval>max_val) max_val = cval; + } + res(x,y,z,c) = max_val; + } + if (boundary_conditions) +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt max_val = cimg::type::min(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const T cval = (Tt)_img._atXYZ(x+xm,y+ym,z+zm); + if (_mask(mx1+xm,my1+ym,mz1+zm) && cval>max_val) max_val = cval; + } + res(x,y,z,c) = max_val; + } + else +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) +#endif + cimg_forYZ(res,y,z) + for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { + Tt max_val = cimg::type::min(); + for (int zm = -mz1; zm<=mz2; ++zm) + for (int ym = -my1; ym<=my2; ++ym) + for (int xm = -mx1; xm<=mx2; ++xm) { + const T cval = (Tt)_img.atXYZ(x+xm,y+ym,z+zm,0,0); + if (_mask(mx1+xm,my1+ym,mz1+zm) && cval>max_val) max_val = cval; + } + res(x,y,z,c) = max_val; + } + } + } + return res; + } + + //! Dilate image by a rectangular structuring element of specified size. + /** + \param sx Width of the structuring element. + \param sy Height of the structuring element. + \param sz Depth of the structuring element. + **/ + CImg& dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) { + if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this; + if (sx>1 && _width>1) { // Along X-axis. + const int L = width(), off = 1, s = (int)sx, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, s2 = _s2>L?L:_s2; + CImg buf(L); +#ifdef cimg_use_opemp +#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) +#endif + cimg_forYZC(*this,y,z,c) { + T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; + const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; + T cur = *ptrs; ptrs+=off; bool is_first = true; + for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { + const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; } + } + *(ptrd++) = cur; + if (ptrs>=ptrse) { + T *pd = data(0,y,z,c); cur = cimg::max(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } + } else { + for (int p = s1; p>0 && ptrd<=ptrde; --p) { + const T val = *ptrs; if (ptrs=cur) { cur = val; is_first = false; } + *(ptrd++) = cur; + } + for (int p = L - s - 1; p>0; --p) { + const T val = *ptrs; ptrs+=off; + if (is_first) { + const T *nptrs = ptrs - off; cur = val; + for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; } + nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false; + } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; } + *(ptrd++) = cur; + } + ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off; + for (int p = s1; p>0 && ptrs>=ptrsb; --p) { + const T val = *ptrs; ptrs-=off; if (val>cur) cur = val; + } + *(ptrd--) = cur; + for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) { + const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur; + } + T *pd = data(0,y,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; } + } + } + } + + if (sy>1 && _height>1) { // Along Y-axis. + const int L = height(), off = width(), s = (int)sy, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, + s2 = _s2>L?L:_s2; + CImg buf(L); +#ifdef cimg_use_opemp +#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) +#endif + cimg_forXZC(*this,x,z,c) { + T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; + const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; + T cur = *ptrs; ptrs+=off; bool is_first = true; + for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { + const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; } + } + *(ptrd++) = cur; + if (ptrs>=ptrse) { + T *pd = data(x,0,z,c); cur = cimg::max(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } + } else { + for (int p = s1; p>0 && ptrd<=ptrde; --p) { + const T val = *ptrs; if (ptrs=cur) { cur = val; is_first = false; } + *(ptrd++) = cur; + } + for (int p = L - s - 1; p>0; --p) { + const T val = *ptrs; ptrs+=off; + if (is_first) { + const T *nptrs = ptrs - off; cur = val; + for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; } + nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false; + } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; } + *(ptrd++) = cur; + } + ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off; + for (int p = s1; p>0 && ptrs>=ptrsb; --p) { + const T val = *ptrs; ptrs-=off; if (val>cur) cur = val; + } + *(ptrd--) = cur; + for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) { + const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur; + } + T *pd = data(x,0,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; } + } + } + } + + if (sz>1 && _depth>1) { // Along Z-axis. + const int L = depth(), off = width()*height(), s = (int)sz, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, + s2 = _s2>L?L:_s2; + CImg buf(L); +#ifdef cimg_use_opemp +#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) +#endif + cimg_forXYC(*this,x,y,c) { + T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; + const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; + T cur = *ptrs; ptrs+=off; bool is_first = true; + for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { + const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; } + } + *(ptrd++) = cur; + if (ptrs>=ptrse) { + T *pd = data(x,y,0,c); cur = cimg::max(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } + } else { + for (int p = s1; p>0 && ptrd<=ptrde; --p) { + const T val = *ptrs; if (ptrs=cur) { cur = val; is_first = false; } + *(ptrd++) = cur; + } + for (int p = L - s - 1; p>0; --p) { + const T val = *ptrs; ptrs+=off; + if (is_first) { + const T *nptrs = ptrs - off; cur = val; + for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; } + nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false; + } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; } + *(ptrd++) = cur; + } + ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off; + for (int p = s1; p>0 && ptrs>=ptrsb; --p) { + const T val = *ptrs; ptrs-=off; if (val>cur) cur = val; + } + *(ptrd--) = cur; + for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) { + const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur; + } + T *pd = data(x,y,0,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; } + } + } + } + return *this; + } + + //! Dilate image by a rectangular structuring element of specified size \newinstance. + CImg get_dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const { + return (+*this).dilate(sx,sy,sz); + } + + //! Dilate image by a square structuring element of specified size. + /** + \param s Size of the structuring element. + **/ + CImg& dilate(const unsigned int s) { + return dilate(s,s,s); + } + + //! Dilate image by a square structuring element of specified size \newinstance. + CImg get_dilate(const unsigned int s) const { + return (+*this).dilate(s); + } + + //! Compute watershed transform. + /** + \param priority Priority map. + \param fill_lines Tells if watershed lines must be filled or not. + \note Non-zero values of the instance instance are propagated to zero-valued ones according to + specified the priority map. + **/ + template + CImg& watershed(const CImg& priority, const bool fill_lines=true) { + if (is_empty()) return *this; + if (!is_sameXYZ(priority)) + throw CImgArgumentException(_cimg_instance + "watershed(): image instance and specified priority (%u,%u,%u,%u,%p) " + "have different dimensions.", + cimg_instance, + priority._width,priority._height,priority._depth,priority._spectrum,priority._data); + if (_spectrum!=1) { + cimg_forC(*this,c) + get_shared_channel(c).watershed(priority.get_shared_channel(c%priority._spectrum),fill_lines); + return *this; + } + + CImg is_queued(_width,_height,_depth,1,0); + CImg::type> Q; + unsigned int sizeQ = 0; + + // Find seed points and insert them in priority queue. + const T *ptrs = _data; + cimg_forXYZ(*this,x,y,z) if (*(ptrs++)) { + if (x-1>=0 && !(*this)(x-1,y,z)) Q._priority_queue_insert(is_queued,sizeQ,priority(x-1,y,z),x-1,y,z); + if (x+1=0 && !(*this)(x,y-1,z)) Q._priority_queue_insert(is_queued,sizeQ,priority(x,y-1,z),x,y-1,z); + if (y+1=0 && !(*this)(x,y,z-1)) Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z-1),x,y,z-1); + if (z+1=0) { + if ((*this)(x-1,y,z)) { + if (!label) label = (unsigned int)(*this)(x-1,y,z); + else if (label!=(*this)(x-1,y,z)) is_same_label = false; + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x-1,y,z),x-1,y,z); + } + if (x+1=0) { + if ((*this)(x,y-1,z)) { + if (!label) label = (unsigned int)(*this)(x,y-1,z); + else if (label!=(*this)(x,y-1,z)) is_same_label = false; + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y-1,z),x,y-1,z); + } + if (y+1=0) { + if ((*this)(x,y,z-1)) { + if (!label) label = (unsigned int)(*this)(x,y,z-1); + else if (label!=(*this)(x,y,z-1)) is_same_label = false; + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z-1),x,y,z-1); + } + if (z+1=0 && (*this)(x-1,y,z)) || (x+1=0 && (*this)(x,y-1,z)) || (y+1=0 && (*this)(x,y,z-1)) || (z+1>depth() && (*this)(x,y,z+1)))) + Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z),x,y,z); + + // Start line filling process. + while (sizeQ) { + const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3); + Q._priority_queue_remove(sizeQ); + t pmax = cimg::type::min(); + int xmax = 0, ymax = 0, zmax = 0; + if (x-1>=0) { + if ((*this)(x-1,y,z)) { + if (priority(x-1,y,z)>pmax) { pmax = priority(x-1,y,z); xmax = x-1; ymax = y; zmax = z; } + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x-1,y,z),x-1,y,z); + } + if (x+1pmax) { pmax = priority(x+1,y,z); xmax = x+1; ymax = y; zmax = z; } + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x+1,y,z),x+1,y,z); + } + if (y-1>=0) { + if ((*this)(x,y-1,z)) { + if (priority(x,y-1,z)>pmax) { pmax = priority(x,y-1,z); xmax = x; ymax = y-1; zmax = z; } + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y-1,z),x,y-1,z); + } + if (y+1pmax) { pmax = priority(x,y+1,z); xmax = x; ymax = y+1; zmax = z; } + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y+1,z),x,y+1,z); + } + if (z-1>=0) { + if ((*this)(x,y,z-1)) { + if (priority(x,y,z-1)>pmax) { pmax = priority(x,y,z-1); xmax = x; ymax = y; zmax = z-1; } + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z-1),x,y,z-1); + } + if (z+1pmax) { pmax = priority(x,y,z+1); xmax = x; ymax = y; zmax = z+1; } + } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z+1),x,y,z+1); + } + (*this)(x,y,z) = (*this)(xmax,ymax,zmax); + } + } + return *this; + } + + //! Compute watershed transform \newinstance. + template + CImg get_watershed(const CImg& priority, const bool fill_lines=true) const { + return (+*this).watershed(priority,fill_lines); + } + + // [internal] Insert/Remove items in priority queue, for watershed/distance transforms. + template + bool _priority_queue_insert(CImg& is_queued, unsigned int& siz, const t value, + const unsigned int x, const unsigned int y, const unsigned int z) { + if (is_queued(x,y,z)) return false; + is_queued(x,y,z) = true; + if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); } + (*this)(siz-1,0) = (T)value; (*this)(siz-1,1) = (T)x; (*this)(siz-1,2) = (T)y; (*this)(siz-1,3) = (T)z; + for (unsigned int pos = siz - 1, par = 0; pos && value>(*this)(par=(pos+1)/2-1,0); pos = par) { + cimg::swap((*this)(pos,0),(*this)(par,0)); cimg::swap((*this)(pos,1),(*this)(par,1)); + cimg::swap((*this)(pos,2),(*this)(par,2)); cimg::swap((*this)(pos,3),(*this)(par,3)); + } + return true; + } + + CImg& _priority_queue_remove(unsigned int& siz) { + (*this)(0,0) = (*this)(--siz,0); (*this)(0,1) = (*this)(siz,1); + (*this)(0,2) = (*this)(siz,2); (*this)(0,3) = (*this)(siz,3); + const float value = (*this)(0,0); + for (unsigned int pos = 0, left = 0, right = 0; + ((right=2*(pos+1),(left=right-1))(*this)(right,0)) { + cimg::swap((*this)(pos,0),(*this)(left,0)); cimg::swap((*this)(pos,1),(*this)(left,1)); + cimg::swap((*this)(pos,2),(*this)(left,2)); cimg::swap((*this)(pos,3),(*this)(left,3)); + pos = left; + } else { + cimg::swap((*this)(pos,0),(*this)(right,0)); cimg::swap((*this)(pos,1),(*this)(right,1)); + cimg::swap((*this)(pos,2),(*this)(right,2)); cimg::swap((*this)(pos,3),(*this)(right,3)); + pos = right; + } + } else { + cimg::swap((*this)(pos,0),(*this)(left,0)); cimg::swap((*this)(pos,1),(*this)(left,1)); + cimg::swap((*this)(pos,2),(*this)(left,2)); cimg::swap((*this)(pos,3),(*this)(left,3)); + pos = left; + } + } + return *this; + } + + //! Apply recursive Deriche filter. + /** + \param sigma Standard deviation of the filter. + \param order Order of the filter. Can be { 0=smooth-filter | 1=1st-derivative | 2=2nd-derivative }. + \param axis Axis along which the filter is computed. Can be { 'x' | 'y' | 'z' | 'c' }. + \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }. + **/ + CImg& deriche(const float sigma, const int order=0, const char axis='x', const bool boundary_conditions=true) { +#define _cimg_deriche_apply \ + CImg Y(N); \ + Tfloat *ptrY = Y._data, yb = 0, yp = 0; \ + T xp = (T)0; \ + if (boundary_conditions) { xp = *ptrX; yb = yp = (Tfloat)(coefp*xp); } \ + for (int m = 0; m=0; --n) { \ + const T xc = *(ptrX-=off); \ + const Tfloat yc = (Tfloat)(a2*xn + a3*xa - b1*yn - b2*ya); \ + xa = xn; xn = xc; ya = yn; yn = yc; \ + *ptrX = (T)(*(--ptrY)+yc); \ + } + const char naxis = cimg::uncase(axis); + const float nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:naxis=='y'?_height:naxis=='z'?_depth:_spectrum)/100; + if (is_empty() || (nsigma<0.1f && !order)) return *this; + const float + nnsigma = nsigma<0.1f?0.1f:nsigma, + alpha = 1.695f/nnsigma, + ema = (float)std::exp(-alpha), + ema2 = (float)std::exp(-2*alpha), + b1 = -2*ema, + b2 = ema2; + float a0 = 0, a1 = 0, a2 = 0, a3 = 0, coefp = 0, coefn = 0; + switch (order) { + case 0 : { + const float k = (1-ema)*(1-ema)/(1+2*alpha*ema-ema2); + a0 = k; + a1 = k*(alpha-1)*ema; + a2 = k*(alpha+1)*ema; + a3 = -k*ema2; + } break; + case 1 : { + const float k = -(1-ema)*(1-ema)*(1-ema)/(2*(ema+1)*ema); + a0 = a3 = 0; + a1 = k*ema; + a2 = -a1; + } break; + case 2 : { + const float + ea = (float)std::exp(-alpha), + k = -(ema2-1)/(2*alpha*ema), + kn = (-2*(-1+3*ea-3*ea*ea+ea*ea*ea)/(3*ea+1+3*ea*ea+ea*ea*ea)); + a0 = kn; + a1 = -kn*(1+k*alpha)*ema; + a2 = kn*(1-k*alpha)*ema; + a3 = -kn*ema2; + } break; + default : + throw CImgArgumentException(_cimg_instance + "deriche(): Invalid specified filter order %u " + "(should be { 0=smoothing | 1=1st-derivative | 2=2nd-derivative }).", + cimg_instance, + order); + } + coefp = (a0+a1)/(1+b1+b2); + coefn = (a2+a3)/(1+b1+b2); + switch (naxis) { + case 'x' : { + const int N = _width; + const unsigned long off = 1U; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forYZC(*this,y,z,c) { T *ptrX = data(0,y,z,c); _cimg_deriche_apply; } + } break; + case 'y' : { + const int N = _height; + const unsigned long off = (unsigned long)_width; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forXZC(*this,x,z,c) { T *ptrX = data(x,0,z,c); _cimg_deriche_apply; } + } break; + case 'z' : { + const int N = _depth; + const unsigned long off = (unsigned long)_width*_height; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forXYC(*this,x,y,c) { T *ptrX = data(x,y,0,c); _cimg_deriche_apply; } + } break; + default : { + const int N = _spectrum; + const unsigned long off = (unsigned long)_width*_height*_depth; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forXYZ(*this,x,y,z) { T *ptrX = data(x,y,z,0); _cimg_deriche_apply; } + } + } + return *this; + } + + //! Apply recursive Deriche filter \newinstance. + CImg get_deriche(const float sigma, const int order=0, const char axis='x', + const bool boundary_conditions=true) const { + return CImg(*this,false).deriche(sigma,order,axis,boundary_conditions); + } + + // [internal] Apply a recursive filter (used by CImg::vanvliet()). + /** + \param ptr the pointer of the data + \param filter the coefficient of the filter in the following order [n,n-1,n-2,n-3]. + \param N size of the data + \param off the offset between two data point + \param order the order of the filter 0 (smoothing), 1st derivtive, 2nd derivative, 3rd derivative + \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }. + \note dirichlet boundary conditions have a strange behavior. And + boundary condition should be corrected using Bill Triggs method (IEEE trans on Sig Proc 2005). + **/ + template + static void _cimg_recursive_apply(T *data, const Tfloat filter[], const int N, const unsigned long off, + const int order, const bool boundary_conditions) { + Tfloat val[K]; // res[n,n-1,n-2,n-3,..] or res[n,n+1,n+2,n+3,..] + switch (order) { + case 0 : { + for (int pass = 0; pass<2; ++pass) { + for (int k = 1; k0; --k) val[k] = val[k-1]; + } + if (!pass) data-=off; + } + } break; + case 1 : { + Tfloat x[3]; // [front,center,back] + for (int pass = 0; pass<2; ++pass) { + for (int k = 0; k<3; ++k) x[k] = (Tfloat)(boundary_conditions?*data:0); + for (int k = 0; k0; --k) x[k] = x[k-1]; + } else data-=off; + for (int k = K-1; k>0; --k) val[k] = val[k-1]; + } + *data = (T)0; + } + } break; + case 2: { + Tfloat x[3]; // [front,center,back] + for (int pass = 0; pass<2; ++pass) { + for (int k = 0; k<3; ++k) x[k] = (Tfloat)(boundary_conditions?*data:0); + for (int k = 0; k0; --k) x[k] = x[k-1]; + for (int k = K-1; k>0; --k) val[k] = val[k-1]; + } + *data = (T)0; + } + } break; + case 3: { + Tfloat x[3]; // [front,center,back] + for (int pass = 0; pass<2; ++pass) { + for (int k = 0; k<3; ++k) x[k] = (Tfloat)(boundary_conditions?*data:0); + for (int k = 0; k0; --k) x[k] = x[k-1]; + for (int k = K-1; k>0; --k) val[k] = val[k-1]; + } + *data = (T)0; + } + } break; + } + } + + //! Van Vliet recursive Gaussian filter. + /** + \param sigma standard deviation of the Gaussian filter + \param order the order of the filter 0,1,2,3 + \param axis Axis along which the filter is computed. Can be { 'x' | 'y' | 'z' | 'c' }. + \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }. + \note dirichlet boundary condition has a strange behavior + + Ian T. Young, Lucas J. van Vliet, Recursive implementation of the + Gaussian filter, Signal Processing, Volume 44, Issue 2, June 1995, + Pages 139-151, + **/ + CImg& vanvliet(const float sigma, const int order, const char axis='x', const bool boundary_conditions=true) { + if (is_empty()) return *this; + const char naxis = cimg::uncase(axis); + const float nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:naxis=='y'?_height:naxis=='z'?_depth:_spectrum)/100; + if (is_empty() || (nsigma<0.1f && !order)) return *this; + const Tfloat + nnsigma = nsigma<0.1f?0.1f:nsigma, + q = (Tfloat)(nnsigma<2.5?3.97156-4.14554*std::sqrt(1-0.2689*nnsigma):0.98711*nnsigma-0.96330), + b0 = 1.57825f + 2.44413f*q + 1.4281f*q*q + 0.422205f*q*q*q, + b1 = (2.44413f*q + 2.85619f*q*q + 1.26661f*q*q*q), + b2 = -(1.4281f*q*q + 1.26661f*q*q*q), + b3 = 0.422205f*q*q*q, + B = 1.f - (b1 + b2 + b3)/b0; + Tfloat filter[4]; + filter[0] = B; filter[1] = b1/b0; filter[2] = b2/b0; filter[3] = b3/b0; + + switch (naxis) { + case 'x' : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forYZC(*this,y,z,c) + _cimg_recursive_apply<4>(data(0,y,z,c),filter,_width,1U,order,boundary_conditions); + } break; + case 'y' : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forXZC(*this,x,z,c) + _cimg_recursive_apply<4>(data(x,0,z,c),filter,_height,(unsigned long)_width,order,boundary_conditions); + } break; + case 'z' : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forXYC(*this,x,y,c) + _cimg_recursive_apply<4>(data(x,y,0,c),filter,_depth,(unsigned long)(_width*_height), + order,boundary_conditions); + } break; + default : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) +#endif + cimg_forXYZ(*this,x,y,z) + _cimg_recursive_apply<4>(data(x,y,z,0),filter,_spectrum,(unsigned long)(_width*_height*_depth), + order,boundary_conditions); + } + } + return *this; + } + + //! Blur image using Van Vliet recursive Gaussian filter. \newinstance. + CImg get_vanvliet(const float sigma, const int order, const char axis='x', + const bool boundary_conditions=true) const { + return CImg(*this,false).vanvliet(sigma,order,axis,boundary_conditions); + } + + //! Blur image. + /** + \param sigma_x Standard deviation of the blur, along the X-axis. + \param sigma_y Standard deviation of the blur, along the Y-axis. + \param sigma_z Standard deviation of the blur, along the Z-axis. + \param boundary_conditions Boundary conditions. Can be { false=dirichlet | true=neumann }. + \param is_gaussian Tells if the blur uses a gaussian (\c true) or quasi-gaussian (\c false) kernel. + \note + - The blur is computed as a 0-order Deriche filter. This is not a gaussian blur. + - This is a recursive algorithm, not depending on the values of the standard deviations. + \see deriche(), vanvliet(). + **/ + CImg& blur(const float sigma_x, const float sigma_y, const float sigma_z, + const bool boundary_conditions=true, const bool is_gaussian=false) { + if (!is_empty()) { + if (is_gaussian) { + if (_width>1) vanvliet(sigma_x,0,'x',boundary_conditions); + if (_height>1) vanvliet(sigma_y,0,'y',boundary_conditions); + if (_depth>1) vanvliet(sigma_z,0,'z',boundary_conditions); + } else { + if (_width>1) deriche(sigma_x,0,'x',boundary_conditions); + if (_height>1) deriche(sigma_y,0,'y',boundary_conditions); + if (_depth>1) deriche(sigma_z,0,'z',boundary_conditions); + } + } + return *this; + } + + //! Blur image \newinstance. + CImg get_blur(const float sigma_x, const float sigma_y, const float sigma_z, + const bool boundary_conditions=true, const bool is_gaussian=false) const { + return CImg(*this,false).blur(sigma_x,sigma_y,sigma_z,boundary_conditions,is_gaussian); + } + + //! Blur image isotropically. + /** + \param sigma Standard deviation of the blur. + \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }.a + \see deriche(), vanvliet(). + **/ + CImg& blur(const float sigma, const bool boundary_conditions=true, const bool is_gaussian=false) { + const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100; + return blur(nsigma,nsigma,nsigma,boundary_conditions,is_gaussian); + } + + //! Blur image isotropically \newinstance. + CImg get_blur(const float sigma, const bool boundary_conditions=true, const bool is_gaussian=false) const { + return CImg(*this,false).blur(sigma,boundary_conditions,is_gaussian); + } + + //! Blur image anisotropically, directed by a field of diffusion tensors. + /** + \param G Field of square roots of diffusion tensors/vectors used to drive the smoothing. + \param amplitude Amplitude of the smoothing. + \param dl Spatial discretization. + \param da Angular discretization. + \param gauss_prec Precision of the diffusion process. + \param interpolation_type Interpolation scheme. + Can be { 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }. + \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not. + **/ + template + CImg& blur_anisotropic(const CImg& G, + const float amplitude=60, const float dl=0.8f, const float da=30, + const float gauss_prec=2, const unsigned int interpolation_type=0, + const bool is_fast_approx=1) { + + // Check arguments and init variables + if (!is_sameXYZ(G) || (G._spectrum!=3 && G._spectrum!=6)) + throw CImgArgumentException(_cimg_instance + "blur_anisotropic(): Invalid specified diffusion tensor field (%u,%u,%u,%u,%p).", + cimg_instance, + G._width,G._height,G._depth,G._spectrum,G._data); + + if (is_empty() || amplitude<=0 || dl<0) return *this; + const bool is_3d = (G._spectrum==6); + T val_min, val_max = max_min(val_min); + + if (da<=0) { // Iterated oriented Laplacians + CImg velocity(_width,_height,_depth,_spectrum); + for (unsigned int iteration = 0; iteration<(unsigned int)amplitude; ++iteration) { + Tfloat *ptrd = velocity._data, veloc_max = 0; + if (is_3d) // 3d version + cimg_forC(*this,c) { + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat + ixx = Incc + Ipcc - 2*Iccc, + ixy = (Innc + Ippc - Inpc - Ipnc)/4, + ixz = (Incn + Ipcp - Incp - Ipcn)/4, + iyy = Icnc + Icpc - 2*Iccc, + iyz = (Icnn + Icpp - Icnp - Icpn)/4, + izz = Iccn + Iccp - 2*Iccc, + veloc = (Tfloat)(G(x,y,z,0)*ixx + 2*G(x,y,z,1)*ixy + 2*G(x,y,z,2)*ixz + + G(x,y,z,3)*iyy + 2*G(x,y,z,4)*iyz + G(x,y,z,5)*izz); + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } + } + else // 2d version + cimg_forZC(*this,z,c) { + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat + ixx = Inc + Ipc - 2*Icc, + ixy = (Inn + Ipp - Inp - Ipn)/4, + iyy = Icn + Icp - 2*Icc, + veloc = (Tfloat)(G(x,y,0,0)*ixx + 2*G(x,y,0,1)*ixy + G(x,y,0,2)*iyy); + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } + } + if (veloc_max>0) *this+=(velocity*=dl/veloc_max); + } + } else { // LIC-based smoothing. + const unsigned long whd = (unsigned long)_width*_height*_depth; + const float sqrt2amplitude = (float)std::sqrt(2*amplitude); + const int dx1 = width() - 1, dy1 = height() - 1, dz1 = depth() - 1; + CImg res(_width,_height,_depth,_spectrum,0), W(_width,_height,_depth,is_3d?4:3), val(_spectrum,1,1,1,0); + int N = 0; + if (is_3d) { // 3d version + for (float phi = (180%(int)da)/2.0f; phi<=180; phi+=da) { + const float phir = (float)(phi*cimg::PI/180), datmp = (float)(da/std::cos(phir)), + da2 = datmp<1?360.0f:datmp; + for (float theta = 0; theta<360; (theta+=da2),++N) { + const float + thetar = (float)(theta*cimg::PI/180), + vx = (float)(std::cos(thetar)*std::cos(phir)), + vy = (float)(std::sin(thetar)*std::cos(phir)), + vz = (float)std::sin(phir); + const t + *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2), + *pd = G.data(0,0,0,3), *pe = G.data(0,0,0,4), *pf = G.data(0,0,0,5); + Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2), *pd3 = W.data(0,0,0,3); + cimg_forXYZ(G,xg,yg,zg) { + const t a = *(pa++), b = *(pb++), c = *(pc++), d = *(pd++), e = *(pe++), f = *(pf++); + const float + u = (float)(a*vx + b*vy + c*vz), + v = (float)(b*vx + d*vy + e*vz), + w = (float)(c*vx + e*vy + f*vz), + n = (float)std::sqrt(1e-5+u*u+v*v+w*w), + dln = dl/n; + *(pd0++) = (Tfloat)(u*dln); + *(pd1++) = (Tfloat)(v*dln); + *(pd2++) = (Tfloat)(w*dln); + *(pd3++) = (Tfloat)n; + } + +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=2) firstprivate(val) +#endif + cimg_forXYZ(*this,x,y,z) { + val.fill(0); + const float + n = (float)W(x,y,z,3), + fsigma = (float)(n*sqrt2amplitude), + fsigma2 = 2*fsigma*fsigma, + length = gauss_prec*fsigma; + float + S = 0, + X = (float)x, + Y = (float)y, + Z = (float)z; + switch (interpolation_type) { + case 0 : { // Nearest neighbor + for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) { + const int + cx = (int)(X+0.5f), + cy = (int)(Y+0.5f), + cz = (int)(Z+0.5f); + const float + u = (float)W(cx,cy,cz,0), + v = (float)W(cx,cy,cz,1), + w = (float)W(cx,cy,cz,2); + if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,cz,c); ++S; } + else { + const float coef = (float)std::exp(-l*l/fsigma2); + cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,cz,c)); + S+=coef; + } + X+=u; Y+=v; Z+=w; + } + } break; + case 1 : { // Linear interpolation + for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) { + const float + u = (float)(W._linear_atXYZ(X,Y,Z,0)), + v = (float)(W._linear_atXYZ(X,Y,Z,1)), + w = (float)(W._linear_atXYZ(X,Y,Z,2)); + if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; } + else { + const float coef = (float)std::exp(-l*l/fsigma2); + cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c)); + S+=coef; + } + X+=u; Y+=v; Z+=w; + } + } break; + default : { // 2nd order Runge Kutta + for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) { + const float + u0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,0)), + v0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,1)), + w0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,2)), + u = (float)(W._linear_atXYZ(X+u0,Y+v0,Z+w0,0)), + v = (float)(W._linear_atXYZ(X+u0,Y+v0,Z+w0,1)), + w = (float)(W._linear_atXYZ(X+u0,Y+v0,Z+w0,2)); + if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; } + else { + const float coef = (float)std::exp(-l*l/fsigma2); + cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c)); + S+=coef; + } + X+=u; Y+=v; Z+=w; + } + } break; + } + Tfloat *ptrd = res.data(x,y,z); + if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; } + else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,z,c)); ptrd+=whd; } + } + } + } + } else { // 2d LIC algorithm + for (float theta = (360%(int)da)/2.0f; theta<360; (theta+=da),++N) { + const float thetar = (float)(theta*cimg::PI/180), + vx = (float)(std::cos(thetar)), vy = (float)(std::sin(thetar)); + const t *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2); + Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2); + cimg_forXY(G,xg,yg) { + const t a = *(pa++), b = *(pb++), c = *(pc++); + const float + u = (float)(a*vx + b*vy), + v = (float)(b*vx + c*vy), + n = (float)std::sqrt(1e-5+u*u+v*v), + dln = dl/n; + *(pd0++) = (Tfloat)(u*dln); + *(pd1++) = (Tfloat)(v*dln); + *(pd2++) = (Tfloat)n; + } + +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width>=256 && _height>=2) firstprivate(val) +#endif + cimg_forXY(*this,x,y) { + val.fill(0); + const float + n = (float)W(x,y,0,2), + fsigma = (float)(n*sqrt2amplitude), + fsigma2 = 2*fsigma*fsigma, + length = gauss_prec*fsigma; + float + S = 0, + X = (float)x, + Y = (float)y; + switch (interpolation_type) { + case 0 : { // Nearest-neighbor + for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) { + const int + cx = (int)(X+0.5f), + cy = (int)(Y+0.5f); + const float + u = (float)W(cx,cy,0,0), + v = (float)W(cx,cy,0,1); + if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,0,c); ++S; } + else { + const float coef = (float)std::exp(-l*l/fsigma2); + cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,0,c)); + S+=coef; + } + X+=u; Y+=v; + } + } break; + case 1 : { // Linear interpolation + for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) { + const float + u = (float)(W._linear_atXY(X,Y,0,0)), + v = (float)(W._linear_atXY(X,Y,0,1)); + if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; } + else { + const float coef = (float)std::exp(-l*l/fsigma2); + cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c)); + S+=coef; + } + X+=u; Y+=v; + } + } break; + default : { // 2nd-order Runge-kutta interpolation + for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) { + const float + u0 = (float)(0.5f*W._linear_atXY(X,Y,0,0)), + v0 = (float)(0.5f*W._linear_atXY(X,Y,0,1)), + u = (float)(W._linear_atXY(X+u0,Y+v0,0,0)), + v = (float)(W._linear_atXY(X+u0,Y+v0,0,1)); + if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; } + else { + const float coef = (float)std::exp(-l*l/fsigma2); + cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c)); + S+=coef; + } + X+=u; Y+=v; + } + } + } + Tfloat *ptrd = res.data(x,y); + if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; } + else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,0,c)); ptrd+=whd; } + } + } + } + const Tfloat *ptrs = res._data; + cimg_for(*this,ptrd,T) { + const Tfloat val = *(ptrs++)/N; + *ptrd = valval_max?val_max:(T)val); + } + } + return *this; + } + + //! Blur image anisotropically, directed by a field of diffusion tensors \newinstance. + template + CImg get_blur_anisotropic(const CImg& G, + const float amplitude=60, const float dl=0.8f, const float da=30, + const float gauss_prec=2, const unsigned int interpolation_type=0, + const bool is_fast_approx=true) const { + return (+*this).blur_anisotropic(G,amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx); + } + + //! Blur image anisotropically, in an edge-preserving way. + /** + \param amplitude Amplitude of the smoothing. + \param sharpness Sharpness. + \param anisotropy Anisotropy. + \param alpha Standard deviation of the gradient blur. + \param sigma Standard deviation of the structure tensor blur. + \param dl Spatial discretization. + \param da Angular discretization. + \param gauss_prec Precision of the diffusion process. + \param interpolation_type Interpolation scheme. + Can be { 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }. + \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not. + **/ + CImg& blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f, + const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f, const float da=30, + const float gauss_prec=2, const unsigned int interpolation_type=0, + const bool is_fast_approx=true) { + return blur_anisotropic(get_diffusion_tensors(sharpness,anisotropy,alpha,sigma,interpolation_type!=3), + amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx); + } + + //! Blur image anisotropically, in an edge-preserving way \newinstance. + CImg get_blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f, + const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f, + const float da=30, const float gauss_prec=2, const unsigned int interpolation_type=0, + const bool is_fast_approx=true) const { + return (+*this).blur_anisotropic(amplitude,sharpness,anisotropy,alpha,sigma,dl,da,gauss_prec,interpolation_type, + is_fast_approx); + } + + //! Blur image, with the joint bilateral filter. + /** + \param guide Image used to model the smoothing weights. + \param sigma_x Amount of blur along the X-axis. + \param sigma_y Amount of blur along the Y-axis. + \param sigma_z Amount of blur along the Z-axis. + \param sigma_r Amount of blur along the value axis. + \param bgrid_x Size of the bilateral grid along the X-axis. + \param bgrid_y Size of the bilateral grid along the Y-axis. + \param bgrid_z Size of the bilateral grid along the Z-axis. + \param bgrid_r Size of the bilateral grid along the value axis. + \param interpolation_type Use interpolation for image slicing. + \note This algorithm uses the optimisation technique proposed by S. Paris and F. Durand, in ECCV'2006 + (extended for 3d volumetric images). + **/ + template + CImg& blur_bilateral(const CImg& guide, + const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_r, + const int bgrid_x, const int bgrid_y, const int bgrid_z, const int bgrid_r, + const bool interpolation_type=true) { + if (!is_sameXYZ(guide)) + throw CImgArgumentException(_cimg_instance + "blur_bilateral(): Invalid size for specified guide image (%u,%u,%u,%u,%p).", + cimg_instance, + guide._width,guide._height,guide._depth,guide._spectrum,guide._data); + if (is_empty()) return *this; + T m, M = guide.max_min(m); + if (m==M) return *this; + const float range = (float)(M - m); + const unsigned int + bx0 = bgrid_x>=0?bgrid_x:_width*-bgrid_x/100, + by0 = bgrid_y>=0?bgrid_y:_height*-bgrid_y/100, + bz0 = bgrid_z>=0?bgrid_z:_depth*-bgrid_z/100, + br0 = bgrid_r>=0?bgrid_r:(int)(-range*bgrid_r/100), + bx = bx0>0?bx0:1, + by = by0>0?by0:1, + bz = bz0>0?bz0:1, + br = br0>0?br0:1; + const float + _sigma_x = sigma_x>=0?sigma_x:-sigma_x*_width/100, + _sigma_y = sigma_y>=0?sigma_y:-sigma_y*_height/100, + _sigma_z = sigma_z>=0?sigma_z:-sigma_z*_depth/100, + _sigma_r = sigma_r>=0?sigma_r:-sigma_r*range/100, + nsigma_x = _sigma_x*bx/_width, + nsigma_y = _sigma_y*by/_height, + nsigma_z = _sigma_z*bz/_depth, + nsigma_r = _sigma_r*br/range; + if (nsigma_x>0 || nsigma_y>0 || nsigma_z>0 || nsigma_r>0) { + const bool is_3d = (_depth>1); + if (is_3d) { // 3d version of the algorithm + CImg bgrid(bx,by,bz,br), bgridw(bx,by,bz,br); + cimg_forC(*this,c) { + const CImg _guide = guide.get_shared_channel(c%guide._spectrum); + bgrid.fill(0); bgridw.fill(0); + cimg_forXYZ(*this,x,y,z) { + const T val = (*this)(x,y,z,c); + const float gval = (float)_guide(x,y,z); + const int X = x*bx/_width, Y = y*by/_height, Z = z*bz/_depth, + R = (int)cimg::min(br-1.0f,(gval-m)*br/range); + bgrid(X,Y,Z,R) += (float)val; + bgridw(X,Y,Z,R) += 1; + } + bgrid.blur(nsigma_x,nsigma_y,nsigma_z,true).deriche(nsigma_r,0,'c',false); + bgridw.blur(nsigma_x,nsigma_y,nsigma_z,true).deriche(nsigma_r,0,'c',false); + if (interpolation_type) cimg_forXYZ(*this,x,y,z) { + const float gval = (float)_guide(x,y,z), + X = (float)x*bx/_width, Y = (float)y*by/_height, Z = (float)z*bz/_depth, + R = (float)cimg::min(br-1.0f,(gval-m)*br/range), + bval0 = bgrid._linear_atXYZC(X,Y,Z,R), bval1 = bgridw._linear_atXYZC(X,Y,Z,R); + (*this)(x,y,z,c) = (T)(bval0/bval1); + } else cimg_forXYZ(*this,x,y,z) { + const float gval = (float)_guide(x,y,z); + const int X = x*bx/_width, Y = y*by/_height, Z = z*bz/_depth, + R = (int)cimg::min(br-1.0f,(gval-m)*br/range); + const float bval0 = bgrid(X,Y,Z,R), bval1 = bgridw(X,Y,Z,R); + (*this)(x,y,z,c) = (T)(bval0/bval1); + } + } + } else { // 2d version of the algorithm + CImg bgrid(bx,by,br,2); + cimg_forC(*this,c) { + const CImg _guide = guide.get_shared_channel(c%guide._spectrum); + bgrid.fill(0); + cimg_forXY(*this,x,y) { + const T val = (*this)(x,y,c); + const float gval = (float)_guide(x,y); + const int X = x*bx/_width, Y = y*by/_height, R = (int)cimg::min(br-1.0f,(gval-m)*br/range); + bgrid(X,Y,R,0) += (float)val; + bgrid(X,Y,R,1) += 1; + } + bgrid.blur(nsigma_x,nsigma_y,0,true).blur(0,0,nsigma_r,false); + if (interpolation_type) cimg_forXY(*this,x,y) { + const float gval = (float)_guide(x,y), + X = (float)x*bx/_width, Y = (float)y*by/_height, R = (float)cimg::min(br-1.0f,(gval-m)*br/range), + bval0 = bgrid._linear_atXYZ(X,Y,R,0), bval1 = bgrid._linear_atXYZ(X,Y,R,1); + (*this)(x,y,c) = (T)(bval0/bval1); + } else cimg_forXY(*this,x,y) { + const float gval = (float)_guide(x,y); + const int X = x*bx/_width, Y = y*by/_height, R = (int)cimg::min(br-1.0f,(gval-m)*br/range); + const float bval0 = bgrid(X,Y,R,0), bval1 = bgrid(X,Y,R,1); + (*this)(x,y,c) = (T)(bval0/bval1); + } + } + } + } + return *this; + } + + //! Blur image, with the joint bilateral filter \newinstance. + template + CImg get_blur_bilateral(const CImg& guide, + const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_r, + const int bgrid_x, const int bgrid_y, const int bgrid_z, const int bgrid_r, + const bool interpolation_type=true) const { + return (+*this).blur_bilateral(guide,sigma_x,sigma_y,sigma_z,sigma_r,bgrid_x,bgrid_y,bgrid_z,bgrid_r, + interpolation_type); + } + + //! Blur image using the joint bilateral filter. + /** + \param guide Image used to model the smoothing weights. + \param sigma_s Amount of blur along the XYZ-axes. + \param sigma_r Amount of blur along the value axis. + \param bgrid_s Size of the bilateral grid along the XYZ-axes. + \param bgrid_r Size of the bilateral grid along the value axis. + \param interpolation_type Use interpolation for image slicing. + **/ + template + CImg& blur_bilateral(const CImg& guide, + const float sigma_s, const float sigma_r, const int bgrid_s=-33, const int bgrid_r=32, + const bool interpolation_type=true) { + const float nsigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100; + return blur_bilateral(guide,nsigma_s,nsigma_s,nsigma_s,sigma_r,bgrid_s,bgrid_s,bgrid_s,bgrid_r, + interpolation_type); + } + + //! Blur image using the bilateral filter \newinstance. + template + CImg get_blur_bilateral(const CImg& guide, + const float sigma_s, const float sigma_r, const int bgrid_s=-33, const int bgrid_r=32, + const bool interpolation_type=true) const { + return (+*this).blur_bilateral(guide,sigma_s,sigma_s,sigma_s,sigma_r,bgrid_s,bgrid_s,bgrid_s,bgrid_r, + interpolation_type); + } + + //! Blur image using patch-based space. + /** + \param sigma_s Amount of blur along the XYZ-axes. + \param sigma_p Amount of blur along the value axis. + \param patch_size Size of the patchs. + \param lookup_size Size of the window to search similar patchs. + \param smoothness Smoothness for the patch comparison. + \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not. + **/ + CImg& blur_patch(const float sigma_s, const float sigma_p, const unsigned int patch_size=3, + const unsigned int lookup_size=4, const float smoothness=0, const bool is_fast_approx=true) { + if (is_empty() || !patch_size || !lookup_size) return *this; + return get_blur_patch(sigma_s,sigma_p,patch_size,lookup_size,smoothness,is_fast_approx).move_to(*this); + } + + //! Blur image using patch-based space \newinstance. + CImg get_blur_patch(const float sigma_s, const float sigma_p, const unsigned int patch_size=3, + const unsigned int lookup_size=4, const float smoothness=0, + const bool is_fast_approx=true) const { + +#define _cimg_blur_patch3d_fast(N) \ + cimg_for##N##XYZ(res,x,y,z) { \ + T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N##x##N(img,x,y,z,c,pP,T); pP+=N3; } \ + const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \ + x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \ + float sum_weights = 0; \ + cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (cimg::abs(img(x,y,z,0) - img(p,q,r,0))3?0.0f:1.0f; \ + sum_weights+=weight; \ + cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); \ + } \ + if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; \ + else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \ + } + +#define _cimg_blur_patch3d(N) \ + cimg_for##N##XYZ(res,x,y,z) { \ + T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N##x##N(img,x,y,z,c,pP,T); pP+=N3; } \ + const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \ + x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \ + float sum_weights = 0, weight_max = 0; \ + cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) { \ + T *pQ = Q._data; cimg_forC(res,c) { cimg_get##N##x##N##x##N(img,p,q,r,c,pQ,T); pQ+=N3; } \ + float distance2 = 0; \ + pQ = Q._data; cimg_for(P,pP,T) { const float dI = (float)*pP - (float)*(pQ++); distance2+=dI*dI; } \ + distance2/=Pnorm; \ + const float dx = (float)p - x, dy = (float)q - y, dz = (float)r - z, \ + alldist = distance2 + (dx*dx + dy*dy + dz*dz)/sigma_s2, weight = (float)std::exp(-alldist); \ + if (weight>weight_max) weight_max = weight; \ + sum_weights+=weight; \ + cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); \ + } \ + sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=weight_max*(*this)(x,y,z,c); \ + if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; \ + else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \ + } + +#define _cimg_blur_patch2d_fast(N) \ + cimg_for##N##XY(res,x,y) { \ + T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N(img,x,y,0,c,pP,T); pP+=N2; } \ + const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \ + float sum_weights = 0; \ + cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) if (cimg::abs(img(x,y,0,0) - img(p,q,0,0))3?0.0f:1.0f; \ + sum_weights+=weight; \ + cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); \ + } \ + if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; \ + else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \ + } + +#define _cimg_blur_patch2d(N) \ + cimg_for##N##XY(res,x,y) { \ + T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N(img,x,y,0,c,pP,T); pP+=N2; } \ + const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \ + float sum_weights = 0, weight_max = 0; \ + cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) { \ + T *pQ = Q._data; cimg_forC(res,c) { cimg_get##N##x##N(img,p,q,0,c,pQ,T); pQ+=N2; } \ + float distance2 = 0; \ + pQ = Q._data; cimg_for(P,pP,T) { const float dI = (float)*pP - (float)*(pQ++); distance2+=dI*dI; } \ + distance2/=Pnorm; \ + const float dx = (float)p - x, dy = (float)q - y, \ + alldist = distance2 + (dx*dx+dy*dy)/sigma_s2, weight = (float)std::exp(-alldist); \ + if (weight>weight_max) weight_max = weight; \ + sum_weights+=weight; \ + cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); \ + } \ + sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=weight_max*(*this)(x,y,c); \ + if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; \ + else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \ + } + + if (is_empty() || !patch_size || !lookup_size) return +*this; + CImg res(_width,_height,_depth,_spectrum,0); + const CImg _img = smoothness>0?get_blur(smoothness):CImg(),&img = smoothness>0?_img:*this; + CImg P(patch_size*patch_size*_spectrum), Q(P); + const float + nsigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100, + sigma_s2 = nsigma_s*nsigma_s, sigma_p2 = sigma_p*sigma_p, sigma_p3 = 3*sigma_p, + Pnorm = P.size()*sigma_p2; + const int rsize2 = (int)lookup_size/2, rsize1 = (int)lookup_size - rsize2 - 1; + const unsigned int N2 = patch_size*patch_size, N3 = N2*patch_size; + cimg::unused(N2,N3); + if (_depth>1) switch (patch_size) { // 3d + case 2 : if (is_fast_approx) _cimg_blur_patch3d_fast(2) else _cimg_blur_patch3d(2) break; + case 3 : if (is_fast_approx) _cimg_blur_patch3d_fast(3) else _cimg_blur_patch3d(3) break; + default : { + const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1; + if (is_fast_approx) +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (res._width>=32 && res._height*res._depth>=4) private(P,Q) +#endif + cimg_forXYZ(res,x,y,z) { // Fast + P = img.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true); + const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, + x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; + float sum_weights = 0; + cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (cimg::abs(img(x,y,z,0)-img(p,q,r,0))3?0.0f:1.0f; + sum_weights+=weight; + cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); + } + if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; + else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); + } else +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (res._width>=32 && res._height*res._depth>=4) firstprivate(P,Q) +#endif + cimg_forXYZ(res,x,y,z) { // Exact + P = img.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true); + const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, + x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; + float sum_weights = 0, weight_max = 0; + cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) { + (Q = img.get_crop(p - psize1,q - psize1,r - psize1,p + psize2,q + psize2,r + psize2,true))-=P; + const float + dx = (float)x - p, dy = (float)y - q, dz = (float)z - r, + distance2 = (float)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy + dz*dz)/sigma_s2), + weight = (float)std::exp(-distance2); + if (weight>weight_max) weight_max = weight; + sum_weights+=weight; + cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); + } + sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=weight_max*(*this)(x,y,z,c); + if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; + else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); + } + } + } else switch (patch_size) { // 2d + case 2 : if (is_fast_approx) _cimg_blur_patch2d_fast(2) else _cimg_blur_patch2d(2) break; + case 3 : if (is_fast_approx) _cimg_blur_patch2d_fast(3) else _cimg_blur_patch2d(3) break; + case 4 : if (is_fast_approx) _cimg_blur_patch2d_fast(4) else _cimg_blur_patch2d(4) break; + case 5 : if (is_fast_approx) _cimg_blur_patch2d_fast(5) else _cimg_blur_patch2d(5) break; + case 6 : if (is_fast_approx) _cimg_blur_patch2d_fast(6) else _cimg_blur_patch2d(6) break; + case 7 : if (is_fast_approx) _cimg_blur_patch2d_fast(7) else _cimg_blur_patch2d(7) break; + case 8 : if (is_fast_approx) _cimg_blur_patch2d_fast(8) else _cimg_blur_patch2d(8) break; + case 9 : if (is_fast_approx) _cimg_blur_patch2d_fast(9) else _cimg_blur_patch2d(9) break; + default : { // Fast + const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1; + if (is_fast_approx) +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._width>=32 && res._height>=4) firstprivate(P,Q) +#endif + cimg_forXY(res,x,y) { // 2d fast approximation. + P = img.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true); + const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; + float sum_weights = 0; + cimg_for_inXY(res,x0,y0,x1,y1,p,q) if (cimg::abs(img(x,y,0)-img(p,q,0))3?0.0f:1.0f; + sum_weights+=weight; + cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); + } + if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; + else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); + } else +#ifdef cimg_use_openmp +#pragma omp parallel for if (res._width>=32 && res._height>=4) firstprivate(P,Q) +#endif + cimg_forXY(res,x,y) { // 2d exact algorithm. + P = img.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true); + const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; + float sum_weights = 0, weight_max = 0; + cimg_for_inXY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) { + (Q = img.get_crop(p - psize1,q - psize1,p + psize2,q + psize2,true))-=P; + const float + dx = (float)x - p, dy = (float)y - q, + distance2 = (float)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy)/sigma_s2), + weight = (float)std::exp(-distance2); + if (weight>weight_max) weight_max = weight; + sum_weights+=weight; + cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); + } + sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=weight_max*(*this)(x,y,c); + if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; + else cimg_forC(res,c) res(x,y,0,c) = (Tfloat)((*this)(x,y,c)); + } + } + } + return res; + } + + //! Blur image with the median filter. + /** + \param n Size of the median filter. + \param threshold Threshold used to discard pixels too far from the current pixel value in the median computation. + **/ + CImg& blur_median(const unsigned int n, const float threshold=0) { + if (!n) return *this; + return get_blur_median(n,threshold).move_to(*this); + } + + //! Blur image with the median filter \newinstance. + CImg get_blur_median(const unsigned int n, const float threshold=0) const { + if (is_empty() || n<=1) return +*this; + CImg res(_width,_height,_depth,_spectrum); + T *ptrd = res._data; + cimg::unused(ptrd); + const int hl = n/2, hr = hl - 1 + n%2; + if (res._depth!=1) { // 3d + if (threshold>0) +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=16 && _height*_depth*_spectrum>=4) +#endif + cimg_forXYZC(*this,x,y,z,c) { // With threshold. + const int + x0 = x - hl, y0 = y - hl, z0 = z-hl, x1 = x + hr, y1 = y + hr, z1 = z+hr, + nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0, + nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1, nz1 = z1>=depth()?depth()-1:z1; + const float val0 = (float)(*this)(x,y,z,c); + CImg values(n*n*n); + unsigned int nb_values = 0; + T *ptrd = values.data(); + cimg_for_inXYZ(*this,nx0,ny0,nz0,nx1,ny1,nz1,p,q,r) + if (cimg::abs((float)(*this)(p,q,r,c)-val0)<=threshold) { *(ptrd++) = (*this)(p,q,r,c); ++nb_values; } + res(x,y,z,c) = values.get_shared_points(0,nb_values-1).median(); + } + else +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width>=16 && _height*_depth*_spectrum>=4) +#endif + cimg_forXYZC(*this,x,y,z,c) { // Without threshold. + const int + x0 = x - hl, y0 = y - hl, z0 = z-hl, x1 = x + hr, y1 = y + hr, z1 = z+hr, + nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0, + nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1, nz1 = z1>=depth()?depth()-1:z1; + res(x,y,z,c) = get_crop(nx0,ny0,nz0,c,nx1,ny1,nz1,c).median(); + } + } else { +#define _cimg_median_sort(a,b) if ((a)>(b)) cimg::swap(a,b) + if (res._height!=1) { // 2d + if (threshold>0) +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=16 && _height*_spectrum>=4) +#endif + cimg_forXYC(*this,x,y,c) { // With threshold. + const int + x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr, + nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, + nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1; + const float val0 = (float)(*this)(x,y,c); + CImg values(n*n); + unsigned int nb_values = 0; + T *ptrd = values.data(); + cimg_for_inXY(*this,nx0,ny0,nx1,ny1,p,q) + if (cimg::abs((float)(*this)(p,q,c)-val0)<=threshold) { *(ptrd++) = (*this)(p,q,c); ++nb_values; } + res(x,y,c) = values.get_shared_points(0,nb_values-1).median(); + } + else switch (n) { // Without threshold. + case 3 : { +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + T I[9] = { 0 }; + CImg_3x3(J,T); + cimg_for3x3(*this,x,y,0,c,I,T) { + std::memcpy(J,I,9*sizeof(T)); + _cimg_median_sort(Jcp, Jnp); _cimg_median_sort(Jcc, Jnc); _cimg_median_sort(Jcn, Jnn); + _cimg_median_sort(Jpp, Jcp); _cimg_median_sort(Jpc, Jcc); _cimg_median_sort(Jpn, Jcn); + _cimg_median_sort(Jcp, Jnp); _cimg_median_sort(Jcc, Jnc); _cimg_median_sort(Jcn, Jnn); + _cimg_median_sort(Jpp, Jpc); _cimg_median_sort(Jnc, Jnn); _cimg_median_sort(Jcc, Jcn); + _cimg_median_sort(Jpc, Jpn); _cimg_median_sort(Jcp, Jcc); _cimg_median_sort(Jnp, Jnc); + _cimg_median_sort(Jcc, Jcn); _cimg_median_sort(Jcc, Jnp); _cimg_median_sort(Jpn, Jcc); + _cimg_median_sort(Jcc, Jnp); + res(x,y,c) = Jcc; + } + } + } break; + case 5 : { +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + T I[25] = { 0 }; + CImg_5x5(J,T); + cimg_for5x5(*this,x,y,0,c,I,T) { + std::memcpy(J,I,25*sizeof(T)); + _cimg_median_sort(Jbb,Jpb); _cimg_median_sort(Jnb,Jab); _cimg_median_sort(Jcb,Jab); + _cimg_median_sort(Jcb,Jnb); _cimg_median_sort(Jpp,Jcp); _cimg_median_sort(Jbp,Jcp); + _cimg_median_sort(Jbp,Jpp); _cimg_median_sort(Jap,Jbc); _cimg_median_sort(Jnp,Jbc); + _cimg_median_sort(Jnp,Jap); _cimg_median_sort(Jcc,Jnc); _cimg_median_sort(Jpc,Jnc); + _cimg_median_sort(Jpc,Jcc); _cimg_median_sort(Jbn,Jpn); _cimg_median_sort(Jac,Jpn); + _cimg_median_sort(Jac,Jbn); _cimg_median_sort(Jnn,Jan); _cimg_median_sort(Jcn,Jan); + _cimg_median_sort(Jcn,Jnn); _cimg_median_sort(Jpa,Jca); _cimg_median_sort(Jba,Jca); + _cimg_median_sort(Jba,Jpa); _cimg_median_sort(Jna,Jaa); _cimg_median_sort(Jcb,Jbp); + _cimg_median_sort(Jnb,Jpp); _cimg_median_sort(Jbb,Jpp); _cimg_median_sort(Jbb,Jnb); + _cimg_median_sort(Jab,Jcp); _cimg_median_sort(Jpb,Jcp); _cimg_median_sort(Jpb,Jab); + _cimg_median_sort(Jpc,Jac); _cimg_median_sort(Jnp,Jac); _cimg_median_sort(Jnp,Jpc); + _cimg_median_sort(Jcc,Jbn); _cimg_median_sort(Jap,Jbn); _cimg_median_sort(Jap,Jcc); + _cimg_median_sort(Jnc,Jpn); _cimg_median_sort(Jbc,Jpn); _cimg_median_sort(Jbc,Jnc); + _cimg_median_sort(Jba,Jna); _cimg_median_sort(Jcn,Jna); _cimg_median_sort(Jcn,Jba); + _cimg_median_sort(Jpa,Jaa); _cimg_median_sort(Jnn,Jaa); _cimg_median_sort(Jnn,Jpa); + _cimg_median_sort(Jan,Jca); _cimg_median_sort(Jnp,Jcn); _cimg_median_sort(Jap,Jnn); + _cimg_median_sort(Jbb,Jnn); _cimg_median_sort(Jbb,Jap); _cimg_median_sort(Jbc,Jan); + _cimg_median_sort(Jpb,Jan); _cimg_median_sort(Jpb,Jbc); _cimg_median_sort(Jpc,Jba); + _cimg_median_sort(Jcb,Jba); _cimg_median_sort(Jcb,Jpc); _cimg_median_sort(Jcc,Jpa); + _cimg_median_sort(Jnb,Jpa); _cimg_median_sort(Jnb,Jcc); _cimg_median_sort(Jnc,Jca); + _cimg_median_sort(Jab,Jca); _cimg_median_sort(Jab,Jnc); _cimg_median_sort(Jac,Jna); + _cimg_median_sort(Jbp,Jna); _cimg_median_sort(Jbp,Jac); _cimg_median_sort(Jbn,Jaa); + _cimg_median_sort(Jpp,Jaa); _cimg_median_sort(Jpp,Jbn); _cimg_median_sort(Jcp,Jpn); + _cimg_median_sort(Jcp,Jan); _cimg_median_sort(Jnc,Jpa); _cimg_median_sort(Jbn,Jna); + _cimg_median_sort(Jcp,Jnc); _cimg_median_sort(Jcp,Jbn); _cimg_median_sort(Jpb,Jap); + _cimg_median_sort(Jnb,Jpc); _cimg_median_sort(Jbp,Jcn); _cimg_median_sort(Jpc,Jcn); + _cimg_median_sort(Jap,Jcn); _cimg_median_sort(Jab,Jbc); _cimg_median_sort(Jpp,Jcc); + _cimg_median_sort(Jcp,Jac); _cimg_median_sort(Jab,Jpp); _cimg_median_sort(Jab,Jcp); + _cimg_median_sort(Jcc,Jac); _cimg_median_sort(Jbc,Jac); _cimg_median_sort(Jpp,Jcp); + _cimg_median_sort(Jbc,Jcc); _cimg_median_sort(Jpp,Jbc); _cimg_median_sort(Jpp,Jcn); + _cimg_median_sort(Jcc,Jcn); _cimg_median_sort(Jcp,Jcn); _cimg_median_sort(Jcp,Jbc); + _cimg_median_sort(Jcc,Jnn); _cimg_median_sort(Jcp,Jcc); _cimg_median_sort(Jbc,Jnn); + _cimg_median_sort(Jcc,Jba); _cimg_median_sort(Jbc,Jba); _cimg_median_sort(Jbc,Jcc); + res(x,y,c) = Jcc; + } + } + } break; + default : { +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=16 && _height*_spectrum>=4) +#endif + cimg_forXYC(*this,x,y,c) { + const int + x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr, + nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, + nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1; + res(x,y,c) = get_crop(nx0,ny0,0,c,nx1,ny1,0,c).median(); + } + } + } + } else { // 1d + + if (threshold>0) +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width>=16 && _spectrum>=2) +#endif + cimg_forXC(*this,x,c) { // With threshold. + const int + x0 = x - hl, x1 = x + hr, + nx0 = x0<0?0:x0, nx1 = x1>=width()?width()-1:x1; + const float val0 = (float)(*this)(x,c); + CImg values(n); + unsigned int nb_values = 0; + T *ptrd = values.data(); + cimg_for_inX(*this,nx0,nx1,p) + if (cimg::abs((float)(*this)(p,c)-val0)<=threshold) { *(ptrd++) = (*this)(p,c); ++nb_values; } + res(x,c) = values.get_shared_points(0,nb_values-1).median(); + } + else switch (n) { // Without threshold. + case 2 : { +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + T I[4] = { 0 }; + cimg_for2x2(*this,x,y,0,c,I,T) res(x,c) = (T)(0.5f*(I[0]+I[1])); + } + } break; + case 3 : { +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + T I[9] = { 0 }; + cimg_for3x3(*this,x,y,0,c,I,T) + res(x,c) = I[3]=16 && _spectrum>=2) +#endif + cimg_forXC(*this,x,c) { + const int + x0 = x - hl, x1 = x + hr, + nx0 = x0<0?0:x0, nx1 = x1>=width()?width()-1:x1; + res(x,c) = get_crop(nx0,0,0,c,nx1,0,0,c).median(); + } + } + } + } + } + return res; + } + + //! Sharpen image. + /** + \param amplitude Sharpening amplitude + \param sharpen_type Select sharpening method. Can be { false=inverse diffusion | true=shock filters }. + \param edge Edge threshold (shock filters only). + \param alpha Gradient smoothness (shock filters only). + \param sigma Tensor smoothness (shock filters only). + **/ + CImg& sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1, + const float alpha=0, const float sigma=0) { + if (is_empty()) return *this; + T val_min, val_max = max_min(val_min); + const float nedge = edge/2; + CImg velocity(_width,_height,_depth,_spectrum), _veloc_max(_spectrum); + + if (_depth>1) { // 3d + if (sharpen_type) { // Shock filters. + CImg G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors()); + if (sigma>0) G.blur(sigma); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=32 && _height*_depth>=16) +#endif + cimg_forYZ(G,y,z) { + Tfloat *ptrG0 = G.data(0,y,z,0), *ptrG1 = G.data(0,y,z,1), + *ptrG2 = G.data(0,y,z,2), *ptrG3 = G.data(0,y,z,3); + CImg val, vec; + cimg_forX(G,x) { + G.get_tensor_at(x,y,z).symmetric_eigen(val,vec); + if (val[0]<0) val[0] = 0; + if (val[1]<0) val[1] = 0; + if (val[2]<0) val[2] = 0; + *(ptrG0++) = vec(0,0); + *(ptrG1++) = vec(0,1); + *(ptrG2++) = vec(0,2); + *(ptrG3++) = 1 - (Tfloat)std::pow(1+val[0]+val[1]+val[2],-(Tfloat)nedge); + } + } +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=512 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat + u = G(x,y,z,0), + v = G(x,y,z,1), + w = G(x,y,z,2), + amp = G(x,y,z,3), + ixx = Incc + Ipcc - 2*Iccc, + ixy = (Innc + Ippc - Inpc - Ipnc)/4, + ixz = (Incn + Ipcp - Incp - Ipcn)/4, + iyy = Icnc + Icpc - 2*Iccc, + iyz = (Icnn + Icpp - Icnp - Icpn)/4, + izz = Iccn + Iccp - 2*Iccc, + ixf = Incc - Iccc, + ixb = Iccc - Ipcc, + iyf = Icnc - Iccc, + iyb = Iccc - Icpc, + izf = Iccn - Iccc, + izb = Iccc - Iccp, + itt = u*u*ixx + v*v*iyy + w*w*izz + 2*u*v*ixy + 2*u*w*ixz + 2*v*w*iyz, + it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb) + w*cimg::minmod(izf,izb), + veloc = -amp*cimg::sign(itt)*cimg::abs(it); + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } + _veloc_max[c] = veloc_max; + } + } else // Inverse diffusion. + cimg_forC(*this,c) { + Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat veloc = -Ipcc - Incc - Icpc - Icnc - Iccp - Iccn + 6*Iccc; + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } + _veloc_max[c] = veloc_max; + } + } else { // 2d. + if (sharpen_type) { // Shock filters. + CImg G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors()); + if (sigma>0) G.blur(sigma); +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width>=32 && _height>=16) +#endif + cimg_forY(G,y) { + CImg val, vec; + Tfloat *ptrG0 = G.data(0,y,0,0), *ptrG1 = G.data(0,y,0,1), *ptrG2 = G.data(0,y,0,2); + cimg_forX(G,x) { + G.get_tensor_at(x,y).symmetric_eigen(val,vec); + if (val[0]<0) val[0] = 0; + if (val[1]<0) val[1] = 0; + *(ptrG0++) = vec(0,0); + *(ptrG1++) = vec(0,1); + *(ptrG2++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1],-(Tfloat)nedge); + } + } +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height>=512 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) { + const Tfloat + u = G(x,y,0), + v = G(x,y,1), + amp = G(x,y,2), + ixx = Inc + Ipc - 2*Icc, + ixy = (Inn + Ipp - Inp - Ipn)/4, + iyy = Icn + Icp - 2*Icc, + ixf = Inc - Icc, + ixb = Icc - Ipc, + iyf = Icn - Icc, + iyb = Icc - Icp, + itt = u*u*ixx + v*v*iyy + 2*u*v*ixy, + it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb), + veloc = -amp*cimg::sign(itt)*cimg::abs(it); + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } + _veloc_max[c] = veloc_max; + } + } else // Inverse diffusion. + cimg_forC(*this,c) { + Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) { + const Tfloat veloc = -Ipc - Inc - Icp - Icn + 4*Icc; + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } + _veloc_max[c] = veloc_max; + } + } + const Tfloat veloc_max = _veloc_max.max(); + if (veloc_max<=0) return *this; + return ((velocity*=amplitude/veloc_max)+=*this).cut(val_min,val_max).move_to(*this); + } + + //! Sharpen image \newinstance. + CImg get_sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1, + const float alpha=0, const float sigma=0) const { + return (+*this).sharpen(amplitude,sharpen_type,edge,alpha,sigma); + } + + //! Return image gradient. + /** + \param axes Axes considered for the gradient computation, as a C-string (e.g "xy"). + \param scheme = Numerical scheme used for the gradient computation: + - -1 = Backward finite differences + - 0 = Centered finite differences + - 1 = Forward finite differences + - 2 = Using Sobel masks + - 3 = Using rotation invariant masks + - 4 = Using Deriche recusrsive filter. + - 5 = Using Van Vliet recusrsive filter. + **/ + CImgList get_gradient(const char *const axes=0, const int scheme=3) const { + CImgList grad(2,_width,_height,_depth,_spectrum); + bool is_3d = false; + if (axes) { + for (unsigned int a = 0; axes[a]; ++a) { + const char axis = cimg::uncase(axes[a]); + switch (axis) { + case 'x' : case 'y' : break; + case 'z' : is_3d = true; break; + default : + throw CImgArgumentException(_cimg_instance + "get_gradient(): Invalid specified axis '%c'.", + cimg_instance, + axis); + } + } + } else is_3d = (_depth>1); + if (is_3d) { + CImg(_width,_height,_depth,_spectrum).move_to(grad); + switch (scheme) { // 3d. + case -1 : { // Backward finite differences. +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + const unsigned long off = c*_width*_height*_depth; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off, *ptrd2 = grad[2]._data + off; + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = Iccc - Ipcc; + *(ptrd1++) = Iccc - Icpc; + *(ptrd2++) = Iccc - Iccp; + } + } + } break; + case 1 : { // Forward finite differences. +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + const unsigned long off = c*_width*_height*_depth; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off, *ptrd2 = grad[2]._data + off; + CImg_2x2x2(I,Tfloat); + cimg_for2x2x2(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = Incc - Iccc; + *(ptrd1++) = Icnc - Iccc; + *(ptrd2++) = Iccn - Iccc; + } + } + } break; + case 4 : { // Deriche filter with low standard variation. + grad[0] = get_deriche(0,1,'x'); + grad[1] = get_deriche(0,1,'y'); + grad[2] = get_deriche(0,1,'z'); + } break; + case 5 : { // Van Vliet filter with low standard variation. + grad[0] = get_vanvliet(0,1,'x'); + grad[1] = get_vanvliet(0,1,'y'); + grad[2] = get_vanvliet(0,1,'z'); + } break; + default : { // Central finite differences. +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + const unsigned long off = c*_width*_height*_depth; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off, *ptrd2 = grad[2]._data + off; + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = (Incc - Ipcc)/2; + *(ptrd1++) = (Icnc - Icpc)/2; + *(ptrd2++) = (Iccn - Iccp)/2; + } + } + } + } + } else switch (scheme) { // 2d. + case -1 : { // Backward finite differences. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + const unsigned long off = c*_width*_height*_depth + z*_width*_height; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = Icc - Ipc; + *(ptrd1++) = Icc - Icp; + } + } + } break; + case 1 : { // Forward finite differences. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + const unsigned long off = c*_width*_height*_depth + z*_width*_height; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; + CImg_2x2(I,Tfloat); + cimg_for2x2(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = Inc - Icc; + *(ptrd1++) = Icn - Icc; + } + } + } break; + case 2 : { // Sobel scheme. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + const unsigned long off = c*_width*_height*_depth + z*_width*_height; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = -Ipp - 2*Ipc - Ipn + Inp + 2*Inc + Inn; + *(ptrd1++) = -Ipp - 2*Icp - Inp + Ipn + 2*Icn + Inn; + } + } + } break; + case 3 : { // Rotation invariant mask. +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + const unsigned long off = c*_width*_height*_depth + z*_width*_height; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; + CImg_3x3(I,Tfloat); + const Tfloat a = (Tfloat)(0.25f*(2-std::sqrt(2.0f))), b = (Tfloat)(0.5f*(std::sqrt(2.0f)-1)); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = -a*Ipp - b*Ipc - a*Ipn + a*Inp + b*Inc + a*Inn; + *(ptrd1++) = -a*Ipp - b*Icp - a*Inp + a*Ipn + b*Icn + a*Inn; + } + } + } break; + case 4 : { // Van Vliet filter with low standard variation + grad[0] = get_deriche(0,1,'x'); + grad[1] = get_deriche(0,1,'y'); + } break; + case 5 : { // Deriche filter with low standard variation + grad[0] = get_vanvliet(0,1,'x'); + grad[1] = get_vanvliet(0,1,'y'); + } break; + default : { // Central finite differences +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + const unsigned long off = c*_width*_height*_depth + z*_width*_height; + Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = (Inc - Ipc)/2; + *(ptrd1++) = (Icn - Icp)/2; + } + } + } + } + if (!axes) return grad; + CImgList res; + for (unsigned int l = 0; axes[l]; ++l) { + const char axis = cimg::uncase(axes[l]); + switch (axis) { + case 'x' : res.insert(grad[0]); break; + case 'y' : res.insert(grad[1]); break; + case 'z' : res.insert(grad[2]); break; + } + } + grad.assign(); + return res; + } + + //! Return image hessian. + /** + \param axes Axes considered for the hessian computation, as a C-string (e.g "xy"). + **/ + CImgList get_hessian(const char *const axes=0) const { + CImgList res; + const char *naxes = axes, *const def_axes2d = "xxxyyy", *const def_axes3d = "xxxyxzyyyzzz"; + if (!axes) naxes = _depth>1?def_axes3d:def_axes2d; + const unsigned int lmax = std::strlen(naxes); + if (lmax%2) + throw CImgArgumentException(_cimg_instance + "get_hessian(): Invalid specified axes '%s'.", + cimg_instance, + naxes); + + res.assign(lmax/2,_width,_height,_depth,_spectrum); + if (!cimg::strcasecmp(naxes,def_axes3d)) { // 3d + +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + const unsigned long off = c*_width*_height*_depth; + Tfloat + *ptrd0 = res[0]._data + off, *ptrd1 = res[1]._data + off, *ptrd2 = res[2]._data + off, + *ptrd3 = res[3]._data + off, *ptrd4 = res[4]._data + off, *ptrd5 = res[5]._data + off; + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = Ipcc + Incc - 2*Iccc; // Ixx + *(ptrd1++) = (Ippc + Innc - Ipnc - Inpc)/4; // Ixy + *(ptrd2++) = (Ipcp + Incn - Ipcn - Incp)/4; // Ixz + *(ptrd3++) = Icpc + Icnc - 2*Iccc; // Iyy + *(ptrd4++) = (Icpp + Icnn - Icpn - Icnp)/4; // Iyz + *(ptrd5++) = Iccn + Iccp - 2*Iccc; // Izz + } + } + } else if (!cimg::strcasecmp(naxes,def_axes2d)) { // 2d +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + const unsigned long off = c*_width*_height*_depth + z*_width*_height; + Tfloat *ptrd0 = res[0]._data + off, *ptrd1 = res[1]._data + off, *ptrd2 = res[2]._data + off; + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) { + *(ptrd0++) = Ipc + Inc - 2*Icc; // Ixx + *(ptrd1++) = (Ipp + Inn - Ipn - Inp)/4; // Ixy + *(ptrd2++) = Icp + Icn - 2*Icc; // Iyy + } + } + } else for (unsigned int l = 0; laxis2) cimg::swap(axis1,axis2); + bool valid_axis = false; + if (axis1=='x' && axis2=='x') { // Ixx + valid_axis = true; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + Tfloat *ptrd = res[l2].data(0,0,z,c); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Ipc + Inc - 2*Icc; + } + } + else if (axis1=='x' && axis2=='y') { // Ixy + valid_axis = true; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + Tfloat *ptrd = res[l2].data(0,0,z,c); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = (Ipp + Inn - Ipn - Inp)/4; + } + } + else if (axis1=='x' && axis2=='z') { // Ixz + valid_axis = true; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = res[l2].data(0,0,0,c); + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = (Ipcp + Incn - Ipcn - Incp)/4; + } + } + else if (axis1=='y' && axis2=='y') { // Iyy + valid_axis = true; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forZC(*this,z,c) { + Tfloat *ptrd = res[l2].data(0,0,z,c); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Icp + Icn - 2*Icc; + } + } + else if (axis1=='y' && axis2=='z') { // Iyz + valid_axis = true; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = res[l2].data(0,0,0,c); + CImg_3x3x3(I,Tfloat); + cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = (Icpp + Icnn - Icpn - Icnp)/4; + } + } + else if (axis1=='z' && axis2=='z') { // Izz + valid_axis = true; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = res[l2].data(0,0,0,c); + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Iccn + Iccp - 2*Iccc; + } + } + else if (!valid_axis) + throw CImgArgumentException(_cimg_instance + "get_hessian(): Invalid specified axes '%s'.", + cimg_instance, + naxes); + } + return res; + } + + //! Compute image laplacian. + CImg& laplacian() { + return get_laplacian().move_to(*this); + } + + //! Compute image laplacian \newinstance. + CImg get_laplacian() const { + if (is_empty()) return CImg(); + CImg res(_width,_height,_depth,_spectrum); + if (_depth>1) { // 3d +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = res.data(0,0,0,c); + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Incc + Ipcc + Icnc + Icpc + Iccn + Iccp - 6*Iccc; + } + } else if (_height>1) { // 2d +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = res.data(0,0,0,c); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc + Icn + Icp - 4*Icc; + } + } else { // 1d +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width>=1048576 && _height*_depth*_spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd = res.data(0,0,0,c); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc - 2*Icc; + } + } + return res; + } + + //! Compute the structure tensor field of an image. + /** + \param scheme Numerical scheme. Can be { 0=central | 1=fwd/bwd1 | 2=fwd/bwd2 } + **/ + CImg& structure_tensors(const unsigned int scheme=2) { + return get_structure_tensors(scheme).move_to(*this); + } + + //! Compute the structure tensor field of an image \newinstance. + CImg get_structure_tensors(const unsigned int scheme=2) const { + if (is_empty()) return *this; + CImg res; + if (_depth>1) { // 3d + res.assign(_width,_height,_depth,6,0); + switch (scheme) { + case 0 : { // classical central finite differences +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat + *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2), + *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5); + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat + ix = (Incc - Ipcc)/2, + iy = (Icnc - Icpc)/2, + iz = (Iccn - Iccp)/2; + *(ptrd0++)+=ix*ix; + *(ptrd1++)+=ix*iy; + *(ptrd2++)+=ix*iz; + *(ptrd3++)+=iy*iy; + *(ptrd4++)+=iy*iz; + *(ptrd5++)+=iz*iz; + } + } + } break; + case 1 : { // Forward/backward finite differences (version 1). +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat + *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2), + *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5); + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat + ixf = Incc - Iccc, ixb = Iccc - Ipcc, + iyf = Icnc - Iccc, iyb = Iccc - Icpc, + izf = Iccn - Iccc, izb = Iccc - Iccp; + *(ptrd0++)+=(ixf*ixf + 2*ixf*ixb + ixb*ixb)/4; + *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; + *(ptrd2++)+=(ixf*izf + ixf*izb + ixb*izf + ixb*izb)/4; + *(ptrd3++)+=(iyf*iyf + 2*iyf*iyb + iyb*iyb)/4; + *(ptrd4++)+=(iyf*izf + iyf*izb + iyb*izf + iyb*izb)/4; + *(ptrd5++)+=(izf*izf + 2*izf*izb + izb*izb)/4; + } + } + } break; + default : { // Forward/backward finite differences (version 2). +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat + *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2), + *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5); + CImg_3x3x3(I,Tfloat); + cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { + const Tfloat + ixf = Incc - Iccc, ixb = Iccc - Ipcc, + iyf = Icnc - Iccc, iyb = Iccc - Icpc, + izf = Iccn - Iccc, izb = Iccc - Iccp; + *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2; + *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; + *(ptrd2++)+=(ixf*izf + ixf*izb + ixb*izf + ixb*izb)/4; + *(ptrd3++)+=(iyf*iyf + iyb*iyb)/2; + *(ptrd4++)+=(iyf*izf + iyf*izb + iyb*izf + iyb*izb)/4; + *(ptrd5++)+=(izf*izf + izb*izb)/2; + } + } + } break; + } + } else { // 2d + res.assign(_width,_height,_depth,3,0); + switch (scheme) { + case 0 : { // classical central finite differences +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) { + const Tfloat + ix = (Inc - Ipc)/2, + iy = (Icn - Icp)/2; + *(ptrd0++)+=ix*ix; + *(ptrd1++)+=ix*iy; + *(ptrd2++)+=iy*iy; + } + } + } break; + case 1 : { // Forward/backward finite differences (version 1). +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) { + const Tfloat + ixf = Inc - Icc, ixb = Icc - Ipc, + iyf = Icn - Icc, iyb = Icc - Icp; + *(ptrd0++)+=(ixf*ixf + 2*ixf*ixb + ixb*ixb)/4; + *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; + *(ptrd2++)+=(iyf*iyf + 2*iyf*iyb + iyb*iyb)/4; + } + } + } break; + default : { // Forward/backward finite differences (version 2). +#ifdef cimg_use_openmp +#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) +#endif + cimg_forC(*this,c) { + Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2); + CImg_3x3(I,Tfloat); + cimg_for3x3(*this,x,y,0,c,I,Tfloat) { + const Tfloat + ixf = Inc - Icc, ixb = Icc - Ipc, + iyf = Icn - Icc, iyb = Icc - Icp; + *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2; + *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; + *(ptrd2++)+=(iyf*iyf + iyb*iyb)/2; + } + } + } break; + } + } + return res; + } + + //! Compute field of diffusion tensors for edge-preserving smoothing. + /** + \param sharpness Sharpness + \param anisotropy Anisotropy + \param alpha Standard deviation of the gradient blur. + \param sigma Standard deviation of the structure tensor blur. + \param is_sqrt Tells if the square root of the tensor field is computed instead. + **/ + CImg& diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f, + const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) { + CImg res; + const float + nsharpness = cimg::max(sharpness,1e-5f), + power1 = (is_sqrt?0.5f:1)*nsharpness, + power2 = power1/(1e-7f+1-anisotropy); + blur(alpha).normalize(0,(T)255); + + if (_depth>1) { // 3d + get_structure_tensors().move_to(res).blur(sigma); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if(_width>=256 && _height*_depth>=256) +#endif + cimg_forYZ(*this,y,z) { + Tfloat + *ptrd0 = res.data(0,y,z,0), *ptrd1 = res.data(0,y,z,1), *ptrd2 = res.data(0,y,z,2), + *ptrd3 = res.data(0,y,z,3), *ptrd4 = res.data(0,y,z,4), *ptrd5 = res.data(0,y,z,5); + CImg val(3), vec(3,3); + cimg_forX(*this,x) { + res.get_tensor_at(x,y,z).symmetric_eigen(val,vec); + const float + _l1 = val[2], _l2 = val[1], _l3 = val[0], + l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0, l3 = _l3>0?_l3:0, + ux = vec(0,0), uy = vec(0,1), uz = vec(0,2), + vx = vec(1,0), vy = vec(1,1), vz = vec(1,2), + wx = vec(2,0), wy = vec(2,1), wz = vec(2,2), + n1 = (float)std::pow(1+l1+l2+l3,-power1), + n2 = (float)std::pow(1+l1+l2+l3,-power2); + *(ptrd0++) = n1*(ux*ux + vx*vx) + n2*wx*wx; + *(ptrd1++) = n1*(ux*uy + vx*vy) + n2*wx*wy; + *(ptrd2++) = n1*(ux*uz + vx*vz) + n2*wx*wz; + *(ptrd3++) = n1*(uy*uy + vy*vy) + n2*wy*wy; + *(ptrd4++) = n1*(uy*uz + vy*vz) + n2*wy*wz; + *(ptrd5++) = n1*(uz*uz + vz*vz) + n2*wz*wz; + } + } + } else { // for 2d images + get_structure_tensors().move_to(res).blur(sigma); +#ifdef cimg_use_openmp +#pragma omp parallel for if(_width>=256 && _height>=256) +#endif + cimg_forY(*this,y) { + Tfloat *ptrd0 = res.data(0,y,0,0), *ptrd1 = res.data(0,y,0,1), *ptrd2 = res.data(0,y,0,2); + CImg val(2), vec(2,2); + cimg_forX(*this,x) { + res.get_tensor_at(x,y).symmetric_eigen(val,vec); + const float + _l1 = val[1], _l2 = val[0], + l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0, + ux = vec(1,0), uy = vec(1,1), + vx = vec(0,0), vy = vec(0,1), + n1 = (float)std::pow(1+l1+l2,-power1), + n2 = (float)std::pow(1+l1+l2,-power2); + *(ptrd0++) = n1*ux*ux + n2*vx*vx; + *(ptrd1++) = n1*ux*uy + n2*vx*vy; + *(ptrd2++) = n1*uy*uy + n2*vy*vy; + } + } + } + return res.move_to(*this); + } + + //! Compute field of diffusion tensors for edge-preserving smoothing \newinstance. + CImg get_diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f, + const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) const { + return CImg(*this,false).diffusion_tensors(sharpness,anisotropy,alpha,sigma,is_sqrt); + } + + //! Estimate displacement field between two images. + /** + \param source Reference image. + \param smoothness Smoothness of estimated displacement field. + \param precision Precision required for algorithm convergence. + \param nb_scales Number of scales used to estimate the displacement field. + \param iteration_max Maximum number of iterations allowed for one scale. + \param is_backward If false, match I2(X+U(X)) = I1(X), else match I2(X) = I1(X-U(X)). + **/ + CImg& displacement(const CImg& source, const float smoothness=0.1f, const float precision=5.0f, + const unsigned int nb_scales=0, const unsigned int iteration_max=10000, + const bool is_backward=false) { + return get_displacement(source,smoothness,precision,nb_scales,iteration_max,is_backward).move_to(*this); + } + + //! Estimate displacement field between two images \newinstance. + CImg get_displacement(const CImg& source, + const float smoothness=0.1f, const float precision=5.0f, + const unsigned int nb_scales=0, const unsigned int iteration_max=10000, + const bool is_backward=false) const { + if (is_empty() || !source) return +*this; + if (!is_sameXYZC(source)) + throw CImgArgumentException(_cimg_instance + "displacement(): Instance and source image (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + source._width,source._height,source._depth,source._spectrum,source._data); + if (precision<0) + throw CImgArgumentException(_cimg_instance + "displacement(): Invalid specified precision %g " + "(should be >=0)", + cimg_instance, + precision); + const unsigned int _nb_scales = nb_scales>0?nb_scales: + (unsigned int)(2*std::log((double)(cimg::max(_width,_height)))); + const float _precision = (float)std::pow(10.0,-(double)precision); + float sm, sM = source.max_min(sm), tm, tM = max_min(tm); + const float sdelta = sm==sM?1:(sM - sm), tdelta = tm==tM?1:(tM - tm); + const bool is_3d = source._depth>1; + CImg U; + + for (int scale = _nb_scales-1; scale>=0; --scale) { + const float factor = (float)std::pow(1.5,(double)scale); + const unsigned int + _sw = (unsigned int)(_width/factor), sw = _sw?_sw:1, + _sh = (unsigned int)(_height/factor), sh = _sh?_sh:1, + _sd = (unsigned int)(_depth/factor), sd = _sd?_sd:1; + if (sw<5 && sh<5 && (!is_3d || sd<5)) continue; // skip too small scales. + const CImg + I1 = (source.get_resize(sw,sh,sd,-100,2)-=sm)/=sdelta, + I2 = (get_resize(I1,2)-=tm)/=tdelta; + if (U) (U*=1.5f).resize(I2._width,I2._height,I2._depth,-100,3); + else U.assign(I2._width,I2._height,I2._depth,is_3d?3:2,0); + float dt = 2, energy = cimg::type::max(); + const CImgList dI = is_backward?I1.get_gradient():I2.get_gradient(); + + for (unsigned int iteration = 0; iteration=0) cimg_for3XYZ(U,x,y,z) { // Isotropic regularization. + const float + X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0), + Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1), + Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2); + float delta_I = 0, _energy_regul = 0; + if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c)); + else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2.linear_atXYZ(X,Y,Z,c)); + cimg_forC(U,c) { + const float + Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)), + Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)), + Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)), + Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c), + Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c), + Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c); + U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c].linear_atXYZ(X,Y,Z) + + smoothness* ( Uxx + Uyy + Uzz)))/(1+6*smoothness*dt); + _energy_regul+=Ux*Ux + Uy*Uy + Uz*Uz; + } + _energy+=delta_I*delta_I + smoothness*_energy_regul; + } else { + const float nsmoothness = -smoothness; + cimg_for3XYZ(U,x,y,z) { // Anisotropic regularization. + const float + X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0), + Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1), + Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2); + float delta_I = 0, _energy_regul = 0; + if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c)); + else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2.linear_atXYZ(X,Y,Z,c)); + cimg_forC(U,c) { + const float + Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)), + Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)), + Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)), + N2 = Ux*Ux + Uy*Uy + Uz*Uz, + N = std::sqrt(N2), + N3 = 1e-5f + N2*N, + coef_a = (1 - Ux*Ux/N2)/N, + coef_b = -2*Ux*Uy/N3, + coef_c = -2*Ux*Uz/N3, + coef_d = (1 - Uy*Uy/N2)/N, + coef_e = -2*Uy*Uz/N3, + coef_f = (1 - Uz*Uz/N2)/N, + Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c), + Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c), + Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c), + Uxy = 0.25f*(U(_n1x,_n1y,z,c) + U(_p1x,_p1y,z,c) - U(_n1x,_p1y,z,c) - U(_n1x,_p1y,z,c)), + Uxz = 0.25f*(U(_n1x,y,_n1z,c) + U(_p1x,y,_p1z,c) - U(_n1x,y,_p1z,c) - U(_n1x,y,_p1z,c)), + Uyz = 0.25f*(U(x,_n1y,_n1z,c) + U(x,_p1y,_p1z,c) - U(x,_n1y,_p1z,c) - U(x,_n1y,_p1z,c)); + U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c].linear_atXYZ(X,Y,Z) + + nsmoothness* ( coef_a*Uxx + coef_b*Uxy + + coef_c*Uxz + coef_d*Uyy + + coef_e*Uyz + coef_f*Uzz )) + )/(1+2*(coef_a+coef_d+coef_f)*nsmoothness*dt); + _energy_regul+=N; + } + _energy+=delta_I*delta_I + nsmoothness*_energy_regul; + } + } + } else { // 2d version. + if (smoothness>=0) cimg_for3XY(U,x,y) { // Isotropic regularization. + const float + X = is_backward?x - U(x,y,0):x + U(x,y,0), + Y = is_backward?y - U(x,y,1):y + U(x,y,1); + float delta_I = 0, _energy_regul = 0; + if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXY(X,Y,c) - I2(x,y,c)); + else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2.linear_atXY(X,Y,c)); + cimg_forC(U,c) { + const float + Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)), + Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)), + Uxx = U(_n1x,y,c) + U(_p1x,y,c), + Uyy = U(x,_n1y,c) + U(x,_p1y,c); + U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c].linear_atXY(X,Y) + + smoothness*( Uxx + Uyy )))/(1+4*smoothness*dt); + _energy_regul+=Ux*Ux + Uy*Uy; + } + _energy+=delta_I*delta_I + smoothness*_energy_regul; + } else { + const float nsmoothness = -smoothness; + cimg_for3XY(U,x,y) { // Anisotropic regularization. + const float + X = is_backward?x - U(x,y,0):x + U(x,y,0), + Y = is_backward?y - U(x,y,1):y + U(x,y,1); + float delta_I = 0, _energy_regul = 0; + if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXY(X,Y,c) - I2(x,y,c)); + else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2.linear_atXY(X,Y,c)); + cimg_forC(U,c) { + const float + Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)), + Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)), + N2 = Ux*Ux + Uy*Uy, + N = std::sqrt(N2), + N3 = 1e-5f + N2*N, + coef_a = Uy*Uy/N3, + coef_b = -2*Ux*Uy/N3, + coef_c = Ux*Ux/N3, + Uxx = U(_n1x,y,c) + U(_p1x,y,c), + Uyy = U(x,_n1y,c) + U(x,_p1y,c), + Uxy = 0.25f*(U(_n1x,_n1y,c) + U(_p1x,_p1y,c) - U(_n1x,_p1y,c) - U(_n1x,_p1y,c)); + U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c].linear_atXY(X,Y) + + nsmoothness*( coef_a*Uxx + coef_b*Uxy + coef_c*Uyy )))/ + (1+2*(coef_a+coef_c)*nsmoothness*dt); + _energy_regul+=N; + } + _energy+=delta_I*delta_I + nsmoothness*_energy_regul; + } + } + } + const float d_energy = (_energy - energy)/(sw*sh*sd); + if (d_energy<=0 && -d_energy<_precision) break; + if (d_energy>0) dt*=0.5f; + energy = _energy; + } + } + return U; + } + + //! Compute Euclidean distance function to a specified value. + /** + \param value Reference value. + \param metric Type of metric. Can be { 0=Chebyshev | 1=Manhattan | 2=Euclidean | 3=Squared-euclidean }. + \note + The distance transform implementation has been submitted by A. Meijster, and implements + the article 'W.H. Hesselink, A. Meijster, J.B.T.M. Roerdink, + "A general algorithm for computing distance transforms in linear time.", + In: Mathematical Morphology and its Applications to Image and Signal Processing, + J. Goutsias, L. Vincent, and D.S. Bloomberg (eds.), Kluwer, 2000, pp. 331-340.' + The submitted code has then been modified to fit CImg coding style and constraints. + **/ + CImg& distance(const T value, const unsigned int metric=2) { + if (is_empty()) return *this; + bool is_value = false; + cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,0:(T)999999999; + if (!is_value) return fill(cimg::type::max()); + switch (metric) { + case 0 : return _distance_core(_distance_sep_cdt,_distance_dist_cdt); // Chebyshev. + case 1 : return _distance_core(_distance_sep_mdt,_distance_dist_mdt); // Manhattan. + case 3 : return _distance_core(_distance_sep_edt,_distance_dist_edt); // Squared Euclidean. + default : return _distance_core(_distance_sep_edt,_distance_dist_edt).sqrt(); // Euclidean. + } + return *this; + } + + //! Compute distance to a specified value \newinstance. + CImg get_distance(const T value, const unsigned int metric=2) const { + return CImg(*this,false).distance((Tfloat)value,metric); + } + + static long _distance_sep_edt(const long i, const long u, const long *const g) { + return (u*u-i*i+g[u]-g[i])/(2*(u-i)); + } + + static long _distance_dist_edt(const long x, const long i, const long *const g) { + return (x-i)*(x-i) + g[i]; + } + + static long _distance_sep_mdt(const long i, const long u, const long *const g) { + return (u-i<=g[u]-g[i]?999999999:(g[u]-g[i]+u+i)/2); + } + + static long _distance_dist_mdt(const long x, const long i, const long *const g) { + return (x=0) && f(t[q],s[q],g)>f(t[q],u,g)) { --q; } + if (q<0) { q = 0; s[0] = u; } + else { const long w = 1 + sep(s[q], u, g); if (w<(long)len) { ++q; s[q] = u; t[q] = w; }} + } + for (int u = (int)len-1; u>=0; --u) { dt[u] = f(u,s[q],g); if (u==t[q]) --q; } // Backward scan. + } + + CImg& _distance_core(long (*const sep)(const long, const long, const long *const), + long (*const f)(const long, const long, const long *const)) { + const unsigned long wh = (unsigned long)_width*_height; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + CImg g(_width), dt(_width), s(_width), t(_width); + CImg img = get_shared_channel(c); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) firstprivate(g,dt,s,t) +#endif + cimg_forYZ(*this,y,z) { // Over X-direction. + cimg_forX(*this,x) g[x] = (long)img(x,y,z,0,wh); + _distance_scan(_width,g,sep,f,s,t,dt); + cimg_forX(*this,x) img(x,y,z,0,wh) = (T)dt[x]; + } + if (_height>1) { + g.assign(_height); dt.assign(_height); s.assign(_height); t.assign(_height); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_height>=512 && _width*_depth>=16) firstprivate(g,dt,s,t) +#endif + cimg_forXZ(*this,x,z) { // Over Y-direction. + cimg_forY(*this,y) g[y] = (long)img(x,y,z,0,wh); + _distance_scan(_height,g,sep,f,s,t,dt); + cimg_forY(*this,y) img(x,y,z,0,wh) = (T)dt[y]; + } + } + if (_depth>1) { + g.assign(_depth); dt.assign(_depth); s.assign(_depth); t.assign(_depth); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if (_depth>=512 && _width*_height>=16) firstprivate(g,dt,s,t) +#endif + cimg_forXY(*this,x,y) { // Over Z-direction. + cimg_forZ(*this,z) g[z] = (long)img(x,y,z,0,wh); + _distance_scan(_depth,g,sep,f,s,t,dt); + cimg_forZ(*this,z) img(x,y,z,0,wh) = (T)dt[z]; + } + } + } + return *this; + } + + //! Compute chamfer distance to a specified value, with a custom metric. + /** + \param value Reference value. + \param metric_mask Metric mask. + \note The algorithm code has been initially proposed by A. Meijster, and modified by D. Tschumperlé. + **/ + template + CImg& distance(const T value, const CImg& metric_mask) { + if (is_empty()) return *this; + bool is_value = false; + cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,0:(T)999999999; + if (!is_value) return fill(cimg::type::max()); + const unsigned long wh = (unsigned long)_width*_height; +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) +#endif + cimg_forC(*this,c) { + CImg img = get_shared_channel(c); +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(3) if (_width*_height*_depth>=1024) +#endif + cimg_forXYZ(metric_mask,dx,dy,dz) { + const t weight = metric_mask(dx,dy,dz); + if (weight) { + for (int z = dz, nz = 0; z=0; --z,--nz) { // Backward scan. + for (int y = height() - 1 - dy, ny = height() - 1; y>=0; --y,--ny) { + for (int x = width() - 1 - dx, nx = width() - 1; x>=0; --x,--nx) { + const T dd = img(nx,ny,nz,0,wh) + weight; + if (dd + CImg get_distance(const T value, const CImg& metric_mask) const { + return CImg(*this,false).distance(value,metric_mask); + } + + //! Compute distance to a specified value, according to a custom metric (use dijkstra algorithm). + /** + \param value Reference value. + \param metric Field of distance potentials. + \param is_high_connectivity Tells if the algorithm uses low or high connectivity. + **/ + template + CImg& distance_dijkstra(const T value, const CImg& metric, const bool is_high_connectivity, + CImg& return_path) { + return get_distance_dijkstra(value,metric,is_high_connectivity,return_path).move_to(*this); + } + + //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \newinstance. + template + CImg::type> + get_distance_dijkstra(const T value, const CImg& metric, const bool is_high_connectivity, + CImg& return_path) const { + if (is_empty()) return return_path.assign(); + if (!is_sameXYZ(metric)) + throw CImgArgumentException(_cimg_instance + "distance_dijkstra(): image instance and metric map (%u,%u,%u,%u) " + "have incompatible dimensions.", + cimg_instance, + metric._width,metric._height,metric._depth,metric._spectrum); + typedef typename cimg::superset::type td; // Type used for computing cumulative distances. + CImg result(_width,_height,_depth,_spectrum), Q; + CImg is_queued(_width,_height,_depth,1); + if (return_path) return_path.assign(_width,_height,_depth,_spectrum); + + cimg_forC(*this,c) { + const CImg img = get_shared_channel(c); + const CImg met = metric.get_shared_channel(c%metric._spectrum); + CImg res = result.get_shared_channel(c); + CImg path = return_path?return_path.get_shared_channel(c):CImg(); + unsigned int sizeQ = 0; + + // Detect initial seeds. + is_queued.fill(0); + cimg_forXYZ(img,x,y,z) if (img(x,y,z)==value) { + Q._priority_queue_insert(is_queued,sizeQ,0,x,y,z); + res(x,y,z) = 0; + if (path) path(x,y,z) = (to)0; + } + + // Start distance propagation. + while (sizeQ) { + + // Get and remove point with minimal potential from the queue. + const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3); + const td P = (td)-Q(0,0); + Q._priority_queue_remove(sizeQ); + + // Update neighbors. + td npot = 0; + if (x-1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x-1,y,z)+P),x-1,y,z)) { + res(x-1,y,z) = npot; if (path) path(x-1,y,z) = (to)2; + } + if (x+1=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y-1,z)+P),x,y-1,z)) { + res(x,y-1,z) = npot; if (path) path(x,y-1,z) = (to)8; + } + if (y+1=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y,z-1)+P),x,y,z-1)) { + res(x,y,z-1) = npot; if (path) path(x,y,z-1) = (to)32; + } + if (z+1=0 && y-1>=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x-1,y-1,z)+P)),x-1,y-1,z)) { + res(x-1,y-1,z) = npot; if (path) path(x-1,y-1,z) = (to)10; + } + if (x+1=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x+1,y-1,z)+P)),x+1,y-1,z)) { + res(x+1,y-1,z) = npot; if (path) path(x+1,y-1,z) = (to)9; + } + if (x-1>=0 && y+1=0) { // Diagonal neighbors on slice z-1. + if (x-1>=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x-1,y,z-1)+P)),x-1,y,z-1)) { + res(x-1,y,z-1) = npot; if (path) path(x-1,y,z-1) = (to)34; + } + if (x+1=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y-1,z-1)+P)),x,y-1,z-1)) { + res(x,y-1,z-1) = npot; if (path) path(x,y-1,z-1) = (to)40; + } + if (y+1=0 && y-1>=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x-1,y-1,z-1)+P)),x-1,y-1,z-1)) { + res(x-1,y-1,z-1) = npot; if (path) path(x-1,y-1,z-1) = (to)42; + } + if (x+1=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x+1,y-1,z-1)+P)),x+1,y-1,z-1)) { + res(x+1,y-1,z-1) = npot; if (path) path(x+1,y-1,z-1) = (to)41; + } + if (x-1>=0 && y+1=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x-1,y,z+1)+P)),x-1,y,z+1)) { + res(x-1,y,z+1) = npot; if (path) path(x-1,y,z+1) = (to)18; + } + if (x+1=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y-1,z+1)+P)),x,y-1,z+1)) { + res(x,y-1,z+1) = npot; if (path) path(x,y-1,z+1) = (to)24; + } + if (y+1=0 && y-1>=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x-1,y-1,z+1)+P)),x-1,y-1,z+1)) { + res(x-1,y-1,z+1) = npot; if (path) path(x-1,y-1,z+1) = (to)26; + } + if (x+1=0 && + Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x+1,y-1,z+1)+P)),x+1,y-1,z+1)) { + res(x+1,y-1,z+1) = npot; if (path) path(x+1,y-1,z+1) = (to)25; + } + if (x-1>=0 && y+1 + CImg& distance_dijkstra(const T value, const CImg& metric, + const bool is_high_connectivity=false) { + return get_distance_dijkstra(value,metric,is_high_connectivity).move_to(*this); + } + + //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \newinstance. + template + CImg get_distance_dijkstra(const T value, const CImg& metric, + const bool is_high_connectivity=false) const { + CImg return_path; + return get_distance_dijkstra(value,metric,is_high_connectivity,return_path); + } + + //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm). + /** + \param value Reference value. + \param metric Field of distance potentials. + **/ + template + CImg& distance_eikonal(const T value, const CImg& metric) { + return get_distance_eikonal(value,metric).move_to(*this); + } + + //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm). + template + CImg get_distance_eikonal(const T value, const CImg& metric) const { + if (is_empty()) return *this; + if (!is_sameXYZ(metric)) + throw CImgArgumentException(_cimg_instance + "distance_eikonal(): image instance and metric map (%u,%u,%u,%u) have " + "incompatible dimensions.", + cimg_instance, + metric._width,metric._height,metric._depth,metric._spectrum); + CImg result(_width,_height,_depth,_spectrum,cimg::type::max()), Q; + CImg state(_width,_height,_depth); // -1=far away, 0=narrow, 1=frozen. + +#ifdef cimg_use_openmp +#pragma omp parallel for if (_spectrum>=2) firstprivate(Q,state) +#endif + cimg_forC(*this,c) { + const CImg img = get_shared_channel(c); + const CImg met = metric.get_shared_channel(c%metric._spectrum); + CImg res = result.get_shared_channel(c); + unsigned int sizeQ = 0; + state.fill(-1); + + // Detect initial seeds. + Tfloat *ptr1 = res._data; char *ptr2 = state._data; + cimg_for(img,ptr0,T) { if (*ptr0==value) { *ptr1 = 0; *ptr2 = 1; } ++ptr1; ++ptr2; } + + // Initialize seeds neighbors. + ptr2 = state._data; + cimg_forXYZ(img,x,y,z) if (*(ptr2++)==1) { + if (x-1>=0 && state(x-1,y,z)==-1) { + const Tfloat dist = res(x-1,y,z) = __distance_eikonal(res,met(x-1,y,z),x-1,y,z); + Q._eik_priority_queue_insert(state,sizeQ,-dist,x-1,y,z); + } + if (x+1=0 && state(x,y-1,z)==-1) { + const Tfloat dist = res(x,y-1,z) = __distance_eikonal(res,met(x,y-1,z),x,y-1,z); + Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y-1,z); + } + if (y+1=0 && state(x,y,z-1)==-1) { + const Tfloat dist = res(x,y,z-1) = __distance_eikonal(res,met(x,y,z-1),x,y,z-1); + Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z-1); + } + if (z+1=0) { + if (x-1>=0 && state(x-1,y,z)!=1) { + const Tfloat dist = __distance_eikonal(res,met(x-1,y,z),x-1,y,z); + if (dist=0 && state(x,y-1,z)!=1) { + const Tfloat dist = __distance_eikonal(res,met(x,y-1,z),x,y-1,z); + if (dist=0 && state(x,y,z-1)!=1) { + const Tfloat dist = __distance_eikonal(res,met(x,y,z-1),x,y,z-1); + if (dist& res, const Tfloat P, + const int x=0, const int y=0, const int z=0) const { + const T M = cimg::type::max(); + T T1 = cimg::min(x-1>=0?res(x-1,y,z):M,x+11) { // 3d. + T + T2 = cimg::min(y-1>=0?res(x,y-1,z):M,y+1=0?res(x,y,z-1):M,z+1T2) cimg::swap(T1,T2); + if (T2>T3) cimg::swap(T2,T3); + if (T1>T2) cimg::swap(T1,T2); + if (P<=0) return (Tfloat)T1; + if (T31) { // 2d. + T T2 = cimg::min(y-1>=0?res(x,y-1,z):M,y+1T2) cimg::swap(T1,T2); + if (P<=0) return (Tfloat)T1; + if (T2 + void _eik_priority_queue_insert(CImg& state, unsigned int& siz, const t value, + const unsigned int x, const unsigned int y, const unsigned int z) { + if (state(x,y,z)>0) return; + state(x,y,z) = 0; + if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); } + (*this)(siz-1,0) = (T)value; (*this)(siz-1,1) = (T)x; (*this)(siz-1,2) = (T)y; (*this)(siz-1,3) = (T)z; + for (unsigned int pos = siz - 1, par = 0; pos && value>(*this)(par=(pos+1)/2-1,0); pos = par) { + cimg::swap((*this)(pos,0),(*this)(par,0)); cimg::swap((*this)(pos,1),(*this)(par,1)); + cimg::swap((*this)(pos,2),(*this)(par,2)); cimg::swap((*this)(pos,3),(*this)(par,3)); + } + } + + //! Compute distance function to 0-valued isophotes, using the Eikonal PDE. + /** + \param nb_iterations Number of PDE iterations. + \param band_size Size of the narrow band. + \param time_step Time step of the PDE iterations. + **/ + CImg& distance_eikonal(const unsigned int nb_iterations, const float band_size=0, const float time_step=0.5f) { + if (is_empty()) return *this; + CImg velocity(*this); + for (unsigned int iteration = 0; iteration1) { // 3d + CImg_3x3x3(I,Tfloat); + cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) if (band_size<=0 || cimg::abs(Iccc)0?(Incc - Iccc):(Iccc - Ipcc), + iy = gy*sgn>0?(Icnc - Iccc):(Iccc - Icpc), + iz = gz*sgn>0?(Iccn - Iccc):(Iccc - Iccp), + ng = (Tfloat)(1e-5f + std::sqrt(gx*gx + gy*gy + gz*gz)), + ngx = gx/ng, + ngy = gy/ng, + ngz = gz/ng, + veloc = sgn*(ngx*ix + ngy*iy + ngz*iz - 1); + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } else *(ptrd++) = 0; + } else { // 2d version + CImg_3x3(I,Tfloat); + cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,Tfloat) if (band_size<=0 || cimg::abs(Icc)0?(Inc - Icc):(Icc - Ipc), + iy = gy*sgn>0?(Icn - Icc):(Icc - Icp), + ng = (Tfloat)(1e-5f + std::sqrt(gx*gx + gy*gy)), + ngx = gx/ng, + ngy = gy/ng, + veloc = sgn*(ngx*ix + ngy*iy - 1); + *(ptrd++) = veloc; + if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; + } else *(ptrd++) = 0; + } + if (veloc_max>0) *this+=(velocity*=time_step/veloc_max); + } + return *this; + } + + //! Compute distance function to 0-valued isophotes, using the Eikonal PDE \newinstance. + CImg get_distance_eikonal(const unsigned int nb_iterations, const float band_size=0, + const float time_step=0.5f) const { + return CImg(*this,false).distance_eikonal(nb_iterations,band_size,time_step); + } + + //! Compute Haar multiscale wavelet transform. + /** + \param axis Axis considered for the transform. + \param invert Set inverse of direct transform. + \param nb_scales Number of scales used for the transform. + **/ + CImg& haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) { + return get_haar(axis,invert,nb_scales).move_to(*this); + } + + //! Compute Haar multiscale wavelet transform \newinstance. + CImg get_haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) const { + if (is_empty() || !nb_scales) return +*this; + CImg res; + const Tfloat sqrt2 = std::sqrt(2); + if (nb_scales==1) { + switch (cimg::uncase(axis)) { // Single scale transform + case 'x' : { + const unsigned int w = _width/2; + if (w) { + if ((w%2) && w!=1) + throw CImgInstanceException(_cimg_instance + "haar(): Sub-image width %u is not even.", + cimg_instance, + w); + + res.assign(_width,_height,_depth,_spectrum); + if (invert) cimg_forYZC(*this,y,z,c) { // Inverse transform along X + for (unsigned int x = 0, xw = w, x2 = 0; x& haar(const bool invert=false, const unsigned int nb_scales=1) { + return get_haar(invert,nb_scales).move_to(*this); + } + + //! Compute Haar multiscale wavelet transform \newinstance. + CImg get_haar(const bool invert=false, const unsigned int nb_scales=1) const { + CImg res; + if (nb_scales==1) { // Single scale transform + if (_width>1) get_haar('x',invert,1).move_to(res); + if (_height>1) { if (res) res.haar('y',invert,1); else get_haar('y',invert,1).move_to(res); } + if (_depth>1) { if (res) res.haar('z',invert,1); else get_haar('z',invert,1).move_to(res); } + if (res) return res; + } else { // Multi-scale transform + if (invert) { // Inverse transform + res.assign(*this); + if (_width>1) { + if (_height>1) { + if (_depth>1) { + unsigned int w = _width, h = _height, d = _depth; + for (unsigned int s = 1; w && h && d && s1) { + unsigned int w = _width, d = _depth; + for (unsigned int s = 1; w && d && s1) { + if (_depth>1) { + unsigned int h = _height, d = _depth; + for (unsigned int s = 1; h && d && s1) { + unsigned int d = _depth; + for (unsigned int s = 1; d && s1) { + if (_height>1) { + if (_depth>1) + for (unsigned int s = 1, w = _width/2, h = _height/2, d = _depth/2; w && h && d && s1) for (unsigned int s = 1, w = _width/2, d = _depth/2; w && d && s1) { + if (_depth>1) + for (unsigned int s = 1, h = _height/2, d = _depth/2; h && d && s1) for (unsigned int s = 1, d = _depth/2; d && s get_FFT(const char axis, const bool is_invert=false) const { + CImgList res(*this,CImg()); + CImg::FFT(res[0],res[1],axis,is_invert); + return res; + } + + //! Compute n-d Fast Fourier Transform. + /* + \param is_invert Tells if the forward (\c false) or inverse (\c true) FFT is computed. + **/ + CImgList get_FFT(const bool is_invert=false) const { + CImgList res(*this,CImg()); + CImg::FFT(res[0],res[1],is_invert); + return res; + } + + //! Compute 1d Fast Fourier Transform, along a specified axis. + /** + \param[in,out] real Real part of the pixel values. + \param[in,out] imag Imaginary part of the pixel values. + \param axis Axis along which the FFT is computed. + \param is_invert Tells if the forward (\c false) or inverse (\c true) FFT is computed. + **/ + static void FFT(CImg& real, CImg& imag, const char axis, const bool is_invert=false) { + if (!real) + throw CImgInstanceException("CImg<%s>::FFT(): Specified real part is empty.", + pixel_type()); + + if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,0); + if (!real.is_sameXYZC(imag)) + throw CImgInstanceException("CImg<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and " + "imaginary part (%u,%u,%u,%u,%p) have different dimensions.", + pixel_type(), + real._width,real._height,real._depth,real._spectrum,real._data, + imag._width,imag._height,imag._depth,imag._spectrum,imag._data); +#ifdef cimg_use_fftw3 + cimg::mutex(12); + fftw_complex *data_in; + fftw_plan data_plan; + + switch (cimg::uncase(axis)) { + case 'x' : { // Fourier along X, using FFTW library. + data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width); + if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " + "for computing FFT of image (%u,%u,%u,%u) along the X-axis.", + pixel_type(), + cimg::strbuffersize(sizeof(fftw_complex)*real._width), + real._width,real._height,real._depth,real._spectrum); + + data_plan = fftw_plan_dft_1d(real._width,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); + cimg_forYZC(real,y,z,c) { + T *ptrr = real.data(0,y,z,c), *ptri = imag.data(0,y,z,c); + double *ptrd = (double*)data_in; + cimg_forX(real,x) { *(ptrd++) = (double)*(ptrr++); *(ptrd++) = (double)*(ptri++); } + fftw_execute(data_plan); + const unsigned int fact = real._width; + if (is_invert) cimg_forX(real,x) { *(--ptri) = (T)(*(--ptrd)/fact); *(--ptrr) = (T)(*(--ptrd)/fact); } + else cimg_forX(real,x) { *(--ptri) = (T)*(--ptrd); *(--ptrr) = (T)*(--ptrd); } + } + } break; + case 'y' : { // Fourier along Y, using FFTW library. + data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * real._height); + if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " + "for computing FFT of image (%u,%u,%u,%u) along the Y-axis.", + pixel_type(), + cimg::strbuffersize(sizeof(fftw_complex)*real._height), + real._width,real._height,real._depth,real._spectrum); + + data_plan = fftw_plan_dft_1d(real._height,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); + const unsigned int off = real._width; + cimg_forXZC(real,x,z,c) { + T *ptrr = real.data(x,0,z,c), *ptri = imag.data(x,0,z,c); + double *ptrd = (double*)data_in; + cimg_forY(real,y) { *(ptrd++) = (double)*ptrr; *(ptrd++) = (double)*ptri; ptrr+=off; ptri+=off; } + fftw_execute(data_plan); + const unsigned int fact = real._height; + if (is_invert) + cimg_forY(real,y) { ptrr-=off; ptri-=off; *ptri = (T)(*(--ptrd)/fact); *ptrr = (T)(*(--ptrd)/fact); } + else cimg_forY(real,y) { ptrr-=off; ptri-=off; *ptri = (T)*(--ptrd); *ptrr = (T)*(--ptrd); } + } + } break; + case 'z' : { // Fourier along Z, using FFTW library. + data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * real._depth); + if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " + "for computing FFT of image (%u,%u,%u,%u) along the Z-axis.", + pixel_type(), + cimg::strbuffersize(sizeof(fftw_complex)*real._depth), + real._width,real._height,real._depth,real._spectrum); + + data_plan = fftw_plan_dft_1d(real._depth,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); + const unsigned long off = (unsigned long)real._width*real._height; + cimg_forXYC(real,x,y,c) { + T *ptrr = real.data(x,y,0,c), *ptri = imag.data(x,y,0,c); + double *ptrd = (double*)data_in; + cimg_forZ(real,z) { *(ptrd++) = (double)*ptrr; *(ptrd++) = (double)*ptri; ptrr+=off; ptri+=off; } + fftw_execute(data_plan); + const unsigned int fact = real._depth; + if (is_invert) + cimg_forZ(real,z) { ptrr-=off; ptri-=off; *ptri = (T)(*(--ptrd)/fact); *ptrr = (T)(*(--ptrd)/fact); } + else cimg_forZ(real,z) { ptrr-=off; ptri-=off; *ptri = (T)*(--ptrd); *ptrr = (T)*(--ptrd); } + } + } break; + default : { // Fourier along C, using FFTW library. + data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * real._spectrum); + if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " + "for computing FFT of image (%u,%u,%u,%u) along the C-axis.", + pixel_type(), + cimg::strbuffersize(sizeof(fftw_complex)*real._spectrum), + real._width,real._height,real._depth,real._spectrum); + + data_plan = fftw_plan_dft_1d(real._spectrum,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); + const unsigned long off = (unsigned long)real._width*real._height*real._depth; + cimg_forXYZ(real,x,y,z) { + T *ptrr = real.data(x,y,z,0), *ptri = imag.data(x,y,z,0); + double *ptrd = (double*)data_in; + cimg_forC(real,c) { *(ptrd++) = (double)*ptrr; *(ptrd++) = (double)*ptri; ptrr+=off; ptri+=off; } + fftw_execute(data_plan); + const unsigned int fact = real._spectrum; + if (is_invert) + cimg_forC(real,c) { ptrr-=off; ptri-=off; *ptri = (T)(*(--ptrd)/fact); *ptrr = (T)(*(--ptrd)/fact); } + else cimg_forC(real,c) { ptrr-=off; ptri-=off; *ptri = (T)*(--ptrd); *ptrr = (T)*(--ptrd); } + } + } + } + fftw_destroy_plan(data_plan); + fftw_free(data_in); + cimg::mutex(12,0); +#else + switch (cimg::uncase(axis)) { + case 'x' : { // Fourier along X, using built-in functions. + const unsigned int N = real._width, N2 = (N>>1); + if (((N-1)&N) && N!=1) + throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) " + "have non 2^N dimension along the X-axis.", + pixel_type(), + real._width,real._height,real._depth,real._spectrum); + + for (unsigned int i = 0, j = 0; ii) cimg_forYZC(real,y,z,c) { + cimg::swap(real(i,y,z,c),real(j,y,z,c)); cimg::swap(imag(i,y,z,c),imag(j,y,z,c)); + if (j=m; j-=m, m = n, n>>=1) {} + } + for (unsigned int delta = 2; delta<=N; delta<<=1) { + const unsigned int delta2 = (delta>>1); + for (unsigned int i = 0; i>1); + if (((N-1)&N) && N!=1) + throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) " + "have non 2^N dimension along the Y-axis.", + pixel_type(), + real._width,real._height,real._depth,real._spectrum); + + for (unsigned int i = 0, j = 0; ii) cimg_forXZC(real,x,z,c) { + cimg::swap(real(x,i,z,c),real(x,j,z,c)); cimg::swap(imag(x,i,z,c),imag(x,j,z,c)); + if (j=m; j-=m, m = n, n>>=1) {} + } + for (unsigned int delta = 2; delta<=N; delta<<=1) { + const unsigned int delta2 = (delta>>1); + for (unsigned int i = 0; i>1); + if (((N-1)&N) && N!=1) + throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) " + "have non 2^N dimension along the Z-axis.", + pixel_type(), + real._width,real._height,real._depth,real._spectrum); + + for (unsigned int i = 0, j = 0; ii) cimg_forXYC(real,x,y,c) { + cimg::swap(real(x,y,i,c),real(x,y,j,c)); cimg::swap(imag(x,y,i,c),imag(x,y,j,c)); + if (j=m; j-=m, m = n, n>>=1) {} + } + for (unsigned int delta = 2; delta<=N; delta<<=1) { + const unsigned int delta2 = (delta>>1); + for (unsigned int i = 0; i::FFT(): Invalid specified axis '%c' for real and imaginary parts " + "(%u,%u,%u,%u) " + "(should be { x | y | z }).", + pixel_type(),axis, + real._width,real._height,real._depth,real._spectrum); + } +#endif + } + + //! Compute n-d Fast Fourier Transform. + /** + \param[in,out] real Real part of the pixel values. + \param[in,out] imag Imaginary part of the pixel values. + \param is_invert Tells if the forward (\c false) or inverse (\c true) FFT is computed. + \param nb_threads Number of parallel threads used for the computation. + Use \c 0 to set this to the number of available cpus. + **/ + static void FFT(CImg& real, CImg& imag, const bool is_invert=false, const unsigned int nb_threads=0) { + if (!real) + throw CImgInstanceException("CImgList<%s>::FFT(): Empty specified real part.", + pixel_type()); + + if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,0); + if (!real.is_sameXYZC(imag)) + throw CImgInstanceException("CImgList<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and " + "imaginary part (%u,%u,%u,%u,%p) have different dimensions.", + pixel_type(), + real._width,real._height,real._depth,real._spectrum,real._data, + imag._width,imag._height,imag._depth,imag._spectrum,imag._data); + +#ifdef cimg_use_fftw3 + cimg::mutex(12); +#ifndef cimg_use_fftw3_singlethread + const unsigned int _nb_threads = nb_threads?nb_threads:cimg::nb_cpus(); + static int fftw_st = fftw_init_threads(); + cimg::unused(fftw_st); + fftw_plan_with_nthreads(_nb_threads); +#else + cimg::unused(nb_threads); +#endif + fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth); + if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " + "for computing FFT of image (%u,%u,%u,%u).", + pixel_type(), + cimg::strbuffersize(sizeof(fftw_complex)*real._width* + real._height*real._depth*real._spectrum), + real._width,real._height,real._depth,real._spectrum); + + fftw_plan data_plan; + const unsigned long w = (unsigned long)real._width, wh = w*real._height, whd = wh*real._depth; + data_plan = fftw_plan_dft_3d(real._width,real._height,real._depth,data_in,data_in, + is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); + cimg_forC(real,c) { + T *ptrr = real.data(0,0,0,c), *ptri = imag.data(0,0,0,c); + double *ptrd = (double*)data_in; + for (unsigned int x = 0; x1) FFT(real,imag,'z',is_invert); + if (real._height>1) FFT(real,imag,'y',is_invert); + if (real._width>1) FFT(real,imag,'x',is_invert); +#endif + } + + //@} + //------------------------------------- + // + //! \name 3d Objects Management + //@{ + //------------------------------------- + + //! Shift 3d object's vertices. + /** + \param tx X-coordinate of the 3d displacement vector. + \param ty Y-coordinate of the 3d displacement vector. + \param tz Z-coordinate of the 3d displacement vector. + **/ + CImg& shift_object3d(const float tx, const float ty=0, const float tz=0) { + if (_height!=3 || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "shift_object3d(): Instance is not a set of 3d vertices.", + cimg_instance); + + get_shared_row(0)+=tx; get_shared_row(1)+=ty; get_shared_row(2)+=tz; + return *this; + } + + //! Shift 3d object's vertices \newinstance. + CImg get_shift_object3d(const float tx, const float ty=0, const float tz=0) const { + return CImg(*this,false).shift_object3d(tx,ty,tz); + } + + //! Shift 3d object's vertices, so that it becomes centered. + /** + \note The object center is computed as its barycenter. + **/ + CImg& shift_object3d() { + if (_height!=3 || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "shift_object3d(): Instance is not a set of 3d vertices.", + cimg_instance); + + CImg xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2); + float + xm, xM = (float)xcoords.max_min(xm), + ym, yM = (float)ycoords.max_min(ym), + zm, zM = (float)zcoords.max_min(zm); + xcoords-=(xm + xM)/2; ycoords-=(ym + yM)/2; zcoords-=(zm + zM)/2; + return *this; + } + + //! Shift 3d object's vertices, so that it becomes centered \newinstance. + CImg get_shift_object3d() const { + return CImg(*this,false).shift_object3d(); + } + + //! Resize 3d object. + /** + \param sx Width of the 3d object's bounding box. + \param sy Height of the 3d object's bounding box. + \param sz Depth of the 3d object's bounding box. + **/ + CImg& resize_object3d(const float sx, const float sy=-100, const float sz=-100) { + if (_height!=3 || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "resize_object3d(): Instance is not a set of 3d vertices.", + cimg_instance); + + CImg xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2); + float + xm, xM = (float)xcoords.max_min(xm), + ym, yM = (float)ycoords.max_min(ym), + zm, zM = (float)zcoords.max_min(zm); + if (xm0) xcoords*=sx/(xM-xm); else xcoords*=-sx/100; } + if (ym0) ycoords*=sy/(yM-ym); else ycoords*=-sy/100; } + if (zm0) zcoords*=sz/(zM-zm); else zcoords*=-sz/100; } + return *this; + } + + //! Resize 3d object \newinstance. + CImg get_resize_object3d(const float sx, const float sy=-100, const float sz=-100) const { + return CImg(*this,false).resize_object3d(sx,sy,sz); + } + + //! Resize 3d object to unit size. + CImg resize_object3d() { + if (_height!=3 || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "resize_object3d(): Instance is not a set of 3d vertices.", + cimg_instance); + + CImg xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2); + float + xm, xM = (float)xcoords.max_min(xm), + ym, yM = (float)ycoords.max_min(ym), + zm, zM = (float)zcoords.max_min(zm); + const float dx = xM - xm, dy = yM - ym, dz = zM - zm, dmax = cimg::max(dx,dy,dz); + if (dmax>0) { xcoords/=dmax; ycoords/=dmax; zcoords/=dmax; } + return *this; + } + + //! Resize 3d object to unit size \newinstance. + CImg get_resize_object3d() const { + return CImg(*this,false).resize_object3d(); + } + + //! Merge two 3d objects together. + /** + \param[in,out] primitives Primitives data of the current 3d object. + \param obj_vertices Vertices data of the additional 3d object. + \param obj_primitives Primitives data of the additional 3d object. + **/ + template + CImg& append_object3d(CImgList& primitives, const CImg& obj_vertices, + const CImgList& obj_primitives) { + if (!obj_vertices || !obj_primitives) return *this; + if (obj_vertices._height!=3 || obj_vertices._depth>1 || obj_vertices._spectrum>1) + throw CImgInstanceException(_cimg_instance + "append_object3d(): Specified vertice image (%u,%u,%u,%u,%p) is not a " + "set of 3d vertices.", + cimg_instance, + obj_vertices._width,obj_vertices._height, + obj_vertices._depth,obj_vertices._spectrum,obj_vertices._data); + + if (is_empty()) { primitives.assign(obj_primitives); return assign(obj_vertices); } + if (_height!=3 || _depth>1 || _spectrum>1) + throw CImgInstanceException(_cimg_instance + "append_object3d(): Instance is not a set of 3d vertices.", + cimg_instance); + + const unsigned int P = _width; + append(obj_vertices,'x'); + const unsigned int N = primitives._width; + primitives.insert(obj_primitives); + for (unsigned int i = N; i &p = primitives[i]; + switch (p.size()) { + case 1 : p[0]+=P; break; // Point. + case 5 : p[0]+=P; p[1]+=P; break; // Sphere. + case 2 : case 6 : p[0]+=P; p[1]+=P; break; // Segment. + case 3 : case 9 : p[0]+=P; p[1]+=P; p[2]+=P; break; // Triangle. + case 4 : case 12 : p[0]+=P; p[1]+=P; p[2]+=P; p[3]+=P; break; // Rectangle. + } + } + return *this; + } + + //! Texturize primitives of a 3d object. + /** + \param[in,out] primitives Primitives data of the 3d object. + \param[in,out] colors Colors data of the 3d object. + \param texture Texture image to map to 3d object. + \param coords Texture-mapping coordinates. + **/ + template + const CImg& texturize_object3d(CImgList& primitives, CImgList& colors, + const CImg& texture, const CImg& coords=CImg::empty()) const { + if (is_empty()) return *this; + if (_height!=3) + throw CImgInstanceException(_cimg_instance + "texturize_object3d(): image instance is not a set of 3d points.", + cimg_instance); + if (coords && (coords._width!=_width || coords._height!=2)) + throw CImgArgumentException(_cimg_instance + "texturize_object3d(): Invalid specified texture coordinates (%u,%u,%u,%u,%p).", + cimg_instance, + coords._width,coords._height,coords._depth,coords._spectrum,coords._data); + CImg _coords; + if (!coords) { // If no texture coordinates specified, do a default XY-projection. + _coords.assign(_width,2); + float + xmin, xmax = (float)get_shared_row(0).max_min(xmin), + ymin, ymax = (float)get_shared_row(1).max_min(ymin), + dx = xmax>xmin?xmax-xmin:1, + dy = ymax>ymin?ymax-ymin:1; + cimg_forX(*this,p) { + _coords(p,0) = (unsigned int)(((*this)(p,0)-xmin)*(texture._width-1)/dx); + _coords(p,1) = (unsigned int)(((*this)(p,1)-ymin)*(texture._height-1)/dy); + } + } else _coords = coords; + + int texture_ind = -1; + cimglist_for(primitives,l) { + CImg &p = primitives[l]; + const unsigned int siz = p.size(); + switch (siz) { + case 1 : { // Point. + const unsigned int + i0 = (unsigned int)p[0], + x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1); + texture.get_vector_at(x0,y0).move_to(colors[l]); + } break; + case 2 : case 6 : { // Line. + const unsigned int + i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], + x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1), + x1 = (unsigned int)_coords(i1,0), y1 = (unsigned int)_coords(i1,1); + if (texture_ind<0) colors[texture_ind=l] = texture; else colors[l].assign(colors[texture_ind],true); + CImg::vector(i0,i1,x0,y0,x1,y1).move_to(p); + } break; + case 3 : case 9 : { // Triangle. + const unsigned int + i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2], + x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1), + x1 = (unsigned int)_coords(i1,0), y1 = (unsigned int)_coords(i1,1), + x2 = (unsigned int)_coords(i2,0), y2 = (unsigned int)_coords(i2,1); + if (texture_ind<0) colors[texture_ind=l] = texture; else colors[l].assign(colors[texture_ind],true); + CImg::vector(i0,i1,i2,x0,y0,x1,y1,x2,y2).move_to(p); + } break; + case 4 : case 12 : { // Quadrangle. + const unsigned int + i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2], i3 = (unsigned int)p[3], + x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1), + x1 = (unsigned int)_coords(i1,0), y1 = (unsigned int)_coords(i1,1), + x2 = (unsigned int)_coords(i2,0), y2 = (unsigned int)_coords(i2,1), + x3 = (unsigned int)_coords(i3,0), y3 = (unsigned int)_coords(i3,1); + if (texture_ind<0) colors[texture_ind=l] = texture; else colors[l].assign(colors[texture_ind],true); + CImg::vector(i0,i1,i2,i3,x0,y0,x1,y1,x2,y2,x3,y3).move_to(p); + } break; + } + } + return *this; + } + + //! Generate a 3d elevation of the image instance. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param[out] colors The returned list of the 3d object colors. + \param elevation The input elevation map. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + const CImg img("reference.jpg"); + CImgList faces3d; + CImgList colors3d; + const CImg points3d = img.get_elevation3d(faces3d,colors3d,img.get_norm()*0.2); + CImg().display_object3d("Elevation3d",points3d,faces3d,colors3d); + \endcode + \image html ref_elevation3d.jpg + **/ + template + CImg get_elevation3d(CImgList& primitives, CImgList& colors, const CImg& elevation) const { + if (!is_sameXY(elevation) || elevation._depth>1 || elevation._spectrum>1) + throw CImgArgumentException(_cimg_instance + "get_elevation3d(): Instance and specified elevation (%u,%u,%u,%u,%p) " + "have incompatible dimensions.", + cimg_instance, + elevation._width,elevation._height,elevation._depth, + elevation._spectrum,elevation._data); + if (is_empty()) return *this; + float m, M = (float)max_min(m); + if (M==m) ++M; + colors.assign(); + const unsigned int size_x1 = _width - 1, size_y1 = _height - 1; + for (unsigned int y = 0; y1?(unsigned char)(((*this)(x,y,1) - m)*255/(M-m)):r, + b = _spectrum>2?(unsigned char)(((*this)(x,y,2) - m)*255/(M-m)):(_spectrum>1?0:r); + CImg::vector((tc)r,(tc)g,(tc)b).move_to(colors); + } + const typename CImg::_functor2d_int func(elevation); + return elevation3d(primitives,func,0,0,_width-1.0f,_height-1.0f,_width,_height); + } + + //! Generate the 3d projection planes of the image instance. + /** + \param[out] primitives Primitives data of the returned 3d object. + \param[out] colors Colors data of the returned 3d object. + \param x0 X-coordinate of the projection point. + \param y0 Y-coordinate of the projection point. + \param z0 Z-coordinate of the projection point. + \param normalize_colors Tells if the created textures have normalized colors. + **/ + template + CImg get_projections3d(CImgList& primitives, CImgList& colors, + const unsigned int x0, const unsigned int y0, const unsigned int z0, + const bool normalize_colors=false) const { + float m = 0, M = 0, delta = 1; + if (normalize_colors) { m = (float)min_max(M); delta = 255/(m==M?1:M-m); } + const unsigned int + _x0 = (x0>=_width)?_width - 1:x0, + _y0 = (y0>=_height)?_height - 1:y0, + _z0 = (z0>=_depth)?_depth - 1:z0; + CImg img_xy, img_xz, img_yz; + if (normalize_colors) { + ((get_crop(0,0,_z0,0,_width-1,_height-1,_z0,_spectrum-1)-=m)*=delta).move_to(img_xy); + ((get_crop(0,_y0,0,0,_width-1,_y0,_depth-1,_spectrum-1)-=m)*=delta).resize(_width,_depth,1,-100,-1). + move_to(img_xz); + ((get_crop(_x0,0,0,0,_x0,_height-1,_depth-1,_spectrum-1)-=m)*=delta).resize(_height,_depth,1,-100,-1). + move_to(img_yz); + } else { + get_crop(0,0,_z0,0,_width-1,_height-1,_z0,_spectrum-1).move_to(img_xy); + get_crop(0,_y0,0,0,_width-1,_y0,_depth-1,_spectrum-1).resize(_width,_depth,1,-100,-1).move_to(img_xz); + get_crop(_x0,0,0,0,_x0,_height-1,_depth-1,_spectrum-1).resize(_height,_depth,1,-100,-1).move_to(img_yz); + } + CImg points(12,3,1,1, + 0,_width-1,_width-1,0, 0,_width-1,_width-1,0, _x0,_x0,_x0,_x0, + 0,0,_height-1,_height-1, _y0,_y0,_y0,_y0, 0,_height-1,_height-1,0, + _z0,_z0,_z0,_z0, 0,0,_depth-1,_depth-1, 0,0,_depth-1,_depth-1); + primitives.assign(); + CImg::vector(0,1,2,3,0,0,img_xy._width-1,0,img_xy._width-1,img_xy._height-1,0,img_xy._height-1). + move_to(primitives); + CImg::vector(4,5,6,7,0,0,img_xz._width-1,0,img_xz._width-1,img_xz._height-1,0,img_xz._height-1). + move_to(primitives); + CImg::vector(8,9,10,11,0,0,img_yz._width-1,0,img_yz._width-1,img_yz._height-1,0,img_yz._height-1). + move_to(primitives); + colors.assign(); + img_xy.move_to(colors); + img_xz.move_to(colors); + img_yz.move_to(colors); + return points; + } + + //! Generate a isoline of the image instance as a 3d object. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param isovalue The returned list of the 3d object colors. + \param size_x The number of subdivisions along the X-axis. + \param size_y The number of subdisivions along the Y-axis. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + const CImg img("reference.jpg"); + CImgList faces3d; + const CImg points3d = img.get_isoline3d(faces3d,100); + CImg().display_object3d("Isoline3d",points3d,faces3d,colors3d); + \endcode + \image html ref_isoline3d.jpg + **/ + template + CImg get_isoline3d(CImgList& primitives, const float isovalue, + const int size_x=-100, const int size_y=-100) const { + if (_spectrum>1) + throw CImgInstanceException(_cimg_instance + "get_isoline3d(): Instance is not a scalar image.", + cimg_instance); + if (_depth>1) + throw CImgInstanceException(_cimg_instance + "get_isoline3d(): Instance is not a 2d image.", + cimg_instance); + primitives.assign(); + if (is_empty()) return *this; + CImg vertices; + if ((size_x==-100 && size_y==-100) || (size_x==width() && size_y==height())) { + const _functor2d_int func(*this); + vertices = isoline3d(primitives,func,isovalue,0,0,width()-1.0f,height()-1.0f,width(),height()); + } else { + const _functor2d_float func(*this); + vertices = isoline3d(primitives,func,isovalue,0,0,width()-1.0f,height()-1.0f,size_x,size_y); + } + return vertices; + } + + //! Generate an isosurface of the image instance as a 3d object. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param isovalue The returned list of the 3d object colors. + \param size_x Number of subdivisions along the X-axis. + \param size_y Number of subdisivions along the Y-axis. + \param size_z Number of subdisivions along the Z-axis. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + const CImg img = CImg("reference.jpg").resize(-100,-100,20); + CImgList faces3d; + const CImg points3d = img.get_isosurface3d(faces3d,100); + CImg().display_object3d("Isosurface3d",points3d,faces3d,colors3d); + \endcode + \image html ref_isosurface3d.jpg + **/ + template + CImg get_isosurface3d(CImgList& primitives, const float isovalue, + const int size_x=-100, const int size_y=-100, const int size_z=-100) const { + if (_spectrum>1) + throw CImgInstanceException(_cimg_instance + "get_isosurface3d(): Instance is not a scalar image.", + cimg_instance); + primitives.assign(); + if (is_empty()) return *this; + CImg vertices; + if ((size_x==-100 && size_y==-100 && size_z==-100) || (size_x==width() && size_y==height() && size_z==depth())) { + const _functor3d_int func(*this); + vertices = isosurface3d(primitives,func,isovalue,0,0,0,width()-1.0f,height()-1.0f,depth()-1.0f, + width(),height(),depth()); + } else { + const _functor3d_float func(*this); + vertices = isosurface3d(primitives,func,isovalue,0,0,0,width()-1.0f,height()-1.0f,depth()-1.0f, + size_x,size_y,size_z); + } + return vertices; + } + + //! Compute 3d elevation of a function as a 3d object. + /** + \param[out] primitives Primitives data of the resulting 3d object. + \param func Elevation function. Is of type float (*func)(const float x,const float y). + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param size_x Resolution of the function along the X-axis. + \param size_y Resolution of the function along the Y-axis. + **/ + template + static CImg elevation3d(CImgList& primitives, const tfunc& func, + const float x0, const float y0, const float x1, const float y1, + const int size_x=256, const int size_y=256) { + const float + nx0 = x0=0?size_x:(nx1-nx0)*-size_x/100), + nsize_x = _nsize_x?_nsize_x:1, nsize_x1 = nsize_x - 1, + _nsize_y = (unsigned int)(size_y>=0?size_y:(ny1-ny0)*-size_y/100), + nsize_y = _nsize_y?_nsize_y:1, nsize_y1 = nsize_y - 1; + if (nsize_x<2 || nsize_y<2) + throw CImgArgumentException("CImg<%s>::elevation3d(): Invalid specified size (%d,%d).", + pixel_type(), + nsize_x,nsize_y); + + CImg vertices(nsize_x*nsize_y,3); + floatT *ptr_x = vertices.data(0,0), *ptr_y = vertices.data(0,1), *ptr_z = vertices.data(0,2); + for (unsigned int y = 0; y + static CImg elevation3d(CImgList& primitives, const char *const expression, + const float x0, const float y0, const float x1, const float y1, + const int size_x=256, const int size_y=256) { + const _functor2d_expr func(expression); + return elevation3d(primitives,func,x0,y0,x1,y1,size_x,size_y); + } + + //! Compute 0-isolines of a function, as a 3d object. + /** + \param[out] primitives Primitives data of the resulting 3d object. + \param func Elevation function. Is of type float (*func)(const float x,const float y). + \param isovalue Isovalue to extract from function. + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param size_x Resolution of the function along the X-axis. + \param size_y Resolution of the function along the Y-axis. + \note Use the marching squares algorithm for extracting the isolines. + **/ + template + static CImg isoline3d(CImgList& primitives, const tfunc& func, const float isovalue, + const float x0, const float y0, const float x1, const float y1, + const int size_x=256, const int size_y=256) { + static const unsigned int edges[16] = { 0x0, 0x9, 0x3, 0xa, 0x6, 0xf, 0x5, 0xc, 0xc, + 0x5, 0xf, 0x6, 0xa, 0x3, 0x9, 0x0 }; + static const int segments[16][4] = { { -1,-1,-1,-1 }, { 0,3,-1,-1 }, { 0,1,-1,-1 }, { 1,3,-1,-1 }, + { 1,2,-1,-1 }, { 0,1,2,3 }, { 0,2,-1,-1 }, { 2,3,-1,-1 }, + { 2,3,-1,-1 }, { 0,2,-1,-1}, { 0,3,1,2 }, { 1,2,-1,-1 }, + { 1,3,-1,-1 }, { 0,1,-1,-1}, { 0,3,-1,-1}, { -1,-1,-1,-1 } }; + const unsigned int + _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)), + _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)), + nx = _nx?_nx:1, + ny = _ny?_ny:1, + nxm1 = nx - 1, + nym1 = ny - 1; + primitives.assign(); + if (!nxm1 || !nym1) return CImg(); + const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1; + CImgList vertices; + CImg indices1(nx,1,1,2,-1), indices2(nx,1,1,2); + CImg values1(nx), values2(nx); + float X = x0, Y = y0, nX = X + dx, nY = Y + dy; + + // Fill first line with values + cimg_forX(values1,x) { values1(x) = (float)func(X,Y); X+=dx; } + + // Run the marching squares algorithm + for (unsigned int yi = 0, nyi = 1; yi::vector(Xi,Y,0).move_to(vertices); + } + if ((edge&2) && indices1(nxi,1)<0) { + const float Yi = Y + (isovalue-val1)*dy/(val2-val1); + indices1(nxi,1) = vertices._width; + CImg::vector(nX,Yi,0).move_to(vertices); + } + if ((edge&4) && indices2(xi,0)<0) { + const float Xi = X + (isovalue-val3)*dx/(val2-val3); + indices2(xi,0) = vertices._width; + CImg::vector(Xi,nY,0).move_to(vertices); + } + if ((edge&8) && indices1(xi,1)<0) { + const float Yi = Y + (isovalue-val0)*dy/(val3-val0); + indices1(xi,1) = vertices._width; + CImg::vector(X,Yi,0).move_to(vertices); + } + + // Create segments + for (const int *segment = segments[configuration]; *segment!=-1; ) { + const unsigned int p0 = *(segment++), p1 = *(segment++); + const tf + i0 = (tf)(_isoline3d_indice(p0,indices1,indices2,xi,nxi)), + i1 = (tf)(_isoline3d_indice(p1,indices1,indices2,xi,nxi)); + CImg::vector(i0,i1).move_to(primitives); + } + } + } + values1.swap(values2); + indices1.swap(indices2); + } + return vertices>'x'; + } + + //! Compute isolines of a function, as a 3d object \overloading. + template + static CImg isoline3d(CImgList& primitives, const char *const expression, const float isovalue, + const float x0, const float y0, const float x1, const float y1, + const int size_x=256, const int size_y=256) { + const _functor2d_expr func(expression); + return isoline3d(primitives,func,isovalue,x0,y0,x1,y1,size_x,size_y); + } + + template + static int _isoline3d_indice(const unsigned int edge, const CImg& indices1, const CImg& indices2, + const unsigned int x, const unsigned int nx) { + switch (edge) { + case 0 : return (int)indices1(x,0); + case 1 : return (int)indices1(nx,1); + case 2 : return (int)indices2(x,0); + case 3 : return (int)indices1(x,1); + } + return 0; + } + + //! Compute isosurface of a function, as a 3d object. + /** + \param[out] primitives Primitives data of the resulting 3d object. + \param func Implicit function. Is of type float (*func)(const float x, const float y, const float z). + \param isovalue Isovalue to extract. + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param z0 Z-coordinate of the starting point. + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param z1 Z-coordinate of the ending point. + \param size_x Resolution of the elevation function along the X-axis. + \param size_y Resolution of the elevation function along the Y-axis. + \param size_z Resolution of the elevation function along the Z-axis. + \note Use the marching cubes algorithm for extracting the isosurface. + **/ + template + static CImg isosurface3d(CImgList& primitives, const tfunc& func, const float isovalue, + const float x0, const float y0, const float z0, + const float x1, const float y1, const float z1, + const int size_x=32, const int size_y=32, const int size_z=32) { + static const unsigned int edges[256] = { + 0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, + 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, + 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, + 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, + 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60, + 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, + 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, + 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc , 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, + 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0, + 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650, + 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, + 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460, + 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0, + 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230, + 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190, + 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000 + }; + + static const int triangles[256][16] = { + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1 }, + { 8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1 }, + { 3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1 }, + { 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1 }, + { 4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1 }, + { 9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1 }, + { 10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1 }, + { 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1 }, + { 5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1 }, + { 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1 }, + { 2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1 }, + { 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1 }, + { 11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1 }, + { 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1 }, + { 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1 }, + { 11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1 }, + { 2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1 }, + { 6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1 }, + { 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1 }, + { 6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1 }, + { 6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1 }, + { 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1 }, + { 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1 }, + { 3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1 }, + { 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1 }, + { 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1 }, + { 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1 }, + { 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1 }, + { 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1 }, + { 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1 }, + { 10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1 }, + { 1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1 }, + { 0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1 }, + { 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1 }, + { 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1 }, + { 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1 }, + { 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1 }, + { 3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1 }, + { 6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1 }, + { 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1 }, + { 10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1 }, + { 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1 }, + { 7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1 }, + { 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1 }, + { 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1 }, + { 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1 }, + { 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1 }, + { 0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1 }, + { 7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1 }, + { 7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1 }, + { 10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1 }, + { 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1 }, + { 7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1 }, + { 6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1 }, + { 6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1 }, + { 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1 }, + { 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1 }, + { 8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1 }, + { 1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1 }, + { 10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1 }, + { 10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1 }, + { 9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1 }, + { 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1 }, + { 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1 }, + { 7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1 }, + { 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1 }, + { 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1 }, + { 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1 }, + { 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1 }, + { 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1 }, + { 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1 }, + { 6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1 }, + { 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1 }, + { 6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1 }, + { 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1 }, + { 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1 }, + { 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1 }, + { 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1 }, + { 9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1 }, + { 1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1 }, + { 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1 }, + { 0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1 }, + { 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1 }, + { 11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1 }, + { 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1 }, + { 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1 }, + { 2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1 }, + { 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1 }, + { 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1 }, + { 1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1 }, + { 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1 }, + { 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1 }, + { 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1 }, + { 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1 }, + { 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1 }, + { 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1 }, + { 9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1 }, + { 5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1 }, + { 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1 }, + { 8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1 }, + { 9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1 }, + { 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1 }, + { 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1 }, + { 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1 }, + { 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1 }, + { 11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1 }, + { 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1 }, + { 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1 }, + { 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1 }, + { 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1 }, + { 1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1 }, + { 4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1 }, + { 0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1 }, + { 9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1 }, + { 1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { 0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } + }; + + const unsigned int + _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)), + _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)), + _nz = (unsigned int)(size_z>=0?size_z:cimg::round((z1-z0)*-size_z/100 + 1)), + nx = _nx?_nx:1, + ny = _ny?_ny:1, + nz = _nz?_nz:1, + nxm1 = nx - 1, + nym1 = ny - 1, + nzm1 = nz - 1; + primitives.assign(); + if (!nxm1 || !nym1 || !nzm1) return CImg(); + const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1, dz = (z1 - z0)/nzm1; + CImgList vertices; + CImg indices1(nx,ny,1,3,-1), indices2(indices1); + CImg values1(nx,ny), values2(nx,ny); + float X = 0, Y = 0, Z = 0, nX = 0, nY = 0, nZ = 0; + + // Fill the first plane with function values + Y = y0; + cimg_forY(values1,y) { + X = x0; + cimg_forX(values1,x) { values1(x,y) = (float)func(X,Y,z0); X+=dx; } + Y+=dy; + } + + // Run Marching Cubes algorithm + Z = z0; nZ = Z + dz; + for (unsigned int zi = 0; zi::vector(Xi,Y,Z).move_to(vertices); + } + if ((edge&2) && indices1(nxi,yi,1)<0) { + const float Yi = Y + (isovalue-val1)*dy/(val2-val1); + indices1(nxi,yi,1) = vertices._width; + CImg::vector(nX,Yi,Z).move_to(vertices); + } + if ((edge&4) && indices1(xi,nyi,0)<0) { + const float Xi = X + (isovalue-val3)*dx/(val2-val3); + indices1(xi,nyi,0) = vertices._width; + CImg::vector(Xi,nY,Z).move_to(vertices); + } + if ((edge&8) && indices1(xi,yi,1)<0) { + const float Yi = Y + (isovalue-val0)*dy/(val3-val0); + indices1(xi,yi,1) = vertices._width; + CImg::vector(X,Yi,Z).move_to(vertices); + } + if ((edge&16) && indices2(xi,yi,0)<0) { + const float Xi = X + (isovalue-val4)*dx/(val5-val4); + indices2(xi,yi,0) = vertices._width; + CImg::vector(Xi,Y,nZ).move_to(vertices); + } + if ((edge&32) && indices2(nxi,yi,1)<0) { + const float Yi = Y + (isovalue-val5)*dy/(val6-val5); + indices2(nxi,yi,1) = vertices._width; + CImg::vector(nX,Yi,nZ).move_to(vertices); + } + if ((edge&64) && indices2(xi,nyi,0)<0) { + const float Xi = X + (isovalue-val7)*dx/(val6-val7); + indices2(xi,nyi,0) = vertices._width; + CImg::vector(Xi,nY,nZ).move_to(vertices); + } + if ((edge&128) && indices2(xi,yi,1)<0) { + const float Yi = Y + (isovalue-val4)*dy/(val7-val4); + indices2(xi,yi,1) = vertices._width; + CImg::vector(X,Yi,nZ).move_to(vertices); + } + if ((edge&256) && indices1(xi,yi,2)<0) { + const float Zi = Z+ (isovalue-val0)*dz/(val4-val0); + indices1(xi,yi,2) = vertices._width; + CImg::vector(X,Y,Zi).move_to(vertices); + } + if ((edge&512) && indices1(nxi,yi,2)<0) { + const float Zi = Z + (isovalue-val1)*dz/(val5-val1); + indices1(nxi,yi,2) = vertices._width; + CImg::vector(nX,Y,Zi).move_to(vertices); + } + if ((edge&1024) && indices1(nxi,nyi,2)<0) { + const float Zi = Z + (isovalue-val2)*dz/(val6-val2); + indices1(nxi,nyi,2) = vertices._width; + CImg::vector(nX,nY,Zi).move_to(vertices); + } + if ((edge&2048) && indices1(xi,nyi,2)<0) { + const float Zi = Z + (isovalue-val3)*dz/(val7-val3); + indices1(xi,nyi,2) = vertices._width; + CImg::vector(X,nY,Zi).move_to(vertices); + } + + // Create triangles + for (const int *triangle = triangles[configuration]; *triangle!=-1; ) { + const unsigned int p0 = *(triangle++), p1 = *(triangle++), p2 = *(triangle++); + const tf + i0 = (tf)(_isosurface3d_indice(p0,indices1,indices2,xi,yi,nxi,nyi)), + i1 = (tf)(_isosurface3d_indice(p1,indices1,indices2,xi,yi,nxi,nyi)), + i2 = (tf)(_isosurface3d_indice(p2,indices1,indices2,xi,yi,nxi,nyi)); + CImg::vector(i0,i2,i1).move_to(primitives); + } + } + } + } + cimg::swap(values1,values2); + cimg::swap(indices1,indices2); + } + return vertices>'x'; + } + + //! Compute isosurface of a function, as a 3d object \overloading. + template + static CImg isosurface3d(CImgList& primitives, const char *const expression, const float isovalue, + const float x0, const float y0, const float z0, + const float x1, const float y1, const float z1, + const int dx=32, const int dy=32, const int dz=32) { + const _functor3d_expr func(expression); + return isosurface3d(primitives,func,isovalue,x0,y0,z0,x1,y1,z1,dx,dy,dz); + } + + template + static int _isosurface3d_indice(const unsigned int edge, const CImg& indices1, const CImg& indices2, + const unsigned int x, const unsigned int y, + const unsigned int nx, const unsigned int ny) { + switch (edge) { + case 0 : return indices1(x,y,0); + case 1 : return indices1(nx,y,1); + case 2 : return indices1(x,ny,0); + case 3 : return indices1(x,y,1); + case 4 : return indices2(x,y,0); + case 5 : return indices2(nx,y,1); + case 6 : return indices2(x,ny,0); + case 7 : return indices2(x,y,1); + case 8 : return indices1(x,y,2); + case 9 : return indices1(nx,y,2); + case 10 : return indices1(nx,ny,2); + case 11 : return indices1(x,ny,2); + } + return 0; + } + + // Define functors for accessing image values (used in previous functions). + struct _functor2d_int { + const CImg& ref; + _functor2d_int(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y) const { + return (float)ref((int)x,(int)y); + } + }; + + struct _functor2d_float { + const CImg& ref; + _functor2d_float(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y) const { + return (float)ref._linear_atXY(x,y); + } + }; + + struct _functor2d_expr { + _cimg_math_parser *mp; + _functor2d_expr(const char *const expr):mp(0) { mp = new _cimg_math_parser(CImg::empty(),expr,0); } + ~_functor2d_expr() { delete mp; } + float operator()(const float x, const float y) const { + return (float)(*mp)(x,y,0,0); + } + }; + + struct _functor3d_int { + const CImg& ref; + _functor3d_int(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y, const float z) const { + return (float)ref((int)x,(int)y,(int)z); + } + }; + + struct _functor3d_float { + const CImg& ref; + _functor3d_float(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y, const float z) const { + return (float)ref._linear_atXYZ(x,y,z); + } + }; + + struct _functor3d_expr { + _cimg_math_parser *mp; + ~_functor3d_expr() { delete mp; } + _functor3d_expr(const char *const expr):mp(0) { mp = new _cimg_math_parser(CImg::empty(),expr,0); } + float operator()(const float x, const float y, const float z) const { + return (float)(*mp)(x,y,z,0); + } + }; + + struct _functor4d_int { + const CImg& ref; + _functor4d_int(const CImg& pref):ref(pref) {} + float operator()(const float x, const float y, const float z, const unsigned int c) const { + return (float)ref((int)x,(int)y,(int)z,c); + } + }; + + //! Generate a 3d box object. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param size_x The width of the box (dimension along the X-axis). + \param size_y The height of the box (dimension along the Y-axis). + \param size_z The depth of the box (dimension along the Z-axis). + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg points3d = CImg::box3d(faces3d,10,20,30); + CImg().display_object3d("Box3d",points3d,faces3d); + \endcode + \image html ref_box3d.jpg + **/ + template + static CImg box3d(CImgList& primitives, + const float size_x=200, const float size_y=100, const float size_z=100) { + primitives.assign(6,1,4,1,1, 0,3,2,1, 4,5,6,7, 0,1,5,4, 3,7,6,2, 0,4,7,3, 1,2,6,5); + return CImg(8,3,1,1, + 0.,size_x,size_x, 0., 0.,size_x,size_x, 0., + 0., 0.,size_y,size_y, 0., 0.,size_y,size_y, + 0., 0., 0., 0.,size_z,size_z,size_z,size_z); + } + + //! Generate a 3d cone. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param radius The radius of the cone basis. + \param size_z The cone's height. + \param subdivisions The number of basis angular subdivisions. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg points3d = CImg::cone3d(faces3d,50); + CImg().display_object3d("Cone3d",points3d,faces3d); + \endcode + \image html ref_cone3d.jpg + **/ + template + static CImg cone3d(CImgList& primitives, + const float radius=50, const float size_z=100, const unsigned int subdivisions=24) { + primitives.assign(); + if (!subdivisions) return CImg(); + CImgList vertices(2,1,3,1,1, + 0.,0.,size_z, + 0.,0.,0.); + for (float delta = 360.0f/subdivisions, angle = 0; angle<360; angle+=delta) { + const float a = (float)(angle*cimg::PI/180); + CImg::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0).move_to(vertices); + } + const unsigned int nbr = vertices._width - 2; + for (unsigned int p = 0; p::vector(1,next,curr).move_to(primitives); + CImg::vector(0,curr,next).move_to(primitives); + } + return vertices>'x'; + } + + //! Generate a 3d cylinder. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param radius The radius of the cylinder basis. + \param size_z The cylinder's height. + \param subdivisions The number of basis angular subdivisions. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg points3d = CImg::cylinder3d(faces3d,50); + CImg().display_object3d("Cylinder3d",points3d,faces3d); + \endcode + \image html ref_cylinder3d.jpg + **/ + template + static CImg cylinder3d(CImgList& primitives, + const float radius=50, const float size_z=100, const unsigned int subdivisions=24) { + primitives.assign(); + if (!subdivisions) return CImg(); + CImgList vertices(2,1,3,1,1, + 0.,0.,0., + 0.,0.,size_z); + for (float delta = 360.0f/subdivisions, angle = 0; angle<360; angle+=delta) { + const float a = (float)(angle*cimg::PI/180); + CImg::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0.0f).move_to(vertices); + CImg::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),size_z).move_to(vertices); + } + const unsigned int nbr = (vertices._width - 2)/2; + for (unsigned int p = 0; p::vector(0,next,curr).move_to(primitives); + CImg::vector(1,curr+1,next+1).move_to(primitives); + CImg::vector(curr,next,next+1,curr+1).move_to(primitives); + } + return vertices>'x'; + } + + //! Generate a 3d torus. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param radius1 The large radius. + \param radius2 The small radius. + \param subdivisions1 The number of angular subdivisions for the large radius. + \param subdivisions2 The number of angular subdivisions for the small radius. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg points3d = CImg::torus3d(faces3d,20,4); + CImg().display_object3d("Torus3d",points3d,faces3d); + \endcode + \image html ref_torus3d.jpg + **/ + template + static CImg torus3d(CImgList& primitives, + const float radius1=100, const float radius2=30, + const unsigned int subdivisions1=24, const unsigned int subdivisions2=12) { + primitives.assign(); + if (!subdivisions1 || !subdivisions2) return CImg(); + CImgList vertices; + for (unsigned int v = 0; v::vector(x,y,z).move_to(vertices); + } + } + for (unsigned int vv = 0; vv::vector(svv+nu,svv+uu,snv+uu,snv+nu).move_to(primitives); + } + } + return vertices>'x'; + } + + //! Generate a 3d XY-plane. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param size_x The width of the plane (dimension along the X-axis). + \param size_y The height of the plane (dimensions along the Y-axis). + \param subdivisions_x The number of planar subdivisions along the X-axis. + \param subdivisions_y The number of planar subdivisions along the Y-axis. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg points3d = CImg::plane3d(faces3d,100,50); + CImg().display_object3d("Plane3d",points3d,faces3d); + \endcode + \image html ref_plane3d.jpg + **/ + template + static CImg plane3d(CImgList& primitives, + const float size_x=100, const float size_y=100, + const unsigned int subdivisions_x=10, const unsigned int subdivisions_y=10) { + primitives.assign(); + if (!subdivisions_x || !subdivisions_y) return CImg(); + CImgList vertices; + const unsigned int w = subdivisions_x + 1, h = subdivisions_y + 1; + const float fx = (float)size_x/w, fy = (float)size_y/h; + for (unsigned int y = 0; y::vector(fx*x,fy*y,0).move_to(vertices); + for (unsigned int y = 0; y::vector(off1,off4,off3,off2).move_to(primitives); + } + return vertices>'x'; + } + + //! Generate a 3d sphere. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param radius The radius of the sphere (dimension along the X-axis). + \param subdivisions The number of recursive subdivisions from an initial icosahedron. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg points3d = CImg::sphere3d(faces3d,100,4); + CImg().display_object3d("Sphere3d",points3d,faces3d); + \endcode + \image html ref_sphere3d.jpg + **/ + template + static CImg sphere3d(CImgList& primitives, + const float radius=50, const unsigned int subdivisions=3) { + + // Create initial icosahedron + primitives.assign(); + const double tmp = (1+std::sqrt(5.0f))/2, a = 1.0/std::sqrt(1+tmp*tmp), b = tmp*a; + CImgList vertices(12,1,3,1,1, b,a,0.0, -b,a,0.0, -b,-a,0.0, b,-a,0.0, a,0.0,b, a,0.0,-b, + -a,0.0,-b, -a,0.0,b, 0.0,b,a, 0.0,-b,a, 0.0,-b,-a, 0.0,b,-a); + primitives.assign(20,1,3,1,1, 4,8,7, 4,7,9, 5,6,11, 5,10,6, 0,4,3, 0,3,5, 2,7,1, 2,1,6, + 8,0,11, 8,11,1, 9,10,3, 9,2,10, 8,4,0, 11,0,5, 4,9,3, + 5,3,10, 7,8,1, 6,1,11, 7,2,9, 6,10,2); + // edge - length/2 + float he = (float)a; + + // Recurse subdivisions + for (unsigned int i = 0; i::vector(nx0,ny0,nz0).move_to(vertices); i0 = vertices._width - 1; } + if (i1<0) { CImg::vector(nx1,ny1,nz1).move_to(vertices); i1 = vertices._width - 1; } + if (i2<0) { CImg::vector(nx2,ny2,nz2).move_to(vertices); i2 = vertices._width - 1; } + primitives.remove(0); + CImg::vector(p0,i0,i1).move_to(primitives); + CImg::vector((tf)i0,(tf)p1,(tf)i2).move_to(primitives); + CImg::vector((tf)i1,(tf)i2,(tf)p2).move_to(primitives); + CImg::vector((tf)i1,(tf)i0,(tf)i2).move_to(primitives); + } + } + return (vertices>'x')*=radius; + } + + //! Generate a 3d ellipsoid. + /** + \param[out] primitives The returned list of the 3d object primitives + (template type \e tf should be at least \e unsigned \e int). + \param tensor The tensor which gives the shape and size of the ellipsoid. + \param subdivisions The number of recursive subdivisions from an initial stretched icosahedron. + \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). + \par Example + \code + CImgList faces3d; + const CImg tensor = CImg::diagonal(10,7,3), + points3d = CImg::ellipsoid3d(faces3d,tensor,4); + CImg().display_object3d("Ellipsoid3d",points3d,faces3d); + \endcode + \image html ref_ellipsoid3d.jpg + **/ + template + static CImg ellipsoid3d(CImgList& primitives, + const CImg& tensor, const unsigned int subdivisions=3) { + primitives.assign(); + if (!subdivisions) return CImg(); + CImg S, V; + tensor.symmetric_eigen(S,V); + const float orient = + (V(0,1)*V(1,2) - V(0,2)*V(1,1))*V(2,0) + + (V(0,2)*V(1,0) - V(0,0)*V(1,2))*V(2,1) + + (V(0,0)*V(1,1) - V(0,1)*V(1,0))*V(2,2); + if (orient<0) { V(2,0) = -V(2,0); V(2,1) = -V(2,1); V(2,2) = -V(2,2); } + const float l0 = S[0], l1 = S[1], l2 = S[2]; + CImg vertices = sphere3d(primitives,1.0,subdivisions); + vertices.get_shared_row(0)*=l0; + vertices.get_shared_row(1)*=l1; + vertices.get_shared_row(2)*=l2; + return V*vertices; + } + + //! Convert 3d object into a CImg3d representation. + /** + \param primitives Primitives data of the 3d object. + \param colors Colors data of the 3d object. + \param opacities Opacities data of the 3d object. + \param full_check Tells if full checking of the 3d object must be performed. + **/ + template + CImg& object3dtoCImg3d(const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const bool full_check=true) { + return get_object3dtoCImg3d(primitives,colors,opacities,full_check).move_to(*this); + } + + //! Convert 3d object into a CImg3d representation \overloading. + template + CImg& object3dtoCImg3d(const CImgList& primitives, + const CImgList& colors, + const bool full_check=true) { + return get_object3dtoCImg3d(primitives,colors,full_check).move_to(*this); + } + + //! Convert 3d object into a CImg3d representation \overloading. + template + CImg& object3dtoCImg3d(const CImgList& primitives, + const bool full_check=true) { + return get_object3dtoCImg3d(primitives,full_check).move_to(*this); + } + + //! Convert 3d object into a CImg3d representation \overloading. + CImg& object3dtoCImg3d(const bool full_check=true) { + return get_object3dtoCImg3d(full_check).move_to(*this); + } + + //! Convert 3d object into a CImg3d representation \newinstance. + template + CImg get_object3dtoCImg3d(const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const bool full_check=true) const { + char error_message[1024] = { 0 }; + if (!is_object3d(primitives,colors,opacities,full_check,error_message)) + throw CImgInstanceException(_cimg_instance + "object3dtoCImg3d(): Invalid specified 3d object (%u,%u) (%s).", + cimg_instance,_width,primitives._width,error_message); + CImg res(1,_size_object3dtoCImg3d(primitives,colors,opacities)); + float *ptrd = res._data; + + // Put magick number. + *(ptrd++) = 'C' + 0.5f; *(ptrd++) = 'I' + 0.5f; *(ptrd++) = 'm' + 0.5f; + *(ptrd++) = 'g' + 0.5f; *(ptrd++) = '3' + 0.5f; *(ptrd++) = 'd' + 0.5f; + + // Put number of vertices and primitives. + *(ptrd++) = cimg::uint2float(_width); + *(ptrd++) = cimg::uint2float(primitives._width); + + // Put vertex data. + if (is_empty() || !primitives) return res; + const T *ptrx = data(0,0), *ptry = data(0,1), *ptrz = data(0,2); + cimg_forX(*this,p) { + *(ptrd++) = (float)*(ptrx++); + *(ptrd++) = (float)*(ptry++); + *(ptrd++) = (float)*(ptrz++); + } + + // Put primitive data. + cimglist_for(primitives,p) { + *(ptrd++) = (float)primitives[p].size(); + const tp *ptrp = primitives[p]._data; + cimg_foroff(primitives[p],i) *(ptrd++) = cimg::uint2float((unsigned int)*(ptrp++)); + } + + // Put color/texture data. + const unsigned int csiz = cimg::min(colors._width,primitives._width); + for (int c = 0; c<(int)csiz; ++c) { + const CImg& color = colors[c]; + const tc *ptrc = color._data; + if (color.size()==3) { *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*ptrc; } + else { + *(ptrd++) = -128.0f; + int shared_ind = -1; + if (color.is_shared()) for (int i = 0; i + float* _object3dtoCImg3d(const CImgList& opacities, float *ptrd) const { + cimglist_for(opacities,o) { + const CImg& opacity = opacities[o]; + const to *ptro = opacity._data; + if (opacity.size()==1) *(ptrd++) = (float)*ptro; + else { + *(ptrd++) = -128.0f; + int shared_ind = -1; + if (opacity.is_shared()) for (int i = 0; i + float* _object3dtoCImg3d(const CImg& opacities, float *ptrd) const { + const to *ptro = opacities._data; + cimg_foroff(opacities,o) *(ptrd++) = (float)*(ptro++); + return ptrd; + } + + template + unsigned int _size_object3dtoCImg3d(const CImgList& primitives, + const CImgList& colors, + const CImgList& opacities) const { + unsigned int siz = 8 + 3*width(); + cimglist_for(primitives,p) siz+=primitives[p].size() + 1; + for (int c = cimg::min(primitives._width,colors._width)-1; c>=0; --c) { + if (colors[c].is_shared()) siz+=4; + else { const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4+csiz:3; } + } + if (colors._width + unsigned int _size_object3dtoCImg3d(const CImgList& primitives, + const CImgList& colors, + const CImg& opacities) const { + unsigned int siz = 8 + 3*width(); + cimglist_for(primitives,p) siz+=primitives[p].size() + 1; + for (int c = cimg::min(primitives._width,colors._width)-1; c>=0; --c) { + const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4+csiz:3; + } + if (colors._width + CImg get_object3dtoCImg3d(const CImgList& primitives, + const CImgList& colors, + const bool full_check=true) const { + CImgList opacities; + return get_object3dtoCImg3d(primitives,colors,opacities,full_check); + } + + //! Convert 3d object into a CImg3d representation \overloading. + template + CImg get_object3dtoCImg3d(const CImgList& primitives, + const bool full_check=true) const { + CImgList colors, opacities; + return get_object3dtoCImg3d(primitives,colors,opacities,full_check); + } + + //! Convert 3d object into a CImg3d representation \overloading. + CImg get_object3dtoCImg3d(const bool full_check=true) const { + CImgList opacities, colors; + CImgList primitives(width(),1,1,1,1); + cimglist_for(primitives,p) primitives(p,0) = p; + return get_object3dtoCImg3d(primitives,colors,opacities,full_check); + } + + //! Convert CImg3d representation into a 3d object. + /** + \param[out] primitives Primitives data of the 3d object. + \param[out] colors Colors data of the 3d object. + \param[out] opacities Opacities data of the 3d object. + \param full_check Tells if full checking of the 3d object must be performed. + **/ + template + CImg& CImg3dtoobject3d(CImgList& primitives, + CImgList& colors, + CImgList& opacities, + const bool full_check=true) { + return get_CImg3dtoobject3d(primitives,colors,opacities,full_check).move_to(*this); + } + + //! Convert CImg3d representation into a 3d object \newinstance. + template + CImg get_CImg3dtoobject3d(CImgList& primitives, + CImgList& colors, + CImgList& opacities, + const bool full_check=true) const { + char error_message[1024] = { 0 }; + if (!is_CImg3d(full_check,error_message)) + throw CImgInstanceException(_cimg_instance + "CImg3dtoobject3d(): image instance is not a CImg3d (%s).", + cimg_instance,error_message); + const T *ptrs = _data + 6; + const unsigned int + nb_points = cimg::float2uint((float)*(ptrs++)), + nb_primitives = cimg::float2uint((float)*(ptrs++)); + const CImg points = CImg(ptrs,3,nb_points,1,1,true).get_transpose(); + ptrs+=3*nb_points; + primitives.assign(nb_primitives); + cimglist_for(primitives,p) { + const unsigned int nb_inds = (unsigned int)*(ptrs++); + primitives[p].assign(1,nb_inds); + tp *ptrp = primitives[p]._data; + for (unsigned int i = 0; i + CImg& _draw_scanline(const int x0, const int x1, const int y, + const tc *const color, const float opacity, + const float brightness, + const float nopacity, const float copacity, const unsigned long whd) { + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const int nx0 = x0>0?x0:0, nx1 = x1=0) { + const tc *col = color; + const unsigned long off = whd - dx - 1; + T *ptrd = data(nx0,y); + if (opacity>=1) { // ** Opaque drawing ** + if (brightness==1) { // Brightness==1 + if (sizeof(T)!=1) cimg_forC(*this,c) { + const T val = (T)*(col++); + for (int x = dx; x>=0; --x) *(ptrd++) = val; + ptrd+=off; + } else cimg_forC(*this,c) { + const T val = (T)*(col++); + std::memset(ptrd,(int)val,dx+1); + ptrd+=whd; + } + } else if (brightness<1) { // Brightness<1 + if (sizeof(T)!=1) cimg_forC(*this,c) { + const T val = (T)(*(col++)*brightness); + for (int x = dx; x>=0; --x) *(ptrd++) = val; + ptrd+=off; + } else cimg_forC(*this,c) { + const T val = (T)(*(col++)*brightness); + std::memset(ptrd,(int)val,dx+1); + ptrd+=whd; + } + } else { // Brightness>1 + if (sizeof(T)!=1) cimg_forC(*this,c) { + const T val = (T)((2-brightness)**(col++) + (brightness-1)*maxval); + for (int x = dx; x>=0; --x) *(ptrd++) = val; + ptrd+=off; + } else cimg_forC(*this,c) { + const T val = (T)((2-brightness)**(col++) + (brightness-1)*maxval); + std::memset(ptrd,(int)val,dx+1); + ptrd+=whd; + } + } + } else { // ** Transparent drawing ** + if (brightness==1) { // Brightness==1 + cimg_forC(*this,c) { + const T val = (T)*(col++); + for (int x = dx; x>=0; --x) { *ptrd = (T)(val*nopacity + *ptrd*copacity); ++ptrd; } + ptrd+=off; + } + } else if (brightness<=1) { // Brightness<1 + cimg_forC(*this,c) { + const T val = (T)(*(col++)*brightness); + for (int x = dx; x>=0; --x) { *ptrd = (T)(val*nopacity + *ptrd*copacity); ++ptrd; } + ptrd+=off; + } + } else { // Brightness>1 + cimg_forC(*this,c) { + const T val = (T)((2-brightness)**(col++) + (brightness-1)*maxval); + for (int x = dx; x>=0; --x) { *ptrd = (T)(val*nopacity + *ptrd*copacity); ++ptrd; } + ptrd+=off; + } + } + } + } + return *this; + } + + //! Draw a 3d point. + /** + \param x0 X-coordinate of the point. + \param y0 Y-coordinate of the point. + \param z0 Z-coordinate of the point. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \note + - To set pixel values without clipping needs, you should use the faster CImg::operator()() function. + \par Example: + \code + CImg img(100,100,1,3,0); + const unsigned char color[] = { 255,128,64 }; + img.draw_point(50,50,color); + \endcode + **/ + template + CImg& draw_point(const int x0, const int y0, const int z0, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_point(): Specified color is (null).", + cimg_instance); + if (x0>=0 && y0>=0 && z0>=0 && x0=1) cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } + else cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; } + } + return *this; + } + + //! Draw a 2d point \simplification. + template + CImg& draw_point(const int x0, const int y0, + const tc *const color, const float opacity=1) { + return draw_point(x0,y0,0,color,opacity); + } + + // Draw a points cloud. + /** + \param points Image of vertices coordinates. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + **/ + template + CImg& draw_point(const CImg& points, + const tc *const color, const float opacity=1) { + if (is_empty() || !points) return *this; + switch (points._height) { + case 0 : case 1 : + throw CImgArgumentException(_cimg_instance + "draw_point(): Invalid specified point set (%u,%u,%u,%u,%p).", + cimg_instance, + points._width,points._height,points._depth,points._spectrum,points._data); + case 2 : { + cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),color,opacity); + } break; + default : { + cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),(int)points(i,2),color,opacity); + } + } + return *this; + } + + //! Draw a 2d line. + /** + \param x0 X-coordinate of the starting line point. + \param y0 Y-coordinate of the starting line point. + \param x1 X-coordinate of the ending line point. + \param y1 Y-coordinate of the ending line point. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch Tells if a reinitialization of the hash state must be done. + \note + - Line routine uses Bresenham's algorithm. + - Set \p init_hatch = false to draw consecutive hatched segments without breaking the line pattern. + \par Example: + \code + CImg img(100,100,1,3,0); + const unsigned char color[] = { 255,128,64 }; + img.draw_line(40,40,80,70,color); + \endcode + **/ + template + CImg& draw_line(const int x0, const int y0, + const int x1, const int y1, + const tc *const color, const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_line(): Specified color is (null).", + cimg_instance); + static unsigned int hatch = ~0U - (~0U>>1); + if (init_hatch) hatch = ~0U - (~0U>>1); + const bool xdir = x0=width()) return *this; + if (xleft<0) { yleft-=(int)((float)xleft*((float)yright - yleft)/((float)xright - xleft)); xleft = 0; } + if (xright>=width()) { + yright-=(int)(((float)xright - width())*((float)yright - yleft)/((float)xright - xleft)); + xright = width() - 1; + } + if (ydown<0 || yup>=height()) return *this; + if (yup<0) { xup-=(int)((float)yup*((float)xdown - xup)/((float)ydown - yup)); yup = 0; } + if (ydown>=height()) { + xdown-=(int)(((float)ydown - height())*((float)xdown - xup)/((float)ydown - yup)); + ydown = height() - 1; + } + T *ptrd0 = data(nx0,ny0); + int dx = xright - xleft, dy = ydown - yup; + const bool steep = dy>dx; + if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); + const long + offx = (nx0=1) { + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + T *ptrd = ptrd0; const tc* col = color; + cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + T *ptrd = ptrd0; const tc* col = color; cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } + } else { + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + T *ptrd = ptrd0; const tc* col = color; + cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + T *ptrd = ptrd0; const tc* col = color; + cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } + } + return *this; + } + + //! Draw a 2d line, with z-buffering. + /** + \param zbuffer Zbuffer image. + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param z0 Z-coordinate of the starting point + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param z1 Z-coordinate of the ending point. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch Tells if a reinitialization of the hash state must be done. + **/ + template + CImg& draw_line(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const tc *const color, const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_line(): Specified color is (null).", + cimg_instance); + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + static unsigned int hatch = ~0U - (~0U>>1); + if (init_hatch) hatch = ~0U - (~0U>>1); + const bool xdir = x0=width()) return *this; + if (xleft<0) { + const float D = (float)xright - xleft; + yleft-=(int)((float)xleft*((float)yright - yleft)/D); + zleft-=(tzfloat)xleft*(zright - zleft)/D; + xleft = 0; + } + if (xright>=width()) { + const float d = (float)xright - width(), D = (float)xright - xleft; + yright-=(int)(d*((float)yright - yleft)/D); + zright-=(tzfloat)d*(zright - zleft)/D; + xright = width() - 1; + } + if (ydown<0 || yup>=height()) return *this; + if (yup<0) { + const float D = (float)ydown - yup; + xup-=(int)((float)yup*((float)xdown - xup)/D); + zup-=(tzfloat)yup*(zdown - zup)/D; + yup = 0; + } + if (ydown>=height()) { + const float d = (float)ydown - height(), D = (float)ydown - yup; + xdown-=(int)(d*((float)xdown - xup)/D); + zdown-=(tzfloat)d*(zdown - zup)/D; + ydown = height() - 1; + } + T *ptrd0 = data(nx0,ny0); + tz *ptrz = zbuffer.data(nx0,ny0); + int dx = xright - xleft, dy = ydown - yup; + const bool steep = dy>dx; + if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); + const long + offx = (nx00?dx:1; + if (opacity>=1) { + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz && pattern&hatch) { + *ptrz = (tz)z; + T *ptrd = ptrd0; const tc *col = color; + cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz) { + *ptrz = (tz)z; + T *ptrd = ptrd0; const tc *col = color; + cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } + } + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } + } else { + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz && pattern&hatch) { + *ptrz = (tz)z; + T *ptrd = ptrd0; const tc *col = color; + cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz) { + *ptrz = (tz)z; + T *ptrd = ptrd0; const tc *col = color; + cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } + } + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } + } + return *this; + } + + //! Draw a 3d line. + /** + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param z0 Z-coordinate of the starting point + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param z1 Z-coordinate of the ending point. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch Tells if a reinitialization of the hash state must be done. + **/ + template + CImg& draw_line(const int x0, const int y0, const int z0, + const int x1, const int y1, const int z1, + const tc *const color, const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_line(): Specified color is (null).", + cimg_instance); + static unsigned int hatch = ~0U - (~0U>>1); + if (init_hatch) hatch = ~0U - (~0U>>1); + int nx0 = x0, ny0 = y0, nz0 = z0, nx1 = x1, ny1 = y1, nz1 = z1; + if (nx0>nx1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); + if (nx1<0 || nx0>=width()) return *this; + if (nx0<0) { + const float D = 1.0f + nx1 - nx0; + ny0-=(int)((float)nx0*(1.0f + ny1 - ny0)/D); + nz0-=(int)((float)nx0*(1.0f + nz1 - nz0)/D); + nx0 = 0; + } + if (nx1>=width()) { + const float d = (float)nx1 - width(), D = 1.0f + nx1 - nx0; + ny1+=(int)(d*(1.0f + ny0 - ny1)/D); + nz1+=(int)(d*(1.0f + nz0 - nz1)/D); + nx1 = width() - 1; + } + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); + if (ny1<0 || ny0>=height()) return *this; + if (ny0<0) { + const float D = 1.0f + ny1 - ny0; + nx0-=(int)((float)ny0*(1.0f + nx1 - nx0)/D); + nz0-=(int)((float)ny0*(1.0f + nz1 - nz0)/D); + ny0 = 0; + } + if (ny1>=height()) { + const float d = (float)ny1 - height(), D = 1.0f + ny1 - ny0; + nx1+=(int)(d*(1.0f + nx0 - nx1)/D); + nz1+=(int)(d*(1.0f + nz0 - nz1)/D); + ny1 = height() - 1; + } + if (nz0>nz1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); + if (nz1<0 || nz0>=depth()) return *this; + if (nz0<0) { + const float D = 1.0f + nz1 - nz0; + nx0-=(int)((float)nz0*(1.0f + nx1 - nx0)/D); + ny0-=(int)((float)nz0*(1.0f + ny1 - ny0)/D); + nz0 = 0; + } + if (nz1>=depth()) { + const float d = (float)nz1 - depth(), D = 1.0f + nz1 - nz0; + nx1+=(int)(d*(1.0f + nx0 - nx1)/D); + ny1+=(int)(d*(1.0f + ny0 - ny1)/D); + nz1 = depth() - 1; + } + const unsigned int dmax = cimg::max(cimg::abs(nx1 - nx0),cimg::abs(ny1 - ny0),nz1 - nz0); + const unsigned long whd = (unsigned long)_width*_height*_depth; + const float px = (nx1 - nx0)/(float)dmax, py = (ny1 - ny0)/(float)dmax, pz = (nz1 - nz0)/(float)dmax; + float x = (float)nx0, y = (float)ny0, z = (float)nz0; + if (opacity>=1) for (unsigned int t = 0; t<=dmax; ++t) { + if (!(~pattern) || (~pattern && pattern&hatch)) { + T* ptrd = data((unsigned int)x,(unsigned int)y,(unsigned int)z); + const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } + } + x+=px; y+=py; z+=pz; if (pattern) { hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); } + } else { + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + for (unsigned int t = 0; t<=dmax; ++t) { + if (!(~pattern) || (~pattern && pattern&hatch)) { + T* ptrd = data((unsigned int)x,(unsigned int)y,(unsigned int)z); + const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; } + } + x+=px; y+=py; z+=pz; if (pattern) { hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); } + } + } + return *this; + } + + //! Draw a textured 2d line. + /** + \param x0 X-coordinate of the starting line point. + \param y0 Y-coordinate of the starting line point. + \param x1 X-coordinate of the ending line point. + \param y1 Y-coordinate of the ending line point. + \param texture Texture image defining the pixel colors. + \param tx0 X-coordinate of the starting texture point. + \param ty0 Y-coordinate of the starting texture point. + \param tx1 X-coordinate of the ending texture point. + \param ty1 Y-coordinate of the ending texture point. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch Tells if the hash variable must be reinitialized. + \note + - Line routine uses the well known Bresenham's algorithm. + \par Example: + \code + CImg img(100,100,1,3,0), texture("texture256x256.ppm"); + const unsigned char color[] = { 255,128,64 }; + img.draw_line(40,40,80,70,texture,0,0,255,255); + \endcode + **/ + template + CImg& draw_line(const int x0, const int y0, + const int x1, const int y1, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty()) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) return draw_line(x0,y0,x1,y1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch); + static unsigned int hatch = ~0U - (~0U>>1); + if (init_hatch) hatch = ~0U - (~0U>>1); + const bool xdir = x0=width()) return *this; + if (xleft<0) { + const float D = (float)xright - xleft; + yleft-=(int)((float)xleft*((float)yright - yleft)/D); + txleft-=(int)((float)xleft*((float)txright - txleft)/D); + tyleft-=(int)((float)xleft*((float)tyright - tyleft)/D); + xleft = 0; + } + if (xright>=width()) { + const float d = (float)xright - width(), D = (float)xright - xleft; + yright-=(int)(d*((float)yright - yleft)/D); + txright-=(int)(d*((float)txright - txleft)/D); + tyright-=(int)(d*((float)tyright - tyleft)/D); + xright = width() - 1; + } + if (ydown<0 || yup>=height()) return *this; + if (yup<0) { + const float D = (float)ydown - yup; + xup-=(int)((float)yup*((float)xdown - xup)/D); + txup-=(int)((float)yup*((float)txdown - txup)/D); + tyup-=(int)((float)yup*((float)tydown - tyup)/D); + yup = 0; + } + if (ydown>=height()) { + const float d = (float)ydown - height(), D = (float)ydown - yup; + xdown-=(int)(d*((float)xdown - xup)/D); + txdown-=(int)(d*((float)txdown - txup)/D); + tydown-=(int)(d*((float)tydown - tyup)/D); + ydown = height() - 1; + } + T *ptrd0 = data(nx0,ny0); + int dx = xright - xleft, dy = ydown - yup; + const bool steep = dy>dx; + if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); + const long + offx = (nx00?dx:1; + const unsigned long wh = (unsigned long)_width*_height; + + if (opacity>=1) { + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + T *ptrd = ptrd0; + const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; + cimg_forC(*this,c) { *ptrd = (T)texture(tx,ty,0,c); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + T *ptrd = ptrd0; + const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; + cimg_forC(*this,c) { *ptrd = (T)texture(tx,ty,0,c); ptrd+=wh; } + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } + } else { + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + T *ptrd = ptrd0; + if (pattern&hatch) { + const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; + cimg_forC(*this,c) { *ptrd = (T)(nopacity*texture(tx,ty,0,c) + *ptrd*copacity); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + T *ptrd = ptrd0; + const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; + cimg_forC(*this,c) { *ptrd = (T)(nopacity*texture(tx,ty,0,c) + *ptrd*copacity); ptrd+=wh; } + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } + } + return *this; + } + + //! Draw a textured 2d line, with perspective correction. + /** + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param z0 Z-coordinate of the starting point + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param z1 Z-coordinate of the ending point. + \param texture Texture image defining the pixel colors. + \param tx0 X-coordinate of the starting texture point. + \param ty0 Y-coordinate of the starting texture point. + \param tx1 X-coordinate of the ending texture point. + \param ty1 Y-coordinate of the ending texture point. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch Tells if the hash variable must be reinitialized. + **/ + template + CImg& draw_line(const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty() && z0<=0 && z1<=0) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_line(x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch); + static unsigned int hatch = ~0U - (~0U>>1); + if (init_hatch) hatch = ~0U - (~0U>>1); + const bool xdir = x0=width()) return *this; + if (xleft<0) { + const float D = (float)xright - xleft; + yleft-=(int)((float)xleft*((float)yright - yleft)/D); + zleft-=(float)xleft*(zright - zleft)/D; + txleft-=(float)xleft*(txright - txleft)/D; + tyleft-=(float)xleft*(tyright - tyleft)/D; + xleft = 0; + } + if (xright>=width()) { + const float d = (float)xright - width(), D = (float)xright - xleft; + yright-=(int)(d*((float)yright - yleft)/D); + zright-=d*(zright - zleft)/D; + txright-=d*(txright - txleft)/D; + tyright-=d*(tyright - tyleft)/D; + xright = width() - 1; + } + if (ydown<0 || yup>=height()) return *this; + if (yup<0) { + const float D = (float)ydown - yup; + xup-=(int)((float)yup*((float)xdown - xup)/D); + zup-=(float)yup*(zdown - zup)/D; + txup-=(float)yup*(txdown - txup)/D; + tyup-=(float)yup*(tydown - tyup)/D; + yup = 0; + } + if (ydown>=height()) { + const float d = (float)ydown - height(), D = (float)ydown - yup; + xdown-=(int)(d*((float)xdown - xup)/D); + zdown-=d*(zdown - zup)/D; + txdown-=d*(txdown - txup)/D; + tydown-=d*(tydown - tyup)/D; + ydown = height() - 1; + } + T *ptrd0 = data(nx0,ny0); + int dx = xright - xleft, dy = ydown - yup; + const bool steep = dy>dx; + if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); + const long + offx = (nx00?dx:1; + const unsigned long wh = (unsigned long)_width*_height; + + if (opacity>=1) { + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } + } else { + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; + cimg_forC(*this,c) { + *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; + } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; + cimg_forC(*this,c) { *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; } + ptrd0+=offx; + if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } + } + } + return *this; + } + + //! Draw a textured 2d line, with perspective correction and z-buffering. + /** + \param zbuffer Z-buffer image. + \param x0 X-coordinate of the starting point. + \param y0 Y-coordinate of the starting point. + \param z0 Z-coordinate of the starting point + \param x1 X-coordinate of the ending point. + \param y1 Y-coordinate of the ending point. + \param z1 Z-coordinate of the ending point. + \param texture Texture image defining the pixel colors. + \param tx0 X-coordinate of the starting texture point. + \param ty0 Y-coordinate of the starting texture point. + \param tx1 X-coordinate of the ending texture point. + \param ty1 Y-coordinate of the ending texture point. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch Tells if the hash variable must be reinitialized. + **/ + template + CImg& draw_line(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0) return *this; + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_line(zbuffer,x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch); + static unsigned int hatch = ~0U - (~0U>>1); + if (init_hatch) hatch = ~0U - (~0U>>1); + const bool xdir = x0=width()) return *this; + if (xleft<0) { + const float D = (float)xright - xleft; + yleft-=(int)((float)xleft*((float)yright - yleft)/D); + zleft-=(float)xleft*(zright - zleft)/D; + txleft-=(float)xleft*(txright - txleft)/D; + tyleft-=(float)xleft*(tyright - tyleft)/D; + xleft = 0; + } + if (xright>=width()) { + const float d = (float)xright - width(), D = (float)xright - xleft; + yright-=(int)(d*((float)yright - yleft)/D); + zright-=d*(zright - zleft)/D; + txright-=d*(txright - txleft)/D; + tyright-=d*(tyright - tyleft)/D; + xright = width()-1; + } + if (ydown<0 || yup>=height()) return *this; + if (yup<0) { + const float D = (float)ydown - yup; + xup-=(int)((float)yup*((float)xdown - xup)/D); + zup-=yup*(zdown - zup)/D; + txup-=yup*(txdown - txup)/D; + tyup-=yup*(tydown - tyup)/D; + yup = 0; + } + if (ydown>=height()) { + const float d = (float)ydown - height(), D = (float)ydown - yup; + xdown-=(int)(d*((float)xdown - xup)/D); + zdown-=d*(zdown - zup)/D; + txdown-=d*(txdown - txup)/D; + tydown-=d*(tydown - tyup)/D; + ydown = height()-1; + } + T *ptrd0 = data(nx0,ny0); + tz *ptrz = zbuffer.data(nx0,ny0); + int dx = xright - xleft, dy = ydown - yup; + const bool steep = dy>dx; + if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); + const long + offx = (nx00?dx:1; + const unsigned long wh = (unsigned long)_width*_height; + + if (opacity>=1) { + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz) { + *ptrz = (tz)z; + const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } + } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz) { + *ptrz = (tz)z; + const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } + } + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } + } else { + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { + if (pattern&hatch) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz) { + *ptrz = (tz)z; + const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; + cimg_forC(*this,c) { + *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; + } + } + } + hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } else for (int error = dx>>1, x = 0; x<=dx; ++x) { + const tzfloat z = Z0 + x*dz/ndx; + if (z>=(tzfloat)*ptrz) { + *ptrz = (tz)z; + const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; + T *ptrd = ptrd0; + cimg_forC(*this,c) { + *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; + } + } + ptrd0+=offx; ptrz+=offx; + if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } + } + } + return *this; + } + + //! Draw a set of consecutive lines. + /** + \param points Coordinates of vertices, stored as a list of vectors. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch If set to true, init hatch motif. + \note + - This function uses several call to the single CImg::draw_line() procedure, + depending on the vectors size in \p points. + **/ + template + CImg& draw_line(const CImg& points, + const tc *const color, const float opacity=1, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty() || !points || points._width<2) return *this; + bool ninit_hatch = init_hatch; + switch (points._height) { + case 0 : case 1 : + throw CImgArgumentException(_cimg_instance + "draw_line(): Invalid specified point set (%u,%u,%u,%u,%p).", + cimg_instance, + points._width,points._height,points._depth,points._spectrum,points._data); + + case 2 : { + const int x0 = (int)points(0,0), y0 = (int)points(0,1); + int ox = x0, oy = y0; + for (unsigned int i = 1; i + CImg& draw_arrow(const int x0, const int y0, + const int x1, const int y1, + const tc *const color, const float opacity=1, + const float angle=30, const float length=-10, + const unsigned int pattern=~0U) { + if (is_empty()) return *this; + const float u = (float)(x0 - x1), v = (float)(y0 - y1), sq = u*u + v*v, + deg = (float)(angle*cimg::PI/180), ang = (sq>0)?(float)std::atan2(v,u):0.0f, + l = (length>=0)?length:-length*(float)std::sqrt(sq)/100; + if (sq>0) { + const float + cl = (float)std::cos(ang - deg), sl = (float)std::sin(ang - deg), + cr = (float)std::cos(ang + deg), sr = (float)std::sin(ang + deg); + const int + xl = x1 + (int)(l*cl), yl = y1 + (int)(l*sl), + xr = x1 + (int)(l*cr), yr = y1 + (int)(l*sr), + xc = x1 + (int)((l+1)*(cl+cr))/2, yc = y1 + (int)((l+1)*(sl+sr))/2; + draw_line(x0,y0,xc,yc,color,opacity,pattern).draw_triangle(x1,y1,xl,yl,xr,yr,color,opacity); + } else draw_point(x0,y0,color,opacity); + return *this; + } + + //! Draw a 2d spline. + /** + \param x0 X-coordinate of the starting curve point + \param y0 Y-coordinate of the starting curve point + \param u0 X-coordinate of the starting velocity + \param v0 Y-coordinate of the starting velocity + \param x1 X-coordinate of the ending curve point + \param y1 Y-coordinate of the ending curve point + \param u1 X-coordinate of the ending velocity + \param v1 Y-coordinate of the ending velocity + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param precision Curve drawing precision. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch If \c true, init hatch motif. + \note + - The curve is a 2d cubic Bezier spline, from the set of specified starting/ending points + and corresponding velocity vectors. + - The spline is drawn as a serie of connected segments. The \p precision parameter sets the + average number of pixels in each drawn segment. + - A cubic Bezier curve is sometimes defined by a set of 4 points { (\p x0,\p y0), (\p xa,\p ya), + (\p xb,\p yb), (\p x1,\p y1) } where (\p x0,\p y0) is the starting point, (\p x1,\p y1) is the ending point + and (\p xa,\p ya), (\p xb,\p yb) are two + \e control points. + The starting and ending velocities (\p u0,\p v0) and (\p u1,\p v1) can be deduced easily from + the control points as + \p u0 = (\p xa - \p x0), \p v0 = (\p ya - \p y0), \p u1 = (\p x1 - \p xb) and \p v1 = (\p y1 - \p yb). + \par Example: + \code + CImg img(100,100,1,3,0); + const unsigned char color[] = { 255,255,255 }; + img.draw_spline(30,30,0,100,90,40,0,-100,color); + \endcode + **/ + template + CImg& draw_spline(const int x0, const int y0, const float u0, const float v0, + const int x1, const int y1, const float u1, const float v1, + const tc *const color, const float opacity=1, + const float precision=0.25, const unsigned int pattern=~0U, + const bool init_hatch=true) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_spline(): Specified color is (null).", + cimg_instance); + if (x0==x1 && y0==y1) return draw_point(x0,y0,color,opacity); + bool ninit_hatch = init_hatch; + const float + ax = u0 + u1 + 2*(x0 - x1), + bx = 3*(x1 - x0) - 2*u0 - u1, + ay = v0 + v1 + 2*(y0 - y1), + by = 3*(y1 - y0) - 2*v0 - v1, + _precision = 1/(std::sqrt(cimg::sqr((float)x0-x1)+cimg::sqr((float)y0-y1))*(precision>0?precision:1)); + int ox = x0, oy = y0; + for (float t = 0; t<1; t+=_precision) { + const float t2 = t*t, t3 = t2*t; + const int + nx = (int)(ax*t3 + bx*t2 + u0*t + x0), + ny = (int)(ay*t3 + by*t2 + v0*t + y0); + draw_line(ox,oy,nx,ny,color,opacity,pattern,ninit_hatch); + ninit_hatch = false; + ox = nx; oy = ny; + } + return draw_line(ox,oy,x1,y1,color,opacity,pattern,false); + } + + //! Draw a 3d spline \overloading. + /** + \note + - Similar to CImg::draw_spline() for a 3d spline in a volumetric image. + **/ + template + CImg& draw_spline(const int x0, const int y0, const int z0, const float u0, const float v0, const float w0, + const int x1, const int y1, const int z1, const float u1, const float v1, const float w1, + const tc *const color, const float opacity=1, + const float precision=4, const unsigned int pattern=~0U, + const bool init_hatch=true) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_spline(): Specified color is (null).", + cimg_instance); + if (x0==x1 && y0==y1 && z0==z1) return draw_point(x0,y0,z0,color,opacity); + bool ninit_hatch = init_hatch; + const float + ax = u0 + u1 + 2*(x0 - x1), + bx = 3*(x1 - x0) - 2*u0 - u1, + ay = v0 + v1 + 2*(y0 - y1), + by = 3*(y1 - y0) - 2*v0 - v1, + az = w0 + w1 + 2*(z0 - z1), + bz = 3*(z1 - z0) - 2*w0 - w1, + _precision = 1/(std::sqrt(cimg::sqr(x0-x1)+cimg::sqr(y0-y1))*(precision>0?precision:1)); + int ox = x0, oy = y0, oz = z0; + for (float t = 0; t<1; t+=_precision) { + const float t2 = t*t, t3 = t2*t; + const int + nx = (int)(ax*t3 + bx*t2 + u0*t + x0), + ny = (int)(ay*t3 + by*t2 + v0*t + y0), + nz = (int)(az*t3 + bz*t2 + w0*t + z0); + draw_line(ox,oy,oz,nx,ny,nz,color,opacity,pattern,ninit_hatch); + ninit_hatch = false; + ox = nx; oy = ny; oz = nz; + } + return draw_line(ox,oy,oz,x1,y1,z1,color,opacity,pattern,false); + } + + //! Draw a textured 2d spline. + /** + \param x0 X-coordinate of the starting curve point + \param y0 Y-coordinate of the starting curve point + \param u0 X-coordinate of the starting velocity + \param v0 Y-coordinate of the starting velocity + \param x1 X-coordinate of the ending curve point + \param y1 Y-coordinate of the ending curve point + \param u1 X-coordinate of the ending velocity + \param v1 Y-coordinate of the ending velocity + \param texture Texture image defining line pixel colors. + \param tx0 X-coordinate of the starting texture point. + \param ty0 Y-coordinate of the starting texture point. + \param tx1 X-coordinate of the ending texture point. + \param ty1 Y-coordinate of the ending texture point. + \param precision Curve drawing precision. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch if \c true, reinit hatch motif. + **/ + template + CImg& draw_spline(const int x0, const int y0, const float u0, const float v0, + const int x1, const int y1, const float u1, const float v1, + const CImg& texture, + const int tx0, const int ty0, const int tx1, const int ty1, + const float opacity=1, + const float precision=4, const unsigned int pattern=~0U, + const bool init_hatch=true) { + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_spline(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_empty()) return *this; + if (is_overlapped(texture)) + return draw_spline(x0,y0,u0,v0,x1,y1,u1,v1,+texture,tx0,ty0,tx1,ty1,precision,opacity,pattern,init_hatch); + if (x0==x1 && y0==y1) return draw_point(x0,y0,texture.get_vector_at(x0,y0),opacity); + bool ninit_hatch = init_hatch; + const float + ax = u0 + u1 + 2*(x0 - x1), + bx = 3*(x1 - x0) - 2*u0 - u1, + ay = v0 + v1 + 2*(y0 - y1), + by = 3*(y1 - y0) - 2*v0 - v1, + _precision = 1/(std::sqrt(cimg::sqr(x0-x1)+cimg::sqr(y0-y1))*(precision>0?precision:1)); + int ox = x0, oy = y0, otx = tx0, oty = ty0; + for (float t1 = 0; t1<1; t1+=_precision) { + const float t2 = t1*t1, t3 = t2*t1; + const int + nx = (int)(ax*t3 + bx*t2 + u0*t1 + x0), + ny = (int)(ay*t3 + by*t2 + v0*t1 + y0), + ntx = tx0 + (int)((tx1-tx0)*t1), + nty = ty0 + (int)((ty1-ty0)*t1); + draw_line(ox,oy,nx,ny,texture,otx,oty,ntx,nty,opacity,pattern,ninit_hatch); + ninit_hatch = false; + ox = nx; oy = ny; otx = ntx; oty = nty; + } + return draw_line(ox,oy,x1,y1,texture,otx,oty,tx1,ty1,opacity,pattern,false); + } + + //! Draw a set of consecutive splines. + /** + \param points Vertices data. + \param tangents Tangents data. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param is_closed_set Tells if the drawn spline set is closed. + \param precision Precision of the drawing. + \param pattern An integer whose bits describe the line pattern. + \param init_hatch If \c true, init hatch motif. + **/ + template + CImg& draw_spline(const CImg& points, const CImg& tangents, + const tc *const color, const float opacity=1, + const bool is_closed_set=false, const float precision=4, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty() || !points || !tangents || points._width<2 || tangents._width<2) return *this; + bool ninit_hatch = init_hatch; + switch (points._height) { + case 0 : case 1 : + throw CImgArgumentException(_cimg_instance + "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).", + cimg_instance, + points._width,points._height,points._depth,points._spectrum,points._data); + + case 2 : { + const int x0 = (int)points(0,0), y0 = (int)points(0,1); + const float u0 = (float)tangents(0,0), v0 = (float)tangents(0,1); + int ox = x0, oy = y0; + float ou = u0, ov = v0; + for (unsigned int i = 1; i + CImg& draw_spline(const CImg& points, + const tc *const color, const float opacity=1, + const bool is_closed_set=false, const float precision=4, + const unsigned int pattern=~0U, const bool init_hatch=true) { + if (is_empty() || !points || points._width<2) return *this; + CImg tangents; + switch (points._height) { + case 0 : case 1 : + throw CImgArgumentException(_cimg_instance + "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).", + cimg_instance, + points._width,points._height,points._depth,points._spectrum,points._data); + case 2 : { + tangents.assign(points._width,points._height); + cimg_forX(points,p) { + const unsigned int + p0 = is_closed_set?(p+points._width-1)%points._width:(p?p-1:0), + p1 = is_closed_set?(p+1)%points._width:(p+1=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ + xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ + _sxn=1, \ + _sxr=1, \ + _sxl=1, \ + _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ + _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ + _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ + _dyn = y2-y1, \ + _dyr = y2-y0, \ + _dyl = y1-y0, \ + _counter = (_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ + _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ + _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ + cimg::min((int)(img)._height-y-1,y2-y)), \ + _errn = _dyn/2, \ + _errr = _dyr/2, \ + _errl = _dyl/2, \ + _rxn = _dyn?(x2-x1)/_dyn:0, \ + _rxr = _dyr?(x2-x0)/_dyr:0, \ + _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ + (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn); \ + _counter>=0; --_counter, ++y, \ + xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ + xl+=(y!=y1)?_rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0): \ + (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) + +#define _cimg_for_triangle2(img,xl,cl,xr,cr,y,x0,y0,c0,x1,y1,c1,x2,y2,c2) \ + for (int y = y0<0?0:y0, \ + xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ + cr = y0>=0?c0:(c0-y0*(c2-c0)/(y2-y0)), \ + xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ + cl = y1>=0?(y0>=0?(y0==y1?c1:c0):(c0-y0*(c1-c0)/(y1-y0))):(c1-y1*(c2-c1)/(y2-y1)), \ + _sxn=1, _scn=1, \ + _sxr=1, _scr=1, \ + _sxl=1, _scl=1, \ + _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ + _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ + _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ + _dcn = c2>c1?c2-c1:(_scn=-1,c1-c2), \ + _dcr = c2>c0?c2-c0:(_scr=-1,c0-c2), \ + _dcl = c1>c0?c1-c0:(_scl=-1,c0-c1), \ + _dyn = y2-y1, \ + _dyr = y2-y0, \ + _dyl = y1-y0, \ + _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ + _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ + _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ + _dcn-=_dyn?_dyn*(_dcn/_dyn):0, \ + _dcr-=_dyr?_dyr*(_dcr/_dyr):0, \ + _dcl-=_dyl?_dyl*(_dcl/_dyl):0, \ + cimg::min((int)(img)._height-y-1,y2-y)), \ + _errn = _dyn/2, _errcn = _errn, \ + _errr = _dyr/2, _errcr = _errr, \ + _errl = _dyl/2, _errcl = _errl, \ + _rxn = _dyn?(x2-x1)/_dyn:0, \ + _rcn = _dyn?(c2-c1)/_dyn:0, \ + _rxr = _dyr?(x2-x0)/_dyr:0, \ + _rcr = _dyr?(c2-c0)/_dyr:0, \ + _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ + (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ + _rcl = (y0!=y1 && y1>0)?(_dyl?(c1-c0)/_dyl:0): \ + (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcn ); \ + _counter>=0; --_counter, ++y, \ + xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ + cr+=_rcr+((_errcr-=_dcr)<0?_errcr+=_dyr,_scr:0), \ + xl+=(y!=y1)?(cl+=_rcl+((_errcl-=_dcl)<0?(_errcl+=_dyl,_scl):0), \ + _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ + (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcl=_rcn, cl=c1, \ + _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) + +#define _cimg_for_triangle3(img,xl,txl,tyl,xr,txr,tyr,y,x0,y0,tx0,ty0,x1,y1,tx1,ty1,x2,y2,tx2,ty2) \ + for (int y = y0<0?0:y0, \ + xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ + txr = y0>=0?tx0:(tx0-y0*(tx2-tx0)/(y2-y0)), \ + tyr = y0>=0?ty0:(ty0-y0*(ty2-ty0)/(y2-y0)), \ + xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ + txl = y1>=0?(y0>=0?(y0==y1?tx1:tx0):(tx0-y0*(tx1-tx0)/(y1-y0))):(tx1-y1*(tx2-tx1)/(y2-y1)), \ + tyl = y1>=0?(y0>=0?(y0==y1?ty1:ty0):(ty0-y0*(ty1-ty0)/(y1-y0))):(ty1-y1*(ty2-ty1)/(y2-y1)), \ + _sxn=1, _stxn=1, _styn=1, \ + _sxr=1, _stxr=1, _styr=1, \ + _sxl=1, _stxl=1, _styl=1, \ + _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ + _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ + _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ + _dtxn = tx2>tx1?tx2-tx1:(_stxn=-1,tx1-tx2), \ + _dtxr = tx2>tx0?tx2-tx0:(_stxr=-1,tx0-tx2), \ + _dtxl = tx1>tx0?tx1-tx0:(_stxl=-1,tx0-tx1), \ + _dtyn = ty2>ty1?ty2-ty1:(_styn=-1,ty1-ty2), \ + _dtyr = ty2>ty0?ty2-ty0:(_styr=-1,ty0-ty2), \ + _dtyl = ty1>ty0?ty1-ty0:(_styl=-1,ty0-ty1), \ + _dyn = y2-y1, \ + _dyr = y2-y0, \ + _dyl = y1-y0, \ + _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ + _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ + _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ + _dtxn-=_dyn?_dyn*(_dtxn/_dyn):0, \ + _dtxr-=_dyr?_dyr*(_dtxr/_dyr):0, \ + _dtxl-=_dyl?_dyl*(_dtxl/_dyl):0, \ + _dtyn-=_dyn?_dyn*(_dtyn/_dyn):0, \ + _dtyr-=_dyr?_dyr*(_dtyr/_dyr):0, \ + _dtyl-=_dyl?_dyl*(_dtyl/_dyl):0, \ + cimg::min((int)(img)._height-y-1,y2-y)), \ + _errn = _dyn/2, _errtxn = _errn, _errtyn = _errn, \ + _errr = _dyr/2, _errtxr = _errr, _errtyr = _errr, \ + _errl = _dyl/2, _errtxl = _errl, _errtyl = _errl, \ + _rxn = _dyn?(x2-x1)/_dyn:0, \ + _rtxn = _dyn?(tx2-tx1)/_dyn:0, \ + _rtyn = _dyn?(ty2-ty1)/_dyn:0, \ + _rxr = _dyr?(x2-x0)/_dyr:0, \ + _rtxr = _dyr?(tx2-tx0)/_dyr:0, \ + _rtyr = _dyr?(ty2-ty0)/_dyr:0, \ + _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ + (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ + _rtxl = (y0!=y1 && y1>0)?(_dyl?(tx1-tx0)/_dyl:0): \ + (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxn ), \ + _rtyl = (y0!=y1 && y1>0)?(_dyl?(ty1-ty0)/_dyl:0): \ + (_errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyn ); \ + _counter>=0; --_counter, ++y, \ + xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ + txr+=_rtxr+((_errtxr-=_dtxr)<0?_errtxr+=_dyr,_stxr:0), \ + tyr+=_rtyr+((_errtyr-=_dtyr)<0?_errtyr+=_dyr,_styr:0), \ + xl+=(y!=y1)?(txl+=_rtxl+((_errtxl-=_dtxl)<0?(_errtxl+=_dyl,_stxl):0), \ + tyl+=_rtyl+((_errtyl-=_dtyl)<0?(_errtyl+=_dyl,_styl):0), \ + _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ + (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxl=_rtxn, txl=tx1, \ + _errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyl=_rtyn, tyl=ty1,\ + _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) + +#define _cimg_for_triangle4(img,xl,cl,txl,tyl,xr,cr,txr,tyr,y,x0,y0,c0,tx0,ty0,x1,y1,c1,tx1,ty1,x2,y2,c2,tx2,ty2) \ + for (int y = y0<0?0:y0, \ + xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ + cr = y0>=0?c0:(c0-y0*(c2-c0)/(y2-y0)), \ + txr = y0>=0?tx0:(tx0-y0*(tx2-tx0)/(y2-y0)), \ + tyr = y0>=0?ty0:(ty0-y0*(ty2-ty0)/(y2-y0)), \ + xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ + cl = y1>=0?(y0>=0?(y0==y1?c1:c0):(c0-y0*(c1-c0)/(y1-y0))):(c1-y1*(c2-c1)/(y2-y1)), \ + txl = y1>=0?(y0>=0?(y0==y1?tx1:tx0):(tx0-y0*(tx1-tx0)/(y1-y0))):(tx1-y1*(tx2-tx1)/(y2-y1)), \ + tyl = y1>=0?(y0>=0?(y0==y1?ty1:ty0):(ty0-y0*(ty1-ty0)/(y1-y0))):(ty1-y1*(ty2-ty1)/(y2-y1)), \ + _sxn=1, _scn=1, _stxn=1, _styn=1, \ + _sxr=1, _scr=1, _stxr=1, _styr=1, \ + _sxl=1, _scl=1, _stxl=1, _styl=1, \ + _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ + _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ + _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ + _dcn = c2>c1?c2-c1:(_scn=-1,c1-c2), \ + _dcr = c2>c0?c2-c0:(_scr=-1,c0-c2), \ + _dcl = c1>c0?c1-c0:(_scl=-1,c0-c1), \ + _dtxn = tx2>tx1?tx2-tx1:(_stxn=-1,tx1-tx2), \ + _dtxr = tx2>tx0?tx2-tx0:(_stxr=-1,tx0-tx2), \ + _dtxl = tx1>tx0?tx1-tx0:(_stxl=-1,tx0-tx1), \ + _dtyn = ty2>ty1?ty2-ty1:(_styn=-1,ty1-ty2), \ + _dtyr = ty2>ty0?ty2-ty0:(_styr=-1,ty0-ty2), \ + _dtyl = ty1>ty0?ty1-ty0:(_styl=-1,ty0-ty1), \ + _dyn = y2-y1, \ + _dyr = y2-y0, \ + _dyl = y1-y0, \ + _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ + _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ + _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ + _dcn-=_dyn?_dyn*(_dcn/_dyn):0, \ + _dcr-=_dyr?_dyr*(_dcr/_dyr):0, \ + _dcl-=_dyl?_dyl*(_dcl/_dyl):0, \ + _dtxn-=_dyn?_dyn*(_dtxn/_dyn):0, \ + _dtxr-=_dyr?_dyr*(_dtxr/_dyr):0, \ + _dtxl-=_dyl?_dyl*(_dtxl/_dyl):0, \ + _dtyn-=_dyn?_dyn*(_dtyn/_dyn):0, \ + _dtyr-=_dyr?_dyr*(_dtyr/_dyr):0, \ + _dtyl-=_dyl?_dyl*(_dtyl/_dyl):0, \ + cimg::min((int)(img)._height-y-1,y2-y)), \ + _errn = _dyn/2, _errcn = _errn, _errtxn = _errn, _errtyn = _errn, \ + _errr = _dyr/2, _errcr = _errr, _errtxr = _errr, _errtyr = _errr, \ + _errl = _dyl/2, _errcl = _errl, _errtxl = _errl, _errtyl = _errl, \ + _rxn = _dyn?(x2-x1)/_dyn:0, \ + _rcn = _dyn?(c2-c1)/_dyn:0, \ + _rtxn = _dyn?(tx2-tx1)/_dyn:0, \ + _rtyn = _dyn?(ty2-ty1)/_dyn:0, \ + _rxr = _dyr?(x2-x0)/_dyr:0, \ + _rcr = _dyr?(c2-c0)/_dyr:0, \ + _rtxr = _dyr?(tx2-tx0)/_dyr:0, \ + _rtyr = _dyr?(ty2-ty0)/_dyr:0, \ + _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ + (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ + _rcl = (y0!=y1 && y1>0)?(_dyl?(c1-c0)/_dyl:0): \ + (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcn ), \ + _rtxl = (y0!=y1 && y1>0)?(_dyl?(tx1-tx0)/_dyl:0): \ + (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxn ), \ + _rtyl = (y0!=y1 && y1>0)?(_dyl?(ty1-ty0)/_dyl:0): \ + (_errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyn ); \ + _counter>=0; --_counter, ++y, \ + xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ + cr+=_rcr+((_errcr-=_dcr)<0?_errcr+=_dyr,_scr:0), \ + txr+=_rtxr+((_errtxr-=_dtxr)<0?_errtxr+=_dyr,_stxr:0), \ + tyr+=_rtyr+((_errtyr-=_dtyr)<0?_errtyr+=_dyr,_styr:0), \ + xl+=(y!=y1)?(cl+=_rcl+((_errcl-=_dcl)<0?(_errcl+=_dyl,_scl):0), \ + txl+=_rtxl+((_errtxl-=_dtxl)<0?(_errtxl+=_dyl,_stxl):0), \ + tyl+=_rtyl+((_errtyl-=_dtyl)<0?(_errtyl+=_dyl,_styl):0), \ + _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ + (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcl=_rcn, cl=c1, \ + _errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxl=_rtxn, txl=tx1, \ + _errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyl=_rtyn, tyl=ty1, \ + _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) + +#define _cimg_for_triangle5(img,xl,txl,tyl,lxl,lyl,xr,txr,tyr,lxr,lyr,y,x0,y0,\ + tx0,ty0,lx0,ly0,x1,y1,tx1,ty1,lx1,ly1,x2,y2,tx2,ty2,lx2,ly2) \ + for (int y = y0<0?0:y0, \ + xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ + txr = y0>=0?tx0:(tx0-y0*(tx2-tx0)/(y2-y0)), \ + tyr = y0>=0?ty0:(ty0-y0*(ty2-ty0)/(y2-y0)), \ + lxr = y0>=0?lx0:(lx0-y0*(lx2-lx0)/(y2-y0)), \ + lyr = y0>=0?ly0:(ly0-y0*(ly2-ly0)/(y2-y0)), \ + xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ + txl = y1>=0?(y0>=0?(y0==y1?tx1:tx0):(tx0-y0*(tx1-tx0)/(y1-y0))):(tx1-y1*(tx2-tx1)/(y2-y1)), \ + tyl = y1>=0?(y0>=0?(y0==y1?ty1:ty0):(ty0-y0*(ty1-ty0)/(y1-y0))):(ty1-y1*(ty2-ty1)/(y2-y1)), \ + lxl = y1>=0?(y0>=0?(y0==y1?lx1:lx0):(lx0-y0*(lx1-lx0)/(y1-y0))):(lx1-y1*(lx2-lx1)/(y2-y1)), \ + lyl = y1>=0?(y0>=0?(y0==y1?ly1:ly0):(ly0-y0*(ly1-ly0)/(y1-y0))):(ly1-y1*(ly2-ly1)/(y2-y1)), \ + _sxn=1, _stxn=1, _styn=1, _slxn=1, _slyn=1, \ + _sxr=1, _stxr=1, _styr=1, _slxr=1, _slyr=1, \ + _sxl=1, _stxl=1, _styl=1, _slxl=1, _slyl=1, \ + _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), _dyn = y2-y1, \ + _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), _dyr = y2-y0, \ + _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), _dyl = y1-y0, \ + _dtxn = tx2>tx1?tx2-tx1:(_stxn=-1,tx1-tx2), \ + _dtxr = tx2>tx0?tx2-tx0:(_stxr=-1,tx0-tx2), \ + _dtxl = tx1>tx0?tx1-tx0:(_stxl=-1,tx0-tx1), \ + _dtyn = ty2>ty1?ty2-ty1:(_styn=-1,ty1-ty2), \ + _dtyr = ty2>ty0?ty2-ty0:(_styr=-1,ty0-ty2), \ + _dtyl = ty1>ty0?ty1-ty0:(_styl=-1,ty0-ty1), \ + _dlxn = lx2>lx1?lx2-lx1:(_slxn=-1,lx1-lx2), \ + _dlxr = lx2>lx0?lx2-lx0:(_slxr=-1,lx0-lx2), \ + _dlxl = lx1>lx0?lx1-lx0:(_slxl=-1,lx0-lx1), \ + _dlyn = ly2>ly1?ly2-ly1:(_slyn=-1,ly1-ly2), \ + _dlyr = ly2>ly0?ly2-ly0:(_slyr=-1,ly0-ly2), \ + _dlyl = ly1>ly0?ly1-ly0:(_slyl=-1,ly0-ly1), \ + _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ + _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ + _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ + _dtxn-=_dyn?_dyn*(_dtxn/_dyn):0, \ + _dtxr-=_dyr?_dyr*(_dtxr/_dyr):0, \ + _dtxl-=_dyl?_dyl*(_dtxl/_dyl):0, \ + _dtyn-=_dyn?_dyn*(_dtyn/_dyn):0, \ + _dtyr-=_dyr?_dyr*(_dtyr/_dyr):0, \ + _dtyl-=_dyl?_dyl*(_dtyl/_dyl):0, \ + _dlxn-=_dyn?_dyn*(_dlxn/_dyn):0, \ + _dlxr-=_dyr?_dyr*(_dlxr/_dyr):0, \ + _dlxl-=_dyl?_dyl*(_dlxl/_dyl):0, \ + _dlyn-=_dyn?_dyn*(_dlyn/_dyn):0, \ + _dlyr-=_dyr?_dyr*(_dlyr/_dyr):0, \ + _dlyl-=_dyl?_dyl*(_dlyl/_dyl):0, \ + cimg::min((int)(img)._height-y-1,y2-y)), \ + _errn = _dyn/2, _errtxn = _errn, _errtyn = _errn, _errlxn = _errn, _errlyn = _errn, \ + _errr = _dyr/2, _errtxr = _errr, _errtyr = _errr, _errlxr = _errr, _errlyr = _errr, \ + _errl = _dyl/2, _errtxl = _errl, _errtyl = _errl, _errlxl = _errl, _errlyl = _errl, \ + _rxn = _dyn?(x2-x1)/_dyn:0, \ + _rtxn = _dyn?(tx2-tx1)/_dyn:0, \ + _rtyn = _dyn?(ty2-ty1)/_dyn:0, \ + _rlxn = _dyn?(lx2-lx1)/_dyn:0, \ + _rlyn = _dyn?(ly2-ly1)/_dyn:0, \ + _rxr = _dyr?(x2-x0)/_dyr:0, \ + _rtxr = _dyr?(tx2-tx0)/_dyr:0, \ + _rtyr = _dyr?(ty2-ty0)/_dyr:0, \ + _rlxr = _dyr?(lx2-lx0)/_dyr:0, \ + _rlyr = _dyr?(ly2-ly0)/_dyr:0, \ + _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ + (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ + _rtxl = (y0!=y1 && y1>0)?(_dyl?(tx1-tx0)/_dyl:0): \ + (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxn ), \ + _rtyl = (y0!=y1 && y1>0)?(_dyl?(ty1-ty0)/_dyl:0): \ + (_errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyn ), \ + _rlxl = (y0!=y1 && y1>0)?(_dyl?(lx1-lx0)/_dyl:0): \ + (_errlxl=_errlxn, _dlxl=_dlxn, _dyl=_dyn, _slxl=_slxn, _rlxn ), \ + _rlyl = (y0!=y1 && y1>0)?(_dyl?(ly1-ly0)/_dyl:0): \ + (_errlyl=_errlyn, _dlyl=_dlyn, _dyl=_dyn, _slyl=_slyn, _rlyn ); \ + _counter>=0; --_counter, ++y, \ + xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ + txr+=_rtxr+((_errtxr-=_dtxr)<0?_errtxr+=_dyr,_stxr:0), \ + tyr+=_rtyr+((_errtyr-=_dtyr)<0?_errtyr+=_dyr,_styr:0), \ + lxr+=_rlxr+((_errlxr-=_dlxr)<0?_errlxr+=_dyr,_slxr:0), \ + lyr+=_rlyr+((_errlyr-=_dlyr)<0?_errlyr+=_dyr,_slyr:0), \ + xl+=(y!=y1)?(txl+=_rtxl+((_errtxl-=_dtxl)<0?(_errtxl+=_dyl,_stxl):0), \ + tyl+=_rtyl+((_errtyl-=_dtyl)<0?(_errtyl+=_dyl,_styl):0), \ + lxl+=_rlxl+((_errlxl-=_dlxl)<0?(_errlxl+=_dyl,_slxl):0), \ + lyl+=_rlyl+((_errlyl-=_dlyl)<0?(_errlyl+=_dyl,_slyl):0), \ + _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ + (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxl=_rtxn, txl=tx1, \ + _errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyl=_rtyn, tyl=ty1, \ + _errlxl=_errlxn, _dlxl=_dlxn, _dyl=_dyn, _slxl=_slxn, _rlxl=_rlxn, lxl=lx1, \ + _errlyl=_errlyn, _dlyl=_dlyn, _dyl=_dyn, _slyl=_slyn, _rlyl=_rlyn, lyl=ly1, \ + _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) + + // [internal] Draw a filled triangle. + template + CImg& _draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const tc *const color, const float opacity, + const float brightness) { + cimg_init_scanline(color,opacity); + const float nbrightness = brightness<0?0:(brightness>2?2:brightness); + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2); + if (ny0=0) { + if ((nx1 - nx0)*(ny2 - ny0) - (nx2 - nx0)*(ny1 - ny0)<0) + _cimg_for_triangle1(*this,xl,xr,y,nx0,ny0,nx1,ny1,nx2,ny2) + cimg_draw_scanline(xl,xr,y,color,opacity,nbrightness); + else + _cimg_for_triangle1(*this,xl,xr,y,nx0,ny0,nx1,ny1,nx2,ny2) + cimg_draw_scanline(xr,xl,y,color,opacity,nbrightness); + } + return *this; + } + + //! Draw a filled 2d triangle. + /** + \param x0 X-coordinate of the first vertex. + \param y0 Y-coordinate of the first vertex. + \param x1 X-coordinate of the second vertex. + \param y1 Y-coordinate of the second vertex. + \param x2 X-coordinate of the third vertex. + \param y2 Y-coordinate of the third vertex. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + _draw_triangle(x0,y0,x1,y1,x2,y2,color,opacity,1); + return *this; + } + + //! Draw a outlined 2d triangle. + /** + \param x0 X-coordinate of the first vertex. + \param y0 Y-coordinate of the first vertex. + \param x1 X-coordinate of the second vertex. + \param y1 Y-coordinate of the second vertex. + \param x2 X-coordinate of the third vertex. + \param y2 Y-coordinate of the third vertex. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the outline pattern. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const tc *const color, const float opacity, + const unsigned int pattern) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + draw_line(x0,y0,x1,y1,color,opacity,pattern,true). + draw_line(x1,y1,x2,y2,color,opacity,pattern,false). + draw_line(x2,y2,x0,y0,color,opacity,pattern,false); + return *this; + } + + //! Draw a filled 2d triangle, with z-buffering. + /** + \param zbuffer Z-buffer image. + \param x0 X-coordinate of the first vertex. + \param y0 Y-coordinate of the first vertex. + \param z0 Z-coordinate of the first vertex. + \param x1 X-coordinate of the second vertex. + \param y1 Y-coordinate of the second vertex. + \param z1 Z-coordinate of the second vertex. + \param x2 X-coordinate of the third vertex. + \param y2 Y-coordinate of the third vertex. + \param z2 Z-coordinate of the third vertex. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + \param brightness Brightness factor. + **/ + template + CImg& draw_triangle(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const tc *const color, const float opacity=1, + const float brightness=1) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float + nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), + nbrightness = brightness<0?0:(brightness>2?2:brightness); + const long whd = (long)_width*_height*_depth, offx = _spectrum*whd; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; + tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nz0,nz2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nz1,nz2); + if (ny0>=height() || ny2<0) return *this; + tzfloat + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); + _cimg_for_triangle1(*this,xleft0,xright0,y,nx0,ny0,nx1,ny1,nx2,ny2) { + if (y==ny1) { zl = nz1; pzl = pzn; } + int xleft = xleft0, xright = xright0; + tzfloat zleft = zl, zright = zr; + if (xright=width()-1) xright = width() - 1; + T* ptrd = data(xleft,y,0,0); + tz *ptrz = zbuffer.data(xleft,y); + if (opacity>=1) { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } + ptrd-=offx; + } + zleft+=pentez; + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)(nbrightness*(*col++)); ptrd+=whd; } + ptrd-=offx; + } + zleft+=pentez; + } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { *ptrd = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); ptrd+=whd; } + ptrd-=offx; + } + zleft+=pentez; + } + } else { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=whd; } + ptrd-=offx; + } + zleft+=pentez; + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { *ptrd = (T)(nopacity*nbrightness**(col++) + *ptrd*copacity); ptrd+=whd; } + ptrd-=offx; + } + zleft+=pentez; + } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { + const T val = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; + } + ptrd-=offx; + } + zleft+=pentez; + } + } + zr+=pzr; zl+=pzl; + } + return *this; + } + + //! Draw a Gouraud-shaded 2d triangle. + /** + \param x0 X-coordinate of the first vertex in the image instance. + \param y0 Y-coordinate of the first vertex in the image instance. + \param x1 X-coordinate of the second vertex in the image instance. + \param y1 Y-coordinate of the second vertex in the image instance. + \param x2 X-coordinate of the third vertex in the image instance. + \param y2 Y-coordinate of the third vertex in the image instance. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param brightness0 Brightness factor of the first vertex (in [0,2]). + \param brightness1 brightness factor of the second vertex (in [0,2]). + \param brightness2 brightness factor of the third vertex (in [0,2]). + \param opacity Drawing opacity. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const tc *const color, + const float brightness0, + const float brightness1, + const float brightness2, + const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), + nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), + nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nc0,nc1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nc0,nc2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nc1,nc2); + if (ny0>=height() || ny2<0) return *this; + _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { + int xleft = xleft0, xright = xright0, cleft = cleft0, cright = cright0; + if (xrightcleft?cright - cleft:cleft - cright, + rc = dx?(cright - cleft)/dx:0, + sc = cright>cleft?1:-1, + ndc = dc-(dx?dx*(dc/dx):0); + int errc = dx>>1; + if (xleft<0 && dx) cleft-=xleft*(cright - cleft)/dx; + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width() - 1; + T* ptrd = data(xleft,y); + if (opacity>=1) for (int x = xleft; x<=xright; ++x) { + const tc *col = color; + cimg_forC(*this,c) { + *ptrd = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); + ptrd+=whd; + } + ptrd-=offx; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } else for (int x = xleft; x<=xright; ++x) { + const tc *col = color; + cimg_forC(*this,c) { + const T val = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; + } + ptrd-=offx; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } + } + return *this; + } + + //! Draw a Gouraud-shaded 2d triangle, with z-buffering \overloading. + template + CImg& draw_triangle(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const tc *const color, + const float brightness0, + const float brightness1, + const float brightness2, + const float opacity=1) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, offx = _spectrum*whd; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), + nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), + nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); + tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1,nc0,nc1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nz0,nz2,nc0,nc2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nz1,nz2,nc1,nc2); + if (ny0>=height() || ny2<0) return *this; + tzfloat + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); + _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { + if (y==ny1) { zl = nz1; pzl = pzn; } + int xleft = xleft0, xright = xright0, cleft = cleft0, cright = cright0; + tzfloat zleft = zl, zright = zr; + if (xrightcleft?cright - cleft:cleft - cright, + rc = dx?(cright-cleft)/dx:0, + sc = cright>cleft?1:-1, + ndc = dc-(dx?dx*(dc/dx):0); + const tzfloat pentez = (zright - zleft)/dx; + int errc = dx>>1; + if (xleft<0 && dx) { + cleft-=xleft*(cright - cleft)/dx; + zleft-=xleft*(zright - zleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T *ptrd = data(xleft,y); + tz *ptrz = zbuffer.data(xleft,y); + if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { + *ptrd = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); + ptrd+=whd; + } + ptrd-=offx; + } + zleft+=pentez; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } else for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { + const T val = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; + } + ptrd-=offx; + } + zleft+=pentez; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } + zr+=pzr; zl+=pzl; + } + return *this; + } + + //! Draw a color-interpolated 2d triangle. + /** + \param x0 X-coordinate of the first vertex in the image instance. + \param y0 Y-coordinate of the first vertex in the image instance. + \param x1 X-coordinate of the second vertex in the image instance. + \param y1 Y-coordinate of the second vertex in the image instance. + \param x2 X-coordinate of the third vertex in the image instance. + \param y2 Y-coordinate of the third vertex in the image instance. + \param color1 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the first vertex. + \param color2 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the seconf vertex. + \param color3 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the third vertex. + \param opacity Drawing opacity. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const tc1 *const color1, + const tc2 *const color2, + const tc3 *const color3, + const float opacity=1) { + const unsigned char one = 1; + cimg_forC(*this,c) + get_shared_channel(c).draw_triangle(x0,y0,x1,y1,x2,y2,&one,color1[c],color2[c],color3[c],opacity); + return *this; + } + + //! Draw a textured 2d triangle. + /** + \param x0 X-coordinate of the first vertex in the image instance. + \param y0 Y-coordinate of the first vertex in the image instance. + \param x1 X-coordinate of the second vertex in the image instance. + \param y1 Y-coordinate of the second vertex in the image instance. + \param x2 X-coordinate of the third vertex in the image instance. + \param y2 Y-coordinate of the third vertex in the image instance. + \param texture Texture image used to fill the triangle. + \param tx0 X-coordinate of the first vertex in the texture image. + \param ty0 Y-coordinate of the first vertex in the texture image. + \param tx1 X-coordinate of the second vertex in the texture image. + \param ty1 Y-coordinate of the second vertex in the texture image. + \param tx2 X-coordinate of the third vertex in the texture image. + \param ty2 Y-coordinate of the third vertex in the texture image. + \param opacity Drawing opacity. + \param brightness Brightness factor of the drawing (in [0,2]). + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const float opacity=1, + const float brightness=1) { + if (is_empty()) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float + nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), + nbrightness = brightness<0?0:(brightness>2?2:brightness); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + ntx0 = tx0, nty0 = ty0, ntx1 = tx1, nty1 = ty1, ntx2 = tx2, nty2 = ty2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2); + if (ny0>=height() || ny2<0) return *this; + _cimg_for_triangle3(*this,xleft0,txleft0,tyleft0,xright0,txright0,tyright0,y, + nx0,ny0,ntx0,nty0,nx1,ny1,ntx1,nty1,nx2,ny2,ntx2,nty2) { + int + xleft = xleft0, xright = xright0, + txleft = txleft0, txright = txright0, + tyleft = tyleft0, tyright = tyright0; + if (xrighttxleft?txright - txleft:txleft - txright, + dty = tyright>tyleft?tyright - tyleft:tyleft - tyright, + rtx = dx?(txright - txleft)/dx:0, + rty = dx?(tyright - tyleft)/dx:0, + stx = txright>txleft?1:-1, + sty = tyright>tyleft?1:-1, + ndtx = dtx - (dx?dx*(dtx/dx):0), + ndty = dty - (dx?dx*(dty/dx):0); + int errtx = dx>>1, errty = errtx; + if (xleft<0 && dx) { + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + *ptrd = (T)*col; + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + *ptrd = (T)(nbrightness**col); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } else for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + *ptrd = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } + } else { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + *ptrd = (T)(nopacity**col + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + *ptrd = (T)(nopacity*nbrightness**col + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } else for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + const T val = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } + } + } + return *this; + } + + //! Draw a 2d textured triangle, with perspective correction. + template + CImg& draw_triangle(const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const float opacity=1, + const float brightness=1) { + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float + nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), + nbrightness = brightness<0?0:(brightness>2?2:brightness); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; + float + ntx0 = tx0/z0, nty0 = ty0/z0, + ntx1 = tx1/z1, nty1 = ty1/z1, + ntx2 = tx2/z2, nty2 = ty2/z2, + nz0 = 1/z0, nz1 = 1/z1, nz2 = 1/z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2); + if (ny0>=height() || ny2<0) return *this; + float + ptxl = (ntx1 - ntx0)/(ny1 - ny0), + ptxr = (ntx2 - ntx0)/(ny2 - ny0), + ptxn = (ntx2 - ntx1)/(ny2 - ny1), + ptyl = (nty1 - nty0)/(ny1 - ny0), + ptyr = (nty2 - nty0)/(ny2 - ny0), + ptyn = (nty2 - nty1)/(ny2 - ny1), + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), + tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))), + txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): + (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), + tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): + (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); + _cimg_for_triangle1(*this,xleft0,xright0,y,nx0,ny0,nx1,ny1,nx2,ny2) { + if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } + int xleft = xleft0, xright = xright0; + float + zleft = zl, zright = zr, + txleft = txl, txright = txr, + tyleft = tyl, tyright = tyr; + if (xright=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)*col; + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else if (nbrightness<1) for (int x=xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(nbrightness**col); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } + } else { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(nopacity**col + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(nopacity*nbrightness**col + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const T val = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } + } + zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; + } + return *this; + } + + //! Draw a textured 2d triangle, with perspective correction and z-buffering. + template + CImg& draw_triangle(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const float opacity=1, + const float brightness=1) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float + nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), + nbrightness = brightness<0?0:(brightness>2?2:brightness); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; + float + ntx0 = tx0/z0, nty0 = ty0/z0, + ntx1 = tx1/z1, nty1 = ty1/z1, + ntx2 = tx2/z2, nty2 = ty2/z2; + tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2); + if (ny0>=height() || ny2<0) return *this; + float + ptxl = (ntx1 - ntx0)/(ny1 - ny0), + ptxr = (ntx2 - ntx0)/(ny2 - ny0), + ptxn = (ntx2 - ntx1)/(ny2 - ny1), + ptyl = (nty1 - nty0)/(ny1 - ny0), + ptyr = (nty2 - nty0)/(ny2 - ny0), + ptyn = (nty2 - nty1)/(ny2 - ny1), + txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), + tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), + txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): + (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), + tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): + (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); + tzfloat + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); + _cimg_for_triangle1(*this,xleft0,xright0,y,nx0,ny0,nx1,ny1,nx2,ny2) { + if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } + int xleft = xleft0, xright = xright0; + float txleft = txl, txright = txr, tyleft = tyl, tyright = tyr; + tzfloat zleft = zl, zright = zr; + if (xright=width()-1) xright = width()-1; + T *ptrd = data(xleft,y,0,0); + tz *ptrz = zbuffer.data(xleft,y); + if (opacity>=1) { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)*col; + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(nbrightness**col); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } + } else { + if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(nopacity**col + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(nopacity*nbrightness**col + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const T val = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + } + } + zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; + } + return *this; + } + + //! Draw a Phong-shaded 2d triangle. + /** + \param x0 X-coordinate of the first vertex in the image instance. + \param y0 Y-coordinate of the first vertex in the image instance. + \param x1 X-coordinate of the second vertex in the image instance. + \param y1 Y-coordinate of the second vertex in the image instance. + \param x2 X-coordinate of the third vertex in the image instance. + \param y2 Y-coordinate of the third vertex in the image instance. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param light Light image. + \param lx0 X-coordinate of the first vertex in the light image. + \param ly0 Y-coordinate of the first vertex in the light image. + \param lx1 X-coordinate of the second vertex in the light image. + \param ly1 Y-coordinate of the second vertex in the light image. + \param lx2 X-coordinate of the third vertex in the light image. + \param ly2 Y-coordinate of the third vertex in the light image. + \param opacity Drawing opacity. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const tc *const color, + const CImg& light, + const int lx0, const int ly0, + const int lx1, const int ly1, + const int lx2, const int ly2, + const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + if (light._depth>1 || light._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", + cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); + if (is_overlapped(light)) return draw_triangle(x0,y0,x1,y1,x2,y2,color,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; + const long whd = (long)_width*_height*_depth, offx = _spectrum*whd-1; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nlx0,nlx1,nly0,nly1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nlx0,nlx2,nly0,nly2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nlx1,nlx2,nly1,nly2); + if (ny0>=height() || ny2<0) return *this; + _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, + nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { + int + xleft = xleft0, xright = xright0, + lxleft = lxleft0, lxright = lxright0, + lyleft = lyleft0, lyright = lyright0; + if (xrightlxleft?lxright - lxleft:lxleft - lxright, + dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, + rlx = dx?(lxright - lxleft)/dx:0, + rly = dx?(lyright - lyleft)/dx:0, + slx = lxright>lxleft?1:-1, + sly = lyright>lyleft?1:-1, + ndlx = dlx - (dx?dx*(dlx/dx):0), + ndly = dly - (dx?dx*(dly/dx):0); + int errlx = dx>>1, errly = errlx; + if (xleft<0 && dx) { + lxleft-=xleft*(lxright - lxleft)/dx; + lyleft-=xleft*(lyright - lyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) for (int x = xleft; x<=xright; ++x) { + const tc *col = color; + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + *ptrd = (T)(l<1?l**(col++):((2-l)**(col++)+(l-1)*maxval)); + ptrd+=whd; + } + ptrd-=offx; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } else for (int x = xleft; x<=xright; ++x) { + const tc *col = color; + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + const T val = (T)(l<1?l**(col++):((2-l)**(col++)+(l-1)*maxval)); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; + } + ptrd-=offx; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } + } + return *this; + } + + //! Draw a Phong-shaded 2d triangle, with z-buffering. + template + CImg& draw_triangle(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const tc *const color, + const CImg& light, + const int lx0, const int ly0, + const int lx1, const int ly1, + const int lx2, const int ly2, + const float opacity=1) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Specified color is (null).", + cimg_instance); + if (light._depth>1 || light._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", + cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + if (is_overlapped(light)) return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color, + +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, offx = _spectrum*whd; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; + tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nlx0,nlx1,nly0,nly1,nz0,nz1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nlx0,nlx2,nly0,nly2,nz0,nz2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nlx1,nlx2,nly1,nly2,nz1,nz2); + if (ny0>=height() || ny2<0) return *this; + tzfloat + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); + _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, + nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { + if (y==ny1) { zl = nz1; pzl = pzn; } + int + xleft = xleft0, xright = xright0, + lxleft = lxleft0, lxright = lxright0, + lyleft = lyleft0, lyright = lyright0; + tzfloat zleft = zl, zright = zr; + if (xrightlxleft?lxright - lxleft:lxleft - lxright, + dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, + rlx = dx?(lxright - lxleft)/dx:0, + rly = dx?(lyright - lyleft)/dx:0, + slx = lxright>lxleft?1:-1, + sly = lyright>lyleft?1:-1, + ndlx = dlx - (dx?dx*(dlx/dx):0), + ndly = dly - (dx?dx*(dly/dx):0); + const tzfloat pentez = (zright - zleft)/dx; + int errlx = dx>>1, errly = errlx; + if (xleft<0 && dx) { + zleft-=xleft*(zright - zleft)/dx; + lxleft-=xleft*(lxright - lxleft)/dx; + lyleft-=xleft*(lyright - lyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T *ptrd = data(xleft,y,0,0); + tz *ptrz = zbuffer.data(xleft,y); + if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + const tc cval = *(col++); + *ptrd = (T)(l<1?l*cval:(2-l)*cval+(l-1)*maxval); + ptrd+=whd; + } + ptrd-=offx; + } + zleft+=pentez; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tc *col = color; + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + const tc cval = *(col++); + const T val = (T)(l<1?l*cval:(2-l)*cval+(l-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; + } + ptrd-=offx; + } + zleft+=pentez; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } + zr+=pzr; zl+=pzl; + } + return *this; + } + + //! Draw a textured Gouraud-shaded 2d triangle. + /** + \param x0 X-coordinate of the first vertex in the image instance. + \param y0 Y-coordinate of the first vertex in the image instance. + \param x1 X-coordinate of the second vertex in the image instance. + \param y1 Y-coordinate of the second vertex in the image instance. + \param x2 X-coordinate of the third vertex in the image instance. + \param y2 Y-coordinate of the third vertex in the image instance. + \param texture Texture image used to fill the triangle. + \param tx0 X-coordinate of the first vertex in the texture image. + \param ty0 Y-coordinate of the first vertex in the texture image. + \param tx1 X-coordinate of the second vertex in the texture image. + \param ty1 Y-coordinate of the second vertex in the texture image. + \param tx2 X-coordinate of the third vertex in the texture image. + \param ty2 Y-coordinate of the third vertex in the texture image. + \param brightness0 Brightness factor of the first vertex. + \param brightness1 Brightness factor of the second vertex. + \param brightness2 Brightness factor of the third vertex. + \param opacity Drawing opacity. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const float brightness0, + const float brightness1, + const float brightness2, + const float opacity=1) { + if (is_empty()) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, + brightness0,brightness1,brightness2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + ntx0 = tx0, nty0 = ty0, ntx1 = tx1, nty1 = ty1, ntx2 = tx2, nty2 = ty2, + nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), + nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), + nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nc0,nc1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nc0,nc2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nc1,nc2); + if (ny0>=height() || ny2<0) return *this; + _cimg_for_triangle4(*this,xleft0,cleft0,txleft0,tyleft0,xright0,cright0,txright0,tyright0,y, + nx0,ny0,nc0,ntx0,nty0,nx1,ny1,nc1,ntx1,nty1,nx2,ny2,nc2,ntx2,nty2) { + int + xleft = xleft0, xright = xright0, + cleft = cleft0, cright = cright0, + txleft = txleft0, txright = txright0, + tyleft = tyleft0, tyright = tyright0; + if (xrightcleft?cright - cleft:cleft - cright, + dtx = txright>txleft?txright - txleft:txleft - txright, + dty = tyright>tyleft?tyright - tyleft:tyleft - tyright, + rc = dx?(cright - cleft)/dx:0, + rtx = dx?(txright - txleft)/dx:0, + rty = dx?(tyright - tyleft)/dx:0, + sc = cright>cleft?1:-1, + stx = txright>txleft?1:-1, + sty = tyright>tyleft?1:-1, + ndc = dc - (dx?dx*(dc/dx):0), + ndtx = dtx - (dx?dx*(dtx/dx):0), + ndty = dty - (dx?dx*(dty/dx):0); + int errc = dx>>1, errtx = errc, errty = errc; + if (xleft<0 && dx) { + cleft-=xleft*(cright - cleft)/dx; + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + *ptrd = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } else for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + const T val = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } + } + return *this; + } + + //! Draw a textured Gouraud-shaded 2d triangle, with perspective correction \overloading. + template + CImg& draw_triangle(const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const float brightness0, + const float brightness1, + const float brightness2, + const float opacity=1) { + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, + brightness0,brightness1,brightness2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), + nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), + nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); + float + ntx0 = tx0/z0, nty0 = ty0/z0, + ntx1 = tx1/z1, nty1 = ty1/z1, + ntx2 = tx2/z2, nty2 = ty2/z2, + nz0 = 1/z0, nz1 = 1/z1, nz2 = 1/z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1,nc0,nc1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2,nc0,nc2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2,nc1,nc2); + if (ny0>=height() || ny2<0) return *this; + float + ptxl = (ntx1 - ntx0)/(ny1 - ny0), + ptxr = (ntx2 - ntx0)/(ny2 - ny0), + ptxn = (ntx2 - ntx1)/(ny2 - ny1), + ptyl = (nty1 - nty0)/(ny1 - ny0), + ptyr = (nty2 - nty0)/(ny2 - ny0), + ptyn = (nty2 - nty1)/(ny2 - ny1), + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), + tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))), + txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): + (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), + tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): + (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); + _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { + if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } + int + xleft = xleft0, xright = xright0, + cleft = cleft0, cright = cright0; + float + zleft = zl, zright = zr, + txleft = txl, txright = txr, + tyleft = tyl, tyright = tyr; + if (xrightcleft?cright - cleft:cleft - cright, + rc = dx?(cright - cleft)/dx:0, + sc = cright>cleft?1:-1, + ndc = dc - (dx?dx*(dc/dx):0); + const float + pentez = (zright - zleft)/dx, + pentetx = (txright - txleft)/dx, + pentety = (tyright - tyleft)/dx; + int errc = dx>>1; + if (xleft<0 && dx) { + cleft-=xleft*(cright - cleft)/dx; + zleft-=xleft*(zright - zleft)/dx; + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } else for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const T val = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } + zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; + } + return *this; + } + + //! Draw a textured Gouraud-shaded 2d triangle, with perspective correction and z-buffering \overloading. + template + CImg& draw_triangle(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const float brightness0, + const float brightness1, + const float brightness2, + const float opacity=1) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (is_overlapped(texture)) + return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, + brightness0,brightness1,brightness2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), + nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), + nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); + float + ntx0 = tx0/z0, nty0 = ty0/z0, + ntx1 = tx1/z1, nty1 = ty1/z1, + ntx2 = tx2/z2, nty2 = ty2/z2; + tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1,nc0,nc1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2,nc0,nc2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2,nc1,nc2); + if (ny0>=height() || ny2<0) return *this; + float + ptxl = (ntx1 - ntx0)/(ny1 - ny0), + ptxr = (ntx2 - ntx0)/(ny2 - ny0), + ptxn = (ntx2 - ntx1)/(ny2 - ny1), + ptyl = (nty1 - nty0)/(ny1 - ny0), + ptyr = (nty2 - nty0)/(ny2 - ny0), + ptyn = (nty2 - nty1)/(ny2 - ny1), + txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), + tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), + txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): + (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), + tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): + (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); + tzfloat + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); + _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { + if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } + int xleft = xleft0, xright = xright0, cleft = cleft0, cright = cright0; + float txleft = txl, txright = txr, tyleft = tyl, tyright = tyr; + tzfloat zleft = zl, zright = zr; + if (xrightcleft?cright - cleft:cleft - cright, + rc = dx?(cright - cleft)/dx:0, + sc = cright>cleft?1:-1, + ndc = dc - (dx?dx*(dc/dx):0); + float pentetx = (txright - txleft)/dx, pentety = (tyright - tyleft)/dx; + const tzfloat pentez = (zright - zleft)/dx; + int errc = dx>>1; + if (xleft<0 && dx) { + cleft-=xleft*(cright - cleft)/dx; + zleft-=xleft*(zright - zleft)/dx; + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y); + tz *ptrz = zbuffer.data(xleft,y); + if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + *ptrd = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } else for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const T val = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); + } + zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; + } + return *this; + } + + //! Draw a textured Phong-shaded 2d triangle. + /** + \param x0 X-coordinate of the first vertex in the image instance. + \param y0 Y-coordinate of the first vertex in the image instance. + \param x1 X-coordinate of the second vertex in the image instance. + \param y1 Y-coordinate of the second vertex in the image instance. + \param x2 X-coordinate of the third vertex in the image instance. + \param y2 Y-coordinate of the third vertex in the image instance. + \param texture Texture image used to fill the triangle. + \param tx0 X-coordinate of the first vertex in the texture image. + \param ty0 Y-coordinate of the first vertex in the texture image. + \param tx1 X-coordinate of the second vertex in the texture image. + \param ty1 Y-coordinate of the second vertex in the texture image. + \param tx2 X-coordinate of the third vertex in the texture image. + \param ty2 Y-coordinate of the third vertex in the texture image. + \param light Light image. + \param lx0 X-coordinate of the first vertex in the light image. + \param ly0 Y-coordinate of the first vertex in the light image. + \param lx1 X-coordinate of the second vertex in the light image. + \param ly1 Y-coordinate of the second vertex in the light image. + \param lx2 X-coordinate of the third vertex in the light image. + \param ly2 Y-coordinate of the third vertex in the light image. + \param opacity Drawing opacity. + **/ + template + CImg& draw_triangle(const int x0, const int y0, + const int x1, const int y1, + const int x2, const int y2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const CImg& light, + const int lx0, const int ly0, + const int lx1, const int ly1, + const int lx2, const int ly2, + const float opacity=1) { + if (is_empty()) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (light._depth>1 || light._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", + cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); + if (is_overlapped(texture)) + return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + if (is_overlapped(light)) + return draw_triangle(x0,y0,x1,y1,x2,y2,texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + ntx0 = tx0, nty0 = ty0, ntx1 = tx1, nty1 = ty1, ntx2 = tx2, nty2 = ty2, + nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nlx0,nlx1,nly0,nly1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nlx0,nlx2,nly0,nly2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nlx1,nlx2,nly1,nly2); + if (ny0>=height() || ny2<0) return *this; + _cimg_for_triangle5(*this,xleft0,lxleft0,lyleft0,txleft0,tyleft0,xright0,lxright0,lyright0,txright0,tyright0,y, + nx0,ny0,nlx0,nly0,ntx0,nty0,nx1,ny1,nlx1,nly1,ntx1,nty1,nx2,ny2,nlx2,nly2,ntx2,nty2) { + int + xleft = xleft0, xright = xright0, + lxleft = lxleft0, lxright = lxright0, + lyleft = lyleft0, lyright = lyright0, + txleft = txleft0, txright = txright0, + tyleft = tyleft0, tyright = tyright0; + if (xrightlxleft?lxright - lxleft:lxleft - lxright, + dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, + dtx = txright>txleft?txright - txleft:txleft - txright, + dty = tyright>tyleft?tyright - tyleft:tyleft - tyright, + rlx = dx?(lxright - lxleft)/dx:0, + rly = dx?(lyright - lyleft)/dx:0, + rtx = dx?(txright - txleft)/dx:0, + rty = dx?(tyright - tyleft)/dx:0, + slx = lxright>lxleft?1:-1, + sly = lyright>lyleft?1:-1, + stx = txright>txleft?1:-1, + sty = tyright>tyleft?1:-1, + ndlx = dlx - (dx?dx*(dlx/dx):0), + ndly = dly - (dx?dx*(dly/dx):0), + ndtx = dtx - (dx?dx*(dtx/dx):0), + ndty = dty - (dx?dx*(dty/dx):0); + int errlx = dx>>1, errly = errlx, errtx = errlx, errty = errlx; + if (xleft<0 && dx) { + lxleft-=xleft*(lxright - lxleft)/dx; + lyleft-=xleft*(lyright - lyleft)/dx; + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + *ptrd = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } else for (int x = xleft; x<=xright; ++x) { + const tc *col = texture.data(txleft,tyleft); + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + const T val = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); + tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); + } + } + return *this; + } + + //! Draw a textured Phong-shaded 2d triangle, with perspective correction. + template + CImg& draw_triangle(const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const CImg& light, + const int lx0, const int ly0, + const int lx1, const int ly1, + const int lx2, const int ly2, + const float opacity=1) { + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (light._depth>1 || light._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", + cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); + if (is_overlapped(texture)) + return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, + light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + if (is_overlapped(light)) + return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,texture,tx0,ty0,tx1,ty1,tx2,ty2, + +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd-1; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; + float + ntx0 = tx0/z0, nty0 = ty0/z0, + ntx1 = tx1/z1, nty1 = ty1/z1, + ntx2 = tx2/z2, nty2 = ty2/z2, + nz0 = 1/z0, nz1 = 1/z1, nz2 = 1/z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nlx0,nlx1,nly0,nly1,nz0,nz1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nlx0,nlx2,nly0,nly2,nz0,nz2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nlx1,nlx2,nly1,nly2,nz1,nz2); + if (ny0>=height() || ny2<0) return *this; + float + ptxl = (ntx1 - ntx0)/(ny1 - ny0), + ptxr = (ntx2 - ntx0)/(ny2 - ny0), + ptxn = (ntx2 - ntx1)/(ny2 - ny1), + ptyl = (nty1 - nty0)/(ny1 - ny0), + ptyr = (nty2 - nty0)/(ny2 - ny0), + ptyn = (nty2 - nty1)/(ny2 - ny1), + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), + tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))), + txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): + (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), + tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): + (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); + _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, + nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { + if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } + int + xleft = xleft0, xright = xright0, + lxleft = lxleft0, lxright = lxright0, + lyleft = lyleft0, lyright = lyright0; + float + zleft = zl, zright = zr, + txleft = txl, txright = txr, + tyleft = tyl, tyright = tyr; + if (xrightlxleft?lxright - lxleft:lxleft - lxright, + dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, + rlx = dx?(lxright - lxleft)/dx:0, + rly = dx?(lyright - lyleft)/dx:0, + slx = lxright>lxleft?1:-1, + sly = lyright>lyleft?1:-1, + ndlx = dlx - (dx?dx*(dlx/dx):0), + ndly = dly - (dx?dx*(dly/dx):0); + const float + pentez = (zright - zleft)/dx, + pentetx = (txright - txleft)/dx, + pentety = (tyright - tyleft)/dx; + int errlx = dx>>1, errly = errlx; + if (xleft<0 && dx) { + zleft-=xleft*(zright - zleft)/dx; + lxleft-=xleft*(lxright - lxleft)/dx; + lyleft-=xleft*(lyright - lyleft)/dx; + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y,0,0); + if (opacity>=1) for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + *ptrd = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } else for (int x = xleft; x<=xright; ++x) { + const float invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + const T val = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } + zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; + } + return *this; + } + + //! Draw a textured Phong-shaded 2d triangle, with perspective correction and z-buffering. + template + CImg& draw_triangle(CImg& zbuffer, + const int x0, const int y0, const float z0, + const int x1, const int y1, const float z1, + const int x2, const int y2, const float z2, + const CImg& texture, + const int tx0, const int ty0, + const int tx1, const int ty1, + const int tx2, const int ty2, + const CImg& light, + const int lx0, const int ly0, + const int lx1, const int ly1, + const int lx2, const int ly2, + const float opacity=1) { + typedef typename cimg::superset::type tzfloat; + if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; + if (!is_sameXY(zbuffer)) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " + "different dimensions.", + cimg_instance, + zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); + if (texture._depth>1 || texture._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", + cimg_instance, + texture._width,texture._height,texture._depth,texture._spectrum,texture._data); + if (light._depth>1 || light._spectrum<_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", + cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); + if (is_overlapped(texture)) + return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2, + +texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + if (is_overlapped(light)) + return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2, + texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, + offx = _spectrum*whd; + int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, + nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; + float + ntx0 = tx0/z0, nty0 = ty0/z0, + ntx1 = tx1/z1, nty1 = ty1/z1, + ntx2 = tx2/z2, nty2 = ty2/z2; + tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; + if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nlx0,nlx1,nly0,nly1,nz0,nz1); + if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nlx0,nlx2,nly0,nly2,nz0,nz2); + if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nlx1,nlx2,nly1,nly2,nz1,nz2); + if (ny0>=height() || ny2<0) return *this; + float + ptxl = (ntx1 - ntx0)/(ny1 - ny0), + ptxr = (ntx2 - ntx0)/(ny2 - ny0), + ptxn = (ntx2 - ntx1)/(ny2 - ny1), + ptyl = (nty1 - nty0)/(ny1 - ny0), + ptyr = (nty2 - nty0)/(ny2 - ny0), + ptyn = (nty2 - nty1)/(ny2 - ny1), + txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), + tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), + txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): + (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), + tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): + (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); + tzfloat + pzl = (nz1 - nz0)/(ny1 - ny0), + pzr = (nz2 - nz0)/(ny2 - ny0), + pzn = (nz2 - nz1)/(ny2 - ny1), + zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), + zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); + _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, + nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { + if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } + int + xleft = xleft0, xright = xright0, + lxleft = lxleft0, lxright = lxright0, + lyleft = lyleft0, lyright = lyright0; + float txleft = txl, txright = txr, tyleft = tyl, tyright = tyr; + tzfloat zleft = zl, zright = zr; + if (xrightlxleft?lxright - lxleft:lxleft - lxright, + dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, + rlx = dx?(lxright - lxleft)/dx:0, + rly = dx?(lyright - lyleft)/dx:0, + slx = lxright>lxleft?1:-1, + sly = lyright>lyleft?1:-1, + ndlx = dlx - (dx?dx*(dlx/dx):0), + ndly = dly - (dx?dx*(dly/dx):0); + float pentetx = (txright - txleft)/dx, pentety = (tyright - tyleft)/dx; + const tzfloat pentez = (zright - zleft)/dx; + int errlx = dx>>1, errly = errlx; + if (xleft<0 && dx) { + zleft-=xleft*(zright - zleft)/dx; + lxleft-=xleft*(lxright - lxleft)/dx; + lyleft-=xleft*(lyright - lyleft)/dx; + txleft-=xleft*(txright - txleft)/dx; + tyleft-=xleft*(tyright - tyleft)/dx; + } + if (xleft<0) xleft = 0; + if (xright>=width()-1) xright = width()-1; + T* ptrd = data(xleft,y); + tz *ptrz = zbuffer.data(xleft,y); + if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + *ptrd = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { + if (zleft>=(tzfloat)*ptrz) { + *ptrz = (tz)zleft; + const tzfloat invz = 1/zleft; + const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); + cimg_forC(*this,c) { + const tl l = light(lxleft,lyleft,c); + const T val = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); + *ptrd = (T)(nopacity*val + *ptrd*copacity); + ptrd+=whd; col+=twhd; + } + ptrd-=offx; + } + zleft+=pentez; txleft+=pentetx; tyleft+=pentety; + lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); + lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); + } + zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; + } + return *this; + } + + //! Draw a filled 4d rectangle. + /** + \param x0 X-coordinate of the upper-left rectangle corner. + \param y0 Y-coordinate of the upper-left rectangle corner. + \param z0 Z-coordinate of the upper-left rectangle corner. + \param c0 C-coordinate of the upper-left rectangle corner. + \param x1 X-coordinate of the lower-right rectangle corner. + \param y1 Y-coordinate of the lower-right rectangle corner. + \param z1 Z-coordinate of the lower-right rectangle corner. + \param c1 C-coordinate of the lower-right rectangle corner. + \param val Scalar value used to fill the rectangle area. + \param opacity Drawing opacity. + **/ + CImg& draw_rectangle(const int x0, const int y0, const int z0, const int c0, + const int x1, const int y1, const int z1, const int c1, + const T val, const float opacity=1) { + if (is_empty()) return *this; + const bool bx = (x0=width()?width() - 1 - nx1:0) + (nx0<0?nx0:0), + lY = (1 + ny1 - ny0) + (ny1>=height()?height() - 1 - ny1:0) + (ny0<0?ny0:0), + lZ = (1 + nz1 - nz0) + (nz1>=depth()?depth() - 1 - nz1:0) + (nz0<0?nz0:0), + lC = (1 + nc1 - nc0) + (nc1>=spectrum()?spectrum() - 1 - nc1:0) + (nc0<0?nc0:0); + const unsigned long + offX = (unsigned long)_width - lX, + offY = (unsigned long)_width*(_height - lY), + offZ = (unsigned long)_width*_height*(_depth - lZ); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + T *ptrd = data(nx0<0?0:nx0,ny0<0?0:ny0,nz0<0?0:nz0,nc0<0?0:nc0); + if (lX>0 && lY>0 && lZ>0 && lC>0) + for (int v = 0; v=1) { + if (sizeof(T)!=1) { for (int x = 0; x + CImg& draw_rectangle(const int x0, const int y0, const int z0, + const int x1, const int y1, const int z1, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_rectangle(): Specified color is (null).", + cimg_instance); + cimg_forC(*this,c) draw_rectangle(x0,y0,z0,c,x1,y1,z1,c,(T)color[c],opacity); + return *this; + } + + //! Draw an outlined 3d rectangle \overloading. + template + CImg& draw_rectangle(const int x0, const int y0, const int z0, + const int x1, const int y1, const int z1, + const tc *const color, const float opacity, + const unsigned int pattern) { + return draw_line(x0,y0,z0,x1,y0,z0,color,opacity,pattern,true). + draw_line(x1,y0,z0,x1,y1,z0,color,opacity,pattern,false). + draw_line(x1,y1,z0,x0,y1,z0,color,opacity,pattern,false). + draw_line(x0,y1,z0,x0,y0,z0,color,opacity,pattern,false). + draw_line(x0,y0,z1,x1,y0,z1,color,opacity,pattern,true). + draw_line(x1,y0,z1,x1,y1,z1,color,opacity,pattern,false). + draw_line(x1,y1,z1,x0,y1,z1,color,opacity,pattern,false). + draw_line(x0,y1,z1,x0,y0,z1,color,opacity,pattern,false). + draw_line(x0,y0,z0,x0,y0,z1,color,opacity,pattern,true). + draw_line(x1,y0,z0,x1,y0,z1,color,opacity,pattern,true). + draw_line(x1,y1,z0,x1,y1,z1,color,opacity,pattern,true). + draw_line(x0,y1,z0,x0,y1,z1,color,opacity,pattern,true); + } + + //! Draw a filled 2d rectangle. + /** + \param x0 X-coordinate of the upper-left rectangle corner. + \param y0 Y-coordinate of the upper-left rectangle corner. + \param x1 X-coordinate of the lower-right rectangle corner. + \param y1 Y-coordinate of the lower-right rectangle corner. + \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. + \param opacity Drawing opacity. + **/ + template + CImg& draw_rectangle(const int x0, const int y0, + const int x1, const int y1, + const tc *const color, const float opacity=1) { + return draw_rectangle(x0,y0,0,x1,y1,_depth-1,color,opacity); + } + + //! Draw a outlined 2d rectangle \overloading. + template + CImg& draw_rectangle(const int x0, const int y0, + const int x1, const int y1, + const tc *const color, const float opacity, + const unsigned int pattern) { + if (is_empty()) return *this; + if (y0==y1) return draw_line(x0,y0,x1,y0,color,opacity,pattern,true); + if (x0==x1) return draw_line(x0,y0,x0,y1,color,opacity,pattern,true); + const bool bx = (x0 + CImg& draw_polygon(const CImg& points, + const tc *const color, const float opacity=1) { + if (is_empty() || !points) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_polygon(): Specified color is (null).", + cimg_instance); + + // Normalize 2d input coordinates (remove adjacent duplicates). + CImg npoints(points._width,2); + unsigned int nb_points = 1, p = 0; + int cx = npoints(0,0) = (int)points(0,0), cy = npoints(0,1) = (int)points(0,1); + const int cx0 = cx, cy0 = cy; + for (p = 1; p npoints_x = npoints.get_shared_row(0), npoints_y = npoints.get_shared_row(1); + int xmax = 0, xmin = (int)npoints_x.min_max(xmax), ymax = 0, ymin = (int)npoints_y.min_max(ymax); + if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this; + if (ymin==ymax) return cimg_draw_scanline(xmin,xmax,ymin,color,opacity,1); + const unsigned int + nxmin = xmin<0?0:(unsigned int)xmin, nxmax = xmax>=width()?_width-1:(unsigned int)xmax, + nymin = ymin<0?0:(unsigned int)ymin, nymax = ymax>=height()?_height-1:(unsigned int)ymax, + dx = 1 + nxmax - nxmin, + dy = 1 + nymax - nymin; + npoints_x-=nxmin; npoints_y-=nymin; + unsigned char one = 1; + const CImg mask = CImg(dx,dy,1,1,0).draw_polygon(npoints,&one,1); + CImg _color(dx,dy,1,spectrum()); + cimg_forC(_color,c) _color.get_shared_channel(c).fill(color[c]); + return draw_image(nxmin,nymin,0,0,_color,mask,opacity,1); + } + + // Draw polygon segments. + int + xmax = 0, xmin = (int)npoints.get_shared_points(0,nb_points-1,0).min_max(xmax), + ymax = 0, ymin = (int)npoints.get_shared_points(0,nb_points-1,1).min_max(ymax); + if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this; + if (ymin==ymax) return cimg_draw_scanline(xmin,xmax,ymin,color,1,1); + const unsigned int + nymin = ymin<0?0:(unsigned int)ymin, + nymax = ymax>=height()?_height-1:(unsigned int)ymax, + dy = 1 + nymax - nymin; + CImg X(1+2*nb_points,dy,1,1,0), tmp; + cx = (int)npoints(0,0), cy = (int)npoints(0,1); + unsigned int cp = 0; + for (unsigned int p = 0; pay && cy>ny))?1:0; + for (int x = cx, y = y0, _sx = 1, _sy = 1, + _dx = nx>cx?nx-cx:((_sx=-1),cx-nx), + _dy = y1>y0?y1-y0:((_sy=-1),y0-y1), + _counter = ((_dx-=_dy?_dy*(_dx/_dy):0),_dy), + _err = _dx>>1, + _rx = _dy?(nx-cx)/_dy:0; + _counter>=countermin; + --_counter, y+=_sy, x+=_rx + ((_err-=_dx)<0?_err+=_dy,_sx:0)) + if (y>=0 && y<(int)dy) X(++X(0,y),y) = x; + cp = np; cx = nx; cy = ny; + } else { + const int pp = (cp?cp-1:nb_points-1), py = (int)npoints(pp,1); + if (y0>=0 && y0<(int)dy) { + cimg_draw_scanline(cxpy && ay>cy) || (cy + CImg& draw_polygon(const CImg& points, + const tc *const color, const float opacity, const unsigned int pattern) { + if (is_empty() || !points || points._width<3) return *this; + bool ninit_hatch = true; + switch (points._height) { + case 0 : case 1 : + throw CImgArgumentException(_cimg_instance + "draw_polygon(): Invalid specified point set.", + cimg_instance); + case 2 : { // 2d version. + CImg npoints(points._width,2); + int x = npoints(0,0) = (int)points(0,0), y = npoints(0,1) = (int)points(0,1); + unsigned int nb_points = 1; + for (unsigned int p = 1; p npoints(points._width,3); + int + x = npoints(0,0) = (int)points(0,0), + y = npoints(0,1) = (int)points(0,1), + z = npoints(0,2) = (int)points(0,2); + unsigned int nb_points = 1; + for (unsigned int p = 1; p + CImg& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle, + const tc *const color, const float opacity=1) { + return _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,0U); + } + + //! Draw a filled 2d ellipse \overloading. + /** + \param x0 X-coordinate of the ellipse center. + \param y0 Y-coordinate of the ellipse center. + \param tensor Diffusion tensor describing the ellipse. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + **/ + template + CImg& draw_ellipse(const int x0, const int y0, const CImg &tensor, + const tc *const color, const float opacity=1) { + CImgList eig = tensor.get_symmetric_eigen(); + const CImg &val = eig[0], &vec = eig[1]; + return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)), + std::atan2(vec(0,1),vec(0,0))*180/cimg::PI, + color,opacity); + } + + //! Draw an outlined 2d ellipse. + /** + \param x0 X-coordinate of the ellipse center. + \param y0 Y-coordinate of the ellipse center. + \param r1 First radius of the ellipse. + \param r2 Second radius of the ellipse. + \param angle Angle of the first radius. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the outline pattern. + **/ + template + CImg& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle, + const tc *const color, const float opacity, const unsigned int pattern) { + if (pattern) _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,pattern); + return *this; + } + + //! Draw an outlined 2d ellipse \overloading. + /** + \param x0 X-coordinate of the ellipse center. + \param y0 Y-coordinate of the ellipse center. + \param tensor Diffusion tensor describing the ellipse. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \param pattern An integer whose bits describe the outline pattern. + **/ + template + CImg& draw_ellipse(const int x0, const int y0, const CImg &tensor, + const tc *const color, const float opacity, + const unsigned int pattern) { + CImgList eig = tensor.get_symmetric_eigen(); + const CImg &val = eig[0], &vec = eig[1]; + return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)), + std::atan2(vec(0,1),vec(0,0))*180/cimg::PI, + color,opacity,pattern); + } + + template + CImg& _draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle, + const tc *const color, const float opacity, + const unsigned int pattern) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_ellipse(): Specified color is (null).", + cimg_instance); + if (r1<=0 || r2<=0) return draw_point(x0,y0,color,opacity); + cimg_init_scanline(color,opacity); + const float + nr1 = cimg::abs(r1), nr2 = cimg::abs(r2), + nangle = (float)(angle*cimg::PI/180), + u = (float)std::cos(nangle), + v = (float)std::sin(nangle), + rmax = cimg::max(nr1,nr2), + l1 = (float)std::pow(rmax/(nr1>0?nr1:1e-6),2), + l2 = (float)std::pow(rmax/(nr2>0?nr2:1e-6),2), + a = l1*u*u + l2*v*v, + b = u*v*(l1-l2), + c = l1*v*v + l2*u*u; + const int + yb = (int)std::sqrt(a*rmax*rmax/(a*c - b*b)), + tymin = y0 - yb - 1, + tymax = y0 + yb + 1, + ymin = tymin<0?0:tymin, + ymax = tymax>=height()?height()-1:tymax; + int oxmin = 0, oxmax = 0; + bool first_line = true; + for (int y = ymin; y<=ymax; ++y) { + const float + Y = y - y0 + (y0?(float)std::sqrt(delta)/a:0.0f, + bY = b*Y/a, + fxmin = x0 - 0.5f - bY - sdelta, + fxmax = x0 + 0.5f - bY + sdelta; + const int xmin = (int)fxmin, xmax = (int)fxmax; + if (!pattern) cimg_draw_scanline(xmin,xmax,y,color,opacity,1); + else { + if (first_line) { + if (y0-yb>=0) cimg_draw_scanline(xmin,xmax,y,color,opacity,1); + else draw_point(xmin,y,color,opacity).draw_point(xmax,y,color,opacity); + first_line = false; + } else { + if (xmin + CImg& draw_circle(const int x0, const int y0, int radius, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_circle(): Specified color is (null).", + cimg_instance); + cimg_init_scanline(color,opacity); + if (radius<0 || x0-radius>=width() || y0+radius<0 || y0-radius>=height()) return *this; + if (y0>=0 && y0=0) { + const int x1 = x0-x, x2 = x0+x, y1 = y0-y, y2 = y0+y; + if (y1>=0 && y1=0 && y2=0 && y1=0 && y2 + CImg& draw_circle(const int x0, const int y0, int radius, + const tc *const color, const float opacity, + const unsigned int pattern) { + cimg::unused(pattern); + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_circle(): Specified color is (null).", + cimg_instance); + if (radius<0 || x0-radius>=width() || y0+radius<0 || y0-radius>=height()) return *this; + if (!radius) return draw_point(x0,y0,color,opacity); + draw_point(x0-radius,y0,color,opacity).draw_point(x0+radius,y0,color,opacity). + draw_point(x0,y0-radius,color,opacity).draw_point(x0,y0+radius,color,opacity); + if (radius==1) return *this; + for (int f = 1-radius, ddFx = 0, ddFy = -(radius<<1), x = 0, y = radius; x=0) { f+=(ddFy+=2); --y; } + ++x; ++(f+=(ddFx+=2)); + if (x!=y+1) { + const int x1 = x0-y, x2 = x0+y, y1 = y0-x, y2 = y0+x, x3 = x0-x, x4 = x0+x, y3 = y0-y, y4 = y0+y; + draw_point(x1,y1,color,opacity).draw_point(x1,y2,color,opacity). + draw_point(x2,y1,color,opacity).draw_point(x2,y2,color,opacity); + if (x!=y) + draw_point(x3,y3,color,opacity).draw_point(x4,y4,color,opacity). + draw_point(x4,y3,color,opacity).draw_point(x3,y4,color,opacity); + } + } + return *this; + } + + //! Draw an image. + /** + \param sprite Sprite image. + \param x0 X-coordinate of the sprite position. + \param y0 Y-coordinate of the sprite position. + \param z0 Z-coordinate of the sprite position. + \param c0 C-coordinate of the sprite position. + \param opacity Drawing opacity. + **/ + template + CImg& draw_image(const int x0, const int y0, const int z0, const int c0, + const CImg& sprite, const float opacity=1) { + if (is_empty() || !sprite) return *this; + if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity); + if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared()) + return assign(sprite,false); + const bool bx = (x0<0), by = (y0<0), bz = (z0<0), bc = (c0<0); + const int + lX = sprite.width() - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0) + (bx?x0:0), + lY = sprite.height() - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0) + (by?y0:0), + lZ = sprite.depth() - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0) + (bz?z0:0), + lC = sprite.spectrum() - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0) + (bc?c0:0); + const t + *ptrs = sprite._data - + (bx?x0:0) - + (by?y0*sprite.width():0) - + (bz?z0*sprite.width()*sprite.height():0) - + (bc?c0*sprite.width()*sprite.height()*sprite.depth():0); + const unsigned long + offX = (unsigned long)_width - lX, + soffX = (unsigned long)sprite._width - lX, + offY = (unsigned long)_width*(_height - lY), + soffY = (unsigned long)sprite._width*(sprite._height - lY), + offZ = (unsigned long)_width*_height*(_depth - lZ), + soffZ = (unsigned long)sprite._width*sprite._height*(sprite._depth - lZ); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (lX>0 && lY>0 && lZ>0 && lC>0) { + T *ptrd = data(x0<0?0:x0,y0<0?0:y0,z0<0?0:z0,c0<0?0:c0); + for (int v = 0; v=1) for (int x = 0; x& draw_image(const int x0, const int y0, const int z0, const int c0, + const CImg& sprite, const float opacity=1) { + if (is_empty() || !sprite) return *this; + if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity); + if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared()) + return assign(sprite,false); + const bool bx = (x0<0), by = (y0<0), bz = (z0<0), bc = (c0<0); + const int + lX = sprite.width() - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0) + (bx?x0:0), + lY = sprite.height() - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0) + (by?y0:0), + lZ = sprite.depth() - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0) + (bz?z0:0), + lC = sprite.spectrum() - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0) + (bc?c0:0); + const T + *ptrs = sprite._data - + (bx?x0:0) - + (by?y0*sprite.width():0) - + (bz?z0*sprite.width()*sprite.height():0) - + (bc?c0*sprite.width()*sprite.height()*sprite.depth():0); + const unsigned long + offX = (unsigned long)_width - lX, + soffX = (unsigned long)sprite._width - lX, + offY = (unsigned long)_width*(_height - lY), + soffY = (unsigned long)sprite._width*(sprite._height - lY), + offZ = (unsigned long)_width*_height*(_depth - lZ), + soffZ = (unsigned long)sprite._width*sprite._height*(sprite._depth - lZ), + slX = lX*sizeof(T); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + if (lX>0 && lY>0 && lZ>0 && lC>0) { + T *ptrd = data(x0<0?0:x0,y0<0?0:y0,z0<0?0:z0,c0<0?0:c0); + for (int v = 0; v=1) + for (int y = 0; y + CImg& draw_image(const int x0, const int y0, const int z0, + const CImg& sprite, const float opacity=1) { + return draw_image(x0,y0,z0,0,sprite,opacity); + } + + //! Draw an image \overloading. + template + CImg& draw_image(const int x0, const int y0, + const CImg& sprite, const float opacity=1) { + return draw_image(x0,y0,0,sprite,opacity); + } + + //! Draw an image \overloading. + template + CImg& draw_image(const int x0, + const CImg& sprite, const float opacity=1) { + return draw_image(x0,0,sprite,opacity); + } + + //! Draw an image \overloading. + template + CImg& draw_image(const CImg& sprite, const float opacity=1) { + return draw_image(0,sprite,opacity); + } + + //! Draw a masked image. + /** + \param sprite Sprite image. + \param mask Mask image. + \param x0 X-coordinate of the sprite position in the image instance. + \param y0 Y-coordinate of the sprite position in the image instance. + \param z0 Z-coordinate of the sprite position in the image instance. + \param c0 C-coordinate of the sprite position in the image instance. + \param mask_max_value Maximum pixel value of the mask image \c mask. + \param opacity Drawing opacity. + \note + - Pixel values of \c mask set the opacity of the corresponding pixels in \c sprite. + - Dimensions along x,y and z of \p sprite and \p mask must be the same. + **/ + template + CImg& draw_image(const int x0, const int y0, const int z0, const int c0, + const CImg& sprite, const CImg& mask, const float opacity=1, + const float mask_max_value=1) { + if (is_empty() || !sprite || !mask) return *this; + if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,mask,opacity,mask_max_value); + if (is_overlapped(mask)) return draw_image(x0,y0,z0,c0,sprite,+mask,opacity,mask_max_value); + if (mask._width!=sprite._width || mask._height!=sprite._height || mask._depth!=sprite._depth) + throw CImgArgumentException(_cimg_instance + "draw_image(): Sprite (%u,%u,%u,%u,%p) and mask (%u,%u,%u,%u,%p) have " + "incompatible dimensions.", + cimg_instance, + sprite._width,sprite._height,sprite._depth,sprite._spectrum,sprite._data, + mask._width,mask._height,mask._depth,mask._spectrum,mask._data); + + const bool bx = (x0<0), by = (y0<0), bz = (z0<0), bc = (c0<0); + const int + lX = sprite.width() - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0) + (bx?x0:0), + lY = sprite.height() - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0) + (by?y0:0), + lZ = sprite.depth() - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0) + (bz?z0:0), + lC = sprite.spectrum() - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0) + (bc?c0:0); + const int + coff = -(bx?x0:0)-(by?y0*mask.width():0)-(bz?z0*mask.width()*mask.height():0)- + (bc?c0*mask.width()*mask.height()*mask.depth():0), + ssize = mask.width()*mask.height()*mask.depth()*mask.spectrum(); + const ti *ptrs = sprite._data + coff; + const tm *ptrm = mask._data + coff; + const unsigned long + offX = (unsigned long)_width - lX, + soffX = (unsigned long)sprite._width - lX, + offY = (unsigned long)_width*(_height - lY), + soffY = (unsigned long)sprite._width*(sprite._height - lY), + offZ = (unsigned long)_width*_height*(_depth - lZ), + soffZ = (unsigned long)sprite._width*sprite._height*(sprite._depth - lZ); + if (lX>0 && lY>0 && lZ>0 && lC>0) { + T *ptrd = data(x0<0?0:x0,y0<0?0:y0,z0<0?0:z0,c0<0?0:c0); + for (int c = 0; c + CImg& draw_image(const int x0, const int y0, const int z0, + const CImg& sprite, const CImg& mask, const float opacity=1, + const float mask_max_value=1) { + return draw_image(x0,y0,z0,0,sprite,mask,opacity,mask_max_value); + } + + //! Draw a image \overloading. + template + CImg& draw_image(const int x0, const int y0, + const CImg& sprite, const CImg& mask, const float opacity=1, + const float mask_max_value=1) { + return draw_image(x0,y0,0,sprite,mask,opacity,mask_max_value); + } + + //! Draw a image \overloading. + template + CImg& draw_image(const int x0, + const CImg& sprite, const CImg& mask, const float opacity=1, + const float mask_max_value=1) { + return draw_image(x0,0,sprite,mask,opacity,mask_max_value); + } + + //! Draw an image. + template + CImg& draw_image(const CImg& sprite, const CImg& mask, const float opacity=1, + const float mask_max_value=1) { + return draw_image(0,sprite,mask,opacity,mask_max_value); + } + + //! Draw a text string. + /** + \param x0 X-coordinate of the text in the image instance. + \param y0 Y-coordinate of the text in the image instance. + \param text Format of the text ('printf'-style format string). + \param foreground_color Pointer to \c spectrum() consecutive values, defining the foreground drawing color. + \param background_color Pointer to \c spectrum() consecutive values, defining the background drawing color. + \param opacity Drawing opacity. + \param font Font used for drawing text. + **/ + template + CImg& draw_text(const int x0, const int y0, + const char *const text, + const tc1 *const foreground_color, const tc2 *const background_color, + const float opacity, const CImgList& font, ...) { + if (!font) return *this; + char tmp[2048] = { 0 }; std::va_list ap; va_start(ap,font); + cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); + return _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,false); + } + + //! Draw a text string \overloading. + /** + \note A transparent background is used for the text. + **/ + template + CImg& draw_text(const int x0, const int y0, + const char *const text, + const tc *const foreground_color, const int, + const float opacity, const CImgList& font, ...) { + if (!font) return *this; + char tmp[2048] = { 0 }; std::va_list ap; va_start(ap,font); + cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); + return _draw_text(x0,y0,tmp,foreground_color,(tc*)0,opacity,font,false); + } + + //! Draw a text string \overloading. + /** + \note A transparent foreground is used for the text. + **/ + template + CImg& draw_text(const int x0, const int y0, + const char *const text, + const int, const tc *const background_color, + const float opacity, const CImgList& font, ...) { + if (!font) return *this; + char tmp[2048] = { 0 }; std::va_list ap; va_start(ap,font); + cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); + return _draw_text(x0,y0,tmp,(tc*)0,background_color,opacity,font,false); + } + + //! Draw a text string \overloading. + /** + \param x0 X-coordinate of the text in the image instance. + \param y0 Y-coordinate of the text in the image instance. + \param text Format of the text ('printf'-style format string). + \param foreground_color Array of spectrum() values of type \c T, + defining the foreground color (0 means 'transparent'). + \param background_color Array of spectrum() values of type \c T, + defining the background color (0 means 'transparent'). + \param opacity Drawing opacity. + \param font_height Height of the text font (exact match for 13,23,53,103, interpolated otherwise). + **/ + template + CImg& draw_text(const int x0, const int y0, + const char *const text, + const tc1 *const foreground_color, const tc2 *const background_color, + const float opacity=1, const unsigned int font_height=13, ...) { + if (!font_height) return *this; + char tmp[2048] = { 0 }; + std::va_list ap; va_start(ap,font_height); cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); + const CImgList& font = CImgList::font(font_height,true); + _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,true); + return *this; + } + + //! Draw a text string \overloading. + template + CImg& draw_text(const int x0, const int y0, + const char *const text, + const tc *const foreground_color, const int background_color=0, + const float opacity=1, const unsigned int font_height=13, ...) { + if (!font_height) return *this; + cimg::unused(background_color); + char tmp[2048] = { 0 }; + std::va_list ap; va_start(ap,font_height); cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); + return draw_text(x0,y0,"%s",foreground_color,(const tc*)0,opacity,font_height,tmp); + } + + //! Draw a text string \overloading. + template + CImg& draw_text(const int x0, const int y0, + const char *const text, + const int, const tc *const background_color, + const float opacity=1, const unsigned int font_height=13, ...) { + if (!font_height) return *this; + char tmp[2048] = { 0 }; + std::va_list ap; va_start(ap,font_height); cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); + return draw_text(x0,y0,"%s",(tc*)0,background_color,opacity,font_height,tmp); + } + + template + CImg& _draw_text(const int x0, const int y0, + const char *const text, + const tc1 *const foreground_color, const tc2 *const background_color, + const float opacity, const CImgList& font, + const bool is_native_font) { + if (!text) return *this; + if (!font) + throw CImgArgumentException(_cimg_instance + "draw_text(): Empty specified font.", + cimg_instance); + + const unsigned int text_length = (unsigned int)std::strlen(text); + const bool _is_empty = is_empty(); + if (_is_empty) { + // If needed, pre-compute necessary size of the image + int x = 0, y = 0, w = 0; + unsigned char c = 0; + for (unsigned int i = 0; iw) w = x; x = 0; break; + case '\t' : x+=4*font[' ']._width; break; + default : if (cw) w=x; + y+=font[0]._height; + } + assign(x0+w,y0+y,1,is_native_font?1:font[0]._spectrum,0); + } + + int x = x0, y = y0; + for (unsigned int i = 0; i letter = font[c]; + if (letter) { + if (is_native_font && _spectrum>letter._spectrum) letter.resize(-100,-100,1,_spectrum,0,2); + const unsigned int cmin = cimg::min(_spectrum,letter._spectrum); + if (foreground_color) + for (unsigned int c = 0; c + CImg& draw_quiver(const CImg& flow, + const t2 *const color, const float opacity=1, + const unsigned int sampling=25, const float factor=-20, + const bool is_arrow=true, const unsigned int pattern=~0U) { + return draw_quiver(flow,CImg(color,_spectrum,1,1,1,true),opacity,sampling,factor,is_arrow,pattern); + } + + //! Draw a 2d vector field, using a field of colors. + /** + \param flow Image of 2d vectors used as input data. + \param color Image of spectrum()-D vectors corresponding to the color of each arrow. + \param opacity Opacity of the drawing. + \param sampling Length (in pixels) between each arrow. + \param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length). + \param is_arrow Tells if arrows must be drawn, instead of oriented segments. + \param pattern Used pattern to draw lines. + \note Clipping is supported. + **/ + template + CImg& draw_quiver(const CImg& flow, + const CImg& color, const float opacity=1, + const unsigned int sampling=25, const float factor=-20, + const bool is_arrow=true, const unsigned int pattern=~0U) { + if (is_empty()) return *this; + if (!flow || flow._spectrum!=2) + throw CImgArgumentException(_cimg_instance + "draw_quiver(): Invalid dimensions of specified flow (%u,%u,%u,%u,%p).", + cimg_instance, + flow._width,flow._height,flow._depth,flow._spectrum,flow._data); + if (sampling<=0) + throw CImgArgumentException(_cimg_instance + "draw_quiver(): Invalid sampling value %g " + "(should be >0)", + cimg_instance, + sampling); + const bool colorfield = (color._width==flow._width && color._height==flow._height && + color._depth==1 && color._spectrum==_spectrum); + if (is_overlapped(flow)) return draw_quiver(+flow,color,opacity,sampling,factor,is_arrow,pattern); + float vmax,fact; + if (factor<=0) { + float m, M = (float)flow.get_norm(2).max_min(m); + vmax = (float)cimg::max(cimg::abs(m),cimg::abs(M)); + if (!vmax) vmax = 1; + fact = -factor; + } else { fact = factor; vmax = 1; } + + for (unsigned int y = sampling/2; y<_height; y+=sampling) + for (unsigned int x = sampling/2; x<_width; x+=sampling) { + const unsigned int X = x*flow._width/_width, Y = y*flow._height/_height; + float u = (float)flow(X,Y,0,0)*fact/vmax, v = (float)flow(X,Y,0,1)*fact/vmax; + if (is_arrow) { + const int xx = x+(int)u, yy = y+(int)v; + if (colorfield) draw_arrow(x,y,xx,yy,color.get_vector_at(X,Y)._data,opacity,45,sampling/5.0f,pattern); + else draw_arrow(x,y,xx,yy,color._data,opacity,45,sampling/5.0f,pattern); + } else { + if (colorfield) + draw_line((int)(x-0.5*u),(int)(y-0.5*v),(int)(x+0.5*u),(int)(y+0.5*v), + color.get_vector_at(X,Y)._data,opacity,pattern); + else draw_line((int)(x-0.5*u),(int)(y-0.5*v),(int)(x+0.5*u),(int)(y+0.5*v), + color._data,opacity,pattern); + } + } + return *this; + } + + //! Draw a labeled horizontal axis. + /** + \param values_x Values along the horizontal axis. + \param y Y-coordinate of the horizontal axis in the image instance. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \param pattern Drawing pattern. + \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise). + \param allow_zero Enable/disable the drawing of label '0' if found. + **/ + template + CImg& draw_axis(const CImg& values_x, const int y, + const tc *const color, const float opacity=1, + const unsigned int pattern=~0U, const unsigned int font_height=13, + const bool allow_zero=true) { + if (is_empty()) return *this; + const int yt = (y+3+font_height)<_height?(y+3):(y-2-font_height); + const int siz = (int)values_x.size()-1; + char txt[32] = { 0 }; + CImg label; + if (siz<=0) { // Degenerated case. + draw_line(0,y,_width-1,y,color,opacity,pattern); + if (!siz) { + cimg_snprintf(txt,sizeof(txt),"%g",(double)*values_x); + label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height); + const int + _xt = (width() - label.width())/2, + xt = _xt<3?3:_xt+label.width()>=width()-2?width()-3-label.width():_xt; + draw_point(width()/2,y-1,color,opacity).draw_point(width()/2,y+1,color,opacity); + if (allow_zero || txt[0]!='0' || txt[1]!=0) + draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); + } + } else { // Regular case. + if (values_x[0]=width()-2?width()-3-label.width():_xt; + draw_point(xi,y-1,color,opacity).draw_point(xi,y+1,color,opacity); + if (allow_zero || txt[0]!='0' || txt[1]!=0) + draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); + } + } + return *this; + } + + //! Draw a labeled vertical axis. + /** + \param x X-coordinate of the vertical axis in the image instance. + \param values_y Values along the Y-axis. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \param pattern Drawing pattern. + \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise). + \param allow_zero Enable/disable the drawing of label '0' if found. + **/ + template + CImg& draw_axis(const int x, const CImg& values_y, + const tc *const color, const float opacity=1, + const unsigned int pattern=~0U, const unsigned int font_height=13, + const bool allow_zero=true) { + if (is_empty()) return *this; + int siz = (int)values_y.size()-1; + char txt[32] = { 0 }; + CImg label; + if (siz<=0) { // Degenerated case. + draw_line(x,0,x,_height-1,color,opacity,pattern); + if (!siz) { + cimg_snprintf(txt,sizeof(txt),"%g",(double)*values_y); + label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height); + const int + _yt = (height() - label.height())/2, + yt = _yt<0?0:_yt+label.height()>=height()?height()-1-label.height():_yt, + _xt = x - 2 - label.width(), + xt = _xt>=0?_xt:x+3; + draw_point(x-1,height()/2,color,opacity).draw_point(x+1,height()/2,color,opacity); + if (allow_zero || txt[0]!='0' || txt[1]!=0) + draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); + } + } else { // Regular case. + if (values_y[0]=height()?height()-1-label.height():_yt, + _xt = x - 2 - label.width(), + xt = _xt>=0?_xt:x+3; + draw_point(x-1,yi,color,opacity).draw_point(x+1,yi,color,opacity); + if (allow_zero || txt[0]!='0' || txt[1]!=0) + draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); + } + } + return *this; + } + + //! Draw labeled horizontal and vertical axes. + /** + \param values_x Values along the X-axis. + \param values_y Values along the Y-axis. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \param pattern_x Drawing pattern for the X-axis. + \param pattern_y Drawing pattern for the Y-axis. + \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise). + \param allow_zero Enable/disable the drawing of label '0' if found. + **/ + template + CImg& draw_axes(const CImg& values_x, const CImg& values_y, + const tc *const color, const float opacity=1, + const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U, + const unsigned int font_height=13, const bool allow_zero=true) { + if (is_empty()) return *this; + const CImg nvalues_x(values_x._data,values_x.size(),1,1,1,true); + const int sizx = (int)values_x.size()-1, wm1 = width()-1; + if (sizx>=0) { + float ox = (float)*nvalues_x; + for (unsigned int x = sizx?1:0; x<_width; ++x) { + const float nx = (float)nvalues_x._linear_atX((float)x*sizx/wm1); + if (nx*ox<=0) { draw_axis(nx==0?x:x-1,values_y,color,opacity,pattern_y,font_height,allow_zero); break; } + ox = nx; + } + } + const CImg nvalues_y(values_y._data,values_y.size(),1,1,1,true); + const int sizy = (int)values_y.size()-1, hm1 = height()-1; + if (sizy>0) { + float oy = (float)nvalues_y[0]; + for (unsigned int y = sizy?1:0; y<_height; ++y) { + const float ny = (float)nvalues_y._linear_atX((float)y*sizy/hm1); + if (ny*oy<=0) { draw_axis(values_x,ny==0?y:y-1,color,opacity,pattern_x,font_height,allow_zero); break; } + oy = ny; + } + } + return *this; + } + + //! Draw labeled horizontal and vertical axes \overloading. + template + CImg& draw_axes(const float x0, const float x1, const float y0, const float y1, + const tc *const color, const float opacity=1, + const int subdivisionx=-60, const int subdivisiony=-60, + const float precisionx=0, const float precisiony=0, + const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U, + const unsigned int font_height=13) { + if (is_empty()) return *this; + const bool allow_zero = (x0*x1>0) || (y0*y1>0); + const float + dx = cimg::abs(x1-x0), dy = cimg::abs(y1-y0), + px = dx<=0?1:precisionx==0?(float)std::pow(10.0,(int)std::log10(dx)-2.0):precisionx, + py = dy<=0?1:precisiony==0?(float)std::pow(10.0,(int)std::log10(dy)-2.0):precisiony; + if (x0!=x1 && y0!=y1) + draw_axes(CImg::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1).round(px), + CImg::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1).round(py), + color,opacity,pattern_x,pattern_y,font_height,allow_zero); + else if (x0==x1 && y0!=y1) + draw_axis((int)x0,CImg::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1).round(py), + color,opacity,pattern_y,font_height); + else if (x0!=x1 && y0==y1) + draw_axis(CImg::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1).round(px),(int)y0, + color,opacity,pattern_x,font_height); + return *this; + } + + //! Draw 2d grid. + /** + \param values_x X-coordinates of the vertical lines. + \param values_y Y-coordinates of the horizontal lines. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + \param pattern_x Drawing pattern for vertical lines. + \param pattern_y Drawing pattern for horizontal lines. + **/ + template + CImg& draw_grid(const CImg& values_x, const CImg& values_y, + const tc *const color, const float opacity=1, + const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) { + if (is_empty()) return *this; + if (values_x) cimg_foroff(values_x,x) { + const int xi = (int)values_x[x]; + if (xi>=0 && xi=0 && yi + CImg& draw_grid(const float delta_x, const float delta_y, + const float offsetx, const float offsety, + const bool invertx, const bool inverty, + const tc *const color, const float opacity=1, + const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) { + if (is_empty()) return *this; + CImg seqx, seqy; + if (delta_x!=0) { + const float dx = delta_x>0?delta_x:_width*-delta_x/100; + const unsigned int nx = (unsigned int)(_width/dx); + seqx = CImg::sequence(1+nx,0,(unsigned int)(dx*nx)); + if (offsetx) cimg_foroff(seqx,x) seqx(x) = (unsigned int)cimg::mod(seqx(x)+offsetx,(float)_width); + if (invertx) cimg_foroff(seqx,x) seqx(x) = _width - 1 - seqx(x); + } + if (delta_y!=0) { + const float dy = delta_y>0?delta_y:_height*-delta_y/100; + const unsigned int ny = (unsigned int)(_height/dy); + seqy = CImg::sequence(1+ny,0,(unsigned int)(dy*ny)); + if (offsety) cimg_foroff(seqy,y) seqy(y) = (unsigned int)cimg::mod(seqy(y)+offsety,(float)_height); + if (inverty) cimg_foroff(seqy,y) seqy(y) = _height - 1 - seqy(y); + } + return draw_grid(seqx,seqy,color,opacity,pattern_x,pattern_y); + } + + //! Draw 1d graph. + /** + \param data Image containing the graph values I = f(x). + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + + \param plot_type Define the type of the plot: + - 0 = No plot. + - 1 = Plot using segments. + - 2 = Plot using cubic splines. + - 3 = Plot with bars. + \param vertex_type Define the type of points: + - 0 = No points. + - 1 = Point. + - 2 = Straight cross. + - 3 = Diagonal cross. + - 4 = Filled circle. + - 5 = Outlined circle. + - 6 = Square. + - 7 = Diamond. + \param ymin Lower bound of the y-range. + \param ymax Upper bound of the y-range. + \param pattern Drawing pattern. + \note + - if \c ymin==ymax==0, the y-range is computed automatically from the input samples. + **/ + template + CImg& draw_graph(const CImg& data, + const tc *const color, const float opacity=1, + const unsigned int plot_type=1, const int vertex_type=1, + const double ymin=0, const double ymax=0, const unsigned int pattern=~0U) { + if (is_empty() || _height<=1) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_graph(): Specified color is (null).", + cimg_instance); + + // Create shaded colors for displaying bar plots. + CImg color1, color2; + if (plot_type==3) { + color1.assign(_spectrum); color2.assign(_spectrum); + cimg_forC(*this,c) { + color1[c] = (tc)cimg::min((float)cimg::type::max(),color[c]*1.2f); + color2[c] = (tc)(color[c]*0.4f); + } + } + + // Compute min/max and normalization factors. + const unsigned long + siz = data.size(), + _siz1 = siz - (plot_type!=3?1:0), + siz1 = _siz1?_siz1:1; + const unsigned int + _width1 = _width - (plot_type!=3?1:0), + width1 = _width1?_width1:1; + double m = ymin, M = ymax; + if (ymin==ymax) m = (double)data.max_min(M); + if (m==M) { --m; ++M; } + const float ca = (float)(M-m)/(_height-1); + bool init_hatch = true; + + // Draw graph edges + switch (plot_type%4) { + case 1 : { // Segments + int oX = 0, oY = (int)((data[0]-m)/ca); + const float fx = (float)_width1/siz1; + if (siz==1) { + const int Y = (int)((*data-m)/ca); + draw_line(0,Y,width()-1,Y,color,opacity,pattern); + } else for (unsigned long off = 1; off ndata(data._data,siz,1,1,1,true); + int oY = (int)((data[0]-m)/ca); + cimg_forX(*this,x) { + const int Y = (int)((ndata._cubic_atX((float)x*siz1/width1)-m)/ca); + if (x>0) draw_line(x,oY,x+1,Y,color,opacity,pattern,init_hatch); + init_hatch = false; + oY = Y; + } + } break; + case 3 : { // Bars + const int Y0 = (int)(-m/ca); + const float fx = (float)_width/(siz-1); + int oX = 0; + cimg_foroff(data,off) { + const int + X = (int)((off+1)*fx), + Y = (int)((data[off]-m)/ca); + draw_rectangle(oX,Y0,X,Y,color,opacity). + draw_line(oX,Y,oX,Y0,color2.data(),opacity). + draw_line(oX,Y0,X,Y0,Y<=Y0?color2.data():color1.data(),opacity). + draw_line(X,Y,X,Y0,color1.data(),opacity). + draw_line(oX,Y,X,Y,Y<=Y0?color1.data():color2.data(),opacity); + oX = X+1; + } + } break; + default : break; // No edges + } + + // Draw graph points + const unsigned int wb2 = plot_type==3?_width1/(2*siz):0; + const float fx = (float)_width1/siz1; + switch (vertex_type%8) { + case 1 : { // Point + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_point(X,Y,color,opacity); + } + } break; + case 2 : { // Straight Cross + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_line(X-3,Y,X+3,Y,color,opacity).draw_line(X,Y-3,X,Y+3,color,opacity); + } + } break; + case 3 : { // Diagonal Cross + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_line(X-3,Y-3,X+3,Y+3,color,opacity).draw_line(X-3,Y+3,X+3,Y-3,color,opacity); + } + } break; + case 4 : { // Filled Circle + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_circle(X,Y,3,color,opacity); + } + } break; + case 5 : { // Outlined circle + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_circle(X,Y,3,color,opacity,0U); + } + } break; + case 6 : { // Square + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_rectangle(X-3,Y-3,X+3,Y+3,color,opacity,~0U); + } + } break; + case 7 : { // Diamond + cimg_foroff(data,off) { + const int + X = (int)(off*fx) + wb2, + Y = (int)((data[off]-m)/ca); + draw_line(X,Y-4,X+4,Y,color,opacity). + draw_line(X+4,Y,X,Y+4,color,opacity). + draw_line(X,Y+4,X-4,Y,color,opacity). + draw_line(X-4,Y,X,Y-4,color,opacity); + } + } break; + default : break; // No points + } + return *this; + } + + //! Draw filled 3d region with the flood fill algorithm. + /** + \param x X-coordinate of the starting point of the region to fill. + \param y Y-coordinate of the starting point of the region to fill. + \param z Z-coordinate of the starting point of the region to fill. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param[out] region Image that will contain the mask of the filled region mask, as an output. + \param sigma Tolerance concerning neighborhood values. + \param opacity Opacity of the drawing. + \param is_high_connexity Tells if 8-connexity must be used (only for 2d images). + \return \c region is initialized with the binary mask of the filled region. + **/ + template + CImg& draw_fill(const int x, const int y, const int z, + const tc *const color, const float opacity, + CImg& region, const float sigma=0, + const bool is_high_connexity=false) { + +#define _cimg_draw_fill_test(x,y,z,res) if (region(x,y,z)) res = false; else { \ + res = true; \ + const T *reference_col = reference_color._data + _spectrum, *ptrs = data(x,y,z) + siz; \ + for (unsigned int i = _spectrum; res && i; --i) { ptrs-=whd; res = (cimg::abs(*ptrs - *(--reference_col))<=sigma); } \ + region(x,y,z) = (t)(res?1:noregion); \ +} + +#define _cimg_draw_fill_set(x,y,z) { \ + const tc *col = color; \ + T *ptrd = data(x,y,z); \ + if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } \ + else cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; } \ +} + +#define _cimg_draw_fill_insert(x,y,z) { \ + if (posr1>=remaining._height) remaining.resize(3,remaining._height<<1,1,1,0); \ + unsigned int *ptrr = remaining.data(0,posr1); \ + *(ptrr++) = x; *(ptrr++) = y; *(ptrr++) = z; ++posr1; \ +} + +#define _cimg_draw_fill_test_neighbor(x,y,z,cond) if (cond) { \ + const unsigned int tx = x, ty = y, tz = z; \ + _cimg_draw_fill_test(tx,ty,tz,res); if (res) _cimg_draw_fill_insert(tx,ty,tz); \ +} + + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_fill(): Specified color is (null).", + cimg_instance); + + region.assign(_width,_height,_depth,1,(t)0); + if (x>=0 && x=0 && y=0 && z1); + const CImg reference_color = get_vector_at(x,y,z); + CImg remaining(3,512,1,1,0); + remaining(0,0) = x; remaining(1,0) = y; remaining(2,0) = z; + unsigned int posr0 = 0, posr1 = 1; + region(x,y,z) = (t)1; + const t noregion = ((t)1==(t)2)?(t)0:(t)(-1); + if (is_3d) do { // 3d version of the filling algorithm + const unsigned int *pcurr = remaining.data(0,posr0++), xc = *(pcurr++), yc = *(pcurr++), zc = *(pcurr++); + if (posr0>=512) { remaining.shift(0,-(int)posr0); posr1-=posr0; posr0 = 0; } + bool cont, res; + unsigned int nxc = xc; + do { // X-backward + _cimg_draw_fill_set(nxc,yc,zc); + _cimg_draw_fill_test_neighbor(nxc,yc-1,zc,yc!=0); + _cimg_draw_fill_test_neighbor(nxc,yc+1,zc,ycposr0); + else do { // 2d version of the filling algorithm + const unsigned int *pcurr = remaining.data(0,posr0++), xc = *(pcurr++), yc = *(pcurr++); + if (posr0>=512) { remaining.shift(0,-(int)posr0); posr1-=posr0; posr0 = 0; } + bool cont, res; + unsigned int nxc = xc; + do { // X-backward + _cimg_draw_fill_set(nxc,yc,0); + _cimg_draw_fill_test_neighbor(nxc,yc-1,0,yc!=0); + _cimg_draw_fill_test_neighbor(nxc,yc+1,0,ycposr0); + if (noregion) cimg_for(region,ptrd,t) if (*ptrd==noregion) *ptrd = (t)0; + } + return *this; + } + + //! Draw filled 3d region with the flood fill algorithm \simplification. + template + CImg& draw_fill(const int x, const int y, const int z, + const tc *const color, const float opacity=1, + const float sigma=0, const bool is_high_connexity=false) { + CImg tmp; + return draw_fill(x,y,z,color,opacity,tmp,sigma,is_high_connexity); + } + + //! Draw filled 2d region with the flood fill algorithm \simplification. + template + CImg& draw_fill(const int x, const int y, + const tc *const color, const float opacity=1, + const float sigma=0, const bool is_high_connexity=false) { + CImg tmp; + return draw_fill(x,y,0,color,opacity,tmp,sigma,is_high_connexity); + } + + //! Draw a random plasma texture. + /** + \param alpha Alpha-parameter. + \param beta Beta-parameter. + \param scale Scale-parameter. + \note Use the mid-point algorithm to render. + **/ + CImg& draw_plasma(const float alpha=1, const float beta=0, const unsigned int scale=8) { + if (is_empty()) return *this; + const int w = width(), h = height(); + const Tfloat m = (Tfloat)cimg::type::min(), M = (Tfloat)cimg::type::max(); + cimg_forZC(*this,z,c) { + CImg ref = get_shared_slice(z,c); + for (int delta = 1<1; delta>>=1) { + const int delta2 = delta>>1; + const float r = alpha*delta + beta; + + // Square step. + for (int y0 = 0; y0M?M:val); + } + + // Diamond steps. + for (int y = -delta2; yM?M:val); + } + for (int y0 = 0; y0M?M:val); + } + for (int y = -delta2; yM?M:val); + } + } + } + return *this; + } + + //! Draw a quadratic Mandelbrot or Julia 2d fractal. + /** + \param x0 X-coordinate of the upper-left pixel. + \param y0 Y-coordinate of the upper-left pixel. + \param x1 X-coordinate of the lower-right pixel. + \param y1 Y-coordinate of the lower-right pixel. + \param colormap Colormap. + \param opacity Drawing opacity. + \param z0r Real part of the upper-left fractal vertex. + \param z0i Imaginary part of the upper-left fractal vertex. + \param z1r Real part of the lower-right fractal vertex. + \param z1i Imaginary part of the lower-right fractal vertex. + \param iteration_max Maximum number of iterations for each estimated point. + \param is_normalized_iteration Tells if iterations are normalized. + \param is_julia_set Tells if the Mandelbrot or Julia set is rendered. + \param param_r Real part of the Julia set parameter. + \param param_i Imaginary part of the Julia set parameter. + \note Fractal rendering is done by the Escape Time Algorithm. + **/ + template + CImg& draw_mandelbrot(const int x0, const int y0, const int x1, const int y1, + const CImg& colormap, const float opacity=1, + const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2, + const unsigned int iteration_max=255, + const bool is_normalized_iteration=false, + const bool is_julia_set=false, + const double param_r=0, const double param_i=0) { + if (is_empty()) return *this; + CImg palette; + if (colormap) palette.assign(colormap._data,colormap.size()/colormap._spectrum,1,1,colormap._spectrum,true); + if (palette && palette._spectrum!=_spectrum) + throw CImgArgumentException(_cimg_instance + "draw_mandelbrot(): Instance and specified colormap (%u,%u,%u,%u,%p) have " + "incompatible dimensions.", + cimg_instance, + colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data); + + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), ln2 = (float)std::log(2.0); + const int + _x0 = x0<0?0:x0>=width()?width()-1:x0, + _y0 = y0<0?0:y0>=height()?height()-1:y0, + _x1 = x1<0?1:x1>=width()?width()-1:x1, + _y1 = y1<0?1:y1>=height()?height()-1:y1; +#ifdef cimg_use_openmp +#pragma omp parallel for collapse(2) if ((1+_x1-_x0)*(1+_y1-_y0)>=2048) +#endif + for (int q = _y0; q<=_y1; ++q) + for (int p = _x0; p<=_x1; ++p) { + unsigned int iteration = 0; + const double x = z0r + p*(z1r-z0r)/_width, y = z0i + q*(z1i-z0i)/_height; + double zr, zi, cr, ci; + if (is_julia_set) { zr = x; zi = y; cr = param_r; ci = param_i; } + else { zr = param_r; zi = param_i; cr = x; ci = y; } + for (iteration=1; zr*zr + zi*zi<=4 && iteration<=iteration_max; ++iteration) { + const double temp = zr*zr - zi*zi + cr; + zi = 2*zr*zi + ci; + zr = temp; + } + if (iteration>iteration_max) { + if (palette) { + if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette(0,c); + else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(0,c)*nopacity + (*this)(p,q,0,c)*copacity); + } else { + if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)0; + else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)((*this)(p,q,0,c)*copacity); + } + } else if (is_normalized_iteration) { + const float + normz = (float)cimg::abs(zr*zr+zi*zi), + niteration = (float)(iteration + 1 - std::log(std::log(normz))/ln2); + if (palette) { + if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._linear_atX(niteration,c); + else cimg_forC(*this,c) + (*this)(p,q,0,c) = (T)(palette._linear_atX(niteration,c)*nopacity + (*this)(p,q,0,c)*copacity); + } else { + if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)niteration; + else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(niteration*nopacity + (*this)(p,q,0,c)*copacity); + } + } else { + if (palette) { + if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._atX(iteration,c); + else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(iteration,c)*nopacity + (*this)(p,q,0,c)*copacity); + } else { + if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)iteration; + else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(iteration*nopacity + (*this)(p,q,0,c)*copacity); + } + } + } + return *this; + } + + //! Draw a quadratic Mandelbrot or Julia 2d fractal \overloading. + template + CImg& draw_mandelbrot(const CImg& colormap, const float opacity=1, + const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2, + const unsigned int iteration_max=255, + const bool is_normalized_iteration=false, + const bool is_julia_set=false, + const double param_r=0, const double param_i=0) { + return draw_mandelbrot(0,0,_width-1,_height-1,colormap,opacity, + z0r,z0i,z1r,z1i,iteration_max,is_normalized_iteration,is_julia_set,param_r,param_i); + } + + //! Draw a 1d gaussian function. + /** + \param xc X-coordinate of the gaussian center. + \param sigma Standard variation of the gaussian distribution. + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + **/ + template + CImg& draw_gaussian(const float xc, const float sigma, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_gaussian(): Specified color is (null).", + cimg_instance); + const float sigma2 = 2*sigma*sigma, nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const unsigned long whd = (unsigned long)_width*_height*_depth; + const tc *col = color; + cimg_forX(*this,x) { + const float dx = (x - xc), val = (float)std::exp(-dx*dx/sigma2); + T *ptrd = data(x,0,0,0); + if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; } + else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; } + col-=_spectrum; + } + return *this; + } + + //! Draw a 2d gaussian function. + /** + \param xc X-coordinate of the gaussian center. + \param yc Y-coordinate of the gaussian center. + \param tensor Covariance matrix (must be 2x2). + \param color Pointer to \c spectrum() consecutive values, defining the drawing color. + \param opacity Drawing opacity. + **/ + template + CImg& draw_gaussian(const float xc, const float yc, const CImg& tensor, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + if (tensor._width!=2 || tensor._height!=2 || tensor._depth!=1 || tensor._spectrum!=1) + throw CImgArgumentException(_cimg_instance + "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 2x2 matrix.", + cimg_instance, + tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data); + if (!color) + throw CImgArgumentException(_cimg_instance + "draw_gaussian(): Specified color is (null).", + cimg_instance); + typedef typename CImg::Tfloat tfloat; + const CImg invT = tensor.get_invert(), invT2 = (invT*invT)/(-2.0); + const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = invT2(1,1); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const unsigned long whd = (unsigned long)_width*_height*_depth; + const tc *col = color; + float dy = -yc; + cimg_forY(*this,y) { + float dx = -xc; + cimg_forX(*this,x) { + const float val = (float)std::exp(a*dx*dx + b*dx*dy + c*dy*dy); + T *ptrd = data(x,y,0,0); + if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; } + else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; } + col-=_spectrum; + ++dx; + } + ++dy; + } + return *this; + } + + //! Draw a 2d gaussian function \overloading. + template + CImg& draw_gaussian(const int xc, const int yc, const float r1, const float r2, const float ru, const float rv, + const tc *const color, const float opacity=1) { + const double + a = r1*ru*ru + r2*rv*rv, + b = (r1-r2)*ru*rv, + c = r1*rv*rv + r2*ru*ru; + const CImg tensor(2,2,1,1, a,b,b,c); + return draw_gaussian(xc,yc,tensor,color,opacity); + } + + //! Draw a 2d gaussian function \overloading. + template + CImg& draw_gaussian(const float xc, const float yc, const float sigma, + const tc *const color, const float opacity=1) { + return draw_gaussian(xc,yc,CImg::diagonal(sigma,sigma),color,opacity); + } + + //! Draw a 3d gaussian function \overloading. + template + CImg& draw_gaussian(const float xc, const float yc, const float zc, const CImg& tensor, + const tc *const color, const float opacity=1) { + if (is_empty()) return *this; + typedef typename CImg::Tfloat tfloat; + if (tensor._width!=3 || tensor._height!=3 || tensor._depth!=1 || tensor._spectrum!=1) + throw CImgArgumentException(_cimg_instance + "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 3x3 matrix.", + cimg_instance, + tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data); + + const CImg invT = tensor.get_invert(), invT2 = (invT*invT)/(-2.0); + const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = 2*invT2(2,0), d = invT2(1,1), e = 2*invT2(2,1), f = invT2(2,2); + const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); + const unsigned long whd = (unsigned long)_width*_height*_depth; + const tc *col = color; + cimg_forXYZ(*this,x,y,z) { + const float + dx = (x - xc), dy = (y - yc), dz = (z - zc), + val = (float)std::exp(a*dx*dx + b*dx*dy + c*dx*dz + d*dy*dy + e*dy*dz + f*dz*dz); + T *ptrd = data(x,y,z,0); + if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; } + else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; } + col-=_spectrum; + } + return *this; + } + + //! Draw a 3d gaussian function \overloading. + template + CImg& draw_gaussian(const float xc, const float yc, const float zc, const float sigma, + const tc *const color, const float opacity=1) { + return draw_gaussian(xc,yc,zc,CImg::diagonal(sigma,sigma,sigma),color,opacity); + } + + //! Draw a 3d object. + /** + \param x0 X-coordinate of the 3d object position + \param y0 Y-coordinate of the 3d object position + \param z0 Z-coordinate of the 3d object position + \param vertices Image Nx3 describing 3d point coordinates + \param primitives List of P primitives + \param colors List of P color (or textures) + \param opacities Image or list of P opacities + \param render_type d Render type (0=Points, 1=Lines, 2=Faces (no light), 3=Faces (flat), 4=Faces(Gouraud) + \param is_double_sided Tells if object faces have two sides or are oriented. + \param focale length of the focale (0 for parallel projection) + \param lightx X-coordinate of the light + \param lighty Y-coordinate of the light + \param lightz Z-coordinate of the light + \param specular_lightness Amount of specular light. + \param specular_shininess Shininess of the object + **/ + template + CImg& draw_object3d(const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImg& opacities, + const unsigned int render_type=4, + const bool is_double_sided=false, const float focale=700, + const float lightx=0, const float lighty=0, const float lightz=-5e8, + const float specular_lightness=0.2f, const float specular_shininess=0.1f) { + return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type, + is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,CImg::empty()); + } + + //! Draw a 3d object \simplification. + template + CImg& draw_object3d(const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImg& opacities, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + CImg& zbuffer) { + return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,1); + } + +#ifdef cimg_use_board + template + CImg& draw_object3d(LibBoard::Board& board, + const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImg& opacities, + const unsigned int render_type=4, + const bool is_double_sided=false, const float focale=700, + const float lightx=0, const float lighty=0, const float lightz=-5e8, + const float specular_lightness=0.2f, const float specular_shininess=0.1f) { + return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type, + is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,CImg::empty()); + } + + template + CImg& draw_object3d(LibBoard::Board& board, + const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImg& opacities, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + CImg& zbuffer) { + return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,1); + } +#endif + + //! Draw a 3d object \simplification. + template + CImg& draw_object3d(const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImgList& opacities, + const unsigned int render_type=4, + const bool is_double_sided=false, const float focale=700, + const float lightx=0, const float lighty=0, const float lightz=-5e8, + const float specular_lightness=0.2f, const float specular_shininess=0.1f) { + return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type, + is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,CImg::empty()); + } + + //! Draw a 3d object \simplification. + template + CImg& draw_object3d(const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImgList& opacities, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + CImg& zbuffer) { + return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,1); + } + +#ifdef cimg_use_board + template + CImg& draw_object3d(LibBoard::Board& board, + const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImgList& opacities, + const unsigned int render_type=4, + const bool is_double_sided=false, const float focale=700, + const float lightx=0, const float lighty=0, const float lightz=-5e8, + const float specular_lightness=0.2f, const float specular_shininess=0.1f) { + return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type, + is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,CImg::empty()); + } + + template + CImg& draw_object3d(LibBoard::Board& board, + const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, const CImgList& opacities, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + CImg& zbuffer) { + return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,1); + } +#endif + + //! Draw a 3d object \simplification. + template + CImg& draw_object3d(const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, + const unsigned int render_type=4, + const bool is_double_sided=false, const float focale=700, + const float lightx=0, const float lighty=0, const float lightz=-5e8, + const float specular_lightness=0.2f, const float specular_shininess=0.1f) { + return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,CImg::empty()); + } + + //! Draw a 3d object \simplification. + template + CImg& draw_object3d(const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + CImg& zbuffer) { + return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,zbuffer); + } + +#ifdef cimg_use_board + template + CImg& draw_object3d(LibBoard::Board& board, + const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, + const unsigned int render_type=4, + const bool is_double_sided=false, const float focale=700, + const float lightx=0, const float lighty=0, const float lightz=-5e8, + const float specular_lightness=0.2f, const float specular_shininess=0.1f) { + return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,CImg::empty()); + } + + template + CImg& draw_object3d(LibBoard::Board& board, + const float x0, const float y0, const float z0, + const CImg& vertices, const CImgList& primitives, + const CImgList& colors, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + CImg& zbuffer) { + return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), + render_type,is_double_sided,focale,lightx,lighty,lightz, + specular_lightness,specular_shininess,zbuffer); + } +#endif + + template + static float __draw_object3d(const CImgList& opacities, const unsigned int n_primitive, CImg& opacity) { + if (n_primitive>=opacities._width || opacities[n_primitive].is_empty()) { opacity.assign(); return 1; } + if (opacities[n_primitive].size()==1) { opacity.assign(); return opacities(n_primitive,0); } + opacity.assign(opacities[n_primitive],true); + return 1.0f; + } + + template + static float __draw_object3d(const CImg& opacities, const unsigned int n_primitive, CImg& opacity) { + opacity.assign(); + return n_primitive>=opacities._width?1.0f:(float)opacities[n_primitive]; + } + + template + CImg& _draw_object3d(void *const pboard, CImg& zbuffer, + const float X, const float Y, const float Z, + const CImg& vertices, + const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const unsigned int render_type, + const bool is_double_sided, const float focale, + const float lightx, const float lighty, const float lightz, + const float specular_lightness, const float specular_shininess, + const float sprite_scale) { + typedef typename cimg::superset2::type tpfloat; + if (is_empty() || !vertices || !primitives) return *this; + char error_message[1024] = { 0 }; + if (!vertices.is_object3d(primitives,colors,opacities,false,error_message)) + throw CImgArgumentException(_cimg_instance + "draw_object3d(): Invalid specified 3d object (%u,%u) (%s).", + cimg_instance,vertices._width,primitives._width,error_message); + if (render_type==5) cimg::mutex(10); // Static variable used in this case, breaks thread-safety. + +#ifndef cimg_use_board + if (pboard) return *this; +#endif + const float + nspec = 1 - (specular_lightness<0.0f?0.0f:(specular_lightness>1.0f?1.0f:specular_lightness)), + nspec2 = 1 + (specular_shininess<0.0f?0.0f:specular_shininess), + nsl1 = (nspec2 - 1)/cimg::sqr(nspec - 1), + nsl2 = 1 - 2*nsl1*nspec, + nsl3 = nspec2 - nsl1 - nsl2; + + // Create light texture for phong-like rendering. + CImg light_texture; + if (render_type==5) { + if (colors._width>primitives._width) { + static CImg default_light_texture; + static const tc *lptr = 0; + static tc ref_values[64] = { 0 }; + const CImg& img = colors.back(); + bool is_same_texture = (lptr==img._data); + if (is_same_texture) + for (unsigned int r = 0, j = 0; j<8; ++j) + for (unsigned int i = 0; i<8; ++i) + if (ref_values[r++]!=img(i*img._width/9,j*img._height/9,0,(i+j)%img._spectrum)) { + is_same_texture = false; break; + } + if (!is_same_texture || default_light_texture._spectrum<_spectrum) { + (default_light_texture.assign(img,false)/=255).resize(-100,-100,1,_spectrum); + lptr = colors.back().data(); + for (unsigned int r = 0, j = 0; j<8; ++j) + for (unsigned int i = 0; i<8; ++i) + ref_values[r++] = img(i*img._width/9,j*img._height/9,0,(i+j)%img._spectrum); + } + light_texture.assign(default_light_texture,true); + } else { + static CImg default_light_texture; + static float olightx = 0, olighty = 0, olightz = 0, ospecular_shininess = 0; + if (!default_light_texture || + lightx!=olightx || lighty!=olighty || lightz!=olightz || + specular_shininess!=ospecular_shininess || default_light_texture._spectrum<_spectrum) { + default_light_texture.assign(512,512); + const float + dlx = lightx - X, + dly = lighty - Y, + dlz = lightz - Z, + nl = (float)std::sqrt(dlx*dlx + dly*dly + dlz*dlz), + nlx = (default_light_texture._width - 1)/2*(1 + dlx/nl), + nly = (default_light_texture._height - 1)/2*(1 + dly/nl), + white[] = { 1 }; + default_light_texture.draw_gaussian(nlx,nly,default_light_texture._width/3.0f,white); + cimg_forXY(default_light_texture,x,y) { + const float factor = default_light_texture(x,y); + if (factor>nspec) default_light_texture(x,y) = cimg::min(2,nsl1*factor*factor + nsl2*factor + nsl3); + } + default_light_texture.resize(-100,-100,1,_spectrum); + olightx = lightx; olighty = lighty; olightz = lightz; ospecular_shininess = specular_shininess; + } + light_texture.assign(default_light_texture,true); + } + } + + // Compute 3d to 2d projection. + CImg projections(vertices._width,2); + tpfloat parallzmin = cimg::type::max(); + const float absfocale = focale?cimg::abs(focale):0; + if (absfocale) { +#ifdef cimg_use_openmp +#pragma omp parallel for if (projections.size()>4096) +#endif + cimg_forX(projections,l) { // Perspective projection + const tpfloat + x = (tpfloat)vertices(l,0), + y = (tpfloat)vertices(l,1), + z = (tpfloat)vertices(l,2); + const tpfloat projectedz = z + Z + absfocale; + projections(l,1) = Y + absfocale*y/projectedz; + projections(l,0) = X + absfocale*x/projectedz; + } + + } else { +#ifdef cimg_use_openmp +#pragma omp parallel for if (projections.size()>4096) +#endif + cimg_forX(projections,l) { // Parallel projection + const tpfloat + x = (tpfloat)vertices(l,0), + y = (tpfloat)vertices(l,1), + z = (tpfloat)vertices(l,2); + if (z visibles(primitives._width,1,1,1,~0U); + CImg zrange(primitives._width); + const tpfloat zmin = absfocale?(tpfloat)(1.5f - absfocale):cimg::type::min(); + +#ifdef cimg_use_openmp +#pragma omp parallel for if (primitives.size()>4096) +#endif + cimglist_for(primitives,l) { + const CImg& primitive = primitives[l]; + switch (primitive.size()) { + case 1 : { // Point + const unsigned int i0 = (unsigned int)primitive(0); + const tpfloat z0 = Z + vertices(i0,2); + if (z0>zmin) { + visibles(l) = (unsigned int)l; + zrange(l) = z0; + } + } break; + case 5 : { // Sphere + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1); + const tpfloat + Xc = 0.5f*((float)vertices(i0,0) + (float)vertices(i1,0)), + Yc = 0.5f*((float)vertices(i0,1) + (float)vertices(i1,1)), + Zc = 0.5f*((float)vertices(i0,2) + (float)vertices(i1,2)), + _zc = Z + Zc, + zc = _zc + _focale, + xc = X + Xc*(absfocale?absfocale/zc:1), + yc = Y + Yc*(absfocale?absfocale/zc:1), + radius = 0.5f*std::sqrt(cimg::sqr(vertices(i1,0) - vertices(i0,0)) + + cimg::sqr(vertices(i1,1) - vertices(i0,1)) + + cimg::sqr(vertices(i1,2) - vertices(i0,2)))*(absfocale?absfocale/zc:1), + xm = xc - radius, + ym = yc - radius, + xM = xc + radius, + yM = yc + radius; + if (xM>=0 && xm<_width && yM>=0 && ym<_height && _zc>zmin) { + visibles(l) = (unsigned int)l; + zrange(l) = _zc; + } + } break; + case 2 : // Segment + case 6 : { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1); + const tpfloat + x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2), + x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2); + tpfloat xm, xM, ym, yM; + if (x0=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin) { + visibles(l) = (unsigned int)l; + zrange(l) = (z0 + z1)/2; + } + } break; + case 3 : // Triangle + case 9 : { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1), + i2 = (unsigned int)primitive(2); + const tpfloat + x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2), + x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2), + x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2); + tpfloat xm, xM, ym, yM; + if (x0xM) xM = x2; + if (y0yM) yM = y2; + if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin) { + const tpfloat d = (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0); + if (is_double_sided || d<0) { + visibles(l) = (unsigned int)l; + zrange(l) = (z0 + z1 + z2)/3; + } + } + } break; + case 4 : // Rectangle + case 12 : { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1), + i2 = (unsigned int)primitive(2), + i3 = (unsigned int)primitive(3); + const tpfloat + x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2), + x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2), + x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2), + x3 = projections(i3,0), y3 = projections(i3,1), z3 = Z + vertices(i3,2); + tpfloat xm, xM, ym, yM; + if (x0xM) xM = x2; + if (x3xM) xM = x3; + if (y0yM) yM = y2; + if (y3yM) yM = y3; + if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin) { + const float d = (x1 - x0)*(y2 - y0) - (x2 - x0)*(y1 - y0); + if (is_double_sided || d<0) { + visibles(l) = (unsigned int)l; + zrange(l) = (z0 + z1 + z2 + z3)/4; + } + } + } break; + default : + throw CImgArgumentException(_cimg_instance + "draw_object3d(): Invalid primitive[%u] with size %u " + "(should have size 1,2,3,4,5,6,9 or 12).", + cimg_instance, + l,primitive.size()); + } + } + + // Sort only visibles primitives. + unsigned int *p_visibles = visibles._data; + float *p_zrange = zrange._data; + const float *ptrz = p_zrange; + cimg_for(visibles,ptr,unsigned int) { + if (*ptr!=~0U) { *(p_visibles++) = *ptr; *(p_zrange++) = *ptrz; } + ++ptrz; + } + const unsigned int nb_visibles = p_zrange - zrange._data; + if (!nb_visibles) return *this; + CImg permutations; + CImg(zrange._data,nb_visibles,1,1,1,true).sort(permutations,false); + + // Compute light properties + CImg lightprops; + switch (render_type) { + case 3 : { // Flat Shading + lightprops.assign(nb_visibles); +#ifdef cimg_use_openmp +#pragma omp parallel for if (nb_visibles>4096) +#endif + cimg_forX(lightprops,l) { + const CImg& primitive = primitives(visibles(permutations(l))); + const unsigned int psize = primitive.size(); + if (psize==3 || psize==4 || psize==9 || psize==12) { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1), + i2 = (unsigned int)primitive(2); + const tpfloat + x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2), + x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2), + x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2), + dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0, + dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0, + nx = dy1*dz2 - dz1*dy2, + ny = dz1*dx2 - dx1*dz2, + nz = dx1*dy2 - dy1*dx2, + norm = (tpfloat)std::sqrt(1e-5f + nx*nx + ny*ny + nz*nz), + lx = X + (x0 + x1 + x2)/3 - lightx, + ly = Y + (y0 + y1 + y2)/3 - lighty, + lz = Z + (z0 + z1 + z2)/3 - lightz, + nl = (tpfloat)std::sqrt(1e-5f + lx*lx + ly*ly + lz*lz), + factor = cimg::max(cimg::abs(-lx*nx-ly*ny-lz*nz)/(norm*nl),0); + lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3); + } else lightprops[l] = 1; + } + } break; + + case 4 : // Gouraud Shading + case 5 : { // Phong-Shading + CImg vertices_normals(vertices._width,3,1,1,0); +#ifdef cimg_use_openmp +#pragma omp parallel for if (nb_visibles>4096) +#endif + for (unsigned int l = 0; l& primitive = primitives[visibles(l)]; + const unsigned int psize = primitive.size(); + const bool + triangle_flag = (psize==3) || (psize==9), + rectangle_flag = (psize==4) || (psize==12); + if (triangle_flag || rectangle_flag) { + const unsigned int + i0 = (unsigned int)primitive(0), + i1 = (unsigned int)primitive(1), + i2 = (unsigned int)primitive(2), + i3 = rectangle_flag?(unsigned int)primitive(3):0; + const tpfloat + x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2), + x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2), + x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2), + dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0, + dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0, + nnx = dy1*dz2 - dz1*dy2, + nny = dz1*dx2 - dx1*dz2, + nnz = dx1*dy2 - dy1*dx2, + norm = (tpfloat)(1e-5f + std::sqrt(nnx*nnx + nny*nny + nnz*nnz)), + nx = nnx/norm, + ny = nny/norm, + nz = nnz/norm; + vertices_normals(i0,0)+=nx; vertices_normals(i0,1)+=ny; vertices_normals(i0,2)+=nz; + vertices_normals(i1,0)+=nx; vertices_normals(i1,1)+=ny; vertices_normals(i1,2)+=nz; + vertices_normals(i2,0)+=nx; vertices_normals(i2,1)+=ny; vertices_normals(i2,2)+=nz; + if (rectangle_flag) { vertices_normals(i3,0)+=nx; vertices_normals(i3,1)+=ny; vertices_normals(i3,2)+=nz; } + } + } + + if (is_double_sided) cimg_forX(vertices_normals,p) if (vertices_normals(p,2)>0) { + vertices_normals(p,0) = -vertices_normals(p,0); + vertices_normals(p,1) = -vertices_normals(p,1); + vertices_normals(p,2) = -vertices_normals(p,2); + } + + if (render_type==4) { + lightprops.assign(vertices._width); +#ifdef cimg_use_openmp +#pragma omp parallel for if (nb_visibles>4096) +#endif + cimg_forX(lightprops,l) { + const tpfloat + nx = vertices_normals(l,0), + ny = vertices_normals(l,1), + nz = vertices_normals(l,2), + norm = (tpfloat)std::sqrt(1e-5f + nx*nx + ny*ny + nz*nz), + lx = X + vertices(l,0) - lightx, + ly = Y + vertices(l,1) - lighty, + lz = Z + vertices(l,2) - lightz, + nl = (tpfloat)std::sqrt(1e-5f + lx*lx + ly*ly + lz*lz), + factor = cimg::max((-lx*nx-ly*ny-lz*nz)/(norm*nl),0); + lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3); + } + } else { + const unsigned int + lw2 = light_texture._width/2 - 1, + lh2 = light_texture._height/2 - 1; + lightprops.assign(vertices._width,2); +#ifdef cimg_use_openmp +#pragma omp parallel for if (nb_visibles>4096) +#endif + cimg_forX(lightprops,l) { + const tpfloat + nx = vertices_normals(l,0), + ny = vertices_normals(l,1), + nz = vertices_normals(l,2), + norm = (tpfloat)std::sqrt(1e-5f + nx*nx + ny*ny + nz*nz), + nnx = nx/norm, + nny = ny/norm; + lightprops(l,0) = lw2*(1 + nnx); + lightprops(l,1) = lh2*(1 + nny); + } + } + } break; + } + + // Draw visible primitives + const CImg default_color(1,_spectrum,1,1,(tc)200); + typedef typename to::value_type _to; + CImg<_to> _opacity; + + for (unsigned int l = 0; l& primitive = primitives[n_primitive]; + const CImg + &__color = n_primitive(), + _color = (__color && __color.size()!=_spectrum && __color._spectrum<_spectrum)? + __color.get_resize(-100,-100,-100,_spectrum,0):CImg(), + &color = _color?_color:(__color?__color:default_color); + const tc *const pcolor = color._data; + const float opacity = __draw_object3d(opacities,n_primitive,_opacity); + +#ifdef cimg_use_board + LibBoard::Board &board = *(LibBoard::Board*)pboard; +#endif + + switch (primitive.size()) { + case 1 : { // Colored point or sprite + const unsigned int n0 = (unsigned int)primitive[0]; + const int x0 = (int)projections(n0,0), y0 = (int)projections(n0,1); + + if (_opacity.is_empty()) { // Scalar opacity. + + if (color.size()==_spectrum) { // Colored point. + draw_point(x0,y0,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.fillCircle((float)x0,height()-(float)y0,0); + } +#endif + } else { // Sprite. + const tpfloat z = Z + vertices(n0,2); + const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1); + const unsigned int + _sw = (unsigned int)(color._width*factor), + _sh = (unsigned int)(color._height*factor), + sw = _sw?_sw:1, sh = _sh?_sh:1; + const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2; + if (sw<=3*_width/2 && sh<=3*_height/2 && + (nx0+(int)sw/2>=0 || nx0-(int)sw/2=0 || ny0-(int)sh/2 + _sprite = (sw!=color._width || sh!=color._height)? + color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg(), + &sprite = _sprite?_sprite:color; + draw_image(nx0,ny0,sprite,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128); + board.setFillColor(LibBoard::Color::None); + board.drawRectangle((float)nx0,height()-(float)ny0,sw,sh); + } +#endif + } + } + } else { // Opacity mask. + const tpfloat z = Z + vertices(n0,2); + const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1); + const unsigned int + _sw = (unsigned int)(cimg::max(color._width,_opacity._width)*factor), + _sh = (unsigned int)(cimg::max(color._height,_opacity._height)*factor), + sw = _sw?_sw:1, sh = _sh?_sh:1; + const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2; + if (sw<=3*_width/2 && sh<=3*_height/2 && + (nx0+(int)sw/2>=0 || nx0-(int)sw/2=0 || ny0-(int)sh/2 + _sprite = (sw!=color._width || sh!=color._height)? + color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg(), + &sprite = _sprite?_sprite:color; + const CImg<_to> + _nopacity = (sw!=_opacity._width || sh!=_opacity._height)? + _opacity.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<_to>(), + &nopacity = _nopacity?_nopacity:_opacity; + draw_image(nx0,ny0,sprite,nopacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128); + board.setFillColor(LibBoard::Color::None); + board.drawRectangle((float)nx0,height()-(float)ny0,sw,sh); + } +#endif + } + } + } break; + case 2 : { // Colored line + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1]; + const int + x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), + x1 = (int)projections(n1,0), y1 = (int)projections(n1,1); + const float + z0 = vertices(n0,2) + Z + _focale, + z1 = vertices(n1,2) + Z + _focale; + if (render_type) { + if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity); + else draw_line(x0,y0,x1,y1,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.drawLine((float)x0,height()-(float)y0,x1,height()-(float)y1); + } +#endif + } else { + draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.drawCircle((float)x0,height()-(float)y0,0); + board.drawCircle((float)x1,height()-(float)y1,0); + } +#endif + } + } break; + case 5 : { // Colored sphere + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1], + is_wireframe = (unsigned int)primitive[2]; + const float + Xc = 0.5f*((float)vertices(n0,0) + (float)vertices(n1,0)), + Yc = 0.5f*((float)vertices(n0,1) + (float)vertices(n1,1)), + Zc = 0.5f*((float)vertices(n0,2) + (float)vertices(n1,2)), + zc = Z + Zc + _focale, + xc = X + Xc*(absfocale?absfocale/zc:1), + yc = Y + Yc*(absfocale?absfocale/zc:1), + radius = 0.5f*std::sqrt(cimg::sqr(vertices(n1,0) - vertices(n0,0)) + + cimg::sqr(vertices(n1,1) - vertices(n0,1)) + + cimg::sqr(vertices(n1,2) - vertices(n0,2)))*(absfocale?absfocale/zc:1); + switch (render_type) { + case 0 : + draw_point((int)xc,(int)yc,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.fillCircle(xc,height()-yc,0); + } +#endif + break; + case 1 : + draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.setFillColor(LibBoard::Color::None); + board.drawCircle(xc,height()-yc,radius); + } +#endif + break; + default : + if (is_wireframe) draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U); + else draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + if (!is_wireframe) board.fillCircle(xc,height()-yc,radius); + else { + board.setFillColor(LibBoard::Color::None); + board.drawCircle(xc,height()-yc,radius); + } + } +#endif + break; + } + } break; + case 6 : { // Textured line + if (!__color) + throw CImgArgumentException(_cimg_instance + "draw_object3d(): Undefined texture for line primitive [%u].", + cimg_instance,n_primitive); + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1], + tx0 = (unsigned int)primitive[2], + ty0 = (unsigned int)primitive[3], + tx1 = (unsigned int)primitive[4], + ty1 = (unsigned int)primitive[5]; + const int + x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), + x1 = (int)projections(n1,0), y1 = (int)projections(n1,1); + const float + z0 = vertices(n0,2) + Z + _focale, + z1 = vertices(n1,2) + Z + _focale; + if (render_type) { + if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity); + else draw_line(x0,y0,x1,y1,color,tx0,ty0,tx1,ty1,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); + } +#endif + } else { + draw_point(x0,y0,color.get_vector_at(tx0,ty0)._data,opacity). + draw_point(x1,y1,color.get_vector_at(tx1,ty1)._data,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.drawCircle((float)x0,height()-(float)y0,0); + board.drawCircle((float)x1,height()-(float)y1,0); + } +#endif + } + } break; + case 3 : { // Colored triangle + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1], + n2 = (unsigned int)primitive[2]; + const int + x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), + x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), + x2 = (int)projections(n2,0), y2 = (int)projections(n2,1); + const float + z0 = vertices(n0,2) + Z + _focale, + z1 = vertices(n1,2) + Z + _focale, + z2 = vertices(n2,2) + Z + _focale; + switch (render_type) { + case 0 : + draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity).draw_point(x2,y2,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.drawCircle((float)x0,height()-(float)y0,0); + board.drawCircle((float)x1,height()-(float)y1,0); + board.drawCircle((float)x2,height()-(float)y2,0); + } +#endif + break; + case 1 : + if (zbuffer) + draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x0,y0,z0,x2,y2,z2,pcolor,opacity). + draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity); + else + draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x0,y0,x2,y2,pcolor,opacity). + draw_line(x1,y1,x2,y2,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); + board.drawLine((float)x0,height()-(float)y0,(float)x2,height()-(float)y2); + board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); + } +#endif + break; + case 2 : + if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity); + else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + } +#endif + break; + case 3 : + if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l)); + else _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l)); +#ifdef cimg_use_board + if (pboard) { + const float lp = cimg::min(lightprops(l),1); + board.setPenColorRGBi((unsigned char)(color[0]*lp), + (unsigned char)(color[1]*lp), + (unsigned char)(color[2]*lp), + (unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + } +#endif + break; + case 4 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor, + lightprops(n0),lightprops(n1),lightprops(n2),opacity); + else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,lightprops(n0),lightprops(n1),lightprops(n2),opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi((unsigned char)(color[0]), + (unsigned char)(color[1]), + (unsigned char)(color[2]), + (unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprops(n0), + (float)x1,height()-(float)y1,lightprops(n1), + (float)x2,height()-(float)y2,lightprops(n2)); + } +#endif + break; + case 5 : { + const unsigned int + lx0 = (unsigned int)lightprops(n0,0), ly0 = (unsigned int)lightprops(n0,1), + lx1 = (unsigned int)lightprops(n1,0), ly1 = (unsigned int)lightprops(n1,1), + lx2 = (unsigned int)lightprops(n2,0), ly2 = (unsigned int)lightprops(n2,1); + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity); + else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity); +#ifdef cimg_use_board + if (pboard) { + const float + l0 = light_texture((int)(light_texture.width()/2*(1+lightprops(n0,0))), + (int)(light_texture.height()/2*(1+lightprops(n0,1)))), + l1 = light_texture((int)(light_texture.width()/2*(1+lightprops(n1,0))), + (int)(light_texture.height()/2*(1+lightprops(n1,1)))), + l2 = light_texture((int)(light_texture.width()/2*(1+lightprops(n2,0))), + (int)(light_texture.height()/2*(1+lightprops(n2,1)))); + board.setPenColorRGBi((unsigned char)(color[0]), + (unsigned char)(color[1]), + (unsigned char)(color[2]), + (unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, + (float)x1,height()-(float)y1,l1, + (float)x2,height()-(float)y2,l2); + } +#endif + } break; + } + } break; + case 4 : { // Colored rectangle + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1], + n2 = (unsigned int)primitive[2], + n3 = (unsigned int)primitive[3]; + const int + x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), + x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), + x2 = (int)projections(n2,0), y2 = (int)projections(n2,1), + x3 = (int)projections(n3,0), y3 = (int)projections(n3,1); + const float + z0 = vertices(n0,2) + Z + _focale, + z1 = vertices(n1,2) + Z + _focale, + z2 = vertices(n2,2) + Z + _focale, + z3 = vertices(n3,2) + Z + _focale; + + switch (render_type) { + case 0 : + draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity). + draw_point(x2,y2,pcolor,opacity).draw_point(x3,y3,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.drawCircle((float)x0,height()-(float)y0,0); + board.drawCircle((float)x1,height()-(float)y1,0); + board.drawCircle((float)x2,height()-(float)y2,0); + board.drawCircle((float)x3,height()-(float)y3,0); + } +#endif + break; + case 1 : + if (zbuffer) + draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity). + draw_line(zbuffer,x2,y2,z2,x3,y3,z3,pcolor,opacity).draw_line(zbuffer,x3,y3,z3,x0,y0,z0,pcolor,opacity); + else + draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x1,y1,x2,y2,pcolor,opacity). + draw_line(x2,y2,x3,y3,pcolor,opacity).draw_line(x3,y3,x0,y0,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); + board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); + board.drawLine((float)x2,height()-(float)y2,(float)x3,height()-(float)y3); + board.drawLine((float)x3,height()-(float)y3,(float)x0,height()-(float)y0); + } +#endif + break; + case 2 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity); + else + draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity).draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x2,height()-(float)y2, + (float)x3,height()-(float)y3); + } +#endif + break; + case 3 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l)). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity,lightprops(l)); + else + _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l)). + _draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity,lightprops(l)); +#ifdef cimg_use_board + if (pboard) { + const float lp = cimg::min(lightprops(l),1); + board.setPenColorRGBi((unsigned char)(color[0]*lp), + (unsigned char)(color[1]*lp), + (unsigned char)(color[2]*lp),(unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x2,height()-(float)y2, + (float)x3,height()-(float)y3); + } +#endif + break; + case 4 : { + const float + lightprop0 = lightprops(n0), lightprop1 = lightprops(n1), + lightprop2 = lightprops(n2), lightprop3 = lightprops(n3); + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,lightprop0,lightprop1,lightprop2,opacity). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,lightprop0,lightprop2,lightprop3,opacity); + else + draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,lightprop0,lightprop1,lightprop2,opacity). + draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,lightprop0,lightprop2,lightprop3,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi((unsigned char)(color[0]), + (unsigned char)(color[1]), + (unsigned char)(color[2]), + (unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, + (float)x1,height()-(float)y1,lightprop1, + (float)x2,height()-(float)y2,lightprop2); + board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, + (float)x2,height()-(float)y2,lightprop2, + (float)x3,height()-(float)y3,lightprop3); + } +#endif + } break; + case 5 : { + const unsigned int + lx0 = (unsigned int)lightprops(n0,0), ly0 = (unsigned int)lightprops(n0,1), + lx1 = (unsigned int)lightprops(n1,0), ly1 = (unsigned int)lightprops(n1,1), + lx2 = (unsigned int)lightprops(n2,0), ly2 = (unsigned int)lightprops(n2,1), + lx3 = (unsigned int)lightprops(n3,0), ly3 = (unsigned int)lightprops(n3,1); + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); + else + draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). + draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); +#ifdef cimg_use_board + if (pboard) { + const float + l0 = light_texture((int)(light_texture.width()/2*(1+lx0)), (int)(light_texture.height()/2*(1+ly0))), + l1 = light_texture((int)(light_texture.width()/2*(1+lx1)), (int)(light_texture.height()/2*(1+ly1))), + l2 = light_texture((int)(light_texture.width()/2*(1+lx2)), (int)(light_texture.height()/2*(1+ly2))), + l3 = light_texture((int)(light_texture.width()/2*(1+lx3)), (int)(light_texture.height()/2*(1+ly3))); + board.setPenColorRGBi((unsigned char)(color[0]), + (unsigned char)(color[1]), + (unsigned char)(color[2]), + (unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, + (float)x1,height()-(float)y1,l1, + (float)x2,height()-(float)y2,l2); + board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, + (float)x2,height()-(float)y2,l2, + (float)x3,height()-(float)y3,l3); + } +#endif + } break; + } + } break; + case 9 : { // Textured triangle + if (!__color) + throw CImgArgumentException(_cimg_instance + "draw_object3d(): Undefined texture for triangle primitive [%u].", + cimg_instance,n_primitive); + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1], + n2 = (unsigned int)primitive[2], + tx0 = (unsigned int)primitive[3], + ty0 = (unsigned int)primitive[4], + tx1 = (unsigned int)primitive[5], + ty1 = (unsigned int)primitive[6], + tx2 = (unsigned int)primitive[7], + ty2 = (unsigned int)primitive[8]; + const int + x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), + x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), + x2 = (int)projections(n2,0), y2 = (int)projections(n2,1); + const float + z0 = vertices(n0,2) + Z + _focale, + z1 = vertices(n1,2) + Z + _focale, + z2 = vertices(n2,2) + Z + _focale; + switch (render_type) { + case 0 : + draw_point(x0,y0,color.get_vector_at(tx0,ty0)._data,opacity). + draw_point(x1,y1,color.get_vector_at(tx1,ty1)._data,opacity). + draw_point(x2,y2,color.get_vector_at(tx2,ty2)._data,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.drawCircle((float)x0,height()-(float)y0,0); + board.drawCircle((float)x1,height()-(float)y1,0); + board.drawCircle((float)x2,height()-(float)y2,0); + } +#endif + break; + case 1 : + if (zbuffer) + draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). + draw_line(zbuffer,x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity). + draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity); + else + draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). + draw_line(x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity). + draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); + board.drawLine((float)x0,height()-(float)y0,(float)x2,height()-(float)y2); + board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); + } +#endif + break; + case 2 : + if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity); + else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + } +#endif + break; + case 3 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)); + else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)); +#ifdef cimg_use_board + if (pboard) { + const float lp = cimg::min(lightprops(l),1); + board.setPenColorRGBi((unsigned char)(128*lp), + (unsigned char)(128*lp), + (unsigned char)(128*lp), + (unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + } +#endif + break; + case 4 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, + lightprops(n0),lightprops(n1),lightprops(n2),opacity); + else + draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, + lightprops(n0),lightprops(n1),lightprops(n2),opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprops(n0), + (float)x1,height()-(float)y1,lightprops(n1), + (float)x2,height()-(float)y2,lightprops(n2)); + } +#endif + break; + case 5 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture, + (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1), + (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1), + (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1), + opacity); + else + draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture, + (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1), + (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1), + (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1), + opacity); +#ifdef cimg_use_board + if (pboard) { + const float + l0 = light_texture((int)(light_texture.width()/2*(1+lightprops(n0,0))), + (int)(light_texture.height()/2*(1+lightprops(n0,1)))), + l1 = light_texture((int)(light_texture.width()/2*(1+lightprops(n1,0))), + (int)(light_texture.height()/2*(1+lightprops(n1,1)))), + l2 = light_texture((int)(light_texture.width()/2*(1+lightprops(n2,0))), + (int)(light_texture.height()/2*(1+lightprops(n2,1)))); + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, + (float)x1,height()-(float)y1,l1, + (float)x2,height()-(float)y2,l2); + } +#endif + break; + } + } break; + case 12 : { // Textured quadrangle + if (!__color) + throw CImgArgumentException(_cimg_instance + "draw_object3d(): Undefined texture for quadrangle primitive [%u].", + cimg_instance,n_primitive); + const unsigned int + n0 = (unsigned int)primitive[0], + n1 = (unsigned int)primitive[1], + n2 = (unsigned int)primitive[2], + n3 = (unsigned int)primitive[3], + tx0 = (unsigned int)primitive[4], + ty0 = (unsigned int)primitive[5], + tx1 = (unsigned int)primitive[6], + ty1 = (unsigned int)primitive[7], + tx2 = (unsigned int)primitive[8], + ty2 = (unsigned int)primitive[9], + tx3 = (unsigned int)primitive[10], + ty3 = (unsigned int)primitive[11]; + const int + x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), + x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), + x2 = (int)projections(n2,0), y2 = (int)projections(n2,1), + x3 = (int)projections(n3,0), y3 = (int)projections(n3,1); + const float + z0 = vertices(n0,2) + Z + _focale, + z1 = vertices(n1,2) + Z + _focale, + z2 = vertices(n2,2) + Z + _focale, + z3 = vertices(n3,2) + Z + _focale; + + switch (render_type) { + case 0 : + draw_point(x0,y0,color.get_vector_at(tx0,ty0)._data,opacity). + draw_point(x1,y1,color.get_vector_at(tx1,ty1)._data,opacity). + draw_point(x2,y2,color.get_vector_at(tx2,ty2)._data,opacity). + draw_point(x3,y3,color.get_vector_at(tx3,ty3)._data,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.drawCircle((float)x0,height()-(float)y0,0); + board.drawCircle((float)x1,height()-(float)y1,0); + board.drawCircle((float)x2,height()-(float)y2,0); + board.drawCircle((float)x3,height()-(float)y3,0); + } +#endif + break; + case 1 : + if (zbuffer) + draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). + draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity). + draw_line(zbuffer,x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity). + draw_line(zbuffer,x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity); + else + draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). + draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity). + draw_line(x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity). + draw_line(x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); + board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); + board.drawLine((float)x2,height()-(float)y2,(float)x3,height()-(float)y3); + board.drawLine((float)x3,height()-(float)y3,(float)x0,height()-(float)y0); + } +#endif + break; + case 2 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity); + else + draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity). + draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x2,height()-(float)y2, + (float)x3,height()-(float)y3); + } +#endif + break; + case 3 : + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l)); + else + draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)). + draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l)); +#ifdef cimg_use_board + if (pboard) { + const float lp = cimg::min(lightprops(l),1); + board.setPenColorRGBi((unsigned char)(128*lp), + (unsigned char)(128*lp), + (unsigned char)(128*lp), + (unsigned char)(opacity*255)); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x1,height()-(float)y1, + (float)x2,height()-(float)y2); + board.fillTriangle((float)x0,height()-(float)y0, + (float)x2,height()-(float)y2, + (float)x3,height()-(float)y3); + } +#endif + break; + case 4 : { + const float + lightprop0 = lightprops(n0), lightprop1 = lightprops(n1), + lightprop2 = lightprops(n2), lightprop3 = lightprops(n3); + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, + lightprop0,lightprop1,lightprop2,opacity). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, + lightprop0,lightprop2,lightprop3,opacity); + else + draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, + lightprop0,lightprop1,lightprop2,opacity). + draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, + lightprop0,lightprop2,lightprop3,opacity); +#ifdef cimg_use_board + if (pboard) { + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, + (float)x1,height()-(float)y1,lightprop1, + (float)x2,height()-(float)y2,lightprop2); + board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, + (float)x2,height()-(float)y2,lightprop2, + (float)x3,height()-(float)y3,lightprop3); + } +#endif + } break; + case 5 : { + const unsigned int + lx0 = (unsigned int)lightprops(n0,0), ly0 = (unsigned int)lightprops(n0,1), + lx1 = (unsigned int)lightprops(n1,0), ly1 = (unsigned int)lightprops(n1,1), + lx2 = (unsigned int)lightprops(n2,0), ly2 = (unsigned int)lightprops(n2,1), + lx3 = (unsigned int)lightprops(n3,0), ly3 = (unsigned int)lightprops(n3,1); + if (zbuffer) + draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, + light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). + draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, + light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); + else + draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, + light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). + draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, + light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); +#ifdef cimg_use_board + if (pboard) { + const float + l0 = light_texture((int)(light_texture.width()/2*(1+lx0)), (int)(light_texture.height()/2*(1+ly0))), + l1 = light_texture((int)(light_texture.width()/2*(1+lx1)), (int)(light_texture.height()/2*(1+ly1))), + l2 = light_texture((int)(light_texture.width()/2*(1+lx2)), (int)(light_texture.height()/2*(1+ly2))), + l3 = light_texture((int)(light_texture.width()/2*(1+lx3)), (int)(light_texture.height()/2*(1+ly3))); + board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); + board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, + (float)x1,height()-(float)y1,l1, + (float)x2,height()-(float)y2,l2); + board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, + (float)x2,height()-(float)y2,l2, + (float)x3,height()-(float)y3,l3); + } +#endif + } break; + } + } break; + } + } + + if (render_type==5) cimg::mutex(10,0); + return *this; + } + + //@} + //--------------------------- + // + //! \name Data Input + //@{ + //--------------------------- + + //! Launch simple interface to select a shape from an image. + /** + \param disp Display window to use. + \param feature_type Type of feature to select. Can be { 0=point | 1=line | 2=rectangle | 3=ellipse }. + \param XYZ Pointer to 3 values X,Y,Z which tells about the projection point coordinates, for volumetric images. + **/ + CImg& select(CImgDisplay &disp, + const unsigned int feature_type=2, unsigned int *const XYZ=0) { + return get_select(disp,feature_type,XYZ).move_to(*this); + } + + //! Simple interface to select a shape from an image \overloading. + CImg& select(const char *const title, + const unsigned int feature_type=2, unsigned int *const XYZ=0) { + return get_select(title,feature_type,XYZ).move_to(*this); + } + + //! Simple interface to select a shape from an image \newinstance. + CImg get_select(CImgDisplay &disp, + const unsigned int feature_type=2, unsigned int *const XYZ=0) const { + return _get_select(disp,0,feature_type,XYZ,0,0,0,true,false); + } + + //! Simple interface to select a shape from an image \newinstance. + CImg get_select(const char *const title, + const unsigned int feature_type=2, unsigned int *const XYZ=0) const { + CImgDisplay disp; + return _get_select(disp,title,feature_type,XYZ,0,0,0,true,false); + } + + CImg _get_select(CImgDisplay &disp, const char *const title, + const unsigned int feature_type, unsigned int *const XYZ, + const int origX, const int origY, const int origZ, + const bool reset_view3d, + const bool force_display_z_coord) const { + if (is_empty()) return CImg(1,feature_type==0?3:6,1,1,-1); + if (!disp) { + disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1); + if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum); + } else if (title) disp.set_title("%s",title); + + const unsigned int old_normalization = disp.normalization(); + bool old_is_resized = disp.is_resized(); + disp._normalization = 0; + disp.show().set_key(0).set_wheel().show_mouse(); + + unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 }; + + int area = 0, starting_area = 0, clicked_area = 0, phase = 0, + X0 = (int)((XYZ?XYZ[0]:(_width-1)/2)%_width), + Y0 = (int)((XYZ?XYZ[1]:(_height-1)/2)%_height), + Z0 = (int)((XYZ?XYZ[2]:(_depth-1)/2)%_depth), + X1 =-1, Y1 = -1, Z1 = -1, + X3d = -1, Y3d = -1, + oX3d = X3d, oY3d = -1, + omx = -1, omy = -1; + float X = -1, Y = -1, Z = -1; + unsigned int old_button = 0, key = 0; + + bool shape_selected = false, text_down = false, visible_cursor = true; + static CImg pose3d; + static bool is_view3d = false, is_axes = true; + if (reset_view3d) { pose3d.assign(); is_view3d = false; } + CImg points3d, opacities3d, sel_opacities3d; + CImgList primitives3d, sel_primitives3d; + CImgList colors3d, sel_colors3d; + CImg visu, visu0, view3d; + char text[1024] = { 0 }; + + while (!key && !disp.is_closed() && !shape_selected) { + + // Handle mouse motion and selection + int + mx = disp.mouse_x(), + my = disp.mouse_y(); + + const float + mX = mx<0?-1.0f:(float)mx*(width()+(depth()>1?depth():0))/disp.width(), + mY = my<0?-1.0f:(float)my*(height()+(depth()>1?depth():0))/disp.height(); + + area = 0; + if (mX>=0 && mY>=0 && mX=0 && mX=height()) { area = 2; X = mX; Z = mY - _height; Y = (float)(phase?Y1:Y0); } + if (mY>=0 && mX>=width() && mY=width() && mY>=height()) area = 4; + if (disp.button()) { if (!clicked_area) clicked_area = area; } else clicked_area = 0; + + switch (key = disp.key()) { +#if cimg_OS!=2 + case cimg::keyCTRLRIGHT : +#endif + case 0 : case cimg::keyCTRLLEFT : key = 0; break; + case cimg::keyPAGEUP : + if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(1); key = 0; } break; + case cimg::keyPAGEDOWN : + if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(-1); key = 0; } break; + case cimg::keyA : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + is_axes = !is_axes; disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), + CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). + _is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + is_view3d = !is_view3d; disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + if (visu0) { + (+visu0).draw_text(0,0," Saving snapshot... ",foreground_color,background_color,0.7f,13).display(disp); + visu0.save(filename); + (+visu0).draw_text(0,0," Snapshot '%s' saved. ",foreground_color,background_color,0.7f,13,filename). + display(disp); + } + disp.set_key(key,false); key = 0; + } break; + case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { +#ifdef cimg_use_zlib + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); +#else + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); +#endif + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu0).draw_text(0,0," Saving instance... ",foreground_color,background_color,0.7f,13).display(disp); + save(filename); + (+visu0).draw_text(0,0," Instance '%s' saved. ",foreground_color,background_color,0.7f,13,filename). + display(disp); + disp.set_key(key,false); key = 0; + } break; + } + + switch (area) { + + case 0 : // When mouse is out of image range. + mx = my = -1; X = Y = Z = -1; + break; + + case 1 : case 2 : case 3 : // When mouse is over the XY,XZ or YZ projections. + if (disp.button()&1 && phase<2 && clicked_area==area) { // When selection has been started (1st step). + if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign(); + X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; + } + if (!(disp.button()&1) && phase>=2 && clicked_area!=area) { // When selection is at 2nd step (for volumes). + switch (starting_area) { + case 1 : if (Z1!=(int)Z) visu0.assign(); Z1 = (int)Z; break; + case 2 : if (Y1!=(int)Y) visu0.assign(); Y1 = (int)Y; break; + case 3 : if (X1!=(int)X) visu0.assign(); X1 = (int)X; break; + } + } + if (disp.button()&2 && clicked_area==area) { // When moving through the image/volume. + if (phase) { + if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign(); + X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; + } else { + if (_depth>1 && (X0!=(int)X || Y0!=(int)Y || Z0!=(int)Z)) visu0.assign(); + X0 = (int)X; Y0 = (int)Y; Z0 = (int)Z; + } + } + if (disp.button()&4) { // Reset positions. + X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = area = clicked_area = starting_area = 0; + visu0.assign(); + } + if (disp.wheel()) { // When moving through the slices of the volume (with mouse wheel). + if (_depth>1 && !disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT() && + !disp.is_keySHIFTLEFT() && !disp.is_keySHIFTRIGHT() && + !disp.is_keyALT() && !disp.is_keyALTGR()) { + switch (area) { + case 1 : + if (phase) Z = (float)(Z1+=disp.wheel()); else Z = (float)(Z0+=disp.wheel()); + visu0.assign(); break; + case 2 : + if (phase) Y = (float)(Y1+=disp.wheel()); else Y = (float)(Y0+=disp.wheel()); + visu0.assign(); break; + case 3 : + if (phase) X = (float)(X1+=disp.wheel()); else X = (float)(X0+=disp.wheel()); + visu0.assign(); break; + } + disp.set_wheel(); + } else key = ~0U; + } + if ((disp.button()&1)!=old_button) { // When left button has just been pressed or released. + switch (phase) { + case 0 : + if (area==clicked_area) { + X0 = X1 = (int)X; Y0 = Y1 = (int)Y; Z0 = Z1 = (int)Z; starting_area = area; ++phase; + } break; + case 1 : + if (area==starting_area) { + X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; ++phase; + } else if (!(disp.button()&1)) { X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = 0; visu0.assign(); } + break; + case 2 : ++phase; break; + } + old_button = disp.button()&1; + } + break; + + case 4 : // When mouse is over the 3d view. + if (is_view3d && points3d) { + X3d = mx - _width*disp.width()/(_width+(_depth>1?_depth:0)); + Y3d = my - _height*disp.height()/(_height+(_depth>1?_depth:0)); + if (oX3d<0) { oX3d = X3d; oY3d = Y3d; } + // Left + right buttons: reset. + if ((disp.button()&3)==3) { pose3d.assign(); view3d.assign(); oX3d = oY3d = X3d = Y3d = -1; } + else if (disp.button()&1 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Left button: rotate. + const float + R = 0.45f*cimg::min(view3d._width,view3d._height), + R2 = R*R, + u0 = (float)(oX3d-view3d.width()/2), + v0 = (float)(oY3d-view3d.height()/2), + u1 = (float)(X3d-view3d.width()/2), + v1 = (float)(Y3d-view3d.height()/2), + n0 = (float)std::sqrt(u0*u0+v0*v0), + n1 = (float)std::sqrt(u1*u1+v1*v1), + nu0 = n0>R?(u0*R/n0):u0, + nv0 = n0>R?(v0*R/n0):v0, + nw0 = (float)std::sqrt(cimg::max(0,R2-nu0*nu0-nv0*nv0)), + nu1 = n1>R?(u1*R/n1):u1, + nv1 = n1>R?(v1*R/n1):v1, + nw1 = (float)std::sqrt(cimg::max(0,R2-nu1*nu1-nv1*nv1)), + u = nv0*nw1 - nw0*nv1, + v = nw0*nu1 - nu0*nw1, + w = nv0*nu1 - nu0*nv1, + n = (float)std::sqrt(u*u+v*v+w*w), + alpha = (float)std::asin(n/R2); + pose3d.draw_image(CImg::rotation_matrix(u,v,w,alpha)*pose3d.get_crop(0,0,2,2)); + view3d.assign(); + } else if (disp.button()&2 && pose3d && oY3d!=Y3d) { // Right button: zoom. + pose3d(3,2)-=(oY3d - Y3d)*1.5f; view3d.assign(); + } + if (disp.wheel()) { // Wheel: zoom + pose3d(3,2)-=disp.wheel()*15; view3d.assign(); disp.set_wheel(); + } + if (disp.button()&4 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Middle button: shift. + pose3d(3,0)-=oX3d - X3d; pose3d(3,1)-=oY3d - Y3d; view3d.assign(); + } + oX3d = X3d; oY3d = Y3d; + } + mx = my = -1; X = Y = Z = -1; + break; + } + + if (phase) { + if (!feature_type) shape_selected = phase?true:false; + else { + if (_depth>1) shape_selected = (phase==3)?true:false; + else shape_selected = (phase==2)?true:false; + } + } + + if (X0<0) X0 = 0; if (X0>=width()) X0 = width() - 1; + if (Y0<0) Y0 = 0; if (Y0>=height()) Y0 = height() - 1; + if (Z0<0) Z0 = 0; if (Z0>=depth()) Z0 = depth() - 1; + if (X1<1) X1 = 0; if (X1>=width()) X1 = width() - 1; + if (Y1<0) Y1 = 0; if (Y1>=height()) Y1 = height() - 1; + if (Z1<0) Z1 = 0; if (Z1>=depth()) Z1 = depth() - 1; + + // Draw visualization image on the display + if (mx!=omx || my!=omy || !visu0 || (_depth>1 && !view3d)) { + + if (!visu0) { // Create image of projected planes. + __get_select(disp,old_normalization,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0).move_to(visu0).resize(disp); + view3d.assign(); + points3d.assign(); + } + + if (is_view3d && _depth>1 && !view3d) { // Create 3d view for volumetric images. + const unsigned int + _x3d = (unsigned int)cimg::round((float)_width*visu0._width/(_width+_depth),1,1), + _y3d = (unsigned int)cimg::round((float)_height*visu0._height/(_height+_depth),1,1), + x3d = _x3d>=visu0._width?visu0._width-1:_x3d, + y3d = _y3d>=visu0._height?visu0._height-1:_y3d; + CImg(1,2,1,1,64,128).resize(visu0._width-x3d,visu0._height-y3d,1,visu0._spectrum,3).move_to(view3d); + if (!points3d) { + get_projections3d(primitives3d,colors3d,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0,true).move_to(points3d); + points3d.append(CImg(8,3,1,1, + 0,_width-1,_width-1,0,0,_width-1,_width-1,0, + 0,0,_height-1,_height-1,0,0,_height-1,_height-1, + 0,0,0,0,_depth-1,_depth-1,_depth-1,_depth-1),'x'); + CImg::vector(12,13).move_to(primitives3d); CImg::vector(13,14).move_to(primitives3d); + CImg::vector(14,15).move_to(primitives3d); CImg::vector(15,12).move_to(primitives3d); + CImg::vector(16,17).move_to(primitives3d); CImg::vector(17,18).move_to(primitives3d); + CImg::vector(18,19).move_to(primitives3d); CImg::vector(19,16).move_to(primitives3d); + CImg::vector(12,16).move_to(primitives3d); CImg::vector(13,17).move_to(primitives3d); + CImg::vector(14,18).move_to(primitives3d); CImg::vector(15,19).move_to(primitives3d); + colors3d.insert(12,CImg::vector(255,255,255)); + opacities3d.assign(primitives3d.width(),1,1,1,0.5f); + if (!phase) { + opacities3d[0] = opacities3d[1] = opacities3d[2] = 0.8f; + sel_primitives3d.assign(); + sel_colors3d.assign(); + sel_opacities3d.assign(); + } else { + if (feature_type==2) { + points3d.append(CImg(8,3,1,1, + X0,X1,X1,X0,X0,X1,X1,X0, + Y0,Y0,Y1,Y1,Y0,Y0,Y1,Y1, + Z0,Z0,Z0,Z0,Z1,Z1,Z1,Z1),'x'); + sel_primitives3d.assign(); + CImg::vector(20,21).move_to(sel_primitives3d); + CImg::vector(21,22).move_to(sel_primitives3d); + CImg::vector(22,23).move_to(sel_primitives3d); + CImg::vector(23,20).move_to(sel_primitives3d); + CImg::vector(24,25).move_to(sel_primitives3d); + CImg::vector(25,26).move_to(sel_primitives3d); + CImg::vector(26,27).move_to(sel_primitives3d); + CImg::vector(27,24).move_to(sel_primitives3d); + CImg::vector(20,24).move_to(sel_primitives3d); + CImg::vector(21,25).move_to(sel_primitives3d); + CImg::vector(22,26).move_to(sel_primitives3d); + CImg::vector(23,27).move_to(sel_primitives3d); + } else { + points3d.append(CImg(2,3,1,1, + X0,X1, + Y0,Y1, + Z0,Z1),'x'); + sel_primitives3d.assign(CImg::vector(20,21)); + } + sel_colors3d.assign(sel_primitives3d._width,CImg::vector(255,255,255)); + sel_opacities3d.assign(sel_primitives3d._width,1,1,1,0.8f); + } + points3d.shift_object3d(-0.5f*(_width-1),-0.5f*(_height-1),-0.5f*(_depth-1)).resize_object3d(); + points3d*=0.75f*cimg::min(view3d._width,view3d._height); + } + + if (!pose3d) CImg(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose3d); + CImg zbuffer3d(view3d._width,view3d._height,1,1,0); + const CImg rotated_points3d = pose3d.get_crop(0,0,2,2)*points3d; + if (sel_primitives3d) + view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width, + pose3d(3,1) + 0.5f*view3d._height, + pose3d(3,2), + rotated_points3d,sel_primitives3d,sel_colors3d,sel_opacities3d, + 2,true,500,0,0,0,0,0,zbuffer3d); + view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width, + pose3d(3,1) + 0.5f*view3d._height, + pose3d(3,2), + rotated_points3d,primitives3d,colors3d,opacities3d, + 2,true,500,0,0,0,0,0,zbuffer3d); + visu0.draw_image(x3d,y3d,view3d); + } + visu = visu0; + + if (X<0 || Y<0 || Z<0) { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }} + else { + if (is_axes) { if (visible_cursor) { disp.hide_mouse(); visible_cursor = false; }} + else { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }} + const int d = (_depth>1)?_depth:0; + int + w = disp.width(), W = width() + d, + h = disp.height(), H = height() + d, + _xp = (int)X*w/W, xp = _xp + (_xp*W/w!=(int)X?1:0), + _yp = (int)Y*h/H, yp = _yp + (_yp*H/h!=(int)Y?1:0), + _xn = (int)(X+1)*w/W-1, xn = _xn + ((_xn+1)*W/w!=(int)X+1?1:0), + _yn = (int)(Y+1)*h/H-1, yn = _yn + ((_yn+1)*H/h!=(int)Y+1?1:0), + _zxp = ((int)Z+width())*w/W, zxp = _zxp + (_zxp*W/w!=(int)Z+width()?1:0), + _zyp = ((int)Z+height())*h/H, zyp = _zyp + (_zyp*H/h!=(int)Z+height()?1:0), + _zxn = ((int)Z+width()+1)*w/W-1, zxn = _zxn + ((_zxn+1)*W/w!=(int)Z+width()+1?1:0), + _zyn = ((int)Z+height()+1)*h/H-1, zyn = _zyn + ((_zyn+1)*H/h!=(int)Z+height()+1?1:0), + _xM = width()*w/W-1, xM = _xM + ((_xM+1)*W/w!=width()?1:0), + _yM = height()*h/H-1, yM = _yM + ((_yM+1)*H/h!=height()?1:0), + xc = (xp + xn)/2, + yc = (yp + yn)/2, + zxc = (zxp + zxn)/2, + zyc = (zyp + zyn)/2, + xf = (int)(X*w/W), + yf = (int)(Y*h/H), + zxf = (int)((Z+width())*w/W), + zyf = (int)((Z+height())*h/H); + + if (is_axes) { // Draw axes. + visu.draw_line(0,yf,visu.width()-1,yf,foreground_color,0.7f,0xFF00FF00). + draw_line(0,yf,visu.width()-1,yf,background_color,0.7f,0x00FF00FF). + draw_line(xf,0,xf,visu.height()-1,foreground_color,0.7f,0xFF00FF00). + draw_line(xf,0,xf,visu.height()-1,background_color,0.7f,0x00FF00FF); + if (_depth>1) + visu.draw_line(zxf,0,zxf,yM,foreground_color,0.7f,0xFF00FF00). + draw_line(zxf,0,zxf,yM,background_color,0.7f,0x00FF00FF). + draw_line(0,zyf,xM,zyf,foreground_color,0.7f,0xFF00FF00). + draw_line(0,zyf,xM,zyf,background_color,0.7f,0x00FF00FF); + } + + // Draw box cursor. + if (xn-xp>=4 && yn-yp>=4) visu.draw_rectangle(xp,yp,xn,yn,foreground_color,0.2f). + draw_rectangle(xp,yp,xn,yn,foreground_color,1,0xAAAAAAAA). + draw_rectangle(xp,yp,xn,yn,background_color,1,0x55555555); + if (_depth>1) { + if (yn-yp>=4 && zxn-zxp>=4) visu.draw_rectangle(zxp,yp,zxn,yn,background_color,0.2f). + draw_rectangle(zxp,yp,zxn,yn,foreground_color,1,0xAAAAAAAA). + draw_rectangle(zxp,yp,zxn,yn,background_color,1,0x55555555); + if (xn-xp>=4 && zyn-zyp>=4) visu.draw_rectangle(xp,zyp,xn,zyn,background_color,0.2f). + draw_rectangle(xp,zyp,xn,zyn,foreground_color,1,0xAAAAAAAA). + draw_rectangle(xp,zyp,xn,zyn,background_color,1,0x55555555); + } + + // Draw selection. + if (phase) { + const int + _xp0 = X0*w/W, xp0 = _xp0 + (_xp0*W/w!=X0?1:0), + _yp0 = Y0*h/H, yp0 = _yp0 + (_yp0*H/h!=Y0?1:0), + _xn0 = (X0+1)*w/W-1, xn0 = _xn0 + ((_xn0+1)*W/w!=X0+1?1:0), + _yn0 = (Y0+1)*h/H-1, yn0 = _yn0 + ((_yn0+1)*H/h!=Y0+1?1:0), + _zxp0 = (Z0+width())*w/W, zxp0 = _zxp0 + (_zxp0*W/w!=Z0+width()?1:0), + _zyp0 = (Z0+height())*h/H, zyp0 = _zyp0 + (_zyp0*H/h!=Z0+height()?1:0), + _zxn0 = (Z0+width()+1)*w/W-1, zxn0 = _zxn0 + ((_zxn0+1)*W/w!=Z0+width()+1?1:0), + _zyn0 = (Z0+height()+1)*h/H-1, zyn0 = _zyn0 + ((_zyn0+1)*H/h!=Z0+height()+1?1:0), + xc0 = (xp0 + xn0)/2, + yc0 = (yp0 + yn0)/2, + zxc0 = (zxp0 + zxn0)/2, + zyc0 = (zyp0 + zyn0)/2; + + switch (feature_type) { + case 1 : { + visu.draw_arrow(xc0,yc0,xc,yc,background_color,0.9f,30,5,0x55555555). + draw_arrow(xc0,yc0,xc,yc,foreground_color,0.9f,30,5,0xAAAAAAAA); + if (d) { + visu.draw_arrow(zxc0,yc0,zxc,yc,background_color,0.9f,30,5,0x55555555). + draw_arrow(zxc0,yc0,zxc,yc,foreground_color,0.9f,30,5,0xAAAAAAAA). + draw_arrow(xc0,zyc0,xc,zyc,background_color,0.9f,30,5,0x55555555). + draw_arrow(xc0,zyc0,xc,zyc,foreground_color,0.9f,30,5,0xAAAAAAAA); + } + } break; + case 2 : { + visu.draw_rectangle(X0=0 && my<13) text_down = true; else if (my>=visu.height()-13) text_down = false; + if (!feature_type || !phase) { + if (X>=0 && Y>=0 && Z>=0 && X1 || force_display_z_coord) + cimg_snprintf(text,sizeof(text)," Point (%d,%d,%d) = [ ",origX+(int)X,origY+(int)Y,origZ+(int)Z); + else cimg_snprintf(text,sizeof(text)," Point (%d,%d) = [ ",origX+(int)X,origY+(int)Y); + char *ctext = text + std::strlen(text), *const ltext = text + 512; + for (unsigned int c = 0; c<_spectrum && ctext::format(), + cimg::type::format((*this)((int)X,(int)Y,(int)Z,c))); + ctext = text + std::strlen(text); + *(ctext++) = ' '; *ctext = 0; + } + std::strcpy(text + std::strlen(text),"] "); + } + } else switch (feature_type) { + case 1 : { + const double dX = (double)(X0 - X1), dY = (double)(Y0 - Y1), dZ = (double)(Z0 - Z1), + norm = std::sqrt(dX*dX+dY*dY+dZ*dZ); + if (_depth>1 || force_display_z_coord) + cimg_snprintf(text,sizeof(text)," Vect (%d,%d,%d)-(%d,%d,%d), Norm = %g ", + origX+X0,origY+Y0,origZ+Z0,origX+X1,origY+Y1,origZ+Z1,norm); + else cimg_snprintf(text,sizeof(text)," Vect (%d,%d)-(%d,%d), Norm = %g ", + origX+X0,origY+Y0,origX+X1,origY+Y1,norm); + } break; + case 2 : + if (_depth>1 || force_display_z_coord) + cimg_snprintf(text,sizeof(text)," Box (%d,%d,%d)-(%d,%d,%d), Size = (%d,%d,%d) ", + origX+(X01 || force_display_z_coord) + cimg_snprintf(text,sizeof(text)," Ellipse (%d,%d,%d)-(%d,%d,%d), Radii = (%d,%d,%d) ", + origX+X0,origY+Y0,origZ+Z0,origX+X1,origY+Y1,origZ+Z1, + 1+cimg::abs(X0-X1),1+cimg::abs(Y0-Y1),1+cimg::abs(Z0-Z1)); + else cimg_snprintf(text,sizeof(text)," Ellipse (%d,%d)-(%d,%d), Radii = (%d,%d) ", + origX+X0,origY+Y0,origX+X1,origY+Y1,1+cimg::abs(X0-X1),1+cimg::abs(Y0-Y1)); + } + if (phase || (mx>=0 && my>=0)) + visu.draw_text(0,text_down?visu.height()-13:0,text,foreground_color,background_color,0.7f,13); + } + + disp.display(visu).wait(); + } else if (!shape_selected) disp.wait(); + if (disp.is_resized()) { disp.resize(false)._is_resized = false; old_is_resized = true; visu0.assign(); } + omx = mx; omy = my; + } + + // Return result. + CImg res(1,feature_type==0?3:6,1,1,-1); + if (XYZ) { XYZ[0] = (unsigned int)X0; XYZ[1] = (unsigned int)Y0; XYZ[2] = (unsigned int)Z0; } + if (shape_selected) { + if (feature_type==2) { + if (X0>X1) cimg::swap(X0,X1); + if (Y0>Y1) cimg::swap(Y0,Y1); + if (Z0>Z1) cimg::swap(Z0,Z1); + } + if (X1<0 || Y1<0 || Z1<0) X0 = Y0 = Z0 = X1 = Y1 = Z1 = -1; + switch (feature_type) { + case 1 : case 2 : res[0] = X0; res[1] = Y0; res[2] = Z0; res[3] = X1; res[4] = Y1; res[5] = Z1; break; + case 3 : + res[3] = cimg::abs(X1-X0); res[4] = cimg::abs(Y1-Y0); res[5] = cimg::abs(Z1-Z0); // keep no break here! + default : res[0] = X0; res[1] = Y0; res[2] = Z0; + } + } + disp.set_button(); + if (!visible_cursor) disp.show_mouse(); + disp._normalization = old_normalization; + disp._is_resized = old_is_resized; + if (key!=~0U) disp.set_key(key); + return res; + } + + // Return a visualizable uchar8 image for display routines. + CImg __get_select(const CImgDisplay& disp, const int normalization, + const int x, const int y, const int z) const { + if (is_empty()) return CImg(1,1,1,1,0); + const CImg crop = get_shared_channels(0,cimg::min(2,spectrum()-1)); + CImg img2d; + if (_depth>1) crop.get_projections2d(x,y,z).move_to(img2d); + else CImg(crop,false).move_to(img2d); + + if (cimg::type::is_float()) { // Check for inf and nan values. + bool is_inf = false, is_nan = false; + cimg_for(img2d,ptr,Tuchar) + if (cimg::type::is_inf(*ptr)) { is_inf = true; break; } + else if (cimg::type::is_nan(*ptr)) { is_nan = true; break; } + if (is_inf || is_nan) { + T m0 = cimg::type::max(), M0 = cimg::type::min(); + if (!normalization) { m0 = 0; M0 = 255; } + else if (normalization==2) { m0 = (T)disp._min; M0 = (T)disp._max; } + else + cimg_for(img2d,ptr,Tuchar) + if (!cimg::type::is_inf(*ptr) && !cimg::type::is_nan(*ptr)) { + if (*ptrM0) M0 = *ptr; + } + const T + val_minf = (normalization==1 || normalization==3)?m0-(M0-m0)*20-1:m0, + val_pinf = (normalization==1 || normalization==3)?M0+(M0-m0)*20+1:M0; + if (is_nan) + cimg_for(img2d,ptr,Tuchar) + if (cimg::type::is_nan(*ptr)) *ptr = val_minf; // Replace nan values. + if (is_inf) + cimg_for(img2d,ptr,Tuchar) + if (cimg::type::is_inf(*ptr)) *ptr = (float)*ptr<0?val_minf:val_pinf; // Replace +-inf values. + } + } + + switch (normalization) { + case 1 : img2d.normalize(0,255); break; + case 2 : { + const float m = disp._min, M = disp._max; + (img2d-=m)*=255.0f/(M-m>0?M-m:1); + } break; + case 3 : + if (cimg::type::is_float()) img2d.normalize(0,255); + else { + const float m = (float)cimg::type::min(), M = (float)cimg::type::max(); + (img2d-=m)*=255.0f/(M-m>0?M-m:1); + } break; + } + + if (img2d.spectrum()==2) img2d.channels(0,2); + return img2d; + } + + //! Select sub-graph in a graph. + CImg get_select_graph(CImgDisplay &disp, + const unsigned int plot_type=1, const unsigned int vertex_type=1, + const char *const labelx=0, const double xmin=0, const double xmax=0, + const char *const labely=0, const double ymin=0, const double ymax=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "select_graph(): Empty instance.", + cimg_instance); + if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0). + set_title("CImg<%s>",pixel_type()); + const unsigned long siz = (unsigned long)_width*_height*_depth; + const unsigned int old_normalization = disp.normalization(); + disp.show().set_button().set_wheel()._normalization = 0; + + double nymin = ymin, nymax = ymax, nxmin = xmin, nxmax = xmax; + if (nymin==nymax) { nymin = (Tfloat)min_max(nymax); const double dy = nymax - nymin; nymin-=dy/20; nymax+=dy/20; } + if (nymin==nymax) { --nymin; ++nymax; } + if (nxmin==nxmax && nxmin==0) { nxmin = 0; nxmax = siz - 1.0; } + + const unsigned char black[] = { 0, 0, 0 }, white[] = { 255, 255, 255 }, gray[] = { 220, 220, 220 }; + const unsigned char gray2[] = { 110, 110, 110 }, ngray[] = { 35, 35, 35 }; + static unsigned int odimv = 0; + static CImg colormap; + if (odimv!=_spectrum) { + odimv = _spectrum; + colormap = CImg(3,_spectrum,1,1,120).noise(70,1); + if (_spectrum==1) { colormap[0] = colormap[1] = 120; colormap[2] = 200; } + else { + colormap(0,0) = 220; colormap(1,0) = 10; colormap(2,0) = 10; + if (_spectrum>1) { colormap(0,1) = 10; colormap(1,1) = 220; colormap(2,1) = 10; } + if (_spectrum>2) { colormap(0,2) = 10; colormap(1,2) = 10; colormap(2,2) = 220; } + } + } + + CImg visu0, visu, graph, text, axes; + int x0 = -1, x1 = -1, y0 = -1, y1 = -1, omouse_x = -2, omouse_y = -2; + const unsigned int one = plot_type==3?0:1; + unsigned int okey = 0, obutton = 0; + char message[1024] = { 0 }; + CImg_3x3(I,unsigned char); + + for (bool selected = false; !selected && !disp.is_closed() && !okey && !disp.wheel(); ) { + const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y(); + const unsigned int key = disp.key(), button = disp.button(); + + // Generate graph representation. + if (!visu0) { + visu0.assign(disp.width(),disp.height(),1,3,220); + const int gdimx = disp.width() - 32, gdimy = disp.height() - 32; + if (gdimx>0 && gdimy>0) { + graph.assign(gdimx,gdimy,1,3,255); + if (siz<32) { + if (siz>1) graph.draw_grid(gdimx/(float)(siz - one),gdimy/(float)(siz - one),0,0, + false,true,black,0.2f,0x33333333,0x33333333); + } else graph.draw_grid(-10,-10,0,0,false,true,black,0.2f,0x33333333,0x33333333); + cimg_forC(*this,c) + graph.draw_graph(get_shared_channel(c),&colormap(0,c),(plot_type!=3 || _spectrum==1)?1:0.6f, + plot_type,vertex_type,nymax,nymin); + + axes.assign(gdimx,gdimy,1,1,0); + const float + dx = (float)cimg::abs(nxmax-nxmin), dy = (float)cimg::abs(nymax-nymin), + px = (float)std::pow(10.0,(int)std::log10(dx?dx:1)-2.0), + py = (float)std::pow(10.0,(int)std::log10(dy?dy:1)-2.0); + const CImg + seqx = dx<=0?CImg::vector(nxmin): + CImg::sequence(1 + gdimx/60,nxmin,one?nxmax:nxmin+(nxmax-nxmin)*(siz+1)/siz).round(px), + seqy = CImg::sequence(1 + gdimy/60,nymax,nymin).round(py); + + const bool allow_zero = (nxmin*nxmax>0) || (nymin*nymax>0); + axes.draw_axes(seqx,seqy,white,1,~0U,~0U,13,allow_zero); + if (nymin>0) axes.draw_axis(seqx,gdimy-1,gray,1,~0U,13,allow_zero); + if (nymax<0) axes.draw_axis(seqx,0,gray,1,~0U,13,allow_zero); + if (nxmin>0) axes.draw_axis(0,seqy,gray,1,~0U,13,allow_zero); + if (nxmax<0) axes.draw_axis(gdimx-1,seqy,gray,1,~0U,13,allow_zero); + + cimg_for3x3(axes,x,y,0,0,I,unsigned char) + if (Icc) { + if (Icc==255) cimg_forC(graph,c) graph(x,y,c) = 0; + else cimg_forC(graph,c) graph(x,y,c) = (unsigned char)(2*graph(x,y,c)/3); + } + else if (Ipc || Inc || Icp || Icn || Ipp || Inn || Ipn || Inp) + cimg_forC(graph,c) graph(x,y,c) = (graph(x,y,c)+511)/3; + + visu0.draw_image(16,16,graph); + visu0.draw_line(15,15,16+gdimx,15,gray2).draw_line(16+gdimx,15,16+gdimx,16+gdimy,gray2). + draw_line(16+gdimx,16+gdimy,15,16+gdimy,white).draw_line(15,16+gdimy,15,15,white); + } else graph.assign(); + text.assign().draw_text(0,0,labelx?labelx:"X-axis",white,ngray,1,13).resize(-100,-100,1,3); + visu0.draw_image((visu0.width()-text.width())/2,visu0.height()-14,~text); + text.assign().draw_text(0,0,labely?labely:"Y-axis",white,ngray,1,13).rotate(-90).resize(-100,-100,1,3); + visu0.draw_image(1,(visu0.height()-text.height())/2,~text); + visu.assign(); + } + + // Generate and display current view. + if (!visu) { + visu.assign(visu0); + if (graph && x0>=0 && x1>=0) { + const int + nx0 = x0<=x1?x0:x1, + nx1 = x0<=x1?x1:x0, + ny0 = y0<=y1?y0:y1, + ny1 = y0<=y1?y1:y0, + sx0 = 16 + nx0*(visu.width()-32)/cimg::max(1U,siz-one), + sx1 = 15 + (nx1+1)*(visu.width()-32)/cimg::max(1U,siz-one), + sy0 = 16 + ny0, + sy1 = 16 + ny1; + if (y0>=0 && y1>=0) + visu.draw_rectangle(sx0,sy0,sx1,sy1,gray,0.5f).draw_rectangle(sx0,sy0,sx1,sy1,black,0.5f,0xCCCCCCCCU); + else visu.draw_rectangle(sx0,0,sx1,visu.height()-17,gray,0.5f). + draw_line(sx0,16,sx0,visu.height()-17,black,0.5f,0xCCCCCCCCU). + draw_line(sx1,16,sx1,visu.height()-17,black,0.5f,0xCCCCCCCCU); + } + if (mouse_x>=16 && mouse_y>=16 && mouse_x=7) + cimg_snprintf(message,sizeof(message),"Value[%u:%g] = ( %g %g %g ... %g %g %g )",x,cx, + (double)(*this)(x,0,0,0),(double)(*this)(x,0,0,1),(double)(*this)(x,0,0,2), + (double)(*this)(x,0,0,_spectrum-4),(double)(*this)(x,0,0,_spectrum-3), + (double)(*this)(x,0,0,_spectrum-1)); + else { + cimg_snprintf(message,sizeof(message),"Value[%u:%g] = ( ",x,cx); + cimg_forC(*this,c) std::sprintf(message + std::strlen(message),"%g ",(double)(*this)(x,0,0,c)); + std::sprintf(message + std::strlen(message),")"); + } + if (x0>=0 && x1>=0) { + const unsigned int + nx0 = x0<=x1?x0:x1, + nx1 = x0<=x1?x1:x0, + ny0 = y0<=y1?y0:y1, + ny1 = y0<=y1?y1:y0; + const double + cx0 = nxmin + nx0*(nxmax-nxmin)/cimg::max(1U,siz-1), + cx1 = nxmin + (nx1+one)*(nxmax-nxmin)/cimg::max(1U,siz-1), + cy0 = nymax - ny0*(nymax-nymin)/(visu._height-32), + cy1 = nymax - ny1*(nymax-nymin)/(visu._height-32); + if (y0>=0 && y1>=0) + std::sprintf(message + std::strlen(message)," - Range ( %u:%g, %g ) - ( %u:%g, %g )", + x0,cx0,cy0,x1+one,cx1,cy1); + else + std::sprintf(message + std::strlen(message)," - Range [ %u:%g - %u:%g ]", + x0,cx0,x1+one,cx1); + } + text.assign().draw_text(0,0,message,white,ngray,1,13).resize(-100,-100,1,3); + visu.draw_image((visu.width()-text.width())/2,1,~text); + } + visu.display(disp); + } + + // Test keys. + switch (okey = key) { +#if cimg_OS!=2 + case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT : +#endif + case cimg::keyCTRLLEFT : case cimg::keySHIFTLEFT : okey = 0; break; + case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), + CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). + _is_resized = true; + disp.set_key(key,false); okey = 0; + } break; + case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; + disp.set_key(key,false); okey = 0; + } break; + case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(CImgDisplay::screen_width()/2, + CImgDisplay::screen_height()/2,1),false)._is_resized = true; + disp.set_key(key,false); okey = 0; + } break; + case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true; + disp.set_key(key,false); okey = 0; + } break; + case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + static unsigned int snap_number = 0; + if (visu || visu0) { + CImg &screen = visu?visu:visu0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+screen).draw_text(0,0," Saving snapshot... ",black,gray,1,13).display(disp); + screen.save(filename); + (+screen).draw_text(0,0," Snapshot '%s' saved. ",black,gray,1,13,filename).display(disp); + } + disp.set_key(key,false); okey = 0; + } break; + case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + static unsigned int snap_number = 0; + if (visu || visu0) { + CImg &screen = visu?visu:visu0; + char filename[32] = { 0 }; + std::FILE *file; + do { +#ifdef cimg_use_zlib + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); +#else + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); +#endif + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+screen).draw_text(0,0," Saving instance... ",black,gray,1,13).display(disp); + save(filename); + (+screen).draw_text(0,0," Instance '%s' saved. ",black,gray,1,13,filename).display(disp); + } + disp.set_key(key,false); okey = 0; + } break; + } + + // Handle mouse motion and mouse buttons + if (obutton!=button || omouse_x!=mouse_x || omouse_y!=mouse_y) { + visu.assign(); + if (disp.mouse_x()>=0 && disp.mouse_y()>=0) { + const int + mx = (mouse_x -16)*(int)(siz-one)/(disp.width()-32), + cx = mx<0?0:(mx>=(int)(siz-one)?(int)(siz-1-one):mx), + my = mouse_y - 16, + cy = my<=0?0:(my>=(disp.height()-32)?(disp.height()-32):my); + if (button&1) { + if (!obutton) { x0 = cx; y0 = -1; } else { x1 = cx; y1 = -1; } + } + else if (button&2) { + if (!obutton) { x0 = cx; y0 = cy; } else { x1 = cx; y1 = cy; } + } + else if (obutton) { x1 = x1>=0?cx:-1; y1 = y1>=0?cy:-1; selected = true; } + } else if (!button && obutton) selected = true; + obutton = button; omouse_x = mouse_x; omouse_y = mouse_y; + } + if (disp.is_resized()) { disp.resize(false); visu0.assign(); } + if (visu && visu0) disp.wait(); + } + + disp._normalization = old_normalization; + if (x1>=0 && x1(4,1,1,1,x0,y0,x1>=0?x1+(int)one:-1,y1); + } + + //! Load image from a file. + /** + \param filename Filename, as a C-string. + \note The extension of \c filename defines the file format. If no filename + extension is provided, CImg::get_load() will try to load the file as a .cimg or .cimgz file. + **/ + CImg& load(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load(): Specified filename is (null).", + cimg_instance); + + if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) { + char filename_local[1024] = { 0 }; + load(cimg::load_network_external(filename,filename_local)); + std::remove(filename_local); + return *this; + } + + const char *const ext = cimg::split_filename(filename); + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { +#ifdef cimg_load_plugin + cimg_load_plugin(filename); +#endif +#ifdef cimg_load_plugin1 + cimg_load_plugin1(filename); +#endif +#ifdef cimg_load_plugin2 + cimg_load_plugin2(filename); +#endif +#ifdef cimg_load_plugin3 + cimg_load_plugin3(filename); +#endif +#ifdef cimg_load_plugin4 + cimg_load_plugin4(filename); +#endif +#ifdef cimg_load_plugin5 + cimg_load_plugin5(filename); +#endif +#ifdef cimg_load_plugin6 + cimg_load_plugin6(filename); +#endif +#ifdef cimg_load_plugin7 + cimg_load_plugin7(filename); +#endif +#ifdef cimg_load_plugin8 + cimg_load_plugin8(filename); +#endif + // Ascii formats + if (!cimg::strcasecmp(ext,"asc")) load_ascii(filename); + else if (!cimg::strcasecmp(ext,"dlm") || + !cimg::strcasecmp(ext,"txt")) load_dlm(filename); + + // 2d binary formats + else if (!cimg::strcasecmp(ext,"bmp")) load_bmp(filename); + else if (!cimg::strcasecmp(ext,"jpg") || + !cimg::strcasecmp(ext,"jpeg") || + !cimg::strcasecmp(ext,"jpe") || + !cimg::strcasecmp(ext,"jfif") || + !cimg::strcasecmp(ext,"jif")) load_jpeg(filename); + else if (!cimg::strcasecmp(ext,"png")) load_png(filename); + else if (!cimg::strcasecmp(ext,"ppm") || + !cimg::strcasecmp(ext,"pgm") || + !cimg::strcasecmp(ext,"pnm") || + !cimg::strcasecmp(ext,"pbm") || + !cimg::strcasecmp(ext,"pnk")) load_pnm(filename); + else if (!cimg::strcasecmp(ext,"pfm")) load_pfm(filename); + else if (!cimg::strcasecmp(ext,"tif") || + !cimg::strcasecmp(ext,"tiff")) load_tiff(filename); + else if (!cimg::strcasecmp(ext,"exr")) load_exr(filename); + else if (!cimg::strcasecmp(ext,"cr2") || + !cimg::strcasecmp(ext,"crw") || + !cimg::strcasecmp(ext,"dcr") || + !cimg::strcasecmp(ext,"mrw") || + !cimg::strcasecmp(ext,"nef") || + !cimg::strcasecmp(ext,"orf") || + !cimg::strcasecmp(ext,"pix") || + !cimg::strcasecmp(ext,"ptx") || + !cimg::strcasecmp(ext,"raf") || + !cimg::strcasecmp(ext,"srf")) load_dcraw_external(filename); + else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename); + + // 3d binary formats + else if (!cimg::strcasecmp(ext,"dcm") || + !cimg::strcasecmp(ext,"dicom")) load_medcon_external(filename); + else if (!cimg::strcasecmp(ext,"hdr") || + !cimg::strcasecmp(ext,"nii")) load_analyze(filename); + else if (!cimg::strcasecmp(ext,"par") || + !cimg::strcasecmp(ext,"rec")) load_parrec(filename); + else if (!cimg::strcasecmp(ext,"mnc")) load_minc2(filename); + else if (!cimg::strcasecmp(ext,"inr")) load_inr(filename); + else if (!cimg::strcasecmp(ext,"pan")) load_pandore(filename); + else if (!cimg::strcasecmp(ext,"cimg") || + !cimg::strcasecmp(ext,"cimgz") || + !*ext) return load_cimg(filename); + + // Archive files + else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename); + + // Image sequences + else if (!cimg::strcasecmp(ext,"avi") || + !cimg::strcasecmp(ext,"mov") || + !cimg::strcasecmp(ext,"asf") || + !cimg::strcasecmp(ext,"divx") || + !cimg::strcasecmp(ext,"flv") || + !cimg::strcasecmp(ext,"mpg") || + !cimg::strcasecmp(ext,"m1v") || + !cimg::strcasecmp(ext,"m2v") || + !cimg::strcasecmp(ext,"m4v") || + !cimg::strcasecmp(ext,"mjp") || + !cimg::strcasecmp(ext,"mp4") || + !cimg::strcasecmp(ext,"mkv") || + !cimg::strcasecmp(ext,"mpe") || + !cimg::strcasecmp(ext,"movie") || + !cimg::strcasecmp(ext,"ogm") || + !cimg::strcasecmp(ext,"ogg") || + !cimg::strcasecmp(ext,"ogv") || + !cimg::strcasecmp(ext,"qt") || + !cimg::strcasecmp(ext,"rm") || + !cimg::strcasecmp(ext,"vob") || + !cimg::strcasecmp(ext,"wmv") || + !cimg::strcasecmp(ext,"xvid") || + !cimg::strcasecmp(ext,"mpeg")) load_ffmpeg(filename); + else throw CImgIOException("CImg<%s>::load()", + pixel_type()); + } catch (CImgIOException&) { + std::FILE *file = 0; + try { + file = cimg::fopen(filename,"rb"); + } catch (CImgIOException&) { + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load(): Failed to open file '%s'.", + cimg_instance, + filename); + } + + try { + const char *const f_type = cimg::file_type(file,filename); + std::fclose(file); + if (!cimg::strcasecmp(f_type,"pnm")) load_pnm(filename); + else if (!cimg::strcasecmp(f_type,"pfm")) load_pfm(filename); + else if (!cimg::strcasecmp(f_type,"bmp")) load_bmp(filename); + else if (!cimg::strcasecmp(f_type,"jpg")) load_jpeg(filename); + else if (!cimg::strcasecmp(f_type,"pan")) load_pandore(filename); + else if (!cimg::strcasecmp(f_type,"png")) load_png(filename); + else if (!cimg::strcasecmp(f_type,"tif")) load_tiff(filename); + else if (!cimg::strcasecmp(f_type,"inr")) load_inr(filename); + else if (!cimg::strcasecmp(f_type,"dcm")) load_medcon_external(filename); + else throw CImgIOException("CImg<%s>::load()", + pixel_type()); + } catch (CImgIOException&) { + try { + load_other(filename); + } catch (CImgIOException&) { + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load(): Failed to recognize format of file '%s'.", + cimg_instance, + filename); + } + } + } + cimg::exception_mode() = omode; + return *this; + } + + //! Load image from a file \newinstance. + static CImg get_load(const char *const filename) { + return CImg().load(filename); + } + + //! Load image from an ascii file. + /** + \param filename Filename, as a C -string. + **/ + CImg& load_ascii(const char *const filename) { + return _load_ascii(0,filename); + } + + //! Load image from an ascii file \inplace. + static CImg get_load_ascii(const char *const filename) { + return CImg().load_ascii(filename); + } + + //! Load image from an ascii file \overloading. + CImg& load_ascii(std::FILE *const file) { + return _load_ascii(file,0); + } + + //! Loadimage from an ascii file \newinstance. + static CImg get_load_ascii(std::FILE *const file) { + return CImg().load_ascii(file); + } + + CImg& _load_ascii(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_ascii(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + char line[256] = { 0 }; + int err = std::fscanf(nfile,"%255[^\n]",line); + unsigned int dx = 0, dy = 1, dz = 1, dc = 1; + std::sscanf(line,"%u%*c%u%*c%u%*c%u",&dx,&dy,&dz,&dc); + err = std::fscanf(nfile,"%*[^0-9.eE+-]"); + if (!dx || !dy || !dz || !dc) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_ascii(): Invalid ascii header in file '%s', image dimensions are set " + "to (%u,%u,%u,%u).", + cimg_instance, + filename?filename:"(FILE*)",dx,dy,dz,dc); + } + assign(dx,dy,dz,dc); + const unsigned long siz = size(); + unsigned long off = 0; + double val; + T *ptr = _data; + for (err = 1, off = 0; off& load_dlm(const char *const filename) { + return _load_dlm(0,filename); + } + + //! Load image from a DLM file \newinstance. + static CImg get_load_dlm(const char *const filename) { + return CImg().load_dlm(filename); + } + + //! Load image from a DLM file \overloading. + CImg& load_dlm(std::FILE *const file) { + return _load_dlm(file,0); + } + + //! Load image from a DLM file \newinstance. + static CImg get_load_dlm(std::FILE *const file) { + return CImg().load_dlm(file); + } + + CImg& _load_dlm(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_dlm(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"r"); + char delimiter[256] = { 0 }, tmp[256] = { 0 }; + unsigned int cdx = 0, dx = 0, dy = 0; + int err = 0; + double val; + assign(256,256); + while ((err = std::fscanf(nfile,"%lf%255[^0-9.+-]",&val,delimiter))>0) { + if (err>0) (*this)(cdx++,dy) = (T)val; + if (cdx>=_width) resize(3*_width/2,_height,1,1,0); + char c = 0; + if (!std::sscanf(delimiter,"%255[^\n]%c",tmp,&c) || c=='\n') { + dx = cimg::max(cdx,dx); + if (++dy>=_height) resize(_width,3*_height/2,1,1,0); + cdx = 0; + } + } + if (cdx && err==1) { dx = cdx; ++dy; } + if (!dx || !dy) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_dlm(): Invalid DLM file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + resize(dx,dy,1,1,0); + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a BMP file. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_bmp(const char *const filename) { + return _load_bmp(0,filename); + } + + //! Load image from a BMP file \newinstance. + static CImg get_load_bmp(const char *const filename) { + return CImg().load_bmp(filename); + } + + //! Load image from a BMP file \overloading. + CImg& load_bmp(std::FILE *const file) { + return _load_bmp(file,0); + } + + //! Load image from a BMP file \newinstance. + static CImg get_load_bmp(std::FILE *const file) { + return CImg().load_bmp(file); + } + + CImg& _load_bmp(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_bmp(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + unsigned char header[64] = { 0 }; + cimg::fread(header,54,nfile); + if (*header!='B' || header[1]!='M') { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_bmp(): Invalid BMP file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + + // Read header and pixel buffer + int + file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24), + offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24), + header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24), + dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24), + dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24), + compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24), + nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24), + bpp = header[0x1C] + (header[0x1D]<<8); + + if (!file_size || file_size==offset) { + std::fseek(nfile,0,SEEK_END); + file_size = (int)std::ftell(nfile); + std::fseek(nfile,54,SEEK_SET); + } + if (header_size>40) std::fseek(nfile, header_size - 40, SEEK_CUR); + + const int + cimg_iobuffer = 12*1024*1024, + dx_bytes = (bpp==1)?(dx/8+(dx%8?1:0)):((bpp==4)?(dx/2+(dx%2?1:0)):(dx*bpp/8)), + align_bytes = (4-dx_bytes%4)%4, + buf_size = cimg::min(cimg::abs(dy)*(dx_bytes + align_bytes),file_size - offset); + + CImg colormap; + if (bpp<16) { if (!nb_colors) nb_colors = 1<0) std::fseek(nfile,xoffset,SEEK_CUR); + + CImg buffer; + if (buf_size=0; --y) { + if (buf_size>=cimg_iobuffer) { + cimg::fread(ptrs=buffer._data,dx_bytes,nfile); + std::fseek(nfile,align_bytes,SEEK_CUR); + } + unsigned char mask = 0x80, val = 0; + cimg_forX(*this,x) { + if (mask==0x80) val = *(ptrs++); + const unsigned char *col = (unsigned char*)(colormap._data + (val&mask?1:0)); + (*this)(x,y,2) = (T)*(col++); + (*this)(x,y,1) = (T)*(col++); + (*this)(x,y,0) = (T)*(col++); + mask = cimg::ror(mask); + } + ptrs+=align_bytes; + } + } break; + case 4 : { // 16 colors + for (int y = height()-1; y>=0; --y) { + if (buf_size>=cimg_iobuffer) { + cimg::fread(ptrs=buffer._data,dx_bytes,nfile); + std::fseek(nfile,align_bytes,SEEK_CUR); + } + unsigned char mask = 0xF0, val = 0; + cimg_forX(*this,x) { + if (mask==0xF0) val = *(ptrs++); + const unsigned char color = (unsigned char)((mask<16)?(val&mask):((val&mask)>>4)); + const unsigned char *col = (unsigned char*)(colormap._data + color); + (*this)(x,y,2) = (T)*(col++); + (*this)(x,y,1) = (T)*(col++); + (*this)(x,y,0) = (T)*(col++); + mask = cimg::ror(mask,4); + } + ptrs+=align_bytes; + } + } break; + case 8 : { // 256 colors + for (int y = height()-1; y>=0; --y) { + if (buf_size>=cimg_iobuffer) { + cimg::fread(ptrs=buffer._data,dx_bytes,nfile); + std::fseek(nfile,align_bytes,SEEK_CUR); + } + cimg_forX(*this,x) { + const unsigned char *col = (unsigned char*)(colormap._data + *(ptrs++)); + (*this)(x,y,2) = (T)*(col++); + (*this)(x,y,1) = (T)*(col++); + (*this)(x,y,0) = (T)*(col++); + } + ptrs+=align_bytes; + } + } break; + case 16 : { // 16 bits colors + for (int y = height()-1; y>=0; --y) { + if (buf_size>=cimg_iobuffer) { + cimg::fread(ptrs=buffer._data,dx_bytes,nfile); + std::fseek(nfile,align_bytes,SEEK_CUR); + } + cimg_forX(*this,x) { + const unsigned char c1 = *(ptrs++), c2 = *(ptrs++); + const unsigned short col = (unsigned short)(c1|(c2<<8)); + (*this)(x,y,2) = (T)(col&0x1F); + (*this)(x,y,1) = (T)((col>>5)&0x1F); + (*this)(x,y,0) = (T)((col>>10)&0x1F); + } + ptrs+=align_bytes; + } + } break; + case 24 : { // 24 bits colors + for (int y = height()-1; y>=0; --y) { + if (buf_size>=cimg_iobuffer) { + cimg::fread(ptrs=buffer._data,dx_bytes,nfile); + std::fseek(nfile,align_bytes,SEEK_CUR); + } + cimg_forX(*this,x) { + (*this)(x,y,2) = (T)*(ptrs++); + (*this)(x,y,1) = (T)*(ptrs++); + (*this)(x,y,0) = (T)*(ptrs++); + } + ptrs+=align_bytes; + } + } break; + case 32 : { // 32 bits colors + for (int y = height()-1; y>=0; --y) { + if (buf_size>=cimg_iobuffer) { + cimg::fread(ptrs=buffer._data,dx_bytes,nfile); + std::fseek(nfile,align_bytes,SEEK_CUR); + } + cimg_forX(*this,x) { + (*this)(x,y,2) = (T)*(ptrs++); + (*this)(x,y,1) = (T)*(ptrs++); + (*this)(x,y,0) = (T)*(ptrs++); + ++ptrs; + } + ptrs+=align_bytes; + } + } break; + } + if (dy<0) mirror('y'); + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a JPEG file. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_jpeg(const char *const filename) { + return _load_jpeg(0,filename); + } + + //! Load image from a JPEG file \newinstance. + static CImg get_load_jpeg(const char *const filename) { + return CImg().load_jpeg(filename); + } + + //! Load image from a JPEG file \overloading. + CImg& load_jpeg(std::FILE *const file) { + return _load_jpeg(file,0); + } + + //! Load image from a JPEG file \newinstance. + static CImg get_load_jpeg(std::FILE *const file) { + return CImg().load_jpeg(file); + } + + // Custom error handler for libjpeg. +#ifdef cimg_use_jpeg + struct _cimg_error_mgr { + struct jpeg_error_mgr original; + jmp_buf setjmp_buffer; + char message[JMSG_LENGTH_MAX]; + }; + + typedef struct _cimg_error_mgr *_cimg_error_ptr; + + METHODDEF(void) _cimg_jpeg_error_exit(j_common_ptr cinfo) { + _cimg_error_ptr c_err = (_cimg_error_ptr) cinfo->err; // Return control to the setjmp point + (*cinfo->err->format_message)(cinfo,c_err->message); + jpeg_destroy(cinfo); // Clean memory and temp files. + longjmp(c_err->setjmp_buffer,1); + } +#endif + + CImg& _load_jpeg(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_jpeg(): Specified filename is (null).", + cimg_instance); + +#ifndef cimg_use_jpeg + if (file) + throw CImgIOException(_cimg_instance + "load_jpeg(): Unable to load data from '(FILE*)' unless libjpeg is enabled.", + cimg_instance); + else return load_other(filename); +#else + + struct jpeg_decompress_struct cinfo; + struct _cimg_error_mgr jerr; + cinfo.err = jpeg_std_error(&jerr.original); + jerr.original.error_exit = _cimg_jpeg_error_exit; + + if (setjmp(jerr.setjmp_buffer)) { // JPEG error + throw CImgIOException(_cimg_instance + "load_jpeg(): Error message returned by libjpeg: %s.", + cimg_instance,jerr.message); + } + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo,nfile); + jpeg_read_header(&cinfo,TRUE); + jpeg_start_decompress(&cinfo); + + if (cinfo.output_components!=1 && cinfo.output_components!=3 && cinfo.output_components!=4) { + if (!file) { + cimg::fclose(nfile); + return load_other(filename); + } else + throw CImgIOException(_cimg_instance + "load_jpeg(): Failed to load JPEG data from file '%s'.", + cimg_instance,filename?filename:"(FILE*)"); + } + CImg buffer(cinfo.output_width*cinfo.output_components); + JSAMPROW row_pointer[1]; + assign(cinfo.output_width,cinfo.output_height,1,cinfo.output_components); + T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height, *ptr_b = _data + 2UL*_width*_height, + *ptr_a = _data + 3UL*_width*_height; + while (cinfo.output_scanline + // This is experimental code, not much tested, use with care. + CImg& load_magick(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_magick(): Specified filename is (null).", + cimg_instance); +#ifdef cimg_use_magick + Magick::Image image(filename); + const unsigned int W = image.size().width(), H = image.size().height(); + switch (image.type()) { + case Magick::PaletteMatteType : + case Magick::TrueColorMatteType : + case Magick::ColorSeparationType : { + assign(W,H,1,4); + T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3); + Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); + for (unsigned long off = (unsigned long)W*H; off; --off) { + *(ptr_r++) = (T)(pixels->red); + *(ptr_g++) = (T)(pixels->green); + *(ptr_b++) = (T)(pixels->blue); + *(ptr_a++) = (T)(pixels->opacity); + ++pixels; + } + } break; + case Magick::PaletteType : + case Magick::TrueColorType : { + assign(W,H,1,3); + T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); + Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); + for (unsigned long off = (unsigned long)W*H; off; --off) { + *(ptr_r++) = (T)(pixels->red); + *(ptr_g++) = (T)(pixels->green); + *(ptr_b++) = (T)(pixels->blue); + ++pixels; + } + } break; + case Magick::GrayscaleMatteType : { + assign(W,H,1,2); + T *ptr_r = data(0,0,0,0), *ptr_a = data(0,0,0,1); + Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); + for (unsigned long off = (unsigned long)W*H; off; --off) { + *(ptr_r++) = (T)(pixels->red); + *(ptr_a++) = (T)(pixels->opacity); + ++pixels; + } + } break; + default : { + assign(W,H,1,1); + T *ptr_r = data(0,0,0,0); + Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); + for (unsigned long off = (unsigned long)W*H; off; --off) { + *(ptr_r++) = (T)(pixels->red); + ++pixels; + } + } + } + return *this; +#else + throw CImgIOException(_cimg_instance + "load_magick(): Unable to load file '%s' unless libMagick++ is enabled.", + cimg_instance, + filename); +#endif + } + + //! Load image from a file, using Magick++ library \newinstance. + static CImg get_load_magick(const char *const filename) { + return CImg().load_magick(filename); + } + + //! Load image from a PNG file. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_png(const char *const filename) { + return _load_png(0,filename); + } + + //! Load image from a PNG file \newinstance. + static CImg get_load_png(const char *const filename) { + return CImg().load_png(filename); + } + + //! Load image from a PNG file \overloading. + CImg& load_png(std::FILE *const file) { + return _load_png(file,0); + } + + //! Load image from a PNG file \newinstance. + static CImg get_load_png(std::FILE *const file) { + return CImg().load_png(file); + } + + // (Note: Most of this function has been written by Eric Fausett) + CImg& _load_png(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_png(): Specified filename is (null).", + cimg_instance); + +#ifndef cimg_use_png + if (file) + throw CImgIOException(_cimg_instance + "load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.", + cimg_instance); + + else return load_other(filename); +#else + // Open file and check for PNG validity + const char *volatile nfilename = filename; // two 'volatile' here to remove a g++ warning due to 'setjmp'. + std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb"); + + unsigned char pngCheck[8] = { 0 }; + cimg::fread(pngCheck,8,(std::FILE*)nfile); + if (png_sig_cmp(pngCheck,0,8)) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_png(): Invalid PNG file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + + // Setup PNG structures for read + png_voidp user_error_ptr = 0; + png_error_ptr user_error_fn = 0, user_warning_fn = 0; + png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn); + if (!png_ptr) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_png(): Failed to initialize 'png_ptr' structure for file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + png_infop info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + if (!file) cimg::fclose(nfile); + png_destroy_read_struct(&png_ptr,(png_infopp)0,(png_infopp)0); + throw CImgIOException(_cimg_instance + "load_png(): Failed to initialize 'info_ptr' structure for file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + png_infop end_info = png_create_info_struct(png_ptr); + if (!end_info) { + if (!file) cimg::fclose(nfile); + png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)0); + throw CImgIOException(_cimg_instance + "load_png(): Failed to initialize 'end_info' structure for file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + + // Error handling callback for png file reading + if (setjmp(png_jmpbuf(png_ptr))) { + if (!file) cimg::fclose((std::FILE*)nfile); + png_destroy_read_struct(&png_ptr, &end_info, (png_infopp)0); + throw CImgIOException(_cimg_instance + "load_png(): Encountered unknown fatal error in libpng for file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + png_init_io(png_ptr, nfile); + png_set_sig_bytes(png_ptr, 8); + + // Get PNG Header Info up to data block + png_read_info(png_ptr,info_ptr); + png_uint_32 W, H; + int bit_depth, color_type, interlace_type; + bool is_gray = false; + png_get_IHDR(png_ptr,info_ptr,&W,&H,&bit_depth,&color_type,&interlace_type,(int*)0,(int*)0); + + // Transforms to unify image data + if (color_type==PNG_COLOR_TYPE_PALETTE) { + png_set_palette_to_rgb(png_ptr); + color_type = PNG_COLOR_TYPE_RGB; + bit_depth = 8; + } + if (color_type==PNG_COLOR_TYPE_GRAY && bit_depth<8) { + png_set_expand_gray_1_2_4_to_8(png_ptr); + is_gray = true; + bit_depth = 8; + } + if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) { + png_set_tRNS_to_alpha(png_ptr); + color_type |= PNG_COLOR_MASK_ALPHA; + } + if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) { + png_set_gray_to_rgb(png_ptr); + color_type |= PNG_COLOR_MASK_COLOR; + is_gray = true; + } + if (color_type==PNG_COLOR_TYPE_RGB) + png_set_filler(png_ptr,0xffffU,PNG_FILLER_AFTER); + + png_read_update_info(png_ptr,info_ptr); + if (bit_depth!=8 && bit_depth!=16) { + if (!file) cimg::fclose(nfile); + png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0); + throw CImgIOException(_cimg_instance + "load_png(): Invalid bit depth %u in file '%s'.", + cimg_instance, + bit_depth,nfilename?nfilename:"(FILE*)"); + } + const int byte_depth = bit_depth>>3; + + // Allocate Memory for Image Read + png_bytep *const imgData = new png_bytep[H]; + for (unsigned int row = 0; row& load_pnm(const char *const filename) { + return _load_pnm(0,filename); + } + + //! Load image from a PNM file \newinstance. + static CImg get_load_pnm(const char *const filename) { + return CImg().load_pnm(filename); + } + + //! Load image from a PNM file \overloading. + CImg& load_pnm(std::FILE *const file) { + return _load_pnm(file,0); + } + + //! Load image from a PNM file \newinstance. + static CImg get_load_pnm(std::FILE *const file) { + return CImg().load_pnm(file); + } + + CImg& _load_pnm(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_pnm(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + unsigned int ppm_type, W, H, D = 1, colormax = 255; + CImg item(16384,1,1,1,0); + int err, rval, gval, bval; + const long cimg_iobuffer = 12*1024*1024; + while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); + if (std::sscanf(item," P%u",&ppm_type)!=1) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pnm(): PNM header not found in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); + if ((err=std::sscanf(item," %u %u %u %u",&W,&H,&D,&colormax))<2) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + if (ppm_type!=1 && ppm_type!=4) { + if (err==2 || (err==3 && (ppm_type==5 || ppm_type==7 || ppm_type==8 || ppm_type==9))) { + while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); + if (std::sscanf(item,"%u",&colormax)!=1) + cimg::warn(_cimg_instance + "load_pnm(): COLORMAX field is undefined in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } else { colormax = D; D = 1; } + } + std::fgetc(nfile); + + switch (ppm_type) { + case 1 : { // 2d b&w ascii. + assign(W,H,1,1); + T* ptrd = _data; + cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)(rval?0:255); else break; } + } break; + case 2 : { // 2d grey ascii. + assign(W,H,1,1); + T* ptrd = _data; + cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)rval; else break; } + } break; + case 3 : { // 2d color ascii. + assign(W,H,1,3); + T *ptrd = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); + cimg_forXY(*this,x,y) { + if (std::fscanf(nfile,"%d %d %d",&rval,&gval,&bval)==3) { + *(ptrd++) = (T)rval; *(ptr_g++) = (T)gval; *(ptr_b++) = (T)bval; + } else break; + } + } break; + case 4 : { // 2d b&w binary (support 3D PINK extension). + CImg raw; + assign(W,H,D,1); + T *ptrd = data(0,0,0,0); + unsigned int w = 0, h = 0, d = 0; + for (long to_read = (long)((W/8 + (W%8?1:0))*H*D); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const unsigned char *ptrs = raw._data; + unsigned char mask = 0, val = 0; + for (unsigned long off = (unsigned long)raw._width; off || mask; mask>>=1) { + if (!mask) { if (off--) val = *(ptrs++); mask = 128; } + *(ptrd++) = (T)((val&mask)?0:255); + if (++w==W) { w = 0; mask = 0; if (++h==H) { h = 0; if (++d==D) break; }} + } + } + } break; + case 5 : case 7 : { // 2d/3d grey binary (support 3D PINK extension). + if (colormax<256) { // 8 bits. + CImg raw; + assign(W,H,D,1); + T *ptrd = data(0,0,0,0); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const unsigned char *ptrs = raw._data; + for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); + } + } else { // 16 bits. + CImg raw; + assign(W,H,D,1); + T *ptrd = data(0,0,0,0); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer/2)); + cimg::fread(raw._data,raw._width,nfile); + if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); + to_read-=raw._width; + const unsigned short *ptrs = raw._data; + for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); + } + } + } break; + case 6 : { // 2d color binary. + if (colormax<256) { // 8 bits. + CImg raw; + assign(W,H,1,3); + T + *ptr_r = data(0,0,0,0), + *ptr_g = data(0,0,0,1), + *ptr_b = data(0,0,0,2); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const unsigned char *ptrs = raw._data; + for (unsigned long off = (unsigned long)raw._width/3; off; --off) { + *(ptr_r++) = (T)*(ptrs++); + *(ptr_g++) = (T)*(ptrs++); + *(ptr_b++) = (T)*(ptrs++); + } + } + } else { // 16 bits. + CImg raw; + assign(W,H,1,3); + T + *ptr_r = data(0,0,0,0), + *ptr_g = data(0,0,0,1), + *ptr_b = data(0,0,0,2); + for (long to_read = (int)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer/2)); + cimg::fread(raw._data,raw._width,nfile); + if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); + to_read-=raw._width; + const unsigned short *ptrs = raw._data; + for (unsigned long off = (unsigned long)raw._width/3; off; --off) { + *(ptr_r++) = (T)*(ptrs++); + *(ptr_g++) = (T)*(ptrs++); + *(ptr_b++) = (T)*(ptrs++); + } + } + } + } break; + case 8 : { // 2d/3d grey binary with int32 integers (PINK extension). + CImg raw; + assign(W,H,D,1); + T *ptrd = data(0,0,0,0); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const int *ptrs = raw._data; + for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); + } + } break; + case 9 : { // 2d/3d grey binary with float values (PINK extension). + CImg raw; + assign(W,H,D,1); + T *ptrd = data(0,0,0,0); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const float *ptrs = raw._data; + for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); + } + } break; + default : + assign(); + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pnm(): PNM type 'P%d' found, but type is not supported.", + cimg_instance, + filename?filename:"(FILE*)",ppm_type); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a PFM file. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_pfm(const char *const filename) { + return _load_pfm(0,filename); + } + + //! Load image from a PFM file \newinstance. + static CImg get_load_pfm(const char *const filename) { + return CImg().load_pfm(filename); + } + + //! Load image from a PFM file \overloading. + CImg& load_pfm(std::FILE *const file) { + return _load_pfm(file,0); + } + + //! Load image from a PFM file \newinstance. + static CImg get_load_pfm(std::FILE *const file) { + return CImg().load_pfm(file); + } + + CImg& _load_pfm(std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_pfm(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + char pfm_type; + CImg item(16384,1,1,1,0); + int W = 0, H = 0, err = 0; + double scale = 0; + while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); + if (std::sscanf(item," P%c",&pfm_type)!=1) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pfm(): PFM header not found in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); + if ((err=std::sscanf(item," %d %d",&W,&H))<2) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + if (err==2) { + while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); + if (std::sscanf(item,"%lf",&scale)!=1) + cimg::warn(_cimg_instance + "load_pfm(): SCALE field is undefined in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + std::fgetc(nfile); + const bool is_color = (pfm_type=='F'), is_inverted = (scale>0)!=cimg::endianness(); + if (is_color) { + assign(W,H,1,3,0); + CImg buf(3*W); + T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); + cimg_forY(*this,y) { + cimg::fread(buf._data,3*W,nfile); + if (is_inverted) cimg::invert_endianness(buf._data,3*W); + const float *ptrs = buf._data; + cimg_forX(*this,x) { + *(ptr_r++) = (T)*(ptrs++); + *(ptr_g++) = (T)*(ptrs++); + *(ptr_b++) = (T)*(ptrs++); + } + } + } else { + assign(W,H,1,1,0); + CImg buf(W); + T *ptrd = data(0,0,0,0); + cimg_forY(*this,y) { + cimg::fread(buf._data,W,nfile); + if (is_inverted) cimg::invert_endianness(buf._data,W); + const float *ptrs = buf._data; + cimg_forX(*this,x) *(ptrd++) = (T)*(ptrs++); + } + } + if (!file) cimg::fclose(nfile); + return mirror('y'); // Most of the .pfm files are flipped along the y-axis. + } + + //! Load image from a RGB file. + /** + \param filename Filename, as a C-string. + \param dimw Width of the image buffer. + \param dimh Height of the image buffer. + **/ + CImg& load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { + return _load_rgb(0,filename,dimw,dimh); + } + + //! Load image from a RGB file \newinstance. + static CImg get_load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { + return CImg().load_rgb(filename,dimw,dimh); + } + + //! Load image from a RGB file \overloading. + CImg& load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { + return _load_rgb(file,0,dimw,dimh); + } + + //! Load image from a RGB file \newinstance. + static CImg get_load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { + return CImg().load_rgb(file,dimw,dimh); + } + + CImg& _load_rgb(std::FILE *const file, const char *const filename, + const unsigned int dimw, const unsigned int dimh) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_rgb(): Specified filename is (null).", + cimg_instance); + + if (!dimw || !dimh) return assign(); + const long cimg_iobuffer = 12*1024*1024; + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + CImg raw; + assign(dimw,dimh,1,3); + T + *ptr_r = data(0,0,0,0), + *ptr_g = data(0,0,0,1), + *ptr_b = data(0,0,0,2); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const unsigned char *ptrs = raw._data; + for (unsigned long off = raw._width/3UL; off; --off) { + *(ptr_r++) = (T)*(ptrs++); + *(ptr_g++) = (T)*(ptrs++); + *(ptr_b++) = (T)*(ptrs++); + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a RGBA file. + /** + \param filename Filename, as a C-string. + \param dimw Width of the image buffer. + \param dimh Height of the image buffer. + **/ + CImg& load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { + return _load_rgba(0,filename,dimw,dimh); + } + + //! Load image from a RGBA file \newinstance. + static CImg get_load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { + return CImg().load_rgba(filename,dimw,dimh); + } + + //! Load image from a RGBA file \overloading. + CImg& load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { + return _load_rgba(file,0,dimw,dimh); + } + + //! Load image from a RGBA file \newinstance. + static CImg get_load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { + return CImg().load_rgba(file,dimw,dimh); + } + + CImg& _load_rgba(std::FILE *const file, const char *const filename, + const unsigned int dimw, const unsigned int dimh) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_rgba(): Specified filename is (null).", + cimg_instance); + + if (!dimw || !dimh) return assign(); + const long cimg_iobuffer = 12*1024*1024; + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + CImg raw; + assign(dimw,dimh,1,4); + T + *ptr_r = data(0,0,0,0), + *ptr_g = data(0,0,0,1), + *ptr_b = data(0,0,0,2), + *ptr_a = data(0,0,0,3); + for (long to_read = (long)size(); to_read>0; ) { + raw.assign(cimg::min(to_read,cimg_iobuffer)); + cimg::fread(raw._data,raw._width,nfile); + to_read-=raw._width; + const unsigned char *ptrs = raw._data; + for (unsigned long off = raw._width/4UL; off; --off) { + *(ptr_r++) = (T)*(ptrs++); + *(ptr_g++) = (T)*(ptrs++); + *(ptr_b++) = (T)*(ptrs++); + *(ptr_a++) = (T)*(ptrs++); + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a TIFF file. + /** + \param filename Filename, as a C-string. + \param first_frame First frame to read (for multi-pages tiff). + \param last_frame Last frame to read (for multi-pages tiff). + \param step_frame Step value of frame reading. + \note + - libtiff support is enabled by defining the precompilation + directive \c cimg_use_tif. + - When libtiff is enabled, 2D and 3D (multipage) several + channel per pixel are supported for + char,uchar,short,ushort,float and \c double pixel types. + - If \c cimg_use_tif is not defined at compilation time the + function uses CImg& load_other(const char*). + **/ + CImg& load_tiff(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, + float *const voxel_size=0) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_tiff(): Specified filename is (null).", + cimg_instance); + + const unsigned int + nfirst_frame = first_frame1) + throw CImgArgumentException(_cimg_instance + "load_tiff(): Unable to read sub-images from file '%s' unless libtiff is enabled.", + cimg_instance, + filename); + return load_other(filename); +#else + TIFF *tif = TIFFOpen(filename,"r"); + if (tif) { + unsigned int nb_images = 0; + do ++nb_images; while (TIFFReadDirectory(tif)); + if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images)) + cimg::warn(_cimg_instance + "load_tiff(): File '%s' contains %u image(s) while specified frame range is [%u,%u] (step %u).", + cimg_instance, + filename,nb_images,nfirst_frame,nlast_frame,nstep_frame); + + if (nfirst_frame>=nb_images) return assign(); + if (nlast_frame>=nb_images) nlast_frame = nb_images-1; + TIFFSetDirectory(tif,0); + CImg frame; + for (unsigned int l = nfirst_frame; l<=nlast_frame; l+=nstep_frame) { + frame._load_tiff(tif,l,voxel_size); + if (l==nfirst_frame) + assign(frame._width,frame._height,1+(nlast_frame-nfirst_frame)/nstep_frame,frame._spectrum); + if (frame._width>_width || frame._height>_height || frame._spectrum>_spectrum) + resize(cimg::max(frame._width,_width), + cimg::max(frame._height,_height),-100, + cimg::max(frame._spectrum,_spectrum),0); + draw_image(0,0,(l-nfirst_frame)/nstep_frame,frame); + } + TIFFClose(tif); + } else throw CImgIOException(_cimg_instance + "load_tiff(): Failed to open file '%s'.", + cimg_instance, + filename); + return *this; +#endif + } + + //! Load image from a TIFF file \newinstance. + static CImg get_load_tiff(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, + float *const voxel_size=0) { + return CImg().load_tiff(filename,first_frame,last_frame,step_frame,voxel_size); + } + + // (Original contribution by Jerome Boulanger). +#ifdef cimg_use_tiff + template + void _load_tiff_tiled_contig(TIFF *const tif, const uint16 samplesperpixel, + const uint32 nx, const uint32 ny, const uint32 tw, const uint32 th) { + t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif)); + if (buf) { + for (unsigned int row = 0; row + void _load_tiff_tiled_separate(TIFF *const tif, const uint16 samplesperpixel, + const uint32 nx, const uint32 ny, const uint32 tw, const uint32 th) { + t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif)); + if (buf) { + for (unsigned int vv = 0; vv + void _load_tiff_contig(TIFF *const tif, const uint16 samplesperpixel, const uint32 nx, const uint32 ny) { + t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif)); + if (buf) { + uint32 row, rowsperstrip = (uint32)-1; + TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip); + for (row = 0; rowny?ny-row:rowsperstrip); + tstrip_t strip = TIFFComputeStrip(tif, row, 0); + if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) { + _TIFFfree(buf); TIFFClose(tif); + throw CImgIOException(_cimg_instance + "load_tiff(): Invalid strip in file '%s'.", + cimg_instance, + TIFFFileName(tif)); + } + const t *ptr = buf; + for (unsigned int rr = 0; rr + void _load_tiff_separate(TIFF *const tif, const uint16 samplesperpixel, const uint32 nx, const uint32 ny) { + t *buf = (t*)_TIFFmalloc(TIFFStripSize(tif)); + if (buf) { + uint32 row, rowsperstrip = (uint32)-1; + TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip); + for (unsigned int vv = 0; vvny?ny-row:rowsperstrip); + tstrip_t strip = TIFFComputeStrip(tif, row, vv); + if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) { + _TIFFfree(buf); TIFFClose(tif); + throw CImgIOException(_cimg_instance + "load_tiff(): Invalid strip in file '%s'.", + cimg_instance, + TIFFFileName(tif)); + } + const t *ptr = buf; + for (unsigned int rr = 0;rr& _load_tiff(TIFF *const tif, const unsigned int directory, float *const voxel_size) { + if (!TIFFSetDirectory(tif,directory)) return assign(); + uint16 samplesperpixel = 1, bitspersample = 8, photo = 0; + uint16 sampleformat = 1; + uint32 nx = 1, ny = 1; + const char *const filename = TIFFFileName(tif); + const bool is_spp = (bool)TIFFGetField(tif,TIFFTAG_SAMPLESPERPIXEL,&samplesperpixel); + TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&nx); + TIFFGetField(tif,TIFFTAG_IMAGELENGTH,&ny); + TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleformat); + TIFFGetFieldDefaulted(tif,TIFFTAG_BITSPERSAMPLE,&bitspersample); + TIFFGetField(tif,TIFFTAG_PHOTOMETRIC,&photo); + if (voxel_size) { + const char *s_description = 0; + float vx = 0, vy = 0, vz = 0; + if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description) { + const char *s_desc = std::strstr(s_description,"VX="); + if (s_desc && std::sscanf(s_desc,"VX=%f VY=%f VZ=%f",&vx,&vy,&vz)==3) { // CImg format. + voxel_size[0] = vx; voxel_size[1] = vy; voxel_size[2] = vz; + } + s_desc = std::strstr(s_description,"spacing="); + if (s_desc && std::sscanf(s_desc,"spacing=%f",&vz)==1) { // fiji format. + voxel_size[2] = vz; + } + } + TIFFGetField(tif,TIFFTAG_XRESOLUTION,voxel_size); + TIFFGetField(tif,TIFFTAG_YRESOLUTION,voxel_size+1); + } + const unsigned int spectrum = is_spp?samplesperpixel:photo==3?3:1; + assign(nx,ny,1,spectrum); + + if (photo>=3 && sampleformat==1 && bitspersample==8 && (samplesperpixel==3 || samplesperpixel==4)) { + // Special case for unsigned color images. + uint32 *const raster = (uint32*)_TIFFmalloc(nx*ny*sizeof(uint32)); + if (!raster) { + _TIFFfree(raster); TIFFClose(tif); + throw CImgException(_cimg_instance + "load_tiff(): Failed to allocate memory (%s) for file '%s'.", + cimg_instance, + cimg::strbuffersize(nx*ny*sizeof(uint32)),filename); + } + TIFFReadRGBAImage(tif,nx,ny,raster,0); + switch (spectrum) { + case 1 : { + cimg_forXY(*this,x,y) (*this)(x,y) = (T)(float)((raster[nx*(ny-1-y)+x] + 128)/257); + } break; + case 3 : { + cimg_forXY(*this,x,y) { + (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny-1-y)+x]); + (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny-1-y)+x]); + (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny-1-y)+x]); + } + } break; + case 4 : { + cimg_forXY(*this,x,y) { + (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny-1-y)+x]); + (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny-1-y)+x]); + (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny-1-y)+x]); + (*this)(x,y,3) = (T)(float)TIFFGetA(raster[nx*(ny-1-y)+x]); + } + } break; + } + _TIFFfree(raster); + } else { // Other cases. + uint16 config; + TIFFGetField(tif,TIFFTAG_PLANARCONFIG,&config); + if (TIFFIsTiled(tif)) { + uint32 tw = 1, th = 1; + TIFFGetField(tif,TIFFTAG_TILEWIDTH,&tw); + TIFFGetField(tif,TIFFTAG_TILELENGTH,&th); + if (config==PLANARCONFIG_CONTIG) switch (bitspersample) { + case 8 : { + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + else _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + } break; + case 16 : + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + else _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + break; + case 32 : + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + else if (sampleformat==SAMPLEFORMAT_INT) + _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + else _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); + break; + } else switch (bitspersample) { + case 8 : + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + else _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + break; + case 16 : + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + else _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + break; + case 32 : + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + else if (sampleformat==SAMPLEFORMAT_INT) + _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + else _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); + break; + } + } else { + if (config==PLANARCONFIG_CONTIG) switch (bitspersample) { + case 8 : + if (sampleformat==SAMPLEFORMAT_UINT) + _load_tiff_contig(tif,samplesperpixel,nx,ny); + else _load_tiff_contig(tif,samplesperpixel,nx,ny); + break; + case 16 : + if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig(tif,samplesperpixel,nx,ny); + else _load_tiff_contig(tif,samplesperpixel,nx,ny); + break; + case 32 : + if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig(tif,samplesperpixel,nx,ny); + else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_contig(tif,samplesperpixel,nx,ny); + else _load_tiff_contig(tif,samplesperpixel,nx,ny); + break; + } else switch (bitspersample) { + case 8 : + if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate(tif,samplesperpixel,nx,ny); + else _load_tiff_separate(tif,samplesperpixel,nx,ny); + break; + case 16 : + if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate(tif,samplesperpixel,nx,ny); + else _load_tiff_separate(tif,samplesperpixel,nx,ny); + break; + case 32 : + if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate(tif,samplesperpixel,nx,ny); + else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_separate(tif,samplesperpixel,nx,ny); + else _load_tiff_separate(tif,samplesperpixel,nx,ny); + break; + } + } + } + return *this; + } +#endif + + //! Load image from a MINC2 file. + /** + \param filename Filename, as a C-string. + **/ + // (Original code by Haz-Edine Assemlal). + CImg& load_minc2(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_minc2(): Specified filename is (null).", + cimg_instance); +#ifndef cimg_use_minc2 + return load_other(filename); +#else + minc::minc_1_reader rdr; + rdr.open(filename); + assign(rdr.ndim(1)?rdr.ndim(1):1, + rdr.ndim(2)?rdr.ndim(2):1, + rdr.ndim(3)?rdr.ndim(3):1, + rdr.ndim(4)?rdr.ndim(4):1); + if(typeid(T)==typeid(unsigned char)) + rdr.setup_read_byte(); + else if(typeid(T)==typeid(int)) + rdr.setup_read_int(); + else if(typeid(T)==typeid(double)) + rdr.setup_read_double(); + else + rdr.setup_read_float(); + minc::load_standard_volume(rdr, this->_data); + return *this; +#endif + } + + //! Load image from a MINC2 file \newinstance. + static CImg get_load_minc2(const char *const filename) { + return CImg().load_analyze(filename); + } + + //! Load image from an ANALYZE7.5/NIFTI file. + /** + \param filename Filename, as a C-string. + \param[out] voxel_size Pointer to the three voxel sizes read from the file. + **/ + CImg& load_analyze(const char *const filename, float *const voxel_size=0) { + return _load_analyze(0,filename,voxel_size); + } + + //! Load image from an ANALYZE7.5/NIFTI file \newinstance. + static CImg get_load_analyze(const char *const filename, float *const voxel_size=0) { + return CImg().load_analyze(filename,voxel_size); + } + + //! Load image from an ANALYZE7.5/NIFTI file \overloading. + CImg& load_analyze(std::FILE *const file, float *const voxel_size=0) { + return _load_analyze(file,0,voxel_size); + } + + //! Load image from an ANALYZE7.5/NIFTI file \newinstance. + static CImg get_load_analyze(std::FILE *const file, float *const voxel_size=0) { + return CImg().load_analyze(file,voxel_size); + } + + CImg& _load_analyze(std::FILE *const file, const char *const filename, float *const voxel_size=0) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_analyze(): Specified filename is (null).", + cimg_instance); + + std::FILE *nfile_header = 0, *nfile = 0; + if (!file) { + char body[1024] = { 0 }; + const char *const ext = cimg::split_filename(filename,body); + if (!cimg::strcasecmp(ext,"hdr")) { // File is an Analyze header file. + nfile_header = cimg::fopen(filename,"rb"); + std::sprintf(body + std::strlen(body),".img"); + nfile = cimg::fopen(body,"rb"); + } else if (!cimg::strcasecmp(ext,"img")) { // File is an Analyze data file. + nfile = cimg::fopen(filename,"rb"); + std::sprintf(body + std::strlen(body),".hdr"); + nfile_header = cimg::fopen(body,"rb"); + } else nfile_header = nfile = cimg::fopen(filename,"rb"); // File is a Niftii file. + } else nfile_header = nfile = file; // File is a Niftii file. + if (!nfile || !nfile_header) + throw CImgIOException(_cimg_instance + "load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + // Read header. + bool endian = false; + unsigned int header_size; + cimg::fread(&header_size,1,nfile_header); + if (!header_size) + throw CImgIOException(_cimg_instance + "load_analyze(): Invalid zero-sized header in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + if (header_size>=4096) { endian = true; cimg::invert_endianness(header_size); } + unsigned char *const header = new unsigned char[header_size]; + cimg::fread(header+4,header_size-4,nfile_header); + if (!file && nfile_header!=nfile) cimg::fclose(nfile_header); + if (endian) { + cimg::invert_endianness((short*)(header+40),5); + cimg::invert_endianness((short*)(header+70),1); + cimg::invert_endianness((short*)(header+72),1); + cimg::invert_endianness((float*)(header+76),4); + cimg::invert_endianness((float*)(header+112),1); + } + unsigned short *dim = (unsigned short*)(header+40), dimx = 1, dimy = 1, dimz = 1, dimv = 1; + if (!dim[0]) + cimg::warn(_cimg_instance + "load_analyze(): File '%s' defines an image with zero dimensions.", + cimg_instance, + filename?filename:"(FILE*)"); + + if (dim[0]>4) + cimg::warn(_cimg_instance + "load_analyze(): File '%s' defines an image with %u dimensions, reading only the 4 first.", + cimg_instance, + filename?filename:"(FILE*)",dim[0]); + + if (dim[0]>=1) dimx = dim[1]; + if (dim[0]>=2) dimy = dim[2]; + if (dim[0]>=3) dimz = dim[3]; + if (dim[0]>=4) dimv = dim[4]; + float scalefactor = *(float*)(header+112); if (scalefactor==0) scalefactor=1; + const unsigned short datatype = *(short*)(header+70); + if (voxel_size) { + const float *vsize = (float*)(header+76); + voxel_size[0] = vsize[1]; voxel_size[1] = vsize[2]; voxel_size[2] = vsize[3]; + } + delete[] header; + + // Read pixel data. + assign(dimx,dimy,dimz,dimv); + switch (datatype) { + case 2 : { + unsigned char *const buffer = new unsigned char[dimx*dimy*dimz*dimv]; + cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); + cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); + delete[] buffer; + } break; + case 4 : { + short *const buffer = new short[dimx*dimy*dimz*dimv]; + cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); + if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); + cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); + delete[] buffer; + } break; + case 8 : { + int *const buffer = new int[dimx*dimy*dimz*dimv]; + cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); + if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); + cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); + delete[] buffer; + } break; + case 16 : { + float *const buffer = new float[dimx*dimy*dimz*dimv]; + cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); + if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); + cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); + delete[] buffer; + } break; + case 64 : { + double *const buffer = new double[dimx*dimy*dimz*dimv]; + cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); + if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); + cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); + delete[] buffer; + } break; + default : + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_analyze(): Unable to load datatype %d in file '%s'", + cimg_instance, + datatype,filename?filename:"(FILE*)"); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a .cimg[z] file. + /** + \param filename Filename, as a C-string. + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg& load_cimg(const char *const filename, const char axis='z', const float align=0) { + CImgList list; + list.load_cimg(filename); + if (list._width==1) return list[0].move_to(*this); + return assign(list.get_append(axis,align)); + } + + //! Load image from a .cimg[z] file \newinstance + static CImg get_load_cimg(const char *const filename, const char axis='z', const float align=0) { + return CImg().load_cimg(filename,axis,align); + } + + //! Load image from a .cimg[z] file \overloading. + CImg& load_cimg(std::FILE *const file, const char axis='z', const float align=0) { + CImgList list; + list.load_cimg(file); + if (list._width==1) return list[0].move_to(*this); + return assign(list.get_append(axis,align)); + } + + //! Load image from a .cimg[z] file \newinstance + static CImg get_load_cimg(std::FILE *const file, const char axis='z', const float align=0) { + return CImg().load_cimg(file,axis,align); + } + + //! Load sub-images of a .cimg file. + /** + \param filename Filename, as a C-string. + \param n0 Starting frame. + \param n1 Ending frame (~0U for max). + \param x0 X-coordinate of the starting sub-image vertex. + \param y0 Y-coordinate of the starting sub-image vertex. + \param z0 Z-coordinate of the starting sub-image vertex. + \param c0 C-coordinate of the starting sub-image vertex. + \param x1 X-coordinate of the ending sub-image vertex (~0U for max). + \param y1 Y-coordinate of the ending sub-image vertex (~0U for max). + \param z1 Z-coordinate of the ending sub-image vertex (~0U for max). + \param c1 C-coordinate of the ending sub-image vertex (~0U for max). + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg& load_cimg(const char *const filename, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1, + const char axis='z', const float align=0) { + CImgList list; + list.load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); + if (list._width==1) return list[0].move_to(*this); + return assign(list.get_append(axis,align)); + } + + //! Load sub-images of a .cimg file \newinstance. + static CImg get_load_cimg(const char *const filename, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1, + const char axis='z', const float align=0) { + return CImg().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align); + } + + //! Load sub-images of a .cimg file \overloading. + CImg& load_cimg(std::FILE *const file, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1, + const char axis='z', const float align=0) { + CImgList list; + list.load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); + if (list._width==1) return list[0].move_to(*this); + return assign(list.get_append(axis,align)); + } + + //! Load sub-images of a .cimg file \newinstance. + static CImg get_load_cimg(std::FILE *const file, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1, + const char axis='z', const float align=0) { + return CImg().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align); + } + + //! Load image from an INRIMAGE-4 file. + /** + \param filename Filename, as a C-string. + \param[out] voxel_size Pointer to the three voxel sizes read from the file. + **/ + CImg& load_inr(const char *const filename, float *const voxel_size=0) { + return _load_inr(0,filename,voxel_size); + } + + //! Load image from an INRIMAGE-4 file \newinstance. + static CImg get_load_inr(const char *const filename, float *const voxel_size=0) { + return CImg().load_inr(filename,voxel_size); + } + + //! Load image from an INRIMAGE-4 file \overloading. + CImg& load_inr(std::FILE *const file, float *const voxel_size=0) { + return _load_inr(file,0,voxel_size); + } + + //! Load image from an INRIMAGE-4 file \newinstance. + static CImg get_load_inr(std::FILE *const file, float *voxel_size=0) { + return CImg().load_inr(file,voxel_size); + } + + static void _load_inr_header(std::FILE *file, int out[8], float *const voxel_size) { + char item[1024] = { 0 }, tmp1[64] = { 0 }, tmp2[64] = { 0 }; + out[0] = std::fscanf(file,"%63s",item); + out[0] = out[1] = out[2] = out[3] = out[5] = 1; out[4] = out[6] = out[7] = -1; + if(cimg::strncasecmp(item,"#INRIMAGE-4#{",13)!=0) + throw CImgIOException("CImg<%s>::load_inr(): INRIMAGE-4 header not found.", + pixel_type()); + + while (std::fscanf(file," %63[^\n]%*c",item)!=EOF && std::strncmp(item,"##}",3)) { + std::sscanf(item," XDIM%*[^0-9]%d",out); + std::sscanf(item," YDIM%*[^0-9]%d",out+1); + std::sscanf(item," ZDIM%*[^0-9]%d",out+2); + std::sscanf(item," VDIM%*[^0-9]%d",out+3); + std::sscanf(item," PIXSIZE%*[^0-9]%d",out+6); + if (voxel_size) { + std::sscanf(item," VX%*[^0-9.+-]%f",voxel_size); + std::sscanf(item," VY%*[^0-9.+-]%f",voxel_size+1); + std::sscanf(item," VZ%*[^0-9.+-]%f",voxel_size+2); + } + if (std::sscanf(item," CPU%*[ =]%s",tmp1)) out[7]=cimg::strncasecmp(tmp1,"sun",3)?0:1; + switch (std::sscanf(item," TYPE%*[ =]%s %s",tmp1,tmp2)) { + case 0 : break; + case 2 : out[5] = cimg::strncasecmp(tmp1,"unsigned",8)?1:0; std::strncpy(tmp1,tmp2,sizeof(tmp1)-1); + case 1 : + if (!cimg::strncasecmp(tmp1,"int",3) || !cimg::strncasecmp(tmp1,"fixed",5)) out[4] = 0; + if (!cimg::strncasecmp(tmp1,"float",5) || !cimg::strncasecmp(tmp1,"double",6)) out[4] = 1; + if (!cimg::strncasecmp(tmp1,"packed",6)) out[4] = 2; + if (out[4]>=0) break; + default : + throw CImgIOException("CImg<%s>::load_inr(): Invalid pixel type '%s' defined in header.", + pixel_type(), + tmp2); + } + } + if(out[0]<0 || out[1]<0 || out[2]<0 || out[3]<0) + throw CImgIOException("CImg<%s>::load_inr(): Invalid dimensions (%d,%d,%d,%d) defined in header.", + pixel_type(), + out[0],out[1],out[2],out[3]); + if(out[4]<0 || out[5]<0) + throw CImgIOException("CImg<%s>::load_inr(): Incomplete pixel type defined in header.", + pixel_type()); + if(out[6]<0) + throw CImgIOException("CImg<%s>::load_inr(): Incomplete PIXSIZE field defined in header.", + pixel_type()); + if(out[7]<0) + throw CImgIOException("CImg<%s>::load_inr(): Big/Little Endian coding type undefined in header.", + pixel_type()); + } + + CImg& _load_inr(std::FILE *const file, const char *const filename, float *const voxel_size) { +#define _cimg_load_inr_case(Tf,sign,pixsize,Ts) \ + if (!loaded && fopt[6]==pixsize && fopt[4]==Tf && fopt[5]==sign) { \ + Ts *xval, *const val = new Ts[fopt[0]*fopt[3]]; \ + cimg_forYZ(*this,y,z) { \ + cimg::fread(val,fopt[0]*fopt[3],nfile); \ + if (fopt[7]!=endian) cimg::invert_endianness(val,fopt[0]*fopt[3]); \ + xval = val; cimg_forX(*this,x) cimg_forC(*this,c) (*this)(x,y,z,c) = (T)*(xval++); \ + } \ + delete[] val; \ + loaded = true; \ + } + + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_inr(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + int fopt[8], endian=cimg::endianness()?1:0; + bool loaded = false; + if (voxel_size) voxel_size[0] = voxel_size[1] = voxel_size[2] = 1; + _load_inr_header(nfile,fopt,voxel_size); + assign(fopt[0],fopt[1],fopt[2],fopt[3]); + _cimg_load_inr_case(0,0,8,unsigned char); + _cimg_load_inr_case(0,1,8,char); + _cimg_load_inr_case(0,0,16,unsigned short); + _cimg_load_inr_case(0,1,16,short); + _cimg_load_inr_case(0,0,32,unsigned int); + _cimg_load_inr_case(0,1,32,int); + _cimg_load_inr_case(1,0,32,float); + _cimg_load_inr_case(1,1,32,float); + _cimg_load_inr_case(1,0,64,double); + _cimg_load_inr_case(1,1,64,double); + if (!loaded) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_inr(): Unknown pixel type defined in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a EXR file. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_exr(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_exr(): Specified filename is (null).", + cimg_instance); + +#ifndef cimg_use_openexr + return load_other(filename); +#else + Imf::RgbaInputFile file(filename); + Imath::Box2i dw = file.dataWindow(); + const int + inwidth = dw.max.x - dw.min.x + 1, + inheight = dw.max.y - dw.min.y + 1; + Imf::Array2D pixels; + pixels.resizeErase(inheight,inwidth); + file.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y*inwidth, 1, inwidth); + file.readPixels(dw.min.y, dw.max.y); + assign(inwidth,inheight,1,4); + T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3); + cimg_forXY(*this,x,y) { + *(ptr_r++) = (T)pixels[y][x].r; + *(ptr_g++) = (T)pixels[y][x].g; + *(ptr_b++) = (T)pixels[y][x].b; + *(ptr_a++) = (T)pixels[y][x].a; + } + return *this; +#endif + } + + //! Load image from a EXR file \newinstance. + static CImg get_load_exr(const char *const filename) { + return CImg().load_exr(filename); + } + + //! Load image from a PANDORE-5 file. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_pandore(const char *const filename) { + return _load_pandore(0,filename); + } + + //! Load image from a PANDORE-5 file \newinstance. + static CImg get_load_pandore(const char *const filename) { + return CImg().load_pandore(filename); + } + + //! Load image from a PANDORE-5 file \overloading. + CImg& load_pandore(std::FILE *const file) { + return _load_pandore(file,0); + } + + //! Load image from a PANDORE-5 file \newinstance. + static CImg get_load_pandore(std::FILE *const file) { + return CImg().load_pandore(file); + } + + CImg& _load_pandore(std::FILE *const file, const char *const filename) { +#define __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,ndim,stype) \ + cimg::fread(dims,nbdim,nfile); \ + if (endian) cimg::invert_endianness(dims,nbdim); \ + assign(nwidth,nheight,ndepth,ndim); \ + const unsigned int siz = size(); \ + stype *buffer = new stype[siz]; \ + cimg::fread(buffer,siz,nfile); \ + if (endian) cimg::invert_endianness(buffer,siz); \ + T *ptrd = _data; \ + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); \ + buffer-=siz; \ + delete[] buffer + +#define _cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1,stype2,stype3,ltype) { \ + if (sizeof(stype1)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1); } \ + else if (sizeof(stype2)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype2); } \ + else if (sizeof(stype3)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype3); } \ + else throw CImgIOException(_cimg_instance \ + "load_pandore(): Unknown pixel datatype in file '%s'.", \ + cimg_instance, \ + filename?filename:"(FILE*)"); } + + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_pandore(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + char header[32] = { 0 }; + cimg::fread(header,12,nfile); + if (cimg::strncasecmp("PANDORE",header,7)) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pandore(): PANDORE header not found in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + unsigned int imageid, dims[8] = { 0 }; + cimg::fread(&imageid,1,nfile); + const bool endian = (imageid>255); + if (endian) cimg::invert_endianness(imageid); + cimg::fread(header,20,nfile); + + switch (imageid) { + case 2: _cimg_load_pandore_case(2,dims[1],1,1,1,unsigned char,unsigned char,unsigned char,1); break; + case 3: _cimg_load_pandore_case(2,dims[1],1,1,1,long,int,short,4); break; + case 4: _cimg_load_pandore_case(2,dims[1],1,1,1,double,float,float,4); break; + case 5: _cimg_load_pandore_case(3,dims[2],dims[1],1,1,unsigned char,unsigned char,unsigned char,1); break; + case 6: _cimg_load_pandore_case(3,dims[2],dims[1],1,1,long,int,short,4); break; + case 7: _cimg_load_pandore_case(3,dims[2],dims[1],1,1,double,float,float,4); break; + case 8: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,unsigned char,unsigned char,unsigned char,1); break; + case 9: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,long,int,short,4); break; + case 10: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,double,float,float,4); break; + case 11 : { // Region 1d + cimg::fread(dims,3,nfile); + if (endian) cimg::invert_endianness(dims,3); + assign(dims[1],1,1,1); + const unsigned siz = size(); + if (dims[2]<256) { + unsigned char *buffer = new unsigned char[siz]; + cimg::fread(buffer,siz,nfile); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } else { + if (dims[2]<65536) { + unsigned short *buffer = new unsigned short[siz]; + cimg::fread(buffer,siz,nfile); + if (endian) cimg::invert_endianness(buffer,siz); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } else { + unsigned int *buffer = new unsigned int[siz]; + cimg::fread(buffer,siz,nfile); + if (endian) cimg::invert_endianness(buffer,siz); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } + } + } + break; + case 12 : { // Region 2d + cimg::fread(dims,4,nfile); + if (endian) cimg::invert_endianness(dims,4); + assign(dims[2],dims[1],1,1); + const unsigned int siz = size(); + if (dims[3]<256) { + unsigned char *buffer = new unsigned char[siz]; + cimg::fread(buffer,siz,nfile); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } else { + if (dims[3]<65536) { + unsigned short *buffer = new unsigned short[siz]; + cimg::fread(buffer,siz,nfile); + if (endian) cimg::invert_endianness(buffer,siz); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } else { + unsigned long *buffer = new unsigned long[siz]; + cimg::fread(buffer,siz,nfile); + if (endian) cimg::invert_endianness(buffer,siz); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } + } + } + break; + case 13 : { // Region 3d + cimg::fread(dims,5,nfile); + if (endian) cimg::invert_endianness(dims,5); + assign(dims[3],dims[2],dims[1],1); + const unsigned int siz = size(); + if (dims[4]<256) { + unsigned char *buffer = new unsigned char[siz]; + cimg::fread(buffer,siz,nfile); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } else { + if (dims[4]<65536) { + unsigned short *buffer = new unsigned short[siz]; + cimg::fread(buffer,siz,nfile); + if (endian) cimg::invert_endianness(buffer,siz); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } else { + unsigned int *buffer = new unsigned int[siz]; + cimg::fread(buffer,siz,nfile); + if (endian) cimg::invert_endianness(buffer,siz); + T *ptrd = _data; + cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); + buffer-=siz; + delete[] buffer; + } + } + } + break; + case 16: _cimg_load_pandore_case(4,dims[2],dims[1],1,3,unsigned char,unsigned char,unsigned char,1); break; + case 17: _cimg_load_pandore_case(4,dims[2],dims[1],1,3,long,int,short,4); break; + case 18: _cimg_load_pandore_case(4,dims[2],dims[1],1,3,double,float,float,4); break; + case 19: _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,unsigned char,unsigned char,unsigned char,1); break; + case 20: _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,long,int,short,4); break; + case 21: _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,double,float,float,4); break; + case 22: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned char,unsigned char,unsigned char,1); break; + case 23: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],long,int,short,4); + case 24: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned long,unsigned int,unsigned short,4); break; + case 25: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],double,float,float,4); break; + case 26: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned char,unsigned char,unsigned char,1); break; + case 27: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],long,int,short,4); break; + case 28: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned long,unsigned int,unsigned short,4); break; + case 29: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],double,float,float,4); break; + case 30: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned char,unsigned char,unsigned char,1); + break; + case 31: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],long,int,short,4); break; + case 32: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned long,unsigned int,unsigned short,4); + break; + case 33: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],double,float,float,4); break; + case 34 : { // Points 1d + int ptbuf[4] = { 0 }; + cimg::fread(ptbuf,1,nfile); + if (endian) cimg::invert_endianness(ptbuf,1); + assign(1); (*this)(0) = (T)ptbuf[0]; + } break; + case 35 : { // Points 2d + int ptbuf[4] = { 0 }; + cimg::fread(ptbuf,2,nfile); + if (endian) cimg::invert_endianness(ptbuf,2); + assign(2); (*this)(0) = (T)ptbuf[1]; (*this)(1) = (T)ptbuf[0]; + } break; + case 36 : { // Points 3d + int ptbuf[4] = { 0 }; + cimg::fread(ptbuf,3,nfile); + if (endian) cimg::invert_endianness(ptbuf,3); + assign(3); (*this)(0) = (T)ptbuf[2]; (*this)(1) = (T)ptbuf[1]; (*this)(2) = (T)ptbuf[0]; + } break; + default : + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_pandore(): Unable to load data with ID_type %u in file '%s'.", + cimg_instance, + imageid,filename?filename:"(FILE*)"); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image from a PAR-REC (Philips) file. + /** + \param filename Filename, as a C-string. + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg& load_parrec(const char *const filename, const char axis='c', const float align=0) { + CImgList list; + list.load_parrec(filename); + if (list._width==1) return list[0].move_to(*this); + return assign(list.get_append(axis,align)); + } + + //! Load image from a PAR-REC (Philips) file \newinstance. + static CImg get_load_parrec(const char *const filename, const char axis='c', const float align=0) { + return CImg().load_parrec(filename,axis,align); + } + + //! Load image from a raw binary file. + /** + \param filename Filename, as a C-string. + \param size_x Width of the image buffer. + \param size_y Height of the image buffer. + \param size_z Depth of the image buffer. + \param size_c Spectrum of the image buffer. + \param is_multiplexed Tells if the image values are multiplexed along the C-axis. + \param invert_endianness Tells if the endianness of the image buffer must be inverted. + \param offset Starting offset of the read in the specified file. + **/ + CImg& load_raw(const char *const filename, + const unsigned int size_x=0, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1, + const bool is_multiplexed=false, const bool invert_endianness=false, + const unsigned long offset=0) { + return _load_raw(0,filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); + } + + //! Load image from a raw binary file \newinstance. + static CImg get_load_raw(const char *const filename, + const unsigned int size_x=0, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1, + const bool is_multiplexed=false, const bool invert_endianness=false, + const unsigned long offset=0) { + return CImg().load_raw(filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); + } + + //! Load image from a raw binary file \overloading. + CImg& load_raw(std::FILE *const file, + const unsigned int size_x=0, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1, + const bool is_multiplexed=false, const bool invert_endianness=false, + const unsigned long offset=0) { + return _load_raw(file,0,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); + } + + //! Load image from a raw binary file \newinstance. + static CImg get_load_raw(std::FILE *const file, + const unsigned int size_x=0, const unsigned int size_y=1, + const unsigned int size_z=1, const unsigned int size_c=1, + const bool is_multiplexed=false, const bool invert_endianness=false, + const unsigned long offset=0) { + return CImg().load_raw(file,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); + } + + CImg& _load_raw(std::FILE *const file, const char *const filename, + const unsigned int size_x, const unsigned int size_y, + const unsigned int size_z, const unsigned int size_c, + const bool is_multiplexed, const bool invert_endianness, + const unsigned long offset) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_raw(): Specified filename is (null).", + cimg_instance); + if (cimg::is_directory(filename)) + throw CImgArgumentException(_cimg_instance + "load_raw(): Specified filename '%s' is a directory.", + cimg_instance,filename); + + unsigned int siz = size_x*size_y*size_z*size_c, + _size_x = size_x, _size_y = size_y, _size_z = size_z, _size_c = size_c; + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + if (!siz) { // Retrieve file size. + const long fpos = std::ftell(nfile); + if (fpos<0) throw CImgArgumentException(_cimg_instance + "load_raw(): Cannot determine size of input file '%s'.", + cimg_instance,filename?filename:"(FILE*)"); + std::fseek(nfile,0,SEEK_END); + siz = _size_y = (unsigned int)std::ftell(nfile)/sizeof(T); + _size_x = _size_z = _size_c = 1; + std::fseek(nfile,fpos,SEEK_SET); + } + std::fseek(nfile,(long)offset,SEEK_SET); + assign(_size_x,_size_y,_size_z,_size_c,0); + if (!is_multiplexed || size_c==1) { + cimg::fread(_data,siz,nfile); + if (invert_endianness) cimg::invert_endianness(_data,siz); + } else { + CImg buf(1,1,1,_size_c); + cimg_forXYZ(*this,x,y,z) { + cimg::fread(buf._data,_size_c,nfile); + if (invert_endianness) cimg::invert_endianness(buf._data,_size_c); + set_vector_at(buf,x,y,z); + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load image sequence using FFMPEG av's libraries. + /** + \param filename Filename, as a C-string. + \param first_frame Index of the first frame to read. + \param last_frame Index of the last frame to read. + \param step_frame Step value for frame reading. + \param pixel_format To be documented. + \param resume To be documented. + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg& load_ffmpeg(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool pixel_format=true, const bool resume=false, + const char axis='z', const float align=0) { + return get_load_ffmpeg(filename,first_frame,last_frame,step_frame,pixel_format,resume,axis,align).move_to(*this); + } + + //! Load image sequence using FFMPEG av's libraries \newinstance. + static CImg get_load_ffmpeg(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool pixel_format=true, + const bool resume=false, + const char axis='z', const float align=0) { + return CImgList().load_ffmpeg(filename,first_frame,last_frame,step_frame,pixel_format,resume). + get_append(axis,align); + } + + //! Load image sequence from a YUV file. + /** + \param filename Filename, as a C-string. + \param size_x Width of the frames. + \param size_y Height of the frames. + \param first_frame Index of the first frame to read. + \param last_frame Index of the last frame to read. + \param step_frame Step value for frame reading. + \param yuv2rgb Tells if the YUV to RGB transform must be applied. + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + **/ + CImg& load_yuv(const char *const filename, + const unsigned int size_x, const unsigned int size_y=1, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { + return get_load_yuv(filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this); + } + + //! Load image sequence from a YUV file \newinstance. + static CImg get_load_yuv(const char *const filename, + const unsigned int size_x, const unsigned int size_y=1, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { + return CImgList().load_yuv(filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb).get_append(axis); + } + + //! Load image sequence from a YUV file \overloading. + CImg& load_yuv(std::FILE *const file, + const unsigned int size_x, const unsigned int size_y=1, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { + return get_load_yuv(file,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this); + } + + //! Load image sequence from a YUV file \newinstance. + static CImg get_load_yuv(std::FILE *const file, + const unsigned int size_x, const unsigned int size_y=1, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { + return CImgList().load_yuv(file,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb).get_append(axis); + } + + //! Load 3d object from a .OFF file. + /** + \param[out] primitives Primitives data of the 3d object. + \param[out] colors Colors data of the 3d object. + \param filename Filename, as a C-string. + **/ + template + CImg& load_off(CImgList& primitives, CImgList& colors, const char *const filename) { + return _load_off(primitives,colors,0,filename); + } + + //! Load 3d object from a .OFF file \newinstance. + template + static CImg get_load_off(CImgList& primitives, CImgList& colors, const char *const filename) { + return CImg().load_off(primitives,colors,filename); + } + + //! Load 3d object from a .OFF file \overloading. + template + CImg& load_off(CImgList& primitives, CImgList& colors, std::FILE *const file) { + return _load_off(primitives,colors,file,0); + } + + //! Load 3d object from a .OFF file \newinstance. + template + static CImg get_load_off(CImgList& primitives, CImgList& colors, std::FILE *const file) { + return CImg().load_off(primitives,colors,file); + } + + template + CImg& _load_off(CImgList& primitives, CImgList& colors, + std::FILE *const file, const char *const filename) { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "load_off(): Specified filename is (null).", + cimg_instance); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"r"); + unsigned int nb_points = 0, nb_primitives = 0, nb_read = 0; + char line[256] = { 0 }; + int err; + + // Skip comments, and read magic string OFF + do { err = std::fscanf(nfile,"%255[^\n] ",line); } while (!err || (err==1 && *line=='#')); + if (cimg::strncasecmp(line,"OFF",3) && cimg::strncasecmp(line,"COFF",4)) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_off(): OFF header not found in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + do { err = std::fscanf(nfile,"%255[^\n] ",line); } while (!err || (err==1 && *line=='#')); + if ((err = std::sscanf(line,"%u%u%*[^\n] ",&nb_points,&nb_primitives))!=2) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_off(): Invalid number of vertices or primitives specified in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + } + + // Read points data + assign(nb_points,3); + float X = 0, Y = 0, Z = 0; + cimg_forX(*this,l) { + do { err = std::fscanf(nfile,"%255[^\n] ",line); } while (!err || (err==1 && *line=='#')); + if ((err = std::sscanf(line,"%f%f%f%*[^\n] ",&X,&Y,&Z))!=3) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "load_off(): Failed to read vertex %u/%u in file '%s'.", + cimg_instance, + l+1,nb_points,filename?filename:"(FILE*)"); + } + (*this)(l,0) = (T)X; (*this)(l,1) = (T)Y; (*this)(l,2) = (T)Z; + } + + // Read primitive data + primitives.assign(); + colors.assign(); + bool stop_flag = false; + while (!stop_flag) { + float c0 = 0.7f, c1 = 0.7f, c2 = 0.7f; + unsigned int prim = 0, i0 = 0, i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0; + *line = 0; + if ((err = std::fscanf(nfile,"%u",&prim))!=1) stop_flag = true; + else { + ++nb_read; + switch (prim) { + case 1 : { + if ((err = std::fscanf(nfile,"%u%255[^\n] ",&i0,line))<2) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0).move_to(primitives); + CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); + } + } break; + case 2 : { + if ((err = std::fscanf(nfile,"%u%u%255[^\n] ",&i0,&i1,line))<2) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i1).move_to(primitives); + CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); + } + } break; + case 3 : { + if ((err = std::fscanf(nfile,"%u%u%u%255[^\n] ",&i0,&i1,&i2,line))<3) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i2,i1).move_to(primitives); + CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); + } + } break; + case 4 : { + if ((err = std::fscanf(nfile,"%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,line))<4) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i3,i2,i1).move_to(primitives); + CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); + } + } break; + case 5 : { + if ((err = std::fscanf(nfile,"%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,line))<5) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i3,i2,i1).move_to(primitives); + CImg::vector(i0,i4,i3).move_to(primitives); + colors.insert(2,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); + ++nb_primitives; + } + } break; + case 6 : { + if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,line))<6) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i3,i2,i1).move_to(primitives); + CImg::vector(i0,i5,i4,i3).move_to(primitives); + colors.insert(2,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); + ++nb_primitives; + } + } break; + case 7 : { + if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,line))<7) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i4,i3,i1).move_to(primitives); + CImg::vector(i0,i6,i5,i4).move_to(primitives); + CImg::vector(i3,i2,i1).move_to(primitives); + colors.insert(3,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); + ++(++nb_primitives); + } + } break; + case 8 : { + if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,&i7,line))<7) { + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u from file '%s'.", + cimg_instance, + nb_read,nb_primitives,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } else { + err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); + CImg::vector(i0,i3,i2,i1).move_to(primitives); + CImg::vector(i0,i5,i4,i3).move_to(primitives); + CImg::vector(i0,i7,i6,i5).move_to(primitives); + colors.insert(3,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); + ++(++nb_primitives); + } + } break; + default : + cimg::warn(_cimg_instance + "load_off(): Failed to read primitive %u/%u (%u vertices) from file '%s'.", + cimg_instance, + nb_read,nb_primitives,prim,filename?filename:"(FILE*)"); + + err = std::fscanf(nfile,"%*[^\n] "); + } + } + } + if (!file) cimg::fclose(nfile); + if (primitives._width!=nb_primitives) + cimg::warn(_cimg_instance + "load_off(): Only %u/%u primitives read from file '%s'.", + cimg_instance, + primitives._width,nb_primitives,filename?filename:"(FILE*)"); + return *this; + } + + //! Load image sequence using FFMPEG's external tool 'ffmpeg'. + /** + \param filename Filename, as a C-string. + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg& load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) { + return get_load_ffmpeg_external(filename,axis,align).move_to(*this); + } + + //! Load image sequence using FFMPEG's external tool 'ffmpeg' \newinstance. + static CImg get_load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) { + return CImgList().load_ffmpeg_external(filename).get_append(axis,align); + } + + //! Load gif file, using Imagemagick or GraphicsMagicks's external tools. + /** + \param filename Filename, as a C-string. + \param use_graphicsmagick Tells if GraphicsMagick's tool 'gm' is used instead of ImageMagick's tool 'convert'. + \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg& load_gif_external(const char *const filename, + const char axis='z', const float align=0) { + return get_load_gif_external(filename,axis,align).move_to(*this); + } + + //! Load gif file, using ImageMagick or GraphicsMagick's external tool 'convert' \newinstance. + static CImg get_load_gif_external(const char *const filename, + const char axis='z', const float align=0) { + return CImgList().load_gif_external(filename).get_append(axis,align); + } + + //! Load image using GraphicsMagick's external tool 'gm'. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_graphicsmagick_external(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_graphicsmagick_external(): Specified filename is (null).", + cimg_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }; + std::FILE *file = 0; + const CImg s_filename = CImg::string(filename)._system_strescape(); +#if cimg_OS==1 + cimg_snprintf(command,sizeof(command),"%s convert \"%s\" pnm:-", + cimg::graphicsmagick_path(),s_filename.data()); + file = popen(command,"r"); + if (file) { + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { load_pnm(file); } catch (...) { + pclose(file); + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load_graphicsmagick_external(): Failed to load file '%s' with external command 'gm'.", + cimg_instance, + filename); + } + pclose(file); + return *this; + } +#endif + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.pnm", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(command,sizeof(command),"%s convert \"%s\" \"%s\"", + cimg::graphicsmagick_path(),s_filename.data(), + CImg::string(filetmp)._system_strescape().data()); + cimg::system(command,cimg::graphicsmagick_path()); + if (!(file = std::fopen(filetmp,"rb"))) { + cimg::fclose(cimg::fopen(filename,"r")); + throw CImgIOException(_cimg_instance + "load_graphicsmagick_external(): Failed to load file '%s' with external command 'gm'.", + cimg_instance, + filename); + + } else cimg::fclose(file); + load_pnm(filetmp); + std::remove(filetmp); + return *this; + } + + //! Load image using GraphicsMagick's external tool 'gm' \newinstance. + static CImg get_load_graphicsmagick_external(const char *const filename) { + return CImg().load_graphicsmagick_external(filename); + } + + //! Load gzipped image file, using external tool 'gunzip'. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_gzip_external(const char *const filename) { + if (!filename) + throw CImgIOException(_cimg_instance + "load_gzip_external(): Specified filename is (null).", + cimg_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; + const char + *const ext = cimg::split_filename(filename,body), + *const ext2 = cimg::split_filename(body,0); + + std::FILE *file = 0; + do { + if (!cimg::strcasecmp(ext,"gz")) { + if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } else { + if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", + cimg::gunzip_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); + cimg::system(command); + if (!(file = std::fopen(filetmp,"rb"))) { + cimg::fclose(cimg::fopen(filename,"r")); + throw CImgIOException(_cimg_instance + "load_gzip_external(): Failed to load file '%s' with external command 'gunzip'.", + cimg_instance, + filename); + + } else cimg::fclose(file); + load(filetmp); + std::remove(filetmp); + return *this; + } + + //! Load gzipped image file, using external tool 'gunzip' \newinstance. + static CImg get_load_gzip_external(const char *const filename) { + return CImg().load_gzip_external(filename); + } + + //! Load image using ImageMagick's external tool 'convert'. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_imagemagick_external(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_imagemagick_external(): Specified filename is (null).", + cimg_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }; + std::FILE *file = 0; + const CImg s_filename = CImg::string(filename)._system_strescape(); +#if cimg_OS==1 + cimg_snprintf(command,sizeof(command),"%s%s \"%s\" pnm:-", + cimg::imagemagick_path(), + !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"", + s_filename.data()); + file = popen(command,"r"); + if (file) { + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { load_pnm(file); } catch (...) { + pclose(file); + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load_imagemagick_external(): Failed to load file '%s' with " + "external command 'convert'.", + cimg_instance, + filename); + } + pclose(file); + return *this; + } +#endif + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.pnm", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(command,sizeof(command),"%s%s \"%s\" \"%s\"", + cimg::imagemagick_path(), + !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"", + s_filename.data(),CImg::string(filetmp)._system_strescape().data()); + cimg::system(command,cimg::imagemagick_path()); + if (!(file = std::fopen(filetmp,"rb"))) { + cimg::fclose(cimg::fopen(filename,"r")); + throw CImgIOException(_cimg_instance + "load_imagemagick_external(): Failed to load file '%s' with external command 'convert'.", + cimg_instance, + filename); + + } else cimg::fclose(file); + load_pnm(filetmp); + std::remove(filetmp); + return *this; + } + + //! Load image using ImageMagick's external tool 'convert' \newinstance. + static CImg get_load_imagemagick_external(const char *const filename) { + return CImg().load_imagemagick_external(filename); + } + + //! Load image from a DICOM file, using XMedcon's external tool 'medcon'. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_medcon_external(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_medcon_external(): Specified filename is (null).", + cimg_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; + cimg::fclose(cimg::fopen(filename,"r")); + std::FILE *file = 0; + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s.hdr",cimg::filenamerand()); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(command,sizeof(command),"%s -w -c anlz -o \"%s\" -f \"%s\"", + cimg::medcon_path(), + CImg::string(filetmp)._system_strescape().data(), + CImg::string(filename)._system_strescape().data()); + cimg::system(command); + cimg::split_filename(filetmp,body); + + cimg_snprintf(command,sizeof(command),"%s.hdr",body); + file = std::fopen(command,"rb"); + if (!file) { + cimg_snprintf(command,sizeof(command),"m000-%s.hdr",body); + file = std::fopen(command,"rb"); + if (!file) { + throw CImgIOException(_cimg_instance + "load_medcon_external(): Failed to load file '%s' with external command 'medcon'.", + cimg_instance, + filename); + } + } + cimg::fclose(file); + load_analyze(command); + std::remove(command); + cimg::split_filename(command,body); + cimg_snprintf(command,sizeof(command),"%s.img",body); + std::remove(command); + return *this; + } + + //! Load image from a DICOM file, using XMedcon's external tool 'medcon' \newinstance. + static CImg get_load_medcon_external(const char *const filename) { + return CImg().load_medcon_external(filename); + } + + //! Load image from a RAW Color Camera file, using external tool 'dcraw'. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_dcraw_external(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_dcraw_external(): Specified filename is (null).", + cimg_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }; + std::FILE *file = 0; + const CImg s_filename = CImg::string(filename)._system_strescape(); +#if cimg_OS==1 + cimg_snprintf(command,sizeof(command),"%s -w -4 -c \"%s\"", + cimg::dcraw_path(),s_filename.data()); + file = popen(command,"r"); + if (file) { + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { load_pnm(file); } catch (...) { + pclose(file); + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.", + cimg_instance, + filename); + } + pclose(file); + return *this; + } +#endif + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.ppm", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(command,sizeof(command),"%s -w -4 -c \"%s\" > \"%s\"", + cimg::dcraw_path(),s_filename.data(),CImg::string(filetmp)._system_strescape().data()); + cimg::system(command,cimg::dcraw_path()); + if (!(file = std::fopen(filetmp,"rb"))) { + cimg::fclose(cimg::fopen(filename,"r")); + throw CImgIOException(_cimg_instance + "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.", + cimg_instance, + filename); + + } else cimg::fclose(file); + load_pnm(filetmp); + std::remove(filetmp); + return *this; + } + + //! Load image from a RAW Color Camera file, using external tool 'dcraw' \newinstance. + static CImg get_load_dcraw_external(const char *const filename) { + return CImg().load_dcraw_external(filename); + } + + //! Load image from a camera stream, using OpenCV. + /** + \param camera_index Index of the camera to capture images from. + \param skip_frames Number of frames to skip before the capture. + \param release_camera Tells if the camera ressource must be released at the end of the method. + **/ + CImg& load_camera(const unsigned int camera_index=0, const unsigned int skip_frames=0, + const bool release_camera=true, const unsigned int capture_width=0, + const unsigned int capture_height=0) { +#ifdef cimg_use_opencv + if (camera_index>255) + throw CImgArgumentException(_cimg_instance + "load_camera(): Invalid request for camera #%u " + "(no more than 256 cameras can be managed).", + cimg_instance, + camera_index); + static CvCapture *capture[256] = { 0 }; + if (release_camera) { + if (capture[camera_index]) cvReleaseCapture(&(capture[camera_index])); + capture[camera_index] = 0; + return *this; + } + if (!capture[camera_index]) { + capture[camera_index] = cvCreateCameraCapture(camera_index); + if (!capture[camera_index]) { + throw CImgIOException(_cimg_instance + "load_camera(): Failed to initialize camera #%u.", + cimg_instance, + camera_index); + } + } + if (capture_width) cvSetCaptureProperty(capture[camera_index],CV_CAP_PROP_FRAME_WIDTH,capture_width); + if (capture_height) cvSetCaptureProperty(capture[camera_index],CV_CAP_PROP_FRAME_HEIGHT,capture_height); + const IplImage *img = 0; + for (unsigned int i = 0; iwidthStep - 3*img->width); + assign(img->width,img->height,1,3); + const unsigned char* ptrs = (unsigned char*)img->imageData; + T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); + if (step>0) cimg_forY(*this,y) { + cimg_forX(*this,x) { *(ptr_b++) = (T)*(ptrs++); *(ptr_g++) = (T)*(ptrs++); *(ptr_r++) = (T)*(ptrs++); } + ptrs+=step; + } else for (unsigned long siz = (unsigned long)img->width*img->height; siz; --siz) { + *(ptr_b++) = (T)*(ptrs++); *(ptr_g++) = (T)*(ptrs++); *(ptr_r++) = (T)*(ptrs++); + } + } + return *this; +#else + cimg::unused(camera_index,skip_frames,release_camera,capture_width,capture_height); + throw CImgIOException(_cimg_instance + "load_camera(): This function requires the OpenCV library to run " + "(macro 'cimg_use_opencv' must be defined).", + cimg_instance); +#endif + } + + //! Load image from a camera stream, using OpenCV \newinstance. + static CImg get_load_camera(const unsigned int camera_index=0, const unsigned int skip_frames=0, + const bool release_camera=true, + const unsigned int capture_width=0, const unsigned int capture_height=0) { + return CImg().load_camera(camera_index,skip_frames,release_camera,capture_width,capture_height); + } + + //! Load image using various non-native ways. + /** + \param filename Filename, as a C-string. + **/ + CImg& load_other(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimg_instance + "load_other(): Specified filename is (null).", + cimg_instance); + + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { load_magick(filename); } + catch (CImgException&) { + try { load_imagemagick_external(filename); } + catch (CImgException&) { + try { load_graphicsmagick_external(filename); } + catch (CImgException&) { + try { load_cimg(filename); } + catch (CImgException&) { + try { + std::fclose(cimg::fopen(filename,"rb")); + } catch (CImgException&) { + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load_other(): Failed to open file '%s'.", + cimg_instance, + filename); + } + cimg::exception_mode() = omode; + throw CImgIOException(_cimg_instance + "load_other(): Failed to recognize format of file '%s'.", + cimg_instance, + filename); + } + } + } + } + cimg::exception_mode() = omode; + return *this; + } + + //! Load image using various non-native ways \newinstance. + static CImg get_load_other(const char *const filename) { + return CImg().load_other(filename); + } + + //@} + //--------------------------- + // + //! \name Data Output + //@{ + //--------------------------- + + //! Display informations about the image data. + /** + \param title Name for the considered image. + \param display_stats Tells to compute and display image statistics. + **/ + const CImg& print(const char *const title=0, const bool display_stats=true) const { + int xm = 0, ym = 0, zm = 0, vm = 0, xM = 0, yM = 0, zM = 0, vM = 0; + CImg st; + if (!is_empty() && display_stats) { + st = get_stats(); + xm = (int)st[4]; ym = (int)st[5], zm = (int)st[6], vm = (int)st[7]; + xM = (int)st[8]; yM = (int)st[9], zM = (int)st[10], vM = (int)st[11]; + } + const unsigned long siz = size(), msiz = siz*sizeof(T), siz1 = siz-1, + mdisp = msiz<8*1024?0:(msiz<8*1024*1024?1:2), width1 = _width-1; + + char _title[64] = { 0 }; + if (!title) cimg_snprintf(_title,sizeof(_title),"CImg<%s>",pixel_type()); + + std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = (%u,%u,%u,%u) [%lu %s], %sdata%s = (%s*)%p", + cimg::t_magenta,cimg::t_bold,title?title:_title,cimg::t_normal, + cimg::t_bold,cimg::t_normal,(void*)this, + cimg::t_bold,cimg::t_normal,_width,_height,_depth,_spectrum, + mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20)), + mdisp==0?"b":(mdisp==1?"Kio":"Mio"), + cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin()); + if (_data) std::fprintf(cimg::output(),"..%p (%s) = [ ",(void*)((char*)end()-1),_is_shared?"shared":"non-shared"); + else std::fprintf(cimg::output()," (%s) = [ ",_is_shared?"shared":"non-shared"); + + if (!is_empty()) cimg_foroff(*this,off) { + std::fprintf(cimg::output(),cimg::type::format(),cimg::type::format(_data[off])); + if (off!=siz1) std::fprintf(cimg::output(),"%s",off%_width==width1?" ; ":" "); + if (off==7 && siz>16) { off = siz1-8; std::fprintf(cimg::output(),"... "); } + } + if (!is_empty() && display_stats) + std::fprintf(cimg::output(), + " ], %smin%s = %g, %smax%s = %g, %smean%s = %g, %sstd%s = %g, %scoords_min%s = (%u,%u,%u,%u), " + "%scoords_max%s = (%u,%u,%u,%u).\n", + cimg::t_bold,cimg::t_normal,st[0], + cimg::t_bold,cimg::t_normal,st[1], + cimg::t_bold,cimg::t_normal,st[2], + cimg::t_bold,cimg::t_normal,std::sqrt(st[3]), + cimg::t_bold,cimg::t_normal,xm,ym,zm,vm, + cimg::t_bold,cimg::t_normal,xM,yM,zM,vM); + else std::fprintf(cimg::output(),"%s].\n",is_empty()?"":" "); + std::fflush(cimg::output()); + return *this; + } + + //! Display image into a CImgDisplay window. + /** + \param disp Display window. + **/ + const CImg& display(CImgDisplay& disp) const { + disp.display(*this); + return *this; + } + + //! Display image into a CImgDisplay window, in an interactive way. + /** + \param disp Display window. + \param display_info Tells if image informations are displayed on the standard output. + **/ + const CImg& display(CImgDisplay &disp, const bool display_info, unsigned int *const XYZ=0) const { + return _display(disp,0,display_info,XYZ,false); + } + + //! Display image into an interactive window. + /** + \param title Window title + \param display_info Tells if image informations are displayed on the standard output. + **/ + const CImg& display(const char *const title=0, const bool display_info=true, unsigned int *const XYZ=0) const { + CImgDisplay disp; + return _display(disp,title,display_info,XYZ,false); + } + + const CImg& _display(CImgDisplay &disp, const char *const title, + const bool display_info, unsigned int *const XYZ, + const bool exit_on_simpleclick) const { + unsigned int oldw = 0, oldh = 0, _XYZ[3] = { 0 }, key = 0; + int x0 = 0, y0 = 0, z0 = 0, x1 = width()-1, y1 = height()-1, z1 = depth()-1; + + if (!disp) { + disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1); + if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum); + else disp.set_title("%s",title); + } else if (title) disp.set_title("%s",title); + disp.show().flush(); + + const CImg dtitle = CImg::string(disp.title()); + if (display_info) print(dtitle); + + CImg zoom; + for (bool reset_view = true, resize_disp = false, is_first_select = true; !key && !disp.is_closed(); ) { + if (reset_view) { + if (XYZ) { _XYZ[0] = XYZ[0]; _XYZ[1] = XYZ[1]; _XYZ[2] = XYZ[2]; } + else { _XYZ[0] = (x0 + x1)/2; _XYZ[1] = (y0 + y1)/2; _XYZ[2] = (z0 + z1)/2; } + x0 = 0; y0 = 0; z0 = 0; x1 = width()-1; y1 = height()-1; z1 = depth()-1; + oldw = disp.width(); oldh = disp.height(); + reset_view = false; + } + if (!x0 && !y0 && !z0 && x1==width()-1 && y1==height()-1 && z1==depth()-1) { + if (is_empty()) zoom.assign(1,1,1,1,0); else zoom.assign(); + } else zoom = get_crop(x0,y0,z0,x1,y1,z1); + + const unsigned int + dx = 1 + x1 - x0, dy = 1 + y1 - y0, dz = 1 + z1 - z0, + tw = dx + (dz>1?dz:0), th = dy + (dz>1?dz:0); + if (!is_empty() && !disp.is_fullscreen() && resize_disp) { + const unsigned int + ttw = tw*disp.width()/oldw, tth = th*disp.height()/oldh, + dM = cimg::max(ttw,tth), diM = (unsigned int)cimg::max(disp.width(),disp.height()), + imgw = cimg::max(16U,ttw*diM/dM), imgh = cimg::max(16U,tth*diM/dM); + disp.set_fullscreen(false).resize(cimg_fitscreen(imgw,imgh,1),false); + resize_disp = false; + } + oldw = tw; oldh = th; + + bool + go_up = false, go_down = false, go_left = false, go_right = false, + go_inc = false, go_dec = false, go_in = false, go_out = false, + go_in_center = false; + const CImg& visu = zoom?zoom:*this; + + disp.set_title("%s",dtitle._data); + if (_width>1 && visu._width==1) disp.set_title("%s | x=%u",disp._title,x0); + if (_height>1 && visu._height==1) disp.set_title("%s | y=%u",disp._title,y0); + if (_depth>1 && visu._depth==1) disp.set_title("%s | z=%u",disp._title,z0); + + if (!is_first_select) { _XYZ[0] = (x1-x0)/2; _XYZ[1] = (y1-y0)/2; _XYZ[2] = (z1-z0)/2; } + const CImg selection = visu._get_select(disp,0,2,_XYZ,x0,y0,z0,is_first_select,_depth>1); + is_first_select = false; + + if (disp.wheel()) { + if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + go_out = !(go_in = disp.wheel()>0); go_in_center = false; + } else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) { go_left = !(go_right = disp.wheel()>0); } + else if (disp.is_keyALT() || disp.is_keyALTGR() || _depth==1) { go_down = !(go_up = disp.wheel()>0); } + disp.set_wheel(); + } + + const int + sx0 = selection(0), sy0 = selection(1), sz0 = selection(2), + sx1 = selection(3), sy1 = selection(4), sz1 = selection(5); + if (sx0>=0 && sy0>=0 && sz0>=0 && sx1>=0 && sy1>=0 && sz1>=0) { + x1 = x0 + sx1; y1 = y0 + sy1; z1 = z0 + sz1; + x0+=sx0; y0+=sy0; z0+=sz0; + if (sx0==sx1 && sy0==sy1 && sz0==sz1) { + if (exit_on_simpleclick && (!zoom || is_empty())) break; else reset_view = true; + } + resize_disp = true; + } else switch (key = disp.key()) { +#if cimg_OS!=2 + case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT : +#endif + case 0 : case cimg::keyCTRLLEFT : case cimg::keyPAD5 : case cimg::keySHIFTLEFT : +#if cimg_OS!=2 + case cimg::keyALTGR : +#endif + case cimg::keyALT : key = 0; break; + case cimg::keyP : if (visu._depth>1 && (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT())) { + // Special mode: play stack of frames + const unsigned int + w1 = visu._width*disp.width()/(visu._width+(visu._depth>1?visu._depth:0)), + h1 = visu._height*disp.height()/(visu._height+(visu._depth>1?visu._depth:0)); + float frame_timing = 5; + bool is_stopped = false; + disp.set_key(key,false).set_wheel().resize(cimg_fitscreen(w1,h1,1),false); key = 0; + for (unsigned int timer = 0; !key && !disp.is_closed() && !disp.button(); ) { + if (disp.is_resized()) disp.resize(false); + if (!timer) { + visu.get_slice((int)_XYZ[2]).display(disp.set_title("%s | z=%d",dtitle.data(),_XYZ[2])); + (++_XYZ[2])%=visu._depth; + } + if (!is_stopped) { if (++timer>(unsigned int)frame_timing) timer = 0; } else timer = ~0U; + if (disp.wheel()) { frame_timing-=disp.wheel()/3.0f; disp.set_wheel(); } + switch (key = disp.key()) { +#if cimg_OS!=2 + case cimg::keyCTRLRIGHT : +#endif + case cimg::keyCTRLLEFT : key = 0; break; + case cimg::keyPAGEUP : frame_timing-=0.3f; key = 0; break; + case cimg::keyPAGEDOWN : frame_timing+=0.3f; key = 0; break; + case cimg::keySPACE : is_stopped = !is_stopped; disp.set_key(key,false); key = 0; break; + case cimg::keyARROWLEFT : case cimg::keyARROWUP : is_stopped = true; timer = 0; key = 0; break; + case cimg::keyARROWRIGHT : case cimg::keyARROWDOWN : is_stopped = true; + (_XYZ[2]+=visu._depth-2)%=visu._depth; timer = 0; key = 0; break; + case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), + CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false); + disp.set_key(key,false); key = 0; + } break; + case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false).set_key(key,false); key = 0; + } break; + case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(_width,_height,_depth),false).set_key(key,false); key = 0; + } break; + case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.resize(disp.screen_width(),disp.screen_height(),false). + toggle_fullscreen().set_key(key,false); key = 0; + } break; + } + frame_timing = frame_timing<1?1:(frame_timing>39?39:frame_timing); + disp.wait(20); + } + const unsigned int + w2 = (visu._width + (visu._depth>1?visu._depth:0))*disp.width()/visu._width, + h2 = (visu._height + (visu._depth>1?visu._depth:0))*disp.height()/visu._height; + disp.resize(cimg_fitscreen(w2,h2,1),false).set_title(dtitle.data()).set_key().set_button().set_wheel(); + key = 0; + } break; + case cimg::keyHOME : reset_view = resize_disp = true; key = 0; break; + case cimg::keyPADADD : go_in = true; go_in_center = true; key = 0; break; + case cimg::keyPADSUB : go_out = true; key = 0; break; + case cimg::keyARROWLEFT : case cimg::keyPAD4: go_left = true; key = 0; break; + case cimg::keyARROWRIGHT : case cimg::keyPAD6: go_right = true; key = 0; break; + case cimg::keyARROWUP : case cimg::keyPAD8: go_up = true; key = 0; break; + case cimg::keyARROWDOWN : case cimg::keyPAD2: go_down = true; key = 0; break; + case cimg::keyPAD7 : go_up = go_left = true; key = 0; break; + case cimg::keyPAD9 : go_up = go_right = true; key = 0; break; + case cimg::keyPAD1 : go_down = go_left = true; key = 0; break; + case cimg::keyPAD3 : go_down = go_right = true; key = 0; break; + case cimg::keyPAGEUP : go_inc = true; key = 0; break; + case cimg::keyPAGEDOWN : go_dec = true; key = 0; break; + } + if (go_in) { + const int + mx = go_in_center?disp.width()/2:disp.mouse_x(), + my = go_in_center?disp.height()/2:disp.mouse_y(), + mX = mx*(_width+(_depth>1?_depth:0))/disp.width(), + mY = my*(_height+(_depth>1?_depth:0))/disp.height(); + int X = _XYZ[0], Y = _XYZ[1], Z = _XYZ[2]; + if (mX=height()) { + X = x0 + mX*(1+x1-x0)/_width; Z = z0 + (mY-_height)*(1+z1-z0)/_depth; Y = _XYZ[1]; + } + if (mX>=width() && mY4) { x0 = X - 3*(X-x0)/4; x1 = X + 3*(x1-X)/4; } + if (y1-y0>4) { y0 = Y - 3*(Y-y0)/4; y1 = Y + 3*(y1-Y)/4; } + if (z1-z0>4) { z0 = Z - 3*(Z-z0)/4; z1 = Z + 3*(z1-Z)/4; } + } + if (go_out) { + const int + delta_x = (x1-x0)/8, delta_y = (y1-y0)/8, delta_z = (z1-z0)/8, + ndelta_x = delta_x?delta_x:(_width>1?1:0), + ndelta_y = delta_y?delta_y:(_height>1?1:0), + ndelta_z = delta_z?delta_z:(_depth>1?1:0); + x0-=ndelta_x; y0-=ndelta_y; z0-=ndelta_z; + x1+=ndelta_x; y1+=ndelta_y; z1+=ndelta_z; + if (x0<0) { x1-=x0; x0 = 0; if (x1>=width()) x1 = width() - 1; } + if (y0<0) { y1-=y0; y0 = 0; if (y1>=height()) y1 = height() - 1; } + if (z0<0) { z1-=z0; z0 = 0; if (z1>=depth()) z1 = depth() - 1; } + if (x1>=width()) { x0-=(x1-width()+1); x1 = width()-1; if (x0<0) x0 = 0; } + if (y1>=height()) { y0-=(y1-height()+1); y1 = height()-1; if (y0<0) y0 = 0; } + if (z1>=depth()) { z0-=(z1-depth()+1); z1 = depth()-1; if (z0<0) z0 = 0; } + } + if (go_left) { + const int delta = (x1-x0)/4, ndelta = delta?delta:(_width>1?1:0); + if (x0-ndelta>=0) { x0-=ndelta; x1-=ndelta; } + else { x1-=x0; x0 = 0; } + } + if (go_right) { + const int delta = (x1-x0)/4, ndelta = delta?delta:(_width>1?1:0); + if (x1+ndelta1?1:0); + if (y0-ndelta>=0) { y0-=ndelta; y1-=ndelta; } + else { y1-=y0; y0 = 0; } + } + if (go_down) { + const int delta = (y1-y0)/4, ndelta = delta?delta:(_height>1?1:0); + if (y1+ndelta1?1:0); + if (z0-ndelta>=0) { z0-=ndelta; z1-=ndelta; } + else { z1-=z0; z0 = 0; } + } + if (go_dec) { + const int delta = (z1-z0)/4, ndelta = delta?delta:(_depth>1?1:0); + if (z1+ndelta + const CImg& display_object3d(CImgDisplay& disp, + const CImg& vertices, + const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return _display_object3d(disp,0,vertices,primitives,colors,opacities,centering,render_static, + render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(const char *const title, + const CImg& vertices, + const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + CImgDisplay disp; + return _display_object3d(disp,title,vertices,primitives,colors,opacities,centering,render_static, + render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(CImgDisplay &disp, + const CImg& vertices, + const CImgList& primitives, + const CImgList& colors, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return display_object3d(disp,vertices,primitives,colors,CImgList(),centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(const char *const title, + const CImg& vertices, + const CImgList& primitives, + const CImgList& colors, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return display_object3d(title,vertices,primitives,colors,CImgList(),centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(CImgDisplay &disp, + const CImg& vertices, + const CImgList& primitives, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return display_object3d(disp,vertices,primitives,CImgList(),centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(const char *const title, + const CImg& vertices, + const CImgList& primitives, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return display_object3d(title,vertices,primitives,CImgList(),centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(CImgDisplay &disp, + const CImg& vertices, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return display_object3d(disp,vertices,CImgList(),centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + //! Display object 3d in an interactive window \simplification. + template + const CImg& display_object3d(const char *const title, + const CImg& vertices, + const bool centering=true, + const int render_static=4, const int render_motion=1, + const bool is_double_sided=true, const float focale=700, + const float light_x=0, const float light_y=0, const float light_z=-5e8f, + const float specular_lightness=0.2f, const float specular_shininess=0.1f, + const bool display_axes=true, float *const pose_matrix=0) const { + return display_object3d(title,vertices,CImgList(),centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + + template + const CImg& _display_object3d(CImgDisplay& disp, const char *const title, + const CImg& vertices, + const CImgList& primitives, + const CImgList& colors, + const to& opacities, + const bool centering, + const int render_static, const int render_motion, + const bool is_double_sided, const float focale, + const float light_x, const float light_y, const float light_z, + const float specular_lightness, const float specular_shininess, + const bool display_axes, float *const pose_matrix) const { + typedef typename cimg::superset::type tpfloat; + + // Check input arguments + if (is_empty()) { + if (disp) return CImg(disp.width(),disp.height(),1,(colors && colors[0].size()==1)?1:3,0). + _display_object3d(disp,title,vertices,primitives,colors,opacities,centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + else return CImg(1,2,1,1,64,128).resize(cimg_fitscreen(CImgDisplay::screen_width()/2, + CImgDisplay::screen_height()/2,1), + 1,(colors && colors[0].size()==1)?1:3,3). + _display_object3d(disp,title,vertices,primitives,colors,opacities,centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } else { if (disp) disp.resize(*this,false); } + char error_message[1024] = { 0 }; + if (!vertices.is_object3d(primitives,colors,opacities,true,error_message)) + throw CImgArgumentException(_cimg_instance + "display_object3d(): Invalid specified 3d object (%u,%u) (%s).", + cimg_instance,vertices._width,primitives._width,error_message); + if (vertices._width && !primitives) { + CImgList nprimitives(vertices._width,1,1,1,1); + cimglist_for(nprimitives,l) nprimitives(l,0) = l; + return _display_object3d(disp,title,vertices,nprimitives,colors,opacities,centering, + render_static,render_motion,is_double_sided,focale, + light_x,light_y,light_z,specular_lightness,specular_shininess, + display_axes,pose_matrix); + } + if (!disp) { + disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,3); + if (!title) disp.set_title("CImg<%s> (%u vertices, %u primitives)", + pixel_type(),vertices._width,primitives._width); + } else if (title) disp.set_title("%s",title); + + // Init 3d objects and compute object statistics + CImg + pose, + rotated_vertices(vertices._width,3), + bbox_vertices, rotated_bbox_vertices, + axes_vertices, rotated_axes_vertices, + bbox_opacities, axes_opacities; + CImgList bbox_primitives, axes_primitives; + CImgList reverse_primitives; + CImgList bbox_colors, bbox_colors2, axes_colors; + unsigned int ns_width = 0, ns_height = 0; + int _is_double_sided = (int)is_double_sided; + bool ndisplay_axes = display_axes; + const CImg + background_color(1,1,1,_spectrum,0), + foreground_color(1,1,1,_spectrum,255); + float + Xoff = 0, Yoff = 0, Zoff = 0, sprite_scale = 1, + xm = 0, xM = vertices?vertices.get_shared_row(0).max_min(xm):0, + ym = 0, yM = vertices?vertices.get_shared_row(1).max_min(ym):0, + zm = 0, zM = vertices?vertices.get_shared_row(2).max_min(zm):0; + const float delta = cimg::max(xM-xm,yM-ym,zM-zm); + + rotated_bbox_vertices = bbox_vertices.assign(8,3,1,1, + xm,xM,xM,xm,xm,xM,xM,xm, + ym,ym,yM,yM,ym,ym,yM,yM, + zm,zm,zm,zm,zM,zM,zM,zM); + bbox_primitives.assign(6,1,4,1,1, 0,3,2,1, 4,5,6,7, 1,2,6,5, 0,4,7,3, 0,1,5,4, 2,3,7,6); + bbox_colors.assign(6,_spectrum,1,1,1,background_color[0]); + bbox_colors2.assign(6,_spectrum,1,1,1,foreground_color[0]); + bbox_opacities.assign(bbox_colors._width,1,1,1,0.3f); + + rotated_axes_vertices = axes_vertices.assign(7,3,1,1, + 0,20,0,0,22,-6,-6, + 0,0,20,0,-6,22,-6, + 0,0,0,20,0,0,22); + axes_opacities.assign(3,1,1,1,1); + axes_colors.assign(3,_spectrum,1,1,1,foreground_color[0]); + axes_primitives.assign(3,1,2,1,1, 0,1, 0,2, 0,3); + + // Begin user interaction loop + CImg visu0(*this), visu; + CImg zbuffer(visu0.width(),visu0.height(),1,1,0); + bool init_pose = true, clicked = false, redraw = true; + unsigned int key = 0; + int + x0 = 0, y0 = 0, x1 = 0, y1 = 0, + nrender_static = render_static, + nrender_motion = render_motion; + disp.show().flush(); + + while (!disp.is_closed() && !key) { + + // Init object pose + if (init_pose) { + const float + ratio = delta>0?(2.0f*cimg::min(disp.width(),disp.height())/(3.0f*delta)):1, + dx = (xM + xm)/2, dy = (yM + ym)/2, dz = (zM + zm)/2; + if (centering) + CImg(4,3,1,1, ratio,0.,0.,-ratio*dx, 0.,ratio,0.,-ratio*dy, 0.,0.,ratio,-ratio*dz).move_to(pose); + else CImg(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose); + if (pose_matrix) { + CImg pose0(pose_matrix,4,3,1,1,false); + pose0.resize(4,4,1,1,0); pose.resize(4,4,1,1,0); + pose0(3,3) = pose(3,3) = 1; + (pose0*pose).get_crop(0,0,3,2).move_to(pose); + Xoff = pose_matrix[12]; Yoff = pose_matrix[13]; Zoff = pose_matrix[14]; sprite_scale = pose_matrix[15]; + } else { Xoff = Yoff = Zoff = 0; sprite_scale = 1; } + init_pose = false; + redraw = true; + } + + // Rotate and draw 3d object + if (redraw) { + const float + r00 = pose(0,0), r10 = pose(1,0), r20 = pose(2,0), r30 = pose(3,0), + r01 = pose(0,1), r11 = pose(1,1), r21 = pose(2,1), r31 = pose(3,1), + r02 = pose(0,2), r12 = pose(1,2), r22 = pose(2,2), r32 = pose(3,2); + if ((clicked && nrender_motion>=0) || (!clicked && nrender_static>=0)) + cimg_forX(vertices,l) { + const float x = (float)vertices(l,0), y = (float)vertices(l,1), z = (float)vertices(l,2); + rotated_vertices(l,0) = r00*x + r10*y + r20*z + r30; + rotated_vertices(l,1) = r01*x + r11*y + r21*z + r31; + rotated_vertices(l,2) = r02*x + r12*y + r22*z + r32; + } + else cimg_forX(bbox_vertices,l) { + const float x = bbox_vertices(l,0), y = bbox_vertices(l,1), z = bbox_vertices(l,2); + rotated_bbox_vertices(l,0) = r00*x + r10*y + r20*z + r30; + rotated_bbox_vertices(l,1) = r01*x + r11*y + r21*z + r31; + rotated_bbox_vertices(l,2) = r02*x + r12*y + r22*z + r32; + } + + // Draw objects +#ifdef cimg_use_openmp + const bool render_with_zbuffer = true; +#else + const bool render_with_zbuffer = !clicked && nrender_static>0; +#endif + visu = visu0; + if ((clicked && nrender_motion<0) || (!clicked && nrender_static<0)) + visu.draw_object3d(Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, + rotated_bbox_vertices,bbox_primitives,bbox_colors,bbox_opacities,2,false,focale). + draw_object3d(Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, + rotated_bbox_vertices,bbox_primitives,bbox_colors2,1,false,focale); + else visu._draw_object3d((void*)0,render_with_zbuffer?zbuffer.fill(0):CImg::empty(), + Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, + rotated_vertices,reverse_primitives?reverse_primitives:primitives, + colors,opacities,clicked?nrender_motion:nrender_static,_is_double_sided==1,focale, + width()/2.0f+light_x,height()/2.0f+light_y,light_z+Zoff, + specular_lightness,specular_shininess,sprite_scale); + // Draw axes + if (ndisplay_axes) { + const float + n = (float)std::sqrt(1e-8 + r00*r00 + r01*r01 + r02*r02), + _r00 = r00/n, _r10 = r10/n, _r20 = r20/n, + _r01 = r01/n, _r11 = r11/n, _r21 = r21/n, + _r02 = r01/n, _r12 = r12/n, _r22 = r22/n, + Xaxes = 25, Yaxes = visu._height - 38.0f; + cimg_forX(axes_vertices,l) { + const float + x = axes_vertices(l,0), + y = axes_vertices(l,1), + z = axes_vertices(l,2); + rotated_axes_vertices(l,0) = _r00*x + _r10*y + _r20*z; + rotated_axes_vertices(l,1) = _r01*x + _r11*y + _r21*z; + rotated_axes_vertices(l,2) = _r02*x + _r12*y + _r22*z; + } + axes_opacities(0,0) = (rotated_axes_vertices(1,2)>0)?0.5f:1.0f; + axes_opacities(1,0) = (rotated_axes_vertices(2,2)>0)?0.5f:1.0f; + axes_opacities(2,0) = (rotated_axes_vertices(3,2)>0)?0.5f:1.0f; + visu.draw_object3d(Xaxes,Yaxes,0,rotated_axes_vertices,axes_primitives, + axes_colors,axes_opacities,1,false,focale). + draw_text((int)(Xaxes+rotated_axes_vertices(4,0)), + (int)(Yaxes+rotated_axes_vertices(4,1)), + "X",axes_colors[0]._data,0,axes_opacities(0,0),13). + draw_text((int)(Xaxes+rotated_axes_vertices(5,0)), + (int)(Yaxes+rotated_axes_vertices(5,1)), + "Y",axes_colors[1]._data,0,axes_opacities(1,0),13). + draw_text((int)(Xaxes+rotated_axes_vertices(6,0)), + (int)(Yaxes+rotated_axes_vertices(6,1)), + "Z",axes_colors[2]._data,0,axes_opacities(2,0),13); + } + visu.display(disp); + if (!clicked || nrender_motion==nrender_static) redraw = false; + } + + // Handle user interaction + disp.wait(); + if ((disp.button() || disp.wheel()) && disp.mouse_x()>=0 && disp.mouse_y()>=0) { + redraw = true; + if (!clicked) { x0 = x1 = disp.mouse_x(); y0 = y1 = disp.mouse_y(); if (!disp.wheel()) clicked = true; } + else { x1 = disp.mouse_x(); y1 = disp.mouse_y(); } + if (disp.button()&1) { + const float + R = 0.45f*cimg::min(disp.width(),disp.height()), + R2 = R*R, + u0 = (float)(x0-disp.width()/2), + v0 = (float)(y0-disp.height()/2), + u1 = (float)(x1-disp.width()/2), + v1 = (float)(y1-disp.height()/2), + n0 = (float)std::sqrt(u0*u0+v0*v0), + n1 = (float)std::sqrt(u1*u1+v1*v1), + nu0 = n0>R?(u0*R/n0):u0, + nv0 = n0>R?(v0*R/n0):v0, + nw0 = (float)std::sqrt(cimg::max(0,R2-nu0*nu0-nv0*nv0)), + nu1 = n1>R?(u1*R/n1):u1, + nv1 = n1>R?(v1*R/n1):v1, + nw1 = (float)std::sqrt(cimg::max(0,R2-nu1*nu1-nv1*nv1)), + u = nv0*nw1-nw0*nv1, + v = nw0*nu1-nu0*nw1, + w = nv0*nu1-nu0*nv1, + n = (float)std::sqrt(u*u+v*v+w*w), + alpha = (float)std::asin(n/R2); + (CImg::rotation_matrix(u,v,w,alpha)*pose).move_to(pose); + x0 = x1; y0 = y1; + } + if (disp.button()&2) { + if (focale>0) Zoff-=(y0-y1)*focale/400; + else { const float s = std::exp((y0-y1)/400.0f); pose*=s; sprite_scale*=s; } + x0 = x1; y0 = y1; + } + if (disp.wheel()) { + if (focale>0) Zoff-=disp.wheel()*focale/20; + else { const float s = std::exp(disp.wheel()/20.0f); pose*=s; sprite_scale*=s; } + disp.set_wheel(); + } + if (disp.button()&4) { Xoff+=(x1-x0); Yoff+=(y1-y0); x0 = x1; y0 = y1; } + if ((disp.button()&1) && (disp.button()&2)) { + init_pose = true; disp.set_button(); x0 = x1; y0 = y1; + pose = CImg(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0); + } + } else if (clicked) { x0 = x1; y0 = y1; clicked = false; redraw = true; } + + switch (key = disp.key()) { +#if cimg_OS!=2 + case cimg::keyCTRLRIGHT : +#endif + case 0 : case cimg::keyCTRLLEFT : key = 0; break; + case cimg::keyD: if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), + CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). + _is_resized = true; + disp.set_key(key,false); key = 0; + } break; + case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; + disp.set_key(key,false); key = 0; + } break; + case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true; + disp.set_key(key,false); key = 0; + } break; + case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + if (!ns_width || !ns_height || + ns_width>(unsigned int)disp.screen_width() || ns_height>(unsigned int)disp.screen_height()) { + ns_width = disp.screen_width()*3U/4; + ns_height = disp.screen_height()*3U/4; + } + if (disp.is_fullscreen()) disp.resize(ns_width,ns_height,false); + else { + ns_width = (unsigned int)disp.width(); ns_height = disp.height(); + disp.resize(disp.screen_width(),disp.screen_height(),false); + } + disp.toggle_fullscreen()._is_resized = true; + disp.set_key(key,false); key = 0; + } break; + case cimg::keyT : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + // Switch single/double-sided primitives. + if (--_is_double_sided==-2) _is_double_sided = 1; + if (_is_double_sided>=0) reverse_primitives.assign(); + else primitives.get_reverse_object3d().move_to(reverse_primitives); + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyZ : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Enable/disable Z-buffer + if (zbuffer) zbuffer.assign(); + else zbuffer.assign(visu0.width(),visu0.height(),1,1,0); + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyA : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Show/hide 3d axes. + ndisplay_axes = !ndisplay_axes; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyF1 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to points. + nrender_motion = (nrender_static==0 && nrender_motion!=0)?0:-1; nrender_static = 0; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyF2 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to lines. + nrender_motion = (nrender_static==1 && nrender_motion!=1)?1:-1; nrender_static = 1; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyF3 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat. + nrender_motion = (nrender_static==2 && nrender_motion!=2)?2:-1; nrender_static = 2; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyF4 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat-shaded. + nrender_motion = (nrender_static==3 && nrender_motion!=3)?3:-1; nrender_static = 3; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyF5 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + // Set rendering mode to gouraud-shaded. + nrender_motion = (nrender_static==4 && nrender_motion!=4)?4:-1; nrender_static = 4; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyF6 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to phong-shaded. + nrender_motion = (nrender_static==5 && nrender_motion!=5)?5:-1; nrender_static = 5; + disp.set_key(key,false); key = 0; redraw = true; + } break; + case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save snapshot + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu).draw_text(0,0," Saving snapshot... ", + foreground_color._data,background_color._data,0.7f,13).display(disp); + visu.save(filename); + (+visu).draw_text(0,0," Snapshot '%s' saved. ", + foreground_color._data,background_color._data,0.7f,13,filename).display(disp); + disp.set_key(key,false); key = 0; + } break; + case cimg::keyG : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .off file + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.off",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu).draw_text(0,0," Saving object... ", + foreground_color._data,background_color._data,0.7f,13).display(disp); + vertices.save_off(reverse_primitives?reverse_primitives:primitives,colors,filename); + (+visu).draw_text(0,0," Object '%s' saved. ", + foreground_color._data,background_color._data,0.7f,13,filename).display(disp); + disp.set_key(key,false); key = 0; + } break; + case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .cimg file + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { +#ifdef cimg_use_zlib + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); +#else + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); +#endif + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu).draw_text(0,0," Saving object... ", + foreground_color._data,background_color._data,0.7f,13).display(disp); + vertices.get_object3dtoCImg3d(reverse_primitives?reverse_primitives:primitives,colors,opacities). + save(filename); + (+visu).draw_text(0,0," Object '%s' saved. ", + foreground_color._data,background_color._data,0.7f,13,filename).display(disp); + disp.set_key(key,false); key = 0; + } break; +#ifdef cimg_use_board + case cimg::keyP : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .EPS file + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.eps",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu).draw_text(0,0," Saving EPS snapshot... ", + foreground_color._data,background_color._data,0.7f,13).display(disp); + LibBoard::Board board; + (+visu)._draw_object3d(&board,zbuffer.fill(0), + Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, + rotated_vertices,reverse_primitives?reverse_primitives:primitives, + colors,opacities,clicked?nrender_motion:nrender_static, + _is_double_sided==1,focale, + visu.width()/2.0f+light_x,visu.height()/2.0f+light_y,light_z+Zoff, + specular_lightness,specular_shininess, + sprite_scale); + board.saveEPS(filename); + (+visu).draw_text(0,0," Object '%s' saved. ", + foreground_color._data,background_color._data,0.7f,13,filename).display(disp); + disp.set_key(key,false); key = 0; + } break; + case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .SVG file + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.svg",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu).draw_text(0,0," Saving SVG snapshot... ", + foreground_color._data,background_color._data,0.7f,13).display(disp); + LibBoard::Board board; + (+visu)._draw_object3d(&board,zbuffer.fill(0), + Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, + rotated_vertices,reverse_primitives?reverse_primitives:primitives, + colors,opacities,clicked?nrender_motion:nrender_static, + _is_double_sided==1,focale, + visu.width()/2.0f+light_x,visu.height()/2.0f+light_y,light_z+Zoff, + specular_lightness,specular_shininess, + sprite_scale); + board.saveSVG(filename); + (+visu).draw_text(0,0," Object '%s' saved. ", + foreground_color._data,background_color._data,0.7f,13,filename).display(disp); + disp.set_key(key,false); key = 0; + } break; +#endif + } + if (disp.is_resized()) { + disp.resize(false); visu0 = get_resize(disp,1); + if (zbuffer) zbuffer.assign(disp.width(),disp.height()); + redraw = true; + } + } + if (pose_matrix) { + std::memcpy(pose_matrix,pose._data,12*sizeof(float)); + pose_matrix[12] = Xoff; pose_matrix[13] = Yoff; pose_matrix[14] = Zoff; pose_matrix[15] = sprite_scale; + } + disp.set_button().set_key(key); + return *this; + } + + //! Display 1d graph in an interactive window. + /** + \param disp Display window. + \param plot_type Plot type. Can be { 0=points | 1=segments | 2=splines | 3=bars }. + \param vertex_type Vertex type. + \param labelx Title for the horizontal axis, as a C-string. + \param xmin Minimum value along the X-axis. + \param xmax Maximum value along the X-axis. + \param labely Title for the vertical axis, as a C-string. + \param ymin Minimum value along the X-axis. + \param ymax Maximum value along the X-axis. + **/ + const CImg& display_graph(CImgDisplay &disp, + const unsigned int plot_type=1, const unsigned int vertex_type=1, + const char *const labelx=0, const double xmin=0, const double xmax=0, + const char *const labely=0, const double ymin=0, const double ymax=0) const { + return _display_graph(disp,0,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax); + } + + //! Display 1d graph in an interactive window \overloading. + const CImg& display_graph(const char *const title=0, + const unsigned int plot_type=1, const unsigned int vertex_type=1, + const char *const labelx=0, const double xmin=0, const double xmax=0, + const char *const labely=0, const double ymin=0, const double ymax=0) const { + CImgDisplay disp; + return _display_graph(disp,title,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax); + } + + const CImg& _display_graph(CImgDisplay &disp, const char *const title=0, + const unsigned int plot_type=1, const unsigned int vertex_type=1, + const char *const labelx=0, const double xmin=0, const double xmax=0, + const char *const labely=0, const double ymin=0, const double ymax=0) const { + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "display_graph(): Empty instance.", + cimg_instance); + if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0). + set_title(title?"%s":"CImg<%s>",title?title:pixel_type()); + const unsigned long siz = (unsigned long)_width*_height*_depth, siz1 = cimg::max(1U,siz-1); + const unsigned int old_normalization = disp.normalization(); + disp.show().flush()._normalization = 0; + + double y0 = ymin, y1 = ymax, nxmin = xmin, nxmax = xmax; + if (nxmin==nxmax) { nxmin = 0; nxmax = siz1; } + int x0 = 0, x1 = width()*height()*depth() - 1, key = 0; + + for (bool reset_view = true; !key && !disp.is_closed(); ) { + if (reset_view) { x0 = 0; x1 = width()*height()*depth()-1; y0 = ymin; y1 = ymax; reset_view = false; } + CImg zoom(x1-x0+1,1,1,spectrum()); + cimg_forC(*this,c) zoom.get_shared_channel(c) = CImg(data(x0,0,0,c),x1-x0+1,1,1,1,true); + if (y0==y1) { y0 = zoom.min_max(y1); const double dy = y1 - y0; y0-=dy/20; y1+=dy/20; } + if (y0==y1) { --y0; ++y1; } + + const CImg selection = zoom.get_select_graph(disp,plot_type,vertex_type, + labelx, + nxmin + x0*(nxmax-nxmin)/siz1, + nxmin + x1*(nxmax-nxmin)/siz1, + labely,y0,y1); + const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y(); + if (selection[0]>=0) { + if (selection[2]<0) reset_view = true; + else { + x1 = x0 + selection[2]; x0+=selection[0]; + if (selection[1]>=0 && selection[3]>=0) { + y0 = y1 - selection[3]*(y1-y0)/(disp.height()-32); + y1-=selection[1]*(y1-y0)/(disp.height()-32); + } + } + } else { + bool go_in = false, go_out = false, go_left = false, go_right = false, go_up = false, go_down = false; + switch (key = disp.key()) { + case cimg::keyHOME : reset_view = true; key = 0; disp.set_key(); break; + case cimg::keyPADADD : go_in = true; go_out = false; key = 0; disp.set_key(); break; + case cimg::keyPADSUB : go_out = true; go_in = false; key = 0; disp.set_key(); break; + case cimg::keyARROWLEFT : case cimg::keyPAD4 : go_left = true; go_right = false; key = 0; disp.set_key(); + break; + case cimg::keyARROWRIGHT : case cimg::keyPAD6 : go_right = true; go_left = false; key = 0; disp.set_key(); + break; + case cimg::keyARROWUP : case cimg::keyPAD8 : go_up = true; go_down = false; key = 0; disp.set_key(); break; + case cimg::keyARROWDOWN : case cimg::keyPAD2 : go_down = true; go_up = false; key = 0; disp.set_key(); break; + case cimg::keyPAD7 : go_left = true; go_up = true; key = 0; disp.set_key(); break; + case cimg::keyPAD9 : go_right = true; go_up = true; key = 0; disp.set_key(); break; + case cimg::keyPAD1 : go_left = true; go_down = true; key = 0; disp.set_key(); break; + case cimg::keyPAD3 : go_right = true; go_down = true; key = 0; disp.set_key(); break; + } + if (disp.wheel()) { + if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) go_out = !(go_in = disp.wheel()>0); + else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) go_left = !(go_right = disp.wheel()>0); + else go_up = !(go_down = disp.wheel()<0); + key = 0; + } + + if (go_in) { + const int + xsiz = x1 - x0, + mx = (mouse_x-16)*xsiz/(disp.width()-32), + cx = x0 + (mx<0?0:(mx>=xsiz?xsiz:mx)); + if (x1-x0>4) { + x0 = cx - 7*(cx-x0)/8; x1 = cx + 7*(x1-cx)/8; + if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + const double + ysiz = y1 - y0, + my = (mouse_y-16)*ysiz/(disp.height()-32), + cy = y1 - (my<0?0:(my>=ysiz?ysiz:my)); + y0 = cy - 7*(cy-y0)/8; y1 = cy + 7*(y1-cy)/8; + } else y0 = y1 = 0; + } + } + if (go_out) { + if (x0>0 || x1<(int)siz1) { + const int delta_x = (x1-x0)/8, ndelta_x = delta_x?delta_x:(siz>1?1:0); + const double ndelta_y = (y1-y0)/8; + x0-=ndelta_x; x1+=ndelta_x; + y0-=ndelta_y; y1+=ndelta_y; + if (x0<0) { x1-=x0; x0 = 0; if (x1>=(int)siz) x1 = (int)siz1; } + if (x1>=(int)siz) { x0-=(x1-siz1); x1 = (int)siz1; if (x0<0) x0 = 0; } + } + } + if (go_left) { + const int delta = (x1-x0)/5, ndelta = delta?delta:1; + if (x0-ndelta>=0) { x0-=ndelta; x1-=ndelta; } + else { x1-=x0; x0 = 0; } + go_left = false; + } + if (go_right) { + const int delta = (x1-x0)/5, ndelta = delta?delta:1; + if (x1+ndelta<(int)siz) { x0+=ndelta; x1+=ndelta; } + else { x0+=(siz1-x1); x1 = siz1; } + go_right = false; + } + if (go_up) { + const double delta = (y1-y0)/10, ndelta = delta?delta:1; + y0+=ndelta; y1+=ndelta; + go_up = false; + } + if (go_down) { + const double delta = (y1-y0)/10, ndelta = delta?delta:1; + y0-=ndelta; y1-=ndelta; + go_down = false; + } + } + } + disp._normalization = old_normalization; + return *this; + } + + //! Save image as a file. + /** + \param filename Filename, as a C-string. + \param number When positive, represents an index added to the filename. Otherwise, no number is added. + \param digits Number of digits used for adding the number to the filename. + \note + - The used file format is defined by the file extension in the filename \p filename. + - Parameter \p number can be used to add a 6-digit number to the filename before saving. + + **/ + const CImg& save(const char *const filename, const int number=-1, const unsigned int digits=6) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save(): Specified filename is (null).", + cimg_instance); + // Do not test for empty instances, since .cimg format is able to manage empty instances. + const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.'); + const char *const ext = cimg::split_filename(filename); + char nfilename[1024] = { 0 }; + const char *const fn = is_stdout?filename:(number>=0)?cimg::number_filename(filename,number,digits,nfilename): + filename; + +#ifdef cimg_save_plugin + cimg_save_plugin(fn); +#endif +#ifdef cimg_save_plugin1 + cimg_save_plugin1(fn); +#endif +#ifdef cimg_save_plugin2 + cimg_save_plugin2(fn); +#endif +#ifdef cimg_save_plugin3 + cimg_save_plugin3(fn); +#endif +#ifdef cimg_save_plugin4 + cimg_save_plugin4(fn); +#endif +#ifdef cimg_save_plugin5 + cimg_save_plugin5(fn); +#endif +#ifdef cimg_save_plugin6 + cimg_save_plugin6(fn); +#endif +#ifdef cimg_save_plugin7 + cimg_save_plugin7(fn); +#endif +#ifdef cimg_save_plugin8 + cimg_save_plugin8(fn); +#endif + // Ascii formats + if (!cimg::strcasecmp(ext,"asc")) return save_ascii(fn); + else if (!cimg::strcasecmp(ext,"dlm") || + !cimg::strcasecmp(ext,"txt")) return save_dlm(fn); + else if (!cimg::strcasecmp(ext,"cpp") || + !cimg::strcasecmp(ext,"hpp") || + !cimg::strcasecmp(ext,"h") || + !cimg::strcasecmp(ext,"c")) return save_cpp(fn); + + // 2d binary formats + else if (!cimg::strcasecmp(ext,"bmp")) return save_bmp(fn); + else if (!cimg::strcasecmp(ext,"jpg") || + !cimg::strcasecmp(ext,"jpeg") || + !cimg::strcasecmp(ext,"jpe") || + !cimg::strcasecmp(ext,"jfif") || + !cimg::strcasecmp(ext,"jif")) return save_jpeg(fn); + else if (!cimg::strcasecmp(ext,"rgb")) return save_rgb(fn); + else if (!cimg::strcasecmp(ext,"rgba")) return save_rgba(fn); + else if (!cimg::strcasecmp(ext,"png")) return save_png(fn); + else if (!cimg::strcasecmp(ext,"pgm") || + !cimg::strcasecmp(ext,"ppm") || + !cimg::strcasecmp(ext,"pnm")) return save_pnm(fn); + else if (!cimg::strcasecmp(ext,"pnk")) return save_pnk(fn); + else if (!cimg::strcasecmp(ext,"pfm")) return save_pfm(fn); + else if (!cimg::strcasecmp(ext,"exr")) return save_exr(fn); + else if (!cimg::strcasecmp(ext,"tif") || + !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn); + + // 3d binary formats + else if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true); + else if (!cimg::strcasecmp(ext,"cimg") || !*ext) return save_cimg(fn,false); + else if (!cimg::strcasecmp(ext,"dcm")) return save_medcon_external(fn); + else if (!cimg::strcasecmp(ext,"hdr") || + !cimg::strcasecmp(ext,"nii")) return save_analyze(fn); + else if (!cimg::strcasecmp(ext,"inr")) return save_inr(fn); + else if (!cimg::strcasecmp(ext,"mnc")) return save_minc2(fn); + else if (!cimg::strcasecmp(ext,"pan")) return save_pandore(fn); + else if (!cimg::strcasecmp(ext,"raw")) return save_raw(fn); + + // Archive files + else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn); + + // Image sequences + else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,true); + else if (!cimg::strcasecmp(ext,"avi") || + !cimg::strcasecmp(ext,"mov") || + !cimg::strcasecmp(ext,"asf") || + !cimg::strcasecmp(ext,"divx") || + !cimg::strcasecmp(ext,"flv") || + !cimg::strcasecmp(ext,"mpg") || + !cimg::strcasecmp(ext,"m1v") || + !cimg::strcasecmp(ext,"m2v") || + !cimg::strcasecmp(ext,"m4v") || + !cimg::strcasecmp(ext,"mjp") || + !cimg::strcasecmp(ext,"mp4") || + !cimg::strcasecmp(ext,"mkv") || + !cimg::strcasecmp(ext,"mpe") || + !cimg::strcasecmp(ext,"movie") || + !cimg::strcasecmp(ext,"ogm") || + !cimg::strcasecmp(ext,"ogg") || + !cimg::strcasecmp(ext,"ogv") || + !cimg::strcasecmp(ext,"qt") || + !cimg::strcasecmp(ext,"rm") || + !cimg::strcasecmp(ext,"vob") || + !cimg::strcasecmp(ext,"wmv") || + !cimg::strcasecmp(ext,"xvid") || + !cimg::strcasecmp(ext,"mpeg")) return save_ffmpeg(fn); + return save_other(fn); + } + + //! Save image as an ascii file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_ascii(const char *const filename) const { + return _save_ascii(0,filename); + } + + //! Save image as an ascii file \overloading. + const CImg& save_ascii(std::FILE *const file) const { + return _save_ascii(file,0); + } + + const CImg& _save_ascii(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_ascii(): Specified filename is (null).", + cimg_instance); + std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); + std::fprintf(nfile,"%u %u %u %u\n",_width,_height,_depth,_spectrum); + const T* ptrs = _data; + cimg_forYZC(*this,y,z,c) { + cimg_forX(*this,x) std::fprintf(nfile,"%.16g ",(double)*(ptrs++)); + std::fputc('\n',nfile); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a .cpp source file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_cpp(const char *const filename) const { + return _save_cpp(0,filename); + } + + //! Save image as a .cpp source file \overloading. + const CImg& save_cpp(std::FILE *const file) const { + return _save_cpp(file,0); + } + + const CImg& _save_cpp(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_cpp(): Specified filename is (null).", + cimg_instance); + std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); + char varname[1024] = { 0 }; + if (filename) std::sscanf(cimg::basename(filename),"%1023[a-zA-Z0-9_]",varname); + if (!*varname) cimg_snprintf(varname,sizeof(varname),"unnamed"); + std::fprintf(nfile, + "/* Define image '%s' of size %ux%ux%ux%u and type '%s' */\n" + "%s data_%s[] = { %s\n ", + varname,_width,_height,_depth,_spectrum,pixel_type(),pixel_type(),varname, + is_empty()?"};":""); + if (!is_empty()) for (unsigned long off = 0, siz = size()-1; off<=siz; ++off) { + std::fprintf(nfile,cimg::type::format(),cimg::type::format((*this)[off])); + if (off==siz) std::fprintf(nfile," };\n"); + else if (!((off+1)%16)) std::fprintf(nfile,",\n "); + else std::fprintf(nfile,", "); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a DLM file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_dlm(const char *const filename) const { + return _save_dlm(0,filename); + } + + //! Save image as a DLM file \overloading. + const CImg& save_dlm(std::FILE *const file) const { + return _save_dlm(file,0); + } + + const CImg& _save_dlm(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_dlm(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_depth>1) + cimg::warn(_cimg_instance + "save_dlm(): Instance is volumetric, values along Z will be unrolled in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + if (_spectrum>1) + cimg::warn(_cimg_instance + "save_dlm(): Instance is multispectral, values along C will be unrolled in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); + const T* ptrs = _data; + cimg_forYZC(*this,y,z,c) { + cimg_forX(*this,x) std::fprintf(nfile,"%.16g%s",(double)*(ptrs++),(x==width()-1)?"":","); + std::fputc('\n',nfile); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a BMP file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_bmp(const char *const filename) const { + return _save_bmp(0,filename); + } + + //! Save image as a BMP file \overloading. + const CImg& save_bmp(std::FILE *const file) const { + return _save_bmp(file,0); + } + + const CImg& _save_bmp(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_bmp(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_depth>1) + cimg::warn(_cimg_instance + "save_bmp(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + if (_spectrum>3) + cimg::warn(_cimg_instance + "save_bmp(): Instance is multispectral, only the three first channels will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + unsigned char header[54] = { 0 }, align_buf[4] = { 0 }; + const unsigned int + align = (4 - (3*_width)%4)%4, + buf_size = (3*_width + align)*height(), + file_size = 54 + buf_size; + header[0] = 'B'; header[1] = 'M'; + header[0x02] = file_size&0xFF; + header[0x03] = (file_size>>8)&0xFF; + header[0x04] = (file_size>>16)&0xFF; + header[0x05] = (file_size>>24)&0xFF; + header[0x0A] = 0x36; + header[0x0E] = 0x28; + header[0x12] = _width&0xFF; + header[0x13] = (_width>>8)&0xFF; + header[0x14] = (_width>>16)&0xFF; + header[0x15] = (_width>>24)&0xFF; + header[0x16] = _height&0xFF; + header[0x17] = (_height>>8)&0xFF; + header[0x18] = (_height>>16)&0xFF; + header[0x19] = (_height>>24)&0xFF; + header[0x1A] = 1; + header[0x1B] = 0; + header[0x1C] = 24; + header[0x1D] = 0; + header[0x22] = buf_size&0xFF; + header[0x23] = (buf_size>>8)&0xFF; + header[0x24] = (buf_size>>16)&0xFF; + header[0x25] = (buf_size>>24)&0xFF; + header[0x27] = 0x1; + header[0x2B] = 0x1; + cimg::fwrite(header,54,nfile); + + const T + *ptr_r = data(0,_height-1,0,0), + *ptr_g = (_spectrum>=2)?data(0,_height-1,0,1):0, + *ptr_b = (_spectrum>=3)?data(0,_height-1,0,2):0; + + switch (_spectrum) { + case 1 : { + cimg_forY(*this,y) { + cimg_forX(*this,x) { + const unsigned char val = (unsigned char)*(ptr_r++); + std::fputc(val,nfile); std::fputc(val,nfile); std::fputc(val,nfile); + } + cimg::fwrite(align_buf,align,nfile); + ptr_r-=2*_width; + } + } break; + case 2 : { + cimg_forY(*this,y) { + cimg_forX(*this,x) { + std::fputc(0,nfile); + std::fputc((unsigned char)(*(ptr_g++)),nfile); + std::fputc((unsigned char)(*(ptr_r++)),nfile); + } + cimg::fwrite(align_buf,align,nfile); + ptr_r-=2*_width; ptr_g-=2*_width; + } + } break; + default : { + cimg_forY(*this,y) { + cimg_forX(*this,x) { + std::fputc((unsigned char)(*(ptr_b++)),nfile); + std::fputc((unsigned char)(*(ptr_g++)),nfile); + std::fputc((unsigned char)(*(ptr_r++)),nfile); + } + cimg::fwrite(align_buf,align,nfile); + ptr_r-=2*_width; ptr_g-=2*_width; ptr_b-=2*_width; + } + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a JPEG file. + /** + \param filename Filename, as a C-string. + \param quality Image quality (in %) + **/ + const CImg& save_jpeg(const char *const filename, const unsigned int quality=100) const { + return _save_jpeg(0,filename,quality); + } + + //! Save image as a JPEG file \overloading. + const CImg& save_jpeg(std::FILE *const file, const unsigned int quality=100) const { + return _save_jpeg(file,0,quality); + } + + const CImg& _save_jpeg(std::FILE *const file, const char *const filename, const unsigned int quality) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_jpeg(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_depth>1) + cimg::warn(_cimg_instance + "save_jpeg(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + +#ifndef cimg_use_jpeg + if (!file) return save_other(filename,quality); + else throw CImgIOException(_cimg_instance + "save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.", + cimg_instance); +#else + unsigned int dimbuf = 0; + J_COLOR_SPACE colortype = JCS_RGB; + + switch(_spectrum) { + case 1 : dimbuf = 1; colortype = JCS_GRAYSCALE; break; + case 2 : dimbuf = 3; colortype = JCS_RGB; break; + case 3 : dimbuf = 3; colortype = JCS_RGB; break; + default : dimbuf = 4; colortype = JCS_CMYK; break; + } + + // Call libjpeg functions + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + jpeg_stdio_dest(&cinfo,nfile); + cinfo.image_width = _width; + cinfo.image_height = _height; + cinfo.input_components = dimbuf; + cinfo.in_color_space = colortype; + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo,quality<100?quality:100,TRUE); + jpeg_start_compress(&cinfo,TRUE); + + JSAMPROW row_pointer[1]; + CImg buffer((unsigned long)_width*dimbuf); + + while (cinfo.next_scanline& save_magick(const char *const filename, const unsigned int bytes_per_pixel=0) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_magick(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifdef cimg_use_magick + double stmin, stmax = (double)max_min(stmin); + if (_depth>1) + cimg::warn(_cimg_instance + "save_magick(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename); + + if (_spectrum>3) + cimg::warn(_cimg_instance + "save_magick(): Instance is multispectral, only the three first channels will be " + "saved in file '%s'.", + cimg_instance, + filename); + + if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536) + cimg::warn(_cimg_instance + "save_magick(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.", + cimg_instance, + filename,stmin,stmax); + + Magick::Image image(Magick::Geometry(_width,_height),"black"); + image.type(Magick::TrueColorType); + image.depth(bytes_per_pixel?(8*bytes_per_pixel):(stmax>=256?16:8)); + const T + *ptr_r = data(0,0,0,0), + *ptr_g = _spectrum>1?data(0,0,0,1):0, + *ptr_b = _spectrum>2?data(0,0,0,2):0; + Magick::PixelPacket *pixels = image.getPixels(0,0,_width,_height); + switch (_spectrum) { + case 1 : // Scalar images + for (unsigned long off = (unsigned long)_width*_height; off; --off) { + pixels->red = pixels->green = pixels->blue = (Magick::Quantum)*(ptr_r++); + ++pixels; + } + break; + case 2 : // RG images + for (unsigned long off = (unsigned long)_width*_height; off; --off) { + pixels->red = (Magick::Quantum)*(ptr_r++); + pixels->green = (Magick::Quantum)*(ptr_g++); + pixels->blue = 0; ++pixels; + } + break; + default : // RGB images + for (unsigned long off = (unsigned long)_width*_height; off; --off) { + pixels->red = (Magick::Quantum)*(ptr_r++); + pixels->green = (Magick::Quantum)*(ptr_g++); + pixels->blue = (Magick::Quantum)*(ptr_b++); + ++pixels; + } + } + image.syncPixels(); + image.write(filename); + return *this; +#else + cimg::unused(bytes_per_pixel); + throw CImgIOException(_cimg_instance + "save_magick(): Unable to save file '%s' unless libMagick++ is enabled.", + cimg_instance, + filename); +#endif + } + + //! Save image as a PNG file. + /** + \param filename Filename, as a C-string. + \param bytes_per_pixel Force the number of bytes per pixels for the saving, when possible. + **/ + const CImg& save_png(const char *const filename, const unsigned int bytes_per_pixel=0) const { + return _save_png(0,filename,bytes_per_pixel); + } + + //! Save image as a PNG file \overloading. + const CImg& save_png(std::FILE *const file, const unsigned int bytes_per_pixel=0) const { + return _save_png(file,0,bytes_per_pixel); + } + + const CImg& _save_png(std::FILE *const file, const char *const filename, + const unsigned int bytes_per_pixel=0) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_png(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + +#ifndef cimg_use_png + cimg::unused(bytes_per_pixel); + if (!file) return save_other(filename); + else throw CImgIOException(_cimg_instance + "save_png(): Unable to save data in '(*FILE)' unless libpng is enabled.", + cimg_instance); +#else + const char *volatile nfilename = filename; // two 'volatile' here to remove a g++ warning due to 'setjmp'. + std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"wb"); + volatile double stmin, stmax = (double)max_min(stmin); + + if (_depth>1) + cimg::warn(_cimg_instance + "save_png(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename); + + if (_spectrum>4) + cimg::warn(_cimg_instance + "save_png(): Instance is multispectral, only the three first channels will be saved in file '%s'.", + cimg_instance, + filename); + + if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536) + cimg::warn(_cimg_instance + "save_png(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.", + cimg_instance, + filename,stmin,stmax); + + // Setup PNG structures for write + png_voidp user_error_ptr = 0; + png_error_ptr user_error_fn = 0, user_warning_fn = 0; + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,user_error_ptr, user_error_fn, + user_warning_fn); + if(!png_ptr){ + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "save_png(): Failed to initialize 'png_ptr' structure when saving file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + png_infop info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_write_struct(&png_ptr,(png_infopp)0); + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "save_png(): Failed to initialize 'info_ptr' structure when saving file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + if (setjmp(png_jmpbuf(png_ptr))) { + png_destroy_write_struct(&png_ptr, &info_ptr); + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + png_init_io(png_ptr, nfile); + + const int bit_depth = bytes_per_pixel?(bytes_per_pixel*8):(stmax>=256?16:8); + + int color_type; + switch (spectrum()) { + case 1 : color_type = PNG_COLOR_TYPE_GRAY; break; + case 2 : color_type = PNG_COLOR_TYPE_GRAY_ALPHA; break; + case 3 : color_type = PNG_COLOR_TYPE_RGB; break; + default : color_type = PNG_COLOR_TYPE_RGB_ALPHA; + } + const int interlace_type = PNG_INTERLACE_NONE; + const int compression_type = PNG_COMPRESSION_TYPE_DEFAULT; + const int filter_method = PNG_FILTER_TYPE_DEFAULT; + png_set_IHDR(png_ptr,info_ptr,_width,_height,bit_depth,color_type,interlace_type,compression_type,filter_method); + png_write_info(png_ptr,info_ptr); + const int byte_depth = bit_depth>>3; + const int numChan = spectrum()>4?4:spectrum(); + const int pixel_bit_depth_flag = numChan * (bit_depth-1); + + // Allocate Memory for Image Save and Fill pixel data + png_bytep *const imgData = new png_byte*[_height]; + for (unsigned int row = 0; row<_height; ++row) imgData[row] = new png_byte[byte_depth*numChan*_width]; + const T *pC0 = data(0,0,0,0); + switch (pixel_bit_depth_flag) { + case 7 : { // Gray 8-bit + cimg_forY(*this,y) { + unsigned char *ptrd = imgData[y]; + cimg_forX(*this,x) *(ptrd++) = (unsigned char)*(pC0++); + } + } break; + case 14 : { // Gray w/ Alpha 8-bit + const T *pC1 = data(0,0,0,1); + cimg_forY(*this,y) { + unsigned char *ptrd = imgData[y]; + cimg_forX(*this,x) { + *(ptrd++) = (unsigned char)*(pC0++); + *(ptrd++) = (unsigned char)*(pC1++); + } + } + } break; + case 21 : { // RGB 8-bit + const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2); + cimg_forY(*this,y) { + unsigned char *ptrd = imgData[y]; + cimg_forX(*this,x) { + *(ptrd++) = (unsigned char)*(pC0++); + *(ptrd++) = (unsigned char)*(pC1++); + *(ptrd++) = (unsigned char)*(pC2++); + } + } + } break; + case 28 : { // RGB x/ Alpha 8-bit + const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3); + cimg_forY(*this,y){ + unsigned char *ptrd = imgData[y]; + cimg_forX(*this,x){ + *(ptrd++) = (unsigned char)*(pC0++); + *(ptrd++) = (unsigned char)*(pC1++); + *(ptrd++) = (unsigned char)*(pC2++); + *(ptrd++) = (unsigned char)*(pC3++); + } + } + } break; + case 15 : { // Gray 16-bit + cimg_forY(*this,y){ + unsigned short *ptrd = (unsigned short*)(imgData[y]); + cimg_forX(*this,x) *(ptrd++) = (unsigned short)*(pC0++); + if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],_width); + } + } break; + case 30 : { // Gray w/ Alpha 16-bit + const T *pC1 = data(0,0,0,1); + cimg_forY(*this,y){ + unsigned short *ptrd = (unsigned short*)(imgData[y]); + cimg_forX(*this,x) { + *(ptrd++) = (unsigned short)*(pC0++); + *(ptrd++) = (unsigned short)*(pC1++); + } + if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],2*_width); + } + } break; + case 45 : { // RGB 16-bit + const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2); + cimg_forY(*this,y) { + unsigned short *ptrd = (unsigned short*)(imgData[y]); + cimg_forX(*this,x) { + *(ptrd++) = (unsigned short)*(pC0++); + *(ptrd++) = (unsigned short)*(pC1++); + *(ptrd++) = (unsigned short)*(pC2++); + } + if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],3*_width); + } + } break; + case 60 : { // RGB w/ Alpha 16-bit + const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3); + cimg_forY(*this,y) { + unsigned short *ptrd = (unsigned short*)(imgData[y]); + cimg_forX(*this,x) { + *(ptrd++) = (unsigned short)*(pC0++); + *(ptrd++) = (unsigned short)*(pC1++); + *(ptrd++) = (unsigned short)*(pC2++); + *(ptrd++) = (unsigned short)*(pC3++); + } + if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],4*_width); + } + } break; + default : + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimg_instance + "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.", + cimg_instance, + nfilename?nfilename:"(FILE*)"); + } + png_write_image(png_ptr,imgData); + png_write_end(png_ptr,info_ptr); + png_destroy_write_struct(&png_ptr, &info_ptr); + + // Deallocate Image Write Memory + cimg_forY(*this,n) delete[] imgData[n]; + delete[] imgData; + + if (!file) cimg::fclose(nfile); + return *this; +#endif + } + + //! Save image as a PNM file. + /** + \param filename Filename, as a C-string. + \param bytes_per_pixel Force the number of bytes per pixels for the saving. + **/ + const CImg& save_pnm(const char *const filename, const unsigned int bytes_per_pixel=0) const { + return _save_pnm(0,filename,bytes_per_pixel); + } + + //! Save image as a PNM file \overloading. + const CImg& save_pnm(std::FILE *const file, const unsigned int bytes_per_pixel=0) const { + return _save_pnm(file,0,bytes_per_pixel); + } + + const CImg& _save_pnm(std::FILE *const file, const char *const filename, + const unsigned int bytes_per_pixel=0) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_pnm(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + + double stmin, stmax = (double)max_min(stmin); + if (_depth>1) + cimg::warn(_cimg_instance + "save_pnm(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + if (_spectrum>3) + cimg::warn(_cimg_instance + "save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536) + cimg::warn(_cimg_instance + "save_pnm(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.", + cimg_instance, + stmin,stmax,filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const T + *ptr_r = data(0,0,0,0), + *ptr_g = (_spectrum>=2)?data(0,0,0,1):0, + *ptr_b = (_spectrum>=3)?data(0,0,0,2):0; + const unsigned long buf_size = cimg::min(1024*1024UL,_width*_height*(_spectrum==1?1UL:3UL)); + + std::fprintf(nfile,"P%c\n%u %u\n%u\n", + (_spectrum==1?'5':'6'),_width,_height,stmax<256?255:(stmax<4096?4095:65535)); + + switch (_spectrum) { + case 1 : { // Scalar image + if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PGM 8 bits + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size); + unsigned char *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr_r++); + cimg::fwrite(buf._data,N,nfile); + to_write-=N; + } + } else { // Binary PGM 16 bits + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size); + unsigned short *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) *(ptrd++) = (unsigned short)*(ptr_r++); + if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); + cimg::fwrite(buf._data,N,nfile); + to_write-=N; + } + } + } break; + case 2 : { // RG image + if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); + unsigned char *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) { + *(ptrd++) = (unsigned char)*(ptr_r++); + *(ptrd++) = (unsigned char)*(ptr_g++); + *(ptrd++) = 0; + } + cimg::fwrite(buf._data,3*N,nfile); + to_write-=N; + } + } else { // Binary PPM 16 bits + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); + unsigned short *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) { + *(ptrd++) = (unsigned short)*(ptr_r++); + *(ptrd++) = (unsigned short)*(ptr_g++); + *(ptrd++) = 0; + } + if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); + cimg::fwrite(buf._data,3*N,nfile); + to_write-=N; + } + } + } break; + default : { // RGB image + if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); + unsigned char *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) { + *(ptrd++) = (unsigned char)*(ptr_r++); + *(ptrd++) = (unsigned char)*(ptr_g++); + *(ptrd++) = (unsigned char)*(ptr_b++); + } + cimg::fwrite(buf._data,3*N,nfile); + to_write-=N; + } + } else { // Binary PPM 16 bits + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); + unsigned short *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) { + *(ptrd++) = (unsigned short)*(ptr_r++); + *(ptrd++) = (unsigned short)*(ptr_g++); + *(ptrd++) = (unsigned short)*(ptr_b++); + } + if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); + cimg::fwrite(buf._data,3*N,nfile); + to_write-=N; + } + } + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a PNK file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_pnk(const char *const filename) const { + return _save_pnk(0,filename); + } + + //! Save image as a PNK file \overloading. + const CImg& save_pnk(std::FILE *const file) const { + return _save_pnk(file,0); + } + + const CImg& _save_pnk(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_pnk(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_spectrum>1) + cimg::warn(_cimg_instance + "save_pnk(): Instance is multispectral, only the first channel will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + const unsigned long buf_size = cimg::min(1024*1024LU,_width*_height*_depth); + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const T *ptr = data(0,0,0,0); + + if (!cimg::type::is_float() && sizeof(T)==1 && _depth<2) // Can be saved as regular PNM file. + _save_pnm(file,filename,0); + else if (!cimg::type::is_float() && sizeof(T)==1) { // Save as extended P5 file: Binary byte-valued 3d. + std::fprintf(nfile,"P5\n%u %u %u\n255\n",_width,_height,_depth); + CImg buf(buf_size); + for (long to_write = (long)_width*_height*_depth; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size); + unsigned char *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr++); + cimg::fwrite(buf._data,N,nfile); + to_write-=N; + } + } else if (!cimg::type::is_float()) { // Save as P8: Binary int32-valued 3d. + if (_depth>1) std::fprintf(nfile,"P8\n%u %u %u\n%d\n",_width,_height,_depth,(int)max()); + else std::fprintf(nfile,"P8\n%u %u\n%d\n",_width,_height,(int)max()); + CImg buf(buf_size); + for (long to_write = (long)_width*_height*_depth; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size); + int *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) *(ptrd++) = (int)*(ptr++); + cimg::fwrite(buf._data,N,nfile); + to_write-=N; + } + } else { // Save as P9: Binary float-valued 3d. + if (_depth>1) std::fprintf(nfile,"P9\n%u %u %u\n%g\n",_width,_height,_depth,(double)max()); + else std::fprintf(nfile,"P9\n%u %u\n%g\n",_width,_height,(double)max()); + CImg buf(buf_size); + for (long to_write = (long)_width*_height*_depth; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size); + float *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) *(ptrd++) = (float)*(ptr++); + cimg::fwrite(buf._data,N,nfile); + to_write-=N; + } + } + + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a PFM file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_pfm(const char *const filename) const { + return get_mirror('y')._save_pfm(0,filename); + } + + //! Save image as a PFM file \overloading. + const CImg& save_pfm(std::FILE *const file) const { + return get_mirror('y')._save_pfm(file,0); + } + + const CImg& _save_pfm(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_pfm(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_depth>1) + cimg::warn(_cimg_instance + "save_pfm(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + if (_spectrum>3) + cimg::warn(_cimg_instance + "save_pfm(): image instance is multispectral, only the three first channels will be saved " + "in file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const T + *ptr_r = data(0,0,0,0), + *ptr_g = (_spectrum>=2)?data(0,0,0,1):0, + *ptr_b = (_spectrum>=3)?data(0,0,0,2):0; + const unsigned int buf_size = cimg::min(1024*1024U,_width*_height*(_spectrum==1?1:3)); + + std::fprintf(nfile,"P%c\n%u %u\n1.0\n", + (_spectrum==1?'f':'F'),_width,_height); + + switch (_spectrum) { + case 1 : { // Scalar image + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned long N = cimg::min((unsigned long)to_write,buf_size); + float *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) *(ptrd++) = (float)*(ptr_r++); + if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); + cimg::fwrite(buf._data,N,nfile); + to_write-=N; + } + } break; + case 2 : { // RG image + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned int N = cimg::min((unsigned int)to_write,buf_size/3); + float *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) { + *(ptrd++) = (float)*(ptr_r++); + *(ptrd++) = (float)*(ptr_g++); + *(ptrd++) = 0; + } + if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); + cimg::fwrite(buf._data,3*N,nfile); + to_write-=N; + } + } break; + default : { // RGB image + CImg buf(buf_size); + for (long to_write = (long)_width*_height; to_write>0; ) { + const unsigned int N = cimg::min((unsigned int)to_write,buf_size/3); + float *ptrd = buf._data; + for (unsigned long i = N; i>0; --i) { + *(ptrd++) = (float)*(ptr_r++); + *(ptrd++) = (float)*(ptr_g++); + *(ptrd++) = (float)*(ptr_b++); + } + if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); + cimg::fwrite(buf._data,3*N,nfile); + to_write-=N; + } + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a RGB file. + /** + \param filename Filename, as a C-string. + **/ + const CImg& save_rgb(const char *const filename) const { + return _save_rgb(0,filename); + } + + //! Save image as a RGB file \overloading. + const CImg& save_rgb(std::FILE *const file) const { + return _save_rgb(file,0); + } + + const CImg& _save_rgb(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_rgb(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_spectrum!=3) + cimg::warn(_cimg_instance + "save_rgb(): image instance has not exactly 3 channels, for file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const unsigned long wh = (unsigned long)_width*_height; + unsigned char *const buffer = new unsigned char[3*wh], *nbuffer = buffer; + const T + *ptr1 = data(0,0,0,0), + *ptr2 = _spectrum>1?data(0,0,0,1):0, + *ptr3 = _spectrum>2?data(0,0,0,2):0; + switch (_spectrum) { + case 1 : { // Scalar image + for (unsigned long k = 0; k& save_rgba(const char *const filename) const { + return _save_rgba(0,filename); + } + + //! Save image as a RGBA file \overloading. + const CImg& save_rgba(std::FILE *const file) const { + return _save_rgba(file,0); + } + + const CImg& _save_rgba(std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_rgba(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if (_spectrum!=4) + cimg::warn(_cimg_instance + "save_rgba(): image instance has not exactly 4 channels, for file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const unsigned long wh = (unsigned long)_width*_height; + unsigned char *const buffer = new unsigned char[4*wh], *nbuffer = buffer; + const T + *ptr1 = data(0,0,0,0), + *ptr2 = _spectrum>1?data(0,0,0,1):0, + *ptr3 = _spectrum>2?data(0,0,0,2):0, + *ptr4 = _spectrum>3?data(0,0,0,3):0; + switch (_spectrum) { + case 1 : { // Scalar images + for (unsigned long k = 0; k{ 1=None | 2=CCITTRLE | 3=CCITTFAX3 | 4=CCITTFAX4 | 5=LZW | 6=JPEG }. + \note + - libtiff support is enabled by defining the precompilation + directive \c cimg_use_tif. + - When libtiff is enabled, 2D and 3D (multipage) several + channel per pixel are supported for + char,uchar,short,ushort,float and \c double pixel types. + - If \c cimg_use_tif is not defined at compilation time the + function uses CImg&save_other(const char*). + **/ + const CImg& save_tiff(const char *const filename, const unsigned int compression_type=0, + const float *const voxel_size=0) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_tiff(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifdef cimg_use_tiff + TIFF *tif = TIFFOpen(filename,"w"); + if (tif) { + cimg_forZ(*this,z) get_slice(z)._save_tiff(tif,z,compression_type,voxel_size); + TIFFClose(tif); + } else throw CImgIOException(_cimg_instance + "save_tiff(): Failed to open file '%s' for writing.", + cimg_instance, + filename); + return *this; +#else + cimg::unused(compression_type,voxel_size); + return save_other(filename); +#endif + } + +#ifdef cimg_use_tiff + +#define _cimg_save_tiff(types,typed,compression_type,voxel_size) if (!std::strcmp(types,pixel_type())) { \ + const typed foo = (typed)0; return _save_tiff(tif,directory,foo,compression_type,voxel_size); } + + // [internal] Save a plane into a tiff file + template + const CImg& _save_tiff(TIFF *tif, const unsigned int directory, const t& pixel_t, + const unsigned int compression_type, + const float *const voxel_size=0) const { + if (is_empty() || !tif || pixel_t) return *this; + const char *const filename = TIFFFileName(tif); + uint32 rowsperstrip = (uint32)-1; + uint16 spp = _spectrum, bpp = sizeof(t)*8, photometric; + if (spp==3 || spp==4) photometric = PHOTOMETRIC_RGB; + else photometric = PHOTOMETRIC_MINISBLACK; + TIFFSetDirectory(tif,directory); + TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,_width); + TIFFSetField(tif,TIFFTAG_IMAGELENGTH,_height); + if (voxel_size) { + const float vx = voxel_size[0], vy = voxel_size[1], _vz = voxel_size[2], vz = _vz>0?_vz:1; + TIFFSetField(tif,TIFFTAG_RESOLUTIONUNIT,RESUNIT_NONE); + TIFFSetField(tif,TIFFTAG_XRESOLUTION,vx/vz); + TIFFSetField(tif,TIFFTAG_YRESOLUTION,vy/vz); + CImg s_description(256); + cimg_snprintf(s_description,s_description._width,"VX=%g VY=%g VZ=%g spacing=%g",vx,vy,vz,vz); + TIFFSetField(tif,TIFFTAG_IMAGEDESCRIPTION,s_description.data()); + } + TIFFSetField(tif,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT); + TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,spp); + if (cimg::type::is_float()) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,3); + else if (cimg::type::min()==0) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,1); + else TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,2); + TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,bpp); + TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG); + TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,photometric); + TIFFSetField(tif,TIFFTAG_COMPRESSION,compression_type?(compression_type-1):COMPRESSION_NONE); + rowsperstrip = TIFFDefaultStripSize(tif,rowsperstrip); + TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,rowsperstrip); + TIFFSetField(tif,TIFFTAG_FILLORDER,FILLORDER_MSB2LSB); + TIFFSetField(tif,TIFFTAG_SOFTWARE,"CImg"); + t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif)); + if (buf) { + for (unsigned int row = 0; row<_height; row+=rowsperstrip) { + uint32 nrow = (row + rowsperstrip>_height?_height-row:rowsperstrip); + tstrip_t strip = TIFFComputeStrip(tif,row,0); + tsize_t i = 0; + for (unsigned int rr = 0; rr& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int compression_type, + const float *const voxel_size) const { + _cimg_save_tiff("bool",unsigned char,compression_type,voxel_size); + _cimg_save_tiff("char",char,compression_type,voxel_size); + _cimg_save_tiff("unsigned char",unsigned char,compression_type,voxel_size); + _cimg_save_tiff("short",short,compression_type,voxel_size); + _cimg_save_tiff("unsigned short",unsigned short,compression_type,voxel_size); + _cimg_save_tiff("int",int,compression_type,voxel_size); + _cimg_save_tiff("unsigned int",unsigned int,compression_type,voxel_size); + _cimg_save_tiff("long",int,compression_type,voxel_size); + _cimg_save_tiff("unsigned long",unsigned int,compression_type,voxel_size); + _cimg_save_tiff("float",float,compression_type,voxel_size); + _cimg_save_tiff("double",float,compression_type,voxel_size); + const char *const filename = TIFFFileName(tif); + throw CImgInstanceException(_cimg_instance + "save_tiff(): Unsupported pixel type '%s' for file '%s'.", + cimg_instance, + pixel_type(),filename?filename:"(FILE*)"); + return *this; + } +#endif + + //! Save image as a MINC2 file. + /** + \param filename Filename, as a C-string. + \param imitate_file If non-zero, reference filename, as a C-string, to borrow header from. + **/ + const CImg& save_minc2(const char *const filename, + const char *const imitate_file=0) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_minc2(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifndef cimg_use_minc2 + cimg::unused(imitate_file); + return save_other(filename); +#else + minc::minc_1_writer wtr; + if (imitate_file) + wtr.open(filename, imitate_file); + else { + minc::minc_info di; + if(width()) di.push_back(minc::dim_info(width(), width()*0.5, -1, minc::dim_info::DIM_X)); + if(height()) di.push_back(minc::dim_info(height(), height()*0.5, -1, minc::dim_info::DIM_Y)); + if(depth()) di.push_back(minc::dim_info(depth(), depth()*0.5, -1, minc::dim_info::DIM_Z)); + if(spectrum()) di.push_back(minc::dim_info(spectrum(), spectrum()*0.5, -1, minc::dim_info::DIM_TIME)); + wtr.open(filename, di, 1, NC_FLOAT, 0); + } + if(typeid(T)==typeid(unsigned char)) + wtr.setup_write_byte(); + else if(typeid(T)==typeid(int)) + wtr.setup_write_int(); + else if(typeid(T)==typeid(double)) + wtr.setup_write_double(); + else + wtr.setup_write_float(); + minc::save_standard_volume(wtr, this->_data); + return *this; +#endif + } + + //! Save image as an ANALYZE7.5 or NIFTI file. + /** + \param filename Filename, as a C-string. + \param voxel_size Pointer to 3 consecutive values that tell about the voxel sizes along the X,Y and Z dimensions. + **/ + const CImg& save_analyze(const char *const filename, const float *const voxel_size=0) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_analyze(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + std::FILE *file; + char header[348] = { 0 }, hname[1024] = { 0 }, iname[1024] = { 0 }; + const char *const ext = cimg::split_filename(filename); + short datatype=-1; + std::memset(header,0,348); + if (!*ext) { + cimg_snprintf(hname,sizeof(hname),"%s.hdr",filename); + cimg_snprintf(iname,sizeof(iname),"%s.img",filename); + } + if (!cimg::strncasecmp(ext,"hdr",3)) { + std::strcpy(hname,filename); + std::strncpy(iname,filename,sizeof(iname)-1); + std::sprintf(iname + std::strlen(iname)-3,"img"); + } + if (!cimg::strncasecmp(ext,"img",3)) { + std::strcpy(hname,filename); + std::strncpy(iname,filename,sizeof(iname)-1); + std::sprintf(hname + std::strlen(iname)-3,"hdr"); + } + if (!cimg::strncasecmp(ext,"nii",3)) { + std::strncpy(hname,filename,sizeof(hname)-1); *iname = 0; + } + int *const iheader = (int*)header; + *iheader = 348; + std::strcpy(header + 4,"CImg"); + std::strcpy(header + 14," "); + ((short*)(header + 36))[0] = 4096; + ((char*)(header + 38))[0] = 114; + ((short*)(header + 40))[0] = 4; + ((short*)(header + 40))[1] = _width; + ((short*)(header + 40))[2] = _height; + ((short*)(header + 40))[3] = _depth; + ((short*)(header + 40))[4] = _spectrum; + if (!cimg::strcasecmp(pixel_type(),"bool")) datatype = 2; + if (!cimg::strcasecmp(pixel_type(),"unsigned char")) datatype = 2; + if (!cimg::strcasecmp(pixel_type(),"char")) datatype = 2; + if (!cimg::strcasecmp(pixel_type(),"unsigned short")) datatype = 4; + if (!cimg::strcasecmp(pixel_type(),"short")) datatype = 4; + if (!cimg::strcasecmp(pixel_type(),"unsigned int")) datatype = 8; + if (!cimg::strcasecmp(pixel_type(),"int")) datatype = 8; + if (!cimg::strcasecmp(pixel_type(),"unsigned long")) datatype = 8; + if (!cimg::strcasecmp(pixel_type(),"long")) datatype = 8; + if (!cimg::strcasecmp(pixel_type(),"float")) datatype = 16; + if (!cimg::strcasecmp(pixel_type(),"double")) datatype = 64; + if (datatype<0) + throw CImgIOException(_cimg_instance + "save_analyze(): Unsupported pixel type '%s' for file '%s'.", + cimg_instance, + pixel_type(),filename); + + ((short*)(header+70))[0] = datatype; + ((short*)(header+72))[0] = sizeof(T); + ((float*)(header+112))[0] = 1; + ((float*)(header+76))[0] = 0; + if (voxel_size) { + ((float*)(header+76))[1] = voxel_size[0]; + ((float*)(header+76))[2] = voxel_size[1]; + ((float*)(header+76))[3] = voxel_size[2]; + } else ((float*)(header+76))[1] = ((float*)(header+76))[2] = ((float*)(header+76))[3] = 1; + file = cimg::fopen(hname,"wb"); + cimg::fwrite(header,348,file); + if (*iname) { cimg::fclose(file); file = cimg::fopen(iname,"wb"); } + cimg::fwrite(_data,size(),file); + cimg::fclose(file); + return *this; + } + + //! Save image as a .cimg file. + /** + \param filename Filename, as a C-string. + \param is_compressed Tells if the file contains compressed image data. + **/ + const CImg& save_cimg(const char *const filename, const bool is_compressed=false) const { + CImgList(*this,true).save_cimg(filename,is_compressed); + return *this; + } + + //! Save image as a .cimg file \overloading. + const CImg& save_cimg(std::FILE *const file, const bool is_compressed=false) const { + CImgList(*this,true).save_cimg(file,is_compressed); + return *this; + } + + //! Save image as a sub-image into an existing .cimg file. + /** + \param filename Filename, as a C-string. + \param n0 Index of the image inside the file. + \param x0 X-coordinate of the sub-image location. + \param y0 Y-coordinate of the sub-image location. + \param z0 Z-coordinate of the sub-image location. + \param c0 C-coordinate of the sub-image location. + **/ + const CImg& save_cimg(const char *const filename, + const unsigned int n0, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0) const { + CImgList(*this,true).save_cimg(filename,n0,x0,y0,z0,c0); + return *this; + } + + //! Save image as a sub-image into an existing .cimg file \overloading. + const CImg& save_cimg(std::FILE *const file, + const unsigned int n0, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0) const { + CImgList(*this,true).save_cimg(file,n0,x0,y0,z0,c0); + return *this; + } + + //! Save blank image as a .cimg file. + /** + \param filename Filename, as a C-string. + \param dx Width of the image. + \param dy Height of the image. + \param dz Depth of the image. + \param dc Number of channels of the image. + \note + - All pixel values of the saved image are set to \c 0. + - Use this method to save large images without having to instanciate and allocate them. + **/ + static void save_empty_cimg(const char *const filename, + const unsigned int dx, const unsigned int dy=1, + const unsigned int dz=1, const unsigned int dc=1) { + return CImgList::save_empty_cimg(filename,1,dx,dy,dz,dc); + } + + //! Save blank image as a .cimg file \overloading. + /** + Same as save_empty_cimg(const char *,unsigned int,unsigned int,unsigned int,unsigned int) + with a file stream argument instead of a filename string. + **/ + static void save_empty_cimg(std::FILE *const file, + const unsigned int dx, const unsigned int dy=1, + const unsigned int dz=1, const unsigned int dc=1) { + return CImgList::save_empty_cimg(file,1,dx,dy,dz,dc); + } + + //! Save image as an INRIMAGE-4 file. + /** + \param filename Filename, as a C-string. + \param voxel_size Pointer to 3 values specifying the voxel sizes along the X,Y and Z dimensions. + **/ + const CImg& save_inr(const char *const filename, const float *const voxel_size=0) const { + return _save_inr(0,filename,voxel_size); + } + + //! Save image as an INRIMAGE-4 file \overloading. + const CImg& save_inr(std::FILE *const file, const float *const voxel_size=0) const { + return _save_inr(file,0,voxel_size); + } + + const CImg& _save_inr(std::FILE *const file, const char *const filename, const float *const voxel_size) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_inr(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + + int inrpixsize=-1; + const char *inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0"; + if (!cimg::strcasecmp(pixel_type(),"unsigned char")) { + inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1; + } + if (!cimg::strcasecmp(pixel_type(),"char")) { + inrtype = "fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1; + } + if (!cimg::strcasecmp(pixel_type(),"unsigned short")) { + inrtype = "unsigned fixed\nPIXSIZE=16 bits\nSCALE=2**0";inrpixsize = 2; + } + if (!cimg::strcasecmp(pixel_type(),"short")) { + inrtype = "fixed\nPIXSIZE=16 bits\nSCALE=2**0"; inrpixsize = 2; + } + if (!cimg::strcasecmp(pixel_type(),"unsigned int")) { + inrtype = "unsigned fixed\nPIXSIZE=32 bits\nSCALE=2**0";inrpixsize = 4; + } + if (!cimg::strcasecmp(pixel_type(),"int")) { + inrtype = "fixed\nPIXSIZE=32 bits\nSCALE=2**0"; inrpixsize = 4; + } + if (!cimg::strcasecmp(pixel_type(),"float")) { + inrtype = "float\nPIXSIZE=32 bits"; inrpixsize = 4; + } + if (!cimg::strcasecmp(pixel_type(),"double")) { + inrtype = "float\nPIXSIZE=64 bits"; inrpixsize = 8; + } + if (inrpixsize<=0) + throw CImgIOException(_cimg_instance + "save_inr(): Unsupported pixel type '%s' for file '%s'", + cimg_instance, + pixel_type(),filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + char header[257] = { 0 }; + int err = cimg_snprintf(header,sizeof(header),"#INRIMAGE-4#{\nXDIM=%u\nYDIM=%u\nZDIM=%u\nVDIM=%u\n", + _width,_height,_depth,_spectrum); + if (voxel_size) err+=std::sprintf(header + err,"VX=%g\nVY=%g\nVZ=%g\n",voxel_size[0],voxel_size[1],voxel_size[2]); + err+=std::sprintf(header + err,"TYPE=%s\nCPU=%s\n",inrtype,cimg::endianness()?"sun":"decm"); + std::memset(header + err,'\n',252 - err); + std::memcpy(header + 252,"##}\n",4); + cimg::fwrite(header,256,nfile); + cimg_forXYZ(*this,x,y,z) cimg_forC(*this,c) cimg::fwrite(&((*this)(x,y,z,c)),1,nfile); + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as an OpenEXR file. + /** + \param filename Filename, as a C-string. + \note The OpenEXR file format is described here. + **/ + const CImg& save_exr(const char *const filename) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_exr(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + if (_depth>1) + cimg::warn(_cimg_instance + "save_exr(): Instance is volumetric, only the first slice will be saved in file '%s'.", + cimg_instance, + filename); + +#ifndef cimg_use_openexr + return save_other(filename); +#else + Imf::Rgba *const ptrd0 = new Imf::Rgba[(unsigned long)_width*_height], *ptrd = ptrd0, rgba; + switch (_spectrum) { + case 1 : { // Grayscale image. + for (const T *ptr_r = data(), *const ptr_e = ptr_r + (unsigned long)_width*_height; ptr_rPandore file specifications + for more informations). + **/ + const CImg& save_pandore(const char *const filename, const unsigned int colorspace=0) const { + return _save_pandore(0,filename,colorspace); + } + + //! Save image as a Pandore-5 file \overloading. + /** + Same as save_pandore(const char *,unsigned int) const + with a file stream argument instead of a filename string. + **/ + const CImg& save_pandore(std::FILE *const file, const unsigned int colorspace=0) const { + return _save_pandore(file,0,colorspace); + } + + unsigned int _save_pandore_header_length(unsigned int id, unsigned int *dims, const unsigned int colorspace) const { + unsigned int nbdims = 0; + if (id==2 || id==3 || id==4) { + dims[0] = 1; dims[1] = _width; nbdims = 2; + } + if (id==5 || id==6 || id==7) { + dims[0] = 1; dims[1] = _height; dims[2] = _width; nbdims=3; + } + if (id==8 || id==9 || id==10) { + dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4; + } + if (id==16 || id==17 || id==18) { + dims[0] = 3; dims[1] = _height; dims[2] = _width; dims[3] = colorspace; nbdims = 4; + } + if (id==19 || id==20 || id==21) { + dims[0] = 3; dims[1] = _depth; dims[2] = _height; dims[3] = _width; dims[4] = colorspace; nbdims = 5; + } + if (id==22 || id==23 || id==25) { + dims[0] = _spectrum; dims[1] = _width; nbdims = 2; + } + if (id==26 || id==27 || id==29) { + dims[0] = _spectrum; dims[1] = _height; dims[2] = _width; nbdims=3; + } + if (id==30 || id==31 || id==33) { + dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4; + } + return nbdims; + } + + const CImg& _save_pandore(std::FILE *const file, const char *const filename, + const unsigned int colorspace) const { + +#define __cimg_save_pandore_case(dtype) \ + dtype *buffer = new dtype[size()]; \ + const T *ptrs = _data; \ + cimg_foroff(*this,off) *(buffer++) = (dtype)(*(ptrs++)); \ + buffer-=size(); \ + cimg::fwrite(buffer,size(),nfile); \ + delete[] buffer + +#define _cimg_save_pandore_case(sy,sz,sv,stype,id) \ + if (!saved && (sy?(sy==_height):true) && (sz?(sz==_depth):true) && \ + (sv?(sv==_spectrum):true) && !std::strcmp(stype,pixel_type())) { \ + unsigned int *iheader = (unsigned int*)(header+12); \ + nbdims = _save_pandore_header_length((*iheader=id),dims,colorspace); \ + cimg::fwrite(header,36,nfile); \ + if (sizeof(unsigned long)==4) { unsigned long ndims[5] = { 0 }; \ + for (int d = 0; d<5; ++d) ndims[d] = (unsigned long)dims[d]; cimg::fwrite(ndims,nbdims,nfile); } \ + else if (sizeof(unsigned int)==4) { unsigned int ndims[5] = { 0 }; \ + for (int d = 0; d<5; ++d) ndims[d] = (unsigned int)dims[d]; cimg::fwrite(ndims,nbdims,nfile); } \ + else if (sizeof(unsigned short)==4) { unsigned short ndims[5] = { 0 }; \ + for (int d = 0; d<5; ++d) ndims[d] = (unsigned short)dims[d]; cimg::fwrite(ndims,nbdims,nfile); } \ + else throw CImgIOException(_cimg_instance \ + "save_pandore(): Unsupported datatype for file '%s'.",\ + cimg_instance, \ + filename?filename:"(FILE*)"); \ + if (id==2 || id==5 || id==8 || id==16 || id==19 || id==22 || id==26 || id==30) { \ + __cimg_save_pandore_case(unsigned char); \ + } else if (id==3 || id==6 || id==9 || id==17 || id==20 || id==23 || id==27 || id==31) { \ + if (sizeof(unsigned long)==4) { __cimg_save_pandore_case(unsigned long); } \ + else if (sizeof(unsigned int)==4) { __cimg_save_pandore_case(unsigned int); } \ + else if (sizeof(unsigned short)==4) { __cimg_save_pandore_case(unsigned short); } \ + else throw CImgIOException(_cimg_instance \ + "save_pandore(): Unsupported datatype for file '%s'.",\ + cimg_instance, \ + filename?filename:"(FILE*)"); \ + } else if (id==4 || id==7 || id==10 || id==18 || id==21 || id==25 || id==29 || id==33) { \ + if (sizeof(double)==4) { __cimg_save_pandore_case(double); } \ + else if (sizeof(float)==4) { __cimg_save_pandore_case(float); } \ + else throw CImgIOException(_cimg_instance \ + "save_pandore(): Unsupported datatype for file '%s'.",\ + cimg_instance, \ + filename?filename:"(FILE*)"); \ + } \ + saved = true; \ + } + + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_pandore(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + unsigned char header[36] = { 'P','A','N','D','O','R','E','0','4',0,0,0, + 0,0,0,0,'C','I','m','g',0,0,0,0,0,'N','o',' ','d','a','t','e',0,0,0,0 }; + unsigned int nbdims, dims[5] = { 0 }; + bool saved = false; + _cimg_save_pandore_case(1,1,1,"unsigned char",2); + _cimg_save_pandore_case(1,1,1,"char",3); + _cimg_save_pandore_case(1,1,1,"short",3); + _cimg_save_pandore_case(1,1,1,"unsigned short",3); + _cimg_save_pandore_case(1,1,1,"unsigned int",3); + _cimg_save_pandore_case(1,1,1,"int",3); + _cimg_save_pandore_case(1,1,1,"unsigned long",4); + _cimg_save_pandore_case(1,1,1,"long",3); + _cimg_save_pandore_case(1,1,1,"float",4); + _cimg_save_pandore_case(1,1,1,"double",4); + + _cimg_save_pandore_case(0,1,1,"unsigned char",5); + _cimg_save_pandore_case(0,1,1,"char",6); + _cimg_save_pandore_case(0,1,1,"short",6); + _cimg_save_pandore_case(0,1,1,"unsigned short",6); + _cimg_save_pandore_case(0,1,1,"unsigned int",6); + _cimg_save_pandore_case(0,1,1,"int",6); + _cimg_save_pandore_case(0,1,1,"unsigned long",7); + _cimg_save_pandore_case(0,1,1,"long",6); + _cimg_save_pandore_case(0,1,1,"float",7); + _cimg_save_pandore_case(0,1,1,"double",7); + + _cimg_save_pandore_case(0,0,1,"unsigned char",8); + _cimg_save_pandore_case(0,0,1,"char",9); + _cimg_save_pandore_case(0,0,1,"short",9); + _cimg_save_pandore_case(0,0,1,"unsigned short",9); + _cimg_save_pandore_case(0,0,1,"unsigned int",9); + _cimg_save_pandore_case(0,0,1,"int",9); + _cimg_save_pandore_case(0,0,1,"unsigned long",10); + _cimg_save_pandore_case(0,0,1,"long",9); + _cimg_save_pandore_case(0,0,1,"float",10); + _cimg_save_pandore_case(0,0,1,"double",10); + + _cimg_save_pandore_case(0,1,3,"unsigned char",16); + _cimg_save_pandore_case(0,1,3,"char",17); + _cimg_save_pandore_case(0,1,3,"short",17); + _cimg_save_pandore_case(0,1,3,"unsigned short",17); + _cimg_save_pandore_case(0,1,3,"unsigned int",17); + _cimg_save_pandore_case(0,1,3,"int",17); + _cimg_save_pandore_case(0,1,3,"unsigned long",18); + _cimg_save_pandore_case(0,1,3,"long",17); + _cimg_save_pandore_case(0,1,3,"float",18); + _cimg_save_pandore_case(0,1,3,"double",18); + + _cimg_save_pandore_case(0,0,3,"unsigned char",19); + _cimg_save_pandore_case(0,0,3,"char",20); + _cimg_save_pandore_case(0,0,3,"short",20); + _cimg_save_pandore_case(0,0,3,"unsigned short",20); + _cimg_save_pandore_case(0,0,3,"unsigned int",20); + _cimg_save_pandore_case(0,0,3,"int",20); + _cimg_save_pandore_case(0,0,3,"unsigned long",21); + _cimg_save_pandore_case(0,0,3,"long",20); + _cimg_save_pandore_case(0,0,3,"float",21); + _cimg_save_pandore_case(0,0,3,"double",21); + + _cimg_save_pandore_case(1,1,0,"unsigned char",22); + _cimg_save_pandore_case(1,1,0,"char",23); + _cimg_save_pandore_case(1,1,0,"short",23); + _cimg_save_pandore_case(1,1,0,"unsigned short",23); + _cimg_save_pandore_case(1,1,0,"unsigned int",23); + _cimg_save_pandore_case(1,1,0,"int",23); + _cimg_save_pandore_case(1,1,0,"unsigned long",25); + _cimg_save_pandore_case(1,1,0,"long",23); + _cimg_save_pandore_case(1,1,0,"float",25); + _cimg_save_pandore_case(1,1,0,"double",25); + + _cimg_save_pandore_case(0,1,0,"unsigned char",26); + _cimg_save_pandore_case(0,1,0,"char",27); + _cimg_save_pandore_case(0,1,0,"short",27); + _cimg_save_pandore_case(0,1,0,"unsigned short",27); + _cimg_save_pandore_case(0,1,0,"unsigned int",27); + _cimg_save_pandore_case(0,1,0,"int",27); + _cimg_save_pandore_case(0,1,0,"unsigned long",29); + _cimg_save_pandore_case(0,1,0,"long",27); + _cimg_save_pandore_case(0,1,0,"float",29); + _cimg_save_pandore_case(0,1,0,"double",29); + + _cimg_save_pandore_case(0,0,0,"unsigned char",30); + _cimg_save_pandore_case(0,0,0,"char",31); + _cimg_save_pandore_case(0,0,0,"short",31); + _cimg_save_pandore_case(0,0,0,"unsigned short",31); + _cimg_save_pandore_case(0,0,0,"unsigned int",31); + _cimg_save_pandore_case(0,0,0,"int",31); + _cimg_save_pandore_case(0,0,0,"unsigned long",33); + _cimg_save_pandore_case(0,0,0,"long",31); + _cimg_save_pandore_case(0,0,0,"float",33); + _cimg_save_pandore_case(0,0,0,"double",33); + + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a raw data file. + /** + \param filename Filename, as a C-string. + \param is_multiplexed Tells if the image channels are stored in a multiplexed way (\c true) or not (\c false). + \note The .raw format does not store the image dimensions in the output file, + so you have to keep track of them somewhere to be able to read the file correctly afterwards. + **/ + const CImg& save_raw(const char *const filename, const bool is_multiplexed=false) const { + return _save_raw(0,filename,is_multiplexed); + } + + //! Save image as a raw data file \overloading. + /** + Same as save_raw(const char *,bool) const + with a file stream argument instead of a filename string. + **/ + const CImg& save_raw(std::FILE *const file, const bool is_multiplexed=false) const { + return _save_raw(file,0,is_multiplexed); + } + + const CImg& _save_raw(std::FILE *const file, const char *const filename, const bool is_multiplexed) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_raw(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + if (!is_multiplexed) cimg::fwrite(_data,size(),nfile); + else { + CImg buf(_spectrum); + cimg_forXYZ(*this,x,y,z) { + cimg_forC(*this,c) buf[c] = (*this)(x,y,z,c); + cimg::fwrite(buf._data,_spectrum,nfile); + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save image as a video file, using the FFmpeg library. + /** + \param filename Filename, as a C-string. + \param fps Video framerate. + \param bitrate Video bitrate. + \note + - Each slice of the instance image is considered to be a single frame of the output video file. + - This method uses functions provided by the FFmpeg library. + Configuration macro \c cimg_use_ffmpeg must be set for the method to succeed natively. + Otherwise, the method calls + save_ffmpeg_external(const char*,unsigned int,unsigned int,const char*,unsigned int,unsigned int) const. + **/ + const CImg& save_ffmpeg(const char *const filename, const unsigned int fps=25, + const unsigned int bitrate=2048) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_ffmpeg(): Specified filename is (null).", + cimg_instance); + if (!fps) + throw CImgArgumentException(_cimg_instance + "save_ffmpeg(): Invalid specified framerate 0, for file '%s'.", + cimg_instance, + filename); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifndef cimg_use_ffmpeg + return save_ffmpeg_external(filename,0,fps,bitrate); +#else + CImgList list; + get_split('z').move_to(list); + list.save_ffmpeg(filename,fps,bitrate); + return *this; +#endif + } + + //! Save image as a .yuv video file. + /** + \param filename Filename, as a C-string. + \param is_rgb Tells if pixel values of the instance image are RGB-coded (\c true) or YUV-coded (\c false). + \note Each slice of the instance image is considered to be a single frame of the output video file. + **/ + const CImg& save_yuv(const char *const filename, const bool is_rgb=true) const { + get_split('z').save_yuv(filename,is_rgb); + return *this; + } + + //! Save image as a .yuv video file \overloading. + /** + Same as save_yuv(const char*,bool) const + with a file stream argument instead of a filename string. + **/ + const CImg& save_yuv(std::FILE *const file, const bool is_rgb=true) const { + get_split('z').save_yuv(file,is_rgb); + return *this; + } + + //! Save 3d object as an Object File Format (.off) file. + /** + \param filename Filename, as a C-string. + \param primitives List of 3d object primitives. + \param colors List of 3d object colors. + \note + - Instance image contains the vertices data of the 3d object. + - Textured, transparent or sphere-shaped primitives cannot be managed by the .off file format. + Such primitives will be lost or simplified during file saving. + - The .off file format is described here. + **/ + template + const CImg& save_off(const CImgList& primitives, const CImgList& colors, + const char *const filename) const { + return _save_off(primitives,colors,0,filename); + } + + //! Save 3d object as an Object File Format (.off) file \overloading. + /** + Same as save_off(const CImgList&,const CImgList&,const char*) const + with a file stream argument instead of a filename string. + **/ + template + const CImg& save_off(const CImgList& primitives, const CImgList& colors, + std::FILE *const file) const { + return _save_off(primitives,colors,file,0); + } + + template + const CImg& _save_off(const CImgList& primitives, const CImgList& colors, + std::FILE *const file, const char *const filename) const { + if (!file && !filename) + throw CImgArgumentException(_cimg_instance + "save_off(): Specified filename is (null).", + cimg_instance); + if (is_empty()) + throw CImgInstanceException(_cimg_instance + "save_off(): Empty instance, for file '%s'.", + cimg_instance, + filename?filename:"(FILE*)"); + + CImgList opacities; + char error_message[1024] = { 0 }; + if (!is_object3d(primitives,colors,opacities,true,error_message)) + throw CImgInstanceException(_cimg_instance + "save_off(): Invalid specified 3d object, for file '%s' (%s).", + cimg_instance, + filename?filename:"(FILE*)",error_message); + + const CImg default_color(1,3,1,1,200); + std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); + unsigned int supported_primitives = 0; + cimglist_for(primitives,l) if (primitives[l].size()!=5) ++supported_primitives; + std::fprintf(nfile,"OFF\n%u %u %u\n",_width,supported_primitives,3*primitives._width); + cimg_forX(*this,i) std::fprintf(nfile,"%f %f %f\n", + (float)((*this)(i,0)),(float)((*this)(i,1)),(float)((*this)(i,2))); + cimglist_for(primitives,l) { + const CImg& color = l1?color[1]:r)/255.0f, b = (csiz>2?color[2]:g)/255.0f; + switch (psiz) { + case 1 : std::fprintf(nfile,"1 %u %f %f %f\n", + (unsigned int)primitives(l,0),r,g,b); break; + case 2 : std::fprintf(nfile,"2 %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break; + case 3 : std::fprintf(nfile,"3 %u %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,2), + (unsigned int)primitives(l,1),r,g,b); break; + case 4 : std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,3), + (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),r,g,b); break; + case 5 : std::fprintf(nfile,"2 %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break; + case 6 : { + const unsigned int xt = (unsigned int)primitives(l,2), yt = (unsigned int)primitives(l,3); + const float + rt = color.atXY(xt,yt,0)/255.0f, + gt = (csiz>1?color.atXY(xt,yt,1):r)/255.0f, + bt = (csiz>2?color.atXY(xt,yt,2):g)/255.0f; + std::fprintf(nfile,"2 %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),rt,gt,bt); + } break; + case 9 : { + const unsigned int xt = (unsigned int)primitives(l,3), yt = (unsigned int)primitives(l,4); + const float + rt = color.atXY(xt,yt,0)/255.0f, + gt = (csiz>1?color.atXY(xt,yt,1):r)/255.0f, + bt = (csiz>2?color.atXY(xt,yt,2):g)/255.0f; + std::fprintf(nfile,"3 %u %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,2), + (unsigned int)primitives(l,1),rt,gt,bt); + } break; + case 12 : { + const unsigned int xt = (unsigned int)primitives(l,4), yt = (unsigned int)primitives(l,5); + const float + rt = color.atXY(xt,yt,0)/255.0f, + gt = (csiz>1?color.atXY(xt,yt,1):r)/255.0f, + bt = (csiz>2?color.atXY(xt,yt,2):g)/255.0f; + std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n", + (unsigned int)primitives(l,0),(unsigned int)primitives(l,3), + (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),rt,gt,bt); + } break; + } + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save volumetric image as a video, using ffmpeg external binary. + /** + \param filename Filename, as a C-string. + \param codec Video codec, as a C-string. + \param fps Video framerate. + \param bitrate Video bitrate. + \note + - Each slice of the instance image is considered to be a single frame of the output video file. + - This method uses \c ffmpeg, an external executable binary provided by + FFmpeg. + It must be installed for the method to succeed. + **/ + const CImg& save_ffmpeg_external(const char *const filename, const char *const codec=0, + const unsigned int fps=25, const unsigned int bitrate=2048) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_ffmpeg_external(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + CImgList list; + get_split('z').move_to(list); + list.save_ffmpeg_external(filename,codec,fps,bitrate); + return *this; + } + + //! Save image using gzip external binary. + /** + \param filename Filename, as a C-string. + \note This method uses \c gzip, an external executable binary provided by + gzip. + It must be installed for the method to succeed. + **/ + const CImg& save_gzip_external(const char *const filename) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_gzip_external(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; + const char + *ext = cimg::split_filename(filename,body), + *ext2 = cimg::split_filename(body,0); + std::FILE *file; + do { + if (!cimg::strcasecmp(ext,"gz")) { + if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } else { + if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + save(filetmp); + cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", + cimg::gzip_path(), + CImg::string(filetmp)._system_strescape().data(), + CImg::string(filename)._system_strescape().data()); + cimg::system(command); + file = std::fopen(filename,"rb"); + if (!file) + throw CImgIOException(_cimg_instance + "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.", + cimg_instance, + filename); + + else cimg::fclose(file); + std::remove(filetmp); + return *this; + } + + //! Save image using GraphicsMagick's external binary. + /** + \param filename Filename, as a C-string. + \param quality Image quality (expressed in percent), when the file format supports it. + \note This method uses \c gm, an external executable binary provided by + GraphicsMagick. + It must be installed for the method to succeed. + **/ + const CImg& save_graphicsmagick_external(const char *const filename, const unsigned int quality=100) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_graphicsmagick_external(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifdef cimg_use_png +#define _cimg_sge_ext1 "png" +#define _cimg_sge_ext2 "png" +#else +#define _cimg_sge_ext1 "pgm" +#define _cimg_sge_ext2 "ppm" +#endif + char command[1024] = { 0 }, filetmp[512] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(), + _spectrum==1?_cimg_sge_ext1:_cimg_sge_ext2); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); +#ifdef cimg_use_png + save_png(filetmp); +#else + save_pnm(filetmp); +#endif + cimg_snprintf(command,sizeof(command),"%s convert -quality %u \"%s\" \"%s\"", + cimg::graphicsmagick_path(),quality, + CImg::string(filetmp)._system_strescape().data(), + CImg::string(filename)._system_strescape().data()); + cimg::system(command); + file = std::fopen(filename,"rb"); + if (!file) + throw CImgIOException(_cimg_instance + "save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.", + cimg_instance, + filename); + + if (file) cimg::fclose(file); + std::remove(filetmp); + return *this; + } + + //! Save image using ImageMagick's external binary. + /** + \param filename Filename, as a C-string. + \param quality Image quality (expressed in percent), when the file format supports it. + \note This method uses \c convert, an external executable binary provided by + ImageMagick. + It must be installed for the method to succeed. + **/ + const CImg& save_imagemagick_external(const char *const filename, const unsigned int quality=100) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_imagemagick_external(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifdef cimg_use_png +#define _cimg_sie_ext1 "png" +#define _cimg_sie_ext2 "png" +#else +#define _cimg_sie_ext1 "pgm" +#define _cimg_sie_ext2 "ppm" +#endif + char command[1024] = { 0 }, filetmp[512] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s",cimg::temporary_path(), + cimg_file_separator,cimg::filenamerand(),_spectrum==1?_cimg_sie_ext1:_cimg_sie_ext2); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); +#ifdef cimg_use_png + save_png(filetmp); +#else + save_pnm(filetmp); +#endif + cimg_snprintf(command,sizeof(command),"%s -quality %u \"%s\" \"%s\"", + cimg::imagemagick_path(),quality, + CImg::string(filetmp)._system_strescape().data(), + CImg::string(filename)._system_strescape().data()); + cimg::system(command); + file = std::fopen(filename,"rb"); + if (!file) + throw CImgIOException(_cimg_instance + "save_imagemagick_external(): Failed to save file '%s' with external command 'convert'.", + cimg_instance, + filename); + + if (file) cimg::fclose(file); + std::remove(filetmp); + return *this; + } + + //! Save image as a Dicom file. + /** + \param filename Filename, as a C-string. + \note This method uses \c medcon, an external executable binary provided by + (X)Medcon. + It must be installed for the method to succeed. + **/ + const CImg& save_medcon_external(const char *const filename) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_medcon_external(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s.hdr",cimg::filenamerand()); + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + save_analyze(filetmp); + cimg_snprintf(command,sizeof(command),"%s -w -c dicom -o \"%s\" -f \"%s\"", + cimg::medcon_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); + cimg::system(command); + std::remove(filetmp); + cimg::split_filename(filetmp,body); + cimg_snprintf(filetmp,sizeof(filetmp),"%s.img",body); + std::remove(filetmp); + + file = std::fopen(filename,"rb"); + if (!file) { + cimg_snprintf(command,sizeof(command),"m000-%s",filename); + file = std::fopen(command,"rb"); + if (!file) { + cimg::fclose(cimg::fopen(filename,"r")); + throw CImgIOException(_cimg_instance + "save_medcon_external(): Failed to save file '%s' with external command 'medcon'.", + cimg_instance, + filename); + } + } + cimg::fclose(file); + std::rename(command,filename); + return *this; + } + + // Save image for non natively supported formats. + /** + \param filename Filename, as a C-string. + \param quality Image quality (expressed in percent), when the file format supports it. + \note + - The filename extension tells about the desired file format. + - This method tries to save the instance image as a file, using external tools from + ImageMagick or + GraphicsMagick. + At least one of these tool must be installed for the method to succeed. + - It is recommended to use the generic method save(const char*, int) const instead, + as it can handle some file formats natively. + **/ + const CImg& save_other(const char *const filename, const unsigned int quality=100) const { + if (!filename) + throw CImgArgumentException(_cimg_instance + "save_other(): Specified filename is (null).", + cimg_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + const unsigned int omode = cimg::exception_mode(); + bool is_saved = true; + cimg::exception_mode() = 0; + try { save_magick(filename); } + catch (CImgException&) { + try { save_imagemagick_external(filename,quality); } + catch (CImgException&) { + try { save_graphicsmagick_external(filename,quality); } + catch (CImgException&) { + is_saved = false; + } + } + } + cimg::exception_mode() = omode; + if (!is_saved) + throw CImgIOException(_cimg_instance + "save_other(): Failed to save file '%s'. Format is not natively supported, " + "and no external commands succeeded.", + cimg_instance, + filename); + return *this; + } + + // [internal] Return a 40x38 color logo of a 'danger' item. + static CImg _logo40x38() { + CImg res(40,38,1,3); + const unsigned char *ptrs = cimg::logo40x38; + T *ptr1 = res.data(0,0,0,0), *ptr2 = res.data(0,0,0,1), *ptr3 = res.data(0,0,0,2); + for (unsigned long off = 0; off<(unsigned long)res._width*res._height;) { + const unsigned char n = *(ptrs++), r = *(ptrs++), g = *(ptrs++), b = *(ptrs++); + for (unsigned int l = 0; l structure + # + # + # + #------------------------------------------ + */ + //! Represent a list of images CImg. + template + struct CImgList { + unsigned int _width, _allocated_width; + CImg *_data; + + //! Simple iterator type, to loop through each image of a list. + /** + \note + - The \c CImgList::iterator type is defined as a CImg*. + - You may use it like this: + \code + CImgList<> list; // Assuming this image list is not empty. + for (CImgList<>::iterator it = list.begin(); it* iterator; + + //! Simple const iterator type, to loop through each image of a \c const list instance. + /** + \note + - The \c CImgList::const_iterator type is defined to be a const CImg*. + - Similar to CImgList::iterator, but for constant list instances. + **/ + typedef const CImg* const_iterator; + + //! Pixel value type. + /** + Refer to the pixels value type of the images in the list. + \note + - The \c CImgList::value_type type of a \c CImgList is defined to be a \c T. + It is then similar to CImg::value_type. + - \c CImgList::value_type is actually not used in %CImg methods. It has been mainly defined for + compatibility with STL naming conventions. + **/ + typedef T value_type; + + // Define common T-dependant types. + typedef typename cimg::superset::type Tbool; + typedef typename cimg::superset::type Tuchar; + typedef typename cimg::superset::type Tchar; + typedef typename cimg::superset::type Tushort; + typedef typename cimg::superset::type Tshort; + typedef typename cimg::superset::type Tuint; + typedef typename cimg::superset::type Tint; + typedef typename cimg::superset::type Tulong; + typedef typename cimg::superset::type Tlong; + typedef typename cimg::superset::type Tfloat; + typedef typename cimg::superset::type Tdouble; + typedef typename cimg::last::type boolT; + typedef typename cimg::last::type ucharT; + typedef typename cimg::last::type charT; + typedef typename cimg::last::type ushortT; + typedef typename cimg::last::type shortT; + typedef typename cimg::last::type uintT; + typedef typename cimg::last::type intT; + typedef typename cimg::last::type ulongT; + typedef typename cimg::last::type longT; + typedef typename cimg::last::type floatT; + typedef typename cimg::last::type doubleT; + + //@} + //--------------------------- + // + //! \name Plugins + //@{ + //--------------------------- +#ifdef cimglist_plugin +#include cimglist_plugin +#endif +#ifdef cimglist_plugin1 +#include cimglist_plugin1 +#endif +#ifdef cimglist_plugin2 +#include cimglist_plugin2 +#endif +#ifdef cimglist_plugin3 +#include cimglist_plugin3 +#endif +#ifdef cimglist_plugin4 +#include cimglist_plugin4 +#endif +#ifdef cimglist_plugin5 +#include cimglist_plugin5 +#endif +#ifdef cimglist_plugin6 +#include cimglist_plugin6 +#endif +#ifdef cimglist_plugin7 +#include cimglist_plugin7 +#endif +#ifdef cimglist_plugin8 +#include cimglist_plugin8 +#endif + + //@} + //-------------------------------------------------------- + // + //! \name Constructors / Destructor / Instance Management + //@{ + //-------------------------------------------------------- + + //! Destructor. + /** + Destroy current list instance. + \note + - Any allocated buffer is deallocated. + - Destroying an empty list does nothing actually. + **/ + ~CImgList() { + delete[] _data; + } + + //! Default constructor. + /** + Construct a new empty list instance. + \note + - An empty list has no pixel data and its dimension width() is set to \c 0, as well as its + image buffer pointer data(). + - An empty list may be reassigned afterwards, with the family of the assign() methods. + In all cases, the type of pixels stays \c T. + **/ + CImgList(): + _width(0),_allocated_width(0),_data(0) {} + + //! Construct list containing empty images. + /** + \param n Number of empty images. + \note Useful when you know by advance the number of images you want to manage, as + it will allocate the right amount of memory for the list, without needs for reallocation + (that may occur when starting from an empty list and inserting several images in it). + **/ + explicit CImgList(const unsigned int n):_width(n) { + if (n) _data = new CImg[_allocated_width = cimg::max(16UL,cimg::nearest_pow2(n))]; + else { _allocated_width = 0; _data = 0; } + } + + //! Construct list containing images of specified size. + /** + \param n Number of images. + \param width Width of images. + \param height Height of images. + \param depth Depth of images. + \param spectrum Number of channels of images. + \note Pixel values are not initialized and may probably contain garbage. + **/ + CImgList(const unsigned int n, const unsigned int width, const unsigned int height=1, + const unsigned int depth=1, const unsigned int spectrum=1): + _width(0),_allocated_width(0),_data(0) { + assign(n); + cimglist_apply(*this,assign)(width,height,depth,spectrum); + } + + //! Construct list containing images of specified size, and initialize pixel values. + /** + \param n Number of images. + \param width Width of images. + \param height Height of images. + \param depth Depth of images. + \param spectrum Number of channels of images. + \param val Initialization value for images pixels. + **/ + CImgList(const unsigned int n, const unsigned int width, const unsigned int height, + const unsigned int depth, const unsigned int spectrum, const T val): + _width(0),_allocated_width(0),_data(0) { + assign(n); + cimglist_apply(*this,assign)(width,height,depth,spectrum,val); + } + + //! Construct list containing images of specified size, and initialize pixel values from a sequence of integers. + /** + \param n Number of images. + \param width Width of images. + \param height Height of images. + \param depth Depth of images. + \param spectrum Number of channels of images. + \param val0 First value of the initializing integers sequence. + \param val1 Second value of the initializing integers sequence. + \warning You must specify at least width*height*depth*spectrum values in your argument list, + or you will probably segfault. + **/ + CImgList(const unsigned int n, const unsigned int width, const unsigned int height, + const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...): + _width(0),_allocated_width(0),_data(0) { +#define _CImgList_stdarg(t) { \ + assign(n,width,height,depth,spectrum); \ + const unsigned long siz = (unsigned long)width*height*depth*spectrum, nsiz = siz*n; \ + T *ptrd = _data->_data; \ + va_list ap; \ + va_start(ap,val1); \ + for (unsigned long l = 0, s = 0, i = 0; iwidth*height*depth*spectrum values in your argument list, + or you will probably segfault. + **/ + CImgList(const unsigned int n, const unsigned int width, const unsigned int height, + const unsigned int depth, const unsigned int spectrum, const double val0, const double val1, ...): + _width(0),_allocated_width(0),_data(0) { + _CImgList_stdarg(double); + } + + //! Construct list containing copies of an input image. + /** + \param n Number of images. + \param img Input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of \c img. + **/ + template + CImgList(const unsigned int n, const CImg& img, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(n); + cimglist_apply(*this,assign)(img,is_shared); + } + + //! Construct list from one image. + /** + \param img Input image to copy in the constructed list. + \param is_shared Tells if the element of the list is a shared or non-shared copy of \c img. + **/ + template + explicit CImgList(const CImg& img, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(1); + _data[0].assign(img,is_shared); + } + + //! Construct list from two images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(2); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); + } + + //! Construct list from three images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param img3 Third input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(3); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + } + + //! Construct list from four images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param img3 Third input image to copy in the constructed list. + \param img4 Fourth input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(4); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); + } + + //! Construct list from five images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param img3 Third input image to copy in the constructed list. + \param img4 Fourth input image to copy in the constructed list. + \param img5 Fifth input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(5); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); + } + + //! Construct list from six images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param img3 Third input image to copy in the constructed list. + \param img4 Fourth input image to copy in the constructed list. + \param img5 Fifth input image to copy in the constructed list. + \param img6 Sixth input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const CImg& img6, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(6); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); + } + + //! Construct list from seven images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param img3 Third input image to copy in the constructed list. + \param img4 Fourth input image to copy in the constructed list. + \param img5 Fifth input image to copy in the constructed list. + \param img6 Sixth input image to copy in the constructed list. + \param img7 Seventh input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const CImg& img6, const CImg& img7, const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(7); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); + _data[6].assign(img7,is_shared); + } + + //! Construct list from eight images. + /** + \param img1 First input image to copy in the constructed list. + \param img2 Second input image to copy in the constructed list. + \param img3 Third input image to copy in the constructed list. + \param img4 Fourth input image to copy in the constructed list. + \param img5 Fifth input image to copy in the constructed list. + \param img6 Sixth input image to copy in the constructed list. + \param img7 Seventh input image to copy in the constructed list. + \param img8 Eighth input image to copy in the constructed list. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const CImg& img6, const CImg& img7, const CImg& img8, + const bool is_shared=false): + _width(0),_allocated_width(0),_data(0) { + assign(8); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); + _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared); + } + + //! Construct list copy. + /** + \param list Input list to copy. + \note The shared state of each element of the constructed list is kept the same as in \c list. + **/ + template + CImgList(const CImgList& list):_width(0),_allocated_width(0),_data(0) { + assign(list._width); + cimglist_for(*this,l) _data[l].assign(list[l],false); + } + + //! Construct list copy \specialization. + CImgList(const CImgList& list):_width(0),_allocated_width(0),_data(0) { + assign(list._width); + cimglist_for(*this,l) _data[l].assign(list[l],list[l]._is_shared); + } + + //! Construct list copy, and force the shared state of the list elements. + /** + \param list Input list to copy. + \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. + **/ + template + CImgList(const CImgList& list, const bool is_shared):_width(0),_allocated_width(0),_data(0) { + assign(list._width); + cimglist_for(*this,l) _data[l].assign(list[l],is_shared); + } + + //! Construct list by reading the content of a file. + /** + \param filename Filename, as a C-string. + **/ + explicit CImgList(const char *const filename):_width(0),_allocated_width(0),_data(0) { + assign(filename); + } + + //! Construct list from the content of a display window. + /** + \param disp Display window to get content from. + \note Constructed list contains a single image only. + **/ + explicit CImgList(const CImgDisplay& disp):_width(0),_allocated_width(0),_data(0) { + assign(disp); + } + + //! Return a list with elements being shared copies of images in the list instance. + /** + \note list2 = list1.get_shared() is equivalent to list2.assign(list1,true). + **/ + CImgList get_shared() { + CImgList res(_width); + cimglist_for(*this,l) res[l].assign(_data[l],true); + return res; + } + + //! Return a list with elements being shared copies of images in the list instance \const. + const CImgList get_shared() const { + CImgList res(_width); + cimglist_for(*this,l) res[l].assign(_data[l],true); + return res; + } + + //! Destructor \inplace. + /** + \see CImgList(). + **/ + CImgList& assign() { + delete[] _data; + _width = _allocated_width = 0; + _data = 0; + return *this; + } + + //! Destructor \inplace. + /** + Equivalent to assign(). + \note Only here for compatibility with STL naming conventions. + **/ + CImgList& clear() { + return assign(); + } + + //! Construct list containing empty images \inplace. + /** + \see CImgList(unsigned int). + **/ + CImgList& assign(const unsigned int n) { + if (!n) return assign(); + if (_allocated_width(n<<2)) { + delete[] _data; + _data = new CImg[_allocated_width=cimg::max(16UL,cimg::nearest_pow2(n))]; + } + _width = n; + return *this; + } + + //! Construct list containing images of specified size \inplace. + /** + \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int). + **/ + CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height=1, + const unsigned int depth=1, const unsigned int spectrum=1) { + assign(n); + cimglist_apply(*this,assign)(width,height,depth,spectrum); + return *this; + } + + //! Construct list containing images of specified size, and initialize pixel values \inplace. + /** + \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const T). + **/ + CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height, + const unsigned int depth, const unsigned int spectrum, const T val) { + assign(n); + cimglist_apply(*this,assign)(width,height,depth,spectrum,val); + return *this; + } + + //! Construct list with images of specified size, and initialize pixel values from a sequence of integers \inplace. + /** + \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const int, const int, ...). + **/ + CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height, + const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...) { + _CImgList_stdarg(int); + return *this; + } + + //! Construct list with images of specified size, and initialize pixel values from a sequence of doubles \inplace. + /** + \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const double, const double, ...). + **/ + CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height, + const unsigned int depth, const unsigned int spectrum, + const double val0, const double val1, ...) { + _CImgList_stdarg(double); + return *this; + } + + //! Construct list containing copies of an input image \inplace. + /** + \see CImgList(unsigned int, const CImg&, bool). + **/ + template + CImgList& assign(const unsigned int n, const CImg& img, const bool is_shared=false) { + assign(n); + cimglist_apply(*this,assign)(img,is_shared); + return *this; + } + + //! Construct list from one image \inplace. + /** + \see CImgList(const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img, const bool is_shared=false) { + assign(1); + _data[0].assign(img,is_shared); + return *this; + } + + //! Construct list from two images \inplace. + /** + \see CImgList(const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const bool is_shared=false) { + assign(2); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); + return *this; + } + + //! Construct list from three images \inplace. + /** + \see CImgList(const CImg&, const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const bool is_shared=false) { + assign(3); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + return *this; + } + + //! Construct list from four images \inplace. + /** + \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const bool is_shared=false) { + assign(4); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); + return *this; + } + + //! Construct list from five images \inplace. + /** + \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const bool is_shared=false) { + assign(5); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); + return *this; + } + + //! Construct list from six images \inplace. + /** + \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const CImg& img6, const bool is_shared=false) { + assign(6); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); + return *this; + } + + //! Construct list from seven images \inplace. + /** + \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const CImg& img6, const CImg& img7, const bool is_shared=false) { + assign(7); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); + _data[6].assign(img7,is_shared); + return *this; + } + + //! Construct list from eight images \inplace. + /** + \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). + **/ + template + CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, + const CImg& img5, const CImg& img6, const CImg& img7, const CImg& img8, + const bool is_shared=false) { + assign(8); + _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); + _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); + _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared); + return *this; + } + + //! Construct list as a copy of an existing list and force the shared state of the list elements \inplace. + /** + \see CImgList(const CImgList&, bool is_shared). + **/ + template + CImgList& assign(const CImgList& list, const bool is_shared=false) { + cimg::unused(is_shared); + assign(list._width); + cimglist_for(*this,l) _data[l].assign(list[l],false); + return *this; + } + + //! Construct list as a copy of an existing list and force shared state of elements \inplace \specialization. + CImgList& assign(const CImgList& list, const bool is_shared=false) { + if (this==&list) return *this; + CImgList res(list._width); + cimglist_for(res,l) res[l].assign(list[l],is_shared); + return res.move_to(*this); + } + + //! Construct list by reading the content of a file \inplace. + /** + \see CImgList(const char *const). + **/ + CImgList& assign(const char *const filename) { + return load(filename); + } + + //! Construct list from the content of a display window \inplace. + /** + \see CImgList(const CImgDisplay&). + **/ + CImgList& assign(const CImgDisplay &disp) { + return assign(CImg(disp)); + } + + //! Transfer the content of the list instance to another list. + /** + \param list Destination list. + \note When returning, the current list instance is empty and the initial content of \c list is destroyed. + **/ + template + CImgList& move_to(CImgList& list) { + list.assign(_width); + bool is_one_shared_element = false; + cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared; + if (is_one_shared_element) cimglist_for(*this,l) list[l].assign(_data[l]); + else cimglist_for(*this,l) _data[l].move_to(list[l]); + assign(); + return list; + } + + //! Transfer the content of the list instance at a specified position in another list. + /** + \param list Destination list. + \param pos Index of the insertion in the list. + \note When returning, the list instance is empty and the initial content of \c list is preserved + (only images indexes may be modified). + **/ + template + CImgList& move_to(CImgList& list, const unsigned int pos) { + if (is_empty()) return list; + const unsigned int npos = pos>list._width?list._width:pos; + list.insert(_width,npos); + bool is_one_shared_element = false; + cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared; + if (is_one_shared_element) cimglist_for(*this,l) list[npos+l].assign(_data[l]); + else cimglist_for(*this,l) _data[l].move_to(list[npos+l]); + assign(); + return list; + } + + //! Swap all fields between two list instances. + /** + \param list List to swap fields with. + \note Can be used to exchange the content of two lists in a fast way. + **/ + CImgList& swap(CImgList& list) { + cimg::swap(_width,list._width); + cimg::swap(_allocated_width,list._allocated_width); + cimg::swap(_data,list._data); + return list; + } + + //! Return a reference to an empty list. + /** + \note Can be used to define default values in a function taking a CImgList as an argument. + \code + void f(const CImgList& list=CImgList::empty()); + \endcode + **/ + static CImgList& empty() { + static CImgList _empty; + return _empty.assign(); + } + + //@} + //------------------------------------------ + // + //! \name Overloaded Operators + //@{ + //------------------------------------------ + + //! Return a reference to one image element of the list. + /** + \param pos Indice of the image element. + **/ + CImg& operator()(const unsigned int pos) { +#if cimg_verbosity>=3 + if (pos>=_width) { + cimg::warn(_cimglist_instance + "operator(): Invalid image request, at position [%u].", + cimglist_instance, + pos); + return *_data; + } +#endif + return _data[pos]; + } + + //! Return a reference to one image of the list. + /** + \param pos Indice of the image element. + **/ + const CImg& operator()(const unsigned int pos) const { + return const_cast*>(this)->operator()(pos); + } + + //! Return a reference to one pixel value of one image of the list. + /** + \param pos Indice of the image element. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note list(n,x,y,z,c) is equivalent to list[n](x,y,z,c). + **/ + T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0, + const unsigned int z=0, const unsigned int c=0) { + return (*this)[pos](x,y,z,c); + } + + //! Return a reference to one pixel value of one image of the list \const. + const T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0, + const unsigned int z=0, const unsigned int c=0) const { + return (*this)[pos](x,y,z,c); + } + + //! Return pointer to the first image of the list. + /** + \note Images in a list are stored as a buffer of \c CImg. + **/ + operator CImg*() { + return _data; + } + + //! Return pointer to the first image of the list \const. + operator const CImg*() const { + return _data; + } + + //! Construct list from one image \inplace. + /** + \param img Input image to copy in the constructed list. + \note list = img; is equivalent to list.assign(img);. + **/ + template + CImgList& operator=(const CImg& img) { + return assign(img); + } + + //! Construct list from another list. + /** + \param list Input list to copy. + \note list1 = list2 is equivalent to list1.assign(list2);. + **/ + template + CImgList& operator=(const CImgList& list) { + return assign(list); + } + + //! Construct list from another list \specialization. + CImgList& operator=(const CImgList& list) { + return assign(list); + } + + //! Construct list by reading the content of a file \inplace. + /** + \see CImgList(const char *const). + **/ + CImgList& operator=(const char *const filename) { + return assign(filename); + } + + //! Construct list from the content of a display window \inplace. + /** + \see CImgList(const CImgDisplay&). + **/ + CImgList& operator=(const CImgDisplay& disp) { + return assign(disp); + } + + //! Return a non-shared copy of a list. + /** + \note +list is equivalent to CImgList(list,false). + It forces the copy to have non-shared elements. + **/ + CImgList operator+() const { + return CImgList(*this,false); + } + + //! Return a copy of the list instance, where image \c img has been inserted at the end. + /** + \param img Image inserted at the end of the instance copy. + \note Define a convenient way to create temporary lists of images, as in the following code: + \code + (img1,img2,img3,img4).display("My four images"); + \endcode + **/ + template + CImgList& operator,(const CImg& img) { + return insert(img); + } + + //! Return a copy of the list instance, where image \c img has been inserted at the end \const. + template + CImgList operator,(const CImg& img) const { + return (+*this).insert(img); + } + + //! Return a copy of the list instance, where all elements of input list \c list have been inserted at the end. + /** + \param list List inserted at the end of the instance copy. + **/ + template + CImgList& operator,(const CImgList& list) { + return insert(list); + } + + //! Return a copy of the list instance, where all elements of input \c list have been inserted at the end \const. + template + CImgList& operator,(const CImgList& list) const { + return (+*this).insert(list); + } + + //! Return image corresponding to the appending of all images of the instance list along specified axis. + /** + \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. + \note list>'x' is equivalent to list.get_append('x'). + **/ + CImg operator>(const char axis) const { + return get_append(axis,0); + } + + //! Return list corresponding to the splitting of all images of the instance list along specified axis. + /** + \param axis Axis used for image splitting. + \note list<'x' is equivalent to list.get_split('x'). + **/ + CImgList operator<(const char axis) const { + return get_split(axis); + } + + //@} + //------------------------------------- + // + //! \name Instance Characteristics + //@{ + //------------------------------------- + + //! Return the type of image pixel values as a C string. + /** + Return a \c char* string containing the usual type name of the image pixel values + (i.e. a stringified version of the template parameter \c T). + \note + - The returned string may contain spaces (as in \c "unsigned char"). + - If the pixel type \c T does not correspond to a registered type, the string "unknown" is returned. + **/ + static const char* pixel_type() { + return cimg::type::string(); + } + + //! Return the size of the list, i.e. the number of images contained in it. + /** + \note Similar to size() but returns result as a (signed) integer. + **/ + int width() const { + return (int)_width; + } + + //! Return the size of the list, i.e. the number of images contained in it. + /** + \note Similar to width() but returns result as an unsigned integer. + **/ + unsigned int size() const { + return _width; + } + + //! Return pointer to the first image of the list. + /** + \note Images in a list are stored as a buffer of \c CImg. + **/ + CImg *data() { + return _data; + } + + //! Return pointer to the first image of the list \const. + const CImg *data() const { + return _data; + } + + //! Return pointer to the pos-th image of the list. + /** + \param pos Indice of the image element to access. + \note list.data(n); is equivalent to list.data + n;. + **/ +#if cimg_verbosity>=3 + CImg *data(const unsigned int pos) { + if (pos>=size()) + cimg::warn(_cimglist_instance + "data(): Invalid pointer request, at position [%u].", + cimglist_instance, + pos); + return _data + pos; + } + + const CImg *data(const unsigned int l) const { + return const_cast*>(this)->data(l); + } +#else + CImg *data(const unsigned int l) { + return _data + l; + } + + //! Return pointer to the pos-th image of the list \const. + const CImg *data(const unsigned int l) const { + return _data + l; + } +#endif + + //! Return iterator to the first image of the list. + /** + **/ + iterator begin() { + return _data; + } + + //! Return iterator to the first image of the list \const. + const_iterator begin() const { + return _data; + } + + //! Return iterator to one position after the last image of the list. + /** + **/ + iterator end() { + return _data + _width; + } + + //! Return iterator to one position after the last image of the list \const. + const_iterator end() const { + return _data + _width; + } + + //! Return reference to the first image of the list. + /** + **/ + CImg& front() { + return *_data; + } + + //! Return reference to the first image of the list \const. + const CImg& front() const { + return *_data; + } + + //! Return a reference to the last image of the list. + /** + **/ + const CImg& back() const { + return *(_data + _width - 1); + } + + //! Return a reference to the last image of the list \const. + CImg& back() { + return *(_data + _width - 1); + } + + //! Return pos-th image of the list. + /** + \param pos Indice of the image element to access. + **/ + CImg& at(const int pos) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "at(): Empty instance.", + cimglist_instance); + + return _data[pos<0?0:pos>=(int)_width?(int)_width-1:pos]; + } + + //! Access to pixel value with Dirichlet boundary conditions. + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c offset is outside image bounds. + \note list.atNXYZC(p,x,y,z,c); is equivalent to list[p].atXYZC(x,y,z,c);. + **/ + T& atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T out_value) { + return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atXYZC(x,y,z,c,out_value); + } + + //! Access to pixel value with Dirichlet boundary conditions \const. + T atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { + return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atXYZC(x,y,z,c,out_value); + } + + //! Access to pixel value with Neumann boundary conditions. + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note list.atNXYZC(p,x,y,z,c); is equivalent to list[p].atXYZC(x,y,z,c);. + **/ + T& atNXYZC(const int pos, const int x, const int y, const int z, const int c) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNXYZC(): Empty instance.", + cimglist_instance); + + return _atNXYZC(pos,x,y,z,c); + } + + //! Access to pixel value with Neumann boundary conditions \const. + T atNXYZC(const int pos, const int x, const int y, const int z, const int c) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNXYZC(): Empty instance.", + cimglist_instance); + + return _atNXYZC(pos,x,y,z,c); + } + + T& _atNXYZC(const int pos, const int x, const int y, const int z, const int c) { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZC(x,y,z,c); + } + + T _atNXYZC(const int pos, const int x, const int y, const int z, const int c) const { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZC(x,y,z,c); + } + + //! Access pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y,\c z). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c offset is outside image bounds. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T out_value) { + return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atXYZ(x,y,z,c,out_value); + } + + //! Access pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y,\c z) \const. + T atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { + return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atXYZ(x,y,z,c,out_value); + } + + //! Access to pixel value with Neumann boundary conditions for the 4 first coordinates (\c pos, \c x,\c y,\c z). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNXYZ(): Empty instance.", + cimglist_instance); + + return _atNXYZ(pos,x,y,z,c); + } + + //! Access to pixel value with Neumann boundary conditions for the 4 first coordinates (\c pos, \c x,\c y,\c z) \const. + T atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNXYZ(): Empty instance.", + cimglist_instance); + + return _atNXYZ(pos,x,y,z,c); + } + + T& _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZ(x,y,z,c); + } + + T _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZ(x,y,z,c); + } + + //! Access to pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c offset is outside image bounds. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atNXY(const int pos, const int x, const int y, const int z, const int c, const T out_value) { + return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atXY(x,y,z,c,out_value); + } + + //! Access to pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y) \const. + T atNXY(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { + return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atXY(x,y,z,c,out_value); + } + + //! Access to pixel value with Neumann boundary conditions for the 3 first coordinates (\c pos, \c x,\c y). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNXY(): Empty instance.", + cimglist_instance); + + return _atNXY(pos,x,y,z,c); + } + + //! Access to pixel value with Neumann boundary conditions for the 3 first coordinates (\c pos, \c x,\c y) \const. + T atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNXY(): Empty instance.", + cimglist_instance); + + return _atNXY(pos,x,y,z,c); + } + + T& _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXY(x,y,z,c); + } + + T _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXY(x,y,z,c); + } + + //! Access to pixel value with Dirichlet boundary conditions for the 2 first coordinates (\c pos,\c x). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c offset is outside image bounds. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atNX(const int pos, const int x, const int y, const int z, const int c, const T out_value) { + return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atX(x,y,z,c,out_value); + } + + //! Access to pixel value with Dirichlet boundary conditions for the 2 first coordinates (\c pos,\c x) \const. + T atNX(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { + return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atX(x,y,z,c,out_value); + } + + //! Access to pixel value with Neumann boundary conditions for the 2 first coordinates (\c pos, \c x). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNX(): Empty instance.", + cimglist_instance); + + return _atNX(pos,x,y,z,c); + } + + //! Access to pixel value with Neumann boundary conditions for the 2 first coordinates (\c pos, \c x) \const. + T atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atNX(): Empty instance.", + cimglist_instance); + + return _atNX(pos,x,y,z,c); + } + + T& _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atX(x,y,z,c); + } + + T _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atX(x,y,z,c); + } + + //! Access to pixel value with Dirichlet boundary conditions for the first coordinate (\c pos). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \param out_value Default value returned if \c offset is outside image bounds. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atN(const int pos, const int x, const int y, const int z, const int c, const T out_value) { + return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):(*this)(pos,x,y,z,c); + } + + //! Access to pixel value with Dirichlet boundary conditions for the first coordinate (\c pos) \const. + T atN(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { + return (pos<0 || pos>=(int)_width)?out_value:(*this)(pos,x,y,z,c); + } + + //! Return pixel value with Neumann boundary conditions for the first coordinate (\c pos). + /** + \param pos Indice of the image element to access. + \param x X-coordinate of the pixel value. + \param y Y-coordinate of the pixel value. + \param z Z-coordinate of the pixel value. + \param c C-coordinate of the pixel value. + \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. + **/ + T& atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atN(): Empty instance.", + cimglist_instance); + return _atN(pos,x,y,z,c); + } + + //! Return pixel value with Neumann boundary conditions for the first coordinate (\c pos) \const. + T atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "atN(): Empty instance.", + cimglist_instance); + return _atN(pos,x,y,z,c); + } + + T& _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)](x,y,z,c); + } + + T _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const { + return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)](x,y,z,c); + } + + //! Return a C-string containing the values of all images in the instance list. + /** + \param separator Character separator set between consecutive pixel values. + \param max_size Maximum size of the returned string. + \note The result is returne as a CImg image whose pixel buffer contains the desired C-string. + **/ + CImg value_string(const char separator=',', const unsigned int max_size=0) const { + if (is_empty()) return CImg(1,1,1,1,0); + CImgList items; + for (unsigned int l = 0; l<_width-1; ++l) { + CImg item = _data[l].value_string(separator,0); + item.back() = separator; + item.move_to(items); + } + _data[_width-1].value_string(separator,0).move_to(items); + CImg res; (items>'x').move_to(res); + if (max_size) { res.crop(0,max_size); res(max_size) = 0; } + return res; + } + + //@} + //------------------------------------- + // + //! \name Instance Checking + //@{ + //------------------------------------- + + //! Return \c true if list is empty. + /** + **/ + bool is_empty() const { + return (!_data || !_width); + } + + //! Test if number of image elements is equal to specified value. + /** + \param size_n Number of image elements to test. + **/ + bool is_sameN(const unsigned int size_n) const { + return _width==size_n; + } + + //! Test if number of image elements is equal between two images lists. + /** + \param list Input list to compare with. + **/ + template + bool is_sameN(const CImgList& list) const { + return is_sameN(list._width); + } + + // Define useful functions to check list dimensions. + // (cannot be documented because macro-generated). +#define _cimglist_def_is_same1(axis) \ + bool is_same##axis(const unsigned int val) const { \ + bool res = true; \ + for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(val); return res; \ + } \ + bool is_sameN##axis(const unsigned int n, const unsigned int val) const { \ + return is_sameN(n) && is_same##axis(val); \ + } \ + +#define _cimglist_def_is_same2(axis1,axis2) \ + bool is_same##axis1##axis2(const unsigned int val1, const unsigned int val2) const { \ + bool res = true; \ + for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2(val1,val2); return res; \ + } \ + bool is_sameN##axis1##axis2(const unsigned int n, const unsigned int val1, const unsigned int val2) const { \ + return is_sameN(n) && is_same##axis1##axis2(val1,val2); \ + } \ + +#define _cimglist_def_is_same3(axis1,axis2,axis3) \ + bool is_same##axis1##axis2##axis3(const unsigned int val1, const unsigned int val2, \ + const unsigned int val3) const { \ + bool res = true; \ + for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2##axis3(val1,val2,val3); \ + return res; \ + } \ + bool is_sameN##axis1##axis2##axis3(const unsigned int n, const unsigned int val1, \ + const unsigned int val2, const unsigned int val3) const { \ + return is_sameN(n) && is_same##axis1##axis2##axis3(val1,val2,val3); \ + } \ + +#define _cimglist_def_is_same(axis) \ + template bool is_same##axis(const CImg& img) const { \ + bool res = true; for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(img); return res; \ + } \ + template bool is_same##axis(const CImgList& list) const { \ + const unsigned int lmin = cimg::min(_width,list._width); \ + bool res = true; for (unsigned int l = 0; l bool is_sameN##axis(const unsigned int n, const CImg& img) const { \ + return (is_sameN(n) && is_same##axis(img)); \ + } \ + template bool is_sameN##axis(const CImgList& list) const { \ + return (is_sameN(list) && is_same##axis(list)); \ + } + + _cimglist_def_is_same(XY) + _cimglist_def_is_same(XZ) + _cimglist_def_is_same(XC) + _cimglist_def_is_same(YZ) + _cimglist_def_is_same(YC) + _cimglist_def_is_same(XYZ) + _cimglist_def_is_same(XYC) + _cimglist_def_is_same(YZC) + _cimglist_def_is_same(XYZC) + _cimglist_def_is_same1(X) + _cimglist_def_is_same1(Y) + _cimglist_def_is_same1(Z) + _cimglist_def_is_same1(C) + _cimglist_def_is_same2(X,Y) + _cimglist_def_is_same2(X,Z) + _cimglist_def_is_same2(X,C) + _cimglist_def_is_same2(Y,Z) + _cimglist_def_is_same2(Y,C) + _cimglist_def_is_same2(Z,C) + _cimglist_def_is_same3(X,Y,Z) + _cimglist_def_is_same3(X,Y,C) + _cimglist_def_is_same3(X,Z,C) + _cimglist_def_is_same3(Y,Z,C) + + //! Test if dimensions of each image of the list match specified arguments. + /** + \param dx Checked image width. + \param dy Checked image height. + \param dz Checked image depth. + \param dc Checked image spectrum. + **/ + bool is_sameXYZC(const unsigned int dx, const unsigned int dy, + const unsigned int dz, const unsigned int dc) const { + bool res = true; + for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_sameXYZC(dx,dy,dz,dc); + return res; + } + + //! Test if list dimensions match specified arguments. + /** + \param n Number of images in the list. + \param dx Checked image width. + \param dy Checked image height. + \param dz Checked image depth. + \param dc Checked image spectrum. + **/ + bool is_sameNXYZC(const unsigned int n, + const unsigned int dx, const unsigned int dy, + const unsigned int dz, const unsigned int dc) const { + return is_sameN(n) && is_sameXYZC(dx,dy,dz,dc); + } + + //! Test if list contains one particular pixel location. + /** + \param n Index of the image whom checked pixel value belong to. + \param x X-coordinate of the checked pixel value. + \param y Y-coordinate of the checked pixel value. + \param z Z-coordinate of the checked pixel value. + \param c C-coordinate of the checked pixel value. + **/ + bool containsNXYZC(const int n, const int x=0, const int y=0, const int z=0, const int c=0) const { + if (is_empty()) return false; + return n>=0 && n<(int)_width && x>=0 && x<_data[n].width() && y>=0 && y<_data[n].height() && + z>=0 && z<_data[n].depth() && c>=0 && c<_data[n].spectrum(); + } + + //! Test if list contains image with specified indice. + /** + \param n Index of the checked image. + **/ + bool containsN(const int n) const { + if (is_empty()) return false; + return n>=0 && n<(int)_width; + } + + //! Test if one image of the list contains the specified referenced value. + /** + \param pixel Reference to pixel value to test. + \param[out] n Index of image containing the pixel value, if test succeeds. + \param[out] x X-coordinate of the pixel value, if test succeeds. + \param[out] y Y-coordinate of the pixel value, if test succeeds. + \param[out] z Z-coordinate of the pixel value, if test succeeds. + \param[out] c C-coordinate of the pixel value, if test succeeds. + \note If true, set coordinates (n,x,y,z,c). + **/ + template + bool contains(const T& pixel, t& n, t& x, t&y, t& z, t& c) const { + if (is_empty()) return false; + cimglist_for(*this,l) if (_data[l].contains(pixel,x,y,z,c)) { n = (t)l; return true; } + return false; + } + + //! Test if one of the image list contains the specified referenced value. + /** + \param pixel Reference to pixel value to test. + \param[out] n Index of image containing the pixel value, if test succeeds. + \param[out] x X-coordinate of the pixel value, if test succeeds. + \param[out] y Y-coordinate of the pixel value, if test succeeds. + \param[out] z Z-coordinate of the pixel value, if test succeeds. + \note If true, set coordinates (n,x,y,z). + **/ + template + bool contains(const T& pixel, t& n, t& x, t&y, t& z) const { + t c; + return contains(pixel,n,x,y,z,c); + } + + //! Test if one of the image list contains the specified referenced value. + /** + \param pixel Reference to pixel value to test. + \param[out] n Index of image containing the pixel value, if test succeeds. + \param[out] x X-coordinate of the pixel value, if test succeeds. + \param[out] y Y-coordinate of the pixel value, if test succeeds. + \note If true, set coordinates (n,x,y). + **/ + template + bool contains(const T& pixel, t& n, t& x, t&y) const { + t z, c; + return contains(pixel,n,x,y,z,c); + } + + //! Test if one of the image list contains the specified referenced value. + /** + \param pixel Reference to pixel value to test. + \param[out] n Index of image containing the pixel value, if test succeeds. + \param[out] x X-coordinate of the pixel value, if test succeeds. + \note If true, set coordinates (n,x). + **/ + template + bool contains(const T& pixel, t& n, t& x) const { + t y, z, c; + return contains(pixel,n,x,y,z,c); + } + + //! Test if one of the image list contains the specified referenced value. + /** + \param pixel Reference to pixel value to test. + \param[out] n Index of image containing the pixel value, if test succeeds. + \note If true, set coordinates (n). + **/ + template + bool contains(const T& pixel, t& n) const { + t x, y, z, c; + return contains(pixel,n,x,y,z,c); + } + + //! Test if one of the image list contains the specified referenced value. + /** + \param pixel Reference to pixel value to test. + **/ + bool contains(const T& pixel) const { + unsigned int n, x, y, z, c; + return contains(pixel,n,x,y,z,c); + } + + //! Test if the list contains the image 'img'. + /** + \param img Reference to image to test. + \param[out] n Index of image in the list, if test succeeds. + \note If true, returns the position (n) of the image in the list. + **/ + template + bool contains(const CImg& img, t& n) const { + if (is_empty()) return false; + const CImg *const ptr = &img; + cimglist_for(*this,i) if (_data+i==ptr) { n = (t)i; return true; } + return false; + } + + //! Test if the list contains the image img. + /** + \param img Reference to image to test. + **/ + bool contains(const CImg& img) const { + unsigned int n; + return contains(img,n); + } + + //@} + //------------------------------------- + // + //! \name Mathematical Functions + //@{ + //------------------------------------- + + //! Return a reference to the minimum pixel value of the instance list. + /** + **/ + T& min() { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "min(): Empty instance.", + cimglist_instance); + T *ptr_min = _data->_data; + T min_value = *ptr_min; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) if (*ptrs_data; + T min_value = *ptr_min; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) if (*ptrs_data; + T max_value = *ptr_max; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs); + } + return *ptr_max; + } + + //! Return a reference to the maximum pixel value of the instance list \const. + const T& max() const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "max(): Empty instance.", + cimglist_instance); + const T *ptr_max = _data->_data; + T max_value = *ptr_max; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs); + } + return *ptr_max; + } + + //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well. + /** + \param[out] max_val Value of the maximum value found. + **/ + template + T& min_max(t& max_val) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "min_max(): Empty instance.", + cimglist_instance); + T *ptr_min = _data->_data; + T min_value = *ptr_min, max_value = min_value; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) { + const T val = *ptrs; + if (valmax_value) max_value = val; + } + } + max_val = (t)max_value; + return *ptr_min; + } + + //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well \const. + /** + \param[out] max_val Value of the maximum value found. + **/ + template + const T& min_max(t& max_val) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "min_max(): Empty instance.", + cimglist_instance); + const T *ptr_min = _data->_data; + T min_value = *ptr_min, max_value = min_value; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) { + const T val = *ptrs; + if (valmax_value) max_value = val; + } + } + max_val = (t)max_value; + return *ptr_min; + } + + //! Return a reference to the minimum pixel value of the instance list and return the minimum value as well. + /** + \param[out] min_val Value of the minimum value found. + **/ + template + T& max_min(t& min_val) { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "max_min(): Empty instance.", + cimglist_instance); + T *ptr_max = _data->_data; + T min_value = *ptr_max, max_value = min_value; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) { + const T val = *ptrs; + if (val>max_value) { max_value = val; ptr_max = ptrs; } + if (val + const T& max_min(t& min_val) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "max_min(): Empty instance.", + cimglist_instance); + const T *ptr_max = _data->_data; + T min_value = *ptr_max, max_value = min_value; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + cimg_for(img,ptrs,T) { + const T val = *ptrs; + if (val>max_value) { max_value = val; ptr_max = ptrs; } + if (val + CImgList& insert(const CImg& img, const unsigned int pos=~0U, const bool is_shared=false) { + const unsigned int npos = pos==~0U?_width:pos; + if (npos>_width) + throw CImgArgumentException(_cimglist_instance + "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) " + "at position %u.", + cimglist_instance, + img._width,img._height,img._depth,img._spectrum,img._data,npos); + if (is_shared) + throw CImgArgumentException(_cimglist_instance + "insert(): Invalid insertion request of specified shared image " + "CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).", + cimglist_instance, + img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos); + + CImg *const new_data = (++_width>_allocated_width)?new CImg[_allocated_width?(_allocated_width<<=1): + (_allocated_width=16)]:0; + if (!_data) { // Insert new element into empty list. + _data = new_data; + *_data = img; + } else { + if (new_data) { // Insert with re-allocation. + if (npos) std::memcpy(new_data,_data,sizeof(CImg)*npos); + if (npos!=_width-1) std::memcpy(new_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); + std::memset(_data,0,sizeof(CImg)*(_width-1)); + delete[] _data; + _data = new_data; + } else if (npos!=_width-1) + std::memmove(_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); // Insert without re-allocation. + _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0; _data[npos]._data = 0; + _data[npos] = img; + } + return *this; + } + + //! Insert a copy of the image \c img into the current image list, at position \c pos \specialization. + CImgList& insert(const CImg& img, const unsigned int pos=~0U, const bool is_shared=false) { + const unsigned int npos = pos==~0U?_width:pos; + if (npos>_width) + throw CImgArgumentException(_cimglist_instance + "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) " + "at position %u.", + cimglist_instance, + img._width,img._height,img._depth,img._spectrum,img._data,npos); + CImg *const new_data = (++_width>_allocated_width)?new CImg[_allocated_width?(_allocated_width<<=1): + (_allocated_width=16)]:0; + if (!_data) { // Insert new element into empty list. + _data = new_data; + if (is_shared && img) { + _data->_width = img._width; + _data->_height = img._height; + _data->_depth = img._depth; + _data->_spectrum = img._spectrum; + _data->_is_shared = true; + _data->_data = img._data; + } else *_data = img; + } + else { + if (new_data) { // Insert with re-allocation. + if (npos) std::memcpy(new_data,_data,sizeof(CImg)*npos); + if (npos!=_width-1) std::memcpy(new_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); + if (is_shared && img) { + new_data[npos]._width = img._width; + new_data[npos]._height = img._height; + new_data[npos]._depth = img._depth; + new_data[npos]._spectrum = img._spectrum; + new_data[npos]._is_shared = true; + new_data[npos]._data = img._data; + } else { + new_data[npos]._width = new_data[npos]._height = new_data[npos]._depth = new_data[npos]._spectrum = 0; + new_data[npos]._data = 0; + new_data[npos] = img; + } + std::memset(_data,0,sizeof(CImg)*(_width-1)); + delete[] _data; + _data = new_data; + } else { // Insert without re-allocation. + if (npos!=_width-1) std::memmove(_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); + if (is_shared && img) { + _data[npos]._width = img._width; + _data[npos]._height = img._height; + _data[npos]._depth = img._depth; + _data[npos]._spectrum = img._spectrum; + _data[npos]._is_shared = true; + _data[npos]._data = img._data; + } else { + _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0; + _data[npos]._data = 0; + _data[npos] = img; + } + } + } + return *this; + } + + //! Insert a copy of the image \c img into the current image list, at position \c pos \newinstance. + template + CImgList get_insert(const CImg& img, const unsigned int pos=~0U, const bool is_shared=false) const { + return (+*this).insert(img,pos,is_shared); + } + + //! Insert n empty images img into the current image list, at position \p pos. + /** + \param n Number of empty images to insert. + \param pos Index of the insertion. + **/ + CImgList& insert(const unsigned int n, const unsigned int pos=~0U) { + CImg empty; + if (!n) return *this; + const unsigned int npos = pos==~0U?_width:pos; + for (unsigned int i = 0; i get_insert(const unsigned int n, const unsigned int pos=~0U) const { + return (+*this).insert(n,pos); + } + + //! Insert \c n copies of the image \c img into the current image list, at position \c pos. + /** + \param n Number of image copies to insert. + \param img Image to insert by copy. + \param pos Index of the insertion. + \param is_shared Tells if inserted images are shared copies of \c img or not. + **/ + template + CImgList& insert(const unsigned int n, const CImg& img, const unsigned int pos=~0U, + const bool is_shared=false) { + if (!n) return *this; + const unsigned int npos = pos==~0U?_width:pos; + insert(img,npos,is_shared); + for (unsigned int i = 1; i + CImgList get_insert(const unsigned int n, const CImg& img, const unsigned int pos=~0U, + const bool is_shared=false) const { + return (+*this).insert(n,img,pos,is_shared); + } + + //! Insert a copy of the image list \c list into the current image list, starting from position \c pos. + /** + \param list Image list to insert. + \param pos Index of the insertion. + \param is_shared Tells if inserted images are shared copies of images of \c list or not. + **/ + template + CImgList& insert(const CImgList& list, const unsigned int pos=~0U, const bool is_shared=false) { + const unsigned int npos = pos==~0U?_width:pos; + if ((void*)this!=(void*)&list) cimglist_for(list,l) insert(list[l],npos+l,is_shared); + else insert(CImgList(list),npos,is_shared); + return *this; + } + + //! Insert a copy of the image list \c list into the current image list, starting from position \c pos \newinstance. + template + CImgList get_insert(const CImgList& list, const unsigned int pos=~0U, const bool is_shared=false) const { + return (+*this).insert(list,pos,is_shared); + } + + //! Insert n copies of the list \c list at position \c pos of the current list. + /** + \param n Number of list copies to insert. + \param list Image list to insert. + \param pos Index of the insertion. + \param is_shared Tells if inserted images are shared copies of images of \c list or not. + **/ + template + CImgList& insert(const unsigned int n, const CImgList& list, const unsigned int pos=~0U, + const bool is_shared=false) { + if (!n) return *this; + const unsigned int npos = pos==~0U?_width:pos; + for (unsigned int i = 0; i + CImgList get_insert(const unsigned int n, const CImgList& list, const unsigned int pos=~0U, + const bool is_shared=false) const { + return (+*this).insert(n,list,pos,is_shared); + } + + //! Remove all images between from indexes. + /** + \param pos1 Starting index of the removal. + \param pos2 Ending index of the removal. + **/ + CImgList& remove(const unsigned int pos1, const unsigned int pos2) { + const unsigned int + npos1 = pos1=_width) + throw CImgArgumentException(_cimglist_instance + "remove(): Invalid remove request at positions %u->%u.", + cimglist_instance, + npos1,tpos2); + else { + if (tpos2>=_width) + throw CImgArgumentException(_cimglist_instance + "remove(): Invalid remove request at positions %u->%u.", + cimglist_instance, + npos1,tpos2); + + for (unsigned int k = npos1; k<=npos2; ++k) _data[k].assign(); + const unsigned int nb = 1 + npos2 - npos1; + if (!(_width-=nb)) return assign(); + if (_width>(_allocated_width>>2) || _allocated_width<=16) { // Removing items without reallocation. + if (npos1!=_width) std::memmove(_data+npos1,_data+npos2+1,sizeof(CImg)*(_width - npos1)); + std::memset(_data + _width,0,sizeof(CImg)*nb); + } else { // Removing items with reallocation. + _allocated_width>>=2; + while (_allocated_width>16 && _width<(_allocated_width>>1)) _allocated_width>>=1; + CImg *const new_data = new CImg[_allocated_width]; + if (npos1) std::memcpy(new_data,_data,sizeof(CImg)*npos1); + if (npos1!=_width) std::memcpy(new_data+npos1,_data+npos2+1,sizeof(CImg)*(_width-npos1)); + if (_width!=_allocated_width) std::memset(new_data+_width,0,sizeof(_allocated_width - _width)); + std::memset(_data,0,sizeof(CImg)*(_width+nb)); + delete[] _data; + _data = new_data; + } + } + return *this; + } + + //! Remove all images between from indexes \newinstance. + CImgList get_remove(const unsigned int pos1, const unsigned int pos2) const { + return (+*this).remove(pos1,pos2); + } + + //! Remove image at index \c pos from the image list. + /** + \param pos Index of the image to remove. + **/ + CImgList& remove(const unsigned int pos) { + return remove(pos,pos); + } + + //! Remove image at index \c pos from the image list \newinstance. + CImgList get_remove(const unsigned int pos) const { + return (+*this).remove(pos); + } + + //! Remove last image. + /** + **/ + CImgList& remove() { + return remove(_width-1); + } + + //! Remove last image \newinstance. + CImgList get_remove() const { + return (+*this).remove(); + } + + //! Reverse list order. + CImgList& reverse() { + for (unsigned int l = 0; l<_width/2; ++l) (*this)[l].swap((*this)[_width-1-l]); + return *this; + } + + //! Reverse list order \newinstance. + CImgList get_reverse() const { + return (+*this).reverse(); + } + + //! Return a sublist. + /** + \param pos0 Starting index of the sublist. + \param pos1 Ending index of the sublist. + **/ + CImgList& images(const unsigned int pos0, const unsigned int pos1) { + return get_images(pos0,pos1).move_to(*this); + } + + //! Return a sublist \newinstance. + CImgList get_images(const unsigned int pos0, const unsigned int pos1) const { + if (pos0>pos1 || pos1>=_width) + throw CImgArgumentException(_cimglist_instance + "images(): Specified sub-list indices (%u->%u) are out of bounds.", + cimglist_instance, + pos0,pos1); + CImgList res(pos1-pos0+1); + cimglist_for(res,l) res[l].assign(_data[pos0+l]); + return res; + } + + //! Return a shared sublist. + /** + \param pos0 Starting index of the sublist. + \param pos1 Ending index of the sublist. + **/ + CImgList get_shared_images(const unsigned int pos0, const unsigned int pos1) { + if (pos0>pos1 || pos1>=_width) + throw CImgArgumentException(_cimglist_instance + "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.", + cimglist_instance, + pos0,pos1); + CImgList res(pos1-pos0+1); + cimglist_for(res,l) res[l].assign(_data[pos0+l],_data[pos0+l]?true:false); + return res; + } + + //! Return a shared sublist \newinstance. + const CImgList get_shared_images(const unsigned int pos0, const unsigned int pos1) const { + if (pos0>pos1 || pos1>=_width) + throw CImgArgumentException(_cimglist_instance + "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.", + cimglist_instance, + pos0,pos1); + CImgList res(pos1-pos0+1); + cimglist_for(res,l) res[l].assign(_data[pos0+l],_data[pos0+l]?true:false); + return res; + } + + //! Return a single image which is the appending of all images of the current CImgList instance. + /** + \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + CImg get_append(const char axis, const float align=0) const { + if (is_empty()) return CImg(); + if (_width==1) return +((*this)[0]); + unsigned int dx = 0, dy = 0, dz = 0, dc = 0, pos = 0; + CImg res; + switch (cimg::uncase(axis)) { + case 'x' : { // Along the X-axis. + cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) { + dx+=img._width; + dy = cimg::max(dy,img._height); + dz = cimg::max(dz,img._depth); + dc = cimg::max(dc,img._spectrum); + } + } + res.assign(dx,dy,dz,dc,0); + if (res) cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) res.draw_image(pos, + (int)(align*(dy-img._height)), + (int)(align*(dz-img._depth)), + (int)(align*(dc-img._spectrum)), + img); + pos+=img._width; + } + } break; + case 'y' : { // Along the Y-axis. + cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) { + dx = cimg::max(dx,img._width); + dy+=img._height; + dz = cimg::max(dz,img._depth); + dc = cimg::max(dc,img._spectrum); + } + } + res.assign(dx,dy,dz,dc,0); + if (res) cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) res.draw_image((int)(align*(dx-img._width)), + pos, + (int)(align*(dz-img._depth)), + (int)(align*(dc-img._spectrum)), + img); + pos+=img._height; + } + } break; + case 'z' : { // Along the Z-axis. + cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) { + dx = cimg::max(dx,img._width); + dy = cimg::max(dy,img._height); + dz+=img._depth; + dc = cimg::max(dc,img._spectrum); + } + } + res.assign(dx,dy,dz,dc,0); + if (res) cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) res.draw_image((int)(align*(dx-img._width)), + (int)(align*(dy-img._height)), + pos, + (int)(align*(dc-img._spectrum)), + img); + pos+=img._depth; + } + } break; + default : { // Along the C-axis. + cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) { + dx = cimg::max(dx,img._width); + dy = cimg::max(dy,img._height); + dz = cimg::max(dz,img._depth); + dc+=img._spectrum; + } + } + res.assign(dx,dy,dz,dc,0); + if (res) cimglist_for(*this,l) { + const CImg& img = (*this)[l]; + if (img) res.draw_image((int)(align*(dx-img._width)), + (int)(align*(dy-img._height)), + (int)(align*(dz-img._depth)), + pos, + img); + pos+=img._spectrum; + } + } + } + return res; + } + + //! Return a list where each image has been split along the specified axis. + /** + \param axis Axis to split images along. + \param nb Number of spliting parts for each image. + **/ + CImgList& split(const char axis, const int nb=0) { + return get_split(axis,nb).move_to(*this); + } + + //! Return a list where each image has been split along the specified axis \newinstance. + CImgList get_split(const char axis, const int nb=0) const { + CImgList res; + cimglist_for(*this,l) _data[l].get_split(axis,nb).move_to(res,~0U); + return res; + } + + //! Insert image at the end of the list. + /** + \param img Image to insert. + **/ + template + CImgList& push_back(const CImg& img) { + return insert(img); + } + + //! Insert image at the front of the list. + /** + \param img Image to insert. + **/ + template + CImgList& push_front(const CImg& img) { + return insert(img,0); + } + + //! Insert list at the end of the current list. + /** + \param list List to insert. + **/ + template + CImgList& push_back(const CImgList& list) { + return insert(list); + } + + //! Insert list at the front of the current list. + /** + \param list List to insert. + **/ + template + CImgList& push_front(const CImgList& list) { + return insert(list,0); + } + + //! Remove last image. + /** + **/ + CImgList& pop_back() { + return remove(_width-1); + } + + //! Remove first image. + /** + **/ + CImgList& pop_front() { + return remove(0); + } + + //! Remove image pointed by iterator. + /** + \param iter Iterator pointing to the image to remove. + **/ + CImgList& erase(const iterator iter) { + return remove(iter-_data); + } + + //@} + //---------------------------------- + // + //! \name Data Input + //@{ + //---------------------------------- + + //! Display a simple interactive interface to select images or sublists. + /** + \param disp Window instance to display selection and user interface. + \param feature_type Can be \c false to select a single image, or \c true to select a sublist. + \param axis Axis along whom images are appended for visualization. + \param align Alignment setting when images have not all the same size. + \return A one-column vector containing the selected image indexes. + **/ + CImg get_select(CImgDisplay &disp, const bool feature_type=true, + const char axis='x', const float align=0) const { + return _get_select(disp,0,feature_type,axis,align,0,false,false,false); + } + + //! Display a simple interactive interface to select images or sublists. + /** + \param title Title of a new window used to display selection and user interface. + \param feature_type Can be \c false to select a single image, or \c true to select a sublist. + \param axis Axis along whom images are appended for visualization. + \param align Alignment setting when images have not all the same size. + \return A one-column vector containing the selected image indexes. + **/ + CImg get_select(const char *const title, const bool feature_type=true, + const char axis='x', const float align=0) const { + CImgDisplay disp; + return _get_select(disp,title,feature_type,axis,align,0,false,false,false); + } + + CImg _get_select(CImgDisplay &disp, const char *const title, const bool feature_type, + const char axis, const float align, + const unsigned int orig, const bool resize_disp, + const bool exit_on_rightbutton, const bool exit_on_wheel) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "select(): Empty instance.", + cimglist_instance); + + // Create image correspondence table and get list dimensions for visualization. + CImgList _indices; + unsigned int max_width = 0, max_height = 0, sum_width = 0, sum_height = 0; + cimglist_for(*this,l) { + const CImg& img = _data[l]; + const unsigned int + w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false), + h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true); + if (w>max_width) max_width = w; + if (h>max_height) max_height = h; + sum_width+=w; sum_height+=h; + if (axis=='x') CImg(w,1,1,1,(unsigned int)l).move_to(_indices); + else CImg(h,1,1,1,(unsigned int)l).move_to(_indices); + } + const CImg indices0 = _indices>'x'; + + // Create display window. + if (!disp) { + if (axis=='x') disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:0,1); + else disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:0,1); + if (!title) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width); + } else if (title) disp.set_title("%s",title); + if (resize_disp) { + if (axis=='x') disp.resize(cimg_fitscreen(sum_width,max_height,1),false); + else disp.resize(cimg_fitscreen(max_width,sum_height,1),false); + } + + const unsigned int old_normalization = disp.normalization(); + bool old_is_resized = disp.is_resized(); + disp._normalization = 0; + disp.show().set_key(0); + const unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 }; + + // Enter event loop. + CImg visu0, visu; + CImg indices; + CImg positions(_width,4,1,1,-1); + int oindice0 = -1, oindice1 = -1, indice0 = -1, indice1 = -1; + bool is_clicked = false, is_selected = false, text_down = false, update_display = true; + unsigned int key = 0; + while (!is_selected && !disp.is_closed() && !key) { + + // Create background image. + if (!visu0) { + visu0.assign(disp._width,disp._height,1,3,0); visu.assign(); + (indices0.get_resize(axis=='x'?visu0._width:visu0._height,1)).move_to(indices); + unsigned int ind = 0; + if (axis=='x') for (unsigned int x = 0; x + onexone(1,1,1,1,0), + &src = _data[ind]?_data[ind]:onexone; + CImg res; + src.__get_select(disp,old_normalization,(src._width-1)/2,(src._height-1)/2,(src._depth-1)/2).move_to(res); + const unsigned int h = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,true); + res.resize(x - x0,cimg::max(32U,h*disp._height/max_height),1,res._spectrum==1?3:-100); + positions(ind,0) = positions(ind,2) = (int)x0; + positions(ind,1) = positions(ind,3) = (int)(align*(visu0.height()-res.height())); + positions(ind,2)+=res._width; + positions(ind,3)+=res._height - 1; + visu0.draw_image(positions(ind,0),positions(ind,1),res); + } else for (unsigned int y = 0; y + &src = _data[ind], + _img2d = src._depth>1?src.get_projections2d((src._width-1)/2,(src._height-1)/2,(src._depth-1)/2): + CImg(), + &img2d = _img2d?_img2d:src; + CImg res = old_normalization==1 || + (old_normalization==3 && cimg::type::string()!=cimg::type::string())? + CImg(img2d.get_normalize(0,255)): + CImg(img2d); + if (res._spectrum>3) res.channels(0,2); + const unsigned int w = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,false); + res.resize(cimg::max(32U,w*disp._width/max_width),y - y0,1,res._spectrum==1?3:-100); + positions(ind,0) = positions(ind,2) = (int)(align*(visu0.width()-res.width())); + positions(ind,1) = positions(ind,3) = (int)y0; + positions(ind,2)+=res._width - 1; + positions(ind,3)+=res._height; + visu0.draw_image(positions(ind,0),positions(ind,1),res); + } + if (axis=='x') --positions(ind,2); else --positions(ind,3); + update_display = true; + } + + if (!visu || oindice0!=indice0 || oindice1!=indice1) { + if (indice0>=0 && indice1>=0) { + visu.assign(visu0,false); + const int indm = cimg::min(indice0,indice1), indM = cimg::max(indice0,indice1); + for (int ind = indm; ind<=indM; ++ind) if (positions(ind,0)>=0) { + visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3), + background_color,0.2f); + if ((axis=='x' && positions(ind,2) - positions(ind,0)>=8) || + (axis!='x' && positions(ind,3) - positions(ind,1)>=8)) + visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3), + foreground_color,0.9f,0xAAAAAAAA); + } + const int yt = (int)text_down?visu.height()-13:0; + if (is_clicked) visu.draw_text(0,yt," Images #%u - #%u, Size = %u", + foreground_color,background_color,0.7f,13, + orig + indm,orig + indM,indM - indm + 1); + else visu.draw_text(0,yt," Image #%u (%u,%u,%u,%u)",foreground_color,background_color,0.7f,13, + orig + indice0, + _data[orig+indice0]._width, + _data[orig+indice0]._height, + _data[orig+indice0]._depth, + _data[orig+indice0]._spectrum); + update_display = true; + } else visu.assign(); + } + if (!visu) { visu.assign(visu0,true); update_display = true; } + if (update_display) { visu.display(disp); update_display = false; } + disp.wait(); + + // Manage user events. + const int xm = disp.mouse_x(), ym = disp.mouse_y(); + int indice = -1; + + if (xm>=0) { + indice = (int)indices(axis=='x'?xm:ym); + if (disp.button()&1) { + if (!is_clicked) { is_clicked = true; oindice0 = indice0; indice0 = indice; } + oindice1 = indice1; indice1 = indice; + if (!feature_type) is_selected = true; + } else { + if (!is_clicked) { oindice0 = oindice1 = indice0; indice0 = indice1 = indice; } + else is_selected = true; + } + } else { + if (is_clicked) { + if (!(disp.button()&1)) { is_clicked = is_selected = false; indice0 = indice1 = -1; } + else indice1 = -1; + } else indice0 = indice1 = -1; + } + + if (disp.button()&4) { is_clicked = is_selected = false; indice0 = indice1 = -1; } + if (disp.button()&2 && exit_on_rightbutton) { is_selected = true; indice1 = indice0 = -1; } + if (disp.wheel() && exit_on_wheel) is_selected = true; + + switch (key = disp.key()) { +#if cimg_OS!=2 + case cimg::keyCTRLRIGHT : +#endif + case 0 : case cimg::keyCTRLLEFT : key = 0; break; + case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), + CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). + _is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.set_fullscreen(false). + resize(cimg_fitscreen(axis=='x'?sum_width:max_width,axis=='x'?max_height:sum_height,1),false). + _is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true; + disp.set_key(key,false); key = 0; visu0.assign(); + } break; + case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + if (visu0) { + (+visu0).draw_text(0,0," Saving snapshot... ", + foreground_color,background_color,0.7f,13).display(disp); + visu0.save(filename); + (+visu0).draw_text(0,0," Snapshot '%s' saved. ", + foreground_color,background_color,0.7f,13,filename).display(disp); + } + disp.set_key(key,false).wait(); key = 0; + } break; + case cimg::keyO : + if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { + static unsigned int snap_number = 0; + char filename[32] = { 0 }; + std::FILE *file; + do { +#ifdef cimg_use_zlib + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); +#else + cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); +#endif + if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); + } while (file); + (+visu0).draw_text(0,0," Saving instance... ", + foreground_color,background_color,0.7f,13).display(disp); + save(filename); + (+visu0).draw_text(0,0," Instance '%s' saved. ", + foreground_color,background_color,0.7f,13,filename).display(disp); + disp.set_key(key,false).wait(); key = 0; + } break; + } + if (disp.is_resized()) { disp.resize(false); visu0.assign(); } + if (ym>=0 && ym<13) { if (!text_down) { visu.assign(); text_down = true; }} + else if (ym>=visu.height()-13) { if(text_down) { visu.assign(); text_down = false; }} + } + CImg res(1,2,1,1,-1); + if (is_selected) { + if (feature_type) res.fill(cimg::min(indice0,indice1),cimg::max(indice0,indice1)); + else res.fill(indice0); + } + if (!(disp.button()&2)) disp.set_button(); + disp._normalization = old_normalization; + disp._is_resized = old_is_resized; + disp.set_key(key); + return res; + } + + //! Load a list from a file. + /** + \param filename Filename to read data from. + **/ + CImgList& load(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "load(): Specified filename is (null).", + cimglist_instance); + + if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) { + char filename_local[1024] = { 0 }; + load(cimg::load_network_external(filename,filename_local)); + std::remove(filename_local); + return *this; + } + + const char *const ext = cimg::split_filename(filename); + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + try { +#ifdef cimglist_load_plugin + cimglist_load_plugin(filename); +#endif +#ifdef cimglist_load_plugin1 + cimglist_load_plugin1(filename); +#endif +#ifdef cimglist_load_plugin2 + cimglist_load_plugin2(filename); +#endif +#ifdef cimglist_load_plugin3 + cimglist_load_plugin3(filename); +#endif +#ifdef cimglist_load_plugin4 + cimglist_load_plugin4(filename); +#endif +#ifdef cimglist_load_plugin5 + cimglist_load_plugin5(filename); +#endif +#ifdef cimglist_load_plugin6 + cimglist_load_plugin6(filename); +#endif +#ifdef cimglist_load_plugin7 + cimglist_load_plugin7(filename); +#endif +#ifdef cimglist_load_plugin8 + cimglist_load_plugin8(filename); +#endif + if (!cimg::strcasecmp(ext,"tif") || + !cimg::strcasecmp(ext,"tiff")) load_tiff(filename); + else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename); + else if (!cimg::strcasecmp(ext,"cimg") || + !cimg::strcasecmp(ext,"cimgz") || + !*ext) load_cimg(filename); + else if (!cimg::strcasecmp(ext,"rec") || + !cimg::strcasecmp(ext,"par")) load_parrec(filename); + else if (!cimg::strcasecmp(ext,"avi") || + !cimg::strcasecmp(ext,"mov") || + !cimg::strcasecmp(ext,"asf") || + !cimg::strcasecmp(ext,"divx") || + !cimg::strcasecmp(ext,"flv") || + !cimg::strcasecmp(ext,"mpg") || + !cimg::strcasecmp(ext,"m1v") || + !cimg::strcasecmp(ext,"m2v") || + !cimg::strcasecmp(ext,"m4v") || + !cimg::strcasecmp(ext,"mjp") || + !cimg::strcasecmp(ext,"mp4") || + !cimg::strcasecmp(ext,"mkv") || + !cimg::strcasecmp(ext,"mpe") || + !cimg::strcasecmp(ext,"movie") || + !cimg::strcasecmp(ext,"ogm") || + !cimg::strcasecmp(ext,"ogg") || + !cimg::strcasecmp(ext,"ogv") || + !cimg::strcasecmp(ext,"qt") || + !cimg::strcasecmp(ext,"rm") || + !cimg::strcasecmp(ext,"vob") || + !cimg::strcasecmp(ext,"wmv") || + !cimg::strcasecmp(ext,"xvid") || + !cimg::strcasecmp(ext,"mpeg")) load_ffmpeg(filename); + else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename); + else throw CImgIOException("CImgList<%s>::load()", + pixel_type()); + } catch (CImgIOException&) { + try { + cimg::fclose(cimg::fopen(filename,"rb")); + } catch (CImgIOException&) { + cimg::exception_mode() = omode; + throw CImgIOException(_cimglist_instance + "load(): Failed to open file '%s'.", + cimglist_instance, + filename); + } + assign(1); + try { + _data->load(filename); + } catch (CImgIOException&) { + cimg::exception_mode() = omode; + throw CImgIOException(_cimglist_instance + "load(): Failed to recognize format of file '%s'.", + cimglist_instance, + filename); + } + } + cimg::exception_mode() = omode; + return *this; + } + + //! Load a list from a file \newinstance. + static CImgList get_load(const char *const filename) { + return CImgList().load(filename); + } + + //! Load a list from a .cimg file. + /** + \param filename Filename to read data from. + **/ + CImgList& load_cimg(const char *const filename) { + return _load_cimg(0,filename); + } + + //! Load a list from a .cimg file \newinstance. + static CImgList get_load_cimg(const char *const filename) { + return CImgList().load_cimg(filename); + } + + //! Load a list from a .cimg file. + /** + \param file File to read data from. + **/ + CImgList& load_cimg(std::FILE *const file) { + return _load_cimg(file,0); + } + + //! Load a list from a .cimg file \newinstance. + static CImgList get_load_cimg(std::FILE *const file) { + return CImgList().load_cimg(file); + } + + CImgList& _load_cimg(std::FILE *const file, const char *const filename) { +#ifdef cimg_use_zlib +#define _cimgz_load_cimg_case(Tss) { \ + Bytef *const cbuf = new Bytef[csiz]; \ + cimg::fread(cbuf,csiz,nfile); \ + raw.assign(W,H,D,C); \ + unsigned long destlen = (unsigned long)raw.size()*sizeof(Tss); \ + uncompress((Bytef*)raw._data,&destlen,cbuf,csiz); \ + delete[] cbuf; \ + const Tss *ptrs = raw._data; \ + for (unsigned long off = raw.size(); off; --off) *(ptrd++) = (T)*(ptrs++); \ +} +#else +#define _cimgz_load_cimg_case(Tss) \ + throw CImgIOException(_cimglist_instance \ + "load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.", \ + cimglist_instance, \ + filename?filename:"(FILE*)"); +#endif + +#define _cimg_load_cimg_case(Ts,Tss) \ + if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \ + for (unsigned int l = 0; l=0) tmp[j++] = (char)i; tmp[j] = 0; \ + W = H = D = C = 0; csiz = 0; \ + if ((err = std::sscanf(tmp,"%u %u %u %u #%u",&W,&H,&D,&C,&csiz))<4) \ + throw CImgIOException(_cimglist_instance \ + "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'", \ + cimglist_instance, \ + W,H,D,C,l,filename?filename:("(FILE*)")); \ + if (W*H*D*C>0) { \ + CImg raw; \ + CImg &img = _data[l]; \ + img.assign(W,H,D,C); \ + T *ptrd = img._data; \ + if (err==5) _cimgz_load_cimg_case(Tss) \ + else for (long to_read = (long)img.size(); to_read>0; ) { \ + raw.assign(cimg::min(to_read,cimg_iobuffer)); \ + cimg::fread(raw._data,raw._width,nfile); \ + if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); \ + to_read-=raw._width; \ + const Tss *ptrs = raw._data; \ + for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \ + } \ + } \ + } \ + loaded = true; \ + } + + if (!filename && !file) + throw CImgArgumentException(_cimglist_instance + "load_cimg(): Specified filename is (null).", + cimglist_instance); + + const int cimg_iobuffer = 12*1024*1024; + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + bool loaded = false, endian = cimg::endianness(); + char tmp[256] = { 0 }, str_pixeltype[256] = { 0 }, str_endian[256] = { 0 }; + unsigned int j, err, N = 0, W, H, D, C, csiz; + int i; + do { + j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0; + } while (*tmp=='#' && i!=EOF); + err = std::sscanf(tmp,"%u%*c%255[A-Za-z_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype,str_endian); + if (err<2) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "load_cimg(): CImg header not found in file '%s'.", + cimglist_instance, + filename?filename:"(FILE*)"); + } + if (!cimg::strncasecmp("little",str_endian,6)) endian = false; + else if (!cimg::strncasecmp("big",str_endian,3)) endian = true; + assign(N); + _cimg_load_cimg_case("bool",bool); + _cimg_load_cimg_case("unsigned_char",unsigned char); + _cimg_load_cimg_case("uchar",unsigned char); + _cimg_load_cimg_case("char",char); + _cimg_load_cimg_case("unsigned_short",unsigned short); + _cimg_load_cimg_case("ushort",unsigned short); + _cimg_load_cimg_case("short",short); + _cimg_load_cimg_case("unsigned_int",unsigned int); + _cimg_load_cimg_case("uint",unsigned int); + _cimg_load_cimg_case("int",int); + _cimg_load_cimg_case("unsigned_long",unsigned long); + _cimg_load_cimg_case("ulong",unsigned long); + _cimg_load_cimg_case("long",long); + _cimg_load_cimg_case("float",float); + _cimg_load_cimg_case("double",double); + if (!loaded) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "load_cimg(): Unsupported pixel type '%s' for file '%s'.", + cimglist_instance, + str_pixeltype,filename?filename:"(FILE*)"); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load a sublist list from a (non compressed) .cimg file. + /** + \param filename Filename to read data from. + \param n0 Starting index of images to read (~0U for max). + \param n1 Ending index of images to read (~0U for max). + \param x0 Starting X-coordinates of image regions to read. + \param y0 Starting Y-coordinates of image regions to read. + \param z0 Starting Z-coordinates of image regions to read. + \param c0 Starting C-coordinates of image regions to read. + \param x1 Ending X-coordinates of image regions to read (~0U for max). + \param y1 Ending Y-coordinates of image regions to read (~0U for max). + \param z1 Ending Z-coordinates of image regions to read (~0U for max). + \param c1 Ending C-coordinates of image regions to read (~0U for max). + **/ + CImgList& load_cimg(const char *const filename, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1) { + return _load_cimg(0,filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); + } + + //! Load a sublist list from a (non compressed) .cimg file \newinstance. + static CImgList get_load_cimg(const char *const filename, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1) { + return CImgList().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); + } + + //! Load a sub-image list from a (non compressed) .cimg file \overloading. + CImgList& load_cimg(std::FILE *const file, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1) { + return _load_cimg(file,0,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); + } + + //! Load a sub-image list from a (non compressed) .cimg file \newinstance. + static CImgList get_load_cimg(std::FILE *const file, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1) { + return CImgList().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); + } + + CImgList& _load_cimg(std::FILE *const file, const char *const filename, + const unsigned int n0, const unsigned int n1, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0, + const unsigned int x1, const unsigned int y1, + const unsigned int z1, const unsigned int c1) { +#define _cimg_load_cimg_case2(Ts,Tss) \ + if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \ + for (unsigned int l = 0; l<=nn1; ++l) { \ + j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0) tmp[j++] = (char)i; tmp[j] = 0; \ + W = H = D = C = 0; \ + if (std::sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=4) \ + throw CImgIOException(_cimglist_instance \ + "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'", \ + cimglist_instance, \ + W,H,D,C,l,filename?filename:"(FILE*)"); \ + if (W*H*D*C>0) { \ + if (l=W || ny0>=H || nz0>=D || nc0>=C) std::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \ + else { \ + const unsigned int \ + _nx1 = nx1==~0U?W-1:nx1, \ + _ny1 = ny1==~0U?H-1:ny1, \ + _nz1 = nz1==~0U?D-1:nz1, \ + _nc1 = nc1==~0U?C-1:nc1; \ + if (_nx1>=W || _ny1>=H || _nz1>=D || _nc1>=C) \ + throw CImgArgumentException(_cimglist_instance \ + "load_cimg(): Invalid specified coordinates " \ + "[%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " \ + "because image [%u] in file '%s' has size (%u,%u,%u,%u).", \ + cimglist_instance, \ + n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,l,filename?filename:"(FILE*)",W,H,D,C); \ + CImg raw(1 + _nx1 - nx0); \ + CImg &img = _data[l - nn0]; \ + img.assign(1 + _nx1 - nx0,1 + _ny1 - ny0,1 + _nz1 - nz0,1 + _nc1 - nc0); \ + T *ptrd = img._data; \ + const unsigned int skipvb = nc0*W*H*D*sizeof(Tss); \ + if (skipvb) std::fseek(nfile,skipvb,SEEK_CUR); \ + for (unsigned int c = 1 + _nc1 - nc0; c; --c) { \ + const unsigned int skipzb = nz0*W*H*sizeof(Tss); \ + if (skipzb) std::fseek(nfile,skipzb,SEEK_CUR); \ + for (unsigned int z = 1 + _nz1 - nz0; z; --z) { \ + const unsigned int skipyb = ny0*W*sizeof(Tss); \ + if (skipyb) std::fseek(nfile,skipyb,SEEK_CUR); \ + for (unsigned int y = 1 + _ny1 - ny0; y; --y) { \ + const unsigned int skipxb = nx0*sizeof(Tss); \ + if (skipxb) std::fseek(nfile,skipxb,SEEK_CUR); \ + cimg::fread(raw._data,raw._width,nfile); \ + if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); \ + const Tss *ptrs = raw._data; \ + for (unsigned int off = raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \ + const unsigned int skipxe = (W-1-_nx1)*sizeof(Tss); \ + if (skipxe) std::fseek(nfile,skipxe,SEEK_CUR); \ + } \ + const unsigned int skipye = (H-1-_ny1)*W*sizeof(Tss); \ + if (skipye) std::fseek(nfile,skipye,SEEK_CUR); \ + } \ + const unsigned int skipze = (D-1-_nz1)*W*H*sizeof(Tss); \ + if (skipze) std::fseek(nfile,skipze,SEEK_CUR); \ + } \ + const unsigned int skipve = (C-1-_nc1)*W*H*D*sizeof(Tss); \ + if (skipve) std::fseek(nfile,skipve,SEEK_CUR); \ + } \ + } \ + } \ + loaded = true; \ + } + + if (!filename && !file) + throw CImgArgumentException(_cimglist_instance + "load_cimg(): Specified filename is (null).", + cimglist_instance); + unsigned int + nn0 = cimg::min(n0,n1), nn1 = cimg::max(n0,n1), + nx0 = cimg::min(x0,x1), nx1 = cimg::max(x0,x1), + ny0 = cimg::min(y0,y1), ny1 = cimg::max(y0,y1), + nz0 = cimg::min(z0,z1), nz1 = cimg::max(z0,z1), + nc0 = cimg::min(c0,c1), nc1 = cimg::max(c0,c1); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + bool loaded = false, endian = cimg::endianness(); + char tmp[256] = { 0 }, str_pixeltype[256] = { 0 }, str_endian[256] = { 0 }; + unsigned int j, err, N, W, H, D, C; + int i; + j = 0; while((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0; + err = std::sscanf(tmp,"%u%*c%255[A-Za-z_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype,str_endian); + if (err<2) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "load_cimg(): CImg header not found in file '%s'.", + cimglist_instance, + filename?filename:"(FILE*)"); + } + if (!cimg::strncasecmp("little",str_endian,6)) endian = false; + else if (!cimg::strncasecmp("big",str_endian,3)) endian = true; + nn1 = n1==~0U?N-1:n1; + if (nn1>=N) + throw CImgArgumentException(_cimglist_instance + "load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " + "because file '%s' contains only %u images.", + cimglist_instance, + n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:"(FILE*)",N); + assign(1+nn1-n0); + _cimg_load_cimg_case2("bool",bool); + _cimg_load_cimg_case2("unsigned_char",unsigned char); + _cimg_load_cimg_case2("uchar",unsigned char); + _cimg_load_cimg_case2("char",char); + _cimg_load_cimg_case2("unsigned_short",unsigned short); + _cimg_load_cimg_case2("ushort",unsigned short); + _cimg_load_cimg_case2("short",short); + _cimg_load_cimg_case2("unsigned_int",unsigned int); + _cimg_load_cimg_case2("uint",unsigned int); + _cimg_load_cimg_case2("int",int); + _cimg_load_cimg_case2("unsigned_long",unsigned long); + _cimg_load_cimg_case2("ulong",unsigned long); + _cimg_load_cimg_case2("long",long); + _cimg_load_cimg_case2("float",float); + _cimg_load_cimg_case2("double",double); + if (!loaded) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "load_cimg(): Unsupported pixel type '%s' for file '%s'.", + cimglist_instance, + str_pixeltype,filename?filename:"(FILE*)"); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load a list from a PAR/REC (Philips) file. + /** + \param filename Filename to read data from. + **/ + CImgList& load_parrec(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "load_parrec(): Specified filename is (null).", + cimglist_instance); + + char body[1024] = { 0 }, filenamepar[1024] = { 0 }, filenamerec[1024] = { 0 }; + const char *const ext = cimg::split_filename(filename,body); + if (!std::strcmp(ext,"par")) { + std::strncpy(filenamepar,filename,sizeof(filenamepar)-1); + cimg_snprintf(filenamerec,sizeof(filenamerec),"%s.rec",body); + } + if (!std::strcmp(ext,"PAR")) { + std::strncpy(filenamepar,filename,sizeof(filenamepar)-1); + cimg_snprintf(filenamerec,sizeof(filenamerec),"%s.REC",body); + } + if (!std::strcmp(ext,"rec")) { + std::strncpy(filenamerec,filename,sizeof(filenamerec)-1); + cimg_snprintf(filenamepar,sizeof(filenamepar),"%s.par",body); + } + if (!std::strcmp(ext,"REC")) { + std::strncpy(filenamerec,filename,sizeof(filenamerec)-1); + cimg_snprintf(filenamepar,sizeof(filenamepar),"%s.PAR",body); + } + std::FILE *file = cimg::fopen(filenamepar,"r"); + + // Parse header file + CImgList st_slices; + CImgList st_global; + int err; + char line[256] = { 0 }; + do { err=std::fscanf(file,"%255[^\n]%*c",line); } while (err!=EOF && (*line=='#' || *line=='.')); + do { + unsigned int sn,size_x,size_y,pixsize; + float rs,ri,ss; + err = std::fscanf(file,"%u%*u%*u%*u%*u%*u%*u%u%*u%u%u%g%g%g%*[^\n]",&sn,&pixsize,&size_x,&size_y,&ri,&rs,&ss); + if (err==7) { + CImg::vector((float)sn,(float)pixsize,(float)size_x,(float)size_y,ri,rs,ss,0).move_to(st_slices); + unsigned int i; for (i = 0; i::vector(size_x,size_y,sn).move_to(st_global); + else { + CImg &vec = st_global[i]; + if (size_x>vec[0]) vec[0] = size_x; + if (size_y>vec[1]) vec[1] = size_y; + vec[2] = sn; + } + st_slices[st_slices._width-1][7] = (float)i; + } + } while (err==7); + + // Read data + std::FILE *file2 = cimg::fopen(filenamerec,"rb"); + cimglist_for(st_global,l) { + const CImg& vec = st_global[l]; + CImg(vec[0],vec[1],vec[2]).move_to(*this); + } + + cimglist_for(st_slices,l) { + const CImg& vec = st_slices[l]; + const unsigned int + sn = (unsigned int)vec[0] - 1, + pixsize = (unsigned int)vec[1], + size_x = (unsigned int)vec[2], + size_y = (unsigned int)vec[3], + imn = (unsigned int)vec[7]; + const float ri = vec[4], rs = vec[5], ss = vec[6]; + switch (pixsize) { + case 8 : { + CImg buf(size_x,size_y); + cimg::fread(buf._data,size_x*size_y,file2); + if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y); + CImg& img = (*this)[imn]; + cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss)); + } break; + case 16 : { + CImg buf(size_x,size_y); + cimg::fread(buf._data,size_x*size_y,file2); + if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y); + CImg& img = (*this)[imn]; + cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss)); + } break; + case 32 : { + CImg buf(size_x,size_y); + cimg::fread(buf._data,size_x*size_y,file2); + if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y); + CImg& img = (*this)[imn]; + cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss)); + } break; + default : + cimg::fclose(file); + cimg::fclose(file2); + throw CImgIOException(_cimglist_instance + "load_parrec(): Unsupported %d-bits pixel type for file '%s'.", + cimglist_instance, + pixsize,filename); + } + } + cimg::fclose(file); + cimg::fclose(file2); + if (!_width) + throw CImgIOException(_cimglist_instance + "load_parrec(): Failed to recognize valid PAR-REC data in file '%s'.", + cimglist_instance, + filename); + return *this; + } + + //! Load a list from a PAR/REC (Philips) file \newinstance. + static CImgList get_load_parrec(const char *const filename) { + return CImgList().load_parrec(filename); + } + + //! Load a list from a YUV image sequence file. + /** + \param filename Filename to read data from. + \param size_x Width of the images. + \param size_y Height of the images. + \param first_frame Index of first image frame to read. + \param last_frame Index of last image frame to read. + \param step_frame Step applied between each frame. + \param yuv2rgb Apply YUV to RGB transformation during reading. + **/ + CImgList& load_yuv(const char *const filename, + const unsigned int size_x, const unsigned int size_y, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true) { + return _load_yuv(0,filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); + } + + //! Load a list from a YUV image sequence file \newinstance. + static CImgList get_load_yuv(const char *const filename, + const unsigned int size_x, const unsigned int size_y=1, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true) { + return CImgList().load_yuv(filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); + } + + //! Load a list from an image sequence YUV file \overloading. + CImgList& load_yuv(std::FILE *const file, + const unsigned int size_x, const unsigned int size_y, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true) { + return _load_yuv(file,0,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); + } + + //! Load a list from an image sequence YUV file \newinstance. + static CImgList get_load_yuv(std::FILE *const file, + const unsigned int size_x, const unsigned int size_y=1, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool yuv2rgb=true) { + return CImgList().load_yuv(file,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); + } + + CImgList& _load_yuv(std::FILE *const file, const char *const filename, + const unsigned int size_x, const unsigned int size_y, + const unsigned int first_frame, const unsigned int last_frame, + const unsigned int step_frame, const bool yuv2rgb) { + if (!filename && !file) + throw CImgArgumentException(_cimglist_instance + "load_yuv(): Specified filename is (null).", + cimglist_instance); + if (size_x%2 || size_y%2) + throw CImgArgumentException(_cimglist_instance + "load_yuv(): Invalid odd XY dimensions %ux%u in file '%s'.", + cimglist_instance, + size_x,size_y,filename?filename:"(FILE*)"); + if (!size_x || !size_y) + throw CImgArgumentException(_cimglist_instance + "load_yuv(): Invalid sequence size (%u,%u) in file '%s'.", + cimglist_instance, + size_x,size_y,filename?filename:"(FILE*)"); + + const unsigned int + nfirst_frame = first_frame tmp(size_x,size_y,1,3), UV(size_x/2,size_y/2,1,2); + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); + bool stop_flag = false; + int err; + if (nfirst_frame) { + err = std::fseek(nfile,nfirst_frame*(size_x*size_y + size_x*size_y/2),SEEK_CUR); + if (err) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "load_yuv(): File '%s' doesn't contain frame number %u.", + cimglist_instance, + filename?filename:"(FILE*)",nfirst_frame); + } + } + unsigned int frame; + for (frame = nfirst_frame; !stop_flag && frame<=nlast_frame; frame+=nstep_frame) { + tmp.fill(0); + // *TRY* to read the luminance part, do not replace by cimg::fread! + err = (int)std::fread((void*)(tmp._data),1,(unsigned long)tmp._width*tmp._height,nfile); + if (err!=(int)(tmp._width*tmp._height)) { + stop_flag = true; + if (err>0) + cimg::warn(_cimglist_instance + "load_yuv(): File '%s' contains incomplete data or given image dimensions " + "(%u,%u) are incorrect.", + cimglist_instance, + filename?filename:"(FILE*)",size_x,size_y); + } else { + UV.fill(0); + // *TRY* to read the luminance part, do not replace by cimg::fread! + err = (int)std::fread((void*)(UV._data),1,(size_t)(UV.size()),nfile); + if (err!=(int)(UV.size())) { + stop_flag = true; + if (err>0) + cimg::warn(_cimglist_instance + "load_yuv(): File '%s' contains incomplete data or given image dimensions (%u,%u) " + "are incorrect.", + cimglist_instance, + filename?filename:"(FILE*)",size_x,size_y); + } else { + cimg_forXY(UV,x,y) { + const int x2 = x*2, y2 = y*2; + tmp(x2,y2,1) = tmp(x2+1,y2,1) = tmp(x2,y2+1,1) = tmp(x2+1,y2+1,1) = UV(x,y,0); + tmp(x2,y2,2) = tmp(x2+1,y2,2) = tmp(x2,y2+1,2) = tmp(x2+1,y2+1,2) = UV(x,y,1); + } + if (yuv2rgb) tmp.YCbCrtoRGB(); + insert(tmp); + if (nstep_frame>1) std::fseek(nfile,(nstep_frame-1)*(size_x*size_y + size_x*size_y/2),SEEK_CUR); + } + } + } + if (stop_flag && nlast_frame!=~0U && frame!=nlast_frame) + cimg::warn(_cimglist_instance + "load_yuv(): Frame %d not reached since only %u frames were found in file '%s'.", + cimglist_instance, + nlast_frame,frame-1,filename?filename:"(FILE*)"); + + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Load an image from a video file, using ffmpeg libraries. + /** + \param filename Filename, as a C-string. + \param first_frame Index of the first frame to read. + \param last_frame Index of the last frame to read. + \param step_frame Step value for frame reading. + \param pixel_format To be documented. + \param resume To be documented. + **/ + // This piece of code has been firstly created by David Starweather (starkdg(at)users(dot)sourceforge(dot)net) + // I modified it afterwards for direct inclusion in the library core. + CImgList& load_ffmpeg(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool pixel_format=true, const bool resume=false) { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "load_ffmpeg(): Specified filename is (null).", + cimglist_instance); + + const unsigned int + nfirst_frame = first_frame1) || (resume && (pixel_format || !pixel_format))) + throw CImgArgumentException(_cimglist_instance + "load_ffmpeg(): Unable to load sub-frames from file '%s' unless libffmpeg " + "is enabled.", + cimglist_instance, + filename); + + return load_ffmpeg_external(filename); +#else + const PixelFormat ffmpeg_pixfmt = pixel_format?PIX_FMT_RGB24:PIX_FMT_GRAY8; + avcodec_register_all(); + av_register_all(); + static AVFormatContext *format_ctx = 0; + static AVCodecContext *codec_ctx = 0; + static AVCodec *codec = 0; + static AVFrame *avframe = avcodec_alloc_frame(), *converted_frame = avcodec_alloc_frame(); + static int vstream = 0; + + if (resume) { + if (!format_ctx || !codec_ctx || !codec || !avframe || !converted_frame) + throw CImgArgumentException(_cimglist_instance + "load_ffmpeg(): Failed to resume loading of file '%s', " + "due to unallocated FFMPEG structures.", + cimglist_instance, + filename); + } else { + // Open video file, find main video stream and codec. + if (format_ctx) avformat_close_input(&format_ctx); + if (avformat_open_input(&format_ctx,filename,0,0)!=0) + throw CImgIOException(_cimglist_instance + "load_ffmpeg(): Failed to open file '%s'.", + cimglist_instance, + filename); + + if (!avframe || !converted_frame || avformat_find_stream_info(format_ctx,NULL)<0) { + avformat_close_input(&format_ctx); format_ctx = 0; + return load_ffmpeg_external(filename); + } +#if cimg_verbosity>=3 + dump_format(format_ctx,0,0,0); +#endif + + // Special command: Return informations on main video stream. + // as a vector 1x4 containing: (nb_frames,width,height,fps). + if (!first_frame && !last_frame && !step_frame) { + for (vstream = 0; vstream<(int)(format_ctx->nb_streams); ++vstream) + if (format_ctx->streams[vstream]->codec->codec_type==AVMEDIA_TYPE_VIDEO) break; + if (vstream==(int)format_ctx->nb_streams) assign(); + else { + CImgList timestamps; + int nb_frames; + AVPacket packet; + // Count frames and store timestamps. + for (nb_frames = 0; av_read_frame(format_ctx,&packet)>=0; av_free_packet(&packet)) + if (packet.stream_index==vstream) { + CImg::vector((double)packet.pts).move_to(timestamps); + ++nb_frames; + } + // Get frame with, height and fps. + const int + framew = format_ctx->streams[vstream]->codec->width, + frameh = format_ctx->streams[vstream]->codec->height; + const float + num = (float)(format_ctx->streams[vstream]->r_frame_rate).num, + den = (float)(format_ctx->streams[vstream]->r_frame_rate).den, + fps = num/den; + // Return infos as a list. + assign(2); + (*this)[0].assign(1,4).fill((T)nb_frames,(T)framew,(T)frameh,(T)fps); + (*this)[1] = (timestamps>'y'); + } + avformat_close_input(&format_ctx); format_ctx = 0; + return *this; + } + + for (vstream = 0; vstream<(int)(format_ctx->nb_streams) && + format_ctx->streams[vstream]->codec->codec_type!=AVMEDIA_TYPE_VIDEO; ) ++vstream; + if (vstream==(int)format_ctx->nb_streams) { + avformat_close_input(&format_ctx); format_ctx = 0; + return load_ffmpeg_external(filename); + } + codec_ctx = format_ctx->streams[vstream]->codec; + codec = avcodec_find_decoder(codec_ctx->codec_id); + if (!codec) { + return load_ffmpeg_external(filename); + } + if (avcodec_open2(codec_ctx,codec,NULL)<0) { // Open codec + return load_ffmpeg_external(filename); + } + } + + // Read video frames + const unsigned int numBytes = avpicture_get_size(ffmpeg_pixfmt,codec_ctx->width,codec_ctx->height); + uint8_t *const buffer = new uint8_t[numBytes]; + avpicture_fill((AVPicture *)converted_frame,buffer,ffmpeg_pixfmt,codec_ctx->width,codec_ctx->height); + const T foo = (T)0; + AVPacket packet; + for (unsigned int frame = 0, next_frame = nfirst_frame; + frame<=nlast_frame && av_read_frame(format_ctx,&packet)>=0; ) { + if (packet.stream_index==(int)vstream) { + int decoded = 0; +#if defined(AV_VERSION_INT) +#if LIBAVCODEC_VERSION_INTwidth,codec_ctx->height,codec_ctx->pix_fmt,codec_ctx->width, + codec_ctx->height,ffmpeg_pixfmt,1,0,0,0); + sws_scale(c,avframe->data,avframe->linesize,0,codec_ctx->height, + converted_frame->data,converted_frame->linesize); + if (ffmpeg_pixfmt==PIX_FMT_RGB24) { + CImg next_image(*converted_frame->data,3,codec_ctx->width,codec_ctx->height,1,true); + next_image._get_permute_axes("yzcx",foo).move_to(*this); + } else { + CImg next_image(*converted_frame->data,1,codec_ctx->width,codec_ctx->height,1,true); + next_image._get_permute_axes("yzcx",foo).move_to(*this); + } + next_frame+=nstep_frame; + } + ++frame; + } + av_free_packet(&packet); + if (next_frame>nlast_frame) break; + } + } + delete[] buffer; + return *this; +#endif + } + + //! Load an image from a video file, using ffmpeg libraries \newinstance. + static CImgList get_load_ffmpeg(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, const bool pixel_format=true) { + return CImgList().load_ffmpeg(filename,first_frame,last_frame,step_frame,pixel_format); + } + + //! Load an image from a video file using the external tool 'ffmpeg'. + /** + \param filename Filename to read data from. + **/ + CImgList& load_ffmpeg_external(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "load_ffmpeg_external(): Specified filename is (null).", + cimglist_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; + std::FILE *file = 0; + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_000001.ppm",filetmp); + if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%%6d.ppm",filetmp); +#if cimg_OS!=2 + cimg_snprintf(command,sizeof(command),"%s -i \"%s\" \"%s\" >/dev/null 2>&1", + cimg::ffmpeg_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp2)._system_strescape().data()); +#else + cimg_snprintf(command,sizeof(command),"\"%s -i \"%s\" \"%s\"\" >NUL 2>&1", + cimg::ffmpeg_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp2)._system_strescape().data()); +#endif + cimg::system(command,0); + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + assign(); + unsigned int i = 1; + for (bool stop_flag = false; !stop_flag; ++i) { + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%.6u.ppm",filetmp,i); + CImg img; + try { img.load_pnm(filetmp2); } + catch (CImgException&) { stop_flag = true; } + if (img) { img.move_to(*this); std::remove(filetmp2); } + } + cimg::exception_mode() = omode; + if (is_empty()) + throw CImgIOException(_cimglist_instance + "load_ffmpeg_external(): Failed to open file '%s' with external command 'ffmpeg'.", + cimglist_instance, + filename); + return *this; + } + + //! Load an image from a video file using the external tool 'ffmpeg' \newinstance. + static CImgList get_load_ffmpeg_external(const char *const filename) { + return CImgList().load_ffmpeg_external(filename); + } + + //! Load gif file, using ImageMagick or GraphicsMagick's external tools. + /** + \param filename Filename to read data from. + \param use_graphicsmagick Tells if GraphicsMagick's tool 'gm' is used instead of ImageMagick's tool 'convert'. + **/ + CImgList& load_gif_external(const char *const filename) { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "load_gif_external(): Specified filename is (null).", + cimglist_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + if (!_load_gif_external(filename,false)) + if (!_load_gif_external(filename,true)) + try { assign(CImg().load_other(filename)); } catch (CImgException&) { assign(); } + if (is_empty()) + throw CImgIOException(_cimglist_instance + "load_gif_external(): Failed to open file '%s'.", + cimglist_instance,filename); + return *this; + } + + CImgList& _load_gif_external(const char *const filename, const bool use_graphicsmagick=false) { + char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; + std::FILE *file = 0; + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + if (use_graphicsmagick) cimg_snprintf(filetmp2,sizeof(filetmp2),"%s.png.0",filetmp); + else cimg_snprintf(filetmp2,sizeof(filetmp2),"%s-0.png",filetmp); + if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); + } while (file); +#if cimg_OS!=2 + if (use_graphicsmagick) cimg_snprintf(command,sizeof(command),"%s convert \"%s\" \"%s.png\" >/dev/null 2>&1", + cimg::graphicsmagick_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); + else cimg_snprintf(command,sizeof(command),"%s \"%s\" \"%s.png\" >/dev/null 2>&1", + cimg::imagemagick_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); +#else + if (use_graphicsmagick) cimg_snprintf(command,sizeof(command),"\"%s convert \"%s\" \"%s.png\"\" >NUL 2>&1", + cimg::graphicsmagick_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); + else cimg_snprintf(command,sizeof(command),"\"%s \"%s\" \"%s.png\"\" >NUL 2>&1", + cimg::imagemagick_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); +#endif + cimg::system(command,0); + const unsigned int omode = cimg::exception_mode(); + cimg::exception_mode() = 0; + assign(); + + // Try to read a single frame gif. + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s.png",filetmp); + CImg img; + try { img.load_png(filetmp2); } + catch (CImgException&) { } + if (img) { img.move_to(*this); std::remove(filetmp2); } + else { // Try to read animated gif. + unsigned int i = 0; + for (bool stop_flag = false; !stop_flag; ++i) { + if (use_graphicsmagick) cimg_snprintf(filetmp2,sizeof(filetmp2),"%s.png.%u",filetmp,i); + else cimg_snprintf(filetmp2,sizeof(filetmp2),"%s-%u.png",filetmp,i); + CImg img; + try { img.load_png(filetmp2); } + catch (CImgException&) { stop_flag = true; } + if (img) { img.move_to(*this); std::remove(filetmp2); } + } + } + cimg::exception_mode() = omode; + return *this; + } + + //! Load gif file, using ImageMagick or GraphicsMagick's external tools \newinstance. + static CImgList get_load_gif_external(const char *const filename) { + return CImgList().load_gif_external(filename); + } + + //! Load a gzipped list, using external tool 'gunzip'. + /** + \param filename Filename to read data from. + **/ + CImgList& load_gzip_external(const char *const filename) { + if (!filename) + throw CImgIOException(_cimglist_instance + "load_gzip_external(): Specified filename is (null).", + cimglist_instance); + std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. + char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; + const char + *ext = cimg::split_filename(filename,body), + *ext2 = cimg::split_filename(body,0); + std::FILE *file = 0; + do { + if (!cimg::strcasecmp(ext,"gz")) { + if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } else { + if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", + cimg::gunzip_path(), + CImg::string(filename)._system_strescape().data(), + CImg::string(filetmp)._system_strescape().data()); + cimg::system(command); + if (!(file = std::fopen(filetmp,"rb"))) { + cimg::fclose(cimg::fopen(filename,"r")); + throw CImgIOException(_cimglist_instance + "load_gzip_external(): Failed to open file '%s'.", + cimglist_instance, + filename); + + } else cimg::fclose(file); + load(filetmp); + std::remove(filetmp); + return *this; + } + + //! Load a gzipped list, using external tool 'gunzip' \newinstance. + static CImgList get_load_gzip_external(const char *const filename) { + return CImgList().load_gzip_external(filename); + } + + //! Load a 3d object from a .OFF file. + /** + \param filename Filename to read data from. + \param[out] primitives At return, contains the list of 3d object primitives. + \param[out] colors At return, contains the list of 3d object colors. + \return List of 3d object vertices. + **/ + template + CImgList& load_off(const char *const filename, + CImgList& primitives, CImgList& colors) { + return get_load_off(filename,primitives,colors).move_to(*this); + } + + //! Load a 3d object from a .OFF file \newinstance. + template + static CImgList get_load_off(const char *const filename, + CImgList& primitives, CImgList& colors) { + return CImg().load_off(filename,primitives,colors)<'x'; + } + + //! Load images from a TIFF file. + /** + \param filename Filename to read data from. + \param first_frame Index of first image frame to read. + \param last_frame Index of last image frame to read. + \param step_frame Step applied between each frame. + **/ + CImgList& load_tiff(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, + float *const voxel_size=0) { + const unsigned int + nfirst_frame = first_frame::get_load_tiff(filename)); +#else + TIFF *tif = TIFFOpen(filename,"r"); + if (tif) { + unsigned int nb_images = 0; + do ++nb_images; while (TIFFReadDirectory(tif)); + if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images)) + cimg::warn(_cimglist_instance + "load_tiff(): Invalid specified frame range is [%u,%u] (step %u) since " + "file '%s' contains %u image(s).", + cimglist_instance, + nfirst_frame,nlast_frame,nstep_frame,filename,nb_images); + + if (nfirst_frame>=nb_images) return assign(); + if (nlast_frame>=nb_images) nlast_frame = nb_images-1; + assign(1+(nlast_frame-nfirst_frame)/nstep_frame); + TIFFSetDirectory(tif,0); +#if cimg_verbosity>=3 + TIFFSetWarningHandler(0); + TIFFSetErrorHandler(0); +#endif + cimglist_for(*this,l) _data[l]._load_tiff(tif,nfirst_frame + l*nstep_frame,voxel_size); + TIFFClose(tif); + } else throw CImgIOException(_cimglist_instance + "load_tiff(): Failed to open file '%s'.", + cimglist_instance, + filename); + return *this; +#endif + } + + //! Load a multi-page TIFF file \newinstance. + static CImgList get_load_tiff(const char *const filename, + const unsigned int first_frame=0, const unsigned int last_frame=~0U, + const unsigned int step_frame=1, + float *const voxel_size=0) { + return CImgList().load_tiff(filename,first_frame,last_frame,step_frame,voxel_size); + } + + //@} + //---------------------------------- + // + //! \name Data Output + //@{ + //---------------------------------- + + //! Print informations about the list on the standard output. + /** + \param title Label set to the informations displayed. + \param display_stats Tells if image statistics must be computed and displayed. + **/ + const CImgList& print(const char *const title=0, const bool display_stats=true) const { + unsigned int msiz = 0; + cimglist_for(*this,l) msiz+=_data[l].size(); + msiz*=sizeof(T); + const unsigned int mdisp = msiz<8*1024?0:(msiz<8*1024*1024?1:2); + char _title[64] = { 0 }; + if (!title) cimg_snprintf(_title,sizeof(_title),"CImgList<%s>",pixel_type()); + std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = %u/%u [%u %s], %sdata%s = (CImg<%s>*)%p", + cimg::t_magenta,cimg::t_bold,title?title:_title,cimg::t_normal, + cimg::t_bold,cimg::t_normal,(void*)this, + cimg::t_bold,cimg::t_normal,_width,_allocated_width, + mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20)), + mdisp==0?"b":(mdisp==1?"Kio":"Mio"), + cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin()); + if (_data) std::fprintf(cimg::output(),"..%p.\n",(void*)((char*)end()-1)); + else std::fprintf(cimg::output(),".\n"); + + char tmp[16] = { 0 }; + cimglist_for(*this,ll) { + cimg_snprintf(tmp,sizeof(tmp),"[%d]",ll); + std::fprintf(cimg::output()," "); + _data[ll].print(tmp,display_stats); + if (ll==3 && _width>8) { ll = _width-5; std::fprintf(cimg::output()," ...\n"); } + } + std::fflush(cimg::output()); + return *this; + } + + //! Display the current CImgList instance in an existing CImgDisplay window (by reference). + /** + \param disp Reference to an existing CImgDisplay instance, where the current image list will be displayed. + \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignmenet. + \note This function displays the list images of the current CImgList instance into an existing + CImgDisplay window. + Images of the list are appended in a single temporarly image for visualization purposes. + The function returns immediately. + **/ + const CImgList& display(CImgDisplay &disp, const char axis='x', const float align=0) const { + disp.display(*this,axis,align); + return *this; + } + + //! Display the current CImgList instance in a new display window. + /** + \param disp Display window. + \param display_info Tells if image informations are displayed on the standard output. + \param axis Alignment axis for images viewing. + \param align Apending alignment. + \note This function opens a new window with a specific title and displays the list images of the + current CImgList instance into it. + Images of the list are appended in a single temporarly image for visualization purposes. + The function returns when a key is pressed or the display window is closed by the user. + **/ + const CImgList& display(CImgDisplay &disp, const bool display_info, + const char axis='x', const float align=0, + unsigned int *const XYZ=0) const { + bool is_exit = false; + return _display(disp,0,display_info,axis,align,XYZ,0,true,is_exit); + } + + //! Display the current CImgList instance in a new display window. + /** + \param title Title of the opening display window. + \param display_info Tells if list informations must be written on standard output. + \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. + \param align Appending alignment. + **/ + const CImgList& display(const char *const title=0, const bool display_info=true, + const char axis='x', const float align=0, + unsigned int *const XYZ=0) const { + CImgDisplay disp; + bool is_exit = false; + return _display(disp,title,display_info,axis,align,XYZ,0,true,is_exit); + } + + const CImgList& _display(CImgDisplay &disp, const char *const title, const bool display_info, + const char axis, const float align, unsigned int *const XYZ, + const unsigned int orig, const bool is_first_call, bool &is_exit) const { + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "display(): Empty instance.", + cimglist_instance); + if (!disp) { + if (axis=='x') { + unsigned int sum_width = 0, max_height = 0; + cimglist_for(*this,l) { + const CImg &img = _data[l]; + const unsigned int + w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false), + h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true); + sum_width+=w; + if (h>max_height) max_height = h; + } + disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:0,1); + } else { + unsigned int max_width = 0, sum_height = 0; + cimglist_for(*this,l) { + const CImg &img = _data[l]; + const unsigned int + w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false), + h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true); + if (w>max_width) max_width = w; + sum_height+=h; + } + disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:0,1); + } + if (!title) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width); + } else if (title) disp.set_title("%s",title); + const CImg dtitle = CImg::string(disp.title()); + if (display_info) print(disp.title()); + disp.show().flush(); + + if (_width==1) { + const unsigned int dw = disp._width, dh = disp._height; + if (!is_first_call) + disp.resize(cimg_fitscreen(_data[0]._width,_data[0]._height,_data[0]._depth),false). + set_title("%s (%ux%ux%ux%u)", + dtitle.data(),_data[0]._width,_data[0]._height,_data[0]._depth,_data[0]._spectrum); + _data[0]._display(disp,0,false,XYZ,!is_first_call); + if (disp.key()) is_exit = true; + disp.resize(cimg_fitscreen(dw,dh,1),false).set_title("%s",dtitle.data()); + } else { + bool disp_resize = !is_first_call; + while (!disp.is_closed() && !is_exit) { + const CImg s = _get_select(disp,0,true,axis,align,orig,disp_resize,!is_first_call,true); + disp_resize = true; + if (s[0]<0) { // No selections done. + if (disp.button()&2) { disp.flush(); break; } + is_exit = true; + } else if (disp.wheel()) { // Zoom in/out. + const int wheel = disp.wheel(); + disp.set_wheel(); + if (!is_first_call && wheel<0) break; + if (wheel>0 && _width>=4) { + const unsigned int + delta = cimg::max(1U,(unsigned int)cimg::round(0.3*_width)), + ind0 = (unsigned int)cimg::max(0,s[0] - (int)delta), + ind1 = (unsigned int)cimg::min(width() - 1,s[0] + (int)delta); + if ((ind0!=0 || ind1!=_width-1) && ind1 - ind0>=3) + get_shared_images(ind0,ind1)._display(disp,0,false,axis,align,XYZ,orig + ind0,false,is_exit); + } + } else if (s[0]!=0 || s[1]!=width()-1) + get_shared_images(s[0],s[1])._display(disp,0,false,axis,align,XYZ,orig+s[0],false,is_exit); + } + } + return *this; + } + + //! Save list into a file. + /** + \param filename Filename to write data to. + \param number When positive, represents an index added to the filename. Otherwise, no number is added. + \param digits Number of digits used for adding the number to the filename. + **/ + const CImgList& save(const char *const filename, const int number=-1, const unsigned int digits=6) const { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "save(): Specified filename is (null).", + cimglist_instance); + // Do not test for empty instances, since .cimg format is able to manage empty instances. + const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.'); + const char *const ext = cimg::split_filename(filename); + char nfilename[1024] = { 0 }; + const char *const fn = is_stdout?filename:number>=0?cimg::number_filename(filename,number,digits,nfilename): + filename; + +#ifdef cimglist_save_plugin + cimglist_save_plugin(fn); +#endif +#ifdef cimglist_save_plugin1 + cimglist_save_plugin1(fn); +#endif +#ifdef cimglist_save_plugin2 + cimglist_save_plugin2(fn); +#endif +#ifdef cimglist_save_plugin3 + cimglist_save_plugin3(fn); +#endif +#ifdef cimglist_save_plugin4 + cimglist_save_plugin4(fn); +#endif +#ifdef cimglist_save_plugin5 + cimglist_save_plugin5(fn); +#endif +#ifdef cimglist_save_plugin6 + cimglist_save_plugin6(fn); +#endif +#ifdef cimglist_save_plugin7 + cimglist_save_plugin7(fn); +#endif +#ifdef cimglist_save_plugin8 + cimglist_save_plugin8(fn); +#endif + if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true); + else if (!cimg::strcasecmp(ext,"cimg") || !*ext) return save_cimg(fn,false); + else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,true); + else if (!cimg::strcasecmp(ext,"avi") || + !cimg::strcasecmp(ext,"mov") || + !cimg::strcasecmp(ext,"asf") || + !cimg::strcasecmp(ext,"divx") || + !cimg::strcasecmp(ext,"flv") || + !cimg::strcasecmp(ext,"mpg") || + !cimg::strcasecmp(ext,"m1v") || + !cimg::strcasecmp(ext,"m2v") || + !cimg::strcasecmp(ext,"m4v") || + !cimg::strcasecmp(ext,"mjp") || + !cimg::strcasecmp(ext,"mp4") || + !cimg::strcasecmp(ext,"mkv") || + !cimg::strcasecmp(ext,"mpe") || + !cimg::strcasecmp(ext,"movie") || + !cimg::strcasecmp(ext,"ogm") || + !cimg::strcasecmp(ext,"ogg") || + !cimg::strcasecmp(ext,"ogv") || + !cimg::strcasecmp(ext,"qt") || + !cimg::strcasecmp(ext,"rm") || + !cimg::strcasecmp(ext,"vob") || + !cimg::strcasecmp(ext,"wmv") || + !cimg::strcasecmp(ext,"xvid") || + !cimg::strcasecmp(ext,"mpeg")) return save_ffmpeg(fn); +#ifdef cimg_use_tiff + else if (!cimg::strcasecmp(ext,"tif") || + !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn); +#endif + else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn); + else { + if (_width==1) _data[0].save(fn,-1); + else cimglist_for(*this,l) { _data[l].save(fn,is_stdout?-1:l); if (is_stdout) std::fputc(EOF,stdout); } + } + return *this; + } + + //! Tell if an image list can be saved as one single file. + /** + \param filename Filename, as a C-string. + \return \c true if the file format supports multiple images, \c false otherwise. + **/ + static bool is_saveable(const char *const filename) { + const char *const ext = cimg::split_filename(filename); + if (!cimg::strcasecmp(ext,"cimgz") || +#ifdef cimg_use_tiff + !cimg::strcasecmp(ext,"tif") || + !cimg::strcasecmp(ext,"tiff") || +#endif + !cimg::strcasecmp(ext,"yuv") || + !cimg::strcasecmp(ext,"avi") || + !cimg::strcasecmp(ext,"mov") || + !cimg::strcasecmp(ext,"asf") || + !cimg::strcasecmp(ext,"divx") || + !cimg::strcasecmp(ext,"flv") || + !cimg::strcasecmp(ext,"mpg") || + !cimg::strcasecmp(ext,"m1v") || + !cimg::strcasecmp(ext,"m2v") || + !cimg::strcasecmp(ext,"m4v") || + !cimg::strcasecmp(ext,"mjp") || + !cimg::strcasecmp(ext,"mp4") || + !cimg::strcasecmp(ext,"mkv") || + !cimg::strcasecmp(ext,"mpe") || + !cimg::strcasecmp(ext,"movie") || + !cimg::strcasecmp(ext,"ogm") || + !cimg::strcasecmp(ext,"ogg") || + !cimg::strcasecmp(ext,"ogv") || + !cimg::strcasecmp(ext,"qt") || + !cimg::strcasecmp(ext,"rm") || + !cimg::strcasecmp(ext,"vob") || + !cimg::strcasecmp(ext,"wmv") || + !cimg::strcasecmp(ext,"xvid") || + !cimg::strcasecmp(ext,"mpeg")) return true; + return false; + } + + //! Save image sequence as a GIF animated file. + /** + \param filename Filename to write data to. + \param fps Number of desired frames per second. + \param nb_loops Number of loops (\c 0 for infinite looping). + **/ + const CImgList& save_gif_external(const char *const filename, const unsigned int fps=25, + const unsigned int nb_loops=0) { + char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; + CImgList filenames; + std::FILE *file = 0; + +#ifdef cimg_use_png +#define _cimg_save_gif_ext "png" +#else +#define _cimg_save_gif_ext "ppm" +#endif + + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_000001." _cimg_save_gif_ext,filetmp); + if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); + } while (file); + cimglist_for(*this,l) { + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%.6u." _cimg_save_gif_ext,filetmp,l+1); + CImg::string(filetmp2).move_to(filenames); + if (_data[l]._depth>1 || _data[l]._spectrum!=3) _data[l].get_resize(-100,-100,1,3).save(filetmp2); + else _data[l].save(filetmp2); + } + +#if cimg_OS!=2 + cimg_snprintf(command,sizeof(command),"%s -delay 1x%u -loop %u", + cimg::imagemagick_path(),fps,nb_loops); + CImg::string(command).move_to(filenames,0); + cimg_snprintf(command,sizeof(command),"\"%s\" >/dev/null 2>&1", + CImg::string(filename)._system_strescape().data()); + CImg::string(command).move_to(filenames); +#else + cimg_snprintf(command,sizeof(command),"\"%s -delay 1x%u -loop %u", + cimg::imagemagick_path(),fps,nb_loops); + CImg::string(command).move_to(filenames,0); + cimg_snprintf(command,sizeof(command),"\"%s\"\" >NUL 2>&1", + CImg::string(filename)._system_strescape().data()); + CImg::string(command).move_to(filenames); +#endif + CImg _command = filenames>'x'; + cimg_for(_command,p,char) if (!*p) *p = ' '; + _command.back() = 0; + + cimg::system(_command); + file = std::fopen(filename,"rb"); + if (!file) + throw CImgIOException(_cimglist_instance + "save_gif_external(): Failed to save file '%s' with external command 'convert'.", + cimglist_instance, + filename); + else cimg::fclose(file); + cimglist_for_in(*this,1,filenames._width-1,l) std::remove(filenames[l]); + return *this; + } + + //! Save image sequence, using FFMPEG library. + /** + \param filename Filename to write data to. + \param fps Desired framerate (in frames per seconds) if chosen format supports it. + \param bitrate Desired bitrate (in bits per seconds) if chosen format supports it. + **/ + // This piece of code has been originally written by David. G. Starkweather. + const CImgList& save_ffmpeg(const char *const filename, const unsigned int fps=25, + const unsigned int bitrate=2048) const { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "save_ffmpeg(): Specified filename is (null).", + cimglist_instance); + if (!fps) + throw CImgArgumentException(_cimglist_instance + "save_ffmpeg(): Invalid specified framerate 0, for file '%s'.", + cimglist_instance, + filename); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + cimglist_for(*this,l) if (!_data[l].is_sameXYZ(_data[0])) + throw CImgInstanceException(_cimglist_instance + "save_ffmpeg(): Invalid instance dimensions, for file '%s'.", + cimglist_instance, + filename); + +#ifndef cimg_use_ffmpeg + return save_ffmpeg_external(filename,0,fps,bitrate); +#else + avcodec_register_all(); + av_register_all(); + const int + frame_dimx = _data[0].width(), + frame_dimy = _data[0].height(), + frame_dimv = _data[0].spectrum(); + if (frame_dimv!=1 && frame_dimv!=3) + throw CImgInstanceException(_cimglist_instance + "save_ffmpeg(): Image[0] (%u,%u,%u,%u,%p) has not 1 or 3 channels, for file '%s'.", + cimglist_instance, + _data[0]._width,_data[0]._height,_data[0]._depth,_data[0]._spectrum,_data,filename); + + PixelFormat dest_pxl_fmt = PIX_FMT_YUV420P; + PixelFormat src_pxl_fmt = (frame_dimv==3)?PIX_FMT_RGB24:PIX_FMT_GRAY8; + + int sws_flags = SWS_FAST_BILINEAR; // Interpolation method (keeping same size images for now). + AVOutputFormat *fmt = 0; +#if defined(AV_VERSION_INT) +#if LIBAVFORMAT_VERSION_INToformat = fmt; + std::sprintf(oc->filename,"%s",filename); + + // Add video stream. + AVStream *video_str = 0; + if (fmt->video_codec!=CODEC_ID_NONE) { + video_str = avformat_new_stream(oc,NULL); + if (!video_str) { // Failed to allocate stream. + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to allocate FFMPEG structure for video stream, for file '%s'.", + cimglist_instance, + filename); + } + } else { // No codec identified. + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to identify proper codec, for file '%s'.", + cimglist_instance, + filename); + } + + AVCodecContext *c = video_str->codec; + c->codec_id = fmt->video_codec; + c->codec_type = AVMEDIA_TYPE_VIDEO; + c->bit_rate = 1024*bitrate; + c->width = frame_dimx; + c->height = frame_dimy; + c->time_base.num = 1; + c->time_base.den = fps; + c->gop_size = 12; + c->pix_fmt = dest_pxl_fmt; + if (c->codec_id==CODEC_ID_MPEG2VIDEO) c->max_b_frames = 2; + if (c->codec_id==CODEC_ID_MPEG1VIDEO) c->mb_decision = 2; + + // Open codecs and alloc buffers. + codec = avcodec_find_encoder(c->codec_id); + if (!codec) { // Failed to find codec. + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): No valid codec found for file '%s'.", + cimglist_instance, + filename); + } + if (avcodec_open2(c,codec,NULL)<0) // Failed to open codec. + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to open codec for file '%s'.", + cimglist_instance, + filename); + + tmp_pict = avcodec_alloc_frame(); + if (!tmp_pict) { // Failed to allocate memory for tmp_pict frame. + avcodec_close(video_str->codec); + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to allocate memory for file '%s'.", + cimglist_instance, + filename); + } + tmp_pict->linesize[0] = (src_pxl_fmt==PIX_FMT_RGB24)?3*frame_dimx:frame_dimx; + // tmp_pict->type = FF_BUFFER_TYPE_USER; + int tmp_size = avpicture_get_size(src_pxl_fmt,frame_dimx,frame_dimy); + uint8_t *tmp_buffer = (uint8_t*)av_malloc(tmp_size); + if (!tmp_buffer) { // Failed to allocate memory for tmp buffer. + av_free(tmp_pict); + avcodec_close(video_str->codec); + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to allocate memory for file '%s'.", + cimglist_instance, + filename); + } + + // Associate buffer with tmp_pict. + avpicture_fill((AVPicture*)tmp_pict,tmp_buffer,src_pxl_fmt,frame_dimx,frame_dimy); + picture = avcodec_alloc_frame(); + if (!picture) { // Failed to allocate picture frame. + av_free(tmp_pict->data[0]); + av_free(tmp_pict); + avcodec_close(video_str->codec); + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to allocate memory for file '%s'.", + cimglist_instance, + filename); + } + + int size = avpicture_get_size(c->pix_fmt,frame_dimx,frame_dimy); + uint8_t *buffer = (uint8_t*)av_malloc(size); + if (!buffer) { // Failed to allocate picture frame buffer. + av_free(picture); + av_free(tmp_pict->data[0]); + av_free(tmp_pict); + avcodec_close(video_str->codec); + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to allocate memory for file '%s'.", + cimglist_instance, + filename); + } + + // Associate the buffer with picture. + avpicture_fill((AVPicture*)picture,buffer,c->pix_fmt,frame_dimx,frame_dimy); + + // Open file. + if (!(fmt->flags&AVFMT_NOFILE)) { + if (avio_open(&oc->pb,filename,AVIO_FLAG_WRITE)<0) + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to open file '%s'.", + cimglist_instance, + filename); + } + + if (avformat_write_header(oc,NULL)<0) + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to write header in file '%s'.", + cimglist_instance, + filename); + + SwsContext *img_convert_context = 0; + img_convert_context = sws_getContext(frame_dimx,frame_dimy,src_pxl_fmt, + c->width,c->height,c->pix_fmt,sws_flags,0,0,0); + if (!img_convert_context) { // Failed to get swscale context. + // if (!(fmt->flags & AVFMT_NOFILE)) url_fclose(&oc->pb); + av_free(picture->data); + av_free(picture); + av_free(tmp_pict->data[0]); + av_free(tmp_pict); + avcodec_close(video_str->codec); + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to get conversion context for file '%s'.", + cimglist_instance, + filename); + } + int ret = 0, out_size; + uint8_t *video_outbuf = 0; + int video_outbuf_size = 1000000; + video_outbuf = (uint8_t*)av_malloc(video_outbuf_size); + if (!video_outbuf) { + // if (!(fmt->flags & AVFMT_NOFILE)) url_fclose(&oc->pb); + av_free(picture->data); + av_free(picture); + av_free(tmp_pict->data[0]); + av_free(tmp_pict); + avcodec_close(video_str->codec); + av_free(oc); + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to allocate memory, for file '%s'.", + cimglist_instance, + filename); + } + + // Loop through each desired image in list. + cimglist_for(*this,i) { + CImg currentIm = _data[i], red, green, blue, gray; + if (src_pxl_fmt==PIX_FMT_RGB24) { + red = currentIm.get_shared_channel(0); + green = currentIm.get_shared_channel(1); + blue = currentIm.get_shared_channel(2); + cimg_forXY(currentIm,X,Y) { // Assign pizel values to data buffer in interlaced RGBRGB ... format. + tmp_pict->data[0][Y*tmp_pict->linesize[0] + 3*X] = red(X,Y); + tmp_pict->data[0][Y*tmp_pict->linesize[0] + 3*X + 1] = green(X,Y); + tmp_pict->data[0][Y*tmp_pict->linesize[0] + 3*X + 2] = blue(X,Y); + } + } else { + gray = currentIm.get_shared_channel(0); + cimg_forXY(currentIm,X,Y) tmp_pict->data[0][Y*tmp_pict->linesize[0] + X] = gray(X,Y); + } + if (!video_str) break; + if (sws_scale(img_convert_context,tmp_pict->data,tmp_pict->linesize,0, + c->height,picture->data,picture->linesize)<0) break; + + AVPacket pkt; + int got_packet; + av_init_packet(&pkt); + out_size = avcodec_encode_video2(c,&pkt,picture,&got_packet); + if (got_packet) { + pkt.pts = av_rescale_q(c->coded_frame->pts,c->time_base,video_str->time_base); + if (c->coded_frame->key_frame) pkt.flags|=AV_PKT_FLAG_KEY; + pkt.stream_index = video_str->index; + pkt.data = video_outbuf; + pkt.size = out_size; + ret = av_write_frame(oc,&pkt); + } else if (out_size<0) break; + if (ret) break; // Error occured in writing frame. + } + + // Close codec. + if (video_str) { + avcodec_close(video_str->codec); + av_free(picture->data[0]); + av_free(picture); + av_free(tmp_pict->data[0]); + av_free(tmp_pict); + } + if (av_write_trailer(oc)<0) + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): Failed to write trailer for file '%s'.", + cimglist_instance, + filename); + + av_freep(&oc->streams[0]->codec); + av_freep(&oc->streams[0]); + if (!(fmt->flags&AVFMT_NOFILE)) { + /*if (url_fclose(oc->pb)<0) + throw CImgIOException(_cimglist_instance + "save_ffmpeg(): File '%s', failed to close file.", + cimglist_instance, + filename); + */ + } + av_free(oc); + av_free(video_outbuf); + return *this; +#endif + } + + const CImgList& _save_yuv(std::FILE *const file, const char *const filename, const bool is_rgb) const { + if (!file && !filename) + throw CImgArgumentException(_cimglist_instance + "save_yuv(): Specified filename is (null).", + cimglist_instance); + if (is_empty()) { cimg::fempty(file,filename); return *this; } + if ((*this)[0].width()%2 || (*this)[0].height()%2) + throw CImgInstanceException(_cimglist_instance + "save_yuv(): Invalid odd instance dimensions (%u,%u) for file '%s'.", + cimglist_instance, + (*this)[0].width(),(*this)[0].height(), + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + cimglist_for(*this,l) { + CImg YCbCr((*this)[l]); + if (is_rgb) YCbCr.RGBtoYCbCr(); + cimg::fwrite(YCbCr._data,(unsigned long)YCbCr._width*YCbCr._height,nfile); + cimg::fwrite(YCbCr.get_resize(YCbCr._width/2, YCbCr._height/2,1,3,3).data(0,0,0,1), + (unsigned long)YCbCr._width*YCbCr._height/2,nfile); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save list as a YUV image sequence file. + /** + \param filename Filename to write data to. + \param is_rgb Tells if the RGB to YUV conversion must be done for saving. + **/ + const CImgList& save_yuv(const char *const filename=0, const bool is_rgb=true) const { + return _save_yuv(0,filename,is_rgb); + } + + //! Save image sequence into a YUV file. + /** + \param file File to write data to. + \param is_rgb Tells if the RGB to YUV conversion must be done for saving. + **/ + const CImgList& save_yuv(std::FILE *const file, const bool is_rgb=true) const { + return _save_yuv(file,0,is_rgb); + } + + const CImgList& _save_cimg(std::FILE *const file, const char *const filename, const bool is_compressed) const { + if (!file && !filename) + throw CImgArgumentException(_cimglist_instance + "save_cimg(): Specified filename is (null).", + cimglist_instance); +#ifndef cimg_use_zlib + if (is_compressed) + cimg::warn(_cimglist_instance + "save_cimg(): Unable to save compressed data in file '%s' unless zlib is enabled, " + "saving them uncompressed.", + cimglist_instance, + filename?filename:"(FILE*)"); +#endif + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little"; + if (std::strstr(ptype,"unsigned")==ptype) std::fprintf(nfile,"%u unsigned_%s %s_endian\n",_width,ptype+9,etype); + else std::fprintf(nfile,"%u %s %s_endian\n",_width,ptype,etype); + cimglist_for(*this,l) { + const CImg& img = _data[l]; + std::fprintf(nfile,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum); + if (img._data) { + CImg tmp; + if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); } + const CImg& ref = cimg::endianness()?tmp:img; + bool failed_to_compress = true; + if (is_compressed) { +#ifdef cimg_use_zlib + const unsigned long siz = sizeof(T)*ref.size(); + unsigned long csiz = siz + siz/100 + 16; + Bytef *const cbuf = new Bytef[csiz]; + if (compress(cbuf,&csiz,(Bytef*)ref._data,siz)) + cimg::warn(_cimglist_instance + "save_cimg(): Failed to save compressed data for file '%s', saving them uncompressed.", + cimglist_instance, + filename?filename:"(FILE*)"); + else { + std::fprintf(nfile," #%lu\n",csiz); + cimg::fwrite(cbuf,csiz,nfile); + delete[] cbuf; + failed_to_compress = false; + } +#endif + } + if (failed_to_compress) { // Write in a non-compressed way. + std::fputc('\n',nfile); + cimg::fwrite(ref._data,ref.size(),nfile); + } + } else std::fputc('\n',nfile); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Save list into a .cimg file. + /** + \param filename Filename to write data to. + \param is_compressed Tells if data compression must be enabled. + **/ + const CImgList& save_cimg(const char *const filename, const bool is_compressed=false) const { + return _save_cimg(0,filename,is_compressed); + } + + //! Save list into a .cimg file. + /** + \param file File to write data to. + \param is_compressed Tells if data compression must be enabled. + **/ + const CImgList& save_cimg(std::FILE *file, const bool is_compressed=false) const { + return _save_cimg(file,0,is_compressed); + } + + const CImgList& _save_cimg(std::FILE *const file, const char *const filename, + const unsigned int n0, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0) const { +#define _cimg_save_cimg_case(Ts,Tss) \ + if (!saved && !cimg::strcasecmp(Ts,str_pixeltype)) { \ + for (unsigned int l = 0; l0) { \ + if (l=W || y0>=H || z0>=D || c0>=D) std::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \ + else { \ + const CImg& img = (*this)[l - n0]; \ + const T *ptrs = img._data; \ + const unsigned int \ + x1 = x0 + img._width - 1, \ + y1 = y0 + img._height - 1, \ + z1 = z0 + img._depth - 1, \ + c1 = c0 + img._spectrum - 1, \ + nx1 = x1>=W?W-1:x1, \ + ny1 = y1>=H?H-1:y1, \ + nz1 = z1>=D?D-1:z1, \ + nc1 = c1>=C?C-1:c1; \ + CImg raw(1+nx1-x0); \ + const unsigned int skipvb = c0*W*H*D*sizeof(Tss); \ + if (skipvb) std::fseek(nfile,skipvb,SEEK_CUR); \ + for (unsigned int v = 1 + nc1 - c0; v; --v) { \ + const unsigned int skipzb = z0*W*H*sizeof(Tss); \ + if (skipzb) std::fseek(nfile,skipzb,SEEK_CUR); \ + for (unsigned int z = 1 + nz1 - z0; z; --z) { \ + const unsigned int skipyb = y0*W*sizeof(Tss); \ + if (skipyb) std::fseek(nfile,skipyb,SEEK_CUR); \ + for (unsigned int y = 1 + ny1 - y0; y; --y) { \ + const unsigned int skipxb = x0*sizeof(Tss); \ + if (skipxb) std::fseek(nfile,skipxb,SEEK_CUR); \ + raw.assign(ptrs, raw._width); \ + ptrs+=img._width; \ + if (endian) cimg::invert_endianness(raw._data,raw._width); \ + cimg::fwrite(raw._data,raw._width,nfile); \ + const unsigned int skipxe = (W - 1 - nx1)*sizeof(Tss); \ + if (skipxe) std::fseek(nfile,skipxe,SEEK_CUR); \ + } \ + const unsigned int skipye = (H - 1 - ny1)*W*sizeof(Tss); \ + if (skipye) std::fseek(nfile,skipye,SEEK_CUR); \ + } \ + const unsigned int skipze = (D - 1 - nz1)*W*H*sizeof(Tss); \ + if (skipze) std::fseek(nfile,skipze,SEEK_CUR); \ + } \ + const unsigned int skipve = (C - 1 - nc1)*W*H*D*sizeof(Tss); \ + if (skipve) std::fseek(nfile,skipve,SEEK_CUR); \ + } \ + } \ + } \ + saved = true; \ + } + + if (!file && !filename) + throw CImgArgumentException(_cimglist_instance + "save_cimg(): Specified filename is (null).", + cimglist_instance); + if (is_empty()) + throw CImgInstanceException(_cimglist_instance + "save_cimg(): Empty instance, for file '%s'.", + cimglist_instance, + filename?filename:"(FILE*)"); + + std::FILE *const nfile = file?file:cimg::fopen(filename,"rb+"); + bool saved = false, endian = cimg::endianness(); + char tmp[256] = { 0 }, str_pixeltype[256] = { 0 }, str_endian[256] = { 0 }; + unsigned int j, err, N, W, H, D, C; + int i; + j = 0; while((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0; + err = std::sscanf(tmp,"%u%*c%255[A-Za-z_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype,str_endian); + if (err<2) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "save_cimg(): CImg header not found in file '%s'.", + cimglist_instance, + filename?filename:"(FILE*)"); + } + if (!cimg::strncasecmp("little",str_endian,6)) endian = false; + else if (!cimg::strncasecmp("big",str_endian,3)) endian = true; + const unsigned int lmax = cimg::min(N,n0+_width); + _cimg_save_cimg_case("bool",bool); + _cimg_save_cimg_case("unsigned_char",unsigned char); + _cimg_save_cimg_case("uchar",unsigned char); + _cimg_save_cimg_case("char",char); + _cimg_save_cimg_case("unsigned_short",unsigned short); + _cimg_save_cimg_case("ushort",unsigned short); + _cimg_save_cimg_case("short",short); + _cimg_save_cimg_case("unsigned_int",unsigned int); + _cimg_save_cimg_case("uint",unsigned int); + _cimg_save_cimg_case("int",int); + _cimg_save_cimg_case("unsigned_long",unsigned long); + _cimg_save_cimg_case("ulong",unsigned long); + _cimg_save_cimg_case("long",long); + _cimg_save_cimg_case("float",float); + _cimg_save_cimg_case("double",double); + if (!saved) { + if (!file) cimg::fclose(nfile); + throw CImgIOException(_cimglist_instance + "save_cimg(): Unsupported data type '%s' for file '%s'.", + cimglist_instance, + filename?filename:"(FILE*)",str_pixeltype); + } + if (!file) cimg::fclose(nfile); + return *this; + } + + //! Insert the image instance into into an existing .cimg file, at specified coordinates. + /** + \param filename Filename to write data to. + \param n0 Starting index of images to write. + \param x0 Starting X-coordinates of image regions to write. + \param y0 Starting Y-coordinates of image regions to write. + \param z0 Starting Z-coordinates of image regions to write. + \param c0 Starting C-coordinates of image regions to write. + **/ + const CImgList& save_cimg(const char *const filename, + const unsigned int n0, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0) const { + return _save_cimg(0,filename,n0,x0,y0,z0,c0); + } + + //! Insert the image instance into into an existing .cimg file, at specified coordinates. + /** + \param file File to write data to. + \param n0 Starting index of images to write. + \param x0 Starting X-coordinates of image regions to write. + \param y0 Starting Y-coordinates of image regions to write. + \param z0 Starting Z-coordinates of image regions to write. + \param c0 Starting C-coordinates of image regions to write. + **/ + const CImgList& save_cimg(std::FILE *const file, + const unsigned int n0, + const unsigned int x0, const unsigned int y0, + const unsigned int z0, const unsigned int c0) const { + return _save_cimg(file,0,n0,x0,y0,z0,c0); + } + + static void _save_empty_cimg(std::FILE *const file, const char *const filename, + const unsigned int nb, + const unsigned int dx, const unsigned int dy, + const unsigned int dz, const unsigned int dc) { + std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); + const unsigned long siz = (unsigned long)dx*dy*dz*dc*sizeof(T); + std::fprintf(nfile,"%u %s\n",nb,pixel_type()); + for (unsigned int i=nb; i; --i) { + std::fprintf(nfile,"%u %u %u %u\n",dx,dy,dz,dc); + for (unsigned long off=siz; off; --off) std::fputc(0,nfile); + } + if (!file) cimg::fclose(nfile); + } + + //! Save empty (non-compressed) .cimg file with specified dimensions. + /** + \param filename Filename to write data to. + \param nb Number of images to write. + \param dx Width of images in the written file. + \param dy Height of images in the written file. + \param dz Depth of images in the written file. + \param dc Spectrum of images in the written file. + **/ + static void save_empty_cimg(const char *const filename, + const unsigned int nb, + const unsigned int dx, const unsigned int dy=1, + const unsigned int dz=1, const unsigned int dc=1) { + return _save_empty_cimg(0,filename,nb,dx,dy,dz,dc); + } + + //! Save empty .cimg file with specified dimensions. + /** + \param file File to write data to. + \param nb Number of images to write. + \param dx Width of images in the written file. + \param dy Height of images in the written file. + \param dz Depth of images in the written file. + \param dc Spectrum of images in the written file. + **/ + static void save_empty_cimg(std::FILE *const file, + const unsigned int nb, + const unsigned int dx, const unsigned int dy=1, + const unsigned int dz=1, const unsigned int dc=1) { + return _save_empty_cimg(file,0,nb,dx,dy,dz,dc); + } + + //! Save list as a TIFF file. + /** + \param filename Filename to write data to. + \param compression_type Compression mode used to write data. + **/ + const CImgList& save_tiff(const char *const filename, const unsigned int compression_type=0, + const float *const voxel_size=0) const { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "save_tiff(): Specified filename is (null).", + cimglist_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + +#ifndef cimg_use_tiff + if (_width==1) _data[0].save_tiff(filename,compression_type,voxel_size); + else cimglist_for(*this,l) { + char nfilename[1024] = { 0 }; + cimg::number_filename(filename,l,6,nfilename); + _data[l].save_tiff(nfilename,compression_type,voxel_size); + } +#else + TIFF *tif = TIFFOpen(filename,"w"); + if (tif) { + for (unsigned int dir = 0, l = 0; l<_width; ++l) { + const CImg& img = (*this)[l]; + if (img) { + if (img._depth==1) img._save_tiff(tif,dir++,compression_type,voxel_size); + else cimg_forZ(img,z) img.get_slice(z)._save_tiff(tif,dir++,compression_type,voxel_size); + } + } + TIFFClose(tif); + } else + throw CImgIOException(_cimglist_instance + "save_tiff(): Failed to open stream for file '%s'.", + cimglist_instance, + filename); +#endif + return *this; + } + + + //! Save list as a gzipped file, using external tool 'gzip'. + /** + \param filename Filename to write data to. + **/ + const CImgList& save_gzip_external(const char *const filename) const { + if (!filename) + throw CImgIOException(_cimglist_instance + "save_gzip_external(): Specified filename is (null).", + cimglist_instance); + + char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; + const char + *ext = cimg::split_filename(filename,body), + *ext2 = cimg::split_filename(body,0); + std::FILE *file; + do { + if (!cimg::strcasecmp(ext,"gz")) { + if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } else { + if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); + else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", + cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + } + if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); + } while (file); + + if (is_saveable(body)) { + save(filetmp); + cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", + cimg::gzip_path(), + CImg::string(filetmp)._system_strescape().data(), + CImg::string(filename)._system_strescape().data()); + cimg::system(command); + file = std::fopen(filename,"rb"); + if (!file) + throw CImgIOException(_cimglist_instance + "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.", + cimglist_instance, + filename); + else cimg::fclose(file); + std::remove(filetmp); + } else { + char nfilename[1024] = { 0 }; + cimglist_for(*this,l) { + cimg::number_filename(body,l,6,nfilename); + if (*ext) std::sprintf(nfilename + std::strlen(nfilename),".%s",ext); + _data[l].save_gzip_external(nfilename); + } + } + return *this; + } + + //! Save image sequence, using the external tool 'ffmpeg'. + /** + \param filename Filename to write data to. + \param codec Type of compression. + \param fps Number of frames per second. + \param bitrate Output bitrate + **/ + const CImgList& save_ffmpeg_external(const char *const filename, const char *const codec=0, + const unsigned int fps=25, const unsigned int bitrate=2048) const { + if (!filename) + throw CImgArgumentException(_cimglist_instance + "save_ffmpeg_external(): Specified filename is (null).", + cimglist_instance); + if (is_empty()) { cimg::fempty(0,filename); return *this; } + + const char + *const ext = cimg::split_filename(filename), + *const _codec = codec?codec:!cimg::strcasecmp(ext,"flv")?"flv":"mpeg2video"; + + char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; + CImgList filenames; + std::FILE *file = 0; + cimglist_for(*this,l) if (!_data[l].is_sameXYZ(_data[0])) + throw CImgInstanceException(_cimglist_instance + "save_ffmpeg_external(): Invalid instance dimensions for file '%s'.", + cimglist_instance, + filename); + do { + cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_000001.ppm",filetmp); + if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); + } while (file); + cimglist_for(*this,l) { + cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%.6u.ppm",filetmp,l+1); + CImg::string(filetmp2).move_to(filenames); + if (_data[l]._depth>1 || _data[l]._spectrum!=3) _data[l].get_resize(-100,-100,1,3).save_pnm(filetmp2); + else _data[l].save_pnm(filetmp2); + } +#if cimg_OS!=2 + cimg_snprintf(command,sizeof(command),"%s -i \"%s_%%6d.ppm\" -vcodec %s -b %uk -r %u -y \"%s\" >/dev/null 2>&1", + cimg::ffmpeg_path(), + CImg::string(filetmp)._system_strescape().data(), + _codec,bitrate,fps, + CImg::string(filename)._system_strescape().data()); +#else + cimg_snprintf(command,sizeof(command),"\"%s -i \"%s_%%6d.ppm\" -vcodec %s -b %uk -r %u -y \"%s\"\" >NUL 2>&1", + cimg::ffmpeg_path(), + CImg::string(filetmp)._system_strescape().data(), + _codec,bitrate,fps, + CImg::string(filename)._system_strescape().data()); +#endif + cimg::system(command); + file = std::fopen(filename,"rb"); + if (!file) + throw CImgIOException(_cimglist_instance + "save_ffmpeg_external(): Failed to save file '%s' with external command 'ffmpeg'.", + cimglist_instance, + filename); + else cimg::fclose(file); + cimglist_for(*this,l) std::remove(filenames[l]); + return *this; + } + + //@} + //---------------------------------- + // + //! \name Others + //@{ + //---------------------------------- + + //! Crop font along the X-axis. + /** + **/ + CImgList& crop_font() { + return get_crop_font().move_to(*this); + } + + //! Crop font along the X-axis \newinstance. + /** + **/ + CImgList get_crop_font() const { + CImgList res; + cimglist_for(*this,l) { + const CImg& letter = (*this)[l]; + int xmin = letter._width, xmax = 0; + cimg_forXY(letter,x,y) if (letter(x,y)) { if (xxmax) xmax = x; } + if (xmin>xmax) CImg(letter._width,letter._height,1,letter._spectrum,0).move_to(res); + else letter.get_crop(xmin,0,xmax,letter._height-1).move_to(res); + } + res[' '].resize(res['f']._width,-100,-100,-100,0); + if (' '+256& font(const unsigned int font_height, const bool is_variable_width=true) { + if (!font_height) return CImgList::empty(); + cimg::mutex(11); + + // Decompress nearest base font data if needed. + const char *data_fonts[] = { cimg::data_font12x13, cimg::data_font20x23, cimg::data_font47x53, 0 }; + const unsigned int data_widths[] = { 12,20,47,90 }, data_heights[] = { 13,23,53,103 }, + data_Ms[] = { 86,79,57,47 }; + const unsigned int data_ind = font_height<=13?0:font_height<=23?1:font_height<=53?2:3; + static CImg base_fonts[4]; + CImg &base_font = base_fonts[data_ind]; + if (!base_font) { + const unsigned int w = data_widths[data_ind], h = data_heights[data_ind], M = data_Ms[data_ind]; + base_font.assign(256*w,h); + const char *data_font = data_fonts[data_ind]; + unsigned char *ptrd = base_font; + const unsigned char *const ptrde = base_font.end(); + + // Special case needed for 90x103 to avoid MS compiler limit with big strings. + CImg data90x103; + if (!data_font) { + ((CImg(cimg::_data_font90x103[0], + (unsigned int)std::strlen(cimg::_data_font90x103[0]),1,1,1,true), + CImg(cimg::_data_font90x103[1], + (unsigned int)std::strlen(cimg::_data_font90x103[1])+1,1,1,1,true))>'x'). + move_to(data90x103); + data_font = data90x103.data(); + } + + // Uncompress font data (decode RLE). + for (const char *ptrs = data_font; *ptrs; ++ptrs) { + const int c = *ptrs-M-32, v = c>=0?255:0, n = c>=0?c:-c; + if (ptrd+n<=ptrde) { std::memset(ptrd,v,n); ptrd+=n; } + else { std::memset(ptrd,v,ptrde-ptrd); break; } + } + } + + // Find optimal font cache location to return. + static CImgList fonts[16]; + static bool is_variable_widths[16] = { 0 }; + unsigned int ind = ~0U; + for (int i = 0; i<16; ++i) + if (!fonts[i] || (is_variable_widths[i]==is_variable_width && font_height==fonts[i][0]._height)) { + ind = i; break; // Found empty slot or cached font. + } + if (ind==~0U) { // No empty slots nor existing font in cache. + std::memmove(fonts,fonts+1,15*sizeof(CImgList)); + std::memmove(is_variable_widths,is_variable_widths+1,15*sizeof(bool)); + std::memset(fonts+(ind=15),0,sizeof(CImgList)); // Free a slot in cache for new font. + } + CImgList &font = fonts[ind]; + + // Render requested font. + if (!font) { + const unsigned int padding_x = font_height<33?1:font_height<53?2:font_height<103?3:4; + is_variable_widths[ind] = is_variable_width; + font = base_font.get_split('x',256); + if (font_height!=font[0]._height) + cimglist_for(font,l) + font[l].resize(cimg::max(1U,font[l]._width*font_height/font[l]._height),font_height,-100,-100, + font[0]._height>font_height?2:5); + if (is_variable_width) font.crop_font(); + cimglist_for(font,l) font[l].resize(font[l]._width + padding_x,-100,1,1,0,0,0.5); + font.insert(256,0); + cimglist_for_in(font,0,255,l) font[l].assign(font[l+256]._width,font[l+256]._height,1,3,1); + } + cimg::mutex(11,0); + return font; + } + + //! Compute a 1d Fast Fourier Transform, along specified axis. + /** + \param axis Axis along which the Fourier transform is computed. + \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed. + **/ + CImgList& FFT(const char axis, const bool invert=false) { + if (is_empty()) return *this; + if (_width==1) insert(1); + if (_width>2) + cimg::warn(_cimglist_instance + "FFT(): Instance has more than 2 images", + cimglist_instance); + + CImg::FFT(_data[0],_data[1],axis,invert); + return *this; + } + + //! Compute a 1-D Fast Fourier Transform, along specified axis \newinstance. + CImgList get_FFT(const char axis, const bool invert=false) const { + return CImgList(*this,false).FFT(axis,invert); + } + + //! Compute a n-d Fast Fourier Transform. + /** + \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed. + **/ + CImgList& FFT(const bool invert=false) { + if (is_empty()) return *this; + if (_width==1) insert(1); + if (_width>2) + cimg::warn(_cimglist_instance + "FFT(): Instance has more than 2 images", + cimglist_instance); + + CImg::FFT(_data[0],_data[1],invert); + return *this; + } + + //! Compute a n-d Fast Fourier Transform \newinstance. + CImgList get_FFT(const bool invert=false) const { + return CImgList(*this,false).FFT(invert); + } + + //! Reverse primitives orientations of a 3d object. + /** + **/ + CImgList& reverse_object3d() { + cimglist_for(*this,l) { + CImg& p = _data[l]; + switch (p.size()) { + case 2: case 3: cimg::swap(p[0],p[1]); break; + case 6: cimg::swap(p[0],p[1],p[2],p[4],p[3],p[5]); break; + case 9: cimg::swap(p[0],p[1],p[3],p[5],p[4],p[6]); break; + case 4: cimg::swap(p[0],p[1],p[2],p[3]); break; + case 12: cimg::swap(p[0],p[1],p[2],p[3],p[4],p[6],p[5],p[7],p[8],p[10],p[9],p[11]); break; + } + } + return *this; + } + + //! Reverse primitives orientations of a 3d object \newinstance. + CImgList get_reverse_object3d() const { + return (+*this).reverse_object3d(); + } + + //@} + }; // struct CImgList { ... + + /* + #--------------------------------------------- + # + # Completion of previously declared functions + # + #---------------------------------------------- + */ + +namespace cimg { + + // Implement a tic/toc mechanism to display elapsed time of algorithms. + inline unsigned long tictoc(const bool is_tic) { + cimg::mutex(2); + static CImg times(64); + static unsigned int pos = 0; + const unsigned long t1 = cimg::time(); + if (is_tic) { // Tic. + times[pos++] = t1; + if (pos>=times._width) + throw CImgArgumentException("cimg::tic(): Too much calls to 'cimg::tic()' without calls to 'cimg::toc()'."); + cimg::mutex(2,0); + return t1; + } + // Toc. + if (!pos) + throw CImgArgumentException("cimg::toc(): No previous call to 'cimg::tic()' has been made."); + const unsigned long + t0 = times[--pos], + dt = t1>=t0?(t1-t0):cimg::type::max(); + const unsigned int + edays = (unsigned int)(dt/86400000.0), + ehours = (unsigned int)((dt - edays*86400000.0)/3600000.0), + emin = (unsigned int)((dt - edays*86400000.0 - ehours*3600000.0)/60000.0), + esec = (unsigned int)((dt - edays*86400000.0 - ehours*3600000.0 - emin*60000.0)/1000.0), + ems = (unsigned int)(dt - edays*86400000.0 - ehours*3600000.0 - emin*60000.0 - esec*1000.0); + if (!edays && !ehours && !emin && !esec) + std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u ms%s\n", + cimg::t_red,1+2*pos,"",ems,cimg::t_normal); + else { + if (!edays && !ehours && !emin) + std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u sec %u ms%s\n", + cimg::t_red,1+2*pos,"",esec,ems,cimg::t_normal); + else { + if (!edays && !ehours) + std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u min %u sec %u ms%s\n", + cimg::t_red,1+2*pos,"",emin,esec,ems,cimg::t_normal); + else{ + if (!edays) + std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u hours %u min %u sec %u ms%s\n", + cimg::t_red,1+2*pos,"",ehours,emin,esec,ems,cimg::t_normal); + else{ + std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u days %u hours %u min %u sec %u ms%s\n", + cimg::t_red,1+2*pos,"",edays,ehours,emin,esec,ems,cimg::t_normal); + } + } + } + } + cimg::mutex(2,0); + return dt; + } + + //! Display a simple dialog box, and wait for the user's response. + /** + \param title Title of the dialog window. + \param msg Main message displayed inside the dialog window. + \param button1_label Label of the 1st button. + \param button2_label Label of the 2nd button (\c 0 to hide button). + \param button3_label Label of the 3rd button (\c 0 to hide button). + \param button4_label Label of the 4th button (\c 0 to hide button). + \param button5_label Label of the 5th button (\c 0 to hide button). + \param button6_label Label of the 6th button (\c 0 to hide button). + \param logo Image logo displayed at the left of the main message. + \param is_centered Tells if the dialog window must be centered on the screen. + \return Indice of clicked button (from \c 0 to \c 5), or \c -1 if the dialog window has been closed by the user. + \note + - Up to 6 buttons can be defined in the dialog window. + - The function returns when a user clicked one of the button or closed the dialog window. + - If a button text is set to 0, the corresponding button (and the followings) will not appear in the dialog box. + At least one button must be specified. + **/ + template + inline int dialog(const char *const title, const char *const msg, + const char *const button1_label, const char *const button2_label, + const char *const button3_label, const char *const button4_label, + const char *const button5_label, const char *const button6_label, + const CImg& logo, const bool is_centered = false) { +#if cimg_display==0 + cimg::unused(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label, + logo._data,is_centered); + throw CImgIOException("cimg::dialog(): No display available."); +#else + const unsigned char + black[] = { 0,0,0 }, white[] = { 255,255,255 }, gray[] = { 200,200,200 }, gray2[] = { 150,150,150 }; + + // Create buttons and canvas graphics + CImgList buttons, cbuttons, sbuttons; + if (button1_label) { CImg().draw_text(0,0,button1_label,black,gray,1,13).move_to(buttons); + if (button2_label) { CImg().draw_text(0,0,button2_label,black,gray,1,13).move_to(buttons); + if (button3_label) { CImg().draw_text(0,0,button3_label,black,gray,1,13).move_to(buttons); + if (button4_label) { CImg().draw_text(0,0,button4_label,black,gray,1,13).move_to(buttons); + if (button5_label) { CImg().draw_text(0,0,button5_label,black,gray,1,13).move_to(buttons); + if (button6_label) { CImg().draw_text(0,0,button6_label,black,gray,1,13).move_to(buttons); + }}}}}} + if (!buttons._width) + throw CImgArgumentException("cimg::dialog(): No buttons have been defined."); + cimglist_for(buttons,l) buttons[l].resize(-100,-100,1,3); + + unsigned int bw = 0, bh = 0; + cimglist_for(buttons,l) { bw = cimg::max(bw,buttons[l]._width); bh = cimg::max(bh,buttons[l]._height); } + bw+=8; bh+=8; + if (bw<64) bw = 64; + if (bw>128) bw = 128; + if (bh<24) bh = 24; + if (bh>48) bh = 48; + + CImg button(bw,bh,1,3); + button.draw_rectangle(0,0,bw-1,bh-1,gray); + button.draw_line(0,0,bw-1,0,white).draw_line(0,bh-1,0,0,white); + button.draw_line(bw-1,0,bw-1,bh-1,black).draw_line(bw-1,bh-1,0,bh-1,black); + button.draw_line(1,bh-2,bw-2,bh-2,gray2).draw_line(bw-2,bh-2,bw-2,1,gray2); + CImg sbutton(bw,bh,1,3); + sbutton.draw_rectangle(0,0,bw-1,bh-1,gray); + sbutton.draw_line(0,0,bw-1,0,black).draw_line(bw-1,0,bw-1,bh-1,black); + sbutton.draw_line(bw-1,bh-1,0,bh-1,black).draw_line(0,bh-1,0,0,black); + sbutton.draw_line(1,1,bw-2,1,white).draw_line(1,bh-2,1,1,white); + sbutton.draw_line(bw-2,1,bw-2,bh-2,black).draw_line(bw-2,bh-2,1,bh-2,black); + sbutton.draw_line(2,bh-3,bw-3,bh-3,gray2).draw_line(bw-3,bh-3,bw-3,2,gray2); + sbutton.draw_line(4,4,bw-5,4,black,1,0xAAAAAAAA,true).draw_line(bw-5,4,bw-5,bh-5,black,1,0xAAAAAAAA,false); + sbutton.draw_line(bw-5,bh-5,4,bh-5,black,1,0xAAAAAAAA,false).draw_line(4,bh-5,4,4,black,1,0xAAAAAAAA,false); + CImg cbutton(bw,bh,1,3); + cbutton.draw_rectangle(0,0,bw-1,bh-1,black).draw_rectangle(1,1,bw-2,bh-2,gray2).draw_rectangle(2,2,bw-3,bh-3,gray); + cbutton.draw_line(4,4,bw-5,4,black,1,0xAAAAAAAA,true).draw_line(bw-5,4,bw-5,bh-5,black,1,0xAAAAAAAA,false); + cbutton.draw_line(bw-5,bh-5,4,bh-5,black,1,0xAAAAAAAA,false).draw_line(4,bh-5,4,4,black,1,0xAAAAAAAA,false); + + cimglist_for(buttons,ll) { + CImg(cbutton).draw_image(1+(bw-buttons[ll].width())/2,1+(bh-buttons[ll].height())/2,buttons[ll]). + move_to(cbuttons); + CImg(sbutton).draw_image((bw-buttons[ll].width())/2,(bh-buttons[ll].height())/2,buttons[ll]). + move_to(sbuttons); + CImg(button).draw_image((bw-buttons[ll].width())/2,(bh-buttons[ll].height())/2,buttons[ll]). + move_to(buttons[ll]); + } + + CImg canvas; + if (msg) + ((CImg().draw_text(0,0,"%s",gray,0,1,13,msg)*=-1)+=200).resize(-100,-100,1,3).move_to(canvas); + + const unsigned int + bwall = (buttons._width-1)*(12+bw) + bw, + w = cimg::max(196U,36+logo._width+canvas._width,24+bwall), + h = cimg::max(96U,36+canvas._height+bh,36+logo._height+bh), + lx = 12 + (canvas._data?0:((w-24-logo._width)/2)), + ly = (h-12-bh-logo._height)/2, + tx = lx+logo._width+12, + ty = (h-12-bh-canvas._height)/2, + bx = (w-bwall)/2, + by = h-12-bh; + + if (canvas._data) + canvas = CImg(w,h,1,3). + draw_rectangle(0,0,w-1,h-1,gray). + draw_line(0,0,w-1,0,white).draw_line(0,h-1,0,0,white). + draw_line(w-1,0,w-1,h-1,black).draw_line(w-1,h-1,0,h-1,black). + draw_image(tx,ty,canvas); + else + canvas = CImg(w,h,1,3). + draw_rectangle(0,0,w-1,h-1,gray). + draw_line(0,0,w-1,0,white).draw_line(0,h-1,0,0,white). + draw_line(w-1,0,w-1,h-1,black).draw_line(w-1,h-1,0,h-1,black); + if (logo._data) canvas.draw_image(lx,ly,logo); + + unsigned int xbuttons[6] = { 0 }; + cimglist_for(buttons,lll) { xbuttons[lll] = bx+(bw+12)*lll; canvas.draw_image(xbuttons[lll],by,buttons[lll]); } + + // Open window and enter events loop + CImgDisplay disp(canvas,title?title:" ",0,false,is_centered?true:false); + if (is_centered) disp.move((CImgDisplay::screen_width() - disp.width())/2, + (CImgDisplay::screen_height() - disp.height())/2); + bool stop_flag = false, refresh = false; + int oselected = -1, oclicked = -1, selected = -1, clicked = -1; + while (!disp.is_closed() && !stop_flag) { + if (refresh) { + if (clicked>=0) + CImg(canvas).draw_image(xbuttons[clicked],by,cbuttons[clicked]).display(disp); + else { + if (selected>=0) + CImg(canvas).draw_image(xbuttons[selected],by,sbuttons[selected]).display(disp); + else canvas.display(disp); + } + refresh = false; + } + disp.wait(15); + if (disp.is_resized()) disp.resize(disp,false); + + if (disp.button()&1) { + oclicked = clicked; + clicked = -1; + cimglist_for(buttons,l) + if (disp.mouse_y()>=(int)by && disp.mouse_y()<(int)(by+bh) && + disp.mouse_x()>=(int)xbuttons[l] && disp.mouse_x()<(int)(xbuttons[l]+bw)) { + clicked = selected = l; + refresh = true; + } + if (clicked!=oclicked) refresh = true; + } else if (clicked>=0) stop_flag = true; + + if (disp.key()) { + oselected = selected; + switch (disp.key()) { + case cimg::keyESC : selected=-1; stop_flag = true; break; + case cimg::keyENTER : if (selected<0) selected = 0; stop_flag = true; break; + case cimg::keyTAB : + case cimg::keyARROWRIGHT : + case cimg::keyARROWDOWN : selected = (selected+1)%buttons._width; break; + case cimg::keyARROWLEFT : + case cimg::keyARROWUP : selected = (selected+buttons._width-1)%buttons._width; break; + } + disp.set_key(); + if (selected!=oselected) refresh = true; + } + } + if (!disp) selected = -1; + return selected; +#endif + } + + //! Display a simple dialog box, and wait for the user's response \specialization. + inline int dialog(const char *const title, const char *const msg, + const char *const button1_label, const char *const button2_label, const char *const button3_label, + const char *const button4_label, const char *const button5_label, const char *const button6_label, + const bool is_centered) { + return dialog(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label, + CImg::_logo40x38(),is_centered); + } + + //! Evaluate math expression. + /** + \param expression C-string describing the formula to evaluate. + \param x Value of the pre-defined variable \c x. + \param y Value of the pre-defined variable \c y. + \param z Value of the pre-defined variable \c z. + \param c Value of the pre-defined variable \c c. + \return Result of the formula evaluation. + \note Set \c expression to \c 0 to keep evaluating the last specified \c expression. + \par Example + \code + const double + res1 = cimg::eval("cos(x)^2+sin(y)^2",2,2), // will return '1'. + res2 = cimg::eval(0,1,1); // will return '1' too. + \endcode + **/ + inline double eval(const char *const expression, const double x, const double y, const double z, const double c) { + static const CImg empty; + return empty.eval(expression,x,y,z,c); + } + + template + inline CImg::type> eval(const char *const expression, const CImg& xyzc) { + static const CImg empty; + return empty.eval(expression,xyzc); + } + + // End of cimg:: namespace +} + + // End of cimg_library:: namespace +} + +//! Short alias name. +namespace cil = cimg_library_suffixed; + +#ifdef _cimg_redefine_False +#define False 0 +#endif +#ifdef _cimg_redefine_True +#define True 1 +#endif +#ifdef _cimg_redefine_None +#define None 0 +#endif +#ifdef _cimg_redefine_min +#define min(a,b) (((a)<(b))?(a):(b)) +#endif +#ifdef _cimg_redefine_max +#define max(a,b) (((a)>(b))?(a):(b)) +#endif +#ifdef _cimg_redefine_PI +#define PI 3.141592653589793238462643383 +#endif + +#endif +// Local Variables: +// mode: c++ +// End: diff --git a/stim/image/image.h b/stim/image/image.h new file mode 100644 index 0000000..a6d5195 --- /dev/null +++ b/stim/image/image.h @@ -0,0 +1,99 @@ +#ifndef STIM_IMAGE_H +#define STIM_IMAGE_H +#define cimg_use_jpeg //necessary for JPG files +#include "CImg.h" + +#include +namespace stim{ +//This static class provides the STIM interface for loading images +// Use this interface for all image management - that way the actual library can be changed without problems + +//currently this interface uses CImg +// T = data type (usually unsigned char) +template +class image{ + + cimg_library::CImg img; + +public: + + //default constructor + image(){ + } + + //constructor (load an image file) + image(std::string filename){ + img.load(filename.c_str()); + } + + //Load an image from a file + void load(std::string filename){ + img.load(filename.c_str()); + } + + //save a file + void save(std::string filename){ + img.save(filename.c_str()); + } + + //create an image from an interleaved buffer + void set_interleaved(T* buffer, unsigned int width, unsigned int height, unsigned int channels = 1){ + + unsigned char* non_interleaved = (unsigned char*)malloc(width * height * 3); + unsigned int S = width * height; + + for(unsigned int i = 0; i < S; i++){ + for(unsigned int c = 0; c < channels; c++){ + non_interleaved[i + c * S] = buffer[i * channels + c]; + } + } + + img = cimg_library::CImg(non_interleaved, width, height, 1, channels); + } + + //fills an allocated region of memory with non-interleaved data + void data_noninterleaved(T* data){ + memcpy(data, img.data(), sizeof(T) * size()); + } + + void data_interleaved(T* data){ + + unsigned int C = channels(); + unsigned int X = size(); + + T* ptr = img.data(); + + //for each channel + for(unsigned int c = 0; c < C; c++) + //convert each pixel + for(unsigned int x = 0; x < X; x++) + data[x * C + c] = ptr[c * X + x]; + } + + unsigned int channels(){ + return (unsigned int)img.spectrum(); + } + + unsigned int width(){ + return img.width(); + } + + unsigned int height(){ + return img.height(); + } + + //returns the size (number of values) of the image + unsigned long size(){ + return img.size(); + } + + + + + +}; + +}; //end namespace stim + + +#endif diff --git a/stim/math/bessel.h b/stim/math/bessel.h new file mode 100644 index 0000000..d291deb --- /dev/null +++ b/stim/math/bessel.h @@ -0,0 +1,1515 @@ +#ifndef RTS_BESSEL_H +#define RTS_BESSEL_H + +#define _USE_MATH_DEFINES +#include +#include "../math/complex.h" +#define eps 1e-15 +#define el 0.5772156649015329 + + +namespace stim{ + +static complex cii(0.0,1.0); +static complex cone(1.0,0.0); +static complex czero(0.0,0.0); + +template< typename P > +P gamma(P x) +{ + int i,k,m; + P ga,gr,r,z; + + static P g[] = { + 1.0, + 0.5772156649015329, + -0.6558780715202538, + -0.420026350340952e-1, + 0.1665386113822915, + -0.421977345555443e-1, + -0.9621971527877e-2, + 0.7218943246663e-2, + -0.11651675918591e-2, + -0.2152416741149e-3, + 0.1280502823882e-3, + -0.201348547807e-4, + -0.12504934821e-5, + 0.1133027232e-5, + -0.2056338417e-6, + 0.6116095e-8, + 0.50020075e-8, + -0.11812746e-8, + 0.1043427e-9, + 0.77823e-11, + -0.36968e-11, + 0.51e-12, + -0.206e-13, + -0.54e-14, + 0.14e-14}; + + if (x > 171.0) return 1e308; // This value is an overflow flag. + if (x == (int)x) { + if (x > 0.0) { + ga = 1.0; // use factorial + for (i=2;i 1.0) { + z = fabs(x); + m = (int)z; + r = 1.0; + for (k=1;k<=m;k++) { + r *= (z-k); + } + z -= m; + } + else + z = x; + gr = g[24]; + for (k=23;k>=0;k--) { + gr = gr*z+g[k]; + } + ga = 1.0/(gr*z); + if (fabs(x) > 1.0) { + ga *= r; + if (x < 0.0) { + ga = -M_PI/(x*ga*sin(M_PI*x)); + } + } + } + return ga; +} + +template +int bessjy01a(P x,P &j0,P &j1,P &y0,P &y1, + P &j0p,P &j1p,P &y0p,P &y1p) +{ + P x2,r,ec,w0,w1,r0,r1,cs0,cs1; + P cu,p0,q0,p1,q1,t1,t2; + int k,kz; + static P a[] = { + -7.03125e-2, + 0.112152099609375, + -0.5725014209747314, + 6.074042001273483, + -1.100171402692467e2, + 3.038090510922384e3, + -1.188384262567832e5, + 6.252951493434797e6, + -4.259392165047669e8, + 3.646840080706556e10, + -3.833534661393944e12, + 4.854014686852901e14, + -7.286857349377656e16, + 1.279721941975975e19}; + static P b[] = { + 7.32421875e-2, + -0.2271080017089844, + 1.727727502584457, + -2.438052969955606e1, + 5.513358961220206e2, + -1.825775547429318e4, + 8.328593040162893e5, + -5.006958953198893e7, + 3.836255180230433e9, + -3.649010818849833e11, + 4.218971570284096e13, + -5.827244631566907e15, + 9.476288099260110e17, + -1.792162323051699e20}; + static P a1[] = { + 0.1171875, + -0.1441955566406250, + 0.6765925884246826, + -6.883914268109947, + 1.215978918765359e2, + -3.302272294480852e3, + 1.276412726461746e5, + -6.656367718817688e6, + 4.502786003050393e8, + -3.833857520742790e10, + 4.011838599133198e12, + -5.060568503314727e14, + 7.572616461117958e16, + -1.326257285320556e19}; + static P b1[] = { + -0.1025390625, + 0.2775764465332031, + -1.993531733751297, + 2.724882731126854e1, + -6.038440767050702e2, + 1.971837591223663e4, + -8.902978767070678e5, + 5.310411010968522e7, + -4.043620325107754e9, + 3.827011346598605e11, + -4.406481417852278e13, + 6.065091351222699e15, + -9.833883876590679e17, + 1.855045211579828e20}; + + if (x < 0.0) return 1; + if (x == 0.0) { + j0 = 1.0; + j1 = 0.0; + y0 = -1e308; + y1 = -1e308; + j0p = 0.0; + j1p = 0.5; + y0p = 1e308; + y1p = 1e308; + return 0; + } + x2 = x*x; + if (x <= 12.0) { + j0 = 1.0; + r = 1.0; + for (k=1;k<=30;k++) { + r *= -0.25*x2/(k*k); + j0 += r; + if (fabs(r) < fabs(j0)*1e-15) break; + } + j1 = 1.0; + r = 1.0; + for (k=1;k<=30;k++) { + r *= -0.25*x2/(k*(k+1)); + j1 += r; + if (fabs(r) < fabs(j1)*1e-15) break; + } + j1 *= 0.5*x; + ec = log(0.5*x)+el; + cs0 = 0.0; + w0 = 0.0; + r0 = 1.0; + for (k=1;k<=30;k++) { + w0 += 1.0/k; + r0 *= -0.25*x2/(k*k); + r = r0 * w0; + cs0 += r; + if (fabs(r) < fabs(cs0)*1e-15) break; + } + y0 = M_2_PI*(ec*j0-cs0); + cs1 = 1.0; + w1 = 0.0; + r1 = 1.0; + for (k=1;k<=30;k++) { + w1 += 1.0/k; + r1 *= -0.25*x2/(k*(k+1)); + r = r1*(2.0*w1+1.0/(k+1)); + cs1 += r; + if (fabs(r) < fabs(cs1)*1e-15) break; + } + y1 = M_2_PI * (ec*j1-1.0/x-0.25*x*cs1); + } + else { + if (x >= 50.0) kz = 8; // Can be changed to 10 + else if (x >= 35.0) kz = 10; // " " 12 + else kz = 12; // " " 14 + t1 = x-M_PI_4; + p0 = 1.0; + q0 = -0.125/x; + for (k=0;k +int bessjy01b(P x,P &j0,P &j1,P &y0,P &y1, + P &j0p,P &j1p,P &y0p,P &y1p) +{ + P t,t2,dtmp,a0,p0,q0,p1,q1,ta0,ta1; + if (x < 0.0) return 1; + if (x == 0.0) { + j0 = 1.0; + j1 = 0.0; + y0 = -1e308; + y1 = -1e308; + j0p = 0.0; + j1p = 0.5; + y0p = 1e308; + y1p = 1e308; + return 0; + } + if(x <= 4.0) { + t = x/4.0; + t2 = t*t; + j0 = ((((((-0.5014415e-3*t2+0.76771853e-2)*t2-0.0709253492)*t2+ + 0.4443584263)*t2-1.7777560599)*t2+3.9999973021)*t2 + -3.9999998721)*t2+1.0; + j1 = t*(((((((-0.1289769e-3*t2+0.22069155e-2)*t2-0.0236616773)*t2+ + 0.1777582922)*t2-0.8888839649)*t2+2.6666660544)*t2- + 3.999999971)*t2+1.9999999998); + dtmp = (((((((-0.567433e-4*t2+0.859977e-3)*t2-0.94855882e-2)*t2+ + 0.0772975809)*t2-0.4261737419)*t2+1.4216421221)*t2- + 2.3498519931)*t2+1.0766115157)*t2+0.3674669052; + y0 = M_2_PI*log(0.5*x)*j0+dtmp; + dtmp = (((((((0.6535773e-3*t2-0.0108175626)*t2+0.107657607)*t2- + 0.7268945577)*t2+3.1261399273)*t2-7.3980241381)*t2+ + 6.8529236342)*t2+0.3932562018)*t2-0.6366197726; + y1 = M_2_PI*log(0.5*x)*j1+dtmp/x; + } + else { + t = 4.0/x; + t2 = t*t; + a0 = sqrt(M_2_PI/x); + p0 = ((((-0.9285e-5*t2+0.43506e-4)*t2-0.122226e-3)*t2+ + 0.434725e-3)*t2-0.4394275e-2)*t2+0.999999997; + q0 = t*(((((0.8099e-5*t2-0.35614e-4)*t2+0.85844e-4)*t2- + 0.218024e-3)*t2+0.1144106e-2)*t2-0.031249995); + ta0 = x-M_PI_4; + j0 = a0*(p0*cos(ta0)-q0*sin(ta0)); + y0 = a0*(p0*sin(ta0)+q0*cos(ta0)); + p1 = ((((0.10632e-4*t2-0.50363e-4)*t2+0.145575e-3)*t2 + -0.559487e-3)*t2+0.7323931e-2)*t2+1.000000004; + q1 = t*(((((-0.9173e-5*t2+0.40658e-4)*t2-0.99941e-4)*t2 + +0.266891e-3)*t2-0.1601836e-2)*t2+0.093749994); + ta1 = x-0.75*M_PI; + j1 = a0*(p1*cos(ta1)-q1*sin(ta1)); + y1 = a0*(p1*sin(ta1)+q1*cos(ta1)); + } + j0p = -j1; + j1p = j0-j1/x; + y0p = -y1; + y1p = y0-y1/x; + return 0; +} +template +int msta1(P x,int mp) +{ + P a0,f0,f1,f; + int i,n0,n1,nn; + + a0 = fabs(x); + n0 = (int)(1.1*a0)+1; + f0 = 0.5*log10(6.28*n0)-n0*log10(1.36*a0/n0)-mp; + n1 = n0+5; + f1 = 0.5*log10(6.28*n1)-n1*log10(1.36*a0/n1)-mp; + for (i=0;i<20;i++) { + nn = (int)(n1-(n1-n0)/(1.0-f0/f1)); + f = 0.5*log10(6.28*nn)-nn*log10(1.36*a0/nn)-mp; + if (abs(nn-n1) < 1) break; + n0 = n1; + f0 = f1; + n1 = nn; + f1 = f; + } + return nn; +} +template +int msta2(P x,int n,int mp) +{ + P a0,ejn,hmp,f0,f1,f,obj; + int i,n0,n1,nn; + + a0 = fabs(x); + hmp = 0.5*mp; + ejn = 0.5*log10(6.28*n)-n*log10(1.36*a0/n); + if (ejn <= hmp) { + obj = mp; + n0 = (int)(1.1*a0); + if (n0 < 1) n0 = 1; + } + else { + obj = hmp+ejn; + n0 = n; + } + f0 = 0.5*log10(6.28*n0)-n0*log10(1.36*a0/n0)-obj; + n1 = n0+5; + f1 = 0.5*log10(6.28*n1)-n1*log10(1.36*a0/n1)-obj; + for (i=0;i<20;i++) { + nn = (int)(n1-(n1-n0)/(1.0-f0/f1)); + f = 0.5*log10(6.28*nn)-nn*log10(1.36*a0/nn)-obj; + if (abs(nn-n1) < 1) break; + n0 = n1; + f0 = f1; + n1 = nn; + f1 = f; + } + return nn+10; +} +// +// INPUT: +// double x -- argument of Bessel function of 1st and 2nd kind. +// int n -- order +// +// OUPUT: +// +// int nm -- highest order actually computed (nm <= n) +// double jn[] -- Bessel function of 1st kind, orders from 0 to nm +// double yn[] -- Bessel function of 2nd kind, orders from 0 to nm +// double j'n[]-- derivative of Bessel function of 1st kind, +// orders from 0 to nm +// double y'n[]-- derivative of Bessel function of 2nd kind, +// orders from 0 to nm +// +// Computes Bessel functions of all order up to 'n' using recurrence +// relations. If 'nm' < 'n' only 'nm' orders are returned. +// +template +int bessjyna(int n,P x,int &nm,P *jn,P *yn, + P *jnp,P *ynp) +{ + P bj0,bj1,f,f0,f1,f2,cs; + int i,k,m,ecode; + + nm = n; + if ((x < 0.0) || (n < 0)) return 1; + if (x < 1e-15) { + for (i=0;i<=n;i++) { + jn[i] = 0.0; + yn[i] = -1e308; + jnp[i] = 0.0; + ynp[i] = 1e308; + } + jn[0] = 1.0; + jnp[1] = 0.5; + return 0; + } + ecode = bessjy01a(x,jn[0],jn[1],yn[0],yn[1],jnp[0],jnp[1],ynp[0],ynp[1]); + if (n < 2) return 0; + bj0 = jn[0]; + bj1 = jn[1]; + if (n < (int)0.9*x) { + for (k=2;k<=n;k++) { + jn[k] = 2.0*(k-1.0)*bj1/x-bj0; + bj0 = bj1; + bj1 = jn[k]; + } + } + else { + m = msta1(x,200); + if (m < n) nm = m; + else m = msta2(x,n,15); + f2 = 0.0; + f1 = 1.0e-100; + for (k=m;k>=0;k--) { + f = 2.0*(k+1.0)/x*f1-f2; + if (k <= nm) jn[k] = f; + f2 = f1; + f1 = f; + } + if (fabs(bj0) > fabs(bj1)) cs = bj0/f; + else cs = bj1/f2; + for (k=0;k<=nm;k++) { + jn[k] *= cs; + } + } + for (k=2;k<=nm;k++) { + jnp[k] = jn[k-1]-k*jn[k]/x; + } + f0 = yn[0]; + f1 = yn[1]; + for (k=2;k<=nm;k++) { + f = 2.0*(k-1.0)*f1/x-f0; + yn[k] = f; + f0 = f1; + f1 = f; + } + for (k=2;k<=nm;k++) { + ynp[k] = yn[k-1]-k*yn[k]/x; + } + return 0; +} +// +// Same input and output conventions as above. Different recurrence +// relations used for 'x' < 300. +// +template +int bessjynb(int n,P x,int &nm,P *jn,P *yn, + P *jnp,P *ynp) +{ + P t1,t2,f,f1,f2,bj0,bj1,bjk,by0,by1,cu,s0,su,sv; + P ec,bs,byk,p0,p1,q0,q1; + static P a[] = { + -0.7031250000000000e-1, + 0.1121520996093750, + -0.5725014209747314, + 6.074042001273483}; + static P b[] = { + 0.7324218750000000e-1, + -0.2271080017089844, + 1.727727502584457, + -2.438052969955606e1}; + static P a1[] = { + 0.1171875, + -0.1441955566406250, + 0.6765925884246826, + -6.883914268109947}; + static P b1[] = { + -0.1025390625, + 0.2775764465332031, + -1.993531733751297, + 2.724882731126854e1}; + + int i,k,m; + nm = n; + if ((x < 0.0) || (n < 0)) return 1; + if (x < 1e-15) { + for (i=0;i<=n;i++) { + jn[i] = 0.0; + yn[i] = -1e308; + jnp[i] = 0.0; + ynp[i] = 1e308; + } + jn[0] = 1.0; + jnp[1] = 0.5; + return 0; + } + if (x <= 300.0 || n > (int)(0.9*x)) { + if (n == 0) nm = 1; + m = msta1(x,200); + if (m < nm) nm = m; + else m = msta2(x,nm,15); + bs = 0.0; + su = 0.0; + sv = 0.0; + f2 = 0.0; + f1 = 1.0e-100; + for (k = m;k>=0;k--) { + f = 2.0*(k+1.0)/x*f1 - f2; + if (k <= nm) jn[k] = f; + if ((k == 2*(int)(k/2)) && (k != 0)) { + bs += 2.0*f; +// su += pow(-1,k>>1)*f/(double)k; + su += (-1)*((k & 2)-1)*f/(P)k; + } + else if (k > 1) { +// sv += pow(-1,k>>1)*k*f/(k*k-1.0); + sv += (-1)*((k & 2)-1)*(P)k*f/(k*k-1.0); + } + f2 = f1; + f1 = f; + } + s0 = bs+f; + for (k=0;k<=nm;k++) { + jn[k] /= s0; + } + ec = log(0.5*x) +0.5772156649015329; + by0 = M_2_PI*(ec*jn[0]-4.0*su/s0); + yn[0] = by0; + by1 = M_2_PI*((ec-1.0)*jn[1]-jn[0]/x-4.0*sv/s0); + yn[1] = by1; + } + else { + t1 = x-M_PI_4; + p0 = 1.0; + q0 = -0.125/x; + for (k=0;k<4;k++) { + p0 += a[k]*pow(x,-2*k-2); + q0 += b[k]*pow(x,-2*k-3); + } + cu = sqrt(M_2_PI/x); + bj0 = cu*(p0*cos(t1)-q0*sin(t1)); + by0 = cu*(p0*sin(t1)+q0*cos(t1)); + jn[0] = bj0; + yn[0] = by0; + t2 = x-0.75*M_PI; + p1 = 1.0; + q1 = 0.375/x; + for (k=0;k<4;k++) { + p1 += a1[k]*pow(x,-2*k-2); + q1 += b1[k]*pow(x,-2*k-3); + } + bj1 = cu*(p1*cos(t2)-q1*sin(t2)); + by1 = cu*(p1*sin(t2)+q1*cos(t2)); + jn[1] = bj1; + yn[1] = by1; + for (k=2;k<=nm;k++) { + bjk = 2.0*(k-1.0)*bj1/x-bj0; + jn[k] = bjk; + bj0 = bj1; + bj1 = bjk; + } + } + jnp[0] = -jn[1]; + for (k=1;k<=nm;k++) { + jnp[k] = jn[k-1]-k*jn[k]/x; + } + for (k=2;k<=nm;k++) { + byk = 2.0*(k-1.0)*by1/x-by0; + yn[k] = byk; + by0 = by1; + by1 = byk; + } + ynp[0] = -yn[1]; + for (k=1;k<=nm;k++) { + ynp[k] = yn[k-1]-k*yn[k]/x; + } + return 0; + +} + +// The following routine computes Bessel Jv(x) and Yv(x) for +// arbitrary positive order (v). For negative order, use: +// +// J-v(x) = Jv(x)cos(v pi) - Yv(x)sin(v pi) +// Y-v(x) = Jv(x)sin(v pi) + Yv(x)cos(v pi) +// +template +int bessjyv(P v,P x,P &vm,P *jv,P *yv, + P *djv,P *dyv) +{ + P v0,vl,vg,vv,a,a0,r,x2,bjv0,bjv1,bjvl,f,f0,f1,f2; + P r0,r1,ck,cs,cs0,cs1,sk,qx,px,byv0,byv1,rp,xk,rq; + P b,ec,w0,w1,bju0,bju1,pv0,pv1,byvk; + int j,k,l,m,n,kz; + + x2 = x*x; + n = (int)v; + v0 = v-n; + if ((x < 0.0) || (v < 0.0)) return 1; + if (x < 1e-15) { + for (k=0;k<=n;k++) { + jv[k] = 0.0; + yv[k] = -1e308; + djv[k] = 0.0; + dyv[k] = 1e308; + if (v0 == 0.0) { + jv[0] = 1.0; + djv[1] = 0.5; + } + else djv[0] = 1e308; + } + vm = v; + return 0; + } + if (x <= 12.0) { + for (l=0;l<2;l++) { + vl = v0 + l; + bjvl = 1.0; + r = 1.0; + for (k=1;k<=40;k++) { + r *= -0.25*x2/(k*(k+vl)); + bjvl += r; + if (fabs(r) < fabs(bjvl)*1e-15) break; + } + vg = 1.0 + vl; + a = pow(0.5*x,vl)/gamma(vg); + if (l == 0) bjv0 = bjvl*a; + else bjv1 = bjvl*a; + } + } + else { + if (x >= 50.0) kz = 8; + else if (x >= 35.0) kz = 10; + else kz = 11; + for (j=0;j<2;j++) { + vv = 4.0*(j+v0)*(j+v0); + px = 1.0; + rp = 1.0; + for (k=1;k<=kz;k++) { + rp *= (-0.78125e-2)*(vv-pow(4.0*k-3.0,2.0))* + (vv-pow(4.0*k-1.0,2.0))/(k*(2.0*k-1.0)*x2); + px += rp; + } + qx = 1.0; + rq = 1.0; + for (k=1;k<=kz;k++) { + rq *= (-0.78125e-2)*(vv-pow(4.0*k-1.0,2.0))* + (vv-pow(4.0*k+1.0,2.0))/(k*(2.0*k+1.0)*x2); + qx += rq; + } + qx *= 0.125*(vv-1.0)/x; + xk = x-(0.5*(j+v0)+0.25)*M_PI; + a0 = sqrt(M_2_PI/x); + ck = cos(xk); + sk = sin(xk); + + if (j == 0) { + bjv0 = a0*(px*ck-qx*sk); + byv0 = a0*(px*sk+qx*ck); + } + else if (j == 1) { + bjv1 = a0*(px*ck-qx*sk); + byv1 = a0*(px*sk+qx*ck); + } + } + } + jv[0] = bjv0; + jv[1] = bjv1; + djv[0] = v0*jv[0]/x-jv[1]; + djv[1] = -(1.0+v0)*jv[1]/x+jv[0]; + if ((n >= 2) && (n <= (int)(0.9*x))) { + f0 = bjv0; + f1 = bjv1; + for (k=2;k<=n;k++) { + f = 2.0*(k+v0-1.0)*f1/x-f0; + jv[k] = f; + f0 = f1; + f1 = f; + } + } + else if (n >= 2) { + m = msta1(x,200); + if (m < n) n = m; + else m = msta2(x,n,15); + f2 = 0.0; + f1 = 1.0e-100; + for (k=m;k>=0;k--) { + f = 2.0*(v0+k+1.0)*f1/x-f2; + if (k <= n) jv[k] = f; + f2 = f1; + f1 = f; + } + if (fabs(bjv0) > fabs(bjv1)) cs = bjv0/f; + else cs = bjv1/f2; + for (k=0;k<=n;k++) { + jv[k] *= cs; + } + } + for (k=2;k<=n;k++) { + djv[k] = -(k+v0)*jv[k]/x+jv[k-1]; + } + if (x <= 12.0) { + if (v0 != 0.0) { + for (l=0;l<2;l++) { + vl = v0 +l; + bjvl = 1.0; + r = 1.0; + for (k=1;k<=40;k++) { + r *= -0.25*x2/(k*(k-vl)); + bjvl += r; + if (fabs(r) < fabs(bjvl)*1e-15) break; + } + vg = 1.0-vl; + b = pow(2.0/x,vl)/gamma(vg); + if (l == 0) bju0 = bjvl*b; + else bju1 = bjvl*b; + } + pv0 = M_PI*v0; + pv1 = M_PI*(1.0+v0); + byv0 = (bjv0*cos(pv0)-bju0)/sin(pv0); + byv1 = (bjv1*cos(pv1)-bju1)/sin(pv1); + } + else { + ec = log(0.5*x)+el; + cs0 = 0.0; + w0 = 0.0; + r0 = 1.0; + for (k=1;k<=30;k++) { + w0 += 1.0/k; + r0 *= -0.25*x2/(k*k); + cs0 += r0*w0; + } + byv0 = M_2_PI*(ec*bjv0-cs0); + cs1 = 1.0; + w1 = 0.0; + r1 = 1.0; + for (k=1;k<=30;k++) { + w1 += 1.0/k; + r1 *= -0.25*x2/(k*(k+1)); + cs1 += r1*(2.0*w1+1.0/(k+1.0)); + } + byv1 = M_2_PI*(ec*bjv1-1.0/x-0.25*x*cs1); + } + } + yv[0] = byv0; + yv[1] = byv1; + for (k=2;k<=n;k++) { + byvk = 2.0*(v0+k-1.0)*byv1/x-byv0; + yv[k] = byvk; + byv0 = byv1; + byv1 = byvk; + } + dyv[0] = v0*yv[0]/x-yv[1]; + for (k=1;k<=n;k++) { + dyv[k] = -(k+v0)*yv[k]/x+yv[k-1]; + } + vm = n + v0; + return 0; +} + +template +int bessjyv_sph(int v, P z, P &vm, P* cjv, + P* cyv, P* cjvp, P* cyvp) +{ + //first, compute the bessel functions of fractional order + bessjyv(v + 0.5, z, vm, cjv, cyv, cjvp, cyvp); + + //iterate through each and scale + for(int n = 0; n<=v; n++) + { + + cjv[n] = cjv[n] * sqrt(rtsPI/(z * 2.0)); + cyv[n] = cyv[n] * sqrt(rtsPI/(z * 2.0)); + + cjvp[n] = -1.0 / (z * 2.0) * cjv[n] + cjvp[n] * sqrt(rtsPI / (z * 2.0)); + cyvp[n] = -1.0 / (z * 2.0) * cyv[n] + cyvp[n] * sqrt(rtsPI / (z * 2.0)); + } + + return 0; + +} + +template +int cbessjy01(complex

z,complex

&cj0,complex

&cj1, + complex

&cy0,complex

&cy1,complex

&cj0p, + complex

&cj1p,complex

&cy0p,complex

&cy1p) +{ + complex

z1,z2,cr,cp,cs,cp0,cq0,cp1,cq1,ct1,ct2,cu; + P a0,w0,w1; + int k,kz; + + static P a[] = { + -7.03125e-2, + 0.112152099609375, + -0.5725014209747314, + 6.074042001273483, + -1.100171402692467e2, + 3.038090510922384e3, + -1.188384262567832e5, + 6.252951493434797e6, + -4.259392165047669e8, + 3.646840080706556e10, + -3.833534661393944e12, + 4.854014686852901e14, + -7.286857349377656e16, + 1.279721941975975e19}; + static P b[] = { + 7.32421875e-2, + -0.2271080017089844, + 1.727727502584457, + -2.438052969955606e1, + 5.513358961220206e2, + -1.825775547429318e4, + 8.328593040162893e5, + -5.006958953198893e7, + 3.836255180230433e9, + -3.649010818849833e11, + 4.218971570284096e13, + -5.827244631566907e15, + 9.476288099260110e17, + -1.792162323051699e20}; + static P a1[] = { + 0.1171875, + -0.1441955566406250, + 0.6765925884246826, + -6.883914268109947, + 1.215978918765359e2, + -3.302272294480852e3, + 1.276412726461746e5, + -6.656367718817688e6, + 4.502786003050393e8, + -3.833857520742790e10, + 4.011838599133198e12, + -5.060568503314727e14, + 7.572616461117958e16, + -1.326257285320556e19}; + static P b1[] = { + -0.1025390625, + 0.2775764465332031, + -1.993531733751297, + 2.724882731126854e1, + -6.038440767050702e2, + 1.971837591223663e4, + -8.902978767070678e5, + 5.310411010968522e7, + -4.043620325107754e9, + 3.827011346598605e11, + -4.406481417852278e13, + 6.065091351222699e15, + -9.833883876590679e17, + 1.855045211579828e20}; + + a0 = abs(z); + z2 = z*z; + z1 = z; + if (a0 == 0.0) { + cj0 = cone; + cj1 = czero; + cy0 = complex

(-1e308,0); + cy1 = complex

(-1e308,0); + cj0p = czero; + cj1p = complex

(0.5,0.0); + cy0p = complex

(1e308,0); + cy1p = complex

(1e308,0); + return 0; + } + if (real(z) < 0.0) z1 = -z; + if (a0 <= 12.0) { + cj0 = cone; + cr = cone; + for (k=1;k<=40;k++) { + cr *= -0.25*z2/(P)(k*k); + cj0 += cr; + if (abs(cr) < abs(cj0)*eps) break; + } + cj1 = cone; + cr = cone; + for (k=1;k<=40;k++) { + cr *= -0.25*z2/(k*(k+1.0)); + cj1 += cr; + if (abs(cr) < abs(cj1)*eps) break; + } + cj1 *= 0.5*z1; + w0 = 0.0; + cr = cone; + cs = czero; + for (k=1;k<=40;k++) { + w0 += 1.0/k; + cr *= -0.25*z2/(P)(k*k); + cp = cr*w0; + cs += cp; + if (abs(cp) < abs(cs)*eps) break; + } + cy0 = M_2_PI*((log(0.5*z1)+el)*cj0-cs); + w1 = 0.0; + cr = cone; + cs = cone; + for (k=1;k<=40;k++) { + w1 += 1.0/k; + cr *= -0.25*z2/(k*(k+1.0)); + cp = cr*(2.0*w1+1.0/(k+1.0)); + cs += cp; + if (abs(cp) < abs(cs)*eps) break; + } + cy1 = M_2_PI*((log(0.5*z1)+el)*cj1-1.0/z1-0.25*z1*cs); + } + else { + if (a0 >= 50.0) kz = 8; // can be changed to 10 + else if (a0 >= 35.0) kz = 10; // " " " 12 + else kz = 12; // " " " 14 + ct1 = z1 - M_PI_4; + cp0 = cone; + for (k=0;k 0.0) { + cy0 += 2.0*cii*cj0; + cy1 = -(cy1+2.0*cii*cj1); + } + cj1 = -cj1; + } + cj0p = -cj1; + cj1p = cj0-cj1/z; + cy0p = -cy1; + cy1p = cy0-cy1/z; + return 0; +} + +template +int cbessjyna(int n,complex

z,int &nm,complex

*cj, + complex

*cy,complex

*cjp,complex

*cyp) +{ + complex

cbj0,cbj1,cby0,cby1,cj0,cjk,cj1,cf,cf1,cf2; + complex

cs,cg0,cg1,cyk,cyl1,cyl2,cylk,cp11,cp12,cp21,cp22; + complex

ch0,ch1,ch2; + P a0,yak,ya1,ya0,wa; + int m,k,lb,lb0; + + if (n < 0) return 1; + a0 = abs(z); + nm = n; + if (a0 < 1.0e-100) { + for (k=0;k<=n;k++) { + cj[k] = czero; + cy[k] = complex

(-1e308,0); + cjp[k] = czero; + cyp[k] = complex

(1e308,0); + } + cj[0] = cone; + cjp[1] = complex

(0.5,0.0); + return 0; + } + cbessjy01(z,cj[0],cj[1],cy[0],cy[1],cjp[0],cjp[1],cyp[0],cyp[1]); + cbj0 = cj[0]; + cbj1 = cj[1]; + cby0 = cy[0]; + cby1 = cy[1]; + if (n <= 1) return 0; + if (n < (int)0.25*a0) { + cj0 = cbj0; + cj1 = cbj1; + for (k=2;k<=n;k++) { + cjk = 2.0*(k-1.0)*cj1/z-cj0; + cj[k] = cjk; + cj0 = cj1; + cj1 = cjk; + } + } + else { + m = msta1(a0,200); + if (m < n) nm = m; + else m = msta2(a0,n,15); + cf2 = czero; + cf1 = complex

(1.0e-100,0.0); + for (k=m;k>=0;k--) { + cf = 2.0*(k+1.0)*cf1/z-cf2; + if (k <=nm) cj[k] = cf; + cf2 = cf1; + cf1 = cf; + } + if (abs(cbj0) > abs(cbj1)) cs = cbj0/cf; + else cs = cbj1/cf2; + for (k=0;k<=nm;k++) { + cj[k] *= cs; + } + } + for (k=2;k<=nm;k++) { + cjp[k] = cj[k-1]-(P)k*cj[k]/z; + } + ya0 = abs(cby0); + lb = 0; + cg0 = cby0; + cg1 = cby1; + for (k=2;k<=nm;k++) { + cyk = 2.0*(k-1.0)*cg1/z-cg0; + yak = abs(cyk); + ya1 = abs(cg0); + if ((yak < ya0) && (yak < ya1)) lb = k; + cy[k] = cyk; + cg0 = cg1; + cg1 = cyk; + } + lb0 = 0; + if ((lb > 4) && (imag(z) != 0.0)) { + while (lb != lb0) { + ch2 = cone; + ch1 = czero; + lb0 = lb; + for (k=lb;k>=1;k--) { + ch0 = 2.0*k*ch1/z-ch2; + ch2 = ch1; + ch1 = ch0; + } + cp12 = ch0; + cp22 = ch2; + ch2 = czero; + ch1 = cone; + for (k=lb;k>=1;k--) { + ch0 = 2.0*k*ch1/z-ch2; + ch2 = ch1; + ch1 = ch0; + } + cp11 = ch0; + cp21 = ch2; + if (lb == nm) + cj[lb+1] = 2.0*lb*cj[lb]/z-cj[lb-1]; + if (abs(cj[0]) > abs(cj[1])) { + cy[lb+1] = (cj[lb+1]*cby0-2.0*cp11/(M_PI*z))/cj[0]; + cy[lb] = (cj[lb]*cby0+2.0*cp12/(M_PI*z))/cj[0]; + } + else { + cy[lb+1] = (cj[lb+1]*cby1-2.0*cp21/(M_PI*z))/cj[1]; + cy[lb] = (cj[lb]*cby1+2.0*cp22/(M_PI*z))/cj[1]; + } + cyl2 = cy[lb+1]; + cyl1 = cy[lb]; + for (k=lb-1;k>=0;k--) { + cylk = 2.0*(k+1.0)*cyl1/z-cyl2; + cy[k] = cylk; + cyl2 = cyl1; + cyl1 = cylk; + } + cyl1 = cy[lb]; + cyl2 = cy[lb+1]; + for (k=lb+1;k +int cbessjynb(int n,complex

z,int &nm,complex

*cj, + complex

*cy,complex

*cjp,complex

*cyp) +{ + complex

cf,cf0,cf1,cf2,cbs,csu,csv,cs0,ce; + complex

ct1,cp0,cq0,cp1,cq1,cu,cbj0,cby0,cbj1,cby1; + complex

cyy,cbjk,ct2; + P a0,y0; + int k,m; + static P a[] = { + -0.7031250000000000e-1, + 0.1121520996093750, + -0.5725014209747314, + 6.074042001273483}; + static P b[] = { + 0.7324218750000000e-1, + -0.2271080017089844, + 1.727727502584457, + -2.438052969955606e1}; + static P a1[] = { + 0.1171875, + -0.1441955566406250, + 0.6765925884246826, + -6.883914268109947}; + static P b1[] = { + -0.1025390625, + 0.2775764465332031, + -1.993531733751297, + 2.724882731126854e1}; + + y0 = abs(imag(z)); + a0 = abs(z); + nm = n; + if (a0 < 1.0e-100) { + for (k=0;k<=n;k++) { + cj[k] = czero; + cy[k] = complex

(-1e308,0); + cjp[k] = czero; + cyp[k] = complex

(1e308,0); + } + cj[0] = cone; + cjp[1] = complex

(0.5,0.0); + return 0; + } + if ((a0 <= 300.0) || (n > (int)(0.25*a0))) { + if (n == 0) nm = 1; + m = msta1(a0,200); + if (m < nm) nm = m; + else m = msta2(a0,nm,15); + cbs = czero; + csu = czero; + csv = czero; + cf2 = czero; + cf1 = complex

(1.0e-100,0.0); + for (k=m;k>=0;k--) { + cf = 2.0*(k+1.0)*cf1/z-cf2; + if (k <= nm) cj[k] = cf; + if (((k & 1) == 0) && (k != 0)) { + if (y0 <= 1.0) { + cbs += 2.0*cf; + } + else { + cbs += (-1)*((k & 2)-1)*2.0*cf; + } + csu += (P)((-1)*((k & 2)-1))*cf/(P)k; + } + else if (k > 1) { + csv += (P)((-1)*((k & 2)-1)*k)*cf/(P)(k*k-1.0); + } + cf2 = cf1; + cf1 = cf; + } + if (y0 <= 1.0) cs0 = cbs+cf; + else cs0 = (cbs+cf)/cos(z); + for (k=0;k<=nm;k++) { + cj[k] /= cs0; + } + ce = log(0.5*z)+el; + cy[0] = M_2_PI*(ce*cj[0]-4.0*csu/cs0); + cy[1] = M_2_PI*(-cj[0]/z+(ce-1.0)*cj[1]-4.0*csv/cs0); + } + else { + ct1 = z-M_PI_4; + cp0 = cone; + for (k=0;k<4;k++) { + cp0 += a[k]*pow(z,-2.0*k-2.0); + } + cq0 = -0.125/z; + for (k=0;k<4;k++) { + cq0 += b[k] *pow(z,-2.0*k-3.0); + } + cu = sqrt(M_2_PI/z); + cbj0 = cu*(cp0*cos(ct1)-cq0*sin(ct1)); + cby0 = cu*(cp0*sin(ct1)+cq0*cos(ct1)); + cj[0] = cbj0; + cy[0] = cby0; + ct2 = z-0.75*M_PI; + cp1 = cone; + for (k=0;k<4;k++) { + cp1 += a1[k]*pow(z,-2.0*k-2.0); + } + cq1 = 0.375/z; + for (k=0;k<4;k++) { + cq1 += b1[k]*pow(z,-2.0*k-3.0); + } + cbj1 = cu*(cp1*cos(ct2)-cq1*sin(ct2)); + cby1 = cu*(cp1*sin(ct2)+cq1*cos(ct2)); + cj[1] = cbj1; + cy[1] = cby1; + for (k=2;k<=n;k++) { + cbjk = 2.0*(k-1.0)*cbj1/z-cbj0; + cj[k] = cbjk; + cbj0 = cbj1; + cbj1 = cbjk; + } + } + cjp[0] = -cj[1]; + for (k=1;k<=nm;k++) { + cjp[k] = cj[k-1]-(P)k*cj[k]/z; + } + if (abs(cj[0]) > 1.0) + cy[1] = (cj[1]*cy[0]-2.0/(M_PI*z))/cj[0]; + for (k=2;k<=nm;k++) { + if (abs(cj[k-1]) >= abs(cj[k-2])) + cyy = (cj[k]*cy[k-1]-2.0/(M_PI*z))/cj[k-1]; + else + cyy = (cj[k]*cy[k-2]-4.0*(k-1.0)/(M_PI*z*z))/cj[k-2]; + cy[k] = cyy; + } + cyp[0] = -cy[1]; + for (k=1;k<=nm;k++) { + cyp[k] = cy[k-1]-(P)k*cy[k]/z; + } + + return 0; +} + +template +int cbessjyva(P v,complex

z,P &vm,complex

*cjv, + complex

*cyv,complex

*cjvp,complex

*cyvp) +{ + complex

z1,z2,zk,cjvl,cr,ca,cjv0,cjv1,cpz,crp; + complex

cqz,crq,ca0,cck,csk,cyv0,cyv1,cju0,cju1,cb; + complex

cs,cs0,cr0,cs1,cr1,cec,cf,cf0,cf1,cf2; + complex

cfac0,cfac1,cg0,cg1,cyk,cp11,cp12,cp21,cp22; + complex

ch0,ch1,ch2,cyl1,cyl2,cylk; + + P a0,v0,pv0,pv1,vl,ga,gb,vg,vv,w0,w1,ya0,yak,ya1,wa; + int j,n,k,kz,l,lb,lb0,m; + + a0 = abs(z); + z1 = z; + z2 = z*z; + n = (int)v; + + + v0 = v-n; + + pv0 = M_PI*v0; + pv1 = M_PI*(1.0+v0); + if (a0 < 1.0e-100) { + for (k=0;k<=n;k++) { + cjv[k] = czero; + cyv[k] = complex

(-1e308,0); + cjvp[k] = czero; + cyvp[k] = complex

(1e308,0); + + } + if (v0 == 0.0) { + cjv[0] = cone; + cjvp[1] = complex

(0.5,0.0); + } + else { + cjvp[0] = complex

(1e308,0); + } + vm = v; + return 0; + } + if (real(z1) < 0.0) z1 = -z; + if (a0 <= 12.0) { + for (l=0;l<2;l++) { + vl = v0+l; + cjvl = cone; + cr = cone; + for (k=1;k<=40;k++) { + cr *= -0.25*z2/(k*(k+vl)); + cjvl += cr; + if (abs(cr) < abs(cjvl)*eps) break; + } + vg = 1.0 + vl; + ga = gamma(vg); + ca = pow(0.5*z1,vl)/ga; + if (l == 0) cjv0 = cjvl*ca; + else cjv1 = cjvl*ca; + } + } + else { + if (a0 >= 50.0) kz = 8; + else if (a0 >= 35.0) kz = 10; + else kz = 11; + for (j=0;j<2;j++) { + vv = 4.0*(j+v0)*(j+v0); + cpz = cone; + crp = cone; + for (k=1;k<=kz;k++) { + crp = -0.78125e-2*crp*(vv-pow(4.0*k-3.0,2.0))* + (vv-pow(4.0*k-1.0,2.0))/(k*(2.0*k-1.0)*z2); + cpz += crp; + } + cqz = cone; + crq = cone; + for (k=1;k<=kz;k++) { + crq = -0.78125e-2*crq*(vv-pow(4.0*k-1.0,2.0))* + (vv-pow(4.0*k+1.0,2.0))/(k*(2.0*k+1.0)*z2); + cqz += crq; + } + cqz *= 0.125*(vv-1.0)/z1; + zk = z1-(0.5*(j+v0)+0.25)*M_PI; + ca0 = sqrt(M_2_PI/z1); + cck = cos(zk); + csk = sin(zk); + if (j == 0) { + cjv0 = ca0*(cpz*cck-cqz*csk); + cyv0 = ca0*(cpz*csk+cqz+cck); + } + else { + cjv1 = ca0*(cpz*cck-cqz*csk); + cyv1 = ca0*(cpz*csk+cqz*cck); + } + } + } + if (a0 <= 12.0) { + if (v0 != 0.0) { + for (l=0;l<2;l++) { + vl = v0+l; + cjvl = cone; + cr = cone; + for (k=1;k<=40;k++) { + cr *= -0.25*z2/(k*(k-vl)); + cjvl += cr; + if (abs(cr) < abs(cjvl)*eps) break; + } + vg = 1.0-vl; + gb = gamma(vg); + cb = pow(2.0/z1,vl)/gb; + if (l == 0) cju0 = cjvl*cb; + else cju1 = cjvl*cb; + } + cyv0 = (cjv0*cos(pv0)-cju0)/sin(pv0); + cyv1 = (cjv1*cos(pv1)-cju1)/sin(pv1); + } + else { + cec = log(0.5*z1)+el; + cs0 = czero; + w0 = 0.0; + cr0 = cone; + for (k=1;k<=30;k++) { + w0 += 1.0/k; + cr0 *= -0.25*z2/(P)(k*k); + cs0 += cr0*w0; + } + cyv0 = M_2_PI*(cec*cjv0-cs0); + cs1 = cone; + w1 = 0.0; + cr1 = cone; + for (k=1;k<=30;k++) { + w1 += 1.0/k; + cr1 *= -0.25*z2/(k*(k+1.0)); + cs1 += cr1*(2.0*w1+1.0/(k+1.0)); + } + cyv1 = M_2_PI*(cec*cjv1-1.0/z1-0.25*z1*cs1); + } + } + if (real(z) < 0.0) { + cfac0 = exp(pv0*cii); + cfac1 = exp(pv1*cii); + if (imag(z) < 0.0) { + cyv0 = cfac0*cyv0-(P)2.0*(complex

)cii*cos(pv0)*cjv0; + cyv1 = cfac1*cyv1-(P)2.0*(complex

)cii*cos(pv1)*cjv1; + cjv0 /= cfac0; + cjv1 /= cfac1; + } + else if (imag(z) > 0.0) { + cyv0 = cyv0/cfac0+(P)2.0*(complex

)cii*cos(pv0)*cjv0; + cyv1 = cyv1/cfac1+(P)2.0*(complex

)cii*cos(pv1)*cjv1; + cjv0 *= cfac0; + cjv1 *= cfac1; + } + } + cjv[0] = cjv0; + cjv[1] = cjv1; + if ((n >= 2) && (n <= (int)(0.25*a0))) { + cf0 = cjv0; + cf1 = cjv1; + for (k=2;k<= n;k++) { + cf = 2.0*(k+v0-1.0)*cf1/z-cf0; + cjv[k] = cf; + cf0 = cf1; + cf1 = cf; + } + } + else if (n >= 2) { + m = msta1(a0,200); + if (m < n) n = m; + else m = msta2(a0,n,15); + cf2 = czero; + cf1 = complex

(1.0e-100,0.0); + for (k=m;k>=0;k--) { + cf = 2.0*(v0+k+1.0)*cf1/z-cf2; + if (k <= n) cjv[k] = cf; + cf2 = cf1; + cf1 = cf; + } + if (abs(cjv0) > abs(cjv1)) cs = cjv0/cf; + else cs = cjv1/cf2; + for (k=0;k<=n;k++) { + cjv[k] *= cs; + } + } + cjvp[0] = v0*cjv[0]/z-cjv[1]; + for (k=1;k<=n;k++) { + cjvp[k] = -(k+v0)*cjv[k]/z+cjv[k-1]; + } + cyv[0] = cyv0; + cyv[1] = cyv1; + ya0 = abs(cyv0); + lb = 0; + cg0 = cyv0; + cg1 = cyv1; + for (k=2;k<=n;k++) { + cyk = 2.0*(v0+k-1.0)*cg1/z-cg0; + yak = abs(cyk); + ya1 = abs(cg0); + if ((yak < ya0) && (yak< ya1)) lb = k; + cyv[k] = cyk; + cg0 = cg1; + cg1 = cyk; + } + lb0 = 0; + if ((lb > 4) && (imag(z) != 0.0)) { + while(lb != lb0) { + ch2 = cone; + ch1 = czero; + lb0 = lb; + for (k=lb;k>=1;k--) { + ch0 = 2.0*(k+v0)*ch1/z-ch2; + ch2 = ch1; + ch1 = ch0; + } + cp12 = ch0; + cp22 = ch2; + ch2 = czero; + ch1 = cone; + for (k=lb;k>=1;k--) { + ch0 = 2.0*(k+v0)*ch1/z-ch2; + ch2 = ch1; + ch1 = ch0; + } + cp11 = ch0; + cp21 = ch2; + if (lb == n) + cjv[lb+1] = 2.0*(lb+v0)*cjv[lb]/z-cjv[lb-1]; + if (abs(cjv[0]) > abs(cjv[1])) { + cyv[lb+1] = (cjv[lb+1]*cyv0-2.0*cp11/(M_PI*z))/cjv[0]; + cyv[lb] = (cjv[lb]*cyv0+2.0*cp12/(M_PI*z))/cjv[0]; + } + else { + cyv[lb+1] = (cjv[lb+1]*cyv1-2.0*cp21/(M_PI*z))/cjv[1]; + cyv[lb] = (cjv[lb]*cyv1+2.0*cp22/(M_PI*z))/cjv[1]; + } + cyl2 = cyv[lb+1]; + cyl1 = cyv[lb]; + for (k=lb-1;k>=0;k--) { + cylk = 2.0*(k+v0+1.0)*cyl1/z-cyl2; + cyv[k] = cylk; + cyl2 = cyl1; + cyl1 = cylk; + } + cyl1 = cyv[lb]; + cyl2 = cyv[lb+1]; + for (k=lb+1;k +int cbessjyva_sph(int v,complex

z,P &vm,complex

*cjv, + complex

*cyv,complex

*cjvp,complex

*cyvp) +{ + //first, compute the bessel functions of fractional order + cbessjyva

(v + 0.5, z, vm, cjv, cyv, cjvp, cyvp); + + //iterate through each and scale + for(int n = 0; n<=v; n++) + { + + cjv[n] = cjv[n] * sqrt(rtsPI/(z * 2.0)); + cyv[n] = cyv[n] * sqrt(rtsPI/(z * 2.0)); + + cjvp[n] = -1.0 / (z * 2.0) * cjv[n] + cjvp[n] * sqrt(rtsPI / (z * 2.0)); + cyvp[n] = -1.0 / (z * 2.0) * cyv[n] + cyvp[n] * sqrt(rtsPI / (z * 2.0)); + } + + return 0; + +} + +} //end namespace rts + + +#endif diff --git a/stim/math/complex.h b/stim/math/complex.h new file mode 100644 index 0000000..84ba67e --- /dev/null +++ b/stim/math/complex.h @@ -0,0 +1,505 @@ +/*RTS Complex number class. This class is CUDA compatible, +and can therefore be used in CUDA code and on CUDA devices. +*/ + +#ifndef RTS_COMPLEX +#define RTS_COMPLEX + +#include "../cuda/callable.h" +#include +#include +#include +#include + +namespace stim +{ + +template +struct complex +{ + T r, i; + + //default constructor + CUDA_CALLABLE complex() + { + r = 0; + i = 0; + } + + //constructor when given real and imaginary values + CUDA_CALLABLE complex(T r, T i = 0) + { + this->r = r; + this->i = i; + } + + //access methods + CUDA_CALLABLE T real() + { + return r; + } + + CUDA_CALLABLE T real(T r_val) + { + r = r_val; + return r_val; + } + + CUDA_CALLABLE T imag() + { + return i; + } + CUDA_CALLABLE T imag(T i_val) + { + i = i_val; + return i_val; + } + + + + //return the current value multiplied by i + CUDA_CALLABLE complex imul() + { + complex result; + result.r = -i; + result.i = r; + + return result; + } + + //returns the complex signum (-1, 0, 1) + CUDA_CALLABLE int sgn(){ + if(r > 0) return 1; + else if(r < 0) return -1; + else return (0 < i - i < 0); + } + + //ARITHMETIC OPERATORS-------------------- + + //binary + operator (returns the result of adding two complex values) + CUDA_CALLABLE complex operator+ (const complex rhs) const + { + complex result; + result.r = r + rhs.r; + result.i = i + rhs.i; + return result; + } + + CUDA_CALLABLE complex operator+ (const T rhs) const + { + complex result; + result.r = r + rhs; + result.i = i; + return result; + } + + //binary - operator (returns the result of adding two complex values) + CUDA_CALLABLE complex operator- (const complex rhs) const + { + complex result; + result.r = r - rhs.r; + result.i = i - rhs.i; + return result; + } + + //binary - operator (returns the result of adding two complex values) + CUDA_CALLABLE complex operator- (const T rhs) + { + complex result; + result.r = r - rhs; + result.i = i; + return result; + } + + //binary MULTIPLICATION operators (returns the result of multiplying complex values) + CUDA_CALLABLE complex operator* (const complex rhs) const + { + complex result; + result.r = r * rhs.r - i * rhs.i; + result.i = r * rhs.i + i * rhs.r; + return result; + } + CUDA_CALLABLE complex operator* (const T rhs) + { + return complex(r * rhs, i * rhs); + } + + //binary DIVISION operators (returns the result of dividing complex values) + CUDA_CALLABLE complex operator/ (const complex rhs) const + { + complex result; + T denom = rhs.r * rhs.r + rhs.i * rhs.i; + result.r = (r * rhs.r + i * rhs.i) / denom; + result.i = (- r * rhs.i + i * rhs.r) / denom; + + return result; + } + CUDA_CALLABLE complex operator/ (const T rhs) + { + return complex(r / rhs, i / rhs); + } + + //ASSIGNMENT operators----------------------------------- + CUDA_CALLABLE complex & operator=(const complex &rhs) + { + //check for self-assignment + if(this != &rhs) + { + this->r = rhs.r; + this->i = rhs.i; + } + return *this; + } + CUDA_CALLABLE complex & operator=(const T &rhs) + { + this->r = rhs; + this->i = 0; + + return *this; + } + + //arithmetic assignment operators + CUDA_CALLABLE complex operator+=(const complex &rhs) + { + *this = *this + rhs; + return *this; + } + CUDA_CALLABLE complex operator+=(const T &rhs) + { + *this = *this + rhs; + return *this; + } + + CUDA_CALLABLE complex operator-=(const complex &rhs) + { + *this = *this - rhs; + return *this; + } + CUDA_CALLABLE complex operator-=(const T &rhs) + { + *this = *this - rhs; + return *this; + } + + CUDA_CALLABLE complex operator*=(const complex &rhs) + { + *this = *this * rhs; + return *this; + } + CUDA_CALLABLE complex operator*=(const T &rhs) + { + *this = *this * rhs; + return *this; + } + //divide and assign + CUDA_CALLABLE complex operator/=(const complex &rhs) + { + *this = *this / rhs; + return *this; + } + CUDA_CALLABLE complex operator/=(const T &rhs) + { + *this = *this / rhs; + return *this; + } + + //absolute value operator (returns the absolute value of the complex number) + CUDA_CALLABLE T abs() + { + return std::sqrt(r * r + i * i); + } + + CUDA_CALLABLE complex log() + { + complex result; + result.r = (T)std::log(std::sqrt(r * r + i * i)); + result.i = (T)std::atan2(i, r); + + + return result; + } + + CUDA_CALLABLE complex exp() + { + complex result; + + T e_r = std::exp(r); + result.r = e_r * (T)std::cos(i); + result.i = e_r * (T)std::sin(i); + + return result; + } + + /*CUDA_CALLABLE complex pow(int y) + { + + return pow((double)y); + }*/ + + CUDA_CALLABLE complex pow(T y) + { + complex result; + + result = log() * y; + + return result.exp(); + } + + CUDA_CALLABLE complex sqrt() + { + complex result; + + //convert to polar coordinates + T a = std::sqrt(r*r + i*i); + T theta = std::atan2(i, r); + + //find the square root + T a_p = std::sqrt(a); + T theta_p = theta/2.0f; + + //convert back to cartesian coordinates + result.r = a_p * std::cos(theta_p); + result.i = a_p * std::sin(theta_p); + + return result; + } + + std::string str() + { + std::stringstream ss; + ss<<"("< rhs) + { + if(r == rhs.r && i == rhs.i) + return true; + return false; + } + + CUDA_CALLABLE bool operator==(T rhs) + { + if(r == rhs && i == 0) + return true; + return false; + } + + CUDA_CALLABLE bool operator!=(T rhs) + { + if(r != rhs || i != 0) + return true; + return false; + } + + CUDA_CALLABLE bool operator<(complex rhs){ + return abs() < rhs.abs(); + } + CUDA_CALLABLE bool operator<=(complex rhs){ + return abs() <= rhs.abs(); + } + CUDA_CALLABLE bool operator>(complex rhs){ + return abs() > rhs.abs(); + } + CUDA_CALLABLE bool operator >=(complex rhs){ + return abs() >= rhs.abs(); + } + + //CASTING operators + template < typename otherT > + operator complex() + { + complex result((otherT)r, (otherT)i); + return result; + } + template< typename otherT > + complex( const complex &rhs) + { + r = (T)rhs.r; + i = (T)rhs.i; + } + template< typename otherT > + complex& operator=(const complex &rhs) + { + r = (T)rhs.r; + i = (T)rhs.i; + return *this; + } + +}; + +} //end RTS namespace + +//addition +template +CUDA_CALLABLE static stim::complex operator+(const double a, const stim::complex b) +{ + return stim::complex((T)a + b.r, b.i); +} + +//subtraction with a real value +template +CUDA_CALLABLE static stim::complex operator-(const double a, const stim::complex b) +{ + return stim::complex((T)a - b.r, -b.i); +} + +//minus sign +template +CUDA_CALLABLE static stim::complex operator-(const stim::complex &rhs) +{ + return stim::complex(-rhs.r, -rhs.i); +} + +//multiply a T value by a complex value +template +CUDA_CALLABLE static stim::complex operator*(const double a, const stim::complex b) +{ + return stim::complex((T)a * b.r, (T)a * b.i); +} + +//divide a T value by a complex value +template +CUDA_CALLABLE static stim::complex operator/(const double a, const stim::complex b) +{ + stim::complex result; + + T denom = b.r * b.r + b.i * b.i; + + result.r = ((T)a * b.r) / denom; + result.i = -((T)a * b.i) / denom; + + return result; +} + + +template +CUDA_CALLABLE static stim::complex pow(stim::complex x, T y) +{ + return x.pow(y); +} +template +CUDA_CALLABLE static stim::complex pow(stim::complex x, int y) +{ + return x.pow(y); +} + +//log function +template +CUDA_CALLABLE static stim::complex log(stim::complex x) +{ + return x.log(); +} + +//exp function +template +CUDA_CALLABLE static stim::complex exp(stim::complex x) +{ + return x.exp(); +} + +//sqrt function +template +CUDA_CALLABLE static stim::complex sqrt(stim::complex x) +{ + return x.sqrt(); +} + + +template +CUDA_CALLABLE static T abs(stim::complex a) +{ + return a.abs(); +} + +template +CUDA_CALLABLE static T real(stim::complex a) +{ + return a.r; +} + +//template +CUDA_CALLABLE static float real(float a) +{ + return a; +} + +template +CUDA_CALLABLE static T imag(stim::complex a) +{ + return a.i; +} + +//trigonometric functions +//template +/*CUDA_CALLABLE static stim::complex sinf(const stim::complex x) +{ + stim::complex result; + result.r = sinf(x.r) * coshf(x.i); + result.i = cosf(x.r) * sinhf(x.i); + + return result; +}*/ + +template +CUDA_CALLABLE stim::complex sin(const stim::complex x) +{ + stim::complex result; + result.r = (A)std::sin(x.r) * (A)std::cosh(x.i); + result.i = (A)std::cos(x.r) * (A)std::sinh(x.i); + + return result; +} + +//floating point template +//template +/*CUDA_CALLABLE static stim::complex cosf(const stim::complex x) +{ + stim::complex result; + result.r = cosf(x.r) * coshf(x.i); + result.i = -(sinf(x.r) * sinhf(x.i)); + + return result; +}*/ + +template +CUDA_CALLABLE stim::complex cos(const stim::complex x) +{ + stim::complex result; + result.r = (A)std::cos(x.r) * (A)std::cosh(x.i); + result.i = -((A)std::sin(x.r) * (A)std::sinh(x.i)); + + return result; +} + + +template +std::ostream& operator<<(std::ostream& os, stim::complex x) +{ + os< +std::istream& operator>>(std::istream& is, stim::complex& x) +{ + A r, i; + r = i = 0; //initialize the real and imaginary parts to zero + is>>r; //parse + is>>i; + + x.real(r); //assign the parsed values to x + x.imag(i); + + return is; //return the stream +} + +//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7 +//template using rtsComplex = stim::complex; +//#endif + + + +#endif diff --git a/stim/math/complexfield.cuh b/stim/math/complexfield.cuh new file mode 100644 index 0000000..81488cf --- /dev/null +++ b/stim/math/complexfield.cuh @@ -0,0 +1,137 @@ +#ifndef RTS_COMPLEXFIELD_H +#define RTS_COMPLEXFIELD_H + +#include "cublas_v2.h" +#include + +#include "../math/field.cuh" +#include "../math/complex.h" +#include "../math/realfield.cuh" + +namespace stim{ + +template +__global__ void gpu_complexfield_mag(T* dest, complex* source, unsigned int r0, unsigned int r1){ + + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //calculate and store the result + dest[i] = source[i].abs(); +} + +/*This class stores functions for saving images of complex fields +*/ +template +class complexfield : public field< stim::complex, D >{ + using field< stim::complex, D >::R; + using field< stim::complex, D >::X; + using field< stim::complex, D >::shape; + using field< stim::complex, D >::cuda_params; + + + +public: + + //find the maximum value of component n + stim::complex find_max(unsigned int n){ + cublasStatus_t stat; + cublasHandle_t handle; + + //create a CUBLAS handle + stat = cublasCreate(&handle); + if(stat != CUBLAS_STATUS_SUCCESS){ + std::cout<<"CUBLAS Error: initialization failed"< result; + + if(sizeof(T) == 8) + stat = cublasIcamax(handle, L, (const cuComplex*)X[n], 1, &index); + else + stat = cublasIzamax(handle, L, (const cuDoubleComplex*)X[n], 1, &index); + + index -= 1; //adjust for 1-based indexing + + //if there was a GPU error, terminate + if(stat != CUBLAS_STATUS_SUCCESS){ + std::cout<<"CUBLAS Error: failure finding maximum value."<), cudaMemcpyDeviceToHost)); + return result; + } + +public: + + enum attribute {magnitude, real, imaginary}; + + //constructor (no parameters) + complexfield() : field, D>(){}; + + //constructor (resolution specified) + complexfield(unsigned int r0, unsigned int r1) : field, D>(r0, r1){}; + + //assignment from a field of complex values + complexfield & operator=(const field< stim::complex, D > rhs){ + field< complex, D >::operator=(rhs); + return *this; + } + + //assignment operator (scalar value) + complexfield & operator= (const complex rhs){ + + field< complex, D >::operator=(rhs); + return *this; + } + + //assignment operator (vector value) + complexfield & operator= (const vec< complex, D > rhs){ + + field< complex, D >::operator=(rhs); + return *this; + } + + //cropping + complexfield crop(unsigned int width, unsigned int height){ + + complexfield result; + result = field< complex, D>::crop(width, height); + return result; + } + + void toImage(std::string filename, attribute type = magnitude, unsigned int n=0){ + + field rf(R[0], R[1]); + + //get cuda parameters + dim3 blocks, grids; + cuda_params(grids, blocks); + + if(type == magnitude){ + gpu_complexfield_mag <<>> (rf.ptr(), X[n], R[0], R[1]); + rf.toImage(filename, n, true); + } + + } + + +}; + + +} //end namespace rts + + +#endif diff --git a/stim/math/constants.h b/stim/math/constants.h new file mode 100644 index 0000000..d333887 --- /dev/null +++ b/stim/math/constants.h @@ -0,0 +1,7 @@ +#ifndef RTS_CONSTANTS_H +#define RTS_CONSTANTS_H + +#define stimPI 3.14159 +#define stimTAU 2 * rtsPI + +#endif diff --git a/stim/math/field.cuh b/stim/math/field.cuh new file mode 100644 index 0000000..1a3a2ab --- /dev/null +++ b/stim/math/field.cuh @@ -0,0 +1,343 @@ +#ifndef RTS_FIELD_CUH +#define RTS_FIELD_CUH + +#include +#include +#include + +#include "cublas_v2.h" +#include + +#include "../math/rect.h" +#include "../cuda/threads.h" +#include "../cuda/error.h" +#include "../cuda/devices.h" +#include "../visualization/colormap.h" + + +namespace stim{ + +//multiply R = X * Y +template +__global__ void gpu_field_multiply(T* R, T* X, T* Y, unsigned int r0, unsigned int r1){ + + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //calculate and store the result + R[i] = X[i] * Y[i]; +} + +//assign a constant value to all points +template +__global__ void gpu_field_assign(T* ptr, T val, unsigned int r0, unsigned int r1){ + + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //calculate and store the result + ptr[i] = val; +} + +//crop the field to the new dimensions (width x height) +template +__global__ void gpu_field_crop(T* dest, T* source, + unsigned int r0, unsigned int r1, + unsigned int width, unsigned int height){ + + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= width || iv >= height) return; + + //compute the index into the field + int is = iv*r0 + iu; + int id = iv*width + iu; + + //calculate and store the result + dest[id] = source[is]; +} + +template +class field{ + +protected: + + T* X[D]; //pointer to the field data + unsigned int R[2]; //field resolution + stim::rect shape; //position and shape of the field slice + + //calculates the optimal block and grid sizes using information from the GPU + void cuda_params(dim3& grids, dim3& blocks){ + int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + blocks = dim3(SQRT_BLOCK, SQRT_BLOCK); + grids = dim3((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + } + + //find the maximum value of component n + T find_max(unsigned int n){ + cublasStatus_t stat; + cublasHandle_t handle; + + //create a CUBLAS handle + stat = cublasCreate(&handle); + if(stat != CUBLAS_STATUS_SUCCESS){ + std::cout<<"CUBLAS Error: initialization failed"< process_filename(std::string name){ + std::stringstream ss(name); + std::string item; + std::vector elems; + while(std::getline(ss, item, '.')) //split the string at the '.' character (filename and extension) + { + elems.push_back(item); + } + + std::string prefix = elems[0]; //prefix contains the filename (with wildcard '?' characters) + std::string ext = elems[1]; //file extension (ex. .bmp, .png) + ext = std::string(".") + ext; //add a period back into the extension + + size_t i0 = prefix.find_first_of("?"); //find the positions of the first and last wildcard ('?'') + size_t i1 = prefix.find_last_of("?"); + + std::string postfix = prefix.substr(i1+1); + prefix = prefix.substr(0, i0); + + unsigned int digits = i1 - i0 + 1; //compute the number of wildcards + + std::vector flist; //create a vector of file names + //fill the list + for(unsigned int d=0; d>> (X[n], rhs, R[0], R[1]); + + return *this; + } + + //assignment of vector component + field & operator= (const vec rhs){ + + int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //assign the constant value to all positions and dimensions + for(unsigned int n=0; n>> (X[n], rhs.v[n], R[0], R[1]); + + return *this; + + } + + //multiply two fields (element-wise multiplication) + field operator* (const field & rhs){ + + int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //create a scalar field to store the result + field result(R[0], R[1]); + + for(int n=0; n>> (result.X[n], X[n], rhs.X[n], R[0], R[1]); + + return result; + } + + T* ptr(unsigned int n = 0){ + if(n < D) + return X[n]; + else return NULL; + } + + //return the vector component at position (u, v) + vec get(unsigned int u, unsigned int v){ + + vec result; + for(unsigned int d=0; d crop(unsigned int width, unsigned int height){ + int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((width + SQRT_BLOCK -1)/SQRT_BLOCK, (height + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //create a scalar field to store the result + field result(width, height); + + for(int n=0; n>> (result.X[n], X[n], R[0], R[1], width, height); + + return result; + } + + //save an image representing component n + void toImage(std::string filename, unsigned int n = 0, + bool positive = false, stim::colormapType cmap = stim::cmBrewer){ + T max_val = find_max(n); //find the maximum value + + if(positive) //if the field is positive, use the range [0 max_val] + stim::gpu2image(X[n], filename, R[0], R[1], 0, max_val, cmap); + else + stim::gpu2image(X[n], filename, R[0], R[1], -max_val, max_val, cmap); + } + +}; + +} //end namespace rts +#endif diff --git a/stim/math/function.h b/stim/math/function.h new file mode 100644 index 0000000..033337d --- /dev/null +++ b/stim/math/function.h @@ -0,0 +1,236 @@ +#ifndef RTS_FUNCTION_H +#define RTS_FUNCTION_H + +#include + +namespace stim{ + +//template class for a one-dimensional function +template +class function +{ +protected: + + std::vector X; + std::vector Y; + Ty range[2]; + Ty bounding[2]; + + //comparison function for searching lambda + static bool findCeiling(Tx ax, Tx bx){ + return (ax > bx); + } + + //process a string to extract a function (generally delimited by tabs, commas, spaces, etc.) + void process_string(std::string s){ + std::stringstream ss(s); + + Tx x; + Ty y; + std::string line; + + while(!ss.eof()){ + + std::getline(ss, line); + if(line[0] == '#') continue; + + std::stringstream lstream(line); + + lstream>>x; //read the x value + lstream>>y; //read the y value + + if(ss.eof()) break; + insert(x, y); //insert the read value into the function + } + } + + void update_range(Ty y){ + //if the function is empty, just set the range to the given value + if(X.size() == 0){ + range[0] = range[1] = y; + } + + //otherwise compare the values and set them appropriately + if(y < range[0]) range[0] = y; + if(y > range[1]) range[1] = y; + } + + +public: + function(){ + range[0] = range[1] = 0; + bounding[0] = bounding[1] = 0; + } + + //linear interpolation + Ty linear(Tx x) const + { + if(X.size() == 0) return bounding[0]; //return zero if the function is empty + //return bounding values if x is outside the sample domain + if(x < X[0]) return bounding[0]; + if(x > X.back()) return bounding[1]; + + unsigned int N = X.size(); //number of sample points + + //declare an iterator + typedef typename std::vector< Tx >::iterator f_iter; //declare an iterator + f_iter it; + + //find the first X-coordinate that is greater than x + unsigned int i; + for(i = 0; i x) + break; + } + //i currently holds the ceiling + + //if the wavelength is past the end of the list, return the last sample point + if(i == N) return Y.back(); + //if the wavelength is before the beginning of the list, return the front + else if(i == 0) return Y[0]; + //otherwise interpolate + else{ + Tx xMax = X[i]; + Tx xMin = X[i-1]; + + Tx a = (x - xMin) / (xMax - xMin); + Ty riMin = Y[i - 1]; + Ty riMax = Y[i]; + Ty interp = riMax * a + riMin * (1 - a); + return interp; + } + } + + ///add a data point to a function + void insert(Tx x, Ty y) + { + unsigned int N = X.size(); //number of sample points + + update_range(y); //update the range of the function + + if(N == 0 || X[N-1] < x){ + X.push_back(x); + Y.push_back(y); + return; + } + + //declare an iterator and search for the x value + typename std::vector< Tx >::iterator it; + it = search(X.begin(), X.end(), &x, &x + 1, &function::findCeiling); + + //if the function value is past the end of the vector, add it to the back + if(*it == N){ + X.push_back(x); + Y.push_back(y); + } + //otherwise add the value at the iterator position + else{ + X.insert(it, x); + Y.insert(Y.begin() + *it, y); + } + + } + + Tx getX(unsigned int i) const{ + return X[i]; + } + + Ty getY(unsigned int i) const{ + return Y[i]; + } + + ///get the number of data points in the function + unsigned int getN() const{ + return X.size(); + } + + //look up an indexed component + Ty operator[](int i) const{ + if(i <= X.size()){ + std::cout<<"ERROR: accessing non-existing sample point in 'function'"< operator+(Ty r) const{ + + function result; + + //if there are points in the function + if(X.size() > 0){ + //add r to every point in f + for(unsigned int i=0; i & operator= (const Ty & rhs){ + X.clear(); + Y.clear(); + bounding[0] = bounding[1] = rhs; //set the boundary values to rhs + if(rhs != 0) //if the RHS is zero, just clear, otherwise add one value of RHS + insert(0, rhs); + + return *this; + } + + + //output a string description of the function (used for console output and debugging) + std::string str(){ + unsigned int N = X.size(); + + std::stringstream ss; + ss<<"# of samples: "<(t)), + std::istreambuf_iterator()); + + process_string(str); + } + + +}; + +} //end namespace rts + + +#endif diff --git a/stim/math/legendre.h b/stim/math/legendre.h new file mode 100644 index 0000000..4859740 --- /dev/null +++ b/stim/math/legendre.h @@ -0,0 +1,45 @@ +#ifndef RTS_LEGENDRE_H +#define RTS_LEGENDRE_H + +#include "rts/cuda/callable.h" + +namespace stim{ + +template +CUDA_CALLABLE void init_legendre(T x, T& P0, T& P1) +{ + //compute the first two Legendre polynomials + P0 = 1; + P1 = x; +} + +template +CUDA_CALLABLE void shift_legendre(int n, T x, T& P0, T& P1) +{ + //compute the next (order n) Legendre polynomial + T Pnew = ( (2 * n - 1) * x * P1 - (n-1) * P0 ) / n; + + //shift and add the new value to the array + P0 = P1; + P1 = Pnew; +} + +template +CUDA_CALLABLE void legendre(int n, T x, T* P) +{ + P[0] = 1; + + if(n >= 1) + P[1] = x; + + for(int i=2; i<=n; i++) + { + P[i] = ( (2 * i - 1) * x * P[i-1] - (i-1) * P[i-2] ) / i; + } + +} + +} + + +#endif diff --git a/stim/math/matrix.h b/stim/math/matrix.h new file mode 100644 index 0000000..9c039e1 --- /dev/null +++ b/stim/math/matrix.h @@ -0,0 +1,89 @@ +#ifndef RTS_MATRIX_H +#define RTS_MATRIX_H + +//#include "rts/vector.h" +#include +#include +#include "vector.h" +#include "../cuda/callable.h" + +namespace stim{ + +template +struct matrix +{ + //the matrix will be stored in column-major order (compatible with OpenGL) + T M[N*N]; + + CUDA_CALLABLE matrix() + { + for(int r=0; r operator=(T rhs) + { + int Nsq = N*N; + for(int i=0; i + CUDA_CALLABLE vec operator*(vec rhs) + { + vec result; + + for(int r=0; r +std::ostream& operator<<(std::ostream& os, stim::matrix M) +{ + os< 3 && __GNUC_MINOR__ > 7 +//template using rtsMatrix = rts::matrix; +//#endif + +#endif diff --git a/stim/math/plane.h b/stim/math/plane.h new file mode 100644 index 0000000..e384ef0 --- /dev/null +++ b/stim/math/plane.h @@ -0,0 +1,179 @@ +#ifndef RTS_PLANE_H +#define RTS_PLANE_H + +#include +#include "../math/vector.h" +#include "rts/cuda/callable.h" + + +namespace stim{ +template class plane; +} + +template +CUDA_CALLABLE stim::plane operator-(stim::plane v); + +namespace stim{ + +template +class plane{ + + //a plane is defined by a point and a normal + +private: + + vec P; //point on the plane + vec N; //plane normal + + CUDA_CALLABLE void init(){ + P = vec(0, 0, 0); + N = vec(0, 0, 1); + } + + +public: + + //default constructor + CUDA_CALLABLE plane(){ + init(); + } + + CUDA_CALLABLE plane(vec n, vec p = vec(0, 0, 0)){ + P = p; + N = n.norm(); + } + + CUDA_CALLABLE plane(T z_pos){ + init(); + P[2] = z_pos; + } + + //create a plane from three points (a triangle) + CUDA_CALLABLE plane(vec a, vec b, vec c){ + P = c; + N = (c - a).cross(b - a); + if(N.len() == 0) //handle the degenerate case when two vectors are the same, N = 0 + N = 0; + else + N = N.norm(); + } + + template< typename U > + CUDA_CALLABLE operator plane(){ + + plane result(N, P); + return result; + } + + CUDA_CALLABLE vec norm(){ + return N; + } + + CUDA_CALLABLE vec p(){ + return P; + } + + //flip the plane front-to-back + CUDA_CALLABLE plane flip(){ + plane result = *this; + result.N = -result.N; + return result; + } + + //determines how a vector v intersects the plane (1 = intersects front, 0 = within plane, -1 = intersects back) + CUDA_CALLABLE int face(vec v){ + + T dprod = v.dot(N); //get the dot product between v and N + + //conditional returns the appropriate value + if(dprod < 0) + return 1; + else if(dprod > 0) + return -1; + else + return 0; + } + + //determine on which side of the plane a point lies (1 = front, 0 = on the plane, -1 = back) + CUDA_CALLABLE int side(vec p){ + + vec v = p - P; //get the vector from P to the query point p + + return face(v); + } + + //compute the component of v that is perpendicular to the plane + CUDA_CALLABLE vec perpendicular(vec v){ + return N * v.dot(N); + } + + //compute the projection of v in the plane + CUDA_CALLABLE vec parallel(vec v){ + return v - perpendicular(v); + } + + CUDA_CALLABLE void decompose(vec v, vec& para, vec& perp){ + perp = N * v.dot(N); + para = v - perp; + } + + //get both the parallel and perpendicular components of a vector v w.r.t. the plane + CUDA_CALLABLE void project(vec v, vec &v_par, vec &v_perp){ + + v_perp = v.dot(N); + v_par = v - v_perp; + } + + //compute the reflection of v off of the plane + CUDA_CALLABLE vec reflect(vec v){ + + //compute the reflection using N_prime as the plane normal + vec par = parallel(v); + vec r = (-v) + par * 2; + + /*std::cout<<"----------------REFLECT-----------------------------"< operator-() + { + rts::plane p = *this; + + //negate the normal vector + p.N = -p.N; + + return p; + } + + //output a string + std::string str(){ + std::stringstream ss; + ss<<"P: "< operator- <> (rts::plane v); + + + +}; + +} + +//arithmetic operators + +//negative operator flips the plane (front to back) +//template + + + + +#endif diff --git a/stim/math/quad.h b/stim/math/quad.h new file mode 100644 index 0000000..628b982 --- /dev/null +++ b/stim/math/quad.h @@ -0,0 +1,186 @@ +#ifndef RTS_QUAD_H +#define RTS_QUAD_H + +//enable CUDA_CALLABLE macro +#include "../cuda/callable.h" +#include "../math/vector.h" +#include "../math/triangle.h" +#include "../math/quaternion.h" +#include +#include +#include + +namespace stim{ + +//template for a quadangle class in ND space +template +struct quad +{ + /* + B------------------>C + ^ ^ + | | + Y | + | | + | | + A---------X-------->O + */ + + /*T A[N]; + T B[N]; + T C[N];*/ + + rts::vec A; + rts::vec X; + rts::vec Y; + + + CUDA_CALLABLE quad() + { + + } + + CUDA_CALLABLE quad(vec a, vec b, vec c) + { + + A = a; + Y = b - a; + X = c - a - Y; + + } + + /******************************************************************* + Constructor - create a quad from a position, normal, and rotation + *******************************************************************/ + CUDA_CALLABLE quad(rts::vec c, rts::vec normal, T width, T height, T theta) + { + + //compute the X direction - start along world-space X + Y = rts::vec(0, 1, 0); + if(Y == normal) + Y = rts::vec(0, 0, 1); + + X = Y.cross(normal).norm(); + + std::cout< q; + q.CreateRotation(theta, normal); + X = q.toMatrix3() * X; + Y = normal.cross(X); + + //normalize everything + X = X.norm(); + Y = Y.norm(); + + //scale to match the quad width and height + X = X * width; + Y = Y * height; + + //set the corner of the plane + A = c - X * 0.5f - Y * 0.5f; + + std::cout< & rhs) + { + if(A == rhs.A && X == rhs.X && Y == rhs.Y) + return true; + else + return false; + } + + /******************************************* + Return the normal for the quad + *******************************************/ + CUDA_CALLABLE rts::vec n() + { + return (X.cross(Y)).norm(); + } + + CUDA_CALLABLE rts::vec p(T a, T b) + { + rts::vec result; + //given the two parameters a, b = [0 1], returns the position in world space + result = A + X * a + Y * b; + + return result; + } + + CUDA_CALLABLE rts::vec operator()(T a, T b) + { + return p(a, b); + } + + std::string str() + { + std::stringstream ss; + + ss<"<<"C="<"<<"D="< operator*(T rhs) + { + //scales the plane by a scalar value + + //compute the center point + rts::vec c = A + X*0.5f + Y*0.5f; + + //create the new quadangle + quad result; + result.X = X * rhs; + result.Y = Y * rhs; + result.A = c - result.X*0.5f - result.Y*0.5f; + + return result; + + } + + CUDA_CALLABLE T dist(vec p) + { + //compute the distance between a point and this quad + + //first break the quad up into two triangles + triangle T0(A, A+X, A+Y); + triangle T1(A+X+Y, A+X, A+Y); + + + T d0 = T0.dist(p); + T d1 = T1.dist(p); + + if(d0 < d1) + return d0; + else + return d1; + } + + CUDA_CALLABLE T dist_max(vec p) + { + T da = (A - p).len(); + T db = (A+X - p).len(); + T dc = (A+Y - p).len(); + T dd = (A+X+Y - p).len(); + + return std::max( da, std::max(db, std::max(dc, dd) ) ); + } +}; + +} //end namespace rts + +template +std::ostream& operator<<(std::ostream& os, rts::quad R) +{ + os< +class quaternion +{ +public: + T w; + T x; + T y; + T z; + + CUDA_CALLABLE void normalize(){ + + double length=sqrt(w*w + x*x + y*y + z*z); + w=w/length; + x=x/length; + y=y/length; + z=z/length; + } + + CUDA_CALLABLE void CreateRotation(T theta, T ux, T uy, T uz){ + + vec u(ux, uy, uz); + CreateRotation(theta, u); + } + + CUDA_CALLABLE void CreateRotation(T theta, vec u){ + + vec u_hat = u.norm(); + + //assign the given Euler rotation to this quaternion + w = (T)cos(theta/2); + x = u_hat[0]*(T)sin(theta/2); + y = u_hat[1]*(T)sin(theta/2); + z = u_hat[2]*(T)sin(theta/2); + } + + CUDA_CALLABLE void CreateRotation(vec from, vec to){ + + vec r = from.cross(to); //compute the rotation vector + T theta = asin(r.len()); //compute the angle of the rotation about r + //deal with a zero vector (both k and kn point in the same direction) + if(theta == (T)0) + return; + + //create a quaternion to capture the rotation + CreateRotation(theta, r.norm()); + } + + + + CUDA_CALLABLE quaternion operator *(quaternion ¶m){ + + float A, B, C, D, E, F, G, H; + + + A = (w + x)*(param.w + param.x); + B = (z - y)*(param.y - param.z); + C = (w - x)*(param.y + param.z); + D = (y + z)*(param.w - param.x); + E = (x + z)*(param.x + param.y); + F = (x - z)*(param.x - param.y); + G = (w + y)*(param.w - param.z); + H = (w - y)*(param.w + param.z); + + quaternion result; + result.w = B + (-E - F + G + H) /2; + result.x = A - (E + F + G + H)/2; + result.y = C + (E - F + G - H)/2; + result.z = D + (E - F - G + H)/2; + + return result; + } + + CUDA_CALLABLE matrix toMatrix3(){ + + matrix result; + + + T wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; + + + // calculate coefficients + x2 = x + x; y2 = y + y; + z2 = z + z; + xx = x * x2; xy = x * y2; xz = x * z2; + yy = y * y2; yz = y * z2; zz = z * z2; + wx = w * x2; wy = w * y2; wz = w * z2; + + result(0, 0) = 1 - (yy + zz); + result(0, 1) = xy - wz; + + result(0, 2) = xz + wy; + + result(1, 0) = xy + wz; + result(1, 1) = 1 - (xx + zz); + + result(1, 2) = yz - wx; + + result(2, 0) = xz - wy; + result(2, 1) = yz + wx; + + result(2, 2) = 1 - (xx + yy); + + return result; + } + + CUDA_CALLABLE matrix toMatrix4(){ + + matrix result; + T wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; + + // calculate coefficients + x2 = x + x; y2 = y + y; + z2 = z + z; + xx = x * x2; xy = x * y2; xz = x * z2; + yy = y * y2; yz = y * z2; zz = z * z2; + wx = w * x2; wy = w * y2; wz = w * z2; + + result(0, 0) = 1 - (yy + zz); + result(0, 1) = xy - wz; + + result(0, 2) = xz + wy; + + result(1, 0) = xy + wz; + result(1, 1) = 1 - (xx + zz); + + result(1, 2) = yz - wx; + + result(2, 0) = xz - wy; + result(2, 1) = yz + wx; + + result(2, 2) = 1 - (xx + yy); + + result(3, 3) = 1; + + return result; + } + + + CUDA_CALLABLE quaternion(){ + w=0; x=0; y=0; z=0; + } + + CUDA_CALLABLE quaternion(T c, T i, T j, T k){ + w=c; x=i; y=j; z=k; + } + +}; + +} //end rts namespace + + +#endif diff --git a/stim/math/realfield.cuh b/stim/math/realfield.cuh new file mode 100644 index 0000000..7d6ce26 --- /dev/null +++ b/stim/math/realfield.cuh @@ -0,0 +1,262 @@ +#ifndef RTS_REALFIELD_H +#define RTS_REALFIELD_H + +#include "../visualization/colormap.h" +#include "../envi/envi.h" +#include "../math/rect.h" +#include "../cuda/devices.h" +#include "cublas_v2.h" +#include + + +namespace stim{ + +//multiply R = X * Y +template +__global__ void gpu_realfield_multiply(T* R, T* X, T* Y, unsigned int r0, unsigned int r1){ + + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //calculate and store the result + R[i] = X[i] * Y[i]; + +} + +template +class realfield{ + + P* X[N]; //an array of N gpu pointers for each field component + int R[2]; //resolution of the slice + rect

shape; + + void process_filename(std::string name, std::string &prefix, std::string &postfix, + std::string &ext, unsigned int &digits) + { + std::stringstream ss(name); + std::string item; + std::vector elems; + while(std::getline(ss, item, '.')) //split the string at the '.' character (filename and extension) + { + elems.push_back(item); + } + + prefix = elems[0]; //prefix contains the filename (with wildcard '?' characters) + ext = elems[1]; //file extension (ex. .bmp, .png) + ext = std::string(".") + ext; //add a period back into the extension + + size_t i0 = prefix.find_first_of("?"); //find the positions of the first and last wildcard ('?'') + size_t i1 = prefix.find_last_of("?"); + + postfix = prefix.substr(i1+1); + prefix = prefix.substr(0, i0); + + digits = i1 - i0 + 1; //compute the number of wildcards + } + + void init() + { + for(unsigned int n=0; n(vec

(-1, -1, 0), vec

(-1, 1, 0), vec

(1, 1, 0)); //default geometry + clear(); //zero the field + //std::cout<<"realfield CONSTRUCTOR"<(X[n], filename, R[0], R[1], vmin, vmax, cmap); + } + + void toImages(std::string filename, bool global_max = true, stim::colormapType cmap = stim::cmBrewer) + { + std::string prefix, postfix, extension; + unsigned int digits; + process_filename(filename, prefix, postfix, extension, digits); //process the filename for wild cards + + cublasStatus_t stat; + cublasHandle_t handle; + + //create a CUBLAS handle + stat = cublasCreate(&handle); + if(stat != CUBLAS_STATUS_SUCCESS) + { + std::cout<<"CUBLAS Error: initialization failed"< maxAll) //if maxVal is larger, update the maxAll variable + maxAll = maxVal[n]; + + } + + cublasDestroy(handle); //destroy the CUBLAS handle + + P outputMax = abs(maxAll); //maximum value used for each output image + for(int n=0; n operator* (const realfield & rhs){ + + int maxThreads = stim::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //create a scalar field to store the result + realfield result(R[0], R[1]); + + for(int n=0; n>> (result.X[n], X[n], rhs.X[n], R[0], R[1]); + + return result; + } + + ///copy constructor + realfield(const realfield &rhs) + { + //first make a shallow copy + R[0] = rhs.R[0]; + R[1] = rhs.R[1]; + + for(unsigned int n=0; n +#include +#include + +namespace stim{ + +//template for a rectangle class in ND space +template +struct rect +{ + /* + ^ O + | + | + Y C + | + | + O---------X---------> + */ + +private: + + stim::vec C; + stim::vec X; + stim::vec Y; + + CUDA_CALLABLE void scale(T factor){ + X *= factor; + Y *= factor; + } + + + CUDA_CALLABLE void normal(vec n){ //orient the rectangle along the specified normal + + n = n.norm(); //normalize, just in case + vec n_current = X.cross(Y).norm(); //compute the current normal + quaternion q; //create a quaternion + q.CreateRotation(n_current, n); //initialize a rotation from n_current to n + + //apply the quaternion to the vectors and position + X = q.toMatrix3() * X; + Y = q.toMatrix3() * Y; + } + + CUDA_CALLABLE void init(){ + C = vec(0, 0, 0); + X = vec(1, 0, 0); + Y = vec(0, 1, 0); + } + +public: + + CUDA_CALLABLE rect(){ + init(); + } + + //create a rectangle given a size and position + CUDA_CALLABLE rect(T size, T z_pos = (T)0){ + init(); //use the default setup + scale(size); //scale the rectangle + C[2] = z_pos; + } + + //create a rectangle from a center point, normal, and size + CUDA_CALLABLE rect(T size, vec c, vec n = vec(0, 0, 1)){ + init(); //start with the default setting + C = c; + scale(size); //scale the rectangle + normal(n); //orient + } + + //create a rectangle from a center point, normal, and size + CUDA_CALLABLE rect(vec size, vec c, vec n = vec(0, 0, 1)){ + init(); //start with the default setting + C = c; + scale(size); //scale the rectangle + normal(n); //orient + } + + CUDA_CALLABLE rect(vec center, vec directionX, vec directionY ) + { + C = center; + X = directionX; + Y = directionY; + } + + CUDA_CALLABLE rect(vec mag, vec center, vec directionX, vec directionY ) + { + C = center; + X = directionX; + Y = directionY; + scale(mag[0], mag[1]); + } + + CUDA_CALLABLE void scale(T factor1, T factor2){ + X *= factor1; + Y *= factor2; + } + + //boolean comparison + bool operator==(const rect & rhs) + { + if(C == rhs.C && X == rhs.X && Y == rhs.Y) + return true; + else + return false; + } + + /******************************************* + Return the normal for the rect + *******************************************/ + CUDA_CALLABLE stim::vec n() + { + return (X.cross(Y)).norm(); + } + + //get the world space value given the planar coordinates a, b in [0, 1] + CUDA_CALLABLE stim::vec p(T a, T b) + { + stim::vec result; + //given the two parameters a, b = [0 1], returns the position in world space + vec A = C - X * (T)0.5 - Y * (T)0.5; + result = A + X * a + Y * b; + + return result; + } + + //parenthesis operator returns the world space given rectangular coordinates a and b in [0 1] + CUDA_CALLABLE stim::vec operator()(T a, T b) + { + return p(a, b); + } + + std::string str() + { + std::stringstream ss; + vec A = C - X * (T)0.5 - Y * (T)0.5; + ss<"<<"C="<"<<"D="< operator*(T rhs) + { + //scales the plane by a scalar value + + //create the new rectangle + rect result = *this; + result.scale(rhs); + + return result; + + } + + //computes the distance between the specified point and this rectangle + CUDA_CALLABLE T dist(vec p) + { + //compute the distance between a point and this rect + + vec A = C - X * (T)0.5 - Y * (T)0.5; + + //first break the rect up into two triangles + triangle T0(A, A+X, A+Y); + triangle T1(A+X+Y, A+X, A+Y); + + + T d0 = T0.dist(p); + T d1 = T1.dist(p); + + if(d0 < d1) + return d0; + else + return d1; + } + + CUDA_CALLABLE T dist_max(vec p) + { + vec A = C - X * (T)0.5 - Y * (T)0.5; + T da = (A - p).len(); + T db = (A+X - p).len(); + T dc = (A+Y - p).len(); + T dd = (A+X+Y - p).len(); + + return std::max( da, std::max(db, std::max(dc, dd) ) ); + } +}; + +} //end namespace rts + +template +std::ostream& operator<<(std::ostream& os, stim::rect R) +{ + os< + +namespace stim{ + +template +struct triangle +{ + /* + A------>B + | / + | / + | / + | / + | / + | / + C + */ + private: + + vec A; + vec B; + vec C; + + CUDA_CALLABLE vec _p(T s, T t) + { + //This function returns the point specified by p = A + s(B-A) + t(C-A) + vec E0 = B-A; + vec E1 = C-A; + + return A + s*E0 + t*E1; + } + + + public: + + + + CUDA_CALLABLE triangle() + { + + } + + CUDA_CALLABLE triangle(vec a, vec b, vec c) + { + A = a; + B = b; + C = c; + } + + CUDA_CALLABLE stim::vec operator()(T s, T t) + { + return _p(s, t); + } + + CUDA_CALLABLE vec nearest(vec p) + { + //comptue the distance between a point and this triangle + // This code is adapted from: http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf + + vec E0 = B-A; + vec E1 = C-A; + vec D = A - p; + + T a = E0.dot(E0); + T b = E0.dot(E1); + T c = E1.dot(E1); + T d = E0.dot(D); + T e = E1.dot(D); + //T f = D.dot(D); + + T det = a*c - b*b; + T s = b*e - c*d; + T t = b*d - a*e; + + /*std::cout<<"E0: "<= 0 ? 0 : ( -e >= c ? 1 : -e/c ) ); + //done + } + } + else if(t < 0) + { + //region 5 + //std::cout<<"Region 5"<= 0 ? 0 : ( -d >= a ? 1 : -d/a ) ); + t = 0; + //done + } + else + { + //region 0 + //std::cout<<"Region 0"<= denom ? 1 : numer/denom ); + } + t = 1 - s; + //done + } + } + + //std::cout<<"s: "< p) + { + vec n = nearest(p); + + return (p - n).len(); + } +}; + +} + +#endif diff --git a/stim/math/vector.h b/stim/math/vector.h new file mode 100644 index 0000000..86fbbfd --- /dev/null +++ b/stim/math/vector.h @@ -0,0 +1,280 @@ +#ifndef RTS_VECTOR_H +#define RTS_VECTOR_H + +#include +#include +#include +//#include "rts/math/point.h" +#include "../cuda/callable.h" + + +namespace stim +{ + + + +template +struct vec +{ + T v[N]; + + CUDA_CALLABLE vec() + { + //memset(v, 0, sizeof(T) * N); + for(int i=0; i& other){ + for(int i=0; i + CUDA_CALLABLE operator vec(){ + vec result; + for(int i=0; i + //friend vec::operator vec(); + + CUDA_CALLABLE T len() const + { + //compute and return the vector length + T sum_sq = (T)0; + for(int i=0; i cart2sph() const + { + //convert the vector from cartesian to spherical coordinates + //x, y, z -> r, theta, phi (where theta = 0 to 2*pi) + + vec sph; + sph[0] = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); + sph[1] = std::atan2(v[1], v[0]); + sph[2] = std::acos(v[2] / sph[0]); + + return sph; + } + + CUDA_CALLABLE vec sph2cart() const + { + //convert the vector from cartesian to spherical coordinates + //r, theta, phi -> x, y, z (where theta = 0 to 2*pi) + + vec cart; + cart[0] = v[0] * std::cos(v[1]) * std::sin(v[2]); + cart[1] = v[0] * std::sin(v[1]) * std::sin(v[2]); + cart[2] = v[0] * std::cos(v[2]); + + return cart; + } + + CUDA_CALLABLE vec norm() const + { + //compute and return the vector norm + vec result; + + //compute the vector length + T l = len(); + + //normalize + for(int i=0; i cross(const vec rhs) const + { + vec result; + + //compute the cross product (only valid for 3D vectors) + result[0] = v[1] * rhs.v[2] - v[2] * rhs.v[1]; + result[1] = v[2] * rhs.v[0] - v[0] * rhs.v[2]; + result[2] = v[0] * rhs.v[1] - v[1] * rhs.v[0]; + + return result; + } + + CUDA_CALLABLE T dot(vec rhs) const + { + T result = (T)0; + + for(int i=0; i operator+(vec rhs) const + { + vec result; + + for(int i=0; i operator+(T rhs) const + { + vec result; + + for(int i=0; i operator-(vec rhs) const + { + vec result; + + for(int i=0; i operator*(T rhs) const + { + vec result; + + for(int i=0; i operator/(T rhs) const + { + vec result; + + for(int i=0; i operator*=(T rhs){ + for(int i=0; i & operator=(T rhs){ + for(int i=0; i + CUDA_CALLABLE vec & operator=(vec rhs){ + for(int i=0; i operator-() const{ + vec r; + + //negate the vector + for(int i=0; i rhs) const + { + if ( (rhs.v[0] == v[0]) && (rhs.v[1] == v[1]) && (rhs.v[2] == v[2]) ) + return true; + + return false; + } + + std::string str() const + { + std::stringstream ss; + + ss<<"["; + for(int i=0; i +std::ostream& operator<<(std::ostream& os, stim::vec v) +{ + os< +CUDA_CALLABLE stim::vec operator*(T lhs, stim::vec rhs) +{ + stim::vec r; + + return rhs * lhs; +} + +//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7 +//template using rtsVector = rts::vector; +//#endif + +#endif diff --git a/stim/matlab/rtsApplyBrewer.m b/stim/matlab/rtsApplyBrewer.m new file mode 100644 index 0000000..bdc0693 --- /dev/null +++ b/stim/matlab/rtsApplyBrewer.m @@ -0,0 +1,41 @@ +function result = rtsApplyBrewer(field, minVal, maxVal) + +if nargin < 3 + maxVal = max(field(:)); +end +if nargin < 2 + minVal = min(field(:)); +end + +fieldMaskMin = field >= minVal; +fieldMaskMax = field <= maxVal; + +fieldMask = min(fieldMaskMin, fieldMaskMax); + +ctrlPts = zeros(11, 3); + +ctrlPts(1, :) = [0.192157, 0.211765, 0.584314]; +ctrlPts(2, :) = [0.270588, 0.458824, 0.705882]; +ctrlPts(3, :) = [0.454902, 0.678431, 0.819608]; +ctrlPts(4, :) = [0.670588, 0.85098, 0.913725]; +ctrlPts(5, :) = [0.878431, 0.952941, 0.972549]; +ctrlPts(6, :) = [1, 1, 0.74902]; +ctrlPts(7, :) = [0.996078, 0.878431, 0.564706]; +ctrlPts(8, :) = [0.992157, 0.682353, 0.380392]; +ctrlPts(9, :) = [0.956863, 0.427451, 0.262745]; +ctrlPts(10, :) = [0.843137, 0.188235, 0.152941]; +ctrlPts(11, :) = [0.647059, 0, 0.14902]; + +%X = 0:1/10:1; +X = minVal:(maxVal - minVal)/10:maxVal; + +R = interp1(X, ctrlPts(:, 1), field); +G = interp1(X, ctrlPts(:, 2), field); +B = interp1(X, ctrlPts(:, 3), field); + +R = R.* fieldMask; +G = G.* fieldMask; +B = B.* fieldMask; + +result = cat(ndims(field)+1, R, G, B); + diff --git a/stim/matlab/rtsBaselineCorrect.m b/stim/matlab/rtsBaselineCorrect.m new file mode 100644 index 0000000..673296d --- /dev/null +++ b/stim/matlab/rtsBaselineCorrect.m @@ -0,0 +1,61 @@ +function R = rtsBaselineCorrect(A, b, dim) + +%This function baselines an n-dimensional array given a series of points b +%B = baseline-corrected data +%A = raw data +%b = baseline points in the format [b0 b1 ... bn] +%dim = dimension along which the baseline correction will occur + + +%allocate space for the sub-component indices +sz = size(A); +inds = repmat({1},1,ndims(A)); + +%allocate space for the result +R = zeros(size(A)); + +%for each baseline point +for bp = 1:length(b) - 1 + + %extract the data at the first baseline point + n = b(bp); + inds = getIndices(A, n, dim); + BP1 = A(inds{:}); + + %extract the data at the second baseline point + n = b(bp+1); + inds = getIndices(A, n, dim); + BP2 = A(inds{:}); + + %for each point in between the baseline points + for p = b(bp):b(bp+1) + + %compute the weighting of the linear function between BP1 and BP2 + a2 = (p - b(bp))/(b(bp+1) - b(bp)); + a1 = 1.0 - a2; + + n = p; + inds = getIndices(A, n, dim); + + baseFunc = a1 * BP1 + a2 * BP2; + R(inds{:}) = A(inds{:}) - baseFunc; + end +end + + +function I = getIndices(A, n, dim) + +%this function returns indices for all elements in S at +%point n along dimension dim + +sz = size(A); +I = repmat({1},1,ndims(A)); + +for d = 1:ndims(A) + if d ~= dim + I{d} = 1:sz(d); + else + I{d} = n; + end +end + diff --git a/stim/matlab/rtsClass2Color.m b/stim/matlab/rtsClass2Color.m new file mode 100644 index 0000000..5b8df59 --- /dev/null +++ b/stim/matlab/rtsClass2Color.m @@ -0,0 +1,15 @@ +function C = rtsClass2Color(classes, classList, colorList) + +C = zeros(length(classes), 3); + +for i = 1:length(classes) + + for c = 1:length(classList) + + if classes(i) == classList(c) + C(i, :) = colorList(c, :); + end + + end + +end \ No newline at end of file diff --git a/stim/matlab/rtsColorMapField.m b/stim/matlab/rtsColorMapField.m new file mode 100644 index 0000000..3ada8c7 --- /dev/null +++ b/stim/matlab/rtsColorMapField.m @@ -0,0 +1,30 @@ +function Result = rtsColorMapField(F, range) +%creates a color map for the field F and returns an array with +%an additional size-3 color dimension + +R = zeros(size(F)); +G = zeros(size(F)); +B = zeros(size(F)); + +%if no range is specified, estimate it using min and max values +if nargin == 1 + range(1) = min(F(:)); + range(2) = max(F(:)); +end +range_size = range(2) - range(1); + +num_elements = size(F(:)); +for i =1:num_elements + if(F(i) > range(1) && F(i) < range(2)) + hue_degrees = (1 - (F(i) - range(1))/range_size)*240; + HSVval = [hue_degrees 1.0 1.0]; + RGBval = rtsHSVtoRGB(HSVval); + else + RGBval = [0 0 F(i)/range(1)]; + end + R(i) = RGBval(1); + G(i) = RGBval(2); + B(i) = RGBval(3); +end +Result = cat(ndims(F)+1, R, G, B); +imshow(Result); diff --git a/stim/matlab/rtsColorMapRainbow.m b/stim/matlab/rtsColorMapRainbow.m new file mode 100644 index 0000000..0c79394 --- /dev/null +++ b/stim/matlab/rtsColorMapRainbow.m @@ -0,0 +1,37 @@ +function result = rtsColorMapRainbow(field, minVal, maxVal) + +if nargin < 3 + maxVal = max(field(:)); +end +if nargin < 2 + minVal = min(field(:)); +end + +fieldMaskMin = field >= minVal; +fieldMaskMax = field <= maxVal; + +fieldMask = fieldMaskMin;%min(fieldMaskMin, fieldMaskMax); + +ctrlPts = zeros(3, 3); + +ctrlPts(1, :) = [0.0, 0.0, 0.5]; +ctrlPts(2, :) = [0.0, 0.0, 1.0]; +ctrlPts(3, :) = [0.0, 1.0, 1.0]; +ctrlPts(4, :) = [0.0, 1.0, 0.0]; +ctrlPts(5, :) = [1.0, 1.0, 0.0]; +ctrlPts(6, :) = [1.0, 0.0, 0.0]; +ctrlPts(7, :) = [0.5, 0.0, 0.0]; + +%X = 0:1/10:1; +X = minVal:(maxVal - minVal)/6:maxVal; + +R = interp1(X, ctrlPts(:, 1), field); +G = interp1(X, ctrlPts(:, 2), field); +B = interp1(X, ctrlPts(:, 3), field); + +%R = R.* fieldMask; +%G = G.* fieldMask; +%B = B.* fieldMask; + +result = cat(ndims(field)+1, R, G, B); + diff --git a/stim/matlab/rtsCropMask.m b/stim/matlab/rtsCropMask.m new file mode 100644 index 0000000..5ac7fb3 --- /dev/null +++ b/stim/matlab/rtsCropMask.m @@ -0,0 +1,21 @@ +function new_mask = rtsCropMask(mask, max_pixels) + +%convert to logical +%mask = mask > 0; + +if nnz(mask) < max_pixels + new_mask = mask; +else + j = 0; + for i = 1:length(mask(:)) + if mask(i) > 0 + j = j + 1; + if j == max_pixels + last_pixel = i; + end + end + end + new_mask = zeros(size(mask, 1), size(mask, 2)); + new_mask(1:last_pixel) = mask(1:last_pixel); +end + \ No newline at end of file diff --git a/stim/matlab/rtsEnviClassify.m b/stim/matlab/rtsEnviClassify.m new file mode 100644 index 0000000..8326995 --- /dev/null +++ b/stim/matlab/rtsEnviClassify.m @@ -0,0 +1,39 @@ +function C = rtsEnviClassify(B, envi_filename, mask, nFeatures) + +% B = TreeBagger class +% nFeatures = number of features cropped out (0 means use all features) + +if nargin < 4 + nFeatures = 0; +end + + +batchSize = 100000; + + + +Ns = nnz(mask) %get the number of samples to be classified +Nb = ceil(Ns / batchSize); %get the number of batches + +C(Ns, 1) = char(0); + +%open the ENVI file +envi = rtsEnviOpen(envi_filename, [envi_filename '.hdr'], mask); + +for b = 1:Nb + + %load a batch + batch = rtsEnviRead(envi, batchSize); + + %crop out the number of features specified (if it is specified) + if nFeatures ~= 0 + batch = batch(1:nFeatures, :); + end + + %classify the batch + C( (b-1)*batchSize + 1 : (b-1)*batchSize + size(batch, 2) ) = cell2mat(predict(B, batch')); + + disp([num2str( round( b / Nb * 100 ) ) '%']); +end + +rtsEnviClose(envi); \ No newline at end of file diff --git a/stim/matlab/rtsEnviClose.m b/stim/matlab/rtsEnviClose.m new file mode 100644 index 0000000..3ee3283 --- /dev/null +++ b/stim/matlab/rtsEnviClose.m @@ -0,0 +1,3 @@ +function rtsEnviClose(enviFID) + +fclose(enviFID.fid); \ No newline at end of file diff --git a/stim/matlab/rtsEnviID.m b/stim/matlab/rtsEnviID.m new file mode 100644 index 0000000..10f559e --- /dev/null +++ b/stim/matlab/rtsEnviID.m @@ -0,0 +1,8 @@ +classdef rtsEnviID < handle + properties + fid + header + mask + fpos + end +end \ No newline at end of file diff --git a/stim/matlab/rtsEnviLoadClasses.m~ b/stim/matlab/rtsEnviLoadClasses.m~ new file mode 100644 index 0000000..ee30933 --- /dev/null +++ b/stim/matlab/rtsEnviLoadClasses.m~ @@ -0,0 +1,11 @@ +function [F, C] = rtsEnviLoadClasses(filenames, classnames, ppc) + +Nc = size(filenames, 1); + +for c = 1:Nc + %load the masks + image = imread(filenames{c}); + mask = image(:, :, 1)' > 0; + + +end \ No newline at end of file diff --git a/stim/matlab/rtsEnviLoadHeader.m b/stim/matlab/rtsEnviLoadHeader.m new file mode 100644 index 0000000..2746cc2 --- /dev/null +++ b/stim/matlab/rtsEnviLoadHeader.m @@ -0,0 +1,78 @@ +function s = rtsEnviLoadHeader(filename) +%create a cell array of fields +s = struct; + +fid = fopen(filename, 'r', 'n', 'US-ASCII'); + +%read the first field and make sure it is "ENVI" +fname = GetFieldName(fid); +if strcmp(fname, 'ENVI') == 0 + disp('Not an ENVI header file'); + return; +end + +while feof(fid) == 0 + fname = GetFieldName(fid); + if feof(fid) == 1 + return; + end + [value, valid] = ReadField(fid, fname); + if valid == 1 + s = setfield(s, fname, value); + end +end +fclose(fid); + +function t = GetFieldName(fid) +string_struct = textscan(fid, '%s', 1, 'Delimiter', '='); +if feof(fid) == 1 + t = []; + return; +end +t = string_struct{1}{1}; +t = strtrim(t); +t(t==' ') = '_'; + +function [v, valid] = ReadField(fid, field_name) +valid = 1; +stringFields = {'file_type', 'interleave', 'sensor_type', 'wavelength_units'}; +intFields = {'samples', 'lines', 'bands', 'header_offset', 'data_type', 'byte_order'}; + +%if the field is "description", read between the brackets +if strcmp(field_name, 'description') == 1 + textscan(fid, '%[{]', 1); + string_struct = textscan(fid, '%[^}]', 1, 'Whitespace', ''); + textscan(fid, '%[}]', 1); + v = string_struct{1}{1}; + v = strtrim(v); + return; +end +if max(strcmp(field_name, intFields)) ~= 0 + v = fscanf(fid, '%d'); + return; +end +if max(strcmp(field_name, stringFields)) ~= 0 + string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n'); + v = string_struct{1}{1}; + v = strtrim(v); + return; +end + +%read and return the wavelength values +if strcmp(field_name, 'wavelength') == 1 + v = []; + textscan(fid, '%[{]', 1); + c = ' '; + while c ~= '}' + new = fscanf(fid, '%f'); + v = [v new]; + c = fscanf(fid, '%c', 1); + end + return; +end + +%if it doesn't match anything, just read until the end of the line +%string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n'); +string_struct = textscan(fid, '%s', 1, 'Delimiter', '\n'); +v = ''; +valid = 0; diff --git a/stim/matlab/rtsEnviLoadImage.m b/stim/matlab/rtsEnviLoadImage.m new file mode 100644 index 0000000..6a7ab0f --- /dev/null +++ b/stim/matlab/rtsEnviLoadImage.m @@ -0,0 +1,44 @@ +function [image, header] = rtsEnviLoadImage(filename, headername) +%enter the file name (no .hdr extension) + +header_file = headername; +header = rtsEnviLoadHeader(header_file); + +if strcmp(header.interleave, 'bsq') == 1 + image = zeros(header.samples, header.lines, header.bands); +end +if strcmp(header.interleave, 'bip') == 1 + image = zeros(header.bands, header.samples, header.lines); +end +if strcmp(header.interleave, 'bil') == 1 + image = zeros(header.samples, header.bands, header.lines); +end + + +file_bytes = header.samples*header.lines*header.bands; +%check to make sure there is enough memory available +if ispc + [~, sys] = memory; + if sys.PhysicalMemory.Available < file_bytes*header.data_type && strcmp(header.interleave, 'bsq') == 0 + strResponse = input('The data set will require virtual memory for permutation. Continue? (y/n) ', 's'); + if strcmp(strResponse, 'n') == 1 + return; + end + end +else + disp('Sorry, I can''t check available memory for this OS. Keep your fingers crossed!'); +end + +fid = fopen(filename); +%skip the header - start reading header_offset bytes from the beginning +fseek(fid, header.header_offset, 'bof'); + +%read the data +image(:) = fread(fid, file_bytes, 'float32'); + +if strcmp(header.interleave, 'bip') == 1 + image = permute(image, [2 3 1]); +end +if strcmp(header.interleave, 'bil') == 1 + image = permute(image, [1 3 2]); +end \ No newline at end of file diff --git a/stim/matlab/rtsEnviLoadTraining.m b/stim/matlab/rtsEnviLoadTraining.m new file mode 100644 index 0000000..1788a08 --- /dev/null +++ b/stim/matlab/rtsEnviLoadTraining.m @@ -0,0 +1,57 @@ +function [F, C] = rtsEnviLoadTraining(filenames, classnames, binfile, ppc) + +Nc = length(filenames); %calculate the number of classes + +if nargin < 3 + ppc = 0; +end + +%---------Load the Masks----------- +%first, get the total number of pixels +Ns = 0; +for c = 1:Nc + %load the mask + image = imread(filenames{c}); + + if c == 1 + mask = boolean(zeros(size(image, 2), size(image, 1), Nc)); + end + + if ppc ~= 0 + mask(:, :, c) = rtsRandomizeMask(image(:, :, 1)' > 0, ppc); + else + mask(:, :, c) = image(:, :, 1)' > 0; + end + +end + +%calculate the number of samples +Ns = Ns + nnz(mask); + +%-----------Load the Training Data------------- +disp(['loading ' num2str(Ns) ' samples']); +ci = 1; +for c = 1:Nc + + disp(['loading class ' classnames(c) '...']); + batch = nnz(mask(:, :, c)); + %create an ENVI file object + envi = rtsEnviOpen(binfile, [binfile '.hdr'], mask(:, :, c)); + + if c == 1 + F = zeros(Ns, envi.header.bands); + C(Ns, 1) = char(0); + end + + %read a bunch of spectra + F(ci:ci+batch - 1, :) = rtsEnviRead(envi, batch)'; + C(ci:ci+batch - 1, 1) = repmat([classnames(c)], [batch 1]); + + %update the current index + ci = ci+batch; + + %close the ENVI file + rtsEnviClose(envi); + + disp('done'); +end \ No newline at end of file diff --git a/stim/matlab/rtsEnviOpen.m b/stim/matlab/rtsEnviOpen.m new file mode 100644 index 0000000..badc640 --- /dev/null +++ b/stim/matlab/rtsEnviOpen.m @@ -0,0 +1,35 @@ +function EID = rtsEnviOpen(filename, headername, mask) + +%opens an ENVI file and returns an ID structure +% filename = name of ENVI file +% headername = name of ENVI header +% FID = structure containing file information +% mask = binary image specifying valid spectra + +%assign the mask variable +if(nargin < 3) + mask = 1; +end + +%open the file and save the file ID +fid = fopen(filename, 'r'); + +%open the ENVI header file +header = rtsEnviLoadHeader(headername); + +%this is currently only valid for BIP files +if(~strcmp(header.interleave, 'bip')) + disp('Error: This function only works for BIP interleave files. Load in ENVI and convert.'); +end +if(header.data_type ~= 4) + disp('Error: This function only works for floating-point files.'); +end + +EID = rtsEnviID; +EID.fid = fid; +EID.header = header; +EID.mask = mask; +EID.fpos = 0; + +%EID = struct('fid', fid, 'header', header, 'mask', mask, 'fpos', 0); + diff --git a/stim/matlab/rtsEnviRandomForest2C_apply.m b/stim/matlab/rtsEnviRandomForest2C_apply.m new file mode 100644 index 0000000..e1d2a7c --- /dev/null +++ b/stim/matlab/rtsEnviRandomForest2C_apply.m @@ -0,0 +1,41 @@ +function P = rtsEnviRandomForest2C_apply(RF, EnviFileName, EnviHeaderName, TissueMask, reference) + +%Applies a 2-class random forest classifier to the given image. Only +%pixels specified in TissueMask are classified. The estimated rule data is +%returned as an image sequence. + +%load the tissue mask +maskImage = imread(TissueMask); +maskBinary = (maskImage(:, :, 1) > 0)'; + +nPixels = nnz(maskBinary); +disp(['Number of pixels: ' num2str(nPixels)]); + + +%load the data +disp(['Loading Features: ' EnviFileName]); +tLoadTime = tic; +fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary); +F = rtsEnviRead(fid, nPixels); +rtsEnviClose(fid); +disp(['Time: ' num2str(toc(tLoadTime)) 's']); + +%transpose +F = F'; + +%apply the reference +if nargin == 5 + Fnorm = repmat(F(:, reference), 1, size(F, 2)); + F = F./Fnorm; + F(:, reference) = []; +end + +%classify the data +disp('Classifying...'); +tClassTime = tic; +T = predict(RF, F); +disp(['Time: ' num2str(toc(tClassTime)) 's']); +%Cl = Cpost > threshold; + +%reconstruct the image from the mask +P = rtsMaskReconstruct(T', maskBinary, -1); \ No newline at end of file diff --git a/stim/matlab/rtsEnviRandomForest2C_train.m b/stim/matlab/rtsEnviRandomForest2C_train.m new file mode 100644 index 0000000..8bf13d7 --- /dev/null +++ b/stim/matlab/rtsEnviRandomForest2C_train.m @@ -0,0 +1,76 @@ +function RF = rtsEnviRandomForest2C_train(EnviFileName, EnviHeaderName, RoiImages, reference) + +%Creates a 2-class random forest classifier for an Envi BIP file using the +%provided ROI images. The resulting classifier uses regression to map +%class A to 1 and class B to 0. This allows the creation of ROC curves. +% +% EnviFilename - Name of an ENVI BIP file +% RoiImages - Cell array containing names of ROI images (masks) + +%default parameters +maxPixels = 200000; +threshold = 0.5; +nTrees = 100; +nThreads = 8; +%trainPixels = 200000; + +%determine the number of classes +nClasses = length(RoiImages); +%make sure that there are only two classes +if nClasses > 2 + disp('This classifier only supports 2 classes.'); + return; +end + +%for each class, load the training data +T = []; +F = []; +for c = 1:nClasses + + %load the class mask + maskImage = imread(RoiImages{c}); + maskBinary = (maskImage(:, :, 1) > 0)'; + + disp('------------------------------------------------------'); + %load epithelium spectra + disp(['Loading Training Class ' num2str(c) ' pixels: ' EnviFileName]); + tLoadTime = tic; + fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary); + Fc = rtsEnviRead(fid, maxPixels); + rtsEnviClose(fid); + + if c == 1 + Tc = ones(size(Fc, 2), 1); + else + Tc = zeros(size(Fc, 2), 1); + end + + disp(['Time: ' num2str(toc(tLoadTime)) 's']); + + %add features and targets to the final vectors + T = [T; Tc]; + F = [F; Fc']; +end + +%apply the reference +if nargin == 4 + Fnorm = repmat(F(:, reference), 1, size(F, 2)); + F = F./Fnorm; + F(:, reference) = []; +end + +%parallelize if specified +if nThreads > 1 + matlabpool('open', nThreads); + paraoptions = statset('UseParallel', 'always'); +end + +%train the classifier +disp('Creating a Random Forest classifier...'); +tTrainTime = tic; +RF = TreeBagger(nTrees, F, T, 'method', 'regression', 'Options',paraoptions); +disp(['Time: ' num2str(toc(tTrainTime)) 's']); + +if nThreads > 1 + matlabpool close +end \ No newline at end of file diff --git a/stim/matlab/rtsEnviRandomForest2C_validate.m b/stim/matlab/rtsEnviRandomForest2C_validate.m new file mode 100644 index 0000000..626a03c --- /dev/null +++ b/stim/matlab/rtsEnviRandomForest2C_validate.m @@ -0,0 +1,65 @@ +function [Croc, Cauc] = rtsEnviRandomForest2C_validate(RF, EnviFileName, EnviHeaderName, RoiImages, reference) + +%Validates a 2-class random forest classifier, creating ROC curves. + +%parameters +maxPixels = 200000; + +%determine the number of classes +nClasses = length(RoiImages); +%make sure that there are only two classes +if nClasses > 2 + disp('This classifier only supports 2 classes.'); + return; +end + +%for each class, load the validation data +T = []; +F = []; +for c = 1:nClasses + + %load the class mask + maskImage = imread(RoiImages{c}); + maskBinary = (maskImage(:, :, 1) > 0)'; + + disp('------------------------------------------------------'); + %load epithelium spectra + disp(['Loading Validation Class ' num2str(c) ' pixels: ' EnviFileName]); + tLoadTime = tic; + fid = rtsEnviOpen(EnviFileName, EnviHeaderName, maskBinary); + Fc = rtsEnviRead(fid, maxPixels); + rtsEnviClose(fid); + + if c == 1 + Tc = ones(size(Fc, 2), 1); + else + Tc = zeros(size(Fc, 2), 1); + end + + disp(['Time: ' num2str(toc(tLoadTime)) 's']); + + %add features and targets to the final vectors + T = [T; Tc]; + F = [F; Fc']; +end + +%apply the reference +if nargin == 5 + Fnorm = repmat(F(:, reference), 1, size(F, 2)); + F = F./Fnorm; + F(:, reference) = []; +end + +%classify the data (get the estimated posterior probability) +disp('Validating...'); +tClassTime = tic; +Tpost = predict(RF, F); +disp(['Time: ' num2str(toc(tClassTime)) 's']); + +%calculate and display the ROC curve +disp('Calculating ROC...'); +tRocTime = tic; +[Croc, Cauc, threshList] = rtsROC(Tpost, T); +disp(['Time: ' num2str(toc(tRocTime)) 's']); +disp(['AUC = ' num2str(Cauc)]); +plot(Croc(:, 1), Croc(:, 2)); \ No newline at end of file diff --git a/stim/matlab/rtsEnviRead.m b/stim/matlab/rtsEnviRead.m new file mode 100644 index 0000000..d46db6c --- /dev/null +++ b/stim/matlab/rtsEnviRead.m @@ -0,0 +1,68 @@ +function S = rtsEnviRead(fid, batchSize) + +%This function reads a batch of spectra from the given ENVI file ID +% fid = ENVI file ID created using rtsEnviOpen +% batchSize = number of spectra to read + +%if there is no mask, just load each spectrum in order +if(fid.mask == 1) + + %compute the new batch size in case we are near the eof + nSpectra = fid.header.samples * fid.header.lines; + remainingSpectra = nSpectra - fid.fpos; + + if(batchSize > remainingSpectra) + batchSize = remainingSpectra; + end + + S = fread(fid.fid, [fid.header.bands batchSize], '*float32'); + + %increment the fid counter + fid.fpos = fid.fpos + batchSize; + +%otherwise only load valid spectra +else + + %compute the new batch size in case we are near the eof + if(fid.fpos == 0) + remainingSpectra = nnz(fid.mask); + else + nSpectra = nnz(fid.mask); + maskRead = fid.mask(1:fid.fpos+1); + remainingSpectra = nSpectra - nnz(maskRead); + end + + if(batchSize > remainingSpectra) + batchSize = remainingSpectra; + end + + %allocate space for the spectra + S = zeros(fid.header.bands, batchSize); + + %for each spectrum in the batch + for s = 1:batchSize + + %while the current spectrum is invalid + skip = 0; + while (~fid.mask(fid.fpos + 1)) + %read the invalid spectrum + %invalid = fread(fid.fid, [fid.header.bands 1], '*float32'); + skip = skip + 1; + + %increment the file position + fid.fpos = fid.fpos + 1; + end + fseek(fid.fid, skip * fid.header.bands * 4, 'cof'); + + test = fread(fid.fid, [fid.header.bands 1], '*float32'); + if size(test) ~= size(S(:, s)) + size(test) + size(S(:, s)) + end + S(:, s) = test; + fid.fpos = fid.fpos + 1; + + end + +end + diff --git a/stim/matlab/rtsEnviSaveImage.m b/stim/matlab/rtsEnviSaveImage.m new file mode 100644 index 0000000..a22402b --- /dev/null +++ b/stim/matlab/rtsEnviSaveImage.m @@ -0,0 +1,41 @@ +function rtsEnviSaveImage(S, fileName, headerName, W) + +% S = 3D hyperspectral data +% W = list of wavelength values + +%save the image +rtsSaveRAW(S, fileName, 'float32', 'l'); + +%create the header file +headerFile = [fileName '.hdr']; +if nargin == 3 + headerFile = headerName; +end + +fid = fopen(headerFile, 'w'); + +%write the header and description +fprintf(fid, 'ENVI\n'); +fprintf(fid, 'description = {\n'); +fprintf(fid, ' File Saved using Matlab.}\n'); +fprintf(fid, 'samples = %d\n', size(S, 1)); +fprintf(fid, 'lines = %d\n', size(S, 2)); +fprintf(fid, 'bands = %d\n', size(S, 3)); +fprintf(fid, 'header offset = 0\n'); +fprintf(fid, 'file type = ENVI Standard\n'); +fprintf(fid, 'data type = 4\n'); +fprintf(fid, 'interleave = bsq\n'); +fprintf(fid, 'sensor type = Unknown\n'); +fprintf(fid, 'byte order = 0\n'); + +if nargin == 4 + %output wavelength units + fprintf(fid, 'wavelength = {\n'); + for i = 1:length(W)-1 + fprintf(fid, ' %f,\n', W(i)); + end + fprintf(fid, ' %f\n', W(length(W))); + fprintf(fid, '}\n'); +end + +fclose(fid); diff --git a/stim/matlab/rtsGaussianBlurND.m b/stim/matlab/rtsGaussianBlurND.m new file mode 100644 index 0000000..3699850 --- /dev/null +++ b/stim/matlab/rtsGaussianBlurND.m @@ -0,0 +1,30 @@ +function R = rtsGaussianBlurND(input, sigma) +%This function convolves an ND array with a gaussian kernel specified by +%sigma. This takes advantage of the separability of the kernel and is +%therefore very fast. + +%disp('begin***********************************************'); + +%get the number of dimensions for the convolution +dim = ndims(input); + +%initialize the result to the source +R = input; + + +for d=1:dim + %if the dimension is greater than 1 and the sigma not a dirac + if size(input, d) > 1 && sigma(d) > 0 + %create a 1D kernel + kernelshape = ones(1, dim); + kernelshape(d) = sigma(d)*6; + kernel = zeros(kernelshape); + kernel(:) = ndgauss(sigma(d)*6, sigma(d)); + + %perform the convolution + R = convn(R, kernel, 'same'); + end +end + + + \ No newline at end of file diff --git a/stim/matlab/rtsHSVtoRGB.m b/stim/matlab/rtsHSVtoRGB.m new file mode 100644 index 0000000..9770aea --- /dev/null +++ b/stim/matlab/rtsHSVtoRGB.m @@ -0,0 +1,29 @@ +function RGBval = rtsHSVtoRGB(HSVval) + +H = HSVval(1); +S = HSVval(2); +V = HSVval(3); + +C = V*S; +Hprime = H/60; +X = C*(1 - abs(mod(Hprime, 2) - 1)); + +RGBval = [0 0 0]; +if Hprime >= 0 && Hprime < 1 + RGBval = [C X 0]; +end +if Hprime >= 1 && Hprime < 2 + RGBval = [X C 0]; +end +if Hprime >= 2 && Hprime < 3 + RGBval = [0 C X]; +end +if Hprime >= 3 && Hprime < 4 + RGBval = [0 X C]; +end +if Hprime >= 4 && Hprime < 5 + RGBval = [X 0 C]; +end +if Hprime >= 5 && Hprime < 6 + RGBval = [C 0 X]; +end \ No newline at end of file diff --git a/stim/matlab/rtsInt2Str.m b/stim/matlab/rtsInt2Str.m new file mode 100644 index 0000000..06b9b8d --- /dev/null +++ b/stim/matlab/rtsInt2Str.m @@ -0,0 +1,20 @@ +function result = rtsInt2Str(num, digits) + + +%get the number of digits in the current number +temp = num; +current_digits = 0; +while temp >= 1 + temp = temp/10; + current_digits = current_digits+1; +end + +if current_digits > digits + result = int2str(num); +else + result = []; + for i = 1:digits - current_digits + result = [result '0']; + end + result = [result int2str(num)]; +end \ No newline at end of file diff --git a/stim/matlab/rtsLoadImageStack.m b/stim/matlab/rtsLoadImageStack.m new file mode 100644 index 0000000..15c921a --- /dev/null +++ b/stim/matlab/rtsLoadImageStack.m @@ -0,0 +1,33 @@ +function R = rtsLoadImageStack(directoryName) + +fileList = dir(directoryName); +fileList = fileList(3:length(fileList)); + +nFiles = length(fileList); + +%enable the progress bar +gui_active(1); +h = progressbar([], 0, ['Loading ' num2str(nFiles) ' Images...'], 'Load Stack'); + + +%load each file into a volume +for i=1:nFiles + fileName = [directoryName '\' fileList(i).name]; + image = imread(fileName); + + %allocate space + if i == 1 + R = zeros(size(image, 1), size(image, 2), nFiles, 'uint8'); + end + R(:, :, i) = image(:, :, 1); + + h = progressbar(h, 1/(nFiles)); + if ~gui_active + progressbar(h, -1); + break; + end +end + +progressbar(h, -1); + + diff --git a/stim/matlab/rtsMaskReconstruct.m b/stim/matlab/rtsMaskReconstruct.m new file mode 100644 index 0000000..eed102e --- /dev/null +++ b/stim/matlab/rtsMaskReconstruct.m @@ -0,0 +1,35 @@ +function I = rtsMaskReconstruct(data, mask, fillVal) + +%This function reconstructs an array of data created from a mask +% data = a VxD matrix, where D = positions and V = values +% mask = a binary image with D nonzero values + +%compute the number of values at each pixel +nV = size(data, 1); + +%allocate space +nX = size(mask, 1); +nY = size(mask, 2); +I = ones(nV, nX*nY) * fillVal; + +d=1; +%for each position +for p = 1:nX*nY + + %if the mask value at the position is true, copy values + if(mask(p) && d < size(data, 2)) + I(:, p) = data(:, d); + + %increment the data pointer + d = d + 1; + + %if the mask value at the position is zero, set all values to zero + else + I(:, p) = ones(nV, 1) * fillVal; + end + +end + +%reshape the array into an image +I = reshape(I', [nX nY nV]); + diff --git a/stim/matlab/rtsMonteCarloSphere.m b/stim/matlab/rtsMonteCarloSphere.m new file mode 100644 index 0000000..0d80b0b --- /dev/null +++ b/stim/matlab/rtsMonteCarloSphere.m @@ -0,0 +1,29 @@ +function S = rtsMonteCarloSphere(sqrtN, startAngle, endAngle) + +%allocate space for the samples +S = zeros(sqrtN^2, 2); + +if sqrtN == 1 + S = [0 0]; + return; +end +cosStart = cos(startAngle); +cosEnd = cos(endAngle); + +i=1; % array index +oneoverN = 1.0/sqrtN; +for a = 0:sqrtN-1 + for b = 0:sqrtN-1 + %generate unbiased distribution of spherical coords + U1 = rand(1, 1); + U2 = rand(1, 1); + %x = 1.0 - (a + U1) * oneoverN * (1.0 - cosEnd) + x = cosStart - (a + U1) * oneoverN * (1.0 - cosEnd); + y = (b + U2) * oneoverN; + theta = acos(x); + phi = 2.0 * pi * y; + S(i, 1) = theta; + S(i, 2) = phi; + i = i+1; + end +end diff --git a/stim/matlab/rtsOffsetPlots.m b/stim/matlab/rtsOffsetPlots.m new file mode 100644 index 0000000..f3e7e84 --- /dev/null +++ b/stim/matlab/rtsOffsetPlots.m @@ -0,0 +1,11 @@ +function R = rtsOffsetPlots(D, s) +%This function offsets a series of plots in D by intervals of s +%R = the resulting offset data +%D = the data as a 2D matrix +%s = the interval used as an offset + + nPlots = size(D, 2); + + offsetMat = repmat(1:nPlots, size(D, 1), 1) * s; + + R = D + offsetMat; \ No newline at end of file diff --git a/stim/matlab/rtsRAW2Images.m b/stim/matlab/rtsRAW2Images.m new file mode 100644 index 0000000..eea8065 --- /dev/null +++ b/stim/matlab/rtsRAW2Images.m @@ -0,0 +1,8 @@ +function rtsRAW2Images(source, destination_directory) + +for i=1:size(source, 3) + filename = [destination_directory '\' rtsInt2Str(i, 4) '.bmp']; + imwrite(source(:, :, i)', filename); + disp(filename); +end + \ No newline at end of file diff --git a/stim/matlab/rtsROC.m b/stim/matlab/rtsROC.m new file mode 100644 index 0000000..82721b1 --- /dev/null +++ b/stim/matlab/rtsROC.m @@ -0,0 +1,35 @@ +function [ROC, AUC, T] = rtsROC(thresholds, class) + +%Computes the ROC curves given a set of thresholds and class assignments +% class = boolean array describing actual class membership +% thresholds = threshold values for class assignment +% ROC = ROC curve as column vectors (1 = 1-specificity, 2 = sensitivity) +% AUC = area under the ROC curve computed using the trapezoid method +% T = threshold values associated with each 1-specificity value + +nValues = length(thresholds); +ROC = zeros(nValues, 2); + +%sort both arrays +[T, ix] = sort(thresholds, 'descend'); +C = class(ix); + +%compute the necessary global values +P = nnz(class); +N = nValues - P; + +for i = 1:nValues + TP = nnz(C(1:i)); + sensitivity = TP/P; + + FP = i - TP; + FPR = FP/N; + specificity = 1 - FPR; + + ROC(i, 1) = 1 - specificity; + ROC(i, 2) = sensitivity; +end + +AUC = trapz(ROC(:, 1), ROC(:, 2)); + + \ No newline at end of file diff --git a/stim/matlab/rtsRandomizeMask.m b/stim/matlab/rtsRandomizeMask.m new file mode 100644 index 0000000..62866d3 --- /dev/null +++ b/stim/matlab/rtsRandomizeMask.m @@ -0,0 +1,22 @@ +function R = rtsRandomizeMask(mask, N) + +%error checking +if N > nnz(mask) + N = nnz(mask); +end + +if N < 0 + N = 0; +end + +%get the indices of all nonzero values in the mask +ind = find(mask); + +%randomize the indices +rind = ind(randperm(size(ind, 1))); + +%create the new randomized mask (random subset of the old mask) +R = zeros(size(mask)); +R(rind(1:N)) = 1; + +R = R > 0; \ No newline at end of file diff --git a/stim/matlab/rtsSaveRAW.m b/stim/matlab/rtsSaveRAW.m new file mode 100644 index 0000000..042b5e5 --- /dev/null +++ b/stim/matlab/rtsSaveRAW.m @@ -0,0 +1,11 @@ +function rtsSaveRAW(data, filename, type, endian) +%Loads a RAW image file. +%LoadRAW(filename, x, y, z, c, type) returns a [CxXxYxZ] array representing +%RAW data loaded from disk. 'type' defines the data type to be loaded (ex. +%'uint8'. 'endian' defines the byte order: 'b' = big endian, 'l' = little +%endian. + +file_id = fopen(filename, 'w', endian); +vol = zeros(size(data, 1), size(data, 2), size(data, 3), size(data, 4)); +vol(:) = fwrite(file_id, data, type); +fclose(file_id); \ No newline at end of file diff --git a/stim/matlab/stimBrewerColormap.m b/stim/matlab/stimBrewerColormap.m new file mode 100644 index 0000000..34568a0 --- /dev/null +++ b/stim/matlab/stimBrewerColormap.m @@ -0,0 +1,28 @@ +function result = stimBrewerColormap(R) + +%returns a Brewer colormap with the specified resolution R + +ctrlPts = zeros(11, 3); + +ctrlPts(1, :) = [0.192157, 0.211765, 0.584314]; +ctrlPts(2, :) = [0.270588, 0.458824, 0.705882]; +ctrlPts(3, :) = [0.454902, 0.678431, 0.819608]; +ctrlPts(4, :) = [0.670588, 0.85098, 0.913725]; +ctrlPts(5, :) = [0.878431, 0.952941, 0.972549]; +ctrlPts(6, :) = [1, 1, 0.74902]; +ctrlPts(7, :) = [0.996078, 0.878431, 0.564706]; +ctrlPts(8, :) = [0.992157, 0.682353, 0.380392]; +ctrlPts(9, :) = [0.956863, 0.427451, 0.262745]; +ctrlPts(10, :) = [0.843137, 0.188235, 0.152941]; +ctrlPts(11, :) = [0.647059, 0, 0.14902]; + +X = 1:11; + +r = 1:11/R:11; + +R = interp1(X, ctrlPts(:, 1), r); +G = interp1(X, ctrlPts(:, 2), r); +B = interp1(X, ctrlPts(:, 3), r); + +result = [R' G' B']; + diff --git a/stim/optics/beam.h b/stim/optics/beam.h new file mode 100644 index 0000000..8587bb4 --- /dev/null +++ b/stim/optics/beam.h @@ -0,0 +1,203 @@ +#ifndef RTS_BEAM +#define RTS_BEAM + +#include "../math/vector.h" +#include "../math/function.h" +#include "../optics/planewave.h" +#include + +namespace stim{ + +template +class beam : public planewave

+{ +public: + enum beam_type {Uniform, Bartlett, Hamming, Hanning}; + +private: + + P _na[2]; //numerical aperature of the focusing optics + vec

f; //focal point + function apod; //apodization function + unsigned int apod_res; //resolution of apodization filter functions + + void apod_uniform() + { + apod = (P)1; + } + void apod_bartlett() + { + apod = (P)1; + apod.insert((P)1, (P)0); + } + void apod_hanning() + { + apod = (P)0; + P x, y; + for(unsigned int n=0; n k = rts::vec

(0, 0, rtsTAU), + vec

_E0 = rts::vec

(1, 0, 0), + beam_type _apod = Uniform) + : planewave

(k, _E0) + { + _na[0] = (P)0.0; + _na[1] = (P)1.0; + f = vec

( (P)0, (P)0, (P)0 ); + apod_res = 256; //set the default resolution for apodization filters + set_apod(_apod); //set the apodization function type + } + + beam

refract(rts::vec

kn) const{ + + beam

new_beam; + new_beam._na[0] = _na[0]; + new_beam._na[1] = _na[1]; + + + rts::planewave

pw = planewave

::bend(kn); + //std::cout< > mc(unsigned int N = 100000, unsigned int seed = 0) const + { + /*Create Monte-Carlo samples of a cassegrain objective by performing uniform sampling + of a sphere and projecting these samples onto an inscribed sphere. + + seed = seed for the random number generator + */ + srand(seed); //seed the random number generator + + vec

k_hat = beam::k.norm(); + + ///compute the rotation operator to transform (0, 0, 1) to k + P cos_angle = k_hat.dot(rts::vec

(0, 0, 1)); + rts::matrix rotation; + + //if the cosine of the angle is -1, the rotation is just a flip across the z axis + if(cos_angle == -1){ + rotation(2, 2) = -1; + } + else if(cos_angle != 1.0) + { + rts::vec

r_axis = rts::vec

(0, 0, 1).cross(k_hat).norm(); //compute the axis of rotation + P angle = acos(cos_angle); //compute the angle of rotation + rts::quaternion

quat; //create a quaternion describing the rotation + quat.CreateRotation(angle, r_axis); + rotation = quat.toMatrix3(); //compute the rotation matrix + } + + //find the phi values associated with the cassegrain ring + P PHI[2]; + PHI[0] = (P)asin(_na[0]); + PHI[1] = (P)asin(_na[1]); + + //calculate the z-axis cylinder coordinates associated with these angles + P Z[2]; + Z[0] = cos(PHI[0]); + Z[1] = cos(PHI[1]); + P range = Z[0] - Z[1]; + + std::vector< planewave

> samples; //create a vector of plane waves + + //draw a distribution of random phi, z values + P z, phi, theta; + for(int i=0; i spherical(1, theta, phi); //convert from spherical to cartesian coordinates + rts::vec

cart = spherical.sph2cart(); + vec

k_prime = rotation * cart; //create a sample vector + + //store a wave refracted along the given direction + //std::cout<<"k prime: "<::refract(k_prime) * apod(phi/PHI[1])); + } + + return samples; + } + + std::string str() + { + std::stringstream ss; + ss<<"Beam:"< +__global__ void gpu_planewave2efield(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, + planewave w, rect q) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //get the current position + vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); + vec r(p[0], p[1], p[2]); + + complex x( 0.0f, w.k.dot(r) ); + + if(Y == NULL) //if this is a scalar simulation + X[i] += w.E0.len() * exp(x); //use the vector magnitude as the plane wave amplitude + else + { + X[i] += w.E0[0] * exp(x); + Y[i] += w.E0[1] * exp(x); + Z[i] += w.E0[2] * exp(x); + } +} + +template +__global__ void gpu_efield_magnitude(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, T* M) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + if(Y == NULL) //if this is a scalar simulation + M[i] = X[i].abs(); //compute the magnitude of the X component + else + { + M[i] = rts::vec(X[i].abs(), Y[i].abs(), Z[i].abs()).len(); + //M[i] = Z[i].abs(); + } +} + +template +__global__ void gpu_efield_real_magnitude(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, T* M) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + if(Y == NULL) //if this is a scalar simulation + M[i] = X[i].abs(); //compute the magnitude of the X component + else + { + M[i] = rts::vec(X[i].real(), Y[i].real(), Z[i].real()).len(); + //M[i] = Z[i].abs(); + } +} + +template +__global__ void gpu_efield_polarization(complex* X, complex* Y, complex* Z, + unsigned int r0, unsigned int r1, + T* Px, T* Py, T* Pz) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //compute the field polarization + Px[i] = X[i].abs(); + Py[i] = Y[i].abs(); + Pz[i] = Z[i].abs(); + +} + +/* This function computes the sum of two complex fields and stores the result in *dest +*/ +template +__global__ void gpu_efield_sum(complex* dest, complex* src, unsigned int r0, unsigned int r1) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //sum the fields + dest[i] += src[i]; +} + +/* This class implements a discrete representation of an electromagnetic field + in 2D. The majority of this representation is done on the GPU. +*/ +template +class efield +{ +protected: + + bool vector; + + //gpu pointer to the field data + rts::complex* X; + rts::complex* Y; + rts::complex* Z; + + //resolution of the discrete field + unsigned int R[2]; + + //shape of the 2D field + rect pos; + + void from_planewave(planewave p) + { + unsigned int SQRT_BLOCK = 16; + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + + gpu_planewave2efield <<>> (X, Y, Z, R[0], R[1], p, pos); + } + + void init(unsigned int res0, unsigned int res1, bool _vector) + { + vector = _vector; //initialize field type + + X = Y = Z = NULL; //initialize all pointers to NULL + R[0] = res0; + R[1] = res1; + + //allocate memory on the gpu + cudaMalloc(&X, sizeof(rts::complex) * R[0] * R[1]); + cudaMemset(X, 0, sizeof(rts::complex) * R[0] * R[1]); + + if(vector) + { + cudaMalloc(&Y, sizeof(rts::complex) * R[0] * R[1]); + cudaMemset(Y, 0, sizeof(rts::complex) * R[0] * R[1]); + + cudaMalloc(&Z, sizeof(rts::complex) * R[0] * R[1]); + cudaMemset(Z, 0, sizeof(rts::complex) * R[0] * R[1]); + } + } + + void destroy() + { + if(X != NULL) cudaFree(X); + if(Y != NULL) cudaFree(Y); + if(Z != NULL) cudaFree(Z); + } + + void shallowcpy(const rts::efield & src) + { + vector = src.vector; + R[0] = src.R[0]; + R[1] = src.R[1]; + } + + void deepcpy(const rts::efield & src) + { + //perform a shallow copy + shallowcpy(src); + + //allocate memory on the gpu + if(src.X != NULL) + { + cudaMalloc(&X, sizeof(rts::complex) * R[0] * R[1]); + cudaMemcpy(X, src.X, sizeof(rts::complex) * R[0] * R[1], cudaMemcpyDeviceToDevice); + } + if(src.Y != NULL) + { + cudaMalloc(&Y, sizeof(rts::complex) * R[0] * R[1]); + cudaMemcpy(Y, src.Y, sizeof(rts::complex) * R[0] * R[1], cudaMemcpyDeviceToDevice); + } + if(src.Z != NULL) + { + cudaMalloc(&Z, sizeof(rts::complex) * R[0] * R[1]); + cudaMemcpy(Z, src.Z, sizeof(rts::complex) * R[0] * R[1], cudaMemcpyDeviceToDevice); + } + } + +public: + efield(unsigned int res0, unsigned int res1, bool _vector = true) + { + init(res0, res1, _vector); + //pos = rts::rect(rts::vec(-10, 0, -10), rts::vec(-10, 0, 10), rts::vec(10, 0, 10)); + } + + //destructor + ~efield() + { + destroy(); + } + + ///Clear the field - set all points to zero + void clear() + { + cudaMemset(X, 0, sizeof(rts::complex) * R[0] * R[1]); + + if(vector) + { + cudaMemset(Y, 0, sizeof(rts::complex) * R[0] * R[1]); + cudaMemset(Z, 0, sizeof(rts::complex) * R[0] * R[1]); + } + } + + void position(rect _p) + { + pos = _p; + } + + //access functions + complex* x(){ + return X; + } + complex* y(){ + return Y; + } + complex* z(){ + return Z; + } + unsigned int Ru(){ + return R[0]; + } + unsigned int Rv(){ + return R[1]; + } + rect p(){ + return pos; + } + + std::string str() + { + stringstream ss; + ss< & operator= (const efield & rhs) + { + destroy(); //destroy any previous information about this field + deepcpy(rhs); //create a deep copy + return *this; //return the current object + } + + //assignment operator: build an electric field from a plane wave + efield & operator= (const planewave & rhs) + { + + clear(); //clear any previous field data + from_planewave(rhs); //create a field from the planewave + return *this; //return the current object + } + + //assignment operator: add an existing electric field + efield & operator+= (const efield & rhs) + { + //if this field and the source field represent the same regions in space + if(R[0] == rhs.R[0] && R[1] == rhs.R[1] && pos == rhs.pos) + { + int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //sum the fields + gpu_efield_sum <<>> (X, rhs.X, R[0], R[1]); + if(Y != NULL) + { + gpu_efield_sum <<>> (Y, rhs.Y, R[0], R[1]); + gpu_efield_sum <<>> (Z, rhs.Z, R[0], R[1]); + } + } + else + { + std::cout<<"ERROR in efield: The two summed fields do not represent the same geometry."< & operator= (const rts::beam & rhs) + { + //get a vector of monte-carlo samples + std::vector< rts::planewave > p_list = rhs.mc(); + + clear(); //clear any previous field data + for(unsigned int i = 0; i < p_list.size(); i++) + from_planewave(p_list[i]); + return *this; + } + + + //return a scalar field representing field magnitude + realfield mag() + { + int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create a scalar field to store the result + realfield M(R[0], R[1]); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //compute the magnitude and store it in a scalar field + gpu_efield_magnitude <<>> (X, Y, Z, R[0], R[1], M.ptr(0)); + + return M; + } + + //return a sacalar field representing the field magnitude at an infinitely small point in time + realfield real_mag() + { + int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create a scalar field to store the result + realfield M(R[0], R[1]); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //compute the magnitude and store it in a scalar field + gpu_efield_real_magnitude <<>> (X, Y, Z, R[0], R[1], M.ptr(0)); + + return M; + } + + //return a vector field representing field polarization + realfield polarization() + { + if(!vector) + { + std::cout<<"ERROR: Cannot compute polarization of a scalar field."< Pol(R[0], R[1]); //create a vector field to store the result + + //compute the polarization and store it in the vector field + gpu_efield_polarization <<>> (X, Y, Z, R[0], R[1], Pol.ptr(0), Pol.ptr(1), Pol.ptr(2)); + + return Pol; //return the vector field + } + + ////////FRIENDSHIP + //friend void operator<< (rts::efield &ef, rts::halfspace hs); + + +}; + + + +} //end namespace rts + +#endif diff --git a/stim/optics/esphere.cuh b/stim/optics/esphere.cuh new file mode 100644 index 0000000..1b5a559 --- /dev/null +++ b/stim/optics/esphere.cuh @@ -0,0 +1,162 @@ +#ifndef RTS_ESPHERE +#define RTS_ESPHERE + +#include "../math/complex.h" +#include "../math/bessel.h" +#include "../visualization/colormap.h" +#include "../optics/planewave.h" +#include "../cuda/devices.h" +#include "../optics/efield.cuh" + +namespace stim{ + +/* This class implements a discrete representation of an electromagnetic field + in 2D scattered by a sphere. This class implements Mie scattering. +*/ +template +class esphere : public efield

+{ +private: + stim::complex

n; //sphere refractive index + P a; //sphere radius + + //parameters dependent on wavelength + unsigned int Nl; //number of orders for the calculation + rts::complex

* A; //internal scattering coefficients + rts::complex

* B; //external scattering coefficients + + void calcNl(P kmag) + { + //return ceil( ((P)6.282 * a) / lambda + 4 * pow( ((P)6.282 * a) / lambda, ((P)1/(P)3)) + 2); + Nl = ceil( kmag*a + 4 * pow(kmag * a, (P)1/(P)3) + 2); + } + + void calcAB(P k, unsigned int Nl, rts::complex

* A, rts::complex

* B) + { + /* These calculations require double precision, so they are computed + using doubles and converted to P at the end. + + Input: + + k = magnitude of the k vector (tau/lambda) + Nl = highest order coefficient ([0 Nl] are computed) + */ + + //clear the previous coefficients + rts::complex

* cpuA = (rts::complex

*)malloc(sizeof(rts::complex

) * (Nl+1)); + rts::complex

* cpuB = (rts::complex

*)malloc(sizeof(rts::complex

) * (Nl+1)); + + //convert to an std complex value + complex nc = (rts::complex)n; + + //compute the magnitude of the k vector + complex kna = nc * k * (double)a; + + //compute the arguments k*a and k*n*a + complex ka(k * a, 0.0); + + //allocate space for the Bessel functions of the first and second kind (and derivatives) + unsigned int bytes = sizeof(complex) * (Nl + 1); + complex* cjv_ka = (complex*)malloc(bytes); + complex* cyv_ka = (complex*)malloc(bytes); + complex* cjvp_ka = (complex*)malloc(bytes); + complex* cyvp_ka = (complex*)malloc(bytes); + complex* cjv_kna = (complex*)malloc(bytes); + complex* cyv_kna = (complex*)malloc(bytes); + complex* cjvp_kna = (complex*)malloc(bytes); + complex* cyvp_kna = (complex*)malloc(bytes); + + //allocate space for the spherical Hankel functions and derivative + complex* chv_ka = (complex*)malloc(bytes); + complex* chvp_ka = (complex*)malloc(bytes); + + //compute the bessel functions using the CPU-based algorithm + double vm; + cbessjyva_sph(Nl, ka, vm, cjv_ka, cyv_ka, cjvp_ka, cyvp_ka); + cbessjyva_sph(Nl, kna, vm, cjv_kna, cyv_kna, cjvp_kna, cyvp_kna); + + //compute A for each order + complex i(0, 1); + complex a, b, c, d; + complex An, Bn; + for(int l=0; l<=Nl; l++) + { + //compute the Hankel functions from j and y + chv_ka[l] = cjv_ka[l] + i * cyv_ka[l]; + chvp_ka[l] = cjvp_ka[l] + i * cyvp_ka[l]; + + //Compute A (internal scattering coefficient) + //compute the numerator and denominator for A + a = cjv_ka[l] * chvp_ka[l] - cjvp_ka[l] * chv_ka[l]; + b = cjv_kna[l] * chvp_ka[l] - chv_ka[l] * cjvp_kna[l] * nc; + + //calculate A and add it to the list + rts::complex An = (2.0 * l + 1.0) * pow(i, l) * (a / b); + cpuA[l] = (rts::complex

)An; + + //Compute B (external scattering coefficient) + c = cjv_ka[l] * cjvp_kna[l] * nc - cjv_kna[l] * cjvp_ka[l]; + d = cjv_kna[l] * chvp_ka[l] - chv_ka[l] * cjvp_kna[l] * nc; + + //calculate B and add it to the list + rts::complex Bn = (2.0 * l + 1.0) * pow(i, l) * (c / d); + cpuB[l] = (rts::complex

)Bn; + + std::cout<<"A: "<) * (Nl+1)); //allocate memory for new coefficients + cudaMalloc(&B, sizeof(rts::complex

) * (Nl+1)); + + //copy the calculations from the CPU to the GPU + cudaMemcpy(A, cpuA, sizeof(rts::complex

) * (Nl+1), cudaMemcpyDeviceToHost); + cudaMemcpy(B, cpuB, sizeof(rts::complex

) * (Nl+1), cudaMemcpyDeviceToHost); + } + +public: + + esphere(unsigned int res0, unsigned int res1, P _a, rts::complex

_n, bool _scalar = false) : + efield

(res0, res1, _scalar) + { + std::cout<<"Sphere scattered field created."< & operator= (const planewave

& rhs) + { + calcNl(rhs.kmag()); //compute the number of scattering coefficients + std::cout<<"Nl: "<::str()< +__global__ void gpu_halfspace_pw2ef(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, + plane P, planewave w, rect q, bool at_surface = false) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //get the current position + vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); + + if(at_surface){ + if(P.side(p) > 0) + return; + } + else{ + if(P.side(p) >= 0) + return; + } + + //if the current position is on the wrong side of the plane + + //vec r(p[0], p[1], p[2]); + + complex x( 0.0f, w.kvec().dot(p) ); + //complex phase( 0.0f, w.phase()); + + if(Y == NULL) //if this is a scalar simulation + X[i] += w.E().len() * exp(x); //use the vector magnitude as the plane wave amplitude + else + { + //X[i] = Y[i] = Z[i] = 1; + X[i] += w.E()[0] * exp(x);// * exp(phase); + Y[i] += w.E()[1] * exp(x);// * exp(phase); + Z[i] += w.E()[2] * exp(x);// * exp(phase); + } +} + +//GPU kernel to compute the electric displacement field +template +__global__ void gpu_halfspace_pw2df(complex* X, complex* Y, complex* Z, unsigned int r0, unsigned int r1, + plane P, planewave w, rect q, T n, bool at_surface = false) +{ + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //get the current position + vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); + + if(at_surface){ + if(P.side(p) > 0) + return; + } + else{ + if(P.side(p) >= 0) + return; + } + + //if the current position is on the wrong side of the plane + + //vec r(p[0], p[1], p[2]); + + complex x( 0.0f, w.kvec().dot(p) ); + //complex phase( 0.0f, w.phase()); + + //vec< complex > testE(1, 0, 0); + + + + if(Y == NULL) //if this is a scalar simulation + X[i] += w.E().len() * exp(x); //use the vector magnitude as the plane wave amplitude + else + { + plane< complex > cplane = plane< complex, 3>(P); + vec< complex > E_para;// = cplane.parallel(w.E()); + vec< complex > E_perp;// = cplane.perpendicular(w.E()) * (n*n); + cplane.decompose(w.E(), E_para, E_perp); + T epsilon = n*n; + + X[i] += (E_para[0] + E_perp[0] * epsilon) * exp(x); + Y[i] += (E_para[1] + E_perp[1] * epsilon) * exp(x); + Z[i] += (E_para[2] + E_perp[2] * epsilon) * exp(x); + } +} + +//computes a scalar field containing the refractive index of the half-space at each point +template +__global__ void gpu_halfspace_n(T* n, unsigned int r0, unsigned int r1, rect q, plane P, T n0, T n1){ + + int iu = blockIdx.x * blockDim.x + threadIdx.x; + int iv = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(iu >= r0 || iv >= r1) return; + + //compute the index into the field + int i = iv*r0 + iu; + + //get the current position + vec p = q( (T)iu/(T)r0, (T)iv/(T)r1 ); + + //set the appropriate refractive index + if(P.side(p) < 0) n[i] = n0; + else n[i] = n1; +} + +template +class halfspace +{ +private: + rts::plane S; //surface plane splitting the half space + rts::complex n0; //refractive index at the front of the plane + rts::complex n1; //refractive index at the back of the plane + + //lists of waves in front (pw0) and behind (pw1) the plane + std::vector< rts::planewave > w0; + std::vector< rts::planewave > w1; + + //rts::planewave pi; //incident plane wave + //rts::planewave pr; //plane wave reflected from the surface + //rts::planewave pt; //plane wave transmitted through the surface + + void init(){ + n0 = 1.0; + n1 = 1.5; + } + + //compute which side of the interface is hit by the incoming plane wave (0 = front, 1 = back) + bool facing(planewave p){ + if(p.kvec().dot(S.norm()) > 0) + return 1; + else + return 0; + } + + T calc_theta_i(vec v){ + + vec v_hat = v.norm(); + + //compute the cosine of the angle between k_hat and the plane normal + T cos_theta_i = v_hat.dot(S.norm()); + + return acos(abs(cos_theta_i)); + } + + T calc_theta_t(T ni_nt, T theta_i){ + + T sin_theta_t = ni_nt * sin(theta_i); + return asin(sin_theta_t); + } + + +public: + + //constructors + halfspace(){ + init(); + } + + halfspace(T na, T nb){ + init(); + n0 = na; + n1 = nb; + } + + halfspace(T na, T nb, plane p){ + n0 = na; + n1 = nb; + S = p; + } + + //compute the transmitted and reflective waves given the incident (vacuum) plane wave p + void incident(rts::planewave p){ + + planewave r, t; + p.scatter(S, n1.real()/n0.real(), r, t); + + //std::cout<<"i+r: "< b, unsigned int N = 10000){ + + //generate a plane wave decomposition for the beam + std::vector< planewave > pw_list = b.mc(N); + + //calculate the reflected and refracted waves for each incident wave + for(unsigned int w = 0; w < pw_list.size(); w++){ + incident(pw_list[w]); + } + } + + //return the electric field at the specified resolution and position + rts::efield E(unsigned int r0, unsigned int r1, rect R){ + + int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((r0 + SQRT_BLOCK -1)/SQRT_BLOCK, (r1 + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //create an electric field + rts::efield ef(r0, r1); + ef.position(R); + + //render each plane wave + + //plane waves at the surface front + for(unsigned int w = 0; w < w0.size(); w++){ + //std::cout<<"w0 "< <<>> (ef.x(), ef.y(), ef.z(), r0, r1, S, w0[w], ef.p()); + } + + //plane waves at the surface back + for(unsigned int w = 0; w < w1.size(); w++){ + //std::cout<<"w1 "< <<>> (ef.x(), ef.y(), ef.z(), r0, r1, -S, w1[w], ef.p(), true); + } + + return ef; + } + + //return the electric displacement at the specified resolution and position + rts::efield D(unsigned int r0, unsigned int r1, rect R){ + + int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((r0 + SQRT_BLOCK -1)/SQRT_BLOCK, (r1 + SQRT_BLOCK - 1)/SQRT_BLOCK); + + //create a complex vector field + rts::efield df(r0, r1); + df.position(R); + + //render each plane wave + + //plane waves at the surface front + for(unsigned int w = 0; w < w0.size(); w++){ + //std::cout<<"w0 "< <<>> (df.x(), df.y(), df.z(), r0, r1, S, w0[w], df.p(), n0.real()); + } + + //plane waves at the surface back + for(unsigned int w = 0; w < w1.size(); w++){ + //std::cout<<"w1 "< <<>> (df.x(), df.y(), df.z(), r0, r1, -S, w1[w], df.p(), n1.real(), true); + } + + return df; + } + + realfield nfield(unsigned int Ru, unsigned int Rv, rect p){ + + int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size + int SQRT_BLOCK = (int)std::sqrt((float)maxThreads); + + //create one thread for each detector pixel + dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK); + dim3 dimGrid((Ru + SQRT_BLOCK -1)/SQRT_BLOCK, (Rv + SQRT_BLOCK - 1)/SQRT_BLOCK); + + realfield n(Ru, Rv); //create a scalar field to store the result of n + + rts::gpu_halfspace_n <<>> (n.ptr(), Ru, Rv, p, S, n0.real(), n1.real()); + + return n; + } + + std::string str(){ + std::stringstream ss; + ss<<"Half Space-------------"< (rts::efield &ef, rts::halfspace hs); + +}; + + + + +} + + +#endif diff --git a/stim/optics/material.h b/stim/optics/material.h new file mode 100644 index 0000000..fb97cd6 --- /dev/null +++ b/stim/optics/material.h @@ -0,0 +1,153 @@ +#ifndef RTS_MATERIAL_H +#define RTS_MATERIAL_H + +#include +#include +#include +#include +#include +#include +#include +#include "../math/complex.h" +#include "../math/constants.h" +#include "../math/function.h" + +namespace stim{ + +//Material class - default representation for the material property is the refractive index (RI) +template +class material : public function< T, complex >{ + +public: + enum wave_property{microns, inverse_cm}; + enum material_property{ri, absorbance}; + +private: + + using function< T, complex >::X; + using function< T, complex >::Y; + using function< T, complex >::insert; + using function< T, complex >::bounding; + + std::string name; //name for the material (defaults to file name) + + void process_header(std::string str, wave_property& wp, material_property& mp){ + + std::stringstream ss(str); //create a stream from the data string + std::string line; + std::getline(ss, line); //get the first line as a string + while(line[0] == '#'){ //continue looping while the line is a comment + + std::stringstream lstream(line); //create a stream from the line + lstream.ignore(); //ignore the first character ('#') + + std::string prop; //get the property name + lstream>>prop; + + if(prop == "X"){ + std::string wp_name; + lstream>>wp_name; + if(wp_name == "microns") wp = microns; + else if(wp_name == "inverse_cm") wp = inverse_cm; + } + else if(prop == "Y"){ + std::string mp_name; + lstream>>mp_name; + if(mp_name == "ri") mp = ri; + else if(mp_name == "absorbance") mp = absorbance; + } + + std::getline(ss, line); //get the next line + } + + function< T, stim::complex >::process_string(str); + } + + void from_inverse_cm(){ + //convert inverse centimeters to wavelength (in microns) + for(unsigned int i=0; i(1, 0); + } + + +public: + + material(std::string filename, wave_property wp, material_property mp){ + name = filename; + load(filename, wp, mp); + } + + material(std::string filename){ + name = filename; + load(filename); + } + + material(){ + init(); + } + + complex getN(T lambda){ + return function< T, complex >::linear(lambda); + } + + void load(std::string filename, wave_property wp, material_property mp){ + + //load the file as a function + function< T, complex >::load(filename); + } + + void load(std::string filename){ + + wave_property wp = inverse_cm; + material_property mp = ri; + //turn the file into a string + std::ifstream t(filename.c_str()); //open the file as a stream + + if(!t){ + std::cout<<"ERROR: Couldn't open the material file '"<(t)), + std::istreambuf_iterator()); + + //process the header information + process_header(str, wp, mp); + + //convert units + if(wp == inverse_cm) + from_inverse_cm(); + //set the bounding values + bounding[0] = Y[0]; + bounding[1] = Y.back(); + } + std::string str(){ + std::stringstream ss; + ss< >::str(); + return ss.str(); + } + std::string get_name(){ + return name; + } + + void set_name(std::string str){ + name = str; + } + +}; + +} + + + + +#endif diff --git a/stim/optics/mirst-1d.cuh b/stim/optics/mirst-1d.cuh new file mode 100644 index 0000000..9279087 --- /dev/null +++ b/stim/optics/mirst-1d.cuh @@ -0,0 +1,447 @@ +#include "../optics/material.h" +#include "../math/complexfield.cuh" +#include "../math/constants.h" +//#include "../envi/bil.h" + +#include "cufft.h" + +#include +#include + +namespace stim{ + +//this function writes a sinc function to "dest" such that an iFFT produces a slab +template +__global__ void gpu_mirst1d_layer_fft(complex* dest, complex* ri, + T* src, T* zf, + T w, unsigned int zR, unsigned int nuR){ + //dest = complex field representing the sample + //ri = refractive indices for each wavelength + //src = intensity of the light source for each wavelength + //zf = z position of the slab interface for each wavelength (accounting for optical path length) + //w = width of the slab (in pixels) + //zR = number of z-axis samples + //nuR = number of wavelengths + + //get the current coordinate in the plane slice + int ifz = blockIdx.x * blockDim.x + threadIdx.x; + int inu = blockIdx.y * blockDim.y + threadIdx.y; + + //make sure that the thread indices are in-bounds + if(inu >= nuR || ifz >= zR) return; + + int i = inu * zR + ifz; + + T fz; + if(ifz < zR/2) + fz = ifz / (T)zR; + else + fz = -(zR - ifz) / (T)zR; + + //if the slab starts outside of the simulation domain, just return + if(zf[inu] >= zR) return; + + //fill the array along z with a sinc function representing the Fourier transform of the layer + + T opl = w * ri[inu].real(); //optical path length + + //handle the case where the slab goes outside the simulation domain + if(zf[inu] + opl >= zR) + opl = zR - zf[inu]; + + if(opl == 0) return; + + //T l = w * ri[inu].real(); + //complex e(0.0, -2 * PI * fz * (zf[inu] + zR/2 - l/2.0)); + complex e(0, -2 * stimPI * fz * (zf[inu] + opl/2)); + + complex eta = ri[inu] * ri[inu] - 1; + + //dest[i] = fz;//exp(e) * m[inu] * src[inu] * sin(PI * fz * l) / (PI * fz); + if(ifz == 0) + dest[i] += opl * exp(e) * eta * src[inu]; + else + dest[i] += opl * exp(e) * eta * src[inu] * sin(stimPI * fz * opl) / (stimPI * fz * opl); +} + +template +__global__ void gpu_mirst1d_increment_z(T* zf, complex* ri, T w, unsigned int S){ + //zf = current z depth (optical path length) in pixels + //ri = refractive index of the material + //w = actual width of the layer (in pixels) + + + //compute the index for this thread + int i = blockIdx.x * blockDim.x + threadIdx.x; + if(i >= S) return; + + if(ri == NULL) + zf[i] += w; + else + zf[i] += ri[i].real() * w; +} + +//apply the 1D MIRST filter to an existing sample (overwriting the sample) +template +__global__ void gpu_mirst1d_apply_filter(complex* sampleFFT, T* lambda, + T dFz, + T inNA, T outNA, + unsigned int lambdaR, unsigned int zR, + T sigma = 0){ + //sampleFFT = the sample in the Fourier domain (will be overwritten) + //lambda = list of wavelengths + //dFz = delta along the Fz axis in the frequency domain + //inNA = NA of the internal obscuration + //outNA = NA of the objective + //zR = number of pixels along the Fz axis (same as the z-axis) + //lambdaR = number of wavelengths + //sigma = width of the Gaussian source + int ifz = blockIdx.x * blockDim.x + threadIdx.x; + int inu = blockIdx.y * blockDim.y + threadIdx.y; + + if(inu >= lambdaR || ifz >= zR) return; + + //calculate the index into the sample FT + int i = inu * zR + ifz; + + //compute the frequency (and set all negative spatial frequencies to zero) + T fz; + if(ifz < zR / 2) + fz = ifz * dFz; + //if the spatial frequency is negative, set it to zero and exit + else{ + sampleFFT[i] = 0; + return; + } + + //compute the frequency in inverse microns + T nu = 1/lambda[inu]; + + //determine the radius of the integration circle + T nu_sq = nu * nu; + T fz_sq = (fz * fz) / 4; + + //cut off frequencies above the diffraction limit + T r; + if(fz_sq < nu_sq) + r = sqrt(nu_sq - fz_sq); + else + r = 0; + + //account for the optics + T Q = 0; + if(r > nu * inNA && r < nu * outNA) + Q = 1; + + //account for the source + //T sigma = 30.0; + T s = exp( - (r*r * sigma*sigma) / 2 ); + //T s=1; + + //compute the final filter + T mirst = 0; + if(fz != 0) + mirst = 2 * stimPI * r * s * Q * (1/fz); + + sampleFFT[i] *= mirst; + +} + +/*This object performs a 1-dimensional (layered) MIRST simulation +*/ +template +class mirst1d{ + +private: + unsigned int Z; //z-axis resolution + unsigned int pad; //pixel padding on either side of the sample + + std::vector< material > matlist; //list of materials + std::vector< T > layers; //list of layer thicknesses + + std::vector< T > lambdas; //list of wavelengths that are being simulated + unsigned int S; //number of wavelengths (size of "lambdas") + + T NA[2]; //numerical aperature (central obscuration and outer diameter) + + function source_profile; //profile (spectrum) of the source (expressed in inverse centimeters) + + complexfield scratch; //scratch GPU memory used to build samples, transforms, etc. + + void fft(int direction = CUFFT_FORWARD){ + + unsigned padZ = Z + pad; + + //create cuFFT handles + cufftHandle plan; + cufftResult result; + + if(sizeof(T) == 4) + result = cufftPlan1d(&plan, padZ, CUFFT_C2C, lambdas.size()); //single precision + else + result = cufftPlan1d(&plan, padZ, CUFFT_Z2Z, lambdas.size()); //double precision + + //check for Plan 1D errors + if(result != CUFFT_SUCCESS){ + std::cout<<"Error creating CUFFT plan for computing the FFT:"<(Z + pad , lambdas.size()); + scratch = 0; + } + + //get the list of scattering efficiency (eta) values for a specified layer + std::vector< complex > layer_etas(unsigned int l){ + + std::vector< complex > etas; + + //fill the list of etas + for(unsigned int i=0; i* gpuRi; + HANDLE_ERROR(cudaMalloc( (void**)&gpuRi, sizeof(complex) * S)); + + //allocate memory for the source profile + T* gpuSrc; + HANDLE_ERROR(cudaMalloc( (void**)&gpuSrc, sizeof(T) * S)); + + complex ri; + T source; + //store the refractive index and source profile in a CPU array + for(int inu=0; inu), cudaMemcpyHostToDevice )); + + //save the source profile to the GPU + source = source_profile(10000 / lambdas[inu]); + HANDLE_ERROR(cudaMemcpy( gpuSrc + inu, &source, sizeof(T), cudaMemcpyHostToDevice )); + + } + + //create one thread for each pixel of the field slice + dim3 gridDim, blockDim; + cuda_params(gridDim, blockDim); + stim::gpu_mirst1d_layer_fft<<>>(scratch.ptr(), gpuRi, gpuSrc, zf, wpx, paddedZ, S); + + int linBlock = stim::maxThreadsPerBlock(); //compute the optimal block size + int linGrid = S / linBlock + 1; + stim::gpu_mirst1d_increment_z <<>>(zf, gpuRi, wpx, S); + + //free memory + HANDLE_ERROR(cudaFree(gpuRi)); + HANDLE_ERROR(cudaFree(gpuSrc)); + } + + void build_sample(){ + init_scratch(); //initialize the GPU scratch space + //build_layer(1); + + T* zf; + HANDLE_ERROR(cudaMalloc(&zf, sizeof(T) * S)); + HANDLE_ERROR(cudaMemset(zf, 0, sizeof(T) * S)); + + //render each layer of the sample + for(unsigned int l=0; l>>(scratch.ptr(), gpuLambdas, + dFz, + NA[0], NA[1], + S, Zpad); + } + + //crop the image to the sample thickness - keep in mind that sample thickness != optical path length + void crop(){ + + scratch = scratch.crop(Z, S); + } + + //save the scratch field as a binary file + void to_binary(std::string filename){ + + } + + +public: + + //constructor + mirst1d(unsigned int rZ = 100, + unsigned int padding = 0){ + Z = rZ; + pad = padding; + NA[0] = 0; + NA[1] = 0.8; + S = 0; + source_profile = 1; + } + + //add a layer, thickness = microns + void add_layer(material mat, T thickness){ + matlist.push_back(mat); + layers.push_back(thickness); + } + + void add_layer(std::string filename, T thickness){ + add_layer(material(filename), thickness); + } + + //adds a profile spectrum for the light source + void set_source(std::string filename){ + source_profile.load(filename); + } + + //adds a block of wavenumbers (cm^-1) to the simulation parameters + void add_wavenumbers(unsigned int start, unsigned int stop, unsigned int step){ + unsigned int nu = start; + while(nu <= stop){ + lambdas.push_back((T)10000 / nu); + nu += step; + } + S = lambdas.size(); //increment the number of wavelengths (shorthand for later) + } + + T thickness(){ + T t = 0; + for(unsigned int l=0; l get_source(){ + return source_profile; + } + + void save_sample(std::string filename){ + //create a sample and save the magnitude as an image + build_sample(); + fft(CUFFT_INVERSE); + scratch.toImage(filename, stim::complexfield::magnitude); + } + + void save_mirst(std::string filename, bool binary = true){ + //apply the MIRST filter to a sample and save the image + + //build the sample in the Fourier domain + build_sample(); + + //apply the MIRST filter + apply_filter(); + + //apply an inverse FFT to bring the results back into the spatial domain + fft(CUFFT_INVERSE); + + crop(); + + //save the image + if(binary) + to_binary(filename); + else + scratch.toImage(filename, stim::complexfield::magnitude); + } + + + + + std::string str(){ + + stringstream ss; + ss<<"1D MIRST Simulation========================="< +#include + +#include "../math/vector.h" +#include "../math/quaternion.h" +#include "../math/constants.h" +#include "../math/plane.h" +#include "../cuda/callable.h" + +/*Basic conversions used here (assuming a vacuum) + lambda = +*/ + +namespace stim{ + +template +class planewave{ + +protected: + + vec k; //k = tau / lambda + vec< complex > E0; //amplitude + //T phi; + + CUDA_CALLABLE planewave bend(rts::vec kn) const{ + + vec kn_hat = kn.norm(); //normalize the new k + vec k_hat = k.norm(); //normalize the current k + + //std::cout<<"PLANE WAVE BENDING------------------"< new_p; //create a new plane wave + + //if kn is equal to k or -k, handle the degenerate case + T k_dot_kn = k_hat.dot(kn_hat); + + //if k . n < 0, then the bend is a reflection + //flip k_hat + if(k_dot_kn < 0) k_hat = -k_hat; + + //std::cout<<"k dot kn: "< r = k_hat.cross(kn_hat); //compute the rotation vector + + //std::cout<<"r: "< q; + q.CreateRotation(theta, r.norm()); + + //apply the rotation to E0 + vec< complex > E0n = q.toMatrix3() * E0; + + new_p.k = kn_hat * kmag(); + new_p.E0 = E0n; + + return new_p; + } + +public: + + + ///constructor: create a plane wave propagating along z, polarized along x + /*planewave(T lambda = (T)1) + { + k = rts::vec(0, 0, 1) * (TAU/lambda); + E0 = rts::vec(1, 0, 0); + }*/ + ///constructor: create a plane wave propagating along k, polarized along _E0, at frequency _omega + CUDA_CALLABLE planewave(vec kvec = rts::vec(0, 0, rtsTAU), + vec< complex > E = rts::vec(1, 0, 0), T phase = 0) + { + //phi = phase; + + k = kvec; + vec< complex > k_hat = k.norm(); + + if(E.len() == 0) //if the plane wave has an amplitude of 0 + E0 = vec(0); //just return it + else{ + vec< complex > s = (k_hat.cross(E)).norm(); //compute an orthogonal side vector + vec< complex > E_hat = (s.cross(k)).norm(); //compute a normalized E0 direction vector + E0 = E_hat * E_hat.dot(E); //compute the projection of _E0 onto E0_hat + } + + E0 = E0 * exp( complex(0, phase) ); + } + + ///multiplication operator: scale E0 + CUDA_CALLABLE planewave & operator* (const T & rhs) + { + + E0 = E0 * rhs; + return *this; + } + + CUDA_CALLABLE T lambda() const + { + return rtsTAU / k.len(); + } + + CUDA_CALLABLE T kmag() const + { + return k.len(); + } + + CUDA_CALLABLE vec< complex > E(){ + return E0; + } + + CUDA_CALLABLE vec kvec(){ + return k; + } + + /*CUDA_CALLABLE T phase(){ + return phi; + } + + CUDA_CALLABLE void phase(T p){ + phi = p; + }*/ + + CUDA_CALLABLE vec< complex > pos(vec p = vec(0, 0, 0)){ + vec< complex > result; + + T kdp = k.dot(p); + complex x = complex(0, kdp); + complex expx = exp(x); + + result[0] = E0[0] * expx; + result[1] = E0[1] * expx; + result[2] = E0[2] * expx; + + return result; + } + + //scales k based on a transition from material ni to material nt + CUDA_CALLABLE planewave n(T ni, T nt){ + return planewave(k * (nt / ni), E0); + } + + CUDA_CALLABLE planewave refract(rts::vec kn) const + { + return bend(kn); + } + + void scatter(rts::plane P, T nr, planewave &r, planewave &t){ + + int facing = P.face(k); //determine which direction the plane wave is coming in + + //if(facing == 0) //if the wave is tangent to the plane, return an identical wave + // return *this; + //else + if(facing == -1){ //if the wave hits the back of the plane, invert the plane and nr + P = P.flip(); //flip the plane + nr = 1/nr; //invert the refractive index (now nr = n0/n1) + } + + //use Snell's Law to calculate the transmitted angle + T cos_theta_i = k.norm().dot(-P.norm()); //compute the cosine of theta_i + T theta_i = acos(cos_theta_i); //compute theta_i + T sin_theta_t = (1/nr) * sin(theta_i); //compute the sine of theta_t using Snell's law + T theta_t = asin(sin_theta_t); //compute the cosine of theta_t + + bool tir = false; //flag for total internal reflection + if(theta_t != theta_t){ + tir = true; + theta_t = rtsPI / (T)2; + } + + //handle the degenerate case where theta_i is 0 (the plane wave hits head-on) + if(theta_i == 0){ + T rp = (1 - nr) / (1 + nr); //compute the Fresnel coefficients + T tp = 2 / (1 + nr); + vec kr = -k; + vec kt = k * nr; //set the k vectors for theta_i = 0 + vec< complex > Er = E0 * rp; //compute the E vectors + vec< complex > Et = E0 * tp; + T phase_t = P.p().dot(k - kt); //compute the phase offset + T phase_r = P.p().dot(k - kr); + //std::cout<<"Degeneracy: Head-On"<(kr, Er, phase_r); + t = planewave(kt, Et, phase_t); + + //std::cout<<"i + r: "< z_hat = -P.norm(); + vec y_hat = P.parallel(k).norm(); + vec x_hat = y_hat.cross(z_hat).norm(); + + //compute the k vectors for r and t + vec kr, kt; + kr = ( y_hat * sin(theta_i) - z_hat * cos(theta_i) ) * kmag(); + kt = ( y_hat * sin(theta_t) + z_hat * cos(theta_t) ) * kmag() * nr; + + //compute the magnitude of the p- and s-polarized components of the incident E vector + complex Ei_s = E0.dot(x_hat); + //int sgn = (0 < E0.dot(y_hat)) - (E0.dot(y_hat) < 0); + int sgn = E0.dot(y_hat).sgn(); + vec< complex > cx_hat = x_hat; + complex Ei_p = ( E0 - cx_hat * Ei_s ).len() * sgn; + //T Ei_p = ( E0 - x_hat * Ei_s ).len(); + //compute the magnitude of the p- and s-polarized components of the reflected E vector + complex Er_s = Ei_s * rs; + complex Er_p = Ei_p * rp; + //compute the magnitude of the p- and s-polarized components of the transmitted E vector + complex Et_s = Ei_s * ts; + complex Et_p = Ei_p * tp; + + //std::cout<<"E0: "< > Er = vec< complex >(y_hat * cos(theta_i) + z_hat * sin(theta_i)) * Er_p + cx_hat * Er_s; + //compute the transmitted E vector + vec< complex > Et = vec< complex >(y_hat * cos(theta_t) - z_hat * sin(theta_t)) * Et_p + cx_hat * Et_s; + + T phase_t = P.p().dot(k - kt); + T phase_r = P.p().dot(k - kr); + + //std::cout<<"phase r: "<(0, phase_r) ); + //r.phi = phase_r; + + //t = bend(kt); + //t.k = t.k * nr; + + t.k = kt; + t.E0 = Et * exp( complex(0, phase_t) ); + //t.phi = phase_t; + //std::cout<<"i: "< +std::ostream& operator<<(std::ostream& os, rts::planewave p) +{ + os< +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +namespace stim{ + + class cmd_option + { + private: + bool ansi; + + //argument name + std::string name; + + //description of the argument + std::vector desc; + + //argument values + std::vector vals; + + //range or example + std::string range; + + //flag is true when the argument is user-specified + bool flag; + + void parse_val(const std::string &s){ + + vals.clear(); + + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, ' ')) { + vals.push_back(item); + } + } + + void parse_desc(const std::string &s){ + + desc.clear(); + + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, '\n')) { + desc.push_back(item); + } + } + + public: + void set_ansi(bool b){ ansi = b; } + //create an option with a given name, description, and default value + cmd_option(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") + { + name = _name; + parse_desc(_desc); + parse_val(_default); + + //if a default value is provided, set the flag + if(_default != "") + flag = true; + else flag = false; + + range = _range; + + + } + + int nargs() + { + return vals.size(); + } + + //return the value of a text option + std::string as_string(unsigned int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - Option requested without being set: "< n) + return vals[n]; + + else return ""; + } + + //return the value of a floating point option + float as_float(unsigned int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - option requested without being set: "< n) + { + float r; + if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; + return r; + } + + else return 0; + } + + //return the value of an integer option + int as_int(unsigned int n = 0) + { + if(!flag) + { + std::cout<<"ERROR - option requested without being set: "< n) + { + int r; + if ( ! (std::istringstream(vals[n]) >> r) ) r = 0; + return r; + } + + else return 0; + } + + //get the width of the left column + int col_width() + { + int n = 3; + //add the length of the option name + n += name.size(); + + //if there are any default parameters + if(vals.size() > 0) + { + //padding (parenthesis, =, etc.) + n += 6; + + //for each default option value + for(unsigned int v=0; v opts; + std::vector args; + + //column width of the longest option + int col_width; + + //list of sections + std::vector sections; + + public: + + arglist(){ + col_width = 0; + ansi = true; + } + + void set_ansi(bool b) + { + ansi = b; + for(unsigned int i=0; i(col_width, opt.col_width()); + } + + void section(std::string _name) + { + argsection s; + s.name = _name; + s.index = opts.size(); + sections.push_back(s); + } + + //output the options (generally in response to --help) + std::string str() + { + std::stringstream ss; + + int si = -1; + + if(sections.size() > 0) + si = 0; + + //for each option + for(unsigned int a=0; a= opts.size()) + return -1; + + return (int)i; + } + + void set(std::string _name, std::string _value) + { + int i = index(_name); + + if(i != -1) + { + opts[i].set(_value); + //adjust the column width if necessary + col_width = (std::max)(col_width, opts[i].col_width()); + } + else + std::cout<<"ERROR - option not recognized: "<<_name<= opts.size()) + { + std::cout<<"ERROR - Unspecified parameter name: "<<_name< /* defines FILENAME_MAX */ +#ifdef _WIN32 + #include + #define GetCurrentDir _getcwd + #define STIM_FILENAME_DIV '\\' +#else + #include + #define GetCurrentDir getcwd + #define STIM_FILENAME_DIV '/' + #endif + +#include +#include +#include +#include +#include + +#include "../parser/parser.h" + +#include + +namespace stim{ + +//filename class designed to work with both Windows and Unix + +class filename{ + +private: + void init(){ + + absolute = false; + + } + +protected: + + std::string drive; //drive letter (for Windows) + std::vector path; //directory hierarchy + std::string prefix; //file prefix (without extension) + std::string ext; //file extension + + bool absolute; //filename is an absolute path + + //replace any incorrect dividers with the appropriate version for the OS + std::string parse_div(std::string s) { + #ifdef _WIN32 + std::replace( s.begin(), s.end(), '/', '\\'); + #else + std::replace( s.begin(), s.end(), '\\', '/'); + #endif + + return s; + } + + //parse the file name (prefix and extension) + void parse_name(std::string fname){ + + //find the extension + size_t xpos = fname.find_last_of('.'); //find the position of the extension period (if there is one) + if(xpos != std::string::npos){ //split the prefix and extension + prefix = fname.substr(0, xpos); + ext = fname.substr(xpos + 1); + } + else + prefix = fname; + } + + //parse a file locator string + void parse(std::string loc){ + + //determine the drive (if Windows) + drive = ""; + #ifdef _WIN32 + //if the second character is a colon, the character right before it is the drive + if(loc[1] == ':'){ + absolute = true; //this is an absolute path + drive = loc[0]; //copy the drive letter + loc = loc.substr(3); //remove the drive information from the locator string + } + #else + if(loc[0] == STIM_FILENAME_DIV){ + absolute = true; + loc = loc.substr(1); + } + #endif + + //determine the file name + std::string fname = loc.substr( loc.find_last_of(STIM_FILENAME_DIV) + 1 ); //find the file name (including extension) + + //parse the file name + parse_name(fname); + + //find the directory hierarchy + std::string dir = loc.substr(0, loc.find_last_of(STIM_FILENAME_DIV) + 1 ); + path = stim::parser::split(dir, STIM_FILENAME_DIV); + } + +public: + + filename(){ + init(); + } + + filename(std::string loc){ + init(); + parse(loc); + } + + + + std::string get_name(){ + if(prefix == "" && ext == "") + return ""; + else + return prefix + "." + ext; + } + + std::string get_extension(){ + return ext; + } + + std::string get_prefix(){ + return prefix; + } + + std::string dir(){ + std::stringstream ss; + + //if the path is absolute + if(absolute){ + //output the drive letter if in Windows + #ifdef _WIN32 + ss< get_list(){ + + boost::filesystem::path p(dir()); //create a path from the current filename + + std::vector file_list; + + if(boost::filesystem::exists(p)){ + if(boost::filesystem::is_directory(p)){ + + typedef std::vector vec; // store paths, + vec v; // so we can sort them later + + std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(v)); + + std::sort(v.begin(), v.end()); // sort, since directory iteration + // is not ordered on some file systems + + //compare file names to the current template (look for wild cards) + for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it) + { + + //if the filename is a wild card *or* it matches the read file name + if( prefix == "*" || prefix == (*it).filename().stem().string()){ + + //if the extension is a wild card *or* it matches the read file extension + if( ext == "*" || "." + ext == (*it).filename().extension().string()){ + + file_list.push_back((*it).string()); //include it in the final list + } + } + + } + } + } + + return file_list; + } + + //gets the current working directory + static stim::filename cwd(){ + + + char cCurrentPath[FILENAME_MAX]; + + if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))){ + std::cout<<"ERROR getting current working directory."< rel_path = stim::parser::split(rel, STIM_FILENAME_DIV); + + //if the relative path doesn't contain a file, add a blank string to be used as the filename + if(rel[rel.size()-1] == STIM_FILENAME_DIV) + rel_path.push_back(""); + + //create a stack representing the current absolute path + std::stack > s(path); + + //for each token in the relative path + for(int d=0; d result_path(begin, end); + + //create a new path to be returned + stim::filename result = *this; + result.path = result_path; + result.set_name(rel_path.back()); + + return result; + } + + bool is_relative(){ + return !absolute; + } + + bool is_absolute(){ + return absolute; + } + + + + + + + + + +}; + + +} //end namespace stim + +#endif diff --git a/stim/parser/parser.h b/stim/parser/parser.h new file mode 100644 index 0000000..2583fde --- /dev/null +++ b/stim/parser/parser.h @@ -0,0 +1,29 @@ +#ifndef STIM_PARSER_H +#define STIM_PARSER_H + +namespace stim{ + +class parser{ + +public: + + static std::vector &split(const std::string &s, char delim, std::vector &elems) { + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, delim)) { + elems.push_back(item); + } + return elems; + } + + static std::vector split(const std::string &s, char delim) { + std::vector elems; + split(s, delim, elems); + return elems; + } + +}; + +} //end namespace stim + +#endif \ No newline at end of file diff --git a/stim/parser/wildcards.h b/stim/parser/wildcards.h new file mode 100644 index 0000000..ac8c42d --- /dev/null +++ b/stim/parser/wildcards.h @@ -0,0 +1,82 @@ +#ifndef STIM_WILDCARDS_H +#define STIM_WILDCARDS_H + +#include +#include + + +//#include + +namespace stim{ + +class wildcards{ +public: + //generate a sequence of strings where '?' is replaced by integer increments + static std::vector increment(std::string input, + unsigned int start, + unsigned int end, + unsigned int step){ + + size_t a = input.find_first_of('?'); //find the first ? + size_t b = input.find_last_of('?'); //find the last ? + size_t n = b - a + 1; //calculate the number of ? + + std::string str_a = input.substr(0, a); //split the strings along the wildcard + std::string str_b = input.substr(b + 1, std::string::npos); + + //create a vector to hold the output strings + std::vector out; + + //create a stringstream to handle the padding + for (unsigned int i = start; i <= end; i += step){ + + std::stringstream ss; + ss << str_a + << std::setfill('0') + << std::setw(n) + << i + << str_b; + + out.push_back(ss.str()); + } + + return out; + + + } +/* + //returns a list of files that match the specified wildcards in 'd' + static std::vector get_file_list(std::string s){ + + stim::filename f(s); + std::string target_path(f.dir()); + boost::regex filter(f.name()); + + std::vector< std::string > all_matching_files; + + boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end + for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i ) + { + // Skip if not a file + if( !boost::filesystem::is_regular_file( i->status() ) ) continue; + + boost::smatch what; + + // Skip if no match + if( !boost::regex_match( i->path().filename().string(), what, filter ) ) continue; + + // File matches, store it + all_matching_files.push_back( i->path().filename().string() ); + } + + return all_matching_files; + + } +*/ + +}; + + +} //end namespace stim + +#endif diff --git a/stim/ui/progressbar.h b/stim/ui/progressbar.h new file mode 100644 index 0000000..f62f7e0 --- /dev/null +++ b/stim/ui/progressbar.h @@ -0,0 +1,33 @@ +#ifndef RTS_PROGRESSBAR_H +#define RTS_PROGRESSBAR_H + +#include +#include +using namespace std; + +static void rtsProgressBar(int percent) +{ + stringstream bar; + static int x = 0; + string slash[4]; + slash[0] = "\\"; + slash[1] = "-"; + slash[2] = "/"; + slash[3] = "|"; + bar<<"["; + for(int i=0; i<40; i++) + { + if(percent > (float)i/(float)40 *100) + bar << "*"; + else + bar << " "; + } + bar<<"]"; + cout << "\r"; // carriage return back to beginning of line + cout << bar.str() << " " << slash[x] << " " << percent << " %"; // print the bars and percentage + x++; // increment to make the slash appear to rotate + if(x == 4) + x = 0; // reset slash animation +} + +#endif diff --git a/stim/visualization/camera.h b/stim/visualization/camera.h new file mode 100644 index 0000000..d1225a3 --- /dev/null +++ b/stim/visualization/camera.h @@ -0,0 +1,197 @@ +#include "../math/vector.h" +#include "../math/quaternion.h" +#include "../math/matrix.h" + +#include + +#ifndef STIM_CAMERA_H +#define STIM_CAMERA_H + +namespace stim{ + +class camera +{ + vec d; //direction that the camera is pointing + vec p; //position of the camera + vec up; //"up" direction + float focus; //focal length of the camera + float fov; + + //private function makes sure that the up vector is orthogonal to the direction vector and both are normalized + void stabalize() + { + vec side = up.cross(d); + up = d.cross(side); + up = up.norm(); + d = d.norm(); + } + +public: + void setPosition(vec pos) + { + p = pos; + } + void setPosition(float x, float y, float z){setPosition(vec(x, y, z));} + + void setFocalDistance(float distance){focus = distance;} + void setFOV(float field_of_view){fov = field_of_view;} + + void LookAt(vec pos) + { + //find the new direction + d = pos - p; + + //find the distance from the look-at point to the current position + focus = d.len(); + + //stabalize the camera + stabalize(); + } + void LookAt(float px, float py, float pz){LookAt(vec(px, py, pz));} + void LookAt(vec pos, vec new_up){up = new_up; LookAt(pos);} + void LookAt(float px, float py, float pz, float ux, float uy, float uz){LookAt(vec(px, py, pz), vec(ux, uy, uz));} + void LookAtDolly(float lx, float ly, float lz) + { + //find the current focus point + vec f = p + focus*d; + vec T = vec(lx, ly, lz) - f; + p = p + T; + } + + void Dolly(vec direction) + { + p = p+direction; + } + void Dolly(float x, float y, float z){Dolly(vec(x, y, z));} + void Push(float delta) + { + if(delta > focus) + delta = focus; + + focus -= delta; + + Dolly(d*delta); + } + + void Pan(float theta_x, float theta_y, float theta_z) + { + //x rotation is around the up axis + quaternion qx; + qx.CreateRotation(theta_x, up[0], up[1], up[2]); + + //y rotation is around the side axis + vec side = up.cross(d); + quaternion qy; + qy.CreateRotation(theta_y, side[0], side[1], side[2]); + + //z rotation is around the direction vector + quaternion qz; + qz.CreateRotation(theta_z, d[0], d[1], d[2]); + + //combine the rotations in x, y, z order + quaternion final = qz*qy*qx; + + //get the rotation matrix + matrix rot_matrix = final.toMatrix3(); + + //apply the rotation + d = rot_matrix*d; + up = rot_matrix*up; + + //stabalize the camera + stabalize(); + + } + void Pan(float theta_x){Pan(theta_x, 0, 0);} + void Tilt(float theta_y){Pan(0, theta_y, 0);} + void Twist(float theta_z){Pan(0, 0, theta_z);} + + void Zoom(float delta) + { + fov -= delta; + if(fov < 0.5) + fov = 0.5; + if(fov > 180) + fov = 180; + } + + void OrbitFocus(float theta_x, float theta_y) + { + //find the focal point + vec focal_point = p + focus*d; + + //center the coordinate system on the focal point + vec centered = p - (focal_point - vec(0, 0, 0)); + + //create the x rotation (around the up vector) + quaternion qx; + qx.CreateRotation(theta_x, up[0], up[1], up[2]); + centered = vec(0, 0, 0) + qx.toMatrix3()*(centered - vec(0, 0, 0)); + + //get a side vector for theta_y rotation + vec side = up.cross((vec(0, 0, 0) - centered).norm()); + + quaternion qy; + qy.CreateRotation(theta_y, side[0], side[1], side[2]); + centered = vec(0, 0, 0) + qy.toMatrix3()*(centered - vec(0, 0, 0)); + + //perform the rotation on the centered camera position + //centered = final.toMatrix()*centered; + + //re-position the camera + p = centered + (focal_point - vec(0, 0, 0)); + + //make sure we are looking at the focal point + LookAt(focal_point); + + //stabalize the camera + stabalize(); + + } + + void Slide(float u, float v) + { + vec V = up.norm(); + vec U = up.cross(d).norm(); + + p = p + (V * v) + (U * u); + } + + //accessor methods + vec getPosition(){return p;} + vec getUp(){return up;} + vec getDirection(){return d;} + vec getLookAt(){return p + focus*d;} + float getFOV(){return fov;} + + //output the camera settings + const void print(std::ostream& output) + { + output<<"Position: "<(0, 0, 0); + d = vec(0, 0, 1); + up = vec(0, 1, 0); + focus = 1; + + } +}; + +} + + + +#endif diff --git a/stim/visualization/colormap.h b/stim/visualization/colormap.h new file mode 100644 index 0000000..34eef07 --- /dev/null +++ b/stim/visualization/colormap.h @@ -0,0 +1,338 @@ +#ifndef STIM_COLORMAP_H +#define STIM_COLORMAP_H + +#include +#include +#ifdef __CUDACC__ +#include "../cuda/error.h" +#endif + +//saving an image to a file uses the CImg library + //this currently throws a lot of "unreachable" warnings (as of GCC 4.8.2, nvcc 6.5.12) +#include "../image/image.h" + + +#define BREWER_CTRL_PTS 11 + +static float BREWERCP[BREWER_CTRL_PTS*4] = {0.192157f, 0.211765f, 0.584314f, 1.0f, + 0.270588f, 0.458824f, 0.705882f, 1.0f, + 0.454902f, 0.678431f, 0.819608f, 1.0f, + 0.670588f, 0.85098f, 0.913725f, 1.0f, + 0.878431f, 0.952941f, 0.972549f, 1.0f, + 1.0f, 1.0f, 0.74902f, 1.0f, + 0.996078f, 0.878431f, 0.564706f, 1.0f, + 0.992157f, 0.682353f, 0.380392f, 1.0f, + 0.956863f, 0.427451f, 0.262745f, 1.0f, + 0.843137f, 0.188235f, 0.152941f, 1.0f, + 0.647059f, 0.0f, 0.14902f, 1.0f}; + + +#ifdef __CUDACC__ +texture cudaTexBrewer; +static cudaArray* gpuBrewer; +#endif + +namespace stim{ + +enum colormapType {cmBrewer, cmGrayscale, cmRainbow}; + +static void buffer2image(unsigned char* buffer, std::string filename, unsigned int width, unsigned int height) +{ + /*unsigned char* non_interleaved = (unsigned char*)malloc(x_size * y_size * 3); + unsigned int S = x_size * y_size; + + for(unsigned int i = 0; i < S; i++){ + non_interleaved[i + 0 * S] = buffer[i * 3 + 0]; + non_interleaved[i + 1 * S] = buffer[i * 3 + 1]; + non_interleaved[i + 2 * S] = buffer[i * 3 + 2]; + }*/ + + //create an image object + //cimg_library::CImg image(non_interleaved, x_size, y_size, 1, 3); + //image.save(filename.c_str()); + image I; + I.set_interleaved(buffer, width, height, 3); + I.save(filename); +} + +#ifdef __CUDACC__ +static void initBrewer() +{ + //initialize the Brewer colormap + + //allocate CPU space + float4 cpuColorMap[BREWER_CTRL_PTS]; + + //define control rtsPoints + cpuColorMap[0] = make_float4(0.192157f, 0.211765f, 0.584314f, 1.0f); + cpuColorMap[1] = make_float4(0.270588f, 0.458824f, 0.705882f, 1.0f); + cpuColorMap[2] = make_float4(0.454902f, 0.678431f, 0.819608f, 1.0f); + cpuColorMap[3] = make_float4(0.670588f, 0.85098f, 0.913725f, 1.0f); + cpuColorMap[4] = make_float4(0.878431f, 0.952941f, 0.972549f, 1.0f); + cpuColorMap[5] = make_float4(1.0f, 1.0f, 0.74902f, 1.0f); + cpuColorMap[6] = make_float4(0.996078f, 0.878431f, 0.564706f, 1.0f); + cpuColorMap[7] = make_float4(0.992157f, 0.682353f, 0.380392f, 1.0f); + cpuColorMap[8] = make_float4(0.956863f, 0.427451f, 0.262745f, 1.0f); + cpuColorMap[9] = make_float4(0.843137f, 0.188235f, 0.152941f, 1.0f); + cpuColorMap[10] = make_float4(0.647059f, 0.0f, 0.14902f, 1.0f); + + + int width = BREWER_CTRL_PTS; + int height = 0; + + + // allocate array and copy colormap data + cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 32, 32, 32, cudaChannelFormatKindFloat); + + HANDLE_ERROR(cudaMallocArray(&gpuBrewer, &channelDesc, width, height)); + + HANDLE_ERROR(cudaMemcpyToArray(gpuBrewer, 0, 0, cpuColorMap, sizeof(float4)*width, cudaMemcpyHostToDevice)); + + // set texture parameters + cudaTexBrewer.addressMode[0] = cudaAddressModeClamp; + //texBrewer.addressMode[1] = cudaAddressModeClamp; + cudaTexBrewer.filterMode = cudaFilterModeLinear; + cudaTexBrewer.normalized = true; // access with normalized texture coordinates + + // Bind the array to the texture + HANDLE_ERROR(cudaBindTextureToArray( cudaTexBrewer, gpuBrewer, channelDesc)); + +} + +static void destroyBrewer() +{ + HANDLE_ERROR(cudaFreeArray(gpuBrewer)); +} + +template +__global__ static void applyBrewer(T* gpuSource, unsigned char* gpuDest, unsigned int N, T minVal = 0, T maxVal = 1) +{ + + int i = blockIdx.y * gridDim.x * blockDim.x + blockIdx.x * blockDim.x + threadIdx.x; + if(i >= N) return; + + //compute the normalized value on [minVal maxVal] + float a = (gpuSource[i] - minVal) / (maxVal - minVal); + + //compensate for the additional space at the edges + a *= (T)(BREWER_CTRL_PTS - 1)/(T)(BREWER_CTRL_PTS); + + //lookup the color + float shift = (T)1/(2*BREWER_CTRL_PTS); + float4 color = tex1D(cudaTexBrewer, a+shift); + //float4 color = tex1D(cudaTexBrewer, a); + + gpuDest[i * 3 + 0] = 255 * color.x; + gpuDest[i * 3 + 1] = 255 * color.y; + gpuDest[i * 3 + 2] = 255 * color.z; +} + +template +__global__ static void applyGrayscale(T* gpuSource, unsigned char* gpuDest, unsigned int N, T minVal = 0, T maxVal = 1) +{ + int i = blockIdx.y * gridDim.x * blockDim.x + blockIdx.x * blockDim.x + threadIdx.x; + if(i >= N) return; + + //compute the normalized value on [minVal maxVal] + float a = (gpuSource[i] - minVal) / (maxVal - minVal); + + //threshold + if(a > 1) + a = 1; + if(a < 0) + a = 0; + + gpuDest[i * 3 + 0] = 255 * a; + gpuDest[i * 3 + 1] = 255 * a; + gpuDest[i * 3 + 2] = 255 * a; +} + +template +static void gpu2gpu(T* gpuSource, unsigned char* gpuDest, unsigned int nVals, T minVal = 0, T maxVal = 1, colormapType cm = cmGrayscale, int blockDim = 128) +{ + //This function converts a scalar field on the GPU to a color image on the GPU + int gridX = (nVals + blockDim - 1)/blockDim; + int gridY = 1; + if(gridX > 65535) + { + gridY = (gridX + 65535 - 1) / 65535; + gridX = 65535; + } + dim3 dimGrid(gridX, gridY); + //int gridDim = (nVals + blockDim - 1)/blockDim; + if(cm == cmGrayscale) + applyGrayscale<<>>(gpuSource, gpuDest, nVals, minVal, maxVal); + else if(cm == cmBrewer) + { + initBrewer(); + applyBrewer<<>>(gpuSource, gpuDest, nVals, minVal, maxVal); + //HANDLE_ERROR(cudaMemset(gpuDest, 0, sizeof(unsigned char) * nVals * 3)); + destroyBrewer(); + } + +} + +template +static void gpu2cpu(T* gpuSource, unsigned char* cpuDest, unsigned int nVals, T minVal, T maxVal, colormapType cm = cmGrayscale) +{ + //this function converts a scalar field on the GPU to a color image on the CPU + + //first create the color image on the GPU + + //allocate GPU memory for the color image + unsigned char* gpuDest; + HANDLE_ERROR(cudaMalloc( (void**)&gpuDest, sizeof(unsigned char) * nVals * 3 )); + + //HANDLE_ERROR(cudaMemset(gpuSource, 0, sizeof(T) * nVals)); + + //create the image on the gpu + gpu2gpu(gpuSource, gpuDest, nVals, minVal, maxVal, cm); + + //HANDLE_ERROR(cudaMemset(gpuDest, 0, sizeof(unsigned char) * nVals * 3)); + + //copy the image from the GPU to the CPU + HANDLE_ERROR(cudaMemcpy(cpuDest, gpuDest, sizeof(unsigned char) * nVals * 3, cudaMemcpyDeviceToHost)); + + HANDLE_ERROR(cudaFree( gpuDest )); + +} + +template +static void gpu2image(T* gpuSource, std::string fileDest, unsigned int x_size, unsigned int y_size, T valMin, T valMax, colormapType cm = cmGrayscale) +{ + //allocate a color buffer + unsigned char* cpuBuffer = NULL; + cpuBuffer = (unsigned char*) malloc(sizeof(unsigned char) * 3 * x_size * y_size); + + //do the mapping + gpu2cpu(gpuSource, cpuBuffer, x_size * y_size, valMin, valMax, cm); + + //copy the buffer to an image + buffer2image(cpuBuffer, fileDest, x_size, y_size); + + free(cpuBuffer); +} + +#endif + +template +static void cpuApplyBrewer(T* cpuSource, unsigned char* cpuDest, unsigned int N, T minVal = 0, T maxVal = 1) +{ + for(int i=0; i 1) a = 1; + + float c = a * (float)(BREWER_CTRL_PTS-1); + int ptLow = (int)c; + float m = c - (float)ptLow; + //std::cout< +static void cpu2cpu(T* cpuSource, unsigned char* cpuDest, unsigned int nVals, T valMin, T valMax, colormapType cm = cmGrayscale) +{ + + if(cm == cmBrewer) + cpuApplyBrewer(cpuSource, cpuDest, nVals, valMin, valMax); + else if(cm == cmGrayscale) + { + int i; + float a; + float range = valMax - valMin; + for(i = 0; i 1) a = 1; + + cpuDest[i * 3 + 0] = 255 * a; + cpuDest[i * 3 + 1] = 255 * a; + cpuDest[i * 3 + 2] = 255 * a; + } + } +} + +template +static void cpu2cpu(T* cpuSource, unsigned char* cpuDest, unsigned int nVals, colormapType cm = cmGrayscale, bool positive = false) +{ + //computes the max and min range automatically + + //find the largest magnitude value + T maxVal = fabs(cpuSource[0]); + for(int i=0; i maxVal) + maxVal = fabs(cpuSource[i]); + } + + if(positive) + cpu2cpu(cpuSource, cpuDest, nVals, (T)0, maxVal, cm); + else + cpu2cpu(cpuSource, cpuDest, nVals, -maxVal, maxVal, cm); + +} + + + +template +static void cpu2image(T* cpuSource, std::string fileDest, unsigned int x_size, unsigned int y_size, T valMin, T valMax, colormapType cm = cmGrayscale) +{ + //allocate a color buffer + unsigned char* cpuBuffer = (unsigned char*) malloc(sizeof(unsigned char) * 3 * x_size * y_size); + + //do the mapping + cpu2cpu(cpuSource, cpuBuffer, x_size * y_size, valMin, valMax, cm); + + //copy the buffer to an image + buffer2image(cpuBuffer, fileDest, x_size, y_size); + + free(cpuBuffer); + +} + +template +static void cpu2image(T* cpuSource, std::string fileDest, unsigned int x_size, unsigned int y_size, colormapType cm = cmGrayscale, bool positive = false) +{ + //allocate a color buffer + unsigned char* cpuBuffer = (unsigned char*) malloc(sizeof(unsigned char) * 3 * x_size * y_size); + + //do the mapping + cpu2cpu(cpuSource, cpuBuffer, x_size * y_size, cm, positive); + + //copy the buffer to an image + buffer2image(cpuBuffer, fileDest, x_size, y_size); + + free(cpuBuffer); + +} + +} //end namespace colormap and rts + +#endif + diff --git a/ui/progressbar.h b/ui/progressbar.h deleted file mode 100644 index f62f7e0..0000000 --- a/ui/progressbar.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef RTS_PROGRESSBAR_H -#define RTS_PROGRESSBAR_H - -#include -#include -using namespace std; - -static void rtsProgressBar(int percent) -{ - stringstream bar; - static int x = 0; - string slash[4]; - slash[0] = "\\"; - slash[1] = "-"; - slash[2] = "/"; - slash[3] = "|"; - bar<<"["; - for(int i=0; i<40; i++) - { - if(percent > (float)i/(float)40 *100) - bar << "*"; - else - bar << " "; - } - bar<<"]"; - cout << "\r"; // carriage return back to beginning of line - cout << bar.str() << " " << slash[x] << " " << percent << " %"; // print the bars and percentage - x++; // increment to make the slash appear to rotate - if(x == 4) - x = 0; // reset slash animation -} - -#endif diff --git a/visualization/camera.h b/visualization/camera.h deleted file mode 100644 index d1225a3..0000000 --- a/visualization/camera.h +++ /dev/null @@ -1,197 +0,0 @@ -#include "../math/vector.h" -#include "../math/quaternion.h" -#include "../math/matrix.h" - -#include - -#ifndef STIM_CAMERA_H -#define STIM_CAMERA_H - -namespace stim{ - -class camera -{ - vec d; //direction that the camera is pointing - vec p; //position of the camera - vec up; //"up" direction - float focus; //focal length of the camera - float fov; - - //private function makes sure that the up vector is orthogonal to the direction vector and both are normalized - void stabalize() - { - vec side = up.cross(d); - up = d.cross(side); - up = up.norm(); - d = d.norm(); - } - -public: - void setPosition(vec pos) - { - p = pos; - } - void setPosition(float x, float y, float z){setPosition(vec(x, y, z));} - - void setFocalDistance(float distance){focus = distance;} - void setFOV(float field_of_view){fov = field_of_view;} - - void LookAt(vec pos) - { - //find the new direction - d = pos - p; - - //find the distance from the look-at point to the current position - focus = d.len(); - - //stabalize the camera - stabalize(); - } - void LookAt(float px, float py, float pz){LookAt(vec(px, py, pz));} - void LookAt(vec pos, vec new_up){up = new_up; LookAt(pos);} - void LookAt(float px, float py, float pz, float ux, float uy, float uz){LookAt(vec(px, py, pz), vec(ux, uy, uz));} - void LookAtDolly(float lx, float ly, float lz) - { - //find the current focus point - vec f = p + focus*d; - vec T = vec(lx, ly, lz) - f; - p = p + T; - } - - void Dolly(vec direction) - { - p = p+direction; - } - void Dolly(float x, float y, float z){Dolly(vec(x, y, z));} - void Push(float delta) - { - if(delta > focus) - delta = focus; - - focus -= delta; - - Dolly(d*delta); - } - - void Pan(float theta_x, float theta_y, float theta_z) - { - //x rotation is around the up axis - quaternion qx; - qx.CreateRotation(theta_x, up[0], up[1], up[2]); - - //y rotation is around the side axis - vec side = up.cross(d); - quaternion qy; - qy.CreateRotation(theta_y, side[0], side[1], side[2]); - - //z rotation is around the direction vector - quaternion qz; - qz.CreateRotation(theta_z, d[0], d[1], d[2]); - - //combine the rotations in x, y, z order - quaternion final = qz*qy*qx; - - //get the rotation matrix - matrix rot_matrix = final.toMatrix3(); - - //apply the rotation - d = rot_matrix*d; - up = rot_matrix*up; - - //stabalize the camera - stabalize(); - - } - void Pan(float theta_x){Pan(theta_x, 0, 0);} - void Tilt(float theta_y){Pan(0, theta_y, 0);} - void Twist(float theta_z){Pan(0, 0, theta_z);} - - void Zoom(float delta) - { - fov -= delta; - if(fov < 0.5) - fov = 0.5; - if(fov > 180) - fov = 180; - } - - void OrbitFocus(float theta_x, float theta_y) - { - //find the focal point - vec focal_point = p + focus*d; - - //center the coordinate system on the focal point - vec centered = p - (focal_point - vec(0, 0, 0)); - - //create the x rotation (around the up vector) - quaternion qx; - qx.CreateRotation(theta_x, up[0], up[1], up[2]); - centered = vec(0, 0, 0) + qx.toMatrix3()*(centered - vec(0, 0, 0)); - - //get a side vector for theta_y rotation - vec side = up.cross((vec(0, 0, 0) - centered).norm()); - - quaternion qy; - qy.CreateRotation(theta_y, side[0], side[1], side[2]); - centered = vec(0, 0, 0) + qy.toMatrix3()*(centered - vec(0, 0, 0)); - - //perform the rotation on the centered camera position - //centered = final.toMatrix()*centered; - - //re-position the camera - p = centered + (focal_point - vec(0, 0, 0)); - - //make sure we are looking at the focal point - LookAt(focal_point); - - //stabalize the camera - stabalize(); - - } - - void Slide(float u, float v) - { - vec V = up.norm(); - vec U = up.cross(d).norm(); - - p = p + (V * v) + (U * u); - } - - //accessor methods - vec getPosition(){return p;} - vec getUp(){return up;} - vec getDirection(){return d;} - vec getLookAt(){return p + focus*d;} - float getFOV(){return fov;} - - //output the camera settings - const void print(std::ostream& output) - { - output<<"Position: "<(0, 0, 0); - d = vec(0, 0, 1); - up = vec(0, 1, 0); - focus = 1; - - } -}; - -} - - - -#endif diff --git a/visualization/colormap.h b/visualization/colormap.h deleted file mode 100644 index 34eef07..0000000 --- a/visualization/colormap.h +++ /dev/null @@ -1,338 +0,0 @@ -#ifndef STIM_COLORMAP_H -#define STIM_COLORMAP_H - -#include -#include -#ifdef __CUDACC__ -#include "../cuda/error.h" -#endif - -//saving an image to a file uses the CImg library - //this currently throws a lot of "unreachable" warnings (as of GCC 4.8.2, nvcc 6.5.12) -#include "../image/image.h" - - -#define BREWER_CTRL_PTS 11 - -static float BREWERCP[BREWER_CTRL_PTS*4] = {0.192157f, 0.211765f, 0.584314f, 1.0f, - 0.270588f, 0.458824f, 0.705882f, 1.0f, - 0.454902f, 0.678431f, 0.819608f, 1.0f, - 0.670588f, 0.85098f, 0.913725f, 1.0f, - 0.878431f, 0.952941f, 0.972549f, 1.0f, - 1.0f, 1.0f, 0.74902f, 1.0f, - 0.996078f, 0.878431f, 0.564706f, 1.0f, - 0.992157f, 0.682353f, 0.380392f, 1.0f, - 0.956863f, 0.427451f, 0.262745f, 1.0f, - 0.843137f, 0.188235f, 0.152941f, 1.0f, - 0.647059f, 0.0f, 0.14902f, 1.0f}; - - -#ifdef __CUDACC__ -texture cudaTexBrewer; -static cudaArray* gpuBrewer; -#endif - -namespace stim{ - -enum colormapType {cmBrewer, cmGrayscale, cmRainbow}; - -static void buffer2image(unsigned char* buffer, std::string filename, unsigned int width, unsigned int height) -{ - /*unsigned char* non_interleaved = (unsigned char*)malloc(x_size * y_size * 3); - unsigned int S = x_size * y_size; - - for(unsigned int i = 0; i < S; i++){ - non_interleaved[i + 0 * S] = buffer[i * 3 + 0]; - non_interleaved[i + 1 * S] = buffer[i * 3 + 1]; - non_interleaved[i + 2 * S] = buffer[i * 3 + 2]; - }*/ - - //create an image object - //cimg_library::CImg image(non_interleaved, x_size, y_size, 1, 3); - //image.save(filename.c_str()); - image I; - I.set_interleaved(buffer, width, height, 3); - I.save(filename); -} - -#ifdef __CUDACC__ -static void initBrewer() -{ - //initialize the Brewer colormap - - //allocate CPU space - float4 cpuColorMap[BREWER_CTRL_PTS]; - - //define control rtsPoints - cpuColorMap[0] = make_float4(0.192157f, 0.211765f, 0.584314f, 1.0f); - cpuColorMap[1] = make_float4(0.270588f, 0.458824f, 0.705882f, 1.0f); - cpuColorMap[2] = make_float4(0.454902f, 0.678431f, 0.819608f, 1.0f); - cpuColorMap[3] = make_float4(0.670588f, 0.85098f, 0.913725f, 1.0f); - cpuColorMap[4] = make_float4(0.878431f, 0.952941f, 0.972549f, 1.0f); - cpuColorMap[5] = make_float4(1.0f, 1.0f, 0.74902f, 1.0f); - cpuColorMap[6] = make_float4(0.996078f, 0.878431f, 0.564706f, 1.0f); - cpuColorMap[7] = make_float4(0.992157f, 0.682353f, 0.380392f, 1.0f); - cpuColorMap[8] = make_float4(0.956863f, 0.427451f, 0.262745f, 1.0f); - cpuColorMap[9] = make_float4(0.843137f, 0.188235f, 0.152941f, 1.0f); - cpuColorMap[10] = make_float4(0.647059f, 0.0f, 0.14902f, 1.0f); - - - int width = BREWER_CTRL_PTS; - int height = 0; - - - // allocate array and copy colormap data - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 32, 32, 32, cudaChannelFormatKindFloat); - - HANDLE_ERROR(cudaMallocArray(&gpuBrewer, &channelDesc, width, height)); - - HANDLE_ERROR(cudaMemcpyToArray(gpuBrewer, 0, 0, cpuColorMap, sizeof(float4)*width, cudaMemcpyHostToDevice)); - - // set texture parameters - cudaTexBrewer.addressMode[0] = cudaAddressModeClamp; - //texBrewer.addressMode[1] = cudaAddressModeClamp; - cudaTexBrewer.filterMode = cudaFilterModeLinear; - cudaTexBrewer.normalized = true; // access with normalized texture coordinates - - // Bind the array to the texture - HANDLE_ERROR(cudaBindTextureToArray( cudaTexBrewer, gpuBrewer, channelDesc)); - -} - -static void destroyBrewer() -{ - HANDLE_ERROR(cudaFreeArray(gpuBrewer)); -} - -template -__global__ static void applyBrewer(T* gpuSource, unsigned char* gpuDest, unsigned int N, T minVal = 0, T maxVal = 1) -{ - - int i = blockIdx.y * gridDim.x * blockDim.x + blockIdx.x * blockDim.x + threadIdx.x; - if(i >= N) return; - - //compute the normalized value on [minVal maxVal] - float a = (gpuSource[i] - minVal) / (maxVal - minVal); - - //compensate for the additional space at the edges - a *= (T)(BREWER_CTRL_PTS - 1)/(T)(BREWER_CTRL_PTS); - - //lookup the color - float shift = (T)1/(2*BREWER_CTRL_PTS); - float4 color = tex1D(cudaTexBrewer, a+shift); - //float4 color = tex1D(cudaTexBrewer, a); - - gpuDest[i * 3 + 0] = 255 * color.x; - gpuDest[i * 3 + 1] = 255 * color.y; - gpuDest[i * 3 + 2] = 255 * color.z; -} - -template -__global__ static void applyGrayscale(T* gpuSource, unsigned char* gpuDest, unsigned int N, T minVal = 0, T maxVal = 1) -{ - int i = blockIdx.y * gridDim.x * blockDim.x + blockIdx.x * blockDim.x + threadIdx.x; - if(i >= N) return; - - //compute the normalized value on [minVal maxVal] - float a = (gpuSource[i] - minVal) / (maxVal - minVal); - - //threshold - if(a > 1) - a = 1; - if(a < 0) - a = 0; - - gpuDest[i * 3 + 0] = 255 * a; - gpuDest[i * 3 + 1] = 255 * a; - gpuDest[i * 3 + 2] = 255 * a; -} - -template -static void gpu2gpu(T* gpuSource, unsigned char* gpuDest, unsigned int nVals, T minVal = 0, T maxVal = 1, colormapType cm = cmGrayscale, int blockDim = 128) -{ - //This function converts a scalar field on the GPU to a color image on the GPU - int gridX = (nVals + blockDim - 1)/blockDim; - int gridY = 1; - if(gridX > 65535) - { - gridY = (gridX + 65535 - 1) / 65535; - gridX = 65535; - } - dim3 dimGrid(gridX, gridY); - //int gridDim = (nVals + blockDim - 1)/blockDim; - if(cm == cmGrayscale) - applyGrayscale<<>>(gpuSource, gpuDest, nVals, minVal, maxVal); - else if(cm == cmBrewer) - { - initBrewer(); - applyBrewer<<>>(gpuSource, gpuDest, nVals, minVal, maxVal); - //HANDLE_ERROR(cudaMemset(gpuDest, 0, sizeof(unsigned char) * nVals * 3)); - destroyBrewer(); - } - -} - -template -static void gpu2cpu(T* gpuSource, unsigned char* cpuDest, unsigned int nVals, T minVal, T maxVal, colormapType cm = cmGrayscale) -{ - //this function converts a scalar field on the GPU to a color image on the CPU - - //first create the color image on the GPU - - //allocate GPU memory for the color image - unsigned char* gpuDest; - HANDLE_ERROR(cudaMalloc( (void**)&gpuDest, sizeof(unsigned char) * nVals * 3 )); - - //HANDLE_ERROR(cudaMemset(gpuSource, 0, sizeof(T) * nVals)); - - //create the image on the gpu - gpu2gpu(gpuSource, gpuDest, nVals, minVal, maxVal, cm); - - //HANDLE_ERROR(cudaMemset(gpuDest, 0, sizeof(unsigned char) * nVals * 3)); - - //copy the image from the GPU to the CPU - HANDLE_ERROR(cudaMemcpy(cpuDest, gpuDest, sizeof(unsigned char) * nVals * 3, cudaMemcpyDeviceToHost)); - - HANDLE_ERROR(cudaFree( gpuDest )); - -} - -template -static void gpu2image(T* gpuSource, std::string fileDest, unsigned int x_size, unsigned int y_size, T valMin, T valMax, colormapType cm = cmGrayscale) -{ - //allocate a color buffer - unsigned char* cpuBuffer = NULL; - cpuBuffer = (unsigned char*) malloc(sizeof(unsigned char) * 3 * x_size * y_size); - - //do the mapping - gpu2cpu(gpuSource, cpuBuffer, x_size * y_size, valMin, valMax, cm); - - //copy the buffer to an image - buffer2image(cpuBuffer, fileDest, x_size, y_size); - - free(cpuBuffer); -} - -#endif - -template -static void cpuApplyBrewer(T* cpuSource, unsigned char* cpuDest, unsigned int N, T minVal = 0, T maxVal = 1) -{ - for(int i=0; i 1) a = 1; - - float c = a * (float)(BREWER_CTRL_PTS-1); - int ptLow = (int)c; - float m = c - (float)ptLow; - //std::cout< -static void cpu2cpu(T* cpuSource, unsigned char* cpuDest, unsigned int nVals, T valMin, T valMax, colormapType cm = cmGrayscale) -{ - - if(cm == cmBrewer) - cpuApplyBrewer(cpuSource, cpuDest, nVals, valMin, valMax); - else if(cm == cmGrayscale) - { - int i; - float a; - float range = valMax - valMin; - for(i = 0; i 1) a = 1; - - cpuDest[i * 3 + 0] = 255 * a; - cpuDest[i * 3 + 1] = 255 * a; - cpuDest[i * 3 + 2] = 255 * a; - } - } -} - -template -static void cpu2cpu(T* cpuSource, unsigned char* cpuDest, unsigned int nVals, colormapType cm = cmGrayscale, bool positive = false) -{ - //computes the max and min range automatically - - //find the largest magnitude value - T maxVal = fabs(cpuSource[0]); - for(int i=0; i maxVal) - maxVal = fabs(cpuSource[i]); - } - - if(positive) - cpu2cpu(cpuSource, cpuDest, nVals, (T)0, maxVal, cm); - else - cpu2cpu(cpuSource, cpuDest, nVals, -maxVal, maxVal, cm); - -} - - - -template -static void cpu2image(T* cpuSource, std::string fileDest, unsigned int x_size, unsigned int y_size, T valMin, T valMax, colormapType cm = cmGrayscale) -{ - //allocate a color buffer - unsigned char* cpuBuffer = (unsigned char*) malloc(sizeof(unsigned char) * 3 * x_size * y_size); - - //do the mapping - cpu2cpu(cpuSource, cpuBuffer, x_size * y_size, valMin, valMax, cm); - - //copy the buffer to an image - buffer2image(cpuBuffer, fileDest, x_size, y_size); - - free(cpuBuffer); - -} - -template -static void cpu2image(T* cpuSource, std::string fileDest, unsigned int x_size, unsigned int y_size, colormapType cm = cmGrayscale, bool positive = false) -{ - //allocate a color buffer - unsigned char* cpuBuffer = (unsigned char*) malloc(sizeof(unsigned char) * 3 * x_size * y_size); - - //do the mapping - cpu2cpu(cpuSource, cpuBuffer, x_size * y_size, cm, positive); - - //copy the buffer to an image - buffer2image(cpuBuffer, fileDest, x_size, y_size); - - free(cpuBuffer); - -} - -} //end namespace colormap and rts - -#endif - -- libgit2 0.21.4

W W " -" 2W 4W ,W HW 3W :V MW KWU.U 4VAV &V 5U *U 2V 6gGU KU 5W?W =U/V\"U/V IU7V LX ,WNW 5WNW 5WNW 5WNW 5WNW 5WNW 4XHX H[4U&X -X -" -"X -X -X -X -X -X ,X6]&`8X\"Z7Z#Z7Z#Z7Z#Z7Z#Z7Z 'Z8['X/X'X/X'X/X'X/X)Y8Y MX ,W:W 9V 0V 3U@U ?[ 1V 0V 3U@V GV 0V 3U?U 8h 1V 0V 2U@U " -" CV 0V 1U@U >V 7W *`L` I`L` I`L` I`L` I`L` JV =X,X >T 6] 9k\"lKl K_ #\\ 'Y8S MX 2VFV %VBV Nk IVAV=V$X 1V %V +V " -"6YHTHY -V EW 5Y>Y :X ?R5Z .Y ;VMX DX +Y DX IYW W 2W 4W ,W HW 3W :V MW KW;W De =W " -" -X *W:W V$X 1V &W +W 5XITIX +V EV 4X[ JX -XNW8WNX0a9X#Y3Y(X9Y JY3Y(X9Y NX LX W W 2W 4W ,W HW " -" 3W :V MW LX;W Df >W ,W +W8W >WLW @Y 2X +Z3Z!t\"X0X)X?X?X*Y3Y Kj 9V 9j AS 5X 8W:W HV /W #T)T KV " -" @T(T 6U?U &V 5T +V AhGU KU 5V=V =U0V!U0V JV7V WLW 7WLW 7WLW 7WLW 7WLW 7XNX 6XGX IY.R&X -X -X -X -X -X -X -X ,X2Z'a9X#Y3Y%Y3Y%Y3Y%Y3Y%Y3" -"Y )Y3Z)X/X'X/X'X/X'X/X'X:X Ki >W8V *XHZ FW ,ZW W " -" 2W 4W ,W HW 3W :V MW LW:W Dg ?W ,X ,W8W >WLW ?Y 3X +Y1Y\"v#X0X)X?X?X+Y1Y MYNVNY :V :" -"YNVNY BS 5X 8XU1V U1V KW7V NWLW 7WLW 7WLW 7WLW 7WLW 7WLW 6XGX JY,Q&X -X " -"-X -X -X -X -X -X ,X1Z(XNX:X$Y1Y'Y1Y'Y1Y'Y1Y'Y1Y P)P$Y3[)X/X'X/X'X/X'X/X'YVKX DX -X BX IX8X NX7W KP 1P =X Y *Z W 0W MW +ZAZ 0W >W W 2W 4W ,W HW 3W :V MW LW:W DSF[ @X -X " -" -X8W ?WJW ?Y 4X ,Y/Y%z%X0X)X?X?X,Y/Y YMVMY ;V ;YMVMY CS 5X 5P*Q JWU2V NU2V$_7V NXLX 9XLX 9XLX 9XLX 9XLX 8WLW 6XGX KY*P&X -X -X -X -X -X -X -X ,X0Z)XNX:X%Y/Y)Y/Y)Y/Y)Y/Y)Y/Y\"R+R&Y3]*X/X'X/X'X/X'X/X&Y>Y Jp EW:Y " -" +R@Y 7Q 2W .XEVFY\"X5Y\"X5Y\"X5Y\"X5Y NV ;X/X 0V 5T 8c ^ AW4W ?Z >W6W KY " -" \"Y 0X 2VFV &VCW#[LSKZ KV?V@V\"W 0V 'W )W 1XNTNX &V FW 6Y:Y X *Z NW 0W MW ,Z?Z 1W >W W 2W 4W ,W H" -"W 3W :V MW LW:W DPAY ?Y .W -W6W @WJW >Y 5X ,X-X&_MXM_&X0X)X?X?X,Y/Y !YLVLY " -"W FV /X 'TCfFT2i CUGfBT 9U?U &V 7U 5] >iGU KU 6V;V >U2V NU2V$]5V NWJW 9WJW 9WJW 9WJW 9WJW 9WJW 8XFX KY /X -X -X -X -X -X -X -X ,X" -"/Y)XMX;X%Y/Y)Y/Y)Y/Y)Y/Y)Y/Y#T-T'Y3]*X/X'X/X'X/X'X/X%X>X Ir GW=\\ GY 9S 3W /XDVDX$X2X$X2X$X" -"2X$X2X V ;X0X 0X 7T 8d X$X-WJW EX6X Y .X.Y)X -X -Y .X/X'X -X -XBZ EX -XLV:VLX0XMX;X&Y-Y+X7X NY-Y+X7X!X KX Z W FV .X (TDgFT3j CTFhDT 9U?U &V 8U 4\\ =iGU KU 6V" -";V >U3V MU3V#\\5V MWJW 9WJW 9WJW 9WJW 9WJW 9WJW 8XFX LY .X -X -X -X -X -X -X -X ,X.Y*XMX;X&Y-Y+Y-Y+Y-Y+Y-Y+Y-Y%V/V)Y3_+X/X'X/X'X/X'X/X%Y@Y Is HW?^ " -"?Z /Z /Z /Z /Z /Z /Z6Y NZ 0Z /Z /Z /Z 8Y 1Y 3Z /Z /Z /Z /Z 3ZCV 5WDX DXCVCW%X0W%X0W%X0W%X0W V :X1X 0X 7T 9f =k#~`\"h Cf " -"EW4W @\\ ?X8X LX !Y /X 2VFV 'VBV#XHSET KV?VAV!W 0V (W 'W .` \"V GW 5X8X W\"W.XJX" -" FX6X X -X.Y)X -X -X -X/X'X -X -XCZ DX -XLV:VLX0XLX^4WG_ 9` @WG^ 9^GW MWG\\ ;f Gm ^BV\"W:W 3X ?^ 0e AWG_ KV.X ?X Z 7X -X+X)\\HXH\\(X0X)X?X?X-X+X $YJVJY >V >YJVJY Ma =X 7V0V JW@W EV .Y *TEiET5k DTEiDT :VAV &V 9U 3_ ;W6W NiGU " -"KU 6V;V >U3V MU3V#_8V NXJX ;XJX ;XJX ;XJX ;XJX ;XJX :XEX LX -X -X -X -X -X -X -X -X ,X.Y*XLXa'b 7` 5` 5` 5` AW ,W ,W ,W DY EWG_ 9` 5` 5` 5` 5` (Z <`GV W6W MW6W MW6W MW6W#W1X NWG^ HW1X NWBVBW&W.W&WJP:PJW&W4PJW&W." -"W!V :X2X 0X 6S 8g >k#~`#j Fj GW4W @\\ >W8W LX X .X 2VFV 'VBV$XGSCR KV?VBV X 1V (W 'W ,\\ V GW 5X8X f CWIb =bIW MWI^ =j Im U4V LU4V\"`:V GX /WHW ;WHW ;WHW ;WHW ;WHW ;WHW :XEX MY -X -X -X -X -X -X -X -X ,X-Y+XKWf ;f ;f ;f ;f +Z >eJU NW6W MW6W MW6W MW6W\"W" -"2W MWIb IW2W NWAVAW(W,W(WJRU5V KU5V GXTKW)W4TKW)W+W\"V 9X3X 2X 5T :k ?i\"~`$m Jn IW4W A^ ?X:X MW " -" NY .X 2VFV 7~X2XFS VIV>X2YIY DYFY +Z JW .V NW 1Y3Y 1n DWLh Bm ChLW Gk Ll 6hLW MWKg HW ,W ,W;Y JW " -",WKfGg8WKg Cl FWLh ChLW MWK` @m Im Y =W6W JW-W&YJb }!WCWCW Hk Dx&{ W4W CWFW P JSCVAVDS :WEV $V W6W NiGU KU 6V" -";V BP>P /U5V KU5V EW=V FX 0XHX =XHX =XHX =XHX =XHX =XHX W:X MW NX -X 2VFV 7~X2WES WKX0XJX>X(Y)X,X7X!Y)X,X7X!Y LX VIV>X1YKY BXFX +Z IW .W " -" W 2Y1Y 2o EWMj Dn DjMW Hn Nl 7jMW MWLi IW ,W ,WW6W NiGU KU 6V;V BQ?Q 0U6V JU6V BU>V EX 0WFW =WFW =WFW =WFW =WFW =WFW X(Y)X.Y)X.Y)X.Y)X.Y)X%Z9Z*Y6WJX,X/X'X/X'X/X'X/X!XFX EX;Z LWDX ?o Do Do Do Do Do DoKn4n Cn Cn Cn Cn HW ,W ,W ,W %l HWLi En Cn Cn Cn Cn /Z Cs LW6W MW6" -"W MW6W MW6W!W4W LWMj LW4W W?V?V+W(V+WKXBXKV+W5XKV+W(V$W 8W4X 2X 5T ;n ?g!~_%p LZDZ JW4W A^ >W:W MW MX -X 2VFV 7~X2WES VJX0XIW>X(X" -"'X-X7X!X'X-X7X!Y LX VIV>X1YKY AXHX +Z HW -V W 3Y/Y 3p FWMk Fo EkMW Io Nl 8kMW MWMk JW ,W ,W=Y HW ,WMjJj:WMk Gp HWMk GkMW MWMb Bo Im \\>W0X=X LW5X u 6W :V MW EkJV Wj Fn CWMk\"\\6X =Z >W6W KW+W)[Ke\"}!WCWCW Jo Hz&{ W4W DWDW ;Y ;X /X'X.YBXBY+X0X)X?X?X/X'X#T HV IT " -":V ;T3T :V CV +o BX 6ZM`MZ GXFX CV *\\ 3SFW,S:V>V 0R@R KSBV@VDS 9e #V ?W \"V ?W6W NiGU KU 6V;V BR@R 1U6V JU6V BV?V EX 1XFX ?XFX ?XFX ?XFX" -" ?XFX ?XFW =XCX NX +X -X -X -X -X -X -X -X ,X+X,XIW>X(X'X/X'X/X'X/X'X/X'X%Z;Z)X5VHX-X/X'X/X'X/X'X/X XHX DX:Y LWEX >p Ep Ep Ep Ep Ep EpMp6o Do Do Do Do" -" HW ,W ,W ,W 'o IWMk Gp Ep Ep Ep Ep 0Z Ds KW6W MW6W MW6W MW6W!W5X LWMk MW5X V>V?W,V'W,VKZDYKW,V5YKW,V'W%W 8X5W 2X 4T ;o @g ~^%q NY@Y KW4W B`" -" ?XX -XJW@WJX0XIX?X(X'X-X7X!X'X-X8Y Y MX W/YMY @YJY +Y GW -V W 4X+X 4YE\\ FWNXG\\ H]EX F\\GXNW J\\F[ " -"GW ,\\GXNW MWNXG[ JW ,W ,W?Z GW ,WNXH[KXH[:WNXG[ H]H] IWNXG\\ I\\GXNW MWNXFQ C\\CW CW ,W6W!X6X NW?\\?W.X?X JW6W 1X 6W :V MW 9X=X\"[IZKW W=Y /W @m H]DV " -"CWNXG[\"\\6W =[ >W6W LW)W*ZJWKY\"}!WCWCW K\\H] J{&{ V3W DWDW :Y XCX NX +X -X -X -X -X -X -X -X ,X+X,XIX?" -"X(X'X/X'X/X'X/X'X/X'X$Z=Z(X6WHX-X/X'X/X'X/X'X/X YJY DX9Y MWEW =YE\\ EYE\\ EYE\\ EYE\\ EYE\\ EYE\\ EYE]N\\G[7]EX E\\F[ F\\F[ F\\F[ F\\F[ IW ,W ,W ,W (p IWNXG[ H]H" -"] G]H] G]H] G]H] G]H] 1Z E]H^ JW6W MW6W MW6W MW6W W6W KWNXG\\ MW6W NV>V>V,V&V,VJZFYIV,V6YIV,V&V%W 7W6X 3X LR:T ;q @e N~^&s!Y>Y LW4W B` >WXJX +Z GW -W !W 5X)X 5U>Z G_CZ I[>T FZC_ KZAZ HW -ZB_ " -"M^BZ KW ,W ,W@Z FW ,^CZMVCZ;^BZ IZBZ I_CZ IZC_ M^ 5YY .W AXJa IZW2W EWDW 9Y =X /X'X/YAXAY,X0X)X?X?X/X'X%X JV KX Z FU>Z FU>Z FU>Z FU>Z FU>Z FU>eBZ9[>T FZAZ HZAZ HZAZ HZAZ JW ,W ,W ,W )r J^BZ IZBZ GZBZ GZBZ GZBZ" -" GZBZ 1Z EZB[ JW6W MW6W MW6W MW6W W6W K_CZ MW6W V=V>V-V%V-VHZHYHV-V6YHV-V%V%W 7X7X 4X NU:T WX !Y 0Y BVDX Dk CXJc -X BX>X LX5Y MX -X Ee Le 3Z ?U=bKUCU6XDX IX9Y X +X+X+X -X /X +X/X" -"'X -X -XL[ Y J]?Y KY?] M] 4X8P CW ,W6W X8X MW?\\?W-XAX IW7X 3Y 5W :V MW =_C_(YBXLV NW?Z -W CXC\\ KY ,]@Y LW8X >] ?W6W LW)W,YHWHY MW=W JWCWCW MY>Y " -"L[B[ ;W >W2W FWBW 9Y >X 0X%X0X@X@X,X0X)X?X?X/X'X&Y JV KY =V >Y7Y =V CV .[HSFR BX 3t BWHW AV .WN\\ 9SFV)S;V?W 3UCU LSAV@VCS 7_ V BV LU ?W" -"6W MhGU KU 5W?W AUCU 4U8V HU8V ?UAV CX 2XDX AXDX AXDX AXDX AXDX AXDX @XBX NX +X -X -X -X -X -X -X -X ,X+X,XHX@X(X'X/X'X/X'X/X'X/X'X\"ZAZ&X8WFX-X/X'" -"X/X'X/X'X/X MXLX BX8X MWFW Y;Z:R GY=Y JY=Y JY=Y JY=Y KW ,W ,W ,W *]E[ J]@Y JY>Y IY>Y IY>Y IY>Y IY>Y 2Z FY>Y JW6W MW" -"6W MW6W MW6W W7X K]?Y NW7X V=V=U-V$U-VGZJYFU-V7YFU-V$U%W 7X8X &~X/X:T =t @c L~\\'v\"W:W LW4W CXNX ?X>X MV $x EX 2~X2WES :VDW" -"EV FZ :W #W 7XKTKX )V IV 4X4X >X !X 0Y BWDX Dm FXKf /Y AYBY KX5Y MX -X Gd ~X d 5Y ?V>dLUCU6WBW IX;Z Y +X+Y,X -X 0Y +X/X'X -X -XM[ ;X -XIWBWIX" -"0XGW@X)Y'Y.X8X!Y'Y.X9Y M] #X aEa)X@XNW NWA[ ,W DW?[ LX +[=X KW:X =] ?W6W MW'W-XGWGX MW=W JWCWCW MXZ W2W FWBW 9Z ?X" -" 0X%X0X@X@X,X0X(X@X@X/Y'Y(Y IV JY >V ?Y5Y >V CV .YFSDP BX 2q @XJX AV /WK[ :SFV)S;V@X 4VDV LSAV@VCS 6\\ MV CV KU ?W6W MhGU KU 4V?V @V" -"DV 5U9V GU9V >UBV BX 2WBW AWBW AWBW AWBW AWBW AXDX @XBX Y +X -X -X -X -X -X -X -X ,X+Y-XGW@X)Y'Y1Y'Y1Y'Y1Y'Y1Y'Y\"ZCZ&Y9WEY.X/X'X/X'X/X'X/X MYNY BX8Y N" -"WFW X NW $w DX $VBV#XFS :WFXEV H] ;W #W 9XITIX" -" +V JW 4X4X >X \"Y 3[ BWCX Dn GXLi 1X ?ZFZ JY7Z MX -X Je M~X Me 9Y >U?gMUCV7WBW IX>\\ NX *X*X,X -X 0X *X/X'X -X -XNZ 9X -XHVBVHX0XGXAX)X%X.X9Y!X%" -"X.X:Y La 'X _ @W6W MW'W.YGWFX NW=W JWCWCW NX:X NYW2W FWBW 8Z @X 0X%X0X@X@X,X0X(X@X@X" -"/X%X)Y HV IY ?V @Y3Y ?V CV /YES 6X 1\\H[ JcJc LV 0WI\\ =TFV)S;WAX 5WEW MTAVAWCS 3W 4~W.W KV ?W6W LgGU KU 4WAW @WEW 6U9V GU9V ?VBV BX 2" -"WBW AWBW AWBW AWBW AWBW AWBW AXAX X *X -X -X -X -X -X -X -X ,X*X-XGXAX)X%X1X%X1X%X1X%X1X%X!ZEZ%X9WCX.X/X'X/X'X/X'X/X LXNX AX7X NWFW !W ,W ,W ,W ,W ,W " -",]:X=Y .X9X LX9X LX9X LX9X LW ,W ,W ,W +Z=X K[x A` J~\\(y%W8W MW4W CXMW >W>W MV $x DX $VCV\"XFS 9XIXEV H_ X #Y ?g AVBX Do HXM" -"k 3Y >l HX7Z MX -X Me J~X Je =Y >V?hNUBU8XBX Ju MX *X*X,w Lq IX *~R'X -X -c 8X -XHVBVHX0XFWAX)X%X.X9Y!X%X.X;Z Ke ,X WNV MW" -"Ib +W EW;Y MW *Z;X KV:W =_ @W6W NW%W/XFWFX NW=W JWCWCW NW8X!Y:Y =W >| GW@W 8Y @X 0X%X1Y@X@Y-X0X(X@X@X/XImIX*Y GV HY @V AY1Y @V CV /XDS 6X 0YDY JdL" -"d LV 1WF[ >SFV'SW6W LgGU KU 3WCW ?XFX 7U:V FU:V >UBV AX 3XBX CXBX CXBX CXBX CXBX CXBX BXAw?X *w Lw Lw Lw " -"LX -X -X -X ,X*X-XFWAX)X%X1X%X1X%X1X%X1X%X ZGZ$X:WBX.X/X'X/X'X/X'X/X K` @X7X NWFW W ,W ,W ,W ,W ,W ,[8W=X -W7W LW7W LW7W LW7W LW ,W ,W ,W ,Y:X LZ;X M" -"Y:Y MY:Y MY:Y MY:Y MY:Y \"Y=\\ LW6W MW6W MW6W MW6W MW:W IZ9X NW:W NVV&V 4W:X %~X2TNVW \"W ;WFTFW -V JV 3X4X >X #Y ?f AWBX Dp IXNm 4X ` @W6W NW%W/WEWEW NW=W JW" -"CWCW X8X!X8X =W >| GW@W 7Y AX 0X%X1X?X?X-X0X(X@X@X/XImIX+Y FV GY AV BY/Y AV DX 1XCS 6X 0W@X KdLd LV 1VCZ ?SFV'S;WE[ 7XFX G~X .S@VBWAS @~W0W " -".P>W >W6W KfGU KU 3XEX >XFX 8U;V:W3U;VCZ9P>WCV:W/Y 3W@W CW@W CW@W CW@W CW@W CXBX CX@w?X *w Lw Lw Lw LX -X -X -X 5p9X-XFXBX)X%X1X%X1X%X1X%X1X%X N" -"ZIZ#X:VAX.X/X'X/X'X/X'X/X K` @X7X NWFW W ,W ,W ,W ,W ,W ,[8X?X -X7X NX7X NX7X NX7X MW ,W ,W ,W ,X9X LY9W MX8X MX8X MX8X MX8X MX8X \"X=] LW6W MW6W MW6" -"W MW6W MW:W IZ9X NW:W NVLuKU/VLuKU/VBaAU/V:YAU/V=X=U&V 4X;X %~X2RLW>T >{!z'~Z)}(W6W NW4W DXLX ?X@X MV KX ,X %VBV!YHS 8eEV" -" Ic ?W !W ;UETEU ,V KW 3X4X >X $Y >c ?WAX DWD^ JbG] 5X 9d DY9[ MX -X #d D~X Dd DY a AW6W NW%W0XEWEX W=W JWCWCW W6W!X8X =W >| HX@X 7Y BX 0X%X1X?X?X-X0" -"X(X@X@X/XImIX,Y EV FY BV CY-Y BV DX 1XCS 6X 1W>W KeNe LV 1VB[ ASFV'S;YI] 9YGY F~X .S@VDX@S @~W1V ,TEZ >W6W JeGU IX +U 2YIY T ?|\"}(~X)~(W6W NW4W DXKW >W@X MV KX ,X %VBV!ZIS 7cEV IYNZ8W 0W !W :RCTCR +V KW 3X4X >X %Y" -" =b >V@X DS=\\ K`C[ 6Y 8b BX9[ Nd A~X Ad HY W ,X8X8W=X8X X6X MY7X\"X7Y MX 0W )W ,W6W MXXMW AW6W NW%W0XEWDW W=W JWCWCW!X6X#X6X >W >| HW>W 6Y CX 0X%X1X?X?X-X0X'XAXAX.XImIX-Y DV EY CV DY+Y CV DX 2X" -"BS 6X 1Vh =W6W JeGU IX 4g :g :YFX DgEV:XhCV:X/X 3X?W EX?W EX?W EX?W EX?W EX@X EX?w?" -"X *w Lw Lw Lw LX -X -X -X 5p9X-XEXCX)X%X1X%X1X%X1X%X1X%X LZMZ!XX7X NWFY !V +V +V +V +V +V +Y6W@X ,W5W NW5W NW5W NW5W MW ,W ,W" -" ,W -X7X MX8X X6X X6X X6X X6X X6X $X=_ MW6W MW6W MW6W MW6W LWS >}%~R)~V(~P)W6W NW4W" -" DWJX ?XAW L~^ $X ,X %VCV N\\LS 6aDVAW0XLZ9W 0W !W :PATAP +V KV 2X4X >X &Z =e BW@X DP8[ L^?Z 7X :h EY;\\ \"d >~X ?e LY ;U@W>Y" -"AU:W>W Ks KX *X*X,w Lq IX6f+~R'X -X -b 7X -XGWFWGX0XDWCX)X%X.X@^ NX%X.s Bl 8X X IXDVCVDX)[ 4\\ -Z @W *V #W $W JX5W\"X -W5X W4W KW 0W5X MX7W" -" MW ,W ,WIZ =W ,X8X8W=X7W W4W MX5W\"W5X MX 0X *W ,W6W LWX >XMX BW6W W#W1WD" -"WDW W=W JWCWCW!W4W#X6X >W >| HW>W 7Y BX 0X%X1X?X?X-X0X'XAXAX.XImIX.Y CV DY DV EY)Y DV DX 2XBS 6X 2WY BSFV'S9bMV ;XFY D~X .S@h>S " -"@~W2i >g W EW>W EW>W EW>W EW>W EW>W EX?w?X *w Lw Lw Lw LX -X -X -X 5p9X-XDWCX)X%X1X%X1X%X1X%X1X%X " -"Ke X=W?X.X/X'X/X'X/X'X/X I\\ >X7X NWEY \"W ,W ,W ,W ,W ,W ,X5W@X -W4W W4W W4W W4W MW ,W ,W ,W -W6X MX7W W4W W4W W4W W4W W4W $W=VMW MW6W MW6W MW6W MW6W " -"LW=X HX5W NW=X MVLuKU/VLuKU/V?[>U/V=Y>U/V=X=U&V 3X=W 7X FW@T ?~&~T*~V)~R*W5V NW4W EXJX ?XBX L~^ $X ,X &VBV Mb 4]CVC]4XJZ:W" -" 0W !W +T KV KV 2X4X >X 'Z X Lu MX *X*X,w Lq IX6f+~R'X -X -c 8X -XFVFVFX0XDXDX)X%X.u MX%X.r" -" ?l :X X IXDVCVDX)\\ 4Z ,Y ?W *V #W $W JX5W\"W ,W5X W3W LW 0W5X MX7W MW ,W ,WJY ;W ,X8X8W=X7W W4W MX5W\"W5X MX 0X *W ,W6W LWW 6Y 0X 9V LX 5`3R 0T?[?T/W:[ KWId DbKW HW5X NW +X7W JV>W =WLX BW6W W#W1WDWDW W=W JWCWCW!W4W#W4W >W >| IX>X 9Y AX 0X%X1X?X?X-X0X'XAXAX.XImIX/Y B" -"V CY EV FY'Y EV DX 2WAS ?r CV:V =^ =V 2V=Y CSFV'S8`LV e :W6W GbGU IX 4g 8c 5XFX FgFV:YX GX>X GX>" -"X GX>X GX>X GX>X FX?w?X *w Lw Lw Lw LX -X -X -X 5p9X-XDXDX)X%X1X%X1X%X1X%X1X%X Jc NX>W>X.X/X'X/X'X/X'X/X HZ =X7X NWEZ #W ,W ,W ,W ,W ,W ,X4WAW ,W3W!W3" -"W!W3W!W3W NW ,W ,W ,W .X5W MX7W W4W W4W W4W W4W W4W $W>VLW MW6W MW6W MW6W MW6W KW>W GX5W MW>W LVLuKU/VLuKU/V>Z>U/V>Y=U/V=X=U&V 2W>X 8Y FW@T " -" ?~P(~V*~T(~Q)V4V NW4W EXJX >WBX L~^ $X ,X &VBV Ld 4WAVD`6XHZ;W 0W !W +T KV LW 2X4X >X 'Y ;i GV>X *Z M\\;Y 9X =p HZ?^ 'd " -" Id$Y 9UAWX GWEVJVEW#a >W>W 7Y 1Y 8V KY 9e8T 0T?Z>T0X:[ KWIf GdLW HW4W MW ,W6W JV?X >XKW BW6" -"W W#W2XDWDX!W=W JWCWCW!W4W#W4W >W >| IWX GX>w?X *w Lw Lw Lw LX -X -X -X 5p9X-XCWDX)X%X1X%X1X%", -// Start of second string. -"X1X%X1X%X Ia MX?W=X.X/X'X/X'X/X'X/X GX W GX5W MW>W LVLuKU/VLuKU/V?\\?U/V?YX 8X DWBT ?~Q)~W)~R&~(V4V NW4W EWHW >WBW K~^ $X ,X &VBV Kg \"" -"VEc8WFZ=W /W !W +T 4~W 5V 1X4X >X (Y -] IW>X )Y M[9X 9X >\\F\\ H[C` 'a Ca$Y 9UAV:WAU;WW )V $W 6i JX5X$X -X5X V2W LW 1W3W MW6W MW ,W ,WLY 9W ,W7W7W=W6W!X4X NX5X$X5X MW .[ .W ,W6W KW>" -"W FWEVJVEW#a >W?X 8Z 4\\ 8V K[ =iW2W IWX X *X -X -X -X -X -X -X -X ,X*X-XCXEX)X%X1X%X1X%X1X%X1X%X H_ LX@Wi >i >i >i" -" >i >i3WBX ,V2W!V2W!V2W!V2W NW ,W ,W ,W .W4W MW6W!X4X\"X4X\"X4X\"X4X\"X4X M~Y2X@VIW NW6W MW6W MW6W MW6W KW?X GX5X NW?X LVLuKU/VLuKU/V@^@U/V@Y;U/V=X=U&" -"V 2X?W 8X CWBT ?~R*~X)~Q%}(V4W W4W FXHX ?XDX K~^ $X ,X 'WCV Ii &VEe:XEZ>W /W !W +T 4~W 5V 1X4X >X )Y )[ KW=X (Y N[9Y ;Y " -"?Z@Z I]Gb '^ =^$X 9U@V:WAUXIW CW6W!W!W3WCWCW!W=W JWCWCW\"W2W%W3X ?W >W2W JW;X ~R+~Z*~P#{'V4W W4W FXHX ?XDX K~^ $X " -" ,X 'VBV Gi (VFg;WCZ?W /W !W +T 4~W 6W 1X4X >X *Y &Z LW=X (Y NZ7X ;X ?Z>Z ImNX '[ 8\\%Y 9UAW:WAUX XIW CW6W!W!W3WCWCW!W=W JWCWCW\"W2W%W2W ?W >W2W JW:W =Y >X 0Y'" -"X0X?X?X-X0X%XCXCX,X%X2~a GV H~a HV I~b HV DX 3W@S ?r DV8V V&V 1XAW 9" -"X @WDT ?~S+~Z)}!y'W4W W4W FWFW >WDW J~^ *r ?V &VBV Eh *VEXIXX +Y $Z NWXHX DW6W!WW2W KX:X ?Y =X /X'X0Y@X@Y-X0X%YDXDY,X%X2~a GV H~a HV I~b HV DX 3W@S ?r DV8V ;X DW;V DSFV'S >XFX " -" ;V .S@VFW=S (V \"W6W :UGU IX 0XFX -V;TLU MV0U!V;TLU6Y 0X:X KX:X KX:X KX:X KX:X KX:X JWV&V 1XBX :X ?WDT ?~S,~[({ x&W4W W4W FWFX ?XFX JV \"q >V &VBV Af -VEXGX=W@ZB" -"W .W !W +T 4~W 5f 8V 0X4X >X ,Y \"Y W;X 'X NZ7X X -XDVJVDX0XAXGX)X%X.i" -" AX%X.X>Z ,\\ ?X XGW DW6W!WW2W KW9X ?Y =X /X'X/X@X@X,X0X$YEXEY" -"+X%X2~a GV H~a HV I~b HV DX 3W@S 6X 3V8V ;X DXWEW :V .TAVEW?T (V \"W6W :UGU IX /WEW .V;TKU NV/U\"V;TKU7Y /X:X KX:X KX:" -"X KX:X KX:X KX:X KXWDS >~T-~\\(y" -" Mw&W4W W4W GXFX ?XFX JV #r >V 'WCV X -Y Y!W;X 'Y Y5X =X @Y8Y HgKX 'a Ca%X 8UAV8" -"VAU=W8W NX4X%X *X+Y,X -X 0X(X+X/X'X -X -XI[ ?X -XDWLWDX0X@WGX)X&Y.X 0X&Y.X=Y *[ @X XFX EW6W!WW2W KW8W @Y ] Jt It It It It It I~iBW ,|\"|\"|\"| NW ,W ,W ,W /W2W NW6W!W2W\"W2W\"W2W\"W2W\"W2W M~Y2WCVEW NW6W MW6W MW6W MW6W IWCX EW3W L" -"WCX IV=V=V.V$V.VFYKZFV.VFY7V.V$V&V 0XCW ;Y =WFT >~T-~\\'w Ku%W4W W4W GXEW >WFW IV #q =V 6~X JSN^ /VEWCW?W=ZDW .W !W :~W" -" 5f 9V /X4X >X .Y MX\"W:X &X Y5X >Y @X6X FcJX &d Id%X 8UAV8VAU>X8X X4X$X +X+X+X -X /X)X+X/X'X -X -XH[ @X -XCVLVCX0X@XHX(X'X-X /X'X-XXFX EW6W!WV-V%V-VGYIZHV-VGY7V-V%V%V /WDX ;X ~T-~\\'v Is" -"$W4W W4W GWDX ?XGW HV %r =V 6~X JSJ[ 0VEVAV?WX ?X6X D`IX $d Ne#X 8UAV8" -"VBU=x X4X$X +X+X+X -X /X)X+X/X'X -X -XG[ AX -XCVLVCX0X?WHX(X'X-X /X'X-X;Y *Y @X WDW EW6W!WV>V,V&V,VIYGZIV,VIY6V,V&V&W /XEW N~X'VGT =~T-~\\&u Ir#W4W NV4W HXDX ?XHX HV KX ,V 6~X JSHZ 2VDVAV?W;ZGW -W !W \"V " -"Lf :W .X6X =X 0Z LY#~ /X NX5X >X @X5Y AYFX !d >~X >d X 8UAV8VBU>z!X3X%X +X+X+X -X /X)X+X/X'X -X -XF[ BX -XCWNWCX0X?XIX(X'X-X /X'X-X:X )Y AX XDX FW6W!WV?W,V'W,VJYEZKW,VJY6W,V'W&W /XFX N~X'WHT =~T-~\\%s Gp\"W4W NV4V GXCW >WH" -"X HW LX ,V 6~X JSGY 3VDWAW@W:ZIW ,W !W \"V Lf :W .X6X =X 1Z JX#~ /X NX5X ?Y @X4X .X Md A~X Ad LX 8UAV8VBU>z!X3X%X +X+X+" -"X -X /X)X+X/X'X -X -XE[ CX -XBVNVBX0X>WIX(X'X-X /X'X-X9X *Y AX Q.X $T>Z?T0W8W HW5W\"WWCX FW6W!WXFX >V ,SBVBWCS &V \"W6W :U" -"GU *m 8XFX .VWIX(X'X/X'X/X'X/X'X/X'X KZMZ XHW6X-X/X'X/X'X/X'X/X GX XIW GW LX ;~X JSFX 3VDV?V@W9ZJW +V \"W !V V -X6X =X 2Z IX#" -"~ /X NX5X ?X ?X4X .X Jd D~X Dd IX 8UAV8VCV>z!X3X%Y ,X,Y+X -X /Y*X+X/X'X -X -XD[ DX -XBVNVBX0X>XJX(Y)X,X /Y)X,X9Y *X AX XBW FW6W!WXJX" -"(Y)X.Y)X.Y)X.Y)X.Y)X KZKZ!YJW6X,X/X'X/X'X/X'X/X GX |\"X3X$X ,X,X*X -X .X*X+X/X'X -X -XC[ EX" -" -XA\\AX0X=WJX'X)X,X .X)X,X8X *X AX XBX GW6W!WW 9X =\\KW >SEWWJX FW LX <~X JSEX 6WCV?V@W7ZMW *W #W !V !W -X6X =X 4Z GX#~ /X NX5X @X >X4X " -"/X De J~X Je DX 8U@V:WDV>|\"X3X$X ,X-Y*X -X .X*X+X/X'X -X -XB[ FX -XA\\AX0X=XKX'X*Y,X .X*Y,X8Y +X AX W WJW DW MX .VCV" -" :SDW 6VBV?V@W6b )W #W !V !V +X8X X4X /X Ad L~X Ld AX 8VAV:WDU=|\"X3X$Y -X-Y*X -X .Y+X+X/X'X -X -XA[ GX -XA\\AX0XWKVDVKW\"XLX 9WJW =Z #X :V MX AUEVKVDU/X:Y IW5W#WX@W GW6W!W=Y=W2WDWDW W=W JWCWCW\"X4W#W4W >W X4X 0X =d ~X" -" d LUAWX2X#X3X#X -X.Y)X -X -X+X+X/X'X -X -X@[ HX -X@Z@X0XW ,W7W7W=W6W W4W MX5W\"W5X MW BX FW ,W7X FWHW >WLVBVLW#YKX :WJW =Y !W :V MW @VHXJWHV-W:Y IW5W#WY>W1WDWDW W=W JWCWCW\"X4W#W4W >W W MW7X MW7X " -"MW7X MW7X EWJW AX5W GWJW AXCVCW%X0W%X0W%X0W%X0W\"V +WJX ?X 2WLT 9bKQKb)gLQMh Mi =g MW4W MV6W IX@X ?XLX CW MX 0VBV :SDW " -"7VAV?V@X5_ (W #W !V \"W +X8X XLV;VLX1Y?Y >X 9Z 2W %W )W EW7X JX5W\"X -W5X X )W 0X7Y MW6W MW ,W ,WFY ?W ,W7W7W=W6W W4W MX5W\"W5X MW AW FW ,W7X FXJX" -" =WMVBVMW#YJY ;WKX >Y W :V MW ?dId,W;Z IW5W#W=W DW4W!W )W6W DVKW >X>W HW6W W>Y>W1WDWDW W=W JWCWDX\"X4W#W4W >W ;V7W LX2X LY 4X *X1X%]JXJ]'X0X Hj L" -"Y-Y%Y IV JY LYKVKY MY5Y MYJVJY $X 2XBS 6X 2q 9X :V #\\ 7TDgFT /XFX EV )TFV>VJT #V \"W6W :UGU +XFX *V=TCU%V1V!V=TCU=X ,X1W$X1W$X1W" -"$X1W$X1W$X2X%X7X LY .X -X -X -X -X -X -X -X ,X.Y*X;XMX&Y-Y+Y-Y+Y-Y+Y-Y+Y-Y ZAZ$_3Y*X1X%X1X%X1X%X1X FX W3W$W7X MW7X MW7X MW7X MW7X MW7X MW7Z NX -X " -"-X -X -X +W ,W ,W ,W .W4W MW6W W4W W4W W4W W4W W4W 5Z IWMV=W MW7X MW7X MW7X MW7X EWKX AX5W GWKX @XDVDX$X2X$X2X$X2X$X2X\"V +XKW ?X 1WMT 7`JQKa" -"'fLQLf Kg W >WLW BX NY 1VBV :SDW 8V@V?V?W4] &V $W V \"V *Y:Y YGW>X0X$X4Y\"Y /X/Y(X -X ,Y-X+X/X'X -X -X>[ JX -X@Z@X0X;XMX%Y/Y*X ,Y/Y*X6Y -X AX ;Y3Y IXLX =WLV;VLW0X=Y ?X :Z 1W $V )W EW8Y JY7X\"X -X7Y X " -")W 0X7Y MW6W MW ,W ,WEY @W ,W7W7W=W6W X6X MY7X\"X7Y MW AW FW ,X8X EWJW Y NW :V MW >bGc,W;[ JW6X#W=W DX6X!W )W6W DVLX >W=X IW7" -"X W>Y>W1XEWEX W=W IWDWDW!Y6X#X6X >W ;W8W MX0X MY 4X *Y3Y$^LXL^&X0X Ff IY/Y#Y JV KY JYLVLY KY7Y KYKVKY #X 2XBS 6X 3t ;X :V ![ 8TCfFT .XFX FV )U" -"GV>WKT MW7X :UGU ,XFX *V=TBU&V2W!V=TBU=X -X0X&X0X&X0X&X0X&X0X&X0W%X7X KY /X -X -X -X -X -X -X -X ,X/Y)X;XMX%Y/Y)Y/Y)Y/Y)Y/Y)Y/Y Z?Z$" -"^4Y)Y3Y%Y3Y%Y3Y%Y3Y FX XEVFY\"X5Y\"X5Y\"X5Y\"X5Y!V *WLX @X /WNT 7`JQJ_&eKQKe Je :d KW4W MW8W HW>X ?XNX AX Y 1VCV 9SDW 9V?V?V?X4\\ " -" &W %W V \"V )X:X ;X 9Z CX 4X (Y KW7X AX W BW6W W )W6W DWMX ?X=X IX8X W?[?W0WEWEW NW=W IWDWDW!Y6W!W6W =W ;W8W MX0X NY 3X )Y5Y\"z%X0X C` FY/Y\"X JV " -"KX HYMVMY IX7X IYLVLY \"X 1XCS 6X 4v X ?XNX AY Y4P VBV 9SDW 9V?V?V?Y4Z %W %W V #W )X:X ;X :Z CY 4X (Y KX9Y AX ;X6X 1Y 1e /e @U@XB[JXW BX8X W )W6W CVNX >W;W IX8X X@[@X0XFWEW " -"NW=W IWDWEX!Z8X!X8X =W :W:W LX0X Y 2X (Y7Y Nv#X0X ?X AY1Y V IV JV FYNVNY GV5V GYMVMY !X 1XCS 6X 5x =X :V MZ 8T?ZBT *VDV FV 'T&T KX" -"8X :UGU ,VDV )VWNX @Y !Z6Q VBV KP>SEW 9V>WAW>X3Z &W %W V " -" #V 'XU?ZH^MZ\\ JX8X\"W?W AX9Y X *W6W CVNX ?X;X JX9Y NW@[@W/XFWFX NW=W IXEWEX!Z8X!X8W ;W ;W;X MX.X\"Y 1X 'Y9Y Lt\"X0X ?X @Y3Y MT HV IT Dj ET3T EYNVN" -"Y X 0XDS 6X 6ZM`LY >X :V LY 7T)T (UCU ET(T JX9Y :UGU ,UCU )V;m.V3V NV;mCY7P HX.X(X.X(X.X(X.X(X.X(X.X(X6X IY.R&X -X -X -X -" -"X -X -X -X ,X2Z'X9a$Z3Y&Z3Y&Z3Y&Z3Y&Z3Y!Z9Z&Z3Y&Y5Y#Y5Y#Y5Y#Y5Y EX `" -" >Y !Y8S MX +VBV KQ?SFX 9V=VAV=Y6] &V &W NV BX 1X 1V 'Y>Y :X X:W JY;Z NXB]BX.XGWGX MW=W H" -"XFWFX [:X NX:X ;W :WX HXX 9X =Z 1P2Z 3X GQ5Z GX=Y @X 9Y:Y KP8Z GX -X 4^ 1^ +X 5U?gM_9W,W%X7Z L[4U&X6]%X -X )[2X+X/X'X -X -X9[ X -X&X0X8`\"Z7Z'X )Z7Z'X3X%T2Y ?X 9Z9Z E` :" -"_9_3Y7Y BX >Z -W #W +W DX=\\ J\\=Y LY7P HY=\\ LY5R JW -Y?] MW6W MW ,W ,W@Y EW ,W7W7W=W6W MYX LX.X#Y 0X %Y=Z Gl MX0X ?X ?Z7Z JP FV GP @f AP/P Ah MX " -"/YFSDP BX 8ZFVEY @X :V JX 7V.U %SAS CU.U HZ\\=Y B^ 7r Gr Gr Gr Gr KV (_ BX )Y S 8RBSCR <] 2\\ GW4W KZBZ HX;W >_ <[ " -" $[=U MX ,VBV JUCSHY :V;WCW<[Z 0R5Z 2X GT9[ GY?Z AY 9[>[ KR;Z FX -X 1[ 1[ (X 5V>dL^9X,X&X9[ J[7W&X9_$X " -"-X (\\6Z+X/X'X -X -X8[!X -X&X0X8`![;[&X ([;[&X3Y&W7[ ?X 8Z;Z D` :^7^3X5Y CX ?Z ,W #W +W DY?] J]?Y KZ:R GY?] LZ8T JW -ZA^ MW6W MW ,W ,W?Y FW ,W7W7" -"W=W6W LY>Y J]?Y KY?] MW /T9X DX ,Y@] CWNW 9]>]'Y@Y =^ AY IW :V MW HYCXNW L\\>Y VAX >Y>Y LY ,W6W B] >X9X K[>[ MXDVMVDX,YIWIY LW=W GYHWHY N]>Y LY" -">Y :X :X@X LX,X%Y /X $ZAZ Ch KX0X ?X >[;[ ?V 6d >f LX /[HSFR BX 9Z3Y AX :V IX 7V1V #R@R BU0U G[>[ :UGU ,R@R 'V(U)V6W" -" LV(UU IX,X*X,X*X,X*X,X*X,X*X,X*W4X G[7W&X -X -X -X -X -X -X -X ,X9_%X8`![;[![;[![;[![;[![;[\"Z3Z(];[\"Z;Z NZ;Z NZ;Z NZ;Z CX Y JW6W LY>Y IY>Y IY>Y IY>Y IY>Y 2Z FY>Y HY@] KY@] KY@] KY@] B^ >]?Y A^ 6o Do Do Do " -"Do IV (_ CX (Y S (S ,[ 0[ GW4W J\\H\\ GW:W >^ :\\ %[@W MX ,VBV JXFSIZ :V:WEW:\\@e (V 'V MV BX 1X 2V $ZDZ 8X ?Z /U;] 2X GV=" -"\\ EZC[ @X 7[@[ JT?[ EX -X /Y 1Y &X 5V=bK\\7X,X&X<^ I]=Z&X=b#X -X ']:\\+X/X'X -X -X7[\"X -X&X0X7_ \\?\\%X '\\?\\%X2X&Z<\\ >X 7[?[ B^ 9^7^4Y5Y CX ?Y +W \"V +W " -" DZB_ J_CZ I[>T G[C_ K[=W JW ,\\GXNW MW6W MW ,W ,W>Y GW ,W7W7W=W6W KZBZ I_CZ J[C_ MW /W>Z DZ .ZB^ C` 8\\>\\&X>Y =\\ AY HW :V MW GZFYNY N]AZ N" -"WCX _ FX0X ?X =\\?\\ >V 5b W;[>T F[=W J[=W J[=W J[=W LW ,W ,W ,W *ZBZ IW6W KZBZ GZBZ " -"GZBZ GZBZ GZBZ 1Z F[BZ GZB^ KZB^ KZB^ KZB^ A\\ =_CZ ?\\ 3l Al Al Al Al HV (^ BX (X NS (S ,Z .Y FW4W In GX:X ?^ 9_ (]FZ MX " -",VBV J[ISL\\ :V9XGX9^Fi )W )W MV BX 1X 3W #[H[ Et Mx MZC_ 1X GZD^ C[G\\ @Y 7^F] IXF] DX -X ,V 1V #X 4V<^IY5X*X'y G_D^&{!y NX &`B`+X/X'X -X -X6[#" -"w LX&X0X7_ N^E^$X &^E^$X2Y'^C^ =X 7^E^ B^ 8]7]4Y3Y DX @~U&W \"W ,W C\\HYNW JWNXG\\ H]EX F\\GXNW J]D[ JW +kMW MW6W MW ,W ,W=Y HW ,W7W7W=W6W K]H] IWNX" -"G\\ I\\GXNW MW /[E\\ Be 9[GXNW B^ 7\\>\\'XP @W8W 3~W :_GaKP @UGU ,P>P 'V&U+V6V KV&" -"U;]GZ JX*X,X*X,X*X,X*X,X*X,Y,Y,X4y7_D^&y Ny Ny Ny NX -X -X -X ,{\"X7_ N^E^ L^E^ L^E^ L^E^ L^E^ MV/V(dE^ N^E^ L^E^ L^E^ L^E^ BX \\ Av 6W :V MW FkL]$u LXGX 9p Hp EW6W A[ ?X6X LpN\\#" -"hKh)s JW<] Lu LWNm Hp 6` Bl K~W'x MX 1iEi HX CX0X ?X ;u X :V HW 3X=X )X\\ /c 8c 8c 8c 8c CV '\\ ?T %W U *T *W ,V DW4W Gj EW8W " -">\\ 5~P In LX -VBV Is 9V7g6qJZ *V )V LV BX 1X 3V !l Dt Mx Mt /X Gr ?m ?X 4r Hm BX -X &P 1P LX 3V 3X*X'w Cv%x My NX #x(X/X'X" -" -X -X4[%w LX&X0X5] Ls\"X $s\"X1Y(w ;X 5s ?\\ 7\\5\\5Y1Y EX @~U&W !V ,W BjLW JWMj Dn DjMW Hr JW )hLW MW6W MW ,W ,W;Y JW ,W7W7W=W6W In GWMj EjMW MW /p" -" ?d 8iLW B^ 6Z<[)Y:Y >Z @v 6W :V MW EiK]$t JYLZ 7n Fo EW6W A[ ?X5W LWNfM\\\"gKg'q IW<] Ks KWMk Fn 5` Aj J~W'x MX 1iEi HX CX0X ?X :s ;V 2\\ 6" -"^ HX +n Lz MR,R =X :V HW 1ZEZ %ZDZ 0~W :WNfM\\ @UGU !V%U,V6i/V%U9n JX*X,X*X,X*X,X*X,X*X,X*X-X3y5v%y Ny Ny Ny NX -X -X -X ," -"x NX5] Ls Hs Hs Hs Hs IR+R(WMs Js Hs Hs Hs @X R $V NU *U *U *U DW4W Fh DW8X ?\\ 4~ Hl KX -VBV Hp 8V5e4nGZ +W +W LV BX" -" 1X 3V j Ct Mx Mr -X Gq =j >Y 3p Gl AX -X 2X 3W 5X(X(u ?s$v Ky NX \"v'X/X'X -X -X3[&w LX&X0X5] Kq!X #p X0X(v :X 4p =\\ 7\\5\\6Y/Y FX @~U&W !V ,W " -" AhKW JWLh Bm ChLW Gq JW (eJW MW6W MW ,W ,W:Y KW ,W7W7W=W6W Hl FWLh ChLW MW /o >d 7gKW A\\ 5ZZ @v 6W :V MW DgI\\$s He 5l Dn EW6W @Y " -">W4X MWMeM\\!eIe%o HW<] Jq JWLi Dk 2_ @h J~Y(x MX 1iEi HX CX0X ?X 9q :V 1Z 4\\ GX *m Lz LP*P X X ?v 6W :V MW CeG[$r Fc 2h Am EW6W @Y ?X3W MWMdL\\ cGc#m GW;\\ Hm HWKg Ah /] ?f I~Y(x MX 1iEi HX CX0X ?X 7m 8V 0" -"X 2Z FX (j Kz AX :V HW -g Lh ,~W :WMdL\\ @UGU \"V$U-V5i0V$U7i HX(X.X(X.X(X.X(X.X(X.X(X/X2y1o\"y Ny Ny Ny NX -X -X -X ,t" -" JX4\\ Im Bm Bm Bm Bm %VHm Dm Bm Bm Bm =X eJW GeJW" -" GeJW GeJW ?X ;WJe 9X MW &Z =U W ,W *R &Q BW4W B` AW6W >[ /y Dd GX -VCV Af 5V2a.gBZ ,W -W KV CX 0X 4V " -" Kd @t Mx Km *X Ek 6d ;X .h Bh >X .X 1X 1W 7X(X(q 7j Np Ey NX Mm\"X/X'X -X -X1[(w LX&X0X4\\ Gi LX Ni LX/X$n 7X 0i 9Z 5[5[6Y-Y GX @~U&W V -W " -" >cIW JWIb k EW6W @Y ?" -"W2W MWK`I[ NaEa i EW;\\ Fi FWIc >e ,\\ =b G~Y(x MX 1iEi HX CX0X ?X 5i 6V /V 0X EX &f Iz AX :V /P;W *c Gb )~W :WK`I[ @UGU " -" #V#U.V4i1V#U6f FX(X.X(X.X(X.X(X.X(X.X(X/X2y/j Ny Ny Ny Ny NX -X -X -X ,p FX4\\ Gi >i >i >i >i $VEi @i >i >i >i ;X i0g ;i >i >i >i HW ,W ,W ,W #d BW6W Ef ;f ;f ;f ;f JUJe ;cIW FcIW FcIW FcIW ?X ;WIb 7X MW %Y =T X -X )P %P AW4W ?Z" -" >W6X ?Z ,w B` EX .VBV <] 1V0]*b?[ -W -W KV CW /X 4V I` >t Mx Hg 'X Bf 2` :X +d =b ;X .W 0X 1X 9X&X)m 0d Kj ?y NX Jg " -"NX/X'X -X -X0[)w LX&X0X3[ Dc IX Kf LX/Y!g 4X .e 7Z 5Z3Z7Y+Y HX @~U&W V -W =`GW JWG^ 7b 9^GW Ad CW \"YDW MW6W MW ,W ,W7Y NW ,W7W7W=W6W B` @WG^ 9" -"^GW MW (c 2] 3_GW @Z 3X:X*Y4Y @X ?v 6W :V MW ?_AW$WKb @^ +` 9g CW6W ?W ?X2X NWJ^GY K]B^ Ke CW:[ Dd CWG_ 9` 'Y ;^ F~[)x MX 1iEi HX CX0X ?X 2c " -"3V .T .V DX $b Gz AX :V /R>X &[ ?Z %~W :WJ^GY ?UGU #V +V +V 1b EX&X0X&X0X&X0X&X0X&X0Y'X1X1y,d Ky Ny Ny Ny NX -X -X " -"-X ,j @X3[ Dc 8c 8c 8c 8c !VBc ;e :e :e :e 9X Y BS .V,W#Z ;V -V 7W ;W EX ;\\ 6] " -"+Z 5\\ 5Z WGXBU FX=X E` \"W >] @WDY 3Z 2X C[ >T :[ KV /TAY " -" EWGXBU =UGU BT 6V +V +V ,Y ?\\ +[ 0[ 0[ 0[ 0[ KT=[ 2[ 0[ 0[ 0[ 7Z ;Y .Y .Y .Y .Y .Y -" -"Y2\\\"Z /\\ 1\\ 1\\ 1\\ CZ 3Z /Z /Z /Z /Z FVCZ 1Y .Y .Y .Y ,W :WDX 2W LW 7R #S" -" >W /W 8W :V \"W 5X )X &Z CW NV .W :W %W" -" @W :W -X -W :V MW LW FW ?W >W NW 0W =W 3S GV /XGZ " -" DW HUGU AT %T 'R JT " -" #T (X :W NX LW 7S =V /V 7W :V \"W" -" 4X'Q &Y %Z DW NV .W :W %W @W :W -W ,W :V MW " -" LW FW ?W >W NW 0W =W 3S GV /j CW HUGU @T " -" %T 'P HT \"Q 'W 9W NW KW " -" 7S =W 1W 7V :W \"V 2X)R &X #Z " -" EW NW /W :W %W @W :W -W ,X ;V NX LW FW ?W >W NW 0W =W " -" 3S GV /j CW HUGU @U &U " -" U \"P 'W 9W NW KV 6S " -" W NW 0W =W 3S GV /h " -" AW HUGU ?T %T NT " -" )X 9W X KV 6S W NW 0W =W 3S GV .f @W HUGU ?U &" -"U U *W 8W W JV " -" 6S ;V 3V 6V :W \"V .[5[ *Y Z " -" Ha (W :a W NW 0W =W " -" 3S GV +a >W HUGU >T %T " -" NT +X 8W !X (VIV 6S :V 5V 5U" -" 9W \"U +\\;] )X MZ Ia (W :a =Y %W ?W :W " -" /W )[ ?V #[ KW FW ?W >W NW 0W =W 3S GV 'Z ;W " -" HUGU >U &U U ,W 7W !" -"W 'VIV 6S :V 6W 6V 4V *_C` " -" )Y LZ Ja :a (P7Y $W ?W :W 0X (b GV +b JW FW ?W >W " -" NW 0W =W 3S GV 7W HUGU >U &U " -" U -X 7W \"X 'VJW " -" 6S 9V 7V 5U 3U 'x (Z KZ Ka :a " -" (R:Z $W ?W :W 0X (b GV +b JW FW ?W >W NW 0W =W 3S " -" GV 7W #U &U U " -" -X 7W \"X &UJW 6S 9W 9W " -" Bu ([ IZ La :a (T>[ $X ?W :W 1X &a GV +a " -" IW FW ?W >W NW 0W =W 3S GV 7W $V " -" 'V !V .X 6W #X %VLW " -" 5S 2p -a " -" 8XE] %Y >W :W 3Z $_ GV +_ GW FW ?W >W NW 0W =W " -" 3S GV 7W /QGW 2QGW ,QG" -"W 0Z 6W %Z %a 5S " -" 0l +a 8p +_ >W :W ;a !] G" -"V +] EW FW ?W >W NW 0W =W 3S GV 7W /` " -" 1` +` 7a 5W -a #` " -" >e '` " -" 7o *^ =W :W ;` KY GV +Y AW FW ?W >W NW 0W =W " -" 3S GV 7W /` 1` +` " -" 7` 4W -` \"_ " -" 8\\ #_ \"} 3n )^ =W :W ;` 9V " -" BW FW ?W >W NW 0W =W 'V 7W /_ " -" 0_ *_ 6` 4W -` !] " -" -] " -" } 3l '] W NW 0W =W " -" 'V 7W /^ /^ )^ " -" 5_ 3W -_ N[ " -" ,[ M} 2j &\\ ;W :W ;^ 7V BW " -" FW ?W >W NW 0W =W 7W -Y *Y " -" $Y 2^ 2W -^ LX " -" *X J} " -" /d #Z 9W :W ;\\ 5V BW FW ?W >W NW 0W =W " -" 7W " -" /\\ 0W HT " -" I} *[ NW 6W :W ;Z 3V BW FW ?W >W" -" NW 0W =W 7W " -" /Z .W " -" =} " -" " -" D" }; - - // Define a 40x38 'danger' color logo (used by cimg::dialog()). - const unsigned char logo40x38[4576] = { - 177,200,200,200,3,123,123,0,36,200,200,200,1,123,123,0,2,255,255,0,1,189,189,189,1,0,0,0,34,200,200,200, - 1,123,123,0,4,255,255,0,1,189,189,189,1,0,0,0,1,123,123,123,32,200,200,200,1,123,123,0,5,255,255,0,1,0,0, - 0,2,123,123,123,30,200,200,200,1,123,123,0,6,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,29,200,200,200, - 1,123,123,0,7,255,255,0,1,0,0,0,2,123,123,123,28,200,200,200,1,123,123,0,8,255,255,0,1,189,189,189,1,0,0,0, - 2,123,123,123,27,200,200,200,1,123,123,0,9,255,255,0,1,0,0,0,2,123,123,123,26,200,200,200,1,123,123,0,10,255, - 255,0,1,189,189,189,1,0,0,0,2,123,123,123,25,200,200,200,1,123,123,0,3,255,255,0,1,189,189,189,3,0,0,0,1,189, - 189,189,3,255,255,0,1,0,0,0,2,123,123,123,24,200,200,200,1,123,123,0,4,255,255,0,5,0,0,0,3,255,255,0,1,189, - 189,189,1,0,0,0,2,123,123,123,23,200,200,200,1,123,123,0,4,255,255,0,5,0,0,0,4,255,255,0,1,0,0,0,2,123,123,123, - 22,200,200,200,1,123,123,0,5,255,255,0,5,0,0,0,4,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,21,200,200,200, - 1,123,123,0,5,255,255,0,5,0,0,0,5,255,255,0,1,0,0,0,2,123,123,123,20,200,200,200,1,123,123,0,6,255,255,0,5,0,0, - 0,5,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,19,200,200,200,1,123,123,0,6,255,255,0,1,123,123,0,3,0,0,0,1, - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 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, - 200,200,200,91,49,124,118,124,71,32,124,95,49,56,114,52,82,121,0}; - - //! Get/set default output stream for the \CImg library messages. - /** - \param file Desired output stream. Set to \c 0 to get the currently used output stream only. - \return Currently used output stream. - **/ - inline std::FILE* output(std::FILE *file) { - cimg::mutex(1); - static std::FILE *res = stderr; - if (file) res = file; - cimg::mutex(1,0); - return res; - } - - // Return number of available CPU cores. - inline unsigned int nb_cpus() { - unsigned int res = 1; -#if cimg_OS==2 - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - res = (unsigned int)sysinfo.dwNumberOfProcessors; -#else - res = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN); -#endif - return res?res:1U; - } - - // Lock/unlock mutex for CImg multi-thread programming. - inline int mutex(const unsigned int n, const int lock_mode) { - switch (lock_mode) { - case 0 : cimg::Mutex_attr().unlock(n); return 0; - case 1 : cimg::Mutex_attr().lock(n); return 0; - default : return cimg::Mutex_attr().trylock(n); - } - } - - //! Display a warning message on the default output stream. - /** - \param format C-string containing the format of the message, as with std::printf(). - \note If configuration macro \c cimg_strict_warnings is set, this function throws a - \c CImgWarningException instead. - \warning As the first argument is a format string, it is highly recommended to write - \code - cimg::warn("%s",warning_message); - \endcode - instead of - \code - cimg::warn(warning_message); - \endcode - if \c warning_message can be arbitrary, to prevent nasty memory access. - **/ - inline void warn(const char *const format, ...) { - if (cimg::exception_mode()>=1) { - char message[16384] = { 0 }; - std::va_list ap; - va_start(ap,format); - cimg_vsnprintf(message,sizeof(message),format,ap); - va_end(ap); -#ifdef cimg_strict_warnings - throw CImgWarningException(message); -#else - std::fprintf(cimg::output(),"\n%s[CImg] *** Warning ***%s%s",cimg::t_red,cimg::t_normal,message); -#endif - } - } - - // Execute an external system command. - /** - \param command C-string containing the command line to execute. - \param module_name Module name. - \return Status value of the executed command, whose meaning is OS-dependent. - \note This function is similar to std::system() - but it does not open an extra console windows - on Windows-based systems. - **/ - inline int system(const char *const command, const char *const module_name=0) { - cimg::unused(module_name); -#ifdef cimg_no_system_calls - return -1; -#else -#if cimg_OS==1 - const unsigned int l = std::strlen(command); - if (l) { - char *const ncommand = new char[l+16]; - std::strncpy(ncommand,command,l); - std::strcpy(ncommand+l," 2> /dev/null"); // Make command silent. - const int out_val = std::system(ncommand); - delete[] ncommand; - return out_val; - } else return -1; -#elif cimg_OS==2 - PROCESS_INFORMATION pi; - STARTUPINFO si; - std::memset(&pi,0,sizeof(PROCESS_INFORMATION)); - std::memset(&si,0,sizeof(STARTUPINFO)); - GetStartupInfo(&si); - si.cb = sizeof(si); - si.wShowWindow = SW_HIDE; - si.dwFlags |= SW_HIDE | STARTF_USESHOWWINDOW; - const BOOL res = CreateProcess((LPCTSTR)module_name,(LPTSTR)command,0,0,FALSE,0,0,0,&si,&pi); - if (res) { - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hThread); - CloseHandle(pi.hProcess); - return 0; - } else return std::system(command); -#endif -#endif - } - - //! Return a reference to a temporary variable of type T. - template - inline T& temporary(const T&) { - static T temp; - return temp; - } - - //! Exchange values of variables \c a and \c b. - template - inline void swap(T& a, T& b) { T t = a; a = b; b = t; } - - //! Exchange values of variables (\c a1,\c a2) and (\c b1,\c b2). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2) { - cimg::swap(a1,b1); cimg::swap(a2,b2); - } - - //! Exchange values of variables (\c a1,\c a2,\c a3) and (\c b1,\c b2,\c b3). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3) { - cimg::swap(a1,b1,a2,b2); cimg::swap(a3,b3); - } - - //! Exchange values of variables (\c a1,\c a2,...,\c a4) and (\c b1,\c b2,...,\c b4). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4) { - cimg::swap(a1,b1,a2,b2,a3,b3); cimg::swap(a4,b4); - } - - //! Exchange values of variables (\c a1,\c a2,...,\c a5) and (\c b1,\c b2,...,\c b5). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5) { - cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4); cimg::swap(a5,b5); - } - - //! Exchange values of variables (\c a1,\c a2,...,\c a6) and (\c b1,\c b2,...,\c b6). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6) { - cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5); cimg::swap(a6,b6); - } - - //! Exchange values of variables (\c a1,\c a2,...,\c a7) and (\c b1,\c b2,...,\c b7). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6, - T7& a7, T7& b7) { - cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6); cimg::swap(a7,b7); - } - - //! Exchange values of variables (\c a1,\c a2,...,\c a8) and (\c b1,\c b2,...,\c b8). - template - inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6, - T7& a7, T7& b7, T8& a8, T8& b8) { - cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6,a7,b7); cimg::swap(a8,b8); - } - - //! Return the endianness of the current architecture. - /** - \return \c false for Little Endian or \c true for Big Endian. - **/ - inline bool endianness() { - const int x = 1; - return ((unsigned char*)&x)[0]?false:true; - } - - //! Reverse endianness of all elements in a memory buffer. - /** - \param[in,out] buffer Memory buffer whose endianness must be reversed. - \param size Number of buffer elements to reverse. - **/ - template - inline void invert_endianness(T* const buffer, const unsigned long size) { - if (size) switch (sizeof(T)) { - case 1 : break; - case 2 : { for (unsigned short *ptr = (unsigned short*)buffer+size; ptr>(unsigned short*)buffer; ) { - const unsigned short val = *(--ptr); - *ptr = (unsigned short)((val>>8)|((val<<8))); - } - } break; - case 4 : { for (unsigned int *ptr = (unsigned int*)buffer+size; ptr>(unsigned int*)buffer; ) { - const unsigned int val = *(--ptr); - *ptr = (val>>24)|((val>>8)&0xff00)|((val<<8)&0xff0000)|(val<<24); - } - } break; - default : { for (T* ptr = buffer+size; ptr>buffer; ) { - unsigned char *pb = (unsigned char*)(--ptr), *pe = pb + sizeof(T); - for (int i = 0; i<(int)sizeof(T)/2; ++i) swap(*(pb++),*(--pe)); - } - } - } - } - - //! Reverse endianness of a single variable. - /** - \param[in,out] a Variable to reverse. - \return Reference to reversed variable. - **/ - template - inline T& invert_endianness(T& a) { - invert_endianness(&a,1); - return a; - } - - // Conversion functions to get more precision when trying to store unsigned ints values as floats. - inline unsigned int float2uint(const float f) { - int tmp = 0; - std::memcpy(&tmp,&f,sizeof(float)); - if (tmp>=0) return (unsigned int)f; - unsigned int u; - // use memcpy instead of assignment to avoid undesired optimizations by C++-compiler. - std::memcpy(&u,&f,sizeof(float)); - return ((u)<<1)>>1; // set sign bit to 0. - } - - inline float uint2float(const unsigned int u) { - if (u<(1U<<19)) return (float)u; // Consider safe storage of unsigned int as floats until 19bits (i.e 524287). - float f; - const unsigned int v = u|(1U<<(8*sizeof(unsigned int)-1)); // set sign bit to 1. - // use memcpy instead of simple assignment to avoid undesired optimizations by C++-compiler. - std::memcpy(&f,&v,sizeof(float)); - return f; - } - - //! Return the value of a system timer, with a millisecond precision. - /** - \note The timer does not necessarily starts from \c 0. - **/ - inline unsigned long time() { -#if cimg_OS==1 - struct timeval st_time; - gettimeofday(&st_time,0); - return (unsigned long)(st_time.tv_usec/1000 + st_time.tv_sec*1000); -#elif cimg_OS==2 - SYSTEMTIME st_time; - GetSystemTime(&st_time); - return (unsigned long)(st_time.wMilliseconds + 1000*(st_time.wSecond + 60*(st_time.wMinute + 60*st_time.wHour))); -#else - return 0; -#endif - } - - // Implement a tic/toc mechanism to display elapsed time of algorithms. - inline unsigned long tictoc(const bool is_tic); - - //! Start tic/toc timer for time measurement between code instructions. - /** - \return Current value of the timer (same value as time()). - **/ - inline unsigned long tic() { - return cimg::tictoc(true); - } - - //! End tic/toc timer and displays elapsed time from last call to tic(). - /** - \return Time elapsed (in ms) since last call to tic(). - **/ - inline unsigned long toc() { - return cimg::tictoc(false); - } - - //! Sleep for a given numbers of milliseconds. - /** - \param milliseconds Number of milliseconds to wait for. - \note This function frees the CPU ressources during the sleeping time. - It can be used to temporize your program properly, without wasting CPU time. - **/ - inline void sleep(const unsigned int milliseconds) { -#if cimg_OS==1 - struct timespec tv; - tv.tv_sec = milliseconds/1000; - tv.tv_nsec = (milliseconds%1000)*1000000; - nanosleep(&tv,0); -#elif cimg_OS==2 - Sleep(milliseconds); -#endif - } - - inline unsigned int _wait(const unsigned int milliseconds, unsigned long& timer) { - if (!timer) timer = cimg::time(); - const unsigned long current_time = cimg::time(); - if (current_time>=timer+milliseconds) { timer = current_time; return 0; } - const unsigned long time_diff = timer + milliseconds - current_time; - timer = current_time + time_diff; - cimg::sleep(time_diff); - return (unsigned int)time_diff; - } - - //! Wait for a given number of milliseconds since the last call to wait(). - /** - \param milliseconds Number of milliseconds to wait for. - \return Number of milliseconds elapsed since the last call to wait(). - \note Same as sleep() with a waiting time computed with regard to the last call - of wait(). It may be used to temporize your program properly, without wasting CPU time. - **/ - inline unsigned int wait(const unsigned int milliseconds) { - cimg::mutex(3); - static unsigned long timer = 0; - if (!timer) timer = cimg::time(); - cimg::mutex(3,0); - return _wait(milliseconds,timer); - } - - // Random number generators. - // CImg may use its own Random Number Generator (RNG) if configuration macro 'cimg_use_rng' is set. - // Use it for instance when you have to deal with concurrent threads trying to call std::srand() - // at the same time! -#ifdef cimg_use_rng - -#include - - // Use a custom RNG. - inline unsigned int _rand(const unsigned int seed=0, const bool set_seed=false) { - static unsigned long next = 0xB16B00B5; - cimg::mutex(4); - if (set_seed) next = (unsigned long)seed; - next = next*1103515245 + 12345U; - cimg::mutex(4,0); - return (unsigned int)(next&0xFFFFFFU); - } - - inline void srand() { - const unsigned int t = (unsigned int)cimg::time(); -#if cimg_OS==1 - cimg::_rand(t+(unsigned int)getpid(),true); -#elif cimg_OS==2 - cimg::_rand(t+(unsigned int)_getpid(),true); -#else - cimg::_rand(t,true); -#endif - } - - inline void srand(const unsigned int seed) { - _rand(seed,true); - } - - inline double rand() { - return cimg::_rand()/16777215.; - } - -#else - - // Use the system RNG. - inline void srand() { - const unsigned int t = (unsigned int)cimg::time(); -#if cimg_OS==1 - std::srand(t+(unsigned int)getpid()); -#elif cimg_OS==2 - std::srand(t+(unsigned int)_getpid()); -#else - std::srand(t); -#endif - } - - inline void srand(const unsigned int seed) { - std::srand(seed); - } - - //! Return a random variable between [0,1] with respect to an uniform distribution. - /** - **/ - inline double rand() { - return (double)std::rand()/RAND_MAX; - } -#endif - - //! Return a random variable between [-1,1] with respect to an uniform distribution. - /** - **/ - inline double crand() { - return 1-2*cimg::rand(); - } - - //! Return a random variable following a gaussian distribution and a standard deviation of 1. - /** - **/ - inline double grand() { - double x1, w; - do { - const double x2 = 2*cimg::rand() - 1.0; - x1 = 2*cimg::rand()-1.0; - w = x1*x1 + x2*x2; - } while (w<=0 || w>=1.0); - return x1*std::sqrt((-2*std::log(w))/w); - } - - //! Return a random variable following a Poisson distribution of parameter z. - /** - **/ - inline unsigned int prand(const double z) { - if (z<=1.0e-10) return 0; - if (z>100) return (unsigned int)((std::sqrt(z) * cimg::grand()) + z); - unsigned int k = 0; - const double y = std::exp(-z); - for (double s = 1.0; s>=y; ++k) s*=cimg::rand(); - return k-1; - } - - //! Bitwise-rotate value on the left. - template - inline T rol(const T a, const unsigned int n=1) { - return n?(T)((a<>((sizeof(T)<<3)-n))):a; - } - - inline float rol(const float a, const unsigned int n=1) { - return (float)rol((int)a,n); - } - - inline double rol(const double a, const unsigned int n=1) { - return (double)rol((long)a,n); - } - - //! Bitwise-rotate value on the right. - template - inline T ror(const T a, const unsigned int n=1) { - return n?(T)((a>>n)|(a<<((sizeof(T)<<3)-n))):a; - } - - inline float ror(const float a, const unsigned int n=1) { - return (float)ror((int)a,n); - } - - inline double ror(const double a, const unsigned int n=1) { - return (double)ror((long)a,n); - } - - //! Return absolute value of a value. - template - inline T abs(const T a) { - return a>=0?a:-a; - } - inline bool abs(const bool a) { - return a; - } - inline unsigned char abs(const unsigned char a) { - return a; - } - inline unsigned short abs(const unsigned short a) { - return a; - } - inline unsigned int abs(const unsigned int a) { - return a; - } - inline unsigned long abs(const unsigned long a) { - return a; - } - inline double abs(const double a) { - return std::fabs(a); - } - inline float abs(const float a) { - return (float)std::fabs((double)a); - } - inline int abs(const int a) { - return std::abs(a); - } - - //! Return square of a value. - template - inline T sqr(const T val) { - return val*val; - } - - //! Return 1 + log_10(x) of a value \c x. - inline int xln(const int x) { - return x>0?(int)(1+std::log10((double)x)):1; - } - - //! Return the minimum between two values. - template - inline typename cimg::superset::type min(const t1& a, const t2& b) { - typedef typename cimg::superset::type t1t2; - return (t1t2)(a<=b?a:b); - } - - //! Return the minimum between three values. - template - inline typename cimg::superset2::type min(const t1& a, const t2& b, const t3& c) { - typedef typename cimg::superset2::type t1t2t3; - return (t1t2t3)cimg::min(cimg::min(a,b),c); - } - - //! Return the minimum between four values. - template - inline typename cimg::superset3::type min(const t1& a, const t2& b, const t3& c, const t4& d) { - typedef typename cimg::superset3::type t1t2t3t4; - return (t1t2t3t4)cimg::min(cimg::min(a,b,c),d); - } - - //! Return the maximum between two values. - template - inline typename cimg::superset::type max(const t1& a, const t2& b) { - typedef typename cimg::superset::type t1t2; - return (t1t2)(a>=b?a:b); - } - - //! Return the maximum between three values. - template - inline typename cimg::superset2::type max(const t1& a, const t2& b, const t3& c) { - typedef typename cimg::superset2::type t1t2t3; - return (t1t2t3)cimg::max(cimg::max(a,b),c); - } - - //! Return the maximum between four values. - template - inline typename cimg::superset3::type max(const t1& a, const t2& b, const t3& c, const t4& d) { - typedef typename cimg::superset3::type t1t2t3t4; - return (t1t2t3t4)cimg::max(cimg::max(a,b,c),d); - } - - //! Return the sign of a value. - template - inline T sign(const T x) { - return (x<0)?(T)(-1):(x==0?(T)0:(T)1); - } - - //! Return the nearest power of 2 higher than given value. - template - inline unsigned long nearest_pow2(const T x) { - unsigned long i = 1; - while (x>i) i<<=1; - return i; - } - - //! Return the sinc of a given value. - inline double sinc(const double x) { - return x?std::sin(x)/x:1; - } - - //! Return the modulo of a value. - /** - \param x Input value. - \param m Modulo value. - \note This modulo function accepts negative and floating-points modulo numbers, as well as variables of any type. - **/ - template - inline T mod(const T& x, const T& m) { - const double dx = (double)x, dm = (double)m; - return (T)(dx - dm * std::floor(dx / dm)); - } - inline int mod(const bool x, const bool m) { - return m?(x?1:0):0; - } - inline int mod(const char x, const char m) { - return x>=0?x%m:(x%m?m+x%m:0); - } - inline int mod(const short x, const short m) { - return x>=0?x%m:(x%m?m+x%m:0); - } - inline int mod(const int x, const int m) { - return x>=0?x%m:(x%m?m+x%m:0); - } - inline int mod(const long x, const long m) { - return x>=0?x%m:(x%m?m+x%m:0); - } - inline int mod(const unsigned char x, const unsigned char m) { - return x%m; - } - inline int mod(const unsigned short x, const unsigned short m) { - return x%m; - } - inline int mod(const unsigned int x, const unsigned int m) { - return x%m; - } - inline int mod(const unsigned long x, const unsigned long m) { - return x%m; - } - - //! Return the min-mod of two values. - /** - \note minmod(\p a,\p b) is defined to be: - - minmod(\p a,\p b) = min(\p a,\p b), if \p a and \p b have the same sign. - - minmod(\p a,\p b) = 0, if \p a and \p b have different signs. - **/ - template - inline T minmod(const T a, const T b) { - return a*b<=0?0:(a>0?(a - inline T round(const T x, const double y=1, const int rounding_type=0) { - if (y<=0) return x; - const double sx = (double)x/y, floor = std::floor(sx), delta = sx - floor; - return (T)(y*(rounding_type<0?floor:rounding_type>0?std::ceil(sx):delta<0.5?floor:std::ceil(sx))); - } - - inline double _pythagore(double a, double b) { - const double absa = cimg::abs(a), absb = cimg::abs(b); - if (absa>absb) { const double tmp = absb/absa; return absa*std::sqrt(1.0+tmp*tmp); } - else { const double tmp = absa/absb; return absb==0?0:absb*std::sqrt(1.0+tmp*tmp); } - } - - inline bool _is_self_expr(const char *expression) { - if (!expression || *expression=='>' || *expression=='<') return false; - for (const char *s = expression; *s; ++s) - if ((*s=='i' || *s=='j') && (s[1]=='(' || s[1]=='[')) return true; - return false; - } - - //! Convert ascii character to lower case. - inline char uncase(const char x) { - return (char)((x<'A'||x>'Z')?x:x-'A'+'a'); - } - - //! Convert C-string to lower case. - inline void uncase(char *const str) { - if (str) for (char *ptr = str; *ptr; ++ptr) *ptr = uncase(*ptr); - } - - //! Read value in a C-string. - /** - \param str C-string containing the float value to read. - \return Read value. - \note Same as std::atof() extended to manage the retrieval of fractions from C-strings, - as in "1/2". - **/ - inline double atof(const char *const str) { - double x = 0, y = 1; - if (!str) return 0; else { std::sscanf(str,"%lf/%lf",&x,&y); return x/y; } - } - - //! Compare the first \p l characters of two C-strings, ignoring the case. - /** - \param str1 C-string. - \param str2 C-string. - \param l Number of characters to compare. - \return \c 0 if the two strings are equal, something else otherwise. - \note This function has to be defined since it is not provided by all C++-compilers (not ANSI). - **/ - inline int strncasecmp(const char *const str1, const char *const str2, const int l) { - if (!l) return 0; - if (!str1) return str2?-1:0; - const char *nstr1 = str1, *nstr2 = str2; - int k, diff = 0; for (k = 0; kp && str[q]==delimiter; ) { --q; if (!is_iterative) break; } - } - const int n = q - p + 1; - if (n!=l) { std::memmove(str,str+p,n); str[n] = 0; return true; } - return false; - } - - //! Replace escape sequences in C-strings by their binary ascii values. - /** - \param[in,out] str C-string to work with (modified at output). - **/ - inline void strunescape(char *const str) { -#define cimg_strunescape(ci,co) case ci: *nd = co; ++ns; break; - unsigned int val = 0; - for (char *ns = str, *nd = str; *ns || (bool)(*nd=0); ++nd) if (*ns=='\\') switch (*(++ns)) { - cimg_strunescape('n','\n'); - cimg_strunescape('t','\t'); - cimg_strunescape('v','\v'); - cimg_strunescape('b','\b'); - cimg_strunescape('r','\r'); - cimg_strunescape('f','\f'); - cimg_strunescape('a','\a'); - cimg_strunescape('\\','\\'); - cimg_strunescape('\?','\?'); - cimg_strunescape('\'','\''); - cimg_strunescape('\"','\"'); - case 0 : *nd = 0; break; - case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : - std::sscanf(ns,"%o",&val); while (*ns>='0' && *ns<='7') ++ns; - *nd = val; break; - case 'x': - std::sscanf(++ns,"%x",&val); - while ((*ns>='0' && *ns<='7') || (*ns>='a' && *ns<='f') || (*ns>='A' && *ns<='F')) ++ns; - *nd = val; break; - default : *nd = *(ns++); - } else *nd = *(ns++); - } - - // Return a temporary string describing the size of a memory buffer. - inline const char *strbuffersize(const unsigned long size) { - static char res[256] = { 0 }; - cimg::mutex(5); - if (size<1024LU) cimg_snprintf(res,sizeof(res),"%lu byte%s",size,size>1?"s":""); - else if (size<1024*1024LU) { const float nsize = size/1024.0f; cimg_snprintf(res,sizeof(res),"%.1f Kio",nsize); } - else if (size<1024*1024*1024LU) { - const float nsize = size/(1024*1024.0f); cimg_snprintf(res,sizeof(res),"%.1f Mio",nsize); - } else { const float nsize = size/(1024*1024*1024.0f); cimg_snprintf(res,sizeof(res),"%.1f Gio",nsize); } - cimg::mutex(5,0); - return res; - } - - // Return string that identifies the running OS. - inline const char *stros() { -#if defined(linux) || defined(__linux) || defined(__linux__) - const char *const str = "Linux"; -#elif defined(sun) || defined(__sun) - const char *const str = "Sun OS"; -#elif defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined (__DragonFly__) - const char *const str = "BSD"; -#elif defined(sgi) || defined(__sgi) - const char *const str = "Irix"; -#elif defined(__MACOSX__) || defined(__APPLE__) - const char *const str = "Mac OS"; -#elif defined(unix) || defined(__unix) || defined(__unix__) - const char *const str = "Generic Unix"; -#elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \ - defined(WIN64) || defined(_WIN64) || defined(__WIN64__) - const char *const str = "Windows"; -#else - const char - *const _str1 = std::getenv("OSTYPE"), - *const _str2 = _str1?_str1:std::getenv("OS"), - *const str = _str2?_str2:"Unknown OS"; -#endif - return str; - } - - //! Return the basename of a filename. - inline const char* basename(const char *const str) { - const char *p = 0; - for (const char *np = str; np>=str && (p=np); np = std::strchr(np,cimg_file_separator)+1) {} - return p; - } - - // Return a random filename. - inline const char* filenamerand() { - cimg::mutex(6); - static char randomid[9] = { 0 }; - cimg::srand(); - for (unsigned int k = 0; k<8; ++k) { - const int v = (int)std::rand()%3; - randomid[k] = (char)(v==0?('0'+(std::rand()%10)):(v==1?('a'+(std::rand()%26)):('A'+(std::rand()%26)))); - } - cimg::mutex(6,0); - return randomid; - } - - // Convert filename as a Windows-style filename (short path name). - inline void winformat_string(char *const str) { - if (str && *str) { -#if cimg_OS==2 - char *const nstr = new char[MAX_PATH]; - if (GetShortPathNameA(str,nstr,MAX_PATH)) std::strcpy(str,nstr); -#endif - } - } - - //! Open a file. - /** - \param path Path of the filename to open. - \param mode C-string describing the opening mode. - \return Opened file. - \note Same as std::fopen() but throw a \c CImgIOException when - the specified file cannot be opened, instead of returning \c 0. - **/ - inline std::FILE *fopen(const char *const path, const char *const mode) { - if (!path) - throw CImgArgumentException("cimg::fopen(): Specified file path is (null)."); - if (!mode) - throw CImgArgumentException("cimg::fopen(): File '%s', specified mode is (null).", - path); - std::FILE *res = 0; - if (*path=='-' && (!path[1] || path[1]=='.')) { - res = (*mode=='r')?stdin:stdout; -#if cimg_OS==2 - if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode. - if (_setmode(_fileno(res),0x8000)==-1) res = 0; - } -#endif - } else res = std::fopen(path,mode); - if (!res) throw CImgIOException("cimg::fopen(): Failed to open file '%s' with mode '%s'.", - path,mode); - return res; - } - - //! Close a file. - /** - \param file File to close. - \return \c 0 if file has been closed properly, something else otherwise. - \note Same as std::fclose() but display a warning message if - the file has not been closed properly. - **/ - inline int fclose(std::FILE *file) { - if (!file) warn("cimg::fclose(): Specified file is (null)."); - if (!file || file==stdin || file==stdout) return 0; - const int errn = std::fclose(file); - if (errn!=0) warn("cimg::fclose(): Error code %d returned during file closing.", - errn); - return errn; - } - - //! Check if a path is a directory - /** - \param path Specified path to test. - **/ - inline bool is_directory(const char *const path) { - if (!path || !*path) return false; -#if cimg_OS==1 - struct stat st_buf; - if (!stat(path,&st_buf) && S_ISDIR(st_buf.st_mode)) return true; -#elif cimg_OS==2 - if (GetFileAttributes(path)&16) return true; -#endif - return false; - } - - //! Get/set path to store temporary files. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path where temporary files can be saved. - **/ - inline const char* temporary_path(const char *const user_path=0, const bool reinit_path=false) { -#define _cimg_test_temporary_path(p) \ - if (!path_found) { \ - cimg_snprintf(s_path,1024,"%s",p); \ - cimg_snprintf(tmp,sizeof(tmp),"%s%c%s",s_path,cimg_file_separator,filetmp); \ - if ((file=std::fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } \ - } - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - char tmp[1024] = { 0 }, filetmp[512] = { 0 }; - std::FILE *file = 0; - cimg_snprintf(filetmp,sizeof(filetmp),"%s.tmp",cimg::filenamerand()); - char *tmpPath = std::getenv("TMP"); - if (!tmpPath) { tmpPath = std::getenv("TEMP"); winformat_string(tmpPath); } - if (tmpPath) _cimg_test_temporary_path(tmpPath); -#if cimg_OS==2 - _cimg_test_temporary_path("C:\\WINNT\\Temp"); - _cimg_test_temporary_path("C:\\WINDOWS\\Temp"); - _cimg_test_temporary_path("C:\\Temp"); - _cimg_test_temporary_path("C:"); - _cimg_test_temporary_path("D:\\WINNT\\Temp"); - _cimg_test_temporary_path("D:\\WINDOWS\\Temp"); - _cimg_test_temporary_path("D:\\Temp"); - _cimg_test_temporary_path("D:"); -#else - _cimg_test_temporary_path("/tmp"); - _cimg_test_temporary_path("/var/tmp"); -#endif - if (!path_found) { - *s_path = 0; - std::strncpy(tmp,filetmp,sizeof(tmp)-1); - if ((file=std::fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } - } - if (!path_found) { - cimg::mutex(7,0); - throw CImgIOException("cimg::temporary_path(): Failed to locate path for writing temporary files.\n"); - } - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the Program Files/ directory (Windows only). - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the program files. - **/ -#if cimg_OS==2 - inline const char* programfiles_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[MAX_PATH]; - std::memset(s_path,0,MAX_PATH); - // Note: in the following line, 0x26 = CSIDL_PROGRAM_FILES (not defined on every compiler). -#if !defined(__INTEL_COMPILER) - if (!SHGetSpecialFolderPathA(0,s_path,0x0026,false)) { - const char *const pfPath = std::getenv("PROGRAMFILES"); - if (pfPath) std::strncpy(s_path,pfPath,MAX_PATH-1); - else std::strcpy(s_path,"C:\\PROGRA~1"); - } -#else - std::strcpy(s_path,"C:\\PROGRA~1"); -#endif - } - cimg::mutex(7,0); - return s_path; - } -#endif - - //! Get/set path to the ImageMagick's \c convert binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c convert binary. - **/ - inline const char* imagemagick_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - const char *const pf_path = programfiles_path(); - if (!path_found) { - std::strcpy(s_path,".\\convert.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%.2d-\\convert.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d-Q\\convert.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d\\convert.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\convert.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\convert.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\IMAGEM~1.%d\\VISUA~1\\BIN\\convert.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%.2d-\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d-Q\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\IMAGEM~1.%d\\VISUA~1\\BIN\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%.2d-\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d-Q\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\IMAGEM~1.%d\\VISUA~1\\BIN\\convert.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"convert.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./convert"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"convert"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the GraphicsMagick's \c gm binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c gm binary. - **/ - inline const char* graphicsmagick_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - const char *const pf_path = programfiles_path(); - if (!path_found) { - std::strcpy(s_path,".\\gm.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%.2d-\\gm.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d-Q\\gm.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d\\gm.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",pf_path,k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%.2d-\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d-Q\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"C:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%.2d-\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d-Q\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=10 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 9; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - for (int k = 32; k>=0 && !path_found; --k) { - cimg_snprintf(s_path,sizeof(s_path),"D:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"gm.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./gm"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"gm"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the XMedcon's \c medcon binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c medcon binary. - **/ - inline const char* medcon_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - const char *const pf_path = programfiles_path(); - if (!path_found) { - std::strcpy(s_path,".\\medcon.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\XMedCon\\bin\\medcon.bat",pf_path); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) { - cimg_snprintf(s_path,sizeof(s_path),"%s\\XMedCon\\bin\\medcon.exe",pf_path); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) { - std::strcpy(s_path,"C:\\XMedCon\\bin\\medcon.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"medcon.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./medcon"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"medcon"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the FFMPEG's \c ffmpeg binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c ffmpeg binary. - **/ - inline const char *ffmpeg_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - if (!path_found) { - std::strcpy(s_path,".\\ffmpeg.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"ffmpeg.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./ffmpeg"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"ffmpeg"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the \c gzip binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c gzip binary. - **/ - inline const char *gzip_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - if (!path_found) { - std::strcpy(s_path,".\\gzip.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"gzip.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./gzip"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"gzip"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the \c gzip binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c gunzip binary. - **/ - inline const char *gunzip_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - if (!path_found) { - std::strcpy(s_path,".\\gunzip.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"gunzip.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./gunzip"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"gunzip"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the \c dcraw binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c dcraw binary. - **/ - inline const char *dcraw_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - if (!path_found) { - std::strcpy(s_path,".\\dcraw.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"dcraw.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./dcraw"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"dcraw"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the \c wget binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c wget binary. - **/ - inline const char *wget_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - if (!path_found) { - std::strcpy(s_path,".\\wget.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"wget.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./wget"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"wget"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Get/set path to the \c curl binary. - /** - \param user_path Specified path, or \c 0 to get the path currently used. - \param reinit_path Force path to be recalculated (may take some time). - \return Path containing the \c curl binary. - **/ - inline const char *curl_path(const char *const user_path=0, const bool reinit_path=false) { - static char *s_path = 0; - cimg::mutex(7); - if (reinit_path) { delete[] s_path; s_path = 0; } - if (user_path) { - if (!s_path) s_path = new char[1024]; - std::memset(s_path,0,1024); - std::strncpy(s_path,user_path,1023); - } else if (!s_path) { - s_path = new char[1024]; - std::memset(s_path,0,1024); - bool path_found = false; - std::FILE *file = 0; -#if cimg_OS==2 - if (!path_found) { - std::strcpy(s_path,".\\curl.exe"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"curl.exe"); -#else - if (!path_found) { - std::strcpy(s_path,"./curl"); - if ((file=std::fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } - } - if (!path_found) std::strcpy(s_path,"curl"); -#endif - winformat_string(s_path); - } - cimg::mutex(7,0); - return s_path; - } - - //! Split filename into two C-strings \c body and \c extension. - inline const char *split_filename(const char *const filename, char *const body=0) { - if (!filename) { if (body) *body = 0; return 0; } - const char *p = 0; for (const char *np = filename; np>=filename && (p=np); np = std::strchr(np,'.')+1) {} - if (p==filename) { - if (body) std::strcpy(body,filename); - return filename + std::strlen(filename); - } - const unsigned int l = (unsigned int)(p - filename - 1); - if (body) { std::memcpy(body,filename,l); body[l] = 0; } - return p; - } - - //! Generate a numbered version of a filename. - inline char* number_filename(const char *const filename, const int number, - const unsigned int digits, char *const str) { - if (!filename) { if (str) *str = 0; return 0; } - char format[1024] = { 0 }, body[1024] = { 0 }; - const char *const ext = cimg::split_filename(filename,body); - if (*ext) cimg_snprintf(format,sizeof(format),"%%s_%%.%ud.%%s",digits); - else cimg_snprintf(format,sizeof(format),"%%s_%%.%ud",digits); - std::sprintf(str,format,body,number,ext); - return str; - } - - //! Try to guess format from an image file. - /** - \param file Input file (can be \c 0 if \c filename is set). - \param filename Filename, as a C-string (can be \c 0 if \c file is set). - \return C-string containing the guessed file format, or \c 0 if nothing has been guessed. - **/ - inline const char *file_type(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException("cimg::file_type(): Specified filename is (null)."); - static const char - *const _pnm = "pnm", - *const _pfm = "pfm", - *const _bmp = "bmp", - *const _gif = "gif", - *const _jpg = "jpg", - *const _off = "off", - *const _pan = "pan", - *const _png = "png", - *const _tif = "tif", - *const _inr = "inr", - *const _dcm = "dcm"; - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - const char *f_type = 0, *head; - char header[2048] = { 0 }, item[1024] = { 0 }; - const unsigned char *const uheader = (unsigned char*)header; - int err; char cerr; - const unsigned int siz = (unsigned int)std::fread(header,2048,1,nfile); // Read first 2048 bytes. - if (!file) cimg::fclose(nfile); - - if (!std::strncmp(header,"OFF\n",4)) f_type = _off; // OFF. - else if (!std::strncmp(header,"#INRIMAGE",9)) f_type = _inr; // INRIMAGE. - else if (!std::strncmp(header,"PANDORE",7)) f_type = _pan; // PANDORE. - else if (!std::strncmp(header+128,"DICM",4)) f_type = _dcm; // DICOM. - else if (uheader[0]==0xFF && uheader[1]==0xD8 && uheader[2]==0xFF) f_type = _jpg; // JPEG. - else if (header[0]=='B' && header[1]=='M') f_type = _bmp; // BMP. - else if (header[0]=='G' && header[1]=='I' && header[2]=='F' && header[3]=='8' && header[5]=='a' && // GIF. - (header[4]=='7' || header[4]=='9')) f_type = _gif; - else if (uheader[0]==0x89 && uheader[1]==0x50 && uheader[2]==0x4E && uheader[3]==0x47 && // PNG. - uheader[4]==0x0D && uheader[5]==0x0A && uheader[6]==0x1A && uheader[7]==0x0A) f_type = _png; - else if ((uheader[0]==0x49 && uheader[1]==0x49) || (uheader[0]==0x4D && uheader[1]==0x4D)) f_type = _tif; // TIFF. - else { // PNM or PFM. - head = header; - while (headstd::fread() but may display warning message if all elements could not be read. - **/ - template - inline int fread(T *const ptr, const unsigned long nmemb, std::FILE *stream) { - if (!ptr || nmemb<=0 || !stream) - throw CImgArgumentException("cimg::fread(): Invalid reading request of %u %s%s from file %p to buffer %p.", - nmemb,cimg::type::string(),nmemb>1?"s":"",stream,ptr); - - const unsigned long wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T); - unsigned long to_read = nmemb, al_read = 0, l_to_read = 0, l_al_read = 0; - do { - l_to_read = (to_read*sizeof(T))0); - if (to_read>0) - warn("cimg::fread(): Only %u/%u elements could be read from file.", - al_read,nmemb); - return al_read; - } - - //! Write data to file. - /** - \param ptr Pointer to memory buffer containing the binary data to write on file. - \param nmemb Number of elements to write. - \param[out] stream File to write data on. - \return Number of written elements. - \note Similar to std::fwrite but may display warning messages if all elements could not be written. - **/ - template - inline int fwrite(const T *ptr, const unsigned long nmemb, std::FILE *stream) { - if (!ptr || !stream) - throw CImgArgumentException("cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.", - nmemb,cimg::type::string(),nmemb>1?"s":"",ptr,stream); - if (nmemb<=0) return 0; - const unsigned long wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T); - unsigned long to_write = nmemb, al_write = 0, l_to_write = 0, l_al_write = 0; - do { - l_to_write = (to_write*sizeof(T))0); - if (to_write>0) - warn("cimg::fwrite(): Only %u/%u elements could be written in file.", - al_write,nmemb); - return al_write; - } - - //! Create an empty file. - /** - \param file Input file (can be \c 0 if \c filename is set). - \param filename Filename, as a C-string (can be \c 0 if \c file is set). - **/ - inline void fempty(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException("cimg::file_type(): Specified filename is (null)."); - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - if (!file) cimg::fclose(nfile); - } - - //! Load file from network as a local temporary file. - /** - \param filename Filename, as a C-string. - \param[out] filename_local C-string containing the path to a local copy of \c filename. - \return Value of \c filename_local. - \note Use external binaries \c wget or \c curl to perform. You must have one of these tools installed - to be able to use this function. - **/ - inline char *load_network_external(const char *const filename, char *const filename_local) { - if (!filename) - throw CImgArgumentException("cimg::load_network_external(): Specified filename is (null)."); - if (!filename_local) - throw CImgArgumentException("cimg::load_network_external(): Specified destination string is (null)."); - const char *const _ext = cimg::split_filename(filename), *const ext = (*_ext && _ext>filename)?_ext-1:_ext; - char command[1024] = { 0 }; - std::FILE *file = 0; - *filename_local = 0; - do { - cimg_snprintf(filename_local,512,"%s%c%s%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); - if ((file=std::fopen(filename_local,"rb"))!=0) cimg::fclose(file); - } while (file); - - // Try with 'curl' first. - cimg_snprintf(command,sizeof(command),"%s -f --silent --compressed -o \"%s\" \"%s\"", - cimg::curl_path(),filename_local,filename); - cimg::system(command); - if (!(file = std::fopen(filename_local,"rb"))) { - - // Try with 'wget' else. - cimg_snprintf(command,sizeof(command),"%s -q -r -l 0 --no-cache -O \"%s\" \"%s\"", - cimg::wget_path(),filename_local,filename); - cimg::system(command); - if (!(file = std::fopen(filename_local,"rb"))) - throw CImgIOException("cimg::load_network_external(): Failed to load file '%s' with external commands " - "'wget' or 'curl'.",filename); - cimg::fclose(file); - - // Try gunzip it. - cimg_snprintf(command,sizeof(command),"%s.gz",filename_local); - std::rename(filename_local,command); - cimg_snprintf(command,sizeof(command),"%s --quiet \"%s.gz\"", - gunzip_path(),filename_local); - cimg::system(command); - file = std::fopen(filename_local,"rb"); - if (!file) { - cimg_snprintf(command,sizeof(command),"%s.gz",filename_local); - std::rename(command,filename_local); - file = std::fopen(filename_local,"rb"); - } - } - std::fseek(file,0,SEEK_END); // Check if file size is 0. - if (std::ftell(file)<=0) - throw CImgIOException("cimg::load_network_external(): Failed to load file '%s' with external commands " - "'wget' or 'curl'.",filename); - cimg::fclose(file); - return filename_local; - } - - //! Return options specified on the command line. - inline const char* option(const char *const name, const int argc, const char *const *const argv, - const char *const defaut, const char *const usage, const bool reset_static) { - static bool first = true, visu = false; - if (reset_static) { first = true; return 0; } - const char *res = 0; - if (first) { - first = false; - visu = cimg::option("-h",argc,argv,(char*)0,(char*)0,false)!=0; - visu |= cimg::option("-help",argc,argv,(char*)0,(char*)0,false)!=0; - visu |= cimg::option("--help",argc,argv,(char*)0,(char*)0,false)!=0; - } - if (!name && visu) { - if (usage) { - std::fprintf(cimg::output(),"\n %s%s%s",cimg::t_red,cimg::basename(argv[0]),cimg::t_normal); - std::fprintf(cimg::output(),": %s",usage); - std::fprintf(cimg::output()," (%s, %s)\n\n",__DATE__,__TIME__); - } - if (defaut) std::fprintf(cimg::output(),"%s\n",defaut); - } - if (name) { - if (argc>0) { - int k = 0; - while (k Operating System: %s%-13s%s %s('cimg_OS'=%d)%s\n", - cimg::t_bold, - cimg_OS==1?"Unix":(cimg_OS==2?"Windows":"Unknow"), - cimg::t_normal,cimg::t_green, - cimg_OS, - cimg::t_normal); - - std::fprintf(cimg::output()," > CPU endianness: %s%s Endian%s\n", - cimg::t_bold, - cimg::endianness()?"Big":"Little", - cimg::t_normal); - - std::fprintf(cimg::output()," > Verbosity mode: %s%-13s%s %s('cimg_verbosity'=%d)%s\n", - cimg::t_bold, - cimg_verbosity==0?"Quiet": - cimg_verbosity==1?"Console": - cimg_verbosity==2?"Dialog": - cimg_verbosity==3?"Console+Warnings":"Dialog+Warnings", - cimg::t_normal,cimg::t_green, - cimg_verbosity, - cimg::t_normal); - - std::fprintf(cimg::output()," > Stricts warnings: %s%-13s%s %s('cimg_strict_warnings' %s)%s\n", - cimg::t_bold, -#ifdef cimg_strict_warnings - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Using VT100 messages: %s%-13s%s %s('cimg_use_vt100' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_vt100 - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Display type: %s%-13s%s %s('cimg_display'=%d)%s\n", - cimg::t_bold, - cimg_display==0?"No display":cimg_display==1?"X11":cimg_display==2?"Windows GDI":"Unknown", - cimg::t_normal,cimg::t_green, - cimg_display, - cimg::t_normal); - -#if cimg_display==1 - std::fprintf(cimg::output()," > Using XShm for X11: %s%-13s%s %s('cimg_use_xshm' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_xshm - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Using XRand for X11: %s%-13s%s %s('cimg_use_xrandr' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_xrandr - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); -#endif - std::fprintf(cimg::output()," > Using OpenMP: %s%-13s%s %s('cimg_use_openmp' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_openmp - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - std::fprintf(cimg::output()," > Using PNG library: %s%-13s%s %s('cimg_use_png' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_png - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - std::fprintf(cimg::output()," > Using JPEG library: %s%-13s%s %s('cimg_use_jpeg' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_jpeg - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Using TIFF library: %s%-13s%s %s('cimg_use_tiff' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_tiff - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Using Magick++ library: %s%-13s%s %s('cimg_use_magick' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_magick - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Using FFTW3 library: %s%-13s%s %s('cimg_use_fftw3' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_fftw3 - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - std::fprintf(cimg::output()," > Using LAPACK library: %s%-13s%s %s('cimg_use_lapack' %s)%s\n", - cimg::t_bold, -#ifdef cimg_use_lapack - "Yes",cimg::t_normal,cimg::t_green,"defined", -#else - "No",cimg::t_normal,cimg::t_green,"undefined", -#endif - cimg::t_normal); - - cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::imagemagick_path()); - std::fprintf(cimg::output()," > Path of ImageMagick: %s%-13s%s\n", - cimg::t_bold, - tmp, - cimg::t_normal); - - cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::graphicsmagick_path()); - std::fprintf(cimg::output()," > Path of GraphicsMagick: %s%-13s%s\n", - cimg::t_bold, - tmp, - cimg::t_normal); - - cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::medcon_path()); - std::fprintf(cimg::output()," > Path of 'medcon': %s%-13s%s\n", - cimg::t_bold, - tmp, - cimg::t_normal); - - cimg_snprintf(tmp,sizeof(tmp),"\"%.1020s\"",cimg::temporary_path()); - std::fprintf(cimg::output()," > Temporary path: %s%-13s%s\n", - cimg::t_bold, - tmp, - cimg::t_normal); - - std::fprintf(cimg::output(),"\n"); - } - - // Declare LAPACK function signatures if LAPACK support is enabled. -#ifdef cimg_use_lapack - template - inline void getrf(int &N, T *lapA, int *IPIV, int &INFO) { - dgetrf_(&N,&N,lapA,&N,IPIV,&INFO); - } - - inline void getrf(int &N, float *lapA, int *IPIV, int &INFO) { - sgetrf_(&N,&N,lapA,&N,IPIV,&INFO); - } - - template - inline void getri(int &N, T *lapA, int *IPIV, T* WORK, int &LWORK, int &INFO) { - dgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO); - } - - inline void getri(int &N, float *lapA, int *IPIV, float* WORK, int &LWORK, int &INFO) { - sgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO); - } - - template - inline void gesvd(char &JOB, int &M, int &N, T *lapA, int &MN, - T *lapS, T *lapU, T *lapV, T *WORK, int &LWORK, int &INFO) { - dgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO); - } - - inline void gesvd(char &JOB, int &M, int &N, float *lapA, int &MN, - float *lapS, float *lapU, float *lapV, float *WORK, int &LWORK, int &INFO) { - sgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO); - } - - template - inline void getrs(char &TRANS, int &N, T *lapA, int *IPIV, T *lapB, int &INFO) { - int one = 1; - dgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO); - } - - inline void getrs(char &TRANS, int &N, float *lapA, int *IPIV, float *lapB, int &INFO) { - int one = 1; - sgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO); - } - - template - inline void syev(char &JOB, char &UPLO, int &N, T *lapA, T *lapW, T *WORK, int &LWORK, int &INFO) { - dsyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO); - } - - inline void syev(char &JOB, char &UPLO, int &N, float *lapA, float *lapW, float *WORK, int &LWORK, int &INFO) { - ssyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO); - } - - template - inline void sgels(char & TRANS, int &M, int &N, int &NRHS, T* lapA, int &LDA, - T* lapB, int &LDB, T* WORK, int &LWORK, int &INFO){ - dgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO); - } - - inline void sgels(char & TRANS, int &M, int &N, int &NRHS, float* lapA, int &LDA, - float* lapB, int &LDB, float* WORK, int &LWORK, int &INFO){ - sgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO); - } - -#endif - - // End of the 'cimg' namespace - } - - /*------------------------------------------------ - # - # - # Definition of mathematical operators and - # external functions. - # - # - -------------------------------------------------*/ - -#define _cimg_create_ext_operators(typ) \ - template \ - inline CImg::type> operator+(const typ val, const CImg& img) { \ - return img + val; \ - } \ - template \ - inline CImg::type> operator-(const typ val, const CImg& img) { \ - typedef typename cimg::superset::type Tt; \ - return CImg(img._width,img._height,img._depth,img._spectrum,val)-=img; \ - } \ - template \ - inline CImg::type> operator*(const typ val, const CImg& img) { \ - return img*val; \ - } \ - template \ - inline CImg::type> operator/(const typ val, const CImg& img) { \ - return val*img.get_invert(); \ - } \ - template \ - inline CImg::type> operator&(const typ val, const CImg& img) { \ - return img & val; \ - } \ - template \ - inline CImg::type> operator|(const typ val, const CImg& img) { \ - return img | val; \ - } \ - template \ - inline CImg::type> operator^(const typ val, const CImg& img) { \ - return img ^ val; \ - } \ - template \ - inline bool operator==(const typ val, const CImg& img) { \ - return img == val; \ - } \ - template \ - inline bool operator!=(const typ val, const CImg& img) { \ - return img != val; \ - } - - _cimg_create_ext_operators(bool) - _cimg_create_ext_operators(unsigned char) - _cimg_create_ext_operators(char) - _cimg_create_ext_operators(signed char) - _cimg_create_ext_operators(unsigned short) - _cimg_create_ext_operators(short) - _cimg_create_ext_operators(unsigned int) - _cimg_create_ext_operators(int) - _cimg_create_ext_operators(unsigned long) - _cimg_create_ext_operators(long) - _cimg_create_ext_operators(float) - _cimg_create_ext_operators(double) - - template - inline CImg<_cimg_Tfloat> operator+(const char *const expression, const CImg& img) { - return img + expression; - } - - template - inline CImg<_cimg_Tfloat> operator-(const char *const expression, const CImg& img) { - return CImg<_cimg_Tfloat>(img._width,img._height,img._depth,img._spectrum,expression,true)-=img; - } - - template - inline CImg<_cimg_Tfloat> operator*(const char *const expression, const CImg& img) { - return img*expression; - } - - template - inline CImg<_cimg_Tfloat> operator/(const char *const expression, const CImg& img) { - return expression*img.get_invert(); - } - - template - inline CImg operator&(const char *const expression, const CImg& img) { - return img & expression; - } - - template - inline CImg operator|(const char *const expression, const CImg& img) { - return img | expression; - } - - template - inline CImg operator^(const char *const expression, const CImg& img) { - return img ^ expression; - } - - template - inline bool operator==(const char *const expression, const CImg& img) { - return img == expression; - } - - template - inline bool operator!=(const char *const expression, const CImg& img) { - return img != expression; - } - - template - inline CImg<_cimg_Tfloat> sqr(const CImg& instance) { - return instance.get_sqr(); - } - - template - inline CImg<_cimg_Tfloat> sqrt(const CImg& instance) { - return instance.get_sqrt(); - } - - template - inline CImg<_cimg_Tfloat> exp(const CImg& instance) { - return instance.get_exp(); - } - - template - inline CImg<_cimg_Tfloat> log(const CImg& instance) { - return instance.get_log(); - } - - template - inline CImg<_cimg_Tfloat> log2(const CImg& instance) { - return instance.get_log2(); - } - - template - inline CImg<_cimg_Tfloat> log10(const CImg& instance) { - return instance.get_log10(); - } - - template - inline CImg<_cimg_Tfloat> abs(const CImg& instance) { - return instance.get_abs(); - } - - template - inline CImg<_cimg_Tfloat> sign(const CImg& instance) { - return instance.get_sign(); - } - - template - inline CImg<_cimg_Tfloat> cos(const CImg& instance) { - return instance.get_cos(); - } - - template - inline CImg<_cimg_Tfloat> sin(const CImg& instance) { - return instance.get_sin(); - } - - template - inline CImg<_cimg_Tfloat> sinc(const CImg& instance) { - return instance.get_sinc(); - } - - template - inline CImg<_cimg_Tfloat> tan(const CImg& instance) { - return instance.get_tan(); - } - - template - inline CImg<_cimg_Tfloat> acos(const CImg& instance) { - return instance.get_acos(); - } - - template - inline CImg<_cimg_Tfloat> asin(const CImg& instance) { - return instance.get_asin(); - } - - template - inline CImg<_cimg_Tfloat> atan(const CImg& instance) { - return instance.get_atan(); - } - - template - inline CImg<_cimg_Tfloat> cosh(const CImg& instance) { - return instance.get_cosh(); - } - - template - inline CImg<_cimg_Tfloat> sinh(const CImg& instance) { - return instance.get_sinh(); - } - - template - inline CImg<_cimg_Tfloat> tanh(const CImg& instance) { - return instance.get_tanh(); - } - - template - inline CImg transpose(const CImg& instance) { - return instance.get_transpose(); - } - - template - inline CImg<_cimg_Tfloat> invert(const CImg& instance) { - return instance.get_invert(); - } - - template - inline CImg<_cimg_Tfloat> pseudoinvert(const CImg& instance) { - return instance.get_pseudoinvert(); - } - - /*----------------------------------- - # - # Define the CImgDisplay structure - # - ----------------------------------*/ - //! Allow to create windows, display images on them and manage user events (keyboard, mouse and windows events). - /** - CImgDisplay methods rely on a low-level graphic library to perform: it can be either \b X-Window - (X11, for Unix-based systems) or \b GDI32 (for Windows-based systems). - If both libraries are missing, CImgDisplay will not be able to display images on screen, and will enter - a minimal mode where warning messages will be outputed each time the program is trying to call one of the - CImgDisplay method. - - The configuration variable \c cimg_display tells about the graphic library used. - It is set automatically by \CImg when one of these graphic libraries has been detected. - But, you can override its value if necessary. Valid choices are: - - 0: Disable display capabilities. - - 1: Use \b X-Window (X11) library. - - 2: Use \b GDI32 library. - - Remember to link your program against \b X11 or \b GDI32 libraries if you use CImgDisplay. - **/ - struct CImgDisplay { - unsigned long _timer, _fps_frames, _fps_timer; - unsigned int _width, _height, _normalization; - float _fps_fps, _min, _max; - bool _is_fullscreen; - char *_title; - volatile unsigned int _window_width, _window_height, _button, _keys[128], _released_keys[128]; - volatile int _window_x, _window_y, _mouse_x, _mouse_y, _wheel; - volatile bool _is_closed, _is_resized, _is_moved, _is_event, - _is_keyESC, _is_keyF1, _is_keyF2, _is_keyF3, _is_keyF4, _is_keyF5, _is_keyF6, _is_keyF7, - _is_keyF8, _is_keyF9, _is_keyF10, _is_keyF11, _is_keyF12, _is_keyPAUSE, _is_key1, _is_key2, - _is_key3, _is_key4, _is_key5, _is_key6, _is_key7, _is_key8, _is_key9, _is_key0, - _is_keyBACKSPACE, _is_keyINSERT, _is_keyHOME, _is_keyPAGEUP, _is_keyTAB, _is_keyQ, _is_keyW, _is_keyE, - _is_keyR, _is_keyT, _is_keyY, _is_keyU, _is_keyI, _is_keyO, _is_keyP, _is_keyDELETE, - _is_keyEND, _is_keyPAGEDOWN, _is_keyCAPSLOCK, _is_keyA, _is_keyS, _is_keyD, _is_keyF, _is_keyG, - _is_keyH, _is_keyJ, _is_keyK, _is_keyL, _is_keyENTER, _is_keySHIFTLEFT, _is_keyZ, _is_keyX, - _is_keyC, _is_keyV, _is_keyB, _is_keyN, _is_keyM, _is_keySHIFTRIGHT, _is_keyARROWUP, _is_keyCTRLLEFT, - _is_keyAPPLEFT, _is_keyALT, _is_keySPACE, _is_keyALTGR, _is_keyAPPRIGHT, _is_keyMENU, _is_keyCTRLRIGHT, - _is_keyARROWLEFT, _is_keyARROWDOWN, _is_keyARROWRIGHT, _is_keyPAD0, _is_keyPAD1, _is_keyPAD2, _is_keyPAD3, - _is_keyPAD4, _is_keyPAD5, _is_keyPAD6, _is_keyPAD7, _is_keyPAD8, _is_keyPAD9, _is_keyPADADD, _is_keyPADSUB, - _is_keyPADMUL, _is_keyPADDIV; - - //@} - //--------------------------- - // - //! \name Plugins - //@{ - //--------------------------- - -#ifdef cimgdisplay_plugin -#include cimgdisplay_plugin -#endif -#ifdef cimgdisplay_plugin1 -#include cimgdisplay_plugin1 -#endif -#ifdef cimgdisplay_plugin2 -#include cimgdisplay_plugin2 -#endif -#ifdef cimgdisplay_plugin3 -#include cimgdisplay_plugin3 -#endif -#ifdef cimgdisplay_plugin4 -#include cimgdisplay_plugin4 -#endif -#ifdef cimgdisplay_plugin5 -#include cimgdisplay_plugin5 -#endif -#ifdef cimgdisplay_plugin6 -#include cimgdisplay_plugin6 -#endif -#ifdef cimgdisplay_plugin7 -#include cimgdisplay_plugin7 -#endif -#ifdef cimgdisplay_plugin8 -#include cimgdisplay_plugin8 -#endif - - //@} - //-------------------------------------------------------- - // - //! \name Constructors / Destructor / Instance Management - //@{ - //-------------------------------------------------------- - - //! Destructor. - /** - \note If the associated window is visible on the screen, it is closed by the call to the destructor. - **/ - ~CImgDisplay() { - assign(); - } - - //! Construct an empty display. - /** - \note Constructing an empty CImgDisplay instance does not make a window appearing on the screen, until - display of valid data is performed. - \par Example - \code - CImgDisplay disp; // Does actually nothing. - ... - disp.display(img); // Construct new window and display image in it. - \endcode - **/ - CImgDisplay(): - _width(0),_height(0),_normalization(0), - _min(0),_max(0), - _is_fullscreen(false), - _title(0), - _window_width(0),_window_height(0),_button(0), - _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), - _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { - assign(); - } - - //! Construct a display with specified dimensions. - /** \param width Window width. - \param height Window height. - \param title Window title. - \param normalization Normalization type - (0=none, 1=always, 2=once, 3=pixel type-dependent, see normalization()). - \param is_fullscreen Tells if fullscreen mode is enabled. - \param is_closed Tells if associated window is initially visible or not. - \note A black background is initially displayed on the associated window. - **/ - CImgDisplay(const unsigned int width, const unsigned int height, - const char *const title=0, const unsigned int normalization=3, - const bool is_fullscreen=false, const bool is_closed=false): - _width(0),_height(0),_normalization(0), - _min(0),_max(0), - _is_fullscreen(false), - _title(0), - _window_width(0),_window_height(0),_button(0), - _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), - _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { - assign(width,height,title,normalization,is_fullscreen,is_closed); - } - - //! Construct a display from an image. - /** \param img Image used as a model to create the window. - \param title Window title. - \param normalization Normalization type - (0=none, 1=always, 2=once, 3=pixel type-dependent, see normalization()). - \param is_fullscreen Tells if fullscreen mode is enabled. - \param is_closed Tells if associated window is initially visible or not. - \note The pixels of the input image are initially displayed on the associated window. - **/ - template - explicit CImgDisplay(const CImg& img, - const char *const title=0, const unsigned int normalization=3, - const bool is_fullscreen=false, const bool is_closed=false): - _width(0),_height(0),_normalization(0), - _min(0),_max(0), - _is_fullscreen(false), - _title(0), - _window_width(0),_window_height(0),_button(0), - _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), - _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { - assign(img,title,normalization,is_fullscreen,is_closed); - } - - //! Construct a display from an image list. - /** \param list The images list to display. - \param title Window title. - \param normalization Normalization type - (0=none, 1=always, 2=once, 3=pixel type-dependent, see normalization()). - \param is_fullscreen Tells if fullscreen mode is enabled. - \param is_closed Tells if associated window is initially visible or not. - \note All images of the list, appended along the X-axis, are initially displayed on the associated window. - **/ - template - explicit CImgDisplay(const CImgList& list, - const char *const title=0, const unsigned int normalization=3, - const bool is_fullscreen=false, const bool is_closed=false): - _width(0),_height(0),_normalization(0), - _min(0),_max(0), - _is_fullscreen(false), - _title(0), - _window_width(0),_window_height(0),_button(0), - _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), - _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { - assign(list,title,normalization,is_fullscreen,is_closed); - } - - //! Construct a display as a copy of an existing one. - /** - \param disp Display instance to copy. - \note The pixel buffer of the input window is initially displayed on the associated window. - **/ - CImgDisplay(const CImgDisplay& disp): - _width(0),_height(0),_normalization(0), - _min(0),_max(0), - _is_fullscreen(false), - _title(0), - _window_width(0),_window_height(0),_button(0), - _window_x(0),_window_y(0),_mouse_x(-1),_mouse_y(-1),_wheel(0), - _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) { - assign(disp); - } - -#if cimg_display==0 - - static void _no_display_exception() { - throw CImgDisplayException("CImgDisplay(): No display available."); - } - - //! Destructor - Empty constructor \inplace. - /** - \note Replace the current instance by an empty display. - **/ - CImgDisplay& assign() { - return flush(); - } - - //! Construct a display with specified dimensions \inplace. - /** - **/ - CImgDisplay& assign(const unsigned int width, const unsigned int height, - const char *const title=0, const unsigned int normalization=3, - const bool is_fullscreen=false, const bool is_closed=false) { - cimg::unused(width,height,title,normalization,is_fullscreen,is_closed); - _no_display_exception(); - return assign(); - } - - //! Construct a display from an image \inplace. - /** - **/ - template - CImgDisplay& assign(const CImg& img, - const char *const title=0, const unsigned int normalization=3, - const bool is_fullscreen=false, const bool is_closed=false) { - _no_display_exception(); - return assign(img._width,img._height,title,normalization,is_fullscreen,is_closed); - } - - //! Construct a display from an image list \inplace. - /** - **/ - template - CImgDisplay& assign(const CImgList& list, - const char *const title=0, const unsigned int normalization=3, - const bool is_fullscreen=false, const bool is_closed=false) { - _no_display_exception(); - return assign(list._width,list._width,title,normalization,is_fullscreen,is_closed); - } - - //! Construct a display as a copy of another one \inplace. - /** - **/ - CImgDisplay& assign(const CImgDisplay &disp) { - _no_display_exception(); - return assign(disp._width,disp._height); - } - -#endif - - //! Return a reference to an empty display. - /** - \note Can be useful for writing function prototypes where one of the argument (of type CImgDisplay&) - must have a default value. - \par Example - \code - void foo(CImgDisplay& disp=CImgDisplay::empty()); - \endcode - **/ - static CImgDisplay& empty() { - static CImgDisplay _empty; - return _empty.assign(); - } - -#define cimg_fitscreen(dx,dy,dz) CImgDisplay::_fitscreen(dx,dy,dz,128,-85,false), \ - CImgDisplay::_fitscreen(dx,dy,dz,128,-85,true) - static unsigned int _fitscreen(const unsigned int dx, const unsigned int dy, const unsigned int dz, - const int dmin, const int dmax,const bool return_y) { - const unsigned int _nw = dx + (dz>1?dz:0), _nh = dy + (dz>1?dz:0); - unsigned int nw = _nw?_nw:1, nh = _nh?_nh:1; - const unsigned int - sw = CImgDisplay::screen_width(), sh = CImgDisplay::screen_height(), - mw = dmin<0?(unsigned int)(sw*-dmin/100):(unsigned int)dmin, - mh = dmin<0?(unsigned int)(sh*-dmin/100):(unsigned int)dmin, - Mw = dmax<0?(unsigned int)(sw*-dmax/100):(unsigned int)dmax, - Mh = dmax<0?(unsigned int)(sh*-dmax/100):(unsigned int)dmax; - if (nwMw) { nh = nh*Mw/nw; nh+=(nh==0?1:0); nw = Mw; } - if (nh>Mh) { nw = nw*Mh/nh; nw+=(nw==0?1:0); nh = Mh; } - if (nwdisp = img is equivalent to disp.display(img). - **/ - template - CImgDisplay& operator=(const CImg& img) { - return display(img); - } - - //! Display list of images on associated window. - /** - \note disp = list is equivalent to disp.display(list). - **/ - template - CImgDisplay& operator=(const CImgList& list) { - return display(list); - } - - //! Construct a display as a copy of another one \inplace. - /** - \note Equivalent to assign(const CImgDisplay&). - **/ - CImgDisplay& operator=(const CImgDisplay& disp) { - return assign(disp); - } - - //! Return \c false if display is empty, \c true otherwise. - /** - \note if (disp) { ... } is equivalent to if (!disp.is_empty()) { ... }. - **/ - operator bool() const { - return !is_empty(); - } - - //@} - //------------------------------------------ - // - //! \name Instance Checking - //@{ - //------------------------------------------ - - //! Return \c true if display is empty, \c false otherwise. - /** - **/ - bool is_empty() const { - return !(_width && _height); - } - - //! Return \c true if display is closed (i.e. not visible on the screen), \c false otherwise. - /** - \note - - When a user physically closes the associated window, the display is set to closed. - - A closed display is not destroyed. Its associated window can be show again on the screen using show(). - **/ - bool is_closed() const { - return _is_closed; - } - - //! Return \c true if associated window has been resized on the screen, \c false otherwise. - /** - **/ - bool is_resized() const { - return _is_resized; - } - - //! Return \c true if associated window has been moved on the screen, \c false otherwise. - /** - **/ - bool is_moved() const { - return _is_moved; - } - - //! Return \c true if any event has occured on the associated window, \c false otherwise. - /** - **/ - bool is_event() const { - return _is_event; - } - - //! Return \c true if current display is in fullscreen mode, \c false otherwise. - /** - **/ - bool is_fullscreen() const { - return _is_fullscreen; - } - - //! Return \c true if any key is being pressed on the associated window, \c false otherwise. - /** - \note The methods below do the same only for specific keys. - **/ - bool is_key() const { - return _is_keyESC || _is_keyF1 || _is_keyF2 || _is_keyF3 || - _is_keyF4 || _is_keyF5 || _is_keyF6 || _is_keyF7 || - _is_keyF8 || _is_keyF9 || _is_keyF10 || _is_keyF11 || - _is_keyF12 || _is_keyPAUSE || _is_key1 || _is_key2 || - _is_key3 || _is_key4 || _is_key5 || _is_key6 || - _is_key7 || _is_key8 || _is_key9 || _is_key0 || - _is_keyBACKSPACE || _is_keyINSERT || _is_keyHOME || - _is_keyPAGEUP || _is_keyTAB || _is_keyQ || _is_keyW || - _is_keyE || _is_keyR || _is_keyT || _is_keyY || - _is_keyU || _is_keyI || _is_keyO || _is_keyP || - _is_keyDELETE || _is_keyEND || _is_keyPAGEDOWN || - _is_keyCAPSLOCK || _is_keyA || _is_keyS || _is_keyD || - _is_keyF || _is_keyG || _is_keyH || _is_keyJ || - _is_keyK || _is_keyL || _is_keyENTER || - _is_keySHIFTLEFT || _is_keyZ || _is_keyX || _is_keyC || - _is_keyV || _is_keyB || _is_keyN || _is_keyM || - _is_keySHIFTRIGHT || _is_keyARROWUP || _is_keyCTRLLEFT || - _is_keyAPPLEFT || _is_keyALT || _is_keySPACE || _is_keyALTGR || - _is_keyAPPRIGHT || _is_keyMENU || _is_keyCTRLRIGHT || - _is_keyARROWLEFT || _is_keyARROWDOWN || _is_keyARROWRIGHT || - _is_keyPAD0 || _is_keyPAD1 || _is_keyPAD2 || - _is_keyPAD3 || _is_keyPAD4 || _is_keyPAD5 || - _is_keyPAD6 || _is_keyPAD7 || _is_keyPAD8 || - _is_keyPAD9 || _is_keyPADADD || _is_keyPADSUB || - _is_keyPADMUL || _is_keyPADDIV; - } - - //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise. - /** - \param keycode Keycode to test. - \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure - your code stay portable (see cimg::keyESC). - \par Example - \code - CImgDisplay disp(400,400); - while (!disp.is_closed()) { - if (disp.key(cimg::keyTAB)) { ... } // Equivalent to 'if (disp.is_keyTAB())'. - disp.wait(); - } - \endcode - **/ - bool is_key(const unsigned int keycode) const { -#define _cimg_iskey_test(k) if (keycode==cimg::key##k) return _is_key##k; - _cimg_iskey_test(ESC); _cimg_iskey_test(F1); _cimg_iskey_test(F2); _cimg_iskey_test(F3); - _cimg_iskey_test(F4); _cimg_iskey_test(F5); _cimg_iskey_test(F6); _cimg_iskey_test(F7); - _cimg_iskey_test(F8); _cimg_iskey_test(F9); _cimg_iskey_test(F10); _cimg_iskey_test(F11); - _cimg_iskey_test(F12); _cimg_iskey_test(PAUSE); _cimg_iskey_test(1); _cimg_iskey_test(2); - _cimg_iskey_test(3); _cimg_iskey_test(4); _cimg_iskey_test(5); _cimg_iskey_test(6); - _cimg_iskey_test(7); _cimg_iskey_test(8); _cimg_iskey_test(9); _cimg_iskey_test(0); - _cimg_iskey_test(BACKSPACE); _cimg_iskey_test(INSERT); _cimg_iskey_test(HOME); - _cimg_iskey_test(PAGEUP); _cimg_iskey_test(TAB); _cimg_iskey_test(Q); _cimg_iskey_test(W); - _cimg_iskey_test(E); _cimg_iskey_test(R); _cimg_iskey_test(T); _cimg_iskey_test(Y); - _cimg_iskey_test(U); _cimg_iskey_test(I); _cimg_iskey_test(O); _cimg_iskey_test(P); - _cimg_iskey_test(DELETE); _cimg_iskey_test(END); _cimg_iskey_test(PAGEDOWN); - _cimg_iskey_test(CAPSLOCK); _cimg_iskey_test(A); _cimg_iskey_test(S); _cimg_iskey_test(D); - _cimg_iskey_test(F); _cimg_iskey_test(G); _cimg_iskey_test(H); _cimg_iskey_test(J); - _cimg_iskey_test(K); _cimg_iskey_test(L); _cimg_iskey_test(ENTER); - _cimg_iskey_test(SHIFTLEFT); _cimg_iskey_test(Z); _cimg_iskey_test(X); _cimg_iskey_test(C); - _cimg_iskey_test(V); _cimg_iskey_test(B); _cimg_iskey_test(N); _cimg_iskey_test(M); - _cimg_iskey_test(SHIFTRIGHT); _cimg_iskey_test(ARROWUP); _cimg_iskey_test(CTRLLEFT); - _cimg_iskey_test(APPLEFT); _cimg_iskey_test(ALT); _cimg_iskey_test(SPACE); _cimg_iskey_test(ALTGR); - _cimg_iskey_test(APPRIGHT); _cimg_iskey_test(MENU); _cimg_iskey_test(CTRLRIGHT); - _cimg_iskey_test(ARROWLEFT); _cimg_iskey_test(ARROWDOWN); _cimg_iskey_test(ARROWRIGHT); - _cimg_iskey_test(PAD0); _cimg_iskey_test(PAD1); _cimg_iskey_test(PAD2); - _cimg_iskey_test(PAD3); _cimg_iskey_test(PAD4); _cimg_iskey_test(PAD5); - _cimg_iskey_test(PAD6); _cimg_iskey_test(PAD7); _cimg_iskey_test(PAD8); - _cimg_iskey_test(PAD9); _cimg_iskey_test(PADADD); _cimg_iskey_test(PADSUB); - _cimg_iskey_test(PADMUL); _cimg_iskey_test(PADDIV); - return false; - } - - //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise. - /** - \param keycode C-string containing the keycode label of the key to test. - \note Use it when the key you want to test can be dynamically set by the user. - \par Example - \code - CImgDisplay disp(400,400); - const char *const keycode = "TAB"; - while (!disp.is_closed()) { - if (disp.is_key(keycode)) { ... } // Equivalent to 'if (disp.is_keyTAB())'. - disp.wait(); - } - \endcode - **/ - volatile bool& is_key(const char *const keycode) { - static bool f = false; - f = false; -#define _cimg_iskey_test2(k) if (!cimg::strcasecmp(keycode,#k)) return _is_key##k; - _cimg_iskey_test2(ESC); _cimg_iskey_test2(F1); _cimg_iskey_test2(F2); _cimg_iskey_test2(F3); - _cimg_iskey_test2(F4); _cimg_iskey_test2(F5); _cimg_iskey_test2(F6); _cimg_iskey_test2(F7); - _cimg_iskey_test2(F8); _cimg_iskey_test2(F9); _cimg_iskey_test2(F10); _cimg_iskey_test2(F11); - _cimg_iskey_test2(F12); _cimg_iskey_test2(PAUSE); _cimg_iskey_test2(1); _cimg_iskey_test2(2); - _cimg_iskey_test2(3); _cimg_iskey_test2(4); _cimg_iskey_test2(5); _cimg_iskey_test2(6); - _cimg_iskey_test2(7); _cimg_iskey_test2(8); _cimg_iskey_test2(9); _cimg_iskey_test2(0); - _cimg_iskey_test2(BACKSPACE); _cimg_iskey_test2(INSERT); _cimg_iskey_test2(HOME); - _cimg_iskey_test2(PAGEUP); _cimg_iskey_test2(TAB); _cimg_iskey_test2(Q); _cimg_iskey_test2(W); - _cimg_iskey_test2(E); _cimg_iskey_test2(R); _cimg_iskey_test2(T); _cimg_iskey_test2(Y); - _cimg_iskey_test2(U); _cimg_iskey_test2(I); _cimg_iskey_test2(O); _cimg_iskey_test2(P); - _cimg_iskey_test2(DELETE); _cimg_iskey_test2(END); _cimg_iskey_test2(PAGEDOWN); - _cimg_iskey_test2(CAPSLOCK); _cimg_iskey_test2(A); _cimg_iskey_test2(S); _cimg_iskey_test2(D); - _cimg_iskey_test2(F); _cimg_iskey_test2(G); _cimg_iskey_test2(H); _cimg_iskey_test2(J); - _cimg_iskey_test2(K); _cimg_iskey_test2(L); _cimg_iskey_test2(ENTER); - _cimg_iskey_test2(SHIFTLEFT); _cimg_iskey_test2(Z); _cimg_iskey_test2(X); _cimg_iskey_test2(C); - _cimg_iskey_test2(V); _cimg_iskey_test2(B); _cimg_iskey_test2(N); _cimg_iskey_test2(M); - _cimg_iskey_test2(SHIFTRIGHT); _cimg_iskey_test2(ARROWUP); _cimg_iskey_test2(CTRLLEFT); - _cimg_iskey_test2(APPLEFT); _cimg_iskey_test2(ALT); _cimg_iskey_test2(SPACE); _cimg_iskey_test2(ALTGR); - _cimg_iskey_test2(APPRIGHT); _cimg_iskey_test2(MENU); _cimg_iskey_test2(CTRLRIGHT); - _cimg_iskey_test2(ARROWLEFT); _cimg_iskey_test2(ARROWDOWN); _cimg_iskey_test2(ARROWRIGHT); - _cimg_iskey_test2(PAD0); _cimg_iskey_test2(PAD1); _cimg_iskey_test2(PAD2); - _cimg_iskey_test2(PAD3); _cimg_iskey_test2(PAD4); _cimg_iskey_test2(PAD5); - _cimg_iskey_test2(PAD6); _cimg_iskey_test2(PAD7); _cimg_iskey_test2(PAD8); - _cimg_iskey_test2(PAD9); _cimg_iskey_test2(PADADD); _cimg_iskey_test2(PADSUB); - _cimg_iskey_test2(PADMUL); _cimg_iskey_test2(PADDIV); - return f; - } - - //! Return \c true if specified key sequence has been typed on the associated window, \c false otherwise. - /** - \param keycodes_sequence Buffer of keycodes to test. - \param length Number of keys in the \c keycodes_sequence buffer. - \param remove_sequence Tells if the key sequence must be removed from the key history, if found. - \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure - your code stay portable (see cimg::keyESC). - \par Example - \code - CImgDisplay disp(400,400); - const unsigned int key_seq[] = { cimg::keyCTRLLEFT, cimg::keyD }; - while (!disp.is_closed()) { - if (disp.is_key_sequence(key_seq,2)) { ... } // Test for the 'CTRL+D' keyboard event. - disp.wait(); - } - \endcode - **/ - bool is_key_sequence(const unsigned int *const keycodes_sequence, const unsigned int length, - const bool remove_sequence=false) { - if (keycodes_sequence && length) { - const unsigned int - *const ps_end = keycodes_sequence + length - 1, - *const pk_end = (unsigned int*)_keys + 1 + sizeof(_keys)/sizeof(unsigned int) - length, - k = *ps_end; - for (unsigned int *pk = (unsigned int*)_keys; pk[0,255]. - If the range of values of the data to display is different, a normalization may be required for displaying - the data in a correct way. The normalization type can be one of: - - \c 0: Value normalization is disabled. It is then assumed that all input data to be displayed by the - CImgDisplay instance have values in range [0,255]. - - \c 1: Value normalization is always performed (this is the default behavior). - Before displaying an input image, its values will be (virtually) stretched - in range [0,255], so that the contrast of the displayed pixels will be maximum. - Use this mode for images whose minimum and maximum values are not prescribed to known values - (e.g. float-valued images). - Note that when normalized versions of images are computed for display purposes, the actual values of these - images are not modified. - - \c 2: Value normalization is performed once (on the first image display), then the same normalization - coefficients are kept for next displayed frames. - - \c 3: Value normalization depends on the pixel type of the data to display. For integer pixel types, - the normalization is done regarding the minimum/maximum values of the type (no normalization occurs then - for unsigned char). - For float-valued pixel types, the normalization is done regarding the minimum/maximum value of the image - data instead. - **/ - unsigned int normalization() const { - return _normalization; - } - - //! Return title of the associated window as a C-string. - /** - \note Window title may be not visible, depending on the used window manager or if the current display is - in fullscreen mode. - **/ - const char *title() const { - return _title; - } - - //! Return width of the associated window. - /** - \note The width of the display (i.e. the width of the pixel data buffer associated to the CImgDisplay instance) - may be different from the actual width of the associated window. - **/ - int window_width() const { - return (int)_window_width; - } - - //! Return height of the associated window. - /** - \note The height of the display (i.e. the height of the pixel data buffer associated to the CImgDisplay instance) - may be different from the actual height of the associated window. - **/ - int window_height() const { - return (int)_window_height; - } - - //! Return X-coordinate of the associated window. - /** - \note The returned coordinate corresponds to the location of the upper-left corner of the associated window. - **/ - int window_x() const { - return _window_x; - } - - //! Return Y-coordinate of the associated window. - /** - \note The returned coordinate corresponds to the location of the upper-left corner of the associated window. - **/ - int window_y() const { - return _window_y; - } - - //! Return X-coordinate of the mouse pointer. - /** - \note - - If the mouse pointer is outside window area, \c -1 is returned. - - Otherwise, the returned value is in the range [0,width()-1]. - **/ - int mouse_x() const { - return _mouse_x; - } - - //! Return Y-coordinate of the mouse pointer. - /** - \note - - If the mouse pointer is outside window area, \c -1 is returned. - - Otherwise, the returned value is in the range [0,height()-1]. - **/ - int mouse_y() const { - return _mouse_y; - } - - //! Return current state of the mouse buttons. - /** - \note Three mouse buttons can be managed. If one button is pressed, its corresponding bit in the returned - value is set: - - bit \c 0 (value \c 0x1): State of the left mouse button. - - bit \c 1 (value \c 0x2): State of the right mouse button. - - bit \c 2 (value \c 0x4): State of the middle mouse button. - - Several bits can be activated if more than one button are pressed at the same time. - \par Example - \code - CImgDisplay disp(400,400); - while (!disp.is_closed()) { - if (disp.button()&1) { // Left button clicked. - ... - } - if (disp.button()&2) { // Right button clicked. - ... - } - if (disp.button()&4) { // Middle button clicked. - ... - } - disp.wait(); - } - \endcode - **/ - unsigned int button() const { - return _button; - } - - //! Return current state of the mouse wheel. - /** - \note - - The returned value can be positive or negative depending on whether the mouse wheel has been scrolled - forward or backward. - - Scrolling the wheel forward add \c 1 to the wheel value. - - Scrolling the wheel backward substract \c 1 to the wheel value. - - The returned value cumulates the number of forward of backward scrolls since the creation of the display, - or since the last reset of the wheel value (using set_wheel()). It is strongly recommended to quickly reset - the wheel counter when an action has been performed regarding the current wheel value. - Otherwise, the returned wheel value may be for instance \c 0 despite the fact that many scrolls have been done - (as many in forward as in backward directions). - \par Example - \code - CImgDisplay disp(400,400); - while (!disp.is_closed()) { - if (disp.wheel()) { - int counter = disp.wheel(); // Read the state of the mouse wheel. - ... // Do what you want with 'counter'. - disp.set_wheel(); // Reset the wheel value to 0. - } - disp.wait(); - } - \endcode - **/ - int wheel() const { - return _wheel; - } - - //! Return one entry from the pressed keys history. - /** - \param pos Indice to read from the pressed keys history (indice \c 0 corresponds to latest entry). - \return Keycode of a pressed key or \c 0 for a released key. - \note - - Each CImgDisplay stores a history of the pressed keys in a buffer of size \c 128. When a new key is pressed, - its keycode is stored in the pressed keys history. When a key is released, \c 0 is put instead. - This means that up to the 64 last pressed keys may be read from the pressed keys history. - When a new value is stored, the pressed keys history is shifted so that the latest entry is always - stored at position \c 0. - - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure - your code stay portable (see cimg::keyESC). - **/ - unsigned int key(const unsigned int pos=0) const { - return pos<(sizeof(_keys)/sizeof(unsigned int))?_keys[pos]:0; - } - - //! Return one entry from the released keys history. - /** - \param pos Indice to read from the released keys history (indice \c 0 corresponds to latest entry). - \return Keycode of a released key or \c 0 for a pressed key. - \note - - Each CImgDisplay stores a history of the released keys in a buffer of size \c 128. When a new key is released, - its keycode is stored in the pressed keys history. When a key is pressed, \c 0 is put instead. - This means that up to the 64 last released keys may be read from the released keys history. - When a new value is stored, the released keys history is shifted so that the latest entry is always - stored at position \c 0. - - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure - your code stay portable (see cimg::keyESC). - **/ - unsigned int released_key(const unsigned int pos=0) const { - return pos<(sizeof(_released_keys)/sizeof(unsigned int))?_released_keys[pos]:0; - } - - //! Return keycode corresponding to the specified string. - /** - \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure - your code stay portable (see cimg::keyESC). - \par Example - \code - const unsigned int keyTAB = CImgDisplay::keycode("TAB"); // Return cimg::keyTAB. - \endcode - **/ - static unsigned int keycode(const char *const keycode) { -#define _cimg_keycode(k) if (!cimg::strcasecmp(keycode,#k)) return cimg::key##k; - _cimg_keycode(ESC); _cimg_keycode(F1); _cimg_keycode(F2); _cimg_keycode(F3); - _cimg_keycode(F4); _cimg_keycode(F5); _cimg_keycode(F6); _cimg_keycode(F7); - _cimg_keycode(F8); _cimg_keycode(F9); _cimg_keycode(F10); _cimg_keycode(F11); - _cimg_keycode(F12); _cimg_keycode(PAUSE); _cimg_keycode(1); _cimg_keycode(2); - _cimg_keycode(3); _cimg_keycode(4); _cimg_keycode(5); _cimg_keycode(6); - _cimg_keycode(7); _cimg_keycode(8); _cimg_keycode(9); _cimg_keycode(0); - _cimg_keycode(BACKSPACE); _cimg_keycode(INSERT); _cimg_keycode(HOME); - _cimg_keycode(PAGEUP); _cimg_keycode(TAB); _cimg_keycode(Q); _cimg_keycode(W); - _cimg_keycode(E); _cimg_keycode(R); _cimg_keycode(T); _cimg_keycode(Y); - _cimg_keycode(U); _cimg_keycode(I); _cimg_keycode(O); _cimg_keycode(P); - _cimg_keycode(DELETE); _cimg_keycode(END); _cimg_keycode(PAGEDOWN); - _cimg_keycode(CAPSLOCK); _cimg_keycode(A); _cimg_keycode(S); _cimg_keycode(D); - _cimg_keycode(F); _cimg_keycode(G); _cimg_keycode(H); _cimg_keycode(J); - _cimg_keycode(K); _cimg_keycode(L); _cimg_keycode(ENTER); - _cimg_keycode(SHIFTLEFT); _cimg_keycode(Z); _cimg_keycode(X); _cimg_keycode(C); - _cimg_keycode(V); _cimg_keycode(B); _cimg_keycode(N); _cimg_keycode(M); - _cimg_keycode(SHIFTRIGHT); _cimg_keycode(ARROWUP); _cimg_keycode(CTRLLEFT); - _cimg_keycode(APPLEFT); _cimg_keycode(ALT); _cimg_keycode(SPACE); _cimg_keycode(ALTGR); - _cimg_keycode(APPRIGHT); _cimg_keycode(MENU); _cimg_keycode(CTRLRIGHT); - _cimg_keycode(ARROWLEFT); _cimg_keycode(ARROWDOWN); _cimg_keycode(ARROWRIGHT); - _cimg_keycode(PAD0); _cimg_keycode(PAD1); _cimg_keycode(PAD2); - _cimg_keycode(PAD3); _cimg_keycode(PAD4); _cimg_keycode(PAD5); - _cimg_keycode(PAD6); _cimg_keycode(PAD7); _cimg_keycode(PAD8); - _cimg_keycode(PAD9); _cimg_keycode(PADADD); _cimg_keycode(PADSUB); - _cimg_keycode(PADMUL); _cimg_keycode(PADDIV); - return 0; - } - - //! Return the current refresh rate, in frames per second. - /** - \note Returns a significant value when the current instance is used to display successive frames. - It measures the delay between successive calls to frames_per_second(). - **/ - float frames_per_second() { - if (!_fps_timer) _fps_timer = cimg::time(); - const float delta = (cimg::time()-_fps_timer)/1000.0f; - ++_fps_frames; - if (delta>=1) { - _fps_fps = _fps_frames/delta; - _fps_frames = 0; - _fps_timer = cimg::time(); - } - return _fps_fps; - } - - //@} - //--------------------------------------- - // - //! \name Window Manipulation - //@{ - //--------------------------------------- - -#if cimg_display==0 - - //! Display image on associated window. - /** - \param img Input image to display. - \note This method returns immediately. - **/ - template - CImgDisplay& display(const CImg& img) { - return assign(img); - } - -#endif - - //! Display list of images on associated window. - /** - \param list List of images to display. - \param axis Axis used to append the images along, for the visualization (can be \c x, \c y, \c z or \c c). - \param align Relative position of aligned images when displaying lists with images of different sizes - (\c 0 for upper-left, \c 0.5 for centering and \c 1 for lower-right). - \note This method returns immediately. - **/ - template - CImgDisplay& display(const CImgList& list, const char axis='x', const float align=0) { - CImgList::ucharT> visu(list._width); - cimglist_for(list,l) { - const CImg& img = list._data[l]; - img.__get_select(*this,_normalization,(img._width-1)/2,(img._height-1)/2,(img._depth-1)/2).move_to(visu[l]); - } - visu.get_append(axis,align).display(*this); - return *this; - } - -#if cimg_display==0 - - //! Show (closed) associated window on the screen. - /** - \note - - Force the associated window of a display to be visible on the screen, even if it has been closed before. - - Using show() on a visible display does nothing. - **/ - CImgDisplay& show() { - return assign(); - } - - //! Close (visible) associated window and make it disappear from the screen. - /** - \note - - A closed display only means the associated window is not visible anymore. This does not mean the display has - been destroyed. - Use show() to make the associated window reappear. - - Using close() on a closed display does nothing. - **/ - CImgDisplay& close() { - return assign(); - } - - //! Move associated window to a new location. - /** - \param pos_x X-coordinate of the new window location. - \param pos_y Y-coordinate of the new window location. - \note Depending on the window manager behavior, this method may not succeed (no exceptions are thrown - nevertheless). - **/ - CImgDisplay& move(const int pos_x, const int pos_y) { - return assign(pos_x,pos_y); - } - -#endif - - //! Resize display to the size of the associated window. - /** - \param force_redraw Tells if the previous window content must be updated and refreshed as well. - \note - - Calling this method ensures that width() and window_width() become equal, as well as height() and - window_height(). - - The associated window is also resized to specified dimensions. - **/ - CImgDisplay& resize(const bool force_redraw=true) { - resize(_window_width,_window_height,force_redraw); - return *this; - } - -#if cimg_display==0 - - //! Resize display to the specified size. - /** - \param width Requested display width. - \param height Requested display height. - \param force_redraw Tells if the previous window content must be updated and refreshed as well. - \note The associated window is also resized to specified dimensions. - **/ - CImgDisplay& resize(const int width, const int height, const bool force_redraw=true) { - return assign(width,height,0,3,force_redraw); - } - -#endif - - //! Resize display to the size of an input image. - /** - \param img Input image to take size from. - \param force_redraw Tells if the previous window content must be resized and updated as well. - \note - - Calling this method ensures that width() and img.width() become equal, as well as height() and - img.height(). - - The associated window is also resized to specified dimensions. - **/ - template - CImgDisplay& resize(const CImg& img, const bool force_redraw=true) { - return resize(img._width,img._height,force_redraw); - } - - //! Resize display to the size of another CImgDisplay instance. - /** - \param disp Input display to take size from. - \param force_redraw Tells if the previous window content must be resized and updated as well. - \note - - Calling this method ensures that width() and disp.width() become equal, as well as height() and - disp.height(). - - The associated window is also resized to specified dimensions. - **/ - CImgDisplay& resize(const CImgDisplay& disp, const bool force_redraw=true) { - return resize(disp._width,disp._height,force_redraw); - } - - // [internal] Render pixel buffer with size (wd,hd) from source buffer of size (ws,hs). - template - static void _render_resize(const T *ptrs, const unsigned int ws, const unsigned int hs, - t *ptrd, const unsigned int wd, const unsigned int hd) { - unsigned int *const offx = new unsigned int[wd], *const offy = new unsigned int[hd+1], *poffx, *poffy; - float s, curr, old; - s = (float)ws/wd; - poffx = offx; curr = 0; for (unsigned int x = 0; xstd::printf(). - \warning As the first argument is a format string, it is highly recommended to write - \code - disp.set_title("%s",window_title); - \endcode - instead of - \code - disp.set_title(window_title); - \endcode - if \c window_title can be arbitrary, to prevent nasty memory access. - **/ - CImgDisplay& set_title(const char *const format, ...) { - return assign(0,0,format); - } - -#endif - - //! Enable or disable fullscreen mode. - /** - \param is_fullscreen Tells is the fullscreen mode must be activated or not. - \param force_redraw Tells if the previous window content must be displayed as well. - \note - - When the fullscreen mode is enabled, the associated window fills the entire screen but the size of the - current display is not modified. - - The screen resolution may be switched to fit the associated window size and ensure it appears the largest - as possible. - For X-Window (X11) users, the configuration flag \c cimg_use_xrandr has to be set to allow the screen - resolution change (requires the X11 extensions to be enabled). - **/ - CImgDisplay& set_fullscreen(const bool is_fullscreen, const bool force_redraw=true) { - if (is_empty() || _is_fullscreen==is_fullscreen) return *this; - return toggle_fullscreen(force_redraw); - } - -#if cimg_display==0 - - //! Toggle fullscreen mode. - /** - \param force_redraw Tells if the previous window content must be displayed as well. - \note Enable fullscreen mode if it was not enabled, and disable it otherwise. - **/ - CImgDisplay& toggle_fullscreen(const bool force_redraw=true) { - return assign(_width,_height,0,3,force_redraw); - } - - //! Show mouse pointer. - /** - \note Depending on the window manager behavior, this method may not succeed - (no exceptions are thrown nevertheless). - **/ - CImgDisplay& show_mouse() { - return assign(); - } - - //! Hide mouse pointer. - /** - \note Depending on the window manager behavior, this method may not succeed - (no exceptions are thrown nevertheless). - **/ - CImgDisplay& hide_mouse() { - return assign(); - } - - //! Move mouse pointer to a specified location. - /** - \note Depending on the window manager behavior, this method may not succeed - (no exceptions are thrown nevertheless). - **/ - CImgDisplay& set_mouse(const int pos_x, const int pos_y) { - return assign(pos_x,pos_y); - } - -#endif - - //! Simulate a mouse button release event. - /** - \note All mouse buttons are considered released at the same time. - **/ - CImgDisplay& set_button() { - _button = 0; - _is_event = true; -#if cimg_display==1 - pthread_cond_broadcast(&cimg::X11_attr().wait_event); -#elif cimg_display==2 - SetEvent(cimg::Win32_attr().wait_event); -#endif - return *this; - } - - //! Simulate a mouse button press or release event. - /** - \param button Buttons event code, where each button is associated to a single bit. - \param is_pressed Tells if the mouse button is considered as pressed or released. - **/ - CImgDisplay& set_button(const unsigned int button, const bool is_pressed=true) { - const unsigned int buttoncode = button==1?1:button==2?2:button==3?4:0; - if (is_pressed) _button |= buttoncode; else _button &= ~buttoncode; - _is_event = buttoncode?true:false; - if (buttoncode) { -#if cimg_display==1 - pthread_cond_broadcast(&cimg::X11_attr().wait_event); -#elif cimg_display==2 - SetEvent(cimg::Win32_attr().wait_event); -#endif - } - return *this; - } - - //! Flush all mouse wheel events. - /** - \note Make wheel() to return \c 0, if called afterwards. - **/ - CImgDisplay& set_wheel() { - _wheel = 0; - _is_event = true; -#if cimg_display==1 - pthread_cond_broadcast(&cimg::X11_attr().wait_event); -#elif cimg_display==2 - SetEvent(cimg::Win32_attr().wait_event); -#endif - return *this; - } - - //! Simulate a wheel event. - /** - \param amplitude Amplitude of the wheel scrolling to simulate. - \note Make wheel() to return \c amplitude, if called afterwards. - **/ - CImgDisplay& set_wheel(const int amplitude) { - _wheel+=amplitude; - _is_event = amplitude?true:false; - if (amplitude) { -#if cimg_display==1 - pthread_cond_broadcast(&cimg::X11_attr().wait_event); -#elif cimg_display==2 - SetEvent(cimg::Win32_attr().wait_event); -#endif - } - return *this; - } - - //! Flush all key events. - /** - \note Make key() to return \c 0, if called afterwards. - **/ - CImgDisplay& set_key() { - std::memset((void*)_keys,0,sizeof(_keys)); - std::memset((void*)_released_keys,0,sizeof(_released_keys)); - _is_keyESC = _is_keyF1 = _is_keyF2 = _is_keyF3 = _is_keyF4 = _is_keyF5 = _is_keyF6 = _is_keyF7 = _is_keyF8 = - _is_keyF9 = _is_keyF10 = _is_keyF11 = _is_keyF12 = _is_keyPAUSE = _is_key1 = _is_key2 = _is_key3 = _is_key4 = - _is_key5 = _is_key6 = _is_key7 = _is_key8 = _is_key9 = _is_key0 = _is_keyBACKSPACE = _is_keyINSERT = - _is_keyHOME = _is_keyPAGEUP = _is_keyTAB = _is_keyQ = _is_keyW = _is_keyE = _is_keyR = _is_keyT = _is_keyY = - _is_keyU = _is_keyI = _is_keyO = _is_keyP = _is_keyDELETE = _is_keyEND = _is_keyPAGEDOWN = _is_keyCAPSLOCK = - _is_keyA = _is_keyS = _is_keyD = _is_keyF = _is_keyG = _is_keyH = _is_keyJ = _is_keyK = _is_keyL = - _is_keyENTER = _is_keySHIFTLEFT = _is_keyZ = _is_keyX = _is_keyC = _is_keyV = _is_keyB = _is_keyN = - _is_keyM = _is_keySHIFTRIGHT = _is_keyARROWUP = _is_keyCTRLLEFT = _is_keyAPPLEFT = _is_keyALT = _is_keySPACE = - _is_keyALTGR = _is_keyAPPRIGHT = _is_keyMENU = _is_keyCTRLRIGHT = _is_keyARROWLEFT = _is_keyARROWDOWN = - _is_keyARROWRIGHT = _is_keyPAD0 = _is_keyPAD1 = _is_keyPAD2 = _is_keyPAD3 = _is_keyPAD4 = _is_keyPAD5 = - _is_keyPAD6 = _is_keyPAD7 = _is_keyPAD8 = _is_keyPAD9 = _is_keyPADADD = _is_keyPADSUB = _is_keyPADMUL = - _is_keyPADDIV = false; - _is_event = true; -#if cimg_display==1 - pthread_cond_broadcast(&cimg::X11_attr().wait_event); -#elif cimg_display==2 - SetEvent(cimg::Win32_attr().wait_event); -#endif - return *this; - } - - //! Simulate a keyboard press/release event. - /** - \param keycode Keycode of the associated key. - \param is_pressed Tells if the key is considered as pressed or released. - \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure - your code stay portable (see cimg::keyESC). - **/ - CImgDisplay& set_key(const unsigned int keycode, const bool is_pressed=true) { -#define _cimg_set_key(k) if (keycode==cimg::key##k) _is_key##k = is_pressed; - _cimg_set_key(ESC); _cimg_set_key(F1); _cimg_set_key(F2); _cimg_set_key(F3); - _cimg_set_key(F4); _cimg_set_key(F5); _cimg_set_key(F6); _cimg_set_key(F7); - _cimg_set_key(F8); _cimg_set_key(F9); _cimg_set_key(F10); _cimg_set_key(F11); - _cimg_set_key(F12); _cimg_set_key(PAUSE); _cimg_set_key(1); _cimg_set_key(2); - _cimg_set_key(3); _cimg_set_key(4); _cimg_set_key(5); _cimg_set_key(6); - _cimg_set_key(7); _cimg_set_key(8); _cimg_set_key(9); _cimg_set_key(0); - _cimg_set_key(BACKSPACE); _cimg_set_key(INSERT); _cimg_set_key(HOME); - _cimg_set_key(PAGEUP); _cimg_set_key(TAB); _cimg_set_key(Q); _cimg_set_key(W); - _cimg_set_key(E); _cimg_set_key(R); _cimg_set_key(T); _cimg_set_key(Y); - _cimg_set_key(U); _cimg_set_key(I); _cimg_set_key(O); _cimg_set_key(P); - _cimg_set_key(DELETE); _cimg_set_key(END); _cimg_set_key(PAGEDOWN); - _cimg_set_key(CAPSLOCK); _cimg_set_key(A); _cimg_set_key(S); _cimg_set_key(D); - _cimg_set_key(F); _cimg_set_key(G); _cimg_set_key(H); _cimg_set_key(J); - _cimg_set_key(K); _cimg_set_key(L); _cimg_set_key(ENTER); - _cimg_set_key(SHIFTLEFT); _cimg_set_key(Z); _cimg_set_key(X); _cimg_set_key(C); - _cimg_set_key(V); _cimg_set_key(B); _cimg_set_key(N); _cimg_set_key(M); - _cimg_set_key(SHIFTRIGHT); _cimg_set_key(ARROWUP); _cimg_set_key(CTRLLEFT); - _cimg_set_key(APPLEFT); _cimg_set_key(ALT); _cimg_set_key(SPACE); _cimg_set_key(ALTGR); - _cimg_set_key(APPRIGHT); _cimg_set_key(MENU); _cimg_set_key(CTRLRIGHT); - _cimg_set_key(ARROWLEFT); _cimg_set_key(ARROWDOWN); _cimg_set_key(ARROWRIGHT); - _cimg_set_key(PAD0); _cimg_set_key(PAD1); _cimg_set_key(PAD2); - _cimg_set_key(PAD3); _cimg_set_key(PAD4); _cimg_set_key(PAD5); - _cimg_set_key(PAD6); _cimg_set_key(PAD7); _cimg_set_key(PAD8); - _cimg_set_key(PAD9); _cimg_set_key(PADADD); _cimg_set_key(PADSUB); - _cimg_set_key(PADMUL); _cimg_set_key(PADDIV); - if (is_pressed) { - if (*_keys) - std::memmove((void*)(_keys+1),(void*)_keys,sizeof(_keys) - sizeof(unsigned int)); - *_keys = keycode; - if (*_released_keys) { - std::memmove((void*)(_released_keys+1),(void*)_released_keys,sizeof(_released_keys) - sizeof(unsigned int)); - *_released_keys = 0; - } - } else { - if (*_keys) { - std::memmove((void*)(_keys+1),(void*)_keys,sizeof(_keys) - sizeof(unsigned int)); - *_keys = 0; - } - if (*_released_keys) - std::memmove((void*)(_released_keys+1),(void*)_released_keys,sizeof(_released_keys) - sizeof(unsigned int)); - *_released_keys = keycode; - } - _is_event = keycode?true:false; - if (keycode) { -#if cimg_display==1 - pthread_cond_broadcast(&cimg::X11_attr().wait_event); -#elif cimg_display==2 - SetEvent(cimg::Win32_attr().wait_event); -#endif - } - return *this; - } - - //! Flush all display events. - /** - \note Remove all passed events from the current display. - **/ - CImgDisplay& flush() { - set_key().set_button().set_wheel(); - _is_resized = _is_moved = _is_event = false; - _fps_timer = _fps_frames = _timer = 0; - _fps_fps = 0; - return *this; - } - - //! Wait for any user event occuring on the current display. - CImgDisplay& wait() { - wait(*this); - return *this; - } - - //! Wait for a given number of milliseconds since the last call to wait(). - /** - \param milliseconds Number of milliseconds to wait for. - \note Similar to cimg::wait(). - **/ - CImgDisplay& wait(const unsigned int milliseconds) { - cimg::_wait(milliseconds,_timer); - return *this; - } - - //! Wait for any event occuring on the display \c disp1. - static void wait(CImgDisplay& disp1) { - disp1._is_event = false; - while (!disp1._is_closed && !disp1._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1 or \c disp2. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2) { - disp1._is_event = disp2._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed) && - !disp1._is_event && !disp2._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2 or \c disp3. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3) { - disp1._is_event = disp2._is_event = disp3._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3 or \c disp4. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4 or \c disp5. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, - CImgDisplay& disp5) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event) - wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp6. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, - CImgDisplay& disp6) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = - disp6._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || - !disp6._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && - !disp6._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp7. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, - CImgDisplay& disp6, CImgDisplay& disp7) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = - disp6._is_event = disp7._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || - !disp6._is_closed || !disp7._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && - !disp6._is_event && !disp7._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp8. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, - CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = - disp6._is_event = disp7._is_event = disp8._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || - !disp6._is_closed || !disp7._is_closed || !disp8._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && - !disp6._is_event && !disp7._is_event && !disp8._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp9. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, - CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = - disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || - !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && - !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event) wait_all(); - } - - //! Wait for any event occuring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp10. - static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5, - CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9, - CImgDisplay& disp10) { - disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = - disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = disp10._is_event = false; - while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed || - !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed || !disp10._is_closed) && - !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event && - !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event && !disp10._is_event) - wait_all(); - } - -#if cimg_display==0 - - //! Wait for any window event occuring in any opened CImgDisplay. - static void wait_all() { - return _no_display_exception(); - } - - //! Render image into internal display buffer. - /** - \param img Input image data to render. - \note - - Convert image data representation into the internal display buffer (architecture-dependent structure). - - The content of the associated window is not modified, until paint() is called. - - Should not be used for common CImgDisplay uses, since display() is more useful. - **/ - template - CImgDisplay& render(const CImg& img) { - return assign(img); - } - - //! Paint internal display buffer on associated window. - /** - \note - - Update the content of the associated window with the internal display buffer, e.g. after a render() call. - - Should not be used for common CImgDisplay uses, since display() is more useful. - **/ - CImgDisplay& paint() { - return assign(); - } - - //! Take a snapshot of the associated window content. - /** - \param[out] img Output snapshot. Can be empty on input. - **/ - template - const CImgDisplay& snapshot(CImg& img) const { - cimg::unused(img); - _no_display_exception(); - return *this; - } -#endif - - // X11-based implementation - //-------------------------- -#if cimg_display==1 - - Atom _wm_window_atom, _wm_protocol_atom; - Window _window, _background_window; - Colormap _colormap; - XImage *_image; - void *_data; -#ifdef cimg_use_xshm - XShmSegmentInfo *_shminfo; -#endif - - static int screen_width() { - Display *const dpy = cimg::X11_attr().display; - int res = 0; - if (!dpy) { - Display *const _dpy = XOpenDisplay(0); - if (!_dpy) - throw CImgDisplayException("CImgDisplay::screen_width(): Failed to open X11 display."); - res = DisplayWidth(_dpy,DefaultScreen(_dpy)); - XCloseDisplay(_dpy); - } else { -#ifdef cimg_use_xrandr - if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) - res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width; - else res = DisplayWidth(dpy,DefaultScreen(dpy)); -#else - res = DisplayWidth(dpy,DefaultScreen(dpy)); -#endif - } - return res; - } - - static int screen_height() { - Display *const dpy = cimg::X11_attr().display; - int res = 0; - if (!dpy) { - Display *const _dpy = XOpenDisplay(0); - if (!_dpy) - throw CImgDisplayException("CImgDisplay::screen_height(): Failed to open X11 display."); - res = DisplayHeight(_dpy,DefaultScreen(_dpy)); - XCloseDisplay(_dpy); - } else { -#ifdef cimg_use_xrandr - if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) - res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height; - else res = DisplayHeight(dpy,DefaultScreen(dpy)); -#else - res = DisplayHeight(dpy,DefaultScreen(dpy)); -#endif - } - return res; - } - - static void wait_all() { - if (!cimg::X11_attr().display) return; - if (cimg::mutex(13,2)) { cimg::sleep(10); return; } - pthread_cond_wait(&cimg::X11_attr().wait_event,&cimg::X11_attr().wait_event_mutex); - cimg::mutex(13,0); - } - - void _handle_events(const XEvent *const pevent) { - Display *const dpy = cimg::X11_attr().display; - XEvent event = *pevent; - switch (event.type) { - case ClientMessage : { - if ((int)event.xclient.message_type==(int)_wm_protocol_atom && - (int)event.xclient.data.l[0]==(int)_wm_window_atom) { - XUnmapWindow(cimg::X11_attr().display,_window); - _is_closed = _is_event = true; - pthread_cond_broadcast(&cimg::X11_attr().wait_event); - } - } break; - case ConfigureNotify : { - while (XCheckWindowEvent(dpy,_window,StructureNotifyMask,&event)) {} - const unsigned int nw = event.xconfigure.width, nh = event.xconfigure.height; - const int nx = event.xconfigure.x, ny = event.xconfigure.y; - if (nw && nh && (nw!=_window_width || nh!=_window_height)) { - _window_width = nw; _window_height = nh; _mouse_x = _mouse_y = -1; - XResizeWindow(dpy,_window,_window_width,_window_height); - _is_resized = _is_event = true; - pthread_cond_broadcast(&cimg::X11_attr().wait_event); - } - if (nx!=_window_x || ny!=_window_y) { - _window_x = nx; _window_y = ny; _is_moved = _is_event = true; - pthread_cond_broadcast(&cimg::X11_attr().wait_event); - } - } break; - case Expose : { - while (XCheckWindowEvent(dpy,_window,ExposureMask,&event)) {} - _paint(false); - if (_is_fullscreen) { - XWindowAttributes attr; - XGetWindowAttributes(dpy,_window,&attr); - while (attr.map_state!=IsViewable) XSync(dpy,0); - XSetInputFocus(dpy,_window,RevertToParent,CurrentTime); - } - } break; - case ButtonPress : { - do { - _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y; - if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; - switch (event.xbutton.button) { - case 1 : set_button(1); break; - case 3 : set_button(2); break; - case 2 : set_button(3); break; - } - } while (XCheckWindowEvent(dpy,_window,ButtonPressMask,&event)); - } break; - case ButtonRelease : { - do { - _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y; - if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; - switch (event.xbutton.button) { - case 1 : set_button(1,false); break; - case 3 : set_button(2,false); break; - case 2 : set_button(3,false); break; - case 4 : set_wheel(1); break; - case 5 : set_wheel(-1); break; - } - } while (XCheckWindowEvent(dpy,_window,ButtonReleaseMask,&event)); - } break; - case KeyPress : { - char tmp = 0; KeySym ksym; - XLookupString(&event.xkey,&tmp,1,&ksym,0); - set_key((unsigned int)ksym,true); - } break; - case KeyRelease : { - char keys_return[32]; // Check that the key has been physically unpressed. - XQueryKeymap(dpy,keys_return); - const unsigned int kc = event.xkey.keycode, kc1 = kc/8, kc2 = kc%8; - const bool is_key_pressed = kc1>=32?false:(keys_return[kc1]>>kc2)&1; - if (!is_key_pressed) { - char tmp = 0; KeySym ksym; - XLookupString(&event.xkey,&tmp,1,&ksym,0); - set_key((unsigned int)ksym,false); - } - } break; - case EnterNotify: { - while (XCheckWindowEvent(dpy,_window,EnterWindowMask,&event)) {} - _mouse_x = event.xmotion.x; - _mouse_y = event.xmotion.y; - if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; - } break; - case LeaveNotify : { - while (XCheckWindowEvent(dpy,_window,LeaveWindowMask,&event)) {} - _mouse_x = _mouse_y =-1; _is_event = true; - pthread_cond_broadcast(&cimg::X11_attr().wait_event); - } break; - case MotionNotify : { - while (XCheckWindowEvent(dpy,_window,PointerMotionMask,&event)) {} - _mouse_x = event.xmotion.x; - _mouse_y = event.xmotion.y; - if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1; - _is_event = true; - pthread_cond_broadcast(&cimg::X11_attr().wait_event); - } break; - } - } - - static void* _events_thread(void *arg) { // Thread to manage events for all opened display windows. - Display *const dpy = cimg::X11_attr().display; - XEvent event; - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,0); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0); - if (!arg) for (;;) { - XLockDisplay(dpy); - bool event_flag = XCheckTypedEvent(dpy,ClientMessage,&event); - if (!event_flag) event_flag = XCheckMaskEvent(dpy, - ExposureMask | StructureNotifyMask | ButtonPressMask | - KeyPressMask | PointerMotionMask | EnterWindowMask | - LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask,&event); - if (event_flag) - for (unsigned int i = 0; i_is_closed && event.xany.window==cimg::X11_attr().wins[i]->_window) - cimg::X11_attr().wins[i]->_handle_events(&event); - XUnlockDisplay(dpy); - pthread_testcancel(); - cimg::sleep(8); - } - return 0; - } - - void _set_colormap(Colormap& _colormap, const unsigned int dim) { - XColor colormap[256]; - switch (dim) { - case 1 : { // colormap for greyscale images - for (unsigned int index = 0; index<256; ++index) { - colormap[index].pixel = index; - colormap[index].red = colormap[index].green = colormap[index].blue = (unsigned short)(index<<8); - colormap[index].flags = DoRed | DoGreen | DoBlue; - } - } break; - case 2 : { // colormap for RG images - for (unsigned int index = 0, r = 8; r<256; r+=16) - for (unsigned int g = 8; g<256; g+=16) { - colormap[index].pixel = index; - colormap[index].red = colormap[index].blue = (unsigned short)(r<<8); - colormap[index].green = (unsigned short)(g<<8); - colormap[index++].flags = DoRed | DoGreen | DoBlue; - } - } break; - default : { // colormap for RGB images - for (unsigned int index = 0, r = 16; r<256; r+=32) - for (unsigned int g = 16; g<256; g+=32) - for (unsigned int b = 32; b<256; b+=64) { - colormap[index].pixel = index; - colormap[index].red = (unsigned short)(r<<8); - colormap[index].green = (unsigned short)(g<<8); - colormap[index].blue = (unsigned short)(b<<8); - colormap[index++].flags = DoRed | DoGreen | DoBlue; - } - } - } - XStoreColors(cimg::X11_attr().display,_colormap,colormap,256); - } - - void _map_window() { - Display *const dpy = cimg::X11_attr().display; - bool is_exposed = false, is_mapped = false; - XWindowAttributes attr; - XEvent event; - XMapRaised(dpy,_window); - do { // Wait for the window to be mapped. - XWindowEvent(dpy,_window,StructureNotifyMask | ExposureMask,&event); - switch (event.type) { - case MapNotify : is_mapped = true; break; - case Expose : is_exposed = true; break; - } - } while (!is_exposed || !is_mapped); - do { // Wait for the window to be visible. - XGetWindowAttributes(dpy,_window,&attr); - if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); } - } while (attr.map_state!=IsViewable); - _window_x = attr.x; - _window_y = attr.y; - } - - void _paint(const bool wait_expose=true) { - if (_is_closed || !_image) return; - Display *const dpy = cimg::X11_attr().display; - if (wait_expose) { // Send an expose event sticked to display window to force repaint. - XEvent event; - event.xexpose.type = Expose; - event.xexpose.serial = 0; - event.xexpose.send_event = 1; - event.xexpose.display = dpy; - event.xexpose.window = _window; - event.xexpose.x = 0; - event.xexpose.y = 0; - event.xexpose.width = width(); - event.xexpose.height = height(); - event.xexpose.count = 0; - XSendEvent(dpy,_window,0,0,&event); - } else { // Repaint directly (may be called from the expose event). - GC gc = DefaultGC(dpy,DefaultScreen(dpy)); -#ifdef cimg_use_xshm - if (_shminfo) XShmPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height,1); - else XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height); -#else - XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height); -#endif - } - } - - template - void _resize(T pixel_type, const unsigned int ndimx, const unsigned int ndimy, const bool force_redraw) { - Display *const dpy = cimg::X11_attr().display; - cimg::unused(pixel_type); - -#ifdef cimg_use_xshm - if (_shminfo) { - XShmSegmentInfo *const nshminfo = new XShmSegmentInfo; - XImage *const nimage = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)), - cimg::X11_attr().nb_bits,ZPixmap,0,nshminfo,ndimx,ndimy); - if (!nimage) { delete nshminfo; return; } - else { - nshminfo->shmid = shmget(IPC_PRIVATE,ndimx*ndimy*sizeof(T),IPC_CREAT | 0777); - if (nshminfo->shmid==-1) { XDestroyImage(nimage); delete nshminfo; return; } - else { - nshminfo->shmaddr = nimage->data = (char*)shmat(nshminfo->shmid,0,0); - if (nshminfo->shmaddr==(char*)-1) { - shmctl(nshminfo->shmid,IPC_RMID,0); XDestroyImage(nimage); delete nshminfo; return; - } else { - nshminfo->readOnly = 0; - cimg::X11_attr().is_shm_enabled = true; - XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm); - XShmAttach(dpy,nshminfo); - XFlush(dpy); - XSetErrorHandler(oldXErrorHandler); - if (!cimg::X11_attr().is_shm_enabled) { - shmdt(nshminfo->shmaddr); - shmctl(nshminfo->shmid,IPC_RMID,0); - XDestroyImage(nimage); - delete nshminfo; - return; - } else { - T *const ndata = (T*)nimage->data; - if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy); - else std::memset(ndata,0,sizeof(T)*ndimx*ndimy); - XShmDetach(dpy,_shminfo); - XDestroyImage(_image); - shmdt(_shminfo->shmaddr); - shmctl(_shminfo->shmid,IPC_RMID,0); - delete _shminfo; - _shminfo = nshminfo; - _image = nimage; - _data = (void*)ndata; - } - } - } - } - } else -#endif - { - T *ndata = (T*)std::malloc(ndimx*ndimy*sizeof(T)); - if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy); - else std::memset(ndata,0,sizeof(T)*ndimx*ndimy); - _data = (void*)ndata; - XDestroyImage(_image); - _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)), - cimg::X11_attr().nb_bits,ZPixmap,0,(char*)_data,ndimx,ndimy,8,0); - } - } - - void _init_fullscreen() { - if (!_is_fullscreen || _is_closed) return; - Display *const dpy = cimg::X11_attr().display; - _background_window = 0; - -#ifdef cimg_use_xrandr - int foo; - if (XRRQueryExtension(dpy,&foo,&foo)) { - XRRRotations(dpy,DefaultScreen(dpy),&cimg::X11_attr().curr_rotation); - if (!cimg::X11_attr().resolutions) { - cimg::X11_attr().resolutions = XRRSizes(dpy,DefaultScreen(dpy),&foo); - cimg::X11_attr().nb_resolutions = (unsigned int)foo; - } - if (cimg::X11_attr().resolutions) { - cimg::X11_attr().curr_resolution = 0; - for (unsigned int i = 0; i=_width && nh>=_height && - nw<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width) && - nh<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height)) - cimg::X11_attr().curr_resolution = i; - } - if (cimg::X11_attr().curr_resolution>0) { - XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy)); - XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy), - cimg::X11_attr().curr_resolution,cimg::X11_attr().curr_rotation,CurrentTime); - XRRFreeScreenConfigInfo(config); - XSync(dpy,0); - } - } - } - if (!cimg::X11_attr().resolutions) - cimg::warn(_cimgdisplay_instance - "init_fullscreen(): Xrandr extension not supported by the X server.", - cimgdisplay_instance); -#endif - - const unsigned int sx = screen_width(), sy = screen_height(); - if (sx==_width && sy==_height) return; - XSetWindowAttributes winattr; - winattr.override_redirect = 1; - _background_window = XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,sx,sy,0,0, - InputOutput,CopyFromParent,CWOverrideRedirect,&winattr); - const unsigned long buf_size = (unsigned long)sx*sy*(cimg::X11_attr().nb_bits==8?1: - (cimg::X11_attr().nb_bits==16?2:4)); - void *background_data = std::malloc(buf_size); - std::memset(background_data,0,buf_size); - XImage *background_image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits, - ZPixmap,0,(char*)background_data,sx,sy,8,0); - XEvent event; - XSelectInput(dpy,_background_window,StructureNotifyMask); - XMapRaised(dpy,_background_window); - do XWindowEvent(dpy,_background_window,StructureNotifyMask,&event); - while (event.type!=MapNotify); - GC gc = DefaultGC(dpy,DefaultScreen(dpy)); -#ifdef cimg_use_xshm - if (_shminfo) XShmPutImage(dpy,_background_window,gc,background_image,0,0,0,0,sx,sy,0); - else XPutImage(dpy,_background_window,gc,background_image,0,0,0,0,sx,sy); -#else - XPutImage(dpy,_background_window,gc,background_image,0,0,0,0,sx,sy); -#endif - XWindowAttributes attr; - XGetWindowAttributes(dpy,_background_window,&attr); - while (attr.map_state!=IsViewable) XSync(dpy,0); - XDestroyImage(background_image); - } - - void _desinit_fullscreen() { - if (!_is_fullscreen) return; - Display *const dpy = cimg::X11_attr().display; - XUngrabKeyboard(dpy,CurrentTime); -#ifdef cimg_use_xrandr - if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) { - XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy)); - XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy),0,cimg::X11_attr().curr_rotation,CurrentTime); - XRRFreeScreenConfigInfo(config); - XSync(dpy,0); - cimg::X11_attr().curr_resolution = 0; - } -#endif - if (_background_window) XDestroyWindow(dpy,_background_window); - _background_window = 0; - _is_fullscreen = false; - } - - static int _assign_xshm(Display *dpy, XErrorEvent *error) { - cimg::unused(dpy,error); - cimg::X11_attr().is_shm_enabled = false; - return 0; - } - - void _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - cimg::mutex(14); - - // Allocate space for window title - const char *const nptitle = ptitle?ptitle:""; - const unsigned int s = std::strlen(nptitle) + 1; - char *const tmp_title = s?new char[s]:0; - if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char)); - - // Destroy previous display window if existing - if (!is_empty()) assign(); - - // Open X11 display and retrieve graphical properties. - Display* &dpy = cimg::X11_attr().display; - if (!dpy) { - dpy = XOpenDisplay(0); - if (!dpy) - throw CImgDisplayException(_cimgdisplay_instance - "assign(): Failed to open X11 display.", - cimgdisplay_instance); - - cimg::X11_attr().nb_bits = DefaultDepth(dpy,DefaultScreen(dpy)); - if (cimg::X11_attr().nb_bits!=8 && cimg::X11_attr().nb_bits!=16 && - cimg::X11_attr().nb_bits!=24 && cimg::X11_attr().nb_bits!=32) - throw CImgDisplayException(_cimgdisplay_instance - "assign(): Invalid %u bits screen mode detected " - "(only 8, 16, 24 and 32 bits modes are managed).", - cimgdisplay_instance, - cimg::X11_attr().nb_bits); - XVisualInfo vtemplate; - vtemplate.visualid = XVisualIDFromVisual(DefaultVisual(dpy,DefaultScreen(dpy))); - int nb_visuals; - XVisualInfo *vinfo = XGetVisualInfo(dpy,VisualIDMask,&vtemplate,&nb_visuals); - if (vinfo && vinfo->red_maskblue_mask) cimg::X11_attr().is_blue_first = true; - cimg::X11_attr().byte_order = ImageByteOrder(dpy); - XFree(vinfo); - - XLockDisplay(dpy); - cimg::X11_attr().events_thread = new pthread_t; - pthread_create(cimg::X11_attr().events_thread,0,_events_thread,0); - } else XLockDisplay(dpy); - - // Set display variables. - _width = cimg::min(dimw,(unsigned int)screen_width()); - _height = cimg::min(dimh,(unsigned int)screen_height()); - _normalization = normalization_type<4?normalization_type:3; - _is_fullscreen = fullscreen_flag; - _window_x = _window_y = 0; - _is_closed = closed_flag; - _title = tmp_title; - flush(); - - // Create X11 window (and LUT, if 8bits display) - if (_is_fullscreen) { - if (!_is_closed) _init_fullscreen(); - const unsigned int sx = screen_width(), sy = screen_height(); - XSetWindowAttributes winattr; - winattr.override_redirect = 1; - _window = XCreateWindow(dpy,DefaultRootWindow(dpy),(sx-_width)/2,(sy-_height)/2,_width,_height,0,0, - InputOutput,CopyFromParent,CWOverrideRedirect,&winattr); - } else - _window = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),0,0,_width,_height,0,0L,0L); - - XSelectInput(dpy,_window, - ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask | PointerMotionMask | - EnterWindowMask | LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask); - - XStoreName(dpy,_window,_title?_title:" "); - if (cimg::X11_attr().nb_bits==8) { - _colormap = XCreateColormap(dpy,_window,DefaultVisual(dpy,DefaultScreen(dpy)),AllocAll); - _set_colormap(_colormap,3); - XSetWindowColormap(dpy,_window,_colormap); - } - - static const char *const _window_class = cimg_appname; - XClassHint *const window_class = XAllocClassHint(); - window_class->res_name = (char*)_window_class; - window_class->res_class = (char*)_window_class; - XSetClassHint(dpy,_window,window_class); - XFree(window_class); - - _window_width = _width; - _window_height = _height; - - // Create XImage -#ifdef cimg_use_xshm - _shminfo = 0; - if (XShmQueryExtension(dpy)) { - _shminfo = new XShmSegmentInfo; - _image = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits, - ZPixmap,0,_shminfo,_width,_height); - if (!_image) { delete _shminfo; _shminfo = 0; } - else { - _shminfo->shmid = shmget(IPC_PRIVATE,_image->bytes_per_line*_image->height,IPC_CREAT|0777); - if (_shminfo->shmid==-1) { XDestroyImage(_image); delete _shminfo; _shminfo = 0; } - else { - _shminfo->shmaddr = _image->data = (char*)(_data = shmat(_shminfo->shmid,0,0)); - if (_shminfo->shmaddr==(char*)-1) { - shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image); delete _shminfo; _shminfo = 0; - } else { - _shminfo->readOnly = 0; - cimg::X11_attr().is_shm_enabled = true; - XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm); - XShmAttach(dpy,_shminfo); - XSync(dpy,0); - XSetErrorHandler(oldXErrorHandler); - if (!cimg::X11_attr().is_shm_enabled) { - shmdt(_shminfo->shmaddr); shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image); - delete _shminfo; _shminfo = 0; - } - } - } - } - } - if (!_shminfo) -#endif - { - const unsigned long buf_size = (unsigned long)_width*_height*(cimg::X11_attr().nb_bits==8?1: - (cimg::X11_attr().nb_bits==16?2:4)); - _data = std::malloc(buf_size); - _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits, - ZPixmap,0,(char*)_data,_width,_height,8,0); - } - - _wm_window_atom = XInternAtom(dpy,"WM_DELETE_WINDOW",0); - _wm_protocol_atom = XInternAtom(dpy,"WM_PROTOCOLS",0); - XSetWMProtocols(dpy,_window,&_wm_window_atom,1); - - if (_is_fullscreen) XGrabKeyboard(dpy,_window,1,GrabModeAsync,GrabModeAsync,CurrentTime); - cimg::X11_attr().wins[cimg::X11_attr().nb_wins++]=this; - if (!_is_closed) _map_window(); else { _window_x = _window_y = cimg::type::min(); } - XUnlockDisplay(dpy); - cimg::mutex(14,0); - } - - CImgDisplay& assign() { - if (is_empty()) return flush(); - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - - // Remove display window from event thread list. - unsigned int i; - for (i = 0; ishmaddr); - shmctl(_shminfo->shmid,IPC_RMID,0); - delete _shminfo; - _shminfo = 0; - } else -#endif - XDestroyImage(_image); - _data = 0; _image = 0; - if (cimg::X11_attr().nb_bits==8) XFreeColormap(dpy,_colormap); - _colormap = 0; - XSync(dpy,0); - - // Reset display variables. - delete[] _title; - _width = _height = _normalization = _window_width = _window_height = 0; - _window_x = _window_y = 0; - _is_fullscreen = false; - _is_closed = true; - _min = _max = 0; - _title = 0; - flush(); - - XUnlockDisplay(dpy); - return *this; - } - - CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - if (!dimw || !dimh) return assign(); - _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag); - _min = _max = 0; - std::memset(_data,0,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char): - (cimg::X11_attr().nb_bits==16?sizeof(unsigned short):sizeof(unsigned int)))* - (unsigned long)_width*_height); - return paint(); - } - - template - CImgDisplay& assign(const CImg& img, const char *const title=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - if (!img) return assign(); - CImg tmp; - const CImg& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, - (img._height-1)/2, - (img._depth-1)/2)); - _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); - if (_normalization==2) _min = (float)nimg.min_max(_max); - return render(nimg).paint(); - } - - template - CImgDisplay& assign(const CImgList& list, const char *const title=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - if (!list) return assign(); - CImg tmp; - const CImg img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, - (img._height-1)/2, - (img._depth-1)/2)); - _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); - if (_normalization==2) _min = (float)nimg.min_max(_max); - return render(nimg).paint(); - } - - CImgDisplay& assign(const CImgDisplay& disp) { - if (!disp) return assign(); - _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed); - std::memcpy(_data,disp._data,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char): - cimg::X11_attr().nb_bits==16?sizeof(unsigned short): - sizeof(unsigned int))*(unsigned long)_width*_height); - return paint(); - } - - CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) { - if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign(); - if (is_empty()) return assign(nwidth,nheight); - Display *const dpy = cimg::X11_attr().display; - const unsigned int - tmpdimx = (nwidth>0)?nwidth:(-nwidth*width()/100), - tmpdimy = (nheight>0)?nheight:(-nheight*height()/100), - dimx = tmpdimx?tmpdimx:1, - dimy = tmpdimy?tmpdimy:1; - XLockDisplay(dpy); - if (_window_width!=dimx || _window_height!=dimy) { - XWindowAttributes attr; - for (unsigned int i = 0; i<10; ++i) { - XResizeWindow(dpy,_window,dimx,dimy); - XGetWindowAttributes(dpy,_window,&attr); - if (attr.width==(int)dimx && attr.height==(int)dimy) break; - cimg::wait(5); - } - } - if (_width!=dimx || _height!=dimy) switch (cimg::X11_attr().nb_bits) { - case 8 : { unsigned char pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break; - case 16 : { unsigned short pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break; - default : { unsigned int pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } - } - _window_width = _width = dimx; _window_height = _height = dimy; - _is_resized = false; - XUnlockDisplay(dpy); - if (_is_fullscreen) move((screen_width()-_width)/2,(screen_height()-_height)/2); - if (force_redraw) return paint(); - return *this; - } - - CImgDisplay& toggle_fullscreen(const bool force_redraw=true) { - if (is_empty()) return *this; - if (force_redraw) { - const unsigned long buf_size = (unsigned long)_width*_height* - (cimg::X11_attr().nb_bits==8?1:(cimg::X11_attr().nb_bits==16?2:4)); - void *image_data = std::malloc(buf_size); - std::memcpy(image_data,_data,buf_size); - assign(_width,_height,_title,_normalization,!_is_fullscreen,false); - std::memcpy(_data,image_data,buf_size); - std::free(image_data); - return paint(); - } - return assign(_width,_height,_title,_normalization,!_is_fullscreen,false); - } - - CImgDisplay& show() { - if (is_empty() || !_is_closed) return *this; - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - if (_is_fullscreen) _init_fullscreen(); - _map_window(); - _is_closed = false; - XUnlockDisplay(dpy); - return paint(); - } - - CImgDisplay& close() { - if (is_empty() || _is_closed) return *this; - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - if (_is_fullscreen) _desinit_fullscreen(); - XUnmapWindow(dpy,_window); - _window_x = _window_y = -1; - _is_closed = true; - XUnlockDisplay(dpy); - return *this; - } - - CImgDisplay& move(const int posx, const int posy) { - if (is_empty()) return *this; - show(); - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - XMoveWindow(dpy,_window,posx,posy); - _window_x = posx; _window_y = posy; - _is_moved = false; - XUnlockDisplay(dpy); - return paint(); - } - - CImgDisplay& show_mouse() { - if (is_empty()) return *this; - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - XUndefineCursor(dpy,_window); - XUnlockDisplay(dpy); - return *this; - } - - CImgDisplay& hide_mouse() { - if (is_empty()) return *this; - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - const char pix_data[8] = { 0 }; - XColor col; - col.red = col.green = col.blue = 0; - Pixmap pix = XCreateBitmapFromData(dpy,_window,pix_data,8,8); - Cursor cur = XCreatePixmapCursor(dpy,pix,pix,&col,&col,0,0); - XFreePixmap(dpy,pix); - XDefineCursor(dpy,_window,cur); - XUnlockDisplay(dpy); - return *this; - } - - CImgDisplay& set_mouse(const int posx, const int posy) { - if (is_empty() || _is_closed) return *this; - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - XWarpPointer(dpy,0L,_window,0,0,0,0,posx,posy); - _mouse_x = posx; _mouse_y = posy; - _is_moved = false; - XSync(dpy,0); - XUnlockDisplay(dpy); - return *this; - } - - CImgDisplay& set_title(const char *const format, ...) { - if (is_empty()) return *this; - char tmp[1024] = { 0 }; - va_list ap; - va_start(ap, format); - cimg_vsnprintf(tmp,sizeof(tmp),format,ap); - va_end(ap); - if (!std::strcmp(_title,tmp)) return *this; - delete[] _title; - const unsigned int s = std::strlen(tmp) + 1; - _title = new char[s]; - std::memcpy(_title,tmp,s*sizeof(char)); - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - XStoreName(dpy,_window,tmp); - XUnlockDisplay(dpy); - return *this; - } - - template - CImgDisplay& display(const CImg& img) { - if (!img) - throw CImgArgumentException(_cimgdisplay_instance - "display(): Empty specified image.", - cimgdisplay_instance); - if (is_empty()) return assign(img); - return render(img).paint(false); - } - - CImgDisplay& paint(const bool wait_expose=true) { - if (is_empty()) return *this; - Display *const dpy = cimg::X11_attr().display; - XLockDisplay(dpy); - _paint(wait_expose); - XUnlockDisplay(dpy); - return *this; - } - - template - CImgDisplay& render(const CImg& img, const bool flag8=false) { - if (!img) - throw CImgArgumentException(_cimgdisplay_instance - "render(): Empty specified image.", - cimgdisplay_instance); - if (is_empty()) return *this; - if (img._depth!=1) return render(img.get_projections2d((img._width-1)/2,(img._height-1)/2,(img._depth-1)/2)); - if (cimg::X11_attr().nb_bits==8 && (img._width!=_width || img._height!=_height)) - return render(img.get_resize(_width,_height,1,-100,1)); - if (cimg::X11_attr().nb_bits==8 && !flag8 && img._spectrum==3) { - static const CImg::ucharT> default_colormap = CImg::ucharT>::default_LUT256(); - return render(img.get_index(default_colormap,1,false)); - } - - Display *const dpy = cimg::X11_attr().display; - const T - *data1 = img._data, - *data2 = (img._spectrum>1)?img.data(0,0,0,1):data1, - *data3 = (img._spectrum>2)?img.data(0,0,0,2):data1; - - if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3); - XLockDisplay(dpy); - - if (!_normalization || (_normalization==3 && cimg::type::string()==cimg::type::string())) { - _min = _max = 0; - switch (cimg::X11_attr().nb_bits) { - case 8 : { // 256 colormap, no normalization - _set_colormap(_colormap,img._spectrum); - unsigned char *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:new unsigned char[(unsigned long)img._width*img._height]; - unsigned char *ptrd = (unsigned char*)ndata; - switch (img._spectrum) { - case 1 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) (*ptrd++) = (unsigned char)*(data1++); - break; - case 2 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char R = (unsigned char)*(data1++), G = (unsigned char)*(data2++); - (*ptrd++) = (R&0xf0) | (G>>4); - } break; - default : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char R = (unsigned char)*(data1++), G = (unsigned char)*(data2++), B = (unsigned char)*(data3++); - (*ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6); - } - } - if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height); delete[] ndata; } - } break; - case 16 : { // 16 bits colors, no normalization - unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:new unsigned short[(unsigned long)img._width*img._height]; - unsigned char *ptrd = (unsigned char*)ndata; - const unsigned int M = 248; - switch (img._spectrum) { - case 1 : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)*(data1++), G = val>>2; - *(ptrd++) = (val&M) | (G>>3); - *(ptrd++) = (G<<5) | (G>>1); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)*(data1++), G = val>>2; - *(ptrd++) = (G<<5) | (G>>1); - *(ptrd++) = (val&M) | (G>>3); - } - break; - case 2 : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)*(data2++)>>2; - *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); - *(ptrd++) = (G<<5); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)*(data2++)>>2; - *(ptrd++) = (G<<5); - *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); - } - break; - default : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)*(data2++)>>2; - *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); - *(ptrd++) = (G<<5) | ((unsigned char)*(data3++)>>3); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)*(data2++)>>2; - *(ptrd++) = (G<<5) | ((unsigned char)*(data3++)>>3); - *(ptrd++) = ((unsigned char)*(data1++)&M) | (G>>3); - } - } - if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height); delete[] ndata; } - } break; - default : { // 24 bits colors, no normalization - unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:new unsigned int[(unsigned long)img._width*img._height]; - if (sizeof(int)==4) { // 32 bits int uses optimized version - unsigned int *ptrd = ndata; - switch (img._spectrum) { - case 1 : - if (cimg::X11_attr().byte_order==cimg::endianness()) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)*(data1++); - *(ptrd++) = (val<<16) | (val<<8) | val; - } - else - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)*(data1++); - *(ptrd++) = (val<<16) | (val<<8) | val; - } - break; - case 2 : - if (cimg::X11_attr().byte_order==cimg::endianness()) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8); - else - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = ((unsigned char)*(data2++)<<16) | ((unsigned char)*(data1++)<<8); - break; - default : - if (cimg::X11_attr().byte_order==cimg::endianness()) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8) | (unsigned char)*(data3++); - else - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = ((unsigned char)*(data3++)<<24) | ((unsigned char)*(data2++)<<16) | ((unsigned char)*(data1++)<<8); - } - } else { - unsigned char *ptrd = (unsigned char*)ndata; - switch (img._spectrum) { - case 1 : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - *(ptrd++) = 0; - *(ptrd++) = (unsigned char)*(data1++); - *(ptrd++) = 0; - *(ptrd++) = 0; - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - *(ptrd++) = 0; - *(ptrd++) = 0; - *(ptrd++) = (unsigned char)*(data1++); - *(ptrd++) = 0; - } - break; - case 2 : - if (cimg::X11_attr().byte_order) cimg::swap(data1,data2); - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - *(ptrd++) = 0; - *(ptrd++) = (unsigned char)*(data2++); - *(ptrd++) = (unsigned char)*(data1++); - *(ptrd++) = 0; - } - break; - default : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - *(ptrd++) = 0; - *(ptrd++) = (unsigned char)*(data1++); - *(ptrd++) = (unsigned char)*(data2++); - *(ptrd++) = (unsigned char)*(data3++); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - *(ptrd++) = (unsigned char)*(data3++); - *(ptrd++) = (unsigned char)*(data2++); - *(ptrd++) = (unsigned char)*(data1++); - *(ptrd++) = 0; - } - } - } - if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height); delete[] ndata; } - } - } - } else { - if (_normalization==3) { - if (cimg::type::is_float()) _min = (float)img.min_max(_max); - else { _min = (float)cimg::type::min(); _max = (float)cimg::type::max(); } - } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max); - const float delta = _max - _min, mm = 255/(delta?delta:1.0f); - switch (cimg::X11_attr().nb_bits) { - case 8 : { // 256 colormap, with normalization - _set_colormap(_colormap,img._spectrum); - unsigned char *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:new unsigned char[(unsigned long)img._width*img._height]; - unsigned char *ptrd = (unsigned char*)ndata; - switch (img._spectrum) { - case 1 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char R = (unsigned char)((*(data1++)-_min)*mm); - *(ptrd++) = R; - } break; - case 2 : for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char - R = (unsigned char)((*(data1++)-_min)*mm), - G = (unsigned char)((*(data2++)-_min)*mm); - (*ptrd++) = (R&0xf0) | (G>>4); - } break; - default : - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char - R = (unsigned char)((*(data1++)-_min)*mm), - G = (unsigned char)((*(data2++)-_min)*mm), - B = (unsigned char)((*(data3++)-_min)*mm); - *(ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6); - } - } - if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height); delete[] ndata; } - } break; - case 16 : { // 16 bits colors, with normalization - unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:new unsigned short[(unsigned long)img._width*img._height]; - unsigned char *ptrd = (unsigned char*)ndata; - const unsigned int M = 248; - switch (img._spectrum) { - case 1 : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm), G = val>>2; - *(ptrd++) = (val&M) | (G>>3); - *(ptrd++) = (G<<5) | (val>>3); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm), G = val>>2; - *(ptrd++) = (G<<5) | (val>>3); - *(ptrd++) = (val&M) | (G>>3); - } - break; - case 2 : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; - *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); - *(ptrd++) = (G<<5); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; - *(ptrd++) = (G<<5); - *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); - } - break; - default : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; - *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); - *(ptrd++) = (G<<5) | ((unsigned char)((*(data3++)-_min)*mm)>>3); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char G = (unsigned char)((*(data2++)-_min)*mm)>>2; - *(ptrd++) = (G<<5) | ((unsigned char)((*(data3++)-_min)*mm)>>3); - *(ptrd++) = ((unsigned char)((*(data1++)-_min)*mm)&M) | (G>>3); - } - } - if (ndata!=_data) { _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height); delete[] ndata; } - } break; - default : { // 24 bits colors, with normalization - unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:new unsigned int[(unsigned long)img._width*img._height]; - if (sizeof(int)==4) { // 32 bits int uses optimized version - unsigned int *ptrd = ndata; - switch (img._spectrum) { - case 1 : - if (cimg::X11_attr().byte_order==cimg::endianness()) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); - *(ptrd++) = (val<<16) | (val<<8) | val; - } - else - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); - *(ptrd++) = (val<<24) | (val<<16) | (val<<8); - } - break; - case 2 : - if (cimg::X11_attr().byte_order==cimg::endianness()) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = - ((unsigned char)((*(data1++)-_min)*mm)<<16) | - ((unsigned char)((*(data2++)-_min)*mm)<<8); - else - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = - ((unsigned char)((*(data2++)-_min)*mm)<<16) | - ((unsigned char)((*(data1++)-_min)*mm)<<8); - break; - default : - if (cimg::X11_attr().byte_order==cimg::endianness()) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = - ((unsigned char)((*(data1++)-_min)*mm)<<16) | - ((unsigned char)((*(data2++)-_min)*mm)<<8) | - (unsigned char)((*(data3++)-_min)*mm); - else - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = - ((unsigned char)((*(data3++)-_min)*mm)<<24) | - ((unsigned char)((*(data2++)-_min)*mm)<<16) | - ((unsigned char)((*(data1++)-_min)*mm)<<8); - } - } else { - unsigned char *ptrd = (unsigned char*)ndata; - switch (img._spectrum) { - case 1 : - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); - (*ptrd++) = 0; - (*ptrd++) = val; - (*ptrd++) = val; - (*ptrd++) = val; - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); - (*ptrd++) = val; - (*ptrd++) = val; - (*ptrd++) = val; - (*ptrd++) = 0; - } - break; - case 2 : - if (cimg::X11_attr().byte_order) cimg::swap(data1,data2); - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - (*ptrd++) = 0; - (*ptrd++) = (unsigned char)((*(data2++)-_min)*mm); - (*ptrd++) = (unsigned char)((*(data1++)-_min)*mm); - (*ptrd++) = 0; - } - break; - default : - if (cimg::X11_attr().byte_order) - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - (*ptrd++) = 0; - (*ptrd++) = (unsigned char)((*(data1++)-_min)*mm); - (*ptrd++) = (unsigned char)((*(data2++)-_min)*mm); - (*ptrd++) = (unsigned char)((*(data3++)-_min)*mm); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - (*ptrd++) = (unsigned char)((*(data3++)-_min)*mm); - (*ptrd++) = (unsigned char)((*(data2++)-_min)*mm); - (*ptrd++) = (unsigned char)((*(data1++)-_min)*mm); - (*ptrd++) = 0; - } - } - } - if (ndata!=_data) { - _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height); delete[] ndata; - } - } - } - } - XUnlockDisplay(dpy); - return *this; - } - - template - const CImgDisplay& snapshot(CImg& img) const { - if (is_empty()) { img.assign(); return *this; } - const unsigned char *ptrs = (unsigned char*)_data; - img.assign(_width,_height,1,3); - T - *data1 = img.data(0,0,0,0), - *data2 = img.data(0,0,0,1), - *data3 = img.data(0,0,0,2); - if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3); - switch (cimg::X11_attr().nb_bits) { - case 8 : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = *(ptrs++); - *(data1++) = (T)(val&0xe0); - *(data2++) = (T)((val&0x1c)<<3); - *(data3++) = (T)(val<<6); - } - } break; - case 16 : { - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val0 = *(ptrs++), val1 = *(ptrs++); - *(data1++) = (T)(val0&0xf8); - *(data2++) = (T)((val0<<5) | ((val1&0xe0)>>5)); - *(data3++) = (T)(val1<<3); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned short val0 = *(ptrs++), val1 = *(ptrs++); - *(data1++) = (T)(val1&0xf8); - *(data2++) = (T)((val1<<5) | ((val0&0xe0)>>5)); - *(data3++) = (T)(val0<<3); - } - } break; - default : { - if (cimg::X11_attr().byte_order) for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - ++ptrs; - *(data1++) = (T)*(ptrs++); - *(data2++) = (T)*(ptrs++); - *(data3++) = (T)*(ptrs++); - } else for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - *(data3++) = (T)*(ptrs++); - *(data2++) = (T)*(ptrs++); - *(data1++) = (T)*(ptrs++); - ++ptrs; - } - } - } - return *this; - } - - // Windows-based implementation. - //------------------------------- -#elif cimg_display==2 - - bool _is_mouse_tracked, _is_cursor_visible; - HANDLE _thread, _is_created, _mutex; - HWND _window, _background_window; - CLIENTCREATESTRUCT _ccs; - unsigned int *_data; - DEVMODE _curr_mode; - BITMAPINFO _bmi; - HDC _hdc; - - static int screen_width() { - DEVMODE mode; - mode.dmSize = sizeof(DEVMODE); - mode.dmDriverExtra = 0; - EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode); - return mode.dmPelsWidth; - } - - static int screen_height() { - DEVMODE mode; - mode.dmSize = sizeof(DEVMODE); - mode.dmDriverExtra = 0; - EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode); - return mode.dmPelsHeight; - } - - static void wait_all() { - WaitForSingleObject(cimg::Win32_attr().wait_event,INFINITE); - } - - static LRESULT APIENTRY _handle_events(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) { -#ifdef _WIN64 - CImgDisplay *const disp = (CImgDisplay*)GetWindowLongPtr(window,GWLP_USERDATA); -#else - CImgDisplay *const disp = (CImgDisplay*)GetWindowLong(window,GWL_USERDATA); -#endif - MSG st_msg; - switch (msg) { - case WM_CLOSE : - disp->_mouse_x = disp->_mouse_y = -1; - disp->_window_x = disp->_window_y = 0; - disp->set_button().set_key(0).set_key(0,false)._is_closed = true; - ReleaseMutex(disp->_mutex); - ShowWindow(disp->_window,SW_HIDE); - disp->_is_event = true; - SetEvent(cimg::Win32_attr().wait_event); - return 0; - case WM_SIZE : { - while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {} - WaitForSingleObject(disp->_mutex,INFINITE); - const unsigned int nw = LOWORD(lParam),nh = HIWORD(lParam); - if (nw && nh && (nw!=disp->_width || nh!=disp->_height)) { - disp->_window_width = nw; - disp->_window_height = nh; - disp->_mouse_x = disp->_mouse_y = -1; - disp->_is_resized = disp->_is_event = true; - SetEvent(cimg::Win32_attr().wait_event); - } - ReleaseMutex(disp->_mutex); - } break; - case WM_MOVE : { - while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {} - WaitForSingleObject(disp->_mutex,INFINITE); - const int nx = (int)(short)(LOWORD(lParam)), ny = (int)(short)(HIWORD(lParam)); - if (nx!=disp->_window_x || ny!=disp->_window_y) { - disp->_window_x = nx; - disp->_window_y = ny; - disp->_is_moved = disp->_is_event = true; - SetEvent(cimg::Win32_attr().wait_event); - } - ReleaseMutex(disp->_mutex); - } break; - case WM_PAINT : - disp->paint(); - if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE)>=0); - break; - case WM_KEYDOWN : - disp->set_key((unsigned int)wParam); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_KEYUP : - disp->set_key((unsigned int)wParam,false); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_MOUSEMOVE : { - while (PeekMessage(&st_msg,window,WM_MOUSEMOVE,WM_MOUSEMOVE,PM_REMOVE)) {} - disp->_mouse_x = LOWORD(lParam); - disp->_mouse_y = HIWORD(lParam); -#if (_WIN32_WINNT>=0x0400) && !defined(NOTRACKMOUSEEVENT) - if (!disp->_is_mouse_tracked) { - TRACKMOUSEEVENT tme; - tme.cbSize = sizeof(TRACKMOUSEEVENT); - tme.dwFlags = TME_LEAVE; - tme.hwndTrack = disp->_window; - if (TrackMouseEvent(&tme)) disp->_is_mouse_tracked = true; - } -#endif - if (disp->_mouse_x<0 || disp->_mouse_y<0 || disp->_mouse_x>=disp->width() || disp->_mouse_y>=disp->height()) - disp->_mouse_x = disp->_mouse_y = -1; - disp->_is_event = true; - SetEvent(cimg::Win32_attr().wait_event); - if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE)>=0); - } break; - case WM_MOUSELEAVE : { - disp->_mouse_x = disp->_mouse_y = -1; - disp->_is_mouse_tracked = false; - while (ShowCursor(TRUE)<0); - } break; - case WM_LBUTTONDOWN : - disp->set_button(1); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_RBUTTONDOWN : - disp->set_button(2); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_MBUTTONDOWN : - disp->set_button(3); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_LBUTTONUP : - disp->set_button(1,false); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_RBUTTONUP : - disp->set_button(2,false); - SetEvent(cimg::Win32_attr().wait_event); - break; - case WM_MBUTTONUP : - disp->set_button(3,false); - SetEvent(cimg::Win32_attr().wait_event); - break; - case 0x020A : // WM_MOUSEWHEEL: - disp->set_wheel((int)((short)HIWORD(wParam))/120); - SetEvent(cimg::Win32_attr().wait_event); - } - return DefWindowProc(window,msg,wParam,lParam); - } - - static DWORD WINAPI _events_thread(void* arg) { - CImgDisplay *const disp = (CImgDisplay*)(((void**)arg)[0]); - const char *const title = (const char*)(((void**)arg)[1]); - MSG msg; - delete[] (void**)arg; - disp->_bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - disp->_bmi.bmiHeader.biWidth = disp->width(); - disp->_bmi.bmiHeader.biHeight = -disp->height(); - disp->_bmi.bmiHeader.biPlanes = 1; - disp->_bmi.bmiHeader.biBitCount = 32; - disp->_bmi.bmiHeader.biCompression = BI_RGB; - disp->_bmi.bmiHeader.biSizeImage = 0; - disp->_bmi.bmiHeader.biXPelsPerMeter = 1; - disp->_bmi.bmiHeader.biYPelsPerMeter = 1; - disp->_bmi.bmiHeader.biClrUsed = 0; - disp->_bmi.bmiHeader.biClrImportant = 0; - disp->_data = new unsigned int[(unsigned long)disp->_width*disp->_height]; - if (!disp->_is_fullscreen) { // Normal window - RECT rect; - rect.left = rect.top = 0; rect.right = disp->_width-1; rect.bottom = disp->_height-1; - AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); - const int - border1 = (rect.right - rect.left + 1 - disp->_width)/2, - border2 = rect.bottom - rect.top + 1 - disp->_height - border1; - disp->_window = CreateWindowA("MDICLIENT",title?title:" ", - WS_OVERLAPPEDWINDOW | (disp->_is_closed?0:WS_VISIBLE), CW_USEDEFAULT,CW_USEDEFAULT, - disp->_width + 2*border1, disp->_height + border1 + border2, - 0,0,0,&(disp->_ccs)); - if (!disp->_is_closed) { - GetWindowRect(disp->_window,&rect); - disp->_window_x = rect.left + border1; - disp->_window_y = rect.top + border2; - } else disp->_window_x = disp->_window_y = 0; - } else { // Fullscreen window - const unsigned int sx = screen_width(), sy = screen_height(); - disp->_window = CreateWindowA("MDICLIENT",title?title:" ", - WS_POPUP | (disp->_is_closed?0:WS_VISIBLE), (sx-disp->_width)/2, - (sy-disp->_height)/2, - disp->_width,disp->_height,0,0,0,&(disp->_ccs)); - disp->_window_x = disp->_window_y = 0; - } - SetForegroundWindow(disp->_window); - disp->_hdc = GetDC(disp->_window); - disp->_window_width = disp->_width; - disp->_window_height = disp->_height; - disp->flush(); -#ifdef _WIN64 - SetWindowLongPtr(disp->_window,GWLP_USERDATA,(LONG_PTR)disp); - SetWindowLongPtr(disp->_window,GWLP_WNDPROC,(LONG_PTR)_handle_events); -#else - SetWindowLong(disp->_window,GWL_USERDATA,(LONG)disp); - SetWindowLong(disp->_window,GWL_WNDPROC,(LONG)_handle_events); -#endif - SetEvent(disp->_is_created); - while (GetMessage(&msg,0,0,0)) DispatchMessage(&msg); - return 0; - } - - CImgDisplay& _update_window_pos() { - if (_is_closed) _window_x = _window_y = -1; - else { - RECT rect; - rect.left = rect.top = 0; rect.right = _width-1; rect.bottom = _height-1; - AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); - const int - border1 = (rect.right - rect.left + 1 - _width)/2, - border2 = rect.bottom - rect.top + 1 - _height - border1; - GetWindowRect(_window,&rect); - _window_x = rect.left + border1; - _window_y = rect.top + border2; - } - return *this; - } - - void _init_fullscreen() { - _background_window = 0; - if (!_is_fullscreen || _is_closed) _curr_mode.dmSize = 0; - else { - DEVMODE mode; - unsigned int imode = 0, ibest = 0, bestbpp = 0, bw = ~0U, bh = ~0U; - for (mode.dmSize = sizeof(DEVMODE), mode.dmDriverExtra = 0; EnumDisplaySettings(0,imode,&mode); ++imode) { - const unsigned int nw = mode.dmPelsWidth, nh = mode.dmPelsHeight; - if (nw>=_width && nh>=_height && mode.dmBitsPerPel>=bestbpp && nw<=bw && nh<=bh) { - bestbpp = mode.dmBitsPerPel; - ibest = imode; - bw = nw; bh = nh; - } - } - if (bestbpp) { - _curr_mode.dmSize = sizeof(DEVMODE); _curr_mode.dmDriverExtra = 0; - EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&_curr_mode); - EnumDisplaySettings(0,ibest,&mode); - ChangeDisplaySettings(&mode,0); - } else _curr_mode.dmSize = 0; - - const unsigned int sx = screen_width(), sy = screen_height(); - if (sx!=_width || sy!=_height) { - CLIENTCREATESTRUCT background_ccs; - _background_window = CreateWindowA("MDICLIENT","",WS_POPUP | WS_VISIBLE, 0,0,sx,sy,0,0,0,&background_ccs); - SetForegroundWindow(_background_window); - } - } - } - - void _desinit_fullscreen() { - if (!_is_fullscreen) return; - if (_background_window) DestroyWindow(_background_window); - _background_window = 0; - if (_curr_mode.dmSize) ChangeDisplaySettings(&_curr_mode,0); - _is_fullscreen = false; - } - - CImgDisplay& _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - - // Allocate space for window title - const char *const nptitle = ptitle?ptitle:""; - const unsigned int s = (unsigned int)std::strlen(nptitle) + 1; - char *const tmp_title = s?new char[s]:0; - if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char)); - - // Destroy previous window if existing - if (!is_empty()) assign(); - - // Set display variables - _width = cimg::min(dimw,(unsigned int)screen_width()); - _height = cimg::min(dimh,(unsigned int)screen_height()); - _normalization = normalization_type<4?normalization_type:3; - _is_fullscreen = fullscreen_flag; - _window_x = _window_y = 0; - _is_closed = closed_flag; - _is_cursor_visible = true; - _is_mouse_tracked = false; - _title = tmp_title; - flush(); - if (_is_fullscreen) _init_fullscreen(); - - // Create event thread - void *const arg = (void*)(new void*[2]); - ((void**)arg)[0] = (void*)this; - ((void**)arg)[1] = (void*)_title; - _mutex = CreateMutex(0,FALSE,0); - _is_created = CreateEvent(0,FALSE,FALSE,0); - _thread = CreateThread(0,0,_events_thread,arg,0,0); - WaitForSingleObject(_is_created,INFINITE); - return *this; - } - - CImgDisplay& assign() { - if (is_empty()) return flush(); - DestroyWindow(_window); - TerminateThread(_thread,0); - delete[] _data; - delete[] _title; - _data = 0; - _title = 0; - if (_is_fullscreen) _desinit_fullscreen(); - _width = _height = _normalization = _window_width = _window_height = 0; - _window_x = _window_y = 0; - _is_fullscreen = false; - _is_closed = true; - _min = _max = 0; - _title = 0; - flush(); - return *this; - } - - CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - if (!dimw || !dimh) return assign(); - _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag); - _min = _max = 0; - std::memset(_data,0,sizeof(unsigned int)*_width*_height); - return paint(); - } - - template - CImgDisplay& assign(const CImg& img, const char *const title=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - if (!img) return assign(); - CImg tmp; - const CImg& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, - (img._height-1)/2, - (img._depth-1)/2)); - _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); - if (_normalization==2) _min = (float)nimg.min_max(_max); - return display(nimg); - } - - template - CImgDisplay& assign(const CImgList& list, const char *const title=0, - const unsigned int normalization_type=3, - const bool fullscreen_flag=false, const bool closed_flag=false) { - if (!list) return assign(); - CImg tmp; - const CImg img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width-1)/2, - (img._height-1)/2, - (img._depth-1)/2)); - _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag); - if (_normalization==2) _min = (float)nimg.min_max(_max); - return display(nimg); - } - - CImgDisplay& assign(const CImgDisplay& disp) { - if (!disp) return assign(); - _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed); - std::memcpy(_data,disp._data,sizeof(unsigned int)*_width*_height); - return paint(); - } - - CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) { - if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign(); - if (is_empty()) return assign(nwidth,nheight); - const unsigned int - tmpdimx = (nwidth>0)?nwidth:(-nwidth*_width/100), - tmpdimy = (nheight>0)?nheight:(-nheight*_height/100), - dimx = tmpdimx?tmpdimx:1, - dimy = tmpdimy?tmpdimy:1; - if (_window_width!=dimx || _window_height!=dimy) { - RECT rect; rect.left = rect.top = 0; rect.right = dimx - 1; rect.bottom = dimy - 1; - AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); - const int cwidth = rect.right - rect.left + 1, cheight = rect.bottom - rect.top + 1; - SetWindowPos(_window,0,0,0,cwidth,cheight,SWP_NOMOVE | SWP_NOZORDER | SWP_NOCOPYBITS); - } - if (_width!=dimx || _height!=dimy) { - unsigned int *const ndata = new unsigned int[dimx*dimy]; - if (force_redraw) _render_resize(_data,_width,_height,ndata,dimx,dimy); - else std::memset(ndata,0x80,sizeof(unsigned int)*dimx*dimy); - delete[] _data; - _data = ndata; - _bmi.bmiHeader.biWidth = dimx; - _bmi.bmiHeader.biHeight = -(int)dimy; - _width = dimx; - _height = dimy; - } - _window_width = dimx; _window_height = dimy; - _is_resized = false; - if (_is_fullscreen) move((screen_width()-_width)/2,(screen_height()-_height)/2); - if (force_redraw) return paint(); - return *this; - } - - CImgDisplay& toggle_fullscreen(const bool force_redraw=true) { - if (is_empty()) return *this; - if (force_redraw) { - const unsigned long buf_size = _width*_height*4UL; - void *odata = std::malloc(buf_size); - std::memcpy(odata,_data,buf_size); - assign(_width,_height,_title,_normalization,!_is_fullscreen,false); - std::memcpy(_data,odata,buf_size); - std::free(odata); - return paint(); - } - return assign(_width,_height,_title,_normalization,!_is_fullscreen,false); - } - - CImgDisplay& show() { - if (is_empty() || !_is_closed) return *this; - _is_closed = false; - if (_is_fullscreen) _init_fullscreen(); - ShowWindow(_window,SW_SHOW); - _update_window_pos(); - return paint(); - } - - CImgDisplay& close() { - if (is_empty() || _is_closed) return *this; - _is_closed = true; - if (_is_fullscreen) _desinit_fullscreen(); - ShowWindow(_window,SW_HIDE); - _window_x = _window_y = 0; - return *this; - } - - CImgDisplay& move(const int posx, const int posy) { - if (is_empty()) return *this; - if (!_is_fullscreen) { - RECT rect; rect.left = rect.top = 0; rect.right = _window_width-1; rect.bottom = _window_height-1; - AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false); - const int border1 = (rect.right-rect.left+1-_width)/2, border2 = rect.bottom-rect.top+1-_height-border1; - SetWindowPos(_window,0,posx-border1,posy-border2,0,0,SWP_NOSIZE | SWP_NOZORDER); - } else SetWindowPos(_window,0,posx,posy,0,0,SWP_NOSIZE | SWP_NOZORDER); - _window_x = posx; - _window_y = posy; - _is_moved = false; - return show(); - } - - CImgDisplay& show_mouse() { - if (is_empty()) return *this; - _is_cursor_visible = true; - return *this; - } - - CImgDisplay& hide_mouse() { - if (is_empty()) return *this; - _is_cursor_visible = false; - return *this; - } - - CImgDisplay& set_mouse(const int posx, const int posy) { - if (_is_closed || posx<0 || posy<0) return *this; - _update_window_pos(); - const int res = (int)SetCursorPos(_window_x + posx,_window_y + posy); - if (res) { _mouse_x = posx; _mouse_y = posy; } - return *this; - } - - CImgDisplay& set_title(const char *const format, ...) { - if (is_empty()) return *this; - char tmp[1024] = { 0 }; - va_list ap; - va_start(ap, format); - cimg_vsnprintf(tmp,sizeof(tmp),format,ap); - va_end(ap); - if (!std::strcmp(_title,tmp)) return *this; - delete[] _title; - const unsigned int s = (unsigned int)std::strlen(tmp) + 1; - _title = new char[s]; - std::memcpy(_title,tmp,s*sizeof(char)); - SetWindowTextA(_window, tmp); - return *this; - } - - template - CImgDisplay& display(const CImg& img) { - if (!img) - throw CImgArgumentException(_cimgdisplay_instance - "display(): Empty specified image.", - cimgdisplay_instance); - if (is_empty()) return assign(img); - return render(img).paint(); - } - - CImgDisplay& paint() { - if (_is_closed) return *this; - WaitForSingleObject(_mutex,INFINITE); - SetDIBitsToDevice(_hdc,0,0,_width,_height,0,0,0,_height,_data,&_bmi,DIB_RGB_COLORS); - ReleaseMutex(_mutex); - return *this; - } - - template - CImgDisplay& render(const CImg& img) { - if (!img) - throw CImgArgumentException(_cimgdisplay_instance - "render(): Empty specified image.", - cimgdisplay_instance); - - if (is_empty()) return *this; - if (img._depth!=1) return render(img.get_projections2d((img._width-1)/2,(img._height-1)/2,(img._depth-1)/2)); - - const T - *data1 = img._data, - *data2 = (img._spectrum>=2)?img.data(0,0,0,1):data1, - *data3 = (img._spectrum>=3)?img.data(0,0,0,2):data1; - - WaitForSingleObject(_mutex,INFINITE); - unsigned int - *const ndata = (img._width==_width && img._height==_height)?_data: - new unsigned int[(unsigned long)img._width*img._height], - *ptrd = ndata; - - if (!_normalization || (_normalization==3 && cimg::type::string()==cimg::type::string())) { - _min = _max = 0; - switch (img._spectrum) { - case 1 : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)*(data1++); - *(ptrd++) = (val<<16) | (val<<8) | val; - } - } break; - case 2 : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8); - } break; - default : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) - *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8) | (unsigned char)*(data3++); - } - } - } else { - if (_normalization==3) { - if (cimg::type::is_float()) _min = (float)img.min_max(_max); - else { _min = (float)cimg::type::min(); _max = (float)cimg::type::max(); } - } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max); - const float delta = _max - _min, mm = 255/(delta?delta:1.0f); - switch (img._spectrum) { - case 1 : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char val = (unsigned char)((*(data1++)-_min)*mm); - *(ptrd++) = (val<<16) | (val<<8) | val; - } - } break; - case 2 : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char - R = (unsigned char)((*(data1++)-_min)*mm), - G = (unsigned char)((*(data2++)-_min)*mm); - *(ptrd++) = (R<<16) | (G<<8); - } - } break; - default : { - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned char - R = (unsigned char)((*(data1++)-_min)*mm), - G = (unsigned char)((*(data2++)-_min)*mm), - B = (unsigned char)((*(data3++)-_min)*mm); - *(ptrd++) = (R<<16) | (G<<8) | B; - } - } - } - } - if (ndata!=_data) { _render_resize(ndata,img._width,img._height,_data,_width,_height); delete[] ndata; } - ReleaseMutex(_mutex); - return *this; - } - - template - const CImgDisplay& snapshot(CImg& img) const { - if (is_empty()) { img.assign(); return *this; } - const unsigned int *ptrs = _data; - img.assign(_width,_height,1,3); - T - *data1 = img.data(0,0,0,0), - *data2 = img.data(0,0,0,1), - *data3 = img.data(0,0,0,2); - for (unsigned long xy = (unsigned long)img._width*img._height; xy>0; --xy) { - const unsigned int val = *(ptrs++); - *(data1++) = (T)(unsigned char)(val>>16); - *(data2++) = (T)(unsigned char)((val>>8)&0xFF); - *(data3++) = (T)(unsigned char)(val&0xFF); - } - return *this; - } -#endif - - //@} - }; - - /* - #-------------------------------------- - # - # - # - # Definition of the CImg structure - # - # - # - #-------------------------------------- - */ - - //! Class representing an image (up to 4 dimensions wide), each pixel being of type \c T. - /** - This is the main class of the %CImg Library. It declares and constructs - an image, allows access to its pixel values, and is able to perform various image operations. - - \par Image representation - - A %CImg image is defined as an instance of the container \c CImg, which contains a regular grid of pixels, - each pixel value being of type \c T. The image grid can have up to 4 dimensions: width, height, depth - and number of channels. - Usually, the three first dimensions are used to describe spatial coordinates (x,y,z), - while the number of channels is rather used as a vector-valued dimension - (it may describe the R,G,B color channels for instance). - If you need a fifth dimension, you can use image lists \c CImgList rather than simple images \c CImg. - - Thus, the \c CImg class is able to represent volumetric images of vector-valued pixels, - as well as images with less dimensions (1d scalar signal, 2d color images, ...). - Most member functions of the class CImg<\c T> are designed to handle this maximum case of (3+1) dimensions. - - Concerning the pixel value type \c T: - fully supported template types are the basic C++ types: unsigned char, char, short, unsigned int, int, - unsigned long, long, float, double, ... . - Typically, fast image display can be done using CImg images, - while complex image processing algorithms may be rather coded using CImg or CImg - images that have floating-point pixel values. The default value for the template T is \c float. - Using your own template types may be possible. However, you will certainly have to define the complete set - of arithmetic and logical operators for your class. - - \par Image structure - - The \c CImg structure contains \e six fields: - - \c _width defines the number of \a columns of the image (size along the X-axis). - - \c _height defines the number of \a rows of the image (size along the Y-axis). - - \c _depth defines the number of \a slices of the image (size along the Z-axis). - - \c _spectrum defines the number of \a channels of the image (size along the C-axis). - - \c _data defines a \a pointer to the \a pixel \a data (of type \c T). - - \c _is_shared is a boolean that tells if the memory buffer \c data is shared with - another image. - - You can access these fields publicly although it is recommended to use the dedicated functions - width(), height(), depth(), spectrum() and ptr() to do so. - Image dimensions are not limited to a specific range (as long as you got enough available memory). - A value of \e 1 usually means that the corresponding dimension is \a flat. - If one of the dimensions is \e 0, or if the data pointer is null, the image is considered as \e empty. - Empty images should not contain any pixel data and thus, will not be processed by CImg member functions - (a CImgInstanceException will be thrown instead). - Pixel data are stored in memory, in a non interlaced mode (See \ref cimg_storage). - - \par Image declaration and construction - - Declaring an image can be done by using one of the several available constructors. - Here is a list of the most used: - - - Construct images from arbitrary dimensions: - - CImg img; declares an empty image. - - CImg img(128,128); declares a 128x128 greyscale image with - \c unsigned \c char pixel values. - - CImg img(3,3); declares a 3x3 matrix with \c double coefficients. - - CImg img(256,256,1,3); declares a 256x256x1x3 (color) image - (colors are stored as an image with three channels). - - CImg img(128,128,128); declares a 128x128x128 volumetric and greyscale image - (with \c double pixel values). - - CImg<> img(128,128,128,3); declares a 128x128x128 volumetric color image - (with \c float pixels, which is the default value of the template parameter \c T). - - \b Note: images pixels are not automatically initialized to 0. You may use the function \c fill() to - do it, or use the specific constructor taking 5 parameters like this: - CImg<> img(128,128,128,3,0); declares a 128x128x128 volumetric color image with all pixel values to 0. - - - Construct images from filenames: - - CImg img("image.jpg"); reads a JPEG color image from the file "image.jpg". - - CImg img("analyze.hdr"); reads a volumetric image (ANALYZE7.5 format) from the - file "analyze.hdr". - - \b Note: You need to install ImageMagick - to be able to read common compressed image formats (JPG,PNG, ...) (See \ref cimg_files_io). - - - Construct images from C-style arrays: - - CImg img(data_buffer,256,256); constructs a 256x256 greyscale image from a \c int* buffer - \c data_buffer (of size 256x256=65536). - - CImg img(data_buffer,256,256,1,3,false); constructs a 256x256 color image - from a \c unsigned \c char* buffer \c data_buffer (where R,G,B channels follow each others). - - CImg img(data_buffer,256,256,1,3,true); constructs a 256x256 color image - from a \c unsigned \c char* buffer \c data_buffer (where R,G,B channels are multiplexed). - - The complete list of constructors can be found here. - - \par Most useful functions - - The \c CImg class contains a lot of functions that operates on images. - Some of the most useful are: - - - operator()(): allows to access or write pixel values. - - display(): displays the image in a new window. - **/ - template - struct CImg { - - unsigned int _width, _height, _depth, _spectrum; - bool _is_shared; - T *_data; - - //! Simple iterator type, to loop through each pixel value of an image instance. - /** - \note - - The \c CImg::iterator type is defined to be a T*. - - You will seldom have to use iterators in %CImg, most classical operations - being achieved (often in a faster way) using methods of \c CImg. - \par Example - \code - CImg img("reference.jpg"); // Load image from file. - for (CImg::iterator it = img.begin(), it::const_iterator type is defined to be a \c const \c T*. - - You will seldom have to use iterators in %CImg, most classical operations - being achieved (often in a faster way) using methods of \c CImg. - \par Example - \code - const CImg img("reference.jpg"); // Load image from file. - float sum = 0; - for (CImg::iterator it = img.begin(), it::value_type type of a \c CImg is defined to be a \c T. - - \c CImg::value_type is actually not used in %CImg methods. It has been mainly defined for - compatibility with STL naming conventions. - **/ - typedef T value_type; - - // Define common types related to template type T. - typedef typename cimg::superset::type Tbool; - typedef typename cimg::superset::type Tuchar; - typedef typename cimg::superset::type Tchar; - typedef typename cimg::superset::type Tushort; - typedef typename cimg::superset::type Tshort; - typedef typename cimg::superset::type Tuint; - typedef typename cimg::superset::type Tint; - typedef typename cimg::superset::type Tulong; - typedef typename cimg::superset::type Tlong; - typedef typename cimg::superset::type Tfloat; - typedef typename cimg::superset::type Tdouble; - typedef typename cimg::last::type boolT; - typedef typename cimg::last::type ucharT; - typedef typename cimg::last::type charT; - typedef typename cimg::last::type ushortT; - typedef typename cimg::last::type shortT; - typedef typename cimg::last::type uintT; - typedef typename cimg::last::type intT; - typedef typename cimg::last::type ulongT; - typedef typename cimg::last::type longT; - typedef typename cimg::last::type floatT; - typedef typename cimg::last::type doubleT; - - //@} - //--------------------------- - // - //! \name Plugins - //@{ - //--------------------------- -#ifdef cimg_plugin -#include cimg_plugin -#endif -#ifdef cimg_plugin1 -#include cimg_plugin1 -#endif -#ifdef cimg_plugin2 -#include cimg_plugin2 -#endif -#ifdef cimg_plugin3 -#include cimg_plugin3 -#endif -#ifdef cimg_plugin4 -#include cimg_plugin4 -#endif -#ifdef cimg_plugin5 -#include cimg_plugin5 -#endif -#ifdef cimg_plugin6 -#include cimg_plugin6 -#endif -#ifdef cimg_plugin7 -#include cimg_plugin7 -#endif -#ifdef cimg_plugin8 -#include cimg_plugin8 -#endif - - //@} - //--------------------------------------------------------- - // - //! \name Constructors / Destructor / Instance Management - //@{ - //--------------------------------------------------------- - - //! Destroy image. - /** - \note - - The pixel buffer data() is deallocated if necessary, e.g. for non-empty and non-shared image instances. - - Destroying an empty or shared image does nothing actually. - \warning - - When destroying a non-shared image, make sure that you will \e not operate on a remaining shared image - that shares its buffer with the destroyed instance, in order to avoid further invalid memory access - (to a deallocated buffer). - **/ - ~CImg() { - if (!_is_shared) delete[] _data; - } - - //! Construct empty image. - /** - \note - - An empty image has no pixel data and all of its dimensions width(), height(), depth(), spectrum() - are set to \c 0, as well as its pixel buffer pointer data(). - - An empty image may be re-assigned afterwards, e.g. with the family of - assign(unsigned int,unsigned int,unsigned int,unsigned int) methods, - or by operator=(const CImg&). In all cases, the type of pixels stays \c T. - - An empty image is never shared. - \par Example - \code - CImg img1, img2; // Construct two empty images. - img1.assign(256,256,1,3); // Re-assign 'img1' to be a 256x256x1x3 (color) image. - img2 = img1.get_rand(0,255); // Re-assign 'img2' to be a random-valued version of 'img1'. - img2.assign(); // Re-assign 'img2' to be an empty image again. - \endcode - **/ - CImg():_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {} - - //! Construct image with specified size. - /** - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \note - - It is able to create only \e non-shared images, and allocates thus a pixel buffer data() - for each constructed image instance. - - Setting one dimension \c size_x,\c size_y,\c size_z or \c size_c to \c 0 leads to the construction of - an \e empty image. - - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated - (e.g. when requested size is too big for available memory). - \warning - - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values. - In order to initialize pixel values during construction (e.g. with \c 0), use constructor - CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) instead. - \par Example - \code - CImg img1(256,256,1,3); // Construct a 256x256x1x3 (color) image, filled with garbage values. - CImg img2(256,256,1,3,0); // Construct a 256x256x1x3 (color) image, filled with value '0'. - \endcode - **/ - explicit CImg(const unsigned int size_x, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1): - _is_shared(false) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (siz) { - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - } - } else { _width = _height = _depth = _spectrum = 0; _data = 0; } - } - - //! Construct image with specified size and initialize pixel values. - /** - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \param value Initialization value. - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), - but it also fills the pixel buffer with the specified \c value. - \warning - - It cannot be used to construct a vector-valued image and initialize it with \e vector-valued pixels - (e.g. RGB vector, for color images). - For this task, you may use fillC() after construction. - **/ - CImg(const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, const T value): - _is_shared(false) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (siz) { - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - } - fill(value); - } else { _width = _height = _depth = _spectrum = 0; _data = 0; } - } - - //! Construct image with specified size and initialize pixel values from a sequence of integers. - /** - Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, - with pixels of type \c T, and initialize pixel - values from the specified sequence of integers \c value0,\c value1,\c ... - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \param value0 First value of the initialization sequence (must be an \e integer). - \param value1 Second value of the initialization sequence (must be an \e integer). - \param ... - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills - the pixel buffer with a sequence of specified integer values. - \warning - - You must specify \e exactly \c size_x*\c size_y*\c size_z*\c size_c integers in the initialization sequence. - Otherwise, the constructor may crash or fill your image pixels with garbage. - \par Example - \code - const CImg img(2,2,1,3, // Construct a 2x2 color (RGB) image. - 0,255,0,255, // Set the 4 values for the red component. - 0,0,255,255, // Set the 4 values for the green component. - 64,64,64,64); // Set the 4 values for the blue component. - img.resize(150,150).display(); - \endcode - \image html ref_constructor1.jpg - **/ - CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, - const int value0, const int value1, ...): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { -#define _CImg_stdarg(img,a0,a1,N,t) { \ - unsigned long _siz = (unsigned long)N; \ - if (_siz--) { \ - va_list ap; \ - va_start(ap,a1); \ - T *ptrd = (img)._data; \ - *(ptrd++) = (T)a0; \ - if (_siz--) { \ - *(ptrd++) = (T)a1; \ - for (; _siz; --_siz) *(ptrd++) = (T)va_arg(ap,t); \ - } \ - va_end(ap); \ - } \ - } - assign(size_x,size_y,size_z,size_c); - _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,int); - } - -#ifdef cimg_use_cpp11 - //! Construct image with specified size and initialize pixel values from an initializer list of integers. - /** - Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, - with pixels of type \c T, and initialize pixel - values from the specified initializer list of integers { \c value0,\c value1,\c ... } - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \param { value0, value1, ... } Initialization list - \param repeat_values Tells if the value filling process is repeated over the image. - - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills - the pixel buffer with a sequence of specified integer values. - \par Example - \code - const CImg img(2,2,1,3, // Construct a 2x2 color (RGB) image. - { 0,255,0,255, // Set the 4 values for the red component. - 0,0,255,255, // Set the 4 values for the green component. - 64,64,64,64 }); // Set the 4 values for the blue component. - img.resize(150,150).display(); - \endcode - \image html ref_constructor1.jpg - **/ - template - CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, - const std::initializer_list values, - const bool repeat_values=true): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { -#define _cimg_constructor_cpp11(repeat_values) \ - auto it = values.begin(); \ - unsigned long siz = size(); \ - if (repeat_values) for (T *ptrd = _data; siz--; ) { \ - *(ptrd++) = (T)(*(it++)); if (it==values.end()) it = values.begin(); } \ - else { siz = cimg::min(siz,values.size()); for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++)); } - assign(size_x,size_y,size_z,size_c); - _cimg_constructor_cpp11(repeat_values); - } - - template - CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, - std::initializer_list values, - const bool repeat_values=true): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(size_x,size_y,size_z); - _cimg_constructor_cpp11(repeat_values); - } - - template - CImg(const unsigned int size_x, const unsigned int size_y, - std::initializer_list values, - const bool repeat_values=true): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(size_x,size_y); - _cimg_constructor_cpp11(repeat_values); - } - - template - CImg(const unsigned int size_x, - std::initializer_list values, - const bool repeat_values=true):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(size_x); - _cimg_constructor_cpp11(repeat_values); - } - - //! Construct single channel 1D image with pixel values and width obtained from an initializer list of integers. - /** - Construct a new image instance of size \c width x \c 1 x \c 1 x \c 1, - with pixels of type \c T, and initialize pixel - values from the specified initializer list of integers { \c value0,\c value1,\c ... }. Image width is - given by the size of the initializer list. - \param { value0, value1, ... } Initialization list - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int) with height=1, depth=1, and spectrum=1, - but it also fills the pixel buffer with a sequence of specified integer values. - \par Example - \code - const CImg img = {10,20,30,20,10 }; // Construct a 5x1 image with one channel, and set its pixel values. - img.resize(150,150).display(); - \endcode - \image html ref_constructor1.jpg - **/ - template - CImg(const std::initializer_list values): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(values.size(),1,1,1); - auto it = values.begin(); - unsigned long siz = _width; - for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++)); - } - - template - CImg & operator=(std::initializer_list values) { - _cimg_constructor_cpp11(siz>values.size()); - return *this; - } -#endif - - //! Construct image with specified size and initialize pixel values from a sequence of doubles. - /** - Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T, - and initialize pixel values from the specified sequence of doubles \c value0,\c value1,\c ... - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \param value0 First value of the initialization sequence (must be a \e double). - \param value1 Second value of the initialization sequence (must be a \e double). - \param ... - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...), but - takes a sequence of double values instead of integers. - \warning - - You must specify \e exactly \c dx*\c dy*\c dz*\c dc doubles in the initialization sequence. - Otherwise, the constructor may crash or fill your image with garbage. - For instance, the code below will probably crash on most platforms: - \code - const CImg img(2,2,1,1, 0.5,0.5,255,255); // FAIL: The two last arguments are 'int', not 'double'! - \endcode - **/ - CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, - const double value0, const double value1, ...): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(size_x,size_y,size_z,size_c); - _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,double); - } - - //! Construct image with specified size and initialize pixel values from a value string. - /** - Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T, - and initializes pixel values from the specified string \c values. - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \param values Value string describing the way pixel values are set. - \param repeat_values Tells if the value filling process is repeated over the image. - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills - the pixel buffer with values described in the value string \c values. - - Value string \c values may describe two different filling processes: - - Either \c values is a sequences of values assigned to the image pixels, as in "1,2,3,7,8,2". - In this case, set \c repeat_values to \c true to periodically fill the image with the value sequence. - - Either, \c values is a formula, as in "cos(x/10)*sin(y/20)". - In this case, parameter \c repeat_values is pointless. - - For both cases, specifying \c repeat_values is mandatory. - It disambiguates the possible overloading of constructor - CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) with \c T being a const char*. - - A \c CImgArgumentException is thrown when an invalid value string \c values is specified. - \par Example - \code - const CImg img1(129,129,1,3,"0,64,128,192,255",true), // Construct image filled from a value sequence. - img2(129,129,1,3,"if(c==0,255*abs(cos(x/10)),1.8*y)",false); // Construct image filled from a formula. - (img1,img2).display(); - \endcode - \image html ref_constructor2.jpg - **/ - CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c, - const char *const values, const bool repeat_values):_is_shared(false) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (siz) { - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - } - fill(values,repeat_values); - } else { _width = _height = _depth = _spectrum = 0; _data = 0; } - } - - //! Construct image with specified size and initialize pixel values from a memory buffer. - /** - Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T, - and initializes pixel values from the specified \c t* memory buffer. - \param values Pointer to the input memory buffer. - \param size_x Image width(). - \param size_y Image height(). - \param size_z Image depth(). - \param size_c Image spectrum() (number of channels). - \param is_shared Tells if input memory buffer must be shared by the current instance. - \note - - If \c is_shared is \c false, the image instance allocates its own pixel buffer, - and values from the specified input buffer are copied to the instance buffer. - If buffer types \c T and \c t are different, a regular static cast is performed during buffer copy. - - Otherwise, the image instance does \e not allocate a new buffer, and uses the input memory buffer as its - own pixel buffer. This case requires that types \c T and \c t are the same. Later, destroying such a shared - image will not deallocate the pixel buffer, this task being obviously charged to the initial buffer allocator. - - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated - (e.g. when requested size is too big for available memory). - \warning - - You must take care when operating on a shared image, since it may have an invalid pixel buffer pointer data() - (e.g. already deallocated). - \par Example - \code - unsigned char tab[256*256] = { 0 }; - CImg img1(tab,256,256,1,1,false), // Construct new non-shared image from buffer 'tab'. - img2(tab,256,256,1,1,true); // Construct new shared-image from buffer 'tab'. - tab[1024] = 255; // Here, 'img2' is indirectly modified, but not 'img1'. - \endcode - **/ - template - CImg(const t *const values, const unsigned int size_x, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false):_is_shared(false) { - if (is_shared) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgArgumentException(_cimg_instance - "CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance " - "from a (%s*) buffer (pixel types are different).", - cimg_instance, - size_x,size_y,size_z,size_c,CImg::pixel_type()); - } - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (values && siz) { - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - - } - const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); - } else { _width = _height = _depth = _spectrum = 0; _data = 0; } - } - - //! Construct image with specified size and initialize pixel values from a memory buffer \specialization. - CImg(const T *const values, const unsigned int size_x, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (values && siz) { - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = is_shared; - if (_is_shared) _data = const_cast(values); - else { - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - } - std::memcpy(_data,values,siz*sizeof(T)); } - } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; } - } - - //! Construct image from reading an image file. - /** - Construct a new image instance with pixels of type \c T, and initialize pixel values with the data read from - an image file. - \param filename Filename, as a C-string. - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it reads the image - dimensions and pixel values from the specified image file. - - The recognition of the image file format by %CImg higly depends on the tools installed on your system - and on the external libraries you used to link your code against. - - Considered pixel type \c T should better fit the file format specification, or data loss may occur during - file load (e.g. constructing a \c CImg from a float-valued image file). - - A \c CImgIOException is thrown when the specified \c filename cannot be read, or if the file format is not - recognized. - \par Example - \code - const CImg img("reference.jpg"); - img.display(); - \endcode - \image html ref_image.jpg - **/ - explicit CImg(const char *const filename):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(filename); - } - - //! Construct image copy. - /** - Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg instance. - \param img Input image to copy. - \note - - Constructed copy has the same size width() x height() x depth() x spectrum() and pixel values as the - input image \c img. - - If input image \c img is \e shared and if types \c T and \c t are the same, the constructed copy is also - \e shared, and shares its pixel buffer with \c img. - Modifying a pixel value in the constructed copy will thus also modifies it in the input image \c img. - This behavior is needful to allow functions to return shared images. - - Otherwise, the constructed copy allocates its own pixel buffer, and copies pixel values from the input - image \c img into its buffer. The copied pixel values may be eventually statically casted if types \c T and - \c t are different. - - Constructing a copy from an image \c img when types \c t and \c T are the same is significantly faster than - with different types. - - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated - (e.g. not enough available memory). - **/ - template - CImg(const CImg& img):_is_shared(false) { - const unsigned long siz = img.size(); - if (img._data && siz) { - _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), - img._width,img._height,img._depth,img._spectrum); - } - const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); - } else { _width = _height = _depth = _spectrum = 0; _data = 0; } - } - - //! Construct image copy \specialization. - CImg(const CImg& img) { - const unsigned long siz = img.size(); - if (img._data && siz) { - _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; - _is_shared = img._is_shared; - if (_is_shared) _data = const_cast(img._data); - else { - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), - img._width,img._height,img._depth,img._spectrum); - - } - std::memcpy(_data,img._data,siz*sizeof(T)); - } - } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; } - } - - //! Advanced copy constructor. - /** - Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg instance, - while forcing the shared state of the constructed copy. - \param img Input image to copy. - \param is_shared Tells about the shared state of the constructed copy. - \note - - Similar to CImg(const CImg&), except that it allows to decide the shared state of - the constructed image, which does not depend anymore on the shared state of the input image \c img: - - If \c is_shared is \c true, the constructed copy will share its pixel buffer with the input image \c img. - For that case, the pixel types \c T and \c t \e must be the same. - - If \c is_shared is \c false, the constructed copy will allocate its own pixel buffer, whether the input - image \c img is shared or not. - - A \c CImgArgumentException is thrown when a shared copy is requested with different pixel types \c T and \c t. - **/ - template - CImg(const CImg& img, const bool is_shared):_is_shared(false) { - if (is_shared) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgArgumentException(_cimg_instance - "CImg(): Invalid construction request of a shared instance from a " - "CImg<%s> image (%u,%u,%u,%u,%p) (pixel types are different).", - cimg_instance, - CImg::pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data); - } - const unsigned long siz = img.size(); - if (img._data && siz) { - _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), - img._width,img._height,img._depth,img._spectrum); - } - const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); - } else { _width = _height = _depth = _spectrum = 0; _data = 0; } - } - - //! Advanced copy constructor \specialization. - CImg(const CImg& img, const bool is_shared) { - const unsigned long siz = img.size(); - if (img._data && siz) { - _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum; - _is_shared = is_shared; - if (_is_shared) _data = const_cast(img._data); - else { - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum), - img._width,img._height,img._depth,img._spectrum); - } - std::memcpy(_data,img._data,siz*sizeof(T)); - } - } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; } - } - - //! Construct image with dimensions borrowed from another image. - /** - Construct a new image instance with pixels of type \c T, and size get from some dimensions of an existing - \c CImg instance. - \param img Input image from which dimensions are borrowed. - \param dimensions C-string describing the image size along the X,Y,Z and C-dimensions. - \note - - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it takes the image dimensions - (\e not its pixel values) from an existing \c CImg instance. - - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values. - In order to initialize pixel values (e.g. with \c 0), use constructor CImg(const CImg&,const char*,T) - instead. - \par Example - \code - const CImg img1(256,128,1,3), // 'img1' is a 256x128x1x3 image. - img2(img1,"xyzc"), // 'img2' is a 256x128x1x3 image. - img3(img1,"y,x,z,c"), // 'img3' is a 128x256x1x3 image. - img4(img1,"c,x,y,3",0), // 'img4' is a 3x128x256x3 image (with pixels initialized to '0'). - \endcode - **/ - template - CImg(const CImg& img, const char *const dimensions): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(img,dimensions); - } - - //! Construct image with dimensions borrowed from another image and initialize pixel values. - /** - Construct a new image instance with pixels of type \c T, and size get from the dimensions of an existing - \c CImg instance, and set all pixel values to specified \c value. - \param img Input image from which dimensions are borrowed. - \param dimensions String describing the image size along the X,Y,Z and V-dimensions. - \param value Value used for initialization. - \note - - Similar to CImg(const CImg&,const char*), but it also fills the pixel buffer with the specified \c value. - **/ - template - CImg(const CImg& img, const char *const dimensions, const T value): - _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - assign(img,dimensions).fill(value); - } - - //! Construct image from a display window. - /** - Construct a new image instance with pixels of type \c T, as a snapshot of an existing \c CImgDisplay instance. - \param disp Input display window. - \note - - The width() and height() of the constructed image instance are the same as the specified \c CImgDisplay. - - The depth() and spectrum() of the constructed image instance are respectively set to \c 1 and \c 3 - (i.e. a 2d color image). - - The image pixels are read as 8-bits RGB values. - **/ - explicit CImg(const CImgDisplay &disp):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - disp.snapshot(*this); - } - - // Constructor and assignment operator for rvalue references (c++11). - // This avoids an additional image copy for methods returning new images. Can save RAM for big images ! -#ifdef cimg_use_cpp11 - CImg(CImg&& img):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) { - swap(img); - } - CImg& operator=(CImg&& img) { - if (_is_shared) return assign(img); - return img.swap(*this); - } -#endif - - //! Construct empty image \inplace. - /** - In-place version of the default constructor CImg(). It simply resets the instance to an empty image. - **/ - CImg& assign() { - if (!_is_shared) delete[] _data; - _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; - return *this; - } - - //! Construct image with specified size \inplace. - /** - In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int). - **/ - CImg& assign(const unsigned int size_x, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (!siz) return assign(); - const unsigned long curr_siz = size(); - if (siz!=curr_siz) { - if (_is_shared) - throw CImgArgumentException(_cimg_instance - "assign(): Invalid assignement request of shared instance from specified " - "image (%u,%u,%u,%u).", - cimg_instance, - size_x,size_y,size_z,size_c); - else { - delete[] _data; - try { _data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - } - } - } - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; - return *this; - } - - //! Construct image with specified size and initialize pixel values \inplace. - /** - In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,T). - **/ - CImg& assign(const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, const T value) { - return assign(size_x,size_y,size_z,size_c).fill(value); - } - - //! Construct image with specified size and initialize pixel values from a sequence of integers \inplace. - /** - In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...). - **/ - CImg& assign(const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, - const int value0, const int value1, ...) { - assign(size_x,size_y,size_z,size_c); - _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,int); - return *this; - } - - //! Construct image with specified size and initialize pixel values from a sequence of doubles \inplace. - /** - In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,double,double,...). - **/ - CImg& assign(const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, - const double value0, const double value1, ...) { - assign(size_x,size_y,size_z,size_c); - _CImg_stdarg(*this,value0,value1,(unsigned long)size_x*size_y*size_z*size_c,double); - return *this; - } - - //! Construct image with specified size and initialize pixel values from a value string \inplace. - /** - In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,const char*,bool). - **/ - CImg& assign(const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, - const char *const values, const bool repeat_values) { - return assign(size_x,size_y,size_z,size_c).fill(values,repeat_values); - } - - //! Construct image with specified size and initialize pixel values from a memory buffer \inplace. - /** - In-place version of the constructor CImg(const t*,unsigned int,unsigned int,unsigned int,unsigned int). - **/ - template - CImg& assign(const t *const values, const unsigned int size_x, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (!values || !siz) return assign(); - assign(size_x,size_y,size_z,size_c); - const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++); - return *this; - } - - //! Construct image with specified size and initialize pixel values from a memory buffer \specialization. - CImg& assign(const T *const values, const unsigned int size_x, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (!values || !siz) return assign(); - const unsigned long curr_siz = size(); - if (values==_data && siz==curr_siz) return assign(size_x,size_y,size_z,size_c); - if (_is_shared || values+siz<_data || values>=_data+size()) { - assign(size_x,size_y,size_z,size_c); - if (_is_shared) std::memmove(_data,values,siz*sizeof(T)); - else std::memcpy(_data,values,siz*sizeof(T)); - } else { - T *new_data = 0; - try { new_data = new T[siz]; } catch (...) { - _width = _height = _depth = _spectrum = 0; _data = 0; - throw CImgInstanceException(_cimg_instance - "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).", - cimg_instance, - cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c), - size_x,size_y,size_z,size_c); - } - std::memcpy(new_data,values,siz*sizeof(T)); - delete[] _data; _data = new_data; _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; - } - return *this; - } - - //! Construct image with specified size and initialize pixel values from a memory buffer \overloading. - template - CImg& assign(const t *const values, const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, const bool is_shared) { - if (is_shared) - throw CImgArgumentException(_cimg_instance - "assign(): Invalid assignment request of shared instance from (%s*) buffer" - "(pixel types are different).", - cimg_instance, - CImg::pixel_type()); - return assign(values,size_x,size_y,size_z,size_c); - } - - //! Construct image with specified size and initialize pixel values from a memory buffer \overloading. - CImg& assign(const T *const values, const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, const bool is_shared) { - const unsigned long siz = (unsigned long)size_x*size_y*size_z*size_c; - if (!values || !siz) { - if (is_shared) - throw CImgArgumentException(_cimg_instance - "assign(): Invalid assignment request of shared instance from (null) or " - "empty buffer.", - cimg_instance); - else return assign(); - } - if (!is_shared) { if (_is_shared) assign(); assign(values,size_x,size_y,size_z,size_c); } - else { - if (!_is_shared) { - if (values+siz<_data || values>=_data+size()) assign(); - else cimg::warn(_cimg_instance - "assign(): Shared image instance has overlapping memory.", - cimg_instance); - } - _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = true; - _data = const_cast(values); - } - return *this; - } - - //! Construct image from reading an image file \inplace. - /** - In-place version of the constructor CImg(const char*). - **/ - CImg& assign(const char *const filename) { - return load(filename); - } - - //! Construct image copy \inplace. - /** - In-place version of the constructor CImg(const CImg&). - **/ - template - CImg& assign(const CImg& img) { - return assign(img._data,img._width,img._height,img._depth,img._spectrum); - } - - //! In-place version of the advanced copy constructor. - /** - In-place version of the constructor CImg(const CImg&,bool). - **/ - template - CImg& assign(const CImg& img, const bool is_shared) { - return assign(img._data,img._width,img._height,img._depth,img._spectrum,is_shared); - } - - //! Construct image with dimensions borrowed from another image \inplace. - /** - In-place version of the constructor CImg(const CImg&,const char*). - **/ - template - CImg& assign(const CImg& img, const char *const dimensions) { - if (!dimensions || !*dimensions) return assign(img._width,img._height,img._depth,img._spectrum); - unsigned int siz[4] = { 0,1,1,1 }, k = 0; - for (const char *s = dimensions; *s && k<4; ++k) { - char item[256] = { 0 }; - if (std::sscanf(s,"%255[^0-9%xyzvwhdcXYZVWHDC]",item)>0) s+=std::strlen(item); - if (*s) { - unsigned int val = 0; char sep = 0; - if (std::sscanf(s,"%u%c",&val,&sep)>0) { - if (sep=='%') siz[k] = val*(k==0?_width:k==1?_height:k==2?_depth:_spectrum)/100; - else siz[k] = val; - while (*s>='0' && *s<='9') ++s; if (sep=='%') ++s; - } else switch (cimg::uncase(*s)) { - case 'x' : case 'w' : siz[k] = img._width; ++s; break; - case 'y' : case 'h' : siz[k] = img._height; ++s; break; - case 'z' : case 'd' : siz[k] = img._depth; ++s; break; - case 'c' : case 's' : siz[k] = img._spectrum; ++s; break; - default : - throw CImgArgumentException(_cimg_instance - "assign(): Invalid character '%c' detected in specified dimension string '%s'.", - cimg_instance, - *s,dimensions); - } - } - } - return assign(siz[0],siz[1],siz[2],siz[3]); - } - - //! Construct image with dimensions borrowed from another image and initialize pixel values \inplace. - /** - In-place version of the constructor CImg(const CImg&,const char*,T). - **/ - template - CImg& assign(const CImg& img, const char *const dimensions, const T value) { - return assign(img,dimensions).fill(value); - } - - //! Construct image from a display window \inplace. - /** - In-place version of the constructor CImg(const CImgDisplay&). - **/ - CImg& assign(const CImgDisplay &disp) { - disp.snapshot(*this); - return *this; - } - - //! Construct empty image \inplace. - /** - Equivalent to assign(). - \note - - It has been defined for compatibility with STL naming conventions. - **/ - CImg& clear() { - return assign(); - } - - //! Transfer content of an image instance into another one. - /** - Transfer the dimensions and the pixel buffer content of an image instance into another one, - and replace instance by an empty image. It avoids the copy of the pixel buffer - when possible. - \param img Destination image. - \note - - Pixel types \c T and \c t of source and destination images can be different, though the process is - designed to be instantaneous when \c T and \c t are the same. - \par Example - \code - CImg src(256,256,1,3,0), // Construct a 256x256x1x3 (color) image filled with value '0'. - dest(16,16); // Construct a 16x16x1x1 (scalar) image. - src.move_to(dest); // Now, 'src' is empty and 'dest' is the 256x256x1x3 image. - \endcode - **/ - template - CImg& move_to(CImg& img) { - img.assign(*this); - assign(); - return img; - } - - //! Transfer content of an image instance into another one \specialization. - CImg& move_to(CImg& img) { - if (_is_shared || img._is_shared) img.assign(*this); - else swap(img); - assign(); - return img; - } - - //! Transfer content of an image instance into a new image in an image list. - /** - Transfer the dimensions and the pixel buffer content of an image instance - into a newly inserted image at position \c pos in specified \c CImgList instance. - \param list Destination list. - \param pos Position of the newly inserted image in the list. - \note - - When optionnal parameter \c pos is ommited, the image instance is transfered as a new - image at the end of the specified \c list. - - It is convenient to sequentially insert new images into image lists, with no - additional copies of memory buffer. - \par Example - \code - CImgList list; // Construct an empty image list. - CImg img("reference.jpg"); // Read image from filename. - img.move_to(list); // Transfer image content as a new item in the list (no buffer copy). - \endcode - **/ - template - CImgList& move_to(CImgList& list, const unsigned int pos=~0U) { - const unsigned int npos = pos>list._width?list._width:pos; - move_to(list.insert(1,npos)[npos]); - return list; - } - - //! Swap fields of two image instances. - /** - \param img Image to swap fields with. - \note - - It can be used to interchange the content of two images in a very fast way. Can be convenient when dealing - with algorithms requiring two swapping buffers. - \par Example - \code - CImg img1("lena.jpg"), - img2("milla.jpg"); - img1.swap(img2); // Now, 'img1' is 'milla' and 'img2' is 'lena'. - \endcode - **/ - CImg& swap(CImg& img) { - cimg::swap(_width,img._width); - cimg::swap(_height,img._height); - cimg::swap(_depth,img._depth); - cimg::swap(_spectrum,img._spectrum); - cimg::swap(_data,img._data); - cimg::swap(_is_shared,img._is_shared); - return img; - } - - //! Return a reference to an empty image. - /** - \note - This function is useful mainly to declare optional parameters having type \c CImg in functions prototypes, - e.g. - \code - void f(const int x=0, const int y=0, const CImg& img=CImg::empty()); - \endcode - **/ - static CImg& empty() { - static CImg _empty; - return _empty.assign(); - } - - //@} - //------------------------------------------ - // - //! \name Overloaded Operators - //@{ - //------------------------------------------ - - //! Access to a pixel value. - /** - Return a reference to a located pixel value of the image instance, - being possibly \e const, whether the image instance is \e const or not. - This is the standard method to get/set pixel values in \c CImg images. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Range of pixel coordinates start from (0,0,0,0) to - (width()-1,height()-1,depth()-1,spectrum()-1). - - Due to the particular arrangement of the pixel buffers defined in %CImg, you can omit one coordinate if the - corresponding dimension is equal to \c 1. - For instance, pixels of a 2d image (depth() equal to \c 1) can be accessed by img(x,y,c) instead of - img(x,y,0,c). - \warning - - There is \e no boundary checking done in this operator, to make it as fast as possible. - You \e must take care of out-of-bounds access by yourself, if necessary. - For debuging purposes, you may want to define macro \c 'cimg_verbosity'>=3 to enable additional boundary - checking operations in this operator. In that case, warning messages will be printed on the error output - when accessing out-of-bounds pixels. - \par Example - \code - CImg img(100,100,1,3,0); // Construct a 100x100x1x3 (color) image with pixels set to '0'. - const float - valR = img(10,10,0,0), // Read red value at coordinates (10,10). - valG = img(10,10,0,1), // Read green value at coordinates (10,10) - valB = img(10,10,2), // Read blue value at coordinates (10,10) (Z-coordinate can be omitted). - avg = (valR + valG + valB)/3; // Compute average pixel value. - img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the color pixel (10,10) by the average grey value. - \endcode - **/ -#if cimg_verbosity>=3 - T& operator()(const unsigned int x, const unsigned int y=0, - const unsigned int z=0, const unsigned int c=0) { - const unsigned long off = (unsigned long)offset(x,y,z,c); - if (!_data || off>=size()) { - cimg::warn(_cimg_instance - "operator(): Invalid pixel request, at coordinates (%u,%u,%u,%u) [offset=%u].", - cimg_instance, - x,y,z,c,off); - return *_data; - } - else return _data[off]; - } - - //! Access to a pixel value \const. - const T& operator()(const unsigned int x, const unsigned int y=0, - const unsigned int z=0, const unsigned int c=0) const { - return const_cast*>(this)->operator()(x,y,z,c); - } - - //! Access to a pixel value. - /** - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param wh Precomputed offset, must be equal to width()*\ref height(). - \param whd Precomputed offset, must be equal to width()*\ref height()*\ref depth(). - \note - - Similar to (but faster than) operator()(). - It uses precomputed offsets to optimize memory access. You may use it to optimize - the reading/writing of several pixel values in the same image (e.g. in a loop). - **/ - T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, - const unsigned long wh, const unsigned long whd=0) { - cimg::unused(wh,whd); - return (*this)(x,y,z,c); - } - - //! Access to a pixel value \const. - const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, - const unsigned long wh, const unsigned long whd=0) const { - cimg::unused(wh,whd); - return (*this)(x,y,z,c); - } -#else - T& operator()(const unsigned int x) { - return _data[x]; - } - - const T& operator()(const unsigned int x) const { - return _data[x]; - } - - T& operator()(const unsigned int x, const unsigned int y) { - return _data[x + y*_width]; - } - - const T& operator()(const unsigned int x, const unsigned int y) const { - return _data[x + y*_width]; - } - - T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) { - return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height]; - } - - const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) const { - return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height]; - } - - T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) { - return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height + - c*(unsigned long)_width*_height*_depth]; - } - - const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) const { - return _data[x + y*(unsigned long)_width + z*(unsigned long)_width*_height + - c*(unsigned long)_width*_height*_depth]; - } - - T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int, - const unsigned long wh) { - return _data[x + y*_width + z*wh]; - } - - const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int, - const unsigned long wh) const { - return _data[x + y*_width + z*wh]; - } - - T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, - const unsigned long wh, const unsigned long whd) { - return _data[x + y*_width + z*wh + c*whd]; - } - - const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c, - const unsigned long wh, const unsigned long whd) const { - return _data[x + y*_width + z*wh + c*whd]; - } -#endif - - //! Implicitely cast an image into a \c T*. - /** - Implicitely cast a \c CImg instance into a \c T* or \c const \c T* pointer, whether the image instance - is \e const or not. The returned pointer points on the first value of the image pixel buffer. - \note - - It simply returns the pointer data() to the pixel buffer. - - This implicit conversion is convenient to test the empty state of images (data() being \c 0 in this case), e.g. - \code - CImg img1(100,100), img2; // 'img1' is a 100x100 image, 'img2' is an empty image. - if (img1) { // Test succeeds, 'img1' is not an empty image. - if (!img2) { // Test succeeds, 'img2' is an empty image. - std::printf("'img1' is not empty, 'img2' is empty."); - } - } - \endcode - - It also allows to use brackets to access pixel values, without need for a \c CImg::operator[](), e.g. - \code - CImg img(100,100); - const float value = img[99]; // Access to value of the last pixel on the first row. - img[510] = 255; // Set pixel value at (10,5). - \endcode - **/ - operator T*() { - return _data; - } - - //! Implicitely cast an image into a \c T* \const. - operator const T*() const { - return _data; - } - - //! Assign a value to all image pixels. - /** - Assign specified \c value to each pixel value of the image instance. - \param value Value that will be assigned to image pixels. - \note - - The image size is never modified. - - The \c value may be casted to pixel type \c T if necessary. - \par Example - \code - CImg img(100,100); // Declare image (with garbage values). - img = 0; // Set all pixel values to '0'. - img = 1.2; // Set all pixel values to '1' (cast of '1.2' as a 'char'). - \endcode - **/ - CImg& operator=(const T value) { - return fill(value); - } - - //! Assign pixels values from a specified expression. - /** - Initialize all pixel values from the specified string \c expression. - \param expression Value string describing the way pixel values are set. - \note - - String parameter \c expression may describe different things: - - If \c expression is a list of values (as in \c "1,2,3,8,3,2"), or a formula (as in \c "(x*y)%255"), - the pixel values are set from specified \c expression and the image size is not modified. - - If \c expression is a filename (as in \c "reference.jpg"), the corresponding image file is loaded and - replace the image instance. The image size is modified if necessary. - \par Example - \code - CImg img1(100,100), img2(img1), img3(img1); // Declare three 100x100 scalar images with unitialized pixel values. - img1 = "0,50,100,150,200,250,200,150,100,50"; // Set pixel values of 'img1' from a value sequence. - img2 = "10*((x*y)%25)"; // Set pixel values of 'img2' from a formula. - img3 = "reference.jpg"; // Set pixel values of 'img3' from a file (image size is modified). - (img1,img2,img3).display(); - \endcode - \image html ref_operator_eq.jpg - **/ - CImg& operator=(const char *const expression) { - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - fill(expression,true); - } catch (CImgException&) { - cimg::exception_mode() = omode; - load(expression); - } - cimg::exception_mode() = omode; - return *this; - } - - //! Copy an image into the current image instance. - /** - Similar to the in-place copy constructor assign(const CImg&). - **/ - template - CImg& operator=(const CImg& img) { - return assign(img); - } - - //! Copy an image into the current image instance \specialization. - CImg& operator=(const CImg& img) { - return assign(img); - } - - //! Copy the content of a display window to the current image instance. - /** - Similar to assign(const CImgDisplay&). - **/ - CImg& operator=(const CImgDisplay& disp) { - disp.snapshot(*this); - return *this; - } - - //! In-place addition operator. - /** - Add specified \c value to all pixels of an image instance. - \param value Value to add. - \note - - Resulting pixel values are casted to fit the pixel type \c T. - For instance, adding \c 0.2 to a \c CImg is possible but does nothing indeed. - - Overflow values are treated as with standard C++ numeric types. For instance, - \code - CImg img(100,100,1,1,255); // Construct a 100x100 image with pixel values '255'. - img+=1; // Add '1' to each pixels -> Overflow. - // here all pixels of image 'img' are equal to '0'. - \endcode - - To prevent value overflow, you may want to consider pixel type \c T as \c float or \c double, - and use cut() after addition. - \par Example - \code - CImg img1("reference.jpg"); // Load a 8-bits RGB image (values in [0,255]). - CImg img2(img1); // Construct a float-valued copy of 'img1'. - img2+=100; // Add '100' to pixel values -> goes out of [0,255] but no problems with floats. - img2.cut(0,255); // Cut values in [0,255] to fit the 'unsigned char' constraint. - img1 = img2; // Rewrite safe result in 'unsigned char' version 'img1'. - const CImg img3 = (img1 + 100).cut(0,255); // Do the same in a more simple and elegant way. - (img1,img2,img3).display(); - \endcode - \image html ref_operator_plus.jpg - **/ - template - CImg& operator+=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=524288) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd + value); - return *this; - } - - //! In-place addition operator. - /** - Add values to image pixels, according to the specified string \c expression. - \param expression Value string describing the way pixel values are added. - \note - - Similar to operator=(const char*), except that it adds values to the pixels of the current image instance, - instead of assigning them. - **/ - CImg& operator+=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator+="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd + mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd + mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)(*ptrd + lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd + mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this+=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place addition operator. - /** - Add values to image pixels, according to the values of the input image \c img. - \param img Input image to add. - \note - - The size of the image instance is never modified. - - It is not mandatory that input image \c img has the same size as the image instance. - If less values are available in \c img, then the values are added periodically. For instance, adding one - WxH scalar image (spectrum() equal to \c 1) to one WxH color image (spectrum() equal to \c 3) - means each color channel will be incremented with the same values at the same locations. - \par Example - \code - CImg img1("reference.jpg"); // Load a RGB color image (img1.spectrum()==3) - const CImg img2(img1.width(),img.height(),1,1,"255*(x/w)^2"); // Construct a scalar shading (img2.spectrum()==1). - img1+=img2; // Add shading to each channel of 'img1'. - img1.cut(0,255); // Prevent [0,255] overflow. - (img2,img1).display(); - \endcode - \image html ref_operator_plus1.jpg - **/ - template - CImg& operator+=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this+=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs& operator++() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=524288) -#endif - cimg_rof(*this,ptrd,T) ++*ptrd; - return *this; - } - - //! In-place increment operator (postfix). - /** - Add \c 1 to all image pixels, and return a new copy of the initial (pre-incremented) image instance. - \note - - Use the prefixed version operator++() if you don't need a copy of the initial - (pre-incremented) image instance, since a useless image copy may be expensive in terms of memory usage. - **/ - CImg operator++(int) { - const CImg copy(*this,false); - ++*this; - return copy; - } - - //! Return a non-shared copy of the image instance. - /** - \note - - Use this operator to ensure you get a non-shared copy of an image instance with same pixel type \c T. - Indeed, the usual copy constructor CImg(const CImg&) returns a shared copy of a shared input image, - and it may be not desirable to work on a regular copy (e.g. for a resize operation) if you have no - informations about the shared state of the input image. - - Writing \c (+img) is equivalent to \c CImg(img,false). - **/ - CImg operator+() const { - return CImg(*this,false); - } - - //! Addition operator. - /** - Similar to operator+=(const t), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator+(const t value) const { - return CImg<_cimg_Tt>(*this,false)+=value; - } - - //! Addition operator. - /** - Similar to operator+=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - CImg operator+(const char *const expression) const { - return CImg(*this,false)+=expression; - } - - //! Addition operator. - /** - Similar to operator+=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator+(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false)+=img; - } - - //! In-place substraction operator. - /** - Similar to operator+=(const t), except that it performs a substraction instead of an addition. - **/ - template - CImg& operator-=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=524288) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd - value); - return *this; - } - - //! In-place substraction operator. - /** - Similar to operator+=(const char*), except that it performs a substraction instead of an addition. - **/ - CImg& operator-=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator-="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd - mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd - mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)(*ptrd - lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd - mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this-=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place substraction operator. - /** - Similar to operator+=(const CImg&), except that it performs a substraction instead of an addition. - **/ - template - CImg& operator-=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this-=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs& operator--() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=524288) -#endif - cimg_rof(*this,ptrd,T) *ptrd = *ptrd-(T)1; - return *this; - } - - //! In-place decrement operator (postfix). - /** - Similar to operator++(int), except that it performs a decrement instead of an increment. - **/ - CImg operator--(int) { - const CImg copy(*this,false); - --*this; - return copy; - } - - //! Replace each pixel by its opposite value. - /** - \note - - If the computed opposite values are out-of-range, they are treated as with standard C++ numeric types. - For instance, the \c unsigned \c char opposite of \c 1 is \c 255. - \par Example - \code - const CImg - img1("reference.jpg"), // Load a RGB color image. - img2 = -img1; // Compute its opposite (in 'unsigned char'). - (img1,img2).display(); - \endcode - \image html ref_operator_minus.jpg - **/ - CImg operator-() const { - return CImg(_width,_height,_depth,_spectrum,(T)0)-=*this; - } - - //! Substraction operator. - /** - Similar to operator-=(const t), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator-(const t value) const { - return CImg<_cimg_Tt>(*this,false)-=value; - } - - //! Substraction operator. - /** - Similar to operator-=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - CImg operator-(const char *const expression) const { - return CImg(*this,false)-=expression; - } - - //! Substraction operator. - /** - Similar to operator-=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator-(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false)-=img; - } - - //! In-place multiplication operator. - /** - Similar to operator+=(const t), except that it performs a multiplication instead of an addition. - **/ - template - CImg& operator*=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=262144) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd * value); - return *this; - } - - //! In-place multiplication operator. - /** - Similar to operator+=(const char*), except that it performs a multiplication instead of an addition. - **/ - CImg& operator*=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator*="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd * mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd * mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)(*ptrd * lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd * mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - mul(CImg(_width,_height,_depth,_spectrum,expression,true)); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place multiplication operator. - /** - Replace the image instance by the matrix multiplication between the image instance and the specified matrix - \c img. - \param img Second operand of the matrix multiplication. - \note - - It does \e not compute a pointwise multiplication between two images. For this purpose, use - mul(const CImg&) instead. - - The size of the image instance can be modified by this operator. - \par Example - \code - CImg A(2,2,1,1, 1,2,3,4); // Construct 2x2 matrix A = [1,2;3,4]. - const CImg X(1,2,1,1, 1,2); // Construct 1x2 vector X = [1;2]. - A*=X; // Assign matrix multiplication A*X to 'A'. - // 'A' is now a 1x2 vector whose values are [5;11]. - \endcode - **/ - template - CImg& operator*=(const CImg& img) { - return ((*this)*img).move_to(*this); - } - - //! Multiplication operator. - /** - Similar to operator*=(const t), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator*(const t value) const { - return CImg<_cimg_Tt>(*this,false)*=value; - } - - //! Multiplication operator. - /** - Similar to operator*=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - CImg operator*(const char *const expression) const { - return CImg(*this,false)*=expression; - } - - //! Multiplication operator. - /** - Similar to operator*=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator*(const CImg& img) const { - if (_width!=img._height || _depth!=1 || _spectrum!=1) - throw CImgArgumentException(_cimg_instance - "operator*(): Invalid multiplication of instance by specified " - "matrix (%u,%u,%u,%u,%p)", - cimg_instance, - img._width,img._height,img._depth,img._spectrum,img._data); - CImg<_cimg_Tt> res(img._width,_height); -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>1024 && img.size()>1024) collapse(2) - cimg_forXY(res,i,j) { - _cimg_Ttdouble value = 0; cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k); res(i,j) = (_cimg_Tt)value; - } -#else - _cimg_Tt *ptrd = res._data; - cimg_forXY(res,i,j) { - _cimg_Ttdouble value = 0; cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k); *(ptrd++) = (_cimg_Tt)value; - } -#endif - return res; - } - - //! In-place division operator. - /** - Similar to operator+=(const t), except that it performs a division instead of an addition. - **/ - template - CImg& operator/=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)(*ptrd / value); - return *this; - } - - //! In-place division operator. - /** - Similar to operator+=(const char*), except that it performs a division instead of an addition. - **/ - CImg& operator/=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator/="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd / mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd / mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)(*ptrd / lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)(*ptrd / mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - div(CImg(_width,_height,_depth,_spectrum,expression,true)); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place division operator. - /** - Replace the image instance by the (right) matrix division between the image instance and the specified - matrix \c img. - \param img Second operand of the matrix division. - \note - - It does \e not compute a pointwise division between two images. For this purpose, use - div(const CImg&) instead. - - It returns the matrix operation \c A*inverse(img). - - The size of the image instance can be modified by this operator. - **/ - template - CImg& operator/=(const CImg& img) { - return (*this*img.get_invert()).move_to(*this); - } - - //! Division operator. - /** - Similar to operator/=(const t), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator/(const t value) const { - return CImg<_cimg_Tt>(*this,false)/=value; - } - - //! Division operator. - /** - Similar to operator/=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - CImg operator/(const char *const expression) const { - return CImg(*this,false)/=expression; - } - - //! Division operator. - /** - Similar to operator/=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator/(const CImg& img) const { - return (*this)*img.get_invert(); - } - - //! In-place modulo operator. - /** - Similar to operator+=(const t), except that it performs a modulo operation instead of an addition. - **/ - template - CImg& operator%=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=16384) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::mod(*ptrd,(T)value); - return *this; - } - - //! In-place modulo operator. - /** - Similar to operator+=(const char*), except that it performs a modulo operation instead of an addition. - **/ - CImg& operator%=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator%="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::mod(*ptrd,(T)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::mod(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)cimg::mod(*ptrd,(T)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::mod(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this%=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place modulo operator. - /** - Similar to operator+=(const CImg&), except that it performs a modulo operation instead of an addition. - **/ - template - CImg& operator%=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this%=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg<_cimg_Tt> operator%(const t value) const { - return CImg<_cimg_Tt>(*this,false)%=value; - } - - //! Modulo operator. - /** - Similar to operator%=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - CImg operator%(const char *const expression) const { - return CImg(*this,false)%=expression; - } - - //! Modulo operator. - /** - Similar to operator%=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary. - **/ - template - CImg<_cimg_Tt> operator%(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false)%=img; - } - - //! In-place bitwise AND operator. - /** - Similar to operator+=(const t), except that it performs a bitwise AND operation instead of an addition. - **/ - template - CImg& operator&=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)((unsigned long)*ptrd & (unsigned long)value); - return *this; - } - - //! In-place bitwise AND operator. - /** - Similar to operator+=(const char*), except that it performs a bitwise AND operation instead of an addition. - **/ - CImg& operator&=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator&="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') - cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd & (unsigned long)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this&=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place bitwise AND operator. - /** - Similar to operator+=(const CImg&), except that it performs a bitwise AND operation instead of an addition. - **/ - template - CImg& operator&=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this&=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg operator&(const t value) const { - return (+*this)&=value; - } - - //! Bitwise AND operator. - /** - Similar to operator&=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - CImg operator&(const char *const expression) const { - return (+*this)&=expression; - } - - //! Bitwise AND operator. - /** - Similar to operator&=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - template - CImg operator&(const CImg& img) const { - return (+*this)&=img; - } - - //! In-place bitwise OR operator. - /** - Similar to operator+=(const t), except that it performs a bitwise OR operation instead of an addition. - **/ - template - CImg& operator|=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)((unsigned long)*ptrd | (unsigned long)value); - return *this; - } - - //! In-place bitwise OR operator. - /** - Similar to operator+=(const char*), except that it performs a bitwise OR operation instead of an addition. - **/ - CImg& operator|=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator|="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') - cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd | (unsigned long)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this|=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place bitwise OR operator. - /** - Similar to operator+=(const CImg&), except that it performs a bitwise OR operation instead of an addition. - **/ - template - CImg& operator|=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this|=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg operator|(const t value) const { - return (+*this)|=value; - } - - //! Bitwise OR operator. - /** - Similar to operator|=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - CImg operator|(const char *const expression) const { - return (+*this)|=expression; - } - - //! Bitwise OR operator. - /** - Similar to operator|=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - template - CImg operator|(const CImg& img) const { - return (+*this)|=img; - } - - //! In-place bitwise XOR operator. - /** - Similar to operator+=(const t), except that it performs a bitwise XOR operation instead of an addition. - \warning - - It does \e not compute the \e power of pixel values. For this purpose, use pow(const t) instead. - **/ - template - CImg& operator^=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)value); - return *this; - } - - //! In-place bitwise XOR operator. - /** - Similar to operator+=(const char*), except that it performs a bitwise XOR operation instead of an addition. - \warning - - It does \e not compute the \e power of pixel values. For this purpose, use pow(const char*) instead. - **/ - CImg& operator^=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator^="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') - cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((unsigned long)*ptrd ^ (unsigned long)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this^=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place bitwise XOR operator. - /** - Similar to operator+=(const CImg&), except that it performs a bitwise XOR operation instead of an addition. - \warning - - It does \e not compute the \e power of pixel values. For this purpose, use pow(const CImg&) instead. - **/ - template - CImg& operator^=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this^=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg operator^(const t value) const { - return (+*this)^=value; - } - - //! Bitwise XOR operator. - /** - Similar to operator^=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - CImg operator^(const char *const expression) const { - return (+*this)^=expression; - } - - //! Bitwise XOR operator. - /** - Similar to operator^=(const CImg&), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - template - CImg operator^(const CImg& img) const { - return (+*this)^=img; - } - - //! In-place bitwise left shift operator. - /** - Similar to operator+=(const t), except that it performs a bitwise left shift instead of an addition. - **/ - template - CImg& operator<<=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)(((long)*ptrd) << (int)value); - return *this; - } - - //! In-place bitwise left shift operator. - /** - Similar to operator+=(const char*), except that it performs a bitwise left shift instead of an addition. - **/ - CImg& operator<<=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator<<="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd << (int)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd << (int)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)((long)*ptrd << (int)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd << (int)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this<<=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place bitwise left shift operator. - /** - Similar to operator+=(const CImg&), except that it performs a bitwise left shift instead of an addition. - **/ - template - CImg& operator<<=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this^=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg operator<<(const t value) const { - return (+*this)<<=value; - } - - //! Bitwise left shift operator. - /** - Similar to operator<<=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - CImg operator<<(const char *const expression) const { - return (+*this)<<=expression; - } - - //! Bitwise left shift operator. - /** - Similar to operator<<=(const CImg&), except that it returns a new image instance instead of - operating in-place. - The pixel type of the returned image is \c T. - **/ - template - CImg operator<<(const CImg& img) const { - return (+*this)<<=img; - } - - //! In-place bitwise right shift operator. - /** - Similar to operator+=(const t), except that it performs a bitwise right shift instead of an addition. - **/ - template - CImg& operator>>=(const t value) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)(((long)*ptrd) >> (int)value); - return *this; - } - - //! In-place bitwise right shift operator. - /** - Similar to operator+=(const char*), except that it performs a bitwise right shift instead of an addition. - **/ - CImg& operator>>=(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator<<="); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd >> (int)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd >> (int)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)((long)*ptrd >> (int)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)((long)*ptrd >> (int)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - cimg::exception_mode() = omode; - *this>>=CImg(_width,_height,_depth,_spectrum,expression,true); - } - cimg::exception_mode() = omode; - return *this; - } - - //! In-place bitwise right shift operator. - /** - Similar to operator+=(const CImg&), except that it performs a bitwise right shift instead of an addition. - **/ - template - CImg& operator>>=(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return *this^=+img; - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs> (int)*(ptrs++)); - for (const t *ptrs = img._data; ptrd> (int)*(ptrs++)); - } - return *this; - } - - //! Bitwise right shift operator. - /** - Similar to operator>>=(const t), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - template - CImg operator>>(const t value) const { - return (+*this)>>=value; - } - - //! Bitwise right shift operator. - /** - Similar to operator>>=(const char*), except that it returns a new image instance instead of operating in-place. - The pixel type of the returned image is \c T. - **/ - CImg operator>>(const char *const expression) const { - return (+*this)>>=expression; - } - - //! Bitwise right shift operator. - /** - Similar to operator>>=(const CImg&), except that it returns a new image instance instead of - operating in-place. - The pixel type of the returned image is \c T. - **/ - template - CImg operator>>(const CImg& img) const { - return (+*this)>>=img; - } - - //! Bitwise inversion operator. - /** - Similar to operator-(), except that it compute the bitwise inverse instead of the opposite value. - **/ - CImg operator~() const { - CImg res(_width,_height,_depth,_spectrum); - const T *ptrs = _data; - cimg_for(res,ptrd,T) { const unsigned long value = (unsigned long)*(ptrs++); *ptrd = (T)~value; } - return res; - } - - //! Test if all pixels of an image have the same value. - /** - Return \c true is all pixels of the image instance are equal to the specified \c value. - \param value Reference value to compare with. - **/ - template - bool operator==(const t value) const { - if (is_empty()) return false; - typedef _cimg_Tt Tt; - bool is_equal = true; - for (T *ptrd = _data + size(); is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)value)) {} - return is_equal; - } - - //! Test if all pixel values of an image follow a specified expression. - /** - Return \c true is all pixels of the image instance are equal to the specified \c expression. - \param expression Value string describing the way pixel values are compared. - **/ - bool operator==(const char *const expression) const { - if (is_empty()) return !*expression; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - bool is_equal = true; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"operator<<="); - const T *ptrs = *expression=='<'?end()-1:_data; - if (*expression=='<') - cimg_rofXYZC(*this,x,y,z,c) { if (!is_equal) break; is_equal = ((double)*(ptrs--)==mp(x,y,z,c)); } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { if (!is_equal) break; is_equal = ((double)*(ptrs++)==mp(x,y,z,c)); } - else cimg_forXYZC(*this,x,y,z,c) { if (!is_equal) break; is_equal = ((double)*(ptrs++)==mp(x,y,z,c)); } - } catch (CImgException&) { - cimg::exception_mode() = omode; - is_equal = (*this==CImg(_width,_height,_depth,_spectrum,expression,true)); - } - cimg::exception_mode() = omode; - return is_equal; - } - - //! Test if two images have the same size and values. - /** - Return \c true if the image instance and the input image \c img have the same dimensions and pixel values, - and \c false otherwise. - \param img Input image to compare with. - \note - - The pixel buffer pointers data() of the two compared images do not have to be the same for operator==() - to return \c true. - Only the dimensions and the pixel values matter. Thus, the comparison can be \c true even for different - pixel types \c T and \c t. - \par Example - \code - const CImg img1(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'float' pixel values). - const CImg img2(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'char' pixel values). - if (img1==img2) { // Test succeeds, image dimensions and values are the same. - std::printf("'img1' and 'img2' have same dimensions and values."); - } - \endcode - **/ - template - bool operator==(const CImg& img) const { - typedef _cimg_Tt Tt; - const unsigned long siz = size(); - bool is_equal = true; - if (siz!=img.size()) return false; - t *ptrs = img._data + siz; - for (T *ptrd = _data + siz; is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)*(--ptrs))) {} - return is_equal; - } - - //! Test if pixels of an image are all different from a value. - /** - Return \c true is all pixels of the image instance are different than the specified \c value. - \param value Reference value to compare with. - **/ - template - bool operator!=(const t value) const { - return !((*this)==value); - } - - //! Test if all pixel values of an image are different from a specified expression. - /** - Return \c true is all pixels of the image instance are different to the specified \c expression. - \param expression Value string describing the way pixel values are compared. - **/ - bool operator!=(const char *const expression) const { - return !((*this)==expression); - } - - //! Test if two images have different sizes or values. - /** - Return \c true if the image instance and the input image \c img have different dimensions or pixel values, - and \c false otherwise. - \param img Input image to compare with. - \note - - Writing \c img1!=img2 is equivalent to \c !(img1==img2). - **/ - template - bool operator!=(const CImg& img) const { - return !((*this)==img); - } - - //! Construct an image list from two images. - /** - Return a new list of image (\c CImgList instance) containing exactly two elements: - - A copy of the image instance, at position [\c 0]. - - A copy of the specified image \c img, at position [\c 1]. - - \param img Input image that will be the second image of the resulting list. - \note - - The family of operator,() is convenient to easily create list of images, but it is also \e quite \e slow - in practice (see warning below). - - Constructed lists contain no shared images. If image instance or input image \c img are shared, they are - inserted as new non-shared copies in the resulting list. - - The pixel type of the returned list may be a superset of the initial pixel type \c T, if necessary. - \warning - - Pipelining operator,() \c N times will perform \c N copies of the entire content of a (growing) image list. - This may become very expensive in terms of speed and used memory. You should avoid using this technique to - build a new CImgList instance from several images, if you are seeking for performance. - Fast insertions of images in an image list are possible with - CImgList::insert(const CImg&,unsigned int,bool) or move_to(CImgList&,unsigned int). - \par Example - \code - const CImg - img1("reference.jpg"), - img2 = img1.get_mirror('x'), - img3 = img2.get_blur(5); - const CImgList list = (img1,img2); // Create list of two elements from 'img1' and 'img2'. - (list,img3).display(); // Display image list containing copies of 'img1','img2' and 'img3'. - \endcode - \image html ref_operator_comma.jpg - **/ - template - CImgList<_cimg_Tt> operator,(const CImg& img) const { - return CImgList<_cimg_Tt>(*this,img); - } - - //! Construct an image list from image instance and an input image list. - /** - Return a new list of images (\c CImgList instance) containing exactly \c list.size() \c + \c 1 elements: - - A copy of the image instance, at position [\c 0]. - - A copy of the specified image list \c list, from positions [\c 1] to [\c list.size()]. - - \param list Input image list that will be appended to the image instance. - \note - - Similar to operator,(const CImg&) const, except that it takes an image list as an argument. - **/ - template - CImgList<_cimg_Tt> operator,(const CImgList& list) const { - return CImgList<_cimg_Tt>(list,false).insert(*this,0); - } - - //! Split image along specified axis. - /** - Return a new list of images (\c CImgList instance) containing the splitted components - of the instance image along the specified axis. - \param axis Splitting axis (can be '\c x','\c y','\c z' or '\c c') - \note - - Similar to get_split(char,int) const, with default second argument. - \par Example - \code - const CImg img("reference.jpg"); // Load a RGB color image. - const CImgList list = (img<'c'); // Get a list of its three R,G,B channels. - (img,list).display(); - \endcode - \image html ref_operator_less.jpg - **/ - CImgList operator<(const char axis) const { - return get_split(axis); - } - - //@} - //------------------------------------- - // - //! \name Instance Characteristics - //@{ - //------------------------------------- - - //! Return the type of image pixel values as a C string. - /** - Return a \c char* string containing the usual type name of the image pixel values - (i.e. a stringified version of the template parameter \c T). - \note - - The returned string may contain spaces (as in \c "unsigned char"). - - If the pixel type \c T does not correspond to a registered type, the string "unknown" is returned. - **/ - static const char* pixel_type() { - return cimg::type::string(); - } - - //! Return the number of image columns. - /** - Return the image width, i.e. the image dimension along the X-axis. - \note - - The width() of an empty image is equal to \c 0. - - width() is typically equal to \c 1 when considering images as \e vectors for matrix calculations. - - width() returns an \c int, although the image width is internally stored as an \c unsigned \c int. - Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving - \c unsigned \c int variables. - Access to the initial \c unsigned \c int variable is possible (though not recommended) by - (*this)._width. - **/ - int width() const { - return (int)_width; - } - - //! Return the number of image rows. - /** - Return the image height, i.e. the image dimension along the Y-axis. - \note - - The height() of an empty image is equal to \c 0. - - height() returns an \c int, although the image height is internally stored as an \c unsigned \c int. - Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving - \c unsigned \c int variables. - Access to the initial \c unsigned \c int variable is possible (though not recommended) by - (*this)._height. - **/ - int height() const { - return (int)_height; - } - - //! Return the number of image slices. - /** - Return the image depth, i.e. the image dimension along the Z-axis. - \note - - The depth() of an empty image is equal to \c 0. - - depth() is typically equal to \c 1 when considering usual 2d images. When depth()\c > \c 1, the image - is said to be \e volumetric. - - depth() returns an \c int, although the image depth is internally stored as an \c unsigned \c int. - Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving - \c unsigned \c int variables. - Access to the initial \c unsigned \c int variable is possible (though not recommended) by - (*this)._depth. - **/ - int depth() const { - return (int)_depth; - } - - //! Return the number of image channels. - /** - Return the number of image channels, i.e. the image dimension along the C-axis. - \note - - The spectrum() of an empty image is equal to \c 0. - - spectrum() is typically equal to \c 1 when considering scalar-valued images, to \c 3 - for RGB-coded color images, and to \c 4 for RGBA-coded color images (with alpha-channel). - The number of channels of an image instance is not limited. The meaning of the pixel values is not linked - up to the number of channels (e.g. a 4-channel image may indifferently stands for a RGBA or CMYK color image). - - spectrum() returns an \c int, although the image spectrum is internally stored as an \c unsigned \c int. - Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving - \c unsigned \c int variables. - Access to the initial \c unsigned \c int variable is possible (though not recommended) by - (*this)._spectrum. - **/ - int spectrum() const { - return (int)_spectrum; - } - - //! Return the total number of pixel values. - /** - Return width()*\ref height()*\ref depth()*\ref spectrum(), - i.e. the total number of values of type \c T in the pixel buffer of the image instance. - \note - - The size() of an empty image is equal to \c 0. - - The allocated memory size for a pixel buffer of a non-shared \c CImg instance is equal to - size()*sizeof(T). - \par Example - \code - const CImg img(100,100,1,3); // Construct new 100x100 color image. - if (img.size()==30000) // Test succeeds. - std::printf("Pixel buffer uses %lu bytes", - img.size()*sizeof(float)); - \endcode - **/ - unsigned long size() const { - return (unsigned long)_width*_height*_depth*_spectrum; - } - - //! Return a pointer to the first pixel value. - /** - Return a \c T*, or a \c const \c T* pointer to the first value in the pixel buffer of the image instance, - whether the instance is \c const or not. - \note - - The data() of an empty image is equal to \c 0 (null pointer). - - The allocated pixel buffer for the image instance starts from \c data() - and goes to data()+\ref size()-1 (included). - - To get the pointer to one particular location of the pixel buffer, use - data(unsigned int,unsigned int,unsigned int,unsigned int) instead. - **/ - T* data() { - return _data; - } - - //! Return a pointer to the first pixel value \const. - const T* data() const { - return _data; - } - - //! Return a pointer to a located pixel value. - /** - Return a \c T*, or a \c const \c T* pointer to the value located at (\c x,\c y,\c z,\c c) in the pixel buffer - of the image instance, - whether the instance is \c const or not. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Writing \c img.data(x,y,z,c) is equivalent to &(img(x,y,z,c)). Thus, this method has the same - properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int). - **/ -#if cimg_verbosity>=3 - T *data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) { - const unsigned long off = (unsigned long)offset(x,y,z,c); - if (off>=size()) - cimg::warn(_cimg_instance - "data(): Invalid pointer request, at coordinates (%u,%u,%u,%u) [offset=%u].", - cimg_instance, - x,y,z,c,off); - return _data + off; - } - - //! Return a pointer to a located pixel value \const. - const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const { - return const_cast*>(this)->data(x,y,z,c); - } -#else - T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) { - return _data + x + y*(unsigned long)_width + z*(unsigned long)_width*_height + - c*(unsigned long)_width*_height*_depth; - } - - const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const { - return _data + x + y*_width + z*(unsigned long)_width*_height + c*(unsigned long)_width*_height*_depth; - } -#endif - - //! Return the offset to a located pixel value, with respect to the beginning of the pixel buffer. - /** - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Writing \c img.data(x,y,z,c) is equivalent to &(img(x,y,z,c)) - img.data(). - Thus, this method has the same properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int). - \par Example - \code - const CImg img(100,100,1,3); // Define a 100x100 RGB-color image. - const long off = img.offset(10,10,0,2); // Get the offset of the blue value of the pixel located at (10,10). - const float val = img[off]; // Get the blue value of this pixel. - \endcode - **/ - long offset(const int x, const int y=0, const int z=0, const int c=0) const { - return x + y*(long)_width + z*(long)_width*_height + c*(long)_width*_height*_depth; - } - - //! Return a CImg::iterator pointing to the first pixel value. - /** - \note - - Equivalent to data(). - - It has been mainly defined for compatibility with STL naming conventions. - **/ - iterator begin() { - return _data; - } - - //! Return a CImg::iterator pointing to the first value of the pixel buffer \const. - const_iterator begin() const { - return _data; - } - - //! Return a CImg::iterator pointing next to the last pixel value. - /** - \note - - Writing \c img.end() is equivalent to img.data() + img.size(). - - It has been mainly defined for compatibility with STL naming conventions. - \warning - - The returned iterator actually points to a value located \e outside the acceptable bounds of the pixel buffer. - Trying to read or write the content of the returned iterator will probably result in a crash. - Use it mainly as a strict upper bound for a CImg::iterator. - \par Example - \code - CImg img(100,100,1,3); // Define a 100x100 RGB color image. - for (CImg::iterator it = img.begin(); it::iterator pointing next to the last pixel value \const. - const_iterator end() const { - return _data + size(); - } - - //! Return a reference to the first pixel value. - /** - \note - - Writing \c img.front() is equivalent to img[0], or img(0,0,0,0). - - It has been mainly defined for compatibility with STL naming conventions. - **/ - T& front() { - return *_data; - } - - //! Return a reference to the first pixel value \const. - const T& front() const { - return *_data; - } - - //! Return a reference to the last pixel value. - /** - \note - - Writing \c img.end() is equivalent to img[img.size()-1], or - img(img.width()-1,img.height()-1,img.depth()-1,img.spectrum()-1). - - It has been mainly defined for compatibility with STL naming conventions. - **/ - T& back() { - return *(_data + size() - 1); - } - - //! Return a reference to the last pixel value \const. - const T& back() const { - return *(_data + size() - 1); - } - - //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions. - /** - Return a reference to the pixel value of the image instance located at a specified \c offset, - or to a specified default value in case of out-of-bounds access. - \param offset Offset to the desired pixel value. - \param out_value Default value returned if \c offset is outside image bounds. - \note - - Writing \c img.at(offset,out_value) is similar to img[offset], except that if \c offset - is outside bounds (e.g. \c offset<0 or \c offset>=img.size()), a reference to a value \c out_value - is safely returned instead. - - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when - you are \e not sure about the validity of the specified pixel offset. - **/ - T& at(const int offset, const T out_value) { - return (offset<0 || offset>=(int)size())?(cimg::temporary(out_value)=out_value):(*this)[offset]; - } - - //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions \const. - T at(const int offset, const T out_value) const { - return (offset<0 || offset>=(int)size())?out_value:(*this)[offset]; - } - - //! Access to a pixel value at a specified offset, using Neumann boundary conditions. - /** - Return a reference to the pixel value of the image instance located at a specified \c offset, - or to the nearest pixel location in the image instance in case of out-of-bounds access. - \param offset Offset to the desired pixel value. - \note - - Similar to at(int,const T), except that an out-of-bounds access returns the value of the - nearest pixel in the image instance, regarding the specified offset, i.e. - - If \c offset<0, then \c img[0] is returned. - - If \c offset>=img.size(), then \c img[img.size()-1] is returned. - - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when - you are \e not sure about the validity of the specified pixel offset. - - If you know your image instance is \e not empty, you may rather use the slightly faster method \c _at(int). - **/ - T& at(const int offset) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "at(): Empty instance.", - cimg_instance); - return _at(offset); - } - - T& _at(const int offset) { - const unsigned int siz = (unsigned int)size(); - return (*this)[offset<0?0:(unsigned int)offset>=siz?siz-1:offset]; - } - - //! Access to a pixel value at a specified offset, using Neumann boundary conditions \const. - T at(const int offset) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "at(): Empty instance.", - cimg_instance); - return _at(offset); - } - - T _at(const int offset) const { - const unsigned int siz = (unsigned int)size(); - return (*this)[offset<0?0:(unsigned int)offset>=siz?siz-1:offset]; - } - - //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate. - /** - Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c), - or to a specified default value in case of out-of-bounds access along the X-axis. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c (\c x,\c y,\c z,\c c) is outside image bounds. - \note - - Similar to operator()(), except that an out-of-bounds access along the X-axis returns the specified value - \c out_value. - - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when - you are \e not sure about the validity of the specified pixel coordinates. - \warning - - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. - **/ - T& atX(const int x, const int y, const int z, const int c, const T out_value) { - return (x<0 || x>=width())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate \const. - T atX(const int x, const int y, const int z, const int c, const T out_value) const { - return (x<0 || x>=width())?out_value:(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate. - /** - Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c), - or to the nearest pixel location in the image instance in case of out-of-bounds access along the X-axis. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Similar to at(int,int,int,int,const T), except that an out-of-bounds access returns the value of the - nearest pixel in the image instance, regarding the specified X-coordinate. - - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when - you are \e not sure about the validity of the specified pixel coordinates. - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _at(int,int,int,int). - \warning - - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. - **/ - T& atX(const int x, const int y=0, const int z=0, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atX(): Empty instance.", - cimg_instance); - return _atX(x,y,z,c); - } - - T& _atX(const int x, const int y=0, const int z=0, const int c=0) { - return (*this)(x<0?0:(x>=width()?width()-1:x),y,z,c); - } - - //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate \const. - T atX(const int x, const int y=0, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atX(): Empty instance.", - cimg_instance); - return _atX(x,y,z,c); - } - - T _atX(const int x, const int y=0, const int z=0, const int c=0) const { - return (*this)(x<0?0:(x>=width()?width()-1:x),y,z,c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y-coordinates. - /** - Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on X and Y-coordinates. - **/ - T& atXY(const int x, const int y, const int z, const int c, const T out_value) { - return (x<0 || y<0 || x>=width() || y>=height())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y coordinates \const. - T atXY(const int x, const int y, const int z, const int c, const T out_value) const { - return (x<0 || y<0 || x>=width() || y>=height())?out_value:(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates. - /** - Similar to atX(int,int,int,int), except that boundary checking is performed both on X and Y-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _atXY(int,int,int,int). - **/ - T& atXY(const int x, const int y, const int z=0, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atXY(): Empty instance.", - cimg_instance); - return _atXY(x,y,z,c); - } - - T& _atXY(const int x, const int y, const int z=0, const int c=0) { - return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y),z,c); - } - - //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates \const. - T atXY(const int x, const int y, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atXY(): Empty instance.", - cimg_instance); - return _atXY(x,y,z,c); - } - - T _atXY(const int x, const int y, const int z=0, const int c=0) const { - return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y),z,c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates. - /** - Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on - X,Y and Z-coordinates. - **/ - T& atXYZ(const int x, const int y, const int z, const int c, const T out_value) { - return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())? - (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates \const. - T atXYZ(const int x, const int y, const int z, const int c, const T out_value) const { - return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())?out_value:(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates. - /** - Similar to atX(int,int,int,int), except that boundary checking is performed both on X,Y and Z-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _atXYZ(int,int,int,int). - **/ - T& atXYZ(const int x, const int y, const int z, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atXYZ(): Empty instance.", - cimg_instance); - return _atXYZ(x,y,z,c); - } - - T& _atXYZ(const int x, const int y, const int z, const int c=0) { - return (*this)(x<0?0:(x>=width()?width()-1:x),y<0?0:(y>=height()?height()-1:y), - z<0?0:(z>=depth()?depth()-1:z),c); - } - - //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates \const. - T atXYZ(const int x, const int y, const int z, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atXYZ(): Empty instance.", - cimg_instance); - return _atXYZ(x,y,z,c); - } - - T _atXYZ(const int x, const int y, const int z, const int c=0) const { - return (*this)(x<0?0:(x>=width()?width()-1:x),y<0?0:(y>=height()?height()-1:y), - z<0?0:(z>=depth()?depth()-1:z),c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions. - /** - Similar to atX(int,int,int,int,const T), except that boundary checking is performed on all - X,Y,Z and C-coordinates. - **/ - T& atXYZC(const int x, const int y, const int z, const int c, const T out_value) { - return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())? - (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c); - } - - //! Access to a pixel value, using Dirichlet boundary conditions \const. - T atXYZC(const int x, const int y, const int z, const int c, const T out_value) const { - return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())?out_value: - (*this)(x,y,z,c); - } - - //! Access to a pixel value, using Neumann boundary conditions. - /** - Similar to atX(int,int,int,int), except that boundary checking is performed on all X,Y,Z and C-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _atXYZC(int,int,int,int). - **/ - T& atXYZC(const int x, const int y, const int z, const int c) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atXYZC(): Empty instance.", - cimg_instance); - return _atXYZC(x,y,z,c); - } - - T& _atXYZC(const int x, const int y, const int z, const int c) { - return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y), - z<0?0:(z>=depth()?depth()-1:z), c<0?0:(c>=spectrum()?spectrum()-1:c)); - } - - //! Access to a pixel value, using Neumann boundary conditions \const. - T atXYZC(const int x, const int y, const int z, const int c) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "atXYZC(): Empty instance.", - cimg_instance); - return _atXYZC(x,y,z,c); - } - - T _atXYZC(const int x, const int y, const int z, const int c) const { - return (*this)(x<0?0:(x>=width()?width()-1:x), y<0?0:(y>=height()?height()-1:y), - z<0?0:(z>=depth()?depth()-1:z), c<0?0:(c>=spectrum()?spectrum()-1:c)); - } - - //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X-coordinate. - /** - Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), - or a specified default value in case of out-of-bounds access along the X-axis. - \param fx X-coordinate of the pixel value (float-valued). - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds. - \note - - Similar to atX(int,int,int,int,const T), except that the returned pixel value is approximated by - a linear interpolation along the X-axis, if corresponding coordinates are not integers. - - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued. - \warning - - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. - **/ - Tfloat linear_atX(const float fx, const int y, const int z, const int c, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), nx = x + 1; - const float - dx = fx - x; - const Tfloat - Ic = (Tfloat)atX(x,y,z,c,out_value), In = (Tfloat)atXY(nx,y,z,c,out_value); - return Ic + dx*(In-Ic); - } - - //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X-coordinate. - /** - Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), - or the value of the nearest pixel location in the image instance in case of out-of-bounds access along - the X-axis. - \param fx X-coordinate of the pixel value (float-valued). - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Similar to linear_atX(float,int,int,int,const T) const, except that an out-of-bounds access returns - the value of the nearest pixel in the image instance, regarding the specified X-coordinate. - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _linear_atX(float,int,int,int). - \warning - - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. - **/ - Tfloat linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "linear_atX(): Empty instance.", - cimg_instance); - - return _linear_atX(fx,y,z,c); - } - - Tfloat _linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx); - const unsigned int - x = (unsigned int)nfx; - const float - dx = nfx - x; - const unsigned int - nx = dx>0?x+1:x; - const Tfloat - Ic = (Tfloat)(*this)(x,y,z,c), In = (Tfloat)(*this)(nx,y,z,c); - return Ic + dx*(In-Ic); - } - - //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X and Y-coordinates. - /** - Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the - boundary checking are achieved both for X and Y-coordinates. - **/ - Tfloat linear_atXY(const float fx, const float fy, const int z, const int c, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), nx = x + 1, - y = (int)fy - (fy>=0?0:1), ny = y + 1; - const float - dx = fx - x, - dy = fy - y; - const Tfloat - Icc = (Tfloat)atXY(x,y,z,c,out_value), Inc = (Tfloat)atXY(nx,y,z,c,out_value), - Icn = (Tfloat)atXY(x,ny,z,c,out_value), Inn = (Tfloat)atXY(nx,ny,z,c,out_value); - return Icc + dx*(Inc-Icc + dy*(Icc+Inn-Icn-Inc)) + dy*(Icn-Icc); - } - - //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X and Y-coordinates. - /** - Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking - are achieved both for X and Y-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _linear_atXY(float,float,int,int). - **/ - Tfloat linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "linear_atXY(): Empty instance.", - cimg_instance); - - return _linear_atXY(fx,fy,z,c); - } - - Tfloat _linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx), - nfy = fy<0?0:(fy>_height-1?_height-1:fy); - const unsigned int - x = (unsigned int)nfx, - y = (unsigned int)nfy; - const float - dx = nfx - x, - dy = nfy - y; - const unsigned int - nx = dx>0?x+1:x, - ny = dy>0?y+1:y; - const Tfloat - Icc = (Tfloat)(*this)(x,y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c), - Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c); - return Icc + dx*(Inc-Icc + dy*(Icc+Inn-Icn-Inc)) + dy*(Icn-Icc); - } - - //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates. - /** - Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the - boundary checking are achieved both for X,Y and Z-coordinates. - **/ - Tfloat linear_atXYZ(const float fx, const float fy, const float fz, const int c, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), nx = x + 1, - y = (int)fy - (fy>=0?0:1), ny = y + 1, - z = (int)fz - (fz>=0?0:1), nz = z + 1; - const float - dx = fx - x, - dy = fy - y, - dz = fz - z; - const Tfloat - Iccc = (Tfloat)atXYZ(x,y,z,c,out_value), Incc = (Tfloat)atXYZ(nx,y,z,c,out_value), - Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value), Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value), - Iccn = (Tfloat)atXYZ(x,y,nz,c,out_value), Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value), - Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value), Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value); - return Iccc + - dx*(Incc-Iccc + - dy*(Iccc+Innc-Icnc-Incc + - dz*(Iccn+Innn+Icnc+Incc-Icnn-Incn-Iccc-Innc)) + - dz*(Iccc+Incn-Iccn-Incc)) + - dy*(Icnc-Iccc + - dz*(Iccc+Icnn-Iccn-Icnc)) + - dz*(Iccn-Iccc); - } - - //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X,Y and Z-coordinates. - /** - Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking - are achieved both for X,Y and Z-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _linear_atXYZ(float,float,float,int). - **/ - Tfloat linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "linear_atXYZ(): Empty instance.", - cimg_instance); - - return _linear_atXYZ(fx,fy,fz,c); - } - - Tfloat _linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx), - nfy = fy<0?0:(fy>_height-1?_height-1:fy), - nfz = fz<0?0:(fz>_depth-1?_depth-1:fz); - const unsigned int - x = (unsigned int)nfx, - y = (unsigned int)nfy, - z = (unsigned int)nfz; - const float - dx = nfx - x, - dy = nfy - y, - dz = nfz - z; - const unsigned int - nx = dx>0?x+1:x, - ny = dy>0?y+1:y, - nz = dz>0?z+1:z; - const Tfloat - Iccc = (Tfloat)(*this)(x,y,z,c), Incc = (Tfloat)(*this)(nx,y,z,c), - Icnc = (Tfloat)(*this)(x,ny,z,c), Innc = (Tfloat)(*this)(nx,ny,z,c), - Iccn = (Tfloat)(*this)(x,y,nz,c), Incn = (Tfloat)(*this)(nx,y,nz,c), - Icnn = (Tfloat)(*this)(x,ny,nz,c), Innn = (Tfloat)(*this)(nx,ny,nz,c); - return Iccc + - dx*(Incc-Iccc + - dy*(Iccc+Innc-Icnc-Incc + - dz*(Iccn+Innn+Icnc+Incc-Icnn-Incn-Iccc-Innc)) + - dz*(Iccc+Incn-Iccn-Incc)) + - dy*(Icnc-Iccc + - dz*(Iccc+Icnn-Iccn-Icnc)) + - dz*(Iccn-Iccc); - } - - //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for all X,Y,Z,C-coordinates. - /** - Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the - boundary checking are achieved for all X,Y,Z and C-coordinates. - **/ - Tfloat linear_atXYZC(const float fx, const float fy, const float fz, const float fc, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), nx = x + 1, - y = (int)fy - (fy>=0?0:1), ny = y + 1, - z = (int)fz - (fz>=0?0:1), nz = z + 1, - c = (int)fc - (fc>=0?0:1), nc = c + 1; - const float - dx = fx - x, - dy = fy - y, - dz = fz - z, - dc = fc - c; - const Tfloat - Icccc = (Tfloat)atXYZC(x,y,z,c,out_value), Inccc = (Tfloat)atXYZC(nx,y,z,c,out_value), - Icncc = (Tfloat)atXYZC(x,ny,z,c,out_value), Inncc = (Tfloat)atXYZC(nx,ny,z,c,out_value), - Iccnc = (Tfloat)atXYZC(x,y,nz,c,out_value), Incnc = (Tfloat)atXYZC(nx,y,nz,c,out_value), - Icnnc = (Tfloat)atXYZC(x,ny,nz,c,out_value), Innnc = (Tfloat)atXYZC(nx,ny,nz,c,out_value), - Icccn = (Tfloat)atXYZC(x,y,z,nc,out_value), Inccn = (Tfloat)atXYZC(nx,y,z,nc,out_value), - Icncn = (Tfloat)atXYZC(x,ny,z,nc,out_value), Inncn = (Tfloat)atXYZC(nx,ny,z,nc,out_value), - Iccnn = (Tfloat)atXYZC(x,y,nz,nc,out_value), Incnn = (Tfloat)atXYZC(nx,y,nz,nc,out_value), - Icnnn = (Tfloat)atXYZC(x,ny,nz,nc,out_value), Innnn = (Tfloat)atXYZC(nx,ny,nz,nc,out_value); - return Icccc + - dx*(Inccc-Icccc + - dy*(Icccc+Inncc-Icncc-Inccc + - dz*(Iccnc+Innnc+Icncc+Inccc-Icnnc-Incnc-Icccc-Inncc + - dc*(Iccnn+Innnn+Icncn+Inccn+Icnnc+Incnc+Icccc+Inncc- - Icnnn-Incnn-Icccn-Inncn-Iccnc-Innnc-Icncc-Inccc)) + - dc*(Icccn+Inncn+Icncc+Inccc-Icncn-Inccn-Icccc-Inncc)) + - dz*(Icccc+Incnc-Iccnc-Inccc + - dc*(Icccn+Incnn+Iccnc+Inccc-Iccnn-Inccn-Icccc-Incnc)) + - dc*(Icccc+Inccn-Inccc-Icccn)) + - dy*(Icncc-Icccc + - dz*(Icccc+Icnnc-Iccnc-Icncc + - dc*(Icccn+Icnnn+Iccnc+Icncc-Iccnn-Icncn-Icccc-Icnnc)) + - dc*(Icccc+Icncn-Icncc-Icccn)) + - dz*(Iccnc-Icccc + - dc*(Icccc+Iccnn-Iccnc-Icccn)) + - dc*(Icccn-Icccc); - } - - //! Return pixel value, using linear interpolation and Neumann boundary conditions for all X,Y,Z and C-coordinates. - /** - Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking - are achieved for all X,Y,Z and C-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _linear_atXYZC(float,float,float,float). - **/ - Tfloat linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "linear_atXYZC(): Empty instance.", - cimg_instance); - - return _linear_atXYZC(fx,fy,fz,fc); - } - - Tfloat _linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx), - nfy = fy<0?0:(fy>_height-1?_height-1:fy), - nfz = fz<0?0:(fz>_depth-1?_depth-1:fz), - nfc = fc<0?0:(fc>_spectrum-1?_spectrum-1:fc); - const unsigned int - x = (unsigned int)nfx, - y = (unsigned int)nfy, - z = (unsigned int)nfz, - c = (unsigned int)nfc; - const float - dx = nfx - x, - dy = nfy - y, - dz = nfz - z, - dc = nfc - c; - const unsigned int - nx = dx>0?x+1:x, - ny = dy>0?y+1:y, - nz = dz>0?z+1:z, - nc = dc>0?c+1:c; - const Tfloat - Icccc = (Tfloat)(*this)(x,y,z,c), Inccc = (Tfloat)(*this)(nx,y,z,c), - Icncc = (Tfloat)(*this)(x,ny,z,c), Inncc = (Tfloat)(*this)(nx,ny,z,c), - Iccnc = (Tfloat)(*this)(x,y,nz,c), Incnc = (Tfloat)(*this)(nx,y,nz,c), - Icnnc = (Tfloat)(*this)(x,ny,nz,c), Innnc = (Tfloat)(*this)(nx,ny,nz,c), - Icccn = (Tfloat)(*this)(x,y,z,nc), Inccn = (Tfloat)(*this)(nx,y,z,nc), - Icncn = (Tfloat)(*this)(x,ny,z,nc), Inncn = (Tfloat)(*this)(nx,ny,z,nc), - Iccnn = (Tfloat)(*this)(x,y,nz,nc), Incnn = (Tfloat)(*this)(nx,y,nz,nc), - Icnnn = (Tfloat)(*this)(x,ny,nz,nc), Innnn = (Tfloat)(*this)(nx,ny,nz,nc); - return Icccc + - dx*(Inccc-Icccc + - dy*(Icccc+Inncc-Icncc-Inccc + - dz*(Iccnc+Innnc+Icncc+Inccc-Icnnc-Incnc-Icccc-Inncc + - dc*(Iccnn+Innnn+Icncn+Inccn+Icnnc+Incnc+Icccc+Inncc- - Icnnn-Incnn-Icccn-Inncn-Iccnc-Innnc-Icncc-Inccc)) + - dc*(Icccn+Inncn+Icncc+Inccc-Icncn-Inccn-Icccc-Inncc)) + - dz*(Icccc+Incnc-Iccnc-Inccc + - dc*(Icccn+Incnn+Iccnc+Inccc-Iccnn-Inccn-Icccc-Incnc)) + - dc*(Icccc+Inccn-Inccc-Icccn)) + - dy*(Icncc-Icccc + - dz*(Icccc+Icnnc-Iccnc-Icncc + - dc*(Icccn+Icnnn+Iccnc+Icncc-Iccnn-Icncn-Icccc-Icnnc)) + - dc*(Icccc+Icncn-Icncc-Icccn)) + - dz*(Iccnc-Icccc + - dc*(Icccc+Iccnn-Iccnc-Icccn)) + - dc*(Icccn-Icccc); - } - - //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate. - /** - Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), - or a specified default value in case of out-of-bounds access along the X-axis. - The cubic interpolation uses Hermite splines. - \param fx d X-coordinate of the pixel value (float-valued). - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds. - \note - - Similar to linear_atX(float,int,int,int,const T) const, except that the returned pixel value is - approximated by a \e cubic interpolation along the X-axis. - - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued. - \warning - - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. - **/ - Tfloat cubic_atX(const float fx, const int y, const int z, const int c, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2; - const float - dx = fx - x; - const Tfloat - Ip = (Tfloat)atX(px,y,z,c,out_value), Ic = (Tfloat)atX(x,y,z,c,out_value), - In = (Tfloat)atX(nx,y,z,c,out_value), Ia = (Tfloat)atX(ax,y,z,c,out_value); - return Ic + 0.5f*(dx*(-Ip+In) + dx*dx*(2*Ip-5*Ic+4*In-Ia) + dx*dx*dx*(-Ip+3*Ic-3*In+Ia)); - } - - //! Return damped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate. - /** - Similar to cubic_atX(float,int,int,int,const T) const, except that you can specify the authorized minimum - and maximum of the returned value. - **/ - Tfloat cubic_atX(const float fx, const int y, const int z, const int c, const T out_value, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = cubic_atX(fx,y,z,c,out_value); - return valmax_value?max_value:val; - } - - //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate. - /** - Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c), - or the value of the nearest pixel location in the image instance in case of out-of-bounds access - along the X-axis. The cubic interpolation uses Hermite splines. - \param fx X-coordinate of the pixel value (float-valued). - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Similar to cubic_atX(float,int,int,int,const T) const, except that the returned pixel value is - approximated by a cubic interpolation along the X-axis. - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _cubic_atX(float,int,int,int). - \warning - - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds. - **/ - Tfloat cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "cubic_atX(): Empty instance.", - cimg_instance); - return _cubic_atX(fx,y,z,c); - } - - Tfloat _cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx); - const int - x = (int)nfx; - const float - dx = nfx - x; - const int - px = x-1<0?0:x-1, nx = dx>0?x+1:x, ax = x+2>=width()?width()-1:x+2; - const Tfloat - Ip = (Tfloat)(*this)(px,y,z,c), Ic = (Tfloat)(*this)(x,y,z,c), - In = (Tfloat)(*this)(nx,y,z,c), Ia = (Tfloat)(*this)(ax,y,z,c); - return Ic + 0.5f*(dx*(-Ip+In) + dx*dx*(2*Ip-5*Ic+4*In-Ia) + dx*dx*dx*(-Ip+3*Ic-3*In+Ia)); - } - - //! Return damped pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate. - /** - Similar to cubic_atX(float,int,int,int) const, except that you can specify the authorized minimum and maximum - of the returned value. - **/ - Tfloat cubic_atX(const float fx, const int y, const int z, const int c, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = cubic_atX(fx,y,z,c); - return valmax_value?max_value:val; - } - - Tfloat _cubic_atX(const float fx, const int y, const int z, const int c, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = _cubic_atX(fx,y,z,c); - return valmax_value?max_value:val; - } - - //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X and Y-coordinates. - /** - Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking - are achieved both for X and Y-coordinates. - **/ - Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2, - y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2; - const float dx = fx - x, dy = fy - y; - const Tfloat - Ipp = (Tfloat)atXY(px,py,z,c,out_value), Icp = (Tfloat)atXY(x,py,z,c,out_value), - Inp = (Tfloat)atXY(nx,py,z,c,out_value), Iap = (Tfloat)atXY(ax,py,z,c,out_value), - Ip = Icp + 0.5f*(dx*(-Ipp+Inp) + dx*dx*(2*Ipp-5*Icp+4*Inp-Iap) + dx*dx*dx*(-Ipp+3*Icp-3*Inp+Iap)), - Ipc = (Tfloat)atXY(px,y,z,c,out_value), Icc = (Tfloat)atXY(x, y,z,c,out_value), - Inc = (Tfloat)atXY(nx,y,z,c,out_value), Iac = (Tfloat)atXY(ax,y,z,c,out_value), - Ic = Icc + 0.5f*(dx*(-Ipc+Inc) + dx*dx*(2*Ipc-5*Icc+4*Inc-Iac) + dx*dx*dx*(-Ipc+3*Icc-3*Inc+Iac)), - Ipn = (Tfloat)atXY(px,ny,z,c,out_value), Icn = (Tfloat)atXY(x,ny,z,c,out_value), - Inn = (Tfloat)atXY(nx,ny,z,c,out_value), Ian = (Tfloat)atXY(ax,ny,z,c,out_value), - In = Icn + 0.5f*(dx*(-Ipn+Inn) + dx*dx*(2*Ipn-5*Icn+4*Inn-Ian) + dx*dx*dx*(-Ipn+3*Icn-3*Inn+Ian)), - Ipa = (Tfloat)atXY(px,ay,z,c,out_value), Ica = (Tfloat)atXY(x,ay,z,c,out_value), - Ina = (Tfloat)atXY(nx,ay,z,c,out_value), Iaa = (Tfloat)atXY(ax,ay,z,c,out_value), - Ia = Ica + 0.5f*(dx*(-Ipa+Ina) + dx*dx*(2*Ipa-5*Ica+4*Ina-Iaa) + dx*dx*dx*(-Ipa+3*Ica-3*Ina+Iaa)); - return Ic + 0.5f*(dy*(-Ip+In) + dy*dy*(2*Ip-5*Ic+4*In-Ia) + dy*dy*dy*(-Ip+3*Ic-3*In+Ia)); - } - - //! Return damped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y-coordinates. - /** - Similar to cubic_atXY(float,float,int,int,const T) const, except that you can specify the authorized - minimum and maximum of the returned value. - **/ - Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, const T out_value, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = cubic_atXY(fx,fy,z,c,out_value); - return valmax_value?max_value:val; - } - - //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X and Y-coordinates. - /** - Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking - are achieved for both X and Y-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _cubic_atXY(float,float,int,int). - **/ - Tfloat cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "cubic_atXY(): Empty instance.", - cimg_instance); - return _cubic_atXY(fx,fy,z,c); - } - - Tfloat _cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx), - nfy = fy<0?0:(fy>_height-1?_height-1:fy); - const int x = (int)nfx, y = (int)nfy; - const float dx = nfx - x, dy = nfy - y; - const int - px = x-1<0?0:x-1, nx = dx>0?x+1:x, ax = x+2>=width()?width()-1:x+2, - py = y-1<0?0:y-1, ny = dy>0?y+1:y, ay = y+2>=height()?height()-1:y+2; - const Tfloat - Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c), - Iap = (Tfloat)(*this)(ax,py,z,c), - Ip = Icp + 0.5f*(dx*(-Ipp+Inp) + dx*dx*(2*Ipp-5*Icp+4*Inp-Iap) + dx*dx*dx*(-Ipp+3*Icp-3*Inp+Iap)), - Ipc = (Tfloat)(*this)(px,y,z,c), Icc = (Tfloat)(*this)(x, y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c), - Iac = (Tfloat)(*this)(ax,y,z,c), - Ic = Icc + 0.5f*(dx*(-Ipc+Inc) + dx*dx*(2*Ipc-5*Icc+4*Inc-Iac) + dx*dx*dx*(-Ipc+3*Icc-3*Inc+Iac)), - Ipn = (Tfloat)(*this)(px,ny,z,c), Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c), - Ian = (Tfloat)(*this)(ax,ny,z,c), - In = Icn + 0.5f*(dx*(-Ipn+Inn) + dx*dx*(2*Ipn-5*Icn+4*Inn-Ian) + dx*dx*dx*(-Ipn+3*Icn-3*Inn+Ian)), - Ipa = (Tfloat)(*this)(px,ay,z,c), Ica = (Tfloat)(*this)(x,ay,z,c), Ina = (Tfloat)(*this)(nx,ay,z,c), - Iaa = (Tfloat)(*this)(ax,ay,z,c), - Ia = Ica + 0.5f*(dx*(-Ipa+Ina) + dx*dx*(2*Ipa-5*Ica+4*Ina-Iaa) + dx*dx*dx*(-Ipa+3*Ica-3*Ina+Iaa)); - return Ic + 0.5f*(dy*(-Ip+In) + dy*dy*(2*Ip-5*Ic+4*In-Ia) + dy*dy*dy*(-Ip+3*Ic-3*In+Ia)); - } - - //! Return damped pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y-coordinates. - /** - Similar to cubic_atXY(float,float,int,int) const, except that you can specify the authorized minimum and - maximum of the returned value. - **/ - Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = cubic_atXY(fx,fy,z,c); - return valmax_value?max_value:val; - } - - Tfloat _cubic_atXY(const float fx, const float fy, const int z, const int c, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = _cubic_atXY(fx,fy,z,c); - return valmax_value?max_value:val; - } - - //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates. - /** - Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking - are achieved both for X,Y and Z-coordinates. - **/ - Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, const T out_value) const { - const int - x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2, - y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2, - z = (int)fz - (fz>=0?0:1), pz = z - 1, nz = z + 1, az = z + 2; - const float dx = fx - x, dy = fy - y, dz = fz - z; - const Tfloat - Ippp = (Tfloat)atXYZ(px,py,pz,c,out_value), Icpp = (Tfloat)atXYZ(x,py,pz,c,out_value), - Inpp = (Tfloat)atXYZ(nx,py,pz,c,out_value), Iapp = (Tfloat)atXYZ(ax,py,pz,c,out_value), - Ipp = Icpp + 0.5f*(dx*(-Ippp+Inpp) + dx*dx*(2*Ippp-5*Icpp+4*Inpp-Iapp) + dx*dx*dx*(-Ippp+3*Icpp-3*Inpp+Iapp)), - Ipcp = (Tfloat)atXYZ(px,y,pz,c,out_value), Iccp = (Tfloat)atXYZ(x, y,pz,c,out_value), - Incp = (Tfloat)atXYZ(nx,y,pz,c,out_value), Iacp = (Tfloat)atXYZ(ax,y,pz,c,out_value), - Icp = Iccp + 0.5f*(dx*(-Ipcp+Incp) + dx*dx*(2*Ipcp-5*Iccp+4*Incp-Iacp) + dx*dx*dx*(-Ipcp+3*Iccp-3*Incp+Iacp)), - Ipnp = (Tfloat)atXYZ(px,ny,pz,c,out_value), Icnp = (Tfloat)atXYZ(x,ny,pz,c,out_value), - Innp = (Tfloat)atXYZ(nx,ny,pz,c,out_value), Ianp = (Tfloat)atXYZ(ax,ny,pz,c,out_value), - Inp = Icnp + 0.5f*(dx*(-Ipnp+Innp) + dx*dx*(2*Ipnp-5*Icnp+4*Innp-Ianp) + dx*dx*dx*(-Ipnp+3*Icnp-3*Innp+Ianp)), - Ipap = (Tfloat)atXYZ(px,ay,pz,c,out_value), Icap = (Tfloat)atXYZ(x,ay,pz,c,out_value), - Inap = (Tfloat)atXYZ(nx,ay,pz,c,out_value), Iaap = (Tfloat)atXYZ(ax,ay,pz,c,out_value), - Iap = Icap + 0.5f*(dx*(-Ipap+Inap) + dx*dx*(2*Ipap-5*Icap+4*Inap-Iaap) + dx*dx*dx*(-Ipap+3*Icap-3*Inap+Iaap)), - Ip = Icp + 0.5f*(dy*(-Ipp+Inp) + dy*dy*(2*Ipp-5*Icp+4*Inp-Iap) + dy*dy*dy*(-Ipp+3*Icp-3*Inp+Iap)), - Ippc = (Tfloat)atXYZ(px,py,z,c,out_value), Icpc = (Tfloat)atXYZ(x,py,z,c,out_value), - Inpc = (Tfloat)atXYZ(nx,py,z,c,out_value), Iapc = (Tfloat)atXYZ(ax,py,z,c,out_value), - Ipc = Icpc + 0.5f*(dx*(-Ippc+Inpc) + dx*dx*(2*Ippc-5*Icpc+4*Inpc-Iapc) + dx*dx*dx*(-Ippc+3*Icpc-3*Inpc+Iapc)), - Ipcc = (Tfloat)atXYZ(px,y,z,c,out_value), Iccc = (Tfloat)atXYZ(x, y,z,c,out_value), - Incc = (Tfloat)atXYZ(nx,y,z,c,out_value), Iacc = (Tfloat)atXYZ(ax,y,z,c,out_value), - Icc = Iccc + 0.5f*(dx*(-Ipcc+Incc) + dx*dx*(2*Ipcc-5*Iccc+4*Incc-Iacc) + dx*dx*dx*(-Ipcc+3*Iccc-3*Incc+Iacc)), - Ipnc = (Tfloat)atXYZ(px,ny,z,c,out_value), Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value), - Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value), Ianc = (Tfloat)atXYZ(ax,ny,z,c,out_value), - Inc = Icnc + 0.5f*(dx*(-Ipnc+Innc) + dx*dx*(2*Ipnc-5*Icnc+4*Innc-Ianc) + dx*dx*dx*(-Ipnc+3*Icnc-3*Innc+Ianc)), - Ipac = (Tfloat)atXYZ(px,ay,z,c,out_value), Icac = (Tfloat)atXYZ(x,ay,z,c,out_value), - Inac = (Tfloat)atXYZ(nx,ay,z,c,out_value), Iaac = (Tfloat)atXYZ(ax,ay,z,c,out_value), - Iac = Icac + 0.5f*(dx*(-Ipac+Inac) + dx*dx*(2*Ipac-5*Icac+4*Inac-Iaac) + dx*dx*dx*(-Ipac+3*Icac-3*Inac+Iaac)), - Ic = Icc + 0.5f*(dy*(-Ipc+Inc) + dy*dy*(2*Ipc-5*Icc+4*Inc-Iac) + dy*dy*dy*(-Ipc+3*Icc-3*Inc+Iac)), - Ippn = (Tfloat)atXYZ(px,py,nz,c,out_value), Icpn = (Tfloat)atXYZ(x,py,nz,c,out_value), - Inpn = (Tfloat)atXYZ(nx,py,nz,c,out_value), Iapn = (Tfloat)atXYZ(ax,py,nz,c,out_value), - Ipn = Icpn + 0.5f*(dx*(-Ippn+Inpn) + dx*dx*(2*Ippn-5*Icpn+4*Inpn-Iapn) + dx*dx*dx*(-Ippn+3*Icpn-3*Inpn+Iapn)), - Ipcn = (Tfloat)atXYZ(px,y,nz,c,out_value), Iccn = (Tfloat)atXYZ(x, y,nz,c,out_value), - Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value), Iacn = (Tfloat)atXYZ(ax,y,nz,c,out_value), - Icn = Iccn + 0.5f*(dx*(-Ipcn+Incn) + dx*dx*(2*Ipcn-5*Iccn+4*Incn-Iacn) + dx*dx*dx*(-Ipcn+3*Iccn-3*Incn+Iacn)), - Ipnn = (Tfloat)atXYZ(px,ny,nz,c,out_value), Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value), - Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value), Iann = (Tfloat)atXYZ(ax,ny,nz,c,out_value), - Inn = Icnn + 0.5f*(dx*(-Ipnn+Innn) + dx*dx*(2*Ipnn-5*Icnn+4*Innn-Iann) + dx*dx*dx*(-Ipnn+3*Icnn-3*Innn+Iann)), - Ipan = (Tfloat)atXYZ(px,ay,nz,c,out_value), Ican = (Tfloat)atXYZ(x,ay,nz,c,out_value), - Inan = (Tfloat)atXYZ(nx,ay,nz,c,out_value), Iaan = (Tfloat)atXYZ(ax,ay,nz,c,out_value), - Ian = Ican + 0.5f*(dx*(-Ipan+Inan) + dx*dx*(2*Ipan-5*Ican+4*Inan-Iaan) + dx*dx*dx*(-Ipan+3*Ican-3*Inan+Iaan)), - In = Icn + 0.5f*(dy*(-Ipn+Inn) + dy*dy*(2*Ipn-5*Icn+4*Inn-Ian) + dy*dy*dy*(-Ipn+3*Icn-3*Inn+Ian)), - Ippa = (Tfloat)atXYZ(px,py,az,c,out_value), Icpa = (Tfloat)atXYZ(x,py,az,c,out_value), - Inpa = (Tfloat)atXYZ(nx,py,az,c,out_value), Iapa = (Tfloat)atXYZ(ax,py,az,c,out_value), - Ipa = Icpa + 0.5f*(dx*(-Ippa+Inpa) + dx*dx*(2*Ippa-5*Icpa+4*Inpa-Iapa) + dx*dx*dx*(-Ippa+3*Icpa-3*Inpa+Iapa)), - Ipca = (Tfloat)atXYZ(px,y,az,c,out_value), Icca = (Tfloat)atXYZ(x, y,az,c,out_value), - Inca = (Tfloat)atXYZ(nx,y,az,c,out_value), Iaca = (Tfloat)atXYZ(ax,y,az,c,out_value), - Ica = Icca + 0.5f*(dx*(-Ipca+Inca) + dx*dx*(2*Ipca-5*Icca+4*Inca-Iaca) + dx*dx*dx*(-Ipca+3*Icca-3*Inca+Iaca)), - Ipna = (Tfloat)atXYZ(px,ny,az,c,out_value), Icna = (Tfloat)atXYZ(x,ny,az,c,out_value), - Inna = (Tfloat)atXYZ(nx,ny,az,c,out_value), Iana = (Tfloat)atXYZ(ax,ny,az,c,out_value), - Ina = Icna + 0.5f*(dx*(-Ipna+Inna) + dx*dx*(2*Ipna-5*Icna+4*Inna-Iana) + dx*dx*dx*(-Ipna+3*Icna-3*Inna+Iana)), - Ipaa = (Tfloat)atXYZ(px,ay,az,c,out_value), Icaa = (Tfloat)atXYZ(x,ay,az,c,out_value), - Inaa = (Tfloat)atXYZ(nx,ay,az,c,out_value), Iaaa = (Tfloat)atXYZ(ax,ay,az,c,out_value), - Iaa = Icaa + 0.5f*(dx*(-Ipaa+Inaa) + dx*dx*(2*Ipaa-5*Icaa+4*Inaa-Iaaa) + dx*dx*dx*(-Ipaa+3*Icaa-3*Inaa+Iaaa)), - Ia = Ica + 0.5f*(dy*(-Ipa+Ina) + dy*dy*(2*Ipa-5*Ica+4*Ina-Iaa) + dy*dy*dy*(-Ipa+3*Ica-3*Ina+Iaa)); - return Ic + 0.5f*(dz*(-Ip+In) + dz*dz*(2*Ip-5*Ic+4*In-Ia) + dz*dz*dz*(-Ip+3*Ic-3*In+Ia)); - } - - //! Return damped pixel value, using cubic interpolation and Dirichlet boundary conditions for the XYZ-coordinates. - /** - Similar to cubic_atXYZ(float,float,float,int,const T) const, except that you can specify the authorized - minimum and maximum of the returned value. - **/ - Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, const T out_value, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = cubic_atXYZ(fx,fy,fz,c,out_value); - return valmax_value?max_value:val; - } - - //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y and Z-coordinates. - /** - Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking - are achieved both for X,Y and Z-coordinates. - \note - - If you know your image instance is \e not empty, you may rather use the slightly faster method - \c _cubic_atXYZ(float,float,float,int). - **/ - Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "cubic_atXYZ(): Empty instance.", - cimg_instance); - return _cubic_atXYZ(fx,fy,fz,c); - } - - Tfloat _cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const { - const float - nfx = fx<0?0:(fx>_width-1?_width-1:fx), - nfy = fy<0?0:(fy>_height-1?_height-1:fy), - nfz = fz<0?0:(fz>_depth-1?_depth-1:fz); - const int x = (int)nfx, y = (int)nfy, z = (int)nfz; - const float dx = nfx - x, dy = nfy - y, dz = nfz - z; - const int - px = x-1<0?0:x-1, nx = dx>0?x+1:x, ax = x+2>=width()?width()-1:x+2, - py = y-1<0?0:y-1, ny = dy>0?y+1:y, ay = y+2>=height()?height()-1:y+2, - pz = z-1<0?0:z-1, nz = dz>0?z+1:z, az = z+2>=depth()?depth()-1:z+2; - const Tfloat - Ippp = (Tfloat)(*this)(px,py,pz,c), Icpp = (Tfloat)(*this)(x,py,pz,c), - Inpp = (Tfloat)(*this)(nx,py,pz,c), Iapp = (Tfloat)(*this)(ax,py,pz,c), - Ipp = Icpp + 0.5f*(dx*(-Ippp+Inpp) + dx*dx*(2*Ippp-5*Icpp+4*Inpp-Iapp) + dx*dx*dx*(-Ippp+3*Icpp-3*Inpp+Iapp)), - Ipcp = (Tfloat)(*this)(px,y,pz,c), Iccp = (Tfloat)(*this)(x, y,pz,c), - Incp = (Tfloat)(*this)(nx,y,pz,c), Iacp = (Tfloat)(*this)(ax,y,pz,c), - Icp = Iccp + 0.5f*(dx*(-Ipcp+Incp) + dx*dx*(2*Ipcp-5*Iccp+4*Incp-Iacp) + dx*dx*dx*(-Ipcp+3*Iccp-3*Incp+Iacp)), - Ipnp = (Tfloat)(*this)(px,ny,pz,c), Icnp = (Tfloat)(*this)(x,ny,pz,c), - Innp = (Tfloat)(*this)(nx,ny,pz,c), Ianp = (Tfloat)(*this)(ax,ny,pz,c), - Inp = Icnp + 0.5f*(dx*(-Ipnp+Innp) + dx*dx*(2*Ipnp-5*Icnp+4*Innp-Ianp) + dx*dx*dx*(-Ipnp+3*Icnp-3*Innp+Ianp)), - Ipap = (Tfloat)(*this)(px,ay,pz,c), Icap = (Tfloat)(*this)(x,ay,pz,c), - Inap = (Tfloat)(*this)(nx,ay,pz,c), Iaap = (Tfloat)(*this)(ax,ay,pz,c), - Iap = Icap + 0.5f*(dx*(-Ipap+Inap) + dx*dx*(2*Ipap-5*Icap+4*Inap-Iaap) + dx*dx*dx*(-Ipap+3*Icap-3*Inap+Iaap)), - Ip = Icp + 0.5f*(dy*(-Ipp+Inp) + dy*dy*(2*Ipp-5*Icp+4*Inp-Iap) + dy*dy*dy*(-Ipp+3*Icp-3*Inp+Iap)), - Ippc = (Tfloat)(*this)(px,py,z,c), Icpc = (Tfloat)(*this)(x,py,z,c), - Inpc = (Tfloat)(*this)(nx,py,z,c), Iapc = (Tfloat)(*this)(ax,py,z,c), - Ipc = Icpc + 0.5f*(dx*(-Ippc+Inpc) + dx*dx*(2*Ippc-5*Icpc+4*Inpc-Iapc) + dx*dx*dx*(-Ippc+3*Icpc-3*Inpc+Iapc)), - Ipcc = (Tfloat)(*this)(px,y,z,c), Iccc = (Tfloat)(*this)(x, y,z,c), - Incc = (Tfloat)(*this)(nx,y,z,c), Iacc = (Tfloat)(*this)(ax,y,z,c), - Icc = Iccc + 0.5f*(dx*(-Ipcc+Incc) + dx*dx*(2*Ipcc-5*Iccc+4*Incc-Iacc) + dx*dx*dx*(-Ipcc+3*Iccc-3*Incc+Iacc)), - Ipnc = (Tfloat)(*this)(px,ny,z,c), Icnc = (Tfloat)(*this)(x,ny,z,c), - Innc = (Tfloat)(*this)(nx,ny,z,c), Ianc = (Tfloat)(*this)(ax,ny,z,c), - Inc = Icnc + 0.5f*(dx*(-Ipnc+Innc) + dx*dx*(2*Ipnc-5*Icnc+4*Innc-Ianc) + dx*dx*dx*(-Ipnc+3*Icnc-3*Innc+Ianc)), - Ipac = (Tfloat)(*this)(px,ay,z,c), Icac = (Tfloat)(*this)(x,ay,z,c), - Inac = (Tfloat)(*this)(nx,ay,z,c), Iaac = (Tfloat)(*this)(ax,ay,z,c), - Iac = Icac + 0.5f*(dx*(-Ipac+Inac) + dx*dx*(2*Ipac-5*Icac+4*Inac-Iaac) + dx*dx*dx*(-Ipac+3*Icac-3*Inac+Iaac)), - Ic = Icc + 0.5f*(dy*(-Ipc+Inc) + dy*dy*(2*Ipc-5*Icc+4*Inc-Iac) + dy*dy*dy*(-Ipc+3*Icc-3*Inc+Iac)), - Ippn = (Tfloat)(*this)(px,py,nz,c), Icpn = (Tfloat)(*this)(x,py,nz,c), - Inpn = (Tfloat)(*this)(nx,py,nz,c), Iapn = (Tfloat)(*this)(ax,py,nz,c), - Ipn = Icpn + 0.5f*(dx*(-Ippn+Inpn) + dx*dx*(2*Ippn-5*Icpn+4*Inpn-Iapn) + dx*dx*dx*(-Ippn+3*Icpn-3*Inpn+Iapn)), - Ipcn = (Tfloat)(*this)(px,y,nz,c), Iccn = (Tfloat)(*this)(x, y,nz,c), - Incn = (Tfloat)(*this)(nx,y,nz,c), Iacn = (Tfloat)(*this)(ax,y,nz,c), - Icn = Iccn + 0.5f*(dx*(-Ipcn+Incn) + dx*dx*(2*Ipcn-5*Iccn+4*Incn-Iacn) + dx*dx*dx*(-Ipcn+3*Iccn-3*Incn+Iacn)), - Ipnn = (Tfloat)(*this)(px,ny,nz,c), Icnn = (Tfloat)(*this)(x,ny,nz,c), - Innn = (Tfloat)(*this)(nx,ny,nz,c), Iann = (Tfloat)(*this)(ax,ny,nz,c), - Inn = Icnn + 0.5f*(dx*(-Ipnn+Innn) + dx*dx*(2*Ipnn-5*Icnn+4*Innn-Iann) + dx*dx*dx*(-Ipnn+3*Icnn-3*Innn+Iann)), - Ipan = (Tfloat)(*this)(px,ay,nz,c), Ican = (Tfloat)(*this)(x,ay,nz,c), - Inan = (Tfloat)(*this)(nx,ay,nz,c), Iaan = (Tfloat)(*this)(ax,ay,nz,c), - Ian = Ican + 0.5f*(dx*(-Ipan+Inan) + dx*dx*(2*Ipan-5*Ican+4*Inan-Iaan) + dx*dx*dx*(-Ipan+3*Ican-3*Inan+Iaan)), - In = Icn + 0.5f*(dy*(-Ipn+Inn) + dy*dy*(2*Ipn-5*Icn+4*Inn-Ian) + dy*dy*dy*(-Ipn+3*Icn-3*Inn+Ian)), - Ippa = (Tfloat)(*this)(px,py,az,c), Icpa = (Tfloat)(*this)(x,py,az,c), - Inpa = (Tfloat)(*this)(nx,py,az,c), Iapa = (Tfloat)(*this)(ax,py,az,c), - Ipa = Icpa + 0.5f*(dx*(-Ippa+Inpa) + dx*dx*(2*Ippa-5*Icpa+4*Inpa-Iapa) + dx*dx*dx*(-Ippa+3*Icpa-3*Inpa+Iapa)), - Ipca = (Tfloat)(*this)(px,y,az,c), Icca = (Tfloat)(*this)(x, y,az,c), - Inca = (Tfloat)(*this)(nx,y,az,c), Iaca = (Tfloat)(*this)(ax,y,az,c), - Ica = Icca + 0.5f*(dx*(-Ipca+Inca) + dx*dx*(2*Ipca-5*Icca+4*Inca-Iaca) + dx*dx*dx*(-Ipca+3*Icca-3*Inca+Iaca)), - Ipna = (Tfloat)(*this)(px,ny,az,c), Icna = (Tfloat)(*this)(x,ny,az,c), - Inna = (Tfloat)(*this)(nx,ny,az,c), Iana = (Tfloat)(*this)(ax,ny,az,c), - Ina = Icna + 0.5f*(dx*(-Ipna+Inna) + dx*dx*(2*Ipna-5*Icna+4*Inna-Iana) + dx*dx*dx*(-Ipna+3*Icna-3*Inna+Iana)), - Ipaa = (Tfloat)(*this)(px,ay,az,c), Icaa = (Tfloat)(*this)(x,ay,az,c), - Inaa = (Tfloat)(*this)(nx,ay,az,c), Iaaa = (Tfloat)(*this)(ax,ay,az,c), - Iaa = Icaa + 0.5f*(dx*(-Ipaa+Inaa) + dx*dx*(2*Ipaa-5*Icaa+4*Inaa-Iaaa) + dx*dx*dx*(-Ipaa+3*Icaa-3*Inaa+Iaaa)), - Ia = Ica + 0.5f*(dy*(-Ipa+Ina) + dy*dy*(2*Ipa-5*Ica+4*Ina-Iaa) + dy*dy*dy*(-Ipa+3*Ica-3*Ina+Iaa)); - return Ic + 0.5f*(dz*(-Ip+In) + dz*dz*(2*Ip-5*Ic+4*In-Ia) + dz*dz*dz*(-Ip+3*Ic-3*In+Ia)); - } - - //! Return damped pixel value, using cubic interpolation and Neumann boundary conditions for the XYZ-coordinates. - /** - Similar to cubic_atXYZ(float,float,float,int) const, except that you can specify the authorized minimum and - maximum of the returned value. - **/ - Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = cubic_atXYZ(fx,fy,fz,c); - return valmax_value?max_value:val; - } - - Tfloat _cubic_atXYZ(const float fx, const float fy, const float fz, const int c, - const Tfloat min_value, const Tfloat max_value) const { - const Tfloat val = _cubic_atXYZ(fx,fy,fz,c); - return valmax_value?max_value:val; - } - - //! Set pixel value, using linear interpolation for the X and Y-coordinates. - /** - Set pixel value at specified coordinates (\c fx,\c fy,\c z,\c c) in the image instance, in a way that - the value is spread amongst several neighbors if the pixel coordinates are indeed float-valued. - \param value Pixel value to set. - \param fx X-coordinate of the pixel value (float-valued). - \param fy Y-coordinate of the pixel value (float-valued). - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param is_added Tells if the pixel value is added to (\c true), or simply replace (\c false) the current image - pixel(s). - \return A reference to the current image instance. - \note - - If specified coordinates are outside image bounds, no operations are performed. - **/ - CImg& set_linear_atXY(const T& value, const float fx, const float fy=0, const int z=0, const int c=0, - const bool is_added=false) { - const int - x = (int)fx - (fx>=0?0:1), nx = x + 1, - y = (int)fy - (fy>=0?0:1), ny = y + 1; - const float - dx = fx - x, - dy = fy - y; - if (z>=0 && z=0 && c=0 && y=0 && x=0 && nx=0 && ny=0 && x=0 && nx& set_linear_atXYZ(const T& value, const float fx, const float fy=0, const float fz=0, const int c=0, - const bool is_added=false) { - const int - x = (int)fx - (fx>=0?0:1), nx = x + 1, - y = (int)fy - (fy>=0?0:1), ny = y + 1, - z = (int)fz - (fz>=0?0:1), nz = z + 1; - const float - dx = fx - x, - dy = fy - y, - dz = fz - z; - if (c>=0 && c=0 && z=0 && y=0 && x=0 && nx=0 && ny=0 && x=0 && nx=0 && nz=0 && y=0 && x=0 && nx=0 && ny=0 && x=0 && nx image whose buffer data() is a \c char* string describing the list of all pixel values - of the image instance (written in base 10), separated by specified \c separator character. - \param separator A \c char character which specifies the separator between values in the returned C-string. - \param max_size Maximum size of the returned image. - \note - - The returned image is never empty. - - For an empty image instance, the returned string is "". - - If \c max_size is equal to \c 0, there are no limits on the size of the returned string. - - Otherwise, if the maximum number of string characters is exceeded, the value string is cut off - and terminated by character \c '\0'. In that case, the returned image size is max_size + 1. - **/ - CImg value_string(const char separator=',', const unsigned int max_size=0) const { - if (is_empty()) return CImg::string(""); - CImgList items; - char s_item[256] = { 0 }; - const T *ptrs = _data; - unsigned int string_size = 0; - for (unsigned long off = 0, siz = (unsigned int)size(); off::format(),cimg::type::format(*(ptrs++))); - CImg item(s_item,printed_size); - item[printed_size-1] = separator; - item.move_to(items); - if (max_size) string_size+=printed_size; - } - CImg res; - (items>'x').move_to(res); - if (max_size && res._width>max_size) res.crop(0,max_size); - res.back() = 0; - return res; - } - - //@} - //------------------------------------- - // - //! \name Instance Checking - //@{ - //------------------------------------- - - //! Test shared state of the pixel buffer. - /** - Return \c true if image instance has a shared memory buffer, and \c false otherwise. - \note - - A shared image do not own his pixel buffer data() and will not deallocate it on destruction. - - Most of the time, a \c CImg image instance will \e not be shared. - - A shared image can only be obtained by a limited set of constructors and methods (see list below). - **/ - bool is_shared() const { - return _is_shared; - } - - //! Test if image instance is empty. - /** - Return \c true, if image instance is empty, i.e. does \e not contain any pixel values, has dimensions - \c 0 x \c 0 x \c 0 x \c 0 and a pixel buffer pointer set to \c 0 (null pointer), and \c false otherwise. - **/ - bool is_empty() const { - return !(_data && _width && _height && _depth && _spectrum); - } - - //! Test if image instance contains a 'inf' value. - /** - Return \c true, if image instance contains a 'inf' value, and \c false otherwise. - **/ - bool is_inf() const { - if (cimg::type::is_float()) cimg_for(*this,p,T) if (cimg::type::is_inf((float)*p)) return true; - return false; - } - - //! Test if image instance contains a 'nan' value. - /** - Return \c true, if image instance contains a 'nan' value, and \c false otherwise. - **/ - bool is_nan() const { - if (cimg::type::is_float()) cimg_for(*this,p,T) if (cimg::type::is_nan((float)*p)) return true; - return false; - } - - //! Test if image width is equal to specified value. - bool is_sameX(const unsigned int size_x) const { - return _width==size_x; - } - - //! Test if image width is equal to specified value. - template - bool is_sameX(const CImg& img) const { - return is_sameX(img._width); - } - - //! Test if image width is equal to specified value. - bool is_sameX(const CImgDisplay& disp) const { - return is_sameX(disp._width); - } - - //! Test if image height is equal to specified value. - bool is_sameY(const unsigned int size_y) const { - return _height==size_y; - } - - //! Test if image height is equal to specified value. - template - bool is_sameY(const CImg& img) const { - return is_sameY(img._height); - } - - //! Test if image height is equal to specified value. - bool is_sameY(const CImgDisplay& disp) const { - return is_sameY(disp._height); - } - - //! Test if image depth is equal to specified value. - bool is_sameZ(const unsigned int size_z) const { - return _depth==size_z; - } - - //! Test if image depth is equal to specified value. - template - bool is_sameZ(const CImg& img) const { - return is_sameZ(img._depth); - } - - //! Test if image spectrum is equal to specified value. - bool is_sameC(const unsigned int size_c) const { - return _spectrum==size_c; - } - - //! Test if image spectrum is equal to specified value. - template - bool is_sameC(const CImg& img) const { - return is_sameC(img._spectrum); - } - - //! Test if image width and height are equal to specified values. - /** - Test if is_sameX(unsigned int) const and is_sameY(unsigned int) const are both verified. - **/ - bool is_sameXY(const unsigned int size_x, const unsigned int size_y) const { - return _width==size_x && _height==size_y; - } - - //! Test if image width and height are the same as that of another image. - /** - Test if is_sameX(const CImg&) const and is_sameY(const CImg&) const are both verified. - **/ - template - bool is_sameXY(const CImg& img) const { - return is_sameXY(img._width,img._height); - } - - //! Test if image width and height are the same as that of an existing display window. - /** - Test if is_sameX(const CImgDisplay&) const and is_sameY(const CImgDisplay&) const are both verified. - **/ - bool is_sameXY(const CImgDisplay& disp) const { - return is_sameXY(disp._width,disp._height); - } - - //! Test if image width and depth are equal to specified values. - /** - Test if is_sameX(unsigned int) const and is_sameZ(unsigned int) const are both verified. - **/ - bool is_sameXZ(const unsigned int size_x, const unsigned int size_z) const { - return _width==size_x && _depth==size_z; - } - - //! Test if image width and depth are the same as that of another image. - /** - Test if is_sameX(const CImg&) const and is_sameZ(const CImg&) const are both verified. - **/ - template - bool is_sameXZ(const CImg& img) const { - return is_sameXZ(img._width,img._depth); - } - - //! Test if image width and spectrum are equal to specified values. - /** - Test if is_sameX(unsigned int) const and is_sameC(unsigned int) const are both verified. - **/ - bool is_sameXC(const unsigned int size_x, const unsigned int size_c) const { - return _width==size_x && _spectrum==size_c; - } - - //! Test if image width and spectrum are the same as that of another image. - /** - Test if is_sameX(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameXC(const CImg& img) const { - return is_sameXC(img._width,img._spectrum); - } - - //! Test if image height and depth are equal to specified values. - /** - Test if is_sameY(unsigned int) const and is_sameZ(unsigned int) const are both verified. - **/ - bool is_sameYZ(const unsigned int size_y, const unsigned int size_z) const { - return _height==size_y && _depth==size_z; - } - - //! Test if image height and depth are the same as that of another image. - /** - Test if is_sameY(const CImg&) const and is_sameZ(const CImg&) const are both verified. - **/ - template - bool is_sameYZ(const CImg& img) const { - return is_sameYZ(img._height,img._depth); - } - - //! Test if image height and spectrum are equal to specified values. - /** - Test if is_sameY(unsigned int) const and is_sameC(unsigned int) const are both verified. - **/ - bool is_sameYC(const unsigned int size_y, const unsigned int size_c) const { - return _height==size_y && _spectrum==size_c; - } - - //! Test if image height and spectrum are the same as that of another image. - /** - Test if is_sameY(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameYC(const CImg& img) const { - return is_sameYC(img._height,img._spectrum); - } - - //! Test if image depth and spectrum are equal to specified values. - /** - Test if is_sameZ(unsigned int) const and is_sameC(unsigned int) const are both verified. - **/ - bool is_sameZC(const unsigned int size_z, const unsigned int size_c) const { - return _depth==size_z && _spectrum==size_c; - } - - //! Test if image depth and spectrum are the same as that of another image. - /** - Test if is_sameZ(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameZC(const CImg& img) const { - return is_sameZC(img._depth,img._spectrum); - } - - //! Test if image width, height and depth are equal to specified values. - /** - Test if is_sameXY(unsigned int,unsigned int) const and is_sameZ(unsigned int) const are both verified. - **/ - bool is_sameXYZ(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z) const { - return is_sameXY(size_x,size_y) && _depth==size_z; - } - - //! Test if image width, height and depth are the same as that of another image. - /** - Test if is_sameXY(const CImg&) const and is_sameZ(const CImg&) const are both verified. - **/ - template - bool is_sameXYZ(const CImg& img) const { - return is_sameXYZ(img._width,img._height,img._depth); - } - - //! Test if image width, height and spectrum are equal to specified values. - /** - Test if is_sameXY(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified. - **/ - bool is_sameXYC(const unsigned int size_x, const unsigned int size_y, const unsigned int size_c) const { - return is_sameXY(size_x,size_y) && _spectrum==size_c; - } - - //! Test if image width, height and spectrum are the same as that of another image. - /** - Test if is_sameXY(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameXYC(const CImg& img) const { - return is_sameXYC(img._width,img._height,img._spectrum); - } - - //! Test if image width, depth and spectrum are equal to specified values. - /** - Test if is_sameXZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified. - **/ - bool is_sameXZC(const unsigned int size_x, const unsigned int size_z, const unsigned int size_c) const { - return is_sameXZ(size_x,size_z) && _spectrum==size_c; - } - - //! Test if image width, depth and spectrum are the same as that of another image. - /** - Test if is_sameXZ(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameXZC(const CImg& img) const { - return is_sameXZC(img._width,img._depth,img._spectrum); - } - - //! Test if image height, depth and spectrum are equal to specified values. - /** - Test if is_sameYZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified. - **/ - bool is_sameYZC(const unsigned int size_y, const unsigned int size_z, const unsigned int size_c) const { - return is_sameYZ(size_y,size_z) && _spectrum==size_c; - } - - //! Test if image height, depth and spectrum are the same as that of another image. - /** - Test if is_sameYZ(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameYZC(const CImg& img) const { - return is_sameYZC(img._height,img._depth,img._spectrum); - } - - //! Test if image width, height, depth and spectrum are equal to specified values. - /** - Test if is_sameXYZ(unsigned int,unsigned int,unsigned int) const and is_sameC(unsigned int) const are both - verified. - **/ - bool is_sameXYZC(const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c) const { - return is_sameXYZ(size_x,size_y,size_z) && _spectrum==size_c; - } - - //! Test if image width, height, depth and spectrum are the same as that of another image. - /** - Test if is_sameXYZ(const CImg&) const and is_sameC(const CImg&) const are both verified. - **/ - template - bool is_sameXYZC(const CImg& img) const { - return is_sameXYZC(img._width,img._height,img._depth,img._spectrum); - } - - //! Test if specified coordinates are inside image bounds. - /** - Return \c true if pixel located at (\c x,\c y,\c z,\c c) is inside bounds of the image instance, - and \c false otherwise. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note - - Return \c true only if all these conditions are verified: - - The image instance is \e not empty. - - 0<=x<=\ref width()-1. - - 0<=y<=\ref height()-1. - - 0<=z<=\ref depth()-1. - - 0<=c<=\ref spectrum()-1. - **/ - bool containsXYZC(const int x, const int y=0, const int z=0, const int c=0) const { - return !is_empty() && x>=0 && x=0 && y=0 && z=0 && c img(100,100,1,3); // Construct a 100x100 RGB color image. - const unsigned long offset = 1249; // Offset to the pixel (49,12,0,0). - unsigned int x,y,z,c; - if (img.contains(img[offset],x,y,z,c)) { // Convert offset to (x,y,z,c) coordinates. - std::printf("Offset %u refers to pixel located at (%u,%u,%u,%u).\n", - offset,x,y,z,c); - } - \endcode - **/ - template - bool contains(const T& pixel, t& x, t& y, t& z, t& c) const { - const unsigned long wh = (unsigned long)_width*_height, whd = wh*_depth, siz = whd*_spectrum; - const T *const ppixel = &pixel; - if (is_empty() || ppixel<_data || ppixel>=_data+siz) return false; - unsigned long off = (unsigned long)(ppixel - _data); - const unsigned long nc = off/whd; - off%=whd; - const unsigned long nz = off/wh; - off%=wh; - const unsigned long ny = off/_width, nx = off%_width; - x = (t)nx; y = (t)ny; z = (t)nz; c = (t)nc; - return true; - } - - //! Test if pixel value is inside image bounds and get its X,Y and Z-coordinates. - /** - Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X,Y and Z-coordinates are set. - **/ - template - bool contains(const T& pixel, t& x, t& y, t& z) const { - const unsigned long wh = (unsigned long)_width*_height, whd = wh*_depth, siz = whd*_spectrum; - const T *const ppixel = &pixel; - if (is_empty() || ppixel<_data || ppixel>=_data+siz) return false; - unsigned long off = ((unsigned long)(ppixel - _data))%whd; - const unsigned long nz = off/wh; - off%=wh; - const unsigned long ny = off/_width, nx = off%_width; - x = (t)nx; y = (t)ny; z = (t)nz; - return true; - } - - //! Test if pixel value is inside image bounds and get its X and Y-coordinates. - /** - Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X and Y-coordinates are set. - **/ - template - bool contains(const T& pixel, t& x, t& y) const { - const unsigned long wh = (unsigned long)_width*_height, siz = wh*_depth*_spectrum; - const T *const ppixel = &pixel; - if (is_empty() || ppixel<_data || ppixel>=_data+siz) return false; - unsigned long off = ((unsigned int)(ppixel - _data))%wh; - const unsigned long ny = off/_width, nx = off%_width; - x = (t)nx; y = (t)ny; - return true; - } - - //! Test if pixel value is inside image bounds and get its X-coordinate. - /** - Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X-coordinate is set. - **/ - template - bool contains(const T& pixel, t& x) const { - const T *const ppixel = &pixel; - if (is_empty() || ppixel<_data || ppixel>=_data+size()) return false; - x = (t)(((unsigned long)(ppixel - _data))%_width); - return true; - } - - //! Test if pixel value is inside image bounds. - /** - Similar to contains(const T&,t&,t&,t&,t&) const, except that no pixel coordinates are set. - **/ - bool contains(const T& pixel) const { - const T *const ppixel = &pixel; - return !is_empty() && ppixel>=_data && ppixel<_data + size(); - } - - //! Test if pixel buffers of instance and input images overlap. - /** - Return \c true, if pixel buffers attached to image instance and input image \c img overlap, - and \c false otherwise. - \param img Input image to compare with. - \note - - Buffer overlapping may happen when manipulating \e shared images. - - If two image buffers overlap, operating on one of the image will probably modify the other one. - - Most of the time, \c CImg instances are \e non-shared and do not overlap between each others. - \par Example - \code - const CImg - img1("reference.jpg"), // Load RGB-color image. - img2 = img1.get_shared_channel(1); // Get shared version of the green channel. - if (img1.is_overlapped(img2)) { // Test succeeds, 'img1' and 'img2' overlaps. - std::printf("Buffers overlap!\n"); - } - \endcode - **/ - template - bool is_overlapped(const CImg& img) const { - const unsigned long csiz = size(), isiz = img.size(); - return !((void*)(_data + csiz)<=(void*)img._data || (void*)_data>=(void*)(img._data + isiz)); - } - - //! Test if the set {\c *this,\c primitives,\c colors,\c opacities} defines a valid 3d object. - /** - Return \c true is the 3d object represented by the set {\c *this,\c primitives,\c colors,\c opacities} defines a - valid 3d object, and \c false otherwise. The vertex coordinates are defined by the instance image. - \param primitives List of primitives of the 3d object. - \param colors List of colors of the 3d object. - \param opacities List (or image) of opacities of the 3d object. - \param full_check Tells if full checking of the 3d object must be performed. - \param[out] error_message C-string to contain the error message, if the test does not succeed. - \note - - Set \c full_checking to \c false to speed-up the 3d object checking. In this case, only the size of - each 3d object component is checked. - - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message. - **/ - template - bool is_object3d(const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const bool full_check=true, - char *const error_message=0) const { - if (error_message) *error_message = 0; - - // Check consistency for the particular case of an empty 3d object. - if (is_empty()) { - if (primitives || colors || opacities) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) defines no vertices but %u primitives, " - "%u colors and %lu opacities", - _width,primitives._width,primitives._width, - colors._width,(unsigned long)opacities.size()); - return false; - } - return true; - } - - // Check consistency of vertices. - if (_height!=3 || _depth>1 || _spectrum>1) { // Check vertices dimensions. - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) has invalid vertex dimensions (%u,%u,%u,%u)", - _width,primitives._width,_width,_height,_depth,_spectrum); - return false; - } - if (colors._width>primitives._width+1) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) defines %u colors", - _width,primitives._width,colors._width); - return false; - } - if (opacities.size()>primitives._width) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) defines %lu opacities", - _width,primitives._width,(unsigned long)opacities.size()); - return false; - } - if (!full_check) return true; - - // Check consistency of primitives. - cimglist_for(primitives,l) { - const CImg& primitive = primitives[l]; - const unsigned long psiz = primitive.size(); - switch (psiz) { - case 1 : { // Point. - const unsigned int i0 = (unsigned int)primitive(0); - if (i0>=_width) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) refers to invalid vertex indice %u in " - "point primitive [%u]", - _width,primitives._width,i0,l); - return false; - } - } break; - case 5 : { // Sphere. - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1); - if (i0>=_width || i1>=_width) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) refers to invalid vertex indices (%u,%u) in " - "sphere primitive [%u]", - _width,primitives._width,i0,i1,l); - return false; - } - } break; - case 2 : // Segment. - case 6 : { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1); - if (i0>=_width || i1>=_width) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) refers to invalid vertex indices (%u,%u) in " - "segment primitive [%u]", - _width,primitives._width,i0,i1,l); - return false; - } - } break; - case 3 : // Triangle. - case 9 : { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1), - i2 = (unsigned int)primitive(2); - if (i0>=_width || i1>=_width || i2>=_width) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) refers to invalid vertex indices (%u,%u,%u) in " - "triangle primitive [%u]", - _width,primitives._width,i0,i1,i2,l); - return false; - } - } break; - case 4 : // Quadrangle. - case 12 : { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1), - i2 = (unsigned int)primitive(2), - i3 = (unsigned int)primitive(3); - if (i0>=_width || i1>=_width || i2>=_width || i3>=_width) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in " - "quadrangle primitive [%u]", - _width,primitives._width,i0,i1,i2,i3,l); - return false; - } - } break; - default : - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) defines an invalid primitive [%u] of size %u", - _width,primitives._width,l,(unsigned int)psiz); - return false; - } - } - - // Check consistency of colors. - cimglist_for(colors,c) { - const CImg& color = colors[c]; - if (!color) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) defines no color for primitive [%u]", - _width,primitives._width,c); - return false; - } - } - - // Check consistency of light texture. - if (colors._width>primitives._width) { - const CImg &light = colors.back(); - if (!light || light._depth>1) { - if (error_message) std::sprintf(error_message, - "3d object (%u,%u) defines an invalid light texture (%u,%u,%u,%u)", - _width,primitives._width,light._width, - light._height,light._depth,light._spectrum); - return false; - } - } - - return true; - } - - //! Test if image instance represents a valid serialization of a 3d object. - /** - Return \c true if the image instance represents a valid serialization of a 3d object, and \c false otherwise. - \param full_check Tells if full checking of the instance must be performed. - \param[out] error_message C-string to contain the error message, if the test does not succeed. - \note - - Set \c full_check to \c false to speed-up the 3d object checking. In this case, only the size of - each 3d object component is checked. - - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message. - **/ - bool is_CImg3d(const bool full_check=true, char *const error_message=0) const { - if (error_message) *error_message = 0; - - // Check instance dimension and header. - if (_width!=1 || _height<8 || _depth!=1 || _spectrum!=1) { - if (error_message) std::sprintf(error_message, - "CImg3d has invalid dimensions (%u,%u,%u,%u)", - _width,_height,_depth,_spectrum); - return false; - } - const T *ptrs = _data, *const ptre = end(); - if (!_is_CImg3d(*(ptrs++),'C') || !_is_CImg3d(*(ptrs++),'I') || !_is_CImg3d(*(ptrs++),'m') || - !_is_CImg3d(*(ptrs++),'g') || !_is_CImg3d(*(ptrs++),'3') || !_is_CImg3d(*(ptrs++),'d')) { - if (error_message) std::sprintf(error_message, - "CImg3d header not found"); - return false; - } - const unsigned int - nb_points = cimg::float2uint((float)*(ptrs++)), - nb_primitives = cimg::float2uint((float)*(ptrs++)); - - // Check consistency of number of vertices / primitives. - if (!full_check) { - const unsigned long minimal_size = 8UL + 3*nb_points + 6*nb_primitives; - if (_data + minimal_size>ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) has only %lu values, while at least %lu values were expected", - nb_points,nb_primitives,size(),minimal_size); - return false; - } - } - - // Check consistency of vertex data. - if (!nb_points) { - if (nb_primitives) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) defines no vertices but %u primitives", - nb_points,nb_primitives,nb_primitives); - return false; - } - if (ptrs!=ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) is an empty object but contains %u value%s " - "more than expected", - nb_points,nb_primitives,(unsigned int)(ptre-ptrs),(ptre-ptrs)>1?"s":""); - return false; - } - return true; - } - if (ptrs+3*nb_points>ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) defines only %u vertices data", - nb_points,nb_primitives,(unsigned int)(ptre-ptrs)/3); - return false; - } - ptrs+=3*nb_points; - - // Check consistency of primitive data. - if (ptrs==ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) defines %u vertices but no primitive", - nb_points,nb_primitives,nb_points); - return false; - } - - if (!full_check) return true; - - for (unsigned int p = 0; p=nb_points) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid vertex indice %u in point primitive [%u]", - nb_points,nb_primitives,i0,p); - return false; - } - } break; - case 5 : { // Sphere. - const unsigned int - i0 = cimg::float2uint((float)*(ptrs++)), - i1 = cimg::float2uint((float)*(ptrs++)); - ptrs+=3; - if (i0>=nb_points || i1>=nb_points) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in " - "sphere primitive [%u]", - nb_points,nb_primitives,i0,i1,p); - return false; - } - } break; - case 2 : case 6 : { // Segment. - const unsigned int - i0 = cimg::float2uint((float)*(ptrs++)), - i1 = cimg::float2uint((float)*(ptrs++)); - if (nb_inds==6) ptrs+=4; - if (i0>=nb_points || i1>=nb_points) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in " - "segment primitive [%u]", - nb_points,nb_primitives,i0,i1,p); - return false; - } - } break; - case 3 : case 9 : { // Triangle. - const unsigned int - i0 = cimg::float2uint((float)*(ptrs++)), - i1 = cimg::float2uint((float)*(ptrs++)), - i2 = cimg::float2uint((float)*(ptrs++)); - if (nb_inds==9) ptrs+=6; - if (i0>=nb_points || i1>=nb_points || i2>=nb_points) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u) in " - "triangle primitive [%u]", - nb_points,nb_primitives,i0,i1,i2,p); - return false; - } - } break; - case 4 : case 12 : { // Quadrangle. - const unsigned int - i0 = cimg::float2uint((float)*(ptrs++)), - i1 = cimg::float2uint((float)*(ptrs++)), - i2 = cimg::float2uint((float)*(ptrs++)), - i3 = cimg::float2uint((float)*(ptrs++)); - if (nb_inds==12) ptrs+=8; - if (i0>=nb_points || i1>=nb_points || i2>=nb_points || i3>=nb_points) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in " - "quadrangle primitive [%u]", - nb_points,nb_primitives,i0,i1,i2,i3,p); - return false; - } - } break; - default : - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) defines an invalid primitive [%u] of size %u", - nb_points,nb_primitives,p,nb_inds); - return false; - } - if (ptrs>ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) has incomplete primitive data for primitive [%u], " - "%u values missing", - nb_points,nb_primitives,p,(unsigned int)(ptrs-ptre)); - return false; - } - } - - // Check consistency of color data. - if (ptrs==ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) defines no color/texture data", - nb_points,nb_primitives); - return false; - } - for (unsigned int c = 0; c=c) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid shared sprite/texture indice %u " - "for primitive [%u]", - nb_points,nb_primitives,w,c); - return false; - } - } else ptrs+=w*h*s; - } - if (ptrs>ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) has incomplete color/texture data for primitive [%u], " - "%u values missing", - nb_points,nb_primitives,c,(unsigned int)(ptrs-ptre)); - return false; - } - } - - // Check consistency of opacity data. - if (ptrs==ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) defines no opacity data", - nb_points,nb_primitives); - return false; - } - for (unsigned int o = 0; o=o) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) refers to invalid shared opacity indice %u " - "for primitive [%u]", - nb_points,nb_primitives,w,o); - return false; - } - } else ptrs+=w*h*s; - } - if (ptrs>ptre) { - if (error_message) std::sprintf(error_message, - "CImg3d (%u,%u) has incomplete opacity data for primitive [%u]", - nb_points,nb_primitives,o); - return false; - } - } - - // Check end of data. - if (ptrs1?"s":""); - return false; - } - return true; - } - - static bool _is_CImg3d(const T val, const char c) { - return val>=(T)c && val<(T)(c+1); - } - - //@} - //------------------------------------- - // - //! \name Mathematical Functions - //@{ - //------------------------------------- - - // Define the math formula parser/compiler and evaluator. - struct _cimg_math_parser { - CImgList code; - CImg opcode; - const CImg* p_code; - CImgList labelM; - CImg level, labelMpos, label1pos; - CImg mem; - CImg expr; - const CImg& reference; - CImg reference_stats; - unsigned int mempos, result; - const char *const calling_function; - typedef double (*mp_func)(_cimg_math_parser&); - -#define _cimg_mp_return(x) { *se = saved_char; return x; } -#define _cimg_mp_opcode0(op) _cimg_mp_return(opcode0(op)); -#define _cimg_mp_opcode1(op,i1) _cimg_mp_return(opcode1(op,i1)); -#define _cimg_mp_opcode2(op,i1,i2) { const unsigned int _i1 = i1, _i2 = i2; _cimg_mp_return(opcode2(op,_i1,_i2)); } -#define _cimg_mp_opcode3(op,i1,i2,i3) \ - { const unsigned int _i1 = i1, _i2 = i2, _i3 = i3; _cimg_mp_return(opcode3(op,_i1,_i2,_i3)); } -#define _cimg_mp_opcode6(op,i1,i2,i3,i4,i5,i6) \ - { const unsigned int _i1 = i1, _i2 = i2, _i3 = i3, _i4 = i4, _i5 = i5, _i6 = i6; \ - _cimg_mp_return(opcode6(op,_i1,_i2,_i3,_i4,_i5,_i6)); } - -#if defined(_WIN64) - // On Win64 and gcc 4.7, sizeof(long)!=sizeof(pointer), so a workaround is needed.. -#define _cimg_mp_enfunc(op) (long)((char*)(op)-(char*)mp_u) -#define _cimg_mp_defunc(mp) (*(mp_func)((char*)mp_u+(mp).opcode[0]))(mp) -#else -#define _cimg_mp_enfunc(op) (long)(op) -#define _cimg_mp_defunc(mp) (*(mp_func)((mp).opcode[0]))(mp) -#endif - - // Constructors. - _cimg_math_parser():reference(CImg::empty()),calling_function(0) {} - - _cimg_math_parser(const CImg& img, const char *const expression, const char *const funcname=0): - reference(img),calling_function(funcname?funcname:"cimg_math_parser") { - unsigned int l = 0; - if (expression) { - l = (unsigned int)std::strlen(expression); - expr.assign(expression,l+1); - if (*expr._data) { - char *d = expr._data; - for (const char *s = expr._data; *s || (bool)(*d=0); ++s) if (*s!=' ') *(d++) = *s; - l = (unsigned int)(d - expr._data); - } - } - if (!l) throw CImgArgumentException("[_cimg_math_parser] " - "CImg<%s>::%s(): Empty specified expression.", - pixel_type(),calling_function); - - int lv = 0; // Count parentheses/brackets level of expression. - level.assign(l); - unsigned int *pd = level._data; - for (const char *ps = expr._data; *ps && lv>=0; ++ps) - *(pd++) = (unsigned int)(*ps=='('||*ps=='['?lv++:*ps==')'||*ps==']'?--lv:lv); - if (lv!=0) { - throw CImgArgumentException("[_cimg_math_parser] " - "CImg<%s>::%s(): Unbalanced parentheses/brackets in specified expression '%s'.", - pixel_type(),calling_function, - expr._data); - } - // Init constant values. - mem.assign(512); - mem[0] = 0; - mem[1] = 1; - mem[2] = 2; - mem[3] = (double)reference._width; - mem[4] = (double)reference._height; - mem[5] = (double)reference._depth; - mem[6] = (double)reference._spectrum; - mem[7] = cimg::PI; - mem[8] = std::exp(1.0); // Then [9] = x, [10] = y, [11] = z, [12] = c - mempos = 13; - labelMpos.assign(8); - label1pos.assign(128,1,1,1,~0U); - label1pos['w'] = 3; - label1pos['h'] = 4; - label1pos['d'] = 5; - label1pos['s'] = 6; - label1pos[0] = 7; // pi - label1pos['e'] = 8; - label1pos['x'] = 9; - label1pos['y'] = 10; - label1pos['z'] = 11; - label1pos['c'] = 12; - result = compile(expr._data,expr._data+l); // Compile formula into a serie of opcodes. - } - - // Insert code instructions. - unsigned int opcode0(const mp_func op) { - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(op),pos).move_to(code); - return pos; - } - - unsigned int opcode1(const mp_func op, const unsigned int arg1) { - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(op),pos,arg1).move_to(code); - return pos; - } - - unsigned int opcode2(const mp_func op, const unsigned int arg1, const unsigned int arg2) { - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(op),pos,arg1,arg2).move_to(code); - return pos; - } - - unsigned int opcode3(const mp_func op, - const unsigned int arg1, const unsigned int arg2, const unsigned int arg3) { - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(op),pos,arg1,arg2,arg3).move_to(code); - return pos; - } - - unsigned int opcode6(const mp_func op, - const unsigned int arg1, const unsigned int arg2, const unsigned int arg3, - const unsigned int arg4, const unsigned int arg5, const unsigned int arg6) { - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(op),pos,arg1,arg2,arg3,arg4,arg5,arg6).move_to(code); - return pos; - } - - // Compilation procedure. - unsigned int compile(char *const ss, char *const se) { - if (!ss || se<=ss || !*ss) { - throw CImgArgumentException("[_cimg_math_parser] " - "CImg<%s>::%s(): Missing item in specified expression '%s'.", - pixel_type(),calling_function, - expr._data); - } - char - *const se1 = se-1, *const se2 = se-2, *const se3 = se-3, *const se4 = se-4, - *const ss1 = ss+1, *const ss2 = ss+2, *const ss3 = ss+3, *const ss4 = ss+4, - *const ss5 = ss+5, *const ss6 = ss+6, *const ss7 = ss+7; - const char saved_char = *se; *se = 0; - const unsigned int clevel = level[ss-expr._data], clevel1 = clevel+1; - if (*se1==';') return compile(ss,se1); - - // Look for a single value, variable or variable assignment. - char end = 0, sep = 0; double val = 0; - const int nb = std::sscanf(ss,"%lf%c%c",&val,&sep,&end); - if (nb==1) { - if (val==0 || val==1 || val==2) _cimg_mp_return((int)val); - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - mem[pos] = val; - _cimg_mp_return(pos); - } - if (nb==2 && sep=='%') { - if (val==0 || val==100 || val==200) _cimg_mp_return((int)(val/100)); - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - mem[pos] = val/100; - _cimg_mp_return(pos); - } - if (ss1==se) switch (*ss) { - case 'w' : case 'h' : case 'd' : case 's' : - case 'x' : case 'y' : case 'z' : case 'c' : case 'e' : _cimg_mp_return(label1pos[*ss]); - case 'u' : if (label1pos['u']!=~0U) _cimg_mp_return(label1pos['u']); _cimg_mp_opcode2(mp_u,0,1); - case 'g' : if (label1pos['g']!=~0U) _cimg_mp_return(label1pos['g']); _cimg_mp_opcode0(mp_g); - case 'i' : if (label1pos['i']!=~0U) _cimg_mp_return(label1pos['i']); _cimg_mp_opcode0(mp_i); - case '?' : _cimg_mp_opcode2(mp_u,0,1); - } - if (ss1==se1) { - if (*ss=='p' && *ss1=='i') _cimg_mp_return(label1pos[0]); // pi - if (*ss=='i') { - if (*ss1=='m') { if (label1pos[1]!=~0U) _cimg_mp_return(label1pos[1]); _cimg_mp_opcode0(mp_im); } // im - if (*ss1=='M') { if (label1pos[2]!=~0U) _cimg_mp_return(label1pos[2]); _cimg_mp_opcode0(mp_iM); } // iM - if (*ss1=='a') { if (label1pos[3]!=~0U) _cimg_mp_return(label1pos[3]); _cimg_mp_opcode0(mp_ia); } // ia - if (*ss1=='v') { if (label1pos[4]!=~0U) _cimg_mp_return(label1pos[4]); _cimg_mp_opcode0(mp_iv); } // iv - } - if (*ss1=='m') { - if (*ss=='x') { if (label1pos[5]!=~0U) _cimg_mp_return(label1pos[5]); _cimg_mp_opcode0(mp_xm); } // xm - if (*ss=='y') { if (label1pos[6]!=~0U) _cimg_mp_return(label1pos[6]); _cimg_mp_opcode0(mp_ym); } // ym - if (*ss=='z') { if (label1pos[7]!=~0U) _cimg_mp_return(label1pos[7]); _cimg_mp_opcode0(mp_zm); } // zm - if (*ss=='c') { if (label1pos[8]!=~0U) _cimg_mp_return(label1pos[8]); _cimg_mp_opcode0(mp_cm); } // cm - } - if (*ss1=='M') { - if (*ss=='x') { if (label1pos[9]!=~0U) _cimg_mp_return(label1pos[9]); _cimg_mp_opcode0(mp_xM); } // xM - if (*ss=='y') { if (label1pos[10]!=~0U) _cimg_mp_return(label1pos[10]); _cimg_mp_opcode0(mp_yM); } // yM - if (*ss=='z') { if (label1pos[11]!=~0U) _cimg_mp_return(label1pos[11]); _cimg_mp_opcode0(mp_zM); } // zM - if (*ss=='c') { if (label1pos[12]!=~0U) _cimg_mp_return(label1pos[12]); _cimg_mp_opcode0(mp_cM); } // cM - } - } - - // Look for variable declarations. - for (char *s = se2; s>ss; --s) - if (*s==';' && level[s-expr._data]==clevel) { compile(ss,s); _cimg_mp_return(compile(s+1,se)); } - for (char *s = ss1, *ps = ss, *ns = ss2; s variable_name(ss,(unsigned int)(s-ss+1)); - variable_name.back() = 0; - bool is_valid_name = true; - if (*ss>='0' && *ss<='9') is_valid_name = false; - else for (const char *ns = ss+1; ns'z') && (*ns<'A' || *ns>'Z') && (*ns<'0' || *ns>'9') && *ns!='_') { - is_valid_name = false; break; - } - if (!is_valid_name) { - *se = saved_char; - throw CImgArgumentException("[_cimg_math_parser] " - "CImg<%s>::%s(): Invalid variable name '%s' in specified expression " - "'%s%s%s'.", - pixel_type(),calling_function, - variable_name._data, - (ss-8)>expr._data?"...":"", - (ss-8)>expr._data?ss-8:expr._data, - se<&expr.back()?"...":""); - } - const unsigned int pos = compile(s+1,se); - - // Check for particular case of a reserved variable. - if (variable_name[0] && variable_name[1] && !variable_name[2]) { - const char c1 = variable_name[0], c2 = variable_name[1]; - if (c1=='p' && c2=='i') variable_name.fill((char)0,(char)0); // pi - else if (c1=='i') { - if (c2=='m') variable_name.fill(1,0); // im - else if (c2=='M') variable_name.fill(2,0); // iM - else if (c2=='a') variable_name.fill(3,0); // ia - else if (c2=='v') variable_name.fill(4,0); // iv - } else if (c2=='m') { - if (c1=='x') variable_name.fill(5,0); // xm - else if (c1=='y') variable_name.fill(6,0); // ym - else if (c1=='z') variable_name.fill(7,0); // zm - else if (c1=='c') variable_name.fill(8,0); // cm - } else if (c2=='M') { - if (c1=='x') variable_name.fill(9,0); // xM - else if (c1=='y') variable_name.fill(10,0); // yM - else if (c1=='z') variable_name.fill(11,0); // zM - else if (c1=='c') variable_name.fill(12,0); // cM - } - } - if (variable_name[1]) { // Multi-char variable. - int label_pos = -1; - cimglist_for(labelM,i) // Check for existing variable with same name. - if (!std::strcmp(variable_name,labelM[i])) { label_pos = i; break; } - if (label_pos<0) { // If new variable. - if (labelM._width>=labelMpos._width) labelMpos.resize(-200,1,1,1,0); - label_pos = labelM._width; - variable_name.move_to(labelM); - } - labelMpos[label_pos] = pos; - } else label1pos[*variable_name] = pos; // Single-char variable. - _cimg_mp_return(pos); - } - - // Look for unary/binary operators. The operator precedences is defined as in C++. - for (char *s = se3, *ns = se2; s>ss; --s, --ns) if (*s=='|' && *ns=='|' && level[s-expr._data]==clevel) { - const unsigned int mem_A = compile(ss,s), bp1 = code._width, mem_B = compile(s+2,se); - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(mp_logical_or),pos,mem_A,mem_B,code._width-bp1).move_to(code,bp1); - _cimg_mp_return(pos); - } - for (char *s = se3, *ns = se2; s>ss; --s, --ns) if (*s=='&' && *ns=='&' && level[s-expr._data]==clevel) { - const unsigned int mem_A = compile(ss,s), bp1 = code._width, mem_B = compile(s+2,se); - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(mp_logical_and),pos,mem_A,mem_B,code._width-bp1).move_to(code,bp1); - _cimg_mp_return(pos); - } - for (char *s = se2; s>ss; --s) - if (*s=='|' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_bitwise_or,compile(ss,s),compile(s+1,se)); - for (char *s = se2; s>ss; --s) - if (*s=='&' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_bitwise_and,compile(ss,s),compile(s+1,se)); - for (char *s = se3, *ns = se2; s>ss; --s, --ns) - if (*s=='!' && *ns=='=' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_noteq,compile(ss,s),compile(s+2,se)); - for (char *s = se3, *ns = se2; s>ss; --s, --ns) - if (*s=='=' && *ns=='=' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_eqeq,compile(ss,s),compile(s+2,se)); - for (char *s = se3, *ns = se2; s>ss; --s, --ns) - if (*s=='<' && *ns=='=' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_infeq,compile(ss,s),compile(s+2,se)); - for (char *s = se3, *ns = se2; s>ss; --s, --ns) - if (*s=='>' && *ns=='=' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_supeq,compile(ss,s),compile(s+2,se)); - for (char *s = se2, *ns = se1, *ps = se3; s>ss; --s, --ns, --ps) - if (*s=='<' && *ns!='<' && *ps!='<' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_inf,compile(ss,s),compile(s+1,se)); - for (char *s = se2, *ns = se1, *ps = se3; s>ss; --s, --ns, --ps) - if (*s=='>' && *ns!='>' && *ps!='>' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_sup,compile(ss,s),compile(s+1,se)); - for (char *s = se3, *ns = se2; s>ss; --s, --ns) - if (*s=='<' && *ns=='<' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_lsl,compile(ss,s),compile(s+2,se)); - for (char *s = se3, *ns = se2; s>ss; --s, --ns) - if (*s=='>' && *ns=='>' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_lsr,compile(ss,s),compile(s+2,se)); - for (char *s = se2, *ps = se3; s>ss; --s, --ps) - if (*s=='+' && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' && - *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && - (*ps!='e' || !(ps>ss && (*(ps-1)=='.' || (*(ps-1)>='0' && *(ps-1)<='9')))) && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_add,compile(ss,s),compile(s+1,se)); - for (char *s = se2, *ps = se3; s>ss; --s, --ps) - if (*s=='-' && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' && - *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && - (*ps!='e' || !(ps>ss && (*(ps-1)=='.' || (*(ps-1)>='0' && *(ps-1)<='9')))) && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_sub,compile(ss,s),compile(s+1,se)); - for (char *s = se2; s>ss; --s) if (*s=='*' && level[s-expr._data]==clevel) { - const unsigned int mem_A = compile(ss,s), bp1 = code._width, mem_B = compile(s+1,se); - if (mempos>=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(mp_mul),pos,mem_A,mem_B,code._width-bp1).move_to(code,bp1); - _cimg_mp_return(pos); - } - for (char *s = se2; s>ss; --s) - if (*s=='/' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_div,compile(ss,s),compile(s+1,se)); - for (char *s = se2, *ns = se1; s>ss; --s, --ns) - if (*s=='%' && *ns!='^' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_modulo,compile(ss,s),compile(s+1,se)); - if (ssss; --s) - if (*s=='^' && level[s-expr._data]==clevel) - _cimg_mp_opcode2(mp_pow,compile(ss,s),compile(s+1,se)); - - // Look for a function call or a parenthesis. - if (*se1==']') { - const bool is_relative = *ss=='j'; - if ((*ss=='i' || is_relative) && *ss1=='[') { - if (*ss2==']') _cimg_mp_opcode0(mp_i); - _cimg_mp_opcode1(is_relative?mp_joff:mp_ioff,compile(ss2,se1)); - } - } - if (*se1==')') { - if (*ss=='(') _cimg_mp_return(compile(ss1,se1)); - if (!std::strncmp(ss,"sin(",4)) _cimg_mp_opcode1(mp_sin,compile(ss4,se1)); - if (!std::strncmp(ss,"cos(",4)) _cimg_mp_opcode1(mp_cos,compile(ss4,se1)); - if (!std::strncmp(ss,"tan(",4)) _cimg_mp_opcode1(mp_tan,compile(ss4,se1)); - if (!std::strncmp(ss,"asin(",5)) _cimg_mp_opcode1(mp_asin,compile(ss5,se1)); - if (!std::strncmp(ss,"acos(",5)) _cimg_mp_opcode1(mp_acos,compile(ss5,se1)); - if (!std::strncmp(ss,"atan(",5)) _cimg_mp_opcode1(mp_atan,compile(ss5,se1)); - if (!std::strncmp(ss,"sinh(",5)) _cimg_mp_opcode1(mp_sinh,compile(ss5,se1)); - if (!std::strncmp(ss,"cosh(",5)) _cimg_mp_opcode1(mp_cosh,compile(ss5,se1)); - if (!std::strncmp(ss,"tanh(",5)) _cimg_mp_opcode1(mp_tanh,compile(ss5,se1)); - if (!std::strncmp(ss,"log10(",6)) _cimg_mp_opcode1(mp_log10,compile(ss6,se1)); - if (!std::strncmp(ss,"log2(",5)) _cimg_mp_opcode1(mp_log2,compile(ss5,se1)); - if (!std::strncmp(ss,"log(",4)) _cimg_mp_opcode1(mp_log,compile(ss4,se1)); - if (!std::strncmp(ss,"exp(",4)) _cimg_mp_opcode1(mp_exp,compile(ss4,se1)); - if (!std::strncmp(ss,"sqrt(",5)) _cimg_mp_opcode1(mp_sqrt,compile(ss5,se1)); - if (!std::strncmp(ss,"sign(",5)) _cimg_mp_opcode1(mp_sign,compile(ss5,se1)); - if (!std::strncmp(ss,"abs(",4)) _cimg_mp_opcode1(mp_abs,compile(ss4,se1)); - if (!std::strncmp(ss,"atan2(",6)) { - char *s1 = ss6; while (s1=mem._width) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(mp_if),pos,mem_cond,mem_A,mem_B,bp2-bp1,code._width-bp2). - move_to(code,bp1); - _cimg_mp_return(pos); - } - if (!std::strncmp(ss,"round(",6)) { - unsigned int value = 0, round = 1, direction = 0; - char *s1 = ss6; while (s1 opcode; - if (mempos>=mem.size()) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(*ss=='k'?mp_kth:ss[1]=='i'?mp_min:ss[1]=='a'?mp_max:mp_med),pos). - move_to(opcode); - for (char *s = ss4; s::vector(compile(s,ns)).move_to(opcode); - s = ns; - } - (opcode>'y').move_to(code); - _cimg_mp_return(pos); - } - if (!std::strncmp(ss,"arg(",4)) { - CImgList opcode; - if (mempos>=mem.size()) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - CImg::vector(_cimg_mp_enfunc(mp_arg),pos).move_to(opcode); - for (char *s = ss4; s::vector(compile(s,ns)).move_to(opcode); - s = ns; - } - (opcode>'y').move_to(code); - _cimg_mp_return(pos); - } - if (!std::strncmp(ss,"narg(",5)) { - if (*ss5==')') _cimg_mp_return(0); - unsigned int nb_args = 0; - for (char *s = ss5; s=mem.size()) mem.resize(-200,1,1,1,0); - const unsigned int pos = mempos++; - mem[pos] = nb_args; - _cimg_mp_return(pos); - } - if (!std::strncmp(ss,"isval(",6)) { - char sep = 0, end = 0; double val = 0; - if (std::sscanf(ss6,"%lf%c%c",&val,&sep,&end)==2 && sep==')') _cimg_mp_return(1); - _cimg_mp_return(0); - } - if (!std::strncmp(ss,"isnan(",6)) _cimg_mp_opcode1(mp_isnan,compile(ss6,se1)); - if (!std::strncmp(ss,"isinf(",6)) _cimg_mp_opcode1(mp_isinf,compile(ss6,se1)); - if (!std::strncmp(ss,"isint(",6)) _cimg_mp_opcode1(mp_isint,compile(ss6,se1)); - if (!std::strncmp(ss,"isbool(",7)) _cimg_mp_opcode1(mp_isbool,compile(ss7,se1)); - if (!std::strncmp(ss,"rol(",4) || !std::strncmp(ss,"ror(",4)) { - unsigned int value = 0, nb = 1; - char *s1 = ss4; while (s1 variable_name(ss,(unsigned int)(se-ss+1)); - variable_name.back() = 0; - if (variable_name[1]) { // Multi-char variable. - cimglist_for(labelM,i) if (!std::strcmp(variable_name,labelM[i])) _cimg_mp_return(labelMpos[i]); - } else if (label1pos[*variable_name]!=~0U) _cimg_mp_return(label1pos[*variable_name]); // Single-char variable. - *se = saved_char; - throw CImgArgumentException("[_cimg_math_parser] " - "CImg<%s>::%s(): Invalid item '%s' in specified expression '%s%s%s'.\n", - pixel_type(),calling_function, - variable_name._data, - (ss-8)>expr._data?"...":"", - (ss-8)>expr._data?ss-8:expr._data, - se<&expr.back()?"...":""); - } - - // Evaluation functions, known by the parser. - // Defining these functions 'static' ensures that sizeof(mp_func)==sizeof(ulong), so we can store pointers to them - // directly in the opcode vectors. - static double mp_u(_cimg_math_parser& mp) { - return mp.mem[mp.opcode(2)] + cimg::rand()*(mp.mem[mp.opcode(3)]-mp.mem[mp.opcode(2)]); - } - static double mp_g(_cimg_math_parser& mp) { - cimg::unused(mp); - return cimg::grand(); - } - static double mp_i(_cimg_math_parser& mp) { - return (double)mp.reference.atXYZC((int)mp.mem[9],(int)mp.mem[10],(int)mp.mem[11],(int)mp.mem[12],0); - } - static double mp_logical_and(_cimg_math_parser& mp) { - const bool is_A = (bool)mp.mem[mp.opcode(2)]; - const CImg *const pE = ++mp.p_code + mp.opcode(4); - if (!is_A) { mp.p_code = pE - 1; return 0; } - const unsigned int mem_B = (unsigned int)mp.opcode(3); - for ( ; mp.p_code &op = *mp.p_code; - mp.opcode._data = op._data; mp.opcode._height = op._height; - const unsigned int target = (unsigned int)mp.opcode[1]; - mp.mem[target] = _cimg_mp_defunc(mp); - } - --mp.p_code; - return (double)(bool)mp.mem[mem_B]; - } - static double mp_logical_or(_cimg_math_parser& mp) { - const bool is_A = (bool)mp.mem[mp.opcode(2)]; - const CImg *const pE = ++mp.p_code + mp.opcode(4); - if (is_A) { mp.p_code = pE - 1; return 1; } - const unsigned int mem_B = (unsigned int)mp.opcode(3); - for ( ; mp.p_code &op = *mp.p_code; - mp.opcode._data = op._data; mp.opcode._height = op._height; - const unsigned int target = (unsigned int)mp.opcode[1]; - mp.mem[target] = _cimg_mp_defunc(mp); - } - --mp.p_code; - return (double)(bool)mp.mem[mem_B]; - } - static double mp_infeq(_cimg_math_parser& mp) { - return (double)(mp.mem[mp.opcode(2)]<=mp.mem[mp.opcode(3)]); - } - static double mp_supeq(_cimg_math_parser& mp) { - return (double)(mp.mem[mp.opcode(2)]>=mp.mem[mp.opcode(3)]); - } - static double mp_noteq(_cimg_math_parser& mp) { - return (double)(mp.mem[mp.opcode(2)]!=mp.mem[mp.opcode(3)]); - } - static double mp_eqeq(_cimg_math_parser& mp) { - return (double)(mp.mem[mp.opcode(2)]==mp.mem[mp.opcode(3)]); - } - static double mp_inf(_cimg_math_parser& mp) { - return (double)(mp.mem[mp.opcode(2)]mp.mem[mp.opcode(3)]); - } - static double mp_add(_cimg_math_parser& mp) { - return mp.mem[mp.opcode(2)] + mp.mem[mp.opcode(3)]; - } - static double mp_sub(_cimg_math_parser& mp) { - return mp.mem[mp.opcode(2)] - mp.mem[mp.opcode(3)]; - } - static double mp_mul(_cimg_math_parser& mp) { - const double A = mp.mem[mp.opcode(2)]; - const CImg *const pE = ++mp.p_code + mp.opcode(4); - if (!A) { mp.p_code = pE - 1; return 0; } - const unsigned int mem_B = (unsigned int)mp.opcode(3); - for ( ; mp.p_code &op = *mp.p_code; - mp.opcode._data = op._data; mp.opcode._height = op._height; - const unsigned int target = (unsigned int)mp.opcode[1]; - mp.mem[target] = _cimg_mp_defunc(mp); - } - --mp.p_code; - return A*(double)mp.mem[mem_B]; - } - static double mp_div(_cimg_math_parser& mp) { - return mp.mem[mp.opcode(2)] / mp.mem[mp.opcode(3)]; - } - static double mp_minus(_cimg_math_parser& mp) { - return -mp.mem[mp.opcode(2)]; - } - static double mp_not(_cimg_math_parser& mp) { - return !mp.mem[mp.opcode(2)]; - } - static double mp_logical_not(_cimg_math_parser& mp) { - return !mp.mem[mp.opcode(2)]; - } - static double mp_bitwise_not(_cimg_math_parser& mp) { - return ~(unsigned long)mp.mem[mp.opcode(2)]; - } - static double mp_modulo(_cimg_math_parser& mp) { - return cimg::mod(mp.mem[mp.opcode(2)],mp.mem[mp.opcode(3)]); - } - static double mp_bitwise_and(_cimg_math_parser& mp) { - return ((unsigned long)mp.mem[mp.opcode(2)] & (unsigned long)mp.mem[mp.opcode(3)]); - } - static double mp_bitwise_or(_cimg_math_parser& mp) { - return ((unsigned long)mp.mem[mp.opcode(2)] | (unsigned long)mp.mem[mp.opcode(3)]); - } - static double mp_pow(_cimg_math_parser& mp) { - const double v = mp.mem[mp.opcode(2)], p = mp.mem[mp.opcode(3)]; - if (p==0) return 1; - if (p==0.5) return std::sqrt(v); - if (p==1) return v; - if (p==2) return v*v; - if (p==3) return v*v*v; - if (p==4) return v*v*v*v; - return std::pow(v,p); - } - static double mp_sin(_cimg_math_parser& mp) { - return std::sin(mp.mem[mp.opcode(2)]); - } - static double mp_cos(_cimg_math_parser& mp) { - return std::cos(mp.mem[mp.opcode(2)]); - } - static double mp_tan(_cimg_math_parser& mp) { - return std::tan(mp.mem[mp.opcode(2)]); - } - static double mp_asin(_cimg_math_parser& mp) { - return std::asin(mp.mem[mp.opcode(2)]); - } - static double mp_acos(_cimg_math_parser& mp) { - return std::acos(mp.mem[mp.opcode(2)]); - } - static double mp_atan(_cimg_math_parser& mp) { - return std::atan(mp.mem[mp.opcode(2)]); - } - static double mp_sinh(_cimg_math_parser& mp) { - return std::sinh(mp.mem[mp.opcode(2)]); - } - static double mp_cosh(_cimg_math_parser& mp) { - return std::cosh(mp.mem[mp.opcode(2)]); - } - static double mp_tanh(_cimg_math_parser& mp) { - return std::tanh(mp.mem[mp.opcode(2)]); - } - static double mp_log10(_cimg_math_parser& mp) { - return std::log10(mp.mem[mp.opcode(2)]); - } - static double mp_log2(_cimg_math_parser& mp) { - return cimg::log2(mp.mem[mp.opcode(2)]); - } - static double mp_log(_cimg_math_parser& mp) { - return std::log(mp.mem[mp.opcode(2)]); - } - static double mp_exp(_cimg_math_parser& mp) { - return std::exp(mp.mem[mp.opcode(2)]); - } - static double mp_sqrt(_cimg_math_parser& mp) { - return std::sqrt(mp.mem[mp.opcode(2)]); - } - static double mp_sign(_cimg_math_parser& mp) { - return cimg::sign(mp.mem[mp.opcode(2)]); - } - static double mp_abs(_cimg_math_parser& mp) { - return cimg::abs(mp.mem[mp.opcode(2)]); - } - static double mp_atan2(_cimg_math_parser& mp) { - return std::atan2(mp.mem[mp.opcode(2)],mp.mem[mp.opcode(3)]); - } - static double mp_if(_cimg_math_parser& mp) { - const bool is_cond = (bool)mp.mem[mp.opcode(2)]; - const unsigned int mem_A = (unsigned int)mp.opcode(3), mem_B = (unsigned int)mp.opcode(4); - const CImg - *const pB = ++mp.p_code + mp.opcode(5), - *const pE = pB + mp.opcode(6); - if (is_cond) { // Evaluate on-the-fly only the correct argument. - for ( ; mp.p_code &op = *mp.p_code; - mp.opcode._data = op._data; mp.opcode._height = op._height; - const unsigned int target = (unsigned int)mp.opcode[1]; - mp.mem[target] = _cimg_mp_defunc(mp); - } - mp.p_code = pE - 1; - return mp.mem[mem_A]; - } - for (mp.p_code = pB; mp.p_code &op = *mp.p_code; - mp.opcode._data = op._data; mp.opcode._height = op._height; - const unsigned int target = (unsigned int)mp.opcode[1]; - mp.mem[target] = _cimg_mp_defunc(mp); - } - --mp.p_code; - return mp.mem[mem_B]; - } - static double mp_round(_cimg_math_parser& mp) { - return cimg::round(mp.mem[mp.opcode(2)],mp.mem[mp.opcode(3)],(int)mp.mem[mp.opcode(4)]); - } - static double mp_ixyzc(_cimg_math_parser& mp) { - const int i = (int)mp.mem[mp.opcode(6)], b = (int)mp.mem[mp.opcode(7)]; - if (i==0) { // Nearest neighbor interpolation. - if (b==2) return (double)mp.reference.atXYZC(cimg::mod((int)mp.mem[mp.opcode(2)],mp.reference.width()), - cimg::mod((int)mp.mem[mp.opcode(3)],mp.reference.height()), - cimg::mod((int)mp.mem[mp.opcode(4)],mp.reference.depth()), - cimg::mod((int)mp.mem[mp.opcode(5)],mp.reference.spectrum())); - if (b==1) return (double)mp.reference.atXYZC((int)mp.mem[mp.opcode(2)], - (int)mp.mem[mp.opcode(3)], - (int)mp.mem[mp.opcode(4)], - (int)mp.mem[mp.opcode(5)]); - return (double)mp.reference.atXYZC((int)mp.mem[mp.opcode(2)], - (int)mp.mem[mp.opcode(3)], - (int)mp.mem[mp.opcode(4)], - (int)mp.mem[mp.opcode(5)],0); - } else { // Linear interpolation. - if (b==2) return (double)mp.reference.linear_atXYZC(cimg::mod((float)mp.mem[mp.opcode(2)],(float)mp.reference.width()), - cimg::mod((float)mp.mem[mp.opcode(3)],(float)mp.reference.height()), - cimg::mod((float)mp.mem[mp.opcode(4)],(float)mp.reference.depth()), - cimg::mod((float)mp.mem[mp.opcode(5)],(float)mp.reference.spectrum())); - if (b==1) return (double)mp.reference.linear_atXYZC((float)mp.mem[mp.opcode(2)], - (float)mp.mem[mp.opcode(3)], - (float)mp.mem[mp.opcode(4)], - (float)mp.mem[mp.opcode(5)]); - return (double)mp.reference.linear_atXYZC((float)mp.mem[mp.opcode(2)], - (float)mp.mem[mp.opcode(3)], - (float)mp.mem[mp.opcode(4)], - (float)mp.mem[mp.opcode(5)],0); - } - } - static double mp_jxyzc(_cimg_math_parser& mp) { - const double x = mp.mem[9], y = mp.mem[10], z = mp.mem[11], c = mp.mem[12]; - const int i = (int)mp.mem[mp.opcode(6)], b = (int)mp.mem[mp.opcode(7)]; - if (i==0) { // Nearest neighbor interpolation. - if (b==2) return (double)mp.reference.atXYZC(cimg::mod((int)(x+mp.mem[mp.opcode(2)]),mp.reference.width()), - cimg::mod((int)(y+mp.mem[mp.opcode(3)]),mp.reference.height()), - cimg::mod((int)(z+mp.mem[mp.opcode(4)]),mp.reference.depth()), - cimg::mod((int)(c+mp.mem[mp.opcode(5)]),mp.reference.spectrum())); - if (b==1) return (double)mp.reference.atXYZC((int)(x+mp.mem[mp.opcode(2)]), - (int)(y+mp.mem[mp.opcode(3)]), - (int)(z+mp.mem[mp.opcode(4)]), - (int)(c+mp.mem[mp.opcode(5)])); - return (double)mp.reference.atXYZC((int)(x+mp.mem[mp.opcode(2)]), - (int)(y+mp.mem[mp.opcode(3)]), - (int)(z+mp.mem[mp.opcode(4)]), - (int)(c+mp.mem[mp.opcode(5)]),0); - } else { // Linear interpolation. - if (b==2) return (double)mp.reference.linear_atXYZC(cimg::mod((float)(x+mp.mem[mp.opcode(2)]),(float)mp.reference.width()), - cimg::mod((float)(y+mp.mem[mp.opcode(3)]),(float)mp.reference.height()), - cimg::mod((float)(z+mp.mem[mp.opcode(4)]),(float)mp.reference.depth()), - cimg::mod((float)(c+mp.mem[mp.opcode(5)]),(float)mp.reference.spectrum())); - if (b==1) return (double)mp.reference.linear_atXYZC((float)(x+mp.mem[mp.opcode(2)]), - (float)(y+mp.mem[mp.opcode(3)]), - (float)(z+mp.mem[mp.opcode(4)]), - (float)(c+mp.mem[mp.opcode(5)])); - return (double)mp.reference.linear_atXYZC((float)(x+mp.mem[mp.opcode(2)]), - (float)(y+mp.mem[mp.opcode(3)]), - (float)(z+mp.mem[mp.opcode(4)]), - (float)(c+mp.mem[mp.opcode(5)]),0); - } - } - static double mp_min(_cimg_math_parser& mp) { - double val = mp.mem[mp.opcode(2)]; - for (unsigned int i = 3; i values(mp.opcode._height-2); - double *p = values.data(); - for (unsigned int i = 2; i values(mp.opcode._height-3); - double *p = values.data(); - for (unsigned int i = 3; i::is_nan(val); - } - static double mp_isinf(_cimg_math_parser& mp) { - const double val = mp.mem[mp.opcode(2)]; - return cimg::type::is_inf(val); - } - static double mp_isint(_cimg_math_parser& mp) { - const double val = mp.mem[mp.opcode(2)]; - return (double)(cimg::mod(val,1.0)==0); - } - static double mp_isbool(_cimg_math_parser& mp) { - const double val = mp.mem[mp.opcode(2)]; - return (val==0.0 || val==1.0); - } - static double mp_rol(_cimg_math_parser& mp) { - return cimg::rol(mp.mem[mp.opcode(2)],(unsigned int)mp.mem[mp.opcode(3)]); - } - static double mp_ror(_cimg_math_parser& mp) { - return cimg::ror(mp.mem[mp.opcode(2)],(unsigned int)mp.mem[mp.opcode(3)]); - } - static double mp_lsl(_cimg_math_parser& mp) { - return (long)mp.mem[mp.opcode(2)]<<(unsigned int)mp.mem[mp.opcode(3)]; - } - static double mp_lsr(_cimg_math_parser& mp) { - return (long)mp.mem[mp.opcode(2)]>>(unsigned int)mp.mem[mp.opcode(3)]; - } - static double mp_sinc(_cimg_math_parser& mp) { - return cimg::sinc(mp.mem[mp.opcode(2)]); - } - static double mp_im(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[0]:0; - } - static double mp_iM(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[1]:0; - } - static double mp_ia(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[2]:0; - } - static double mp_iv(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[3]:0; - } - static double mp_xm(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[4]:0; - } - static double mp_ym(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[5]:0; - } - static double mp_zm(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[6]:0; - } - static double mp_cm(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[7]:0; - } - static double mp_xM(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[8]:0; - } - static double mp_yM(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[9]:0; - } - static double mp_zM(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[10]:0; - } - static double mp_cM(_cimg_math_parser& mp) { - if (!mp.reference_stats) mp.reference.get_stats().move_to(mp.reference_stats); - return mp.reference_stats?mp.reference_stats[11]:0; - } - static double mp_arg(_cimg_math_parser& mp) { - const int _ind = (int)mp.mem[mp.opcode(2)]; - const unsigned int nb_args = mp.opcode._height-2, ind = _ind<0?_ind+nb_args:(unsigned int)_ind; - if (ind>=nb_args) return 0; - return mp.mem[mp.opcode(ind+2)]; - } - static double mp_int(_cimg_math_parser& mp) { - return (double)(long)mp.mem[mp.opcode(2)]; - } - static double mp_ioff(_cimg_math_parser& mp) { - const unsigned long off = (unsigned long)mp.mem[mp.opcode(2)]; - if (off>=mp.reference.size()) return 0; - return (double)mp.reference[off]; - } - static double mp_joff(_cimg_math_parser& mp) { - const int x = (int)mp.mem[9], y = (int)mp.mem[10], z = (int)mp.mem[11], c = (int)mp.mem[12]; - const unsigned long off = mp.reference.offset(x,y,z,c) + (unsigned long)(mp.mem[mp.opcode(2)]); - if (off>=mp.reference.size()) return 0; - return (double)mp.reference[off]; - } - - // Evaluation procedure, with image data. - double operator()(const double x, const double y, const double z, const double c) { - if (!mem) return 0; - mem[9] = x; mem[10] = y; mem[11] = z; mem[12] = c; - opcode._is_shared = true; opcode._width = opcode._depth = opcode._spectrum = 1; - - for (p_code = code._data; p_code &op = *p_code; - // Allows to avoid parameter passing to evaluation functions. - opcode._data = op._data; opcode._height = op._height; - const unsigned int target = (unsigned int)opcode[1]; - mem[target] = _cimg_mp_defunc(*this); - } - return mem[result]; - } - }; - - //! Compute the square value of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its square value \f$I_{(x,y,z,c)}^2\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - \par Example - \code - const CImg img("reference.jpg"); - (img,img.get_sqr().normalize(0,255)).display(); - \endcode - \image html ref_sqr.jpg - **/ - CImg& sqr() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=524288) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(val*val); }; - return *this; - } - - //! Compute the square value of each pixel value \newinstance. - CImg get_sqr() const { - return CImg(*this,false).sqr(); - } - - //! Compute the square root of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its square root \f$\sqrt{I_{(x,y,z,c)}}\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - \par Example - \code - const CImg img("reference.jpg"); - (img,img.get_sqrt().normalize(0,255)).display(); - \endcode - \image html ref_sqrt.jpg - **/ - CImg& sqrt() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::sqrt((double)*ptrd); - return *this; - } - - //! Compute the square root of each pixel value \newinstance. - CImg get_sqrt() const { - return CImg(*this,false).sqrt(); - } - - //! Compute the exponential of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its exponential \f$e^{I_{(x,y,z,c)}}\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& exp() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=4096) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::exp((double)*ptrd); - return *this; - } - - //! Compute the exponential of each pixel value \newinstance. - CImg get_exp() const { - return CImg(*this,false).exp(); - } - - //! Compute the logarithm of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its logarithm - \f$\mathrm{log}_{e}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& log() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=262144) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::log((double)*ptrd); - return *this; - } - - //! Compute the logarithm of each pixel value \newinstance. - CImg get_log() const { - return CImg(*this,false).log(); - } - - //! Compute the base-2 logarithm of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-2 logarithm - \f$\mathrm{log}_{2}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& log2() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=4096) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::log2((double)*ptrd); - return *this; - } - - //! Compute the base-10 logarithm of each pixel value \newinstance. - CImg get_log2() const { - return CImg(*this,false).log2(); - } - - //! Compute the base-10 logarithm of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-10 logarithm - \f$\mathrm{log}_{10}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& log10() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=4096) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::log10((double)*ptrd); - return *this; - } - - //! Compute the base-10 logarithm of each pixel value \newinstance. - CImg get_log10() const { - return CImg(*this,false).log10(); - } - - //! Compute the absolute value of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its absolute value \f$|I_{(x,y,z,c)}|\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& abs() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=524288) -#endif - cimg_rof(*this,ptrd,T) *ptrd = cimg::abs(*ptrd); - return *this; - } - - //! Compute the absolute value of each pixel value \newinstance. - CImg get_abs() const { - return CImg(*this,false).abs(); - } - - //! Compute the sign of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sign - \f$\mathrm{sign}(I_{(x,y,z,c)})\f$. - \note - - The sign is set to: - - \c 1 if pixel value is strictly positive. - - \c -1 if pixel value is strictly negative. - - \c 0 if pixel value is equal to \c 0. - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& sign() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = cimg::sign(*ptrd); - return *this; - } - - //! Compute the sign of each pixel value \newinstance. - CImg get_sign() const { - return CImg(*this,false).sign(); - } - - //! Compute the cosine of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its cosine \f$\cos(I_{(x,y,z,c)})\f$. - \note - - Pixel values are regarded as being in \e radian. - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& cos() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::cos((double)*ptrd); - return *this; - } - - //! Compute the cosine of each pixel value \newinstance. - CImg get_cos() const { - return CImg(*this,false).cos(); - } - - //! Compute the sine of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sine \f$\sin(I_{(x,y,z,c)})\f$. - \note - - Pixel values are regarded as being in \e radian. - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& sin() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::sin((double)*ptrd); - return *this; - } - - //! Compute the sine of each pixel value \newinstance. - CImg get_sin() const { - return CImg(*this,false).sin(); - } - - //! Compute the sinc of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sinc - \f$\mathrm{sinc}(I_{(x,y,z,c)})\f$. - \note - - Pixel values are regarded as being exin \e radian. - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& sinc() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=2048) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::sinc((double)*ptrd); - return *this; - } - - //! Compute the sinc of each pixel value \newinstance. - CImg get_sinc() const { - return CImg(*this,false).sinc(); - } - - //! Compute the tangent of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its tangent \f$\tan(I_{(x,y,z,c)})\f$. - \note - - Pixel values are regarded as being exin \e radian. - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& tan() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=2048) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::tan((double)*ptrd); - return *this; - } - - //! Compute the tangent of each pixel value \newinstance. - CImg get_tan() const { - return CImg(*this,false).tan(); - } - - //! Compute the hyperbolic cosine of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic cosine - \f$\mathrm{cosh}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& cosh() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=2048) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::cosh((double)*ptrd); - return *this; - } - - //! Compute the hyperbolic cosine of each pixel value \newinstance. - CImg get_cosh() const { - return CImg(*this,false).cosh(); - } - - //! Compute the hyperbolic sine of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic sine - \f$\mathrm{sinh}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& sinh() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=2048) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::sinh((double)*ptrd); - return *this; - } - - //! Compute the hyperbolic sine of each pixel value \newinstance. - CImg get_sinh() const { - return CImg(*this,false).sinh(); - } - - //! Compute the hyperbolic tangent of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic tangent - \f$\mathrm{tanh}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& tanh() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=2048) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::tanh((double)*ptrd); - return *this; - } - - //! Compute the hyperbolic tangent of each pixel value \newinstance. - CImg get_tanh() const { - return CImg(*this,false).tanh(); - } - - //! Compute the arccosine of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arccosine - \f$\mathrm{acos}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& acos() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::acos((double)*ptrd); - return *this; - } - - //! Compute the arccosine of each pixel value \newinstance. - CImg get_acos() const { - return CImg(*this,false).acos(); - } - - //! Compute the arcsine of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arcsine - \f$\mathrm{asin}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& asin() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::asin((double)*ptrd); - return *this; - } - - //! Compute the arcsine of each pixel value \newinstance. - CImg get_asin() const { - return CImg(*this,false).asin(); - } - - //! Compute the arctangent of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent - \f$\mathrm{atan}(I_{(x,y,z,c)})\f$. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - **/ - CImg& atan() { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::atan((double)*ptrd); - return *this; - } - - //! Compute the arctangent of each pixel value \newinstance. - CImg get_atan() const { - return CImg(*this,false).atan(); - } - - //! Compute the arctangent2 of each pixel value. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent2 - \f$\mathrm{atan2}(I_{(x,y,z,c)})\f$. - \param img Image whose pixel values specify the second argument of the \c atan2() function. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - \par Example - \code - const CImg - img_x(100,100,1,1,"x-w/2",false), // Define an horizontal centered gradient, from '-width/2' to 'width/2'. - img_y(100,100,1,1,"y-h/2",false), // Define a vertical centered gradient, from '-height/2' to 'height/2'. - img_atan2 = img_y.get_atan2(img_x); // Compute atan2(y,x) for each pixel value. - (img_x,img_y,img_atan2).display(); - \endcode - **/ - template - CImg& atan2(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return atan2(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg get_atan2(const CImg& img) const { - return CImg(*this,false).atan2(img); - } - - //! In-place pointwise multiplication. - /** - Compute the pointwise multiplication between the image instance and the specified input image \c img. - \param img Input image, as the second operand of the multiplication. - \note - - Similar to operator+=(const CImg&), except that it performs a pointwise multiplication - instead of an addition. - - It does \e not perform a \e matrix multiplication. For this purpose, use operator*=(const CImg&) instead. - \par Example - \code - CImg - img("reference.jpg"), - shade(img.width,img.height(),1,1,"-(x-w/2)^2-(y-h/2)^2",false); - shade.normalize(0,1); - (img,shade,img.get_mul(shade)).display(); - \endcode - **/ - template - CImg& mul(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return mul(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg<_cimg_Tt> get_mul(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false).mul(img); - } - - //! In-place pointwise division. - /** - Similar to mul(const CImg&), except that it performs a pointwise division instead of a multiplication. - **/ - template - CImg& div(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return div(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg<_cimg_Tt> get_div(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false).div(img); - } - - //! Raise each pixel value to a specified power. - /** - Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its power \f$I_{(x,y,z,c)}^p\f$. - \param p Exponent value. - \note - - The \inplace of this method statically casts the computed values to the pixel type \c T. - - The \newinstance returns a \c CImg image, if the pixel type \c T is \e not float-valued. - \par Example - \code - const CImg - img0("reference.jpg"), // Load reference color image. - img1 = (img0/255).pow(1.8)*=255, // Compute gamma correction, with gamma = 1.8. - img2 = (img0/255).pow(0.5)*=255; // Compute gamma correction, with gamma = 0.5. - (img0,img1,img2).display(); - \endcode - **/ - CImg& pow(const double p) { - if (is_empty()) return *this; - if (p==-4) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/(val*val*val*val)); } - return *this; - } - if (p==-3) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/(val*val*val)); } - return *this; - } - if (p==-2) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/(val*val)); } - return *this; - } - if (p==-1) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1.0/val); } - return *this; - } - if (p==-0.5) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = (T)(1/std::sqrt((double)val)); } - return *this; - } - if (p==0) return fill(1); - if (p==0.5) return sqrt(); - if (p==1) return *this; - if (p==2) return sqr(); - if (p==3) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=262144) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = val*val*val; } - return *this; - } - if (p==4) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=131072) -#endif - cimg_rof(*this,ptrd,T) { const T val = *ptrd; *ptrd = val*val*val*val; } - return *this; - } -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=1024) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)std::pow((double)*ptrd,p); - return *this; - } - - //! Raise each pixel value to a specified power \newinstance. - CImg get_pow(const double p) const { - return CImg(*this,false).pow(p); - } - - //! Raise each pixel value to a power, specified from an expression. - /** - Similar to operator+=(const char*), except it performs a pointwise exponentiation instead of an addition. - **/ - CImg& pow(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"pow"); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)std::pow((double)*ptrd,mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)std::pow((double)*ptrd,mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)std::pow((double)*ptrd,lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)std::pow((double)*ptrd,mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - CImg values(_width,_height,_depth,_spectrum); - try { - values.fill(expression,true); - } catch (CImgException&) { - cimg::exception_mode() = omode; - values.load(expression); - } - pow(values); - } - cimg::exception_mode() = omode; - return *this; - } - - //! Raise each pixel value to a power, specified from an expression \newinstance. - CImg get_pow(const char *const expression) const { - return CImg(*this,false).pow(expression); - } - - //! Raise each pixel value to a power, pointwisely specified from another image. - /** - Similar to operator+=(const CImg& img), except that it performs an exponentiation instead of an addition. - **/ - template - CImg& pow(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return pow(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg get_pow(const CImg& img) const { - return CImg(*this,false).pow(img); - } - - //! Compute the bitwise left rotation of each pixel value. - /** - Similar to operator<<=(unsigned int), except that it performs a left rotation instead of a left shift. - **/ - CImg& rol(const unsigned int n=1) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::rol(*ptrd,n); - return *this; - } - - //! Compute the bitwise left rotation of each pixel value \newinstance. - CImg get_rol(const unsigned int n=1) const { - return (+*this).rol(n); - } - - //! Compute the bitwise left rotation of each pixel value. - /** - Similar to operator<<=(const char*), except that it performs a left rotation instead of a left shift. - **/ - CImg& rol(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"rol"); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') - cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::rol(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - CImg values(_width,_height,_depth,_spectrum); - try { - values.fill(expression,true); - } catch (CImgException&) { - cimg::exception_mode() = omode; - values.load(expression); - } - rol(values); - } - cimg::exception_mode() = omode; - return *this; - } - - //! Compute the bitwise left rotation of each pixel value \newinstance. - CImg get_rol(const char *const expression) const { - return (+*this).rol(expression); - } - - //! Compute the bitwise left rotation of each pixel value. - /** - Similar to operator<<=(const CImg&), except that it performs a left rotation instead of a left shift. - **/ - template - CImg& rol(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return rol(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg get_rol(const CImg& img) const { - return (+*this).rol(img); - } - - //! Compute the bitwise right rotation of each pixel value. - /** - Similar to operator>>=(unsigned int), except that it performs a right rotation instead of a right shift. - **/ - CImg& ror(const unsigned int n=1) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)cimg::ror(*ptrd,n); - return *this; - } - - //! Compute the bitwise right rotation of each pixel value \newinstance. - CImg get_ror(const unsigned int n=1) const { - return (+*this).ror(n); - } - - //! Compute the bitwise right rotation of each pixel value. - /** - Similar to operator>>=(const char*), except that it performs a right rotation instead of a right shift. - **/ - CImg& ror(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"ror"); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') - cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::ror(*ptrd,(unsigned int)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - CImg values(_width,_height,_depth,_spectrum); - try { - values.fill(expression,true); - } catch (CImgException&) { - cimg::exception_mode() = omode; - values.load(expression); - } - ror(values); - } - cimg::exception_mode() = omode; - return *this; - } - - //! Compute the bitwise right rotation of each pixel value \newinstance. - CImg get_ror(const char *const expression) const { - return (+*this).ror(expression); - } - - //! Compute the bitwise right rotation of each pixel value. - /** - Similar to operator>>=(const CImg&), except that it performs a right rotation instead of a right shift. - **/ - template - CImg& ror(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return ror(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg get_ror(const CImg& img) const { - return (+*this).ror(img); - } - - //! Pointwise min operator between instance image and a value. - /** - \param val Value used as the reference argument of the min operator. - \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by - \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{val})\f$. - **/ - CImg& min(const T val) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = cimg::min(*ptrd,val); - return *this; - } - - //! Pointwise min operator between instance image and a value \newinstance. - CImg get_min(const T val) const { - return (+*this).min(val); - } - - //! Pointwise min operator between two images. - /** - \param img Image used as the reference argument of the min operator. - \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by - \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$. - **/ - template - CImg& min(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return min(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg<_cimg_Tt> get_min(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false).min(img); - } - - //! Pointwise min operator between an image and an expression. - /** - \param expression Math formula as a C-string. - \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by - \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$. - **/ - CImg& min(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"min"); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::min(*ptrd,(T)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::min(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)cimg::min(*ptrd,(T)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::min(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - CImg values(_width,_height,_depth,_spectrum); - try { - values.fill(expression,true); - } catch (CImgException&) { - cimg::exception_mode() = omode; - values.load(expression); - } - min(values); - } - cimg::exception_mode() = omode; - return *this; - } - - //! Pointwise min operator between an image and an expression \newinstance. - CImg get_min(const char *const expression) const { - return CImg(*this,false).min(expression); - } - - //! Pointwise max operator between instance image and a value. - /** - \param val Value used as the reference argument of the max operator. - \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by - \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{val})\f$. - **/ - CImg& max(const T val) { - if (is_empty()) return *this; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = cimg::max(*ptrd,val); - return *this; - } - - //! Pointwise max operator between instance image and a value \newinstance. - CImg get_max(const T val) const { - return (+*this).max(val); - } - - //! Pointwise max operator between two images. - /** - \param img Image used as the reference argument of the max operator. - \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by - \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$. - **/ - template - CImg& max(const CImg& img) { - const unsigned long siz = size(), isiz = img.size(); - if (siz && isiz) { - if (is_overlapped(img)) return max(+img); - T *ptrd = _data, *const ptre = _data + siz; - if (siz>isiz) for (unsigned long n = siz/isiz; n; --n) - for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs - CImg<_cimg_Tt> get_max(const CImg& img) const { - return CImg<_cimg_Tt>(*this,false).max(img); - } - - //! Pointwise max operator between an image and an expression. - /** - \param expression Math formula as a C-string. - \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by - \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$. - **/ - CImg& max(const char *const expression) { - if (is_empty()) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"max"); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::max(*ptrd,(T)mp(x,y,z,c)); --ptrd; } - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::max(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) { *ptrd = (T)cimg::max(*ptrd,(T)lmp(x,y,z,c)); ++ptrd; } - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) { *ptrd = (T)cimg::max(*ptrd,(T)mp(x,y,z,c)); ++ptrd; } - } - } catch (CImgException&) { - CImg values(_width,_height,_depth,_spectrum); - try { - values.fill(expression,true); - } catch (CImgException&) { - cimg::exception_mode() = omode; - values.load(expression); - } - max(values); - } - cimg::exception_mode() = omode; - return *this; - } - - //! Pointwise max operator between an image and an expression \newinstance. - CImg get_max(const char *const expression) const { - return CImg(*this,false).max(expression); - } - - //! Return a reference to the minimum pixel value. - /** - **/ - T& min() { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "min(): Empty instance.", - cimg_instance); - T *ptr_min = _data; - T min_value = *ptr_min; - cimg_for(*this,ptrs,T) if (*ptrsmax_value) max_value = *(ptr_max=ptrs); - return *ptr_max; - } - - //! Return a reference to the maximum pixel value \const. - const T& max() const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "max(): Empty instance.", - cimg_instance); - const T *ptr_max = _data; - T max_value = *ptr_max; - cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs); - return *ptr_max; - } - - //! Return a reference to the minimum pixel value as well as the maximum pixel value. - /** - \param[out] max_val Maximum pixel value. - **/ - template - T& min_max(t& max_val) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "min_max(): Empty instance.", - cimg_instance); - T *ptr_min = _data; - T min_value = *ptr_min, max_value = min_value; - cimg_for(*this,ptrs,T) { - const T val = *ptrs; - if (valmax_value) max_value = val; - } - max_val = (t)max_value; - return *ptr_min; - } - - //! Return a reference to the minimum pixel value as well as the maximum pixel value \const. - template - const T& min_max(t& max_val) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "min_max(): Empty instance.", - cimg_instance); - const T *ptr_min = _data; - T min_value = *ptr_min, max_value = min_value; - cimg_for(*this,ptrs,T) { - const T val = *ptrs; - if (valmax_value) max_value = val; - } - max_val = (t)max_value; - return *ptr_min; - } - - //! Return a reference to the maximum pixel value as well as the minimum pixel value. - /** - \param[out] min_val Minimum pixel value. - **/ - template - T& max_min(t& min_val) { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "max_min(): Empty instance.", - cimg_instance); - T *ptr_max = _data; - T max_value = *ptr_max, min_value = max_value; - cimg_for(*this,ptrs,T) { - const T val = *ptrs; - if (val>max_value) { max_value = val; ptr_max = ptrs; } - if (val - const T& max_min(t& min_val) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "max_min(): Empty instance.", - cimg_instance); - const T *ptr_max = _data; - T max_value = *ptr_max, min_value = max_value; - cimg_for(*this,ptrs,T) { - const T val = *ptrs; - if (val>max_value) { max_value = val; ptr_max = ptrs; } - if (val arr(*this); - unsigned int l = 0, ir = size() - 1; - for (;;) { - if (ir<=l+1) { - if (ir==l+1 && arr[ir]>1; - cimg::swap(arr[mid],arr[l+1]); - if (arr[l]>arr[ir]) cimg::swap(arr[l],arr[ir]); - if (arr[l+1]>arr[ir]) cimg::swap(arr[l+1],arr[ir]); - if (arr[l]>arr[l+1]) cimg::swap(arr[l],arr[l+1]); - unsigned int i = l + 1, j = ir; - const T pivot = arr[l+1]; - for (;;) { - do ++i; while (arr[i]pivot); - if (j=k) ir = j - 1; - if (j<=k) l = i; - } - } - } - - //! Return the median pixel value. - /** - **/ - T median() const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "median(): Empty instance.", - cimg_instance); - const unsigned int s = size(); - const T res = kth_smallest(s>>1); - return (s%2)?res:((res+kth_smallest((s>>1)-1))/2); - } - - //! Return the sum of all the pixel values. - /** - **/ - Tdouble sum() const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "sum(): Empty instance.", - cimg_instance); - Tdouble res = 0; - cimg_for(*this,ptrs,T) res+=(Tdouble)*ptrs; - return res; - } - - //! Return the average pixel value. - /** - **/ - Tdouble mean() const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "mean(): Empty instance.", - cimg_instance); - Tdouble res = 0; - cimg_for(*this,ptrs,T) res+=(Tdouble)*ptrs; - return res/size(); - } - - //! Return the variance of the pixel values. - /** - \param variance_method Method used to estimate the variance. Can be: - - \c 0: Second moment, computed as - \f$1/N \sum\limits_{k=1}^{N} (x_k - \bar x)^2 = - 1/N \left( \sum\limits_{k=1}^N x_k^2 - \left( \sum\limits_{k=1}^N x_k \right)^2 / N \right)\f$ - with \f$ \bar x = 1/N \sum\limits_{k=1}^N x_k \f$. - - \c 1: Best unbiased estimator, computed as \f$\frac{1}{N-1} \sum\limits_{k=1}^{N} (x_k - \bar x)^2 \f$. - - \c 2: Least median of squares. - - \c 3: Least trimmed of squares. - **/ - Tdouble variance(const unsigned int variance_method=1) const { - Tdouble foo; - return variance_mean(variance_method,foo); - } - - //! Return the variance as well as the average of the pixel values. - /** - \param variance_method Method used to estimate the variance (see variance(const unsigned int) const). - \param[out] mean Average pixel value. - **/ - template - Tdouble variance_mean(const unsigned int variance_method, t& mean) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "variance_mean(): Empty instance.", - cimg_instance); - - Tdouble variance = 0, average = 0; - const unsigned long siz = size(); - switch (variance_method) { - case 0 :{ // Least mean square (standard definition) - Tdouble S = 0, S2 = 0; - cimg_for(*this,ptrs,T) { const Tdouble val = (Tdouble)*ptrs; S+=val; S2+=val*val; } - variance = (S2 - S*S/siz)/siz; - average = S; - } break; - case 1 : { // Least mean square (robust definition) - Tdouble S = 0, S2 = 0; - cimg_for(*this,ptrs,T) { const Tdouble val = (Tdouble)*ptrs; S+=val; S2+=val*val; } - variance = siz>1?(S2 - S*S/siz)/(siz - 1):0; - average = S; - } break; - case 2 : { // Least Median of Squares (MAD) - CImg buf(*this,false); - buf.sort(); - const unsigned long siz2 = siz>>1; - const Tdouble med_i = (double)buf[siz2]; - cimg_for(buf,ptrs,Tfloat) { - const Tdouble val = (Tdouble)*ptrs; *ptrs = (Tfloat)cimg::abs(val - med_i); average+=val; - } - buf.sort(); - const Tdouble sig = (Tdouble)(1.4828*buf[siz2]); - variance = sig*sig; - } break; - default : { // Least trimmed of Squares - CImg buf(*this,false); - const unsigned long siz2 = siz>>1; - cimg_for(buf,ptrs,Tfloat) { - const Tdouble val = (Tdouble)*ptrs; (*ptrs)=(Tfloat)((*ptrs)*val); average+=val; - } - buf.sort(); - Tdouble a = 0; - const Tfloat *ptrs = buf._data; - for (unsigned long j = 0; j0?variance:0; - } - - //! Return estimated variance of the noise. - /** - \param variance_method Method used to compute the variance (see variance(const unsigned int) const). - \note Because of structures such as edges in images it is - recommanded to use a robust variance estimation. The variance of the - noise is estimated by computing the variance of the Laplacian \f$(\Delta - I)^2 \f$ scaled by a factor \f$c\f$ insuring \f$ c E[(\Delta I)^2]= - \sigma^2\f$ where \f$\sigma\f$ is the noise variance. - **/ - Tdouble variance_noise(const unsigned int variance_method=2) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "variance_noise(): Empty instance.", - cimg_instance); - - const unsigned long siz = size(); - if (!siz || !_data) return 0; - if (variance_method>1) { // Compute a scaled version of the Laplacian. - CImg tmp(*this); - if (_depth==1) { - const Tdouble cste = 1.0/std::sqrt(20.0); // Depends on how the Laplacian is computed. -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height>=262144 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - CImg_3x3(I,T); - cimg_for3x3(*this,x,y,0,c,I,T) { - tmp(x,y,c) = cste*((Tdouble)Inc + (Tdouble)Ipc + (Tdouble)Icn + - (Tdouble)Icp - 4*(Tdouble)Icc); - } - } - } else { - const Tdouble cste = 1.0/std::sqrt(42.0); // Depends on how the Laplacian is computed. -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=262144 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - CImg_3x3x3(I,T); - cimg_for3x3x3(*this,x,y,z,c,I,T) { - tmp(x,y,z,c) = cste*( - (Tdouble)Incc + (Tdouble)Ipcc + (Tdouble)Icnc + (Tdouble)Icpc + - (Tdouble)Iccn + (Tdouble)Iccp - 6*(Tdouble)Iccc); - } - } - } - return tmp.variance(variance_method); - } - - // Version that doesn't need intermediate images. - Tdouble variance = 0, S = 0, S2 = 0; - if (_depth==1) { - const Tdouble cste = 1.0/std::sqrt(20.0); - CImg_3x3(I,T); - cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,T) { - const Tdouble val = cste*((Tdouble)Inc + (Tdouble)Ipc + - (Tdouble)Icn + (Tdouble)Icp - 4*(Tdouble)Icc); - S+=val; S2+=val*val; - } - } else { - const Tdouble cste = 1.0/std::sqrt(42.0); - CImg_3x3x3(I,T); - cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,T) { - const Tdouble val = cste * - ((Tdouble)Incc + (Tdouble)Ipcc + (Tdouble)Icnc + - (Tdouble)Icpc + - (Tdouble)Iccn + (Tdouble)Iccp - 6*(Tdouble)Iccc); - S+=val; S2+=val*val; - } - } - if (variance_method) variance = siz>1?(S2 - S*S/siz)/(siz - 1):0; - else variance = (S2 - S*S/siz)/siz; - return variance>0?variance:0; - } - - //! Compute the MSE (Mean-Squared Error) between two images. - /** - \param img Image used as the second argument of the MSE operator. - **/ - template - Tdouble MSE(const CImg& img) const { - if (img.size()!=size()) - throw CImgArgumentException(_cimg_instance - "MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.", - cimg_instance, - img._width,img._height,img._depth,img._spectrum,img._data); - Tdouble vMSE = 0; - const t* ptr2 = img._data; - cimg_for(*this,ptr1,T) { - const Tdouble diff = (Tdouble)*ptr1 - (Tdouble)*(ptr2++); - vMSE+=diff*diff; - } - const unsigned long siz = img.size(); - if (siz) vMSE/=siz; - return vMSE; - } - - //! Compute the PSNR (Peak Signal-to-Noise Ratio) between two images. - /** - \param img Image used as the second argument of the PSNR operator. - \param max_value Maximum theoretical value of the signal. - **/ - template - Tdouble PSNR(const CImg& img, const Tdouble max_value=255) const { - const Tdouble vMSE = (Tdouble)std::sqrt(MSE(img)); - return (vMSE!=0)?(Tdouble)(20*std::log10(max_value/vMSE)):(Tdouble)(cimg::type::max()); - } - - //! Evaluate math formula. - /** - \param expression Math formula, as a C-string. - \param x Value of the pre-defined variable \c x. - \param y Value of the pre-defined variable \c y. - \param z Value of the pre-defined variable \c z. - \param c Value of the pre-defined variable \c c. - **/ - double eval(const char *const expression, - const double x=0, const double y=0, const double z=0, const double c=0) const { - if (!expression) return 0; - return _cimg_math_parser(*this,expression,"eval")(x,y,z,c); - } - - //! Evaluate math formula on a set of variables. - /** - \param expression Math formula, as a C-string. - \param xyzc Set of values (x,y,z,c) used for the evaluation. - **/ - template - CImg eval(const char *const expression, const CImg& xyzc) const { - CImg res(1,xyzc.size()/4); - if (!expression) return res.fill(0); - _cimg_math_parser mp(*this,expression,"eval"); -#ifdef cimg_use_openmp -#pragma omp parallel if (res._height>=512 && std::strlen(expression)>=6) - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for - for (unsigned int i = 0; i[min; max; mean; variance; xmin; ymin; zmin; cmin; xmax; ymax; zmax; cmax]. - **/ - CImg get_stats(const unsigned int variance_method=1) const { - if (is_empty()) return CImg(); - const unsigned long siz = size(); - const T *const odata = _data; - const T *pm = odata, *pM = odata; - Tdouble S = 0, S2 = 0; - T m = *pm, M = m; - cimg_for(*this,ptrs,T) { - const T val = *ptrs; - const Tdouble _val = (Tdouble)val; - if (valM) { M = val; pM = ptrs; } - S+=_val; - S2+=_val*_val; - } - const Tdouble - mean_value = S/siz, - _variance_value = variance_method==0?(S2 - S*S/siz)/siz: - (variance_method==1?(siz>1?(S2 - S*S/siz)/(siz - 1):0): - variance(variance_method)), - variance_value = _variance_value>0?_variance_value:0; - int - xm = 0, ym = 0, zm = 0, cm = 0, - xM = 0, yM = 0, zM = 0, cM = 0; - contains(*pm,xm,ym,zm,cm); - contains(*pM,xM,yM,zM,cM); - return CImg(1,12).fill((Tdouble)m,(Tdouble)M,mean_value,variance_value, - (Tdouble)xm,(Tdouble)ym,(Tdouble)zm,(Tdouble)cm, - (Tdouble)xM,(Tdouble)yM,(Tdouble)zM,(Tdouble)cM); - } - - //! Compute statistics vector from the pixel values \inplace. - CImg& stats(const unsigned int variance_method=1) { - return get_stats(variance_method).move_to(*this); - } - - //@} - //------------------------------------- - // - //! \name Vector / Matrix Operations - //@{ - //------------------------------------- - - //! Compute norm of the image, viewed as a matrix. - /** - \param magnitude_type Norm type. Can be: - - \c -1: Linf-norm - - \c 0: L2-norm - - \c 1: L1-norm - **/ - Tdouble magnitude(const int magnitude_type=2) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "magnitude(): Empty instance.", - cimg_instance); - Tdouble res = 0; - switch (magnitude_type) { - case -1 : { - cimg_for(*this,ptrs,T) { const Tdouble val = (Tdouble)cimg::abs(*ptrs); if (val>res) res = val; } - } break; - case 1 : { - cimg_for(*this,ptrs,T) res+=(Tdouble)cimg::abs(*ptrs); - } break; - default : { - cimg_for(*this,ptrs,T) res+=(Tdouble)cimg::sqr(*ptrs); - res = (Tdouble)std::sqrt(res); - } - } - return res; - } - - //! Compute the trace of the image, viewed as a matrix. - /** - **/ - Tdouble trace() const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "trace(): Empty instance.", - cimg_instance); - Tdouble res = 0; - cimg_forX(*this,k) res+=(Tdouble)(*this)(k,k); - return res; - } - - //! Compute the determinant of the image, viewed as a matrix. - /** - **/ - Tdouble det() const { - if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1) - throw CImgInstanceException(_cimg_instance - "det(): Instance is not a square matrix.", - cimg_instance); - - switch (_width) { - case 1 : return (Tdouble)((*this)(0,0)); - case 2 : return (Tdouble)((*this)(0,0))*(Tdouble)((*this)(1,1)) - (Tdouble)((*this)(0,1))*(Tdouble)((*this)(1,0)); - case 3 : { - const Tdouble - a = (Tdouble)_data[0], d = (Tdouble)_data[1], g = (Tdouble)_data[2], - b = (Tdouble)_data[3], e = (Tdouble)_data[4], h = (Tdouble)_data[5], - c = (Tdouble)_data[6], f = (Tdouble)_data[7], i = (Tdouble)_data[8]; - return i*a*e - a*h*f - i*b*d + b*g*f + c*d*h - c*g*e; - } - default : { - CImg lu(*this); - CImg indx; - bool d; - lu._LU(indx,d); - Tdouble res = d?(Tdouble)1:(Tdouble)-1; - cimg_forX(lu,i) res*=lu(i,i); - return res; - } - } - } - - //! Compute the dot product between instance and argument, viewed as matrices. - /** - \param img Image used as a second argument of the dot product. - **/ - template - Tdouble dot(const CImg& img) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "dot(): Empty instance.", - cimg_instance); - if (!img) - throw CImgArgumentException(_cimg_instance - "dot(): Empty specified image.", - cimg_instance); - - const unsigned int nb = cimg::min(size(),img.size()); - Tdouble res = 0; - for (unsigned int off = 0; off get_vector_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const { - CImg res; - if (res._height!=_spectrum) res.assign(1,_spectrum); - const unsigned long whd = (unsigned long)_width*_height*_depth; - const T *ptrs = data(x,y,z); - T *ptrd = res._data; - cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; } - return res; - } - - //! Get (square) matrix-valued pixel located at specified position. - /** - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \note - The spectrum() of the image must be a square. - **/ - CImg get_matrix_at(const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) const { - const int n = (int)std::sqrt((double)_spectrum); - const T *ptrs = data(x,y,z,0); - const unsigned long whd = (unsigned long)_width*_height*_depth; - CImg res(n,n); - T *ptrd = res._data; - cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; } - return res; - } - - //! Get tensor-valued pixel located at specified position. - /** - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - **/ - CImg get_tensor_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const { - const T *ptrs = data(x,y,z,0); - const unsigned long whd = (unsigned long)_width*_height*_depth; - if (_spectrum==6) return tensor(*ptrs,*(ptrs+whd),*(ptrs+2*whd),*(ptrs+3*whd),*(ptrs+4*whd),*(ptrs+5*whd)); - if (_spectrum==3) return tensor(*ptrs,*(ptrs+whd),*(ptrs+2*whd)); - return tensor(*ptrs); - } - - //! Set vector-valued pixel at specified position. - /** - \param vec Vector to put on the instance image. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - **/ - template - CImg& set_vector_at(const CImg& vec, const unsigned int x, const unsigned int y=0, const unsigned int z=0) { - if (x<_width && y<_height && z<_depth) { - const t *ptrs = vec._data; - const unsigned long whd = (unsigned long)_width*_height*_depth; - T *ptrd = data(x,y,z); - for (unsigned int k = cimg::min((unsigned int)vec.size(),_spectrum); k; --k) { - *ptrd = (T)*(ptrs++); ptrd+=whd; - } - } - return *this; - } - - //! Set (square) matrix-valued pixel at specified position. - /** - \param mat Matrix to put on the instance image. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - **/ - template - CImg& set_matrix_at(const CImg& mat, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) { - return set_vector_at(mat,x,y,z); - } - - //! Set tensor-valued pixel at specified position. - /** - \param ten Tensor to put on the instance image. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - **/ - template - CImg& set_tensor_at(const CImg& ten, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) { - T *ptrd = data(x,y,z,0); - const unsigned long siz = (unsigned long)_width*_height*_depth; - if (ten._height==2) { - *ptrd = (T)ten[0]; ptrd+=siz; - *ptrd = (T)ten[1]; ptrd+=siz; - *ptrd = (T)ten[3]; - } - else { - *ptrd = (T)ten[0]; ptrd+=siz; - *ptrd = (T)ten[1]; ptrd+=siz; - *ptrd = (T)ten[2]; ptrd+=siz; - *ptrd = (T)ten[4]; ptrd+=siz; - *ptrd = (T)ten[5]; ptrd+=siz; - *ptrd = (T)ten[8]; - } - return *this; - } - - //! Unroll pixel values along axis \c y. - /** - \note Equivalent to \code unroll('y'); \endcode. - **/ - CImg& vector() { - return unroll('y'); - } - - //! Unroll pixel values along axis \c y \newinstance. - CImg get_vector() const { - return get_unroll('y'); - } - - //! Resize image to become a scalar square matrix. - /** - **/ - CImg& matrix() { - const unsigned long siz = size(); - switch (siz) { - case 1 : break; - case 4 : _width = _height = 2; break; - case 9 : _width = _height = 3; break; - case 16 : _width = _height = 4; break; - case 25 : _width = _height = 5; break; - case 36 : _width = _height = 6; break; - case 49 : _width = _height = 7; break; - case 64 : _width = _height = 8; break; - case 81 : _width = _height = 9; break; - case 100 : _width = _height = 10; break; - default : { - unsigned long i = 11, i2 = i*i; - while (i2 get_matrix() const { - return (+*this).matrix(); - } - - //! Resize image to become a symmetric tensor. - /** - **/ - CImg& tensor() { - return get_tensor().move_to(*this); - } - - //! Resize image to become a symmetric tensor \newinstance. - CImg get_tensor() const { - CImg res; - const unsigned long siz = size(); - switch (siz) { - case 1 : break; - case 3 : - res.assign(2,2); - res(0,0) = (*this)(0); - res(1,0) = res(0,1) = (*this)(1); - res(1,1) = (*this)(2); - break; - case 6 : - res.assign(3,3); - res(0,0) = (*this)(0); - res(1,0) = res(0,1) = (*this)(1); - res(2,0) = res(0,2) = (*this)(2); - res(1,1) = (*this)(3); - res(2,1) = res(1,2) = (*this)(4); - res(2,2) = (*this)(5); - break; - default : - throw CImgInstanceException(_cimg_instance - "tensor(): Invalid instance size (does not define a 1x1, 2x2 or 3x3 tensor).", - cimg_instance); - } - return res; - } - - //! Resize image to become a diagonal matrix. - /** - \note Transform the image as a diagonal matrix so that each of its initial value becomes a diagonal coefficient. - **/ - CImg& diagonal() { - return get_diagonal().move_to(*this); - } - - //! Resize image to become a diagonal matrix \newinstance. - CImg get_diagonal() const { - if (is_empty()) return *this; - CImg res(size(),size(),1,1,0); - cimg_foroff(*this,off) res(off,off) = (*this)(off); - return res; - } - - //! Replace the image by an identity matrix. - /** - \note If the instance image is not square, it is resized to a square matrix using its maximum - dimension as a reference. - **/ - CImg& identity_matrix() { - return identity_matrix(cimg::max(_width,_height)).move_to(*this); - } - - //! Replace the image by an identity matrix \newinstance. - CImg get_identity_matrix() const { - return identity_matrix(cimg::max(_width,_height)); - } - - //! Fill image with a linear sequence of values. - /** - \param a0 Starting value of the sequence. - \param a1 Ending value of the sequence. - **/ - CImg& sequence(const T a0, const T a1) { - if (is_empty()) return *this; - const unsigned int siz = size() - 1; - T* ptr = _data; - if (siz) { - const Tdouble delta = (Tdouble)a1 - (Tdouble)a0; - cimg_foroff(*this,l) *(ptr++) = (T)(a0 + delta*l/siz); - } else *ptr = a0; - return *this; - } - - //! Fill image with a linear sequence of values \newinstance. - CImg get_sequence(const T a0, const T a1) const { - return (+*this).sequence(a0,a1); - } - - //! Transpose the image, viewed as a matrix. - /** - \note Equivalent to \code permute_axes("yxzc"); \endcode - **/ - CImg& transpose() { - if (_width==1) { _width = _height; _height = 1; return *this; } - if (_height==1) { _height = _width; _width = 1; return *this; } - if (_width==_height) { - cimg_forYZC(*this,y,z,c) for (int x = y; x get_transpose() const { - return get_permute_axes("yxzc"); - } - - //! Compute the cross product between two \c 1x3 images, viewed as 3d vectors. - /** - \param img Image used as the second argument of the cross product. - \note The first argument of the cross product is \c *this. - **/ - template - CImg& cross(const CImg& img) { - if (_width!=1 || _height<3 || img._width!=1 || img._height<3) - throw CImgInstanceException(_cimg_instance - "cross(): Instance and/or specified image (%u,%u,%u,%u,%p) are not 3d vectors.", - cimg_instance, - img._width,img._height,img._depth,img._spectrum,img._data); - - const T x = (*this)[0], y = (*this)[1], z = (*this)[2]; - (*this)[0] = (T)(y*img[2] - z*img[1]); - (*this)[1] = (T)(z*img[0] - x*img[2]); - (*this)[2] = (T)(x*img[1] - y*img[0]); - return *this; - } - - //! Compute the cross product between two \c 1x3 images, viewed as 3d vectors \newinstance. - template - CImg<_cimg_Tt> get_cross(const CImg& img) const { - return CImg<_cimg_Tt>(*this).cross(img); - } - - //! Invert the instance image, viewed as a matrix. - /** - \param use_LU Choose the inverting algorithm. Can be: - - \c true: LU-based matrix inversion. - - \c false: SVD-based matrix inversion. - **/ - CImg& invert(const bool use_LU=true) { - if (_width!=_height || _depth!=1 || _spectrum!=1) - throw CImgInstanceException(_cimg_instance - "invert(): Instance is not a square matrix.", - cimg_instance); -#ifdef cimg_use_lapack - int INFO = (int)use_LU, N = _width, LWORK = 4*N, *const IPIV = new int[N]; - Tfloat - *const lapA = new Tfloat[N*N], - *const WORK = new Tfloat[LWORK]; - cimg_forXY(*this,k,l) lapA[k*N+l] = (Tfloat)((*this)(k,l)); - cimg::getrf(N,lapA,IPIV,INFO); - if (INFO) - cimg::warn(_cimg_instance - "invert(): LAPACK function dgetrf_() returned error code %d.", - cimg_instance, - INFO); - else { - cimg::getri(N,lapA,IPIV,WORK,LWORK,INFO); - if (INFO) - cimg::warn(_cimg_instance - "invert(): LAPACK function dgetri_() returned error code %d.", - cimg_instance, - INFO); - } - if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)(lapA[k*N+l]); else fill(0); - delete[] IPIV; delete[] lapA; delete[] WORK; -#else - const double dete = _width>3?-1.0:det(); - if (dete!=0.0 && _width==2) { - const double - a = _data[0], c = _data[1], - b = _data[2], d = _data[3]; - _data[0] = (T)(d/dete); _data[1] = (T)(-c/dete); - _data[2] = (T)(-b/dete); _data[3] = (T)(a/dete); - } else if (dete!=0.0 && _width==3) { - const double - a = _data[0], d = _data[1], g = _data[2], - b = _data[3], e = _data[4], h = _data[5], - c = _data[6], f = _data[7], i = _data[8]; - _data[0] = (T)((i*e-f*h)/dete), _data[1] = (T)((g*f-i*d)/dete), _data[2] = (T)((d*h-g*e)/dete); - _data[3] = (T)((h*c-i*b)/dete), _data[4] = (T)((i*a-c*g)/dete), _data[5] = (T)((g*b-a*h)/dete); - _data[6] = (T)((b*f-e*c)/dete), _data[7] = (T)((d*c-a*f)/dete), _data[8] = (T)((a*e-d*b)/dete); - } else { - if (use_LU) { // LU-based inverse computation - CImg A(*this), indx, col(1,_width); - bool d; - A._LU(indx,d); - cimg_forX(*this,j) { - col.fill(0); - col(j) = 1; - col._solve(A,indx); - cimg_forX(*this,i) (*this)(j,i) = (T)col(i); - } - } else { // SVD-based inverse computation - CImg U(_width,_width), S(1,_width), V(_width,_width); - SVD(U,S,V,false); - U.transpose(); - cimg_forY(S,k) if (S[k]!=0) S[k]=1/S[k]; - S.diagonal(); - *this = V*S*U; - } - } -#endif - return *this; - } - - //! Invert the instance image, viewed as a matrix \newinstance. - CImg get_invert(const bool use_LU=true) const { - return CImg(*this,false).invert(use_LU); - } - - //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix. - /** - **/ - CImg& pseudoinvert() { - return get_pseudoinvert().move_to(*this); - } - - //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix \newinstance. - CImg get_pseudoinvert() const { - CImg U, S, V; - SVD(U,S,V); - const Tfloat tolerance = (sizeof(Tfloat)<=4?5.96e-8f:1.11e-16f)*cimg::max(_width,_height)*S.max(); - cimg_forX(V,x) { - const Tfloat s = S(x), invs = s>tolerance?1/s:(Tfloat)0; - cimg_forY(V,y) V(x,y)*=invs; - } - return V*U.transpose(); - } - - //! Solve a system of linear equations. - /** - \param A Matrix of the linear system. - \note Solve \c AX=B where \c B=*this. - **/ - template - CImg& solve(const CImg& A) { - if (_width!=1 || _depth!=1 || _spectrum!=1 || _height!=A._height || A._depth!=1 || A._spectrum!=1) - throw CImgArgumentException(_cimg_instance - "solve(): Instance and specified matrix (%u,%u,%u,%u,%p) have " - "incompatible dimensions.", - cimg_instance, - A._width,A._height,A._depth,A._spectrum,A._data); - typedef _cimg_Ttfloat Ttfloat; - if (A._width==A._height) { -#ifdef cimg_use_lapack - char TRANS = 'N'; - int INFO, N = _height, LWORK = 4*N, *const IPIV = new int[N]; - Ttfloat - *const lapA = new Ttfloat[N*N], - *const lapB = new Ttfloat[N], - *const WORK = new Ttfloat[LWORK]; - cimg_forXY(A,k,l) lapA[k*N+l] = (Ttfloat)(A(k,l)); - cimg_forY(*this,i) lapB[i] = (Ttfloat)((*this)(i)); - cimg::getrf(N,lapA,IPIV,INFO); - if (INFO) - cimg::warn(_cimg_instance - "solve(): LAPACK library function dgetrf_() returned error code %d.", - cimg_instance, - INFO); - - if (!INFO) { - cimg::getrs(TRANS,N,lapA,IPIV,lapB,INFO); - if (INFO) - cimg::warn(_cimg_instance - "solve(): LAPACK library function dgetrs_() returned error code %d.", - cimg_instance, - INFO); - } - if (!INFO) cimg_forY(*this,i) (*this)(i) = (T)(lapB[i]); else fill(0); - delete[] IPIV; delete[] lapA; delete[] lapB; delete[] WORK; -#else - CImg lu(A,false); - CImg indx; - bool d; - lu._LU(indx,d); - _solve(lu,indx); -#endif - } else { // Least-square solution for non-square systems. -#ifdef cimg_use_lapack - char TRANS = 'N'; - int INFO, N = A._width, M = A._height, LWORK = -1, LDA = M, LDB = M, NRHS = _width; - Ttfloat WORK_QUERY; - Ttfloat - * const lapA = new Ttfloat[M*N], - * const lapB = new Ttfloat[M*NRHS]; - cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, &WORK_QUERY, LWORK, INFO); - LWORK = (int) WORK_QUERY; - Ttfloat *const WORK = new Ttfloat[LWORK]; - cimg_forXY(A,k,l) lapA[k*M+l] = (Ttfloat)(A(k,l)); - cimg_forXY(*this,k,l) lapB[k*M+l] = (Ttfloat)((*this)(k,l)); - cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, WORK, LWORK, INFO); - if (INFO != 0) - cimg::warn(_cimg_instance - "solve(): LAPACK library function sgels() returned error code %d.", - cimg_instance, - INFO); - assign(NRHS, N); - if (!INFO != 0) - cimg_forXY(*this,k,l) (*this)(k,l) = (T)lapB[k*M+l]; - else - assign(A.get_pseudoinvert()*(*this)); - delete[] lapA; delete[] lapB; delete[] WORK; -#else - assign(A.get_pseudoinvert()*(*this)); -#endif - } - return *this; - } - - //! Solve a system of linear equations \newinstance. - template - CImg<_cimg_Ttfloat> get_solve(const CImg& A) const { - return CImg<_cimg_Ttfloat>(*this,false).solve(A); - } - - template - CImg& _solve(const CImg& A, const CImg& indx) { - typedef _cimg_Ttfloat Ttfloat; - const int N = size(); - int ii = -1; - Ttfloat sum; - for (int i = 0; i=0) for (int j = ii; j<=i-1; ++j) sum-=A(j,i)*(*this)(j); - else if (sum!=0) ii = i; - (*this)(i) = (T)sum; - } - for (int i = N - 1; i>=0; --i) { - sum = (*this)(i); - for (int j = i + 1; j - CImg& solve_tridiagonal(const CImg& A) { - const unsigned int siz = (int)size(); - if (A._width!=3 || A._height!=siz) - throw CImgArgumentException(_cimg_instance - "solve_tridiagonal(): Instance and tridiagonal matrix " - "(%u,%u,%u,%u,%p) have incompatible dimensions.", - cimg_instance, - A._width,A._height,A._depth,A._spectrum,A._data); - typedef _cimg_Ttfloat Ttfloat; - const Ttfloat epsilon = 1e-4f; - CImg B = A.get_column(1), V(*this,false); - for (int i = 1; i<(int)siz; ++i) { - const Ttfloat m = A(0,i)/(B[i-1]?B[i-1]:epsilon); - B[i] -= m*A(2,i-1); - V[i] -= m*V[i-1]; - } - (*this)[siz-1] = (T)(V[siz-1]/(B[siz-1]?B[siz-1]:epsilon)); - for (int i = (int)siz - 2; i>=0; --i) (*this)[i] = (T)((V[i] - A(2,i)*(*this)[i+1])/(B[i]?B[i]:epsilon)); - return *this; - } - - //! Solve a tridiagonal system of linear equations \newinstance. - template - CImg<_cimg_Ttfloat> get_solve_tridiagonal(const CImg& A) const { - return CImg<_cimg_Ttfloat>(*this,false).solve_tridiagonal(A); - } - - //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix. - /** - \param[out] val Vector of the estimated eigenvalues, in decreasing order. - \param[out] vec Matrix of the estimated eigenvalues, sorted by columns. - **/ - template - const CImg& eigen(CImg& val, CImg &vec) const { - if (is_empty()) { val.assign(); vec.assign(); } - else { - if (_width!=_height || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "eigen(): Instance is not a square matrix.", - cimg_instance); - - if (val.size()<(unsigned long)_width) val.assign(1,_width); - if (vec.size()<(unsigned long)_width*_width) vec.assign(_width,_width); - switch (_width) { - case 1 : { val[0] = (t)(*this)[0]; vec[0] = (t)1; } break; - case 2 : { - const double a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3], e = a + d; - double f = e*e - 4*(a*d - b*c); - if (f<0) - cimg::warn(_cimg_instance - "eigen(): Complex eigenvalues found.", - cimg_instance); - - f = std::sqrt(f); - const double l1 = 0.5*(e-f), l2 = 0.5*(e+f); - const double theta1 = std::atan2(l2-a,b), theta2 = std::atan2(l1-a,b); - val[0] = (t)l2; - val[1] = (t)l1; - vec(0,0) = (t)std::cos(theta1); - vec(0,1) = (t)std::sin(theta1); - vec(1,0) = (t)std::cos(theta2); - vec(1,1) = (t)std::sin(theta2); - } break; - default : - throw CImgInstanceException(_cimg_instance - "eigen(): Eigenvalues computation of general matrices is limited " - "to 2x2 matrices.", - cimg_instance); - } - } - return *this; - } - - //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix. - /** - \return A list of two images [val; vec], whose meaning is similar as in eigen(CImg&,CImg&) const. - **/ - CImgList get_eigen() const { - CImgList res(2); - eigen(res[0],res[1]); - return res; - } - - //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix. - /** - \param[out] val Vector of the estimated eigenvalues, in decreasing order. - \param[out] vec Matrix of the estimated eigenvalues, sorted by columns. - **/ - template - const CImg& symmetric_eigen(CImg& val, CImg& vec) const { - if (is_empty()) { val.assign(); vec.assign(); } - else { -#ifdef cimg_use_lapack - char JOB = 'V', UPLO = 'U'; - int N = _width, LWORK = 4*N, INFO; - Tfloat - *const lapA = new Tfloat[N*N], - *const lapW = new Tfloat[N], - *const WORK = new Tfloat[LWORK]; - cimg_forXY(*this,k,l) lapA[k*N+l] = (Tfloat)((*this)(k,l)); - cimg::syev(JOB,UPLO,N,lapA,lapW,WORK,LWORK,INFO); - if (INFO) - cimg::warn(_cimg_instance - "symmetric_eigen(): LAPACK library function dsyev_() returned error code %d.", - cimg_instance, - INFO); - - val.assign(1,N); - vec.assign(N,N); - if (!INFO) { - cimg_forY(val,i) val(i) = (T)lapW[N-1-i]; - cimg_forXY(vec,k,l) vec(k,l) = (T)(lapA[(N-1-k)*N+l]); - } else { val.fill(0); vec.fill(0); } - delete[] lapA; delete[] lapW; delete[] WORK; -#else - if (_width!=_height || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "eigen(): Instance is not a square matrix.", - cimg_instance); - - val.assign(1,_width); - if (vec._data) vec.assign(_width,_width); - if (_width<3) { - eigen(val,vec); - if (_width==2) { vec[1] = -vec[2]; vec[3] = vec[0]; } // Force orthogonality for 2x2 matrices. - return *this; - } - CImg V(_width,_width); - SVD(vec,val,V,false); - - bool is_ambiguous = false; - float eig = 0; - cimg_forY(val,p) { // check for ambiguous cases. - if (val[p]>eig) eig = (float)val[p]; - t scal = 0; - cimg_forY(vec,y) scal+=vec(p,y)*V(p,y); - if (cimg::abs(scal)<0.9f) is_ambiguous = true; - if (scal<0) val[p] = -val[p]; - } - if (is_ambiguous) { - ++(eig*=2); - SVD(vec,val,V,false,40,eig); - val-=eig; - } - CImg permutations; // sort eigenvalues in decreasing order - CImg tmp(_width); - val.sort(permutations,false); - cimg_forY(vec,k) { - cimg_forY(permutations,y) tmp(y) = vec(permutations(y),k); - std::memcpy(vec.data(0,k),tmp._data,sizeof(t)*_width); - } -#endif - } - return *this; - } - - //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix. - /** - \return A list of two images [val; vec], whose meaning are similar as in - symmetric_eigen(CImg&,CImg&) const. - **/ - CImgList get_symmetric_eigen() const { - CImgList res(2); - symmetric_eigen(res[0],res[1]); - return res; - } - - //! Sort pixel values and get sorting permutations. - /** - \param[out] permutations Permutation map used for the sorting. - \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way. - **/ - template - CImg& sort(CImg& permutations, const bool is_increasing=true) { - permutations.assign(_width,_height,_depth,_spectrum); - if (is_empty()) return *this; - cimg_foroff(permutations,off) permutations[off] = (t)off; - return _quicksort(0,size()-1,permutations,is_increasing,true); - } - - //! Sort pixel values and get sorting permutations \newinstance. - template - CImg get_sort(CImg& permutations, const bool is_increasing=true) const { - return (+*this).sort(permutations,is_increasing); - } - - //! Sort pixel values. - /** - \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way. - \param axis Tells if the value sorting must be done along a specific axis. Can be: - - \c 0: All pixel values are sorted, independently on their initial position. - - \c 'x': Image columns are sorted, according to the first value in each column. - - \c 'y': Image rows are sorted, according to the first value in each row. - - \c 'z': Image slices are sorted, according to the first value in each slice. - - \c 'c': Image channels are sorted, according to the first value in each channel. - **/ - CImg& sort(const bool is_increasing=true, const char axis=0) { - if (is_empty()) return *this; - CImg perm; - switch (cimg::uncase(axis)) { - case 0 : - _quicksort(0,size()-1,perm,is_increasing,false); - break; - case 'x' : { - perm.assign(_width); - get_crop(0,0,0,0,_width-1,0,0,0).sort(perm,is_increasing); - CImg img(*this,false); - cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(perm[x],y,z,c); - } break; - case 'y' : { - perm.assign(_height); - get_crop(0,0,0,0,0,_height-1,0,0).sort(perm,is_increasing); - CImg img(*this,false); - cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,perm[y],z,c); - } break; - case 'z' : { - perm.assign(_depth); - get_crop(0,0,0,0,0,0,_depth-1,0).sort(perm,is_increasing); - CImg img(*this,false); - cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,perm[z],c); - } break; - case 'c' : { - perm.assign(_spectrum); - get_crop(0,0,0,0,0,0,0,_spectrum-1).sort(perm,is_increasing); - CImg img(*this,false); - cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,z,perm[c]); - } break; - default : - throw CImgArgumentException(_cimg_instance - "sort(): Invalid specified axis '%c' " - "(should be { x | y | z | c }).", - cimg_instance,axis); - } - return *this; - } - - //! Sort pixel values \newinstance. - CImg get_sort(const bool is_increasing=true, const char axis=0) const { - return (+*this).sort(is_increasing,axis); - } - - template - CImg& _quicksort(const int indm, const int indM, CImg& permutations, - const bool is_increasing, const bool is_permutations) { - if (indm(*this)[mid]) { - cimg::swap((*this)[indm],(*this)[mid]); - if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); - } - if ((*this)[mid]>(*this)[indM]) { - cimg::swap((*this)[indM],(*this)[mid]); - if (is_permutations) cimg::swap(permutations[indM],permutations[mid]); - } - if ((*this)[indm]>(*this)[mid]) { - cimg::swap((*this)[indm],(*this)[mid]); - if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); - } - } else { - if ((*this)[indm]<(*this)[mid]) { - cimg::swap((*this)[indm],(*this)[mid]); - if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); - } - if ((*this)[mid]<(*this)[indM]) { - cimg::swap((*this)[indM],(*this)[mid]); - if (is_permutations) cimg::swap(permutations[indM],permutations[mid]); - } - if ((*this)[indm]<(*this)[mid]) { - cimg::swap((*this)[indm],(*this)[mid]); - if (is_permutations) cimg::swap(permutations[indm],permutations[mid]); - } - } - if (indM - indm>=3) { - const T pivot = (*this)[mid]; - int i = indm, j = indM; - if (is_increasing) { - do { - while ((*this)[i]pivot) --j; - if (i<=j) { - if (is_permutations) cimg::swap(permutations[i],permutations[j]); - cimg::swap((*this)[i++],(*this)[j--]); - } - } while (i<=j); - } else { - do { - while ((*this)[i]>pivot) ++i; - while ((*this)[j] A; // Input matrix (assumed to contain some values). - CImg<> U,S,V; - A.SVD(U,S,V) - \endcode - **/ - template - const CImg& SVD(CImg& U, CImg& S, CImg& V, const bool sorting=true, - const unsigned int max_iteration=40, const float lambda=0) const { - if (is_empty()) { U.assign(); S.assign(); V.assign(); } - else { - U = *this; - if (lambda!=0) { - const unsigned int delta = cimg::min(U._width,U._height); - for (unsigned int i = 0; i rv1(_width); - t anorm = 0, c, f, g = 0, h, s, scale = 0; - int l = 0, nm = 0; - - cimg_forX(U,i) { - l = i+1; rv1[i] = scale*g; g = s = scale = 0; - if (i=0?-1:1)*std::sqrt(s)); h=f*g-s; U(i,i) = f-g; - for (int j = l; j=0?-1:1)*std::sqrt(s)); h = f*g-s; U(l,i) = f-g; - for (int k = l; k=0; --i) { - if (i=0; --i) { - l = i+1; g = S[i]; - for (int j = l; j=0; --k) { - for (unsigned int its = 0; its=1; --l) { - nm = l-1; - if ((cimg::abs(rv1[l])+anorm)==anorm) { flag = false; break; } - if ((cimg::abs(S[nm])+anorm)==anorm) break; - } - if (flag) { - c = 0; s = 1; - for (int i = l; i<=k; ++i) { - f = s*rv1[i]; rv1[i] = c*rv1[i]; - if ((cimg::abs(f)+anorm)==anorm) break; - g = S[i]; h = (t)cimg::_pythagore(f,g); S[i] = h; h = 1/h; c = g*h; s = -f*h; - cimg_forY(U,j) { const t y = U(nm,j), z = U(i,j); U(nm,j) = y*c + z*s; U(i,j) = z*c - y*s; } - } - } - - const t z = S[k]; - if (l==k) { if (z<0) { S[k] = -z; cimg_forX(U,j) V(k,j) = -V(k,j); } break; } - nm = k-1; - t x = S[l], y = S[nm]; - g = rv1[nm]; h = rv1[k]; - f = ((y-z)*(y+z)+(g-h)*(g+h))/cimg::max((t)1e-25,2*h*y); - g = (t)cimg::_pythagore(f,1.0); - f = ((x-z)*(x+z)+h*((y/(f + (f>=0?g:-g)))-h))/cimg::max((t)1e-25,x); - c = s = 1; - for (int j = l; j<=nm; ++j) { - const int i = j+1; - g = rv1[i]; h = s*g; g = c*g; - t y = S[i]; - t z = (t)cimg::_pythagore(f,h); - rv1[j] = z; c = f/cimg::max((t)1e-25,z); s = h/cimg::max((t)1e-25,z); - f = x*c+g*s; g = g*c-x*s; h = y*s; y*=c; - cimg_forX(U,jj) { const t x = V(j,jj), z = V(i,jj); V(j,jj) = x*c + z*s; V(i,jj) = z*c - x*s; } - z = (t)cimg::_pythagore(f,h); S[j] = z; - if (z) { z = 1/cimg::max((t)1e-25,z); c = f*z; s = h*z; } - f = c*g+s*y; x = c*y-s*g; - cimg_forY(U,jj) { const t y = U(j,jj); z = U(i,jj); U(j,jj) = y*c + z*s; U(i,jj) = z*c - y*s; } - } - rv1[l] = 0; rv1[k]=f; S[k]=x; - } - } - - if (sorting) { - CImg permutations; - CImg tmp(_width); - S.sort(permutations,false); - cimg_forY(U,k) { - cimg_forY(permutations,y) tmp(y) = U(permutations(y),k); - std::memcpy(U.data(0,k),tmp._data,sizeof(t)*_width); - } - cimg_forY(V,k) { - cimg_forY(permutations,y) tmp(y) = V(permutations(y),k); - std::memcpy(V.data(0,k),tmp._data,sizeof(t)*_width); - } - } - } - return *this; - } - - //! Compute the SVD of the instance image, viewed as a general matrix. - /** - \return A list of three images [U; S; V], whose meaning is similar as in - SVD(CImg&,CImg&,CImg&,bool,unsigned int,float) const. - **/ - CImgList get_SVD(const bool sorting=true, - const unsigned int max_iteration=40, const float lambda=0) const { - CImgList res(3); - SVD(res[0],res[1],res[2],sorting,max_iteration,lambda); - return res; - } - - // [internal] Compute the LU decomposition of a permuted matrix. - template - CImg& _LU(CImg& indx, bool& d) { - const int N = width(); - int imax = 0; - CImg vv(N); - indx.assign(N); - d = true; - cimg_forX(*this,i) { - Tfloat vmax = 0; - cimg_forX(*this,j) { - const Tfloat tmp = cimg::abs((*this)(j,i)); - if (tmp>vmax) vmax = tmp; - } - if (vmax==0) { indx.fill(0); return fill(0); } - vv[i] = 1/vmax; - } - cimg_forX(*this,j) { - for (int i = 0; i=vmax) { vmax=tmp; imax=i; } - } - if (j!=imax) { - cimg_forX(*this,k) cimg::swap((*this)(k,imax),(*this)(k,j)); - d =!d; - vv[imax] = vv[j]; - } - indx[j] = (t)imax; - if ((*this)(j,j)==0) (*this)(j,j) = (T)1e-20; - if (j - static CImg dijkstra(const tf& distance, const unsigned int nb_nodes, - const unsigned int starting_node, const unsigned int ending_node, - CImg& previous_node) { - if (starting_node>=nb_nodes) - throw CImgArgumentException("CImg<%s>::dijkstra(): Specified indice of starting node %u is higher " - "than number of nodes %u.", - pixel_type(),starting_node,nb_nodes); - CImg dist(1,nb_nodes,1,1,cimg::type::max()); - dist(starting_node) = 0; - previous_node.assign(1,nb_nodes,1,1,(t)-1); - previous_node(starting_node) = (t)starting_node; - CImg Q(nb_nodes); - cimg_forX(Q,u) Q(u) = u; - cimg::swap(Q(starting_node),Q(0)); - unsigned int sizeQ = nb_nodes; - while (sizeQ) { - // Update neighbors from minimal vertex - const unsigned int umin = Q(0); - if (umin==ending_node) sizeQ = 0; - else { - const T dmin = dist(umin); - const T infty = cimg::type::max(); - for (unsigned int q = 1; qdist(Q(left))) || - (rightdist(Q(right)));) { - if (right - static CImg dijkstra(const tf& distance, const unsigned int nb_nodes, - const unsigned int starting_node, const unsigned int ending_node=~0U) { - CImg foo; - return dijkstra(distance,nb_nodes,starting_node,ending_node,foo); - } - - //! Return minimal path in a graph, using the Dijkstra algorithm. - /** - \param starting_node Indice of the starting node. - \param ending_node Indice of the ending node. - \param previous_node Array that gives the previous node indice in the path to the starting node - (optional parameter). - \return Array of distances of each node to the starting node. - \note image instance corresponds to the adjacency matrix of the graph. - **/ - template - CImg& dijkstra(const unsigned int starting_node, const unsigned int ending_node, - CImg& previous_node) { - return get_dijkstra(starting_node,ending_node,previous_node).move_to(*this); - } - - //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance. - template - CImg get_dijkstra(const unsigned int starting_node, const unsigned int ending_node, - CImg& previous_node) const { - if (_width!=_height || _depth!=1 || _spectrum!=1) - throw CImgInstanceException(_cimg_instance - "dijkstra(): Instance is not a graph adjacency matrix.", - cimg_instance); - - return dijkstra(*this,_width,starting_node,ending_node,previous_node); - } - - //! Return minimal path in a graph, using the Dijkstra algorithm. - CImg& dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) { - return get_dijkstra(starting_node,ending_node).move_to(*this); - } - - //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance. - CImg get_dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) const { - CImg foo; - return get_dijkstra(starting_node,ending_node,foo); - } - - //! Return an image containing the ascii codes of the specified string. - /** - \param str input C-string to encode as an image. - \param is_last_zero Tells if the ending \c '0' character appear in the resulting image. - **/ - static CImg string(const char *const str, const bool is_last_zero=true, const bool is_shared=false) { - if (!str) return CImg(); - return CImg(str,(unsigned int)std::strlen(str)+(is_last_zero?1:0),1,1,1,is_shared); - } - - //! Return a \c 1x1 image containing specified value. - /** - \param a0 First vector value. - **/ - static CImg vector(const T& a0) { - CImg r(1,1); - r[0] = a0; - return r; - } - - //! Return a \c 1x2 image containing specified values. - /** - \param a0 First vector value. - \param a1 Second vector value. - **/ - static CImg vector(const T& a0, const T& a1) { - CImg r(1,2); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; - return r; - } - - //! Return a \c 1x3 image containing specified values. - /** - \param a0 First vector value. - \param a1 Second vector value. - \param a2 Third vector value. - **/ - static CImg vector(const T& a0, const T& a1, const T& a2) { - CImg r(1,3); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; - return r; - } - - //! Return a \c 1x4 image containing specified values. - /** - \param a0 First vector value. - \param a1 Second vector value. - \param a2 Third vector value. - \param a3 Fourth vector value. - **/ - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3) { - CImg r(1,4); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - return r; - } - - //! Return a \c 1x5 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) { - CImg r(1,5); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4; - return r; - } - - //! Return a \c 1x6 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) { - CImg r(1,6); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4; *(ptr++) = a5; - return r; - } - - //! Return a \c 1x7 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6) { - CImg r(1,7); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; - return r; - } - - //! Return a \c 1x8 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7) { - CImg r(1,8); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - return r; - } - - //! Return a \c 1x9 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8) { - CImg r(1,9); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; - return r; - } - - //! Return a \c 1x10 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9) { - CImg r(1,10); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; - return r; - } - - //! Return a \c 1x11 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10) { - CImg r(1,11); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; - return r; - } - - //! Return a \c 1x12 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10, const T& a11) { - CImg r(1,12); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; - return r; - } - - //! Return a \c 1x13 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10, const T& a11, - const T& a12) { - CImg r(1,13); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; - *(ptr++) = a12; - return r; - } - - //! Return a \c 1x14 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10, const T& a11, - const T& a12, const T& a13) { - CImg r(1,14); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; - *(ptr++) = a12; *(ptr++) = a13; - return r; - } - - //! Return a \c 1x15 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10, const T& a11, - const T& a12, const T& a13, const T& a14) { - CImg r(1,15); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; - *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; - return r; - } - - //! Return a \c 1x16 image containing specified values. - static CImg vector(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10, const T& a11, - const T& a12, const T& a13, const T& a14, const T& a15) { - CImg r(1,16); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; - *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; *(ptr++) = a15; - return r; - } - - //! Return a 1x1 matrix containing specified coefficients. - /** - \param a0 First matrix value. - \note Equivalent to vector(const T&). - **/ - static CImg matrix(const T& a0) { - return vector(a0); - } - - //! Return a 2x2 matrix containing specified coefficients. - /** - \param a0 First matrix value. - \param a1 Second matrix value. - \param a2 Third matrix value. - \param a3 Fourth matrix value. - **/ - static CImg matrix(const T& a0, const T& a1, - const T& a2, const T& a3) { - CImg r(2,2); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; - *(ptr++) = a2; *(ptr++) = a3; - return r; - } - - //! Return a 3x3 matrix containing specified coefficients. - /** - \param a0 First matrix value. - \param a1 Second matrix value. - \param a2 Third matrix value. - \param a3 Fourth matrix value. - \param a4 Fifth matrix value. - \param a5 Sixth matrix value. - \param a6 Seventh matrix value. - \param a7 Eighth matrix value. - \param a8 Nineth matrix value. - **/ - static CImg matrix(const T& a0, const T& a1, const T& a2, - const T& a3, const T& a4, const T& a5, - const T& a6, const T& a7, const T& a8) { - CImg r(3,3); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; - *(ptr++) = a3; *(ptr++) = a4; *(ptr++) = a5; - *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8; - return r; - } - - //! Return a 4x4 matrix containing specified coefficients. - static CImg matrix(const T& a0, const T& a1, const T& a2, const T& a3, - const T& a4, const T& a5, const T& a6, const T& a7, - const T& a8, const T& a9, const T& a10, const T& a11, - const T& a12, const T& a13, const T& a14, const T& a15) { - CImg r(4,4); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; - *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; - *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11; - *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; *(ptr++) = a15; - return r; - } - - //! Return a 5x5 matrix containing specified coefficients. - static CImg matrix(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, - const T& a5, const T& a6, const T& a7, const T& a8, const T& a9, - const T& a10, const T& a11, const T& a12, const T& a13, const T& a14, - const T& a15, const T& a16, const T& a17, const T& a18, const T& a19, - const T& a20, const T& a21, const T& a22, const T& a23, const T& a24) { - CImg r(5,5); T *ptr = r._data; - *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4; - *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8; *(ptr++) = a9; - *(ptr++) = a10; *(ptr++) = a11; *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; - *(ptr++) = a15; *(ptr++) = a16; *(ptr++) = a17; *(ptr++) = a18; *(ptr++) = a19; - *(ptr++) = a20; *(ptr++) = a21; *(ptr++) = a22; *(ptr++) = a23; *(ptr++) = a24; - return r; - } - - //! Return a 1x1 symmetric matrix containing specified coefficients. - /** - \param a0 First matrix value. - \note Equivalent to vector(const T&). - **/ - static CImg tensor(const T& a0) { - return matrix(a0); - } - - //! Return a 2x2 symmetric matrix tensor containing specified coefficients. - static CImg tensor(const T& a0, const T& a1, const T& a2) { - return matrix(a0,a1,a1,a2); - } - - //! Return a 3x3 symmetric matrix containing specified coefficients. - static CImg tensor(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) { - return matrix(a0,a1,a2,a1,a3,a4,a2,a4,a5); - } - - //! Return a 1x1 diagonal matrix containing specified coefficients. - static CImg diagonal(const T& a0) { - return matrix(a0); - } - - //! Return a 2x2 diagonal matrix containing specified coefficients. - static CImg diagonal(const T& a0, const T& a1) { - return matrix(a0,0,0,a1); - } - - //! Return a 3x3 diagonal matrix containing specified coefficients. - static CImg diagonal(const T& a0, const T& a1, const T& a2) { - return matrix(a0,0,0,0,a1,0,0,0,a2); - } - - //! Return a 4x4 diagonal matrix containing specified coefficients. - static CImg diagonal(const T& a0, const T& a1, const T& a2, const T& a3) { - return matrix(a0,0,0,0,0,a1,0,0,0,0,a2,0,0,0,0,a3); - } - - //! Return a 5x5 diagonal matrix containing specified coefficients. - static CImg diagonal(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) { - return matrix(a0,0,0,0,0,0,a1,0,0,0,0,0,a2,0,0,0,0,0,a3,0,0,0,0,0,a4); - } - - //! Return a NxN identity matrix. - /** - \param N Dimension of the matrix. - **/ - static CImg identity_matrix(const unsigned int N) { - CImg res(N,N,1,1,0); - cimg_forX(res,x) res(x,x) = 1; - return res; - } - - //! Return a N-numbered sequence vector from \p a0 to \p a1. - /** - \param N Size of the resulting vector. - \param a0 Starting value of the sequence. - \param a1 Ending value of the sequence. - **/ - static CImg sequence(const unsigned int N, const T a0, const T a1) { - if (N) return CImg(1,N).sequence(a0,a1); - return CImg(); - } - - //! Return a 3x3 rotation matrix along the (x,y,z)-axis with an angle w. - /** - \param x X-coordinate of the rotation axis, or first quaternion coordinate. - \param y Y-coordinate of the rotation axis, or second quaternion coordinate. - \param z Z-coordinate of the rotation axis, or third quaternion coordinate. - \param w Angle of the rotation axis, or fourth quaternion coordinate. - \param is_quaternion Tell is the four arguments denotes a set { axis + angle } or a quaternion. - **/ - static CImg rotation_matrix(const float x, const float y, const float z, const float w, - const bool is_quaternion=false) { - float X,Y,Z,W; - if (!is_quaternion) { - const float norm = (float)std::sqrt(x*x + y*y + z*z), - nx = norm>0?x/norm:0, - ny = norm>0?y/norm:0, - nz = norm>0?z/norm:1, - nw = norm>0?w:0, - sina = (float)std::sin(nw/2), - cosa = (float)std::cos(nw/2); - X = nx*sina; - Y = ny*sina; - Z = nz*sina; - W = cosa; - } else { - const float norm = (float)std::sqrt(x*x + y*y + z*z + w*w); - if (norm>0) { X = x/norm; Y = y/norm; Z = z/norm; W = w/norm; } - else { X = Y = Z = 0; W = 1; } - } - const float xx = X*X, xy = X*Y, xz = X*Z, xw = X*W, yy = Y*Y, yz = Y*Z, yw = Y*W, zz = Z*Z, zw = Z*W; - return CImg::matrix((T)(1-2*(yy+zz)), (T)(2*(xy+zw)), (T)(2*(xz-yw)), - (T)(2*(xy-zw)), (T)(1-2*(xx+zz)), (T)(2*(yz+xw)), - (T)(2*(xz+yw)), (T)(2*(yz-xw)), (T)(1-2*(xx+yy))); - } - - //@} - //----------------------------------- - // - //! \name Value Manipulation - //@{ - //----------------------------------- - - //! Fill all pixel values with specified value. - /** - \param val Fill value. - **/ - CImg& fill(const T val) { - if (is_empty()) return *this; - if (val && sizeof(T)!=1) cimg_for(*this,ptrd,T) *ptrd = val; - else std::memset(_data,(int)val,sizeof(T)*size()); - return *this; - } - - //! Fill all pixel values with specified value \newinstance. - CImg get_fill(const T val) const { - return CImg(_width,_height,_depth,_spectrum).fill(val); - } - - //! Fill sequentially all pixel values with specified values. - /** - \param val0 First fill value. - \param val1 Second fill value. - **/ - CImg& fill(const T val0, const T val1) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-1; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-2; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-3; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-4; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-5; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-6; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, - const T val6) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, - const T val6, const T val7) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-7; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-8; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-9; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-10; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-11; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, - val11); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-12; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, - val11,val12); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, - const T val13) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-13; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, - const T val13) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, - val11,val12,val13); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, - const T val13, const T val14) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-14; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, - const T val13, const T val14) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, - val11,val12,val13,val14); - } - - //! Fill sequentially all pixel values with specified values \overloading. - CImg& fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, - const T val13, const T val14, const T val15) { - if (is_empty()) return *this; - T *ptrd, *ptre = end()-15; - for (ptrd = _data; ptrd get_fill(const T val0, const T val1, const T val2, const T val3, const T val4, const T val5, const T val6, - const T val7, const T val8, const T val9, const T val10, const T val11, const T val12, - const T val13, const T val14, const T val15) const { - return CImg(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10, - val11,val12,val13,val14,val15); - } - - //! Fill sequentially pixel values according to a given expression. - /** - \param expression C-string describing a math formula, or a list of values. - \param repeat_flag In case a list of values is provided, tells if this list must be repeated for the filling. - **/ - CImg& fill(const char *const expression, const bool repeat_flag) { - if (is_empty() || !expression || !*expression) return *this; - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { // Try to fill values according to a formula. - const CImg _base = cimg::_is_self_expr(expression)?+*this:CImg(), &base = _base?_base:*this; - _cimg_math_parser mp(base,expression+(*expression=='>' || *expression=='<'?1:0),"fill"); - T *ptrd = *expression=='<'?end()-1:_data; - if (*expression=='<') cimg_rofXYZC(*this,x,y,z,c) *(ptrd--) = (T)mp(x,y,z,c); - else if (*expression=='>') cimg_forXYZC(*this,x,y,z,c) *(ptrd++) = (T)mp(x,y,z,c); - else { -#ifdef cimg_use_openmp - if (_width>=512 && _height*_depth*_spectrum>=2 && std::strlen(expression)>=6) -#pragma omp parallel - { - _cimg_math_parser _mp = omp_get_thread_num()?mp:_cimg_math_parser(), &lmp = omp_get_thread_num()?_mp:mp; -#pragma omp for collapse(3) - cimg_forYZC(*this,y,z,c) { - T *ptrd = data(0,y,z,c); - cimg_forX(*this,x) *ptrd++ = (T)lmp(x,y,z,c); - } - } - else -#endif - cimg_forXYZC(*this,x,y,z,c) *(ptrd++) = (T)mp(x,y,z,c); - } - } catch (CImgException& e) { // If failed, try to recognize a list of values. - char item[16384] = { 0 }, sep = 0; - const char *nexpression = expression; - unsigned long nb = 0; - const unsigned long siz = size(); - T *ptrd = _data; - for (double val = 0; *nexpression && nb0 && std::sscanf(item,"%lf",&val)==1) { - nexpression+=std::strlen(item) + (err>1?1:0); - *(ptrd++) = (T)val; - } else break; - } - cimg::exception_mode() = omode; - if (nb get_fill(const char *const values, const bool repeat_values) const { - return (+*this).fill(values,repeat_values); - } - - //! Fill sequentially pixel values according to the values found in another image. - /** - \param values Image containing the values used for the filling. - \param repeat_values In case there are less values than necessary in \c values, tells if these values must be - repeated for the filling. - **/ - template - CImg& fill(const CImg& values, const bool repeat_values=true) { - if (is_empty() || !values) return *this; - T *ptrd = _data, *ptre = ptrd + size(); - for (t *ptrs = values._data, *ptrs_end = ptrs + values.size(); ptrs - CImg get_fill(const CImg& values, const bool repeat_values=true) const { - return repeat_values?CImg(_width,_height,_depth,_spectrum).fill(values,repeat_values): - (+*this).fill(values,repeat_values); - } - - //! Fill pixel values along the X-axis at a specified pixel position. - /** - \param y Y-coordinate of the filled column. - \param z Z-coordinate of the filled column. - \param c C-coordinate of the filled column. - \param a0 First fill value. - **/ - CImg& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const int a0, ...) { -#define _cimg_fill1(x,y,z,c,off,siz,t) { \ - va_list ap; va_start(ap,a0); T *ptrd = data(x,y,z,c); *ptrd = (T)a0; \ - for (unsigned long k = 1; k& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const double a0, ...) { - if (y<_height && z<_depth && c<_spectrum) _cimg_fill1(0,y,z,c,1,_width,double); - return *this; - } - - //! Fill pixel values along the Y-axis at a specified pixel position. - /** - \param x X-coordinate of the filled row. - \param z Z-coordinate of the filled row. - \param c C-coordinate of the filled row. - \param a0 First fill value. - **/ - CImg& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const int a0, ...) { - if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,int); - return *this; - } - - //! Fill pixel values along the Y-axis at a specified pixel position \overloading. - CImg& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const double a0, ...) { - if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,double); - return *this; - } - - //! Fill pixel values along the Z-axis at a specified pixel position. - /** - \param x X-coordinate of the filled slice. - \param y Y-coordinate of the filled slice. - \param c C-coordinate of the filled slice. - \param a0 First fill value. - **/ - CImg& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const int a0, ...) { - const unsigned long wh = (unsigned long)_width*_height; - if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,int); - return *this; - } - - //! Fill pixel values along the Z-axis at a specified pixel position \overloading. - CImg& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const double a0, ...) { - const unsigned long wh = (unsigned long)_width*_height; - if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,double); - return *this; - } - - //! Fill pixel values along the C-axis at a specified pixel position. - /** - \param x X-coordinate of the filled channel. - \param y Y-coordinate of the filled channel. - \param z Z-coordinate of the filled channel. - \param a0 First filling value. - **/ - CImg& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const int a0, ...) { - const unsigned long whd = (unsigned long)_width*_height*_depth; - if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,int); - return *this; - } - - //! Fill pixel values along the C-axis at a specified pixel position \overloading. - CImg& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const double a0, ...) { - const unsigned long whd = (unsigned long)_width*_height*_depth; - if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,double); - return *this; - } - - //! Discard specified value in the image buffer. - /** - \param value Value to discard. - \note Discarded values will change the image geometry, so the resulting image - is returned as a one-column vector. - **/ - CImg& discard(const T value) { - return get_discard(value).move_to(*this); - } - - //! Discard specified value in the image buffer \newinstance. - CImg get_discard(const T value) const { - CImg res(1,size()); - T *pd = res._data; - for (const T *ps = _data, *const pse = end(); ps(); - return res.resize(1,pd-res._data,1,1,-1); - } - - //! Discard specified sequence of values in the image buffer. - /** - \param values Sequence of values to discard. - \note Discarded values will change the image geometry, so the resulting image - is returned as a one-column vector. - **/ - template - CImg& discard(const CImg& values) { - return get_discard(values).move_to(*this); - } - - //! Discard specified sequence of values in the image buffer \newinstance. - template - CImg get_discard(const CImg& values) const { - if (!values) return *this; - if (values.size()==1) return get_discard(*values); - CImg res(1,size()); - T *pd = res._data; - const t *const pve = values.end(); - for (const T *ps = _data, *const pse = end(); ps(); - return res.resize(1,pd-res._data,1,1,-1); - } - - //! Invert endianness of all pixel values. - /** - **/ - CImg& invert_endianness() { - cimg::invert_endianness(_data,size()); - return *this; - } - - //! Invert endianness of all pixel values \newinstance. - CImg get_invert_endianness() const { - return (+*this).invert_endianness(); - } - - //! Fill image with random values in specified range. - /** - \param val_min Minimal random value. - \param val_max Maximal random value. - \note Random samples are following a uniform distribution. - **/ - CImg& rand(const T val_min, const T val_max) { - const float delta = (float)val_max - (float)val_min; - cimg_for(*this,ptrd,T) *ptrd = (T)(val_min + cimg::rand()*delta); - return *this; - } - - //! Fill image with random values in specified range \newinstance. - CImg get_rand(const T val_min, const T val_max) const { - return (+*this).rand(val_min,val_max); - } - - //! Round pixel values. - /** - \param y Rounding precision. - \param rounding_type Rounding type. Can be: - - \c -1: Backward. - - \c 0: Nearest. - - \c 1: Forward. - **/ - CImg& round(const double y=1, const int rounding_type=0) { - if (y>0) -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=8192) -#endif - cimg_rof(*this,ptrd,T) *ptrd = cimg::round(*ptrd,y,rounding_type); - return *this; - } - - //! Round pixel values \newinstance. - CImg get_round(const double y=1, const unsigned int rounding_type=0) const { - return (+*this).round(y,rounding_type); - } - - //! Add random noise to pixel values. - /** - \param sigma Amplitude of the random additive noise. If \p sigma<0, it stands for a percentage of the - global value range. - \param noise_type Type of additive noise (can be \p 0=gaussian, \p 1=uniform, \p 2=Salt and Pepper, - \p 3=Poisson or \p 4=Rician). - \return A reference to the modified image instance. - \note - - For Poisson noise (\p noise_type=3), parameter \p sigma is ignored, as Poisson noise only depends on - the image value itself. - - Function \p CImg::get_noise() is also defined. It returns a non-shared modified copy of the image instance. - \par Example - \code - const CImg img("reference.jpg"), res = img.get_noise(40); - (img,res.normalize(0,255)).display(); - \endcode - \image html ref_noise.jpg - **/ - CImg& noise(const double sigma, const unsigned int noise_type=0) { - if (!is_empty()) { - const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); - Tfloat nsigma = (Tfloat)sigma, m = 0, M = 0; - if (nsigma==0 && noise_type!=3) return *this; - if (nsigma<0 || noise_type==2) m = (Tfloat)min_max(M); - if (nsigma<0) nsigma = (Tfloat)(-nsigma*(M-m)/100.0); - switch (noise_type) { - case 0 : { // Gaussian noise - cimg_rof(*this,ptrd,T) { - Tfloat val = (Tfloat)(*ptrd + nsigma*cimg::grand()); - if (val>vmax) val = vmax; - if (valvmax) val = vmax; - if (val::is_float()?1:cimg::type::max()); } - cimg_rof(*this,ptrd,T) if (cimg::rand()*100vmax) val = vmax; - if (val get_noise(const double sigma, const unsigned int noise_type=0) const { - return (+*this).noise(sigma,noise_type); - } - - //! Linearly normalize pixel values. - /** - \param min_value Minimum desired value of the resulting image. - \param max_value Maximum desired value of the resulting image. - \par Example - \code - const CImg img("reference.jpg"), res = img.get_normalize(160,220); - (img,res).display(); - \endcode - \image html ref_normalize2.jpg - **/ - CImg& normalize(const T min_value, const T max_value) { - if (is_empty()) return *this; - const T a = min_value=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (T)((*ptrd-fm)/(fM-fm)*(b-a)+a); - return *this; - } - - //! Linearly normalize pixel values \newinstance. - CImg get_normalize(const T min_value, const T max_value) const { - return CImg(*this,false).normalize((Tfloat)min_value,(Tfloat)max_value); - } - - //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm. - /** - \par Example - \code - const CImg img("reference.jpg"), res = img.get_normalize(); - (img,res.normalize(0,255)).display(); - \endcode - \image html ref_normalize.jpg - **/ - CImg& normalize() { - const unsigned long whd = (unsigned long)_width*_height*_depth; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) -#endif - cimg_forYZ(*this,y,z) { - T *ptrd = data(0,y,z,0); - cimg_forX(*this,x) { - const T *ptrs = ptrd; - float n = 0; - cimg_forC(*this,c) { n+=cimg::sqr((float)*ptrs); ptrs+=whd; } - n = (float)std::sqrt(n); - T *_ptrd = ptrd++; - if (n>0) cimg_forC(*this,c) { *_ptrd = (T)(*_ptrd/n); _ptrd+=whd; } - else cimg_forC(*this,c) { *_ptrd = (T)0; _ptrd+=whd; } - } - } - return *this; - } - - //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm \newinstance. - CImg get_normalize() const { - return CImg(*this,false).normalize(); - } - - //! Compute L2-norm of each multi-valued pixel of the image instance. - /** - \param norm_type Type of computed vector norm (can be \p 0=Linf, \p 1=L1 or \p 2=L2). - \par Example - \code - const CImg img("reference.jpg"), res = img.get_norm(); - (img,res.normalize(0,255)).display(); - \endcode - \image html ref_norm.jpg - **/ - CImg& norm(const int norm_type=2) { - if (_spectrum==1) return abs(); - return get_norm(norm_type).move_to(*this); - } - - //! Compute L2-norm of each multi-valued pixel of the image instance \newinstance. - CImg get_norm(const int norm_type=2) const { - if (is_empty()) return *this; - if (_spectrum==1) return get_abs(); - const unsigned long whd = (unsigned long)_width*_height*_depth; - CImg res(_width,_height,_depth); - switch (norm_type) { - case -1 : { // Linf norm -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) -#endif - cimg_forYZ(*this,y,z) { - const unsigned long off = offset(0,y,z); - const T *ptrs = _data + off; - Tfloat *ptrd = res._data + off; - cimg_forX(*this,x) { - Tfloat n = 0; - const T *_ptrs = ptrs++; - cimg_forC(*this,c) { const Tfloat val = (Tfloat)cimg::abs(*_ptrs); if (val>n) n = val; _ptrs+=whd; } - *(ptrd++) = n; - } - } - } break; - case 1 : { // L1 norm -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) -#endif - cimg_forYZ(*this,y,z) { - const unsigned long off = offset(0,y,z); - const T *ptrs = _data + off; - Tfloat *ptrd = res._data + off; - cimg_forX(*this,x) { - Tfloat n = 0; - const T *_ptrs = ptrs++; - cimg_forC(*this,c) { n+=cimg::abs(*_ptrs); _ptrs+=whd; } - *(ptrd++) = n; - } - } - } break; - default : { // L2 norm -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) -#endif - cimg_forYZ(*this,y,z) { - const unsigned long off = offset(0,y,z); - const T *ptrs = _data + off; - Tfloat *ptrd = res._data + off; - cimg_forX(*this,x) { - Tfloat n = 0; - const T *_ptrs = ptrs++; - cimg_forC(*this,c) { n+=cimg::sqr((Tfloat)*_ptrs); _ptrs+=whd; } - *(ptrd++) = (Tfloat)std::sqrt((Tfloat)n); - } - } - } - } - return res; - } - - //! Cut pixel values in specified range. - /** - \param min_value Minimum desired value of the resulting image. - \param max_value Maximum desired value of the resulting image. - \par Example - \code - const CImg img("reference.jpg"), res = img.get_cut(160,220); - (img,res).display(); - \endcode - \image html ref_cut.jpg - **/ - CImg& cut(const T min_value, const T max_value) { - if (is_empty()) return *this; - const T a = min_value=32768) -#endif - cimg_rof(*this,ptrd,T) *ptrd = (*ptrdb)?b:*ptrd); - return *this; - } - - //! Cut pixel values in specified range \newinstance. - CImg get_cut(const T min_value, const T max_value) const { - return (+*this).cut(min_value,max_value); - } - - //! Uniformly quantize pixel values. - /** - \param nb_levels Number of quantization levels. - \param keep_range Tells if resulting values keep the same range as the original ones. - \par Example - \code - const CImg img("reference.jpg"), res = img.get_quantize(4); - (img,res).display(); - \endcode - \image html ref_quantize.jpg - **/ - CImg& quantize(const unsigned int nb_levels, const bool keep_range=true) { - if (!nb_levels) - throw CImgArgumentException(_cimg_instance - "quantize(): Invalid quantization request with 0 values.", - cimg_instance); - - if (is_empty()) return *this; - Tfloat m, M = (Tfloat)max_min(m), range = M - m; - if (range>0) { - if (keep_range) -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { - const unsigned int val = (unsigned int)((*ptrd-m)*nb_levels/range); - *ptrd = (T)(m + cimg::min(val,nb_levels-1)*range/nb_levels); - } else -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { - const unsigned int val = (unsigned int)((*ptrd-m)*nb_levels/range); - *ptrd = (T)cimg::min(val,nb_levels-1); - } - } - return *this; - } - - //! Uniformly quantize pixel values \newinstance. - CImg get_quantize(const unsigned int n, const bool keep_range=true) const { - return (+*this).quantize(n,keep_range); - } - - //! Threshold pixel values. - /** - \param value Threshold value - \param soft_threshold Tells if soft thresholding must be applied (instead of hard one). - \param strict_threshold Tells if threshold value is strict. - \par Example - \code - const CImg img("reference.jpg"), res = img.get_threshold(128); - (img,res.normalize(0,255)).display(); - \endcode - \image html ref_threshold.jpg - **/ - CImg& threshold(const T value, const bool soft_threshold=false, const bool strict_threshold=false) { - if (is_empty()) return *this; - if (strict_threshold) { - if (soft_threshold) -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { const T v = *ptrd; *ptrd = v>value?(T)(v-value):v<-(float)value?(T)(v+value):(T)0; } - else -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = *ptrd>value?(T)1:(T)0; - } else { - if (soft_threshold) -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=32768) -#endif - cimg_rof(*this,ptrd,T) { - const T v = *ptrd; *ptrd = v>=value?(T)(v-value):v<=-(float)value?(T)(v+value):(T)0; - } - else -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=65536) -#endif - cimg_rof(*this,ptrd,T) *ptrd = *ptrd>=value?(T)1:(T)0; - } - return *this; - } - - //! Threshold pixel values \newinstance. - CImg get_threshold(const T value, const bool soft_threshold=false, const bool strict_threshold=false) const { - return (+*this).threshold(value,soft_threshold,strict_threshold); - } - - //! Compute the histogram of pixel values. - /** - \param nb_levels Number of desired histogram levels. - \param min_value Minimum pixel value considered for the histogram computation. - All pixel values lower than \p min_value will not be counted. - \param max_value Maximum pixel value considered for the histogram computation. - All pixel values higher than \p max_value will not be counted. - \note - - The histogram H of an image I is the 1d function where H(x) counts the number of occurences of the value x - in the image I. - - The resulting histogram is always defined in 1d. Histograms of multi-valued images are not multi-dimensional. - \par Example - \code - const CImg img = CImg("reference.jpg").histogram(256); - img.display_graph(0,3); - \endcode - \image html ref_histogram.jpg - **/ - CImg& histogram(const unsigned int nb_levels, const T min_value, const T max_value) { - return get_histogram(nb_levels,min_value,max_value).move_to(*this); - } - - //! Compute the histogram of pixel values \overloading. - CImg& histogram(const unsigned int nb_levels) { - return get_histogram(nb_levels).move_to(*this); - } - - //! Compute the histogram of pixel values \newinstance. - CImg get_histogram(const unsigned int nb_levels, const T min_value, const T max_value) const { - if (!nb_levels || is_empty()) return CImg(); - T vmin = min_value res(nb_levels,1,1,1,0); - cimg_rof(*this,ptrs,T) { - const T val = *ptrs; - if (val>=vmin && val<=vmax) ++res[val==vmax?nb_levels-1:(unsigned int)((val-vmin)*nb_levels/(vmax-vmin))]; - } - return res; - } - - //! Compute the histogram of pixel values \newinstance. - CImg get_histogram(const unsigned int nb_levels) const { - if (!nb_levels || is_empty()) return CImg(); - T vmax = 0, vmin = min_max(vmax); - return get_histogram(nb_levels,vmin,vmax); - } - - //! Equalize histogram of pixel values. - /** - \param nb_levels Number of histogram levels used for the equalization. - \param min_value Minimum pixel value considered for the histogram computation. - All pixel values lower than \p min_value will not be counted. - \param max_value Maximum pixel value considered for the histogram computation. - All pixel values higher than \p max_value will not be counted. - \par Example - \code - const CImg img("reference.jpg"), res = img.get_equalize(256); - (img,res).display(); - \endcode - \image html ref_equalize.jpg - **/ - CImg& equalize(const unsigned int nb_levels, const T min_value, const T max_value) { - if (!nb_levels || is_empty()) return *this; - T vmin = min_value hist = get_histogram(nb_levels,vmin,vmax); - unsigned long cumul = 0; - cimg_forX(hist,pos) { cumul+=hist[pos]; hist[pos] = cumul; } - if (!cumul) cumul = 1; -#ifdef cimg_use_openmp -#pragma omp parallel for if (size()>=1048576) -#endif - cimg_rof(*this,ptrd,T) { - const int pos = (int)((*ptrd-vmin)*(nb_levels-1)/(vmax-vmin)); - if (pos>=0 && pos<(int)nb_levels) *ptrd = (T)(vmin + (vmax-vmin)*hist[pos]/cumul); - } - return *this; - } - - //! Equalize histogram of pixel values \overloading. - CImg& equalize(const unsigned int nb_levels) { - if (!nb_levels || is_empty()) return *this; - T vmax = 0, vmin = min_max(vmax); - return equalize(nb_levels,vmin,vmax); - } - - //! Equalize histogram of pixel values \newinstance. - CImg get_equalize(const unsigned int nblevels, const T val_min, const T val_max) const { - return (+*this).equalize(nblevels,val_min,val_max); - } - - //! Equalize histogram of pixel values \newinstance. - CImg get_equalize(const unsigned int nblevels) const { - return (+*this).equalize(nblevels); - } - - //! Index multi-valued pixels regarding to a specified colormap. - /** - \param colormap Multi-valued colormap used as the basis for multi-valued pixel indexing. - \param dithering Level of dithering (0=disable, 1=standard level). - \param map_indexes Tell if the values of the resulting image are the colormap indices or the colormap vectors. - \note - - \p img.index(colormap,dithering,1) is equivalent to img.index(colormap,dithering,0).map(colormap). - \par Example - \code - const CImg img("reference.jpg"), colormap(3,1,1,3, 0,128,255, 0,128,255, 0,128,255); - const CImg res = img.get_index(colormap,1,true); - (img,res).display(); - \endcode - \image html ref_index.jpg - **/ - template - CImg& index(const CImg& colormap, const float dithering=1, const bool map_indexes=false) { - return get_index(colormap,dithering,map_indexes).move_to(*this); - } - - //! Index multi-valued pixels regarding to a specified colormap \newinstance. - template - CImg::Tuint> - get_index(const CImg& colormap, const float dithering=1, const bool map_indexes=true) const { - if (colormap._spectrum!=_spectrum) - throw CImgArgumentException(_cimg_instance - "index(): Instance and specified colormap (%u,%u,%u,%u,%p) " - "have incompatible dimensions.", - cimg_instance, - colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data); - - typedef typename CImg::Tuint tuint; - if (is_empty()) return CImg(); - const unsigned long - whd = (unsigned long)_width*_height*_depth, - pwhd = (unsigned long)colormap._width*colormap._height*colormap._depth; - CImg res(_width,_height,_depth,map_indexes?_spectrum:1); - tuint *ptrd = res._data; - if (dithering>0) { // Dithered versions. - const float ndithering = (dithering<0?0:dithering>1?1:dithering)/16; - Tfloat valm = 0, valM = (Tfloat)max_min(valm); - if (valm==valM && valm>=0 && valM<=255) { valm = 0; valM = 255; } - CImg cache = get_crop(-1,0,0,0,_width,1,0,_spectrum-1); - Tfloat *cache_current = cache.data(1,0,0,0), *cache_next = cache.data(1,1,0,0); - const unsigned long cwhd = (unsigned long)cache._width*cache._height*cache._depth; - switch (_spectrum) { - case 1 : { // Optimized for scalars. - cimg_forYZ(*this,y,z) { - if (yvalM?valM:_val0; - Tfloat distmin = cimg::type::max(); const t *ptrmin0 = colormap._data; - for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0valM?valM:_val0, - _val1 = (Tfloat)*ptrs1, val1 = _val1valM?valM:_val1; - Tfloat distmin = cimg::type::max(); const t *ptrmin0 = colormap._data; - for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0valM?valM:_val0, - _val1 = (Tfloat)*ptrs1, val1 = _val1valM?valM:_val1, - _val2 = (Tfloat)*ptrs2, val2 = _val2valM?valM:_val2; - Tfloat distmin = cimg::type::max(); const t *ptrmin0 = colormap._data; - for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd, - *ptrp_end = ptrp1; ptrp0::max(); const t *ptrmin = colormap._data; - for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrpvalM?valM:_val; - dist+=cimg::sqr((*_ptrs=val) - (Tfloat)*_ptrp); _ptrs+=cwhd; _ptrp+=pwhd; - } - if (dist=64 && _height*_depth>=16 && pwhd>=16) -#endif - cimg_forYZ(*this,y,z) { - tuint *ptrd = res.data(0,y,z); - for (const T *ptrs0 = data(0,y,z), *ptrs_end = ptrs0 + _width; ptrs0::max(); const t *ptrmin0 = colormap._data; - for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0=64 && _height*_depth>=16 && pwhd>=16) -#endif - cimg_forYZ(*this,y,z) { - tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd; - for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs_end = ptrs0 + _width; ptrs0::max(); const t *ptrmin0 = colormap._data; - for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0=64 && _height*_depth>=16 && pwhd>=16) -#endif - cimg_forYZ(*this,y,z) { - tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd, *ptrd2 = ptrd1 + whd; - for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs2 = ptrs1 + whd, - *ptrs_end = ptrs0 + _width; ptrs0::max(); const t *ptrmin0 = colormap._data; - for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd, - *ptrp_end = ptrp1; ptrp0=64 && _height*_depth>=16 && pwhd>=16) -#endif - cimg_forYZ(*this,y,z) { - tuint *ptrd = res.data(0,y,z); - for (const T *ptrs = data(0,y,z), *ptrs_end = ptrs + _width; ptrs::max(); const t *ptrmin = colormap._data; - for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrp img("reference.jpg"), - colormap1(3,1,1,3, 0,128,255, 0,128,255, 0,128,255), - colormap2(3,1,1,3, 255,0,0, 0,255,0, 0,0,255), - res = img.get_index(colormap1,0).map(colormap2); - (img,res).display(); - \endcode - \image html ref_map.jpg - **/ - template - CImg& map(const CImg& colormap, const unsigned int boundary_conditions=0) { - return get_map(colormap,boundary_conditions).move_to(*this); - } - - //! Map predefined colormap on the scalar (indexed) image instance \newinstance. - template - CImg get_map(const CImg& colormap, const unsigned int boundary_conditions=0) const { - if (_spectrum!=1 && colormap._spectrum!=1) - throw CImgArgumentException(_cimg_instance - "map(): Instance and specified colormap (%u,%u,%u,%u,%p) " - "have incompatible dimensions.", - cimg_instance, - colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data); - - const unsigned long - whd = (unsigned long)_width*_height*_depth, - pwhd = (unsigned long)colormap._width*colormap._height*colormap._depth; - CImg res(_width,_height,_depth,colormap._spectrum==1?_spectrum:colormap._spectrum); - switch (colormap._spectrum) { - - case 1 : { // Optimized for scalars. - const T *ptrs = _data; - switch (boundary_conditions) { - case 2 : // Periodic boundaries. - cimg_for(res,ptrd,t) { - const unsigned long ind = (unsigned long)*(ptrs++); - *ptrd = colormap[ind%pwhd]; - } break; - case 1 : // Neumann boundaries. - cimg_for(res,ptrd,t) { - const long ind = (long)*(ptrs++); - *ptrd = colormap[ind<0?0:ind>=(long)pwhd?pwhd-1:ind]; - } break; - default : // Dirichlet boundaries. - cimg_for(res,ptrd,t) { - const unsigned long ind = (unsigned long)*(ptrs++); - *ptrd = ind=(long)pwhd?pwhd-1:_ind; - *(ptrd0++) = ptrp0[ind]; *(ptrd1++) = ptrp1[ind]; - } - } break; - default : { // Dirichlet boundaries. - const t *const ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd; - t *ptrd0 = res._data, *ptrd1 = ptrd0 + whd; - for (const T *ptrs = _data, *ptrs_end = ptrs + whd; ptrs=(long)pwhd?pwhd-1:_ind; - *(ptrd0++) = ptrp0[ind]; *(ptrd1++) = ptrp1[ind]; *(ptrd2++) = ptrp2[ind]; - } - } break; - default : { // Dirichlet boundaries. - const t *const ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd; - t *ptrd0 = res._data, *ptrd1 = ptrd0 + whd, *ptrd2 = ptrd1 + whd; - for (const T *ptrs = _data, *ptrs_end = ptrs + whd; ptrs=(long)pwhd?pwhd-1:_ind; - const t *ptrp = colormap._data + ind; - t *_ptrd = ptrd++; cimg_forC(res,c) { *_ptrd = *ptrp; _ptrd+=whd; ptrp+=pwhd; } - } - } break; - default : { // Dirichlet boundaries. - t *ptrd = res._data; - for (const T *ptrs = _data, *ptrs_end = ptrs + whd; ptrs& label(const bool is_high_connectivity=false, const Tfloat tolerance=0) { - return get_label(is_high_connectivity,tolerance).move_to(*this); - } - - //! Label connected components \newinstance. - CImg get_label(const bool is_high_connectivity=false, - const Tfloat tolerance=0) const { - if (is_empty()) return CImg(); - - // Create neighborhood tables. - int dx[13], dy[13], dz[13], nb = 0; - dx[nb]=1; dy[nb] = 0; dz[nb++]=0; - dx[nb]=0; dy[nb] = 1; dz[nb++]=0; - if (is_high_connectivity) { - dx[nb]=1; dy[nb] = 1; dz[nb++]=0; - dx[nb]=1; dy[nb] = -1; dz[nb++]=0; - } - if (_depth>1) { // 3d version. - dx[nb]=0; dy[nb] = 0; dz[nb++]=1; - if (is_high_connectivity) { - dx[nb]=1; dy[nb] = 1; dz[nb++]=-1; - dx[nb]=1; dy[nb] = 0; dz[nb++]=-1; - dx[nb]=1; dy[nb] = -1; dz[nb++]=-1; - dx[nb]=0; dy[nb] = 1; dz[nb++]=-1; - - dx[nb]=0; dy[nb] = 1; dz[nb++]=1; - dx[nb]=1; dy[nb] = -1; dz[nb++]=1; - dx[nb]=1; dy[nb] = 0; dz[nb++]=1; - dx[nb]=1; dy[nb] = 1; dz[nb++]=1; - } - } - return _get_label(nb,dx,dy,dz,tolerance); - } - - //! Label connected components \overloading. - /** - \param connectivity_mask Mask of the neighboring pixels. - \param tolerance Tolerance used to determine if two neighboring pixels belong to the same region. - **/ - template - CImg& label(const CImg& connectivity_mask, const Tfloat tolerance=0) { - return get_label(connectivity_mask,tolerance).move_to(*this); - } - - //! Label connected components \newinstance. - template - CImg get_label(const CImg& connectivity_mask, - const Tfloat tolerance=0) const { - int nb = 0; - cimg_for(connectivity_mask,ptr,t) if (*ptr) ++nb; - CImg dx(nb,1,1,1,0), dy(nb,1,1,1,0), dz(nb,1,1,1,0); - nb = 0; - cimg_forXYZ(connectivity_mask,x,y,z) if ((x || y || z) && - connectivity_mask(x,y,z)) { - dx[nb] = x; dy[nb] = y; dz[nb++] = z; - } - return _get_label(nb,dx,dy,dz,tolerance); - } - - CImg _get_label(const unsigned int nb, const int - *const dx, const int *const dy, const int *const dz, - const Tfloat tolerance) const { - CImg res(_width,_height,_depth,_spectrum); - cimg_forC(*this,c) { - CImg _res = res.get_shared_channel(c); - - // Init label numbers. - unsigned long *ptr = _res.data(); - cimg_foroff(_res,p) *(ptr++) = p; - - // For each neighbour-direction, label. - for (unsigned int n = 0; n& _system_strescape() { -#define cimg_system_strescape(c,s) case c : if (p!=ptrs) CImg(ptrs,(unsigned int)(p-ptrs),1,1,1,false).\ - move_to(list); \ - CImg(s,(unsigned int)std::strlen(s),1,1,1,false).move_to(list); ptrs = p+1; break - CImgList list; - const T *ptrs = _data; - cimg_for(*this,p,T) switch ((int)*p) { - cimg_system_strescape('\\',"\\\\"); - cimg_system_strescape('\"',"\\\""); - cimg_system_strescape('!',"\"\\!\""); - cimg_system_strescape('`',"\\`"); - cimg_system_strescape('$',"\\$"); - } - if (ptrs(ptrs,(unsigned int)(end()-ptrs),1,1,1,false).move_to(list); - return (list>'x').move_to(*this); - } - - //@} - //--------------------------------- - // - //! \name Color Base Management - //@{ - //--------------------------------- - - //! Return colormap \e "default", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_default.jpg - **/ - static const CImg& default_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) { - colormap.assign(1,256,1,3); - for (unsigned int index = 0, r = 16; r<256; r+=32) - for (unsigned int g = 16; g<256; g+=32) - for (unsigned int b = 32; b<256; b+=64) { - colormap(0,index,0) = (Tuchar)r; - colormap(0,index,1) = (Tuchar)g; - colormap(0,index++,2) = (Tuchar)b; - } - } - cimg::mutex(8,0); - return colormap; - } - - //! Return colormap \e "HSV", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_hsv.jpg - **/ - static const CImg& HSV_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) { - CImg tmp(1,256,1,3,1); - tmp.get_shared_channel(0).sequence(0,359); - colormap = tmp.HSVtoRGB(); - } - cimg::mutex(8,0); - return colormap; - } - - //! Return colormap \e "lines", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_lines.jpg - **/ - static const CImg& lines_LUT256() { - static const unsigned char pal[] = { - 217,62,88,75,1,237,240,12,56,160,165,116,1,1,204,2,15,248,148,185,133,141,46,246,222,116,16,5,207,226, - 17,114,247,1,214,53,238,0,95,55,233,235,109,0,17,54,33,0,90,30,3,0,94,27,19,0,68,212,166,130,0,15,7,119, - 238,2,246,198,0,3,16,10,13,2,25,28,12,6,2,99,18,141,30,4,3,140,12,4,30,233,7,10,0,136,35,160,168,184,20, - 233,0,1,242,83,90,56,180,44,41,0,6,19,207,5,31,214,4,35,153,180,75,21,76,16,202,218,22,17,2,136,71,74, - 81,251,244,148,222,17,0,234,24,0,200,16,239,15,225,102,230,186,58,230,110,12,0,7,129,249,22,241,37,219, - 1,3,254,210,3,212,113,131,197,162,123,252,90,96,209,60,0,17,0,180,249,12,112,165,43,27,229,77,40,195,12, - 87,1,210,148,47,80,5,9,1,137,2,40,57,205,244,40,8,252,98,0,40,43,206,31,187,0,180,1,69,70,227,131,108,0, - 223,94,228,35,248,243,4,16,0,34,24,2,9,35,73,91,12,199,51,1,249,12,103,131,20,224,2,70,32, - 233,1,165,3,8,154,246,233,196,5,0,6,183,227,247,195,208,36,0,0,226,160,210,198,69,153,210,1,23,8,192,2,4, - 137,1,0,52,2,249,241,129,0,0,234,7,238,71,7,32,15,157,157,252,158,2,250,6,13,30,11,162,0,199,21,11,27,224, - 4,157,20,181,111,187,218,3,0,11,158,230,196,34,223,22,248,135,254,210,157,219,0,117,239,3,255,4,227,5,247, - 11,4,3,188,111,11,105,195,2,0,14,1,21,219,192,0,183,191,113,241,1,12,17,248,0,48,7,19,1,254,212,0,239,246, - 0,23,0,250,165,194,194,17,3,253,0,24,6,0,141,167,221,24,212,2,235,243,0,0,205,1,251,133,204,28,4,6,1,10, - 141,21,74,12,236,254,228,19,1,0,214,1,186,13,13,6,13,16,27,209,6,216,11,207,251,59,32,9,155,23,19,235,143, - 116,6,213,6,75,159,23,6,0,228,4,10,245,249,1,7,44,234,4,102,174,0,19,239,103,16,15,18,8,214,22,4,47,244, - 255,8,0,251,173,1,212,252,250,251,252,6,0,29,29,222,233,246,5,149,0,182,180,13,151,0,203,183,0,35,149,0, - 235,246,254,78,9,17,203,73,11,195,0,3,5,44,0,0,237,5,106,6,130,16,214,20,168,247,168,4,207,11,5,1,232,251, - 129,210,116,231,217,223,214,27,45,38,4,177,186,249,7,215,172,16,214,27,249,230,236,2,34,216,217,0,175,30, - 243,225,244,182,20,212,2,226,21,255,20,0,2,13,62,13,191,14,76,64,20,121,4,118,0,216,1,147,0,2,210,1,215, - 95,210,236,225,184,46,0,248,24,11,1,9,141,250,243,9,221,233,160,11,147,2,55,8,23,12,253,9,0,54,0,231,6,3, - 141,8,2,246,9,180,5,11,8,227,8,43,110,242,1,130,5,97,36,10,6,219,86,133,11,108,6,1,5,244,67,19,28,0,174, - 154,16,127,149,252,188,196,196,228,244,9,249,0,0,0,37,170,32,250,0,73,255,23,3,224,234,38,195,198,0,255,87, - 33,221,174,31,3,0,189,228,6,153,14,144,14,108,197,0,9,206,245,254,3,16,253,178,248,0,95,125,8,0,3,168,21, - 23,168,19,50,240,244,185,0,1,144,10,168,31,82,1,13 }; - static const CImg colormap(pal,1,256,1,3,false); - return colormap; - } - - //! Return colormap \e "hot", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_hot.jpg - **/ - static const CImg& hot_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) { - colormap.assign(1,4,1,3,0); - colormap[1] = colormap[2] = colormap[3] = colormap[6] = colormap[7] = colormap[11] = 255; - colormap.resize(1,256,1,3,3); - } - cimg::mutex(8,0); - return colormap; - } - - //! Return colormap \e "cool", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_cool.jpg - **/ - static const CImg& cool_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) colormap.assign(1,2,1,3).fill(0,255,255,0,255,255).resize(1,256,1,3,3); - cimg::mutex(8,0); - return colormap; - } - - //! Return colormap \e "jet", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_jet.jpg - **/ - static const CImg& jet_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) { - colormap.assign(1,4,1,3,0); - colormap[2] = colormap[3] = colormap[5] = colormap[6] = colormap[8] = colormap[9] = 255; - colormap.resize(1,256,1,3,3); - } - cimg::mutex(8,0); - return colormap; - } - - //! Return colormap \e "flag", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_flag.jpg - **/ - static const CImg& flag_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) { - colormap.assign(1,4,1,3,0); - colormap[0] = colormap[1] = colormap[5] = colormap[9] = colormap[10] = 255; - colormap.resize(1,256,1,3,0,2); - } - cimg::mutex(8,0); - return colormap; - } - - //! Return colormap \e "cube", containing 256 colors entries in RGB. - /** - \return The following \c 256x1x1x3 colormap is returned: - \image html ref_colormap_cube.jpg - **/ - static const CImg& cube_LUT256() { - static CImg colormap; - cimg::mutex(8); - if (!colormap) { - colormap.assign(1,8,1,3,0); - colormap[1] = colormap[3] = colormap[5] = colormap[7] = - colormap[10] = colormap[11] = colormap[12] = colormap[13] = - colormap[20] = colormap[21] = colormap[22] = colormap[23] = 255; - colormap.resize(1,256,1,3,3); - } - cimg::mutex(8,0); - return colormap; - } - - //! Convert pixel values from sRGB to RGB color spaces. - CImg& sRGBtoRGB() { - cimg_for(*this,ptr,T) { - const Tfloat - sval = (Tfloat)*ptr, - nsval = (sval<0?0:sval>255?255:sval)/255, - val = (Tfloat)(nsval<=0.04045f?nsval/12.92f:std::pow((nsval+0.055f)/(1.055f),2.4f)); - *ptr = (T)(val*255); - } - return *this; - } - - //! Convert pixel values from sRGB to RGB color spaces \newinstance. - CImg get_sRGBtoRGB() const { - return CImg(*this,false).sRGBtoRGB(); - } - - //! Convert pixel values from RGB to sRGB color spaces. - CImg& RGBtosRGB() { - cimg_for(*this,ptr,T) { - const Tfloat - val = (Tfloat)*ptr, - nval = (val<0?0:val>255?255:val)/255, - sval = (Tfloat)(nval<=0.0031308f?nval*12.92f:1.055f*std::pow(nval,0.416667f)-0.055f); - *ptr = (T)(sval*255); - } - return *this; - } - - //! Convert pixel values from RGB to sRGB color spaces \newinstance. - CImg get_RGBtosRGB() const { - return CImg(*this,false).RGBtosRGB(); - } - - //! Convert pixel values from RGB to HSV color spaces. - CImg& RGBtoHSV() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoHSV(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1, - G = (Tfloat)*p2, - B = (Tfloat)*p3, - nR = (R<0?0:(R>255?255:R))/255, - nG = (G<0?0:(G>255?255:G))/255, - nB = (B<0?0:(B>255?255:B))/255, - m = cimg::min(nR,nG,nB), - M = cimg::max(nR,nG,nB); - Tfloat H = 0, S = 0; - if (M!=m) { - const Tfloat - f = (nR==m)?(nG-nB):((nG==m)?(nB-nR):(nR-nG)), - i = (Tfloat)((nR==m)?3:((nG==m)?5:1)); - H = (i-f/(M-m)); - if (H>=6) H-=6; - H*=60; - S = (M-m)/M; - } - *(p1++) = (T)H; - *(p2++) = (T)S; - *(p3++) = (T)M; - } - return *this; - } - - //! Convert pixel values from RGB to HSV color spaces \newinstance. - CImg get_RGBtoHSV() const { - return CImg(*this,false).RGBtoHSV(); - } - - //! Convert pixel values from HSV to RGB color spaces. - CImg& HSVtoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "HSVtoRGB(): Instance is not a HSV image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - Tfloat - H = (Tfloat)*p1, - S = (Tfloat)*p2, - V = (Tfloat)*p3, - R = 0, G = 0, B = 0; - if (H==0 && S==0) R = G = B = V; - else { - H/=60; - const int i = (int)std::floor(H); - const Tfloat - f = (i&1)?(H - i):(1 - H + i), - m = V*(1 - S), - n = V*(1 - S*f); - switch (i) { - case 6 : - case 0 : R = V; G = n; B = m; break; - case 1 : R = n; G = V; B = m; break; - case 2 : R = m; G = V; B = n; break; - case 3 : R = m; G = n; B = V; break; - case 4 : R = n; G = m; B = V; break; - case 5 : R = V; G = m; B = n; break; - } - } - R*=255; G*=255; B*=255; - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from HSV to RGB color spaces \newinstance. - CImg get_HSVtoRGB() const { - return CImg(*this,false).HSVtoRGB(); - } - - //! Convert pixel values from RGB to HSL color spaces. - CImg& RGBtoHSL() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoHSL(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1, - G = (Tfloat)*p2, - B = (Tfloat)*p3, - nR = (R<0?0:(R>255?255:R))/255, - nG = (G<0?0:(G>255?255:G))/255, - nB = (B<0?0:(B>255?255:B))/255, - m = cimg::min(nR,nG,nB), - M = cimg::max(nR,nG,nB), - L = (m + M)/2; - Tfloat H = 0, S = 0; - if (M==m) H = S = 0; - else { - const Tfloat - f = (nR==m)?(nG-nB):((nG==m)?(nB-nR):(nR-nG)), - i = (nR==m)?3.0f:((nG==m)?5.0f:1.0f); - H = (i-f/(M-m)); - if (H>=6) H-=6; - H*=60; - S = (2*L<=1)?((M-m)/(M+m)):((M-m)/(2-M-m)); - } - *(p1++) = (T)H; - *(p2++) = (T)S; - *(p3++) = (T)L; - } - return *this; - } - - //! Convert pixel values from RGB to HSL color spaces \newinstance. - CImg get_RGBtoHSL() const { - return CImg< Tfloat>(*this,false).RGBtoHSL(); - } - - //! Convert pixel values from HSL to RGB color spaces. - CImg& HSLtoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "HSLtoRGB(): Instance is not a HSL image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - H = (Tfloat)*p1, - S = (Tfloat)*p2, - L = (Tfloat)*p3, - q = 2*L<1?L*(1+S):(L+S-L*S), - p = 2*L-q, - h = H/360, - tr = h + 1.0f/3, - tg = h, - tb = h - 1.0f/3, - ntr = tr<0?tr+1:(tr>1?tr-1:tr), - ntg = tg<0?tg+1:(tg>1?tg-1:tg), - ntb = tb<0?tb+1:(tb>1?tb-1:tb), - R = 255*(6*ntr<1?p+(q-p)*6*ntr:(2*ntr<1?q:(3*ntr<2?p+(q-p)*6*(2.0f/3-ntr):p))), - G = 255*(6*ntg<1?p+(q-p)*6*ntg:(2*ntg<1?q:(3*ntg<2?p+(q-p)*6*(2.0f/3-ntg):p))), - B = 255*(6*ntb<1?p+(q-p)*6*ntb:(2*ntb<1?q:(3*ntb<2?p+(q-p)*6*(2.0f/3-ntb):p))); - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from HSL to RGB color spaces \newinstance. - CImg get_HSLtoRGB() const { - return CImg(*this,false).HSLtoRGB(); - } - - //! Convert pixel values from RGB to HSI color spaces. - CImg& RGBtoHSI() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoHSI(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1, - G = (Tfloat)*p2, - B = (Tfloat)*p3, - nR = (R<0?0:(R>255?255:R))/255, - nG = (G<0?0:(G>255?255:G))/255, - nB = (B<0?0:(B>255?255:B))/255, - m = cimg::min(nR,nG,nB), - theta = (Tfloat)(std::acos(0.5f*((nR-nG)+(nR-nB))/std::sqrt(std::pow(nR-nG,2)+(nR-nB)*(nG-nB)))*180/cimg::PI), - sum = nR + nG + nB; - Tfloat H = 0, S = 0, I = 0; - if (theta>0) H = (nB<=nG)?theta:360-theta; - if (sum>0) S = 1 - 3/sum*m; - I = sum/3; - *(p1++) = (T)H; - *(p2++) = (T)S; - *(p3++) = (T)I; - } - return *this; - } - - //! Convert pixel values from RGB to HSI color spaces \newinstance. - CImg get_RGBtoHSI() const { - return CImg(*this,false).RGBtoHSI(); - } - - //! Convert pixel values from HSI to RGB color spaces. - CImg& HSItoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "HSItoRGB(): Instance is not a HSI image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - Tfloat - H = (Tfloat)*p1, - S = (Tfloat)*p2, - I = (Tfloat)*p3, - a = I*(1-S), - R = 0, G = 0, B = 0; - if (H<120) { - B = a; - R = (Tfloat)(I*(1+S*std::cos(H*cimg::PI/180)/std::cos((60-H)*cimg::PI/180))); - G = 3*I-(R+B); - } else if (H<240) { - H-=120; - R = a; - G = (Tfloat)(I*(1+S*std::cos(H*cimg::PI/180)/std::cos((60-H)*cimg::PI/180))); - B = 3*I-(R+G); - } else { - H-=240; - G = a; - B = (Tfloat)(I*(1+S*std::cos(H*cimg::PI/180)/std::cos((60-H)*cimg::PI/180))); - R = 3*I-(G+B); - } - R*=255; G*=255; B*=255; - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from HSI to RGB color spaces \newinstance. - CImg get_HSItoRGB() const { - return CImg< Tuchar>(*this,false).HSItoRGB(); - } - - //! Convert pixel values from RGB to YCbCr color spaces. - CImg& RGBtoYCbCr() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoYCbCr(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1, - G = (Tfloat)*p2, - B = (Tfloat)*p3, - Y = (66*R + 129*G + 25*B + 128)/256 + 16, - Cb = (-38*R - 74*G + 112*B + 128)/256 + 128, - Cr = (112*R - 94*G - 18*B + 128)/256 + 128; - *(p1++) = (T)(Y<0?0:(Y>255?255:Y)); - *(p2++) = (T)(Cb<0?0:(Cb>255?255:Cb)); - *(p3++) = (T)(Cr<0?0:(Cr>255?255:Cr)); - } - return *this; - } - - //! Convert pixel values from RGB to YCbCr color spaces \newinstance. - CImg get_RGBtoYCbCr() const { - return CImg(*this,false).RGBtoYCbCr(); - } - - //! Convert pixel values from RGB to YCbCr color spaces. - CImg& YCbCrtoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "YCbCrtoRGB(): Instance is not a YCbCr image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - Y = (Tfloat)*p1 - 16, - Cb = (Tfloat)*p2 - 128, - Cr = (Tfloat)*p3 - 128, - R = (298*Y + 409*Cr + 128)/256, - G = (298*Y - 100*Cb - 208*Cr + 128)/256, - B = (298*Y + 516*Cb + 128)/256; - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from RGB to YCbCr color spaces \newinstance. - CImg get_YCbCrtoRGB() const { - return CImg(*this,false).YCbCrtoRGB(); - } - - //! Convert pixel values from RGB to YUV color spaces. - CImg& RGBtoYUV() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoYUV(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1/255, - G = (Tfloat)*p2/255, - B = (Tfloat)*p3/255, - Y = 0.299f*R + 0.587f*G + 0.114f*B; - *(p1++) = (T)Y; - *(p2++) = (T)(0.492f*(B-Y)); - *(p3++) = (T)(0.877*(R-Y)); - } - return *this; - } - - //! Convert pixel values from RGB to YUV color spaces \newinstance. - CImg get_RGBtoYUV() const { - return CImg(*this,false).RGBtoYUV(); - } - - //! Convert pixel values from YUV to RGB color spaces. - CImg& YUVtoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "YUVtoRGB(): Instance is not a YUV image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - Y = (Tfloat)*p1, - U = (Tfloat)*p2, - V = (Tfloat)*p3, - R = (Y + 1.140f*V)*255, - G = (Y - 0.395f*U - 0.581f*V)*255, - B = (Y + 2.032f*U)*255; - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from YUV to RGB color spaces \newinstance. - CImg get_YUVtoRGB() const { - return CImg< Tuchar>(*this,false).YUVtoRGB(); - } - - //! Convert pixel values from RGB to CMY color spaces. - CImg& RGBtoCMY() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoCMY(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1, - G = (Tfloat)*p2, - B = (Tfloat)*p3, - C = 255 - R, - M = 255 - G, - Y = 255 - B; - *(p1++) = (T)(C<0?0:(C>255?255:C)); - *(p2++) = (T)(M<0?0:(M>255?255:M)); - *(p3++) = (T)(Y<0?0:(Y>255?255:Y)); - } - return *this; - } - - //! Convert pixel values from RGB to CMY color spaces \newinstance. - CImg get_RGBtoCMY() const { - return CImg(*this,false).RGBtoCMY(); - } - - //! Convert pixel values from CMY to RGB color spaces. - CImg& CMYtoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "CMYtoRGB(): Instance is not a CMY image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - C = (Tfloat)*p1, - M = (Tfloat)*p2, - Y = (Tfloat)*p3, - R = 255 - C, - G = 255 - M, - B = 255 - Y; - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from CMY to RGB color spaces \newinstance. - CImg get_CMYtoRGB() const { - return CImg(*this,false).CMYtoRGB(); - } - - //! Convert pixel values from CMY to CMYK color spaces. - CImg& CMYtoCMYK() { - return get_CMYtoCMYK().move_to(*this); - } - - //! Convert pixel values from CMY to CMYK color spaces \newinstance. - CImg get_CMYtoCMYK() const { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "CMYtoCMYK(): Instance is not a CMY image.", - cimg_instance); - - CImg res(_width,_height,_depth,4); - const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2); - Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2), *pd4 = res.data(0,0,0,3); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - Tfloat - C = (Tfloat)*(ps1++), - M = (Tfloat)*(ps2++), - Y = (Tfloat)*(ps3++), - K = cimg::min(C,M,Y); - if (K>=255) C = M = Y = 0; - else { const Tfloat K1 = 255 - K; C = 255*(C - K)/K1; M = 255*(M - K)/K1; Y = 255*(Y - K)/K1; } - *(pd1++) = (Tfloat)(C<0?0:(C>255?255:C)); - *(pd2++) = (Tfloat)(M<0?0:(M>255?255:M)); - *(pd3++) = (Tfloat)(Y<0?0:(Y>255?255:Y)); - *(pd4++) = (Tfloat)(K<0?0:(K>255?255:K)); - } - return res; - } - - //! Convert pixel values from CMYK to CMY color spaces. - CImg& CMYKtoCMY() { - return get_CMYKtoCMY().move_to(*this); - } - - //! Convert pixel values from CMYK to CMY color spaces \newinstance. - CImg get_CMYKtoCMY() const { - if (_spectrum!=4) - throw CImgInstanceException(_cimg_instance - "CMYKtoCMY(): Instance is not a CMYK image.", - cimg_instance); - - CImg res(_width,_height,_depth,3); - const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2), *ps4 = data(0,0,0,3); - Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - C = (Tfloat)*(ps1++), - M = (Tfloat)*(ps2++), - Y = (Tfloat)*(ps3++), - K = (Tfloat)*(ps4++), - K1 = 1 - K/255, - nC = C*K1 + K, - nM = M*K1 + K, - nY = Y*K1 + K; - *(pd1++) = (Tfloat)(nC<0?0:(nC>255?255:nC)); - *(pd2++) = (Tfloat)(nM<0?0:(nM>255?255:nM)); - *(pd3++) = (Tfloat)(nY<0?0:(nY>255?255:nY)); - } - return res; - } - - //! Convert pixel values from RGB to XYZ_709 color spaces. - /** - \note Uses the standard D65 white point. - **/ - CImg& RGBtoXYZ() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoXYZ(): Instance is not a RGB image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - R = (Tfloat)*p1/255, - G = (Tfloat)*p2/255, - B = (Tfloat)*p3/255; - *(p1++) = (T)(0.412453f*R + 0.357580f*G + 0.180423f*B); - *(p2++) = (T)(0.212671f*R + 0.715160f*G + 0.072169f*B); - *(p3++) = (T)(0.019334f*R + 0.119193f*G + 0.950227f*B); - } - return *this; - } - - //! Convert pixel values from RGB to XYZ_709 color spaces \newinstance. - CImg get_RGBtoXYZ() const { - return CImg(*this,false).RGBtoXYZ(); - } - - //! Convert pixel values from XYZ_709 to RGB color spaces. - CImg& XYZtoRGB() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "XYZtoRGB(): Instance is not a XYZ image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - X = (Tfloat)*p1*255, - Y = (Tfloat)*p2*255, - Z = (Tfloat)*p3*255, - R = 3.240479f*X - 1.537150f*Y - 0.498535f*Z, - G = -0.969256f*X + 1.875992f*Y + 0.041556f*Z, - B = 0.055648f*X - 0.204043f*Y + 1.057311f*Z; - *(p1++) = (T)(R<0?0:(R>255?255:R)); - *(p2++) = (T)(G<0?0:(G>255?255:G)); - *(p3++) = (T)(B<0?0:(B>255?255:B)); - } - return *this; - } - - //! Convert pixel values from XYZ_709 to RGB color spaces \newinstance. - CImg get_XYZtoRGB() const { - return CImg(*this,false).XYZtoRGB(); - } - - //! Convert pixel values from XYZ_709 to Lab color spaces. - CImg& XYZtoLab() { -#define _cimg_Labf(x) ((x)>=0.008856f?(std::pow(x,(Tfloat)1/3)):(7.787f*(x)+16.0f/116)) - - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "XYZtoLab(): Instance is not a XYZ image.", - cimg_instance); - - const Tfloat - Xn = (Tfloat)(0.412453f + 0.357580f + 0.180423f), - Yn = (Tfloat)(0.212671f + 0.715160f + 0.072169f), - Zn = (Tfloat)(0.019334f + 0.119193f + 0.950227f); - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - X = (Tfloat)*p1, - Y = (Tfloat)*p2, - Z = (Tfloat)*p3, - XXn = X/Xn, YYn = Y/Yn, ZZn = Z/Zn, - fX = (Tfloat)_cimg_Labf(XXn), - fY = (Tfloat)_cimg_Labf(YYn), - fZ = (Tfloat)_cimg_Labf(ZZn); - *(p1++) = (T)cimg::max(0.0f,116*fY - 16); - *(p2++) = (T)(500*(fX - fY)); - *(p3++) = (T)(200*(fY - fZ)); - } - return *this; - } - - //! Convert pixel values from XYZ_709 to Lab color spaces \newinstance. - CImg get_XYZtoLab() const { - return CImg(*this,false).XYZtoLab(); - } - - //! Convert pixel values from Lab to XYZ_709 color spaces. - CImg& LabtoXYZ() { -#define _cimg_Labfi(x) ((x)>=0.206893f?((x)*(x)*(x)):(((x)-16.0f/116)/7.787f)) - - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "LabtoXYZ(): Instance is not a Lab image.", - cimg_instance); - - const Tfloat - Xn = (Tfloat)(0.412453f + 0.357580f + 0.180423f), - Yn = (Tfloat)(0.212671f + 0.715160f + 0.072169f), - Zn = (Tfloat)(0.019334f + 0.119193f + 0.950227f); - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - L = (Tfloat)*p1, - a = (Tfloat)*p2, - b = (Tfloat)*p3, - cY = (L + 16)/116, - Y = (Tfloat)(Yn*_cimg_Labfi(cY)), - pY = (Tfloat)std::pow(Y/Yn,(Tfloat)1/3), - cX = a/500 + pY, - X = Xn*cX*cX*cX, - cZ = pY - b/200, - Z = Zn*cZ*cZ*cZ; - *(p1++) = (T)(X); - *(p2++) = (T)(Y); - *(p3++) = (T)(Z); - } - return *this; - } - - //! Convert pixel values from Lab to XYZ_709 color spaces \newinstance. - CImg get_LabtoXYZ() const { - return CImg(*this,false).LabtoXYZ(); - } - - //! Convert pixel values from XYZ_709 to xyY color spaces. - CImg& XYZtoxyY() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "XYZtoxyY(): Instance is not a XYZ image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - X = (Tfloat)*p1, - Y = (Tfloat)*p2, - Z = (Tfloat)*p3, - sum = (X+Y+Z), - nsum = sum>0?sum:1; - *(p1++) = (T)(X/nsum); - *(p2++) = (T)(Y/nsum); - *(p3++) = (T)Y; - } - return *this; - } - - //! Convert pixel values from XYZ_709 to xyY color spaces \newinstance. - CImg get_XYZtoxyY() const { - return CImg(*this,false).XYZtoxyY(); - } - - //! Convert pixel values from xyY pixels to XYZ_709 color spaces. - CImg& xyYtoXYZ() { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "xyYtoXYZ(): Instance is not a xyY image.", - cimg_instance); - - T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2); - for (unsigned long N = (unsigned long)_width*_height*_depth; N; --N) { - const Tfloat - px = (Tfloat)*p1, - py = (Tfloat)*p2, - Y = (Tfloat)*p3, - ny = py>0?py:1; - *(p1++) = (T)(px*Y/ny); - *(p2++) = (T)Y; - *(p3++) = (T)((1-px-py)*Y/ny); - } - return *this; - } - - //! Convert pixel values from xyY pixels to XYZ_709 color spaces \newinstance. - CImg get_xyYtoXYZ() const { - return CImg(*this,false).xyYtoXYZ(); - } - - //! Convert pixel values from RGB to Lab color spaces. - CImg& RGBtoLab() { - return RGBtoXYZ().XYZtoLab(); - } - - //! Convert pixel values from RGB to Lab color spaces \newinstance. - CImg get_RGBtoLab() const { - return CImg(*this,false).RGBtoLab(); - } - - //! Convert pixel values from Lab to RGB color spaces. - CImg& LabtoRGB() { - return LabtoXYZ().XYZtoRGB(); - } - - //! Convert pixel values from Lab to RGB color spaces \newinstance. - CImg get_LabtoRGB() const { - return CImg(*this,false).LabtoRGB(); - } - - //! Convert pixel values from RGB to xyY color spaces. - CImg& RGBtoxyY() { - return RGBtoXYZ().XYZtoxyY(); - } - - //! Convert pixel values from RGB to xyY color spaces \newinstance. - CImg get_RGBtoxyY() const { - return CImg(*this,false).RGBtoxyY(); - } - - //! Convert pixel values from xyY to RGB color spaces. - CImg& xyYtoRGB() { - return xyYtoXYZ().XYZtoRGB(); - } - - //! Convert pixel values from xyY to RGB color spaces \newinstance. - CImg get_xyYtoRGB() const { - return CImg(*this,false).xyYtoRGB(); - } - - //! Convert pixel values from RGB to CMYK color spaces. - CImg& RGBtoCMYK() { - return RGBtoCMY().CMYtoCMYK(); - } - - //! Convert pixel values from RGB to CMYK color spaces \newinstance. - CImg get_RGBtoCMYK() const { - return CImg(*this,false).RGBtoCMYK(); - } - - //! Convert pixel values from CMYK to RGB color spaces. - CImg& CMYKtoRGB() { - return CMYKtoCMY().CMYtoRGB(); - } - - //! Convert pixel values from CMYK to RGB color spaces \newinstance. - CImg get_CMYKtoRGB() const { - return CImg(*this,false).CMYKtoRGB(); - } - - //! Convert RGB color image to a Bayer-coded scalar image. - /** - \note First (upper-left) pixel if the red component of the pixel color. - **/ - CImg& RGBtoBayer() { - return get_RGBtoBayer().move_to(*this); - } - - //! Convert RGB color image to a Bayer-coded scalar image \newinstance. - CImg get_RGBtoBayer() const { - if (_spectrum!=3) - throw CImgInstanceException(_cimg_instance - "RGBtoBayer(): Instance is not a RGB image.", - cimg_instance); - - CImg res(_width,_height,_depth,1); - const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); - T *ptrd = res._data; - cimg_forXYZ(*this,x,y,z) { - if (y%2) { - if (x%2) *(ptrd++) = *ptr_b; - else *(ptrd++) = *ptr_g; - } else { - if (x%2) *(ptrd++) = *ptr_g; - else *(ptrd++) = *ptr_r; - } - ++ptr_r; ++ptr_g; ++ptr_b; - } - return res; - } - - //! Convert Bayer-coded scalar image to a RGB color image. - CImg& BayertoRGB(const unsigned int interpolation_type=3) { - return get_BayertoRGB(interpolation_type).move_to(*this); - } - - //! Convert Bayer-coded scalar image to a RGB color image \newinstance. - CImg get_BayertoRGB(const unsigned int interpolation_type=3) const { - if (_spectrum!=1) - throw CImgInstanceException(_cimg_instance - "BayertoRGB(): Instance is not a Bayer image.", - cimg_instance); - - CImg res(_width,_height,_depth,3); - CImg_3x3(I,T); - Tuchar *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2); - switch (interpolation_type) { - case 3 : { // Edge-directed - CImg_3x3(R,T); - CImg_3x3(G,T); - CImg_3x3(B,T); - cimg_forXYZ(*this,x,y,z) { - const int _p1x = x?x-1:1, _p1y = y?y-1:1, _n1x = x=2) return 0; - const float a = (float)cimg::PI*x, b = 0.5f*a; - return (float)(x?std::sin(a)*std::sin(b)/(a*b):1); - } - - //! Resize image to new dimensions. - /** - \param size_x Number of columns (new size along the X-axis). - \param size_y Number of rows (new size along the Y-axis). - \param size_z Number of slices (new size along the Z-axis). - \param size_c Number of vector-channels (new size along the C-axis). - \param interpolation_type Method of interpolation: - - -1 = no interpolation: raw memory resizing. - - 0 = no interpolation: additional space is filled according to \p boundary_conditions. - - 1 = nearest-neighbor interpolation. - - 2 = moving average interpolation. - - 3 = linear interpolation. - - 4 = grid interpolation. - - 5 = cubic interpolation. - - 6 = lanczos interpolation. - \param boundary_conditions Border condition type. - \param centering_x Set centering type (only if \p interpolation_type=0). - \param centering_y Set centering type (only if \p interpolation_type=0). - \param centering_z Set centering type (only if \p interpolation_type=0). - \param centering_c Set centering type (only if \p interpolation_type=0). - \note If pd[x,y,z,v]<0, it corresponds to a percentage of the original size (the default value is -100). - **/ - CImg& resize(const int size_x, const int size_y=-100, - const int size_z=-100, const int size_c=-100, - const int interpolation_type=1, const unsigned int boundary_conditions=0, - const float centering_x = 0, const float centering_y = 0, - const float centering_z = 0, const float centering_c = 0) { - if (!size_x || !size_y || !size_z || !size_c) return assign(); - const unsigned int - _sx = (unsigned int)(size_x<0?-size_x*width()/100:size_x), - _sy = (unsigned int)(size_y<0?-size_y*height()/100:size_y), - _sz = (unsigned int)(size_z<0?-size_z*depth()/100:size_z), - _sc = (unsigned int)(size_c<0?-size_c*spectrum()/100:size_c), - sx = _sx?_sx:1, sy = _sy?_sy:1, sz = _sz?_sz:1, sc = _sc?_sc:1; - if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return *this; - if (is_empty()) return assign(sx,sy,sz,sc,(T)0); - if (interpolation_type==-1 && sx*sy*sz*sc==size()) { - _width = sx; _height = sy; _depth = sz; _spectrum = sc; - return *this; - } - return get_resize(sx,sy,sz,sc,interpolation_type,boundary_conditions, - centering_x,centering_y,centering_z,centering_c).move_to(*this); - } - - //! Resize image to new dimensions \newinstance. - CImg get_resize(const int size_x, const int size_y = -100, - const int size_z = -100, const int size_c = -100, - const int interpolation_type=1, const unsigned int boundary_conditions=0, - const float centering_x = 0, const float centering_y = 0, - const float centering_z = 0, const float centering_c = 0) const { - if (centering_x<0 || centering_x>1 || centering_y<0 || centering_y>1 || - centering_z<0 || centering_z>1 || centering_c<0 || centering_c>1) - throw CImgArgumentException(_cimg_instance - "resize(): Specified centering arguments (%g,%g,%g,%g) are outside range [0,1].", - cimg_instance, - centering_x,centering_y,centering_z,centering_c); - - if (!size_x || !size_y || !size_z || !size_c) return CImg(); - const unsigned int - _sx = (unsigned int)(size_x<0?-size_x*width()/100:size_x), - _sy = (unsigned int)(size_y<0?-size_y*height()/100:size_y), - _sz = (unsigned int)(size_z<0?-size_z*depth()/100:size_z), - _sc = (unsigned int)(size_c<0?-size_c*spectrum()/100:size_c), - sx = _sx?_sx:1, sy = _sy?_sy:1, sz = _sz?_sz:1, sc = _sc?_sc:1; - if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return +*this; - if (is_empty()) return CImg(sx,sy,sz,sc,0); - - CImg res; - switch (interpolation_type) { - - // Raw resizing. - // - case -1 : - std::memcpy(res.assign(sx,sy,sz,sc,0)._data,_data,sizeof(T)*cimg::min(size(),sx*sy*sz*sc)); - break; - - // No interpolation. - // - case 0 : { - const int - xc = (int)(centering_x*((int)sx - width())), - yc = (int)(centering_y*((int)sy - height())), - zc = (int)(centering_z*((int)sz - depth())), - cc = (int)(centering_c*((int)sc - spectrum())); - - switch (boundary_conditions) { - case 2 : { // Periodic borders. - res.assign(sx,sy,sz,sc); - const int - x0 = ((int)xc%width()) - width(), - y0 = ((int)yc%height()) - height(), - z0 = ((int)zc%depth()) - depth(), - c0 = ((int)cc%spectrum()) - spectrum(); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=65536) -#endif - for (int c = c0; c<(int)sc; c+=spectrum()) - for (int z = z0; z<(int)sz; z+=depth()) - for (int y = y0; y<(int)sy; y+=height()) - for (int x = x0; x<(int)sx; x+=width()) - res.draw_image(x,y,z,c,*this); - } break; - case 1 : { // Neumann borders. - res.assign(sx,sy,sz,sc).draw_image(xc,yc,zc,cc,*this); - CImg sprite; - if (xc>0) { // X-backward - res.get_crop(xc,yc,zc,cc,xc,yc+height()-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); - for (int x = xc-1; x>=0; --x) res.draw_image(x,yc,zc,cc,sprite); - } - if (xc+width()<(int)sx) { // X-forward - res.get_crop(xc+width()-1,yc,zc,cc,xc+width()-1,yc+height()-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); - for (int x = xc+width(); x<(int)sx; ++x) res.draw_image(x,yc,zc,cc,sprite); - } - if (yc>0) { // Y-backward - res.get_crop(0,yc,zc,cc,sx-1,yc,zc+depth()-1,cc+spectrum()-1).move_to(sprite); - for (int y = yc-1; y>=0; --y) res.draw_image(0,y,zc,cc,sprite); - } - if (yc+height()<(int)sy) { // Y-forward - res.get_crop(0,yc+height()-1,zc,cc,sx-1,yc+height()-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); - for (int y = yc+height(); y<(int)sy; ++y) res.draw_image(0,y,zc,cc,sprite); - } - if (zc>0) { // Z-backward - res.get_crop(0,0,zc,cc,sx-1,sy-1,zc,cc+spectrum()-1).move_to(sprite); - for (int z = zc-1; z>=0; --z) res.draw_image(0,0,z,cc,sprite); - } - if (zc+depth()<(int)sz) { // Z-forward - res.get_crop(0,0,zc+depth()-1,cc,sx-1,sy-1,zc+depth()-1,cc+spectrum()-1).move_to(sprite); - for (int z = zc+depth(); z<(int)sz; ++z) res.draw_image(0,0,z,cc,sprite); - } - if (cc>0) { // C-backward - res.get_crop(0,0,0,cc,sx-1,sy-1,sz-1,cc).move_to(sprite); - for (int c = cc-1; c>=0; --c) res.draw_image(0,0,0,c,sprite); - } - if (cc+spectrum()<(int)sc) { // C-forward - res.get_crop(0,0,0,cc+spectrum()-1,sx-1,sy-1,sz-1,cc+spectrum()-1).move_to(sprite); - for (int c = cc+spectrum(); c<(int)sc; ++c) res.draw_image(0,0,0,c,sprite); - } - } break; - default : // Dirichlet borders. - res.assign(sx,sy,sz,sc,0).draw_image(xc,yc,zc,cc,*this); - } - break; - } break; - - // Nearest neighbor interpolation. - // - case 1 : { - res.assign(sx,sy,sz,sc); - CImg off_x(sx), off_y(sy+1), off_z(sz+1), off_c(sc+1); - const unsigned long - wh = (unsigned long)_width*_height, - whd = (unsigned long)_width*_height*_depth, - sxy = (unsigned long)sx*sy, - sxyz = (unsigned long)sx*sy*sz; - if (sx==_width) off_x.fill(1); - else { - unsigned long *poff_x = off_x._data, curr = 0; - cimg_forX(res,x) { const unsigned long old = curr; curr = ((x+1LU)*_width/sx); *(poff_x++) = curr - old; } - } - if (sy==_height) off_y.fill(_width); - else { - unsigned long *poff_y = off_y._data, curr = 0; - cimg_forY(res,y) { - const unsigned long old = curr; - curr = ((y+1LU)*_height/sy); - *(poff_y++) = _width*(curr - old); - } - *poff_y = 0; - } - if (sz==_depth) off_z.fill(wh); - else { - unsigned long *poff_z = off_z._data, curr = 0; - cimg_forZ(res,z) { - const unsigned long old = curr; - curr = ((z+1LU)*_depth/sz); - *(poff_z++) = wh*(curr - old); - } - *poff_z = 0; - } - if (sc==_spectrum) off_c.fill(whd); - else { - unsigned long *poff_c = off_c._data, curr = 0; - cimg_forC(res,c) { - const unsigned long old = curr; - curr = ((c+1LU)*_spectrum/sc); - *(poff_c++) = whd*(curr - old); - } - *poff_c = 0; - } - - T *ptrd = res._data; - const T* ptrc = _data; - const unsigned long *poff_c = off_c._data; - for (unsigned int c = 0; c tmp(sx,_height,_depth,_spectrum,0); - for (unsigned int a = _width*sx, b = _width, c = sx, s = 0, t = 0; a; ) { - const unsigned int d = cimg::min(b,c); - a-=d; b-=d; c-=d; - cimg_forYZC(tmp,y,z,v) tmp(t,y,z,v)+=(Tfloat)(*this)(s,y,z,v)*d; - if (!b) { - cimg_forYZC(tmp,y,z,v) tmp(t,y,z,v)/=_width; - ++t; - b = _width; - } - if (!c) { ++s; c = sx; } - } - tmp.move_to(res); - instance_first = false; - } - if (sy!=_height) { - CImg tmp(sx,sy,_depth,_spectrum,0); - for (unsigned int a = _height*sy, b = _height, c = sy, s = 0, t = 0; a; ) { - const unsigned int d = cimg::min(b,c); - a-=d; b-=d; c-=d; - if (instance_first) - cimg_forXZC(tmp,x,z,v) tmp(x,t,z,v)+=(Tfloat)(*this)(x,s,z,v)*d; - else - cimg_forXZC(tmp,x,z,v) tmp(x,t,z,v)+=(Tfloat)res(x,s,z,v)*d; - if (!b) { - cimg_forXZC(tmp,x,z,v) tmp(x,t,z,v)/=_height; - ++t; - b = _height; - } - if (!c) { ++s; c = sy; } - } - tmp.move_to(res); - instance_first = false; - } - if (sz!=_depth) { - CImg tmp(sx,sy,sz,_spectrum,0); - for (unsigned int a = _depth*sz, b = _depth, c = sz, s = 0, t = 0; a; ) { - const unsigned int d = cimg::min(b,c); - a-=d; b-=d; c-=d; - if (instance_first) - cimg_forXYC(tmp,x,y,v) tmp(x,y,t,v)+=(Tfloat)(*this)(x,y,s,v)*d; - else - cimg_forXYC(tmp,x,y,v) tmp(x,y,t,v)+=(Tfloat)res(x,y,s,v)*d; - if (!b) { - cimg_forXYC(tmp,x,y,v) tmp(x,y,t,v)/=_depth; - ++t; - b = _depth; - } - if (!c) { ++s; c = sz; } - } - tmp.move_to(res); - instance_first = false; - } - if (sc!=_spectrum) { - CImg tmp(sx,sy,sz,sc,0); - for (unsigned int a = _spectrum*sc, b = _spectrum, c = sc, s = 0, t = 0; a; ) { - const unsigned int d = cimg::min(b,c); - a-=d; b-=d; c-=d; - if (instance_first) - cimg_forXYZ(tmp,x,y,z) tmp(x,y,z,t)+=(Tfloat)(*this)(x,y,z,s)*d; - else - cimg_forXYZ(tmp,x,y,z) tmp(x,y,z,t)+=(Tfloat)res(x,y,z,s)*d; - if (!b) { - cimg_forXYZ(tmp,x,y,z) tmp(x,y,z,t)/=_spectrum; - ++t; - b = _spectrum; - } - if (!c) { ++s; c = sc; } - } - tmp.move_to(res); - instance_first = false; - } - } break; - - // Linear interpolation. - // - case 3 : { - CImg off(cimg::max(sx,sy,sz,sc)); - CImg foff(off._width); - CImg resx, resy, resz, resc; - - if (sx!=_width) { - if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); - else { - if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx); - else { - const float fx = (!boundary_conditions && sx>_width)?(sx>1?(_width-1.0f)/(sx-1):0):(float)_width/sx; - resx.assign(sx,_height,_depth,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forX(resx,x) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fx; - *(poff++) = (unsigned int)curr - (unsigned int)old; - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resx.size()>=65536) -#endif - cimg_forYZC(resx,y,z,c) { - const T *ptrs = data(0,y,z,c), *const ptrsmax = ptrs + (_width-1); - T *ptrd = resx.data(0,y,z,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forX(resx,x) { - const float alpha = *(pfoff++); - const T val1 = *ptrs, val2 = ptrssy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy); - else { - const float fy = (!boundary_conditions && sy>_height)?(sy>1?(_height-1.0f)/(sy-1):0):(float)_height/sy; - resy.assign(sx,sy,_depth,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forY(resy,y) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fy; - *(poff++) = sx*((unsigned int)curr-(unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resy.size()>=65536) -#endif - cimg_forXZC(resy,x,z,c) { - const T *ptrs = resx.data(x,0,z,c), *const ptrsmax = ptrs + (_height-1)*sx; - T *ptrd = resy.data(x,0,z,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forY(resy,y) { - const float alpha = *(pfoff++); - const T val1 = *ptrs, val2 = ptrssz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz); - else { - const float fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth-1.0f)/(sz-1):0):(float)_depth/sz; - const unsigned int sxy = sx*sy; - resz.assign(sx,sy,sz,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forZ(resz,z) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fz; - *(poff++) = sxy*((unsigned int)curr - (unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resz.size()>=65536) -#endif - cimg_forXYC(resz,x,y,c) { - const T *ptrs = resy.data(x,y,0,c), *const ptrsmax = ptrs + (_depth-1)*sxy; - T *ptrd = resz.data(x,y,0,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forZ(resz,z) { - const float alpha = *(pfoff++); - const T val1 = *ptrs, val2 = ptrssc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc); - else { - const float fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum-1.0f)/(sc-1):0): - (float)_spectrum/sc; - const unsigned int sxyz = sx*sy*sz; - resc.assign(sx,sy,sz,sc); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forC(resc,c) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fc; - *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resc.size()>=65536) -#endif - cimg_forXYZ(resc,x,y,z) { - const T *ptrs = resz.data(x,y,z,0), *const ptrsmax = ptrs + (_spectrum-1)*sxyz; - T *ptrd = resc.data(x,y,z,0); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forC(resc,c) { - const float alpha = *(pfoff++); - const T val1 = *ptrs, val2 = ptrs resx, resy, resz, resc; - if (sx!=_width) { - if (sx<_width) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); - else { - resx.assign(sx,_height,_depth,_spectrum,0); - const int dx = sx*2, dy = width()*2; - int err = (int)(dy + centering_x*(sx*dy/width() - dy)), xs = 0; - cimg_forX(resx,x) if ((err-=dy)<=0) { - cimg_forYZC(resx,y,z,c) resx(x,y,z,c) = (*this)(xs,y,z,c); - ++xs; - err+=dx; - } - } - } else resx.assign(*this,true); - - if (sy!=_height) { - if (sy<_height) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy); - else { - resy.assign(sx,sy,_depth,_spectrum,0); - const int dx = sy*2, dy = height()*2; - int err = (int)(dy + centering_y*(sy*dy/height() - dy)), ys = 0; - cimg_forY(resy,y) if ((err-=dy)<=0) { - cimg_forXZC(resy,x,z,c) resy(x,y,z,c) = resx(x,ys,z,c); - ++ys; - err+=dx; - } - } - resx.assign(); - } else resy.assign(resx,true); - - if (sz!=_depth) { - if (sz<_depth) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz); - else { - resz.assign(sx,sy,sz,_spectrum,0); - const int dx = sz*2, dy = depth()*2; - int err = (int)(dy + centering_z*(sz*dy/depth() - dy)), zs = 0; - cimg_forZ(resz,z) if ((err-=dy)<=0) { - cimg_forXYC(resz,x,y,c) resz(x,y,z,c) = resy(x,y,zs,c); - ++zs; - err+=dx; - } - } - resy.assign(); - } else resz.assign(resy,true); - - if (sc!=_spectrum) { - if (sc<_spectrum) resz.get_resize(sx,sy,sz,sc,1).move_to(resc); - else { - resc.assign(sx,sy,sz,sc,0); - const int dx = sc*2, dy = spectrum()*2; - int err = (int)(dy + centering_c*(sc*dy/spectrum() - dy)), cs = 0; - cimg_forC(resc,c) if ((err-=dy)<=0) { - cimg_forXYZ(resc,x,y,z) resc(x,y,z,c) = resz(x,y,z,cs); - ++cs; - err+=dx; - } - } - resz.assign(); - } else resc.assign(resz,true); - - return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc; - } break; - - // Cubic interpolation. - // - case 5 : { - const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); - CImg off(cimg::max(sx,sy,sz,sc)); - CImg foff(off._width); - CImg resx, resy, resz, resc; - - if (sx!=_width) { - if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); - else { - if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx); - else { - const float fx = (!boundary_conditions && sx>_width)?(sx>1?(_width-1.0f)/(sx-1):0):(float)_width/sx; - resx.assign(sx,_height,_depth,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forX(resx,x) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fx; - *(poff++) = (unsigned int)curr - (unsigned int)old; - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resx.size()>=65536) -#endif - cimg_forYZC(resx,y,z,c) { - const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_width-2); - T *ptrd = resx.data(0,y,z,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forX(resx,x) { - const float t = *(pfoff++); - const Tfloat - val1 = (Tfloat)*ptrs, - val0 = ptrs>ptrs0?(Tfloat)*(ptrs-1):val1, - val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+1):val1, - val3 = ptrsvmax?vmax:val); - ptrs+=*(poff++); - } - } - } - } - } else resx.assign(*this,true); - - if (sy!=_height) { - if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy); - else { - if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy); - else { - const float fy = (!boundary_conditions && sy>_height)?(sy>1?(_height-1.0f)/(sy-1):0):(float)_height/sy; - resy.assign(sx,sy,_depth,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forY(resy,y) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fy; - *(poff++) = sx*((unsigned int)curr-(unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resy.size()>=65536) -#endif - cimg_forXZC(resy,x,z,c) { - const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_height-2)*sx; - T *ptrd = resy.data(x,0,z,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forY(resy,y) { - const float t = *(pfoff++); - const Tfloat - val1 = (Tfloat)*ptrs, - val0 = ptrs>ptrs0?(Tfloat)*(ptrs-sx):val1, - val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sx):val1, - val3 = ptrsvmax?vmax:val); - ptrd+=sx; - ptrs+=*(poff++); - } - } - } - } - resx.assign(); - } else resy.assign(resx,true); - - if (sz!=_depth) { - if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz); - else { - if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz); - else { - const float fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth-1.0f)/(sz-1):0):(float)_depth/sz; - const unsigned int sxy = sx*sy; - resz.assign(sx,sy,sz,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forZ(resz,z) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fz; - *(poff++) = sxy*((unsigned int)curr - (unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resz.size()>=65536) -#endif - cimg_forXYC(resz,x,y,c) { - const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_depth-2)*sxy; - T *ptrd = resz.data(x,y,0,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forZ(resz,z) { - const float t = *(pfoff++); - const Tfloat - val1 = (Tfloat)*ptrs, - val0 = ptrs>ptrs0?(Tfloat)*(ptrs-sxy):val1, - val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxy):val1, - val3 = ptrsvmax?vmax:val); - ptrd+=sxy; - ptrs+=*(poff++); - } - } - } - } - resy.assign(); - } else resz.assign(resy,true); - - if (sc!=_spectrum) { - if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc); - else { - if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc); - else { - const float fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum-1.0f)/(sc-1):0): - (float)_spectrum/sc; - const unsigned int sxyz = sx*sy*sz; - resc.assign(sx,sy,sz,sc); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forC(resc,c) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fc; - *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resc.size()>=65536) -#endif - cimg_forXYZ(resc,x,y,z) { - const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmax = ptrs + (_spectrum-2)*sxyz; - T *ptrd = resc.data(x,y,z,0); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forC(resc,c) { - const float t = *(pfoff++); - const Tfloat - val1 = (Tfloat)*ptrs, - val0 = ptrs>ptrs0?(Tfloat)*(ptrs-sxyz):val1, - val2 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxyz):val1, - val3 = ptrsvmax?vmax:val); - ptrd+=sxyz; - ptrs+=*(poff++); - } - } - } - } - resz.assign(); - } else resc.assign(resz,true); - - return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc; - } break; - - // Lanczos interpolation. - // - case 6 : { - const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); - CImg off(cimg::max(sx,sy,sz,sc)); - CImg foff(off._width); - CImg resx, resy, resz, resc; - - if (sx!=_width) { - if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx); - else { - if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx); - else { - const float fx = (!boundary_conditions && sx>_width)?(sx>1?(_width-1.0f)/(sx-1):0):(float)_width/sx; - resx.assign(sx,_height,_depth,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forX(resx,x) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fx; - *(poff++) = (unsigned int)curr - (unsigned int)old; - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resx.size()>=65536) -#endif - cimg_forYZC(resx,y,z,c) { - const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + 1, - *const ptrsmax = ptrs0 + (_width-2); - T *ptrd = resx.data(0,y,z,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forX(resx,x) { - const float - t = *(pfoff++), - w0 = _cimg_lanczos(t+2), - w1 = _cimg_lanczos(t+1), - w2 = _cimg_lanczos(t), - w3 = _cimg_lanczos(t-1), - w4 = _cimg_lanczos(t-2); - const Tfloat - val2 = (Tfloat)*ptrs, - val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-1):val2, - val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2):val1, - val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+1):val2, - val4 = ptrsvmax?vmax:val); - ptrs+=*(poff++); - } - } - } - } - } else resx.assign(*this,true); - - if (sy!=_height) { - if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy); - else { - if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy); - else { - const float fy = (!boundary_conditions && sy>_height)?(sy>1?(_height-1.0f)/(sy-1):0):(float)_height/sy; - resy.assign(sx,sy,_depth,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forY(resy,y) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fy; - *(poff++) = sx*((unsigned int)curr-(unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resy.size()>=65536) -#endif - cimg_forXZC(resy,x,z,c) { - const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sx, - *const ptrsmax = ptrs0 + (_height-2)*sx; - T *ptrd = resy.data(x,0,z,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forY(resy,y) { - const float - t = *(pfoff++), - w0 = _cimg_lanczos(t+2), - w1 = _cimg_lanczos(t+1), - w2 = _cimg_lanczos(t), - w3 = _cimg_lanczos(t-1), - w4 = _cimg_lanczos(t-2); - const Tfloat - val2 = (Tfloat)*ptrs, - val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-sx):val2, - val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2*sx):val1, - val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sx):val2, - val4 = ptrsvmax?vmax:val); - ptrd+=sx; - ptrs+=*(poff++); - } - } - } - } - resx.assign(); - } else resy.assign(resx,true); - - if (sz!=_depth) { - if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz); - else { - if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz); - else { - const float fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth-1.0f)/(sz-1):0):(float)_depth/sz; - const unsigned int sxy = sx*sy; - resz.assign(sx,sy,sz,_spectrum); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forZ(resz,z) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fz; - *(poff++) = sxy*((unsigned int)curr - (unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resz.size()>=65536) -#endif - cimg_forXYC(resz,x,y,c) { - const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxy, - *const ptrsmax = ptrs0 + (_depth-2)*sxy; - T *ptrd = resz.data(x,y,0,c); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forZ(resz,z) { - const float - t = *(pfoff++), - w0 = _cimg_lanczos(t+2), - w1 = _cimg_lanczos(t+1), - w2 = _cimg_lanczos(t), - w3 = _cimg_lanczos(t-1), - w4 = _cimg_lanczos(t-2); - const Tfloat - val2 = (Tfloat)*ptrs, - val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-sxy):val2, - val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2*sxy):val1, - val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxy):val2, - val4 = ptrsvmax?vmax:val); - ptrd+=sxy; - ptrs+=*(poff++); - } - } - } - } - resy.assign(); - } else resz.assign(resy,true); - - if (sc!=_spectrum) { - if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc); - else { - if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc); - else { - const float fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum-1.0f)/(sc-1):0): - (float)_spectrum/sc; - const unsigned int sxyz = sx*sy*sz; - resc.assign(sx,sy,sz,sc); - float curr = 0, old = 0; - unsigned int *poff = off._data; - float *pfoff = foff._data; - cimg_forC(resc,c) { - *(pfoff++) = curr - (unsigned int)curr; - old = curr; - curr+=fc; - *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old); - } -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (resc.size()>=65536) -#endif - cimg_forXYZ(resc,x,y,z) { - const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxyz, - *const ptrsmax = ptrs + (_spectrum-2)*sxyz; - T *ptrd = resc.data(x,y,z,0); - const unsigned int *poff = off._data; - const float *pfoff = foff._data; - cimg_forC(resc,c) { - const float - t = *(pfoff++), - w0 = _cimg_lanczos(t+2), - w1 = _cimg_lanczos(t+1), - w2 = _cimg_lanczos(t), - w3 = _cimg_lanczos(t-1), - w4 = _cimg_lanczos(t-2); - const Tfloat - val2 = (Tfloat)*ptrs, - val1 = ptrs>=ptrsmin?(Tfloat)*(ptrs-sxyz):val2, - val0 = ptrs>ptrsmin?(Tfloat)*(ptrs-2*sxyz):val1, - val3 = ptrs<=ptrsmax?(Tfloat)*(ptrs+sxyz):val2, - val4 = ptrsvmax?vmax:val); - ptrd+=sxyz; - ptrs+=*(poff++); - } - } - } - } - resz.assign(); - } else resc.assign(resz,true); - - return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc; - } break; - - // Unknow interpolation. - // - default : - throw CImgArgumentException(_cimg_instance - "resize(): Invalid specified interpolation %d " - "(should be { -1=raw | 0=none | 1=nearest | 2=average | 3=linear | 4=grid | " - "5=cubic | 6=lanczos }).", - cimg_instance, - interpolation_type); - } - return res; - } - - //! Resize image to dimensions of another image. - /** - \param src Reference image used for dimensions. - \param interpolation_type Interpolation method. - \param boundary_conditions Boundary conditions. - \param centering_x Set centering type (only if \p interpolation_type=0). - \param centering_y Set centering type (only if \p interpolation_type=0). - \param centering_z Set centering type (only if \p interpolation_type=0). - \param centering_c Set centering type (only if \p interpolation_type=0). - **/ - template - CImg& resize(const CImg& src, - const int interpolation_type=1, const unsigned int boundary_conditions=0, - const float centering_x = 0, const float centering_y = 0, - const float centering_z = 0, const float centering_c = 0) { - return resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions, - centering_x,centering_y,centering_z,centering_c); - } - - //! Resize image to dimensions of another image \newinstance. - template - CImg get_resize(const CImg& src, - const int interpolation_type=1, const unsigned int boundary_conditions=0, - const float centering_x = 0, const float centering_y = 0, - const float centering_z = 0, const float centering_c = 0) const { - return get_resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions, - centering_x,centering_y,centering_z,centering_c); - } - - //! Resize image to dimensions of a display window. - /** - \param disp Reference display window used for dimensions. - \param interpolation_type Interpolation method. - \param boundary_conditions Boundary conditions. - \param centering_x Set centering type (only if \p interpolation_type=0). - \param centering_y Set centering type (only if \p interpolation_type=0). - \param centering_z Set centering type (only if \p interpolation_type=0). - \param centering_c Set centering type (only if \p interpolation_type=0). - **/ - CImg& resize(const CImgDisplay& disp, - const int interpolation_type=1, const unsigned int boundary_conditions=0, - const float centering_x = 0, const float centering_y = 0, - const float centering_z = 0, const float centering_c = 0) { - return resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions, - centering_x,centering_y,centering_z,centering_c); - } - - //! Resize image to dimensions of a display window \newinstance. - CImg get_resize(const CImgDisplay& disp, - const int interpolation_type=1, const unsigned int boundary_conditions=0, - const float centering_x = 0, const float centering_y = 0, - const float centering_z = 0, const float centering_c = 0) const { - return get_resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions, - centering_x,centering_y,centering_z,centering_c); - } - - //! Resize image to half-size along XY axes, using an optimized filter. - CImg& resize_halfXY() { - return get_resize_halfXY().move_to(*this); - } - - //! Resize image to half-size along XY axes, using an optimized filter \newinstance. - CImg get_resize_halfXY() const { - if (is_empty()) return *this; - const Tfloat mask[9] = { 0.07842776544f, 0.1231940459f, 0.07842776544f, - 0.1231940459f, 0.1935127547f, 0.1231940459f, - 0.07842776544f, 0.1231940459f, 0.07842776544f }; - T I[9] = { 0 }; - CImg res(_width/2,_height/2,_depth,_spectrum); - T *ptrd = res._data; - cimg_forZC(*this,z,c) cimg_for3x3(*this,x,y,z,c,I,T) - if (x%2 && y%2) *(ptrd++) = (T) - (I[0]*mask[0] + I[1]*mask[1] + I[2]*mask[2] + - I[3]*mask[3] + I[4]*mask[4] + I[5]*mask[5] + - I[6]*mask[6] + I[7]*mask[7] + I[8]*mask[8]); - return res; - } - - //! Resize image to double-size, using the Scale2X algorithm. - /** - \note Use anisotropic upscaling algorithm - described here. - **/ - CImg& resize_doubleXY() { - return get_resize_doubleXY().move_to(*this); - } - - //! Resize image to double-size, using the Scale2X algorithm \newinstance. - CImg get_resize_doubleXY() const { -#define _cimg_gs2x_for3(bound,i) \ - for (int i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1; \ - _n1##i<(int)(bound) || i==--_n1##i; \ - _p1##i = i++, ++_n1##i, ptrd1+=(res)._width, ptrd2+=(res)._width) - -#define _cimg_gs2x_for3x3(img,x,y,z,c,I,T) \ - _cimg_gs2x_for3((img)._height,y) for (int x = 0, \ - _p1##x = 0, \ - _n1##x = (int)( \ - (I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[3] = I[4] = (T)(img)(0,y,z,c)), \ - (I[7] = (T)(img)(0,_n1##y,z,c)), \ - 1>=(img)._width?(img).width()-1:1); \ - (_n1##x<(img).width() && ( \ - (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,y,z,c)), \ - (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ - x==--_n1##x; \ - I[1] = I[2], \ - I[3] = I[4], I[4] = I[5], \ - I[7] = I[8], \ - _p1##x = x++, ++_n1##x) - - if (is_empty()) return *this; - CImg res(_width<<1,_height<<1,_depth,_spectrum); - CImg_3x3(I,T); - cimg_forZC(*this,z,c) { - T - *ptrd1 = res.data(0,0,z,c), - *ptrd2 = ptrd1 + res._width; - _cimg_gs2x_for3x3(*this,x,y,z,c,I,T) { - if (Icp!=Icn && Ipc!=Inc) { - *(ptrd1++) = Ipc==Icp?Ipc:Icc; - *(ptrd1++) = Icp==Inc?Inc:Icc; - *(ptrd2++) = Ipc==Icn?Ipc:Icc; - *(ptrd2++) = Icn==Inc?Inc:Icc; - } else { *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc; } - } - } - return res; - } - - //! Resize image to triple-size, using the Scale3X algorithm. - /** - \note Use anisotropic upscaling algorithm - described here. - **/ - CImg& resize_tripleXY() { - return get_resize_tripleXY().move_to(*this); - } - - //! Resize image to triple-size, using the Scale3X algorithm \newinstance. - CImg get_resize_tripleXY() const { -#define _cimg_gs3x_for3(bound,i) \ - for (int i = 0, _p1##i = 0, \ - _n1##i = 1>=(bound)?(int)(bound)-1:1; \ - _n1##i<(int)(bound) || i==--_n1##i; \ - _p1##i = i++, ++_n1##i, ptrd1+=2*(res)._width, ptrd2+=2*(res)._width, ptrd3+=2*(res)._width) - -#define _cimg_gs3x_for3x3(img,x,y,z,c,I,T) \ - _cimg_gs3x_for3((img)._height,y) for (int x = 0, \ - _p1##x = 0, \ - _n1##x = (int)( \ - (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \ - (I[3] = I[4] = (T)(img)(0,y,z,c)), \ - (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \ - 1>=(img)._width?(img).width()-1:1); \ - (_n1##x<(img).width() && ( \ - (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \ - (I[5] = (T)(img)(_n1##x,y,z,c)), \ - (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \ - x==--_n1##x; \ - I[0] = I[1], I[1] = I[2], \ - I[3] = I[4], I[4] = I[5], \ - I[6] = I[7], I[7] = I[8], \ - _p1##x = x++, ++_n1##x) - - if (is_empty()) return *this; - CImg res(3*_width,3*_height,_depth,_spectrum); - CImg_3x3(I,T); - cimg_forZC(*this,z,c) { - T - *ptrd1 = res.data(0,0,z,c), - *ptrd2 = ptrd1 + res._width, - *ptrd3 = ptrd2 + res._width; - _cimg_gs3x_for3x3(*this,x,y,z,c,I,T) { - if (Icp != Icn && Ipc != Inc) { - *(ptrd1++) = Ipc==Icp?Ipc:Icc; - *(ptrd1++) = (Ipc==Icp && Icc!=Inp) || (Icp==Inc && Icc!=Ipp)?Icp:Icc; - *(ptrd1++) = Icp==Inc?Inc:Icc; - *(ptrd2++) = (Ipc==Icp && Icc!=Ipn) || (Ipc==Icn && Icc!=Ipp)?Ipc:Icc; - *(ptrd2++) = Icc; - *(ptrd2++) = (Icp==Inc && Icc!=Inn) || (Icn==Inc && Icc!=Inp)?Inc:Icc; - *(ptrd3++) = Ipc==Icn?Ipc:Icc; - *(ptrd3++) = (Ipc==Icn && Icc!=Inn) || (Icn==Inc && Icc!=Ipn)?Icn:Icc; - *(ptrd3++) = Icn==Inc?Inc:Icc; - } else { - *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd1++) = Icc; - *(ptrd2++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc; - *(ptrd3++) = Icc; *(ptrd3++) = Icc; *(ptrd3++) = Icc; - } - } - } - return res; - } - - //! Mirror image content along specified axis. - /** - \param axis Mirror axis - **/ - CImg& mirror(const char axis) { - if (is_empty()) return *this; - T *pf, *pb, *buf = 0; - switch (cimg::uncase(axis)) { - case 'x' : { - pf = _data; pb = data(_width-1); - const unsigned int width2 = _width/2; - for (unsigned int yzv = 0; yzv<_height*_depth*_spectrum; ++yzv) { - for (unsigned int x = 0; x get_mirror(const char axis) const { - return (+*this).mirror(axis); - } - - //! Mirror image content along specified axes. - /** - \param axes Mirror axes, as a C-string. - \note \c axes may contains multiple character, e.g. \c "xyz" - **/ - CImg& mirror(const char *const axes) { - for (const char *s = axes; *s; s++) mirror(*s); - return *this; - } - - //! Mirror image content along specified axes \newinstance. - CImg get_mirror(const char *const axes) const { - return (+*this).mirror(axes); - } - - //! Shift image content. - /** - \param delta_x Amount of displacement along the X-axis. - \param delta_y Amount of displacement along the Y-axis. - \param delta_z Amount of displacement along the Z-axis. - \param delta_c Amount of displacement along the C-axis. - \param boundary_conditions Border condition. - - - \c boundary_conditions can be: - - 0: Zero border condition (Dirichlet). - - 1: Nearest neighbors (Neumann). - - 2: Repeat Pattern (Fourier style). - **/ - CImg& shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0, - const int boundary_conditions=0) { - if (is_empty()) return *this; - if (delta_x) // Shift along X-axis - switch (boundary_conditions) { - case 0 : - if (cimg::abs(delta_x)>=width()) return fill(0); - if (delta_x<0) cimg_forYZC(*this,y,z,c) { - std::memmove(data(0,y,z,c),data(-delta_x,y,z,c),(_width+delta_x)*sizeof(T)); - std::memset(data(_width+delta_x,y,z,c),0,-delta_x*sizeof(T)); - } else cimg_forYZC(*this,y,z,c) { - std::memmove(data(delta_x,y,z,c),data(0,y,z,c),(_width-delta_x)*sizeof(T)); - std::memset(data(0,y,z,c),0,delta_x*sizeof(T)); - } - break; - case 1 : - if (delta_x<0) { - const int ndelta_x = (-delta_x>=width())?width()-1:-delta_x; - if (!ndelta_x) return *this; - cimg_forYZC(*this,y,z,c) { - std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T)); - T *ptrd = data(_width-1,y,z,c); - const T val = *ptrd; - for (int l = 0; l=width())?width()-1:delta_x; - if (!ndelta_x) return *this; - cimg_forYZC(*this,y,z,c) { - std::memmove(data(ndelta_x,y,z,c),data(0,y,z,c),(_width-ndelta_x)*sizeof(T)); - T *ptrd = data(0,y,z,c); - const T val = *ptrd; - for (int l = 0; l0) cimg_forYZC(*this,y,z,c) { - std::memcpy(buf,data(0,y,z,c),ndelta_x*sizeof(T)); - std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T)); - std::memcpy(data(_width-ndelta_x,y,z,c),buf,ndelta_x*sizeof(T)); - } else cimg_forYZC(*this,y,z,c) { - std::memcpy(buf,data(_width+ndelta_x,y,z,c),-ndelta_x*sizeof(T)); - std::memmove(data(-ndelta_x,y,z,c),data(0,y,z,c),(_width+ndelta_x)*sizeof(T)); - std::memcpy(data(0,y,z,c),buf,-ndelta_x*sizeof(T)); - } - delete[] buf; - } - } - - if (delta_y) // Shift along Y-axis - switch (boundary_conditions) { - case 0 : - if (cimg::abs(delta_y)>=height()) return fill(0); - if (delta_y<0) cimg_forZC(*this,z,c) { - std::memmove(data(0,0,z,c),data(0,-delta_y,z,c),_width*(_height+delta_y)*sizeof(T)); - std::memset(data(0,_height+delta_y,z,c),0,-delta_y*_width*sizeof(T)); - } else cimg_forZC(*this,z,c) { - std::memmove(data(0,delta_y,z,c),data(0,0,z,c),_width*(_height-delta_y)*sizeof(T)); - std::memset(data(0,0,z,c),0,delta_y*_width*sizeof(T)); - } - break; - case 1 : - if (delta_y<0) { - const int ndelta_y = (-delta_y>=height())?height()-1:-delta_y; - if (!ndelta_y) return *this; - cimg_forZC(*this,z,c) { - std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T)); - T *ptrd = data(0,_height-ndelta_y,z,c), *ptrs = data(0,_height-1,z,c); - for (int l = 0; l=height())?height()-1:delta_y; - if (!ndelta_y) return *this; - cimg_forZC(*this,z,c) { - std::memmove(data(0,ndelta_y,z,c),data(0,0,z,c),_width*(_height-ndelta_y)*sizeof(T)); - T *ptrd = data(0,1,z,c), *ptrs = data(0,0,z,c); - for (int l = 0; l0) cimg_forZC(*this,z,c) { - std::memcpy(buf,data(0,0,z,c),_width*ndelta_y*sizeof(T)); - std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T)); - std::memcpy(data(0,_height-ndelta_y,z,c),buf,_width*ndelta_y*sizeof(T)); - } else cimg_forZC(*this,z,c) { - std::memcpy(buf,data(0,_height+ndelta_y,z,c),-ndelta_y*_width*sizeof(T)); - std::memmove(data(0,-ndelta_y,z,c),data(0,0,z,c),_width*(_height+ndelta_y)*sizeof(T)); - std::memcpy(data(0,0,z,c),buf,-ndelta_y*_width*sizeof(T)); - } - delete[] buf; - } - } - - if (delta_z) // Shift along Z-axis - switch (boundary_conditions) { - case 0 : - if (cimg::abs(delta_z)>=depth()) return fill(0); - if (delta_z<0) cimg_forC(*this,c) { - std::memmove(data(0,0,0,c),data(0,0,-delta_z,c),_width*_height*(_depth+delta_z)*sizeof(T)); - std::memset(data(0,0,_depth+delta_z,c),0,_width*_height*(-delta_z)*sizeof(T)); - } else cimg_forC(*this,c) { - std::memmove(data(0,0,delta_z,c),data(0,0,0,c),_width*_height*(_depth-delta_z)*sizeof(T)); - std::memset(data(0,0,0,c),0,delta_z*_width*_height*sizeof(T)); - } - break; - case 1 : - if (delta_z<0) { - const int ndelta_z = (-delta_z>=depth())?depth()-1:-delta_z; - if (!ndelta_z) return *this; - cimg_forC(*this,c) { - std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T)); - T *ptrd = data(0,0,_depth-ndelta_z,c), *ptrs = data(0,0,_depth-1,c); - for (int l = 0; l=depth())?depth()-1:delta_z; - if (!ndelta_z) return *this; - cimg_forC(*this,c) { - std::memmove(data(0,0,ndelta_z,c),data(0,0,0,c),_width*_height*(_depth-ndelta_z)*sizeof(T)); - T *ptrd = data(0,0,1,c), *ptrs = data(0,0,0,c); - for (int l = 0; l0) cimg_forC(*this,c) { - std::memcpy(buf,data(0,0,0,c),_width*_height*ndelta_z*sizeof(T)); - std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T)); - std::memcpy(data(0,0,_depth-ndelta_z,c),buf,_width*_height*ndelta_z*sizeof(T)); - } else cimg_forC(*this,c) { - std::memcpy(buf,data(0,0,_depth+ndelta_z,c),-ndelta_z*_width*_height*sizeof(T)); - std::memmove(data(0,0,-ndelta_z,c),data(0,0,0,c),_width*_height*(_depth+ndelta_z)*sizeof(T)); - std::memcpy(data(0,0,0,c),buf,-ndelta_z*_width*_height*sizeof(T)); - } - delete[] buf; - } - } - - if (delta_c) // Shift along C-axis - switch (boundary_conditions) { - case 0 : - if (cimg::abs(delta_c)>=spectrum()) return fill(0); - if (delta_c<0) { - std::memmove(_data,data(0,0,0,-delta_c),_width*_height*_depth*(_spectrum+delta_c)*sizeof(T)); - std::memset(data(0,0,0,_spectrum+delta_c),0,_width*_height*_depth*(-delta_c)*sizeof(T)); - } else { - std::memmove(data(0,0,0,delta_c),_data,_width*_height*_depth*(_spectrum-delta_c)*sizeof(T)); - std::memset(_data,0,delta_c*_width*_height*_depth*sizeof(T)); - } - break; - case 1 : - if (delta_c<0) { - const int ndelta_c = (-delta_c>=spectrum())?spectrum()-1:-delta_c; - if (!ndelta_c) return *this; - std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T)); - T *ptrd = data(0,0,0,_spectrum-ndelta_c), *ptrs = data(0,0,0,_spectrum-1); - for (int l = 0; l=spectrum())?spectrum()-1:delta_c; - if (!ndelta_c) return *this; - std::memmove(data(0,0,0,ndelta_c),_data,_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T)); - T *ptrd = data(0,0,0,1); - for (int l = 0; l0) { - std::memcpy(buf,_data,_width*_height*_depth*ndelta_c*sizeof(T)); - std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T)); - std::memcpy(data(0,0,0,_spectrum-ndelta_c),buf,_width*_height*_depth*ndelta_c*sizeof(T)); - } else { - std::memcpy(buf,data(0,0,0,_spectrum+ndelta_c),-ndelta_c*_width*_height*_depth*sizeof(T)); - std::memmove(data(0,0,0,-ndelta_c),_data,_width*_height*_depth*(_spectrum+ndelta_c)*sizeof(T)); - std::memcpy(_data,buf,-ndelta_c*_width*_height*_depth*sizeof(T)); - } - delete[] buf; - } - } - return *this; - } - - //! Shift image content \newinstance. - CImg get_shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0, - const int boundary_conditions=0) const { - return (+*this).shift(delta_x,delta_y,delta_z,delta_c,boundary_conditions); - } - - //! Permute axes order. - /** - \param order Axes permutations, as a C-string of 4 characters. - This function permutes image content regarding the specified axes permutation. - **/ - CImg& permute_axes(const char *const order) { - return get_permute_axes(order).move_to(*this); - } - - //! Permute axes order \newinstance. - CImg get_permute_axes(const char *const order) const { - const T foo = (T)0; - return _get_permute_axes(order,foo); - } - - template - CImg _get_permute_axes(const char *const permut, const t&) const { - if (is_empty() || !permut) return CImg(*this,false); - CImg res; - const T* ptrs = _data; - if (!cimg::strncasecmp(permut,"xyzc",4)) return +*this; - if (!cimg::strncasecmp(permut,"xycz",4)) { - res.assign(_width,_height,_spectrum,_depth); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(x,y,c,z,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"xzyc",4)) { - res.assign(_width,_depth,_height,_spectrum); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(x,z,y,c,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"xzcy",4)) { - res.assign(_width,_depth,_spectrum,_height); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(x,z,c,y,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"xcyz",4)) { - res.assign(_width,_spectrum,_height,_depth); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(x,c,y,z,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"xczy",4)) { - res.assign(_width,_spectrum,_depth,_height); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(x,c,z,y,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"yxzc",4)) { - res.assign(_height,_width,_depth,_spectrum); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(y,x,z,c,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"yxcz",4)) { - res.assign(_height,_width,_spectrum,_depth); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(y,x,c,z,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"yzxc",4)) { - res.assign(_height,_depth,_width,_spectrum); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(y,z,x,c,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"yzcx",4)) { - res.assign(_height,_depth,_spectrum,_width); - switch (_width) { - case 1 : { - t *ptr_r = res.data(0,0,0,0); - for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { - *(ptr_r++) = (t)*(ptrs++); - } - } break; - case 2 : { - t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1); - for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { - *(ptr_r++) = (t)*(ptrs++); *(ptr_g++) = (t)*(ptrs++); - } - } break; - case 3 : { // Optimization for the classical conversion from interleaved RGB to planar RGB - t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2); - for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { - *(ptr_r++) = (t)*(ptrs++); *(ptr_g++) = (t)*(ptrs++); *(ptr_b++) = (t)*(ptrs++); - } - } break; - case 4 : { // Optimization for the classical conversion from interleaved RGBA to planar RGBA - t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2), *ptr_a = res.data(0,0,0,3); - for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) { - *(ptr_r++) = (t)*(ptrs++); *(ptr_g++) = (t)*(ptrs++); *(ptr_b++) = (t)*(ptrs++); *(ptr_a++) = (t)*(ptrs++); - } - } break; - default : { - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(y,z,c,x,wh,whd) = *(ptrs++); - return res; - } - } - } - if (!cimg::strncasecmp(permut,"ycxz",4)) { - res.assign(_height,_spectrum,_width,_depth); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(y,c,x,z,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"yczx",4)) { - res.assign(_height,_spectrum,_depth,_width); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(y,c,z,x,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"zxyc",4)) { - res.assign(_depth,_width,_height,_spectrum); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(z,x,y,c,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"zxcy",4)) { - res.assign(_depth,_width,_spectrum,_height); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(z,x,c,y,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"zyxc",4)) { - res.assign(_depth,_height,_width,_spectrum); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(z,y,x,c,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"zycx",4)) { - res.assign(_depth,_height,_spectrum,_width); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(z,y,c,x,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"zcxy",4)) { - res.assign(_depth,_spectrum,_width,_height); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(z,c,x,y,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"zcyx",4)) { - res.assign(_depth,_spectrum,_height,_width); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(z,c,y,x,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"cxyz",4)) { - res.assign(_spectrum,_width,_height,_depth); - switch (_spectrum) { - case 1 : { - const T *ptr_r = data(0,0,0,0); - t *ptrd = res._data; - for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) *(ptrd++) = (t)*(ptr_r++); - } break; - case 2 : { - const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1); - t *ptrd = res._data; - for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) { - *(ptrd++) = (t)*(ptr_r++); *(ptrd++) = (t)*(ptr_g++); - } - } break; - case 3 : { // Optimization for the classical conversion from planar RGB to interleaved RGB - const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); - t *ptrd = res._data; - for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) { - *(ptrd++) = (t)*(ptr_r++); *(ptrd++) = (t)*(ptr_g++); *(ptrd++) = (t)*(ptr_b++); - } - } break; - case 4 : { // Optimization for the classical conversion from planar RGBA to interleaved RGBA - const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3); - t *ptrd = res._data; - for (unsigned long siz = (unsigned long)_width*_height*_depth; siz; --siz) { - *(ptrd++) = (t)*(ptr_r++); *(ptrd++) = (t)*(ptr_g++); *(ptrd++) = (t)*(ptr_b++); *(ptrd++) = (t)*(ptr_a++); - } - } break; - default : { - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(c,x,y,z,wh,whd) = (t)*(ptrs++); - } - } - } - if (!cimg::strncasecmp(permut,"cxzy",4)) { - res.assign(_spectrum,_width,_depth,_height); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(c,x,z,y,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"cyxz",4)) { - res.assign(_spectrum,_height,_width,_depth); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(c,y,x,z,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"cyzx",4)) { - res.assign(_spectrum,_height,_depth,_width); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(c,y,z,x,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"czxy",4)) { - res.assign(_spectrum,_depth,_width,_height); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(c,z,x,y,wh,whd) = (t)*(ptrs++); - } - if (!cimg::strncasecmp(permut,"czyx",4)) { - res.assign(_spectrum,_depth,_height,_width); - const unsigned long wh = (unsigned long)res._width*res._height, whd = wh*res._depth; - cimg_forXYZC(*this,x,y,z,c) res(c,z,y,x,wh,whd) = (t)*(ptrs++); - } - if (!res) - throw CImgArgumentException(_cimg_instance - "permute_axes(): Invalid specified permutation '%s'.", - cimg_instance, - permut); - return res; - } - - //! Unroll pixel values along specified axis. - /** - \param axis Unroll axis (can be \c 'x', \c 'y', \c 'z' or c 'c'). - **/ - CImg& unroll(const char axis) { - const unsigned int siz = size(); - if (siz) switch (axis) { - case 'x' : _width = siz; _height = _depth = _spectrum = 1; break; - case 'y' : _height = siz; _width = _depth = _spectrum = 1; break; - case 'z' : _depth = siz; _width = _height = _spectrum = 1; break; - default : _spectrum = siz; _width = _height = _depth = 1; - } - return *this; - } - - //! Unroll pixel values along specified axis \newinstance. - CImg get_unroll(const char axis) const { - return (+*this).unroll(axis); - } - - //! Rotate image with arbitrary angle. - /** - \param angle Rotation angle, in degrees. - \param interpolation Type of interpolation. Can be { 0=nearest | 1=linear | 2=cubic }. - \param boundary Boundary conditions. Can be { 0=dirichlet | 1=neumann | 2=periodic }. - \note Most of the time, size of the image is modified. - **/ - CImg& rotate(const float angle, const unsigned int interpolation=1, const unsigned int boundary=0) { - const float nangle = cimg::mod(angle,360.0f); - if (nangle==0.0f) return *this; - return get_rotate(angle,interpolation,boundary).move_to(*this); - } - - //! Rotate image with arbitrary angle \newinstance. - CImg get_rotate(const float angle, const unsigned int interpolation=1, const unsigned int boundary=0) const { - if (is_empty()) return *this; - CImg res; - const float nangle = cimg::mod(angle,360.0f); - if (boundary!=1 && cimg::mod(nangle,90.0f)==0) { // Optimized version for orthogonal angles. - const int wm1 = width() - 1, hm1 = height() - 1; - const int iangle = (int)nangle/90; - switch (iangle) { - case 1 : { // 90 deg. - res.assign(_height,_width,_depth,_spectrum); - T *ptrd = res._data; - cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(y,hm1-x,z,c); - } break; - case 2 : { // 180 deg. - res.assign(_width,_height,_depth,_spectrum); - T *ptrd = res._data; - cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1-x,hm1-y,z,c); - } break; - case 3 : { // 270 deg. - res.assign(_height,_width,_depth,_spectrum); - T *ptrd = res._data; - cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1-y,x,z,c); - } break; - default : // 0 deg. - return *this; - } - } else { // Generic angle. - const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); - const float - rad = (float)(nangle*cimg::PI/180.0), - ca = (float)std::cos(rad), - sa = (float)std::sin(rad), - ux = cimg::abs(_width*ca), uy = cimg::abs(_width*sa), - vx = cimg::abs(_height*sa), vy = cimg::abs(_height*ca), - w2 = 0.5f*_width, h2 = 0.5f*_height, - dw2 = 0.5f*(ux+vx), dh2 = 0.5f*(uy+vy); - res.assign((int)(ux+vx),(int)(uy+vy),_depth,_spectrum); - switch (boundary) { - case 0 : { // Dirichlet boundaries. - switch (interpolation) { - case 2 : { // Cubic interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) { - const Tfloat val = cubic_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c,0); - res(x,y,z,c) = (T)(valvmax?vmax:val); - } - } break; - case 1 : { // Linear interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (T)linear_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c,0); - } break; - default : { // Nearest-neighbor interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = atXY((int)(w2 + (x-dw2)*ca + (y-dh2)*sa),(int)(h2 - (x-dw2)*sa + (y-dh2)*ca),z,c,0); - } - } - } break; - case 1 : { // Neumann boundaries. - switch (interpolation) { - case 2 : { // Cubic interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) { - const Tfloat val = _cubic_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c); - res(x,y,z,c) = (T)(valvmax?vmax:val); - } - } break; - case 1 : { // Linear interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (T)_linear_atXY(w2 + (x-dw2)*ca + (y-dh2)*sa,h2 - (x-dw2)*sa + (y-dh2)*ca,z,c); - } break; - default : { // Nearest-neighbor interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = _atXY((int)(w2 + (x-dw2)*ca + (y-dh2)*sa),(int)(h2 - (x-dw2)*sa + (y-dh2)*ca),z,c); - } - } - } break; - case 2 : { // Periodic boundaries. - switch (interpolation) { - case 2 : { // Cubic interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) { - const Tfloat val = _cubic_atXY(cimg::mod(w2 + (x-dw2)*ca + (y-dh2)*sa,(float)width()), - cimg::mod(h2 - (x-dw2)*sa + (y-dh2)*ca,(float)height()),z,c); - res(x,y,z,c) = (T)(valvmax?vmax:val); - } - } break; - case 1 : { // Linear interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (T)_linear_atXY(cimg::mod(w2 + (x-dw2)*ca + (y-dh2)*sa,(float)width()), - cimg::mod(h2 - (x-dw2)*sa + (y-dh2)*ca,(float)height()),z,c); - } break; - default : { // Nearest-neighbor interpolation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (*this)(cimg::mod((int)(w2 + (x-dw2)*ca + (y-dh2)*sa),width()), - cimg::mod((int)(h2 - (x-dw2)*sa + (y-dh2)*ca),height()),z,c); - } - } - } break; - default : - throw CImgArgumentException(_cimg_instance - "rotate(): Invalid specified border conditions %d " - "(should be { 0=dirichlet | 1=neumann | 2=periodic }).", - cimg_instance, - boundary); - } - } - return res; - } - - //! Rotate image with arbitrary angle, around a center point. - /** - \param angle Rotation angle, in degrees. - \param cx X-coordinate of the rotation center. - \param cy Y-coordinate of the rotation center. - \param zoom Zoom factor. - \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann | 2=periodic }. - \param interpolation_type Type of interpolation. Can be { 0=nearest | 1=linear | 2=cubic }. - **/ - CImg& rotate(const float angle, const float cx, const float cy, const float zoom, - const unsigned int interpolation=1, const unsigned int boundary=3) { - return get_rotate(angle,cx,cy,zoom,interpolation,boundary).move_to(*this); - } - - //! Rotate image with arbitrary angle, around a center point \newinstance. - CImg get_rotate(const float angle, const float cx, const float cy, const float zoom, - const unsigned int interpolation=1, const unsigned int boundary=3) const { - if (interpolation>2) - throw CImgArgumentException(_cimg_instance - "rotate(): Invalid specified interpolation type %d " - "(should be { 0=none | 1=linear | 2=cubic }).", - cimg_instance, - interpolation); - if (is_empty()) return *this; - CImg res(_width,_height,_depth,_spectrum); - const Tfloat vmin = (Tfloat)cimg::type::min(), vmax = (Tfloat)cimg::type::max(); - const float - rad = (float)((angle*cimg::PI)/180.0), - ca = (float)std::cos(rad)/zoom, - sa = (float)std::sin(rad)/zoom; - switch (boundary) { - case 0 : { - switch (interpolation) { - case 2 : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) { - const Tfloat val = cubic_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c,0); - res(x,y,z,c) = (T)(valvmax?vmax:val); - } - } break; - case 1 : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (T)linear_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c,0); - } break; - default : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = atXY((int)(cx + (x-cx)*ca + (y-cy)*sa),(int)(cy - (x-cx)*sa + (y-cy)*ca),z,c,0); - } - } - } break; - case 1 : { - switch (interpolation) { - case 2 : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) { - const Tfloat val = _cubic_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c); - res(x,y,z,c) = (T)(valvmax?vmax:val); - } - } break; - case 1 : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (T)_linear_atXY(cx + (x-cx)*ca + (y-cy)*sa,cy - (x-cx)*sa + (y-cy)*ca,z,c); - } break; - default : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = _atXY((int)(cx + (x-cx)*ca + (y-cy)*sa),(int)(cy - (x-cx)*sa + (y-cy)*ca),z,c); - } - } - } break; - case 2 : { - switch (interpolation) { - case 2 : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) { - const Tfloat val = _cubic_atXY(cimg::mod(cx + (x-cx)*ca + (y-cy)*sa,(float)width()), - cimg::mod(cy - (x-cx)*sa + (y-cy)*ca,(float)height()),z,c); - res(x,y,z,c) = (T)(valvmax?vmax:val); - } - } break; - case 1 : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (T)_linear_atXY(cimg::mod(cx + (x-cx)*ca + (y-cy)*sa,(float)width()), - cimg::mod(cy - (x-cx)*sa + (y-cy)*ca,(float)height()),z,c); - } break; - default : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=2048) -#endif - cimg_forXYZC(res,x,y,z,c) - res(x,y,z,c) = (*this)(cimg::mod((int)(cx + (x-cx)*ca + (y-cy)*sa),width()), - cimg::mod((int)(cy - (x-cx)*sa + (y-cy)*ca),height()),z,c); - } - } - } break; - default : - throw CImgArgumentException(_cimg_instance - "rotate(): Invalid specified border conditions %d " - "(should be { 0=dirichlet | 1=neumann | 2=periodic }).", - cimg_instance, - boundary); - } - return res; - } - - //! Warp image content by a warping field. - /** - \param warp Warping field. - \param is_relative Tells if warping field gives absolute or relative warping coordinates. - \param interpolation Can be { 0=nearest | 1=linear | 2=cubic }. - \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann | 2=periodic }. - **/ - template - CImg& warp(const CImg& warp, const bool is_relative=false, - const unsigned int interpolation=1, const unsigned int boundary_conditions=0) { - return get_warp(warp,is_relative,interpolation,boundary_conditions).move_to(*this); - } - - //! Warp image content by a warping field \newinstance - template - CImg get_warp(const CImg& warp, const bool is_relative=false, - const unsigned int interpolation=1, const unsigned int boundary_conditions=0) const { - if (is_empty() || !warp) return *this; - if (is_relative && !is_sameXYZ(warp)) - throw CImgArgumentException(_cimg_instance - "warp(): Instance and specified relative warping field (%u,%u,%u,%u,%p) " - "have different XYZ dimensions.", - cimg_instance, - warp._width,warp._height,warp._depth,warp._spectrum,warp._data); - - CImg res(warp._width,warp._height,warp._depth,_spectrum); - - if (warp._spectrum==1) { // 1d warping. - if (is_relative) { // Relative warp. - if (interpolation==2) { // Cubic interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX(cimg::mod(x - (float)*(ptrs0++),(float)_width),y,z,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX(x - (float)*(ptrs0++),y,z,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)cubic_atX(x - (float)*(ptrs0++),y,z,c,0); - } - } else if (interpolation==1) { // Linear interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(cimg::mod(x - (float)*(ptrs0++),(float)_width),y,z,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(x - (float)*(ptrs0++),y,z,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)linear_atX(x - (float)*(ptrs0++),y,z,c,0); - } - } else { // Nearest-neighbor interpolation. - if (boundary_conditions==2) // Periodic boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)*(ptrs0++),(int)_width),y,z,c); - } - else if (boundary_conditions==1) // Neumann boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = _atX(x - (int)*(ptrs0++),y,z,c); - } - else // Dirichlet boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = atX(x - (int)*(ptrs0++),y,z,c,0); - } - } - } else { // Absolute warp. - if (interpolation==2) { // Cubic interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX(cimg::mod((float)*(ptrs0++),(float)_width),0,0,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atX((float)*(ptrs0++),0,0,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)cubic_atX((float)*(ptrs0++),0,0,c,0); - } - } else if (interpolation==1) { // Linear interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(cimg::mod((float)*(ptrs0++),(float)_width),0,0,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atX((float)*(ptrs0++),0,0,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)linear_atX((float)*(ptrs0++),0,0,c,0); - } - } else { // Nearest-neighbor interpolation. - if (boundary_conditions==2) // Periodic boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)*(ptrs0++),(int)_width),0,0,c); - } - else if (boundary_conditions==1) // Neumann boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = _atX((int)*(ptrs0++),0,0,c); - } - else // Dirichlet boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = atX((int)*(ptrs0++),0,0,c,0); - } - } - } - - } else if (warp._spectrum==2) { // 2d warping. - if (is_relative) { // Relative warp. - if (interpolation==2) { // Cubic interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY(cimg::mod(x - (float)*(ptrs0++),(float)_width), - cimg::mod(y - (float)*(ptrs1++),(float)_height),z,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)cubic_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,0); - } - } else if (interpolation==1) { // Linear interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(cimg::mod(x - (float)*(ptrs0++),(float)_width), - cimg::mod(y - (float)*(ptrs1++),(float)_height),z,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,0); - } - } else { // Nearest-neighbor interpolation. - if (boundary_conditions==2) // Periodic boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)*(ptrs0++),(int)_width), - cimg::mod(y - (int)*(ptrs1++),(int)_height),z,c); - } - else if (boundary_conditions==1) // Neumann boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = _atXY(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z,c); - } - else // Dirichlet boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = atXY(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z,c,0); - } - } - } else { // Absolute warp. - if (interpolation==2) { // Cubic interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY(cimg::mod((float)*(ptrs0++),(float)_width), - cimg::mod((float)*(ptrs1++),(float)_height),0,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)cubic_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c,0); - } - } else if (interpolation==1) { // Linear interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(cimg::mod((float)*(ptrs0++),(float)_width), - cimg::mod((float)*(ptrs1++),(float)_height),0,c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c,0); - } - } else { // Nearest-neighbor interpolation. - if (boundary_conditions==2) // Periodic boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)*(ptrs0++),(int)_width), - cimg::mod((int)*(ptrs1++),(int)_height),0,c); - } - else if (boundary_conditions==1) // Neumann boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = _atXY((int)*(ptrs0++),(int)*(ptrs1++),0,c); - } - else // Dirichlet boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = atXY((int)*(ptrs0++),(int)*(ptrs1++),0,c,0); - } - } - } - - } else if (warp._spectrum==3) { // 3d warping. - if (is_relative) { // Relative warp. - if (interpolation==2) { // Cubic interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXYZ(cimg::mod(x - (float)*(ptrs0++),(float)_width), - cimg::mod(y - (float)*(ptrs1++),(float)_height), - cimg::mod(z - (float)*(ptrs2++),(float)_depth),c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) - *(ptrd++) = (T)_cubic_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) - *(ptrd++) = (T)cubic_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,0); - } - } else if (interpolation==1) { // Linear interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ(cimg::mod(x - (float)*(ptrs0++),(float)_width), - cimg::mod(y - (float)*(ptrs1++),(float)_height), - cimg::mod(z - (float)*(ptrs2++),(float)_depth),c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) - *(ptrd++) = (T)_linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) - *(ptrd++) = (T)linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,0); - } - } else { // Nearest neighbor interpolation. - if (boundary_conditions==2) // Periodic boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)*(ptrs0++),(int)_width), - cimg::mod(y - (int)*(ptrs1++),(int)_height), - cimg::mod(z - (int)*(ptrs2++),(int)_depth),c); - } - else if (boundary_conditions==1) // Neumann boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = _atXYZ(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z - (int)*(ptrs2++),c); - } - else // Dirichlet boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = atXYZ(x - (int)*(ptrs0++),y - (int)*(ptrs1++),z - (int)*(ptrs2++),c,0); - } - } - } else { // Absolute warp. - if (interpolation==2) { // Cubic interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXYZ(cimg::mod((float)*(ptrs0++),(float)_width), - cimg::mod((float)*(ptrs1++),(float)_height), - cimg::mod((float)*(ptrs2++),(float)_depth),c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_cubic_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=4096) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)cubic_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c,0); - } - } else if (interpolation==1) { // Linear interpolation. - if (boundary_conditions==2) // Periodic boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ(cimg::mod((float)*(ptrs0++),(float)_width), - cimg::mod((float)*(ptrs1++),(float)_height), - cimg::mod((float)*(ptrs2++),(float)_depth),c); - } - else if (boundary_conditions==1) // Neumann boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c); - } - else // Dirichlet boundaries. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (res.size()>=1048576) -#endif - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (T)linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c,0); - } - } else { // Nearest-neighbor interpolation. - if (boundary_conditions==2) // Periodic boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)*(ptrs0++),(int)_width), - cimg::mod((int)*(ptrs1++),(int)_height), - cimg::mod((int)*(ptrs2++),(int)_depth),c); - } - else if (boundary_conditions==1) // Neumann boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = _atXYZ((int)*(ptrs0++),(int)*(ptrs1++),(int)*(ptrs2++),c); - } - else // Dirichlet boundaries. - cimg_forYZC(res,y,z,c) { - const t *ptrs0 = warp.data(0,y,z,0), *ptrs1 = warp.data(0,y,z,1), *ptrs2 = warp.data(0,y,z,2); - T *ptrd = res.data(0,y,z,c); - cimg_forX(res,x) *(ptrd++) = atXYZ((int)*(ptrs0++),(int)*(ptrs1++),(int)*(ptrs2++),c,0); - } - } - } - } - return res; - } - - //! Generate a 2d representation of a 3d image, with XY,XZ and YZ views. - /** - \param x0 X-coordinate of the projection point. - \param y0 Y-coordinate of the projection point. - \param z0 Z-coordinate of the projection point. - **/ - CImg get_projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) const { - if (is_empty() || _depth<2) return +*this; - const unsigned int - _x0 = (x0>=_width)?_width - 1:x0, - _y0 = (y0>=_height)?_height - 1:y0, - _z0 = (z0>=_depth)?_depth - 1:z0; - const CImg - img_xy = get_crop(0,0,_z0,0,_width-1,_height-1,_z0,_spectrum-1), - img_zy = get_crop(_x0,0,0,0,_x0,_height-1,_depth-1,_spectrum-1).permute_axes("xzyc"). - resize(_depth,_height,1,-100,-1), - img_xz = get_crop(0,_y0,0,0,_width-1,_y0,_depth-1,_spectrum-1).resize(_width,_depth,1,-100,-1); - return CImg(_width + _depth,_height + _depth,1,_spectrum,cimg::min(img_xy.min(),img_zy.min(),img_xz.min())). - draw_image(0,0,img_xy).draw_image(img_xy._width,0,img_zy). - draw_image(0,img_xy._height,img_xz); - } - - //! Construct a 2d representation of a 3d image, with XY,XZ and YZ views \inplace. - CImg& projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) { - if (_depth<2) return *this; - return get_projections2d(x0,y0,z0).move_to(*this); - } - - //! Crop image region. - /** - \param x0 = X-coordinate of the upper-left crop rectangle corner. - \param y0 = Y-coordinate of the upper-left crop rectangle corner. - \param z0 = Z-coordinate of the upper-left crop rectangle corner. - \param c0 = C-coordinate of the upper-left crop rectangle corner. - \param x1 = X-coordinate of the lower-right crop rectangle corner. - \param y1 = Y-coordinate of the lower-right crop rectangle corner. - \param z1 = Z-coordinate of the lower-right crop rectangle corner. - \param c1 = C-coordinate of the lower-right crop rectangle corner. - \param boundary_conditions = Dirichlet (false) or Neumann border conditions. - **/ - CImg& crop(const int x0, const int y0, const int z0, const int c0, - const int x1, const int y1, const int z1, const int c1, - const bool boundary_conditions=false) { - return get_crop(x0,y0,z0,c0,x1,y1,z1,c1,boundary_conditions).move_to(*this); - } - - //! Crop image region \newinstance. - CImg get_crop(const int x0, const int y0, const int z0, const int c0, - const int x1, const int y1, const int z1, const int c1, - const bool boundary_conditions=false) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "crop(): Empty instance.", - cimg_instance); - const int - nx0 = x0 res(1U + nx1 - nx0,1U + ny1 - ny0,1U + nz1 - nz0,1U + nc1 - nc0); - if (nx0<0 || nx1>=width() || ny0<0 || ny1>=height() || nz0<0 || nz1>=depth() || nc0<0 || nc1>=spectrum()) { - if (boundary_conditions) cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _atXYZC(nx0+x,ny0+y,nz0+z,nc0+c); - else res.fill(0).draw_image(-nx0,-ny0,-nz0,-nc0,*this); - } else res.draw_image(-nx0,-ny0,-nz0,-nc0,*this); - return res; - } - - //! Crop image region \overloading. - CImg& crop(const int x0, const int y0, const int z0, - const int x1, const int y1, const int z1, - const bool boundary_conditions=false) { - return crop(x0,y0,z0,0,x1,y1,z1,_spectrum-1,boundary_conditions); - } - - //! Crop image region \newinstance. - CImg get_crop(const int x0, const int y0, const int z0, - const int x1, const int y1, const int z1, - const bool boundary_conditions=false) const { - return get_crop(x0,y0,z0,0,x1,y1,z1,_spectrum-1,boundary_conditions); - } - - //! Crop image region \overloading. - CImg& crop(const int x0, const int y0, - const int x1, const int y1, - const bool boundary_conditions=false) { - return crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions); - } - - //! Crop image region \newinstance. - CImg get_crop(const int x0, const int y0, - const int x1, const int y1, - const bool boundary_conditions=false) const { - return get_crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions); - } - - //! Crop image region \overloading. - CImg& crop(const int x0, const int x1, const bool boundary_conditions=false) { - return crop(x0,0,0,0,x1,_height-1,_depth-1,_spectrum-1,boundary_conditions); - } - - //! Crop image region \newinstance. - CImg get_crop(const int x0, const int x1, const bool boundary_conditions=false) const { - return get_crop(x0,0,0,0,x1,_height-1,_depth-1,_spectrum-1,boundary_conditions); - } - - //! Autocrop image region, regarding the specified background value. - CImg& autocrop(const T value, const char *const axes="czyx") { - if (is_empty()) return *this; - for (const char *s = axes; *s; ++s) { - const char axis = cimg::uncase(*s); - const CImg coords = _autocrop(value,axis); - if (coords[0]==-1 && coords[1]==-1) return assign(); // Image has only 'value' pixels. - else switch (axis) { - case 'x' : { - const int x0 = coords[0], x1 = coords[1]; - if (x0>=0 && x1>=0) crop(x0,x1); - } break; - case 'y' : { - const int y0 = coords[0], y1 = coords[1]; - if (y0>=0 && y1>=0) crop(0,y0,_width-1,y1); - } break; - case 'z' : { - const int z0 = coords[0], z1 = coords[1]; - if (z0>=0 && z1>=0) crop(0,0,z0,_width-1,_height-1,z1); - } break; - default : { - const int c0 = coords[0], c1 = coords[1]; - if (c0>=0 && c1>=0) crop(0,0,0,c0,_width-1,_height-1,_depth-1,c1); - } - } - } - return *this; - } - - //! Autocrop image region, regarding the specified background value \newinstance. - CImg get_autocrop(const T value, const char *const axes="czyx") const { - return (+*this).autocrop(value,axes); - } - - //! Autocrop image region, regarding the specified background color. - /** - \param color Color used for the crop. If \c 0, color is guessed. - \param axes Axes used for the crop. - **/ - CImg& autocrop(const T *const color=0, const char *const axes="zyx") { - if (is_empty()) return *this; - if (!color) { // Guess color. - const CImg col1 = get_vector_at(0,0,0); - const unsigned int w = _width, h = _height, d = _depth, s = _spectrum; - autocrop(col1,axes); - if (_width==w && _height==h && _depth==d && _spectrum==s) { - const CImg col2 = get_vector_at(w-1,h-1,d-1); - autocrop(col2,axes); - } - return *this; - } - for (const char *s = axes; *s; ++s) { - const char axis = cimg::uncase(*s); - switch (axis) { - case 'x' : { - int x0 = width(), x1 = -1; - cimg_forC(*this,c) { - const CImg coords = get_shared_channel(c)._autocrop(color[c],'x'); - const int nx0 = coords[0], nx1 = coords[1]; - if (nx0>=0 && nx1>=0) { x0 = cimg::min(x0,nx0); x1 = cimg::max(x1,nx1); } - } - if (x0==width() && x1==-1) return assign(); else crop(x0,x1); - } break; - case 'y' : { - int y0 = height(), y1 = -1; - cimg_forC(*this,c) { - const CImg coords = get_shared_channel(c)._autocrop(color[c],'y'); - const int ny0 = coords[0], ny1 = coords[1]; - if (ny0>=0 && ny1>=0) { y0 = cimg::min(y0,ny0); y1 = cimg::max(y1,ny1); } - } - if (y0==height() && y1==-1) return assign(); else crop(0,y0,_width-1,y1); - } break; - default : { - int z0 = depth(), z1 = -1; - cimg_forC(*this,c) { - const CImg coords = get_shared_channel(c)._autocrop(color[c],'z'); - const int nz0 = coords[0], nz1 = coords[1]; - if (nz0>=0 && nz1>=0) { z0 = cimg::min(z0,nz0); z1 = cimg::max(z1,nz1); } - } - if (z0==depth() && z1==-1) return assign(); else crop(0,0,z0,_width-1,_height-1,z1); - } - } - } - return *this; - } - - //! Autocrop image region, regarding the specified background color \newinstance. - CImg get_autocrop(const T *const color=0, const char *const axes="zyx") const { - return (+*this).autocrop(color,axes); - } - - //! Autocrop image region, regarding the specified background color \overloading. - template CImg& autocrop(const CImg& color, const char *const axes="zyx") { - return get_autocrop(color,axes).move_to(*this); - } - - //! Autocrop image region, regarding the specified background color \newinstance. - template CImg get_autocrop(const CImg& color, const char *const axes="zyx") const { - return get_autocrop(color._data,axes); - } - - CImg _autocrop(const T value, const char axis) const { - CImg res; - switch (cimg::uncase(axis)) { - case 'x' : { - int x0 = -1, x1 = -1; - cimg_forX(*this,x) cimg_forYZC(*this,y,z,c) - if ((*this)(x,y,z,c)!=value) { x0 = x; x = width(); y = height(); z = depth(); c = spectrum(); } - if (x0>=0) { - for (int x = width()-1; x>=0; --x) cimg_forYZC(*this,y,z,c) - if ((*this)(x,y,z,c)!=value) { x1 = x; x = 0; y = height(); z = depth(); c = spectrum(); } - } - res = CImg::vector(x0,x1); - } break; - case 'y' : { - int y0 = -1, y1 = -1; - cimg_forY(*this,y) cimg_forXZC(*this,x,z,c) - if ((*this)(x,y,z,c)!=value) { y0 = y; x = width(); y = height(); z = depth(); c = spectrum(); } - if (y0>=0) { - for (int y = height()-1; y>=0; --y) cimg_forXZC(*this,x,z,c) - if ((*this)(x,y,z,c)!=value) { y1 = y; x = width(); y = 0; z = depth(); c = spectrum(); } - } - res = CImg::vector(y0,y1); - } break; - case 'z' : { - int z0 = -1, z1 = -1; - cimg_forZ(*this,z) cimg_forXYC(*this,x,y,c) - if ((*this)(x,y,z,c)!=value) { z0 = z; x = width(); y = height(); z = depth(); c = spectrum(); } - if (z0>=0) { - for (int z = depth()-1; z>=0; --z) cimg_forXYC(*this,x,y,c) - if ((*this)(x,y,z,c)!=value) { z1 = z; x = width(); y = height(); z = 0; c = spectrum(); } - } - res = CImg::vector(z0,z1); - } break; - default : { - int c0 = -1, c1 = -1; - cimg_forC(*this,c) cimg_forXYZ(*this,x,y,z) - if ((*this)(x,y,z,c)!=value) { c0 = c; x = width(); y = height(); z = depth(); c = spectrum(); } - if (c0>=0) { - for (int c = spectrum()-1; c>=0; --c) cimg_forXYZ(*this,x,y,z) - if ((*this)(x,y,z,c)!=value) { c1 = c; x = width(); y = height(); z = depth(); c = 0; } - } - res = CImg::vector(c0,c1); - } - } - return res; - } - - //! Return specified image column. - /** - \param x0 Image column. - **/ - CImg get_column(const int x0) const { - return get_columns(x0,x0); - } - - //! Return specified image column \inplace. - CImg& column(const int x0) { - return columns(x0,x0); - } - - //! Return specified range of image columns. - /** - \param x0 Starting image column. - \param x1 Ending image column. - **/ - CImg& columns(const int x0, const int x1) { - return get_columns(x0,x1).move_to(*this); - } - - //! Return specified range of image columns \inplace. - CImg get_columns(const int x0, const int x1) const { - return get_crop(x0,0,0,0,x1,height()-1,depth()-1,spectrum()-1); - } - - //! Return specified image row. - CImg get_row(const int y0) const { - return get_rows(y0,y0); - } - - //! Return specified image row \inplace. - /** - \param y0 Image row. - **/ - CImg& row(const int y0) { - return rows(y0,y0); - } - - //! Return specified range of image rows. - /** - \param y0 Starting image row. - \param y1 Ending image row. - **/ - CImg get_rows(const int y0, const int y1) const { - return get_crop(0,y0,0,0,width()-1,y1,depth()-1,spectrum()-1); - } - - //! Return specified range of image rows \inplace. - CImg& rows(const int y0, const int y1) { - return get_rows(y0,y1).move_to(*this); - } - - //! Return specified image slice. - /** - \param z0 Image slice. - **/ - CImg get_slice(const int z0) const { - return get_slices(z0,z0); - } - - //! Return specified image slice \inplace. - CImg& slice(const int z0) { - return slices(z0,z0); - } - - //! Return specified range of image slices. - /** - \param z0 Starting image slice. - \param z1 Ending image slice. - **/ - CImg get_slices(const int z0, const int z1) const { - return get_crop(0,0,z0,0,width()-1,height()-1,z1,spectrum()-1); - } - - //! Return specified range of image slices \inplace. - CImg& slices(const int z0, const int z1) { - return get_slices(z0,z1).move_to(*this); - } - - //! Return specified image channel. - /** - \param c0 Image channel. - **/ - CImg get_channel(const int c0) const { - return get_channels(c0,c0); - } - - //! Return specified image channel \inplace. - CImg& channel(const int c0) { - return channels(c0,c0); - } - - //! Return specified range of image channels. - /** - \param c0 Starting image channel. - \param c1 Ending image channel. - **/ - CImg get_channels(const int c0, const int c1) const { - return get_crop(0,0,0,c0,width()-1,height()-1,depth()-1,c1); - } - - //! Return specified range of image channels \inplace. - CImg& channels(const int c0, const int c1) { - return get_channels(c0,c1).move_to(*this); - } - - //! Return stream line of a 2d or 3d vector field. - CImg get_streamline(const float x, const float y, const float z, - const float L=256, const float dl=0.1f, - const unsigned int interpolation_type=2, const bool is_backward_tracking=false, - const bool is_oriented_only=false) const { - if (_spectrum!=2 && _spectrum!=3) - throw CImgInstanceException(_cimg_instance - "streamline(): Instance is not a 2d or 3d vector field.", - cimg_instance); - if (_spectrum==2) { - if (is_oriented_only) { - typename CImg::_functor4d_streamline2d_oriented func(*this); - return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true, - 0,0,0,_width-1.0f,_height-1.0f,0.0f); - } else { - typename CImg::_functor4d_streamline2d_directed func(*this); - return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false, - 0,0,0,_width-1.0f,_height-1.0f,0.0f); - } - } - if (is_oriented_only) { - typename CImg::_functor4d_streamline3d_oriented func(*this); - return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true, - 0,0,0,_width-1.0f,_height-1.0f,_depth-1.0f); - } - typename CImg::_functor4d_streamline3d_directed func(*this); - return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false, - 0,0,0,_width-1.0f,_height-1.0f,_depth-1.0f); - } - - //! Return stream line of a 3d vector field. - /** - \param func Vector field function. - \param x X-coordinate of the starting point of the streamline. - \param y Y-coordinate of the starting point of the streamline. - \param z Z-coordinate of the starting point of the streamline. - \param L Streamline length. - \param dl Streamline length increment. - \param interpolation_type Type of interpolation. - Can be { 0=nearest int | 1=linear | 2=2nd-order RK | 3=4th-order RK. }. - \param is_backward_tracking Tells if the streamline is estimated forward or backward. - \param is_oriented_only Tells if the direction of the vectors must be ignored. - \param x0 X-coordinate of the first bounding-box vertex. - \param y0 Y-coordinate of the first bounding-box vertex. - \param z0 Z-coordinate of the first bounding-box vertex. - \param x1 X-coordinate of the second bounding-box vertex. - \param y1 Y-coordinate of the second bounding-box vertex. - \param z1 Z-coordinate of the second bounding-box vertex. - **/ - template - static CImg streamline(const tfunc& func, - const float x, const float y, const float z, - const float L=256, const float dl=0.1f, - const unsigned int interpolation_type=2, const bool is_backward_tracking=false, - const bool is_oriented_only=false, - const float x0=0, const float y0=0, const float z0=0, - const float x1=0, const float y1=0, const float z1=0) { - if (dl<=0) - throw CImgArgumentException("CImg<%s>::streamline(): Invalid specified integration length %g " - "(should be >0).", - pixel_type(), - dl); - - const bool is_bounded = (x0!=x1 || y0!=y1 || z0!=z1); - if (L<=0 || (is_bounded && (xx1 || yy1 || zz1))) return CImg(); - const unsigned int size_L = (unsigned int)cimg::round(L/dl+1); - CImg coordinates(size_L,3); - const float dl2 = dl/2; - float - *ptr_x = coordinates.data(0,0), - *ptr_y = coordinates.data(0,1), - *ptr_z = coordinates.data(0,2), - pu = (float)(dl*func(x,y,z,0)), - pv = (float)(dl*func(x,y,z,1)), - pw = (float)(dl*func(x,y,z,2)), - X = x, Y = y, Z = z; - - switch (interpolation_type) { - case 0 : { // Nearest integer interpolation. - cimg_forX(coordinates,l) { - *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; - const int - xi = (int)(X>0?X+0.5f:X-0.5f), - yi = (int)(Y>0?Y+0.5f:Y-0.5f), - zi = (int)(Z>0?Z+0.5f:Z-0.5f); - float - u = (float)(dl*func((float)xi,(float)yi,(float)zi,0)), - v = (float)(dl*func((float)xi,(float)yi,(float)zi,1)), - w = (float)(dl*func((float)xi,(float)yi,(float)zi,2)); - if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; } - if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } - if (is_bounded && (Xx1 || Yy1 || Zz1)) break; - } - } break; - case 1 : { // First-order interpolation. - cimg_forX(coordinates,l) { - *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; - float - u = (float)(dl*func(X,Y,Z,0)), - v = (float)(dl*func(X,Y,Z,1)), - w = (float)(dl*func(X,Y,Z,2)); - if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; } - if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } - if (is_bounded && (Xx1 || Yy1 || Zz1)) break; - } - } break; - case 2 : { // Second order interpolation. - cimg_forX(coordinates,l) { - *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; - float - u0 = (float)(dl2*func(X,Y,Z,0)), - v0 = (float)(dl2*func(X,Y,Z,1)), - w0 = (float)(dl2*func(X,Y,Z,2)); - if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; } - float - u = (float)(dl*func(X+u0,Y+v0,Z+w0,0)), - v = (float)(dl*func(X+u0,Y+v0,Z+w0,1)), - w = (float)(dl*func(X+u0,Y+v0,Z+w0,2)); - if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; } - if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } - if (is_bounded && (Xx1 || Yy1 || Zz1)) break; - } - } break; - default : { // Fourth order interpolation. - cimg_forX(coordinates,x) { - *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z; - float - u0 = (float)(dl2*func(X,Y,Z,0)), - v0 = (float)(dl2*func(X,Y,Z,1)), - w0 = (float)(dl2*func(X,Y,Z,2)); - if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; } - float - u1 = (float)(dl2*func(X+u0,Y+v0,Z+w0,0)), - v1 = (float)(dl2*func(X+u0,Y+v0,Z+w0,1)), - w1 = (float)(dl2*func(X+u0,Y+v0,Z+w0,2)); - if (is_oriented_only && u1*pu + v1*pv + w1*pw<0) { u1 = -u1; v1 = -v1; w1 = -w1; } - float - u2 = (float)(dl2*func(X+u1,Y+v1,Z+w1,0)), - v2 = (float)(dl2*func(X+u1,Y+v1,Z+w1,1)), - w2 = (float)(dl2*func(X+u1,Y+v1,Z+w1,2)); - if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u2 = -u2; v2 = -v2; w2 = -w2; } - float - u3 = (float)(dl2*func(X+u2,Y+v2,Z+w2,0)), - v3 = (float)(dl2*func(X+u2,Y+v2,Z+w2,1)), - w3 = (float)(dl2*func(X+u2,Y+v2,Z+w2,2)); - if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u3 = -u3; v3 = -v3; w3 = -w3; } - const float - u = (u0 + u3)/3 + (u1 + u2)/1.5f, - v = (v0 + v3)/3 + (v1 + v2)/1.5f, - w = (w0 + w3)/3 + (w1 + w2)/1.5f; - if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); } - if (is_bounded && (Xx1 || Yy1 || Zz1)) break; - } - } - } - if (ptr_x!=coordinates.data(0,1)) coordinates.resize((int)(ptr_x-coordinates.data()),3,1,1,0); - return coordinates; - } - - //! Return stream line of a 3d vector field \overloading. - static CImg streamline(const char *const expression, - const float x, const float y, const float z, - const float L=256, const float dl=0.1f, - const unsigned int interpolation_type=2, const bool is_backward_tracking=true, - const bool is_oriented_only=false, - const float x0=0, const float y0=0, const float z0=0, - const float x1=0, const float y1=0, const float z1=0) { - _functor4d_streamline_expr func(expression); - return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,is_oriented_only,x0,y0,z0,x1,y1,z1); - } - - struct _functor4d_streamline2d_directed { - const CImg& ref; - _functor4d_streamline2d_directed(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y, const float z, const unsigned int c) const { - return c<2?(float)ref._linear_atXY(x,y,(int)z,c):0; - } - }; - - struct _functor4d_streamline3d_directed { - const CImg& ref; - _functor4d_streamline3d_directed(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y, const float z, const unsigned int c) const { - return (float)ref._linear_atXYZ(x,y,z,c); - } - }; - - struct _functor4d_streamline2d_oriented { - const CImg& ref; - CImg *pI; - _functor4d_streamline2d_oriented(const CImg& pref):ref(pref),pI(0) { pI = new CImg(2,2,1,2); } - ~_functor4d_streamline2d_oriented() { delete pI; } - float operator()(const float x, const float y, const float z, const unsigned int c) const { -#define _cimg_vecalign2d(i,j) if (I(i,j,0)*I(0,0,0)+I(i,j,1)*I(0,0,1)<0) { I(i,j,0) = -I(i,j,0); I(i,j,1) = -I(i,j,1); } - int - xi = (int)x - (x>=0?0:1), nxi = xi + 1, - yi = (int)y - (y>=0?0:1), nyi = yi + 1, - zi = (int)z; - const float - dx = x - xi, - dy = y - yi; - if (c==0) { - CImg& I = *pI; - if (xi<0) xi = 0; if (nxi<0) nxi = 0; - if (xi>=ref.width()) xi = ref.width()-1; if (nxi>=ref.width()) nxi = ref.width()-1; - if (yi<0) yi = 0; if (nyi<0) nyi = 0; - if (yi>=ref.height()) yi = ref.height()-1; if (nyi>=ref.height()) nyi = ref.height()-1; - I(0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,1) = (float)ref(xi,yi,zi,1); - I(1,0,0) = (float)ref(nxi,yi,zi,0); I(1,0,1) = (float)ref(nxi,yi,zi,1); - I(1,1,0) = (float)ref(nxi,nyi,zi,0); I(1,1,1) = (float)ref(nxi,nyi,zi,1); - I(0,1,0) = (float)ref(xi,nyi,zi,0); I(0,1,1) = (float)ref(xi,nyi,zi,1); - _cimg_vecalign2d(1,0); _cimg_vecalign2d(1,1); _cimg_vecalign2d(0,1); - } - return c<2?(float)pI->_linear_atXY(dx,dy,0,c):0; - } - }; - - struct _functor4d_streamline3d_oriented { - const CImg& ref; - CImg *pI; - _functor4d_streamline3d_oriented(const CImg& pref):ref(pref),pI(0) { pI = new CImg(2,2,2,3); } - ~_functor4d_streamline3d_oriented() { delete pI; } - float operator()(const float x, const float y, const float z, const unsigned int c) const { -#define _cimg_vecalign3d(i,j,k) if (I(i,j,k,0)*I(0,0,0,0)+I(i,j,k,1)*I(0,0,0,1)+I(i,j,k,2)*I(0,0,0,2)<0) { \ - I(i,j,k,0) = -I(i,j,k,0); I(i,j,k,1) = -I(i,j,k,1); I(i,j,k,2) = -I(i,j,k,2); } - int - xi = (int)x - (x>=0?0:1), nxi = xi + 1, - yi = (int)y - (y>=0?0:1), nyi = yi + 1, - zi = (int)z - (z>=0?0:1), nzi = zi + 1; - const float - dx = x - xi, - dy = y - yi, - dz = z - zi; - if (c==0) { - CImg& I = *pI; - if (xi<0) xi = 0; if (nxi<0) nxi = 0; - if (xi>=ref.width()) xi = ref.width()-1; if (nxi>=ref.width()) nxi = ref.width()-1; - if (yi<0) yi = 0; if (nyi<0) nyi = 0; - if (yi>=ref.height()) yi = ref.height()-1; if (nyi>=ref.height()) nyi = ref.height()-1; - if (zi<0) zi = 0; if (nzi<0) nzi = 0; - if (zi>=ref.depth()) zi = ref.depth()-1; if (nzi>=ref.depth()) nzi = ref.depth()-1; - I(0,0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,0,1) = (float)ref(xi,yi,zi,1); - I(0,0,0,2) = (float)ref(xi,yi,zi,2); I(1,0,0,0) = (float)ref(nxi,yi,zi,0); - I(1,0,0,1) = (float)ref(nxi,yi,zi,1); I(1,0,0,2) = (float)ref(nxi,yi,zi,2); - I(1,1,0,0) = (float)ref(nxi,nyi,zi,0); I(1,1,0,1) = (float)ref(nxi,nyi,zi,1); - I(1,1,0,2) = (float)ref(nxi,nyi,zi,2); I(0,1,0,0) = (float)ref(xi,nyi,zi,0); - I(0,1,0,1) = (float)ref(xi,nyi,zi,1); I(0,1,0,2) = (float)ref(xi,nyi,zi,2); - I(0,0,1,0) = (float)ref(xi,yi,nzi,0); I(0,0,1,1) = (float)ref(xi,yi,nzi,1); - I(0,0,1,2) = (float)ref(xi,yi,nzi,2); I(1,0,1,0) = (float)ref(nxi,yi,nzi,0); - I(1,0,1,1) = (float)ref(nxi,yi,nzi,1); I(1,0,1,2) = (float)ref(nxi,yi,nzi,2); - I(1,1,1,0) = (float)ref(nxi,nyi,nzi,0); I(1,1,1,1) = (float)ref(nxi,nyi,nzi,1); - I(1,1,1,2) = (float)ref(nxi,nyi,nzi,2); I(0,1,1,0) = (float)ref(xi,nyi,nzi,0); - I(0,1,1,1) = (float)ref(xi,nyi,nzi,1); I(0,1,1,2) = (float)ref(xi,nyi,nzi,2); - _cimg_vecalign3d(1,0,0); _cimg_vecalign3d(1,1,0); _cimg_vecalign3d(0,1,0); - _cimg_vecalign3d(0,0,1); _cimg_vecalign3d(1,0,1); _cimg_vecalign3d(1,1,1); _cimg_vecalign3d(0,1,1); - } - return (float)pI->_linear_atXYZ(dx,dy,dz,c); - } - }; - - struct _functor4d_streamline_expr { - _cimg_math_parser *mp; - ~_functor4d_streamline_expr() { delete mp; } - _functor4d_streamline_expr(const char *const expr):mp(0) { - mp = new _cimg_math_parser(CImg::empty(),expr,"streamline"); - } - float operator()(const float x, const float y, const float z, const unsigned int c) const { - return (float)(*mp)(x,y,z,c); - } - }; - - //! Return a shared-memory image referencing a range of pixels of the image instance. - /** - \param x0 X-coordinate of the starting pixel. - \param x1 X-coordinate of the ending pixel. - \param y0 Y-coordinate. - \param z0 Z-coordinate. - \param c0 C-coordinate. - **/ - CImg get_shared_points(const unsigned int x0, const unsigned int x1, - const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) { - const unsigned int beg = (unsigned int)offset(x0,y0,z0,c0), end = offset(x1,y0,z0,c0); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).", - cimg_instance, - x0,x1,y0,z0,c0); - - return CImg(_data+beg,x1-x0+1,1,1,1,true); - } - - //! Return a shared-memory image referencing a range of pixels of the image instance \const. - const CImg get_shared_points(const unsigned int x0, const unsigned int x1, - const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) const { - const unsigned int beg = (unsigned int)offset(x0,y0,z0,c0), end = offset(x1,y0,z0,c0); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).", - cimg_instance, - x0,x1,y0,z0,c0); - - return CImg(_data+beg,x1-x0+1,1,1,1,true); - } - - //! Return a shared-memory image referencing a range of rows of the image instance. - /** - \param y0 Y-coordinate of the starting row. - \param y1 Y-coordinate of the ending row. - \param z0 Z-coordinate. - \param c0 C-coordinate. - **/ - CImg get_shared_rows(const unsigned int y0, const unsigned int y1, - const unsigned int z0=0, const unsigned int c0=0) { - const unsigned int beg = offset(0,y0,z0,c0), end = offset(0,y1,z0,c0); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_rows(): Invalid request of a shared-memory subset " - "(0->%u,%u->%u,%u,%u).", - cimg_instance, - _width-1,y0,y1,z0,c0); - - return CImg(_data+beg,_width,y1-y0+1,1,1,true); - } - - //! Return a shared-memory image referencing a range of rows of the image instance \const. - const CImg get_shared_rows(const unsigned int y0, const unsigned int y1, - const unsigned int z0=0, const unsigned int c0=0) const { - const unsigned int beg = offset(0,y0,z0,c0), end = offset(0,y1,z0,c0); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_rows(): Invalid request of a shared-memory subset " - "(0->%u,%u->%u,%u,%u).", - cimg_instance, - _width-1,y0,y1,z0,c0); - - return CImg(_data+beg,_width,y1-y0+1,1,1,true); - } - - //! Return a shared-memory image referencing one row of the image instance. - /** - \param y0 Y-coordinate. - \param z0 Z-coordinate. - \param c0 C-coordinate. - **/ - CImg get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) { - return get_shared_rows(y0,y0,z0,c0); - } - - //! Return a shared-memory image referencing one row of the image instance \const. - const CImg get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) const { - return get_shared_rows(y0,y0,z0,c0); - } - - //! Return a shared memory image referencing a range of slices of the image instance. - /** - \param z0 Z-coordinate of the starting slice. - \param z1 Z-coordinate of the ending slice. - \param c0 C-coordinate. - **/ - CImg get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) { - const unsigned int beg = offset(0,0,z0,c0), end = offset(0,0,z1,c0); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_slices(): Invalid request of a shared-memory subset " - "(0->%u,0->%u,%u->%u,%u).", - cimg_instance, - _width-1,_height-1,z0,z1,c0); - - return CImg(_data+beg,_width,_height,z1-z0+1,1,true); - } - - //! Return a shared memory image referencing a range of slices of the image instance \const. - const CImg get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) const { - const unsigned int beg = offset(0,0,z0,c0), end = offset(0,0,z1,c0); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_slices(): Invalid request of a shared-memory subset " - "(0->%u,0->%u,%u->%u,%u).", - cimg_instance, - _width-1,_height-1,z0,z1,c0); - - return CImg(_data+beg,_width,_height,z1-z0+1,1,true); - } - - //! Return a shared-memory image referencing one slice of the image instance. - /** - \param z0 Z-coordinate. - \param c0 C-coordinate. - **/ - CImg get_shared_slice(const unsigned int z0, const unsigned int c0=0) { - return get_shared_slices(z0,z0,c0); - } - - //! Return a shared-memory image referencing one slice of the image instance \const. - const CImg get_shared_slice(const unsigned int z0, const unsigned int c0=0) const { - return get_shared_slices(z0,z0,c0); - } - - //! Return a shared-memory image referencing a range of channels of the image instance. - /** - \param c0 C-coordinate of the starting channel. - \param c1 C-coordinate of the ending channel. - **/ - CImg get_shared_channels(const unsigned int c0, const unsigned int c1) { - const unsigned int beg = offset(0,0,0,c0), end = offset(0,0,0,c1); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_channels(): Invalid request of a shared-memory subset " - "(0->%u,0->%u,0->%u,%u->%u).", - cimg_instance, - _width-1,_height-1,_depth-1,c0,c1); - - return CImg(_data+beg,_width,_height,_depth,c1-c0+1,true); - } - - //! Return a shared-memory image referencing a range of channels of the image instance \const. - const CImg get_shared_channels(const unsigned int c0, const unsigned int c1) const { - const unsigned int beg = offset(0,0,0,c0), end = offset(0,0,0,c1); - if (beg>end || beg>=size() || end>=size()) - throw CImgArgumentException(_cimg_instance - "get_shared_channels(): Invalid request of a shared-memory subset " - "(0->%u,0->%u,0->%u,%u->%u).", - cimg_instance, - _width-1,_height-1,_depth-1,c0,c1); - - return CImg(_data+beg,_width,_height,_depth,c1-c0+1,true); - } - - //! Return a shared-memory image referencing one channel of the image instance. - /** - \param c0 C-coordinate. - **/ - CImg get_shared_channel(const unsigned int c0) { - return get_shared_channels(c0,c0); - } - - //! Return a shared-memory image referencing one channel of the image instance \const. - const CImg get_shared_channel(const unsigned int c0) const { - return get_shared_channels(c0,c0); - } - - //! Return a shared-memory version of the image instance. - CImg get_shared() { - return CImg(_data,_width,_height,_depth,_spectrum,true); - } - - //! Return a shared-memory version of the image instance \const. - const CImg get_shared() const { - return CImg(_data,_width,_height,_depth,_spectrum,true); - } - - //! Split image into a list along specified axis. - /** - \param axis Splitting axis. Can be { 'x' | 'y' | 'z' | 'c' }. - \param nb Number of splitted parts. - \note - - If \c nb==0, there are as much splitted parts as the image size along the specified axis. - - If \c nb<=0, instance image is splitted into blocs of -\c nb pixel wide. - - If \c nb>0, instance image is splitted into \c nb blocs. - **/ - CImgList get_split(const char axis, const int nb=0) const { - CImgList res; - if (is_empty()) return res; - const char _axis = cimg::uncase(axis); - - if (nb<=0) { // Split by bloc size. - const unsigned int dp = (unsigned int)(nb?-nb:1); - switch (_axis) { - case 'x': { - if (_width>dp) { - res.assign(_width/dp+(_width%dp?1:0),1,1); - const unsigned int pe = _width - dp; -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._width>=128 && _height*_depth*_spectrum>=128) -#endif - for (unsigned int p = 0; pdp) { - res.assign(_height/dp+(_height%dp?1:0),1,1); - const unsigned int pe = _height - dp; -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._width>=128 && _width*_depth*_spectrum>=128) -#endif - for (unsigned int p = 0; pdp) { - res.assign(_depth/dp+(_depth%dp?1:0),1,1); - const unsigned int pe = _depth - dp; -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._width>=128 && _width*_height*_spectrum>=128) -#endif - for (unsigned int p = 0; pdp) { - res.assign(_spectrum/dp+(_spectrum%dp?1:0),1,1); - const unsigned int pe = _spectrum - dp; -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._width>=128 && _width*_height*_depth>=128) -#endif - for (unsigned int p = 0; psiz) - throw CImgArgumentException(_cimg_instance - "get_split(): Instance cannot be split along %c-axis into %u blocs.", - cimg_instance, - axis,nb); - if (nb==1) res.assign(*this); - else { - int err = (int)siz; - unsigned int _p = 0; - switch (_axis) { - case 'x' : { - cimg_forX(*this,p) if ((err-=nb)<=0) { - get_crop(_p,0,0,0,p,_height-1,_depth-1,_spectrum-1).move_to(res); - err+=(int)siz; - _p=p+1; - } - } break; - case 'y' : { - cimg_forY(*this,p) if ((err-=nb)<=0) { - get_crop(0,_p,0,0,_width-1,p,_depth-1,_spectrum-1).move_to(res); - err+=(int)siz; - _p=p+1; - } - } break; - case 'z' : { - cimg_forZ(*this,p) if ((err-=nb)<=0) { - get_crop(0,0,_p,0,_width-1,_height-1,p,_spectrum-1).move_to(res); - err+=(int)siz; - _p=p+1; - } - } break; - default : { - cimg_forC(*this,p) if ((err-=nb)<=0) { - get_crop(0,0,0,_p,_width-1,_height-1,_depth-1,p).move_to(res); - err+=(int)siz; - _p=p+1; - } - } - } - } - } - return res; - } - - //! Split image into a list of one-column vectors, according to a specified splitting value. - /** - \param value Splitting value. - \param keep_values Tells if the splitting value must be kept in the splitted blocs. - \param is_shared Tells if the splitted blocs have shared memory buffers. - **/ - CImgList get_split(const T value, const bool keep_values, const bool is_shared) const { - CImgList res; - if (is_empty()) return res; - for (const T *ps = _data, *_ps = ps, *const pe = end(); ps(ps,1,siz,1,1,is_shared),~0U,is_shared); - ps = _ps; - while (_ps(ps,1,siz,1,1,is_shared),~0U,is_shared); - ps = _ps; - } - return res; - } - - //! Split image into a list of one-column vectors, according to a specified splitting value sequence. - /** - \param values Splitting value sequence. - \param keep_values Tells if the splitting sequence must be kept in the splitted blocs. - \param is_shared Tells if the splitted blocs have shared memory buffers. - **/ - template - CImgList get_split(const CImg& values, const bool keep_values, const bool is_shared) const { - CImgList res; - if (is_empty()) return res; - if (!values) return CImgList(*this); - if (values.size()==1) return get_split(*values,keep_values,is_shared); - const t *pve = values.end(); - for (const T *ps = _data, *_ps = ps, *const pe = end(); ps(ps,1,siz,1,1,is_shared),~0U,is_shared); // If match found. - ps = _ps; - - // Try to find non-match from current position. - do { - pv = values._data; - while (_ps(ps,1,siz,1,1,is_shared),~0U,is_shared); - ps = _ps; - } - return res; - } - - //! Split the image into a list of one-column vectors each having same values. - CImgList get_split(const bool is_shared) const { - CImgList res; - if (is_empty()) return res; - T *p0 = _data, current = *p0; - cimg_for(*this,p,T) if (*p!=current) { - res.insert(CImg(p0,1,p-p0,1,1,is_shared),~0U,is_shared); p0 = p; current = *p; - } - res.insert(CImg(p0,1,end()-p0,1,1,is_shared),~0U,is_shared); - return res; - } - - //! Append two images along specified axis. - /** - \param img Image to append with instance image. - \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Append alignment in \c [0,1]. - **/ - template - CImg& append(const CImg& img, const char axis='x', const float align=0) { - if (is_empty()) return assign(img,false); - if (!img) return *this; - return CImgList(*this,true).insert(img).get_append(axis,align).move_to(*this); - } - - //! Append two images along specified axis \specialization. - CImg& append(const CImg& img, const char axis='x', const float align=0) { - if (is_empty()) return assign(img,false); - if (!img) return *this; - return CImgList(*this,img,true).get_append(axis,align).move_to(*this); - } - - //! Append two images along specified axis \const. - template - CImg<_cimg_Tt> get_append(const CImg& img, const char axis='x', const float align=0) const { - if (is_empty()) return +img; - if (!img) return +*this; - return CImgList<_cimg_Tt>(*this,true).insert(img).get_append(axis,align); - } - - //! Append two images along specified axis \specialization. - CImg get_append(const CImg& img, const char axis='x', const float align=0) const { - if (is_empty()) return +img; - if (!img) return +*this; - return CImgList(*this,img,true).get_append(axis,align); - } - - //@} - //--------------------------------------- - // - //! \name Filtering / Transforms - //@{ - //--------------------------------------- - - //! Correlate image by a mask. - /** - \param mask = the correlation kernel. - \param boundary_conditions = the border condition type (0=zero, 1=dirichlet) - \param is_normalized = enable local normalization. - \note - - The correlation of the image instance \p *this by the mask \p mask is defined to be: - res(x,y,z) = sum_{i,j,k} (*this)(x+i,y+j,z+k)*mask(i,j,k). - **/ - template - CImg& correlate(const CImg& mask, const unsigned int boundary_conditions=1, const bool is_normalized=false) { - if (is_empty() || !mask) return *this; - return get_correlate(mask,boundary_conditions,is_normalized).move_to(*this); - } - - //! Correlate image by a mask \newinstance. - template - CImg<_cimg_Ttfloat> get_correlate(const CImg& mask, const unsigned int boundary_conditions=1, - const bool is_normalized=false) const { - if (is_empty() || !mask) return *this; - typedef _cimg_Ttfloat Ttfloat; - CImg res(_width,_height,_depth,cimg::max(_spectrum,mask._spectrum)); - if (boundary_conditions && mask._width==mask._height && - ((mask._depth==1 && mask._width<=5) || (mask._depth==mask._width && mask._width<=3))) { - // A special optimization is done for 2x2, 3x3, 4x4, 5x5, 2x2x2 and 3x3x3 mask (with boundary_conditions=1) - Ttfloat *ptrd = res._data; - switch (mask._depth) { - case 3 : { - T I[27] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_for3x3x3(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + - I[ 3]*I[ 3] + I[ 4]*I[ 4] + I[ 5]*I[ 5] + - I[ 6]*I[ 6] + I[ 7]*I[ 7] + I[ 8]*I[ 8] + - I[ 9]*I[ 9] + I[10]*I[10] + I[11]*I[11] + - I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + - I[15]*I[15] + I[16]*I[16] + I[17]*I[17] + - I[18]*I[18] + I[19]*I[19] + I[20]*I[20] + - I[21]*I[21] + I[22]*I[22] + I[23]*I[23] + - I[24]*I[24] + I[25]*I[25] + I[26]*I[26]); - *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + - I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + - I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + - I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + - I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + - I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + - I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + - I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + - I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26])/std::sqrt(N):0); - } - } else cimg_for3x3x3(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + - I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + - I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + - I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + - I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + - I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + - I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + - I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + - I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26]); - } - } break; - case 2 : { - T I[8] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_for2x2x2(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[0]*I[0] + I[1]*I[1] + - I[2]*I[2] + I[3]*I[3] + - I[4]*I[4] + I[5]*I[5] + - I[6]*I[6] + I[7]*I[7]); - *(ptrd++) = (Ttfloat)(N?(I[0]*_mask[0] + I[1]*_mask[1] + - I[2]*_mask[2] + I[3]*_mask[3] + - I[4]*_mask[4] + I[5]*_mask[5] + - I[6]*_mask[6] + I[7]*_mask[7])/std::sqrt(N):0); - } - } else cimg_for2x2x2(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[0]*_mask[0] + I[1]*_mask[1] + - I[2]*_mask[2] + I[3]*_mask[3] + - I[4]*_mask[4] + I[5]*_mask[5] + - I[6]*_mask[6] + I[7]*_mask[7]); - } - } break; - default : - case 1 : - switch (mask._width) { - case 6 : { - T I[36] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_forZ(_img,z) cimg_for6x6(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + I[ 3]*I[ 3] + I[ 4]*I[ 4] + I[ 5]*I[ 5] + - I[ 6]*I[ 6] + I[ 7]*I[ 7] + I[ 8]*I[ 8] + I[ 9]*I[ 9] + I[10]*I[10] + I[11]*I[11] + - I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + I[15]*I[15] + I[16]*I[16] + I[17]*I[17] + - I[18]*I[18] + I[19]*I[19] + I[20]*I[20] + I[21]*I[21] + I[22]*I[22] + I[23]*I[23] + - I[24]*I[24] + I[25]*I[25] + I[26]*I[26] + I[27]*I[27] + I[28]*I[28] + I[29]*I[29] + - I[30]*I[30] + I[31]*I[31] + I[32]*I[32] + I[33]*I[33] + I[34]*I[34] + I[35]*I[35]); - *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + - I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + - I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + - I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + - I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26] + I[27]*_mask[27] + I[28]*_mask[28] + I[29]*_mask[29] + - I[30]*_mask[30] + I[31]*_mask[31] + I[32]*_mask[32] + I[33]*_mask[33] + I[34]*_mask[34] + I[35]*_mask[35])/std::sqrt(N):0); - } - } else cimg_forZ(_img,z) cimg_for6x6(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + - I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + - I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + - I[18]*_mask[18] + I[19]*_mask[19] + I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + - I[24]*_mask[24] + I[25]*_mask[25] + I[26]*_mask[26] + I[27]*_mask[27] + I[28]*_mask[28] + I[29]*_mask[29] + - I[30]*_mask[30] + I[31]*_mask[31] + I[32]*_mask[32] + I[33]*_mask[33] + I[34]*_mask[34] + I[35]*_mask[35]); - } - } break; - case 5 : { - T I[25] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_forZ(_img,z) cimg_for5x5(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + I[ 3]*I[ 3] + I[ 4]*I[ 4] + - I[ 5]*I[ 5] + I[ 6]*I[ 6] + I[ 7]*I[ 7] + I[ 8]*I[ 8] + I[ 9]*I[ 9] + - I[10]*I[10] + I[11]*I[11] + I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + - I[15]*I[15] + I[16]*I[16] + I[17]*I[17] + I[18]*I[18] + I[19]*I[19] + - I[20]*I[20] + I[21]*I[21] + I[22]*I[22] + I[23]*I[23] + I[24]*I[24]); - *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + - I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + - I[10]*_mask[10] + I[11]*_mask[11] + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + - I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + I[18]*_mask[18] + I[19]*_mask[19] + - I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + I[24]*_mask[24])/std::sqrt(N):0); - } - } else cimg_forZ(_img,z) cimg_for5x5(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + I[ 4]*_mask[ 4] + - I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + - I[10]*_mask[10] + I[11]*_mask[11] + I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + - I[15]*_mask[15] + I[16]*_mask[16] + I[17]*_mask[17] + I[18]*_mask[18] + I[19]*_mask[19] + - I[20]*_mask[20] + I[21]*_mask[21] + I[22]*_mask[22] + I[23]*_mask[23] + I[24]*_mask[24]); - } - } break; - case 4 : { - T I[16] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_forZ(_img,z) cimg_for4x4(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[ 0]*I[ 0] + I[ 1]*I[ 1] + I[ 2]*I[ 2] + I[ 3]*I[ 3] + - I[ 4]*I[ 4] + I[ 5]*I[ 5] + I[ 6]*I[ 6] + I[ 7]*I[ 7] + - I[ 8]*I[ 8] + I[ 9]*I[ 9] + I[10]*I[10] + I[11]*I[11] + - I[12]*I[12] + I[13]*I[13] + I[14]*I[14] + I[15]*I[15]); - *(ptrd++) = (Ttfloat)(N?(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + - I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + - I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + - I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15])/std::sqrt(N):0); - } - } else cimg_forZ(_img,z) cimg_for4x4(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[ 0]*_mask[ 0] + I[ 1]*_mask[ 1] + I[ 2]*_mask[ 2] + I[ 3]*_mask[ 3] + - I[ 4]*_mask[ 4] + I[ 5]*_mask[ 5] + I[ 6]*_mask[ 6] + I[ 7]*_mask[ 7] + - I[ 8]*_mask[ 8] + I[ 9]*_mask[ 9] + I[10]*_mask[10] + I[11]*_mask[11] + - I[12]*_mask[12] + I[13]*_mask[13] + I[14]*_mask[14] + I[15]*_mask[15]); - } - } break; - case 3 : { - T I[9] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_forZ(_img,z) cimg_for3x3(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[0]*I[0] + I[1]*I[1] + I[2]*I[2] + - I[3]*I[3] + I[4]*I[4] + I[5]*I[5] + - I[6]*I[6] + I[7]*I[7] + I[8]*I[8]); - *(ptrd++) = (Ttfloat)(N?(I[0]*_mask[0] + I[1]*_mask[1] + I[2]*_mask[2] + - I[3]*_mask[3] + I[4]*_mask[4] + I[5]*_mask[5] + - I[6]*_mask[6] + I[7]*_mask[7] + I[8]*_mask[8])/std::sqrt(N):0); - } - } else cimg_forZ(_img,z) cimg_for3x3(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[0]*_mask[0] + I[1]*_mask[1] + I[2]*_mask[2] + - I[3]*_mask[3] + I[4]*_mask[4] + I[5]*_mask[5] + - I[6]*_mask[6] + I[7]*_mask[7] + I[8]*_mask[8]); - } - } break; - case 2 : { - T I[4] = { 0 }; - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; - cimg_forZ(_img,z) cimg_for2x2(_img,x,y,z,0,I,T) { - const Ttfloat N = M*(I[0]*I[0] + I[1]*I[1] + - I[2]*I[2] + I[3]*I[3]); - *(ptrd++) = (Ttfloat)(N?(I[0]*_mask[0] + I[1]*_mask[1] + - I[2]*_mask[2] + I[3]*_mask[3])/std::sqrt(N):0); - } - } else cimg_forZ(_img,z) cimg_for2x2(_img,x,y,z,0,I,T) - *(ptrd++) = (Ttfloat)(I[0]*_mask[0] + I[1]*_mask[1] + - I[2]*_mask[2] + I[3]*_mask[3]); - } - } break; - case 1 : - if (is_normalized) res.fill(1); - else cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - res.get_shared_channel(c).assign(_img)*=_mask[0]; - } - break; - } - } - } else { // Generic version for other masks and borders conditions. - const int - mx2 = mask.width()/2, my2 = mask.height()/2, mz2 = mask.depth()/2, - mx1 = mx2 - 1 + (mask.width()%2), my1 = my2 - 1 + (mask.height()%2), mz1 = mz2 - 1 + (mask.depth()%2), - mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2; -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._spectrum>=2) -#endif - cimg_forC(res,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { // Normalized correlation. - const Ttfloat _M = (Ttfloat)_mask.magnitude(2), M = _M*_M; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) -#endif - for (int z = mz1; z=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Ttfloat val = 0, N = 0; - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const Ttfloat _val = (Ttfloat)_img._atXYZ(x+xm,y+ym,z+zm); - val+=_val*_mask(mx1+xm,my1+ym,mz1+zm); - N+=_val*_val; - } - N*=M; - res(x,y,z,c) = (Ttfloat)(N?val/std::sqrt(N):0); - } - else -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Ttfloat val = 0, N = 0; - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const Ttfloat _val = (Ttfloat)_img.atXYZ(x+xm,y+ym,z+zm,0,0); - val+=_val*_mask(mx1+xm,my1+ym,mz1+zm); - N+=_val*_val; - } - N*=M; - res(x,y,z,c) = (Ttfloat)(N?val/std::sqrt(N):0); - } - } else { // Classical correlation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) -#endif - for (int z = mz1; z=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Ttfloat val = 0; - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) - val+=_img._atXYZ(x+xm,y+ym,z+zm)*_mask(mx1+xm,my1+ym,mz1+zm); - res(x,y,z,c) = (Ttfloat)val; - } - else -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Ttfloat val = 0; - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) - val+=_img.atXYZ(x+xm,y+ym,z+zm,0,0)*_mask(mx1+xm,my1+ym,mz1+zm); - res(x,y,z,c) = (Ttfloat)val; - } - } - } - } - return res; - } - - //! Convolve image by a mask. - /** - \param mask = the correlation kernel. - \param boundary_conditions = the border condition type (0=zero, 1=dirichlet) - \param is_normalized = enable local normalization. - \note - - The result \p res of the convolution of an image \p img by a mask \p mask is defined to be: - res(x,y,z) = sum_{i,j,k} img(x-i,y-j,z-k)*mask(i,j,k) - **/ - template - CImg& convolve(const CImg& mask, const unsigned int boundary_conditions=1, const bool is_normalized=false) { - if (is_empty() || !mask) return *this; - return get_convolve(mask,boundary_conditions,is_normalized).move_to(*this); - } - - //! Convolve image by a mask \newinstance. - template - CImg<_cimg_Ttfloat> get_convolve(const CImg& mask, const unsigned int boundary_conditions=1, - const bool is_normalized=false) const { - if (is_empty() || !mask) return *this; - return get_correlate(CImg(mask._data,mask.size(),1,1,1,true).get_mirror('x'). - resize(mask,-1),boundary_conditions,is_normalized); - } - - //! Erode image by a structuring element. - /** - \param mask Structuring element. - \param boundary_conditions Boundary conditions. - \param is_normalized Tells if the erosion is locally normalized. - **/ - template - CImg& erode(const CImg& mask, const unsigned int boundary_conditions=1, - const bool is_normalized=false) { - if (is_empty() || !mask) return *this; - return get_erode(mask,boundary_conditions,is_normalized).move_to(*this); - } - - //! Erode image by a structuring element \newinstance. - template - CImg<_cimg_Tt> get_erode(const CImg& mask, const unsigned int boundary_conditions=1, - const bool is_normalized=false) const { - if (is_empty() || !mask) return *this; - typedef _cimg_Tt Tt; - CImg res(_width,_height,_depth,cimg::max(_spectrum,mask._spectrum)); - const int - mx2 = mask.width()/2, my2 = mask.height()/2, mz2 = mask.depth()/2, - mx1 = mx2 - 1 + (mask.width()%2), my1 = my2 - 1 + (mask.height()%2), mz1 = mz2 - 1 + (mask.depth()%2), - mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { // Normalized erosion. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) -#endif - for (int z = mz1; z::max(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const t mval = _mask(mx1+xm,my1+ym,mz1+zm); - const Tt cval = (Tt)(_img(x+xm,y+ym,z+zm) + mval); - if (mval && cval=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt min_val = cimg::type::max(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const t mval = _mask(mx1+xm,my1+ym,mz1+zm); - const Tt cval = (Tt)(_img._atXYZ(x+xm,y+ym,z+zm) + mval); - if (mval && cval=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt min_val = cimg::type::max(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const t mval = _mask(mx1+xm,my1+ym,mz1+zm); - const Tt cval = (Tt)(_img.atXYZ(x+xm,y+ym,z+zm,0,0) + mval); - if (mval && cval=32768) -#endif - for (int z = mz1; z::max(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const Tt cval = (Tt)_img(x+xm,y+ym,z+zm); - if (_mask(mx1+xm,my1+ym,mz1+zm) && cval=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt min_val = cimg::type::max(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const T cval = (Tt)_img._atXYZ(x+xm,y+ym,z+zm); - if (_mask(mx1+xm,my1+ym,mz1+zm) && cval=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt min_val = cimg::type::max(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const T cval = (Tt)_img.atXYZ(x+xm,y+ym,z+zm,0,0); - if (_mask(mx1+xm,my1+ym,mz1+zm) && cval& erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) { - if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this; - if (sx>1 && _width>1) { // Along X-axis. - const int L = width(), off = 1, s = (int)sx, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, s2 = _s2>L?L:_s2; - CImg buf(L); -#ifdef cimg_use_opemp -#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) -#endif - cimg_forYZC(*this,y,z,c) { - T *const ptrdb = buf._data, *ptrd = buf._data, *const ptrde = buf._data + L - 1; - const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; - T cur = *ptrs; ptrs+=off; bool is_first = true; - for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { - const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }} - *(ptrd++) = cur; - if (ptrs>=ptrse) { - T *pd = data(0,y,z,c); cur = cimg::min(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } - } else { - for (int p = s1; p>0 && ptrd<=ptrde; --p) { - const T val = *ptrs; if (ptrs0; --p) { - const T val = *ptrs; ptrs+=off; - if (is_first) { - const T *nptrs = ptrs - off; cur = val; - for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval0 && ptrs>=ptrsb; --p) { - const T val = *ptrs; ptrs-=off; if (val0 && ptrd>=ptrdb; --p) { - const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val1 && _height>1) { // Along Y-axis. - const int L = height(), off = width(), s = (int)sy, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, - s2 = _s2>L?L:_s2; - CImg buf(L); -#ifdef cimg_use_opemp -#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) -#endif - cimg_forXZC(*this,x,z,c) { - T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; - const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; - T cur = *ptrs; ptrs+=off; bool is_first = true; - for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { - const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; } - } - *(ptrd++) = cur; - if (ptrs>=ptrse) { - T *pd = data(x,0,z,c); cur = cimg::min(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } - } else { - for (int p = s1; p>0 && ptrd<=ptrde; --p) { - const T val = *ptrs; if (ptrs0; --p) { - const T val = *ptrs; ptrs+=off; - if (is_first) { - const T *nptrs = ptrs - off; cur = val; - for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval0 && ptrs>=ptrsb; --p) { - const T val = *ptrs; ptrs-=off; if (val0 && ptrd>=ptrdb; --p) { - const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val1 && _depth>1) { // Along Z-axis. - const int L = depth(), off = width()*height(), s = (int)sz, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, - s2 = _s2>L?L:_s2; - CImg buf(L); -#ifdef cimg_use_opemp -#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) -#endif - cimg_forXYC(*this,x,y,c) { - T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; - const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; - T cur = *ptrs; ptrs+=off; bool is_first = true; - for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { - const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; } - } - *(ptrd++) = cur; - if (ptrs>=ptrse) { - T *pd = data(x,y,0,c); cur = cimg::min(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } - } else { - for (int p = s1; p>0 && ptrd<=ptrde; --p) { - const T val = *ptrs; if (ptrs0; --p) { - const T val = *ptrs; ptrs+=off; - if (is_first) { - const T *nptrs = ptrs - off; cur = val; - for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval0 && ptrs>=ptrsb; --p) { - const T val = *ptrs; ptrs-=off; if (val0 && ptrd>=ptrdb; --p) { - const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val get_erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const { - return (+*this).erode(sx,sy,sz); - } - - //! Erode the image by a square structuring element of specified size. - /** - \param s Size of the structuring element. - **/ - CImg& erode(const unsigned int s) { - return erode(s,s,s); - } - - //! Erode the image by a square structuring element of specified size \newinstance. - CImg get_erode(const unsigned int s) const { - return (+*this).erode(s); - } - - //! Dilate image by a structuring element. - /** - \param mask Structuring element. - \param boundary_conditions Boundary conditions. - \param is_normalized Tells if the erosion is locally normalized. - **/ - template - CImg& dilate(const CImg& mask, const unsigned int boundary_conditions=1, - const bool is_normalized=false) { - if (is_empty() || !mask) return *this; - return get_dilate(mask,boundary_conditions,is_normalized).move_to(*this); - } - - //! Dilate image by a structuring element \newinstance. - template - CImg<_cimg_Tt> get_dilate(const CImg& mask, const unsigned int boundary_conditions=1, - const bool is_normalized=false) const { - if (is_empty() || !mask) return *this; - typedef _cimg_Tt Tt; - CImg res(_width,_height,_depth,_spectrum); - const int - mx2 = mask.width()/2, my2 = mask.height()/2, mz2 = mask.depth()/2, - mx1 = mx2 - 1 + (mask.width()%2), my1 = my2 - 1 + (mask.height()%2), mz1 = mz2 - 1 + (mask.depth()%2), - mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - const CImg _img = get_shared_channel(c%_spectrum); - const CImg _mask = mask.get_shared_channel(c%mask._spectrum); - if (is_normalized) { // Normalized dilation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width*_height*_depth>=32768) -#endif - for (int z = mz1; z::min(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const t mval = _mask(mx1+xm,my1+ym,mz1+zm); - const Tt cval = (Tt)(_img(x+xm,y+ym,z+zm) - mval); - if (mval && cval>max_val) max_val = cval; - } - res(x,y,z,c) = max_val; - } - if (boundary_conditions) -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt max_val = cimg::type::min(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const t mval = _mask(mx1+xm,my1+ym,mz1+zm); - const Tt cval = (Tt)(_img._atXYZ(x+xm,y+ym,z+zm) - mval); - if (mval && cval>max_val) max_val = cval; - } - res(x,y,z,c) = max_val; - } - else -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) -#endif - cimg_forYZ(*this,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt max_val = cimg::type::min(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const t mval = _mask(mx1+xm,my1+ym,mz1+zm); - const Tt cval = (Tt)(_img.atXYZ(x+xm,y+ym,z+zm,0,0) - mval); - if (mval && cval>max_val) max_val = cval; - } - res(x,y,z,c) = max_val; - } - } else { // Classical dilation. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth>=128) -#endif - for (int z = mz1; z::min(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const Tt cval = (Tt)_img(x+xm,y+ym,z+zm); - if (_mask(mx1+xm,my1+ym,mz1+zm) && cval>max_val) max_val = cval; - } - res(x,y,z,c) = max_val; - } - if (boundary_conditions) -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt max_val = cimg::type::min(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const T cval = (Tt)_img._atXYZ(x+xm,y+ym,z+zm); - if (_mask(mx1+xm,my1+ym,mz1+zm) && cval>max_val) max_val = cval; - } - res(x,y,z,c) = max_val; - } - else -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=128) -#endif - cimg_forYZ(res,y,z) - for (int x = 0; x=mye || z=mze)?++x:((x=mxe)?++x:(x=mxe))) { - Tt max_val = cimg::type::min(); - for (int zm = -mz1; zm<=mz2; ++zm) - for (int ym = -my1; ym<=my2; ++ym) - for (int xm = -mx1; xm<=mx2; ++xm) { - const T cval = (Tt)_img.atXYZ(x+xm,y+ym,z+zm,0,0); - if (_mask(mx1+xm,my1+ym,mz1+zm) && cval>max_val) max_val = cval; - } - res(x,y,z,c) = max_val; - } - } - } - return res; - } - - //! Dilate image by a rectangular structuring element of specified size. - /** - \param sx Width of the structuring element. - \param sy Height of the structuring element. - \param sz Depth of the structuring element. - **/ - CImg& dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) { - if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this; - if (sx>1 && _width>1) { // Along X-axis. - const int L = width(), off = 1, s = (int)sx, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, s2 = _s2>L?L:_s2; - CImg buf(L); -#ifdef cimg_use_opemp -#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) -#endif - cimg_forYZC(*this,y,z,c) { - T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; - const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; - T cur = *ptrs; ptrs+=off; bool is_first = true; - for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { - const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; } - } - *(ptrd++) = cur; - if (ptrs>=ptrse) { - T *pd = data(0,y,z,c); cur = cimg::max(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } - } else { - for (int p = s1; p>0 && ptrd<=ptrde; --p) { - const T val = *ptrs; if (ptrs=cur) { cur = val; is_first = false; } - *(ptrd++) = cur; - } - for (int p = L - s - 1; p>0; --p) { - const T val = *ptrs; ptrs+=off; - if (is_first) { - const T *nptrs = ptrs - off; cur = val; - for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; } - nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false; - } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; } - *(ptrd++) = cur; - } - ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off; - for (int p = s1; p>0 && ptrs>=ptrsb; --p) { - const T val = *ptrs; ptrs-=off; if (val>cur) cur = val; - } - *(ptrd--) = cur; - for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) { - const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur; - } - T *pd = data(0,y,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; } - } - } - } - - if (sy>1 && _height>1) { // Along Y-axis. - const int L = height(), off = width(), s = (int)sy, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, - s2 = _s2>L?L:_s2; - CImg buf(L); -#ifdef cimg_use_opemp -#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) -#endif - cimg_forXZC(*this,x,z,c) { - T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; - const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; - T cur = *ptrs; ptrs+=off; bool is_first = true; - for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { - const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; } - } - *(ptrd++) = cur; - if (ptrs>=ptrse) { - T *pd = data(x,0,z,c); cur = cimg::max(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } - } else { - for (int p = s1; p>0 && ptrd<=ptrde; --p) { - const T val = *ptrs; if (ptrs=cur) { cur = val; is_first = false; } - *(ptrd++) = cur; - } - for (int p = L - s - 1; p>0; --p) { - const T val = *ptrs; ptrs+=off; - if (is_first) { - const T *nptrs = ptrs - off; cur = val; - for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; } - nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false; - } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; } - *(ptrd++) = cur; - } - ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off; - for (int p = s1; p>0 && ptrs>=ptrsb; --p) { - const T val = *ptrs; ptrs-=off; if (val>cur) cur = val; - } - *(ptrd--) = cur; - for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) { - const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur; - } - T *pd = data(x,0,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; } - } - } - } - - if (sz>1 && _depth>1) { // Along Z-axis. - const int L = depth(), off = width()*height(), s = (int)sz, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, - s2 = _s2>L?L:_s2; - CImg buf(L); -#ifdef cimg_use_opemp -#pragma omp parallel for collapse(3) firstprivate(buf) if (size()>524288) -#endif - cimg_forXYC(*this,x,y,c) { - T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1; - const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off; - T cur = *ptrs; ptrs+=off; bool is_first = true; - for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) { - const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; } - } - *(ptrd++) = cur; - if (ptrs>=ptrse) { - T *pd = data(x,y,0,c); cur = cimg::max(cur,*ptrse); cimg_forX(buf,x) { *pd = cur; pd+=off; } - } else { - for (int p = s1; p>0 && ptrd<=ptrde; --p) { - const T val = *ptrs; if (ptrs=cur) { cur = val; is_first = false; } - *(ptrd++) = cur; - } - for (int p = L - s - 1; p>0; --p) { - const T val = *ptrs; ptrs+=off; - if (is_first) { - const T *nptrs = ptrs - off; cur = val; - for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; } - nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false; - } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; } - *(ptrd++) = cur; - } - ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off; - for (int p = s1; p>0 && ptrs>=ptrsb; --p) { - const T val = *ptrs; ptrs-=off; if (val>cur) cur = val; - } - *(ptrd--) = cur; - for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) { - const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur; - } - T *pd = data(x,y,0,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; } - } - } - } - return *this; - } - - //! Dilate image by a rectangular structuring element of specified size \newinstance. - CImg get_dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const { - return (+*this).dilate(sx,sy,sz); - } - - //! Dilate image by a square structuring element of specified size. - /** - \param s Size of the structuring element. - **/ - CImg& dilate(const unsigned int s) { - return dilate(s,s,s); - } - - //! Dilate image by a square structuring element of specified size \newinstance. - CImg get_dilate(const unsigned int s) const { - return (+*this).dilate(s); - } - - //! Compute watershed transform. - /** - \param priority Priority map. - \param fill_lines Tells if watershed lines must be filled or not. - \note Non-zero values of the instance instance are propagated to zero-valued ones according to - specified the priority map. - **/ - template - CImg& watershed(const CImg& priority, const bool fill_lines=true) { - if (is_empty()) return *this; - if (!is_sameXYZ(priority)) - throw CImgArgumentException(_cimg_instance - "watershed(): image instance and specified priority (%u,%u,%u,%u,%p) " - "have different dimensions.", - cimg_instance, - priority._width,priority._height,priority._depth,priority._spectrum,priority._data); - if (_spectrum!=1) { - cimg_forC(*this,c) - get_shared_channel(c).watershed(priority.get_shared_channel(c%priority._spectrum),fill_lines); - return *this; - } - - CImg is_queued(_width,_height,_depth,1,0); - CImg::type> Q; - unsigned int sizeQ = 0; - - // Find seed points and insert them in priority queue. - const T *ptrs = _data; - cimg_forXYZ(*this,x,y,z) if (*(ptrs++)) { - if (x-1>=0 && !(*this)(x-1,y,z)) Q._priority_queue_insert(is_queued,sizeQ,priority(x-1,y,z),x-1,y,z); - if (x+1=0 && !(*this)(x,y-1,z)) Q._priority_queue_insert(is_queued,sizeQ,priority(x,y-1,z),x,y-1,z); - if (y+1=0 && !(*this)(x,y,z-1)) Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z-1),x,y,z-1); - if (z+1=0) { - if ((*this)(x-1,y,z)) { - if (!label) label = (unsigned int)(*this)(x-1,y,z); - else if (label!=(*this)(x-1,y,z)) is_same_label = false; - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x-1,y,z),x-1,y,z); - } - if (x+1=0) { - if ((*this)(x,y-1,z)) { - if (!label) label = (unsigned int)(*this)(x,y-1,z); - else if (label!=(*this)(x,y-1,z)) is_same_label = false; - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y-1,z),x,y-1,z); - } - if (y+1=0) { - if ((*this)(x,y,z-1)) { - if (!label) label = (unsigned int)(*this)(x,y,z-1); - else if (label!=(*this)(x,y,z-1)) is_same_label = false; - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z-1),x,y,z-1); - } - if (z+1=0 && (*this)(x-1,y,z)) || (x+1=0 && (*this)(x,y-1,z)) || (y+1=0 && (*this)(x,y,z-1)) || (z+1>depth() && (*this)(x,y,z+1)))) - Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z),x,y,z); - - // Start line filling process. - while (sizeQ) { - const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3); - Q._priority_queue_remove(sizeQ); - t pmax = cimg::type::min(); - int xmax = 0, ymax = 0, zmax = 0; - if (x-1>=0) { - if ((*this)(x-1,y,z)) { - if (priority(x-1,y,z)>pmax) { pmax = priority(x-1,y,z); xmax = x-1; ymax = y; zmax = z; } - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x-1,y,z),x-1,y,z); - } - if (x+1pmax) { pmax = priority(x+1,y,z); xmax = x+1; ymax = y; zmax = z; } - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x+1,y,z),x+1,y,z); - } - if (y-1>=0) { - if ((*this)(x,y-1,z)) { - if (priority(x,y-1,z)>pmax) { pmax = priority(x,y-1,z); xmax = x; ymax = y-1; zmax = z; } - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y-1,z),x,y-1,z); - } - if (y+1pmax) { pmax = priority(x,y+1,z); xmax = x; ymax = y+1; zmax = z; } - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y+1,z),x,y+1,z); - } - if (z-1>=0) { - if ((*this)(x,y,z-1)) { - if (priority(x,y,z-1)>pmax) { pmax = priority(x,y,z-1); xmax = x; ymax = y; zmax = z-1; } - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z-1),x,y,z-1); - } - if (z+1pmax) { pmax = priority(x,y,z+1); xmax = x; ymax = y; zmax = z+1; } - } else Q._priority_queue_insert(is_queued,sizeQ,priority(x,y,z+1),x,y,z+1); - } - (*this)(x,y,z) = (*this)(xmax,ymax,zmax); - } - } - return *this; - } - - //! Compute watershed transform \newinstance. - template - CImg get_watershed(const CImg& priority, const bool fill_lines=true) const { - return (+*this).watershed(priority,fill_lines); - } - - // [internal] Insert/Remove items in priority queue, for watershed/distance transforms. - template - bool _priority_queue_insert(CImg& is_queued, unsigned int& siz, const t value, - const unsigned int x, const unsigned int y, const unsigned int z) { - if (is_queued(x,y,z)) return false; - is_queued(x,y,z) = true; - if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); } - (*this)(siz-1,0) = (T)value; (*this)(siz-1,1) = (T)x; (*this)(siz-1,2) = (T)y; (*this)(siz-1,3) = (T)z; - for (unsigned int pos = siz - 1, par = 0; pos && value>(*this)(par=(pos+1)/2-1,0); pos = par) { - cimg::swap((*this)(pos,0),(*this)(par,0)); cimg::swap((*this)(pos,1),(*this)(par,1)); - cimg::swap((*this)(pos,2),(*this)(par,2)); cimg::swap((*this)(pos,3),(*this)(par,3)); - } - return true; - } - - CImg& _priority_queue_remove(unsigned int& siz) { - (*this)(0,0) = (*this)(--siz,0); (*this)(0,1) = (*this)(siz,1); - (*this)(0,2) = (*this)(siz,2); (*this)(0,3) = (*this)(siz,3); - const float value = (*this)(0,0); - for (unsigned int pos = 0, left = 0, right = 0; - ((right=2*(pos+1),(left=right-1))(*this)(right,0)) { - cimg::swap((*this)(pos,0),(*this)(left,0)); cimg::swap((*this)(pos,1),(*this)(left,1)); - cimg::swap((*this)(pos,2),(*this)(left,2)); cimg::swap((*this)(pos,3),(*this)(left,3)); - pos = left; - } else { - cimg::swap((*this)(pos,0),(*this)(right,0)); cimg::swap((*this)(pos,1),(*this)(right,1)); - cimg::swap((*this)(pos,2),(*this)(right,2)); cimg::swap((*this)(pos,3),(*this)(right,3)); - pos = right; - } - } else { - cimg::swap((*this)(pos,0),(*this)(left,0)); cimg::swap((*this)(pos,1),(*this)(left,1)); - cimg::swap((*this)(pos,2),(*this)(left,2)); cimg::swap((*this)(pos,3),(*this)(left,3)); - pos = left; - } - } - return *this; - } - - //! Apply recursive Deriche filter. - /** - \param sigma Standard deviation of the filter. - \param order Order of the filter. Can be { 0=smooth-filter | 1=1st-derivative | 2=2nd-derivative }. - \param axis Axis along which the filter is computed. Can be { 'x' | 'y' | 'z' | 'c' }. - \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }. - **/ - CImg& deriche(const float sigma, const int order=0, const char axis='x', const bool boundary_conditions=true) { -#define _cimg_deriche_apply \ - CImg Y(N); \ - Tfloat *ptrY = Y._data, yb = 0, yp = 0; \ - T xp = (T)0; \ - if (boundary_conditions) { xp = *ptrX; yb = yp = (Tfloat)(coefp*xp); } \ - for (int m = 0; m=0; --n) { \ - const T xc = *(ptrX-=off); \ - const Tfloat yc = (Tfloat)(a2*xn + a3*xa - b1*yn - b2*ya); \ - xa = xn; xn = xc; ya = yn; yn = yc; \ - *ptrX = (T)(*(--ptrY)+yc); \ - } - const char naxis = cimg::uncase(axis); - const float nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:naxis=='y'?_height:naxis=='z'?_depth:_spectrum)/100; - if (is_empty() || (nsigma<0.1f && !order)) return *this; - const float - nnsigma = nsigma<0.1f?0.1f:nsigma, - alpha = 1.695f/nnsigma, - ema = (float)std::exp(-alpha), - ema2 = (float)std::exp(-2*alpha), - b1 = -2*ema, - b2 = ema2; - float a0 = 0, a1 = 0, a2 = 0, a3 = 0, coefp = 0, coefn = 0; - switch (order) { - case 0 : { - const float k = (1-ema)*(1-ema)/(1+2*alpha*ema-ema2); - a0 = k; - a1 = k*(alpha-1)*ema; - a2 = k*(alpha+1)*ema; - a3 = -k*ema2; - } break; - case 1 : { - const float k = -(1-ema)*(1-ema)*(1-ema)/(2*(ema+1)*ema); - a0 = a3 = 0; - a1 = k*ema; - a2 = -a1; - } break; - case 2 : { - const float - ea = (float)std::exp(-alpha), - k = -(ema2-1)/(2*alpha*ema), - kn = (-2*(-1+3*ea-3*ea*ea+ea*ea*ea)/(3*ea+1+3*ea*ea+ea*ea*ea)); - a0 = kn; - a1 = -kn*(1+k*alpha)*ema; - a2 = kn*(1-k*alpha)*ema; - a3 = -kn*ema2; - } break; - default : - throw CImgArgumentException(_cimg_instance - "deriche(): Invalid specified filter order %u " - "(should be { 0=smoothing | 1=1st-derivative | 2=2nd-derivative }).", - cimg_instance, - order); - } - coefp = (a0+a1)/(1+b1+b2); - coefn = (a2+a3)/(1+b1+b2); - switch (naxis) { - case 'x' : { - const int N = _width; - const unsigned long off = 1U; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forYZC(*this,y,z,c) { T *ptrX = data(0,y,z,c); _cimg_deriche_apply; } - } break; - case 'y' : { - const int N = _height; - const unsigned long off = (unsigned long)_width; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forXZC(*this,x,z,c) { T *ptrX = data(x,0,z,c); _cimg_deriche_apply; } - } break; - case 'z' : { - const int N = _depth; - const unsigned long off = (unsigned long)_width*_height; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forXYC(*this,x,y,c) { T *ptrX = data(x,y,0,c); _cimg_deriche_apply; } - } break; - default : { - const int N = _spectrum; - const unsigned long off = (unsigned long)_width*_height*_depth; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forXYZ(*this,x,y,z) { T *ptrX = data(x,y,z,0); _cimg_deriche_apply; } - } - } - return *this; - } - - //! Apply recursive Deriche filter \newinstance. - CImg get_deriche(const float sigma, const int order=0, const char axis='x', - const bool boundary_conditions=true) const { - return CImg(*this,false).deriche(sigma,order,axis,boundary_conditions); - } - - // [internal] Apply a recursive filter (used by CImg::vanvliet()). - /** - \param ptr the pointer of the data - \param filter the coefficient of the filter in the following order [n,n-1,n-2,n-3]. - \param N size of the data - \param off the offset between two data point - \param order the order of the filter 0 (smoothing), 1st derivtive, 2nd derivative, 3rd derivative - \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }. - \note dirichlet boundary conditions have a strange behavior. And - boundary condition should be corrected using Bill Triggs method (IEEE trans on Sig Proc 2005). - **/ - template - static void _cimg_recursive_apply(T *data, const Tfloat filter[], const int N, const unsigned long off, - const int order, const bool boundary_conditions) { - Tfloat val[K]; // res[n,n-1,n-2,n-3,..] or res[n,n+1,n+2,n+3,..] - switch (order) { - case 0 : { - for (int pass = 0; pass<2; ++pass) { - for (int k = 1; k0; --k) val[k] = val[k-1]; - } - if (!pass) data-=off; - } - } break; - case 1 : { - Tfloat x[3]; // [front,center,back] - for (int pass = 0; pass<2; ++pass) { - for (int k = 0; k<3; ++k) x[k] = (Tfloat)(boundary_conditions?*data:0); - for (int k = 0; k0; --k) x[k] = x[k-1]; - } else data-=off; - for (int k = K-1; k>0; --k) val[k] = val[k-1]; - } - *data = (T)0; - } - } break; - case 2: { - Tfloat x[3]; // [front,center,back] - for (int pass = 0; pass<2; ++pass) { - for (int k = 0; k<3; ++k) x[k] = (Tfloat)(boundary_conditions?*data:0); - for (int k = 0; k0; --k) x[k] = x[k-1]; - for (int k = K-1; k>0; --k) val[k] = val[k-1]; - } - *data = (T)0; - } - } break; - case 3: { - Tfloat x[3]; // [front,center,back] - for (int pass = 0; pass<2; ++pass) { - for (int k = 0; k<3; ++k) x[k] = (Tfloat)(boundary_conditions?*data:0); - for (int k = 0; k0; --k) x[k] = x[k-1]; - for (int k = K-1; k>0; --k) val[k] = val[k-1]; - } - *data = (T)0; - } - } break; - } - } - - //! Van Vliet recursive Gaussian filter. - /** - \param sigma standard deviation of the Gaussian filter - \param order the order of the filter 0,1,2,3 - \param axis Axis along which the filter is computed. Can be { 'x' | 'y' | 'z' | 'c' }. - \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }. - \note dirichlet boundary condition has a strange behavior - - Ian T. Young, Lucas J. van Vliet, Recursive implementation of the - Gaussian filter, Signal Processing, Volume 44, Issue 2, June 1995, - Pages 139-151, - **/ - CImg& vanvliet(const float sigma, const int order, const char axis='x', const bool boundary_conditions=true) { - if (is_empty()) return *this; - const char naxis = cimg::uncase(axis); - const float nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:naxis=='y'?_height:naxis=='z'?_depth:_spectrum)/100; - if (is_empty() || (nsigma<0.1f && !order)) return *this; - const Tfloat - nnsigma = nsigma<0.1f?0.1f:nsigma, - q = (Tfloat)(nnsigma<2.5?3.97156-4.14554*std::sqrt(1-0.2689*nnsigma):0.98711*nnsigma-0.96330), - b0 = 1.57825f + 2.44413f*q + 1.4281f*q*q + 0.422205f*q*q*q, - b1 = (2.44413f*q + 2.85619f*q*q + 1.26661f*q*q*q), - b2 = -(1.4281f*q*q + 1.26661f*q*q*q), - b3 = 0.422205f*q*q*q, - B = 1.f - (b1 + b2 + b3)/b0; - Tfloat filter[4]; - filter[0] = B; filter[1] = b1/b0; filter[2] = b2/b0; filter[3] = b3/b0; - - switch (naxis) { - case 'x' : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forYZC(*this,y,z,c) - _cimg_recursive_apply<4>(data(0,y,z,c),filter,_width,1U,order,boundary_conditions); - } break; - case 'y' : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forXZC(*this,x,z,c) - _cimg_recursive_apply<4>(data(x,0,z,c),filter,_height,(unsigned long)_width,order,boundary_conditions); - } break; - case 'z' : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forXYC(*this,x,y,c) - _cimg_recursive_apply<4>(data(x,y,0,c),filter,_depth,(unsigned long)(_width*_height), - order,boundary_conditions); - } break; - default : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=256 && _height*_depth*_spectrum>=16) -#endif - cimg_forXYZ(*this,x,y,z) - _cimg_recursive_apply<4>(data(x,y,z,0),filter,_spectrum,(unsigned long)(_width*_height*_depth), - order,boundary_conditions); - } - } - return *this; - } - - //! Blur image using Van Vliet recursive Gaussian filter. \newinstance. - CImg get_vanvliet(const float sigma, const int order, const char axis='x', - const bool boundary_conditions=true) const { - return CImg(*this,false).vanvliet(sigma,order,axis,boundary_conditions); - } - - //! Blur image. - /** - \param sigma_x Standard deviation of the blur, along the X-axis. - \param sigma_y Standard deviation of the blur, along the Y-axis. - \param sigma_z Standard deviation of the blur, along the Z-axis. - \param boundary_conditions Boundary conditions. Can be { false=dirichlet | true=neumann }. - \param is_gaussian Tells if the blur uses a gaussian (\c true) or quasi-gaussian (\c false) kernel. - \note - - The blur is computed as a 0-order Deriche filter. This is not a gaussian blur. - - This is a recursive algorithm, not depending on the values of the standard deviations. - \see deriche(), vanvliet(). - **/ - CImg& blur(const float sigma_x, const float sigma_y, const float sigma_z, - const bool boundary_conditions=true, const bool is_gaussian=false) { - if (!is_empty()) { - if (is_gaussian) { - if (_width>1) vanvliet(sigma_x,0,'x',boundary_conditions); - if (_height>1) vanvliet(sigma_y,0,'y',boundary_conditions); - if (_depth>1) vanvliet(sigma_z,0,'z',boundary_conditions); - } else { - if (_width>1) deriche(sigma_x,0,'x',boundary_conditions); - if (_height>1) deriche(sigma_y,0,'y',boundary_conditions); - if (_depth>1) deriche(sigma_z,0,'z',boundary_conditions); - } - } - return *this; - } - - //! Blur image \newinstance. - CImg get_blur(const float sigma_x, const float sigma_y, const float sigma_z, - const bool boundary_conditions=true, const bool is_gaussian=false) const { - return CImg(*this,false).blur(sigma_x,sigma_y,sigma_z,boundary_conditions,is_gaussian); - } - - //! Blur image isotropically. - /** - \param sigma Standard deviation of the blur. - \param boundary_conditions Boundary conditions. Can be { 0=dirichlet | 1=neumann }.a - \see deriche(), vanvliet(). - **/ - CImg& blur(const float sigma, const bool boundary_conditions=true, const bool is_gaussian=false) { - const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100; - return blur(nsigma,nsigma,nsigma,boundary_conditions,is_gaussian); - } - - //! Blur image isotropically \newinstance. - CImg get_blur(const float sigma, const bool boundary_conditions=true, const bool is_gaussian=false) const { - return CImg(*this,false).blur(sigma,boundary_conditions,is_gaussian); - } - - //! Blur image anisotropically, directed by a field of diffusion tensors. - /** - \param G Field of square roots of diffusion tensors/vectors used to drive the smoothing. - \param amplitude Amplitude of the smoothing. - \param dl Spatial discretization. - \param da Angular discretization. - \param gauss_prec Precision of the diffusion process. - \param interpolation_type Interpolation scheme. - Can be { 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }. - \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not. - **/ - template - CImg& blur_anisotropic(const CImg& G, - const float amplitude=60, const float dl=0.8f, const float da=30, - const float gauss_prec=2, const unsigned int interpolation_type=0, - const bool is_fast_approx=1) { - - // Check arguments and init variables - if (!is_sameXYZ(G) || (G._spectrum!=3 && G._spectrum!=6)) - throw CImgArgumentException(_cimg_instance - "blur_anisotropic(): Invalid specified diffusion tensor field (%u,%u,%u,%u,%p).", - cimg_instance, - G._width,G._height,G._depth,G._spectrum,G._data); - - if (is_empty() || amplitude<=0 || dl<0) return *this; - const bool is_3d = (G._spectrum==6); - T val_min, val_max = max_min(val_min); - - if (da<=0) { // Iterated oriented Laplacians - CImg velocity(_width,_height,_depth,_spectrum); - for (unsigned int iteration = 0; iteration<(unsigned int)amplitude; ++iteration) { - Tfloat *ptrd = velocity._data, veloc_max = 0; - if (is_3d) // 3d version - cimg_forC(*this,c) { - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat - ixx = Incc + Ipcc - 2*Iccc, - ixy = (Innc + Ippc - Inpc - Ipnc)/4, - ixz = (Incn + Ipcp - Incp - Ipcn)/4, - iyy = Icnc + Icpc - 2*Iccc, - iyz = (Icnn + Icpp - Icnp - Icpn)/4, - izz = Iccn + Iccp - 2*Iccc, - veloc = (Tfloat)(G(x,y,z,0)*ixx + 2*G(x,y,z,1)*ixy + 2*G(x,y,z,2)*ixz + - G(x,y,z,3)*iyy + 2*G(x,y,z,4)*iyz + G(x,y,z,5)*izz); - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } - } - else // 2d version - cimg_forZC(*this,z,c) { - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat - ixx = Inc + Ipc - 2*Icc, - ixy = (Inn + Ipp - Inp - Ipn)/4, - iyy = Icn + Icp - 2*Icc, - veloc = (Tfloat)(G(x,y,0,0)*ixx + 2*G(x,y,0,1)*ixy + G(x,y,0,2)*iyy); - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } - } - if (veloc_max>0) *this+=(velocity*=dl/veloc_max); - } - } else { // LIC-based smoothing. - const unsigned long whd = (unsigned long)_width*_height*_depth; - const float sqrt2amplitude = (float)std::sqrt(2*amplitude); - const int dx1 = width() - 1, dy1 = height() - 1, dz1 = depth() - 1; - CImg res(_width,_height,_depth,_spectrum,0), W(_width,_height,_depth,is_3d?4:3), val(_spectrum,1,1,1,0); - int N = 0; - if (is_3d) { // 3d version - for (float phi = (180%(int)da)/2.0f; phi<=180; phi+=da) { - const float phir = (float)(phi*cimg::PI/180), datmp = (float)(da/std::cos(phir)), - da2 = datmp<1?360.0f:datmp; - for (float theta = 0; theta<360; (theta+=da2),++N) { - const float - thetar = (float)(theta*cimg::PI/180), - vx = (float)(std::cos(thetar)*std::cos(phir)), - vy = (float)(std::sin(thetar)*std::cos(phir)), - vz = (float)std::sin(phir); - const t - *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2), - *pd = G.data(0,0,0,3), *pe = G.data(0,0,0,4), *pf = G.data(0,0,0,5); - Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2), *pd3 = W.data(0,0,0,3); - cimg_forXYZ(G,xg,yg,zg) { - const t a = *(pa++), b = *(pb++), c = *(pc++), d = *(pd++), e = *(pe++), f = *(pf++); - const float - u = (float)(a*vx + b*vy + c*vz), - v = (float)(b*vx + d*vy + e*vz), - w = (float)(c*vx + e*vy + f*vz), - n = (float)std::sqrt(1e-5+u*u+v*v+w*w), - dln = dl/n; - *(pd0++) = (Tfloat)(u*dln); - *(pd1++) = (Tfloat)(v*dln); - *(pd2++) = (Tfloat)(w*dln); - *(pd3++) = (Tfloat)n; - } - -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=256 && _height*_depth>=2) firstprivate(val) -#endif - cimg_forXYZ(*this,x,y,z) { - val.fill(0); - const float - n = (float)W(x,y,z,3), - fsigma = (float)(n*sqrt2amplitude), - fsigma2 = 2*fsigma*fsigma, - length = gauss_prec*fsigma; - float - S = 0, - X = (float)x, - Y = (float)y, - Z = (float)z; - switch (interpolation_type) { - case 0 : { // Nearest neighbor - for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) { - const int - cx = (int)(X+0.5f), - cy = (int)(Y+0.5f), - cz = (int)(Z+0.5f); - const float - u = (float)W(cx,cy,cz,0), - v = (float)W(cx,cy,cz,1), - w = (float)W(cx,cy,cz,2); - if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,cz,c); ++S; } - else { - const float coef = (float)std::exp(-l*l/fsigma2); - cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,cz,c)); - S+=coef; - } - X+=u; Y+=v; Z+=w; - } - } break; - case 1 : { // Linear interpolation - for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) { - const float - u = (float)(W._linear_atXYZ(X,Y,Z,0)), - v = (float)(W._linear_atXYZ(X,Y,Z,1)), - w = (float)(W._linear_atXYZ(X,Y,Z,2)); - if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; } - else { - const float coef = (float)std::exp(-l*l/fsigma2); - cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c)); - S+=coef; - } - X+=u; Y+=v; Z+=w; - } - } break; - default : { // 2nd order Runge Kutta - for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) { - const float - u0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,0)), - v0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,1)), - w0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,2)), - u = (float)(W._linear_atXYZ(X+u0,Y+v0,Z+w0,0)), - v = (float)(W._linear_atXYZ(X+u0,Y+v0,Z+w0,1)), - w = (float)(W._linear_atXYZ(X+u0,Y+v0,Z+w0,2)); - if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; } - else { - const float coef = (float)std::exp(-l*l/fsigma2); - cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c)); - S+=coef; - } - X+=u; Y+=v; Z+=w; - } - } break; - } - Tfloat *ptrd = res.data(x,y,z); - if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; } - else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,z,c)); ptrd+=whd; } - } - } - } - } else { // 2d LIC algorithm - for (float theta = (360%(int)da)/2.0f; theta<360; (theta+=da),++N) { - const float thetar = (float)(theta*cimg::PI/180), - vx = (float)(std::cos(thetar)), vy = (float)(std::sin(thetar)); - const t *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2); - Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2); - cimg_forXY(G,xg,yg) { - const t a = *(pa++), b = *(pb++), c = *(pc++); - const float - u = (float)(a*vx + b*vy), - v = (float)(b*vx + c*vy), - n = (float)std::sqrt(1e-5+u*u+v*v), - dln = dl/n; - *(pd0++) = (Tfloat)(u*dln); - *(pd1++) = (Tfloat)(v*dln); - *(pd2++) = (Tfloat)n; - } - -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width>=256 && _height>=2) firstprivate(val) -#endif - cimg_forXY(*this,x,y) { - val.fill(0); - const float - n = (float)W(x,y,0,2), - fsigma = (float)(n*sqrt2amplitude), - fsigma2 = 2*fsigma*fsigma, - length = gauss_prec*fsigma; - float - S = 0, - X = (float)x, - Y = (float)y; - switch (interpolation_type) { - case 0 : { // Nearest-neighbor - for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) { - const int - cx = (int)(X+0.5f), - cy = (int)(Y+0.5f); - const float - u = (float)W(cx,cy,0,0), - v = (float)W(cx,cy,0,1); - if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,0,c); ++S; } - else { - const float coef = (float)std::exp(-l*l/fsigma2); - cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,0,c)); - S+=coef; - } - X+=u; Y+=v; - } - } break; - case 1 : { // Linear interpolation - for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) { - const float - u = (float)(W._linear_atXY(X,Y,0,0)), - v = (float)(W._linear_atXY(X,Y,0,1)); - if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; } - else { - const float coef = (float)std::exp(-l*l/fsigma2); - cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c)); - S+=coef; - } - X+=u; Y+=v; - } - } break; - default : { // 2nd-order Runge-kutta interpolation - for (float l = 0; l=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) { - const float - u0 = (float)(0.5f*W._linear_atXY(X,Y,0,0)), - v0 = (float)(0.5f*W._linear_atXY(X,Y,0,1)), - u = (float)(W._linear_atXY(X+u0,Y+v0,0,0)), - v = (float)(W._linear_atXY(X+u0,Y+v0,0,1)); - if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; } - else { - const float coef = (float)std::exp(-l*l/fsigma2); - cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c)); - S+=coef; - } - X+=u; Y+=v; - } - } - } - Tfloat *ptrd = res.data(x,y); - if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; } - else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,0,c)); ptrd+=whd; } - } - } - } - const Tfloat *ptrs = res._data; - cimg_for(*this,ptrd,T) { - const Tfloat val = *(ptrs++)/N; - *ptrd = valval_max?val_max:(T)val); - } - } - return *this; - } - - //! Blur image anisotropically, directed by a field of diffusion tensors \newinstance. - template - CImg get_blur_anisotropic(const CImg& G, - const float amplitude=60, const float dl=0.8f, const float da=30, - const float gauss_prec=2, const unsigned int interpolation_type=0, - const bool is_fast_approx=true) const { - return (+*this).blur_anisotropic(G,amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx); - } - - //! Blur image anisotropically, in an edge-preserving way. - /** - \param amplitude Amplitude of the smoothing. - \param sharpness Sharpness. - \param anisotropy Anisotropy. - \param alpha Standard deviation of the gradient blur. - \param sigma Standard deviation of the structure tensor blur. - \param dl Spatial discretization. - \param da Angular discretization. - \param gauss_prec Precision of the diffusion process. - \param interpolation_type Interpolation scheme. - Can be { 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }. - \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not. - **/ - CImg& blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f, - const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f, const float da=30, - const float gauss_prec=2, const unsigned int interpolation_type=0, - const bool is_fast_approx=true) { - return blur_anisotropic(get_diffusion_tensors(sharpness,anisotropy,alpha,sigma,interpolation_type!=3), - amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx); - } - - //! Blur image anisotropically, in an edge-preserving way \newinstance. - CImg get_blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f, - const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f, - const float da=30, const float gauss_prec=2, const unsigned int interpolation_type=0, - const bool is_fast_approx=true) const { - return (+*this).blur_anisotropic(amplitude,sharpness,anisotropy,alpha,sigma,dl,da,gauss_prec,interpolation_type, - is_fast_approx); - } - - //! Blur image, with the joint bilateral filter. - /** - \param guide Image used to model the smoothing weights. - \param sigma_x Amount of blur along the X-axis. - \param sigma_y Amount of blur along the Y-axis. - \param sigma_z Amount of blur along the Z-axis. - \param sigma_r Amount of blur along the value axis. - \param bgrid_x Size of the bilateral grid along the X-axis. - \param bgrid_y Size of the bilateral grid along the Y-axis. - \param bgrid_z Size of the bilateral grid along the Z-axis. - \param bgrid_r Size of the bilateral grid along the value axis. - \param interpolation_type Use interpolation for image slicing. - \note This algorithm uses the optimisation technique proposed by S. Paris and F. Durand, in ECCV'2006 - (extended for 3d volumetric images). - **/ - template - CImg& blur_bilateral(const CImg& guide, - const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_r, - const int bgrid_x, const int bgrid_y, const int bgrid_z, const int bgrid_r, - const bool interpolation_type=true) { - if (!is_sameXYZ(guide)) - throw CImgArgumentException(_cimg_instance - "blur_bilateral(): Invalid size for specified guide image (%u,%u,%u,%u,%p).", - cimg_instance, - guide._width,guide._height,guide._depth,guide._spectrum,guide._data); - if (is_empty()) return *this; - T m, M = guide.max_min(m); - if (m==M) return *this; - const float range = (float)(M - m); - const unsigned int - bx0 = bgrid_x>=0?bgrid_x:_width*-bgrid_x/100, - by0 = bgrid_y>=0?bgrid_y:_height*-bgrid_y/100, - bz0 = bgrid_z>=0?bgrid_z:_depth*-bgrid_z/100, - br0 = bgrid_r>=0?bgrid_r:(int)(-range*bgrid_r/100), - bx = bx0>0?bx0:1, - by = by0>0?by0:1, - bz = bz0>0?bz0:1, - br = br0>0?br0:1; - const float - _sigma_x = sigma_x>=0?sigma_x:-sigma_x*_width/100, - _sigma_y = sigma_y>=0?sigma_y:-sigma_y*_height/100, - _sigma_z = sigma_z>=0?sigma_z:-sigma_z*_depth/100, - _sigma_r = sigma_r>=0?sigma_r:-sigma_r*range/100, - nsigma_x = _sigma_x*bx/_width, - nsigma_y = _sigma_y*by/_height, - nsigma_z = _sigma_z*bz/_depth, - nsigma_r = _sigma_r*br/range; - if (nsigma_x>0 || nsigma_y>0 || nsigma_z>0 || nsigma_r>0) { - const bool is_3d = (_depth>1); - if (is_3d) { // 3d version of the algorithm - CImg bgrid(bx,by,bz,br), bgridw(bx,by,bz,br); - cimg_forC(*this,c) { - const CImg _guide = guide.get_shared_channel(c%guide._spectrum); - bgrid.fill(0); bgridw.fill(0); - cimg_forXYZ(*this,x,y,z) { - const T val = (*this)(x,y,z,c); - const float gval = (float)_guide(x,y,z); - const int X = x*bx/_width, Y = y*by/_height, Z = z*bz/_depth, - R = (int)cimg::min(br-1.0f,(gval-m)*br/range); - bgrid(X,Y,Z,R) += (float)val; - bgridw(X,Y,Z,R) += 1; - } - bgrid.blur(nsigma_x,nsigma_y,nsigma_z,true).deriche(nsigma_r,0,'c',false); - bgridw.blur(nsigma_x,nsigma_y,nsigma_z,true).deriche(nsigma_r,0,'c',false); - if (interpolation_type) cimg_forXYZ(*this,x,y,z) { - const float gval = (float)_guide(x,y,z), - X = (float)x*bx/_width, Y = (float)y*by/_height, Z = (float)z*bz/_depth, - R = (float)cimg::min(br-1.0f,(gval-m)*br/range), - bval0 = bgrid._linear_atXYZC(X,Y,Z,R), bval1 = bgridw._linear_atXYZC(X,Y,Z,R); - (*this)(x,y,z,c) = (T)(bval0/bval1); - } else cimg_forXYZ(*this,x,y,z) { - const float gval = (float)_guide(x,y,z); - const int X = x*bx/_width, Y = y*by/_height, Z = z*bz/_depth, - R = (int)cimg::min(br-1.0f,(gval-m)*br/range); - const float bval0 = bgrid(X,Y,Z,R), bval1 = bgridw(X,Y,Z,R); - (*this)(x,y,z,c) = (T)(bval0/bval1); - } - } - } else { // 2d version of the algorithm - CImg bgrid(bx,by,br,2); - cimg_forC(*this,c) { - const CImg _guide = guide.get_shared_channel(c%guide._spectrum); - bgrid.fill(0); - cimg_forXY(*this,x,y) { - const T val = (*this)(x,y,c); - const float gval = (float)_guide(x,y); - const int X = x*bx/_width, Y = y*by/_height, R = (int)cimg::min(br-1.0f,(gval-m)*br/range); - bgrid(X,Y,R,0) += (float)val; - bgrid(X,Y,R,1) += 1; - } - bgrid.blur(nsigma_x,nsigma_y,0,true).blur(0,0,nsigma_r,false); - if (interpolation_type) cimg_forXY(*this,x,y) { - const float gval = (float)_guide(x,y), - X = (float)x*bx/_width, Y = (float)y*by/_height, R = (float)cimg::min(br-1.0f,(gval-m)*br/range), - bval0 = bgrid._linear_atXYZ(X,Y,R,0), bval1 = bgrid._linear_atXYZ(X,Y,R,1); - (*this)(x,y,c) = (T)(bval0/bval1); - } else cimg_forXY(*this,x,y) { - const float gval = (float)_guide(x,y); - const int X = x*bx/_width, Y = y*by/_height, R = (int)cimg::min(br-1.0f,(gval-m)*br/range); - const float bval0 = bgrid(X,Y,R,0), bval1 = bgrid(X,Y,R,1); - (*this)(x,y,c) = (T)(bval0/bval1); - } - } - } - } - return *this; - } - - //! Blur image, with the joint bilateral filter \newinstance. - template - CImg get_blur_bilateral(const CImg& guide, - const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_r, - const int bgrid_x, const int bgrid_y, const int bgrid_z, const int bgrid_r, - const bool interpolation_type=true) const { - return (+*this).blur_bilateral(guide,sigma_x,sigma_y,sigma_z,sigma_r,bgrid_x,bgrid_y,bgrid_z,bgrid_r, - interpolation_type); - } - - //! Blur image using the joint bilateral filter. - /** - \param guide Image used to model the smoothing weights. - \param sigma_s Amount of blur along the XYZ-axes. - \param sigma_r Amount of blur along the value axis. - \param bgrid_s Size of the bilateral grid along the XYZ-axes. - \param bgrid_r Size of the bilateral grid along the value axis. - \param interpolation_type Use interpolation for image slicing. - **/ - template - CImg& blur_bilateral(const CImg& guide, - const float sigma_s, const float sigma_r, const int bgrid_s=-33, const int bgrid_r=32, - const bool interpolation_type=true) { - const float nsigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100; - return blur_bilateral(guide,nsigma_s,nsigma_s,nsigma_s,sigma_r,bgrid_s,bgrid_s,bgrid_s,bgrid_r, - interpolation_type); - } - - //! Blur image using the bilateral filter \newinstance. - template - CImg get_blur_bilateral(const CImg& guide, - const float sigma_s, const float sigma_r, const int bgrid_s=-33, const int bgrid_r=32, - const bool interpolation_type=true) const { - return (+*this).blur_bilateral(guide,sigma_s,sigma_s,sigma_s,sigma_r,bgrid_s,bgrid_s,bgrid_s,bgrid_r, - interpolation_type); - } - - //! Blur image using patch-based space. - /** - \param sigma_s Amount of blur along the XYZ-axes. - \param sigma_p Amount of blur along the value axis. - \param patch_size Size of the patchs. - \param lookup_size Size of the window to search similar patchs. - \param smoothness Smoothness for the patch comparison. - \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not. - **/ - CImg& blur_patch(const float sigma_s, const float sigma_p, const unsigned int patch_size=3, - const unsigned int lookup_size=4, const float smoothness=0, const bool is_fast_approx=true) { - if (is_empty() || !patch_size || !lookup_size) return *this; - return get_blur_patch(sigma_s,sigma_p,patch_size,lookup_size,smoothness,is_fast_approx).move_to(*this); - } - - //! Blur image using patch-based space \newinstance. - CImg get_blur_patch(const float sigma_s, const float sigma_p, const unsigned int patch_size=3, - const unsigned int lookup_size=4, const float smoothness=0, - const bool is_fast_approx=true) const { - -#define _cimg_blur_patch3d_fast(N) \ - cimg_for##N##XYZ(res,x,y,z) { \ - T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N##x##N(img,x,y,z,c,pP,T); pP+=N3; } \ - const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \ - x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \ - float sum_weights = 0; \ - cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (cimg::abs(img(x,y,z,0) - img(p,q,r,0))3?0.0f:1.0f; \ - sum_weights+=weight; \ - cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); \ - } \ - if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; \ - else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \ - } - -#define _cimg_blur_patch3d(N) \ - cimg_for##N##XYZ(res,x,y,z) { \ - T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N##x##N(img,x,y,z,c,pP,T); pP+=N3; } \ - const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \ - x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \ - float sum_weights = 0, weight_max = 0; \ - cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) { \ - T *pQ = Q._data; cimg_forC(res,c) { cimg_get##N##x##N##x##N(img,p,q,r,c,pQ,T); pQ+=N3; } \ - float distance2 = 0; \ - pQ = Q._data; cimg_for(P,pP,T) { const float dI = (float)*pP - (float)*(pQ++); distance2+=dI*dI; } \ - distance2/=Pnorm; \ - const float dx = (float)p - x, dy = (float)q - y, dz = (float)r - z, \ - alldist = distance2 + (dx*dx + dy*dy + dz*dz)/sigma_s2, weight = (float)std::exp(-alldist); \ - if (weight>weight_max) weight_max = weight; \ - sum_weights+=weight; \ - cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); \ - } \ - sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=weight_max*(*this)(x,y,z,c); \ - if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; \ - else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \ - } - -#define _cimg_blur_patch2d_fast(N) \ - cimg_for##N##XY(res,x,y) { \ - T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N(img,x,y,0,c,pP,T); pP+=N2; } \ - const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \ - float sum_weights = 0; \ - cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) if (cimg::abs(img(x,y,0,0) - img(p,q,0,0))3?0.0f:1.0f; \ - sum_weights+=weight; \ - cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); \ - } \ - if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; \ - else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \ - } - -#define _cimg_blur_patch2d(N) \ - cimg_for##N##XY(res,x,y) { \ - T *pP = P._data; cimg_forC(res,c) { cimg_get##N##x##N(img,x,y,0,c,pP,T); pP+=N2; } \ - const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \ - float sum_weights = 0, weight_max = 0; \ - cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) { \ - T *pQ = Q._data; cimg_forC(res,c) { cimg_get##N##x##N(img,p,q,0,c,pQ,T); pQ+=N2; } \ - float distance2 = 0; \ - pQ = Q._data; cimg_for(P,pP,T) { const float dI = (float)*pP - (float)*(pQ++); distance2+=dI*dI; } \ - distance2/=Pnorm; \ - const float dx = (float)p - x, dy = (float)q - y, \ - alldist = distance2 + (dx*dx+dy*dy)/sigma_s2, weight = (float)std::exp(-alldist); \ - if (weight>weight_max) weight_max = weight; \ - sum_weights+=weight; \ - cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); \ - } \ - sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=weight_max*(*this)(x,y,c); \ - if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; \ - else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \ - } - - if (is_empty() || !patch_size || !lookup_size) return +*this; - CImg res(_width,_height,_depth,_spectrum,0); - const CImg _img = smoothness>0?get_blur(smoothness):CImg(),&img = smoothness>0?_img:*this; - CImg P(patch_size*patch_size*_spectrum), Q(P); - const float - nsigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100, - sigma_s2 = nsigma_s*nsigma_s, sigma_p2 = sigma_p*sigma_p, sigma_p3 = 3*sigma_p, - Pnorm = P.size()*sigma_p2; - const int rsize2 = (int)lookup_size/2, rsize1 = (int)lookup_size - rsize2 - 1; - const unsigned int N2 = patch_size*patch_size, N3 = N2*patch_size; - cimg::unused(N2,N3); - if (_depth>1) switch (patch_size) { // 3d - case 2 : if (is_fast_approx) _cimg_blur_patch3d_fast(2) else _cimg_blur_patch3d(2) break; - case 3 : if (is_fast_approx) _cimg_blur_patch3d_fast(3) else _cimg_blur_patch3d(3) break; - default : { - const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1; - if (is_fast_approx) -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (res._width>=32 && res._height*res._depth>=4) private(P,Q) -#endif - cimg_forXYZ(res,x,y,z) { // Fast - P = img.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true); - const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, - x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; - float sum_weights = 0; - cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (cimg::abs(img(x,y,z,0)-img(p,q,r,0))3?0.0f:1.0f; - sum_weights+=weight; - cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); - } - if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; - else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); - } else -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (res._width>=32 && res._height*res._depth>=4) firstprivate(P,Q) -#endif - cimg_forXYZ(res,x,y,z) { // Exact - P = img.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true); - const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, - x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; - float sum_weights = 0, weight_max = 0; - cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) { - (Q = img.get_crop(p - psize1,q - psize1,r - psize1,p + psize2,q + psize2,r + psize2,true))-=P; - const float - dx = (float)x - p, dy = (float)y - q, dz = (float)z - r, - distance2 = (float)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy + dz*dz)/sigma_s2), - weight = (float)std::exp(-distance2); - if (weight>weight_max) weight_max = weight; - sum_weights+=weight; - cimg_forC(res,c) res(x,y,z,c)+=weight*(*this)(p,q,r,c); - } - sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=weight_max*(*this)(x,y,z,c); - if (sum_weights>0) cimg_forC(res,c) res(x,y,z,c)/=sum_weights; - else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); - } - } - } else switch (patch_size) { // 2d - case 2 : if (is_fast_approx) _cimg_blur_patch2d_fast(2) else _cimg_blur_patch2d(2) break; - case 3 : if (is_fast_approx) _cimg_blur_patch2d_fast(3) else _cimg_blur_patch2d(3) break; - case 4 : if (is_fast_approx) _cimg_blur_patch2d_fast(4) else _cimg_blur_patch2d(4) break; - case 5 : if (is_fast_approx) _cimg_blur_patch2d_fast(5) else _cimg_blur_patch2d(5) break; - case 6 : if (is_fast_approx) _cimg_blur_patch2d_fast(6) else _cimg_blur_patch2d(6) break; - case 7 : if (is_fast_approx) _cimg_blur_patch2d_fast(7) else _cimg_blur_patch2d(7) break; - case 8 : if (is_fast_approx) _cimg_blur_patch2d_fast(8) else _cimg_blur_patch2d(8) break; - case 9 : if (is_fast_approx) _cimg_blur_patch2d_fast(9) else _cimg_blur_patch2d(9) break; - default : { // Fast - const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1; - if (is_fast_approx) -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._width>=32 && res._height>=4) firstprivate(P,Q) -#endif - cimg_forXY(res,x,y) { // 2d fast approximation. - P = img.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true); - const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; - float sum_weights = 0; - cimg_for_inXY(res,x0,y0,x1,y1,p,q) if (cimg::abs(img(x,y,0)-img(p,q,0))3?0.0f:1.0f; - sum_weights+=weight; - cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); - } - if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; - else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); - } else -#ifdef cimg_use_openmp -#pragma omp parallel for if (res._width>=32 && res._height>=4) firstprivate(P,Q) -#endif - cimg_forXY(res,x,y) { // 2d exact algorithm. - P = img.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true); - const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; - float sum_weights = 0, weight_max = 0; - cimg_for_inXY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) { - (Q = img.get_crop(p - psize1,q - psize1,p + psize2,q + psize2,true))-=P; - const float - dx = (float)x - p, dy = (float)y - q, - distance2 = (float)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy)/sigma_s2), - weight = (float)std::exp(-distance2); - if (weight>weight_max) weight_max = weight; - sum_weights+=weight; - cimg_forC(res,c) res(x,y,c)+=weight*(*this)(p,q,c); - } - sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=weight_max*(*this)(x,y,c); - if (sum_weights>0) cimg_forC(res,c) res(x,y,c)/=sum_weights; - else cimg_forC(res,c) res(x,y,0,c) = (Tfloat)((*this)(x,y,c)); - } - } - } - return res; - } - - //! Blur image with the median filter. - /** - \param n Size of the median filter. - \param threshold Threshold used to discard pixels too far from the current pixel value in the median computation. - **/ - CImg& blur_median(const unsigned int n, const float threshold=0) { - if (!n) return *this; - return get_blur_median(n,threshold).move_to(*this); - } - - //! Blur image with the median filter \newinstance. - CImg get_blur_median(const unsigned int n, const float threshold=0) const { - if (is_empty() || n<=1) return +*this; - CImg res(_width,_height,_depth,_spectrum); - T *ptrd = res._data; - cimg::unused(ptrd); - const int hl = n/2, hr = hl - 1 + n%2; - if (res._depth!=1) { // 3d - if (threshold>0) -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=16 && _height*_depth*_spectrum>=4) -#endif - cimg_forXYZC(*this,x,y,z,c) { // With threshold. - const int - x0 = x - hl, y0 = y - hl, z0 = z-hl, x1 = x + hr, y1 = y + hr, z1 = z+hr, - nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0, - nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1, nz1 = z1>=depth()?depth()-1:z1; - const float val0 = (float)(*this)(x,y,z,c); - CImg values(n*n*n); - unsigned int nb_values = 0; - T *ptrd = values.data(); - cimg_for_inXYZ(*this,nx0,ny0,nz0,nx1,ny1,nz1,p,q,r) - if (cimg::abs((float)(*this)(p,q,r,c)-val0)<=threshold) { *(ptrd++) = (*this)(p,q,r,c); ++nb_values; } - res(x,y,z,c) = values.get_shared_points(0,nb_values-1).median(); - } - else -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width>=16 && _height*_depth*_spectrum>=4) -#endif - cimg_forXYZC(*this,x,y,z,c) { // Without threshold. - const int - x0 = x - hl, y0 = y - hl, z0 = z-hl, x1 = x + hr, y1 = y + hr, z1 = z+hr, - nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0, - nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1, nz1 = z1>=depth()?depth()-1:z1; - res(x,y,z,c) = get_crop(nx0,ny0,nz0,c,nx1,ny1,nz1,c).median(); - } - } else { -#define _cimg_median_sort(a,b) if ((a)>(b)) cimg::swap(a,b) - if (res._height!=1) { // 2d - if (threshold>0) -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=16 && _height*_spectrum>=4) -#endif - cimg_forXYC(*this,x,y,c) { // With threshold. - const int - x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr, - nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, - nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1; - const float val0 = (float)(*this)(x,y,c); - CImg values(n*n); - unsigned int nb_values = 0; - T *ptrd = values.data(); - cimg_for_inXY(*this,nx0,ny0,nx1,ny1,p,q) - if (cimg::abs((float)(*this)(p,q,c)-val0)<=threshold) { *(ptrd++) = (*this)(p,q,c); ++nb_values; } - res(x,y,c) = values.get_shared_points(0,nb_values-1).median(); - } - else switch (n) { // Without threshold. - case 3 : { -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - T I[9] = { 0 }; - CImg_3x3(J,T); - cimg_for3x3(*this,x,y,0,c,I,T) { - std::memcpy(J,I,9*sizeof(T)); - _cimg_median_sort(Jcp, Jnp); _cimg_median_sort(Jcc, Jnc); _cimg_median_sort(Jcn, Jnn); - _cimg_median_sort(Jpp, Jcp); _cimg_median_sort(Jpc, Jcc); _cimg_median_sort(Jpn, Jcn); - _cimg_median_sort(Jcp, Jnp); _cimg_median_sort(Jcc, Jnc); _cimg_median_sort(Jcn, Jnn); - _cimg_median_sort(Jpp, Jpc); _cimg_median_sort(Jnc, Jnn); _cimg_median_sort(Jcc, Jcn); - _cimg_median_sort(Jpc, Jpn); _cimg_median_sort(Jcp, Jcc); _cimg_median_sort(Jnp, Jnc); - _cimg_median_sort(Jcc, Jcn); _cimg_median_sort(Jcc, Jnp); _cimg_median_sort(Jpn, Jcc); - _cimg_median_sort(Jcc, Jnp); - res(x,y,c) = Jcc; - } - } - } break; - case 5 : { -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - T I[25] = { 0 }; - CImg_5x5(J,T); - cimg_for5x5(*this,x,y,0,c,I,T) { - std::memcpy(J,I,25*sizeof(T)); - _cimg_median_sort(Jbb,Jpb); _cimg_median_sort(Jnb,Jab); _cimg_median_sort(Jcb,Jab); - _cimg_median_sort(Jcb,Jnb); _cimg_median_sort(Jpp,Jcp); _cimg_median_sort(Jbp,Jcp); - _cimg_median_sort(Jbp,Jpp); _cimg_median_sort(Jap,Jbc); _cimg_median_sort(Jnp,Jbc); - _cimg_median_sort(Jnp,Jap); _cimg_median_sort(Jcc,Jnc); _cimg_median_sort(Jpc,Jnc); - _cimg_median_sort(Jpc,Jcc); _cimg_median_sort(Jbn,Jpn); _cimg_median_sort(Jac,Jpn); - _cimg_median_sort(Jac,Jbn); _cimg_median_sort(Jnn,Jan); _cimg_median_sort(Jcn,Jan); - _cimg_median_sort(Jcn,Jnn); _cimg_median_sort(Jpa,Jca); _cimg_median_sort(Jba,Jca); - _cimg_median_sort(Jba,Jpa); _cimg_median_sort(Jna,Jaa); _cimg_median_sort(Jcb,Jbp); - _cimg_median_sort(Jnb,Jpp); _cimg_median_sort(Jbb,Jpp); _cimg_median_sort(Jbb,Jnb); - _cimg_median_sort(Jab,Jcp); _cimg_median_sort(Jpb,Jcp); _cimg_median_sort(Jpb,Jab); - _cimg_median_sort(Jpc,Jac); _cimg_median_sort(Jnp,Jac); _cimg_median_sort(Jnp,Jpc); - _cimg_median_sort(Jcc,Jbn); _cimg_median_sort(Jap,Jbn); _cimg_median_sort(Jap,Jcc); - _cimg_median_sort(Jnc,Jpn); _cimg_median_sort(Jbc,Jpn); _cimg_median_sort(Jbc,Jnc); - _cimg_median_sort(Jba,Jna); _cimg_median_sort(Jcn,Jna); _cimg_median_sort(Jcn,Jba); - _cimg_median_sort(Jpa,Jaa); _cimg_median_sort(Jnn,Jaa); _cimg_median_sort(Jnn,Jpa); - _cimg_median_sort(Jan,Jca); _cimg_median_sort(Jnp,Jcn); _cimg_median_sort(Jap,Jnn); - _cimg_median_sort(Jbb,Jnn); _cimg_median_sort(Jbb,Jap); _cimg_median_sort(Jbc,Jan); - _cimg_median_sort(Jpb,Jan); _cimg_median_sort(Jpb,Jbc); _cimg_median_sort(Jpc,Jba); - _cimg_median_sort(Jcb,Jba); _cimg_median_sort(Jcb,Jpc); _cimg_median_sort(Jcc,Jpa); - _cimg_median_sort(Jnb,Jpa); _cimg_median_sort(Jnb,Jcc); _cimg_median_sort(Jnc,Jca); - _cimg_median_sort(Jab,Jca); _cimg_median_sort(Jab,Jnc); _cimg_median_sort(Jac,Jna); - _cimg_median_sort(Jbp,Jna); _cimg_median_sort(Jbp,Jac); _cimg_median_sort(Jbn,Jaa); - _cimg_median_sort(Jpp,Jaa); _cimg_median_sort(Jpp,Jbn); _cimg_median_sort(Jcp,Jpn); - _cimg_median_sort(Jcp,Jan); _cimg_median_sort(Jnc,Jpa); _cimg_median_sort(Jbn,Jna); - _cimg_median_sort(Jcp,Jnc); _cimg_median_sort(Jcp,Jbn); _cimg_median_sort(Jpb,Jap); - _cimg_median_sort(Jnb,Jpc); _cimg_median_sort(Jbp,Jcn); _cimg_median_sort(Jpc,Jcn); - _cimg_median_sort(Jap,Jcn); _cimg_median_sort(Jab,Jbc); _cimg_median_sort(Jpp,Jcc); - _cimg_median_sort(Jcp,Jac); _cimg_median_sort(Jab,Jpp); _cimg_median_sort(Jab,Jcp); - _cimg_median_sort(Jcc,Jac); _cimg_median_sort(Jbc,Jac); _cimg_median_sort(Jpp,Jcp); - _cimg_median_sort(Jbc,Jcc); _cimg_median_sort(Jpp,Jbc); _cimg_median_sort(Jpp,Jcn); - _cimg_median_sort(Jcc,Jcn); _cimg_median_sort(Jcp,Jcn); _cimg_median_sort(Jcp,Jbc); - _cimg_median_sort(Jcc,Jnn); _cimg_median_sort(Jcp,Jcc); _cimg_median_sort(Jbc,Jnn); - _cimg_median_sort(Jcc,Jba); _cimg_median_sort(Jbc,Jba); _cimg_median_sort(Jbc,Jcc); - res(x,y,c) = Jcc; - } - } - } break; - default : { -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=16 && _height*_spectrum>=4) -#endif - cimg_forXYC(*this,x,y,c) { - const int - x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr, - nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, - nx1 = x1>=width()?width()-1:x1, ny1 = y1>=height()?height()-1:y1; - res(x,y,c) = get_crop(nx0,ny0,0,c,nx1,ny1,0,c).median(); - } - } - } - } else { // 1d - - if (threshold>0) -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width>=16 && _spectrum>=2) -#endif - cimg_forXC(*this,x,c) { // With threshold. - const int - x0 = x - hl, x1 = x + hr, - nx0 = x0<0?0:x0, nx1 = x1>=width()?width()-1:x1; - const float val0 = (float)(*this)(x,c); - CImg values(n); - unsigned int nb_values = 0; - T *ptrd = values.data(); - cimg_for_inX(*this,nx0,nx1,p) - if (cimg::abs((float)(*this)(p,c)-val0)<=threshold) { *(ptrd++) = (*this)(p,c); ++nb_values; } - res(x,c) = values.get_shared_points(0,nb_values-1).median(); - } - else switch (n) { // Without threshold. - case 2 : { -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - T I[4] = { 0 }; - cimg_for2x2(*this,x,y,0,c,I,T) res(x,c) = (T)(0.5f*(I[0]+I[1])); - } - } break; - case 3 : { -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - T I[9] = { 0 }; - cimg_for3x3(*this,x,y,0,c,I,T) - res(x,c) = I[3]=16 && _spectrum>=2) -#endif - cimg_forXC(*this,x,c) { - const int - x0 = x - hl, x1 = x + hr, - nx0 = x0<0?0:x0, nx1 = x1>=width()?width()-1:x1; - res(x,c) = get_crop(nx0,0,0,c,nx1,0,0,c).median(); - } - } - } - } - } - return res; - } - - //! Sharpen image. - /** - \param amplitude Sharpening amplitude - \param sharpen_type Select sharpening method. Can be { false=inverse diffusion | true=shock filters }. - \param edge Edge threshold (shock filters only). - \param alpha Gradient smoothness (shock filters only). - \param sigma Tensor smoothness (shock filters only). - **/ - CImg& sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1, - const float alpha=0, const float sigma=0) { - if (is_empty()) return *this; - T val_min, val_max = max_min(val_min); - const float nedge = edge/2; - CImg velocity(_width,_height,_depth,_spectrum), _veloc_max(_spectrum); - - if (_depth>1) { // 3d - if (sharpen_type) { // Shock filters. - CImg G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors()); - if (sigma>0) G.blur(sigma); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=32 && _height*_depth>=16) -#endif - cimg_forYZ(G,y,z) { - Tfloat *ptrG0 = G.data(0,y,z,0), *ptrG1 = G.data(0,y,z,1), - *ptrG2 = G.data(0,y,z,2), *ptrG3 = G.data(0,y,z,3); - CImg val, vec; - cimg_forX(G,x) { - G.get_tensor_at(x,y,z).symmetric_eigen(val,vec); - if (val[0]<0) val[0] = 0; - if (val[1]<0) val[1] = 0; - if (val[2]<0) val[2] = 0; - *(ptrG0++) = vec(0,0); - *(ptrG1++) = vec(0,1); - *(ptrG2++) = vec(0,2); - *(ptrG3++) = 1 - (Tfloat)std::pow(1+val[0]+val[1]+val[2],-(Tfloat)nedge); - } - } -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=512 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat - u = G(x,y,z,0), - v = G(x,y,z,1), - w = G(x,y,z,2), - amp = G(x,y,z,3), - ixx = Incc + Ipcc - 2*Iccc, - ixy = (Innc + Ippc - Inpc - Ipnc)/4, - ixz = (Incn + Ipcp - Incp - Ipcn)/4, - iyy = Icnc + Icpc - 2*Iccc, - iyz = (Icnn + Icpp - Icnp - Icpn)/4, - izz = Iccn + Iccp - 2*Iccc, - ixf = Incc - Iccc, - ixb = Iccc - Ipcc, - iyf = Icnc - Iccc, - iyb = Iccc - Icpc, - izf = Iccn - Iccc, - izb = Iccc - Iccp, - itt = u*u*ixx + v*v*iyy + w*w*izz + 2*u*v*ixy + 2*u*w*ixz + 2*v*w*iyz, - it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb) + w*cimg::minmod(izf,izb), - veloc = -amp*cimg::sign(itt)*cimg::abs(it); - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } - _veloc_max[c] = veloc_max; - } - } else // Inverse diffusion. - cimg_forC(*this,c) { - Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat veloc = -Ipcc - Incc - Icpc - Icnc - Iccp - Iccn + 6*Iccc; - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } - _veloc_max[c] = veloc_max; - } - } else { // 2d. - if (sharpen_type) { // Shock filters. - CImg G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors()); - if (sigma>0) G.blur(sigma); -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width>=32 && _height>=16) -#endif - cimg_forY(G,y) { - CImg val, vec; - Tfloat *ptrG0 = G.data(0,y,0,0), *ptrG1 = G.data(0,y,0,1), *ptrG2 = G.data(0,y,0,2); - cimg_forX(G,x) { - G.get_tensor_at(x,y).symmetric_eigen(val,vec); - if (val[0]<0) val[0] = 0; - if (val[1]<0) val[1] = 0; - *(ptrG0++) = vec(0,0); - *(ptrG1++) = vec(0,1); - *(ptrG2++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1],-(Tfloat)nedge); - } - } -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height>=512 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) { - const Tfloat - u = G(x,y,0), - v = G(x,y,1), - amp = G(x,y,2), - ixx = Inc + Ipc - 2*Icc, - ixy = (Inn + Ipp - Inp - Ipn)/4, - iyy = Icn + Icp - 2*Icc, - ixf = Inc - Icc, - ixb = Icc - Ipc, - iyf = Icn - Icc, - iyb = Icc - Icp, - itt = u*u*ixx + v*v*iyy + 2*u*v*ixy, - it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb), - veloc = -amp*cimg::sign(itt)*cimg::abs(it); - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } - _veloc_max[c] = veloc_max; - } - } else // Inverse diffusion. - cimg_forC(*this,c) { - Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0; - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) { - const Tfloat veloc = -Ipc - Inc - Icp - Icn + 4*Icc; - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } - _veloc_max[c] = veloc_max; - } - } - const Tfloat veloc_max = _veloc_max.max(); - if (veloc_max<=0) return *this; - return ((velocity*=amplitude/veloc_max)+=*this).cut(val_min,val_max).move_to(*this); - } - - //! Sharpen image \newinstance. - CImg get_sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1, - const float alpha=0, const float sigma=0) const { - return (+*this).sharpen(amplitude,sharpen_type,edge,alpha,sigma); - } - - //! Return image gradient. - /** - \param axes Axes considered for the gradient computation, as a C-string (e.g "xy"). - \param scheme = Numerical scheme used for the gradient computation: - - -1 = Backward finite differences - - 0 = Centered finite differences - - 1 = Forward finite differences - - 2 = Using Sobel masks - - 3 = Using rotation invariant masks - - 4 = Using Deriche recusrsive filter. - - 5 = Using Van Vliet recusrsive filter. - **/ - CImgList get_gradient(const char *const axes=0, const int scheme=3) const { - CImgList grad(2,_width,_height,_depth,_spectrum); - bool is_3d = false; - if (axes) { - for (unsigned int a = 0; axes[a]; ++a) { - const char axis = cimg::uncase(axes[a]); - switch (axis) { - case 'x' : case 'y' : break; - case 'z' : is_3d = true; break; - default : - throw CImgArgumentException(_cimg_instance - "get_gradient(): Invalid specified axis '%c'.", - cimg_instance, - axis); - } - } - } else is_3d = (_depth>1); - if (is_3d) { - CImg(_width,_height,_depth,_spectrum).move_to(grad); - switch (scheme) { // 3d. - case -1 : { // Backward finite differences. -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - const unsigned long off = c*_width*_height*_depth; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off, *ptrd2 = grad[2]._data + off; - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = Iccc - Ipcc; - *(ptrd1++) = Iccc - Icpc; - *(ptrd2++) = Iccc - Iccp; - } - } - } break; - case 1 : { // Forward finite differences. -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - const unsigned long off = c*_width*_height*_depth; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off, *ptrd2 = grad[2]._data + off; - CImg_2x2x2(I,Tfloat); - cimg_for2x2x2(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = Incc - Iccc; - *(ptrd1++) = Icnc - Iccc; - *(ptrd2++) = Iccn - Iccc; - } - } - } break; - case 4 : { // Deriche filter with low standard variation. - grad[0] = get_deriche(0,1,'x'); - grad[1] = get_deriche(0,1,'y'); - grad[2] = get_deriche(0,1,'z'); - } break; - case 5 : { // Van Vliet filter with low standard variation. - grad[0] = get_vanvliet(0,1,'x'); - grad[1] = get_vanvliet(0,1,'y'); - grad[2] = get_vanvliet(0,1,'z'); - } break; - default : { // Central finite differences. -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - const unsigned long off = c*_width*_height*_depth; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off, *ptrd2 = grad[2]._data + off; - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = (Incc - Ipcc)/2; - *(ptrd1++) = (Icnc - Icpc)/2; - *(ptrd2++) = (Iccn - Iccp)/2; - } - } - } - } - } else switch (scheme) { // 2d. - case -1 : { // Backward finite differences. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - const unsigned long off = c*_width*_height*_depth + z*_width*_height; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = Icc - Ipc; - *(ptrd1++) = Icc - Icp; - } - } - } break; - case 1 : { // Forward finite differences. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - const unsigned long off = c*_width*_height*_depth + z*_width*_height; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; - CImg_2x2(I,Tfloat); - cimg_for2x2(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = Inc - Icc; - *(ptrd1++) = Icn - Icc; - } - } - } break; - case 2 : { // Sobel scheme. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - const unsigned long off = c*_width*_height*_depth + z*_width*_height; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = -Ipp - 2*Ipc - Ipn + Inp + 2*Inc + Inn; - *(ptrd1++) = -Ipp - 2*Icp - Inp + Ipn + 2*Icn + Inn; - } - } - } break; - case 3 : { // Rotation invariant mask. -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - const unsigned long off = c*_width*_height*_depth + z*_width*_height; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; - CImg_3x3(I,Tfloat); - const Tfloat a = (Tfloat)(0.25f*(2-std::sqrt(2.0f))), b = (Tfloat)(0.5f*(std::sqrt(2.0f)-1)); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = -a*Ipp - b*Ipc - a*Ipn + a*Inp + b*Inc + a*Inn; - *(ptrd1++) = -a*Ipp - b*Icp - a*Inp + a*Ipn + b*Icn + a*Inn; - } - } - } break; - case 4 : { // Van Vliet filter with low standard variation - grad[0] = get_deriche(0,1,'x'); - grad[1] = get_deriche(0,1,'y'); - } break; - case 5 : { // Deriche filter with low standard variation - grad[0] = get_vanvliet(0,1,'x'); - grad[1] = get_vanvliet(0,1,'y'); - } break; - default : { // Central finite differences -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - const unsigned long off = c*_width*_height*_depth + z*_width*_height; - Tfloat *ptrd0 = grad[0]._data + off, *ptrd1 = grad[1]._data + off; - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = (Inc - Ipc)/2; - *(ptrd1++) = (Icn - Icp)/2; - } - } - } - } - if (!axes) return grad; - CImgList res; - for (unsigned int l = 0; axes[l]; ++l) { - const char axis = cimg::uncase(axes[l]); - switch (axis) { - case 'x' : res.insert(grad[0]); break; - case 'y' : res.insert(grad[1]); break; - case 'z' : res.insert(grad[2]); break; - } - } - grad.assign(); - return res; - } - - //! Return image hessian. - /** - \param axes Axes considered for the hessian computation, as a C-string (e.g "xy"). - **/ - CImgList get_hessian(const char *const axes=0) const { - CImgList res; - const char *naxes = axes, *const def_axes2d = "xxxyyy", *const def_axes3d = "xxxyxzyyyzzz"; - if (!axes) naxes = _depth>1?def_axes3d:def_axes2d; - const unsigned int lmax = std::strlen(naxes); - if (lmax%2) - throw CImgArgumentException(_cimg_instance - "get_hessian(): Invalid specified axes '%s'.", - cimg_instance, - naxes); - - res.assign(lmax/2,_width,_height,_depth,_spectrum); - if (!cimg::strcasecmp(naxes,def_axes3d)) { // 3d - -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - const unsigned long off = c*_width*_height*_depth; - Tfloat - *ptrd0 = res[0]._data + off, *ptrd1 = res[1]._data + off, *ptrd2 = res[2]._data + off, - *ptrd3 = res[3]._data + off, *ptrd4 = res[4]._data + off, *ptrd5 = res[5]._data + off; - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = Ipcc + Incc - 2*Iccc; // Ixx - *(ptrd1++) = (Ippc + Innc - Ipnc - Inpc)/4; // Ixy - *(ptrd2++) = (Ipcp + Incn - Ipcn - Incp)/4; // Ixz - *(ptrd3++) = Icpc + Icnc - 2*Iccc; // Iyy - *(ptrd4++) = (Icpp + Icnn - Icpn - Icnp)/4; // Iyz - *(ptrd5++) = Iccn + Iccp - 2*Iccc; // Izz - } - } - } else if (!cimg::strcasecmp(naxes,def_axes2d)) { // 2d -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - const unsigned long off = c*_width*_height*_depth + z*_width*_height; - Tfloat *ptrd0 = res[0]._data + off, *ptrd1 = res[1]._data + off, *ptrd2 = res[2]._data + off; - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) { - *(ptrd0++) = Ipc + Inc - 2*Icc; // Ixx - *(ptrd1++) = (Ipp + Inn - Ipn - Inp)/4; // Ixy - *(ptrd2++) = Icp + Icn - 2*Icc; // Iyy - } - } - } else for (unsigned int l = 0; laxis2) cimg::swap(axis1,axis2); - bool valid_axis = false; - if (axis1=='x' && axis2=='x') { // Ixx - valid_axis = true; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - Tfloat *ptrd = res[l2].data(0,0,z,c); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Ipc + Inc - 2*Icc; - } - } - else if (axis1=='x' && axis2=='y') { // Ixy - valid_axis = true; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - Tfloat *ptrd = res[l2].data(0,0,z,c); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = (Ipp + Inn - Ipn - Inp)/4; - } - } - else if (axis1=='x' && axis2=='z') { // Ixz - valid_axis = true; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = res[l2].data(0,0,0,c); - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = (Ipcp + Incn - Ipcn - Incp)/4; - } - } - else if (axis1=='y' && axis2=='y') { // Iyy - valid_axis = true; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forZC(*this,z,c) { - Tfloat *ptrd = res[l2].data(0,0,z,c); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Icp + Icn - 2*Icc; - } - } - else if (axis1=='y' && axis2=='z') { // Iyz - valid_axis = true; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = res[l2].data(0,0,0,c); - CImg_3x3x3(I,Tfloat); - cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = (Icpp + Icnn - Icpn - Icnp)/4; - } - } - else if (axis1=='z' && axis2=='z') { // Izz - valid_axis = true; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = res[l2].data(0,0,0,c); - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Iccn + Iccp - 2*Iccc; - } - } - else if (!valid_axis) - throw CImgArgumentException(_cimg_instance - "get_hessian(): Invalid specified axes '%s'.", - cimg_instance, - naxes); - } - return res; - } - - //! Compute image laplacian. - CImg& laplacian() { - return get_laplacian().move_to(*this); - } - - //! Compute image laplacian \newinstance. - CImg get_laplacian() const { - if (is_empty()) return CImg(); - CImg res(_width,_height,_depth,_spectrum); - if (_depth>1) { // 3d -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = res.data(0,0,0,c); - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Incc + Ipcc + Icnc + Icpc + Iccn + Iccp - 6*Iccc; - } - } else if (_height>1) { // 2d -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = res.data(0,0,0,c); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc + Icn + Icp - 4*Icc; - } - } else { // 1d -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width>=1048576 && _height*_depth*_spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd = res.data(0,0,0,c); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc - 2*Icc; - } - } - return res; - } - - //! Compute the structure tensor field of an image. - /** - \param scheme Numerical scheme. Can be { 0=central | 1=fwd/bwd1 | 2=fwd/bwd2 } - **/ - CImg& structure_tensors(const unsigned int scheme=2) { - return get_structure_tensors(scheme).move_to(*this); - } - - //! Compute the structure tensor field of an image \newinstance. - CImg get_structure_tensors(const unsigned int scheme=2) const { - if (is_empty()) return *this; - CImg res; - if (_depth>1) { // 3d - res.assign(_width,_height,_depth,6,0); - switch (scheme) { - case 0 : { // classical central finite differences -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat - *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2), - *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5); - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat - ix = (Incc - Ipcc)/2, - iy = (Icnc - Icpc)/2, - iz = (Iccn - Iccp)/2; - *(ptrd0++)+=ix*ix; - *(ptrd1++)+=ix*iy; - *(ptrd2++)+=ix*iz; - *(ptrd3++)+=iy*iy; - *(ptrd4++)+=iy*iz; - *(ptrd5++)+=iz*iz; - } - } - } break; - case 1 : { // Forward/backward finite differences (version 1). -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat - *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2), - *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5); - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat - ixf = Incc - Iccc, ixb = Iccc - Ipcc, - iyf = Icnc - Iccc, iyb = Iccc - Icpc, - izf = Iccn - Iccc, izb = Iccc - Iccp; - *(ptrd0++)+=(ixf*ixf + 2*ixf*ixb + ixb*ixb)/4; - *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; - *(ptrd2++)+=(ixf*izf + ixf*izb + ixb*izf + ixb*izb)/4; - *(ptrd3++)+=(iyf*iyf + 2*iyf*iyb + iyb*iyb)/4; - *(ptrd4++)+=(iyf*izf + iyf*izb + iyb*izf + iyb*izb)/4; - *(ptrd5++)+=(izf*izf + 2*izf*izb + izb*izb)/4; - } - } - } break; - default : { // Forward/backward finite differences (version 2). -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height*_depth>=1048576 && _spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat - *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2), - *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5); - CImg_3x3x3(I,Tfloat); - cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) { - const Tfloat - ixf = Incc - Iccc, ixb = Iccc - Ipcc, - iyf = Icnc - Iccc, iyb = Iccc - Icpc, - izf = Iccn - Iccc, izb = Iccc - Iccp; - *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2; - *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; - *(ptrd2++)+=(ixf*izf + ixf*izb + ixb*izf + ixb*izb)/4; - *(ptrd3++)+=(iyf*iyf + iyb*iyb)/2; - *(ptrd4++)+=(iyf*izf + iyf*izb + iyb*izf + iyb*izb)/4; - *(ptrd5++)+=(izf*izf + izb*izb)/2; - } - } - } break; - } - } else { // 2d - res.assign(_width,_height,_depth,3,0); - switch (scheme) { - case 0 : { // classical central finite differences -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) { - const Tfloat - ix = (Inc - Ipc)/2, - iy = (Icn - Icp)/2; - *(ptrd0++)+=ix*ix; - *(ptrd1++)+=ix*iy; - *(ptrd2++)+=iy*iy; - } - } - } break; - case 1 : { // Forward/backward finite differences (version 1). -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) { - const Tfloat - ixf = Inc - Icc, ixb = Icc - Ipc, - iyf = Icn - Icc, iyb = Icc - Icp; - *(ptrd0++)+=(ixf*ixf + 2*ixf*ixb + ixb*ixb)/4; - *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; - *(ptrd2++)+=(iyf*iyf + 2*iyf*iyb + iyb*iyb)/4; - } - } - } break; - default : { // Forward/backward finite differences (version 2). -#ifdef cimg_use_openmp -#pragma omp parallel for if (_width*_height>=1048576 && _depth*_spectrum>=2) -#endif - cimg_forC(*this,c) { - Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2); - CImg_3x3(I,Tfloat); - cimg_for3x3(*this,x,y,0,c,I,Tfloat) { - const Tfloat - ixf = Inc - Icc, ixb = Icc - Ipc, - iyf = Icn - Icc, iyb = Icc - Icp; - *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2; - *(ptrd1++)+=(ixf*iyf + ixf*iyb + ixb*iyf + ixb*iyb)/4; - *(ptrd2++)+=(iyf*iyf + iyb*iyb)/2; - } - } - } break; - } - } - return res; - } - - //! Compute field of diffusion tensors for edge-preserving smoothing. - /** - \param sharpness Sharpness - \param anisotropy Anisotropy - \param alpha Standard deviation of the gradient blur. - \param sigma Standard deviation of the structure tensor blur. - \param is_sqrt Tells if the square root of the tensor field is computed instead. - **/ - CImg& diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f, - const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) { - CImg res; - const float - nsharpness = cimg::max(sharpness,1e-5f), - power1 = (is_sqrt?0.5f:1)*nsharpness, - power2 = power1/(1e-7f+1-anisotropy); - blur(alpha).normalize(0,(T)255); - - if (_depth>1) { // 3d - get_structure_tensors().move_to(res).blur(sigma); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if(_width>=256 && _height*_depth>=256) -#endif - cimg_forYZ(*this,y,z) { - Tfloat - *ptrd0 = res.data(0,y,z,0), *ptrd1 = res.data(0,y,z,1), *ptrd2 = res.data(0,y,z,2), - *ptrd3 = res.data(0,y,z,3), *ptrd4 = res.data(0,y,z,4), *ptrd5 = res.data(0,y,z,5); - CImg val(3), vec(3,3); - cimg_forX(*this,x) { - res.get_tensor_at(x,y,z).symmetric_eigen(val,vec); - const float - _l1 = val[2], _l2 = val[1], _l3 = val[0], - l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0, l3 = _l3>0?_l3:0, - ux = vec(0,0), uy = vec(0,1), uz = vec(0,2), - vx = vec(1,0), vy = vec(1,1), vz = vec(1,2), - wx = vec(2,0), wy = vec(2,1), wz = vec(2,2), - n1 = (float)std::pow(1+l1+l2+l3,-power1), - n2 = (float)std::pow(1+l1+l2+l3,-power2); - *(ptrd0++) = n1*(ux*ux + vx*vx) + n2*wx*wx; - *(ptrd1++) = n1*(ux*uy + vx*vy) + n2*wx*wy; - *(ptrd2++) = n1*(ux*uz + vx*vz) + n2*wx*wz; - *(ptrd3++) = n1*(uy*uy + vy*vy) + n2*wy*wy; - *(ptrd4++) = n1*(uy*uz + vy*vz) + n2*wy*wz; - *(ptrd5++) = n1*(uz*uz + vz*vz) + n2*wz*wz; - } - } - } else { // for 2d images - get_structure_tensors().move_to(res).blur(sigma); -#ifdef cimg_use_openmp -#pragma omp parallel for if(_width>=256 && _height>=256) -#endif - cimg_forY(*this,y) { - Tfloat *ptrd0 = res.data(0,y,0,0), *ptrd1 = res.data(0,y,0,1), *ptrd2 = res.data(0,y,0,2); - CImg val(2), vec(2,2); - cimg_forX(*this,x) { - res.get_tensor_at(x,y).symmetric_eigen(val,vec); - const float - _l1 = val[1], _l2 = val[0], - l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0, - ux = vec(1,0), uy = vec(1,1), - vx = vec(0,0), vy = vec(0,1), - n1 = (float)std::pow(1+l1+l2,-power1), - n2 = (float)std::pow(1+l1+l2,-power2); - *(ptrd0++) = n1*ux*ux + n2*vx*vx; - *(ptrd1++) = n1*ux*uy + n2*vx*vy; - *(ptrd2++) = n1*uy*uy + n2*vy*vy; - } - } - } - return res.move_to(*this); - } - - //! Compute field of diffusion tensors for edge-preserving smoothing \newinstance. - CImg get_diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f, - const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) const { - return CImg(*this,false).diffusion_tensors(sharpness,anisotropy,alpha,sigma,is_sqrt); - } - - //! Estimate displacement field between two images. - /** - \param source Reference image. - \param smoothness Smoothness of estimated displacement field. - \param precision Precision required for algorithm convergence. - \param nb_scales Number of scales used to estimate the displacement field. - \param iteration_max Maximum number of iterations allowed for one scale. - \param is_backward If false, match I2(X+U(X)) = I1(X), else match I2(X) = I1(X-U(X)). - **/ - CImg& displacement(const CImg& source, const float smoothness=0.1f, const float precision=5.0f, - const unsigned int nb_scales=0, const unsigned int iteration_max=10000, - const bool is_backward=false) { - return get_displacement(source,smoothness,precision,nb_scales,iteration_max,is_backward).move_to(*this); - } - - //! Estimate displacement field between two images \newinstance. - CImg get_displacement(const CImg& source, - const float smoothness=0.1f, const float precision=5.0f, - const unsigned int nb_scales=0, const unsigned int iteration_max=10000, - const bool is_backward=false) const { - if (is_empty() || !source) return +*this; - if (!is_sameXYZC(source)) - throw CImgArgumentException(_cimg_instance - "displacement(): Instance and source image (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - source._width,source._height,source._depth,source._spectrum,source._data); - if (precision<0) - throw CImgArgumentException(_cimg_instance - "displacement(): Invalid specified precision %g " - "(should be >=0)", - cimg_instance, - precision); - const unsigned int _nb_scales = nb_scales>0?nb_scales: - (unsigned int)(2*std::log((double)(cimg::max(_width,_height)))); - const float _precision = (float)std::pow(10.0,-(double)precision); - float sm, sM = source.max_min(sm), tm, tM = max_min(tm); - const float sdelta = sm==sM?1:(sM - sm), tdelta = tm==tM?1:(tM - tm); - const bool is_3d = source._depth>1; - CImg U; - - for (int scale = _nb_scales-1; scale>=0; --scale) { - const float factor = (float)std::pow(1.5,(double)scale); - const unsigned int - _sw = (unsigned int)(_width/factor), sw = _sw?_sw:1, - _sh = (unsigned int)(_height/factor), sh = _sh?_sh:1, - _sd = (unsigned int)(_depth/factor), sd = _sd?_sd:1; - if (sw<5 && sh<5 && (!is_3d || sd<5)) continue; // skip too small scales. - const CImg - I1 = (source.get_resize(sw,sh,sd,-100,2)-=sm)/=sdelta, - I2 = (get_resize(I1,2)-=tm)/=tdelta; - if (U) (U*=1.5f).resize(I2._width,I2._height,I2._depth,-100,3); - else U.assign(I2._width,I2._height,I2._depth,is_3d?3:2,0); - float dt = 2, energy = cimg::type::max(); - const CImgList dI = is_backward?I1.get_gradient():I2.get_gradient(); - - for (unsigned int iteration = 0; iteration=0) cimg_for3XYZ(U,x,y,z) { // Isotropic regularization. - const float - X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0), - Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1), - Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2); - float delta_I = 0, _energy_regul = 0; - if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c)); - else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2.linear_atXYZ(X,Y,Z,c)); - cimg_forC(U,c) { - const float - Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)), - Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)), - Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)), - Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c), - Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c), - Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c); - U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c].linear_atXYZ(X,Y,Z) + - smoothness* ( Uxx + Uyy + Uzz)))/(1+6*smoothness*dt); - _energy_regul+=Ux*Ux + Uy*Uy + Uz*Uz; - } - _energy+=delta_I*delta_I + smoothness*_energy_regul; - } else { - const float nsmoothness = -smoothness; - cimg_for3XYZ(U,x,y,z) { // Anisotropic regularization. - const float - X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0), - Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1), - Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2); - float delta_I = 0, _energy_regul = 0; - if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c)); - else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2.linear_atXYZ(X,Y,Z,c)); - cimg_forC(U,c) { - const float - Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)), - Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)), - Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)), - N2 = Ux*Ux + Uy*Uy + Uz*Uz, - N = std::sqrt(N2), - N3 = 1e-5f + N2*N, - coef_a = (1 - Ux*Ux/N2)/N, - coef_b = -2*Ux*Uy/N3, - coef_c = -2*Ux*Uz/N3, - coef_d = (1 - Uy*Uy/N2)/N, - coef_e = -2*Uy*Uz/N3, - coef_f = (1 - Uz*Uz/N2)/N, - Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c), - Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c), - Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c), - Uxy = 0.25f*(U(_n1x,_n1y,z,c) + U(_p1x,_p1y,z,c) - U(_n1x,_p1y,z,c) - U(_n1x,_p1y,z,c)), - Uxz = 0.25f*(U(_n1x,y,_n1z,c) + U(_p1x,y,_p1z,c) - U(_n1x,y,_p1z,c) - U(_n1x,y,_p1z,c)), - Uyz = 0.25f*(U(x,_n1y,_n1z,c) + U(x,_p1y,_p1z,c) - U(x,_n1y,_p1z,c) - U(x,_n1y,_p1z,c)); - U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c].linear_atXYZ(X,Y,Z) + - nsmoothness* ( coef_a*Uxx + coef_b*Uxy + - coef_c*Uxz + coef_d*Uyy + - coef_e*Uyz + coef_f*Uzz )) - )/(1+2*(coef_a+coef_d+coef_f)*nsmoothness*dt); - _energy_regul+=N; - } - _energy+=delta_I*delta_I + nsmoothness*_energy_regul; - } - } - } else { // 2d version. - if (smoothness>=0) cimg_for3XY(U,x,y) { // Isotropic regularization. - const float - X = is_backward?x - U(x,y,0):x + U(x,y,0), - Y = is_backward?y - U(x,y,1):y + U(x,y,1); - float delta_I = 0, _energy_regul = 0; - if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXY(X,Y,c) - I2(x,y,c)); - else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2.linear_atXY(X,Y,c)); - cimg_forC(U,c) { - const float - Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)), - Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)), - Uxx = U(_n1x,y,c) + U(_p1x,y,c), - Uyy = U(x,_n1y,c) + U(x,_p1y,c); - U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c].linear_atXY(X,Y) + - smoothness*( Uxx + Uyy )))/(1+4*smoothness*dt); - _energy_regul+=Ux*Ux + Uy*Uy; - } - _energy+=delta_I*delta_I + smoothness*_energy_regul; - } else { - const float nsmoothness = -smoothness; - cimg_for3XY(U,x,y) { // Anisotropic regularization. - const float - X = is_backward?x - U(x,y,0):x + U(x,y,0), - Y = is_backward?y - U(x,y,1):y + U(x,y,1); - float delta_I = 0, _energy_regul = 0; - if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1.linear_atXY(X,Y,c) - I2(x,y,c)); - else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2.linear_atXY(X,Y,c)); - cimg_forC(U,c) { - const float - Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)), - Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)), - N2 = Ux*Ux + Uy*Uy, - N = std::sqrt(N2), - N3 = 1e-5f + N2*N, - coef_a = Uy*Uy/N3, - coef_b = -2*Ux*Uy/N3, - coef_c = Ux*Ux/N3, - Uxx = U(_n1x,y,c) + U(_p1x,y,c), - Uyy = U(x,_n1y,c) + U(x,_p1y,c), - Uxy = 0.25f*(U(_n1x,_n1y,c) + U(_p1x,_p1y,c) - U(_n1x,_p1y,c) - U(_n1x,_p1y,c)); - U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c].linear_atXY(X,Y) + - nsmoothness*( coef_a*Uxx + coef_b*Uxy + coef_c*Uyy )))/ - (1+2*(coef_a+coef_c)*nsmoothness*dt); - _energy_regul+=N; - } - _energy+=delta_I*delta_I + nsmoothness*_energy_regul; - } - } - } - const float d_energy = (_energy - energy)/(sw*sh*sd); - if (d_energy<=0 && -d_energy<_precision) break; - if (d_energy>0) dt*=0.5f; - energy = _energy; - } - } - return U; - } - - //! Compute Euclidean distance function to a specified value. - /** - \param value Reference value. - \param metric Type of metric. Can be { 0=Chebyshev | 1=Manhattan | 2=Euclidean | 3=Squared-euclidean }. - \note - The distance transform implementation has been submitted by A. Meijster, and implements - the article 'W.H. Hesselink, A. Meijster, J.B.T.M. Roerdink, - "A general algorithm for computing distance transforms in linear time.", - In: Mathematical Morphology and its Applications to Image and Signal Processing, - J. Goutsias, L. Vincent, and D.S. Bloomberg (eds.), Kluwer, 2000, pp. 331-340.' - The submitted code has then been modified to fit CImg coding style and constraints. - **/ - CImg& distance(const T value, const unsigned int metric=2) { - if (is_empty()) return *this; - bool is_value = false; - cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,0:(T)999999999; - if (!is_value) return fill(cimg::type::max()); - switch (metric) { - case 0 : return _distance_core(_distance_sep_cdt,_distance_dist_cdt); // Chebyshev. - case 1 : return _distance_core(_distance_sep_mdt,_distance_dist_mdt); // Manhattan. - case 3 : return _distance_core(_distance_sep_edt,_distance_dist_edt); // Squared Euclidean. - default : return _distance_core(_distance_sep_edt,_distance_dist_edt).sqrt(); // Euclidean. - } - return *this; - } - - //! Compute distance to a specified value \newinstance. - CImg get_distance(const T value, const unsigned int metric=2) const { - return CImg(*this,false).distance((Tfloat)value,metric); - } - - static long _distance_sep_edt(const long i, const long u, const long *const g) { - return (u*u-i*i+g[u]-g[i])/(2*(u-i)); - } - - static long _distance_dist_edt(const long x, const long i, const long *const g) { - return (x-i)*(x-i) + g[i]; - } - - static long _distance_sep_mdt(const long i, const long u, const long *const g) { - return (u-i<=g[u]-g[i]?999999999:(g[u]-g[i]+u+i)/2); - } - - static long _distance_dist_mdt(const long x, const long i, const long *const g) { - return (x=0) && f(t[q],s[q],g)>f(t[q],u,g)) { --q; } - if (q<0) { q = 0; s[0] = u; } - else { const long w = 1 + sep(s[q], u, g); if (w<(long)len) { ++q; s[q] = u; t[q] = w; }} - } - for (int u = (int)len-1; u>=0; --u) { dt[u] = f(u,s[q],g); if (u==t[q]) --q; } // Backward scan. - } - - CImg& _distance_core(long (*const sep)(const long, const long, const long *const), - long (*const f)(const long, const long, const long *const)) { - const unsigned long wh = (unsigned long)_width*_height; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - CImg g(_width), dt(_width), s(_width), t(_width); - CImg img = get_shared_channel(c); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_width>=512 && _height*_depth>=16) firstprivate(g,dt,s,t) -#endif - cimg_forYZ(*this,y,z) { // Over X-direction. - cimg_forX(*this,x) g[x] = (long)img(x,y,z,0,wh); - _distance_scan(_width,g,sep,f,s,t,dt); - cimg_forX(*this,x) img(x,y,z,0,wh) = (T)dt[x]; - } - if (_height>1) { - g.assign(_height); dt.assign(_height); s.assign(_height); t.assign(_height); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_height>=512 && _width*_depth>=16) firstprivate(g,dt,s,t) -#endif - cimg_forXZ(*this,x,z) { // Over Y-direction. - cimg_forY(*this,y) g[y] = (long)img(x,y,z,0,wh); - _distance_scan(_height,g,sep,f,s,t,dt); - cimg_forY(*this,y) img(x,y,z,0,wh) = (T)dt[y]; - } - } - if (_depth>1) { - g.assign(_depth); dt.assign(_depth); s.assign(_depth); t.assign(_depth); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if (_depth>=512 && _width*_height>=16) firstprivate(g,dt,s,t) -#endif - cimg_forXY(*this,x,y) { // Over Z-direction. - cimg_forZ(*this,z) g[z] = (long)img(x,y,z,0,wh); - _distance_scan(_depth,g,sep,f,s,t,dt); - cimg_forZ(*this,z) img(x,y,z,0,wh) = (T)dt[z]; - } - } - } - return *this; - } - - //! Compute chamfer distance to a specified value, with a custom metric. - /** - \param value Reference value. - \param metric_mask Metric mask. - \note The algorithm code has been initially proposed by A. Meijster, and modified by D. Tschumperlé. - **/ - template - CImg& distance(const T value, const CImg& metric_mask) { - if (is_empty()) return *this; - bool is_value = false; - cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,0:(T)999999999; - if (!is_value) return fill(cimg::type::max()); - const unsigned long wh = (unsigned long)_width*_height; -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) -#endif - cimg_forC(*this,c) { - CImg img = get_shared_channel(c); -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(3) if (_width*_height*_depth>=1024) -#endif - cimg_forXYZ(metric_mask,dx,dy,dz) { - const t weight = metric_mask(dx,dy,dz); - if (weight) { - for (int z = dz, nz = 0; z=0; --z,--nz) { // Backward scan. - for (int y = height() - 1 - dy, ny = height() - 1; y>=0; --y,--ny) { - for (int x = width() - 1 - dx, nx = width() - 1; x>=0; --x,--nx) { - const T dd = img(nx,ny,nz,0,wh) + weight; - if (dd - CImg get_distance(const T value, const CImg& metric_mask) const { - return CImg(*this,false).distance(value,metric_mask); - } - - //! Compute distance to a specified value, according to a custom metric (use dijkstra algorithm). - /** - \param value Reference value. - \param metric Field of distance potentials. - \param is_high_connectivity Tells if the algorithm uses low or high connectivity. - **/ - template - CImg& distance_dijkstra(const T value, const CImg& metric, const bool is_high_connectivity, - CImg& return_path) { - return get_distance_dijkstra(value,metric,is_high_connectivity,return_path).move_to(*this); - } - - //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \newinstance. - template - CImg::type> - get_distance_dijkstra(const T value, const CImg& metric, const bool is_high_connectivity, - CImg& return_path) const { - if (is_empty()) return return_path.assign(); - if (!is_sameXYZ(metric)) - throw CImgArgumentException(_cimg_instance - "distance_dijkstra(): image instance and metric map (%u,%u,%u,%u) " - "have incompatible dimensions.", - cimg_instance, - metric._width,metric._height,metric._depth,metric._spectrum); - typedef typename cimg::superset::type td; // Type used for computing cumulative distances. - CImg result(_width,_height,_depth,_spectrum), Q; - CImg is_queued(_width,_height,_depth,1); - if (return_path) return_path.assign(_width,_height,_depth,_spectrum); - - cimg_forC(*this,c) { - const CImg img = get_shared_channel(c); - const CImg met = metric.get_shared_channel(c%metric._spectrum); - CImg res = result.get_shared_channel(c); - CImg path = return_path?return_path.get_shared_channel(c):CImg(); - unsigned int sizeQ = 0; - - // Detect initial seeds. - is_queued.fill(0); - cimg_forXYZ(img,x,y,z) if (img(x,y,z)==value) { - Q._priority_queue_insert(is_queued,sizeQ,0,x,y,z); - res(x,y,z) = 0; - if (path) path(x,y,z) = (to)0; - } - - // Start distance propagation. - while (sizeQ) { - - // Get and remove point with minimal potential from the queue. - const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3); - const td P = (td)-Q(0,0); - Q._priority_queue_remove(sizeQ); - - // Update neighbors. - td npot = 0; - if (x-1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x-1,y,z)+P),x-1,y,z)) { - res(x-1,y,z) = npot; if (path) path(x-1,y,z) = (to)2; - } - if (x+1=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y-1,z)+P),x,y-1,z)) { - res(x,y-1,z) = npot; if (path) path(x,y-1,z) = (to)8; - } - if (y+1=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y,z-1)+P),x,y,z-1)) { - res(x,y,z-1) = npot; if (path) path(x,y,z-1) = (to)32; - } - if (z+1=0 && y-1>=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x-1,y-1,z)+P)),x-1,y-1,z)) { - res(x-1,y-1,z) = npot; if (path) path(x-1,y-1,z) = (to)10; - } - if (x+1=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x+1,y-1,z)+P)),x+1,y-1,z)) { - res(x+1,y-1,z) = npot; if (path) path(x+1,y-1,z) = (to)9; - } - if (x-1>=0 && y+1=0) { // Diagonal neighbors on slice z-1. - if (x-1>=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x-1,y,z-1)+P)),x-1,y,z-1)) { - res(x-1,y,z-1) = npot; if (path) path(x-1,y,z-1) = (to)34; - } - if (x+1=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y-1,z-1)+P)),x,y-1,z-1)) { - res(x,y-1,z-1) = npot; if (path) path(x,y-1,z-1) = (to)40; - } - if (y+1=0 && y-1>=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x-1,y-1,z-1)+P)),x-1,y-1,z-1)) { - res(x-1,y-1,z-1) = npot; if (path) path(x-1,y-1,z-1) = (to)42; - } - if (x+1=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x+1,y-1,z-1)+P)),x+1,y-1,z-1)) { - res(x+1,y-1,z-1) = npot; if (path) path(x+1,y-1,z-1) = (to)41; - } - if (x-1>=0 && y+1=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x-1,y,z+1)+P)),x-1,y,z+1)) { - res(x-1,y,z+1) = npot; if (path) path(x-1,y,z+1) = (to)18; - } - if (x+1=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y-1,z+1)+P)),x,y-1,z+1)) { - res(x,y-1,z+1) = npot; if (path) path(x,y-1,z+1) = (to)24; - } - if (y+1=0 && y-1>=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x-1,y-1,z+1)+P)),x-1,y-1,z+1)) { - res(x-1,y-1,z+1) = npot; if (path) path(x-1,y-1,z+1) = (to)26; - } - if (x+1=0 && - Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x+1,y-1,z+1)+P)),x+1,y-1,z+1)) { - res(x+1,y-1,z+1) = npot; if (path) path(x+1,y-1,z+1) = (to)25; - } - if (x-1>=0 && y+1 - CImg& distance_dijkstra(const T value, const CImg& metric, - const bool is_high_connectivity=false) { - return get_distance_dijkstra(value,metric,is_high_connectivity).move_to(*this); - } - - //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \newinstance. - template - CImg get_distance_dijkstra(const T value, const CImg& metric, - const bool is_high_connectivity=false) const { - CImg return_path; - return get_distance_dijkstra(value,metric,is_high_connectivity,return_path); - } - - //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm). - /** - \param value Reference value. - \param metric Field of distance potentials. - **/ - template - CImg& distance_eikonal(const T value, const CImg& metric) { - return get_distance_eikonal(value,metric).move_to(*this); - } - - //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm). - template - CImg get_distance_eikonal(const T value, const CImg& metric) const { - if (is_empty()) return *this; - if (!is_sameXYZ(metric)) - throw CImgArgumentException(_cimg_instance - "distance_eikonal(): image instance and metric map (%u,%u,%u,%u) have " - "incompatible dimensions.", - cimg_instance, - metric._width,metric._height,metric._depth,metric._spectrum); - CImg result(_width,_height,_depth,_spectrum,cimg::type::max()), Q; - CImg state(_width,_height,_depth); // -1=far away, 0=narrow, 1=frozen. - -#ifdef cimg_use_openmp -#pragma omp parallel for if (_spectrum>=2) firstprivate(Q,state) -#endif - cimg_forC(*this,c) { - const CImg img = get_shared_channel(c); - const CImg met = metric.get_shared_channel(c%metric._spectrum); - CImg res = result.get_shared_channel(c); - unsigned int sizeQ = 0; - state.fill(-1); - - // Detect initial seeds. - Tfloat *ptr1 = res._data; char *ptr2 = state._data; - cimg_for(img,ptr0,T) { if (*ptr0==value) { *ptr1 = 0; *ptr2 = 1; } ++ptr1; ++ptr2; } - - // Initialize seeds neighbors. - ptr2 = state._data; - cimg_forXYZ(img,x,y,z) if (*(ptr2++)==1) { - if (x-1>=0 && state(x-1,y,z)==-1) { - const Tfloat dist = res(x-1,y,z) = __distance_eikonal(res,met(x-1,y,z),x-1,y,z); - Q._eik_priority_queue_insert(state,sizeQ,-dist,x-1,y,z); - } - if (x+1=0 && state(x,y-1,z)==-1) { - const Tfloat dist = res(x,y-1,z) = __distance_eikonal(res,met(x,y-1,z),x,y-1,z); - Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y-1,z); - } - if (y+1=0 && state(x,y,z-1)==-1) { - const Tfloat dist = res(x,y,z-1) = __distance_eikonal(res,met(x,y,z-1),x,y,z-1); - Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z-1); - } - if (z+1=0) { - if (x-1>=0 && state(x-1,y,z)!=1) { - const Tfloat dist = __distance_eikonal(res,met(x-1,y,z),x-1,y,z); - if (dist=0 && state(x,y-1,z)!=1) { - const Tfloat dist = __distance_eikonal(res,met(x,y-1,z),x,y-1,z); - if (dist=0 && state(x,y,z-1)!=1) { - const Tfloat dist = __distance_eikonal(res,met(x,y,z-1),x,y,z-1); - if (dist& res, const Tfloat P, - const int x=0, const int y=0, const int z=0) const { - const T M = cimg::type::max(); - T T1 = cimg::min(x-1>=0?res(x-1,y,z):M,x+11) { // 3d. - T - T2 = cimg::min(y-1>=0?res(x,y-1,z):M,y+1=0?res(x,y,z-1):M,z+1T2) cimg::swap(T1,T2); - if (T2>T3) cimg::swap(T2,T3); - if (T1>T2) cimg::swap(T1,T2); - if (P<=0) return (Tfloat)T1; - if (T31) { // 2d. - T T2 = cimg::min(y-1>=0?res(x,y-1,z):M,y+1T2) cimg::swap(T1,T2); - if (P<=0) return (Tfloat)T1; - if (T2 - void _eik_priority_queue_insert(CImg& state, unsigned int& siz, const t value, - const unsigned int x, const unsigned int y, const unsigned int z) { - if (state(x,y,z)>0) return; - state(x,y,z) = 0; - if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); } - (*this)(siz-1,0) = (T)value; (*this)(siz-1,1) = (T)x; (*this)(siz-1,2) = (T)y; (*this)(siz-1,3) = (T)z; - for (unsigned int pos = siz - 1, par = 0; pos && value>(*this)(par=(pos+1)/2-1,0); pos = par) { - cimg::swap((*this)(pos,0),(*this)(par,0)); cimg::swap((*this)(pos,1),(*this)(par,1)); - cimg::swap((*this)(pos,2),(*this)(par,2)); cimg::swap((*this)(pos,3),(*this)(par,3)); - } - } - - //! Compute distance function to 0-valued isophotes, using the Eikonal PDE. - /** - \param nb_iterations Number of PDE iterations. - \param band_size Size of the narrow band. - \param time_step Time step of the PDE iterations. - **/ - CImg& distance_eikonal(const unsigned int nb_iterations, const float band_size=0, const float time_step=0.5f) { - if (is_empty()) return *this; - CImg velocity(*this); - for (unsigned int iteration = 0; iteration1) { // 3d - CImg_3x3x3(I,Tfloat); - cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) if (band_size<=0 || cimg::abs(Iccc)0?(Incc - Iccc):(Iccc - Ipcc), - iy = gy*sgn>0?(Icnc - Iccc):(Iccc - Icpc), - iz = gz*sgn>0?(Iccn - Iccc):(Iccc - Iccp), - ng = (Tfloat)(1e-5f + std::sqrt(gx*gx + gy*gy + gz*gz)), - ngx = gx/ng, - ngy = gy/ng, - ngz = gz/ng, - veloc = sgn*(ngx*ix + ngy*iy + ngz*iz - 1); - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } else *(ptrd++) = 0; - } else { // 2d version - CImg_3x3(I,Tfloat); - cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,Tfloat) if (band_size<=0 || cimg::abs(Icc)0?(Inc - Icc):(Icc - Ipc), - iy = gy*sgn>0?(Icn - Icc):(Icc - Icp), - ng = (Tfloat)(1e-5f + std::sqrt(gx*gx + gy*gy)), - ngx = gx/ng, - ngy = gy/ng, - veloc = sgn*(ngx*ix + ngy*iy - 1); - *(ptrd++) = veloc; - if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; - } else *(ptrd++) = 0; - } - if (veloc_max>0) *this+=(velocity*=time_step/veloc_max); - } - return *this; - } - - //! Compute distance function to 0-valued isophotes, using the Eikonal PDE \newinstance. - CImg get_distance_eikonal(const unsigned int nb_iterations, const float band_size=0, - const float time_step=0.5f) const { - return CImg(*this,false).distance_eikonal(nb_iterations,band_size,time_step); - } - - //! Compute Haar multiscale wavelet transform. - /** - \param axis Axis considered for the transform. - \param invert Set inverse of direct transform. - \param nb_scales Number of scales used for the transform. - **/ - CImg& haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) { - return get_haar(axis,invert,nb_scales).move_to(*this); - } - - //! Compute Haar multiscale wavelet transform \newinstance. - CImg get_haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) const { - if (is_empty() || !nb_scales) return +*this; - CImg res; - const Tfloat sqrt2 = std::sqrt(2); - if (nb_scales==1) { - switch (cimg::uncase(axis)) { // Single scale transform - case 'x' : { - const unsigned int w = _width/2; - if (w) { - if ((w%2) && w!=1) - throw CImgInstanceException(_cimg_instance - "haar(): Sub-image width %u is not even.", - cimg_instance, - w); - - res.assign(_width,_height,_depth,_spectrum); - if (invert) cimg_forYZC(*this,y,z,c) { // Inverse transform along X - for (unsigned int x = 0, xw = w, x2 = 0; x& haar(const bool invert=false, const unsigned int nb_scales=1) { - return get_haar(invert,nb_scales).move_to(*this); - } - - //! Compute Haar multiscale wavelet transform \newinstance. - CImg get_haar(const bool invert=false, const unsigned int nb_scales=1) const { - CImg res; - if (nb_scales==1) { // Single scale transform - if (_width>1) get_haar('x',invert,1).move_to(res); - if (_height>1) { if (res) res.haar('y',invert,1); else get_haar('y',invert,1).move_to(res); } - if (_depth>1) { if (res) res.haar('z',invert,1); else get_haar('z',invert,1).move_to(res); } - if (res) return res; - } else { // Multi-scale transform - if (invert) { // Inverse transform - res.assign(*this); - if (_width>1) { - if (_height>1) { - if (_depth>1) { - unsigned int w = _width, h = _height, d = _depth; - for (unsigned int s = 1; w && h && d && s1) { - unsigned int w = _width, d = _depth; - for (unsigned int s = 1; w && d && s1) { - if (_depth>1) { - unsigned int h = _height, d = _depth; - for (unsigned int s = 1; h && d && s1) { - unsigned int d = _depth; - for (unsigned int s = 1; d && s1) { - if (_height>1) { - if (_depth>1) - for (unsigned int s = 1, w = _width/2, h = _height/2, d = _depth/2; w && h && d && s1) for (unsigned int s = 1, w = _width/2, d = _depth/2; w && d && s1) { - if (_depth>1) - for (unsigned int s = 1, h = _height/2, d = _depth/2; h && d && s1) for (unsigned int s = 1, d = _depth/2; d && s get_FFT(const char axis, const bool is_invert=false) const { - CImgList res(*this,CImg()); - CImg::FFT(res[0],res[1],axis,is_invert); - return res; - } - - //! Compute n-d Fast Fourier Transform. - /* - \param is_invert Tells if the forward (\c false) or inverse (\c true) FFT is computed. - **/ - CImgList get_FFT(const bool is_invert=false) const { - CImgList res(*this,CImg()); - CImg::FFT(res[0],res[1],is_invert); - return res; - } - - //! Compute 1d Fast Fourier Transform, along a specified axis. - /** - \param[in,out] real Real part of the pixel values. - \param[in,out] imag Imaginary part of the pixel values. - \param axis Axis along which the FFT is computed. - \param is_invert Tells if the forward (\c false) or inverse (\c true) FFT is computed. - **/ - static void FFT(CImg& real, CImg& imag, const char axis, const bool is_invert=false) { - if (!real) - throw CImgInstanceException("CImg<%s>::FFT(): Specified real part is empty.", - pixel_type()); - - if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,0); - if (!real.is_sameXYZC(imag)) - throw CImgInstanceException("CImg<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and " - "imaginary part (%u,%u,%u,%u,%p) have different dimensions.", - pixel_type(), - real._width,real._height,real._depth,real._spectrum,real._data, - imag._width,imag._height,imag._depth,imag._spectrum,imag._data); -#ifdef cimg_use_fftw3 - cimg::mutex(12); - fftw_complex *data_in; - fftw_plan data_plan; - - switch (cimg::uncase(axis)) { - case 'x' : { // Fourier along X, using FFTW library. - data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width); - if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " - "for computing FFT of image (%u,%u,%u,%u) along the X-axis.", - pixel_type(), - cimg::strbuffersize(sizeof(fftw_complex)*real._width), - real._width,real._height,real._depth,real._spectrum); - - data_plan = fftw_plan_dft_1d(real._width,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); - cimg_forYZC(real,y,z,c) { - T *ptrr = real.data(0,y,z,c), *ptri = imag.data(0,y,z,c); - double *ptrd = (double*)data_in; - cimg_forX(real,x) { *(ptrd++) = (double)*(ptrr++); *(ptrd++) = (double)*(ptri++); } - fftw_execute(data_plan); - const unsigned int fact = real._width; - if (is_invert) cimg_forX(real,x) { *(--ptri) = (T)(*(--ptrd)/fact); *(--ptrr) = (T)(*(--ptrd)/fact); } - else cimg_forX(real,x) { *(--ptri) = (T)*(--ptrd); *(--ptrr) = (T)*(--ptrd); } - } - } break; - case 'y' : { // Fourier along Y, using FFTW library. - data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * real._height); - if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " - "for computing FFT of image (%u,%u,%u,%u) along the Y-axis.", - pixel_type(), - cimg::strbuffersize(sizeof(fftw_complex)*real._height), - real._width,real._height,real._depth,real._spectrum); - - data_plan = fftw_plan_dft_1d(real._height,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); - const unsigned int off = real._width; - cimg_forXZC(real,x,z,c) { - T *ptrr = real.data(x,0,z,c), *ptri = imag.data(x,0,z,c); - double *ptrd = (double*)data_in; - cimg_forY(real,y) { *(ptrd++) = (double)*ptrr; *(ptrd++) = (double)*ptri; ptrr+=off; ptri+=off; } - fftw_execute(data_plan); - const unsigned int fact = real._height; - if (is_invert) - cimg_forY(real,y) { ptrr-=off; ptri-=off; *ptri = (T)(*(--ptrd)/fact); *ptrr = (T)(*(--ptrd)/fact); } - else cimg_forY(real,y) { ptrr-=off; ptri-=off; *ptri = (T)*(--ptrd); *ptrr = (T)*(--ptrd); } - } - } break; - case 'z' : { // Fourier along Z, using FFTW library. - data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * real._depth); - if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " - "for computing FFT of image (%u,%u,%u,%u) along the Z-axis.", - pixel_type(), - cimg::strbuffersize(sizeof(fftw_complex)*real._depth), - real._width,real._height,real._depth,real._spectrum); - - data_plan = fftw_plan_dft_1d(real._depth,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); - const unsigned long off = (unsigned long)real._width*real._height; - cimg_forXYC(real,x,y,c) { - T *ptrr = real.data(x,y,0,c), *ptri = imag.data(x,y,0,c); - double *ptrd = (double*)data_in; - cimg_forZ(real,z) { *(ptrd++) = (double)*ptrr; *(ptrd++) = (double)*ptri; ptrr+=off; ptri+=off; } - fftw_execute(data_plan); - const unsigned int fact = real._depth; - if (is_invert) - cimg_forZ(real,z) { ptrr-=off; ptri-=off; *ptri = (T)(*(--ptrd)/fact); *ptrr = (T)(*(--ptrd)/fact); } - else cimg_forZ(real,z) { ptrr-=off; ptri-=off; *ptri = (T)*(--ptrd); *ptrr = (T)*(--ptrd); } - } - } break; - default : { // Fourier along C, using FFTW library. - data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * real._spectrum); - if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " - "for computing FFT of image (%u,%u,%u,%u) along the C-axis.", - pixel_type(), - cimg::strbuffersize(sizeof(fftw_complex)*real._spectrum), - real._width,real._height,real._depth,real._spectrum); - - data_plan = fftw_plan_dft_1d(real._spectrum,data_in,data_in,is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); - const unsigned long off = (unsigned long)real._width*real._height*real._depth; - cimg_forXYZ(real,x,y,z) { - T *ptrr = real.data(x,y,z,0), *ptri = imag.data(x,y,z,0); - double *ptrd = (double*)data_in; - cimg_forC(real,c) { *(ptrd++) = (double)*ptrr; *(ptrd++) = (double)*ptri; ptrr+=off; ptri+=off; } - fftw_execute(data_plan); - const unsigned int fact = real._spectrum; - if (is_invert) - cimg_forC(real,c) { ptrr-=off; ptri-=off; *ptri = (T)(*(--ptrd)/fact); *ptrr = (T)(*(--ptrd)/fact); } - else cimg_forC(real,c) { ptrr-=off; ptri-=off; *ptri = (T)*(--ptrd); *ptrr = (T)*(--ptrd); } - } - } - } - fftw_destroy_plan(data_plan); - fftw_free(data_in); - cimg::mutex(12,0); -#else - switch (cimg::uncase(axis)) { - case 'x' : { // Fourier along X, using built-in functions. - const unsigned int N = real._width, N2 = (N>>1); - if (((N-1)&N) && N!=1) - throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) " - "have non 2^N dimension along the X-axis.", - pixel_type(), - real._width,real._height,real._depth,real._spectrum); - - for (unsigned int i = 0, j = 0; ii) cimg_forYZC(real,y,z,c) { - cimg::swap(real(i,y,z,c),real(j,y,z,c)); cimg::swap(imag(i,y,z,c),imag(j,y,z,c)); - if (j=m; j-=m, m = n, n>>=1) {} - } - for (unsigned int delta = 2; delta<=N; delta<<=1) { - const unsigned int delta2 = (delta>>1); - for (unsigned int i = 0; i>1); - if (((N-1)&N) && N!=1) - throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) " - "have non 2^N dimension along the Y-axis.", - pixel_type(), - real._width,real._height,real._depth,real._spectrum); - - for (unsigned int i = 0, j = 0; ii) cimg_forXZC(real,x,z,c) { - cimg::swap(real(x,i,z,c),real(x,j,z,c)); cimg::swap(imag(x,i,z,c),imag(x,j,z,c)); - if (j=m; j-=m, m = n, n>>=1) {} - } - for (unsigned int delta = 2; delta<=N; delta<<=1) { - const unsigned int delta2 = (delta>>1); - for (unsigned int i = 0; i>1); - if (((N-1)&N) && N!=1) - throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) " - "have non 2^N dimension along the Z-axis.", - pixel_type(), - real._width,real._height,real._depth,real._spectrum); - - for (unsigned int i = 0, j = 0; ii) cimg_forXYC(real,x,y,c) { - cimg::swap(real(x,y,i,c),real(x,y,j,c)); cimg::swap(imag(x,y,i,c),imag(x,y,j,c)); - if (j=m; j-=m, m = n, n>>=1) {} - } - for (unsigned int delta = 2; delta<=N; delta<<=1) { - const unsigned int delta2 = (delta>>1); - for (unsigned int i = 0; i::FFT(): Invalid specified axis '%c' for real and imaginary parts " - "(%u,%u,%u,%u) " - "(should be { x | y | z }).", - pixel_type(),axis, - real._width,real._height,real._depth,real._spectrum); - } -#endif - } - - //! Compute n-d Fast Fourier Transform. - /** - \param[in,out] real Real part of the pixel values. - \param[in,out] imag Imaginary part of the pixel values. - \param is_invert Tells if the forward (\c false) or inverse (\c true) FFT is computed. - \param nb_threads Number of parallel threads used for the computation. - Use \c 0 to set this to the number of available cpus. - **/ - static void FFT(CImg& real, CImg& imag, const bool is_invert=false, const unsigned int nb_threads=0) { - if (!real) - throw CImgInstanceException("CImgList<%s>::FFT(): Empty specified real part.", - pixel_type()); - - if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,0); - if (!real.is_sameXYZC(imag)) - throw CImgInstanceException("CImgList<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and " - "imaginary part (%u,%u,%u,%u,%p) have different dimensions.", - pixel_type(), - real._width,real._height,real._depth,real._spectrum,real._data, - imag._width,imag._height,imag._depth,imag._spectrum,imag._data); - -#ifdef cimg_use_fftw3 - cimg::mutex(12); -#ifndef cimg_use_fftw3_singlethread - const unsigned int _nb_threads = nb_threads?nb_threads:cimg::nb_cpus(); - static int fftw_st = fftw_init_threads(); - cimg::unused(fftw_st); - fftw_plan_with_nthreads(_nb_threads); -#else - cimg::unused(nb_threads); -#endif - fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth); - if (!data_in) throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) " - "for computing FFT of image (%u,%u,%u,%u).", - pixel_type(), - cimg::strbuffersize(sizeof(fftw_complex)*real._width* - real._height*real._depth*real._spectrum), - real._width,real._height,real._depth,real._spectrum); - - fftw_plan data_plan; - const unsigned long w = (unsigned long)real._width, wh = w*real._height, whd = wh*real._depth; - data_plan = fftw_plan_dft_3d(real._width,real._height,real._depth,data_in,data_in, - is_invert?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE); - cimg_forC(real,c) { - T *ptrr = real.data(0,0,0,c), *ptri = imag.data(0,0,0,c); - double *ptrd = (double*)data_in; - for (unsigned int x = 0; x1) FFT(real,imag,'z',is_invert); - if (real._height>1) FFT(real,imag,'y',is_invert); - if (real._width>1) FFT(real,imag,'x',is_invert); -#endif - } - - //@} - //------------------------------------- - // - //! \name 3d Objects Management - //@{ - //------------------------------------- - - //! Shift 3d object's vertices. - /** - \param tx X-coordinate of the 3d displacement vector. - \param ty Y-coordinate of the 3d displacement vector. - \param tz Z-coordinate of the 3d displacement vector. - **/ - CImg& shift_object3d(const float tx, const float ty=0, const float tz=0) { - if (_height!=3 || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "shift_object3d(): Instance is not a set of 3d vertices.", - cimg_instance); - - get_shared_row(0)+=tx; get_shared_row(1)+=ty; get_shared_row(2)+=tz; - return *this; - } - - //! Shift 3d object's vertices \newinstance. - CImg get_shift_object3d(const float tx, const float ty=0, const float tz=0) const { - return CImg(*this,false).shift_object3d(tx,ty,tz); - } - - //! Shift 3d object's vertices, so that it becomes centered. - /** - \note The object center is computed as its barycenter. - **/ - CImg& shift_object3d() { - if (_height!=3 || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "shift_object3d(): Instance is not a set of 3d vertices.", - cimg_instance); - - CImg xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2); - float - xm, xM = (float)xcoords.max_min(xm), - ym, yM = (float)ycoords.max_min(ym), - zm, zM = (float)zcoords.max_min(zm); - xcoords-=(xm + xM)/2; ycoords-=(ym + yM)/2; zcoords-=(zm + zM)/2; - return *this; - } - - //! Shift 3d object's vertices, so that it becomes centered \newinstance. - CImg get_shift_object3d() const { - return CImg(*this,false).shift_object3d(); - } - - //! Resize 3d object. - /** - \param sx Width of the 3d object's bounding box. - \param sy Height of the 3d object's bounding box. - \param sz Depth of the 3d object's bounding box. - **/ - CImg& resize_object3d(const float sx, const float sy=-100, const float sz=-100) { - if (_height!=3 || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "resize_object3d(): Instance is not a set of 3d vertices.", - cimg_instance); - - CImg xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2); - float - xm, xM = (float)xcoords.max_min(xm), - ym, yM = (float)ycoords.max_min(ym), - zm, zM = (float)zcoords.max_min(zm); - if (xm0) xcoords*=sx/(xM-xm); else xcoords*=-sx/100; } - if (ym0) ycoords*=sy/(yM-ym); else ycoords*=-sy/100; } - if (zm0) zcoords*=sz/(zM-zm); else zcoords*=-sz/100; } - return *this; - } - - //! Resize 3d object \newinstance. - CImg get_resize_object3d(const float sx, const float sy=-100, const float sz=-100) const { - return CImg(*this,false).resize_object3d(sx,sy,sz); - } - - //! Resize 3d object to unit size. - CImg resize_object3d() { - if (_height!=3 || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "resize_object3d(): Instance is not a set of 3d vertices.", - cimg_instance); - - CImg xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2); - float - xm, xM = (float)xcoords.max_min(xm), - ym, yM = (float)ycoords.max_min(ym), - zm, zM = (float)zcoords.max_min(zm); - const float dx = xM - xm, dy = yM - ym, dz = zM - zm, dmax = cimg::max(dx,dy,dz); - if (dmax>0) { xcoords/=dmax; ycoords/=dmax; zcoords/=dmax; } - return *this; - } - - //! Resize 3d object to unit size \newinstance. - CImg get_resize_object3d() const { - return CImg(*this,false).resize_object3d(); - } - - //! Merge two 3d objects together. - /** - \param[in,out] primitives Primitives data of the current 3d object. - \param obj_vertices Vertices data of the additional 3d object. - \param obj_primitives Primitives data of the additional 3d object. - **/ - template - CImg& append_object3d(CImgList& primitives, const CImg& obj_vertices, - const CImgList& obj_primitives) { - if (!obj_vertices || !obj_primitives) return *this; - if (obj_vertices._height!=3 || obj_vertices._depth>1 || obj_vertices._spectrum>1) - throw CImgInstanceException(_cimg_instance - "append_object3d(): Specified vertice image (%u,%u,%u,%u,%p) is not a " - "set of 3d vertices.", - cimg_instance, - obj_vertices._width,obj_vertices._height, - obj_vertices._depth,obj_vertices._spectrum,obj_vertices._data); - - if (is_empty()) { primitives.assign(obj_primitives); return assign(obj_vertices); } - if (_height!=3 || _depth>1 || _spectrum>1) - throw CImgInstanceException(_cimg_instance - "append_object3d(): Instance is not a set of 3d vertices.", - cimg_instance); - - const unsigned int P = _width; - append(obj_vertices,'x'); - const unsigned int N = primitives._width; - primitives.insert(obj_primitives); - for (unsigned int i = N; i &p = primitives[i]; - switch (p.size()) { - case 1 : p[0]+=P; break; // Point. - case 5 : p[0]+=P; p[1]+=P; break; // Sphere. - case 2 : case 6 : p[0]+=P; p[1]+=P; break; // Segment. - case 3 : case 9 : p[0]+=P; p[1]+=P; p[2]+=P; break; // Triangle. - case 4 : case 12 : p[0]+=P; p[1]+=P; p[2]+=P; p[3]+=P; break; // Rectangle. - } - } - return *this; - } - - //! Texturize primitives of a 3d object. - /** - \param[in,out] primitives Primitives data of the 3d object. - \param[in,out] colors Colors data of the 3d object. - \param texture Texture image to map to 3d object. - \param coords Texture-mapping coordinates. - **/ - template - const CImg& texturize_object3d(CImgList& primitives, CImgList& colors, - const CImg& texture, const CImg& coords=CImg::empty()) const { - if (is_empty()) return *this; - if (_height!=3) - throw CImgInstanceException(_cimg_instance - "texturize_object3d(): image instance is not a set of 3d points.", - cimg_instance); - if (coords && (coords._width!=_width || coords._height!=2)) - throw CImgArgumentException(_cimg_instance - "texturize_object3d(): Invalid specified texture coordinates (%u,%u,%u,%u,%p).", - cimg_instance, - coords._width,coords._height,coords._depth,coords._spectrum,coords._data); - CImg _coords; - if (!coords) { // If no texture coordinates specified, do a default XY-projection. - _coords.assign(_width,2); - float - xmin, xmax = (float)get_shared_row(0).max_min(xmin), - ymin, ymax = (float)get_shared_row(1).max_min(ymin), - dx = xmax>xmin?xmax-xmin:1, - dy = ymax>ymin?ymax-ymin:1; - cimg_forX(*this,p) { - _coords(p,0) = (unsigned int)(((*this)(p,0)-xmin)*(texture._width-1)/dx); - _coords(p,1) = (unsigned int)(((*this)(p,1)-ymin)*(texture._height-1)/dy); - } - } else _coords = coords; - - int texture_ind = -1; - cimglist_for(primitives,l) { - CImg &p = primitives[l]; - const unsigned int siz = p.size(); - switch (siz) { - case 1 : { // Point. - const unsigned int - i0 = (unsigned int)p[0], - x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1); - texture.get_vector_at(x0,y0).move_to(colors[l]); - } break; - case 2 : case 6 : { // Line. - const unsigned int - i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], - x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1), - x1 = (unsigned int)_coords(i1,0), y1 = (unsigned int)_coords(i1,1); - if (texture_ind<0) colors[texture_ind=l] = texture; else colors[l].assign(colors[texture_ind],true); - CImg::vector(i0,i1,x0,y0,x1,y1).move_to(p); - } break; - case 3 : case 9 : { // Triangle. - const unsigned int - i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2], - x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1), - x1 = (unsigned int)_coords(i1,0), y1 = (unsigned int)_coords(i1,1), - x2 = (unsigned int)_coords(i2,0), y2 = (unsigned int)_coords(i2,1); - if (texture_ind<0) colors[texture_ind=l] = texture; else colors[l].assign(colors[texture_ind],true); - CImg::vector(i0,i1,i2,x0,y0,x1,y1,x2,y2).move_to(p); - } break; - case 4 : case 12 : { // Quadrangle. - const unsigned int - i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2], i3 = (unsigned int)p[3], - x0 = (unsigned int)_coords(i0,0), y0 = (unsigned int)_coords(i0,1), - x1 = (unsigned int)_coords(i1,0), y1 = (unsigned int)_coords(i1,1), - x2 = (unsigned int)_coords(i2,0), y2 = (unsigned int)_coords(i2,1), - x3 = (unsigned int)_coords(i3,0), y3 = (unsigned int)_coords(i3,1); - if (texture_ind<0) colors[texture_ind=l] = texture; else colors[l].assign(colors[texture_ind],true); - CImg::vector(i0,i1,i2,i3,x0,y0,x1,y1,x2,y2,x3,y3).move_to(p); - } break; - } - } - return *this; - } - - //! Generate a 3d elevation of the image instance. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param[out] colors The returned list of the 3d object colors. - \param elevation The input elevation map. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - const CImg img("reference.jpg"); - CImgList faces3d; - CImgList colors3d; - const CImg points3d = img.get_elevation3d(faces3d,colors3d,img.get_norm()*0.2); - CImg().display_object3d("Elevation3d",points3d,faces3d,colors3d); - \endcode - \image html ref_elevation3d.jpg - **/ - template - CImg get_elevation3d(CImgList& primitives, CImgList& colors, const CImg& elevation) const { - if (!is_sameXY(elevation) || elevation._depth>1 || elevation._spectrum>1) - throw CImgArgumentException(_cimg_instance - "get_elevation3d(): Instance and specified elevation (%u,%u,%u,%u,%p) " - "have incompatible dimensions.", - cimg_instance, - elevation._width,elevation._height,elevation._depth, - elevation._spectrum,elevation._data); - if (is_empty()) return *this; - float m, M = (float)max_min(m); - if (M==m) ++M; - colors.assign(); - const unsigned int size_x1 = _width - 1, size_y1 = _height - 1; - for (unsigned int y = 0; y1?(unsigned char)(((*this)(x,y,1) - m)*255/(M-m)):r, - b = _spectrum>2?(unsigned char)(((*this)(x,y,2) - m)*255/(M-m)):(_spectrum>1?0:r); - CImg::vector((tc)r,(tc)g,(tc)b).move_to(colors); - } - const typename CImg::_functor2d_int func(elevation); - return elevation3d(primitives,func,0,0,_width-1.0f,_height-1.0f,_width,_height); - } - - //! Generate the 3d projection planes of the image instance. - /** - \param[out] primitives Primitives data of the returned 3d object. - \param[out] colors Colors data of the returned 3d object. - \param x0 X-coordinate of the projection point. - \param y0 Y-coordinate of the projection point. - \param z0 Z-coordinate of the projection point. - \param normalize_colors Tells if the created textures have normalized colors. - **/ - template - CImg get_projections3d(CImgList& primitives, CImgList& colors, - const unsigned int x0, const unsigned int y0, const unsigned int z0, - const bool normalize_colors=false) const { - float m = 0, M = 0, delta = 1; - if (normalize_colors) { m = (float)min_max(M); delta = 255/(m==M?1:M-m); } - const unsigned int - _x0 = (x0>=_width)?_width - 1:x0, - _y0 = (y0>=_height)?_height - 1:y0, - _z0 = (z0>=_depth)?_depth - 1:z0; - CImg img_xy, img_xz, img_yz; - if (normalize_colors) { - ((get_crop(0,0,_z0,0,_width-1,_height-1,_z0,_spectrum-1)-=m)*=delta).move_to(img_xy); - ((get_crop(0,_y0,0,0,_width-1,_y0,_depth-1,_spectrum-1)-=m)*=delta).resize(_width,_depth,1,-100,-1). - move_to(img_xz); - ((get_crop(_x0,0,0,0,_x0,_height-1,_depth-1,_spectrum-1)-=m)*=delta).resize(_height,_depth,1,-100,-1). - move_to(img_yz); - } else { - get_crop(0,0,_z0,0,_width-1,_height-1,_z0,_spectrum-1).move_to(img_xy); - get_crop(0,_y0,0,0,_width-1,_y0,_depth-1,_spectrum-1).resize(_width,_depth,1,-100,-1).move_to(img_xz); - get_crop(_x0,0,0,0,_x0,_height-1,_depth-1,_spectrum-1).resize(_height,_depth,1,-100,-1).move_to(img_yz); - } - CImg points(12,3,1,1, - 0,_width-1,_width-1,0, 0,_width-1,_width-1,0, _x0,_x0,_x0,_x0, - 0,0,_height-1,_height-1, _y0,_y0,_y0,_y0, 0,_height-1,_height-1,0, - _z0,_z0,_z0,_z0, 0,0,_depth-1,_depth-1, 0,0,_depth-1,_depth-1); - primitives.assign(); - CImg::vector(0,1,2,3,0,0,img_xy._width-1,0,img_xy._width-1,img_xy._height-1,0,img_xy._height-1). - move_to(primitives); - CImg::vector(4,5,6,7,0,0,img_xz._width-1,0,img_xz._width-1,img_xz._height-1,0,img_xz._height-1). - move_to(primitives); - CImg::vector(8,9,10,11,0,0,img_yz._width-1,0,img_yz._width-1,img_yz._height-1,0,img_yz._height-1). - move_to(primitives); - colors.assign(); - img_xy.move_to(colors); - img_xz.move_to(colors); - img_yz.move_to(colors); - return points; - } - - //! Generate a isoline of the image instance as a 3d object. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param isovalue The returned list of the 3d object colors. - \param size_x The number of subdivisions along the X-axis. - \param size_y The number of subdisivions along the Y-axis. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - const CImg img("reference.jpg"); - CImgList faces3d; - const CImg points3d = img.get_isoline3d(faces3d,100); - CImg().display_object3d("Isoline3d",points3d,faces3d,colors3d); - \endcode - \image html ref_isoline3d.jpg - **/ - template - CImg get_isoline3d(CImgList& primitives, const float isovalue, - const int size_x=-100, const int size_y=-100) const { - if (_spectrum>1) - throw CImgInstanceException(_cimg_instance - "get_isoline3d(): Instance is not a scalar image.", - cimg_instance); - if (_depth>1) - throw CImgInstanceException(_cimg_instance - "get_isoline3d(): Instance is not a 2d image.", - cimg_instance); - primitives.assign(); - if (is_empty()) return *this; - CImg vertices; - if ((size_x==-100 && size_y==-100) || (size_x==width() && size_y==height())) { - const _functor2d_int func(*this); - vertices = isoline3d(primitives,func,isovalue,0,0,width()-1.0f,height()-1.0f,width(),height()); - } else { - const _functor2d_float func(*this); - vertices = isoline3d(primitives,func,isovalue,0,0,width()-1.0f,height()-1.0f,size_x,size_y); - } - return vertices; - } - - //! Generate an isosurface of the image instance as a 3d object. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param isovalue The returned list of the 3d object colors. - \param size_x Number of subdivisions along the X-axis. - \param size_y Number of subdisivions along the Y-axis. - \param size_z Number of subdisivions along the Z-axis. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - const CImg img = CImg("reference.jpg").resize(-100,-100,20); - CImgList faces3d; - const CImg points3d = img.get_isosurface3d(faces3d,100); - CImg().display_object3d("Isosurface3d",points3d,faces3d,colors3d); - \endcode - \image html ref_isosurface3d.jpg - **/ - template - CImg get_isosurface3d(CImgList& primitives, const float isovalue, - const int size_x=-100, const int size_y=-100, const int size_z=-100) const { - if (_spectrum>1) - throw CImgInstanceException(_cimg_instance - "get_isosurface3d(): Instance is not a scalar image.", - cimg_instance); - primitives.assign(); - if (is_empty()) return *this; - CImg vertices; - if ((size_x==-100 && size_y==-100 && size_z==-100) || (size_x==width() && size_y==height() && size_z==depth())) { - const _functor3d_int func(*this); - vertices = isosurface3d(primitives,func,isovalue,0,0,0,width()-1.0f,height()-1.0f,depth()-1.0f, - width(),height(),depth()); - } else { - const _functor3d_float func(*this); - vertices = isosurface3d(primitives,func,isovalue,0,0,0,width()-1.0f,height()-1.0f,depth()-1.0f, - size_x,size_y,size_z); - } - return vertices; - } - - //! Compute 3d elevation of a function as a 3d object. - /** - \param[out] primitives Primitives data of the resulting 3d object. - \param func Elevation function. Is of type float (*func)(const float x,const float y). - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param size_x Resolution of the function along the X-axis. - \param size_y Resolution of the function along the Y-axis. - **/ - template - static CImg elevation3d(CImgList& primitives, const tfunc& func, - const float x0, const float y0, const float x1, const float y1, - const int size_x=256, const int size_y=256) { - const float - nx0 = x0=0?size_x:(nx1-nx0)*-size_x/100), - nsize_x = _nsize_x?_nsize_x:1, nsize_x1 = nsize_x - 1, - _nsize_y = (unsigned int)(size_y>=0?size_y:(ny1-ny0)*-size_y/100), - nsize_y = _nsize_y?_nsize_y:1, nsize_y1 = nsize_y - 1; - if (nsize_x<2 || nsize_y<2) - throw CImgArgumentException("CImg<%s>::elevation3d(): Invalid specified size (%d,%d).", - pixel_type(), - nsize_x,nsize_y); - - CImg vertices(nsize_x*nsize_y,3); - floatT *ptr_x = vertices.data(0,0), *ptr_y = vertices.data(0,1), *ptr_z = vertices.data(0,2); - for (unsigned int y = 0; y - static CImg elevation3d(CImgList& primitives, const char *const expression, - const float x0, const float y0, const float x1, const float y1, - const int size_x=256, const int size_y=256) { - const _functor2d_expr func(expression); - return elevation3d(primitives,func,x0,y0,x1,y1,size_x,size_y); - } - - //! Compute 0-isolines of a function, as a 3d object. - /** - \param[out] primitives Primitives data of the resulting 3d object. - \param func Elevation function. Is of type float (*func)(const float x,const float y). - \param isovalue Isovalue to extract from function. - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param size_x Resolution of the function along the X-axis. - \param size_y Resolution of the function along the Y-axis. - \note Use the marching squares algorithm for extracting the isolines. - **/ - template - static CImg isoline3d(CImgList& primitives, const tfunc& func, const float isovalue, - const float x0, const float y0, const float x1, const float y1, - const int size_x=256, const int size_y=256) { - static const unsigned int edges[16] = { 0x0, 0x9, 0x3, 0xa, 0x6, 0xf, 0x5, 0xc, 0xc, - 0x5, 0xf, 0x6, 0xa, 0x3, 0x9, 0x0 }; - static const int segments[16][4] = { { -1,-1,-1,-1 }, { 0,3,-1,-1 }, { 0,1,-1,-1 }, { 1,3,-1,-1 }, - { 1,2,-1,-1 }, { 0,1,2,3 }, { 0,2,-1,-1 }, { 2,3,-1,-1 }, - { 2,3,-1,-1 }, { 0,2,-1,-1}, { 0,3,1,2 }, { 1,2,-1,-1 }, - { 1,3,-1,-1 }, { 0,1,-1,-1}, { 0,3,-1,-1}, { -1,-1,-1,-1 } }; - const unsigned int - _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)), - _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)), - nx = _nx?_nx:1, - ny = _ny?_ny:1, - nxm1 = nx - 1, - nym1 = ny - 1; - primitives.assign(); - if (!nxm1 || !nym1) return CImg(); - const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1; - CImgList vertices; - CImg indices1(nx,1,1,2,-1), indices2(nx,1,1,2); - CImg values1(nx), values2(nx); - float X = x0, Y = y0, nX = X + dx, nY = Y + dy; - - // Fill first line with values - cimg_forX(values1,x) { values1(x) = (float)func(X,Y); X+=dx; } - - // Run the marching squares algorithm - for (unsigned int yi = 0, nyi = 1; yi::vector(Xi,Y,0).move_to(vertices); - } - if ((edge&2) && indices1(nxi,1)<0) { - const float Yi = Y + (isovalue-val1)*dy/(val2-val1); - indices1(nxi,1) = vertices._width; - CImg::vector(nX,Yi,0).move_to(vertices); - } - if ((edge&4) && indices2(xi,0)<0) { - const float Xi = X + (isovalue-val3)*dx/(val2-val3); - indices2(xi,0) = vertices._width; - CImg::vector(Xi,nY,0).move_to(vertices); - } - if ((edge&8) && indices1(xi,1)<0) { - const float Yi = Y + (isovalue-val0)*dy/(val3-val0); - indices1(xi,1) = vertices._width; - CImg::vector(X,Yi,0).move_to(vertices); - } - - // Create segments - for (const int *segment = segments[configuration]; *segment!=-1; ) { - const unsigned int p0 = *(segment++), p1 = *(segment++); - const tf - i0 = (tf)(_isoline3d_indice(p0,indices1,indices2,xi,nxi)), - i1 = (tf)(_isoline3d_indice(p1,indices1,indices2,xi,nxi)); - CImg::vector(i0,i1).move_to(primitives); - } - } - } - values1.swap(values2); - indices1.swap(indices2); - } - return vertices>'x'; - } - - //! Compute isolines of a function, as a 3d object \overloading. - template - static CImg isoline3d(CImgList& primitives, const char *const expression, const float isovalue, - const float x0, const float y0, const float x1, const float y1, - const int size_x=256, const int size_y=256) { - const _functor2d_expr func(expression); - return isoline3d(primitives,func,isovalue,x0,y0,x1,y1,size_x,size_y); - } - - template - static int _isoline3d_indice(const unsigned int edge, const CImg& indices1, const CImg& indices2, - const unsigned int x, const unsigned int nx) { - switch (edge) { - case 0 : return (int)indices1(x,0); - case 1 : return (int)indices1(nx,1); - case 2 : return (int)indices2(x,0); - case 3 : return (int)indices1(x,1); - } - return 0; - } - - //! Compute isosurface of a function, as a 3d object. - /** - \param[out] primitives Primitives data of the resulting 3d object. - \param func Implicit function. Is of type float (*func)(const float x, const float y, const float z). - \param isovalue Isovalue to extract. - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param z0 Z-coordinate of the starting point. - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param z1 Z-coordinate of the ending point. - \param size_x Resolution of the elevation function along the X-axis. - \param size_y Resolution of the elevation function along the Y-axis. - \param size_z Resolution of the elevation function along the Z-axis. - \note Use the marching cubes algorithm for extracting the isosurface. - **/ - template - static CImg isosurface3d(CImgList& primitives, const tfunc& func, const float isovalue, - const float x0, const float y0, const float z0, - const float x1, const float y1, const float z1, - const int size_x=32, const int size_y=32, const int size_z=32) { - static const unsigned int edges[256] = { - 0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, - 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, - 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, - 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, - 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60, - 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, - 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, - 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc , 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, - 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0, - 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650, - 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, - 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460, - 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0, - 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230, - 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190, - 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000 - }; - - static const int triangles[256][16] = { - { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1 }, - { 8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1 }, - { 3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1 }, - { 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1 }, - { 4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1 }, - { 9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1 }, - { 10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1 }, - { 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1 }, - { 5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1 }, - { 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1 }, - { 2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1 }, - { 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1 }, - { 11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1 }, - { 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1 }, - { 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1 }, - { 11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1 }, - { 2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1 }, - { 6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1 }, - { 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1 }, - { 6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1 }, - { 6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1 }, - { 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1 }, - { 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1 }, - { 3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1 }, - { 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1 }, - { 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1 }, - { 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1 }, - { 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1 }, - { 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1 }, - { 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1 }, - { 10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1 }, - { 1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1 }, - { 0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1 }, - { 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1 }, - { 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1 }, - { 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1 }, - { 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1 }, - { 3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1 }, - { 6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1 }, - { 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1 }, - { 10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1 }, - { 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1 }, - { 7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1 }, - { 7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1 }, - { 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1 }, - { 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1 }, - { 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1 }, - { 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1 }, - { 0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1 }, - { 7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1 }, - { 7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1 }, - { 10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1 }, - { 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1 }, - { 7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1 }, - { 6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1 }, - { 6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1 }, - { 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1 }, - { 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1 }, - { 8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1 }, - { 1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1 }, - { 10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1 }, - { 10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1 }, - { 9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1 }, - { 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1 }, - { 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1 }, - { 7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1 }, - { 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1 }, - { 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1 }, - { 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1 }, - { 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1 }, - { 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1 }, - { 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1 }, - { 6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1 }, - { 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1 }, - { 6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1 }, - { 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1 }, - { 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1 }, - { 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1 }, - { 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1 }, - { 9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1 }, - { 1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1 }, - { 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1 }, - { 0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1 }, - { 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1 }, - { 11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1 }, - { 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1 }, - { 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1 }, - { 2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1 }, - { 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1 }, - { 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1 }, - { 1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1 }, - { 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1 }, - { 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1 }, - { 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1 }, - { 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1 }, - { 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1 }, - { 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1 }, - { 9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1 }, - { 5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1 }, - { 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1 }, - { 8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1 }, - { 9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1 }, - { 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1 }, - { 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1 }, - { 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1 }, - { 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1 }, - { 11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1 }, - { 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1 }, - { 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1 }, - { 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1 }, - { 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1 }, - { 1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1 }, - { 4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1 }, - { 0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1 }, - { 9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1 }, - { 1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { 0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, - { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } - }; - - const unsigned int - _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)), - _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)), - _nz = (unsigned int)(size_z>=0?size_z:cimg::round((z1-z0)*-size_z/100 + 1)), - nx = _nx?_nx:1, - ny = _ny?_ny:1, - nz = _nz?_nz:1, - nxm1 = nx - 1, - nym1 = ny - 1, - nzm1 = nz - 1; - primitives.assign(); - if (!nxm1 || !nym1 || !nzm1) return CImg(); - const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1, dz = (z1 - z0)/nzm1; - CImgList vertices; - CImg indices1(nx,ny,1,3,-1), indices2(indices1); - CImg values1(nx,ny), values2(nx,ny); - float X = 0, Y = 0, Z = 0, nX = 0, nY = 0, nZ = 0; - - // Fill the first plane with function values - Y = y0; - cimg_forY(values1,y) { - X = x0; - cimg_forX(values1,x) { values1(x,y) = (float)func(X,Y,z0); X+=dx; } - Y+=dy; - } - - // Run Marching Cubes algorithm - Z = z0; nZ = Z + dz; - for (unsigned int zi = 0; zi::vector(Xi,Y,Z).move_to(vertices); - } - if ((edge&2) && indices1(nxi,yi,1)<0) { - const float Yi = Y + (isovalue-val1)*dy/(val2-val1); - indices1(nxi,yi,1) = vertices._width; - CImg::vector(nX,Yi,Z).move_to(vertices); - } - if ((edge&4) && indices1(xi,nyi,0)<0) { - const float Xi = X + (isovalue-val3)*dx/(val2-val3); - indices1(xi,nyi,0) = vertices._width; - CImg::vector(Xi,nY,Z).move_to(vertices); - } - if ((edge&8) && indices1(xi,yi,1)<0) { - const float Yi = Y + (isovalue-val0)*dy/(val3-val0); - indices1(xi,yi,1) = vertices._width; - CImg::vector(X,Yi,Z).move_to(vertices); - } - if ((edge&16) && indices2(xi,yi,0)<0) { - const float Xi = X + (isovalue-val4)*dx/(val5-val4); - indices2(xi,yi,0) = vertices._width; - CImg::vector(Xi,Y,nZ).move_to(vertices); - } - if ((edge&32) && indices2(nxi,yi,1)<0) { - const float Yi = Y + (isovalue-val5)*dy/(val6-val5); - indices2(nxi,yi,1) = vertices._width; - CImg::vector(nX,Yi,nZ).move_to(vertices); - } - if ((edge&64) && indices2(xi,nyi,0)<0) { - const float Xi = X + (isovalue-val7)*dx/(val6-val7); - indices2(xi,nyi,0) = vertices._width; - CImg::vector(Xi,nY,nZ).move_to(vertices); - } - if ((edge&128) && indices2(xi,yi,1)<0) { - const float Yi = Y + (isovalue-val4)*dy/(val7-val4); - indices2(xi,yi,1) = vertices._width; - CImg::vector(X,Yi,nZ).move_to(vertices); - } - if ((edge&256) && indices1(xi,yi,2)<0) { - const float Zi = Z+ (isovalue-val0)*dz/(val4-val0); - indices1(xi,yi,2) = vertices._width; - CImg::vector(X,Y,Zi).move_to(vertices); - } - if ((edge&512) && indices1(nxi,yi,2)<0) { - const float Zi = Z + (isovalue-val1)*dz/(val5-val1); - indices1(nxi,yi,2) = vertices._width; - CImg::vector(nX,Y,Zi).move_to(vertices); - } - if ((edge&1024) && indices1(nxi,nyi,2)<0) { - const float Zi = Z + (isovalue-val2)*dz/(val6-val2); - indices1(nxi,nyi,2) = vertices._width; - CImg::vector(nX,nY,Zi).move_to(vertices); - } - if ((edge&2048) && indices1(xi,nyi,2)<0) { - const float Zi = Z + (isovalue-val3)*dz/(val7-val3); - indices1(xi,nyi,2) = vertices._width; - CImg::vector(X,nY,Zi).move_to(vertices); - } - - // Create triangles - for (const int *triangle = triangles[configuration]; *triangle!=-1; ) { - const unsigned int p0 = *(triangle++), p1 = *(triangle++), p2 = *(triangle++); - const tf - i0 = (tf)(_isosurface3d_indice(p0,indices1,indices2,xi,yi,nxi,nyi)), - i1 = (tf)(_isosurface3d_indice(p1,indices1,indices2,xi,yi,nxi,nyi)), - i2 = (tf)(_isosurface3d_indice(p2,indices1,indices2,xi,yi,nxi,nyi)); - CImg::vector(i0,i2,i1).move_to(primitives); - } - } - } - } - cimg::swap(values1,values2); - cimg::swap(indices1,indices2); - } - return vertices>'x'; - } - - //! Compute isosurface of a function, as a 3d object \overloading. - template - static CImg isosurface3d(CImgList& primitives, const char *const expression, const float isovalue, - const float x0, const float y0, const float z0, - const float x1, const float y1, const float z1, - const int dx=32, const int dy=32, const int dz=32) { - const _functor3d_expr func(expression); - return isosurface3d(primitives,func,isovalue,x0,y0,z0,x1,y1,z1,dx,dy,dz); - } - - template - static int _isosurface3d_indice(const unsigned int edge, const CImg& indices1, const CImg& indices2, - const unsigned int x, const unsigned int y, - const unsigned int nx, const unsigned int ny) { - switch (edge) { - case 0 : return indices1(x,y,0); - case 1 : return indices1(nx,y,1); - case 2 : return indices1(x,ny,0); - case 3 : return indices1(x,y,1); - case 4 : return indices2(x,y,0); - case 5 : return indices2(nx,y,1); - case 6 : return indices2(x,ny,0); - case 7 : return indices2(x,y,1); - case 8 : return indices1(x,y,2); - case 9 : return indices1(nx,y,2); - case 10 : return indices1(nx,ny,2); - case 11 : return indices1(x,ny,2); - } - return 0; - } - - // Define functors for accessing image values (used in previous functions). - struct _functor2d_int { - const CImg& ref; - _functor2d_int(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y) const { - return (float)ref((int)x,(int)y); - } - }; - - struct _functor2d_float { - const CImg& ref; - _functor2d_float(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y) const { - return (float)ref._linear_atXY(x,y); - } - }; - - struct _functor2d_expr { - _cimg_math_parser *mp; - _functor2d_expr(const char *const expr):mp(0) { mp = new _cimg_math_parser(CImg::empty(),expr,0); } - ~_functor2d_expr() { delete mp; } - float operator()(const float x, const float y) const { - return (float)(*mp)(x,y,0,0); - } - }; - - struct _functor3d_int { - const CImg& ref; - _functor3d_int(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y, const float z) const { - return (float)ref((int)x,(int)y,(int)z); - } - }; - - struct _functor3d_float { - const CImg& ref; - _functor3d_float(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y, const float z) const { - return (float)ref._linear_atXYZ(x,y,z); - } - }; - - struct _functor3d_expr { - _cimg_math_parser *mp; - ~_functor3d_expr() { delete mp; } - _functor3d_expr(const char *const expr):mp(0) { mp = new _cimg_math_parser(CImg::empty(),expr,0); } - float operator()(const float x, const float y, const float z) const { - return (float)(*mp)(x,y,z,0); - } - }; - - struct _functor4d_int { - const CImg& ref; - _functor4d_int(const CImg& pref):ref(pref) {} - float operator()(const float x, const float y, const float z, const unsigned int c) const { - return (float)ref((int)x,(int)y,(int)z,c); - } - }; - - //! Generate a 3d box object. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param size_x The width of the box (dimension along the X-axis). - \param size_y The height of the box (dimension along the Y-axis). - \param size_z The depth of the box (dimension along the Z-axis). - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg points3d = CImg::box3d(faces3d,10,20,30); - CImg().display_object3d("Box3d",points3d,faces3d); - \endcode - \image html ref_box3d.jpg - **/ - template - static CImg box3d(CImgList& primitives, - const float size_x=200, const float size_y=100, const float size_z=100) { - primitives.assign(6,1,4,1,1, 0,3,2,1, 4,5,6,7, 0,1,5,4, 3,7,6,2, 0,4,7,3, 1,2,6,5); - return CImg(8,3,1,1, - 0.,size_x,size_x, 0., 0.,size_x,size_x, 0., - 0., 0.,size_y,size_y, 0., 0.,size_y,size_y, - 0., 0., 0., 0.,size_z,size_z,size_z,size_z); - } - - //! Generate a 3d cone. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param radius The radius of the cone basis. - \param size_z The cone's height. - \param subdivisions The number of basis angular subdivisions. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg points3d = CImg::cone3d(faces3d,50); - CImg().display_object3d("Cone3d",points3d,faces3d); - \endcode - \image html ref_cone3d.jpg - **/ - template - static CImg cone3d(CImgList& primitives, - const float radius=50, const float size_z=100, const unsigned int subdivisions=24) { - primitives.assign(); - if (!subdivisions) return CImg(); - CImgList vertices(2,1,3,1,1, - 0.,0.,size_z, - 0.,0.,0.); - for (float delta = 360.0f/subdivisions, angle = 0; angle<360; angle+=delta) { - const float a = (float)(angle*cimg::PI/180); - CImg::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0).move_to(vertices); - } - const unsigned int nbr = vertices._width - 2; - for (unsigned int p = 0; p::vector(1,next,curr).move_to(primitives); - CImg::vector(0,curr,next).move_to(primitives); - } - return vertices>'x'; - } - - //! Generate a 3d cylinder. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param radius The radius of the cylinder basis. - \param size_z The cylinder's height. - \param subdivisions The number of basis angular subdivisions. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg points3d = CImg::cylinder3d(faces3d,50); - CImg().display_object3d("Cylinder3d",points3d,faces3d); - \endcode - \image html ref_cylinder3d.jpg - **/ - template - static CImg cylinder3d(CImgList& primitives, - const float radius=50, const float size_z=100, const unsigned int subdivisions=24) { - primitives.assign(); - if (!subdivisions) return CImg(); - CImgList vertices(2,1,3,1,1, - 0.,0.,0., - 0.,0.,size_z); - for (float delta = 360.0f/subdivisions, angle = 0; angle<360; angle+=delta) { - const float a = (float)(angle*cimg::PI/180); - CImg::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0.0f).move_to(vertices); - CImg::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),size_z).move_to(vertices); - } - const unsigned int nbr = (vertices._width - 2)/2; - for (unsigned int p = 0; p::vector(0,next,curr).move_to(primitives); - CImg::vector(1,curr+1,next+1).move_to(primitives); - CImg::vector(curr,next,next+1,curr+1).move_to(primitives); - } - return vertices>'x'; - } - - //! Generate a 3d torus. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param radius1 The large radius. - \param radius2 The small radius. - \param subdivisions1 The number of angular subdivisions for the large radius. - \param subdivisions2 The number of angular subdivisions for the small radius. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg points3d = CImg::torus3d(faces3d,20,4); - CImg().display_object3d("Torus3d",points3d,faces3d); - \endcode - \image html ref_torus3d.jpg - **/ - template - static CImg torus3d(CImgList& primitives, - const float radius1=100, const float radius2=30, - const unsigned int subdivisions1=24, const unsigned int subdivisions2=12) { - primitives.assign(); - if (!subdivisions1 || !subdivisions2) return CImg(); - CImgList vertices; - for (unsigned int v = 0; v::vector(x,y,z).move_to(vertices); - } - } - for (unsigned int vv = 0; vv::vector(svv+nu,svv+uu,snv+uu,snv+nu).move_to(primitives); - } - } - return vertices>'x'; - } - - //! Generate a 3d XY-plane. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param size_x The width of the plane (dimension along the X-axis). - \param size_y The height of the plane (dimensions along the Y-axis). - \param subdivisions_x The number of planar subdivisions along the X-axis. - \param subdivisions_y The number of planar subdivisions along the Y-axis. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg points3d = CImg::plane3d(faces3d,100,50); - CImg().display_object3d("Plane3d",points3d,faces3d); - \endcode - \image html ref_plane3d.jpg - **/ - template - static CImg plane3d(CImgList& primitives, - const float size_x=100, const float size_y=100, - const unsigned int subdivisions_x=10, const unsigned int subdivisions_y=10) { - primitives.assign(); - if (!subdivisions_x || !subdivisions_y) return CImg(); - CImgList vertices; - const unsigned int w = subdivisions_x + 1, h = subdivisions_y + 1; - const float fx = (float)size_x/w, fy = (float)size_y/h; - for (unsigned int y = 0; y::vector(fx*x,fy*y,0).move_to(vertices); - for (unsigned int y = 0; y::vector(off1,off4,off3,off2).move_to(primitives); - } - return vertices>'x'; - } - - //! Generate a 3d sphere. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param radius The radius of the sphere (dimension along the X-axis). - \param subdivisions The number of recursive subdivisions from an initial icosahedron. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg points3d = CImg::sphere3d(faces3d,100,4); - CImg().display_object3d("Sphere3d",points3d,faces3d); - \endcode - \image html ref_sphere3d.jpg - **/ - template - static CImg sphere3d(CImgList& primitives, - const float radius=50, const unsigned int subdivisions=3) { - - // Create initial icosahedron - primitives.assign(); - const double tmp = (1+std::sqrt(5.0f))/2, a = 1.0/std::sqrt(1+tmp*tmp), b = tmp*a; - CImgList vertices(12,1,3,1,1, b,a,0.0, -b,a,0.0, -b,-a,0.0, b,-a,0.0, a,0.0,b, a,0.0,-b, - -a,0.0,-b, -a,0.0,b, 0.0,b,a, 0.0,-b,a, 0.0,-b,-a, 0.0,b,-a); - primitives.assign(20,1,3,1,1, 4,8,7, 4,7,9, 5,6,11, 5,10,6, 0,4,3, 0,3,5, 2,7,1, 2,1,6, - 8,0,11, 8,11,1, 9,10,3, 9,2,10, 8,4,0, 11,0,5, 4,9,3, - 5,3,10, 7,8,1, 6,1,11, 7,2,9, 6,10,2); - // edge - length/2 - float he = (float)a; - - // Recurse subdivisions - for (unsigned int i = 0; i::vector(nx0,ny0,nz0).move_to(vertices); i0 = vertices._width - 1; } - if (i1<0) { CImg::vector(nx1,ny1,nz1).move_to(vertices); i1 = vertices._width - 1; } - if (i2<0) { CImg::vector(nx2,ny2,nz2).move_to(vertices); i2 = vertices._width - 1; } - primitives.remove(0); - CImg::vector(p0,i0,i1).move_to(primitives); - CImg::vector((tf)i0,(tf)p1,(tf)i2).move_to(primitives); - CImg::vector((tf)i1,(tf)i2,(tf)p2).move_to(primitives); - CImg::vector((tf)i1,(tf)i0,(tf)i2).move_to(primitives); - } - } - return (vertices>'x')*=radius; - } - - //! Generate a 3d ellipsoid. - /** - \param[out] primitives The returned list of the 3d object primitives - (template type \e tf should be at least \e unsigned \e int). - \param tensor The tensor which gives the shape and size of the ellipsoid. - \param subdivisions The number of recursive subdivisions from an initial stretched icosahedron. - \return The N vertices (xi,yi,zi) of the 3d object as a Nx3 CImg image (0<=i<=N-1). - \par Example - \code - CImgList faces3d; - const CImg tensor = CImg::diagonal(10,7,3), - points3d = CImg::ellipsoid3d(faces3d,tensor,4); - CImg().display_object3d("Ellipsoid3d",points3d,faces3d); - \endcode - \image html ref_ellipsoid3d.jpg - **/ - template - static CImg ellipsoid3d(CImgList& primitives, - const CImg& tensor, const unsigned int subdivisions=3) { - primitives.assign(); - if (!subdivisions) return CImg(); - CImg S, V; - tensor.symmetric_eigen(S,V); - const float orient = - (V(0,1)*V(1,2) - V(0,2)*V(1,1))*V(2,0) + - (V(0,2)*V(1,0) - V(0,0)*V(1,2))*V(2,1) + - (V(0,0)*V(1,1) - V(0,1)*V(1,0))*V(2,2); - if (orient<0) { V(2,0) = -V(2,0); V(2,1) = -V(2,1); V(2,2) = -V(2,2); } - const float l0 = S[0], l1 = S[1], l2 = S[2]; - CImg vertices = sphere3d(primitives,1.0,subdivisions); - vertices.get_shared_row(0)*=l0; - vertices.get_shared_row(1)*=l1; - vertices.get_shared_row(2)*=l2; - return V*vertices; - } - - //! Convert 3d object into a CImg3d representation. - /** - \param primitives Primitives data of the 3d object. - \param colors Colors data of the 3d object. - \param opacities Opacities data of the 3d object. - \param full_check Tells if full checking of the 3d object must be performed. - **/ - template - CImg& object3dtoCImg3d(const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const bool full_check=true) { - return get_object3dtoCImg3d(primitives,colors,opacities,full_check).move_to(*this); - } - - //! Convert 3d object into a CImg3d representation \overloading. - template - CImg& object3dtoCImg3d(const CImgList& primitives, - const CImgList& colors, - const bool full_check=true) { - return get_object3dtoCImg3d(primitives,colors,full_check).move_to(*this); - } - - //! Convert 3d object into a CImg3d representation \overloading. - template - CImg& object3dtoCImg3d(const CImgList& primitives, - const bool full_check=true) { - return get_object3dtoCImg3d(primitives,full_check).move_to(*this); - } - - //! Convert 3d object into a CImg3d representation \overloading. - CImg& object3dtoCImg3d(const bool full_check=true) { - return get_object3dtoCImg3d(full_check).move_to(*this); - } - - //! Convert 3d object into a CImg3d representation \newinstance. - template - CImg get_object3dtoCImg3d(const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const bool full_check=true) const { - char error_message[1024] = { 0 }; - if (!is_object3d(primitives,colors,opacities,full_check,error_message)) - throw CImgInstanceException(_cimg_instance - "object3dtoCImg3d(): Invalid specified 3d object (%u,%u) (%s).", - cimg_instance,_width,primitives._width,error_message); - CImg res(1,_size_object3dtoCImg3d(primitives,colors,opacities)); - float *ptrd = res._data; - - // Put magick number. - *(ptrd++) = 'C' + 0.5f; *(ptrd++) = 'I' + 0.5f; *(ptrd++) = 'm' + 0.5f; - *(ptrd++) = 'g' + 0.5f; *(ptrd++) = '3' + 0.5f; *(ptrd++) = 'd' + 0.5f; - - // Put number of vertices and primitives. - *(ptrd++) = cimg::uint2float(_width); - *(ptrd++) = cimg::uint2float(primitives._width); - - // Put vertex data. - if (is_empty() || !primitives) return res; - const T *ptrx = data(0,0), *ptry = data(0,1), *ptrz = data(0,2); - cimg_forX(*this,p) { - *(ptrd++) = (float)*(ptrx++); - *(ptrd++) = (float)*(ptry++); - *(ptrd++) = (float)*(ptrz++); - } - - // Put primitive data. - cimglist_for(primitives,p) { - *(ptrd++) = (float)primitives[p].size(); - const tp *ptrp = primitives[p]._data; - cimg_foroff(primitives[p],i) *(ptrd++) = cimg::uint2float((unsigned int)*(ptrp++)); - } - - // Put color/texture data. - const unsigned int csiz = cimg::min(colors._width,primitives._width); - for (int c = 0; c<(int)csiz; ++c) { - const CImg& color = colors[c]; - const tc *ptrc = color._data; - if (color.size()==3) { *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*ptrc; } - else { - *(ptrd++) = -128.0f; - int shared_ind = -1; - if (color.is_shared()) for (int i = 0; i - float* _object3dtoCImg3d(const CImgList& opacities, float *ptrd) const { - cimglist_for(opacities,o) { - const CImg& opacity = opacities[o]; - const to *ptro = opacity._data; - if (opacity.size()==1) *(ptrd++) = (float)*ptro; - else { - *(ptrd++) = -128.0f; - int shared_ind = -1; - if (opacity.is_shared()) for (int i = 0; i - float* _object3dtoCImg3d(const CImg& opacities, float *ptrd) const { - const to *ptro = opacities._data; - cimg_foroff(opacities,o) *(ptrd++) = (float)*(ptro++); - return ptrd; - } - - template - unsigned int _size_object3dtoCImg3d(const CImgList& primitives, - const CImgList& colors, - const CImgList& opacities) const { - unsigned int siz = 8 + 3*width(); - cimglist_for(primitives,p) siz+=primitives[p].size() + 1; - for (int c = cimg::min(primitives._width,colors._width)-1; c>=0; --c) { - if (colors[c].is_shared()) siz+=4; - else { const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4+csiz:3; } - } - if (colors._width - unsigned int _size_object3dtoCImg3d(const CImgList& primitives, - const CImgList& colors, - const CImg& opacities) const { - unsigned int siz = 8 + 3*width(); - cimglist_for(primitives,p) siz+=primitives[p].size() + 1; - for (int c = cimg::min(primitives._width,colors._width)-1; c>=0; --c) { - const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4+csiz:3; - } - if (colors._width - CImg get_object3dtoCImg3d(const CImgList& primitives, - const CImgList& colors, - const bool full_check=true) const { - CImgList opacities; - return get_object3dtoCImg3d(primitives,colors,opacities,full_check); - } - - //! Convert 3d object into a CImg3d representation \overloading. - template - CImg get_object3dtoCImg3d(const CImgList& primitives, - const bool full_check=true) const { - CImgList colors, opacities; - return get_object3dtoCImg3d(primitives,colors,opacities,full_check); - } - - //! Convert 3d object into a CImg3d representation \overloading. - CImg get_object3dtoCImg3d(const bool full_check=true) const { - CImgList opacities, colors; - CImgList primitives(width(),1,1,1,1); - cimglist_for(primitives,p) primitives(p,0) = p; - return get_object3dtoCImg3d(primitives,colors,opacities,full_check); - } - - //! Convert CImg3d representation into a 3d object. - /** - \param[out] primitives Primitives data of the 3d object. - \param[out] colors Colors data of the 3d object. - \param[out] opacities Opacities data of the 3d object. - \param full_check Tells if full checking of the 3d object must be performed. - **/ - template - CImg& CImg3dtoobject3d(CImgList& primitives, - CImgList& colors, - CImgList& opacities, - const bool full_check=true) { - return get_CImg3dtoobject3d(primitives,colors,opacities,full_check).move_to(*this); - } - - //! Convert CImg3d representation into a 3d object \newinstance. - template - CImg get_CImg3dtoobject3d(CImgList& primitives, - CImgList& colors, - CImgList& opacities, - const bool full_check=true) const { - char error_message[1024] = { 0 }; - if (!is_CImg3d(full_check,error_message)) - throw CImgInstanceException(_cimg_instance - "CImg3dtoobject3d(): image instance is not a CImg3d (%s).", - cimg_instance,error_message); - const T *ptrs = _data + 6; - const unsigned int - nb_points = cimg::float2uint((float)*(ptrs++)), - nb_primitives = cimg::float2uint((float)*(ptrs++)); - const CImg points = CImg(ptrs,3,nb_points,1,1,true).get_transpose(); - ptrs+=3*nb_points; - primitives.assign(nb_primitives); - cimglist_for(primitives,p) { - const unsigned int nb_inds = (unsigned int)*(ptrs++); - primitives[p].assign(1,nb_inds); - tp *ptrp = primitives[p]._data; - for (unsigned int i = 0; i - CImg& _draw_scanline(const int x0, const int x1, const int y, - const tc *const color, const float opacity, - const float brightness, - const float nopacity, const float copacity, const unsigned long whd) { - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const int nx0 = x0>0?x0:0, nx1 = x1=0) { - const tc *col = color; - const unsigned long off = whd - dx - 1; - T *ptrd = data(nx0,y); - if (opacity>=1) { // ** Opaque drawing ** - if (brightness==1) { // Brightness==1 - if (sizeof(T)!=1) cimg_forC(*this,c) { - const T val = (T)*(col++); - for (int x = dx; x>=0; --x) *(ptrd++) = val; - ptrd+=off; - } else cimg_forC(*this,c) { - const T val = (T)*(col++); - std::memset(ptrd,(int)val,dx+1); - ptrd+=whd; - } - } else if (brightness<1) { // Brightness<1 - if (sizeof(T)!=1) cimg_forC(*this,c) { - const T val = (T)(*(col++)*brightness); - for (int x = dx; x>=0; --x) *(ptrd++) = val; - ptrd+=off; - } else cimg_forC(*this,c) { - const T val = (T)(*(col++)*brightness); - std::memset(ptrd,(int)val,dx+1); - ptrd+=whd; - } - } else { // Brightness>1 - if (sizeof(T)!=1) cimg_forC(*this,c) { - const T val = (T)((2-brightness)**(col++) + (brightness-1)*maxval); - for (int x = dx; x>=0; --x) *(ptrd++) = val; - ptrd+=off; - } else cimg_forC(*this,c) { - const T val = (T)((2-brightness)**(col++) + (brightness-1)*maxval); - std::memset(ptrd,(int)val,dx+1); - ptrd+=whd; - } - } - } else { // ** Transparent drawing ** - if (brightness==1) { // Brightness==1 - cimg_forC(*this,c) { - const T val = (T)*(col++); - for (int x = dx; x>=0; --x) { *ptrd = (T)(val*nopacity + *ptrd*copacity); ++ptrd; } - ptrd+=off; - } - } else if (brightness<=1) { // Brightness<1 - cimg_forC(*this,c) { - const T val = (T)(*(col++)*brightness); - for (int x = dx; x>=0; --x) { *ptrd = (T)(val*nopacity + *ptrd*copacity); ++ptrd; } - ptrd+=off; - } - } else { // Brightness>1 - cimg_forC(*this,c) { - const T val = (T)((2-brightness)**(col++) + (brightness-1)*maxval); - for (int x = dx; x>=0; --x) { *ptrd = (T)(val*nopacity + *ptrd*copacity); ++ptrd; } - ptrd+=off; - } - } - } - } - return *this; - } - - //! Draw a 3d point. - /** - \param x0 X-coordinate of the point. - \param y0 Y-coordinate of the point. - \param z0 Z-coordinate of the point. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \note - - To set pixel values without clipping needs, you should use the faster CImg::operator()() function. - \par Example: - \code - CImg img(100,100,1,3,0); - const unsigned char color[] = { 255,128,64 }; - img.draw_point(50,50,color); - \endcode - **/ - template - CImg& draw_point(const int x0, const int y0, const int z0, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_point(): Specified color is (null).", - cimg_instance); - if (x0>=0 && y0>=0 && z0>=0 && x0=1) cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } - else cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; } - } - return *this; - } - - //! Draw a 2d point \simplification. - template - CImg& draw_point(const int x0, const int y0, - const tc *const color, const float opacity=1) { - return draw_point(x0,y0,0,color,opacity); - } - - // Draw a points cloud. - /** - \param points Image of vertices coordinates. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - **/ - template - CImg& draw_point(const CImg& points, - const tc *const color, const float opacity=1) { - if (is_empty() || !points) return *this; - switch (points._height) { - case 0 : case 1 : - throw CImgArgumentException(_cimg_instance - "draw_point(): Invalid specified point set (%u,%u,%u,%u,%p).", - cimg_instance, - points._width,points._height,points._depth,points._spectrum,points._data); - case 2 : { - cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),color,opacity); - } break; - default : { - cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),(int)points(i,2),color,opacity); - } - } - return *this; - } - - //! Draw a 2d line. - /** - \param x0 X-coordinate of the starting line point. - \param y0 Y-coordinate of the starting line point. - \param x1 X-coordinate of the ending line point. - \param y1 Y-coordinate of the ending line point. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch Tells if a reinitialization of the hash state must be done. - \note - - Line routine uses Bresenham's algorithm. - - Set \p init_hatch = false to draw consecutive hatched segments without breaking the line pattern. - \par Example: - \code - CImg img(100,100,1,3,0); - const unsigned char color[] = { 255,128,64 }; - img.draw_line(40,40,80,70,color); - \endcode - **/ - template - CImg& draw_line(const int x0, const int y0, - const int x1, const int y1, - const tc *const color, const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_line(): Specified color is (null).", - cimg_instance); - static unsigned int hatch = ~0U - (~0U>>1); - if (init_hatch) hatch = ~0U - (~0U>>1); - const bool xdir = x0=width()) return *this; - if (xleft<0) { yleft-=(int)((float)xleft*((float)yright - yleft)/((float)xright - xleft)); xleft = 0; } - if (xright>=width()) { - yright-=(int)(((float)xright - width())*((float)yright - yleft)/((float)xright - xleft)); - xright = width() - 1; - } - if (ydown<0 || yup>=height()) return *this; - if (yup<0) { xup-=(int)((float)yup*((float)xdown - xup)/((float)ydown - yup)); yup = 0; } - if (ydown>=height()) { - xdown-=(int)(((float)ydown - height())*((float)xdown - xup)/((float)ydown - yup)); - ydown = height() - 1; - } - T *ptrd0 = data(nx0,ny0); - int dx = xright - xleft, dy = ydown - yup; - const bool steep = dy>dx; - if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); - const long - offx = (nx0=1) { - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - T *ptrd = ptrd0; const tc* col = color; - cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - T *ptrd = ptrd0; const tc* col = color; cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } - } else { - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - T *ptrd = ptrd0; const tc* col = color; - cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - T *ptrd = ptrd0; const tc* col = color; - cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } - } - return *this; - } - - //! Draw a 2d line, with z-buffering. - /** - \param zbuffer Zbuffer image. - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param z0 Z-coordinate of the starting point - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param z1 Z-coordinate of the ending point. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch Tells if a reinitialization of the hash state must be done. - **/ - template - CImg& draw_line(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const tc *const color, const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_line(): Specified color is (null).", - cimg_instance); - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - static unsigned int hatch = ~0U - (~0U>>1); - if (init_hatch) hatch = ~0U - (~0U>>1); - const bool xdir = x0=width()) return *this; - if (xleft<0) { - const float D = (float)xright - xleft; - yleft-=(int)((float)xleft*((float)yright - yleft)/D); - zleft-=(tzfloat)xleft*(zright - zleft)/D; - xleft = 0; - } - if (xright>=width()) { - const float d = (float)xright - width(), D = (float)xright - xleft; - yright-=(int)(d*((float)yright - yleft)/D); - zright-=(tzfloat)d*(zright - zleft)/D; - xright = width() - 1; - } - if (ydown<0 || yup>=height()) return *this; - if (yup<0) { - const float D = (float)ydown - yup; - xup-=(int)((float)yup*((float)xdown - xup)/D); - zup-=(tzfloat)yup*(zdown - zup)/D; - yup = 0; - } - if (ydown>=height()) { - const float d = (float)ydown - height(), D = (float)ydown - yup; - xdown-=(int)(d*((float)xdown - xup)/D); - zdown-=(tzfloat)d*(zdown - zup)/D; - ydown = height() - 1; - } - T *ptrd0 = data(nx0,ny0); - tz *ptrz = zbuffer.data(nx0,ny0); - int dx = xright - xleft, dy = ydown - yup; - const bool steep = dy>dx; - if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); - const long - offx = (nx00?dx:1; - if (opacity>=1) { - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz && pattern&hatch) { - *ptrz = (tz)z; - T *ptrd = ptrd0; const tc *col = color; - cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz) { - *ptrz = (tz)z; - T *ptrd = ptrd0; const tc *col = color; - cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=wh; } - } - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } - } else { - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz && pattern&hatch) { - *ptrz = (tz)z; - T *ptrd = ptrd0; const tc *col = color; - cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz) { - *ptrz = (tz)z; - T *ptrd = ptrd0; const tc *col = color; - cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=wh; } - } - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } - } - return *this; - } - - //! Draw a 3d line. - /** - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param z0 Z-coordinate of the starting point - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param z1 Z-coordinate of the ending point. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch Tells if a reinitialization of the hash state must be done. - **/ - template - CImg& draw_line(const int x0, const int y0, const int z0, - const int x1, const int y1, const int z1, - const tc *const color, const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_line(): Specified color is (null).", - cimg_instance); - static unsigned int hatch = ~0U - (~0U>>1); - if (init_hatch) hatch = ~0U - (~0U>>1); - int nx0 = x0, ny0 = y0, nz0 = z0, nx1 = x1, ny1 = y1, nz1 = z1; - if (nx0>nx1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); - if (nx1<0 || nx0>=width()) return *this; - if (nx0<0) { - const float D = 1.0f + nx1 - nx0; - ny0-=(int)((float)nx0*(1.0f + ny1 - ny0)/D); - nz0-=(int)((float)nx0*(1.0f + nz1 - nz0)/D); - nx0 = 0; - } - if (nx1>=width()) { - const float d = (float)nx1 - width(), D = 1.0f + nx1 - nx0; - ny1+=(int)(d*(1.0f + ny0 - ny1)/D); - nz1+=(int)(d*(1.0f + nz0 - nz1)/D); - nx1 = width() - 1; - } - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); - if (ny1<0 || ny0>=height()) return *this; - if (ny0<0) { - const float D = 1.0f + ny1 - ny0; - nx0-=(int)((float)ny0*(1.0f + nx1 - nx0)/D); - nz0-=(int)((float)ny0*(1.0f + nz1 - nz0)/D); - ny0 = 0; - } - if (ny1>=height()) { - const float d = (float)ny1 - height(), D = 1.0f + ny1 - ny0; - nx1+=(int)(d*(1.0f + nx0 - nx1)/D); - nz1+=(int)(d*(1.0f + nz0 - nz1)/D); - ny1 = height() - 1; - } - if (nz0>nz1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); - if (nz1<0 || nz0>=depth()) return *this; - if (nz0<0) { - const float D = 1.0f + nz1 - nz0; - nx0-=(int)((float)nz0*(1.0f + nx1 - nx0)/D); - ny0-=(int)((float)nz0*(1.0f + ny1 - ny0)/D); - nz0 = 0; - } - if (nz1>=depth()) { - const float d = (float)nz1 - depth(), D = 1.0f + nz1 - nz0; - nx1+=(int)(d*(1.0f + nx0 - nx1)/D); - ny1+=(int)(d*(1.0f + ny0 - ny1)/D); - nz1 = depth() - 1; - } - const unsigned int dmax = cimg::max(cimg::abs(nx1 - nx0),cimg::abs(ny1 - ny0),nz1 - nz0); - const unsigned long whd = (unsigned long)_width*_height*_depth; - const float px = (nx1 - nx0)/(float)dmax, py = (ny1 - ny0)/(float)dmax, pz = (nz1 - nz0)/(float)dmax; - float x = (float)nx0, y = (float)ny0, z = (float)nz0; - if (opacity>=1) for (unsigned int t = 0; t<=dmax; ++t) { - if (!(~pattern) || (~pattern && pattern&hatch)) { - T* ptrd = data((unsigned int)x,(unsigned int)y,(unsigned int)z); - const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } - } - x+=px; y+=py; z+=pz; if (pattern) { hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); } - } else { - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - for (unsigned int t = 0; t<=dmax; ++t) { - if (!(~pattern) || (~pattern && pattern&hatch)) { - T* ptrd = data((unsigned int)x,(unsigned int)y,(unsigned int)z); - const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; } - } - x+=px; y+=py; z+=pz; if (pattern) { hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); } - } - } - return *this; - } - - //! Draw a textured 2d line. - /** - \param x0 X-coordinate of the starting line point. - \param y0 Y-coordinate of the starting line point. - \param x1 X-coordinate of the ending line point. - \param y1 Y-coordinate of the ending line point. - \param texture Texture image defining the pixel colors. - \param tx0 X-coordinate of the starting texture point. - \param ty0 Y-coordinate of the starting texture point. - \param tx1 X-coordinate of the ending texture point. - \param ty1 Y-coordinate of the ending texture point. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch Tells if the hash variable must be reinitialized. - \note - - Line routine uses the well known Bresenham's algorithm. - \par Example: - \code - CImg img(100,100,1,3,0), texture("texture256x256.ppm"); - const unsigned char color[] = { 255,128,64 }; - img.draw_line(40,40,80,70,texture,0,0,255,255); - \endcode - **/ - template - CImg& draw_line(const int x0, const int y0, - const int x1, const int y1, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty()) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) return draw_line(x0,y0,x1,y1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch); - static unsigned int hatch = ~0U - (~0U>>1); - if (init_hatch) hatch = ~0U - (~0U>>1); - const bool xdir = x0=width()) return *this; - if (xleft<0) { - const float D = (float)xright - xleft; - yleft-=(int)((float)xleft*((float)yright - yleft)/D); - txleft-=(int)((float)xleft*((float)txright - txleft)/D); - tyleft-=(int)((float)xleft*((float)tyright - tyleft)/D); - xleft = 0; - } - if (xright>=width()) { - const float d = (float)xright - width(), D = (float)xright - xleft; - yright-=(int)(d*((float)yright - yleft)/D); - txright-=(int)(d*((float)txright - txleft)/D); - tyright-=(int)(d*((float)tyright - tyleft)/D); - xright = width() - 1; - } - if (ydown<0 || yup>=height()) return *this; - if (yup<0) { - const float D = (float)ydown - yup; - xup-=(int)((float)yup*((float)xdown - xup)/D); - txup-=(int)((float)yup*((float)txdown - txup)/D); - tyup-=(int)((float)yup*((float)tydown - tyup)/D); - yup = 0; - } - if (ydown>=height()) { - const float d = (float)ydown - height(), D = (float)ydown - yup; - xdown-=(int)(d*((float)xdown - xup)/D); - txdown-=(int)(d*((float)txdown - txup)/D); - tydown-=(int)(d*((float)tydown - tyup)/D); - ydown = height() - 1; - } - T *ptrd0 = data(nx0,ny0); - int dx = xright - xleft, dy = ydown - yup; - const bool steep = dy>dx; - if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); - const long - offx = (nx00?dx:1; - const unsigned long wh = (unsigned long)_width*_height; - - if (opacity>=1) { - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - T *ptrd = ptrd0; - const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; - cimg_forC(*this,c) { *ptrd = (T)texture(tx,ty,0,c); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - T *ptrd = ptrd0; - const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; - cimg_forC(*this,c) { *ptrd = (T)texture(tx,ty,0,c); ptrd+=wh; } - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } - } else { - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - T *ptrd = ptrd0; - if (pattern&hatch) { - const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; - cimg_forC(*this,c) { *ptrd = (T)(nopacity*texture(tx,ty,0,c) + *ptrd*copacity); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - T *ptrd = ptrd0; - const int tx = tx0 + x*dtx/ndx, ty = ty0 + x*dty/ndx; - cimg_forC(*this,c) { *ptrd = (T)(nopacity*texture(tx,ty,0,c) + *ptrd*copacity); ptrd+=wh; } - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } - } - return *this; - } - - //! Draw a textured 2d line, with perspective correction. - /** - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param z0 Z-coordinate of the starting point - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param z1 Z-coordinate of the ending point. - \param texture Texture image defining the pixel colors. - \param tx0 X-coordinate of the starting texture point. - \param ty0 Y-coordinate of the starting texture point. - \param tx1 X-coordinate of the ending texture point. - \param ty1 Y-coordinate of the ending texture point. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch Tells if the hash variable must be reinitialized. - **/ - template - CImg& draw_line(const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty() && z0<=0 && z1<=0) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_line(x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch); - static unsigned int hatch = ~0U - (~0U>>1); - if (init_hatch) hatch = ~0U - (~0U>>1); - const bool xdir = x0=width()) return *this; - if (xleft<0) { - const float D = (float)xright - xleft; - yleft-=(int)((float)xleft*((float)yright - yleft)/D); - zleft-=(float)xleft*(zright - zleft)/D; - txleft-=(float)xleft*(txright - txleft)/D; - tyleft-=(float)xleft*(tyright - tyleft)/D; - xleft = 0; - } - if (xright>=width()) { - const float d = (float)xright - width(), D = (float)xright - xleft; - yright-=(int)(d*((float)yright - yleft)/D); - zright-=d*(zright - zleft)/D; - txright-=d*(txright - txleft)/D; - tyright-=d*(tyright - tyleft)/D; - xright = width() - 1; - } - if (ydown<0 || yup>=height()) return *this; - if (yup<0) { - const float D = (float)ydown - yup; - xup-=(int)((float)yup*((float)xdown - xup)/D); - zup-=(float)yup*(zdown - zup)/D; - txup-=(float)yup*(txdown - txup)/D; - tyup-=(float)yup*(tydown - tyup)/D; - yup = 0; - } - if (ydown>=height()) { - const float d = (float)ydown - height(), D = (float)ydown - yup; - xdown-=(int)(d*((float)xdown - xup)/D); - zdown-=d*(zdown - zup)/D; - txdown-=d*(txdown - txup)/D; - tydown-=d*(tydown - tyup)/D; - ydown = height() - 1; - } - T *ptrd0 = data(nx0,ny0); - int dx = xright - xleft, dy = ydown - yup; - const bool steep = dy>dx; - if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); - const long - offx = (nx00?dx:1; - const unsigned long wh = (unsigned long)_width*_height; - - if (opacity>=1) { - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } - } else { - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; - cimg_forC(*this,c) { - *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; - } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - const float z = Z0 + x*dz/ndx, tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; - cimg_forC(*this,c) { *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; } - ptrd0+=offx; - if ((error-=dy)<0) { ptrd0+=offy; error+=dx; } - } - } - return *this; - } - - //! Draw a textured 2d line, with perspective correction and z-buffering. - /** - \param zbuffer Z-buffer image. - \param x0 X-coordinate of the starting point. - \param y0 Y-coordinate of the starting point. - \param z0 Z-coordinate of the starting point - \param x1 X-coordinate of the ending point. - \param y1 Y-coordinate of the ending point. - \param z1 Z-coordinate of the ending point. - \param texture Texture image defining the pixel colors. - \param tx0 X-coordinate of the starting texture point. - \param ty0 Y-coordinate of the starting texture point. - \param tx1 X-coordinate of the ending texture point. - \param ty1 Y-coordinate of the ending texture point. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch Tells if the hash variable must be reinitialized. - **/ - template - CImg& draw_line(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0) return *this; - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_line(zbuffer,x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch); - static unsigned int hatch = ~0U - (~0U>>1); - if (init_hatch) hatch = ~0U - (~0U>>1); - const bool xdir = x0=width()) return *this; - if (xleft<0) { - const float D = (float)xright - xleft; - yleft-=(int)((float)xleft*((float)yright - yleft)/D); - zleft-=(float)xleft*(zright - zleft)/D; - txleft-=(float)xleft*(txright - txleft)/D; - tyleft-=(float)xleft*(tyright - tyleft)/D; - xleft = 0; - } - if (xright>=width()) { - const float d = (float)xright - width(), D = (float)xright - xleft; - yright-=(int)(d*((float)yright - yleft)/D); - zright-=d*(zright - zleft)/D; - txright-=d*(txright - txleft)/D; - tyright-=d*(tyright - tyleft)/D; - xright = width()-1; - } - if (ydown<0 || yup>=height()) return *this; - if (yup<0) { - const float D = (float)ydown - yup; - xup-=(int)((float)yup*((float)xdown - xup)/D); - zup-=yup*(zdown - zup)/D; - txup-=yup*(txdown - txup)/D; - tyup-=yup*(tydown - tyup)/D; - yup = 0; - } - if (ydown>=height()) { - const float d = (float)ydown - height(), D = (float)ydown - yup; - xdown-=(int)(d*((float)xdown - xup)/D); - zdown-=d*(zdown - zup)/D; - txdown-=d*(txdown - txup)/D; - tydown-=d*(tydown - tyup)/D; - ydown = height()-1; - } - T *ptrd0 = data(nx0,ny0); - tz *ptrz = zbuffer.data(nx0,ny0); - int dx = xright - xleft, dy = ydown - yup; - const bool steep = dy>dx; - if (steep) cimg::swap(nx0,ny0,nx1,ny1,dx,dy); - const long - offx = (nx00?dx:1; - const unsigned long wh = (unsigned long)_width*_height; - - if (opacity>=1) { - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz) { - *ptrz = (tz)z; - const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } - } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz) { - *ptrz = (tz)z; - const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; cimg_forC(*this,c) { *ptrd = (T)texture((int)(tx/z),(int)(ty/z),0,c); ptrd+=wh; } - } - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } - } else { - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (~pattern) for (int error = dx>>1, x = 0; x<=dx; ++x) { - if (pattern&hatch) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz) { - *ptrz = (tz)z; - const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; - cimg_forC(*this,c) { - *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; - } - } - } - hatch>>=1; if (!hatch) hatch = ~0U - (~0U>>1); - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } else for (int error = dx>>1, x = 0; x<=dx; ++x) { - const tzfloat z = Z0 + x*dz/ndx; - if (z>=(tzfloat)*ptrz) { - *ptrz = (tz)z; - const float tx = Tx0 + x*dtx/ndx, ty = Ty0 + x*dty/ndx; - T *ptrd = ptrd0; - cimg_forC(*this,c) { - *ptrd = (T)(nopacity*texture((int)(tx/z),(int)(ty/z),0,c) + *ptrd*copacity); ptrd+=wh; - } - } - ptrd0+=offx; ptrz+=offx; - if ((error-=dy)<0) { ptrd0+=offy; ptrz+=offy; error+=dx; } - } - } - return *this; - } - - //! Draw a set of consecutive lines. - /** - \param points Coordinates of vertices, stored as a list of vectors. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch If set to true, init hatch motif. - \note - - This function uses several call to the single CImg::draw_line() procedure, - depending on the vectors size in \p points. - **/ - template - CImg& draw_line(const CImg& points, - const tc *const color, const float opacity=1, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty() || !points || points._width<2) return *this; - bool ninit_hatch = init_hatch; - switch (points._height) { - case 0 : case 1 : - throw CImgArgumentException(_cimg_instance - "draw_line(): Invalid specified point set (%u,%u,%u,%u,%p).", - cimg_instance, - points._width,points._height,points._depth,points._spectrum,points._data); - - case 2 : { - const int x0 = (int)points(0,0), y0 = (int)points(0,1); - int ox = x0, oy = y0; - for (unsigned int i = 1; i - CImg& draw_arrow(const int x0, const int y0, - const int x1, const int y1, - const tc *const color, const float opacity=1, - const float angle=30, const float length=-10, - const unsigned int pattern=~0U) { - if (is_empty()) return *this; - const float u = (float)(x0 - x1), v = (float)(y0 - y1), sq = u*u + v*v, - deg = (float)(angle*cimg::PI/180), ang = (sq>0)?(float)std::atan2(v,u):0.0f, - l = (length>=0)?length:-length*(float)std::sqrt(sq)/100; - if (sq>0) { - const float - cl = (float)std::cos(ang - deg), sl = (float)std::sin(ang - deg), - cr = (float)std::cos(ang + deg), sr = (float)std::sin(ang + deg); - const int - xl = x1 + (int)(l*cl), yl = y1 + (int)(l*sl), - xr = x1 + (int)(l*cr), yr = y1 + (int)(l*sr), - xc = x1 + (int)((l+1)*(cl+cr))/2, yc = y1 + (int)((l+1)*(sl+sr))/2; - draw_line(x0,y0,xc,yc,color,opacity,pattern).draw_triangle(x1,y1,xl,yl,xr,yr,color,opacity); - } else draw_point(x0,y0,color,opacity); - return *this; - } - - //! Draw a 2d spline. - /** - \param x0 X-coordinate of the starting curve point - \param y0 Y-coordinate of the starting curve point - \param u0 X-coordinate of the starting velocity - \param v0 Y-coordinate of the starting velocity - \param x1 X-coordinate of the ending curve point - \param y1 Y-coordinate of the ending curve point - \param u1 X-coordinate of the ending velocity - \param v1 Y-coordinate of the ending velocity - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param precision Curve drawing precision. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch If \c true, init hatch motif. - \note - - The curve is a 2d cubic Bezier spline, from the set of specified starting/ending points - and corresponding velocity vectors. - - The spline is drawn as a serie of connected segments. The \p precision parameter sets the - average number of pixels in each drawn segment. - - A cubic Bezier curve is sometimes defined by a set of 4 points { (\p x0,\p y0), (\p xa,\p ya), - (\p xb,\p yb), (\p x1,\p y1) } where (\p x0,\p y0) is the starting point, (\p x1,\p y1) is the ending point - and (\p xa,\p ya), (\p xb,\p yb) are two - \e control points. - The starting and ending velocities (\p u0,\p v0) and (\p u1,\p v1) can be deduced easily from - the control points as - \p u0 = (\p xa - \p x0), \p v0 = (\p ya - \p y0), \p u1 = (\p x1 - \p xb) and \p v1 = (\p y1 - \p yb). - \par Example: - \code - CImg img(100,100,1,3,0); - const unsigned char color[] = { 255,255,255 }; - img.draw_spline(30,30,0,100,90,40,0,-100,color); - \endcode - **/ - template - CImg& draw_spline(const int x0, const int y0, const float u0, const float v0, - const int x1, const int y1, const float u1, const float v1, - const tc *const color, const float opacity=1, - const float precision=0.25, const unsigned int pattern=~0U, - const bool init_hatch=true) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_spline(): Specified color is (null).", - cimg_instance); - if (x0==x1 && y0==y1) return draw_point(x0,y0,color,opacity); - bool ninit_hatch = init_hatch; - const float - ax = u0 + u1 + 2*(x0 - x1), - bx = 3*(x1 - x0) - 2*u0 - u1, - ay = v0 + v1 + 2*(y0 - y1), - by = 3*(y1 - y0) - 2*v0 - v1, - _precision = 1/(std::sqrt(cimg::sqr((float)x0-x1)+cimg::sqr((float)y0-y1))*(precision>0?precision:1)); - int ox = x0, oy = y0; - for (float t = 0; t<1; t+=_precision) { - const float t2 = t*t, t3 = t2*t; - const int - nx = (int)(ax*t3 + bx*t2 + u0*t + x0), - ny = (int)(ay*t3 + by*t2 + v0*t + y0); - draw_line(ox,oy,nx,ny,color,opacity,pattern,ninit_hatch); - ninit_hatch = false; - ox = nx; oy = ny; - } - return draw_line(ox,oy,x1,y1,color,opacity,pattern,false); - } - - //! Draw a 3d spline \overloading. - /** - \note - - Similar to CImg::draw_spline() for a 3d spline in a volumetric image. - **/ - template - CImg& draw_spline(const int x0, const int y0, const int z0, const float u0, const float v0, const float w0, - const int x1, const int y1, const int z1, const float u1, const float v1, const float w1, - const tc *const color, const float opacity=1, - const float precision=4, const unsigned int pattern=~0U, - const bool init_hatch=true) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_spline(): Specified color is (null).", - cimg_instance); - if (x0==x1 && y0==y1 && z0==z1) return draw_point(x0,y0,z0,color,opacity); - bool ninit_hatch = init_hatch; - const float - ax = u0 + u1 + 2*(x0 - x1), - bx = 3*(x1 - x0) - 2*u0 - u1, - ay = v0 + v1 + 2*(y0 - y1), - by = 3*(y1 - y0) - 2*v0 - v1, - az = w0 + w1 + 2*(z0 - z1), - bz = 3*(z1 - z0) - 2*w0 - w1, - _precision = 1/(std::sqrt(cimg::sqr(x0-x1)+cimg::sqr(y0-y1))*(precision>0?precision:1)); - int ox = x0, oy = y0, oz = z0; - for (float t = 0; t<1; t+=_precision) { - const float t2 = t*t, t3 = t2*t; - const int - nx = (int)(ax*t3 + bx*t2 + u0*t + x0), - ny = (int)(ay*t3 + by*t2 + v0*t + y0), - nz = (int)(az*t3 + bz*t2 + w0*t + z0); - draw_line(ox,oy,oz,nx,ny,nz,color,opacity,pattern,ninit_hatch); - ninit_hatch = false; - ox = nx; oy = ny; oz = nz; - } - return draw_line(ox,oy,oz,x1,y1,z1,color,opacity,pattern,false); - } - - //! Draw a textured 2d spline. - /** - \param x0 X-coordinate of the starting curve point - \param y0 Y-coordinate of the starting curve point - \param u0 X-coordinate of the starting velocity - \param v0 Y-coordinate of the starting velocity - \param x1 X-coordinate of the ending curve point - \param y1 Y-coordinate of the ending curve point - \param u1 X-coordinate of the ending velocity - \param v1 Y-coordinate of the ending velocity - \param texture Texture image defining line pixel colors. - \param tx0 X-coordinate of the starting texture point. - \param ty0 Y-coordinate of the starting texture point. - \param tx1 X-coordinate of the ending texture point. - \param ty1 Y-coordinate of the ending texture point. - \param precision Curve drawing precision. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch if \c true, reinit hatch motif. - **/ - template - CImg& draw_spline(const int x0, const int y0, const float u0, const float v0, - const int x1, const int y1, const float u1, const float v1, - const CImg& texture, - const int tx0, const int ty0, const int tx1, const int ty1, - const float opacity=1, - const float precision=4, const unsigned int pattern=~0U, - const bool init_hatch=true) { - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_spline(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_empty()) return *this; - if (is_overlapped(texture)) - return draw_spline(x0,y0,u0,v0,x1,y1,u1,v1,+texture,tx0,ty0,tx1,ty1,precision,opacity,pattern,init_hatch); - if (x0==x1 && y0==y1) return draw_point(x0,y0,texture.get_vector_at(x0,y0),opacity); - bool ninit_hatch = init_hatch; - const float - ax = u0 + u1 + 2*(x0 - x1), - bx = 3*(x1 - x0) - 2*u0 - u1, - ay = v0 + v1 + 2*(y0 - y1), - by = 3*(y1 - y0) - 2*v0 - v1, - _precision = 1/(std::sqrt(cimg::sqr(x0-x1)+cimg::sqr(y0-y1))*(precision>0?precision:1)); - int ox = x0, oy = y0, otx = tx0, oty = ty0; - for (float t1 = 0; t1<1; t1+=_precision) { - const float t2 = t1*t1, t3 = t2*t1; - const int - nx = (int)(ax*t3 + bx*t2 + u0*t1 + x0), - ny = (int)(ay*t3 + by*t2 + v0*t1 + y0), - ntx = tx0 + (int)((tx1-tx0)*t1), - nty = ty0 + (int)((ty1-ty0)*t1); - draw_line(ox,oy,nx,ny,texture,otx,oty,ntx,nty,opacity,pattern,ninit_hatch); - ninit_hatch = false; - ox = nx; oy = ny; otx = ntx; oty = nty; - } - return draw_line(ox,oy,x1,y1,texture,otx,oty,tx1,ty1,opacity,pattern,false); - } - - //! Draw a set of consecutive splines. - /** - \param points Vertices data. - \param tangents Tangents data. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param is_closed_set Tells if the drawn spline set is closed. - \param precision Precision of the drawing. - \param pattern An integer whose bits describe the line pattern. - \param init_hatch If \c true, init hatch motif. - **/ - template - CImg& draw_spline(const CImg& points, const CImg& tangents, - const tc *const color, const float opacity=1, - const bool is_closed_set=false, const float precision=4, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty() || !points || !tangents || points._width<2 || tangents._width<2) return *this; - bool ninit_hatch = init_hatch; - switch (points._height) { - case 0 : case 1 : - throw CImgArgumentException(_cimg_instance - "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).", - cimg_instance, - points._width,points._height,points._depth,points._spectrum,points._data); - - case 2 : { - const int x0 = (int)points(0,0), y0 = (int)points(0,1); - const float u0 = (float)tangents(0,0), v0 = (float)tangents(0,1); - int ox = x0, oy = y0; - float ou = u0, ov = v0; - for (unsigned int i = 1; i - CImg& draw_spline(const CImg& points, - const tc *const color, const float opacity=1, - const bool is_closed_set=false, const float precision=4, - const unsigned int pattern=~0U, const bool init_hatch=true) { - if (is_empty() || !points || points._width<2) return *this; - CImg tangents; - switch (points._height) { - case 0 : case 1 : - throw CImgArgumentException(_cimg_instance - "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).", - cimg_instance, - points._width,points._height,points._depth,points._spectrum,points._data); - case 2 : { - tangents.assign(points._width,points._height); - cimg_forX(points,p) { - const unsigned int - p0 = is_closed_set?(p+points._width-1)%points._width:(p?p-1:0), - p1 = is_closed_set?(p+1)%points._width:(p+1=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ - xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ - _sxn=1, \ - _sxr=1, \ - _sxl=1, \ - _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ - _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ - _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ - _dyn = y2-y1, \ - _dyr = y2-y0, \ - _dyl = y1-y0, \ - _counter = (_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ - _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ - _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ - cimg::min((int)(img)._height-y-1,y2-y)), \ - _errn = _dyn/2, \ - _errr = _dyr/2, \ - _errl = _dyl/2, \ - _rxn = _dyn?(x2-x1)/_dyn:0, \ - _rxr = _dyr?(x2-x0)/_dyr:0, \ - _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ - (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn); \ - _counter>=0; --_counter, ++y, \ - xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ - xl+=(y!=y1)?_rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0): \ - (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) - -#define _cimg_for_triangle2(img,xl,cl,xr,cr,y,x0,y0,c0,x1,y1,c1,x2,y2,c2) \ - for (int y = y0<0?0:y0, \ - xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ - cr = y0>=0?c0:(c0-y0*(c2-c0)/(y2-y0)), \ - xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ - cl = y1>=0?(y0>=0?(y0==y1?c1:c0):(c0-y0*(c1-c0)/(y1-y0))):(c1-y1*(c2-c1)/(y2-y1)), \ - _sxn=1, _scn=1, \ - _sxr=1, _scr=1, \ - _sxl=1, _scl=1, \ - _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ - _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ - _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ - _dcn = c2>c1?c2-c1:(_scn=-1,c1-c2), \ - _dcr = c2>c0?c2-c0:(_scr=-1,c0-c2), \ - _dcl = c1>c0?c1-c0:(_scl=-1,c0-c1), \ - _dyn = y2-y1, \ - _dyr = y2-y0, \ - _dyl = y1-y0, \ - _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ - _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ - _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ - _dcn-=_dyn?_dyn*(_dcn/_dyn):0, \ - _dcr-=_dyr?_dyr*(_dcr/_dyr):0, \ - _dcl-=_dyl?_dyl*(_dcl/_dyl):0, \ - cimg::min((int)(img)._height-y-1,y2-y)), \ - _errn = _dyn/2, _errcn = _errn, \ - _errr = _dyr/2, _errcr = _errr, \ - _errl = _dyl/2, _errcl = _errl, \ - _rxn = _dyn?(x2-x1)/_dyn:0, \ - _rcn = _dyn?(c2-c1)/_dyn:0, \ - _rxr = _dyr?(x2-x0)/_dyr:0, \ - _rcr = _dyr?(c2-c0)/_dyr:0, \ - _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ - (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ - _rcl = (y0!=y1 && y1>0)?(_dyl?(c1-c0)/_dyl:0): \ - (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcn ); \ - _counter>=0; --_counter, ++y, \ - xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ - cr+=_rcr+((_errcr-=_dcr)<0?_errcr+=_dyr,_scr:0), \ - xl+=(y!=y1)?(cl+=_rcl+((_errcl-=_dcl)<0?(_errcl+=_dyl,_scl):0), \ - _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ - (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcl=_rcn, cl=c1, \ - _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) - -#define _cimg_for_triangle3(img,xl,txl,tyl,xr,txr,tyr,y,x0,y0,tx0,ty0,x1,y1,tx1,ty1,x2,y2,tx2,ty2) \ - for (int y = y0<0?0:y0, \ - xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ - txr = y0>=0?tx0:(tx0-y0*(tx2-tx0)/(y2-y0)), \ - tyr = y0>=0?ty0:(ty0-y0*(ty2-ty0)/(y2-y0)), \ - xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ - txl = y1>=0?(y0>=0?(y0==y1?tx1:tx0):(tx0-y0*(tx1-tx0)/(y1-y0))):(tx1-y1*(tx2-tx1)/(y2-y1)), \ - tyl = y1>=0?(y0>=0?(y0==y1?ty1:ty0):(ty0-y0*(ty1-ty0)/(y1-y0))):(ty1-y1*(ty2-ty1)/(y2-y1)), \ - _sxn=1, _stxn=1, _styn=1, \ - _sxr=1, _stxr=1, _styr=1, \ - _sxl=1, _stxl=1, _styl=1, \ - _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ - _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ - _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ - _dtxn = tx2>tx1?tx2-tx1:(_stxn=-1,tx1-tx2), \ - _dtxr = tx2>tx0?tx2-tx0:(_stxr=-1,tx0-tx2), \ - _dtxl = tx1>tx0?tx1-tx0:(_stxl=-1,tx0-tx1), \ - _dtyn = ty2>ty1?ty2-ty1:(_styn=-1,ty1-ty2), \ - _dtyr = ty2>ty0?ty2-ty0:(_styr=-1,ty0-ty2), \ - _dtyl = ty1>ty0?ty1-ty0:(_styl=-1,ty0-ty1), \ - _dyn = y2-y1, \ - _dyr = y2-y0, \ - _dyl = y1-y0, \ - _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ - _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ - _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ - _dtxn-=_dyn?_dyn*(_dtxn/_dyn):0, \ - _dtxr-=_dyr?_dyr*(_dtxr/_dyr):0, \ - _dtxl-=_dyl?_dyl*(_dtxl/_dyl):0, \ - _dtyn-=_dyn?_dyn*(_dtyn/_dyn):0, \ - _dtyr-=_dyr?_dyr*(_dtyr/_dyr):0, \ - _dtyl-=_dyl?_dyl*(_dtyl/_dyl):0, \ - cimg::min((int)(img)._height-y-1,y2-y)), \ - _errn = _dyn/2, _errtxn = _errn, _errtyn = _errn, \ - _errr = _dyr/2, _errtxr = _errr, _errtyr = _errr, \ - _errl = _dyl/2, _errtxl = _errl, _errtyl = _errl, \ - _rxn = _dyn?(x2-x1)/_dyn:0, \ - _rtxn = _dyn?(tx2-tx1)/_dyn:0, \ - _rtyn = _dyn?(ty2-ty1)/_dyn:0, \ - _rxr = _dyr?(x2-x0)/_dyr:0, \ - _rtxr = _dyr?(tx2-tx0)/_dyr:0, \ - _rtyr = _dyr?(ty2-ty0)/_dyr:0, \ - _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ - (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ - _rtxl = (y0!=y1 && y1>0)?(_dyl?(tx1-tx0)/_dyl:0): \ - (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxn ), \ - _rtyl = (y0!=y1 && y1>0)?(_dyl?(ty1-ty0)/_dyl:0): \ - (_errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyn ); \ - _counter>=0; --_counter, ++y, \ - xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ - txr+=_rtxr+((_errtxr-=_dtxr)<0?_errtxr+=_dyr,_stxr:0), \ - tyr+=_rtyr+((_errtyr-=_dtyr)<0?_errtyr+=_dyr,_styr:0), \ - xl+=(y!=y1)?(txl+=_rtxl+((_errtxl-=_dtxl)<0?(_errtxl+=_dyl,_stxl):0), \ - tyl+=_rtyl+((_errtyl-=_dtyl)<0?(_errtyl+=_dyl,_styl):0), \ - _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ - (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxl=_rtxn, txl=tx1, \ - _errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyl=_rtyn, tyl=ty1,\ - _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) - -#define _cimg_for_triangle4(img,xl,cl,txl,tyl,xr,cr,txr,tyr,y,x0,y0,c0,tx0,ty0,x1,y1,c1,tx1,ty1,x2,y2,c2,tx2,ty2) \ - for (int y = y0<0?0:y0, \ - xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ - cr = y0>=0?c0:(c0-y0*(c2-c0)/(y2-y0)), \ - txr = y0>=0?tx0:(tx0-y0*(tx2-tx0)/(y2-y0)), \ - tyr = y0>=0?ty0:(ty0-y0*(ty2-ty0)/(y2-y0)), \ - xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ - cl = y1>=0?(y0>=0?(y0==y1?c1:c0):(c0-y0*(c1-c0)/(y1-y0))):(c1-y1*(c2-c1)/(y2-y1)), \ - txl = y1>=0?(y0>=0?(y0==y1?tx1:tx0):(tx0-y0*(tx1-tx0)/(y1-y0))):(tx1-y1*(tx2-tx1)/(y2-y1)), \ - tyl = y1>=0?(y0>=0?(y0==y1?ty1:ty0):(ty0-y0*(ty1-ty0)/(y1-y0))):(ty1-y1*(ty2-ty1)/(y2-y1)), \ - _sxn=1, _scn=1, _stxn=1, _styn=1, \ - _sxr=1, _scr=1, _stxr=1, _styr=1, \ - _sxl=1, _scl=1, _stxl=1, _styl=1, \ - _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), \ - _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), \ - _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), \ - _dcn = c2>c1?c2-c1:(_scn=-1,c1-c2), \ - _dcr = c2>c0?c2-c0:(_scr=-1,c0-c2), \ - _dcl = c1>c0?c1-c0:(_scl=-1,c0-c1), \ - _dtxn = tx2>tx1?tx2-tx1:(_stxn=-1,tx1-tx2), \ - _dtxr = tx2>tx0?tx2-tx0:(_stxr=-1,tx0-tx2), \ - _dtxl = tx1>tx0?tx1-tx0:(_stxl=-1,tx0-tx1), \ - _dtyn = ty2>ty1?ty2-ty1:(_styn=-1,ty1-ty2), \ - _dtyr = ty2>ty0?ty2-ty0:(_styr=-1,ty0-ty2), \ - _dtyl = ty1>ty0?ty1-ty0:(_styl=-1,ty0-ty1), \ - _dyn = y2-y1, \ - _dyr = y2-y0, \ - _dyl = y1-y0, \ - _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ - _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ - _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ - _dcn-=_dyn?_dyn*(_dcn/_dyn):0, \ - _dcr-=_dyr?_dyr*(_dcr/_dyr):0, \ - _dcl-=_dyl?_dyl*(_dcl/_dyl):0, \ - _dtxn-=_dyn?_dyn*(_dtxn/_dyn):0, \ - _dtxr-=_dyr?_dyr*(_dtxr/_dyr):0, \ - _dtxl-=_dyl?_dyl*(_dtxl/_dyl):0, \ - _dtyn-=_dyn?_dyn*(_dtyn/_dyn):0, \ - _dtyr-=_dyr?_dyr*(_dtyr/_dyr):0, \ - _dtyl-=_dyl?_dyl*(_dtyl/_dyl):0, \ - cimg::min((int)(img)._height-y-1,y2-y)), \ - _errn = _dyn/2, _errcn = _errn, _errtxn = _errn, _errtyn = _errn, \ - _errr = _dyr/2, _errcr = _errr, _errtxr = _errr, _errtyr = _errr, \ - _errl = _dyl/2, _errcl = _errl, _errtxl = _errl, _errtyl = _errl, \ - _rxn = _dyn?(x2-x1)/_dyn:0, \ - _rcn = _dyn?(c2-c1)/_dyn:0, \ - _rtxn = _dyn?(tx2-tx1)/_dyn:0, \ - _rtyn = _dyn?(ty2-ty1)/_dyn:0, \ - _rxr = _dyr?(x2-x0)/_dyr:0, \ - _rcr = _dyr?(c2-c0)/_dyr:0, \ - _rtxr = _dyr?(tx2-tx0)/_dyr:0, \ - _rtyr = _dyr?(ty2-ty0)/_dyr:0, \ - _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ - (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ - _rcl = (y0!=y1 && y1>0)?(_dyl?(c1-c0)/_dyl:0): \ - (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcn ), \ - _rtxl = (y0!=y1 && y1>0)?(_dyl?(tx1-tx0)/_dyl:0): \ - (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxn ), \ - _rtyl = (y0!=y1 && y1>0)?(_dyl?(ty1-ty0)/_dyl:0): \ - (_errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyn ); \ - _counter>=0; --_counter, ++y, \ - xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ - cr+=_rcr+((_errcr-=_dcr)<0?_errcr+=_dyr,_scr:0), \ - txr+=_rtxr+((_errtxr-=_dtxr)<0?_errtxr+=_dyr,_stxr:0), \ - tyr+=_rtyr+((_errtyr-=_dtyr)<0?_errtyr+=_dyr,_styr:0), \ - xl+=(y!=y1)?(cl+=_rcl+((_errcl-=_dcl)<0?(_errcl+=_dyl,_scl):0), \ - txl+=_rtxl+((_errtxl-=_dtxl)<0?(_errtxl+=_dyl,_stxl):0), \ - tyl+=_rtyl+((_errtyl-=_dtyl)<0?(_errtyl+=_dyl,_styl):0), \ - _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ - (_errcl=_errcn, _dcl=_dcn, _dyl=_dyn, _scl=_scn, _rcl=_rcn, cl=c1, \ - _errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxl=_rtxn, txl=tx1, \ - _errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyl=_rtyn, tyl=ty1, \ - _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) - -#define _cimg_for_triangle5(img,xl,txl,tyl,lxl,lyl,xr,txr,tyr,lxr,lyr,y,x0,y0,\ - tx0,ty0,lx0,ly0,x1,y1,tx1,ty1,lx1,ly1,x2,y2,tx2,ty2,lx2,ly2) \ - for (int y = y0<0?0:y0, \ - xr = y0>=0?x0:(x0-y0*(x2-x0)/(y2-y0)), \ - txr = y0>=0?tx0:(tx0-y0*(tx2-tx0)/(y2-y0)), \ - tyr = y0>=0?ty0:(ty0-y0*(ty2-ty0)/(y2-y0)), \ - lxr = y0>=0?lx0:(lx0-y0*(lx2-lx0)/(y2-y0)), \ - lyr = y0>=0?ly0:(ly0-y0*(ly2-ly0)/(y2-y0)), \ - xl = y1>=0?(y0>=0?(y0==y1?x1:x0):(x0-y0*(x1-x0)/(y1-y0))):(x1-y1*(x2-x1)/(y2-y1)), \ - txl = y1>=0?(y0>=0?(y0==y1?tx1:tx0):(tx0-y0*(tx1-tx0)/(y1-y0))):(tx1-y1*(tx2-tx1)/(y2-y1)), \ - tyl = y1>=0?(y0>=0?(y0==y1?ty1:ty0):(ty0-y0*(ty1-ty0)/(y1-y0))):(ty1-y1*(ty2-ty1)/(y2-y1)), \ - lxl = y1>=0?(y0>=0?(y0==y1?lx1:lx0):(lx0-y0*(lx1-lx0)/(y1-y0))):(lx1-y1*(lx2-lx1)/(y2-y1)), \ - lyl = y1>=0?(y0>=0?(y0==y1?ly1:ly0):(ly0-y0*(ly1-ly0)/(y1-y0))):(ly1-y1*(ly2-ly1)/(y2-y1)), \ - _sxn=1, _stxn=1, _styn=1, _slxn=1, _slyn=1, \ - _sxr=1, _stxr=1, _styr=1, _slxr=1, _slyr=1, \ - _sxl=1, _stxl=1, _styl=1, _slxl=1, _slyl=1, \ - _dxn = x2>x1?x2-x1:(_sxn=-1,x1-x2), _dyn = y2-y1, \ - _dxr = x2>x0?x2-x0:(_sxr=-1,x0-x2), _dyr = y2-y0, \ - _dxl = x1>x0?x1-x0:(_sxl=-1,x0-x1), _dyl = y1-y0, \ - _dtxn = tx2>tx1?tx2-tx1:(_stxn=-1,tx1-tx2), \ - _dtxr = tx2>tx0?tx2-tx0:(_stxr=-1,tx0-tx2), \ - _dtxl = tx1>tx0?tx1-tx0:(_stxl=-1,tx0-tx1), \ - _dtyn = ty2>ty1?ty2-ty1:(_styn=-1,ty1-ty2), \ - _dtyr = ty2>ty0?ty2-ty0:(_styr=-1,ty0-ty2), \ - _dtyl = ty1>ty0?ty1-ty0:(_styl=-1,ty0-ty1), \ - _dlxn = lx2>lx1?lx2-lx1:(_slxn=-1,lx1-lx2), \ - _dlxr = lx2>lx0?lx2-lx0:(_slxr=-1,lx0-lx2), \ - _dlxl = lx1>lx0?lx1-lx0:(_slxl=-1,lx0-lx1), \ - _dlyn = ly2>ly1?ly2-ly1:(_slyn=-1,ly1-ly2), \ - _dlyr = ly2>ly0?ly2-ly0:(_slyr=-1,ly0-ly2), \ - _dlyl = ly1>ly0?ly1-ly0:(_slyl=-1,ly0-ly1), \ - _counter =(_dxn-=_dyn?_dyn*(_dxn/_dyn):0, \ - _dxr-=_dyr?_dyr*(_dxr/_dyr):0, \ - _dxl-=_dyl?_dyl*(_dxl/_dyl):0, \ - _dtxn-=_dyn?_dyn*(_dtxn/_dyn):0, \ - _dtxr-=_dyr?_dyr*(_dtxr/_dyr):0, \ - _dtxl-=_dyl?_dyl*(_dtxl/_dyl):0, \ - _dtyn-=_dyn?_dyn*(_dtyn/_dyn):0, \ - _dtyr-=_dyr?_dyr*(_dtyr/_dyr):0, \ - _dtyl-=_dyl?_dyl*(_dtyl/_dyl):0, \ - _dlxn-=_dyn?_dyn*(_dlxn/_dyn):0, \ - _dlxr-=_dyr?_dyr*(_dlxr/_dyr):0, \ - _dlxl-=_dyl?_dyl*(_dlxl/_dyl):0, \ - _dlyn-=_dyn?_dyn*(_dlyn/_dyn):0, \ - _dlyr-=_dyr?_dyr*(_dlyr/_dyr):0, \ - _dlyl-=_dyl?_dyl*(_dlyl/_dyl):0, \ - cimg::min((int)(img)._height-y-1,y2-y)), \ - _errn = _dyn/2, _errtxn = _errn, _errtyn = _errn, _errlxn = _errn, _errlyn = _errn, \ - _errr = _dyr/2, _errtxr = _errr, _errtyr = _errr, _errlxr = _errr, _errlyr = _errr, \ - _errl = _dyl/2, _errtxl = _errl, _errtyl = _errl, _errlxl = _errl, _errlyl = _errl, \ - _rxn = _dyn?(x2-x1)/_dyn:0, \ - _rtxn = _dyn?(tx2-tx1)/_dyn:0, \ - _rtyn = _dyn?(ty2-ty1)/_dyn:0, \ - _rlxn = _dyn?(lx2-lx1)/_dyn:0, \ - _rlyn = _dyn?(ly2-ly1)/_dyn:0, \ - _rxr = _dyr?(x2-x0)/_dyr:0, \ - _rtxr = _dyr?(tx2-tx0)/_dyr:0, \ - _rtyr = _dyr?(ty2-ty0)/_dyr:0, \ - _rlxr = _dyr?(lx2-lx0)/_dyr:0, \ - _rlyr = _dyr?(ly2-ly0)/_dyr:0, \ - _rxl = (y0!=y1 && y1>0)?(_dyl?(x1-x0)/_dyl:0): \ - (_errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxn), \ - _rtxl = (y0!=y1 && y1>0)?(_dyl?(tx1-tx0)/_dyl:0): \ - (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxn ), \ - _rtyl = (y0!=y1 && y1>0)?(_dyl?(ty1-ty0)/_dyl:0): \ - (_errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyn ), \ - _rlxl = (y0!=y1 && y1>0)?(_dyl?(lx1-lx0)/_dyl:0): \ - (_errlxl=_errlxn, _dlxl=_dlxn, _dyl=_dyn, _slxl=_slxn, _rlxn ), \ - _rlyl = (y0!=y1 && y1>0)?(_dyl?(ly1-ly0)/_dyl:0): \ - (_errlyl=_errlyn, _dlyl=_dlyn, _dyl=_dyn, _slyl=_slyn, _rlyn ); \ - _counter>=0; --_counter, ++y, \ - xr+=_rxr+((_errr-=_dxr)<0?_errr+=_dyr,_sxr:0), \ - txr+=_rtxr+((_errtxr-=_dtxr)<0?_errtxr+=_dyr,_stxr:0), \ - tyr+=_rtyr+((_errtyr-=_dtyr)<0?_errtyr+=_dyr,_styr:0), \ - lxr+=_rlxr+((_errlxr-=_dlxr)<0?_errlxr+=_dyr,_slxr:0), \ - lyr+=_rlyr+((_errlyr-=_dlyr)<0?_errlyr+=_dyr,_slyr:0), \ - xl+=(y!=y1)?(txl+=_rtxl+((_errtxl-=_dtxl)<0?(_errtxl+=_dyl,_stxl):0), \ - tyl+=_rtyl+((_errtyl-=_dtyl)<0?(_errtyl+=_dyl,_styl):0), \ - lxl+=_rlxl+((_errlxl-=_dlxl)<0?(_errlxl+=_dyl,_slxl):0), \ - lyl+=_rlyl+((_errlyl-=_dlyl)<0?(_errlyl+=_dyl,_slyl):0), \ - _rxl+((_errl-=_dxl)<0?(_errl+=_dyl,_sxl):0)): \ - (_errtxl=_errtxn, _dtxl=_dtxn, _dyl=_dyn, _stxl=_stxn, _rtxl=_rtxn, txl=tx1, \ - _errtyl=_errtyn, _dtyl=_dtyn, _dyl=_dyn, _styl=_styn, _rtyl=_rtyn, tyl=ty1, \ - _errlxl=_errlxn, _dlxl=_dlxn, _dyl=_dyn, _slxl=_slxn, _rlxl=_rlxn, lxl=lx1, \ - _errlyl=_errlyn, _dlyl=_dlyn, _dyl=_dyn, _slyl=_slyn, _rlyl=_rlyn, lyl=ly1, \ - _errl=_errn, _dxl=_dxn, _dyl=_dyn, _sxl=_sxn, _rxl=_rxn, x1-xl)) - - // [internal] Draw a filled triangle. - template - CImg& _draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const tc *const color, const float opacity, - const float brightness) { - cimg_init_scanline(color,opacity); - const float nbrightness = brightness<0?0:(brightness>2?2:brightness); - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2); - if (ny0=0) { - if ((nx1 - nx0)*(ny2 - ny0) - (nx2 - nx0)*(ny1 - ny0)<0) - _cimg_for_triangle1(*this,xl,xr,y,nx0,ny0,nx1,ny1,nx2,ny2) - cimg_draw_scanline(xl,xr,y,color,opacity,nbrightness); - else - _cimg_for_triangle1(*this,xl,xr,y,nx0,ny0,nx1,ny1,nx2,ny2) - cimg_draw_scanline(xr,xl,y,color,opacity,nbrightness); - } - return *this; - } - - //! Draw a filled 2d triangle. - /** - \param x0 X-coordinate of the first vertex. - \param y0 Y-coordinate of the first vertex. - \param x1 X-coordinate of the second vertex. - \param y1 Y-coordinate of the second vertex. - \param x2 X-coordinate of the third vertex. - \param y2 Y-coordinate of the third vertex. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - _draw_triangle(x0,y0,x1,y1,x2,y2,color,opacity,1); - return *this; - } - - //! Draw a outlined 2d triangle. - /** - \param x0 X-coordinate of the first vertex. - \param y0 Y-coordinate of the first vertex. - \param x1 X-coordinate of the second vertex. - \param y1 Y-coordinate of the second vertex. - \param x2 X-coordinate of the third vertex. - \param y2 Y-coordinate of the third vertex. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the outline pattern. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const tc *const color, const float opacity, - const unsigned int pattern) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - draw_line(x0,y0,x1,y1,color,opacity,pattern,true). - draw_line(x1,y1,x2,y2,color,opacity,pattern,false). - draw_line(x2,y2,x0,y0,color,opacity,pattern,false); - return *this; - } - - //! Draw a filled 2d triangle, with z-buffering. - /** - \param zbuffer Z-buffer image. - \param x0 X-coordinate of the first vertex. - \param y0 Y-coordinate of the first vertex. - \param z0 Z-coordinate of the first vertex. - \param x1 X-coordinate of the second vertex. - \param y1 Y-coordinate of the second vertex. - \param z1 Z-coordinate of the second vertex. - \param x2 X-coordinate of the third vertex. - \param y2 Y-coordinate of the third vertex. - \param z2 Z-coordinate of the third vertex. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - \param brightness Brightness factor. - **/ - template - CImg& draw_triangle(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const tc *const color, const float opacity=1, - const float brightness=1) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float - nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), - nbrightness = brightness<0?0:(brightness>2?2:brightness); - const long whd = (long)_width*_height*_depth, offx = _spectrum*whd; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; - tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nz0,nz2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nz1,nz2); - if (ny0>=height() || ny2<0) return *this; - tzfloat - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); - _cimg_for_triangle1(*this,xleft0,xright0,y,nx0,ny0,nx1,ny1,nx2,ny2) { - if (y==ny1) { zl = nz1; pzl = pzn; } - int xleft = xleft0, xright = xright0; - tzfloat zleft = zl, zright = zr; - if (xright=width()-1) xright = width() - 1; - T* ptrd = data(xleft,y,0,0); - tz *ptrz = zbuffer.data(xleft,y); - if (opacity>=1) { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } - ptrd-=offx; - } - zleft+=pentez; - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)(nbrightness*(*col++)); ptrd+=whd; } - ptrd-=offx; - } - zleft+=pentez; - } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { *ptrd = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); ptrd+=whd; } - ptrd-=offx; - } - zleft+=pentez; - } - } else { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; cimg_forC(*this,c) { *ptrd = (T)(nopacity**(col++) + *ptrd*copacity); ptrd+=whd; } - ptrd-=offx; - } - zleft+=pentez; - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { *ptrd = (T)(nopacity*nbrightness**(col++) + *ptrd*copacity); ptrd+=whd; } - ptrd-=offx; - } - zleft+=pentez; - } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { - const T val = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; - } - ptrd-=offx; - } - zleft+=pentez; - } - } - zr+=pzr; zl+=pzl; - } - return *this; - } - - //! Draw a Gouraud-shaded 2d triangle. - /** - \param x0 X-coordinate of the first vertex in the image instance. - \param y0 Y-coordinate of the first vertex in the image instance. - \param x1 X-coordinate of the second vertex in the image instance. - \param y1 Y-coordinate of the second vertex in the image instance. - \param x2 X-coordinate of the third vertex in the image instance. - \param y2 Y-coordinate of the third vertex in the image instance. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param brightness0 Brightness factor of the first vertex (in [0,2]). - \param brightness1 brightness factor of the second vertex (in [0,2]). - \param brightness2 brightness factor of the third vertex (in [0,2]). - \param opacity Drawing opacity. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const tc *const color, - const float brightness0, - const float brightness1, - const float brightness2, - const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), - nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), - nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nc0,nc1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nc0,nc2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nc1,nc2); - if (ny0>=height() || ny2<0) return *this; - _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { - int xleft = xleft0, xright = xright0, cleft = cleft0, cright = cright0; - if (xrightcleft?cright - cleft:cleft - cright, - rc = dx?(cright - cleft)/dx:0, - sc = cright>cleft?1:-1, - ndc = dc-(dx?dx*(dc/dx):0); - int errc = dx>>1; - if (xleft<0 && dx) cleft-=xleft*(cright - cleft)/dx; - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width() - 1; - T* ptrd = data(xleft,y); - if (opacity>=1) for (int x = xleft; x<=xright; ++x) { - const tc *col = color; - cimg_forC(*this,c) { - *ptrd = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); - ptrd+=whd; - } - ptrd-=offx; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } else for (int x = xleft; x<=xright; ++x) { - const tc *col = color; - cimg_forC(*this,c) { - const T val = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; - } - ptrd-=offx; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } - } - return *this; - } - - //! Draw a Gouraud-shaded 2d triangle, with z-buffering \overloading. - template - CImg& draw_triangle(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const tc *const color, - const float brightness0, - const float brightness1, - const float brightness2, - const float opacity=1) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, offx = _spectrum*whd; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), - nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), - nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); - tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nz0,nz1,nc0,nc1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nz0,nz2,nc0,nc2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nz1,nz2,nc1,nc2); - if (ny0>=height() || ny2<0) return *this; - tzfloat - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); - _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { - if (y==ny1) { zl = nz1; pzl = pzn; } - int xleft = xleft0, xright = xright0, cleft = cleft0, cright = cright0; - tzfloat zleft = zl, zright = zr; - if (xrightcleft?cright - cleft:cleft - cright, - rc = dx?(cright-cleft)/dx:0, - sc = cright>cleft?1:-1, - ndc = dc-(dx?dx*(dc/dx):0); - const tzfloat pentez = (zright - zleft)/dx; - int errc = dx>>1; - if (xleft<0 && dx) { - cleft-=xleft*(cright - cleft)/dx; - zleft-=xleft*(zright - zleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T *ptrd = data(xleft,y); - tz *ptrz = zbuffer.data(xleft,y); - if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { - *ptrd = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); - ptrd+=whd; - } - ptrd-=offx; - } - zleft+=pentez; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } else for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { - const T val = (T)(cleft<256?cleft**(col++)/256:((512-cleft)**(col++)+(cleft-256)*maxval)/256); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; - } - ptrd-=offx; - } - zleft+=pentez; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } - zr+=pzr; zl+=pzl; - } - return *this; - } - - //! Draw a color-interpolated 2d triangle. - /** - \param x0 X-coordinate of the first vertex in the image instance. - \param y0 Y-coordinate of the first vertex in the image instance. - \param x1 X-coordinate of the second vertex in the image instance. - \param y1 Y-coordinate of the second vertex in the image instance. - \param x2 X-coordinate of the third vertex in the image instance. - \param y2 Y-coordinate of the third vertex in the image instance. - \param color1 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the first vertex. - \param color2 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the seconf vertex. - \param color3 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the third vertex. - \param opacity Drawing opacity. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const tc1 *const color1, - const tc2 *const color2, - const tc3 *const color3, - const float opacity=1) { - const unsigned char one = 1; - cimg_forC(*this,c) - get_shared_channel(c).draw_triangle(x0,y0,x1,y1,x2,y2,&one,color1[c],color2[c],color3[c],opacity); - return *this; - } - - //! Draw a textured 2d triangle. - /** - \param x0 X-coordinate of the first vertex in the image instance. - \param y0 Y-coordinate of the first vertex in the image instance. - \param x1 X-coordinate of the second vertex in the image instance. - \param y1 Y-coordinate of the second vertex in the image instance. - \param x2 X-coordinate of the third vertex in the image instance. - \param y2 Y-coordinate of the third vertex in the image instance. - \param texture Texture image used to fill the triangle. - \param tx0 X-coordinate of the first vertex in the texture image. - \param ty0 Y-coordinate of the first vertex in the texture image. - \param tx1 X-coordinate of the second vertex in the texture image. - \param ty1 Y-coordinate of the second vertex in the texture image. - \param tx2 X-coordinate of the third vertex in the texture image. - \param ty2 Y-coordinate of the third vertex in the texture image. - \param opacity Drawing opacity. - \param brightness Brightness factor of the drawing (in [0,2]). - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const float opacity=1, - const float brightness=1) { - if (is_empty()) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float - nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), - nbrightness = brightness<0?0:(brightness>2?2:brightness); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - ntx0 = tx0, nty0 = ty0, ntx1 = tx1, nty1 = ty1, ntx2 = tx2, nty2 = ty2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2); - if (ny0>=height() || ny2<0) return *this; - _cimg_for_triangle3(*this,xleft0,txleft0,tyleft0,xright0,txright0,tyright0,y, - nx0,ny0,ntx0,nty0,nx1,ny1,ntx1,nty1,nx2,ny2,ntx2,nty2) { - int - xleft = xleft0, xright = xright0, - txleft = txleft0, txright = txright0, - tyleft = tyleft0, tyright = tyright0; - if (xrighttxleft?txright - txleft:txleft - txright, - dty = tyright>tyleft?tyright - tyleft:tyleft - tyright, - rtx = dx?(txright - txleft)/dx:0, - rty = dx?(tyright - tyleft)/dx:0, - stx = txright>txleft?1:-1, - sty = tyright>tyleft?1:-1, - ndtx = dtx - (dx?dx*(dtx/dx):0), - ndty = dty - (dx?dx*(dty/dx):0); - int errtx = dx>>1, errty = errtx; - if (xleft<0 && dx) { - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - *ptrd = (T)*col; - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - *ptrd = (T)(nbrightness**col); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } else for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - *ptrd = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } - } else { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - *ptrd = (T)(nopacity**col + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - *ptrd = (T)(nopacity*nbrightness**col + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } else for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - const T val = (T)((2-nbrightness)**(col++) + (nbrightness-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } - } - } - return *this; - } - - //! Draw a 2d textured triangle, with perspective correction. - template - CImg& draw_triangle(const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const float opacity=1, - const float brightness=1) { - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float - nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), - nbrightness = brightness<0?0:(brightness>2?2:brightness); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; - float - ntx0 = tx0/z0, nty0 = ty0/z0, - ntx1 = tx1/z1, nty1 = ty1/z1, - ntx2 = tx2/z2, nty2 = ty2/z2, - nz0 = 1/z0, nz1 = 1/z1, nz2 = 1/z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2); - if (ny0>=height() || ny2<0) return *this; - float - ptxl = (ntx1 - ntx0)/(ny1 - ny0), - ptxr = (ntx2 - ntx0)/(ny2 - ny0), - ptxn = (ntx2 - ntx1)/(ny2 - ny1), - ptyl = (nty1 - nty0)/(ny1 - ny0), - ptyr = (nty2 - nty0)/(ny2 - ny0), - ptyn = (nty2 - nty1)/(ny2 - ny1), - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), - tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))), - txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): - (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), - tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): - (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); - _cimg_for_triangle1(*this,xleft0,xright0,y,nx0,ny0,nx1,ny1,nx2,ny2) { - if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } - int xleft = xleft0, xright = xright0; - float - zleft = zl, zright = zr, - txleft = txl, txright = txr, - tyleft = tyl, tyright = tyr; - if (xright=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)*col; - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else if (nbrightness<1) for (int x=xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(nbrightness**col); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } - } else { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(nopacity**col + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(nopacity*nbrightness**col + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const T val = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } - } - zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; - } - return *this; - } - - //! Draw a textured 2d triangle, with perspective correction and z-buffering. - template - CImg& draw_triangle(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const float opacity=1, - const float brightness=1) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float - nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), - nbrightness = brightness<0?0:(brightness>2?2:brightness); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2; - float - ntx0 = tx0/z0, nty0 = ty0/z0, - ntx1 = tx1/z1, nty1 = ty1/z1, - ntx2 = tx2/z2, nty2 = ty2/z2; - tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2); - if (ny0>=height() || ny2<0) return *this; - float - ptxl = (ntx1 - ntx0)/(ny1 - ny0), - ptxr = (ntx2 - ntx0)/(ny2 - ny0), - ptxn = (ntx2 - ntx1)/(ny2 - ny1), - ptyl = (nty1 - nty0)/(ny1 - ny0), - ptyr = (nty2 - nty0)/(ny2 - ny0), - ptyn = (nty2 - nty1)/(ny2 - ny1), - txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), - tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), - txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): - (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), - tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): - (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); - tzfloat - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); - _cimg_for_triangle1(*this,xleft0,xright0,y,nx0,ny0,nx1,ny1,nx2,ny2) { - if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } - int xleft = xleft0, xright = xright0; - float txleft = txl, txright = txr, tyleft = tyl, tyright = tyr; - tzfloat zleft = zl, zright = zr; - if (xright=width()-1) xright = width()-1; - T *ptrd = data(xleft,y,0,0); - tz *ptrz = zbuffer.data(xleft,y); - if (opacity>=1) { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)*col; - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(nbrightness**col); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } - } else { - if (nbrightness==1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(nopacity**col + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else if (nbrightness<1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(nopacity*nbrightness**col + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const T val = (T)((2-nbrightness)**col + (nbrightness-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - } - } - zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; - } - return *this; - } - - //! Draw a Phong-shaded 2d triangle. - /** - \param x0 X-coordinate of the first vertex in the image instance. - \param y0 Y-coordinate of the first vertex in the image instance. - \param x1 X-coordinate of the second vertex in the image instance. - \param y1 Y-coordinate of the second vertex in the image instance. - \param x2 X-coordinate of the third vertex in the image instance. - \param y2 Y-coordinate of the third vertex in the image instance. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param light Light image. - \param lx0 X-coordinate of the first vertex in the light image. - \param ly0 Y-coordinate of the first vertex in the light image. - \param lx1 X-coordinate of the second vertex in the light image. - \param ly1 Y-coordinate of the second vertex in the light image. - \param lx2 X-coordinate of the third vertex in the light image. - \param ly2 Y-coordinate of the third vertex in the light image. - \param opacity Drawing opacity. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const tc *const color, - const CImg& light, - const int lx0, const int ly0, - const int lx1, const int ly1, - const int lx2, const int ly2, - const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - if (light._depth>1 || light._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", - cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); - if (is_overlapped(light)) return draw_triangle(x0,y0,x1,y1,x2,y2,color,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; - const long whd = (long)_width*_height*_depth, offx = _spectrum*whd-1; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nlx0,nlx1,nly0,nly1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nlx0,nlx2,nly0,nly2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nlx1,nlx2,nly1,nly2); - if (ny0>=height() || ny2<0) return *this; - _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, - nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { - int - xleft = xleft0, xright = xright0, - lxleft = lxleft0, lxright = lxright0, - lyleft = lyleft0, lyright = lyright0; - if (xrightlxleft?lxright - lxleft:lxleft - lxright, - dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, - rlx = dx?(lxright - lxleft)/dx:0, - rly = dx?(lyright - lyleft)/dx:0, - slx = lxright>lxleft?1:-1, - sly = lyright>lyleft?1:-1, - ndlx = dlx - (dx?dx*(dlx/dx):0), - ndly = dly - (dx?dx*(dly/dx):0); - int errlx = dx>>1, errly = errlx; - if (xleft<0 && dx) { - lxleft-=xleft*(lxright - lxleft)/dx; - lyleft-=xleft*(lyright - lyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) for (int x = xleft; x<=xright; ++x) { - const tc *col = color; - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - *ptrd = (T)(l<1?l**(col++):((2-l)**(col++)+(l-1)*maxval)); - ptrd+=whd; - } - ptrd-=offx; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } else for (int x = xleft; x<=xright; ++x) { - const tc *col = color; - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - const T val = (T)(l<1?l**(col++):((2-l)**(col++)+(l-1)*maxval)); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; - } - ptrd-=offx; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } - } - return *this; - } - - //! Draw a Phong-shaded 2d triangle, with z-buffering. - template - CImg& draw_triangle(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const tc *const color, - const CImg& light, - const int lx0, const int ly0, - const int lx1, const int ly1, - const int lx2, const int ly2, - const float opacity=1) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Specified color is (null).", - cimg_instance); - if (light._depth>1 || light._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", - cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - if (is_overlapped(light)) return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color, - +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, offx = _spectrum*whd; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; - tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,nlx0,nlx1,nly0,nly1,nz0,nz1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,nlx0,nlx2,nly0,nly2,nz0,nz2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,nlx1,nlx2,nly1,nly2,nz1,nz2); - if (ny0>=height() || ny2<0) return *this; - tzfloat - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); - _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, - nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { - if (y==ny1) { zl = nz1; pzl = pzn; } - int - xleft = xleft0, xright = xright0, - lxleft = lxleft0, lxright = lxright0, - lyleft = lyleft0, lyright = lyright0; - tzfloat zleft = zl, zright = zr; - if (xrightlxleft?lxright - lxleft:lxleft - lxright, - dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, - rlx = dx?(lxright - lxleft)/dx:0, - rly = dx?(lyright - lyleft)/dx:0, - slx = lxright>lxleft?1:-1, - sly = lyright>lyleft?1:-1, - ndlx = dlx - (dx?dx*(dlx/dx):0), - ndly = dly - (dx?dx*(dly/dx):0); - const tzfloat pentez = (zright - zleft)/dx; - int errlx = dx>>1, errly = errlx; - if (xleft<0 && dx) { - zleft-=xleft*(zright - zleft)/dx; - lxleft-=xleft*(lxright - lxleft)/dx; - lyleft-=xleft*(lyright - lyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T *ptrd = data(xleft,y,0,0); - tz *ptrz = zbuffer.data(xleft,y); - if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - const tc cval = *(col++); - *ptrd = (T)(l<1?l*cval:(2-l)*cval+(l-1)*maxval); - ptrd+=whd; - } - ptrd-=offx; - } - zleft+=pentez; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tc *col = color; - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - const tc cval = *(col++); - const T val = (T)(l<1?l*cval:(2-l)*cval+(l-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; - } - ptrd-=offx; - } - zleft+=pentez; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } - zr+=pzr; zl+=pzl; - } - return *this; - } - - //! Draw a textured Gouraud-shaded 2d triangle. - /** - \param x0 X-coordinate of the first vertex in the image instance. - \param y0 Y-coordinate of the first vertex in the image instance. - \param x1 X-coordinate of the second vertex in the image instance. - \param y1 Y-coordinate of the second vertex in the image instance. - \param x2 X-coordinate of the third vertex in the image instance. - \param y2 Y-coordinate of the third vertex in the image instance. - \param texture Texture image used to fill the triangle. - \param tx0 X-coordinate of the first vertex in the texture image. - \param ty0 Y-coordinate of the first vertex in the texture image. - \param tx1 X-coordinate of the second vertex in the texture image. - \param ty1 Y-coordinate of the second vertex in the texture image. - \param tx2 X-coordinate of the third vertex in the texture image. - \param ty2 Y-coordinate of the third vertex in the texture image. - \param brightness0 Brightness factor of the first vertex. - \param brightness1 Brightness factor of the second vertex. - \param brightness2 Brightness factor of the third vertex. - \param opacity Drawing opacity. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const float brightness0, - const float brightness1, - const float brightness2, - const float opacity=1) { - if (is_empty()) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, - brightness0,brightness1,brightness2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - ntx0 = tx0, nty0 = ty0, ntx1 = tx1, nty1 = ty1, ntx2 = tx2, nty2 = ty2, - nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), - nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), - nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nc0,nc1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nc0,nc2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nc1,nc2); - if (ny0>=height() || ny2<0) return *this; - _cimg_for_triangle4(*this,xleft0,cleft0,txleft0,tyleft0,xright0,cright0,txright0,tyright0,y, - nx0,ny0,nc0,ntx0,nty0,nx1,ny1,nc1,ntx1,nty1,nx2,ny2,nc2,ntx2,nty2) { - int - xleft = xleft0, xright = xright0, - cleft = cleft0, cright = cright0, - txleft = txleft0, txright = txright0, - tyleft = tyleft0, tyright = tyright0; - if (xrightcleft?cright - cleft:cleft - cright, - dtx = txright>txleft?txright - txleft:txleft - txright, - dty = tyright>tyleft?tyright - tyleft:tyleft - tyright, - rc = dx?(cright - cleft)/dx:0, - rtx = dx?(txright - txleft)/dx:0, - rty = dx?(tyright - tyleft)/dx:0, - sc = cright>cleft?1:-1, - stx = txright>txleft?1:-1, - sty = tyright>tyleft?1:-1, - ndc = dc - (dx?dx*(dc/dx):0), - ndtx = dtx - (dx?dx*(dtx/dx):0), - ndty = dty - (dx?dx*(dty/dx):0); - int errc = dx>>1, errtx = errc, errty = errc; - if (xleft<0 && dx) { - cleft-=xleft*(cright - cleft)/dx; - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - *ptrd = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } else for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - const T val = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } - } - return *this; - } - - //! Draw a textured Gouraud-shaded 2d triangle, with perspective correction \overloading. - template - CImg& draw_triangle(const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const float brightness0, - const float brightness1, - const float brightness2, - const float opacity=1) { - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, - brightness0,brightness1,brightness2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), - nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), - nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); - float - ntx0 = tx0/z0, nty0 = ty0/z0, - ntx1 = tx1/z1, nty1 = ty1/z1, - ntx2 = tx2/z2, nty2 = ty2/z2, - nz0 = 1/z0, nz1 = 1/z1, nz2 = 1/z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1,nc0,nc1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2,nc0,nc2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2,nc1,nc2); - if (ny0>=height() || ny2<0) return *this; - float - ptxl = (ntx1 - ntx0)/(ny1 - ny0), - ptxr = (ntx2 - ntx0)/(ny2 - ny0), - ptxn = (ntx2 - ntx1)/(ny2 - ny1), - ptyl = (nty1 - nty0)/(ny1 - ny0), - ptyr = (nty2 - nty0)/(ny2 - ny0), - ptyn = (nty2 - nty1)/(ny2 - ny1), - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), - tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))), - txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): - (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), - tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): - (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); - _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { - if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } - int - xleft = xleft0, xright = xright0, - cleft = cleft0, cright = cright0; - float - zleft = zl, zright = zr, - txleft = txl, txright = txr, - tyleft = tyl, tyright = tyr; - if (xrightcleft?cright - cleft:cleft - cright, - rc = dx?(cright - cleft)/dx:0, - sc = cright>cleft?1:-1, - ndc = dc - (dx?dx*(dc/dx):0); - const float - pentez = (zright - zleft)/dx, - pentetx = (txright - txleft)/dx, - pentety = (tyright - tyleft)/dx; - int errc = dx>>1; - if (xleft<0 && dx) { - cleft-=xleft*(cright - cleft)/dx; - zleft-=xleft*(zright - zleft)/dx; - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } else for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const T val = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } - zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; - } - return *this; - } - - //! Draw a textured Gouraud-shaded 2d triangle, with perspective correction and z-buffering \overloading. - template - CImg& draw_triangle(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const float brightness0, - const float brightness1, - const float brightness2, - const float opacity=1) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (is_overlapped(texture)) - return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, - brightness0,brightness1,brightness2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nc0 = (int)((brightness0<0.0f?0.0f:(brightness0>2.0f?2.0f:brightness0))*256.0f), - nc1 = (int)((brightness1<0.0f?0.0f:(brightness1>2.0f?2.0f:brightness1))*256.0f), - nc2 = (int)((brightness2<0.0f?0.0f:(brightness2>2.0f?2.0f:brightness2))*256.0f); - float - ntx0 = tx0/z0, nty0 = ty0/z0, - ntx1 = tx1/z1, nty1 = ty1/z1, - ntx2 = tx2/z2, nty2 = ty2/z2; - tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nz0,nz1,nc0,nc1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nz0,nz2,nc0,nc2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nz1,nz2,nc1,nc2); - if (ny0>=height() || ny2<0) return *this; - float - ptxl = (ntx1 - ntx0)/(ny1 - ny0), - ptxr = (ntx2 - ntx0)/(ny2 - ny0), - ptxn = (ntx2 - ntx1)/(ny2 - ny1), - ptyl = (nty1 - nty0)/(ny1 - ny0), - ptyr = (nty2 - nty0)/(ny2 - ny0), - ptyn = (nty2 - nty1)/(ny2 - ny1), - txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), - tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), - txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): - (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), - tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): - (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); - tzfloat - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); - _cimg_for_triangle2(*this,xleft0,cleft0,xright0,cright0,y,nx0,ny0,nc0,nx1,ny1,nc1,nx2,ny2,nc2) { - if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } - int xleft = xleft0, xright = xright0, cleft = cleft0, cright = cright0; - float txleft = txl, txright = txr, tyleft = tyl, tyright = tyr; - tzfloat zleft = zl, zright = zr; - if (xrightcleft?cright - cleft:cleft - cright, - rc = dx?(cright - cleft)/dx:0, - sc = cright>cleft?1:-1, - ndc = dc - (dx?dx*(dc/dx):0); - float pentetx = (txright - txleft)/dx, pentety = (tyright - tyleft)/dx; - const tzfloat pentez = (zright - zleft)/dx; - int errc = dx>>1; - if (xleft<0 && dx) { - cleft-=xleft*(cright - cleft)/dx; - zleft-=xleft*(zright - zleft)/dx; - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y); - tz *ptrz = zbuffer.data(xleft,y); - if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - *ptrd = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } else for (int x = xleft; x<=xright; ++x, ++ptrd, ++ptrz) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const T val = (T)(cleft<256?cleft**col/256:((512-cleft)**col+(cleft-256)*maxval)/256); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - cleft+=rc+((errc-=ndc)<0?errc+=dx,sc:0); - } - zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; - } - return *this; - } - - //! Draw a textured Phong-shaded 2d triangle. - /** - \param x0 X-coordinate of the first vertex in the image instance. - \param y0 Y-coordinate of the first vertex in the image instance. - \param x1 X-coordinate of the second vertex in the image instance. - \param y1 Y-coordinate of the second vertex in the image instance. - \param x2 X-coordinate of the third vertex in the image instance. - \param y2 Y-coordinate of the third vertex in the image instance. - \param texture Texture image used to fill the triangle. - \param tx0 X-coordinate of the first vertex in the texture image. - \param ty0 Y-coordinate of the first vertex in the texture image. - \param tx1 X-coordinate of the second vertex in the texture image. - \param ty1 Y-coordinate of the second vertex in the texture image. - \param tx2 X-coordinate of the third vertex in the texture image. - \param ty2 Y-coordinate of the third vertex in the texture image. - \param light Light image. - \param lx0 X-coordinate of the first vertex in the light image. - \param ly0 Y-coordinate of the first vertex in the light image. - \param lx1 X-coordinate of the second vertex in the light image. - \param ly1 Y-coordinate of the second vertex in the light image. - \param lx2 X-coordinate of the third vertex in the light image. - \param ly2 Y-coordinate of the third vertex in the light image. - \param opacity Drawing opacity. - **/ - template - CImg& draw_triangle(const int x0, const int y0, - const int x1, const int y1, - const int x2, const int y2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const CImg& light, - const int lx0, const int ly0, - const int lx1, const int ly1, - const int lx2, const int ly2, - const float opacity=1) { - if (is_empty()) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (light._depth>1 || light._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", - cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); - if (is_overlapped(texture)) - return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - if (is_overlapped(light)) - return draw_triangle(x0,y0,x1,y1,x2,y2,texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - ntx0 = tx0, nty0 = ty0, ntx1 = tx1, nty1 = ty1, ntx2 = tx2, nty2 = ty2, - nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nlx0,nlx1,nly0,nly1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nlx0,nlx2,nly0,nly2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nlx1,nlx2,nly1,nly2); - if (ny0>=height() || ny2<0) return *this; - _cimg_for_triangle5(*this,xleft0,lxleft0,lyleft0,txleft0,tyleft0,xright0,lxright0,lyright0,txright0,tyright0,y, - nx0,ny0,nlx0,nly0,ntx0,nty0,nx1,ny1,nlx1,nly1,ntx1,nty1,nx2,ny2,nlx2,nly2,ntx2,nty2) { - int - xleft = xleft0, xright = xright0, - lxleft = lxleft0, lxright = lxright0, - lyleft = lyleft0, lyright = lyright0, - txleft = txleft0, txright = txright0, - tyleft = tyleft0, tyright = tyright0; - if (xrightlxleft?lxright - lxleft:lxleft - lxright, - dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, - dtx = txright>txleft?txright - txleft:txleft - txright, - dty = tyright>tyleft?tyright - tyleft:tyleft - tyright, - rlx = dx?(lxright - lxleft)/dx:0, - rly = dx?(lyright - lyleft)/dx:0, - rtx = dx?(txright - txleft)/dx:0, - rty = dx?(tyright - tyleft)/dx:0, - slx = lxright>lxleft?1:-1, - sly = lyright>lyleft?1:-1, - stx = txright>txleft?1:-1, - sty = tyright>tyleft?1:-1, - ndlx = dlx - (dx?dx*(dlx/dx):0), - ndly = dly - (dx?dx*(dly/dx):0), - ndtx = dtx - (dx?dx*(dtx/dx):0), - ndty = dty - (dx?dx*(dty/dx):0); - int errlx = dx>>1, errly = errlx, errtx = errlx, errty = errlx; - if (xleft<0 && dx) { - lxleft-=xleft*(lxright - lxleft)/dx; - lyleft-=xleft*(lyright - lyleft)/dx; - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - *ptrd = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } else for (int x = xleft; x<=xright; ++x) { - const tc *col = texture.data(txleft,tyleft); - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - const T val = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - txleft+=rtx+((errtx-=ndtx)<0?errtx+=dx,stx:0); - tyleft+=rty+((errty-=ndty)<0?errty+=dx,sty:0); - } - } - return *this; - } - - //! Draw a textured Phong-shaded 2d triangle, with perspective correction. - template - CImg& draw_triangle(const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const CImg& light, - const int lx0, const int ly0, - const int lx1, const int ly1, - const int lx2, const int ly2, - const float opacity=1) { - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (light._depth>1 || light._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", - cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); - if (is_overlapped(texture)) - return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2, - light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - if (is_overlapped(light)) - return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,texture,tx0,ty0,tx1,ty1,tx2,ty2, - +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd-1; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; - float - ntx0 = tx0/z0, nty0 = ty0/z0, - ntx1 = tx1/z1, nty1 = ty1/z1, - ntx2 = tx2/z2, nty2 = ty2/z2, - nz0 = 1/z0, nz1 = 1/z1, nz2 = 1/z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nlx0,nlx1,nly0,nly1,nz0,nz1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nlx0,nlx2,nly0,nly2,nz0,nz2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nlx1,nlx2,nly1,nly2,nz1,nz2); - if (ny0>=height() || ny2<0) return *this; - float - ptxl = (ntx1 - ntx0)/(ny1 - ny0), - ptxr = (ntx2 - ntx0)/(ny2 - ny0), - ptxn = (ntx2 - ntx1)/(ny2 - ny1), - ptyl = (nty1 - nty0)/(ny1 - ny0), - ptyr = (nty2 - nty0)/(ny2 - ny0), - ptyn = (nty2 - nty1)/(ny2 - ny1), - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), - tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))), - txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): - (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), - tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): - (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); - _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, - nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { - if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } - int - xleft = xleft0, xright = xright0, - lxleft = lxleft0, lxright = lxright0, - lyleft = lyleft0, lyright = lyright0; - float - zleft = zl, zright = zr, - txleft = txl, txright = txr, - tyleft = tyl, tyright = tyr; - if (xrightlxleft?lxright - lxleft:lxleft - lxright, - dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, - rlx = dx?(lxright - lxleft)/dx:0, - rly = dx?(lyright - lyleft)/dx:0, - slx = lxright>lxleft?1:-1, - sly = lyright>lyleft?1:-1, - ndlx = dlx - (dx?dx*(dlx/dx):0), - ndly = dly - (dx?dx*(dly/dx):0); - const float - pentez = (zright - zleft)/dx, - pentetx = (txright - txleft)/dx, - pentety = (tyright - tyleft)/dx; - int errlx = dx>>1, errly = errlx; - if (xleft<0 && dx) { - zleft-=xleft*(zright - zleft)/dx; - lxleft-=xleft*(lxright - lxleft)/dx; - lyleft-=xleft*(lyright - lyleft)/dx; - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y,0,0); - if (opacity>=1) for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - *ptrd = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } else for (int x = xleft; x<=xright; ++x) { - const float invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - const T val = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } - zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; - } - return *this; - } - - //! Draw a textured Phong-shaded 2d triangle, with perspective correction and z-buffering. - template - CImg& draw_triangle(CImg& zbuffer, - const int x0, const int y0, const float z0, - const int x1, const int y1, const float z1, - const int x2, const int y2, const float z2, - const CImg& texture, - const int tx0, const int ty0, - const int tx1, const int ty1, - const int tx2, const int ty2, - const CImg& light, - const int lx0, const int ly0, - const int lx1, const int ly1, - const int lx2, const int ly2, - const float opacity=1) { - typedef typename cimg::superset::type tzfloat; - if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this; - if (!is_sameXY(zbuffer)) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have " - "different dimensions.", - cimg_instance, - zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data); - if (texture._depth>1 || texture._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).", - cimg_instance, - texture._width,texture._height,texture._depth,texture._spectrum,texture._data); - if (light._depth>1 || light._spectrum<_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).", - cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data); - if (is_overlapped(texture)) - return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2, - +texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - if (is_overlapped(light)) - return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2, - texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - static const T maxval = (T)cimg::min(cimg::type::max(),cimg::type::max()); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const long whd = (long)_width*_height*_depth, twhd = (long)texture._width*texture._height*texture._depth, - offx = _spectrum*whd; - int nx0 = x0, ny0 = y0, nx1 = x1, ny1 = y1, nx2 = x2, ny2 = y2, - nlx0 = lx0, nly0 = ly0, nlx1 = lx1, nly1 = ly1, nlx2 = lx2, nly2 = ly2; - float - ntx0 = tx0/z0, nty0 = ty0/z0, - ntx1 = tx1/z1, nty1 = ty1/z1, - ntx2 = tx2/z2, nty2 = ty2/z2; - tzfloat nz0 = 1/(tzfloat)z0, nz1 = 1/(tzfloat)z1, nz2 = 1/(tzfloat)z2; - if (ny0>ny1) cimg::swap(nx0,nx1,ny0,ny1,ntx0,ntx1,nty0,nty1,nlx0,nlx1,nly0,nly1,nz0,nz1); - if (ny0>ny2) cimg::swap(nx0,nx2,ny0,ny2,ntx0,ntx2,nty0,nty2,nlx0,nlx2,nly0,nly2,nz0,nz2); - if (ny1>ny2) cimg::swap(nx1,nx2,ny1,ny2,ntx1,ntx2,nty1,nty2,nlx1,nlx2,nly1,nly2,nz1,nz2); - if (ny0>=height() || ny2<0) return *this; - float - ptxl = (ntx1 - ntx0)/(ny1 - ny0), - ptxr = (ntx2 - ntx0)/(ny2 - ny0), - ptxn = (ntx2 - ntx1)/(ny2 - ny1), - ptyl = (nty1 - nty0)/(ny1 - ny0), - ptyr = (nty2 - nty0)/(ny2 - ny0), - ptyn = (nty2 - nty1)/(ny2 - ny1), - txr = ny0>=0?ntx0:(ntx0 - ny0*(ntx2 - ntx0)/(ny2 - ny0)), - tyr = ny0>=0?nty0:(nty0 - ny0*(nty2 - nty0)/(ny2 - ny0)), - txl = ny1>=0?(ny0>=0?ntx0:(ntx0 - ny0*(ntx1 - ntx0)/(ny1 - ny0))): - (ptxl=ptxn,(ntx1 - ny1*(ntx2 - ntx1)/(ny2 - ny1))), - tyl = ny1>=0?(ny0>=0?nty0:(nty0 - ny0*(nty1 - nty0)/(ny1 - ny0))): - (ptyl=ptyn,(nty1 - ny1*(nty2 - nty1)/(ny2 - ny1))); - tzfloat - pzl = (nz1 - nz0)/(ny1 - ny0), - pzr = (nz2 - nz0)/(ny2 - ny0), - pzn = (nz2 - nz1)/(ny2 - ny1), - zr = ny0>=0?nz0:(nz0 - ny0*(nz2 - nz0)/(ny2 - ny0)), - zl = ny1>=0?(ny0>=0?nz0:(nz0 - ny0*(nz1 - nz0)/(ny1 - ny0))):(pzl=pzn,(nz1 - ny1*(nz2 - nz1)/(ny2 - ny1))); - _cimg_for_triangle3(*this,xleft0,lxleft0,lyleft0,xright0,lxright0,lyright0,y, - nx0,ny0,nlx0,nly0,nx1,ny1,nlx1,nly1,nx2,ny2,nlx2,nly2) { - if (y==ny1) { zl = nz1; txl = ntx1; tyl = nty1; pzl = pzn; ptxl = ptxn; ptyl = ptyn; } - int - xleft = xleft0, xright = xright0, - lxleft = lxleft0, lxright = lxright0, - lyleft = lyleft0, lyright = lyright0; - float txleft = txl, txright = txr, tyleft = tyl, tyright = tyr; - tzfloat zleft = zl, zright = zr; - if (xrightlxleft?lxright - lxleft:lxleft - lxright, - dly = lyright>lyleft?lyright - lyleft:lyleft - lyright, - rlx = dx?(lxright - lxleft)/dx:0, - rly = dx?(lyright - lyleft)/dx:0, - slx = lxright>lxleft?1:-1, - sly = lyright>lyleft?1:-1, - ndlx = dlx - (dx?dx*(dlx/dx):0), - ndly = dly - (dx?dx*(dly/dx):0); - float pentetx = (txright - txleft)/dx, pentety = (tyright - tyleft)/dx; - const tzfloat pentez = (zright - zleft)/dx; - int errlx = dx>>1, errly = errlx; - if (xleft<0 && dx) { - zleft-=xleft*(zright - zleft)/dx; - lxleft-=xleft*(lxright - lxleft)/dx; - lyleft-=xleft*(lyright - lyleft)/dx; - txleft-=xleft*(txright - txleft)/dx; - tyleft-=xleft*(tyright - tyleft)/dx; - } - if (xleft<0) xleft = 0; - if (xright>=width()-1) xright = width()-1; - T* ptrd = data(xleft,y); - tz *ptrz = zbuffer.data(xleft,y); - if (opacity>=1) for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - *ptrd = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } else for (int x = xleft; x<=xright; ++x, ++ptrz, ++ptrd) { - if (zleft>=(tzfloat)*ptrz) { - *ptrz = (tz)zleft; - const tzfloat invz = 1/zleft; - const tc *col = texture.data((int)(txleft*invz),(int)(tyleft*invz)); - cimg_forC(*this,c) { - const tl l = light(lxleft,lyleft,c); - const T val = (T)(l<1?l**col:(2-l)**col+(l-1)*maxval); - *ptrd = (T)(nopacity*val + *ptrd*copacity); - ptrd+=whd; col+=twhd; - } - ptrd-=offx; - } - zleft+=pentez; txleft+=pentetx; tyleft+=pentety; - lxleft+=rlx+((errlx-=ndlx)<0?errlx+=dx,slx:0); - lyleft+=rly+((errly-=ndly)<0?errly+=dx,sly:0); - } - zr+=pzr; txr+=ptxr; tyr+=ptyr; zl+=pzl; txl+=ptxl; tyl+=ptyl; - } - return *this; - } - - //! Draw a filled 4d rectangle. - /** - \param x0 X-coordinate of the upper-left rectangle corner. - \param y0 Y-coordinate of the upper-left rectangle corner. - \param z0 Z-coordinate of the upper-left rectangle corner. - \param c0 C-coordinate of the upper-left rectangle corner. - \param x1 X-coordinate of the lower-right rectangle corner. - \param y1 Y-coordinate of the lower-right rectangle corner. - \param z1 Z-coordinate of the lower-right rectangle corner. - \param c1 C-coordinate of the lower-right rectangle corner. - \param val Scalar value used to fill the rectangle area. - \param opacity Drawing opacity. - **/ - CImg& draw_rectangle(const int x0, const int y0, const int z0, const int c0, - const int x1, const int y1, const int z1, const int c1, - const T val, const float opacity=1) { - if (is_empty()) return *this; - const bool bx = (x0=width()?width() - 1 - nx1:0) + (nx0<0?nx0:0), - lY = (1 + ny1 - ny0) + (ny1>=height()?height() - 1 - ny1:0) + (ny0<0?ny0:0), - lZ = (1 + nz1 - nz0) + (nz1>=depth()?depth() - 1 - nz1:0) + (nz0<0?nz0:0), - lC = (1 + nc1 - nc0) + (nc1>=spectrum()?spectrum() - 1 - nc1:0) + (nc0<0?nc0:0); - const unsigned long - offX = (unsigned long)_width - lX, - offY = (unsigned long)_width*(_height - lY), - offZ = (unsigned long)_width*_height*(_depth - lZ); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - T *ptrd = data(nx0<0?0:nx0,ny0<0?0:ny0,nz0<0?0:nz0,nc0<0?0:nc0); - if (lX>0 && lY>0 && lZ>0 && lC>0) - for (int v = 0; v=1) { - if (sizeof(T)!=1) { for (int x = 0; x - CImg& draw_rectangle(const int x0, const int y0, const int z0, - const int x1, const int y1, const int z1, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_rectangle(): Specified color is (null).", - cimg_instance); - cimg_forC(*this,c) draw_rectangle(x0,y0,z0,c,x1,y1,z1,c,(T)color[c],opacity); - return *this; - } - - //! Draw an outlined 3d rectangle \overloading. - template - CImg& draw_rectangle(const int x0, const int y0, const int z0, - const int x1, const int y1, const int z1, - const tc *const color, const float opacity, - const unsigned int pattern) { - return draw_line(x0,y0,z0,x1,y0,z0,color,opacity,pattern,true). - draw_line(x1,y0,z0,x1,y1,z0,color,opacity,pattern,false). - draw_line(x1,y1,z0,x0,y1,z0,color,opacity,pattern,false). - draw_line(x0,y1,z0,x0,y0,z0,color,opacity,pattern,false). - draw_line(x0,y0,z1,x1,y0,z1,color,opacity,pattern,true). - draw_line(x1,y0,z1,x1,y1,z1,color,opacity,pattern,false). - draw_line(x1,y1,z1,x0,y1,z1,color,opacity,pattern,false). - draw_line(x0,y1,z1,x0,y0,z1,color,opacity,pattern,false). - draw_line(x0,y0,z0,x0,y0,z1,color,opacity,pattern,true). - draw_line(x1,y0,z0,x1,y0,z1,color,opacity,pattern,true). - draw_line(x1,y1,z0,x1,y1,z1,color,opacity,pattern,true). - draw_line(x0,y1,z0,x0,y1,z1,color,opacity,pattern,true); - } - - //! Draw a filled 2d rectangle. - /** - \param x0 X-coordinate of the upper-left rectangle corner. - \param y0 Y-coordinate of the upper-left rectangle corner. - \param x1 X-coordinate of the lower-right rectangle corner. - \param y1 Y-coordinate of the lower-right rectangle corner. - \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color. - \param opacity Drawing opacity. - **/ - template - CImg& draw_rectangle(const int x0, const int y0, - const int x1, const int y1, - const tc *const color, const float opacity=1) { - return draw_rectangle(x0,y0,0,x1,y1,_depth-1,color,opacity); - } - - //! Draw a outlined 2d rectangle \overloading. - template - CImg& draw_rectangle(const int x0, const int y0, - const int x1, const int y1, - const tc *const color, const float opacity, - const unsigned int pattern) { - if (is_empty()) return *this; - if (y0==y1) return draw_line(x0,y0,x1,y0,color,opacity,pattern,true); - if (x0==x1) return draw_line(x0,y0,x0,y1,color,opacity,pattern,true); - const bool bx = (x0 - CImg& draw_polygon(const CImg& points, - const tc *const color, const float opacity=1) { - if (is_empty() || !points) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_polygon(): Specified color is (null).", - cimg_instance); - - // Normalize 2d input coordinates (remove adjacent duplicates). - CImg npoints(points._width,2); - unsigned int nb_points = 1, p = 0; - int cx = npoints(0,0) = (int)points(0,0), cy = npoints(0,1) = (int)points(0,1); - const int cx0 = cx, cy0 = cy; - for (p = 1; p npoints_x = npoints.get_shared_row(0), npoints_y = npoints.get_shared_row(1); - int xmax = 0, xmin = (int)npoints_x.min_max(xmax), ymax = 0, ymin = (int)npoints_y.min_max(ymax); - if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this; - if (ymin==ymax) return cimg_draw_scanline(xmin,xmax,ymin,color,opacity,1); - const unsigned int - nxmin = xmin<0?0:(unsigned int)xmin, nxmax = xmax>=width()?_width-1:(unsigned int)xmax, - nymin = ymin<0?0:(unsigned int)ymin, nymax = ymax>=height()?_height-1:(unsigned int)ymax, - dx = 1 + nxmax - nxmin, - dy = 1 + nymax - nymin; - npoints_x-=nxmin; npoints_y-=nymin; - unsigned char one = 1; - const CImg mask = CImg(dx,dy,1,1,0).draw_polygon(npoints,&one,1); - CImg _color(dx,dy,1,spectrum()); - cimg_forC(_color,c) _color.get_shared_channel(c).fill(color[c]); - return draw_image(nxmin,nymin,0,0,_color,mask,opacity,1); - } - - // Draw polygon segments. - int - xmax = 0, xmin = (int)npoints.get_shared_points(0,nb_points-1,0).min_max(xmax), - ymax = 0, ymin = (int)npoints.get_shared_points(0,nb_points-1,1).min_max(ymax); - if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this; - if (ymin==ymax) return cimg_draw_scanline(xmin,xmax,ymin,color,1,1); - const unsigned int - nymin = ymin<0?0:(unsigned int)ymin, - nymax = ymax>=height()?_height-1:(unsigned int)ymax, - dy = 1 + nymax - nymin; - CImg X(1+2*nb_points,dy,1,1,0), tmp; - cx = (int)npoints(0,0), cy = (int)npoints(0,1); - unsigned int cp = 0; - for (unsigned int p = 0; pay && cy>ny))?1:0; - for (int x = cx, y = y0, _sx = 1, _sy = 1, - _dx = nx>cx?nx-cx:((_sx=-1),cx-nx), - _dy = y1>y0?y1-y0:((_sy=-1),y0-y1), - _counter = ((_dx-=_dy?_dy*(_dx/_dy):0),_dy), - _err = _dx>>1, - _rx = _dy?(nx-cx)/_dy:0; - _counter>=countermin; - --_counter, y+=_sy, x+=_rx + ((_err-=_dx)<0?_err+=_dy,_sx:0)) - if (y>=0 && y<(int)dy) X(++X(0,y),y) = x; - cp = np; cx = nx; cy = ny; - } else { - const int pp = (cp?cp-1:nb_points-1), py = (int)npoints(pp,1); - if (y0>=0 && y0<(int)dy) { - cimg_draw_scanline(cxpy && ay>cy) || (cy - CImg& draw_polygon(const CImg& points, - const tc *const color, const float opacity, const unsigned int pattern) { - if (is_empty() || !points || points._width<3) return *this; - bool ninit_hatch = true; - switch (points._height) { - case 0 : case 1 : - throw CImgArgumentException(_cimg_instance - "draw_polygon(): Invalid specified point set.", - cimg_instance); - case 2 : { // 2d version. - CImg npoints(points._width,2); - int x = npoints(0,0) = (int)points(0,0), y = npoints(0,1) = (int)points(0,1); - unsigned int nb_points = 1; - for (unsigned int p = 1; p npoints(points._width,3); - int - x = npoints(0,0) = (int)points(0,0), - y = npoints(0,1) = (int)points(0,1), - z = npoints(0,2) = (int)points(0,2); - unsigned int nb_points = 1; - for (unsigned int p = 1; p - CImg& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle, - const tc *const color, const float opacity=1) { - return _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,0U); - } - - //! Draw a filled 2d ellipse \overloading. - /** - \param x0 X-coordinate of the ellipse center. - \param y0 Y-coordinate of the ellipse center. - \param tensor Diffusion tensor describing the ellipse. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - **/ - template - CImg& draw_ellipse(const int x0, const int y0, const CImg &tensor, - const tc *const color, const float opacity=1) { - CImgList eig = tensor.get_symmetric_eigen(); - const CImg &val = eig[0], &vec = eig[1]; - return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)), - std::atan2(vec(0,1),vec(0,0))*180/cimg::PI, - color,opacity); - } - - //! Draw an outlined 2d ellipse. - /** - \param x0 X-coordinate of the ellipse center. - \param y0 Y-coordinate of the ellipse center. - \param r1 First radius of the ellipse. - \param r2 Second radius of the ellipse. - \param angle Angle of the first radius. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the outline pattern. - **/ - template - CImg& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle, - const tc *const color, const float opacity, const unsigned int pattern) { - if (pattern) _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,pattern); - return *this; - } - - //! Draw an outlined 2d ellipse \overloading. - /** - \param x0 X-coordinate of the ellipse center. - \param y0 Y-coordinate of the ellipse center. - \param tensor Diffusion tensor describing the ellipse. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \param pattern An integer whose bits describe the outline pattern. - **/ - template - CImg& draw_ellipse(const int x0, const int y0, const CImg &tensor, - const tc *const color, const float opacity, - const unsigned int pattern) { - CImgList eig = tensor.get_symmetric_eigen(); - const CImg &val = eig[0], &vec = eig[1]; - return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)), - std::atan2(vec(0,1),vec(0,0))*180/cimg::PI, - color,opacity,pattern); - } - - template - CImg& _draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle, - const tc *const color, const float opacity, - const unsigned int pattern) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_ellipse(): Specified color is (null).", - cimg_instance); - if (r1<=0 || r2<=0) return draw_point(x0,y0,color,opacity); - cimg_init_scanline(color,opacity); - const float - nr1 = cimg::abs(r1), nr2 = cimg::abs(r2), - nangle = (float)(angle*cimg::PI/180), - u = (float)std::cos(nangle), - v = (float)std::sin(nangle), - rmax = cimg::max(nr1,nr2), - l1 = (float)std::pow(rmax/(nr1>0?nr1:1e-6),2), - l2 = (float)std::pow(rmax/(nr2>0?nr2:1e-6),2), - a = l1*u*u + l2*v*v, - b = u*v*(l1-l2), - c = l1*v*v + l2*u*u; - const int - yb = (int)std::sqrt(a*rmax*rmax/(a*c - b*b)), - tymin = y0 - yb - 1, - tymax = y0 + yb + 1, - ymin = tymin<0?0:tymin, - ymax = tymax>=height()?height()-1:tymax; - int oxmin = 0, oxmax = 0; - bool first_line = true; - for (int y = ymin; y<=ymax; ++y) { - const float - Y = y - y0 + (y0?(float)std::sqrt(delta)/a:0.0f, - bY = b*Y/a, - fxmin = x0 - 0.5f - bY - sdelta, - fxmax = x0 + 0.5f - bY + sdelta; - const int xmin = (int)fxmin, xmax = (int)fxmax; - if (!pattern) cimg_draw_scanline(xmin,xmax,y,color,opacity,1); - else { - if (first_line) { - if (y0-yb>=0) cimg_draw_scanline(xmin,xmax,y,color,opacity,1); - else draw_point(xmin,y,color,opacity).draw_point(xmax,y,color,opacity); - first_line = false; - } else { - if (xmin - CImg& draw_circle(const int x0, const int y0, int radius, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_circle(): Specified color is (null).", - cimg_instance); - cimg_init_scanline(color,opacity); - if (radius<0 || x0-radius>=width() || y0+radius<0 || y0-radius>=height()) return *this; - if (y0>=0 && y0=0) { - const int x1 = x0-x, x2 = x0+x, y1 = y0-y, y2 = y0+y; - if (y1>=0 && y1=0 && y2=0 && y1=0 && y2 - CImg& draw_circle(const int x0, const int y0, int radius, - const tc *const color, const float opacity, - const unsigned int pattern) { - cimg::unused(pattern); - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_circle(): Specified color is (null).", - cimg_instance); - if (radius<0 || x0-radius>=width() || y0+radius<0 || y0-radius>=height()) return *this; - if (!radius) return draw_point(x0,y0,color,opacity); - draw_point(x0-radius,y0,color,opacity).draw_point(x0+radius,y0,color,opacity). - draw_point(x0,y0-radius,color,opacity).draw_point(x0,y0+radius,color,opacity); - if (radius==1) return *this; - for (int f = 1-radius, ddFx = 0, ddFy = -(radius<<1), x = 0, y = radius; x=0) { f+=(ddFy+=2); --y; } - ++x; ++(f+=(ddFx+=2)); - if (x!=y+1) { - const int x1 = x0-y, x2 = x0+y, y1 = y0-x, y2 = y0+x, x3 = x0-x, x4 = x0+x, y3 = y0-y, y4 = y0+y; - draw_point(x1,y1,color,opacity).draw_point(x1,y2,color,opacity). - draw_point(x2,y1,color,opacity).draw_point(x2,y2,color,opacity); - if (x!=y) - draw_point(x3,y3,color,opacity).draw_point(x4,y4,color,opacity). - draw_point(x4,y3,color,opacity).draw_point(x3,y4,color,opacity); - } - } - return *this; - } - - //! Draw an image. - /** - \param sprite Sprite image. - \param x0 X-coordinate of the sprite position. - \param y0 Y-coordinate of the sprite position. - \param z0 Z-coordinate of the sprite position. - \param c0 C-coordinate of the sprite position. - \param opacity Drawing opacity. - **/ - template - CImg& draw_image(const int x0, const int y0, const int z0, const int c0, - const CImg& sprite, const float opacity=1) { - if (is_empty() || !sprite) return *this; - if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity); - if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared()) - return assign(sprite,false); - const bool bx = (x0<0), by = (y0<0), bz = (z0<0), bc = (c0<0); - const int - lX = sprite.width() - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0) + (bx?x0:0), - lY = sprite.height() - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0) + (by?y0:0), - lZ = sprite.depth() - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0) + (bz?z0:0), - lC = sprite.spectrum() - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0) + (bc?c0:0); - const t - *ptrs = sprite._data - - (bx?x0:0) - - (by?y0*sprite.width():0) - - (bz?z0*sprite.width()*sprite.height():0) - - (bc?c0*sprite.width()*sprite.height()*sprite.depth():0); - const unsigned long - offX = (unsigned long)_width - lX, - soffX = (unsigned long)sprite._width - lX, - offY = (unsigned long)_width*(_height - lY), - soffY = (unsigned long)sprite._width*(sprite._height - lY), - offZ = (unsigned long)_width*_height*(_depth - lZ), - soffZ = (unsigned long)sprite._width*sprite._height*(sprite._depth - lZ); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (lX>0 && lY>0 && lZ>0 && lC>0) { - T *ptrd = data(x0<0?0:x0,y0<0?0:y0,z0<0?0:z0,c0<0?0:c0); - for (int v = 0; v=1) for (int x = 0; x& draw_image(const int x0, const int y0, const int z0, const int c0, - const CImg& sprite, const float opacity=1) { - if (is_empty() || !sprite) return *this; - if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity); - if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared()) - return assign(sprite,false); - const bool bx = (x0<0), by = (y0<0), bz = (z0<0), bc = (c0<0); - const int - lX = sprite.width() - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0) + (bx?x0:0), - lY = sprite.height() - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0) + (by?y0:0), - lZ = sprite.depth() - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0) + (bz?z0:0), - lC = sprite.spectrum() - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0) + (bc?c0:0); - const T - *ptrs = sprite._data - - (bx?x0:0) - - (by?y0*sprite.width():0) - - (bz?z0*sprite.width()*sprite.height():0) - - (bc?c0*sprite.width()*sprite.height()*sprite.depth():0); - const unsigned long - offX = (unsigned long)_width - lX, - soffX = (unsigned long)sprite._width - lX, - offY = (unsigned long)_width*(_height - lY), - soffY = (unsigned long)sprite._width*(sprite._height - lY), - offZ = (unsigned long)_width*_height*(_depth - lZ), - soffZ = (unsigned long)sprite._width*sprite._height*(sprite._depth - lZ), - slX = lX*sizeof(T); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - if (lX>0 && lY>0 && lZ>0 && lC>0) { - T *ptrd = data(x0<0?0:x0,y0<0?0:y0,z0<0?0:z0,c0<0?0:c0); - for (int v = 0; v=1) - for (int y = 0; y - CImg& draw_image(const int x0, const int y0, const int z0, - const CImg& sprite, const float opacity=1) { - return draw_image(x0,y0,z0,0,sprite,opacity); - } - - //! Draw an image \overloading. - template - CImg& draw_image(const int x0, const int y0, - const CImg& sprite, const float opacity=1) { - return draw_image(x0,y0,0,sprite,opacity); - } - - //! Draw an image \overloading. - template - CImg& draw_image(const int x0, - const CImg& sprite, const float opacity=1) { - return draw_image(x0,0,sprite,opacity); - } - - //! Draw an image \overloading. - template - CImg& draw_image(const CImg& sprite, const float opacity=1) { - return draw_image(0,sprite,opacity); - } - - //! Draw a masked image. - /** - \param sprite Sprite image. - \param mask Mask image. - \param x0 X-coordinate of the sprite position in the image instance. - \param y0 Y-coordinate of the sprite position in the image instance. - \param z0 Z-coordinate of the sprite position in the image instance. - \param c0 C-coordinate of the sprite position in the image instance. - \param mask_max_value Maximum pixel value of the mask image \c mask. - \param opacity Drawing opacity. - \note - - Pixel values of \c mask set the opacity of the corresponding pixels in \c sprite. - - Dimensions along x,y and z of \p sprite and \p mask must be the same. - **/ - template - CImg& draw_image(const int x0, const int y0, const int z0, const int c0, - const CImg& sprite, const CImg& mask, const float opacity=1, - const float mask_max_value=1) { - if (is_empty() || !sprite || !mask) return *this; - if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,mask,opacity,mask_max_value); - if (is_overlapped(mask)) return draw_image(x0,y0,z0,c0,sprite,+mask,opacity,mask_max_value); - if (mask._width!=sprite._width || mask._height!=sprite._height || mask._depth!=sprite._depth) - throw CImgArgumentException(_cimg_instance - "draw_image(): Sprite (%u,%u,%u,%u,%p) and mask (%u,%u,%u,%u,%p) have " - "incompatible dimensions.", - cimg_instance, - sprite._width,sprite._height,sprite._depth,sprite._spectrum,sprite._data, - mask._width,mask._height,mask._depth,mask._spectrum,mask._data); - - const bool bx = (x0<0), by = (y0<0), bz = (z0<0), bc = (c0<0); - const int - lX = sprite.width() - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0) + (bx?x0:0), - lY = sprite.height() - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0) + (by?y0:0), - lZ = sprite.depth() - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0) + (bz?z0:0), - lC = sprite.spectrum() - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0) + (bc?c0:0); - const int - coff = -(bx?x0:0)-(by?y0*mask.width():0)-(bz?z0*mask.width()*mask.height():0)- - (bc?c0*mask.width()*mask.height()*mask.depth():0), - ssize = mask.width()*mask.height()*mask.depth()*mask.spectrum(); - const ti *ptrs = sprite._data + coff; - const tm *ptrm = mask._data + coff; - const unsigned long - offX = (unsigned long)_width - lX, - soffX = (unsigned long)sprite._width - lX, - offY = (unsigned long)_width*(_height - lY), - soffY = (unsigned long)sprite._width*(sprite._height - lY), - offZ = (unsigned long)_width*_height*(_depth - lZ), - soffZ = (unsigned long)sprite._width*sprite._height*(sprite._depth - lZ); - if (lX>0 && lY>0 && lZ>0 && lC>0) { - T *ptrd = data(x0<0?0:x0,y0<0?0:y0,z0<0?0:z0,c0<0?0:c0); - for (int c = 0; c - CImg& draw_image(const int x0, const int y0, const int z0, - const CImg& sprite, const CImg& mask, const float opacity=1, - const float mask_max_value=1) { - return draw_image(x0,y0,z0,0,sprite,mask,opacity,mask_max_value); - } - - //! Draw a image \overloading. - template - CImg& draw_image(const int x0, const int y0, - const CImg& sprite, const CImg& mask, const float opacity=1, - const float mask_max_value=1) { - return draw_image(x0,y0,0,sprite,mask,opacity,mask_max_value); - } - - //! Draw a image \overloading. - template - CImg& draw_image(const int x0, - const CImg& sprite, const CImg& mask, const float opacity=1, - const float mask_max_value=1) { - return draw_image(x0,0,sprite,mask,opacity,mask_max_value); - } - - //! Draw an image. - template - CImg& draw_image(const CImg& sprite, const CImg& mask, const float opacity=1, - const float mask_max_value=1) { - return draw_image(0,sprite,mask,opacity,mask_max_value); - } - - //! Draw a text string. - /** - \param x0 X-coordinate of the text in the image instance. - \param y0 Y-coordinate of the text in the image instance. - \param text Format of the text ('printf'-style format string). - \param foreground_color Pointer to \c spectrum() consecutive values, defining the foreground drawing color. - \param background_color Pointer to \c spectrum() consecutive values, defining the background drawing color. - \param opacity Drawing opacity. - \param font Font used for drawing text. - **/ - template - CImg& draw_text(const int x0, const int y0, - const char *const text, - const tc1 *const foreground_color, const tc2 *const background_color, - const float opacity, const CImgList& font, ...) { - if (!font) return *this; - char tmp[2048] = { 0 }; std::va_list ap; va_start(ap,font); - cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); - return _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,false); - } - - //! Draw a text string \overloading. - /** - \note A transparent background is used for the text. - **/ - template - CImg& draw_text(const int x0, const int y0, - const char *const text, - const tc *const foreground_color, const int, - const float opacity, const CImgList& font, ...) { - if (!font) return *this; - char tmp[2048] = { 0 }; std::va_list ap; va_start(ap,font); - cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); - return _draw_text(x0,y0,tmp,foreground_color,(tc*)0,opacity,font,false); - } - - //! Draw a text string \overloading. - /** - \note A transparent foreground is used for the text. - **/ - template - CImg& draw_text(const int x0, const int y0, - const char *const text, - const int, const tc *const background_color, - const float opacity, const CImgList& font, ...) { - if (!font) return *this; - char tmp[2048] = { 0 }; std::va_list ap; va_start(ap,font); - cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); - return _draw_text(x0,y0,tmp,(tc*)0,background_color,opacity,font,false); - } - - //! Draw a text string \overloading. - /** - \param x0 X-coordinate of the text in the image instance. - \param y0 Y-coordinate of the text in the image instance. - \param text Format of the text ('printf'-style format string). - \param foreground_color Array of spectrum() values of type \c T, - defining the foreground color (0 means 'transparent'). - \param background_color Array of spectrum() values of type \c T, - defining the background color (0 means 'transparent'). - \param opacity Drawing opacity. - \param font_height Height of the text font (exact match for 13,23,53,103, interpolated otherwise). - **/ - template - CImg& draw_text(const int x0, const int y0, - const char *const text, - const tc1 *const foreground_color, const tc2 *const background_color, - const float opacity=1, const unsigned int font_height=13, ...) { - if (!font_height) return *this; - char tmp[2048] = { 0 }; - std::va_list ap; va_start(ap,font_height); cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); - const CImgList& font = CImgList::font(font_height,true); - _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,true); - return *this; - } - - //! Draw a text string \overloading. - template - CImg& draw_text(const int x0, const int y0, - const char *const text, - const tc *const foreground_color, const int background_color=0, - const float opacity=1, const unsigned int font_height=13, ...) { - if (!font_height) return *this; - cimg::unused(background_color); - char tmp[2048] = { 0 }; - std::va_list ap; va_start(ap,font_height); cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); - return draw_text(x0,y0,"%s",foreground_color,(const tc*)0,opacity,font_height,tmp); - } - - //! Draw a text string \overloading. - template - CImg& draw_text(const int x0, const int y0, - const char *const text, - const int, const tc *const background_color, - const float opacity=1, const unsigned int font_height=13, ...) { - if (!font_height) return *this; - char tmp[2048] = { 0 }; - std::va_list ap; va_start(ap,font_height); cimg_vsnprintf(tmp,sizeof(tmp),text,ap); va_end(ap); - return draw_text(x0,y0,"%s",(tc*)0,background_color,opacity,font_height,tmp); - } - - template - CImg& _draw_text(const int x0, const int y0, - const char *const text, - const tc1 *const foreground_color, const tc2 *const background_color, - const float opacity, const CImgList& font, - const bool is_native_font) { - if (!text) return *this; - if (!font) - throw CImgArgumentException(_cimg_instance - "draw_text(): Empty specified font.", - cimg_instance); - - const unsigned int text_length = (unsigned int)std::strlen(text); - const bool _is_empty = is_empty(); - if (_is_empty) { - // If needed, pre-compute necessary size of the image - int x = 0, y = 0, w = 0; - unsigned char c = 0; - for (unsigned int i = 0; iw) w = x; x = 0; break; - case '\t' : x+=4*font[' ']._width; break; - default : if (cw) w=x; - y+=font[0]._height; - } - assign(x0+w,y0+y,1,is_native_font?1:font[0]._spectrum,0); - } - - int x = x0, y = y0; - for (unsigned int i = 0; i letter = font[c]; - if (letter) { - if (is_native_font && _spectrum>letter._spectrum) letter.resize(-100,-100,1,_spectrum,0,2); - const unsigned int cmin = cimg::min(_spectrum,letter._spectrum); - if (foreground_color) - for (unsigned int c = 0; c - CImg& draw_quiver(const CImg& flow, - const t2 *const color, const float opacity=1, - const unsigned int sampling=25, const float factor=-20, - const bool is_arrow=true, const unsigned int pattern=~0U) { - return draw_quiver(flow,CImg(color,_spectrum,1,1,1,true),opacity,sampling,factor,is_arrow,pattern); - } - - //! Draw a 2d vector field, using a field of colors. - /** - \param flow Image of 2d vectors used as input data. - \param color Image of spectrum()-D vectors corresponding to the color of each arrow. - \param opacity Opacity of the drawing. - \param sampling Length (in pixels) between each arrow. - \param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length). - \param is_arrow Tells if arrows must be drawn, instead of oriented segments. - \param pattern Used pattern to draw lines. - \note Clipping is supported. - **/ - template - CImg& draw_quiver(const CImg& flow, - const CImg& color, const float opacity=1, - const unsigned int sampling=25, const float factor=-20, - const bool is_arrow=true, const unsigned int pattern=~0U) { - if (is_empty()) return *this; - if (!flow || flow._spectrum!=2) - throw CImgArgumentException(_cimg_instance - "draw_quiver(): Invalid dimensions of specified flow (%u,%u,%u,%u,%p).", - cimg_instance, - flow._width,flow._height,flow._depth,flow._spectrum,flow._data); - if (sampling<=0) - throw CImgArgumentException(_cimg_instance - "draw_quiver(): Invalid sampling value %g " - "(should be >0)", - cimg_instance, - sampling); - const bool colorfield = (color._width==flow._width && color._height==flow._height && - color._depth==1 && color._spectrum==_spectrum); - if (is_overlapped(flow)) return draw_quiver(+flow,color,opacity,sampling,factor,is_arrow,pattern); - float vmax,fact; - if (factor<=0) { - float m, M = (float)flow.get_norm(2).max_min(m); - vmax = (float)cimg::max(cimg::abs(m),cimg::abs(M)); - if (!vmax) vmax = 1; - fact = -factor; - } else { fact = factor; vmax = 1; } - - for (unsigned int y = sampling/2; y<_height; y+=sampling) - for (unsigned int x = sampling/2; x<_width; x+=sampling) { - const unsigned int X = x*flow._width/_width, Y = y*flow._height/_height; - float u = (float)flow(X,Y,0,0)*fact/vmax, v = (float)flow(X,Y,0,1)*fact/vmax; - if (is_arrow) { - const int xx = x+(int)u, yy = y+(int)v; - if (colorfield) draw_arrow(x,y,xx,yy,color.get_vector_at(X,Y)._data,opacity,45,sampling/5.0f,pattern); - else draw_arrow(x,y,xx,yy,color._data,opacity,45,sampling/5.0f,pattern); - } else { - if (colorfield) - draw_line((int)(x-0.5*u),(int)(y-0.5*v),(int)(x+0.5*u),(int)(y+0.5*v), - color.get_vector_at(X,Y)._data,opacity,pattern); - else draw_line((int)(x-0.5*u),(int)(y-0.5*v),(int)(x+0.5*u),(int)(y+0.5*v), - color._data,opacity,pattern); - } - } - return *this; - } - - //! Draw a labeled horizontal axis. - /** - \param values_x Values along the horizontal axis. - \param y Y-coordinate of the horizontal axis in the image instance. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \param pattern Drawing pattern. - \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise). - \param allow_zero Enable/disable the drawing of label '0' if found. - **/ - template - CImg& draw_axis(const CImg& values_x, const int y, - const tc *const color, const float opacity=1, - const unsigned int pattern=~0U, const unsigned int font_height=13, - const bool allow_zero=true) { - if (is_empty()) return *this; - const int yt = (y+3+font_height)<_height?(y+3):(y-2-font_height); - const int siz = (int)values_x.size()-1; - char txt[32] = { 0 }; - CImg label; - if (siz<=0) { // Degenerated case. - draw_line(0,y,_width-1,y,color,opacity,pattern); - if (!siz) { - cimg_snprintf(txt,sizeof(txt),"%g",(double)*values_x); - label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height); - const int - _xt = (width() - label.width())/2, - xt = _xt<3?3:_xt+label.width()>=width()-2?width()-3-label.width():_xt; - draw_point(width()/2,y-1,color,opacity).draw_point(width()/2,y+1,color,opacity); - if (allow_zero || txt[0]!='0' || txt[1]!=0) - draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); - } - } else { // Regular case. - if (values_x[0]=width()-2?width()-3-label.width():_xt; - draw_point(xi,y-1,color,opacity).draw_point(xi,y+1,color,opacity); - if (allow_zero || txt[0]!='0' || txt[1]!=0) - draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); - } - } - return *this; - } - - //! Draw a labeled vertical axis. - /** - \param x X-coordinate of the vertical axis in the image instance. - \param values_y Values along the Y-axis. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \param pattern Drawing pattern. - \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise). - \param allow_zero Enable/disable the drawing of label '0' if found. - **/ - template - CImg& draw_axis(const int x, const CImg& values_y, - const tc *const color, const float opacity=1, - const unsigned int pattern=~0U, const unsigned int font_height=13, - const bool allow_zero=true) { - if (is_empty()) return *this; - int siz = (int)values_y.size()-1; - char txt[32] = { 0 }; - CImg label; - if (siz<=0) { // Degenerated case. - draw_line(x,0,x,_height-1,color,opacity,pattern); - if (!siz) { - cimg_snprintf(txt,sizeof(txt),"%g",(double)*values_y); - label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height); - const int - _yt = (height() - label.height())/2, - yt = _yt<0?0:_yt+label.height()>=height()?height()-1-label.height():_yt, - _xt = x - 2 - label.width(), - xt = _xt>=0?_xt:x+3; - draw_point(x-1,height()/2,color,opacity).draw_point(x+1,height()/2,color,opacity); - if (allow_zero || txt[0]!='0' || txt[1]!=0) - draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); - } - } else { // Regular case. - if (values_y[0]=height()?height()-1-label.height():_yt, - _xt = x - 2 - label.width(), - xt = _xt>=0?_xt:x+3; - draw_point(x-1,yi,color,opacity).draw_point(x+1,yi,color,opacity); - if (allow_zero || txt[0]!='0' || txt[1]!=0) - draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height); - } - } - return *this; - } - - //! Draw labeled horizontal and vertical axes. - /** - \param values_x Values along the X-axis. - \param values_y Values along the Y-axis. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \param pattern_x Drawing pattern for the X-axis. - \param pattern_y Drawing pattern for the Y-axis. - \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise). - \param allow_zero Enable/disable the drawing of label '0' if found. - **/ - template - CImg& draw_axes(const CImg& values_x, const CImg& values_y, - const tc *const color, const float opacity=1, - const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U, - const unsigned int font_height=13, const bool allow_zero=true) { - if (is_empty()) return *this; - const CImg nvalues_x(values_x._data,values_x.size(),1,1,1,true); - const int sizx = (int)values_x.size()-1, wm1 = width()-1; - if (sizx>=0) { - float ox = (float)*nvalues_x; - for (unsigned int x = sizx?1:0; x<_width; ++x) { - const float nx = (float)nvalues_x._linear_atX((float)x*sizx/wm1); - if (nx*ox<=0) { draw_axis(nx==0?x:x-1,values_y,color,opacity,pattern_y,font_height,allow_zero); break; } - ox = nx; - } - } - const CImg nvalues_y(values_y._data,values_y.size(),1,1,1,true); - const int sizy = (int)values_y.size()-1, hm1 = height()-1; - if (sizy>0) { - float oy = (float)nvalues_y[0]; - for (unsigned int y = sizy?1:0; y<_height; ++y) { - const float ny = (float)nvalues_y._linear_atX((float)y*sizy/hm1); - if (ny*oy<=0) { draw_axis(values_x,ny==0?y:y-1,color,opacity,pattern_x,font_height,allow_zero); break; } - oy = ny; - } - } - return *this; - } - - //! Draw labeled horizontal and vertical axes \overloading. - template - CImg& draw_axes(const float x0, const float x1, const float y0, const float y1, - const tc *const color, const float opacity=1, - const int subdivisionx=-60, const int subdivisiony=-60, - const float precisionx=0, const float precisiony=0, - const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U, - const unsigned int font_height=13) { - if (is_empty()) return *this; - const bool allow_zero = (x0*x1>0) || (y0*y1>0); - const float - dx = cimg::abs(x1-x0), dy = cimg::abs(y1-y0), - px = dx<=0?1:precisionx==0?(float)std::pow(10.0,(int)std::log10(dx)-2.0):precisionx, - py = dy<=0?1:precisiony==0?(float)std::pow(10.0,(int)std::log10(dy)-2.0):precisiony; - if (x0!=x1 && y0!=y1) - draw_axes(CImg::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1).round(px), - CImg::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1).round(py), - color,opacity,pattern_x,pattern_y,font_height,allow_zero); - else if (x0==x1 && y0!=y1) - draw_axis((int)x0,CImg::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1).round(py), - color,opacity,pattern_y,font_height); - else if (x0!=x1 && y0==y1) - draw_axis(CImg::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1).round(px),(int)y0, - color,opacity,pattern_x,font_height); - return *this; - } - - //! Draw 2d grid. - /** - \param values_x X-coordinates of the vertical lines. - \param values_y Y-coordinates of the horizontal lines. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - \param pattern_x Drawing pattern for vertical lines. - \param pattern_y Drawing pattern for horizontal lines. - **/ - template - CImg& draw_grid(const CImg& values_x, const CImg& values_y, - const tc *const color, const float opacity=1, - const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) { - if (is_empty()) return *this; - if (values_x) cimg_foroff(values_x,x) { - const int xi = (int)values_x[x]; - if (xi>=0 && xi=0 && yi - CImg& draw_grid(const float delta_x, const float delta_y, - const float offsetx, const float offsety, - const bool invertx, const bool inverty, - const tc *const color, const float opacity=1, - const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) { - if (is_empty()) return *this; - CImg seqx, seqy; - if (delta_x!=0) { - const float dx = delta_x>0?delta_x:_width*-delta_x/100; - const unsigned int nx = (unsigned int)(_width/dx); - seqx = CImg::sequence(1+nx,0,(unsigned int)(dx*nx)); - if (offsetx) cimg_foroff(seqx,x) seqx(x) = (unsigned int)cimg::mod(seqx(x)+offsetx,(float)_width); - if (invertx) cimg_foroff(seqx,x) seqx(x) = _width - 1 - seqx(x); - } - if (delta_y!=0) { - const float dy = delta_y>0?delta_y:_height*-delta_y/100; - const unsigned int ny = (unsigned int)(_height/dy); - seqy = CImg::sequence(1+ny,0,(unsigned int)(dy*ny)); - if (offsety) cimg_foroff(seqy,y) seqy(y) = (unsigned int)cimg::mod(seqy(y)+offsety,(float)_height); - if (inverty) cimg_foroff(seqy,y) seqy(y) = _height - 1 - seqy(y); - } - return draw_grid(seqx,seqy,color,opacity,pattern_x,pattern_y); - } - - //! Draw 1d graph. - /** - \param data Image containing the graph values I = f(x). - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - - \param plot_type Define the type of the plot: - - 0 = No plot. - - 1 = Plot using segments. - - 2 = Plot using cubic splines. - - 3 = Plot with bars. - \param vertex_type Define the type of points: - - 0 = No points. - - 1 = Point. - - 2 = Straight cross. - - 3 = Diagonal cross. - - 4 = Filled circle. - - 5 = Outlined circle. - - 6 = Square. - - 7 = Diamond. - \param ymin Lower bound of the y-range. - \param ymax Upper bound of the y-range. - \param pattern Drawing pattern. - \note - - if \c ymin==ymax==0, the y-range is computed automatically from the input samples. - **/ - template - CImg& draw_graph(const CImg& data, - const tc *const color, const float opacity=1, - const unsigned int plot_type=1, const int vertex_type=1, - const double ymin=0, const double ymax=0, const unsigned int pattern=~0U) { - if (is_empty() || _height<=1) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_graph(): Specified color is (null).", - cimg_instance); - - // Create shaded colors for displaying bar plots. - CImg color1, color2; - if (plot_type==3) { - color1.assign(_spectrum); color2.assign(_spectrum); - cimg_forC(*this,c) { - color1[c] = (tc)cimg::min((float)cimg::type::max(),color[c]*1.2f); - color2[c] = (tc)(color[c]*0.4f); - } - } - - // Compute min/max and normalization factors. - const unsigned long - siz = data.size(), - _siz1 = siz - (plot_type!=3?1:0), - siz1 = _siz1?_siz1:1; - const unsigned int - _width1 = _width - (plot_type!=3?1:0), - width1 = _width1?_width1:1; - double m = ymin, M = ymax; - if (ymin==ymax) m = (double)data.max_min(M); - if (m==M) { --m; ++M; } - const float ca = (float)(M-m)/(_height-1); - bool init_hatch = true; - - // Draw graph edges - switch (plot_type%4) { - case 1 : { // Segments - int oX = 0, oY = (int)((data[0]-m)/ca); - const float fx = (float)_width1/siz1; - if (siz==1) { - const int Y = (int)((*data-m)/ca); - draw_line(0,Y,width()-1,Y,color,opacity,pattern); - } else for (unsigned long off = 1; off ndata(data._data,siz,1,1,1,true); - int oY = (int)((data[0]-m)/ca); - cimg_forX(*this,x) { - const int Y = (int)((ndata._cubic_atX((float)x*siz1/width1)-m)/ca); - if (x>0) draw_line(x,oY,x+1,Y,color,opacity,pattern,init_hatch); - init_hatch = false; - oY = Y; - } - } break; - case 3 : { // Bars - const int Y0 = (int)(-m/ca); - const float fx = (float)_width/(siz-1); - int oX = 0; - cimg_foroff(data,off) { - const int - X = (int)((off+1)*fx), - Y = (int)((data[off]-m)/ca); - draw_rectangle(oX,Y0,X,Y,color,opacity). - draw_line(oX,Y,oX,Y0,color2.data(),opacity). - draw_line(oX,Y0,X,Y0,Y<=Y0?color2.data():color1.data(),opacity). - draw_line(X,Y,X,Y0,color1.data(),opacity). - draw_line(oX,Y,X,Y,Y<=Y0?color1.data():color2.data(),opacity); - oX = X+1; - } - } break; - default : break; // No edges - } - - // Draw graph points - const unsigned int wb2 = plot_type==3?_width1/(2*siz):0; - const float fx = (float)_width1/siz1; - switch (vertex_type%8) { - case 1 : { // Point - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_point(X,Y,color,opacity); - } - } break; - case 2 : { // Straight Cross - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_line(X-3,Y,X+3,Y,color,opacity).draw_line(X,Y-3,X,Y+3,color,opacity); - } - } break; - case 3 : { // Diagonal Cross - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_line(X-3,Y-3,X+3,Y+3,color,opacity).draw_line(X-3,Y+3,X+3,Y-3,color,opacity); - } - } break; - case 4 : { // Filled Circle - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_circle(X,Y,3,color,opacity); - } - } break; - case 5 : { // Outlined circle - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_circle(X,Y,3,color,opacity,0U); - } - } break; - case 6 : { // Square - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_rectangle(X-3,Y-3,X+3,Y+3,color,opacity,~0U); - } - } break; - case 7 : { // Diamond - cimg_foroff(data,off) { - const int - X = (int)(off*fx) + wb2, - Y = (int)((data[off]-m)/ca); - draw_line(X,Y-4,X+4,Y,color,opacity). - draw_line(X+4,Y,X,Y+4,color,opacity). - draw_line(X,Y+4,X-4,Y,color,opacity). - draw_line(X-4,Y,X,Y-4,color,opacity); - } - } break; - default : break; // No points - } - return *this; - } - - //! Draw filled 3d region with the flood fill algorithm. - /** - \param x X-coordinate of the starting point of the region to fill. - \param y Y-coordinate of the starting point of the region to fill. - \param z Z-coordinate of the starting point of the region to fill. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param[out] region Image that will contain the mask of the filled region mask, as an output. - \param sigma Tolerance concerning neighborhood values. - \param opacity Opacity of the drawing. - \param is_high_connexity Tells if 8-connexity must be used (only for 2d images). - \return \c region is initialized with the binary mask of the filled region. - **/ - template - CImg& draw_fill(const int x, const int y, const int z, - const tc *const color, const float opacity, - CImg& region, const float sigma=0, - const bool is_high_connexity=false) { - -#define _cimg_draw_fill_test(x,y,z,res) if (region(x,y,z)) res = false; else { \ - res = true; \ - const T *reference_col = reference_color._data + _spectrum, *ptrs = data(x,y,z) + siz; \ - for (unsigned int i = _spectrum; res && i; --i) { ptrs-=whd; res = (cimg::abs(*ptrs - *(--reference_col))<=sigma); } \ - region(x,y,z) = (t)(res?1:noregion); \ -} - -#define _cimg_draw_fill_set(x,y,z) { \ - const tc *col = color; \ - T *ptrd = data(x,y,z); \ - if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; } \ - else cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; } \ -} - -#define _cimg_draw_fill_insert(x,y,z) { \ - if (posr1>=remaining._height) remaining.resize(3,remaining._height<<1,1,1,0); \ - unsigned int *ptrr = remaining.data(0,posr1); \ - *(ptrr++) = x; *(ptrr++) = y; *(ptrr++) = z; ++posr1; \ -} - -#define _cimg_draw_fill_test_neighbor(x,y,z,cond) if (cond) { \ - const unsigned int tx = x, ty = y, tz = z; \ - _cimg_draw_fill_test(tx,ty,tz,res); if (res) _cimg_draw_fill_insert(tx,ty,tz); \ -} - - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_fill(): Specified color is (null).", - cimg_instance); - - region.assign(_width,_height,_depth,1,(t)0); - if (x>=0 && x=0 && y=0 && z1); - const CImg reference_color = get_vector_at(x,y,z); - CImg remaining(3,512,1,1,0); - remaining(0,0) = x; remaining(1,0) = y; remaining(2,0) = z; - unsigned int posr0 = 0, posr1 = 1; - region(x,y,z) = (t)1; - const t noregion = ((t)1==(t)2)?(t)0:(t)(-1); - if (is_3d) do { // 3d version of the filling algorithm - const unsigned int *pcurr = remaining.data(0,posr0++), xc = *(pcurr++), yc = *(pcurr++), zc = *(pcurr++); - if (posr0>=512) { remaining.shift(0,-(int)posr0); posr1-=posr0; posr0 = 0; } - bool cont, res; - unsigned int nxc = xc; - do { // X-backward - _cimg_draw_fill_set(nxc,yc,zc); - _cimg_draw_fill_test_neighbor(nxc,yc-1,zc,yc!=0); - _cimg_draw_fill_test_neighbor(nxc,yc+1,zc,ycposr0); - else do { // 2d version of the filling algorithm - const unsigned int *pcurr = remaining.data(0,posr0++), xc = *(pcurr++), yc = *(pcurr++); - if (posr0>=512) { remaining.shift(0,-(int)posr0); posr1-=posr0; posr0 = 0; } - bool cont, res; - unsigned int nxc = xc; - do { // X-backward - _cimg_draw_fill_set(nxc,yc,0); - _cimg_draw_fill_test_neighbor(nxc,yc-1,0,yc!=0); - _cimg_draw_fill_test_neighbor(nxc,yc+1,0,ycposr0); - if (noregion) cimg_for(region,ptrd,t) if (*ptrd==noregion) *ptrd = (t)0; - } - return *this; - } - - //! Draw filled 3d region with the flood fill algorithm \simplification. - template - CImg& draw_fill(const int x, const int y, const int z, - const tc *const color, const float opacity=1, - const float sigma=0, const bool is_high_connexity=false) { - CImg tmp; - return draw_fill(x,y,z,color,opacity,tmp,sigma,is_high_connexity); - } - - //! Draw filled 2d region with the flood fill algorithm \simplification. - template - CImg& draw_fill(const int x, const int y, - const tc *const color, const float opacity=1, - const float sigma=0, const bool is_high_connexity=false) { - CImg tmp; - return draw_fill(x,y,0,color,opacity,tmp,sigma,is_high_connexity); - } - - //! Draw a random plasma texture. - /** - \param alpha Alpha-parameter. - \param beta Beta-parameter. - \param scale Scale-parameter. - \note Use the mid-point algorithm to render. - **/ - CImg& draw_plasma(const float alpha=1, const float beta=0, const unsigned int scale=8) { - if (is_empty()) return *this; - const int w = width(), h = height(); - const Tfloat m = (Tfloat)cimg::type::min(), M = (Tfloat)cimg::type::max(); - cimg_forZC(*this,z,c) { - CImg ref = get_shared_slice(z,c); - for (int delta = 1<1; delta>>=1) { - const int delta2 = delta>>1; - const float r = alpha*delta + beta; - - // Square step. - for (int y0 = 0; y0M?M:val); - } - - // Diamond steps. - for (int y = -delta2; yM?M:val); - } - for (int y0 = 0; y0M?M:val); - } - for (int y = -delta2; yM?M:val); - } - } - } - return *this; - } - - //! Draw a quadratic Mandelbrot or Julia 2d fractal. - /** - \param x0 X-coordinate of the upper-left pixel. - \param y0 Y-coordinate of the upper-left pixel. - \param x1 X-coordinate of the lower-right pixel. - \param y1 Y-coordinate of the lower-right pixel. - \param colormap Colormap. - \param opacity Drawing opacity. - \param z0r Real part of the upper-left fractal vertex. - \param z0i Imaginary part of the upper-left fractal vertex. - \param z1r Real part of the lower-right fractal vertex. - \param z1i Imaginary part of the lower-right fractal vertex. - \param iteration_max Maximum number of iterations for each estimated point. - \param is_normalized_iteration Tells if iterations are normalized. - \param is_julia_set Tells if the Mandelbrot or Julia set is rendered. - \param param_r Real part of the Julia set parameter. - \param param_i Imaginary part of the Julia set parameter. - \note Fractal rendering is done by the Escape Time Algorithm. - **/ - template - CImg& draw_mandelbrot(const int x0, const int y0, const int x1, const int y1, - const CImg& colormap, const float opacity=1, - const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2, - const unsigned int iteration_max=255, - const bool is_normalized_iteration=false, - const bool is_julia_set=false, - const double param_r=0, const double param_i=0) { - if (is_empty()) return *this; - CImg palette; - if (colormap) palette.assign(colormap._data,colormap.size()/colormap._spectrum,1,1,colormap._spectrum,true); - if (palette && palette._spectrum!=_spectrum) - throw CImgArgumentException(_cimg_instance - "draw_mandelbrot(): Instance and specified colormap (%u,%u,%u,%u,%p) have " - "incompatible dimensions.", - cimg_instance, - colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data); - - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0), ln2 = (float)std::log(2.0); - const int - _x0 = x0<0?0:x0>=width()?width()-1:x0, - _y0 = y0<0?0:y0>=height()?height()-1:y0, - _x1 = x1<0?1:x1>=width()?width()-1:x1, - _y1 = y1<0?1:y1>=height()?height()-1:y1; -#ifdef cimg_use_openmp -#pragma omp parallel for collapse(2) if ((1+_x1-_x0)*(1+_y1-_y0)>=2048) -#endif - for (int q = _y0; q<=_y1; ++q) - for (int p = _x0; p<=_x1; ++p) { - unsigned int iteration = 0; - const double x = z0r + p*(z1r-z0r)/_width, y = z0i + q*(z1i-z0i)/_height; - double zr, zi, cr, ci; - if (is_julia_set) { zr = x; zi = y; cr = param_r; ci = param_i; } - else { zr = param_r; zi = param_i; cr = x; ci = y; } - for (iteration=1; zr*zr + zi*zi<=4 && iteration<=iteration_max; ++iteration) { - const double temp = zr*zr - zi*zi + cr; - zi = 2*zr*zi + ci; - zr = temp; - } - if (iteration>iteration_max) { - if (palette) { - if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette(0,c); - else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(0,c)*nopacity + (*this)(p,q,0,c)*copacity); - } else { - if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)0; - else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)((*this)(p,q,0,c)*copacity); - } - } else if (is_normalized_iteration) { - const float - normz = (float)cimg::abs(zr*zr+zi*zi), - niteration = (float)(iteration + 1 - std::log(std::log(normz))/ln2); - if (palette) { - if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._linear_atX(niteration,c); - else cimg_forC(*this,c) - (*this)(p,q,0,c) = (T)(palette._linear_atX(niteration,c)*nopacity + (*this)(p,q,0,c)*copacity); - } else { - if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)niteration; - else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(niteration*nopacity + (*this)(p,q,0,c)*copacity); - } - } else { - if (palette) { - if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._atX(iteration,c); - else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(iteration,c)*nopacity + (*this)(p,q,0,c)*copacity); - } else { - if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)iteration; - else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(iteration*nopacity + (*this)(p,q,0,c)*copacity); - } - } - } - return *this; - } - - //! Draw a quadratic Mandelbrot or Julia 2d fractal \overloading. - template - CImg& draw_mandelbrot(const CImg& colormap, const float opacity=1, - const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2, - const unsigned int iteration_max=255, - const bool is_normalized_iteration=false, - const bool is_julia_set=false, - const double param_r=0, const double param_i=0) { - return draw_mandelbrot(0,0,_width-1,_height-1,colormap,opacity, - z0r,z0i,z1r,z1i,iteration_max,is_normalized_iteration,is_julia_set,param_r,param_i); - } - - //! Draw a 1d gaussian function. - /** - \param xc X-coordinate of the gaussian center. - \param sigma Standard variation of the gaussian distribution. - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - **/ - template - CImg& draw_gaussian(const float xc, const float sigma, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_gaussian(): Specified color is (null).", - cimg_instance); - const float sigma2 = 2*sigma*sigma, nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const unsigned long whd = (unsigned long)_width*_height*_depth; - const tc *col = color; - cimg_forX(*this,x) { - const float dx = (x - xc), val = (float)std::exp(-dx*dx/sigma2); - T *ptrd = data(x,0,0,0); - if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; } - else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; } - col-=_spectrum; - } - return *this; - } - - //! Draw a 2d gaussian function. - /** - \param xc X-coordinate of the gaussian center. - \param yc Y-coordinate of the gaussian center. - \param tensor Covariance matrix (must be 2x2). - \param color Pointer to \c spectrum() consecutive values, defining the drawing color. - \param opacity Drawing opacity. - **/ - template - CImg& draw_gaussian(const float xc, const float yc, const CImg& tensor, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - if (tensor._width!=2 || tensor._height!=2 || tensor._depth!=1 || tensor._spectrum!=1) - throw CImgArgumentException(_cimg_instance - "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 2x2 matrix.", - cimg_instance, - tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data); - if (!color) - throw CImgArgumentException(_cimg_instance - "draw_gaussian(): Specified color is (null).", - cimg_instance); - typedef typename CImg::Tfloat tfloat; - const CImg invT = tensor.get_invert(), invT2 = (invT*invT)/(-2.0); - const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = invT2(1,1); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const unsigned long whd = (unsigned long)_width*_height*_depth; - const tc *col = color; - float dy = -yc; - cimg_forY(*this,y) { - float dx = -xc; - cimg_forX(*this,x) { - const float val = (float)std::exp(a*dx*dx + b*dx*dy + c*dy*dy); - T *ptrd = data(x,y,0,0); - if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; } - else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; } - col-=_spectrum; - ++dx; - } - ++dy; - } - return *this; - } - - //! Draw a 2d gaussian function \overloading. - template - CImg& draw_gaussian(const int xc, const int yc, const float r1, const float r2, const float ru, const float rv, - const tc *const color, const float opacity=1) { - const double - a = r1*ru*ru + r2*rv*rv, - b = (r1-r2)*ru*rv, - c = r1*rv*rv + r2*ru*ru; - const CImg tensor(2,2,1,1, a,b,b,c); - return draw_gaussian(xc,yc,tensor,color,opacity); - } - - //! Draw a 2d gaussian function \overloading. - template - CImg& draw_gaussian(const float xc, const float yc, const float sigma, - const tc *const color, const float opacity=1) { - return draw_gaussian(xc,yc,CImg::diagonal(sigma,sigma),color,opacity); - } - - //! Draw a 3d gaussian function \overloading. - template - CImg& draw_gaussian(const float xc, const float yc, const float zc, const CImg& tensor, - const tc *const color, const float opacity=1) { - if (is_empty()) return *this; - typedef typename CImg::Tfloat tfloat; - if (tensor._width!=3 || tensor._height!=3 || tensor._depth!=1 || tensor._spectrum!=1) - throw CImgArgumentException(_cimg_instance - "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 3x3 matrix.", - cimg_instance, - tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data); - - const CImg invT = tensor.get_invert(), invT2 = (invT*invT)/(-2.0); - const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = 2*invT2(2,0), d = invT2(1,1), e = 2*invT2(2,1), f = invT2(2,2); - const float nopacity = cimg::abs(opacity), copacity = 1 - cimg::max(opacity,0); - const unsigned long whd = (unsigned long)_width*_height*_depth; - const tc *col = color; - cimg_forXYZ(*this,x,y,z) { - const float - dx = (x - xc), dy = (y - yc), dz = (z - zc), - val = (float)std::exp(a*dx*dx + b*dx*dy + c*dx*dz + d*dy*dy + e*dy*dz + f*dz*dz); - T *ptrd = data(x,y,z,0); - if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; } - else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; } - col-=_spectrum; - } - return *this; - } - - //! Draw a 3d gaussian function \overloading. - template - CImg& draw_gaussian(const float xc, const float yc, const float zc, const float sigma, - const tc *const color, const float opacity=1) { - return draw_gaussian(xc,yc,zc,CImg::diagonal(sigma,sigma,sigma),color,opacity); - } - - //! Draw a 3d object. - /** - \param x0 X-coordinate of the 3d object position - \param y0 Y-coordinate of the 3d object position - \param z0 Z-coordinate of the 3d object position - \param vertices Image Nx3 describing 3d point coordinates - \param primitives List of P primitives - \param colors List of P color (or textures) - \param opacities Image or list of P opacities - \param render_type d Render type (0=Points, 1=Lines, 2=Faces (no light), 3=Faces (flat), 4=Faces(Gouraud) - \param is_double_sided Tells if object faces have two sides or are oriented. - \param focale length of the focale (0 for parallel projection) - \param lightx X-coordinate of the light - \param lighty Y-coordinate of the light - \param lightz Z-coordinate of the light - \param specular_lightness Amount of specular light. - \param specular_shininess Shininess of the object - **/ - template - CImg& draw_object3d(const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImg& opacities, - const unsigned int render_type=4, - const bool is_double_sided=false, const float focale=700, - const float lightx=0, const float lighty=0, const float lightz=-5e8, - const float specular_lightness=0.2f, const float specular_shininess=0.1f) { - return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type, - is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,CImg::empty()); - } - - //! Draw a 3d object \simplification. - template - CImg& draw_object3d(const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImg& opacities, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - CImg& zbuffer) { - return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,1); - } - -#ifdef cimg_use_board - template - CImg& draw_object3d(LibBoard::Board& board, - const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImg& opacities, - const unsigned int render_type=4, - const bool is_double_sided=false, const float focale=700, - const float lightx=0, const float lighty=0, const float lightz=-5e8, - const float specular_lightness=0.2f, const float specular_shininess=0.1f) { - return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type, - is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,CImg::empty()); - } - - template - CImg& draw_object3d(LibBoard::Board& board, - const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImg& opacities, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - CImg& zbuffer) { - return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,1); - } -#endif - - //! Draw a 3d object \simplification. - template - CImg& draw_object3d(const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImgList& opacities, - const unsigned int render_type=4, - const bool is_double_sided=false, const float focale=700, - const float lightx=0, const float lighty=0, const float lightz=-5e8, - const float specular_lightness=0.2f, const float specular_shininess=0.1f) { - return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type, - is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,CImg::empty()); - } - - //! Draw a 3d object \simplification. - template - CImg& draw_object3d(const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImgList& opacities, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - CImg& zbuffer) { - return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,1); - } - -#ifdef cimg_use_board - template - CImg& draw_object3d(LibBoard::Board& board, - const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImgList& opacities, - const unsigned int render_type=4, - const bool is_double_sided=false, const float focale=700, - const float lightx=0, const float lighty=0, const float lightz=-5e8, - const float specular_lightness=0.2f, const float specular_shininess=0.1f) { - return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type, - is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,CImg::empty()); - } - - template - CImg& draw_object3d(LibBoard::Board& board, - const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, const CImgList& opacities, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - CImg& zbuffer) { - return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities, - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,1); - } -#endif - - //! Draw a 3d object \simplification. - template - CImg& draw_object3d(const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, - const unsigned int render_type=4, - const bool is_double_sided=false, const float focale=700, - const float lightx=0, const float lighty=0, const float lightz=-5e8, - const float specular_lightness=0.2f, const float specular_shininess=0.1f) { - return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,CImg::empty()); - } - - //! Draw a 3d object \simplification. - template - CImg& draw_object3d(const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - CImg& zbuffer) { - return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,zbuffer); - } - -#ifdef cimg_use_board - template - CImg& draw_object3d(LibBoard::Board& board, - const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, - const unsigned int render_type=4, - const bool is_double_sided=false, const float focale=700, - const float lightx=0, const float lighty=0, const float lightz=-5e8, - const float specular_lightness=0.2f, const float specular_shininess=0.1f) { - return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,CImg::empty()); - } - - template - CImg& draw_object3d(LibBoard::Board& board, - const float x0, const float y0, const float z0, - const CImg& vertices, const CImgList& primitives, - const CImgList& colors, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - CImg& zbuffer) { - return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg::empty(), - render_type,is_double_sided,focale,lightx,lighty,lightz, - specular_lightness,specular_shininess,zbuffer); - } -#endif - - template - static float __draw_object3d(const CImgList& opacities, const unsigned int n_primitive, CImg& opacity) { - if (n_primitive>=opacities._width || opacities[n_primitive].is_empty()) { opacity.assign(); return 1; } - if (opacities[n_primitive].size()==1) { opacity.assign(); return opacities(n_primitive,0); } - opacity.assign(opacities[n_primitive],true); - return 1.0f; - } - - template - static float __draw_object3d(const CImg& opacities, const unsigned int n_primitive, CImg& opacity) { - opacity.assign(); - return n_primitive>=opacities._width?1.0f:(float)opacities[n_primitive]; - } - - template - CImg& _draw_object3d(void *const pboard, CImg& zbuffer, - const float X, const float Y, const float Z, - const CImg& vertices, - const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const unsigned int render_type, - const bool is_double_sided, const float focale, - const float lightx, const float lighty, const float lightz, - const float specular_lightness, const float specular_shininess, - const float sprite_scale) { - typedef typename cimg::superset2::type tpfloat; - if (is_empty() || !vertices || !primitives) return *this; - char error_message[1024] = { 0 }; - if (!vertices.is_object3d(primitives,colors,opacities,false,error_message)) - throw CImgArgumentException(_cimg_instance - "draw_object3d(): Invalid specified 3d object (%u,%u) (%s).", - cimg_instance,vertices._width,primitives._width,error_message); - if (render_type==5) cimg::mutex(10); // Static variable used in this case, breaks thread-safety. - -#ifndef cimg_use_board - if (pboard) return *this; -#endif - const float - nspec = 1 - (specular_lightness<0.0f?0.0f:(specular_lightness>1.0f?1.0f:specular_lightness)), - nspec2 = 1 + (specular_shininess<0.0f?0.0f:specular_shininess), - nsl1 = (nspec2 - 1)/cimg::sqr(nspec - 1), - nsl2 = 1 - 2*nsl1*nspec, - nsl3 = nspec2 - nsl1 - nsl2; - - // Create light texture for phong-like rendering. - CImg light_texture; - if (render_type==5) { - if (colors._width>primitives._width) { - static CImg default_light_texture; - static const tc *lptr = 0; - static tc ref_values[64] = { 0 }; - const CImg& img = colors.back(); - bool is_same_texture = (lptr==img._data); - if (is_same_texture) - for (unsigned int r = 0, j = 0; j<8; ++j) - for (unsigned int i = 0; i<8; ++i) - if (ref_values[r++]!=img(i*img._width/9,j*img._height/9,0,(i+j)%img._spectrum)) { - is_same_texture = false; break; - } - if (!is_same_texture || default_light_texture._spectrum<_spectrum) { - (default_light_texture.assign(img,false)/=255).resize(-100,-100,1,_spectrum); - lptr = colors.back().data(); - for (unsigned int r = 0, j = 0; j<8; ++j) - for (unsigned int i = 0; i<8; ++i) - ref_values[r++] = img(i*img._width/9,j*img._height/9,0,(i+j)%img._spectrum); - } - light_texture.assign(default_light_texture,true); - } else { - static CImg default_light_texture; - static float olightx = 0, olighty = 0, olightz = 0, ospecular_shininess = 0; - if (!default_light_texture || - lightx!=olightx || lighty!=olighty || lightz!=olightz || - specular_shininess!=ospecular_shininess || default_light_texture._spectrum<_spectrum) { - default_light_texture.assign(512,512); - const float - dlx = lightx - X, - dly = lighty - Y, - dlz = lightz - Z, - nl = (float)std::sqrt(dlx*dlx + dly*dly + dlz*dlz), - nlx = (default_light_texture._width - 1)/2*(1 + dlx/nl), - nly = (default_light_texture._height - 1)/2*(1 + dly/nl), - white[] = { 1 }; - default_light_texture.draw_gaussian(nlx,nly,default_light_texture._width/3.0f,white); - cimg_forXY(default_light_texture,x,y) { - const float factor = default_light_texture(x,y); - if (factor>nspec) default_light_texture(x,y) = cimg::min(2,nsl1*factor*factor + nsl2*factor + nsl3); - } - default_light_texture.resize(-100,-100,1,_spectrum); - olightx = lightx; olighty = lighty; olightz = lightz; ospecular_shininess = specular_shininess; - } - light_texture.assign(default_light_texture,true); - } - } - - // Compute 3d to 2d projection. - CImg projections(vertices._width,2); - tpfloat parallzmin = cimg::type::max(); - const float absfocale = focale?cimg::abs(focale):0; - if (absfocale) { -#ifdef cimg_use_openmp -#pragma omp parallel for if (projections.size()>4096) -#endif - cimg_forX(projections,l) { // Perspective projection - const tpfloat - x = (tpfloat)vertices(l,0), - y = (tpfloat)vertices(l,1), - z = (tpfloat)vertices(l,2); - const tpfloat projectedz = z + Z + absfocale; - projections(l,1) = Y + absfocale*y/projectedz; - projections(l,0) = X + absfocale*x/projectedz; - } - - } else { -#ifdef cimg_use_openmp -#pragma omp parallel for if (projections.size()>4096) -#endif - cimg_forX(projections,l) { // Parallel projection - const tpfloat - x = (tpfloat)vertices(l,0), - y = (tpfloat)vertices(l,1), - z = (tpfloat)vertices(l,2); - if (z visibles(primitives._width,1,1,1,~0U); - CImg zrange(primitives._width); - const tpfloat zmin = absfocale?(tpfloat)(1.5f - absfocale):cimg::type::min(); - -#ifdef cimg_use_openmp -#pragma omp parallel for if (primitives.size()>4096) -#endif - cimglist_for(primitives,l) { - const CImg& primitive = primitives[l]; - switch (primitive.size()) { - case 1 : { // Point - const unsigned int i0 = (unsigned int)primitive(0); - const tpfloat z0 = Z + vertices(i0,2); - if (z0>zmin) { - visibles(l) = (unsigned int)l; - zrange(l) = z0; - } - } break; - case 5 : { // Sphere - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1); - const tpfloat - Xc = 0.5f*((float)vertices(i0,0) + (float)vertices(i1,0)), - Yc = 0.5f*((float)vertices(i0,1) + (float)vertices(i1,1)), - Zc = 0.5f*((float)vertices(i0,2) + (float)vertices(i1,2)), - _zc = Z + Zc, - zc = _zc + _focale, - xc = X + Xc*(absfocale?absfocale/zc:1), - yc = Y + Yc*(absfocale?absfocale/zc:1), - radius = 0.5f*std::sqrt(cimg::sqr(vertices(i1,0) - vertices(i0,0)) + - cimg::sqr(vertices(i1,1) - vertices(i0,1)) + - cimg::sqr(vertices(i1,2) - vertices(i0,2)))*(absfocale?absfocale/zc:1), - xm = xc - radius, - ym = yc - radius, - xM = xc + radius, - yM = yc + radius; - if (xM>=0 && xm<_width && yM>=0 && ym<_height && _zc>zmin) { - visibles(l) = (unsigned int)l; - zrange(l) = _zc; - } - } break; - case 2 : // Segment - case 6 : { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1); - const tpfloat - x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2), - x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2); - tpfloat xm, xM, ym, yM; - if (x0=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin) { - visibles(l) = (unsigned int)l; - zrange(l) = (z0 + z1)/2; - } - } break; - case 3 : // Triangle - case 9 : { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1), - i2 = (unsigned int)primitive(2); - const tpfloat - x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2), - x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2), - x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2); - tpfloat xm, xM, ym, yM; - if (x0xM) xM = x2; - if (y0yM) yM = y2; - if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin) { - const tpfloat d = (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0); - if (is_double_sided || d<0) { - visibles(l) = (unsigned int)l; - zrange(l) = (z0 + z1 + z2)/3; - } - } - } break; - case 4 : // Rectangle - case 12 : { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1), - i2 = (unsigned int)primitive(2), - i3 = (unsigned int)primitive(3); - const tpfloat - x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2), - x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2), - x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2), - x3 = projections(i3,0), y3 = projections(i3,1), z3 = Z + vertices(i3,2); - tpfloat xm, xM, ym, yM; - if (x0xM) xM = x2; - if (x3xM) xM = x3; - if (y0yM) yM = y2; - if (y3yM) yM = y3; - if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin) { - const float d = (x1 - x0)*(y2 - y0) - (x2 - x0)*(y1 - y0); - if (is_double_sided || d<0) { - visibles(l) = (unsigned int)l; - zrange(l) = (z0 + z1 + z2 + z3)/4; - } - } - } break; - default : - throw CImgArgumentException(_cimg_instance - "draw_object3d(): Invalid primitive[%u] with size %u " - "(should have size 1,2,3,4,5,6,9 or 12).", - cimg_instance, - l,primitive.size()); - } - } - - // Sort only visibles primitives. - unsigned int *p_visibles = visibles._data; - float *p_zrange = zrange._data; - const float *ptrz = p_zrange; - cimg_for(visibles,ptr,unsigned int) { - if (*ptr!=~0U) { *(p_visibles++) = *ptr; *(p_zrange++) = *ptrz; } - ++ptrz; - } - const unsigned int nb_visibles = p_zrange - zrange._data; - if (!nb_visibles) return *this; - CImg permutations; - CImg(zrange._data,nb_visibles,1,1,1,true).sort(permutations,false); - - // Compute light properties - CImg lightprops; - switch (render_type) { - case 3 : { // Flat Shading - lightprops.assign(nb_visibles); -#ifdef cimg_use_openmp -#pragma omp parallel for if (nb_visibles>4096) -#endif - cimg_forX(lightprops,l) { - const CImg& primitive = primitives(visibles(permutations(l))); - const unsigned int psize = primitive.size(); - if (psize==3 || psize==4 || psize==9 || psize==12) { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1), - i2 = (unsigned int)primitive(2); - const tpfloat - x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2), - x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2), - x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2), - dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0, - dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0, - nx = dy1*dz2 - dz1*dy2, - ny = dz1*dx2 - dx1*dz2, - nz = dx1*dy2 - dy1*dx2, - norm = (tpfloat)std::sqrt(1e-5f + nx*nx + ny*ny + nz*nz), - lx = X + (x0 + x1 + x2)/3 - lightx, - ly = Y + (y0 + y1 + y2)/3 - lighty, - lz = Z + (z0 + z1 + z2)/3 - lightz, - nl = (tpfloat)std::sqrt(1e-5f + lx*lx + ly*ly + lz*lz), - factor = cimg::max(cimg::abs(-lx*nx-ly*ny-lz*nz)/(norm*nl),0); - lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3); - } else lightprops[l] = 1; - } - } break; - - case 4 : // Gouraud Shading - case 5 : { // Phong-Shading - CImg vertices_normals(vertices._width,3,1,1,0); -#ifdef cimg_use_openmp -#pragma omp parallel for if (nb_visibles>4096) -#endif - for (unsigned int l = 0; l& primitive = primitives[visibles(l)]; - const unsigned int psize = primitive.size(); - const bool - triangle_flag = (psize==3) || (psize==9), - rectangle_flag = (psize==4) || (psize==12); - if (triangle_flag || rectangle_flag) { - const unsigned int - i0 = (unsigned int)primitive(0), - i1 = (unsigned int)primitive(1), - i2 = (unsigned int)primitive(2), - i3 = rectangle_flag?(unsigned int)primitive(3):0; - const tpfloat - x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2), - x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2), - x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2), - dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0, - dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0, - nnx = dy1*dz2 - dz1*dy2, - nny = dz1*dx2 - dx1*dz2, - nnz = dx1*dy2 - dy1*dx2, - norm = (tpfloat)(1e-5f + std::sqrt(nnx*nnx + nny*nny + nnz*nnz)), - nx = nnx/norm, - ny = nny/norm, - nz = nnz/norm; - vertices_normals(i0,0)+=nx; vertices_normals(i0,1)+=ny; vertices_normals(i0,2)+=nz; - vertices_normals(i1,0)+=nx; vertices_normals(i1,1)+=ny; vertices_normals(i1,2)+=nz; - vertices_normals(i2,0)+=nx; vertices_normals(i2,1)+=ny; vertices_normals(i2,2)+=nz; - if (rectangle_flag) { vertices_normals(i3,0)+=nx; vertices_normals(i3,1)+=ny; vertices_normals(i3,2)+=nz; } - } - } - - if (is_double_sided) cimg_forX(vertices_normals,p) if (vertices_normals(p,2)>0) { - vertices_normals(p,0) = -vertices_normals(p,0); - vertices_normals(p,1) = -vertices_normals(p,1); - vertices_normals(p,2) = -vertices_normals(p,2); - } - - if (render_type==4) { - lightprops.assign(vertices._width); -#ifdef cimg_use_openmp -#pragma omp parallel for if (nb_visibles>4096) -#endif - cimg_forX(lightprops,l) { - const tpfloat - nx = vertices_normals(l,0), - ny = vertices_normals(l,1), - nz = vertices_normals(l,2), - norm = (tpfloat)std::sqrt(1e-5f + nx*nx + ny*ny + nz*nz), - lx = X + vertices(l,0) - lightx, - ly = Y + vertices(l,1) - lighty, - lz = Z + vertices(l,2) - lightz, - nl = (tpfloat)std::sqrt(1e-5f + lx*lx + ly*ly + lz*lz), - factor = cimg::max((-lx*nx-ly*ny-lz*nz)/(norm*nl),0); - lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3); - } - } else { - const unsigned int - lw2 = light_texture._width/2 - 1, - lh2 = light_texture._height/2 - 1; - lightprops.assign(vertices._width,2); -#ifdef cimg_use_openmp -#pragma omp parallel for if (nb_visibles>4096) -#endif - cimg_forX(lightprops,l) { - const tpfloat - nx = vertices_normals(l,0), - ny = vertices_normals(l,1), - nz = vertices_normals(l,2), - norm = (tpfloat)std::sqrt(1e-5f + nx*nx + ny*ny + nz*nz), - nnx = nx/norm, - nny = ny/norm; - lightprops(l,0) = lw2*(1 + nnx); - lightprops(l,1) = lh2*(1 + nny); - } - } - } break; - } - - // Draw visible primitives - const CImg default_color(1,_spectrum,1,1,(tc)200); - typedef typename to::value_type _to; - CImg<_to> _opacity; - - for (unsigned int l = 0; l& primitive = primitives[n_primitive]; - const CImg - &__color = n_primitive(), - _color = (__color && __color.size()!=_spectrum && __color._spectrum<_spectrum)? - __color.get_resize(-100,-100,-100,_spectrum,0):CImg(), - &color = _color?_color:(__color?__color:default_color); - const tc *const pcolor = color._data; - const float opacity = __draw_object3d(opacities,n_primitive,_opacity); - -#ifdef cimg_use_board - LibBoard::Board &board = *(LibBoard::Board*)pboard; -#endif - - switch (primitive.size()) { - case 1 : { // Colored point or sprite - const unsigned int n0 = (unsigned int)primitive[0]; - const int x0 = (int)projections(n0,0), y0 = (int)projections(n0,1); - - if (_opacity.is_empty()) { // Scalar opacity. - - if (color.size()==_spectrum) { // Colored point. - draw_point(x0,y0,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.fillCircle((float)x0,height()-(float)y0,0); - } -#endif - } else { // Sprite. - const tpfloat z = Z + vertices(n0,2); - const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1); - const unsigned int - _sw = (unsigned int)(color._width*factor), - _sh = (unsigned int)(color._height*factor), - sw = _sw?_sw:1, sh = _sh?_sh:1; - const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2; - if (sw<=3*_width/2 && sh<=3*_height/2 && - (nx0+(int)sw/2>=0 || nx0-(int)sw/2=0 || ny0-(int)sh/2 - _sprite = (sw!=color._width || sh!=color._height)? - color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg(), - &sprite = _sprite?_sprite:color; - draw_image(nx0,ny0,sprite,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128); - board.setFillColor(LibBoard::Color::None); - board.drawRectangle((float)nx0,height()-(float)ny0,sw,sh); - } -#endif - } - } - } else { // Opacity mask. - const tpfloat z = Z + vertices(n0,2); - const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1); - const unsigned int - _sw = (unsigned int)(cimg::max(color._width,_opacity._width)*factor), - _sh = (unsigned int)(cimg::max(color._height,_opacity._height)*factor), - sw = _sw?_sw:1, sh = _sh?_sh:1; - const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2; - if (sw<=3*_width/2 && sh<=3*_height/2 && - (nx0+(int)sw/2>=0 || nx0-(int)sw/2=0 || ny0-(int)sh/2 - _sprite = (sw!=color._width || sh!=color._height)? - color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg(), - &sprite = _sprite?_sprite:color; - const CImg<_to> - _nopacity = (sw!=_opacity._width || sh!=_opacity._height)? - _opacity.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<_to>(), - &nopacity = _nopacity?_nopacity:_opacity; - draw_image(nx0,ny0,sprite,nopacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128); - board.setFillColor(LibBoard::Color::None); - board.drawRectangle((float)nx0,height()-(float)ny0,sw,sh); - } -#endif - } - } - } break; - case 2 : { // Colored line - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1]; - const int - x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), - x1 = (int)projections(n1,0), y1 = (int)projections(n1,1); - const float - z0 = vertices(n0,2) + Z + _focale, - z1 = vertices(n1,2) + Z + _focale; - if (render_type) { - if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity); - else draw_line(x0,y0,x1,y1,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.drawLine((float)x0,height()-(float)y0,x1,height()-(float)y1); - } -#endif - } else { - draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.drawCircle((float)x0,height()-(float)y0,0); - board.drawCircle((float)x1,height()-(float)y1,0); - } -#endif - } - } break; - case 5 : { // Colored sphere - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1], - is_wireframe = (unsigned int)primitive[2]; - const float - Xc = 0.5f*((float)vertices(n0,0) + (float)vertices(n1,0)), - Yc = 0.5f*((float)vertices(n0,1) + (float)vertices(n1,1)), - Zc = 0.5f*((float)vertices(n0,2) + (float)vertices(n1,2)), - zc = Z + Zc + _focale, - xc = X + Xc*(absfocale?absfocale/zc:1), - yc = Y + Yc*(absfocale?absfocale/zc:1), - radius = 0.5f*std::sqrt(cimg::sqr(vertices(n1,0) - vertices(n0,0)) + - cimg::sqr(vertices(n1,1) - vertices(n0,1)) + - cimg::sqr(vertices(n1,2) - vertices(n0,2)))*(absfocale?absfocale/zc:1); - switch (render_type) { - case 0 : - draw_point((int)xc,(int)yc,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.fillCircle(xc,height()-yc,0); - } -#endif - break; - case 1 : - draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.setFillColor(LibBoard::Color::None); - board.drawCircle(xc,height()-yc,radius); - } -#endif - break; - default : - if (is_wireframe) draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U); - else draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - if (!is_wireframe) board.fillCircle(xc,height()-yc,radius); - else { - board.setFillColor(LibBoard::Color::None); - board.drawCircle(xc,height()-yc,radius); - } - } -#endif - break; - } - } break; - case 6 : { // Textured line - if (!__color) - throw CImgArgumentException(_cimg_instance - "draw_object3d(): Undefined texture for line primitive [%u].", - cimg_instance,n_primitive); - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1], - tx0 = (unsigned int)primitive[2], - ty0 = (unsigned int)primitive[3], - tx1 = (unsigned int)primitive[4], - ty1 = (unsigned int)primitive[5]; - const int - x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), - x1 = (int)projections(n1,0), y1 = (int)projections(n1,1); - const float - z0 = vertices(n0,2) + Z + _focale, - z1 = vertices(n1,2) + Z + _focale; - if (render_type) { - if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity); - else draw_line(x0,y0,x1,y1,color,tx0,ty0,tx1,ty1,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); - } -#endif - } else { - draw_point(x0,y0,color.get_vector_at(tx0,ty0)._data,opacity). - draw_point(x1,y1,color.get_vector_at(tx1,ty1)._data,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.drawCircle((float)x0,height()-(float)y0,0); - board.drawCircle((float)x1,height()-(float)y1,0); - } -#endif - } - } break; - case 3 : { // Colored triangle - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1], - n2 = (unsigned int)primitive[2]; - const int - x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), - x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), - x2 = (int)projections(n2,0), y2 = (int)projections(n2,1); - const float - z0 = vertices(n0,2) + Z + _focale, - z1 = vertices(n1,2) + Z + _focale, - z2 = vertices(n2,2) + Z + _focale; - switch (render_type) { - case 0 : - draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity).draw_point(x2,y2,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.drawCircle((float)x0,height()-(float)y0,0); - board.drawCircle((float)x1,height()-(float)y1,0); - board.drawCircle((float)x2,height()-(float)y2,0); - } -#endif - break; - case 1 : - if (zbuffer) - draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x0,y0,z0,x2,y2,z2,pcolor,opacity). - draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity); - else - draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x0,y0,x2,y2,pcolor,opacity). - draw_line(x1,y1,x2,y2,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); - board.drawLine((float)x0,height()-(float)y0,(float)x2,height()-(float)y2); - board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); - } -#endif - break; - case 2 : - if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity); - else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - } -#endif - break; - case 3 : - if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l)); - else _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l)); -#ifdef cimg_use_board - if (pboard) { - const float lp = cimg::min(lightprops(l),1); - board.setPenColorRGBi((unsigned char)(color[0]*lp), - (unsigned char)(color[1]*lp), - (unsigned char)(color[2]*lp), - (unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - } -#endif - break; - case 4 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor, - lightprops(n0),lightprops(n1),lightprops(n2),opacity); - else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,lightprops(n0),lightprops(n1),lightprops(n2),opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi((unsigned char)(color[0]), - (unsigned char)(color[1]), - (unsigned char)(color[2]), - (unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprops(n0), - (float)x1,height()-(float)y1,lightprops(n1), - (float)x2,height()-(float)y2,lightprops(n2)); - } -#endif - break; - case 5 : { - const unsigned int - lx0 = (unsigned int)lightprops(n0,0), ly0 = (unsigned int)lightprops(n0,1), - lx1 = (unsigned int)lightprops(n1,0), ly1 = (unsigned int)lightprops(n1,1), - lx2 = (unsigned int)lightprops(n2,0), ly2 = (unsigned int)lightprops(n2,1); - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity); - else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity); -#ifdef cimg_use_board - if (pboard) { - const float - l0 = light_texture((int)(light_texture.width()/2*(1+lightprops(n0,0))), - (int)(light_texture.height()/2*(1+lightprops(n0,1)))), - l1 = light_texture((int)(light_texture.width()/2*(1+lightprops(n1,0))), - (int)(light_texture.height()/2*(1+lightprops(n1,1)))), - l2 = light_texture((int)(light_texture.width()/2*(1+lightprops(n2,0))), - (int)(light_texture.height()/2*(1+lightprops(n2,1)))); - board.setPenColorRGBi((unsigned char)(color[0]), - (unsigned char)(color[1]), - (unsigned char)(color[2]), - (unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, - (float)x1,height()-(float)y1,l1, - (float)x2,height()-(float)y2,l2); - } -#endif - } break; - } - } break; - case 4 : { // Colored rectangle - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1], - n2 = (unsigned int)primitive[2], - n3 = (unsigned int)primitive[3]; - const int - x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), - x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), - x2 = (int)projections(n2,0), y2 = (int)projections(n2,1), - x3 = (int)projections(n3,0), y3 = (int)projections(n3,1); - const float - z0 = vertices(n0,2) + Z + _focale, - z1 = vertices(n1,2) + Z + _focale, - z2 = vertices(n2,2) + Z + _focale, - z3 = vertices(n3,2) + Z + _focale; - - switch (render_type) { - case 0 : - draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity). - draw_point(x2,y2,pcolor,opacity).draw_point(x3,y3,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.drawCircle((float)x0,height()-(float)y0,0); - board.drawCircle((float)x1,height()-(float)y1,0); - board.drawCircle((float)x2,height()-(float)y2,0); - board.drawCircle((float)x3,height()-(float)y3,0); - } -#endif - break; - case 1 : - if (zbuffer) - draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity). - draw_line(zbuffer,x2,y2,z2,x3,y3,z3,pcolor,opacity).draw_line(zbuffer,x3,y3,z3,x0,y0,z0,pcolor,opacity); - else - draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x1,y1,x2,y2,pcolor,opacity). - draw_line(x2,y2,x3,y3,pcolor,opacity).draw_line(x3,y3,x0,y0,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); - board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); - board.drawLine((float)x2,height()-(float)y2,(float)x3,height()-(float)y3); - board.drawLine((float)x3,height()-(float)y3,(float)x0,height()-(float)y0); - } -#endif - break; - case 2 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity); - else - draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity).draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x2,height()-(float)y2, - (float)x3,height()-(float)y3); - } -#endif - break; - case 3 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l)). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity,lightprops(l)); - else - _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l)). - _draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity,lightprops(l)); -#ifdef cimg_use_board - if (pboard) { - const float lp = cimg::min(lightprops(l),1); - board.setPenColorRGBi((unsigned char)(color[0]*lp), - (unsigned char)(color[1]*lp), - (unsigned char)(color[2]*lp),(unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x2,height()-(float)y2, - (float)x3,height()-(float)y3); - } -#endif - break; - case 4 : { - const float - lightprop0 = lightprops(n0), lightprop1 = lightprops(n1), - lightprop2 = lightprops(n2), lightprop3 = lightprops(n3); - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,lightprop0,lightprop1,lightprop2,opacity). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,lightprop0,lightprop2,lightprop3,opacity); - else - draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,lightprop0,lightprop1,lightprop2,opacity). - draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,lightprop0,lightprop2,lightprop3,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi((unsigned char)(color[0]), - (unsigned char)(color[1]), - (unsigned char)(color[2]), - (unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, - (float)x1,height()-(float)y1,lightprop1, - (float)x2,height()-(float)y2,lightprop2); - board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, - (float)x2,height()-(float)y2,lightprop2, - (float)x3,height()-(float)y3,lightprop3); - } -#endif - } break; - case 5 : { - const unsigned int - lx0 = (unsigned int)lightprops(n0,0), ly0 = (unsigned int)lightprops(n0,1), - lx1 = (unsigned int)lightprops(n1,0), ly1 = (unsigned int)lightprops(n1,1), - lx2 = (unsigned int)lightprops(n2,0), ly2 = (unsigned int)lightprops(n2,1), - lx3 = (unsigned int)lightprops(n3,0), ly3 = (unsigned int)lightprops(n3,1); - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); - else - draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). - draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); -#ifdef cimg_use_board - if (pboard) { - const float - l0 = light_texture((int)(light_texture.width()/2*(1+lx0)), (int)(light_texture.height()/2*(1+ly0))), - l1 = light_texture((int)(light_texture.width()/2*(1+lx1)), (int)(light_texture.height()/2*(1+ly1))), - l2 = light_texture((int)(light_texture.width()/2*(1+lx2)), (int)(light_texture.height()/2*(1+ly2))), - l3 = light_texture((int)(light_texture.width()/2*(1+lx3)), (int)(light_texture.height()/2*(1+ly3))); - board.setPenColorRGBi((unsigned char)(color[0]), - (unsigned char)(color[1]), - (unsigned char)(color[2]), - (unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, - (float)x1,height()-(float)y1,l1, - (float)x2,height()-(float)y2,l2); - board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, - (float)x2,height()-(float)y2,l2, - (float)x3,height()-(float)y3,l3); - } -#endif - } break; - } - } break; - case 9 : { // Textured triangle - if (!__color) - throw CImgArgumentException(_cimg_instance - "draw_object3d(): Undefined texture for triangle primitive [%u].", - cimg_instance,n_primitive); - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1], - n2 = (unsigned int)primitive[2], - tx0 = (unsigned int)primitive[3], - ty0 = (unsigned int)primitive[4], - tx1 = (unsigned int)primitive[5], - ty1 = (unsigned int)primitive[6], - tx2 = (unsigned int)primitive[7], - ty2 = (unsigned int)primitive[8]; - const int - x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), - x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), - x2 = (int)projections(n2,0), y2 = (int)projections(n2,1); - const float - z0 = vertices(n0,2) + Z + _focale, - z1 = vertices(n1,2) + Z + _focale, - z2 = vertices(n2,2) + Z + _focale; - switch (render_type) { - case 0 : - draw_point(x0,y0,color.get_vector_at(tx0,ty0)._data,opacity). - draw_point(x1,y1,color.get_vector_at(tx1,ty1)._data,opacity). - draw_point(x2,y2,color.get_vector_at(tx2,ty2)._data,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.drawCircle((float)x0,height()-(float)y0,0); - board.drawCircle((float)x1,height()-(float)y1,0); - board.drawCircle((float)x2,height()-(float)y2,0); - } -#endif - break; - case 1 : - if (zbuffer) - draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). - draw_line(zbuffer,x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity). - draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity); - else - draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). - draw_line(x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity). - draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); - board.drawLine((float)x0,height()-(float)y0,(float)x2,height()-(float)y2); - board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); - } -#endif - break; - case 2 : - if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity); - else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - } -#endif - break; - case 3 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)); - else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)); -#ifdef cimg_use_board - if (pboard) { - const float lp = cimg::min(lightprops(l),1); - board.setPenColorRGBi((unsigned char)(128*lp), - (unsigned char)(128*lp), - (unsigned char)(128*lp), - (unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - } -#endif - break; - case 4 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, - lightprops(n0),lightprops(n1),lightprops(n2),opacity); - else - draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, - lightprops(n0),lightprops(n1),lightprops(n2),opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprops(n0), - (float)x1,height()-(float)y1,lightprops(n1), - (float)x2,height()-(float)y2,lightprops(n2)); - } -#endif - break; - case 5 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture, - (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1), - (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1), - (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1), - opacity); - else - draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture, - (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1), - (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1), - (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1), - opacity); -#ifdef cimg_use_board - if (pboard) { - const float - l0 = light_texture((int)(light_texture.width()/2*(1+lightprops(n0,0))), - (int)(light_texture.height()/2*(1+lightprops(n0,1)))), - l1 = light_texture((int)(light_texture.width()/2*(1+lightprops(n1,0))), - (int)(light_texture.height()/2*(1+lightprops(n1,1)))), - l2 = light_texture((int)(light_texture.width()/2*(1+lightprops(n2,0))), - (int)(light_texture.height()/2*(1+lightprops(n2,1)))); - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, - (float)x1,height()-(float)y1,l1, - (float)x2,height()-(float)y2,l2); - } -#endif - break; - } - } break; - case 12 : { // Textured quadrangle - if (!__color) - throw CImgArgumentException(_cimg_instance - "draw_object3d(): Undefined texture for quadrangle primitive [%u].", - cimg_instance,n_primitive); - const unsigned int - n0 = (unsigned int)primitive[0], - n1 = (unsigned int)primitive[1], - n2 = (unsigned int)primitive[2], - n3 = (unsigned int)primitive[3], - tx0 = (unsigned int)primitive[4], - ty0 = (unsigned int)primitive[5], - tx1 = (unsigned int)primitive[6], - ty1 = (unsigned int)primitive[7], - tx2 = (unsigned int)primitive[8], - ty2 = (unsigned int)primitive[9], - tx3 = (unsigned int)primitive[10], - ty3 = (unsigned int)primitive[11]; - const int - x0 = (int)projections(n0,0), y0 = (int)projections(n0,1), - x1 = (int)projections(n1,0), y1 = (int)projections(n1,1), - x2 = (int)projections(n2,0), y2 = (int)projections(n2,1), - x3 = (int)projections(n3,0), y3 = (int)projections(n3,1); - const float - z0 = vertices(n0,2) + Z + _focale, - z1 = vertices(n1,2) + Z + _focale, - z2 = vertices(n2,2) + Z + _focale, - z3 = vertices(n3,2) + Z + _focale; - - switch (render_type) { - case 0 : - draw_point(x0,y0,color.get_vector_at(tx0,ty0)._data,opacity). - draw_point(x1,y1,color.get_vector_at(tx1,ty1)._data,opacity). - draw_point(x2,y2,color.get_vector_at(tx2,ty2)._data,opacity). - draw_point(x3,y3,color.get_vector_at(tx3,ty3)._data,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.drawCircle((float)x0,height()-(float)y0,0); - board.drawCircle((float)x1,height()-(float)y1,0); - board.drawCircle((float)x2,height()-(float)y2,0); - board.drawCircle((float)x3,height()-(float)y3,0); - } -#endif - break; - case 1 : - if (zbuffer) - draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). - draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity). - draw_line(zbuffer,x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity). - draw_line(zbuffer,x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity); - else - draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity). - draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity). - draw_line(x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity). - draw_line(x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.drawLine((float)x0,height()-(float)y0,(float)x1,height()-(float)y1); - board.drawLine((float)x1,height()-(float)y1,(float)x2,height()-(float)y2); - board.drawLine((float)x2,height()-(float)y2,(float)x3,height()-(float)y3); - board.drawLine((float)x3,height()-(float)y3,(float)x0,height()-(float)y0); - } -#endif - break; - case 2 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity); - else - draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity). - draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x2,height()-(float)y2, - (float)x3,height()-(float)y3); - } -#endif - break; - case 3 : - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l)); - else - draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)). - draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l)); -#ifdef cimg_use_board - if (pboard) { - const float lp = cimg::min(lightprops(l),1); - board.setPenColorRGBi((unsigned char)(128*lp), - (unsigned char)(128*lp), - (unsigned char)(128*lp), - (unsigned char)(opacity*255)); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x1,height()-(float)y1, - (float)x2,height()-(float)y2); - board.fillTriangle((float)x0,height()-(float)y0, - (float)x2,height()-(float)y2, - (float)x3,height()-(float)y3); - } -#endif - break; - case 4 : { - const float - lightprop0 = lightprops(n0), lightprop1 = lightprops(n1), - lightprop2 = lightprops(n2), lightprop3 = lightprops(n3); - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, - lightprop0,lightprop1,lightprop2,opacity). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, - lightprop0,lightprop2,lightprop3,opacity); - else - draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, - lightprop0,lightprop1,lightprop2,opacity). - draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, - lightprop0,lightprop2,lightprop3,opacity); -#ifdef cimg_use_board - if (pboard) { - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, - (float)x1,height()-(float)y1,lightprop1, - (float)x2,height()-(float)y2,lightprop2); - board.fillGouraudTriangle((float)x0,height()-(float)y0,lightprop0, - (float)x2,height()-(float)y2,lightprop2, - (float)x3,height()-(float)y3,lightprop3); - } -#endif - } break; - case 5 : { - const unsigned int - lx0 = (unsigned int)lightprops(n0,0), ly0 = (unsigned int)lightprops(n0,1), - lx1 = (unsigned int)lightprops(n1,0), ly1 = (unsigned int)lightprops(n1,1), - lx2 = (unsigned int)lightprops(n2,0), ly2 = (unsigned int)lightprops(n2,1), - lx3 = (unsigned int)lightprops(n3,0), ly3 = (unsigned int)lightprops(n3,1); - if (zbuffer) - draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, - light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). - draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, - light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); - else - draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2, - light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity). - draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3, - light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity); -#ifdef cimg_use_board - if (pboard) { - const float - l0 = light_texture((int)(light_texture.width()/2*(1+lx0)), (int)(light_texture.height()/2*(1+ly0))), - l1 = light_texture((int)(light_texture.width()/2*(1+lx1)), (int)(light_texture.height()/2*(1+ly1))), - l2 = light_texture((int)(light_texture.width()/2*(1+lx2)), (int)(light_texture.height()/2*(1+ly2))), - l3 = light_texture((int)(light_texture.width()/2*(1+lx3)), (int)(light_texture.height()/2*(1+ly3))); - board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255)); - board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, - (float)x1,height()-(float)y1,l1, - (float)x2,height()-(float)y2,l2); - board.fillGouraudTriangle((float)x0,height()-(float)y0,l0, - (float)x2,height()-(float)y2,l2, - (float)x3,height()-(float)y3,l3); - } -#endif - } break; - } - } break; - } - } - - if (render_type==5) cimg::mutex(10,0); - return *this; - } - - //@} - //--------------------------- - // - //! \name Data Input - //@{ - //--------------------------- - - //! Launch simple interface to select a shape from an image. - /** - \param disp Display window to use. - \param feature_type Type of feature to select. Can be { 0=point | 1=line | 2=rectangle | 3=ellipse }. - \param XYZ Pointer to 3 values X,Y,Z which tells about the projection point coordinates, for volumetric images. - **/ - CImg& select(CImgDisplay &disp, - const unsigned int feature_type=2, unsigned int *const XYZ=0) { - return get_select(disp,feature_type,XYZ).move_to(*this); - } - - //! Simple interface to select a shape from an image \overloading. - CImg& select(const char *const title, - const unsigned int feature_type=2, unsigned int *const XYZ=0) { - return get_select(title,feature_type,XYZ).move_to(*this); - } - - //! Simple interface to select a shape from an image \newinstance. - CImg get_select(CImgDisplay &disp, - const unsigned int feature_type=2, unsigned int *const XYZ=0) const { - return _get_select(disp,0,feature_type,XYZ,0,0,0,true,false); - } - - //! Simple interface to select a shape from an image \newinstance. - CImg get_select(const char *const title, - const unsigned int feature_type=2, unsigned int *const XYZ=0) const { - CImgDisplay disp; - return _get_select(disp,title,feature_type,XYZ,0,0,0,true,false); - } - - CImg _get_select(CImgDisplay &disp, const char *const title, - const unsigned int feature_type, unsigned int *const XYZ, - const int origX, const int origY, const int origZ, - const bool reset_view3d, - const bool force_display_z_coord) const { - if (is_empty()) return CImg(1,feature_type==0?3:6,1,1,-1); - if (!disp) { - disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1); - if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum); - } else if (title) disp.set_title("%s",title); - - const unsigned int old_normalization = disp.normalization(); - bool old_is_resized = disp.is_resized(); - disp._normalization = 0; - disp.show().set_key(0).set_wheel().show_mouse(); - - unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 }; - - int area = 0, starting_area = 0, clicked_area = 0, phase = 0, - X0 = (int)((XYZ?XYZ[0]:(_width-1)/2)%_width), - Y0 = (int)((XYZ?XYZ[1]:(_height-1)/2)%_height), - Z0 = (int)((XYZ?XYZ[2]:(_depth-1)/2)%_depth), - X1 =-1, Y1 = -1, Z1 = -1, - X3d = -1, Y3d = -1, - oX3d = X3d, oY3d = -1, - omx = -1, omy = -1; - float X = -1, Y = -1, Z = -1; - unsigned int old_button = 0, key = 0; - - bool shape_selected = false, text_down = false, visible_cursor = true; - static CImg pose3d; - static bool is_view3d = false, is_axes = true; - if (reset_view3d) { pose3d.assign(); is_view3d = false; } - CImg points3d, opacities3d, sel_opacities3d; - CImgList primitives3d, sel_primitives3d; - CImgList colors3d, sel_colors3d; - CImg visu, visu0, view3d; - char text[1024] = { 0 }; - - while (!key && !disp.is_closed() && !shape_selected) { - - // Handle mouse motion and selection - int - mx = disp.mouse_x(), - my = disp.mouse_y(); - - const float - mX = mx<0?-1.0f:(float)mx*(width()+(depth()>1?depth():0))/disp.width(), - mY = my<0?-1.0f:(float)my*(height()+(depth()>1?depth():0))/disp.height(); - - area = 0; - if (mX>=0 && mY>=0 && mX=0 && mX=height()) { area = 2; X = mX; Z = mY - _height; Y = (float)(phase?Y1:Y0); } - if (mY>=0 && mX>=width() && mY=width() && mY>=height()) area = 4; - if (disp.button()) { if (!clicked_area) clicked_area = area; } else clicked_area = 0; - - switch (key = disp.key()) { -#if cimg_OS!=2 - case cimg::keyCTRLRIGHT : -#endif - case 0 : case cimg::keyCTRLLEFT : key = 0; break; - case cimg::keyPAGEUP : - if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(1); key = 0; } break; - case cimg::keyPAGEDOWN : - if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(-1); key = 0; } break; - case cimg::keyA : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - is_axes = !is_axes; disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), - CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). - _is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - is_view3d = !is_view3d; disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - if (visu0) { - (+visu0).draw_text(0,0," Saving snapshot... ",foreground_color,background_color,0.7f,13).display(disp); - visu0.save(filename); - (+visu0).draw_text(0,0," Snapshot '%s' saved. ",foreground_color,background_color,0.7f,13,filename). - display(disp); - } - disp.set_key(key,false); key = 0; - } break; - case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { -#ifdef cimg_use_zlib - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); -#else - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); -#endif - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu0).draw_text(0,0," Saving instance... ",foreground_color,background_color,0.7f,13).display(disp); - save(filename); - (+visu0).draw_text(0,0," Instance '%s' saved. ",foreground_color,background_color,0.7f,13,filename). - display(disp); - disp.set_key(key,false); key = 0; - } break; - } - - switch (area) { - - case 0 : // When mouse is out of image range. - mx = my = -1; X = Y = Z = -1; - break; - - case 1 : case 2 : case 3 : // When mouse is over the XY,XZ or YZ projections. - if (disp.button()&1 && phase<2 && clicked_area==area) { // When selection has been started (1st step). - if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign(); - X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; - } - if (!(disp.button()&1) && phase>=2 && clicked_area!=area) { // When selection is at 2nd step (for volumes). - switch (starting_area) { - case 1 : if (Z1!=(int)Z) visu0.assign(); Z1 = (int)Z; break; - case 2 : if (Y1!=(int)Y) visu0.assign(); Y1 = (int)Y; break; - case 3 : if (X1!=(int)X) visu0.assign(); X1 = (int)X; break; - } - } - if (disp.button()&2 && clicked_area==area) { // When moving through the image/volume. - if (phase) { - if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign(); - X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; - } else { - if (_depth>1 && (X0!=(int)X || Y0!=(int)Y || Z0!=(int)Z)) visu0.assign(); - X0 = (int)X; Y0 = (int)Y; Z0 = (int)Z; - } - } - if (disp.button()&4) { // Reset positions. - X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = area = clicked_area = starting_area = 0; - visu0.assign(); - } - if (disp.wheel()) { // When moving through the slices of the volume (with mouse wheel). - if (_depth>1 && !disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT() && - !disp.is_keySHIFTLEFT() && !disp.is_keySHIFTRIGHT() && - !disp.is_keyALT() && !disp.is_keyALTGR()) { - switch (area) { - case 1 : - if (phase) Z = (float)(Z1+=disp.wheel()); else Z = (float)(Z0+=disp.wheel()); - visu0.assign(); break; - case 2 : - if (phase) Y = (float)(Y1+=disp.wheel()); else Y = (float)(Y0+=disp.wheel()); - visu0.assign(); break; - case 3 : - if (phase) X = (float)(X1+=disp.wheel()); else X = (float)(X0+=disp.wheel()); - visu0.assign(); break; - } - disp.set_wheel(); - } else key = ~0U; - } - if ((disp.button()&1)!=old_button) { // When left button has just been pressed or released. - switch (phase) { - case 0 : - if (area==clicked_area) { - X0 = X1 = (int)X; Y0 = Y1 = (int)Y; Z0 = Z1 = (int)Z; starting_area = area; ++phase; - } break; - case 1 : - if (area==starting_area) { - X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; ++phase; - } else if (!(disp.button()&1)) { X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = 0; visu0.assign(); } - break; - case 2 : ++phase; break; - } - old_button = disp.button()&1; - } - break; - - case 4 : // When mouse is over the 3d view. - if (is_view3d && points3d) { - X3d = mx - _width*disp.width()/(_width+(_depth>1?_depth:0)); - Y3d = my - _height*disp.height()/(_height+(_depth>1?_depth:0)); - if (oX3d<0) { oX3d = X3d; oY3d = Y3d; } - // Left + right buttons: reset. - if ((disp.button()&3)==3) { pose3d.assign(); view3d.assign(); oX3d = oY3d = X3d = Y3d = -1; } - else if (disp.button()&1 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Left button: rotate. - const float - R = 0.45f*cimg::min(view3d._width,view3d._height), - R2 = R*R, - u0 = (float)(oX3d-view3d.width()/2), - v0 = (float)(oY3d-view3d.height()/2), - u1 = (float)(X3d-view3d.width()/2), - v1 = (float)(Y3d-view3d.height()/2), - n0 = (float)std::sqrt(u0*u0+v0*v0), - n1 = (float)std::sqrt(u1*u1+v1*v1), - nu0 = n0>R?(u0*R/n0):u0, - nv0 = n0>R?(v0*R/n0):v0, - nw0 = (float)std::sqrt(cimg::max(0,R2-nu0*nu0-nv0*nv0)), - nu1 = n1>R?(u1*R/n1):u1, - nv1 = n1>R?(v1*R/n1):v1, - nw1 = (float)std::sqrt(cimg::max(0,R2-nu1*nu1-nv1*nv1)), - u = nv0*nw1 - nw0*nv1, - v = nw0*nu1 - nu0*nw1, - w = nv0*nu1 - nu0*nv1, - n = (float)std::sqrt(u*u+v*v+w*w), - alpha = (float)std::asin(n/R2); - pose3d.draw_image(CImg::rotation_matrix(u,v,w,alpha)*pose3d.get_crop(0,0,2,2)); - view3d.assign(); - } else if (disp.button()&2 && pose3d && oY3d!=Y3d) { // Right button: zoom. - pose3d(3,2)-=(oY3d - Y3d)*1.5f; view3d.assign(); - } - if (disp.wheel()) { // Wheel: zoom - pose3d(3,2)-=disp.wheel()*15; view3d.assign(); disp.set_wheel(); - } - if (disp.button()&4 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Middle button: shift. - pose3d(3,0)-=oX3d - X3d; pose3d(3,1)-=oY3d - Y3d; view3d.assign(); - } - oX3d = X3d; oY3d = Y3d; - } - mx = my = -1; X = Y = Z = -1; - break; - } - - if (phase) { - if (!feature_type) shape_selected = phase?true:false; - else { - if (_depth>1) shape_selected = (phase==3)?true:false; - else shape_selected = (phase==2)?true:false; - } - } - - if (X0<0) X0 = 0; if (X0>=width()) X0 = width() - 1; - if (Y0<0) Y0 = 0; if (Y0>=height()) Y0 = height() - 1; - if (Z0<0) Z0 = 0; if (Z0>=depth()) Z0 = depth() - 1; - if (X1<1) X1 = 0; if (X1>=width()) X1 = width() - 1; - if (Y1<0) Y1 = 0; if (Y1>=height()) Y1 = height() - 1; - if (Z1<0) Z1 = 0; if (Z1>=depth()) Z1 = depth() - 1; - - // Draw visualization image on the display - if (mx!=omx || my!=omy || !visu0 || (_depth>1 && !view3d)) { - - if (!visu0) { // Create image of projected planes. - __get_select(disp,old_normalization,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0).move_to(visu0).resize(disp); - view3d.assign(); - points3d.assign(); - } - - if (is_view3d && _depth>1 && !view3d) { // Create 3d view for volumetric images. - const unsigned int - _x3d = (unsigned int)cimg::round((float)_width*visu0._width/(_width+_depth),1,1), - _y3d = (unsigned int)cimg::round((float)_height*visu0._height/(_height+_depth),1,1), - x3d = _x3d>=visu0._width?visu0._width-1:_x3d, - y3d = _y3d>=visu0._height?visu0._height-1:_y3d; - CImg(1,2,1,1,64,128).resize(visu0._width-x3d,visu0._height-y3d,1,visu0._spectrum,3).move_to(view3d); - if (!points3d) { - get_projections3d(primitives3d,colors3d,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0,true).move_to(points3d); - points3d.append(CImg(8,3,1,1, - 0,_width-1,_width-1,0,0,_width-1,_width-1,0, - 0,0,_height-1,_height-1,0,0,_height-1,_height-1, - 0,0,0,0,_depth-1,_depth-1,_depth-1,_depth-1),'x'); - CImg::vector(12,13).move_to(primitives3d); CImg::vector(13,14).move_to(primitives3d); - CImg::vector(14,15).move_to(primitives3d); CImg::vector(15,12).move_to(primitives3d); - CImg::vector(16,17).move_to(primitives3d); CImg::vector(17,18).move_to(primitives3d); - CImg::vector(18,19).move_to(primitives3d); CImg::vector(19,16).move_to(primitives3d); - CImg::vector(12,16).move_to(primitives3d); CImg::vector(13,17).move_to(primitives3d); - CImg::vector(14,18).move_to(primitives3d); CImg::vector(15,19).move_to(primitives3d); - colors3d.insert(12,CImg::vector(255,255,255)); - opacities3d.assign(primitives3d.width(),1,1,1,0.5f); - if (!phase) { - opacities3d[0] = opacities3d[1] = opacities3d[2] = 0.8f; - sel_primitives3d.assign(); - sel_colors3d.assign(); - sel_opacities3d.assign(); - } else { - if (feature_type==2) { - points3d.append(CImg(8,3,1,1, - X0,X1,X1,X0,X0,X1,X1,X0, - Y0,Y0,Y1,Y1,Y0,Y0,Y1,Y1, - Z0,Z0,Z0,Z0,Z1,Z1,Z1,Z1),'x'); - sel_primitives3d.assign(); - CImg::vector(20,21).move_to(sel_primitives3d); - CImg::vector(21,22).move_to(sel_primitives3d); - CImg::vector(22,23).move_to(sel_primitives3d); - CImg::vector(23,20).move_to(sel_primitives3d); - CImg::vector(24,25).move_to(sel_primitives3d); - CImg::vector(25,26).move_to(sel_primitives3d); - CImg::vector(26,27).move_to(sel_primitives3d); - CImg::vector(27,24).move_to(sel_primitives3d); - CImg::vector(20,24).move_to(sel_primitives3d); - CImg::vector(21,25).move_to(sel_primitives3d); - CImg::vector(22,26).move_to(sel_primitives3d); - CImg::vector(23,27).move_to(sel_primitives3d); - } else { - points3d.append(CImg(2,3,1,1, - X0,X1, - Y0,Y1, - Z0,Z1),'x'); - sel_primitives3d.assign(CImg::vector(20,21)); - } - sel_colors3d.assign(sel_primitives3d._width,CImg::vector(255,255,255)); - sel_opacities3d.assign(sel_primitives3d._width,1,1,1,0.8f); - } - points3d.shift_object3d(-0.5f*(_width-1),-0.5f*(_height-1),-0.5f*(_depth-1)).resize_object3d(); - points3d*=0.75f*cimg::min(view3d._width,view3d._height); - } - - if (!pose3d) CImg(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose3d); - CImg zbuffer3d(view3d._width,view3d._height,1,1,0); - const CImg rotated_points3d = pose3d.get_crop(0,0,2,2)*points3d; - if (sel_primitives3d) - view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width, - pose3d(3,1) + 0.5f*view3d._height, - pose3d(3,2), - rotated_points3d,sel_primitives3d,sel_colors3d,sel_opacities3d, - 2,true,500,0,0,0,0,0,zbuffer3d); - view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width, - pose3d(3,1) + 0.5f*view3d._height, - pose3d(3,2), - rotated_points3d,primitives3d,colors3d,opacities3d, - 2,true,500,0,0,0,0,0,zbuffer3d); - visu0.draw_image(x3d,y3d,view3d); - } - visu = visu0; - - if (X<0 || Y<0 || Z<0) { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }} - else { - if (is_axes) { if (visible_cursor) { disp.hide_mouse(); visible_cursor = false; }} - else { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }} - const int d = (_depth>1)?_depth:0; - int - w = disp.width(), W = width() + d, - h = disp.height(), H = height() + d, - _xp = (int)X*w/W, xp = _xp + (_xp*W/w!=(int)X?1:0), - _yp = (int)Y*h/H, yp = _yp + (_yp*H/h!=(int)Y?1:0), - _xn = (int)(X+1)*w/W-1, xn = _xn + ((_xn+1)*W/w!=(int)X+1?1:0), - _yn = (int)(Y+1)*h/H-1, yn = _yn + ((_yn+1)*H/h!=(int)Y+1?1:0), - _zxp = ((int)Z+width())*w/W, zxp = _zxp + (_zxp*W/w!=(int)Z+width()?1:0), - _zyp = ((int)Z+height())*h/H, zyp = _zyp + (_zyp*H/h!=(int)Z+height()?1:0), - _zxn = ((int)Z+width()+1)*w/W-1, zxn = _zxn + ((_zxn+1)*W/w!=(int)Z+width()+1?1:0), - _zyn = ((int)Z+height()+1)*h/H-1, zyn = _zyn + ((_zyn+1)*H/h!=(int)Z+height()+1?1:0), - _xM = width()*w/W-1, xM = _xM + ((_xM+1)*W/w!=width()?1:0), - _yM = height()*h/H-1, yM = _yM + ((_yM+1)*H/h!=height()?1:0), - xc = (xp + xn)/2, - yc = (yp + yn)/2, - zxc = (zxp + zxn)/2, - zyc = (zyp + zyn)/2, - xf = (int)(X*w/W), - yf = (int)(Y*h/H), - zxf = (int)((Z+width())*w/W), - zyf = (int)((Z+height())*h/H); - - if (is_axes) { // Draw axes. - visu.draw_line(0,yf,visu.width()-1,yf,foreground_color,0.7f,0xFF00FF00). - draw_line(0,yf,visu.width()-1,yf,background_color,0.7f,0x00FF00FF). - draw_line(xf,0,xf,visu.height()-1,foreground_color,0.7f,0xFF00FF00). - draw_line(xf,0,xf,visu.height()-1,background_color,0.7f,0x00FF00FF); - if (_depth>1) - visu.draw_line(zxf,0,zxf,yM,foreground_color,0.7f,0xFF00FF00). - draw_line(zxf,0,zxf,yM,background_color,0.7f,0x00FF00FF). - draw_line(0,zyf,xM,zyf,foreground_color,0.7f,0xFF00FF00). - draw_line(0,zyf,xM,zyf,background_color,0.7f,0x00FF00FF); - } - - // Draw box cursor. - if (xn-xp>=4 && yn-yp>=4) visu.draw_rectangle(xp,yp,xn,yn,foreground_color,0.2f). - draw_rectangle(xp,yp,xn,yn,foreground_color,1,0xAAAAAAAA). - draw_rectangle(xp,yp,xn,yn,background_color,1,0x55555555); - if (_depth>1) { - if (yn-yp>=4 && zxn-zxp>=4) visu.draw_rectangle(zxp,yp,zxn,yn,background_color,0.2f). - draw_rectangle(zxp,yp,zxn,yn,foreground_color,1,0xAAAAAAAA). - draw_rectangle(zxp,yp,zxn,yn,background_color,1,0x55555555); - if (xn-xp>=4 && zyn-zyp>=4) visu.draw_rectangle(xp,zyp,xn,zyn,background_color,0.2f). - draw_rectangle(xp,zyp,xn,zyn,foreground_color,1,0xAAAAAAAA). - draw_rectangle(xp,zyp,xn,zyn,background_color,1,0x55555555); - } - - // Draw selection. - if (phase) { - const int - _xp0 = X0*w/W, xp0 = _xp0 + (_xp0*W/w!=X0?1:0), - _yp0 = Y0*h/H, yp0 = _yp0 + (_yp0*H/h!=Y0?1:0), - _xn0 = (X0+1)*w/W-1, xn0 = _xn0 + ((_xn0+1)*W/w!=X0+1?1:0), - _yn0 = (Y0+1)*h/H-1, yn0 = _yn0 + ((_yn0+1)*H/h!=Y0+1?1:0), - _zxp0 = (Z0+width())*w/W, zxp0 = _zxp0 + (_zxp0*W/w!=Z0+width()?1:0), - _zyp0 = (Z0+height())*h/H, zyp0 = _zyp0 + (_zyp0*H/h!=Z0+height()?1:0), - _zxn0 = (Z0+width()+1)*w/W-1, zxn0 = _zxn0 + ((_zxn0+1)*W/w!=Z0+width()+1?1:0), - _zyn0 = (Z0+height()+1)*h/H-1, zyn0 = _zyn0 + ((_zyn0+1)*H/h!=Z0+height()+1?1:0), - xc0 = (xp0 + xn0)/2, - yc0 = (yp0 + yn0)/2, - zxc0 = (zxp0 + zxn0)/2, - zyc0 = (zyp0 + zyn0)/2; - - switch (feature_type) { - case 1 : { - visu.draw_arrow(xc0,yc0,xc,yc,background_color,0.9f,30,5,0x55555555). - draw_arrow(xc0,yc0,xc,yc,foreground_color,0.9f,30,5,0xAAAAAAAA); - if (d) { - visu.draw_arrow(zxc0,yc0,zxc,yc,background_color,0.9f,30,5,0x55555555). - draw_arrow(zxc0,yc0,zxc,yc,foreground_color,0.9f,30,5,0xAAAAAAAA). - draw_arrow(xc0,zyc0,xc,zyc,background_color,0.9f,30,5,0x55555555). - draw_arrow(xc0,zyc0,xc,zyc,foreground_color,0.9f,30,5,0xAAAAAAAA); - } - } break; - case 2 : { - visu.draw_rectangle(X0=0 && my<13) text_down = true; else if (my>=visu.height()-13) text_down = false; - if (!feature_type || !phase) { - if (X>=0 && Y>=0 && Z>=0 && X1 || force_display_z_coord) - cimg_snprintf(text,sizeof(text)," Point (%d,%d,%d) = [ ",origX+(int)X,origY+(int)Y,origZ+(int)Z); - else cimg_snprintf(text,sizeof(text)," Point (%d,%d) = [ ",origX+(int)X,origY+(int)Y); - char *ctext = text + std::strlen(text), *const ltext = text + 512; - for (unsigned int c = 0; c<_spectrum && ctext::format(), - cimg::type::format((*this)((int)X,(int)Y,(int)Z,c))); - ctext = text + std::strlen(text); - *(ctext++) = ' '; *ctext = 0; - } - std::strcpy(text + std::strlen(text),"] "); - } - } else switch (feature_type) { - case 1 : { - const double dX = (double)(X0 - X1), dY = (double)(Y0 - Y1), dZ = (double)(Z0 - Z1), - norm = std::sqrt(dX*dX+dY*dY+dZ*dZ); - if (_depth>1 || force_display_z_coord) - cimg_snprintf(text,sizeof(text)," Vect (%d,%d,%d)-(%d,%d,%d), Norm = %g ", - origX+X0,origY+Y0,origZ+Z0,origX+X1,origY+Y1,origZ+Z1,norm); - else cimg_snprintf(text,sizeof(text)," Vect (%d,%d)-(%d,%d), Norm = %g ", - origX+X0,origY+Y0,origX+X1,origY+Y1,norm); - } break; - case 2 : - if (_depth>1 || force_display_z_coord) - cimg_snprintf(text,sizeof(text)," Box (%d,%d,%d)-(%d,%d,%d), Size = (%d,%d,%d) ", - origX+(X01 || force_display_z_coord) - cimg_snprintf(text,sizeof(text)," Ellipse (%d,%d,%d)-(%d,%d,%d), Radii = (%d,%d,%d) ", - origX+X0,origY+Y0,origZ+Z0,origX+X1,origY+Y1,origZ+Z1, - 1+cimg::abs(X0-X1),1+cimg::abs(Y0-Y1),1+cimg::abs(Z0-Z1)); - else cimg_snprintf(text,sizeof(text)," Ellipse (%d,%d)-(%d,%d), Radii = (%d,%d) ", - origX+X0,origY+Y0,origX+X1,origY+Y1,1+cimg::abs(X0-X1),1+cimg::abs(Y0-Y1)); - } - if (phase || (mx>=0 && my>=0)) - visu.draw_text(0,text_down?visu.height()-13:0,text,foreground_color,background_color,0.7f,13); - } - - disp.display(visu).wait(); - } else if (!shape_selected) disp.wait(); - if (disp.is_resized()) { disp.resize(false)._is_resized = false; old_is_resized = true; visu0.assign(); } - omx = mx; omy = my; - } - - // Return result. - CImg res(1,feature_type==0?3:6,1,1,-1); - if (XYZ) { XYZ[0] = (unsigned int)X0; XYZ[1] = (unsigned int)Y0; XYZ[2] = (unsigned int)Z0; } - if (shape_selected) { - if (feature_type==2) { - if (X0>X1) cimg::swap(X0,X1); - if (Y0>Y1) cimg::swap(Y0,Y1); - if (Z0>Z1) cimg::swap(Z0,Z1); - } - if (X1<0 || Y1<0 || Z1<0) X0 = Y0 = Z0 = X1 = Y1 = Z1 = -1; - switch (feature_type) { - case 1 : case 2 : res[0] = X0; res[1] = Y0; res[2] = Z0; res[3] = X1; res[4] = Y1; res[5] = Z1; break; - case 3 : - res[3] = cimg::abs(X1-X0); res[4] = cimg::abs(Y1-Y0); res[5] = cimg::abs(Z1-Z0); // keep no break here! - default : res[0] = X0; res[1] = Y0; res[2] = Z0; - } - } - disp.set_button(); - if (!visible_cursor) disp.show_mouse(); - disp._normalization = old_normalization; - disp._is_resized = old_is_resized; - if (key!=~0U) disp.set_key(key); - return res; - } - - // Return a visualizable uchar8 image for display routines. - CImg __get_select(const CImgDisplay& disp, const int normalization, - const int x, const int y, const int z) const { - if (is_empty()) return CImg(1,1,1,1,0); - const CImg crop = get_shared_channels(0,cimg::min(2,spectrum()-1)); - CImg img2d; - if (_depth>1) crop.get_projections2d(x,y,z).move_to(img2d); - else CImg(crop,false).move_to(img2d); - - if (cimg::type::is_float()) { // Check for inf and nan values. - bool is_inf = false, is_nan = false; - cimg_for(img2d,ptr,Tuchar) - if (cimg::type::is_inf(*ptr)) { is_inf = true; break; } - else if (cimg::type::is_nan(*ptr)) { is_nan = true; break; } - if (is_inf || is_nan) { - T m0 = cimg::type::max(), M0 = cimg::type::min(); - if (!normalization) { m0 = 0; M0 = 255; } - else if (normalization==2) { m0 = (T)disp._min; M0 = (T)disp._max; } - else - cimg_for(img2d,ptr,Tuchar) - if (!cimg::type::is_inf(*ptr) && !cimg::type::is_nan(*ptr)) { - if (*ptrM0) M0 = *ptr; - } - const T - val_minf = (normalization==1 || normalization==3)?m0-(M0-m0)*20-1:m0, - val_pinf = (normalization==1 || normalization==3)?M0+(M0-m0)*20+1:M0; - if (is_nan) - cimg_for(img2d,ptr,Tuchar) - if (cimg::type::is_nan(*ptr)) *ptr = val_minf; // Replace nan values. - if (is_inf) - cimg_for(img2d,ptr,Tuchar) - if (cimg::type::is_inf(*ptr)) *ptr = (float)*ptr<0?val_minf:val_pinf; // Replace +-inf values. - } - } - - switch (normalization) { - case 1 : img2d.normalize(0,255); break; - case 2 : { - const float m = disp._min, M = disp._max; - (img2d-=m)*=255.0f/(M-m>0?M-m:1); - } break; - case 3 : - if (cimg::type::is_float()) img2d.normalize(0,255); - else { - const float m = (float)cimg::type::min(), M = (float)cimg::type::max(); - (img2d-=m)*=255.0f/(M-m>0?M-m:1); - } break; - } - - if (img2d.spectrum()==2) img2d.channels(0,2); - return img2d; - } - - //! Select sub-graph in a graph. - CImg get_select_graph(CImgDisplay &disp, - const unsigned int plot_type=1, const unsigned int vertex_type=1, - const char *const labelx=0, const double xmin=0, const double xmax=0, - const char *const labely=0, const double ymin=0, const double ymax=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "select_graph(): Empty instance.", - cimg_instance); - if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0). - set_title("CImg<%s>",pixel_type()); - const unsigned long siz = (unsigned long)_width*_height*_depth; - const unsigned int old_normalization = disp.normalization(); - disp.show().set_button().set_wheel()._normalization = 0; - - double nymin = ymin, nymax = ymax, nxmin = xmin, nxmax = xmax; - if (nymin==nymax) { nymin = (Tfloat)min_max(nymax); const double dy = nymax - nymin; nymin-=dy/20; nymax+=dy/20; } - if (nymin==nymax) { --nymin; ++nymax; } - if (nxmin==nxmax && nxmin==0) { nxmin = 0; nxmax = siz - 1.0; } - - const unsigned char black[] = { 0, 0, 0 }, white[] = { 255, 255, 255 }, gray[] = { 220, 220, 220 }; - const unsigned char gray2[] = { 110, 110, 110 }, ngray[] = { 35, 35, 35 }; - static unsigned int odimv = 0; - static CImg colormap; - if (odimv!=_spectrum) { - odimv = _spectrum; - colormap = CImg(3,_spectrum,1,1,120).noise(70,1); - if (_spectrum==1) { colormap[0] = colormap[1] = 120; colormap[2] = 200; } - else { - colormap(0,0) = 220; colormap(1,0) = 10; colormap(2,0) = 10; - if (_spectrum>1) { colormap(0,1) = 10; colormap(1,1) = 220; colormap(2,1) = 10; } - if (_spectrum>2) { colormap(0,2) = 10; colormap(1,2) = 10; colormap(2,2) = 220; } - } - } - - CImg visu0, visu, graph, text, axes; - int x0 = -1, x1 = -1, y0 = -1, y1 = -1, omouse_x = -2, omouse_y = -2; - const unsigned int one = plot_type==3?0:1; - unsigned int okey = 0, obutton = 0; - char message[1024] = { 0 }; - CImg_3x3(I,unsigned char); - - for (bool selected = false; !selected && !disp.is_closed() && !okey && !disp.wheel(); ) { - const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y(); - const unsigned int key = disp.key(), button = disp.button(); - - // Generate graph representation. - if (!visu0) { - visu0.assign(disp.width(),disp.height(),1,3,220); - const int gdimx = disp.width() - 32, gdimy = disp.height() - 32; - if (gdimx>0 && gdimy>0) { - graph.assign(gdimx,gdimy,1,3,255); - if (siz<32) { - if (siz>1) graph.draw_grid(gdimx/(float)(siz - one),gdimy/(float)(siz - one),0,0, - false,true,black,0.2f,0x33333333,0x33333333); - } else graph.draw_grid(-10,-10,0,0,false,true,black,0.2f,0x33333333,0x33333333); - cimg_forC(*this,c) - graph.draw_graph(get_shared_channel(c),&colormap(0,c),(plot_type!=3 || _spectrum==1)?1:0.6f, - plot_type,vertex_type,nymax,nymin); - - axes.assign(gdimx,gdimy,1,1,0); - const float - dx = (float)cimg::abs(nxmax-nxmin), dy = (float)cimg::abs(nymax-nymin), - px = (float)std::pow(10.0,(int)std::log10(dx?dx:1)-2.0), - py = (float)std::pow(10.0,(int)std::log10(dy?dy:1)-2.0); - const CImg - seqx = dx<=0?CImg::vector(nxmin): - CImg::sequence(1 + gdimx/60,nxmin,one?nxmax:nxmin+(nxmax-nxmin)*(siz+1)/siz).round(px), - seqy = CImg::sequence(1 + gdimy/60,nymax,nymin).round(py); - - const bool allow_zero = (nxmin*nxmax>0) || (nymin*nymax>0); - axes.draw_axes(seqx,seqy,white,1,~0U,~0U,13,allow_zero); - if (nymin>0) axes.draw_axis(seqx,gdimy-1,gray,1,~0U,13,allow_zero); - if (nymax<0) axes.draw_axis(seqx,0,gray,1,~0U,13,allow_zero); - if (nxmin>0) axes.draw_axis(0,seqy,gray,1,~0U,13,allow_zero); - if (nxmax<0) axes.draw_axis(gdimx-1,seqy,gray,1,~0U,13,allow_zero); - - cimg_for3x3(axes,x,y,0,0,I,unsigned char) - if (Icc) { - if (Icc==255) cimg_forC(graph,c) graph(x,y,c) = 0; - else cimg_forC(graph,c) graph(x,y,c) = (unsigned char)(2*graph(x,y,c)/3); - } - else if (Ipc || Inc || Icp || Icn || Ipp || Inn || Ipn || Inp) - cimg_forC(graph,c) graph(x,y,c) = (graph(x,y,c)+511)/3; - - visu0.draw_image(16,16,graph); - visu0.draw_line(15,15,16+gdimx,15,gray2).draw_line(16+gdimx,15,16+gdimx,16+gdimy,gray2). - draw_line(16+gdimx,16+gdimy,15,16+gdimy,white).draw_line(15,16+gdimy,15,15,white); - } else graph.assign(); - text.assign().draw_text(0,0,labelx?labelx:"X-axis",white,ngray,1,13).resize(-100,-100,1,3); - visu0.draw_image((visu0.width()-text.width())/2,visu0.height()-14,~text); - text.assign().draw_text(0,0,labely?labely:"Y-axis",white,ngray,1,13).rotate(-90).resize(-100,-100,1,3); - visu0.draw_image(1,(visu0.height()-text.height())/2,~text); - visu.assign(); - } - - // Generate and display current view. - if (!visu) { - visu.assign(visu0); - if (graph && x0>=0 && x1>=0) { - const int - nx0 = x0<=x1?x0:x1, - nx1 = x0<=x1?x1:x0, - ny0 = y0<=y1?y0:y1, - ny1 = y0<=y1?y1:y0, - sx0 = 16 + nx0*(visu.width()-32)/cimg::max(1U,siz-one), - sx1 = 15 + (nx1+1)*(visu.width()-32)/cimg::max(1U,siz-one), - sy0 = 16 + ny0, - sy1 = 16 + ny1; - if (y0>=0 && y1>=0) - visu.draw_rectangle(sx0,sy0,sx1,sy1,gray,0.5f).draw_rectangle(sx0,sy0,sx1,sy1,black,0.5f,0xCCCCCCCCU); - else visu.draw_rectangle(sx0,0,sx1,visu.height()-17,gray,0.5f). - draw_line(sx0,16,sx0,visu.height()-17,black,0.5f,0xCCCCCCCCU). - draw_line(sx1,16,sx1,visu.height()-17,black,0.5f,0xCCCCCCCCU); - } - if (mouse_x>=16 && mouse_y>=16 && mouse_x=7) - cimg_snprintf(message,sizeof(message),"Value[%u:%g] = ( %g %g %g ... %g %g %g )",x,cx, - (double)(*this)(x,0,0,0),(double)(*this)(x,0,0,1),(double)(*this)(x,0,0,2), - (double)(*this)(x,0,0,_spectrum-4),(double)(*this)(x,0,0,_spectrum-3), - (double)(*this)(x,0,0,_spectrum-1)); - else { - cimg_snprintf(message,sizeof(message),"Value[%u:%g] = ( ",x,cx); - cimg_forC(*this,c) std::sprintf(message + std::strlen(message),"%g ",(double)(*this)(x,0,0,c)); - std::sprintf(message + std::strlen(message),")"); - } - if (x0>=0 && x1>=0) { - const unsigned int - nx0 = x0<=x1?x0:x1, - nx1 = x0<=x1?x1:x0, - ny0 = y0<=y1?y0:y1, - ny1 = y0<=y1?y1:y0; - const double - cx0 = nxmin + nx0*(nxmax-nxmin)/cimg::max(1U,siz-1), - cx1 = nxmin + (nx1+one)*(nxmax-nxmin)/cimg::max(1U,siz-1), - cy0 = nymax - ny0*(nymax-nymin)/(visu._height-32), - cy1 = nymax - ny1*(nymax-nymin)/(visu._height-32); - if (y0>=0 && y1>=0) - std::sprintf(message + std::strlen(message)," - Range ( %u:%g, %g ) - ( %u:%g, %g )", - x0,cx0,cy0,x1+one,cx1,cy1); - else - std::sprintf(message + std::strlen(message)," - Range [ %u:%g - %u:%g ]", - x0,cx0,x1+one,cx1); - } - text.assign().draw_text(0,0,message,white,ngray,1,13).resize(-100,-100,1,3); - visu.draw_image((visu.width()-text.width())/2,1,~text); - } - visu.display(disp); - } - - // Test keys. - switch (okey = key) { -#if cimg_OS!=2 - case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT : -#endif - case cimg::keyCTRLLEFT : case cimg::keySHIFTLEFT : okey = 0; break; - case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), - CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). - _is_resized = true; - disp.set_key(key,false); okey = 0; - } break; - case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; - disp.set_key(key,false); okey = 0; - } break; - case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(CImgDisplay::screen_width()/2, - CImgDisplay::screen_height()/2,1),false)._is_resized = true; - disp.set_key(key,false); okey = 0; - } break; - case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true; - disp.set_key(key,false); okey = 0; - } break; - case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - static unsigned int snap_number = 0; - if (visu || visu0) { - CImg &screen = visu?visu:visu0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+screen).draw_text(0,0," Saving snapshot... ",black,gray,1,13).display(disp); - screen.save(filename); - (+screen).draw_text(0,0," Snapshot '%s' saved. ",black,gray,1,13,filename).display(disp); - } - disp.set_key(key,false); okey = 0; - } break; - case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - static unsigned int snap_number = 0; - if (visu || visu0) { - CImg &screen = visu?visu:visu0; - char filename[32] = { 0 }; - std::FILE *file; - do { -#ifdef cimg_use_zlib - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); -#else - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); -#endif - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+screen).draw_text(0,0," Saving instance... ",black,gray,1,13).display(disp); - save(filename); - (+screen).draw_text(0,0," Instance '%s' saved. ",black,gray,1,13,filename).display(disp); - } - disp.set_key(key,false); okey = 0; - } break; - } - - // Handle mouse motion and mouse buttons - if (obutton!=button || omouse_x!=mouse_x || omouse_y!=mouse_y) { - visu.assign(); - if (disp.mouse_x()>=0 && disp.mouse_y()>=0) { - const int - mx = (mouse_x -16)*(int)(siz-one)/(disp.width()-32), - cx = mx<0?0:(mx>=(int)(siz-one)?(int)(siz-1-one):mx), - my = mouse_y - 16, - cy = my<=0?0:(my>=(disp.height()-32)?(disp.height()-32):my); - if (button&1) { - if (!obutton) { x0 = cx; y0 = -1; } else { x1 = cx; y1 = -1; } - } - else if (button&2) { - if (!obutton) { x0 = cx; y0 = cy; } else { x1 = cx; y1 = cy; } - } - else if (obutton) { x1 = x1>=0?cx:-1; y1 = y1>=0?cy:-1; selected = true; } - } else if (!button && obutton) selected = true; - obutton = button; omouse_x = mouse_x; omouse_y = mouse_y; - } - if (disp.is_resized()) { disp.resize(false); visu0.assign(); } - if (visu && visu0) disp.wait(); - } - - disp._normalization = old_normalization; - if (x1>=0 && x1(4,1,1,1,x0,y0,x1>=0?x1+(int)one:-1,y1); - } - - //! Load image from a file. - /** - \param filename Filename, as a C-string. - \note The extension of \c filename defines the file format. If no filename - extension is provided, CImg::get_load() will try to load the file as a .cimg or .cimgz file. - **/ - CImg& load(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load(): Specified filename is (null).", - cimg_instance); - - if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) { - char filename_local[1024] = { 0 }; - load(cimg::load_network_external(filename,filename_local)); - std::remove(filename_local); - return *this; - } - - const char *const ext = cimg::split_filename(filename); - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { -#ifdef cimg_load_plugin - cimg_load_plugin(filename); -#endif -#ifdef cimg_load_plugin1 - cimg_load_plugin1(filename); -#endif -#ifdef cimg_load_plugin2 - cimg_load_plugin2(filename); -#endif -#ifdef cimg_load_plugin3 - cimg_load_plugin3(filename); -#endif -#ifdef cimg_load_plugin4 - cimg_load_plugin4(filename); -#endif -#ifdef cimg_load_plugin5 - cimg_load_plugin5(filename); -#endif -#ifdef cimg_load_plugin6 - cimg_load_plugin6(filename); -#endif -#ifdef cimg_load_plugin7 - cimg_load_plugin7(filename); -#endif -#ifdef cimg_load_plugin8 - cimg_load_plugin8(filename); -#endif - // Ascii formats - if (!cimg::strcasecmp(ext,"asc")) load_ascii(filename); - else if (!cimg::strcasecmp(ext,"dlm") || - !cimg::strcasecmp(ext,"txt")) load_dlm(filename); - - // 2d binary formats - else if (!cimg::strcasecmp(ext,"bmp")) load_bmp(filename); - else if (!cimg::strcasecmp(ext,"jpg") || - !cimg::strcasecmp(ext,"jpeg") || - !cimg::strcasecmp(ext,"jpe") || - !cimg::strcasecmp(ext,"jfif") || - !cimg::strcasecmp(ext,"jif")) load_jpeg(filename); - else if (!cimg::strcasecmp(ext,"png")) load_png(filename); - else if (!cimg::strcasecmp(ext,"ppm") || - !cimg::strcasecmp(ext,"pgm") || - !cimg::strcasecmp(ext,"pnm") || - !cimg::strcasecmp(ext,"pbm") || - !cimg::strcasecmp(ext,"pnk")) load_pnm(filename); - else if (!cimg::strcasecmp(ext,"pfm")) load_pfm(filename); - else if (!cimg::strcasecmp(ext,"tif") || - !cimg::strcasecmp(ext,"tiff")) load_tiff(filename); - else if (!cimg::strcasecmp(ext,"exr")) load_exr(filename); - else if (!cimg::strcasecmp(ext,"cr2") || - !cimg::strcasecmp(ext,"crw") || - !cimg::strcasecmp(ext,"dcr") || - !cimg::strcasecmp(ext,"mrw") || - !cimg::strcasecmp(ext,"nef") || - !cimg::strcasecmp(ext,"orf") || - !cimg::strcasecmp(ext,"pix") || - !cimg::strcasecmp(ext,"ptx") || - !cimg::strcasecmp(ext,"raf") || - !cimg::strcasecmp(ext,"srf")) load_dcraw_external(filename); - else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename); - - // 3d binary formats - else if (!cimg::strcasecmp(ext,"dcm") || - !cimg::strcasecmp(ext,"dicom")) load_medcon_external(filename); - else if (!cimg::strcasecmp(ext,"hdr") || - !cimg::strcasecmp(ext,"nii")) load_analyze(filename); - else if (!cimg::strcasecmp(ext,"par") || - !cimg::strcasecmp(ext,"rec")) load_parrec(filename); - else if (!cimg::strcasecmp(ext,"mnc")) load_minc2(filename); - else if (!cimg::strcasecmp(ext,"inr")) load_inr(filename); - else if (!cimg::strcasecmp(ext,"pan")) load_pandore(filename); - else if (!cimg::strcasecmp(ext,"cimg") || - !cimg::strcasecmp(ext,"cimgz") || - !*ext) return load_cimg(filename); - - // Archive files - else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename); - - // Image sequences - else if (!cimg::strcasecmp(ext,"avi") || - !cimg::strcasecmp(ext,"mov") || - !cimg::strcasecmp(ext,"asf") || - !cimg::strcasecmp(ext,"divx") || - !cimg::strcasecmp(ext,"flv") || - !cimg::strcasecmp(ext,"mpg") || - !cimg::strcasecmp(ext,"m1v") || - !cimg::strcasecmp(ext,"m2v") || - !cimg::strcasecmp(ext,"m4v") || - !cimg::strcasecmp(ext,"mjp") || - !cimg::strcasecmp(ext,"mp4") || - !cimg::strcasecmp(ext,"mkv") || - !cimg::strcasecmp(ext,"mpe") || - !cimg::strcasecmp(ext,"movie") || - !cimg::strcasecmp(ext,"ogm") || - !cimg::strcasecmp(ext,"ogg") || - !cimg::strcasecmp(ext,"ogv") || - !cimg::strcasecmp(ext,"qt") || - !cimg::strcasecmp(ext,"rm") || - !cimg::strcasecmp(ext,"vob") || - !cimg::strcasecmp(ext,"wmv") || - !cimg::strcasecmp(ext,"xvid") || - !cimg::strcasecmp(ext,"mpeg")) load_ffmpeg(filename); - else throw CImgIOException("CImg<%s>::load()", - pixel_type()); - } catch (CImgIOException&) { - std::FILE *file = 0; - try { - file = cimg::fopen(filename,"rb"); - } catch (CImgIOException&) { - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load(): Failed to open file '%s'.", - cimg_instance, - filename); - } - - try { - const char *const f_type = cimg::file_type(file,filename); - std::fclose(file); - if (!cimg::strcasecmp(f_type,"pnm")) load_pnm(filename); - else if (!cimg::strcasecmp(f_type,"pfm")) load_pfm(filename); - else if (!cimg::strcasecmp(f_type,"bmp")) load_bmp(filename); - else if (!cimg::strcasecmp(f_type,"jpg")) load_jpeg(filename); - else if (!cimg::strcasecmp(f_type,"pan")) load_pandore(filename); - else if (!cimg::strcasecmp(f_type,"png")) load_png(filename); - else if (!cimg::strcasecmp(f_type,"tif")) load_tiff(filename); - else if (!cimg::strcasecmp(f_type,"inr")) load_inr(filename); - else if (!cimg::strcasecmp(f_type,"dcm")) load_medcon_external(filename); - else throw CImgIOException("CImg<%s>::load()", - pixel_type()); - } catch (CImgIOException&) { - try { - load_other(filename); - } catch (CImgIOException&) { - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load(): Failed to recognize format of file '%s'.", - cimg_instance, - filename); - } - } - } - cimg::exception_mode() = omode; - return *this; - } - - //! Load image from a file \newinstance. - static CImg get_load(const char *const filename) { - return CImg().load(filename); - } - - //! Load image from an ascii file. - /** - \param filename Filename, as a C -string. - **/ - CImg& load_ascii(const char *const filename) { - return _load_ascii(0,filename); - } - - //! Load image from an ascii file \inplace. - static CImg get_load_ascii(const char *const filename) { - return CImg().load_ascii(filename); - } - - //! Load image from an ascii file \overloading. - CImg& load_ascii(std::FILE *const file) { - return _load_ascii(file,0); - } - - //! Loadimage from an ascii file \newinstance. - static CImg get_load_ascii(std::FILE *const file) { - return CImg().load_ascii(file); - } - - CImg& _load_ascii(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_ascii(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - char line[256] = { 0 }; - int err = std::fscanf(nfile,"%255[^\n]",line); - unsigned int dx = 0, dy = 1, dz = 1, dc = 1; - std::sscanf(line,"%u%*c%u%*c%u%*c%u",&dx,&dy,&dz,&dc); - err = std::fscanf(nfile,"%*[^0-9.eE+-]"); - if (!dx || !dy || !dz || !dc) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_ascii(): Invalid ascii header in file '%s', image dimensions are set " - "to (%u,%u,%u,%u).", - cimg_instance, - filename?filename:"(FILE*)",dx,dy,dz,dc); - } - assign(dx,dy,dz,dc); - const unsigned long siz = size(); - unsigned long off = 0; - double val; - T *ptr = _data; - for (err = 1, off = 0; off& load_dlm(const char *const filename) { - return _load_dlm(0,filename); - } - - //! Load image from a DLM file \newinstance. - static CImg get_load_dlm(const char *const filename) { - return CImg().load_dlm(filename); - } - - //! Load image from a DLM file \overloading. - CImg& load_dlm(std::FILE *const file) { - return _load_dlm(file,0); - } - - //! Load image from a DLM file \newinstance. - static CImg get_load_dlm(std::FILE *const file) { - return CImg().load_dlm(file); - } - - CImg& _load_dlm(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_dlm(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"r"); - char delimiter[256] = { 0 }, tmp[256] = { 0 }; - unsigned int cdx = 0, dx = 0, dy = 0; - int err = 0; - double val; - assign(256,256); - while ((err = std::fscanf(nfile,"%lf%255[^0-9.+-]",&val,delimiter))>0) { - if (err>0) (*this)(cdx++,dy) = (T)val; - if (cdx>=_width) resize(3*_width/2,_height,1,1,0); - char c = 0; - if (!std::sscanf(delimiter,"%255[^\n]%c",tmp,&c) || c=='\n') { - dx = cimg::max(cdx,dx); - if (++dy>=_height) resize(_width,3*_height/2,1,1,0); - cdx = 0; - } - } - if (cdx && err==1) { dx = cdx; ++dy; } - if (!dx || !dy) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_dlm(): Invalid DLM file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - resize(dx,dy,1,1,0); - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a BMP file. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_bmp(const char *const filename) { - return _load_bmp(0,filename); - } - - //! Load image from a BMP file \newinstance. - static CImg get_load_bmp(const char *const filename) { - return CImg().load_bmp(filename); - } - - //! Load image from a BMP file \overloading. - CImg& load_bmp(std::FILE *const file) { - return _load_bmp(file,0); - } - - //! Load image from a BMP file \newinstance. - static CImg get_load_bmp(std::FILE *const file) { - return CImg().load_bmp(file); - } - - CImg& _load_bmp(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_bmp(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - unsigned char header[64] = { 0 }; - cimg::fread(header,54,nfile); - if (*header!='B' || header[1]!='M') { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_bmp(): Invalid BMP file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - - // Read header and pixel buffer - int - file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24), - offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24), - header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24), - dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24), - dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24), - compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24), - nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24), - bpp = header[0x1C] + (header[0x1D]<<8); - - if (!file_size || file_size==offset) { - std::fseek(nfile,0,SEEK_END); - file_size = (int)std::ftell(nfile); - std::fseek(nfile,54,SEEK_SET); - } - if (header_size>40) std::fseek(nfile, header_size - 40, SEEK_CUR); - - const int - cimg_iobuffer = 12*1024*1024, - dx_bytes = (bpp==1)?(dx/8+(dx%8?1:0)):((bpp==4)?(dx/2+(dx%2?1:0)):(dx*bpp/8)), - align_bytes = (4-dx_bytes%4)%4, - buf_size = cimg::min(cimg::abs(dy)*(dx_bytes + align_bytes),file_size - offset); - - CImg colormap; - if (bpp<16) { if (!nb_colors) nb_colors = 1<0) std::fseek(nfile,xoffset,SEEK_CUR); - - CImg buffer; - if (buf_size=0; --y) { - if (buf_size>=cimg_iobuffer) { - cimg::fread(ptrs=buffer._data,dx_bytes,nfile); - std::fseek(nfile,align_bytes,SEEK_CUR); - } - unsigned char mask = 0x80, val = 0; - cimg_forX(*this,x) { - if (mask==0x80) val = *(ptrs++); - const unsigned char *col = (unsigned char*)(colormap._data + (val&mask?1:0)); - (*this)(x,y,2) = (T)*(col++); - (*this)(x,y,1) = (T)*(col++); - (*this)(x,y,0) = (T)*(col++); - mask = cimg::ror(mask); - } - ptrs+=align_bytes; - } - } break; - case 4 : { // 16 colors - for (int y = height()-1; y>=0; --y) { - if (buf_size>=cimg_iobuffer) { - cimg::fread(ptrs=buffer._data,dx_bytes,nfile); - std::fseek(nfile,align_bytes,SEEK_CUR); - } - unsigned char mask = 0xF0, val = 0; - cimg_forX(*this,x) { - if (mask==0xF0) val = *(ptrs++); - const unsigned char color = (unsigned char)((mask<16)?(val&mask):((val&mask)>>4)); - const unsigned char *col = (unsigned char*)(colormap._data + color); - (*this)(x,y,2) = (T)*(col++); - (*this)(x,y,1) = (T)*(col++); - (*this)(x,y,0) = (T)*(col++); - mask = cimg::ror(mask,4); - } - ptrs+=align_bytes; - } - } break; - case 8 : { // 256 colors - for (int y = height()-1; y>=0; --y) { - if (buf_size>=cimg_iobuffer) { - cimg::fread(ptrs=buffer._data,dx_bytes,nfile); - std::fseek(nfile,align_bytes,SEEK_CUR); - } - cimg_forX(*this,x) { - const unsigned char *col = (unsigned char*)(colormap._data + *(ptrs++)); - (*this)(x,y,2) = (T)*(col++); - (*this)(x,y,1) = (T)*(col++); - (*this)(x,y,0) = (T)*(col++); - } - ptrs+=align_bytes; - } - } break; - case 16 : { // 16 bits colors - for (int y = height()-1; y>=0; --y) { - if (buf_size>=cimg_iobuffer) { - cimg::fread(ptrs=buffer._data,dx_bytes,nfile); - std::fseek(nfile,align_bytes,SEEK_CUR); - } - cimg_forX(*this,x) { - const unsigned char c1 = *(ptrs++), c2 = *(ptrs++); - const unsigned short col = (unsigned short)(c1|(c2<<8)); - (*this)(x,y,2) = (T)(col&0x1F); - (*this)(x,y,1) = (T)((col>>5)&0x1F); - (*this)(x,y,0) = (T)((col>>10)&0x1F); - } - ptrs+=align_bytes; - } - } break; - case 24 : { // 24 bits colors - for (int y = height()-1; y>=0; --y) { - if (buf_size>=cimg_iobuffer) { - cimg::fread(ptrs=buffer._data,dx_bytes,nfile); - std::fseek(nfile,align_bytes,SEEK_CUR); - } - cimg_forX(*this,x) { - (*this)(x,y,2) = (T)*(ptrs++); - (*this)(x,y,1) = (T)*(ptrs++); - (*this)(x,y,0) = (T)*(ptrs++); - } - ptrs+=align_bytes; - } - } break; - case 32 : { // 32 bits colors - for (int y = height()-1; y>=0; --y) { - if (buf_size>=cimg_iobuffer) { - cimg::fread(ptrs=buffer._data,dx_bytes,nfile); - std::fseek(nfile,align_bytes,SEEK_CUR); - } - cimg_forX(*this,x) { - (*this)(x,y,2) = (T)*(ptrs++); - (*this)(x,y,1) = (T)*(ptrs++); - (*this)(x,y,0) = (T)*(ptrs++); - ++ptrs; - } - ptrs+=align_bytes; - } - } break; - } - if (dy<0) mirror('y'); - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a JPEG file. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_jpeg(const char *const filename) { - return _load_jpeg(0,filename); - } - - //! Load image from a JPEG file \newinstance. - static CImg get_load_jpeg(const char *const filename) { - return CImg().load_jpeg(filename); - } - - //! Load image from a JPEG file \overloading. - CImg& load_jpeg(std::FILE *const file) { - return _load_jpeg(file,0); - } - - //! Load image from a JPEG file \newinstance. - static CImg get_load_jpeg(std::FILE *const file) { - return CImg().load_jpeg(file); - } - - // Custom error handler for libjpeg. -#ifdef cimg_use_jpeg - struct _cimg_error_mgr { - struct jpeg_error_mgr original; - jmp_buf setjmp_buffer; - char message[JMSG_LENGTH_MAX]; - }; - - typedef struct _cimg_error_mgr *_cimg_error_ptr; - - METHODDEF(void) _cimg_jpeg_error_exit(j_common_ptr cinfo) { - _cimg_error_ptr c_err = (_cimg_error_ptr) cinfo->err; // Return control to the setjmp point - (*cinfo->err->format_message)(cinfo,c_err->message); - jpeg_destroy(cinfo); // Clean memory and temp files. - longjmp(c_err->setjmp_buffer,1); - } -#endif - - CImg& _load_jpeg(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_jpeg(): Specified filename is (null).", - cimg_instance); - -#ifndef cimg_use_jpeg - if (file) - throw CImgIOException(_cimg_instance - "load_jpeg(): Unable to load data from '(FILE*)' unless libjpeg is enabled.", - cimg_instance); - else return load_other(filename); -#else - - struct jpeg_decompress_struct cinfo; - struct _cimg_error_mgr jerr; - cinfo.err = jpeg_std_error(&jerr.original); - jerr.original.error_exit = _cimg_jpeg_error_exit; - - if (setjmp(jerr.setjmp_buffer)) { // JPEG error - throw CImgIOException(_cimg_instance - "load_jpeg(): Error message returned by libjpeg: %s.", - cimg_instance,jerr.message); - } - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - jpeg_create_decompress(&cinfo); - jpeg_stdio_src(&cinfo,nfile); - jpeg_read_header(&cinfo,TRUE); - jpeg_start_decompress(&cinfo); - - if (cinfo.output_components!=1 && cinfo.output_components!=3 && cinfo.output_components!=4) { - if (!file) { - cimg::fclose(nfile); - return load_other(filename); - } else - throw CImgIOException(_cimg_instance - "load_jpeg(): Failed to load JPEG data from file '%s'.", - cimg_instance,filename?filename:"(FILE*)"); - } - CImg buffer(cinfo.output_width*cinfo.output_components); - JSAMPROW row_pointer[1]; - assign(cinfo.output_width,cinfo.output_height,1,cinfo.output_components); - T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height, *ptr_b = _data + 2UL*_width*_height, - *ptr_a = _data + 3UL*_width*_height; - while (cinfo.output_scanline - // This is experimental code, not much tested, use with care. - CImg& load_magick(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_magick(): Specified filename is (null).", - cimg_instance); -#ifdef cimg_use_magick - Magick::Image image(filename); - const unsigned int W = image.size().width(), H = image.size().height(); - switch (image.type()) { - case Magick::PaletteMatteType : - case Magick::TrueColorMatteType : - case Magick::ColorSeparationType : { - assign(W,H,1,4); - T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3); - Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); - for (unsigned long off = (unsigned long)W*H; off; --off) { - *(ptr_r++) = (T)(pixels->red); - *(ptr_g++) = (T)(pixels->green); - *(ptr_b++) = (T)(pixels->blue); - *(ptr_a++) = (T)(pixels->opacity); - ++pixels; - } - } break; - case Magick::PaletteType : - case Magick::TrueColorType : { - assign(W,H,1,3); - T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); - Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); - for (unsigned long off = (unsigned long)W*H; off; --off) { - *(ptr_r++) = (T)(pixels->red); - *(ptr_g++) = (T)(pixels->green); - *(ptr_b++) = (T)(pixels->blue); - ++pixels; - } - } break; - case Magick::GrayscaleMatteType : { - assign(W,H,1,2); - T *ptr_r = data(0,0,0,0), *ptr_a = data(0,0,0,1); - Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); - for (unsigned long off = (unsigned long)W*H; off; --off) { - *(ptr_r++) = (T)(pixels->red); - *(ptr_a++) = (T)(pixels->opacity); - ++pixels; - } - } break; - default : { - assign(W,H,1,1); - T *ptr_r = data(0,0,0,0); - Magick::PixelPacket *pixels = image.getPixels(0,0,W,H); - for (unsigned long off = (unsigned long)W*H; off; --off) { - *(ptr_r++) = (T)(pixels->red); - ++pixels; - } - } - } - return *this; -#else - throw CImgIOException(_cimg_instance - "load_magick(): Unable to load file '%s' unless libMagick++ is enabled.", - cimg_instance, - filename); -#endif - } - - //! Load image from a file, using Magick++ library \newinstance. - static CImg get_load_magick(const char *const filename) { - return CImg().load_magick(filename); - } - - //! Load image from a PNG file. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_png(const char *const filename) { - return _load_png(0,filename); - } - - //! Load image from a PNG file \newinstance. - static CImg get_load_png(const char *const filename) { - return CImg().load_png(filename); - } - - //! Load image from a PNG file \overloading. - CImg& load_png(std::FILE *const file) { - return _load_png(file,0); - } - - //! Load image from a PNG file \newinstance. - static CImg get_load_png(std::FILE *const file) { - return CImg().load_png(file); - } - - // (Note: Most of this function has been written by Eric Fausett) - CImg& _load_png(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_png(): Specified filename is (null).", - cimg_instance); - -#ifndef cimg_use_png - if (file) - throw CImgIOException(_cimg_instance - "load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.", - cimg_instance); - - else return load_other(filename); -#else - // Open file and check for PNG validity - const char *volatile nfilename = filename; // two 'volatile' here to remove a g++ warning due to 'setjmp'. - std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb"); - - unsigned char pngCheck[8] = { 0 }; - cimg::fread(pngCheck,8,(std::FILE*)nfile); - if (png_sig_cmp(pngCheck,0,8)) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_png(): Invalid PNG file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - - // Setup PNG structures for read - png_voidp user_error_ptr = 0; - png_error_ptr user_error_fn = 0, user_warning_fn = 0; - png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn); - if (!png_ptr) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_png(): Failed to initialize 'png_ptr' structure for file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - png_infop info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - if (!file) cimg::fclose(nfile); - png_destroy_read_struct(&png_ptr,(png_infopp)0,(png_infopp)0); - throw CImgIOException(_cimg_instance - "load_png(): Failed to initialize 'info_ptr' structure for file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - png_infop end_info = png_create_info_struct(png_ptr); - if (!end_info) { - if (!file) cimg::fclose(nfile); - png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)0); - throw CImgIOException(_cimg_instance - "load_png(): Failed to initialize 'end_info' structure for file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - - // Error handling callback for png file reading - if (setjmp(png_jmpbuf(png_ptr))) { - if (!file) cimg::fclose((std::FILE*)nfile); - png_destroy_read_struct(&png_ptr, &end_info, (png_infopp)0); - throw CImgIOException(_cimg_instance - "load_png(): Encountered unknown fatal error in libpng for file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - png_init_io(png_ptr, nfile); - png_set_sig_bytes(png_ptr, 8); - - // Get PNG Header Info up to data block - png_read_info(png_ptr,info_ptr); - png_uint_32 W, H; - int bit_depth, color_type, interlace_type; - bool is_gray = false; - png_get_IHDR(png_ptr,info_ptr,&W,&H,&bit_depth,&color_type,&interlace_type,(int*)0,(int*)0); - - // Transforms to unify image data - if (color_type==PNG_COLOR_TYPE_PALETTE) { - png_set_palette_to_rgb(png_ptr); - color_type = PNG_COLOR_TYPE_RGB; - bit_depth = 8; - } - if (color_type==PNG_COLOR_TYPE_GRAY && bit_depth<8) { - png_set_expand_gray_1_2_4_to_8(png_ptr); - is_gray = true; - bit_depth = 8; - } - if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) { - png_set_tRNS_to_alpha(png_ptr); - color_type |= PNG_COLOR_MASK_ALPHA; - } - if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) { - png_set_gray_to_rgb(png_ptr); - color_type |= PNG_COLOR_MASK_COLOR; - is_gray = true; - } - if (color_type==PNG_COLOR_TYPE_RGB) - png_set_filler(png_ptr,0xffffU,PNG_FILLER_AFTER); - - png_read_update_info(png_ptr,info_ptr); - if (bit_depth!=8 && bit_depth!=16) { - if (!file) cimg::fclose(nfile); - png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0); - throw CImgIOException(_cimg_instance - "load_png(): Invalid bit depth %u in file '%s'.", - cimg_instance, - bit_depth,nfilename?nfilename:"(FILE*)"); - } - const int byte_depth = bit_depth>>3; - - // Allocate Memory for Image Read - png_bytep *const imgData = new png_bytep[H]; - for (unsigned int row = 0; row& load_pnm(const char *const filename) { - return _load_pnm(0,filename); - } - - //! Load image from a PNM file \newinstance. - static CImg get_load_pnm(const char *const filename) { - return CImg().load_pnm(filename); - } - - //! Load image from a PNM file \overloading. - CImg& load_pnm(std::FILE *const file) { - return _load_pnm(file,0); - } - - //! Load image from a PNM file \newinstance. - static CImg get_load_pnm(std::FILE *const file) { - return CImg().load_pnm(file); - } - - CImg& _load_pnm(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_pnm(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - unsigned int ppm_type, W, H, D = 1, colormax = 255; - CImg item(16384,1,1,1,0); - int err, rval, gval, bval; - const long cimg_iobuffer = 12*1024*1024; - while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); - if (std::sscanf(item," P%u",&ppm_type)!=1) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pnm(): PNM header not found in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); - if ((err=std::sscanf(item," %u %u %u %u",&W,&H,&D,&colormax))<2) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - if (ppm_type!=1 && ppm_type!=4) { - if (err==2 || (err==3 && (ppm_type==5 || ppm_type==7 || ppm_type==8 || ppm_type==9))) { - while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); - if (std::sscanf(item,"%u",&colormax)!=1) - cimg::warn(_cimg_instance - "load_pnm(): COLORMAX field is undefined in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } else { colormax = D; D = 1; } - } - std::fgetc(nfile); - - switch (ppm_type) { - case 1 : { // 2d b&w ascii. - assign(W,H,1,1); - T* ptrd = _data; - cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)(rval?0:255); else break; } - } break; - case 2 : { // 2d grey ascii. - assign(W,H,1,1); - T* ptrd = _data; - cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)rval; else break; } - } break; - case 3 : { // 2d color ascii. - assign(W,H,1,3); - T *ptrd = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); - cimg_forXY(*this,x,y) { - if (std::fscanf(nfile,"%d %d %d",&rval,&gval,&bval)==3) { - *(ptrd++) = (T)rval; *(ptr_g++) = (T)gval; *(ptr_b++) = (T)bval; - } else break; - } - } break; - case 4 : { // 2d b&w binary (support 3D PINK extension). - CImg raw; - assign(W,H,D,1); - T *ptrd = data(0,0,0,0); - unsigned int w = 0, h = 0, d = 0; - for (long to_read = (long)((W/8 + (W%8?1:0))*H*D); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const unsigned char *ptrs = raw._data; - unsigned char mask = 0, val = 0; - for (unsigned long off = (unsigned long)raw._width; off || mask; mask>>=1) { - if (!mask) { if (off--) val = *(ptrs++); mask = 128; } - *(ptrd++) = (T)((val&mask)?0:255); - if (++w==W) { w = 0; mask = 0; if (++h==H) { h = 0; if (++d==D) break; }} - } - } - } break; - case 5 : case 7 : { // 2d/3d grey binary (support 3D PINK extension). - if (colormax<256) { // 8 bits. - CImg raw; - assign(W,H,D,1); - T *ptrd = data(0,0,0,0); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const unsigned char *ptrs = raw._data; - for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); - } - } else { // 16 bits. - CImg raw; - assign(W,H,D,1); - T *ptrd = data(0,0,0,0); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer/2)); - cimg::fread(raw._data,raw._width,nfile); - if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); - to_read-=raw._width; - const unsigned short *ptrs = raw._data; - for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); - } - } - } break; - case 6 : { // 2d color binary. - if (colormax<256) { // 8 bits. - CImg raw; - assign(W,H,1,3); - T - *ptr_r = data(0,0,0,0), - *ptr_g = data(0,0,0,1), - *ptr_b = data(0,0,0,2); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const unsigned char *ptrs = raw._data; - for (unsigned long off = (unsigned long)raw._width/3; off; --off) { - *(ptr_r++) = (T)*(ptrs++); - *(ptr_g++) = (T)*(ptrs++); - *(ptr_b++) = (T)*(ptrs++); - } - } - } else { // 16 bits. - CImg raw; - assign(W,H,1,3); - T - *ptr_r = data(0,0,0,0), - *ptr_g = data(0,0,0,1), - *ptr_b = data(0,0,0,2); - for (long to_read = (int)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer/2)); - cimg::fread(raw._data,raw._width,nfile); - if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); - to_read-=raw._width; - const unsigned short *ptrs = raw._data; - for (unsigned long off = (unsigned long)raw._width/3; off; --off) { - *(ptr_r++) = (T)*(ptrs++); - *(ptr_g++) = (T)*(ptrs++); - *(ptr_b++) = (T)*(ptrs++); - } - } - } - } break; - case 8 : { // 2d/3d grey binary with int32 integers (PINK extension). - CImg raw; - assign(W,H,D,1); - T *ptrd = data(0,0,0,0); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const int *ptrs = raw._data; - for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); - } - } break; - case 9 : { // 2d/3d grey binary with float values (PINK extension). - CImg raw; - assign(W,H,D,1); - T *ptrd = data(0,0,0,0); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const float *ptrs = raw._data; - for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); - } - } break; - default : - assign(); - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pnm(): PNM type 'P%d' found, but type is not supported.", - cimg_instance, - filename?filename:"(FILE*)",ppm_type); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a PFM file. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_pfm(const char *const filename) { - return _load_pfm(0,filename); - } - - //! Load image from a PFM file \newinstance. - static CImg get_load_pfm(const char *const filename) { - return CImg().load_pfm(filename); - } - - //! Load image from a PFM file \overloading. - CImg& load_pfm(std::FILE *const file) { - return _load_pfm(file,0); - } - - //! Load image from a PFM file \newinstance. - static CImg get_load_pfm(std::FILE *const file) { - return CImg().load_pfm(file); - } - - CImg& _load_pfm(std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_pfm(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - char pfm_type; - CImg item(16384,1,1,1,0); - int W = 0, H = 0, err = 0; - double scale = 0; - while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); - if (std::sscanf(item," P%c",&pfm_type)!=1) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pfm(): PFM header not found in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); - if ((err=std::sscanf(item," %d %d",&W,&H))<2) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - if (err==2) { - while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile); - if (std::sscanf(item,"%lf",&scale)!=1) - cimg::warn(_cimg_instance - "load_pfm(): SCALE field is undefined in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - std::fgetc(nfile); - const bool is_color = (pfm_type=='F'), is_inverted = (scale>0)!=cimg::endianness(); - if (is_color) { - assign(W,H,1,3,0); - CImg buf(3*W); - T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); - cimg_forY(*this,y) { - cimg::fread(buf._data,3*W,nfile); - if (is_inverted) cimg::invert_endianness(buf._data,3*W); - const float *ptrs = buf._data; - cimg_forX(*this,x) { - *(ptr_r++) = (T)*(ptrs++); - *(ptr_g++) = (T)*(ptrs++); - *(ptr_b++) = (T)*(ptrs++); - } - } - } else { - assign(W,H,1,1,0); - CImg buf(W); - T *ptrd = data(0,0,0,0); - cimg_forY(*this,y) { - cimg::fread(buf._data,W,nfile); - if (is_inverted) cimg::invert_endianness(buf._data,W); - const float *ptrs = buf._data; - cimg_forX(*this,x) *(ptrd++) = (T)*(ptrs++); - } - } - if (!file) cimg::fclose(nfile); - return mirror('y'); // Most of the .pfm files are flipped along the y-axis. - } - - //! Load image from a RGB file. - /** - \param filename Filename, as a C-string. - \param dimw Width of the image buffer. - \param dimh Height of the image buffer. - **/ - CImg& load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { - return _load_rgb(0,filename,dimw,dimh); - } - - //! Load image from a RGB file \newinstance. - static CImg get_load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { - return CImg().load_rgb(filename,dimw,dimh); - } - - //! Load image from a RGB file \overloading. - CImg& load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { - return _load_rgb(file,0,dimw,dimh); - } - - //! Load image from a RGB file \newinstance. - static CImg get_load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { - return CImg().load_rgb(file,dimw,dimh); - } - - CImg& _load_rgb(std::FILE *const file, const char *const filename, - const unsigned int dimw, const unsigned int dimh) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_rgb(): Specified filename is (null).", - cimg_instance); - - if (!dimw || !dimh) return assign(); - const long cimg_iobuffer = 12*1024*1024; - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - CImg raw; - assign(dimw,dimh,1,3); - T - *ptr_r = data(0,0,0,0), - *ptr_g = data(0,0,0,1), - *ptr_b = data(0,0,0,2); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const unsigned char *ptrs = raw._data; - for (unsigned long off = raw._width/3UL; off; --off) { - *(ptr_r++) = (T)*(ptrs++); - *(ptr_g++) = (T)*(ptrs++); - *(ptr_b++) = (T)*(ptrs++); - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a RGBA file. - /** - \param filename Filename, as a C-string. - \param dimw Width of the image buffer. - \param dimh Height of the image buffer. - **/ - CImg& load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { - return _load_rgba(0,filename,dimw,dimh); - } - - //! Load image from a RGBA file \newinstance. - static CImg get_load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) { - return CImg().load_rgba(filename,dimw,dimh); - } - - //! Load image from a RGBA file \overloading. - CImg& load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { - return _load_rgba(file,0,dimw,dimh); - } - - //! Load image from a RGBA file \newinstance. - static CImg get_load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) { - return CImg().load_rgba(file,dimw,dimh); - } - - CImg& _load_rgba(std::FILE *const file, const char *const filename, - const unsigned int dimw, const unsigned int dimh) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_rgba(): Specified filename is (null).", - cimg_instance); - - if (!dimw || !dimh) return assign(); - const long cimg_iobuffer = 12*1024*1024; - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - CImg raw; - assign(dimw,dimh,1,4); - T - *ptr_r = data(0,0,0,0), - *ptr_g = data(0,0,0,1), - *ptr_b = data(0,0,0,2), - *ptr_a = data(0,0,0,3); - for (long to_read = (long)size(); to_read>0; ) { - raw.assign(cimg::min(to_read,cimg_iobuffer)); - cimg::fread(raw._data,raw._width,nfile); - to_read-=raw._width; - const unsigned char *ptrs = raw._data; - for (unsigned long off = raw._width/4UL; off; --off) { - *(ptr_r++) = (T)*(ptrs++); - *(ptr_g++) = (T)*(ptrs++); - *(ptr_b++) = (T)*(ptrs++); - *(ptr_a++) = (T)*(ptrs++); - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a TIFF file. - /** - \param filename Filename, as a C-string. - \param first_frame First frame to read (for multi-pages tiff). - \param last_frame Last frame to read (for multi-pages tiff). - \param step_frame Step value of frame reading. - \note - - libtiff support is enabled by defining the precompilation - directive \c cimg_use_tif. - - When libtiff is enabled, 2D and 3D (multipage) several - channel per pixel are supported for - char,uchar,short,ushort,float and \c double pixel types. - - If \c cimg_use_tif is not defined at compilation time the - function uses CImg& load_other(const char*). - **/ - CImg& load_tiff(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, - float *const voxel_size=0) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_tiff(): Specified filename is (null).", - cimg_instance); - - const unsigned int - nfirst_frame = first_frame1) - throw CImgArgumentException(_cimg_instance - "load_tiff(): Unable to read sub-images from file '%s' unless libtiff is enabled.", - cimg_instance, - filename); - return load_other(filename); -#else - TIFF *tif = TIFFOpen(filename,"r"); - if (tif) { - unsigned int nb_images = 0; - do ++nb_images; while (TIFFReadDirectory(tif)); - if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images)) - cimg::warn(_cimg_instance - "load_tiff(): File '%s' contains %u image(s) while specified frame range is [%u,%u] (step %u).", - cimg_instance, - filename,nb_images,nfirst_frame,nlast_frame,nstep_frame); - - if (nfirst_frame>=nb_images) return assign(); - if (nlast_frame>=nb_images) nlast_frame = nb_images-1; - TIFFSetDirectory(tif,0); - CImg frame; - for (unsigned int l = nfirst_frame; l<=nlast_frame; l+=nstep_frame) { - frame._load_tiff(tif,l,voxel_size); - if (l==nfirst_frame) - assign(frame._width,frame._height,1+(nlast_frame-nfirst_frame)/nstep_frame,frame._spectrum); - if (frame._width>_width || frame._height>_height || frame._spectrum>_spectrum) - resize(cimg::max(frame._width,_width), - cimg::max(frame._height,_height),-100, - cimg::max(frame._spectrum,_spectrum),0); - draw_image(0,0,(l-nfirst_frame)/nstep_frame,frame); - } - TIFFClose(tif); - } else throw CImgIOException(_cimg_instance - "load_tiff(): Failed to open file '%s'.", - cimg_instance, - filename); - return *this; -#endif - } - - //! Load image from a TIFF file \newinstance. - static CImg get_load_tiff(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, - float *const voxel_size=0) { - return CImg().load_tiff(filename,first_frame,last_frame,step_frame,voxel_size); - } - - // (Original contribution by Jerome Boulanger). -#ifdef cimg_use_tiff - template - void _load_tiff_tiled_contig(TIFF *const tif, const uint16 samplesperpixel, - const uint32 nx, const uint32 ny, const uint32 tw, const uint32 th) { - t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif)); - if (buf) { - for (unsigned int row = 0; row - void _load_tiff_tiled_separate(TIFF *const tif, const uint16 samplesperpixel, - const uint32 nx, const uint32 ny, const uint32 tw, const uint32 th) { - t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif)); - if (buf) { - for (unsigned int vv = 0; vv - void _load_tiff_contig(TIFF *const tif, const uint16 samplesperpixel, const uint32 nx, const uint32 ny) { - t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif)); - if (buf) { - uint32 row, rowsperstrip = (uint32)-1; - TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip); - for (row = 0; rowny?ny-row:rowsperstrip); - tstrip_t strip = TIFFComputeStrip(tif, row, 0); - if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) { - _TIFFfree(buf); TIFFClose(tif); - throw CImgIOException(_cimg_instance - "load_tiff(): Invalid strip in file '%s'.", - cimg_instance, - TIFFFileName(tif)); - } - const t *ptr = buf; - for (unsigned int rr = 0; rr - void _load_tiff_separate(TIFF *const tif, const uint16 samplesperpixel, const uint32 nx, const uint32 ny) { - t *buf = (t*)_TIFFmalloc(TIFFStripSize(tif)); - if (buf) { - uint32 row, rowsperstrip = (uint32)-1; - TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip); - for (unsigned int vv = 0; vvny?ny-row:rowsperstrip); - tstrip_t strip = TIFFComputeStrip(tif, row, vv); - if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) { - _TIFFfree(buf); TIFFClose(tif); - throw CImgIOException(_cimg_instance - "load_tiff(): Invalid strip in file '%s'.", - cimg_instance, - TIFFFileName(tif)); - } - const t *ptr = buf; - for (unsigned int rr = 0;rr& _load_tiff(TIFF *const tif, const unsigned int directory, float *const voxel_size) { - if (!TIFFSetDirectory(tif,directory)) return assign(); - uint16 samplesperpixel = 1, bitspersample = 8, photo = 0; - uint16 sampleformat = 1; - uint32 nx = 1, ny = 1; - const char *const filename = TIFFFileName(tif); - const bool is_spp = (bool)TIFFGetField(tif,TIFFTAG_SAMPLESPERPIXEL,&samplesperpixel); - TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&nx); - TIFFGetField(tif,TIFFTAG_IMAGELENGTH,&ny); - TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleformat); - TIFFGetFieldDefaulted(tif,TIFFTAG_BITSPERSAMPLE,&bitspersample); - TIFFGetField(tif,TIFFTAG_PHOTOMETRIC,&photo); - if (voxel_size) { - const char *s_description = 0; - float vx = 0, vy = 0, vz = 0; - if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description) { - const char *s_desc = std::strstr(s_description,"VX="); - if (s_desc && std::sscanf(s_desc,"VX=%f VY=%f VZ=%f",&vx,&vy,&vz)==3) { // CImg format. - voxel_size[0] = vx; voxel_size[1] = vy; voxel_size[2] = vz; - } - s_desc = std::strstr(s_description,"spacing="); - if (s_desc && std::sscanf(s_desc,"spacing=%f",&vz)==1) { // fiji format. - voxel_size[2] = vz; - } - } - TIFFGetField(tif,TIFFTAG_XRESOLUTION,voxel_size); - TIFFGetField(tif,TIFFTAG_YRESOLUTION,voxel_size+1); - } - const unsigned int spectrum = is_spp?samplesperpixel:photo==3?3:1; - assign(nx,ny,1,spectrum); - - if (photo>=3 && sampleformat==1 && bitspersample==8 && (samplesperpixel==3 || samplesperpixel==4)) { - // Special case for unsigned color images. - uint32 *const raster = (uint32*)_TIFFmalloc(nx*ny*sizeof(uint32)); - if (!raster) { - _TIFFfree(raster); TIFFClose(tif); - throw CImgException(_cimg_instance - "load_tiff(): Failed to allocate memory (%s) for file '%s'.", - cimg_instance, - cimg::strbuffersize(nx*ny*sizeof(uint32)),filename); - } - TIFFReadRGBAImage(tif,nx,ny,raster,0); - switch (spectrum) { - case 1 : { - cimg_forXY(*this,x,y) (*this)(x,y) = (T)(float)((raster[nx*(ny-1-y)+x] + 128)/257); - } break; - case 3 : { - cimg_forXY(*this,x,y) { - (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny-1-y)+x]); - (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny-1-y)+x]); - (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny-1-y)+x]); - } - } break; - case 4 : { - cimg_forXY(*this,x,y) { - (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny-1-y)+x]); - (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny-1-y)+x]); - (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny-1-y)+x]); - (*this)(x,y,3) = (T)(float)TIFFGetA(raster[nx*(ny-1-y)+x]); - } - } break; - } - _TIFFfree(raster); - } else { // Other cases. - uint16 config; - TIFFGetField(tif,TIFFTAG_PLANARCONFIG,&config); - if (TIFFIsTiled(tif)) { - uint32 tw = 1, th = 1; - TIFFGetField(tif,TIFFTAG_TILEWIDTH,&tw); - TIFFGetField(tif,TIFFTAG_TILELENGTH,&th); - if (config==PLANARCONFIG_CONTIG) switch (bitspersample) { - case 8 : { - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - else _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - } break; - case 16 : - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - else _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - break; - case 32 : - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - else if (sampleformat==SAMPLEFORMAT_INT) - _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - else _load_tiff_tiled_contig(tif,samplesperpixel,nx,ny,tw,th); - break; - } else switch (bitspersample) { - case 8 : - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - else _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - break; - case 16 : - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - else _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - break; - case 32 : - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - else if (sampleformat==SAMPLEFORMAT_INT) - _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - else _load_tiff_tiled_separate(tif,samplesperpixel,nx,ny,tw,th); - break; - } - } else { - if (config==PLANARCONFIG_CONTIG) switch (bitspersample) { - case 8 : - if (sampleformat==SAMPLEFORMAT_UINT) - _load_tiff_contig(tif,samplesperpixel,nx,ny); - else _load_tiff_contig(tif,samplesperpixel,nx,ny); - break; - case 16 : - if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig(tif,samplesperpixel,nx,ny); - else _load_tiff_contig(tif,samplesperpixel,nx,ny); - break; - case 32 : - if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig(tif,samplesperpixel,nx,ny); - else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_contig(tif,samplesperpixel,nx,ny); - else _load_tiff_contig(tif,samplesperpixel,nx,ny); - break; - } else switch (bitspersample) { - case 8 : - if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate(tif,samplesperpixel,nx,ny); - else _load_tiff_separate(tif,samplesperpixel,nx,ny); - break; - case 16 : - if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate(tif,samplesperpixel,nx,ny); - else _load_tiff_separate(tif,samplesperpixel,nx,ny); - break; - case 32 : - if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate(tif,samplesperpixel,nx,ny); - else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_separate(tif,samplesperpixel,nx,ny); - else _load_tiff_separate(tif,samplesperpixel,nx,ny); - break; - } - } - } - return *this; - } -#endif - - //! Load image from a MINC2 file. - /** - \param filename Filename, as a C-string. - **/ - // (Original code by Haz-Edine Assemlal). - CImg& load_minc2(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_minc2(): Specified filename is (null).", - cimg_instance); -#ifndef cimg_use_minc2 - return load_other(filename); -#else - minc::minc_1_reader rdr; - rdr.open(filename); - assign(rdr.ndim(1)?rdr.ndim(1):1, - rdr.ndim(2)?rdr.ndim(2):1, - rdr.ndim(3)?rdr.ndim(3):1, - rdr.ndim(4)?rdr.ndim(4):1); - if(typeid(T)==typeid(unsigned char)) - rdr.setup_read_byte(); - else if(typeid(T)==typeid(int)) - rdr.setup_read_int(); - else if(typeid(T)==typeid(double)) - rdr.setup_read_double(); - else - rdr.setup_read_float(); - minc::load_standard_volume(rdr, this->_data); - return *this; -#endif - } - - //! Load image from a MINC2 file \newinstance. - static CImg get_load_minc2(const char *const filename) { - return CImg().load_analyze(filename); - } - - //! Load image from an ANALYZE7.5/NIFTI file. - /** - \param filename Filename, as a C-string. - \param[out] voxel_size Pointer to the three voxel sizes read from the file. - **/ - CImg& load_analyze(const char *const filename, float *const voxel_size=0) { - return _load_analyze(0,filename,voxel_size); - } - - //! Load image from an ANALYZE7.5/NIFTI file \newinstance. - static CImg get_load_analyze(const char *const filename, float *const voxel_size=0) { - return CImg().load_analyze(filename,voxel_size); - } - - //! Load image from an ANALYZE7.5/NIFTI file \overloading. - CImg& load_analyze(std::FILE *const file, float *const voxel_size=0) { - return _load_analyze(file,0,voxel_size); - } - - //! Load image from an ANALYZE7.5/NIFTI file \newinstance. - static CImg get_load_analyze(std::FILE *const file, float *const voxel_size=0) { - return CImg().load_analyze(file,voxel_size); - } - - CImg& _load_analyze(std::FILE *const file, const char *const filename, float *const voxel_size=0) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_analyze(): Specified filename is (null).", - cimg_instance); - - std::FILE *nfile_header = 0, *nfile = 0; - if (!file) { - char body[1024] = { 0 }; - const char *const ext = cimg::split_filename(filename,body); - if (!cimg::strcasecmp(ext,"hdr")) { // File is an Analyze header file. - nfile_header = cimg::fopen(filename,"rb"); - std::sprintf(body + std::strlen(body),".img"); - nfile = cimg::fopen(body,"rb"); - } else if (!cimg::strcasecmp(ext,"img")) { // File is an Analyze data file. - nfile = cimg::fopen(filename,"rb"); - std::sprintf(body + std::strlen(body),".hdr"); - nfile_header = cimg::fopen(body,"rb"); - } else nfile_header = nfile = cimg::fopen(filename,"rb"); // File is a Niftii file. - } else nfile_header = nfile = file; // File is a Niftii file. - if (!nfile || !nfile_header) - throw CImgIOException(_cimg_instance - "load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - // Read header. - bool endian = false; - unsigned int header_size; - cimg::fread(&header_size,1,nfile_header); - if (!header_size) - throw CImgIOException(_cimg_instance - "load_analyze(): Invalid zero-sized header in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - if (header_size>=4096) { endian = true; cimg::invert_endianness(header_size); } - unsigned char *const header = new unsigned char[header_size]; - cimg::fread(header+4,header_size-4,nfile_header); - if (!file && nfile_header!=nfile) cimg::fclose(nfile_header); - if (endian) { - cimg::invert_endianness((short*)(header+40),5); - cimg::invert_endianness((short*)(header+70),1); - cimg::invert_endianness((short*)(header+72),1); - cimg::invert_endianness((float*)(header+76),4); - cimg::invert_endianness((float*)(header+112),1); - } - unsigned short *dim = (unsigned short*)(header+40), dimx = 1, dimy = 1, dimz = 1, dimv = 1; - if (!dim[0]) - cimg::warn(_cimg_instance - "load_analyze(): File '%s' defines an image with zero dimensions.", - cimg_instance, - filename?filename:"(FILE*)"); - - if (dim[0]>4) - cimg::warn(_cimg_instance - "load_analyze(): File '%s' defines an image with %u dimensions, reading only the 4 first.", - cimg_instance, - filename?filename:"(FILE*)",dim[0]); - - if (dim[0]>=1) dimx = dim[1]; - if (dim[0]>=2) dimy = dim[2]; - if (dim[0]>=3) dimz = dim[3]; - if (dim[0]>=4) dimv = dim[4]; - float scalefactor = *(float*)(header+112); if (scalefactor==0) scalefactor=1; - const unsigned short datatype = *(short*)(header+70); - if (voxel_size) { - const float *vsize = (float*)(header+76); - voxel_size[0] = vsize[1]; voxel_size[1] = vsize[2]; voxel_size[2] = vsize[3]; - } - delete[] header; - - // Read pixel data. - assign(dimx,dimy,dimz,dimv); - switch (datatype) { - case 2 : { - unsigned char *const buffer = new unsigned char[dimx*dimy*dimz*dimv]; - cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); - cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); - delete[] buffer; - } break; - case 4 : { - short *const buffer = new short[dimx*dimy*dimz*dimv]; - cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); - if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); - cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); - delete[] buffer; - } break; - case 8 : { - int *const buffer = new int[dimx*dimy*dimz*dimv]; - cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); - if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); - cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); - delete[] buffer; - } break; - case 16 : { - float *const buffer = new float[dimx*dimy*dimz*dimv]; - cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); - if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); - cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); - delete[] buffer; - } break; - case 64 : { - double *const buffer = new double[dimx*dimy*dimz*dimv]; - cimg::fread(buffer,dimx*dimy*dimz*dimv,nfile); - if (endian) cimg::invert_endianness(buffer,dimx*dimy*dimz*dimv); - cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor); - delete[] buffer; - } break; - default : - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_analyze(): Unable to load datatype %d in file '%s'", - cimg_instance, - datatype,filename?filename:"(FILE*)"); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a .cimg[z] file. - /** - \param filename Filename, as a C-string. - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg& load_cimg(const char *const filename, const char axis='z', const float align=0) { - CImgList list; - list.load_cimg(filename); - if (list._width==1) return list[0].move_to(*this); - return assign(list.get_append(axis,align)); - } - - //! Load image from a .cimg[z] file \newinstance - static CImg get_load_cimg(const char *const filename, const char axis='z', const float align=0) { - return CImg().load_cimg(filename,axis,align); - } - - //! Load image from a .cimg[z] file \overloading. - CImg& load_cimg(std::FILE *const file, const char axis='z', const float align=0) { - CImgList list; - list.load_cimg(file); - if (list._width==1) return list[0].move_to(*this); - return assign(list.get_append(axis,align)); - } - - //! Load image from a .cimg[z] file \newinstance - static CImg get_load_cimg(std::FILE *const file, const char axis='z', const float align=0) { - return CImg().load_cimg(file,axis,align); - } - - //! Load sub-images of a .cimg file. - /** - \param filename Filename, as a C-string. - \param n0 Starting frame. - \param n1 Ending frame (~0U for max). - \param x0 X-coordinate of the starting sub-image vertex. - \param y0 Y-coordinate of the starting sub-image vertex. - \param z0 Z-coordinate of the starting sub-image vertex. - \param c0 C-coordinate of the starting sub-image vertex. - \param x1 X-coordinate of the ending sub-image vertex (~0U for max). - \param y1 Y-coordinate of the ending sub-image vertex (~0U for max). - \param z1 Z-coordinate of the ending sub-image vertex (~0U for max). - \param c1 C-coordinate of the ending sub-image vertex (~0U for max). - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg& load_cimg(const char *const filename, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1, - const char axis='z', const float align=0) { - CImgList list; - list.load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); - if (list._width==1) return list[0].move_to(*this); - return assign(list.get_append(axis,align)); - } - - //! Load sub-images of a .cimg file \newinstance. - static CImg get_load_cimg(const char *const filename, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1, - const char axis='z', const float align=0) { - return CImg().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align); - } - - //! Load sub-images of a .cimg file \overloading. - CImg& load_cimg(std::FILE *const file, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1, - const char axis='z', const float align=0) { - CImgList list; - list.load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); - if (list._width==1) return list[0].move_to(*this); - return assign(list.get_append(axis,align)); - } - - //! Load sub-images of a .cimg file \newinstance. - static CImg get_load_cimg(std::FILE *const file, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1, - const char axis='z', const float align=0) { - return CImg().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align); - } - - //! Load image from an INRIMAGE-4 file. - /** - \param filename Filename, as a C-string. - \param[out] voxel_size Pointer to the three voxel sizes read from the file. - **/ - CImg& load_inr(const char *const filename, float *const voxel_size=0) { - return _load_inr(0,filename,voxel_size); - } - - //! Load image from an INRIMAGE-4 file \newinstance. - static CImg get_load_inr(const char *const filename, float *const voxel_size=0) { - return CImg().load_inr(filename,voxel_size); - } - - //! Load image from an INRIMAGE-4 file \overloading. - CImg& load_inr(std::FILE *const file, float *const voxel_size=0) { - return _load_inr(file,0,voxel_size); - } - - //! Load image from an INRIMAGE-4 file \newinstance. - static CImg get_load_inr(std::FILE *const file, float *voxel_size=0) { - return CImg().load_inr(file,voxel_size); - } - - static void _load_inr_header(std::FILE *file, int out[8], float *const voxel_size) { - char item[1024] = { 0 }, tmp1[64] = { 0 }, tmp2[64] = { 0 }; - out[0] = std::fscanf(file,"%63s",item); - out[0] = out[1] = out[2] = out[3] = out[5] = 1; out[4] = out[6] = out[7] = -1; - if(cimg::strncasecmp(item,"#INRIMAGE-4#{",13)!=0) - throw CImgIOException("CImg<%s>::load_inr(): INRIMAGE-4 header not found.", - pixel_type()); - - while (std::fscanf(file," %63[^\n]%*c",item)!=EOF && std::strncmp(item,"##}",3)) { - std::sscanf(item," XDIM%*[^0-9]%d",out); - std::sscanf(item," YDIM%*[^0-9]%d",out+1); - std::sscanf(item," ZDIM%*[^0-9]%d",out+2); - std::sscanf(item," VDIM%*[^0-9]%d",out+3); - std::sscanf(item," PIXSIZE%*[^0-9]%d",out+6); - if (voxel_size) { - std::sscanf(item," VX%*[^0-9.+-]%f",voxel_size); - std::sscanf(item," VY%*[^0-9.+-]%f",voxel_size+1); - std::sscanf(item," VZ%*[^0-9.+-]%f",voxel_size+2); - } - if (std::sscanf(item," CPU%*[ =]%s",tmp1)) out[7]=cimg::strncasecmp(tmp1,"sun",3)?0:1; - switch (std::sscanf(item," TYPE%*[ =]%s %s",tmp1,tmp2)) { - case 0 : break; - case 2 : out[5] = cimg::strncasecmp(tmp1,"unsigned",8)?1:0; std::strncpy(tmp1,tmp2,sizeof(tmp1)-1); - case 1 : - if (!cimg::strncasecmp(tmp1,"int",3) || !cimg::strncasecmp(tmp1,"fixed",5)) out[4] = 0; - if (!cimg::strncasecmp(tmp1,"float",5) || !cimg::strncasecmp(tmp1,"double",6)) out[4] = 1; - if (!cimg::strncasecmp(tmp1,"packed",6)) out[4] = 2; - if (out[4]>=0) break; - default : - throw CImgIOException("CImg<%s>::load_inr(): Invalid pixel type '%s' defined in header.", - pixel_type(), - tmp2); - } - } - if(out[0]<0 || out[1]<0 || out[2]<0 || out[3]<0) - throw CImgIOException("CImg<%s>::load_inr(): Invalid dimensions (%d,%d,%d,%d) defined in header.", - pixel_type(), - out[0],out[1],out[2],out[3]); - if(out[4]<0 || out[5]<0) - throw CImgIOException("CImg<%s>::load_inr(): Incomplete pixel type defined in header.", - pixel_type()); - if(out[6]<0) - throw CImgIOException("CImg<%s>::load_inr(): Incomplete PIXSIZE field defined in header.", - pixel_type()); - if(out[7]<0) - throw CImgIOException("CImg<%s>::load_inr(): Big/Little Endian coding type undefined in header.", - pixel_type()); - } - - CImg& _load_inr(std::FILE *const file, const char *const filename, float *const voxel_size) { -#define _cimg_load_inr_case(Tf,sign,pixsize,Ts) \ - if (!loaded && fopt[6]==pixsize && fopt[4]==Tf && fopt[5]==sign) { \ - Ts *xval, *const val = new Ts[fopt[0]*fopt[3]]; \ - cimg_forYZ(*this,y,z) { \ - cimg::fread(val,fopt[0]*fopt[3],nfile); \ - if (fopt[7]!=endian) cimg::invert_endianness(val,fopt[0]*fopt[3]); \ - xval = val; cimg_forX(*this,x) cimg_forC(*this,c) (*this)(x,y,z,c) = (T)*(xval++); \ - } \ - delete[] val; \ - loaded = true; \ - } - - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_inr(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - int fopt[8], endian=cimg::endianness()?1:0; - bool loaded = false; - if (voxel_size) voxel_size[0] = voxel_size[1] = voxel_size[2] = 1; - _load_inr_header(nfile,fopt,voxel_size); - assign(fopt[0],fopt[1],fopt[2],fopt[3]); - _cimg_load_inr_case(0,0,8,unsigned char); - _cimg_load_inr_case(0,1,8,char); - _cimg_load_inr_case(0,0,16,unsigned short); - _cimg_load_inr_case(0,1,16,short); - _cimg_load_inr_case(0,0,32,unsigned int); - _cimg_load_inr_case(0,1,32,int); - _cimg_load_inr_case(1,0,32,float); - _cimg_load_inr_case(1,1,32,float); - _cimg_load_inr_case(1,0,64,double); - _cimg_load_inr_case(1,1,64,double); - if (!loaded) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_inr(): Unknown pixel type defined in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a EXR file. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_exr(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_exr(): Specified filename is (null).", - cimg_instance); - -#ifndef cimg_use_openexr - return load_other(filename); -#else - Imf::RgbaInputFile file(filename); - Imath::Box2i dw = file.dataWindow(); - const int - inwidth = dw.max.x - dw.min.x + 1, - inheight = dw.max.y - dw.min.y + 1; - Imf::Array2D pixels; - pixels.resizeErase(inheight,inwidth); - file.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y*inwidth, 1, inwidth); - file.readPixels(dw.min.y, dw.max.y); - assign(inwidth,inheight,1,4); - T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3); - cimg_forXY(*this,x,y) { - *(ptr_r++) = (T)pixels[y][x].r; - *(ptr_g++) = (T)pixels[y][x].g; - *(ptr_b++) = (T)pixels[y][x].b; - *(ptr_a++) = (T)pixels[y][x].a; - } - return *this; -#endif - } - - //! Load image from a EXR file \newinstance. - static CImg get_load_exr(const char *const filename) { - return CImg().load_exr(filename); - } - - //! Load image from a PANDORE-5 file. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_pandore(const char *const filename) { - return _load_pandore(0,filename); - } - - //! Load image from a PANDORE-5 file \newinstance. - static CImg get_load_pandore(const char *const filename) { - return CImg().load_pandore(filename); - } - - //! Load image from a PANDORE-5 file \overloading. - CImg& load_pandore(std::FILE *const file) { - return _load_pandore(file,0); - } - - //! Load image from a PANDORE-5 file \newinstance. - static CImg get_load_pandore(std::FILE *const file) { - return CImg().load_pandore(file); - } - - CImg& _load_pandore(std::FILE *const file, const char *const filename) { -#define __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,ndim,stype) \ - cimg::fread(dims,nbdim,nfile); \ - if (endian) cimg::invert_endianness(dims,nbdim); \ - assign(nwidth,nheight,ndepth,ndim); \ - const unsigned int siz = size(); \ - stype *buffer = new stype[siz]; \ - cimg::fread(buffer,siz,nfile); \ - if (endian) cimg::invert_endianness(buffer,siz); \ - T *ptrd = _data; \ - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); \ - buffer-=siz; \ - delete[] buffer - -#define _cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1,stype2,stype3,ltype) { \ - if (sizeof(stype1)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1); } \ - else if (sizeof(stype2)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype2); } \ - else if (sizeof(stype3)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype3); } \ - else throw CImgIOException(_cimg_instance \ - "load_pandore(): Unknown pixel datatype in file '%s'.", \ - cimg_instance, \ - filename?filename:"(FILE*)"); } - - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_pandore(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - char header[32] = { 0 }; - cimg::fread(header,12,nfile); - if (cimg::strncasecmp("PANDORE",header,7)) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pandore(): PANDORE header not found in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - unsigned int imageid, dims[8] = { 0 }; - cimg::fread(&imageid,1,nfile); - const bool endian = (imageid>255); - if (endian) cimg::invert_endianness(imageid); - cimg::fread(header,20,nfile); - - switch (imageid) { - case 2: _cimg_load_pandore_case(2,dims[1],1,1,1,unsigned char,unsigned char,unsigned char,1); break; - case 3: _cimg_load_pandore_case(2,dims[1],1,1,1,long,int,short,4); break; - case 4: _cimg_load_pandore_case(2,dims[1],1,1,1,double,float,float,4); break; - case 5: _cimg_load_pandore_case(3,dims[2],dims[1],1,1,unsigned char,unsigned char,unsigned char,1); break; - case 6: _cimg_load_pandore_case(3,dims[2],dims[1],1,1,long,int,short,4); break; - case 7: _cimg_load_pandore_case(3,dims[2],dims[1],1,1,double,float,float,4); break; - case 8: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,unsigned char,unsigned char,unsigned char,1); break; - case 9: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,long,int,short,4); break; - case 10: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,double,float,float,4); break; - case 11 : { // Region 1d - cimg::fread(dims,3,nfile); - if (endian) cimg::invert_endianness(dims,3); - assign(dims[1],1,1,1); - const unsigned siz = size(); - if (dims[2]<256) { - unsigned char *buffer = new unsigned char[siz]; - cimg::fread(buffer,siz,nfile); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } else { - if (dims[2]<65536) { - unsigned short *buffer = new unsigned short[siz]; - cimg::fread(buffer,siz,nfile); - if (endian) cimg::invert_endianness(buffer,siz); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } else { - unsigned int *buffer = new unsigned int[siz]; - cimg::fread(buffer,siz,nfile); - if (endian) cimg::invert_endianness(buffer,siz); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } - } - } - break; - case 12 : { // Region 2d - cimg::fread(dims,4,nfile); - if (endian) cimg::invert_endianness(dims,4); - assign(dims[2],dims[1],1,1); - const unsigned int siz = size(); - if (dims[3]<256) { - unsigned char *buffer = new unsigned char[siz]; - cimg::fread(buffer,siz,nfile); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } else { - if (dims[3]<65536) { - unsigned short *buffer = new unsigned short[siz]; - cimg::fread(buffer,siz,nfile); - if (endian) cimg::invert_endianness(buffer,siz); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } else { - unsigned long *buffer = new unsigned long[siz]; - cimg::fread(buffer,siz,nfile); - if (endian) cimg::invert_endianness(buffer,siz); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } - } - } - break; - case 13 : { // Region 3d - cimg::fread(dims,5,nfile); - if (endian) cimg::invert_endianness(dims,5); - assign(dims[3],dims[2],dims[1],1); - const unsigned int siz = size(); - if (dims[4]<256) { - unsigned char *buffer = new unsigned char[siz]; - cimg::fread(buffer,siz,nfile); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } else { - if (dims[4]<65536) { - unsigned short *buffer = new unsigned short[siz]; - cimg::fread(buffer,siz,nfile); - if (endian) cimg::invert_endianness(buffer,siz); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } else { - unsigned int *buffer = new unsigned int[siz]; - cimg::fread(buffer,siz,nfile); - if (endian) cimg::invert_endianness(buffer,siz); - T *ptrd = _data; - cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); - buffer-=siz; - delete[] buffer; - } - } - } - break; - case 16: _cimg_load_pandore_case(4,dims[2],dims[1],1,3,unsigned char,unsigned char,unsigned char,1); break; - case 17: _cimg_load_pandore_case(4,dims[2],dims[1],1,3,long,int,short,4); break; - case 18: _cimg_load_pandore_case(4,dims[2],dims[1],1,3,double,float,float,4); break; - case 19: _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,unsigned char,unsigned char,unsigned char,1); break; - case 20: _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,long,int,short,4); break; - case 21: _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,double,float,float,4); break; - case 22: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned char,unsigned char,unsigned char,1); break; - case 23: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],long,int,short,4); - case 24: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned long,unsigned int,unsigned short,4); break; - case 25: _cimg_load_pandore_case(2,dims[1],1,1,dims[0],double,float,float,4); break; - case 26: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned char,unsigned char,unsigned char,1); break; - case 27: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],long,int,short,4); break; - case 28: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned long,unsigned int,unsigned short,4); break; - case 29: _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],double,float,float,4); break; - case 30: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned char,unsigned char,unsigned char,1); - break; - case 31: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],long,int,short,4); break; - case 32: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned long,unsigned int,unsigned short,4); - break; - case 33: _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],double,float,float,4); break; - case 34 : { // Points 1d - int ptbuf[4] = { 0 }; - cimg::fread(ptbuf,1,nfile); - if (endian) cimg::invert_endianness(ptbuf,1); - assign(1); (*this)(0) = (T)ptbuf[0]; - } break; - case 35 : { // Points 2d - int ptbuf[4] = { 0 }; - cimg::fread(ptbuf,2,nfile); - if (endian) cimg::invert_endianness(ptbuf,2); - assign(2); (*this)(0) = (T)ptbuf[1]; (*this)(1) = (T)ptbuf[0]; - } break; - case 36 : { // Points 3d - int ptbuf[4] = { 0 }; - cimg::fread(ptbuf,3,nfile); - if (endian) cimg::invert_endianness(ptbuf,3); - assign(3); (*this)(0) = (T)ptbuf[2]; (*this)(1) = (T)ptbuf[1]; (*this)(2) = (T)ptbuf[0]; - } break; - default : - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_pandore(): Unable to load data with ID_type %u in file '%s'.", - cimg_instance, - imageid,filename?filename:"(FILE*)"); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image from a PAR-REC (Philips) file. - /** - \param filename Filename, as a C-string. - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg& load_parrec(const char *const filename, const char axis='c', const float align=0) { - CImgList list; - list.load_parrec(filename); - if (list._width==1) return list[0].move_to(*this); - return assign(list.get_append(axis,align)); - } - - //! Load image from a PAR-REC (Philips) file \newinstance. - static CImg get_load_parrec(const char *const filename, const char axis='c', const float align=0) { - return CImg().load_parrec(filename,axis,align); - } - - //! Load image from a raw binary file. - /** - \param filename Filename, as a C-string. - \param size_x Width of the image buffer. - \param size_y Height of the image buffer. - \param size_z Depth of the image buffer. - \param size_c Spectrum of the image buffer. - \param is_multiplexed Tells if the image values are multiplexed along the C-axis. - \param invert_endianness Tells if the endianness of the image buffer must be inverted. - \param offset Starting offset of the read in the specified file. - **/ - CImg& load_raw(const char *const filename, - const unsigned int size_x=0, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1, - const bool is_multiplexed=false, const bool invert_endianness=false, - const unsigned long offset=0) { - return _load_raw(0,filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); - } - - //! Load image from a raw binary file \newinstance. - static CImg get_load_raw(const char *const filename, - const unsigned int size_x=0, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1, - const bool is_multiplexed=false, const bool invert_endianness=false, - const unsigned long offset=0) { - return CImg().load_raw(filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); - } - - //! Load image from a raw binary file \overloading. - CImg& load_raw(std::FILE *const file, - const unsigned int size_x=0, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1, - const bool is_multiplexed=false, const bool invert_endianness=false, - const unsigned long offset=0) { - return _load_raw(file,0,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); - } - - //! Load image from a raw binary file \newinstance. - static CImg get_load_raw(std::FILE *const file, - const unsigned int size_x=0, const unsigned int size_y=1, - const unsigned int size_z=1, const unsigned int size_c=1, - const bool is_multiplexed=false, const bool invert_endianness=false, - const unsigned long offset=0) { - return CImg().load_raw(file,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset); - } - - CImg& _load_raw(std::FILE *const file, const char *const filename, - const unsigned int size_x, const unsigned int size_y, - const unsigned int size_z, const unsigned int size_c, - const bool is_multiplexed, const bool invert_endianness, - const unsigned long offset) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_raw(): Specified filename is (null).", - cimg_instance); - if (cimg::is_directory(filename)) - throw CImgArgumentException(_cimg_instance - "load_raw(): Specified filename '%s' is a directory.", - cimg_instance,filename); - - unsigned int siz = size_x*size_y*size_z*size_c, - _size_x = size_x, _size_y = size_y, _size_z = size_z, _size_c = size_c; - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - if (!siz) { // Retrieve file size. - const long fpos = std::ftell(nfile); - if (fpos<0) throw CImgArgumentException(_cimg_instance - "load_raw(): Cannot determine size of input file '%s'.", - cimg_instance,filename?filename:"(FILE*)"); - std::fseek(nfile,0,SEEK_END); - siz = _size_y = (unsigned int)std::ftell(nfile)/sizeof(T); - _size_x = _size_z = _size_c = 1; - std::fseek(nfile,fpos,SEEK_SET); - } - std::fseek(nfile,(long)offset,SEEK_SET); - assign(_size_x,_size_y,_size_z,_size_c,0); - if (!is_multiplexed || size_c==1) { - cimg::fread(_data,siz,nfile); - if (invert_endianness) cimg::invert_endianness(_data,siz); - } else { - CImg buf(1,1,1,_size_c); - cimg_forXYZ(*this,x,y,z) { - cimg::fread(buf._data,_size_c,nfile); - if (invert_endianness) cimg::invert_endianness(buf._data,_size_c); - set_vector_at(buf,x,y,z); - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load image sequence using FFMPEG av's libraries. - /** - \param filename Filename, as a C-string. - \param first_frame Index of the first frame to read. - \param last_frame Index of the last frame to read. - \param step_frame Step value for frame reading. - \param pixel_format To be documented. - \param resume To be documented. - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg& load_ffmpeg(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool pixel_format=true, const bool resume=false, - const char axis='z', const float align=0) { - return get_load_ffmpeg(filename,first_frame,last_frame,step_frame,pixel_format,resume,axis,align).move_to(*this); - } - - //! Load image sequence using FFMPEG av's libraries \newinstance. - static CImg get_load_ffmpeg(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool pixel_format=true, - const bool resume=false, - const char axis='z', const float align=0) { - return CImgList().load_ffmpeg(filename,first_frame,last_frame,step_frame,pixel_format,resume). - get_append(axis,align); - } - - //! Load image sequence from a YUV file. - /** - \param filename Filename, as a C-string. - \param size_x Width of the frames. - \param size_y Height of the frames. - \param first_frame Index of the first frame to read. - \param last_frame Index of the last frame to read. - \param step_frame Step value for frame reading. - \param yuv2rgb Tells if the YUV to RGB transform must be applied. - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - **/ - CImg& load_yuv(const char *const filename, - const unsigned int size_x, const unsigned int size_y=1, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { - return get_load_yuv(filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this); - } - - //! Load image sequence from a YUV file \newinstance. - static CImg get_load_yuv(const char *const filename, - const unsigned int size_x, const unsigned int size_y=1, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { - return CImgList().load_yuv(filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb).get_append(axis); - } - - //! Load image sequence from a YUV file \overloading. - CImg& load_yuv(std::FILE *const file, - const unsigned int size_x, const unsigned int size_y=1, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { - return get_load_yuv(file,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this); - } - - //! Load image sequence from a YUV file \newinstance. - static CImg get_load_yuv(std::FILE *const file, - const unsigned int size_x, const unsigned int size_y=1, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') { - return CImgList().load_yuv(file,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb).get_append(axis); - } - - //! Load 3d object from a .OFF file. - /** - \param[out] primitives Primitives data of the 3d object. - \param[out] colors Colors data of the 3d object. - \param filename Filename, as a C-string. - **/ - template - CImg& load_off(CImgList& primitives, CImgList& colors, const char *const filename) { - return _load_off(primitives,colors,0,filename); - } - - //! Load 3d object from a .OFF file \newinstance. - template - static CImg get_load_off(CImgList& primitives, CImgList& colors, const char *const filename) { - return CImg().load_off(primitives,colors,filename); - } - - //! Load 3d object from a .OFF file \overloading. - template - CImg& load_off(CImgList& primitives, CImgList& colors, std::FILE *const file) { - return _load_off(primitives,colors,file,0); - } - - //! Load 3d object from a .OFF file \newinstance. - template - static CImg get_load_off(CImgList& primitives, CImgList& colors, std::FILE *const file) { - return CImg().load_off(primitives,colors,file); - } - - template - CImg& _load_off(CImgList& primitives, CImgList& colors, - std::FILE *const file, const char *const filename) { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "load_off(): Specified filename is (null).", - cimg_instance); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"r"); - unsigned int nb_points = 0, nb_primitives = 0, nb_read = 0; - char line[256] = { 0 }; - int err; - - // Skip comments, and read magic string OFF - do { err = std::fscanf(nfile,"%255[^\n] ",line); } while (!err || (err==1 && *line=='#')); - if (cimg::strncasecmp(line,"OFF",3) && cimg::strncasecmp(line,"COFF",4)) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_off(): OFF header not found in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - do { err = std::fscanf(nfile,"%255[^\n] ",line); } while (!err || (err==1 && *line=='#')); - if ((err = std::sscanf(line,"%u%u%*[^\n] ",&nb_points,&nb_primitives))!=2) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_off(): Invalid number of vertices or primitives specified in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - } - - // Read points data - assign(nb_points,3); - float X = 0, Y = 0, Z = 0; - cimg_forX(*this,l) { - do { err = std::fscanf(nfile,"%255[^\n] ",line); } while (!err || (err==1 && *line=='#')); - if ((err = std::sscanf(line,"%f%f%f%*[^\n] ",&X,&Y,&Z))!=3) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "load_off(): Failed to read vertex %u/%u in file '%s'.", - cimg_instance, - l+1,nb_points,filename?filename:"(FILE*)"); - } - (*this)(l,0) = (T)X; (*this)(l,1) = (T)Y; (*this)(l,2) = (T)Z; - } - - // Read primitive data - primitives.assign(); - colors.assign(); - bool stop_flag = false; - while (!stop_flag) { - float c0 = 0.7f, c1 = 0.7f, c2 = 0.7f; - unsigned int prim = 0, i0 = 0, i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0; - *line = 0; - if ((err = std::fscanf(nfile,"%u",&prim))!=1) stop_flag = true; - else { - ++nb_read; - switch (prim) { - case 1 : { - if ((err = std::fscanf(nfile,"%u%255[^\n] ",&i0,line))<2) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0).move_to(primitives); - CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); - } - } break; - case 2 : { - if ((err = std::fscanf(nfile,"%u%u%255[^\n] ",&i0,&i1,line))<2) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i1).move_to(primitives); - CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); - } - } break; - case 3 : { - if ((err = std::fscanf(nfile,"%u%u%u%255[^\n] ",&i0,&i1,&i2,line))<3) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i2,i1).move_to(primitives); - CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); - } - } break; - case 4 : { - if ((err = std::fscanf(nfile,"%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,line))<4) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i3,i2,i1).move_to(primitives); - CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors); - } - } break; - case 5 : { - if ((err = std::fscanf(nfile,"%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,line))<5) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i3,i2,i1).move_to(primitives); - CImg::vector(i0,i4,i3).move_to(primitives); - colors.insert(2,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); - ++nb_primitives; - } - } break; - case 6 : { - if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,line))<6) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i3,i2,i1).move_to(primitives); - CImg::vector(i0,i5,i4,i3).move_to(primitives); - colors.insert(2,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); - ++nb_primitives; - } - } break; - case 7 : { - if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,line))<7) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i4,i3,i1).move_to(primitives); - CImg::vector(i0,i6,i5,i4).move_to(primitives); - CImg::vector(i3,i2,i1).move_to(primitives); - colors.insert(3,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); - ++(++nb_primitives); - } - } break; - case 8 : { - if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,&i7,line))<7) { - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u from file '%s'.", - cimg_instance, - nb_read,nb_primitives,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } else { - err = std::sscanf(line,"%f%f%f",&c0,&c1,&c2); - CImg::vector(i0,i3,i2,i1).move_to(primitives); - CImg::vector(i0,i5,i4,i3).move_to(primitives); - CImg::vector(i0,i7,i6,i5).move_to(primitives); - colors.insert(3,CImg::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255))); - ++(++nb_primitives); - } - } break; - default : - cimg::warn(_cimg_instance - "load_off(): Failed to read primitive %u/%u (%u vertices) from file '%s'.", - cimg_instance, - nb_read,nb_primitives,prim,filename?filename:"(FILE*)"); - - err = std::fscanf(nfile,"%*[^\n] "); - } - } - } - if (!file) cimg::fclose(nfile); - if (primitives._width!=nb_primitives) - cimg::warn(_cimg_instance - "load_off(): Only %u/%u primitives read from file '%s'.", - cimg_instance, - primitives._width,nb_primitives,filename?filename:"(FILE*)"); - return *this; - } - - //! Load image sequence using FFMPEG's external tool 'ffmpeg'. - /** - \param filename Filename, as a C-string. - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg& load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) { - return get_load_ffmpeg_external(filename,axis,align).move_to(*this); - } - - //! Load image sequence using FFMPEG's external tool 'ffmpeg' \newinstance. - static CImg get_load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) { - return CImgList().load_ffmpeg_external(filename).get_append(axis,align); - } - - //! Load gif file, using Imagemagick or GraphicsMagicks's external tools. - /** - \param filename Filename, as a C-string. - \param use_graphicsmagick Tells if GraphicsMagick's tool 'gm' is used instead of ImageMagick's tool 'convert'. - \param axis Appending axis, if file contains multiple images. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg& load_gif_external(const char *const filename, - const char axis='z', const float align=0) { - return get_load_gif_external(filename,axis,align).move_to(*this); - } - - //! Load gif file, using ImageMagick or GraphicsMagick's external tool 'convert' \newinstance. - static CImg get_load_gif_external(const char *const filename, - const char axis='z', const float align=0) { - return CImgList().load_gif_external(filename).get_append(axis,align); - } - - //! Load image using GraphicsMagick's external tool 'gm'. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_graphicsmagick_external(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_graphicsmagick_external(): Specified filename is (null).", - cimg_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }; - std::FILE *file = 0; - const CImg s_filename = CImg::string(filename)._system_strescape(); -#if cimg_OS==1 - cimg_snprintf(command,sizeof(command),"%s convert \"%s\" pnm:-", - cimg::graphicsmagick_path(),s_filename.data()); - file = popen(command,"r"); - if (file) { - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { load_pnm(file); } catch (...) { - pclose(file); - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load_graphicsmagick_external(): Failed to load file '%s' with external command 'gm'.", - cimg_instance, - filename); - } - pclose(file); - return *this; - } -#endif - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.pnm", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(command,sizeof(command),"%s convert \"%s\" \"%s\"", - cimg::graphicsmagick_path(),s_filename.data(), - CImg::string(filetmp)._system_strescape().data()); - cimg::system(command,cimg::graphicsmagick_path()); - if (!(file = std::fopen(filetmp,"rb"))) { - cimg::fclose(cimg::fopen(filename,"r")); - throw CImgIOException(_cimg_instance - "load_graphicsmagick_external(): Failed to load file '%s' with external command 'gm'.", - cimg_instance, - filename); - - } else cimg::fclose(file); - load_pnm(filetmp); - std::remove(filetmp); - return *this; - } - - //! Load image using GraphicsMagick's external tool 'gm' \newinstance. - static CImg get_load_graphicsmagick_external(const char *const filename) { - return CImg().load_graphicsmagick_external(filename); - } - - //! Load gzipped image file, using external tool 'gunzip'. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_gzip_external(const char *const filename) { - if (!filename) - throw CImgIOException(_cimg_instance - "load_gzip_external(): Specified filename is (null).", - cimg_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; - const char - *const ext = cimg::split_filename(filename,body), - *const ext2 = cimg::split_filename(body,0); - - std::FILE *file = 0; - do { - if (!cimg::strcasecmp(ext,"gz")) { - if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } else { - if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", - cimg::gunzip_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); - cimg::system(command); - if (!(file = std::fopen(filetmp,"rb"))) { - cimg::fclose(cimg::fopen(filename,"r")); - throw CImgIOException(_cimg_instance - "load_gzip_external(): Failed to load file '%s' with external command 'gunzip'.", - cimg_instance, - filename); - - } else cimg::fclose(file); - load(filetmp); - std::remove(filetmp); - return *this; - } - - //! Load gzipped image file, using external tool 'gunzip' \newinstance. - static CImg get_load_gzip_external(const char *const filename) { - return CImg().load_gzip_external(filename); - } - - //! Load image using ImageMagick's external tool 'convert'. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_imagemagick_external(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_imagemagick_external(): Specified filename is (null).", - cimg_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }; - std::FILE *file = 0; - const CImg s_filename = CImg::string(filename)._system_strescape(); -#if cimg_OS==1 - cimg_snprintf(command,sizeof(command),"%s%s \"%s\" pnm:-", - cimg::imagemagick_path(), - !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"", - s_filename.data()); - file = popen(command,"r"); - if (file) { - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { load_pnm(file); } catch (...) { - pclose(file); - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load_imagemagick_external(): Failed to load file '%s' with " - "external command 'convert'.", - cimg_instance, - filename); - } - pclose(file); - return *this; - } -#endif - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.pnm", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(command,sizeof(command),"%s%s \"%s\" \"%s\"", - cimg::imagemagick_path(), - !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"", - s_filename.data(),CImg::string(filetmp)._system_strescape().data()); - cimg::system(command,cimg::imagemagick_path()); - if (!(file = std::fopen(filetmp,"rb"))) { - cimg::fclose(cimg::fopen(filename,"r")); - throw CImgIOException(_cimg_instance - "load_imagemagick_external(): Failed to load file '%s' with external command 'convert'.", - cimg_instance, - filename); - - } else cimg::fclose(file); - load_pnm(filetmp); - std::remove(filetmp); - return *this; - } - - //! Load image using ImageMagick's external tool 'convert' \newinstance. - static CImg get_load_imagemagick_external(const char *const filename) { - return CImg().load_imagemagick_external(filename); - } - - //! Load image from a DICOM file, using XMedcon's external tool 'medcon'. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_medcon_external(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_medcon_external(): Specified filename is (null).", - cimg_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; - cimg::fclose(cimg::fopen(filename,"r")); - std::FILE *file = 0; - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s.hdr",cimg::filenamerand()); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(command,sizeof(command),"%s -w -c anlz -o \"%s\" -f \"%s\"", - cimg::medcon_path(), - CImg::string(filetmp)._system_strescape().data(), - CImg::string(filename)._system_strescape().data()); - cimg::system(command); - cimg::split_filename(filetmp,body); - - cimg_snprintf(command,sizeof(command),"%s.hdr",body); - file = std::fopen(command,"rb"); - if (!file) { - cimg_snprintf(command,sizeof(command),"m000-%s.hdr",body); - file = std::fopen(command,"rb"); - if (!file) { - throw CImgIOException(_cimg_instance - "load_medcon_external(): Failed to load file '%s' with external command 'medcon'.", - cimg_instance, - filename); - } - } - cimg::fclose(file); - load_analyze(command); - std::remove(command); - cimg::split_filename(command,body); - cimg_snprintf(command,sizeof(command),"%s.img",body); - std::remove(command); - return *this; - } - - //! Load image from a DICOM file, using XMedcon's external tool 'medcon' \newinstance. - static CImg get_load_medcon_external(const char *const filename) { - return CImg().load_medcon_external(filename); - } - - //! Load image from a RAW Color Camera file, using external tool 'dcraw'. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_dcraw_external(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_dcraw_external(): Specified filename is (null).", - cimg_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }; - std::FILE *file = 0; - const CImg s_filename = CImg::string(filename)._system_strescape(); -#if cimg_OS==1 - cimg_snprintf(command,sizeof(command),"%s -w -4 -c \"%s\"", - cimg::dcraw_path(),s_filename.data()); - file = popen(command,"r"); - if (file) { - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { load_pnm(file); } catch (...) { - pclose(file); - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.", - cimg_instance, - filename); - } - pclose(file); - return *this; - } -#endif - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.ppm", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(command,sizeof(command),"%s -w -4 -c \"%s\" > \"%s\"", - cimg::dcraw_path(),s_filename.data(),CImg::string(filetmp)._system_strescape().data()); - cimg::system(command,cimg::dcraw_path()); - if (!(file = std::fopen(filetmp,"rb"))) { - cimg::fclose(cimg::fopen(filename,"r")); - throw CImgIOException(_cimg_instance - "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.", - cimg_instance, - filename); - - } else cimg::fclose(file); - load_pnm(filetmp); - std::remove(filetmp); - return *this; - } - - //! Load image from a RAW Color Camera file, using external tool 'dcraw' \newinstance. - static CImg get_load_dcraw_external(const char *const filename) { - return CImg().load_dcraw_external(filename); - } - - //! Load image from a camera stream, using OpenCV. - /** - \param camera_index Index of the camera to capture images from. - \param skip_frames Number of frames to skip before the capture. - \param release_camera Tells if the camera ressource must be released at the end of the method. - **/ - CImg& load_camera(const unsigned int camera_index=0, const unsigned int skip_frames=0, - const bool release_camera=true, const unsigned int capture_width=0, - const unsigned int capture_height=0) { -#ifdef cimg_use_opencv - if (camera_index>255) - throw CImgArgumentException(_cimg_instance - "load_camera(): Invalid request for camera #%u " - "(no more than 256 cameras can be managed).", - cimg_instance, - camera_index); - static CvCapture *capture[256] = { 0 }; - if (release_camera) { - if (capture[camera_index]) cvReleaseCapture(&(capture[camera_index])); - capture[camera_index] = 0; - return *this; - } - if (!capture[camera_index]) { - capture[camera_index] = cvCreateCameraCapture(camera_index); - if (!capture[camera_index]) { - throw CImgIOException(_cimg_instance - "load_camera(): Failed to initialize camera #%u.", - cimg_instance, - camera_index); - } - } - if (capture_width) cvSetCaptureProperty(capture[camera_index],CV_CAP_PROP_FRAME_WIDTH,capture_width); - if (capture_height) cvSetCaptureProperty(capture[camera_index],CV_CAP_PROP_FRAME_HEIGHT,capture_height); - const IplImage *img = 0; - for (unsigned int i = 0; iwidthStep - 3*img->width); - assign(img->width,img->height,1,3); - const unsigned char* ptrs = (unsigned char*)img->imageData; - T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2); - if (step>0) cimg_forY(*this,y) { - cimg_forX(*this,x) { *(ptr_b++) = (T)*(ptrs++); *(ptr_g++) = (T)*(ptrs++); *(ptr_r++) = (T)*(ptrs++); } - ptrs+=step; - } else for (unsigned long siz = (unsigned long)img->width*img->height; siz; --siz) { - *(ptr_b++) = (T)*(ptrs++); *(ptr_g++) = (T)*(ptrs++); *(ptr_r++) = (T)*(ptrs++); - } - } - return *this; -#else - cimg::unused(camera_index,skip_frames,release_camera,capture_width,capture_height); - throw CImgIOException(_cimg_instance - "load_camera(): This function requires the OpenCV library to run " - "(macro 'cimg_use_opencv' must be defined).", - cimg_instance); -#endif - } - - //! Load image from a camera stream, using OpenCV \newinstance. - static CImg get_load_camera(const unsigned int camera_index=0, const unsigned int skip_frames=0, - const bool release_camera=true, - const unsigned int capture_width=0, const unsigned int capture_height=0) { - return CImg().load_camera(camera_index,skip_frames,release_camera,capture_width,capture_height); - } - - //! Load image using various non-native ways. - /** - \param filename Filename, as a C-string. - **/ - CImg& load_other(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimg_instance - "load_other(): Specified filename is (null).", - cimg_instance); - - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { load_magick(filename); } - catch (CImgException&) { - try { load_imagemagick_external(filename); } - catch (CImgException&) { - try { load_graphicsmagick_external(filename); } - catch (CImgException&) { - try { load_cimg(filename); } - catch (CImgException&) { - try { - std::fclose(cimg::fopen(filename,"rb")); - } catch (CImgException&) { - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load_other(): Failed to open file '%s'.", - cimg_instance, - filename); - } - cimg::exception_mode() = omode; - throw CImgIOException(_cimg_instance - "load_other(): Failed to recognize format of file '%s'.", - cimg_instance, - filename); - } - } - } - } - cimg::exception_mode() = omode; - return *this; - } - - //! Load image using various non-native ways \newinstance. - static CImg get_load_other(const char *const filename) { - return CImg().load_other(filename); - } - - //@} - //--------------------------- - // - //! \name Data Output - //@{ - //--------------------------- - - //! Display informations about the image data. - /** - \param title Name for the considered image. - \param display_stats Tells to compute and display image statistics. - **/ - const CImg& print(const char *const title=0, const bool display_stats=true) const { - int xm = 0, ym = 0, zm = 0, vm = 0, xM = 0, yM = 0, zM = 0, vM = 0; - CImg st; - if (!is_empty() && display_stats) { - st = get_stats(); - xm = (int)st[4]; ym = (int)st[5], zm = (int)st[6], vm = (int)st[7]; - xM = (int)st[8]; yM = (int)st[9], zM = (int)st[10], vM = (int)st[11]; - } - const unsigned long siz = size(), msiz = siz*sizeof(T), siz1 = siz-1, - mdisp = msiz<8*1024?0:(msiz<8*1024*1024?1:2), width1 = _width-1; - - char _title[64] = { 0 }; - if (!title) cimg_snprintf(_title,sizeof(_title),"CImg<%s>",pixel_type()); - - std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = (%u,%u,%u,%u) [%lu %s], %sdata%s = (%s*)%p", - cimg::t_magenta,cimg::t_bold,title?title:_title,cimg::t_normal, - cimg::t_bold,cimg::t_normal,(void*)this, - cimg::t_bold,cimg::t_normal,_width,_height,_depth,_spectrum, - mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20)), - mdisp==0?"b":(mdisp==1?"Kio":"Mio"), - cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin()); - if (_data) std::fprintf(cimg::output(),"..%p (%s) = [ ",(void*)((char*)end()-1),_is_shared?"shared":"non-shared"); - else std::fprintf(cimg::output()," (%s) = [ ",_is_shared?"shared":"non-shared"); - - if (!is_empty()) cimg_foroff(*this,off) { - std::fprintf(cimg::output(),cimg::type::format(),cimg::type::format(_data[off])); - if (off!=siz1) std::fprintf(cimg::output(),"%s",off%_width==width1?" ; ":" "); - if (off==7 && siz>16) { off = siz1-8; std::fprintf(cimg::output(),"... "); } - } - if (!is_empty() && display_stats) - std::fprintf(cimg::output(), - " ], %smin%s = %g, %smax%s = %g, %smean%s = %g, %sstd%s = %g, %scoords_min%s = (%u,%u,%u,%u), " - "%scoords_max%s = (%u,%u,%u,%u).\n", - cimg::t_bold,cimg::t_normal,st[0], - cimg::t_bold,cimg::t_normal,st[1], - cimg::t_bold,cimg::t_normal,st[2], - cimg::t_bold,cimg::t_normal,std::sqrt(st[3]), - cimg::t_bold,cimg::t_normal,xm,ym,zm,vm, - cimg::t_bold,cimg::t_normal,xM,yM,zM,vM); - else std::fprintf(cimg::output(),"%s].\n",is_empty()?"":" "); - std::fflush(cimg::output()); - return *this; - } - - //! Display image into a CImgDisplay window. - /** - \param disp Display window. - **/ - const CImg& display(CImgDisplay& disp) const { - disp.display(*this); - return *this; - } - - //! Display image into a CImgDisplay window, in an interactive way. - /** - \param disp Display window. - \param display_info Tells if image informations are displayed on the standard output. - **/ - const CImg& display(CImgDisplay &disp, const bool display_info, unsigned int *const XYZ=0) const { - return _display(disp,0,display_info,XYZ,false); - } - - //! Display image into an interactive window. - /** - \param title Window title - \param display_info Tells if image informations are displayed on the standard output. - **/ - const CImg& display(const char *const title=0, const bool display_info=true, unsigned int *const XYZ=0) const { - CImgDisplay disp; - return _display(disp,title,display_info,XYZ,false); - } - - const CImg& _display(CImgDisplay &disp, const char *const title, - const bool display_info, unsigned int *const XYZ, - const bool exit_on_simpleclick) const { - unsigned int oldw = 0, oldh = 0, _XYZ[3] = { 0 }, key = 0; - int x0 = 0, y0 = 0, z0 = 0, x1 = width()-1, y1 = height()-1, z1 = depth()-1; - - if (!disp) { - disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1); - if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum); - else disp.set_title("%s",title); - } else if (title) disp.set_title("%s",title); - disp.show().flush(); - - const CImg dtitle = CImg::string(disp.title()); - if (display_info) print(dtitle); - - CImg zoom; - for (bool reset_view = true, resize_disp = false, is_first_select = true; !key && !disp.is_closed(); ) { - if (reset_view) { - if (XYZ) { _XYZ[0] = XYZ[0]; _XYZ[1] = XYZ[1]; _XYZ[2] = XYZ[2]; } - else { _XYZ[0] = (x0 + x1)/2; _XYZ[1] = (y0 + y1)/2; _XYZ[2] = (z0 + z1)/2; } - x0 = 0; y0 = 0; z0 = 0; x1 = width()-1; y1 = height()-1; z1 = depth()-1; - oldw = disp.width(); oldh = disp.height(); - reset_view = false; - } - if (!x0 && !y0 && !z0 && x1==width()-1 && y1==height()-1 && z1==depth()-1) { - if (is_empty()) zoom.assign(1,1,1,1,0); else zoom.assign(); - } else zoom = get_crop(x0,y0,z0,x1,y1,z1); - - const unsigned int - dx = 1 + x1 - x0, dy = 1 + y1 - y0, dz = 1 + z1 - z0, - tw = dx + (dz>1?dz:0), th = dy + (dz>1?dz:0); - if (!is_empty() && !disp.is_fullscreen() && resize_disp) { - const unsigned int - ttw = tw*disp.width()/oldw, tth = th*disp.height()/oldh, - dM = cimg::max(ttw,tth), diM = (unsigned int)cimg::max(disp.width(),disp.height()), - imgw = cimg::max(16U,ttw*diM/dM), imgh = cimg::max(16U,tth*diM/dM); - disp.set_fullscreen(false).resize(cimg_fitscreen(imgw,imgh,1),false); - resize_disp = false; - } - oldw = tw; oldh = th; - - bool - go_up = false, go_down = false, go_left = false, go_right = false, - go_inc = false, go_dec = false, go_in = false, go_out = false, - go_in_center = false; - const CImg& visu = zoom?zoom:*this; - - disp.set_title("%s",dtitle._data); - if (_width>1 && visu._width==1) disp.set_title("%s | x=%u",disp._title,x0); - if (_height>1 && visu._height==1) disp.set_title("%s | y=%u",disp._title,y0); - if (_depth>1 && visu._depth==1) disp.set_title("%s | z=%u",disp._title,z0); - - if (!is_first_select) { _XYZ[0] = (x1-x0)/2; _XYZ[1] = (y1-y0)/2; _XYZ[2] = (z1-z0)/2; } - const CImg selection = visu._get_select(disp,0,2,_XYZ,x0,y0,z0,is_first_select,_depth>1); - is_first_select = false; - - if (disp.wheel()) { - if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - go_out = !(go_in = disp.wheel()>0); go_in_center = false; - } else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) { go_left = !(go_right = disp.wheel()>0); } - else if (disp.is_keyALT() || disp.is_keyALTGR() || _depth==1) { go_down = !(go_up = disp.wheel()>0); } - disp.set_wheel(); - } - - const int - sx0 = selection(0), sy0 = selection(1), sz0 = selection(2), - sx1 = selection(3), sy1 = selection(4), sz1 = selection(5); - if (sx0>=0 && sy0>=0 && sz0>=0 && sx1>=0 && sy1>=0 && sz1>=0) { - x1 = x0 + sx1; y1 = y0 + sy1; z1 = z0 + sz1; - x0+=sx0; y0+=sy0; z0+=sz0; - if (sx0==sx1 && sy0==sy1 && sz0==sz1) { - if (exit_on_simpleclick && (!zoom || is_empty())) break; else reset_view = true; - } - resize_disp = true; - } else switch (key = disp.key()) { -#if cimg_OS!=2 - case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT : -#endif - case 0 : case cimg::keyCTRLLEFT : case cimg::keyPAD5 : case cimg::keySHIFTLEFT : -#if cimg_OS!=2 - case cimg::keyALTGR : -#endif - case cimg::keyALT : key = 0; break; - case cimg::keyP : if (visu._depth>1 && (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT())) { - // Special mode: play stack of frames - const unsigned int - w1 = visu._width*disp.width()/(visu._width+(visu._depth>1?visu._depth:0)), - h1 = visu._height*disp.height()/(visu._height+(visu._depth>1?visu._depth:0)); - float frame_timing = 5; - bool is_stopped = false; - disp.set_key(key,false).set_wheel().resize(cimg_fitscreen(w1,h1,1),false); key = 0; - for (unsigned int timer = 0; !key && !disp.is_closed() && !disp.button(); ) { - if (disp.is_resized()) disp.resize(false); - if (!timer) { - visu.get_slice((int)_XYZ[2]).display(disp.set_title("%s | z=%d",dtitle.data(),_XYZ[2])); - (++_XYZ[2])%=visu._depth; - } - if (!is_stopped) { if (++timer>(unsigned int)frame_timing) timer = 0; } else timer = ~0U; - if (disp.wheel()) { frame_timing-=disp.wheel()/3.0f; disp.set_wheel(); } - switch (key = disp.key()) { -#if cimg_OS!=2 - case cimg::keyCTRLRIGHT : -#endif - case cimg::keyCTRLLEFT : key = 0; break; - case cimg::keyPAGEUP : frame_timing-=0.3f; key = 0; break; - case cimg::keyPAGEDOWN : frame_timing+=0.3f; key = 0; break; - case cimg::keySPACE : is_stopped = !is_stopped; disp.set_key(key,false); key = 0; break; - case cimg::keyARROWLEFT : case cimg::keyARROWUP : is_stopped = true; timer = 0; key = 0; break; - case cimg::keyARROWRIGHT : case cimg::keyARROWDOWN : is_stopped = true; - (_XYZ[2]+=visu._depth-2)%=visu._depth; timer = 0; key = 0; break; - case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), - CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false); - disp.set_key(key,false); key = 0; - } break; - case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false).set_key(key,false); key = 0; - } break; - case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(_width,_height,_depth),false).set_key(key,false); key = 0; - } break; - case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.resize(disp.screen_width(),disp.screen_height(),false). - toggle_fullscreen().set_key(key,false); key = 0; - } break; - } - frame_timing = frame_timing<1?1:(frame_timing>39?39:frame_timing); - disp.wait(20); - } - const unsigned int - w2 = (visu._width + (visu._depth>1?visu._depth:0))*disp.width()/visu._width, - h2 = (visu._height + (visu._depth>1?visu._depth:0))*disp.height()/visu._height; - disp.resize(cimg_fitscreen(w2,h2,1),false).set_title(dtitle.data()).set_key().set_button().set_wheel(); - key = 0; - } break; - case cimg::keyHOME : reset_view = resize_disp = true; key = 0; break; - case cimg::keyPADADD : go_in = true; go_in_center = true; key = 0; break; - case cimg::keyPADSUB : go_out = true; key = 0; break; - case cimg::keyARROWLEFT : case cimg::keyPAD4: go_left = true; key = 0; break; - case cimg::keyARROWRIGHT : case cimg::keyPAD6: go_right = true; key = 0; break; - case cimg::keyARROWUP : case cimg::keyPAD8: go_up = true; key = 0; break; - case cimg::keyARROWDOWN : case cimg::keyPAD2: go_down = true; key = 0; break; - case cimg::keyPAD7 : go_up = go_left = true; key = 0; break; - case cimg::keyPAD9 : go_up = go_right = true; key = 0; break; - case cimg::keyPAD1 : go_down = go_left = true; key = 0; break; - case cimg::keyPAD3 : go_down = go_right = true; key = 0; break; - case cimg::keyPAGEUP : go_inc = true; key = 0; break; - case cimg::keyPAGEDOWN : go_dec = true; key = 0; break; - } - if (go_in) { - const int - mx = go_in_center?disp.width()/2:disp.mouse_x(), - my = go_in_center?disp.height()/2:disp.mouse_y(), - mX = mx*(_width+(_depth>1?_depth:0))/disp.width(), - mY = my*(_height+(_depth>1?_depth:0))/disp.height(); - int X = _XYZ[0], Y = _XYZ[1], Z = _XYZ[2]; - if (mX=height()) { - X = x0 + mX*(1+x1-x0)/_width; Z = z0 + (mY-_height)*(1+z1-z0)/_depth; Y = _XYZ[1]; - } - if (mX>=width() && mY4) { x0 = X - 3*(X-x0)/4; x1 = X + 3*(x1-X)/4; } - if (y1-y0>4) { y0 = Y - 3*(Y-y0)/4; y1 = Y + 3*(y1-Y)/4; } - if (z1-z0>4) { z0 = Z - 3*(Z-z0)/4; z1 = Z + 3*(z1-Z)/4; } - } - if (go_out) { - const int - delta_x = (x1-x0)/8, delta_y = (y1-y0)/8, delta_z = (z1-z0)/8, - ndelta_x = delta_x?delta_x:(_width>1?1:0), - ndelta_y = delta_y?delta_y:(_height>1?1:0), - ndelta_z = delta_z?delta_z:(_depth>1?1:0); - x0-=ndelta_x; y0-=ndelta_y; z0-=ndelta_z; - x1+=ndelta_x; y1+=ndelta_y; z1+=ndelta_z; - if (x0<0) { x1-=x0; x0 = 0; if (x1>=width()) x1 = width() - 1; } - if (y0<0) { y1-=y0; y0 = 0; if (y1>=height()) y1 = height() - 1; } - if (z0<0) { z1-=z0; z0 = 0; if (z1>=depth()) z1 = depth() - 1; } - if (x1>=width()) { x0-=(x1-width()+1); x1 = width()-1; if (x0<0) x0 = 0; } - if (y1>=height()) { y0-=(y1-height()+1); y1 = height()-1; if (y0<0) y0 = 0; } - if (z1>=depth()) { z0-=(z1-depth()+1); z1 = depth()-1; if (z0<0) z0 = 0; } - } - if (go_left) { - const int delta = (x1-x0)/4, ndelta = delta?delta:(_width>1?1:0); - if (x0-ndelta>=0) { x0-=ndelta; x1-=ndelta; } - else { x1-=x0; x0 = 0; } - } - if (go_right) { - const int delta = (x1-x0)/4, ndelta = delta?delta:(_width>1?1:0); - if (x1+ndelta1?1:0); - if (y0-ndelta>=0) { y0-=ndelta; y1-=ndelta; } - else { y1-=y0; y0 = 0; } - } - if (go_down) { - const int delta = (y1-y0)/4, ndelta = delta?delta:(_height>1?1:0); - if (y1+ndelta1?1:0); - if (z0-ndelta>=0) { z0-=ndelta; z1-=ndelta; } - else { z1-=z0; z0 = 0; } - } - if (go_dec) { - const int delta = (z1-z0)/4, ndelta = delta?delta:(_depth>1?1:0); - if (z1+ndelta - const CImg& display_object3d(CImgDisplay& disp, - const CImg& vertices, - const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return _display_object3d(disp,0,vertices,primitives,colors,opacities,centering,render_static, - render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(const char *const title, - const CImg& vertices, - const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - CImgDisplay disp; - return _display_object3d(disp,title,vertices,primitives,colors,opacities,centering,render_static, - render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(CImgDisplay &disp, - const CImg& vertices, - const CImgList& primitives, - const CImgList& colors, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return display_object3d(disp,vertices,primitives,colors,CImgList(),centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(const char *const title, - const CImg& vertices, - const CImgList& primitives, - const CImgList& colors, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return display_object3d(title,vertices,primitives,colors,CImgList(),centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(CImgDisplay &disp, - const CImg& vertices, - const CImgList& primitives, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return display_object3d(disp,vertices,primitives,CImgList(),centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(const char *const title, - const CImg& vertices, - const CImgList& primitives, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return display_object3d(title,vertices,primitives,CImgList(),centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(CImgDisplay &disp, - const CImg& vertices, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return display_object3d(disp,vertices,CImgList(),centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - //! Display object 3d in an interactive window \simplification. - template - const CImg& display_object3d(const char *const title, - const CImg& vertices, - const bool centering=true, - const int render_static=4, const int render_motion=1, - const bool is_double_sided=true, const float focale=700, - const float light_x=0, const float light_y=0, const float light_z=-5e8f, - const float specular_lightness=0.2f, const float specular_shininess=0.1f, - const bool display_axes=true, float *const pose_matrix=0) const { - return display_object3d(title,vertices,CImgList(),centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - - template - const CImg& _display_object3d(CImgDisplay& disp, const char *const title, - const CImg& vertices, - const CImgList& primitives, - const CImgList& colors, - const to& opacities, - const bool centering, - const int render_static, const int render_motion, - const bool is_double_sided, const float focale, - const float light_x, const float light_y, const float light_z, - const float specular_lightness, const float specular_shininess, - const bool display_axes, float *const pose_matrix) const { - typedef typename cimg::superset::type tpfloat; - - // Check input arguments - if (is_empty()) { - if (disp) return CImg(disp.width(),disp.height(),1,(colors && colors[0].size()==1)?1:3,0). - _display_object3d(disp,title,vertices,primitives,colors,opacities,centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - else return CImg(1,2,1,1,64,128).resize(cimg_fitscreen(CImgDisplay::screen_width()/2, - CImgDisplay::screen_height()/2,1), - 1,(colors && colors[0].size()==1)?1:3,3). - _display_object3d(disp,title,vertices,primitives,colors,opacities,centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } else { if (disp) disp.resize(*this,false); } - char error_message[1024] = { 0 }; - if (!vertices.is_object3d(primitives,colors,opacities,true,error_message)) - throw CImgArgumentException(_cimg_instance - "display_object3d(): Invalid specified 3d object (%u,%u) (%s).", - cimg_instance,vertices._width,primitives._width,error_message); - if (vertices._width && !primitives) { - CImgList nprimitives(vertices._width,1,1,1,1); - cimglist_for(nprimitives,l) nprimitives(l,0) = l; - return _display_object3d(disp,title,vertices,nprimitives,colors,opacities,centering, - render_static,render_motion,is_double_sided,focale, - light_x,light_y,light_z,specular_lightness,specular_shininess, - display_axes,pose_matrix); - } - if (!disp) { - disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,3); - if (!title) disp.set_title("CImg<%s> (%u vertices, %u primitives)", - pixel_type(),vertices._width,primitives._width); - } else if (title) disp.set_title("%s",title); - - // Init 3d objects and compute object statistics - CImg - pose, - rotated_vertices(vertices._width,3), - bbox_vertices, rotated_bbox_vertices, - axes_vertices, rotated_axes_vertices, - bbox_opacities, axes_opacities; - CImgList bbox_primitives, axes_primitives; - CImgList reverse_primitives; - CImgList bbox_colors, bbox_colors2, axes_colors; - unsigned int ns_width = 0, ns_height = 0; - int _is_double_sided = (int)is_double_sided; - bool ndisplay_axes = display_axes; - const CImg - background_color(1,1,1,_spectrum,0), - foreground_color(1,1,1,_spectrum,255); - float - Xoff = 0, Yoff = 0, Zoff = 0, sprite_scale = 1, - xm = 0, xM = vertices?vertices.get_shared_row(0).max_min(xm):0, - ym = 0, yM = vertices?vertices.get_shared_row(1).max_min(ym):0, - zm = 0, zM = vertices?vertices.get_shared_row(2).max_min(zm):0; - const float delta = cimg::max(xM-xm,yM-ym,zM-zm); - - rotated_bbox_vertices = bbox_vertices.assign(8,3,1,1, - xm,xM,xM,xm,xm,xM,xM,xm, - ym,ym,yM,yM,ym,ym,yM,yM, - zm,zm,zm,zm,zM,zM,zM,zM); - bbox_primitives.assign(6,1,4,1,1, 0,3,2,1, 4,5,6,7, 1,2,6,5, 0,4,7,3, 0,1,5,4, 2,3,7,6); - bbox_colors.assign(6,_spectrum,1,1,1,background_color[0]); - bbox_colors2.assign(6,_spectrum,1,1,1,foreground_color[0]); - bbox_opacities.assign(bbox_colors._width,1,1,1,0.3f); - - rotated_axes_vertices = axes_vertices.assign(7,3,1,1, - 0,20,0,0,22,-6,-6, - 0,0,20,0,-6,22,-6, - 0,0,0,20,0,0,22); - axes_opacities.assign(3,1,1,1,1); - axes_colors.assign(3,_spectrum,1,1,1,foreground_color[0]); - axes_primitives.assign(3,1,2,1,1, 0,1, 0,2, 0,3); - - // Begin user interaction loop - CImg visu0(*this), visu; - CImg zbuffer(visu0.width(),visu0.height(),1,1,0); - bool init_pose = true, clicked = false, redraw = true; - unsigned int key = 0; - int - x0 = 0, y0 = 0, x1 = 0, y1 = 0, - nrender_static = render_static, - nrender_motion = render_motion; - disp.show().flush(); - - while (!disp.is_closed() && !key) { - - // Init object pose - if (init_pose) { - const float - ratio = delta>0?(2.0f*cimg::min(disp.width(),disp.height())/(3.0f*delta)):1, - dx = (xM + xm)/2, dy = (yM + ym)/2, dz = (zM + zm)/2; - if (centering) - CImg(4,3,1,1, ratio,0.,0.,-ratio*dx, 0.,ratio,0.,-ratio*dy, 0.,0.,ratio,-ratio*dz).move_to(pose); - else CImg(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose); - if (pose_matrix) { - CImg pose0(pose_matrix,4,3,1,1,false); - pose0.resize(4,4,1,1,0); pose.resize(4,4,1,1,0); - pose0(3,3) = pose(3,3) = 1; - (pose0*pose).get_crop(0,0,3,2).move_to(pose); - Xoff = pose_matrix[12]; Yoff = pose_matrix[13]; Zoff = pose_matrix[14]; sprite_scale = pose_matrix[15]; - } else { Xoff = Yoff = Zoff = 0; sprite_scale = 1; } - init_pose = false; - redraw = true; - } - - // Rotate and draw 3d object - if (redraw) { - const float - r00 = pose(0,0), r10 = pose(1,0), r20 = pose(2,0), r30 = pose(3,0), - r01 = pose(0,1), r11 = pose(1,1), r21 = pose(2,1), r31 = pose(3,1), - r02 = pose(0,2), r12 = pose(1,2), r22 = pose(2,2), r32 = pose(3,2); - if ((clicked && nrender_motion>=0) || (!clicked && nrender_static>=0)) - cimg_forX(vertices,l) { - const float x = (float)vertices(l,0), y = (float)vertices(l,1), z = (float)vertices(l,2); - rotated_vertices(l,0) = r00*x + r10*y + r20*z + r30; - rotated_vertices(l,1) = r01*x + r11*y + r21*z + r31; - rotated_vertices(l,2) = r02*x + r12*y + r22*z + r32; - } - else cimg_forX(bbox_vertices,l) { - const float x = bbox_vertices(l,0), y = bbox_vertices(l,1), z = bbox_vertices(l,2); - rotated_bbox_vertices(l,0) = r00*x + r10*y + r20*z + r30; - rotated_bbox_vertices(l,1) = r01*x + r11*y + r21*z + r31; - rotated_bbox_vertices(l,2) = r02*x + r12*y + r22*z + r32; - } - - // Draw objects -#ifdef cimg_use_openmp - const bool render_with_zbuffer = true; -#else - const bool render_with_zbuffer = !clicked && nrender_static>0; -#endif - visu = visu0; - if ((clicked && nrender_motion<0) || (!clicked && nrender_static<0)) - visu.draw_object3d(Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, - rotated_bbox_vertices,bbox_primitives,bbox_colors,bbox_opacities,2,false,focale). - draw_object3d(Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, - rotated_bbox_vertices,bbox_primitives,bbox_colors2,1,false,focale); - else visu._draw_object3d((void*)0,render_with_zbuffer?zbuffer.fill(0):CImg::empty(), - Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, - rotated_vertices,reverse_primitives?reverse_primitives:primitives, - colors,opacities,clicked?nrender_motion:nrender_static,_is_double_sided==1,focale, - width()/2.0f+light_x,height()/2.0f+light_y,light_z+Zoff, - specular_lightness,specular_shininess,sprite_scale); - // Draw axes - if (ndisplay_axes) { - const float - n = (float)std::sqrt(1e-8 + r00*r00 + r01*r01 + r02*r02), - _r00 = r00/n, _r10 = r10/n, _r20 = r20/n, - _r01 = r01/n, _r11 = r11/n, _r21 = r21/n, - _r02 = r01/n, _r12 = r12/n, _r22 = r22/n, - Xaxes = 25, Yaxes = visu._height - 38.0f; - cimg_forX(axes_vertices,l) { - const float - x = axes_vertices(l,0), - y = axes_vertices(l,1), - z = axes_vertices(l,2); - rotated_axes_vertices(l,0) = _r00*x + _r10*y + _r20*z; - rotated_axes_vertices(l,1) = _r01*x + _r11*y + _r21*z; - rotated_axes_vertices(l,2) = _r02*x + _r12*y + _r22*z; - } - axes_opacities(0,0) = (rotated_axes_vertices(1,2)>0)?0.5f:1.0f; - axes_opacities(1,0) = (rotated_axes_vertices(2,2)>0)?0.5f:1.0f; - axes_opacities(2,0) = (rotated_axes_vertices(3,2)>0)?0.5f:1.0f; - visu.draw_object3d(Xaxes,Yaxes,0,rotated_axes_vertices,axes_primitives, - axes_colors,axes_opacities,1,false,focale). - draw_text((int)(Xaxes+rotated_axes_vertices(4,0)), - (int)(Yaxes+rotated_axes_vertices(4,1)), - "X",axes_colors[0]._data,0,axes_opacities(0,0),13). - draw_text((int)(Xaxes+rotated_axes_vertices(5,0)), - (int)(Yaxes+rotated_axes_vertices(5,1)), - "Y",axes_colors[1]._data,0,axes_opacities(1,0),13). - draw_text((int)(Xaxes+rotated_axes_vertices(6,0)), - (int)(Yaxes+rotated_axes_vertices(6,1)), - "Z",axes_colors[2]._data,0,axes_opacities(2,0),13); - } - visu.display(disp); - if (!clicked || nrender_motion==nrender_static) redraw = false; - } - - // Handle user interaction - disp.wait(); - if ((disp.button() || disp.wheel()) && disp.mouse_x()>=0 && disp.mouse_y()>=0) { - redraw = true; - if (!clicked) { x0 = x1 = disp.mouse_x(); y0 = y1 = disp.mouse_y(); if (!disp.wheel()) clicked = true; } - else { x1 = disp.mouse_x(); y1 = disp.mouse_y(); } - if (disp.button()&1) { - const float - R = 0.45f*cimg::min(disp.width(),disp.height()), - R2 = R*R, - u0 = (float)(x0-disp.width()/2), - v0 = (float)(y0-disp.height()/2), - u1 = (float)(x1-disp.width()/2), - v1 = (float)(y1-disp.height()/2), - n0 = (float)std::sqrt(u0*u0+v0*v0), - n1 = (float)std::sqrt(u1*u1+v1*v1), - nu0 = n0>R?(u0*R/n0):u0, - nv0 = n0>R?(v0*R/n0):v0, - nw0 = (float)std::sqrt(cimg::max(0,R2-nu0*nu0-nv0*nv0)), - nu1 = n1>R?(u1*R/n1):u1, - nv1 = n1>R?(v1*R/n1):v1, - nw1 = (float)std::sqrt(cimg::max(0,R2-nu1*nu1-nv1*nv1)), - u = nv0*nw1-nw0*nv1, - v = nw0*nu1-nu0*nw1, - w = nv0*nu1-nu0*nv1, - n = (float)std::sqrt(u*u+v*v+w*w), - alpha = (float)std::asin(n/R2); - (CImg::rotation_matrix(u,v,w,alpha)*pose).move_to(pose); - x0 = x1; y0 = y1; - } - if (disp.button()&2) { - if (focale>0) Zoff-=(y0-y1)*focale/400; - else { const float s = std::exp((y0-y1)/400.0f); pose*=s; sprite_scale*=s; } - x0 = x1; y0 = y1; - } - if (disp.wheel()) { - if (focale>0) Zoff-=disp.wheel()*focale/20; - else { const float s = std::exp(disp.wheel()/20.0f); pose*=s; sprite_scale*=s; } - disp.set_wheel(); - } - if (disp.button()&4) { Xoff+=(x1-x0); Yoff+=(y1-y0); x0 = x1; y0 = y1; } - if ((disp.button()&1) && (disp.button()&2)) { - init_pose = true; disp.set_button(); x0 = x1; y0 = y1; - pose = CImg(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0); - } - } else if (clicked) { x0 = x1; y0 = y1; clicked = false; redraw = true; } - - switch (key = disp.key()) { -#if cimg_OS!=2 - case cimg::keyCTRLRIGHT : -#endif - case 0 : case cimg::keyCTRLLEFT : key = 0; break; - case cimg::keyD: if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), - CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). - _is_resized = true; - disp.set_key(key,false); key = 0; - } break; - case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; - disp.set_key(key,false); key = 0; - } break; - case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true; - disp.set_key(key,false); key = 0; - } break; - case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - if (!ns_width || !ns_height || - ns_width>(unsigned int)disp.screen_width() || ns_height>(unsigned int)disp.screen_height()) { - ns_width = disp.screen_width()*3U/4; - ns_height = disp.screen_height()*3U/4; - } - if (disp.is_fullscreen()) disp.resize(ns_width,ns_height,false); - else { - ns_width = (unsigned int)disp.width(); ns_height = disp.height(); - disp.resize(disp.screen_width(),disp.screen_height(),false); - } - disp.toggle_fullscreen()._is_resized = true; - disp.set_key(key,false); key = 0; - } break; - case cimg::keyT : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - // Switch single/double-sided primitives. - if (--_is_double_sided==-2) _is_double_sided = 1; - if (_is_double_sided>=0) reverse_primitives.assign(); - else primitives.get_reverse_object3d().move_to(reverse_primitives); - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyZ : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Enable/disable Z-buffer - if (zbuffer) zbuffer.assign(); - else zbuffer.assign(visu0.width(),visu0.height(),1,1,0); - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyA : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Show/hide 3d axes. - ndisplay_axes = !ndisplay_axes; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyF1 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to points. - nrender_motion = (nrender_static==0 && nrender_motion!=0)?0:-1; nrender_static = 0; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyF2 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to lines. - nrender_motion = (nrender_static==1 && nrender_motion!=1)?1:-1; nrender_static = 1; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyF3 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat. - nrender_motion = (nrender_static==2 && nrender_motion!=2)?2:-1; nrender_static = 2; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyF4 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat-shaded. - nrender_motion = (nrender_static==3 && nrender_motion!=3)?3:-1; nrender_static = 3; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyF5 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - // Set rendering mode to gouraud-shaded. - nrender_motion = (nrender_static==4 && nrender_motion!=4)?4:-1; nrender_static = 4; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyF6 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to phong-shaded. - nrender_motion = (nrender_static==5 && nrender_motion!=5)?5:-1; nrender_static = 5; - disp.set_key(key,false); key = 0; redraw = true; - } break; - case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save snapshot - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu).draw_text(0,0," Saving snapshot... ", - foreground_color._data,background_color._data,0.7f,13).display(disp); - visu.save(filename); - (+visu).draw_text(0,0," Snapshot '%s' saved. ", - foreground_color._data,background_color._data,0.7f,13,filename).display(disp); - disp.set_key(key,false); key = 0; - } break; - case cimg::keyG : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .off file - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.off",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu).draw_text(0,0," Saving object... ", - foreground_color._data,background_color._data,0.7f,13).display(disp); - vertices.save_off(reverse_primitives?reverse_primitives:primitives,colors,filename); - (+visu).draw_text(0,0," Object '%s' saved. ", - foreground_color._data,background_color._data,0.7f,13,filename).display(disp); - disp.set_key(key,false); key = 0; - } break; - case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .cimg file - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { -#ifdef cimg_use_zlib - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); -#else - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); -#endif - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu).draw_text(0,0," Saving object... ", - foreground_color._data,background_color._data,0.7f,13).display(disp); - vertices.get_object3dtoCImg3d(reverse_primitives?reverse_primitives:primitives,colors,opacities). - save(filename); - (+visu).draw_text(0,0," Object '%s' saved. ", - foreground_color._data,background_color._data,0.7f,13,filename).display(disp); - disp.set_key(key,false); key = 0; - } break; -#ifdef cimg_use_board - case cimg::keyP : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .EPS file - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.eps",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu).draw_text(0,0," Saving EPS snapshot... ", - foreground_color._data,background_color._data,0.7f,13).display(disp); - LibBoard::Board board; - (+visu)._draw_object3d(&board,zbuffer.fill(0), - Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, - rotated_vertices,reverse_primitives?reverse_primitives:primitives, - colors,opacities,clicked?nrender_motion:nrender_static, - _is_double_sided==1,focale, - visu.width()/2.0f+light_x,visu.height()/2.0f+light_y,light_z+Zoff, - specular_lightness,specular_shininess, - sprite_scale); - board.saveEPS(filename); - (+visu).draw_text(0,0," Object '%s' saved. ", - foreground_color._data,background_color._data,0.7f,13,filename).display(disp); - disp.set_key(key,false); key = 0; - } break; - case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .SVG file - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.svg",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu).draw_text(0,0," Saving SVG snapshot... ", - foreground_color._data,background_color._data,0.7f,13).display(disp); - LibBoard::Board board; - (+visu)._draw_object3d(&board,zbuffer.fill(0), - Xoff + visu._width/2.0f,Yoff + visu._height/2.0f,Zoff, - rotated_vertices,reverse_primitives?reverse_primitives:primitives, - colors,opacities,clicked?nrender_motion:nrender_static, - _is_double_sided==1,focale, - visu.width()/2.0f+light_x,visu.height()/2.0f+light_y,light_z+Zoff, - specular_lightness,specular_shininess, - sprite_scale); - board.saveSVG(filename); - (+visu).draw_text(0,0," Object '%s' saved. ", - foreground_color._data,background_color._data,0.7f,13,filename).display(disp); - disp.set_key(key,false); key = 0; - } break; -#endif - } - if (disp.is_resized()) { - disp.resize(false); visu0 = get_resize(disp,1); - if (zbuffer) zbuffer.assign(disp.width(),disp.height()); - redraw = true; - } - } - if (pose_matrix) { - std::memcpy(pose_matrix,pose._data,12*sizeof(float)); - pose_matrix[12] = Xoff; pose_matrix[13] = Yoff; pose_matrix[14] = Zoff; pose_matrix[15] = sprite_scale; - } - disp.set_button().set_key(key); - return *this; - } - - //! Display 1d graph in an interactive window. - /** - \param disp Display window. - \param plot_type Plot type. Can be { 0=points | 1=segments | 2=splines | 3=bars }. - \param vertex_type Vertex type. - \param labelx Title for the horizontal axis, as a C-string. - \param xmin Minimum value along the X-axis. - \param xmax Maximum value along the X-axis. - \param labely Title for the vertical axis, as a C-string. - \param ymin Minimum value along the X-axis. - \param ymax Maximum value along the X-axis. - **/ - const CImg& display_graph(CImgDisplay &disp, - const unsigned int plot_type=1, const unsigned int vertex_type=1, - const char *const labelx=0, const double xmin=0, const double xmax=0, - const char *const labely=0, const double ymin=0, const double ymax=0) const { - return _display_graph(disp,0,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax); - } - - //! Display 1d graph in an interactive window \overloading. - const CImg& display_graph(const char *const title=0, - const unsigned int plot_type=1, const unsigned int vertex_type=1, - const char *const labelx=0, const double xmin=0, const double xmax=0, - const char *const labely=0, const double ymin=0, const double ymax=0) const { - CImgDisplay disp; - return _display_graph(disp,title,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax); - } - - const CImg& _display_graph(CImgDisplay &disp, const char *const title=0, - const unsigned int plot_type=1, const unsigned int vertex_type=1, - const char *const labelx=0, const double xmin=0, const double xmax=0, - const char *const labely=0, const double ymin=0, const double ymax=0) const { - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "display_graph(): Empty instance.", - cimg_instance); - if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0). - set_title(title?"%s":"CImg<%s>",title?title:pixel_type()); - const unsigned long siz = (unsigned long)_width*_height*_depth, siz1 = cimg::max(1U,siz-1); - const unsigned int old_normalization = disp.normalization(); - disp.show().flush()._normalization = 0; - - double y0 = ymin, y1 = ymax, nxmin = xmin, nxmax = xmax; - if (nxmin==nxmax) { nxmin = 0; nxmax = siz1; } - int x0 = 0, x1 = width()*height()*depth() - 1, key = 0; - - for (bool reset_view = true; !key && !disp.is_closed(); ) { - if (reset_view) { x0 = 0; x1 = width()*height()*depth()-1; y0 = ymin; y1 = ymax; reset_view = false; } - CImg zoom(x1-x0+1,1,1,spectrum()); - cimg_forC(*this,c) zoom.get_shared_channel(c) = CImg(data(x0,0,0,c),x1-x0+1,1,1,1,true); - if (y0==y1) { y0 = zoom.min_max(y1); const double dy = y1 - y0; y0-=dy/20; y1+=dy/20; } - if (y0==y1) { --y0; ++y1; } - - const CImg selection = zoom.get_select_graph(disp,plot_type,vertex_type, - labelx, - nxmin + x0*(nxmax-nxmin)/siz1, - nxmin + x1*(nxmax-nxmin)/siz1, - labely,y0,y1); - const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y(); - if (selection[0]>=0) { - if (selection[2]<0) reset_view = true; - else { - x1 = x0 + selection[2]; x0+=selection[0]; - if (selection[1]>=0 && selection[3]>=0) { - y0 = y1 - selection[3]*(y1-y0)/(disp.height()-32); - y1-=selection[1]*(y1-y0)/(disp.height()-32); - } - } - } else { - bool go_in = false, go_out = false, go_left = false, go_right = false, go_up = false, go_down = false; - switch (key = disp.key()) { - case cimg::keyHOME : reset_view = true; key = 0; disp.set_key(); break; - case cimg::keyPADADD : go_in = true; go_out = false; key = 0; disp.set_key(); break; - case cimg::keyPADSUB : go_out = true; go_in = false; key = 0; disp.set_key(); break; - case cimg::keyARROWLEFT : case cimg::keyPAD4 : go_left = true; go_right = false; key = 0; disp.set_key(); - break; - case cimg::keyARROWRIGHT : case cimg::keyPAD6 : go_right = true; go_left = false; key = 0; disp.set_key(); - break; - case cimg::keyARROWUP : case cimg::keyPAD8 : go_up = true; go_down = false; key = 0; disp.set_key(); break; - case cimg::keyARROWDOWN : case cimg::keyPAD2 : go_down = true; go_up = false; key = 0; disp.set_key(); break; - case cimg::keyPAD7 : go_left = true; go_up = true; key = 0; disp.set_key(); break; - case cimg::keyPAD9 : go_right = true; go_up = true; key = 0; disp.set_key(); break; - case cimg::keyPAD1 : go_left = true; go_down = true; key = 0; disp.set_key(); break; - case cimg::keyPAD3 : go_right = true; go_down = true; key = 0; disp.set_key(); break; - } - if (disp.wheel()) { - if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) go_out = !(go_in = disp.wheel()>0); - else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) go_left = !(go_right = disp.wheel()>0); - else go_up = !(go_down = disp.wheel()<0); - key = 0; - } - - if (go_in) { - const int - xsiz = x1 - x0, - mx = (mouse_x-16)*xsiz/(disp.width()-32), - cx = x0 + (mx<0?0:(mx>=xsiz?xsiz:mx)); - if (x1-x0>4) { - x0 = cx - 7*(cx-x0)/8; x1 = cx + 7*(x1-cx)/8; - if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - const double - ysiz = y1 - y0, - my = (mouse_y-16)*ysiz/(disp.height()-32), - cy = y1 - (my<0?0:(my>=ysiz?ysiz:my)); - y0 = cy - 7*(cy-y0)/8; y1 = cy + 7*(y1-cy)/8; - } else y0 = y1 = 0; - } - } - if (go_out) { - if (x0>0 || x1<(int)siz1) { - const int delta_x = (x1-x0)/8, ndelta_x = delta_x?delta_x:(siz>1?1:0); - const double ndelta_y = (y1-y0)/8; - x0-=ndelta_x; x1+=ndelta_x; - y0-=ndelta_y; y1+=ndelta_y; - if (x0<0) { x1-=x0; x0 = 0; if (x1>=(int)siz) x1 = (int)siz1; } - if (x1>=(int)siz) { x0-=(x1-siz1); x1 = (int)siz1; if (x0<0) x0 = 0; } - } - } - if (go_left) { - const int delta = (x1-x0)/5, ndelta = delta?delta:1; - if (x0-ndelta>=0) { x0-=ndelta; x1-=ndelta; } - else { x1-=x0; x0 = 0; } - go_left = false; - } - if (go_right) { - const int delta = (x1-x0)/5, ndelta = delta?delta:1; - if (x1+ndelta<(int)siz) { x0+=ndelta; x1+=ndelta; } - else { x0+=(siz1-x1); x1 = siz1; } - go_right = false; - } - if (go_up) { - const double delta = (y1-y0)/10, ndelta = delta?delta:1; - y0+=ndelta; y1+=ndelta; - go_up = false; - } - if (go_down) { - const double delta = (y1-y0)/10, ndelta = delta?delta:1; - y0-=ndelta; y1-=ndelta; - go_down = false; - } - } - } - disp._normalization = old_normalization; - return *this; - } - - //! Save image as a file. - /** - \param filename Filename, as a C-string. - \param number When positive, represents an index added to the filename. Otherwise, no number is added. - \param digits Number of digits used for adding the number to the filename. - \note - - The used file format is defined by the file extension in the filename \p filename. - - Parameter \p number can be used to add a 6-digit number to the filename before saving. - - **/ - const CImg& save(const char *const filename, const int number=-1, const unsigned int digits=6) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save(): Specified filename is (null).", - cimg_instance); - // Do not test for empty instances, since .cimg format is able to manage empty instances. - const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.'); - const char *const ext = cimg::split_filename(filename); - char nfilename[1024] = { 0 }; - const char *const fn = is_stdout?filename:(number>=0)?cimg::number_filename(filename,number,digits,nfilename): - filename; - -#ifdef cimg_save_plugin - cimg_save_plugin(fn); -#endif -#ifdef cimg_save_plugin1 - cimg_save_plugin1(fn); -#endif -#ifdef cimg_save_plugin2 - cimg_save_plugin2(fn); -#endif -#ifdef cimg_save_plugin3 - cimg_save_plugin3(fn); -#endif -#ifdef cimg_save_plugin4 - cimg_save_plugin4(fn); -#endif -#ifdef cimg_save_plugin5 - cimg_save_plugin5(fn); -#endif -#ifdef cimg_save_plugin6 - cimg_save_plugin6(fn); -#endif -#ifdef cimg_save_plugin7 - cimg_save_plugin7(fn); -#endif -#ifdef cimg_save_plugin8 - cimg_save_plugin8(fn); -#endif - // Ascii formats - if (!cimg::strcasecmp(ext,"asc")) return save_ascii(fn); - else if (!cimg::strcasecmp(ext,"dlm") || - !cimg::strcasecmp(ext,"txt")) return save_dlm(fn); - else if (!cimg::strcasecmp(ext,"cpp") || - !cimg::strcasecmp(ext,"hpp") || - !cimg::strcasecmp(ext,"h") || - !cimg::strcasecmp(ext,"c")) return save_cpp(fn); - - // 2d binary formats - else if (!cimg::strcasecmp(ext,"bmp")) return save_bmp(fn); - else if (!cimg::strcasecmp(ext,"jpg") || - !cimg::strcasecmp(ext,"jpeg") || - !cimg::strcasecmp(ext,"jpe") || - !cimg::strcasecmp(ext,"jfif") || - !cimg::strcasecmp(ext,"jif")) return save_jpeg(fn); - else if (!cimg::strcasecmp(ext,"rgb")) return save_rgb(fn); - else if (!cimg::strcasecmp(ext,"rgba")) return save_rgba(fn); - else if (!cimg::strcasecmp(ext,"png")) return save_png(fn); - else if (!cimg::strcasecmp(ext,"pgm") || - !cimg::strcasecmp(ext,"ppm") || - !cimg::strcasecmp(ext,"pnm")) return save_pnm(fn); - else if (!cimg::strcasecmp(ext,"pnk")) return save_pnk(fn); - else if (!cimg::strcasecmp(ext,"pfm")) return save_pfm(fn); - else if (!cimg::strcasecmp(ext,"exr")) return save_exr(fn); - else if (!cimg::strcasecmp(ext,"tif") || - !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn); - - // 3d binary formats - else if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true); - else if (!cimg::strcasecmp(ext,"cimg") || !*ext) return save_cimg(fn,false); - else if (!cimg::strcasecmp(ext,"dcm")) return save_medcon_external(fn); - else if (!cimg::strcasecmp(ext,"hdr") || - !cimg::strcasecmp(ext,"nii")) return save_analyze(fn); - else if (!cimg::strcasecmp(ext,"inr")) return save_inr(fn); - else if (!cimg::strcasecmp(ext,"mnc")) return save_minc2(fn); - else if (!cimg::strcasecmp(ext,"pan")) return save_pandore(fn); - else if (!cimg::strcasecmp(ext,"raw")) return save_raw(fn); - - // Archive files - else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn); - - // Image sequences - else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,true); - else if (!cimg::strcasecmp(ext,"avi") || - !cimg::strcasecmp(ext,"mov") || - !cimg::strcasecmp(ext,"asf") || - !cimg::strcasecmp(ext,"divx") || - !cimg::strcasecmp(ext,"flv") || - !cimg::strcasecmp(ext,"mpg") || - !cimg::strcasecmp(ext,"m1v") || - !cimg::strcasecmp(ext,"m2v") || - !cimg::strcasecmp(ext,"m4v") || - !cimg::strcasecmp(ext,"mjp") || - !cimg::strcasecmp(ext,"mp4") || - !cimg::strcasecmp(ext,"mkv") || - !cimg::strcasecmp(ext,"mpe") || - !cimg::strcasecmp(ext,"movie") || - !cimg::strcasecmp(ext,"ogm") || - !cimg::strcasecmp(ext,"ogg") || - !cimg::strcasecmp(ext,"ogv") || - !cimg::strcasecmp(ext,"qt") || - !cimg::strcasecmp(ext,"rm") || - !cimg::strcasecmp(ext,"vob") || - !cimg::strcasecmp(ext,"wmv") || - !cimg::strcasecmp(ext,"xvid") || - !cimg::strcasecmp(ext,"mpeg")) return save_ffmpeg(fn); - return save_other(fn); - } - - //! Save image as an ascii file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_ascii(const char *const filename) const { - return _save_ascii(0,filename); - } - - //! Save image as an ascii file \overloading. - const CImg& save_ascii(std::FILE *const file) const { - return _save_ascii(file,0); - } - - const CImg& _save_ascii(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_ascii(): Specified filename is (null).", - cimg_instance); - std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); - std::fprintf(nfile,"%u %u %u %u\n",_width,_height,_depth,_spectrum); - const T* ptrs = _data; - cimg_forYZC(*this,y,z,c) { - cimg_forX(*this,x) std::fprintf(nfile,"%.16g ",(double)*(ptrs++)); - std::fputc('\n',nfile); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a .cpp source file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_cpp(const char *const filename) const { - return _save_cpp(0,filename); - } - - //! Save image as a .cpp source file \overloading. - const CImg& save_cpp(std::FILE *const file) const { - return _save_cpp(file,0); - } - - const CImg& _save_cpp(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_cpp(): Specified filename is (null).", - cimg_instance); - std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); - char varname[1024] = { 0 }; - if (filename) std::sscanf(cimg::basename(filename),"%1023[a-zA-Z0-9_]",varname); - if (!*varname) cimg_snprintf(varname,sizeof(varname),"unnamed"); - std::fprintf(nfile, - "/* Define image '%s' of size %ux%ux%ux%u and type '%s' */\n" - "%s data_%s[] = { %s\n ", - varname,_width,_height,_depth,_spectrum,pixel_type(),pixel_type(),varname, - is_empty()?"};":""); - if (!is_empty()) for (unsigned long off = 0, siz = size()-1; off<=siz; ++off) { - std::fprintf(nfile,cimg::type::format(),cimg::type::format((*this)[off])); - if (off==siz) std::fprintf(nfile," };\n"); - else if (!((off+1)%16)) std::fprintf(nfile,",\n "); - else std::fprintf(nfile,", "); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a DLM file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_dlm(const char *const filename) const { - return _save_dlm(0,filename); - } - - //! Save image as a DLM file \overloading. - const CImg& save_dlm(std::FILE *const file) const { - return _save_dlm(file,0); - } - - const CImg& _save_dlm(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_dlm(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_depth>1) - cimg::warn(_cimg_instance - "save_dlm(): Instance is volumetric, values along Z will be unrolled in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - if (_spectrum>1) - cimg::warn(_cimg_instance - "save_dlm(): Instance is multispectral, values along C will be unrolled in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); - const T* ptrs = _data; - cimg_forYZC(*this,y,z,c) { - cimg_forX(*this,x) std::fprintf(nfile,"%.16g%s",(double)*(ptrs++),(x==width()-1)?"":","); - std::fputc('\n',nfile); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a BMP file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_bmp(const char *const filename) const { - return _save_bmp(0,filename); - } - - //! Save image as a BMP file \overloading. - const CImg& save_bmp(std::FILE *const file) const { - return _save_bmp(file,0); - } - - const CImg& _save_bmp(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_bmp(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_depth>1) - cimg::warn(_cimg_instance - "save_bmp(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - if (_spectrum>3) - cimg::warn(_cimg_instance - "save_bmp(): Instance is multispectral, only the three first channels will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - unsigned char header[54] = { 0 }, align_buf[4] = { 0 }; - const unsigned int - align = (4 - (3*_width)%4)%4, - buf_size = (3*_width + align)*height(), - file_size = 54 + buf_size; - header[0] = 'B'; header[1] = 'M'; - header[0x02] = file_size&0xFF; - header[0x03] = (file_size>>8)&0xFF; - header[0x04] = (file_size>>16)&0xFF; - header[0x05] = (file_size>>24)&0xFF; - header[0x0A] = 0x36; - header[0x0E] = 0x28; - header[0x12] = _width&0xFF; - header[0x13] = (_width>>8)&0xFF; - header[0x14] = (_width>>16)&0xFF; - header[0x15] = (_width>>24)&0xFF; - header[0x16] = _height&0xFF; - header[0x17] = (_height>>8)&0xFF; - header[0x18] = (_height>>16)&0xFF; - header[0x19] = (_height>>24)&0xFF; - header[0x1A] = 1; - header[0x1B] = 0; - header[0x1C] = 24; - header[0x1D] = 0; - header[0x22] = buf_size&0xFF; - header[0x23] = (buf_size>>8)&0xFF; - header[0x24] = (buf_size>>16)&0xFF; - header[0x25] = (buf_size>>24)&0xFF; - header[0x27] = 0x1; - header[0x2B] = 0x1; - cimg::fwrite(header,54,nfile); - - const T - *ptr_r = data(0,_height-1,0,0), - *ptr_g = (_spectrum>=2)?data(0,_height-1,0,1):0, - *ptr_b = (_spectrum>=3)?data(0,_height-1,0,2):0; - - switch (_spectrum) { - case 1 : { - cimg_forY(*this,y) { - cimg_forX(*this,x) { - const unsigned char val = (unsigned char)*(ptr_r++); - std::fputc(val,nfile); std::fputc(val,nfile); std::fputc(val,nfile); - } - cimg::fwrite(align_buf,align,nfile); - ptr_r-=2*_width; - } - } break; - case 2 : { - cimg_forY(*this,y) { - cimg_forX(*this,x) { - std::fputc(0,nfile); - std::fputc((unsigned char)(*(ptr_g++)),nfile); - std::fputc((unsigned char)(*(ptr_r++)),nfile); - } - cimg::fwrite(align_buf,align,nfile); - ptr_r-=2*_width; ptr_g-=2*_width; - } - } break; - default : { - cimg_forY(*this,y) { - cimg_forX(*this,x) { - std::fputc((unsigned char)(*(ptr_b++)),nfile); - std::fputc((unsigned char)(*(ptr_g++)),nfile); - std::fputc((unsigned char)(*(ptr_r++)),nfile); - } - cimg::fwrite(align_buf,align,nfile); - ptr_r-=2*_width; ptr_g-=2*_width; ptr_b-=2*_width; - } - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a JPEG file. - /** - \param filename Filename, as a C-string. - \param quality Image quality (in %) - **/ - const CImg& save_jpeg(const char *const filename, const unsigned int quality=100) const { - return _save_jpeg(0,filename,quality); - } - - //! Save image as a JPEG file \overloading. - const CImg& save_jpeg(std::FILE *const file, const unsigned int quality=100) const { - return _save_jpeg(file,0,quality); - } - - const CImg& _save_jpeg(std::FILE *const file, const char *const filename, const unsigned int quality) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_jpeg(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_depth>1) - cimg::warn(_cimg_instance - "save_jpeg(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - -#ifndef cimg_use_jpeg - if (!file) return save_other(filename,quality); - else throw CImgIOException(_cimg_instance - "save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.", - cimg_instance); -#else - unsigned int dimbuf = 0; - J_COLOR_SPACE colortype = JCS_RGB; - - switch(_spectrum) { - case 1 : dimbuf = 1; colortype = JCS_GRAYSCALE; break; - case 2 : dimbuf = 3; colortype = JCS_RGB; break; - case 3 : dimbuf = 3; colortype = JCS_RGB; break; - default : dimbuf = 4; colortype = JCS_CMYK; break; - } - - // Call libjpeg functions - struct jpeg_compress_struct cinfo; - struct jpeg_error_mgr jerr; - cinfo.err = jpeg_std_error(&jerr); - jpeg_create_compress(&cinfo); - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - jpeg_stdio_dest(&cinfo,nfile); - cinfo.image_width = _width; - cinfo.image_height = _height; - cinfo.input_components = dimbuf; - cinfo.in_color_space = colortype; - jpeg_set_defaults(&cinfo); - jpeg_set_quality(&cinfo,quality<100?quality:100,TRUE); - jpeg_start_compress(&cinfo,TRUE); - - JSAMPROW row_pointer[1]; - CImg buffer((unsigned long)_width*dimbuf); - - while (cinfo.next_scanline& save_magick(const char *const filename, const unsigned int bytes_per_pixel=0) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_magick(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifdef cimg_use_magick - double stmin, stmax = (double)max_min(stmin); - if (_depth>1) - cimg::warn(_cimg_instance - "save_magick(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename); - - if (_spectrum>3) - cimg::warn(_cimg_instance - "save_magick(): Instance is multispectral, only the three first channels will be " - "saved in file '%s'.", - cimg_instance, - filename); - - if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536) - cimg::warn(_cimg_instance - "save_magick(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.", - cimg_instance, - filename,stmin,stmax); - - Magick::Image image(Magick::Geometry(_width,_height),"black"); - image.type(Magick::TrueColorType); - image.depth(bytes_per_pixel?(8*bytes_per_pixel):(stmax>=256?16:8)); - const T - *ptr_r = data(0,0,0,0), - *ptr_g = _spectrum>1?data(0,0,0,1):0, - *ptr_b = _spectrum>2?data(0,0,0,2):0; - Magick::PixelPacket *pixels = image.getPixels(0,0,_width,_height); - switch (_spectrum) { - case 1 : // Scalar images - for (unsigned long off = (unsigned long)_width*_height; off; --off) { - pixels->red = pixels->green = pixels->blue = (Magick::Quantum)*(ptr_r++); - ++pixels; - } - break; - case 2 : // RG images - for (unsigned long off = (unsigned long)_width*_height; off; --off) { - pixels->red = (Magick::Quantum)*(ptr_r++); - pixels->green = (Magick::Quantum)*(ptr_g++); - pixels->blue = 0; ++pixels; - } - break; - default : // RGB images - for (unsigned long off = (unsigned long)_width*_height; off; --off) { - pixels->red = (Magick::Quantum)*(ptr_r++); - pixels->green = (Magick::Quantum)*(ptr_g++); - pixels->blue = (Magick::Quantum)*(ptr_b++); - ++pixels; - } - } - image.syncPixels(); - image.write(filename); - return *this; -#else - cimg::unused(bytes_per_pixel); - throw CImgIOException(_cimg_instance - "save_magick(): Unable to save file '%s' unless libMagick++ is enabled.", - cimg_instance, - filename); -#endif - } - - //! Save image as a PNG file. - /** - \param filename Filename, as a C-string. - \param bytes_per_pixel Force the number of bytes per pixels for the saving, when possible. - **/ - const CImg& save_png(const char *const filename, const unsigned int bytes_per_pixel=0) const { - return _save_png(0,filename,bytes_per_pixel); - } - - //! Save image as a PNG file \overloading. - const CImg& save_png(std::FILE *const file, const unsigned int bytes_per_pixel=0) const { - return _save_png(file,0,bytes_per_pixel); - } - - const CImg& _save_png(std::FILE *const file, const char *const filename, - const unsigned int bytes_per_pixel=0) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_png(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - -#ifndef cimg_use_png - cimg::unused(bytes_per_pixel); - if (!file) return save_other(filename); - else throw CImgIOException(_cimg_instance - "save_png(): Unable to save data in '(*FILE)' unless libpng is enabled.", - cimg_instance); -#else - const char *volatile nfilename = filename; // two 'volatile' here to remove a g++ warning due to 'setjmp'. - std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"wb"); - volatile double stmin, stmax = (double)max_min(stmin); - - if (_depth>1) - cimg::warn(_cimg_instance - "save_png(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename); - - if (_spectrum>4) - cimg::warn(_cimg_instance - "save_png(): Instance is multispectral, only the three first channels will be saved in file '%s'.", - cimg_instance, - filename); - - if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536) - cimg::warn(_cimg_instance - "save_png(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.", - cimg_instance, - filename,stmin,stmax); - - // Setup PNG structures for write - png_voidp user_error_ptr = 0; - png_error_ptr user_error_fn = 0, user_warning_fn = 0; - png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,user_error_ptr, user_error_fn, - user_warning_fn); - if(!png_ptr){ - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "save_png(): Failed to initialize 'png_ptr' structure when saving file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - png_infop info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - png_destroy_write_struct(&png_ptr,(png_infopp)0); - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "save_png(): Failed to initialize 'info_ptr' structure when saving file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - if (setjmp(png_jmpbuf(png_ptr))) { - png_destroy_write_struct(&png_ptr, &info_ptr); - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - png_init_io(png_ptr, nfile); - - const int bit_depth = bytes_per_pixel?(bytes_per_pixel*8):(stmax>=256?16:8); - - int color_type; - switch (spectrum()) { - case 1 : color_type = PNG_COLOR_TYPE_GRAY; break; - case 2 : color_type = PNG_COLOR_TYPE_GRAY_ALPHA; break; - case 3 : color_type = PNG_COLOR_TYPE_RGB; break; - default : color_type = PNG_COLOR_TYPE_RGB_ALPHA; - } - const int interlace_type = PNG_INTERLACE_NONE; - const int compression_type = PNG_COMPRESSION_TYPE_DEFAULT; - const int filter_method = PNG_FILTER_TYPE_DEFAULT; - png_set_IHDR(png_ptr,info_ptr,_width,_height,bit_depth,color_type,interlace_type,compression_type,filter_method); - png_write_info(png_ptr,info_ptr); - const int byte_depth = bit_depth>>3; - const int numChan = spectrum()>4?4:spectrum(); - const int pixel_bit_depth_flag = numChan * (bit_depth-1); - - // Allocate Memory for Image Save and Fill pixel data - png_bytep *const imgData = new png_byte*[_height]; - for (unsigned int row = 0; row<_height; ++row) imgData[row] = new png_byte[byte_depth*numChan*_width]; - const T *pC0 = data(0,0,0,0); - switch (pixel_bit_depth_flag) { - case 7 : { // Gray 8-bit - cimg_forY(*this,y) { - unsigned char *ptrd = imgData[y]; - cimg_forX(*this,x) *(ptrd++) = (unsigned char)*(pC0++); - } - } break; - case 14 : { // Gray w/ Alpha 8-bit - const T *pC1 = data(0,0,0,1); - cimg_forY(*this,y) { - unsigned char *ptrd = imgData[y]; - cimg_forX(*this,x) { - *(ptrd++) = (unsigned char)*(pC0++); - *(ptrd++) = (unsigned char)*(pC1++); - } - } - } break; - case 21 : { // RGB 8-bit - const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2); - cimg_forY(*this,y) { - unsigned char *ptrd = imgData[y]; - cimg_forX(*this,x) { - *(ptrd++) = (unsigned char)*(pC0++); - *(ptrd++) = (unsigned char)*(pC1++); - *(ptrd++) = (unsigned char)*(pC2++); - } - } - } break; - case 28 : { // RGB x/ Alpha 8-bit - const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3); - cimg_forY(*this,y){ - unsigned char *ptrd = imgData[y]; - cimg_forX(*this,x){ - *(ptrd++) = (unsigned char)*(pC0++); - *(ptrd++) = (unsigned char)*(pC1++); - *(ptrd++) = (unsigned char)*(pC2++); - *(ptrd++) = (unsigned char)*(pC3++); - } - } - } break; - case 15 : { // Gray 16-bit - cimg_forY(*this,y){ - unsigned short *ptrd = (unsigned short*)(imgData[y]); - cimg_forX(*this,x) *(ptrd++) = (unsigned short)*(pC0++); - if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],_width); - } - } break; - case 30 : { // Gray w/ Alpha 16-bit - const T *pC1 = data(0,0,0,1); - cimg_forY(*this,y){ - unsigned short *ptrd = (unsigned short*)(imgData[y]); - cimg_forX(*this,x) { - *(ptrd++) = (unsigned short)*(pC0++); - *(ptrd++) = (unsigned short)*(pC1++); - } - if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],2*_width); - } - } break; - case 45 : { // RGB 16-bit - const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2); - cimg_forY(*this,y) { - unsigned short *ptrd = (unsigned short*)(imgData[y]); - cimg_forX(*this,x) { - *(ptrd++) = (unsigned short)*(pC0++); - *(ptrd++) = (unsigned short)*(pC1++); - *(ptrd++) = (unsigned short)*(pC2++); - } - if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],3*_width); - } - } break; - case 60 : { // RGB w/ Alpha 16-bit - const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3); - cimg_forY(*this,y) { - unsigned short *ptrd = (unsigned short*)(imgData[y]); - cimg_forX(*this,x) { - *(ptrd++) = (unsigned short)*(pC0++); - *(ptrd++) = (unsigned short)*(pC1++); - *(ptrd++) = (unsigned short)*(pC2++); - *(ptrd++) = (unsigned short)*(pC3++); - } - if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],4*_width); - } - } break; - default : - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimg_instance - "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.", - cimg_instance, - nfilename?nfilename:"(FILE*)"); - } - png_write_image(png_ptr,imgData); - png_write_end(png_ptr,info_ptr); - png_destroy_write_struct(&png_ptr, &info_ptr); - - // Deallocate Image Write Memory - cimg_forY(*this,n) delete[] imgData[n]; - delete[] imgData; - - if (!file) cimg::fclose(nfile); - return *this; -#endif - } - - //! Save image as a PNM file. - /** - \param filename Filename, as a C-string. - \param bytes_per_pixel Force the number of bytes per pixels for the saving. - **/ - const CImg& save_pnm(const char *const filename, const unsigned int bytes_per_pixel=0) const { - return _save_pnm(0,filename,bytes_per_pixel); - } - - //! Save image as a PNM file \overloading. - const CImg& save_pnm(std::FILE *const file, const unsigned int bytes_per_pixel=0) const { - return _save_pnm(file,0,bytes_per_pixel); - } - - const CImg& _save_pnm(std::FILE *const file, const char *const filename, - const unsigned int bytes_per_pixel=0) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_pnm(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - - double stmin, stmax = (double)max_min(stmin); - if (_depth>1) - cimg::warn(_cimg_instance - "save_pnm(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - if (_spectrum>3) - cimg::warn(_cimg_instance - "save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536) - cimg::warn(_cimg_instance - "save_pnm(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.", - cimg_instance, - stmin,stmax,filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const T - *ptr_r = data(0,0,0,0), - *ptr_g = (_spectrum>=2)?data(0,0,0,1):0, - *ptr_b = (_spectrum>=3)?data(0,0,0,2):0; - const unsigned long buf_size = cimg::min(1024*1024UL,_width*_height*(_spectrum==1?1UL:3UL)); - - std::fprintf(nfile,"P%c\n%u %u\n%u\n", - (_spectrum==1?'5':'6'),_width,_height,stmax<256?255:(stmax<4096?4095:65535)); - - switch (_spectrum) { - case 1 : { // Scalar image - if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PGM 8 bits - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size); - unsigned char *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr_r++); - cimg::fwrite(buf._data,N,nfile); - to_write-=N; - } - } else { // Binary PGM 16 bits - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size); - unsigned short *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) *(ptrd++) = (unsigned short)*(ptr_r++); - if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); - cimg::fwrite(buf._data,N,nfile); - to_write-=N; - } - } - } break; - case 2 : { // RG image - if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); - unsigned char *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) { - *(ptrd++) = (unsigned char)*(ptr_r++); - *(ptrd++) = (unsigned char)*(ptr_g++); - *(ptrd++) = 0; - } - cimg::fwrite(buf._data,3*N,nfile); - to_write-=N; - } - } else { // Binary PPM 16 bits - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); - unsigned short *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) { - *(ptrd++) = (unsigned short)*(ptr_r++); - *(ptrd++) = (unsigned short)*(ptr_g++); - *(ptrd++) = 0; - } - if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); - cimg::fwrite(buf._data,3*N,nfile); - to_write-=N; - } - } - } break; - default : { // RGB image - if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); - unsigned char *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) { - *(ptrd++) = (unsigned char)*(ptr_r++); - *(ptrd++) = (unsigned char)*(ptr_g++); - *(ptrd++) = (unsigned char)*(ptr_b++); - } - cimg::fwrite(buf._data,3*N,nfile); - to_write-=N; - } - } else { // Binary PPM 16 bits - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size/3); - unsigned short *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) { - *(ptrd++) = (unsigned short)*(ptr_r++); - *(ptrd++) = (unsigned short)*(ptr_g++); - *(ptrd++) = (unsigned short)*(ptr_b++); - } - if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); - cimg::fwrite(buf._data,3*N,nfile); - to_write-=N; - } - } - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a PNK file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_pnk(const char *const filename) const { - return _save_pnk(0,filename); - } - - //! Save image as a PNK file \overloading. - const CImg& save_pnk(std::FILE *const file) const { - return _save_pnk(file,0); - } - - const CImg& _save_pnk(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_pnk(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_spectrum>1) - cimg::warn(_cimg_instance - "save_pnk(): Instance is multispectral, only the first channel will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - const unsigned long buf_size = cimg::min(1024*1024LU,_width*_height*_depth); - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const T *ptr = data(0,0,0,0); - - if (!cimg::type::is_float() && sizeof(T)==1 && _depth<2) // Can be saved as regular PNM file. - _save_pnm(file,filename,0); - else if (!cimg::type::is_float() && sizeof(T)==1) { // Save as extended P5 file: Binary byte-valued 3d. - std::fprintf(nfile,"P5\n%u %u %u\n255\n",_width,_height,_depth); - CImg buf(buf_size); - for (long to_write = (long)_width*_height*_depth; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size); - unsigned char *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr++); - cimg::fwrite(buf._data,N,nfile); - to_write-=N; - } - } else if (!cimg::type::is_float()) { // Save as P8: Binary int32-valued 3d. - if (_depth>1) std::fprintf(nfile,"P8\n%u %u %u\n%d\n",_width,_height,_depth,(int)max()); - else std::fprintf(nfile,"P8\n%u %u\n%d\n",_width,_height,(int)max()); - CImg buf(buf_size); - for (long to_write = (long)_width*_height*_depth; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size); - int *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) *(ptrd++) = (int)*(ptr++); - cimg::fwrite(buf._data,N,nfile); - to_write-=N; - } - } else { // Save as P9: Binary float-valued 3d. - if (_depth>1) std::fprintf(nfile,"P9\n%u %u %u\n%g\n",_width,_height,_depth,(double)max()); - else std::fprintf(nfile,"P9\n%u %u\n%g\n",_width,_height,(double)max()); - CImg buf(buf_size); - for (long to_write = (long)_width*_height*_depth; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size); - float *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) *(ptrd++) = (float)*(ptr++); - cimg::fwrite(buf._data,N,nfile); - to_write-=N; - } - } - - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a PFM file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_pfm(const char *const filename) const { - return get_mirror('y')._save_pfm(0,filename); - } - - //! Save image as a PFM file \overloading. - const CImg& save_pfm(std::FILE *const file) const { - return get_mirror('y')._save_pfm(file,0); - } - - const CImg& _save_pfm(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_pfm(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_depth>1) - cimg::warn(_cimg_instance - "save_pfm(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - if (_spectrum>3) - cimg::warn(_cimg_instance - "save_pfm(): image instance is multispectral, only the three first channels will be saved " - "in file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const T - *ptr_r = data(0,0,0,0), - *ptr_g = (_spectrum>=2)?data(0,0,0,1):0, - *ptr_b = (_spectrum>=3)?data(0,0,0,2):0; - const unsigned int buf_size = cimg::min(1024*1024U,_width*_height*(_spectrum==1?1:3)); - - std::fprintf(nfile,"P%c\n%u %u\n1.0\n", - (_spectrum==1?'f':'F'),_width,_height); - - switch (_spectrum) { - case 1 : { // Scalar image - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned long N = cimg::min((unsigned long)to_write,buf_size); - float *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) *(ptrd++) = (float)*(ptr_r++); - if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); - cimg::fwrite(buf._data,N,nfile); - to_write-=N; - } - } break; - case 2 : { // RG image - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned int N = cimg::min((unsigned int)to_write,buf_size/3); - float *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) { - *(ptrd++) = (float)*(ptr_r++); - *(ptrd++) = (float)*(ptr_g++); - *(ptrd++) = 0; - } - if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); - cimg::fwrite(buf._data,3*N,nfile); - to_write-=N; - } - } break; - default : { // RGB image - CImg buf(buf_size); - for (long to_write = (long)_width*_height; to_write>0; ) { - const unsigned int N = cimg::min((unsigned int)to_write,buf_size/3); - float *ptrd = buf._data; - for (unsigned long i = N; i>0; --i) { - *(ptrd++) = (float)*(ptr_r++); - *(ptrd++) = (float)*(ptr_g++); - *(ptrd++) = (float)*(ptr_b++); - } - if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size); - cimg::fwrite(buf._data,3*N,nfile); - to_write-=N; - } - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a RGB file. - /** - \param filename Filename, as a C-string. - **/ - const CImg& save_rgb(const char *const filename) const { - return _save_rgb(0,filename); - } - - //! Save image as a RGB file \overloading. - const CImg& save_rgb(std::FILE *const file) const { - return _save_rgb(file,0); - } - - const CImg& _save_rgb(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_rgb(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_spectrum!=3) - cimg::warn(_cimg_instance - "save_rgb(): image instance has not exactly 3 channels, for file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const unsigned long wh = (unsigned long)_width*_height; - unsigned char *const buffer = new unsigned char[3*wh], *nbuffer = buffer; - const T - *ptr1 = data(0,0,0,0), - *ptr2 = _spectrum>1?data(0,0,0,1):0, - *ptr3 = _spectrum>2?data(0,0,0,2):0; - switch (_spectrum) { - case 1 : { // Scalar image - for (unsigned long k = 0; k& save_rgba(const char *const filename) const { - return _save_rgba(0,filename); - } - - //! Save image as a RGBA file \overloading. - const CImg& save_rgba(std::FILE *const file) const { - return _save_rgba(file,0); - } - - const CImg& _save_rgba(std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_rgba(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if (_spectrum!=4) - cimg::warn(_cimg_instance - "save_rgba(): image instance has not exactly 4 channels, for file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const unsigned long wh = (unsigned long)_width*_height; - unsigned char *const buffer = new unsigned char[4*wh], *nbuffer = buffer; - const T - *ptr1 = data(0,0,0,0), - *ptr2 = _spectrum>1?data(0,0,0,1):0, - *ptr3 = _spectrum>2?data(0,0,0,2):0, - *ptr4 = _spectrum>3?data(0,0,0,3):0; - switch (_spectrum) { - case 1 : { // Scalar images - for (unsigned long k = 0; k{ 1=None | 2=CCITTRLE | 3=CCITTFAX3 | 4=CCITTFAX4 | 5=LZW | 6=JPEG }. - \note - - libtiff support is enabled by defining the precompilation - directive \c cimg_use_tif. - - When libtiff is enabled, 2D and 3D (multipage) several - channel per pixel are supported for - char,uchar,short,ushort,float and \c double pixel types. - - If \c cimg_use_tif is not defined at compilation time the - function uses CImg&save_other(const char*). - **/ - const CImg& save_tiff(const char *const filename, const unsigned int compression_type=0, - const float *const voxel_size=0) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_tiff(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifdef cimg_use_tiff - TIFF *tif = TIFFOpen(filename,"w"); - if (tif) { - cimg_forZ(*this,z) get_slice(z)._save_tiff(tif,z,compression_type,voxel_size); - TIFFClose(tif); - } else throw CImgIOException(_cimg_instance - "save_tiff(): Failed to open file '%s' for writing.", - cimg_instance, - filename); - return *this; -#else - cimg::unused(compression_type,voxel_size); - return save_other(filename); -#endif - } - -#ifdef cimg_use_tiff - -#define _cimg_save_tiff(types,typed,compression_type,voxel_size) if (!std::strcmp(types,pixel_type())) { \ - const typed foo = (typed)0; return _save_tiff(tif,directory,foo,compression_type,voxel_size); } - - // [internal] Save a plane into a tiff file - template - const CImg& _save_tiff(TIFF *tif, const unsigned int directory, const t& pixel_t, - const unsigned int compression_type, - const float *const voxel_size=0) const { - if (is_empty() || !tif || pixel_t) return *this; - const char *const filename = TIFFFileName(tif); - uint32 rowsperstrip = (uint32)-1; - uint16 spp = _spectrum, bpp = sizeof(t)*8, photometric; - if (spp==3 || spp==4) photometric = PHOTOMETRIC_RGB; - else photometric = PHOTOMETRIC_MINISBLACK; - TIFFSetDirectory(tif,directory); - TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,_width); - TIFFSetField(tif,TIFFTAG_IMAGELENGTH,_height); - if (voxel_size) { - const float vx = voxel_size[0], vy = voxel_size[1], _vz = voxel_size[2], vz = _vz>0?_vz:1; - TIFFSetField(tif,TIFFTAG_RESOLUTIONUNIT,RESUNIT_NONE); - TIFFSetField(tif,TIFFTAG_XRESOLUTION,vx/vz); - TIFFSetField(tif,TIFFTAG_YRESOLUTION,vy/vz); - CImg s_description(256); - cimg_snprintf(s_description,s_description._width,"VX=%g VY=%g VZ=%g spacing=%g",vx,vy,vz,vz); - TIFFSetField(tif,TIFFTAG_IMAGEDESCRIPTION,s_description.data()); - } - TIFFSetField(tif,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT); - TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,spp); - if (cimg::type::is_float()) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,3); - else if (cimg::type::min()==0) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,1); - else TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,2); - TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,bpp); - TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG); - TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,photometric); - TIFFSetField(tif,TIFFTAG_COMPRESSION,compression_type?(compression_type-1):COMPRESSION_NONE); - rowsperstrip = TIFFDefaultStripSize(tif,rowsperstrip); - TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,rowsperstrip); - TIFFSetField(tif,TIFFTAG_FILLORDER,FILLORDER_MSB2LSB); - TIFFSetField(tif,TIFFTAG_SOFTWARE,"CImg"); - t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif)); - if (buf) { - for (unsigned int row = 0; row<_height; row+=rowsperstrip) { - uint32 nrow = (row + rowsperstrip>_height?_height-row:rowsperstrip); - tstrip_t strip = TIFFComputeStrip(tif,row,0); - tsize_t i = 0; - for (unsigned int rr = 0; rr& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int compression_type, - const float *const voxel_size) const { - _cimg_save_tiff("bool",unsigned char,compression_type,voxel_size); - _cimg_save_tiff("char",char,compression_type,voxel_size); - _cimg_save_tiff("unsigned char",unsigned char,compression_type,voxel_size); - _cimg_save_tiff("short",short,compression_type,voxel_size); - _cimg_save_tiff("unsigned short",unsigned short,compression_type,voxel_size); - _cimg_save_tiff("int",int,compression_type,voxel_size); - _cimg_save_tiff("unsigned int",unsigned int,compression_type,voxel_size); - _cimg_save_tiff("long",int,compression_type,voxel_size); - _cimg_save_tiff("unsigned long",unsigned int,compression_type,voxel_size); - _cimg_save_tiff("float",float,compression_type,voxel_size); - _cimg_save_tiff("double",float,compression_type,voxel_size); - const char *const filename = TIFFFileName(tif); - throw CImgInstanceException(_cimg_instance - "save_tiff(): Unsupported pixel type '%s' for file '%s'.", - cimg_instance, - pixel_type(),filename?filename:"(FILE*)"); - return *this; - } -#endif - - //! Save image as a MINC2 file. - /** - \param filename Filename, as a C-string. - \param imitate_file If non-zero, reference filename, as a C-string, to borrow header from. - **/ - const CImg& save_minc2(const char *const filename, - const char *const imitate_file=0) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_minc2(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifndef cimg_use_minc2 - cimg::unused(imitate_file); - return save_other(filename); -#else - minc::minc_1_writer wtr; - if (imitate_file) - wtr.open(filename, imitate_file); - else { - minc::minc_info di; - if(width()) di.push_back(minc::dim_info(width(), width()*0.5, -1, minc::dim_info::DIM_X)); - if(height()) di.push_back(minc::dim_info(height(), height()*0.5, -1, minc::dim_info::DIM_Y)); - if(depth()) di.push_back(minc::dim_info(depth(), depth()*0.5, -1, minc::dim_info::DIM_Z)); - if(spectrum()) di.push_back(minc::dim_info(spectrum(), spectrum()*0.5, -1, minc::dim_info::DIM_TIME)); - wtr.open(filename, di, 1, NC_FLOAT, 0); - } - if(typeid(T)==typeid(unsigned char)) - wtr.setup_write_byte(); - else if(typeid(T)==typeid(int)) - wtr.setup_write_int(); - else if(typeid(T)==typeid(double)) - wtr.setup_write_double(); - else - wtr.setup_write_float(); - minc::save_standard_volume(wtr, this->_data); - return *this; -#endif - } - - //! Save image as an ANALYZE7.5 or NIFTI file. - /** - \param filename Filename, as a C-string. - \param voxel_size Pointer to 3 consecutive values that tell about the voxel sizes along the X,Y and Z dimensions. - **/ - const CImg& save_analyze(const char *const filename, const float *const voxel_size=0) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_analyze(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - std::FILE *file; - char header[348] = { 0 }, hname[1024] = { 0 }, iname[1024] = { 0 }; - const char *const ext = cimg::split_filename(filename); - short datatype=-1; - std::memset(header,0,348); - if (!*ext) { - cimg_snprintf(hname,sizeof(hname),"%s.hdr",filename); - cimg_snprintf(iname,sizeof(iname),"%s.img",filename); - } - if (!cimg::strncasecmp(ext,"hdr",3)) { - std::strcpy(hname,filename); - std::strncpy(iname,filename,sizeof(iname)-1); - std::sprintf(iname + std::strlen(iname)-3,"img"); - } - if (!cimg::strncasecmp(ext,"img",3)) { - std::strcpy(hname,filename); - std::strncpy(iname,filename,sizeof(iname)-1); - std::sprintf(hname + std::strlen(iname)-3,"hdr"); - } - if (!cimg::strncasecmp(ext,"nii",3)) { - std::strncpy(hname,filename,sizeof(hname)-1); *iname = 0; - } - int *const iheader = (int*)header; - *iheader = 348; - std::strcpy(header + 4,"CImg"); - std::strcpy(header + 14," "); - ((short*)(header + 36))[0] = 4096; - ((char*)(header + 38))[0] = 114; - ((short*)(header + 40))[0] = 4; - ((short*)(header + 40))[1] = _width; - ((short*)(header + 40))[2] = _height; - ((short*)(header + 40))[3] = _depth; - ((short*)(header + 40))[4] = _spectrum; - if (!cimg::strcasecmp(pixel_type(),"bool")) datatype = 2; - if (!cimg::strcasecmp(pixel_type(),"unsigned char")) datatype = 2; - if (!cimg::strcasecmp(pixel_type(),"char")) datatype = 2; - if (!cimg::strcasecmp(pixel_type(),"unsigned short")) datatype = 4; - if (!cimg::strcasecmp(pixel_type(),"short")) datatype = 4; - if (!cimg::strcasecmp(pixel_type(),"unsigned int")) datatype = 8; - if (!cimg::strcasecmp(pixel_type(),"int")) datatype = 8; - if (!cimg::strcasecmp(pixel_type(),"unsigned long")) datatype = 8; - if (!cimg::strcasecmp(pixel_type(),"long")) datatype = 8; - if (!cimg::strcasecmp(pixel_type(),"float")) datatype = 16; - if (!cimg::strcasecmp(pixel_type(),"double")) datatype = 64; - if (datatype<0) - throw CImgIOException(_cimg_instance - "save_analyze(): Unsupported pixel type '%s' for file '%s'.", - cimg_instance, - pixel_type(),filename); - - ((short*)(header+70))[0] = datatype; - ((short*)(header+72))[0] = sizeof(T); - ((float*)(header+112))[0] = 1; - ((float*)(header+76))[0] = 0; - if (voxel_size) { - ((float*)(header+76))[1] = voxel_size[0]; - ((float*)(header+76))[2] = voxel_size[1]; - ((float*)(header+76))[3] = voxel_size[2]; - } else ((float*)(header+76))[1] = ((float*)(header+76))[2] = ((float*)(header+76))[3] = 1; - file = cimg::fopen(hname,"wb"); - cimg::fwrite(header,348,file); - if (*iname) { cimg::fclose(file); file = cimg::fopen(iname,"wb"); } - cimg::fwrite(_data,size(),file); - cimg::fclose(file); - return *this; - } - - //! Save image as a .cimg file. - /** - \param filename Filename, as a C-string. - \param is_compressed Tells if the file contains compressed image data. - **/ - const CImg& save_cimg(const char *const filename, const bool is_compressed=false) const { - CImgList(*this,true).save_cimg(filename,is_compressed); - return *this; - } - - //! Save image as a .cimg file \overloading. - const CImg& save_cimg(std::FILE *const file, const bool is_compressed=false) const { - CImgList(*this,true).save_cimg(file,is_compressed); - return *this; - } - - //! Save image as a sub-image into an existing .cimg file. - /** - \param filename Filename, as a C-string. - \param n0 Index of the image inside the file. - \param x0 X-coordinate of the sub-image location. - \param y0 Y-coordinate of the sub-image location. - \param z0 Z-coordinate of the sub-image location. - \param c0 C-coordinate of the sub-image location. - **/ - const CImg& save_cimg(const char *const filename, - const unsigned int n0, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0) const { - CImgList(*this,true).save_cimg(filename,n0,x0,y0,z0,c0); - return *this; - } - - //! Save image as a sub-image into an existing .cimg file \overloading. - const CImg& save_cimg(std::FILE *const file, - const unsigned int n0, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0) const { - CImgList(*this,true).save_cimg(file,n0,x0,y0,z0,c0); - return *this; - } - - //! Save blank image as a .cimg file. - /** - \param filename Filename, as a C-string. - \param dx Width of the image. - \param dy Height of the image. - \param dz Depth of the image. - \param dc Number of channels of the image. - \note - - All pixel values of the saved image are set to \c 0. - - Use this method to save large images without having to instanciate and allocate them. - **/ - static void save_empty_cimg(const char *const filename, - const unsigned int dx, const unsigned int dy=1, - const unsigned int dz=1, const unsigned int dc=1) { - return CImgList::save_empty_cimg(filename,1,dx,dy,dz,dc); - } - - //! Save blank image as a .cimg file \overloading. - /** - Same as save_empty_cimg(const char *,unsigned int,unsigned int,unsigned int,unsigned int) - with a file stream argument instead of a filename string. - **/ - static void save_empty_cimg(std::FILE *const file, - const unsigned int dx, const unsigned int dy=1, - const unsigned int dz=1, const unsigned int dc=1) { - return CImgList::save_empty_cimg(file,1,dx,dy,dz,dc); - } - - //! Save image as an INRIMAGE-4 file. - /** - \param filename Filename, as a C-string. - \param voxel_size Pointer to 3 values specifying the voxel sizes along the X,Y and Z dimensions. - **/ - const CImg& save_inr(const char *const filename, const float *const voxel_size=0) const { - return _save_inr(0,filename,voxel_size); - } - - //! Save image as an INRIMAGE-4 file \overloading. - const CImg& save_inr(std::FILE *const file, const float *const voxel_size=0) const { - return _save_inr(file,0,voxel_size); - } - - const CImg& _save_inr(std::FILE *const file, const char *const filename, const float *const voxel_size) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_inr(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - - int inrpixsize=-1; - const char *inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0"; - if (!cimg::strcasecmp(pixel_type(),"unsigned char")) { - inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1; - } - if (!cimg::strcasecmp(pixel_type(),"char")) { - inrtype = "fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1; - } - if (!cimg::strcasecmp(pixel_type(),"unsigned short")) { - inrtype = "unsigned fixed\nPIXSIZE=16 bits\nSCALE=2**0";inrpixsize = 2; - } - if (!cimg::strcasecmp(pixel_type(),"short")) { - inrtype = "fixed\nPIXSIZE=16 bits\nSCALE=2**0"; inrpixsize = 2; - } - if (!cimg::strcasecmp(pixel_type(),"unsigned int")) { - inrtype = "unsigned fixed\nPIXSIZE=32 bits\nSCALE=2**0";inrpixsize = 4; - } - if (!cimg::strcasecmp(pixel_type(),"int")) { - inrtype = "fixed\nPIXSIZE=32 bits\nSCALE=2**0"; inrpixsize = 4; - } - if (!cimg::strcasecmp(pixel_type(),"float")) { - inrtype = "float\nPIXSIZE=32 bits"; inrpixsize = 4; - } - if (!cimg::strcasecmp(pixel_type(),"double")) { - inrtype = "float\nPIXSIZE=64 bits"; inrpixsize = 8; - } - if (inrpixsize<=0) - throw CImgIOException(_cimg_instance - "save_inr(): Unsupported pixel type '%s' for file '%s'", - cimg_instance, - pixel_type(),filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - char header[257] = { 0 }; - int err = cimg_snprintf(header,sizeof(header),"#INRIMAGE-4#{\nXDIM=%u\nYDIM=%u\nZDIM=%u\nVDIM=%u\n", - _width,_height,_depth,_spectrum); - if (voxel_size) err+=std::sprintf(header + err,"VX=%g\nVY=%g\nVZ=%g\n",voxel_size[0],voxel_size[1],voxel_size[2]); - err+=std::sprintf(header + err,"TYPE=%s\nCPU=%s\n",inrtype,cimg::endianness()?"sun":"decm"); - std::memset(header + err,'\n',252 - err); - std::memcpy(header + 252,"##}\n",4); - cimg::fwrite(header,256,nfile); - cimg_forXYZ(*this,x,y,z) cimg_forC(*this,c) cimg::fwrite(&((*this)(x,y,z,c)),1,nfile); - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as an OpenEXR file. - /** - \param filename Filename, as a C-string. - \note The OpenEXR file format is described here. - **/ - const CImg& save_exr(const char *const filename) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_exr(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - if (_depth>1) - cimg::warn(_cimg_instance - "save_exr(): Instance is volumetric, only the first slice will be saved in file '%s'.", - cimg_instance, - filename); - -#ifndef cimg_use_openexr - return save_other(filename); -#else - Imf::Rgba *const ptrd0 = new Imf::Rgba[(unsigned long)_width*_height], *ptrd = ptrd0, rgba; - switch (_spectrum) { - case 1 : { // Grayscale image. - for (const T *ptr_r = data(), *const ptr_e = ptr_r + (unsigned long)_width*_height; ptr_rPandore file specifications - for more informations). - **/ - const CImg& save_pandore(const char *const filename, const unsigned int colorspace=0) const { - return _save_pandore(0,filename,colorspace); - } - - //! Save image as a Pandore-5 file \overloading. - /** - Same as save_pandore(const char *,unsigned int) const - with a file stream argument instead of a filename string. - **/ - const CImg& save_pandore(std::FILE *const file, const unsigned int colorspace=0) const { - return _save_pandore(file,0,colorspace); - } - - unsigned int _save_pandore_header_length(unsigned int id, unsigned int *dims, const unsigned int colorspace) const { - unsigned int nbdims = 0; - if (id==2 || id==3 || id==4) { - dims[0] = 1; dims[1] = _width; nbdims = 2; - } - if (id==5 || id==6 || id==7) { - dims[0] = 1; dims[1] = _height; dims[2] = _width; nbdims=3; - } - if (id==8 || id==9 || id==10) { - dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4; - } - if (id==16 || id==17 || id==18) { - dims[0] = 3; dims[1] = _height; dims[2] = _width; dims[3] = colorspace; nbdims = 4; - } - if (id==19 || id==20 || id==21) { - dims[0] = 3; dims[1] = _depth; dims[2] = _height; dims[3] = _width; dims[4] = colorspace; nbdims = 5; - } - if (id==22 || id==23 || id==25) { - dims[0] = _spectrum; dims[1] = _width; nbdims = 2; - } - if (id==26 || id==27 || id==29) { - dims[0] = _spectrum; dims[1] = _height; dims[2] = _width; nbdims=3; - } - if (id==30 || id==31 || id==33) { - dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4; - } - return nbdims; - } - - const CImg& _save_pandore(std::FILE *const file, const char *const filename, - const unsigned int colorspace) const { - -#define __cimg_save_pandore_case(dtype) \ - dtype *buffer = new dtype[size()]; \ - const T *ptrs = _data; \ - cimg_foroff(*this,off) *(buffer++) = (dtype)(*(ptrs++)); \ - buffer-=size(); \ - cimg::fwrite(buffer,size(),nfile); \ - delete[] buffer - -#define _cimg_save_pandore_case(sy,sz,sv,stype,id) \ - if (!saved && (sy?(sy==_height):true) && (sz?(sz==_depth):true) && \ - (sv?(sv==_spectrum):true) && !std::strcmp(stype,pixel_type())) { \ - unsigned int *iheader = (unsigned int*)(header+12); \ - nbdims = _save_pandore_header_length((*iheader=id),dims,colorspace); \ - cimg::fwrite(header,36,nfile); \ - if (sizeof(unsigned long)==4) { unsigned long ndims[5] = { 0 }; \ - for (int d = 0; d<5; ++d) ndims[d] = (unsigned long)dims[d]; cimg::fwrite(ndims,nbdims,nfile); } \ - else if (sizeof(unsigned int)==4) { unsigned int ndims[5] = { 0 }; \ - for (int d = 0; d<5; ++d) ndims[d] = (unsigned int)dims[d]; cimg::fwrite(ndims,nbdims,nfile); } \ - else if (sizeof(unsigned short)==4) { unsigned short ndims[5] = { 0 }; \ - for (int d = 0; d<5; ++d) ndims[d] = (unsigned short)dims[d]; cimg::fwrite(ndims,nbdims,nfile); } \ - else throw CImgIOException(_cimg_instance \ - "save_pandore(): Unsupported datatype for file '%s'.",\ - cimg_instance, \ - filename?filename:"(FILE*)"); \ - if (id==2 || id==5 || id==8 || id==16 || id==19 || id==22 || id==26 || id==30) { \ - __cimg_save_pandore_case(unsigned char); \ - } else if (id==3 || id==6 || id==9 || id==17 || id==20 || id==23 || id==27 || id==31) { \ - if (sizeof(unsigned long)==4) { __cimg_save_pandore_case(unsigned long); } \ - else if (sizeof(unsigned int)==4) { __cimg_save_pandore_case(unsigned int); } \ - else if (sizeof(unsigned short)==4) { __cimg_save_pandore_case(unsigned short); } \ - else throw CImgIOException(_cimg_instance \ - "save_pandore(): Unsupported datatype for file '%s'.",\ - cimg_instance, \ - filename?filename:"(FILE*)"); \ - } else if (id==4 || id==7 || id==10 || id==18 || id==21 || id==25 || id==29 || id==33) { \ - if (sizeof(double)==4) { __cimg_save_pandore_case(double); } \ - else if (sizeof(float)==4) { __cimg_save_pandore_case(float); } \ - else throw CImgIOException(_cimg_instance \ - "save_pandore(): Unsupported datatype for file '%s'.",\ - cimg_instance, \ - filename?filename:"(FILE*)"); \ - } \ - saved = true; \ - } - - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_pandore(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - unsigned char header[36] = { 'P','A','N','D','O','R','E','0','4',0,0,0, - 0,0,0,0,'C','I','m','g',0,0,0,0,0,'N','o',' ','d','a','t','e',0,0,0,0 }; - unsigned int nbdims, dims[5] = { 0 }; - bool saved = false; - _cimg_save_pandore_case(1,1,1,"unsigned char",2); - _cimg_save_pandore_case(1,1,1,"char",3); - _cimg_save_pandore_case(1,1,1,"short",3); - _cimg_save_pandore_case(1,1,1,"unsigned short",3); - _cimg_save_pandore_case(1,1,1,"unsigned int",3); - _cimg_save_pandore_case(1,1,1,"int",3); - _cimg_save_pandore_case(1,1,1,"unsigned long",4); - _cimg_save_pandore_case(1,1,1,"long",3); - _cimg_save_pandore_case(1,1,1,"float",4); - _cimg_save_pandore_case(1,1,1,"double",4); - - _cimg_save_pandore_case(0,1,1,"unsigned char",5); - _cimg_save_pandore_case(0,1,1,"char",6); - _cimg_save_pandore_case(0,1,1,"short",6); - _cimg_save_pandore_case(0,1,1,"unsigned short",6); - _cimg_save_pandore_case(0,1,1,"unsigned int",6); - _cimg_save_pandore_case(0,1,1,"int",6); - _cimg_save_pandore_case(0,1,1,"unsigned long",7); - _cimg_save_pandore_case(0,1,1,"long",6); - _cimg_save_pandore_case(0,1,1,"float",7); - _cimg_save_pandore_case(0,1,1,"double",7); - - _cimg_save_pandore_case(0,0,1,"unsigned char",8); - _cimg_save_pandore_case(0,0,1,"char",9); - _cimg_save_pandore_case(0,0,1,"short",9); - _cimg_save_pandore_case(0,0,1,"unsigned short",9); - _cimg_save_pandore_case(0,0,1,"unsigned int",9); - _cimg_save_pandore_case(0,0,1,"int",9); - _cimg_save_pandore_case(0,0,1,"unsigned long",10); - _cimg_save_pandore_case(0,0,1,"long",9); - _cimg_save_pandore_case(0,0,1,"float",10); - _cimg_save_pandore_case(0,0,1,"double",10); - - _cimg_save_pandore_case(0,1,3,"unsigned char",16); - _cimg_save_pandore_case(0,1,3,"char",17); - _cimg_save_pandore_case(0,1,3,"short",17); - _cimg_save_pandore_case(0,1,3,"unsigned short",17); - _cimg_save_pandore_case(0,1,3,"unsigned int",17); - _cimg_save_pandore_case(0,1,3,"int",17); - _cimg_save_pandore_case(0,1,3,"unsigned long",18); - _cimg_save_pandore_case(0,1,3,"long",17); - _cimg_save_pandore_case(0,1,3,"float",18); - _cimg_save_pandore_case(0,1,3,"double",18); - - _cimg_save_pandore_case(0,0,3,"unsigned char",19); - _cimg_save_pandore_case(0,0,3,"char",20); - _cimg_save_pandore_case(0,0,3,"short",20); - _cimg_save_pandore_case(0,0,3,"unsigned short",20); - _cimg_save_pandore_case(0,0,3,"unsigned int",20); - _cimg_save_pandore_case(0,0,3,"int",20); - _cimg_save_pandore_case(0,0,3,"unsigned long",21); - _cimg_save_pandore_case(0,0,3,"long",20); - _cimg_save_pandore_case(0,0,3,"float",21); - _cimg_save_pandore_case(0,0,3,"double",21); - - _cimg_save_pandore_case(1,1,0,"unsigned char",22); - _cimg_save_pandore_case(1,1,0,"char",23); - _cimg_save_pandore_case(1,1,0,"short",23); - _cimg_save_pandore_case(1,1,0,"unsigned short",23); - _cimg_save_pandore_case(1,1,0,"unsigned int",23); - _cimg_save_pandore_case(1,1,0,"int",23); - _cimg_save_pandore_case(1,1,0,"unsigned long",25); - _cimg_save_pandore_case(1,1,0,"long",23); - _cimg_save_pandore_case(1,1,0,"float",25); - _cimg_save_pandore_case(1,1,0,"double",25); - - _cimg_save_pandore_case(0,1,0,"unsigned char",26); - _cimg_save_pandore_case(0,1,0,"char",27); - _cimg_save_pandore_case(0,1,0,"short",27); - _cimg_save_pandore_case(0,1,0,"unsigned short",27); - _cimg_save_pandore_case(0,1,0,"unsigned int",27); - _cimg_save_pandore_case(0,1,0,"int",27); - _cimg_save_pandore_case(0,1,0,"unsigned long",29); - _cimg_save_pandore_case(0,1,0,"long",27); - _cimg_save_pandore_case(0,1,0,"float",29); - _cimg_save_pandore_case(0,1,0,"double",29); - - _cimg_save_pandore_case(0,0,0,"unsigned char",30); - _cimg_save_pandore_case(0,0,0,"char",31); - _cimg_save_pandore_case(0,0,0,"short",31); - _cimg_save_pandore_case(0,0,0,"unsigned short",31); - _cimg_save_pandore_case(0,0,0,"unsigned int",31); - _cimg_save_pandore_case(0,0,0,"int",31); - _cimg_save_pandore_case(0,0,0,"unsigned long",33); - _cimg_save_pandore_case(0,0,0,"long",31); - _cimg_save_pandore_case(0,0,0,"float",33); - _cimg_save_pandore_case(0,0,0,"double",33); - - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a raw data file. - /** - \param filename Filename, as a C-string. - \param is_multiplexed Tells if the image channels are stored in a multiplexed way (\c true) or not (\c false). - \note The .raw format does not store the image dimensions in the output file, - so you have to keep track of them somewhere to be able to read the file correctly afterwards. - **/ - const CImg& save_raw(const char *const filename, const bool is_multiplexed=false) const { - return _save_raw(0,filename,is_multiplexed); - } - - //! Save image as a raw data file \overloading. - /** - Same as save_raw(const char *,bool) const - with a file stream argument instead of a filename string. - **/ - const CImg& save_raw(std::FILE *const file, const bool is_multiplexed=false) const { - return _save_raw(file,0,is_multiplexed); - } - - const CImg& _save_raw(std::FILE *const file, const char *const filename, const bool is_multiplexed) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_raw(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - if (!is_multiplexed) cimg::fwrite(_data,size(),nfile); - else { - CImg buf(_spectrum); - cimg_forXYZ(*this,x,y,z) { - cimg_forC(*this,c) buf[c] = (*this)(x,y,z,c); - cimg::fwrite(buf._data,_spectrum,nfile); - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save image as a video file, using the FFmpeg library. - /** - \param filename Filename, as a C-string. - \param fps Video framerate. - \param bitrate Video bitrate. - \note - - Each slice of the instance image is considered to be a single frame of the output video file. - - This method uses functions provided by the FFmpeg library. - Configuration macro \c cimg_use_ffmpeg must be set for the method to succeed natively. - Otherwise, the method calls - save_ffmpeg_external(const char*,unsigned int,unsigned int,const char*,unsigned int,unsigned int) const. - **/ - const CImg& save_ffmpeg(const char *const filename, const unsigned int fps=25, - const unsigned int bitrate=2048) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_ffmpeg(): Specified filename is (null).", - cimg_instance); - if (!fps) - throw CImgArgumentException(_cimg_instance - "save_ffmpeg(): Invalid specified framerate 0, for file '%s'.", - cimg_instance, - filename); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifndef cimg_use_ffmpeg - return save_ffmpeg_external(filename,0,fps,bitrate); -#else - CImgList list; - get_split('z').move_to(list); - list.save_ffmpeg(filename,fps,bitrate); - return *this; -#endif - } - - //! Save image as a .yuv video file. - /** - \param filename Filename, as a C-string. - \param is_rgb Tells if pixel values of the instance image are RGB-coded (\c true) or YUV-coded (\c false). - \note Each slice of the instance image is considered to be a single frame of the output video file. - **/ - const CImg& save_yuv(const char *const filename, const bool is_rgb=true) const { - get_split('z').save_yuv(filename,is_rgb); - return *this; - } - - //! Save image as a .yuv video file \overloading. - /** - Same as save_yuv(const char*,bool) const - with a file stream argument instead of a filename string. - **/ - const CImg& save_yuv(std::FILE *const file, const bool is_rgb=true) const { - get_split('z').save_yuv(file,is_rgb); - return *this; - } - - //! Save 3d object as an Object File Format (.off) file. - /** - \param filename Filename, as a C-string. - \param primitives List of 3d object primitives. - \param colors List of 3d object colors. - \note - - Instance image contains the vertices data of the 3d object. - - Textured, transparent or sphere-shaped primitives cannot be managed by the .off file format. - Such primitives will be lost or simplified during file saving. - - The .off file format is described here. - **/ - template - const CImg& save_off(const CImgList& primitives, const CImgList& colors, - const char *const filename) const { - return _save_off(primitives,colors,0,filename); - } - - //! Save 3d object as an Object File Format (.off) file \overloading. - /** - Same as save_off(const CImgList&,const CImgList&,const char*) const - with a file stream argument instead of a filename string. - **/ - template - const CImg& save_off(const CImgList& primitives, const CImgList& colors, - std::FILE *const file) const { - return _save_off(primitives,colors,file,0); - } - - template - const CImg& _save_off(const CImgList& primitives, const CImgList& colors, - std::FILE *const file, const char *const filename) const { - if (!file && !filename) - throw CImgArgumentException(_cimg_instance - "save_off(): Specified filename is (null).", - cimg_instance); - if (is_empty()) - throw CImgInstanceException(_cimg_instance - "save_off(): Empty instance, for file '%s'.", - cimg_instance, - filename?filename:"(FILE*)"); - - CImgList opacities; - char error_message[1024] = { 0 }; - if (!is_object3d(primitives,colors,opacities,true,error_message)) - throw CImgInstanceException(_cimg_instance - "save_off(): Invalid specified 3d object, for file '%s' (%s).", - cimg_instance, - filename?filename:"(FILE*)",error_message); - - const CImg default_color(1,3,1,1,200); - std::FILE *const nfile = file?file:cimg::fopen(filename,"w"); - unsigned int supported_primitives = 0; - cimglist_for(primitives,l) if (primitives[l].size()!=5) ++supported_primitives; - std::fprintf(nfile,"OFF\n%u %u %u\n",_width,supported_primitives,3*primitives._width); - cimg_forX(*this,i) std::fprintf(nfile,"%f %f %f\n", - (float)((*this)(i,0)),(float)((*this)(i,1)),(float)((*this)(i,2))); - cimglist_for(primitives,l) { - const CImg& color = l1?color[1]:r)/255.0f, b = (csiz>2?color[2]:g)/255.0f; - switch (psiz) { - case 1 : std::fprintf(nfile,"1 %u %f %f %f\n", - (unsigned int)primitives(l,0),r,g,b); break; - case 2 : std::fprintf(nfile,"2 %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break; - case 3 : std::fprintf(nfile,"3 %u %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,2), - (unsigned int)primitives(l,1),r,g,b); break; - case 4 : std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,3), - (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),r,g,b); break; - case 5 : std::fprintf(nfile,"2 %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break; - case 6 : { - const unsigned int xt = (unsigned int)primitives(l,2), yt = (unsigned int)primitives(l,3); - const float - rt = color.atXY(xt,yt,0)/255.0f, - gt = (csiz>1?color.atXY(xt,yt,1):r)/255.0f, - bt = (csiz>2?color.atXY(xt,yt,2):g)/255.0f; - std::fprintf(nfile,"2 %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),rt,gt,bt); - } break; - case 9 : { - const unsigned int xt = (unsigned int)primitives(l,3), yt = (unsigned int)primitives(l,4); - const float - rt = color.atXY(xt,yt,0)/255.0f, - gt = (csiz>1?color.atXY(xt,yt,1):r)/255.0f, - bt = (csiz>2?color.atXY(xt,yt,2):g)/255.0f; - std::fprintf(nfile,"3 %u %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,2), - (unsigned int)primitives(l,1),rt,gt,bt); - } break; - case 12 : { - const unsigned int xt = (unsigned int)primitives(l,4), yt = (unsigned int)primitives(l,5); - const float - rt = color.atXY(xt,yt,0)/255.0f, - gt = (csiz>1?color.atXY(xt,yt,1):r)/255.0f, - bt = (csiz>2?color.atXY(xt,yt,2):g)/255.0f; - std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n", - (unsigned int)primitives(l,0),(unsigned int)primitives(l,3), - (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),rt,gt,bt); - } break; - } - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save volumetric image as a video, using ffmpeg external binary. - /** - \param filename Filename, as a C-string. - \param codec Video codec, as a C-string. - \param fps Video framerate. - \param bitrate Video bitrate. - \note - - Each slice of the instance image is considered to be a single frame of the output video file. - - This method uses \c ffmpeg, an external executable binary provided by - FFmpeg. - It must be installed for the method to succeed. - **/ - const CImg& save_ffmpeg_external(const char *const filename, const char *const codec=0, - const unsigned int fps=25, const unsigned int bitrate=2048) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_ffmpeg_external(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - CImgList list; - get_split('z').move_to(list); - list.save_ffmpeg_external(filename,codec,fps,bitrate); - return *this; - } - - //! Save image using gzip external binary. - /** - \param filename Filename, as a C-string. - \note This method uses \c gzip, an external executable binary provided by - gzip. - It must be installed for the method to succeed. - **/ - const CImg& save_gzip_external(const char *const filename) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_gzip_external(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; - const char - *ext = cimg::split_filename(filename,body), - *ext2 = cimg::split_filename(body,0); - std::FILE *file; - do { - if (!cimg::strcasecmp(ext,"gz")) { - if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } else { - if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - save(filetmp); - cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", - cimg::gzip_path(), - CImg::string(filetmp)._system_strescape().data(), - CImg::string(filename)._system_strescape().data()); - cimg::system(command); - file = std::fopen(filename,"rb"); - if (!file) - throw CImgIOException(_cimg_instance - "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.", - cimg_instance, - filename); - - else cimg::fclose(file); - std::remove(filetmp); - return *this; - } - - //! Save image using GraphicsMagick's external binary. - /** - \param filename Filename, as a C-string. - \param quality Image quality (expressed in percent), when the file format supports it. - \note This method uses \c gm, an external executable binary provided by - GraphicsMagick. - It must be installed for the method to succeed. - **/ - const CImg& save_graphicsmagick_external(const char *const filename, const unsigned int quality=100) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_graphicsmagick_external(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifdef cimg_use_png -#define _cimg_sge_ext1 "png" -#define _cimg_sge_ext2 "png" -#else -#define _cimg_sge_ext1 "pgm" -#define _cimg_sge_ext2 "ppm" -#endif - char command[1024] = { 0 }, filetmp[512] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(), - _spectrum==1?_cimg_sge_ext1:_cimg_sge_ext2); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); -#ifdef cimg_use_png - save_png(filetmp); -#else - save_pnm(filetmp); -#endif - cimg_snprintf(command,sizeof(command),"%s convert -quality %u \"%s\" \"%s\"", - cimg::graphicsmagick_path(),quality, - CImg::string(filetmp)._system_strescape().data(), - CImg::string(filename)._system_strescape().data()); - cimg::system(command); - file = std::fopen(filename,"rb"); - if (!file) - throw CImgIOException(_cimg_instance - "save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.", - cimg_instance, - filename); - - if (file) cimg::fclose(file); - std::remove(filetmp); - return *this; - } - - //! Save image using ImageMagick's external binary. - /** - \param filename Filename, as a C-string. - \param quality Image quality (expressed in percent), when the file format supports it. - \note This method uses \c convert, an external executable binary provided by - ImageMagick. - It must be installed for the method to succeed. - **/ - const CImg& save_imagemagick_external(const char *const filename, const unsigned int quality=100) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_imagemagick_external(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifdef cimg_use_png -#define _cimg_sie_ext1 "png" -#define _cimg_sie_ext2 "png" -#else -#define _cimg_sie_ext1 "pgm" -#define _cimg_sie_ext2 "ppm" -#endif - char command[1024] = { 0 }, filetmp[512] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s",cimg::temporary_path(), - cimg_file_separator,cimg::filenamerand(),_spectrum==1?_cimg_sie_ext1:_cimg_sie_ext2); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); -#ifdef cimg_use_png - save_png(filetmp); -#else - save_pnm(filetmp); -#endif - cimg_snprintf(command,sizeof(command),"%s -quality %u \"%s\" \"%s\"", - cimg::imagemagick_path(),quality, - CImg::string(filetmp)._system_strescape().data(), - CImg::string(filename)._system_strescape().data()); - cimg::system(command); - file = std::fopen(filename,"rb"); - if (!file) - throw CImgIOException(_cimg_instance - "save_imagemagick_external(): Failed to save file '%s' with external command 'convert'.", - cimg_instance, - filename); - - if (file) cimg::fclose(file); - std::remove(filetmp); - return *this; - } - - //! Save image as a Dicom file. - /** - \param filename Filename, as a C-string. - \note This method uses \c medcon, an external executable binary provided by - (X)Medcon. - It must be installed for the method to succeed. - **/ - const CImg& save_medcon_external(const char *const filename) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_medcon_external(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s.hdr",cimg::filenamerand()); - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - save_analyze(filetmp); - cimg_snprintf(command,sizeof(command),"%s -w -c dicom -o \"%s\" -f \"%s\"", - cimg::medcon_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); - cimg::system(command); - std::remove(filetmp); - cimg::split_filename(filetmp,body); - cimg_snprintf(filetmp,sizeof(filetmp),"%s.img",body); - std::remove(filetmp); - - file = std::fopen(filename,"rb"); - if (!file) { - cimg_snprintf(command,sizeof(command),"m000-%s",filename); - file = std::fopen(command,"rb"); - if (!file) { - cimg::fclose(cimg::fopen(filename,"r")); - throw CImgIOException(_cimg_instance - "save_medcon_external(): Failed to save file '%s' with external command 'medcon'.", - cimg_instance, - filename); - } - } - cimg::fclose(file); - std::rename(command,filename); - return *this; - } - - // Save image for non natively supported formats. - /** - \param filename Filename, as a C-string. - \param quality Image quality (expressed in percent), when the file format supports it. - \note - - The filename extension tells about the desired file format. - - This method tries to save the instance image as a file, using external tools from - ImageMagick or - GraphicsMagick. - At least one of these tool must be installed for the method to succeed. - - It is recommended to use the generic method save(const char*, int) const instead, - as it can handle some file formats natively. - **/ - const CImg& save_other(const char *const filename, const unsigned int quality=100) const { - if (!filename) - throw CImgArgumentException(_cimg_instance - "save_other(): Specified filename is (null).", - cimg_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - const unsigned int omode = cimg::exception_mode(); - bool is_saved = true; - cimg::exception_mode() = 0; - try { save_magick(filename); } - catch (CImgException&) { - try { save_imagemagick_external(filename,quality); } - catch (CImgException&) { - try { save_graphicsmagick_external(filename,quality); } - catch (CImgException&) { - is_saved = false; - } - } - } - cimg::exception_mode() = omode; - if (!is_saved) - throw CImgIOException(_cimg_instance - "save_other(): Failed to save file '%s'. Format is not natively supported, " - "and no external commands succeeded.", - cimg_instance, - filename); - return *this; - } - - // [internal] Return a 40x38 color logo of a 'danger' item. - static CImg _logo40x38() { - CImg res(40,38,1,3); - const unsigned char *ptrs = cimg::logo40x38; - T *ptr1 = res.data(0,0,0,0), *ptr2 = res.data(0,0,0,1), *ptr3 = res.data(0,0,0,2); - for (unsigned long off = 0; off<(unsigned long)res._width*res._height;) { - const unsigned char n = *(ptrs++), r = *(ptrs++), g = *(ptrs++), b = *(ptrs++); - for (unsigned int l = 0; l structure - # - # - # - #------------------------------------------ - */ - //! Represent a list of images CImg. - template - struct CImgList { - unsigned int _width, _allocated_width; - CImg *_data; - - //! Simple iterator type, to loop through each image of a list. - /** - \note - - The \c CImgList::iterator type is defined as a CImg*. - - You may use it like this: - \code - CImgList<> list; // Assuming this image list is not empty. - for (CImgList<>::iterator it = list.begin(); it* iterator; - - //! Simple const iterator type, to loop through each image of a \c const list instance. - /** - \note - - The \c CImgList::const_iterator type is defined to be a const CImg*. - - Similar to CImgList::iterator, but for constant list instances. - **/ - typedef const CImg* const_iterator; - - //! Pixel value type. - /** - Refer to the pixels value type of the images in the list. - \note - - The \c CImgList::value_type type of a \c CImgList is defined to be a \c T. - It is then similar to CImg::value_type. - - \c CImgList::value_type is actually not used in %CImg methods. It has been mainly defined for - compatibility with STL naming conventions. - **/ - typedef T value_type; - - // Define common T-dependant types. - typedef typename cimg::superset::type Tbool; - typedef typename cimg::superset::type Tuchar; - typedef typename cimg::superset::type Tchar; - typedef typename cimg::superset::type Tushort; - typedef typename cimg::superset::type Tshort; - typedef typename cimg::superset::type Tuint; - typedef typename cimg::superset::type Tint; - typedef typename cimg::superset::type Tulong; - typedef typename cimg::superset::type Tlong; - typedef typename cimg::superset::type Tfloat; - typedef typename cimg::superset::type Tdouble; - typedef typename cimg::last::type boolT; - typedef typename cimg::last::type ucharT; - typedef typename cimg::last::type charT; - typedef typename cimg::last::type ushortT; - typedef typename cimg::last::type shortT; - typedef typename cimg::last::type uintT; - typedef typename cimg::last::type intT; - typedef typename cimg::last::type ulongT; - typedef typename cimg::last::type longT; - typedef typename cimg::last::type floatT; - typedef typename cimg::last::type doubleT; - - //@} - //--------------------------- - // - //! \name Plugins - //@{ - //--------------------------- -#ifdef cimglist_plugin -#include cimglist_plugin -#endif -#ifdef cimglist_plugin1 -#include cimglist_plugin1 -#endif -#ifdef cimglist_plugin2 -#include cimglist_plugin2 -#endif -#ifdef cimglist_plugin3 -#include cimglist_plugin3 -#endif -#ifdef cimglist_plugin4 -#include cimglist_plugin4 -#endif -#ifdef cimglist_plugin5 -#include cimglist_plugin5 -#endif -#ifdef cimglist_plugin6 -#include cimglist_plugin6 -#endif -#ifdef cimglist_plugin7 -#include cimglist_plugin7 -#endif -#ifdef cimglist_plugin8 -#include cimglist_plugin8 -#endif - - //@} - //-------------------------------------------------------- - // - //! \name Constructors / Destructor / Instance Management - //@{ - //-------------------------------------------------------- - - //! Destructor. - /** - Destroy current list instance. - \note - - Any allocated buffer is deallocated. - - Destroying an empty list does nothing actually. - **/ - ~CImgList() { - delete[] _data; - } - - //! Default constructor. - /** - Construct a new empty list instance. - \note - - An empty list has no pixel data and its dimension width() is set to \c 0, as well as its - image buffer pointer data(). - - An empty list may be reassigned afterwards, with the family of the assign() methods. - In all cases, the type of pixels stays \c T. - **/ - CImgList(): - _width(0),_allocated_width(0),_data(0) {} - - //! Construct list containing empty images. - /** - \param n Number of empty images. - \note Useful when you know by advance the number of images you want to manage, as - it will allocate the right amount of memory for the list, without needs for reallocation - (that may occur when starting from an empty list and inserting several images in it). - **/ - explicit CImgList(const unsigned int n):_width(n) { - if (n) _data = new CImg[_allocated_width = cimg::max(16UL,cimg::nearest_pow2(n))]; - else { _allocated_width = 0; _data = 0; } - } - - //! Construct list containing images of specified size. - /** - \param n Number of images. - \param width Width of images. - \param height Height of images. - \param depth Depth of images. - \param spectrum Number of channels of images. - \note Pixel values are not initialized and may probably contain garbage. - **/ - CImgList(const unsigned int n, const unsigned int width, const unsigned int height=1, - const unsigned int depth=1, const unsigned int spectrum=1): - _width(0),_allocated_width(0),_data(0) { - assign(n); - cimglist_apply(*this,assign)(width,height,depth,spectrum); - } - - //! Construct list containing images of specified size, and initialize pixel values. - /** - \param n Number of images. - \param width Width of images. - \param height Height of images. - \param depth Depth of images. - \param spectrum Number of channels of images. - \param val Initialization value for images pixels. - **/ - CImgList(const unsigned int n, const unsigned int width, const unsigned int height, - const unsigned int depth, const unsigned int spectrum, const T val): - _width(0),_allocated_width(0),_data(0) { - assign(n); - cimglist_apply(*this,assign)(width,height,depth,spectrum,val); - } - - //! Construct list containing images of specified size, and initialize pixel values from a sequence of integers. - /** - \param n Number of images. - \param width Width of images. - \param height Height of images. - \param depth Depth of images. - \param spectrum Number of channels of images. - \param val0 First value of the initializing integers sequence. - \param val1 Second value of the initializing integers sequence. - \warning You must specify at least width*height*depth*spectrum values in your argument list, - or you will probably segfault. - **/ - CImgList(const unsigned int n, const unsigned int width, const unsigned int height, - const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...): - _width(0),_allocated_width(0),_data(0) { -#define _CImgList_stdarg(t) { \ - assign(n,width,height,depth,spectrum); \ - const unsigned long siz = (unsigned long)width*height*depth*spectrum, nsiz = siz*n; \ - T *ptrd = _data->_data; \ - va_list ap; \ - va_start(ap,val1); \ - for (unsigned long l = 0, s = 0, i = 0; iwidth*height*depth*spectrum values in your argument list, - or you will probably segfault. - **/ - CImgList(const unsigned int n, const unsigned int width, const unsigned int height, - const unsigned int depth, const unsigned int spectrum, const double val0, const double val1, ...): - _width(0),_allocated_width(0),_data(0) { - _CImgList_stdarg(double); - } - - //! Construct list containing copies of an input image. - /** - \param n Number of images. - \param img Input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of \c img. - **/ - template - CImgList(const unsigned int n, const CImg& img, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(n); - cimglist_apply(*this,assign)(img,is_shared); - } - - //! Construct list from one image. - /** - \param img Input image to copy in the constructed list. - \param is_shared Tells if the element of the list is a shared or non-shared copy of \c img. - **/ - template - explicit CImgList(const CImg& img, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(1); - _data[0].assign(img,is_shared); - } - - //! Construct list from two images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(2); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); - } - - //! Construct list from three images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param img3 Third input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(3); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - } - - //! Construct list from four images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param img3 Third input image to copy in the constructed list. - \param img4 Fourth input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(4); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); - } - - //! Construct list from five images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param img3 Third input image to copy in the constructed list. - \param img4 Fourth input image to copy in the constructed list. - \param img5 Fifth input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(5); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); - } - - //! Construct list from six images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param img3 Third input image to copy in the constructed list. - \param img4 Fourth input image to copy in the constructed list. - \param img5 Fifth input image to copy in the constructed list. - \param img6 Sixth input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const CImg& img6, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(6); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); - } - - //! Construct list from seven images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param img3 Third input image to copy in the constructed list. - \param img4 Fourth input image to copy in the constructed list. - \param img5 Fifth input image to copy in the constructed list. - \param img6 Sixth input image to copy in the constructed list. - \param img7 Seventh input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const CImg& img6, const CImg& img7, const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(7); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); - _data[6].assign(img7,is_shared); - } - - //! Construct list from eight images. - /** - \param img1 First input image to copy in the constructed list. - \param img2 Second input image to copy in the constructed list. - \param img3 Third input image to copy in the constructed list. - \param img4 Fourth input image to copy in the constructed list. - \param img5 Fifth input image to copy in the constructed list. - \param img6 Sixth input image to copy in the constructed list. - \param img7 Seventh input image to copy in the constructed list. - \param img8 Eighth input image to copy in the constructed list. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const CImg& img6, const CImg& img7, const CImg& img8, - const bool is_shared=false): - _width(0),_allocated_width(0),_data(0) { - assign(8); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); - _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared); - } - - //! Construct list copy. - /** - \param list Input list to copy. - \note The shared state of each element of the constructed list is kept the same as in \c list. - **/ - template - CImgList(const CImgList& list):_width(0),_allocated_width(0),_data(0) { - assign(list._width); - cimglist_for(*this,l) _data[l].assign(list[l],false); - } - - //! Construct list copy \specialization. - CImgList(const CImgList& list):_width(0),_allocated_width(0),_data(0) { - assign(list._width); - cimglist_for(*this,l) _data[l].assign(list[l],list[l]._is_shared); - } - - //! Construct list copy, and force the shared state of the list elements. - /** - \param list Input list to copy. - \param is_shared Tells if the elements of the list are shared or non-shared copies of input images. - **/ - template - CImgList(const CImgList& list, const bool is_shared):_width(0),_allocated_width(0),_data(0) { - assign(list._width); - cimglist_for(*this,l) _data[l].assign(list[l],is_shared); - } - - //! Construct list by reading the content of a file. - /** - \param filename Filename, as a C-string. - **/ - explicit CImgList(const char *const filename):_width(0),_allocated_width(0),_data(0) { - assign(filename); - } - - //! Construct list from the content of a display window. - /** - \param disp Display window to get content from. - \note Constructed list contains a single image only. - **/ - explicit CImgList(const CImgDisplay& disp):_width(0),_allocated_width(0),_data(0) { - assign(disp); - } - - //! Return a list with elements being shared copies of images in the list instance. - /** - \note list2 = list1.get_shared() is equivalent to list2.assign(list1,true). - **/ - CImgList get_shared() { - CImgList res(_width); - cimglist_for(*this,l) res[l].assign(_data[l],true); - return res; - } - - //! Return a list with elements being shared copies of images in the list instance \const. - const CImgList get_shared() const { - CImgList res(_width); - cimglist_for(*this,l) res[l].assign(_data[l],true); - return res; - } - - //! Destructor \inplace. - /** - \see CImgList(). - **/ - CImgList& assign() { - delete[] _data; - _width = _allocated_width = 0; - _data = 0; - return *this; - } - - //! Destructor \inplace. - /** - Equivalent to assign(). - \note Only here for compatibility with STL naming conventions. - **/ - CImgList& clear() { - return assign(); - } - - //! Construct list containing empty images \inplace. - /** - \see CImgList(unsigned int). - **/ - CImgList& assign(const unsigned int n) { - if (!n) return assign(); - if (_allocated_width(n<<2)) { - delete[] _data; - _data = new CImg[_allocated_width=cimg::max(16UL,cimg::nearest_pow2(n))]; - } - _width = n; - return *this; - } - - //! Construct list containing images of specified size \inplace. - /** - \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int). - **/ - CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height=1, - const unsigned int depth=1, const unsigned int spectrum=1) { - assign(n); - cimglist_apply(*this,assign)(width,height,depth,spectrum); - return *this; - } - - //! Construct list containing images of specified size, and initialize pixel values \inplace. - /** - \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const T). - **/ - CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height, - const unsigned int depth, const unsigned int spectrum, const T val) { - assign(n); - cimglist_apply(*this,assign)(width,height,depth,spectrum,val); - return *this; - } - - //! Construct list with images of specified size, and initialize pixel values from a sequence of integers \inplace. - /** - \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const int, const int, ...). - **/ - CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height, - const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...) { - _CImgList_stdarg(int); - return *this; - } - - //! Construct list with images of specified size, and initialize pixel values from a sequence of doubles \inplace. - /** - \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const double, const double, ...). - **/ - CImgList& assign(const unsigned int n, const unsigned int width, const unsigned int height, - const unsigned int depth, const unsigned int spectrum, - const double val0, const double val1, ...) { - _CImgList_stdarg(double); - return *this; - } - - //! Construct list containing copies of an input image \inplace. - /** - \see CImgList(unsigned int, const CImg&, bool). - **/ - template - CImgList& assign(const unsigned int n, const CImg& img, const bool is_shared=false) { - assign(n); - cimglist_apply(*this,assign)(img,is_shared); - return *this; - } - - //! Construct list from one image \inplace. - /** - \see CImgList(const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img, const bool is_shared=false) { - assign(1); - _data[0].assign(img,is_shared); - return *this; - } - - //! Construct list from two images \inplace. - /** - \see CImgList(const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const bool is_shared=false) { - assign(2); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); - return *this; - } - - //! Construct list from three images \inplace. - /** - \see CImgList(const CImg&, const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const bool is_shared=false) { - assign(3); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - return *this; - } - - //! Construct list from four images \inplace. - /** - \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const bool is_shared=false) { - assign(4); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); - return *this; - } - - //! Construct list from five images \inplace. - /** - \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const bool is_shared=false) { - assign(5); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); - return *this; - } - - //! Construct list from six images \inplace. - /** - \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const CImg& img6, const bool is_shared=false) { - assign(6); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); - return *this; - } - - //! Construct list from seven images \inplace. - /** - \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const CImg& img6, const CImg& img7, const bool is_shared=false) { - assign(7); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); - _data[6].assign(img7,is_shared); - return *this; - } - - //! Construct list from eight images \inplace. - /** - \see CImgList(const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, const CImg&, bool). - **/ - template - CImgList& assign(const CImg& img1, const CImg& img2, const CImg& img3, const CImg& img4, - const CImg& img5, const CImg& img6, const CImg& img7, const CImg& img8, - const bool is_shared=false) { - assign(8); - _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared); - _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared); - _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared); - return *this; - } - - //! Construct list as a copy of an existing list and force the shared state of the list elements \inplace. - /** - \see CImgList(const CImgList&, bool is_shared). - **/ - template - CImgList& assign(const CImgList& list, const bool is_shared=false) { - cimg::unused(is_shared); - assign(list._width); - cimglist_for(*this,l) _data[l].assign(list[l],false); - return *this; - } - - //! Construct list as a copy of an existing list and force shared state of elements \inplace \specialization. - CImgList& assign(const CImgList& list, const bool is_shared=false) { - if (this==&list) return *this; - CImgList res(list._width); - cimglist_for(res,l) res[l].assign(list[l],is_shared); - return res.move_to(*this); - } - - //! Construct list by reading the content of a file \inplace. - /** - \see CImgList(const char *const). - **/ - CImgList& assign(const char *const filename) { - return load(filename); - } - - //! Construct list from the content of a display window \inplace. - /** - \see CImgList(const CImgDisplay&). - **/ - CImgList& assign(const CImgDisplay &disp) { - return assign(CImg(disp)); - } - - //! Transfer the content of the list instance to another list. - /** - \param list Destination list. - \note When returning, the current list instance is empty and the initial content of \c list is destroyed. - **/ - template - CImgList& move_to(CImgList& list) { - list.assign(_width); - bool is_one_shared_element = false; - cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared; - if (is_one_shared_element) cimglist_for(*this,l) list[l].assign(_data[l]); - else cimglist_for(*this,l) _data[l].move_to(list[l]); - assign(); - return list; - } - - //! Transfer the content of the list instance at a specified position in another list. - /** - \param list Destination list. - \param pos Index of the insertion in the list. - \note When returning, the list instance is empty and the initial content of \c list is preserved - (only images indexes may be modified). - **/ - template - CImgList& move_to(CImgList& list, const unsigned int pos) { - if (is_empty()) return list; - const unsigned int npos = pos>list._width?list._width:pos; - list.insert(_width,npos); - bool is_one_shared_element = false; - cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared; - if (is_one_shared_element) cimglist_for(*this,l) list[npos+l].assign(_data[l]); - else cimglist_for(*this,l) _data[l].move_to(list[npos+l]); - assign(); - return list; - } - - //! Swap all fields between two list instances. - /** - \param list List to swap fields with. - \note Can be used to exchange the content of two lists in a fast way. - **/ - CImgList& swap(CImgList& list) { - cimg::swap(_width,list._width); - cimg::swap(_allocated_width,list._allocated_width); - cimg::swap(_data,list._data); - return list; - } - - //! Return a reference to an empty list. - /** - \note Can be used to define default values in a function taking a CImgList as an argument. - \code - void f(const CImgList& list=CImgList::empty()); - \endcode - **/ - static CImgList& empty() { - static CImgList _empty; - return _empty.assign(); - } - - //@} - //------------------------------------------ - // - //! \name Overloaded Operators - //@{ - //------------------------------------------ - - //! Return a reference to one image element of the list. - /** - \param pos Indice of the image element. - **/ - CImg& operator()(const unsigned int pos) { -#if cimg_verbosity>=3 - if (pos>=_width) { - cimg::warn(_cimglist_instance - "operator(): Invalid image request, at position [%u].", - cimglist_instance, - pos); - return *_data; - } -#endif - return _data[pos]; - } - - //! Return a reference to one image of the list. - /** - \param pos Indice of the image element. - **/ - const CImg& operator()(const unsigned int pos) const { - return const_cast*>(this)->operator()(pos); - } - - //! Return a reference to one pixel value of one image of the list. - /** - \param pos Indice of the image element. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note list(n,x,y,z,c) is equivalent to list[n](x,y,z,c). - **/ - T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0, - const unsigned int z=0, const unsigned int c=0) { - return (*this)[pos](x,y,z,c); - } - - //! Return a reference to one pixel value of one image of the list \const. - const T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0, - const unsigned int z=0, const unsigned int c=0) const { - return (*this)[pos](x,y,z,c); - } - - //! Return pointer to the first image of the list. - /** - \note Images in a list are stored as a buffer of \c CImg. - **/ - operator CImg*() { - return _data; - } - - //! Return pointer to the first image of the list \const. - operator const CImg*() const { - return _data; - } - - //! Construct list from one image \inplace. - /** - \param img Input image to copy in the constructed list. - \note list = img; is equivalent to list.assign(img);. - **/ - template - CImgList& operator=(const CImg& img) { - return assign(img); - } - - //! Construct list from another list. - /** - \param list Input list to copy. - \note list1 = list2 is equivalent to list1.assign(list2);. - **/ - template - CImgList& operator=(const CImgList& list) { - return assign(list); - } - - //! Construct list from another list \specialization. - CImgList& operator=(const CImgList& list) { - return assign(list); - } - - //! Construct list by reading the content of a file \inplace. - /** - \see CImgList(const char *const). - **/ - CImgList& operator=(const char *const filename) { - return assign(filename); - } - - //! Construct list from the content of a display window \inplace. - /** - \see CImgList(const CImgDisplay&). - **/ - CImgList& operator=(const CImgDisplay& disp) { - return assign(disp); - } - - //! Return a non-shared copy of a list. - /** - \note +list is equivalent to CImgList(list,false). - It forces the copy to have non-shared elements. - **/ - CImgList operator+() const { - return CImgList(*this,false); - } - - //! Return a copy of the list instance, where image \c img has been inserted at the end. - /** - \param img Image inserted at the end of the instance copy. - \note Define a convenient way to create temporary lists of images, as in the following code: - \code - (img1,img2,img3,img4).display("My four images"); - \endcode - **/ - template - CImgList& operator,(const CImg& img) { - return insert(img); - } - - //! Return a copy of the list instance, where image \c img has been inserted at the end \const. - template - CImgList operator,(const CImg& img) const { - return (+*this).insert(img); - } - - //! Return a copy of the list instance, where all elements of input list \c list have been inserted at the end. - /** - \param list List inserted at the end of the instance copy. - **/ - template - CImgList& operator,(const CImgList& list) { - return insert(list); - } - - //! Return a copy of the list instance, where all elements of input \c list have been inserted at the end \const. - template - CImgList& operator,(const CImgList& list) const { - return (+*this).insert(list); - } - - //! Return image corresponding to the appending of all images of the instance list along specified axis. - /** - \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. - \note list>'x' is equivalent to list.get_append('x'). - **/ - CImg operator>(const char axis) const { - return get_append(axis,0); - } - - //! Return list corresponding to the splitting of all images of the instance list along specified axis. - /** - \param axis Axis used for image splitting. - \note list<'x' is equivalent to list.get_split('x'). - **/ - CImgList operator<(const char axis) const { - return get_split(axis); - } - - //@} - //------------------------------------- - // - //! \name Instance Characteristics - //@{ - //------------------------------------- - - //! Return the type of image pixel values as a C string. - /** - Return a \c char* string containing the usual type name of the image pixel values - (i.e. a stringified version of the template parameter \c T). - \note - - The returned string may contain spaces (as in \c "unsigned char"). - - If the pixel type \c T does not correspond to a registered type, the string "unknown" is returned. - **/ - static const char* pixel_type() { - return cimg::type::string(); - } - - //! Return the size of the list, i.e. the number of images contained in it. - /** - \note Similar to size() but returns result as a (signed) integer. - **/ - int width() const { - return (int)_width; - } - - //! Return the size of the list, i.e. the number of images contained in it. - /** - \note Similar to width() but returns result as an unsigned integer. - **/ - unsigned int size() const { - return _width; - } - - //! Return pointer to the first image of the list. - /** - \note Images in a list are stored as a buffer of \c CImg. - **/ - CImg *data() { - return _data; - } - - //! Return pointer to the first image of the list \const. - const CImg *data() const { - return _data; - } - - //! Return pointer to the pos-th image of the list. - /** - \param pos Indice of the image element to access. - \note list.data(n); is equivalent to list.data + n;. - **/ -#if cimg_verbosity>=3 - CImg *data(const unsigned int pos) { - if (pos>=size()) - cimg::warn(_cimglist_instance - "data(): Invalid pointer request, at position [%u].", - cimglist_instance, - pos); - return _data + pos; - } - - const CImg *data(const unsigned int l) const { - return const_cast*>(this)->data(l); - } -#else - CImg *data(const unsigned int l) { - return _data + l; - } - - //! Return pointer to the pos-th image of the list \const. - const CImg *data(const unsigned int l) const { - return _data + l; - } -#endif - - //! Return iterator to the first image of the list. - /** - **/ - iterator begin() { - return _data; - } - - //! Return iterator to the first image of the list \const. - const_iterator begin() const { - return _data; - } - - //! Return iterator to one position after the last image of the list. - /** - **/ - iterator end() { - return _data + _width; - } - - //! Return iterator to one position after the last image of the list \const. - const_iterator end() const { - return _data + _width; - } - - //! Return reference to the first image of the list. - /** - **/ - CImg& front() { - return *_data; - } - - //! Return reference to the first image of the list \const. - const CImg& front() const { - return *_data; - } - - //! Return a reference to the last image of the list. - /** - **/ - const CImg& back() const { - return *(_data + _width - 1); - } - - //! Return a reference to the last image of the list \const. - CImg& back() { - return *(_data + _width - 1); - } - - //! Return pos-th image of the list. - /** - \param pos Indice of the image element to access. - **/ - CImg& at(const int pos) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "at(): Empty instance.", - cimglist_instance); - - return _data[pos<0?0:pos>=(int)_width?(int)_width-1:pos]; - } - - //! Access to pixel value with Dirichlet boundary conditions. - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c offset is outside image bounds. - \note list.atNXYZC(p,x,y,z,c); is equivalent to list[p].atXYZC(x,y,z,c);. - **/ - T& atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T out_value) { - return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atXYZC(x,y,z,c,out_value); - } - - //! Access to pixel value with Dirichlet boundary conditions \const. - T atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { - return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atXYZC(x,y,z,c,out_value); - } - - //! Access to pixel value with Neumann boundary conditions. - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note list.atNXYZC(p,x,y,z,c); is equivalent to list[p].atXYZC(x,y,z,c);. - **/ - T& atNXYZC(const int pos, const int x, const int y, const int z, const int c) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNXYZC(): Empty instance.", - cimglist_instance); - - return _atNXYZC(pos,x,y,z,c); - } - - //! Access to pixel value with Neumann boundary conditions \const. - T atNXYZC(const int pos, const int x, const int y, const int z, const int c) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNXYZC(): Empty instance.", - cimglist_instance); - - return _atNXYZC(pos,x,y,z,c); - } - - T& _atNXYZC(const int pos, const int x, const int y, const int z, const int c) { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZC(x,y,z,c); - } - - T _atNXYZC(const int pos, const int x, const int y, const int z, const int c) const { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZC(x,y,z,c); - } - - //! Access pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y,\c z). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c offset is outside image bounds. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T out_value) { - return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atXYZ(x,y,z,c,out_value); - } - - //! Access pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y,\c z) \const. - T atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { - return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atXYZ(x,y,z,c,out_value); - } - - //! Access to pixel value with Neumann boundary conditions for the 4 first coordinates (\c pos, \c x,\c y,\c z). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNXYZ(): Empty instance.", - cimglist_instance); - - return _atNXYZ(pos,x,y,z,c); - } - - //! Access to pixel value with Neumann boundary conditions for the 4 first coordinates (\c pos, \c x,\c y,\c z) \const. - T atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNXYZ(): Empty instance.", - cimglist_instance); - - return _atNXYZ(pos,x,y,z,c); - } - - T& _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZ(x,y,z,c); - } - - T _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXYZ(x,y,z,c); - } - - //! Access to pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c offset is outside image bounds. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atNXY(const int pos, const int x, const int y, const int z, const int c, const T out_value) { - return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atXY(x,y,z,c,out_value); - } - - //! Access to pixel value with Dirichlet boundary conditions for the 3 first coordinates (\c pos, \c x,\c y) \const. - T atNXY(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { - return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atXY(x,y,z,c,out_value); - } - - //! Access to pixel value with Neumann boundary conditions for the 3 first coordinates (\c pos, \c x,\c y). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNXY(): Empty instance.", - cimglist_instance); - - return _atNXY(pos,x,y,z,c); - } - - //! Access to pixel value with Neumann boundary conditions for the 3 first coordinates (\c pos, \c x,\c y) \const. - T atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNXY(): Empty instance.", - cimglist_instance); - - return _atNXY(pos,x,y,z,c); - } - - T& _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXY(x,y,z,c); - } - - T _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atXY(x,y,z,c); - } - - //! Access to pixel value with Dirichlet boundary conditions for the 2 first coordinates (\c pos,\c x). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c offset is outside image bounds. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atNX(const int pos, const int x, const int y, const int z, const int c, const T out_value) { - return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):_data[pos].atX(x,y,z,c,out_value); - } - - //! Access to pixel value with Dirichlet boundary conditions for the 2 first coordinates (\c pos,\c x) \const. - T atNX(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { - return (pos<0 || pos>=(int)_width)?out_value:_data[pos].atX(x,y,z,c,out_value); - } - - //! Access to pixel value with Neumann boundary conditions for the 2 first coordinates (\c pos, \c x). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNX(): Empty instance.", - cimglist_instance); - - return _atNX(pos,x,y,z,c); - } - - //! Access to pixel value with Neumann boundary conditions for the 2 first coordinates (\c pos, \c x) \const. - T atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atNX(): Empty instance.", - cimglist_instance); - - return _atNX(pos,x,y,z,c); - } - - T& _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atX(x,y,z,c); - } - - T _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)].atX(x,y,z,c); - } - - //! Access to pixel value with Dirichlet boundary conditions for the first coordinate (\c pos). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \param out_value Default value returned if \c offset is outside image bounds. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atN(const int pos, const int x, const int y, const int z, const int c, const T out_value) { - return (pos<0 || pos>=(int)_width)?(cimg::temporary(out_value)=out_value):(*this)(pos,x,y,z,c); - } - - //! Access to pixel value with Dirichlet boundary conditions for the first coordinate (\c pos) \const. - T atN(const int pos, const int x, const int y, const int z, const int c, const T out_value) const { - return (pos<0 || pos>=(int)_width)?out_value:(*this)(pos,x,y,z,c); - } - - //! Return pixel value with Neumann boundary conditions for the first coordinate (\c pos). - /** - \param pos Indice of the image element to access. - \param x X-coordinate of the pixel value. - \param y Y-coordinate of the pixel value. - \param z Z-coordinate of the pixel value. - \param c C-coordinate of the pixel value. - \note list.atNXYZ(p,x,y,z,c); is equivalent to list[p].atXYZ(x,y,z,c);. - **/ - T& atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atN(): Empty instance.", - cimglist_instance); - return _atN(pos,x,y,z,c); - } - - //! Return pixel value with Neumann boundary conditions for the first coordinate (\c pos) \const. - T atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "atN(): Empty instance.", - cimglist_instance); - return _atN(pos,x,y,z,c); - } - - T& _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)](x,y,z,c); - } - - T _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const { - return _data[pos<0?0:(pos>=(int)_width?(int)_width-1:pos)](x,y,z,c); - } - - //! Return a C-string containing the values of all images in the instance list. - /** - \param separator Character separator set between consecutive pixel values. - \param max_size Maximum size of the returned string. - \note The result is returne as a CImg image whose pixel buffer contains the desired C-string. - **/ - CImg value_string(const char separator=',', const unsigned int max_size=0) const { - if (is_empty()) return CImg(1,1,1,1,0); - CImgList items; - for (unsigned int l = 0; l<_width-1; ++l) { - CImg item = _data[l].value_string(separator,0); - item.back() = separator; - item.move_to(items); - } - _data[_width-1].value_string(separator,0).move_to(items); - CImg res; (items>'x').move_to(res); - if (max_size) { res.crop(0,max_size); res(max_size) = 0; } - return res; - } - - //@} - //------------------------------------- - // - //! \name Instance Checking - //@{ - //------------------------------------- - - //! Return \c true if list is empty. - /** - **/ - bool is_empty() const { - return (!_data || !_width); - } - - //! Test if number of image elements is equal to specified value. - /** - \param size_n Number of image elements to test. - **/ - bool is_sameN(const unsigned int size_n) const { - return _width==size_n; - } - - //! Test if number of image elements is equal between two images lists. - /** - \param list Input list to compare with. - **/ - template - bool is_sameN(const CImgList& list) const { - return is_sameN(list._width); - } - - // Define useful functions to check list dimensions. - // (cannot be documented because macro-generated). -#define _cimglist_def_is_same1(axis) \ - bool is_same##axis(const unsigned int val) const { \ - bool res = true; \ - for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(val); return res; \ - } \ - bool is_sameN##axis(const unsigned int n, const unsigned int val) const { \ - return is_sameN(n) && is_same##axis(val); \ - } \ - -#define _cimglist_def_is_same2(axis1,axis2) \ - bool is_same##axis1##axis2(const unsigned int val1, const unsigned int val2) const { \ - bool res = true; \ - for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2(val1,val2); return res; \ - } \ - bool is_sameN##axis1##axis2(const unsigned int n, const unsigned int val1, const unsigned int val2) const { \ - return is_sameN(n) && is_same##axis1##axis2(val1,val2); \ - } \ - -#define _cimglist_def_is_same3(axis1,axis2,axis3) \ - bool is_same##axis1##axis2##axis3(const unsigned int val1, const unsigned int val2, \ - const unsigned int val3) const { \ - bool res = true; \ - for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2##axis3(val1,val2,val3); \ - return res; \ - } \ - bool is_sameN##axis1##axis2##axis3(const unsigned int n, const unsigned int val1, \ - const unsigned int val2, const unsigned int val3) const { \ - return is_sameN(n) && is_same##axis1##axis2##axis3(val1,val2,val3); \ - } \ - -#define _cimglist_def_is_same(axis) \ - template bool is_same##axis(const CImg& img) const { \ - bool res = true; for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(img); return res; \ - } \ - template bool is_same##axis(const CImgList& list) const { \ - const unsigned int lmin = cimg::min(_width,list._width); \ - bool res = true; for (unsigned int l = 0; l bool is_sameN##axis(const unsigned int n, const CImg& img) const { \ - return (is_sameN(n) && is_same##axis(img)); \ - } \ - template bool is_sameN##axis(const CImgList& list) const { \ - return (is_sameN(list) && is_same##axis(list)); \ - } - - _cimglist_def_is_same(XY) - _cimglist_def_is_same(XZ) - _cimglist_def_is_same(XC) - _cimglist_def_is_same(YZ) - _cimglist_def_is_same(YC) - _cimglist_def_is_same(XYZ) - _cimglist_def_is_same(XYC) - _cimglist_def_is_same(YZC) - _cimglist_def_is_same(XYZC) - _cimglist_def_is_same1(X) - _cimglist_def_is_same1(Y) - _cimglist_def_is_same1(Z) - _cimglist_def_is_same1(C) - _cimglist_def_is_same2(X,Y) - _cimglist_def_is_same2(X,Z) - _cimglist_def_is_same2(X,C) - _cimglist_def_is_same2(Y,Z) - _cimglist_def_is_same2(Y,C) - _cimglist_def_is_same2(Z,C) - _cimglist_def_is_same3(X,Y,Z) - _cimglist_def_is_same3(X,Y,C) - _cimglist_def_is_same3(X,Z,C) - _cimglist_def_is_same3(Y,Z,C) - - //! Test if dimensions of each image of the list match specified arguments. - /** - \param dx Checked image width. - \param dy Checked image height. - \param dz Checked image depth. - \param dc Checked image spectrum. - **/ - bool is_sameXYZC(const unsigned int dx, const unsigned int dy, - const unsigned int dz, const unsigned int dc) const { - bool res = true; - for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_sameXYZC(dx,dy,dz,dc); - return res; - } - - //! Test if list dimensions match specified arguments. - /** - \param n Number of images in the list. - \param dx Checked image width. - \param dy Checked image height. - \param dz Checked image depth. - \param dc Checked image spectrum. - **/ - bool is_sameNXYZC(const unsigned int n, - const unsigned int dx, const unsigned int dy, - const unsigned int dz, const unsigned int dc) const { - return is_sameN(n) && is_sameXYZC(dx,dy,dz,dc); - } - - //! Test if list contains one particular pixel location. - /** - \param n Index of the image whom checked pixel value belong to. - \param x X-coordinate of the checked pixel value. - \param y Y-coordinate of the checked pixel value. - \param z Z-coordinate of the checked pixel value. - \param c C-coordinate of the checked pixel value. - **/ - bool containsNXYZC(const int n, const int x=0, const int y=0, const int z=0, const int c=0) const { - if (is_empty()) return false; - return n>=0 && n<(int)_width && x>=0 && x<_data[n].width() && y>=0 && y<_data[n].height() && - z>=0 && z<_data[n].depth() && c>=0 && c<_data[n].spectrum(); - } - - //! Test if list contains image with specified indice. - /** - \param n Index of the checked image. - **/ - bool containsN(const int n) const { - if (is_empty()) return false; - return n>=0 && n<(int)_width; - } - - //! Test if one image of the list contains the specified referenced value. - /** - \param pixel Reference to pixel value to test. - \param[out] n Index of image containing the pixel value, if test succeeds. - \param[out] x X-coordinate of the pixel value, if test succeeds. - \param[out] y Y-coordinate of the pixel value, if test succeeds. - \param[out] z Z-coordinate of the pixel value, if test succeeds. - \param[out] c C-coordinate of the pixel value, if test succeeds. - \note If true, set coordinates (n,x,y,z,c). - **/ - template - bool contains(const T& pixel, t& n, t& x, t&y, t& z, t& c) const { - if (is_empty()) return false; - cimglist_for(*this,l) if (_data[l].contains(pixel,x,y,z,c)) { n = (t)l; return true; } - return false; - } - - //! Test if one of the image list contains the specified referenced value. - /** - \param pixel Reference to pixel value to test. - \param[out] n Index of image containing the pixel value, if test succeeds. - \param[out] x X-coordinate of the pixel value, if test succeeds. - \param[out] y Y-coordinate of the pixel value, if test succeeds. - \param[out] z Z-coordinate of the pixel value, if test succeeds. - \note If true, set coordinates (n,x,y,z). - **/ - template - bool contains(const T& pixel, t& n, t& x, t&y, t& z) const { - t c; - return contains(pixel,n,x,y,z,c); - } - - //! Test if one of the image list contains the specified referenced value. - /** - \param pixel Reference to pixel value to test. - \param[out] n Index of image containing the pixel value, if test succeeds. - \param[out] x X-coordinate of the pixel value, if test succeeds. - \param[out] y Y-coordinate of the pixel value, if test succeeds. - \note If true, set coordinates (n,x,y). - **/ - template - bool contains(const T& pixel, t& n, t& x, t&y) const { - t z, c; - return contains(pixel,n,x,y,z,c); - } - - //! Test if one of the image list contains the specified referenced value. - /** - \param pixel Reference to pixel value to test. - \param[out] n Index of image containing the pixel value, if test succeeds. - \param[out] x X-coordinate of the pixel value, if test succeeds. - \note If true, set coordinates (n,x). - **/ - template - bool contains(const T& pixel, t& n, t& x) const { - t y, z, c; - return contains(pixel,n,x,y,z,c); - } - - //! Test if one of the image list contains the specified referenced value. - /** - \param pixel Reference to pixel value to test. - \param[out] n Index of image containing the pixel value, if test succeeds. - \note If true, set coordinates (n). - **/ - template - bool contains(const T& pixel, t& n) const { - t x, y, z, c; - return contains(pixel,n,x,y,z,c); - } - - //! Test if one of the image list contains the specified referenced value. - /** - \param pixel Reference to pixel value to test. - **/ - bool contains(const T& pixel) const { - unsigned int n, x, y, z, c; - return contains(pixel,n,x,y,z,c); - } - - //! Test if the list contains the image 'img'. - /** - \param img Reference to image to test. - \param[out] n Index of image in the list, if test succeeds. - \note If true, returns the position (n) of the image in the list. - **/ - template - bool contains(const CImg& img, t& n) const { - if (is_empty()) return false; - const CImg *const ptr = &img; - cimglist_for(*this,i) if (_data+i==ptr) { n = (t)i; return true; } - return false; - } - - //! Test if the list contains the image img. - /** - \param img Reference to image to test. - **/ - bool contains(const CImg& img) const { - unsigned int n; - return contains(img,n); - } - - //@} - //------------------------------------- - // - //! \name Mathematical Functions - //@{ - //------------------------------------- - - //! Return a reference to the minimum pixel value of the instance list. - /** - **/ - T& min() { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "min(): Empty instance.", - cimglist_instance); - T *ptr_min = _data->_data; - T min_value = *ptr_min; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) if (*ptrs_data; - T min_value = *ptr_min; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) if (*ptrs_data; - T max_value = *ptr_max; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs); - } - return *ptr_max; - } - - //! Return a reference to the maximum pixel value of the instance list \const. - const T& max() const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "max(): Empty instance.", - cimglist_instance); - const T *ptr_max = _data->_data; - T max_value = *ptr_max; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs); - } - return *ptr_max; - } - - //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well. - /** - \param[out] max_val Value of the maximum value found. - **/ - template - T& min_max(t& max_val) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "min_max(): Empty instance.", - cimglist_instance); - T *ptr_min = _data->_data; - T min_value = *ptr_min, max_value = min_value; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) { - const T val = *ptrs; - if (valmax_value) max_value = val; - } - } - max_val = (t)max_value; - return *ptr_min; - } - - //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well \const. - /** - \param[out] max_val Value of the maximum value found. - **/ - template - const T& min_max(t& max_val) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "min_max(): Empty instance.", - cimglist_instance); - const T *ptr_min = _data->_data; - T min_value = *ptr_min, max_value = min_value; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) { - const T val = *ptrs; - if (valmax_value) max_value = val; - } - } - max_val = (t)max_value; - return *ptr_min; - } - - //! Return a reference to the minimum pixel value of the instance list and return the minimum value as well. - /** - \param[out] min_val Value of the minimum value found. - **/ - template - T& max_min(t& min_val) { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "max_min(): Empty instance.", - cimglist_instance); - T *ptr_max = _data->_data; - T min_value = *ptr_max, max_value = min_value; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) { - const T val = *ptrs; - if (val>max_value) { max_value = val; ptr_max = ptrs; } - if (val - const T& max_min(t& min_val) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "max_min(): Empty instance.", - cimglist_instance); - const T *ptr_max = _data->_data; - T min_value = *ptr_max, max_value = min_value; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - cimg_for(img,ptrs,T) { - const T val = *ptrs; - if (val>max_value) { max_value = val; ptr_max = ptrs; } - if (val - CImgList& insert(const CImg& img, const unsigned int pos=~0U, const bool is_shared=false) { - const unsigned int npos = pos==~0U?_width:pos; - if (npos>_width) - throw CImgArgumentException(_cimglist_instance - "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) " - "at position %u.", - cimglist_instance, - img._width,img._height,img._depth,img._spectrum,img._data,npos); - if (is_shared) - throw CImgArgumentException(_cimglist_instance - "insert(): Invalid insertion request of specified shared image " - "CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).", - cimglist_instance, - img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos); - - CImg *const new_data = (++_width>_allocated_width)?new CImg[_allocated_width?(_allocated_width<<=1): - (_allocated_width=16)]:0; - if (!_data) { // Insert new element into empty list. - _data = new_data; - *_data = img; - } else { - if (new_data) { // Insert with re-allocation. - if (npos) std::memcpy(new_data,_data,sizeof(CImg)*npos); - if (npos!=_width-1) std::memcpy(new_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); - std::memset(_data,0,sizeof(CImg)*(_width-1)); - delete[] _data; - _data = new_data; - } else if (npos!=_width-1) - std::memmove(_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); // Insert without re-allocation. - _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0; _data[npos]._data = 0; - _data[npos] = img; - } - return *this; - } - - //! Insert a copy of the image \c img into the current image list, at position \c pos \specialization. - CImgList& insert(const CImg& img, const unsigned int pos=~0U, const bool is_shared=false) { - const unsigned int npos = pos==~0U?_width:pos; - if (npos>_width) - throw CImgArgumentException(_cimglist_instance - "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) " - "at position %u.", - cimglist_instance, - img._width,img._height,img._depth,img._spectrum,img._data,npos); - CImg *const new_data = (++_width>_allocated_width)?new CImg[_allocated_width?(_allocated_width<<=1): - (_allocated_width=16)]:0; - if (!_data) { // Insert new element into empty list. - _data = new_data; - if (is_shared && img) { - _data->_width = img._width; - _data->_height = img._height; - _data->_depth = img._depth; - _data->_spectrum = img._spectrum; - _data->_is_shared = true; - _data->_data = img._data; - } else *_data = img; - } - else { - if (new_data) { // Insert with re-allocation. - if (npos) std::memcpy(new_data,_data,sizeof(CImg)*npos); - if (npos!=_width-1) std::memcpy(new_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); - if (is_shared && img) { - new_data[npos]._width = img._width; - new_data[npos]._height = img._height; - new_data[npos]._depth = img._depth; - new_data[npos]._spectrum = img._spectrum; - new_data[npos]._is_shared = true; - new_data[npos]._data = img._data; - } else { - new_data[npos]._width = new_data[npos]._height = new_data[npos]._depth = new_data[npos]._spectrum = 0; - new_data[npos]._data = 0; - new_data[npos] = img; - } - std::memset(_data,0,sizeof(CImg)*(_width-1)); - delete[] _data; - _data = new_data; - } else { // Insert without re-allocation. - if (npos!=_width-1) std::memmove(_data+npos+1,_data+npos,sizeof(CImg)*(_width-1-npos)); - if (is_shared && img) { - _data[npos]._width = img._width; - _data[npos]._height = img._height; - _data[npos]._depth = img._depth; - _data[npos]._spectrum = img._spectrum; - _data[npos]._is_shared = true; - _data[npos]._data = img._data; - } else { - _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0; - _data[npos]._data = 0; - _data[npos] = img; - } - } - } - return *this; - } - - //! Insert a copy of the image \c img into the current image list, at position \c pos \newinstance. - template - CImgList get_insert(const CImg& img, const unsigned int pos=~0U, const bool is_shared=false) const { - return (+*this).insert(img,pos,is_shared); - } - - //! Insert n empty images img into the current image list, at position \p pos. - /** - \param n Number of empty images to insert. - \param pos Index of the insertion. - **/ - CImgList& insert(const unsigned int n, const unsigned int pos=~0U) { - CImg empty; - if (!n) return *this; - const unsigned int npos = pos==~0U?_width:pos; - for (unsigned int i = 0; i get_insert(const unsigned int n, const unsigned int pos=~0U) const { - return (+*this).insert(n,pos); - } - - //! Insert \c n copies of the image \c img into the current image list, at position \c pos. - /** - \param n Number of image copies to insert. - \param img Image to insert by copy. - \param pos Index of the insertion. - \param is_shared Tells if inserted images are shared copies of \c img or not. - **/ - template - CImgList& insert(const unsigned int n, const CImg& img, const unsigned int pos=~0U, - const bool is_shared=false) { - if (!n) return *this; - const unsigned int npos = pos==~0U?_width:pos; - insert(img,npos,is_shared); - for (unsigned int i = 1; i - CImgList get_insert(const unsigned int n, const CImg& img, const unsigned int pos=~0U, - const bool is_shared=false) const { - return (+*this).insert(n,img,pos,is_shared); - } - - //! Insert a copy of the image list \c list into the current image list, starting from position \c pos. - /** - \param list Image list to insert. - \param pos Index of the insertion. - \param is_shared Tells if inserted images are shared copies of images of \c list or not. - **/ - template - CImgList& insert(const CImgList& list, const unsigned int pos=~0U, const bool is_shared=false) { - const unsigned int npos = pos==~0U?_width:pos; - if ((void*)this!=(void*)&list) cimglist_for(list,l) insert(list[l],npos+l,is_shared); - else insert(CImgList(list),npos,is_shared); - return *this; - } - - //! Insert a copy of the image list \c list into the current image list, starting from position \c pos \newinstance. - template - CImgList get_insert(const CImgList& list, const unsigned int pos=~0U, const bool is_shared=false) const { - return (+*this).insert(list,pos,is_shared); - } - - //! Insert n copies of the list \c list at position \c pos of the current list. - /** - \param n Number of list copies to insert. - \param list Image list to insert. - \param pos Index of the insertion. - \param is_shared Tells if inserted images are shared copies of images of \c list or not. - **/ - template - CImgList& insert(const unsigned int n, const CImgList& list, const unsigned int pos=~0U, - const bool is_shared=false) { - if (!n) return *this; - const unsigned int npos = pos==~0U?_width:pos; - for (unsigned int i = 0; i - CImgList get_insert(const unsigned int n, const CImgList& list, const unsigned int pos=~0U, - const bool is_shared=false) const { - return (+*this).insert(n,list,pos,is_shared); - } - - //! Remove all images between from indexes. - /** - \param pos1 Starting index of the removal. - \param pos2 Ending index of the removal. - **/ - CImgList& remove(const unsigned int pos1, const unsigned int pos2) { - const unsigned int - npos1 = pos1=_width) - throw CImgArgumentException(_cimglist_instance - "remove(): Invalid remove request at positions %u->%u.", - cimglist_instance, - npos1,tpos2); - else { - if (tpos2>=_width) - throw CImgArgumentException(_cimglist_instance - "remove(): Invalid remove request at positions %u->%u.", - cimglist_instance, - npos1,tpos2); - - for (unsigned int k = npos1; k<=npos2; ++k) _data[k].assign(); - const unsigned int nb = 1 + npos2 - npos1; - if (!(_width-=nb)) return assign(); - if (_width>(_allocated_width>>2) || _allocated_width<=16) { // Removing items without reallocation. - if (npos1!=_width) std::memmove(_data+npos1,_data+npos2+1,sizeof(CImg)*(_width - npos1)); - std::memset(_data + _width,0,sizeof(CImg)*nb); - } else { // Removing items with reallocation. - _allocated_width>>=2; - while (_allocated_width>16 && _width<(_allocated_width>>1)) _allocated_width>>=1; - CImg *const new_data = new CImg[_allocated_width]; - if (npos1) std::memcpy(new_data,_data,sizeof(CImg)*npos1); - if (npos1!=_width) std::memcpy(new_data+npos1,_data+npos2+1,sizeof(CImg)*(_width-npos1)); - if (_width!=_allocated_width) std::memset(new_data+_width,0,sizeof(_allocated_width - _width)); - std::memset(_data,0,sizeof(CImg)*(_width+nb)); - delete[] _data; - _data = new_data; - } - } - return *this; - } - - //! Remove all images between from indexes \newinstance. - CImgList get_remove(const unsigned int pos1, const unsigned int pos2) const { - return (+*this).remove(pos1,pos2); - } - - //! Remove image at index \c pos from the image list. - /** - \param pos Index of the image to remove. - **/ - CImgList& remove(const unsigned int pos) { - return remove(pos,pos); - } - - //! Remove image at index \c pos from the image list \newinstance. - CImgList get_remove(const unsigned int pos) const { - return (+*this).remove(pos); - } - - //! Remove last image. - /** - **/ - CImgList& remove() { - return remove(_width-1); - } - - //! Remove last image \newinstance. - CImgList get_remove() const { - return (+*this).remove(); - } - - //! Reverse list order. - CImgList& reverse() { - for (unsigned int l = 0; l<_width/2; ++l) (*this)[l].swap((*this)[_width-1-l]); - return *this; - } - - //! Reverse list order \newinstance. - CImgList get_reverse() const { - return (+*this).reverse(); - } - - //! Return a sublist. - /** - \param pos0 Starting index of the sublist. - \param pos1 Ending index of the sublist. - **/ - CImgList& images(const unsigned int pos0, const unsigned int pos1) { - return get_images(pos0,pos1).move_to(*this); - } - - //! Return a sublist \newinstance. - CImgList get_images(const unsigned int pos0, const unsigned int pos1) const { - if (pos0>pos1 || pos1>=_width) - throw CImgArgumentException(_cimglist_instance - "images(): Specified sub-list indices (%u->%u) are out of bounds.", - cimglist_instance, - pos0,pos1); - CImgList res(pos1-pos0+1); - cimglist_for(res,l) res[l].assign(_data[pos0+l]); - return res; - } - - //! Return a shared sublist. - /** - \param pos0 Starting index of the sublist. - \param pos1 Ending index of the sublist. - **/ - CImgList get_shared_images(const unsigned int pos0, const unsigned int pos1) { - if (pos0>pos1 || pos1>=_width) - throw CImgArgumentException(_cimglist_instance - "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.", - cimglist_instance, - pos0,pos1); - CImgList res(pos1-pos0+1); - cimglist_for(res,l) res[l].assign(_data[pos0+l],_data[pos0+l]?true:false); - return res; - } - - //! Return a shared sublist \newinstance. - const CImgList get_shared_images(const unsigned int pos0, const unsigned int pos1) const { - if (pos0>pos1 || pos1>=_width) - throw CImgArgumentException(_cimglist_instance - "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.", - cimglist_instance, - pos0,pos1); - CImgList res(pos1-pos0+1); - cimglist_for(res,l) res[l].assign(_data[pos0+l],_data[pos0+l]?true:false); - return res; - } - - //! Return a single image which is the appending of all images of the current CImgList instance. - /** - \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - CImg get_append(const char axis, const float align=0) const { - if (is_empty()) return CImg(); - if (_width==1) return +((*this)[0]); - unsigned int dx = 0, dy = 0, dz = 0, dc = 0, pos = 0; - CImg res; - switch (cimg::uncase(axis)) { - case 'x' : { // Along the X-axis. - cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) { - dx+=img._width; - dy = cimg::max(dy,img._height); - dz = cimg::max(dz,img._depth); - dc = cimg::max(dc,img._spectrum); - } - } - res.assign(dx,dy,dz,dc,0); - if (res) cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) res.draw_image(pos, - (int)(align*(dy-img._height)), - (int)(align*(dz-img._depth)), - (int)(align*(dc-img._spectrum)), - img); - pos+=img._width; - } - } break; - case 'y' : { // Along the Y-axis. - cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) { - dx = cimg::max(dx,img._width); - dy+=img._height; - dz = cimg::max(dz,img._depth); - dc = cimg::max(dc,img._spectrum); - } - } - res.assign(dx,dy,dz,dc,0); - if (res) cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) res.draw_image((int)(align*(dx-img._width)), - pos, - (int)(align*(dz-img._depth)), - (int)(align*(dc-img._spectrum)), - img); - pos+=img._height; - } - } break; - case 'z' : { // Along the Z-axis. - cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) { - dx = cimg::max(dx,img._width); - dy = cimg::max(dy,img._height); - dz+=img._depth; - dc = cimg::max(dc,img._spectrum); - } - } - res.assign(dx,dy,dz,dc,0); - if (res) cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) res.draw_image((int)(align*(dx-img._width)), - (int)(align*(dy-img._height)), - pos, - (int)(align*(dc-img._spectrum)), - img); - pos+=img._depth; - } - } break; - default : { // Along the C-axis. - cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) { - dx = cimg::max(dx,img._width); - dy = cimg::max(dy,img._height); - dz = cimg::max(dz,img._depth); - dc+=img._spectrum; - } - } - res.assign(dx,dy,dz,dc,0); - if (res) cimglist_for(*this,l) { - const CImg& img = (*this)[l]; - if (img) res.draw_image((int)(align*(dx-img._width)), - (int)(align*(dy-img._height)), - (int)(align*(dz-img._depth)), - pos, - img); - pos+=img._spectrum; - } - } - } - return res; - } - - //! Return a list where each image has been split along the specified axis. - /** - \param axis Axis to split images along. - \param nb Number of spliting parts for each image. - **/ - CImgList& split(const char axis, const int nb=0) { - return get_split(axis,nb).move_to(*this); - } - - //! Return a list where each image has been split along the specified axis \newinstance. - CImgList get_split(const char axis, const int nb=0) const { - CImgList res; - cimglist_for(*this,l) _data[l].get_split(axis,nb).move_to(res,~0U); - return res; - } - - //! Insert image at the end of the list. - /** - \param img Image to insert. - **/ - template - CImgList& push_back(const CImg& img) { - return insert(img); - } - - //! Insert image at the front of the list. - /** - \param img Image to insert. - **/ - template - CImgList& push_front(const CImg& img) { - return insert(img,0); - } - - //! Insert list at the end of the current list. - /** - \param list List to insert. - **/ - template - CImgList& push_back(const CImgList& list) { - return insert(list); - } - - //! Insert list at the front of the current list. - /** - \param list List to insert. - **/ - template - CImgList& push_front(const CImgList& list) { - return insert(list,0); - } - - //! Remove last image. - /** - **/ - CImgList& pop_back() { - return remove(_width-1); - } - - //! Remove first image. - /** - **/ - CImgList& pop_front() { - return remove(0); - } - - //! Remove image pointed by iterator. - /** - \param iter Iterator pointing to the image to remove. - **/ - CImgList& erase(const iterator iter) { - return remove(iter-_data); - } - - //@} - //---------------------------------- - // - //! \name Data Input - //@{ - //---------------------------------- - - //! Display a simple interactive interface to select images or sublists. - /** - \param disp Window instance to display selection and user interface. - \param feature_type Can be \c false to select a single image, or \c true to select a sublist. - \param axis Axis along whom images are appended for visualization. - \param align Alignment setting when images have not all the same size. - \return A one-column vector containing the selected image indexes. - **/ - CImg get_select(CImgDisplay &disp, const bool feature_type=true, - const char axis='x', const float align=0) const { - return _get_select(disp,0,feature_type,axis,align,0,false,false,false); - } - - //! Display a simple interactive interface to select images or sublists. - /** - \param title Title of a new window used to display selection and user interface. - \param feature_type Can be \c false to select a single image, or \c true to select a sublist. - \param axis Axis along whom images are appended for visualization. - \param align Alignment setting when images have not all the same size. - \return A one-column vector containing the selected image indexes. - **/ - CImg get_select(const char *const title, const bool feature_type=true, - const char axis='x', const float align=0) const { - CImgDisplay disp; - return _get_select(disp,title,feature_type,axis,align,0,false,false,false); - } - - CImg _get_select(CImgDisplay &disp, const char *const title, const bool feature_type, - const char axis, const float align, - const unsigned int orig, const bool resize_disp, - const bool exit_on_rightbutton, const bool exit_on_wheel) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "select(): Empty instance.", - cimglist_instance); - - // Create image correspondence table and get list dimensions for visualization. - CImgList _indices; - unsigned int max_width = 0, max_height = 0, sum_width = 0, sum_height = 0; - cimglist_for(*this,l) { - const CImg& img = _data[l]; - const unsigned int - w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false), - h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true); - if (w>max_width) max_width = w; - if (h>max_height) max_height = h; - sum_width+=w; sum_height+=h; - if (axis=='x') CImg(w,1,1,1,(unsigned int)l).move_to(_indices); - else CImg(h,1,1,1,(unsigned int)l).move_to(_indices); - } - const CImg indices0 = _indices>'x'; - - // Create display window. - if (!disp) { - if (axis=='x') disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:0,1); - else disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:0,1); - if (!title) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width); - } else if (title) disp.set_title("%s",title); - if (resize_disp) { - if (axis=='x') disp.resize(cimg_fitscreen(sum_width,max_height,1),false); - else disp.resize(cimg_fitscreen(max_width,sum_height,1),false); - } - - const unsigned int old_normalization = disp.normalization(); - bool old_is_resized = disp.is_resized(); - disp._normalization = 0; - disp.show().set_key(0); - const unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 }; - - // Enter event loop. - CImg visu0, visu; - CImg indices; - CImg positions(_width,4,1,1,-1); - int oindice0 = -1, oindice1 = -1, indice0 = -1, indice1 = -1; - bool is_clicked = false, is_selected = false, text_down = false, update_display = true; - unsigned int key = 0; - while (!is_selected && !disp.is_closed() && !key) { - - // Create background image. - if (!visu0) { - visu0.assign(disp._width,disp._height,1,3,0); visu.assign(); - (indices0.get_resize(axis=='x'?visu0._width:visu0._height,1)).move_to(indices); - unsigned int ind = 0; - if (axis=='x') for (unsigned int x = 0; x - onexone(1,1,1,1,0), - &src = _data[ind]?_data[ind]:onexone; - CImg res; - src.__get_select(disp,old_normalization,(src._width-1)/2,(src._height-1)/2,(src._depth-1)/2).move_to(res); - const unsigned int h = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,true); - res.resize(x - x0,cimg::max(32U,h*disp._height/max_height),1,res._spectrum==1?3:-100); - positions(ind,0) = positions(ind,2) = (int)x0; - positions(ind,1) = positions(ind,3) = (int)(align*(visu0.height()-res.height())); - positions(ind,2)+=res._width; - positions(ind,3)+=res._height - 1; - visu0.draw_image(positions(ind,0),positions(ind,1),res); - } else for (unsigned int y = 0; y - &src = _data[ind], - _img2d = src._depth>1?src.get_projections2d((src._width-1)/2,(src._height-1)/2,(src._depth-1)/2): - CImg(), - &img2d = _img2d?_img2d:src; - CImg res = old_normalization==1 || - (old_normalization==3 && cimg::type::string()!=cimg::type::string())? - CImg(img2d.get_normalize(0,255)): - CImg(img2d); - if (res._spectrum>3) res.channels(0,2); - const unsigned int w = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,false); - res.resize(cimg::max(32U,w*disp._width/max_width),y - y0,1,res._spectrum==1?3:-100); - positions(ind,0) = positions(ind,2) = (int)(align*(visu0.width()-res.width())); - positions(ind,1) = positions(ind,3) = (int)y0; - positions(ind,2)+=res._width - 1; - positions(ind,3)+=res._height; - visu0.draw_image(positions(ind,0),positions(ind,1),res); - } - if (axis=='x') --positions(ind,2); else --positions(ind,3); - update_display = true; - } - - if (!visu || oindice0!=indice0 || oindice1!=indice1) { - if (indice0>=0 && indice1>=0) { - visu.assign(visu0,false); - const int indm = cimg::min(indice0,indice1), indM = cimg::max(indice0,indice1); - for (int ind = indm; ind<=indM; ++ind) if (positions(ind,0)>=0) { - visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3), - background_color,0.2f); - if ((axis=='x' && positions(ind,2) - positions(ind,0)>=8) || - (axis!='x' && positions(ind,3) - positions(ind,1)>=8)) - visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3), - foreground_color,0.9f,0xAAAAAAAA); - } - const int yt = (int)text_down?visu.height()-13:0; - if (is_clicked) visu.draw_text(0,yt," Images #%u - #%u, Size = %u", - foreground_color,background_color,0.7f,13, - orig + indm,orig + indM,indM - indm + 1); - else visu.draw_text(0,yt," Image #%u (%u,%u,%u,%u)",foreground_color,background_color,0.7f,13, - orig + indice0, - _data[orig+indice0]._width, - _data[orig+indice0]._height, - _data[orig+indice0]._depth, - _data[orig+indice0]._spectrum); - update_display = true; - } else visu.assign(); - } - if (!visu) { visu.assign(visu0,true); update_display = true; } - if (update_display) { visu.display(disp); update_display = false; } - disp.wait(); - - // Manage user events. - const int xm = disp.mouse_x(), ym = disp.mouse_y(); - int indice = -1; - - if (xm>=0) { - indice = (int)indices(axis=='x'?xm:ym); - if (disp.button()&1) { - if (!is_clicked) { is_clicked = true; oindice0 = indice0; indice0 = indice; } - oindice1 = indice1; indice1 = indice; - if (!feature_type) is_selected = true; - } else { - if (!is_clicked) { oindice0 = oindice1 = indice0; indice0 = indice1 = indice; } - else is_selected = true; - } - } else { - if (is_clicked) { - if (!(disp.button()&1)) { is_clicked = is_selected = false; indice0 = indice1 = -1; } - else indice1 = -1; - } else indice0 = indice1 = -1; - } - - if (disp.button()&4) { is_clicked = is_selected = false; indice0 = indice1 = -1; } - if (disp.button()&2 && exit_on_rightbutton) { is_selected = true; indice1 = indice0 = -1; } - if (disp.wheel() && exit_on_wheel) is_selected = true; - - switch (key = disp.key()) { -#if cimg_OS!=2 - case cimg::keyCTRLRIGHT : -#endif - case 0 : case cimg::keyCTRLLEFT : key = 0; break; - case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false), - CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false). - _is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.set_fullscreen(false). - resize(cimg_fitscreen(axis=='x'?sum_width:max_width,axis=='x'?max_height:sum_height,1),false). - _is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true; - disp.set_key(key,false); key = 0; visu0.assign(); - } break; - case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.bmp",snap_number++); - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - if (visu0) { - (+visu0).draw_text(0,0," Saving snapshot... ", - foreground_color,background_color,0.7f,13).display(disp); - visu0.save(filename); - (+visu0).draw_text(0,0," Snapshot '%s' saved. ", - foreground_color,background_color,0.7f,13,filename).display(disp); - } - disp.set_key(key,false).wait(); key = 0; - } break; - case cimg::keyO : - if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { - static unsigned int snap_number = 0; - char filename[32] = { 0 }; - std::FILE *file; - do { -#ifdef cimg_use_zlib - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimgz",snap_number++); -#else - cimg_snprintf(filename,sizeof(filename),cimg_appname "_%.4u.cimg",snap_number++); -#endif - if ((file=std::fopen(filename,"r"))!=0) cimg::fclose(file); - } while (file); - (+visu0).draw_text(0,0," Saving instance... ", - foreground_color,background_color,0.7f,13).display(disp); - save(filename); - (+visu0).draw_text(0,0," Instance '%s' saved. ", - foreground_color,background_color,0.7f,13,filename).display(disp); - disp.set_key(key,false).wait(); key = 0; - } break; - } - if (disp.is_resized()) { disp.resize(false); visu0.assign(); } - if (ym>=0 && ym<13) { if (!text_down) { visu.assign(); text_down = true; }} - else if (ym>=visu.height()-13) { if(text_down) { visu.assign(); text_down = false; }} - } - CImg res(1,2,1,1,-1); - if (is_selected) { - if (feature_type) res.fill(cimg::min(indice0,indice1),cimg::max(indice0,indice1)); - else res.fill(indice0); - } - if (!(disp.button()&2)) disp.set_button(); - disp._normalization = old_normalization; - disp._is_resized = old_is_resized; - disp.set_key(key); - return res; - } - - //! Load a list from a file. - /** - \param filename Filename to read data from. - **/ - CImgList& load(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "load(): Specified filename is (null).", - cimglist_instance); - - if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) { - char filename_local[1024] = { 0 }; - load(cimg::load_network_external(filename,filename_local)); - std::remove(filename_local); - return *this; - } - - const char *const ext = cimg::split_filename(filename); - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - try { -#ifdef cimglist_load_plugin - cimglist_load_plugin(filename); -#endif -#ifdef cimglist_load_plugin1 - cimglist_load_plugin1(filename); -#endif -#ifdef cimglist_load_plugin2 - cimglist_load_plugin2(filename); -#endif -#ifdef cimglist_load_plugin3 - cimglist_load_plugin3(filename); -#endif -#ifdef cimglist_load_plugin4 - cimglist_load_plugin4(filename); -#endif -#ifdef cimglist_load_plugin5 - cimglist_load_plugin5(filename); -#endif -#ifdef cimglist_load_plugin6 - cimglist_load_plugin6(filename); -#endif -#ifdef cimglist_load_plugin7 - cimglist_load_plugin7(filename); -#endif -#ifdef cimglist_load_plugin8 - cimglist_load_plugin8(filename); -#endif - if (!cimg::strcasecmp(ext,"tif") || - !cimg::strcasecmp(ext,"tiff")) load_tiff(filename); - else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename); - else if (!cimg::strcasecmp(ext,"cimg") || - !cimg::strcasecmp(ext,"cimgz") || - !*ext) load_cimg(filename); - else if (!cimg::strcasecmp(ext,"rec") || - !cimg::strcasecmp(ext,"par")) load_parrec(filename); - else if (!cimg::strcasecmp(ext,"avi") || - !cimg::strcasecmp(ext,"mov") || - !cimg::strcasecmp(ext,"asf") || - !cimg::strcasecmp(ext,"divx") || - !cimg::strcasecmp(ext,"flv") || - !cimg::strcasecmp(ext,"mpg") || - !cimg::strcasecmp(ext,"m1v") || - !cimg::strcasecmp(ext,"m2v") || - !cimg::strcasecmp(ext,"m4v") || - !cimg::strcasecmp(ext,"mjp") || - !cimg::strcasecmp(ext,"mp4") || - !cimg::strcasecmp(ext,"mkv") || - !cimg::strcasecmp(ext,"mpe") || - !cimg::strcasecmp(ext,"movie") || - !cimg::strcasecmp(ext,"ogm") || - !cimg::strcasecmp(ext,"ogg") || - !cimg::strcasecmp(ext,"ogv") || - !cimg::strcasecmp(ext,"qt") || - !cimg::strcasecmp(ext,"rm") || - !cimg::strcasecmp(ext,"vob") || - !cimg::strcasecmp(ext,"wmv") || - !cimg::strcasecmp(ext,"xvid") || - !cimg::strcasecmp(ext,"mpeg")) load_ffmpeg(filename); - else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename); - else throw CImgIOException("CImgList<%s>::load()", - pixel_type()); - } catch (CImgIOException&) { - try { - cimg::fclose(cimg::fopen(filename,"rb")); - } catch (CImgIOException&) { - cimg::exception_mode() = omode; - throw CImgIOException(_cimglist_instance - "load(): Failed to open file '%s'.", - cimglist_instance, - filename); - } - assign(1); - try { - _data->load(filename); - } catch (CImgIOException&) { - cimg::exception_mode() = omode; - throw CImgIOException(_cimglist_instance - "load(): Failed to recognize format of file '%s'.", - cimglist_instance, - filename); - } - } - cimg::exception_mode() = omode; - return *this; - } - - //! Load a list from a file \newinstance. - static CImgList get_load(const char *const filename) { - return CImgList().load(filename); - } - - //! Load a list from a .cimg file. - /** - \param filename Filename to read data from. - **/ - CImgList& load_cimg(const char *const filename) { - return _load_cimg(0,filename); - } - - //! Load a list from a .cimg file \newinstance. - static CImgList get_load_cimg(const char *const filename) { - return CImgList().load_cimg(filename); - } - - //! Load a list from a .cimg file. - /** - \param file File to read data from. - **/ - CImgList& load_cimg(std::FILE *const file) { - return _load_cimg(file,0); - } - - //! Load a list from a .cimg file \newinstance. - static CImgList get_load_cimg(std::FILE *const file) { - return CImgList().load_cimg(file); - } - - CImgList& _load_cimg(std::FILE *const file, const char *const filename) { -#ifdef cimg_use_zlib -#define _cimgz_load_cimg_case(Tss) { \ - Bytef *const cbuf = new Bytef[csiz]; \ - cimg::fread(cbuf,csiz,nfile); \ - raw.assign(W,H,D,C); \ - unsigned long destlen = (unsigned long)raw.size()*sizeof(Tss); \ - uncompress((Bytef*)raw._data,&destlen,cbuf,csiz); \ - delete[] cbuf; \ - const Tss *ptrs = raw._data; \ - for (unsigned long off = raw.size(); off; --off) *(ptrd++) = (T)*(ptrs++); \ -} -#else -#define _cimgz_load_cimg_case(Tss) \ - throw CImgIOException(_cimglist_instance \ - "load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.", \ - cimglist_instance, \ - filename?filename:"(FILE*)"); -#endif - -#define _cimg_load_cimg_case(Ts,Tss) \ - if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \ - for (unsigned int l = 0; l=0) tmp[j++] = (char)i; tmp[j] = 0; \ - W = H = D = C = 0; csiz = 0; \ - if ((err = std::sscanf(tmp,"%u %u %u %u #%u",&W,&H,&D,&C,&csiz))<4) \ - throw CImgIOException(_cimglist_instance \ - "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'", \ - cimglist_instance, \ - W,H,D,C,l,filename?filename:("(FILE*)")); \ - if (W*H*D*C>0) { \ - CImg raw; \ - CImg &img = _data[l]; \ - img.assign(W,H,D,C); \ - T *ptrd = img._data; \ - if (err==5) _cimgz_load_cimg_case(Tss) \ - else for (long to_read = (long)img.size(); to_read>0; ) { \ - raw.assign(cimg::min(to_read,cimg_iobuffer)); \ - cimg::fread(raw._data,raw._width,nfile); \ - if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); \ - to_read-=raw._width; \ - const Tss *ptrs = raw._data; \ - for (unsigned long off = (unsigned long)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \ - } \ - } \ - } \ - loaded = true; \ - } - - if (!filename && !file) - throw CImgArgumentException(_cimglist_instance - "load_cimg(): Specified filename is (null).", - cimglist_instance); - - const int cimg_iobuffer = 12*1024*1024; - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - bool loaded = false, endian = cimg::endianness(); - char tmp[256] = { 0 }, str_pixeltype[256] = { 0 }, str_endian[256] = { 0 }; - unsigned int j, err, N = 0, W, H, D, C, csiz; - int i; - do { - j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0; - } while (*tmp=='#' && i!=EOF); - err = std::sscanf(tmp,"%u%*c%255[A-Za-z_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype,str_endian); - if (err<2) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "load_cimg(): CImg header not found in file '%s'.", - cimglist_instance, - filename?filename:"(FILE*)"); - } - if (!cimg::strncasecmp("little",str_endian,6)) endian = false; - else if (!cimg::strncasecmp("big",str_endian,3)) endian = true; - assign(N); - _cimg_load_cimg_case("bool",bool); - _cimg_load_cimg_case("unsigned_char",unsigned char); - _cimg_load_cimg_case("uchar",unsigned char); - _cimg_load_cimg_case("char",char); - _cimg_load_cimg_case("unsigned_short",unsigned short); - _cimg_load_cimg_case("ushort",unsigned short); - _cimg_load_cimg_case("short",short); - _cimg_load_cimg_case("unsigned_int",unsigned int); - _cimg_load_cimg_case("uint",unsigned int); - _cimg_load_cimg_case("int",int); - _cimg_load_cimg_case("unsigned_long",unsigned long); - _cimg_load_cimg_case("ulong",unsigned long); - _cimg_load_cimg_case("long",long); - _cimg_load_cimg_case("float",float); - _cimg_load_cimg_case("double",double); - if (!loaded) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "load_cimg(): Unsupported pixel type '%s' for file '%s'.", - cimglist_instance, - str_pixeltype,filename?filename:"(FILE*)"); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load a sublist list from a (non compressed) .cimg file. - /** - \param filename Filename to read data from. - \param n0 Starting index of images to read (~0U for max). - \param n1 Ending index of images to read (~0U for max). - \param x0 Starting X-coordinates of image regions to read. - \param y0 Starting Y-coordinates of image regions to read. - \param z0 Starting Z-coordinates of image regions to read. - \param c0 Starting C-coordinates of image regions to read. - \param x1 Ending X-coordinates of image regions to read (~0U for max). - \param y1 Ending Y-coordinates of image regions to read (~0U for max). - \param z1 Ending Z-coordinates of image regions to read (~0U for max). - \param c1 Ending C-coordinates of image regions to read (~0U for max). - **/ - CImgList& load_cimg(const char *const filename, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1) { - return _load_cimg(0,filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); - } - - //! Load a sublist list from a (non compressed) .cimg file \newinstance. - static CImgList get_load_cimg(const char *const filename, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1) { - return CImgList().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); - } - - //! Load a sub-image list from a (non compressed) .cimg file \overloading. - CImgList& load_cimg(std::FILE *const file, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1) { - return _load_cimg(file,0,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); - } - - //! Load a sub-image list from a (non compressed) .cimg file \newinstance. - static CImgList get_load_cimg(std::FILE *const file, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1) { - return CImgList().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1); - } - - CImgList& _load_cimg(std::FILE *const file, const char *const filename, - const unsigned int n0, const unsigned int n1, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0, - const unsigned int x1, const unsigned int y1, - const unsigned int z1, const unsigned int c1) { -#define _cimg_load_cimg_case2(Ts,Tss) \ - if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \ - for (unsigned int l = 0; l<=nn1; ++l) { \ - j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0) tmp[j++] = (char)i; tmp[j] = 0; \ - W = H = D = C = 0; \ - if (std::sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=4) \ - throw CImgIOException(_cimglist_instance \ - "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'", \ - cimglist_instance, \ - W,H,D,C,l,filename?filename:"(FILE*)"); \ - if (W*H*D*C>0) { \ - if (l=W || ny0>=H || nz0>=D || nc0>=C) std::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \ - else { \ - const unsigned int \ - _nx1 = nx1==~0U?W-1:nx1, \ - _ny1 = ny1==~0U?H-1:ny1, \ - _nz1 = nz1==~0U?D-1:nz1, \ - _nc1 = nc1==~0U?C-1:nc1; \ - if (_nx1>=W || _ny1>=H || _nz1>=D || _nc1>=C) \ - throw CImgArgumentException(_cimglist_instance \ - "load_cimg(): Invalid specified coordinates " \ - "[%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " \ - "because image [%u] in file '%s' has size (%u,%u,%u,%u).", \ - cimglist_instance, \ - n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,l,filename?filename:"(FILE*)",W,H,D,C); \ - CImg raw(1 + _nx1 - nx0); \ - CImg &img = _data[l - nn0]; \ - img.assign(1 + _nx1 - nx0,1 + _ny1 - ny0,1 + _nz1 - nz0,1 + _nc1 - nc0); \ - T *ptrd = img._data; \ - const unsigned int skipvb = nc0*W*H*D*sizeof(Tss); \ - if (skipvb) std::fseek(nfile,skipvb,SEEK_CUR); \ - for (unsigned int c = 1 + _nc1 - nc0; c; --c) { \ - const unsigned int skipzb = nz0*W*H*sizeof(Tss); \ - if (skipzb) std::fseek(nfile,skipzb,SEEK_CUR); \ - for (unsigned int z = 1 + _nz1 - nz0; z; --z) { \ - const unsigned int skipyb = ny0*W*sizeof(Tss); \ - if (skipyb) std::fseek(nfile,skipyb,SEEK_CUR); \ - for (unsigned int y = 1 + _ny1 - ny0; y; --y) { \ - const unsigned int skipxb = nx0*sizeof(Tss); \ - if (skipxb) std::fseek(nfile,skipxb,SEEK_CUR); \ - cimg::fread(raw._data,raw._width,nfile); \ - if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); \ - const Tss *ptrs = raw._data; \ - for (unsigned int off = raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \ - const unsigned int skipxe = (W-1-_nx1)*sizeof(Tss); \ - if (skipxe) std::fseek(nfile,skipxe,SEEK_CUR); \ - } \ - const unsigned int skipye = (H-1-_ny1)*W*sizeof(Tss); \ - if (skipye) std::fseek(nfile,skipye,SEEK_CUR); \ - } \ - const unsigned int skipze = (D-1-_nz1)*W*H*sizeof(Tss); \ - if (skipze) std::fseek(nfile,skipze,SEEK_CUR); \ - } \ - const unsigned int skipve = (C-1-_nc1)*W*H*D*sizeof(Tss); \ - if (skipve) std::fseek(nfile,skipve,SEEK_CUR); \ - } \ - } \ - } \ - loaded = true; \ - } - - if (!filename && !file) - throw CImgArgumentException(_cimglist_instance - "load_cimg(): Specified filename is (null).", - cimglist_instance); - unsigned int - nn0 = cimg::min(n0,n1), nn1 = cimg::max(n0,n1), - nx0 = cimg::min(x0,x1), nx1 = cimg::max(x0,x1), - ny0 = cimg::min(y0,y1), ny1 = cimg::max(y0,y1), - nz0 = cimg::min(z0,z1), nz1 = cimg::max(z0,z1), - nc0 = cimg::min(c0,c1), nc1 = cimg::max(c0,c1); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - bool loaded = false, endian = cimg::endianness(); - char tmp[256] = { 0 }, str_pixeltype[256] = { 0 }, str_endian[256] = { 0 }; - unsigned int j, err, N, W, H, D, C; - int i; - j = 0; while((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0; - err = std::sscanf(tmp,"%u%*c%255[A-Za-z_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype,str_endian); - if (err<2) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "load_cimg(): CImg header not found in file '%s'.", - cimglist_instance, - filename?filename:"(FILE*)"); - } - if (!cimg::strncasecmp("little",str_endian,6)) endian = false; - else if (!cimg::strncasecmp("big",str_endian,3)) endian = true; - nn1 = n1==~0U?N-1:n1; - if (nn1>=N) - throw CImgArgumentException(_cimglist_instance - "load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " - "because file '%s' contains only %u images.", - cimglist_instance, - n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:"(FILE*)",N); - assign(1+nn1-n0); - _cimg_load_cimg_case2("bool",bool); - _cimg_load_cimg_case2("unsigned_char",unsigned char); - _cimg_load_cimg_case2("uchar",unsigned char); - _cimg_load_cimg_case2("char",char); - _cimg_load_cimg_case2("unsigned_short",unsigned short); - _cimg_load_cimg_case2("ushort",unsigned short); - _cimg_load_cimg_case2("short",short); - _cimg_load_cimg_case2("unsigned_int",unsigned int); - _cimg_load_cimg_case2("uint",unsigned int); - _cimg_load_cimg_case2("int",int); - _cimg_load_cimg_case2("unsigned_long",unsigned long); - _cimg_load_cimg_case2("ulong",unsigned long); - _cimg_load_cimg_case2("long",long); - _cimg_load_cimg_case2("float",float); - _cimg_load_cimg_case2("double",double); - if (!loaded) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "load_cimg(): Unsupported pixel type '%s' for file '%s'.", - cimglist_instance, - str_pixeltype,filename?filename:"(FILE*)"); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load a list from a PAR/REC (Philips) file. - /** - \param filename Filename to read data from. - **/ - CImgList& load_parrec(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "load_parrec(): Specified filename is (null).", - cimglist_instance); - - char body[1024] = { 0 }, filenamepar[1024] = { 0 }, filenamerec[1024] = { 0 }; - const char *const ext = cimg::split_filename(filename,body); - if (!std::strcmp(ext,"par")) { - std::strncpy(filenamepar,filename,sizeof(filenamepar)-1); - cimg_snprintf(filenamerec,sizeof(filenamerec),"%s.rec",body); - } - if (!std::strcmp(ext,"PAR")) { - std::strncpy(filenamepar,filename,sizeof(filenamepar)-1); - cimg_snprintf(filenamerec,sizeof(filenamerec),"%s.REC",body); - } - if (!std::strcmp(ext,"rec")) { - std::strncpy(filenamerec,filename,sizeof(filenamerec)-1); - cimg_snprintf(filenamepar,sizeof(filenamepar),"%s.par",body); - } - if (!std::strcmp(ext,"REC")) { - std::strncpy(filenamerec,filename,sizeof(filenamerec)-1); - cimg_snprintf(filenamepar,sizeof(filenamepar),"%s.PAR",body); - } - std::FILE *file = cimg::fopen(filenamepar,"r"); - - // Parse header file - CImgList st_slices; - CImgList st_global; - int err; - char line[256] = { 0 }; - do { err=std::fscanf(file,"%255[^\n]%*c",line); } while (err!=EOF && (*line=='#' || *line=='.')); - do { - unsigned int sn,size_x,size_y,pixsize; - float rs,ri,ss; - err = std::fscanf(file,"%u%*u%*u%*u%*u%*u%*u%u%*u%u%u%g%g%g%*[^\n]",&sn,&pixsize,&size_x,&size_y,&ri,&rs,&ss); - if (err==7) { - CImg::vector((float)sn,(float)pixsize,(float)size_x,(float)size_y,ri,rs,ss,0).move_to(st_slices); - unsigned int i; for (i = 0; i::vector(size_x,size_y,sn).move_to(st_global); - else { - CImg &vec = st_global[i]; - if (size_x>vec[0]) vec[0] = size_x; - if (size_y>vec[1]) vec[1] = size_y; - vec[2] = sn; - } - st_slices[st_slices._width-1][7] = (float)i; - } - } while (err==7); - - // Read data - std::FILE *file2 = cimg::fopen(filenamerec,"rb"); - cimglist_for(st_global,l) { - const CImg& vec = st_global[l]; - CImg(vec[0],vec[1],vec[2]).move_to(*this); - } - - cimglist_for(st_slices,l) { - const CImg& vec = st_slices[l]; - const unsigned int - sn = (unsigned int)vec[0] - 1, - pixsize = (unsigned int)vec[1], - size_x = (unsigned int)vec[2], - size_y = (unsigned int)vec[3], - imn = (unsigned int)vec[7]; - const float ri = vec[4], rs = vec[5], ss = vec[6]; - switch (pixsize) { - case 8 : { - CImg buf(size_x,size_y); - cimg::fread(buf._data,size_x*size_y,file2); - if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y); - CImg& img = (*this)[imn]; - cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss)); - } break; - case 16 : { - CImg buf(size_x,size_y); - cimg::fread(buf._data,size_x*size_y,file2); - if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y); - CImg& img = (*this)[imn]; - cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss)); - } break; - case 32 : { - CImg buf(size_x,size_y); - cimg::fread(buf._data,size_x*size_y,file2); - if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y); - CImg& img = (*this)[imn]; - cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss)); - } break; - default : - cimg::fclose(file); - cimg::fclose(file2); - throw CImgIOException(_cimglist_instance - "load_parrec(): Unsupported %d-bits pixel type for file '%s'.", - cimglist_instance, - pixsize,filename); - } - } - cimg::fclose(file); - cimg::fclose(file2); - if (!_width) - throw CImgIOException(_cimglist_instance - "load_parrec(): Failed to recognize valid PAR-REC data in file '%s'.", - cimglist_instance, - filename); - return *this; - } - - //! Load a list from a PAR/REC (Philips) file \newinstance. - static CImgList get_load_parrec(const char *const filename) { - return CImgList().load_parrec(filename); - } - - //! Load a list from a YUV image sequence file. - /** - \param filename Filename to read data from. - \param size_x Width of the images. - \param size_y Height of the images. - \param first_frame Index of first image frame to read. - \param last_frame Index of last image frame to read. - \param step_frame Step applied between each frame. - \param yuv2rgb Apply YUV to RGB transformation during reading. - **/ - CImgList& load_yuv(const char *const filename, - const unsigned int size_x, const unsigned int size_y, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true) { - return _load_yuv(0,filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); - } - - //! Load a list from a YUV image sequence file \newinstance. - static CImgList get_load_yuv(const char *const filename, - const unsigned int size_x, const unsigned int size_y=1, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true) { - return CImgList().load_yuv(filename,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); - } - - //! Load a list from an image sequence YUV file \overloading. - CImgList& load_yuv(std::FILE *const file, - const unsigned int size_x, const unsigned int size_y, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true) { - return _load_yuv(file,0,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); - } - - //! Load a list from an image sequence YUV file \newinstance. - static CImgList get_load_yuv(std::FILE *const file, - const unsigned int size_x, const unsigned int size_y=1, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool yuv2rgb=true) { - return CImgList().load_yuv(file,size_x,size_y,first_frame,last_frame,step_frame,yuv2rgb); - } - - CImgList& _load_yuv(std::FILE *const file, const char *const filename, - const unsigned int size_x, const unsigned int size_y, - const unsigned int first_frame, const unsigned int last_frame, - const unsigned int step_frame, const bool yuv2rgb) { - if (!filename && !file) - throw CImgArgumentException(_cimglist_instance - "load_yuv(): Specified filename is (null).", - cimglist_instance); - if (size_x%2 || size_y%2) - throw CImgArgumentException(_cimglist_instance - "load_yuv(): Invalid odd XY dimensions %ux%u in file '%s'.", - cimglist_instance, - size_x,size_y,filename?filename:"(FILE*)"); - if (!size_x || !size_y) - throw CImgArgumentException(_cimglist_instance - "load_yuv(): Invalid sequence size (%u,%u) in file '%s'.", - cimglist_instance, - size_x,size_y,filename?filename:"(FILE*)"); - - const unsigned int - nfirst_frame = first_frame tmp(size_x,size_y,1,3), UV(size_x/2,size_y/2,1,2); - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb"); - bool stop_flag = false; - int err; - if (nfirst_frame) { - err = std::fseek(nfile,nfirst_frame*(size_x*size_y + size_x*size_y/2),SEEK_CUR); - if (err) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "load_yuv(): File '%s' doesn't contain frame number %u.", - cimglist_instance, - filename?filename:"(FILE*)",nfirst_frame); - } - } - unsigned int frame; - for (frame = nfirst_frame; !stop_flag && frame<=nlast_frame; frame+=nstep_frame) { - tmp.fill(0); - // *TRY* to read the luminance part, do not replace by cimg::fread! - err = (int)std::fread((void*)(tmp._data),1,(unsigned long)tmp._width*tmp._height,nfile); - if (err!=(int)(tmp._width*tmp._height)) { - stop_flag = true; - if (err>0) - cimg::warn(_cimglist_instance - "load_yuv(): File '%s' contains incomplete data or given image dimensions " - "(%u,%u) are incorrect.", - cimglist_instance, - filename?filename:"(FILE*)",size_x,size_y); - } else { - UV.fill(0); - // *TRY* to read the luminance part, do not replace by cimg::fread! - err = (int)std::fread((void*)(UV._data),1,(size_t)(UV.size()),nfile); - if (err!=(int)(UV.size())) { - stop_flag = true; - if (err>0) - cimg::warn(_cimglist_instance - "load_yuv(): File '%s' contains incomplete data or given image dimensions (%u,%u) " - "are incorrect.", - cimglist_instance, - filename?filename:"(FILE*)",size_x,size_y); - } else { - cimg_forXY(UV,x,y) { - const int x2 = x*2, y2 = y*2; - tmp(x2,y2,1) = tmp(x2+1,y2,1) = tmp(x2,y2+1,1) = tmp(x2+1,y2+1,1) = UV(x,y,0); - tmp(x2,y2,2) = tmp(x2+1,y2,2) = tmp(x2,y2+1,2) = tmp(x2+1,y2+1,2) = UV(x,y,1); - } - if (yuv2rgb) tmp.YCbCrtoRGB(); - insert(tmp); - if (nstep_frame>1) std::fseek(nfile,(nstep_frame-1)*(size_x*size_y + size_x*size_y/2),SEEK_CUR); - } - } - } - if (stop_flag && nlast_frame!=~0U && frame!=nlast_frame) - cimg::warn(_cimglist_instance - "load_yuv(): Frame %d not reached since only %u frames were found in file '%s'.", - cimglist_instance, - nlast_frame,frame-1,filename?filename:"(FILE*)"); - - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Load an image from a video file, using ffmpeg libraries. - /** - \param filename Filename, as a C-string. - \param first_frame Index of the first frame to read. - \param last_frame Index of the last frame to read. - \param step_frame Step value for frame reading. - \param pixel_format To be documented. - \param resume To be documented. - **/ - // This piece of code has been firstly created by David Starweather (starkdg(at)users(dot)sourceforge(dot)net) - // I modified it afterwards for direct inclusion in the library core. - CImgList& load_ffmpeg(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool pixel_format=true, const bool resume=false) { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "load_ffmpeg(): Specified filename is (null).", - cimglist_instance); - - const unsigned int - nfirst_frame = first_frame1) || (resume && (pixel_format || !pixel_format))) - throw CImgArgumentException(_cimglist_instance - "load_ffmpeg(): Unable to load sub-frames from file '%s' unless libffmpeg " - "is enabled.", - cimglist_instance, - filename); - - return load_ffmpeg_external(filename); -#else - const PixelFormat ffmpeg_pixfmt = pixel_format?PIX_FMT_RGB24:PIX_FMT_GRAY8; - avcodec_register_all(); - av_register_all(); - static AVFormatContext *format_ctx = 0; - static AVCodecContext *codec_ctx = 0; - static AVCodec *codec = 0; - static AVFrame *avframe = avcodec_alloc_frame(), *converted_frame = avcodec_alloc_frame(); - static int vstream = 0; - - if (resume) { - if (!format_ctx || !codec_ctx || !codec || !avframe || !converted_frame) - throw CImgArgumentException(_cimglist_instance - "load_ffmpeg(): Failed to resume loading of file '%s', " - "due to unallocated FFMPEG structures.", - cimglist_instance, - filename); - } else { - // Open video file, find main video stream and codec. - if (format_ctx) avformat_close_input(&format_ctx); - if (avformat_open_input(&format_ctx,filename,0,0)!=0) - throw CImgIOException(_cimglist_instance - "load_ffmpeg(): Failed to open file '%s'.", - cimglist_instance, - filename); - - if (!avframe || !converted_frame || avformat_find_stream_info(format_ctx,NULL)<0) { - avformat_close_input(&format_ctx); format_ctx = 0; - return load_ffmpeg_external(filename); - } -#if cimg_verbosity>=3 - dump_format(format_ctx,0,0,0); -#endif - - // Special command: Return informations on main video stream. - // as a vector 1x4 containing: (nb_frames,width,height,fps). - if (!first_frame && !last_frame && !step_frame) { - for (vstream = 0; vstream<(int)(format_ctx->nb_streams); ++vstream) - if (format_ctx->streams[vstream]->codec->codec_type==AVMEDIA_TYPE_VIDEO) break; - if (vstream==(int)format_ctx->nb_streams) assign(); - else { - CImgList timestamps; - int nb_frames; - AVPacket packet; - // Count frames and store timestamps. - for (nb_frames = 0; av_read_frame(format_ctx,&packet)>=0; av_free_packet(&packet)) - if (packet.stream_index==vstream) { - CImg::vector((double)packet.pts).move_to(timestamps); - ++nb_frames; - } - // Get frame with, height and fps. - const int - framew = format_ctx->streams[vstream]->codec->width, - frameh = format_ctx->streams[vstream]->codec->height; - const float - num = (float)(format_ctx->streams[vstream]->r_frame_rate).num, - den = (float)(format_ctx->streams[vstream]->r_frame_rate).den, - fps = num/den; - // Return infos as a list. - assign(2); - (*this)[0].assign(1,4).fill((T)nb_frames,(T)framew,(T)frameh,(T)fps); - (*this)[1] = (timestamps>'y'); - } - avformat_close_input(&format_ctx); format_ctx = 0; - return *this; - } - - for (vstream = 0; vstream<(int)(format_ctx->nb_streams) && - format_ctx->streams[vstream]->codec->codec_type!=AVMEDIA_TYPE_VIDEO; ) ++vstream; - if (vstream==(int)format_ctx->nb_streams) { - avformat_close_input(&format_ctx); format_ctx = 0; - return load_ffmpeg_external(filename); - } - codec_ctx = format_ctx->streams[vstream]->codec; - codec = avcodec_find_decoder(codec_ctx->codec_id); - if (!codec) { - return load_ffmpeg_external(filename); - } - if (avcodec_open2(codec_ctx,codec,NULL)<0) { // Open codec - return load_ffmpeg_external(filename); - } - } - - // Read video frames - const unsigned int numBytes = avpicture_get_size(ffmpeg_pixfmt,codec_ctx->width,codec_ctx->height); - uint8_t *const buffer = new uint8_t[numBytes]; - avpicture_fill((AVPicture *)converted_frame,buffer,ffmpeg_pixfmt,codec_ctx->width,codec_ctx->height); - const T foo = (T)0; - AVPacket packet; - for (unsigned int frame = 0, next_frame = nfirst_frame; - frame<=nlast_frame && av_read_frame(format_ctx,&packet)>=0; ) { - if (packet.stream_index==(int)vstream) { - int decoded = 0; -#if defined(AV_VERSION_INT) -#if LIBAVCODEC_VERSION_INTwidth,codec_ctx->height,codec_ctx->pix_fmt,codec_ctx->width, - codec_ctx->height,ffmpeg_pixfmt,1,0,0,0); - sws_scale(c,avframe->data,avframe->linesize,0,codec_ctx->height, - converted_frame->data,converted_frame->linesize); - if (ffmpeg_pixfmt==PIX_FMT_RGB24) { - CImg next_image(*converted_frame->data,3,codec_ctx->width,codec_ctx->height,1,true); - next_image._get_permute_axes("yzcx",foo).move_to(*this); - } else { - CImg next_image(*converted_frame->data,1,codec_ctx->width,codec_ctx->height,1,true); - next_image._get_permute_axes("yzcx",foo).move_to(*this); - } - next_frame+=nstep_frame; - } - ++frame; - } - av_free_packet(&packet); - if (next_frame>nlast_frame) break; - } - } - delete[] buffer; - return *this; -#endif - } - - //! Load an image from a video file, using ffmpeg libraries \newinstance. - static CImgList get_load_ffmpeg(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, const bool pixel_format=true) { - return CImgList().load_ffmpeg(filename,first_frame,last_frame,step_frame,pixel_format); - } - - //! Load an image from a video file using the external tool 'ffmpeg'. - /** - \param filename Filename to read data from. - **/ - CImgList& load_ffmpeg_external(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "load_ffmpeg_external(): Specified filename is (null).", - cimglist_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; - std::FILE *file = 0; - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_000001.ppm",filetmp); - if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%%6d.ppm",filetmp); -#if cimg_OS!=2 - cimg_snprintf(command,sizeof(command),"%s -i \"%s\" \"%s\" >/dev/null 2>&1", - cimg::ffmpeg_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp2)._system_strescape().data()); -#else - cimg_snprintf(command,sizeof(command),"\"%s -i \"%s\" \"%s\"\" >NUL 2>&1", - cimg::ffmpeg_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp2)._system_strescape().data()); -#endif - cimg::system(command,0); - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - assign(); - unsigned int i = 1; - for (bool stop_flag = false; !stop_flag; ++i) { - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%.6u.ppm",filetmp,i); - CImg img; - try { img.load_pnm(filetmp2); } - catch (CImgException&) { stop_flag = true; } - if (img) { img.move_to(*this); std::remove(filetmp2); } - } - cimg::exception_mode() = omode; - if (is_empty()) - throw CImgIOException(_cimglist_instance - "load_ffmpeg_external(): Failed to open file '%s' with external command 'ffmpeg'.", - cimglist_instance, - filename); - return *this; - } - - //! Load an image from a video file using the external tool 'ffmpeg' \newinstance. - static CImgList get_load_ffmpeg_external(const char *const filename) { - return CImgList().load_ffmpeg_external(filename); - } - - //! Load gif file, using ImageMagick or GraphicsMagick's external tools. - /** - \param filename Filename to read data from. - \param use_graphicsmagick Tells if GraphicsMagick's tool 'gm' is used instead of ImageMagick's tool 'convert'. - **/ - CImgList& load_gif_external(const char *const filename) { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "load_gif_external(): Specified filename is (null).", - cimglist_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - if (!_load_gif_external(filename,false)) - if (!_load_gif_external(filename,true)) - try { assign(CImg().load_other(filename)); } catch (CImgException&) { assign(); } - if (is_empty()) - throw CImgIOException(_cimglist_instance - "load_gif_external(): Failed to open file '%s'.", - cimglist_instance,filename); - return *this; - } - - CImgList& _load_gif_external(const char *const filename, const bool use_graphicsmagick=false) { - char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; - std::FILE *file = 0; - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - if (use_graphicsmagick) cimg_snprintf(filetmp2,sizeof(filetmp2),"%s.png.0",filetmp); - else cimg_snprintf(filetmp2,sizeof(filetmp2),"%s-0.png",filetmp); - if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); - } while (file); -#if cimg_OS!=2 - if (use_graphicsmagick) cimg_snprintf(command,sizeof(command),"%s convert \"%s\" \"%s.png\" >/dev/null 2>&1", - cimg::graphicsmagick_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); - else cimg_snprintf(command,sizeof(command),"%s \"%s\" \"%s.png\" >/dev/null 2>&1", - cimg::imagemagick_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); -#else - if (use_graphicsmagick) cimg_snprintf(command,sizeof(command),"\"%s convert \"%s\" \"%s.png\"\" >NUL 2>&1", - cimg::graphicsmagick_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); - else cimg_snprintf(command,sizeof(command),"\"%s \"%s\" \"%s.png\"\" >NUL 2>&1", - cimg::imagemagick_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); -#endif - cimg::system(command,0); - const unsigned int omode = cimg::exception_mode(); - cimg::exception_mode() = 0; - assign(); - - // Try to read a single frame gif. - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s.png",filetmp); - CImg img; - try { img.load_png(filetmp2); } - catch (CImgException&) { } - if (img) { img.move_to(*this); std::remove(filetmp2); } - else { // Try to read animated gif. - unsigned int i = 0; - for (bool stop_flag = false; !stop_flag; ++i) { - if (use_graphicsmagick) cimg_snprintf(filetmp2,sizeof(filetmp2),"%s.png.%u",filetmp,i); - else cimg_snprintf(filetmp2,sizeof(filetmp2),"%s-%u.png",filetmp,i); - CImg img; - try { img.load_png(filetmp2); } - catch (CImgException&) { stop_flag = true; } - if (img) { img.move_to(*this); std::remove(filetmp2); } - } - } - cimg::exception_mode() = omode; - return *this; - } - - //! Load gif file, using ImageMagick or GraphicsMagick's external tools \newinstance. - static CImgList get_load_gif_external(const char *const filename) { - return CImgList().load_gif_external(filename); - } - - //! Load a gzipped list, using external tool 'gunzip'. - /** - \param filename Filename to read data from. - **/ - CImgList& load_gzip_external(const char *const filename) { - if (!filename) - throw CImgIOException(_cimglist_instance - "load_gzip_external(): Specified filename is (null).", - cimglist_instance); - std::fclose(cimg::fopen(filename,"rb")); // Check if file exists. - char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; - const char - *ext = cimg::split_filename(filename,body), - *ext2 = cimg::split_filename(body,0); - std::FILE *file = 0; - do { - if (!cimg::strcasecmp(ext,"gz")) { - if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } else { - if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", - cimg::gunzip_path(), - CImg::string(filename)._system_strescape().data(), - CImg::string(filetmp)._system_strescape().data()); - cimg::system(command); - if (!(file = std::fopen(filetmp,"rb"))) { - cimg::fclose(cimg::fopen(filename,"r")); - throw CImgIOException(_cimglist_instance - "load_gzip_external(): Failed to open file '%s'.", - cimglist_instance, - filename); - - } else cimg::fclose(file); - load(filetmp); - std::remove(filetmp); - return *this; - } - - //! Load a gzipped list, using external tool 'gunzip' \newinstance. - static CImgList get_load_gzip_external(const char *const filename) { - return CImgList().load_gzip_external(filename); - } - - //! Load a 3d object from a .OFF file. - /** - \param filename Filename to read data from. - \param[out] primitives At return, contains the list of 3d object primitives. - \param[out] colors At return, contains the list of 3d object colors. - \return List of 3d object vertices. - **/ - template - CImgList& load_off(const char *const filename, - CImgList& primitives, CImgList& colors) { - return get_load_off(filename,primitives,colors).move_to(*this); - } - - //! Load a 3d object from a .OFF file \newinstance. - template - static CImgList get_load_off(const char *const filename, - CImgList& primitives, CImgList& colors) { - return CImg().load_off(filename,primitives,colors)<'x'; - } - - //! Load images from a TIFF file. - /** - \param filename Filename to read data from. - \param first_frame Index of first image frame to read. - \param last_frame Index of last image frame to read. - \param step_frame Step applied between each frame. - **/ - CImgList& load_tiff(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, - float *const voxel_size=0) { - const unsigned int - nfirst_frame = first_frame::get_load_tiff(filename)); -#else - TIFF *tif = TIFFOpen(filename,"r"); - if (tif) { - unsigned int nb_images = 0; - do ++nb_images; while (TIFFReadDirectory(tif)); - if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images)) - cimg::warn(_cimglist_instance - "load_tiff(): Invalid specified frame range is [%u,%u] (step %u) since " - "file '%s' contains %u image(s).", - cimglist_instance, - nfirst_frame,nlast_frame,nstep_frame,filename,nb_images); - - if (nfirst_frame>=nb_images) return assign(); - if (nlast_frame>=nb_images) nlast_frame = nb_images-1; - assign(1+(nlast_frame-nfirst_frame)/nstep_frame); - TIFFSetDirectory(tif,0); -#if cimg_verbosity>=3 - TIFFSetWarningHandler(0); - TIFFSetErrorHandler(0); -#endif - cimglist_for(*this,l) _data[l]._load_tiff(tif,nfirst_frame + l*nstep_frame,voxel_size); - TIFFClose(tif); - } else throw CImgIOException(_cimglist_instance - "load_tiff(): Failed to open file '%s'.", - cimglist_instance, - filename); - return *this; -#endif - } - - //! Load a multi-page TIFF file \newinstance. - static CImgList get_load_tiff(const char *const filename, - const unsigned int first_frame=0, const unsigned int last_frame=~0U, - const unsigned int step_frame=1, - float *const voxel_size=0) { - return CImgList().load_tiff(filename,first_frame,last_frame,step_frame,voxel_size); - } - - //@} - //---------------------------------- - // - //! \name Data Output - //@{ - //---------------------------------- - - //! Print informations about the list on the standard output. - /** - \param title Label set to the informations displayed. - \param display_stats Tells if image statistics must be computed and displayed. - **/ - const CImgList& print(const char *const title=0, const bool display_stats=true) const { - unsigned int msiz = 0; - cimglist_for(*this,l) msiz+=_data[l].size(); - msiz*=sizeof(T); - const unsigned int mdisp = msiz<8*1024?0:(msiz<8*1024*1024?1:2); - char _title[64] = { 0 }; - if (!title) cimg_snprintf(_title,sizeof(_title),"CImgList<%s>",pixel_type()); - std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = %u/%u [%u %s], %sdata%s = (CImg<%s>*)%p", - cimg::t_magenta,cimg::t_bold,title?title:_title,cimg::t_normal, - cimg::t_bold,cimg::t_normal,(void*)this, - cimg::t_bold,cimg::t_normal,_width,_allocated_width, - mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20)), - mdisp==0?"b":(mdisp==1?"Kio":"Mio"), - cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin()); - if (_data) std::fprintf(cimg::output(),"..%p.\n",(void*)((char*)end()-1)); - else std::fprintf(cimg::output(),".\n"); - - char tmp[16] = { 0 }; - cimglist_for(*this,ll) { - cimg_snprintf(tmp,sizeof(tmp),"[%d]",ll); - std::fprintf(cimg::output()," "); - _data[ll].print(tmp,display_stats); - if (ll==3 && _width>8) { ll = _width-5; std::fprintf(cimg::output()," ...\n"); } - } - std::fflush(cimg::output()); - return *this; - } - - //! Display the current CImgList instance in an existing CImgDisplay window (by reference). - /** - \param disp Reference to an existing CImgDisplay instance, where the current image list will be displayed. - \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignmenet. - \note This function displays the list images of the current CImgList instance into an existing - CImgDisplay window. - Images of the list are appended in a single temporarly image for visualization purposes. - The function returns immediately. - **/ - const CImgList& display(CImgDisplay &disp, const char axis='x', const float align=0) const { - disp.display(*this,axis,align); - return *this; - } - - //! Display the current CImgList instance in a new display window. - /** - \param disp Display window. - \param display_info Tells if image informations are displayed on the standard output. - \param axis Alignment axis for images viewing. - \param align Apending alignment. - \note This function opens a new window with a specific title and displays the list images of the - current CImgList instance into it. - Images of the list are appended in a single temporarly image for visualization purposes. - The function returns when a key is pressed or the display window is closed by the user. - **/ - const CImgList& display(CImgDisplay &disp, const bool display_info, - const char axis='x', const float align=0, - unsigned int *const XYZ=0) const { - bool is_exit = false; - return _display(disp,0,display_info,axis,align,XYZ,0,true,is_exit); - } - - //! Display the current CImgList instance in a new display window. - /** - \param title Title of the opening display window. - \param display_info Tells if list informations must be written on standard output. - \param axis Appending axis. Can be { 'x' | 'y' | 'z' | 'c' }. - \param align Appending alignment. - **/ - const CImgList& display(const char *const title=0, const bool display_info=true, - const char axis='x', const float align=0, - unsigned int *const XYZ=0) const { - CImgDisplay disp; - bool is_exit = false; - return _display(disp,title,display_info,axis,align,XYZ,0,true,is_exit); - } - - const CImgList& _display(CImgDisplay &disp, const char *const title, const bool display_info, - const char axis, const float align, unsigned int *const XYZ, - const unsigned int orig, const bool is_first_call, bool &is_exit) const { - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "display(): Empty instance.", - cimglist_instance); - if (!disp) { - if (axis=='x') { - unsigned int sum_width = 0, max_height = 0; - cimglist_for(*this,l) { - const CImg &img = _data[l]; - const unsigned int - w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false), - h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true); - sum_width+=w; - if (h>max_height) max_height = h; - } - disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:0,1); - } else { - unsigned int max_width = 0, sum_height = 0; - cimglist_for(*this,l) { - const CImg &img = _data[l]; - const unsigned int - w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false), - h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true); - if (w>max_width) max_width = w; - sum_height+=h; - } - disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:0,1); - } - if (!title) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width); - } else if (title) disp.set_title("%s",title); - const CImg dtitle = CImg::string(disp.title()); - if (display_info) print(disp.title()); - disp.show().flush(); - - if (_width==1) { - const unsigned int dw = disp._width, dh = disp._height; - if (!is_first_call) - disp.resize(cimg_fitscreen(_data[0]._width,_data[0]._height,_data[0]._depth),false). - set_title("%s (%ux%ux%ux%u)", - dtitle.data(),_data[0]._width,_data[0]._height,_data[0]._depth,_data[0]._spectrum); - _data[0]._display(disp,0,false,XYZ,!is_first_call); - if (disp.key()) is_exit = true; - disp.resize(cimg_fitscreen(dw,dh,1),false).set_title("%s",dtitle.data()); - } else { - bool disp_resize = !is_first_call; - while (!disp.is_closed() && !is_exit) { - const CImg s = _get_select(disp,0,true,axis,align,orig,disp_resize,!is_first_call,true); - disp_resize = true; - if (s[0]<0) { // No selections done. - if (disp.button()&2) { disp.flush(); break; } - is_exit = true; - } else if (disp.wheel()) { // Zoom in/out. - const int wheel = disp.wheel(); - disp.set_wheel(); - if (!is_first_call && wheel<0) break; - if (wheel>0 && _width>=4) { - const unsigned int - delta = cimg::max(1U,(unsigned int)cimg::round(0.3*_width)), - ind0 = (unsigned int)cimg::max(0,s[0] - (int)delta), - ind1 = (unsigned int)cimg::min(width() - 1,s[0] + (int)delta); - if ((ind0!=0 || ind1!=_width-1) && ind1 - ind0>=3) - get_shared_images(ind0,ind1)._display(disp,0,false,axis,align,XYZ,orig + ind0,false,is_exit); - } - } else if (s[0]!=0 || s[1]!=width()-1) - get_shared_images(s[0],s[1])._display(disp,0,false,axis,align,XYZ,orig+s[0],false,is_exit); - } - } - return *this; - } - - //! Save list into a file. - /** - \param filename Filename to write data to. - \param number When positive, represents an index added to the filename. Otherwise, no number is added. - \param digits Number of digits used for adding the number to the filename. - **/ - const CImgList& save(const char *const filename, const int number=-1, const unsigned int digits=6) const { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "save(): Specified filename is (null).", - cimglist_instance); - // Do not test for empty instances, since .cimg format is able to manage empty instances. - const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.'); - const char *const ext = cimg::split_filename(filename); - char nfilename[1024] = { 0 }; - const char *const fn = is_stdout?filename:number>=0?cimg::number_filename(filename,number,digits,nfilename): - filename; - -#ifdef cimglist_save_plugin - cimglist_save_plugin(fn); -#endif -#ifdef cimglist_save_plugin1 - cimglist_save_plugin1(fn); -#endif -#ifdef cimglist_save_plugin2 - cimglist_save_plugin2(fn); -#endif -#ifdef cimglist_save_plugin3 - cimglist_save_plugin3(fn); -#endif -#ifdef cimglist_save_plugin4 - cimglist_save_plugin4(fn); -#endif -#ifdef cimglist_save_plugin5 - cimglist_save_plugin5(fn); -#endif -#ifdef cimglist_save_plugin6 - cimglist_save_plugin6(fn); -#endif -#ifdef cimglist_save_plugin7 - cimglist_save_plugin7(fn); -#endif -#ifdef cimglist_save_plugin8 - cimglist_save_plugin8(fn); -#endif - if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true); - else if (!cimg::strcasecmp(ext,"cimg") || !*ext) return save_cimg(fn,false); - else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,true); - else if (!cimg::strcasecmp(ext,"avi") || - !cimg::strcasecmp(ext,"mov") || - !cimg::strcasecmp(ext,"asf") || - !cimg::strcasecmp(ext,"divx") || - !cimg::strcasecmp(ext,"flv") || - !cimg::strcasecmp(ext,"mpg") || - !cimg::strcasecmp(ext,"m1v") || - !cimg::strcasecmp(ext,"m2v") || - !cimg::strcasecmp(ext,"m4v") || - !cimg::strcasecmp(ext,"mjp") || - !cimg::strcasecmp(ext,"mp4") || - !cimg::strcasecmp(ext,"mkv") || - !cimg::strcasecmp(ext,"mpe") || - !cimg::strcasecmp(ext,"movie") || - !cimg::strcasecmp(ext,"ogm") || - !cimg::strcasecmp(ext,"ogg") || - !cimg::strcasecmp(ext,"ogv") || - !cimg::strcasecmp(ext,"qt") || - !cimg::strcasecmp(ext,"rm") || - !cimg::strcasecmp(ext,"vob") || - !cimg::strcasecmp(ext,"wmv") || - !cimg::strcasecmp(ext,"xvid") || - !cimg::strcasecmp(ext,"mpeg")) return save_ffmpeg(fn); -#ifdef cimg_use_tiff - else if (!cimg::strcasecmp(ext,"tif") || - !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn); -#endif - else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn); - else { - if (_width==1) _data[0].save(fn,-1); - else cimglist_for(*this,l) { _data[l].save(fn,is_stdout?-1:l); if (is_stdout) std::fputc(EOF,stdout); } - } - return *this; - } - - //! Tell if an image list can be saved as one single file. - /** - \param filename Filename, as a C-string. - \return \c true if the file format supports multiple images, \c false otherwise. - **/ - static bool is_saveable(const char *const filename) { - const char *const ext = cimg::split_filename(filename); - if (!cimg::strcasecmp(ext,"cimgz") || -#ifdef cimg_use_tiff - !cimg::strcasecmp(ext,"tif") || - !cimg::strcasecmp(ext,"tiff") || -#endif - !cimg::strcasecmp(ext,"yuv") || - !cimg::strcasecmp(ext,"avi") || - !cimg::strcasecmp(ext,"mov") || - !cimg::strcasecmp(ext,"asf") || - !cimg::strcasecmp(ext,"divx") || - !cimg::strcasecmp(ext,"flv") || - !cimg::strcasecmp(ext,"mpg") || - !cimg::strcasecmp(ext,"m1v") || - !cimg::strcasecmp(ext,"m2v") || - !cimg::strcasecmp(ext,"m4v") || - !cimg::strcasecmp(ext,"mjp") || - !cimg::strcasecmp(ext,"mp4") || - !cimg::strcasecmp(ext,"mkv") || - !cimg::strcasecmp(ext,"mpe") || - !cimg::strcasecmp(ext,"movie") || - !cimg::strcasecmp(ext,"ogm") || - !cimg::strcasecmp(ext,"ogg") || - !cimg::strcasecmp(ext,"ogv") || - !cimg::strcasecmp(ext,"qt") || - !cimg::strcasecmp(ext,"rm") || - !cimg::strcasecmp(ext,"vob") || - !cimg::strcasecmp(ext,"wmv") || - !cimg::strcasecmp(ext,"xvid") || - !cimg::strcasecmp(ext,"mpeg")) return true; - return false; - } - - //! Save image sequence as a GIF animated file. - /** - \param filename Filename to write data to. - \param fps Number of desired frames per second. - \param nb_loops Number of loops (\c 0 for infinite looping). - **/ - const CImgList& save_gif_external(const char *const filename, const unsigned int fps=25, - const unsigned int nb_loops=0) { - char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; - CImgList filenames; - std::FILE *file = 0; - -#ifdef cimg_use_png -#define _cimg_save_gif_ext "png" -#else -#define _cimg_save_gif_ext "ppm" -#endif - - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_000001." _cimg_save_gif_ext,filetmp); - if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); - } while (file); - cimglist_for(*this,l) { - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%.6u." _cimg_save_gif_ext,filetmp,l+1); - CImg::string(filetmp2).move_to(filenames); - if (_data[l]._depth>1 || _data[l]._spectrum!=3) _data[l].get_resize(-100,-100,1,3).save(filetmp2); - else _data[l].save(filetmp2); - } - -#if cimg_OS!=2 - cimg_snprintf(command,sizeof(command),"%s -delay 1x%u -loop %u", - cimg::imagemagick_path(),fps,nb_loops); - CImg::string(command).move_to(filenames,0); - cimg_snprintf(command,sizeof(command),"\"%s\" >/dev/null 2>&1", - CImg::string(filename)._system_strescape().data()); - CImg::string(command).move_to(filenames); -#else - cimg_snprintf(command,sizeof(command),"\"%s -delay 1x%u -loop %u", - cimg::imagemagick_path(),fps,nb_loops); - CImg::string(command).move_to(filenames,0); - cimg_snprintf(command,sizeof(command),"\"%s\"\" >NUL 2>&1", - CImg::string(filename)._system_strescape().data()); - CImg::string(command).move_to(filenames); -#endif - CImg _command = filenames>'x'; - cimg_for(_command,p,char) if (!*p) *p = ' '; - _command.back() = 0; - - cimg::system(_command); - file = std::fopen(filename,"rb"); - if (!file) - throw CImgIOException(_cimglist_instance - "save_gif_external(): Failed to save file '%s' with external command 'convert'.", - cimglist_instance, - filename); - else cimg::fclose(file); - cimglist_for_in(*this,1,filenames._width-1,l) std::remove(filenames[l]); - return *this; - } - - //! Save image sequence, using FFMPEG library. - /** - \param filename Filename to write data to. - \param fps Desired framerate (in frames per seconds) if chosen format supports it. - \param bitrate Desired bitrate (in bits per seconds) if chosen format supports it. - **/ - // This piece of code has been originally written by David. G. Starkweather. - const CImgList& save_ffmpeg(const char *const filename, const unsigned int fps=25, - const unsigned int bitrate=2048) const { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "save_ffmpeg(): Specified filename is (null).", - cimglist_instance); - if (!fps) - throw CImgArgumentException(_cimglist_instance - "save_ffmpeg(): Invalid specified framerate 0, for file '%s'.", - cimglist_instance, - filename); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - cimglist_for(*this,l) if (!_data[l].is_sameXYZ(_data[0])) - throw CImgInstanceException(_cimglist_instance - "save_ffmpeg(): Invalid instance dimensions, for file '%s'.", - cimglist_instance, - filename); - -#ifndef cimg_use_ffmpeg - return save_ffmpeg_external(filename,0,fps,bitrate); -#else - avcodec_register_all(); - av_register_all(); - const int - frame_dimx = _data[0].width(), - frame_dimy = _data[0].height(), - frame_dimv = _data[0].spectrum(); - if (frame_dimv!=1 && frame_dimv!=3) - throw CImgInstanceException(_cimglist_instance - "save_ffmpeg(): Image[0] (%u,%u,%u,%u,%p) has not 1 or 3 channels, for file '%s'.", - cimglist_instance, - _data[0]._width,_data[0]._height,_data[0]._depth,_data[0]._spectrum,_data,filename); - - PixelFormat dest_pxl_fmt = PIX_FMT_YUV420P; - PixelFormat src_pxl_fmt = (frame_dimv==3)?PIX_FMT_RGB24:PIX_FMT_GRAY8; - - int sws_flags = SWS_FAST_BILINEAR; // Interpolation method (keeping same size images for now). - AVOutputFormat *fmt = 0; -#if defined(AV_VERSION_INT) -#if LIBAVFORMAT_VERSION_INToformat = fmt; - std::sprintf(oc->filename,"%s",filename); - - // Add video stream. - AVStream *video_str = 0; - if (fmt->video_codec!=CODEC_ID_NONE) { - video_str = avformat_new_stream(oc,NULL); - if (!video_str) { // Failed to allocate stream. - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to allocate FFMPEG structure for video stream, for file '%s'.", - cimglist_instance, - filename); - } - } else { // No codec identified. - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to identify proper codec, for file '%s'.", - cimglist_instance, - filename); - } - - AVCodecContext *c = video_str->codec; - c->codec_id = fmt->video_codec; - c->codec_type = AVMEDIA_TYPE_VIDEO; - c->bit_rate = 1024*bitrate; - c->width = frame_dimx; - c->height = frame_dimy; - c->time_base.num = 1; - c->time_base.den = fps; - c->gop_size = 12; - c->pix_fmt = dest_pxl_fmt; - if (c->codec_id==CODEC_ID_MPEG2VIDEO) c->max_b_frames = 2; - if (c->codec_id==CODEC_ID_MPEG1VIDEO) c->mb_decision = 2; - - // Open codecs and alloc buffers. - codec = avcodec_find_encoder(c->codec_id); - if (!codec) { // Failed to find codec. - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): No valid codec found for file '%s'.", - cimglist_instance, - filename); - } - if (avcodec_open2(c,codec,NULL)<0) // Failed to open codec. - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to open codec for file '%s'.", - cimglist_instance, - filename); - - tmp_pict = avcodec_alloc_frame(); - if (!tmp_pict) { // Failed to allocate memory for tmp_pict frame. - avcodec_close(video_str->codec); - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to allocate memory for file '%s'.", - cimglist_instance, - filename); - } - tmp_pict->linesize[0] = (src_pxl_fmt==PIX_FMT_RGB24)?3*frame_dimx:frame_dimx; - // tmp_pict->type = FF_BUFFER_TYPE_USER; - int tmp_size = avpicture_get_size(src_pxl_fmt,frame_dimx,frame_dimy); - uint8_t *tmp_buffer = (uint8_t*)av_malloc(tmp_size); - if (!tmp_buffer) { // Failed to allocate memory for tmp buffer. - av_free(tmp_pict); - avcodec_close(video_str->codec); - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to allocate memory for file '%s'.", - cimglist_instance, - filename); - } - - // Associate buffer with tmp_pict. - avpicture_fill((AVPicture*)tmp_pict,tmp_buffer,src_pxl_fmt,frame_dimx,frame_dimy); - picture = avcodec_alloc_frame(); - if (!picture) { // Failed to allocate picture frame. - av_free(tmp_pict->data[0]); - av_free(tmp_pict); - avcodec_close(video_str->codec); - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to allocate memory for file '%s'.", - cimglist_instance, - filename); - } - - int size = avpicture_get_size(c->pix_fmt,frame_dimx,frame_dimy); - uint8_t *buffer = (uint8_t*)av_malloc(size); - if (!buffer) { // Failed to allocate picture frame buffer. - av_free(picture); - av_free(tmp_pict->data[0]); - av_free(tmp_pict); - avcodec_close(video_str->codec); - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to allocate memory for file '%s'.", - cimglist_instance, - filename); - } - - // Associate the buffer with picture. - avpicture_fill((AVPicture*)picture,buffer,c->pix_fmt,frame_dimx,frame_dimy); - - // Open file. - if (!(fmt->flags&AVFMT_NOFILE)) { - if (avio_open(&oc->pb,filename,AVIO_FLAG_WRITE)<0) - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to open file '%s'.", - cimglist_instance, - filename); - } - - if (avformat_write_header(oc,NULL)<0) - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to write header in file '%s'.", - cimglist_instance, - filename); - - SwsContext *img_convert_context = 0; - img_convert_context = sws_getContext(frame_dimx,frame_dimy,src_pxl_fmt, - c->width,c->height,c->pix_fmt,sws_flags,0,0,0); - if (!img_convert_context) { // Failed to get swscale context. - // if (!(fmt->flags & AVFMT_NOFILE)) url_fclose(&oc->pb); - av_free(picture->data); - av_free(picture); - av_free(tmp_pict->data[0]); - av_free(tmp_pict); - avcodec_close(video_str->codec); - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to get conversion context for file '%s'.", - cimglist_instance, - filename); - } - int ret = 0, out_size; - uint8_t *video_outbuf = 0; - int video_outbuf_size = 1000000; - video_outbuf = (uint8_t*)av_malloc(video_outbuf_size); - if (!video_outbuf) { - // if (!(fmt->flags & AVFMT_NOFILE)) url_fclose(&oc->pb); - av_free(picture->data); - av_free(picture); - av_free(tmp_pict->data[0]); - av_free(tmp_pict); - avcodec_close(video_str->codec); - av_free(oc); - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to allocate memory, for file '%s'.", - cimglist_instance, - filename); - } - - // Loop through each desired image in list. - cimglist_for(*this,i) { - CImg currentIm = _data[i], red, green, blue, gray; - if (src_pxl_fmt==PIX_FMT_RGB24) { - red = currentIm.get_shared_channel(0); - green = currentIm.get_shared_channel(1); - blue = currentIm.get_shared_channel(2); - cimg_forXY(currentIm,X,Y) { // Assign pizel values to data buffer in interlaced RGBRGB ... format. - tmp_pict->data[0][Y*tmp_pict->linesize[0] + 3*X] = red(X,Y); - tmp_pict->data[0][Y*tmp_pict->linesize[0] + 3*X + 1] = green(X,Y); - tmp_pict->data[0][Y*tmp_pict->linesize[0] + 3*X + 2] = blue(X,Y); - } - } else { - gray = currentIm.get_shared_channel(0); - cimg_forXY(currentIm,X,Y) tmp_pict->data[0][Y*tmp_pict->linesize[0] + X] = gray(X,Y); - } - if (!video_str) break; - if (sws_scale(img_convert_context,tmp_pict->data,tmp_pict->linesize,0, - c->height,picture->data,picture->linesize)<0) break; - - AVPacket pkt; - int got_packet; - av_init_packet(&pkt); - out_size = avcodec_encode_video2(c,&pkt,picture,&got_packet); - if (got_packet) { - pkt.pts = av_rescale_q(c->coded_frame->pts,c->time_base,video_str->time_base); - if (c->coded_frame->key_frame) pkt.flags|=AV_PKT_FLAG_KEY; - pkt.stream_index = video_str->index; - pkt.data = video_outbuf; - pkt.size = out_size; - ret = av_write_frame(oc,&pkt); - } else if (out_size<0) break; - if (ret) break; // Error occured in writing frame. - } - - // Close codec. - if (video_str) { - avcodec_close(video_str->codec); - av_free(picture->data[0]); - av_free(picture); - av_free(tmp_pict->data[0]); - av_free(tmp_pict); - } - if (av_write_trailer(oc)<0) - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): Failed to write trailer for file '%s'.", - cimglist_instance, - filename); - - av_freep(&oc->streams[0]->codec); - av_freep(&oc->streams[0]); - if (!(fmt->flags&AVFMT_NOFILE)) { - /*if (url_fclose(oc->pb)<0) - throw CImgIOException(_cimglist_instance - "save_ffmpeg(): File '%s', failed to close file.", - cimglist_instance, - filename); - */ - } - av_free(oc); - av_free(video_outbuf); - return *this; -#endif - } - - const CImgList& _save_yuv(std::FILE *const file, const char *const filename, const bool is_rgb) const { - if (!file && !filename) - throw CImgArgumentException(_cimglist_instance - "save_yuv(): Specified filename is (null).", - cimglist_instance); - if (is_empty()) { cimg::fempty(file,filename); return *this; } - if ((*this)[0].width()%2 || (*this)[0].height()%2) - throw CImgInstanceException(_cimglist_instance - "save_yuv(): Invalid odd instance dimensions (%u,%u) for file '%s'.", - cimglist_instance, - (*this)[0].width(),(*this)[0].height(), - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - cimglist_for(*this,l) { - CImg YCbCr((*this)[l]); - if (is_rgb) YCbCr.RGBtoYCbCr(); - cimg::fwrite(YCbCr._data,(unsigned long)YCbCr._width*YCbCr._height,nfile); - cimg::fwrite(YCbCr.get_resize(YCbCr._width/2, YCbCr._height/2,1,3,3).data(0,0,0,1), - (unsigned long)YCbCr._width*YCbCr._height/2,nfile); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save list as a YUV image sequence file. - /** - \param filename Filename to write data to. - \param is_rgb Tells if the RGB to YUV conversion must be done for saving. - **/ - const CImgList& save_yuv(const char *const filename=0, const bool is_rgb=true) const { - return _save_yuv(0,filename,is_rgb); - } - - //! Save image sequence into a YUV file. - /** - \param file File to write data to. - \param is_rgb Tells if the RGB to YUV conversion must be done for saving. - **/ - const CImgList& save_yuv(std::FILE *const file, const bool is_rgb=true) const { - return _save_yuv(file,0,is_rgb); - } - - const CImgList& _save_cimg(std::FILE *const file, const char *const filename, const bool is_compressed) const { - if (!file && !filename) - throw CImgArgumentException(_cimglist_instance - "save_cimg(): Specified filename is (null).", - cimglist_instance); -#ifndef cimg_use_zlib - if (is_compressed) - cimg::warn(_cimglist_instance - "save_cimg(): Unable to save compressed data in file '%s' unless zlib is enabled, " - "saving them uncompressed.", - cimglist_instance, - filename?filename:"(FILE*)"); -#endif - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little"; - if (std::strstr(ptype,"unsigned")==ptype) std::fprintf(nfile,"%u unsigned_%s %s_endian\n",_width,ptype+9,etype); - else std::fprintf(nfile,"%u %s %s_endian\n",_width,ptype,etype); - cimglist_for(*this,l) { - const CImg& img = _data[l]; - std::fprintf(nfile,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum); - if (img._data) { - CImg tmp; - if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); } - const CImg& ref = cimg::endianness()?tmp:img; - bool failed_to_compress = true; - if (is_compressed) { -#ifdef cimg_use_zlib - const unsigned long siz = sizeof(T)*ref.size(); - unsigned long csiz = siz + siz/100 + 16; - Bytef *const cbuf = new Bytef[csiz]; - if (compress(cbuf,&csiz,(Bytef*)ref._data,siz)) - cimg::warn(_cimglist_instance - "save_cimg(): Failed to save compressed data for file '%s', saving them uncompressed.", - cimglist_instance, - filename?filename:"(FILE*)"); - else { - std::fprintf(nfile," #%lu\n",csiz); - cimg::fwrite(cbuf,csiz,nfile); - delete[] cbuf; - failed_to_compress = false; - } -#endif - } - if (failed_to_compress) { // Write in a non-compressed way. - std::fputc('\n',nfile); - cimg::fwrite(ref._data,ref.size(),nfile); - } - } else std::fputc('\n',nfile); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Save list into a .cimg file. - /** - \param filename Filename to write data to. - \param is_compressed Tells if data compression must be enabled. - **/ - const CImgList& save_cimg(const char *const filename, const bool is_compressed=false) const { - return _save_cimg(0,filename,is_compressed); - } - - //! Save list into a .cimg file. - /** - \param file File to write data to. - \param is_compressed Tells if data compression must be enabled. - **/ - const CImgList& save_cimg(std::FILE *file, const bool is_compressed=false) const { - return _save_cimg(file,0,is_compressed); - } - - const CImgList& _save_cimg(std::FILE *const file, const char *const filename, - const unsigned int n0, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0) const { -#define _cimg_save_cimg_case(Ts,Tss) \ - if (!saved && !cimg::strcasecmp(Ts,str_pixeltype)) { \ - for (unsigned int l = 0; l0) { \ - if (l=W || y0>=H || z0>=D || c0>=D) std::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \ - else { \ - const CImg& img = (*this)[l - n0]; \ - const T *ptrs = img._data; \ - const unsigned int \ - x1 = x0 + img._width - 1, \ - y1 = y0 + img._height - 1, \ - z1 = z0 + img._depth - 1, \ - c1 = c0 + img._spectrum - 1, \ - nx1 = x1>=W?W-1:x1, \ - ny1 = y1>=H?H-1:y1, \ - nz1 = z1>=D?D-1:z1, \ - nc1 = c1>=C?C-1:c1; \ - CImg raw(1+nx1-x0); \ - const unsigned int skipvb = c0*W*H*D*sizeof(Tss); \ - if (skipvb) std::fseek(nfile,skipvb,SEEK_CUR); \ - for (unsigned int v = 1 + nc1 - c0; v; --v) { \ - const unsigned int skipzb = z0*W*H*sizeof(Tss); \ - if (skipzb) std::fseek(nfile,skipzb,SEEK_CUR); \ - for (unsigned int z = 1 + nz1 - z0; z; --z) { \ - const unsigned int skipyb = y0*W*sizeof(Tss); \ - if (skipyb) std::fseek(nfile,skipyb,SEEK_CUR); \ - for (unsigned int y = 1 + ny1 - y0; y; --y) { \ - const unsigned int skipxb = x0*sizeof(Tss); \ - if (skipxb) std::fseek(nfile,skipxb,SEEK_CUR); \ - raw.assign(ptrs, raw._width); \ - ptrs+=img._width; \ - if (endian) cimg::invert_endianness(raw._data,raw._width); \ - cimg::fwrite(raw._data,raw._width,nfile); \ - const unsigned int skipxe = (W - 1 - nx1)*sizeof(Tss); \ - if (skipxe) std::fseek(nfile,skipxe,SEEK_CUR); \ - } \ - const unsigned int skipye = (H - 1 - ny1)*W*sizeof(Tss); \ - if (skipye) std::fseek(nfile,skipye,SEEK_CUR); \ - } \ - const unsigned int skipze = (D - 1 - nz1)*W*H*sizeof(Tss); \ - if (skipze) std::fseek(nfile,skipze,SEEK_CUR); \ - } \ - const unsigned int skipve = (C - 1 - nc1)*W*H*D*sizeof(Tss); \ - if (skipve) std::fseek(nfile,skipve,SEEK_CUR); \ - } \ - } \ - } \ - saved = true; \ - } - - if (!file && !filename) - throw CImgArgumentException(_cimglist_instance - "save_cimg(): Specified filename is (null).", - cimglist_instance); - if (is_empty()) - throw CImgInstanceException(_cimglist_instance - "save_cimg(): Empty instance, for file '%s'.", - cimglist_instance, - filename?filename:"(FILE*)"); - - std::FILE *const nfile = file?file:cimg::fopen(filename,"rb+"); - bool saved = false, endian = cimg::endianness(); - char tmp[256] = { 0 }, str_pixeltype[256] = { 0 }, str_endian[256] = { 0 }; - unsigned int j, err, N, W, H, D, C; - int i; - j = 0; while((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0; - err = std::sscanf(tmp,"%u%*c%255[A-Za-z_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype,str_endian); - if (err<2) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "save_cimg(): CImg header not found in file '%s'.", - cimglist_instance, - filename?filename:"(FILE*)"); - } - if (!cimg::strncasecmp("little",str_endian,6)) endian = false; - else if (!cimg::strncasecmp("big",str_endian,3)) endian = true; - const unsigned int lmax = cimg::min(N,n0+_width); - _cimg_save_cimg_case("bool",bool); - _cimg_save_cimg_case("unsigned_char",unsigned char); - _cimg_save_cimg_case("uchar",unsigned char); - _cimg_save_cimg_case("char",char); - _cimg_save_cimg_case("unsigned_short",unsigned short); - _cimg_save_cimg_case("ushort",unsigned short); - _cimg_save_cimg_case("short",short); - _cimg_save_cimg_case("unsigned_int",unsigned int); - _cimg_save_cimg_case("uint",unsigned int); - _cimg_save_cimg_case("int",int); - _cimg_save_cimg_case("unsigned_long",unsigned long); - _cimg_save_cimg_case("ulong",unsigned long); - _cimg_save_cimg_case("long",long); - _cimg_save_cimg_case("float",float); - _cimg_save_cimg_case("double",double); - if (!saved) { - if (!file) cimg::fclose(nfile); - throw CImgIOException(_cimglist_instance - "save_cimg(): Unsupported data type '%s' for file '%s'.", - cimglist_instance, - filename?filename:"(FILE*)",str_pixeltype); - } - if (!file) cimg::fclose(nfile); - return *this; - } - - //! Insert the image instance into into an existing .cimg file, at specified coordinates. - /** - \param filename Filename to write data to. - \param n0 Starting index of images to write. - \param x0 Starting X-coordinates of image regions to write. - \param y0 Starting Y-coordinates of image regions to write. - \param z0 Starting Z-coordinates of image regions to write. - \param c0 Starting C-coordinates of image regions to write. - **/ - const CImgList& save_cimg(const char *const filename, - const unsigned int n0, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0) const { - return _save_cimg(0,filename,n0,x0,y0,z0,c0); - } - - //! Insert the image instance into into an existing .cimg file, at specified coordinates. - /** - \param file File to write data to. - \param n0 Starting index of images to write. - \param x0 Starting X-coordinates of image regions to write. - \param y0 Starting Y-coordinates of image regions to write. - \param z0 Starting Z-coordinates of image regions to write. - \param c0 Starting C-coordinates of image regions to write. - **/ - const CImgList& save_cimg(std::FILE *const file, - const unsigned int n0, - const unsigned int x0, const unsigned int y0, - const unsigned int z0, const unsigned int c0) const { - return _save_cimg(file,0,n0,x0,y0,z0,c0); - } - - static void _save_empty_cimg(std::FILE *const file, const char *const filename, - const unsigned int nb, - const unsigned int dx, const unsigned int dy, - const unsigned int dz, const unsigned int dc) { - std::FILE *const nfile = file?file:cimg::fopen(filename,"wb"); - const unsigned long siz = (unsigned long)dx*dy*dz*dc*sizeof(T); - std::fprintf(nfile,"%u %s\n",nb,pixel_type()); - for (unsigned int i=nb; i; --i) { - std::fprintf(nfile,"%u %u %u %u\n",dx,dy,dz,dc); - for (unsigned long off=siz; off; --off) std::fputc(0,nfile); - } - if (!file) cimg::fclose(nfile); - } - - //! Save empty (non-compressed) .cimg file with specified dimensions. - /** - \param filename Filename to write data to. - \param nb Number of images to write. - \param dx Width of images in the written file. - \param dy Height of images in the written file. - \param dz Depth of images in the written file. - \param dc Spectrum of images in the written file. - **/ - static void save_empty_cimg(const char *const filename, - const unsigned int nb, - const unsigned int dx, const unsigned int dy=1, - const unsigned int dz=1, const unsigned int dc=1) { - return _save_empty_cimg(0,filename,nb,dx,dy,dz,dc); - } - - //! Save empty .cimg file with specified dimensions. - /** - \param file File to write data to. - \param nb Number of images to write. - \param dx Width of images in the written file. - \param dy Height of images in the written file. - \param dz Depth of images in the written file. - \param dc Spectrum of images in the written file. - **/ - static void save_empty_cimg(std::FILE *const file, - const unsigned int nb, - const unsigned int dx, const unsigned int dy=1, - const unsigned int dz=1, const unsigned int dc=1) { - return _save_empty_cimg(file,0,nb,dx,dy,dz,dc); - } - - //! Save list as a TIFF file. - /** - \param filename Filename to write data to. - \param compression_type Compression mode used to write data. - **/ - const CImgList& save_tiff(const char *const filename, const unsigned int compression_type=0, - const float *const voxel_size=0) const { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "save_tiff(): Specified filename is (null).", - cimglist_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - -#ifndef cimg_use_tiff - if (_width==1) _data[0].save_tiff(filename,compression_type,voxel_size); - else cimglist_for(*this,l) { - char nfilename[1024] = { 0 }; - cimg::number_filename(filename,l,6,nfilename); - _data[l].save_tiff(nfilename,compression_type,voxel_size); - } -#else - TIFF *tif = TIFFOpen(filename,"w"); - if (tif) { - for (unsigned int dir = 0, l = 0; l<_width; ++l) { - const CImg& img = (*this)[l]; - if (img) { - if (img._depth==1) img._save_tiff(tif,dir++,compression_type,voxel_size); - else cimg_forZ(img,z) img.get_slice(z)._save_tiff(tif,dir++,compression_type,voxel_size); - } - } - TIFFClose(tif); - } else - throw CImgIOException(_cimglist_instance - "save_tiff(): Failed to open stream for file '%s'.", - cimglist_instance, - filename); -#endif - return *this; - } - - - //! Save list as a gzipped file, using external tool 'gzip'. - /** - \param filename Filename to write data to. - **/ - const CImgList& save_gzip_external(const char *const filename) const { - if (!filename) - throw CImgIOException(_cimglist_instance - "save_gzip_external(): Specified filename is (null).", - cimglist_instance); - - char command[1024] = { 0 }, filetmp[512] = { 0 }, body[512] = { 0 }; - const char - *ext = cimg::split_filename(filename,body), - *ext2 = cimg::split_filename(body,0); - std::FILE *file; - do { - if (!cimg::strcasecmp(ext,"gz")) { - if (*ext2) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } else { - if (*ext) cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.%s", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext); - else cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s.cimg", - cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - } - if ((file=std::fopen(filetmp,"rb"))!=0) cimg::fclose(file); - } while (file); - - if (is_saveable(body)) { - save(filetmp); - cimg_snprintf(command,sizeof(command),"%s -c \"%s\" > \"%s\"", - cimg::gzip_path(), - CImg::string(filetmp)._system_strescape().data(), - CImg::string(filename)._system_strescape().data()); - cimg::system(command); - file = std::fopen(filename,"rb"); - if (!file) - throw CImgIOException(_cimglist_instance - "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.", - cimglist_instance, - filename); - else cimg::fclose(file); - std::remove(filetmp); - } else { - char nfilename[1024] = { 0 }; - cimglist_for(*this,l) { - cimg::number_filename(body,l,6,nfilename); - if (*ext) std::sprintf(nfilename + std::strlen(nfilename),".%s",ext); - _data[l].save_gzip_external(nfilename); - } - } - return *this; - } - - //! Save image sequence, using the external tool 'ffmpeg'. - /** - \param filename Filename to write data to. - \param codec Type of compression. - \param fps Number of frames per second. - \param bitrate Output bitrate - **/ - const CImgList& save_ffmpeg_external(const char *const filename, const char *const codec=0, - const unsigned int fps=25, const unsigned int bitrate=2048) const { - if (!filename) - throw CImgArgumentException(_cimglist_instance - "save_ffmpeg_external(): Specified filename is (null).", - cimglist_instance); - if (is_empty()) { cimg::fempty(0,filename); return *this; } - - const char - *const ext = cimg::split_filename(filename), - *const _codec = codec?codec:!cimg::strcasecmp(ext,"flv")?"flv":"mpeg2video"; - - char command[1024] = { 0 }, filetmp[512] = { 0 }, filetmp2[512] = { 0 }; - CImgList filenames; - std::FILE *file = 0; - cimglist_for(*this,l) if (!_data[l].is_sameXYZ(_data[0])) - throw CImgInstanceException(_cimglist_instance - "save_ffmpeg_external(): Invalid instance dimensions for file '%s'.", - cimglist_instance, - filename); - do { - cimg_snprintf(filetmp,sizeof(filetmp),"%s%c%s",cimg::temporary_path(),cimg_file_separator,cimg::filenamerand()); - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_000001.ppm",filetmp); - if ((file=std::fopen(filetmp2,"rb"))!=0) cimg::fclose(file); - } while (file); - cimglist_for(*this,l) { - cimg_snprintf(filetmp2,sizeof(filetmp2),"%s_%.6u.ppm",filetmp,l+1); - CImg::string(filetmp2).move_to(filenames); - if (_data[l]._depth>1 || _data[l]._spectrum!=3) _data[l].get_resize(-100,-100,1,3).save_pnm(filetmp2); - else _data[l].save_pnm(filetmp2); - } -#if cimg_OS!=2 - cimg_snprintf(command,sizeof(command),"%s -i \"%s_%%6d.ppm\" -vcodec %s -b %uk -r %u -y \"%s\" >/dev/null 2>&1", - cimg::ffmpeg_path(), - CImg::string(filetmp)._system_strescape().data(), - _codec,bitrate,fps, - CImg::string(filename)._system_strescape().data()); -#else - cimg_snprintf(command,sizeof(command),"\"%s -i \"%s_%%6d.ppm\" -vcodec %s -b %uk -r %u -y \"%s\"\" >NUL 2>&1", - cimg::ffmpeg_path(), - CImg::string(filetmp)._system_strescape().data(), - _codec,bitrate,fps, - CImg::string(filename)._system_strescape().data()); -#endif - cimg::system(command); - file = std::fopen(filename,"rb"); - if (!file) - throw CImgIOException(_cimglist_instance - "save_ffmpeg_external(): Failed to save file '%s' with external command 'ffmpeg'.", - cimglist_instance, - filename); - else cimg::fclose(file); - cimglist_for(*this,l) std::remove(filenames[l]); - return *this; - } - - //@} - //---------------------------------- - // - //! \name Others - //@{ - //---------------------------------- - - //! Crop font along the X-axis. - /** - **/ - CImgList& crop_font() { - return get_crop_font().move_to(*this); - } - - //! Crop font along the X-axis \newinstance. - /** - **/ - CImgList get_crop_font() const { - CImgList res; - cimglist_for(*this,l) { - const CImg& letter = (*this)[l]; - int xmin = letter._width, xmax = 0; - cimg_forXY(letter,x,y) if (letter(x,y)) { if (xxmax) xmax = x; } - if (xmin>xmax) CImg(letter._width,letter._height,1,letter._spectrum,0).move_to(res); - else letter.get_crop(xmin,0,xmax,letter._height-1).move_to(res); - } - res[' '].resize(res['f']._width,-100,-100,-100,0); - if (' '+256& font(const unsigned int font_height, const bool is_variable_width=true) { - if (!font_height) return CImgList::empty(); - cimg::mutex(11); - - // Decompress nearest base font data if needed. - const char *data_fonts[] = { cimg::data_font12x13, cimg::data_font20x23, cimg::data_font47x53, 0 }; - const unsigned int data_widths[] = { 12,20,47,90 }, data_heights[] = { 13,23,53,103 }, - data_Ms[] = { 86,79,57,47 }; - const unsigned int data_ind = font_height<=13?0:font_height<=23?1:font_height<=53?2:3; - static CImg base_fonts[4]; - CImg &base_font = base_fonts[data_ind]; - if (!base_font) { - const unsigned int w = data_widths[data_ind], h = data_heights[data_ind], M = data_Ms[data_ind]; - base_font.assign(256*w,h); - const char *data_font = data_fonts[data_ind]; - unsigned char *ptrd = base_font; - const unsigned char *const ptrde = base_font.end(); - - // Special case needed for 90x103 to avoid MS compiler limit with big strings. - CImg data90x103; - if (!data_font) { - ((CImg(cimg::_data_font90x103[0], - (unsigned int)std::strlen(cimg::_data_font90x103[0]),1,1,1,true), - CImg(cimg::_data_font90x103[1], - (unsigned int)std::strlen(cimg::_data_font90x103[1])+1,1,1,1,true))>'x'). - move_to(data90x103); - data_font = data90x103.data(); - } - - // Uncompress font data (decode RLE). - for (const char *ptrs = data_font; *ptrs; ++ptrs) { - const int c = *ptrs-M-32, v = c>=0?255:0, n = c>=0?c:-c; - if (ptrd+n<=ptrde) { std::memset(ptrd,v,n); ptrd+=n; } - else { std::memset(ptrd,v,ptrde-ptrd); break; } - } - } - - // Find optimal font cache location to return. - static CImgList fonts[16]; - static bool is_variable_widths[16] = { 0 }; - unsigned int ind = ~0U; - for (int i = 0; i<16; ++i) - if (!fonts[i] || (is_variable_widths[i]==is_variable_width && font_height==fonts[i][0]._height)) { - ind = i; break; // Found empty slot or cached font. - } - if (ind==~0U) { // No empty slots nor existing font in cache. - std::memmove(fonts,fonts+1,15*sizeof(CImgList)); - std::memmove(is_variable_widths,is_variable_widths+1,15*sizeof(bool)); - std::memset(fonts+(ind=15),0,sizeof(CImgList)); // Free a slot in cache for new font. - } - CImgList &font = fonts[ind]; - - // Render requested font. - if (!font) { - const unsigned int padding_x = font_height<33?1:font_height<53?2:font_height<103?3:4; - is_variable_widths[ind] = is_variable_width; - font = base_font.get_split('x',256); - if (font_height!=font[0]._height) - cimglist_for(font,l) - font[l].resize(cimg::max(1U,font[l]._width*font_height/font[l]._height),font_height,-100,-100, - font[0]._height>font_height?2:5); - if (is_variable_width) font.crop_font(); - cimglist_for(font,l) font[l].resize(font[l]._width + padding_x,-100,1,1,0,0,0.5); - font.insert(256,0); - cimglist_for_in(font,0,255,l) font[l].assign(font[l+256]._width,font[l+256]._height,1,3,1); - } - cimg::mutex(11,0); - return font; - } - - //! Compute a 1d Fast Fourier Transform, along specified axis. - /** - \param axis Axis along which the Fourier transform is computed. - \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed. - **/ - CImgList& FFT(const char axis, const bool invert=false) { - if (is_empty()) return *this; - if (_width==1) insert(1); - if (_width>2) - cimg::warn(_cimglist_instance - "FFT(): Instance has more than 2 images", - cimglist_instance); - - CImg::FFT(_data[0],_data[1],axis,invert); - return *this; - } - - //! Compute a 1-D Fast Fourier Transform, along specified axis \newinstance. - CImgList get_FFT(const char axis, const bool invert=false) const { - return CImgList(*this,false).FFT(axis,invert); - } - - //! Compute a n-d Fast Fourier Transform. - /** - \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed. - **/ - CImgList& FFT(const bool invert=false) { - if (is_empty()) return *this; - if (_width==1) insert(1); - if (_width>2) - cimg::warn(_cimglist_instance - "FFT(): Instance has more than 2 images", - cimglist_instance); - - CImg::FFT(_data[0],_data[1],invert); - return *this; - } - - //! Compute a n-d Fast Fourier Transform \newinstance. - CImgList get_FFT(const bool invert=false) const { - return CImgList(*this,false).FFT(invert); - } - - //! Reverse primitives orientations of a 3d object. - /** - **/ - CImgList& reverse_object3d() { - cimglist_for(*this,l) { - CImg& p = _data[l]; - switch (p.size()) { - case 2: case 3: cimg::swap(p[0],p[1]); break; - case 6: cimg::swap(p[0],p[1],p[2],p[4],p[3],p[5]); break; - case 9: cimg::swap(p[0],p[1],p[3],p[5],p[4],p[6]); break; - case 4: cimg::swap(p[0],p[1],p[2],p[3]); break; - case 12: cimg::swap(p[0],p[1],p[2],p[3],p[4],p[6],p[5],p[7],p[8],p[10],p[9],p[11]); break; - } - } - return *this; - } - - //! Reverse primitives orientations of a 3d object \newinstance. - CImgList get_reverse_object3d() const { - return (+*this).reverse_object3d(); - } - - //@} - }; // struct CImgList { ... - - /* - #--------------------------------------------- - # - # Completion of previously declared functions - # - #---------------------------------------------- - */ - -namespace cimg { - - // Implement a tic/toc mechanism to display elapsed time of algorithms. - inline unsigned long tictoc(const bool is_tic) { - cimg::mutex(2); - static CImg times(64); - static unsigned int pos = 0; - const unsigned long t1 = cimg::time(); - if (is_tic) { // Tic. - times[pos++] = t1; - if (pos>=times._width) - throw CImgArgumentException("cimg::tic(): Too much calls to 'cimg::tic()' without calls to 'cimg::toc()'."); - cimg::mutex(2,0); - return t1; - } - // Toc. - if (!pos) - throw CImgArgumentException("cimg::toc(): No previous call to 'cimg::tic()' has been made."); - const unsigned long - t0 = times[--pos], - dt = t1>=t0?(t1-t0):cimg::type::max(); - const unsigned int - edays = (unsigned int)(dt/86400000.0), - ehours = (unsigned int)((dt - edays*86400000.0)/3600000.0), - emin = (unsigned int)((dt - edays*86400000.0 - ehours*3600000.0)/60000.0), - esec = (unsigned int)((dt - edays*86400000.0 - ehours*3600000.0 - emin*60000.0)/1000.0), - ems = (unsigned int)(dt - edays*86400000.0 - ehours*3600000.0 - emin*60000.0 - esec*1000.0); - if (!edays && !ehours && !emin && !esec) - std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u ms%s\n", - cimg::t_red,1+2*pos,"",ems,cimg::t_normal); - else { - if (!edays && !ehours && !emin) - std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u sec %u ms%s\n", - cimg::t_red,1+2*pos,"",esec,ems,cimg::t_normal); - else { - if (!edays && !ehours) - std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u min %u sec %u ms%s\n", - cimg::t_red,1+2*pos,"",emin,esec,ems,cimg::t_normal); - else{ - if (!edays) - std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u hours %u min %u sec %u ms%s\n", - cimg::t_red,1+2*pos,"",ehours,emin,esec,ems,cimg::t_normal); - else{ - std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u days %u hours %u min %u sec %u ms%s\n", - cimg::t_red,1+2*pos,"",edays,ehours,emin,esec,ems,cimg::t_normal); - } - } - } - } - cimg::mutex(2,0); - return dt; - } - - //! Display a simple dialog box, and wait for the user's response. - /** - \param title Title of the dialog window. - \param msg Main message displayed inside the dialog window. - \param button1_label Label of the 1st button. - \param button2_label Label of the 2nd button (\c 0 to hide button). - \param button3_label Label of the 3rd button (\c 0 to hide button). - \param button4_label Label of the 4th button (\c 0 to hide button). - \param button5_label Label of the 5th button (\c 0 to hide button). - \param button6_label Label of the 6th button (\c 0 to hide button). - \param logo Image logo displayed at the left of the main message. - \param is_centered Tells if the dialog window must be centered on the screen. - \return Indice of clicked button (from \c 0 to \c 5), or \c -1 if the dialog window has been closed by the user. - \note - - Up to 6 buttons can be defined in the dialog window. - - The function returns when a user clicked one of the button or closed the dialog window. - - If a button text is set to 0, the corresponding button (and the followings) will not appear in the dialog box. - At least one button must be specified. - **/ - template - inline int dialog(const char *const title, const char *const msg, - const char *const button1_label, const char *const button2_label, - const char *const button3_label, const char *const button4_label, - const char *const button5_label, const char *const button6_label, - const CImg& logo, const bool is_centered = false) { -#if cimg_display==0 - cimg::unused(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label, - logo._data,is_centered); - throw CImgIOException("cimg::dialog(): No display available."); -#else - const unsigned char - black[] = { 0,0,0 }, white[] = { 255,255,255 }, gray[] = { 200,200,200 }, gray2[] = { 150,150,150 }; - - // Create buttons and canvas graphics - CImgList buttons, cbuttons, sbuttons; - if (button1_label) { CImg().draw_text(0,0,button1_label,black,gray,1,13).move_to(buttons); - if (button2_label) { CImg().draw_text(0,0,button2_label,black,gray,1,13).move_to(buttons); - if (button3_label) { CImg().draw_text(0,0,button3_label,black,gray,1,13).move_to(buttons); - if (button4_label) { CImg().draw_text(0,0,button4_label,black,gray,1,13).move_to(buttons); - if (button5_label) { CImg().draw_text(0,0,button5_label,black,gray,1,13).move_to(buttons); - if (button6_label) { CImg().draw_text(0,0,button6_label,black,gray,1,13).move_to(buttons); - }}}}}} - if (!buttons._width) - throw CImgArgumentException("cimg::dialog(): No buttons have been defined."); - cimglist_for(buttons,l) buttons[l].resize(-100,-100,1,3); - - unsigned int bw = 0, bh = 0; - cimglist_for(buttons,l) { bw = cimg::max(bw,buttons[l]._width); bh = cimg::max(bh,buttons[l]._height); } - bw+=8; bh+=8; - if (bw<64) bw = 64; - if (bw>128) bw = 128; - if (bh<24) bh = 24; - if (bh>48) bh = 48; - - CImg button(bw,bh,1,3); - button.draw_rectangle(0,0,bw-1,bh-1,gray); - button.draw_line(0,0,bw-1,0,white).draw_line(0,bh-1,0,0,white); - button.draw_line(bw-1,0,bw-1,bh-1,black).draw_line(bw-1,bh-1,0,bh-1,black); - button.draw_line(1,bh-2,bw-2,bh-2,gray2).draw_line(bw-2,bh-2,bw-2,1,gray2); - CImg sbutton(bw,bh,1,3); - sbutton.draw_rectangle(0,0,bw-1,bh-1,gray); - sbutton.draw_line(0,0,bw-1,0,black).draw_line(bw-1,0,bw-1,bh-1,black); - sbutton.draw_line(bw-1,bh-1,0,bh-1,black).draw_line(0,bh-1,0,0,black); - sbutton.draw_line(1,1,bw-2,1,white).draw_line(1,bh-2,1,1,white); - sbutton.draw_line(bw-2,1,bw-2,bh-2,black).draw_line(bw-2,bh-2,1,bh-2,black); - sbutton.draw_line(2,bh-3,bw-3,bh-3,gray2).draw_line(bw-3,bh-3,bw-3,2,gray2); - sbutton.draw_line(4,4,bw-5,4,black,1,0xAAAAAAAA,true).draw_line(bw-5,4,bw-5,bh-5,black,1,0xAAAAAAAA,false); - sbutton.draw_line(bw-5,bh-5,4,bh-5,black,1,0xAAAAAAAA,false).draw_line(4,bh-5,4,4,black,1,0xAAAAAAAA,false); - CImg cbutton(bw,bh,1,3); - cbutton.draw_rectangle(0,0,bw-1,bh-1,black).draw_rectangle(1,1,bw-2,bh-2,gray2).draw_rectangle(2,2,bw-3,bh-3,gray); - cbutton.draw_line(4,4,bw-5,4,black,1,0xAAAAAAAA,true).draw_line(bw-5,4,bw-5,bh-5,black,1,0xAAAAAAAA,false); - cbutton.draw_line(bw-5,bh-5,4,bh-5,black,1,0xAAAAAAAA,false).draw_line(4,bh-5,4,4,black,1,0xAAAAAAAA,false); - - cimglist_for(buttons,ll) { - CImg(cbutton).draw_image(1+(bw-buttons[ll].width())/2,1+(bh-buttons[ll].height())/2,buttons[ll]). - move_to(cbuttons); - CImg(sbutton).draw_image((bw-buttons[ll].width())/2,(bh-buttons[ll].height())/2,buttons[ll]). - move_to(sbuttons); - CImg(button).draw_image((bw-buttons[ll].width())/2,(bh-buttons[ll].height())/2,buttons[ll]). - move_to(buttons[ll]); - } - - CImg canvas; - if (msg) - ((CImg().draw_text(0,0,"%s",gray,0,1,13,msg)*=-1)+=200).resize(-100,-100,1,3).move_to(canvas); - - const unsigned int - bwall = (buttons._width-1)*(12+bw) + bw, - w = cimg::max(196U,36+logo._width+canvas._width,24+bwall), - h = cimg::max(96U,36+canvas._height+bh,36+logo._height+bh), - lx = 12 + (canvas._data?0:((w-24-logo._width)/2)), - ly = (h-12-bh-logo._height)/2, - tx = lx+logo._width+12, - ty = (h-12-bh-canvas._height)/2, - bx = (w-bwall)/2, - by = h-12-bh; - - if (canvas._data) - canvas = CImg(w,h,1,3). - draw_rectangle(0,0,w-1,h-1,gray). - draw_line(0,0,w-1,0,white).draw_line(0,h-1,0,0,white). - draw_line(w-1,0,w-1,h-1,black).draw_line(w-1,h-1,0,h-1,black). - draw_image(tx,ty,canvas); - else - canvas = CImg(w,h,1,3). - draw_rectangle(0,0,w-1,h-1,gray). - draw_line(0,0,w-1,0,white).draw_line(0,h-1,0,0,white). - draw_line(w-1,0,w-1,h-1,black).draw_line(w-1,h-1,0,h-1,black); - if (logo._data) canvas.draw_image(lx,ly,logo); - - unsigned int xbuttons[6] = { 0 }; - cimglist_for(buttons,lll) { xbuttons[lll] = bx+(bw+12)*lll; canvas.draw_image(xbuttons[lll],by,buttons[lll]); } - - // Open window and enter events loop - CImgDisplay disp(canvas,title?title:" ",0,false,is_centered?true:false); - if (is_centered) disp.move((CImgDisplay::screen_width() - disp.width())/2, - (CImgDisplay::screen_height() - disp.height())/2); - bool stop_flag = false, refresh = false; - int oselected = -1, oclicked = -1, selected = -1, clicked = -1; - while (!disp.is_closed() && !stop_flag) { - if (refresh) { - if (clicked>=0) - CImg(canvas).draw_image(xbuttons[clicked],by,cbuttons[clicked]).display(disp); - else { - if (selected>=0) - CImg(canvas).draw_image(xbuttons[selected],by,sbuttons[selected]).display(disp); - else canvas.display(disp); - } - refresh = false; - } - disp.wait(15); - if (disp.is_resized()) disp.resize(disp,false); - - if (disp.button()&1) { - oclicked = clicked; - clicked = -1; - cimglist_for(buttons,l) - if (disp.mouse_y()>=(int)by && disp.mouse_y()<(int)(by+bh) && - disp.mouse_x()>=(int)xbuttons[l] && disp.mouse_x()<(int)(xbuttons[l]+bw)) { - clicked = selected = l; - refresh = true; - } - if (clicked!=oclicked) refresh = true; - } else if (clicked>=0) stop_flag = true; - - if (disp.key()) { - oselected = selected; - switch (disp.key()) { - case cimg::keyESC : selected=-1; stop_flag = true; break; - case cimg::keyENTER : if (selected<0) selected = 0; stop_flag = true; break; - case cimg::keyTAB : - case cimg::keyARROWRIGHT : - case cimg::keyARROWDOWN : selected = (selected+1)%buttons._width; break; - case cimg::keyARROWLEFT : - case cimg::keyARROWUP : selected = (selected+buttons._width-1)%buttons._width; break; - } - disp.set_key(); - if (selected!=oselected) refresh = true; - } - } - if (!disp) selected = -1; - return selected; -#endif - } - - //! Display a simple dialog box, and wait for the user's response \specialization. - inline int dialog(const char *const title, const char *const msg, - const char *const button1_label, const char *const button2_label, const char *const button3_label, - const char *const button4_label, const char *const button5_label, const char *const button6_label, - const bool is_centered) { - return dialog(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label, - CImg::_logo40x38(),is_centered); - } - - //! Evaluate math expression. - /** - \param expression C-string describing the formula to evaluate. - \param x Value of the pre-defined variable \c x. - \param y Value of the pre-defined variable \c y. - \param z Value of the pre-defined variable \c z. - \param c Value of the pre-defined variable \c c. - \return Result of the formula evaluation. - \note Set \c expression to \c 0 to keep evaluating the last specified \c expression. - \par Example - \code - const double - res1 = cimg::eval("cos(x)^2+sin(y)^2",2,2), // will return '1'. - res2 = cimg::eval(0,1,1); // will return '1' too. - \endcode - **/ - inline double eval(const char *const expression, const double x, const double y, const double z, const double c) { - static const CImg empty; - return empty.eval(expression,x,y,z,c); - } - - template - inline CImg::type> eval(const char *const expression, const CImg& xyzc) { - static const CImg empty; - return empty.eval(expression,xyzc); - } - - // End of cimg:: namespace -} - - // End of cimg_library:: namespace -} - -//! Short alias name. -namespace cil = cimg_library_suffixed; - -#ifdef _cimg_redefine_False -#define False 0 -#endif -#ifdef _cimg_redefine_True -#define True 1 -#endif -#ifdef _cimg_redefine_None -#define None 0 -#endif -#ifdef _cimg_redefine_min -#define min(a,b) (((a)<(b))?(a):(b)) -#endif -#ifdef _cimg_redefine_max -#define max(a,b) (((a)>(b))?(a):(b)) -#endif -#ifdef _cimg_redefine_PI -#define PI 3.141592653589793238462643383 -#endif - -#endif -// Local Variables: -// mode: c++ -// End: diff --git a/image/image.h b/image/image.h deleted file mode 100644 index a6d5195..0000000 --- a/image/image.h +++ /dev/null @@ -1,99 +0,0 @@ -#ifndef STIM_IMAGE_H -#define STIM_IMAGE_H -#define cimg_use_jpeg //necessary for JPG files -#include "CImg.h" - -#include -namespace stim{ -//This static class provides the STIM interface for loading images -// Use this interface for all image management - that way the actual library can be changed without problems - -//currently this interface uses CImg -// T = data type (usually unsigned char) -template -class image{ - - cimg_library::CImg img; - -public: - - //default constructor - image(){ - } - - //constructor (load an image file) - image(std::string filename){ - img.load(filename.c_str()); - } - - //Load an image from a file - void load(std::string filename){ - img.load(filename.c_str()); - } - - //save a file - void save(std::string filename){ - img.save(filename.c_str()); - } - - //create an image from an interleaved buffer - void set_interleaved(T* buffer, unsigned int width, unsigned int height, unsigned int channels = 1){ - - unsigned char* non_interleaved = (unsigned char*)malloc(width * height * 3); - unsigned int S = width * height; - - for(unsigned int i = 0; i < S; i++){ - for(unsigned int c = 0; c < channels; c++){ - non_interleaved[i + c * S] = buffer[i * channels + c]; - } - } - - img = cimg_library::CImg(non_interleaved, width, height, 1, channels); - } - - //fills an allocated region of memory with non-interleaved data - void data_noninterleaved(T* data){ - memcpy(data, img.data(), sizeof(T) * size()); - } - - void data_interleaved(T* data){ - - unsigned int C = channels(); - unsigned int X = size(); - - T* ptr = img.data(); - - //for each channel - for(unsigned int c = 0; c < C; c++) - //convert each pixel - for(unsigned int x = 0; x < X; x++) - data[x * C + c] = ptr[c * X + x]; - } - - unsigned int channels(){ - return (unsigned int)img.spectrum(); - } - - unsigned int width(){ - return img.width(); - } - - unsigned int height(){ - return img.height(); - } - - //returns the size (number of values) of the image - unsigned long size(){ - return img.size(); - } - - - - - -}; - -}; //end namespace stim - - -#endif diff --git a/math/bessel.h b/math/bessel.h deleted file mode 100644 index d291deb..0000000 --- a/math/bessel.h +++ /dev/null @@ -1,1515 +0,0 @@ -#ifndef RTS_BESSEL_H -#define RTS_BESSEL_H - -#define _USE_MATH_DEFINES -#include -#include "../math/complex.h" -#define eps 1e-15 -#define el 0.5772156649015329 - - -namespace stim{ - -static complex cii(0.0,1.0); -static complex cone(1.0,0.0); -static complex czero(0.0,0.0); - -template< typename P > -P gamma(P x) -{ - int i,k,m; - P ga,gr,r,z; - - static P g[] = { - 1.0, - 0.5772156649015329, - -0.6558780715202538, - -0.420026350340952e-1, - 0.1665386113822915, - -0.421977345555443e-1, - -0.9621971527877e-2, - 0.7218943246663e-2, - -0.11651675918591e-2, - -0.2152416741149e-3, - 0.1280502823882e-3, - -0.201348547807e-4, - -0.12504934821e-5, - 0.1133027232e-5, - -0.2056338417e-6, - 0.6116095e-8, - 0.50020075e-8, - -0.11812746e-8, - 0.1043427e-9, - 0.77823e-11, - -0.36968e-11, - 0.51e-12, - -0.206e-13, - -0.54e-14, - 0.14e-14}; - - if (x > 171.0) return 1e308; // This value is an overflow flag. - if (x == (int)x) { - if (x > 0.0) { - ga = 1.0; // use factorial - for (i=2;i 1.0) { - z = fabs(x); - m = (int)z; - r = 1.0; - for (k=1;k<=m;k++) { - r *= (z-k); - } - z -= m; - } - else - z = x; - gr = g[24]; - for (k=23;k>=0;k--) { - gr = gr*z+g[k]; - } - ga = 1.0/(gr*z); - if (fabs(x) > 1.0) { - ga *= r; - if (x < 0.0) { - ga = -M_PI/(x*ga*sin(M_PI*x)); - } - } - } - return ga; -} - -template -int bessjy01a(P x,P &j0,P &j1,P &y0,P &y1, - P &j0p,P &j1p,P &y0p,P &y1p) -{ - P x2,r,ec,w0,w1,r0,r1,cs0,cs1; - P cu,p0,q0,p1,q1,t1,t2; - int k,kz; - static P a[] = { - -7.03125e-2, - 0.112152099609375, - -0.5725014209747314, - 6.074042001273483, - -1.100171402692467e2, - 3.038090510922384e3, - -1.188384262567832e5, - 6.252951493434797e6, - -4.259392165047669e8, - 3.646840080706556e10, - -3.833534661393944e12, - 4.854014686852901e14, - -7.286857349377656e16, - 1.279721941975975e19}; - static P b[] = { - 7.32421875e-2, - -0.2271080017089844, - 1.727727502584457, - -2.438052969955606e1, - 5.513358961220206e2, - -1.825775547429318e4, - 8.328593040162893e5, - -5.006958953198893e7, - 3.836255180230433e9, - -3.649010818849833e11, - 4.218971570284096e13, - -5.827244631566907e15, - 9.476288099260110e17, - -1.792162323051699e20}; - static P a1[] = { - 0.1171875, - -0.1441955566406250, - 0.6765925884246826, - -6.883914268109947, - 1.215978918765359e2, - -3.302272294480852e3, - 1.276412726461746e5, - -6.656367718817688e6, - 4.502786003050393e8, - -3.833857520742790e10, - 4.011838599133198e12, - -5.060568503314727e14, - 7.572616461117958e16, - -1.326257285320556e19}; - static P b1[] = { - -0.1025390625, - 0.2775764465332031, - -1.993531733751297, - 2.724882731126854e1, - -6.038440767050702e2, - 1.971837591223663e4, - -8.902978767070678e5, - 5.310411010968522e7, - -4.043620325107754e9, - 3.827011346598605e11, - -4.406481417852278e13, - 6.065091351222699e15, - -9.833883876590679e17, - 1.855045211579828e20}; - - if (x < 0.0) return 1; - if (x == 0.0) { - j0 = 1.0; - j1 = 0.0; - y0 = -1e308; - y1 = -1e308; - j0p = 0.0; - j1p = 0.5; - y0p = 1e308; - y1p = 1e308; - return 0; - } - x2 = x*x; - if (x <= 12.0) { - j0 = 1.0; - r = 1.0; - for (k=1;k<=30;k++) { - r *= -0.25*x2/(k*k); - j0 += r; - if (fabs(r) < fabs(j0)*1e-15) break; - } - j1 = 1.0; - r = 1.0; - for (k=1;k<=30;k++) { - r *= -0.25*x2/(k*(k+1)); - j1 += r; - if (fabs(r) < fabs(j1)*1e-15) break; - } - j1 *= 0.5*x; - ec = log(0.5*x)+el; - cs0 = 0.0; - w0 = 0.0; - r0 = 1.0; - for (k=1;k<=30;k++) { - w0 += 1.0/k; - r0 *= -0.25*x2/(k*k); - r = r0 * w0; - cs0 += r; - if (fabs(r) < fabs(cs0)*1e-15) break; - } - y0 = M_2_PI*(ec*j0-cs0); - cs1 = 1.0; - w1 = 0.0; - r1 = 1.0; - for (k=1;k<=30;k++) { - w1 += 1.0/k; - r1 *= -0.25*x2/(k*(k+1)); - r = r1*(2.0*w1+1.0/(k+1)); - cs1 += r; - if (fabs(r) < fabs(cs1)*1e-15) break; - } - y1 = M_2_PI * (ec*j1-1.0/x-0.25*x*cs1); - } - else { - if (x >= 50.0) kz = 8; // Can be changed to 10 - else if (x >= 35.0) kz = 10; // " " 12 - else kz = 12; // " " 14 - t1 = x-M_PI_4; - p0 = 1.0; - q0 = -0.125/x; - for (k=0;k -int bessjy01b(P x,P &j0,P &j1,P &y0,P &y1, - P &j0p,P &j1p,P &y0p,P &y1p) -{ - P t,t2,dtmp,a0,p0,q0,p1,q1,ta0,ta1; - if (x < 0.0) return 1; - if (x == 0.0) { - j0 = 1.0; - j1 = 0.0; - y0 = -1e308; - y1 = -1e308; - j0p = 0.0; - j1p = 0.5; - y0p = 1e308; - y1p = 1e308; - return 0; - } - if(x <= 4.0) { - t = x/4.0; - t2 = t*t; - j0 = ((((((-0.5014415e-3*t2+0.76771853e-2)*t2-0.0709253492)*t2+ - 0.4443584263)*t2-1.7777560599)*t2+3.9999973021)*t2 - -3.9999998721)*t2+1.0; - j1 = t*(((((((-0.1289769e-3*t2+0.22069155e-2)*t2-0.0236616773)*t2+ - 0.1777582922)*t2-0.8888839649)*t2+2.6666660544)*t2- - 3.999999971)*t2+1.9999999998); - dtmp = (((((((-0.567433e-4*t2+0.859977e-3)*t2-0.94855882e-2)*t2+ - 0.0772975809)*t2-0.4261737419)*t2+1.4216421221)*t2- - 2.3498519931)*t2+1.0766115157)*t2+0.3674669052; - y0 = M_2_PI*log(0.5*x)*j0+dtmp; - dtmp = (((((((0.6535773e-3*t2-0.0108175626)*t2+0.107657607)*t2- - 0.7268945577)*t2+3.1261399273)*t2-7.3980241381)*t2+ - 6.8529236342)*t2+0.3932562018)*t2-0.6366197726; - y1 = M_2_PI*log(0.5*x)*j1+dtmp/x; - } - else { - t = 4.0/x; - t2 = t*t; - a0 = sqrt(M_2_PI/x); - p0 = ((((-0.9285e-5*t2+0.43506e-4)*t2-0.122226e-3)*t2+ - 0.434725e-3)*t2-0.4394275e-2)*t2+0.999999997; - q0 = t*(((((0.8099e-5*t2-0.35614e-4)*t2+0.85844e-4)*t2- - 0.218024e-3)*t2+0.1144106e-2)*t2-0.031249995); - ta0 = x-M_PI_4; - j0 = a0*(p0*cos(ta0)-q0*sin(ta0)); - y0 = a0*(p0*sin(ta0)+q0*cos(ta0)); - p1 = ((((0.10632e-4*t2-0.50363e-4)*t2+0.145575e-3)*t2 - -0.559487e-3)*t2+0.7323931e-2)*t2+1.000000004; - q1 = t*(((((-0.9173e-5*t2+0.40658e-4)*t2-0.99941e-4)*t2 - +0.266891e-3)*t2-0.1601836e-2)*t2+0.093749994); - ta1 = x-0.75*M_PI; - j1 = a0*(p1*cos(ta1)-q1*sin(ta1)); - y1 = a0*(p1*sin(ta1)+q1*cos(ta1)); - } - j0p = -j1; - j1p = j0-j1/x; - y0p = -y1; - y1p = y0-y1/x; - return 0; -} -template -int msta1(P x,int mp) -{ - P a0,f0,f1,f; - int i,n0,n1,nn; - - a0 = fabs(x); - n0 = (int)(1.1*a0)+1; - f0 = 0.5*log10(6.28*n0)-n0*log10(1.36*a0/n0)-mp; - n1 = n0+5; - f1 = 0.5*log10(6.28*n1)-n1*log10(1.36*a0/n1)-mp; - for (i=0;i<20;i++) { - nn = (int)(n1-(n1-n0)/(1.0-f0/f1)); - f = 0.5*log10(6.28*nn)-nn*log10(1.36*a0/nn)-mp; - if (abs(nn-n1) < 1) break; - n0 = n1; - f0 = f1; - n1 = nn; - f1 = f; - } - return nn; -} -template -int msta2(P x,int n,int mp) -{ - P a0,ejn,hmp,f0,f1,f,obj; - int i,n0,n1,nn; - - a0 = fabs(x); - hmp = 0.5*mp; - ejn = 0.5*log10(6.28*n)-n*log10(1.36*a0/n); - if (ejn <= hmp) { - obj = mp; - n0 = (int)(1.1*a0); - if (n0 < 1) n0 = 1; - } - else { - obj = hmp+ejn; - n0 = n; - } - f0 = 0.5*log10(6.28*n0)-n0*log10(1.36*a0/n0)-obj; - n1 = n0+5; - f1 = 0.5*log10(6.28*n1)-n1*log10(1.36*a0/n1)-obj; - for (i=0;i<20;i++) { - nn = (int)(n1-(n1-n0)/(1.0-f0/f1)); - f = 0.5*log10(6.28*nn)-nn*log10(1.36*a0/nn)-obj; - if (abs(nn-n1) < 1) break; - n0 = n1; - f0 = f1; - n1 = nn; - f1 = f; - } - return nn+10; -} -// -// INPUT: -// double x -- argument of Bessel function of 1st and 2nd kind. -// int n -- order -// -// OUPUT: -// -// int nm -- highest order actually computed (nm <= n) -// double jn[] -- Bessel function of 1st kind, orders from 0 to nm -// double yn[] -- Bessel function of 2nd kind, orders from 0 to nm -// double j'n[]-- derivative of Bessel function of 1st kind, -// orders from 0 to nm -// double y'n[]-- derivative of Bessel function of 2nd kind, -// orders from 0 to nm -// -// Computes Bessel functions of all order up to 'n' using recurrence -// relations. If 'nm' < 'n' only 'nm' orders are returned. -// -template -int bessjyna(int n,P x,int &nm,P *jn,P *yn, - P *jnp,P *ynp) -{ - P bj0,bj1,f,f0,f1,f2,cs; - int i,k,m,ecode; - - nm = n; - if ((x < 0.0) || (n < 0)) return 1; - if (x < 1e-15) { - for (i=0;i<=n;i++) { - jn[i] = 0.0; - yn[i] = -1e308; - jnp[i] = 0.0; - ynp[i] = 1e308; - } - jn[0] = 1.0; - jnp[1] = 0.5; - return 0; - } - ecode = bessjy01a(x,jn[0],jn[1],yn[0],yn[1],jnp[0],jnp[1],ynp[0],ynp[1]); - if (n < 2) return 0; - bj0 = jn[0]; - bj1 = jn[1]; - if (n < (int)0.9*x) { - for (k=2;k<=n;k++) { - jn[k] = 2.0*(k-1.0)*bj1/x-bj0; - bj0 = bj1; - bj1 = jn[k]; - } - } - else { - m = msta1(x,200); - if (m < n) nm = m; - else m = msta2(x,n,15); - f2 = 0.0; - f1 = 1.0e-100; - for (k=m;k>=0;k--) { - f = 2.0*(k+1.0)/x*f1-f2; - if (k <= nm) jn[k] = f; - f2 = f1; - f1 = f; - } - if (fabs(bj0) > fabs(bj1)) cs = bj0/f; - else cs = bj1/f2; - for (k=0;k<=nm;k++) { - jn[k] *= cs; - } - } - for (k=2;k<=nm;k++) { - jnp[k] = jn[k-1]-k*jn[k]/x; - } - f0 = yn[0]; - f1 = yn[1]; - for (k=2;k<=nm;k++) { - f = 2.0*(k-1.0)*f1/x-f0; - yn[k] = f; - f0 = f1; - f1 = f; - } - for (k=2;k<=nm;k++) { - ynp[k] = yn[k-1]-k*yn[k]/x; - } - return 0; -} -// -// Same input and output conventions as above. Different recurrence -// relations used for 'x' < 300. -// -template -int bessjynb(int n,P x,int &nm,P *jn,P *yn, - P *jnp,P *ynp) -{ - P t1,t2,f,f1,f2,bj0,bj1,bjk,by0,by1,cu,s0,su,sv; - P ec,bs,byk,p0,p1,q0,q1; - static P a[] = { - -0.7031250000000000e-1, - 0.1121520996093750, - -0.5725014209747314, - 6.074042001273483}; - static P b[] = { - 0.7324218750000000e-1, - -0.2271080017089844, - 1.727727502584457, - -2.438052969955606e1}; - static P a1[] = { - 0.1171875, - -0.1441955566406250, - 0.6765925884246826, - -6.883914268109947}; - static P b1[] = { - -0.1025390625, - 0.2775764465332031, - -1.993531733751297, - 2.724882731126854e1}; - - int i,k,m; - nm = n; - if ((x < 0.0) || (n < 0)) return 1; - if (x < 1e-15) { - for (i=0;i<=n;i++) { - jn[i] = 0.0; - yn[i] = -1e308; - jnp[i] = 0.0; - ynp[i] = 1e308; - } - jn[0] = 1.0; - jnp[1] = 0.5; - return 0; - } - if (x <= 300.0 || n > (int)(0.9*x)) { - if (n == 0) nm = 1; - m = msta1(x,200); - if (m < nm) nm = m; - else m = msta2(x,nm,15); - bs = 0.0; - su = 0.0; - sv = 0.0; - f2 = 0.0; - f1 = 1.0e-100; - for (k = m;k>=0;k--) { - f = 2.0*(k+1.0)/x*f1 - f2; - if (k <= nm) jn[k] = f; - if ((k == 2*(int)(k/2)) && (k != 0)) { - bs += 2.0*f; -// su += pow(-1,k>>1)*f/(double)k; - su += (-1)*((k & 2)-1)*f/(P)k; - } - else if (k > 1) { -// sv += pow(-1,k>>1)*k*f/(k*k-1.0); - sv += (-1)*((k & 2)-1)*(P)k*f/(k*k-1.0); - } - f2 = f1; - f1 = f; - } - s0 = bs+f; - for (k=0;k<=nm;k++) { - jn[k] /= s0; - } - ec = log(0.5*x) +0.5772156649015329; - by0 = M_2_PI*(ec*jn[0]-4.0*su/s0); - yn[0] = by0; - by1 = M_2_PI*((ec-1.0)*jn[1]-jn[0]/x-4.0*sv/s0); - yn[1] = by1; - } - else { - t1 = x-M_PI_4; - p0 = 1.0; - q0 = -0.125/x; - for (k=0;k<4;k++) { - p0 += a[k]*pow(x,-2*k-2); - q0 += b[k]*pow(x,-2*k-3); - } - cu = sqrt(M_2_PI/x); - bj0 = cu*(p0*cos(t1)-q0*sin(t1)); - by0 = cu*(p0*sin(t1)+q0*cos(t1)); - jn[0] = bj0; - yn[0] = by0; - t2 = x-0.75*M_PI; - p1 = 1.0; - q1 = 0.375/x; - for (k=0;k<4;k++) { - p1 += a1[k]*pow(x,-2*k-2); - q1 += b1[k]*pow(x,-2*k-3); - } - bj1 = cu*(p1*cos(t2)-q1*sin(t2)); - by1 = cu*(p1*sin(t2)+q1*cos(t2)); - jn[1] = bj1; - yn[1] = by1; - for (k=2;k<=nm;k++) { - bjk = 2.0*(k-1.0)*bj1/x-bj0; - jn[k] = bjk; - bj0 = bj1; - bj1 = bjk; - } - } - jnp[0] = -jn[1]; - for (k=1;k<=nm;k++) { - jnp[k] = jn[k-1]-k*jn[k]/x; - } - for (k=2;k<=nm;k++) { - byk = 2.0*(k-1.0)*by1/x-by0; - yn[k] = byk; - by0 = by1; - by1 = byk; - } - ynp[0] = -yn[1]; - for (k=1;k<=nm;k++) { - ynp[k] = yn[k-1]-k*yn[k]/x; - } - return 0; - -} - -// The following routine computes Bessel Jv(x) and Yv(x) for -// arbitrary positive order (v). For negative order, use: -// -// J-v(x) = Jv(x)cos(v pi) - Yv(x)sin(v pi) -// Y-v(x) = Jv(x)sin(v pi) + Yv(x)cos(v pi) -// -template -int bessjyv(P v,P x,P &vm,P *jv,P *yv, - P *djv,P *dyv) -{ - P v0,vl,vg,vv,a,a0,r,x2,bjv0,bjv1,bjvl,f,f0,f1,f2; - P r0,r1,ck,cs,cs0,cs1,sk,qx,px,byv0,byv1,rp,xk,rq; - P b,ec,w0,w1,bju0,bju1,pv0,pv1,byvk; - int j,k,l,m,n,kz; - - x2 = x*x; - n = (int)v; - v0 = v-n; - if ((x < 0.0) || (v < 0.0)) return 1; - if (x < 1e-15) { - for (k=0;k<=n;k++) { - jv[k] = 0.0; - yv[k] = -1e308; - djv[k] = 0.0; - dyv[k] = 1e308; - if (v0 == 0.0) { - jv[0] = 1.0; - djv[1] = 0.5; - } - else djv[0] = 1e308; - } - vm = v; - return 0; - } - if (x <= 12.0) { - for (l=0;l<2;l++) { - vl = v0 + l; - bjvl = 1.0; - r = 1.0; - for (k=1;k<=40;k++) { - r *= -0.25*x2/(k*(k+vl)); - bjvl += r; - if (fabs(r) < fabs(bjvl)*1e-15) break; - } - vg = 1.0 + vl; - a = pow(0.5*x,vl)/gamma(vg); - if (l == 0) bjv0 = bjvl*a; - else bjv1 = bjvl*a; - } - } - else { - if (x >= 50.0) kz = 8; - else if (x >= 35.0) kz = 10; - else kz = 11; - for (j=0;j<2;j++) { - vv = 4.0*(j+v0)*(j+v0); - px = 1.0; - rp = 1.0; - for (k=1;k<=kz;k++) { - rp *= (-0.78125e-2)*(vv-pow(4.0*k-3.0,2.0))* - (vv-pow(4.0*k-1.0,2.0))/(k*(2.0*k-1.0)*x2); - px += rp; - } - qx = 1.0; - rq = 1.0; - for (k=1;k<=kz;k++) { - rq *= (-0.78125e-2)*(vv-pow(4.0*k-1.0,2.0))* - (vv-pow(4.0*k+1.0,2.0))/(k*(2.0*k+1.0)*x2); - qx += rq; - } - qx *= 0.125*(vv-1.0)/x; - xk = x-(0.5*(j+v0)+0.25)*M_PI; - a0 = sqrt(M_2_PI/x); - ck = cos(xk); - sk = sin(xk); - - if (j == 0) { - bjv0 = a0*(px*ck-qx*sk); - byv0 = a0*(px*sk+qx*ck); - } - else if (j == 1) { - bjv1 = a0*(px*ck-qx*sk); - byv1 = a0*(px*sk+qx*ck); - } - } - } - jv[0] = bjv0; - jv[1] = bjv1; - djv[0] = v0*jv[0]/x-jv[1]; - djv[1] = -(1.0+v0)*jv[1]/x+jv[0]; - if ((n >= 2) && (n <= (int)(0.9*x))) { - f0 = bjv0; - f1 = bjv1; - for (k=2;k<=n;k++) { - f = 2.0*(k+v0-1.0)*f1/x-f0; - jv[k] = f; - f0 = f1; - f1 = f; - } - } - else if (n >= 2) { - m = msta1(x,200); - if (m < n) n = m; - else m = msta2(x,n,15); - f2 = 0.0; - f1 = 1.0e-100; - for (k=m;k>=0;k--) { - f = 2.0*(v0+k+1.0)*f1/x-f2; - if (k <= n) jv[k] = f; - f2 = f1; - f1 = f; - } - if (fabs(bjv0) > fabs(bjv1)) cs = bjv0/f; - else cs = bjv1/f2; - for (k=0;k<=n;k++) { - jv[k] *= cs; - } - } - for (k=2;k<=n;k++) { - djv[k] = -(k+v0)*jv[k]/x+jv[k-1]; - } - if (x <= 12.0) { - if (v0 != 0.0) { - for (l=0;l<2;l++) { - vl = v0 +l; - bjvl = 1.0; - r = 1.0; - for (k=1;k<=40;k++) { - r *= -0.25*x2/(k*(k-vl)); - bjvl += r; - if (fabs(r) < fabs(bjvl)*1e-15) break; - } - vg = 1.0-vl; - b = pow(2.0/x,vl)/gamma(vg); - if (l == 0) bju0 = bjvl*b; - else bju1 = bjvl*b; - } - pv0 = M_PI*v0; - pv1 = M_PI*(1.0+v0); - byv0 = (bjv0*cos(pv0)-bju0)/sin(pv0); - byv1 = (bjv1*cos(pv1)-bju1)/sin(pv1); - } - else { - ec = log(0.5*x)+el; - cs0 = 0.0; - w0 = 0.0; - r0 = 1.0; - for (k=1;k<=30;k++) { - w0 += 1.0/k; - r0 *= -0.25*x2/(k*k); - cs0 += r0*w0; - } - byv0 = M_2_PI*(ec*bjv0-cs0); - cs1 = 1.0; - w1 = 0.0; - r1 = 1.0; - for (k=1;k<=30;k++) { - w1 += 1.0/k; - r1 *= -0.25*x2/(k*(k+1)); - cs1 += r1*(2.0*w1+1.0/(k+1.0)); - } - byv1 = M_2_PI*(ec*bjv1-1.0/x-0.25*x*cs1); - } - } - yv[0] = byv0; - yv[1] = byv1; - for (k=2;k<=n;k++) { - byvk = 2.0*(v0+k-1.0)*byv1/x-byv0; - yv[k] = byvk; - byv0 = byv1; - byv1 = byvk; - } - dyv[0] = v0*yv[0]/x-yv[1]; - for (k=1;k<=n;k++) { - dyv[k] = -(k+v0)*yv[k]/x+yv[k-1]; - } - vm = n + v0; - return 0; -} - -template -int bessjyv_sph(int v, P z, P &vm, P* cjv, - P* cyv, P* cjvp, P* cyvp) -{ - //first, compute the bessel functions of fractional order - bessjyv(v + 0.5, z, vm, cjv, cyv, cjvp, cyvp); - - //iterate through each and scale - for(int n = 0; n<=v; n++) - { - - cjv[n] = cjv[n] * sqrt(rtsPI/(z * 2.0)); - cyv[n] = cyv[n] * sqrt(rtsPI/(z * 2.0)); - - cjvp[n] = -1.0 / (z * 2.0) * cjv[n] + cjvp[n] * sqrt(rtsPI / (z * 2.0)); - cyvp[n] = -1.0 / (z * 2.0) * cyv[n] + cyvp[n] * sqrt(rtsPI / (z * 2.0)); - } - - return 0; - -} - -template -int cbessjy01(complex