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.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 dane[][] = {
- {186,198,199,190,182,177,182,197},
- {179,184,183,176,173,172,175,184},
- {188,182,180,178,174,172,171,166},
- {132,130,139,146,151,169,191,201},
- {131,134,137,140,139,139,139,138},
- {153,157,161,172,177,145,89,49},
- {190,178,192,196,120,43,39,47},
- {176,184,187,112,41,39,43,44},
- };
- 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 int[][] joinArrays(List<int[][]> arrays, int size){
- int joined[][] = new int[size][size];
- int a = 0;
- int b = 0;
- int ilosc = 0;
- int max = size / 8;
- for(int[][] ar : arrays){
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- joined[a][b] = ar[i][j];
- b++;
- }
- a++;
- }
- a=0;
- }
- return joined;
- }
- 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;
- }
- }
- int test[][] = new int[16][16];
- for(int i = 0; i < 16;)
- for(int j = 0; j < 16; j++)
- test[i][j] = j;
- //List<int[][]> chunks = chunks(red, 8);
- List<int[][]> chunks = chunks(test, 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.floor(dct[i][j] / 100);
- }
- }
- quan.add(dct);
- }*/
- int afterQuantisation[][] = joinArrays(chunks, 16);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement