frames2img.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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;
  8. void usage();
  9. uint32 averagergb(png_image *, uint8 *);
  10. int main(int argc, char **argv){
  11. if(argc == 1) usage();
  12. int height = 1080;
  13. int width = argc-1;
  14. int percent = 0;
  15. uint32 *frames = malloc(width*sizeof(uint32));
  16. #pragma omp parallel for
  17. for(int i =1;i<argc;i++) {
  18. png_image image = {.version = PNG_IMAGE_VERSION, .opaque = NULL, };
  19. if(!png_image_begin_read_from_file(&image, argv[i])) usage();
  20. image.format = PNG_FORMAT_RGB;
  21. uint8 *buffer = malloc(PNG_IMAGE_SIZE(image));
  22. png_image_finish_read(&image, NULL, buffer, 0, NULL);
  23. frames[i-1] = averagergb(&image, buffer);
  24. free(buffer);
  25. png_image_free(&image);
  26. fprintf(stderr, "%d%% Complete\r", ++percent * 100 / argc);
  27. fflush(stderr);
  28. }
  29. uint8 *imgPointer = malloc(height*width*3*sizeof(uint8 *));
  30. for(int i=0; i<height; i++){
  31. for(int j=0; j<width; j++){
  32. int index = i+j*height;
  33. imgPointer[j*3+i*width*3+2] = ((frames[j] >> 16)&0xff);
  34. imgPointer[j*3+i*width*3+1] = ((frames[j] >> 8)&0xff);
  35. imgPointer[j*3+i*width*3+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_stdio(&output, stdout, 0, imgPointer, 0, NULL);
  44. free(imgPointer);
  45. free(frames);
  46. }
  47. void
  48. usage() {
  49. fprintf(stderr, "USAGE: ./frames2img PNGs...");
  50. exit(1);
  51. }
  52. uint32
  53. averagergb(png_image *img, uint8 *buffer) {
  54. png_image image = *img;
  55. double sumR = 0;
  56. double sumG = 0;
  57. double sumB = 0;
  58. double fact = image.width * image.height;
  59. for(int y =0;y<image.height;y++) {
  60. for(int x=0;x<image.width;x++) {
  61. sumR += buffer[3*(x+y*image.width)+2] / fact;
  62. sumG += buffer[3*(x+y*image.width)+1] / fact;
  63. sumB += buffer[3*(x+y*image.width)+0] / fact;
  64. }
  65. }
  66. return (((uint8) sumR & 0xff) << 16) |
  67. (((uint8)sumG & 0xff) << 8) |
  68. ((uint8) sumB & 0xff);
  69. }