📄 encode.cpp
字号:
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u1[width] = curr = recon;
E0[width] = err;
}
#else
E0[width] = err = curr - temp;
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
++H[k][nremap (err, temp)];
encode_symbol (k, nremap (err, temp));
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
u1[width+1] = u1[width];
}
#endif
//NEARLY LOSSLESS COMPRESSION
Dh[width] = ABS (curr - w);
Dv[width] = ABS (curr - n);
//From 3rd row process each row
for (j = 2; j < height; ++j)
{
//First pixel of each row
//if(j%8==0) printf("%3d ",j); //ZN:
nn = u2[1];
n = u1[1];
ne = u1[2];
curr = u0[1];
temp = n + (ne - nn)/4;
if (temp > UPPER) temp = UPPER;
else if (temp < 0) temp = 0;
k = nh (2*ABS (n - ne) + 2*ABS (n - nn));
err = curr - temp;
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
err = quant2 (err);
recon = temp + err;
if (recon < 0) recon = 0;
if (recon > UPPER) recon = UPPER;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[0] = u0[1] = curr = recon;
}
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
++H[k][nremap (err, temp)];
encode_symbol (k, nremap (err, temp));
//Second pixel of each row
nn = u2[2];
nne = u2[3];
nw = u1[1];
n = u1[2];
ne = u1[3];
w = u0[1];
curr = u0[2];
temp = (w + n + 1)/2 + (ne - nw)/4;
if (temp > UPPER) temp = UPPER;
else if (temp < 0) temp = 0;
k = nh (aerr + ABS (n - ne) + ABS (n - nw) + ABS (n - nn) + ABS (ne - nne) + ABS (w - nw));
E0[1] = err;
err = curr - temp;
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
err = quant2 (err);
recon = temp + err;
if (recon < 0) recon = 0;
if (recon > UPPER) recon = UPPER;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[2] = curr = recon;
}
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
++H[k][nremap (err, temp)];
encode_symbol (k, nremap (err, temp));
//Rest of the row
for (x = 3; x <= width - 1; ++x)
{
nw = u1[x-1];
n = u1[x];
ne = u1[x+1];
ww = u0[x-2];
w = u0[x-1];
curr = u0[x];
symb1 = symb2 = w;
Dv[x-1] = ABS (symb1 - nw);
Dh[x-1] = ABS (symb1 - ww);
if (ne != symb1) symb2 = ne;
else if (n != symb1) symb2 = n;
else if (nw != symb1) symb2 = nw;
else if (ww != symb1) symb2 = ww;
bflag = 0;
if (symb1 == symb2) //only one symbol
{
bflag = 1;
//ZN:
tolerance=QT[0];
mask=QS[0];
//
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
if ((curr != symb1) && (ABS (symb1 - curr) <= tolerance))
{
recon = symb1;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[x] = curr = recon;
}
}
#endif
//NEARLY LOSSLESS COMPRESSION
if (curr == symb1)
{
++BH[0][0];
bencode_symbol (0, 0);
E0[x-1] = err;
aerr = err = 0;
}
else
{
//only TWO symbols: symb1 and esc
++BH[0][1];
bencode_symbol (0, 1);
temp = symb1;
E0[x-1] = err;
err = curr - temp;
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
err = quant2 (err);
recon = temp + err;
if (recon < 0) recon = 0;
if (recon > UPPER) recon = UPPER;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[x] = curr = recon;
}
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
++H[N_SEQ-2][nremap (err, temp)];
encode_symbol (N_SEQ-2, nremap (err, temp));
}
}
else
{
nn = u2[x];
nne = u2[x+1];
if ((nne != symb1) && (nne != symb2)) bflag = 0;
else if ((nn != symb1) && (nn != symb2)) bflag = 0;
else if ((ne != symb1) && (ne != symb2)) bflag = 0;
else if ((n != symb1) && (n != symb2)) bflag = 0;
else if ((nw != symb1) && (nw != symb2)) bflag = 0;
else if ((ww != symb1) && (ww != symb2)) bflag = 0;
else bflag = 2; /* two symbols */
}
if (bflag == 2)
{
//ZN:
tolerance=QT[0];
mask=QS[0];
//
if (ww == symb1) ptn = 1; else ptn = 0;
if (nw == symb1) ptn |= 02;
if (n == symb1) ptn |= 04;
if (ne == symb1) ptn |= 010;
if (nn == symb1) ptn |= 020;
if (symb1 < symb2) ptn |= 040;
//pass out symb1, symb2, esc
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
if ((curr != symb1) && (curr != symb2) && ((ABS (symb1 - curr) <= tolerance) || (ABS (symb2 - curr) <= tolerance)))
{
if (ABS (symb1 - curr) <= ABS (symb2 - curr))
{
recon = symb1;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[x] = curr = recon;
}
else
{
recon = symb2;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[x] = curr = recon;
}
}
}
#endif
//NEARLY LOSSLESS COMPRESSION
if (curr == symb1)
{
++BH[ptn][0];
bencode_symbol (ptn, 0);
E0[x-1] = err;
aerr = err = 0;
}
else if (curr == symb2)
{
++BH[ptn][1];
bencode_symbol (ptn, 1);
E0[x-1] = err;
aerr = err = 0;
}
else
{
++BH[ptn][2];
bencode_symbol (ptn, 2);
temp = symb1;
E0[x-1] = err;
err = curr - temp;
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
err = quant2 (err);
recon = temp + err;
if (recon < 0) recon = 0;
if (recon > UPPER) recon = UPPER;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[x] = curr = recon;
}
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
++H[N_SEQ-1][nremap (err, temp)];
encode_symbol (N_SEQ-1, nremap (err, temp));
}
}
if (bflag == 0)
{
nnww = u2[x-2];
nnw = u2[x-1];
nww = u1[x-2];
nee = u1[x+2];
www = u0[x-3];
dv = Dv[x-1] + Dv[x] + Dv[x+1]/2;
dh = Dh[x-1] + Dh[x] + Dh[x+1];
k = nh (dv + dh + aerr + ABS (E0[x]) + (ABS (E0[x-1]) + ABS (E0[x+1]))/2);
//This computation can be made incremental to reduce running time
min = max = w;
if (n < min) min = n;
else if (n > max) max = n;
if (nw < min) min = nw;
else if (nw > max) max = nw;
if (ne < min) min = ne;
else if (ne > max) max = ne;
int tempmed,tempgap;
symb1 = dv - dh;
temp = (w + n)/2 + (ne - nw)/4;
if (symb1 > 32) temp = (temp + w)/2;
else if (symb1 > 8) temp = (temp + (temp << 1) + w)/4;
else if (symb1 < -31) temp = (temp + n)/2;
else if (symb1 < -8) temp = (temp + (temp << 1) + n)/4;
tempgap = temp;
if (w < n) { symb1 = w; symb2 = n; }
else { symb1 = n; symb2 = w; }
if (nw >= symb2) temp = symb1;
else if (nw <= symb1) temp = symb2;
else temp = w + n - nw;
tempmed = temp;
if ( max-min<100 || Pr[x-1]+Pr[x]+Pr[x+1]<2 )
temp = tempgap;
else temp = tempmed;
if (w < temp) ptn0 = 01; else ptn0 = 0;
if (n < temp) ptn0 |= 02;
if (nw < temp) ptn0 |= 04;
if (ne < temp) ptn0 |= 010;
if (nn < temp) ptn0 |= 020;
if (ww < temp) ptn0 |= 040;
if (2*w-ww < temp) ptn0 |= 0100;
if (2*n-nn < temp) ptn0 |= 0200;
ptn0 |= ((k/2) << 8);
if (nww < temp) ptn1 = ptn0 | 02000; else ptn1 = ptn0;
if (nnw < temp) ptn1 |= 04000;
if (nee < temp) ptn2 = ptn0 | 02000; else ptn2 = ptn0;
if (nne < temp) ptn2 |= 04000;
//lw
symb1 = symb2 = 0;
if (N1[ptn1] > 3)
{
symb1 += N1[ptn1];
symb2 += S1[ptn1];
}
adjust = symb2/(symb1 + 1);
temp += adjust;
if (temp > max) temp = max;
else if (temp < min) temp = min;
symb1 = S1[ptn1] + S2[ptn2];
symb1 += err + E0[x] + E0[x-1] + E0[x+1];
E0[x-1] = err;
err = curr - temp;
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
//zn:
tolerance=QT[k];
mask=QS[k];
err = quant2 (err);
recon = temp + err;
if (recon < 0) recon = 0;
if (recon > UPPER) recon = UPPER;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[x] = curr = recon;
}
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
if (symb1 < 0)
{
++H[k][nremap (-err, temp)];
encode_symbol (k, nremap (-err, temp));
}
else
{
++H[k][nremap (err, temp)];
encode_symbol (k, nremap (err, temp));
}
if (ABS (err) < (16*((k >> 1) + 1)))
{
if (N1[ptn1] == 63)
{
if (ABS (S1[ptn1]) > 128)
{
S1[ptn1] = ((S1[ptn1] + err)/4);
N1[ptn1] = 16;
}
else
{
S1[ptn1] = ((S1[ptn1] + err)/2);
N1[ptn1] = 32;
}
}
else
{
S1[ptn1] += err;
N1[ptn1]++;
}
if (N2[ptn2] == 63)
{
if (ABS (S2[ptn2]) > 128)
{
S2[ptn2] = ((S2[ptn2] + err)/4);
N2[ptn2] = 16;
}
else
{
S2[ptn2] = ((S2[ptn2] + err)/2);
N2[ptn2] = 32;
}
}
else
{
S2[ptn2] += err;
N2[ptn2]++;
}
}
} //if flag==0
}//x
//Last pixel
nn = u2[width];
nw = u1[width-1];
n = u1[width];
ww = u0[width-2];
w = u0[width-1];
curr = u0[width];
Dh[width-1] = ABS (w - ww);
Dv[width-1] = ABS (w - nw);
temp = (w + n + 1)/2;
if (temp > UPPER) temp = UPPER;
else if (temp < 0) temp = 0;
k = nh (aerr + ABS (n - nw) + ABS (n - nn) + ABS (w - ww) + ABS (w - nw));
E0[width-1] = err;
err = curr - temp;
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
err = quant2 (err);
recon = temp + err;
if (recon < 0) recon = 0;
if (recon > UPPER) recon = UPPER;
if (ABS (MaxError) < ABS (curr - recon)) MaxError = curr - recon;
u0[width] = curr = recon;
}
#endif
//NEARLY LOSSLESS COMPRESSION
aerr = ABS (err);
++H[k][nremap (err, temp)];
encode_symbol (k, nremap (err, temp));
//NEARLY LOSSLESS COMPRESSION
#ifdef OPTION_NEARLY_LOSSLESS
{
u0[width+1] = u0[width];
}
#endif
//NEARLY LOSSLESS COMPRESSION
Dh[width] = ABS (curr - w);
Dv[width] = ABS (curr - n);
E0[width] = err;
//Update input buffer here
if (j < (height - 1)) update_buffer (&u0, &u1, &u2, width);
}//j
//Free the memory
free (N1); free (S1);
free (N2); free (S2);
free (Dh); free (Dv);
free (E0); free (Pr);
}
//Function : main
void main (int argc, char *argv[])
{
unsigned char *line_curr;
unsigned char *line_1_up;
unsigned char *line_2_up;
int width, height, depth;
//Check if the number of arguments is correct
if (argc != 7)
{
fprintf (stderr, "USAGE : %s InputFile OutputFile Width Height Depth PAE\n", argv[0]);
exit (1);
}
//Create output file
if (!(outfile = fopen (argv[2], "wb")))
{
fprintf (stderr, "ERROR : cannot open output file %s!\n", argv[2]);
exit (1);
}
//Determine the dimension of the input file
width = atoi (argv[3]);
height = atoi (argv[4]);
if ((depth = atoi (argv[5])) != 8)
{
fprintf (stderr, "ERROR : the input must be an 8-bits image!\n");
exit (1);
}
tolerance=atoi(argv[6]);
mask=(tolerance<<1)+1;
for(int i=0;i<=7;i++)
{
QT[i]=tolerance;
QS[i]=mask;
}
/*
if(tolerance>3)
{
QT[0]=7;
QS[0]=15;
QT[1]=7;
QS[1]=15;
}*/
//Process the input file
init_bcoder ();
init_scoder ();
init_mycoder ();
create_buffer (&line_curr, &line_1_up, &line_2_up, width, argv[1]);
encode_buffer (line_curr, line_1_up, line_2_up, width, height);
destory_buffer (&line_curr, &line_1_up, &line_2_up);
stop_mycoder ();
fclose(outfile);
double bpp=(double)(bytes_output<<3)/(width*height);
double CR=depth/bpp;
printf("Bit Rate: %.3f Compression Ratio: %.3f:1 Peak Absulute Error:%d\n",bpp,CR,ABS(MaxError));
// printf("%.3f\t\t%.3f\n",bpp,CR);
FILE* fp;
fp=fopen("encode.log","a");
fprintf(fp,"%s--Bit Rate: %.3f Compression Ratio: %.3f:1\n",argv[1],bpp,CR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -