The seg fault error message suggests that you're accessing memory that your program doesn't have permission to access. In this case, it's possible that the pixel_offset pointer is pointing to a memory location that is out of bounds.
To fix the issue, you should check if j + width * i is within the bounds of the data array. If it's not, you should either stop processing the current iteration of the loop or return an error message.
Here's an example:
C:
if (j + width * i >= width * height * byte_per_pixel) {
printf("Error: Out of bounds\n");
return;
}
unsigned char* pixel_offset = data + (j + width * i) * byte_per_pixel;
unsigned char y = pixel_offset[0];
This checks if the calculated offset is larger than the total size of the data array, and if so, it prints an error message and returns.