2014-10-12 16:30:00 +04:00
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
|
2014-10-14 02:12:04 +04:00
|
|
|
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
|
|
|
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
|
|
|
|
|
2014-10-18 18:37:14 +04:00
|
|
|
typedef UINT8 pixel[4];
|
2014-10-12 17:11:24 +04:00
|
|
|
|
2014-12-02 07:40:10 +03:00
|
|
|
void static inline
|
2014-11-21 21:53:13 +03:00
|
|
|
ImagingLineBoxBlur32(pixel *lineOut, pixel *lineIn, int lastx, int radius, int edgeA,
|
2014-10-18 18:37:14 +04:00
|
|
|
int edgeB, UINT32 ww, UINT32 fw)
|
|
|
|
{
|
|
|
|
int x;
|
2014-10-18 04:00:07 +04:00
|
|
|
UINT32 acc[4];
|
|
|
|
UINT32 bulk[4];
|
2014-10-12 17:11:24 +04:00
|
|
|
|
2014-10-30 14:19:12 +03:00
|
|
|
#define MOVE_ACC(acc, subtract, add) \
|
2014-10-30 14:32:41 +03:00
|
|
|
acc[0] += lineIn[add][0] - lineIn[subtract][0]; \
|
|
|
|
acc[1] += lineIn[add][1] - lineIn[subtract][1]; \
|
|
|
|
acc[2] += lineIn[add][2] - lineIn[subtract][2]; \
|
|
|
|
acc[3] += lineIn[add][3] - lineIn[subtract][3];
|
2014-10-12 17:47:00 +04:00
|
|
|
|
2014-10-12 20:01:49 +04:00
|
|
|
#define ADD_FAR(bulk, acc, left, right) \
|
2014-10-30 14:32:41 +03:00
|
|
|
bulk[0] = (acc[0] * ww) + (lineIn[left][0] + lineIn[right][0]) * fw; \
|
|
|
|
bulk[1] = (acc[1] * ww) + (lineIn[left][1] + lineIn[right][1]) * fw; \
|
|
|
|
bulk[2] = (acc[2] * ww) + (lineIn[left][2] + lineIn[right][2]) * fw; \
|
|
|
|
bulk[3] = (acc[3] * ww) + (lineIn[left][3] + lineIn[right][3]) * fw;
|
2014-10-12 20:01:49 +04:00
|
|
|
|
2014-11-21 21:53:13 +03:00
|
|
|
#define SAVE(x, bulk) \
|
|
|
|
lineOut[x][0] = (UINT8)((bulk[0] + (1 << 23)) >> 24); \
|
|
|
|
lineOut[x][1] = (UINT8)((bulk[1] + (1 << 23)) >> 24); \
|
|
|
|
lineOut[x][2] = (UINT8)((bulk[2] + (1 << 23)) >> 24); \
|
|
|
|
lineOut[x][3] = (UINT8)((bulk[3] + (1 << 23)) >> 24);
|
2014-10-12 20:01:49 +04:00
|
|
|
|
2014-10-18 18:37:14 +04:00
|
|
|
/* Compute acc for -1 pixel (outside of image):
|
|
|
|
From "-radius-1" to "-1" get first pixel,
|
|
|
|
then from "0" to "radius-1". */
|
2014-10-30 14:32:41 +03:00
|
|
|
acc[0] = lineIn[0][0] * (radius + 1);
|
|
|
|
acc[1] = lineIn[0][1] * (radius + 1);
|
|
|
|
acc[2] = lineIn[0][2] * (radius + 1);
|
|
|
|
acc[3] = lineIn[0][3] * (radius + 1);
|
2014-10-18 18:37:14 +04:00
|
|
|
/* As radius can be bigger than xsize, iterate to edgeA -1. */
|
|
|
|
for (x = 0; x < edgeA - 1; x++) {
|
2014-10-30 14:32:41 +03:00
|
|
|
acc[0] += lineIn[x][0];
|
|
|
|
acc[1] += lineIn[x][1];
|
|
|
|
acc[2] += lineIn[x][2];
|
|
|
|
acc[3] += lineIn[x][3];
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
/* Then multiply remainder to last x. */
|
2014-10-30 14:32:41 +03:00
|
|
|
acc[0] += lineIn[lastx][0] * (radius - edgeA + 1);
|
|
|
|
acc[1] += lineIn[lastx][1] * (radius - edgeA + 1);
|
|
|
|
acc[2] += lineIn[lastx][2] * (radius - edgeA + 1);
|
|
|
|
acc[3] += lineIn[lastx][3] * (radius - edgeA + 1);
|
2014-10-18 18:37:14 +04:00
|
|
|
|
|
|
|
if (edgeA <= edgeB)
|
|
|
|
{
|
2014-10-30 14:19:12 +03:00
|
|
|
/* Subtract pixel from left ("0").
|
2014-10-18 18:37:14 +04:00
|
|
|
Add pixels from radius. */
|
|
|
|
for (x = 0; x < edgeA; x++) {
|
|
|
|
MOVE_ACC(acc, 0, x + radius);
|
|
|
|
ADD_FAR(bulk, acc, 0, x + radius + 1);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-12 17:47:00 +04:00
|
|
|
}
|
2014-10-30 14:19:12 +03:00
|
|
|
/* Subtract previous pixel from "-radius".
|
2014-10-18 18:37:14 +04:00
|
|
|
Add pixels from radius. */
|
|
|
|
for (x = edgeA; x < edgeB; x++) {
|
|
|
|
MOVE_ACC(acc, x - radius - 1, x + radius);
|
|
|
|
ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-12 17:47:00 +04:00
|
|
|
}
|
2014-10-30 14:19:12 +03:00
|
|
|
/* Subtract previous pixel from "-radius".
|
2014-10-18 18:37:14 +04:00
|
|
|
Add last pixel. */
|
|
|
|
for (x = edgeB; x <= lastx; x++) {
|
|
|
|
MOVE_ACC(acc, x - radius - 1, lastx);
|
|
|
|
ADD_FAR(bulk, acc, x - radius - 1, lastx);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (x = 0; x < edgeB; x++) {
|
|
|
|
MOVE_ACC(acc, 0, x + radius);
|
|
|
|
ADD_FAR(bulk, acc, 0, x + radius + 1);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
for (x = edgeB; x < edgeA; x++) {
|
|
|
|
MOVE_ACC(acc, 0, lastx);
|
|
|
|
ADD_FAR(bulk, acc, 0, lastx);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
for (x = edgeA; x <= lastx; x++) {
|
|
|
|
MOVE_ACC(acc, x - radius - 1, lastx);
|
|
|
|
ADD_FAR(bulk, acc, x - radius - 1, lastx);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-12 17:11:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-18 04:00:07 +04:00
|
|
|
#undef MOVE_ACC
|
|
|
|
#undef ADD_FAR
|
|
|
|
#undef SAVE
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
2014-10-18 04:00:07 +04:00
|
|
|
|
2014-10-18 18:37:14 +04:00
|
|
|
|
2014-12-02 07:40:10 +03:00
|
|
|
void static inline
|
2014-10-30 14:32:41 +03:00
|
|
|
ImagingLineBoxBlur8(UINT8 *lineOut, UINT8 *lineIn, int lastx, int radius, int edgeA,
|
2014-10-18 18:37:14 +04:00
|
|
|
int edgeB, UINT32 ww, UINT32 fw)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
UINT32 acc;
|
|
|
|
UINT32 bulk;
|
|
|
|
|
2014-10-30 14:19:12 +03:00
|
|
|
#define MOVE_ACC(acc, subtract, add) \
|
2014-10-30 14:32:41 +03:00
|
|
|
acc += lineIn[add] - lineIn[subtract];
|
2014-10-18 18:37:14 +04:00
|
|
|
|
|
|
|
#define ADD_FAR(bulk, acc, left, right) \
|
2014-10-30 14:32:41 +03:00
|
|
|
bulk = (acc * ww) + (lineIn[left] + lineIn[right]) * fw;
|
2014-10-18 18:37:14 +04:00
|
|
|
|
2014-11-21 21:53:13 +03:00
|
|
|
#define SAVE(x, bulk) \
|
|
|
|
lineOut[x] = (UINT8)((bulk + (1 << 23)) >> 24)
|
2014-10-18 18:37:14 +04:00
|
|
|
|
2014-10-30 14:32:41 +03:00
|
|
|
acc = lineIn[0] * (radius + 1);
|
2014-10-18 18:37:14 +04:00
|
|
|
for (x = 0; x < edgeA - 1; x++) {
|
2014-10-30 14:32:41 +03:00
|
|
|
acc += lineIn[x];
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
2014-10-30 14:32:41 +03:00
|
|
|
acc += lineIn[lastx] * (radius - edgeA + 1);
|
2014-10-18 18:37:14 +04:00
|
|
|
|
|
|
|
if (edgeA <= edgeB)
|
|
|
|
{
|
|
|
|
for (x = 0; x < edgeA; x++) {
|
|
|
|
MOVE_ACC(acc, 0, x + radius);
|
|
|
|
ADD_FAR(bulk, acc, 0, x + radius + 1);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
for (x = edgeA; x < edgeB; x++) {
|
|
|
|
MOVE_ACC(acc, x - radius - 1, x + radius);
|
|
|
|
ADD_FAR(bulk, acc, x - radius - 1, x + radius + 1);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
for (x = edgeB; x <= lastx; x++) {
|
|
|
|
MOVE_ACC(acc, x - radius - 1, lastx);
|
|
|
|
ADD_FAR(bulk, acc, x - radius - 1, lastx);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (x = 0; x < edgeB; x++) {
|
|
|
|
MOVE_ACC(acc, 0, x + radius);
|
|
|
|
ADD_FAR(bulk, acc, 0, x + radius + 1);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
for (x = edgeB; x < edgeA; x++) {
|
|
|
|
MOVE_ACC(acc, 0, lastx);
|
|
|
|
ADD_FAR(bulk, acc, 0, lastx);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
for (x = edgeA; x <= lastx; x++) {
|
|
|
|
MOVE_ACC(acc, x - radius - 1, lastx);
|
|
|
|
ADD_FAR(bulk, acc, x - radius - 1, lastx);
|
2014-11-21 21:53:13 +03:00
|
|
|
SAVE(x, bulk);
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MOVE_ACC
|
|
|
|
#undef ADD_FAR
|
|
|
|
#undef SAVE
|
2014-10-12 17:11:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-18 18:37:14 +04:00
|
|
|
|
2014-10-12 22:37:25 +04:00
|
|
|
Imaging
|
2014-10-30 14:48:15 +03:00
|
|
|
ImagingHorizontalBoxBlur(Imaging imOut, Imaging imIn, float floatRadius)
|
2014-10-12 22:37:25 +04:00
|
|
|
{
|
|
|
|
ImagingSectionCookie cookie;
|
|
|
|
|
2014-10-19 18:19:56 +04:00
|
|
|
int y;
|
2014-10-12 22:37:25 +04:00
|
|
|
|
|
|
|
int radius = (int) floatRadius;
|
2014-10-18 04:00:07 +04:00
|
|
|
UINT32 ww = (UINT32) (1 << 24) / (floatRadius * 2 + 1);
|
|
|
|
UINT32 fw = ((1 << 24) - (radius * 2 + 1) * ww) / 2;
|
2014-10-12 22:37:25 +04:00
|
|
|
|
2014-10-30 14:32:41 +03:00
|
|
|
int edgeA = MIN(radius + 1, imIn->xsize);
|
|
|
|
int edgeB = MAX(imIn->xsize - radius - 1, 0);
|
2014-10-13 23:45:21 +04:00
|
|
|
|
2014-10-30 14:32:41 +03:00
|
|
|
UINT32 *lineOut = calloc(imIn->xsize, sizeof(UINT32));
|
2020-05-10 12:56:36 +03:00
|
|
|
if (lineOut == NULL) {
|
2014-10-18 18:37:14 +04:00
|
|
|
return ImagingError_MemoryError();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-10-18 04:00:07 +04:00
|
|
|
|
2014-10-18 18:37:14 +04:00
|
|
|
// printf(">>> %d %d %d\n", radius, ww, fw);
|
2014-10-18 04:00:07 +04:00
|
|
|
|
2014-10-12 22:37:25 +04:00
|
|
|
ImagingSectionEnter(&cookie);
|
|
|
|
|
2014-10-30 14:32:41 +03:00
|
|
|
if (imIn->image8)
|
2014-10-18 18:37:14 +04:00
|
|
|
{
|
2014-10-30 14:32:41 +03:00
|
|
|
for (y = 0; y < imIn->ysize; y++) {
|
|
|
|
ImagingLineBoxBlur8(
|
|
|
|
(imIn == imOut ? (UINT8 *) lineOut : imOut->image8[y]),
|
|
|
|
imIn->image8[y],
|
|
|
|
imIn->xsize - 1,
|
2014-10-18 18:37:14 +04:00
|
|
|
radius, edgeA, edgeB,
|
|
|
|
ww, fw
|
|
|
|
);
|
2014-10-30 14:32:41 +03:00
|
|
|
if (imIn == imOut) {
|
2014-10-19 01:14:08 +04:00
|
|
|
// Commit.
|
2014-10-30 14:32:41 +03:00
|
|
|
memcpy(imOut->image8[y], lineOut, imIn->xsize);
|
2014-10-14 02:12:04 +04:00
|
|
|
}
|
2014-10-12 22:37:25 +04:00
|
|
|
}
|
2014-10-18 18:37:14 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-30 14:32:41 +03:00
|
|
|
for (y = 0; y < imIn->ysize; y++) {
|
|
|
|
ImagingLineBoxBlur32(
|
2014-11-21 21:53:13 +03:00
|
|
|
imIn == imOut ? (pixel *) lineOut : (pixel *) imOut->image32[y],
|
2014-10-30 14:32:41 +03:00
|
|
|
(pixel *) imIn->image32[y],
|
|
|
|
imIn->xsize - 1,
|
2014-10-18 18:37:14 +04:00
|
|
|
radius, edgeA, edgeB,
|
|
|
|
ww, fw
|
|
|
|
);
|
2014-10-30 14:32:41 +03:00
|
|
|
if (imIn == imOut) {
|
2014-10-19 01:14:08 +04:00
|
|
|
// Commit.
|
2014-10-30 14:32:41 +03:00
|
|
|
memcpy(imOut->image32[y], lineOut, imIn->xsize * 4);
|
2014-10-14 02:12:04 +04:00
|
|
|
}
|
2014-10-12 22:37:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImagingSectionLeave(&cookie);
|
|
|
|
|
2014-10-18 18:37:14 +04:00
|
|
|
free(lineOut);
|
2014-10-18 04:00:07 +04:00
|
|
|
|
2014-10-12 22:37:25 +04:00
|
|
|
return imOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-12 17:11:24 +04:00
|
|
|
Imaging
|
2014-10-25 04:28:05 +04:00
|
|
|
ImagingBoxBlur(Imaging imOut, Imaging imIn, float radius, int n)
|
2014-10-12 17:11:24 +04:00
|
|
|
{
|
2014-10-19 00:14:26 +04:00
|
|
|
int i;
|
2014-12-16 01:44:12 +03:00
|
|
|
Imaging imTransposed;
|
2014-10-19 00:14:26 +04:00
|
|
|
|
|
|
|
if (n < 1) {
|
|
|
|
return ImagingError_ValueError(
|
|
|
|
"number of passes must be greater than zero"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-25 04:28:05 +04:00
|
|
|
if (strcmp(imIn->mode, imOut->mode) ||
|
|
|
|
imIn->type != imOut->type ||
|
|
|
|
imIn->bands != imOut->bands ||
|
|
|
|
imIn->xsize != imOut->xsize ||
|
2020-05-10 12:56:36 +03:00
|
|
|
imIn->ysize != imOut->ysize) {
|
2014-10-12 22:39:02 +04:00
|
|
|
return ImagingError_Mismatch();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-10-12 22:39:02 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (imIn->type != IMAGING_TYPE_UINT8) {
|
2014-10-12 22:39:02 +04:00
|
|
|
return ImagingError_ModeError();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-10-12 22:39:02 +04:00
|
|
|
|
2014-10-30 14:19:12 +03:00
|
|
|
if (!(strcmp(imIn->mode, "RGB") == 0 ||
|
|
|
|
strcmp(imIn->mode, "RGBA") == 0 ||
|
2016-05-19 19:16:34 +03:00
|
|
|
strcmp(imIn->mode, "RGBa") == 0 ||
|
2014-10-30 14:19:12 +03:00
|
|
|
strcmp(imIn->mode, "RGBX") == 0 ||
|
|
|
|
strcmp(imIn->mode, "CMYK") == 0 ||
|
|
|
|
strcmp(imIn->mode, "L") == 0 ||
|
2016-05-19 19:16:34 +03:00
|
|
|
strcmp(imIn->mode, "LA") == 0 ||
|
2020-05-10 12:56:36 +03:00
|
|
|
strcmp(imIn->mode, "La") == 0)) {
|
2014-10-14 23:29:50 +04:00
|
|
|
return ImagingError_ModeError();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-10-14 23:29:50 +04:00
|
|
|
|
2017-08-06 13:32:46 +03:00
|
|
|
imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!imTransposed) {
|
2014-10-12 17:59:20 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2014-10-12 17:59:20 +04:00
|
|
|
|
2014-10-30 14:48:15 +03:00
|
|
|
/* Apply blur in one dimension.
|
|
|
|
Use imOut as a destination at first pass,
|
|
|
|
then use imOut as a source too. */
|
|
|
|
ImagingHorizontalBoxBlur(imOut, imIn, radius);
|
2014-10-19 00:14:26 +04:00
|
|
|
for (i = 1; i < n; i ++) {
|
2014-10-30 14:48:15 +03:00
|
|
|
ImagingHorizontalBoxBlur(imOut, imOut, radius);
|
2014-10-19 00:14:26 +04:00
|
|
|
}
|
2014-10-30 14:48:15 +03:00
|
|
|
/* Transpose result for blur in another direction. */
|
|
|
|
ImagingTranspose(imTransposed, imOut);
|
2014-10-12 17:59:20 +04:00
|
|
|
|
2014-10-30 14:48:15 +03:00
|
|
|
/* Reuse imTransposed as a source and destination there. */
|
2014-10-19 00:14:26 +04:00
|
|
|
for (i = 0; i < n; i ++) {
|
2014-10-30 14:48:15 +03:00
|
|
|
ImagingHorizontalBoxBlur(imTransposed, imTransposed, radius);
|
2014-10-19 00:14:26 +04:00
|
|
|
}
|
2014-10-30 14:48:15 +03:00
|
|
|
/* Restore original orientation. */
|
|
|
|
ImagingTranspose(imOut, imTransposed);
|
2014-10-12 17:59:20 +04:00
|
|
|
|
2014-10-30 14:48:15 +03:00
|
|
|
ImagingDelete(imTransposed);
|
2014-10-12 17:59:20 +04:00
|
|
|
|
2014-10-12 16:30:00 +04:00
|
|
|
return imOut;
|
|
|
|
}
|
2014-10-25 17:17:53 +04:00
|
|
|
|
|
|
|
|
|
|
|
Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius,
|
|
|
|
int passes)
|
|
|
|
{
|
|
|
|
float sigma2, L, l, a;
|
|
|
|
|
|
|
|
sigma2 = radius * radius / passes;
|
|
|
|
// from http://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf
|
|
|
|
// [7] Box length.
|
|
|
|
L = sqrt(12.0 * sigma2 + 1.0);
|
|
|
|
// [11] Integer part of box radius.
|
|
|
|
l = floor((L - 1.0) / 2.0);
|
|
|
|
// [14], [Fig. 2] Fractional part of box radius.
|
|
|
|
a = (2 * l + 1) * (l * (l + 1) - 3 * sigma2);
|
|
|
|
a /= 6 * (sigma2 - (l + 1) * (l + 1));
|
|
|
|
|
|
|
|
return ImagingBoxBlur(imOut, imIn, l + a, passes);
|
|
|
|
}
|