📄 reducergb.cpp
字号:
//
// ReduceRgb
// Averages rectangular windows in the color 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 "ReduceRgb.h"
#include "Debug.h"
#include "Image.h"
#include "RgbImageIter.h"
using namespace Core;
namespace Algorithm
{
EXPORT_C CReduceRgb* CReduceRgb::NewL(int nWidthFactor, int nHeightFactor)
{
return new (ELeave) CReduceRgb(nWidthFactor, nHeightFactor);
}
CReduceRgb::CReduceRgb(int nWidthFactor, int nHeightFactor) :
ibEmpty(true),
inWidthFactor(nWidthFactor),
inHeightFactor(nHeightFactor)
{
}
CReduceRgb::~CReduceRgb(void)
{
}
CImage CReduceRgb::FrontL()
{
if (ibEmpty) {
User::Leave(KErrGeneral);
return CImage();
}
ibEmpty = true;
return iImage;
}
void CReduceRgb::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, EColor16M));
iImage = CImage(bmp);
iSize = rSize;
}
typedef int (*RgbColArray)[][3];
RgbColArray nColSums = (RgbColArray) new (ELeave) int [nOutRowWidth][3];
Mem::FillZ((TAny*) nColSums, sizeof(int) * nOutRowWidth * 3);
CleanupStack::PushL(nColSums);
image.LockLC();
IRgbImageIter rIn(image);
IRgbImageIter rOut(iImage);
while (!rIn.End()) {
int nRow = 0;
for (; nRow < inHeightFactor; nRow++, rIn.NextRow()) {
int nOutCol = 0;
int nOutCount = 0;
for (; !rIn.REnd(); rIn++) {
int color;
for (color=0; color<3; color++) {
(*nColSums)[nOutCol][color] += rIn[color];
}
if (++nOutCount == inWidthFactor) {
nOutCol++;
nOutCount = 0;
}
}
}
int nCol = 0;
for (; !rOut.REnd(); rOut++, nCol++) {
int color;
for (color=0; color<3; color++) {
rOut[color] = (unsigned char) ((*nColSums)[nCol][color] / (inWidthFactor*inHeightFactor));
(*nColSums)[nCol][color] = 0;
}
}
rOut.NextRow();
}
CleanupStack::PopAndDestroy(); // unlock bitmaps
CDebug::ShowImage(iImage);
CleanupStack::Check(nColSums);
CleanupStack::PopAndDestroy();
ibEmpty = false;
}
bool CReduceRgb::Empty() const
{
return ibEmpty;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -