From 1c073005b8f8da7b5e25384ae8c2b8f1ccc8bab4 Mon Sep 17 00:00:00 2001 From: Tiago Date: Mon, 20 Nov 2023 16:34:11 +0000 Subject: [PATCH] ImageBlend fixes --- image8bit.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/image8bit.c b/image8bit.c index 883172f..0533fa8 100644 --- a/image8bit.c +++ b/image8bit.c @@ -576,28 +576,23 @@ void ImageBlend(Image img1, int x, int y, Image img2, double alpha) { /// assert (ImageValidRect(img1, x, y, img2->width, img2->height)); // Insert your code here! int img1Width = img1->width; - int img1Height = img1->height; - int img2Width = img2->width; - int img2Height = img2->height; - - for (int i = 0; i < img2Height; ++i) { - for (int j = 0; j < img2Width; ++j) { - int new_x = x + j; - int new_y = y + i; - - if (new_x >= 0 && new_x < img1Width && new_y >= 0 && new_y < img1Height) { - uint8 pixel1 = ImageGetPixel(img1, new_x, new_y); - uint8 pixel2 = ImageGetPixel(img2, j, i); - - - uint8 blended_pixel = (uint8)(alpha * pixel2 + (1 - alpha) * pixel1); - - - ImageSetPixel(img1, new_x, new_y, blended_pixel); - } - } + int img1Height = img1->height; + int img2Width = img2->width; + int img2Height = img2->height; + + for (int i = 0; i < img2Height; ++i) { + for (int j = 0; j < img2Width; ++j) { + int new_x = x + j; + int new_y = y + i; + + if (new_x >= 0 && new_x < img1Width && new_y >= 0 && new_y < img1Height) { + uint8 pixel1 = ImageGetPixel(img1, new_x, new_y); + uint8 pixel2 = ImageGetPixel(img2, j, i); + uint8 blended_pixel = (uint8)(alpha * pixel2 + (1 - alpha) * pixel1); + ImageSetPixel(img1, new_x, new_y, blended_pixel); + } } - + } } /// Compare an image to a subimage of a larger image.