Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab4;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- public class Rast {
- public static void main(String[] args) throws IOException {
- BufferedImage img = ImageIO.read(new File("in.png"));
- int height = img.getHeight();
- int width = img.getWidth();
- int k = 0 ;
- int z = 0 ;
- byte[][] alpha = new byte[width][height];
- byte[][] red = new byte[width][height];
- byte[][] green = new byte[width][height];
- byte[][] blue = new byte[width][height];
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = img.getRGB(x, y);
- alpha[x][y] = (byte) (color >> 24);
- red[x][y] = (byte) (color >> 16);
- green[x][y] = (byte) (color >> 8);
- blue[x][y] = (byte) (color);
- }
- }
- // --------------------------------------------
- // обробка першого зображення ( прямокутники )
- for (int j = 0; j < width; j++) {
- for (int i = 0; i < height; i++) {
- if (j>0 && j< 50 && i>0 && i<100 ){
- red[j][i] = (byte) 100;
- green[j][i] = (byte) 0;
- blue[j][i] = (byte) 0;
- }
- else if( j > 65 && j < 115 && i>0 && i<100 )
- {
- red[j][i] = (byte) 0;
- green[j][i] = (byte) 100;
- blue[j][i] = (byte) 0;
- }
- else if( j > 130 && j < 180 && i>0 && i<100 )
- {
- red[j][i] = (byte) 0;
- green[j][i] = (byte) 0;
- blue[j][i] = (byte) 100;
- }
- else {
- red[j][i] = (byte) 255;
- green[j][i] = (byte) 255;
- blue[j][i] = (byte) 255;
- }
- }
- }
- BufferedImage outImg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = blue[x][y];
- color += (green[x][y] << 8);
- color += (red[x][y] << 16);
- color += (alpha[x][y] << 24);
- outImg.setRGB(x, y, color);
- }
- }
- ImageIO.write(outImg, "png", new File("out img1.png"));
- //--------------------------------------------
- // створено перше зображення
- BufferedImage img2 = ImageIO.read(new File("in2.png"));
- height = img2.getHeight();
- width = img2.getWidth();
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = img2.getRGB(x, y);
- alpha[x][y] = (byte) (color >> 24);
- red[x][y] = (byte) (color >> 16);
- green[x][y] = (byte) (color >> 8);
- blue[x][y] = (byte) (color);
- }
- }
- // --------------------------------------------
- for (int j = 0; j < width; j++) {
- for (int i = 0; i < height; i++) {
- // red[j][i] = 0;
- }
- }
- BufferedImage outImg2 = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = 0;//blue[x][y];
- color += (green[x][y] << 8);
- color += (red[x][y] << 16);
- color += (alpha[x][y] << 24);
- outImg2.setRGB(x, y, color);
- }
- }
- ImageIO.write(outImg2, "png", new File("out img 2.png"));
- // градації сірого
- byte[][] Y = new byte[width][height];
- BufferedImage img3 = ImageIO.read(new File("in2.png"));
- height = img3.getHeight();
- width = img3.getWidth();
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = img3.getRGB(x, y);
- alpha[x][y] = (byte) (color >> 24);
- red[x][y] = (byte) (color >> 16);
- green[x][y] = (byte) (color >> 8);
- blue[x][y] = (byte) (color);
- }
- }
- // --------------------------------------------
- for (int j = 0; j < width; j++) {
- for (int i = 0; i < height; i++) {
- Y[j][i]=(byte) (0.3 * red[j][i] + 0.6 * green[j][i] +0.1 * blue[j][i]);
- red[j][i] = Y[j][i];
- blue[j][i] = Y[j][i];
- green[j][i] = Y[j][i];
- }
- }
- BufferedImage outImg3 = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = blue[x][y];
- color += (green[x][y] << 8);
- color += (red[x][y] << 16);
- color += (alpha[x][y] << 24);
- outImg3.setRGB(x, y, color);
- }
- }
- ImageIO.write(outImg3, "png", new File("out img 3.png"));
- // транспонування
- BufferedImage img4 = ImageIO.read(new File("in2.png"));
- height = img4.getHeight();
- width = img4.getWidth();
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = img4.getRGB(x, y);
- alpha[x][y] = (byte) (color >> 24);
- red[x][y] = (byte) (color >> 16);
- green[x][y] = (byte) (color >> 8);
- blue[x][y] = (byte) (color);
- }
- }
- // --------------------------------------------
- byte [][] tempr = new byte[256][256];
- byte [][] tempg = new byte[256][256];
- byte [][] tempb = new byte[256][256];
- for (int j = 0; j < width; j++) {
- for (int i = 0; i < height ; i++) {
- tempr[j][i] = red[j][i];
- tempb[j][i] = blue[j][i];
- tempg[j][i] = green[j][i];
- }
- }
- for (int j = 0; j < width / 2; j++) {
- for (int i = 0; i < height ; i++) {
- red[j][i] = (byte)(red[width - j - 1][i]) ;
- green[j][i] = blue[width - 1- j][i] ;
- blue[j][i] = green[width - 1 -j][i] ;
- blue[width - 1- j][i ] = tempb[j][i];
- green[width - 1 - j][i ] = tempg[j][i];
- red[width - j - 1][i] = tempr[j][i];
- }
- }
- BufferedImage outImg4 = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = blue[x][y];
- color += (green[x][y] << 8);
- color += (red[x][y] << 16);
- color += (alpha[x][y] << 24);
- outImg4.setRGB(x, y, color);
- }
- }
- ImageIO.write(outImg4, "png", new File("out img 4.png"));
- // 5 штука, яка накладає 2 зображення
- BufferedImage img5 = ImageIO.read(new File("in.png"));
- BufferedImage img6 = ImageIO.read(new File("in3.png"));
- height = img.getHeight();
- width = img.getWidth();
- byte[][] alpha1 = new byte[width][height];
- byte[][] red1 = new byte[width][height];
- byte[][] green1 = new byte[width][height];
- byte[][] blue1 = new byte[width][height];
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = img5.getRGB(x, y);
- alpha[x][y] = (byte) (color >> 24);
- red[x][y] = (byte) (color >> 16);
- green[x][y] = (byte) (color >> 8);
- blue[x][y] = (byte) (color);
- int color2 = img6.getRGB(x, y);
- alpha1[x][y] = (byte) (color2 >> 24);
- red1[x][y] = (byte) (color2 >> 16);
- green1[x][y] = (byte) (color2 >> 8);
- blue1[x][y] = (byte) (color2);
- }
- }
- byte[][] tempa = new byte[width][height];
- // --------------------------------------------
- for (int j = 0; j < width; j++) {
- for (int i = 0; i < height; i++) {
- tempr[j][i] = (byte) ((red[j][i] * red1[j][i]) / 200);
- tempb[j][i] = (byte) ((blue[j][i] * blue1[j][i]) / 200);
- tempg[j][i] = (byte) ((green[j][i] * green1[j][i]) / 200);
- tempa[j][i] = (byte) ((alpha[j][i] * alpha1[j][i]) / 200);
- }
- }
- BufferedImage outImg5 = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- int color = tempb[x][y];
- color += (tempg[x][y] << 8);
- color += (tempr[x][y] << 16);
- color += (tempa[x][y] << 24);
- outImg5.setRGB(x, y, color);
- }
- }
- ImageIO.write(outImg5, "png", new File("out img5.png"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement