Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/leptonica/src/convertfiles.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 * \file convertfiles.c | |
| 29 * <pre> | |
| 30 * | |
| 31 * Conversion to 1 bpp | |
| 32 * l_int32 convertFilesTo1bpp() | |
| 33 * | |
| 34 * These are utility functions that will perform depth conversion | |
| 35 * on selected files, writing the results to a specified directory. | |
| 36 * We start with conversion to 1 bpp. | |
| 37 * </pre> | |
| 38 */ | |
| 39 | |
| 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 *------------------------------------------------------------------*/ | |
| 50 /*! | |
| 51 * \brief convertFilesTo1bpp() | |
| 52 * | |
| 53 * \param[in] dirin | |
| 54 * \param[in] substr [optional] substring filter on filenames; | |
| 55 8 can be NULL | |
| 56 * \param[in] upscaling 1, 2 or 4; only for input color or grayscale | |
| 57 * \param[in] thresh global threshold for binarization; 0 for default | |
| 58 * \param[in] firstpage | |
| 59 * \param[in] npages use 0 to do all from %firstpage to the end | |
| 60 * \param[in] dirout | |
| 61 * \param[in] outformat IFF_PNG, IFF_TIFF_G4 | |
| 62 * \return 0 if OK, 1 on error | |
| 63 * | |
| 64 * <pre> | |
| 65 * Notes: | |
| 66 * (1) Images are sorted lexicographically, and the names in the | |
| 67 * output directory are retained except for the extension. | |
| 68 * </pre> | |
| 69 */ | |
| 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); | |
| 116 pixg2 = pixRemoveColormap(pixg1, REMOVE_CMAP_TO_GRAYSCALE); | |
| 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 } |
