Class TPixelSurface

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type TPixelSurface = class(TConceptualPixelSurface)

Description

Surface that stores pixels in one of supported formats, with facilities for pixel format conversion, resizing, copying, drawing, shrinking and so on. This can serve as a base for more advanced hardware-based surfaces, but it also provides full software implementation for all the functions.

Hierarchy

Overview

Fields

Protected FBits: Pointer;
Protected FBufferSize: Cardinal;
Protected FBytesPerPixel: Cardinal;
Protected FHeight: Integer;
Protected FPitch: Cardinal;
Protected FPixelFormat: TPixelFormat;
Protected FWidth: Integer;

Methods

Public constructor Create; virtual;
Public constructor CreateNamed(const AName: StdString);
Public function ApproximatePixelFormat(const NewPixelFormat: TPixelFormat): TPixelFormat; virtual;
Public function ComposeWhiteBlackToGrayAlpha(const SourceWhite, SourceBlack: TPixelSurface): Boolean;
Public function ConvertPixelFormat(const NewPixelFormat: TPixelFormat): Boolean; virtual;
Public function CopyFrom(const Source: TPixelSurface): Boolean;
Public function CopyRect(DestPos: TPoint2i; const Source: TPixelSurface; SourceRect: TIntRect): Boolean;
Public function GetBilinearPixel(const X, Y, FracX, FracY: Integer): TIntColor; overload;
Public function GetBilinearPixel(const X, Y: VectorFloat): TIntColor; overload;
Public function GetPixelWithCheck(X, Y: Integer): TIntColor; inline;
Public function HasAlphaChannel: Boolean;
Public function IsEmpty: Boolean;
Public function SetSize(NewWidth, NewHeight: Integer; NewPixelFormat: TPixelFormat = TPixelFormat.Unknown): Boolean; overload;
Public function SetSize(const NewSize: TPoint2i; const NewPixelFormat: TPixelFormat = TPixelFormat.Unknown): Boolean; overload; inline;
Public function ShrinkToHalfFrom(const Source: TPixelSurface): Boolean;
Protected function GetPixel(X, Y: Integer): TIntColor; override;
Protected function GetPixelUnsafe(X, Y: Integer): TIntColor; override;
Protected function Reallocate(const NewWidth, NewHeight: Integer; const NewPixelFormat: TPixelFormat): Boolean; virtual;
Public procedure Clear; overload;
Public procedure Clear(const Color: TIntColor); overload;
Public procedure DrawFilledRect(DestRect: TIntRect; const Colors: TColorRect);
Public procedure DrawPixel(const X, Y: Integer; const Color: TIntColor); override;
Public procedure DrawSurface(DestPos: TPoint2i; const Source: TPixelSurface; SourceRect: TIntRect; const Colors: TColorRect);
Public procedure FillRect(Rect: TIntRect; const Color: TIntColor); overload;
Public procedure FillRect(const X, Y, Width, Height: Integer; const Color: TIntColor); overload; inline;
Public procedure Flip;
Public procedure FrameRect(const X, Y, Width, Height: Integer; const Color: TIntColor); overload; inline;
Public procedure FrameRect(const Rect: TIntRect; const Color: TIntColor); overload;
Public procedure HorizLine(X, Y, LineWidth: Integer; const Color: TIntColor);
Public procedure Mirror;
Public procedure PremultiplyAlpha;
Public procedure ResetAlpha(const Opaque: Boolean = True);
Public procedure StretchBilinearFrom(const Source: TPixelSurface; DestRect, SourceRect: TFloatRect);
Public procedure StretchFrom(const Source: TPixelSurface; DestRect, SourceRect: TFloatRect);
Public procedure UnpremultiplyAlpha;
Public procedure VertLine(X, Y, LineHeight: Integer; const Color: TIntColor);
Protected procedure ResetAllocation; virtual;
Protected procedure SetPixel(X, Y: Integer; const Color: TIntColor); override;
Protected procedure SetPixelUnsafe(X, Y: Integer; const Color: TIntColor); override;

Properties

Public property Bits: Pointer read FBits;
Public property BufferSize: Cardinal read FBufferSize;
Public property BytesPerPixel: Cardinal read FBytesPerPixel;
Public property Height: Integer read FHeight;
Public property Name: StdString read FName;
Public property Pitch: Cardinal read FPitch;
Public property PixelFormat: TPixelFormat read FPixelFormat;
Public property PixelPtr[constX,Y:Integer]: Pointer read GetPixelPtr;
Public property PremultipliedAlpha: Boolean read FPremultipliedAlpha write FPremultipliedAlpha;
Public property Scanline[constIndex:Integer]: Pointer read GetScanline;
Public property Size: TPoint2i read GetSize;
Public property Width: Integer read FWidth;

Description

Fields

Protected FBits: Pointer;

Memory reference to top/left corner of pixel data contained by this surface, with horizontal rows arranged linearly from top to bottom.

Protected FBufferSize: Cardinal;

Size of current surface in bytes.

Protected FBytesPerPixel: Cardinal;

Current number of bytes each pixel occupies.

Protected FHeight: Integer;

Current height of surface in pixels.

Protected FPitch: Cardinal;

Currently set number of bytes each horizontal row of pixels occupies. This may differ than the actual calculated number and may include unused or even protected memory locations, which should simply be skipped.

Protected FPixelFormat: TPixelFormat;

Current pixel format in which pixels are stored.

Protected FWidth: Integer;

Current width of surface in pixels.

Methods

Public constructor Create; virtual;

Creates new instance of this class with empty name.

Public constructor CreateNamed(const AName: StdString);

Creates new instance of this class with the specified name.

Public function ApproximatePixelFormat(const NewPixelFormat: TPixelFormat): TPixelFormat; virtual;

Takes the provided pixel format and returns one of pixel formats currently supported, which is a closest match to the provided one. If there is no possible match, this function returns TPixelFormat.Unknown.

Public function ComposeWhiteBlackToGrayAlpha(const SourceWhite, SourceBlack: TPixelSurface): Boolean;

Takes first source image with content rendered on white background and second source image with content rendered on black background (discarding alpha-channels, if such are present), calculating resulting grayscale image with alpha-channel. This is useful in situations when an application does not generate alpha-channel, but can pre-render the image on different backgrounds - by rendering this image on black and white backgrounds, the actual alpha-channel can then be calculated using this function.

Public function ConvertPixelFormat(const NewPixelFormat: TPixelFormat): Boolean; virtual;

Converts surface from its currently set pixel format to the new one. If both format match, the function does nothing. True is returned when the operation was successful and False otherwise.

Public function CopyFrom(const Source: TPixelSurface): Boolean;

Copies entire contents from source surface to this one. If the current surface has size and/or pixel format not specified, these will be copied from the source surface as well. If current surface is not empty, then its pixel format will not be modified - in this case, pixel format conversion may occur. This function will try to ensure that current surface size matches the source surface and if if this cannot be achieved, will fail; as an alternative, CopyRect can be used to instead copy a portion of source surface to this one. True is returned when the operation was successful and False otherwise.

Public function CopyRect(DestPos: TPoint2i; const Source: TPixelSurface; SourceRect: TIntRect): Boolean;

Copies a portion of source surface to this one according to specified source rectangle and destination position. If source rectangle is empty, then the entire source surface will be copied. This function does the appropriate clipping and pixel format conversion. It does not change current surface size or pixel format. True is returned when the operation was successful and False otherwise.

Public function GetBilinearPixel(const X, Y, FracX, FracY: Integer): TIntColor; overload;

Retrieves pixel from integer and fractional coordinates. This works similarly to other GetBilinearPixel variant that receives floating-point coordinates, but this one considers FracX and FracY as values ranging from 0 to 255, representing fractions of X and Y.

Public function GetBilinearPixel(const X, Y: VectorFloat): TIntColor; overload;

Retrieves pixel from floating-point coordinates, interpolating linearly between four neighbor pixels as necessary to get an accurate match. This can be used for limitless stretching, to get color values that lie between individual pixels and slowly change from one pixel to another.

Public function GetPixelWithCheck(X, Y: Integer): TIntColor; inline;

Returns color value of pixel at the specified coordinates with an additional sanity check: if the coordinates are outside of valid range, they will be clamped so that they stay within.

Public function HasAlphaChannel: Boolean;

Processes the whole surface to determine whether it has meaningful alpha-channel. A surface that has all its pixels with alpha-channel set to fully translucent or fully opaque (but not mixed) is considered lacking alpha-channel. On the other hand, a surface that has at least one pixel with alpha-channel value different than any other pixel, is considered to have alpha-channel. This is useful to determine whether the surface can be stored in one of pixel formats lacking alpha-channel, to avoid losing any transparency information.

Public function IsEmpty: Boolean;

Checks whether the surface has non-zero width and height.

Public function SetSize(NewWidth, NewHeight: Integer; NewPixelFormat: TPixelFormat = TPixelFormat.Unknown): Boolean; overload;

Redefines surface size to the specified width, height and pixel format, discarding previous contents. This function provide sanity check on specified parameters and calls Reallocate accordingly. True is returned when the operation has been successful and False otherwise.

Public function SetSize(const NewSize: TPoint2i; const NewPixelFormat: TPixelFormat = TPixelFormat.Unknown): Boolean; overload; inline;

Redefines surface size to the specified size and pixel format, discarding previous contents. This function provide sanity check on specified parameters and calls Reallocate accordingly. True is returned when the operation has been successful and False otherwise.

Public function ShrinkToHalfFrom(const Source: TPixelSurface): Boolean;

This function works similarly to CopyFrom, except that it produces image with half of size, averaging each four pixels to one. This is specifically useful to generate mipmaps.

Protected function GetPixel(X, Y: Integer): TIntColor; override;

Reads pixel from the surface and provides necessary pixel format conversion based on parameters such as FBits, FPitch, FPixelFormat, FBytesPerPixel, FWidth and FHeight. This function does range checking for X and Y parameters and if they are outside of valid range, returns completely black/translucent color (in other words, zero).

Protected function GetPixelUnsafe(X, Y: Integer): TIntColor; override;

Reads pixel from the surface similarly to GetPixel, but does not do any range checking for X and Y with the benefit of increased performance.

Protected function Reallocate(const NewWidth, NewHeight: Integer; const NewPixelFormat: TPixelFormat): Boolean; virtual;

Reallocates current pixel surface to new size and pixel format, discarding any previous written content. This function returns True when the operation was successful and False otherwise.

Public procedure Clear; overload;

Clears the entire surface with zeros.

Public procedure Clear(const Color: TIntColor); overload;

Clears the entire surface with the given color. This does pixel format conversion when appropriate, so for better performance, consider using Clear without parameters.

Public procedure DrawFilledRect(DestRect: TIntRect; const Colors: TColorRect);

Draws a rectangle filled with specified gradient onto this surface with alpha-blending. For filling areas with the same color without alpha-blending, a better performance can be achieved with FillRect.

Public procedure DrawPixel(const X, Y: Integer; const Color: TIntColor); override;

Draws a single pixel on this surface with alpha-blending.

Public procedure DrawSurface(DestPos: TPoint2i; const Source: TPixelSurface; SourceRect: TIntRect; const Colors: TColorRect);

Renders a portion of source surface onto this one at the specified origin, using alpha-blending and premultiplying pixels taken from the source surface with specified color gradient. This does pixel pixel conversion and clipping as necessary.

Public procedure FillRect(Rect: TIntRect; const Color: TIntColor); overload;

Fills specified rectangle area with the given color. This also does clipping when appropriate. Note that unlike DrawFilledRect method, this just sets pixels to given color, without alpha-blending.

Public procedure FillRect(const X, Y, Width, Height: Integer; const Color: TIntColor); overload; inline;

Fills rectangle area of the specified coordinates with the given color. This also does clipping when appropriate. Note that unlike DrawFilledRect method, this just sets pixels to given color, without alpha-blending.

Public procedure Flip;

Flips the visible image on surface vertically.

Public procedure FrameRect(const X, Y, Width, Height: Integer; const Color: TIntColor); overload; inline;

Fills surface with rectangle of one pixel wide and single color at the specified coordinates.

Public procedure FrameRect(const Rect: TIntRect; const Color: TIntColor); overload;

Fills surface with rectangle of one pixel wide and single color at the specified area.

Public procedure HorizLine(X, Y, LineWidth: Integer; const Color: TIntColor);

Fills surface with horizontal line of single color at the specified coordinates.

Public procedure Mirror;

Mirrors the visible image on surface horizontally.

Public procedure PremultiplyAlpha;

Processes the whole surface, premultiplying each pixel's red, green and blue values by the corresponding alpha-channel value, resulting in image with premultiplied alpha. Note that this is an irreversible process, during which some color information is lost permanently (smaller alpha values contribute to bigger information loss). This is generally useful to prepare the image for generating mipmaps and/or alpha-blending, to get more accurate visual results.

Public procedure ResetAlpha(const Opaque: Boolean = True);

Processes surface pixels, setting alpha-channel to either fully translucent or fully opaque depending on Opaque parameter.

Public procedure StretchBilinearFrom(const Source: TPixelSurface; DestRect, SourceRect: TFloatRect);

This function works similarly to CopyRect, but provides bilinear stretching. That is, it copies source surface rectangle onto destination rectangle with linear filtering (uses GetBilinearPixel function). Clipping and pixel format conversion is done as necessary. Note that this function is meant for stretching only; shrinking although will also work, but result in inaccurate results as shrinking requires calculating average of variable number of pixels depending on shrink ratio.

Public procedure StretchFrom(const Source: TPixelSurface; DestRect, SourceRect: TFloatRect);

This function works similarly to CopyRect, but provides stretching and/or shrinking. That is, it copies source surface rectangle onto destination rectangle with point filtering. Clipping and pixel format conversion is done as necessary. For a more accurate stretching, consider using StretchBilinearFrom.

Public procedure UnpremultiplyAlpha;

Processes the whole surface, dividing each pixel by its alpha-value, resulting in image with non-premultiplied alpha. This can be considered an opposite or reversal process of PremultiplyAlpha. During this process, some color information may be lost due to precision issues. This can be useful to obtain original pixel information from image that has been previously premultiplied; however, this does not recover lost information during premultiplication process. For instance, pixels that had alpha value of zero and were premultiplied lose all information and cannot be recovered; pixels with alpha value of 128 (that is, 50% opaque) lose half of their precision and after "unpremultiply" process will have values multiple of 2.

Public procedure VertLine(X, Y, LineHeight: Integer; const Color: TIntColor);

Fills surface with vertical line of single color at the specified coordinates.

Protected procedure ResetAllocation; virtual;

Resets pixel surface allocation, releasing any previously allocated memory and setting all relevant parameters to zero.

Protected procedure SetPixel(X, Y: Integer; const Color: TIntColor); override;

Writes pixel to the surface and provides necessary pixel format conversion based on parameters such as FBits, FPitch, FPixelFormat, FBytesPerPixel, FWidth and FHeight. This function does range checking for X and Y parameters and if they are outside of valid range, does nothing.

Protected procedure SetPixelUnsafe(X, Y: Integer; const Color: TIntColor); override;

Write pixel to the surface similarly to SetPixel, but does not do any range checking for X and Y with the benefit of increased performance.

Properties

Public property Bits: Pointer read FBits;

Pointer to top/left corner of pixel data contained by this surface, with horizontal rows arranged linearly from top to bottom.

Public property BufferSize: Cardinal read FBufferSize;

Size of the surface in bytes.

Public property BytesPerPixel: Cardinal read FBytesPerPixel;

Number of bytes each pixel occupies.

Public property Height: Integer read FHeight;

Height of surface in pixels.

Public property Name: StdString read FName;

Unique name of this surface.

Public property Pitch: Cardinal read FPitch;

The number of bytes each horizontal row of pixels occupies. This may differ than the actual calculated number and may include unusued or even protected memory locations, which should simply be skipped.

Public property PixelFormat: TPixelFormat read FPixelFormat;

Pixel format in which individual pixels are stored.

Public property PixelPtr[constX,Y:Integer]: Pointer read GetPixelPtr;

Provides pointer to the pixel data at the given coordinates. If the specified coordinates are outside of valid range, Nil is returned.

Public property PremultipliedAlpha: Boolean read FPremultipliedAlpha write FPremultipliedAlpha;

Indicates whether the pixels in this surface have their alpha premultiplied or not. This is just an informative parameter; to actually convert pixels from one mode to another, use PremultiplyAlpha and UnpremultiplyAlpha methods.

Public property Scanline[constIndex:Integer]: Pointer read GetScanline;

Provides pointer to left corner of pixel data at the given scanline index (that is, row number). If the specified index is outside of valid range, Nil is returned.

Public property Size: TPoint2i read GetSize;

Size of surface in pixels.

Public property Width: Integer read FWidth;

Width of surface in pixels.


Copyright © 2000 - 2017 Yuriy Kotsarenko. Help files generated by PasDoc.