2#include "kaleidoscope-config.h"
5#define _USE_MATH_DEFINES
15 if (major && minor && patch)
17 *major = PROJECT_MAJOR_VERSION;
18 *minor = PROJECT_MINOR_VERSION;
19 *patch = PROJECT_PATCH_VERSION;
25 static char info[
sizeof(PROJECT_VERSION)];
26 strncpy(info, PROJECT_VERSION,
sizeof(PROJECT_VERSION));
33 static char info[125];
35 strncpy(info, PROJECT_VERSION,
sizeof(PROJECT_VERSION));
36 offset +=
sizeof(PROJECT_VERSION);
37 memset(&info[offset - 1], 32, 1);
38 strncpy(&info[offset], COMPILER_NAME,
sizeof(COMPILER_NAME));
39 offset +=
sizeof(COMPILER_NAME);
40 memset(&info[offset - 1], 32, 1);
41 strncpy(&info[offset], COMPILER_VERSION,
sizeof(COMPILER_VERSION));
42 offset +=
sizeof(COMPILER_VERSION);
43 memset(&info[offset - 1], 32, 1);
44 strncpy(&info[offset], BUILD_TYPE,
sizeof(BUILD_TYPE));
45 offset +=
sizeof(BUILD_TYPE);
46 memset(&info[offset - 1], 32, 1);
47 strncpy(&info[offset], PROJECT_BUILD_DATE,
sizeof(PROJECT_BUILD_DATE));
48 offset +=
sizeof(PROJECT_BUILD_DATE);
49 memset(&info[offset - 1], 32, 1);
50 strncpy(&info[offset], PROJECT_BUILD_TIME,
sizeof(PROJECT_BUILD_TIME));
55static int compare(
const void *lhsPtr,
const void *rhsPtr)
65 for (
int idx = 1; idx < height - 1; ++idx)
67 int heightOffset = idx * width;
68 for (
int jdx = 1; jdx < width - 1; ++jdx)
76 if (((ptrIn - 1)->dstLocation.x) || ((ptrIn - 1)->dstLocation.y))
81 else if (((ptrIn + 1)->dstLocation.x) || ((ptrIn + 1)->dstLocation.y))
86 else if (((ptrIn - width)->dstLocation.x) || ((ptrIn - width)->dstLocation.y))
89 ptrOut->
srcLocation.
y = (ptrIn - width)->srcLocation.y - 1;
91 else if (((ptrIn + width)->dstLocation.x) || ((ptrIn + width)->dstLocation.y))
94 ptrOut->
srcLocation.
y = (ptrIn + width)->srcLocation.y + 1;
96 else if (((ptrIn - width - 1)->dstLocation.x) || ((ptrIn - width - 1)->dstLocation.y))
98 ptrOut->
srcLocation.
x = (ptrIn - width - 1)->srcLocation.x - 1;
99 ptrOut->
srcLocation.
y = (ptrIn - width - 1)->srcLocation.y - 1;
101 else if (((ptrIn - width + 1)->dstLocation.x) || ((ptrIn - width + 1)->dstLocation.y))
103 ptrOut->
srcLocation.
x = (ptrIn - width + 1)->srcLocation.x + 1;
104 ptrOut->
srcLocation.
y = (ptrIn - width + 1)->srcLocation.y - 1;
106 else if (((ptrIn + width - 1)->dstLocation.x) || ((ptrIn + width - 1)->dstLocation.y))
108 ptrOut->
srcLocation.
x = (ptrIn + width - 1)->srcLocation.x - 1;
109 ptrOut->
srcLocation.
y = (ptrIn + width - 1)->srcLocation.y - 1;
111 else if (((ptrIn + width + 1)->dstLocation.x) || ((ptrIn + width + 1)->dstLocation.y))
113 ptrOut->
srcLocation.
x = (ptrIn + width + 1)->srcLocation.x + 1;
114 ptrOut->
srcLocation.
y = (ptrIn + width + 1)->srcLocation.y + 1;
131 double cosVal = cos(angle * M_PI / 180);
132 double sinVal = sin(angle * M_PI / 180);
134 for (
int idx = 0; idx < width * height; ++idx)
136 if (orgData[idx].dstLocation.x || orgData[idx].
dstLocation.
y)
138 int newX = (int)round(orgData[idx].dstLocation.x * cosVal + orgData[idx].
dstLocation.
y * sinVal);
139 int newY = (int)round(orgData[idx].dstLocation.y * cosVal - orgData[idx].
dstLocation.
x * sinVal);
143 newY += (height / 2);
145 if (newX <= width && newX >= 0 && newY <= height && newY >= 0)
158 const double topAngle = 360.0 / n;
159 const double tanVal = tan(topAngle / 2.0 * M_PI / 180.0);
160 const int triangleHeight = (int)fmin(round(width / (2.0 * tanVal)), height - 1);
161 const int heightStart = (height - triangleHeight) / 2;
162 const int heightEnd = (height + triangleHeight) / 2;
163 const int scaleDownOffset = (int)(height * scaleDown / 2);
166 assert(heightStart >= 0);
167 assert(heightStart <= height);
168 assert(heightEnd >= 0);
169 assert(heightEnd <= height);
171 for (
int idx = heightStart; idx < heightEnd; ++idx)
173 const int currentBaseLength = (int)((idx - heightStart) * tanVal);
175 const int widthStart = (width / 2 - currentBaseLength);
176 const int widthEnd = (width / 2 + currentBaseLength);
179 if (widthStart < 0 || widthStart > width || widthEnd < 0 || widthEnd > width)
185 for (
int jdx = widthStart; jdx <= widthEnd; ++jdx)
191 ptr[jdx].
dstLocation.
x = (int)((jdx - width / 2) * scaleDown);
192 ptr[jdx].
dstLocation.
y = (int)((idx - heightStart - height / 2) * scaleDown + scaleDownOffset);
201 int retval = EXIT_FAILURE;
202 const int nPixels = width * height;
207 if (handler == NULL || n <= 2 || width <= 0 || height <= 0 || nComponents <= 0 || scaleDown <= 0.0 ||
217 assert(nComponents > 0);
218 assert(scaleDown > 0.0);
219 assert(scaleDown < 1.0);
221 handler->
width = width;
227 if (!buffPtr1 || !buffPtr2)
235 for (
int idx = 0; idx < n; ++idx)
237 double rotationAngle = idx * (360.0 / n);
238 rotatePoints(buffPtr2, buffPtr1, width, height, rotationAngle);
247 for (
int idx = 0; idx < nPixels; ++idx)
255 buffPtr1[handler->
nPoints] = *ptr;
268 for (
int idx = 1; idx < handler->
nPoints; ++idx)
270 if (
compare(&buffPtr1[jdx], &buffPtr1[idx]))
272 buffPtr1[jdx] = buffPtr1[idx];
280 retval = EXIT_SUCCESS;
286 if (retval == EXIT_FAILURE)
297 const long long nComponents = handler->
nComponents;
300 const unsigned char *srcPtr = imgIn;
301 unsigned char *destPtr = imgOut;
304 for (idx = 0; idx < nPixels; ++idx, ++destPtr, ++srcPtr)
306 *destPtr = (
unsigned char)((*srcPtr) * k);
308 for (idx = 0; idx < handler->
nPoints; ++idx, ++ptrTransform)
310 memcpy(&(imgOut[ptrTransform->
dstOffset]), &(imgIn[ptrTransform->
srcOffset]), nComponents);
void getKaleidoscopeVersion(int *major, int *minor, int *patch)
Get the Kaleidoscope Library version as integer.
void processKaleidoscope(const KaleidoscopeHandle *handler, double k, const unsigned char *imgIn, unsigned char *imgOut)
Applies kaleidoscope effect to image.
char * getKaleidoscopeLibraryInfo()
Get the Kaleidoscope Library info as string.
void sliceTriangle(TransformationInfo *transformPtr, int width, int height, int n, double scaleDown)
Slices a suitable triangle from image.
static int compare(const void *lhsPtr, const void *rhsPtr)
int initKaleidoscope(KaleidoscopeHandle *handler, int n, int width, int height, int nComponents, double scaleDown)
Initializes kaleidoscope handler.
char * getKaleidoscopeVersionString()
Get the Kaleidoscope Library version as string.
void interpolate(TransformationInfo *dataOut, const TransformationInfo *dataIn, int width, int height)
A simple interpolation function. Internal use only.
void rotatePoints(TransformationInfo *outData, const TransformationInfo *orgData, int width, int height, double angle)
Rotates the coordinates of sliced triangle. Internal use only.
void deInitKaleidoscope(KaleidoscopeHandle *handler)
Deinitializes kaleidoscope handler.
Struct for kaleidoscope effect generator.
long long nPoints
Total number of points of transfer function.
struct TransformationInfo_t * pTransferFunc
Transformation info.
unsigned char nComponents
Number of components (eg 3 for RGB)