  Platform eXtended Library (PXL)Units Class Hierarchy Classes, Interfaces, Objects and Records Types Variables Constants Functions and Procedures Identifiers |
Class TPixelSurface
Unit
PXL.Surfaces
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
Methods
 |
constructor Create; virtual; |
 |
constructor CreateNamed(const AName: StdString); |
 |
function ApproximatePixelFormat(const NewPixelFormat: TPixelFormat): TPixelFormat; virtual; |
 |
function ComposeWhiteBlackToGrayAlpha(const SourceWhite, SourceBlack: TPixelSurface): Boolean; |
 |
function ConvertPixelFormat(const NewPixelFormat: TPixelFormat): Boolean; virtual; |
 |
function CopyFrom(const Source: TPixelSurface): Boolean; |
 |
function CopyRect(DestPos: TPoint2i; const Source: TPixelSurface; SourceRect: TIntRect): Boolean; |
 |
function GetBilinearPixel(const X, Y, FracX, FracY: Integer): TIntColor; overload; |
 |
function GetBilinearPixel(const X, Y: VectorFloat): TIntColor; overload; |
 |
function GetPixelWithCheck(X, Y: Integer): TIntColor; inline; |
 |
function HasAlphaChannel: Boolean; |
 |
function IsEmpty: Boolean; |
 |
function SetSize(NewWidth, NewHeight: Integer; NewPixelFormat: TPixelFormat = TPixelFormat.Unknown): Boolean; overload; |
 |
function SetSize(const NewSize: TPoint2i; const NewPixelFormat: TPixelFormat = TPixelFormat.Unknown): Boolean; overload; inline; |
 |
function ShrinkToHalfFrom(const Source: TPixelSurface): Boolean; |
 |
function GetPixel(X, Y: Integer): TIntColor; override; |
 |
function GetPixelUnsafe(X, Y: Integer): TIntColor; override; |
 |
function Reallocate(const NewWidth, NewHeight: Integer; const NewPixelFormat: TPixelFormat): Boolean; virtual; |
 |
procedure Clear; overload; |
 |
procedure Clear(const Color: TIntColor); overload; |
 |
procedure DrawFilledRect(DestRect: TIntRect; const Colors: TColorRect); |
 |
procedure DrawPixel(const X, Y: Integer; const Color: TIntColor); override; |
 |
procedure DrawSurface(DestPos: TPoint2i; const Source: TPixelSurface; SourceRect: TIntRect; const Colors: TColorRect); |
 |
procedure FillRect(Rect: TIntRect; const Color: TIntColor); overload; |
 |
procedure FillRect(const X, Y, Width, Height: Integer; const Color: TIntColor); overload; inline; |
 |
procedure Flip; |
 |
procedure FrameRect(const X, Y, Width, Height: Integer; const Color: TIntColor); overload; inline; |
 |
procedure FrameRect(const Rect: TIntRect; const Color: TIntColor); overload; |
 |
procedure HorizLine(X, Y, LineWidth: Integer; const Color: TIntColor); |
 |
procedure Mirror; |
 |
procedure PremultiplyAlpha; |
 |
procedure ResetAlpha(const Opaque: Boolean = True); |
 |
procedure StretchBilinearFrom(const Source: TPixelSurface; DestRect, SourceRect: TFloatRect); |
 |
procedure StretchFrom(const Source: TPixelSurface; DestRect, SourceRect: TFloatRect); |
 |
procedure UnpremultiplyAlpha; |
 |
procedure VertLine(X, Y, LineHeight: Integer; const Color: TIntColor); |
 |
procedure ResetAllocation; virtual; |
 |
procedure SetPixel(X, Y: Integer; const Color: TIntColor); override; |
 |
procedure SetPixelUnsafe(X, Y: Integer; const Color: TIntColor); override; |
Properties
Description
Fields
 |
FBits: Pointer; |
|
Memory reference to top/left corner of pixel data contained by this surface, with horizontal rows arranged linearly from top to bottom.
|
 |
FBufferSize: Cardinal; |
|
Size of current surface in bytes.
|
 |
FBytesPerPixel: Cardinal; |
|
Current number of bytes each pixel occupies.
|
 |
FHeight: Integer; |
|
Current height of surface in pixels.
|
 |
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.
|
 |
FPixelFormat: TPixelFormat; |
|
Current pixel format in which pixels are stored.
|
 |
FWidth: Integer; |
|
Current width of surface in pixels.
|
Methods
 |
constructor Create; virtual; |
|
Creates new instance of this class with empty name.
|
 |
constructor CreateNamed(const AName: StdString); |
|
Creates new instance of this class with the specified name.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
function IsEmpty: Boolean; |
|
Checks whether the surface has non-zero width and height.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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).
|
 |
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.
|
 |
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.
|
 |
procedure Clear; overload; |
|
Clears the entire surface with zeros.
|
 |
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.
|
 |
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.
|
 |
procedure DrawPixel(const X, Y: Integer; const Color: TIntColor); override; |
|
Draws a single pixel on this surface with alpha-blending.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
procedure Flip; |
|
Flips the visible image on surface vertically.
|
 |
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.
|
 |
procedure FrameRect(const Rect: TIntRect; const Color: TIntColor); overload; |
|
Fills surface with rectangle of one pixel wide and single color at the specified area.
|
 |
procedure HorizLine(X, Y, LineWidth: Integer; const Color: TIntColor); |
|
Fills surface with horizontal line of single color at the specified coordinates.
|
 |
procedure Mirror; |
|
Mirrors the visible image on surface horizontally.
|
 |
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.
|
 |
procedure ResetAlpha(const Opaque: Boolean = True); |
|
Processes surface pixels, setting alpha-channel to either fully translucent or fully opaque depending on Opaque parameter.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
procedure VertLine(X, Y, LineHeight: Integer; const Color: TIntColor); |
|
Fills surface with vertical line of single color at the specified coordinates.
|
 |
procedure ResetAllocation; virtual; |
|
Resets pixel surface allocation, releasing any previously allocated memory and setting all relevant parameters to zero.
|
 |
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.
|
 |
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
 |
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.
|
 |
property BufferSize: Cardinal read FBufferSize; |
|
Size of the surface in bytes.
|
 |
property BytesPerPixel: Cardinal read FBytesPerPixel; |
|
Number of bytes each pixel occupies.
|
 |
property Height: Integer read FHeight; |
|
Height of surface in pixels.
|
 |
property Name: StdString read FName; |
|
Unique name of this surface.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
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.
|
 |
property Size: TPoint2i read GetSize; |
|
Size of surface in pixels.
|
 |
property Width: Integer read FWidth; |
|
Width of surface in pixels.
|
Copyright © 2000 - 2017 Yuriy Kotsarenko. Help files generated by PasDoc.
|