C++将图像旋转正负90°及180°

时间:2025-01-26 14:13:22
//判断是否需要保存码图像 int iRotationType = 1; //自己根据情况设置,1 2 3 if (0 != iRotationType) { //申请三个通道的内存 char* pdataR = new char[Width * Height]; char* pdataG = new char[Width * Height]; char* pdataB = new char[Width * Height]; //pSrcdataR pSrcdataG pSrcdataB 为源数据内存 //pdataR pdataR pdataB 为旋转之后的内存 for (int h = 0; h < Height; h++) { for (int w = 0; w < Width; w++) { switch (iRotationType) { case 1: { //向右旋转90° pdataR[(Width - w - 1) * Height + h] = pSrcdataR[h * Width + w]; pdataG[(Width - w - 1) * Height + h] = pSrcdataG[h * Width + w]; pdataB[(Width - w - 1) * Height + h] = pSrcdataB[h * Width + w]; break; case 2: { //向左旋转90° pdataR[w * Height + (Height - h - 1)] = pSrcdataR[h * Width + w]; pdataG[w * Height + (Height - h - 1)] = pSrcdataG[h * Width + w]; pdataB[w * Height + (Height - h - 1)] = pSrcdataB[h * Width + w]; break; } case 3: { //旋转180° pdataR[(Height - h - 1) * Width + (Width - w - 1)] = pSrcdataR[h * Width + w]; pdataG[(Height - h - 1) * Width + (Width - w - 1)] = pSrcdataG[h * Width + w]; pdataB[(Height - h - 1) * Width + (Width - w - 1)] = pSrcdataB[h * Width + w]; break; } } default: break; } } } //主意宽高的变化 //判断宽高是否需要对调 if (1 == iRotationType || 2 == iRotationType) { //宽高进行交换 } //TODO 将旋转好的内存,再进行通道合并即可,不再赘述。 //删除内存 delete[] pSrcdataR; delete[] pdataR; delete[] pdataG; delete[] pdataB; }