📄 warpipl.cpp
字号:
#include "stdafx.h"#include "ipl.h"#include <xmmintrin.h>#include <math.h>BOOL Rotate2DImage(unsigned short *pSrc, unsigned short *pDst, int width, int height, int degree){ ASSERT(width % 4 == 0); IplImage* srcImage; IplImage* dstImage; srcImage = iplCreateImageHeader( 1, 0, IPL_DEPTH_16U, // data of byte type "Gray", "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 1, 0, IPL_DEPTH_16U, // data of byte type "Gray", "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, height, // image height NULL, NULL, NULL, NULL); // not tiled srcImage->imageData = (char *)pSrc; dstImage->imageData = (char *)pDst; const double coeffs[2][3]={{cos(degree), -sin(degree), 0}, {sin(degree), cos(degree), 0}}; iplWarpAffine(srcImage, dstImage, coeffs, IPL_INTER_LINEAR); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER ); return TRUE;}BOOL GetDIBColor(unsigned short *pSrc, unsigned int *pDst, __m128 *pPallete, int nPixelNum, float fWeight){ __m128 vScale = _mm_set_ps1(255.0f * fWeight); for(int i=0; i<nPixelNum; i++) { __m128 vColor = pPallete[pSrc[i]]; vColor = _mm_mul_ps(vColor, vScale); __m64 v64Color = _mm_packs_pu16(_mm_cvtps_pi16(vColor), _mm_setzero_si64()); unsigned int iABGRColor = (unsigned int)_mm_cvtsi64_si32(v64Color); unsigned int iBGRAColor = (iABGRColor << 24) | (iABGRColor >> 8); // don't care alpha pDst[i] = iBGRAColor; } _mm_empty(); return TRUE;}BOOL AddConstant(char* buffer, int width, int height, int offset, char *obuffer){ int iSize = width * height * sizeof(short), i, iMMXEffective; iMMXEffective = iSize >> 3; __m64 *pvSrc, *pvDst, vConstant; pvSrc = (__m64*)buffer; pvDst = (__m64*)obuffer; vConstant = _mm_set1_pi16((short)offset); for (i = 0; i < iMMXEffective; i++) pvDst[i] = _mm_add_pi16(vConstant, pvSrc[i]); _mm_empty(); for (i = iMMXEffective << 3; i < iSize; i += 2) *(short*)(obuffer + i) = *(short*)(buffer + i) + offset; return TRUE;}// assert 曼绊void ImageFiltering(char *src, char *dst, int width, int height, int level){ ASSERT(src && dst); ASSERT(src != dst); ASSERT(width % 4 == 0); ASSERT(0 <= level && level < 4); if(level==0) { memcpy(dst, src, width*height*sizeof(short)); return; } IplImage *srcImage; IplImage *dstImage; srcImage = iplCreateImageHeader( 1, 0, IPL_DEPTH_16U, // data of byte type "Gray", "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 1, 0, IPL_DEPTH_16U, // data of byte type "Gray", "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, height, // image height NULL, NULL, NULL, NULL); // not tiled srcImage->imageData = src; dstImage->imageData = dst; iplSetBorderMode( srcImage, IPL_BORDER_REPLICATE, IPL_SIDE_ALL, 0); iplSetBorderMode( dstImage, IPL_BORDER_REPLICATE, IPL_SIDE_ALL, 0); iplBlur(srcImage, dstImage, level*2+1, level*2+1, level, level); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER );}void Subtract(char *src1, char *src2, char *dst, int width, int height, int type){ ASSERT(type==8 || type==16); int depth = (type==8)? IPL_DEPTH_8U : IPL_DEPTH_16S; IplImage *srcImage1; IplImage *srcImage2; IplImage *dstImage; srcImage1 = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel depth, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, // image width height, // image height NULL, NULL, NULL, NULL); // not tiled srcImage2 = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel depth, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, // image width height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel depth, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align width, // image width height, // image height NULL, NULL, NULL, NULL); // not tiled if( NULL == srcImage1 ) return; srcImage1->imageData = src1; if( NULL == srcImage2 ) return; srcImage2->imageData = src2; if( NULL == dstImage ) return; dstImage->imageData = dst; iplSubtract(srcImage1, srcImage2, dstImage); iplAbs(dstImage,dstImage); iplDeallocate( srcImage1, IPL_IMAGE_HEADER ); iplDeallocate( srcImage2, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER );}void Crop(BYTE *in_image, int in_width, int in_height, int in_xoffset, int in_yoffset, BYTE *out_image, int out_width, int out_height, int out_xoffset, int out_yoffset, int width, int height, int color){ IplImage *srcImage; IplImage *dstImage; IplROI SrcROI, DstROI; iplSetROI(&SrcROI, 0, in_xoffset, in_height-height-in_yoffset, width, height); iplSetROI(&DstROI, 0, out_xoffset, out_height-height-out_yoffset, width, height); if(color == 4) {// ASSERT(in_width % 2 == 0 && out_width % 2 == 0); srcImage = iplCreateImageHeader( 4, // number of channels 4, // no alpha channel IPL_DEPTH_8U, // data of byte type "RGBA", // color model "BGRA", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 8 bytes align in_width, // image width in_height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 4, // number of channels 4, // no alpha channel IPL_DEPTH_8U, // data of byte type "RGBA", // color model "BGRA", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 8 bytes align out_width, // image width out_height, // image height NULL, NULL, NULL, NULL); // not tiled } if( NULL == srcImage ) return; srcImage->imageData = (char*)in_image; if( NULL == dstImage ) return; dstImage->imageData = (char*)out_image; iplSet(dstImage, 0); srcImage->roi = &SrcROI; dstImage->roi = &DstROI; iplCopy(srcImage, dstImage); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER );}void Zoom(BYTE *in_image, int in_width, int in_height, unsigned char *out_image, int out_width, int out_height, int color){ IplImage *srcImage; IplImage *dstImage; if(color == 1) { ASSERT(in_width % 4 == 0 && out_width % 4 == 0); srcImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_8U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align in_width, // image width in_height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_8U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align out_width, // image width out_height, // image height NULL, NULL, NULL, NULL); // not tiled } else if(color == 2) { ASSERT(in_width % 2 == 0 && out_width % 2 == 0); srcImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_16U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align in_width, // image width in_height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_16U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align out_width, // image width out_height, // image height NULL, NULL, NULL, NULL); // not tiledelse } else if(color == 3) { ASSERT(in_width % 4 == 0 && out_width % 4 == 0); srcImage = iplCreateImageHeader( 3, // number of channels 0, // no alpha channel IPL_DEPTH_8U, // data of byte type "RGB", // color model "RGB", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align in_width, // image width in_height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 3, // number of channels 0, // no alpha channel IPL_DEPTH_8U, // data of byte type "RGB", // color model "RGB", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align out_width, // image width out_height, // image height NULL, NULL, NULL, NULL); // not tiled } else { // color == 4 srcImage = iplCreateImageHeader( 4, // number of channels 4, // no alpha channel IPL_DEPTH_8U, // data of byte type "RGBA", // color model "BGRA", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align in_width, // image width in_height, // image height NULL, NULL, NULL, NULL); // not tiled dstImage = iplCreateImageHeader( 4, // number of channels 4, // no alpha channel IPL_DEPTH_8U, // data of byte type "RGBA", // color model "BGRA", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 4 bytes align out_width, // image width out_height, // image height NULL, NULL, NULL, NULL); // not tiled } if( NULL == srcImage ) return; srcImage->imageData = (char*)in_image; if( NULL == dstImage ) return; dstImage->imageData = (char*)out_image; iplResize(srcImage, dstImage, out_width, in_width, out_height, in_height, IPL_INTER_LINEAR); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER );}void Zoom(BYTE *in_image, int in_width, int in_height, unsigned char *out_image, int nZoomFactor){ int out_width = in_width * nZoomFactor; int out_height = in_height * nZoomFactor; IplImage *srcImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_8U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 8 bytes align in_width, // image width in_height, // image height NULL, // no ROI NULL, // no mask ROI NULL, // no image ID NULL); // not tiled if( NULL == srcImage ) return; srcImage->imageData = (char*)in_image; IplImage *dstImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_8U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 8 bytes align out_width, // image width out_height, // image height NULL, // no ROI NULL, // no mask ROI NULL, // no image ID NULL); // not tiled if( NULL == dstImage ) return; dstImage->imageData = (char*)out_image; iplZoom(srcImage, dstImage, nZoomFactor, 1, nZoomFactor, 1, IPL_INTER_LINEAR); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER );}void Zoom16(BYTE *in_image, int in_width, int in_height, unsigned char *out_image, int nZoomFactor){ int out_width = in_width * nZoomFactor; int out_height = in_height * nZoomFactor; ASSERT(in_width % 2 == 0 && out_width % 2 == 0); IplImage *srcImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_16U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align in_width, // image width in_height, // image height NULL, // no ROI NULL, // no mask ROI NULL, // no image ID NULL); // not tiled if( NULL == srcImage ) return; srcImage->imageData = (char*)in_image; IplImage *dstImage = iplCreateImageHeader( 1, // number of channels 0, // no alpha channel IPL_DEPTH_16U, // data of byte type "Gray", // color model "Gray", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_QWORD, // 8 bytes align out_width, // image width out_height, // image height NULL, // no ROI NULL, // no mask ROI NULL, // no image ID NULL); // not tiled if( NULL == dstImage ) return; dstImage->imageData = (char*)out_image; iplZoom(srcImage, dstImage, nZoomFactor, 1, nZoomFactor, 1, IPL_INTER_LINEAR); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER );}// in_image绰 read_only
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -