Thread: bribe

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    bribe

    Code:
    /***************************** fuzz ********************************************
    
    This function takes coordinate parameters and fuzzes the graphics display.  It
    his perhaps the slowest of all graphics functions in this program.
    
    *******************************************************************************/
      
      vo OUTPUT :: fuzz (un xl, un yl, un xh, un yh)
      {
        un *master_buffer;
        un *r_add_table,
           *g_add_table,
           *b_add_table,
           *div_table;
    
        un alloc_size = (yh - yl) * (xh - xl);
    
        //  allocate the master buffer
        if ( (master_buffer = (un *) calloc (alloc_size * 4, 4)) == NULL) return;
    
        r_add_table = master_buffer + 0 * alloc_size;
        g_add_table = master_buffer + 1 * alloc_size;
        b_add_table = master_buffer + 2 * alloc_size;
        div_table   = master_buffer + 3 * alloc_size;
        
        //  add the buffers
        for (un u0 = 0; u0 < yh - yl; u0++)
          for (un u1 = 0; u1 < xh - xl; u1++)
          {
            un pix_add = h.vb.g_pixel (u1 + xl, u0 + yl);
            uc r, g, b;
    
            //  over line flow, reword...
            r = (pix_add >> (h.vb.vars.g_bpp + h.vb.vars.b_bpp)) & h.vb.vars.r_max;
            g = (pix_add >>                    h.vb.vars.b_bpp ) & h.vb.vars.g_max;
            b = (pix_add >>                    0               ) & h.vb.vars.b_max;
    
            //  add the original pixel to the add buffers
            *(r_add_table + u0 * (xh - xl) + u1) += r;
            *(g_add_table + u0 * (xh - xl) + u1) += g;
            *(b_add_table + u0 * (xh - xl) + u1) += b;
            *(div_table   + u0 * (xh - xl) + u1) += 1;
    
            //  add up the rgbs to random adjacent/on pixels
            for (un u2 = 0; u2 < 8; u2++)
            {
              un x_t = u1 + (rand () % 3) - 1,
                 y_t = u0 + (rand () % 3) - 1;
    
              if ((x_t < (xh - xl)) &&
                  (y_t < (yh - yl)))
              {
                *(r_add_table + y_t * (xh - xl) + x_t) += r;
                *(g_add_table + y_t * (xh - xl) + x_t) += g;
                *(b_add_table + y_t * (xh - xl) + x_t) += b;
                *(div_table   + y_t * (xh - xl) + x_t) += 1;
              }
            }
          }
        
        for (un u0 = 0; u0 < yh - yl; u0++)
          for (un u1 = 0; u1 < xh - xl; u1++)
          {
            un offset = u0 * (xh - xl) + u1;
    
            *(r_add_table + offset) /= *(div_table + offset);
            *(g_add_table + offset) /= *(div_table + offset);
            *(b_add_table + offset) /= *(div_table + offset);
          }
    
        for (un u0 = 0; u0 < yh - yl; u0++)
          for (un u1 = 0; u1 < xh - xl; u1++)
            P_PIXEL (xl + u1, yl + u0,
              *(r_add_table + u0 * (xh - xl) + u1),
              *(g_add_table + u0 * (xh - xl) + u1),
              *(b_add_table + u0 * (xh - xl) + u1));
    
        h.vb.update (xl, yl, xh, yh);
    
        free (master_buffer);
      }
    if you want to see the next demonstration, optimize this code or else!!!
    hasafraggin shizigishin oppashigger...

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    First 4 fairly obvious, but small, optimizations. Store the value of these 4 operations rather than repeating them.

    Code:
    u0 * (xh - xl) + u1
    y_t * (xh - xl) + x_t
    *(div_table + offset)
    u0 * (xh - xl) + u1)
    And, as an aside, making variables longer than 2 letters does not slow down the runtime performance of your app :P.
    Last edited by SilentStrike; 01-07-2002 at 01:06 AM.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    oh yeah, that's right, i should be using incrementing pointers instead of incrementing offsets... [that really helped my text overlaying procedures]. your suggestion isn't totally correct since those values do vary, but it did trigger that memory. thank you. anything else? i was thinking more along the lines of something algorithmic.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed