SubPixel.java
package ac.essex.ooechs.imaging.commons.subpixel;
import ac.essex.ooechs.imaging.commons.PixelLoader;
import ac.essex.ooechs.imaging.commons.util.panels.ImagePanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
/**
* Allows you to extract one image from another.
* Based on Code from Adrian Clark's Image Processing Library: adlib
*
* @author Olly Oechsle, University of Essex, Date: 03-Mar-2008
* @version 1.0
*/
public class SubPixel {
public static void main(String[] args) throws Exception {
final PixelLoader from = new PixelLoader("/home/ooechs/Desktop/Lenna.png");
// display the images
JFrame f = new JFrame("ADLIB Extract");
f.getContentPane().setLayout(new GridLayout(1, 2));
final ImagePanel p1 = new ImagePanel(from);
final ImagePanel p2 = new ImagePanel();
p1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
final int width = 100;
final int height = 100;
p2.setImage(extractRectangle(from, e.getX(), e.getY(), width, height, Math.toRadians(30)));
}
});
p1.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
final int width = 100;
final int height = 10;
p2.setImage(extractRectangle(from, e.getX(), e.getY(), width, height, Math.toRadians(30)));
}
});
f.add(p1);
f.add(p2);
f.setSize(from.getWidth() * 2 + 100, from.getHeight() + 150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void extract(PixelLoader from, PixelLoader to, double xc, double yc, double step, double angle) {
double cosfac = Math.cos(-angle) * step;
double sinfac = Math.sin(-angle) * step;
int nx = from.getWidth();
int ny = from.getHeight();
double atx = xc;
double aty = yc;
xc = nx / 2;
yc = ny / 2;
atx = atx - cosfac * xc - sinfac * yc;
aty = aty + sinfac * xc - cosfac * yc;
double xs = atx;
double ys = aty;
for (int ty = 0; ty < to.getHeight(); ty++) {
double rx = xs;
double ry = ys;
for (int tx = 0; tx < to.getWidth(); tx++) {
if (rx < 0) rx += nx;
rx = rx % nx;
int fx = (int) rx;
int fx1 = fx + 1;
if (fx1 >= nx) fx1 = 0;
double xa = rx - fx;
double xa1 = 1.0 - xa;
if (ry < 0) ry += ny;
ry = ry % ny;
int fy = (int) ry;
int fy1 = fy + 1;
if (fy1 >= ny) fy1 = 0;
double ya = ry - fy;
double ya1 = 1.0 - ya;
int value = (int) (from.getGreyValue(fx, fy) * xa1 * ya1 + from.getGreyValue(fx, fy1) * ya * xa1 + from.getGreyValue(fx1, fy) * ya1 * xa + from.getGreyValue(fx1, fy1) * ya * xa);
to.setRGB(tx, ty, new Color(value, value, value).getRGB());
rx += cosfac;
ry -= sinfac;
}
xs += sinfac;
ys += cosfac;
}
}
public static PixelLoader extractRectangle(PixelLoader image, double centerX, double centerY, int width, int height, double angle) {
double zoom = 1;
PixelLoader pl = new PixelLoader((int) (width * zoom), (int) (height * zoom));
double xstep = 1 * Math.cos(angle);
double ystep = 1 * Math.sin(angle);
double atx = 0;
double aty = 0;
int halfHeight = pl.getHeight() / 2;
int halfWidth = pl.getWidth() / 2;
for (int y = -halfHeight; y < halfHeight; y++) {
atx = (-ystep * y) - (halfWidth * xstep);
aty = (xstep * y) - (halfWidth * ystep);
for (int x = -halfWidth; x < halfWidth; x++) {
int value = extractPixel(image, centerX + atx, centerY + aty);
pl.setRGB(x + halfWidth, y + halfHeight, new Color(value, value, value).getRGB());
atx += xstep;
aty += ystep;
}
}
return pl;
}
public static int extractPixel(PixelLoader image, double xc, double yc) {
int nx = image.getWidth();
int ny = image.getHeight();
double rx = xc;
double ry = yc;
if (rx < 0) rx += nx;
rx = rx % nx;
int fx = (int) rx;
int fx1 = fx + 1;
if (fx1 >= nx) fx1 = 0;
double xa = rx - fx;
double xa1 = 1.0 - xa;
if (ry < 0) ry += ny;
ry = ry % ny;
int fy = (int) ry;
int fy1 = fy + 1;
if (fy1 >= ny) fy1 = 0;
double ya = ry - fy;
double ya1 = 1.0 - ya;
return (int) (image.getGreyValue(fx, fy) * xa1 * ya1 + image.getGreyValue(fx, fy1) * ya * xa1 + image.getGreyValue(fx1, fy) * ya1 * xa + image.getGreyValue(fx1, fy1) * ya * xa);
}
}