Skip to content

Commit d81fd26

Browse files
committed
0.20191016: LibImthreshold: fix Clamp
1 parent 2d44abb commit d81fd26

File tree

7 files changed

+105
-13
lines changed

7 files changed

+105
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CPP = g++
55
CFLAGS = -DUNIX -O2 -Wall -s
66
LIBS = -lfreeimage
77
VER = 0
8-
VERB = 20191013
8+
VERB = 20191016
99
PLIBF = lib$(PNAME).so.$(VER)
1010
PLIBFI = lib$(PNAME)freeimage.so.$(VER)
1111
PLIB = $(PLIBF) $(PLIBFI)

doc/imthreshold/CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.20191016
2+
3+
LibImthreshold: fix Clamp
4+
RIS: add: FRP upscale
5+
16
0.20191013
27

38
RIS: add: GSample upscale

doc/imthreshold/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.20191013
1+
0.20191016

man/man1/imthreshold-shris.1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ imthreshold-shris [options] <input_file> <output_file>
1414
.TP
1515
-f str
1616
name filter:
17-
'hris'
17+
'hris' (default)
1818
'gsample'
19+
'frp' (long!)
1920
.TP
2021
-m
2122
mode (int, optional, default = 2, {2,{3(hris),all(gsample)}})

src/imthreshold.c

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ double RADGRD = 57.295779513082320876798154814105;
1414

1515
BYTE ByteClamp(int c)
1616
{
17-
BYTE buff[3] = {0, (BYTE)c, 255};
18-
return buff[ (int)(c > 0) + (int)(c > 255) ];
17+
BYTE buff[3] = {(BYTE)c, 255, 0};
18+
return buff[ (int)(c < 0) + (int)((unsigned)c > 255) ];
1919
}
2020

2121
////////////////////////////////////////////////////////////////////////////////
2222

2323
WORD Byte3Clamp(int c)
2424
{
25-
WORD buff[3] = {0, (WORD)c, 765};
26-
return buff[ (int)(c > 0) + (int)(c > 765) ];
25+
WORD buff[3] = {(WORD)c, 765, 0};
26+
return buff[ (int)(c < 0) + (int)((unsigned)c > 765) ];
2727
}
2828

2929
////////////////////////////////////////////////////////////////////////////////
3030

3131
unsigned IndexClamp(int i, unsigned threshold)
3232
{
33-
unsigned buff[3] = {0, (unsigned)i, threshold};
34-
return buff[ (int)(i > 0) + (int)(i > threshold) ];
33+
unsigned buff[3] = {(unsigned)i, threshold, 0};
34+
return buff[ (int)(i < 0) + (int)((unsigned)i > threshold) ];
3535
}
3636

3737
////////////////////////////////////////////////////////////////////////////////
@@ -6339,6 +6339,89 @@ void IMTFilterSNearest (IMTpixel** p_im, IMTpixel** d_im, unsigned height, unsig
63396339

63406340
////////////////////////////////////////////////////////////////////////////////
63416341

6342+
void IMTFilterSFRP (IMTpixel** p_im, IMTpixel** d_im, unsigned height, unsigned width, int smode)
6343+
{
6344+
unsigned y, x, yf, xf, ys, xs, ysf, xsf, ym, xm, yr, xr, d, k, l, bpp = 3, r = 1, rd = (2 * r + 1);
6345+
unsigned widths, heights, imdm, imdt, imdms = (512 * bpp * rd * rd);
6346+
int im, ims, imd;
6347+
IMTpixel **s_im, **r_im;
6348+
6349+
if (smode < 2) {smode = 2;}
6350+
heights = (height + smode - 1) / smode;
6351+
widths = (width + smode - 1) / smode;
6352+
s_im = IMTalloc(heights, widths);
6353+
r_im = IMTalloc(rd, rd);
6354+
IMTFilterSReduce(p_im, s_im, height, width, smode);
6355+
6356+
for (y = 0; y < height; y++)
6357+
{
6358+
yr = y * smode;
6359+
for (x = 0; x < width; x++)
6360+
{
6361+
xr = x * smode;
6362+
for (k = 0; k < rd; k++)
6363+
{
6364+
yf = IndexClamp(((int)(y + k) - r), (height - 1));
6365+
for (l = 0; l < rd; l++)
6366+
{
6367+
xf = IndexClamp(((int)(x + l) - r), (width - 1));
6368+
r_im[k][l] = p_im[yf][xf];
6369+
}
6370+
}
6371+
ym = 0;
6372+
xm = 0;
6373+
imdm = imdms;
6374+
for (ys = 0; ys < heights; ys++)
6375+
{
6376+
for (xs = 0; xs < widths; xs++)
6377+
{
6378+
imdt = 0;
6379+
for (k = 0; k < rd; k++)
6380+
{
6381+
ysf = IndexClamp(((int)(ys + k) - r), (heights - 1));
6382+
for (l = 0; l < rd; l++)
6383+
{
6384+
xsf = IndexClamp(((int)(xs + l) - r), (widths - 1));
6385+
im = r_im[k][l].s;
6386+
ims = s_im[ysf][xsf].s;
6387+
imd = (im > ims) ? (im - ims) : (ims - im);
6388+
imdt += imd;
6389+
for (d = 0; d < bpp; d++)
6390+
{
6391+
im = r_im[k][l].c[d];
6392+
ims = s_im[ysf][xsf].c[d];
6393+
imd = (im > ims) ? (im - ims) : (ims - im);
6394+
imdt += imd;
6395+
}
6396+
}
6397+
}
6398+
if (imdt < imdm)
6399+
{
6400+
ym = ys;
6401+
xm = xs;
6402+
imdm = imdt;
6403+
}
6404+
}
6405+
}
6406+
ym *= smode;
6407+
xm *= smode;
6408+
for (k = 0; k < smode; k++)
6409+
{
6410+
yf = IndexClamp((ym + k), (height - 1));
6411+
for (l = 0; l < smode; l++)
6412+
{
6413+
xf = IndexClamp((xm + l), (width - 1));
6414+
d_im[yr + k][xr + l] = p_im[yf][xf];
6415+
}
6416+
}
6417+
}
6418+
}
6419+
IMTfree(r_im, rd);
6420+
IMTfree(s_im, heights);
6421+
}
6422+
6423+
////////////////////////////////////////////////////////////////////////////////
6424+
63426425
int IMTFilterThreshold (IMTpixel** p_im, BYTE** d_im, unsigned height, unsigned width, int threshold)
63436426
{
63446427
unsigned y, x;

src/imthreshold.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ void IMTFilterSHRIS (IMTpixel**, IMTpixel**, unsigned, unsigned, int);
177177
void IMTFilterSGSampleUp (IMTpixel**, IMTpixel**, unsigned, unsigned, int);
178178
void IMTFilterSReduce (IMTpixel**, IMTpixel**, unsigned, unsigned, unsigned);
179179
void IMTFilterSNearest (IMTpixel**, IMTpixel**, unsigned, unsigned, unsigned, unsigned);
180+
void IMTFilterSFRP (IMTpixel** p_im, IMTpixel** d_im, unsigned height, unsigned width, int smode);
180181
int IMTFilterThreshold (IMTpixel**, BYTE**, unsigned, unsigned, int);
181182
int IMTFilterThresholdLayer (IMTpixel**, WORD**, BYTE**, unsigned, unsigned);
182183
void IMTFilterTLayerToImg (WORD**, IMTpixel**, unsigned, unsigned);

src/shris.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ void ImthresholdFilterSHRISUsage()
2424
printf("Usage : imthreshold-shris [options] <input_file> <output_file>\n\n");
2525
printf("options:\n");
2626
printf(" -f str name filter:\n");
27-
printf(" 'hris'\n");
27+
printf(" 'hris' (default)\n");
2828
printf(" 'gsample'\n");
29+
printf(" 'frp' (long!)\n");
2930
printf(" -m mode (int, optional, default = 2, {2,3})\n");
3031
printf(" -r reduce scale (bool, optional)\n");
3132
printf(" -h this help\n");
@@ -82,7 +83,7 @@ int main(int argc, char *argv[])
8283
const char *output_filename = argv[optind + 1];
8384

8485
if (smode < 2) {smode = 2;}
85-
if (strcmp(namefilter, "hris") == 0)
86+
if ((strcmp(namefilter, "gsample") > 0) && (strcmp(namefilter, "frp") > 0) && !(reduce))
8687
{
8788
if (smode > 3) {smode = 3;}
8889
}
@@ -101,8 +102,6 @@ int main(int argc, char *argv[])
101102
unsigned width2;
102103
unsigned height2;
103104
printf("Mode= %d\n", smode);
104-
width2 = width * smode;
105-
height2 = height * smode;
106105
if (reduce > 0)
107106
{
108107
width2 = (width + smode - 1) / smode;
@@ -127,6 +126,9 @@ int main(int argc, char *argv[])
127126
{
128127
printf("Scale= Up GSample.\n");
129128
IMTFilterSGSampleUp(p_im, d_im, height, width, smode);
129+
} else if (strcmp(namefilter, "frp") == 0) {
130+
printf("Scale= Up FRP.\n");
131+
IMTFilterSFRP(p_im, d_im, height, width, smode);
130132
} else {
131133
printf("Scale= Up HRIS.\n");
132134
IMTFilterSHRIS(p_im, d_im, height, width, smode);

0 commit comments

Comments
 (0)