Leptonica 1.68
C Image Processing Library

fhmtgen.1.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  *      Top-level fast hit-miss transform with auto-generated sels
00018  *
00019  *             PIX     *pixHMTDwa_1()
00020  *             PIX     *pixFHMTGen_1()
00021  */
00022 
00023 #include <string.h>
00024 #include "allheaders.h"
00025 
00026 PIX *pixHMTDwa_1(PIX *pixd, PIX *pixs, char *selname);
00027 PIX *pixFHMTGen_1(PIX *pixd, PIX *pixs, char *selname);
00028 l_int32 fhmtgen_low_1(l_uint32 *datad, l_int32 w,
00029                       l_int32 h, l_int32 wpld,
00030                       l_uint32 *datas, l_int32 wpls,
00031                       l_int32 index);
00032 
00033 static l_int32   NUM_SELS_GENERATED = 6;
00034 static char  SEL_NAMES[][80] = {
00035                              "sel_3hm",
00036                              "sel_3de",
00037                              "sel_3ue",
00038                              "sel_3re",
00039                              "sel_3le",
00040                              "sel_sl1"};
00041 
00042 /*!
00043  *  pixHMTDwa_1()
00044  *
00045  *      Input:  pixd (usual 3 choices: null, == pixs, != pixs)
00046  *              pixs (1 bpp)
00047  *              sel name
00048  *      Return: pixd
00049  *
00050  *  Notes:
00051  *      (1) This simply adds a 32 pixel border, calls the appropriate
00052  *          pixFHMTGen_*(), and removes the border.
00053  *          See notes below for that function.
00054  */
00055 PIX *
00056 pixHMTDwa_1(PIX   *pixd,
00057             PIX   *pixs,
00058             char  *selname)
00059 {
00060 PIX  *pixt1, *pixt2, *pixt3;
00061 
00062     PROCNAME("pixHMTDwa_1");
00063 
00064     if (!pixs)
00065         return (PIX *)ERROR_PTR("pixs not defined", procName, pixd);
00066     if (pixGetDepth(pixs) != 1)
00067         return (PIX *)ERROR_PTR("pixs must be 1 bpp", procName, pixd);
00068 
00069     pixt1 = pixAddBorder(pixs, 32, 0);
00070     pixt2 = pixFHMTGen_1(NULL, pixt1, selname);
00071     pixt3 = pixRemoveBorder(pixt2, 32);
00072     pixDestroy(&pixt1);
00073     pixDestroy(&pixt2);
00074 
00075     if (!pixd)
00076         return pixt3;
00077 
00078     pixCopy(pixd, pixt3);
00079     pixDestroy(&pixt3);
00080     return pixd;
00081 }
00082 
00083 
00084 /*!
00085  *  pixFHMTGen_1()
00086  *
00087  *      Input:  pixd (usual 3 choices: null, == pixs, != pixs)
00088  *              pixs (1 bpp)
00089  *              sel name
00090  *      Return: pixd
00091  *
00092  *  Notes:
00093  *      (1) This is a dwa implementation of the hit-miss transform
00094  *          on pixs by the sel.
00095  *      (2) The sel must be limited in size to not more than 31 pixels
00096  *          about the origin.  It must have at least one hit, and it
00097  *          can have any number of misses.
00098  *      (3) This handles all required setting of the border pixels
00099  *          before erosion and dilation.
00100  */
00101 PIX *
00102 pixFHMTGen_1(PIX   *pixd,
00103              PIX   *pixs,
00104              char  *selname)
00105 {
00106 l_int32    i, index, found, w, h, wpls, wpld;
00107 l_uint32  *datad, *datas, *datat;
00108 PIX       *pixt;
00109 
00110     PROCNAME("pixFHMTGen_1");
00111 
00112     if (!pixs)
00113         return (PIX *)ERROR_PTR("pixs not defined", procName, pixd);
00114     if (pixGetDepth(pixs) != 1)
00115         return (PIX *)ERROR_PTR("pixs must be 1 bpp", procName, pixd);
00116 
00117     found = FALSE;
00118     for (i = 0; i < NUM_SELS_GENERATED; i++) {
00119         if (strcmp(selname, SEL_NAMES[i]) == 0) {
00120             found = TRUE;
00121             index = i;
00122             break;
00123         }
00124     }
00125     if (found == FALSE)
00126         return (PIX *)ERROR_PTR("sel index not found", procName, pixd);
00127 
00128     if (!pixd) {
00129         if ((pixd = pixCreateTemplate(pixs)) == NULL)
00130             return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
00131     }
00132     else  /* for in-place or pre-allocated */
00133         pixResizeImageData(pixd, pixs);
00134     wpls = pixGetWpl(pixs);
00135     wpld = pixGetWpl(pixd);
00136 
00137         /*  The images must be surrounded with 32 additional border
00138          *  pixels, that we'll read from.  We fabricate a "proper"
00139          *  image as the subimage within the border, having the 
00140          *  following parameters:  */
00141     w = pixGetWidth(pixs) - 64;
00142     h = pixGetHeight(pixs) - 64;
00143     datas = pixGetData(pixs) + 32 * wpls + 1;
00144     datad = pixGetData(pixd) + 32 * wpld + 1;
00145 
00146     if (pixd == pixs) {  /* need temp image if in-place */
00147         if ((pixt = pixCopy(NULL, pixs)) == NULL)
00148             return (PIX *)ERROR_PTR("pixt not made", procName, pixd);
00149         datat = pixGetData(pixt) + 32 * wpls + 1;
00150         fhmtgen_low_1(datad, w, h, wpld, datat, wpls, index);
00151         pixDestroy(&pixt);
00152     }
00153     else {  /* not in-place */
00154         fhmtgen_low_1(datad, w, h, wpld, datas, wpls, index);
00155     }
00156 
00157     return pixd;
00158 }
00159 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines