在图像上画一个矩形c代码

//防止图像出界的函数
int clamp_s(int value, int m_min, int m_max)
{
    int mm_ret = value;
    if(value < m_min)
    {
        mm_ret = m_min;
    }else if(value > m_max)
    {
        mm_ret = m_max;
    }
    return mm_ret;
}

// data 图像数据
// width图像宽度
// height图像高度
// channel图像通道
// x0 第一个坐标x
// y0 第一个坐标y
// x1 第2个坐标x
// y1 第2个坐标y
// red 红色通道值
// gree红色通道值
// blue红色通道值
// shinner 线条宽度

int LiDrewRect_Source(unsigned char * data, int width, int height, int channel, int x0, int y0, int x1, int y1, int shinner, 
                      unsigned char red, unsigned char gree, unsigned char blue)
{

    int i = 0;
    int j = 0;
    int k = 0;
    unsigned char * tempUchar = data;
    unsigned char color[4] = {red, gree, blue, 0};
    int tempx0 = 0;
    int tempy0 = 0;
    int tempx1 = 0;
    int tempy1 = 0;

    if(x0 >= x1)
    {
        tempx0 = x1;
        tempx1 = x0;
    }else
    {
        tempx0 = x0;
        tempx1 = x1;
    }

    if(y0 >= y1)
    {
        tempy0 = y1;
        tempy1 = y0;
    }else
    {
        tempy0 = y0;
        tempy1 = y1;
    }

    for(i = tempy0 - shinner; i <= tempy0 + shinner; i ++ )
    {
        for(j = tempx0 - shinner; j <= tempx1 + shinner; j ++)
        {
            for(k = 0; k < channel; k ++)
            {
                tempUchar[(clamp_s(i, 0, height - 1) * width + clamp_s(j, 0, width - 1)) * channel + k] = color[k];
            }

        }
    }

    for(i = tempy1 - shinner; i <= tempy1 + shinner; i ++ )
    {
        for(j = tempx0 - shinner; j <= tempx1 + shinner; j ++)
        {
            for(k = 0; k < channel; k ++)
            {
                tempUchar[(clamp_s(i, 0, height - 1) * width + clamp_s(j, 0, width - 1)) * channel + k] = color[k];
            }

        }
    }

    for(i = tempy0 - shinner; i <= tempy1 + shinner; i ++ )
    {
        for(j = tempx0 - shinner; j <= tempx0 + shinner; j ++)
        {
            for(k = 0; k < channel; k ++)
            {
                tempUchar[(clamp_s(i, 0, height - 1) * width + clamp_s(j, 0, width - 1)) * channel + k] = color[k];
            }

        }
    }

    for(i = tempy0 - shinner; i <= tempy1 + shinner; i ++ )
    {
        for(j = tempx1 - shinner; j <= tempx1 + shinner; j ++)
        {
            for(k = 0; k < channel; k ++)
            {
                tempUchar[(clamp_s(i, 0, height - 1) * width + clamp_s(j, 0, width - 1)) * channel + k] = color[k];
            }

        }
    }

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值