Logo Search packages:      
Sourcecode: race version File versions

CL_Canvas * Blitter::scale_surface_to_canvas ( CL_Surface *  sur,
int  width,
int  height 
) [static]

Creates a new canvas with the given width and height and stretches the source surface onto it

Parameters:
sur The source surface
width The new width of the surface.
height The new height of the surface.
Returns:
A newly created surface, the caller is responsible to delete it.

Definition at line 300 of file blitter.cpp.

Referenced by scale_surface().

{
  Color color;
  CL_SurfaceProvider* provider = sur->get_provider ();
  CL_Canvas* canvas = new CL_Canvas (width, height);

  provider->lock ();
  canvas->lock ();

  unsigned char* sbuffer = static_cast<unsigned char*>(provider->get_data ());
  unsigned char* tbuffer = static_cast<unsigned char*>(canvas->get_data ());
  int pwidth = provider->get_width ();
  int pheight = provider->get_height ();
  
  switch (provider->get_bytes_per_pixel ())
    {
    case 3:
      {
      // We assume that we have the data in RGB888, which might not be
      // the case
      for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
          {
            int ti = (y * width + x) * 4;
            int si = ((y * pheight / height) * pwidth
                  + (x * pwidth / width)) * 3;
            
            tbuffer[ti + 0] = 255; // alpha
            tbuffer[ti + 1] = sbuffer[(si + 0)]; // blue
            tbuffer[ti + 2] = sbuffer[(si + 1)]; // green
            tbuffer[ti + 3] = sbuffer[(si + 2)]; // red
          }
      }
      break;
    case 4:
      {
      // We assume that we have the data in RGBA8888, which might not be
      // the case
      for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
          {
            int ti = (y * width + x) * 4;
            int si = ((y * pheight / height) * pwidth
                  + (x * pwidth / width)) * 4;
            
            tbuffer[ti + 0] = sbuffer[(si + 0)]; // alpha
            tbuffer[ti + 1] = sbuffer[(si + 1)]; // blue
            tbuffer[ti + 2] = sbuffer[(si + 2)]; // green
            tbuffer[ti + 3] = sbuffer[(si + 3)]; // red
          }
      }
      break;
    default:
      // Slow but generic, using get_data () would be better, but would
      // require quite a bit of work
      for (int y = 0; y < height; y++)
      for (int x = 0; x < width; x++)
        {
          // std::cout << "X: " << x << " Y: " << y << std::endl;
          provider->get_pixel (x * provider->get_width () / width ,
                         y * provider->get_height () / height,
                         &color.red, &color.green, &color.blue, &color.alpha);
          // ignoring the source alpha due to get_pixel brokeness... no time to test the patch
//        canvas->draw_pixel (x, y, color.red, color.green, color.blue, color.alpha);
          canvas->draw_pixel (x, y, color.red, color.green, color.blue, 0);
        }
      break;
    }

  canvas->unlock ();
  provider->unlock ();

  return canvas;
}


Generated by  Doxygen 1.6.0   Back to index