原文出處:http://www.ddj.com/184405045?pgno=1

void ScaleLine(PIXEL *Target, PIXEL *Source, int SrcWidth, int TgtWidth)
{
int NumPixels = TgtWidth;
int IntPart = SrcWidth / TgtWidth;
int FractPart = SrcWidth % TgtWidth;
int E = 0;
while (NumPixels-- > 0) {
*Target++ = *Source;
Source += IntPart;

E += FractPart;
if (E >= TgtWidth) {
E -= TgtWidth;
Source++;
} /* if */
} /* while */
}

#define average(a, b) (PIXEL)(( (int)(a) + (int)(b) ) >> 1)
void ScaleLineAvg(PIXEL *Target, PIXEL *Source, int SrcWidth, int TgtWidth)
{
int NumPixels = TgtWidth;
int Mid = TgtWidth / 2;
int E = 0;
PIXEL p;
if (TgtWidth > SrcWidth)
NumPixels--;
while (NumPixels-- > 0) {
p = *Source;
if (E >= Mid)
p = average(p, *(Source+1));
*Target++ = p;
E += SrcWidth;
if (E >= TgtWidth) {
E -= TgtWidth;
Source++;
} /* if */
} /* while */
if (TgtWidth > SrcWidth)
*Target = *Source;
}


void ScaleRect(PIXEL *Target, PIXEL *Source, int SrcWidth, int SrcHeight,
int TgtWidth, int TgtHeight)
{
int NumPixels = TgtHeight;
int IntPart = (SrcHeight / TgtHeight) * SrcWidth;
int FractPart = SrcHeight % TgtHeight;
int E = 0;
PIXEL *PrevSource = NULL;
while (NumPixels-- > 0) {
if (Source == PrevSource) {
memcpy(Target, Target-TgtWidth, TgtWidth*sizeof(*Target));
} else {
ScaleLine(Target, Source, SrcWidth, TgtWidth);
PrevSource = Source;
} /* if */
Target += TgtWidth;
Source += IntPart;
E += FractPart;
if (E >= TgtHeight) {
E -= TgtHeight;
Source += SrcWidth;
} /* if */
} /* while */
}


創作者介紹

邱小新の工作筆記

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()