(顺时针)旋转BW图像程序

来源:互联网 发布:博彦科技java面试题 编辑:程序博客网 时间:2024/05/30 04:54
/**Code adapted from CxImage (http://www.xdp.it/cximage.htm)*/static BYTE* Rotate90(BYTE *src, int src_width, int src_height, int bpp ) {int x, y, y2;int dst_width  = src_height;int dst_height = src_width;// get src and dst scan widthint src_pitch  = (src_width*bpp + 7) /8 + 3 & ~3; int dst_pitch  = (dst_width*bpp + 7) /8 + 3 & ~3;// allocate dst imageBYTE *dst =  new BYTE[dst_pitch * dst_height];if(NULL == dst) return NULL;if(bpp == 1) {// speedy rotate for BW imagesBYTE *sbits, *dbits, *dbitsmax, bitpos, *nrow, *srcdisp;div_t div_r;BYTE *bsrc  = src; BYTE *bdest = dst;dbitsmax = bdest + dst_height * dst_pitch - 1;for(y = 0; y < src_height; y++) {// figure out the column we are going to be copying todiv_r = div(y, 8);// set bit pos of src column bytebitpos = (BYTE)(128 >> div_r.rem);srcdisp = bsrc + y * src_pitch;for (x = 0; x < src_pitch; x++) {// get source bitssbits = srcdisp + x;// get destination columnnrow = bdest + (dst_height - 1 - (x * 8)) * dst_pitch + div_r.quot;for (int z = 0; z < 8; z++) {// get destination bytedbits = nrow - z * dst_pitch;if ((dbits < bdest) || (dbits > dbitsmax)) break;if (*sbits & (128 >> z)) *dbits |= bitpos;}}}}else if((bpp == 8) || (bpp == 24) || (bpp == 32)) {}return dst;}