Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * convertfiles.c 00018 * 00019 * Conversion to 1 bpp 00020 * l_int32 convertFilesTo1bpp() 00021 * 00022 * These are utility functions that will perform depth conversion 00023 * on selected files, writing the results to a specified directory. 00024 * We start with conversion to 1 bpp. 00025 */ 00026 00027 #include <string.h> 00028 #include "allheaders.h" 00029 00030 00031 /*------------------------------------------------------------------* 00032 * Conversion to 1 bpp * 00033 *------------------------------------------------------------------*/ 00034 /*! 00035 * convertFilesTo1bpp() 00036 * 00037 * Input: dirin 00038 * substr (<optional> substring filter on filenames; can be NULL) 00039 * upscaling (1, 2 or 4; only for input color or grayscale) 00040 * thresh (global threshold for binarization; use 0 for default) 00041 * firstpage 00042 * npages (use 0 to do all from @firstpage to the end) 00043 * dirout 00044 * outformat (IFF_PNG, IFF_TIFF_G4) 00045 * Return: 0 if OK, 1 on error 00046 * 00047 * Notes: 00048 * (1) Images are sorted lexicographically, and the names in the 00049 * output directory are retained except for the extension. 00050 */ 00051 l_int32 00052 convertFilesTo1bpp(const char *dirin, 00053 const char *substr, 00054 l_int32 upscaling, 00055 l_int32 thresh, 00056 l_int32 firstpage, 00057 l_int32 npages, 00058 const char *dirout, 00059 l_int32 outformat) 00060 { 00061 l_int32 i, nfiles; 00062 char buf[512]; 00063 char *fname, *tail, *basename; 00064 PIX *pixs, *pixg1, *pixg2, *pixb; 00065 SARRAY *safiles; 00066 00067 PROCNAME("convertFilesTo1bpp"); 00068 00069 if (!dirin) 00070 return ERROR_INT("dirin", procName, 1); 00071 if (!dirout) 00072 return ERROR_INT("dirout", procName, 1); 00073 if (upscaling != 1 && upscaling != 2 && upscaling != 4) 00074 return ERROR_INT("invalid upscaling factor", procName, 1); 00075 if (thresh <= 0) thresh = 180; 00076 if (firstpage < 0) firstpage = 0; 00077 if (npages < 0) npages = 0; 00078 if (outformat != IFF_TIFF_G4) 00079 outformat = IFF_PNG; 00080 00081 safiles = getSortedPathnamesInDirectory(dirin, substr, firstpage, npages); 00082 if (!safiles) 00083 return ERROR_INT("safiles not made", procName, 1); 00084 if ((nfiles = sarrayGetCount(safiles)) == 0) { 00085 sarrayDestroy(&safiles); 00086 return ERROR_INT("no matching files in the directory", procName, 1); 00087 } 00088 00089 for (i = 0; i < nfiles; i++) { 00090 fname = sarrayGetString(safiles, i, L_NOCOPY); 00091 if ((pixs = pixRead(fname)) == NULL) { 00092 L_WARNING_STRING("Couldn't read file %s\n", procName, fname); 00093 continue; 00094 } 00095 if (pixGetDepth(pixs) == 32) 00096 pixg1 = pixConvertRGBToLuminance(pixs); 00097 else 00098 pixg1 = pixClone(pixs); 00099 pixg2 = pixRemoveColormap(pixg1, REMOVE_CMAP_TO_GRAYSCALE); 00100 if (pixGetDepth(pixg2) == 1) 00101 pixb = pixClone(pixg2); 00102 else { 00103 if (upscaling == 1) 00104 pixb = pixThresholdToBinary(pixg2, thresh); 00105 else if (upscaling == 2) 00106 pixb = pixScaleGray2xLIThresh(pixg2, thresh); 00107 else /* upscaling == 4 */ 00108 pixb = pixScaleGray4xLIThresh(pixg2, thresh); 00109 } 00110 pixDestroy(&pixs); 00111 pixDestroy(&pixg1); 00112 pixDestroy(&pixg2); 00113 00114 splitPathAtDirectory(fname, NULL, &tail); 00115 splitPathAtExtension(tail, &basename, NULL); 00116 if (outformat == IFF_TIFF_G4) { 00117 snprintf(buf, sizeof(buf), "%s/%s.tif", dirout, basename); 00118 pixWrite(buf, pixb, IFF_TIFF_G4); 00119 } 00120 else { 00121 snprintf(buf, sizeof(buf), "%s/%s.png", dirout, basename); 00122 pixWrite(buf, pixb, IFF_PNG); 00123 } 00124 pixDestroy(&pixb); 00125 FREE(tail); 00126 FREE(basename); 00127 } 00128 00129 sarrayDestroy(&safiles); 00130 return 0; 00131 } 00132