You are on page 1of 10

Value RGB

Main.java package ValueRGB; public class Main { public static void main(String[] args) { image choser = new image(); } } Image.java package ValueRGB; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.swing.*; import javax.imageio.*; public class image extends JFrame implements ActionListener { private final JButton tombol; private final JPanel panel; private final JFileChooser filechooser; private final JLabel label; public image() { setTitle("TUGAS PCD RGB VALUE"); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); setSize(500,400); tombol = new JButton("Click to Open Image"); tombol.setSize(tombol.getPreferredSize()); panel = new JPanel(); filechooser = new JFileChooser(); label = new JLabel(); setLayout(new BorderLayout()); add(tombol, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); panel.add(label); setVisible(true); tombol.addActionListener(this); } public void actionPerformed(ActionEvent e) { image ic = new image(); BufferedImage bf; try{ int a = filechooser.showOpenDialog(panel); if (a == JFileChooser.APPROVE_OPTION) { String imgSource = filechooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(imgSource)); ReadRGB readRGB = new ReadRGB(imgSource); System.out.println(imgSource); bf = ImageIO.read(new File("imgSource")); }

}catch(IOException ie){} } public BufferedImage biner(BufferedImage src){ BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); int gray = 0; for (int y = 0; y < src.getHeight(); y++) { for (int x = 0; x < src.getWidth(); x++) { int rgb = src.getRGB(x, y); int red = rgb & 0x000000FF; int green = (rgb & 0x0000FF00) >> 8; int blue = (rgb & 0x00FF0000) >> 16; int avg = (red + green + blue) / 3; if(avg < 128){ gray = 0; }else if(avg > 128){ gray = 255; } int biner = gray+(gray<<8)+(gray<<16); dest.setRGB(x, y, biner); } } return dest; } }

Rgb.java package ValueRGB; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; class ReadRGB { int baris=2; int kolom=2; int baris2=0; int pixel; int red,green,blue,alpha; public ReadRGB(String s){ BufferedImage bf; try{ bf = ImageIO.read(new File(s)); baris=bf.getWidth(); kolom=bf.getHeight(); int[][]buffer=new int[baris][kolom]; for(int i=0;i<baris;i++){ for(int j=0;j<kolom;j++){ buffer[i][j]=bf.getRGB(i,j); pixel=buffer[i][j]; alpha=(pixel>> 24)& 0x000000FF; red= (pixel>> 16 )& 0x000000FF; green= (pixel>> 8 )& 0x000000FF; blue= (pixel)& 0x000000FF; System.out.println(" ("+i+","+j+") "+red+" "+green+" "+blue+" ");

} } } catch(IOException e) { } } }

PengolahanCitra.java
import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import javax.imageio.*; import javax.swing.*; class panel extends JPanel{ BufferedImage bi; public panel(){ bi=null; setPreferredSize(new Dimension(320,240)); } public panel(BufferedImage bi){ this.bi = bi; int h = bi.getHeight(); int w = bi.getWidth(); setSize(w,h); } public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D) g; if (bi != null){ g2.drawImage(bi, 0, 0, this); }else{ g2.drawRect(0, 0, getWidth(), getHeight()); } } public void setImage(BufferedImage bi){ this.bi = bi; setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight())); invalidate(); repaint(); } } public class PengolahanCitra extends JFrame implements ActionListener{ panel sebelumIMG, sesudahIMG; BufferedImage bi,bi2,bi3; JFileChooser fc; JMenuBar mb; JMenu mn,mno; JMenuItem mi; Container co; public PengolahanCitra() { setTitle("TUGAS PROGRAM CITRA DIGITAL"); fc = new JFileChooser(); mb = new JMenuBar(); setJMenuBar(mb); mn = new JMenu("Browse"); mi = new JMenuItem("Buka Gambar"); mi.addActionListener(this); mn.add(mi); mi = new JMenuItem("Simpan Gambar"); mi.addActionListener(this); mn.add(mi); mn.addSeparator(); mi = new JMenuItem("Keluar"); mi.addActionListener(this); mn.add(mi); mno=new JMenu("Operasi"); mi = new JMenuItem("Negatif"); mi.addActionListener(this); mno.add(mi); mi = new JMenuItem("Gray Scale"); mi.addActionListener(this); mno.add(mi);

mi = new JMenuItem("Operasi Red"); mi.addActionListener(this); mno.add(mi); mi = new JMenuItem("Operasi Green"); mi.addActionListener(this); mno.add(mi); mi = new JMenuItem("Operasi Blue"); mi.addActionListener(this); mno.add(mi); mi = new JMenuItem("Normal RGB"); mi.addActionListener(this); mno.add(mi); mb.add(mn); mb.add(mno); co = getContentPane(); co.setLayout(new FlowLayout()); sebelumIMG = new panel(); sesudahIMG = new panel(); co.add(sebelumIMG); co.add(sesudahIMG); } public void actionPerformed(ActionEvent ae){ String pil = ae.getActionCommand(); if ("Buka Gambar".equals(pil)){ int op = fc.showOpenDialog(this); if (op==JFileChooser.APPROVE_OPTION){ try{ bi = ImageIO.read(fc.getSelectedFile()); sebelumIMG.setImage(bi); pack(); }catch(IOException e){ System.out.println("Error"); } }else{ JOptionPane.showMessageDialog(null,"Gambar Belum diLoad","Peringatan",JOptionPane.WARNING_MESSAGE); } }else if ("Negatif".equals(pil)){ if (bi==null){ JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses", "Peringatan", JOptionPane.WARNING_MESSAGE); }else{ bi3=prosesNegasi(bi); sesudahIMG.setImage(bi3); pack(); } }else if ("Gray Scale".equals(pil)){ if (bi==null){ JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses", "Peringatan", JOptionPane.WARNING_MESSAGE); }else{ bi3=prosesGray(bi); sesudahIMG.setImage(bi3); pack(); } }else if ("Operasi Red".equals(pil)){ if (bi==null){ JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses", "Peringatan", JOptionPane.WARNING_MESSAGE); }else{ bi3=prosesRed(bi); sesudahIMG.setImage(bi3); pack(); } }else if ("Operasi Green".equals(pil)){ if (bi==null){ JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses", "Peringatan", JOptionPane.WARNING_MESSAGE); }else{ bi3=prosesGreen(bi); sesudahIMG.setImage(bi3); pack(); }

}else if ("Operasi Blue".equals(pil)){ if (bi==null){ JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses", "Peringatan", JOptionPane.WARNING_MESSAGE); }else{ bi3=prosesBlue(bi); sesudahIMG.setImage(bi3); pack(); } }else if ("Normal RGB".equals(pil)){ if (bi==null){ JOptionPane.showMessageDialog(null,"Anda Belum memilih gambar untuk diproses", "Peringatan", JOptionPane.WARNING_MESSAGE); }else{ bi3=normalRGB(bi); sesudahIMG.setImage(bi3); pack(); } }else if ("Simpan Gambar".equals(pil)){ int confi = fc.showSaveDialog(this); if (confi==JFileChooser.APPROVE_OPTION){ try { ImageIO.write(bi, "jpg", fc.getSelectedFile()); } catch (IOException ex) { System.out.println("Error"); } }else{ JOptionPane.showMessageDialog(null, "Gambar Belum Disimpan","Info", JOptionPane.INFORMATION_MESSAGE); } }else{ System.exit(0); } } public BufferedImage prosesNegasi(BufferedImage bi){ BufferedImage dest = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); int gray = 0; for (int y = 0; y < bi.getHeight(); y++) { for (int x = 0; x < bi.getWidth(); x++) { int rgb = bi.getRGB(x, y); int red = rgb & 0x000000FF; int green = (rgb & 0x0000FF00) >> 8; int blue = (rgb & 0x00FF0000) >> 16; red=255-red; green=255-green; blue=255-blue; int biner = red+(green<<8)+(blue<<16); dest.setRGB(x, y, biner); } } return dest; } public BufferedImage prosesGray(BufferedImage src){ BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < src.getHeight(); y++) { for (int x = 0; x < src.getWidth(); x++) { int rgb = src.getRGB(x, y); int alpha = (rgb << 24) & 0xFF; int red = rgb & 0x000000FF; int green = (rgb & 0x0000FF00) >> 8; int blue = (rgb & 0x00FF0000) >> 16; int avg = (red + green + blue) / 3; int gray = alpha | avg << 16 | avg << 8 | avg; dest.setRGB(x, y, gray); } } return dest; } public BufferedImage prosesBlue(BufferedImage bl){

BufferedImage dest = new BufferedImage(bl.getWidth(), bl.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < bl.getHeight(); y++) { for (int x = 0; x < bl.getWidth(); x++) { int rgb = bl.getRGB(x, y); int blue = (rgb & 0x000000FF); int avg = blue; dest.setRGB(x, y, avg); } } return dest; } public BufferedImage prosesRed(BufferedImage re){ BufferedImage dest = new BufferedImage(re.getWidth(), re.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < re.getHeight(); y++) { for (int x = 0; x < re.getWidth(); x++) { int rgb = re.getRGB(x, y); int red = (rgb & 0x00FF0000); int avg = red; dest.setRGB(x, y, avg); } } return dest; } public BufferedImage prosesGreen(BufferedImage gr){ BufferedImage dest = new BufferedImage(gr.getWidth(), gr.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < gr.getHeight(); y++) { for (int x = 0; x < gr.getWidth(); x++) { int rgb = gr.getRGB(x, y); int green = (rgb & 0x0000FF00); int avg = green; dest.setRGB(x, y, avg); } } return dest; } public BufferedImage prosesBlue(BufferedImage bl){ BufferedImage dest = new BufferedImage(bl.getWidth(), bl.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < bl.getHeight(); y++) { for (int x = 0; x < bl.getWidth(); x++) { int rgb = bl.getRGB(x, y); int blue = (rgb & 0x000000FF); int avg = blue; dest.setRGB(x, y, avg); } } return dest; } public BufferedImage normalRGB(BufferedImage as){ BufferedImage dest = new BufferedImage(as.getWidth(), as.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < as.getHeight(); y++) { for (int x = 0; x < as.getWidth(); x++) { int rgb = as.getRGB(x, y); int red = (rgb & 0x00FF0000); int green = (rgb & 0x0000FF00); int blue = (rgb & 0x000000FF); int avg = red+green+blue; dest.setRGB(x, y, avg); } } return dest; } public static void main(String[] args){ JFrame frame = new PengolahanCitra(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); frame.pack(); } }

Berikut adalah proses untuk negasi:


public BufferedImage prosesNegasi(BufferedImage bi){ BufferedImage dest = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); int gray = 0; for (int y = 0; y < bi.getHeight(); y++) { for (int x = 0; x < bi.getWidth(); x++) { int rgb = bi.getRGB(x, y); int red = rgb & 0x000000FF; int green = (rgb & 0x0000FF00) >> 8; int blue = (rgb & 0x00FF0000) >> 16; red=255-red; green=255-green; blue=255-blue; int biner = red+(green<<8)+(blue<<16); dest.setRGB(x, y, biner); } } return dest; }

Output negasi:

Berikut adalah proses Greyscale:


public BufferedImage prosesGray(BufferedImage src){ BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < src.getHeight(); y++) { for (int x = 0; x < src.getWidth(); x++) { int rgb = src.getRGB(x, y); int alpha = (rgb << 24) & 0xFF; int red = rgb & 0x000000FF; int green = (rgb & 0x0000FF00) >> 8; int blue = (rgb & 0x00FF0000) >> 16; int avg = (red + green + blue) / 3; int gray = alpha | avg << 16 | avg << 8 | avg; dest.setRGB(x, y, gray); } } return dest; }

Output Grayscale:

Berikut adalah proses Red:


public BufferedImage prosesRed(BufferedImage re){ BufferedImage dest = new BufferedImage(re.getWidth(), re.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < re.getHeight(); y++) { for (int x = 0; x < re.getWidth(); x++) { int rgb = re.getRGB(x, y); int red = (rgb & 0x00FF0000); int avg = red; dest.setRGB(x, y, avg); } } return dest; }

Output Red:

Berikut adalah proses Green:


public BufferedImage prosesGreen(BufferedImage gr){ BufferedImage dest = new BufferedImage(gr.getWidth(), gr.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < gr.getHeight(); y++) { for (int x = 0; x < gr.getWidth(); x++) { int rgb = gr.getRGB(x, y); int green = (rgb & 0x0000FF00); int avg = green; dest.setRGB(x, y, avg); } } return dest; }

Output Green:

Berikut adalah proses Blue:


public BufferedImage prosesBlue(BufferedImage bl){ BufferedImage dest = new BufferedImage(bl.getWidth(), bl.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < bl.getHeight(); y++) { for (int x = 0; x < bl.getWidth(); x++) { int rgb = bl.getRGB(x, y); int blue = (rgb & 0x000000FF); int avg = blue; dest.setRGB(x, y, avg); } } return dest; }

Output Blue:

Berikut adalah proses Blue:


public BufferedImage normalRGB(BufferedImage as){ BufferedImage dest = new BufferedImage(as.getWidth(), as.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < as.getHeight(); y++) { for (int x = 0; x < as.getWidth(); x++) { int rgb = as.getRGB(x, y); int red = (rgb & 0x00FF0000); int green = (rgb & 0x0000FF00); int blue = (rgb & 0x000000FF); int avg = red+green+blue; dest.setRGB(x, y, avg); } } return dest; }

Output Normal RGB: