C语言之图像旋转

最近用到了图像向左或者向右旋转90°的情况,纠结了好一会儿,写下来供大家参考。

1 向左旋转90°

//unsigned char *pImgData: 输入图像指针
//int WidthIn, int HeightIn:输入图像宽、高
//unsigned char *pImgOut: 旋转后图像指针
void RotateOfLeft(unsigned char *pImgData, int WidthIn, int HeightIn)
{
    int i, j, tempSize;
    int WidthOut  = HeightIn;
    int HeightOut = WidthIn;
    unsigned char pImgtemp[148*36];            
    for (i = 0; i < HeightIn; i ++)
    {
        for (j = 0;j < WidthIn; j ++)      
        {
            *(pImgtemp + i*WidthIn + j) = *(pImgData*WidthIn + j);
        }      
    }  
    //memcpy(pImgtemp,pImgData,148*36);
    for (i = 0; i < HeightOut; i ++)
    {
        for (j = 0;j < WidthOut; j ++)      
        {
            *(pImgData + i*WidthOut + j) = *(pImgtemp + (j+1)*WidthIn - 1 - i);
        }          
    }
}

 

2 向右旋转90°

//unsigned char *pImgData: 输入图像指针
//int WidthIn, int HeightIn:输入图像宽、高
//unsigned char *pImgOut: 旋转后图像指针
void RotateOfRight(unsigned char *pImgData, int WidthIn, int HeightIn, unsigned char *pImgOut)
{
    int i, j;
    int WidthOut  = HeightIn;
    int HeightOut = WidthIn;
    for (i = 0; i < HeightOut; i ++)
    {
        for (j = 0;j < WidthOut; j ++)      
        {
            *(pImgOut + i*WidthOut + j) = *(pImgData + (HeightIn - j - 1)*WidthIn + i);
        }          
    }
}

 

posted @ 2018-08-10 15:37  闪电gogogo  阅读(5049)  评论(0编辑  收藏  举报