Camera tracking integration

===========================

- Fixed bug with swapped width/height used for per-pixel cycle
  when disabling color channel to be used for tracking.
- Marker preview is now using sub-pixel precision.
- Tracking stuff uses the same imbug generation function
  as preview area does. Sub-pixel precision should be ok
  now here too.
This commit is contained in:
Sergey Sharybin 2011-07-02 11:06:30 +00:00
parent 59e815fefd
commit 9cabee006b
3 changed files with 81 additions and 44 deletions

@ -55,8 +55,10 @@ struct MovieTrackingTrack *BKE_tracking_copy_track(struct MovieTrackingTrack *tr
void BKE_tracking_clear_path(struct MovieTrackingTrack *track, int ref_frame, int action);
void BKE_tracking_free(struct MovieTracking *tracking);
struct ImBuf *BKE_tracking_acquire_pattern_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track, struct MovieTrackingMarker *marker, int pos[2]);
struct ImBuf *BKE_tracking_acquire_search_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track, struct MovieTrackingMarker *marker, int pos[2]);
struct ImBuf *BKE_tracking_acquire_pattern_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track,
struct MovieTrackingMarker *marker, int margin, float pos[2], int origin[2]);
struct ImBuf *BKE_tracking_acquire_search_imbuf(struct ImBuf *ibuf, struct MovieTrackingTrack *track,
struct MovieTrackingMarker *marker, int margin, float pos[2], int origin[2]);
struct MovieTrackingContext *BKE_tracking_context_new(struct MovieClip *clip, struct MovieClipUser *user, int backwards);
void BKE_tracking_context_free(struct MovieTrackingContext *context);

@ -422,8 +422,8 @@ static void disable_imbuf_channels(ImBuf *ibuf, MovieTrackingTrack *track)
if((track->flag&(TRACK_DISABLE_RED|TRACK_DISABLE_GREEN|TRACK_DISABLE_BLUE))==0)
return;
for(y= 0; y<ibuf->x; y++) {
for (x= 0; x<ibuf->y; x++) {
for(y= 0; y<ibuf->y; y++) {
for (x= 0; x<ibuf->x; x++) {
int pixel= ibuf->x*y + x;
char *rrgb= (char*)ibuf->rect + pixel*4;
@ -434,7 +434,8 @@ static void disable_imbuf_channels(ImBuf *ibuf, MovieTrackingTrack *track)
}
}
static ImBuf *acquire_area_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker, float min[2], float max[2], int pos[2])
static ImBuf *acquire_area_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
float min[2], float max[2], int margin, float pos[2], int origin[2])
{
ImBuf *tmpibuf;
int x, y;
@ -455,30 +456,39 @@ static ImBuf *acquire_area_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTr
if(x1+w<=x) x1++;
if(y1+h<=y) y1++;
tmpibuf= IMB_allocImBuf(w, h, 32, IB_rect);
IMB_rectcpy(tmpibuf, ibuf, 0, 0, x1, y1, w, h);
tmpibuf= IMB_allocImBuf(w+margin*2, h+margin*2, 32, IB_rect);
IMB_rectcpy(tmpibuf, ibuf, 0, 0, x1-margin, y1-margin, w+margin*2, h+margin*2);
pos[0]= x-x1;
pos[1]= y-y1;
if(pos != NULL) {
pos[0]= x-x1+(marker->pos[0]*ibuf->x-x)+margin;
pos[1]= y-y1+(marker->pos[1]*ibuf->y-y)+margin;
}
if(origin != NULL) {
origin[0]= x1-margin;
origin[1]= y1-margin;
}
disable_imbuf_channels(tmpibuf, track);
return tmpibuf;
}
ImBuf *BKE_tracking_acquire_pattern_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker, int pos[2])
ImBuf *BKE_tracking_acquire_pattern_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
int margin, float pos[2], int origin[2])
{
return acquire_area_imbuf(ibuf, track, marker, track->pat_min, track->pat_max, pos);
return acquire_area_imbuf(ibuf, track, marker, track->pat_min, track->pat_max, margin, pos, origin);
}
ImBuf *BKE_tracking_acquire_search_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker, int pos[2])
ImBuf *BKE_tracking_acquire_search_imbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
int margin, float pos[2], int origin[2])
{
return acquire_area_imbuf(ibuf, track, marker, track->search_min, track->search_max, pos);
return acquire_area_imbuf(ibuf, track, marker, track->search_min, track->search_max, margin, pos, origin);
}
#ifdef WITH_LIBMV
static float *acquire_search_floatbuf(ImBuf *ibuf, MovieTrackingTrack *track, MovieTrackingMarker *marker,
int *width_r, int *height_r, int pos[2])
int *width_r, int *height_r, float pos[2], int origin[2])
{
ImBuf *tmpibuf;
float *pixels, *fp;
@ -487,10 +497,7 @@ static float *acquire_search_floatbuf(ImBuf *ibuf, MovieTrackingTrack *track, Mo
width= (track->search_max[0]-track->search_min[0])*ibuf->x;
height= (track->search_max[1]-track->search_min[1])*ibuf->y;
tmpibuf= IMB_allocImBuf(width, height, 32, IB_rect);
IMB_rectcpy(tmpibuf, ibuf, 0, 0,
(track->search_min[0]+marker->pos[0])*ibuf->x,
(track->search_min[1]+marker->pos[1])*ibuf->y, width, height);
tmpibuf= BKE_tracking_acquire_search_imbuf(ibuf, track, marker, 0, pos, origin);
disable_imbuf_channels(tmpibuf, track);
*width_r= width;
@ -621,11 +628,11 @@ int BKE_tracking_next(MovieTrackingContext *context)
if(marker && (marker->flag&MARKER_DISABLED)==0 && marker->framenr==curfra) {
#ifdef WITH_LIBMV
int width, height, pos[2];
float *patch= acquire_search_floatbuf(ibuf, track, marker, &width, &height, pos);
float *patch_new= acquire_search_floatbuf(ibuf_new, track, marker, &width, &height, pos);
//double x1= pos[0], y1= pos[1];
double x1= -track->search_min[0]*ibuf->x, y1= -track->search_min[1]*ibuf->y;
int width, height, origin[2];
float pos[2];
float *patch= acquire_search_floatbuf(ibuf, track, marker, &width, &height, pos, origin);
float *patch_new= acquire_search_floatbuf(ibuf_new, track, marker, &width, &height, pos, origin);
double x1= pos[0], y1= pos[1];
double x2= x1, y2= y1;
int wndx, wndy;
@ -638,8 +645,8 @@ int BKE_tracking_next(MovieTrackingContext *context)
MovieTrackingMarker marker_new;
memset(&marker_new, 0, sizeof(marker_new));
marker_new.pos[0]= marker->pos[0]+track->search_min[0]+x2/ibuf_new->x;
marker_new.pos[1]= marker->pos[1]+track->search_min[1]+y2/ibuf_new->y;
marker_new.pos[0]= (origin[0]+x2)/ibuf_new->x;
marker_new.pos[1]= (origin[1]+y2)/ibuf_new->y;
if(context->backwards) marker_new.framenr= curfra-1;
else marker_new.framenr= curfra+1;

@ -491,6 +491,28 @@ void draw_clip_main(SpaceClip *sc, ARegion *ar, Scene *scene)
draw_movieclip_cache(sc, ar, clip, scene);
}
static ImBuf *scale_ibuf(ImBuf *ibuf, float zoomx, float zoomy)
{
ImBuf *scaleibuf;
int x, y, w= ibuf->x*zoomx, h= ibuf->y*zoomy;
scaleibuf= IMB_allocImBuf(w, h, 32, IB_rect);
for(y= 0; y<scaleibuf->y; y++) {
for (x= 0; x<scaleibuf->x; x++) {
int pixel= scaleibuf->x*y + x;
int orig_pixel= ibuf->x*(int)(((float)y)/zoomy) + (int)(((float)x)/zoomx);
char *rrgb= (char*)scaleibuf->rect + pixel*4;
char *orig_rrgb= (char*)ibuf->rect + orig_pixel*4;
rrgb[0]= orig_rrgb[0];
rrgb[1]= orig_rrgb[1];
rrgb[2]= orig_rrgb[2];
rrgb[3]= orig_rrgb[3];
}
}
return scaleibuf;
}
void draw_clip_track_widget(const bContext *C, void *trackp, void *userp, void *clipp, rcti *rect)
{
ARegion *ar= CTX_wm_region(C);
@ -516,35 +538,39 @@ void draw_clip_track_widget(const bContext *C, void *trackp, void *userp, void *
ImBuf* ibuf= BKE_movieclip_acquire_ibuf(clip, user);
if(ibuf && ibuf->rect) {
int pos[2];
ImBuf* tmpibuf;
float pos[2];
ImBuf *tmpibuf;
tmpibuf= BKE_tracking_acquire_pattern_imbuf(ibuf, track, marker, pos);
tmpibuf= BKE_tracking_acquire_pattern_imbuf(ibuf, track, marker, 1, pos, NULL);
if(tmpibuf->rect_float)
IMB_rect_from_float(tmpibuf);
if(tmpibuf->rect) {
int a, w, h;
float zoomx, zoomy;
int a;
float zoomx, zoomy, off_x, off_y;
GLint scissor[4];
ImBuf *drawibuf;
w= rect->xmax-rect->xmin;
h= rect->ymax-rect->ymin;
zoomx= ((float)rect->xmax-rect->xmin) / (tmpibuf->x-2);
zoomy= ((float)rect->ymax-rect->ymin) / (tmpibuf->y-2);
zoomx= ((float)w) / tmpibuf->x;
zoomy= ((float)h) / tmpibuf->y;
off_x= ((int)pos[0]-pos[0]-0.5)*zoomx;
off_y= ((int)pos[1]-pos[1]-0.5)*zoomy;
glPushMatrix();
glPixelZoom(zoomx, zoomy);
glaDrawPixelsSafe(rect->xmin, rect->ymin, tmpibuf->x, tmpibuf->y, tmpibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, tmpibuf->rect);
glPixelZoom(1.f, 1.f);
glGetIntegerv(GL_VIEWPORT, scissor);
glScissor(ar->winrct.xmin + (rect->xmin-1), ar->winrct.ymin+(rect->ymin-1), (rect->xmax+1)-(rect->xmin-1), (rect->ymax+1)-(rect->ymin-1));
glTranslatef(rect->xmin+(pos[0]+0.5f)*zoomx, rect->ymin+(pos[1]+0.5f)*zoomy, 0.f);
/* draw content of pattern area */
glScissor(ar->winrct.xmin+rect->xmin, ar->winrct.ymin+rect->ymin, scissor[2], scissor[3]);
drawibuf= scale_ibuf(tmpibuf, zoomx, zoomy);
glaDrawPixelsSafe(off_x+rect->xmin, off_y+rect->ymin, rect->xmax-rect->xmin-off_x, rect->ymax-rect->ymin-off_y, drawibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, drawibuf->rect);
/* draw cross for pizel position */
glTranslatef(off_x+rect->xmin+pos[0]*zoomx, off_y+rect->ymin+pos[1]*zoomy, 0.f);
glScissor(ar->winrct.xmin + (rect->xmin-1), ar->winrct.ymin+(rect->ymin-1), (rect->xmax+1)-(rect->xmin-1), (rect->ymax+1)-(rect->ymin-1));
for(a= 0; a< 2; a++) {
if(a==1) {
@ -557,10 +583,10 @@ void draw_clip_track_widget(const bContext *C, void *trackp, void *userp, void *
}
glBegin(GL_LINES);
glVertex2f(-10, 0);
glVertex2f(10, 0);
glVertex2f(0, -10);
glVertex2f(0, 10);
glVertex2f(-10.0f, 0.0f);
glVertex2f(10.0f, 0.0f);
glVertex2f(0.0f, -10.0f);
glVertex2f(0.0f, 10.0f);
glEnd();
}
@ -569,6 +595,8 @@ void draw_clip_track_widget(const bContext *C, void *trackp, void *userp, void *
glPopMatrix();
glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
IMB_freeImBuf(drawibuf);
}
IMB_freeImBuf(tmpibuf);