Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //changed the signature of Khai's function
- public static int byteToInteger(byte b) {
- return (short) ((short) b & 0xff);
- }
- public static int[] correctedPixels(byte [] pixels)
- {
- int [] corrected_pixels = new int[pixels.length];
- for(int i = 0 ; i < pixels.length ; i++)
- corrected_pixels[i] = byteToInteger(pixels[i]);
- return corrected_pixels;
- }
- /*
- * Predictive Coding
- */
- public static int[] applyPredictiveCoding(int option, int[] channel, int width)
- {
- int [] pc_errors = new int[channel.length];
- int A = 0, B = 0, C = 0;
- for(int i = 0 ; i < pc_errors.length ; i++)
- {
- if( i % width == 0 || i < width)
- {
- pc_errors[i] = channel[i];
- continue;
- }
- if(option != 1)
- {
- A = channel[i - 1];
- B = channel[i - width];
- C = channel[i - width - 1];
- }
- switch(option)
- {
- case 1:
- return channel;
- // break;
- case 2:
- pc_errors[i] = (channel[i] - A);
- break;
- case 3:
- pc_errors[i] = (channel[i] - B);
- break;
- case 4:
- pc_errors[i] = (channel[i] - C);
- break;
- case 5:
- pc_errors[i] = (channel[i] - ((A + B + C) / 3));
- break;
- case 6:
- pc_errors[i] = (channel[i] - (A + B - C));
- break;
- case 7:
- pc_errors[i] = (channel[i] - ((A + B) /2));
- break;
- case 8:
- if( (B - C > 0) && (A - C > 0) )
- pc_errors[i] = channel[i] - (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
- else if( (B - C < 0) && (A - C < 0) )
- pc_errors[i] = channel[i] - (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
- else
- pc_errors[i] = channel[i] - ((A + B) /2);
- break;
- default:
- //wrong input
- break;
- }
- }
- return pc_errors;
- }
- public static int[] removePredictiveCoding(int option, int[] pc_errors, int width)
- {
- int [] channel = new int[pc_errors.length];
- int A = 0, B = 0, C = 0;
- for(int i = 0 ; i < channel.length ; i++)
- {
- if( i % width == 0 || i < width)
- {
- channel[i] = pc_errors[i];
- continue;
- }
- if(option != 1)
- {
- A = pc_errors[i - 1];
- B = pc_errors[i - width];
- C = pc_errors[i - width - 1];
- }
- switch(option)
- {
- case 1:
- return pc_errors;
- // break;
- case 2:
- channel[i] = (pc_errors[i] + A);
- break;
- case 3:
- channel[i] = (pc_errors[i] + B);
- break;
- case 4:
- channel[i] = (pc_errors[i] + C);
- break;
- case 5://correct from here
- channel[i] = (pc_errors[i] + ((A + B + C) / 3));
- break;
- case 6:
- channel[i] = (pc_errors[i] + (A + B - C));
- break;
- case 7:
- channel[i] = (pc_errors[i] + ((A + B) /2));
- break;
- case 8:
- if( (B - C > 0) && (A - C > 0) )
- channel[i] = pc_errors[i] + (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
- else if( (B - C < 0) && (A - C < 0) )
- channel[i] = pc_errors[i] + (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
- else
- channel[i] = pc_errors[i] + ((A + B) /2);
- break;
- default:
- //wrong input
- break;
- }
- }
- return channel;
- }
- public static void main(String[] args) throws MagickException {
- Image test = new Image("Tulips.jpg");
- byte[] red = test.getLayer(0);
- int[] int_red = correctedPixels(red);
- int[] pc = applyPredictiveCoding(2,int_red,test.w);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement