TIFF6 Packbit algorithm
“Packbits” from ISO 12369
参考TIFF 6.0 Specification,点击TIFF, Version 6.0;
@Section 9: PackBits Compression
> This section describes TIFF compression type 32773, a simple byte-oriented runlength scheme.
描述[摘录]
In choosing a simple byte-oriented run-length compression scheme, we arbitrarily chose the Apple Macintosh PackBits scheme. It has a good worst case behavior
(at most 1 extra byte for every 128 input bytes). For Macintosh users, the toolbox utilities PackBits and UnPackBits will do the work for you, but it is easy to implement your own routines.
A pseudo code fragment to unpack might look like this:
Loop until you get the number of unpacked bytes you are expecting:
Read the next source byte into n.
If n is between and inclusive, copy the next n+ bytes literally.
Else if n is between - and - inclusive, copy the next byte -n+
times.
Else if n is -, noop.
Endloop
In the inverse routine, it is best to encode a 2-byte repeat run as a replicate run except when preceded and followed by a literal run. In that case, it is best to merge
the three runs into one literal run. Always encode 3-byte repeats as replicate runs.
That is the essence of the algorithm. Here are some additional rules:
• Pack each row separately. Do not compress across row boundaries.
• The number of uncompressed bytes per row is defined to be (ImageWidth + 7)/8. If the uncompressed bitmap is required to have an even number of bytes per
row, decompress into word-aligned buffers.
• If a run is larger than 128 bytes, encode the remainder of the run as one or more additional replicate runs.
When PackBits data is decompressed, the result should be interpreted as per compression type 1 (no compression).
Coding
function declaration
// Packbit algorithm, make sure the _countof(Pack[*]) >= (count + count/128 +2);
// return _countof(Pack[*]) actual count
// AVC_FMAC
int tiff6_PackBits(unsigned char array[], int count, unsigned char Pack[]);
// return _countof(array[*]);
// AVC_FMAC
int tiff6_unPackBits(char Pack[], int count, unsigned char array[] /*= NULL*/);
implementation
static signed char* Pack_init(unsigned char Pack[], unsigned char Byte);
static signed char* Pack_byte(signed char* Count, unsigned char Byte);
static unsigned char* End_byte(signed char* Count);
int tiff6_PackBits(unsigned char array[], int count, unsigned char Pack[])
{
int i = ;
signed char* Count = Pack_init(Pack, array[i]); i++;
for (; i < count; i++) Count = Pack_byte(Count, array[i]);
unsigned char* End = End_byte(Count); *End = '\0';
return (End - Pack);
}
static int unPack_count(char Pack[], int count);
int tiff6_unPackBits(char Pack[], int count, unsigned char array[] /*= NULL*/)
{
if (!array) return unPack_count(Pack, count);
int nRes = ;
signed char* Count = (signed char*)Pack;
while ((char*)Count < (Pack+count))
{
int c = *Count;
if (c<)
{
int n = (-c);
memset(&(array[nRes]), Count[], n);
nRes += n;
}
else
{
int n = (+c);
memcpy(&(array[nRes]), &Count[], n);
nRes += n;
}
Count = (signed char*)End_byte(Count);
}
return nRes;
} int unPack_count(char Pack[], int count)
{
int nRes = ;
signed char* Count = (signed char*)Pack;
while ((char*)Count < (Pack+count))
{
int c = *Count;
if (c<)
nRes += (-c);
else
nRes += (+c);
Count = (signed char*)End_byte(Count);
}
return nRes;
}
signed char* Pack_init( unsigned char Pack[], unsigned char Byte )
{
signed char* Cnt = (signed char*)Pack;
*Cnt = ; Pack[] = Byte;
return Cnt;
}
unsigned char* End_byte( signed char* Count )
{
unsigned char* Pack = (unsigned char*)(Count+);
signed char c = *Count;
if (c >)
Pack = &(Pack[c+]);
else Pack = &(Pack[]);
return Pack;
}
signed char* Pack_byte( signed char* Count, unsigned char Byte )
{
signed char c = *Count;
unsigned char* End = End_byte(Count);
if ( == c || c == -)
return Pack_init(End, Byte);
else
{
unsigned char* Pack = End-;
if (*(Pack) == Byte)
{
if (c >)
{
(*Count) = c-;
Count = Pack_byte(Pack_init(Pack, Byte), Byte);
}
else (*Count)--;
}
else
{
if (c >= )
{
*End = Byte;
(*Count)++;
}
else Count = Pack_init(End, Byte);
}
}
return Count;
}
另,本文明确使用了signed修饰char, 因为有些编译环境下char默认是有符号的,但是笔者也曾遇到默认无符号的环境,因此为了使代码在其中编译能正确使用,则指明符号修饰。
TIFF6 Packbit algorithm的更多相关文章
- 挑子学习笔记:两步聚类算法(TwoStep Cluster Algorithm)——改进的BIRCH算法
转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/twostep_cluster_algorithm.html 两步聚类算法是在SPSS Modeler中使用的 ...
- PE Checksum Algorithm的较简实现
这篇BLOG是我很早以前写的,因为现在搬移到CNBLOGS了,经过整理后重新发出来. 工作之前的几年一直都在搞计算机安全/病毒相关的东西(纯学习,不作恶),其中PE文件格式是必须知识.有些PE文件,比 ...
- [异常解决] windows用SSH和linux同步文件&linux开启SSH&ssh client 报 algorithm negotiation failed的解决方法之一
1.安装.配置与启动 SSH分客户端openssh-client和openssh-server 如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有 ...
- [Algorithm] 使用SimHash进行海量文本去重
在之前的两篇博文分别介绍了常用的hash方法([Data Structure & Algorithm] Hash那点事儿)以及局部敏感hash算法([Algorithm] 局部敏感哈希算法(L ...
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- [Algorithm & NLP] 文本深度表示模型——word2vec&doc2vec词向量模型
深度学习掀开了机器学习的新篇章,目前深度学习应用于图像和语音已经产生了突破性的研究进展.深度学习一直被人们推崇为一种类似于人脑结构的人工智能算法,那为什么深度学习在语义分析领域仍然没有实质性的进展呢? ...
- [Algorithm] 群体智能优化算法之粒子群优化算法
同进化算法(见博客<[Evolutionary Algorithm] 进化算法简介>,进化算法是受生物进化机制启发而产生的一系列算法)和人工神经网络算法(Neural Networks,简 ...
- [Evolutionary Algorithm] 进化算法简介
进化算法,也被成为是演化算法(evolutionary algorithms,简称EAs),它不是一个具体的算法,而是一个“算法簇”.进化算法的产生的灵感借鉴了大自然中生物的进化操作,它一般包括基因编 ...
- Debian 8 jessie, OpenSSH ssh connection server responded Algorithm negotiation failed
安装了debian 8.5 就出问题了. root@debian8:~# lsb_release -aNo LSB modules are available.Distributor ID: Debi ...
随机推荐
- Java项目中使用配置文件配置
private String readConfig() { Properties p = new Properties(); InputStream in = getClass().getClassL ...
- 双11不再孤单,结识ECharts---强大的常用图表库
又是一年双十一,广大单身狗们有没有很寂寞(好把,其实我也是)!但是这次的双十一,我不再孤单,因为结识了一个js的强大的图表库---ECharts. 最近做软件工程项目的时候,由于设计图中有柱状图和饼图 ...
- 折腾iPhone的生活——设置“查找我的iPhone”,让iPhone更防盗
对于iPhone,防盗一直是一个非常那啥的话题,很多买过iPhone的人都被偷过,但疼,然而苹果也看到了这个问题,所以在iOS7,我们终于看到了一个比较靠谱的防盗软件:查找我的iPhone 之前小偷解 ...
- Hibernate 以流的方式获取数据
hibernateQuery.setFetchSize(Integer.MIN_VALUE); results = hibernateQuery.scroll(ScrollMode.FORWARD_O ...
- Shell上传文件到ftp
写一个shell文件,将给定的文件上传到指定的ftp. 代码如下: #!/bin/bash #用来将文件上传到ftp,输入参数:文件名(包括路径).ftp的IP.ftp的端口.用户名.密码 ip=$ ...
- java 新手
public class hello{ public static void main(String args[]){ int a=23,b=32,c=34; int s=Math.max(a,c); ...
- Centos6.5 nginx+nginx-rtmp配置流媒体服务器
之前使用命令方式安装nginx并配置了反向代理,由于想做一个视频直播的小项目,查了流媒体服务器的方案,发现nginx有相关模块,于是开始搞起. nginx-rtmp模块需要在nginx编译时,以模块方 ...
- flash引入
博客页面引入一个小人的动画时钟代码 <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase ...
- javascript正则表达式简述
compile 编译或改变字符串,参数跟new RegExp相同 var patt = /man/g; var str = "man and woman"; str.replace ...
- dispatch_get_current_queue 废弃
由于iOS7以后 dispatch_get_current_queue 被废弃,所以需要寻找一个替代的方案. 发现 dispatch_get_current_queue 并没有字面上那么简单. 这个函 ...