-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db8b5b3
commit fd09975
Showing
5 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <math.h> | ||
|
||
float clamp(float low, float value, float high) { | ||
return (value < low) ? low : ((value > high) ? high : value); | ||
} | ||
|
||
int interpolate(const unsigned char *image, int width, int components, float x, float y, int offset) { | ||
int stride = width * components; | ||
float px = x - floor(x); | ||
float py = y - floor(y); | ||
int x1 = floor(x); | ||
int x2 = ceil(x); | ||
int y1 = floor(y); | ||
int y2 = ceil(y); | ||
|
||
float top = (float) image[y1 * stride + x1 * components + offset] * (1.0 - px) + | ||
(float) image[y1 * stride + x2 * components + offset] * px; | ||
float bot = (float) image[y2 * stride + x1 * components + offset] * (1.0 - px) + | ||
(float) image[y2 * stride + x2 * components + offset] * px; | ||
|
||
return (top * (1.0 - py)) + (bot * py); | ||
} | ||
|
||
void defish(const unsigned char *input, unsigned char *output, int width, int height, int components, float strength, float zoom) { | ||
const int cx = width / 2; | ||
const int cy = height / 2; | ||
const float len = sqrt(width * width + height * height); | ||
|
||
for (int y = 0; y < height; y++) { | ||
for (int x = 0; x < width; x++) { | ||
float dx = (cx - x) * zoom; | ||
float dy = (cy - y) * zoom; | ||
float r = sqrt(dx * dx + dy * dy) / len * strength; | ||
float theta = 1.0; | ||
|
||
if (r != 0.0) { | ||
theta = atan(r) / r; | ||
} | ||
|
||
dx = clamp(0.0, (float) width / 2.0 - theta * dx, width); | ||
dy = clamp(0.0, (float) height / 2.0 - theta * dy, height); | ||
|
||
for (int z = 0; z < components; z++) { | ||
output[y * width * 3 + x * 3 + z] = interpolate(input, width, components, dx, dy, z); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Image editing functions | ||
*/ | ||
#ifndef EDIT_H | ||
#define EDIT_H | ||
|
||
/* | ||
Clamp a value between low and high. | ||
*/ | ||
float clamp(float low, float value, float high); | ||
|
||
/* | ||
Bilinear interpolation | ||
*/ | ||
int interpolate(const unsigned char *image, int width, int components, float x, float y, int offset); | ||
|
||
/* | ||
Remove fisheye distortion from an image. The amount of distortion is | ||
controlled by strength, while zoom controls where the image gets | ||
cropped. For example, the Tokina 10-17mm ATX fisheye on a Canon APS-C | ||
body set to 10mm looks good with strength=2.6 and zoom=1.2. | ||
*/ | ||
void defish(const unsigned char *input, unsigned char *output, int width, int height, int components, float strength, float zoom); | ||
|
||
#endif |