reduce.cpp

来自「barcode readers [ from Image]」· C++ 代码 · 共 121 行

CPP
121
字号
//
// Reduce
//   Averages rectangular windows in the image to produce an image of smaller
//   size. The reduction factor must always divide the image size, otherwise
//   an error results.
//
// Copyright (C) 2003, 2006 by Jon A. Webb (Contact via GMail; username is jonawebb)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//


#include "Reduce.h"

#include "Debug.h"
#include "GrayImageIter.h"
#include "Image.h"

using namespace Core;

namespace Algorithm
{

	EXPORT_C CReduce* CReduce::NewL(int nWidthFactor, int nHeightFactor)
	{	
		return new (ELeave) CReduce(nWidthFactor, nHeightFactor);	
	}

	CReduce::CReduce(int nWidthFactor, int nHeightFactor) :
		ibEmpty(true),
		inWidthFactor(nWidthFactor),
		inHeightFactor(nHeightFactor)
	{
	}

	CReduce::~CReduce(void)
	{
	}

	CImage CReduce::FrontL() 
	{
		if (ibEmpty) {
			User::Leave(KErrGeneral);
			return CImage();
		}
		ibEmpty = true;
		return iImage;
	}

	void CReduce::PushL(CImage image)
	{
		IImageSize rSize(image);
		int nOutRowWidth = rSize.Width()/inWidthFactor;
		if (rSize != iSize) {
			if (rSize.Width() % inWidthFactor != 0 ||
				rSize.Height() % inHeightFactor != 0) {
					User::Leave(KErrNotSupported);
				}
			CFbsBitmap bmp;
			TSize sizeReduced(nOutRowWidth, rSize.Height()/inHeightFactor);
			User::LeaveIfError(bmp.Create(sizeReduced, EGray256));
			iImage = CImage(bmp);
			iSize = rSize;
		}

		int *nColSums = new (ELeave) int [nOutRowWidth];
		Mem::FillZ((TAny*) nColSums, sizeof(int) * nOutRowWidth);

		CleanupStack::PushL(nColSums);

		image.LockLC();
		IGrayImageIter rIn(image);
		IGrayImageIter rOut(iImage);

		while (!rIn.End()) {
			int nRow = 0;
			for (; nRow < inHeightFactor; nRow++, rIn.NextRow()) {
				int nOutCol = 0;
				int nOutCount = 0;
				for (; !rIn.REnd(); rIn++) {
					nColSums[nOutCol] += rIn();
					if (++nOutCount == inWidthFactor) {
						nOutCol++;
						nOutCount = 0;
					}
				}
			}
			int nCol = 0;
			for (; !rOut.REnd(); rOut++, nCol++) {
				rOut() = (unsigned char) (nColSums[nCol] / (inWidthFactor*inHeightFactor));
				nColSums[nCol] = 0;
			}
			rOut.NextRow();
		}
		CleanupStack::PopAndDestroy(); // unlock bitmaps
		CDebug::ShowImage(iImage);
		CleanupStack::Check(nColSums);
		CleanupStack::PopAndDestroy();
		ibEmpty = false;
	}

	bool CReduce::Empty() const
	{
		return ibEmpty;
	}


};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?