avimod.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // #ifdef __cplusplus
  2. // extern "C" {
  3. // #endif
  4. //===============================================================================================================================================================================================================
  5. // DEFINE / INCLUDE
  6. //===============================================================================================================================================================================================================
  7. #include "avimod.h"
  8. //===============================================================================================================================================================================================================
  9. // FUNCTIONS
  10. //===============================================================================================================================================================================================================
  11. // Flips the specified image and crops it to the specified dimensions
  12. // If scaled == true, all values are scaled to the range [0.0, 1.0
  13. fp* chop_flip_image( char *image,
  14. int height,
  15. int width,
  16. int cropped,
  17. int scaled,
  18. int converted) {
  19. // fixed dimensions for cropping or not cropping, square vertices starting from initial point in top left corner going down and right
  20. int top;
  21. int bottom;
  22. int left;
  23. int right;
  24. if(cropped==1){
  25. top = 0;
  26. bottom = 0;
  27. left = 0;
  28. right = 0;
  29. }
  30. else{
  31. top = 0;
  32. bottom = height - 1;
  33. left = 0;
  34. right = width - 1;
  35. }
  36. // dimensions of new cropped image
  37. int height_new = bottom - top + 1;
  38. int width_new = right - left + 1;
  39. // counters
  40. int i, j;
  41. // allocate memory for cropped/flipped frame
  42. fp* result = (fp *) malloc(height_new * width_new * sizeof(fp));
  43. // crop/flip and scale frame
  44. fp temp;
  45. if (scaled) {
  46. fp scale = 1.0 / 255.0;
  47. for(i = 0; i <height_new; i++){ // rows
  48. for(j = 0; j <width_new; j++){ // colums
  49. temp = (fp) image[((height - 1 - (i + top)) * width) + (j + left)] * scale;
  50. if(temp<0){
  51. result[i*width_new+j] = temp + 256;
  52. }
  53. else{
  54. result[i*width_new+j] = temp;
  55. }
  56. }
  57. }
  58. } else {
  59. for(i = 0; i <height_new; i++){ // rows
  60. for(j = 0; j <width_new; j++){ // colums
  61. temp = (fp) image[((height - 1 - (i + top)) * width) + (j + left)] ;
  62. if(temp<0){
  63. result[i*width_new+j] = temp + 256;
  64. }
  65. else{
  66. result[i*width_new+j] = temp;
  67. }
  68. }
  69. }
  70. }
  71. // convert storage method (from row-major to column-major)
  72. fp* result_converted = (fp *) malloc(height_new * width_new * sizeof(fp));
  73. if(converted==1){
  74. for(i = 0; i <width_new; i++){ // rows
  75. for(j = 0; j <height_new; j++){ // colums
  76. result_converted[i*height_new+j] = result[j*width_new+i];
  77. }
  78. }
  79. }
  80. else{
  81. result_converted = result;
  82. }
  83. free(result);
  84. // return
  85. return result_converted;
  86. }
  87. // Returns the specified frame from the specified video file
  88. // If cropped == true, the frame is cropped to pre-determined dimensions
  89. // (hardcoded to the boundaries of the blood vessel in the test video)
  90. // If scaled == true, all values are scaled to the range [0.0, 1.0]
  91. fp* get_frame( avi_t* cell_file,
  92. int frame_num,
  93. int cropped,
  94. int scaled,
  95. int converted) {
  96. // variable
  97. int dummy;
  98. int width = AVI_video_width(cell_file);
  99. int height = AVI_video_height(cell_file);
  100. int status;
  101. // There are 600 frames in this file (i.e. frame_num = 600 causes an error)
  102. AVI_set_video_position(cell_file, frame_num);
  103. //Read in the frame from the AVI
  104. char* image_buf = (char*) malloc(width * height * sizeof(char));
  105. status = AVI_read_frame(cell_file, image_buf, &dummy);
  106. if(status == -1) {
  107. AVI_print_error((char*) "Error with AVI_read_frame");
  108. exit(-1);
  109. }
  110. // The image is read in upside-down, so we need to flip it
  111. fp* image_chopped;
  112. image_chopped = chop_flip_image( image_buf,
  113. height,
  114. width,
  115. cropped,
  116. scaled,
  117. converted);
  118. // free image buffer
  119. free(image_buf);
  120. // return
  121. return image_chopped;
  122. }
  123. // #ifdef __cplusplus
  124. // }
  125. // #endif