📄 invdct.java
字号:
package jpeg;
import java.lang.*;
import java.util.*;
import jcp.*;
/**
* InvDCT.java
* A component that computes the inverse DCT of a block
*
* Input(s):
* in - Takes an 8x8 matrix of integers as input.
* Ouput(s):
* out - Outputs an 8x8 matrix of the inverse DCT of the input.
*
* @see IntMatrix
*/
public class InvDCT extends SynchComponent
{
// Block size
private final static int N = 8;
// InvDCT coefficients
private double[][] c;
private Port in_p, out_p;
public InvDCT() {
setName("InvDCT");
in_p = addPort(true,"in");
out_p = addPort(false,"out");
c = new double[N][N];
initMatrix();
//new inst.InstMathOps (this);
}
public void go(Port port) {
Object s = port.signal();
if (s instanceof IntMatrix) {
int[][] matrix = ((IntMatrix)s).get_matrix();
emit(new IntMatrix(inverseDCT(matrix)),out_p);
}
}
int output[][] = new int[N][N];
double temp[][] = new double[N][N];
double temp1;
public int[][] inverseDCT(int input[][])
{
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
temp[i][j] = 0.0;
for (int k=0; k<N; k++) {
temp[i][j] += input[i][k] * c[k][j];
}
}
}
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
temp1 = 0.0;
for (int k=0; k<N; k++) {
temp1 += c[k][i] * temp[k][j];
}
temp1 += 128.0;
if (temp1 < 0) {
output[i][j] = 0;
} else if (temp1 > 255) {
output[i][j] = 255;
} else {
output[i][j] = (int)Math.round(temp1);
}
}
}
return output;
}
private void initMatrix()
{
for (int i = 0; i < N; i++) {
double nn = (double)(N);
c[0][i] = 1.0 / Math.sqrt(nn);
}
for (int i = 1; i < N; i++) {
for (int j = 0; j < N; j++) {
double jj = (double)j;
double ii = (double)i;
c[i][j] = Math.sqrt(2.0/8.0) * Math.cos(((2.0 * jj + 1.0) * ii * Math.PI) / (2.0 * 8.0));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -