Advertisement
Guest User

StegoImage

a guest
Oct 22nd, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. /**
  2. * @author...
  3. * @version October 2019
  4. */
  5. import javax.imageio.ImageIO;
  6. import java.awt.*;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.awt.Color;
  11. import java.lang.IllegalArgumentException;
  12.  
  13. public class StegoImage
  14. {
  15. BufferedImage image;
  16. int width;
  17. int height;
  18. public StegoImage(String filename)
  19. {
  20. //create a StegoImage by reading an image from a file.
  21. try
  22. {
  23. File file = new File(filename);
  24. image = ImageIO.read(file);
  25. width = image.getWidth();
  26. height = image.getHeight();
  27. }
  28. catch (IOException e)
  29. {
  30. e.printStackTrace();
  31. }
  32. catch (NullPointerException e)
  33. {
  34. System.out.println("NullPointerException Caught");
  35. }
  36. if(image.getHeight() <= 0 && image.getWidth() <= 0)
  37. {
  38. throw new IllegalArgumentException("Please input an image with valid dimensions");
  39. }
  40. }
  41.  
  42. public BufferedImage getImage()
  43. {
  44. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  45. return image;
  46. }
  47.  
  48. public int getHeight()
  49. {
  50. height = StegoViewer.IMG_HEIGHT;
  51. return height;
  52. }
  53.  
  54. public int getWidth()
  55. {
  56. width = StegoViewer.IMG_WIDTH;
  57. return width;
  58. }
  59.  
  60. public void scaleImage(int div, int mult)
  61. {
  62. //Modify this image by changing every pixel
  63. for(int y = 0; y < image.getHeight(); y++)
  64. {
  65. for(int x = 0; x < image.getWidth(); x++)
  66. {
  67. if(div > 0 && mult > 0)
  68. {
  69. Color s = new Color(image.getRGB(x,y));
  70. int r = s.getRed();
  71. int g = s.getGreen();
  72. int b = s.getBlue();
  73.  
  74. r = (r/div)*mult;
  75. g = (g/div)*mult;
  76. b = (b/div)*mult;
  77.  
  78. image.setRGB(r,g,b);
  79. }
  80. else
  81. {
  82. break;
  83. }
  84. }
  85. }
  86. }
  87.  
  88. public void clearLowBit()
  89. {
  90. //Modify this image by setting the lowest bit of each pixel colour to 0
  91. for(int y = 0; y < image.getHeight(); y++)
  92. {
  93. for(int x = 0; x < image.getWidth(); x++)
  94. {
  95.  
  96. Color a = new Color(image.getRGB(x,y));
  97. int r = a.getRed();
  98. int g = a.getGreen();
  99. int b = a.getBlue();
  100.  
  101. image.setRGB(0,0,0);
  102. }
  103. }
  104. }
  105. public void setZeroOne()
  106. {
  107. //Modify this image by thresholding its pixels to 0 or 1 in each pixel colour
  108. for(int y = 0; y<image.getHeight(); y++)
  109. {
  110. for(int x = 0; x < image.getWidth(); x++)
  111. {
  112. int pixel = image.getRGB(x,y);
  113. int r = (pixel >> 16) & 0xff;
  114. int g = (pixel >> 8) & 0xff;
  115. int b = pixel & 0xff;
  116. r = 0;
  117. g = 0;
  118. b = 1;
  119.  
  120. pixel = (r<<16) | (g<<8) | b;
  121. image.setRGB(x,y,pixel);
  122. }
  123. }
  124. }
  125.  
  126. public void setBlackWhite()
  127. {
  128. //Modify this image by upscaling each 0,1 pixel to a greyscale for viewing
  129. for(int y = 0; y < image.getHeight(); y++)
  130. {
  131. for(int x = 0; x < image.getWidth(); x++)
  132. {
  133. Color blkWht = new Color(image.getRGB(x,y));
  134. int p = image.getRGB(x,y);
  135. int r = (p>>16) & 0xff;
  136. int g = (p>> 8) & 0xff;
  137. int b = (p) & 0xff;
  138.  
  139. int grey = (b/b)+255;
  140.  
  141. image.setRGB(x,y,grey);
  142. }
  143. }
  144. }
  145.  
  146. public void setToLowBit()
  147. {
  148. //Modify this image by setting each pixel to just its low bit value (0 or 1)
  149. for(int y = 0; y < image.getHeight(); y++)
  150. {
  151. for(int x = 0; x < image.getWidth(); x++)
  152. {
  153. Color w = new Color(image.getRGB(0,0));
  154. int r = w.getRed();
  155. int g = w.getGreen();
  156. int b = w.getBlue();
  157. int lowBit = (r<<16)|(g<<8)|b;
  158.  
  159. image.setRGB(r,g,lowBit);
  160. }
  161. }
  162. }
  163.  
  164. public void mergeImage(StegoImage newimage)
  165. {
  166. //Modify this image to become this + newimage by adding each of their RGB pixels
  167. for(int y = 0; y < image.getHeight(); y++)
  168. {
  169. for(int x = 0; x < image.getWidth(); x++)
  170. {
  171. StegoImage img1 = new StegoImage("");
  172. StegoImage img2 = new StegoImage("");
  173.  
  174. Color one = new Color(img1.image.getRGB(x,y));
  175. Color two = new Color(img2.image.getRGB(x,y));
  176.  
  177. int r = one.getRGB() + two.getRGB();
  178. int g = one.getRGB() + two.getRGB();
  179. int b = one.getRGB() + two.getRGB();
  180.  
  181. newimage.image.setRGB(r,g,b);
  182. }
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement