frames2img.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <png.h>
  4. #include <omp.h>
  5. typedef uint8_t uint8;
  6. typedef uint32_t uint32;
  7. const int bytesPerPixel = 3; /// red, green, blue
  8. void usage();
  9. uint32 averagergb(png_image *, uint8 *);
  10. int main(int argc, char **argv){
  11. if(argc == 1) usage();
  12. char *imageFileName = "output.png";
  13. int height = 720;
  14. int width = argc-1;
  15. int percent = 0;
  16. uint32 frames[width];
  17. #pragma omp parallel for
  18. for(int i =1;i<argc;i++) {
  19. png_image image = {.version = PNG_IMAGE_VERSION, .opaque = NULL, };
  20. if(!png_image_begin_read_from_file(&image, argv[i])) usage();
  21. image.format = PNG_FORMAT_RGB;
  22. uint8 *buffer = malloc(PNG_IMAGE_SIZE(image));
  23. png_image_finish_read(&image, NULL, buffer, 0, NULL);
  24. frames[i-1] = averagergb(&image, buffer);
  25. free(buffer);
  26. png_image_free(&image);
  27. fprintf(stdout, "%d%% Complete\r", ++percent * 100 / argc);
  28. fflush(stdout);
  29. }
  30. uint8 image[height][width][bytesPerPixel];
  31. for(int i=0; i<height; i++){
  32. for(int j=0; j<width; j++){
  33. image[i][j][2] = ((frames[j] >> 16)&0xff);
  34. image[i][j][1] = ((frames[j] >> 8)&0xff);
  35. image[i][j][0] = (frames[j] & 0xff);
  36. }
  37. }
  38. png_image output = {0};
  39. output.format = PNG_FORMAT_RGB;
  40. output.version = PNG_IMAGE_VERSION;
  41. output.width = width;
  42. output.height = height;
  43. png_image_write_to_file(&output, imageFileName, 0, image, 0, NULL);
  44. }
  45. void
  46. usage() {
  47. fprintf(stderr, "USAGE: ./frames2img PNGs...");
  48. exit(1);
  49. }
  50. uint32
  51. averagergb(png_image *img, uint8 *buffer) {
  52. png_image image = *img;
  53. double sumR = 0;
  54. double sumG = 0;
  55. double sumB = 0;
  56. double fact = image.width * image.height;
  57. for(int y =0;y<image.height;y++) {
  58. for(int x=0;x<image.width;x++) {
  59. sumR += buffer[3*(x+y*image.width)+2] / fact;
  60. sumG += buffer[3*(x+y*image.width)+1] / fact;
  61. sumB += buffer[3*(x+y*image.width)+0] / fact;
  62. }
  63. }
  64. return (((uint8) sumR & 0xff) << 16) |
  65. (((uint8)sumG & 0xff) << 8) |
  66. ((uint8) sumB & 0xff);
  67. }