Leptonica 1.68
C Image Processing Library

croptext.c

Go to the documentation of this file.
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  *  croptext.c
00018  *
00019  *     Simple program that crops text pages to a given border
00020  *
00021  *     Syntax:
00022  *         croptext dirin border dirout
00023  *     where
00024  *         border = number of pixels added on each side (e.g., 50)
00025  *
00026  *     The output file name has the same tail as the input file name.
00027  *     If dirout is the same as dirin, you overwrite the input files.
00028  */
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include "allheaders.h"
00033 
00034 
00035 main(int    argc,
00036 char **argv)
00037 {
00038 char        *dirin, *dirout, *infile, *outfile, *tail;
00039 l_int32      i, nfiles, border, x, y, w, h, xb, yb, wb, hb;
00040 BOX         *box1, *box2;
00041 BOXA        *boxa1, *boxa2;
00042 PIX         *pixs, *pixt1, *pixd;
00043 SARRAY      *safiles;
00044 static char  mainName[] = "croptext";
00045 
00046     if (argc != 4)
00047         exit(ERROR_INT("Syntax: croptext dirin border dirout", mainName, 1)); 
00048 
00049     dirin = argv[1];
00050     border = atoi(argv[2]);
00051     dirout = argv[3];
00052 
00053     safiles = getSortedPathnamesInDirectory(dirin, NULL, 0, 0);
00054     nfiles = sarrayGetCount(safiles);
00055 
00056     for (i = 0; i < nfiles; i++) {
00057         infile = sarrayGetString(safiles, i, 0);
00058         splitPathAtDirectory(infile, NULL, &tail);
00059         outfile = genPathname(dirout, tail);
00060         pixs = pixRead(infile);
00061         pixt1 = pixMorphSequence(pixs, "r11 + c10.40 + o5.5 + x4", 0);
00062         boxa1 = pixConnComp(pixt1, NULL, 8);
00063         if (boxaGetCount(boxa1) == 0) {
00064             fprintf(stderr, "Warning: no components on page %s\n", tail);
00065             continue;
00066         }
00067         boxa2 = boxaSort(boxa1, L_SORT_BY_AREA, L_SORT_DECREASING, NULL);
00068         box1 = boxaGetBox(boxa2, 0, L_CLONE);
00069         boxGetGeometry(box1, &x, &y, &w, &h);
00070         xb = L_MAX(0, x - border);
00071         yb = L_MAX(0, y - border);
00072         wb = w + 2 * border;
00073         hb = h + 2 * border;
00074         box2 = boxCreate(xb, yb, wb, hb);
00075         pixd = pixClipRectangle(pixs, box2, NULL);
00076         pixWrite(outfile, pixd, IFF_TIFF_G4);
00077 
00078         pixDestroy(&pixs);
00079         pixDestroy(&pixt1);
00080         pixDestroy(&pixd);
00081         boxaDestroy(&boxa1);
00082         boxaDestroy(&boxa2);
00083     }
00084 
00085     return 0;
00086 }
00087 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines