1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 /*
28 * binarizefiles.c
29 *
30 * Program that optionally scales and then binarizes a set of files,
31 * writing them to the specified directory in tiff-g4 format.
32 * The resolution is preserved.
33 */
34
35 #include "string.h"
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include "allheaders.h"
39
main(int argc,char ** argv)40 l_int32 main(int argc,
41 char **argv)
42 {
43 char buf[256], dirname[256];
44 char *dirin, *pattern, *subdirout, *fname, *tail, *basename;
45 l_int32 thresh, i, n;
46 l_float32 scalefactor;
47 PIX *pix1, *pix2, *pix3, *pix4;
48 SARRAY *sa;
49 static char mainName[] = "binarizefiles.c";
50
51 if (argc != 6) {
52 fprintf(stderr,
53 "Syntax: binarizefiles dirin pattern thresh scalefact dirout\n"
54 " dirin: input directory for image files\n"
55 " pattern: use 'allfiles' to convert all files\n"
56 " in the directory\n"
57 " thresh: 0 for adaptive; > 0 for global thresh (e.g., 128)\n"
58 " scalefactor: in (0.0 ... 4.0]; use 1.0 to prevent scaling\n"
59 " subdirout: subdirectory of /tmp for output files\n");
60 return 1;
61 }
62 dirin = argv[1];
63 pattern = argv[2];
64 thresh = atoi(argv[3]);
65 scalefactor = atof(argv[4]);
66 subdirout = argv[5];
67 if (!strcmp(pattern, "allfiles"))
68 pattern = NULL;
69 if (scalefactor <= 0.0 || scalefactor > 4.0) {
70 L_WARNING("invalid scalefactor: setting to 1.0\n", mainName);
71 scalefactor = 1.0;
72 }
73
74 setLeptDebugOK(1);
75
76 /* Get the input filenames */
77 sa = getSortedPathnamesInDirectory(dirin, pattern, 0, 0);
78 sarrayWriteStream(stderr, sa);
79 n = sarrayGetCount(sa);
80
81 /* Write the output files */
82 makeTempDirname(dirname, 256, subdirout);
83 fprintf(stderr, "dirname: %s\n", dirname);
84 lept_mkdir(subdirout);
85 for (i = 0; i < n; i++) {
86 fname = sarrayGetString(sa, i, L_NOCOPY);
87 if ((pix1 = pixRead(fname)) == NULL) {
88 L_ERROR("file %s not read as image", mainName, fname);
89 continue;
90 }
91 splitPathAtDirectory(fname, NULL, &tail);
92 splitPathAtExtension(tail, &basename, NULL);
93 snprintf(buf, sizeof(buf), "%s/%s.tif", dirname, basename);
94 lept_free(tail);
95 lept_free(basename);
96 fprintf(stderr, "fileout: %s\n", buf);
97 if (scalefactor != 1.0)
98 pix2 = pixScale(pix1, scalefactor, scalefactor);
99 else
100 pix2 = pixClone(pix1);
101 if (thresh == 0) {
102 pix4 = pixConvertTo8(pix2, 0);
103 pix3 = pixAdaptThresholdToBinary(pix4, NULL, 1.0);
104 pixDestroy(&pix4);
105 } else {
106 pix3 = pixConvertTo1(pix2, thresh);
107 }
108 pixWrite(buf, pix3, IFF_TIFF_G4);
109 pixDestroy(&pix1);
110 pixDestroy(&pix2);
111 pixDestroy(&pix3);
112 }
113 sarrayDestroy(&sa);
114 return 0;
115 }
116
117