Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jdk.nashorn.internal.runtime.regexp.joni.ast.QuantifierNode;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import static java.lang.Math.cos;
- import static java.lang.Math.getExponent;
- import static java.lang.Math.sqrt;
- /**
- * Created by Marcin on 03.12.2016.
- */
- public class DCT {
- static int size = 8;
- static int q[][] = {
- {16,11,10,16,24,40,51,61},
- {12,12,14,19,26,58,60,55},
- {14,13,16,24,40,57,69,56},
- {14,17,22,29,51,87,80,62},
- {18,22,37,56,68,109,103,77},
- {24,35,55,64,81,104,113,92},
- {49,64,78,87,103,121,120,101},
- {72,92,95,98,112,100,103,99}
- };
- public static double[][] countDCT(int array[][], int size){
- double dct[][] = new double[size][size];
- for(int v = 0; v < size; ++v){
- for(int u = 0; u < size; ++u){
- double cu = (u == 0)? 1.0 / sqrt(2) : 1.0;
- double cv = (v== 0)? 1.0 / sqrt(2) : 1.0;
- double suma = 0;
- for(int y = 0; y < size; ++y){
- for(int x = 0; x < size; ++x){
- double uCosFactor = cos((2 * x + 1) * Math.PI * u / (2 * size));
- double vCosFactor = cos((2 * y + 1) * Math.PI * v / (2 * size));
- suma += array[x][y] * uCosFactor * vCosFactor;
- }
- }
- suma *= ((double)2 / size) * cu * cv;
- dct[u][v] = suma;
- }
- }
- return dct;
- }
- public static List<int[][]> chunks(int[][] array, int chunks){
- int size = array.length / chunks * (array[0].length / chunks);
- List<int[][]> subArrays = new ArrayList<>();
- for (int c = 0; c < size; c++) {
- int[][] sub = new int[chunks][chunks];
- int startx = (chunks * (c / chunks)) % array.length;
- int starty = (chunks * c) % array[0].length;
- if (starty + chunks > array[0].length) {
- starty = 0;
- }
- if (startx + chunks > array.length) {
- continue;
- }
- for (int row = 0; row < chunks; row++) {
- for (int col = 0; col < chunks; col++) {
- sub[row][col] = array[startx + row][col + starty];
- }
- }
- subArrays.add(sub);
- }
- return subArrays;
- }
- public static double[][] joinArrays(List<double[][]> arrays, int size){
- double joined[][] = new double[size][size];
- int counter = 0;
- int counter1 = 0;
- int lol = 0;
- for(double ar[][] : arrays)
- {
- for(int i = 0;i<8;i++)
- {
- for(int j = 0;j<8;j++)
- {
- if(counter1>7)lol+=8;
- joined[lol+i][counter+j] = ar[i][j];
- }
- }
- counter+=8;
- counter1+=1;
- }
- return joined;
- }
- public static double[][] inverseDCT(double[][] array, int size){
- double inverse[][] = new double[8][8];
- for(int x = 0; x < size; x++){
- for(int y = 0; y < size; y++){
- double pixel = 0;
- for(int v = 0; v < size; ++v) {
- for (int u = 0; u < size; ++u) {
- double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
- double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
- double uCosFactor = cos((2 * x + 1) * Math.PI * u / (2 * size));
- double vCosFactor = cos((2 * y + 1) * Math.PI * v / (2 * size));
- double point = array[u][v];
- pixel += point * uCosFactor * vCosFactor * cu * cv;
- }
- }
- pixel *= (2 / (double) size);
- inverse[x][y] = pixel;
- }
- }
- return inverse;
- }
- public static void main(String[] args) throws IOException {
- BufferedImage bf = ImageIO.read(new File("1.png"));
- int width = bf.getWidth();
- int height = bf.getHeight();
- int red[][] = new int[width][height];
- for(int i = 0; i < width; i++){
- for(int j = 0; j<height;j++){
- int pixel = bf.getRGB(i,j);
- red[i][j] = (pixel >> 16) & 0xFF;
- }
- }
- List<int[][]> chunks = chunks(red, 8);
- List<double[][]> quan = new ArrayList<>();
- for(int[][] array:chunks){
- double dct[][] = countDCT(array, 8);
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- dct[i][j] = Math.round(dct[i][j]/q[i][j]);
- }
- }
- dct = inverseDCT(dct, 8);
- quan.add(dct);
- }
- double joined[][] = joinArrays(quan, 256);
- BufferedImage bufferedImage = new BufferedImage(width,height, BufferedImage.TYPE_INT_BGR);
- for (int i = 0; i < width; i++){
- for(int j = 0; j < height; j++){
- bufferedImage.setRGB(i,j,((int)joined[i][j]<<16));
- }
- }
- File file = new File("nowy.jpeg");
- ImageIO.write(bufferedImage, "jpeg", file);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement