1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
Copyright 2006 Jerry Huxtable
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.jhlabs.image;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
/**
* A filter for warping images using the gridwarp algorithm.
* You need to supply two warp grids, one for the source image and
* one for the destination image. The image will be warped so that
* a point in the source grid moves to its counterpart in the destination
* grid.
*/
public class WarpFilter extends WholeImageFilter {
static final long serialVersionUID = 1299148944426051330L;
private WarpGrid sourceGrid;
private WarpGrid destGrid;
private int frames = 1;
private BufferedImage morphImage;
private float time;
/**
* Create a WarpFilter.
*/
public WarpFilter() {
}
/**
* Create a WarpFilter with two warp grids.
* @param sourceGrid the source grid
* @param destGrid the destination grid
*/
public WarpFilter(WarpGrid sourceGrid, WarpGrid destGrid) {
this.sourceGrid = sourceGrid;
this.destGrid = destGrid;
}
/**
* Set the source warp grid.
* @param sourceGrid the source grid
*/
public void setSourceGrid(WarpGrid sourceGrid) {
this.sourceGrid = sourceGrid;
}
/**
* Get the source warp grid.
* @return the source grid
*/
public WarpGrid getSourceGrid() {
return sourceGrid;
}
/**
* Set the destination warp grid.
* @param destGrid the destination grid
*/
public void setDestGrid(WarpGrid destGrid) {
this.destGrid = destGrid;
}
/**
* Get the destination warp grid.
* @return the destination grid
*/
public WarpGrid getDestGrid() {
return destGrid;
}
public void setFrames(int frames) {
this.frames = frames;
}
public int getFrames() {
return frames;
}
/**
* For morphing, sets the image we're morphing to. If not, set then we're just warping.
*/
public void setMorphImage(BufferedImage morphImage) {
this.morphImage = morphImage;
}
public BufferedImage getMorphImage() {
return morphImage;
}
public void setTime(float time) {
this.time = time;
}
public float getTime() {
return time;
}
protected void transformSpace(Rectangle r) {
r.width *= frames;
}
protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
int[] outPixels = new int[width * height];
if ( morphImage != null ) {
int[] morphPixels = getRGB( morphImage, 0, 0, width, height, null );
morph( inPixels, morphPixels, outPixels, sourceGrid, destGrid, width, height, time );
} else if (frames <= 1) {
sourceGrid.warp(inPixels, width, height, sourceGrid, destGrid, outPixels);
} else {
WarpGrid newGrid = new WarpGrid(sourceGrid.rows, sourceGrid.cols, width, height);
for (int i = 0; i < frames; i++) {
float t = (float)i/(frames-1);
sourceGrid.lerp(t, destGrid, newGrid);
sourceGrid.warp(inPixels, width, height, sourceGrid, newGrid, outPixels);
}
}
return outPixels;
}
public void morph(int[] srcPixels, int[] destPixels, int[] outPixels, WarpGrid srcGrid, WarpGrid destGrid, int width, int height, float t) {
WarpGrid newGrid = new WarpGrid(srcGrid.rows, srcGrid.cols, width, height);
srcGrid.lerp(t, destGrid, newGrid);
srcGrid.warp(srcPixels, width, height, srcGrid, newGrid, outPixels);
int[] destPixels2 = new int[width * height];
destGrid.warp(destPixels, width, height, destGrid, newGrid, destPixels2);
crossDissolve(outPixels, destPixels2, width, height, t);
}
public void crossDissolve(int[] pixels1, int[] pixels2, int width, int height, float t) {
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels1[index] = ImageMath.mixColors(t, pixels1[index], pixels2[index]);
index++;
}
}
}
public String toString() {
return "Distort/Mesh Warp...";
}
}
|