c++对rgb数据进行抽样

以下算法是对rgb数据进行抽样,将数据在内容不变的情况下,降低数据的内存占用,方便应用处理和网络传输。

int scaleRGB(const unsigned char* src_img, unsigned char* dst_img, unsigned int src_width, unsigned int src_height, unsigned int dst_width, unsigned int dst_height)
{
    //bilinear interpolation
    if (!src_img || !dst_img)
    {
        return -1;
    }
    if (!dst_width || !dst_height)
    {
        return -2;
    }

    double x_ratio = (double)src_width / dst_width;
    double y_ratio = (double)src_height / dst_height;

    double aspect_ratio = 0.0f;
    int border_shift = 0;
    int h = 0, w = 0;
    int x0 = 0, x1 = 0, y0 = 0, y1 = 0;
    int dst_r = 0, dst_g = 0, dst_b = 0;
    if (x_ratio < y_ratio)
    {
        aspect_ratio = x_ratio;
        border_shift = (int)((src_height - (int)(dst_height * aspect_ratio)) / 2);
        for (h = 0; h < dst_height; h++)
        {
            for (w = 0; w < dst_width; w++)
            {
                double src_h = border_shift + h * aspect_ratio;
                double src_w = w * aspect_ratio;
                x0 = (int)src_w;
                x1 = x0 + 1;
                y0 = (int)src_h;
                y1 = y0 +  + 1;

                int rx0y0 = src_img[3 * (y0 * src_width + x0) + 0];
                int gx0y0 = src_img[3 * (y0 * src_width + x0) + 1];
                int bx0y0 = src_img[3 * (y0 * src_width + x0) + 2];

                int rx1y0 = src_img[3 * (y0 * src_width + x1) + 0];
                int gx1y0 = src_img[3 * (y0 * src_width + x1) + 1];
                int bx1y0 = src_img[3 * (y0 * src_width + x1) + 2];

                int rx0y1 = src_img[3 * (y1 * src_width + x0) + 0];
                int gx0y1 = src_img[3 * (y1 * src_width + x0) + 1];
                int bx0y1 = src_img[3 * (y1 * src_width + x0) + 2];

                int rx1y1 = src_img[3 * (y1 * src_width + x1) + 0];
                int gx1y1 = src_img[3 * (y1 * src_width + x1) + 1];
                int bx1y1 = src_img[3 * (y1 * src_width + x1) + 2];

                dst_r = (y1 - src_h) * ((x1 - src_w) * rx0y0 + (src_w - x0) * rx1y0) + (src_h - y0) * ((x1 - src_w) * rx0y1 + (src_w - x0) * rx1y1);
                dst_g = (y1 - src_h) * ((x1 - src_w) * gx0y0 + (src_w - x0) * gx1y0) + (src_h - y0) * ((x1 - src_w) * gx0y1 + (src_w - x0) * gx1y1);
                dst_b = (y1 - src_h) * ((x1 - src_w) * bx0y0 + (src_w - x0) * bx1y0) + (src_h - y0) * ((x1 - src_w) * bx0y1 + (src_w - x0) * bx1y1);

                dst_r = dst_r > 255 ? 255 : dst_r < 0 ? 0 : dst_r;
                dst_g = dst_g > 255 ? 255 : dst_g < 0 ? 0 : dst_g;
                dst_b = dst_b > 255 ? 255 : dst_b < 0 ? 0 : dst_b;

                dst_img[3 * (h * dst_width + w) + 0] = (unsigned char)dst_r;
                dst_img[3 * (h * dst_width + w) + 1] = (unsigned char)dst_g;
                dst_img[3 * (h * dst_width + w) + 2] = (unsigned char)dst_b;
            }
        }
    }
    else
    {
        aspect_ratio = y_ratio;
        border_shift = (int)((src_width - (int)(dst_width * aspect_ratio)) / 2);
        for (h = 0; h < dst_height; h++)
        {
            double src_h = h * aspect_ratio;
            for (w = 0; w < dst_width; w++)
            {
                double src_w = border_shift + w * aspect_ratio;
                x0 = (int)src_w;
                x1 = x0 + 1;
                y0 = (int)src_h;
                y1 = y0 + 1;

                int rx0y0 = src_img[3 * (y0 * src_width + x0) + 0];
                int gx0y0 = src_img[3 * (y0 * src_width + x0) + 1];
                int bx0y0 = src_img[3 * (y0 * src_width + x0) + 2];

                int rx1y0 = src_img[3 * (y0 * src_width + x1) + 0];
                int gx1y0 = src_img[3 * (y0 * src_width + x1) + 1];
                int bx1y0 = src_img[3 * (y0 * src_width + x1) + 2];

                int rx0y1 = src_img[3 * (y1 * src_width + x0) + 0];
                int gx0y1 = src_img[3 * (y1 * src_width + x0) + 1];
                int bx0y1 = src_img[3 * (y1 * src_width + x0) + 2];

                int rx1y1 = src_img[3 * (y1 * src_width + x1) + 0];
                int gx1y1 = src_img[3 * (y1 * src_width + x1) + 1];
                int bx1y1 = src_img[3 * (y1 * src_width + x1) + 2];
                     
                dst_r = (y1 - src_h) * ((x1 - src_w) * rx0y0 + (src_w - x0) * rx1y0) + (src_h - y0) * ((x1 - src_w) * rx0y1 + (src_w - x0) * rx1y1);
                dst_g = (y1 - src_h) * ((x1 - src_w) * gx0y0 + (src_w - x0) * gx1y0) + (src_h - y0) * ((x1 - src_w) * gx0y1 + (src_w - x0) * gx1y1);
                dst_b = (y1 - src_h) * ((x1 - src_w) * bx0y0 + (src_w - x0) * bx1y0) + (src_h - y0) * ((x1 - src_w) * bx0y1 + (src_w - x0) * bx1y1);
				
				dst_r = dst_r > 255 ? 255 : dst_r < 0 ? 0 : dst_r;
                dst_g = dst_g > 255 ? 255 : dst_g < 0 ? 0 : dst_g;
                dst_b = dst_b > 255 ? 255 : dst_b < 0 ? 0 : dst_b;

                dst_img[3 * (h * dst_width + w) + 0] = (unsigned char)dst_r;
                dst_img[3 * (h * dst_width + w) + 1] = (unsigned char)dst_g;
                dst_img[3 * (h * dst_width + w) + 2] = (unsigned char)dst_b;
            }
        }
    }
    
	return 0;
}
  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_君莫笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值