Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. //changed the signature of Khai's function
  2. public static int byteToInteger(byte b) {
  3. return (short) ((short) b & 0xff);
  4. }
  5.  
  6.  
  7. public static int[] correctedPixels(byte [] pixels)
  8. {
  9. int [] corrected_pixels = new int[pixels.length];
  10.  
  11. for(int i = 0 ; i < pixels.length ; i++)
  12. corrected_pixels[i] = byteToInteger(pixels[i]);
  13.  
  14.  
  15. return corrected_pixels;
  16. }
  17.  
  18.  
  19. /*
  20. * Predictive Coding
  21. */
  22. public static int[] applyPredictiveCoding(int option, int[] channel, int width)
  23. {
  24. int [] pc_errors = new int[channel.length];
  25. int A = 0, B = 0, C = 0;
  26.  
  27. for(int i = 0 ; i < pc_errors.length ; i++)
  28. {
  29.  
  30. if( i % width == 0 || i < width)
  31. {
  32. pc_errors[i] = channel[i];
  33. continue;
  34. }
  35.  
  36. if(option != 1)
  37. {
  38. A = channel[i - 1];
  39. B = channel[i - width];
  40. C = channel[i - width - 1];
  41. }
  42.  
  43.  
  44. switch(option)
  45. {
  46. case 1:
  47. return channel;
  48. // break;
  49.  
  50. case 2:
  51. pc_errors[i] = (channel[i] - A);
  52. break;
  53.  
  54. case 3:
  55. pc_errors[i] = (channel[i] - B);
  56. break;
  57.  
  58. case 4:
  59. pc_errors[i] = (channel[i] - C);
  60. break;
  61.  
  62. case 5:
  63. pc_errors[i] = (channel[i] - ((A + B + C) / 3));
  64. break;
  65.  
  66. case 6:
  67. pc_errors[i] = (channel[i] - (A + B - C));
  68. break;
  69.  
  70. case 7:
  71. pc_errors[i] = (channel[i] - ((A + B) /2));
  72. break;
  73.  
  74. case 8:
  75. if( (B - C > 0) && (A - C > 0) )
  76. pc_errors[i] = channel[i] - (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
  77. else if( (B - C < 0) && (A - C < 0) )
  78. pc_errors[i] = channel[i] - (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
  79. else
  80. pc_errors[i] = channel[i] - ((A + B) /2);
  81. break;
  82.  
  83. default:
  84. //wrong input
  85. break;
  86.  
  87. }
  88.  
  89. }
  90.  
  91. return pc_errors;
  92. }
  93.  
  94. public static int[] removePredictiveCoding(int option, int[] pc_errors, int width)
  95. {
  96. int [] channel = new int[pc_errors.length];
  97. int A = 0, B = 0, C = 0;
  98.  
  99. for(int i = 0 ; i < channel.length ; i++)
  100. {
  101.  
  102. if( i % width == 0 || i < width)
  103. {
  104. channel[i] = pc_errors[i];
  105. continue;
  106. }
  107.  
  108. if(option != 1)
  109. {
  110. A = pc_errors[i - 1];
  111. B = pc_errors[i - width];
  112. C = pc_errors[i - width - 1];
  113. }
  114.  
  115.  
  116. switch(option)
  117. {
  118. case 1:
  119. return pc_errors;
  120. // break;
  121.  
  122. case 2:
  123. channel[i] = (pc_errors[i] + A);
  124. break;
  125.  
  126. case 3:
  127. channel[i] = (pc_errors[i] + B);
  128. break;
  129.  
  130. case 4:
  131. channel[i] = (pc_errors[i] + C);
  132. break;
  133.  
  134. case 5://correct from here
  135. channel[i] = (pc_errors[i] + ((A + B + C) / 3));
  136. break;
  137.  
  138. case 6:
  139. channel[i] = (pc_errors[i] + (A + B - C));
  140. break;
  141.  
  142. case 7:
  143. channel[i] = (pc_errors[i] + ((A + B) /2));
  144. break;
  145.  
  146. case 8:
  147. if( (B - C > 0) && (A - C > 0) )
  148. channel[i] = pc_errors[i] + (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
  149. else if( (B - C < 0) && (A - C < 0) )
  150. channel[i] = pc_errors[i] + (int) (C + Math.sqrt(Math.pow(B-C, 2) + Math.pow(A-C, 2))) ;
  151. else
  152. channel[i] = pc_errors[i] + ((A + B) /2);
  153. break;
  154.  
  155. default:
  156. //wrong input
  157. break;
  158.  
  159. }
  160.  
  161. }
  162.  
  163. return channel;
  164. }
  165.  
  166. public static void main(String[] args) throws MagickException {
  167.  
  168. Image test = new Image("Tulips.jpg");
  169.  
  170. byte[] red = test.getLayer(0);
  171. int[] int_red = correctedPixels(red);
  172. int[] pc = applyPredictiveCoding(2,int_red,test.w);
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement