📄 ximaint.cpp
字号:
for (i=0; i<4; i++) { kernelx[i]=KernelSinc((float)(xi+i-1-x)); kernely[i]=KernelSinc((float)(yi+i-1-y)); }//for i break; case IM_BLACKMAN: for (i=0; i<4; i++) { kernelx[i]=KernelBlackman((float)(xi+i-1-x)); kernely[i]=KernelBlackman((float)(yi+i-1-y)); }//for i break; case IM_BESSEL: for (i=0; i<4; i++) { kernelx[i]=KernelBessel((float)(xi+i-1-x)); kernely[i]=KernelBessel((float)(yi+i-1-y)); }//for i break; case IM_GAUSSIAN: for (i=0; i<4; i++) { kernelx[i]=KernelGaussian((float)(xi+i-1-x)); kernely[i]=KernelGaussian((float)(yi+i-1-y)); }//for i break; case IM_QUADRATIC: for (i=0; i<4; i++) { kernelx[i]=KernelQuadratic((float)(xi+i-1-x)); kernely[i]=KernelQuadratic((float)(yi+i-1-y)); }//for i break; case IM_MITCHELL: for (i=0; i<4; i++) { kernelx[i]=KernelMitchell((float)(xi+i-1-x)); kernely[i]=KernelMitchell((float)(yi+i-1-y)); }//for i break; case IM_CATROM: for (i=0; i<4; i++) { kernelx[i]=KernelCatrom((float)(xi+i-1-x)); kernely[i]=KernelCatrom((float)(yi+i-1-y)); }//for i break; }//switch rr=gg=bb=aa=0; if (((xi+2)<head.biWidth) && xi>=1 && ((yi+2)<head.biHeight) && (yi>=1) && !IsIndexed()) { //optimized interpolation (faster pixel reads) for RGB24 images with all pixels inside bounds BYTE *pxptr, *pxptra; for (yii=yi-1; yii<yi+3; yii++) { pxptr=(BYTE *)BlindGetPixelPointer(xi-1, yii); //calculate pointer to first byte in row kernelyc=kernely[yii-(yi-1)];#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) { //alpha is supported and valid (optimized bicubic int. for image with alpha) pxptra=AlphaGetPointer(xi-1, yii); kernel=kernelyc*kernelx[0]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++); kernel=kernelyc*kernelx[1]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++); kernel=kernelyc*kernelx[2]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++); kernel=kernelyc*kernelx[3]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr); aa+=kernel*(*pxptra); } else#endif //alpha not supported or valid (optimized bicubic int. for no alpha channel) { kernel=kernelyc*kernelx[0]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); kernel=kernelyc*kernelx[1]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); kernel=kernelyc*kernelx[2]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); kernel=kernelyc*kernelx[3]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr); } }//yii } else { //slower more flexible interpolation for border pixels and paletted images RGBQUAD rgbs; for (yii=yi-1; yii<yi+3; yii++) { kernelyc=kernely[yii-(yi-1)]; for (xii=xi-1; xii<xi+3; xii++) { kernel=kernelyc*kernelx[xii-(xi-1)]; rgbs=GetPixelColorWithOverflow(xii, yii, ofMethod, rplColor); rr+=kernel*rgbs.rgbRed; gg+=kernel*rgbs.rgbGreen; bb+=kernel*rgbs.rgbBlue;#if CXIMAGE_SUPPORT_ALPHA aa+=kernel*rgbs.rgbReserved;#endif }//xii }//yii }//if //for all colors, clip to 0..255 and assign to RGBQUAD if (rr>255) rr=255; if (rr<0) rr=0; color.rgbRed=(BYTE) rr; if (gg>255) gg=255; if (gg<0) gg=0; color.rgbGreen=(BYTE) gg; if (bb>255) bb=255; if (bb<0) bb=0; color.rgbBlue=(BYTE) bb;#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) { if (aa>255) aa=255; if (aa<0) aa=0; color.rgbReserved=(BYTE) aa; } else#endif { //Alpha not supported or no alpha at all color.rgbReserved = 0; } return color; case IM_LANCZOS: //lanczos window (16*16) sinc interpolation if (((xi+6)<0) || ((xi-5)>=head.biWidth) || ((yi+6)<0) || ((yi-5)>=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 for (xii=xi-5; xii<xi+7; xii++) kernelx[xii-(xi-5)]=KernelLanczosSinc((float)(xii-x), 6.0f); rr=gg=bb=aa=0; if (((xi+6)<head.biWidth) && ((xi-5)>=0) && ((yi+6)<head.biHeight) && ((yi-5)>=0) && !IsIndexed()) { //optimized interpolation (faster pixel reads) for RGB24 images with all pixels inside bounds BYTE *pxptr, *pxptra; for (yii=yi-5; yii<yi+7; yii++) { pxptr=(BYTE *)BlindGetPixelPointer(xi-5, yii); //calculate pointer to first byte in row kernelyc=KernelLanczosSinc((float)(yii-y),6.0f);#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) { //alpha is supported and valid pxptra=AlphaGetPointer(xi-1, yii); for (xii=0; xii<12; xii++) { kernel=kernelyc*kernelx[xii]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++); }//for xii } else#endif //alpha not supported or valid { for (xii=0; xii<12; xii++) { kernel=kernelyc*kernelx[xii]; bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); }//for xii } }//yii } else { //slower more flexible interpolation for border pixels and paletted images RGBQUAD rgbs; for (yii=yi-5; yii<yi+7; yii++) { kernelyc=KernelLanczosSinc((float)(yii-y),6.0f); for (xii=xi-5; xii<xi+7; xii++) { kernel=kernelyc*kernelx[xii-(xi-5)]; rgbs=GetPixelColorWithOverflow(xii, yii, ofMethod, rplColor); rr+=kernel*rgbs.rgbRed; gg+=kernel*rgbs.rgbGreen; bb+=kernel*rgbs.rgbBlue;#if CXIMAGE_SUPPORT_ALPHA aa+=kernel*rgbs.rgbReserved;#endif }//xii }//yii }//if //for all colors, clip to 0..255 and assign to RGBQUAD if (rr>255) rr=255; if (rr<0) rr=0; color.rgbRed=(BYTE) rr; if (gg>255) gg=255; if (gg<0) gg=0; color.rgbGreen=(BYTE) gg; if (bb>255) bb=255; if (bb<0) bb=0; color.rgbBlue=(BYTE) bb;#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) { if (aa>255) aa=255; if (aa<0) aa=0; color.rgbReserved=(BYTE) aa; } else#endif { //Alpha not supported or no alpha at all color.rgbReserved = 0; } return color; }//switch}/////////////////////////////////////////////////////////////////////////////////** * Helper function for GetAreaColorInterpolated. * Adds 'surf' portion of image pixel with color 'color' to (rr,gg,bb,aa). */void CxImage::AddAveragingCont(RGBQUAD const &color, float const surf, float &rr, float &gg, float &bb, float &aa){ rr+=color.rgbRed*surf; gg+=color.rgbGreen*surf; bb+=color.rgbBlue*surf;#if CXIMAGE_SUPPORT_ALPHA aa+=color.rgbReserved*surf;#endif}/////////////////////////////////////////////////////////////////////////////////** * This method is similar to GetPixelColorInterpolated, but this method also properly handles * subsampling. * If you need to sample original image with interval of more than 1 pixel (as when shrinking an image), * you should use this method instead of GetPixelColorInterpolated or aliasing will occur. * When area width and height are both less than pixel, this method gets pixel color by interpolating * color of frame center with selected (inMethod) interpolation by calling GetPixelColorInterpolated. * If width and height are more than 1, method calculates color by averaging color of pixels within area. * Interpolation method is not used in this case. Pixel color is interpolated by averaging instead. * If only one of both is more than 1, method uses combination of interpolation and averaging. * Chosen interpolation method is used, but since it is averaged later on, there is little difference * between IM_BILINEAR (perhaps best for this case) and better methods. IM_NEAREST_NEIGHBOUR again * leads to aliasing artifacts. * This method is a bit slower than GetPixelColorInterpolated and when aliasing is not a problem, you should * simply use the later. * * \param xc, yc - center of (rectangular) area * \param w, h - width and height of area * \param inMethod - interpolation method that is used, when interpolation is used (see above) * \param ofMethod - overflow method used when retrieving individual pixel colors * \param rplColor - replacement colour to use, in OM_COLOR * * \author ***bd*** 2.2004 */RGBQUAD CxImage::GetAreaColorInterpolated( float const xc, float const yc, float const w, float const h, InterpolationMethod const inMethod, OverflowMethod const ofMethod, RGBQUAD* const rplColor){ RGBQUAD color; //calculated colour if (h<=1 && w<=1) { //both width and height are less than one... we will use interpolation of center point return GetPixelColorInterpolated(xc, yc, inMethod, ofMethod, rplColor); } else { //area is wider and/or taller than one pixel: CxRect2 area(xc-w/2.0f, yc-h/2.0f, xc+w/2.0f, yc+h/2.0f); //area int xi1=(int)(area.botLeft.x+0.49999999f); //low x int yi1=(int)(area.botLeft.y+0.49999999f); //low y int xi2=(int)(area.topRight.x+0.5f); //top x int yi2=(int)(area.topRight.y+0.5f); //top y (for loops) float rr,gg,bb,aa; //red, green, blue and alpha components rr=gg=bb=aa=0; int x,y; //loop counters float s=0; //surface of all pixels float cps; //surface of current crosssection if (h>1 && w>1) { //width and height of area are greater than one pixel, so we can employ "ordinary" averaging CxRect2 intBL, intTR; //bottom left and top right intersection intBL=area.CrossSection(CxRect2(((float)xi1)-0.5f, ((float)yi1)-0.5f, ((float)xi1)+0.5f, ((float)yi1)+0.5f)); intTR=area.CrossSection(CxRect2(((float)xi2)-0.5f, ((float)yi2)-0.5f, ((float)xi2)+0.5f, ((float)yi2)+0.5f)); float wBL, wTR, hBL, hTR; wBL=intBL.Width(); //width of bottom left pixel-area intersection hBL=intBL.Height(); //height of bottom left... wTR=intTR.Width(); //width of top right... hTR=intTR.Height(); //height of top right... AddAveragingCont(GetPixelColorWithOverflow(xi1,yi1,ofMethod,rplColor), wBL*hBL, rr, gg, bb, aa); //bottom left pixel AddAveragingCont(GetPixelColorWithOverflow(xi2,yi1,ofMethod,rplColor), wTR*hBL, rr, gg, bb, aa); //bottom right pixel AddAveragingCont(GetPixelColorWithOverflow(xi1,yi2,ofMethod,rplColor), wBL*hTR, rr, gg, bb, aa); //top left pixel AddAveragingCont(GetPixelColorWithOverflow(xi2,yi2,ofMethod,rplColor), wTR*hTR, rr, gg, bb, aa); //top right pixel //bottom and top row for (x=xi1+1; x<xi2; x++) { AddAveragingCont(GetPixelColorWithOverflow(x,yi1,ofMethod,rplColor), hBL, rr, gg, bb, aa); //bottom row AddAveragingCont(GetPixelColorWithOverflow(x,yi2,ofMethod,rplColor), hTR, rr, gg, bb, aa); //top row } //leftmost and rightmost column for (y=yi1+1; y<yi2; y++) { AddAveragingCont(GetPixelColorWithOverflow(xi1,y,ofMethod,rplColor), wBL, rr, gg, bb, aa); //left column AddAveragingCont(GetPixelColorWithOverflow(xi2,y,ofMethod,rplColor), wTR, rr, gg, bb, aa); //right column } for (y=yi1+1; y<yi2; y++) { for (x=xi1+1; x<xi2; x++) { color=GetPixelColorWithOverflow(x,y,ofMethod,rplColor); rr+=color.rgbRed; gg+=color.rgbGreen; bb+=color.rgbBlue;#if CXIMAGE_SUPPORT_ALPHA aa+=color.rgbReserved;#endif }//for x }//for y } else { //width or height greater than one: CxRect2 intersect; //intersection with current pixel CxPoint2 center; for (y=yi1; y<=yi2; y++) { for (x=xi1; x<=xi2; x++) { intersect=area.CrossSection(CxRect2(((float)x)-0.5f, ((float)y)-0.5f, ((float)x)+0.5f, ((float)y)+0.5f)); center=intersect.Center(); color=GetPixelColorInterpolated(center.x, center.y, inMethod, ofMethod, rplColor); cps=intersect.Surface(); rr+=color.rgbRed*cps; gg+=color.rgbGreen*cps; bb+=color.rgbBlue*cps;#if CXIMAGE_SUPPORT_ALPHA aa+=color.rgbReserved*cps;#endif }//for x }//for y }//if s=area.Surface(); rr/=s; gg/=s; bb/=s; aa/=s; if (rr>255) rr=255; if (rr<0) rr=0; color.rgbRed=(BYTE) rr; if (gg>255) gg=255; if (gg<0) gg=0; color.rgbGreen=(BYTE) gg; if (bb>255) bb=255; if (bb<0) bb=0; color.rgbBlue=(BYTE) bb;#if CXIMAGE_SUPPORT_ALPHA if (AlphaIsValid()) { if (aa>255) aa=255; if (aa<0) aa=0; color.rgbReserved=(BYTE) aa; }//if#endif }//if return color;}////////////////////////////////////////////////////////////////////////////////float CxImage::KernelBSpline(const float x){ if (x>2.0f) return 0.0f; // thanks to Kristian Kratzenstein float a, b, c, d; float xm1 = x - 1.0f; // Was calculatet anyway cause the "if((x-1.0f) < 0)" float xp1 = x + 1.0f; float xp2 = x + 2.0f; if ((xp2) <= 0.0f) a = 0.0f; else a = xp2*xp2*xp2; // Only float, not float -> double -> float if ((xp1) <= 0.0f) b = 0.0f; else b = xp1*xp1*xp1; if (x <= 0) c = 0.0f; else c = x*x*x; if ((xm1) <= 0.0f) d = 0.0f; else d = xm1*xm1*xm1; return (0.16666666666666666667f * (a - (4.0f * b) + (6.0f * c) - (4.0f * d))); /* equivalent <Vladim韗 Kloucek> if (x < -2.0) return(0.0f); if (x < -1.0) return((2.0f+x)*(2.0f+x)*(2.0f+x)*0.16666666666666666667f); if (x < 0.0) return((4.0f+x*x*(-6.0f-3.0f*x))*0.16666666666666666667f); if (x < 1.0) return((4.0f+x*x*(-6.0f+3.0f*x))*0.16666666666666666667f); if (x < 2.0) return((2.0f-x)*(2.0f-x)*(2.0f-x)*0.16666666666666666667f);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -