|
-
help regarding image processing
hi
I'm doing image processing in java.In tht i want to do the glass effect.I got one calculations frm the net.It's working fine with the image of size 128X128 but i want to work for 120X160.i tried with tht calculation but it's pretty difficult for me to find exactly the flow.Here is tht code
import java.awt.*;
import java.applet.*;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
public class glass extends Applet implements Runnable {
// Only in class array.
static public int[] PCos; // Pre-calculated Degree cos table.
static public int[] PSin; // Pre-calculated Degree sin table.
// private members.
private Thread lineThread; // This thread.
private Image img = null; // Destination image.
private Image Original; // Original image.
private MemoryImageSource source; // Use to convert rawbuffer to img.
private PixelGrabber pg; // Pix grabber to access pixels.
private int width, height; // Our dimensions.
private int[] pixeldata; // Work buffer.
private int[] src_data; // Original pixel buffer.
private int[] glass_buffer; // Buffer for glass distance.
private int[] glass_shape = {0,0,0,1,1,1,2,2,2,3,3,4,4,5,5,6,7,8,9,10,12,14,16,18,20,22,24,26,28,30};
private int xpos,ypos,dx,dy; // Glass position, and direction.
public void init() {
Original = getImage (getDocumentBase(),getParameter ("image")); // Get image named 'image' in html source.
width = getSize().width; // Get our display width.
height= getSize().height; // Get our display height.
pixeldata=new int [width*height]; // Work buffer is [w*h] int.
src_data=new int [width*height]; // Source image buffer is [w*h] int.
PSin = new int [360]; // Create 360 index array for precalc math.
PCos = new int [360]; // Create 360 index array for precalc math.
glass_buffer = new int [3600]; // Create glass buffer.
setBackground (Color.black); // Turn bg to black.
source = new MemoryImageSource(width, height, pixeldata, 0, width); // Make source = gfx from a raw buffer.
source.setAnimated(true); // Tell it's animated :-)
pg = new PixelGrabber(Original, 0, 0, 128, 128, src_data, 0, width); // Get pixbuffer from source image.
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
img = createImage(source); // Create image from source.
for (int i=0;i<360;i++){
PCos [i] = (int)(256*Math.cos(Math.toRadians(i))); // Fill our pre-calc cosine table:8 bit fixed math !
PSin [i] = (int)(256*Math.sin(Math.toRadians(i))); // Fill our pre-calc cosine table:8 bit fixed math !
}
for (int i=0;i<3600;i++)
{
glass_buffer [i]=255; /* Fill buffer with 0xff */
}
for (int i=0;i<30;i++)
{
for (int o=0;o<360;o++)
{
int x1= 30 + ((i * PCos [o])>>8);
int y1= 30 + ((i * PSin [o])>>8);
int x2= 30 + ((glass_shape[i] * PCos [o])>>8);
int y2= 30 + ((glass_shape[i] * PSin [o])>>8);
glass_buffer [(y1*60)+x1]=y2*128+x2;
}
}
xpos = 25;
ypos = 0;
dx = 1;
dy = 2;
}
public void start() {
if(lineThread==null) { // Does lineThread already exists ?
lineThread = new Thread(this); // Create new Thread.
lineThread.setPriority(lineThread.MAX_PRIORITY); // Set highest propriety.
lineThread.start(); // start him !
}
}
public void stop() {
if(lineThread!=null) { // Does lineThread already exists ?
//lineThread.stop(); // Kill him (deprecated method)
lineThread = null; // Dereference him.
}
}
public void paint(Graphics g) {
source.newPixels(0,0,width,height); // Our buffer as changed, tell it :-)
g.drawImage(img,0,0,this); // Redraw destination image.
}
public void update (Graphics g) {
paint (g); // Call paint on Update.
}
public void run() { // Thread main function.
while(true) { // Infinite loop.
ApplyGlass (xpos,ypos);
xpos += dx;
ypos += dy;
if ((xpos>127-60) || (xpos<1)) dx *= -1; // invert x direction.
if ((ypos>127-60) || (ypos<1)) dy *= -1; // invert y direction.
repaint(); // Calls paint.
try {
lineThread.sleep(20); // Free a few cpu cycles :-).
}
catch(InterruptedException e) {}
}
}
private void ApplyGlass (int x, int y)
{
int deph;
int i,o;
int col;
int ptr =0;
int source = ((y<<7)+x);
for (i=0;i<128*128;i++) pixeldata[i]=src_data[i];
for (i=0;i<59;i++)
{
for (o=0;o<60;o++)
{
deph =glass_buffer [ptr++];
if (deph != 255)
{
col = src_data [source+deph];
pixeldata [(x+o)+((y+i)<<7)] = col;
}
}
}
}
}
can anyone tell me how to change the calculation frm 128X128 to 120X160.
I'm struggling with this code for last 2 days please help me
thanx a lot
Similar Threads
-
By James Graham in forum .NET
Replies: 4
Last Post: 10-23-2011, 02:47 PM
-
By rechmbrs in forum Java
Replies: 0
Last Post: 07-11-2005, 09:41 PM
-
Replies: 2
Last Post: 10-02-2001, 07:00 PM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
-
By David Crowell in forum vb.announcements
Replies: 0
Last Post: 09-05-2000, 12:20 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks