Skip to content

Commit

Permalink
3.1: threshold: EdgePlus
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Jun 7, 2023
1 parent d52a1a0 commit 141a3fc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/djvul.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#ifndef DJVUL_H_
#define DJVUL_H_
#define DJVUL_VERSION "3.0"
#define DJVUL_VERSION "3.1"

#include <stdbool.h>
#include <math.h>
Expand Down Expand Up @@ -34,6 +34,7 @@ DJVULAPI int ImageDjvulSelect(unsigned char* buf, bool* bufmask, unsigned char*
#define TBIMOD 1
#define TSAUVOLA 2
#define TBLUR 3
#define TEDGEPLUS 4

#ifdef DJVUL_IMPLEMENTATION

Expand Down Expand Up @@ -996,6 +997,10 @@ DJVULAPI int ImageDjvulSelect(unsigned char* buf, bool* bufmask, unsigned char*
(void)ImageThresholdBlur(buf, bufmask, width, height, channels, radius, fbscale, delta, sensitivity);
return ImageDjvulGround(buf, bufmask, bufbg, buffg, width, height, channels, bgs, level, doverlay);
break;
case TEDGEPLUS:
(void)ImageThresholdEdgePlus(buf, bufmask, width, height, channels, radius, fbscale, delta, sensitivity);
return ImageDjvulGround(buf, bufmask, bufbg, buffg, width, height, channels, bgs, level, doverlay);
break;
default:
return ImageDjvulThreshold(buf, bufmask, bufbg, buffg, width, height, channels, bgs, level, wbmode, doverlay, anisotropic, contrast, fbscale, delta);
break;
Expand Down
7 changes: 5 additions & 2 deletions src/stbdjvul.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void djvul_usage(char* prog, unsigned int bgs, unsigned int fgs, unsigned int le
printf(" -o N.N part of overlay blocks (default %f)\n", doverlay);
printf(" -r rewrite maks in ground mode\n");
printf(" -s N.N sensitivity Sauvola and Blur threshold (default %f)\n", sensitivity);
printf(" -t NUM threshold: 0 - DjVuL, 1 - BiMod, 2 - Sauvola, 3 - Blur (default %d)\n", tmode);
printf(" -t NUM threshold: 0 - DjVuL, 1 - BiMod, 2 - Sauvola, 3 - Blur, 4 - EdgePlus (default %d)\n", tmode);
printf(" -w white/black mode (default %d)\n", wbmode);
printf(" -h show this help message and exit\n");
}
Expand Down Expand Up @@ -116,7 +116,7 @@ int main(int argc, char **argv)
break;
case 't':
tmode = atoi(optarg);
if ((tmode < 0) || (tmode > 3))
if ((tmode < 0) || (tmode > 4))
{
fprintf(stderr, "Bad argument\n");
fprintf(stderr, "threshold = %d\n", tmode);
Expand Down Expand Up @@ -376,6 +376,9 @@ int main(int argc, char **argv)
case TBLUR:
printf("threshold: Blur\n");
break;
case TEDGEPLUS:
printf("threshold: EdgePlus\n");
break;
default:
printf("threshold: DjVuL\n");
break;
Expand Down
61 changes: 60 additions & 1 deletion src/threshold.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#ifndef THRESHOLD_H_
#define THRESHOLD_H_
#define THRESHOLD_VERSION "3.0"
#define THRESHOLD_VERSION "3.1"

#include <stdbool.h>
#include <math.h>
Expand All @@ -23,6 +23,7 @@ THRESHOLDAPI int ImageThreshold(unsigned char* buf, bool* bufmask, unsigned int
THRESHOLDAPI int ImageThresholdBimod(unsigned char* buf, bool* bufmask, unsigned int width, unsigned int height, unsigned int channels, float part, float delta);
THRESHOLDAPI int ImageThresholdSauvola(unsigned char* buf, bool* bufmask, unsigned int width, unsigned int height, unsigned int channels, int radius, float sensitivity, float part, int lower_bound, int upper_bound, float delta);
THRESHOLDAPI int ImageThresholdBlur(unsigned char* buf, bool* bufmask, unsigned int width, unsigned int height, unsigned int channels, float raduis, float part, float delta, float sensitivity);
THRESHOLDAPI int ImageThresholdEdgePlus(unsigned char* buf, bool* bufmask, unsigned int width, unsigned int height, unsigned int channels, float raduis, float part, float delta, float sensitivity);

#ifdef __cplusplus
}
Expand Down Expand Up @@ -638,6 +639,64 @@ THRESHOLDAPI int ImageThresholdBlur(unsigned char* buf, bool* bufmask, unsigned
return threshold;
}

/*
ImageThresholdEdgePlus()
input:
buf - unsigned char* image (height * width * channels)
part = 1.0f [1:1]
delta = 0.0f [off, regulator]
sensitivity = 0.2;
output:
bufmask - bool* image mask (height * width)
threshold - threshold value
Use:
int threshold = ImageThresholdEdgePlus(buf, bufmask, width, height, channels, raduis, part, delta, sensitivity);
*/

THRESHOLDAPI int ImageThresholdEdgePlus(unsigned char* buf, bool* bufmask, unsigned int width, unsigned int height, unsigned int channels, float raduis, float part, float delta, float sensitivity)
{
int threshold = 0;
unsigned int y, x, d;
float imo, imx, edge, edgeplus;
unsigned long int k;
unsigned char *bufb = NULL;

if ((bufb = (unsigned char*)malloc(height * width * channels * sizeof(unsigned char))))
{
ImageCopy(buf, bufb, width, height, channels);
(void)GaussBlurFilter(bufb, width, height, channels, raduis, raduis);
k = 0;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
for (d = 0; d < channels; d++)
{
imo = (float)buf[k];
imx = (float)bufb[k];
edge = (imo + 1.0f) / (imx + 1.0f) - 0.5f;
edgeplus = imo * edge;
imx = sensitivity * edgeplus + (1.0f - sensitivity) * imo;
imx = (imx < 0.0f) ? 0.0f : (imx < 255.0f) ? imx : 255.0f;
bufb[k] = (unsigned char)imx;
k++;
}
}
}
threshold = ImageThresholdBimod(bufb, bufmask, width, height, channels, part, delta);
free(bufb);
}
else
{
threshold = ImageThresholdBimod(buf, bufmask, width, height, channels, part, delta);
}

return threshold;
}

#endif /* THRESHOLD_IMPLEMENTATION */

#endif /* THRESHOLD_H_ */

1 comment on commit 141a3fc

@zvezdochiot
Copy link
Member Author

@zvezdochiot zvezdochiot commented on 141a3fc Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @plzombie .

ℹ️ Добавил любопытный во всех смыслах слова порог. Описание см. в ScanTailor-Advanced/scantailor-advanced#48 .

Please sign in to comment.