/* * The Npic library * * Copyright (C) 2003 Edouard Thiel * * This library is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* * dist_dt.c - 07/01/2011 * * Front-end for Distance Transforms */ #include /*--------------------- P U B L I C - I N T E R F A C E ----------------------*/ /* * Compute the distance transform (DT) on image np for the mask mh; * mh is a half weighted distance mask or describe an Euclidean distance. * * Do nothing if np is not ok. set not ok on error. Verbose. * Return computation time in second.microsecond */ double NpicDT (Npic_image *np, Npic_mask *mh) { int dtype; if (NpicDTCheckData (np, mh, __func__) != NPIC_SUCCESS) return 0; dtype = NpicDTFindDistType (mh); switch (dtype) { case NPIC_DIST_WEIGHTED : return NpicWDT (np, mh); case NPIC_DIST_EUCLID_SQUARED : return NpicSEDT_Hirata (np); default : np->gen.ok = NpicError (__func__, NPIC_ERROR, ": distance type not available"); } return 0; } /* * Compute the distance transform (DT) on image np for the mask mh * with "infinite border", i.e border pixels, propagating no distance value; * mh is a half weighted distance mask or describe an Euclidean distance. * * Do nothing if np is not ok. set not ok on error. Verbose. * Return computation time in second.microsecond */ double NpicDT_inf (Npic_image *np, Npic_mask *mh) { int dtype; if (NpicDTCheckData (np, mh, __func__) != NPIC_SUCCESS) return 0; dtype = NpicDTFindDistType (mh); switch (dtype) { case NPIC_DIST_WEIGHTED : return NpicWDT_inf (np, mh); case NPIC_DIST_EUCLID_SQUARED : return NpicSEDT_Hirata_inf (np); default : np->gen.ok = NpicError (__func__, NPIC_ERR_UNAV_DIST, ""); } return 0; } /* * Compute the reverse weighted distance transform (DT_rev) on image np * for the mask mh; * mh is a half weighted distance mask or describe an Euclidean distance. * * Do nothing if np is not ok. set not ok on error. Verbose. * Return computation time in second.microsecond */ double NpicDT_rev (Npic_image *np, Npic_mask *mh) { int dtype; if (NpicDTCheckData (np, mh, __func__) != NPIC_SUCCESS) return 0; dtype = NpicDTFindDistType (mh); switch (dtype) { case NPIC_DIST_WEIGHTED : return NpicWDT_rev (np, mh); case NPIC_DIST_EUCLID_SQUARED : return NpicSEDT_Hirata_rev (np); default : np->gen.ok = NpicError (__func__, NPIC_ERR_UNAV_DIST, ""); } return 0; } /*-------------------- P R I V A T E - F U N C T I O N S ---------------------*/ /* * Check if images np and mask mh are ok and are compatible for DT and RDT * computations. * Return NPIC_SUCCESS, else error code. * set not ok for np on error. Verbose. */ int NpicDTCheckData (Npic_image *np, Npic_mask *mh, const char *funcname) { if (NpicImageIsOK (np, funcname) != NPIC_SUCCESS) return NPIC_ERR_NOT_OK; if (NpicMaskIsOK (mh, funcname) != NPIC_SUCCESS) return np->gen.ok = NPIC_ERR_NOT_OK; if (NpicMaskCompat (mh, np) != NPIC_SUCCESS) return np->gen.ok = NpicError (funcname, NPIC_ERR_INCOMPAT, ": dest image and dist mask"); return NPIC_SUCCESS; } /* * Return the distance type from the property DistanceType of the mask. * On error, return NPIC_DIST_NONE. Silent. */ Npic_dist_type NpicDTFindDistType (Npic_mask *mh) { int dtype = NPIC_DIST_NONE; const char *val; if (mh == NULL) return dtype; val = NpicMaskGetProp (mh, "DistanceType"); if (strcmp (val, "Weighted" ) == 0) dtype = NPIC_DIST_WEIGHTED; else if (strcmp (val, "SquaredEuclidean") == 0) dtype = NPIC_DIST_EUCLID_SQUARED; else if (strcmp (val, "CeilEuclidean" ) == 0) dtype = NPIC_DIST_EUCLID_CEIL; else if (strcmp (val, "FloorEulidean" ) == 0) dtype = NPIC_DIST_EUCLID_FLOOR; return dtype; }