📄 encoder.cpp
字号:
#include "stdafx.h"
#include "globals.h"
#include "prototypes.h"
#include "marker.h"
#include "read_write.h"
#define BLOCK 32
void EncodingImage(short int **ImageData,PBYTE DirectionBlock[BLOCK*2],long width,long height,CString OutFile)
{
extern char **dim2(int nr,int nc,unsigned size);
int i,j;
int tmp;
dim.x=height;
dim.y=width;
P = 8;//一个像素点用8位来表示
pdim.x = dim.x+1;
pdim.y = dim.y+2;
/* set the encoding parameters based on precision value */
if(P<=8)
{
T1 = 3;
T2 = 7;
T3 = 21;
}
else
{
T1 = 18;
T2 = 67;
T3 = 276;
}
MAXVAL = (int)(pow(2.0,P)-1);
// fprintf(stderr,"Precision = %d, MAXVAL = %d\n", P, MAXVAL);
/* Write Header Information */
ofp = fopen(OutFile,"wb");
WriteSoi();
WriteSof();
WriteSos();
in_buf = (int **) dim2(pdim.x,pdim.y,sizeof(int));//申请2维指针
//将ImageData中的数据传给in_buf,注意in_buf和ImageData的大小不一(长宽)
//in_buf中第0行、第0列和最后一列数据为0(未赋值)
GetData(ImageData,dim.x,dim.y,in_buf);
/* initialize the boundary conditions */
for(j=0; j<pdim.y; j++)
in_buf[0][j] = 0; //第0行数据为0
for(i=1; i<pdim.x; i++)
{
in_buf[i][0] = in_buf[i-1][1];//第0列数据与第1列数据相等
in_buf[i][pdim.y-1] = in_buf[i][pdim.y-2];//最后1列数据与倒数第2列数据相等
}
/* RANGE = (int)((MAXVAL+2*NEAR)/(2*NEAR+1))+1; */
RANGE = MAXVAL + 1;
/* updated variable */
qbpp = (int)(log(RANGE)/(log(2.0)));
bpp = MAX(2,(int)(log(MAXVAL+1)/log(2.0)));
LIMIT = 2*(bpp+MAX(8,bpp));
/* initialaize some variables */
tmp = MAX(2,(int)((RANGE+32)/64));
for(i=0; i< 365; i++)
{
A[i] = tmp;
N[i] = 1;
B[i] = 0;
Nn[i] = 0;
C[i] = 0;
}
A[365] = tmp;
A[366] = tmp;
N[365] = 1;
N[366] = 1;
RUNindex = 0;
Nn[365] = 0;
Nn[366] = 0;
x.x = 1;
x.y = 0;
count = 0;
EOLine = 0;
current_write_byte = 0;
write_position = 7;
for(;;)
{
if(count == dim.x*dim.y )
break;
GetNextSample();
D[0] = Rd - Rb;
D[1] = Rb - Rc;
D[2] = Rc - Ra;
if (D[0] == 0 && D[1] == 0 && D[2] == 0)
RunModeProcessing();
else
RegularModeProcessing();
}
if(write_position < 7)
putc(current_write_byte, ofp);
WriteEoi();
fclose(ofp);
}
/**************************************************************
AppendToBitStream() puts n bits from b onto the writer stream.
**************************************************************/
void AppendToBitStream(int b,int n)
{
if((b>>n) > 0 || b < 0)
{
fprintf(stderr,"Error: %d Bits are not enough to represent the number %d\n", n, b);
exit(0);
}
while(n--)
{
if(b&bit_set_mask[n])
{
mput1();
}
else
{
mput0();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -