Lab9-Blend-Chromakey


5530356221  นายปุณณวิช พันทอง (COM21914) (9/21/2012 (11:08:40 AM))

MiniQuiz + TestScript
JLab>javac Blend.java ChromaKey.java Mirror.java
JLab>
JLab>java Selftest 

JLab> testBlend     : ok ok ok ok ok  (5.0/5.0)
JLab> testChromaKey : ok ok ok ok ok  (5.0/5.0)
JLab> : -----------------------------
JLab> : You got 10.0 points (out of 10.0)
JLab> : -----------------------------
JLab> :<POINT>10.0</POINT> (<TOTAL>10.0</TOTAL> )
JLab>

ได้ 10 คะแนน
Source Code

ChromaKey.java


import jlab.graphics.DWindow; public class ChromaKey { public static void main(String[] args) { DWindow fg = new DWindow(); fg.loadImage("bird.gif"); int w = fg.getWidth(); int h = fg.getHeight(); DWindow bg = new DWindow(); bg.loadImage("sky.jpg",w, h); DWindow out = new DWindow(w, h); fg.setLocation(10, 10); bg.setLocation(10 + fg.getWidth(), 10); out.setLocation(10 + 2*fg.getWidth(), 10); out.setPixmap(chromaKey(fg.getPixmap(), bg.getPixmap(), DWindow.GREEN)); } //----------------------------------------------------------------- public static int[][] chromaKey(int[][] fg, int[][] bg, int c) { int w = fg.length; int h = fg[0].length; int[][] b = new int[w][h]; for (int i = 0; i < fg.length; i++) { for (int j = 0; j < fg[i].length; j++) { if (fg[i][j] == c) fg[i][j] = bg[i][j]; } } for (int i = 0; i < fg.length; i++) { for (int j = 0; j < fg[i].length; j++) { b[i][j] = fg[i][j]; } } return b; } //----------------------------------------------------------------- }

Blend.java


import jlab.graphics.DWindow; public class Blend { public static void main(String[] args) { int w = 300, h = 300; DWindow pic1 = new DWindow(w, h); DWindow pic2 = new DWindow(w, h); DWindow out = new DWindow(w, h); pic1.loadImage("cat.jpg", w, h); pic2.loadImage("baby.jpg", w, h); pic1.setLocation(10, 10); pic2.setLocation(10 + w, 10); out.setLocation(10 + 2 * w, 10); while (true) { for (double a = 0; a <= 1; a += 0.1) { int[][] b = blend(pic1.getPixmap(), pic2.getPixmap(), a); out.setPixmap(b); } for (double a = 1; a >= 0; a -= 0.1) { int[][] b = blend(pic1.getPixmap(), pic2.getPixmap(), a); out.setPixmap(b); } } } //----------------------------------------------------------------- public static int[][] blend(int[][] b1, int[][] b2, double a) { int w = b1.length; int h = b1[0].length; int[][] b3 = new int[w][h]; for (int i = 0; i < b1.length; i++) { for (int j = 0; j < b1[i].length; j++) { int r4 = DWindow.getR(b1[i][j]); int b4 = DWindow.getB(b1[i][j]); int g4 = DWindow.getG(b1[i][j]); int r5 = DWindow.getR(b2[i][j]); int b5 = DWindow.getB(b2[i][j]); int g5 = DWindow.getG(b2[i][j]); int r = (int)(a * r4 + (1 - a) * r5); int b = (int)(a * b4 + (1 - a) * b5); int g = (int)(a * g4 + (1 - a) * g5); b3[i][j] = DWindow.mixRGB(r, g, b); } } return b3; } //----------------------------------------------------------------- }
SM=2, CM=7, ST=23, KY=391, TR=60:00
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;