Read image files into a C program using ffmpeg and a pipe
//
// readimage.c - written by Ted Burke - 5-Aug-2022
//
// Uses ffmpeg to read an image from a jpg file via a pipe, inverts
// all pixels in the image, then writes the modified image to a PPM
// image file.
//
#include <stdio.h> // required for file i/o functions
#include <stdint.h> // required for uint8_t data type
#define W 1280 // image width
#define H 853 // image height
uint8_t p[3*W*H]; // RGB pixel buffer
int main()
{
// invoke ffmpeg to feed raw RGB pixel data in through a pipe
FILE *pipein = popen("ffmpeg -v quiet -i input.jpg -f image2pipe "
"-vcodec rawvideo -pix_fmt rgb24 -", "r"); // open pipe
fread(p, 3, W*H, pipein); // read pixel data
pclose(pipein); // close pipe
// invert pixel data, one byte at a time
for (int n=0 ; n<3*W*H ; ++n) p[n] = 255 - p[n];
// write modified image to a PPM file
FILE *fout = fopen("output.ppm", "w"); // open file
fprintf(fout, "P6\n%d %d\n255\n", W, H); // write image header
fwrite(p, 3, W*H, fout); // write pixel data
fclose(fout); // close file
return 0;
}