📄 ximaint.cpp
字号:
// xImaInt.cpp : interpolation functions/* 02/2004 - Branko Brevensek * CxImage version 5.99c 17/Oct/2004 - Davide Pizzolato - www.xdp.it */#include "ximage.h"#include "ximath.h"#if CXIMAGE_SUPPORT_INTERPOLATION/////////////////////////////////////////////////////////////////////////////////** * Recalculates coordinates according to specified overflow method. * If pixel (x,y) lies within image, nothing changes. * * \param x, y - coordinates of pixel * \param ofMethod - overflow method * * \return x, y - new coordinates (pixel (x,y) now lies inside image) * * \author ***bd*** 2.2004 */void CxImage::OverflowCoordinates(long &x, long &y, OverflowMethod const ofMethod){ if (IsInside(x,y)) return; //if pixel is within bounds, no change switch (ofMethod) { case OM_REPEAT: //clip coordinates x=max(x,0); x=min(x, head.biWidth-1); y=max(y,0); y=min(y, head.biHeight-1); break; case OM_WRAP: //wrap coordinates x = x % head.biWidth; y = y % head.biHeight; if (x<0) x = head.biWidth + x; if (y<0) y = head.biHeight + y; break; case OM_MIRROR: //mirror pixels near border if (x<0) x=((-x) % head.biWidth); else if (x>=head.biWidth) x=head.biWidth-(x % head.biWidth + 1); if (y<0) y=((-y) % head.biHeight); else if (y>=head.biHeight) y=head.biHeight-(y % head.biHeight + 1); break; default: return; }//switch}/////////////////////////////////////////////////////////////////////////////////** * See OverflowCoordinates for integer version * \author ***bd*** 2.2004 */void CxImage::OverflowCoordinates(float &x, float &y, OverflowMethod const ofMethod){ if (x>=0 && x<head.biWidth && y>=0 && y<head.biHeight) return; //if pixel is within bounds, no change switch (ofMethod) { case OM_REPEAT: //clip coordinates x=max(x,0); x=min(x, head.biWidth-1); y=max(y,0); y=min(y, head.biHeight-1); break; case OM_WRAP: //wrap coordinates x = (float)fmod(x, (float) head.biWidth); y = (float)fmod(y, (float) head.biHeight); if (x<0) x = head.biWidth + x; if (y<0) y = head.biHeight + y; break; case OM_MIRROR: //mirror pixels near border if (x<0) x=(float)fmod(-x, (float) head.biWidth); else if (x>=head.biWidth) x=head.biWidth-((float)fmod(x, (float) head.biWidth) + 1); if (y<0) y=(float)fmod(-y, (float) head.biHeight); else if (y>=head.biHeight) y=head.biHeight-((float)fmod(y, (float) head.biHeight) + 1); break; default: return; }//switch}/////////////////////////////////////////////////////////////////////////////////** * Method return pixel color. Different methods are implemented for out of bounds pixels. * If an image has alpha channel, alpha value is returned in .RGBReserved. * * \param x,y : pixel coordinates * \param ofMethod : out-of-bounds method: * - OF_WRAP - wrap over to pixels on other side of the image * - OF_REPEAT - repeat last pixel on the edge * - OF_COLOR - return input value of color * - OF_BACKGROUND - return background color (if not set, return input color) * - OF_TRANSPARENT - return transparent pixel * * \param rplColor : input color (returned for out-of-bound coordinates in OF_COLOR mode and if other mode is not applicable) * * \return color : color of pixel * \author ***bd*** 2.2004 */RGBQUAD CxImage::GetPixelColorWithOverflow(long x, long y, OverflowMethod const ofMethod, RGBQUAD* const rplColor){ RGBQUAD color; //color to return if ((!IsInside(x,y)) || pDib==NULL) { //is pixel within bouns?: //pixel is out of bounds or no DIB if (rplColor!=NULL) color=*rplColor; else { color.rgbRed=color.rgbGreen=color.rgbBlue=255; color.rgbReserved=0; //default replacement colour: white transparent }//if if (pDib==NULL) return color; //pixel is out of bounds: switch (ofMethod) { case OM_TRANSPARENT:#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) { //alpha transparency is supported and image has alpha layer color.rgbReserved=0; } else {#endif //CXIMAGE_SUPPORT_ALPHA //no alpha transparency if (GetTransIndex()>=0) { color=GetTransColor(); //single color transparency enabled (return transparent color) }//if#if CXIMAGE_SUPPORT_ALPHA }//if#endif //CXIMAGE_SUPPORT_ALPHA return color; case OM_BACKGROUND: //return background color (if it exists, otherwise input value) if (info.nBkgndIndex != -1) { if (head.biBitCount<24) color = GetPaletteColor((BYTE)info.nBkgndIndex); else color = info.nBkgndColor; }//if return color; case OM_REPEAT: case OM_WRAP: case OM_MIRROR: OverflowCoordinates(x,y,ofMethod); break; default: //simply return replacement color (OM_COLOR and others) return color; }//switch }//if //just return specified pixel (it's within bounds) return BlindGetPixelColor(x,y);}/////////////////////////////////////////////////////////////////////////////////** * This method reconstructs image according to chosen interpolation method and then returns pixel (x,y). * (x,y) can lie between actual image pixels. If (x,y) lies outside of image, method returns value * according to overflow method. * This method is very useful for geometrical image transformations, where destination pixel * can often assume color value lying between source pixels. * * \param (x,y) - coordinates of pixel to return * GPCI method recreates "analogue" image back from digital data, so x and y * are float values and color value of point (1.1,1) will generally not be same * as (1,1). Center of first pixel is at (0,0) and center of pixel right to it is (1,0). * (0.5,0) is half way between these two pixels. * \param inMethod - interpolation (reconstruction) method (kernel) to use: * - IM_NEAREST_NEIGHBOUR - returns colour of nearest lying pixel (causes stairy look of * processed images) * - IM_BILINEAR - interpolates colour from four neighbouring pixels (softens image a bit) * - IM_BICUBIC - interpolates from 16 neighbouring pixels (can produce "halo" artifacts) * - IM_BICUBIC2 - interpolates from 16 neighbouring pixels (perhaps a bit less halo artifacts than IM_BICUBIC) * - IM_BSPLINE - interpolates from 16 neighbouring pixels (softens image, washes colours) * (As far as I know, image should be prefiltered for this method to give * good results... some other time :) ) * This method uses bicubic interpolation kernel from CXImage 5.99a and older * versions. * - IM_LANCZOS - interpolates from 12*12 pixels (slow, ringing artifacts) * * \param ofMethod - overflow method (see comments at GetPixelColorWithOverflow) * \param rplColor - pointer to color used for out of borders pixels in OM_COLOR mode * (and other modes if colour can't calculated in a specified way) * * \return interpolated color value (including interpolated alpha value, if image has alpha layer) * * \author ***bd*** 2.2004 */RGBQUAD CxImage::GetPixelColorInterpolated( float x,float y, InterpolationMethod const inMethod, OverflowMethod const ofMethod, RGBQUAD* const rplColor){ //calculate nearest pixel int xi=(int)(x); if (x<0) xi--; //these replace (incredibly slow) floor (Visual c++ 2003, AMD Athlon) int yi=(int)(y); if (y<0) yi--; RGBQUAD color; //calculated colour switch (inMethod) { case IM_NEAREST_NEIGHBOUR: return GetPixelColorWithOverflow((long)(x+0.5f), (long)(y+0.5f), ofMethod, rplColor); default: { //bilinear interpolation if (xi<-1 || xi>=head.biWidth || yi<-1 || yi>=head.biHeight) { //all 4 points are outside bounds?: switch (ofMethod) { case OM_COLOR: case OM_TRANSPARENT: case OM_BACKGROUND: //we don't need to interpolate anything with all points outside in this case return GetPixelColorWithOverflow(-999, -999, ofMethod, rplColor); default: //recalculate coordinates and use faster method later on OverflowCoordinates(x,y,ofMethod); xi=(int)(x); if (x<0) xi--; //x and/or y have changed ... recalculate xi and yi yi=(int)(y); if (y<0) yi--; }//switch }//if //get four neighbouring pixels if ((xi+1)<head.biWidth && xi>=0 && (yi+1)<head.biHeight && yi>=0 && head.biClrUsed==0) { //all pixels are inside RGB24 image... optimize reading (and use fixed point arithmetic) WORD wt1=(WORD)((x-xi)*256.0f), wt2=(WORD)((y-yi)*256.0f); WORD wd=wt1*wt2>>8; WORD wb=wt1-wd; WORD wc=wt2-wd; WORD wa=256-wt1-wc; WORD wrr,wgg,wbb; BYTE *pxptr=(BYTE*)info.pImage+yi*info.dwEffWidth+xi*3; wbb=wa*(*pxptr++); wgg=wa*(*pxptr++); wrr=wa*(*pxptr++); wbb+=wb*(*pxptr++); wgg+=wb*(*pxptr++); wrr+=wb*(*pxptr); pxptr+=(info.dwEffWidth-5); //move to next row wbb+=wc*(*pxptr++); wgg+=wc*(*pxptr++); wrr+=wc*(*pxptr++); wbb+=wd*(*pxptr++); wgg+=wd*(*pxptr++); wrr+=wd*(*pxptr); color.rgbRed=(BYTE) (wrr>>8); color.rgbGreen=(BYTE) (wgg>>8); color.rgbBlue=(BYTE) (wbb>>8);#if CXIMAGE_SUPPORT_ALPHA if (pAlpha) { WORD waa; //image has alpha layer... we have to do the same for alpha data pxptr=AlphaGetPointer(xi,yi); //pointer to first byte waa=wa*(*pxptr++); waa+=wb*(*pxptr); //first two pixels pxptr+=(head.biWidth-1); //move to next row waa+=wc*(*pxptr++); waa+=wd*(*pxptr); //and second row pixels color.rgbReserved=(BYTE) (waa>>8); } else#endif { //Alpha not supported or no alpha at all color.rgbReserved = 0; } return color; } else { //default (slower) way to get pixels (not RGB24 or some pixels out of borders) float t1=x-xi, t2=y-yi; float d=t1*t2; float b=t1-d; float c=t2-d; float a=1-t1-c; RGBQUAD rgb11,rgb21,rgb12,rgb22; rgb11=GetPixelColorWithOverflow(xi, yi, ofMethod, rplColor); rgb21=GetPixelColorWithOverflow(xi+1, yi, ofMethod, rplColor); rgb12=GetPixelColorWithOverflow(xi, yi+1, ofMethod, rplColor); rgb22=GetPixelColorWithOverflow(xi+1, yi+1, ofMethod, rplColor); //calculate linear interpolation color.rgbRed=(BYTE) (a*rgb11.rgbRed+b*rgb21.rgbRed+c*rgb12.rgbRed+d*rgb22.rgbRed); color.rgbGreen=(BYTE) (a*rgb11.rgbGreen+b*rgb21.rgbGreen+c*rgb12.rgbGreen+d*rgb22.rgbGreen); color.rgbBlue=(BYTE) (a*rgb11.rgbBlue+b*rgb21.rgbBlue+c*rgb12.rgbBlue+d*rgb22.rgbBlue);#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) color.rgbReserved=(BYTE) (a*rgb11.rgbReserved+b*rgb21.rgbReserved+c*rgb12.rgbReserved+d*rgb22.rgbReserved); else#endif { //Alpha not supported or no alpha at all color.rgbReserved = 0; } return color; }//if }//default case IM_BICUBIC: case IM_BICUBIC2: case IM_BSPLINE: case IM_BOX: case IM_HERMITE: case IM_HAMMING: case IM_SINC: case IM_BLACKMAN: case IM_BESSEL: case IM_GAUSSIAN: case IM_QUADRATIC: case IM_MITCHELL: case IM_CATROM: //bicubic interpolation(s) if (((xi+2)<0) || ((xi-1)>=head.biWidth) || ((yi+2)<0) || ((yi-1)>=head.biHeight)) { //all points are outside bounds?: switch (ofMethod) { case OM_COLOR: case OM_TRANSPARENT: case OM_BACKGROUND: //we don't need to interpolate anything with all points outside in this case return GetPixelColorWithOverflow(-999, -999, ofMethod, rplColor); break; default: //recalculate coordinates and use faster method later on OverflowCoordinates(x,y,ofMethod); xi=(int)(x); if (x<0) xi--; //x and/or y have changed ... recalculate xi and yi yi=(int)(y); if (y<0) yi--; }//switch }//if //some variables needed from here on int xii,yii; //x any y integer indexes for loops float kernel, kernelyc; //kernel cache float kernelx[12], kernely[4]; //precalculated kernel values float rr,gg,bb,aa; //accumulated color values //calculate multiplication factors for all pixels int i; switch (inMethod) { case IM_BICUBIC: for (i=0; i<4; i++) { kernelx[i]=KernelCubic((float)(xi+i-1-x)); kernely[i]=KernelCubic((float)(yi+i-1-y)); }//for i break; case IM_BICUBIC2: for (i=0; i<4; i++) { kernelx[i]=KernelGeneralizedCubic((float)(xi+i-1-x), -0.5); kernely[i]=KernelGeneralizedCubic((float)(yi+i-1-y), -0.5); }//for i break; case IM_BSPLINE: for (i=0; i<4; i++) { kernelx[i]=KernelBSpline((float)(xi+i-1-x)); kernely[i]=KernelBSpline((float)(yi+i-1-y)); }//for i break; case IM_BOX: for (i=0; i<4; i++) { kernelx[i]=KernelBox((float)(xi+i-1-x)); kernely[i]=KernelBox((float)(yi+i-1-y)); }//for i break; case IM_HERMITE: for (i=0; i<4; i++) { kernelx[i]=KernelHermite((float)(xi+i-1-x)); kernely[i]=KernelHermite((float)(yi+i-1-y)); }//for i break; case IM_HAMMING: for (i=0; i<4; i++) { kernelx[i]=KernelHamming((float)(xi+i-1-x)); kernely[i]=KernelHamming((float)(yi+i-1-y)); }//for i break; case IM_SINC:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -