frames2img.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. char *imageFileName = "output.png";
  13. int height = 480;
  14. int width = argc-1;
  15. int percent = 0;
  16. uint32 *frames = malloc(width*sizeof(uint32));
  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 *imgPointer = malloc(height*width*3*sizeof(uint8 *));
  31. for(int i=0; i<height; i++){
  32. for(int j=0; j<width; j++){
  33. int index = i+j*height;
  34. imgPointer[j*3+i*width*3+2] = ((frames[j] >> 16)&0xff);
  35. imgPointer[j*3+i*width*3+1] = ((frames[j] >> 8)&0xff);
  36. imgPointer[j*3+i*width*3+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, imgPointer, 0, NULL);
  45. free(imgPointer);
  46. free(frames);
  47. }
  48. void
  49. usage() {
  50. fprintf(stderr, "USAGE: ./frames2img PNGs...");
  51. exit(1);
  52. }
  53. uint32
  54. averagergb(png_image *img, uint8 *buffer) {
  55. png_image image = *img;
  56. double sumR = 0;
  57. double sumG = 0;
  58. double sumB = 0;
  59. double fact = image.width * image.height;
  60. for(int y =0;y<image.height;y++) {
  61. for(int x=0;x<image.width;x++) {
  62. sumR += buffer[3*(x+y*image.width)+2] / fact;
  63. sumG += buffer[3*(x+y*image.width)+1] / fact;
  64. sumB += buffer[3*(x+y*image.width)+0] / fact;
  65. }
  66. }
  67. return (((uint8) sumR & 0xff) << 16) |
  68. (((uint8)sumG & 0xff) << 8) |
  69. ((uint8) sumB & 0xff);
  70. }