Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
Visualization of the dynamic programming algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKniephoff committed Jan 18, 2011
1 parent 1071f22 commit a9ae054
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,61 @@ energize (SDL_Surface * image, energy_function f)
return result;
}

/*
* TODO Think about the tradeoff between storing the predecessor index vs. horizontal offset:
* index makes "backtracking" faster...
*/
char *
dynamic_program (float * array, int width, int height)
{
char *pred = malloc (sizeof (char) * width * (height - 1));
float min;
char min_pos;
float *preds = array;
float *pixel = array + width;
char *pixel_pred = pred;
for (int y = 1; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
if (x > 0)
{
min = preds[-1];
min_pos = -1;
}
else
{
min = preds[0];
min_pos = 0;
}

if (preds[0] < min)
{
++min_pos;
min = preds[0];
}

if (x < width - 1)
{
if (preds[1] < min)
{
++min_pos;
min = preds[1];
}
}

*pixel_pred = min_pos;
++pixel_pred;
*pixel += min;
++pixel;

++preds;
}
}

return pred;
}

SDL_Surface *
energy_to_surface (float *array, Uint32 flags, int width,
int height, int bitsPerPixel, Uint32 Rmask,
Expand Down Expand Up @@ -137,6 +192,7 @@ main (int argc, char *argv[])
Uint32 black = SDL_MapRGB (screen->format, 0, 0, 0);
SDL_Event event;
float *array = energize (image, gradient_magnitude);
free (dynamic_program (array, image->w, image->h));
SDL_Surface *vis =
energy_to_surface (array, image->flags, image->w, image->h,
image->format->BitsPerPixel, image->format->Rmask,
Expand Down

0 comments on commit a9ae054

Please sign in to comment.