-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
106 lines (95 loc) · 2.91 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "browse.h"
#define USE_SHELL_OPEN
#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define TJE_IMPLEMENTATION
#include "tiny_jpeg.h"
#include "Binarization.h"
char saveFile[1024];
// 读取图片
unsigned char *loadImage(const char *filename, int *Width, int *Height, int *Channels) {
return (stbi_load(filename, Width, Height, Channels, 0));
}
// 保存图片
void saveImage(const char *filename, int Width, int Height, int Channels, unsigned char *Output) {
memcpy(saveFile + strlen(saveFile), filename, strlen(filename));
*(saveFile + strlen(saveFile) + 1) = 0;
if (!tje_encode_to_file(saveFile, Width, Height, Channels, true, Output)) {
fprintf(stderr, "save JPEG fail.\n");
return;
}
#ifdef USE_SHELL_OPEN
browse(saveFile);
#endif
}
void splitpath(const char *path, char *drv, char *dir, char *name, char *ext) {
const char *end;
const char *p;
const char *s;
if (path[0] && path[1] == ':') {
if (drv) {
*drv++ = *path++;
*drv++ = *path++;
*drv = '\0';
}
} else if (drv)
*drv = '\0';
for (end = path; *end && *end != ':';) end++;
for (p = end; p > path && *--p != '\\' && *p != '/';)
if (*p == '.') {
end = p;
break;
}
if (ext)
for (s = end; (*ext = *s++);) ext++;
for (p = end; p > path;)
if (*--p == '\\' || *p == '/') {
p++;
break;
}
if (name) {
for (s = p; s < end;) *name++ = *s++;
*name = '\0';
}
if (dir) {
for (s = path; s < p;) *dir++ = *s++;
*dir = '\0';
}
}
void getCurrentFilePath(const char *filePath, char *saveFile) {
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
splitpath(filePath, drive, dir, fname, ext);
size_t n = strlen(filePath);
memcpy(saveFile, filePath, n);
char *cur_saveFile = saveFile + (n - strlen(ext));
cur_saveFile[0] = '_';
cur_saveFile[1] = 0;
}
int main(int argc, char **argv) {
if (argc < 2) {
return (0);
}
char *szfile1 = argv[1];
int width, height, channels;
width = height = channels = 0;
unsigned char *src = nullptr;
src = loadImage(szfile1, &width, &height, &channels);
if (width == 0 || height == 0 || channels == 0 || src == nullptr) {
printf("Image Read Failure\n");
return -1;
}
unsigned char *graysrc = (unsigned char *)malloc(width * height * sizeof(unsigned char));
unsigned char *graydst = (unsigned char *)malloc(width * height * sizeof(unsigned char));
double thresh = 127.0;
ImageToGRAY(src, graysrc, width, height, channels);
ThresholdingTo0(graysrc, graydst, width, height, thresh);
saveImage("done.jpg", width, height, 1, graydst);
delete[] src;
delete[] graysrc;
delete[] graydst;
printf("fulfillment");
}