trapezoidwarp.cpp

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

CPP
144
字号
//
// TrapezoidWarp
//   Wraps a trapezoidal region in an image into a rectangular image.
//   We always return an image as wide as the widest row in the trapezoid
//   so all the calculations can be done using interpolation.
//   The height of the output image is the height of the trapezoid.
//
// Copyright (C) 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 "TrapezoidWarp.h"

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

#include <e32math.h>

using namespace Core;

namespace Algorithm
{

	EXPORT_C CTrapezoidWarp* CTrapezoidWarp::NewL(
		TUint nRowStart, 
		TUint nRowEnd, 
		TUint nColLeftStart, 
		TUint nColRightStart,
		TUint nColLeftEnd, 
		TUint nColRightEnd)
	{	
		CTrapezoidWarp* pMe = new (ELeave) 
			CTrapezoidWarp(nRowStart, nRowEnd, nColLeftStart, nColRightStart, nColLeftEnd, nColRightEnd);
		CleanupStack::PushL(pMe);														
		pMe->ConstructL();																 
		CleanupStack::Pop(pMe);															
		return pMe;																		
	}																					

	CTrapezoidWarp::CTrapezoidWarp(
		TUint nRowStart, 
		TUint nRowEnd, 
		TUint nColLeftStart, 
		TUint nColRightStart,
		TUint nColLeftEnd, 
		TUint nColRightEnd) :
		ibEmpty(true),
		inColLeftEnd(int(nColLeftEnd)),
		inColLeftStart(int(nColLeftStart)),
		inColRightEnd(int(nColRightEnd)),
		inColRightStart(int(nColRightStart)),
		inRowEnd(int(nRowEnd)),
		inRowStart(int(nRowStart))
	{
	}

	void CTrapezoidWarp::ConstructL()
	{
		__ASSERT_DEBUG(
			inColLeftStart < inColRightStart && 
			inColLeftEnd < inColRightEnd &&
			inRowEnd > inRowStart, 
			User::Invariant());
		inHeight = inRowEnd - inRowStart;
		inWidth = Max(inColRightStart - inColLeftStart, inColRightEnd - inColLeftEnd);
		iImage = CImage::NewL(inWidth, inHeight, EGray256);
	}

	CTrapezoidWarp::~CTrapezoidWarp(void)
	{
	}

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

	void CTrapezoidWarp::PushL(CImage image)
	{
		IImageSize size(image);
		// assert trapezoid is inside the input image
		__ASSERT_DEBUG(inColRightStart <= size.Width() &&
			inColRightEnd <= size.Width() &&
			inRowEnd <= size.Height(),
			User::Invariant());
		int i;
		TInt32 fLeft = (inColLeftStart * 256), fRight = (inColRightStart * 256);
		TInt32 fLeftIncr = ((inColLeftEnd - inColLeftStart) * 256) / inHeight;
		TInt32 fRightIncr = ((inColRightEnd - inColRightStart) * 256) / inHeight;
		image.LockLC();
		IGrayImageIter it(iImage); // output iterator
		IGrayIndex index(image);   // input indexer
		for (i=0; i<inHeight; i++, it.NextRow()) {
			int j;
			// we scale everything by 8 bits to avoid floating point
			// calculation
			TInt32 fY = fLeft;
			TInt32 fYIncr = (fRight - fLeft) / inWidth;
			for (j=0; j<inWidth; j++, it++) {
				TInt32 iY = fY / 256;
				// truncate floating-point pixel position
				TInt32 leftPixel = index[i+inRowStart][iY];
				TInt32 rightPixel = index[i+inRowStart][iY+1];
				// calculate fractional component of pixel between left and right
				TInt32 interp = ((rightPixel - leftPixel) * (fY - (iY * 256))) / 256;
				it() = (unsigned char) (leftPixel + interp);
				fY += fYIncr;
			}
			fLeft += fLeftIncr;
			fRight += fRightIncr;
		}
		CleanupStack::PopAndDestroy(); // unlock 
		ibEmpty = false;
	}

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


};

⌨️ 快捷键说明

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