Castle Game Engine: CastleImages: Class TCastleImage

# Class TCastleImage
Description | Hierarchy | Fields | Methods | Properties
## Unit CastleImages ## Declaration `type TCastleImage = class(TEncodedImage)` ## Description An abstract class representing image as a simple array of pixels. RawPixels is a pointer to Width * Height * Depth of pixels. What exactly is a "pixel" is undefined in this class. Each descendant of `TCastleImage` defines it's own pixel encoding and interpretation. The only requirement is that all pixels have the same size (PixelSize). For example, for TRGBImage a "pixel" is a TVector3Byte type representing a (red, green, blue) color value. When Depth > 1, the image is actually a 3D (not just 2D!) image. We call the particular 2D layers then "slices". Although some `TCastleImage` methods (and functions in other units, like CastleGLImages) still operate only on the 1st "slice", that is the 2D image on Depth = 0 — be careful. But many methods correctly take the depth into consideration. Pixels in RawPixels are ordered in slices, each slice is ordered in rows, in each row pixels are specified from left to right, rows are specified starting from lower row to upper. This means that you can think of RawPixels as
      ˆ(packed array[0..Depth - 1, 0..Height - 1, 0..Width - 1] of TPixel)
Assuming the above definition, RawPixelsˆ[z, y, x] is color of pixel at position z, x, y. Note that specifying rows from lower to upper follows an OpenGL standard, this makes using this unit with OpenGL straightforward. Don't ever operate on RawPixels pointer directly — allocating, reallocating, freeing memory pointed to by RawPixels is handled inside this class. You must only worry to always free created `TCastleImage` instances (like with any class). Note that the only valid states of instances of this class are when (Width * Height * Depth > 0 and RawPixels <> nil) or (Width * Height * Depth = 0 and RawPixels = nil). Otherwise the fundamental assumption that RawPixels is a pointer to Width * Height * Depth pixels would be broken (as nil pointer cannot point to anything, and on the other side it's rather useless to have a pointer to 0 bytes (since you can never dereference it anyway) even if theoretically every PtrInt value can be treated as valid pointer to 0 bytes). Note about coordinates: All X, Y, Z coordinates of pixels are 0-based (X in range 0..Width-1, and Y in 0..Height-1, and Z in 0..Depth-1). If documentation for some method does not specify otherwise, correctness of coordinates is *not* checked in method, which can lead to various errors at runtime if you will pass incorrect coordinates to given routine. ## Hierarchy TObject TEncodedImage TCastleImage ## Overview ### Methods
| `constructor Create; overload; virtual;`
| `constructor Create( const AWidth, AHeight: Cardinal; const ADepth: Cardinal = 1); overload; virtual;`
| `procedure Empty;`
| `procedure SetSize( const AWidth, AHeight: Cardinal; const ADepth: Cardinal = 1);`
| `class function PixelSize: Cardinal; virtual; abstract;`
| `class function ColorComponentsCount: Cardinal; virtual; abstract;`
| `function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): Pointer;`
| `function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): Pointer;`
| `procedure InvertRGBColors; virtual;`
| `procedure SetColorRGB(const X, Y: Integer; const v: TVector3Single); virtual;`
| `function MakeCopy: TCastleImage;`
| `procedure Resize(ResizeToX, ResizeToY: Cardinal; const Interpolation: TResizeInterpolation = riNearest; const ProgressTitle: string = '');`
| `function MakeResized(ResizeToX, ResizeToY: Cardinal; const Interpolation: TResizeInterpolation = riNearest; const ProgressTitle: string = ''): TCastleImage;`
| `procedure FlipHorizontal;`
| `function MakeRotated(Angle: Integer): TCastleImage;`
| `procedure Rotate(const Angle: Integer);`
| `function MakeTiled(TileX, TileY: Cardinal): TCastleImage;`
| `function MakeExtracted(X0, Y0, ExtractWidth, ExtractHeight: Cardinal): TCastleImage;`
| `procedure Clear(const Pixel: TVector4Byte); virtual;`
| `function IsClear(const Pixel: TVector4Byte): boolean; virtual;`
| `procedure TransformRGB(const Matrix: TMatrix3Single); virtual;`
| `procedure ModulateRGB(const ColorModulator: TColorModulatorByteFunc); virtual;`
| `function MakeModulatedRGB( const ColorModulator: TColorModulatorByteFunc): TCastleImage;`
| `procedure Grayscale;`
| `procedure ConvertToChannelRGB(Channel: Integer);`
| `procedure StripToChannelRGB(Channel: Integer);`
| `function IsEqual(Image: TCastleImage): boolean;`
| `function ArePartsEqual( const SelfX0, SelfY0, SelfWidth, SelfHeight: Cardinal; Image: TCastleImage; const ImageX0, ImageY0, ImageWidth, ImageHeight: Cardinal): boolean; overload;`
| `function ArePartsEqual( Image: TCastleImage; const ImageX0, ImageY0, ImageWidth, ImageHeight: Cardinal): boolean; overload;`
| `function ArePartsEqual( const SelfX0, SelfY0, SelfWidth, SelfHeight: Cardinal; Image: TCastleImage): boolean; overload;`
| `procedure CopyFrom(Image: TCastleImage; const X0, Y0: Cardinal);`
| `procedure CopyFrom(Image: TCastleImage; const X0, Y0: Cardinal; const SourceX0, SourceY0, SourceWidth, SourceHeight: Cardinal);`
| `procedure CopyTo(Image: TCastleImage; const X0, Y0: Cardinal);`
| `procedure LerpWith(const Value: Single; SecondImage: TCastleImage); virtual;`
| `class procedure MixColors(const OutputColor: Pointer; const Weights: TVector4Single; const Colors: TVector4Pointer); virtual;`
## Description ### Methods
| `constructor Create; overload; virtual;`
Constructor without parameters creates image with Width = Height = Depth = 0 and RawPixels = nil, so IsEmpty will return `True`. Both constructors must be virtual, this allows to implement things like TCastleImage.MakeCopy.
| `constructor Create( const AWidth, AHeight: Cardinal; const ADepth: Cardinal = 1); overload; virtual;`
| `procedure Empty;`
This is equivalent to SetSize(0, 0, 0). It sets Width = Height = 0 and RawPixels = nil.
| `procedure SetSize( const AWidth, AHeight: Cardinal; const ADepth: Cardinal = 1);`
Change Width and Height to given AWidth, AHeight. RawPixels is changed to point to the new memory. Previous image contents are lost. (use one of the other methods, like Resize, if you want to change image size preserving it's contents)
| `class function PixelSize: Cardinal; virtual; abstract;`
Size of TPixel in bytes for this TCastleImage descendant.
| `class function ColorComponentsCount: Cardinal; virtual; abstract;`
Number of color components in TPixel. E.g. RGB is 3 components and RGB+Alpha is 4 components, RGB+Exponent is 3 components (because it describes only Red, Green and Blue values (Exponent value is just used to correctly interpret these, it's not a 4th component)).
| `function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): Pointer;`
Pointer to the (x, y, z) pixel of image. Note that they don't check X, Y, Z correctness in any way, it's your responsibility to always pass 0 <= X < Width and 0 <= Y < Height and 0 <= Z < Depth. Note that this function should be reintroduced in descendants to return the same value but typecasted to something better then Pointer (something like ˆTPixel).
| `function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): Pointer;`
Pointer to the first pixel in the Y row of the image. Same thing as PixelPtr but always with X = 0. Note that this function should be reintroduced in descendants to return the same value but typecasted to something better then Pointer, preferably something like ˆ(array of TPixel).
| `procedure InvertRGBColors; virtual;`
This inverts RGB colors (i.e. changes each RGB component's value to High(Byte)-value). Doesn't touch other components, e.g. alpha value in case of TRGBAlphaImage descendant. Note that this may be not overriden in every TCastleImage descendant, then default implementation of this method in this class will raise EInternalError. This also means that you must not call inherited in descendants when overriding this method.
| `procedure SetColorRGB(const X, Y: Integer; const v: TVector3Single); virtual;`
Set the RGB color portion of the pixel. In case of descendants that have more then RGB components, other color components are not touched (e.g. in case of TRGBAlphaImage alpha value of given pixel is not changed). In case of descendants that don't have anything like RGB encoded inside (e.g. TGrayscaleImage), this should not be overriden and then default implementation of this method in this class will raise EInternalError. This also means that you must not call inherited in descendants when overriding this method. As usual, you are responsible for guaranting correctness of given X, Y coordinates because their correctness is not checked here.
| `function MakeCopy: TCastleImage;`
Create a new object that has exactly the same class and the same contents as this object. (note: no, this function is *not* constructor, because it's implemented in TCastleImage, but it always returns some descendant of TCastleImage).
| `procedure Resize(ResizeToX, ResizeToY: Cardinal; const Interpolation: TResizeInterpolation = riNearest; const ProgressTitle: string = '');`
Change Width and Height and appropriately stretch image contents. If ResizeToX or ResizeToY is 0 then it means to take Width or Height, respectively. So e.g. using ResizeToX = ResizeToY = 0 is the same thing as using ResizeToX = Width and ResizeToY = Height and this is NOP. Remember that resizing may change RawPixels pointer, so all pointers that you aquired using functions like RawPixels, RGBPixels, AlphaPixels, RowPtr, PixelPtr may be invalid after calling Resize. If ProgressTitle <> '' this will call Progress.Init/Step/Fini from CastleProgress to indicate progress of operation.
| `function MakeResized(ResizeToX, ResizeToY: Cardinal; const Interpolation: TResizeInterpolation = riNearest; const ProgressTitle: string = ''): TCastleImage;`
Create a new TCastleImage instance with size ResizeToX, ResizeToY and pixels copied from us and appropriately stretched. Class of new instance is the same as our class. As with Resize, ResizeTo* = 0 means to use current Width/Height. So e.g. using `MakeResized`(0, 0) is the same thing as using MakeCopy. As with Resize, if ProgressTitle <> '' this will call Progress.Init/Step/Fini from CastleProgress to indicate progress of operation.
| `procedure FlipHorizontal;`
Mirror image horizotally (i.e. right edge is swapped with left edge)
| `function MakeRotated(Angle: Integer): TCastleImage;`
Make rotated version of the image. See Rotate for description of parameters.
| `procedure Rotate(const Angle: Integer);`
Rotate image by Angle * 90 degrees, clockwise. For example, 0 does nothing. 1 rotates by 90 degrees, 2 rotates by 180, 3 rotates by 270. All other values (negative too) are circular (modulo), so e.g. 4 again does nothing, 5 rotates by 90 degrees and so on.
| `function MakeTiled(TileX, TileY: Cardinal): TCastleImage;`
Create a new instance with the same class, and size TileX * Width and TileY * Height and contents being our contents duplicated (tiled). Must be TileX, TileY > 0.
| `function MakeExtracted(X0, Y0, ExtractWidth, ExtractHeight: Cardinal): TCastleImage;`
Extract rectangular area of this image. X0 and Y0 are start position (lower-left corner), ExtractWidth, ExtractHeight specify size of area. This checks parameters for correctness – if start position in not good or ExtractWidth/Height are too large exception EImagePosOutOfRange is raised.
| `procedure Clear(const Pixel: TVector4Byte); virtual;`
Set all image pixels to the same value. This is implemented only in descendants that represent a pixel as a TVector4Byte (e.g. TRGBAlphaImage) or TVector3Byte (e.g. TRGBImage, 4th component is ignored in this case). In this class this simply raises EInternalError to say 'not implemented'. This also means that you must not call inherited in descendants when overriding this method.
| `function IsClear(const Pixel: TVector4Byte): boolean; virtual;`
Check do all image pixels have the same value Pixel. This is implemented only in descendants that represent a pixel as TVector4Byte or TVector3Byte (4th component is ignored in this case), just like method Clear. In this class this simply raises EInternalError to say 'not implemented'. This also means that you must not call inherited in descendants when overriding this method.
| `procedure TransformRGB(const Matrix: TMatrix3Single); virtual;`
Multiply each RGB color by a matrix. This is a useful routine for many various conversions of image colors. Every pixel's RGB color is multiplied by given Matrix, i.e. PixelRGBColor := Matrix * PixelRGBColor. If some value in some channel will be < 0, it will be set to 0. And if it will be > High(Byte), it will be set to High(Byte). Examples: when Matrix = IdentityMatrix3Single, this is NOOP. Matrix = ((2, 0, 0), (0, 1, 0), (0, 0, 1)) red channel is made lighter. Matrix = ((0, 0, 1), (0, 1, 0), (1, 0, 0)) swaps red and blue channel. Matrix = ((0.33, 0.33, 0.33), (0.33, 0.33, 0.33), (0.33, 0.33, 0.33)) is a simple conversion to grayscale (actually incorrect, even if often visually acceptable; actually instead of 0.33 one has to use GrayscaleFloat/ByteValues, this is already implemented in ImageTransformColorsTo1st function) Note: it's often more optimal to hard-code necessary color transformations as TColorModulatorFunc and use ModulateRGB. This function is only implemented for images that represent Pixel as RGB values, for now this means TRGBImage and TRGBAlphaImage. In case of TRGBAlphaImage (or any other class that represents colors as RGB + something more) alpha channel (i.e. "something more") is ignored (i.e. left without any modification). In this class this simply raises EInternalError to say 'not implemented'. This also means that you must not call inherited in descendants when overriding this method.
| `procedure ModulateRGB(const ColorModulator: TColorModulatorByteFunc); virtual;`
Process each pixel by given function. If ColorModulator = nil then this procedure does nothing. Else, every RGB color value of an image will be transformed using ColorModulator. Like TransformRGB: This function is only implemented for images that represent Pixel as RGB values, for now this means TRGBImage and TRGBAlphaImage. In case of TRGBAlphaImage (or any other class that represents colors as RGB + something more) alpha channel (i.e. "something more") is ignored (i.e. left without any modification). In this class this simply raises EInternalError to say 'not implemented'. This also means that you must not call inherited in descendants when overriding this method.
| `function MakeModulatedRGB( const ColorModulator: TColorModulatorByteFunc): TCastleImage;`
Just like ModulateRGB, but this returns new image, not changing initial image. This means that if ColorModulator = nil this is equivalent to MakeCopy. Implemented if and only if ModulateRGB is implemented.
| `procedure Grayscale;`
Convert image colors to grayscale. Implemented if and only if ModulateRGB is implemented. When image has alpha channel, alpha channel value (or just anything beyond 3 rgb components) is ignored (not modified). This changes color to grayscale, but format of memory storage is the same. For example, for TRGBImage, they are still kept in RGB format (just Red = Green = Blue). If you want to convert to true Grayscale format, you should use TRGBImage.ToGrayscale that will create new TGrayscaleImage instance.
| `procedure ConvertToChannelRGB(Channel: Integer);`
Convert every image color using Color*Convert function from CastleVectors. "Channel" parameter determines which Color*Convert function to use (Red, Green or Blue), must be 0, 1 or 2. Implemented if and only if ModulateRGB is implemented.
| `procedure StripToChannelRGB(Channel: Integer);`
Converts every image color using Color*Strip function from CastleVectors. "Channel" parameter determines which Color*Strip function to use (Red, Green or Blue), must be 0, 1 or 2. Implemented if and only if ModulateRGB is implemented.
| `function IsEqual(Image: TCastleImage): boolean;`
Check if given Image has the same class, the same sizes (Width, Height) and contains exactly the same pixel values.
| `function ArePartsEqual( const SelfX0, SelfY0, SelfWidth, SelfHeight: Cardinal; Image: TCastleImage; const ImageX0, ImageY0, ImageWidth, ImageHeight: Cardinal): boolean; overload;`
This is like IsEqual, but is compares only given parts of the images. Note that it's your responsibility to make sure that given areas are really within the sizes of Self or Image. Overloaded version without SelfXxx parameters compares whole Self to given part of Image. Analogously, version without ImageXxx parameters compares whole Image to part of Self.
| `function ArePartsEqual( Image: TCastleImage; const ImageX0, ImageY0, ImageWidth, ImageHeight: Cardinal): boolean; overload;`
| `function ArePartsEqual( const SelfX0, SelfY0, SelfWidth, SelfHeight: Cardinal; Image: TCastleImage): boolean; overload;`
| `procedure CopyFrom(Image: TCastleImage; const X0, Y0: Cardinal);`
These check that Image and Self have equal classes, and then copy Self to Image or Image to Self. X0 and Y0 is each case are the position on the destinantion image. Optionally you can specify dimensions of rectangle from source image to use (please note that they are assumed correct here; so you better check them, or risk invalid memory reads).
| `procedure CopyFrom(Image: TCastleImage; const X0, Y0: Cardinal; const SourceX0, SourceY0, SourceWidth, SourceHeight: Cardinal);`
| `procedure CopyTo(Image: TCastleImage; const X0, Y0: Cardinal);`
| `procedure LerpWith(const Value: Single; SecondImage: TCastleImage); virtual;`
Makes linear interpolation of colors from this image and the SecondImage. Intuitively, every pixel in new image is set to
      (1 - Value) * Self[pixel] + Value * SecondImage[pixel]
Both images need to have the exact same size. If they are not, EImageLerpDifferentSizes is raised. Not all TCastleImage combinations are allowed. Every subclass is required to override this to at least handle Lerp between itself. That is, TRGBImage.Lerp has to handle Lerp with other TRGBImage, TRGBAlphaImage.Lerp has to handle Lerp with other TRGBAlphaImage etc. Other combinations may be permitted, if useful and implemented. EImageLerpInvalidClasses is raised if given class combinations are not allowed. In this class, this simply always raises EImageLerpInvalidClasses. ###### Exceptions raised EImageLerpDifferentSizes When SecondImage size differs from this image. EImageLerpInvalidClasses When Lerp between this TCastleImage descendant class and SecondImage class is not implemented.
| `class procedure MixColors(const OutputColor: Pointer; const Weights: TVector4Single; const Colors: TVector4Pointer); virtual;`
Mix 4 colors, with 4 weights, into a resulting color. All 4 Colors and OutputColor must be pointers to a pixel of current image class, that is they must point to PixelSize bytes of memory. ###### Exceptions raised EImageLerpInvalidClasses When mixing is not implemented for this image class.
Generated by PasDoc 0.13.0 on 2013-08-17 21:27:12