导航:首页 > 源码编译 > cbmp压缩算法

cbmp压缩算法

发布时间:2023-03-10 02:25:57

① 怎样用c语言c++编码打开读取bmp。两者的的区别。可否用java编写bmp。为什么

1C++读取bmp文件实例
2java读取bmp文件

#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
typedef unsigned char BYTE;//字符型
typedef unsigned short WORD;//短整型
typedef unsigned long DWORD;//长整形
typedef long LONG;
//位图文件头信息结构定义
//其中不包含文件类型信息(由于结构体的内存结构决定,要是加了的话将不能正确读取文件信息)
typedef struct tagBITMAPFILEHEADER {
//WORD bfType;
DWORD bfSize; //文件大小
WORD bfReserved1; //保留字,不考虑
WORD bfReserved2; //保留字,同上
DWORD bfOffBits; //实际位图数据的偏移字节数,即前三个部分长度之和
} BITMAPFILEHEADER;
//信息头BITMAPINFOHEADER,也是一个结构,其定义如下:
typedef struct tagBITMAPINFOHEADER{
//public:
DWORD biSize; //指定此结构体的长度,为40
LONG biWidth; //位图宽
LONG biHeight; //位图高
WORD biPlanes; //平面数,为1
WORD biBitCount; //采用颜色位数,可以是1,2,4,8,16,24,新的可以是32
DWORD biCompression; //压缩方式,可以是0,1,2,其中0表示不压缩
DWORD biSizeImage; //实际位图数据占用的字节数
LONG biXPelsPerMeter; //X方向分辨率
LONG biYPelsPerMeter; //Y方向分辨率
DWORD biClrUsed; //使用的颜色数,如果为0,则表示默认值(2^颜色位数)
DWORD biClrImportant; //重要颜色数,如果为0,则表示所有颜色都是重要的
} BITMAPINFOHEADER;
//调色板Palette,当然,这里是对那些需要调色板的位图文件而言的。24位和32位是不需要调色板的。
//(似乎是调色板结构体个数等于使用的颜色数。)
typedef struct tagRGBQUAD {
//public:
BYTE rgbBlue; //该颜色的蓝色分量
BYTE rgbGreen; //该颜色的绿色分量
BYTE rgbRed; //该颜色的红色分量
BYTE rgbReserved; //保留值
} RGBQUAD;
void showRgbQuan(tagRGBQUAD* pRGB)
{
printf("(%-3d,%-3d,%-3d) ",pRGB->rgbRed,pRGB->rgbGreen,pRGB->rgbBlue);
}
int main(){
char ch,pd;
int b,c,m,d,w,e,f;
int x,y;
unsigned char *q;
int i,j;
unsigned char a[2];
char strFile[50];
BITMAPFILEHEADER bitHead;
BITMAPINFOHEADER bitInfoHead;
tagRGBQUAD *pRgb ;
FILE *fp;
FILE *p;
q=(unsigned char*)malloc(1);
printf("mkx**用C语言编程来读取BMP文件某一像素点的数据** 请输入一个bmp文件: ");
scanf("%s",strFile);
fp=fopen(strFile,"rb");

if(fp!=NULL)
{
printf("file open success! ");
WORD fileType;
fread(&fileType,1,sizeof(WORD),fp);
if(fileType != 0x4d42)
{
printf("file is not .bmp file!");
system("pause");
return 0;
}
}
else
{
printf("file open fail! ");
system("pause");
return 0;
}
//读文件头信息,并打印文件头信息各项的值
fread(&bitHead,1,sizeof(tagBITMAPFILEHEADER),fp);
printf("bmp文件头信息 文件大小:%d 保留字:%d 保留字:%d 实际位图数据的偏移字节数:%d ",
bitHead.bfSize,bitHead.bfReserved1,bitHead.bfReserved2,bitHead.bfOffBits);
//读文件信息头,并打印文件信息头各项的值
fread(&bitInfoHead,1,sizeof(tagBITMAPINFOHEADER),fp);
printf("bmp文件信息头 结构体的长度:%d 位图宽:%d 位图高:%d biPlanes平面数:%d biBitCount采用颜色位数:%d 压缩方式:%d biSizeImage实际位图数据占用的字节数:%d X方向分辨率:%d Y方向分辨率:%d 使用的颜色数:%d 重要颜色数:%d ",
bitInfoHead.biSize,bitInfoHead.biWidth,bitInfoHead.biHeight,bitInfoHead.biPlanes,bitInfoHead.biBitCount,bitInfoHead.biCompression,bitInfoHead.biSizeImage,bitInfoHead.biXPelsPerMeter,bitInfoHead.biYPelsPerMeter,bitInfoHead.biClrUsed,bitInfoHead.biClrImportant);
if(bitInfoHead.biBitCount < 24){
printf("该文件有调色板,即该位图为非真彩色 ");
m=1;
if(bitInfoHead.biBitCount =8){
long nPlantNum = long(pow(2,double(bitInfoHead.biBitCount))); // Mix color Plant Number;
pRgb=(tagRGBQUAD *)malloc(nPlantNum*sizeof(tagRGBQUAD));
memset(pRgb,0,nPlantNum*sizeof(tagRGBQUAD));
int num = fread(pRgb,4,nPlantNum,fp);
printf("Color Plate Number: %d ",nPlantNum);
printf("颜色板信息: ");
for (int i =0; i<nPlantNum;i++)
{
if (i%5==0)
{
printf(" ");
}
showRgbQuan(&pRgb[i]);
}
printf(" ");
}
}
else{
printf("该位图为24位真彩色 ");
m=3;
}

fclose(fp);
while(ch!='N'){
p=fopen(strFile,"rb");
ch=NULL;
f=bitInfoHead.biHeight;
e=bitInfoHead.biWidth;
b=bitHead.bfOffBits;
printf("**输入指定像素点的数据**(行:1~%d 列1~%d) ",f,e);
printf("请输入第i行:");
scanf("%d",&x);
printf("请输入第j列:");
scanf("%d",&y);
c=b+(x-1)*e*m+m*(y-1);
fseek(p,c,0);
//printf("%d ",m);
if(m<2){
fread(&a[0],1,1,p);
printf("该点像素点的数据(十六进制)为:%x ",a[0]);
printf("用十进制表示:%d ",a[0]);
d=a[0];
printf("它对应的rgb值为:");
showRgbQuan(&pRgb[d]);
printf(" ");
}
else {
for(i=0;i<3;i++){
fread(&a[i],1,1,p);
// printf("%x ",a[i]);
}
printf("十六进制表示: 蓝:%x 绿:%x 红:%x ",a[0],a[1],a[2]);
printf("十进制表示: 蓝:%d 绿:%d 红:%d ",a[0],a[1],a[2]);
}
rewind(p);
//w=ftell(p);
//printf("%d",w);
fclose(p);
printf("**继续请输入任意字符,如需退出请输入N ");
getchar();
scanf("%c",&ch);
}
printf("感谢您使用~ ");
system("pause");
return 0;
}

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
//
//really just a collection of methods to read a BMP file
//
public class BMPLoader
{
// build an int from a byte array - convert little to big endian
public static int constructInt(byte[] in, int offset) {
int ret = ((int) in[offset + 3] & 0xff);
ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
return (ret);
}
// build an int from a byte array - convert little to big endian
// set high order bytes to 0xfff
public static int constructInt3(byte[] in, int offset) {
int ret = 0xff;
ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
return (ret);
}
// build an int from a byte array - convert little to big endian
public static long constructLong(byte[] in, int offset) {
long ret = ((long) in[offset + 7] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);
ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);
return (ret);
}
// build an double from a byte array - convert little to big endian
public static double constructDouble(byte[] in, int offset) {
long ret = constructLong(in, offset);
return (Double.longBitsToDouble(ret));
}
// build an short from a byte array - convert little to big endian
public static short constructShort(byte[] in, int offset) {
short ret = (short) ((short) in[offset + 1] & 0xff);
ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));
return (ret);
}
// internal class representing a bitmap header structure
// with code to read it from a file
static class BitmapHeader {
public int nsize;
public int nbisize;
public int nwidth;
public int nheight;
public int nplanes;
public int nbitcount;
public int ncompression;
public int nsizeimage;
public int nxpm;
public int nypm;
public int nclrused;
public int nclrimp;
// read in the bitmap header
public void read(FileInputStream fs) throws IOException
{
final int bflen = 14; // 14 byte BITMAPFILEHEADER
byte bf[] = new byte[bflen];
fs.read(bf, 0, bflen);
final int bilen = 40; // 40-byte BITMAPINFOHEADER
byte bi[] = new byte[bilen];
fs.read(bi, 0, bilen);
// Interperet data.
nsize = constructInt(bf, 2);
// System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);
// System.out.println("Size of file is :"+nsize);
nbisize = constructInt(bi, 2);
// System.out.println("Size of bitmapinfoheader is :"+nbisize);
nwidth = constructInt(bi, 4);
// System.out.println("Width is :"+nwidth);
nheight = constructInt(bi, 8);
// System.out.println("Height is :"+nheight);
nplanes = constructShort(bi, 12); //(((int)bi[13]&0xff)<<8) |
// (int)bi[12]&0xff;
// System.out.println("Planes is :"+nplanes);
nbitcount = constructShort(bi, 14); //(((int)bi[15]&0xff)<<8) |
// (int)bi[14]&0xff;
// System.out.println("BitCount is :"+nbitcount);
// Look for non-zero values to indicate compression
ncompression = constructInt(bi, 16);
// System.out.println("Compression is :"+ncompression);
nsizeimage = constructInt(bi, 20);
// System.out.println("SizeImage is :"+nsizeimage);
nxpm = constructInt(bi, 24);
// System.out.println("X-Pixels per meter is :"+nxpm);
nypm = constructInt(bi, 28);
// System.out.println("Y-Pixels per meter is :"+nypm);
nclrused = constructInt(bi, 32);
// System.out.println("Colors used are :"+nclrused);
nclrimp = constructInt(bi, 36);
// System.out.println("Colors important are :"+nclrimp);
}
}
public static Image read(FileInputStream fs)
{
try {
BitmapHeader bh = new BitmapHeader();
bh.read(fs);
if (bh.nbitcount == 24)
return (readMap24(fs, bh));
if (bh.nbitcount == 32)
return (readMap32(fs, bh));
if (bh.nbitcount == 8)
return (readMap8(fs, bh));
fs.close();
} catch (IOException e) {
// System.out.println("Caught exception in loadbitmap!");
}
return (null);
}
/**
*
* readMap24 internal routine to read the bytes in a 24 bit bitmap
*
*
*
* Arguments:
*
* fs - file stream
*
* bh - header struct
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
protected static Image readMap32(FileInputStream fs, BitmapHeader bh)
throws IOException
{
Image image;
// No Palatte data for 24-bit format but scan lines are
// padded out to even 4-byte boundaries.
int xwidth = bh.nsizeimage / bh.nheight;
int ndata[] = new int[bh.nheight * bh.nwidth];
byte brgb[] = new byte[bh.nwidth * 4 * bh.nheight];
fs.read(brgb, 0, bh.nwidth * 4 * bh.nheight);
int nindex = 0;
for (int j = 0; j < bh.nheight; j++)
{
for (int i = 0; i < bh.nwidth; i++)
{
ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(
brgb, nindex);
nindex += 4;
}
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.nwidth, bh.nheight,
ndata, 0, bh.nwidth));
fs.close();
return (image);
}
/**
*
* readMap24 internal routine to read the bytes in a 24 bit bitmap
*
*
*
* Arguments:
*
* fs - file stream
*
* bh - header struct
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
protected static Image readMap24(FileInputStream fs, BitmapHeader bh)
throws IOException
{
Image image;
// No Palatte data for 24-bit format but scan lines are
// padded out to even 4-byte boundaries.
int npad = (bh.nsizeimage / bh.nheight) - bh.nwidth * 3;
int ndata[] = new int[bh.nheight * bh.nwidth];
byte brgb[] = new byte[(bh.nwidth + npad) * 3 * bh.nheight];
fs.read(brgb, 0, (bh.nwidth + npad) * 3 * bh.nheight);
int nindex = 0;
for (int j = 0; j < bh.nheight; j++)
{
for (int i = 0; i < bh.nwidth; i++)
{
ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(
brgb, nindex);
nindex += 3;
}
nindex += npad;
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.nwidth, bh.nheight,
ndata, 0, bh.nwidth));
fs.close();
return (image);
}
/**
*
* readMap8 internal routine to read the bytes in a 8 bit bitmap
*
*
*
* Arguments:
*
* fs - file stream
*
* bh - header struct
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
protected static Image readMap8(FileInputStream fs, BitmapHeader bh)
throws IOException
{
Image image;
// Have to determine the number of colors, the clrsused
// parameter is dominant if it is greater than zero. If
// zero, calculate colors based on bitsperpixel.
int nNumColors = 0;
if (bh.nclrused > 0)
{
nNumColors = bh.nclrused;
}
else
{
nNumColors = (1 & 0xff) << bh.nbitcount;
}
// System.out.println("The number of Colors is"+nNumColors);
// Some bitmaps do not have the sizeimage field calculated
// Ferret out these cases and fix 'em.
if (bh.nsizeimage == 0)
{
bh.nsizeimage = ((((bh.nwidth * bh.nbitcount) + 31) & ~31) >> 3);
bh.nsizeimage *= bh.nheight;
// System.out.println("nsizeimage (backup) is"+nsizeimage);
}
// Read the palatte colors.
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++)
{
npalette[n] = constructInt3(bpalette, nindex8);
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (bh.nsizeimage / bh.nheight) - bh.nwidth;
// System.out.println("nPad is:"+npad8);
int ndata8[] = new int[bh.nwidth * bh.nheight];
byte bdata[] = new byte[(bh.nwidth + npad8) * bh.nheight];
fs.read(bdata, 0, (bh.nwidth + npad8) * bh.nheight);
nindex8 = 0;
for (int j8 = 0; j8 < bh.nheight; j8++)
{
for (int i8 = 0; i8 < bh.nwidth; i8++)
{
ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =
npalette[((int) bdata[nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.nwidth, bh.nheight,
ndata8, 0, bh.nwidth));
return (image);
}
/**
*
* load method - see read for details
*
*
*
* Arguments:
*
* sdir and sfile are the result of the FileDialog()
*
* getDirectory() and getFile() methods.
*
*
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
public static Image load(String sdir, String sfile) {
return (load(sdir + sfile));
}
/**
*
* load method - see read for details
*
*
*
* Arguments:
*
* sdir - full path name
*
*
*
* Returns:
*
* Image Object, be sure to check for (Image)null !!!!
*
*
*
*/
public static Image load(String sdir)
{
try
{
FileInputStream fs = new FileInputStream(sdir);
return (read(fs));
}
catch (IOException ex) {
return (null);
}
}
public static void main(String[] args) throws IOException
{
if (args.length == 0) {
System.out.println("Usage >java BMPLoader ImageFile.bmp");
System.exit(0);
}
FileInputStream in = new FileInputStream(args[0]);
Image TheImage = read(in);
JFrame TheFrame = new JFrame(args[0]);
JLabel TheLabel = new JLabel(new ImageIcon(TheImage));
TheFrame.getContentPane().add(new JScrollPane(TheLabel));
TheFrame.setSize(300, 300);
TheFrame.setVisible(true);
}
//end class BMPLoader
}

② 动态规划算法如何实现 .bmp灰度图像压缩的压缩与解压缩啊

诶,转回来了。但是还差一点。。到底是哪呢。。

③ BMP图片容量压缩问题

操作步骤:

第一步、双击打开图片压缩器,点击“添加文件”,将需要压缩的BMP图片添加进来。

④ 求一个c++的用lzw(字典)算法来压缩bmp图片的代码

参见gif压缩算法源代码。

1.LZW的全称是什么?
Lempel-Ziv-Welch (LZW).
2. LZW的简介和压缩原理是什么?
LZW压缩算法是一种新颖的压缩方法,由Lemple-Ziv-Welch 三人共同创造,用他们的名字命名。它采用了一种先进的串表压缩,将每个第一次出现的串放在一个串表中,用一个数字来表示串,压缩文件只存贮数字,则不存贮串,从而使图象文件的压缩效率得到较大的提高。奇妙的是,不管是在压缩还是在解压缩的过程中都能正确的建立这个串表,压缩或解压缩完成后,这个串表又被丢弃。
LZW算法中,首先建立一个字符串表,把每一个第一次出现的字符串放入串表中,并用一个数字来表示,这个数字与此字符串在串表中的位置有关,并将这个数字存入压缩文件中,如果这个字符串再次出现时,即可用表示它的数字来代替,并将这个数字存入文件中。压缩完成后将串表丢弃。如"print" 字符串,如果在压缩时用266表示,只要再次出现,均用266表示,并将"print"字符串存入串表中,在图象解码时遇到数字266,即可从串表中查出266所代表的字符串"print",在解压缩时,串表可以根据压缩数据重新生成。
3.在详细介绍算法之前,先列出一些与该算法相关的概念和词汇
1)'Character': 字符,一种基础数据元素,在普通文本文件中,它占用1个单独的byte,而在图像中,它却是 一种代表给定像素颜色的索引值。
2)'CharStream':数据文件中的字符流。
3)'Prefix':前缀。如这个单词的含义一样,代表着在一个字符最直接的前一个字符。一个前缀字符长度可以为0,一个prefix和一个character可以组成一个字符串(string),
4)'Suffix': 后缀,是一个字符,一个字符串可以由(A,B)来组成,A是前缀,B是后缀,当A长度为0的时候,代表Root,根
5)'Code:码,用于代表一个字符串的位置编码
6)'Entry',一个Code和它所代表的字符串(string)
4.压缩算法的简单示例,不是完全实现LZW算法,只是从最直观的角度看lzw算法的思想
对原始数据ABCCAABCDDAACCDB进行LZW压缩
原始数据中,只包括4个字符(Character),A,B,C,D,四个字符可以用一个2bit的数表示,0-A,1-B,2-C,3-D,从最直观的角度看,原始字符串存在重复字符:ABCCAABCDDAACCDB,用4代表AB,5代表CC,上面的字符串可以替代表示为:45A4CDDAA5DB,这样是不是就比原数据短了一些呢!
5.LZW算法的适用范围
为了区别代表串的值(Code)和原来的单个的数据值(String),需要使它们的数值域不重合,上面用0-3来代表A-D,那么AB就必须用大于3的数值来代替,再举另外一个例子,原来的数值范围可以用8bit来表示,那么就认为原始的数的范围是0~255,压缩程序生成的标号的范围就不能为0~255(如果是0-255,就重复了)。只能从256开始,但是这样一来就超过了8位的表示范围了,所以必须要扩展数据的位数,至少扩展一位,但是这样不是增加了1个字符占用的空间了么?但是却可以用一个字符代表几个字符,比如原来255是8bit,但是现在用256来表示254,255两个数,还是划得来的。从这个原理可以看出LZW算法的适用范围是原始数据串最好是有大量的子串多次重复出现,重复的越多,压缩效果越好。反之则越差,可能真的不减反增了。
6.LZW算法中特殊标记
随着新的串(string)不断被发现,标号也会不断地增长,如果原数据过大,生成的标号集(string table)会越来越大,这时候操作这个集合就会产生效率问题。如何避免这个问题呢?Gif在采用lzw算法的做法是当标号集足够大的时候,就不能增大了,干脆从头开始再来,在这个位置要插入一个标号,就是清除标志CLEAR,表示从这里我重新开始构造字典,以前的所有标记作废,开始使用新的标记。
这时候又有一个问题出现,足够大是多大?这个标号集的大小为比较合适呢?理论上是标号集大小越大,则压缩比率就越高,但开销也越高。 一般根据处理速度和内存空间连个因素来选定。GIF规范规定的是12位,超过12位的表达范围就推倒重来,并且GIF为了提高压缩率,采用的是变长的字长。比如说原始数据是8位,那么一开始,先加上一位再说,开始的字长就成了9位,然后开始加标号,当标号加到512时,也就是超过9为所能表达的最大数据时,也就意味着后面的标号要用10位字长才能表示了,那么从这里开始,后面的字长就是10位了。依此类推,到了2^12也就是4096时,在这里插一个清除标志,从后面开始,从9位再来。
GIF规定的清除标志CLEAR的数值是原始数据字长表示的最大值加1,如果原始数据字长是8,那么清除标志就是256,如果原始数据字长为4那么就是16。另外GIF还规定了一个结束标志END,它的值是清除标志CLEAR再加1。由于GIF规定的位数有1位(单色图),4位(16色)和8位(256色),而1位的情况下如果只扩展1位,只能表示4种状态,那么加上一个清除标志和结束标志就用完了,所以1位的情况下就必须扩充到3位。其它两种情况初始的字长就为5位和9位。此处参照了http://blog.csdn.net/whycadi/
7.用lzw算法压缩原始数据的示例分析
输入流,也就是原始的数据为:255,24,54,255,24,255,255,24,5,123,45,255,24,5,24,54..................
这个正好可以看到是gif文件中像素数组的一部分,如何对它进行压缩
因为原始数据可以用8bit来表示,故清除标志Clear=255+1 =256,结束标志为End=256+1=257,目前标号集为
0 1 2 3 .................................................................................255 CLEAR END
第一步,读取第一个字符为255,在标记表里面查找,255已经存在,我们已经认识255了,不做处理
第二步,取第二个字符,此时前缀为A,形成当前的Entry为(255,24),在标记集合不存在,我们并不认识255,24好,这次你小子来了,我就记住你,把它在标记集合中标记为258,然后输出前缀A,保留后缀24,并作为下一次的前缀(后缀变前缀)
第三步,取第三个字符为54,当前Entry(24,54),不认识,记录(24,54)为标号259,并输出24,后缀变前缀
第四部:取第四个字符255,Entry=(54,255),不认识,记录(54,255)为标号260,输出54,后缀变前缀
第五步 取第5个字符24,entry=(255,24),啊,认识你,这不是老258么,于是把字符串规约为258,并作为前缀
第六步 取第六个字符255,entry=(258,255),不认识,记录(258,255)为261,输出258,后缀变前缀
.......
一直处理到最后一个字符,
用一个表记录处理过程
CLEAR=256,END=257
第几步 前缀 后缀 Entry 认识(Y/N) 输出 标号
1 255 (,255)
2 255 24 (255,24) N 255 258
3 24 54 (24,54) N 24 259
4 54 255 (54,255) N 54 260
5 255 24 (255,24) Y
6 258 255 (258,255) N 258 261
7 255 255 (255,255) N 255 262
.....
上面这个示例有些不能完整体现,另外一个例子是
原输入数据为:A B A B A B A B B B A B A B A A C D A C D A D C A B A A A B A B .....
采用LZW算法对其进行压缩,压缩过程用一个表来表述为:
注意原数据中只包含4个character,A,B,C,D
用两bit即可表述,根据lzw算法,首先扩展一位变为3为,Clear=2的2次方+1=4; End=4+1=5;
初始标号集因该为

0 1 2 3 4 5
A B C D Clear End

而压缩过程为:

第几步 前缀 后缀 Entry 认识(Y/N) 输出 标号
1 A (,A)
2 A B (A,B) N A 6
3 B A (B,A) N B 7
4 A B (A,B) Y
5 6 A (6,A) N 6 8
6 A B (A,B) Y
7 6 A (6,A) Y
8 8 B (8,B) N 8 9
9 B B (B,B) N B 10
10 B B (B,B) Y
11 10 A (10,A) N 10 11
12 A B (A,B) Y

.....
当进行到第12步的时候,标号集应该为

0 1 2 3 4 5 6 7 8 9 10 11
A B C D Clear End AB BA 6A 8B BB 10A

8.LZW算法的伪代码实现

1STRING = get input character
2WHILE there are still input characters DO
3 CHARACTER = get input character
4 IF STRING+CHARACTER is in the string table then
5 STRING = STRING+character
6 ELSE
7 output the code for STRING
8 add STRING+CHARACTER to the string table
9 STRING = CHARACTER
10 END of IF
11END of WHILE
12output the code for STRING
13

⑤ 怎么压缩bmp格式的图片大小

bmp格式不能压缩的,要减小体积只能被压缩成jpg或者png格式了。用这个在线图片压缩工具试试吧,可以压缩bmp格式,想把图片文件的大小减到多少都行,直接设置一下数值就搞定,马上瞬间就能压缩完。在线智能压缩图片大小,图片压缩体积

在线图片智能压缩使用步骤:

一、首先点击加号添加需要压缩的图片。目前已知支持对jpg、png等多种常见的图片格式进行压缩,如果上传图片并压缩成功,则代表支持该图片格式。
二、可以自行修改图片需要被压缩到的最大宽高尺寸,默认为图片原始的宽高尺寸,且宽高比例是自动锁定的。
三、必须设置图片被压缩后,期望输出的图片文件的最大占用空间。(必填项)
四、选择图片生成的算法。默认为混合优先算法,绝大多数情况下使用默认算法即可。
五、压缩的设定值不能小于1Kb,但图片压缩的最终效果可以小于1Kb。

butterpig

阅读全文

与cbmp压缩算法相关的资料

热点内容
把pdf导入iphone 浏览:504
米哈游租赁的云服务器是哪个 浏览:522
android直接打电话 浏览:1015
ubuntu停止命令 浏览:283
cnc攻丝编程 浏览:868
换个手机号码app怎么注册 浏览:319
怎么下载小猴口算app 浏览:115
轻链app的货怎么样 浏览:625
电脑里的u盘如何加密 浏览:370
我的世界全部版本服务器下载地址 浏览:49
交换原理pdf 浏览:228
菜鸟驿站app怎么邀请新人 浏览:447
电脑里总是有一些1k的文件夹 浏览:44
drm加密绝对安全 浏览:512
android灭屏流程 浏览:496
如何更改站点文件夹名字 浏览:896
如何看服务器几核 浏览:276
找酒吧设计公司用什么app 浏览:683
基本初等函数的导数公式及导数的运算法则 浏览:917
为什么小米app启动广告关不了 浏览:879