Bitmap Scaling and Shearing

The fg_scale() function performs horizontal and vertical scaling of 256-color bitmaps. The scaling is independent in each direction, meaning you can reduce the bitmap in both directions, expand it in both directions, or reduce it in one direction and expand it in the other.

The fg_scale() function has six parameters. The first two specify the names of the source and destination bitmap arrays. The source array contains the original (unscaled) bitmap; the destination array will receive the expanded or reduced (scaled) bitmap. The third and fourth parameters specify the width and height in pixels of the unscaled bitmap, while the fifth and sixth parameters do the same for the resulting scaled bitmap.

Shearing can be thought of as anchoring one corner of a rectangular region and stretching the opposite corner horizontally or vertically. For example, bitmaps containing text or other characters could be sheared horizontally to the right for an italic effect. A sheared bitmap will always be larger than the original image, and it will contain empty triangular areas at its corners. These empty areas will be filled with color 0 pixels, meaning they will be transparent when you display a sheared bitmap.

The fg_shear() function shears 256-color bitmaps and has six parameters. The first two specify the names of the source and destination bitmap arrays. The source array contains the original bitmap; the destination array will receive the sheared bitmap. The third and fourth parameters specify the width and height in pixels of the original bitmap. For horizontal shears, the fifth parameter specifies the resulting width in pixels of the sheared bitmap. For vertical shears, it specifies the resulting height.

The final fg_shear() parameter defines the shear type. Four distinct shear types are available:

  • horizontal left shear (region's bottom edge is stretched to the right)

  • horizontal right shear (region's top edge is stretched to the right)

  • vertical left shear (region's left edge is stretched up)

  • vertical right shear (region's right edge is stretched up)

The type is selected by a value between 0 and 3, respectively corresponding to the above four shear types. For example, shear type 2 represents a vertical left shear.

Note that fg_scale() and fg_shear() work with 256-color bitmaps only. The fg_scaledcb() and fg_sheardcb() functions perform the identical functions with direct color bitmaps. These two functions have the exact same parameters as their 256-color counterparts, but of course the source and destination arrays will contain direct color bitmaps. For high color virtual buffers, the bitmaps are stored two bytes per pixel, with each pixel in an encoded RGB format. For true color virtual buffers, the bitmaps are stored three bytes per pixel, with the blue byte first, then the green byte, and finally the red.

Under some circumstances, you can pass virtual buffer addresses to the bitmap scaling and shearing functions and apply these operations directly on virtual buffers. To do this, the size of the source and destination regions being scaled or sheared must exactly match the size of the source and destination virtual buffers. For instance, if we want to scale a 320x200 image in one virtual buffer to a 160x100 image in another virtual buffer, the source virtual buffer size must be 320x200 and the destination virtual buffer size must be 160x100. For example, we could use the code shown here to scale a 320x200 PCX image residing in a virtual buffer into a second 160x100 virtual buffer.

C/C++:

BYTE *Buffer1;
BYTE *Buffer2;
hVB1 = fg_vballoc(320,200);
fg_vbopen(hVB1);
fg_vbcolors();
fg_showpcx("MOUSE.PCX",FG_AT_XY);
hVB2 = fg_vballoc(160,100);
fg_vbopen(hVB2);
fg_vbcolors();
Buffer1 = (BYTE *)fg_vbaddr(hVB1);
Buffer2 = (BYTE *)fg_vbaddr(hVB2);
fg_scale(Buffer1,Buffer2,320,200,160,100);

Delphi:

var
  Buffer1, Buffer2 : pointer;
begin
  hVB1 := fg_vballoc(320,200);
  fg_vbopen(hVB1);
  fg_vbcolors;
  fg_showpcx('MOUSE.PCX'+chr(0),FG_AT_XY);
  hVB2 := fg_vballoc(160,100);
  fg_vbopen(hVB2);
  fg_vbcolors;
  Buffer1 := fg_vbaddr(hVB1);
  Buffer2 := fg_vbaddr(hVB2);
  fg_scale(Buffer1^,Buffer2^,320,200,160,100);

Visual Basic:

Dim Buffer1 As Long, Buffer2 As Long
hVB1 = fg_vballoc(320, 200)
Call fg_vbopen(hVB1)
Call fg_vbcolors
Call fg_showpcx("MOUSE.PCX", FG_AT_XY)
hVB2 = fg_vballoc(160, 100)
Call fg_vbopen(hVB2)
Call fg_vbcolors
Buffer1 = fg_vbaddr(hVB1)
Buffer2 = fg_vbaddr(hVB2)
Call fg_scale(ByVal Buffer1,ByVal Buffer2,320,200,160,100)

Note how this code uses fg_vbaddr() to retrieve the address of each virtual buffer's drawing surface and pass these to fg_scale(). You can use fg_vbaddr() in this manner only when using Fastgraph's native libraries. If you're creating a DirectX program, you must instead obtain the address of the drawing surfaces with fg_ddlock().

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.