Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. /*
  2. * Project 2: Image lab
  3. *
  4. * Name: Josh Wojcik
  5. * Email: jjw68@pitt.edu
  6. *
  7. */
  8.  
  9. import java.awt.Color;
  10. import java.awt.image.BufferedImage;
  11. import java.io.*;
  12. import javax.imageio.ImageIO;
  13.  
  14.  
  15. public class ImageLab3 {
  16.  
  17. public static BufferedImage threshold (BufferedImage input, int level)
  18. {
  19. int img_height = input.getHeight();
  20. int img_width = input.getWidth();
  21. BufferedImage output_img = new BufferedImage (img_width, img_height, BufferedImage. TYPE_INT_RGB);
  22.  
  23.  
  24. for (int x = 0; x < img_width; x++)
  25. {
  26. for (int y = 0; y < img_height; y++)
  27. {
  28. int r = getRedValue(input, x, y);
  29. int g = getGreenValue(input, x, y);
  30. int b = getBlueValue(input, x, y);
  31.  
  32. int average = (r + g + b) / 3;
  33.  
  34. if (average < level) {
  35. average = 0;
  36. } else if (average > level) {
  37. average = 255;
  38. }
  39.  
  40. Color average_color = new Color(average, average, average);
  41. output_img.setRGB(x, y, average_color.getRGB());
  42. }
  43. }
  44. return output_img;
  45. }
  46.  
  47. public static int getRedValue(BufferedImage img, int x, int y) {
  48. Color col = new Color(img.getRGB(x,y));
  49. int red = col.getRed();
  50. return red;
  51. }
  52.  
  53. public static int getGreenValue(BufferedImage img, int x, int y) {
  54. Color col = new Color(img.getRGB(x,y));
  55. int green = col.getGreen();
  56. return green;
  57. }
  58.  
  59. public static int getBlueValue(BufferedImage img, int x, int y) {
  60. Color col = new Color(img.getRGB(x,y));
  61. int blue = col.getBlue();
  62. return blue;
  63. }
  64.  
  65. public static void setColor(BufferedImage img, int x, int y, int r, int g, int b) {
  66. Color c = new Color(r, g, b);
  67. img.setRGB(x, y, c.getRGB());
  68. }
  69.  
  70.  
  71. public static BufferedImage colorRotate(BufferedImage input) {
  72.  
  73. int img_width = input.getWidth();
  74. int img_height = input.getHeight();
  75. BufferedImage output_img = new BufferedImage(
  76. img_width, img_height, BufferedImage.TYPE_INT_RGB);
  77.  
  78. for (int x = 0; x < img_width; x++) {
  79. for (int y = 0; y < img_height; y++) {
  80.  
  81. Color color_at_pos = new Color(input.getRGB(x, y));
  82.  
  83. int red = color_at_pos.getRed();
  84. int green = color_at_pos.getGreen();
  85. int blue = color_at_pos.getBlue();
  86.  
  87. Color rotate = new Color(blue, red, green);
  88.  
  89. output_img.setRGB(x, y, rotate.getRGB());
  90.  
  91. }
  92. }
  93. return output_img;
  94. }
  95. }
  96.  
  97. public static BufferedImage quantize(BufferedImage input) {
  98.  
  99. int img_height = input.getHeight();
  100. int img_width = input.getWidth();
  101. BufferedImage output_img = new BufferedImage (img_width, img_height, BufferedImage. TYPE_INT_RGB);
  102.  
  103. for (int x = 0; x < img_width; x++) {
  104. for (int y = 0; y < img_height; y++) {
  105.  
  106. Color color_at_pos = new Color(input.getRGB(x, y));
  107.  
  108. int quantized_red = color_at_pos.getRed();
  109. int quantized_green = color_at_pos.getGreen();
  110. int quantized_blue = color_at_pos.getBlue();
  111.  
  112. quantized_red = (quantized_red /64) * 85;
  113. quantized_green = (quantized_green / 64) * 85;
  114. quantized_blue = (quantized_blue / 64) * 85;
  115.  
  116. Color quantized_color = new Color(quantized_red, quantized_green, quantized_blue);
  117. output_img.setRGB(x, y, quantized_color.getRGB());
  118.  
  119. }
  120. }
  121. return output_img;
  122. }
  123. }
  124.  
  125. public static BufferedImage warhol(BufferedImage input) {
  126.  
  127. int img_height = input.getHeight();
  128. int img_width = input.getWidth();
  129.  
  130. BufferedImage output_img = new BufferedImage(img_width * 3, img_height, BufferedImage.TYPE_INT_RGB);
  131.  
  132. for (int x = 0; x < img_width; x++) {
  133. for (int y = 0; y < img_height; y++) {
  134.  
  135. Color color_at_pos = new Color (input.getRGB(x, y));
  136.  
  137. int red = color_at_pos.getRed();
  138. int green = color_at_pos.getGreen();
  139. int blue = color_at_pos.getBlue();
  140.  
  141. Color warhol_color = new Color(red, green, blue);
  142. output_img.setRGB(x, y, warhol_color.getRGB());
  143.  
  144. Color warhol_color_1 = new Color(red, green, blue);
  145. Color warhol_color_2 = new Color(red, green, blue);
  146. Color warhol_color_3 = new Color(red, green, blue);
  147.  
  148. output_img.setRGB(x, y, warhol_color_1.getRGB());
  149. output_img.setRGB(x + img_width, y, warhol_color_2.getRGB());
  150. output_img.setRGB(x + 2 * img_width, y, warhol_color_3.getRGB());
  151.  
  152. }
  153. }
  154. return output_img;
  155. }
  156. }
  157. public static BufferedImage sharpen(BufferedImage input) {
  158.  
  159. int img_width = input.getWidth();
  160. int img_height = input.getHeight();
  161. BufferedImage output_img = new BufferedImage(img_width, img_height, BufferedImage.TYPE_INT_RGB);
  162.  
  163. for (int x = 0; x < img_width; x++) {
  164. for (int y = 0; y < img_height; y++) {
  165.  
  166. Color color_at_pos = new Color(input.getRGB(x, y));
  167.  
  168. int sharpened_red = 5 * color_at_pos.getRed();
  169. int sharpened_blue = 5 * color_at_pos.getBlue();
  170. int sharpened_green = 5 * color_at_pos.getGreen();
  171.  
  172. Color new_sharpened_color = new Color(sharpened_red, sharpened_blue, sharpened_green);
  173. output_img.setRGB(x, y, new_sharpened_color.getRGB());
  174.  
  175. //get the r, g, b from the left neighbor
  176. //left is valid if x > 0
  177. if (x > 0) {
  178. Color left_color = new Color(input.getRGB(x-1, y));
  179. sharpened_red -= left_color.getRed();
  180. sharpened_blue -= left_color.getBlue();
  181. sharpened_green -= left_color.getGreen();
  182. }
  183.  
  184. if (x < 0) {
  185. Color right_color = new Color(input.getRGB(x+1, y));
  186. sharpened_red -= right_color.getRed();
  187. sharpened_blue -= right_color.getBlue();
  188. sharpened_green -= right_color.getGreen();
  189. }
  190.  
  191. if (x < 255) {
  192. Color top_color = new Color(input.getRGB(x, y-1));
  193. sharpened_red -= top_color.getRed();
  194. sharpened_blue -= top_color.getBlue();
  195. sharpened_green -= top_color.getGreen();
  196. }
  197.  
  198. if (x > 255) {
  199. Color bottom_color = new Color(input.getRGB(x, y+1));
  200. sharpened_red -= bottom_color.getRed();
  201. sharpened_blue -= bottom_color.getBlue();
  202. sharpened_green -= bottom_color.getGreen();
  203. /*
  204. Color left_color = new Color(input.getRGB(x, y));
  205. Color right_color = new Color(input.getRGB(x, y));
  206.  
  207. Color left_color;
  208.  
  209. if (x > 0) {
  210. left_color = new Color(input.getRGB(x-1, y));
  211. } else {
  212. left_color = new Color(0, 0, 0);
  213. }
  214. */
  215. // do for top, bottom, and right
  216. if (sharpened_red > 255) {
  217. sharpened_red = 255;
  218. } else if (sharpened_red < 0) {
  219. sharpened_red = 0;
  220. }
  221. if (sharpened_blue > 255) {
  222. sharpened_blue = 255;
  223. } else if (sharpened_blue < 0) {
  224. sharpened_blue = 0;
  225. }
  226. if (sharpened_green > 255) {
  227. sharpened_green = 255;
  228. } else if (sharpened_green < 0) {
  229. sharpened_green = 0;
  230. }
  231.  
  232. Color computed_color = new Color(sharpened_red, sharpened_green, sharpened_blue);
  233. output_img.setRGB(x,y,computed_color.getRGB());
  234. }
  235. }
  236.  
  237. return output_img;
  238. }
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement