当前位置: 首页 > news >正文

图像压缩-将8bit数据压缩为2bit

std::shared_ptr<unsigned char> Convert8bitTo2bit(std::shared_ptr<unsigned char>& pixels, int width, int height)
{size_t pixelCount = width * height;size_t compressedSize = (pixelCount + 3) / 4; // 每4个像素压缩为1字节std::shared_ptr<unsigned char> output(new unsigned char[compressedSize], std::default_delete<unsigned char[]>());for (size_t i = 0; i < compressedSize; ++i) {unsigned char byte = 0;for (size_t j = 0; j < 4; ++j) {size_t idx = i * 4 + j;unsigned char level = 0;if (idx < pixelCount) {unsigned char value = pixels.get()[idx];level = value / 64; // 8bit → 2bit(0–3)}byte |= (level & 0x03) << (6 - j * 2); // 高位优先排列}output.get()[i] = byte;}return output;
}

为使的压缩后的图像更平滑增加抖动算法处理

uint8_t quantize2bit(uint8_t value) {return value / 64;  // 0~255 → 0~3
}uint8_t levelToGray(uint8_t level) {return level * 85;  // 0→0, 1→85, 2→170, 3→255
}std::shared_ptr<unsigned char> Convert8bitTo2bitFloydSteinberg(std::shared_ptr<unsigned char>& pixels, int width, int height)
{int totalPixels = width * height;std::vector<uint8_t> temp(pixels.get(), pixels.get() + totalPixels);// Floyd–Steinberg 抖动int nFactor = 16;    //扩散因子for (int y = 0; y < height; ++y) {for (int x = 0; x < width; ++x) {int idx = y * width + x;uint8_t old = temp[idx];uint8_t level = quantize2bit(old);uint8_t newVal = levelToGray(level);temp[idx] = newVal;int error = static_cast<int>(old) - newVal;// 扩散误差if (x + 1 < width)temp[idx + 1] = clamp(temp[idx + 1] + error * 7 / nFactor, 0, 255);   //向右扩散if (y + 1 < height) {if (x > 0)temp[idx + width - 1] = clamp(temp[idx + width - 1] + error * 3 / nFactor, 0, 255);    //左下扩散temp[idx + width] = clamp(temp[idx + width] + error * 5 / nFactor, 0, 255);    //向下扩散if (x + 1 < width)temp[idx + width + 1] = clamp(temp[idx + width + 1] + error * 1 / nFactor, 0, 255);    //右下扩散}}}// 压缩为 2bit 数据(每 4 像素 → 1 字节)int compressedSize = (totalPixels + 3) / 4;std::shared_ptr<unsigned char> pixels2bit(new unsigned char[compressedSize], std::default_delete<unsigned char[]>());for (int i = 0; i < compressedSize; ++i) {uint8_t byte = 0;for (int j = 0; j < 4; ++j) {int idx = i * 4 + j;uint8_t level = (idx < totalPixels) ? quantize2bit(temp[idx]) : 0;byte |= (level & 0x03) << (6 - j * 2);}pixels2bit.get()[i] = byte;}return pixels2bit;
}
http://www.dtcms.com/a/553347.html

相关文章:

  • 海宁网站怎么做seo企业网站能起到什么作用
  • 手机在网上怎么创建自己的网站网站开发人员职位
  • LineSlam线特征投影融合(Fuse) 中pML->GetLineNormalVector()的理解代码理解
  • 【图像处理基石】多波段图像融合算法入门:从概念到实践
  • Docker核心文件:DockerCompose文件
  • 企业网站 自助建站网站做竞价经常会被攻击吗
  • Android ble理解
  • 深入理解 Spring Boot Web 开发中的静态资源处理机制
  • 网站建设费记什么科目产品ui设计是什么
  • 衡水企业网站设计国内app开发公司
  • 【java EE】IDEA 中创建或迁移 Spring 或 Java EE 项目的核心步骤和注意事项
  • 如何保证缓存与数据库更新时候的一致性
  • 【Spring Boot Starter 设计思考:分离模式是否适用于所有场景】
  • HTTP 头部参数数据注入测试sqlilabs less 18
  • 网站速度慢的原因做网站建设优化的电话话术
  • 【数据结构】单链表 练习记录
  • mac 安装 jdk17
  • 【项目实战1-瑞吉外卖|day22】
  • 怎么用dw做响应式网站网站主持人制作网站代言人
  • Android开发自学笔记 --- Kotlin
  • 从VB到PyCharm:编程工具跨越时代的传承与革命
  • 网站建设创新成果四年级写一小段新闻
  • 生产环境用Go语言完成微服务搭建和业务融入
  • 第九课 四川料理は辛いです
  • DevEco Studio在模拟器中改变运行的 ets 文件
  • 第5讲:项目依赖管理与资源管理
  • 网站定制案例微安电力wordpress 分类合并
  • Orleans 的异步
  • comsol livelink with matlab
  • PDF文档中表格以及形状解析-后续处理(线段生成最小多边形)