Leptonica 1.68
C Image Processing Library

binreduce.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  *  binreduce.c
00018  *
00019  *      Subsampled reduction
00020  *
00021  *           PIX    *pixReduceBinary2()
00022  *
00023  *      Rank filtered reductions
00024  *
00025  *           PIX    *pixReduceRankBinaryCascade()
00026  *           PIX    *pixReduceRankBinary2()
00027  */
00028 
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <stdlib.h>
00032 #include "allheaders.h"
00033 
00034 
00035 /*------------------------------------------------------------------*
00036  *                       Subsampled reduction                       *
00037  *------------------------------------------------------------------*/
00038 /*!
00039  *  pixReduceBinary2()
00040  *
00041  *      Input:  pixs
00042  *              tab (<optional>; if null, a table is made here
00043  *                   and destroyed before exit)
00044  *      Return: pixd (2x subsampled), or null on error
00045  */
00046 PIX *
00047 pixReduceBinary2(PIX      *pixs,
00048                  l_uint8  *intab)
00049 {
00050 l_uint8   *tab;
00051 l_int32    ws, hs, wpls, wpld;
00052 l_uint32  *datas, *datad;
00053 PIX       *pixd;
00054 
00055     PROCNAME("pixReduceBinary2");
00056 
00057     if (!pixs)
00058         return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
00059 
00060     if (pixGetDepth(pixs) != 1)
00061         return (PIX *)ERROR_PTR("pixs not binary", procName, NULL);
00062 
00063     if (intab) /* use input table */
00064         tab = intab;
00065     else {
00066         if ((tab = makeSubsampleTab2x()) == NULL)
00067             return (PIX *)ERROR_PTR("tab not made", procName, NULL);
00068     }
00069     
00070     ws = pixGetWidth(pixs);
00071     hs = pixGetHeight(pixs);
00072     if (hs <= 1)
00073         return (PIX *)ERROR_PTR("hs must be at least 2", procName, NULL);
00074     wpls = pixGetWpl(pixs);
00075     datas = pixGetData(pixs);
00076 
00077     if ((pixd = pixCreate(ws / 2, hs / 2, 1)) == NULL)
00078         return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
00079     pixCopyResolution(pixd, pixs);
00080     pixScaleResolution(pixd, 0.5, 0.5);
00081     wpld = pixGetWpl(pixd);
00082     datad = pixGetData(pixd);
00083 
00084     reduceBinary2Low(datad, wpld, datas, hs, wpls, tab);
00085 
00086     if (intab == NULL)
00087         FREE(tab);
00088 
00089     return pixd;
00090 }
00091 
00092 
00093 /*------------------------------------------------------------------*
00094  *                   Rank filtered binary reductions                *
00095  *------------------------------------------------------------------*/
00096 /*!
00097  *  pixReduceRankBinaryCascade()
00098  *
00099  *      Input:  pixs (1 bpp)
00100  *              level1, ... level 4 (thresholds, in the set {0, 1, 2, 3, 4})
00101  *      Return: pixd, or null on error
00102  *
00103  *  Notes:
00104  *      (1) This performs up to four cascaded 2x rank reductions.
00105  *      (2) Use level = 0 to truncate the cascade.
00106  */
00107 PIX *
00108 pixReduceRankBinaryCascade(PIX     *pixs,
00109                            l_int32  level1,
00110                            l_int32  level2,
00111                            l_int32  level3,
00112                            l_int32  level4)
00113 {
00114 PIX      *pix1, *pix2, *pix3, *pix4;
00115 l_uint8  *tab;
00116 
00117     PROCNAME("pixReduceRankBinaryCascade");
00118 
00119     if (!pixs)
00120         return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
00121     if (pixGetDepth(pixs) != 1)
00122         return (PIX *)ERROR_PTR("pixs must be binary", procName, NULL);
00123     if (level1 > 4 || level2 > 4 || level3 > 4 || level4 > 4)
00124         return (PIX *)ERROR_PTR("levels must not exceed 4", procName, NULL);
00125 
00126     if (level1 <= 0) {
00127         L_WARNING("no reduction because level1 not > 0", procName);
00128         return pixCopy(NULL, pixs);
00129     }
00130 
00131     if ((tab = makeSubsampleTab2x()) == NULL)
00132         return (PIX *)ERROR_PTR("tab not made", procName, NULL);
00133     
00134     pix1 = pixReduceRankBinary2(pixs, level1, tab);
00135     if (level2 <= 0) {
00136         FREE(tab);
00137         return pix1;
00138     }
00139 
00140     pix2 = pixReduceRankBinary2(pix1, level2, tab);
00141     pixDestroy(&pix1);
00142     if (level3 <= 0) {
00143         FREE(tab);
00144         return pix2;
00145     }
00146 
00147     pix3 = pixReduceRankBinary2(pix2, level3, tab);
00148     pixDestroy(&pix2);
00149     if (level4 <= 0) {
00150         FREE(tab);
00151         return pix3;
00152     }
00153 
00154     pix4 = pixReduceRankBinary2(pix3, level4, tab);
00155     pixDestroy(&pix3);
00156     FREE(tab);
00157     return pix4;
00158 }
00159 
00160 
00161 /*!
00162  *  pixReduceRankBinary2()
00163  *
00164  *      Input:  pixs (1 bpp)
00165  *              level (rank threshold: 1, 2, 3, 4)
00166  *              intab (<optional>; if null, a table is made here
00167  *                     and destroyed before exit)
00168  *      Return: pixd (1 bpp, 2x rank threshold reduced), or null on error
00169  *
00170  *  Notes:
00171  *      (1) pixd is downscaled by 2x from pixs.
00172  *      (2) The rank threshold specifies the minimum number of ON
00173  *          pixels in each 2x2 region of pixs that are required to
00174  *          set the corresponding pixel ON in pixd.
00175  */
00176 PIX *
00177 pixReduceRankBinary2(PIX      *pixs,
00178                      l_int32   level,
00179                      l_uint8  *intab)
00180 {
00181 l_uint8   *tab;
00182 l_int32    ws, hs, wpls, wpld;
00183 l_uint32  *datas, *datad;
00184 PIX       *pixd;
00185 
00186     PROCNAME("pixReduceRankBinary2");
00187 
00188     if (!pixs)
00189         return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
00190 
00191     if (pixGetDepth(pixs) != 1)
00192         return (PIX *)ERROR_PTR("pixs not binary", procName, NULL);
00193     if (level < 1 || level > 4)
00194         return (PIX *)ERROR_PTR("level must be in set {1,2,3,4}",
00195             procName, NULL);
00196 
00197     if (intab) /* use input table */
00198         tab = intab;
00199     else {
00200         if ((tab = makeSubsampleTab2x()) == NULL)
00201             return (PIX *)ERROR_PTR("tab not made", procName, NULL);
00202     }
00203     
00204     ws = pixGetWidth(pixs);
00205     hs = pixGetHeight(pixs);
00206     if (hs <= 1)
00207         return (PIX *)ERROR_PTR("hs must be at least 2", procName, NULL);
00208     wpls = pixGetWpl(pixs);
00209     datas = pixGetData(pixs);
00210 
00211     if ((pixd = pixCreate(ws / 2, hs / 2, 1)) == NULL)
00212         return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
00213     pixCopyResolution(pixd, pixs);
00214     pixScaleResolution(pixd, 0.5, 0.5);
00215     wpld = pixGetWpl(pixd);
00216     datad = pixGetData(pixd);
00217 
00218     reduceRankBinary2Low(datad, wpld, datas, hs, wpls, tab, level);
00219 
00220     if (!intab)
00221         FREE(tab);
00222 
00223     return pixd;
00224 }
00225 
00226 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines