0

I'm trying to make a C program that composites two images together, pixel by pixel. To store the image, I defined this struct:

struct pixel {
    int r;//red
    int g;//green
    int b;//blue
    int a;//alpha
};
typedef struct pixel pixel;

and then created an array of pointers to store each pixel:

int pixelcount=1000*1000; //Resolution of Image

pixel** image1pointer=malloc(sizeof(pixel*)*pixelcount);
pixel** image2pointer=malloc(sizeof(pixel*)*pixelcount);
pixel** resultimagepointer=malloc(sizeof(pixel*)*pixelcount); 

then used a for loop to initialise and assign (random) data to each pixel for both images, and blend them for the result image. This seems to work fine, but how would I import an actual JPEG (or other image format) into pixel values that this program can use?

I found a library called libjpeg that looks like it might be able to do the trick, but I couldn't find how to use it to accomplish this, and how it would return the pixel values.

Edit: In addition to converting an image file into raw data, what is the format of the data that I can use to place it in the struct?

TheInnerParty
  • 396
  • 2
  • 13
  • @JimGarrison I'm also concerned with taking the output of libjpeg and placing it into a struct. That answer also basically just says to use libjpeg without specifying how that worked. – TheInnerParty May 14 '15 at 03:35
  • 1
    The answer from Peter Parker is pretty explicit. Assign your struct from the decoded rgba values shown in the answer. – Retired Ninja May 14 '15 at 04:16
  • 1
    @TheInnerParty `libjpeg` is a collection of code tailored to manipulating jpeg images. You are free to write your own (by why?). As for your code above, you allocate a million `pointers-to-pointers` which will do you little good. You need 2 allocations per imageXpointer. `pixel **image1pointer = malloc( sizeof *image1pointer * 1000);` to allocate pointers, and then `image1pointer[i] = malloc( sizeof **image1pointer * 1000);` to allocate the structs. This will allow access by `image1pointer[i][j].red`, etc.. – David C. Rankin May 14 '15 at 04:23

0 Answers0