Leptonica  1.83.1
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  *------------------------------------------------------------------*/
70 l_ok
71 convertFilesTo1bpp(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 {
80 l_int32 i, nfiles;
81 char buf[512];
82 char *fname, *tail, *basename;
83 PIX *pixs, *pixg1, *pixg2, *pixb;
84 SARRAY *safiles;
85 
86  if (!dirin)
87  return ERROR_INT("dirin", __func__, 1);
88  if (!dirout)
89  return ERROR_INT("dirout", __func__, 1);
90  if (upscaling != 1 && upscaling != 2 && upscaling != 4)
91  return ERROR_INT("invalid upscaling factor", __func__, 1);
92  if (thresh <= 0) thresh = 180;
93  if (firstpage < 0) firstpage = 0;
94  if (npages < 0) npages = 0;
95  if (outformat != IFF_TIFF_G4)
96  outformat = IFF_PNG;
97 
98  safiles = getSortedPathnamesInDirectory(dirin, substr, firstpage, npages);
99  if (!safiles)
100  return ERROR_INT("safiles not made", __func__, 1);
101  if ((nfiles = sarrayGetCount(safiles)) == 0) {
102  sarrayDestroy(&safiles);
103  return ERROR_INT("no matching files in the directory", __func__, 1);
104  }
105 
106  for (i = 0; i < nfiles; i++) {
107  fname = sarrayGetString(safiles, i, L_NOCOPY);
108  if ((pixs = pixRead(fname)) == NULL) {
109  L_WARNING("Couldn't read file %s\n", __func__, fname);
110  continue;
111  }
112  if (pixGetDepth(pixs) == 32)
113  pixg1 = pixConvertRGBToLuminance(pixs);
114  else
115  pixg1 = pixClone(pixs);
117  if (pixGetDepth(pixg2) == 1) {
118  pixb = pixClone(pixg2);
119  } else {
120  if (upscaling == 1)
121  pixb = pixThresholdToBinary(pixg2, thresh);
122  else if (upscaling == 2)
123  pixb = pixScaleGray2xLIThresh(pixg2, thresh);
124  else /* upscaling == 4 */
125  pixb = pixScaleGray4xLIThresh(pixg2, thresh);
126  }
127  pixDestroy(&pixs);
128  pixDestroy(&pixg1);
129  pixDestroy(&pixg2);
130 
131  splitPathAtDirectory(fname, NULL, &tail);
132  splitPathAtExtension(tail, &basename, NULL);
133  if (outformat == IFF_TIFF_G4) {
134  snprintf(buf, sizeof(buf), "%s/%s.tif", dirout, basename);
135  pixWrite(buf, pixb, IFF_TIFF_G4);
136  } else {
137  snprintf(buf, sizeof(buf), "%s/%s.png", dirout, basename);
138  pixWrite(buf, pixb, IFF_PNG);
139  }
140  pixDestroy(&pixb);
141  LEPT_FREE(tail);
142  LEPT_FREE(basename);
143  }
144 
145  sarrayDestroy(&safiles);
146  return 0;
147 }
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:443
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:608
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:582
@ REMOVE_CMAP_TO_GRAYSCALE
Definition: pix.h:381
@ L_NOCOPY
Definition: pix.h:503
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:324
PIX * pixConvertRGBToLuminance(PIX *pixs)
pixConvertRGBToLuminance()
Definition: pixconv.c:732
PIX * pixRead(const char *filename)
pixRead()
Definition: readfile.c:189
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:673
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:617
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:353
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
Definition: sarray1.c:1739
PIX * pixScaleGray4xLIThresh(PIX *pixs, l_int32 thresh)
pixScaleGray4xLIThresh()
Definition: scale1.c:1097
PIX * pixScaleGray2xLIThresh(PIX *pixs, l_int32 thresh)
pixScaleGray2xLIThresh()
Definition: scale1.c:903
l_ok splitPathAtExtension(const char *pathname, char **pbasename, char **pextension)
splitPathAtExtension()
Definition: utils2.c:2796
l_ok splitPathAtDirectory(const char *pathname, char **pdir, char **ptail)
splitPathAtDirectory()
Definition: utils2.c:2728