游程编码算法
来源:互联网 发布:北京地铁网络取票 编辑:程序博客网 时间:2024/05/19 03:44
游程编码算法
概述
利用比特流常见的冗余形式:连续的重复数据,来压缩数据。
0000000000000001111111000000011111111111 --40bit在源数据中,记录重复bit的个数,记录到压缩数据中。1111,0111,0111,1011 --16bit
实现
- 压缩原理:
- 将源文件中连续的1或0的个数(count)写到压缩文件中,
- 比如count用8位表示,规定压缩文件的格式为:
- 0连续的个数-1连续的个数-0连续的个数-1连续的个数…..
- 个数的大小为0~255,
- 如何处理连续的0(或1)的个数过大,无法存到count中:
count0(=255)-count1(=0)-count0(=?)-…….
压缩
private final static int R = 256;private final static int lgR = 8; //存储cnt需要多少位/** * 压缩 */private static void compress() { boolean b, old = false; int cnt = 0; //记录连续的0或1的个数 while (!BinaryStdIn.isEmpty()) { b = BinaryStdIn.readBoolean(); if (b != old) { //该向输出流写数据了 BinaryStdOut.write(cnt,lgR); cnt = 0; old = !old; } else { if (cnt == (R - 1)) { // 处理连续的0(或1)的个数过大,无法存到count BinaryStdOut.write(cnt,lgR); //count0(=255)-count1(=0)-count0(=?)-....... cnt = 0; BinaryStdOut.write(cnt,lgR); } } cnt++; } BinaryStdOut.write(cnt,lgR); //把剩余的cnt写出 BinaryStdOut.close();}
解压
/** * 解压 */private static void expand() { boolean bit = false; while (!BinaryStdIn.isEmpty()) { int run = BinaryStdIn.readInt(lgR); //从输入流中读取lgR 位,这个数值代表了源文件中连续1或连续0的长度 for (int i = 0; i < run; i++) { //根据run的大小向输出流中写位 BinaryStdOut.write(bit); } bit = !bit; //压缩的格式要求 } BinaryStdOut.close();}
测试用例
public static void main(String[] args) { if (args[0].equals("-")) compress(); else if (args[0].equals("+")) expand(); else throw new IllegalArgumentException("Illegal command line argument");}
0 0
- 游程编码算法
- 数据压缩算法-游程编码RLE
- 【转】计算机算法:数据压缩之游程编码
- 游程编码
- 游程编码
- 游程编码
- 游程编码
- 游程编码!
- 游程编码
- 游程编码
- 算法百题004:数据压缩——游程编码
- [算法系列之十六]数据压缩之游程编码
- 字符串算法面试题之:游程编码和词组倒转
- 游程长度编码
- 游程长度编码
- 游程编码之加密
- 游程长度编码
- 1138 -- 游程编码
- JAVA 实现手机号码压缩,解压
- C 预处理指令 条件编译
- vim小知识
- C++s合并两个链表(牛客剑指offer)
- CMS 管理系统使用
- 游程编码算法
- 如何在Android开发中让你的代码更有效率
- iOS开发学习之触摸事件和手势识别(转
- log4j和web.xml配置webAppRootKey 的问题
- js 中 String.replace() 的使用
- JavaScript中常见的数组操作函数及用法
- mybatis中的#和$的区别
- Android【Layout_weight的深刻理解】
- Sicily 1901. Beans