Advertisement
Guest User

Untitled

a guest
May 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1.  
  2. /****************************************************************************
  3. * Func: fields_warp *
  4. * *
  5. * Desc: warps an image base on the fields warping algorithm presented in *
  6. * Siggraph 1992 proceedings *
  7. * *
  8. * Params: source- pointer to source image *
  9. * ptr- pointer to destination image buffer *
  10. * source_lines- array of source image control lines *
  11. * dest_lines- array of destination image control lines *
  12. * num_lines- number of control lines *
  13. * num_frames- number of frames in warp sequence *
  14. * outfile- base name of output file *
  15. * rows- number of rows in image *
  16. * cols- number of cols in image *
  17. * type- image type *
  18. ****************************************************************************/
  19.  
  20. void fields_warp(void *source_ptr, void *dest_ptr, LINE *source_lines, LINE *dest_lines,
  21. int num_lines, int num_frames, char *outfile, int rows, int cols,
  22. int type)
  23. {
  24. int frame; /* current frame number */
  25. int line; /* current line number */
  26. int x, y; /* destination coordinate indices */
  27. int source_x, source_y; /* source coordinate indices */
  28. float u, v; /* intermediate source coordinate indices */
  29. char suffix[3]; /* terminating output filename string */
  30. char file_name[100]; /* name of output file */
  31. unsigned long source_index; /* index into linear addressed source image */
  32. unsigned long dest_index; /* index into linear addressed destination */
  33. LINE warp_lines[100]; /* array of intermediate control lines */
  34. float fweight; /* frame weight used to interpolate lines */
  35. int last_row, last_col; /* last valid row and column */
  36. float fraction, distance; /* fraction along and distance from control line */
  37. float fdist; /* intermediate variable used to compute distance */
  38. int dx, dy; /* delta X and delta Y */
  39. float weight_sum; /* accumulated weight */
  40. double numerator; /* numerator used to compute line weight */
  41. double denominator; /* denominator used to compute line weight */
  42. float sum_x, sum_y; /* delta sum X and Y */
  43. float weight; /* line weight */
  44. float a=0.001; /* small number used to avoid divide by 0 */
  45. float b=2.0; /* exponent used in line weight calculation */
  46. float p=0.75; /* exponent used in line weight calculation */
  47. image_ptr sourcei; /* source pointer for gray-scale image */
  48. pixel_ptr sourcep; /* source pointer for color image */
  49. pixel_ptr destp; /* destination pointer for color image */
  50. image_ptr desti; /* destination pointer for gray-scale */
  51.  
  52. last_row = rows-1; /* initialize last row */
  53. last_col = cols-1; /* initialize last column */
  54.  
  55.  
  56. /* setup for a color or gray scale image */
  57. if(type == 5)
  58. {
  59. sourcei = (image_ptr) source_ptr;
  60. desti = (image_ptr) dest_ptr;
  61. }
  62. else
  63. {
  64. sourcep = (pixel_ptr) source_ptr;
  65. destp = (pixel_ptr) dest_ptr;
  66. }
  67.  
  68. for(frame=1; frame<=num_frames; frame++)
  69. {
  70. fweight = (float)(frame) / num_frames;
  71.  
  72. /* interpolate intermediate control lines */
  73. for(line=0; line<num_lines; line++)
  74. {
  75. warp_lines[line].P.x=source_lines[line].P.x +
  76. ((dest_lines[line].P.x - source_lines[line].P.x)*fweight);
  77. warp_lines[line].P.y=source_lines[line].P.y +
  78. ((dest_lines[line].P.y - source_lines[line].P.y)*fweight);
  79. warp_lines[line].Q.x=source_lines[line].Q.x +
  80. ((dest_lines[line].Q.x - source_lines[line].Q.x)*fweight);
  81. warp_lines[line].Q.y=source_lines[line].Q.y +
  82. ((dest_lines[line].Q.y - source_lines[line].Q.y)*fweight);
  83. warp_lines[line].dx = warp_lines[line].Q.x - warp_lines[line].P.x;
  84. warp_lines[line].dy = warp_lines[line].Q.y - warp_lines[line].P.y;
  85. warp_lines[line].length_squared =
  86. (long) (warp_lines[line].dx) * warp_lines[line].dx +
  87. (long) (warp_lines[line].dy) * warp_lines[line].dy;
  88. warp_lines[line].length = sqrt(warp_lines[line].length_squared);
  89. }
  90.  
  91. for(y=0; y<rows; y++)
  92. {
  93. printf("processing frame %d: line %d of %d\n", frame, y, rows);
  94. for(x=0; x<cols; x++)
  95. {
  96. weight_sum = 0.0;
  97. sum_x = 0.0;
  98. sum_y = 0.0;
  99. for (line = 0; line < num_lines; line++)
  100. {
  101. dx = x - warp_lines[line].P.x;
  102. dy = y - warp_lines[line].P.y;
  103. fraction = (dx * (long) warp_lines[line].dx +
  104. dy * (long) warp_lines[line].dy) /
  105. (float) warp_lines[line].length_squared;
  106. fdist = (dy * (long) warp_lines[line].dx -
  107. dx * (long) warp_lines[line].dy) /
  108. warp_lines[line].length;
  109. if (fraction <= 0 )
  110. distance = sqrt(dx * (long) dx + dy * (long) dy);
  111. else if (fraction >= 1)
  112. {
  113. dx = x - warp_lines[line].Q.x;
  114. dy = y - warp_lines[line].Q.y;
  115. distance = sqrt(dx * (long) dx + dy * (long) dy);
  116. }
  117. else if (fdist >= 0)
  118. distance = fdist;
  119. else
  120. distance = -1.0 * fdist;
  121. u = source_lines[line].P.x +
  122. fraction * source_lines[line].dx -
  123. fdist * source_lines[line].dy /
  124. source_lines[line].length;
  125. v = source_lines[line].P.y +
  126. fraction * source_lines[line].dy +
  127. fdist * source_lines[line].dx /
  128. source_lines[line].length;
  129.  
  130. /* calculate line weight */
  131. numerator = pow((double)(warp_lines[line].length),p);
  132. denominator = a + distance;
  133. weight = pow((numerator / denominator) , b);
  134.  
  135. sum_x += (u - x) * weight;
  136. sum_y += (v - y) * weight;
  137. weight_sum += weight;
  138. }
  139.  
  140. source_x = x + sum_x / weight_sum + 0.5;
  141. source_y = y + sum_y / weight_sum + 0.5;
  142. CLIP(source_x, 0, last_col);
  143. CLIP(source_y, 0, last_row);
  144. source_index = source_x + (unsigned long) cols * source_y;
  145. dest_index = x + (unsigned long) cols * y;
  146. if(type == 5)
  147. desti[dest_index] = sourcei[source_index];
  148. else
  149. {
  150. destp[dest_index].r = sourcep[source_index].r;
  151. destp[dest_index].g = sourcep[source_index].g;
  152. destp[dest_index].b = sourcep[source_index].b;
  153. }
  154. }
  155. }
  156. suffix[0]='.';
  157. itoa(frame, &suffix[1], 10);
  158. suffix[2]=0;
  159. strcpy(file_name, outfile);
  160. strcat(file_name, suffix);
  161. printf("Writing out frame %d\n",frame);
  162. if(type == 5)
  163. write_pnm(desti, file_name, rows, cols, type);
  164. else
  165. write_pnm((image_ptr) destp, file_name, rows, cols, type);
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement