Leptonica 1.82.0
Image processing and image analysis suite
convertfiles.c
Go to the documentation of this file.
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
40#ifdef HAVE_CONFIG_H
41#include <config_auto.h>
42#endif /* HAVE_CONFIG_H */
43
44#include <string.h>
45#include "allheaders.h"
46
47/*------------------------------------------------------------------*
48 * Conversion to 1 bpp *
49 *------------------------------------------------------------------*/
70l_ok
71convertFilesTo1bpp(const char *dirin,
72 const char *substr,
73 l_int32 upscaling,
74 l_int32 thresh,
75 l_int32 firstpage,
76 l_int32 npages,
77 const char *dirout,
78 l_int32 outformat)
79{
80l_int32 i, nfiles;
81char buf[512];
82char *fname, *tail, *basename;
83PIX *pixs, *pixg1, *pixg2, *pixb;
84SARRAY *safiles;
85
86 PROCNAME("convertFilesTo1bpp");
87
88 if (!dirin)
89 return ERROR_INT("dirin", procName, 1);
90 if (!dirout)
91 return ERROR_INT("dirout", procName, 1);
92 if (upscaling != 1 && upscaling != 2 && upscaling != 4)
93 return ERROR_INT("invalid upscaling factor", procName, 1);
94 if (thresh <= 0) thresh = 180;
95 if (firstpage < 0) firstpage = 0;
96 if (npages < 0) npages = 0;
97 if (outformat != IFF_TIFF_G4)
98 outformat = IFF_PNG;
99
100 safiles = getSortedPathnamesInDirectory(dirin, substr, firstpage, npages);
101 if (!safiles)
102 return ERROR_INT("safiles not made", procName, 1);
103 if ((nfiles = sarrayGetCount(safiles)) == 0) {
104 sarrayDestroy(&safiles);
105 return ERROR_INT("no matching files in the directory", procName, 1);
106 }
107
108 for (i = 0; i < nfiles; i++) {
109 fname = sarrayGetString(safiles, i, L_NOCOPY);
110 if ((pixs = pixRead(fname)) == NULL) {
111 L_WARNING("Couldn't read file %s\n", procName, fname);
112 continue;
113 }
114 if (pixGetDepth(pixs) == 32)
115 pixg1 = pixConvertRGBToLuminance(pixs);
116 else
117 pixg1 = pixClone(pixs);
119 if (pixGetDepth(pixg2) == 1) {
120 pixb = pixClone(pixg2);
121 } else {
122 if (upscaling == 1)
123 pixb = pixThresholdToBinary(pixg2, thresh);
124 else if (upscaling == 2)
125 pixb = pixScaleGray2xLIThresh(pixg2, thresh);
126 else /* upscaling == 4 */
127 pixb = pixScaleGray4xLIThresh(pixg2, thresh);
128 }
129 pixDestroy(&pixs);
130 pixDestroy(&pixg1);
131 pixDestroy(&pixg2);
132
133 splitPathAtDirectory(fname, NULL, &tail);
134 splitPathAtExtension(tail, &basename, NULL);
135 if (outformat == IFF_TIFF_G4) {
136 snprintf(buf, sizeof(buf), "%s/%s.tif", dirout, basename);
137 pixWrite(buf, pixb, IFF_TIFF_G4);
138 } else {
139 snprintf(buf, sizeof(buf), "%s/%s.png", dirout, basename);
140 pixWrite(buf, pixb, IFF_PNG);
141 }
142 pixDestroy(&pixb);
143 LEPT_FREE(tail);
144 LEPT_FREE(basename);
145 }
146
147 sarrayDestroy(&safiles);
148 return 0;
149}
l_ok convertFilesTo1bpp(const char *dirin, const char *substr, l_int32 upscaling, l_int32 thresh, l_int32 firstpage, l_int32 npages, const char *dirout, l_int32 outformat)
convertFilesTo1bpp()
Definition: convertfiles.c:71
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
Definition: grayquant.c:447
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:621
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:593
@ REMOVE_CMAP_TO_GRAYSCALE
Definition: pix.h:257
@ L_NOCOPY
Definition: pix.h:710
PIX * pixConvertRGBToLuminance(PIX *pixs)
pixConvertRGBToLuminance()
Definition: pixconv.c:742
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:328
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:193
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1848
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:703
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:643
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:362
PIX * pixScaleGray2xLIThresh(PIX *pixs, l_int32 thresh)
pixScaleGray2xLIThresh()
Definition: scale1.c:927
PIX * pixScaleGray4xLIThresh(PIX *pixs, l_int32 thresh)
pixScaleGray4xLIThresh()
Definition: scale1.c:1125
Definition: pix.h:139
Definition: array.h:127
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
Definition: utils2.c:2894
l_ok splitPathAtDirectory(const char *pathname, char **pdir, char **ptail)
splitPathAtDirectory()
Definition: utils2.c:2824