Speedup track preview widget for byte images
This gives a huge speedup gain for cases when you've got rather huge markers on a byte images. Done by skipping IMB_float_from_rect()/IMB_rect_from_float() for such cases. We can sample the buffers without color space conversion.
This commit is contained in:
		
							
								
								
									
										59
									
								
								extern/libmv/libmv-capi.cc
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										59
									
								
								extern/libmv/libmv-capi.cc
									
									
									
									
										vendored
									
									
								
							@@ -158,6 +158,19 @@ static void imageToFloatBuf(const libmv::FloatImage *image, int channels, float
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void imageToByteBuf(const libmv::FloatImage *image, int channels, unsigned char *buf)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int x, y, k, a = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (y = 0; y < image->Height(); y++) {
 | 
				
			||||||
 | 
							for (x = 0; x < image->Width(); x++) {
 | 
				
			||||||
 | 
								for (k = 0; k < channels; k++) {
 | 
				
			||||||
 | 
									buf[a++] = (*image)(y, x, k) * 255.0f;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(DUMP_FAILURE) || defined (DUMP_ALWAYS)
 | 
					#if defined(DUMP_FAILURE) || defined (DUMP_ALWAYS)
 | 
				
			||||||
static void savePNGImage(png_bytep *row_pointers, int width, int height, int depth, int color_type,
 | 
					static void savePNGImage(png_bytep *row_pointers, int width, int height, int depth, int color_type,
 | 
				
			||||||
                         const char *file_name)
 | 
					                         const char *file_name)
 | 
				
			||||||
@@ -385,10 +398,12 @@ int libmv_trackRegion(const libmv_TrackRegionOptions *options,
 | 
				
			|||||||
	return tracking_result;
 | 
						return tracking_result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void libmv_samplePlanarPatch(const float *image, int width, int height,
 | 
					void libmv_samplePlanarPatch(const float *image,
 | 
				
			||||||
                             int channels, const double *xs, const double *ys,
 | 
					                             int width, int height, int channels,
 | 
				
			||||||
 | 
					                             const double *xs, const double *ys,
 | 
				
			||||||
                             int num_samples_x, int num_samples_y,
 | 
					                             int num_samples_x, int num_samples_y,
 | 
				
			||||||
                             const float *mask, float *patch,
 | 
					                             const float *mask,
 | 
				
			||||||
 | 
					                             float *patch,
 | 
				
			||||||
                             double *warped_position_x, double *warped_position_y)
 | 
					                             double *warped_position_x, double *warped_position_y)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	libmv::FloatImage libmv_image, libmv_patch, libmv_mask;
 | 
						libmv::FloatImage libmv_image, libmv_patch, libmv_mask;
 | 
				
			||||||
@@ -402,13 +417,45 @@ void libmv_samplePlanarPatch(const float *image, int width, int height,
 | 
				
			|||||||
		libmv_mask_for_sample = &libmv_mask;
 | 
							libmv_mask_for_sample = &libmv_mask;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	libmv::SamplePlanarPatch(libmv_image, xs, ys, num_samples_x, num_samples_y,
 | 
						libmv::SamplePlanarPatch(libmv_image, xs, ys,
 | 
				
			||||||
	                         libmv_mask_for_sample, &libmv_patch,
 | 
						                         num_samples_x, num_samples_y,
 | 
				
			||||||
	                         warped_position_x, warped_position_y);
 | 
						                         libmv_mask_for_sample,
 | 
				
			||||||
 | 
						                         &libmv_patch,
 | 
				
			||||||
 | 
						                         warped_position_x,
 | 
				
			||||||
 | 
						                         warped_position_y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	imageToFloatBuf(&libmv_patch, channels, patch);
 | 
						imageToFloatBuf(&libmv_patch, channels, patch);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 void libmv_samplePlanarPatchByte(const unsigned char *image,
 | 
				
			||||||
 | 
					                                  int width, int height, int channels,
 | 
				
			||||||
 | 
					                                  const double *xs, const double *ys,
 | 
				
			||||||
 | 
					                                  int num_samples_x, int num_samples_y,
 | 
				
			||||||
 | 
					                                  const float *mask,
 | 
				
			||||||
 | 
					                                  unsigned char *patch,
 | 
				
			||||||
 | 
					                                  double *warped_position_x, double *warped_position_y)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						libmv::FloatImage libmv_image, libmv_patch, libmv_mask;
 | 
				
			||||||
 | 
						libmv::FloatImage *libmv_mask_for_sample = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						byteBufToImage(image, width, height, channels, &libmv_image);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (mask) {
 | 
				
			||||||
 | 
							floatBufToImage(mask, width, height, 1, &libmv_mask);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							libmv_mask_for_sample = &libmv_mask;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						libmv::SamplePlanarPatch(libmv_image, xs, ys,
 | 
				
			||||||
 | 
						                         num_samples_x, num_samples_y,
 | 
				
			||||||
 | 
						                         libmv_mask_for_sample,
 | 
				
			||||||
 | 
						                         &libmv_patch,
 | 
				
			||||||
 | 
						                         warped_position_x,
 | 
				
			||||||
 | 
						                         warped_position_y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						imageToByteBuf(&libmv_patch, channels, patch);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* ************ Tracks ************ */
 | 
					/* ************ Tracks ************ */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct libmv_Tracks *libmv_tracksNew(void)
 | 
					struct libmv_Tracks *libmv_tracksNew(void)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										17
									
								
								extern/libmv/libmv-capi.h
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										17
									
								
								extern/libmv/libmv-capi.h
									
									
									
									
										vendored
									
									
								
							@@ -64,11 +64,22 @@ int libmv_trackRegion(const libmv_TrackRegionOptions *options,
 | 
				
			|||||||
                      const double *x1, const double *y1,
 | 
					                      const double *x1, const double *y1,
 | 
				
			||||||
                      libmv_TrackRegionResult *result,
 | 
					                      libmv_TrackRegionResult *result,
 | 
				
			||||||
                      double *x2, double *y2);
 | 
					                      double *x2, double *y2);
 | 
				
			||||||
void libmv_samplePlanarPatch(const float *image, int width, int height,
 | 
					void libmv_samplePlanarPatch(const float *image,
 | 
				
			||||||
                             int channels, const double *xs, const double *ys,
 | 
					                             int width, int height,
 | 
				
			||||||
 | 
					                             int channels,
 | 
				
			||||||
 | 
					                             const double *xs, const double *ys,
 | 
				
			||||||
                             int num_samples_x, int num_samples_y,
 | 
					                             int num_samples_x, int num_samples_y,
 | 
				
			||||||
                             const float *mask, float *patch,
 | 
					                             const float *mask,
 | 
				
			||||||
 | 
					                             float *patch,
 | 
				
			||||||
                             double *warped_position_x, double *warped_position_y);
 | 
					                             double *warped_position_x, double *warped_position_y);
 | 
				
			||||||
 | 
					void libmv_samplePlanarPatchByte(const unsigned char *image,
 | 
				
			||||||
 | 
					                                 int width, int height,
 | 
				
			||||||
 | 
					                                 int channels,
 | 
				
			||||||
 | 
					                                 const double *xs, const double *ys,
 | 
				
			||||||
 | 
					                                 int num_samples_x, int num_samples_y,
 | 
				
			||||||
 | 
					                                 const float *mask,
 | 
				
			||||||
 | 
					                                 unsigned char *patch,
 | 
				
			||||||
 | 
					                                 double *warped_position_x, double *warped_position_y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Tracks */
 | 
					/* Tracks */
 | 
				
			||||||
struct libmv_Tracks *libmv_tracksNew(void);
 | 
					struct libmv_Tracks *libmv_tracksNew(void);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								extern/libmv/libmv-capi_stub.cc
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										23
									
								
								extern/libmv/libmv-capi_stub.cc
									
									
									
									
										vendored
									
									
								
							@@ -68,11 +68,24 @@ int libmv_trackRegion(const libmv_TrackRegionOptions * /*options*/,
 | 
				
			|||||||
	return false;
 | 
						return false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void libmv_samplePlanarPatch(const float *image, int width, int height,
 | 
					void libmv_samplePlanarPatch(const float * /*image*/,
 | 
				
			||||||
                             int channels, const double *xs, const double *ys,
 | 
					                             int /*width*/, int /*height*/, int /*channels*/,
 | 
				
			||||||
                             int num_samples_x, int num_samples_y,
 | 
					                             const double * /*xs*/, const double * /*ys*/,
 | 
				
			||||||
                             const float *mask, float *patch,
 | 
					                             int /*num_samples_x*/, int /*num_samples_y*/,
 | 
				
			||||||
                             double *warped_position_x, double *warped_position_y)
 | 
					                             const float * /*mask*/,
 | 
				
			||||||
 | 
					                             float * /*patch*/,
 | 
				
			||||||
 | 
					                             double * /*warped_position_x*/, double * /*warped_position_y*/
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						/* TODO(sergey): implement */
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void libmv_samplePlanarPatch(const unsigned char * /*image*/,
 | 
				
			||||||
 | 
					                             int /*width*/, int /*height*/, int /*channels*/,
 | 
				
			||||||
 | 
					                             const double * /*xs*/, const double * /*ys*/,
 | 
				
			||||||
 | 
					                             int /*num_samples_x*/, int /*num_samples_y*/,
 | 
				
			||||||
 | 
					                             const float * /*mask*/,
 | 
				
			||||||
 | 
					                             unsigned char * /*patch*/,
 | 
				
			||||||
 | 
					                             double * /*warped_position_x*/, double * /*warped_position_y*/
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	/* TODO(sergey): implement */
 | 
						/* TODO(sergey): implement */
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1243,7 +1243,6 @@ void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClip
 | 
				
			|||||||
				scopes->track_disabled = false;
 | 
									scopes->track_disabled = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if (ibuf && (ibuf->rect || ibuf->rect_float)) {
 | 
									if (ibuf && (ibuf->rect || ibuf->rect_float)) {
 | 
				
			||||||
					ImBuf *search_ibuf;
 | 
					 | 
				
			||||||
					MovieTrackingMarker undist_marker = *marker;
 | 
										MovieTrackingMarker undist_marker = *marker;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					if (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
 | 
										if (user->render_flag & MCLIP_PROXY_RENDER_UNDISTORT) {
 | 
				
			||||||
@@ -1261,16 +1260,7 @@ void BKE_movieclip_update_scopes(MovieClip *clip, MovieClipUser *user, MovieClip
 | 
				
			|||||||
						undist_marker.pos[1] /= height * aspy;
 | 
											undist_marker.pos[1] /= height * aspy;
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					search_ibuf = BKE_tracking_get_search_imbuf(ibuf, track, &undist_marker, true, true);
 | 
										scopes->track_search = BKE_tracking_get_search_imbuf(ibuf, track, &undist_marker, true, true);
 | 
				
			||||||
 | 
					 | 
				
			||||||
					if (search_ibuf) {
 | 
					 | 
				
			||||||
						if (!search_ibuf->rect_float) {
 | 
					 | 
				
			||||||
							/* sampling happens in float buffer */
 | 
					 | 
				
			||||||
							IMB_float_from_rect(search_ibuf);
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
						scopes->track_search = search_ibuf;
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
					scopes->undist_marker = undist_marker;
 | 
										scopes->undist_marker = undist_marker;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1981,11 +1981,9 @@ ImBuf *BKE_tracking_sample_pattern(int frame_width, int frame_height, ImBuf *sea
 | 
				
			|||||||
	if (num_samples_x <= 0 || num_samples_y <= 0)
 | 
						if (num_samples_x <= 0 || num_samples_y <= 0)
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pattern_ibuf = IMB_allocImBuf(num_samples_x, num_samples_y, 32, IB_rectfloat);
 | 
						pattern_ibuf = IMB_allocImBuf(num_samples_x, num_samples_y,
 | 
				
			||||||
 | 
						                              32,
 | 
				
			||||||
	if (!search_ibuf->rect_float) {
 | 
						                              search_ibuf->rect_float ? IB_rectfloat : IB_rect);
 | 
				
			||||||
		IMB_float_from_rect(search_ibuf);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tracking_get_marker_coords_for_tracking(frame_width, frame_height, marker, src_pixel_x, src_pixel_y);
 | 
						tracking_get_marker_coords_for_tracking(frame_width, frame_height, marker, src_pixel_x, src_pixel_y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -2015,10 +2013,26 @@ ImBuf *BKE_tracking_sample_pattern(int frame_width, int frame_height, ImBuf *sea
 | 
				
			|||||||
		mask = BKE_tracking_track_get_mask(frame_width, frame_height, track, marker);
 | 
							mask = BKE_tracking_track_get_mask(frame_width, frame_height, track, marker);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	libmv_samplePlanarPatch(search_ibuf->rect_float, search_ibuf->x, search_ibuf->y, 4,
 | 
						if (search_ibuf->rect_float) {
 | 
				
			||||||
	                        src_pixel_x, src_pixel_y, num_samples_x,
 | 
							libmv_samplePlanarPatch(search_ibuf->rect_float,
 | 
				
			||||||
	                        num_samples_y, mask, pattern_ibuf->rect_float,
 | 
							                        search_ibuf->x, search_ibuf->y, 4,
 | 
				
			||||||
	                        &warped_position_x, &warped_position_y);
 | 
							                        src_pixel_x, src_pixel_y,
 | 
				
			||||||
 | 
							                        num_samples_x, num_samples_y,
 | 
				
			||||||
 | 
							                        mask,
 | 
				
			||||||
 | 
							                        pattern_ibuf->rect_float,
 | 
				
			||||||
 | 
							                        &warped_position_x,
 | 
				
			||||||
 | 
							                        &warped_position_y);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							libmv_samplePlanarPatchByte((unsigned char *) search_ibuf->rect,
 | 
				
			||||||
 | 
							                            search_ibuf->x, search_ibuf->y, 4,
 | 
				
			||||||
 | 
							                            src_pixel_x, src_pixel_y,
 | 
				
			||||||
 | 
							                            num_samples_x, num_samples_y,
 | 
				
			||||||
 | 
							                            mask,
 | 
				
			||||||
 | 
							                            (unsigned char *) pattern_ibuf->rect,
 | 
				
			||||||
 | 
							                            &warped_position_x,
 | 
				
			||||||
 | 
							                            &warped_position_y);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (pos) {
 | 
						if (pos) {
 | 
				
			||||||
		pos[0] = warped_position_x;
 | 
							pos[0] = warped_position_x;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1522,9 +1522,9 @@ void ui_draw_but_TRACKPREVIEW(ARegion *ar, uiBut *but, uiWidgetColors *UNUSED(wc
 | 
				
			|||||||
			IMB_freeImBuf(scopes->track_preview);
 | 
								IMB_freeImBuf(scopes->track_preview);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		tmpibuf = BKE_tracking_sample_pattern(scopes->frame_width, scopes->frame_height,
 | 
							tmpibuf = BKE_tracking_sample_pattern(scopes->frame_width, scopes->frame_height,
 | 
				
			||||||
		                                            scopes->track_search, scopes->track,
 | 
							                                      scopes->track_search, scopes->track,
 | 
				
			||||||
		                                            &scopes->undist_marker, true, scopes->use_track_mask,
 | 
							                                      &scopes->undist_marker, true, scopes->use_track_mask,
 | 
				
			||||||
		                                            width, height, scopes->track_pos);
 | 
							                                      width, height, scopes->track_pos);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (tmpibuf) {
 | 
							if (tmpibuf) {
 | 
				
			||||||
			if (tmpibuf->rect_float)
 | 
								if (tmpibuf->rect_float)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user