account for pixel size in imaging filters

This commit is contained in:
Yay295 2022-08-25 19:45:13 -05:00
parent 632b96b2c2
commit 25df706e67

View File

@ -27,7 +27,7 @@
#include "Imaging.h" #include "Imaging.h"
static inline UINT8 static inline UINT8
clip8(float in) { clip8(const float in) {
if (in <= 0.0) { if (in <= 0.0) {
return 0; return 0;
} }
@ -99,12 +99,13 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
(_i2f((UINT8)in0[x - d]) * (kernel)[0] + _i2f((UINT8)in0[x]) * (kernel)[1] + \ (_i2f((UINT8)in0[x - d]) * (kernel)[0] + _i2f((UINT8)in0[x]) * (kernel)[1] + \
_i2f((UINT8)in0[x + d]) * (kernel)[2]) _i2f((UINT8)in0[x + d]) * (kernel)[2])
// Add one time for rounding
offset += 0.5;
int x = 0, y = 0; int x = 0, y = 0;
memcpy(imOut->image[0], im->image[0], im->linesize); memcpy(imOut->image[0], im->image[0], im->linesize);
if (im->bands == 1) { if (im->pixelsize == 1) {
// Add one time for rounding
offset += 0.5;
for (y = 1; y < im->ysize - 1; y++) { for (y = 1; y < im->ysize - 1; y++) {
const UINT8 *in_1 = (UINT8 *)im->image[y - 1]; const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
const UINT8 *in0 = (UINT8 *)im->image[y]; const UINT8 *in0 = (UINT8 *)im->image[y];
@ -121,9 +122,30 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
} }
out[x] = in0[x]; out[x] = in0[x];
} }
} else { } else if (im->pixelsize == 2) {
// Add one time for rounding for (y = 1; y < im->ysize - 1; y++) {
offset += 0.5; const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
const UINT8 *in0 = (UINT8 *)im->image[y];
const UINT8 *in1 = (UINT8 *)im->image[y + 1];
UINT8 *out = (UINT8 *)imOut->image[y];
memcpy(out, in0, 2);
for (x = 1; x < im->xsize - 1; x++) {
float ss0 = offset;
float ss1 = offset;
ss0 += KERNEL1x3(in1, x * 2 + 0, &kernel[0], 2);
ss1 += KERNEL1x3(in1, x * 2 + 1, &kernel[0], 2);
ss0 += KERNEL1x3(in0, x * 2 + 0, &kernel[3], 2);
ss1 += KERNEL1x3(in0, x * 2 + 1, &kernel[3], 2);
ss0 += KERNEL1x3(in_1, x * 2 + 0, &kernel[6], 2);
ss1 += KERNEL1x3(in_1, x * 2 + 1, &kernel[6], 2);
out[x * 2 + 0] = clip8(ss0);
out[x * 2 + 1] = clip8(ss1);
}
memcpy(out + x * 2, in0 + x * 2, 2);
}
} else if (im->pixelsize == 4) {
if (im->bands == 3) {
for (y = 1; y < im->ysize - 1; y++) { for (y = 1; y < im->ysize - 1; y++) {
const UINT8 *in_1 = (UINT8 *)im->image[y - 1]; const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
const UINT8 *in0 = (UINT8 *)im->image[y]; const UINT8 *in0 = (UINT8 *)im->image[y];
@ -131,26 +153,10 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
UINT8 *out = (UINT8 *)imOut->image[y]; UINT8 *out = (UINT8 *)imOut->image[y];
memcpy(out, in0, sizeof(UINT32)); memcpy(out, in0, sizeof(UINT32));
if (im->bands == 2) {
for (x = 1; x < im->xsize - 1; x++) {
float ss0 = offset;
float ss3 = offset;
UINT32 v;
ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
ss3 += KERNEL1x3(in1, x * 4 + 3, &kernel[0], 4);
ss0 += KERNEL1x3(in0, x * 4 + 0, &kernel[3], 4);
ss3 += KERNEL1x3(in0, x * 4 + 3, &kernel[3], 4);
ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
ss3 += KERNEL1x3(in_1, x * 4 + 3, &kernel[6], 4);
v = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3));
memcpy(out + x * sizeof(v), &v, sizeof(v));
}
} else if (im->bands == 3) {
for (x = 1; x < im->xsize - 1; x++) { for (x = 1; x < im->xsize - 1; x++) {
float ss0 = offset; float ss0 = offset;
float ss1 = offset; float ss1 = offset;
float ss2 = offset; float ss2 = offset;
UINT32 v;
ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4); ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
ss1 += KERNEL1x3(in1, x * 4 + 1, &kernel[0], 4); ss1 += KERNEL1x3(in1, x * 4 + 1, &kernel[0], 4);
ss2 += KERNEL1x3(in1, x * 4 + 2, &kernel[0], 4); ss2 += KERNEL1x3(in1, x * 4 + 2, &kernel[0], 4);
@ -160,34 +166,42 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4); ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
ss1 += KERNEL1x3(in_1, x * 4 + 1, &kernel[6], 4); ss1 += KERNEL1x3(in_1, x * 4 + 1, &kernel[6], 4);
ss2 += KERNEL1x3(in_1, x * 4 + 2, &kernel[6], 4); ss2 += KERNEL1x3(in_1, x * 4 + 2, &kernel[6], 4);
v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0); const UINT32 v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0);
memcpy(out + x * sizeof(v), &v, sizeof(v)); memcpy(out + x * sizeof(v), &v, sizeof(v));
} }
} else if (im->bands == 4) {
for (x = 1; x < im->xsize - 1; x++) {
float ss0 = offset;
float ss1 = offset;
float ss2 = offset;
float ss3 = offset;
UINT32 v;
ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
ss1 += KERNEL1x3(in1, x * 4 + 1, &kernel[0], 4);
ss2 += KERNEL1x3(in1, x * 4 + 2, &kernel[0], 4);
ss3 += KERNEL1x3(in1, x * 4 + 3, &kernel[0], 4);
ss0 += KERNEL1x3(in0, x * 4 + 0, &kernel[3], 4);
ss1 += KERNEL1x3(in0, x * 4 + 1, &kernel[3], 4);
ss2 += KERNEL1x3(in0, x * 4 + 2, &kernel[3], 4);
ss3 += KERNEL1x3(in0, x * 4 + 3, &kernel[3], 4);
ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
ss1 += KERNEL1x3(in_1, x * 4 + 1, &kernel[6], 4);
ss2 += KERNEL1x3(in_1, x * 4 + 2, &kernel[6], 4);
ss3 += KERNEL1x3(in_1, x * 4 + 3, &kernel[6], 4);
v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
memcpy(out + x * sizeof(v), &v, sizeof(v));
}
}
memcpy(out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32)); memcpy(out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32));
} }
} else if (im->bands == 4) {
for (y = 1; y < im->ysize - 1; y++) {
const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
const UINT8 *in0 = (UINT8 *)im->image[y];
const UINT8 *in1 = (UINT8 *)im->image[y + 1];
UINT8 *out = (UINT8 *)imOut->image[y];
memcpy(out, in0, sizeof(UINT32));
for (x = 1; x < im->xsize - 1; x++) {
float ss0 = offset;
float ss1 = offset;
float ss2 = offset;
float ss3 = offset;
ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
ss1 += KERNEL1x3(in1, x * 4 + 1, &kernel[0], 4);
ss2 += KERNEL1x3(in1, x * 4 + 2, &kernel[0], 4);
ss3 += KERNEL1x3(in1, x * 4 + 3, &kernel[0], 4);
ss0 += KERNEL1x3(in0, x * 4 + 0, &kernel[3], 4);
ss1 += KERNEL1x3(in0, x * 4 + 1, &kernel[3], 4);
ss2 += KERNEL1x3(in0, x * 4 + 2, &kernel[3], 4);
ss3 += KERNEL1x3(in0, x * 4 + 3, &kernel[3], 4);
ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
ss1 += KERNEL1x3(in_1, x * 4 + 1, &kernel[6], 4);
ss2 += KERNEL1x3(in_1, x * 4 + 2, &kernel[6], 4);
ss3 += KERNEL1x3(in_1, x * 4 + 3, &kernel[6], 4);
const UINT32 v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
memcpy(out + x * sizeof(v), &v, sizeof(v));
}
memcpy(out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32));
}
}
} }
memcpy(imOut->image[y], im->image[y], im->linesize); memcpy(imOut->image[y], im->image[y], im->linesize);
} }
@ -200,13 +214,14 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
_i2f((UINT8)in0[x + d]) * (kernel)[3] + \ _i2f((UINT8)in0[x + d]) * (kernel)[3] + \
_i2f((UINT8)in0[x + d + d]) * (kernel)[4]) _i2f((UINT8)in0[x + d + d]) * (kernel)[4])
// Add one time for rounding
offset += 0.5;
int x = 0, y = 0; int x = 0, y = 0;
memcpy(imOut->image[0], im->image[0], im->linesize); memcpy(imOut->image[0], im->image[0], im->linesize);
memcpy(imOut->image[1], im->image[1], im->linesize); memcpy(imOut->image[1], im->image[1], im->linesize);
if (im->bands == 1) { if (im->pixelsize == 1) {
// Add one time for rounding
offset += 0.5;
for (y = 2; y < im->ysize - 2; y++) { for (y = 2; y < im->ysize - 2; y++) {
const UINT8 *in_2 = (UINT8 *)im->image[y - 2]; const UINT8 *in_2 = (UINT8 *)im->image[y - 2];
const UINT8 *in_1 = (UINT8 *)im->image[y - 1]; const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
@ -229,9 +244,36 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
out[x + 0] = in0[x + 0]; out[x + 0] = in0[x + 0];
out[x + 1] = in0[x + 1]; out[x + 1] = in0[x + 1];
} }
} else { } else if (im->pixelsize == 2) {
// Add one time for rounding for (y = 2; y < im->ysize - 2; y++) {
offset += 0.5; const UINT8 *in_2 = (UINT8 *)im->image[y - 2];
const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
const UINT8 *in0 = (UINT8 *)im->image[y];
const UINT8 *in1 = (UINT8 *)im->image[y + 1];
const UINT8 *in2 = (UINT8 *)im->image[y + 2];
UINT8 *out = (UINT8 *)imOut->image[y];
memcpy(out, in0, 2 * 2);
for (x = 2; x < im->xsize - 2; x++) {
float ss0 = offset;
float ss1 = offset;
ss0 += KERNEL1x5(in2, x * 2 + 0, &kernel[0], 2);
ss1 += KERNEL1x5(in2, x * 2 + 1, &kernel[0], 2);
ss0 += KERNEL1x5(in1, x * 2 + 0, &kernel[5], 2);
ss1 += KERNEL1x5(in1, x * 2 + 1, &kernel[5], 2);
ss0 += KERNEL1x5(in0, x * 2 + 0, &kernel[10], 2);
ss1 += KERNEL1x5(in0, x * 2 + 1, &kernel[10], 2);
ss0 += KERNEL1x5(in_1, x * 2 + 0, &kernel[15], 2);
ss1 += KERNEL1x5(in_1, x * 2 + 1, &kernel[15], 2);
ss0 += KERNEL1x5(in_2, x * 2 + 0, &kernel[20], 2);
ss1 += KERNEL1x5(in_2, x * 2 + 1, &kernel[20], 2);
out[x * 2 + 0] = clip8(ss0);
out[x * 2 + 1] = clip8(ss1);
}
memcpy(out + x * 2, in0 + x * 2, 2 * 2);
}
} else if (im->pixelsize == 4) {
if (im->bands == 3) {
for (y = 2; y < im->ysize - 2; y++) { for (y = 2; y < im->ysize - 2; y++) {
const UINT8 *in_2 = (UINT8 *)im->image[y - 2]; const UINT8 *in_2 = (UINT8 *)im->image[y - 2];
const UINT8 *in_1 = (UINT8 *)im->image[y - 1]; const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
@ -241,30 +283,10 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
UINT8 *out = (UINT8 *)imOut->image[y]; UINT8 *out = (UINT8 *)imOut->image[y];
memcpy(out, in0, sizeof(UINT32) * 2); memcpy(out, in0, sizeof(UINT32) * 2);
if (im->bands == 2) {
for (x = 2; x < im->xsize - 2; x++) {
float ss0 = offset;
float ss3 = offset;
UINT32 v;
ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4);
ss3 += KERNEL1x5(in2, x * 4 + 3, &kernel[0], 4);
ss0 += KERNEL1x5(in1, x * 4 + 0, &kernel[5], 4);
ss3 += KERNEL1x5(in1, x * 4 + 3, &kernel[5], 4);
ss0 += KERNEL1x5(in0, x * 4 + 0, &kernel[10], 4);
ss3 += KERNEL1x5(in0, x * 4 + 3, &kernel[10], 4);
ss0 += KERNEL1x5(in_1, x * 4 + 0, &kernel[15], 4);
ss3 += KERNEL1x5(in_1, x * 4 + 3, &kernel[15], 4);
ss0 += KERNEL1x5(in_2, x * 4 + 0, &kernel[20], 4);
ss3 += KERNEL1x5(in_2, x * 4 + 3, &kernel[20], 4);
v = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3));
memcpy(out + x * sizeof(v), &v, sizeof(v));
}
} else if (im->bands == 3) {
for (x = 2; x < im->xsize - 2; x++) { for (x = 2; x < im->xsize - 2; x++) {
float ss0 = offset; float ss0 = offset;
float ss1 = offset; float ss1 = offset;
float ss2 = offset; float ss2 = offset;
UINT32 v;
ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4); ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4);
ss1 += KERNEL1x5(in2, x * 4 + 1, &kernel[0], 4); ss1 += KERNEL1x5(in2, x * 4 + 1, &kernel[0], 4);
ss2 += KERNEL1x5(in2, x * 4 + 2, &kernel[0], 4); ss2 += KERNEL1x5(in2, x * 4 + 2, &kernel[0], 4);
@ -280,16 +302,26 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
ss0 += KERNEL1x5(in_2, x * 4 + 0, &kernel[20], 4); ss0 += KERNEL1x5(in_2, x * 4 + 0, &kernel[20], 4);
ss1 += KERNEL1x5(in_2, x * 4 + 1, &kernel[20], 4); ss1 += KERNEL1x5(in_2, x * 4 + 1, &kernel[20], 4);
ss2 += KERNEL1x5(in_2, x * 4 + 2, &kernel[20], 4); ss2 += KERNEL1x5(in_2, x * 4 + 2, &kernel[20], 4);
v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0); const UINT32 v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0);
memcpy(out + x * sizeof(v), &v, sizeof(v)); memcpy(out + x * sizeof(v), &v, sizeof(v));
} }
memcpy(out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32) * 2);
}
} else if (im->bands == 4) { } else if (im->bands == 4) {
for (y = 2; y < im->ysize - 2; y++) {
const UINT8 *in_2 = (UINT8 *)im->image[y - 2];
const UINT8 *in_1 = (UINT8 *)im->image[y - 1];
const UINT8 *in0 = (UINT8 *)im->image[y];
const UINT8 *in1 = (UINT8 *)im->image[y + 1];
const UINT8 *in2 = (UINT8 *)im->image[y + 2];
UINT8 *out = (UINT8 *)imOut->image[y];
memcpy(out, in0, sizeof(UINT32) * 2);
for (x = 2; x < im->xsize - 2; x++) { for (x = 2; x < im->xsize - 2; x++) {
float ss0 = offset; float ss0 = offset;
float ss1 = offset; float ss1 = offset;
float ss2 = offset; float ss2 = offset;
float ss3 = offset; float ss3 = offset;
UINT32 v;
ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4); ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4);
ss1 += KERNEL1x5(in2, x * 4 + 1, &kernel[0], 4); ss1 += KERNEL1x5(in2, x * 4 + 1, &kernel[0], 4);
ss2 += KERNEL1x5(in2, x * 4 + 2, &kernel[0], 4); ss2 += KERNEL1x5(in2, x * 4 + 2, &kernel[0], 4);
@ -310,12 +342,11 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
ss1 += KERNEL1x5(in_2, x * 4 + 1, &kernel[20], 4); ss1 += KERNEL1x5(in_2, x * 4 + 1, &kernel[20], 4);
ss2 += KERNEL1x5(in_2, x * 4 + 2, &kernel[20], 4); ss2 += KERNEL1x5(in_2, x * 4 + 2, &kernel[20], 4);
ss3 += KERNEL1x5(in_2, x * 4 + 3, &kernel[20], 4); ss3 += KERNEL1x5(in_2, x * 4 + 3, &kernel[20], 4);
v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3)); const UINT32 v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
memcpy(out + x * sizeof(v), &v, sizeof(v)); memcpy(out + x * sizeof(v), &v, sizeof(v));
} }
memcpy(out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32) * 2);
} }
memcpy(
out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32) * 2);
} }
} }
memcpy(imOut->image[y], im->image[y], im->linesize); memcpy(imOut->image[y], im->image[y], im->linesize);