C picture rotation optimization -


this c experts out there..

the first function takes two-dimensional matrix src[dim][dim] representing pixels of image, , rotates 90 degrees destination matrix dst[dim][dim]. second function takes same src[dim][dim] , smoothens image replacing every pixel value average of pixels around (in maximum of 3 × 3 window centered @ pixel).

i need optimize program in account time , cycles, how else able optimize following?:

void rotate(int dim, pixel *src, pixel *dst,)  {     int i, j, nj;     nj = 0;      /* below main computations implementation of rotate. */     (j = 0; j < dim; j++) {         nj = dim-1-j;               /* code motion moved operation outside inner loop */         (i = 0; < dim; i++) {             dst[ridx(nj, i, dim)] = src[ridx(i, j, dim)];         }     } }    /* struct used compute averaged pixel value */ typedef struct {     int red;     int green;     int blue;     int num; } pixel_sum;  /* compute min , max of 2 integers, respectively */ static int minimum(int a, int b)  { return (a < b ? : b); } static int maximum(int a, int b)  { return (a > b ? : b); }  /*   * initialize_pixel_sum - initializes fields of sum 0   */ static void initialize_pixel_sum(pixel_sum *sum)  {     sum->red = sum->green = sum->blue = 0;     sum->num = 0;     return; }  /*   * accumulate_sum - accumulates field values of p in corresponding   * fields of sum   */ static void accumulate_sum(pixel_sum *sum, pixel p)  {     sum->red += (int) p.red;     sum->green += (int) p.green;     sum->blue += (int) p.blue;     sum->num++;     return; }  /*   * assign_sum_to_pixel - computes averaged pixel value in current_pixel   */ static void assign_sum_to_pixel(pixel *current_pixel, pixel_sum sum)  {     current_pixel->red = (unsigned short) (sum.red/sum.num);     current_pixel->green = (unsigned short) (sum.green/sum.num);     current_pixel->blue = (unsigned short) (sum.blue/sum.num);     return; }  /*   * avg - returns averaged pixel value @ (i,j)   */ static pixel avg(int dim, int i, int j, pixel *src)  {     int ii, jj;     pixel_sum sum;     pixel current_pixel;      initialize_pixel_sum(&sum);     for(ii = maximum(i-1, 0); ii <= minimum(i+1, dim-1); ii++)          for(jj = maximum(j-1, 0); jj <= minimum(j+1, dim-1); jj++)              accumulate_sum(&sum, src[ridx(ii, jj, dim)]);      assign_sum_to_pixel(&current_pixel, sum);     return current_pixel; }  void smooth(int dim, pixel *src, pixel *dst)  {     int i, j;       /* below main computations implementation of smooth function. */      (j = 0; j < dim; j++)         (i = 0; < dim; i++)             dst[ridx(i, j, dim)] = avg(dim, i, j, src); } 

i moved dim-1-j outside of inner loop of rotate reduces time , cycles used in program, there else can used either main function?

thanks!

there several oprimizations can do; compiler might best write out yourself. example: moving constant expressions out of loop (you did once; there more places can - don't forget condition checked every iteration too, optimize loop condition in manner too) and, chris pointed out, use pointers increment instead of full array indexing. see function calls can rewritten in-line.

i want point article on stackoverflow matrix multiplication , optimizing use processor cache. in essence first rearranges arrrays memory bocks fit cache, performs operation on blocks, moves next block, , on. may able re-use ideas rotation. see optimizing assembly generated microsoft visual studio compiler


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -