hi i try to warp an image use following code but not works. need a help thank you
PImage img; // Declare a variable to hold the image
int widthTop = 102; // Example parameters
int heightTop = 80;
int widthBottom = 20;
int heightBottom = 214;
void setup() {
size(1155, 870); // Set the size of the canvas
img = loadImage("target.png"); // Load the image
}
void draw() {
background(255); // Clear the background
// Draw the original image
image(img, 0, 0);
// Define the corners of the original image
float[] srcX = {0, width, width, 0}; // Top-left, top-right, bottom-right, bottom-left
float[] srcY = {0, 0, height, height};
// Define the corners of the warped shape
float[] dstX = {widthTop, width - widthTop, width - widthBottom, widthBottom}; // Top-left, top-right, bottom-right, bottom-left
float[] dstY = {heightTop, heightTop, height - heightBottom, height - heightBottom};
// Apply the warp and get the result image
PImage warpedImage = warpImage(img, srcX, srcY, dstX, dstY);
// Display the warped image
image(warpedImage, width, 0);
}
// Function to warp the image
PImage warpImage(PImage img, float[] srcX, float[] srcY, float[] dstX, float[] dstY) {
PGraphics pg = createGraphics(img.width, img.height);
pg.beginDraw();
pg.texture(img);
pg.beginShape();
for (int i = 0; i < srcX.length; i++) {
pg.vertex(dstX[i], dstY[i], srcX[i], srcY[i]); // Map texture coordinates to vertices
}
pg.endShape(CLOSE);
pg.endDraw();
// Get the result image from PGraphics
PImage resultImage = pg.get();
return resultImage;
}