00001 /*---------------------------------------------------------------------+ 00002 | Library QgarLib, graphics analysis and recognition | 00003 | Copyright (C) 2002 Qgar Project, LORIA | 00004 | | 00005 | This library is free software; you can redistribute it and/or | 00006 | modify it under the terms of the GNU Lesser General Public | 00007 | License version 2.1, as published by the Free Software Foundation. | 00008 | | 00009 | This library is distributed in the hope that it will be useful, | 00010 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00011 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 00012 | See the GNU Lesser General Public License for more details. | 00013 | | 00014 | The GNU Lesser General Public License is included in the file | 00015 | LICENSE.LGPL, in the root directory of the Qgar packaging. See | 00016 | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. | 00017 | To receive a paper copy, write to the Free Software Foundation, | 00018 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. | 00019 | | 00020 | Contact Project Qgar for any information: | 00021 | LORIA - équipe Qgar | 00022 | B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France | 00023 | email: qgar-contact@loria.fr | 00024 | http://www.qgar.org/ | 00025 *---------------------------------------------------------------------*/ 00026 00027 00028 /** 00029 * @file Dist34BlackCCImage.C 00030 * @brief Implementation of class DistanceTransform34Image. 00031 * 00032 * See file DistanceTransform34Image.H for the interface. 00033 * 00034 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a> 00035 * @date July 3, 2001 15:03 00036 * @since Qgar 1.0 00037 */ 00038 00039 00040 // STL 00041 #include <algorithm> 00042 // QGAR 00043 #include <qgarlib/Dist34BlackCCImage.H> 00044 #include <qgarlib/GenImage.H> 00045 00046 00047 00048 namespace qgar 00049 { 00050 00051 // ------------------------------------------------------------------- 00052 // C O N S T R U C T O R 00053 // ------------------------------------------------------------------- 00054 Dist34BlackCCImage::Dist34BlackCCImage(const BinaryImage& anImg) 00055 00056 : GreyLevelImage(anImg.width(), anImg.height()) 00057 00058 { 00059 const BinaryImage::value_type* pMapImg; 00060 GreyLevelImage::value_type* pMapRes; 00061 00062 // Size of pixel maps 00063 int pixsize = _width * _height; 00064 00065 // Initialization of the result image: a 0 value is needed on the 00066 // borders of the image 00067 00068 // First line 00069 pMapRes = _pPixMap; 00070 for (int iCnt = 0 ; iCnt < _width ; ++iCnt) 00071 { 00072 *pMapRes = 0; 00073 ++pMapRes; 00074 } 00075 00076 // First and last columns 00077 for (int iCnt = 1 ; iCnt < (_height - 1) ; ++iCnt, pMapRes += _width) 00078 { 00079 *pMapRes = 0; 00080 *(pMapRes + _width - 1) = 0; 00081 } 00082 00083 // Last line 00084 for (int iCnt = 0 ; iCnt < _width ; ++iCnt) 00085 { 00086 *pMapRes = 0; 00087 ++pMapRes; 00088 } 00089 00090 // DISTANCE TRANSFORM 00091 00092 // Only the distance transform of the black pixels is computed (check 00093 // according to the original image). Their distance transform value is 00094 // computed according to 4 neighbors, either black or white. The 00095 // direction of processing ensures that pixels are always associated 00096 // to a consistent TD: white pixels are always associated to TD value 0 00097 // (which is neutral for this operation) and black pixels result from 00098 // previous computations. 00099 // 00100 // In the first step, and only in this step, we take advantage of the 00101 // loops to initialize white pixels. 00102 00103 // STEP ONE: up to bottom and left to right 00104 00105 pMapImg = anImg.pPixMap() + _width + 1; 00106 pMapRes = _pPixMap + _width + 1; 00107 00108 for (int iCnt = 1 ; iCnt < (_height - 1) ; ++iCnt) 00109 { 00110 for (int jCnt = 1 ; jCnt < (_width - 1) ; ++jCnt, ++pMapRes, ++pMapImg) 00111 { 00112 if (*pMapImg == QGE_BW_BLACK) 00113 { 00114 *pMapRes = std::min(std::min(*(pMapRes - 1) + 3, 00115 *(pMapRes - _width - 1) + 4), 00116 std::min(*(pMapRes - _width) + 3, 00117 *(pMapRes - _width + 1) + 4)); 00118 } 00119 else // Pixel is white 00120 { 00121 *pMapRes = 0; 00122 } 00123 } // END for jCnt 00124 00125 pMapRes += 2; 00126 pMapImg += 2; 00127 00128 } // END for iCnt 00129 00130 00131 // STEP TWO: bottom to up and right to left 00132 00133 pMapImg = anImg.pPixMap() + pixsize - _width - 2; 00134 pMapRes = _pPixMap + pixsize - _width - 2; 00135 00136 for (int iCnt = _height - 2 ; iCnt > 0 ; --iCnt) 00137 { 00138 for (int jCnt = _width - 2 ; jCnt > 0 ; --jCnt, --pMapRes, -- pMapImg) 00139 { 00140 if (*pMapImg == QGE_BW_BLACK) 00141 { 00142 GreyLevelImage::value_type m 00143 = std::min(std::min(*(pMapRes + 1) + 3, 00144 *(pMapRes + _width + 1) + 4), 00145 std::min(*(pMapRes + _width) + 3, 00146 *(pMapRes + _width - 1) + 4)); 00147 00148 *pMapRes = std::min(m, *pMapRes); 00149 } 00150 } // END for jCnt 00151 00152 pMapRes -= 2; 00153 pMapImg -= 2; 00154 00155 } // END for iCnt 00156 } 00157 // ------------------------------------------------------------------- 00158 00159 } // namespace qgar 00160