I want to add two images using c programming language. If the dimension of the images is 600*600, then from the 1st image, 1 to 300 pixels will be there in the new image and 300 to 600 pixel of the second image will be there in the new image.
Original images
Adding two images together looks like
My code of adding image
#include <stdio.h>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int main() {
int width, height, channels;
//read image using stbi_load function
unsigned char *apple = stbi_load("apple.jpg", &width, &height, &channels, 0);
unsigned char *orange = stbi_load("orange.jpg", &width, &height, &channels, 0);
size_t img_size = width * height * channels;
size_t new_img_size = width * height * channels;
unsigned char *new_img = malloc(new_img_size);
for(unsigned char *p = apple, *q = orange, *pg = new_img; p != apple + img_size/2 && q!=(orange+300)+img_size ; p += channels , q += channels, pg+=channels) {
*pg = (*p + *q);
*(pg + 1) = (*(p+1) + *(q+1));
*(pg + 2) = (*(p+2)+*(q+2));
}
stbi_write_jpg("new_image.jpg", width, height, channels, new_img, 100);
}
The output of the code
In this for loop for(unsigned char *p = apple, *q = orange, *pg = new_img; p != apple + img_size/2 && q!=(orange+300)+img_size ; p += channels , q += channels, pg+=channels)
I tried to implement *p loop over 1st pixel of the image apple through 300th pixel and *q loop over 300th pixel of image orange through the last pixel.
I like to know what logic error is there in my code block. Thank you. Any help would be appreciated.