frames2img.c 1.9 KB

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