Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author...
- * @version October 2019
- */
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.awt.Color;
- import java.lang.IllegalArgumentException;
- public class StegoImage
- {
- BufferedImage image;
- int width;
- int height;
- public StegoImage(String filename)
- {
- //create a StegoImage by reading an image from a file.
- try
- {
- File file = new File(filename);
- image = ImageIO.read(file);
- width = image.getWidth();
- height = image.getHeight();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- catch (NullPointerException e)
- {
- System.out.println("NullPointerException Caught");
- }
- if(image.getHeight() <= 0 && image.getWidth() <= 0)
- {
- throw new IllegalArgumentException("Please input an image with valid dimensions");
- }
- }
- public BufferedImage getImage()
- {
- image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- return image;
- }
- public int getHeight()
- {
- height = StegoViewer.IMG_HEIGHT;
- return height;
- }
- public int getWidth()
- {
- width = StegoViewer.IMG_WIDTH;
- return width;
- }
- public void scaleImage(int div, int mult)
- {
- //Modify this image by changing every pixel
- for(int y = 0; y < image.getHeight(); y++)
- {
- for(int x = 0; x < image.getWidth(); x++)
- {
- if(div > 0 && mult > 0)
- {
- Color s = new Color(image.getRGB(x,y));
- int r = s.getRed();
- int g = s.getGreen();
- int b = s.getBlue();
- r = (r/div)*mult;
- g = (g/div)*mult;
- b = (b/div)*mult;
- image.setRGB(r,g,b);
- }
- else
- {
- break;
- }
- }
- }
- }
- public void clearLowBit()
- {
- //Modify this image by setting the lowest bit of each pixel colour to 0
- for(int y = 0; y < image.getHeight(); y++)
- {
- for(int x = 0; x < image.getWidth(); x++)
- {
- Color a = new Color(image.getRGB(x,y));
- int r = a.getRed();
- int g = a.getGreen();
- int b = a.getBlue();
- image.setRGB(0,0,0);
- }
- }
- }
- public void setZeroOne()
- {
- //Modify this image by thresholding its pixels to 0 or 1 in each pixel colour
- for(int y = 0; y<image.getHeight(); y++)
- {
- for(int x = 0; x < image.getWidth(); x++)
- {
- int pixel = image.getRGB(x,y);
- int r = (pixel >> 16) & 0xff;
- int g = (pixel >> 8) & 0xff;
- int b = pixel & 0xff;
- r = 0;
- g = 0;
- b = 1;
- pixel = (r<<16) | (g<<8) | b;
- image.setRGB(x,y,pixel);
- }
- }
- }
- public void setBlackWhite()
- {
- //Modify this image by upscaling each 0,1 pixel to a greyscale for viewing
- for(int y = 0; y < image.getHeight(); y++)
- {
- for(int x = 0; x < image.getWidth(); x++)
- {
- Color blkWht = new Color(image.getRGB(x,y));
- int p = image.getRGB(x,y);
- int r = (p>>16) & 0xff;
- int g = (p>> 8) & 0xff;
- int b = (p) & 0xff;
- int grey = (b/b)+255;
- image.setRGB(x,y,grey);
- }
- }
- }
- public void setToLowBit()
- {
- //Modify this image by setting each pixel to just its low bit value (0 or 1)
- for(int y = 0; y < image.getHeight(); y++)
- {
- for(int x = 0; x < image.getWidth(); x++)
- {
- Color w = new Color(image.getRGB(0,0));
- int r = w.getRed();
- int g = w.getGreen();
- int b = w.getBlue();
- int lowBit = (r<<16)|(g<<8)|b;
- image.setRGB(r,g,lowBit);
- }
- }
- }
- public void mergeImage(StegoImage newimage)
- {
- //Modify this image to become this + newimage by adding each of their RGB pixels
- for(int y = 0; y < image.getHeight(); y++)
- {
- for(int x = 0; x < image.getWidth(); x++)
- {
- StegoImage img1 = new StegoImage("");
- StegoImage img2 = new StegoImage("");
- Color one = new Color(img1.image.getRGB(x,y));
- Color two = new Color(img2.image.getRGB(x,y));
- int r = one.getRGB() + two.getRGB();
- int g = one.getRGB() + two.getRGB();
- int b = one.getRGB() + two.getRGB();
- newimage.image.setRGB(r,g,b);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement