histmod.cpp

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

CPP
124
字号
//
// Histogram modification
//
// 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 "HistMod.h"

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

#include <e32math.h>

using namespace Core;

namespace Algorithm
{
	EXPORT_C CHistMod* CHistMod::NewL(const float* fTargetHist)
	{	
		return new (ELeave) CHistMod(fTargetHist);	
	}

	CHistMod::CHistMod(const float* fTargetHist) :
		ibEmpty(true)
	{
        Mem::Copy(&ifTargetHist[0], &fTargetHist[0], 256 * sizeof(float));
	}

	CHistMod::~CHistMod(void)
	{
	}

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

	void CHistMod::PushL(CImage image)
	{
		IImageSize rSize(image);
		int i=0;
        if (rSize != iSize) {
			// recalculate the cumulative target histogram. This is a count of number of
			// pixels, not a frequency distribution
            for (i=0; i<256; i++) {
				// scale by number of pixels in the image
				Math::Int(inHistTarg[i], rSize.Width() * rSize.Height() * ifTargetHist[i]);
            }
			// transform into a cumulative histogram
            for (i=1; i<256; i++) {
                inHistTarg[i] += inHistTarg[i-1];
            }
            iSize = rSize;
        }
		int nHist[256] = {0};
		// build a histogram
		image.LockLC();
		IGrayImageIter rGray(image);
		for (; !rGray.End(); rGray.NextRow()) {
			for (; !rGray.REnd(); rGray++) {
				nHist[rGray()] ++;
			}
		}
		CleanupStack::PopAndDestroy(); // unlock bitmaps
        // change the image histogram into a cumulative histogram
        for (i=1; i<256; i++) {
            nHist[i] += nHist[i-1];
        }
		// use the cumulative distribution to make a lookup table
		unsigned char ucLut[256];
        int j = 0;
		for (i=0; i<256; i++) {
            while (inHistTarg[j] < nHist[i]) {
                j++;
            }
			if (j<256) {
                ucLut[i] = (unsigned char) j;
			} else {
				ucLut[i] = 255;
			}
		}
		// apply the lookup table
		image.LockLC();
		for (rGray = IGrayImageIter(image); // reinitialize in case bitmap moved
			!rGray.End(); 
			rGray.NextRow()) {
			for (; !rGray.REnd(); rGray++) {
				rGray() = ucLut[rGray()];
			}
		}	
		CleanupStack::PopAndDestroy(); // unlock bitmaps
		iImage = image;
		ibEmpty = false;
		CDebug::ShowImage(iImage);
	}

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

⌨️ 快捷键说明

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