202 #include <config_auto.h>
206 #include "allheaders.h"
209 static const char *textsel1 =
"x oo "
215 static const char *textsel2 =
" oo x"
221 static const char *textsel3 =
"xxxxxx"
227 static const char *textsel4 =
"xxxxxx"
234 static const l_int32 DefaultMinUpDownCount = 70;
235 static const l_float32 DefaultMinUpDownConf = 8.0;
236 static const l_float32 DefaultMinUpDownRatio = 2.5;
239 static const l_int32 DefaultMinMirrorFlipCount = 100;
240 static const l_float32 DefaultMinMirrorFlipConf = 5.0;
243 static void pixDebugFlipDetect(
const char *filename,
PIX *pixs,
244 PIX *pixhm, l_int32 enable);
278 l_float32 *pleftconf,
283 l_float32 upconf, leftconf;
286 if (!pixs || pixGetDepth(pixs) != 1)
287 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
294 if (pupconf) *pupconf = upconf;
295 if (pleftconf) *pleftconf = leftconf;
304 L_INFO(
"text orientation not determined; no rotation\n", __func__);
305 if (protation) *protation = 0;
309 L_INFO(
"text is oriented up; no rotation\n", __func__);
310 if (protation) *protation = 0;
314 L_INFO(
"landscape; text oriented left; 90 cw rotation\n", __func__);
315 if (protation) *protation = 90;
319 L_INFO(
"text oriented down; 180 cw rotation\n", __func__);
320 if (protation) *protation = 180;
324 L_INFO(
"landscape; text oriented right; 270 cw rotation\n", __func__);
325 if (protation) *protation = 270;
329 L_ERROR(
"invalid orient flag!\n", __func__);
405 l_float32 *pleftconf,
411 if (!pixs || pixGetDepth(pixs) != 1)
412 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
413 if (!pupconf && !pleftconf)
414 return ERROR_INT(
"nothing to do", __func__, 1);
416 mincount = DefaultMinUpDownCount;
467 l_float32 absupconf, absleftconf;
470 return ERROR_INT(
"&orient not defined", __func__, 1);
472 if (upconf == 0.0 || leftconf == 0.0) {
473 L_INFO(
"not enough confidence to get orientation\n", __func__);
477 if (minupconf == 0.0)
478 minupconf = DefaultMinUpDownConf;
480 minratio = DefaultMinUpDownRatio;
481 absupconf = L_ABS(upconf);
482 absleftconf = L_ABS(leftconf);
486 if (upconf > minupconf && absupconf > minratio * absleftconf)
488 else if (leftconf > minupconf && absleftconf > minratio * absupconf)
490 else if (upconf < -minupconf && absupconf > minratio * absleftconf)
492 else if (leftconf < -minupconf && absleftconf > minratio * absupconf)
496 lept_stderr(
"upconf = %7.3f, leftconf = %7.3f\n", upconf, leftconf);
498 lept_stderr(
"Confidence is low; no determination is made\n");
558 l_int32 countup, countdown, nmax;
559 l_float32 nup, ndown;
560 PIX *pix0, *pix1, *pix2, *pix3, *pixm;
561 SEL *sel1, *sel2, *sel3, *sel4;
564 return ERROR_INT(
"&conf not defined", __func__, 1);
566 if (!pixs || pixGetDepth(pixs) != 1)
567 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
569 mincount = DefaultMinUpDownCount;
592 l_int32 i, nbox, x, y, w, h;
600 for (i = 0; i < nbox; i++) {
604 pixRasterop(pixm, x + npixels, y - 6, w - 2 * npixels, h + 13,
614 pix1 =
pixHMT(NULL, pix0, sel1);
615 pix2 =
pixHMT(NULL, pix0, sel2);
616 pixOr(pix1, pix1, pix2);
621 pixDebugFlipDetect(
"/tmp/lept/orient/up.png", pixs, pix1, debug);
627 pix1 =
pixHMT(NULL, pix0, sel3);
628 pix2 =
pixHMT(NULL, pix0, sel4);
629 pixOr(pix1, pix1, pix2);
634 pixDebugFlipDetect(
"/tmp/lept/orient/down.png", pixs, pix1, debug);
641 nup = (l_float32)(countup);
642 ndown = (l_float32)(countdown);
643 nmax = L_MAX(countup, countdown);
645 *pconf = 2. * ((nup - ndown) / sqrt(nup + ndown));
648 if (pixm) pixWriteDebug(
"/tmp/lept/orient/pixm1.png", pixm, IFF_PNG);
649 lept_stderr(
"nup = %7.3f, ndown = %7.3f, conf = %7.3f\n",
651 if (*pconf > DefaultMinUpDownConf)
653 if (*pconf < -DefaultMinUpDownConf)
715 l_int32 count1, count2, nmax;
716 l_float32 nleft, nright;
717 PIX *pix0, *pix1, *pix2, *pix3;
721 return ERROR_INT(
"&conf not defined", __func__, 1);
723 if (!pixs || pixGetDepth(pixs) != 1)
724 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
726 mincount = DefaultMinMirrorFlipCount;
741 pixOr(pix0, pix0, pixs);
745 pix1 =
pixHMT(NULL, pix0, sel1);
748 pixDebugFlipDetect(
"/tmp/lept/orient/right.png", pixs, pix1, debug);
753 pix2 =
pixHMT(NULL, pix0, sel2);
756 pixDebugFlipDetect(
"/tmp/lept/orient/left.png", pixs, pix2, debug);
760 nright = (l_float32)count1;
761 nleft = (l_float32)count2;
762 nmax = L_MAX(count1, count2);
768 *pconf = 2. * ((nright - nleft) / sqrt(nright + nleft));
771 lept_stderr(
"nright = %f, nleft = %f\n", nright, nleft);
772 if (*pconf > DefaultMinMirrorFlipConf)
774 if (*pconf < -DefaultMinMirrorFlipConf)
795 pixDebugFlipDetect(
const char *filename,
809 pixWriteDebug(filename, pixt, IFF_PNG);
PIX * pixReduceRankBinaryCascade(PIX *pixs, l_int32 level1, l_int32 level2, l_int32 level3, l_int32 level4)
pixReduceRankBinaryCascade()
l_ok boxGetGeometry(const BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
void boxDestroy(BOX **pbox)
boxDestroy()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
l_int32 boxaGetCount(const BOXA *boxa)
boxaGetCount()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
BOXA * pixConnComp(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnComp()
PIX * pixOrientCorrect(PIX *pixs, l_float32 minupconf, l_float32 minratio, l_float32 *pupconf, l_float32 *pleftconf, l_int32 *protation, l_int32 debug)
pixOrientCorrect()
l_ok makeOrientDecision(l_float32 upconf, l_float32 leftconf, l_float32 minupconf, l_float32 minratio, l_int32 *porient, l_int32 debug)
makeOrientDecision()
l_ok pixMirrorDetect(PIX *pixs, l_float32 *pconf, l_int32 mincount, l_int32 debug)
pixMirrorDetect()
l_ok pixUpDownDetect(PIX *pixs, l_float32 *pconf, l_int32 mincount, l_int32 npixels, l_int32 debug)
pixUpDownDetect()
l_ok pixOrientDetect(PIX *pixs, l_float32 *pupconf, l_float32 *pleftconf, l_int32 mincount, l_int32 debug)
pixOrientDetect()
PIX * pixHMT(PIX *pixd, PIX *pixs, SEL *sel)
pixHMT()
PIX * pixMorphSequence(PIX *pixs, const char *sequence, l_int32 dispsep)
pixMorphSequence()
PIX * pixMorphCompSequence(PIX *pixs, const char *sequence, l_int32 dispsep)
pixMorphCompSequence()
l_ok pixSetMaskedCmap(PIX *pixs, PIX *pixm, l_int32 x, l_int32 y, l_int32 rval, l_int32 gval, l_int32 bval)
pixSetMaskedCmap()
void pixDestroy(PIX **ppix)
pixDestroy()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
PIX * pixOr(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixOr()
PIX * pixAnd(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixAnd()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
PIX * pixConvert1To4Cmap(PIX *pixs)
pixConvert1To4Cmap()
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
PIX * pixRotate90(PIX *pixs, l_int32 direction)
pixRotate90()
PIX * pixRotateOrth(PIX *pixs, l_int32 quads)
pixRotateOrth()
SEL * selCreateFromString(const char *text, l_int32 h, l_int32 w, const char *name)
selCreateFromString()
void selDestroy(SEL **psel)
selDestroy()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()