Leptonica  1.77.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 #include <string.h>
41 #include "allheaders.h"
42 
43 
44 /*------------------------------------------------------------------*
45  * Conversion to 1 bpp *
46  *------------------------------------------------------------------*/
66 l_ok
67 convertFilesTo1bpp(const char *dirin,
68  const char *substr,
69  l_int32 upscaling,
70  l_int32 thresh,
71  l_int32 firstpage,
72  l_int32 npages,
73  const char *dirout,
74  l_int32 outformat)
75 {
76 l_int32 i, nfiles;
77 char buf[512];
78 char *fname, *tail, *basename;
79 PIX *pixs, *pixg1, *pixg2, *pixb;
80 SARRAY *safiles;
81 
82  PROCNAME("convertFilesTo1bpp");
83 
84  if (!dirin)
85  return ERROR_INT("dirin", procName, 1);
86  if (!dirout)
87  return ERROR_INT("dirout", procName, 1);
88  if (upscaling != 1 && upscaling != 2 && upscaling != 4)
89  return ERROR_INT("invalid upscaling factor", procName, 1);
90  if (thresh <= 0) thresh = 180;
91  if (firstpage < 0) firstpage = 0;
92  if (npages < 0) npages = 0;
93  if (outformat != IFF_TIFF_G4)
94  outformat = IFF_PNG;
95 
96  safiles = getSortedPathnamesInDirectory(dirin, substr, firstpage, npages);
97  if (!safiles)
98  return ERROR_INT("safiles not made", procName, 1);
99  if ((nfiles = sarrayGetCount(safiles)) == 0) {
100  sarrayDestroy(&safiles);
101  return ERROR_INT("no matching files in the directory", procName, 1);
102  }
103 
104  for (i = 0; i < nfiles; i++) {
105  fname = sarrayGetString(safiles, i, L_NOCOPY);
106  if ((pixs = pixRead(fname)) == NULL) {
107  L_WARNING("Couldn't read file %s\n", procName, fname);
108  continue;
109  }
110  if (pixGetDepth(pixs) == 32)
111  pixg1 = pixConvertRGBToLuminance(pixs);
112  else
113  pixg1 = pixClone(pixs);
115  if (pixGetDepth(pixg2) == 1) {
116  pixb = pixClone(pixg2);
117  } else {
118  if (upscaling == 1)
119  pixb = pixThresholdToBinary(pixg2, thresh);
120  else if (upscaling == 2)
121  pixb = pixScaleGray2xLIThresh(pixg2, thresh);
122  else /* upscaling == 4 */
123  pixb = pixScaleGray4xLIThresh(pixg2, thresh);
124  }
125  pixDestroy(&pixs);
126  pixDestroy(&pixg1);
127  pixDestroy(&pixg2);
128 
129  splitPathAtDirectory(fname, NULL, &tail);
130  splitPathAtExtension(tail, &basename, NULL);
131  if (outformat == IFF_TIFF_G4) {
132  snprintf(buf, sizeof(buf), "%s/%s.tif", dirout, basename);
133  pixWrite(buf, pixb, IFF_TIFF_G4);
134  } else {
135  snprintf(buf, sizeof(buf), "%s/%s.png", dirout, basename);
136  pixWrite(buf, pixb, IFF_PNG);
137  }
138  pixDestroy(&pixb);
139  LEPT_FREE(tail);
140  LEPT_FREE(basename);
141  }
142 
143  sarrayDestroy(&safiles);
144  return 0;
145 }
PIX * pixConvertRGBToLuminance(PIX *pixs)
pixConvertRGBToLuminance()
Definition: pixconv.c:733
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:322
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
Definition: utils2.c:2607
PIX * pixScaleGray4xLIThresh(PIX *pixs, l_int32 thresh)
pixScaleGray4xLIThresh()
Definition: scale1.c:1127
Definition: pix.h:716
PIX * pixScaleGray2xLIThresh(PIX *pixs, l_int32 thresh)
pixScaleGray2xLIThresh()
Definition: scale1.c:929
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
Definition: grayquant.c:443
Definition: array.h:116
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:681
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:515
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1717
l_ok splitPathAtDirectory(const char *pathname, char **pdir, char **ptail)
splitPathAtDirectory()
Definition: utils2.c:2540
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:621
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:190
Definition: pix.h:134
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:67
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355