Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- int[][] img = {
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0},
- {1,1,1,1,1,0,0,0,0,0}
- };
- int[][] res = scale(img, 4, 4);
- // here res.length = 12(height) and res[0].length = 10(width)
- for (int i = 0; i < res[0].length; i++) {
- for (int j = 0; j < res.length; j++) {
- System.out.print(res[i][j] + " ");
- }
- System.out.println("");
- }
- }
- /*
- * Params:
- * img => Image Matrix of RGB value
- * w2 => New Width
- * h2 => New Height
- */
- public static int[][] scale(int[][] img, int w2, int h2){
- int w1 = img.length;
- int h1 = img[0].length;
- int[][] output = new int[w2][h2];
- double x_ratio = (double)w1/w2;
- double y_ratio = (double)h1/h2;
- for(int i=0; i< w2; i++){
- for (int j = 0; j < h2; j++) {
- int rgb = img[(int)Math.floor(i*x_ratio)][(int)Math.floor(j*y_ratio)];
- output[i][j] = rgb;
- }
- }
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement