Advertisement
mahmudkuet

Image Scaling Method

Feb 14th, 2016
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. public static void main(String[] args) {
  2.         int[][] img = {
  3.             {1,1,1,1,1,0,0,0,0,0},
  4.             {1,1,1,1,1,0,0,0,0,0},
  5.             {1,1,1,1,1,0,0,0,0,0},
  6.             {1,1,1,1,1,0,0,0,0,0},
  7.             {1,1,1,1,1,0,0,0,0,0},
  8.             {1,1,1,1,1,0,0,0,0,0},
  9.             {1,1,1,1,1,0,0,0,0,0},
  10.             {1,1,1,1,1,0,0,0,0,0},
  11.             {1,1,1,1,1,0,0,0,0,0},
  12.             {1,1,1,1,1,0,0,0,0,0},
  13.             {1,1,1,1,1,0,0,0,0,0},
  14.             {1,1,1,1,1,0,0,0,0,0}
  15.         };
  16.        
  17.         int[][] res = scale(img, 4, 4);
  18.         // here res.length = 12(height) and res[0].length = 10(width)
  19.         for (int i = 0; i < res[0].length; i++) {
  20.             for (int j = 0; j < res.length; j++) {
  21.                 System.out.print(res[i][j] + " ");
  22.             }
  23.             System.out.println("");
  24.         }
  25.     }
  26.    
  27.     /*
  28.     * Params:
  29.     *   img => Image Matrix of RGB value
  30.     *   w2 => New Width
  31.     *   h2 => New Height
  32.     */
  33.     public static int[][] scale(int[][] img, int w2, int h2){
  34.         int w1 = img.length;
  35.         int h1 = img[0].length;
  36.         int[][] output = new int[w2][h2];
  37.         double x_ratio = (double)w1/w2;
  38.         double y_ratio = (double)h1/h2;
  39.         for(int i=0; i< w2; i++){
  40.             for (int j = 0; j < h2; j++) {
  41.                 int rgb = img[(int)Math.floor(i*x_ratio)][(int)Math.floor(j*y_ratio)];
  42.                 output[i][j] = rgb;
  43.             }
  44.         }
  45.         return output;
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement